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>
9 * Portions of this software were developed by Edward Tomasz Napierala
10 * under sponsorship from the FreeBSD Foundation.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
34 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGES.
40 * CAM Target Layer, a SCSI device emulation subsystem.
42 * Author: Ken Merry <ken@FreeBSD.org>
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/ctype.h>
51 #include <sys/kernel.h>
52 #include <sys/types.h>
53 #include <sys/kthread.h>
55 #include <sys/fcntl.h>
57 #include <sys/module.h>
58 #include <sys/mutex.h>
59 #include <sys/condvar.h>
60 #include <sys/malloc.h>
62 #include <sys/ioccom.h>
63 #include <sys/queue.h>
66 #include <sys/endian.h>
67 #include <sys/sysctl.h>
71 #include <cam/scsi/scsi_all.h>
72 #include <cam/scsi/scsi_cd.h>
73 #include <cam/scsi/scsi_da.h>
74 #include <cam/ctl/ctl_io.h>
75 #include <cam/ctl/ctl.h>
76 #include <cam/ctl/ctl_frontend.h>
77 #include <cam/ctl/ctl_util.h>
78 #include <cam/ctl/ctl_backend.h>
79 #include <cam/ctl/ctl_ioctl.h>
80 #include <cam/ctl/ctl_ha.h>
81 #include <cam/ctl/ctl_private.h>
82 #include <cam/ctl/ctl_debug.h>
83 #include <cam/ctl/ctl_scsi_all.h>
84 #include <cam/ctl/ctl_error.h>
86 struct ctl_softc *control_softc = NULL;
89 * Template mode pages.
93 * Note that these are default values only. The actual values will be
94 * filled in when the user does a mode sense.
96 const static struct scsi_da_rw_recovery_page rw_er_page_default = {
97 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
98 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
99 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE,
100 /*read_retry_count*/0,
101 /*correction_span*/0,
102 /*head_offset_count*/0,
103 /*data_strobe_offset_cnt*/0,
104 /*byte8*/SMS_RWER_LBPERE,
105 /*write_retry_count*/0,
107 /*recovery_time_limit*/{0, 0},
110 const static struct scsi_da_rw_recovery_page rw_er_page_changeable = {
111 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
112 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
113 /*byte3*/SMS_RWER_PER,
114 /*read_retry_count*/0,
115 /*correction_span*/0,
116 /*head_offset_count*/0,
117 /*data_strobe_offset_cnt*/0,
118 /*byte8*/SMS_RWER_LBPERE,
119 /*write_retry_count*/0,
121 /*recovery_time_limit*/{0, 0},
124 const static struct scsi_format_page format_page_default = {
125 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
126 /*page_length*/sizeof(struct scsi_format_page) - 2,
127 /*tracks_per_zone*/ {0, 0},
128 /*alt_sectors_per_zone*/ {0, 0},
129 /*alt_tracks_per_zone*/ {0, 0},
130 /*alt_tracks_per_lun*/ {0, 0},
131 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
132 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
133 /*bytes_per_sector*/ {0, 0},
134 /*interleave*/ {0, 0},
135 /*track_skew*/ {0, 0},
136 /*cylinder_skew*/ {0, 0},
138 /*reserved*/ {0, 0, 0}
141 const static struct scsi_format_page format_page_changeable = {
142 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
143 /*page_length*/sizeof(struct scsi_format_page) - 2,
144 /*tracks_per_zone*/ {0, 0},
145 /*alt_sectors_per_zone*/ {0, 0},
146 /*alt_tracks_per_zone*/ {0, 0},
147 /*alt_tracks_per_lun*/ {0, 0},
148 /*sectors_per_track*/ {0, 0},
149 /*bytes_per_sector*/ {0, 0},
150 /*interleave*/ {0, 0},
151 /*track_skew*/ {0, 0},
152 /*cylinder_skew*/ {0, 0},
154 /*reserved*/ {0, 0, 0}
157 const static struct scsi_rigid_disk_page rigid_disk_page_default = {
158 /*page_code*/SMS_RIGID_DISK_PAGE,
159 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
160 /*cylinders*/ {0, 0, 0},
161 /*heads*/ CTL_DEFAULT_HEADS,
162 /*start_write_precomp*/ {0, 0, 0},
163 /*start_reduced_current*/ {0, 0, 0},
164 /*step_rate*/ {0, 0},
165 /*landing_zone_cylinder*/ {0, 0, 0},
166 /*rpl*/ SRDP_RPL_DISABLED,
167 /*rotational_offset*/ 0,
169 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
170 CTL_DEFAULT_ROTATION_RATE & 0xff},
174 const static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
175 /*page_code*/SMS_RIGID_DISK_PAGE,
176 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
177 /*cylinders*/ {0, 0, 0},
179 /*start_write_precomp*/ {0, 0, 0},
180 /*start_reduced_current*/ {0, 0, 0},
181 /*step_rate*/ {0, 0},
182 /*landing_zone_cylinder*/ {0, 0, 0},
184 /*rotational_offset*/ 0,
186 /*rotation_rate*/ {0, 0},
190 const static struct scsi_da_verify_recovery_page verify_er_page_default = {
191 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE,
192 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2,
194 /*read_retry_count*/0,
195 /*reserved*/{ 0, 0, 0, 0, 0, 0 },
196 /*recovery_time_limit*/{0, 0},
199 const static struct scsi_da_verify_recovery_page verify_er_page_changeable = {
200 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE,
201 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2,
202 /*byte3*/SMS_VER_PER,
203 /*read_retry_count*/0,
204 /*reserved*/{ 0, 0, 0, 0, 0, 0 },
205 /*recovery_time_limit*/{0, 0},
208 const static struct scsi_caching_page caching_page_default = {
209 /*page_code*/SMS_CACHING_PAGE,
210 /*page_length*/sizeof(struct scsi_caching_page) - 2,
211 /*flags1*/ SCP_DISC | SCP_WCE,
213 /*disable_pf_transfer_len*/ {0xff, 0xff},
214 /*min_prefetch*/ {0, 0},
215 /*max_prefetch*/ {0xff, 0xff},
216 /*max_pf_ceiling*/ {0xff, 0xff},
218 /*cache_segments*/ 0,
219 /*cache_seg_size*/ {0, 0},
221 /*non_cache_seg_size*/ {0, 0, 0}
224 const static struct scsi_caching_page caching_page_changeable = {
225 /*page_code*/SMS_CACHING_PAGE,
226 /*page_length*/sizeof(struct scsi_caching_page) - 2,
227 /*flags1*/ SCP_WCE | SCP_RCD,
229 /*disable_pf_transfer_len*/ {0, 0},
230 /*min_prefetch*/ {0, 0},
231 /*max_prefetch*/ {0, 0},
232 /*max_pf_ceiling*/ {0, 0},
234 /*cache_segments*/ 0,
235 /*cache_seg_size*/ {0, 0},
237 /*non_cache_seg_size*/ {0, 0, 0}
240 const static struct scsi_control_page control_page_default = {
241 /*page_code*/SMS_CONTROL_MODE_PAGE,
242 /*page_length*/sizeof(struct scsi_control_page) - 2,
244 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED,
247 /*aen_holdoff_period*/{0, 0},
248 /*busy_timeout_period*/{0, 0},
249 /*extended_selftest_completion_time*/{0, 0}
252 const static struct scsi_control_page control_page_changeable = {
253 /*page_code*/SMS_CONTROL_MODE_PAGE,
254 /*page_length*/sizeof(struct scsi_control_page) - 2,
256 /*queue_flags*/SCP_QUEUE_ALG_MASK | SCP_NUAR,
257 /*eca_and_aen*/SCP_SWP,
259 /*aen_holdoff_period*/{0, 0},
260 /*busy_timeout_period*/{0, 0},
261 /*extended_selftest_completion_time*/{0, 0}
264 #define CTL_CEM_LEN (sizeof(struct scsi_control_ext_page) - 4)
266 const static struct scsi_control_ext_page control_ext_page_default = {
267 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
268 /*subpage_code*/0x01,
269 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
275 const static struct scsi_control_ext_page control_ext_page_changeable = {
276 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
277 /*subpage_code*/0x01,
278 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
284 const static struct scsi_info_exceptions_page ie_page_default = {
285 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
286 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
287 /*info_flags*/SIEP_FLAGS_EWASC,
288 /*mrie*/SIEP_MRIE_NO,
289 /*interval_timer*/{0, 0, 0, 0},
290 /*report_count*/{0, 0, 0, 1}
293 const static struct scsi_info_exceptions_page ie_page_changeable = {
294 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
295 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
296 /*info_flags*/SIEP_FLAGS_EWASC | SIEP_FLAGS_DEXCPT | SIEP_FLAGS_TEST |
299 /*interval_timer*/{0xff, 0xff, 0xff, 0xff},
300 /*report_count*/{0xff, 0xff, 0xff, 0xff}
303 #define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4)
305 const static struct ctl_logical_block_provisioning_page lbp_page_default = {{
306 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
307 /*subpage_code*/0x02,
308 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
310 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
315 /*count*/{0, 0, 0, 0}},
319 /*count*/{0, 0, 0, 0}},
323 /*count*/{0, 0, 0, 0}},
327 /*count*/{0, 0, 0, 0}}
331 const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{
332 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
333 /*subpage_code*/0x02,
334 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
335 /*flags*/SLBPP_SITUA,
336 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
341 /*count*/{0, 0, 0, 0}},
345 /*count*/{0, 0, 0, 0}},
349 /*count*/{0, 0, 0, 0}},
353 /*count*/{0, 0, 0, 0}}
357 const static struct scsi_cddvd_capabilities_page cddvd_page_default = {
358 /*page_code*/SMS_CDDVD_CAPS_PAGE,
359 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2,
367 /*nvol_levels*/{0, 0},
368 /*buffer_size*/{8, 0},
373 /*copy_management*/0,
375 /*rotation_control*/0,
376 /*cur_write_speed*/0,
377 /*num_speed_descr*/0,
380 const static struct scsi_cddvd_capabilities_page cddvd_page_changeable = {
381 /*page_code*/SMS_CDDVD_CAPS_PAGE,
382 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2,
390 /*nvol_levels*/{0, 0},
391 /*buffer_size*/{0, 0},
396 /*copy_management*/0,
398 /*rotation_control*/0,
399 /*cur_write_speed*/0,
400 /*num_speed_descr*/0,
403 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
404 static int worker_threads = -1;
405 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
406 &worker_threads, 1, "Number of worker threads");
407 static int ctl_debug = CTL_DEBUG_NONE;
408 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN,
409 &ctl_debug, 0, "Enabled debug flags");
410 static int ctl_lun_map_size = 1024;
411 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, lun_map_size, CTLFLAG_RWTUN,
412 &ctl_lun_map_size, 0, "Size of per-port LUN map (max LUN + 1)");
414 static int ctl_time_io_secs = CTL_TIME_IO_DEFAULT_SECS;
415 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, time_io_secs, CTLFLAG_RWTUN,
416 &ctl_time_io_secs, 0, "Log requests taking more seconds");
420 * Maximum number of LUNs we support. MUST be a power of 2.
422 #define CTL_DEFAULT_MAX_LUNS 1024
423 static int ctl_max_luns = CTL_DEFAULT_MAX_LUNS;
424 TUNABLE_INT("kern.cam.ctl.max_luns", &ctl_max_luns);
425 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, max_luns, CTLFLAG_RDTUN,
426 &ctl_max_luns, CTL_DEFAULT_MAX_LUNS, "Maximum number of LUNs");
429 * Maximum number of ports registered at one time.
431 #define CTL_DEFAULT_MAX_PORTS 256
432 static int ctl_max_ports = CTL_DEFAULT_MAX_PORTS;
433 TUNABLE_INT("kern.cam.ctl.max_ports", &ctl_max_ports);
434 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, max_ports, CTLFLAG_RDTUN,
435 &ctl_max_ports, CTL_DEFAULT_MAX_LUNS, "Maximum number of ports");
438 * Maximum number of initiators we support.
440 #define CTL_MAX_INITIATORS (CTL_MAX_INIT_PER_PORT * ctl_max_ports)
443 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
444 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
445 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0),
446 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
448 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 10
450 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
452 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
453 static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest);
454 static int ctl_init(void);
455 static int ctl_shutdown(void);
456 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
457 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
458 static void ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
459 static void ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
460 struct ctl_ooa *ooa_hdr,
461 struct ctl_ooa_entry *kern_entries);
462 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
464 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
465 struct ctl_be_lun *be_lun);
466 static int ctl_free_lun(struct ctl_lun *lun);
467 static void ctl_create_lun(struct ctl_be_lun *be_lun);
469 static int ctl_do_mode_select(union ctl_io *io);
470 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
471 uint64_t res_key, uint64_t sa_res_key,
472 uint8_t type, uint32_t residx,
473 struct ctl_scsiio *ctsio,
474 struct scsi_per_res_out *cdb,
475 struct scsi_per_res_out_parms* param);
476 static void ctl_pro_preempt_other(struct ctl_lun *lun,
477 union ctl_ha_msg *msg);
478 static void ctl_hndl_per_res_out_on_other_sc(union ctl_io *io);
479 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
480 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
481 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
482 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
483 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
484 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
486 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
488 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
489 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
490 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
491 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
492 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
493 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2,
495 static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2);
496 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
497 union ctl_io *pending_io, union ctl_io *ooa_io);
498 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
499 union ctl_io *starting_io);
500 static int ctl_check_blocked(struct ctl_lun *lun);
501 static int ctl_scsiio_lun_check(struct ctl_lun *lun,
502 const struct ctl_cmd_entry *entry,
503 struct ctl_scsiio *ctsio);
504 static void ctl_failover_lun(union ctl_io *io);
505 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
506 struct ctl_scsiio *ctsio);
507 static int ctl_scsiio(struct ctl_scsiio *ctsio);
509 static int ctl_target_reset(union ctl_io *io);
510 static void ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx,
511 ctl_ua_type ua_type);
512 static int ctl_lun_reset(union ctl_io *io);
513 static int ctl_abort_task(union ctl_io *io);
514 static int ctl_abort_task_set(union ctl_io *io);
515 static int ctl_query_task(union ctl_io *io, int task_set);
516 static void ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx,
517 ctl_ua_type ua_type);
518 static int ctl_i_t_nexus_reset(union ctl_io *io);
519 static int ctl_query_async_event(union ctl_io *io);
520 static void ctl_run_task(union ctl_io *io);
522 static void ctl_datamove_timer_wakeup(void *arg);
523 static void ctl_done_timer_wakeup(void *arg);
524 #endif /* CTL_IO_DELAY */
526 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
527 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
528 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
529 static void ctl_datamove_remote_write(union ctl_io *io);
530 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
531 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
532 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
533 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
534 ctl_ha_dt_cb callback);
535 static void ctl_datamove_remote_read(union ctl_io *io);
536 static void ctl_datamove_remote(union ctl_io *io);
537 static void ctl_process_done(union ctl_io *io);
538 static void ctl_lun_thread(void *arg);
539 static void ctl_thresh_thread(void *arg);
540 static void ctl_work_thread(void *arg);
541 static void ctl_enqueue_incoming(union ctl_io *io);
542 static void ctl_enqueue_rtr(union ctl_io *io);
543 static void ctl_enqueue_done(union ctl_io *io);
544 static void ctl_enqueue_isc(union ctl_io *io);
545 static const struct ctl_cmd_entry *
546 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
547 static const struct ctl_cmd_entry *
548 ctl_validate_command(struct ctl_scsiio *ctsio);
549 static int ctl_cmd_applicable(uint8_t lun_type,
550 const struct ctl_cmd_entry *entry);
551 static int ctl_ha_init(void);
552 static int ctl_ha_shutdown(void);
554 static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx);
555 static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx);
556 static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx);
557 static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key);
560 * Load the serialization table. This isn't very pretty, but is probably
561 * the easiest way to do it.
563 #include "ctl_ser_table.c"
566 * We only need to define open, close and ioctl routines for this driver.
568 static struct cdevsw ctl_cdevsw = {
569 .d_version = D_VERSION,
572 .d_close = ctl_close,
573 .d_ioctl = ctl_ioctl,
578 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
580 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
582 static moduledata_t ctl_moduledata = {
584 ctl_module_event_handler,
588 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
589 MODULE_VERSION(ctl, 1);
591 static struct ctl_frontend ha_frontend =
595 .shutdown = ctl_ha_shutdown,
601 struct ctl_softc *softc = control_softc;
603 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
604 &softc->othersc_pool) != 0)
606 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) {
607 ctl_pool_free(softc->othersc_pool);
610 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
611 != CTL_HA_STATUS_SUCCESS) {
612 ctl_ha_msg_destroy(softc);
613 ctl_pool_free(softc->othersc_pool);
620 ctl_ha_shutdown(void)
622 struct ctl_softc *softc = control_softc;
623 struct ctl_port *port;
625 ctl_ha_msg_shutdown(softc);
626 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL) != CTL_HA_STATUS_SUCCESS)
628 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS)
630 ctl_pool_free(softc->othersc_pool);
631 while ((port = STAILQ_FIRST(&ha_frontend.port_list)) != NULL) {
632 ctl_port_deregister(port);
633 free(port->port_name, M_CTL);
640 ctl_ha_datamove(union ctl_io *io)
642 struct ctl_lun *lun = CTL_LUN(io);
643 struct ctl_sg_entry *sgl;
644 union ctl_ha_msg msg;
645 uint32_t sg_entries_sent;
646 int do_sg_copy, i, j;
648 memset(&msg.dt, 0, sizeof(msg.dt));
649 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
650 msg.hdr.original_sc = io->io_hdr.original_sc;
651 msg.hdr.serializing_sc = io;
652 msg.hdr.nexus = io->io_hdr.nexus;
653 msg.hdr.status = io->io_hdr.status;
654 msg.dt.flags = io->io_hdr.flags;
657 * We convert everything into a S/G list here. We can't
658 * pass by reference, only by value between controllers.
659 * So we can't pass a pointer to the S/G list, only as many
660 * S/G entries as we can fit in here. If it's possible for
661 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
662 * then we need to break this up into multiple transfers.
664 if (io->scsiio.kern_sg_entries == 0) {
665 msg.dt.kern_sg_entries = 1;
667 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
668 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
670 /* XXX KDM use busdma here! */
671 msg.dt.sg_list[0].addr =
672 (void *)vtophys(io->scsiio.kern_data_ptr);
675 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
676 ("HA does not support BUS_ADDR"));
677 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
679 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
682 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
686 msg.dt.kern_data_len = io->scsiio.kern_data_len;
687 msg.dt.kern_total_len = io->scsiio.kern_total_len;
688 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
689 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
690 msg.dt.sg_sequence = 0;
693 * Loop until we've sent all of the S/G entries. On the
694 * other end, we'll recompose these S/G entries into one
695 * contiguous list before processing.
697 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries;
698 msg.dt.sg_sequence++) {
699 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) /
700 sizeof(msg.dt.sg_list[0])),
701 msg.dt.kern_sg_entries - sg_entries_sent);
702 if (do_sg_copy != 0) {
703 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
704 for (i = sg_entries_sent, j = 0;
705 i < msg.dt.cur_sg_entries; i++, j++) {
707 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
708 msg.dt.sg_list[j].addr = sgl[i].addr;
710 /* XXX KDM use busdma here! */
711 msg.dt.sg_list[j].addr =
712 (void *)vtophys(sgl[i].addr);
715 KASSERT((io->io_hdr.flags &
716 CTL_FLAG_BUS_ADDR) == 0,
717 ("HA does not support BUS_ADDR"));
718 msg.dt.sg_list[j].addr = sgl[i].addr;
720 msg.dt.sg_list[j].len = sgl[i].len;
724 sg_entries_sent += msg.dt.cur_sg_entries;
725 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries);
726 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
727 sizeof(msg.dt) - sizeof(msg.dt.sg_list) +
728 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries,
729 M_WAITOK) > CTL_HA_STATUS_SUCCESS) {
730 io->io_hdr.port_status = 31341;
731 io->scsiio.be_move_done(io);
734 msg.dt.sent_sg_entries = sg_entries_sent;
738 * Officially handover the request from us to peer.
739 * If failover has just happened, then we must return error.
740 * If failover happen just after, then it is not our problem.
743 mtx_lock(&lun->lun_lock);
744 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
746 mtx_unlock(&lun->lun_lock);
747 io->io_hdr.port_status = 31342;
748 io->scsiio.be_move_done(io);
751 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
752 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
754 mtx_unlock(&lun->lun_lock);
758 ctl_ha_done(union ctl_io *io)
760 union ctl_ha_msg msg;
762 if (io->io_hdr.io_type == CTL_IO_SCSI) {
763 memset(&msg, 0, sizeof(msg));
764 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
765 msg.hdr.original_sc = io->io_hdr.original_sc;
766 msg.hdr.nexus = io->io_hdr.nexus;
767 msg.hdr.status = io->io_hdr.status;
768 msg.scsi.scsi_status = io->scsiio.scsi_status;
769 msg.scsi.tag_num = io->scsiio.tag_num;
770 msg.scsi.tag_type = io->scsiio.tag_type;
771 msg.scsi.sense_len = io->scsiio.sense_len;
772 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
773 io->scsiio.sense_len);
774 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
775 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
776 msg.scsi.sense_len, M_WAITOK);
782 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
783 union ctl_ha_msg *msg_info)
785 struct ctl_scsiio *ctsio;
787 if (msg_info->hdr.original_sc == NULL) {
788 printf("%s: original_sc == NULL!\n", __func__);
789 /* XXX KDM now what? */
793 ctsio = &msg_info->hdr.original_sc->scsiio;
794 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
795 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
796 ctsio->io_hdr.status = msg_info->hdr.status;
797 ctsio->scsi_status = msg_info->scsi.scsi_status;
798 ctsio->sense_len = msg_info->scsi.sense_len;
799 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
800 msg_info->scsi.sense_len);
801 ctl_enqueue_isc((union ctl_io *)ctsio);
805 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
806 union ctl_ha_msg *msg_info)
808 struct ctl_scsiio *ctsio;
810 if (msg_info->hdr.serializing_sc == NULL) {
811 printf("%s: serializing_sc == NULL!\n", __func__);
812 /* XXX KDM now what? */
816 ctsio = &msg_info->hdr.serializing_sc->scsiio;
817 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
818 ctl_enqueue_isc((union ctl_io *)ctsio);
822 ctl_isc_announce_lun(struct ctl_lun *lun)
824 struct ctl_softc *softc = lun->ctl_softc;
825 union ctl_ha_msg *msg;
826 struct ctl_ha_msg_lun_pr_key pr_key;
829 if (softc->ha_link != CTL_HA_LINK_ONLINE)
831 mtx_lock(&lun->lun_lock);
832 i = sizeof(msg->lun);
834 i += lun->lun_devid->len;
835 i += sizeof(pr_key) * lun->pr_key_count;
837 mtx_unlock(&lun->lun_lock);
838 msg = malloc(i, M_CTL, M_WAITOK);
839 mtx_lock(&lun->lun_lock);
840 k = sizeof(msg->lun);
842 k += lun->lun_devid->len;
843 k += sizeof(pr_key) * lun->pr_key_count;
849 bzero(&msg->lun, sizeof(msg->lun));
850 msg->hdr.msg_type = CTL_MSG_LUN_SYNC;
851 msg->hdr.nexus.targ_lun = lun->lun;
852 msg->hdr.nexus.targ_mapped_lun = lun->lun;
853 msg->lun.flags = lun->flags;
854 msg->lun.pr_generation = lun->pr_generation;
855 msg->lun.pr_res_idx = lun->pr_res_idx;
856 msg->lun.pr_res_type = lun->pr_res_type;
857 msg->lun.pr_key_count = lun->pr_key_count;
859 if (lun->lun_devid) {
860 msg->lun.lun_devid_len = lun->lun_devid->len;
861 memcpy(&msg->lun.data[i], lun->lun_devid->data,
862 msg->lun.lun_devid_len);
863 i += msg->lun.lun_devid_len;
865 for (k = 0; k < CTL_MAX_INITIATORS; k++) {
866 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0)
869 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key));
872 mtx_unlock(&lun->lun_lock);
873 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
877 if (lun->flags & CTL_LUN_PRIMARY_SC) {
878 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
879 ctl_isc_announce_mode(lun, -1,
880 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
881 lun->mode_pages.index[i].subpage);
887 ctl_isc_announce_port(struct ctl_port *port)
889 struct ctl_softc *softc = port->ctl_softc;
890 union ctl_ha_msg *msg;
893 if (port->targ_port < softc->port_min ||
894 port->targ_port >= softc->port_max ||
895 softc->ha_link != CTL_HA_LINK_ONLINE)
897 i = sizeof(msg->port) + strlen(port->port_name) + 1;
899 i += port->lun_map_size * sizeof(uint32_t);
900 if (port->port_devid)
901 i += port->port_devid->len;
902 if (port->target_devid)
903 i += port->target_devid->len;
904 if (port->init_devid)
905 i += port->init_devid->len;
906 msg = malloc(i, M_CTL, M_WAITOK);
907 bzero(&msg->port, sizeof(msg->port));
908 msg->hdr.msg_type = CTL_MSG_PORT_SYNC;
909 msg->hdr.nexus.targ_port = port->targ_port;
910 msg->port.port_type = port->port_type;
911 msg->port.physical_port = port->physical_port;
912 msg->port.virtual_port = port->virtual_port;
913 msg->port.status = port->status;
915 msg->port.name_len = sprintf(&msg->port.data[i],
916 "%d:%s", softc->ha_id, port->port_name) + 1;
917 i += msg->port.name_len;
919 msg->port.lun_map_len = port->lun_map_size * sizeof(uint32_t);
920 memcpy(&msg->port.data[i], port->lun_map,
921 msg->port.lun_map_len);
922 i += msg->port.lun_map_len;
924 if (port->port_devid) {
925 msg->port.port_devid_len = port->port_devid->len;
926 memcpy(&msg->port.data[i], port->port_devid->data,
927 msg->port.port_devid_len);
928 i += msg->port.port_devid_len;
930 if (port->target_devid) {
931 msg->port.target_devid_len = port->target_devid->len;
932 memcpy(&msg->port.data[i], port->target_devid->data,
933 msg->port.target_devid_len);
934 i += msg->port.target_devid_len;
936 if (port->init_devid) {
937 msg->port.init_devid_len = port->init_devid->len;
938 memcpy(&msg->port.data[i], port->init_devid->data,
939 msg->port.init_devid_len);
940 i += msg->port.init_devid_len;
942 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
948 ctl_isc_announce_iid(struct ctl_port *port, int iid)
950 struct ctl_softc *softc = port->ctl_softc;
951 union ctl_ha_msg *msg;
954 if (port->targ_port < softc->port_min ||
955 port->targ_port >= softc->port_max ||
956 softc->ha_link != CTL_HA_LINK_ONLINE)
958 mtx_lock(&softc->ctl_lock);
959 i = sizeof(msg->iid);
961 if (port->wwpn_iid[iid].name)
962 l = strlen(port->wwpn_iid[iid].name) + 1;
964 msg = malloc(i, M_CTL, M_NOWAIT);
966 mtx_unlock(&softc->ctl_lock);
969 bzero(&msg->iid, sizeof(msg->iid));
970 msg->hdr.msg_type = CTL_MSG_IID_SYNC;
971 msg->hdr.nexus.targ_port = port->targ_port;
972 msg->hdr.nexus.initid = iid;
973 msg->iid.in_use = port->wwpn_iid[iid].in_use;
974 msg->iid.name_len = l;
975 msg->iid.wwpn = port->wwpn_iid[iid].wwpn;
976 if (port->wwpn_iid[iid].name)
977 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l);
978 mtx_unlock(&softc->ctl_lock);
979 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT);
984 ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx,
985 uint8_t page, uint8_t subpage)
987 struct ctl_softc *softc = lun->ctl_softc;
988 union ctl_ha_msg msg;
991 if (softc->ha_link != CTL_HA_LINK_ONLINE)
993 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
994 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
995 page && lun->mode_pages.index[i].subpage == subpage)
998 if (i == CTL_NUM_MODE_PAGES)
1001 /* Don't try to replicate pages not present on this device. */
1002 if (lun->mode_pages.index[i].page_data == NULL)
1005 bzero(&msg.mode, sizeof(msg.mode));
1006 msg.hdr.msg_type = CTL_MSG_MODE_SYNC;
1007 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT;
1008 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT;
1009 msg.hdr.nexus.targ_lun = lun->lun;
1010 msg.hdr.nexus.targ_mapped_lun = lun->lun;
1011 msg.mode.page_code = page;
1012 msg.mode.subpage = subpage;
1013 msg.mode.page_len = lun->mode_pages.index[i].page_len;
1014 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data,
1016 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode),
1021 ctl_isc_ha_link_up(struct ctl_softc *softc)
1023 struct ctl_port *port;
1024 struct ctl_lun *lun;
1025 union ctl_ha_msg msg;
1028 /* Announce this node parameters to peer for validation. */
1029 msg.login.msg_type = CTL_MSG_LOGIN;
1030 msg.login.version = CTL_HA_VERSION;
1031 msg.login.ha_mode = softc->ha_mode;
1032 msg.login.ha_id = softc->ha_id;
1033 msg.login.max_luns = ctl_max_luns;
1034 msg.login.max_ports = ctl_max_ports;
1035 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT;
1036 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login),
1039 STAILQ_FOREACH(port, &softc->port_list, links) {
1040 ctl_isc_announce_port(port);
1041 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1042 if (port->wwpn_iid[i].in_use)
1043 ctl_isc_announce_iid(port, i);
1046 STAILQ_FOREACH(lun, &softc->lun_list, links)
1047 ctl_isc_announce_lun(lun);
1051 ctl_isc_ha_link_down(struct ctl_softc *softc)
1053 struct ctl_port *port;
1054 struct ctl_lun *lun;
1058 mtx_lock(&softc->ctl_lock);
1059 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1060 mtx_lock(&lun->lun_lock);
1061 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) {
1062 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1063 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1065 mtx_unlock(&lun->lun_lock);
1067 mtx_unlock(&softc->ctl_lock);
1068 io = ctl_alloc_io(softc->othersc_pool);
1069 mtx_lock(&softc->ctl_lock);
1071 io->io_hdr.msg_type = CTL_MSG_FAILOVER;
1072 io->io_hdr.nexus.targ_mapped_lun = lun->lun;
1073 ctl_enqueue_isc(io);
1076 STAILQ_FOREACH(port, &softc->port_list, links) {
1077 if (port->targ_port >= softc->port_min &&
1078 port->targ_port < softc->port_max)
1080 port->status &= ~CTL_PORT_STATUS_ONLINE;
1081 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1082 port->wwpn_iid[i].in_use = 0;
1083 free(port->wwpn_iid[i].name, M_CTL);
1084 port->wwpn_iid[i].name = NULL;
1087 mtx_unlock(&softc->ctl_lock);
1091 ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1093 struct ctl_lun *lun;
1094 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus);
1096 mtx_lock(&softc->ctl_lock);
1097 if (msg->hdr.nexus.targ_mapped_lun >= ctl_max_luns ||
1098 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) == NULL) {
1099 mtx_unlock(&softc->ctl_lock);
1102 mtx_lock(&lun->lun_lock);
1103 mtx_unlock(&softc->ctl_lock);
1104 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && msg->ua.ua_set)
1105 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8);
1106 if (msg->ua.ua_all) {
1108 ctl_est_ua_all(lun, iid, msg->ua.ua_type);
1110 ctl_clr_ua_all(lun, iid, msg->ua.ua_type);
1113 ctl_est_ua(lun, iid, msg->ua.ua_type);
1115 ctl_clr_ua(lun, iid, msg->ua.ua_type);
1117 mtx_unlock(&lun->lun_lock);
1121 ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1123 struct ctl_lun *lun;
1124 struct ctl_ha_msg_lun_pr_key pr_key;
1126 ctl_lun_flags oflags;
1129 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1130 mtx_lock(&softc->ctl_lock);
1131 if (targ_lun >= ctl_max_luns ||
1132 (lun = softc->ctl_luns[targ_lun]) == NULL) {
1133 mtx_unlock(&softc->ctl_lock);
1136 mtx_lock(&lun->lun_lock);
1137 mtx_unlock(&softc->ctl_lock);
1138 if (lun->flags & CTL_LUN_DISABLED) {
1139 mtx_unlock(&lun->lun_lock);
1142 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0;
1143 if (msg->lun.lun_devid_len != i || (i > 0 &&
1144 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) {
1145 mtx_unlock(&lun->lun_lock);
1146 printf("%s: Received conflicting HA LUN %d\n",
1147 __func__, targ_lun);
1150 /* Record whether peer is primary. */
1151 oflags = lun->flags;
1152 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) &&
1153 (msg->lun.flags & CTL_LUN_DISABLED) == 0)
1154 lun->flags |= CTL_LUN_PEER_SC_PRIMARY;
1156 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1157 if (oflags != lun->flags)
1158 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1160 /* If peer is primary and we are not -- use data */
1161 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
1162 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) {
1163 lun->pr_generation = msg->lun.pr_generation;
1164 lun->pr_res_idx = msg->lun.pr_res_idx;
1165 lun->pr_res_type = msg->lun.pr_res_type;
1166 lun->pr_key_count = msg->lun.pr_key_count;
1167 for (k = 0; k < CTL_MAX_INITIATORS; k++)
1168 ctl_clr_prkey(lun, k);
1169 for (k = 0; k < msg->lun.pr_key_count; k++) {
1170 memcpy(&pr_key, &msg->lun.data[i],
1172 ctl_alloc_prkey(lun, pr_key.pr_iid);
1173 ctl_set_prkey(lun, pr_key.pr_iid,
1175 i += sizeof(pr_key);
1179 mtx_unlock(&lun->lun_lock);
1180 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n",
1182 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ?
1183 "primary" : "secondary"));
1185 /* If we are primary but peer doesn't know -- notify */
1186 if ((lun->flags & CTL_LUN_PRIMARY_SC) &&
1187 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0)
1188 ctl_isc_announce_lun(lun);
1193 ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1195 struct ctl_port *port;
1196 struct ctl_lun *lun;
1199 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1201 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__,
1202 msg->hdr.nexus.targ_port));
1204 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO);
1205 port->frontend = &ha_frontend;
1206 port->targ_port = msg->hdr.nexus.targ_port;
1207 port->fe_datamove = ctl_ha_datamove;
1208 port->fe_done = ctl_ha_done;
1209 } else if (port->frontend == &ha_frontend) {
1210 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__,
1211 msg->hdr.nexus.targ_port));
1214 printf("%s: Received conflicting HA port %d\n",
1215 __func__, msg->hdr.nexus.targ_port);
1218 port->port_type = msg->port.port_type;
1219 port->physical_port = msg->port.physical_port;
1220 port->virtual_port = msg->port.virtual_port;
1221 port->status = msg->port.status;
1223 free(port->port_name, M_CTL);
1224 port->port_name = strndup(&msg->port.data[i], msg->port.name_len,
1226 i += msg->port.name_len;
1227 if (msg->port.lun_map_len != 0) {
1228 if (port->lun_map == NULL ||
1229 port->lun_map_size * sizeof(uint32_t) <
1230 msg->port.lun_map_len) {
1231 port->lun_map_size = 0;
1232 free(port->lun_map, M_CTL);
1233 port->lun_map = malloc(msg->port.lun_map_len,
1236 memcpy(port->lun_map, &msg->port.data[i], msg->port.lun_map_len);
1237 port->lun_map_size = msg->port.lun_map_len / sizeof(uint32_t);
1238 i += msg->port.lun_map_len;
1240 port->lun_map_size = 0;
1241 free(port->lun_map, M_CTL);
1242 port->lun_map = NULL;
1244 if (msg->port.port_devid_len != 0) {
1245 if (port->port_devid == NULL ||
1246 port->port_devid->len < msg->port.port_devid_len) {
1247 free(port->port_devid, M_CTL);
1248 port->port_devid = malloc(sizeof(struct ctl_devid) +
1249 msg->port.port_devid_len, M_CTL, M_WAITOK);
1251 memcpy(port->port_devid->data, &msg->port.data[i],
1252 msg->port.port_devid_len);
1253 port->port_devid->len = msg->port.port_devid_len;
1254 i += msg->port.port_devid_len;
1256 free(port->port_devid, M_CTL);
1257 port->port_devid = NULL;
1259 if (msg->port.target_devid_len != 0) {
1260 if (port->target_devid == NULL ||
1261 port->target_devid->len < msg->port.target_devid_len) {
1262 free(port->target_devid, M_CTL);
1263 port->target_devid = malloc(sizeof(struct ctl_devid) +
1264 msg->port.target_devid_len, M_CTL, M_WAITOK);
1266 memcpy(port->target_devid->data, &msg->port.data[i],
1267 msg->port.target_devid_len);
1268 port->target_devid->len = msg->port.target_devid_len;
1269 i += msg->port.target_devid_len;
1271 free(port->target_devid, M_CTL);
1272 port->target_devid = NULL;
1274 if (msg->port.init_devid_len != 0) {
1275 if (port->init_devid == NULL ||
1276 port->init_devid->len < msg->port.init_devid_len) {
1277 free(port->init_devid, M_CTL);
1278 port->init_devid = malloc(sizeof(struct ctl_devid) +
1279 msg->port.init_devid_len, M_CTL, M_WAITOK);
1281 memcpy(port->init_devid->data, &msg->port.data[i],
1282 msg->port.init_devid_len);
1283 port->init_devid->len = msg->port.init_devid_len;
1284 i += msg->port.init_devid_len;
1286 free(port->init_devid, M_CTL);
1287 port->init_devid = NULL;
1290 if (ctl_port_register(port) != 0) {
1291 printf("%s: ctl_port_register() failed with error\n",
1295 mtx_lock(&softc->ctl_lock);
1296 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1297 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
1299 mtx_lock(&lun->lun_lock);
1300 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
1301 mtx_unlock(&lun->lun_lock);
1303 mtx_unlock(&softc->ctl_lock);
1307 ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1309 struct ctl_port *port;
1312 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1314 printf("%s: Received IID for unknown port %d\n",
1315 __func__, msg->hdr.nexus.targ_port);
1318 iid = msg->hdr.nexus.initid;
1319 if (port->wwpn_iid[iid].in_use != 0 &&
1320 msg->iid.in_use == 0)
1321 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON);
1322 port->wwpn_iid[iid].in_use = msg->iid.in_use;
1323 port->wwpn_iid[iid].wwpn = msg->iid.wwpn;
1324 free(port->wwpn_iid[iid].name, M_CTL);
1325 if (msg->iid.name_len) {
1326 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0],
1327 msg->iid.name_len, M_CTL);
1329 port->wwpn_iid[iid].name = NULL;
1333 ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1336 if (msg->login.version != CTL_HA_VERSION) {
1337 printf("CTL HA peers have different versions %d != %d\n",
1338 msg->login.version, CTL_HA_VERSION);
1339 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1342 if (msg->login.ha_mode != softc->ha_mode) {
1343 printf("CTL HA peers have different ha_mode %d != %d\n",
1344 msg->login.ha_mode, softc->ha_mode);
1345 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1348 if (msg->login.ha_id == softc->ha_id) {
1349 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id);
1350 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1353 if (msg->login.max_luns != ctl_max_luns ||
1354 msg->login.max_ports != ctl_max_ports ||
1355 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) {
1356 printf("CTL HA peers have different limits\n");
1357 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1363 ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1365 struct ctl_lun *lun;
1367 uint32_t initidx, targ_lun;
1369 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1370 mtx_lock(&softc->ctl_lock);
1371 if (targ_lun >= ctl_max_luns ||
1372 (lun = softc->ctl_luns[targ_lun]) == NULL) {
1373 mtx_unlock(&softc->ctl_lock);
1376 mtx_lock(&lun->lun_lock);
1377 mtx_unlock(&softc->ctl_lock);
1378 if (lun->flags & CTL_LUN_DISABLED) {
1379 mtx_unlock(&lun->lun_lock);
1382 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
1383 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
1384 msg->mode.page_code &&
1385 lun->mode_pages.index[i].subpage == msg->mode.subpage)
1388 if (i == CTL_NUM_MODE_PAGES) {
1389 mtx_unlock(&lun->lun_lock);
1392 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data,
1393 lun->mode_pages.index[i].page_len);
1394 initidx = ctl_get_initindex(&msg->hdr.nexus);
1396 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
1397 mtx_unlock(&lun->lun_lock);
1401 * ISC (Inter Shelf Communication) event handler. Events from the HA
1402 * subsystem come in here.
1405 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
1407 struct ctl_softc *softc = control_softc;
1409 struct ctl_prio *presio;
1410 ctl_ha_status isc_status;
1412 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event));
1413 if (event == CTL_HA_EVT_MSG_RECV) {
1414 union ctl_ha_msg *msg, msgbuf;
1416 if (param > sizeof(msgbuf))
1417 msg = malloc(param, M_CTL, M_WAITOK);
1420 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param,
1422 if (isc_status != CTL_HA_STATUS_SUCCESS) {
1423 printf("%s: Error receiving message: %d\n",
1424 __func__, isc_status);
1430 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type));
1431 switch (msg->hdr.msg_type) {
1432 case CTL_MSG_SERIALIZE:
1433 io = ctl_alloc_io(softc->othersc_pool);
1435 // populate ctsio from msg
1436 io->io_hdr.io_type = CTL_IO_SCSI;
1437 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
1438 io->io_hdr.original_sc = msg->hdr.original_sc;
1439 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
1442 * If we're in serialization-only mode, we don't
1443 * want to go through full done processing. Thus
1446 * XXX KDM add another flag that is more specific.
1448 if (softc->ha_mode != CTL_HA_MODE_XFER)
1449 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
1450 io->io_hdr.nexus = msg->hdr.nexus;
1452 printf("port %u, iid %u, lun %u\n",
1453 io->io_hdr.nexus.targ_port,
1454 io->io_hdr.nexus.initid,
1455 io->io_hdr.nexus.targ_lun);
1457 io->scsiio.tag_num = msg->scsi.tag_num;
1458 io->scsiio.tag_type = msg->scsi.tag_type;
1460 io->io_hdr.start_time = time_uptime;
1461 getbinuptime(&io->io_hdr.start_bt);
1462 #endif /* CTL_TIME_IO */
1463 io->scsiio.cdb_len = msg->scsi.cdb_len;
1464 memcpy(io->scsiio.cdb, msg->scsi.cdb,
1466 if (softc->ha_mode == CTL_HA_MODE_XFER) {
1467 const struct ctl_cmd_entry *entry;
1469 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
1470 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
1472 entry->flags & CTL_FLAG_DATA_MASK;
1474 ctl_enqueue_isc(io);
1477 /* Performed on the Originating SC, XFER mode only */
1478 case CTL_MSG_DATAMOVE: {
1479 struct ctl_sg_entry *sgl;
1482 io = msg->hdr.original_sc;
1484 printf("%s: original_sc == NULL!\n", __func__);
1485 /* XXX KDM do something here */
1488 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
1489 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1491 * Keep track of this, we need to send it back over
1492 * when the datamove is complete.
1494 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1495 if (msg->hdr.status == CTL_SUCCESS)
1496 io->io_hdr.status = msg->hdr.status;
1498 if (msg->dt.sg_sequence == 0) {
1500 getbinuptime(&io->io_hdr.dma_start_bt);
1502 i = msg->dt.kern_sg_entries +
1503 msg->dt.kern_data_len /
1504 CTL_HA_DATAMOVE_SEGMENT + 1;
1505 sgl = malloc(sizeof(*sgl) * i, M_CTL,
1507 io->io_hdr.remote_sglist = sgl;
1508 io->io_hdr.local_sglist =
1509 &sgl[msg->dt.kern_sg_entries];
1511 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
1513 io->scsiio.kern_sg_entries =
1514 msg->dt.kern_sg_entries;
1515 io->scsiio.rem_sg_entries =
1516 msg->dt.kern_sg_entries;
1517 io->scsiio.kern_data_len =
1518 msg->dt.kern_data_len;
1519 io->scsiio.kern_total_len =
1520 msg->dt.kern_total_len;
1521 io->scsiio.kern_data_resid =
1522 msg->dt.kern_data_resid;
1523 io->scsiio.kern_rel_offset =
1524 msg->dt.kern_rel_offset;
1525 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR;
1526 io->io_hdr.flags |= msg->dt.flags &
1529 sgl = (struct ctl_sg_entry *)
1530 io->scsiio.kern_data_ptr;
1532 for (i = msg->dt.sent_sg_entries, j = 0;
1533 i < (msg->dt.sent_sg_entries +
1534 msg->dt.cur_sg_entries); i++, j++) {
1535 sgl[i].addr = msg->dt.sg_list[j].addr;
1536 sgl[i].len = msg->dt.sg_list[j].len;
1539 printf("%s: DATAMOVE: %p,%lu j=%d, i=%d\n",
1540 __func__, sgl[i].addr, sgl[i].len, j, i);
1545 * If this is the last piece of the I/O, we've got
1546 * the full S/G list. Queue processing in the thread.
1547 * Otherwise wait for the next piece.
1549 if (msg->dt.sg_last != 0)
1550 ctl_enqueue_isc(io);
1553 /* Performed on the Serializing (primary) SC, XFER mode only */
1554 case CTL_MSG_DATAMOVE_DONE: {
1555 if (msg->hdr.serializing_sc == NULL) {
1556 printf("%s: serializing_sc == NULL!\n",
1558 /* XXX KDM now what? */
1562 * We grab the sense information here in case
1563 * there was a failure, so we can return status
1564 * back to the initiator.
1566 io = msg->hdr.serializing_sc;
1567 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
1568 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1569 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1570 io->io_hdr.port_status = msg->scsi.port_status;
1571 io->scsiio.kern_data_resid = msg->scsi.kern_data_resid;
1572 if (msg->hdr.status != CTL_STATUS_NONE) {
1573 io->io_hdr.status = msg->hdr.status;
1574 io->scsiio.scsi_status = msg->scsi.scsi_status;
1575 io->scsiio.sense_len = msg->scsi.sense_len;
1576 memcpy(&io->scsiio.sense_data,
1577 &msg->scsi.sense_data,
1578 msg->scsi.sense_len);
1579 if (msg->hdr.status == CTL_SUCCESS)
1580 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1582 ctl_enqueue_isc(io);
1586 /* Preformed on Originating SC, SER_ONLY mode */
1588 io = msg->hdr.original_sc;
1590 printf("%s: original_sc == NULL!\n",
1594 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1595 io->io_hdr.msg_type = CTL_MSG_R2R;
1596 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1597 ctl_enqueue_isc(io);
1601 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
1603 * Performed on the Originating (i.e. secondary) SC in XFER
1606 case CTL_MSG_FINISH_IO:
1607 if (softc->ha_mode == CTL_HA_MODE_XFER)
1608 ctl_isc_handler_finish_xfer(softc, msg);
1610 ctl_isc_handler_finish_ser_only(softc, msg);
1613 /* Preformed on Originating SC */
1614 case CTL_MSG_BAD_JUJU:
1615 io = msg->hdr.original_sc;
1617 printf("%s: Bad JUJU!, original_sc is NULL!\n",
1621 ctl_copy_sense_data(msg, io);
1623 * IO should have already been cleaned up on other
1624 * SC so clear this flag so we won't send a message
1625 * back to finish the IO there.
1627 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
1628 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1630 /* io = msg->hdr.serializing_sc; */
1631 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
1632 ctl_enqueue_isc(io);
1635 /* Handle resets sent from the other side */
1636 case CTL_MSG_MANAGE_TASKS: {
1637 struct ctl_taskio *taskio;
1638 taskio = (struct ctl_taskio *)ctl_alloc_io(
1639 softc->othersc_pool);
1640 ctl_zero_io((union ctl_io *)taskio);
1641 taskio->io_hdr.io_type = CTL_IO_TASK;
1642 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1643 taskio->io_hdr.nexus = msg->hdr.nexus;
1644 taskio->task_action = msg->task.task_action;
1645 taskio->tag_num = msg->task.tag_num;
1646 taskio->tag_type = msg->task.tag_type;
1648 taskio->io_hdr.start_time = time_uptime;
1649 getbinuptime(&taskio->io_hdr.start_bt);
1650 #endif /* CTL_TIME_IO */
1651 ctl_run_task((union ctl_io *)taskio);
1654 /* Persistent Reserve action which needs attention */
1655 case CTL_MSG_PERS_ACTION:
1656 presio = (struct ctl_prio *)ctl_alloc_io(
1657 softc->othersc_pool);
1658 ctl_zero_io((union ctl_io *)presio);
1659 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
1660 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1661 presio->io_hdr.nexus = msg->hdr.nexus;
1662 presio->pr_msg = msg->pr;
1663 ctl_enqueue_isc((union ctl_io *)presio);
1666 ctl_isc_ua(softc, msg, param);
1668 case CTL_MSG_PORT_SYNC:
1669 ctl_isc_port_sync(softc, msg, param);
1671 case CTL_MSG_LUN_SYNC:
1672 ctl_isc_lun_sync(softc, msg, param);
1674 case CTL_MSG_IID_SYNC:
1675 ctl_isc_iid_sync(softc, msg, param);
1678 ctl_isc_login(softc, msg, param);
1680 case CTL_MSG_MODE_SYNC:
1681 ctl_isc_mode_sync(softc, msg, param);
1684 printf("Received HA message of unknown type %d\n",
1686 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1691 } else if (event == CTL_HA_EVT_LINK_CHANGE) {
1692 printf("CTL: HA link status changed from %d to %d\n",
1693 softc->ha_link, param);
1694 if (param == softc->ha_link)
1696 if (softc->ha_link == CTL_HA_LINK_ONLINE) {
1697 softc->ha_link = param;
1698 ctl_isc_ha_link_down(softc);
1700 softc->ha_link = param;
1701 if (softc->ha_link == CTL_HA_LINK_ONLINE)
1702 ctl_isc_ha_link_up(softc);
1706 printf("ctl_isc_event_handler: Unknown event %d\n", event);
1712 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
1715 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data,
1716 src->scsi.sense_len);
1717 dest->scsiio.scsi_status = src->scsi.scsi_status;
1718 dest->scsiio.sense_len = src->scsi.sense_len;
1719 dest->io_hdr.status = src->hdr.status;
1723 ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest)
1726 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data,
1727 src->scsiio.sense_len);
1728 dest->scsi.scsi_status = src->scsiio.scsi_status;
1729 dest->scsi.sense_len = src->scsiio.sense_len;
1730 dest->hdr.status = src->io_hdr.status;
1734 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1736 struct ctl_softc *softc = lun->ctl_softc;
1739 if (initidx < softc->init_min || initidx >= softc->init_max)
1741 mtx_assert(&lun->lun_lock, MA_OWNED);
1742 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1745 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua;
1749 ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua)
1753 mtx_assert(&lun->lun_lock, MA_OWNED);
1754 if (lun->pending_ua[port] == NULL)
1756 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1757 if (port * CTL_MAX_INIT_PER_PORT + i == except)
1759 lun->pending_ua[port][i] |= ua;
1764 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1766 struct ctl_softc *softc = lun->ctl_softc;
1769 mtx_assert(&lun->lun_lock, MA_OWNED);
1770 for (i = softc->port_min; i < softc->port_max; i++)
1771 ctl_est_ua_port(lun, i, except, ua);
1775 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1777 struct ctl_softc *softc = lun->ctl_softc;
1780 if (initidx < softc->init_min || initidx >= softc->init_max)
1782 mtx_assert(&lun->lun_lock, MA_OWNED);
1783 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1786 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua;
1790 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1792 struct ctl_softc *softc = lun->ctl_softc;
1795 mtx_assert(&lun->lun_lock, MA_OWNED);
1796 for (i = softc->port_min; i < softc->port_max; i++) {
1797 if (lun->pending_ua[i] == NULL)
1799 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
1800 if (i * CTL_MAX_INIT_PER_PORT + j == except)
1802 lun->pending_ua[i][j] &= ~ua;
1808 ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx,
1809 ctl_ua_type ua_type)
1811 struct ctl_lun *lun;
1813 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
1814 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) {
1815 mtx_lock(&lun->lun_lock);
1816 ctl_clr_ua(lun, initidx, ua_type);
1817 mtx_unlock(&lun->lun_lock);
1822 ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS)
1824 struct ctl_softc *softc = (struct ctl_softc *)arg1;
1825 struct ctl_lun *lun;
1826 struct ctl_lun_req ireq;
1829 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1;
1830 error = sysctl_handle_int(oidp, &value, 0, req);
1831 if ((error != 0) || (req->newptr == NULL))
1834 mtx_lock(&softc->ctl_lock);
1836 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1838 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
1839 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1840 mtx_unlock(&softc->ctl_lock);
1841 bzero(&ireq, sizeof(ireq));
1842 ireq.reqtype = CTL_LUNREQ_MODIFY;
1843 ireq.reqdata.modify.lun_id = lun->lun;
1844 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0,
1846 if (ireq.status != CTL_LUN_OK) {
1847 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n",
1848 __func__, ireq.status, ireq.error_str);
1850 mtx_lock(&softc->ctl_lock);
1852 mtx_unlock(&softc->ctl_lock);
1859 struct make_dev_args args;
1860 struct ctl_softc *softc;
1863 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1866 make_dev_args_init(&args);
1867 args.mda_devsw = &ctl_cdevsw;
1868 args.mda_uid = UID_ROOT;
1869 args.mda_gid = GID_OPERATOR;
1870 args.mda_mode = 0600;
1871 args.mda_si_drv1 = softc;
1872 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;
2291 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
2293 /* Make sure that we know about this port. */
2294 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) {
2295 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2300 /* Make sure that we know about this LUN. */
2301 mtx_lock(&softc->ctl_lock);
2302 if (targ_lun >= ctl_max_luns ||
2303 (lun = softc->ctl_luns[targ_lun]) == NULL) {
2304 mtx_unlock(&softc->ctl_lock);
2307 * The other node would not send this request to us unless
2308 * received announce that we are primary node for this LUN.
2309 * If this LUN does not exist now, it is probably result of
2310 * a race, so respond to initiator in the most opaque way.
2312 ctl_set_busy(ctsio);
2315 mtx_lock(&lun->lun_lock);
2316 mtx_unlock(&softc->ctl_lock);
2319 * If the LUN is invalid, pretend that it doesn't exist.
2320 * It will go away as soon as all pending I/Os completed.
2322 if (lun->flags & CTL_LUN_DISABLED) {
2323 mtx_unlock(&lun->lun_lock);
2324 ctl_set_busy(ctsio);
2328 entry = ctl_get_cmd_entry(ctsio, NULL);
2329 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
2330 mtx_unlock(&lun->lun_lock);
2334 CTL_LUN(ctsio) = lun;
2335 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
2338 * Every I/O goes into the OOA queue for a
2339 * particular LUN, and stays there until completion.
2342 if (TAILQ_EMPTY(&lun->ooa_queue))
2343 lun->idle_time += getsbinuptime() - lun->last_busy;
2345 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2347 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
2348 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
2350 case CTL_ACTION_BLOCK:
2351 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
2352 TAILQ_INSERT_TAIL(&lun->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.original_sc;
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.original_sc;
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.flags & CTL_FLAG_BLOCKED)
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 ctl_copyin_alloc(void *user_addr, unsigned int len, char *error_str,
2454 size_t error_str_len)
2458 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2460 if (copyin(user_addr, kptr, len) != 0) {
2461 snprintf(error_str, error_str_len, "Error copying %d bytes "
2462 "from user address %p to kernel address %p", len,
2472 ctl_free_args(int num_args, struct ctl_be_arg *args)
2479 for (i = 0; i < num_args; i++) {
2480 free(args[i].kname, M_CTL);
2481 free(args[i].kvalue, M_CTL);
2487 static struct ctl_be_arg *
2488 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
2489 char *error_str, size_t error_str_len)
2491 struct ctl_be_arg *args;
2494 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
2495 error_str, error_str_len);
2500 for (i = 0; i < num_args; i++) {
2501 args[i].kname = NULL;
2502 args[i].kvalue = NULL;
2505 for (i = 0; i < num_args; i++) {
2508 if (args[i].namelen == 0) {
2509 snprintf(error_str, error_str_len, "Argument %d "
2510 "name length is zero", i);
2514 args[i].kname = ctl_copyin_alloc(args[i].name,
2515 args[i].namelen, error_str, error_str_len);
2516 if (args[i].kname == NULL)
2519 if (args[i].kname[args[i].namelen - 1] != '\0') {
2520 snprintf(error_str, error_str_len, "Argument %d "
2521 "name is not NUL-terminated", i);
2525 if (args[i].flags & CTL_BEARG_RD) {
2526 if (args[i].vallen == 0) {
2527 snprintf(error_str, error_str_len, "Argument %d "
2528 "value length is zero", i);
2532 tmpptr = ctl_copyin_alloc(args[i].value,
2533 args[i].vallen, error_str, error_str_len);
2537 if ((args[i].flags & CTL_BEARG_ASCII)
2538 && (tmpptr[args[i].vallen - 1] != '\0')) {
2539 snprintf(error_str, error_str_len, "Argument "
2540 "%d value is not NUL-terminated", i);
2541 free(tmpptr, M_CTL);
2544 args[i].kvalue = tmpptr;
2546 args[i].kvalue = malloc(args[i].vallen,
2547 M_CTL, M_WAITOK | M_ZERO);
2554 ctl_free_args(num_args, args);
2560 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2564 for (i = 0; i < num_args; i++) {
2565 if (args[i].flags & CTL_BEARG_WR)
2566 copyout(args[i].kvalue, args[i].value, args[i].vallen);
2571 * Escape characters that are illegal or not recommended in XML.
2574 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2576 char *end = str + size;
2581 for (; *str && str < end; str++) {
2584 retval = sbuf_printf(sb, "&");
2587 retval = sbuf_printf(sb, ">");
2590 retval = sbuf_printf(sb, "<");
2593 retval = sbuf_putc(sb, *str);
2606 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2608 struct scsi_vpd_id_descriptor *desc;
2611 if (id == NULL || id->len < 4)
2613 desc = (struct scsi_vpd_id_descriptor *)id->data;
2614 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2615 case SVPD_ID_TYPE_T10:
2616 sbuf_printf(sb, "t10.");
2618 case SVPD_ID_TYPE_EUI64:
2619 sbuf_printf(sb, "eui.");
2621 case SVPD_ID_TYPE_NAA:
2622 sbuf_printf(sb, "naa.");
2624 case SVPD_ID_TYPE_SCSI_NAME:
2627 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2628 case SVPD_ID_CODESET_BINARY:
2629 for (i = 0; i < desc->length; i++)
2630 sbuf_printf(sb, "%02x", desc->identifier[i]);
2632 case SVPD_ID_CODESET_ASCII:
2633 sbuf_printf(sb, "%.*s", (int)desc->length,
2634 (char *)desc->identifier);
2636 case SVPD_ID_CODESET_UTF8:
2637 sbuf_printf(sb, "%s", (char *)desc->identifier);
2643 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2646 struct ctl_softc *softc = dev->si_drv1;
2647 struct ctl_port *port;
2648 struct ctl_lun *lun;
2655 retval = ctl_ioctl_io(dev, cmd, addr, flag, td);
2657 case CTL_ENABLE_PORT:
2658 case CTL_DISABLE_PORT:
2659 case CTL_SET_PORT_WWNS: {
2660 struct ctl_port *port;
2661 struct ctl_port_entry *entry;
2663 entry = (struct ctl_port_entry *)addr;
2665 mtx_lock(&softc->ctl_lock);
2666 STAILQ_FOREACH(port, &softc->port_list, links) {
2669 if (port->targ_port < softc->port_min ||
2670 port->targ_port >= softc->port_max)
2675 if ((entry->port_type == CTL_PORT_NONE)
2676 && (entry->targ_port == port->targ_port)) {
2678 * If the user only wants to enable or
2679 * disable or set WWNs on a specific port,
2680 * do the operation and we're done.
2684 } else if (entry->port_type & port->port_type) {
2686 * Compare the user's type mask with the
2687 * particular frontend type to see if we
2694 * Make sure the user isn't trying to set
2695 * WWNs on multiple ports at the same time.
2697 if (cmd == CTL_SET_PORT_WWNS) {
2698 printf("%s: Can't set WWNs on "
2699 "multiple ports\n", __func__);
2708 * XXX KDM we have to drop the lock here, because
2709 * the online/offline operations can potentially
2710 * block. We need to reference count the frontends
2711 * so they can't go away,
2713 if (cmd == CTL_ENABLE_PORT) {
2714 mtx_unlock(&softc->ctl_lock);
2715 ctl_port_online(port);
2716 mtx_lock(&softc->ctl_lock);
2717 } else if (cmd == CTL_DISABLE_PORT) {
2718 mtx_unlock(&softc->ctl_lock);
2719 ctl_port_offline(port);
2720 mtx_lock(&softc->ctl_lock);
2721 } else if (cmd == CTL_SET_PORT_WWNS) {
2722 ctl_port_set_wwns(port,
2723 (entry->flags & CTL_PORT_WWNN_VALID) ?
2725 (entry->flags & CTL_PORT_WWPN_VALID) ?
2726 1 : 0, entry->wwpn);
2731 mtx_unlock(&softc->ctl_lock);
2735 struct ctl_ooa *ooa_hdr;
2736 struct ctl_ooa_entry *entries;
2737 uint32_t cur_fill_num;
2739 ooa_hdr = (struct ctl_ooa *)addr;
2741 if ((ooa_hdr->alloc_len == 0)
2742 || (ooa_hdr->alloc_num == 0)) {
2743 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2744 "must be non-zero\n", __func__,
2745 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2750 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2751 sizeof(struct ctl_ooa_entry))) {
2752 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2753 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2754 __func__, ooa_hdr->alloc_len,
2755 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2760 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2761 if (entries == NULL) {
2762 printf("%s: could not allocate %d bytes for OOA "
2763 "dump\n", __func__, ooa_hdr->alloc_len);
2768 mtx_lock(&softc->ctl_lock);
2769 if ((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0 &&
2770 (ooa_hdr->lun_num >= ctl_max_luns ||
2771 softc->ctl_luns[ooa_hdr->lun_num] == NULL)) {
2772 mtx_unlock(&softc->ctl_lock);
2773 free(entries, M_CTL);
2774 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2775 __func__, (uintmax_t)ooa_hdr->lun_num);
2782 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2783 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2784 ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2788 lun = softc->ctl_luns[ooa_hdr->lun_num];
2789 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr,
2792 mtx_unlock(&softc->ctl_lock);
2794 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2795 ooa_hdr->fill_len = ooa_hdr->fill_num *
2796 sizeof(struct ctl_ooa_entry);
2797 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2799 printf("%s: error copying out %d bytes for OOA dump\n",
2800 __func__, ooa_hdr->fill_len);
2803 getbinuptime(&ooa_hdr->cur_bt);
2805 if (cur_fill_num > ooa_hdr->alloc_num) {
2806 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2807 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2809 ooa_hdr->dropped_num = 0;
2810 ooa_hdr->status = CTL_OOA_OK;
2813 free(entries, M_CTL);
2816 case CTL_DELAY_IO: {
2817 struct ctl_io_delay_info *delay_info;
2819 delay_info = (struct ctl_io_delay_info *)addr;
2822 mtx_lock(&softc->ctl_lock);
2823 if (delay_info->lun_id >= ctl_max_luns ||
2824 (lun = softc->ctl_luns[delay_info->lun_id]) == NULL) {
2825 mtx_unlock(&softc->ctl_lock);
2826 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2829 mtx_lock(&lun->lun_lock);
2830 mtx_unlock(&softc->ctl_lock);
2831 delay_info->status = CTL_DELAY_STATUS_OK;
2832 switch (delay_info->delay_type) {
2833 case CTL_DELAY_TYPE_CONT:
2834 case CTL_DELAY_TYPE_ONESHOT:
2837 delay_info->status = CTL_DELAY_STATUS_INVALID_TYPE;
2840 switch (delay_info->delay_loc) {
2841 case CTL_DELAY_LOC_DATAMOVE:
2842 lun->delay_info.datamove_type = delay_info->delay_type;
2843 lun->delay_info.datamove_delay = delay_info->delay_secs;
2845 case CTL_DELAY_LOC_DONE:
2846 lun->delay_info.done_type = delay_info->delay_type;
2847 lun->delay_info.done_delay = delay_info->delay_secs;
2850 delay_info->status = CTL_DELAY_STATUS_INVALID_LOC;
2853 mtx_unlock(&lun->lun_lock);
2855 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2856 #endif /* CTL_IO_DELAY */
2859 #ifdef CTL_LEGACY_STATS
2860 case CTL_GETSTATS: {
2861 struct ctl_stats *stats = (struct ctl_stats *)addr;
2865 * XXX KDM no locking here. If the LUN list changes,
2866 * things can blow up.
2869 stats->status = CTL_SS_OK;
2870 stats->fill_len = 0;
2871 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2872 if (stats->fill_len + sizeof(lun->legacy_stats) >
2874 stats->status = CTL_SS_NEED_MORE_SPACE;
2877 retval = copyout(&lun->legacy_stats, &stats->lun_stats[i++],
2878 sizeof(lun->legacy_stats));
2881 stats->fill_len += sizeof(lun->legacy_stats);
2883 stats->num_luns = softc->num_luns;
2884 stats->flags = CTL_STATS_FLAG_NONE;
2886 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
2888 getnanouptime(&stats->timestamp);
2891 #endif /* CTL_LEGACY_STATS */
2892 case CTL_ERROR_INJECT: {
2893 struct ctl_error_desc *err_desc, *new_err_desc;
2895 err_desc = (struct ctl_error_desc *)addr;
2897 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2899 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2901 mtx_lock(&softc->ctl_lock);
2902 if (err_desc->lun_id >= ctl_max_luns ||
2903 (lun = softc->ctl_luns[err_desc->lun_id]) == NULL) {
2904 mtx_unlock(&softc->ctl_lock);
2905 free(new_err_desc, M_CTL);
2906 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2907 __func__, (uintmax_t)err_desc->lun_id);
2911 mtx_lock(&lun->lun_lock);
2912 mtx_unlock(&softc->ctl_lock);
2915 * We could do some checking here to verify the validity
2916 * of the request, but given the complexity of error
2917 * injection requests, the checking logic would be fairly
2920 * For now, if the request is invalid, it just won't get
2921 * executed and might get deleted.
2923 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2926 * XXX KDM check to make sure the serial number is unique,
2927 * in case we somehow manage to wrap. That shouldn't
2928 * happen for a very long time, but it's the right thing to
2931 new_err_desc->serial = lun->error_serial;
2932 err_desc->serial = lun->error_serial;
2933 lun->error_serial++;
2935 mtx_unlock(&lun->lun_lock);
2938 case CTL_ERROR_INJECT_DELETE: {
2939 struct ctl_error_desc *delete_desc, *desc, *desc2;
2942 delete_desc = (struct ctl_error_desc *)addr;
2945 mtx_lock(&softc->ctl_lock);
2946 if (delete_desc->lun_id >= ctl_max_luns ||
2947 (lun = softc->ctl_luns[delete_desc->lun_id]) == NULL) {
2948 mtx_unlock(&softc->ctl_lock);
2949 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2950 __func__, (uintmax_t)delete_desc->lun_id);
2954 mtx_lock(&lun->lun_lock);
2955 mtx_unlock(&softc->ctl_lock);
2956 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2957 if (desc->serial != delete_desc->serial)
2960 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2965 mtx_unlock(&lun->lun_lock);
2966 if (delete_done == 0) {
2967 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2968 "error serial %ju on LUN %u\n", __func__,
2969 delete_desc->serial, delete_desc->lun_id);
2975 case CTL_DUMP_STRUCTS: {
2977 struct ctl_port *port;
2978 struct ctl_frontend *fe;
2980 mtx_lock(&softc->ctl_lock);
2981 printf("CTL Persistent Reservation information start:\n");
2982 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2983 mtx_lock(&lun->lun_lock);
2984 if ((lun->flags & CTL_LUN_DISABLED) != 0) {
2985 mtx_unlock(&lun->lun_lock);
2989 for (j = 0; j < ctl_max_ports; j++) {
2990 if (lun->pr_keys[j] == NULL)
2992 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2993 if (lun->pr_keys[j][k] == 0)
2995 printf(" LUN %ju port %d iid %d key "
2996 "%#jx\n", lun->lun, j, k,
2997 (uintmax_t)lun->pr_keys[j][k]);
3000 mtx_unlock(&lun->lun_lock);
3002 printf("CTL Persistent Reservation information end\n");
3003 printf("CTL Ports:\n");
3004 STAILQ_FOREACH(port, &softc->port_list, links) {
3005 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
3006 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
3007 port->frontend->name, port->port_type,
3008 port->physical_port, port->virtual_port,
3009 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
3010 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3011 if (port->wwpn_iid[j].in_use == 0 &&
3012 port->wwpn_iid[j].wwpn == 0 &&
3013 port->wwpn_iid[j].name == NULL)
3016 printf(" iid %u use %d WWPN %#jx '%s'\n",
3017 j, port->wwpn_iid[j].in_use,
3018 (uintmax_t)port->wwpn_iid[j].wwpn,
3019 port->wwpn_iid[j].name);
3022 printf("CTL Port information end\n");
3023 mtx_unlock(&softc->ctl_lock);
3025 * XXX KDM calling this without a lock. We'd likely want
3026 * to drop the lock before calling the frontend's dump
3029 printf("CTL Frontends:\n");
3030 STAILQ_FOREACH(fe, &softc->fe_list, links) {
3031 printf(" Frontend '%s'\n", fe->name);
3032 if (fe->fe_dump != NULL)
3035 printf("CTL Frontend information end\n");
3039 struct ctl_lun_req *lun_req;
3040 struct ctl_backend_driver *backend;
3042 lun_req = (struct ctl_lun_req *)addr;
3044 backend = ctl_backend_find(lun_req->backend);
3045 if (backend == NULL) {
3046 lun_req->status = CTL_LUN_ERROR;
3047 snprintf(lun_req->error_str,
3048 sizeof(lun_req->error_str),
3049 "Backend \"%s\" not found.",
3053 if (lun_req->num_be_args > 0) {
3054 lun_req->kern_be_args = ctl_copyin_args(
3055 lun_req->num_be_args,
3058 sizeof(lun_req->error_str));
3059 if (lun_req->kern_be_args == NULL) {
3060 lun_req->status = CTL_LUN_ERROR;
3065 retval = backend->ioctl(dev, cmd, addr, flag, td);
3067 if (lun_req->num_be_args > 0) {
3068 ctl_copyout_args(lun_req->num_be_args,
3069 lun_req->kern_be_args);
3070 ctl_free_args(lun_req->num_be_args,
3071 lun_req->kern_be_args);
3075 case CTL_LUN_LIST: {
3077 struct ctl_lun_list *list;
3078 struct ctl_option *opt;
3080 list = (struct ctl_lun_list *)addr;
3083 * Allocate a fixed length sbuf here, based on the length
3084 * of the user's buffer. We could allocate an auto-extending
3085 * buffer, and then tell the user how much larger our
3086 * amount of data is than his buffer, but that presents
3089 * 1. The sbuf(9) routines use a blocking malloc, and so
3090 * we can't hold a lock while calling them with an
3091 * auto-extending buffer.
3093 * 2. There is not currently a LUN reference counting
3094 * mechanism, outside of outstanding transactions on
3095 * the LUN's OOA queue. So a LUN could go away on us
3096 * while we're getting the LUN number, backend-specific
3097 * information, etc. Thus, given the way things
3098 * currently work, we need to hold the CTL lock while
3099 * grabbing LUN information.
3101 * So, from the user's standpoint, the best thing to do is
3102 * allocate what he thinks is a reasonable buffer length,
3103 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3104 * double the buffer length and try again. (And repeat
3105 * that until he succeeds.)
3107 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3109 list->status = CTL_LUN_LIST_ERROR;
3110 snprintf(list->error_str, sizeof(list->error_str),
3111 "Unable to allocate %d bytes for LUN list",
3116 sbuf_printf(sb, "<ctllunlist>\n");
3118 mtx_lock(&softc->ctl_lock);
3119 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3120 mtx_lock(&lun->lun_lock);
3121 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3122 (uintmax_t)lun->lun);
3125 * Bail out as soon as we see that we've overfilled
3131 retval = sbuf_printf(sb, "\t<backend_type>%s"
3132 "</backend_type>\n",
3133 (lun->backend == NULL) ? "none" :
3134 lun->backend->name);
3139 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3140 lun->be_lun->lun_type);
3145 if (lun->backend == NULL) {
3146 retval = sbuf_printf(sb, "</lun>\n");
3152 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3153 (lun->be_lun->maxlba > 0) ?
3154 lun->be_lun->maxlba + 1 : 0);
3159 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3160 lun->be_lun->blocksize);
3165 retval = sbuf_printf(sb, "\t<serial_number>");
3170 retval = ctl_sbuf_printf_esc(sb,
3171 lun->be_lun->serial_num,
3172 sizeof(lun->be_lun->serial_num));
3177 retval = sbuf_printf(sb, "</serial_number>\n");
3182 retval = sbuf_printf(sb, "\t<device_id>");
3187 retval = ctl_sbuf_printf_esc(sb,
3188 lun->be_lun->device_id,
3189 sizeof(lun->be_lun->device_id));
3194 retval = sbuf_printf(sb, "</device_id>\n");
3199 if (lun->backend->lun_info != NULL) {
3200 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3204 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3205 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3206 opt->name, opt->value, opt->name);
3211 retval = sbuf_printf(sb, "</lun>\n");
3215 mtx_unlock(&lun->lun_lock);
3218 mtx_unlock(&lun->lun_lock);
3219 mtx_unlock(&softc->ctl_lock);
3222 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3225 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3226 snprintf(list->error_str, sizeof(list->error_str),
3227 "Out of space, %d bytes is too small",
3234 retval = copyout(sbuf_data(sb), list->lun_xml,
3237 list->fill_len = sbuf_len(sb) + 1;
3238 list->status = CTL_LUN_LIST_OK;
3243 struct ctl_iscsi *ci;
3244 struct ctl_frontend *fe;
3246 ci = (struct ctl_iscsi *)addr;
3248 fe = ctl_frontend_find("iscsi");
3250 ci->status = CTL_ISCSI_ERROR;
3251 snprintf(ci->error_str, sizeof(ci->error_str),
3252 "Frontend \"iscsi\" not found.");
3256 retval = fe->ioctl(dev, cmd, addr, flag, td);
3259 case CTL_PORT_REQ: {
3260 struct ctl_req *req;
3261 struct ctl_frontend *fe;
3263 req = (struct ctl_req *)addr;
3265 fe = ctl_frontend_find(req->driver);
3267 req->status = CTL_LUN_ERROR;
3268 snprintf(req->error_str, sizeof(req->error_str),
3269 "Frontend \"%s\" not found.", req->driver);
3272 if (req->num_args > 0) {
3273 req->kern_args = ctl_copyin_args(req->num_args,
3274 req->args, req->error_str, sizeof(req->error_str));
3275 if (req->kern_args == NULL) {
3276 req->status = CTL_LUN_ERROR;
3282 retval = fe->ioctl(dev, cmd, addr, flag, td);
3286 if (req->num_args > 0) {
3287 ctl_copyout_args(req->num_args, req->kern_args);
3288 ctl_free_args(req->num_args, req->kern_args);
3292 case CTL_PORT_LIST: {
3294 struct ctl_port *port;
3295 struct ctl_lun_list *list;
3296 struct ctl_option *opt;
3300 list = (struct ctl_lun_list *)addr;
3302 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3304 list->status = CTL_LUN_LIST_ERROR;
3305 snprintf(list->error_str, sizeof(list->error_str),
3306 "Unable to allocate %d bytes for LUN list",
3311 sbuf_printf(sb, "<ctlportlist>\n");
3313 mtx_lock(&softc->ctl_lock);
3314 STAILQ_FOREACH(port, &softc->port_list, links) {
3315 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3316 (uintmax_t)port->targ_port);
3319 * Bail out as soon as we see that we've overfilled
3325 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3326 "</frontend_type>\n", port->frontend->name);
3330 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3335 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3336 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3340 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3345 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3346 port->physical_port);
3350 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3351 port->virtual_port);
3355 if (port->target_devid != NULL) {
3356 sbuf_printf(sb, "\t<target>");
3357 ctl_id_sbuf(port->target_devid, sb);
3358 sbuf_printf(sb, "</target>\n");
3361 if (port->port_devid != NULL) {
3362 sbuf_printf(sb, "\t<port>");
3363 ctl_id_sbuf(port->port_devid, sb);
3364 sbuf_printf(sb, "</port>\n");
3367 if (port->port_info != NULL) {
3368 retval = port->port_info(port->onoff_arg, sb);
3372 STAILQ_FOREACH(opt, &port->options, links) {
3373 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3374 opt->name, opt->value, opt->name);
3379 if (port->lun_map != NULL) {
3380 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n");
3381 for (j = 0; j < port->lun_map_size; j++) {
3382 plun = ctl_lun_map_from_port(port, j);
3383 if (plun == UINT32_MAX)
3386 "\t<lun id=\"%u\">%u</lun>\n",
3391 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3392 if (port->wwpn_iid[j].in_use == 0 ||
3393 (port->wwpn_iid[j].wwpn == 0 &&
3394 port->wwpn_iid[j].name == NULL))
3397 if (port->wwpn_iid[j].name != NULL)
3398 retval = sbuf_printf(sb,
3399 "\t<initiator id=\"%u\">%s</initiator>\n",
3400 j, port->wwpn_iid[j].name);
3402 retval = sbuf_printf(sb,
3403 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3404 j, port->wwpn_iid[j].wwpn);
3411 retval = sbuf_printf(sb, "</targ_port>\n");
3415 mtx_unlock(&softc->ctl_lock);
3418 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3421 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3422 snprintf(list->error_str, sizeof(list->error_str),
3423 "Out of space, %d bytes is too small",
3430 retval = copyout(sbuf_data(sb), list->lun_xml,
3433 list->fill_len = sbuf_len(sb) + 1;
3434 list->status = CTL_LUN_LIST_OK;
3439 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr;
3440 struct ctl_port *port;
3442 mtx_lock(&softc->ctl_lock);
3443 if (lm->port < softc->port_min ||
3444 lm->port >= softc->port_max ||
3445 (port = softc->ctl_ports[lm->port]) == NULL) {
3446 mtx_unlock(&softc->ctl_lock);
3449 if (port->status & CTL_PORT_STATUS_ONLINE) {
3450 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3451 if (ctl_lun_map_to_port(port, lun->lun) ==
3454 mtx_lock(&lun->lun_lock);
3455 ctl_est_ua_port(lun, lm->port, -1,
3457 mtx_unlock(&lun->lun_lock);
3460 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps
3461 if (lm->plun != UINT32_MAX) {
3462 if (lm->lun == UINT32_MAX)
3463 retval = ctl_lun_map_unset(port, lm->plun);
3464 else if (lm->lun < ctl_max_luns &&
3465 softc->ctl_luns[lm->lun] != NULL)
3466 retval = ctl_lun_map_set(port, lm->plun, lm->lun);
3470 if (lm->lun == UINT32_MAX)
3471 retval = ctl_lun_map_deinit(port);
3473 retval = ctl_lun_map_init(port);
3475 if (port->status & CTL_PORT_STATUS_ONLINE)
3476 ctl_isc_announce_port(port);
3479 case CTL_GET_LUN_STATS: {
3480 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr;
3484 * XXX KDM no locking here. If the LUN list changes,
3485 * things can blow up.
3488 stats->status = CTL_SS_OK;
3489 stats->fill_len = 0;
3490 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3491 if (lun->lun < stats->first_item)
3493 if (stats->fill_len + sizeof(lun->stats) >
3495 stats->status = CTL_SS_NEED_MORE_SPACE;
3498 retval = copyout(&lun->stats, &stats->stats[i++],
3499 sizeof(lun->stats));
3502 stats->fill_len += sizeof(lun->stats);
3504 stats->num_items = softc->num_luns;
3505 stats->flags = CTL_STATS_FLAG_NONE;
3507 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
3509 getnanouptime(&stats->timestamp);
3512 case CTL_GET_PORT_STATS: {
3513 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr;
3517 * XXX KDM no locking here. If the LUN list changes,
3518 * things can blow up.
3521 stats->status = CTL_SS_OK;
3522 stats->fill_len = 0;
3523 STAILQ_FOREACH(port, &softc->port_list, links) {
3524 if (port->targ_port < stats->first_item)
3526 if (stats->fill_len + sizeof(port->stats) >
3528 stats->status = CTL_SS_NEED_MORE_SPACE;
3531 retval = copyout(&port->stats, &stats->stats[i++],
3532 sizeof(port->stats));
3535 stats->fill_len += sizeof(port->stats);
3537 stats->num_items = softc->num_ports;
3538 stats->flags = CTL_STATS_FLAG_NONE;
3540 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
3542 getnanouptime(&stats->timestamp);
3546 /* XXX KDM should we fix this? */
3548 struct ctl_backend_driver *backend;
3555 * We encode the backend type as the ioctl type for backend
3556 * ioctls. So parse it out here, and then search for a
3557 * backend of this type.
3559 type = _IOC_TYPE(cmd);
3561 STAILQ_FOREACH(backend, &softc->be_list, links) {
3562 if (backend->type == type) {
3568 printf("ctl: unknown ioctl command %#lx or backend "
3573 retval = backend->ioctl(dev, cmd, addr, flag, td);
3583 ctl_get_initindex(struct ctl_nexus *nexus)
3585 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3589 ctl_lun_map_init(struct ctl_port *port)
3591 struct ctl_softc *softc = port->ctl_softc;
3592 struct ctl_lun *lun;
3593 int size = ctl_lun_map_size;
3596 if (port->lun_map == NULL || port->lun_map_size < size) {
3597 port->lun_map_size = 0;
3598 free(port->lun_map, M_CTL);
3599 port->lun_map = malloc(size * sizeof(uint32_t),
3602 if (port->lun_map == NULL)
3604 for (i = 0; i < size; i++)
3605 port->lun_map[i] = UINT32_MAX;
3606 port->lun_map_size = size;
3607 if (port->status & CTL_PORT_STATUS_ONLINE) {
3608 if (port->lun_disable != NULL) {
3609 STAILQ_FOREACH(lun, &softc->lun_list, links)
3610 port->lun_disable(port->targ_lun_arg, lun->lun);
3612 ctl_isc_announce_port(port);
3618 ctl_lun_map_deinit(struct ctl_port *port)
3620 struct ctl_softc *softc = port->ctl_softc;
3621 struct ctl_lun *lun;
3623 if (port->lun_map == NULL)
3625 port->lun_map_size = 0;
3626 free(port->lun_map, M_CTL);
3627 port->lun_map = NULL;
3628 if (port->status & CTL_PORT_STATUS_ONLINE) {
3629 if (port->lun_enable != NULL) {
3630 STAILQ_FOREACH(lun, &softc->lun_list, links)
3631 port->lun_enable(port->targ_lun_arg, lun->lun);
3633 ctl_isc_announce_port(port);
3639 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun)
3644 if (port->lun_map == NULL) {
3645 status = ctl_lun_map_init(port);
3649 if (plun >= port->lun_map_size)
3651 old = port->lun_map[plun];
3652 port->lun_map[plun] = glun;
3653 if ((port->status & CTL_PORT_STATUS_ONLINE) && old == UINT32_MAX) {
3654 if (port->lun_enable != NULL)
3655 port->lun_enable(port->targ_lun_arg, plun);
3656 ctl_isc_announce_port(port);
3662 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun)
3666 if (port->lun_map == NULL || plun >= port->lun_map_size)
3668 old = port->lun_map[plun];
3669 port->lun_map[plun] = UINT32_MAX;
3670 if ((port->status & CTL_PORT_STATUS_ONLINE) && old != UINT32_MAX) {
3671 if (port->lun_disable != NULL)
3672 port->lun_disable(port->targ_lun_arg, plun);
3673 ctl_isc_announce_port(port);
3679 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
3683 return (UINT32_MAX);
3684 if (port->lun_map == NULL)
3686 if (lun_id > port->lun_map_size)
3687 return (UINT32_MAX);
3688 return (port->lun_map[lun_id]);
3692 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
3697 return (UINT32_MAX);
3698 if (port->lun_map == NULL)
3700 for (i = 0; i < port->lun_map_size; i++) {
3701 if (port->lun_map[i] == lun_id)
3704 return (UINT32_MAX);
3708 ctl_decode_lun(uint64_t encoded)
3711 uint32_t result = 0xffffffff;
3713 be64enc(lun, encoded);
3714 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) {
3715 case RPL_LUNDATA_ATYP_PERIPH:
3716 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 &&
3717 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0)
3720 case RPL_LUNDATA_ATYP_FLAT:
3721 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 &&
3722 lun[6] == 0 && lun[7] == 0)
3723 result = ((lun[0] & 0x3f) << 8) + lun[1];
3725 case RPL_LUNDATA_ATYP_EXTLUN:
3726 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) {
3728 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) {
3733 result = (lun[1] << 16) + (lun[2] << 8) +
3737 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0)
3738 result = (lun[2] << 24) +
3739 (lun[3] << 16) + (lun[4] << 8) +
3744 case RPL_LUNDATA_EXT_EAM_NOT_SPEC:
3745 result = 0xffffffff;
3754 ctl_encode_lun(uint32_t decoded)
3756 uint64_t l = decoded;
3759 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48));
3761 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48));
3763 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) |
3765 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16));
3769 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last)
3773 for (i = first; i < last; i++) {
3774 if ((mask[i / 32] & (1 << (i % 32))) == 0)
3781 ctl_set_mask(uint32_t *mask, uint32_t bit)
3783 uint32_t chunk, piece;
3786 piece = bit % (sizeof(uint32_t) * 8);
3788 if ((mask[chunk] & (1 << piece)) != 0)
3791 mask[chunk] |= (1 << piece);
3797 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3799 uint32_t chunk, piece;
3802 piece = bit % (sizeof(uint32_t) * 8);
3804 if ((mask[chunk] & (1 << piece)) == 0)
3807 mask[chunk] &= ~(1 << piece);
3813 ctl_is_set(uint32_t *mask, uint32_t bit)
3815 uint32_t chunk, piece;
3818 piece = bit % (sizeof(uint32_t) * 8);
3820 if ((mask[chunk] & (1 << piece)) == 0)
3827 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3831 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3834 return (t[residx % CTL_MAX_INIT_PER_PORT]);
3838 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3842 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3845 t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3849 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3854 i = residx/CTL_MAX_INIT_PER_PORT;
3855 if (lun->pr_keys[i] != NULL)
3857 mtx_unlock(&lun->lun_lock);
3858 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3860 mtx_lock(&lun->lun_lock);
3861 if (lun->pr_keys[i] == NULL)
3862 lun->pr_keys[i] = p;
3868 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3872 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3873 KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3874 t[residx % CTL_MAX_INIT_PER_PORT] = key;
3878 * ctl_softc, pool_name, total_ctl_io are passed in.
3879 * npool is passed out.
3882 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3883 uint32_t total_ctl_io, void **npool)
3885 struct ctl_io_pool *pool;
3887 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3892 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3893 pool->ctl_softc = ctl_softc;
3895 pool->zone = uma_zsecond_create(pool->name, NULL,
3896 NULL, NULL, NULL, ctl_softc->io_zone);
3897 /* uma_prealloc(pool->zone, total_ctl_io); */
3899 pool->zone = ctl_softc->io_zone;
3907 ctl_pool_free(struct ctl_io_pool *pool)
3914 uma_zdestroy(pool->zone);
3920 ctl_alloc_io(void *pool_ref)
3922 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3925 io = uma_zalloc(pool->zone, M_WAITOK);
3927 io->io_hdr.pool = pool_ref;
3928 CTL_SOFTC(io) = pool->ctl_softc;
3934 ctl_alloc_io_nowait(void *pool_ref)
3936 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3939 io = uma_zalloc(pool->zone, M_NOWAIT);
3941 io->io_hdr.pool = pool_ref;
3942 CTL_SOFTC(io) = pool->ctl_softc;
3948 ctl_free_io(union ctl_io *io)
3950 struct ctl_io_pool *pool;
3955 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3956 uma_zfree(pool->zone, io);
3960 ctl_zero_io(union ctl_io *io)
3962 struct ctl_io_pool *pool;
3968 * May need to preserve linked list pointers at some point too.
3970 pool = io->io_hdr.pool;
3971 memset(io, 0, sizeof(*io));
3972 io->io_hdr.pool = pool;
3973 CTL_SOFTC(io) = pool->ctl_softc;
3977 ctl_expand_number(const char *buf, uint64_t *num)
3983 number = strtoq(buf, &endptr, 0);
3985 switch (tolower((unsigned char)*endptr)) {
4005 case '\0': /* No unit. */
4009 /* Unrecognized unit. */
4013 if ((number << shift) >> shift != number) {
4017 *num = number << shift;
4023 * This routine could be used in the future to load default and/or saved
4024 * mode page parameters for a particuar lun.
4027 ctl_init_page_index(struct ctl_lun *lun)
4030 struct ctl_page_index *page_index;
4034 memcpy(&lun->mode_pages.index, page_index_template,
4035 sizeof(page_index_template));
4037 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4039 page_index = &lun->mode_pages.index[i];
4040 if (lun->be_lun->lun_type == T_DIRECT &&
4041 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4043 if (lun->be_lun->lun_type == T_PROCESSOR &&
4044 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4046 if (lun->be_lun->lun_type == T_CDROM &&
4047 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4050 page_code = page_index->page_code & SMPH_PC_MASK;
4051 switch (page_code) {
4052 case SMS_RW_ERROR_RECOVERY_PAGE: {
4053 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4054 ("subpage %#x for page %#x is incorrect!",
4055 page_index->subpage, page_code));
4056 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
4057 &rw_er_page_default,
4058 sizeof(rw_er_page_default));
4059 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
4060 &rw_er_page_changeable,
4061 sizeof(rw_er_page_changeable));
4062 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
4063 &rw_er_page_default,
4064 sizeof(rw_er_page_default));
4065 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
4066 &rw_er_page_default,
4067 sizeof(rw_er_page_default));
4068 page_index->page_data =
4069 (uint8_t *)lun->mode_pages.rw_er_page;
4072 case SMS_FORMAT_DEVICE_PAGE: {
4073 struct scsi_format_page *format_page;
4075 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4076 ("subpage %#x for page %#x is incorrect!",
4077 page_index->subpage, page_code));
4080 * Sectors per track are set above. Bytes per
4081 * sector need to be set here on a per-LUN basis.
4083 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
4084 &format_page_default,
4085 sizeof(format_page_default));
4086 memcpy(&lun->mode_pages.format_page[
4087 CTL_PAGE_CHANGEABLE], &format_page_changeable,
4088 sizeof(format_page_changeable));
4089 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
4090 &format_page_default,
4091 sizeof(format_page_default));
4092 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
4093 &format_page_default,
4094 sizeof(format_page_default));
4096 format_page = &lun->mode_pages.format_page[
4098 scsi_ulto2b(lun->be_lun->blocksize,
4099 format_page->bytes_per_sector);
4101 format_page = &lun->mode_pages.format_page[
4103 scsi_ulto2b(lun->be_lun->blocksize,
4104 format_page->bytes_per_sector);
4106 format_page = &lun->mode_pages.format_page[
4108 scsi_ulto2b(lun->be_lun->blocksize,
4109 format_page->bytes_per_sector);
4111 page_index->page_data =
4112 (uint8_t *)lun->mode_pages.format_page;
4115 case SMS_RIGID_DISK_PAGE: {
4116 struct scsi_rigid_disk_page *rigid_disk_page;
4117 uint32_t sectors_per_cylinder;
4121 #endif /* !__XSCALE__ */
4123 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4124 ("subpage %#x for page %#x is incorrect!",
4125 page_index->subpage, page_code));
4128 * Rotation rate and sectors per track are set
4129 * above. We calculate the cylinders here based on
4130 * capacity. Due to the number of heads and
4131 * sectors per track we're using, smaller arrays
4132 * may turn out to have 0 cylinders. Linux and
4133 * FreeBSD don't pay attention to these mode pages
4134 * to figure out capacity, but Solaris does. It
4135 * seems to deal with 0 cylinders just fine, and
4136 * works out a fake geometry based on the capacity.
4138 memcpy(&lun->mode_pages.rigid_disk_page[
4139 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4140 sizeof(rigid_disk_page_default));
4141 memcpy(&lun->mode_pages.rigid_disk_page[
4142 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4143 sizeof(rigid_disk_page_changeable));
4145 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4149 * The divide method here will be more accurate,
4150 * probably, but results in floating point being
4151 * used in the kernel on i386 (__udivdi3()). On the
4152 * XScale, though, __udivdi3() is implemented in
4155 * The shift method for cylinder calculation is
4156 * accurate if sectors_per_cylinder is a power of
4157 * 2. Otherwise it might be slightly off -- you
4158 * might have a bit of a truncation problem.
4161 cylinders = (lun->be_lun->maxlba + 1) /
4162 sectors_per_cylinder;
4164 for (shift = 31; shift > 0; shift--) {
4165 if (sectors_per_cylinder & (1 << shift))
4168 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4172 * We've basically got 3 bytes, or 24 bits for the
4173 * cylinder size in the mode page. If we're over,
4174 * just round down to 2^24.
4176 if (cylinders > 0xffffff)
4177 cylinders = 0xffffff;
4179 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4181 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4183 if ((value = ctl_get_opt(&lun->be_lun->options,
4185 scsi_ulto2b(strtol(value, NULL, 0),
4186 rigid_disk_page->rotation_rate);
4189 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
4190 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4191 sizeof(rigid_disk_page_default));
4192 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
4193 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4194 sizeof(rigid_disk_page_default));
4196 page_index->page_data =
4197 (uint8_t *)lun->mode_pages.rigid_disk_page;
4200 case SMS_VERIFY_ERROR_RECOVERY_PAGE: {
4201 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4202 ("subpage %#x for page %#x is incorrect!",
4203 page_index->subpage, page_code));
4204 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CURRENT],
4205 &verify_er_page_default,
4206 sizeof(verify_er_page_default));
4207 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CHANGEABLE],
4208 &verify_er_page_changeable,
4209 sizeof(verify_er_page_changeable));
4210 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_DEFAULT],
4211 &verify_er_page_default,
4212 sizeof(verify_er_page_default));
4213 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_SAVED],
4214 &verify_er_page_default,
4215 sizeof(verify_er_page_default));
4216 page_index->page_data =
4217 (uint8_t *)lun->mode_pages.verify_er_page;
4220 case SMS_CACHING_PAGE: {
4221 struct scsi_caching_page *caching_page;
4223 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4224 ("subpage %#x for page %#x is incorrect!",
4225 page_index->subpage, page_code));
4226 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4227 &caching_page_default,
4228 sizeof(caching_page_default));
4229 memcpy(&lun->mode_pages.caching_page[
4230 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4231 sizeof(caching_page_changeable));
4232 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4233 &caching_page_default,
4234 sizeof(caching_page_default));
4235 caching_page = &lun->mode_pages.caching_page[
4237 value = ctl_get_opt(&lun->be_lun->options, "writecache");
4238 if (value != NULL && strcmp(value, "off") == 0)
4239 caching_page->flags1 &= ~SCP_WCE;
4240 value = ctl_get_opt(&lun->be_lun->options, "readcache");
4241 if (value != NULL && strcmp(value, "off") == 0)
4242 caching_page->flags1 |= SCP_RCD;
4243 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4244 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4245 sizeof(caching_page_default));
4246 page_index->page_data =
4247 (uint8_t *)lun->mode_pages.caching_page;
4250 case SMS_CONTROL_MODE_PAGE: {
4251 switch (page_index->subpage) {
4252 case SMS_SUBPAGE_PAGE_0: {
4253 struct scsi_control_page *control_page;
4255 memcpy(&lun->mode_pages.control_page[
4257 &control_page_default,
4258 sizeof(control_page_default));
4259 memcpy(&lun->mode_pages.control_page[
4260 CTL_PAGE_CHANGEABLE],
4261 &control_page_changeable,
4262 sizeof(control_page_changeable));
4263 memcpy(&lun->mode_pages.control_page[
4265 &control_page_default,
4266 sizeof(control_page_default));
4267 control_page = &lun->mode_pages.control_page[
4269 value = ctl_get_opt(&lun->be_lun->options,
4271 if (value != NULL &&
4272 strcmp(value, "unrestricted") == 0) {
4273 control_page->queue_flags &=
4274 ~SCP_QUEUE_ALG_MASK;
4275 control_page->queue_flags |=
4276 SCP_QUEUE_ALG_UNRESTRICTED;
4278 memcpy(&lun->mode_pages.control_page[
4280 &lun->mode_pages.control_page[
4282 sizeof(control_page_default));
4283 page_index->page_data =
4284 (uint8_t *)lun->mode_pages.control_page;
4288 memcpy(&lun->mode_pages.control_ext_page[
4290 &control_ext_page_default,
4291 sizeof(control_ext_page_default));
4292 memcpy(&lun->mode_pages.control_ext_page[
4293 CTL_PAGE_CHANGEABLE],
4294 &control_ext_page_changeable,
4295 sizeof(control_ext_page_changeable));
4296 memcpy(&lun->mode_pages.control_ext_page[
4298 &control_ext_page_default,
4299 sizeof(control_ext_page_default));
4300 memcpy(&lun->mode_pages.control_ext_page[
4302 &lun->mode_pages.control_ext_page[
4304 sizeof(control_ext_page_default));
4305 page_index->page_data =
4306 (uint8_t *)lun->mode_pages.control_ext_page;
4309 panic("subpage %#x for page %#x is incorrect!",
4310 page_index->subpage, page_code);
4314 case SMS_INFO_EXCEPTIONS_PAGE: {
4315 switch (page_index->subpage) {
4316 case SMS_SUBPAGE_PAGE_0:
4317 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4319 sizeof(ie_page_default));
4320 memcpy(&lun->mode_pages.ie_page[
4321 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4322 sizeof(ie_page_changeable));
4323 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4325 sizeof(ie_page_default));
4326 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4328 sizeof(ie_page_default));
4329 page_index->page_data =
4330 (uint8_t *)lun->mode_pages.ie_page;
4333 struct ctl_logical_block_provisioning_page *page;
4335 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4337 sizeof(lbp_page_default));
4338 memcpy(&lun->mode_pages.lbp_page[
4339 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4340 sizeof(lbp_page_changeable));
4341 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4343 sizeof(lbp_page_default));
4344 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4345 value = ctl_get_opt(&lun->be_lun->options,
4347 if (value != NULL &&
4348 ctl_expand_number(value, &ival) == 0) {
4349 page->descr[0].flags |= SLBPPD_ENABLED |
4351 if (lun->be_lun->blocksize)
4352 ival /= lun->be_lun->blocksize;
4355 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4356 page->descr[0].count);
4358 value = ctl_get_opt(&lun->be_lun->options,
4360 if (value != NULL &&
4361 ctl_expand_number(value, &ival) == 0) {
4362 page->descr[1].flags |= SLBPPD_ENABLED |
4364 if (lun->be_lun->blocksize)
4365 ival /= lun->be_lun->blocksize;
4368 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4369 page->descr[1].count);
4371 value = ctl_get_opt(&lun->be_lun->options,
4372 "pool-avail-threshold");
4373 if (value != NULL &&
4374 ctl_expand_number(value, &ival) == 0) {
4375 page->descr[2].flags |= SLBPPD_ENABLED |
4377 if (lun->be_lun->blocksize)
4378 ival /= lun->be_lun->blocksize;
4381 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4382 page->descr[2].count);
4384 value = ctl_get_opt(&lun->be_lun->options,
4385 "pool-used-threshold");
4386 if (value != NULL &&
4387 ctl_expand_number(value, &ival) == 0) {
4388 page->descr[3].flags |= SLBPPD_ENABLED |
4390 if (lun->be_lun->blocksize)
4391 ival /= lun->be_lun->blocksize;
4394 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4395 page->descr[3].count);
4397 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4398 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4399 sizeof(lbp_page_default));
4400 page_index->page_data =
4401 (uint8_t *)lun->mode_pages.lbp_page;
4405 panic("subpage %#x for page %#x is incorrect!",
4406 page_index->subpage, page_code);
4410 case SMS_CDDVD_CAPS_PAGE:{
4411 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4412 ("subpage %#x for page %#x is incorrect!",
4413 page_index->subpage, page_code));
4414 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT],
4415 &cddvd_page_default,
4416 sizeof(cddvd_page_default));
4417 memcpy(&lun->mode_pages.cddvd_page[
4418 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable,
4419 sizeof(cddvd_page_changeable));
4420 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4421 &cddvd_page_default,
4422 sizeof(cddvd_page_default));
4423 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT],
4424 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4425 sizeof(cddvd_page_default));
4426 page_index->page_data =
4427 (uint8_t *)lun->mode_pages.cddvd_page;
4431 panic("invalid page code value %#x", page_code);
4435 return (CTL_RETVAL_COMPLETE);
4439 ctl_init_log_page_index(struct ctl_lun *lun)
4441 struct ctl_page_index *page_index;
4444 memcpy(&lun->log_pages.index, log_page_index_template,
4445 sizeof(log_page_index_template));
4448 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4450 page_index = &lun->log_pages.index[i];
4451 if (lun->be_lun->lun_type == T_DIRECT &&
4452 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4454 if (lun->be_lun->lun_type == T_PROCESSOR &&
4455 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4457 if (lun->be_lun->lun_type == T_CDROM &&
4458 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4461 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4462 lun->backend->lun_attr == NULL)
4465 if (page_index->page_code != prev) {
4466 lun->log_pages.pages_page[j] = page_index->page_code;
4467 prev = page_index->page_code;
4470 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4471 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4474 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4475 lun->log_pages.index[0].page_len = j;
4476 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4477 lun->log_pages.index[1].page_len = k * 2;
4478 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4479 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4480 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page;
4481 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page);
4482 lun->log_pages.index[4].page_data = (uint8_t *)&lun->log_pages.ie_page;
4483 lun->log_pages.index[4].page_len = sizeof(lun->log_pages.ie_page);
4485 return (CTL_RETVAL_COMPLETE);
4489 hex2bin(const char *str, uint8_t *buf, int buf_size)
4494 memset(buf, 0, buf_size);
4495 while (isspace(str[0]))
4497 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4500 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4501 while (str[i] == '-') /* Skip dashes in UUIDs. */
4506 else if (isalpha(c))
4507 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4513 buf[i / 2] |= (c << 4);
4517 return ((i + 1) / 2);
4524 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4525 * wants us to allocate the LUN and he can block.
4526 * - ctl_softc is always set
4527 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4529 * Returns 0 for success, non-zero (errno) for failure.
4532 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4533 struct ctl_be_lun *const be_lun)
4535 struct ctl_lun *nlun, *lun;
4536 struct scsi_vpd_id_descriptor *desc;
4537 struct scsi_vpd_id_t10 *t10id;
4538 const char *eui, *naa, *scsiname, *uuid, *vendor, *value;
4539 int lun_number, lun_malloced;
4540 int devidlen, idlen1, idlen2 = 0, len;
4546 * We currently only support Direct Access or Processor LUN types.
4548 switch (be_lun->lun_type) {
4556 be_lun->lun_config_status(be_lun->be_lun,
4557 CTL_LUN_CONFIG_FAILURE);
4560 if (ctl_lun == NULL) {
4561 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4568 memset(lun, 0, sizeof(*lun));
4570 lun->flags = CTL_LUN_MALLOCED;
4572 lun->pending_sense = malloc(sizeof(struct scsi_sense_data *) *
4573 ctl_max_ports, M_DEVBUF, M_WAITOK | M_ZERO);
4574 lun->pending_ua = malloc(sizeof(ctl_ua_type *) * ctl_max_ports,
4575 M_DEVBUF, M_WAITOK | M_ZERO);
4576 lun->pr_keys = malloc(sizeof(uint64_t *) * ctl_max_ports,
4577 M_DEVBUF, M_WAITOK | M_ZERO);
4579 /* Generate LUN ID. */
4580 devidlen = max(CTL_DEVID_MIN_LEN,
4581 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4582 idlen1 = sizeof(*t10id) + devidlen;
4583 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4584 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4585 if (scsiname != NULL) {
4586 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4587 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4589 eui = ctl_get_opt(&be_lun->options, "eui");
4591 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4593 naa = ctl_get_opt(&be_lun->options, "naa");
4595 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4597 uuid = ctl_get_opt(&be_lun->options, "uuid");
4599 len += sizeof(struct scsi_vpd_id_descriptor) + 18;
4601 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4602 M_CTL, M_WAITOK | M_ZERO);
4603 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4604 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4605 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4606 desc->length = idlen1;
4607 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4608 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4609 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4610 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4612 strncpy(t10id->vendor, vendor,
4613 min(sizeof(t10id->vendor), strlen(vendor)));
4615 strncpy((char *)t10id->vendor_spec_id,
4616 (char *)be_lun->device_id, devidlen);
4617 if (scsiname != NULL) {
4618 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4620 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4621 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4622 SVPD_ID_TYPE_SCSI_NAME;
4623 desc->length = idlen2;
4624 strlcpy(desc->identifier, scsiname, idlen2);
4627 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4629 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4630 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4632 desc->length = hex2bin(eui, desc->identifier, 16);
4633 desc->length = desc->length > 12 ? 16 :
4634 (desc->length > 8 ? 12 : 8);
4635 len -= 16 - desc->length;
4638 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4640 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4641 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4643 desc->length = hex2bin(naa, desc->identifier, 16);
4644 desc->length = desc->length > 8 ? 16 : 8;
4645 len -= 16 - desc->length;
4648 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4650 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4651 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4653 desc->identifier[0] = 0x10;
4654 hex2bin(uuid, &desc->identifier[2], 16);
4657 lun->lun_devid->len = len;
4659 mtx_lock(&ctl_softc->ctl_lock);
4661 * See if the caller requested a particular LUN number. If so, see
4662 * if it is available. Otherwise, allocate the first available LUN.
4664 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4665 if ((be_lun->req_lun_id > (ctl_max_luns - 1))
4666 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4667 mtx_unlock(&ctl_softc->ctl_lock);
4668 if (be_lun->req_lun_id > (ctl_max_luns - 1)) {
4669 printf("ctl: requested LUN ID %d is higher "
4670 "than ctl_max_luns - 1 (%d)\n",
4671 be_lun->req_lun_id, ctl_max_luns - 1);
4674 * XXX KDM return an error, or just assign
4675 * another LUN ID in this case??
4677 printf("ctl: requested LUN ID %d is already "
4678 "in use\n", be_lun->req_lun_id);
4681 free(lun->lun_devid, M_CTL);
4682 if (lun->flags & CTL_LUN_MALLOCED)
4684 be_lun->lun_config_status(be_lun->be_lun,
4685 CTL_LUN_CONFIG_FAILURE);
4688 lun_number = be_lun->req_lun_id;
4690 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, ctl_max_luns);
4691 if (lun_number == -1) {
4692 mtx_unlock(&ctl_softc->ctl_lock);
4693 printf("ctl: can't allocate LUN, out of LUNs\n");
4697 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4698 mtx_unlock(&ctl_softc->ctl_lock);
4700 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4701 lun->lun = lun_number;
4702 lun->be_lun = be_lun;
4704 * The processor LUN is always enabled. Disk LUNs come on line
4705 * disabled, and must be enabled by the backend.
4707 lun->flags |= CTL_LUN_DISABLED;
4708 lun->backend = be_lun->be;
4709 be_lun->ctl_lun = lun;
4710 be_lun->lun_id = lun_number;
4711 atomic_add_int(&be_lun->be->num_luns, 1);
4712 if (be_lun->flags & CTL_LUN_FLAG_EJECTED)
4713 lun->flags |= CTL_LUN_EJECTED;
4714 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA)
4715 lun->flags |= CTL_LUN_NO_MEDIA;
4716 if (be_lun->flags & CTL_LUN_FLAG_STOPPED)
4717 lun->flags |= CTL_LUN_STOPPED;
4719 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4720 lun->flags |= CTL_LUN_PRIMARY_SC;
4722 value = ctl_get_opt(&be_lun->options, "removable");
4723 if (value != NULL) {
4724 if (strcmp(value, "on") == 0)
4725 lun->flags |= CTL_LUN_REMOVABLE;
4726 } else if (be_lun->lun_type == T_CDROM)
4727 lun->flags |= CTL_LUN_REMOVABLE;
4729 lun->ctl_softc = ctl_softc;
4731 lun->last_busy = getsbinuptime();
4733 TAILQ_INIT(&lun->ooa_queue);
4734 TAILQ_INIT(&lun->blocked_queue);
4735 STAILQ_INIT(&lun->error_list);
4736 lun->ie_reported = 1;
4737 callout_init_mtx(&lun->ie_callout, &lun->lun_lock, 0);
4738 ctl_tpc_lun_init(lun);
4739 if (lun->flags & CTL_LUN_REMOVABLE) {
4740 lun->prevent = malloc((CTL_MAX_INITIATORS + 31) / 32 * 4,
4745 * Initialize the mode and log page index.
4747 ctl_init_page_index(lun);
4748 ctl_init_log_page_index(lun);
4750 /* Setup statistics gathering */
4751 #ifdef CTL_LEGACY_STATS
4752 lun->legacy_stats.device_type = be_lun->lun_type;
4753 lun->legacy_stats.lun_number = lun_number;
4754 lun->legacy_stats.blocksize = be_lun->blocksize;
4755 if (be_lun->blocksize == 0)
4756 lun->legacy_stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4757 lun->legacy_stats.ports = malloc(sizeof(struct ctl_lun_io_port_stats) *
4758 ctl_max_ports, M_DEVBUF, M_WAITOK | M_ZERO);
4759 for (len = 0; len < ctl_max_ports; len++)
4760 lun->legacy_stats.ports[len].targ_port = len;
4761 #endif /* CTL_LEGACY_STATS */
4762 lun->stats.item = lun_number;
4765 * Now, before we insert this lun on the lun list, set the lun
4766 * inventory changed UA for all other luns.
4768 mtx_lock(&ctl_softc->ctl_lock);
4769 STAILQ_FOREACH(nlun, &ctl_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 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4775 ctl_softc->ctl_luns[lun_number] = lun;
4776 ctl_softc->num_luns++;
4777 mtx_unlock(&ctl_softc->ctl_lock);
4779 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4786 * - LUN has already been marked invalid and any pending I/O has been taken
4790 ctl_free_lun(struct ctl_lun *lun)
4792 struct ctl_softc *softc = lun->ctl_softc;
4793 struct ctl_lun *nlun;
4796 KASSERT(TAILQ_EMPTY(&lun->ooa_queue),
4797 ("Freeing a LUN %p with outstanding I/O!\n", lun));
4799 mtx_lock(&softc->ctl_lock);
4800 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4801 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4802 softc->ctl_luns[lun->lun] = NULL;
4804 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4805 mtx_lock(&nlun->lun_lock);
4806 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4807 mtx_unlock(&nlun->lun_lock);
4809 mtx_unlock(&softc->ctl_lock);
4812 * Tell the backend to free resources, if this LUN has a backend.
4814 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4815 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4817 lun->ie_reportcnt = UINT32_MAX;
4818 callout_drain(&lun->ie_callout);
4819 ctl_tpc_lun_shutdown(lun);
4820 mtx_destroy(&lun->lun_lock);
4821 free(lun->lun_devid, M_CTL);
4822 for (i = 0; i < ctl_max_ports; i++)
4823 free(lun->pending_ua[i], M_CTL);
4824 free(lun->pending_ua, M_DEVBUF);
4825 for (i = 0; i < ctl_max_ports; i++)
4826 free(lun->pr_keys[i], M_CTL);
4827 free(lun->pr_keys, M_DEVBUF);
4828 free(lun->write_buffer, M_CTL);
4829 free(lun->prevent, M_CTL);
4830 if (lun->flags & CTL_LUN_MALLOCED)
4837 ctl_create_lun(struct ctl_be_lun *be_lun)
4841 * ctl_alloc_lun() should handle all potential failure cases.
4843 ctl_alloc_lun(control_softc, NULL, be_lun);
4847 ctl_add_lun(struct ctl_be_lun *be_lun)
4849 struct ctl_softc *softc = control_softc;
4851 mtx_lock(&softc->ctl_lock);
4852 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links);
4853 mtx_unlock(&softc->ctl_lock);
4854 wakeup(&softc->pending_lun_queue);
4860 ctl_enable_lun(struct ctl_be_lun *be_lun)
4862 struct ctl_softc *softc;
4863 struct ctl_port *port, *nport;
4864 struct ctl_lun *lun;
4867 lun = (struct ctl_lun *)be_lun->ctl_lun;
4868 softc = lun->ctl_softc;
4870 mtx_lock(&softc->ctl_lock);
4871 mtx_lock(&lun->lun_lock);
4872 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4874 * eh? Why did we get called if the LUN is already
4877 mtx_unlock(&lun->lun_lock);
4878 mtx_unlock(&softc->ctl_lock);
4881 lun->flags &= ~CTL_LUN_DISABLED;
4882 mtx_unlock(&lun->lun_lock);
4884 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) {
4885 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4886 port->lun_map != NULL || port->lun_enable == NULL)
4890 * Drop the lock while we call the FETD's enable routine.
4891 * This can lead to a callback into CTL (at least in the
4892 * case of the internal initiator frontend.
4894 mtx_unlock(&softc->ctl_lock);
4895 retval = port->lun_enable(port->targ_lun_arg, lun->lun);
4896 mtx_lock(&softc->ctl_lock);
4898 printf("%s: FETD %s port %d returned error "
4899 "%d for lun_enable on lun %jd\n",
4900 __func__, port->port_name, port->targ_port,
4901 retval, (intmax_t)lun->lun);
4905 mtx_unlock(&softc->ctl_lock);
4906 ctl_isc_announce_lun(lun);
4912 ctl_disable_lun(struct ctl_be_lun *be_lun)
4914 struct ctl_softc *softc;
4915 struct ctl_port *port;
4916 struct ctl_lun *lun;
4919 lun = (struct ctl_lun *)be_lun->ctl_lun;
4920 softc = lun->ctl_softc;
4922 mtx_lock(&softc->ctl_lock);
4923 mtx_lock(&lun->lun_lock);
4924 if (lun->flags & CTL_LUN_DISABLED) {
4925 mtx_unlock(&lun->lun_lock);
4926 mtx_unlock(&softc->ctl_lock);
4929 lun->flags |= CTL_LUN_DISABLED;
4930 mtx_unlock(&lun->lun_lock);
4932 STAILQ_FOREACH(port, &softc->port_list, links) {
4933 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4934 port->lun_map != NULL || port->lun_disable == NULL)
4938 * Drop the lock before we call the frontend's disable
4939 * routine, to avoid lock order reversals.
4941 * XXX KDM what happens if the frontend list changes while
4942 * we're traversing it? It's unlikely, but should be handled.
4944 mtx_unlock(&softc->ctl_lock);
4945 retval = port->lun_disable(port->targ_lun_arg, lun->lun);
4946 mtx_lock(&softc->ctl_lock);
4948 printf("%s: FETD %s port %d returned error "
4949 "%d for lun_disable on lun %jd\n",
4950 __func__, port->port_name, port->targ_port,
4951 retval, (intmax_t)lun->lun);
4955 mtx_unlock(&softc->ctl_lock);
4956 ctl_isc_announce_lun(lun);
4962 ctl_start_lun(struct ctl_be_lun *be_lun)
4964 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4966 mtx_lock(&lun->lun_lock);
4967 lun->flags &= ~CTL_LUN_STOPPED;
4968 mtx_unlock(&lun->lun_lock);
4973 ctl_stop_lun(struct ctl_be_lun *be_lun)
4975 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4977 mtx_lock(&lun->lun_lock);
4978 lun->flags |= CTL_LUN_STOPPED;
4979 mtx_unlock(&lun->lun_lock);
4984 ctl_lun_no_media(struct ctl_be_lun *be_lun)
4986 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4988 mtx_lock(&lun->lun_lock);
4989 lun->flags |= CTL_LUN_NO_MEDIA;
4990 mtx_unlock(&lun->lun_lock);
4995 ctl_lun_has_media(struct ctl_be_lun *be_lun)
4997 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4998 union ctl_ha_msg msg;
5000 mtx_lock(&lun->lun_lock);
5001 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED);
5002 if (lun->flags & CTL_LUN_REMOVABLE)
5003 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE);
5004 mtx_unlock(&lun->lun_lock);
5005 if ((lun->flags & CTL_LUN_REMOVABLE) &&
5006 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
5007 bzero(&msg.ua, sizeof(msg.ua));
5008 msg.hdr.msg_type = CTL_MSG_UA;
5009 msg.hdr.nexus.initid = -1;
5010 msg.hdr.nexus.targ_port = -1;
5011 msg.hdr.nexus.targ_lun = lun->lun;
5012 msg.hdr.nexus.targ_mapped_lun = lun->lun;
5015 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE;
5016 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
5023 ctl_lun_ejected(struct ctl_be_lun *be_lun)
5025 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5027 mtx_lock(&lun->lun_lock);
5028 lun->flags |= CTL_LUN_EJECTED;
5029 mtx_unlock(&lun->lun_lock);
5034 ctl_lun_primary(struct ctl_be_lun *be_lun)
5036 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5038 mtx_lock(&lun->lun_lock);
5039 lun->flags |= CTL_LUN_PRIMARY_SC;
5040 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
5041 mtx_unlock(&lun->lun_lock);
5042 ctl_isc_announce_lun(lun);
5047 ctl_lun_secondary(struct ctl_be_lun *be_lun)
5049 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5051 mtx_lock(&lun->lun_lock);
5052 lun->flags &= ~CTL_LUN_PRIMARY_SC;
5053 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
5054 mtx_unlock(&lun->lun_lock);
5055 ctl_isc_announce_lun(lun);
5060 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
5062 struct ctl_softc *softc;
5063 struct ctl_lun *lun;
5065 lun = (struct ctl_lun *)be_lun->ctl_lun;
5066 softc = lun->ctl_softc;
5068 mtx_lock(&lun->lun_lock);
5071 * The LUN needs to be disabled before it can be marked invalid.
5073 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
5074 mtx_unlock(&lun->lun_lock);
5078 * Mark the LUN invalid.
5080 lun->flags |= CTL_LUN_INVALID;
5083 * If there is nothing in the OOA queue, go ahead and free the LUN.
5084 * If we have something in the OOA queue, we'll free it when the
5085 * last I/O completes.
5087 if (TAILQ_EMPTY(&lun->ooa_queue)) {
5088 mtx_unlock(&lun->lun_lock);
5091 mtx_unlock(&lun->lun_lock);
5097 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
5099 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5100 union ctl_ha_msg msg;
5102 mtx_lock(&lun->lun_lock);
5103 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE);
5104 mtx_unlock(&lun->lun_lock);
5105 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
5106 /* Send msg to other side. */
5107 bzero(&msg.ua, sizeof(msg.ua));
5108 msg.hdr.msg_type = CTL_MSG_UA;
5109 msg.hdr.nexus.initid = -1;
5110 msg.hdr.nexus.targ_port = -1;
5111 msg.hdr.nexus.targ_lun = lun->lun;
5112 msg.hdr.nexus.targ_mapped_lun = lun->lun;
5115 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE;
5116 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
5122 * Backend "memory move is complete" callback for requests that never
5123 * make it down to say RAIDCore's configuration code.
5126 ctl_config_move_done(union ctl_io *io)
5130 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
5131 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
5132 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
5134 if ((io->io_hdr.port_status != 0) &&
5135 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5136 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5137 ctl_set_internal_failure(&io->scsiio, /*sks_valid*/ 1,
5138 /*retry_count*/ io->io_hdr.port_status);
5139 } else if (io->scsiio.kern_data_resid != 0 &&
5140 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT &&
5141 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5142 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5143 ctl_set_invalid_field_ciu(&io->scsiio);
5146 if (ctl_debug & CTL_DEBUG_CDB_DATA)
5148 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
5149 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5150 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
5151 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5153 * XXX KDM just assuming a single pointer here, and not a
5154 * S/G list. If we start using S/G lists for config data,
5155 * we'll need to know how to clean them up here as well.
5157 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5158 free(io->scsiio.kern_data_ptr, M_CTL);
5160 retval = CTL_RETVAL_COMPLETE;
5163 * XXX KDM now we need to continue data movement. Some
5165 * - call ctl_scsiio() again? We don't do this for data
5166 * writes, because for those at least we know ahead of
5167 * time where the write will go and how long it is. For
5168 * config writes, though, that information is largely
5169 * contained within the write itself, thus we need to
5170 * parse out the data again.
5172 * - Call some other function once the data is in?
5176 * XXX KDM call ctl_scsiio() again for now, and check flag
5177 * bits to see whether we're allocated or not.
5179 retval = ctl_scsiio(&io->scsiio);
5185 * This gets called by a backend driver when it is done with a
5186 * data_submit method.
5189 ctl_data_submit_done(union ctl_io *io)
5192 * If the IO_CONT flag is set, we need to call the supplied
5193 * function to continue processing the I/O, instead of completing
5196 * If there is an error, though, we don't want to keep processing.
5197 * Instead, just send status back to the initiator.
5199 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5200 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5201 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5202 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5203 io->scsiio.io_cont(io);
5210 * This gets called by a backend driver when it is done with a
5211 * configuration write.
5214 ctl_config_write_done(union ctl_io *io)
5219 * If the IO_CONT flag is set, we need to call the supplied
5220 * function to continue processing the I/O, instead of completing
5223 * If there is an error, though, we don't want to keep processing.
5224 * Instead, just send status back to the initiator.
5226 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5227 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5228 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5229 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5230 io->scsiio.io_cont(io);
5234 * Since a configuration write can be done for commands that actually
5235 * have data allocated, like write buffer, and commands that have
5236 * no data, like start/stop unit, we need to check here.
5238 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5239 buf = io->scsiio.kern_data_ptr;
5248 ctl_config_read_done(union ctl_io *io)
5253 * If there is some error -- we are done, skip data transfer.
5255 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
5256 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5257 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
5258 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5259 buf = io->scsiio.kern_data_ptr;
5269 * If the IO_CONT flag is set, we need to call the supplied
5270 * function to continue processing the I/O, instead of completing
5273 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
5274 io->scsiio.io_cont(io);
5282 * SCSI release command.
5285 ctl_scsi_release(struct ctl_scsiio *ctsio)
5287 struct ctl_lun *lun = CTL_LUN(ctsio);
5290 CTL_DEBUG_PRINT(("ctl_scsi_release\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);
5305 * According to SPC, it is not an error for an intiator to attempt
5306 * to release a reservation on a LUN that isn't reserved, or that
5307 * is reserved by another initiator. The reservation can only be
5308 * released, though, by the initiator who made it or by one of
5309 * several reset type events.
5311 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5312 lun->flags &= ~CTL_LUN_RESERVED;
5314 mtx_unlock(&lun->lun_lock);
5316 ctl_set_success(ctsio);
5317 ctl_done((union ctl_io *)ctsio);
5318 return (CTL_RETVAL_COMPLETE);
5322 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5324 struct ctl_lun *lun = CTL_LUN(ctsio);
5327 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5329 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5332 * XXX KDM right now, we only support LUN reservation. We don't
5333 * support 3rd party reservations, or extent reservations, which
5334 * might actually need the parameter list. If we've gotten this
5335 * far, we've got a LUN reservation. Anything else got kicked out
5336 * above. So, according to SPC, ignore the length.
5339 mtx_lock(&lun->lun_lock);
5340 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5341 ctl_set_reservation_conflict(ctsio);
5345 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */
5346 if (lun->flags & CTL_LUN_PR_RESERVED) {
5347 ctl_set_success(ctsio);
5351 lun->flags |= CTL_LUN_RESERVED;
5352 lun->res_idx = residx;
5353 ctl_set_success(ctsio);
5356 mtx_unlock(&lun->lun_lock);
5357 ctl_done((union ctl_io *)ctsio);
5358 return (CTL_RETVAL_COMPLETE);
5362 ctl_start_stop(struct ctl_scsiio *ctsio)
5364 struct ctl_lun *lun = CTL_LUN(ctsio);
5365 struct scsi_start_stop_unit *cdb;
5368 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5370 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5372 if ((cdb->how & SSS_PC_MASK) == 0) {
5373 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
5374 (cdb->how & SSS_START) == 0) {
5377 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5378 if (ctl_get_prkey(lun, residx) == 0 ||
5379 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) {
5381 ctl_set_reservation_conflict(ctsio);
5382 ctl_done((union ctl_io *)ctsio);
5383 return (CTL_RETVAL_COMPLETE);
5387 if ((cdb->how & SSS_LOEJ) &&
5388 (lun->flags & CTL_LUN_REMOVABLE) == 0) {
5389 ctl_set_invalid_field(ctsio,
5395 ctl_done((union ctl_io *)ctsio);
5396 return (CTL_RETVAL_COMPLETE);
5399 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) &&
5400 lun->prevent_count > 0) {
5401 /* "Medium removal prevented" */
5402 ctl_set_sense(ctsio, /*current_error*/ 1,
5403 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ?
5404 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST,
5405 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE);
5406 ctl_done((union ctl_io *)ctsio);
5407 return (CTL_RETVAL_COMPLETE);
5411 retval = lun->backend->config_write((union ctl_io *)ctsio);
5416 ctl_prevent_allow(struct ctl_scsiio *ctsio)
5418 struct ctl_lun *lun = CTL_LUN(ctsio);
5419 struct scsi_prevent *cdb;
5423 CTL_DEBUG_PRINT(("ctl_prevent_allow\n"));
5425 cdb = (struct scsi_prevent *)ctsio->cdb;
5427 if ((lun->flags & CTL_LUN_REMOVABLE) == 0 || lun->prevent == NULL) {
5428 ctl_set_invalid_opcode(ctsio);
5429 ctl_done((union ctl_io *)ctsio);
5430 return (CTL_RETVAL_COMPLETE);
5433 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5434 mtx_lock(&lun->lun_lock);
5435 if ((cdb->how & PR_PREVENT) &&
5436 ctl_is_set(lun->prevent, initidx) == 0) {
5437 ctl_set_mask(lun->prevent, initidx);
5438 lun->prevent_count++;
5439 } else if ((cdb->how & PR_PREVENT) == 0 &&
5440 ctl_is_set(lun->prevent, initidx)) {
5441 ctl_clear_mask(lun->prevent, initidx);
5442 lun->prevent_count--;
5444 mtx_unlock(&lun->lun_lock);
5445 retval = lun->backend->config_write((union ctl_io *)ctsio);
5450 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5451 * we don't really do anything with the LBA and length fields if the user
5452 * passes them in. Instead we'll just flush out the cache for the entire
5456 ctl_sync_cache(struct ctl_scsiio *ctsio)
5458 struct ctl_lun *lun = CTL_LUN(ctsio);
5459 struct ctl_lba_len_flags *lbalen;
5460 uint64_t starting_lba;
5461 uint32_t block_count;
5465 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5469 switch (ctsio->cdb[0]) {
5470 case SYNCHRONIZE_CACHE: {
5471 struct scsi_sync_cache *cdb;
5472 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5474 starting_lba = scsi_4btoul(cdb->begin_lba);
5475 block_count = scsi_2btoul(cdb->lb_count);
5479 case SYNCHRONIZE_CACHE_16: {
5480 struct scsi_sync_cache_16 *cdb;
5481 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5483 starting_lba = scsi_8btou64(cdb->begin_lba);
5484 block_count = scsi_4btoul(cdb->lb_count);
5489 ctl_set_invalid_opcode(ctsio);
5490 ctl_done((union ctl_io *)ctsio);
5492 break; /* NOTREACHED */
5496 * We check the LBA and length, but don't do anything with them.
5497 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5498 * get flushed. This check will just help satisfy anyone who wants
5499 * to see an error for an out of range LBA.
5501 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5502 ctl_set_lba_out_of_range(ctsio,
5503 MAX(starting_lba, lun->be_lun->maxlba + 1));
5504 ctl_done((union ctl_io *)ctsio);
5508 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5509 lbalen->lba = starting_lba;
5510 lbalen->len = block_count;
5511 lbalen->flags = byte2;
5512 retval = lun->backend->config_write((union ctl_io *)ctsio);
5519 ctl_format(struct ctl_scsiio *ctsio)
5521 struct scsi_format *cdb;
5522 int length, defect_list_len;
5524 CTL_DEBUG_PRINT(("ctl_format\n"));
5526 cdb = (struct scsi_format *)ctsio->cdb;
5529 if (cdb->byte2 & SF_FMTDATA) {
5530 if (cdb->byte2 & SF_LONGLIST)
5531 length = sizeof(struct scsi_format_header_long);
5533 length = sizeof(struct scsi_format_header_short);
5536 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5538 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5539 ctsio->kern_data_len = length;
5540 ctsio->kern_total_len = length;
5541 ctsio->kern_rel_offset = 0;
5542 ctsio->kern_sg_entries = 0;
5543 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5544 ctsio->be_move_done = ctl_config_move_done;
5545 ctl_datamove((union ctl_io *)ctsio);
5547 return (CTL_RETVAL_COMPLETE);
5550 defect_list_len = 0;
5552 if (cdb->byte2 & SF_FMTDATA) {
5553 if (cdb->byte2 & SF_LONGLIST) {
5554 struct scsi_format_header_long *header;
5556 header = (struct scsi_format_header_long *)
5557 ctsio->kern_data_ptr;
5559 defect_list_len = scsi_4btoul(header->defect_list_len);
5560 if (defect_list_len != 0) {
5561 ctl_set_invalid_field(ctsio,
5570 struct scsi_format_header_short *header;
5572 header = (struct scsi_format_header_short *)
5573 ctsio->kern_data_ptr;
5575 defect_list_len = scsi_2btoul(header->defect_list_len);
5576 if (defect_list_len != 0) {
5577 ctl_set_invalid_field(ctsio,
5588 ctl_set_success(ctsio);
5591 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5592 free(ctsio->kern_data_ptr, M_CTL);
5593 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5596 ctl_done((union ctl_io *)ctsio);
5597 return (CTL_RETVAL_COMPLETE);
5601 ctl_read_buffer(struct ctl_scsiio *ctsio)
5603 struct ctl_lun *lun = CTL_LUN(ctsio);
5604 uint64_t buffer_offset;
5607 static uint8_t descr[4];
5608 static uint8_t echo_descr[4] = { 0 };
5610 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5612 switch (ctsio->cdb[0]) {
5614 struct scsi_read_buffer *cdb;
5616 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5617 buffer_offset = scsi_3btoul(cdb->offset);
5618 len = scsi_3btoul(cdb->length);
5622 case READ_BUFFER_16: {
5623 struct scsi_read_buffer_16 *cdb;
5625 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb;
5626 buffer_offset = scsi_8btou64(cdb->offset);
5627 len = scsi_4btoul(cdb->length);
5631 default: /* This shouldn't happen. */
5632 ctl_set_invalid_opcode(ctsio);
5633 ctl_done((union ctl_io *)ctsio);
5634 return (CTL_RETVAL_COMPLETE);
5637 if (buffer_offset > CTL_WRITE_BUFFER_SIZE ||
5638 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5639 ctl_set_invalid_field(ctsio,
5645 ctl_done((union ctl_io *)ctsio);
5646 return (CTL_RETVAL_COMPLETE);
5649 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5651 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5652 ctsio->kern_data_ptr = descr;
5653 len = min(len, sizeof(descr));
5654 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5655 ctsio->kern_data_ptr = echo_descr;
5656 len = min(len, sizeof(echo_descr));
5658 if (lun->write_buffer == NULL) {
5659 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5662 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5664 ctsio->kern_data_len = len;
5665 ctsio->kern_total_len = len;
5666 ctsio->kern_rel_offset = 0;
5667 ctsio->kern_sg_entries = 0;
5668 ctl_set_success(ctsio);
5669 ctsio->be_move_done = ctl_config_move_done;
5670 ctl_datamove((union ctl_io *)ctsio);
5671 return (CTL_RETVAL_COMPLETE);
5675 ctl_write_buffer(struct ctl_scsiio *ctsio)
5677 struct ctl_lun *lun = CTL_LUN(ctsio);
5678 struct scsi_write_buffer *cdb;
5679 int buffer_offset, len;
5681 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5683 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5685 len = scsi_3btoul(cdb->length);
5686 buffer_offset = scsi_3btoul(cdb->offset);
5688 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5689 ctl_set_invalid_field(ctsio,
5695 ctl_done((union ctl_io *)ctsio);
5696 return (CTL_RETVAL_COMPLETE);
5700 * If we've got a kernel request that hasn't been malloced yet,
5701 * malloc it and tell the caller the data buffer is here.
5703 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5704 if (lun->write_buffer == NULL) {
5705 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5708 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5709 ctsio->kern_data_len = len;
5710 ctsio->kern_total_len = len;
5711 ctsio->kern_rel_offset = 0;
5712 ctsio->kern_sg_entries = 0;
5713 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5714 ctsio->be_move_done = ctl_config_move_done;
5715 ctl_datamove((union ctl_io *)ctsio);
5717 return (CTL_RETVAL_COMPLETE);
5720 ctl_set_success(ctsio);
5721 ctl_done((union ctl_io *)ctsio);
5722 return (CTL_RETVAL_COMPLETE);
5726 ctl_write_same(struct ctl_scsiio *ctsio)
5728 struct ctl_lun *lun = CTL_LUN(ctsio);
5729 struct ctl_lba_len_flags *lbalen;
5731 uint32_t num_blocks;
5735 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5737 switch (ctsio->cdb[0]) {
5738 case WRITE_SAME_10: {
5739 struct scsi_write_same_10 *cdb;
5741 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5743 lba = scsi_4btoul(cdb->addr);
5744 num_blocks = scsi_2btoul(cdb->length);
5748 case WRITE_SAME_16: {
5749 struct scsi_write_same_16 *cdb;
5751 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5753 lba = scsi_8btou64(cdb->addr);
5754 num_blocks = scsi_4btoul(cdb->length);
5760 * We got a command we don't support. This shouldn't
5761 * happen, commands should be filtered out above us.
5763 ctl_set_invalid_opcode(ctsio);
5764 ctl_done((union ctl_io *)ctsio);
5766 return (CTL_RETVAL_COMPLETE);
5767 break; /* NOTREACHED */
5770 /* ANCHOR flag can be used only together with UNMAP */
5771 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) {
5772 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5773 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5774 ctl_done((union ctl_io *)ctsio);
5775 return (CTL_RETVAL_COMPLETE);
5779 * The first check is to make sure we're in bounds, the second
5780 * check is to catch wrap-around problems. If the lba + num blocks
5781 * is less than the lba, then we've wrapped around and the block
5782 * range is invalid anyway.
5784 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5785 || ((lba + num_blocks) < lba)) {
5786 ctl_set_lba_out_of_range(ctsio,
5787 MAX(lba, lun->be_lun->maxlba + 1));
5788 ctl_done((union ctl_io *)ctsio);
5789 return (CTL_RETVAL_COMPLETE);
5792 /* Zero number of blocks means "to the last logical block" */
5793 if (num_blocks == 0) {
5794 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5795 ctl_set_invalid_field(ctsio,
5801 ctl_done((union ctl_io *)ctsio);
5802 return (CTL_RETVAL_COMPLETE);
5804 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5807 len = lun->be_lun->blocksize;
5810 * If we've got a kernel request that hasn't been malloced yet,
5811 * malloc it and tell the caller the data buffer is here.
5813 if ((byte2 & SWS_NDOB) == 0 &&
5814 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5815 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5816 ctsio->kern_data_len = len;
5817 ctsio->kern_total_len = len;
5818 ctsio->kern_rel_offset = 0;
5819 ctsio->kern_sg_entries = 0;
5820 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5821 ctsio->be_move_done = ctl_config_move_done;
5822 ctl_datamove((union ctl_io *)ctsio);
5824 return (CTL_RETVAL_COMPLETE);
5827 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5829 lbalen->len = num_blocks;
5830 lbalen->flags = byte2;
5831 retval = lun->backend->config_write((union ctl_io *)ctsio);
5837 ctl_unmap(struct ctl_scsiio *ctsio)
5839 struct ctl_lun *lun = CTL_LUN(ctsio);
5840 struct scsi_unmap *cdb;
5841 struct ctl_ptr_len_flags *ptrlen;
5842 struct scsi_unmap_header *hdr;
5843 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5845 uint32_t num_blocks;
5849 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5851 cdb = (struct scsi_unmap *)ctsio->cdb;
5852 len = scsi_2btoul(cdb->length);
5856 * If we've got a kernel request that hasn't been malloced yet,
5857 * malloc it and tell the caller the data buffer is here.
5859 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5860 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5861 ctsio->kern_data_len = len;
5862 ctsio->kern_total_len = len;
5863 ctsio->kern_rel_offset = 0;
5864 ctsio->kern_sg_entries = 0;
5865 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5866 ctsio->be_move_done = ctl_config_move_done;
5867 ctl_datamove((union ctl_io *)ctsio);
5869 return (CTL_RETVAL_COMPLETE);
5872 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5873 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5874 if (len < sizeof (*hdr) ||
5875 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5876 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5877 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5878 ctl_set_invalid_field(ctsio,
5886 len = scsi_2btoul(hdr->desc_length);
5887 buf = (struct scsi_unmap_desc *)(hdr + 1);
5888 end = buf + len / sizeof(*buf);
5891 for (range = buf; range < end; range++) {
5892 lba = scsi_8btou64(range->lba);
5893 num_blocks = scsi_4btoul(range->length);
5894 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5895 || ((lba + num_blocks) < lba)) {
5896 ctl_set_lba_out_of_range(ctsio,
5897 MAX(lba, lun->be_lun->maxlba + 1));
5898 ctl_done((union ctl_io *)ctsio);
5899 return (CTL_RETVAL_COMPLETE);
5901 if (num_blocks != 0)
5906 * Block backend can not handle zero last range.
5907 * Filter it out and return if there is nothing left.
5909 len = (uint8_t *)endnz - (uint8_t *)buf;
5911 ctl_set_success(ctsio);
5915 mtx_lock(&lun->lun_lock);
5916 ptrlen = (struct ctl_ptr_len_flags *)
5917 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5918 ptrlen->ptr = (void *)buf;
5920 ptrlen->flags = byte2;
5921 ctl_check_blocked(lun);
5922 mtx_unlock(&lun->lun_lock);
5924 retval = lun->backend->config_write((union ctl_io *)ctsio);
5928 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5929 free(ctsio->kern_data_ptr, M_CTL);
5930 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5932 ctl_done((union ctl_io *)ctsio);
5933 return (CTL_RETVAL_COMPLETE);
5937 ctl_default_page_handler(struct ctl_scsiio *ctsio,
5938 struct ctl_page_index *page_index, uint8_t *page_ptr)
5940 struct ctl_lun *lun = CTL_LUN(ctsio);
5941 uint8_t *current_cp;
5945 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5948 current_cp = (page_index->page_data + (page_index->page_len *
5951 mtx_lock(&lun->lun_lock);
5952 if (memcmp(current_cp, page_ptr, page_index->page_len)) {
5953 memcpy(current_cp, page_ptr, page_index->page_len);
5957 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5958 mtx_unlock(&lun->lun_lock);
5960 ctl_isc_announce_mode(lun,
5961 ctl_get_initindex(&ctsio->io_hdr.nexus),
5962 page_index->page_code, page_index->subpage);
5964 return (CTL_RETVAL_COMPLETE);
5968 ctl_ie_timer(void *arg)
5970 struct ctl_lun *lun = arg;
5973 if (lun->ie_asc == 0)
5976 if (lun->MODE_IE.mrie == SIEP_MRIE_UA)
5977 ctl_est_ua_all(lun, -1, CTL_UA_IE);
5979 lun->ie_reported = 0;
5981 if (lun->ie_reportcnt < scsi_4btoul(lun->MODE_IE.report_count)) {
5982 lun->ie_reportcnt++;
5983 t = scsi_4btoul(lun->MODE_IE.interval_timer);
5984 if (t == 0 || t == UINT32_MAX)
5985 t = 3000; /* 5 min */
5986 callout_schedule(&lun->ie_callout, t * hz / 10);
5991 ctl_ie_page_handler(struct ctl_scsiio *ctsio,
5992 struct ctl_page_index *page_index, uint8_t *page_ptr)
5994 struct ctl_lun *lun = CTL_LUN(ctsio);
5995 struct scsi_info_exceptions_page *pg;
5998 (void)ctl_default_page_handler(ctsio, page_index, page_ptr);
6000 pg = (struct scsi_info_exceptions_page *)page_ptr;
6001 mtx_lock(&lun->lun_lock);
6002 if (pg->info_flags & SIEP_FLAGS_TEST) {
6004 lun->ie_ascq = 0xff;
6005 if (pg->mrie == SIEP_MRIE_UA) {
6006 ctl_est_ua_all(lun, -1, CTL_UA_IE);
6007 lun->ie_reported = 1;
6009 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
6010 lun->ie_reported = -1;
6012 lun->ie_reportcnt = 1;
6013 if (lun->ie_reportcnt < scsi_4btoul(pg->report_count)) {
6014 lun->ie_reportcnt++;
6015 t = scsi_4btoul(pg->interval_timer);
6016 if (t == 0 || t == UINT32_MAX)
6017 t = 3000; /* 5 min */
6018 callout_reset(&lun->ie_callout, t * hz / 10,
6024 lun->ie_reported = 1;
6025 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
6026 lun->ie_reportcnt = UINT32_MAX;
6027 callout_stop(&lun->ie_callout);
6029 mtx_unlock(&lun->lun_lock);
6030 return (CTL_RETVAL_COMPLETE);
6034 ctl_do_mode_select(union ctl_io *io)
6036 struct ctl_lun *lun = CTL_LUN(io);
6037 struct scsi_mode_page_header *page_header;
6038 struct ctl_page_index *page_index;
6039 struct ctl_scsiio *ctsio;
6040 int page_len, page_len_offset, page_len_size;
6041 union ctl_modepage_info *modepage_info;
6042 uint16_t *len_left, *len_used;
6045 ctsio = &io->scsiio;
6049 modepage_info = (union ctl_modepage_info *)
6050 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6051 len_left = &modepage_info->header.len_left;
6052 len_used = &modepage_info->header.len_used;
6056 page_header = (struct scsi_mode_page_header *)
6057 (ctsio->kern_data_ptr + *len_used);
6059 if (*len_left == 0) {
6060 free(ctsio->kern_data_ptr, M_CTL);
6061 ctl_set_success(ctsio);
6062 ctl_done((union ctl_io *)ctsio);
6063 return (CTL_RETVAL_COMPLETE);
6064 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6066 free(ctsio->kern_data_ptr, M_CTL);
6067 ctl_set_param_len_error(ctsio);
6068 ctl_done((union ctl_io *)ctsio);
6069 return (CTL_RETVAL_COMPLETE);
6071 } else if ((page_header->page_code & SMPH_SPF)
6072 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6074 free(ctsio->kern_data_ptr, M_CTL);
6075 ctl_set_param_len_error(ctsio);
6076 ctl_done((union ctl_io *)ctsio);
6077 return (CTL_RETVAL_COMPLETE);
6082 * XXX KDM should we do something with the block descriptor?
6084 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6085 page_index = &lun->mode_pages.index[i];
6086 if (lun->be_lun->lun_type == T_DIRECT &&
6087 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6089 if (lun->be_lun->lun_type == T_PROCESSOR &&
6090 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6092 if (lun->be_lun->lun_type == T_CDROM &&
6093 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6096 if ((page_index->page_code & SMPH_PC_MASK) !=
6097 (page_header->page_code & SMPH_PC_MASK))
6101 * If neither page has a subpage code, then we've got a
6104 if (((page_index->page_code & SMPH_SPF) == 0)
6105 && ((page_header->page_code & SMPH_SPF) == 0)) {
6106 page_len = page_header->page_length;
6111 * If both pages have subpages, then the subpage numbers
6114 if ((page_index->page_code & SMPH_SPF)
6115 && (page_header->page_code & SMPH_SPF)) {
6116 struct scsi_mode_page_header_sp *sph;
6118 sph = (struct scsi_mode_page_header_sp *)page_header;
6119 if (page_index->subpage == sph->subpage) {
6120 page_len = scsi_2btoul(sph->page_length);
6127 * If we couldn't find the page, or if we don't have a mode select
6128 * handler for it, send back an error to the user.
6130 if ((i >= CTL_NUM_MODE_PAGES)
6131 || (page_index->select_handler == NULL)) {
6132 ctl_set_invalid_field(ctsio,
6135 /*field*/ *len_used,
6138 free(ctsio->kern_data_ptr, M_CTL);
6139 ctl_done((union ctl_io *)ctsio);
6140 return (CTL_RETVAL_COMPLETE);
6143 if (page_index->page_code & SMPH_SPF) {
6144 page_len_offset = 2;
6148 page_len_offset = 1;
6152 * If the length the initiator gives us isn't the one we specify in
6153 * the mode page header, or if they didn't specify enough data in
6154 * the CDB to avoid truncating this page, kick out the request.
6156 if (page_len != page_index->page_len - page_len_offset - page_len_size) {
6157 ctl_set_invalid_field(ctsio,
6160 /*field*/ *len_used + page_len_offset,
6163 free(ctsio->kern_data_ptr, M_CTL);
6164 ctl_done((union ctl_io *)ctsio);
6165 return (CTL_RETVAL_COMPLETE);
6167 if (*len_left < page_index->page_len) {
6168 free(ctsio->kern_data_ptr, M_CTL);
6169 ctl_set_param_len_error(ctsio);
6170 ctl_done((union ctl_io *)ctsio);
6171 return (CTL_RETVAL_COMPLETE);
6175 * Run through the mode page, checking to make sure that the bits
6176 * the user changed are actually legal for him to change.
6178 for (i = 0; i < page_index->page_len; i++) {
6179 uint8_t *user_byte, *change_mask, *current_byte;
6183 user_byte = (uint8_t *)page_header + i;
6184 change_mask = page_index->page_data +
6185 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6186 current_byte = page_index->page_data +
6187 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6190 * Check to see whether the user set any bits in this byte
6191 * that he is not allowed to set.
6193 if ((*user_byte & ~(*change_mask)) ==
6194 (*current_byte & ~(*change_mask)))
6198 * Go through bit by bit to determine which one is illegal.
6201 for (j = 7; j >= 0; j--) {
6202 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6203 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6208 ctl_set_invalid_field(ctsio,
6211 /*field*/ *len_used + i,
6214 free(ctsio->kern_data_ptr, M_CTL);
6215 ctl_done((union ctl_io *)ctsio);
6216 return (CTL_RETVAL_COMPLETE);
6220 * Decrement these before we call the page handler, since we may
6221 * end up getting called back one way or another before the handler
6222 * returns to this context.
6224 *len_left -= page_index->page_len;
6225 *len_used += page_index->page_len;
6227 retval = page_index->select_handler(ctsio, page_index,
6228 (uint8_t *)page_header);
6231 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6232 * wait until this queued command completes to finish processing
6233 * the mode page. If it returns anything other than
6234 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6235 * already set the sense information, freed the data pointer, and
6236 * completed the io for us.
6238 if (retval != CTL_RETVAL_COMPLETE)
6239 goto bailout_no_done;
6242 * If the initiator sent us more than one page, parse the next one.
6247 ctl_set_success(ctsio);
6248 free(ctsio->kern_data_ptr, M_CTL);
6249 ctl_done((union ctl_io *)ctsio);
6253 return (CTL_RETVAL_COMPLETE);
6258 ctl_mode_select(struct ctl_scsiio *ctsio)
6260 struct ctl_lun *lun = CTL_LUN(ctsio);
6261 union ctl_modepage_info *modepage_info;
6262 int bd_len, i, header_size, param_len, pf, rtd, sp;
6265 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6266 switch (ctsio->cdb[0]) {
6267 case MODE_SELECT_6: {
6268 struct scsi_mode_select_6 *cdb;
6270 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6272 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6273 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6274 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6275 param_len = cdb->length;
6276 header_size = sizeof(struct scsi_mode_header_6);
6279 case MODE_SELECT_10: {
6280 struct scsi_mode_select_10 *cdb;
6282 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6284 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6285 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6286 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6287 param_len = scsi_2btoul(cdb->length);
6288 header_size = sizeof(struct scsi_mode_header_10);
6292 ctl_set_invalid_opcode(ctsio);
6293 ctl_done((union ctl_io *)ctsio);
6294 return (CTL_RETVAL_COMPLETE);
6298 if (param_len != 0) {
6299 ctl_set_invalid_field(ctsio, /*sks_valid*/ 0,
6300 /*command*/ 1, /*field*/ 0,
6301 /*bit_valid*/ 0, /*bit*/ 0);
6302 ctl_done((union ctl_io *)ctsio);
6303 return (CTL_RETVAL_COMPLETE);
6306 /* Revert to defaults. */
6307 ctl_init_page_index(lun);
6308 mtx_lock(&lun->lun_lock);
6309 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
6310 mtx_unlock(&lun->lun_lock);
6311 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6312 ctl_isc_announce_mode(lun, -1,
6313 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
6314 lun->mode_pages.index[i].subpage);
6316 ctl_set_success(ctsio);
6317 ctl_done((union ctl_io *)ctsio);
6318 return (CTL_RETVAL_COMPLETE);
6323 * "A parameter list length of zero indicates that the Data-Out Buffer
6324 * shall be empty. This condition shall not be considered as an error."
6326 if (param_len == 0) {
6327 ctl_set_success(ctsio);
6328 ctl_done((union ctl_io *)ctsio);
6329 return (CTL_RETVAL_COMPLETE);
6333 * Since we'll hit this the first time through, prior to
6334 * allocation, we don't need to free a data buffer here.
6336 if (param_len < header_size) {
6337 ctl_set_param_len_error(ctsio);
6338 ctl_done((union ctl_io *)ctsio);
6339 return (CTL_RETVAL_COMPLETE);
6343 * Allocate the data buffer and grab the user's data. In theory,
6344 * we shouldn't have to sanity check the parameter list length here
6345 * because the maximum size is 64K. We should be able to malloc
6346 * that much without too many problems.
6348 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6349 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6350 ctsio->kern_data_len = param_len;
6351 ctsio->kern_total_len = param_len;
6352 ctsio->kern_rel_offset = 0;
6353 ctsio->kern_sg_entries = 0;
6354 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6355 ctsio->be_move_done = ctl_config_move_done;
6356 ctl_datamove((union ctl_io *)ctsio);
6358 return (CTL_RETVAL_COMPLETE);
6361 switch (ctsio->cdb[0]) {
6362 case MODE_SELECT_6: {
6363 struct scsi_mode_header_6 *mh6;
6365 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6366 bd_len = mh6->blk_desc_len;
6369 case MODE_SELECT_10: {
6370 struct scsi_mode_header_10 *mh10;
6372 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6373 bd_len = scsi_2btoul(mh10->blk_desc_len);
6377 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6380 if (param_len < (header_size + bd_len)) {
6381 free(ctsio->kern_data_ptr, M_CTL);
6382 ctl_set_param_len_error(ctsio);
6383 ctl_done((union ctl_io *)ctsio);
6384 return (CTL_RETVAL_COMPLETE);
6388 * Set the IO_CONT flag, so that if this I/O gets passed to
6389 * ctl_config_write_done(), it'll get passed back to
6390 * ctl_do_mode_select() for further processing, or completion if
6393 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6394 ctsio->io_cont = ctl_do_mode_select;
6396 modepage_info = (union ctl_modepage_info *)
6397 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6398 memset(modepage_info, 0, sizeof(*modepage_info));
6399 modepage_info->header.len_left = param_len - header_size - bd_len;
6400 modepage_info->header.len_used = header_size + bd_len;
6402 return (ctl_do_mode_select((union ctl_io *)ctsio));
6406 ctl_mode_sense(struct ctl_scsiio *ctsio)
6408 struct ctl_lun *lun = CTL_LUN(ctsio);
6409 int pc, page_code, dbd, llba, subpage;
6410 int alloc_len, page_len, header_len, total_len;
6411 struct scsi_mode_block_descr *block_desc;
6412 struct ctl_page_index *page_index;
6418 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6420 switch (ctsio->cdb[0]) {
6421 case MODE_SENSE_6: {
6422 struct scsi_mode_sense_6 *cdb;
6424 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6426 header_len = sizeof(struct scsi_mode_hdr_6);
6427 if (cdb->byte2 & SMS_DBD)
6430 header_len += sizeof(struct scsi_mode_block_descr);
6432 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6433 page_code = cdb->page & SMS_PAGE_CODE;
6434 subpage = cdb->subpage;
6435 alloc_len = cdb->length;
6438 case MODE_SENSE_10: {
6439 struct scsi_mode_sense_10 *cdb;
6441 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6443 header_len = sizeof(struct scsi_mode_hdr_10);
6445 if (cdb->byte2 & SMS_DBD)
6448 header_len += sizeof(struct scsi_mode_block_descr);
6449 if (cdb->byte2 & SMS10_LLBAA)
6451 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6452 page_code = cdb->page & SMS_PAGE_CODE;
6453 subpage = cdb->subpage;
6454 alloc_len = scsi_2btoul(cdb->length);
6458 ctl_set_invalid_opcode(ctsio);
6459 ctl_done((union ctl_io *)ctsio);
6460 return (CTL_RETVAL_COMPLETE);
6461 break; /* NOTREACHED */
6465 * We have to make a first pass through to calculate the size of
6466 * the pages that match the user's query. Then we allocate enough
6467 * memory to hold it, and actually copy the data into the buffer.
6469 switch (page_code) {
6470 case SMS_ALL_PAGES_PAGE: {
6476 * At the moment, values other than 0 and 0xff here are
6477 * reserved according to SPC-3.
6479 if ((subpage != SMS_SUBPAGE_PAGE_0)
6480 && (subpage != SMS_SUBPAGE_ALL)) {
6481 ctl_set_invalid_field(ctsio,
6487 ctl_done((union ctl_io *)ctsio);
6488 return (CTL_RETVAL_COMPLETE);
6491 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6492 page_index = &lun->mode_pages.index[i];
6494 /* Make sure the page is supported for this dev type */
6495 if (lun->be_lun->lun_type == T_DIRECT &&
6496 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6498 if (lun->be_lun->lun_type == T_PROCESSOR &&
6499 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6501 if (lun->be_lun->lun_type == T_CDROM &&
6502 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6506 * We don't use this subpage if the user didn't
6507 * request all subpages.
6509 if ((page_index->subpage != 0)
6510 && (subpage == SMS_SUBPAGE_PAGE_0))
6514 printf("found page %#x len %d\n",
6515 page_index->page_code & SMPH_PC_MASK,
6516 page_index->page_len);
6518 page_len += page_index->page_len;
6527 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6528 page_index = &lun->mode_pages.index[i];
6530 /* Make sure the page is supported for this dev type */
6531 if (lun->be_lun->lun_type == T_DIRECT &&
6532 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6534 if (lun->be_lun->lun_type == T_PROCESSOR &&
6535 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6537 if (lun->be_lun->lun_type == T_CDROM &&
6538 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6541 /* Look for the right page code */
6542 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6545 /* Look for the right subpage or the subpage wildcard*/
6546 if ((page_index->subpage != subpage)
6547 && (subpage != SMS_SUBPAGE_ALL))
6551 printf("found page %#x len %d\n",
6552 page_index->page_code & SMPH_PC_MASK,
6553 page_index->page_len);
6556 page_len += page_index->page_len;
6559 if (page_len == 0) {
6560 ctl_set_invalid_field(ctsio,
6566 ctl_done((union ctl_io *)ctsio);
6567 return (CTL_RETVAL_COMPLETE);
6573 total_len = header_len + page_len;
6575 printf("header_len = %d, page_len = %d, total_len = %d\n",
6576 header_len, page_len, total_len);
6579 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6580 ctsio->kern_sg_entries = 0;
6581 ctsio->kern_rel_offset = 0;
6582 ctsio->kern_data_len = min(total_len, alloc_len);
6583 ctsio->kern_total_len = ctsio->kern_data_len;
6585 switch (ctsio->cdb[0]) {
6586 case MODE_SENSE_6: {
6587 struct scsi_mode_hdr_6 *header;
6589 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6591 header->datalen = MIN(total_len - 1, 254);
6592 if (lun->be_lun->lun_type == T_DIRECT) {
6593 header->dev_specific = 0x10; /* DPOFUA */
6594 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6595 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6596 header->dev_specific |= 0x80; /* WP */
6599 header->block_descr_len = 0;
6601 header->block_descr_len =
6602 sizeof(struct scsi_mode_block_descr);
6603 block_desc = (struct scsi_mode_block_descr *)&header[1];
6606 case MODE_SENSE_10: {
6607 struct scsi_mode_hdr_10 *header;
6610 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6612 datalen = MIN(total_len - 2, 65533);
6613 scsi_ulto2b(datalen, header->datalen);
6614 if (lun->be_lun->lun_type == T_DIRECT) {
6615 header->dev_specific = 0x10; /* DPOFUA */
6616 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6617 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6618 header->dev_specific |= 0x80; /* WP */
6621 scsi_ulto2b(0, header->block_descr_len);
6623 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6624 header->block_descr_len);
6625 block_desc = (struct scsi_mode_block_descr *)&header[1];
6629 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6633 * If we've got a disk, use its blocksize in the block
6634 * descriptor. Otherwise, just set it to 0.
6637 if (lun->be_lun->lun_type == T_DIRECT)
6638 scsi_ulto3b(lun->be_lun->blocksize,
6639 block_desc->block_len);
6641 scsi_ulto3b(0, block_desc->block_len);
6644 switch (page_code) {
6645 case SMS_ALL_PAGES_PAGE: {
6648 data_used = header_len;
6649 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6650 struct ctl_page_index *page_index;
6652 page_index = &lun->mode_pages.index[i];
6653 if (lun->be_lun->lun_type == T_DIRECT &&
6654 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6656 if (lun->be_lun->lun_type == T_PROCESSOR &&
6657 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6659 if (lun->be_lun->lun_type == T_CDROM &&
6660 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6664 * We don't use this subpage if the user didn't
6665 * request all subpages. We already checked (above)
6666 * to make sure the user only specified a subpage
6667 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6669 if ((page_index->subpage != 0)
6670 && (subpage == SMS_SUBPAGE_PAGE_0))
6674 * Call the handler, if it exists, to update the
6675 * page to the latest values.
6677 if (page_index->sense_handler != NULL)
6678 page_index->sense_handler(ctsio, page_index,pc);
6680 memcpy(ctsio->kern_data_ptr + data_used,
6681 page_index->page_data +
6682 (page_index->page_len * pc),
6683 page_index->page_len);
6684 data_used += page_index->page_len;
6691 data_used = header_len;
6693 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6694 struct ctl_page_index *page_index;
6696 page_index = &lun->mode_pages.index[i];
6698 /* Look for the right page code */
6699 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6702 /* Look for the right subpage or the subpage wildcard*/
6703 if ((page_index->subpage != subpage)
6704 && (subpage != SMS_SUBPAGE_ALL))
6707 /* Make sure the page is supported for this dev type */
6708 if (lun->be_lun->lun_type == T_DIRECT &&
6709 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6711 if (lun->be_lun->lun_type == T_PROCESSOR &&
6712 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6714 if (lun->be_lun->lun_type == T_CDROM &&
6715 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6719 * Call the handler, if it exists, to update the
6720 * page to the latest values.
6722 if (page_index->sense_handler != NULL)
6723 page_index->sense_handler(ctsio, page_index,pc);
6725 memcpy(ctsio->kern_data_ptr + data_used,
6726 page_index->page_data +
6727 (page_index->page_len * pc),
6728 page_index->page_len);
6729 data_used += page_index->page_len;
6735 ctl_set_success(ctsio);
6736 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6737 ctsio->be_move_done = ctl_config_move_done;
6738 ctl_datamove((union ctl_io *)ctsio);
6739 return (CTL_RETVAL_COMPLETE);
6743 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6744 struct ctl_page_index *page_index,
6747 struct ctl_lun *lun = CTL_LUN(ctsio);
6748 struct scsi_log_param_header *phdr;
6752 data = page_index->page_data;
6754 if (lun->backend->lun_attr != NULL &&
6755 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6757 phdr = (struct scsi_log_param_header *)data;
6758 scsi_ulto2b(0x0001, phdr->param_code);
6759 phdr->param_control = SLP_LBIN | SLP_LP;
6760 phdr->param_len = 8;
6761 data = (uint8_t *)(phdr + 1);
6762 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6763 data[4] = 0x02; /* per-pool */
6764 data += phdr->param_len;
6767 if (lun->backend->lun_attr != NULL &&
6768 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6770 phdr = (struct scsi_log_param_header *)data;
6771 scsi_ulto2b(0x0002, phdr->param_code);
6772 phdr->param_control = SLP_LBIN | SLP_LP;
6773 phdr->param_len = 8;
6774 data = (uint8_t *)(phdr + 1);
6775 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6776 data[4] = 0x01; /* per-LUN */
6777 data += phdr->param_len;
6780 if (lun->backend->lun_attr != NULL &&
6781 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6783 phdr = (struct scsi_log_param_header *)data;
6784 scsi_ulto2b(0x00f1, phdr->param_code);
6785 phdr->param_control = SLP_LBIN | SLP_LP;
6786 phdr->param_len = 8;
6787 data = (uint8_t *)(phdr + 1);
6788 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6789 data[4] = 0x02; /* per-pool */
6790 data += phdr->param_len;
6793 if (lun->backend->lun_attr != NULL &&
6794 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6796 phdr = (struct scsi_log_param_header *)data;
6797 scsi_ulto2b(0x00f2, phdr->param_code);
6798 phdr->param_control = SLP_LBIN | SLP_LP;
6799 phdr->param_len = 8;
6800 data = (uint8_t *)(phdr + 1);
6801 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6802 data[4] = 0x02; /* per-pool */
6803 data += phdr->param_len;
6806 page_index->page_len = data - page_index->page_data;
6811 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
6812 struct ctl_page_index *page_index,
6815 struct ctl_lun *lun = CTL_LUN(ctsio);
6816 struct stat_page *data;
6819 data = (struct stat_page *)page_index->page_data;
6821 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
6822 data->sap.hdr.param_control = SLP_LBIN;
6823 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
6824 sizeof(struct scsi_log_param_header);
6825 scsi_u64to8b(lun->stats.operations[CTL_STATS_READ],
6826 data->sap.read_num);
6827 scsi_u64to8b(lun->stats.operations[CTL_STATS_WRITE],
6828 data->sap.write_num);
6829 if (lun->be_lun->blocksize > 0) {
6830 scsi_u64to8b(lun->stats.bytes[CTL_STATS_WRITE] /
6831 lun->be_lun->blocksize, data->sap.recvieved_lba);
6832 scsi_u64to8b(lun->stats.bytes[CTL_STATS_READ] /
6833 lun->be_lun->blocksize, data->sap.transmitted_lba);
6835 t = &lun->stats.time[CTL_STATS_READ];
6836 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000),
6837 data->sap.read_int);
6838 t = &lun->stats.time[CTL_STATS_WRITE];
6839 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000),
6840 data->sap.write_int);
6841 scsi_u64to8b(0, data->sap.weighted_num);
6842 scsi_u64to8b(0, data->sap.weighted_int);
6843 scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
6844 data->it.hdr.param_control = SLP_LBIN;
6845 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
6846 sizeof(struct scsi_log_param_header);
6848 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
6850 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
6851 data->it.hdr.param_control = SLP_LBIN;
6852 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
6853 sizeof(struct scsi_log_param_header);
6854 scsi_ulto4b(3, data->ti.exponent);
6855 scsi_ulto4b(1, data->ti.integer);
6860 ctl_ie_log_sense_handler(struct ctl_scsiio *ctsio,
6861 struct ctl_page_index *page_index,
6864 struct ctl_lun *lun = CTL_LUN(ctsio);
6865 struct scsi_log_informational_exceptions *data;
6867 data = (struct scsi_log_informational_exceptions *)page_index->page_data;
6869 scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code);
6870 data->hdr.param_control = SLP_LBIN;
6871 data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) -
6872 sizeof(struct scsi_log_param_header);
6873 data->ie_asc = lun->ie_asc;
6874 data->ie_ascq = lun->ie_ascq;
6875 data->temperature = 0xff;
6880 ctl_log_sense(struct ctl_scsiio *ctsio)
6882 struct ctl_lun *lun = CTL_LUN(ctsio);
6883 int i, pc, page_code, subpage;
6884 int alloc_len, total_len;
6885 struct ctl_page_index *page_index;
6886 struct scsi_log_sense *cdb;
6887 struct scsi_log_header *header;
6889 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6891 cdb = (struct scsi_log_sense *)ctsio->cdb;
6892 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6893 page_code = cdb->page & SLS_PAGE_CODE;
6894 subpage = cdb->subpage;
6895 alloc_len = scsi_2btoul(cdb->length);
6898 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6899 page_index = &lun->log_pages.index[i];
6901 /* Look for the right page code */
6902 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6905 /* Look for the right subpage or the subpage wildcard*/
6906 if (page_index->subpage != subpage)
6911 if (i >= CTL_NUM_LOG_PAGES) {
6912 ctl_set_invalid_field(ctsio,
6918 ctl_done((union ctl_io *)ctsio);
6919 return (CTL_RETVAL_COMPLETE);
6922 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6924 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6925 ctsio->kern_sg_entries = 0;
6926 ctsio->kern_rel_offset = 0;
6927 ctsio->kern_data_len = min(total_len, alloc_len);
6928 ctsio->kern_total_len = ctsio->kern_data_len;
6930 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
6931 header->page = page_index->page_code;
6932 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING)
6933 header->page |= SL_DS;
6934 if (page_index->subpage) {
6935 header->page |= SL_SPF;
6936 header->subpage = page_index->subpage;
6938 scsi_ulto2b(page_index->page_len, header->datalen);
6941 * Call the handler, if it exists, to update the
6942 * page to the latest values.
6944 if (page_index->sense_handler != NULL)
6945 page_index->sense_handler(ctsio, page_index, pc);
6947 memcpy(header + 1, page_index->page_data, page_index->page_len);
6949 ctl_set_success(ctsio);
6950 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6951 ctsio->be_move_done = ctl_config_move_done;
6952 ctl_datamove((union ctl_io *)ctsio);
6953 return (CTL_RETVAL_COMPLETE);
6957 ctl_read_capacity(struct ctl_scsiio *ctsio)
6959 struct ctl_lun *lun = CTL_LUN(ctsio);
6960 struct scsi_read_capacity *cdb;
6961 struct scsi_read_capacity_data *data;
6964 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6966 cdb = (struct scsi_read_capacity *)ctsio->cdb;
6968 lba = scsi_4btoul(cdb->addr);
6969 if (((cdb->pmi & SRC_PMI) == 0)
6971 ctl_set_invalid_field(/*ctsio*/ ctsio,
6977 ctl_done((union ctl_io *)ctsio);
6978 return (CTL_RETVAL_COMPLETE);
6981 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6982 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6983 ctsio->kern_data_len = sizeof(*data);
6984 ctsio->kern_total_len = sizeof(*data);
6985 ctsio->kern_rel_offset = 0;
6986 ctsio->kern_sg_entries = 0;
6989 * If the maximum LBA is greater than 0xfffffffe, the user must
6990 * issue a SERVICE ACTION IN (16) command, with the read capacity
6991 * serivce action set.
6993 if (lun->be_lun->maxlba > 0xfffffffe)
6994 scsi_ulto4b(0xffffffff, data->addr);
6996 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6999 * XXX KDM this may not be 512 bytes...
7001 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7003 ctl_set_success(ctsio);
7004 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7005 ctsio->be_move_done = ctl_config_move_done;
7006 ctl_datamove((union ctl_io *)ctsio);
7007 return (CTL_RETVAL_COMPLETE);
7011 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
7013 struct ctl_lun *lun = CTL_LUN(ctsio);
7014 struct scsi_read_capacity_16 *cdb;
7015 struct scsi_read_capacity_data_long *data;
7019 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
7021 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
7023 alloc_len = scsi_4btoul(cdb->alloc_len);
7024 lba = scsi_8btou64(cdb->addr);
7026 if ((cdb->reladr & SRC16_PMI)
7028 ctl_set_invalid_field(/*ctsio*/ ctsio,
7034 ctl_done((union ctl_io *)ctsio);
7035 return (CTL_RETVAL_COMPLETE);
7038 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7039 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
7040 ctsio->kern_rel_offset = 0;
7041 ctsio->kern_sg_entries = 0;
7042 ctsio->kern_data_len = min(sizeof(*data), alloc_len);
7043 ctsio->kern_total_len = ctsio->kern_data_len;
7045 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7046 /* XXX KDM this may not be 512 bytes... */
7047 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7048 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7049 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7050 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7051 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
7053 ctl_set_success(ctsio);
7054 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7055 ctsio->be_move_done = ctl_config_move_done;
7056 ctl_datamove((union ctl_io *)ctsio);
7057 return (CTL_RETVAL_COMPLETE);
7061 ctl_get_lba_status(struct ctl_scsiio *ctsio)
7063 struct ctl_lun *lun = CTL_LUN(ctsio);
7064 struct scsi_get_lba_status *cdb;
7065 struct scsi_get_lba_status_data *data;
7066 struct ctl_lba_len_flags *lbalen;
7068 uint32_t alloc_len, total_len;
7071 CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
7073 cdb = (struct scsi_get_lba_status *)ctsio->cdb;
7074 lba = scsi_8btou64(cdb->addr);
7075 alloc_len = scsi_4btoul(cdb->alloc_len);
7077 if (lba > lun->be_lun->maxlba) {
7078 ctl_set_lba_out_of_range(ctsio, lba);
7079 ctl_done((union ctl_io *)ctsio);
7080 return (CTL_RETVAL_COMPLETE);
7083 total_len = sizeof(*data) + sizeof(data->descr[0]);
7084 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7085 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
7086 ctsio->kern_rel_offset = 0;
7087 ctsio->kern_sg_entries = 0;
7088 ctsio->kern_data_len = min(total_len, alloc_len);
7089 ctsio->kern_total_len = ctsio->kern_data_len;
7091 /* Fill dummy data in case backend can't tell anything. */
7092 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
7093 scsi_u64to8b(lba, data->descr[0].addr);
7094 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
7095 data->descr[0].length);
7096 data->descr[0].status = 0; /* Mapped or unknown. */
7098 ctl_set_success(ctsio);
7099 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7100 ctsio->be_move_done = ctl_config_move_done;
7102 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
7104 lbalen->len = total_len;
7106 retval = lun->backend->config_read((union ctl_io *)ctsio);
7111 ctl_read_defect(struct ctl_scsiio *ctsio)
7113 struct scsi_read_defect_data_10 *ccb10;
7114 struct scsi_read_defect_data_12 *ccb12;
7115 struct scsi_read_defect_data_hdr_10 *data10;
7116 struct scsi_read_defect_data_hdr_12 *data12;
7117 uint32_t alloc_len, data_len;
7120 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7122 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7123 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7124 format = ccb10->format;
7125 alloc_len = scsi_2btoul(ccb10->alloc_length);
7126 data_len = sizeof(*data10);
7128 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7129 format = ccb12->format;
7130 alloc_len = scsi_4btoul(ccb12->alloc_length);
7131 data_len = sizeof(*data12);
7133 if (alloc_len == 0) {
7134 ctl_set_success(ctsio);
7135 ctl_done((union ctl_io *)ctsio);
7136 return (CTL_RETVAL_COMPLETE);
7139 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7140 ctsio->kern_rel_offset = 0;
7141 ctsio->kern_sg_entries = 0;
7142 ctsio->kern_data_len = min(data_len, alloc_len);
7143 ctsio->kern_total_len = ctsio->kern_data_len;
7145 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7146 data10 = (struct scsi_read_defect_data_hdr_10 *)
7147 ctsio->kern_data_ptr;
7148 data10->format = format;
7149 scsi_ulto2b(0, data10->length);
7151 data12 = (struct scsi_read_defect_data_hdr_12 *)
7152 ctsio->kern_data_ptr;
7153 data12->format = format;
7154 scsi_ulto2b(0, data12->generation);
7155 scsi_ulto4b(0, data12->length);
7158 ctl_set_success(ctsio);
7159 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7160 ctsio->be_move_done = ctl_config_move_done;
7161 ctl_datamove((union ctl_io *)ctsio);
7162 return (CTL_RETVAL_COMPLETE);
7166 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7168 struct ctl_softc *softc = CTL_SOFTC(ctsio);
7169 struct ctl_lun *lun = CTL_LUN(ctsio);
7170 struct scsi_maintenance_in *cdb;
7172 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os;
7173 int num_ha_groups, num_target_ports, shared_group;
7174 struct ctl_port *port;
7175 struct scsi_target_group_data *rtg_ptr;
7176 struct scsi_target_group_data_extended *rtg_ext_ptr;
7177 struct scsi_target_port_group_descriptor *tpg_desc;
7179 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7181 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7182 retval = CTL_RETVAL_COMPLETE;
7184 switch (cdb->byte2 & STG_PDF_MASK) {
7185 case STG_PDF_LENGTH:
7188 case STG_PDF_EXTENDED:
7192 ctl_set_invalid_field(/*ctsio*/ ctsio,
7198 ctl_done((union ctl_io *)ctsio);
7202 num_target_ports = 0;
7203 shared_group = (softc->is_single != 0);
7204 mtx_lock(&softc->ctl_lock);
7205 STAILQ_FOREACH(port, &softc->port_list, links) {
7206 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7208 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7211 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7214 mtx_unlock(&softc->ctl_lock);
7215 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES;
7218 total_len = sizeof(struct scsi_target_group_data_extended);
7220 total_len = sizeof(struct scsi_target_group_data);
7221 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7222 (shared_group + num_ha_groups) +
7223 sizeof(struct scsi_target_port_descriptor) * num_target_ports;
7225 alloc_len = scsi_4btoul(cdb->length);
7227 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7228 ctsio->kern_sg_entries = 0;
7229 ctsio->kern_rel_offset = 0;
7230 ctsio->kern_data_len = min(total_len, alloc_len);
7231 ctsio->kern_total_len = ctsio->kern_data_len;
7234 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7235 ctsio->kern_data_ptr;
7236 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7237 rtg_ext_ptr->format_type = 0x10;
7238 rtg_ext_ptr->implicit_transition_time = 0;
7239 tpg_desc = &rtg_ext_ptr->groups[0];
7241 rtg_ptr = (struct scsi_target_group_data *)
7242 ctsio->kern_data_ptr;
7243 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7244 tpg_desc = &rtg_ptr->groups[0];
7247 mtx_lock(&softc->ctl_lock);
7248 pg = softc->port_min / softc->port_cnt;
7249 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) {
7250 /* Some shelf is known to be primary. */
7251 if (softc->ha_link == CTL_HA_LINK_OFFLINE)
7252 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7253 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN)
7254 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7255 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY)
7256 os = TPG_ASYMMETRIC_ACCESS_STANDBY;
7258 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7259 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7260 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7263 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7266 /* No known primary shelf. */
7267 if (softc->ha_link == CTL_HA_LINK_OFFLINE) {
7268 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7269 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7270 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) {
7271 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7272 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7274 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7278 tpg_desc->pref_state = ts;
7279 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7280 TPG_U_SUP | TPG_T_SUP;
7281 scsi_ulto2b(1, tpg_desc->target_port_group);
7282 tpg_desc->status = TPG_IMPLICIT;
7284 STAILQ_FOREACH(port, &softc->port_list, links) {
7285 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7287 if (!softc->is_single &&
7288 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0)
7290 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7292 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7293 relative_target_port_identifier);
7296 tpg_desc->target_port_count = pc;
7297 tpg_desc = (struct scsi_target_port_group_descriptor *)
7298 &tpg_desc->descriptors[pc];
7300 for (g = 0; g < num_ha_groups; g++) {
7301 tpg_desc->pref_state = (g == pg) ? ts : os;
7302 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7303 TPG_U_SUP | TPG_T_SUP;
7304 scsi_ulto2b(2 + g, tpg_desc->target_port_group);
7305 tpg_desc->status = TPG_IMPLICIT;
7307 STAILQ_FOREACH(port, &softc->port_list, links) {
7308 if (port->targ_port < g * softc->port_cnt ||
7309 port->targ_port >= (g + 1) * softc->port_cnt)
7311 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7313 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7315 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7317 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7318 relative_target_port_identifier);
7321 tpg_desc->target_port_count = pc;
7322 tpg_desc = (struct scsi_target_port_group_descriptor *)
7323 &tpg_desc->descriptors[pc];
7325 mtx_unlock(&softc->ctl_lock);
7327 ctl_set_success(ctsio);
7328 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7329 ctsio->be_move_done = ctl_config_move_done;
7330 ctl_datamove((union ctl_io *)ctsio);
7335 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7337 struct ctl_lun *lun = CTL_LUN(ctsio);
7338 struct scsi_report_supported_opcodes *cdb;
7339 const struct ctl_cmd_entry *entry, *sentry;
7340 struct scsi_report_supported_opcodes_all *all;
7341 struct scsi_report_supported_opcodes_descr *descr;
7342 struct scsi_report_supported_opcodes_one *one;
7344 int alloc_len, total_len;
7345 int opcode, service_action, i, j, num;
7347 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7349 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7350 retval = CTL_RETVAL_COMPLETE;
7352 opcode = cdb->requested_opcode;
7353 service_action = scsi_2btoul(cdb->requested_service_action);
7354 switch (cdb->options & RSO_OPTIONS_MASK) {
7355 case RSO_OPTIONS_ALL:
7357 for (i = 0; i < 256; i++) {
7358 entry = &ctl_cmd_table[i];
7359 if (entry->flags & CTL_CMD_FLAG_SA5) {
7360 for (j = 0; j < 32; j++) {
7361 sentry = &((const struct ctl_cmd_entry *)
7363 if (ctl_cmd_applicable(
7364 lun->be_lun->lun_type, sentry))
7368 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7373 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7374 num * sizeof(struct scsi_report_supported_opcodes_descr);
7376 case RSO_OPTIONS_OC:
7377 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7378 ctl_set_invalid_field(/*ctsio*/ ctsio,
7384 ctl_done((union ctl_io *)ctsio);
7385 return (CTL_RETVAL_COMPLETE);
7387 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7389 case RSO_OPTIONS_OC_SA:
7390 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7391 service_action >= 32) {
7392 ctl_set_invalid_field(/*ctsio*/ ctsio,
7398 ctl_done((union ctl_io *)ctsio);
7399 return (CTL_RETVAL_COMPLETE);
7402 case RSO_OPTIONS_OC_ASA:
7403 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7406 ctl_set_invalid_field(/*ctsio*/ ctsio,
7412 ctl_done((union ctl_io *)ctsio);
7413 return (CTL_RETVAL_COMPLETE);
7416 alloc_len = scsi_4btoul(cdb->length);
7418 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7419 ctsio->kern_sg_entries = 0;
7420 ctsio->kern_rel_offset = 0;
7421 ctsio->kern_data_len = min(total_len, alloc_len);
7422 ctsio->kern_total_len = ctsio->kern_data_len;
7424 switch (cdb->options & RSO_OPTIONS_MASK) {
7425 case RSO_OPTIONS_ALL:
7426 all = (struct scsi_report_supported_opcodes_all *)
7427 ctsio->kern_data_ptr;
7429 for (i = 0; i < 256; i++) {
7430 entry = &ctl_cmd_table[i];
7431 if (entry->flags & CTL_CMD_FLAG_SA5) {
7432 for (j = 0; j < 32; j++) {
7433 sentry = &((const struct ctl_cmd_entry *)
7435 if (!ctl_cmd_applicable(
7436 lun->be_lun->lun_type, sentry))
7438 descr = &all->descr[num++];
7440 scsi_ulto2b(j, descr->service_action);
7441 descr->flags = RSO_SERVACTV;
7442 scsi_ulto2b(sentry->length,
7446 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7449 descr = &all->descr[num++];
7451 scsi_ulto2b(0, descr->service_action);
7453 scsi_ulto2b(entry->length, descr->cdb_length);
7457 num * sizeof(struct scsi_report_supported_opcodes_descr),
7460 case RSO_OPTIONS_OC:
7461 one = (struct scsi_report_supported_opcodes_one *)
7462 ctsio->kern_data_ptr;
7463 entry = &ctl_cmd_table[opcode];
7465 case RSO_OPTIONS_OC_SA:
7466 one = (struct scsi_report_supported_opcodes_one *)
7467 ctsio->kern_data_ptr;
7468 entry = &ctl_cmd_table[opcode];
7469 entry = &((const struct ctl_cmd_entry *)
7470 entry->execute)[service_action];
7472 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7474 scsi_ulto2b(entry->length, one->cdb_length);
7475 one->cdb_usage[0] = opcode;
7476 memcpy(&one->cdb_usage[1], entry->usage,
7481 case RSO_OPTIONS_OC_ASA:
7482 one = (struct scsi_report_supported_opcodes_one *)
7483 ctsio->kern_data_ptr;
7484 entry = &ctl_cmd_table[opcode];
7485 if (entry->flags & CTL_CMD_FLAG_SA5) {
7486 entry = &((const struct ctl_cmd_entry *)
7487 entry->execute)[service_action];
7488 } else if (service_action != 0) {
7495 ctl_set_success(ctsio);
7496 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7497 ctsio->be_move_done = ctl_config_move_done;
7498 ctl_datamove((union ctl_io *)ctsio);
7503 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7505 struct scsi_report_supported_tmf *cdb;
7506 struct scsi_report_supported_tmf_ext_data *data;
7508 int alloc_len, total_len;
7510 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7512 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7514 retval = CTL_RETVAL_COMPLETE;
7516 if (cdb->options & RST_REPD)
7517 total_len = sizeof(struct scsi_report_supported_tmf_ext_data);
7519 total_len = sizeof(struct scsi_report_supported_tmf_data);
7520 alloc_len = scsi_4btoul(cdb->length);
7522 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7523 ctsio->kern_sg_entries = 0;
7524 ctsio->kern_rel_offset = 0;
7525 ctsio->kern_data_len = min(total_len, alloc_len);
7526 ctsio->kern_total_len = ctsio->kern_data_len;
7528 data = (struct scsi_report_supported_tmf_ext_data *)ctsio->kern_data_ptr;
7529 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS |
7531 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS;
7532 data->length = total_len - 4;
7534 ctl_set_success(ctsio);
7535 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7536 ctsio->be_move_done = ctl_config_move_done;
7537 ctl_datamove((union ctl_io *)ctsio);
7542 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7544 struct scsi_report_timestamp *cdb;
7545 struct scsi_report_timestamp_data *data;
7549 int alloc_len, total_len;
7551 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7553 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7555 retval = CTL_RETVAL_COMPLETE;
7557 total_len = sizeof(struct scsi_report_timestamp_data);
7558 alloc_len = scsi_4btoul(cdb->length);
7560 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7561 ctsio->kern_sg_entries = 0;
7562 ctsio->kern_rel_offset = 0;
7563 ctsio->kern_data_len = min(total_len, alloc_len);
7564 ctsio->kern_total_len = ctsio->kern_data_len;
7566 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7567 scsi_ulto2b(sizeof(*data) - 2, data->length);
7568 data->origin = RTS_ORIG_OUTSIDE;
7570 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7571 scsi_ulto4b(timestamp >> 16, data->timestamp);
7572 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7574 ctl_set_success(ctsio);
7575 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7576 ctsio->be_move_done = ctl_config_move_done;
7577 ctl_datamove((union ctl_io *)ctsio);
7582 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7584 struct ctl_softc *softc = CTL_SOFTC(ctsio);
7585 struct ctl_lun *lun = CTL_LUN(ctsio);
7586 struct scsi_per_res_in *cdb;
7587 int alloc_len, total_len = 0;
7588 /* struct scsi_per_res_in_rsrv in_data; */
7591 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7593 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7595 alloc_len = scsi_2btoul(cdb->length);
7598 mtx_lock(&lun->lun_lock);
7599 switch (cdb->action) {
7600 case SPRI_RK: /* read keys */
7601 total_len = sizeof(struct scsi_per_res_in_keys) +
7603 sizeof(struct scsi_per_res_key);
7605 case SPRI_RR: /* read reservation */
7606 if (lun->flags & CTL_LUN_PR_RESERVED)
7607 total_len = sizeof(struct scsi_per_res_in_rsrv);
7609 total_len = sizeof(struct scsi_per_res_in_header);
7611 case SPRI_RC: /* report capabilities */
7612 total_len = sizeof(struct scsi_per_res_cap);
7614 case SPRI_RS: /* read full status */
7615 total_len = sizeof(struct scsi_per_res_in_header) +
7616 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7620 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7622 mtx_unlock(&lun->lun_lock);
7624 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7625 ctsio->kern_rel_offset = 0;
7626 ctsio->kern_sg_entries = 0;
7627 ctsio->kern_data_len = min(total_len, alloc_len);
7628 ctsio->kern_total_len = ctsio->kern_data_len;
7630 mtx_lock(&lun->lun_lock);
7631 switch (cdb->action) {
7632 case SPRI_RK: { // read keys
7633 struct scsi_per_res_in_keys *res_keys;
7636 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7639 * We had to drop the lock to allocate our buffer, which
7640 * leaves time for someone to come in with another
7641 * persistent reservation. (That is unlikely, though,
7642 * since this should be the only persistent reservation
7643 * command active right now.)
7645 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7646 (lun->pr_key_count *
7647 sizeof(struct scsi_per_res_key)))){
7648 mtx_unlock(&lun->lun_lock);
7649 free(ctsio->kern_data_ptr, M_CTL);
7650 printf("%s: reservation length changed, retrying\n",
7655 scsi_ulto4b(lun->pr_generation, res_keys->header.generation);
7657 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7658 lun->pr_key_count, res_keys->header.length);
7660 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) {
7661 if ((key = ctl_get_prkey(lun, i)) == 0)
7665 * We used lun->pr_key_count to calculate the
7666 * size to allocate. If it turns out the number of
7667 * initiators with the registered flag set is
7668 * larger than that (i.e. they haven't been kept in
7669 * sync), we've got a problem.
7671 if (key_count >= lun->pr_key_count) {
7675 scsi_u64to8b(key, res_keys->keys[key_count].key);
7680 case SPRI_RR: { // read reservation
7681 struct scsi_per_res_in_rsrv *res;
7682 int tmp_len, header_only;
7684 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7686 scsi_ulto4b(lun->pr_generation, res->header.generation);
7688 if (lun->flags & CTL_LUN_PR_RESERVED)
7690 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7691 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7692 res->header.length);
7695 tmp_len = sizeof(struct scsi_per_res_in_header);
7696 scsi_ulto4b(0, res->header.length);
7701 * We had to drop the lock to allocate our buffer, which
7702 * leaves time for someone to come in with another
7703 * persistent reservation. (That is unlikely, though,
7704 * since this should be the only persistent reservation
7705 * command active right now.)
7707 if (tmp_len != total_len) {
7708 mtx_unlock(&lun->lun_lock);
7709 free(ctsio->kern_data_ptr, M_CTL);
7710 printf("%s: reservation status changed, retrying\n",
7716 * No reservation held, so we're done.
7718 if (header_only != 0)
7722 * If the registration is an All Registrants type, the key
7723 * is 0, since it doesn't really matter.
7725 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7726 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7727 res->data.reservation);
7729 res->data.scopetype = lun->pr_res_type;
7732 case SPRI_RC: //report capabilities
7734 struct scsi_per_res_cap *res_cap;
7737 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7738 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7739 res_cap->flags1 = SPRI_CRH;
7740 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5;
7741 type_mask = SPRI_TM_WR_EX_AR |
7747 scsi_ulto2b(type_mask, res_cap->type_mask);
7750 case SPRI_RS: { // read full status
7751 struct scsi_per_res_in_full *res_status;
7752 struct scsi_per_res_in_full_desc *res_desc;
7753 struct ctl_port *port;
7756 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7759 * We had to drop the lock to allocate our buffer, which
7760 * leaves time for someone to come in with another
7761 * persistent reservation. (That is unlikely, though,
7762 * since this should be the only persistent reservation
7763 * command active right now.)
7765 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7766 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7767 lun->pr_key_count)){
7768 mtx_unlock(&lun->lun_lock);
7769 free(ctsio->kern_data_ptr, M_CTL);
7770 printf("%s: reservation length changed, retrying\n",
7775 scsi_ulto4b(lun->pr_generation, res_status->header.generation);
7777 res_desc = &res_status->desc[0];
7778 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7779 if ((key = ctl_get_prkey(lun, i)) == 0)
7782 scsi_u64to8b(key, res_desc->res_key.key);
7783 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7784 (lun->pr_res_idx == i ||
7785 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7786 res_desc->flags = SPRI_FULL_R_HOLDER;
7787 res_desc->scopetype = lun->pr_res_type;
7789 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7790 res_desc->rel_trgt_port_id);
7792 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7794 len = ctl_create_iid(port,
7795 i % CTL_MAX_INIT_PER_PORT,
7796 res_desc->transport_id);
7797 scsi_ulto4b(len, res_desc->additional_length);
7798 res_desc = (struct scsi_per_res_in_full_desc *)
7799 &res_desc->transport_id[len];
7801 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7802 res_status->header.length);
7806 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7808 mtx_unlock(&lun->lun_lock);
7810 ctl_set_success(ctsio);
7811 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7812 ctsio->be_move_done = ctl_config_move_done;
7813 ctl_datamove((union ctl_io *)ctsio);
7814 return (CTL_RETVAL_COMPLETE);
7818 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7822 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7823 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7824 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7825 struct scsi_per_res_out_parms* param)
7827 union ctl_ha_msg persis_io;
7830 mtx_lock(&lun->lun_lock);
7831 if (sa_res_key == 0) {
7832 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7833 /* validate scope and type */
7834 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7836 mtx_unlock(&lun->lun_lock);
7837 ctl_set_invalid_field(/*ctsio*/ ctsio,
7843 ctl_done((union ctl_io *)ctsio);
7847 if (type>8 || type==2 || type==4 || type==0) {
7848 mtx_unlock(&lun->lun_lock);
7849 ctl_set_invalid_field(/*ctsio*/ ctsio,
7855 ctl_done((union ctl_io *)ctsio);
7860 * Unregister everybody else and build UA for
7863 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7864 if (i == residx || ctl_get_prkey(lun, i) == 0)
7867 ctl_clr_prkey(lun, i);
7868 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7870 lun->pr_key_count = 1;
7871 lun->pr_res_type = type;
7872 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7873 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7874 lun->pr_res_idx = residx;
7875 lun->pr_generation++;
7876 mtx_unlock(&lun->lun_lock);
7878 /* send msg to other side */
7879 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7880 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7881 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7882 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7883 persis_io.pr.pr_info.res_type = type;
7884 memcpy(persis_io.pr.pr_info.sa_res_key,
7885 param->serv_act_res_key,
7886 sizeof(param->serv_act_res_key));
7887 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7888 sizeof(persis_io.pr), M_WAITOK);
7890 /* not all registrants */
7891 mtx_unlock(&lun->lun_lock);
7892 free(ctsio->kern_data_ptr, M_CTL);
7893 ctl_set_invalid_field(ctsio,
7899 ctl_done((union ctl_io *)ctsio);
7902 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7903 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7906 if (res_key == sa_res_key) {
7909 * The spec implies this is not good but doesn't
7910 * say what to do. There are two choices either
7911 * generate a res conflict or check condition
7912 * with illegal field in parameter data. Since
7913 * that is what is done when the sa_res_key is
7914 * zero I'll take that approach since this has
7915 * to do with the sa_res_key.
7917 mtx_unlock(&lun->lun_lock);
7918 free(ctsio->kern_data_ptr, M_CTL);
7919 ctl_set_invalid_field(ctsio,
7925 ctl_done((union ctl_io *)ctsio);
7929 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7930 if (ctl_get_prkey(lun, i) != sa_res_key)
7934 ctl_clr_prkey(lun, i);
7935 lun->pr_key_count--;
7936 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7939 mtx_unlock(&lun->lun_lock);
7940 free(ctsio->kern_data_ptr, M_CTL);
7941 ctl_set_reservation_conflict(ctsio);
7942 ctl_done((union ctl_io *)ctsio);
7943 return (CTL_RETVAL_COMPLETE);
7945 lun->pr_generation++;
7946 mtx_unlock(&lun->lun_lock);
7948 /* send msg to other side */
7949 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7950 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7951 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7952 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7953 persis_io.pr.pr_info.res_type = type;
7954 memcpy(persis_io.pr.pr_info.sa_res_key,
7955 param->serv_act_res_key,
7956 sizeof(param->serv_act_res_key));
7957 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7958 sizeof(persis_io.pr), M_WAITOK);
7960 /* Reserved but not all registrants */
7961 /* sa_res_key is res holder */
7962 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
7963 /* validate scope and type */
7964 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7966 mtx_unlock(&lun->lun_lock);
7967 ctl_set_invalid_field(/*ctsio*/ ctsio,
7973 ctl_done((union ctl_io *)ctsio);
7977 if (type>8 || type==2 || type==4 || type==0) {
7978 mtx_unlock(&lun->lun_lock);
7979 ctl_set_invalid_field(/*ctsio*/ ctsio,
7985 ctl_done((union ctl_io *)ctsio);
7991 * if sa_res_key != res_key remove all
7992 * registrants w/sa_res_key and generate UA
7993 * for these registrants(Registrations
7994 * Preempted) if it wasn't an exclusive
7995 * reservation generate UA(Reservations
7996 * Preempted) for all other registered nexuses
7997 * if the type has changed. Establish the new
7998 * reservation and holder. If res_key and
7999 * sa_res_key are the same do the above
8000 * except don't unregister the res holder.
8003 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8004 if (i == residx || ctl_get_prkey(lun, i) == 0)
8007 if (sa_res_key == ctl_get_prkey(lun, i)) {
8008 ctl_clr_prkey(lun, i);
8009 lun->pr_key_count--;
8010 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8011 } else if (type != lun->pr_res_type &&
8012 (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8013 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8014 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8017 lun->pr_res_type = type;
8018 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8019 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8020 lun->pr_res_idx = residx;
8022 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8023 lun->pr_generation++;
8024 mtx_unlock(&lun->lun_lock);
8026 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8027 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8028 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8029 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8030 persis_io.pr.pr_info.res_type = type;
8031 memcpy(persis_io.pr.pr_info.sa_res_key,
8032 param->serv_act_res_key,
8033 sizeof(param->serv_act_res_key));
8034 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8035 sizeof(persis_io.pr), M_WAITOK);
8038 * sa_res_key is not the res holder just
8039 * remove registrants
8043 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8044 if (sa_res_key != ctl_get_prkey(lun, i))
8048 ctl_clr_prkey(lun, i);
8049 lun->pr_key_count--;
8050 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8054 mtx_unlock(&lun->lun_lock);
8055 free(ctsio->kern_data_ptr, M_CTL);
8056 ctl_set_reservation_conflict(ctsio);
8057 ctl_done((union ctl_io *)ctsio);
8060 lun->pr_generation++;
8061 mtx_unlock(&lun->lun_lock);
8063 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8064 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8065 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8066 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8067 persis_io.pr.pr_info.res_type = type;
8068 memcpy(persis_io.pr.pr_info.sa_res_key,
8069 param->serv_act_res_key,
8070 sizeof(param->serv_act_res_key));
8071 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8072 sizeof(persis_io.pr), M_WAITOK);
8079 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8081 uint64_t sa_res_key;
8084 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8086 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8087 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8088 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
8089 if (sa_res_key == 0) {
8091 * Unregister everybody else and build UA for
8094 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8095 if (i == msg->pr.pr_info.residx ||
8096 ctl_get_prkey(lun, i) == 0)
8099 ctl_clr_prkey(lun, i);
8100 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8103 lun->pr_key_count = 1;
8104 lun->pr_res_type = msg->pr.pr_info.res_type;
8105 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8106 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8107 lun->pr_res_idx = msg->pr.pr_info.residx;
8109 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8110 if (sa_res_key == ctl_get_prkey(lun, i))
8113 ctl_clr_prkey(lun, i);
8114 lun->pr_key_count--;
8115 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8119 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8120 if (i == msg->pr.pr_info.residx ||
8121 ctl_get_prkey(lun, i) == 0)
8124 if (sa_res_key == ctl_get_prkey(lun, i)) {
8125 ctl_clr_prkey(lun, i);
8126 lun->pr_key_count--;
8127 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8128 } else if (msg->pr.pr_info.res_type != lun->pr_res_type
8129 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8130 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8131 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8134 lun->pr_res_type = msg->pr.pr_info.res_type;
8135 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8136 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8137 lun->pr_res_idx = msg->pr.pr_info.residx;
8139 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8141 lun->pr_generation++;
8147 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8149 struct ctl_softc *softc = CTL_SOFTC(ctsio);
8150 struct ctl_lun *lun = CTL_LUN(ctsio);
8152 u_int32_t param_len;
8153 struct scsi_per_res_out *cdb;
8154 struct scsi_per_res_out_parms* param;
8156 uint64_t res_key, sa_res_key, key;
8158 union ctl_ha_msg persis_io;
8161 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8163 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8164 retval = CTL_RETVAL_COMPLETE;
8167 * We only support whole-LUN scope. The scope & type are ignored for
8168 * register, register and ignore existing key and clear.
8169 * We sometimes ignore scope and type on preempts too!!
8170 * Verify reservation type here as well.
8172 type = cdb->scope_type & SPR_TYPE_MASK;
8173 if ((cdb->action == SPRO_RESERVE)
8174 || (cdb->action == SPRO_RELEASE)) {
8175 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8176 ctl_set_invalid_field(/*ctsio*/ ctsio,
8182 ctl_done((union ctl_io *)ctsio);
8183 return (CTL_RETVAL_COMPLETE);
8186 if (type>8 || type==2 || type==4 || type==0) {
8187 ctl_set_invalid_field(/*ctsio*/ ctsio,
8193 ctl_done((union ctl_io *)ctsio);
8194 return (CTL_RETVAL_COMPLETE);
8198 param_len = scsi_4btoul(cdb->length);
8200 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8201 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8202 ctsio->kern_data_len = param_len;
8203 ctsio->kern_total_len = param_len;
8204 ctsio->kern_rel_offset = 0;
8205 ctsio->kern_sg_entries = 0;
8206 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8207 ctsio->be_move_done = ctl_config_move_done;
8208 ctl_datamove((union ctl_io *)ctsio);
8210 return (CTL_RETVAL_COMPLETE);
8213 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8215 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8216 res_key = scsi_8btou64(param->res_key.key);
8217 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8220 * Validate the reservation key here except for SPRO_REG_IGNO
8221 * This must be done for all other service actions
8223 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8224 mtx_lock(&lun->lun_lock);
8225 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8226 if (res_key != key) {
8228 * The current key passed in doesn't match
8229 * the one the initiator previously
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 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8240 * We are not registered
8242 mtx_unlock(&lun->lun_lock);
8243 free(ctsio->kern_data_ptr, M_CTL);
8244 ctl_set_reservation_conflict(ctsio);
8245 ctl_done((union ctl_io *)ctsio);
8246 return (CTL_RETVAL_COMPLETE);
8247 } else if (res_key != 0) {
8249 * We are not registered and trying to register but
8250 * the register key isn't zero.
8252 mtx_unlock(&lun->lun_lock);
8253 free(ctsio->kern_data_ptr, M_CTL);
8254 ctl_set_reservation_conflict(ctsio);
8255 ctl_done((union ctl_io *)ctsio);
8256 return (CTL_RETVAL_COMPLETE);
8258 mtx_unlock(&lun->lun_lock);
8261 switch (cdb->action & SPRO_ACTION_MASK) {
8263 case SPRO_REG_IGNO: {
8266 printf("Registration received\n");
8270 * We don't support any of these options, as we report in
8271 * the read capabilities request (see
8272 * ctl_persistent_reserve_in(), above).
8274 if ((param->flags & SPR_SPEC_I_PT)
8275 || (param->flags & SPR_ALL_TG_PT)
8276 || (param->flags & SPR_APTPL)) {
8279 if (param->flags & SPR_APTPL)
8281 else if (param->flags & SPR_ALL_TG_PT)
8283 else /* SPR_SPEC_I_PT */
8286 free(ctsio->kern_data_ptr, M_CTL);
8287 ctl_set_invalid_field(ctsio,
8293 ctl_done((union ctl_io *)ctsio);
8294 return (CTL_RETVAL_COMPLETE);
8297 mtx_lock(&lun->lun_lock);
8300 * The initiator wants to clear the
8303 if (sa_res_key == 0) {
8305 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8306 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8307 && ctl_get_prkey(lun, residx) == 0)) {
8308 mtx_unlock(&lun->lun_lock);
8312 ctl_clr_prkey(lun, residx);
8313 lun->pr_key_count--;
8315 if (residx == lun->pr_res_idx) {
8316 lun->flags &= ~CTL_LUN_PR_RESERVED;
8317 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8319 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8320 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8321 lun->pr_key_count) {
8323 * If the reservation is a registrants
8324 * only type we need to generate a UA
8325 * for other registered inits. The
8326 * sense code should be RESERVATIONS
8330 for (i = softc->init_min; i < softc->init_max; i++){
8331 if (ctl_get_prkey(lun, i) == 0)
8334 CTL_UA_RES_RELEASE);
8337 lun->pr_res_type = 0;
8338 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8339 if (lun->pr_key_count==0) {
8340 lun->flags &= ~CTL_LUN_PR_RESERVED;
8341 lun->pr_res_type = 0;
8342 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8345 lun->pr_generation++;
8346 mtx_unlock(&lun->lun_lock);
8348 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8349 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8350 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8351 persis_io.pr.pr_info.residx = residx;
8352 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8353 sizeof(persis_io.pr), M_WAITOK);
8354 } else /* sa_res_key != 0 */ {
8357 * If we aren't registered currently then increment
8358 * the key count and set the registered flag.
8360 ctl_alloc_prkey(lun, residx);
8361 if (ctl_get_prkey(lun, residx) == 0)
8362 lun->pr_key_count++;
8363 ctl_set_prkey(lun, residx, sa_res_key);
8364 lun->pr_generation++;
8365 mtx_unlock(&lun->lun_lock);
8367 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8368 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8369 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8370 persis_io.pr.pr_info.residx = residx;
8371 memcpy(persis_io.pr.pr_info.sa_res_key,
8372 param->serv_act_res_key,
8373 sizeof(param->serv_act_res_key));
8374 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8375 sizeof(persis_io.pr), M_WAITOK);
8382 printf("Reserve executed type %d\n", type);
8384 mtx_lock(&lun->lun_lock);
8385 if (lun->flags & CTL_LUN_PR_RESERVED) {
8387 * if this isn't the reservation holder and it's
8388 * not a "all registrants" type or if the type is
8389 * different then we have a conflict
8391 if ((lun->pr_res_idx != residx
8392 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8393 || lun->pr_res_type != type) {
8394 mtx_unlock(&lun->lun_lock);
8395 free(ctsio->kern_data_ptr, M_CTL);
8396 ctl_set_reservation_conflict(ctsio);
8397 ctl_done((union ctl_io *)ctsio);
8398 return (CTL_RETVAL_COMPLETE);
8400 mtx_unlock(&lun->lun_lock);
8401 } else /* create a reservation */ {
8403 * If it's not an "all registrants" type record
8404 * reservation holder
8406 if (type != SPR_TYPE_WR_EX_AR
8407 && type != SPR_TYPE_EX_AC_AR)
8408 lun->pr_res_idx = residx; /* Res holder */
8410 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8412 lun->flags |= CTL_LUN_PR_RESERVED;
8413 lun->pr_res_type = type;
8415 mtx_unlock(&lun->lun_lock);
8417 /* send msg to other side */
8418 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8419 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8420 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8421 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8422 persis_io.pr.pr_info.res_type = type;
8423 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8424 sizeof(persis_io.pr), M_WAITOK);
8429 mtx_lock(&lun->lun_lock);
8430 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8431 /* No reservation exists return good status */
8432 mtx_unlock(&lun->lun_lock);
8436 * Is this nexus a reservation holder?
8438 if (lun->pr_res_idx != residx
8439 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8441 * not a res holder return good status but
8444 mtx_unlock(&lun->lun_lock);
8448 if (lun->pr_res_type != type) {
8449 mtx_unlock(&lun->lun_lock);
8450 free(ctsio->kern_data_ptr, M_CTL);
8451 ctl_set_illegal_pr_release(ctsio);
8452 ctl_done((union ctl_io *)ctsio);
8453 return (CTL_RETVAL_COMPLETE);
8456 /* okay to release */
8457 lun->flags &= ~CTL_LUN_PR_RESERVED;
8458 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8459 lun->pr_res_type = 0;
8462 * If this isn't an exclusive access reservation and NUAR
8463 * is not set, generate UA for all other registrants.
8465 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX &&
8466 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8467 for (i = softc->init_min; i < softc->init_max; i++) {
8468 if (i == residx || ctl_get_prkey(lun, i) == 0)
8470 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8473 mtx_unlock(&lun->lun_lock);
8475 /* Send msg to other side */
8476 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8477 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8478 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8479 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8480 sizeof(persis_io.pr), M_WAITOK);
8484 /* send msg to other side */
8486 mtx_lock(&lun->lun_lock);
8487 lun->flags &= ~CTL_LUN_PR_RESERVED;
8488 lun->pr_res_type = 0;
8489 lun->pr_key_count = 0;
8490 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8492 ctl_clr_prkey(lun, residx);
8493 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8494 if (ctl_get_prkey(lun, i) != 0) {
8495 ctl_clr_prkey(lun, i);
8496 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8498 lun->pr_generation++;
8499 mtx_unlock(&lun->lun_lock);
8501 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8502 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8503 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8504 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8505 sizeof(persis_io.pr), M_WAITOK);
8509 case SPRO_PRE_ABO: {
8512 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8513 residx, ctsio, cdb, param);
8515 return (CTL_RETVAL_COMPLETE);
8519 panic("%s: Invalid PR type %#x", __func__, cdb->action);
8523 free(ctsio->kern_data_ptr, M_CTL);
8524 ctl_set_success(ctsio);
8525 ctl_done((union ctl_io *)ctsio);
8531 * This routine is for handling a message from the other SC pertaining to
8532 * persistent reserve out. All the error checking will have been done
8533 * so only perorming the action need be done here to keep the two
8537 ctl_hndl_per_res_out_on_other_sc(union ctl_io *io)
8539 struct ctl_softc *softc = CTL_SOFTC(io);
8540 union ctl_ha_msg *msg = (union ctl_ha_msg *)&io->presio.pr_msg;
8541 struct ctl_lun *lun;
8543 uint32_t residx, targ_lun;
8545 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8546 mtx_lock(&softc->ctl_lock);
8547 if (targ_lun >= ctl_max_luns ||
8548 (lun = softc->ctl_luns[targ_lun]) == NULL) {
8549 mtx_unlock(&softc->ctl_lock);
8552 mtx_lock(&lun->lun_lock);
8553 mtx_unlock(&softc->ctl_lock);
8554 if (lun->flags & CTL_LUN_DISABLED) {
8555 mtx_unlock(&lun->lun_lock);
8558 residx = ctl_get_initindex(&msg->hdr.nexus);
8559 switch(msg->pr.pr_info.action) {
8560 case CTL_PR_REG_KEY:
8561 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8562 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8563 lun->pr_key_count++;
8564 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8565 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8566 lun->pr_generation++;
8569 case CTL_PR_UNREG_KEY:
8570 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8571 lun->pr_key_count--;
8573 /* XXX Need to see if the reservation has been released */
8574 /* if so do we need to generate UA? */
8575 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8576 lun->flags &= ~CTL_LUN_PR_RESERVED;
8577 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8579 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8580 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8581 lun->pr_key_count) {
8583 * If the reservation is a registrants
8584 * only type we need to generate a UA
8585 * for other registered inits. The
8586 * sense code should be RESERVATIONS
8590 for (i = softc->init_min; i < softc->init_max; i++) {
8591 if (ctl_get_prkey(lun, i) == 0)
8594 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8597 lun->pr_res_type = 0;
8598 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8599 if (lun->pr_key_count==0) {
8600 lun->flags &= ~CTL_LUN_PR_RESERVED;
8601 lun->pr_res_type = 0;
8602 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8605 lun->pr_generation++;
8608 case CTL_PR_RESERVE:
8609 lun->flags |= CTL_LUN_PR_RESERVED;
8610 lun->pr_res_type = msg->pr.pr_info.res_type;
8611 lun->pr_res_idx = msg->pr.pr_info.residx;
8615 case CTL_PR_RELEASE:
8617 * If this isn't an exclusive access reservation and NUAR
8618 * is not set, generate UA for all other registrants.
8620 if (lun->pr_res_type != SPR_TYPE_EX_AC &&
8621 lun->pr_res_type != SPR_TYPE_WR_EX &&
8622 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8623 for (i = softc->init_min; i < softc->init_max; i++)
8624 if (i == residx || ctl_get_prkey(lun, i) == 0)
8626 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8629 lun->flags &= ~CTL_LUN_PR_RESERVED;
8630 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8631 lun->pr_res_type = 0;
8634 case CTL_PR_PREEMPT:
8635 ctl_pro_preempt_other(lun, msg);
8638 lun->flags &= ~CTL_LUN_PR_RESERVED;
8639 lun->pr_res_type = 0;
8640 lun->pr_key_count = 0;
8641 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8643 for (i=0; i < CTL_MAX_INITIATORS; i++) {
8644 if (ctl_get_prkey(lun, i) == 0)
8646 ctl_clr_prkey(lun, i);
8647 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8649 lun->pr_generation++;
8653 mtx_unlock(&lun->lun_lock);
8657 ctl_read_write(struct ctl_scsiio *ctsio)
8659 struct ctl_lun *lun = CTL_LUN(ctsio);
8660 struct ctl_lba_len_flags *lbalen;
8662 uint32_t num_blocks;
8666 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8669 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8670 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8671 switch (ctsio->cdb[0]) {
8674 struct scsi_rw_6 *cdb;
8676 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8678 lba = scsi_3btoul(cdb->addr);
8679 /* only 5 bits are valid in the most significant address byte */
8681 num_blocks = cdb->length;
8683 * This is correct according to SBC-2.
8685 if (num_blocks == 0)
8691 struct scsi_rw_10 *cdb;
8693 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8694 if (cdb->byte2 & SRW10_FUA)
8695 flags |= CTL_LLF_FUA;
8696 if (cdb->byte2 & SRW10_DPO)
8697 flags |= CTL_LLF_DPO;
8698 lba = scsi_4btoul(cdb->addr);
8699 num_blocks = scsi_2btoul(cdb->length);
8702 case WRITE_VERIFY_10: {
8703 struct scsi_write_verify_10 *cdb;
8705 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8706 flags |= CTL_LLF_FUA;
8707 if (cdb->byte2 & SWV_DPO)
8708 flags |= CTL_LLF_DPO;
8709 lba = scsi_4btoul(cdb->addr);
8710 num_blocks = scsi_2btoul(cdb->length);
8715 struct scsi_rw_12 *cdb;
8717 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8718 if (cdb->byte2 & SRW12_FUA)
8719 flags |= CTL_LLF_FUA;
8720 if (cdb->byte2 & SRW12_DPO)
8721 flags |= CTL_LLF_DPO;
8722 lba = scsi_4btoul(cdb->addr);
8723 num_blocks = scsi_4btoul(cdb->length);
8726 case WRITE_VERIFY_12: {
8727 struct scsi_write_verify_12 *cdb;
8729 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8730 flags |= CTL_LLF_FUA;
8731 if (cdb->byte2 & SWV_DPO)
8732 flags |= CTL_LLF_DPO;
8733 lba = scsi_4btoul(cdb->addr);
8734 num_blocks = scsi_4btoul(cdb->length);
8739 struct scsi_rw_16 *cdb;
8741 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8742 if (cdb->byte2 & SRW12_FUA)
8743 flags |= CTL_LLF_FUA;
8744 if (cdb->byte2 & SRW12_DPO)
8745 flags |= CTL_LLF_DPO;
8746 lba = scsi_8btou64(cdb->addr);
8747 num_blocks = scsi_4btoul(cdb->length);
8750 case WRITE_ATOMIC_16: {
8751 struct scsi_write_atomic_16 *cdb;
8753 if (lun->be_lun->atomicblock == 0) {
8754 ctl_set_invalid_opcode(ctsio);
8755 ctl_done((union ctl_io *)ctsio);
8756 return (CTL_RETVAL_COMPLETE);
8759 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb;
8760 if (cdb->byte2 & SRW12_FUA)
8761 flags |= CTL_LLF_FUA;
8762 if (cdb->byte2 & SRW12_DPO)
8763 flags |= CTL_LLF_DPO;
8764 lba = scsi_8btou64(cdb->addr);
8765 num_blocks = scsi_2btoul(cdb->length);
8766 if (num_blocks > lun->be_lun->atomicblock) {
8767 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8768 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8770 ctl_done((union ctl_io *)ctsio);
8771 return (CTL_RETVAL_COMPLETE);
8775 case WRITE_VERIFY_16: {
8776 struct scsi_write_verify_16 *cdb;
8778 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8779 flags |= CTL_LLF_FUA;
8780 if (cdb->byte2 & SWV_DPO)
8781 flags |= CTL_LLF_DPO;
8782 lba = scsi_8btou64(cdb->addr);
8783 num_blocks = scsi_4btoul(cdb->length);
8788 * We got a command we don't support. This shouldn't
8789 * happen, commands should be filtered out above us.
8791 ctl_set_invalid_opcode(ctsio);
8792 ctl_done((union ctl_io *)ctsio);
8794 return (CTL_RETVAL_COMPLETE);
8795 break; /* NOTREACHED */
8799 * The first check is to make sure we're in bounds, the second
8800 * check is to catch wrap-around problems. If the lba + num blocks
8801 * is less than the lba, then we've wrapped around and the block
8802 * range is invalid anyway.
8804 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8805 || ((lba + num_blocks) < lba)) {
8806 ctl_set_lba_out_of_range(ctsio,
8807 MAX(lba, lun->be_lun->maxlba + 1));
8808 ctl_done((union ctl_io *)ctsio);
8809 return (CTL_RETVAL_COMPLETE);
8813 * According to SBC-3, a transfer length of 0 is not an error.
8814 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8815 * translates to 256 blocks for those commands.
8817 if (num_blocks == 0) {
8818 ctl_set_success(ctsio);
8819 ctl_done((union ctl_io *)ctsio);
8820 return (CTL_RETVAL_COMPLETE);
8823 /* Set FUA and/or DPO if caches are disabled. */
8825 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0)
8826 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8828 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8829 flags |= CTL_LLF_FUA;
8832 lbalen = (struct ctl_lba_len_flags *)
8833 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8835 lbalen->len = num_blocks;
8836 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8838 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8839 ctsio->kern_rel_offset = 0;
8841 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8843 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8848 ctl_cnw_cont(union ctl_io *io)
8850 struct ctl_lun *lun = CTL_LUN(io);
8851 struct ctl_scsiio *ctsio;
8852 struct ctl_lba_len_flags *lbalen;
8855 ctsio = &io->scsiio;
8856 ctsio->io_hdr.status = CTL_STATUS_NONE;
8857 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8858 lbalen = (struct ctl_lba_len_flags *)
8859 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8860 lbalen->flags &= ~CTL_LLF_COMPARE;
8861 lbalen->flags |= CTL_LLF_WRITE;
8863 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8864 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8869 ctl_cnw(struct ctl_scsiio *ctsio)
8871 struct ctl_lun *lun = CTL_LUN(ctsio);
8872 struct ctl_lba_len_flags *lbalen;
8874 uint32_t num_blocks;
8877 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8880 switch (ctsio->cdb[0]) {
8881 case COMPARE_AND_WRITE: {
8882 struct scsi_compare_and_write *cdb;
8884 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8885 if (cdb->byte2 & SRW10_FUA)
8886 flags |= CTL_LLF_FUA;
8887 if (cdb->byte2 & SRW10_DPO)
8888 flags |= CTL_LLF_DPO;
8889 lba = scsi_8btou64(cdb->addr);
8890 num_blocks = cdb->length;
8895 * We got a command we don't support. This shouldn't
8896 * happen, commands should be filtered out above us.
8898 ctl_set_invalid_opcode(ctsio);
8899 ctl_done((union ctl_io *)ctsio);
8901 return (CTL_RETVAL_COMPLETE);
8902 break; /* NOTREACHED */
8906 * The first check is to make sure we're in bounds, the second
8907 * check is to catch wrap-around problems. If the lba + num blocks
8908 * is less than the lba, then we've wrapped around and the block
8909 * range is invalid anyway.
8911 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8912 || ((lba + num_blocks) < lba)) {
8913 ctl_set_lba_out_of_range(ctsio,
8914 MAX(lba, lun->be_lun->maxlba + 1));
8915 ctl_done((union ctl_io *)ctsio);
8916 return (CTL_RETVAL_COMPLETE);
8920 * According to SBC-3, a transfer length of 0 is not an error.
8922 if (num_blocks == 0) {
8923 ctl_set_success(ctsio);
8924 ctl_done((union ctl_io *)ctsio);
8925 return (CTL_RETVAL_COMPLETE);
8928 /* Set FUA if write cache is disabled. */
8929 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8930 flags |= CTL_LLF_FUA;
8932 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
8933 ctsio->kern_rel_offset = 0;
8936 * Set the IO_CONT flag, so that if this I/O gets passed to
8937 * ctl_data_submit_done(), it'll get passed back to
8938 * ctl_ctl_cnw_cont() for further processing.
8940 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
8941 ctsio->io_cont = ctl_cnw_cont;
8943 lbalen = (struct ctl_lba_len_flags *)
8944 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8946 lbalen->len = num_blocks;
8947 lbalen->flags = CTL_LLF_COMPARE | flags;
8949 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
8950 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8955 ctl_verify(struct ctl_scsiio *ctsio)
8957 struct ctl_lun *lun = CTL_LUN(ctsio);
8958 struct ctl_lba_len_flags *lbalen;
8960 uint32_t num_blocks;
8964 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
8967 flags = CTL_LLF_FUA;
8968 switch (ctsio->cdb[0]) {
8970 struct scsi_verify_10 *cdb;
8972 cdb = (struct scsi_verify_10 *)ctsio->cdb;
8973 if (cdb->byte2 & SVFY_BYTCHK)
8975 if (cdb->byte2 & SVFY_DPO)
8976 flags |= CTL_LLF_DPO;
8977 lba = scsi_4btoul(cdb->addr);
8978 num_blocks = scsi_2btoul(cdb->length);
8982 struct scsi_verify_12 *cdb;
8984 cdb = (struct scsi_verify_12 *)ctsio->cdb;
8985 if (cdb->byte2 & SVFY_BYTCHK)
8987 if (cdb->byte2 & SVFY_DPO)
8988 flags |= CTL_LLF_DPO;
8989 lba = scsi_4btoul(cdb->addr);
8990 num_blocks = scsi_4btoul(cdb->length);
8994 struct scsi_rw_16 *cdb;
8996 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8997 if (cdb->byte2 & SVFY_BYTCHK)
8999 if (cdb->byte2 & SVFY_DPO)
9000 flags |= CTL_LLF_DPO;
9001 lba = scsi_8btou64(cdb->addr);
9002 num_blocks = scsi_4btoul(cdb->length);
9007 * We got a command we don't support. This shouldn't
9008 * happen, commands should be filtered out above us.
9010 ctl_set_invalid_opcode(ctsio);
9011 ctl_done((union ctl_io *)ctsio);
9012 return (CTL_RETVAL_COMPLETE);
9016 * The first check is to make sure we're in bounds, the second
9017 * check is to catch wrap-around problems. If the lba + num blocks
9018 * is less than the lba, then we've wrapped around and the block
9019 * range is invalid anyway.
9021 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9022 || ((lba + num_blocks) < lba)) {
9023 ctl_set_lba_out_of_range(ctsio,
9024 MAX(lba, lun->be_lun->maxlba + 1));
9025 ctl_done((union ctl_io *)ctsio);
9026 return (CTL_RETVAL_COMPLETE);
9030 * According to SBC-3, a transfer length of 0 is not an error.
9032 if (num_blocks == 0) {
9033 ctl_set_success(ctsio);
9034 ctl_done((union ctl_io *)ctsio);
9035 return (CTL_RETVAL_COMPLETE);
9038 lbalen = (struct ctl_lba_len_flags *)
9039 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9041 lbalen->len = num_blocks;
9043 lbalen->flags = CTL_LLF_COMPARE | flags;
9044 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9046 lbalen->flags = CTL_LLF_VERIFY | flags;
9047 ctsio->kern_total_len = 0;
9049 ctsio->kern_rel_offset = 0;
9051 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9052 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9057 ctl_report_luns(struct ctl_scsiio *ctsio)
9059 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9060 struct ctl_port *port = CTL_PORT(ctsio);
9061 struct ctl_lun *lun, *request_lun = CTL_LUN(ctsio);
9062 struct scsi_report_luns *cdb;
9063 struct scsi_report_luns_data *lun_data;
9064 int num_filled, num_luns, num_port_luns, retval;
9065 uint32_t alloc_len, lun_datalen;
9066 uint32_t initidx, targ_lun_id, lun_id;
9068 retval = CTL_RETVAL_COMPLETE;
9069 cdb = (struct scsi_report_luns *)ctsio->cdb;
9071 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9074 num_port_luns = port->lun_map ? port->lun_map_size : ctl_max_luns;
9075 mtx_lock(&softc->ctl_lock);
9076 for (targ_lun_id = 0; targ_lun_id < num_port_luns; targ_lun_id++) {
9077 if (ctl_lun_map_from_port(port, targ_lun_id) != UINT32_MAX)
9080 mtx_unlock(&softc->ctl_lock);
9082 switch (cdb->select_report) {
9083 case RPL_REPORT_DEFAULT:
9084 case RPL_REPORT_ALL:
9085 case RPL_REPORT_NONSUBSID:
9087 case RPL_REPORT_WELLKNOWN:
9088 case RPL_REPORT_ADMIN:
9089 case RPL_REPORT_CONGLOM:
9093 ctl_set_invalid_field(ctsio,
9099 ctl_done((union ctl_io *)ctsio);
9101 break; /* NOTREACHED */
9104 alloc_len = scsi_4btoul(cdb->length);
9106 * The initiator has to allocate at least 16 bytes for this request,
9107 * so he can at least get the header and the first LUN. Otherwise
9108 * we reject the request (per SPC-3 rev 14, section 6.21).
9110 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9111 sizeof(struct scsi_report_luns_lundata))) {
9112 ctl_set_invalid_field(ctsio,
9118 ctl_done((union ctl_io *)ctsio);
9122 lun_datalen = sizeof(*lun_data) +
9123 (num_luns * sizeof(struct scsi_report_luns_lundata));
9125 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9126 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9127 ctsio->kern_sg_entries = 0;
9129 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9131 mtx_lock(&softc->ctl_lock);
9132 for (targ_lun_id = 0, num_filled = 0;
9133 targ_lun_id < num_port_luns && num_filled < num_luns;
9135 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9136 if (lun_id == UINT32_MAX)
9138 lun = softc->ctl_luns[lun_id];
9142 be64enc(lun_data->luns[num_filled++].lundata,
9143 ctl_encode_lun(targ_lun_id));
9146 * According to SPC-3, rev 14 section 6.21:
9148 * "The execution of a REPORT LUNS command to any valid and
9149 * installed logical unit shall clear the REPORTED LUNS DATA
9150 * HAS CHANGED unit attention condition for all logical
9151 * units of that target with respect to the requesting
9152 * initiator. A valid and installed logical unit is one
9153 * having a PERIPHERAL QUALIFIER of 000b in the standard
9154 * INQUIRY data (see 6.4.2)."
9156 * If request_lun is NULL, the LUN this report luns command
9157 * was issued to is either disabled or doesn't exist. In that
9158 * case, we shouldn't clear any pending lun change unit
9161 if (request_lun != NULL) {
9162 mtx_lock(&lun->lun_lock);
9163 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE);
9164 mtx_unlock(&lun->lun_lock);
9167 mtx_unlock(&softc->ctl_lock);
9170 * It's quite possible that we've returned fewer LUNs than we allocated
9171 * space for. Trim it.
9173 lun_datalen = sizeof(*lun_data) +
9174 (num_filled * sizeof(struct scsi_report_luns_lundata));
9175 ctsio->kern_rel_offset = 0;
9176 ctsio->kern_sg_entries = 0;
9177 ctsio->kern_data_len = min(lun_datalen, alloc_len);
9178 ctsio->kern_total_len = ctsio->kern_data_len;
9181 * We set this to the actual data length, regardless of how much
9182 * space we actually have to return results. If the user looks at
9183 * this value, he'll know whether or not he allocated enough space
9184 * and reissue the command if necessary. We don't support well
9185 * known logical units, so if the user asks for that, return none.
9187 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9190 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9193 ctl_set_success(ctsio);
9194 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9195 ctsio->be_move_done = ctl_config_move_done;
9196 ctl_datamove((union ctl_io *)ctsio);
9201 ctl_request_sense(struct ctl_scsiio *ctsio)
9203 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9204 struct ctl_lun *lun = CTL_LUN(ctsio);
9205 struct scsi_request_sense *cdb;
9206 struct scsi_sense_data *sense_ptr, *ps;
9209 u_int sense_len = SSD_FULL_SIZE;
9210 scsi_sense_data_type sense_format;
9211 ctl_ua_type ua_type;
9212 uint8_t asc = 0, ascq = 0;
9214 cdb = (struct scsi_request_sense *)ctsio->cdb;
9216 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9219 * Determine which sense format the user wants.
9221 if (cdb->byte2 & SRS_DESC)
9222 sense_format = SSD_TYPE_DESC;
9224 sense_format = SSD_TYPE_FIXED;
9226 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9227 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9228 ctsio->kern_sg_entries = 0;
9229 ctsio->kern_rel_offset = 0;
9232 * struct scsi_sense_data, which is currently set to 256 bytes, is
9233 * larger than the largest allowed value for the length field in the
9234 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9236 ctsio->kern_data_len = cdb->length;
9237 ctsio->kern_total_len = cdb->length;
9240 * If we don't have a LUN, we don't have any pending sense.
9243 ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
9244 softc->ha_link < CTL_HA_LINK_UNKNOWN)) {
9245 /* "Logical unit not supported" */
9246 ctl_set_sense_data(sense_ptr, &sense_len, NULL, sense_format,
9247 /*current_error*/ 1,
9248 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
9256 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9258 * Check for pending sense, and then for pending unit attentions.
9259 * Pending sense gets returned first, then pending unit attentions.
9261 mtx_lock(&lun->lun_lock);
9262 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT];
9264 ps += initidx % CTL_MAX_INIT_PER_PORT;
9265 if (ps != NULL && ps->error_code != 0) {
9266 scsi_sense_data_type stored_format;
9269 * Check to see which sense format was used for the stored
9272 stored_format = scsi_sense_type(ps);
9275 * If the user requested a different sense format than the
9276 * one we stored, then we need to convert it to the other
9277 * format. If we're going from descriptor to fixed format
9278 * sense data, we may lose things in translation, depending
9279 * on what options were used.
9281 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9282 * for some reason we'll just copy it out as-is.
9284 if ((stored_format == SSD_TYPE_FIXED)
9285 && (sense_format == SSD_TYPE_DESC))
9286 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9287 ps, (struct scsi_sense_data_desc *)sense_ptr);
9288 else if ((stored_format == SSD_TYPE_DESC)
9289 && (sense_format == SSD_TYPE_FIXED))
9290 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9291 ps, (struct scsi_sense_data_fixed *)sense_ptr);
9293 memcpy(sense_ptr, ps, sizeof(*sense_ptr));
9298 ua_type = ctl_build_ua(lun, initidx, sense_ptr, &sense_len,
9300 if (ua_type != CTL_UA_NONE)
9303 if (have_error == 0) {
9305 * Report informational exception if have one and allowed.
9307 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) {
9309 ascq = lun->ie_ascq;
9311 ctl_set_sense_data(sense_ptr, &sense_len, lun, sense_format,
9312 /*current_error*/ 1,
9313 /*sense_key*/ SSD_KEY_NO_SENSE,
9318 mtx_unlock(&lun->lun_lock);
9322 * We report the SCSI status as OK, since the status of the command
9323 * itself is OK. We're reporting sense as parameter data.
9325 ctl_set_success(ctsio);
9326 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9327 ctsio->be_move_done = ctl_config_move_done;
9328 ctl_datamove((union ctl_io *)ctsio);
9329 return (CTL_RETVAL_COMPLETE);
9333 ctl_tur(struct ctl_scsiio *ctsio)
9336 CTL_DEBUG_PRINT(("ctl_tur\n"));
9338 ctl_set_success(ctsio);
9339 ctl_done((union ctl_io *)ctsio);
9341 return (CTL_RETVAL_COMPLETE);
9345 * SCSI VPD page 0x00, the Supported VPD Pages page.
9348 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9350 struct ctl_lun *lun = CTL_LUN(ctsio);
9351 struct scsi_vpd_supported_pages *pages;
9355 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9356 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9357 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9358 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9359 ctsio->kern_rel_offset = 0;
9360 ctsio->kern_sg_entries = 0;
9361 ctsio->kern_data_len = min(sup_page_size, alloc_len);
9362 ctsio->kern_total_len = ctsio->kern_data_len;
9365 * The control device is always connected. The disk device, on the
9366 * other hand, may not be online all the time. Need to change this
9367 * to figure out whether the disk device is actually online or not.
9370 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9371 lun->be_lun->lun_type;
9373 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9376 /* Supported VPD pages */
9377 pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9379 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9380 /* Device Identification */
9381 pages->page_list[p++] = SVPD_DEVICE_ID;
9382 /* Extended INQUIRY Data */
9383 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9384 /* Mode Page Policy */
9385 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9387 pages->page_list[p++] = SVPD_SCSI_PORTS;
9388 /* Third-party Copy */
9389 pages->page_list[p++] = SVPD_SCSI_TPC;
9390 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9392 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9393 /* Block Device Characteristics */
9394 pages->page_list[p++] = SVPD_BDC;
9395 /* Logical Block Provisioning */
9396 pages->page_list[p++] = SVPD_LBP;
9400 ctl_set_success(ctsio);
9401 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9402 ctsio->be_move_done = ctl_config_move_done;
9403 ctl_datamove((union ctl_io *)ctsio);
9404 return (CTL_RETVAL_COMPLETE);
9408 * SCSI VPD page 0x80, the Unit Serial Number page.
9411 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9413 struct ctl_lun *lun = CTL_LUN(ctsio);
9414 struct scsi_vpd_unit_serial_number *sn_ptr;
9417 data_len = 4 + CTL_SN_LEN;
9418 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9419 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9420 ctsio->kern_rel_offset = 0;
9421 ctsio->kern_sg_entries = 0;
9422 ctsio->kern_data_len = min(data_len, alloc_len);
9423 ctsio->kern_total_len = ctsio->kern_data_len;
9426 * The control device is always connected. The disk device, on the
9427 * other hand, may not be online all the time. Need to change this
9428 * to figure out whether the disk device is actually online or not.
9431 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9432 lun->be_lun->lun_type;
9434 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9436 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9437 sn_ptr->length = CTL_SN_LEN;
9439 * If we don't have a LUN, we just leave the serial number as
9443 strncpy((char *)sn_ptr->serial_num,
9444 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9446 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9448 ctl_set_success(ctsio);
9449 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9450 ctsio->be_move_done = ctl_config_move_done;
9451 ctl_datamove((union ctl_io *)ctsio);
9452 return (CTL_RETVAL_COMPLETE);
9457 * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9460 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9462 struct ctl_lun *lun = CTL_LUN(ctsio);
9463 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9466 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9467 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9468 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9469 ctsio->kern_sg_entries = 0;
9470 ctsio->kern_rel_offset = 0;
9471 ctsio->kern_data_len = min(data_len, alloc_len);
9472 ctsio->kern_total_len = ctsio->kern_data_len;
9475 * The control device is always connected. The disk device, on the
9476 * other hand, may not be online all the time.
9479 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9480 lun->be_lun->lun_type;
9482 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9483 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9484 scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9486 * We support head of queue, ordered and simple tags.
9488 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9490 * Volatile cache supported.
9492 eid_ptr->flags3 = SVPD_EID_V_SUP;
9495 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9496 * attention for a particular IT nexus on all LUNs once we report
9497 * it to that nexus once. This bit is required as of SPC-4.
9499 eid_ptr->flags4 = SVPD_EID_LUICLR;
9502 * We support revert to defaults (RTD) bit in MODE SELECT.
9504 eid_ptr->flags5 = SVPD_EID_RTD_SUP;
9507 * XXX KDM in order to correctly answer this, we would need
9508 * information from the SIM to determine how much sense data it
9509 * can send. So this would really be a path inquiry field, most
9510 * likely. This can be set to a maximum of 252 according to SPC-4,
9511 * but the hardware may or may not be able to support that much.
9512 * 0 just means that the maximum sense data length is not reported.
9514 eid_ptr->max_sense_length = 0;
9516 ctl_set_success(ctsio);
9517 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9518 ctsio->be_move_done = ctl_config_move_done;
9519 ctl_datamove((union ctl_io *)ctsio);
9520 return (CTL_RETVAL_COMPLETE);
9524 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9526 struct ctl_lun *lun = CTL_LUN(ctsio);
9527 struct scsi_vpd_mode_page_policy *mpp_ptr;
9530 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9531 sizeof(struct scsi_vpd_mode_page_policy_descr);
9533 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9534 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9535 ctsio->kern_rel_offset = 0;
9536 ctsio->kern_sg_entries = 0;
9537 ctsio->kern_data_len = min(data_len, alloc_len);
9538 ctsio->kern_total_len = ctsio->kern_data_len;
9541 * The control device is always connected. The disk device, on the
9542 * other hand, may not be online all the time.
9545 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9546 lun->be_lun->lun_type;
9548 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9549 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9550 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9551 mpp_ptr->descr[0].page_code = 0x3f;
9552 mpp_ptr->descr[0].subpage_code = 0xff;
9553 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9555 ctl_set_success(ctsio);
9556 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9557 ctsio->be_move_done = ctl_config_move_done;
9558 ctl_datamove((union ctl_io *)ctsio);
9559 return (CTL_RETVAL_COMPLETE);
9563 * SCSI VPD page 0x83, the Device Identification page.
9566 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9568 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9569 struct ctl_port *port = CTL_PORT(ctsio);
9570 struct ctl_lun *lun = CTL_LUN(ctsio);
9571 struct scsi_vpd_device_id *devid_ptr;
9572 struct scsi_vpd_id_descriptor *desc;
9576 data_len = sizeof(struct scsi_vpd_device_id) +
9577 sizeof(struct scsi_vpd_id_descriptor) +
9578 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9579 sizeof(struct scsi_vpd_id_descriptor) +
9580 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9581 if (lun && lun->lun_devid)
9582 data_len += lun->lun_devid->len;
9583 if (port && port->port_devid)
9584 data_len += port->port_devid->len;
9585 if (port && port->target_devid)
9586 data_len += port->target_devid->len;
9588 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9589 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9590 ctsio->kern_sg_entries = 0;
9591 ctsio->kern_rel_offset = 0;
9592 ctsio->kern_sg_entries = 0;
9593 ctsio->kern_data_len = min(data_len, alloc_len);
9594 ctsio->kern_total_len = ctsio->kern_data_len;
9597 * The control device is always connected. The disk device, on the
9598 * other hand, may not be online all the time.
9601 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9602 lun->be_lun->lun_type;
9604 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9605 devid_ptr->page_code = SVPD_DEVICE_ID;
9606 scsi_ulto2b(data_len - 4, devid_ptr->length);
9608 if (port && port->port_type == CTL_PORT_FC)
9609 proto = SCSI_PROTO_FC << 4;
9610 else if (port && port->port_type == CTL_PORT_SAS)
9611 proto = SCSI_PROTO_SAS << 4;
9612 else if (port && port->port_type == CTL_PORT_ISCSI)
9613 proto = SCSI_PROTO_ISCSI << 4;
9615 proto = SCSI_PROTO_SPI << 4;
9616 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9619 * We're using a LUN association here. i.e., this device ID is a
9620 * per-LUN identifier.
9622 if (lun && lun->lun_devid) {
9623 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9624 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9625 lun->lun_devid->len);
9629 * This is for the WWPN which is a port association.
9631 if (port && port->port_devid) {
9632 memcpy(desc, port->port_devid->data, port->port_devid->len);
9633 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9634 port->port_devid->len);
9638 * This is for the Relative Target Port(type 4h) identifier
9640 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9641 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9642 SVPD_ID_TYPE_RELTARG;
9644 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9645 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9646 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9649 * This is for the Target Port Group(type 5h) identifier
9651 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9652 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9653 SVPD_ID_TYPE_TPORTGRP;
9655 if (softc->is_single ||
9656 (port && port->status & CTL_PORT_STATUS_HA_SHARED))
9659 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt;
9660 scsi_ulto2b(g, &desc->identifier[2]);
9661 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9662 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9665 * This is for the Target identifier
9667 if (port && port->target_devid) {
9668 memcpy(desc, port->target_devid->data, port->target_devid->len);
9671 ctl_set_success(ctsio);
9672 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9673 ctsio->be_move_done = ctl_config_move_done;
9674 ctl_datamove((union ctl_io *)ctsio);
9675 return (CTL_RETVAL_COMPLETE);
9679 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9681 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9682 struct ctl_lun *lun = CTL_LUN(ctsio);
9683 struct scsi_vpd_scsi_ports *sp;
9684 struct scsi_vpd_port_designation *pd;
9685 struct scsi_vpd_port_designation_cont *pdc;
9686 struct ctl_port *port;
9687 int data_len, num_target_ports, iid_len, id_len;
9689 num_target_ports = 0;
9692 mtx_lock(&softc->ctl_lock);
9693 STAILQ_FOREACH(port, &softc->port_list, links) {
9694 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9697 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9700 if (port->init_devid)
9701 iid_len += port->init_devid->len;
9702 if (port->port_devid)
9703 id_len += port->port_devid->len;
9705 mtx_unlock(&softc->ctl_lock);
9707 data_len = sizeof(struct scsi_vpd_scsi_ports) +
9708 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9709 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9710 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9711 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9712 ctsio->kern_sg_entries = 0;
9713 ctsio->kern_rel_offset = 0;
9714 ctsio->kern_sg_entries = 0;
9715 ctsio->kern_data_len = min(data_len, alloc_len);
9716 ctsio->kern_total_len = ctsio->kern_data_len;
9719 * The control device is always connected. The disk device, on the
9720 * other hand, may not be online all the time. Need to change this
9721 * to figure out whether the disk device is actually online or not.
9724 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9725 lun->be_lun->lun_type;
9727 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9729 sp->page_code = SVPD_SCSI_PORTS;
9730 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9732 pd = &sp->design[0];
9734 mtx_lock(&softc->ctl_lock);
9735 STAILQ_FOREACH(port, &softc->port_list, links) {
9736 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9739 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9741 scsi_ulto2b(port->targ_port, pd->relative_port_id);
9742 if (port->init_devid) {
9743 iid_len = port->init_devid->len;
9744 memcpy(pd->initiator_transportid,
9745 port->init_devid->data, port->init_devid->len);
9748 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9749 pdc = (struct scsi_vpd_port_designation_cont *)
9750 (&pd->initiator_transportid[iid_len]);
9751 if (port->port_devid) {
9752 id_len = port->port_devid->len;
9753 memcpy(pdc->target_port_descriptors,
9754 port->port_devid->data, port->port_devid->len);
9757 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9758 pd = (struct scsi_vpd_port_designation *)
9759 ((uint8_t *)pdc->target_port_descriptors + id_len);
9761 mtx_unlock(&softc->ctl_lock);
9763 ctl_set_success(ctsio);
9764 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9765 ctsio->be_move_done = ctl_config_move_done;
9766 ctl_datamove((union ctl_io *)ctsio);
9767 return (CTL_RETVAL_COMPLETE);
9771 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9773 struct ctl_lun *lun = CTL_LUN(ctsio);
9774 struct scsi_vpd_block_limits *bl_ptr;
9777 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9778 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9779 ctsio->kern_sg_entries = 0;
9780 ctsio->kern_rel_offset = 0;
9781 ctsio->kern_sg_entries = 0;
9782 ctsio->kern_data_len = min(sizeof(*bl_ptr), alloc_len);
9783 ctsio->kern_total_len = ctsio->kern_data_len;
9786 * The control device is always connected. The disk device, on the
9787 * other hand, may not be online all the time. Need to change this
9788 * to figure out whether the disk device is actually online or not.
9791 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9792 lun->be_lun->lun_type;
9794 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9796 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9797 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
9798 bl_ptr->max_cmp_write_len = 0xff;
9799 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9801 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
9802 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9804 ctl_get_opt_number(&lun->be_lun->options,
9805 "unmap_max_lba", &ival);
9806 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt);
9808 ctl_get_opt_number(&lun->be_lun->options,
9809 "unmap_max_descr", &ival);
9810 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt);
9811 if (lun->be_lun->ublockexp != 0) {
9812 scsi_ulto4b((1 << lun->be_lun->ublockexp),
9813 bl_ptr->opt_unmap_grain);
9814 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
9815 bl_ptr->unmap_grain_align);
9818 scsi_ulto4b(lun->be_lun->atomicblock,
9819 bl_ptr->max_atomic_transfer_length);
9820 scsi_ulto4b(0, bl_ptr->atomic_alignment);
9821 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
9822 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary);
9823 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size);
9825 ctl_get_opt_number(&lun->be_lun->options, "write_same_max_lba", &ival);
9826 scsi_u64to8b(ival, bl_ptr->max_write_same_length);
9829 ctl_set_success(ctsio);
9830 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9831 ctsio->be_move_done = ctl_config_move_done;
9832 ctl_datamove((union ctl_io *)ctsio);
9833 return (CTL_RETVAL_COMPLETE);
9837 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
9839 struct ctl_lun *lun = CTL_LUN(ctsio);
9840 struct scsi_vpd_block_device_characteristics *bdc_ptr;
9844 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
9845 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
9846 ctsio->kern_sg_entries = 0;
9847 ctsio->kern_rel_offset = 0;
9848 ctsio->kern_data_len = min(sizeof(*bdc_ptr), alloc_len);
9849 ctsio->kern_total_len = ctsio->kern_data_len;
9852 * The control device is always connected. The disk device, on the
9853 * other hand, may not be online all the time. Need to change this
9854 * to figure out whether the disk device is actually online or not.
9857 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9858 lun->be_lun->lun_type;
9860 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9861 bdc_ptr->page_code = SVPD_BDC;
9862 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
9864 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL)
9865 i = strtol(value, NULL, 0);
9867 i = CTL_DEFAULT_ROTATION_RATE;
9868 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
9870 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL)
9871 i = strtol(value, NULL, 0);
9874 bdc_ptr->wab_wac_ff = (i & 0x0f);
9875 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
9877 ctl_set_success(ctsio);
9878 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9879 ctsio->be_move_done = ctl_config_move_done;
9880 ctl_datamove((union ctl_io *)ctsio);
9881 return (CTL_RETVAL_COMPLETE);
9885 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
9887 struct ctl_lun *lun = CTL_LUN(ctsio);
9888 struct scsi_vpd_logical_block_prov *lbp_ptr;
9891 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
9892 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
9893 ctsio->kern_sg_entries = 0;
9894 ctsio->kern_rel_offset = 0;
9895 ctsio->kern_data_len = min(sizeof(*lbp_ptr), alloc_len);
9896 ctsio->kern_total_len = ctsio->kern_data_len;
9899 * The control device is always connected. The disk device, on the
9900 * other hand, may not be online all the time. Need to change this
9901 * to figure out whether the disk device is actually online or not.
9904 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9905 lun->be_lun->lun_type;
9907 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9909 lbp_ptr->page_code = SVPD_LBP;
9910 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
9911 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
9912 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9913 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
9914 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
9915 value = ctl_get_opt(&lun->be_lun->options, "provisioning_type");
9916 if (value != NULL) {
9917 if (strcmp(value, "resource") == 0)
9918 lbp_ptr->prov_type = SVPD_LBP_RESOURCE;
9919 else if (strcmp(value, "thin") == 0)
9920 lbp_ptr->prov_type = SVPD_LBP_THIN;
9922 lbp_ptr->prov_type = SVPD_LBP_THIN;
9925 ctl_set_success(ctsio);
9926 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9927 ctsio->be_move_done = ctl_config_move_done;
9928 ctl_datamove((union ctl_io *)ctsio);
9929 return (CTL_RETVAL_COMPLETE);
9933 * INQUIRY with the EVPD bit set.
9936 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
9938 struct ctl_lun *lun = CTL_LUN(ctsio);
9939 struct scsi_inquiry *cdb;
9940 int alloc_len, retval;
9942 cdb = (struct scsi_inquiry *)ctsio->cdb;
9943 alloc_len = scsi_2btoul(cdb->length);
9945 switch (cdb->page_code) {
9946 case SVPD_SUPPORTED_PAGES:
9947 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
9949 case SVPD_UNIT_SERIAL_NUMBER:
9950 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
9952 case SVPD_DEVICE_ID:
9953 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
9955 case SVPD_EXTENDED_INQUIRY_DATA:
9956 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
9958 case SVPD_MODE_PAGE_POLICY:
9959 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
9961 case SVPD_SCSI_PORTS:
9962 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
9965 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
9967 case SVPD_BLOCK_LIMITS:
9968 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
9970 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
9973 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
9975 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
9978 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
9980 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
9984 ctl_set_invalid_field(ctsio,
9990 ctl_done((union ctl_io *)ctsio);
9991 retval = CTL_RETVAL_COMPLETE;
9999 * Standard INQUIRY data.
10002 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10004 struct ctl_softc *softc = CTL_SOFTC(ctsio);
10005 struct ctl_port *port = CTL_PORT(ctsio);
10006 struct ctl_lun *lun = CTL_LUN(ctsio);
10007 struct scsi_inquiry_data *inq_ptr;
10008 struct scsi_inquiry *cdb;
10010 uint32_t alloc_len, data_len;
10011 ctl_port_type port_type;
10013 port_type = port->port_type;
10014 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10015 port_type = CTL_PORT_SCSI;
10017 cdb = (struct scsi_inquiry *)ctsio->cdb;
10018 alloc_len = scsi_2btoul(cdb->length);
10021 * We malloc the full inquiry data size here and fill it
10022 * in. If the user only asks for less, we'll give him
10025 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10026 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10027 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10028 ctsio->kern_sg_entries = 0;
10029 ctsio->kern_rel_offset = 0;
10030 ctsio->kern_data_len = min(data_len, alloc_len);
10031 ctsio->kern_total_len = ctsio->kern_data_len;
10034 if ((lun->flags & CTL_LUN_PRIMARY_SC) ||
10035 softc->ha_link >= CTL_HA_LINK_UNKNOWN) {
10036 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10037 lun->be_lun->lun_type;
10039 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) |
10040 lun->be_lun->lun_type;
10042 if (lun->flags & CTL_LUN_REMOVABLE)
10043 inq_ptr->dev_qual2 |= SID_RMB;
10045 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10047 /* RMB in byte 2 is 0 */
10048 inq_ptr->version = SCSI_REV_SPC5;
10051 * According to SAM-3, even if a device only supports a single
10052 * level of LUN addressing, it should still set the HISUP bit:
10054 * 4.9.1 Logical unit numbers overview
10056 * All logical unit number formats described in this standard are
10057 * hierarchical in structure even when only a single level in that
10058 * hierarchy is used. The HISUP bit shall be set to one in the
10059 * standard INQUIRY data (see SPC-2) when any logical unit number
10060 * format described in this standard is used. Non-hierarchical
10061 * formats are outside the scope of this standard.
10063 * Therefore we set the HiSup bit here.
10065 * The response format is 2, per SPC-3.
10067 inq_ptr->response_format = SID_HiSup | 2;
10069 inq_ptr->additional_length = data_len -
10070 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10071 CTL_DEBUG_PRINT(("additional_length = %d\n",
10072 inq_ptr->additional_length));
10074 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10075 if (port_type == CTL_PORT_SCSI)
10076 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10077 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10078 inq_ptr->flags = SID_CmdQue;
10079 if (port_type == CTL_PORT_SCSI)
10080 inq_ptr->flags |= SID_WBus16 | SID_Sync;
10083 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10084 * We have 8 bytes for the vendor name, and 16 bytes for the device
10085 * name and 4 bytes for the revision.
10087 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10088 "vendor")) == NULL) {
10089 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10091 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10092 strncpy(inq_ptr->vendor, val,
10093 min(sizeof(inq_ptr->vendor), strlen(val)));
10096 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10097 sizeof(inq_ptr->product));
10098 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10099 switch (lun->be_lun->lun_type) {
10101 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10102 sizeof(inq_ptr->product));
10105 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10106 sizeof(inq_ptr->product));
10109 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT,
10110 sizeof(inq_ptr->product));
10113 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10114 sizeof(inq_ptr->product));
10118 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10119 strncpy(inq_ptr->product, val,
10120 min(sizeof(inq_ptr->product), strlen(val)));
10124 * XXX make this a macro somewhere so it automatically gets
10125 * incremented when we make changes.
10127 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10128 "revision")) == NULL) {
10129 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10131 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10132 strncpy(inq_ptr->revision, val,
10133 min(sizeof(inq_ptr->revision), strlen(val)));
10137 * For parallel SCSI, we support double transition and single
10138 * transition clocking. We also support QAS (Quick Arbitration
10139 * and Selection) and Information Unit transfers on both the
10140 * control and array devices.
10142 if (port_type == CTL_PORT_SCSI)
10143 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10146 /* SAM-6 (no version claimed) */
10147 scsi_ulto2b(0x00C0, inq_ptr->version1);
10148 /* SPC-5 (no version claimed) */
10149 scsi_ulto2b(0x05C0, inq_ptr->version2);
10150 if (port_type == CTL_PORT_FC) {
10151 /* FCP-2 ANSI INCITS.350:2003 */
10152 scsi_ulto2b(0x0917, inq_ptr->version3);
10153 } else if (port_type == CTL_PORT_SCSI) {
10154 /* SPI-4 ANSI INCITS.362:200x */
10155 scsi_ulto2b(0x0B56, inq_ptr->version3);
10156 } else if (port_type == CTL_PORT_ISCSI) {
10157 /* iSCSI (no version claimed) */
10158 scsi_ulto2b(0x0960, inq_ptr->version3);
10159 } else if (port_type == CTL_PORT_SAS) {
10160 /* SAS (no version claimed) */
10161 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10162 } else if (port_type == CTL_PORT_UMASS) {
10163 /* USB Mass Storage Class Bulk-Only Transport, Revision 1.0 */
10164 scsi_ulto2b(0x1730, inq_ptr->version3);
10168 /* SBC-4 (no version claimed) */
10169 scsi_ulto2b(0x0600, inq_ptr->version4);
10171 switch (lun->be_lun->lun_type) {
10173 /* SBC-4 (no version claimed) */
10174 scsi_ulto2b(0x0600, inq_ptr->version4);
10179 /* MMC-6 (no version claimed) */
10180 scsi_ulto2b(0x04E0, inq_ptr->version4);
10187 ctl_set_success(ctsio);
10188 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10189 ctsio->be_move_done = ctl_config_move_done;
10190 ctl_datamove((union ctl_io *)ctsio);
10191 return (CTL_RETVAL_COMPLETE);
10195 ctl_inquiry(struct ctl_scsiio *ctsio)
10197 struct scsi_inquiry *cdb;
10200 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10202 cdb = (struct scsi_inquiry *)ctsio->cdb;
10203 if (cdb->byte2 & SI_EVPD)
10204 retval = ctl_inquiry_evpd(ctsio);
10205 else if (cdb->page_code == 0)
10206 retval = ctl_inquiry_std(ctsio);
10208 ctl_set_invalid_field(ctsio,
10214 ctl_done((union ctl_io *)ctsio);
10215 return (CTL_RETVAL_COMPLETE);
10222 ctl_get_config(struct ctl_scsiio *ctsio)
10224 struct ctl_lun *lun = CTL_LUN(ctsio);
10225 struct scsi_get_config_header *hdr;
10226 struct scsi_get_config_feature *feature;
10227 struct scsi_get_config *cdb;
10228 uint32_t alloc_len, data_len;
10231 cdb = (struct scsi_get_config *)ctsio->cdb;
10232 rt = (cdb->rt & SGC_RT_MASK);
10233 starting = scsi_2btoul(cdb->starting_feature);
10234 alloc_len = scsi_2btoul(cdb->length);
10236 data_len = sizeof(struct scsi_get_config_header) +
10237 sizeof(struct scsi_get_config_feature) + 8 +
10238 sizeof(struct scsi_get_config_feature) + 8 +
10239 sizeof(struct scsi_get_config_feature) + 4 +
10240 sizeof(struct scsi_get_config_feature) + 4 +
10241 sizeof(struct scsi_get_config_feature) + 8 +
10242 sizeof(struct scsi_get_config_feature) +
10243 sizeof(struct scsi_get_config_feature) + 4 +
10244 sizeof(struct scsi_get_config_feature) + 4 +
10245 sizeof(struct scsi_get_config_feature) + 4 +
10246 sizeof(struct scsi_get_config_feature) + 4 +
10247 sizeof(struct scsi_get_config_feature) + 4 +
10248 sizeof(struct scsi_get_config_feature) + 4;
10249 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10250 ctsio->kern_sg_entries = 0;
10251 ctsio->kern_rel_offset = 0;
10253 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr;
10254 if (lun->flags & CTL_LUN_NO_MEDIA)
10255 scsi_ulto2b(0x0000, hdr->current_profile);
10257 scsi_ulto2b(0x0010, hdr->current_profile);
10258 feature = (struct scsi_get_config_feature *)(hdr + 1);
10260 if (starting > 0x003b)
10262 if (starting > 0x003a)
10264 if (starting > 0x002b)
10266 if (starting > 0x002a)
10268 if (starting > 0x001f)
10270 if (starting > 0x001e)
10272 if (starting > 0x001d)
10274 if (starting > 0x0010)
10276 if (starting > 0x0003)
10278 if (starting > 0x0002)
10280 if (starting > 0x0001)
10282 if (starting > 0x0000)
10286 scsi_ulto2b(0x0000, feature->feature_code);
10287 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT;
10288 feature->add_length = 8;
10289 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */
10290 feature->feature_data[2] = 0x00;
10291 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */
10292 feature->feature_data[6] = 0x01;
10293 feature = (struct scsi_get_config_feature *)
10294 &feature->feature_data[feature->add_length];
10297 scsi_ulto2b(0x0001, feature->feature_code);
10298 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10299 feature->add_length = 8;
10300 scsi_ulto4b(0x00000000, &feature->feature_data[0]);
10301 feature->feature_data[4] = 0x03;
10302 feature = (struct scsi_get_config_feature *)
10303 &feature->feature_data[feature->add_length];
10306 scsi_ulto2b(0x0002, feature->feature_code);
10307 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10308 feature->add_length = 4;
10309 feature->feature_data[0] = 0x02;
10310 feature = (struct scsi_get_config_feature *)
10311 &feature->feature_data[feature->add_length];
10313 f3: /* Removable Medium */
10314 scsi_ulto2b(0x0003, feature->feature_code);
10315 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10316 feature->add_length = 4;
10317 feature->feature_data[0] = 0x39;
10318 feature = (struct scsi_get_config_feature *)
10319 &feature->feature_data[feature->add_length];
10321 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA))
10324 f10: /* Random Read */
10325 scsi_ulto2b(0x0010, feature->feature_code);
10326 feature->flags = 0x00;
10327 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10328 feature->flags |= SGC_F_CURRENT;
10329 feature->add_length = 8;
10330 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]);
10331 scsi_ulto2b(1, &feature->feature_data[4]);
10332 feature->feature_data[6] = 0x00;
10333 feature = (struct scsi_get_config_feature *)
10334 &feature->feature_data[feature->add_length];
10336 f1d: /* Multi-Read */
10337 scsi_ulto2b(0x001D, feature->feature_code);
10338 feature->flags = 0x00;
10339 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10340 feature->flags |= SGC_F_CURRENT;
10341 feature->add_length = 0;
10342 feature = (struct scsi_get_config_feature *)
10343 &feature->feature_data[feature->add_length];
10346 scsi_ulto2b(0x001E, feature->feature_code);
10347 feature->flags = 0x00;
10348 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10349 feature->flags |= SGC_F_CURRENT;
10350 feature->add_length = 4;
10351 feature->feature_data[0] = 0x00;
10352 feature = (struct scsi_get_config_feature *)
10353 &feature->feature_data[feature->add_length];
10355 f1f: /* DVD Read */
10356 scsi_ulto2b(0x001F, feature->feature_code);
10357 feature->flags = 0x08;
10358 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10359 feature->flags |= SGC_F_CURRENT;
10360 feature->add_length = 4;
10361 feature->feature_data[0] = 0x01;
10362 feature->feature_data[2] = 0x03;
10363 feature = (struct scsi_get_config_feature *)
10364 &feature->feature_data[feature->add_length];
10367 scsi_ulto2b(0x002A, feature->feature_code);
10368 feature->flags = 0x04;
10369 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10370 feature->flags |= SGC_F_CURRENT;
10371 feature->add_length = 4;
10372 feature->feature_data[0] = 0x00;
10373 feature->feature_data[1] = 0x00;
10374 feature = (struct scsi_get_config_feature *)
10375 &feature->feature_data[feature->add_length];
10378 scsi_ulto2b(0x002B, 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 = 4;
10383 feature->feature_data[0] = 0x00;
10384 feature = (struct scsi_get_config_feature *)
10385 &feature->feature_data[feature->add_length];
10387 f3a: /* DVD+RW Dual Layer */
10388 scsi_ulto2b(0x003A, feature->feature_code);
10389 feature->flags = 0x00;
10390 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10391 feature->flags |= SGC_F_CURRENT;
10392 feature->add_length = 4;
10393 feature->feature_data[0] = 0x00;
10394 feature->feature_data[1] = 0x00;
10395 feature = (struct scsi_get_config_feature *)
10396 &feature->feature_data[feature->add_length];
10398 f3b: /* DVD+R Dual Layer */
10399 scsi_ulto2b(0x003B, feature->feature_code);
10400 feature->flags = 0x00;
10401 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10402 feature->flags |= SGC_F_CURRENT;
10403 feature->add_length = 4;
10404 feature->feature_data[0] = 0x00;
10405 feature = (struct scsi_get_config_feature *)
10406 &feature->feature_data[feature->add_length];
10409 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10410 if (rt == SGC_RT_SPECIFIC && data_len > 4) {
10411 feature = (struct scsi_get_config_feature *)(hdr + 1);
10412 if (scsi_2btoul(feature->feature_code) == starting)
10413 feature = (struct scsi_get_config_feature *)
10414 &feature->feature_data[feature->add_length];
10415 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10417 scsi_ulto4b(data_len - 4, hdr->data_length);
10418 ctsio->kern_data_len = min(data_len, alloc_len);
10419 ctsio->kern_total_len = ctsio->kern_data_len;
10421 ctl_set_success(ctsio);
10422 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10423 ctsio->be_move_done = ctl_config_move_done;
10424 ctl_datamove((union ctl_io *)ctsio);
10425 return (CTL_RETVAL_COMPLETE);
10429 ctl_get_event_status(struct ctl_scsiio *ctsio)
10431 struct scsi_get_event_status_header *hdr;
10432 struct scsi_get_event_status *cdb;
10433 uint32_t alloc_len, data_len;
10436 cdb = (struct scsi_get_event_status *)ctsio->cdb;
10437 if ((cdb->byte2 & SGESN_POLLED) == 0) {
10438 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
10439 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
10440 ctl_done((union ctl_io *)ctsio);
10441 return (CTL_RETVAL_COMPLETE);
10443 notif_class = cdb->notif_class;
10444 alloc_len = scsi_2btoul(cdb->length);
10446 data_len = sizeof(struct scsi_get_event_status_header);
10447 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10448 ctsio->kern_sg_entries = 0;
10449 ctsio->kern_rel_offset = 0;
10450 ctsio->kern_data_len = min(data_len, alloc_len);
10451 ctsio->kern_total_len = ctsio->kern_data_len;
10453 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr;
10454 scsi_ulto2b(0, hdr->descr_length);
10455 hdr->nea_class = SGESN_NEA;
10456 hdr->supported_class = 0;
10458 ctl_set_success(ctsio);
10459 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10460 ctsio->be_move_done = ctl_config_move_done;
10461 ctl_datamove((union ctl_io *)ctsio);
10462 return (CTL_RETVAL_COMPLETE);
10466 ctl_mechanism_status(struct ctl_scsiio *ctsio)
10468 struct scsi_mechanism_status_header *hdr;
10469 struct scsi_mechanism_status *cdb;
10470 uint32_t alloc_len, data_len;
10472 cdb = (struct scsi_mechanism_status *)ctsio->cdb;
10473 alloc_len = scsi_2btoul(cdb->length);
10475 data_len = sizeof(struct scsi_mechanism_status_header);
10476 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10477 ctsio->kern_sg_entries = 0;
10478 ctsio->kern_rel_offset = 0;
10479 ctsio->kern_data_len = min(data_len, alloc_len);
10480 ctsio->kern_total_len = ctsio->kern_data_len;
10482 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr;
10483 hdr->state1 = 0x00;
10484 hdr->state2 = 0xe0;
10485 scsi_ulto3b(0, hdr->lba);
10486 hdr->slots_num = 0;
10487 scsi_ulto2b(0, hdr->slots_length);
10489 ctl_set_success(ctsio);
10490 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10491 ctsio->be_move_done = ctl_config_move_done;
10492 ctl_datamove((union ctl_io *)ctsio);
10493 return (CTL_RETVAL_COMPLETE);
10497 ctl_ultomsf(uint32_t lba, uint8_t *buf)
10502 buf[1] = bin2bcd((lba / 75) / 60);
10503 buf[2] = bin2bcd((lba / 75) % 60);
10504 buf[3] = bin2bcd(lba % 75);
10508 ctl_read_toc(struct ctl_scsiio *ctsio)
10510 struct ctl_lun *lun = CTL_LUN(ctsio);
10511 struct scsi_read_toc_hdr *hdr;
10512 struct scsi_read_toc_type01_descr *descr;
10513 struct scsi_read_toc *cdb;
10514 uint32_t alloc_len, data_len;
10517 cdb = (struct scsi_read_toc *)ctsio->cdb;
10518 msf = (cdb->byte2 & CD_MSF) != 0;
10519 format = cdb->format;
10520 alloc_len = scsi_2btoul(cdb->data_len);
10522 data_len = sizeof(struct scsi_read_toc_hdr);
10524 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr);
10526 data_len += sizeof(struct scsi_read_toc_type01_descr);
10527 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10528 ctsio->kern_sg_entries = 0;
10529 ctsio->kern_rel_offset = 0;
10530 ctsio->kern_data_len = min(data_len, alloc_len);
10531 ctsio->kern_total_len = ctsio->kern_data_len;
10533 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr;
10535 scsi_ulto2b(0x12, hdr->data_length);
10538 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10539 descr->addr_ctl = 0x14;
10540 descr->track_number = 1;
10542 ctl_ultomsf(0, descr->track_start);
10544 scsi_ulto4b(0, descr->track_start);
10546 descr->addr_ctl = 0x14;
10547 descr->track_number = 0xaa;
10549 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start);
10551 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start);
10553 scsi_ulto2b(0x0a, hdr->data_length);
10556 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10557 descr->addr_ctl = 0x14;
10558 descr->track_number = 1;
10560 ctl_ultomsf(0, descr->track_start);
10562 scsi_ulto4b(0, descr->track_start);
10565 ctl_set_success(ctsio);
10566 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10567 ctsio->be_move_done = ctl_config_move_done;
10568 ctl_datamove((union ctl_io *)ctsio);
10569 return (CTL_RETVAL_COMPLETE);
10573 * For known CDB types, parse the LBA and length.
10576 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10578 if (io->io_hdr.io_type != CTL_IO_SCSI)
10581 switch (io->scsiio.cdb[0]) {
10582 case COMPARE_AND_WRITE: {
10583 struct scsi_compare_and_write *cdb;
10585 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10587 *lba = scsi_8btou64(cdb->addr);
10588 *len = cdb->length;
10593 struct scsi_rw_6 *cdb;
10595 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10597 *lba = scsi_3btoul(cdb->addr);
10598 /* only 5 bits are valid in the most significant address byte */
10600 *len = cdb->length;
10605 struct scsi_rw_10 *cdb;
10607 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10609 *lba = scsi_4btoul(cdb->addr);
10610 *len = scsi_2btoul(cdb->length);
10613 case WRITE_VERIFY_10: {
10614 struct scsi_write_verify_10 *cdb;
10616 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10618 *lba = scsi_4btoul(cdb->addr);
10619 *len = scsi_2btoul(cdb->length);
10624 struct scsi_rw_12 *cdb;
10626 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10628 *lba = scsi_4btoul(cdb->addr);
10629 *len = scsi_4btoul(cdb->length);
10632 case WRITE_VERIFY_12: {
10633 struct scsi_write_verify_12 *cdb;
10635 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10637 *lba = scsi_4btoul(cdb->addr);
10638 *len = scsi_4btoul(cdb->length);
10643 struct scsi_rw_16 *cdb;
10645 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10647 *lba = scsi_8btou64(cdb->addr);
10648 *len = scsi_4btoul(cdb->length);
10651 case WRITE_ATOMIC_16: {
10652 struct scsi_write_atomic_16 *cdb;
10654 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb;
10656 *lba = scsi_8btou64(cdb->addr);
10657 *len = scsi_2btoul(cdb->length);
10660 case WRITE_VERIFY_16: {
10661 struct scsi_write_verify_16 *cdb;
10663 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10665 *lba = scsi_8btou64(cdb->addr);
10666 *len = scsi_4btoul(cdb->length);
10669 case WRITE_SAME_10: {
10670 struct scsi_write_same_10 *cdb;
10672 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10674 *lba = scsi_4btoul(cdb->addr);
10675 *len = scsi_2btoul(cdb->length);
10678 case WRITE_SAME_16: {
10679 struct scsi_write_same_16 *cdb;
10681 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10683 *lba = scsi_8btou64(cdb->addr);
10684 *len = scsi_4btoul(cdb->length);
10688 struct scsi_verify_10 *cdb;
10690 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10692 *lba = scsi_4btoul(cdb->addr);
10693 *len = scsi_2btoul(cdb->length);
10697 struct scsi_verify_12 *cdb;
10699 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10701 *lba = scsi_4btoul(cdb->addr);
10702 *len = scsi_4btoul(cdb->length);
10706 struct scsi_verify_16 *cdb;
10708 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10710 *lba = scsi_8btou64(cdb->addr);
10711 *len = scsi_4btoul(cdb->length);
10719 case SERVICE_ACTION_IN: { /* GET LBA STATUS */
10720 struct scsi_get_lba_status *cdb;
10722 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10723 *lba = scsi_8btou64(cdb->addr);
10729 break; /* NOTREACHED */
10736 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10739 uint64_t endlba1, endlba2;
10741 endlba1 = lba1 + len1 - (seq ? 0 : 1);
10742 endlba2 = lba2 + len2 - 1;
10744 if ((endlba1 < lba2) || (endlba2 < lba1))
10745 return (CTL_ACTION_PASS);
10747 return (CTL_ACTION_BLOCK);
10751 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10753 struct ctl_ptr_len_flags *ptrlen;
10754 struct scsi_unmap_desc *buf, *end, *range;
10758 /* If not UNMAP -- go other way. */
10759 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10760 io->scsiio.cdb[0] != UNMAP)
10761 return (CTL_ACTION_ERROR);
10763 /* If UNMAP without data -- block and wait for data. */
10764 ptrlen = (struct ctl_ptr_len_flags *)
10765 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10766 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10767 ptrlen->ptr == NULL)
10768 return (CTL_ACTION_BLOCK);
10770 /* UNMAP with data -- check for collision. */
10771 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10772 end = buf + ptrlen->len / sizeof(*buf);
10773 for (range = buf; range < end; range++) {
10774 lba = scsi_8btou64(range->lba);
10775 len = scsi_4btoul(range->length);
10776 if ((lba < lba2 + len2) && (lba + len > lba2))
10777 return (CTL_ACTION_BLOCK);
10779 return (CTL_ACTION_PASS);
10783 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10785 uint64_t lba1, lba2;
10786 uint64_t len1, len2;
10789 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10790 return (CTL_ACTION_ERROR);
10792 retval = ctl_extent_check_unmap(io1, lba2, len2);
10793 if (retval != CTL_ACTION_ERROR)
10796 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10797 return (CTL_ACTION_ERROR);
10799 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10801 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
10805 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2)
10807 uint64_t lba1, lba2;
10808 uint64_t len1, len2;
10810 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10811 return (CTL_ACTION_PASS);
10812 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10813 return (CTL_ACTION_ERROR);
10814 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10815 return (CTL_ACTION_ERROR);
10817 if (lba1 + len1 == lba2)
10818 return (CTL_ACTION_BLOCK);
10819 return (CTL_ACTION_PASS);
10823 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10824 union ctl_io *ooa_io)
10826 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10827 const ctl_serialize_action *serialize_row;
10830 * The initiator attempted multiple untagged commands at the same
10831 * time. Can't do that.
10833 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10834 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10835 && ((pending_io->io_hdr.nexus.targ_port ==
10836 ooa_io->io_hdr.nexus.targ_port)
10837 && (pending_io->io_hdr.nexus.initid ==
10838 ooa_io->io_hdr.nexus.initid))
10839 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10840 CTL_FLAG_STATUS_SENT)) == 0))
10841 return (CTL_ACTION_OVERLAP);
10844 * The initiator attempted to send multiple tagged commands with
10845 * the same ID. (It's fine if different initiators have the same
10848 * Even if all of those conditions are true, we don't kill the I/O
10849 * if the command ahead of us has been aborted. We won't end up
10850 * sending it to the FETD, and it's perfectly legal to resend a
10851 * command with the same tag number as long as the previous
10852 * instance of this tag number has been aborted somehow.
10854 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10855 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10856 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10857 && ((pending_io->io_hdr.nexus.targ_port ==
10858 ooa_io->io_hdr.nexus.targ_port)
10859 && (pending_io->io_hdr.nexus.initid ==
10860 ooa_io->io_hdr.nexus.initid))
10861 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10862 CTL_FLAG_STATUS_SENT)) == 0))
10863 return (CTL_ACTION_OVERLAP_TAG);
10866 * If we get a head of queue tag, SAM-3 says that we should
10867 * immediately execute it.
10869 * What happens if this command would normally block for some other
10870 * reason? e.g. a request sense with a head of queue tag
10871 * immediately after a write. Normally that would block, but this
10872 * will result in its getting executed immediately...
10874 * We currently return "pass" instead of "skip", so we'll end up
10875 * going through the rest of the queue to check for overlapped tags.
10877 * XXX KDM check for other types of blockage first??
10879 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10880 return (CTL_ACTION_PASS);
10883 * Ordered tags have to block until all items ahead of them
10884 * have completed. If we get called with an ordered tag, we always
10885 * block, if something else is ahead of us in the queue.
10887 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10888 return (CTL_ACTION_BLOCK);
10891 * Simple tags get blocked until all head of queue and ordered tags
10892 * ahead of them have completed. I'm lumping untagged commands in
10893 * with simple tags here. XXX KDM is that the right thing to do?
10895 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10896 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10897 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10898 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10899 return (CTL_ACTION_BLOCK);
10901 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
10902 KASSERT(pending_entry->seridx < CTL_SERIDX_COUNT,
10903 ("%s: Invalid seridx %d for pending CDB %02x %02x @ %p",
10904 __func__, pending_entry->seridx, pending_io->scsiio.cdb[0],
10905 pending_io->scsiio.cdb[1], pending_io));
10906 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
10907 if (ooa_entry->seridx == CTL_SERIDX_INVLD)
10908 return (CTL_ACTION_PASS); /* Unsupported command in OOA queue */
10909 KASSERT(ooa_entry->seridx < CTL_SERIDX_COUNT,
10910 ("%s: Invalid seridx %d for ooa CDB %02x %02x @ %p",
10911 __func__, ooa_entry->seridx, ooa_io->scsiio.cdb[0],
10912 ooa_io->scsiio.cdb[1], ooa_io));
10914 serialize_row = ctl_serialize_table[ooa_entry->seridx];
10916 switch (serialize_row[pending_entry->seridx]) {
10917 case CTL_SER_BLOCK:
10918 return (CTL_ACTION_BLOCK);
10919 case CTL_SER_EXTENT:
10920 return (ctl_extent_check(ooa_io, pending_io,
10921 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10922 case CTL_SER_EXTENTOPT:
10923 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
10924 SCP_QUEUE_ALG_UNRESTRICTED)
10925 return (ctl_extent_check(ooa_io, pending_io,
10927 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10928 return (CTL_ACTION_PASS);
10929 case CTL_SER_EXTENTSEQ:
10930 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF)
10931 return (ctl_extent_check_seq(ooa_io, pending_io));
10932 return (CTL_ACTION_PASS);
10934 return (CTL_ACTION_PASS);
10935 case CTL_SER_BLOCKOPT:
10936 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
10937 SCP_QUEUE_ALG_UNRESTRICTED)
10938 return (CTL_ACTION_BLOCK);
10939 return (CTL_ACTION_PASS);
10941 return (CTL_ACTION_SKIP);
10943 panic("%s: Invalid serialization value %d for %d => %d",
10944 __func__, serialize_row[pending_entry->seridx],
10945 pending_entry->seridx, ooa_entry->seridx);
10948 return (CTL_ACTION_ERROR);
10952 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
10954 * - pending_io is generally either incoming, or on the blocked queue
10955 * - starting I/O is the I/O we want to start the check with.
10958 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
10959 union ctl_io *starting_io)
10961 union ctl_io *ooa_io;
10964 mtx_assert(&lun->lun_lock, MA_OWNED);
10967 * Run back along the OOA queue, starting with the current
10968 * blocked I/O and going through every I/O before it on the
10969 * queue. If starting_io is NULL, we'll just end up returning
10972 for (ooa_io = starting_io; ooa_io != NULL;
10973 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
10977 * This routine just checks to see whether
10978 * cur_blocked is blocked by ooa_io, which is ahead
10979 * of it in the queue. It doesn't queue/dequeue
10982 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
10984 case CTL_ACTION_BLOCK:
10985 case CTL_ACTION_OVERLAP:
10986 case CTL_ACTION_OVERLAP_TAG:
10987 case CTL_ACTION_SKIP:
10988 case CTL_ACTION_ERROR:
10990 break; /* NOTREACHED */
10991 case CTL_ACTION_PASS:
10994 panic("%s: Invalid action %d\n", __func__, action);
10998 return (CTL_ACTION_PASS);
11003 * - An I/O has just completed, and has been removed from the per-LUN OOA
11004 * queue, so some items on the blocked queue may now be unblocked.
11007 ctl_check_blocked(struct ctl_lun *lun)
11009 struct ctl_softc *softc = lun->ctl_softc;
11010 union ctl_io *cur_blocked, *next_blocked;
11012 mtx_assert(&lun->lun_lock, MA_OWNED);
11015 * Run forward from the head of the blocked queue, checking each
11016 * entry against the I/Os prior to it on the OOA queue to see if
11017 * there is still any blockage.
11019 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
11020 * with our removing a variable on it while it is traversing the
11023 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
11024 cur_blocked != NULL; cur_blocked = next_blocked) {
11025 union ctl_io *prev_ooa;
11028 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
11031 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
11032 ctl_ooaq, ooa_links);
11035 * If cur_blocked happens to be the first item in the OOA
11036 * queue now, prev_ooa will be NULL, and the action
11037 * returned will just be CTL_ACTION_PASS.
11039 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
11042 case CTL_ACTION_BLOCK:
11043 /* Nothing to do here, still blocked */
11045 case CTL_ACTION_OVERLAP:
11046 case CTL_ACTION_OVERLAP_TAG:
11048 * This shouldn't happen! In theory we've already
11049 * checked this command for overlap...
11052 case CTL_ACTION_PASS:
11053 case CTL_ACTION_SKIP: {
11054 const struct ctl_cmd_entry *entry;
11057 * The skip case shouldn't happen, this transaction
11058 * should have never made it onto the blocked queue.
11061 * This I/O is no longer blocked, we can remove it
11062 * from the blocked queue. Since this is a TAILQ
11063 * (doubly linked list), we can do O(1) removals
11064 * from any place on the list.
11066 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
11068 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11070 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
11071 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){
11073 * Need to send IO back to original side to
11076 union ctl_ha_msg msg_info;
11078 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11079 msg_info.hdr.original_sc =
11080 cur_blocked->io_hdr.original_sc;
11081 msg_info.hdr.serializing_sc = cur_blocked;
11082 msg_info.hdr.msg_type = CTL_MSG_R2R;
11083 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11084 sizeof(msg_info.hdr), M_NOWAIT);
11087 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
11090 * Check this I/O for LUN state changes that may
11091 * have happened while this command was blocked.
11092 * The LUN state may have been changed by a command
11093 * ahead of us in the queue, so we need to re-check
11094 * for any states that can be caused by SCSI
11097 if (ctl_scsiio_lun_check(lun, entry,
11098 &cur_blocked->scsiio) == 0) {
11099 cur_blocked->io_hdr.flags |=
11100 CTL_FLAG_IS_WAS_ON_RTR;
11101 ctl_enqueue_rtr(cur_blocked);
11103 ctl_done(cur_blocked);
11108 * This probably shouldn't happen -- we shouldn't
11109 * get CTL_ACTION_ERROR, or anything else.
11115 return (CTL_RETVAL_COMPLETE);
11119 * This routine (with one exception) checks LUN flags that can be set by
11120 * commands ahead of us in the OOA queue. These flags have to be checked
11121 * when a command initially comes in, and when we pull a command off the
11122 * blocked queue and are preparing to execute it. The reason we have to
11123 * check these flags for commands on the blocked queue is that the LUN
11124 * state may have been changed by a command ahead of us while we're on the
11127 * Ordering is somewhat important with these checks, so please pay
11128 * careful attention to the placement of any new checks.
11131 ctl_scsiio_lun_check(struct ctl_lun *lun,
11132 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11134 struct ctl_softc *softc = lun->ctl_softc;
11140 mtx_assert(&lun->lun_lock, MA_OWNED);
11143 * If this shelf is a secondary shelf controller, we may have to
11144 * reject some commands disallowed by HA mode and link state.
11146 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11147 if (softc->ha_link == CTL_HA_LINK_OFFLINE &&
11148 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11149 ctl_set_lun_unavail(ctsio);
11153 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 &&
11154 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11155 ctl_set_lun_transit(ctsio);
11159 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY &&
11160 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) {
11161 ctl_set_lun_standby(ctsio);
11166 /* The rest of checks are only done on executing side */
11167 if (softc->ha_mode == CTL_HA_MODE_XFER)
11171 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11173 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) {
11174 ctl_set_hw_write_protected(ctsio);
11178 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) {
11179 ctl_set_sense(ctsio, /*current_error*/ 1,
11180 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11181 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11188 * Check for a reservation conflict. If this command isn't allowed
11189 * even on reserved LUNs, and if this initiator isn't the one who
11190 * reserved us, reject the command with a reservation conflict.
11192 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11193 if ((lun->flags & CTL_LUN_RESERVED)
11194 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11195 if (lun->res_idx != residx) {
11196 ctl_set_reservation_conflict(ctsio);
11202 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11203 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11204 /* No reservation or command is allowed. */;
11205 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11206 (lun->pr_res_type == SPR_TYPE_WR_EX ||
11207 lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
11208 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) {
11209 /* The command is allowed for Write Exclusive resv. */;
11212 * if we aren't registered or it's a res holder type
11213 * reservation and this isn't the res holder then set a
11216 if (ctl_get_prkey(lun, residx) == 0 ||
11217 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) {
11218 ctl_set_reservation_conflict(ctsio);
11224 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) {
11225 if (lun->flags & CTL_LUN_EJECTED)
11226 ctl_set_lun_ejected(ctsio);
11227 else if (lun->flags & CTL_LUN_NO_MEDIA) {
11228 if (lun->flags & CTL_LUN_REMOVABLE)
11229 ctl_set_lun_no_media(ctsio);
11231 ctl_set_lun_int_reqd(ctsio);
11232 } else if (lun->flags & CTL_LUN_STOPPED)
11233 ctl_set_lun_stopped(ctsio);
11245 ctl_failover_io(union ctl_io *io, int have_lock)
11247 ctl_set_busy(&io->scsiio);
11252 ctl_failover_lun(union ctl_io *rio)
11254 struct ctl_softc *softc = CTL_SOFTC(rio);
11255 struct ctl_lun *lun;
11256 struct ctl_io_hdr *io, *next_io;
11259 targ_lun = rio->io_hdr.nexus.targ_mapped_lun;
11260 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun));
11262 /* Find and lock the LUN. */
11263 mtx_lock(&softc->ctl_lock);
11264 if (targ_lun > ctl_max_luns ||
11265 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11266 mtx_unlock(&softc->ctl_lock);
11269 mtx_lock(&lun->lun_lock);
11270 mtx_unlock(&softc->ctl_lock);
11271 if (lun->flags & CTL_LUN_DISABLED) {
11272 mtx_unlock(&lun->lun_lock);
11276 if (softc->ha_mode == CTL_HA_MODE_XFER) {
11277 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11278 /* We are master */
11279 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11280 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11281 io->flags |= CTL_FLAG_ABORT;
11282 io->flags |= CTL_FLAG_FAILOVER;
11283 } else { /* This can be only due to DATAMOVE */
11284 io->msg_type = CTL_MSG_DATAMOVE_DONE;
11285 io->flags &= ~CTL_FLAG_DMA_INPROG;
11286 io->flags |= CTL_FLAG_IO_ACTIVE;
11287 io->port_status = 31340;
11288 ctl_enqueue_isc((union ctl_io *)io);
11292 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11293 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11294 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11295 io->flags |= CTL_FLAG_FAILOVER;
11297 ctl_set_busy(&((union ctl_io *)io)->
11299 ctl_done((union ctl_io *)io);
11303 } else { /* SERIALIZE modes */
11304 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links,
11306 /* We are master */
11307 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11308 TAILQ_REMOVE(&lun->blocked_queue, io,
11310 io->flags &= ~CTL_FLAG_BLOCKED;
11311 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11312 ctl_free_io((union ctl_io *)io);
11315 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11316 /* We are master */
11317 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11318 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11319 ctl_free_io((union ctl_io *)io);
11322 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11323 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11324 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) {
11325 ctl_set_busy(&((union ctl_io *)io)->
11327 ctl_done((union ctl_io *)io);
11331 ctl_check_blocked(lun);
11333 mtx_unlock(&lun->lun_lock);
11337 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio)
11339 struct ctl_lun *lun;
11340 const struct ctl_cmd_entry *entry;
11341 uint32_t initidx, targ_lun;
11345 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11346 if (targ_lun < ctl_max_luns)
11347 lun = softc->ctl_luns[targ_lun];
11350 * If the LUN is invalid, pretend that it doesn't exist.
11351 * It will go away as soon as all pending I/O has been
11354 mtx_lock(&lun->lun_lock);
11355 if (lun->flags & CTL_LUN_DISABLED) {
11356 mtx_unlock(&lun->lun_lock);
11360 CTL_LUN(ctsio) = lun;
11362 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
11365 * Every I/O goes into the OOA queue for a particular LUN,
11366 * and stays there until completion.
11369 if (TAILQ_EMPTY(&lun->ooa_queue))
11370 lun->idle_time += getsbinuptime() - lun->last_busy;
11372 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
11375 /* Get command entry and return error if it is unsuppotyed. */
11376 entry = ctl_validate_command(ctsio);
11377 if (entry == NULL) {
11379 mtx_unlock(&lun->lun_lock);
11383 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11384 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11387 * Check to see whether we can send this command to LUNs that don't
11388 * exist. This should pretty much only be the case for inquiry
11389 * and request sense. Further checks, below, really require having
11390 * a LUN, so we can't really check the command anymore. Just put
11391 * it on the rtr queue.
11394 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) {
11395 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11396 ctl_enqueue_rtr((union ctl_io *)ctsio);
11400 ctl_set_unsupported_lun(ctsio);
11401 ctl_done((union ctl_io *)ctsio);
11402 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11406 * Make sure we support this particular command on this LUN.
11407 * e.g., we don't support writes to the control LUN.
11409 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11410 mtx_unlock(&lun->lun_lock);
11411 ctl_set_invalid_opcode(ctsio);
11412 ctl_done((union ctl_io *)ctsio);
11417 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11420 * If we've got a request sense, it'll clear the contingent
11421 * allegiance condition. Otherwise, if we have a CA condition for
11422 * this initiator, clear it, because it sent down a command other
11423 * than request sense.
11425 if (ctsio->cdb[0] != REQUEST_SENSE) {
11426 struct scsi_sense_data *ps;
11428 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT];
11430 ps[initidx % CTL_MAX_INIT_PER_PORT].error_code = 0;
11434 * If the command has this flag set, it handles its own unit
11435 * attention reporting, we shouldn't do anything. Otherwise we
11436 * check for any pending unit attentions, and send them back to the
11437 * initiator. We only do this when a command initially comes in,
11438 * not when we pull it off the blocked queue.
11440 * According to SAM-3, section 5.3.2, the order that things get
11441 * presented back to the host is basically unit attentions caused
11442 * by some sort of reset event, busy status, reservation conflicts
11443 * or task set full, and finally any other status.
11445 * One issue here is that some of the unit attentions we report
11446 * don't fall into the "reset" category (e.g. "reported luns data
11447 * has changed"). So reporting it here, before the reservation
11448 * check, may be technically wrong. I guess the only thing to do
11449 * would be to check for and report the reset events here, and then
11450 * check for the other unit attention types after we check for a
11451 * reservation conflict.
11453 * XXX KDM need to fix this
11455 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11456 ctl_ua_type ua_type;
11457 u_int sense_len = 0;
11459 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11460 &sense_len, SSD_TYPE_NONE);
11461 if (ua_type != CTL_UA_NONE) {
11462 mtx_unlock(&lun->lun_lock);
11463 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11464 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11465 ctsio->sense_len = sense_len;
11466 ctl_done((union ctl_io *)ctsio);
11472 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11473 mtx_unlock(&lun->lun_lock);
11474 ctl_done((union ctl_io *)ctsio);
11479 * XXX CHD this is where we want to send IO to other side if
11480 * this LUN is secondary on this SC. We will need to make a copy
11481 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11482 * the copy we send as FROM_OTHER.
11483 * We also need to stuff the address of the original IO so we can
11484 * find it easily. Something similar will need be done on the other
11485 * side so when we are done we can find the copy.
11487 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
11488 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 &&
11489 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) {
11490 union ctl_ha_msg msg_info;
11493 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11494 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11495 mtx_unlock(&lun->lun_lock);
11497 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11498 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11499 msg_info.hdr.serializing_sc = NULL;
11500 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11501 msg_info.scsi.tag_num = ctsio->tag_num;
11502 msg_info.scsi.tag_type = ctsio->tag_type;
11503 msg_info.scsi.cdb_len = ctsio->cdb_len;
11504 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11506 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11507 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data),
11508 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) {
11509 ctl_set_busy(ctsio);
11510 ctl_done((union ctl_io *)ctsio);
11516 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11517 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11518 ctl_ooaq, ooa_links))) {
11519 case CTL_ACTION_BLOCK:
11520 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11521 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11523 mtx_unlock(&lun->lun_lock);
11525 case CTL_ACTION_PASS:
11526 case CTL_ACTION_SKIP:
11527 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11528 mtx_unlock(&lun->lun_lock);
11529 ctl_enqueue_rtr((union ctl_io *)ctsio);
11531 case CTL_ACTION_OVERLAP:
11532 mtx_unlock(&lun->lun_lock);
11533 ctl_set_overlapped_cmd(ctsio);
11534 ctl_done((union ctl_io *)ctsio);
11536 case CTL_ACTION_OVERLAP_TAG:
11537 mtx_unlock(&lun->lun_lock);
11538 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11539 ctl_done((union ctl_io *)ctsio);
11541 case CTL_ACTION_ERROR:
11543 mtx_unlock(&lun->lun_lock);
11544 ctl_set_internal_failure(ctsio,
11546 /*retry_count*/ 0);
11547 ctl_done((union ctl_io *)ctsio);
11553 const struct ctl_cmd_entry *
11554 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11556 const struct ctl_cmd_entry *entry;
11557 int service_action;
11559 entry = &ctl_cmd_table[ctsio->cdb[0]];
11561 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11562 if (entry->flags & CTL_CMD_FLAG_SA5) {
11563 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11564 entry = &((const struct ctl_cmd_entry *)
11565 entry->execute)[service_action];
11570 const struct ctl_cmd_entry *
11571 ctl_validate_command(struct ctl_scsiio *ctsio)
11573 const struct ctl_cmd_entry *entry;
11577 entry = ctl_get_cmd_entry(ctsio, &sa);
11578 if (entry->execute == NULL) {
11580 ctl_set_invalid_field(ctsio,
11587 ctl_set_invalid_opcode(ctsio);
11588 ctl_done((union ctl_io *)ctsio);
11591 KASSERT(entry->length > 0,
11592 ("Not defined length for command 0x%02x/0x%02x",
11593 ctsio->cdb[0], ctsio->cdb[1]));
11594 for (i = 1; i < entry->length; i++) {
11595 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11598 ctl_set_invalid_field(ctsio,
11603 /*bit*/ fls(diff) - 1);
11604 ctl_done((union ctl_io *)ctsio);
11611 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11614 switch (lun_type) {
11616 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0)
11620 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
11624 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0)
11634 ctl_scsiio(struct ctl_scsiio *ctsio)
11637 const struct ctl_cmd_entry *entry;
11639 retval = CTL_RETVAL_COMPLETE;
11641 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11643 entry = ctl_get_cmd_entry(ctsio, NULL);
11646 * If this I/O has been aborted, just send it straight to
11647 * ctl_done() without executing it.
11649 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11650 ctl_done((union ctl_io *)ctsio);
11655 * All the checks should have been handled by ctl_scsiio_precheck().
11656 * We should be clear now to just execute the I/O.
11658 retval = entry->execute(ctsio);
11665 ctl_target_reset(union ctl_io *io)
11667 struct ctl_softc *softc = CTL_SOFTC(io);
11668 struct ctl_port *port = CTL_PORT(io);
11669 struct ctl_lun *lun;
11671 ctl_ua_type ua_type;
11673 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11674 union ctl_ha_msg msg_info;
11676 msg_info.hdr.nexus = io->io_hdr.nexus;
11677 msg_info.task.task_action = io->taskio.task_action;
11678 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11679 msg_info.hdr.original_sc = NULL;
11680 msg_info.hdr.serializing_sc = NULL;
11681 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11682 sizeof(msg_info.task), M_WAITOK);
11685 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11686 if (io->taskio.task_action == CTL_TASK_TARGET_RESET)
11687 ua_type = CTL_UA_TARG_RESET;
11689 ua_type = CTL_UA_BUS_RESET;
11690 mtx_lock(&softc->ctl_lock);
11691 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11692 if (port != NULL &&
11693 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
11695 ctl_do_lun_reset(lun, initidx, ua_type);
11697 mtx_unlock(&softc->ctl_lock);
11698 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11703 * The LUN should always be set. The I/O is optional, and is used to
11704 * distinguish between I/Os sent by this initiator, and by other
11705 * initiators. We set unit attention for initiators other than this one.
11706 * SAM-3 is vague on this point. It does say that a unit attention should
11707 * be established for other initiators when a LUN is reset (see section
11708 * 5.7.3), but it doesn't specifically say that the unit attention should
11709 * be established for this particular initiator when a LUN is reset. Here
11710 * is the relevant text, from SAM-3 rev 8:
11712 * 5.7.2 When a SCSI initiator port aborts its own tasks
11714 * When a SCSI initiator port causes its own task(s) to be aborted, no
11715 * notification that the task(s) have been aborted shall be returned to
11716 * the SCSI initiator port other than the completion response for the
11717 * command or task management function action that caused the task(s) to
11718 * be aborted and notification(s) associated with related effects of the
11719 * action (e.g., a reset unit attention condition).
11721 * XXX KDM for now, we're setting unit attention for all initiators.
11724 ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua_type)
11729 mtx_lock(&lun->lun_lock);
11731 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11732 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11733 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11736 for (i = 0; i < ctl_max_ports; i++) {
11737 free(lun->pending_sense[i], M_CTL);
11738 lun->pending_sense[i] = NULL;
11740 /* Clear reservation. */
11741 lun->flags &= ~CTL_LUN_RESERVED;
11742 /* Clear prevent media removal. */
11743 if (lun->prevent) {
11744 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11745 ctl_clear_mask(lun->prevent, i);
11746 lun->prevent_count = 0;
11748 /* Clear TPC status */
11749 ctl_tpc_lun_clear(lun, -1);
11750 /* Establish UA. */
11752 ctl_est_ua_all(lun, initidx, ua_type);
11754 ctl_est_ua_all(lun, -1, ua_type);
11756 mtx_unlock(&lun->lun_lock);
11760 ctl_lun_reset(union ctl_io *io)
11762 struct ctl_softc *softc = CTL_SOFTC(io);
11763 struct ctl_lun *lun;
11764 uint32_t targ_lun, initidx;
11766 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11767 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11768 mtx_lock(&softc->ctl_lock);
11769 if (targ_lun >= ctl_max_luns ||
11770 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11771 mtx_unlock(&softc->ctl_lock);
11772 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11775 ctl_do_lun_reset(lun, initidx, CTL_UA_LUN_RESET);
11776 mtx_unlock(&softc->ctl_lock);
11777 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11779 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) {
11780 union ctl_ha_msg msg_info;
11782 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11783 msg_info.hdr.nexus = io->io_hdr.nexus;
11784 msg_info.task.task_action = CTL_TASK_LUN_RESET;
11785 msg_info.hdr.original_sc = NULL;
11786 msg_info.hdr.serializing_sc = NULL;
11787 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11788 sizeof(msg_info.task), M_WAITOK);
11794 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11799 mtx_assert(&lun->lun_lock, MA_OWNED);
11802 * Run through the OOA queue and attempt to find the given I/O.
11803 * The target port, initiator ID, tag type and tag number have to
11804 * match the values that we got from the initiator. If we have an
11805 * untagged command to abort, simply abort the first untagged command
11806 * we come to. We only allow one untagged command at a time of course.
11808 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11809 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11811 if ((targ_port == UINT32_MAX ||
11812 targ_port == xio->io_hdr.nexus.targ_port) &&
11813 (init_id == UINT32_MAX ||
11814 init_id == xio->io_hdr.nexus.initid)) {
11815 if (targ_port != xio->io_hdr.nexus.targ_port ||
11816 init_id != xio->io_hdr.nexus.initid)
11817 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
11818 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11819 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11820 union ctl_ha_msg msg_info;
11822 msg_info.hdr.nexus = xio->io_hdr.nexus;
11823 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11824 msg_info.task.tag_num = xio->scsiio.tag_num;
11825 msg_info.task.tag_type = xio->scsiio.tag_type;
11826 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11827 msg_info.hdr.original_sc = NULL;
11828 msg_info.hdr.serializing_sc = NULL;
11829 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11830 sizeof(msg_info.task), M_NOWAIT);
11837 ctl_abort_task_set(union ctl_io *io)
11839 struct ctl_softc *softc = CTL_SOFTC(io);
11840 struct ctl_lun *lun;
11846 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11847 mtx_lock(&softc->ctl_lock);
11848 if (targ_lun >= ctl_max_luns ||
11849 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11850 mtx_unlock(&softc->ctl_lock);
11851 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11855 mtx_lock(&lun->lun_lock);
11856 mtx_unlock(&softc->ctl_lock);
11857 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
11858 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11859 io->io_hdr.nexus.initid,
11860 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11861 } else { /* CTL_TASK_CLEAR_TASK_SET */
11862 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
11863 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11865 mtx_unlock(&lun->lun_lock);
11866 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11871 ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx,
11872 ctl_ua_type ua_type)
11874 struct ctl_lun *lun;
11875 struct scsi_sense_data *ps;
11878 p = initidx / CTL_MAX_INIT_PER_PORT;
11879 i = initidx % CTL_MAX_INIT_PER_PORT;
11880 mtx_lock(&softc->ctl_lock);
11881 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11882 mtx_lock(&lun->lun_lock);
11884 ctl_abort_tasks_lun(lun, p, i, 1);
11886 ps = lun->pending_sense[p];
11888 ps[i].error_code = 0;
11889 /* Clear reservation. */
11890 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx))
11891 lun->flags &= ~CTL_LUN_RESERVED;
11892 /* Clear prevent media removal. */
11893 if (lun->prevent && ctl_is_set(lun->prevent, initidx)) {
11894 ctl_clear_mask(lun->prevent, initidx);
11895 lun->prevent_count--;
11897 /* Clear TPC status */
11898 ctl_tpc_lun_clear(lun, initidx);
11899 /* Establish UA. */
11900 ctl_est_ua(lun, initidx, ua_type);
11901 mtx_unlock(&lun->lun_lock);
11903 mtx_unlock(&softc->ctl_lock);
11907 ctl_i_t_nexus_reset(union ctl_io *io)
11909 struct ctl_softc *softc = CTL_SOFTC(io);
11912 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11913 union ctl_ha_msg msg_info;
11915 msg_info.hdr.nexus = io->io_hdr.nexus;
11916 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET;
11917 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11918 msg_info.hdr.original_sc = NULL;
11919 msg_info.hdr.serializing_sc = NULL;
11920 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11921 sizeof(msg_info.task), M_WAITOK);
11924 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11925 ctl_i_t_nexus_loss(softc, initidx, CTL_UA_I_T_NEXUS_LOSS);
11926 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11931 ctl_abort_task(union ctl_io *io)
11933 struct ctl_softc *softc = CTL_SOFTC(io);
11935 struct ctl_lun *lun;
11938 char printbuf[128];
11948 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11949 mtx_lock(&softc->ctl_lock);
11950 if (targ_lun >= ctl_max_luns ||
11951 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11952 mtx_unlock(&softc->ctl_lock);
11953 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11958 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
11959 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
11962 mtx_lock(&lun->lun_lock);
11963 mtx_unlock(&softc->ctl_lock);
11965 * Run through the OOA queue and attempt to find the given I/O.
11966 * The target port, initiator ID, tag type and tag number have to
11967 * match the values that we got from the initiator. If we have an
11968 * untagged command to abort, simply abort the first untagged command
11969 * we come to. We only allow one untagged command at a time of course.
11971 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11972 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11974 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
11976 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
11977 lun->lun, xio->scsiio.tag_num,
11978 xio->scsiio.tag_type,
11979 (xio->io_hdr.blocked_links.tqe_prev
11980 == NULL) ? "" : " BLOCKED",
11981 (xio->io_hdr.flags &
11982 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
11983 (xio->io_hdr.flags &
11984 CTL_FLAG_ABORT) ? " ABORT" : "",
11985 (xio->io_hdr.flags &
11986 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
11987 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
11989 printf("%s\n", sbuf_data(&sb));
11992 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
11993 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
11994 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
11998 * If the abort says that the task is untagged, the
11999 * task in the queue must be untagged. Otherwise,
12000 * we just check to see whether the tag numbers
12001 * match. This is because the QLogic firmware
12002 * doesn't pass back the tag type in an abort
12006 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12007 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12008 || (xio->scsiio.tag_num == io->taskio.tag_num))
12011 * XXX KDM we've got problems with FC, because it
12012 * doesn't send down a tag type with aborts. So we
12013 * can only really go by the tag number...
12014 * This may cause problems with parallel SCSI.
12015 * Need to figure that out!!
12017 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12018 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12020 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
12021 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12022 union ctl_ha_msg msg_info;
12024 msg_info.hdr.nexus = io->io_hdr.nexus;
12025 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12026 msg_info.task.tag_num = io->taskio.tag_num;
12027 msg_info.task.tag_type = io->taskio.tag_type;
12028 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12029 msg_info.hdr.original_sc = NULL;
12030 msg_info.hdr.serializing_sc = NULL;
12032 printf("Sent Abort to other side\n");
12034 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12035 sizeof(msg_info.task), M_NOWAIT);
12038 printf("ctl_abort_task: found I/O to abort\n");
12042 mtx_unlock(&lun->lun_lock);
12046 * This isn't really an error. It's entirely possible for
12047 * the abort and command completion to cross on the wire.
12048 * This is more of an informative/diagnostic error.
12051 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
12052 "%u:%u:%u tag %d type %d\n",
12053 io->io_hdr.nexus.initid,
12054 io->io_hdr.nexus.targ_port,
12055 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
12056 io->taskio.tag_type);
12059 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12064 ctl_query_task(union ctl_io *io, int task_set)
12066 struct ctl_softc *softc = CTL_SOFTC(io);
12068 struct ctl_lun *lun;
12072 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12073 mtx_lock(&softc->ctl_lock);
12074 if (targ_lun >= ctl_max_luns ||
12075 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12076 mtx_unlock(&softc->ctl_lock);
12077 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12080 mtx_lock(&lun->lun_lock);
12081 mtx_unlock(&softc->ctl_lock);
12082 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12083 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12085 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12086 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12087 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12090 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) {
12095 mtx_unlock(&lun->lun_lock);
12097 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12099 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12104 ctl_query_async_event(union ctl_io *io)
12106 struct ctl_softc *softc = CTL_SOFTC(io);
12107 struct ctl_lun *lun;
12109 uint32_t targ_lun, initidx;
12111 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12112 mtx_lock(&softc->ctl_lock);
12113 if (targ_lun >= ctl_max_luns ||
12114 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12115 mtx_unlock(&softc->ctl_lock);
12116 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12119 mtx_lock(&lun->lun_lock);
12120 mtx_unlock(&softc->ctl_lock);
12121 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12122 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp);
12123 mtx_unlock(&lun->lun_lock);
12124 if (ua != CTL_UA_NONE)
12125 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12127 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12132 ctl_run_task(union ctl_io *io)
12136 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12137 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12138 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type));
12139 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED;
12140 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp));
12141 switch (io->taskio.task_action) {
12142 case CTL_TASK_ABORT_TASK:
12143 retval = ctl_abort_task(io);
12145 case CTL_TASK_ABORT_TASK_SET:
12146 case CTL_TASK_CLEAR_TASK_SET:
12147 retval = ctl_abort_task_set(io);
12149 case CTL_TASK_CLEAR_ACA:
12151 case CTL_TASK_I_T_NEXUS_RESET:
12152 retval = ctl_i_t_nexus_reset(io);
12154 case CTL_TASK_LUN_RESET:
12155 retval = ctl_lun_reset(io);
12157 case CTL_TASK_TARGET_RESET:
12158 case CTL_TASK_BUS_RESET:
12159 retval = ctl_target_reset(io);
12161 case CTL_TASK_PORT_LOGIN:
12163 case CTL_TASK_PORT_LOGOUT:
12165 case CTL_TASK_QUERY_TASK:
12166 retval = ctl_query_task(io, 0);
12168 case CTL_TASK_QUERY_TASK_SET:
12169 retval = ctl_query_task(io, 1);
12171 case CTL_TASK_QUERY_ASYNC_EVENT:
12172 retval = ctl_query_async_event(io);
12175 printf("%s: got unknown task management event %d\n",
12176 __func__, io->taskio.task_action);
12180 io->io_hdr.status = CTL_SUCCESS;
12182 io->io_hdr.status = CTL_ERROR;
12187 * For HA operation. Handle commands that come in from the other
12191 ctl_handle_isc(union ctl_io *io)
12193 struct ctl_softc *softc = CTL_SOFTC(io);
12194 struct ctl_lun *lun;
12195 const struct ctl_cmd_entry *entry;
12198 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12199 switch (io->io_hdr.msg_type) {
12200 case CTL_MSG_SERIALIZE:
12201 ctl_serialize_other_sc_cmd(&io->scsiio);
12203 case CTL_MSG_R2R: /* Only used in SER_ONLY mode. */
12204 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12205 if (targ_lun >= ctl_max_luns ||
12206 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12210 mtx_lock(&lun->lun_lock);
12211 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) {
12212 mtx_unlock(&lun->lun_lock);
12216 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12217 mtx_unlock(&lun->lun_lock);
12218 ctl_enqueue_rtr(io);
12220 case CTL_MSG_FINISH_IO:
12221 if (softc->ha_mode == CTL_HA_MODE_XFER) {
12225 if (targ_lun >= ctl_max_luns ||
12226 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12230 mtx_lock(&lun->lun_lock);
12231 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
12232 ctl_check_blocked(lun);
12233 mtx_unlock(&lun->lun_lock);
12236 case CTL_MSG_PERS_ACTION:
12237 ctl_hndl_per_res_out_on_other_sc(io);
12240 case CTL_MSG_BAD_JUJU:
12243 case CTL_MSG_DATAMOVE: /* Only used in XFER mode */
12244 ctl_datamove_remote(io);
12246 case CTL_MSG_DATAMOVE_DONE: /* Only used in XFER mode */
12247 io->scsiio.be_move_done(io);
12249 case CTL_MSG_FAILOVER:
12250 ctl_failover_lun(io);
12254 printf("%s: Invalid message type %d\n",
12255 __func__, io->io_hdr.msg_type);
12264 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12265 * there is no match.
12267 static ctl_lun_error_pattern
12268 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12270 const struct ctl_cmd_entry *entry;
12271 ctl_lun_error_pattern filtered_pattern, pattern;
12273 pattern = desc->error_pattern;
12276 * XXX KDM we need more data passed into this function to match a
12277 * custom pattern, and we actually need to implement custom pattern
12280 if (pattern & CTL_LUN_PAT_CMD)
12281 return (CTL_LUN_PAT_CMD);
12283 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12284 return (CTL_LUN_PAT_ANY);
12286 entry = ctl_get_cmd_entry(ctsio, NULL);
12288 filtered_pattern = entry->pattern & pattern;
12291 * If the user requested specific flags in the pattern (e.g.
12292 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12295 * If the user did not specify any flags, it doesn't matter whether
12296 * or not the command supports the flags.
12298 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12299 (pattern & ~CTL_LUN_PAT_MASK))
12300 return (CTL_LUN_PAT_NONE);
12303 * If the user asked for a range check, see if the requested LBA
12304 * range overlaps with this command's LBA range.
12306 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12312 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12314 return (CTL_LUN_PAT_NONE);
12316 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12317 desc->lba_range.len, FALSE);
12319 * A "pass" means that the LBA ranges don't overlap, so
12320 * this doesn't match the user's range criteria.
12322 if (action == CTL_ACTION_PASS)
12323 return (CTL_LUN_PAT_NONE);
12326 return (filtered_pattern);
12330 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12332 struct ctl_error_desc *desc, *desc2;
12334 mtx_assert(&lun->lun_lock, MA_OWNED);
12336 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12337 ctl_lun_error_pattern pattern;
12339 * Check to see whether this particular command matches
12340 * the pattern in the descriptor.
12342 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12343 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12346 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12347 case CTL_LUN_INJ_ABORTED:
12348 ctl_set_aborted(&io->scsiio);
12350 case CTL_LUN_INJ_MEDIUM_ERR:
12351 ctl_set_medium_error(&io->scsiio,
12352 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) !=
12353 CTL_FLAG_DATA_OUT);
12355 case CTL_LUN_INJ_UA:
12356 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12358 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12360 case CTL_LUN_INJ_CUSTOM:
12362 * We're assuming the user knows what he is doing.
12363 * Just copy the sense information without doing
12366 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12367 MIN(sizeof(desc->custom_sense),
12368 sizeof(io->scsiio.sense_data)));
12369 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12370 io->scsiio.sense_len = SSD_FULL_SIZE;
12371 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12373 case CTL_LUN_INJ_NONE:
12376 * If this is an error injection type we don't know
12377 * about, clear the continuous flag (if it is set)
12378 * so it will get deleted below.
12380 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12384 * By default, each error injection action is a one-shot
12386 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12389 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12395 #ifdef CTL_IO_DELAY
12397 ctl_datamove_timer_wakeup(void *arg)
12401 io = (union ctl_io *)arg;
12405 #endif /* CTL_IO_DELAY */
12408 ctl_datamove(union ctl_io *io)
12410 void (*fe_datamove)(union ctl_io *io);
12412 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
12414 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12416 /* No data transferred yet. Frontend must update this when done. */
12417 io->scsiio.kern_data_resid = io->scsiio.kern_data_len;
12420 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12425 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12426 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12428 sbuf_cat(&sb, path_str);
12429 switch (io->io_hdr.io_type) {
12431 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12432 sbuf_printf(&sb, "\n");
12433 sbuf_cat(&sb, path_str);
12434 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12435 io->scsiio.tag_num, io->scsiio.tag_type);
12438 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12439 "Tag Type: %d\n", io->taskio.task_action,
12440 io->taskio.tag_num, io->taskio.tag_type);
12443 panic("%s: Invalid CTL I/O type %d\n",
12444 __func__, io->io_hdr.io_type);
12446 sbuf_cat(&sb, path_str);
12447 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12448 (intmax_t)time_uptime - io->io_hdr.start_time);
12450 printf("%s", sbuf_data(&sb));
12452 #endif /* CTL_TIME_IO */
12454 #ifdef CTL_IO_DELAY
12455 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12456 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12458 struct ctl_lun *lun;
12462 && (lun->delay_info.datamove_delay > 0)) {
12464 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
12465 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12466 callout_reset(&io->io_hdr.delay_callout,
12467 lun->delay_info.datamove_delay * hz,
12468 ctl_datamove_timer_wakeup, io);
12469 if (lun->delay_info.datamove_type ==
12470 CTL_DELAY_TYPE_ONESHOT)
12471 lun->delay_info.datamove_delay = 0;
12478 * This command has been aborted. Set the port status, so we fail
12481 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12482 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n",
12483 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12484 io->io_hdr.nexus.targ_port,
12485 io->io_hdr.nexus.targ_lun);
12486 io->io_hdr.port_status = 31337;
12488 * Note that the backend, in this case, will get the
12489 * callback in its context. In other cases it may get
12490 * called in the frontend's interrupt thread context.
12492 io->scsiio.be_move_done(io);
12496 /* Don't confuse frontend with zero length data move. */
12497 if (io->scsiio.kern_data_len == 0) {
12498 io->scsiio.be_move_done(io);
12502 fe_datamove = CTL_PORT(io)->fe_datamove;
12507 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12509 union ctl_ha_msg msg;
12511 struct bintime cur_bt;
12514 memset(&msg, 0, sizeof(msg));
12515 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12516 msg.hdr.original_sc = io;
12517 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12518 msg.hdr.nexus = io->io_hdr.nexus;
12519 msg.hdr.status = io->io_hdr.status;
12520 msg.scsi.kern_data_resid = io->scsiio.kern_data_resid;
12521 msg.scsi.tag_num = io->scsiio.tag_num;
12522 msg.scsi.tag_type = io->scsiio.tag_type;
12523 msg.scsi.scsi_status = io->scsiio.scsi_status;
12524 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12525 io->scsiio.sense_len);
12526 msg.scsi.sense_len = io->scsiio.sense_len;
12527 msg.scsi.port_status = io->io_hdr.port_status;
12528 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12529 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12530 ctl_failover_io(io, /*have_lock*/ have_lock);
12533 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12534 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12535 msg.scsi.sense_len, M_WAITOK);
12538 getbinuptime(&cur_bt);
12539 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
12540 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
12542 io->io_hdr.num_dmas++;
12546 * The DMA to the remote side is done, now we need to tell the other side
12547 * we're done so it can continue with its data movement.
12550 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12557 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12558 printf("%s: ISC DMA write failed with error %d", __func__,
12560 ctl_set_internal_failure(&io->scsiio,
12562 /*retry_count*/ rq->ret);
12565 ctl_dt_req_free(rq);
12567 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12568 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12569 free(io->io_hdr.remote_sglist, M_CTL);
12570 io->io_hdr.remote_sglist = NULL;
12571 io->io_hdr.local_sglist = NULL;
12574 * The data is in local and remote memory, so now we need to send
12575 * status (good or back) back to the other side.
12577 ctl_send_datamove_done(io, /*have_lock*/ 0);
12581 * We've moved the data from the host/controller into local memory. Now we
12582 * need to push it over to the remote controller's memory.
12585 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12589 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12590 ctl_datamove_remote_write_cb);
12595 ctl_datamove_remote_write(union ctl_io *io)
12598 void (*fe_datamove)(union ctl_io *io);
12601 * - Get the data from the host/HBA into local memory.
12602 * - DMA memory from the local controller to the remote controller.
12603 * - Send status back to the remote controller.
12606 retval = ctl_datamove_remote_sgl_setup(io);
12610 /* Switch the pointer over so the FETD knows what to do */
12611 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12614 * Use a custom move done callback, since we need to send completion
12615 * back to the other controller, not to the backend on this side.
12617 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12619 fe_datamove = CTL_PORT(io)->fe_datamove;
12624 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12633 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12634 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12635 free(io->io_hdr.remote_sglist, M_CTL);
12636 io->io_hdr.remote_sglist = NULL;
12637 io->io_hdr.local_sglist = NULL;
12640 scsi_path_string(io, path_str, sizeof(path_str));
12641 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12642 sbuf_cat(&sb, path_str);
12643 scsi_command_string(&io->scsiio, NULL, &sb);
12644 sbuf_printf(&sb, "\n");
12645 sbuf_cat(&sb, path_str);
12646 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12647 io->scsiio.tag_num, io->scsiio.tag_type);
12648 sbuf_cat(&sb, path_str);
12649 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12650 io->io_hdr.flags, io->io_hdr.status);
12652 printk("%s", sbuf_data(&sb));
12657 * The read is done, now we need to send status (good or bad) back
12658 * to the other side.
12660 ctl_send_datamove_done(io, /*have_lock*/ 0);
12666 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12669 void (*fe_datamove)(union ctl_io *io);
12673 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12674 printf("%s: ISC DMA read failed with error %d\n", __func__,
12676 ctl_set_internal_failure(&io->scsiio,
12678 /*retry_count*/ rq->ret);
12681 ctl_dt_req_free(rq);
12683 /* Switch the pointer over so the FETD knows what to do */
12684 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12687 * Use a custom move done callback, since we need to send completion
12688 * back to the other controller, not to the backend on this side.
12690 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12692 /* XXX KDM add checks like the ones in ctl_datamove? */
12694 fe_datamove = CTL_PORT(io)->fe_datamove;
12699 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12701 struct ctl_sg_entry *local_sglist;
12702 uint32_t len_to_go;
12707 local_sglist = io->io_hdr.local_sglist;
12708 len_to_go = io->scsiio.kern_data_len;
12711 * The difficult thing here is that the size of the various
12712 * S/G segments may be different than the size from the
12713 * remote controller. That'll make it harder when DMAing
12714 * the data back to the other side.
12716 for (i = 0; len_to_go > 0; i++) {
12717 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT);
12718 local_sglist[i].addr =
12719 malloc(local_sglist[i].len, M_CTL, M_WAITOK);
12721 len_to_go -= local_sglist[i].len;
12724 * Reset the number of S/G entries accordingly. The original
12725 * number of S/G entries is available in rem_sg_entries.
12727 io->scsiio.kern_sg_entries = i;
12730 printf("%s: kern_sg_entries = %d\n", __func__,
12731 io->scsiio.kern_sg_entries);
12732 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12733 printf("%s: sg[%d] = %p, %lu\n", __func__, i,
12734 local_sglist[i].addr, local_sglist[i].len);
12741 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12742 ctl_ha_dt_cb callback)
12744 struct ctl_ha_dt_req *rq;
12745 struct ctl_sg_entry *remote_sglist, *local_sglist;
12746 uint32_t local_used, remote_used, total_used;
12749 rq = ctl_dt_req_alloc();
12752 * If we failed to allocate the request, and if the DMA didn't fail
12753 * anyway, set busy status. This is just a resource allocation
12757 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12758 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS))
12759 ctl_set_busy(&io->scsiio);
12761 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12762 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
12765 ctl_dt_req_free(rq);
12768 * The data move failed. We need to return status back
12769 * to the other controller. No point in trying to DMA
12770 * data to the remote controller.
12773 ctl_send_datamove_done(io, /*have_lock*/ 0);
12778 local_sglist = io->io_hdr.local_sglist;
12779 remote_sglist = io->io_hdr.remote_sglist;
12785 * Pull/push the data over the wire from/to the other controller.
12786 * This takes into account the possibility that the local and
12787 * remote sglists may not be identical in terms of the size of
12788 * the elements and the number of elements.
12790 * One fundamental assumption here is that the length allocated for
12791 * both the local and remote sglists is identical. Otherwise, we've
12792 * essentially got a coding error of some sort.
12794 isc_ret = CTL_HA_STATUS_SUCCESS;
12795 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12799 rq->command = command;
12803 * Both pointers should be aligned. But it is possible
12804 * that the allocation length is not. They should both
12805 * also have enough slack left over at the end, though,
12806 * to round up to the next 8 byte boundary.
12808 cur_len = MIN(local_sglist[i].len - local_used,
12809 remote_sglist[j].len - remote_used);
12810 rq->size = cur_len;
12812 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12813 tmp_ptr += local_used;
12816 /* Use physical addresses when talking to ISC hardware */
12817 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12818 /* XXX KDM use busdma */
12819 rq->local = vtophys(tmp_ptr);
12821 rq->local = tmp_ptr;
12823 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
12824 ("HA does not support BUS_ADDR"));
12825 rq->local = tmp_ptr;
12828 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12829 tmp_ptr += remote_used;
12830 rq->remote = tmp_ptr;
12832 rq->callback = NULL;
12834 local_used += cur_len;
12835 if (local_used >= local_sglist[i].len) {
12840 remote_used += cur_len;
12841 if (remote_used >= remote_sglist[j].len) {
12845 total_used += cur_len;
12847 if (total_used >= io->scsiio.kern_data_len)
12848 rq->callback = callback;
12851 printf("%s: %s: local %p remote %p size %d\n", __func__,
12852 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
12853 rq->local, rq->remote, rq->size);
12856 isc_ret = ctl_dt_single(rq);
12857 if (isc_ret > CTL_HA_STATUS_SUCCESS)
12860 if (isc_ret != CTL_HA_STATUS_WAIT) {
12869 ctl_datamove_remote_read(union ctl_io *io)
12875 * This will send an error to the other controller in the case of a
12878 retval = ctl_datamove_remote_sgl_setup(io);
12882 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12883 ctl_datamove_remote_read_cb);
12886 * Make sure we free memory if there was an error.. The
12887 * ctl_datamove_remote_xfer() function will send the
12888 * datamove done message, or call the callback with an
12889 * error if there is a problem.
12891 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12892 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12893 free(io->io_hdr.remote_sglist, M_CTL);
12894 io->io_hdr.remote_sglist = NULL;
12895 io->io_hdr.local_sglist = NULL;
12900 * Process a datamove request from the other controller. This is used for
12901 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
12902 * first. Once that is complete, the data gets DMAed into the remote
12903 * controller's memory. For reads, we DMA from the remote controller's
12904 * memory into our memory first, and then move it out to the FETD.
12907 ctl_datamove_remote(union ctl_io *io)
12910 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
12912 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12913 ctl_failover_io(io, /*have_lock*/ 0);
12918 * Note that we look for an aborted I/O here, but don't do some of
12919 * the other checks that ctl_datamove() normally does.
12920 * We don't need to run the datamove delay code, since that should
12921 * have been done if need be on the other controller.
12923 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12924 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__,
12925 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12926 io->io_hdr.nexus.targ_port,
12927 io->io_hdr.nexus.targ_lun);
12928 io->io_hdr.port_status = 31338;
12929 ctl_send_datamove_done(io, /*have_lock*/ 0);
12933 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
12934 ctl_datamove_remote_write(io);
12935 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
12936 ctl_datamove_remote_read(io);
12938 io->io_hdr.port_status = 31339;
12939 ctl_send_datamove_done(io, /*have_lock*/ 0);
12944 ctl_process_done(union ctl_io *io)
12946 struct ctl_softc *softc = CTL_SOFTC(io);
12947 struct ctl_port *port = CTL_PORT(io);
12948 struct ctl_lun *lun = CTL_LUN(io);
12949 void (*fe_done)(union ctl_io *io);
12950 union ctl_ha_msg msg;
12952 CTL_DEBUG_PRINT(("ctl_process_done\n"));
12953 fe_done = port->fe_done;
12956 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12961 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12962 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12964 sbuf_cat(&sb, path_str);
12965 switch (io->io_hdr.io_type) {
12967 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12968 sbuf_printf(&sb, "\n");
12969 sbuf_cat(&sb, path_str);
12970 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12971 io->scsiio.tag_num, io->scsiio.tag_type);
12974 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12975 "Tag Type: %d\n", io->taskio.task_action,
12976 io->taskio.tag_num, io->taskio.tag_type);
12979 panic("%s: Invalid CTL I/O type %d\n",
12980 __func__, io->io_hdr.io_type);
12982 sbuf_cat(&sb, path_str);
12983 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
12984 (intmax_t)time_uptime - io->io_hdr.start_time);
12986 printf("%s", sbuf_data(&sb));
12988 #endif /* CTL_TIME_IO */
12990 switch (io->io_hdr.io_type) {
12994 if (ctl_debug & CTL_DEBUG_INFO)
12995 ctl_io_error_print(io, NULL);
12999 panic("%s: Invalid CTL I/O type %d\n",
13000 __func__, io->io_hdr.io_type);
13004 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13005 io->io_hdr.nexus.targ_mapped_lun));
13009 mtx_lock(&lun->lun_lock);
13012 * Check to see if we have any informational exception and status
13013 * of this command can be modified to report it in form of either
13014 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field.
13016 if (lun->ie_reported == 0 && lun->ie_asc != 0 &&
13017 io->io_hdr.status == CTL_SUCCESS &&
13018 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) {
13019 uint8_t mrie = lun->MODE_IE.mrie;
13020 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) ||
13021 (lun->MODE_VER.byte3 & SMS_VER_PER));
13022 if (((mrie == SIEP_MRIE_REC_COND && per) ||
13023 mrie == SIEP_MRIE_REC_UNCOND ||
13024 mrie == SIEP_MRIE_NO_SENSE) &&
13025 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags &
13026 CTL_CMD_FLAG_NO_SENSE) == 0) {
13027 ctl_set_sense(&io->scsiio,
13028 /*current_error*/ 1,
13029 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ?
13030 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR,
13031 /*asc*/ lun->ie_asc,
13032 /*ascq*/ lun->ie_ascq,
13034 lun->ie_reported = 1;
13036 } else if (lun->ie_reported < 0)
13037 lun->ie_reported = 0;
13040 * Check to see if we have any errors to inject here. We only
13041 * inject errors for commands that don't already have errors set.
13043 if (!STAILQ_EMPTY(&lun->error_list) &&
13044 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
13045 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
13046 ctl_inject_error(lun, io);
13049 * XXX KDM how do we treat commands that aren't completed
13052 * XXX KDM should we also track I/O latency?
13054 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13055 io->io_hdr.io_type == CTL_IO_SCSI) {
13061 bintime_sub(&bt, &io->io_hdr.start_bt);
13063 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13065 type = CTL_STATS_READ;
13066 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13068 type = CTL_STATS_WRITE;
13070 type = CTL_STATS_NO_IO;
13072 #ifdef CTL_LEGACY_STATS
13073 uint32_t targ_port = port->targ_port;
13074 lun->legacy_stats.ports[targ_port].bytes[type] +=
13075 io->scsiio.kern_total_len;
13076 lun->legacy_stats.ports[targ_port].operations[type] ++;
13077 lun->legacy_stats.ports[targ_port].num_dmas[type] +=
13078 io->io_hdr.num_dmas;
13080 bintime_add(&lun->legacy_stats.ports[targ_port].dma_time[type],
13081 &io->io_hdr.dma_bt);
13082 bintime_add(&lun->legacy_stats.ports[targ_port].time[type],
13085 #endif /* CTL_LEGACY_STATS */
13087 lun->stats.bytes[type] += io->scsiio.kern_total_len;
13088 lun->stats.operations[type] ++;
13089 lun->stats.dmas[type] += io->io_hdr.num_dmas;
13091 bintime_add(&lun->stats.dma_time[type], &io->io_hdr.dma_bt);
13092 bintime_add(&lun->stats.time[type], &bt);
13095 mtx_lock(&port->port_lock);
13096 port->stats.bytes[type] += io->scsiio.kern_total_len;
13097 port->stats.operations[type] ++;
13098 port->stats.dmas[type] += io->io_hdr.num_dmas;
13100 bintime_add(&port->stats.dma_time[type], &io->io_hdr.dma_bt);
13101 bintime_add(&port->stats.time[type], &bt);
13103 mtx_unlock(&port->port_lock);
13107 * Remove this from the OOA queue.
13109 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13111 if (TAILQ_EMPTY(&lun->ooa_queue))
13112 lun->last_busy = getsbinuptime();
13116 * Run through the blocked queue on this LUN and see if anything
13117 * has become unblocked, now that this transaction is done.
13119 ctl_check_blocked(lun);
13122 * If the LUN has been invalidated, free it if there is nothing
13123 * left on its OOA queue.
13125 if ((lun->flags & CTL_LUN_INVALID)
13126 && TAILQ_EMPTY(&lun->ooa_queue)) {
13127 mtx_unlock(&lun->lun_lock);
13130 mtx_unlock(&lun->lun_lock);
13135 * If this command has been aborted, make sure we set the status
13136 * properly. The FETD is responsible for freeing the I/O and doing
13137 * whatever it needs to do to clean up its state.
13139 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13140 ctl_set_task_aborted(&io->scsiio);
13143 * If enabled, print command error status.
13145 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
13146 (ctl_debug & CTL_DEBUG_INFO) != 0)
13147 ctl_io_error_print(io, NULL);
13150 * Tell the FETD or the other shelf controller we're done with this
13151 * command. Note that only SCSI commands get to this point. Task
13152 * management commands are completed above.
13154 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
13155 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) {
13156 memset(&msg, 0, sizeof(msg));
13157 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13158 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
13159 msg.hdr.nexus = io->io_hdr.nexus;
13160 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13161 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data),
13169 * Front end should call this if it doesn't do autosense. When the request
13170 * sense comes back in from the initiator, we'll dequeue this and send it.
13173 ctl_queue_sense(union ctl_io *io)
13175 struct ctl_softc *softc = CTL_SOFTC(io);
13176 struct ctl_port *port = CTL_PORT(io);
13177 struct ctl_lun *lun;
13178 struct scsi_sense_data *ps;
13179 uint32_t initidx, p, targ_lun;
13181 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13183 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13186 * LUN lookup will likely move to the ctl_work_thread() once we
13187 * have our new queueing infrastructure (that doesn't put things on
13188 * a per-LUN queue initially). That is so that we can handle
13189 * things like an INQUIRY to a LUN that we don't have enabled. We
13190 * can't deal with that right now.
13191 * If we don't have a LUN for this, just toss the sense information.
13193 mtx_lock(&softc->ctl_lock);
13194 if (targ_lun >= ctl_max_luns ||
13195 (lun = softc->ctl_luns[targ_lun]) == NULL) {
13196 mtx_unlock(&softc->ctl_lock);
13199 mtx_lock(&lun->lun_lock);
13200 mtx_unlock(&softc->ctl_lock);
13202 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13203 p = initidx / CTL_MAX_INIT_PER_PORT;
13204 if (lun->pending_sense[p] == NULL) {
13205 lun->pending_sense[p] = malloc(sizeof(*ps) * CTL_MAX_INIT_PER_PORT,
13206 M_CTL, M_NOWAIT | M_ZERO);
13208 if ((ps = lun->pending_sense[p]) != NULL) {
13209 ps += initidx % CTL_MAX_INIT_PER_PORT;
13210 memset(ps, 0, sizeof(*ps));
13211 memcpy(ps, &io->scsiio.sense_data, io->scsiio.sense_len);
13213 mtx_unlock(&lun->lun_lock);
13217 return (CTL_RETVAL_COMPLETE);
13221 * Primary command inlet from frontend ports. All SCSI and task I/O
13222 * requests must go through this function.
13225 ctl_queue(union ctl_io *io)
13227 struct ctl_port *port = CTL_PORT(io);
13229 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13232 io->io_hdr.start_time = time_uptime;
13233 getbinuptime(&io->io_hdr.start_bt);
13234 #endif /* CTL_TIME_IO */
13236 /* Map FE-specific LUN ID into global one. */
13237 io->io_hdr.nexus.targ_mapped_lun =
13238 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13240 switch (io->io_hdr.io_type) {
13243 if (ctl_debug & CTL_DEBUG_CDB)
13245 ctl_enqueue_incoming(io);
13248 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13252 return (CTL_RETVAL_COMPLETE);
13255 #ifdef CTL_IO_DELAY
13257 ctl_done_timer_wakeup(void *arg)
13261 io = (union ctl_io *)arg;
13264 #endif /* CTL_IO_DELAY */
13267 ctl_serseq_done(union ctl_io *io)
13269 struct ctl_lun *lun = CTL_LUN(io);;
13271 if (lun->be_lun == NULL ||
13272 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF)
13274 mtx_lock(&lun->lun_lock);
13275 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE;
13276 ctl_check_blocked(lun);
13277 mtx_unlock(&lun->lun_lock);
13281 ctl_done(union ctl_io *io)
13285 * Enable this to catch duplicate completion issues.
13288 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13289 printf("%s: type %d msg %d cdb %x iptl: "
13290 "%u:%u:%u tag 0x%04x "
13291 "flag %#x status %x\n",
13293 io->io_hdr.io_type,
13294 io->io_hdr.msg_type,
13296 io->io_hdr.nexus.initid,
13297 io->io_hdr.nexus.targ_port,
13298 io->io_hdr.nexus.targ_lun,
13299 (io->io_hdr.io_type ==
13301 io->taskio.tag_num :
13302 io->scsiio.tag_num,
13304 io->io_hdr.status);
13306 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13310 * This is an internal copy of an I/O, and should not go through
13311 * the normal done processing logic.
13313 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13316 #ifdef CTL_IO_DELAY
13317 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13318 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13320 struct ctl_lun *lun = CTL_LUN(io);
13323 && (lun->delay_info.done_delay > 0)) {
13325 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
13326 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13327 callout_reset(&io->io_hdr.delay_callout,
13328 lun->delay_info.done_delay * hz,
13329 ctl_done_timer_wakeup, io);
13330 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13331 lun->delay_info.done_delay = 0;
13335 #endif /* CTL_IO_DELAY */
13337 ctl_enqueue_done(io);
13341 ctl_work_thread(void *arg)
13343 struct ctl_thread *thr = (struct ctl_thread *)arg;
13344 struct ctl_softc *softc = thr->ctl_softc;
13348 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13350 while (!softc->shutdown) {
13352 * We handle the queues in this order:
13354 * - done queue (to free up resources, unblock other commands)
13358 * If those queues are empty, we break out of the loop and
13361 mtx_lock(&thr->queue_lock);
13362 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13364 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13365 mtx_unlock(&thr->queue_lock);
13366 ctl_handle_isc(io);
13369 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13371 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13372 /* clear any blocked commands, call fe_done */
13373 mtx_unlock(&thr->queue_lock);
13374 ctl_process_done(io);
13377 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13379 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13380 mtx_unlock(&thr->queue_lock);
13381 if (io->io_hdr.io_type == CTL_IO_TASK)
13384 ctl_scsiio_precheck(softc, &io->scsiio);
13387 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13389 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13390 mtx_unlock(&thr->queue_lock);
13391 retval = ctl_scsiio(&io->scsiio);
13392 if (retval != CTL_RETVAL_COMPLETE)
13393 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13397 /* Sleep until we have something to do. */
13398 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13400 thr->thread = NULL;
13405 ctl_lun_thread(void *arg)
13407 struct ctl_softc *softc = (struct ctl_softc *)arg;
13408 struct ctl_be_lun *be_lun;
13410 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13412 while (!softc->shutdown) {
13413 mtx_lock(&softc->ctl_lock);
13414 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13415 if (be_lun != NULL) {
13416 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13417 mtx_unlock(&softc->ctl_lock);
13418 ctl_create_lun(be_lun);
13422 /* Sleep until we have something to do. */
13423 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13424 PDROP | PRIBIO, "-", 0);
13426 softc->lun_thread = NULL;
13431 ctl_thresh_thread(void *arg)
13433 struct ctl_softc *softc = (struct ctl_softc *)arg;
13434 struct ctl_lun *lun;
13435 struct ctl_logical_block_provisioning_page *page;
13437 union ctl_ha_msg msg;
13438 uint64_t thres, val;
13441 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13443 while (!softc->shutdown) {
13444 mtx_lock(&softc->ctl_lock);
13445 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13446 if ((lun->flags & CTL_LUN_DISABLED) ||
13447 (lun->flags & CTL_LUN_NO_MEDIA) ||
13448 lun->backend->lun_attr == NULL)
13450 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
13451 softc->ha_mode == CTL_HA_MODE_XFER)
13453 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0)
13456 page = &lun->MODE_LBP;
13457 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13458 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13460 thres = scsi_4btoul(page->descr[i].count);
13461 thres <<= CTL_LBP_EXPONENT;
13462 switch (page->descr[i].resource) {
13464 attr = "blocksavail";
13467 attr = "blocksused";
13470 attr = "poolblocksavail";
13473 attr = "poolblocksused";
13478 mtx_unlock(&softc->ctl_lock); // XXX
13479 val = lun->backend->lun_attr(
13480 lun->be_lun->be_lun, attr);
13481 mtx_lock(&softc->ctl_lock);
13482 if (val == UINT64_MAX)
13484 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13485 == SLBPPD_ARMING_INC)
13486 e = (val >= thres);
13488 e = (val <= thres);
13492 mtx_lock(&lun->lun_lock);
13494 scsi_u64to8b((uint8_t *)&page->descr[i] -
13495 (uint8_t *)page, lun->ua_tpt_info);
13496 if (lun->lasttpt == 0 ||
13497 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13498 lun->lasttpt = time_uptime;
13499 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13505 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13508 mtx_unlock(&lun->lun_lock);
13510 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
13511 /* Send msg to other side. */
13512 bzero(&msg.ua, sizeof(msg.ua));
13513 msg.hdr.msg_type = CTL_MSG_UA;
13514 msg.hdr.nexus.initid = -1;
13515 msg.hdr.nexus.targ_port = -1;
13516 msg.hdr.nexus.targ_lun = lun->lun;
13517 msg.hdr.nexus.targ_mapped_lun = lun->lun;
13519 msg.ua.ua_set = (set > 0);
13520 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES;
13521 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8);
13522 mtx_unlock(&softc->ctl_lock); // XXX
13523 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13524 sizeof(msg.ua), M_WAITOK);
13525 mtx_lock(&softc->ctl_lock);
13528 mtx_sleep(&softc->thresh_thread, &softc->ctl_lock,
13529 PDROP | PRIBIO, "-", CTL_LBP_PERIOD * hz);
13531 softc->thresh_thread = NULL;
13536 ctl_enqueue_incoming(union ctl_io *io)
13538 struct ctl_softc *softc = CTL_SOFTC(io);
13539 struct ctl_thread *thr;
13542 idx = (io->io_hdr.nexus.targ_port * 127 +
13543 io->io_hdr.nexus.initid) % worker_threads;
13544 thr = &softc->threads[idx];
13545 mtx_lock(&thr->queue_lock);
13546 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13547 mtx_unlock(&thr->queue_lock);
13552 ctl_enqueue_rtr(union ctl_io *io)
13554 struct ctl_softc *softc = CTL_SOFTC(io);
13555 struct ctl_thread *thr;
13557 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13558 mtx_lock(&thr->queue_lock);
13559 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13560 mtx_unlock(&thr->queue_lock);
13565 ctl_enqueue_done(union ctl_io *io)
13567 struct ctl_softc *softc = CTL_SOFTC(io);
13568 struct ctl_thread *thr;
13570 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13571 mtx_lock(&thr->queue_lock);
13572 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13573 mtx_unlock(&thr->queue_lock);
13578 ctl_enqueue_isc(union ctl_io *io)
13580 struct ctl_softc *softc = CTL_SOFTC(io);
13581 struct ctl_thread *thr;
13583 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13584 mtx_lock(&thr->queue_lock);
13585 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13586 mtx_unlock(&thr->queue_lock);