2 * Copyright (c) 2003-2009 Silicon Graphics International Corp.
3 * Copyright (c) 2012 The FreeBSD Foundation
4 * Copyright (c) 2014-2017 Alexander Motin <mav@FreeBSD.org>
7 * Portions of this software were developed by Edward Tomasz Napierala
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions, and the following disclaimer,
15 * without modification.
16 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17 * substantially similar to the "NO WARRANTY" disclaimer below
18 * ("Disclaimer") and any redistribution must be conditioned upon
19 * including a substantially similar Disclaimer requirement for further
20 * binary redistribution.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGES.
38 * CAM Target Layer, a SCSI device emulation subsystem.
40 * 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 TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads);
406 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
407 &worker_threads, 1, "Number of worker threads");
408 static int ctl_debug = CTL_DEBUG_NONE;
409 TUNABLE_INT("kern.cam.ctl.debug", &ctl_debug);
410 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN,
411 &ctl_debug, 0, "Enabled debug flags");
412 static int ctl_lun_map_size = 1024;
413 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, lun_map_size, CTLFLAG_RWTUN,
414 &ctl_lun_map_size, 0, "Size of per-port LUN map (max LUN + 1)");
417 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
418 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
419 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0),
420 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
422 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 10
424 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
426 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
427 static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest);
428 static int ctl_init(void);
429 void ctl_shutdown(void);
430 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
431 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
432 static void ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
433 static void ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
434 struct ctl_ooa *ooa_hdr,
435 struct ctl_ooa_entry *kern_entries);
436 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
438 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
439 struct ctl_be_lun *be_lun);
440 static int ctl_free_lun(struct ctl_lun *lun);
441 static void ctl_create_lun(struct ctl_be_lun *be_lun);
443 static int ctl_do_mode_select(union ctl_io *io);
444 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
445 uint64_t res_key, uint64_t sa_res_key,
446 uint8_t type, uint32_t residx,
447 struct ctl_scsiio *ctsio,
448 struct scsi_per_res_out *cdb,
449 struct scsi_per_res_out_parms* param);
450 static void ctl_pro_preempt_other(struct ctl_lun *lun,
451 union ctl_ha_msg *msg);
452 static void ctl_hndl_per_res_out_on_other_sc(union ctl_io *io);
453 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
454 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
455 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
456 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
457 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
458 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
460 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
462 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
463 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
464 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
465 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
466 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
467 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2,
469 static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2);
470 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
471 union ctl_io *pending_io, union ctl_io *ooa_io);
472 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
473 union ctl_io *starting_io);
474 static int ctl_check_blocked(struct ctl_lun *lun);
475 static int ctl_scsiio_lun_check(struct ctl_lun *lun,
476 const struct ctl_cmd_entry *entry,
477 struct ctl_scsiio *ctsio);
478 static void ctl_failover_lun(union ctl_io *io);
479 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
480 struct ctl_scsiio *ctsio);
481 static int ctl_scsiio(struct ctl_scsiio *ctsio);
483 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
484 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
485 ctl_ua_type ua_type);
486 static int ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io,
487 ctl_ua_type ua_type);
488 static int ctl_lun_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
489 static int ctl_abort_task(union ctl_io *io);
490 static int ctl_abort_task_set(union ctl_io *io);
491 static int ctl_query_task(union ctl_io *io, int task_set);
492 static int ctl_i_t_nexus_reset(union ctl_io *io);
493 static int ctl_query_async_event(union ctl_io *io);
494 static void ctl_run_task(union ctl_io *io);
496 static void ctl_datamove_timer_wakeup(void *arg);
497 static void ctl_done_timer_wakeup(void *arg);
498 #endif /* CTL_IO_DELAY */
500 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
501 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
502 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
503 static void ctl_datamove_remote_write(union ctl_io *io);
504 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
505 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
506 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
507 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
508 ctl_ha_dt_cb callback);
509 static void ctl_datamove_remote_read(union ctl_io *io);
510 static void ctl_datamove_remote(union ctl_io *io);
511 static void ctl_process_done(union ctl_io *io);
512 static void ctl_lun_thread(void *arg);
513 static void ctl_thresh_thread(void *arg);
514 static void ctl_work_thread(void *arg);
515 static void ctl_enqueue_incoming(union ctl_io *io);
516 static void ctl_enqueue_rtr(union ctl_io *io);
517 static void ctl_enqueue_done(union ctl_io *io);
518 static void ctl_enqueue_isc(union ctl_io *io);
519 static const struct ctl_cmd_entry *
520 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
521 static const struct ctl_cmd_entry *
522 ctl_validate_command(struct ctl_scsiio *ctsio);
523 static int ctl_cmd_applicable(uint8_t lun_type,
524 const struct ctl_cmd_entry *entry);
526 static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx);
527 static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx);
528 static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx);
529 static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key);
532 * Load the serialization table. This isn't very pretty, but is probably
533 * the easiest way to do it.
535 #include "ctl_ser_table.c"
538 * We only need to define open, close and ioctl routines for this driver.
540 static struct cdevsw ctl_cdevsw = {
541 .d_version = D_VERSION,
544 .d_close = ctl_close,
545 .d_ioctl = ctl_ioctl,
550 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
552 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
554 static moduledata_t ctl_moduledata = {
556 ctl_module_event_handler,
560 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
561 MODULE_VERSION(ctl, 1);
563 static struct ctl_frontend ha_frontend =
569 ctl_ha_datamove(union ctl_io *io)
571 struct ctl_lun *lun = CTL_LUN(io);
572 struct ctl_sg_entry *sgl;
573 union ctl_ha_msg msg;
574 uint32_t sg_entries_sent;
575 int do_sg_copy, i, j;
577 memset(&msg.dt, 0, sizeof(msg.dt));
578 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
579 msg.hdr.original_sc = io->io_hdr.original_sc;
580 msg.hdr.serializing_sc = io;
581 msg.hdr.nexus = io->io_hdr.nexus;
582 msg.hdr.status = io->io_hdr.status;
583 msg.dt.flags = io->io_hdr.flags;
586 * We convert everything into a S/G list here. We can't
587 * pass by reference, only by value between controllers.
588 * So we can't pass a pointer to the S/G list, only as many
589 * S/G entries as we can fit in here. If it's possible for
590 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
591 * then we need to break this up into multiple transfers.
593 if (io->scsiio.kern_sg_entries == 0) {
594 msg.dt.kern_sg_entries = 1;
596 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
597 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
599 /* XXX KDM use busdma here! */
600 msg.dt.sg_list[0].addr =
601 (void *)vtophys(io->scsiio.kern_data_ptr);
604 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
605 ("HA does not support BUS_ADDR"));
606 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
608 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
611 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
615 msg.dt.kern_data_len = io->scsiio.kern_data_len;
616 msg.dt.kern_total_len = io->scsiio.kern_total_len;
617 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
618 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
619 msg.dt.sg_sequence = 0;
622 * Loop until we've sent all of the S/G entries. On the
623 * other end, we'll recompose these S/G entries into one
624 * contiguous list before processing.
626 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries;
627 msg.dt.sg_sequence++) {
628 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) /
629 sizeof(msg.dt.sg_list[0])),
630 msg.dt.kern_sg_entries - sg_entries_sent);
631 if (do_sg_copy != 0) {
632 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
633 for (i = sg_entries_sent, j = 0;
634 i < msg.dt.cur_sg_entries; i++, j++) {
636 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
637 msg.dt.sg_list[j].addr = sgl[i].addr;
639 /* XXX KDM use busdma here! */
640 msg.dt.sg_list[j].addr =
641 (void *)vtophys(sgl[i].addr);
644 KASSERT((io->io_hdr.flags &
645 CTL_FLAG_BUS_ADDR) == 0,
646 ("HA does not support BUS_ADDR"));
647 msg.dt.sg_list[j].addr = sgl[i].addr;
649 msg.dt.sg_list[j].len = sgl[i].len;
653 sg_entries_sent += msg.dt.cur_sg_entries;
654 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries);
655 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
656 sizeof(msg.dt) - sizeof(msg.dt.sg_list) +
657 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries,
658 M_WAITOK) > CTL_HA_STATUS_SUCCESS) {
659 io->io_hdr.port_status = 31341;
660 io->scsiio.be_move_done(io);
663 msg.dt.sent_sg_entries = sg_entries_sent;
667 * Officially handover the request from us to peer.
668 * If failover has just happened, then we must return error.
669 * If failover happen just after, then it is not our problem.
672 mtx_lock(&lun->lun_lock);
673 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
675 mtx_unlock(&lun->lun_lock);
676 io->io_hdr.port_status = 31342;
677 io->scsiio.be_move_done(io);
680 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
681 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
683 mtx_unlock(&lun->lun_lock);
687 ctl_ha_done(union ctl_io *io)
689 union ctl_ha_msg msg;
691 if (io->io_hdr.io_type == CTL_IO_SCSI) {
692 memset(&msg, 0, sizeof(msg));
693 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
694 msg.hdr.original_sc = io->io_hdr.original_sc;
695 msg.hdr.nexus = io->io_hdr.nexus;
696 msg.hdr.status = io->io_hdr.status;
697 msg.scsi.scsi_status = io->scsiio.scsi_status;
698 msg.scsi.tag_num = io->scsiio.tag_num;
699 msg.scsi.tag_type = io->scsiio.tag_type;
700 msg.scsi.sense_len = io->scsiio.sense_len;
701 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
702 io->scsiio.sense_len);
703 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
704 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
705 msg.scsi.sense_len, M_WAITOK);
711 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
712 union ctl_ha_msg *msg_info)
714 struct ctl_scsiio *ctsio;
716 if (msg_info->hdr.original_sc == NULL) {
717 printf("%s: original_sc == NULL!\n", __func__);
718 /* XXX KDM now what? */
722 ctsio = &msg_info->hdr.original_sc->scsiio;
723 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
724 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
725 ctsio->io_hdr.status = msg_info->hdr.status;
726 ctsio->scsi_status = msg_info->scsi.scsi_status;
727 ctsio->sense_len = msg_info->scsi.sense_len;
728 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
729 msg_info->scsi.sense_len);
730 ctl_enqueue_isc((union ctl_io *)ctsio);
734 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
735 union ctl_ha_msg *msg_info)
737 struct ctl_scsiio *ctsio;
739 if (msg_info->hdr.serializing_sc == NULL) {
740 printf("%s: serializing_sc == NULL!\n", __func__);
741 /* XXX KDM now what? */
745 ctsio = &msg_info->hdr.serializing_sc->scsiio;
746 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
747 ctl_enqueue_isc((union ctl_io *)ctsio);
751 ctl_isc_announce_lun(struct ctl_lun *lun)
753 struct ctl_softc *softc = lun->ctl_softc;
754 union ctl_ha_msg *msg;
755 struct ctl_ha_msg_lun_pr_key pr_key;
758 if (softc->ha_link != CTL_HA_LINK_ONLINE)
760 mtx_lock(&lun->lun_lock);
761 i = sizeof(msg->lun);
763 i += lun->lun_devid->len;
764 i += sizeof(pr_key) * lun->pr_key_count;
766 mtx_unlock(&lun->lun_lock);
767 msg = malloc(i, M_CTL, M_WAITOK);
768 mtx_lock(&lun->lun_lock);
769 k = sizeof(msg->lun);
771 k += lun->lun_devid->len;
772 k += sizeof(pr_key) * lun->pr_key_count;
778 bzero(&msg->lun, sizeof(msg->lun));
779 msg->hdr.msg_type = CTL_MSG_LUN_SYNC;
780 msg->hdr.nexus.targ_lun = lun->lun;
781 msg->hdr.nexus.targ_mapped_lun = lun->lun;
782 msg->lun.flags = lun->flags;
783 msg->lun.pr_generation = lun->pr_generation;
784 msg->lun.pr_res_idx = lun->pr_res_idx;
785 msg->lun.pr_res_type = lun->pr_res_type;
786 msg->lun.pr_key_count = lun->pr_key_count;
788 if (lun->lun_devid) {
789 msg->lun.lun_devid_len = lun->lun_devid->len;
790 memcpy(&msg->lun.data[i], lun->lun_devid->data,
791 msg->lun.lun_devid_len);
792 i += msg->lun.lun_devid_len;
794 for (k = 0; k < CTL_MAX_INITIATORS; k++) {
795 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0)
798 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key));
801 mtx_unlock(&lun->lun_lock);
802 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
806 if (lun->flags & CTL_LUN_PRIMARY_SC) {
807 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
808 ctl_isc_announce_mode(lun, -1,
809 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
810 lun->mode_pages.index[i].subpage);
816 ctl_isc_announce_port(struct ctl_port *port)
818 struct ctl_softc *softc = port->ctl_softc;
819 union ctl_ha_msg *msg;
822 if (port->targ_port < softc->port_min ||
823 port->targ_port >= softc->port_max ||
824 softc->ha_link != CTL_HA_LINK_ONLINE)
826 i = sizeof(msg->port) + strlen(port->port_name) + 1;
828 i += port->lun_map_size * sizeof(uint32_t);
829 if (port->port_devid)
830 i += port->port_devid->len;
831 if (port->target_devid)
832 i += port->target_devid->len;
833 if (port->init_devid)
834 i += port->init_devid->len;
835 msg = malloc(i, M_CTL, M_WAITOK);
836 bzero(&msg->port, sizeof(msg->port));
837 msg->hdr.msg_type = CTL_MSG_PORT_SYNC;
838 msg->hdr.nexus.targ_port = port->targ_port;
839 msg->port.port_type = port->port_type;
840 msg->port.physical_port = port->physical_port;
841 msg->port.virtual_port = port->virtual_port;
842 msg->port.status = port->status;
844 msg->port.name_len = sprintf(&msg->port.data[i],
845 "%d:%s", softc->ha_id, port->port_name) + 1;
846 i += msg->port.name_len;
848 msg->port.lun_map_len = port->lun_map_size * sizeof(uint32_t);
849 memcpy(&msg->port.data[i], port->lun_map,
850 msg->port.lun_map_len);
851 i += msg->port.lun_map_len;
853 if (port->port_devid) {
854 msg->port.port_devid_len = port->port_devid->len;
855 memcpy(&msg->port.data[i], port->port_devid->data,
856 msg->port.port_devid_len);
857 i += msg->port.port_devid_len;
859 if (port->target_devid) {
860 msg->port.target_devid_len = port->target_devid->len;
861 memcpy(&msg->port.data[i], port->target_devid->data,
862 msg->port.target_devid_len);
863 i += msg->port.target_devid_len;
865 if (port->init_devid) {
866 msg->port.init_devid_len = port->init_devid->len;
867 memcpy(&msg->port.data[i], port->init_devid->data,
868 msg->port.init_devid_len);
869 i += msg->port.init_devid_len;
871 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
877 ctl_isc_announce_iid(struct ctl_port *port, int iid)
879 struct ctl_softc *softc = port->ctl_softc;
880 union ctl_ha_msg *msg;
883 if (port->targ_port < softc->port_min ||
884 port->targ_port >= softc->port_max ||
885 softc->ha_link != CTL_HA_LINK_ONLINE)
887 mtx_lock(&softc->ctl_lock);
888 i = sizeof(msg->iid);
890 if (port->wwpn_iid[iid].name)
891 l = strlen(port->wwpn_iid[iid].name) + 1;
893 msg = malloc(i, M_CTL, M_NOWAIT);
895 mtx_unlock(&softc->ctl_lock);
898 bzero(&msg->iid, sizeof(msg->iid));
899 msg->hdr.msg_type = CTL_MSG_IID_SYNC;
900 msg->hdr.nexus.targ_port = port->targ_port;
901 msg->hdr.nexus.initid = iid;
902 msg->iid.in_use = port->wwpn_iid[iid].in_use;
903 msg->iid.name_len = l;
904 msg->iid.wwpn = port->wwpn_iid[iid].wwpn;
905 if (port->wwpn_iid[iid].name)
906 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l);
907 mtx_unlock(&softc->ctl_lock);
908 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT);
913 ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx,
914 uint8_t page, uint8_t subpage)
916 struct ctl_softc *softc = lun->ctl_softc;
917 union ctl_ha_msg msg;
920 if (softc->ha_link != CTL_HA_LINK_ONLINE)
922 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
923 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
924 page && lun->mode_pages.index[i].subpage == subpage)
927 if (i == CTL_NUM_MODE_PAGES)
930 /* Don't try to replicate pages not present on this device. */
931 if (lun->mode_pages.index[i].page_data == NULL)
934 bzero(&msg.mode, sizeof(msg.mode));
935 msg.hdr.msg_type = CTL_MSG_MODE_SYNC;
936 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT;
937 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT;
938 msg.hdr.nexus.targ_lun = lun->lun;
939 msg.hdr.nexus.targ_mapped_lun = lun->lun;
940 msg.mode.page_code = page;
941 msg.mode.subpage = subpage;
942 msg.mode.page_len = lun->mode_pages.index[i].page_len;
943 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data,
945 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode),
950 ctl_isc_ha_link_up(struct ctl_softc *softc)
952 struct ctl_port *port;
954 union ctl_ha_msg msg;
957 /* Announce this node parameters to peer for validation. */
958 msg.login.msg_type = CTL_MSG_LOGIN;
959 msg.login.version = CTL_HA_VERSION;
960 msg.login.ha_mode = softc->ha_mode;
961 msg.login.ha_id = softc->ha_id;
962 msg.login.max_luns = CTL_MAX_LUNS;
963 msg.login.max_ports = CTL_MAX_PORTS;
964 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT;
965 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login),
968 STAILQ_FOREACH(port, &softc->port_list, links) {
969 ctl_isc_announce_port(port);
970 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
971 if (port->wwpn_iid[i].in_use)
972 ctl_isc_announce_iid(port, i);
975 STAILQ_FOREACH(lun, &softc->lun_list, links)
976 ctl_isc_announce_lun(lun);
980 ctl_isc_ha_link_down(struct ctl_softc *softc)
982 struct ctl_port *port;
987 mtx_lock(&softc->ctl_lock);
988 STAILQ_FOREACH(lun, &softc->lun_list, links) {
989 mtx_lock(&lun->lun_lock);
990 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) {
991 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
992 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
994 mtx_unlock(&lun->lun_lock);
996 mtx_unlock(&softc->ctl_lock);
997 io = ctl_alloc_io(softc->othersc_pool);
998 mtx_lock(&softc->ctl_lock);
1000 io->io_hdr.msg_type = CTL_MSG_FAILOVER;
1001 io->io_hdr.nexus.targ_mapped_lun = lun->lun;
1002 ctl_enqueue_isc(io);
1005 STAILQ_FOREACH(port, &softc->port_list, links) {
1006 if (port->targ_port >= softc->port_min &&
1007 port->targ_port < softc->port_max)
1009 port->status &= ~CTL_PORT_STATUS_ONLINE;
1010 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1011 port->wwpn_iid[i].in_use = 0;
1012 free(port->wwpn_iid[i].name, M_CTL);
1013 port->wwpn_iid[i].name = NULL;
1016 mtx_unlock(&softc->ctl_lock);
1020 ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1022 struct ctl_lun *lun;
1023 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus);
1025 mtx_lock(&softc->ctl_lock);
1026 if (msg->hdr.nexus.targ_mapped_lun >= CTL_MAX_LUNS ||
1027 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) == NULL) {
1028 mtx_unlock(&softc->ctl_lock);
1031 mtx_lock(&lun->lun_lock);
1032 mtx_unlock(&softc->ctl_lock);
1033 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && msg->ua.ua_set)
1034 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8);
1035 if (msg->ua.ua_all) {
1037 ctl_est_ua_all(lun, iid, msg->ua.ua_type);
1039 ctl_clr_ua_all(lun, iid, msg->ua.ua_type);
1042 ctl_est_ua(lun, iid, msg->ua.ua_type);
1044 ctl_clr_ua(lun, iid, msg->ua.ua_type);
1046 mtx_unlock(&lun->lun_lock);
1050 ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1052 struct ctl_lun *lun;
1053 struct ctl_ha_msg_lun_pr_key pr_key;
1055 ctl_lun_flags oflags;
1058 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1059 mtx_lock(&softc->ctl_lock);
1060 if (targ_lun >= CTL_MAX_LUNS ||
1061 (lun = softc->ctl_luns[targ_lun]) == NULL) {
1062 mtx_unlock(&softc->ctl_lock);
1065 mtx_lock(&lun->lun_lock);
1066 mtx_unlock(&softc->ctl_lock);
1067 if (lun->flags & CTL_LUN_DISABLED) {
1068 mtx_unlock(&lun->lun_lock);
1071 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0;
1072 if (msg->lun.lun_devid_len != i || (i > 0 &&
1073 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) {
1074 mtx_unlock(&lun->lun_lock);
1075 printf("%s: Received conflicting HA LUN %d\n",
1076 __func__, targ_lun);
1079 /* Record whether peer is primary. */
1080 oflags = lun->flags;
1081 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) &&
1082 (msg->lun.flags & CTL_LUN_DISABLED) == 0)
1083 lun->flags |= CTL_LUN_PEER_SC_PRIMARY;
1085 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1086 if (oflags != lun->flags)
1087 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1089 /* If peer is primary and we are not -- use data */
1090 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
1091 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) {
1092 lun->pr_generation = msg->lun.pr_generation;
1093 lun->pr_res_idx = msg->lun.pr_res_idx;
1094 lun->pr_res_type = msg->lun.pr_res_type;
1095 lun->pr_key_count = msg->lun.pr_key_count;
1096 for (k = 0; k < CTL_MAX_INITIATORS; k++)
1097 ctl_clr_prkey(lun, k);
1098 for (k = 0; k < msg->lun.pr_key_count; k++) {
1099 memcpy(&pr_key, &msg->lun.data[i],
1101 ctl_alloc_prkey(lun, pr_key.pr_iid);
1102 ctl_set_prkey(lun, pr_key.pr_iid,
1104 i += sizeof(pr_key);
1108 mtx_unlock(&lun->lun_lock);
1109 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n",
1111 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ?
1112 "primary" : "secondary"));
1114 /* If we are primary but peer doesn't know -- notify */
1115 if ((lun->flags & CTL_LUN_PRIMARY_SC) &&
1116 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0)
1117 ctl_isc_announce_lun(lun);
1122 ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1124 struct ctl_port *port;
1125 struct ctl_lun *lun;
1128 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1130 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__,
1131 msg->hdr.nexus.targ_port));
1133 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO);
1134 port->frontend = &ha_frontend;
1135 port->targ_port = msg->hdr.nexus.targ_port;
1136 port->fe_datamove = ctl_ha_datamove;
1137 port->fe_done = ctl_ha_done;
1138 } else if (port->frontend == &ha_frontend) {
1139 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__,
1140 msg->hdr.nexus.targ_port));
1143 printf("%s: Received conflicting HA port %d\n",
1144 __func__, msg->hdr.nexus.targ_port);
1147 port->port_type = msg->port.port_type;
1148 port->physical_port = msg->port.physical_port;
1149 port->virtual_port = msg->port.virtual_port;
1150 port->status = msg->port.status;
1152 free(port->port_name, M_CTL);
1153 port->port_name = strndup(&msg->port.data[i], msg->port.name_len,
1155 i += msg->port.name_len;
1156 if (msg->port.lun_map_len != 0) {
1157 if (port->lun_map == NULL ||
1158 port->lun_map_size * sizeof(uint32_t) <
1159 msg->port.lun_map_len) {
1160 port->lun_map_size = 0;
1161 free(port->lun_map, M_CTL);
1162 port->lun_map = malloc(msg->port.lun_map_len,
1165 memcpy(port->lun_map, &msg->port.data[i], msg->port.lun_map_len);
1166 port->lun_map_size = msg->port.lun_map_len / sizeof(uint32_t);
1167 i += msg->port.lun_map_len;
1169 port->lun_map_size = 0;
1170 free(port->lun_map, M_CTL);
1171 port->lun_map = NULL;
1173 if (msg->port.port_devid_len != 0) {
1174 if (port->port_devid == NULL ||
1175 port->port_devid->len < msg->port.port_devid_len) {
1176 free(port->port_devid, M_CTL);
1177 port->port_devid = malloc(sizeof(struct ctl_devid) +
1178 msg->port.port_devid_len, M_CTL, M_WAITOK);
1180 memcpy(port->port_devid->data, &msg->port.data[i],
1181 msg->port.port_devid_len);
1182 port->port_devid->len = msg->port.port_devid_len;
1183 i += msg->port.port_devid_len;
1185 free(port->port_devid, M_CTL);
1186 port->port_devid = NULL;
1188 if (msg->port.target_devid_len != 0) {
1189 if (port->target_devid == NULL ||
1190 port->target_devid->len < msg->port.target_devid_len) {
1191 free(port->target_devid, M_CTL);
1192 port->target_devid = malloc(sizeof(struct ctl_devid) +
1193 msg->port.target_devid_len, M_CTL, M_WAITOK);
1195 memcpy(port->target_devid->data, &msg->port.data[i],
1196 msg->port.target_devid_len);
1197 port->target_devid->len = msg->port.target_devid_len;
1198 i += msg->port.target_devid_len;
1200 free(port->target_devid, M_CTL);
1201 port->target_devid = NULL;
1203 if (msg->port.init_devid_len != 0) {
1204 if (port->init_devid == NULL ||
1205 port->init_devid->len < msg->port.init_devid_len) {
1206 free(port->init_devid, M_CTL);
1207 port->init_devid = malloc(sizeof(struct ctl_devid) +
1208 msg->port.init_devid_len, M_CTL, M_WAITOK);
1210 memcpy(port->init_devid->data, &msg->port.data[i],
1211 msg->port.init_devid_len);
1212 port->init_devid->len = msg->port.init_devid_len;
1213 i += msg->port.init_devid_len;
1215 free(port->init_devid, M_CTL);
1216 port->init_devid = NULL;
1219 if (ctl_port_register(port) != 0) {
1220 printf("%s: ctl_port_register() failed with error\n",
1224 mtx_lock(&softc->ctl_lock);
1225 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1226 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
1228 mtx_lock(&lun->lun_lock);
1229 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
1230 mtx_unlock(&lun->lun_lock);
1232 mtx_unlock(&softc->ctl_lock);
1236 ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1238 struct ctl_port *port;
1241 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1243 printf("%s: Received IID for unknown port %d\n",
1244 __func__, msg->hdr.nexus.targ_port);
1247 iid = msg->hdr.nexus.initid;
1248 port->wwpn_iid[iid].in_use = msg->iid.in_use;
1249 port->wwpn_iid[iid].wwpn = msg->iid.wwpn;
1250 free(port->wwpn_iid[iid].name, M_CTL);
1251 if (msg->iid.name_len) {
1252 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0],
1253 msg->iid.name_len, M_CTL);
1255 port->wwpn_iid[iid].name = NULL;
1259 ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1262 if (msg->login.version != CTL_HA_VERSION) {
1263 printf("CTL HA peers have different versions %d != %d\n",
1264 msg->login.version, CTL_HA_VERSION);
1265 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1268 if (msg->login.ha_mode != softc->ha_mode) {
1269 printf("CTL HA peers have different ha_mode %d != %d\n",
1270 msg->login.ha_mode, softc->ha_mode);
1271 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1274 if (msg->login.ha_id == softc->ha_id) {
1275 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id);
1276 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1279 if (msg->login.max_luns != CTL_MAX_LUNS ||
1280 msg->login.max_ports != CTL_MAX_PORTS ||
1281 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) {
1282 printf("CTL HA peers have different limits\n");
1283 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1289 ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1291 struct ctl_lun *lun;
1293 uint32_t initidx, targ_lun;
1295 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1296 mtx_lock(&softc->ctl_lock);
1297 if (targ_lun >= CTL_MAX_LUNS ||
1298 (lun = softc->ctl_luns[targ_lun]) == NULL) {
1299 mtx_unlock(&softc->ctl_lock);
1302 mtx_lock(&lun->lun_lock);
1303 mtx_unlock(&softc->ctl_lock);
1304 if (lun->flags & CTL_LUN_DISABLED) {
1305 mtx_unlock(&lun->lun_lock);
1308 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
1309 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
1310 msg->mode.page_code &&
1311 lun->mode_pages.index[i].subpage == msg->mode.subpage)
1314 if (i == CTL_NUM_MODE_PAGES) {
1315 mtx_unlock(&lun->lun_lock);
1318 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data,
1319 lun->mode_pages.index[i].page_len);
1320 initidx = ctl_get_initindex(&msg->hdr.nexus);
1322 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
1323 mtx_unlock(&lun->lun_lock);
1327 * ISC (Inter Shelf Communication) event handler. Events from the HA
1328 * subsystem come in here.
1331 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
1333 struct ctl_softc *softc = control_softc;
1335 struct ctl_prio *presio;
1336 ctl_ha_status isc_status;
1338 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event));
1339 if (event == CTL_HA_EVT_MSG_RECV) {
1340 union ctl_ha_msg *msg, msgbuf;
1342 if (param > sizeof(msgbuf))
1343 msg = malloc(param, M_CTL, M_WAITOK);
1346 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param,
1348 if (isc_status != CTL_HA_STATUS_SUCCESS) {
1349 printf("%s: Error receiving message: %d\n",
1350 __func__, isc_status);
1356 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type));
1357 switch (msg->hdr.msg_type) {
1358 case CTL_MSG_SERIALIZE:
1359 io = ctl_alloc_io(softc->othersc_pool);
1361 // populate ctsio from msg
1362 io->io_hdr.io_type = CTL_IO_SCSI;
1363 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
1364 io->io_hdr.original_sc = msg->hdr.original_sc;
1365 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
1368 * If we're in serialization-only mode, we don't
1369 * want to go through full done processing. Thus
1372 * XXX KDM add another flag that is more specific.
1374 if (softc->ha_mode != CTL_HA_MODE_XFER)
1375 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
1376 io->io_hdr.nexus = msg->hdr.nexus;
1378 printf("port %u, iid %u, lun %u\n",
1379 io->io_hdr.nexus.targ_port,
1380 io->io_hdr.nexus.initid,
1381 io->io_hdr.nexus.targ_lun);
1383 io->scsiio.tag_num = msg->scsi.tag_num;
1384 io->scsiio.tag_type = msg->scsi.tag_type;
1386 io->io_hdr.start_time = time_uptime;
1387 getbinuptime(&io->io_hdr.start_bt);
1388 #endif /* CTL_TIME_IO */
1389 io->scsiio.cdb_len = msg->scsi.cdb_len;
1390 memcpy(io->scsiio.cdb, msg->scsi.cdb,
1392 if (softc->ha_mode == CTL_HA_MODE_XFER) {
1393 const struct ctl_cmd_entry *entry;
1395 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
1396 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
1398 entry->flags & CTL_FLAG_DATA_MASK;
1400 ctl_enqueue_isc(io);
1403 /* Performed on the Originating SC, XFER mode only */
1404 case CTL_MSG_DATAMOVE: {
1405 struct ctl_sg_entry *sgl;
1408 io = msg->hdr.original_sc;
1410 printf("%s: original_sc == NULL!\n", __func__);
1411 /* XXX KDM do something here */
1414 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
1415 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1417 * Keep track of this, we need to send it back over
1418 * when the datamove is complete.
1420 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1421 if (msg->hdr.status == CTL_SUCCESS)
1422 io->io_hdr.status = msg->hdr.status;
1424 if (msg->dt.sg_sequence == 0) {
1426 getbinuptime(&io->io_hdr.dma_start_bt);
1428 i = msg->dt.kern_sg_entries +
1429 msg->dt.kern_data_len /
1430 CTL_HA_DATAMOVE_SEGMENT + 1;
1431 sgl = malloc(sizeof(*sgl) * i, M_CTL,
1433 io->io_hdr.remote_sglist = sgl;
1434 io->io_hdr.local_sglist =
1435 &sgl[msg->dt.kern_sg_entries];
1437 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
1439 io->scsiio.kern_sg_entries =
1440 msg->dt.kern_sg_entries;
1441 io->scsiio.rem_sg_entries =
1442 msg->dt.kern_sg_entries;
1443 io->scsiio.kern_data_len =
1444 msg->dt.kern_data_len;
1445 io->scsiio.kern_total_len =
1446 msg->dt.kern_total_len;
1447 io->scsiio.kern_data_resid =
1448 msg->dt.kern_data_resid;
1449 io->scsiio.kern_rel_offset =
1450 msg->dt.kern_rel_offset;
1451 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR;
1452 io->io_hdr.flags |= msg->dt.flags &
1455 sgl = (struct ctl_sg_entry *)
1456 io->scsiio.kern_data_ptr;
1458 for (i = msg->dt.sent_sg_entries, j = 0;
1459 i < (msg->dt.sent_sg_entries +
1460 msg->dt.cur_sg_entries); i++, j++) {
1461 sgl[i].addr = msg->dt.sg_list[j].addr;
1462 sgl[i].len = msg->dt.sg_list[j].len;
1465 printf("%s: DATAMOVE: %p,%lu j=%d, i=%d\n",
1466 __func__, sgl[i].addr, sgl[i].len, j, i);
1471 * If this is the last piece of the I/O, we've got
1472 * the full S/G list. Queue processing in the thread.
1473 * Otherwise wait for the next piece.
1475 if (msg->dt.sg_last != 0)
1476 ctl_enqueue_isc(io);
1479 /* Performed on the Serializing (primary) SC, XFER mode only */
1480 case CTL_MSG_DATAMOVE_DONE: {
1481 if (msg->hdr.serializing_sc == NULL) {
1482 printf("%s: serializing_sc == NULL!\n",
1484 /* XXX KDM now what? */
1488 * We grab the sense information here in case
1489 * there was a failure, so we can return status
1490 * back to the initiator.
1492 io = msg->hdr.serializing_sc;
1493 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
1494 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1495 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1496 io->io_hdr.port_status = msg->scsi.port_status;
1497 io->scsiio.kern_data_resid = msg->scsi.kern_data_resid;
1498 if (msg->hdr.status != CTL_STATUS_NONE) {
1499 io->io_hdr.status = msg->hdr.status;
1500 io->scsiio.scsi_status = msg->scsi.scsi_status;
1501 io->scsiio.sense_len = msg->scsi.sense_len;
1502 memcpy(&io->scsiio.sense_data,
1503 &msg->scsi.sense_data,
1504 msg->scsi.sense_len);
1505 if (msg->hdr.status == CTL_SUCCESS)
1506 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1508 ctl_enqueue_isc(io);
1512 /* Preformed on Originating SC, SER_ONLY mode */
1514 io = msg->hdr.original_sc;
1516 printf("%s: original_sc == NULL!\n",
1520 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1521 io->io_hdr.msg_type = CTL_MSG_R2R;
1522 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1523 ctl_enqueue_isc(io);
1527 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
1529 * Performed on the Originating (i.e. secondary) SC in XFER
1532 case CTL_MSG_FINISH_IO:
1533 if (softc->ha_mode == CTL_HA_MODE_XFER)
1534 ctl_isc_handler_finish_xfer(softc, msg);
1536 ctl_isc_handler_finish_ser_only(softc, msg);
1539 /* Preformed on Originating SC */
1540 case CTL_MSG_BAD_JUJU:
1541 io = msg->hdr.original_sc;
1543 printf("%s: Bad JUJU!, original_sc is NULL!\n",
1547 ctl_copy_sense_data(msg, io);
1549 * IO should have already been cleaned up on other
1550 * SC so clear this flag so we won't send a message
1551 * back to finish the IO there.
1553 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
1554 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1556 /* io = msg->hdr.serializing_sc; */
1557 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
1558 ctl_enqueue_isc(io);
1561 /* Handle resets sent from the other side */
1562 case CTL_MSG_MANAGE_TASKS: {
1563 struct ctl_taskio *taskio;
1564 taskio = (struct ctl_taskio *)ctl_alloc_io(
1565 softc->othersc_pool);
1566 ctl_zero_io((union ctl_io *)taskio);
1567 taskio->io_hdr.io_type = CTL_IO_TASK;
1568 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1569 taskio->io_hdr.nexus = msg->hdr.nexus;
1570 taskio->task_action = msg->task.task_action;
1571 taskio->tag_num = msg->task.tag_num;
1572 taskio->tag_type = msg->task.tag_type;
1574 taskio->io_hdr.start_time = time_uptime;
1575 getbinuptime(&taskio->io_hdr.start_bt);
1576 #endif /* CTL_TIME_IO */
1577 ctl_run_task((union ctl_io *)taskio);
1580 /* Persistent Reserve action which needs attention */
1581 case CTL_MSG_PERS_ACTION:
1582 presio = (struct ctl_prio *)ctl_alloc_io(
1583 softc->othersc_pool);
1584 ctl_zero_io((union ctl_io *)presio);
1585 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
1586 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1587 presio->io_hdr.nexus = msg->hdr.nexus;
1588 presio->pr_msg = msg->pr;
1589 ctl_enqueue_isc((union ctl_io *)presio);
1592 ctl_isc_ua(softc, msg, param);
1594 case CTL_MSG_PORT_SYNC:
1595 ctl_isc_port_sync(softc, msg, param);
1597 case CTL_MSG_LUN_SYNC:
1598 ctl_isc_lun_sync(softc, msg, param);
1600 case CTL_MSG_IID_SYNC:
1601 ctl_isc_iid_sync(softc, msg, param);
1604 ctl_isc_login(softc, msg, param);
1606 case CTL_MSG_MODE_SYNC:
1607 ctl_isc_mode_sync(softc, msg, param);
1610 printf("Received HA message of unknown type %d\n",
1612 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1617 } else if (event == CTL_HA_EVT_LINK_CHANGE) {
1618 printf("CTL: HA link status changed from %d to %d\n",
1619 softc->ha_link, param);
1620 if (param == softc->ha_link)
1622 if (softc->ha_link == CTL_HA_LINK_ONLINE) {
1623 softc->ha_link = param;
1624 ctl_isc_ha_link_down(softc);
1626 softc->ha_link = param;
1627 if (softc->ha_link == CTL_HA_LINK_ONLINE)
1628 ctl_isc_ha_link_up(softc);
1632 printf("ctl_isc_event_handler: Unknown event %d\n", event);
1638 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
1641 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data,
1642 src->scsi.sense_len);
1643 dest->scsiio.scsi_status = src->scsi.scsi_status;
1644 dest->scsiio.sense_len = src->scsi.sense_len;
1645 dest->io_hdr.status = src->hdr.status;
1649 ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest)
1652 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data,
1653 src->scsiio.sense_len);
1654 dest->scsi.scsi_status = src->scsiio.scsi_status;
1655 dest->scsi.sense_len = src->scsiio.sense_len;
1656 dest->hdr.status = src->io_hdr.status;
1660 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1662 struct ctl_softc *softc = lun->ctl_softc;
1665 if (initidx < softc->init_min || initidx >= softc->init_max)
1667 mtx_assert(&lun->lun_lock, MA_OWNED);
1668 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1671 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua;
1675 ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua)
1679 mtx_assert(&lun->lun_lock, MA_OWNED);
1680 if (lun->pending_ua[port] == NULL)
1682 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1683 if (port * CTL_MAX_INIT_PER_PORT + i == except)
1685 lun->pending_ua[port][i] |= ua;
1690 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1692 struct ctl_softc *softc = lun->ctl_softc;
1695 mtx_assert(&lun->lun_lock, MA_OWNED);
1696 for (i = softc->port_min; i < softc->port_max; i++)
1697 ctl_est_ua_port(lun, i, except, ua);
1701 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1703 struct ctl_softc *softc = lun->ctl_softc;
1706 if (initidx < softc->init_min || initidx >= softc->init_max)
1708 mtx_assert(&lun->lun_lock, MA_OWNED);
1709 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1712 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua;
1716 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1718 struct ctl_softc *softc = lun->ctl_softc;
1721 mtx_assert(&lun->lun_lock, MA_OWNED);
1722 for (i = softc->port_min; i < softc->port_max; i++) {
1723 if (lun->pending_ua[i] == NULL)
1725 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
1726 if (i * CTL_MAX_INIT_PER_PORT + j == except)
1728 lun->pending_ua[i][j] &= ~ua;
1734 ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx,
1735 ctl_ua_type ua_type)
1737 struct ctl_lun *lun;
1739 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
1740 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) {
1741 mtx_lock(&lun->lun_lock);
1742 ctl_clr_ua(lun, initidx, ua_type);
1743 mtx_unlock(&lun->lun_lock);
1748 ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS)
1750 struct ctl_softc *softc = (struct ctl_softc *)arg1;
1751 struct ctl_lun *lun;
1752 struct ctl_lun_req ireq;
1755 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1;
1756 error = sysctl_handle_int(oidp, &value, 0, req);
1757 if ((error != 0) || (req->newptr == NULL))
1760 mtx_lock(&softc->ctl_lock);
1762 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1764 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
1765 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1766 mtx_unlock(&softc->ctl_lock);
1767 bzero(&ireq, sizeof(ireq));
1768 ireq.reqtype = CTL_LUNREQ_MODIFY;
1769 ireq.reqdata.modify.lun_id = lun->lun;
1770 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0,
1772 if (ireq.status != CTL_LUN_OK) {
1773 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n",
1774 __func__, ireq.status, ireq.error_str);
1776 mtx_lock(&softc->ctl_lock);
1778 mtx_unlock(&softc->ctl_lock);
1785 struct make_dev_args args;
1786 struct ctl_softc *softc;
1790 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1793 make_dev_args_init(&args);
1794 args.mda_devsw = &ctl_cdevsw;
1795 args.mda_uid = UID_ROOT;
1796 args.mda_gid = GID_OPERATOR;
1797 args.mda_mode = 0600;
1798 args.mda_si_drv1 = softc;
1799 error = make_dev_s(&args, &softc->dev, "cam/ctl");
1801 free(softc, M_DEVBUF);
1802 control_softc = NULL;
1806 sysctl_ctx_init(&softc->sysctl_ctx);
1807 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1808 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1809 CTLFLAG_RD, 0, "CAM Target Layer");
1811 if (softc->sysctl_tree == NULL) {
1812 printf("%s: unable to allocate sysctl tree\n", __func__);
1813 destroy_dev(softc->dev);
1814 free(softc, M_DEVBUF);
1815 control_softc = NULL;
1819 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1820 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
1821 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1824 TUNABLE_INT_FETCH("kern.cam.ctl.ha_mode", (int *)&softc->ha_mode);
1825 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1826 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0,
1827 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)");
1830 * In Copan's HA scheme, the "master" and "slave" roles are
1831 * figured out through the slot the controller is in. Although it
1832 * is an active/active system, someone has to be in charge.
1834 TUNABLE_INT_FETCH("kern.cam.ctl.ha_id", &softc->ha_id);
1835 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1836 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1837 "HA head ID (0 - no HA)");
1838 if (softc->ha_id == 0 || softc->ha_id > NUM_HA_SHELVES) {
1839 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1840 softc->is_single = 1;
1841 softc->port_cnt = CTL_MAX_PORTS;
1842 softc->port_min = 0;
1844 softc->port_cnt = CTL_MAX_PORTS / NUM_HA_SHELVES;
1845 softc->port_min = (softc->ha_id - 1) * softc->port_cnt;
1847 softc->port_max = softc->port_min + softc->port_cnt;
1848 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT;
1849 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT;
1851 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1852 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0,
1853 "HA link state (0 - offline, 1 - unknown, 2 - online)");
1855 STAILQ_INIT(&softc->lun_list);
1856 STAILQ_INIT(&softc->pending_lun_queue);
1857 STAILQ_INIT(&softc->fe_list);
1858 STAILQ_INIT(&softc->port_list);
1859 STAILQ_INIT(&softc->be_list);
1860 ctl_tpc_init(softc);
1862 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
1865 printf("ctl: can't allocate %d entry other SC pool, "
1866 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1869 softc->othersc_pool = other_pool;
1871 if (worker_threads <= 0)
1872 worker_threads = max(1, mp_ncpus / 4);
1873 if (worker_threads > CTL_MAX_THREADS)
1874 worker_threads = CTL_MAX_THREADS;
1876 for (i = 0; i < worker_threads; i++) {
1877 struct ctl_thread *thr = &softc->threads[i];
1879 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1880 thr->ctl_softc = softc;
1881 STAILQ_INIT(&thr->incoming_queue);
1882 STAILQ_INIT(&thr->rtr_queue);
1883 STAILQ_INIT(&thr->done_queue);
1884 STAILQ_INIT(&thr->isc_queue);
1886 error = kproc_kthread_add(ctl_work_thread, thr,
1887 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1889 printf("error creating CTL work thread!\n");
1890 ctl_pool_free(other_pool);
1894 error = kproc_kthread_add(ctl_lun_thread, softc,
1895 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
1897 printf("error creating CTL lun thread!\n");
1898 ctl_pool_free(other_pool);
1901 error = kproc_kthread_add(ctl_thresh_thread, softc,
1902 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh");
1904 printf("error creating CTL threshold thread!\n");
1905 ctl_pool_free(other_pool);
1909 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1910 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN,
1911 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head");
1913 if (softc->is_single == 0) {
1914 ctl_frontend_register(&ha_frontend);
1915 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) {
1916 printf("ctl_init: ctl_ha_msg_init failed.\n");
1917 softc->is_single = 1;
1919 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
1920 != CTL_HA_STATUS_SUCCESS) {
1921 printf("ctl_init: ctl_ha_msg_register failed.\n");
1922 softc->is_single = 1;
1931 struct ctl_softc *softc = control_softc;
1932 struct ctl_lun *lun, *next_lun;
1934 if (softc->is_single == 0) {
1935 ctl_ha_msg_shutdown(softc);
1936 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL)
1937 != CTL_HA_STATUS_SUCCESS)
1938 printf("%s: ctl_ha_msg_deregister failed.\n", __func__);
1939 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS)
1940 printf("%s: ctl_ha_msg_destroy failed.\n", __func__);
1941 ctl_frontend_deregister(&ha_frontend);
1944 mtx_lock(&softc->ctl_lock);
1946 STAILQ_FOREACH_SAFE(lun, &softc->lun_list, links, next_lun)
1949 mtx_unlock(&softc->ctl_lock);
1952 ctl_shutdown_thread(softc->work_thread);
1953 mtx_destroy(&softc->queue_lock);
1956 ctl_tpc_shutdown(softc);
1957 uma_zdestroy(softc->io_zone);
1958 mtx_destroy(&softc->ctl_lock);
1960 destroy_dev(softc->dev);
1962 sysctl_ctx_free(&softc->sysctl_ctx);
1964 free(softc, M_DEVBUF);
1965 control_softc = NULL;
1969 ctl_module_event_handler(module_t mod, int what, void *arg)
1974 return (ctl_init());
1978 return (EOPNOTSUPP);
1983 * XXX KDM should we do some access checks here? Bump a reference count to
1984 * prevent a CTL module from being unloaded while someone has it open?
1987 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1993 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1999 * Remove an initiator by port number and initiator ID.
2000 * Returns 0 for success, -1 for failure.
2003 ctl_remove_initiator(struct ctl_port *port, int iid)
2005 struct ctl_softc *softc = port->ctl_softc;
2007 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2009 if (iid > CTL_MAX_INIT_PER_PORT) {
2010 printf("%s: initiator ID %u > maximun %u!\n",
2011 __func__, iid, CTL_MAX_INIT_PER_PORT);
2015 mtx_lock(&softc->ctl_lock);
2016 port->wwpn_iid[iid].in_use--;
2017 port->wwpn_iid[iid].last_use = time_uptime;
2018 mtx_unlock(&softc->ctl_lock);
2019 ctl_isc_announce_iid(port, iid);
2025 * Add an initiator to the initiator map.
2026 * Returns iid for success, < 0 for failure.
2029 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
2031 struct ctl_softc *softc = port->ctl_softc;
2035 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2037 if (iid >= CTL_MAX_INIT_PER_PORT) {
2038 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
2039 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
2044 mtx_lock(&softc->ctl_lock);
2046 if (iid < 0 && (wwpn != 0 || name != NULL)) {
2047 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2048 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
2052 if (name != NULL && port->wwpn_iid[i].name != NULL &&
2053 strcmp(name, port->wwpn_iid[i].name) == 0) {
2061 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2062 if (port->wwpn_iid[i].in_use == 0 &&
2063 port->wwpn_iid[i].wwpn == 0 &&
2064 port->wwpn_iid[i].name == NULL) {
2073 best_time = INT32_MAX;
2074 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2075 if (port->wwpn_iid[i].in_use == 0) {
2076 if (port->wwpn_iid[i].last_use < best_time) {
2078 best_time = port->wwpn_iid[i].last_use;
2086 mtx_unlock(&softc->ctl_lock);
2091 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
2093 * This is not an error yet.
2095 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
2097 printf("%s: port %d iid %u WWPN %#jx arrived"
2098 " again\n", __func__, port->targ_port,
2099 iid, (uintmax_t)wwpn);
2103 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
2104 strcmp(name, port->wwpn_iid[iid].name) == 0) {
2106 printf("%s: port %d iid %u name '%s' arrived"
2107 " again\n", __func__, port->targ_port,
2114 * This is an error, but what do we do about it? The
2115 * driver is telling us we have a new WWPN for this
2116 * initiator ID, so we pretty much need to use it.
2118 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
2119 " but WWPN %#jx '%s' is still at that address\n",
2120 __func__, port->targ_port, iid, wwpn, name,
2121 (uintmax_t)port->wwpn_iid[iid].wwpn,
2122 port->wwpn_iid[iid].name);
2125 * XXX KDM clear have_ca and ua_pending on each LUN for
2130 free(port->wwpn_iid[iid].name, M_CTL);
2131 port->wwpn_iid[iid].name = name;
2132 port->wwpn_iid[iid].wwpn = wwpn;
2133 port->wwpn_iid[iid].in_use++;
2134 mtx_unlock(&softc->ctl_lock);
2135 ctl_isc_announce_iid(port, iid);
2141 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
2145 switch (port->port_type) {
2148 struct scsi_transportid_fcp *id =
2149 (struct scsi_transportid_fcp *)buf;
2150 if (port->wwpn_iid[iid].wwpn == 0)
2152 memset(id, 0, sizeof(*id));
2153 id->format_protocol = SCSI_PROTO_FC;
2154 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
2155 return (sizeof(*id));
2157 case CTL_PORT_ISCSI:
2159 struct scsi_transportid_iscsi_port *id =
2160 (struct scsi_transportid_iscsi_port *)buf;
2161 if (port->wwpn_iid[iid].name == NULL)
2164 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
2166 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
2167 len = roundup2(min(len, 252), 4);
2168 scsi_ulto2b(len, id->additional_length);
2169 return (sizeof(*id) + len);
2173 struct scsi_transportid_sas *id =
2174 (struct scsi_transportid_sas *)buf;
2175 if (port->wwpn_iid[iid].wwpn == 0)
2177 memset(id, 0, sizeof(*id));
2178 id->format_protocol = SCSI_PROTO_SAS;
2179 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
2180 return (sizeof(*id));
2184 struct scsi_transportid_spi *id =
2185 (struct scsi_transportid_spi *)buf;
2186 memset(id, 0, sizeof(*id));
2187 id->format_protocol = SCSI_PROTO_SPI;
2188 scsi_ulto2b(iid, id->scsi_addr);
2189 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
2190 return (sizeof(*id));
2196 * Serialize a command that went down the "wrong" side, and so was sent to
2197 * this controller for execution. The logic is a little different than the
2198 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
2199 * sent back to the other side, but in the success case, we execute the
2200 * command on this side (XFER mode) or tell the other side to execute it
2204 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
2206 struct ctl_softc *softc = CTL_SOFTC(ctsio);
2207 struct ctl_port *port = CTL_PORT(ctsio);
2208 union ctl_ha_msg msg_info;
2209 struct ctl_lun *lun;
2210 const struct ctl_cmd_entry *entry;
2213 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
2215 /* Make sure that we know about this port. */
2216 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) {
2217 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2222 /* Make sure that we know about this LUN. */
2223 mtx_lock(&softc->ctl_lock);
2224 if (targ_lun >= CTL_MAX_LUNS ||
2225 (lun = softc->ctl_luns[targ_lun]) == NULL) {
2226 mtx_unlock(&softc->ctl_lock);
2229 * The other node would not send this request to us unless
2230 * received announce that we are primary node for this LUN.
2231 * If this LUN does not exist now, it is probably result of
2232 * a race, so respond to initiator in the most opaque way.
2234 ctl_set_busy(ctsio);
2237 mtx_lock(&lun->lun_lock);
2238 mtx_unlock(&softc->ctl_lock);
2241 * If the LUN is invalid, pretend that it doesn't exist.
2242 * It will go away as soon as all pending I/Os completed.
2244 if (lun->flags & CTL_LUN_DISABLED) {
2245 mtx_unlock(&lun->lun_lock);
2246 ctl_set_busy(ctsio);
2250 entry = ctl_get_cmd_entry(ctsio, NULL);
2251 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
2252 mtx_unlock(&lun->lun_lock);
2256 CTL_LUN(ctsio) = lun;
2257 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
2260 * Every I/O goes into the OOA queue for a
2261 * particular LUN, and stays there until completion.
2264 if (TAILQ_EMPTY(&lun->ooa_queue))
2265 lun->idle_time += getsbinuptime() - lun->last_busy;
2267 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2269 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
2270 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
2272 case CTL_ACTION_BLOCK:
2273 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
2274 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
2276 mtx_unlock(&lun->lun_lock);
2278 case CTL_ACTION_PASS:
2279 case CTL_ACTION_SKIP:
2280 if (softc->ha_mode == CTL_HA_MODE_XFER) {
2281 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
2282 ctl_enqueue_rtr((union ctl_io *)ctsio);
2283 mtx_unlock(&lun->lun_lock);
2285 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
2286 mtx_unlock(&lun->lun_lock);
2288 /* send msg back to other side */
2289 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2290 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
2291 msg_info.hdr.msg_type = CTL_MSG_R2R;
2292 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2293 sizeof(msg_info.hdr), M_WAITOK);
2296 case CTL_ACTION_OVERLAP:
2297 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2298 mtx_unlock(&lun->lun_lock);
2299 ctl_set_overlapped_cmd(ctsio);
2301 case CTL_ACTION_OVERLAP_TAG:
2302 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2303 mtx_unlock(&lun->lun_lock);
2304 ctl_set_overlapped_tag(ctsio, ctsio->tag_num);
2306 case CTL_ACTION_ERROR:
2308 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2309 mtx_unlock(&lun->lun_lock);
2311 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2314 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
2315 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2316 msg_info.hdr.serializing_sc = NULL;
2317 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
2318 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2319 sizeof(msg_info.scsi), M_WAITOK);
2320 ctl_free_io((union ctl_io *)ctsio);
2326 * Returns 0 for success, errno for failure.
2329 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2330 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2334 mtx_lock(&lun->lun_lock);
2335 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2336 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2338 struct ctl_ooa_entry *entry;
2341 * If we've got more than we can fit, just count the
2342 * remaining entries.
2344 if (*cur_fill_num >= ooa_hdr->alloc_num)
2347 entry = &kern_entries[*cur_fill_num];
2349 entry->tag_num = io->scsiio.tag_num;
2350 entry->lun_num = lun->lun;
2352 entry->start_bt = io->io_hdr.start_bt;
2354 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2355 entry->cdb_len = io->scsiio.cdb_len;
2356 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2357 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2359 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2360 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2362 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2363 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2365 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2366 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2368 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2369 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2371 mtx_unlock(&lun->lun_lock);
2375 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2376 size_t error_str_len)
2380 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2382 if (copyin(user_addr, kptr, len) != 0) {
2383 snprintf(error_str, error_str_len, "Error copying %d bytes "
2384 "from user address %p to kernel address %p", len,
2394 ctl_free_args(int num_args, struct ctl_be_arg *args)
2401 for (i = 0; i < num_args; i++) {
2402 free(args[i].kname, M_CTL);
2403 free(args[i].kvalue, M_CTL);
2409 static struct ctl_be_arg *
2410 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
2411 char *error_str, size_t error_str_len)
2413 struct ctl_be_arg *args;
2416 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
2417 error_str, error_str_len);
2422 for (i = 0; i < num_args; i++) {
2423 args[i].kname = NULL;
2424 args[i].kvalue = NULL;
2427 for (i = 0; i < num_args; i++) {
2430 args[i].kname = ctl_copyin_alloc(args[i].name,
2431 args[i].namelen, error_str, error_str_len);
2432 if (args[i].kname == NULL)
2435 if (args[i].kname[args[i].namelen - 1] != '\0') {
2436 snprintf(error_str, error_str_len, "Argument %d "
2437 "name is not NUL-terminated", i);
2441 if (args[i].flags & CTL_BEARG_RD) {
2442 tmpptr = ctl_copyin_alloc(args[i].value,
2443 args[i].vallen, error_str, error_str_len);
2446 if ((args[i].flags & CTL_BEARG_ASCII)
2447 && (tmpptr[args[i].vallen - 1] != '\0')) {
2448 snprintf(error_str, error_str_len, "Argument "
2449 "%d value is not NUL-terminated", i);
2450 free(tmpptr, M_CTL);
2453 args[i].kvalue = tmpptr;
2455 args[i].kvalue = malloc(args[i].vallen,
2456 M_CTL, M_WAITOK | M_ZERO);
2463 ctl_free_args(num_args, args);
2469 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2473 for (i = 0; i < num_args; i++) {
2474 if (args[i].flags & CTL_BEARG_WR)
2475 copyout(args[i].kvalue, args[i].value, args[i].vallen);
2480 * Escape characters that are illegal or not recommended in XML.
2483 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2485 char *end = str + size;
2490 for (; *str && str < end; str++) {
2493 retval = sbuf_printf(sb, "&");
2496 retval = sbuf_printf(sb, ">");
2499 retval = sbuf_printf(sb, "<");
2502 retval = sbuf_putc(sb, *str);
2515 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2517 struct scsi_vpd_id_descriptor *desc;
2520 if (id == NULL || id->len < 4)
2522 desc = (struct scsi_vpd_id_descriptor *)id->data;
2523 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2524 case SVPD_ID_TYPE_T10:
2525 sbuf_printf(sb, "t10.");
2527 case SVPD_ID_TYPE_EUI64:
2528 sbuf_printf(sb, "eui.");
2530 case SVPD_ID_TYPE_NAA:
2531 sbuf_printf(sb, "naa.");
2533 case SVPD_ID_TYPE_SCSI_NAME:
2536 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2537 case SVPD_ID_CODESET_BINARY:
2538 for (i = 0; i < desc->length; i++)
2539 sbuf_printf(sb, "%02x", desc->identifier[i]);
2541 case SVPD_ID_CODESET_ASCII:
2542 sbuf_printf(sb, "%.*s", (int)desc->length,
2543 (char *)desc->identifier);
2545 case SVPD_ID_CODESET_UTF8:
2546 sbuf_printf(sb, "%s", (char *)desc->identifier);
2552 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2555 struct ctl_softc *softc = dev->si_drv1;
2556 struct ctl_port *port;
2557 struct ctl_lun *lun;
2564 retval = ctl_ioctl_io(dev, cmd, addr, flag, td);
2566 case CTL_ENABLE_PORT:
2567 case CTL_DISABLE_PORT:
2568 case CTL_SET_PORT_WWNS: {
2569 struct ctl_port *port;
2570 struct ctl_port_entry *entry;
2572 entry = (struct ctl_port_entry *)addr;
2574 mtx_lock(&softc->ctl_lock);
2575 STAILQ_FOREACH(port, &softc->port_list, links) {
2578 if (port->targ_port < softc->port_min ||
2579 port->targ_port >= softc->port_max)
2584 if ((entry->port_type == CTL_PORT_NONE)
2585 && (entry->targ_port == port->targ_port)) {
2587 * If the user only wants to enable or
2588 * disable or set WWNs on a specific port,
2589 * do the operation and we're done.
2593 } else if (entry->port_type & port->port_type) {
2595 * Compare the user's type mask with the
2596 * particular frontend type to see if we
2603 * Make sure the user isn't trying to set
2604 * WWNs on multiple ports at the same time.
2606 if (cmd == CTL_SET_PORT_WWNS) {
2607 printf("%s: Can't set WWNs on "
2608 "multiple ports\n", __func__);
2617 * XXX KDM we have to drop the lock here, because
2618 * the online/offline operations can potentially
2619 * block. We need to reference count the frontends
2620 * so they can't go away,
2622 if (cmd == CTL_ENABLE_PORT) {
2623 mtx_unlock(&softc->ctl_lock);
2624 ctl_port_online(port);
2625 mtx_lock(&softc->ctl_lock);
2626 } else if (cmd == CTL_DISABLE_PORT) {
2627 mtx_unlock(&softc->ctl_lock);
2628 ctl_port_offline(port);
2629 mtx_lock(&softc->ctl_lock);
2630 } else if (cmd == CTL_SET_PORT_WWNS) {
2631 ctl_port_set_wwns(port,
2632 (entry->flags & CTL_PORT_WWNN_VALID) ?
2634 (entry->flags & CTL_PORT_WWPN_VALID) ?
2635 1 : 0, entry->wwpn);
2640 mtx_unlock(&softc->ctl_lock);
2644 struct ctl_ooa *ooa_hdr;
2645 struct ctl_ooa_entry *entries;
2646 uint32_t cur_fill_num;
2648 ooa_hdr = (struct ctl_ooa *)addr;
2650 if ((ooa_hdr->alloc_len == 0)
2651 || (ooa_hdr->alloc_num == 0)) {
2652 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2653 "must be non-zero\n", __func__,
2654 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2659 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2660 sizeof(struct ctl_ooa_entry))) {
2661 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2662 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2663 __func__, ooa_hdr->alloc_len,
2664 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2669 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2670 if (entries == NULL) {
2671 printf("%s: could not allocate %d bytes for OOA "
2672 "dump\n", __func__, ooa_hdr->alloc_len);
2677 mtx_lock(&softc->ctl_lock);
2678 if ((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0 &&
2679 (ooa_hdr->lun_num >= CTL_MAX_LUNS ||
2680 softc->ctl_luns[ooa_hdr->lun_num] == NULL)) {
2681 mtx_unlock(&softc->ctl_lock);
2682 free(entries, M_CTL);
2683 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2684 __func__, (uintmax_t)ooa_hdr->lun_num);
2691 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2692 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2693 ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2697 lun = softc->ctl_luns[ooa_hdr->lun_num];
2698 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr,
2701 mtx_unlock(&softc->ctl_lock);
2703 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2704 ooa_hdr->fill_len = ooa_hdr->fill_num *
2705 sizeof(struct ctl_ooa_entry);
2706 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2708 printf("%s: error copying out %d bytes for OOA dump\n",
2709 __func__, ooa_hdr->fill_len);
2712 getbinuptime(&ooa_hdr->cur_bt);
2714 if (cur_fill_num > ooa_hdr->alloc_num) {
2715 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2716 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2718 ooa_hdr->dropped_num = 0;
2719 ooa_hdr->status = CTL_OOA_OK;
2722 free(entries, M_CTL);
2725 case CTL_DELAY_IO: {
2726 struct ctl_io_delay_info *delay_info;
2728 delay_info = (struct ctl_io_delay_info *)addr;
2731 mtx_lock(&softc->ctl_lock);
2732 if (delay_info->lun_id >= CTL_MAX_LUNS ||
2733 (lun = softc->ctl_luns[delay_info->lun_id]) == NULL) {
2734 mtx_unlock(&softc->ctl_lock);
2735 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2738 mtx_lock(&lun->lun_lock);
2739 mtx_unlock(&softc->ctl_lock);
2740 delay_info->status = CTL_DELAY_STATUS_OK;
2741 switch (delay_info->delay_type) {
2742 case CTL_DELAY_TYPE_CONT:
2743 case CTL_DELAY_TYPE_ONESHOT:
2746 delay_info->status = CTL_DELAY_STATUS_INVALID_TYPE;
2749 switch (delay_info->delay_loc) {
2750 case CTL_DELAY_LOC_DATAMOVE:
2751 lun->delay_info.datamove_type = delay_info->delay_type;
2752 lun->delay_info.datamove_delay = delay_info->delay_secs;
2754 case CTL_DELAY_LOC_DONE:
2755 lun->delay_info.done_type = delay_info->delay_type;
2756 lun->delay_info.done_delay = delay_info->delay_secs;
2759 delay_info->status = CTL_DELAY_STATUS_INVALID_LOC;
2762 mtx_unlock(&lun->lun_lock);
2764 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2765 #endif /* CTL_IO_DELAY */
2768 #ifdef CTL_LEGACY_STATS
2769 case CTL_GETSTATS: {
2770 struct ctl_stats *stats = (struct ctl_stats *)addr;
2774 * XXX KDM no locking here. If the LUN list changes,
2775 * things can blow up.
2778 stats->status = CTL_SS_OK;
2779 stats->fill_len = 0;
2780 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2781 if (stats->fill_len + sizeof(lun->legacy_stats) >
2783 stats->status = CTL_SS_NEED_MORE_SPACE;
2786 retval = copyout(&lun->legacy_stats, &stats->lun_stats[i++],
2787 sizeof(lun->legacy_stats));
2790 stats->fill_len += sizeof(lun->legacy_stats);
2792 stats->num_luns = softc->num_luns;
2793 stats->flags = CTL_STATS_FLAG_NONE;
2795 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
2797 getnanouptime(&stats->timestamp);
2800 #endif /* CTL_LEGACY_STATS */
2801 case CTL_ERROR_INJECT: {
2802 struct ctl_error_desc *err_desc, *new_err_desc;
2804 err_desc = (struct ctl_error_desc *)addr;
2806 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2808 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2810 mtx_lock(&softc->ctl_lock);
2811 if (err_desc->lun_id >= CTL_MAX_LUNS ||
2812 (lun = softc->ctl_luns[err_desc->lun_id]) == NULL) {
2813 mtx_unlock(&softc->ctl_lock);
2814 free(new_err_desc, M_CTL);
2815 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2816 __func__, (uintmax_t)err_desc->lun_id);
2820 mtx_lock(&lun->lun_lock);
2821 mtx_unlock(&softc->ctl_lock);
2824 * We could do some checking here to verify the validity
2825 * of the request, but given the complexity of error
2826 * injection requests, the checking logic would be fairly
2829 * For now, if the request is invalid, it just won't get
2830 * executed and might get deleted.
2832 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2835 * XXX KDM check to make sure the serial number is unique,
2836 * in case we somehow manage to wrap. That shouldn't
2837 * happen for a very long time, but it's the right thing to
2840 new_err_desc->serial = lun->error_serial;
2841 err_desc->serial = lun->error_serial;
2842 lun->error_serial++;
2844 mtx_unlock(&lun->lun_lock);
2847 case CTL_ERROR_INJECT_DELETE: {
2848 struct ctl_error_desc *delete_desc, *desc, *desc2;
2851 delete_desc = (struct ctl_error_desc *)addr;
2854 mtx_lock(&softc->ctl_lock);
2855 if (delete_desc->lun_id >= CTL_MAX_LUNS ||
2856 (lun = softc->ctl_luns[delete_desc->lun_id]) == NULL) {
2857 mtx_unlock(&softc->ctl_lock);
2858 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2859 __func__, (uintmax_t)delete_desc->lun_id);
2863 mtx_lock(&lun->lun_lock);
2864 mtx_unlock(&softc->ctl_lock);
2865 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2866 if (desc->serial != delete_desc->serial)
2869 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2874 mtx_unlock(&lun->lun_lock);
2875 if (delete_done == 0) {
2876 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2877 "error serial %ju on LUN %u\n", __func__,
2878 delete_desc->serial, delete_desc->lun_id);
2884 case CTL_DUMP_STRUCTS: {
2886 struct ctl_port *port;
2887 struct ctl_frontend *fe;
2889 mtx_lock(&softc->ctl_lock);
2890 printf("CTL Persistent Reservation information start:\n");
2891 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2892 mtx_lock(&lun->lun_lock);
2893 if ((lun->flags & CTL_LUN_DISABLED) != 0) {
2894 mtx_unlock(&lun->lun_lock);
2898 for (j = 0; j < CTL_MAX_PORTS; j++) {
2899 if (lun->pr_keys[j] == NULL)
2901 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2902 if (lun->pr_keys[j][k] == 0)
2904 printf(" LUN %ju port %d iid %d key "
2905 "%#jx\n", lun->lun, j, k,
2906 (uintmax_t)lun->pr_keys[j][k]);
2909 mtx_unlock(&lun->lun_lock);
2911 printf("CTL Persistent Reservation information end\n");
2912 printf("CTL Ports:\n");
2913 STAILQ_FOREACH(port, &softc->port_list, links) {
2914 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
2915 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
2916 port->frontend->name, port->port_type,
2917 port->physical_port, port->virtual_port,
2918 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
2919 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2920 if (port->wwpn_iid[j].in_use == 0 &&
2921 port->wwpn_iid[j].wwpn == 0 &&
2922 port->wwpn_iid[j].name == NULL)
2925 printf(" iid %u use %d WWPN %#jx '%s'\n",
2926 j, port->wwpn_iid[j].in_use,
2927 (uintmax_t)port->wwpn_iid[j].wwpn,
2928 port->wwpn_iid[j].name);
2931 printf("CTL Port information end\n");
2932 mtx_unlock(&softc->ctl_lock);
2934 * XXX KDM calling this without a lock. We'd likely want
2935 * to drop the lock before calling the frontend's dump
2938 printf("CTL Frontends:\n");
2939 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2940 printf(" Frontend '%s'\n", fe->name);
2941 if (fe->fe_dump != NULL)
2944 printf("CTL Frontend information end\n");
2948 struct ctl_lun_req *lun_req;
2949 struct ctl_backend_driver *backend;
2951 lun_req = (struct ctl_lun_req *)addr;
2953 backend = ctl_backend_find(lun_req->backend);
2954 if (backend == NULL) {
2955 lun_req->status = CTL_LUN_ERROR;
2956 snprintf(lun_req->error_str,
2957 sizeof(lun_req->error_str),
2958 "Backend \"%s\" not found.",
2962 if (lun_req->num_be_args > 0) {
2963 lun_req->kern_be_args = ctl_copyin_args(
2964 lun_req->num_be_args,
2967 sizeof(lun_req->error_str));
2968 if (lun_req->kern_be_args == NULL) {
2969 lun_req->status = CTL_LUN_ERROR;
2974 retval = backend->ioctl(dev, cmd, addr, flag, td);
2976 if (lun_req->num_be_args > 0) {
2977 ctl_copyout_args(lun_req->num_be_args,
2978 lun_req->kern_be_args);
2979 ctl_free_args(lun_req->num_be_args,
2980 lun_req->kern_be_args);
2984 case CTL_LUN_LIST: {
2986 struct ctl_lun_list *list;
2987 struct ctl_option *opt;
2989 list = (struct ctl_lun_list *)addr;
2992 * Allocate a fixed length sbuf here, based on the length
2993 * of the user's buffer. We could allocate an auto-extending
2994 * buffer, and then tell the user how much larger our
2995 * amount of data is than his buffer, but that presents
2998 * 1. The sbuf(9) routines use a blocking malloc, and so
2999 * we can't hold a lock while calling them with an
3000 * auto-extending buffer.
3002 * 2. There is not currently a LUN reference counting
3003 * mechanism, outside of outstanding transactions on
3004 * the LUN's OOA queue. So a LUN could go away on us
3005 * while we're getting the LUN number, backend-specific
3006 * information, etc. Thus, given the way things
3007 * currently work, we need to hold the CTL lock while
3008 * grabbing LUN information.
3010 * So, from the user's standpoint, the best thing to do is
3011 * allocate what he thinks is a reasonable buffer length,
3012 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3013 * double the buffer length and try again. (And repeat
3014 * that until he succeeds.)
3016 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3018 list->status = CTL_LUN_LIST_ERROR;
3019 snprintf(list->error_str, sizeof(list->error_str),
3020 "Unable to allocate %d bytes for LUN list",
3025 sbuf_printf(sb, "<ctllunlist>\n");
3027 mtx_lock(&softc->ctl_lock);
3028 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3029 mtx_lock(&lun->lun_lock);
3030 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3031 (uintmax_t)lun->lun);
3034 * Bail out as soon as we see that we've overfilled
3040 retval = sbuf_printf(sb, "\t<backend_type>%s"
3041 "</backend_type>\n",
3042 (lun->backend == NULL) ? "none" :
3043 lun->backend->name);
3048 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3049 lun->be_lun->lun_type);
3054 if (lun->backend == NULL) {
3055 retval = sbuf_printf(sb, "</lun>\n");
3061 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3062 (lun->be_lun->maxlba > 0) ?
3063 lun->be_lun->maxlba + 1 : 0);
3068 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3069 lun->be_lun->blocksize);
3074 retval = sbuf_printf(sb, "\t<serial_number>");
3079 retval = ctl_sbuf_printf_esc(sb,
3080 lun->be_lun->serial_num,
3081 sizeof(lun->be_lun->serial_num));
3086 retval = sbuf_printf(sb, "</serial_number>\n");
3091 retval = sbuf_printf(sb, "\t<device_id>");
3096 retval = ctl_sbuf_printf_esc(sb,
3097 lun->be_lun->device_id,
3098 sizeof(lun->be_lun->device_id));
3103 retval = sbuf_printf(sb, "</device_id>\n");
3108 if (lun->backend->lun_info != NULL) {
3109 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3113 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3114 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3115 opt->name, opt->value, opt->name);
3120 retval = sbuf_printf(sb, "</lun>\n");
3124 mtx_unlock(&lun->lun_lock);
3127 mtx_unlock(&lun->lun_lock);
3128 mtx_unlock(&softc->ctl_lock);
3131 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3134 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3135 snprintf(list->error_str, sizeof(list->error_str),
3136 "Out of space, %d bytes is too small",
3143 retval = copyout(sbuf_data(sb), list->lun_xml,
3146 list->fill_len = sbuf_len(sb) + 1;
3147 list->status = CTL_LUN_LIST_OK;
3152 struct ctl_iscsi *ci;
3153 struct ctl_frontend *fe;
3155 ci = (struct ctl_iscsi *)addr;
3157 fe = ctl_frontend_find("iscsi");
3159 ci->status = CTL_ISCSI_ERROR;
3160 snprintf(ci->error_str, sizeof(ci->error_str),
3161 "Frontend \"iscsi\" not found.");
3165 retval = fe->ioctl(dev, cmd, addr, flag, td);
3168 case CTL_PORT_REQ: {
3169 struct ctl_req *req;
3170 struct ctl_frontend *fe;
3172 req = (struct ctl_req *)addr;
3174 fe = ctl_frontend_find(req->driver);
3176 req->status = CTL_LUN_ERROR;
3177 snprintf(req->error_str, sizeof(req->error_str),
3178 "Frontend \"%s\" not found.", req->driver);
3181 if (req->num_args > 0) {
3182 req->kern_args = ctl_copyin_args(req->num_args,
3183 req->args, req->error_str, sizeof(req->error_str));
3184 if (req->kern_args == NULL) {
3185 req->status = CTL_LUN_ERROR;
3191 retval = fe->ioctl(dev, cmd, addr, flag, td);
3195 if (req->num_args > 0) {
3196 ctl_copyout_args(req->num_args, req->kern_args);
3197 ctl_free_args(req->num_args, req->kern_args);
3201 case CTL_PORT_LIST: {
3203 struct ctl_port *port;
3204 struct ctl_lun_list *list;
3205 struct ctl_option *opt;
3209 list = (struct ctl_lun_list *)addr;
3211 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3213 list->status = CTL_LUN_LIST_ERROR;
3214 snprintf(list->error_str, sizeof(list->error_str),
3215 "Unable to allocate %d bytes for LUN list",
3220 sbuf_printf(sb, "<ctlportlist>\n");
3222 mtx_lock(&softc->ctl_lock);
3223 STAILQ_FOREACH(port, &softc->port_list, links) {
3224 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3225 (uintmax_t)port->targ_port);
3228 * Bail out as soon as we see that we've overfilled
3234 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3235 "</frontend_type>\n", port->frontend->name);
3239 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3244 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3245 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3249 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3254 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3255 port->physical_port);
3259 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3260 port->virtual_port);
3264 if (port->target_devid != NULL) {
3265 sbuf_printf(sb, "\t<target>");
3266 ctl_id_sbuf(port->target_devid, sb);
3267 sbuf_printf(sb, "</target>\n");
3270 if (port->port_devid != NULL) {
3271 sbuf_printf(sb, "\t<port>");
3272 ctl_id_sbuf(port->port_devid, sb);
3273 sbuf_printf(sb, "</port>\n");
3276 if (port->port_info != NULL) {
3277 retval = port->port_info(port->onoff_arg, sb);
3281 STAILQ_FOREACH(opt, &port->options, links) {
3282 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3283 opt->name, opt->value, opt->name);
3288 if (port->lun_map != NULL) {
3289 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n");
3290 for (j = 0; j < port->lun_map_size; j++) {
3291 plun = ctl_lun_map_from_port(port, j);
3292 if (plun == UINT32_MAX)
3295 "\t<lun id=\"%u\">%u</lun>\n",
3300 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3301 if (port->wwpn_iid[j].in_use == 0 ||
3302 (port->wwpn_iid[j].wwpn == 0 &&
3303 port->wwpn_iid[j].name == NULL))
3306 if (port->wwpn_iid[j].name != NULL)
3307 retval = sbuf_printf(sb,
3308 "\t<initiator id=\"%u\">%s</initiator>\n",
3309 j, port->wwpn_iid[j].name);
3311 retval = sbuf_printf(sb,
3312 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3313 j, port->wwpn_iid[j].wwpn);
3320 retval = sbuf_printf(sb, "</targ_port>\n");
3324 mtx_unlock(&softc->ctl_lock);
3327 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3330 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3331 snprintf(list->error_str, sizeof(list->error_str),
3332 "Out of space, %d bytes is too small",
3339 retval = copyout(sbuf_data(sb), list->lun_xml,
3342 list->fill_len = sbuf_len(sb) + 1;
3343 list->status = CTL_LUN_LIST_OK;
3348 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr;
3349 struct ctl_port *port;
3351 mtx_lock(&softc->ctl_lock);
3352 if (lm->port < softc->port_min ||
3353 lm->port >= softc->port_max ||
3354 (port = softc->ctl_ports[lm->port]) == NULL) {
3355 mtx_unlock(&softc->ctl_lock);
3358 if (port->status & CTL_PORT_STATUS_ONLINE) {
3359 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3360 if (ctl_lun_map_to_port(port, lun->lun) ==
3363 mtx_lock(&lun->lun_lock);
3364 ctl_est_ua_port(lun, lm->port, -1,
3366 mtx_unlock(&lun->lun_lock);
3369 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps
3370 if (lm->plun != UINT32_MAX) {
3371 if (lm->lun == UINT32_MAX)
3372 retval = ctl_lun_map_unset(port, lm->plun);
3373 else if (lm->lun < CTL_MAX_LUNS &&
3374 softc->ctl_luns[lm->lun] != NULL)
3375 retval = ctl_lun_map_set(port, lm->plun, lm->lun);
3379 if (lm->lun == UINT32_MAX)
3380 retval = ctl_lun_map_deinit(port);
3382 retval = ctl_lun_map_init(port);
3384 if (port->status & CTL_PORT_STATUS_ONLINE)
3385 ctl_isc_announce_port(port);
3388 case CTL_GET_LUN_STATS: {
3389 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr;
3393 * XXX KDM no locking here. If the LUN list changes,
3394 * things can blow up.
3397 stats->status = CTL_SS_OK;
3398 stats->fill_len = 0;
3399 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3400 if (lun->lun < stats->first_item)
3402 if (stats->fill_len + sizeof(lun->stats) >
3404 stats->status = CTL_SS_NEED_MORE_SPACE;
3407 retval = copyout(&lun->stats, &stats->stats[i++],
3408 sizeof(lun->stats));
3411 stats->fill_len += sizeof(lun->stats);
3413 stats->num_items = softc->num_luns;
3414 stats->flags = CTL_STATS_FLAG_NONE;
3416 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
3418 getnanouptime(&stats->timestamp);
3421 case CTL_GET_PORT_STATS: {
3422 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr;
3426 * XXX KDM no locking here. If the LUN list changes,
3427 * things can blow up.
3430 stats->status = CTL_SS_OK;
3431 stats->fill_len = 0;
3432 STAILQ_FOREACH(port, &softc->port_list, links) {
3433 if (port->targ_port < stats->first_item)
3435 if (stats->fill_len + sizeof(port->stats) >
3437 stats->status = CTL_SS_NEED_MORE_SPACE;
3440 retval = copyout(&port->stats, &stats->stats[i++],
3441 sizeof(port->stats));
3444 stats->fill_len += sizeof(port->stats);
3446 stats->num_items = softc->num_ports;
3447 stats->flags = CTL_STATS_FLAG_NONE;
3449 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
3451 getnanouptime(&stats->timestamp);
3455 /* XXX KDM should we fix this? */
3457 struct ctl_backend_driver *backend;
3464 * We encode the backend type as the ioctl type for backend
3465 * ioctls. So parse it out here, and then search for a
3466 * backend of this type.
3468 type = _IOC_TYPE(cmd);
3470 STAILQ_FOREACH(backend, &softc->be_list, links) {
3471 if (backend->type == type) {
3477 printf("ctl: unknown ioctl command %#lx or backend "
3482 retval = backend->ioctl(dev, cmd, addr, flag, td);
3492 ctl_get_initindex(struct ctl_nexus *nexus)
3494 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3498 ctl_lun_map_init(struct ctl_port *port)
3500 struct ctl_softc *softc = port->ctl_softc;
3501 struct ctl_lun *lun;
3502 int size = ctl_lun_map_size;
3505 if (port->lun_map == NULL || port->lun_map_size < size) {
3506 port->lun_map_size = 0;
3507 free(port->lun_map, M_CTL);
3508 port->lun_map = malloc(size * sizeof(uint32_t),
3511 if (port->lun_map == NULL)
3513 for (i = 0; i < size; i++)
3514 port->lun_map[i] = UINT32_MAX;
3515 port->lun_map_size = size;
3516 if (port->status & CTL_PORT_STATUS_ONLINE) {
3517 if (port->lun_disable != NULL) {
3518 STAILQ_FOREACH(lun, &softc->lun_list, links)
3519 port->lun_disable(port->targ_lun_arg, lun->lun);
3521 ctl_isc_announce_port(port);
3527 ctl_lun_map_deinit(struct ctl_port *port)
3529 struct ctl_softc *softc = port->ctl_softc;
3530 struct ctl_lun *lun;
3532 if (port->lun_map == NULL)
3534 port->lun_map_size = 0;
3535 free(port->lun_map, M_CTL);
3536 port->lun_map = NULL;
3537 if (port->status & CTL_PORT_STATUS_ONLINE) {
3538 if (port->lun_enable != NULL) {
3539 STAILQ_FOREACH(lun, &softc->lun_list, links)
3540 port->lun_enable(port->targ_lun_arg, lun->lun);
3542 ctl_isc_announce_port(port);
3548 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun)
3553 if (port->lun_map == NULL) {
3554 status = ctl_lun_map_init(port);
3558 if (plun >= port->lun_map_size)
3560 old = port->lun_map[plun];
3561 port->lun_map[plun] = glun;
3562 if ((port->status & CTL_PORT_STATUS_ONLINE) && old == UINT32_MAX) {
3563 if (port->lun_enable != NULL)
3564 port->lun_enable(port->targ_lun_arg, plun);
3565 ctl_isc_announce_port(port);
3571 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun)
3575 if (port->lun_map == NULL || plun >= port->lun_map_size)
3577 old = port->lun_map[plun];
3578 port->lun_map[plun] = UINT32_MAX;
3579 if ((port->status & CTL_PORT_STATUS_ONLINE) && old != UINT32_MAX) {
3580 if (port->lun_disable != NULL)
3581 port->lun_disable(port->targ_lun_arg, plun);
3582 ctl_isc_announce_port(port);
3588 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
3592 return (UINT32_MAX);
3593 if (port->lun_map == NULL)
3595 if (lun_id > port->lun_map_size)
3596 return (UINT32_MAX);
3597 return (port->lun_map[lun_id]);
3601 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
3606 return (UINT32_MAX);
3607 if (port->lun_map == NULL)
3609 for (i = 0; i < port->lun_map_size; i++) {
3610 if (port->lun_map[i] == lun_id)
3613 return (UINT32_MAX);
3617 ctl_decode_lun(uint64_t encoded)
3620 uint32_t result = 0xffffffff;
3622 be64enc(lun, encoded);
3623 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) {
3624 case RPL_LUNDATA_ATYP_PERIPH:
3625 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 &&
3626 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0)
3629 case RPL_LUNDATA_ATYP_FLAT:
3630 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 &&
3631 lun[6] == 0 && lun[7] == 0)
3632 result = ((lun[0] & 0x3f) << 8) + lun[1];
3634 case RPL_LUNDATA_ATYP_EXTLUN:
3635 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) {
3637 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) {
3642 result = (lun[1] << 16) + (lun[2] << 8) +
3646 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0)
3647 result = (lun[2] << 24) +
3648 (lun[3] << 16) + (lun[4] << 8) +
3653 case RPL_LUNDATA_EXT_EAM_NOT_SPEC:
3654 result = 0xffffffff;
3663 ctl_encode_lun(uint32_t decoded)
3665 uint64_t l = decoded;
3668 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48));
3670 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48));
3672 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) |
3674 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16));
3678 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last)
3682 for (i = first; i < last; i++) {
3683 if ((mask[i / 32] & (1 << (i % 32))) == 0)
3690 ctl_set_mask(uint32_t *mask, uint32_t bit)
3692 uint32_t chunk, piece;
3695 piece = bit % (sizeof(uint32_t) * 8);
3697 if ((mask[chunk] & (1 << piece)) != 0)
3700 mask[chunk] |= (1 << piece);
3706 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3708 uint32_t chunk, piece;
3711 piece = bit % (sizeof(uint32_t) * 8);
3713 if ((mask[chunk] & (1 << piece)) == 0)
3716 mask[chunk] &= ~(1 << piece);
3722 ctl_is_set(uint32_t *mask, uint32_t bit)
3724 uint32_t chunk, piece;
3727 piece = bit % (sizeof(uint32_t) * 8);
3729 if ((mask[chunk] & (1 << piece)) == 0)
3736 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3740 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3743 return (t[residx % CTL_MAX_INIT_PER_PORT]);
3747 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3751 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3754 t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3758 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3763 i = residx/CTL_MAX_INIT_PER_PORT;
3764 if (lun->pr_keys[i] != NULL)
3766 mtx_unlock(&lun->lun_lock);
3767 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3769 mtx_lock(&lun->lun_lock);
3770 if (lun->pr_keys[i] == NULL)
3771 lun->pr_keys[i] = p;
3777 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3781 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3782 KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3783 t[residx % CTL_MAX_INIT_PER_PORT] = key;
3787 * ctl_softc, pool_name, total_ctl_io are passed in.
3788 * npool is passed out.
3791 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3792 uint32_t total_ctl_io, void **npool)
3794 struct ctl_io_pool *pool;
3796 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3801 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3802 pool->ctl_softc = ctl_softc;
3804 pool->zone = uma_zsecond_create(pool->name, NULL,
3805 NULL, NULL, NULL, ctl_softc->io_zone);
3806 /* uma_prealloc(pool->zone, total_ctl_io); */
3808 pool->zone = ctl_softc->io_zone;
3816 ctl_pool_free(struct ctl_io_pool *pool)
3823 uma_zdestroy(pool->zone);
3829 ctl_alloc_io(void *pool_ref)
3831 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3834 io = uma_zalloc(pool->zone, M_WAITOK);
3836 io->io_hdr.pool = pool_ref;
3837 CTL_SOFTC(io) = pool->ctl_softc;
3843 ctl_alloc_io_nowait(void *pool_ref)
3845 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3848 io = uma_zalloc(pool->zone, M_NOWAIT);
3850 io->io_hdr.pool = pool_ref;
3851 CTL_SOFTC(io) = pool->ctl_softc;
3857 ctl_free_io(union ctl_io *io)
3859 struct ctl_io_pool *pool;
3864 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3865 uma_zfree(pool->zone, io);
3869 ctl_zero_io(union ctl_io *io)
3871 struct ctl_io_pool *pool;
3877 * May need to preserve linked list pointers at some point too.
3879 pool = io->io_hdr.pool;
3880 memset(io, 0, sizeof(*io));
3881 io->io_hdr.pool = pool;
3882 CTL_SOFTC(io) = pool->ctl_softc;
3886 ctl_expand_number(const char *buf, uint64_t *num)
3892 number = strtoq(buf, &endptr, 0);
3894 switch (tolower((unsigned char)*endptr)) {
3914 case '\0': /* No unit. */
3918 /* Unrecognized unit. */
3922 if ((number << shift) >> shift != number) {
3926 *num = number << shift;
3932 * This routine could be used in the future to load default and/or saved
3933 * mode page parameters for a particuar lun.
3936 ctl_init_page_index(struct ctl_lun *lun)
3939 struct ctl_page_index *page_index;
3943 memcpy(&lun->mode_pages.index, page_index_template,
3944 sizeof(page_index_template));
3946 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3948 page_index = &lun->mode_pages.index[i];
3949 if (lun->be_lun->lun_type == T_DIRECT &&
3950 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
3952 if (lun->be_lun->lun_type == T_PROCESSOR &&
3953 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
3955 if (lun->be_lun->lun_type == T_CDROM &&
3956 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
3959 page_code = page_index->page_code & SMPH_PC_MASK;
3960 switch (page_code) {
3961 case SMS_RW_ERROR_RECOVERY_PAGE: {
3962 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
3963 ("subpage %#x for page %#x is incorrect!",
3964 page_index->subpage, page_code));
3965 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
3966 &rw_er_page_default,
3967 sizeof(rw_er_page_default));
3968 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
3969 &rw_er_page_changeable,
3970 sizeof(rw_er_page_changeable));
3971 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
3972 &rw_er_page_default,
3973 sizeof(rw_er_page_default));
3974 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
3975 &rw_er_page_default,
3976 sizeof(rw_er_page_default));
3977 page_index->page_data =
3978 (uint8_t *)lun->mode_pages.rw_er_page;
3981 case SMS_FORMAT_DEVICE_PAGE: {
3982 struct scsi_format_page *format_page;
3984 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
3985 ("subpage %#x for page %#x is incorrect!",
3986 page_index->subpage, page_code));
3989 * Sectors per track are set above. Bytes per
3990 * sector need to be set here on a per-LUN basis.
3992 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
3993 &format_page_default,
3994 sizeof(format_page_default));
3995 memcpy(&lun->mode_pages.format_page[
3996 CTL_PAGE_CHANGEABLE], &format_page_changeable,
3997 sizeof(format_page_changeable));
3998 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
3999 &format_page_default,
4000 sizeof(format_page_default));
4001 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
4002 &format_page_default,
4003 sizeof(format_page_default));
4005 format_page = &lun->mode_pages.format_page[
4007 scsi_ulto2b(lun->be_lun->blocksize,
4008 format_page->bytes_per_sector);
4010 format_page = &lun->mode_pages.format_page[
4012 scsi_ulto2b(lun->be_lun->blocksize,
4013 format_page->bytes_per_sector);
4015 format_page = &lun->mode_pages.format_page[
4017 scsi_ulto2b(lun->be_lun->blocksize,
4018 format_page->bytes_per_sector);
4020 page_index->page_data =
4021 (uint8_t *)lun->mode_pages.format_page;
4024 case SMS_RIGID_DISK_PAGE: {
4025 struct scsi_rigid_disk_page *rigid_disk_page;
4026 uint32_t sectors_per_cylinder;
4030 #endif /* !__XSCALE__ */
4032 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4033 ("subpage %#x for page %#x is incorrect!",
4034 page_index->subpage, page_code));
4037 * Rotation rate and sectors per track are set
4038 * above. We calculate the cylinders here based on
4039 * capacity. Due to the number of heads and
4040 * sectors per track we're using, smaller arrays
4041 * may turn out to have 0 cylinders. Linux and
4042 * FreeBSD don't pay attention to these mode pages
4043 * to figure out capacity, but Solaris does. It
4044 * seems to deal with 0 cylinders just fine, and
4045 * works out a fake geometry based on the capacity.
4047 memcpy(&lun->mode_pages.rigid_disk_page[
4048 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4049 sizeof(rigid_disk_page_default));
4050 memcpy(&lun->mode_pages.rigid_disk_page[
4051 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4052 sizeof(rigid_disk_page_changeable));
4054 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4058 * The divide method here will be more accurate,
4059 * probably, but results in floating point being
4060 * used in the kernel on i386 (__udivdi3()). On the
4061 * XScale, though, __udivdi3() is implemented in
4064 * The shift method for cylinder calculation is
4065 * accurate if sectors_per_cylinder is a power of
4066 * 2. Otherwise it might be slightly off -- you
4067 * might have a bit of a truncation problem.
4070 cylinders = (lun->be_lun->maxlba + 1) /
4071 sectors_per_cylinder;
4073 for (shift = 31; shift > 0; shift--) {
4074 if (sectors_per_cylinder & (1 << shift))
4077 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4081 * We've basically got 3 bytes, or 24 bits for the
4082 * cylinder size in the mode page. If we're over,
4083 * just round down to 2^24.
4085 if (cylinders > 0xffffff)
4086 cylinders = 0xffffff;
4088 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4090 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4092 if ((value = ctl_get_opt(&lun->be_lun->options,
4094 scsi_ulto2b(strtol(value, NULL, 0),
4095 rigid_disk_page->rotation_rate);
4098 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
4099 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4100 sizeof(rigid_disk_page_default));
4101 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
4102 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4103 sizeof(rigid_disk_page_default));
4105 page_index->page_data =
4106 (uint8_t *)lun->mode_pages.rigid_disk_page;
4109 case SMS_VERIFY_ERROR_RECOVERY_PAGE: {
4110 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4111 ("subpage %#x for page %#x is incorrect!",
4112 page_index->subpage, page_code));
4113 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CURRENT],
4114 &verify_er_page_default,
4115 sizeof(verify_er_page_default));
4116 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CHANGEABLE],
4117 &verify_er_page_changeable,
4118 sizeof(verify_er_page_changeable));
4119 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_DEFAULT],
4120 &verify_er_page_default,
4121 sizeof(verify_er_page_default));
4122 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_SAVED],
4123 &verify_er_page_default,
4124 sizeof(verify_er_page_default));
4125 page_index->page_data =
4126 (uint8_t *)lun->mode_pages.verify_er_page;
4129 case SMS_CACHING_PAGE: {
4130 struct scsi_caching_page *caching_page;
4132 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4133 ("subpage %#x for page %#x is incorrect!",
4134 page_index->subpage, page_code));
4135 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4136 &caching_page_default,
4137 sizeof(caching_page_default));
4138 memcpy(&lun->mode_pages.caching_page[
4139 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4140 sizeof(caching_page_changeable));
4141 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4142 &caching_page_default,
4143 sizeof(caching_page_default));
4144 caching_page = &lun->mode_pages.caching_page[
4146 value = ctl_get_opt(&lun->be_lun->options, "writecache");
4147 if (value != NULL && strcmp(value, "off") == 0)
4148 caching_page->flags1 &= ~SCP_WCE;
4149 value = ctl_get_opt(&lun->be_lun->options, "readcache");
4150 if (value != NULL && strcmp(value, "off") == 0)
4151 caching_page->flags1 |= SCP_RCD;
4152 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4153 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4154 sizeof(caching_page_default));
4155 page_index->page_data =
4156 (uint8_t *)lun->mode_pages.caching_page;
4159 case SMS_CONTROL_MODE_PAGE: {
4160 switch (page_index->subpage) {
4161 case SMS_SUBPAGE_PAGE_0: {
4162 struct scsi_control_page *control_page;
4164 memcpy(&lun->mode_pages.control_page[
4166 &control_page_default,
4167 sizeof(control_page_default));
4168 memcpy(&lun->mode_pages.control_page[
4169 CTL_PAGE_CHANGEABLE],
4170 &control_page_changeable,
4171 sizeof(control_page_changeable));
4172 memcpy(&lun->mode_pages.control_page[
4174 &control_page_default,
4175 sizeof(control_page_default));
4176 control_page = &lun->mode_pages.control_page[
4178 value = ctl_get_opt(&lun->be_lun->options,
4180 if (value != NULL &&
4181 strcmp(value, "unrestricted") == 0) {
4182 control_page->queue_flags &=
4183 ~SCP_QUEUE_ALG_MASK;
4184 control_page->queue_flags |=
4185 SCP_QUEUE_ALG_UNRESTRICTED;
4187 memcpy(&lun->mode_pages.control_page[
4189 &lun->mode_pages.control_page[
4191 sizeof(control_page_default));
4192 page_index->page_data =
4193 (uint8_t *)lun->mode_pages.control_page;
4197 memcpy(&lun->mode_pages.control_ext_page[
4199 &control_ext_page_default,
4200 sizeof(control_ext_page_default));
4201 memcpy(&lun->mode_pages.control_ext_page[
4202 CTL_PAGE_CHANGEABLE],
4203 &control_ext_page_changeable,
4204 sizeof(control_ext_page_changeable));
4205 memcpy(&lun->mode_pages.control_ext_page[
4207 &control_ext_page_default,
4208 sizeof(control_ext_page_default));
4209 memcpy(&lun->mode_pages.control_ext_page[
4211 &lun->mode_pages.control_ext_page[
4213 sizeof(control_ext_page_default));
4214 page_index->page_data =
4215 (uint8_t *)lun->mode_pages.control_ext_page;
4218 panic("subpage %#x for page %#x is incorrect!",
4219 page_index->subpage, page_code);
4223 case SMS_INFO_EXCEPTIONS_PAGE: {
4224 switch (page_index->subpage) {
4225 case SMS_SUBPAGE_PAGE_0:
4226 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4228 sizeof(ie_page_default));
4229 memcpy(&lun->mode_pages.ie_page[
4230 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4231 sizeof(ie_page_changeable));
4232 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4234 sizeof(ie_page_default));
4235 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4237 sizeof(ie_page_default));
4238 page_index->page_data =
4239 (uint8_t *)lun->mode_pages.ie_page;
4242 struct ctl_logical_block_provisioning_page *page;
4244 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4246 sizeof(lbp_page_default));
4247 memcpy(&lun->mode_pages.lbp_page[
4248 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4249 sizeof(lbp_page_changeable));
4250 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4252 sizeof(lbp_page_default));
4253 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4254 value = ctl_get_opt(&lun->be_lun->options,
4256 if (value != NULL &&
4257 ctl_expand_number(value, &ival) == 0) {
4258 page->descr[0].flags |= SLBPPD_ENABLED |
4260 if (lun->be_lun->blocksize)
4261 ival /= lun->be_lun->blocksize;
4264 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4265 page->descr[0].count);
4267 value = ctl_get_opt(&lun->be_lun->options,
4269 if (value != NULL &&
4270 ctl_expand_number(value, &ival) == 0) {
4271 page->descr[1].flags |= SLBPPD_ENABLED |
4273 if (lun->be_lun->blocksize)
4274 ival /= lun->be_lun->blocksize;
4277 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4278 page->descr[1].count);
4280 value = ctl_get_opt(&lun->be_lun->options,
4281 "pool-avail-threshold");
4282 if (value != NULL &&
4283 ctl_expand_number(value, &ival) == 0) {
4284 page->descr[2].flags |= SLBPPD_ENABLED |
4286 if (lun->be_lun->blocksize)
4287 ival /= lun->be_lun->blocksize;
4290 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4291 page->descr[2].count);
4293 value = ctl_get_opt(&lun->be_lun->options,
4294 "pool-used-threshold");
4295 if (value != NULL &&
4296 ctl_expand_number(value, &ival) == 0) {
4297 page->descr[3].flags |= SLBPPD_ENABLED |
4299 if (lun->be_lun->blocksize)
4300 ival /= lun->be_lun->blocksize;
4303 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4304 page->descr[3].count);
4306 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4307 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4308 sizeof(lbp_page_default));
4309 page_index->page_data =
4310 (uint8_t *)lun->mode_pages.lbp_page;
4314 panic("subpage %#x for page %#x is incorrect!",
4315 page_index->subpage, page_code);
4319 case SMS_CDDVD_CAPS_PAGE:{
4320 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4321 ("subpage %#x for page %#x is incorrect!",
4322 page_index->subpage, page_code));
4323 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT],
4324 &cddvd_page_default,
4325 sizeof(cddvd_page_default));
4326 memcpy(&lun->mode_pages.cddvd_page[
4327 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable,
4328 sizeof(cddvd_page_changeable));
4329 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4330 &cddvd_page_default,
4331 sizeof(cddvd_page_default));
4332 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT],
4333 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4334 sizeof(cddvd_page_default));
4335 page_index->page_data =
4336 (uint8_t *)lun->mode_pages.cddvd_page;
4340 panic("invalid page code value %#x", page_code);
4344 return (CTL_RETVAL_COMPLETE);
4348 ctl_init_log_page_index(struct ctl_lun *lun)
4350 struct ctl_page_index *page_index;
4353 memcpy(&lun->log_pages.index, log_page_index_template,
4354 sizeof(log_page_index_template));
4357 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4359 page_index = &lun->log_pages.index[i];
4360 if (lun->be_lun->lun_type == T_DIRECT &&
4361 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4363 if (lun->be_lun->lun_type == T_PROCESSOR &&
4364 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4366 if (lun->be_lun->lun_type == T_CDROM &&
4367 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4370 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4371 lun->backend->lun_attr == NULL)
4374 if (page_index->page_code != prev) {
4375 lun->log_pages.pages_page[j] = page_index->page_code;
4376 prev = page_index->page_code;
4379 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4380 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4383 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4384 lun->log_pages.index[0].page_len = j;
4385 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4386 lun->log_pages.index[1].page_len = k * 2;
4387 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4388 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4389 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page;
4390 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page);
4391 lun->log_pages.index[4].page_data = (uint8_t *)&lun->log_pages.ie_page;
4392 lun->log_pages.index[4].page_len = sizeof(lun->log_pages.ie_page);
4394 return (CTL_RETVAL_COMPLETE);
4398 hex2bin(const char *str, uint8_t *buf, int buf_size)
4403 memset(buf, 0, buf_size);
4404 while (isspace(str[0]))
4406 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4409 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4410 while (str[i] == '-') /* Skip dashes in UUIDs. */
4415 else if (isalpha(c))
4416 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4422 buf[i / 2] |= (c << 4);
4426 return ((i + 1) / 2);
4433 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4434 * wants us to allocate the LUN and he can block.
4435 * - ctl_softc is always set
4436 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4438 * Returns 0 for success, non-zero (errno) for failure.
4441 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4442 struct ctl_be_lun *const be_lun)
4444 struct ctl_lun *nlun, *lun;
4445 struct scsi_vpd_id_descriptor *desc;
4446 struct scsi_vpd_id_t10 *t10id;
4447 const char *eui, *naa, *scsiname, *uuid, *vendor, *value;
4448 int lun_number, lun_malloced;
4449 int devidlen, idlen1, idlen2 = 0, len;
4455 * We currently only support Direct Access or Processor LUN types.
4457 switch (be_lun->lun_type) {
4465 be_lun->lun_config_status(be_lun->be_lun,
4466 CTL_LUN_CONFIG_FAILURE);
4469 if (ctl_lun == NULL) {
4470 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4477 memset(lun, 0, sizeof(*lun));
4479 lun->flags = CTL_LUN_MALLOCED;
4481 /* Generate LUN ID. */
4482 devidlen = max(CTL_DEVID_MIN_LEN,
4483 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4484 idlen1 = sizeof(*t10id) + devidlen;
4485 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4486 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4487 if (scsiname != NULL) {
4488 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4489 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4491 eui = ctl_get_opt(&be_lun->options, "eui");
4493 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4495 naa = ctl_get_opt(&be_lun->options, "naa");
4497 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4499 uuid = ctl_get_opt(&be_lun->options, "uuid");
4501 len += sizeof(struct scsi_vpd_id_descriptor) + 18;
4503 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4504 M_CTL, M_WAITOK | M_ZERO);
4505 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4506 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4507 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4508 desc->length = idlen1;
4509 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4510 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4511 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4512 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4514 strncpy(t10id->vendor, vendor,
4515 min(sizeof(t10id->vendor), strlen(vendor)));
4517 strncpy((char *)t10id->vendor_spec_id,
4518 (char *)be_lun->device_id, devidlen);
4519 if (scsiname != NULL) {
4520 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4522 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4523 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4524 SVPD_ID_TYPE_SCSI_NAME;
4525 desc->length = idlen2;
4526 strlcpy(desc->identifier, scsiname, idlen2);
4529 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4531 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4532 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4534 desc->length = hex2bin(eui, desc->identifier, 16);
4535 desc->length = desc->length > 12 ? 16 :
4536 (desc->length > 8 ? 12 : 8);
4537 len -= 16 - desc->length;
4540 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4542 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4543 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4545 desc->length = hex2bin(naa, desc->identifier, 16);
4546 desc->length = desc->length > 8 ? 16 : 8;
4547 len -= 16 - desc->length;
4550 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4552 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4553 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4555 desc->identifier[0] = 0x10;
4556 hex2bin(uuid, &desc->identifier[2], 16);
4559 lun->lun_devid->len = len;
4561 mtx_lock(&ctl_softc->ctl_lock);
4563 * See if the caller requested a particular LUN number. If so, see
4564 * if it is available. Otherwise, allocate the first available LUN.
4566 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4567 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4568 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4569 mtx_unlock(&ctl_softc->ctl_lock);
4570 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4571 printf("ctl: requested LUN ID %d is higher "
4572 "than CTL_MAX_LUNS - 1 (%d)\n",
4573 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4576 * XXX KDM return an error, or just assign
4577 * another LUN ID in this case??
4579 printf("ctl: requested LUN ID %d is already "
4580 "in use\n", be_lun->req_lun_id);
4583 free(lun->lun_devid, M_CTL);
4584 if (lun->flags & CTL_LUN_MALLOCED)
4586 be_lun->lun_config_status(be_lun->be_lun,
4587 CTL_LUN_CONFIG_FAILURE);
4590 lun_number = be_lun->req_lun_id;
4592 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, CTL_MAX_LUNS);
4593 if (lun_number == -1) {
4594 mtx_unlock(&ctl_softc->ctl_lock);
4595 printf("ctl: can't allocate LUN, out of LUNs\n");
4599 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4600 mtx_unlock(&ctl_softc->ctl_lock);
4602 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4603 lun->lun = lun_number;
4604 lun->be_lun = be_lun;
4606 * The processor LUN is always enabled. Disk LUNs come on line
4607 * disabled, and must be enabled by the backend.
4609 lun->flags |= CTL_LUN_DISABLED;
4610 lun->backend = be_lun->be;
4611 be_lun->ctl_lun = lun;
4612 be_lun->lun_id = lun_number;
4613 atomic_add_int(&be_lun->be->num_luns, 1);
4614 if (be_lun->flags & CTL_LUN_FLAG_EJECTED)
4615 lun->flags |= CTL_LUN_EJECTED;
4616 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA)
4617 lun->flags |= CTL_LUN_NO_MEDIA;
4618 if (be_lun->flags & CTL_LUN_FLAG_STOPPED)
4619 lun->flags |= CTL_LUN_STOPPED;
4621 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4622 lun->flags |= CTL_LUN_PRIMARY_SC;
4624 value = ctl_get_opt(&be_lun->options, "removable");
4625 if (value != NULL) {
4626 if (strcmp(value, "on") == 0)
4627 lun->flags |= CTL_LUN_REMOVABLE;
4628 } else if (be_lun->lun_type == T_CDROM)
4629 lun->flags |= CTL_LUN_REMOVABLE;
4631 lun->ctl_softc = ctl_softc;
4633 lun->last_busy = getsbinuptime();
4635 TAILQ_INIT(&lun->ooa_queue);
4636 TAILQ_INIT(&lun->blocked_queue);
4637 STAILQ_INIT(&lun->error_list);
4638 lun->ie_reported = 1;
4639 callout_init_mtx(&lun->ie_callout, &lun->lun_lock, 0);
4640 ctl_tpc_lun_init(lun);
4641 if (lun->flags & CTL_LUN_REMOVABLE) {
4642 lun->prevent = malloc((CTL_MAX_INITIATORS + 31) / 32 * 4,
4647 * Initialize the mode and log page index.
4649 ctl_init_page_index(lun);
4650 ctl_init_log_page_index(lun);
4652 /* Setup statistics gathering */
4653 #ifdef CTL_LEGACY_STATS
4654 lun->legacy_stats.device_type = be_lun->lun_type;
4655 lun->legacy_stats.lun_number = lun_number;
4656 lun->legacy_stats.blocksize = be_lun->blocksize;
4657 if (be_lun->blocksize == 0)
4658 lun->legacy_stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4659 for (len = 0; len < CTL_MAX_PORTS; len++)
4660 lun->legacy_stats.ports[len].targ_port = len;
4661 #endif /* CTL_LEGACY_STATS */
4662 lun->stats.item = lun_number;
4665 * Now, before we insert this lun on the lun list, set the lun
4666 * inventory changed UA for all other luns.
4668 mtx_lock(&ctl_softc->ctl_lock);
4669 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4670 mtx_lock(&nlun->lun_lock);
4671 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4672 mtx_unlock(&nlun->lun_lock);
4674 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4675 ctl_softc->ctl_luns[lun_number] = lun;
4676 ctl_softc->num_luns++;
4677 mtx_unlock(&ctl_softc->ctl_lock);
4679 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4686 * - LUN has already been marked invalid and any pending I/O has been taken
4690 ctl_free_lun(struct ctl_lun *lun)
4692 struct ctl_softc *softc = lun->ctl_softc;
4693 struct ctl_lun *nlun;
4696 mtx_assert(&softc->ctl_lock, MA_OWNED);
4698 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4700 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4702 softc->ctl_luns[lun->lun] = NULL;
4704 if (!TAILQ_EMPTY(&lun->ooa_queue))
4705 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4710 * Tell the backend to free resources, if this LUN has a backend.
4712 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4713 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4715 lun->ie_reportcnt = UINT32_MAX;
4716 callout_drain(&lun->ie_callout);
4718 ctl_tpc_lun_shutdown(lun);
4719 mtx_destroy(&lun->lun_lock);
4720 free(lun->lun_devid, M_CTL);
4721 for (i = 0; i < CTL_MAX_PORTS; i++)
4722 free(lun->pending_ua[i], M_CTL);
4723 for (i = 0; i < CTL_MAX_PORTS; i++)
4724 free(lun->pr_keys[i], M_CTL);
4725 free(lun->write_buffer, M_CTL);
4726 free(lun->prevent, M_CTL);
4727 if (lun->flags & CTL_LUN_MALLOCED)
4730 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4731 mtx_lock(&nlun->lun_lock);
4732 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4733 mtx_unlock(&nlun->lun_lock);
4740 ctl_create_lun(struct ctl_be_lun *be_lun)
4744 * ctl_alloc_lun() should handle all potential failure cases.
4746 ctl_alloc_lun(control_softc, NULL, be_lun);
4750 ctl_add_lun(struct ctl_be_lun *be_lun)
4752 struct ctl_softc *softc = control_softc;
4754 mtx_lock(&softc->ctl_lock);
4755 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links);
4756 mtx_unlock(&softc->ctl_lock);
4757 wakeup(&softc->pending_lun_queue);
4763 ctl_enable_lun(struct ctl_be_lun *be_lun)
4765 struct ctl_softc *softc;
4766 struct ctl_port *port, *nport;
4767 struct ctl_lun *lun;
4770 lun = (struct ctl_lun *)be_lun->ctl_lun;
4771 softc = lun->ctl_softc;
4773 mtx_lock(&softc->ctl_lock);
4774 mtx_lock(&lun->lun_lock);
4775 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4777 * eh? Why did we get called if the LUN is already
4780 mtx_unlock(&lun->lun_lock);
4781 mtx_unlock(&softc->ctl_lock);
4784 lun->flags &= ~CTL_LUN_DISABLED;
4785 mtx_unlock(&lun->lun_lock);
4787 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) {
4788 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4789 port->lun_map != NULL || port->lun_enable == NULL)
4793 * Drop the lock while we call the FETD's enable routine.
4794 * This can lead to a callback into CTL (at least in the
4795 * case of the internal initiator frontend.
4797 mtx_unlock(&softc->ctl_lock);
4798 retval = port->lun_enable(port->targ_lun_arg, lun->lun);
4799 mtx_lock(&softc->ctl_lock);
4801 printf("%s: FETD %s port %d returned error "
4802 "%d for lun_enable on lun %jd\n",
4803 __func__, port->port_name, port->targ_port,
4804 retval, (intmax_t)lun->lun);
4808 mtx_unlock(&softc->ctl_lock);
4809 ctl_isc_announce_lun(lun);
4815 ctl_disable_lun(struct ctl_be_lun *be_lun)
4817 struct ctl_softc *softc;
4818 struct ctl_port *port;
4819 struct ctl_lun *lun;
4822 lun = (struct ctl_lun *)be_lun->ctl_lun;
4823 softc = lun->ctl_softc;
4825 mtx_lock(&softc->ctl_lock);
4826 mtx_lock(&lun->lun_lock);
4827 if (lun->flags & CTL_LUN_DISABLED) {
4828 mtx_unlock(&lun->lun_lock);
4829 mtx_unlock(&softc->ctl_lock);
4832 lun->flags |= CTL_LUN_DISABLED;
4833 mtx_unlock(&lun->lun_lock);
4835 STAILQ_FOREACH(port, &softc->port_list, links) {
4836 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4837 port->lun_map != NULL || port->lun_disable == NULL)
4841 * Drop the lock before we call the frontend's disable
4842 * routine, to avoid lock order reversals.
4844 * XXX KDM what happens if the frontend list changes while
4845 * we're traversing it? It's unlikely, but should be handled.
4847 mtx_unlock(&softc->ctl_lock);
4848 retval = port->lun_disable(port->targ_lun_arg, lun->lun);
4849 mtx_lock(&softc->ctl_lock);
4851 printf("%s: FETD %s port %d returned error "
4852 "%d for lun_disable on lun %jd\n",
4853 __func__, port->port_name, port->targ_port,
4854 retval, (intmax_t)lun->lun);
4858 mtx_unlock(&softc->ctl_lock);
4859 ctl_isc_announce_lun(lun);
4865 ctl_start_lun(struct ctl_be_lun *be_lun)
4867 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4869 mtx_lock(&lun->lun_lock);
4870 lun->flags &= ~CTL_LUN_STOPPED;
4871 mtx_unlock(&lun->lun_lock);
4876 ctl_stop_lun(struct ctl_be_lun *be_lun)
4878 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4880 mtx_lock(&lun->lun_lock);
4881 lun->flags |= CTL_LUN_STOPPED;
4882 mtx_unlock(&lun->lun_lock);
4887 ctl_lun_no_media(struct ctl_be_lun *be_lun)
4889 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4891 mtx_lock(&lun->lun_lock);
4892 lun->flags |= CTL_LUN_NO_MEDIA;
4893 mtx_unlock(&lun->lun_lock);
4898 ctl_lun_has_media(struct ctl_be_lun *be_lun)
4900 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4901 union ctl_ha_msg msg;
4903 mtx_lock(&lun->lun_lock);
4904 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED);
4905 if (lun->flags & CTL_LUN_REMOVABLE)
4906 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE);
4907 mtx_unlock(&lun->lun_lock);
4908 if ((lun->flags & CTL_LUN_REMOVABLE) &&
4909 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4910 bzero(&msg.ua, sizeof(msg.ua));
4911 msg.hdr.msg_type = CTL_MSG_UA;
4912 msg.hdr.nexus.initid = -1;
4913 msg.hdr.nexus.targ_port = -1;
4914 msg.hdr.nexus.targ_lun = lun->lun;
4915 msg.hdr.nexus.targ_mapped_lun = lun->lun;
4918 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE;
4919 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4926 ctl_lun_ejected(struct ctl_be_lun *be_lun)
4928 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4930 mtx_lock(&lun->lun_lock);
4931 lun->flags |= CTL_LUN_EJECTED;
4932 mtx_unlock(&lun->lun_lock);
4937 ctl_lun_primary(struct ctl_be_lun *be_lun)
4939 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4941 mtx_lock(&lun->lun_lock);
4942 lun->flags |= CTL_LUN_PRIMARY_SC;
4943 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4944 mtx_unlock(&lun->lun_lock);
4945 ctl_isc_announce_lun(lun);
4950 ctl_lun_secondary(struct ctl_be_lun *be_lun)
4952 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4954 mtx_lock(&lun->lun_lock);
4955 lun->flags &= ~CTL_LUN_PRIMARY_SC;
4956 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4957 mtx_unlock(&lun->lun_lock);
4958 ctl_isc_announce_lun(lun);
4963 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4965 struct ctl_softc *softc;
4966 struct ctl_lun *lun;
4968 lun = (struct ctl_lun *)be_lun->ctl_lun;
4969 softc = lun->ctl_softc;
4971 mtx_lock(&lun->lun_lock);
4974 * The LUN needs to be disabled before it can be marked invalid.
4976 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4977 mtx_unlock(&lun->lun_lock);
4981 * Mark the LUN invalid.
4983 lun->flags |= CTL_LUN_INVALID;
4986 * If there is nothing in the OOA queue, go ahead and free the LUN.
4987 * If we have something in the OOA queue, we'll free it when the
4988 * last I/O completes.
4990 if (TAILQ_EMPTY(&lun->ooa_queue)) {
4991 mtx_unlock(&lun->lun_lock);
4992 mtx_lock(&softc->ctl_lock);
4994 mtx_unlock(&softc->ctl_lock);
4996 mtx_unlock(&lun->lun_lock);
5002 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
5004 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5005 union ctl_ha_msg msg;
5007 mtx_lock(&lun->lun_lock);
5008 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE);
5009 mtx_unlock(&lun->lun_lock);
5010 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
5011 /* Send msg to other side. */
5012 bzero(&msg.ua, sizeof(msg.ua));
5013 msg.hdr.msg_type = CTL_MSG_UA;
5014 msg.hdr.nexus.initid = -1;
5015 msg.hdr.nexus.targ_port = -1;
5016 msg.hdr.nexus.targ_lun = lun->lun;
5017 msg.hdr.nexus.targ_mapped_lun = lun->lun;
5020 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE;
5021 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
5027 * Backend "memory move is complete" callback for requests that never
5028 * make it down to say RAIDCore's configuration code.
5031 ctl_config_move_done(union ctl_io *io)
5035 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
5036 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
5037 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
5039 if ((io->io_hdr.port_status != 0) &&
5040 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5041 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5042 ctl_set_internal_failure(&io->scsiio, /*sks_valid*/ 1,
5043 /*retry_count*/ io->io_hdr.port_status);
5044 } else if (io->scsiio.kern_data_resid != 0 &&
5045 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT &&
5046 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5047 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5048 ctl_set_invalid_field_ciu(&io->scsiio);
5051 if (ctl_debug & CTL_DEBUG_CDB_DATA)
5053 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
5054 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5055 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
5056 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5058 * XXX KDM just assuming a single pointer here, and not a
5059 * S/G list. If we start using S/G lists for config data,
5060 * we'll need to know how to clean them up here as well.
5062 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5063 free(io->scsiio.kern_data_ptr, M_CTL);
5065 retval = CTL_RETVAL_COMPLETE;
5068 * XXX KDM now we need to continue data movement. Some
5070 * - call ctl_scsiio() again? We don't do this for data
5071 * writes, because for those at least we know ahead of
5072 * time where the write will go and how long it is. For
5073 * config writes, though, that information is largely
5074 * contained within the write itself, thus we need to
5075 * parse out the data again.
5077 * - Call some other function once the data is in?
5081 * XXX KDM call ctl_scsiio() again for now, and check flag
5082 * bits to see whether we're allocated or not.
5084 retval = ctl_scsiio(&io->scsiio);
5090 * This gets called by a backend driver when it is done with a
5091 * data_submit method.
5094 ctl_data_submit_done(union ctl_io *io)
5097 * If the IO_CONT flag is set, we need to call the supplied
5098 * function to continue processing the I/O, instead of completing
5101 * If there is an error, though, we don't want to keep processing.
5102 * Instead, just send status back to the initiator.
5104 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5105 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5106 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5107 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5108 io->scsiio.io_cont(io);
5115 * This gets called by a backend driver when it is done with a
5116 * configuration write.
5119 ctl_config_write_done(union ctl_io *io)
5124 * If the IO_CONT flag is set, we need to call the supplied
5125 * function to continue processing the I/O, instead of completing
5128 * If there is an error, though, we don't want to keep processing.
5129 * Instead, just send status back to the initiator.
5131 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5132 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5133 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5134 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5135 io->scsiio.io_cont(io);
5139 * Since a configuration write can be done for commands that actually
5140 * have data allocated, like write buffer, and commands that have
5141 * no data, like start/stop unit, we need to check here.
5143 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5144 buf = io->scsiio.kern_data_ptr;
5153 ctl_config_read_done(union ctl_io *io)
5158 * If there is some error -- we are done, skip data transfer.
5160 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
5161 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5162 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
5163 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5164 buf = io->scsiio.kern_data_ptr;
5174 * If the IO_CONT flag is set, we need to call the supplied
5175 * function to continue processing the I/O, instead of completing
5178 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
5179 io->scsiio.io_cont(io);
5187 * SCSI release command.
5190 ctl_scsi_release(struct ctl_scsiio *ctsio)
5192 struct ctl_lun *lun = CTL_LUN(ctsio);
5195 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5197 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5200 * XXX KDM right now, we only support LUN reservation. We don't
5201 * support 3rd party reservations, or extent reservations, which
5202 * might actually need the parameter list. If we've gotten this
5203 * far, we've got a LUN reservation. Anything else got kicked out
5204 * above. So, according to SPC, ignore the length.
5207 mtx_lock(&lun->lun_lock);
5210 * According to SPC, it is not an error for an intiator to attempt
5211 * to release a reservation on a LUN that isn't reserved, or that
5212 * is reserved by another initiator. The reservation can only be
5213 * released, though, by the initiator who made it or by one of
5214 * several reset type events.
5216 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5217 lun->flags &= ~CTL_LUN_RESERVED;
5219 mtx_unlock(&lun->lun_lock);
5221 ctl_set_success(ctsio);
5222 ctl_done((union ctl_io *)ctsio);
5223 return (CTL_RETVAL_COMPLETE);
5227 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5229 struct ctl_lun *lun = CTL_LUN(ctsio);
5232 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5234 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5237 * XXX KDM right now, we only support LUN reservation. We don't
5238 * support 3rd party reservations, or extent reservations, which
5239 * might actually need the parameter list. If we've gotten this
5240 * far, we've got a LUN reservation. Anything else got kicked out
5241 * above. So, according to SPC, ignore the length.
5244 mtx_lock(&lun->lun_lock);
5245 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5246 ctl_set_reservation_conflict(ctsio);
5250 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */
5251 if (lun->flags & CTL_LUN_PR_RESERVED) {
5252 ctl_set_success(ctsio);
5256 lun->flags |= CTL_LUN_RESERVED;
5257 lun->res_idx = residx;
5258 ctl_set_success(ctsio);
5261 mtx_unlock(&lun->lun_lock);
5262 ctl_done((union ctl_io *)ctsio);
5263 return (CTL_RETVAL_COMPLETE);
5267 ctl_start_stop(struct ctl_scsiio *ctsio)
5269 struct ctl_lun *lun = CTL_LUN(ctsio);
5270 struct scsi_start_stop_unit *cdb;
5273 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5275 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5277 if ((cdb->how & SSS_PC_MASK) == 0) {
5278 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
5279 (cdb->how & SSS_START) == 0) {
5282 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5283 if (ctl_get_prkey(lun, residx) == 0 ||
5284 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) {
5286 ctl_set_reservation_conflict(ctsio);
5287 ctl_done((union ctl_io *)ctsio);
5288 return (CTL_RETVAL_COMPLETE);
5292 if ((cdb->how & SSS_LOEJ) &&
5293 (lun->flags & CTL_LUN_REMOVABLE) == 0) {
5294 ctl_set_invalid_field(ctsio,
5300 ctl_done((union ctl_io *)ctsio);
5301 return (CTL_RETVAL_COMPLETE);
5304 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) &&
5305 lun->prevent_count > 0) {
5306 /* "Medium removal prevented" */
5307 ctl_set_sense(ctsio, /*current_error*/ 1,
5308 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ?
5309 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST,
5310 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE);
5311 ctl_done((union ctl_io *)ctsio);
5312 return (CTL_RETVAL_COMPLETE);
5316 retval = lun->backend->config_write((union ctl_io *)ctsio);
5321 ctl_prevent_allow(struct ctl_scsiio *ctsio)
5323 struct ctl_lun *lun = CTL_LUN(ctsio);
5324 struct scsi_prevent *cdb;
5328 CTL_DEBUG_PRINT(("ctl_prevent_allow\n"));
5330 cdb = (struct scsi_prevent *)ctsio->cdb;
5332 if ((lun->flags & CTL_LUN_REMOVABLE) == 0 || lun->prevent == NULL) {
5333 ctl_set_invalid_opcode(ctsio);
5334 ctl_done((union ctl_io *)ctsio);
5335 return (CTL_RETVAL_COMPLETE);
5338 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5339 mtx_lock(&lun->lun_lock);
5340 if ((cdb->how & PR_PREVENT) &&
5341 ctl_is_set(lun->prevent, initidx) == 0) {
5342 ctl_set_mask(lun->prevent, initidx);
5343 lun->prevent_count++;
5344 } else if ((cdb->how & PR_PREVENT) == 0 &&
5345 ctl_is_set(lun->prevent, initidx)) {
5346 ctl_clear_mask(lun->prevent, initidx);
5347 lun->prevent_count--;
5349 mtx_unlock(&lun->lun_lock);
5350 retval = lun->backend->config_write((union ctl_io *)ctsio);
5355 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5356 * we don't really do anything with the LBA and length fields if the user
5357 * passes them in. Instead we'll just flush out the cache for the entire
5361 ctl_sync_cache(struct ctl_scsiio *ctsio)
5363 struct ctl_lun *lun = CTL_LUN(ctsio);
5364 struct ctl_lba_len_flags *lbalen;
5365 uint64_t starting_lba;
5366 uint32_t block_count;
5370 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5374 switch (ctsio->cdb[0]) {
5375 case SYNCHRONIZE_CACHE: {
5376 struct scsi_sync_cache *cdb;
5377 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5379 starting_lba = scsi_4btoul(cdb->begin_lba);
5380 block_count = scsi_2btoul(cdb->lb_count);
5384 case SYNCHRONIZE_CACHE_16: {
5385 struct scsi_sync_cache_16 *cdb;
5386 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5388 starting_lba = scsi_8btou64(cdb->begin_lba);
5389 block_count = scsi_4btoul(cdb->lb_count);
5394 ctl_set_invalid_opcode(ctsio);
5395 ctl_done((union ctl_io *)ctsio);
5397 break; /* NOTREACHED */
5401 * We check the LBA and length, but don't do anything with them.
5402 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5403 * get flushed. This check will just help satisfy anyone who wants
5404 * to see an error for an out of range LBA.
5406 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5407 ctl_set_lba_out_of_range(ctsio,
5408 MAX(starting_lba, lun->be_lun->maxlba + 1));
5409 ctl_done((union ctl_io *)ctsio);
5413 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5414 lbalen->lba = starting_lba;
5415 lbalen->len = block_count;
5416 lbalen->flags = byte2;
5417 retval = lun->backend->config_write((union ctl_io *)ctsio);
5424 ctl_format(struct ctl_scsiio *ctsio)
5426 struct scsi_format *cdb;
5427 int length, defect_list_len;
5429 CTL_DEBUG_PRINT(("ctl_format\n"));
5431 cdb = (struct scsi_format *)ctsio->cdb;
5434 if (cdb->byte2 & SF_FMTDATA) {
5435 if (cdb->byte2 & SF_LONGLIST)
5436 length = sizeof(struct scsi_format_header_long);
5438 length = sizeof(struct scsi_format_header_short);
5441 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5443 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5444 ctsio->kern_data_len = length;
5445 ctsio->kern_total_len = length;
5446 ctsio->kern_rel_offset = 0;
5447 ctsio->kern_sg_entries = 0;
5448 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5449 ctsio->be_move_done = ctl_config_move_done;
5450 ctl_datamove((union ctl_io *)ctsio);
5452 return (CTL_RETVAL_COMPLETE);
5455 defect_list_len = 0;
5457 if (cdb->byte2 & SF_FMTDATA) {
5458 if (cdb->byte2 & SF_LONGLIST) {
5459 struct scsi_format_header_long *header;
5461 header = (struct scsi_format_header_long *)
5462 ctsio->kern_data_ptr;
5464 defect_list_len = scsi_4btoul(header->defect_list_len);
5465 if (defect_list_len != 0) {
5466 ctl_set_invalid_field(ctsio,
5475 struct scsi_format_header_short *header;
5477 header = (struct scsi_format_header_short *)
5478 ctsio->kern_data_ptr;
5480 defect_list_len = scsi_2btoul(header->defect_list_len);
5481 if (defect_list_len != 0) {
5482 ctl_set_invalid_field(ctsio,
5493 ctl_set_success(ctsio);
5496 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5497 free(ctsio->kern_data_ptr, M_CTL);
5498 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5501 ctl_done((union ctl_io *)ctsio);
5502 return (CTL_RETVAL_COMPLETE);
5506 ctl_read_buffer(struct ctl_scsiio *ctsio)
5508 struct ctl_lun *lun = CTL_LUN(ctsio);
5509 uint64_t buffer_offset;
5512 static uint8_t descr[4];
5513 static uint8_t echo_descr[4] = { 0 };
5515 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5517 switch (ctsio->cdb[0]) {
5519 struct scsi_read_buffer *cdb;
5521 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5522 buffer_offset = scsi_3btoul(cdb->offset);
5523 len = scsi_3btoul(cdb->length);
5527 case READ_BUFFER_16: {
5528 struct scsi_read_buffer_16 *cdb;
5530 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb;
5531 buffer_offset = scsi_8btou64(cdb->offset);
5532 len = scsi_4btoul(cdb->length);
5536 default: /* This shouldn't happen. */
5537 ctl_set_invalid_opcode(ctsio);
5538 ctl_done((union ctl_io *)ctsio);
5539 return (CTL_RETVAL_COMPLETE);
5542 if (buffer_offset > CTL_WRITE_BUFFER_SIZE ||
5543 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5544 ctl_set_invalid_field(ctsio,
5550 ctl_done((union ctl_io *)ctsio);
5551 return (CTL_RETVAL_COMPLETE);
5554 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5556 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5557 ctsio->kern_data_ptr = descr;
5558 len = min(len, sizeof(descr));
5559 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5560 ctsio->kern_data_ptr = echo_descr;
5561 len = min(len, sizeof(echo_descr));
5563 if (lun->write_buffer == NULL) {
5564 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5567 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5569 ctsio->kern_data_len = len;
5570 ctsio->kern_total_len = len;
5571 ctsio->kern_rel_offset = 0;
5572 ctsio->kern_sg_entries = 0;
5573 ctl_set_success(ctsio);
5574 ctsio->be_move_done = ctl_config_move_done;
5575 ctl_datamove((union ctl_io *)ctsio);
5576 return (CTL_RETVAL_COMPLETE);
5580 ctl_write_buffer(struct ctl_scsiio *ctsio)
5582 struct ctl_lun *lun = CTL_LUN(ctsio);
5583 struct scsi_write_buffer *cdb;
5584 int buffer_offset, len;
5586 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5588 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5590 len = scsi_3btoul(cdb->length);
5591 buffer_offset = scsi_3btoul(cdb->offset);
5593 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5594 ctl_set_invalid_field(ctsio,
5600 ctl_done((union ctl_io *)ctsio);
5601 return (CTL_RETVAL_COMPLETE);
5605 * If we've got a kernel request that hasn't been malloced yet,
5606 * malloc it and tell the caller the data buffer is here.
5608 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5609 if (lun->write_buffer == NULL) {
5610 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5613 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5614 ctsio->kern_data_len = len;
5615 ctsio->kern_total_len = len;
5616 ctsio->kern_rel_offset = 0;
5617 ctsio->kern_sg_entries = 0;
5618 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5619 ctsio->be_move_done = ctl_config_move_done;
5620 ctl_datamove((union ctl_io *)ctsio);
5622 return (CTL_RETVAL_COMPLETE);
5625 ctl_set_success(ctsio);
5626 ctl_done((union ctl_io *)ctsio);
5627 return (CTL_RETVAL_COMPLETE);
5631 ctl_write_same(struct ctl_scsiio *ctsio)
5633 struct ctl_lun *lun = CTL_LUN(ctsio);
5634 struct ctl_lba_len_flags *lbalen;
5636 uint32_t num_blocks;
5640 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5642 switch (ctsio->cdb[0]) {
5643 case WRITE_SAME_10: {
5644 struct scsi_write_same_10 *cdb;
5646 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5648 lba = scsi_4btoul(cdb->addr);
5649 num_blocks = scsi_2btoul(cdb->length);
5653 case WRITE_SAME_16: {
5654 struct scsi_write_same_16 *cdb;
5656 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5658 lba = scsi_8btou64(cdb->addr);
5659 num_blocks = scsi_4btoul(cdb->length);
5665 * We got a command we don't support. This shouldn't
5666 * happen, commands should be filtered out above us.
5668 ctl_set_invalid_opcode(ctsio);
5669 ctl_done((union ctl_io *)ctsio);
5671 return (CTL_RETVAL_COMPLETE);
5672 break; /* NOTREACHED */
5675 /* ANCHOR flag can be used only together with UNMAP */
5676 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) {
5677 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5678 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5679 ctl_done((union ctl_io *)ctsio);
5680 return (CTL_RETVAL_COMPLETE);
5684 * The first check is to make sure we're in bounds, the second
5685 * check is to catch wrap-around problems. If the lba + num blocks
5686 * is less than the lba, then we've wrapped around and the block
5687 * range is invalid anyway.
5689 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5690 || ((lba + num_blocks) < lba)) {
5691 ctl_set_lba_out_of_range(ctsio,
5692 MAX(lba, lun->be_lun->maxlba + 1));
5693 ctl_done((union ctl_io *)ctsio);
5694 return (CTL_RETVAL_COMPLETE);
5697 /* Zero number of blocks means "to the last logical block" */
5698 if (num_blocks == 0) {
5699 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5700 ctl_set_invalid_field(ctsio,
5706 ctl_done((union ctl_io *)ctsio);
5707 return (CTL_RETVAL_COMPLETE);
5709 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5712 len = lun->be_lun->blocksize;
5715 * If we've got a kernel request that hasn't been malloced yet,
5716 * malloc it and tell the caller the data buffer is here.
5718 if ((byte2 & SWS_NDOB) == 0 &&
5719 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5720 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5721 ctsio->kern_data_len = len;
5722 ctsio->kern_total_len = len;
5723 ctsio->kern_rel_offset = 0;
5724 ctsio->kern_sg_entries = 0;
5725 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5726 ctsio->be_move_done = ctl_config_move_done;
5727 ctl_datamove((union ctl_io *)ctsio);
5729 return (CTL_RETVAL_COMPLETE);
5732 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5734 lbalen->len = num_blocks;
5735 lbalen->flags = byte2;
5736 retval = lun->backend->config_write((union ctl_io *)ctsio);
5742 ctl_unmap(struct ctl_scsiio *ctsio)
5744 struct ctl_lun *lun = CTL_LUN(ctsio);
5745 struct scsi_unmap *cdb;
5746 struct ctl_ptr_len_flags *ptrlen;
5747 struct scsi_unmap_header *hdr;
5748 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5750 uint32_t num_blocks;
5754 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5756 cdb = (struct scsi_unmap *)ctsio->cdb;
5757 len = scsi_2btoul(cdb->length);
5761 * If we've got a kernel request that hasn't been malloced yet,
5762 * malloc it and tell the caller the data buffer is here.
5764 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5765 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5766 ctsio->kern_data_len = len;
5767 ctsio->kern_total_len = len;
5768 ctsio->kern_rel_offset = 0;
5769 ctsio->kern_sg_entries = 0;
5770 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5771 ctsio->be_move_done = ctl_config_move_done;
5772 ctl_datamove((union ctl_io *)ctsio);
5774 return (CTL_RETVAL_COMPLETE);
5777 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5778 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5779 if (len < sizeof (*hdr) ||
5780 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5781 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5782 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5783 ctl_set_invalid_field(ctsio,
5791 len = scsi_2btoul(hdr->desc_length);
5792 buf = (struct scsi_unmap_desc *)(hdr + 1);
5793 end = buf + len / sizeof(*buf);
5796 for (range = buf; range < end; range++) {
5797 lba = scsi_8btou64(range->lba);
5798 num_blocks = scsi_4btoul(range->length);
5799 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5800 || ((lba + num_blocks) < lba)) {
5801 ctl_set_lba_out_of_range(ctsio,
5802 MAX(lba, lun->be_lun->maxlba + 1));
5803 ctl_done((union ctl_io *)ctsio);
5804 return (CTL_RETVAL_COMPLETE);
5806 if (num_blocks != 0)
5811 * Block backend can not handle zero last range.
5812 * Filter it out and return if there is nothing left.
5814 len = (uint8_t *)endnz - (uint8_t *)buf;
5816 ctl_set_success(ctsio);
5820 mtx_lock(&lun->lun_lock);
5821 ptrlen = (struct ctl_ptr_len_flags *)
5822 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5823 ptrlen->ptr = (void *)buf;
5825 ptrlen->flags = byte2;
5826 ctl_check_blocked(lun);
5827 mtx_unlock(&lun->lun_lock);
5829 retval = lun->backend->config_write((union ctl_io *)ctsio);
5833 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5834 free(ctsio->kern_data_ptr, M_CTL);
5835 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5837 ctl_done((union ctl_io *)ctsio);
5838 return (CTL_RETVAL_COMPLETE);
5842 ctl_default_page_handler(struct ctl_scsiio *ctsio,
5843 struct ctl_page_index *page_index, uint8_t *page_ptr)
5845 struct ctl_lun *lun = CTL_LUN(ctsio);
5846 uint8_t *current_cp;
5850 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5853 current_cp = (page_index->page_data + (page_index->page_len *
5856 mtx_lock(&lun->lun_lock);
5857 if (memcmp(current_cp, page_ptr, page_index->page_len)) {
5858 memcpy(current_cp, page_ptr, page_index->page_len);
5862 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5863 mtx_unlock(&lun->lun_lock);
5865 ctl_isc_announce_mode(lun,
5866 ctl_get_initindex(&ctsio->io_hdr.nexus),
5867 page_index->page_code, page_index->subpage);
5869 return (CTL_RETVAL_COMPLETE);
5873 ctl_ie_timer(void *arg)
5875 struct ctl_lun *lun = arg;
5878 if (lun->ie_asc == 0)
5881 if (lun->MODE_IE.mrie == SIEP_MRIE_UA)
5882 ctl_est_ua_all(lun, -1, CTL_UA_IE);
5884 lun->ie_reported = 0;
5886 if (lun->ie_reportcnt < scsi_4btoul(lun->MODE_IE.report_count)) {
5887 lun->ie_reportcnt++;
5888 t = scsi_4btoul(lun->MODE_IE.interval_timer);
5889 if (t == 0 || t == UINT32_MAX)
5890 t = 3000; /* 5 min */
5891 callout_schedule(&lun->ie_callout, t * hz / 10);
5896 ctl_ie_page_handler(struct ctl_scsiio *ctsio,
5897 struct ctl_page_index *page_index, uint8_t *page_ptr)
5899 struct ctl_lun *lun = CTL_LUN(ctsio);
5900 struct scsi_info_exceptions_page *pg;
5903 (void)ctl_default_page_handler(ctsio, page_index, page_ptr);
5905 pg = (struct scsi_info_exceptions_page *)page_ptr;
5906 mtx_lock(&lun->lun_lock);
5907 if (pg->info_flags & SIEP_FLAGS_TEST) {
5909 lun->ie_ascq = 0xff;
5910 if (pg->mrie == SIEP_MRIE_UA) {
5911 ctl_est_ua_all(lun, -1, CTL_UA_IE);
5912 lun->ie_reported = 1;
5914 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
5915 lun->ie_reported = -1;
5917 lun->ie_reportcnt = 1;
5918 if (lun->ie_reportcnt < scsi_4btoul(pg->report_count)) {
5919 lun->ie_reportcnt++;
5920 t = scsi_4btoul(pg->interval_timer);
5921 if (t == 0 || t == UINT32_MAX)
5922 t = 3000; /* 5 min */
5923 callout_reset(&lun->ie_callout, t * hz / 10,
5929 lun->ie_reported = 1;
5930 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
5931 lun->ie_reportcnt = UINT32_MAX;
5932 callout_stop(&lun->ie_callout);
5934 mtx_unlock(&lun->lun_lock);
5935 return (CTL_RETVAL_COMPLETE);
5939 ctl_do_mode_select(union ctl_io *io)
5941 struct ctl_lun *lun = CTL_LUN(io);
5942 struct scsi_mode_page_header *page_header;
5943 struct ctl_page_index *page_index;
5944 struct ctl_scsiio *ctsio;
5945 int page_len, page_len_offset, page_len_size;
5946 union ctl_modepage_info *modepage_info;
5947 uint16_t *len_left, *len_used;
5950 ctsio = &io->scsiio;
5954 modepage_info = (union ctl_modepage_info *)
5955 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
5956 len_left = &modepage_info->header.len_left;
5957 len_used = &modepage_info->header.len_used;
5961 page_header = (struct scsi_mode_page_header *)
5962 (ctsio->kern_data_ptr + *len_used);
5964 if (*len_left == 0) {
5965 free(ctsio->kern_data_ptr, M_CTL);
5966 ctl_set_success(ctsio);
5967 ctl_done((union ctl_io *)ctsio);
5968 return (CTL_RETVAL_COMPLETE);
5969 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
5971 free(ctsio->kern_data_ptr, M_CTL);
5972 ctl_set_param_len_error(ctsio);
5973 ctl_done((union ctl_io *)ctsio);
5974 return (CTL_RETVAL_COMPLETE);
5976 } else if ((page_header->page_code & SMPH_SPF)
5977 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
5979 free(ctsio->kern_data_ptr, M_CTL);
5980 ctl_set_param_len_error(ctsio);
5981 ctl_done((union ctl_io *)ctsio);
5982 return (CTL_RETVAL_COMPLETE);
5987 * XXX KDM should we do something with the block descriptor?
5989 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
5990 page_index = &lun->mode_pages.index[i];
5991 if (lun->be_lun->lun_type == T_DIRECT &&
5992 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
5994 if (lun->be_lun->lun_type == T_PROCESSOR &&
5995 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
5997 if (lun->be_lun->lun_type == T_CDROM &&
5998 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6001 if ((page_index->page_code & SMPH_PC_MASK) !=
6002 (page_header->page_code & SMPH_PC_MASK))
6006 * If neither page has a subpage code, then we've got a
6009 if (((page_index->page_code & SMPH_SPF) == 0)
6010 && ((page_header->page_code & SMPH_SPF) == 0)) {
6011 page_len = page_header->page_length;
6016 * If both pages have subpages, then the subpage numbers
6019 if ((page_index->page_code & SMPH_SPF)
6020 && (page_header->page_code & SMPH_SPF)) {
6021 struct scsi_mode_page_header_sp *sph;
6023 sph = (struct scsi_mode_page_header_sp *)page_header;
6024 if (page_index->subpage == sph->subpage) {
6025 page_len = scsi_2btoul(sph->page_length);
6032 * If we couldn't find the page, or if we don't have a mode select
6033 * handler for it, send back an error to the user.
6035 if ((i >= CTL_NUM_MODE_PAGES)
6036 || (page_index->select_handler == NULL)) {
6037 ctl_set_invalid_field(ctsio,
6040 /*field*/ *len_used,
6043 free(ctsio->kern_data_ptr, M_CTL);
6044 ctl_done((union ctl_io *)ctsio);
6045 return (CTL_RETVAL_COMPLETE);
6048 if (page_index->page_code & SMPH_SPF) {
6049 page_len_offset = 2;
6053 page_len_offset = 1;
6057 * If the length the initiator gives us isn't the one we specify in
6058 * the mode page header, or if they didn't specify enough data in
6059 * the CDB to avoid truncating this page, kick out the request.
6061 if (page_len != page_index->page_len - page_len_offset - page_len_size) {
6062 ctl_set_invalid_field(ctsio,
6065 /*field*/ *len_used + page_len_offset,
6068 free(ctsio->kern_data_ptr, M_CTL);
6069 ctl_done((union ctl_io *)ctsio);
6070 return (CTL_RETVAL_COMPLETE);
6072 if (*len_left < page_index->page_len) {
6073 free(ctsio->kern_data_ptr, M_CTL);
6074 ctl_set_param_len_error(ctsio);
6075 ctl_done((union ctl_io *)ctsio);
6076 return (CTL_RETVAL_COMPLETE);
6080 * Run through the mode page, checking to make sure that the bits
6081 * the user changed are actually legal for him to change.
6083 for (i = 0; i < page_index->page_len; i++) {
6084 uint8_t *user_byte, *change_mask, *current_byte;
6088 user_byte = (uint8_t *)page_header + i;
6089 change_mask = page_index->page_data +
6090 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6091 current_byte = page_index->page_data +
6092 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6095 * Check to see whether the user set any bits in this byte
6096 * that he is not allowed to set.
6098 if ((*user_byte & ~(*change_mask)) ==
6099 (*current_byte & ~(*change_mask)))
6103 * Go through bit by bit to determine which one is illegal.
6106 for (j = 7; j >= 0; j--) {
6107 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6108 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6113 ctl_set_invalid_field(ctsio,
6116 /*field*/ *len_used + i,
6119 free(ctsio->kern_data_ptr, M_CTL);
6120 ctl_done((union ctl_io *)ctsio);
6121 return (CTL_RETVAL_COMPLETE);
6125 * Decrement these before we call the page handler, since we may
6126 * end up getting called back one way or another before the handler
6127 * returns to this context.
6129 *len_left -= page_index->page_len;
6130 *len_used += page_index->page_len;
6132 retval = page_index->select_handler(ctsio, page_index,
6133 (uint8_t *)page_header);
6136 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6137 * wait until this queued command completes to finish processing
6138 * the mode page. If it returns anything other than
6139 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6140 * already set the sense information, freed the data pointer, and
6141 * completed the io for us.
6143 if (retval != CTL_RETVAL_COMPLETE)
6144 goto bailout_no_done;
6147 * If the initiator sent us more than one page, parse the next one.
6152 ctl_set_success(ctsio);
6153 free(ctsio->kern_data_ptr, M_CTL);
6154 ctl_done((union ctl_io *)ctsio);
6158 return (CTL_RETVAL_COMPLETE);
6163 ctl_mode_select(struct ctl_scsiio *ctsio)
6165 struct ctl_lun *lun = CTL_LUN(ctsio);
6166 union ctl_modepage_info *modepage_info;
6167 int bd_len, i, header_size, param_len, pf, rtd, sp;
6170 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6171 switch (ctsio->cdb[0]) {
6172 case MODE_SELECT_6: {
6173 struct scsi_mode_select_6 *cdb;
6175 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6177 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6178 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6179 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6180 param_len = cdb->length;
6181 header_size = sizeof(struct scsi_mode_header_6);
6184 case MODE_SELECT_10: {
6185 struct scsi_mode_select_10 *cdb;
6187 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6189 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6190 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6191 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6192 param_len = scsi_2btoul(cdb->length);
6193 header_size = sizeof(struct scsi_mode_header_10);
6197 ctl_set_invalid_opcode(ctsio);
6198 ctl_done((union ctl_io *)ctsio);
6199 return (CTL_RETVAL_COMPLETE);
6203 if (param_len != 0) {
6204 ctl_set_invalid_field(ctsio, /*sks_valid*/ 0,
6205 /*command*/ 1, /*field*/ 0,
6206 /*bit_valid*/ 0, /*bit*/ 0);
6207 ctl_done((union ctl_io *)ctsio);
6208 return (CTL_RETVAL_COMPLETE);
6211 /* Revert to defaults. */
6212 ctl_init_page_index(lun);
6213 mtx_lock(&lun->lun_lock);
6214 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
6215 mtx_unlock(&lun->lun_lock);
6216 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6217 ctl_isc_announce_mode(lun, -1,
6218 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
6219 lun->mode_pages.index[i].subpage);
6221 ctl_set_success(ctsio);
6222 ctl_done((union ctl_io *)ctsio);
6223 return (CTL_RETVAL_COMPLETE);
6228 * "A parameter list length of zero indicates that the Data-Out Buffer
6229 * shall be empty. This condition shall not be considered as an error."
6231 if (param_len == 0) {
6232 ctl_set_success(ctsio);
6233 ctl_done((union ctl_io *)ctsio);
6234 return (CTL_RETVAL_COMPLETE);
6238 * Since we'll hit this the first time through, prior to
6239 * allocation, we don't need to free a data buffer here.
6241 if (param_len < header_size) {
6242 ctl_set_param_len_error(ctsio);
6243 ctl_done((union ctl_io *)ctsio);
6244 return (CTL_RETVAL_COMPLETE);
6248 * Allocate the data buffer and grab the user's data. In theory,
6249 * we shouldn't have to sanity check the parameter list length here
6250 * because the maximum size is 64K. We should be able to malloc
6251 * that much without too many problems.
6253 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6254 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6255 ctsio->kern_data_len = param_len;
6256 ctsio->kern_total_len = param_len;
6257 ctsio->kern_rel_offset = 0;
6258 ctsio->kern_sg_entries = 0;
6259 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6260 ctsio->be_move_done = ctl_config_move_done;
6261 ctl_datamove((union ctl_io *)ctsio);
6263 return (CTL_RETVAL_COMPLETE);
6266 switch (ctsio->cdb[0]) {
6267 case MODE_SELECT_6: {
6268 struct scsi_mode_header_6 *mh6;
6270 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6271 bd_len = mh6->blk_desc_len;
6274 case MODE_SELECT_10: {
6275 struct scsi_mode_header_10 *mh10;
6277 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6278 bd_len = scsi_2btoul(mh10->blk_desc_len);
6282 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6285 if (param_len < (header_size + bd_len)) {
6286 free(ctsio->kern_data_ptr, M_CTL);
6287 ctl_set_param_len_error(ctsio);
6288 ctl_done((union ctl_io *)ctsio);
6289 return (CTL_RETVAL_COMPLETE);
6293 * Set the IO_CONT flag, so that if this I/O gets passed to
6294 * ctl_config_write_done(), it'll get passed back to
6295 * ctl_do_mode_select() for further processing, or completion if
6298 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6299 ctsio->io_cont = ctl_do_mode_select;
6301 modepage_info = (union ctl_modepage_info *)
6302 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6303 memset(modepage_info, 0, sizeof(*modepage_info));
6304 modepage_info->header.len_left = param_len - header_size - bd_len;
6305 modepage_info->header.len_used = header_size + bd_len;
6307 return (ctl_do_mode_select((union ctl_io *)ctsio));
6311 ctl_mode_sense(struct ctl_scsiio *ctsio)
6313 struct ctl_lun *lun = CTL_LUN(ctsio);
6314 int pc, page_code, dbd, llba, subpage;
6315 int alloc_len, page_len, header_len, total_len;
6316 struct scsi_mode_block_descr *block_desc;
6317 struct ctl_page_index *page_index;
6323 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6325 switch (ctsio->cdb[0]) {
6326 case MODE_SENSE_6: {
6327 struct scsi_mode_sense_6 *cdb;
6329 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6331 header_len = sizeof(struct scsi_mode_hdr_6);
6332 if (cdb->byte2 & SMS_DBD)
6335 header_len += sizeof(struct scsi_mode_block_descr);
6337 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6338 page_code = cdb->page & SMS_PAGE_CODE;
6339 subpage = cdb->subpage;
6340 alloc_len = cdb->length;
6343 case MODE_SENSE_10: {
6344 struct scsi_mode_sense_10 *cdb;
6346 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6348 header_len = sizeof(struct scsi_mode_hdr_10);
6350 if (cdb->byte2 & SMS_DBD)
6353 header_len += sizeof(struct scsi_mode_block_descr);
6354 if (cdb->byte2 & SMS10_LLBAA)
6356 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6357 page_code = cdb->page & SMS_PAGE_CODE;
6358 subpage = cdb->subpage;
6359 alloc_len = scsi_2btoul(cdb->length);
6363 ctl_set_invalid_opcode(ctsio);
6364 ctl_done((union ctl_io *)ctsio);
6365 return (CTL_RETVAL_COMPLETE);
6366 break; /* NOTREACHED */
6370 * We have to make a first pass through to calculate the size of
6371 * the pages that match the user's query. Then we allocate enough
6372 * memory to hold it, and actually copy the data into the buffer.
6374 switch (page_code) {
6375 case SMS_ALL_PAGES_PAGE: {
6381 * At the moment, values other than 0 and 0xff here are
6382 * reserved according to SPC-3.
6384 if ((subpage != SMS_SUBPAGE_PAGE_0)
6385 && (subpage != SMS_SUBPAGE_ALL)) {
6386 ctl_set_invalid_field(ctsio,
6392 ctl_done((union ctl_io *)ctsio);
6393 return (CTL_RETVAL_COMPLETE);
6396 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6397 page_index = &lun->mode_pages.index[i];
6399 /* Make sure the page is supported for this dev type */
6400 if (lun->be_lun->lun_type == T_DIRECT &&
6401 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6403 if (lun->be_lun->lun_type == T_PROCESSOR &&
6404 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6406 if (lun->be_lun->lun_type == T_CDROM &&
6407 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6411 * We don't use this subpage if the user didn't
6412 * request all subpages.
6414 if ((page_index->subpage != 0)
6415 && (subpage == SMS_SUBPAGE_PAGE_0))
6419 printf("found page %#x len %d\n",
6420 page_index->page_code & SMPH_PC_MASK,
6421 page_index->page_len);
6423 page_len += page_index->page_len;
6432 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6433 page_index = &lun->mode_pages.index[i];
6435 /* Make sure the page is supported for this dev type */
6436 if (lun->be_lun->lun_type == T_DIRECT &&
6437 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6439 if (lun->be_lun->lun_type == T_PROCESSOR &&
6440 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6442 if (lun->be_lun->lun_type == T_CDROM &&
6443 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6446 /* Look for the right page code */
6447 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6450 /* Look for the right subpage or the subpage wildcard*/
6451 if ((page_index->subpage != subpage)
6452 && (subpage != SMS_SUBPAGE_ALL))
6456 printf("found page %#x len %d\n",
6457 page_index->page_code & SMPH_PC_MASK,
6458 page_index->page_len);
6461 page_len += page_index->page_len;
6464 if (page_len == 0) {
6465 ctl_set_invalid_field(ctsio,
6471 ctl_done((union ctl_io *)ctsio);
6472 return (CTL_RETVAL_COMPLETE);
6478 total_len = header_len + page_len;
6480 printf("header_len = %d, page_len = %d, total_len = %d\n",
6481 header_len, page_len, total_len);
6484 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6485 ctsio->kern_sg_entries = 0;
6486 ctsio->kern_rel_offset = 0;
6487 ctsio->kern_data_len = min(total_len, alloc_len);
6488 ctsio->kern_total_len = ctsio->kern_data_len;
6490 switch (ctsio->cdb[0]) {
6491 case MODE_SENSE_6: {
6492 struct scsi_mode_hdr_6 *header;
6494 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6496 header->datalen = MIN(total_len - 1, 254);
6497 if (lun->be_lun->lun_type == T_DIRECT) {
6498 header->dev_specific = 0x10; /* DPOFUA */
6499 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6500 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6501 header->dev_specific |= 0x80; /* WP */
6504 header->block_descr_len = 0;
6506 header->block_descr_len =
6507 sizeof(struct scsi_mode_block_descr);
6508 block_desc = (struct scsi_mode_block_descr *)&header[1];
6511 case MODE_SENSE_10: {
6512 struct scsi_mode_hdr_10 *header;
6515 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6517 datalen = MIN(total_len - 2, 65533);
6518 scsi_ulto2b(datalen, header->datalen);
6519 if (lun->be_lun->lun_type == T_DIRECT) {
6520 header->dev_specific = 0x10; /* DPOFUA */
6521 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6522 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6523 header->dev_specific |= 0x80; /* WP */
6526 scsi_ulto2b(0, header->block_descr_len);
6528 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6529 header->block_descr_len);
6530 block_desc = (struct scsi_mode_block_descr *)&header[1];
6534 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6538 * If we've got a disk, use its blocksize in the block
6539 * descriptor. Otherwise, just set it to 0.
6542 if (lun->be_lun->lun_type == T_DIRECT)
6543 scsi_ulto3b(lun->be_lun->blocksize,
6544 block_desc->block_len);
6546 scsi_ulto3b(0, block_desc->block_len);
6549 switch (page_code) {
6550 case SMS_ALL_PAGES_PAGE: {
6553 data_used = header_len;
6554 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6555 struct ctl_page_index *page_index;
6557 page_index = &lun->mode_pages.index[i];
6558 if (lun->be_lun->lun_type == T_DIRECT &&
6559 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6561 if (lun->be_lun->lun_type == T_PROCESSOR &&
6562 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6564 if (lun->be_lun->lun_type == T_CDROM &&
6565 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6569 * We don't use this subpage if the user didn't
6570 * request all subpages. We already checked (above)
6571 * to make sure the user only specified a subpage
6572 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6574 if ((page_index->subpage != 0)
6575 && (subpage == SMS_SUBPAGE_PAGE_0))
6579 * Call the handler, if it exists, to update the
6580 * page to the latest values.
6582 if (page_index->sense_handler != NULL)
6583 page_index->sense_handler(ctsio, page_index,pc);
6585 memcpy(ctsio->kern_data_ptr + data_used,
6586 page_index->page_data +
6587 (page_index->page_len * pc),
6588 page_index->page_len);
6589 data_used += page_index->page_len;
6596 data_used = header_len;
6598 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6599 struct ctl_page_index *page_index;
6601 page_index = &lun->mode_pages.index[i];
6603 /* Look for the right page code */
6604 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6607 /* Look for the right subpage or the subpage wildcard*/
6608 if ((page_index->subpage != subpage)
6609 && (subpage != SMS_SUBPAGE_ALL))
6612 /* Make sure the page is supported for this dev type */
6613 if (lun->be_lun->lun_type == T_DIRECT &&
6614 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6616 if (lun->be_lun->lun_type == T_PROCESSOR &&
6617 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6619 if (lun->be_lun->lun_type == T_CDROM &&
6620 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6624 * Call the handler, if it exists, to update the
6625 * page to the latest values.
6627 if (page_index->sense_handler != NULL)
6628 page_index->sense_handler(ctsio, page_index,pc);
6630 memcpy(ctsio->kern_data_ptr + data_used,
6631 page_index->page_data +
6632 (page_index->page_len * pc),
6633 page_index->page_len);
6634 data_used += page_index->page_len;
6640 ctl_set_success(ctsio);
6641 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6642 ctsio->be_move_done = ctl_config_move_done;
6643 ctl_datamove((union ctl_io *)ctsio);
6644 return (CTL_RETVAL_COMPLETE);
6648 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6649 struct ctl_page_index *page_index,
6652 struct ctl_lun *lun = CTL_LUN(ctsio);
6653 struct scsi_log_param_header *phdr;
6657 data = page_index->page_data;
6659 if (lun->backend->lun_attr != NULL &&
6660 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6662 phdr = (struct scsi_log_param_header *)data;
6663 scsi_ulto2b(0x0001, phdr->param_code);
6664 phdr->param_control = SLP_LBIN | SLP_LP;
6665 phdr->param_len = 8;
6666 data = (uint8_t *)(phdr + 1);
6667 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6668 data[4] = 0x02; /* per-pool */
6669 data += phdr->param_len;
6672 if (lun->backend->lun_attr != NULL &&
6673 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6675 phdr = (struct scsi_log_param_header *)data;
6676 scsi_ulto2b(0x0002, phdr->param_code);
6677 phdr->param_control = SLP_LBIN | SLP_LP;
6678 phdr->param_len = 8;
6679 data = (uint8_t *)(phdr + 1);
6680 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6681 data[4] = 0x01; /* per-LUN */
6682 data += phdr->param_len;
6685 if (lun->backend->lun_attr != NULL &&
6686 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6688 phdr = (struct scsi_log_param_header *)data;
6689 scsi_ulto2b(0x00f1, phdr->param_code);
6690 phdr->param_control = SLP_LBIN | SLP_LP;
6691 phdr->param_len = 8;
6692 data = (uint8_t *)(phdr + 1);
6693 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6694 data[4] = 0x02; /* per-pool */
6695 data += phdr->param_len;
6698 if (lun->backend->lun_attr != NULL &&
6699 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6701 phdr = (struct scsi_log_param_header *)data;
6702 scsi_ulto2b(0x00f2, phdr->param_code);
6703 phdr->param_control = SLP_LBIN | SLP_LP;
6704 phdr->param_len = 8;
6705 data = (uint8_t *)(phdr + 1);
6706 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6707 data[4] = 0x02; /* per-pool */
6708 data += phdr->param_len;
6711 page_index->page_len = data - page_index->page_data;
6716 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
6717 struct ctl_page_index *page_index,
6720 struct ctl_lun *lun = CTL_LUN(ctsio);
6721 struct stat_page *data;
6724 data = (struct stat_page *)page_index->page_data;
6726 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
6727 data->sap.hdr.param_control = SLP_LBIN;
6728 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
6729 sizeof(struct scsi_log_param_header);
6730 scsi_u64to8b(lun->stats.operations[CTL_STATS_READ],
6731 data->sap.read_num);
6732 scsi_u64to8b(lun->stats.operations[CTL_STATS_WRITE],
6733 data->sap.write_num);
6734 if (lun->be_lun->blocksize > 0) {
6735 scsi_u64to8b(lun->stats.bytes[CTL_STATS_WRITE] /
6736 lun->be_lun->blocksize, data->sap.recvieved_lba);
6737 scsi_u64to8b(lun->stats.bytes[CTL_STATS_READ] /
6738 lun->be_lun->blocksize, data->sap.transmitted_lba);
6740 t = &lun->stats.time[CTL_STATS_READ];
6741 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000),
6742 data->sap.read_int);
6743 t = &lun->stats.time[CTL_STATS_WRITE];
6744 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000),
6745 data->sap.write_int);
6746 scsi_u64to8b(0, data->sap.weighted_num);
6747 scsi_u64to8b(0, data->sap.weighted_int);
6748 scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
6749 data->it.hdr.param_control = SLP_LBIN;
6750 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
6751 sizeof(struct scsi_log_param_header);
6753 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
6755 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
6756 data->it.hdr.param_control = SLP_LBIN;
6757 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
6758 sizeof(struct scsi_log_param_header);
6759 scsi_ulto4b(3, data->ti.exponent);
6760 scsi_ulto4b(1, data->ti.integer);
6765 ctl_ie_log_sense_handler(struct ctl_scsiio *ctsio,
6766 struct ctl_page_index *page_index,
6769 struct ctl_lun *lun = CTL_LUN(ctsio);
6770 struct scsi_log_informational_exceptions *data;
6772 data = (struct scsi_log_informational_exceptions *)page_index->page_data;
6774 scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code);
6775 data->hdr.param_control = SLP_LBIN;
6776 data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) -
6777 sizeof(struct scsi_log_param_header);
6778 data->ie_asc = lun->ie_asc;
6779 data->ie_ascq = lun->ie_ascq;
6780 data->temperature = 0xff;
6785 ctl_log_sense(struct ctl_scsiio *ctsio)
6787 struct ctl_lun *lun = CTL_LUN(ctsio);
6788 int i, pc, page_code, subpage;
6789 int alloc_len, total_len;
6790 struct ctl_page_index *page_index;
6791 struct scsi_log_sense *cdb;
6792 struct scsi_log_header *header;
6794 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6796 cdb = (struct scsi_log_sense *)ctsio->cdb;
6797 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6798 page_code = cdb->page & SLS_PAGE_CODE;
6799 subpage = cdb->subpage;
6800 alloc_len = scsi_2btoul(cdb->length);
6803 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6804 page_index = &lun->log_pages.index[i];
6806 /* Look for the right page code */
6807 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6810 /* Look for the right subpage or the subpage wildcard*/
6811 if (page_index->subpage != subpage)
6816 if (i >= CTL_NUM_LOG_PAGES) {
6817 ctl_set_invalid_field(ctsio,
6823 ctl_done((union ctl_io *)ctsio);
6824 return (CTL_RETVAL_COMPLETE);
6827 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6829 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6830 ctsio->kern_sg_entries = 0;
6831 ctsio->kern_rel_offset = 0;
6832 ctsio->kern_data_len = min(total_len, alloc_len);
6833 ctsio->kern_total_len = ctsio->kern_data_len;
6835 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
6836 header->page = page_index->page_code;
6837 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING)
6838 header->page |= SL_DS;
6839 if (page_index->subpage) {
6840 header->page |= SL_SPF;
6841 header->subpage = page_index->subpage;
6843 scsi_ulto2b(page_index->page_len, header->datalen);
6846 * Call the handler, if it exists, to update the
6847 * page to the latest values.
6849 if (page_index->sense_handler != NULL)
6850 page_index->sense_handler(ctsio, page_index, pc);
6852 memcpy(header + 1, page_index->page_data, page_index->page_len);
6854 ctl_set_success(ctsio);
6855 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6856 ctsio->be_move_done = ctl_config_move_done;
6857 ctl_datamove((union ctl_io *)ctsio);
6858 return (CTL_RETVAL_COMPLETE);
6862 ctl_read_capacity(struct ctl_scsiio *ctsio)
6864 struct ctl_lun *lun = CTL_LUN(ctsio);
6865 struct scsi_read_capacity *cdb;
6866 struct scsi_read_capacity_data *data;
6869 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6871 cdb = (struct scsi_read_capacity *)ctsio->cdb;
6873 lba = scsi_4btoul(cdb->addr);
6874 if (((cdb->pmi & SRC_PMI) == 0)
6876 ctl_set_invalid_field(/*ctsio*/ ctsio,
6882 ctl_done((union ctl_io *)ctsio);
6883 return (CTL_RETVAL_COMPLETE);
6886 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6887 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6888 ctsio->kern_data_len = sizeof(*data);
6889 ctsio->kern_total_len = sizeof(*data);
6890 ctsio->kern_rel_offset = 0;
6891 ctsio->kern_sg_entries = 0;
6894 * If the maximum LBA is greater than 0xfffffffe, the user must
6895 * issue a SERVICE ACTION IN (16) command, with the read capacity
6896 * serivce action set.
6898 if (lun->be_lun->maxlba > 0xfffffffe)
6899 scsi_ulto4b(0xffffffff, data->addr);
6901 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6904 * XXX KDM this may not be 512 bytes...
6906 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6908 ctl_set_success(ctsio);
6909 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6910 ctsio->be_move_done = ctl_config_move_done;
6911 ctl_datamove((union ctl_io *)ctsio);
6912 return (CTL_RETVAL_COMPLETE);
6916 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
6918 struct ctl_lun *lun = CTL_LUN(ctsio);
6919 struct scsi_read_capacity_16 *cdb;
6920 struct scsi_read_capacity_data_long *data;
6924 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
6926 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
6928 alloc_len = scsi_4btoul(cdb->alloc_len);
6929 lba = scsi_8btou64(cdb->addr);
6931 if ((cdb->reladr & SRC16_PMI)
6933 ctl_set_invalid_field(/*ctsio*/ ctsio,
6939 ctl_done((union ctl_io *)ctsio);
6940 return (CTL_RETVAL_COMPLETE);
6943 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6944 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
6945 ctsio->kern_rel_offset = 0;
6946 ctsio->kern_sg_entries = 0;
6947 ctsio->kern_data_len = min(sizeof(*data), alloc_len);
6948 ctsio->kern_total_len = ctsio->kern_data_len;
6950 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
6951 /* XXX KDM this may not be 512 bytes... */
6952 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6953 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
6954 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
6955 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
6956 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
6958 ctl_set_success(ctsio);
6959 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6960 ctsio->be_move_done = ctl_config_move_done;
6961 ctl_datamove((union ctl_io *)ctsio);
6962 return (CTL_RETVAL_COMPLETE);
6966 ctl_get_lba_status(struct ctl_scsiio *ctsio)
6968 struct ctl_lun *lun = CTL_LUN(ctsio);
6969 struct scsi_get_lba_status *cdb;
6970 struct scsi_get_lba_status_data *data;
6971 struct ctl_lba_len_flags *lbalen;
6973 uint32_t alloc_len, total_len;
6976 CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
6978 cdb = (struct scsi_get_lba_status *)ctsio->cdb;
6979 lba = scsi_8btou64(cdb->addr);
6980 alloc_len = scsi_4btoul(cdb->alloc_len);
6982 if (lba > lun->be_lun->maxlba) {
6983 ctl_set_lba_out_of_range(ctsio, lba);
6984 ctl_done((union ctl_io *)ctsio);
6985 return (CTL_RETVAL_COMPLETE);
6988 total_len = sizeof(*data) + sizeof(data->descr[0]);
6989 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6990 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
6991 ctsio->kern_rel_offset = 0;
6992 ctsio->kern_sg_entries = 0;
6993 ctsio->kern_data_len = min(total_len, alloc_len);
6994 ctsio->kern_total_len = ctsio->kern_data_len;
6996 /* Fill dummy data in case backend can't tell anything. */
6997 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
6998 scsi_u64to8b(lba, data->descr[0].addr);
6999 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
7000 data->descr[0].length);
7001 data->descr[0].status = 0; /* Mapped or unknown. */
7003 ctl_set_success(ctsio);
7004 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7005 ctsio->be_move_done = ctl_config_move_done;
7007 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
7009 lbalen->len = total_len;
7011 retval = lun->backend->config_read((union ctl_io *)ctsio);
7012 return (CTL_RETVAL_COMPLETE);
7016 ctl_read_defect(struct ctl_scsiio *ctsio)
7018 struct scsi_read_defect_data_10 *ccb10;
7019 struct scsi_read_defect_data_12 *ccb12;
7020 struct scsi_read_defect_data_hdr_10 *data10;
7021 struct scsi_read_defect_data_hdr_12 *data12;
7022 uint32_t alloc_len, data_len;
7025 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7027 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7028 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7029 format = ccb10->format;
7030 alloc_len = scsi_2btoul(ccb10->alloc_length);
7031 data_len = sizeof(*data10);
7033 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7034 format = ccb12->format;
7035 alloc_len = scsi_4btoul(ccb12->alloc_length);
7036 data_len = sizeof(*data12);
7038 if (alloc_len == 0) {
7039 ctl_set_success(ctsio);
7040 ctl_done((union ctl_io *)ctsio);
7041 return (CTL_RETVAL_COMPLETE);
7044 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7045 ctsio->kern_rel_offset = 0;
7046 ctsio->kern_sg_entries = 0;
7047 ctsio->kern_data_len = min(data_len, alloc_len);
7048 ctsio->kern_total_len = ctsio->kern_data_len;
7050 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7051 data10 = (struct scsi_read_defect_data_hdr_10 *)
7052 ctsio->kern_data_ptr;
7053 data10->format = format;
7054 scsi_ulto2b(0, data10->length);
7056 data12 = (struct scsi_read_defect_data_hdr_12 *)
7057 ctsio->kern_data_ptr;
7058 data12->format = format;
7059 scsi_ulto2b(0, data12->generation);
7060 scsi_ulto4b(0, data12->length);
7063 ctl_set_success(ctsio);
7064 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7065 ctsio->be_move_done = ctl_config_move_done;
7066 ctl_datamove((union ctl_io *)ctsio);
7067 return (CTL_RETVAL_COMPLETE);
7071 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7073 struct ctl_softc *softc = CTL_SOFTC(ctsio);
7074 struct ctl_lun *lun = CTL_LUN(ctsio);
7075 struct scsi_maintenance_in *cdb;
7077 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os;
7078 int num_ha_groups, num_target_ports, shared_group;
7079 struct ctl_port *port;
7080 struct scsi_target_group_data *rtg_ptr;
7081 struct scsi_target_group_data_extended *rtg_ext_ptr;
7082 struct scsi_target_port_group_descriptor *tpg_desc;
7084 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7086 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7087 retval = CTL_RETVAL_COMPLETE;
7089 switch (cdb->byte2 & STG_PDF_MASK) {
7090 case STG_PDF_LENGTH:
7093 case STG_PDF_EXTENDED:
7097 ctl_set_invalid_field(/*ctsio*/ ctsio,
7103 ctl_done((union ctl_io *)ctsio);
7107 num_target_ports = 0;
7108 shared_group = (softc->is_single != 0);
7109 mtx_lock(&softc->ctl_lock);
7110 STAILQ_FOREACH(port, &softc->port_list, links) {
7111 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7113 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7116 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7119 mtx_unlock(&softc->ctl_lock);
7120 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES;
7123 total_len = sizeof(struct scsi_target_group_data_extended);
7125 total_len = sizeof(struct scsi_target_group_data);
7126 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7127 (shared_group + num_ha_groups) +
7128 sizeof(struct scsi_target_port_descriptor) * num_target_ports;
7130 alloc_len = scsi_4btoul(cdb->length);
7132 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7133 ctsio->kern_sg_entries = 0;
7134 ctsio->kern_rel_offset = 0;
7135 ctsio->kern_data_len = min(total_len, alloc_len);
7136 ctsio->kern_total_len = ctsio->kern_data_len;
7139 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7140 ctsio->kern_data_ptr;
7141 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7142 rtg_ext_ptr->format_type = 0x10;
7143 rtg_ext_ptr->implicit_transition_time = 0;
7144 tpg_desc = &rtg_ext_ptr->groups[0];
7146 rtg_ptr = (struct scsi_target_group_data *)
7147 ctsio->kern_data_ptr;
7148 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7149 tpg_desc = &rtg_ptr->groups[0];
7152 mtx_lock(&softc->ctl_lock);
7153 pg = softc->port_min / softc->port_cnt;
7154 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) {
7155 /* Some shelf is known to be primary. */
7156 if (softc->ha_link == CTL_HA_LINK_OFFLINE)
7157 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7158 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN)
7159 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7160 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY)
7161 os = TPG_ASYMMETRIC_ACCESS_STANDBY;
7163 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7164 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7165 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7168 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7171 /* No known primary shelf. */
7172 if (softc->ha_link == CTL_HA_LINK_OFFLINE) {
7173 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7174 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7175 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) {
7176 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7177 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7179 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7183 tpg_desc->pref_state = ts;
7184 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7185 TPG_U_SUP | TPG_T_SUP;
7186 scsi_ulto2b(1, tpg_desc->target_port_group);
7187 tpg_desc->status = TPG_IMPLICIT;
7189 STAILQ_FOREACH(port, &softc->port_list, links) {
7190 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7192 if (!softc->is_single &&
7193 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0)
7195 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7197 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7198 relative_target_port_identifier);
7201 tpg_desc->target_port_count = pc;
7202 tpg_desc = (struct scsi_target_port_group_descriptor *)
7203 &tpg_desc->descriptors[pc];
7205 for (g = 0; g < num_ha_groups; g++) {
7206 tpg_desc->pref_state = (g == pg) ? ts : os;
7207 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7208 TPG_U_SUP | TPG_T_SUP;
7209 scsi_ulto2b(2 + g, tpg_desc->target_port_group);
7210 tpg_desc->status = TPG_IMPLICIT;
7212 STAILQ_FOREACH(port, &softc->port_list, links) {
7213 if (port->targ_port < g * softc->port_cnt ||
7214 port->targ_port >= (g + 1) * softc->port_cnt)
7216 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7218 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7220 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7222 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7223 relative_target_port_identifier);
7226 tpg_desc->target_port_count = pc;
7227 tpg_desc = (struct scsi_target_port_group_descriptor *)
7228 &tpg_desc->descriptors[pc];
7230 mtx_unlock(&softc->ctl_lock);
7232 ctl_set_success(ctsio);
7233 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7234 ctsio->be_move_done = ctl_config_move_done;
7235 ctl_datamove((union ctl_io *)ctsio);
7240 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7242 struct ctl_lun *lun = CTL_LUN(ctsio);
7243 struct scsi_report_supported_opcodes *cdb;
7244 const struct ctl_cmd_entry *entry, *sentry;
7245 struct scsi_report_supported_opcodes_all *all;
7246 struct scsi_report_supported_opcodes_descr *descr;
7247 struct scsi_report_supported_opcodes_one *one;
7249 int alloc_len, total_len;
7250 int opcode, service_action, i, j, num;
7252 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7254 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7255 retval = CTL_RETVAL_COMPLETE;
7257 opcode = cdb->requested_opcode;
7258 service_action = scsi_2btoul(cdb->requested_service_action);
7259 switch (cdb->options & RSO_OPTIONS_MASK) {
7260 case RSO_OPTIONS_ALL:
7262 for (i = 0; i < 256; i++) {
7263 entry = &ctl_cmd_table[i];
7264 if (entry->flags & CTL_CMD_FLAG_SA5) {
7265 for (j = 0; j < 32; j++) {
7266 sentry = &((const struct ctl_cmd_entry *)
7268 if (ctl_cmd_applicable(
7269 lun->be_lun->lun_type, sentry))
7273 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7278 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7279 num * sizeof(struct scsi_report_supported_opcodes_descr);
7281 case RSO_OPTIONS_OC:
7282 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7283 ctl_set_invalid_field(/*ctsio*/ ctsio,
7289 ctl_done((union ctl_io *)ctsio);
7290 return (CTL_RETVAL_COMPLETE);
7292 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7294 case RSO_OPTIONS_OC_SA:
7295 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7296 service_action >= 32) {
7297 ctl_set_invalid_field(/*ctsio*/ ctsio,
7303 ctl_done((union ctl_io *)ctsio);
7304 return (CTL_RETVAL_COMPLETE);
7307 case RSO_OPTIONS_OC_ASA:
7308 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7311 ctl_set_invalid_field(/*ctsio*/ ctsio,
7317 ctl_done((union ctl_io *)ctsio);
7318 return (CTL_RETVAL_COMPLETE);
7321 alloc_len = scsi_4btoul(cdb->length);
7323 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7324 ctsio->kern_sg_entries = 0;
7325 ctsio->kern_rel_offset = 0;
7326 ctsio->kern_data_len = min(total_len, alloc_len);
7327 ctsio->kern_total_len = ctsio->kern_data_len;
7329 switch (cdb->options & RSO_OPTIONS_MASK) {
7330 case RSO_OPTIONS_ALL:
7331 all = (struct scsi_report_supported_opcodes_all *)
7332 ctsio->kern_data_ptr;
7334 for (i = 0; i < 256; i++) {
7335 entry = &ctl_cmd_table[i];
7336 if (entry->flags & CTL_CMD_FLAG_SA5) {
7337 for (j = 0; j < 32; j++) {
7338 sentry = &((const struct ctl_cmd_entry *)
7340 if (!ctl_cmd_applicable(
7341 lun->be_lun->lun_type, sentry))
7343 descr = &all->descr[num++];
7345 scsi_ulto2b(j, descr->service_action);
7346 descr->flags = RSO_SERVACTV;
7347 scsi_ulto2b(sentry->length,
7351 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7354 descr = &all->descr[num++];
7356 scsi_ulto2b(0, descr->service_action);
7358 scsi_ulto2b(entry->length, descr->cdb_length);
7362 num * sizeof(struct scsi_report_supported_opcodes_descr),
7365 case RSO_OPTIONS_OC:
7366 one = (struct scsi_report_supported_opcodes_one *)
7367 ctsio->kern_data_ptr;
7368 entry = &ctl_cmd_table[opcode];
7370 case RSO_OPTIONS_OC_SA:
7371 one = (struct scsi_report_supported_opcodes_one *)
7372 ctsio->kern_data_ptr;
7373 entry = &ctl_cmd_table[opcode];
7374 entry = &((const struct ctl_cmd_entry *)
7375 entry->execute)[service_action];
7377 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7379 scsi_ulto2b(entry->length, one->cdb_length);
7380 one->cdb_usage[0] = opcode;
7381 memcpy(&one->cdb_usage[1], entry->usage,
7386 case RSO_OPTIONS_OC_ASA:
7387 one = (struct scsi_report_supported_opcodes_one *)
7388 ctsio->kern_data_ptr;
7389 entry = &ctl_cmd_table[opcode];
7390 if (entry->flags & CTL_CMD_FLAG_SA5) {
7391 entry = &((const struct ctl_cmd_entry *)
7392 entry->execute)[service_action];
7393 } else if (service_action != 0) {
7400 ctl_set_success(ctsio);
7401 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7402 ctsio->be_move_done = ctl_config_move_done;
7403 ctl_datamove((union ctl_io *)ctsio);
7408 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7410 struct scsi_report_supported_tmf *cdb;
7411 struct scsi_report_supported_tmf_ext_data *data;
7413 int alloc_len, total_len;
7415 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7417 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7419 retval = CTL_RETVAL_COMPLETE;
7421 if (cdb->options & RST_REPD)
7422 total_len = sizeof(struct scsi_report_supported_tmf_ext_data);
7424 total_len = sizeof(struct scsi_report_supported_tmf_data);
7425 alloc_len = scsi_4btoul(cdb->length);
7427 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7428 ctsio->kern_sg_entries = 0;
7429 ctsio->kern_rel_offset = 0;
7430 ctsio->kern_data_len = min(total_len, alloc_len);
7431 ctsio->kern_total_len = ctsio->kern_data_len;
7433 data = (struct scsi_report_supported_tmf_ext_data *)ctsio->kern_data_ptr;
7434 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS |
7436 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS;
7437 data->length = total_len - 4;
7439 ctl_set_success(ctsio);
7440 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7441 ctsio->be_move_done = ctl_config_move_done;
7442 ctl_datamove((union ctl_io *)ctsio);
7447 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7449 struct scsi_report_timestamp *cdb;
7450 struct scsi_report_timestamp_data *data;
7454 int alloc_len, total_len;
7456 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7458 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7460 retval = CTL_RETVAL_COMPLETE;
7462 total_len = sizeof(struct scsi_report_timestamp_data);
7463 alloc_len = scsi_4btoul(cdb->length);
7465 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7466 ctsio->kern_sg_entries = 0;
7467 ctsio->kern_rel_offset = 0;
7468 ctsio->kern_data_len = min(total_len, alloc_len);
7469 ctsio->kern_total_len = ctsio->kern_data_len;
7471 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7472 scsi_ulto2b(sizeof(*data) - 2, data->length);
7473 data->origin = RTS_ORIG_OUTSIDE;
7475 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7476 scsi_ulto4b(timestamp >> 16, data->timestamp);
7477 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7479 ctl_set_success(ctsio);
7480 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7481 ctsio->be_move_done = ctl_config_move_done;
7482 ctl_datamove((union ctl_io *)ctsio);
7487 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7489 struct ctl_softc *softc = CTL_SOFTC(ctsio);
7490 struct ctl_lun *lun = CTL_LUN(ctsio);
7491 struct scsi_per_res_in *cdb;
7492 int alloc_len, total_len = 0;
7493 /* struct scsi_per_res_in_rsrv in_data; */
7496 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7498 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7500 alloc_len = scsi_2btoul(cdb->length);
7503 mtx_lock(&lun->lun_lock);
7504 switch (cdb->action) {
7505 case SPRI_RK: /* read keys */
7506 total_len = sizeof(struct scsi_per_res_in_keys) +
7508 sizeof(struct scsi_per_res_key);
7510 case SPRI_RR: /* read reservation */
7511 if (lun->flags & CTL_LUN_PR_RESERVED)
7512 total_len = sizeof(struct scsi_per_res_in_rsrv);
7514 total_len = sizeof(struct scsi_per_res_in_header);
7516 case SPRI_RC: /* report capabilities */
7517 total_len = sizeof(struct scsi_per_res_cap);
7519 case SPRI_RS: /* read full status */
7520 total_len = sizeof(struct scsi_per_res_in_header) +
7521 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7525 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7527 mtx_unlock(&lun->lun_lock);
7529 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7530 ctsio->kern_rel_offset = 0;
7531 ctsio->kern_sg_entries = 0;
7532 ctsio->kern_data_len = min(total_len, alloc_len);
7533 ctsio->kern_total_len = ctsio->kern_data_len;
7535 mtx_lock(&lun->lun_lock);
7536 switch (cdb->action) {
7537 case SPRI_RK: { // read keys
7538 struct scsi_per_res_in_keys *res_keys;
7541 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7544 * We had to drop the lock to allocate our buffer, which
7545 * leaves time for someone to come in with another
7546 * persistent reservation. (That is unlikely, though,
7547 * since this should be the only persistent reservation
7548 * command active right now.)
7550 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7551 (lun->pr_key_count *
7552 sizeof(struct scsi_per_res_key)))){
7553 mtx_unlock(&lun->lun_lock);
7554 free(ctsio->kern_data_ptr, M_CTL);
7555 printf("%s: reservation length changed, retrying\n",
7560 scsi_ulto4b(lun->pr_generation, res_keys->header.generation);
7562 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7563 lun->pr_key_count, res_keys->header.length);
7565 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) {
7566 if ((key = ctl_get_prkey(lun, i)) == 0)
7570 * We used lun->pr_key_count to calculate the
7571 * size to allocate. If it turns out the number of
7572 * initiators with the registered flag set is
7573 * larger than that (i.e. they haven't been kept in
7574 * sync), we've got a problem.
7576 if (key_count >= lun->pr_key_count) {
7580 scsi_u64to8b(key, res_keys->keys[key_count].key);
7585 case SPRI_RR: { // read reservation
7586 struct scsi_per_res_in_rsrv *res;
7587 int tmp_len, header_only;
7589 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7591 scsi_ulto4b(lun->pr_generation, res->header.generation);
7593 if (lun->flags & CTL_LUN_PR_RESERVED)
7595 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7596 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7597 res->header.length);
7600 tmp_len = sizeof(struct scsi_per_res_in_header);
7601 scsi_ulto4b(0, res->header.length);
7606 * We had to drop the lock to allocate our buffer, which
7607 * leaves time for someone to come in with another
7608 * persistent reservation. (That is unlikely, though,
7609 * since this should be the only persistent reservation
7610 * command active right now.)
7612 if (tmp_len != total_len) {
7613 mtx_unlock(&lun->lun_lock);
7614 free(ctsio->kern_data_ptr, M_CTL);
7615 printf("%s: reservation status changed, retrying\n",
7621 * No reservation held, so we're done.
7623 if (header_only != 0)
7627 * If the registration is an All Registrants type, the key
7628 * is 0, since it doesn't really matter.
7630 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7631 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7632 res->data.reservation);
7634 res->data.scopetype = lun->pr_res_type;
7637 case SPRI_RC: //report capabilities
7639 struct scsi_per_res_cap *res_cap;
7642 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7643 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7644 res_cap->flags1 = SPRI_CRH;
7645 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5;
7646 type_mask = SPRI_TM_WR_EX_AR |
7652 scsi_ulto2b(type_mask, res_cap->type_mask);
7655 case SPRI_RS: { // read full status
7656 struct scsi_per_res_in_full *res_status;
7657 struct scsi_per_res_in_full_desc *res_desc;
7658 struct ctl_port *port;
7661 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7664 * We had to drop the lock to allocate our buffer, which
7665 * leaves time for someone to come in with another
7666 * persistent reservation. (That is unlikely, though,
7667 * since this should be the only persistent reservation
7668 * command active right now.)
7670 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7671 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7672 lun->pr_key_count)){
7673 mtx_unlock(&lun->lun_lock);
7674 free(ctsio->kern_data_ptr, M_CTL);
7675 printf("%s: reservation length changed, retrying\n",
7680 scsi_ulto4b(lun->pr_generation, res_status->header.generation);
7682 res_desc = &res_status->desc[0];
7683 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7684 if ((key = ctl_get_prkey(lun, i)) == 0)
7687 scsi_u64to8b(key, res_desc->res_key.key);
7688 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7689 (lun->pr_res_idx == i ||
7690 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7691 res_desc->flags = SPRI_FULL_R_HOLDER;
7692 res_desc->scopetype = lun->pr_res_type;
7694 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7695 res_desc->rel_trgt_port_id);
7697 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7699 len = ctl_create_iid(port,
7700 i % CTL_MAX_INIT_PER_PORT,
7701 res_desc->transport_id);
7702 scsi_ulto4b(len, res_desc->additional_length);
7703 res_desc = (struct scsi_per_res_in_full_desc *)
7704 &res_desc->transport_id[len];
7706 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7707 res_status->header.length);
7711 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7713 mtx_unlock(&lun->lun_lock);
7715 ctl_set_success(ctsio);
7716 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7717 ctsio->be_move_done = ctl_config_move_done;
7718 ctl_datamove((union ctl_io *)ctsio);
7719 return (CTL_RETVAL_COMPLETE);
7723 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7727 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7728 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7729 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7730 struct scsi_per_res_out_parms* param)
7732 union ctl_ha_msg persis_io;
7735 mtx_lock(&lun->lun_lock);
7736 if (sa_res_key == 0) {
7737 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7738 /* validate scope and type */
7739 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7741 mtx_unlock(&lun->lun_lock);
7742 ctl_set_invalid_field(/*ctsio*/ ctsio,
7748 ctl_done((union ctl_io *)ctsio);
7752 if (type>8 || type==2 || type==4 || type==0) {
7753 mtx_unlock(&lun->lun_lock);
7754 ctl_set_invalid_field(/*ctsio*/ ctsio,
7760 ctl_done((union ctl_io *)ctsio);
7765 * Unregister everybody else and build UA for
7768 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7769 if (i == residx || ctl_get_prkey(lun, i) == 0)
7772 ctl_clr_prkey(lun, i);
7773 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7775 lun->pr_key_count = 1;
7776 lun->pr_res_type = type;
7777 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7778 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7779 lun->pr_res_idx = residx;
7780 lun->pr_generation++;
7781 mtx_unlock(&lun->lun_lock);
7783 /* send msg to other side */
7784 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7785 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7786 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7787 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7788 persis_io.pr.pr_info.res_type = type;
7789 memcpy(persis_io.pr.pr_info.sa_res_key,
7790 param->serv_act_res_key,
7791 sizeof(param->serv_act_res_key));
7792 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7793 sizeof(persis_io.pr), M_WAITOK);
7795 /* not all registrants */
7796 mtx_unlock(&lun->lun_lock);
7797 free(ctsio->kern_data_ptr, M_CTL);
7798 ctl_set_invalid_field(ctsio,
7804 ctl_done((union ctl_io *)ctsio);
7807 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7808 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7811 if (res_key == sa_res_key) {
7814 * The spec implies this is not good but doesn't
7815 * say what to do. There are two choices either
7816 * generate a res conflict or check condition
7817 * with illegal field in parameter data. Since
7818 * that is what is done when the sa_res_key is
7819 * zero I'll take that approach since this has
7820 * to do with the sa_res_key.
7822 mtx_unlock(&lun->lun_lock);
7823 free(ctsio->kern_data_ptr, M_CTL);
7824 ctl_set_invalid_field(ctsio,
7830 ctl_done((union ctl_io *)ctsio);
7834 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7835 if (ctl_get_prkey(lun, i) != sa_res_key)
7839 ctl_clr_prkey(lun, i);
7840 lun->pr_key_count--;
7841 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7844 mtx_unlock(&lun->lun_lock);
7845 free(ctsio->kern_data_ptr, M_CTL);
7846 ctl_set_reservation_conflict(ctsio);
7847 ctl_done((union ctl_io *)ctsio);
7848 return (CTL_RETVAL_COMPLETE);
7850 lun->pr_generation++;
7851 mtx_unlock(&lun->lun_lock);
7853 /* send msg to other side */
7854 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7855 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7856 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7857 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7858 persis_io.pr.pr_info.res_type = type;
7859 memcpy(persis_io.pr.pr_info.sa_res_key,
7860 param->serv_act_res_key,
7861 sizeof(param->serv_act_res_key));
7862 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7863 sizeof(persis_io.pr), M_WAITOK);
7865 /* Reserved but not all registrants */
7866 /* sa_res_key is res holder */
7867 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
7868 /* validate scope and type */
7869 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7871 mtx_unlock(&lun->lun_lock);
7872 ctl_set_invalid_field(/*ctsio*/ ctsio,
7878 ctl_done((union ctl_io *)ctsio);
7882 if (type>8 || type==2 || type==4 || type==0) {
7883 mtx_unlock(&lun->lun_lock);
7884 ctl_set_invalid_field(/*ctsio*/ ctsio,
7890 ctl_done((union ctl_io *)ctsio);
7896 * if sa_res_key != res_key remove all
7897 * registrants w/sa_res_key and generate UA
7898 * for these registrants(Registrations
7899 * Preempted) if it wasn't an exclusive
7900 * reservation generate UA(Reservations
7901 * Preempted) for all other registered nexuses
7902 * if the type has changed. Establish the new
7903 * reservation and holder. If res_key and
7904 * sa_res_key are the same do the above
7905 * except don't unregister the res holder.
7908 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7909 if (i == residx || ctl_get_prkey(lun, i) == 0)
7912 if (sa_res_key == ctl_get_prkey(lun, i)) {
7913 ctl_clr_prkey(lun, i);
7914 lun->pr_key_count--;
7915 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7916 } else if (type != lun->pr_res_type &&
7917 (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
7918 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
7919 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
7922 lun->pr_res_type = type;
7923 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7924 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7925 lun->pr_res_idx = residx;
7927 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
7928 lun->pr_generation++;
7929 mtx_unlock(&lun->lun_lock);
7931 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7932 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7933 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7934 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7935 persis_io.pr.pr_info.res_type = type;
7936 memcpy(persis_io.pr.pr_info.sa_res_key,
7937 param->serv_act_res_key,
7938 sizeof(param->serv_act_res_key));
7939 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7940 sizeof(persis_io.pr), M_WAITOK);
7943 * sa_res_key is not the res holder just
7944 * remove registrants
7948 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7949 if (sa_res_key != ctl_get_prkey(lun, i))
7953 ctl_clr_prkey(lun, i);
7954 lun->pr_key_count--;
7955 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7959 mtx_unlock(&lun->lun_lock);
7960 free(ctsio->kern_data_ptr, M_CTL);
7961 ctl_set_reservation_conflict(ctsio);
7962 ctl_done((union ctl_io *)ctsio);
7965 lun->pr_generation++;
7966 mtx_unlock(&lun->lun_lock);
7968 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7969 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7970 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7971 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7972 persis_io.pr.pr_info.res_type = type;
7973 memcpy(persis_io.pr.pr_info.sa_res_key,
7974 param->serv_act_res_key,
7975 sizeof(param->serv_act_res_key));
7976 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7977 sizeof(persis_io.pr), M_WAITOK);
7984 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
7986 uint64_t sa_res_key;
7989 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
7991 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7992 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
7993 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
7994 if (sa_res_key == 0) {
7996 * Unregister everybody else and build UA for
7999 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8000 if (i == msg->pr.pr_info.residx ||
8001 ctl_get_prkey(lun, i) == 0)
8004 ctl_clr_prkey(lun, i);
8005 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8008 lun->pr_key_count = 1;
8009 lun->pr_res_type = msg->pr.pr_info.res_type;
8010 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8011 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8012 lun->pr_res_idx = msg->pr.pr_info.residx;
8014 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8015 if (sa_res_key == ctl_get_prkey(lun, i))
8018 ctl_clr_prkey(lun, i);
8019 lun->pr_key_count--;
8020 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8024 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8025 if (i == msg->pr.pr_info.residx ||
8026 ctl_get_prkey(lun, i) == 0)
8029 if (sa_res_key == ctl_get_prkey(lun, i)) {
8030 ctl_clr_prkey(lun, i);
8031 lun->pr_key_count--;
8032 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8033 } else if (msg->pr.pr_info.res_type != lun->pr_res_type
8034 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8035 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8036 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8039 lun->pr_res_type = msg->pr.pr_info.res_type;
8040 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8041 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8042 lun->pr_res_idx = msg->pr.pr_info.residx;
8044 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8046 lun->pr_generation++;
8052 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8054 struct ctl_softc *softc = CTL_SOFTC(ctsio);
8055 struct ctl_lun *lun = CTL_LUN(ctsio);
8057 u_int32_t param_len;
8058 struct scsi_per_res_out *cdb;
8059 struct scsi_per_res_out_parms* param;
8061 uint64_t res_key, sa_res_key, key;
8063 union ctl_ha_msg persis_io;
8066 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8068 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8069 retval = CTL_RETVAL_COMPLETE;
8072 * We only support whole-LUN scope. The scope & type are ignored for
8073 * register, register and ignore existing key and clear.
8074 * We sometimes ignore scope and type on preempts too!!
8075 * Verify reservation type here as well.
8077 type = cdb->scope_type & SPR_TYPE_MASK;
8078 if ((cdb->action == SPRO_RESERVE)
8079 || (cdb->action == SPRO_RELEASE)) {
8080 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8081 ctl_set_invalid_field(/*ctsio*/ ctsio,
8087 ctl_done((union ctl_io *)ctsio);
8088 return (CTL_RETVAL_COMPLETE);
8091 if (type>8 || type==2 || type==4 || type==0) {
8092 ctl_set_invalid_field(/*ctsio*/ ctsio,
8098 ctl_done((union ctl_io *)ctsio);
8099 return (CTL_RETVAL_COMPLETE);
8103 param_len = scsi_4btoul(cdb->length);
8105 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8106 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8107 ctsio->kern_data_len = param_len;
8108 ctsio->kern_total_len = param_len;
8109 ctsio->kern_rel_offset = 0;
8110 ctsio->kern_sg_entries = 0;
8111 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8112 ctsio->be_move_done = ctl_config_move_done;
8113 ctl_datamove((union ctl_io *)ctsio);
8115 return (CTL_RETVAL_COMPLETE);
8118 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8120 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8121 res_key = scsi_8btou64(param->res_key.key);
8122 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8125 * Validate the reservation key here except for SPRO_REG_IGNO
8126 * This must be done for all other service actions
8128 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8129 mtx_lock(&lun->lun_lock);
8130 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8131 if (res_key != key) {
8133 * The current key passed in doesn't match
8134 * the one the initiator previously
8137 mtx_unlock(&lun->lun_lock);
8138 free(ctsio->kern_data_ptr, M_CTL);
8139 ctl_set_reservation_conflict(ctsio);
8140 ctl_done((union ctl_io *)ctsio);
8141 return (CTL_RETVAL_COMPLETE);
8143 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8145 * We are not registered
8147 mtx_unlock(&lun->lun_lock);
8148 free(ctsio->kern_data_ptr, M_CTL);
8149 ctl_set_reservation_conflict(ctsio);
8150 ctl_done((union ctl_io *)ctsio);
8151 return (CTL_RETVAL_COMPLETE);
8152 } else if (res_key != 0) {
8154 * We are not registered and trying to register but
8155 * the register key isn't zero.
8157 mtx_unlock(&lun->lun_lock);
8158 free(ctsio->kern_data_ptr, M_CTL);
8159 ctl_set_reservation_conflict(ctsio);
8160 ctl_done((union ctl_io *)ctsio);
8161 return (CTL_RETVAL_COMPLETE);
8163 mtx_unlock(&lun->lun_lock);
8166 switch (cdb->action & SPRO_ACTION_MASK) {
8168 case SPRO_REG_IGNO: {
8171 printf("Registration received\n");
8175 * We don't support any of these options, as we report in
8176 * the read capabilities request (see
8177 * ctl_persistent_reserve_in(), above).
8179 if ((param->flags & SPR_SPEC_I_PT)
8180 || (param->flags & SPR_ALL_TG_PT)
8181 || (param->flags & SPR_APTPL)) {
8184 if (param->flags & SPR_APTPL)
8186 else if (param->flags & SPR_ALL_TG_PT)
8188 else /* SPR_SPEC_I_PT */
8191 free(ctsio->kern_data_ptr, M_CTL);
8192 ctl_set_invalid_field(ctsio,
8198 ctl_done((union ctl_io *)ctsio);
8199 return (CTL_RETVAL_COMPLETE);
8202 mtx_lock(&lun->lun_lock);
8205 * The initiator wants to clear the
8208 if (sa_res_key == 0) {
8210 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8211 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8212 && ctl_get_prkey(lun, residx) == 0)) {
8213 mtx_unlock(&lun->lun_lock);
8217 ctl_clr_prkey(lun, residx);
8218 lun->pr_key_count--;
8220 if (residx == lun->pr_res_idx) {
8221 lun->flags &= ~CTL_LUN_PR_RESERVED;
8222 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8224 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8225 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8226 lun->pr_key_count) {
8228 * If the reservation is a registrants
8229 * only type we need to generate a UA
8230 * for other registered inits. The
8231 * sense code should be RESERVATIONS
8235 for (i = softc->init_min; i < softc->init_max; i++){
8236 if (ctl_get_prkey(lun, i) == 0)
8239 CTL_UA_RES_RELEASE);
8242 lun->pr_res_type = 0;
8243 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8244 if (lun->pr_key_count==0) {
8245 lun->flags &= ~CTL_LUN_PR_RESERVED;
8246 lun->pr_res_type = 0;
8247 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8250 lun->pr_generation++;
8251 mtx_unlock(&lun->lun_lock);
8253 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8254 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8255 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8256 persis_io.pr.pr_info.residx = residx;
8257 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8258 sizeof(persis_io.pr), M_WAITOK);
8259 } else /* sa_res_key != 0 */ {
8262 * If we aren't registered currently then increment
8263 * the key count and set the registered flag.
8265 ctl_alloc_prkey(lun, residx);
8266 if (ctl_get_prkey(lun, residx) == 0)
8267 lun->pr_key_count++;
8268 ctl_set_prkey(lun, residx, sa_res_key);
8269 lun->pr_generation++;
8270 mtx_unlock(&lun->lun_lock);
8272 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8273 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8274 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8275 persis_io.pr.pr_info.residx = residx;
8276 memcpy(persis_io.pr.pr_info.sa_res_key,
8277 param->serv_act_res_key,
8278 sizeof(param->serv_act_res_key));
8279 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8280 sizeof(persis_io.pr), M_WAITOK);
8287 printf("Reserve executed type %d\n", type);
8289 mtx_lock(&lun->lun_lock);
8290 if (lun->flags & CTL_LUN_PR_RESERVED) {
8292 * if this isn't the reservation holder and it's
8293 * not a "all registrants" type or if the type is
8294 * different then we have a conflict
8296 if ((lun->pr_res_idx != residx
8297 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8298 || lun->pr_res_type != type) {
8299 mtx_unlock(&lun->lun_lock);
8300 free(ctsio->kern_data_ptr, M_CTL);
8301 ctl_set_reservation_conflict(ctsio);
8302 ctl_done((union ctl_io *)ctsio);
8303 return (CTL_RETVAL_COMPLETE);
8305 mtx_unlock(&lun->lun_lock);
8306 } else /* create a reservation */ {
8308 * If it's not an "all registrants" type record
8309 * reservation holder
8311 if (type != SPR_TYPE_WR_EX_AR
8312 && type != SPR_TYPE_EX_AC_AR)
8313 lun->pr_res_idx = residx; /* Res holder */
8315 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8317 lun->flags |= CTL_LUN_PR_RESERVED;
8318 lun->pr_res_type = type;
8320 mtx_unlock(&lun->lun_lock);
8322 /* send msg to other side */
8323 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8324 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8325 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8326 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8327 persis_io.pr.pr_info.res_type = type;
8328 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8329 sizeof(persis_io.pr), M_WAITOK);
8334 mtx_lock(&lun->lun_lock);
8335 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8336 /* No reservation exists return good status */
8337 mtx_unlock(&lun->lun_lock);
8341 * Is this nexus a reservation holder?
8343 if (lun->pr_res_idx != residx
8344 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8346 * not a res holder return good status but
8349 mtx_unlock(&lun->lun_lock);
8353 if (lun->pr_res_type != type) {
8354 mtx_unlock(&lun->lun_lock);
8355 free(ctsio->kern_data_ptr, M_CTL);
8356 ctl_set_illegal_pr_release(ctsio);
8357 ctl_done((union ctl_io *)ctsio);
8358 return (CTL_RETVAL_COMPLETE);
8361 /* okay to release */
8362 lun->flags &= ~CTL_LUN_PR_RESERVED;
8363 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8364 lun->pr_res_type = 0;
8367 * If this isn't an exclusive access reservation and NUAR
8368 * is not set, generate UA for all other registrants.
8370 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX &&
8371 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8372 for (i = softc->init_min; i < softc->init_max; i++) {
8373 if (i == residx || ctl_get_prkey(lun, i) == 0)
8375 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8378 mtx_unlock(&lun->lun_lock);
8380 /* Send msg to other side */
8381 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8382 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8383 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8384 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8385 sizeof(persis_io.pr), M_WAITOK);
8389 /* send msg to other side */
8391 mtx_lock(&lun->lun_lock);
8392 lun->flags &= ~CTL_LUN_PR_RESERVED;
8393 lun->pr_res_type = 0;
8394 lun->pr_key_count = 0;
8395 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8397 ctl_clr_prkey(lun, residx);
8398 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8399 if (ctl_get_prkey(lun, i) != 0) {
8400 ctl_clr_prkey(lun, i);
8401 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8403 lun->pr_generation++;
8404 mtx_unlock(&lun->lun_lock);
8406 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8407 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8408 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8409 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8410 sizeof(persis_io.pr), M_WAITOK);
8414 case SPRO_PRE_ABO: {
8417 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8418 residx, ctsio, cdb, param);
8420 return (CTL_RETVAL_COMPLETE);
8424 panic("%s: Invalid PR type %#x", __func__, cdb->action);
8428 free(ctsio->kern_data_ptr, M_CTL);
8429 ctl_set_success(ctsio);
8430 ctl_done((union ctl_io *)ctsio);
8436 * This routine is for handling a message from the other SC pertaining to
8437 * persistent reserve out. All the error checking will have been done
8438 * so only perorming the action need be done here to keep the two
8442 ctl_hndl_per_res_out_on_other_sc(union ctl_io *io)
8444 struct ctl_softc *softc = CTL_SOFTC(io);
8445 union ctl_ha_msg *msg = (union ctl_ha_msg *)&io->presio.pr_msg;
8446 struct ctl_lun *lun;
8448 uint32_t residx, targ_lun;
8450 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8451 mtx_lock(&softc->ctl_lock);
8452 if (targ_lun >= CTL_MAX_LUNS ||
8453 (lun = softc->ctl_luns[targ_lun]) == NULL) {
8454 mtx_unlock(&softc->ctl_lock);
8457 mtx_lock(&lun->lun_lock);
8458 mtx_unlock(&softc->ctl_lock);
8459 if (lun->flags & CTL_LUN_DISABLED) {
8460 mtx_unlock(&lun->lun_lock);
8463 residx = ctl_get_initindex(&msg->hdr.nexus);
8464 switch(msg->pr.pr_info.action) {
8465 case CTL_PR_REG_KEY:
8466 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8467 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8468 lun->pr_key_count++;
8469 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8470 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8471 lun->pr_generation++;
8474 case CTL_PR_UNREG_KEY:
8475 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8476 lun->pr_key_count--;
8478 /* XXX Need to see if the reservation has been released */
8479 /* if so do we need to generate UA? */
8480 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8481 lun->flags &= ~CTL_LUN_PR_RESERVED;
8482 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8484 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8485 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8486 lun->pr_key_count) {
8488 * If the reservation is a registrants
8489 * only type we need to generate a UA
8490 * for other registered inits. The
8491 * sense code should be RESERVATIONS
8495 for (i = softc->init_min; i < softc->init_max; i++) {
8496 if (ctl_get_prkey(lun, i) == 0)
8499 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8502 lun->pr_res_type = 0;
8503 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8504 if (lun->pr_key_count==0) {
8505 lun->flags &= ~CTL_LUN_PR_RESERVED;
8506 lun->pr_res_type = 0;
8507 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8510 lun->pr_generation++;
8513 case CTL_PR_RESERVE:
8514 lun->flags |= CTL_LUN_PR_RESERVED;
8515 lun->pr_res_type = msg->pr.pr_info.res_type;
8516 lun->pr_res_idx = msg->pr.pr_info.residx;
8520 case CTL_PR_RELEASE:
8522 * If this isn't an exclusive access reservation and NUAR
8523 * is not set, generate UA for all other registrants.
8525 if (lun->pr_res_type != SPR_TYPE_EX_AC &&
8526 lun->pr_res_type != SPR_TYPE_WR_EX &&
8527 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8528 for (i = softc->init_min; i < softc->init_max; i++)
8529 if (i == residx || ctl_get_prkey(lun, i) == 0)
8531 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8534 lun->flags &= ~CTL_LUN_PR_RESERVED;
8535 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8536 lun->pr_res_type = 0;
8539 case CTL_PR_PREEMPT:
8540 ctl_pro_preempt_other(lun, msg);
8543 lun->flags &= ~CTL_LUN_PR_RESERVED;
8544 lun->pr_res_type = 0;
8545 lun->pr_key_count = 0;
8546 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8548 for (i=0; i < CTL_MAX_INITIATORS; i++) {
8549 if (ctl_get_prkey(lun, i) == 0)
8551 ctl_clr_prkey(lun, i);
8552 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8554 lun->pr_generation++;
8558 mtx_unlock(&lun->lun_lock);
8562 ctl_read_write(struct ctl_scsiio *ctsio)
8564 struct ctl_lun *lun = CTL_LUN(ctsio);
8565 struct ctl_lba_len_flags *lbalen;
8567 uint32_t num_blocks;
8571 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8574 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8575 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8576 switch (ctsio->cdb[0]) {
8579 struct scsi_rw_6 *cdb;
8581 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8583 lba = scsi_3btoul(cdb->addr);
8584 /* only 5 bits are valid in the most significant address byte */
8586 num_blocks = cdb->length;
8588 * This is correct according to SBC-2.
8590 if (num_blocks == 0)
8596 struct scsi_rw_10 *cdb;
8598 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8599 if (cdb->byte2 & SRW10_FUA)
8600 flags |= CTL_LLF_FUA;
8601 if (cdb->byte2 & SRW10_DPO)
8602 flags |= CTL_LLF_DPO;
8603 lba = scsi_4btoul(cdb->addr);
8604 num_blocks = scsi_2btoul(cdb->length);
8607 case WRITE_VERIFY_10: {
8608 struct scsi_write_verify_10 *cdb;
8610 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8611 flags |= CTL_LLF_FUA;
8612 if (cdb->byte2 & SWV_DPO)
8613 flags |= CTL_LLF_DPO;
8614 lba = scsi_4btoul(cdb->addr);
8615 num_blocks = scsi_2btoul(cdb->length);
8620 struct scsi_rw_12 *cdb;
8622 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8623 if (cdb->byte2 & SRW12_FUA)
8624 flags |= CTL_LLF_FUA;
8625 if (cdb->byte2 & SRW12_DPO)
8626 flags |= CTL_LLF_DPO;
8627 lba = scsi_4btoul(cdb->addr);
8628 num_blocks = scsi_4btoul(cdb->length);
8631 case WRITE_VERIFY_12: {
8632 struct scsi_write_verify_12 *cdb;
8634 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8635 flags |= CTL_LLF_FUA;
8636 if (cdb->byte2 & SWV_DPO)
8637 flags |= CTL_LLF_DPO;
8638 lba = scsi_4btoul(cdb->addr);
8639 num_blocks = scsi_4btoul(cdb->length);
8644 struct scsi_rw_16 *cdb;
8646 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8647 if (cdb->byte2 & SRW12_FUA)
8648 flags |= CTL_LLF_FUA;
8649 if (cdb->byte2 & SRW12_DPO)
8650 flags |= CTL_LLF_DPO;
8651 lba = scsi_8btou64(cdb->addr);
8652 num_blocks = scsi_4btoul(cdb->length);
8655 case WRITE_ATOMIC_16: {
8656 struct scsi_write_atomic_16 *cdb;
8658 if (lun->be_lun->atomicblock == 0) {
8659 ctl_set_invalid_opcode(ctsio);
8660 ctl_done((union ctl_io *)ctsio);
8661 return (CTL_RETVAL_COMPLETE);
8664 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb;
8665 if (cdb->byte2 & SRW12_FUA)
8666 flags |= CTL_LLF_FUA;
8667 if (cdb->byte2 & SRW12_DPO)
8668 flags |= CTL_LLF_DPO;
8669 lba = scsi_8btou64(cdb->addr);
8670 num_blocks = scsi_2btoul(cdb->length);
8671 if (num_blocks > lun->be_lun->atomicblock) {
8672 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8673 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8675 ctl_done((union ctl_io *)ctsio);
8676 return (CTL_RETVAL_COMPLETE);
8680 case WRITE_VERIFY_16: {
8681 struct scsi_write_verify_16 *cdb;
8683 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8684 flags |= CTL_LLF_FUA;
8685 if (cdb->byte2 & SWV_DPO)
8686 flags |= CTL_LLF_DPO;
8687 lba = scsi_8btou64(cdb->addr);
8688 num_blocks = scsi_4btoul(cdb->length);
8693 * We got a command we don't support. This shouldn't
8694 * happen, commands should be filtered out above us.
8696 ctl_set_invalid_opcode(ctsio);
8697 ctl_done((union ctl_io *)ctsio);
8699 return (CTL_RETVAL_COMPLETE);
8700 break; /* NOTREACHED */
8704 * The first check is to make sure we're in bounds, the second
8705 * check is to catch wrap-around problems. If the lba + num blocks
8706 * is less than the lba, then we've wrapped around and the block
8707 * range is invalid anyway.
8709 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8710 || ((lba + num_blocks) < lba)) {
8711 ctl_set_lba_out_of_range(ctsio,
8712 MAX(lba, lun->be_lun->maxlba + 1));
8713 ctl_done((union ctl_io *)ctsio);
8714 return (CTL_RETVAL_COMPLETE);
8718 * According to SBC-3, a transfer length of 0 is not an error.
8719 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8720 * translates to 256 blocks for those commands.
8722 if (num_blocks == 0) {
8723 ctl_set_success(ctsio);
8724 ctl_done((union ctl_io *)ctsio);
8725 return (CTL_RETVAL_COMPLETE);
8728 /* Set FUA and/or DPO if caches are disabled. */
8730 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0)
8731 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8733 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8734 flags |= CTL_LLF_FUA;
8737 lbalen = (struct ctl_lba_len_flags *)
8738 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8740 lbalen->len = num_blocks;
8741 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8743 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8744 ctsio->kern_rel_offset = 0;
8746 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8748 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8753 ctl_cnw_cont(union ctl_io *io)
8755 struct ctl_lun *lun = CTL_LUN(io);
8756 struct ctl_scsiio *ctsio;
8757 struct ctl_lba_len_flags *lbalen;
8760 ctsio = &io->scsiio;
8761 ctsio->io_hdr.status = CTL_STATUS_NONE;
8762 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8763 lbalen = (struct ctl_lba_len_flags *)
8764 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8765 lbalen->flags &= ~CTL_LLF_COMPARE;
8766 lbalen->flags |= CTL_LLF_WRITE;
8768 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8769 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8774 ctl_cnw(struct ctl_scsiio *ctsio)
8776 struct ctl_lun *lun = CTL_LUN(ctsio);
8777 struct ctl_lba_len_flags *lbalen;
8779 uint32_t num_blocks;
8782 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8785 switch (ctsio->cdb[0]) {
8786 case COMPARE_AND_WRITE: {
8787 struct scsi_compare_and_write *cdb;
8789 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8790 if (cdb->byte2 & SRW10_FUA)
8791 flags |= CTL_LLF_FUA;
8792 if (cdb->byte2 & SRW10_DPO)
8793 flags |= CTL_LLF_DPO;
8794 lba = scsi_8btou64(cdb->addr);
8795 num_blocks = cdb->length;
8800 * We got a command we don't support. This shouldn't
8801 * happen, commands should be filtered out above us.
8803 ctl_set_invalid_opcode(ctsio);
8804 ctl_done((union ctl_io *)ctsio);
8806 return (CTL_RETVAL_COMPLETE);
8807 break; /* NOTREACHED */
8811 * The first check is to make sure we're in bounds, the second
8812 * check is to catch wrap-around problems. If the lba + num blocks
8813 * is less than the lba, then we've wrapped around and the block
8814 * range is invalid anyway.
8816 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8817 || ((lba + num_blocks) < lba)) {
8818 ctl_set_lba_out_of_range(ctsio,
8819 MAX(lba, lun->be_lun->maxlba + 1));
8820 ctl_done((union ctl_io *)ctsio);
8821 return (CTL_RETVAL_COMPLETE);
8825 * According to SBC-3, a transfer length of 0 is not an error.
8827 if (num_blocks == 0) {
8828 ctl_set_success(ctsio);
8829 ctl_done((union ctl_io *)ctsio);
8830 return (CTL_RETVAL_COMPLETE);
8833 /* Set FUA if write cache is disabled. */
8834 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8835 flags |= CTL_LLF_FUA;
8837 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
8838 ctsio->kern_rel_offset = 0;
8841 * Set the IO_CONT flag, so that if this I/O gets passed to
8842 * ctl_data_submit_done(), it'll get passed back to
8843 * ctl_ctl_cnw_cont() for further processing.
8845 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
8846 ctsio->io_cont = ctl_cnw_cont;
8848 lbalen = (struct ctl_lba_len_flags *)
8849 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8851 lbalen->len = num_blocks;
8852 lbalen->flags = CTL_LLF_COMPARE | flags;
8854 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
8855 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8860 ctl_verify(struct ctl_scsiio *ctsio)
8862 struct ctl_lun *lun = CTL_LUN(ctsio);
8863 struct ctl_lba_len_flags *lbalen;
8865 uint32_t num_blocks;
8869 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
8872 flags = CTL_LLF_FUA;
8873 switch (ctsio->cdb[0]) {
8875 struct scsi_verify_10 *cdb;
8877 cdb = (struct scsi_verify_10 *)ctsio->cdb;
8878 if (cdb->byte2 & SVFY_BYTCHK)
8880 if (cdb->byte2 & SVFY_DPO)
8881 flags |= CTL_LLF_DPO;
8882 lba = scsi_4btoul(cdb->addr);
8883 num_blocks = scsi_2btoul(cdb->length);
8887 struct scsi_verify_12 *cdb;
8889 cdb = (struct scsi_verify_12 *)ctsio->cdb;
8890 if (cdb->byte2 & SVFY_BYTCHK)
8892 if (cdb->byte2 & SVFY_DPO)
8893 flags |= CTL_LLF_DPO;
8894 lba = scsi_4btoul(cdb->addr);
8895 num_blocks = scsi_4btoul(cdb->length);
8899 struct scsi_rw_16 *cdb;
8901 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8902 if (cdb->byte2 & SVFY_BYTCHK)
8904 if (cdb->byte2 & SVFY_DPO)
8905 flags |= CTL_LLF_DPO;
8906 lba = scsi_8btou64(cdb->addr);
8907 num_blocks = scsi_4btoul(cdb->length);
8912 * We got a command we don't support. This shouldn't
8913 * happen, commands should be filtered out above us.
8915 ctl_set_invalid_opcode(ctsio);
8916 ctl_done((union ctl_io *)ctsio);
8917 return (CTL_RETVAL_COMPLETE);
8921 * The first check is to make sure we're in bounds, the second
8922 * check is to catch wrap-around problems. If the lba + num blocks
8923 * is less than the lba, then we've wrapped around and the block
8924 * range is invalid anyway.
8926 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8927 || ((lba + num_blocks) < lba)) {
8928 ctl_set_lba_out_of_range(ctsio,
8929 MAX(lba, lun->be_lun->maxlba + 1));
8930 ctl_done((union ctl_io *)ctsio);
8931 return (CTL_RETVAL_COMPLETE);
8935 * According to SBC-3, a transfer length of 0 is not an error.
8937 if (num_blocks == 0) {
8938 ctl_set_success(ctsio);
8939 ctl_done((union ctl_io *)ctsio);
8940 return (CTL_RETVAL_COMPLETE);
8943 lbalen = (struct ctl_lba_len_flags *)
8944 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8946 lbalen->len = num_blocks;
8948 lbalen->flags = CTL_LLF_COMPARE | flags;
8949 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8951 lbalen->flags = CTL_LLF_VERIFY | flags;
8952 ctsio->kern_total_len = 0;
8954 ctsio->kern_rel_offset = 0;
8956 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
8957 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8962 ctl_report_luns(struct ctl_scsiio *ctsio)
8964 struct ctl_softc *softc = CTL_SOFTC(ctsio);
8965 struct ctl_port *port = CTL_PORT(ctsio);
8966 struct ctl_lun *lun, *request_lun = CTL_LUN(ctsio);
8967 struct scsi_report_luns *cdb;
8968 struct scsi_report_luns_data *lun_data;
8969 int num_filled, num_luns, num_port_luns, retval;
8970 uint32_t alloc_len, lun_datalen;
8971 uint32_t initidx, targ_lun_id, lun_id;
8973 retval = CTL_RETVAL_COMPLETE;
8974 cdb = (struct scsi_report_luns *)ctsio->cdb;
8976 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
8979 num_port_luns = port->lun_map ? port->lun_map_size : CTL_MAX_LUNS;
8980 mtx_lock(&softc->ctl_lock);
8981 for (targ_lun_id = 0; targ_lun_id < num_port_luns; targ_lun_id++) {
8982 if (ctl_lun_map_from_port(port, targ_lun_id) != UINT32_MAX)
8985 mtx_unlock(&softc->ctl_lock);
8987 switch (cdb->select_report) {
8988 case RPL_REPORT_DEFAULT:
8989 case RPL_REPORT_ALL:
8990 case RPL_REPORT_NONSUBSID:
8992 case RPL_REPORT_WELLKNOWN:
8993 case RPL_REPORT_ADMIN:
8994 case RPL_REPORT_CONGLOM:
8998 ctl_set_invalid_field(ctsio,
9004 ctl_done((union ctl_io *)ctsio);
9006 break; /* NOTREACHED */
9009 alloc_len = scsi_4btoul(cdb->length);
9011 * The initiator has to allocate at least 16 bytes for this request,
9012 * so he can at least get the header and the first LUN. Otherwise
9013 * we reject the request (per SPC-3 rev 14, section 6.21).
9015 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9016 sizeof(struct scsi_report_luns_lundata))) {
9017 ctl_set_invalid_field(ctsio,
9023 ctl_done((union ctl_io *)ctsio);
9027 lun_datalen = sizeof(*lun_data) +
9028 (num_luns * sizeof(struct scsi_report_luns_lundata));
9030 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9031 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9032 ctsio->kern_sg_entries = 0;
9034 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9036 mtx_lock(&softc->ctl_lock);
9037 for (targ_lun_id = 0, num_filled = 0;
9038 targ_lun_id < num_port_luns && num_filled < num_luns;
9040 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9041 if (lun_id == UINT32_MAX)
9043 lun = softc->ctl_luns[lun_id];
9047 be64enc(lun_data->luns[num_filled++].lundata,
9048 ctl_encode_lun(targ_lun_id));
9051 * According to SPC-3, rev 14 section 6.21:
9053 * "The execution of a REPORT LUNS command to any valid and
9054 * installed logical unit shall clear the REPORTED LUNS DATA
9055 * HAS CHANGED unit attention condition for all logical
9056 * units of that target with respect to the requesting
9057 * initiator. A valid and installed logical unit is one
9058 * having a PERIPHERAL QUALIFIER of 000b in the standard
9059 * INQUIRY data (see 6.4.2)."
9061 * If request_lun is NULL, the LUN this report luns command
9062 * was issued to is either disabled or doesn't exist. In that
9063 * case, we shouldn't clear any pending lun change unit
9066 if (request_lun != NULL) {
9067 mtx_lock(&lun->lun_lock);
9068 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE);
9069 mtx_unlock(&lun->lun_lock);
9072 mtx_unlock(&softc->ctl_lock);
9075 * It's quite possible that we've returned fewer LUNs than we allocated
9076 * space for. Trim it.
9078 lun_datalen = sizeof(*lun_data) +
9079 (num_filled * sizeof(struct scsi_report_luns_lundata));
9080 ctsio->kern_rel_offset = 0;
9081 ctsio->kern_sg_entries = 0;
9082 ctsio->kern_data_len = min(lun_datalen, alloc_len);
9083 ctsio->kern_total_len = ctsio->kern_data_len;
9086 * We set this to the actual data length, regardless of how much
9087 * space we actually have to return results. If the user looks at
9088 * this value, he'll know whether or not he allocated enough space
9089 * and reissue the command if necessary. We don't support well
9090 * known logical units, so if the user asks for that, return none.
9092 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9095 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9098 ctl_set_success(ctsio);
9099 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9100 ctsio->be_move_done = ctl_config_move_done;
9101 ctl_datamove((union ctl_io *)ctsio);
9106 ctl_request_sense(struct ctl_scsiio *ctsio)
9108 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9109 struct ctl_lun *lun = CTL_LUN(ctsio);
9110 struct scsi_request_sense *cdb;
9111 struct scsi_sense_data *sense_ptr;
9114 u_int sense_len = SSD_FULL_SIZE;
9115 scsi_sense_data_type sense_format;
9116 ctl_ua_type ua_type;
9117 uint8_t asc = 0, ascq = 0;
9119 cdb = (struct scsi_request_sense *)ctsio->cdb;
9121 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9124 * Determine which sense format the user wants.
9126 if (cdb->byte2 & SRS_DESC)
9127 sense_format = SSD_TYPE_DESC;
9129 sense_format = SSD_TYPE_FIXED;
9131 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9132 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9133 ctsio->kern_sg_entries = 0;
9134 ctsio->kern_rel_offset = 0;
9137 * struct scsi_sense_data, which is currently set to 256 bytes, is
9138 * larger than the largest allowed value for the length field in the
9139 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9141 ctsio->kern_data_len = cdb->length;
9142 ctsio->kern_total_len = cdb->length;
9145 * If we don't have a LUN, we don't have any pending sense.
9148 ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
9149 softc->ha_link < CTL_HA_LINK_UNKNOWN)) {
9150 /* "Logical unit not supported" */
9151 ctl_set_sense_data(sense_ptr, &sense_len, NULL, sense_format,
9152 /*current_error*/ 1,
9153 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
9161 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9163 * Check for pending sense, and then for pending unit attentions.
9164 * Pending sense gets returned first, then pending unit attentions.
9166 mtx_lock(&lun->lun_lock);
9168 if (ctl_is_set(lun->have_ca, initidx)) {
9169 scsi_sense_data_type stored_format;
9172 * Check to see which sense format was used for the stored
9175 stored_format = scsi_sense_type(&lun->pending_sense[initidx]);
9178 * If the user requested a different sense format than the
9179 * one we stored, then we need to convert it to the other
9180 * format. If we're going from descriptor to fixed format
9181 * sense data, we may lose things in translation, depending
9182 * on what options were used.
9184 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9185 * for some reason we'll just copy it out as-is.
9187 if ((stored_format == SSD_TYPE_FIXED)
9188 && (sense_format == SSD_TYPE_DESC))
9189 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9190 &lun->pending_sense[initidx],
9191 (struct scsi_sense_data_desc *)sense_ptr);
9192 else if ((stored_format == SSD_TYPE_DESC)
9193 && (sense_format == SSD_TYPE_FIXED))
9194 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9195 &lun->pending_sense[initidx],
9196 (struct scsi_sense_data_fixed *)sense_ptr);
9198 memcpy(sense_ptr, &lun->pending_sense[initidx],
9199 MIN(sizeof(*sense_ptr),
9200 sizeof(lun->pending_sense[initidx])));
9202 ctl_clear_mask(lun->have_ca, initidx);
9206 if (have_error == 0) {
9207 ua_type = ctl_build_ua(lun, initidx, sense_ptr, &sense_len,
9209 if (ua_type != CTL_UA_NONE)
9212 if (have_error == 0) {
9214 * Report informational exception if have one and allowed.
9216 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) {
9218 ascq = lun->ie_ascq;
9220 ctl_set_sense_data(sense_ptr, &sense_len, lun, sense_format,
9221 /*current_error*/ 1,
9222 /*sense_key*/ SSD_KEY_NO_SENSE,
9227 mtx_unlock(&lun->lun_lock);
9231 * We report the SCSI status as OK, since the status of the command
9232 * itself is OK. We're reporting sense as parameter data.
9234 ctl_set_success(ctsio);
9235 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9236 ctsio->be_move_done = ctl_config_move_done;
9237 ctl_datamove((union ctl_io *)ctsio);
9238 return (CTL_RETVAL_COMPLETE);
9242 ctl_tur(struct ctl_scsiio *ctsio)
9245 CTL_DEBUG_PRINT(("ctl_tur\n"));
9247 ctl_set_success(ctsio);
9248 ctl_done((union ctl_io *)ctsio);
9250 return (CTL_RETVAL_COMPLETE);
9254 * SCSI VPD page 0x00, the Supported VPD Pages page.
9257 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9259 struct ctl_lun *lun = CTL_LUN(ctsio);
9260 struct scsi_vpd_supported_pages *pages;
9264 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9265 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9266 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9267 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9268 ctsio->kern_rel_offset = 0;
9269 ctsio->kern_sg_entries = 0;
9270 ctsio->kern_data_len = min(sup_page_size, alloc_len);
9271 ctsio->kern_total_len = ctsio->kern_data_len;
9274 * The control device is always connected. The disk device, on the
9275 * other hand, may not be online all the time. Need to change this
9276 * to figure out whether the disk device is actually online or not.
9279 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9280 lun->be_lun->lun_type;
9282 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9285 /* Supported VPD pages */
9286 pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9288 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9289 /* Device Identification */
9290 pages->page_list[p++] = SVPD_DEVICE_ID;
9291 /* Extended INQUIRY Data */
9292 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9293 /* Mode Page Policy */
9294 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9296 pages->page_list[p++] = SVPD_SCSI_PORTS;
9297 /* Third-party Copy */
9298 pages->page_list[p++] = SVPD_SCSI_TPC;
9299 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9301 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9302 /* Block Device Characteristics */
9303 pages->page_list[p++] = SVPD_BDC;
9304 /* Logical Block Provisioning */
9305 pages->page_list[p++] = SVPD_LBP;
9309 ctl_set_success(ctsio);
9310 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9311 ctsio->be_move_done = ctl_config_move_done;
9312 ctl_datamove((union ctl_io *)ctsio);
9313 return (CTL_RETVAL_COMPLETE);
9317 * SCSI VPD page 0x80, the Unit Serial Number page.
9320 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9322 struct ctl_lun *lun = CTL_LUN(ctsio);
9323 struct scsi_vpd_unit_serial_number *sn_ptr;
9326 data_len = 4 + CTL_SN_LEN;
9327 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9328 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9329 ctsio->kern_rel_offset = 0;
9330 ctsio->kern_sg_entries = 0;
9331 ctsio->kern_data_len = min(data_len, alloc_len);
9332 ctsio->kern_total_len = ctsio->kern_data_len;
9335 * The control device is always connected. The disk device, on the
9336 * other hand, may not be online all the time. Need to change this
9337 * to figure out whether the disk device is actually online or not.
9340 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9341 lun->be_lun->lun_type;
9343 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9345 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9346 sn_ptr->length = CTL_SN_LEN;
9348 * If we don't have a LUN, we just leave the serial number as
9352 strncpy((char *)sn_ptr->serial_num,
9353 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9355 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9357 ctl_set_success(ctsio);
9358 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9359 ctsio->be_move_done = ctl_config_move_done;
9360 ctl_datamove((union ctl_io *)ctsio);
9361 return (CTL_RETVAL_COMPLETE);
9366 * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9369 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9371 struct ctl_lun *lun = CTL_LUN(ctsio);
9372 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9375 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9376 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9377 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9378 ctsio->kern_sg_entries = 0;
9379 ctsio->kern_rel_offset = 0;
9380 ctsio->kern_data_len = min(data_len, alloc_len);
9381 ctsio->kern_total_len = ctsio->kern_data_len;
9384 * The control device is always connected. The disk device, on the
9385 * other hand, may not be online all the time.
9388 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9389 lun->be_lun->lun_type;
9391 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9392 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9393 scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9395 * We support head of queue, ordered and simple tags.
9397 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9399 * Volatile cache supported.
9401 eid_ptr->flags3 = SVPD_EID_V_SUP;
9404 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9405 * attention for a particular IT nexus on all LUNs once we report
9406 * it to that nexus once. This bit is required as of SPC-4.
9408 eid_ptr->flags4 = SVPD_EID_LUICLR;
9411 * We support revert to defaults (RTD) bit in MODE SELECT.
9413 eid_ptr->flags5 = SVPD_EID_RTD_SUP;
9416 * XXX KDM in order to correctly answer this, we would need
9417 * information from the SIM to determine how much sense data it
9418 * can send. So this would really be a path inquiry field, most
9419 * likely. This can be set to a maximum of 252 according to SPC-4,
9420 * but the hardware may or may not be able to support that much.
9421 * 0 just means that the maximum sense data length is not reported.
9423 eid_ptr->max_sense_length = 0;
9425 ctl_set_success(ctsio);
9426 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9427 ctsio->be_move_done = ctl_config_move_done;
9428 ctl_datamove((union ctl_io *)ctsio);
9429 return (CTL_RETVAL_COMPLETE);
9433 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9435 struct ctl_lun *lun = CTL_LUN(ctsio);
9436 struct scsi_vpd_mode_page_policy *mpp_ptr;
9439 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9440 sizeof(struct scsi_vpd_mode_page_policy_descr);
9442 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9443 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9444 ctsio->kern_rel_offset = 0;
9445 ctsio->kern_sg_entries = 0;
9446 ctsio->kern_data_len = min(data_len, alloc_len);
9447 ctsio->kern_total_len = ctsio->kern_data_len;
9450 * The control device is always connected. The disk device, on the
9451 * other hand, may not be online all the time.
9454 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9455 lun->be_lun->lun_type;
9457 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9458 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9459 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9460 mpp_ptr->descr[0].page_code = 0x3f;
9461 mpp_ptr->descr[0].subpage_code = 0xff;
9462 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9464 ctl_set_success(ctsio);
9465 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9466 ctsio->be_move_done = ctl_config_move_done;
9467 ctl_datamove((union ctl_io *)ctsio);
9468 return (CTL_RETVAL_COMPLETE);
9472 * SCSI VPD page 0x83, the Device Identification page.
9475 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9477 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9478 struct ctl_port *port = CTL_PORT(ctsio);
9479 struct ctl_lun *lun = CTL_LUN(ctsio);
9480 struct scsi_vpd_device_id *devid_ptr;
9481 struct scsi_vpd_id_descriptor *desc;
9485 data_len = sizeof(struct scsi_vpd_device_id) +
9486 sizeof(struct scsi_vpd_id_descriptor) +
9487 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9488 sizeof(struct scsi_vpd_id_descriptor) +
9489 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9490 if (lun && lun->lun_devid)
9491 data_len += lun->lun_devid->len;
9492 if (port && port->port_devid)
9493 data_len += port->port_devid->len;
9494 if (port && port->target_devid)
9495 data_len += port->target_devid->len;
9497 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9498 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9499 ctsio->kern_sg_entries = 0;
9500 ctsio->kern_rel_offset = 0;
9501 ctsio->kern_sg_entries = 0;
9502 ctsio->kern_data_len = min(data_len, alloc_len);
9503 ctsio->kern_total_len = ctsio->kern_data_len;
9506 * The control device is always connected. The disk device, on the
9507 * other hand, may not be online all the time.
9510 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9511 lun->be_lun->lun_type;
9513 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9514 devid_ptr->page_code = SVPD_DEVICE_ID;
9515 scsi_ulto2b(data_len - 4, devid_ptr->length);
9517 if (port && port->port_type == CTL_PORT_FC)
9518 proto = SCSI_PROTO_FC << 4;
9519 else if (port && port->port_type == CTL_PORT_ISCSI)
9520 proto = SCSI_PROTO_ISCSI << 4;
9522 proto = SCSI_PROTO_SPI << 4;
9523 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9526 * We're using a LUN association here. i.e., this device ID is a
9527 * per-LUN identifier.
9529 if (lun && lun->lun_devid) {
9530 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9531 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9532 lun->lun_devid->len);
9536 * This is for the WWPN which is a port association.
9538 if (port && port->port_devid) {
9539 memcpy(desc, port->port_devid->data, port->port_devid->len);
9540 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9541 port->port_devid->len);
9545 * This is for the Relative Target Port(type 4h) identifier
9547 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9548 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9549 SVPD_ID_TYPE_RELTARG;
9551 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9552 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9553 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9556 * This is for the Target Port Group(type 5h) identifier
9558 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9559 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9560 SVPD_ID_TYPE_TPORTGRP;
9562 if (softc->is_single ||
9563 (port && port->status & CTL_PORT_STATUS_HA_SHARED))
9566 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt;
9567 scsi_ulto2b(g, &desc->identifier[2]);
9568 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9569 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9572 * This is for the Target identifier
9574 if (port && port->target_devid) {
9575 memcpy(desc, port->target_devid->data, port->target_devid->len);
9578 ctl_set_success(ctsio);
9579 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9580 ctsio->be_move_done = ctl_config_move_done;
9581 ctl_datamove((union ctl_io *)ctsio);
9582 return (CTL_RETVAL_COMPLETE);
9586 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9588 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9589 struct ctl_lun *lun = CTL_LUN(ctsio);
9590 struct scsi_vpd_scsi_ports *sp;
9591 struct scsi_vpd_port_designation *pd;
9592 struct scsi_vpd_port_designation_cont *pdc;
9593 struct ctl_port *port;
9594 int data_len, num_target_ports, iid_len, id_len;
9596 num_target_ports = 0;
9599 mtx_lock(&softc->ctl_lock);
9600 STAILQ_FOREACH(port, &softc->port_list, links) {
9601 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9604 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9607 if (port->init_devid)
9608 iid_len += port->init_devid->len;
9609 if (port->port_devid)
9610 id_len += port->port_devid->len;
9612 mtx_unlock(&softc->ctl_lock);
9614 data_len = sizeof(struct scsi_vpd_scsi_ports) +
9615 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9616 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9617 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9618 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9619 ctsio->kern_sg_entries = 0;
9620 ctsio->kern_rel_offset = 0;
9621 ctsio->kern_sg_entries = 0;
9622 ctsio->kern_data_len = min(data_len, alloc_len);
9623 ctsio->kern_total_len = ctsio->kern_data_len;
9626 * The control device is always connected. The disk device, on the
9627 * other hand, may not be online all the time. Need to change this
9628 * to figure out whether the disk device is actually online or not.
9631 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9632 lun->be_lun->lun_type;
9634 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9636 sp->page_code = SVPD_SCSI_PORTS;
9637 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9639 pd = &sp->design[0];
9641 mtx_lock(&softc->ctl_lock);
9642 STAILQ_FOREACH(port, &softc->port_list, links) {
9643 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9646 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9648 scsi_ulto2b(port->targ_port, pd->relative_port_id);
9649 if (port->init_devid) {
9650 iid_len = port->init_devid->len;
9651 memcpy(pd->initiator_transportid,
9652 port->init_devid->data, port->init_devid->len);
9655 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9656 pdc = (struct scsi_vpd_port_designation_cont *)
9657 (&pd->initiator_transportid[iid_len]);
9658 if (port->port_devid) {
9659 id_len = port->port_devid->len;
9660 memcpy(pdc->target_port_descriptors,
9661 port->port_devid->data, port->port_devid->len);
9664 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9665 pd = (struct scsi_vpd_port_designation *)
9666 ((uint8_t *)pdc->target_port_descriptors + id_len);
9668 mtx_unlock(&softc->ctl_lock);
9670 ctl_set_success(ctsio);
9671 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9672 ctsio->be_move_done = ctl_config_move_done;
9673 ctl_datamove((union ctl_io *)ctsio);
9674 return (CTL_RETVAL_COMPLETE);
9678 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9680 struct ctl_lun *lun = CTL_LUN(ctsio);
9681 struct scsi_vpd_block_limits *bl_ptr;
9684 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9685 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9686 ctsio->kern_sg_entries = 0;
9687 ctsio->kern_rel_offset = 0;
9688 ctsio->kern_sg_entries = 0;
9689 ctsio->kern_data_len = min(sizeof(*bl_ptr), alloc_len);
9690 ctsio->kern_total_len = ctsio->kern_data_len;
9693 * The control device is always connected. The disk device, on the
9694 * other hand, may not be online all the time. Need to change this
9695 * to figure out whether the disk device is actually online or not.
9698 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9699 lun->be_lun->lun_type;
9701 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9703 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9704 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
9705 bl_ptr->max_cmp_write_len = 0xff;
9706 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9708 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
9709 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9711 ctl_get_opt_number(&lun->be_lun->options,
9712 "unmap_max_lba", &ival);
9713 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt);
9715 ctl_get_opt_number(&lun->be_lun->options,
9716 "unmap_max_descr", &ival);
9717 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt);
9718 if (lun->be_lun->ublockexp != 0) {
9719 scsi_ulto4b((1 << lun->be_lun->ublockexp),
9720 bl_ptr->opt_unmap_grain);
9721 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
9722 bl_ptr->unmap_grain_align);
9725 scsi_ulto4b(lun->be_lun->atomicblock,
9726 bl_ptr->max_atomic_transfer_length);
9727 scsi_ulto4b(0, bl_ptr->atomic_alignment);
9728 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
9729 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary);
9730 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size);
9732 ctl_get_opt_number(&lun->be_lun->options, "write_same_max_lba", &ival);
9733 scsi_u64to8b(ival, bl_ptr->max_write_same_length);
9736 ctl_set_success(ctsio);
9737 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9738 ctsio->be_move_done = ctl_config_move_done;
9739 ctl_datamove((union ctl_io *)ctsio);
9740 return (CTL_RETVAL_COMPLETE);
9744 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
9746 struct ctl_lun *lun = CTL_LUN(ctsio);
9747 struct scsi_vpd_block_device_characteristics *bdc_ptr;
9751 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
9752 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
9753 ctsio->kern_sg_entries = 0;
9754 ctsio->kern_rel_offset = 0;
9755 ctsio->kern_data_len = min(sizeof(*bdc_ptr), alloc_len);
9756 ctsio->kern_total_len = ctsio->kern_data_len;
9759 * The control device is always connected. The disk device, on the
9760 * other hand, may not be online all the time. Need to change this
9761 * to figure out whether the disk device is actually online or not.
9764 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9765 lun->be_lun->lun_type;
9767 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9768 bdc_ptr->page_code = SVPD_BDC;
9769 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
9771 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL)
9772 i = strtol(value, NULL, 0);
9774 i = CTL_DEFAULT_ROTATION_RATE;
9775 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
9777 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL)
9778 i = strtol(value, NULL, 0);
9781 bdc_ptr->wab_wac_ff = (i & 0x0f);
9782 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
9784 ctl_set_success(ctsio);
9785 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9786 ctsio->be_move_done = ctl_config_move_done;
9787 ctl_datamove((union ctl_io *)ctsio);
9788 return (CTL_RETVAL_COMPLETE);
9792 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
9794 struct ctl_lun *lun = CTL_LUN(ctsio);
9795 struct scsi_vpd_logical_block_prov *lbp_ptr;
9798 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
9799 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
9800 ctsio->kern_sg_entries = 0;
9801 ctsio->kern_rel_offset = 0;
9802 ctsio->kern_data_len = min(sizeof(*lbp_ptr), alloc_len);
9803 ctsio->kern_total_len = ctsio->kern_data_len;
9806 * The control device is always connected. The disk device, on the
9807 * other hand, may not be online all the time. Need to change this
9808 * to figure out whether the disk device is actually online or not.
9811 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9812 lun->be_lun->lun_type;
9814 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9816 lbp_ptr->page_code = SVPD_LBP;
9817 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
9818 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
9819 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9820 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
9821 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
9822 value = ctl_get_opt(&lun->be_lun->options, "provisioning_type");
9823 if (value != NULL) {
9824 if (strcmp(value, "resource") == 0)
9825 lbp_ptr->prov_type = SVPD_LBP_RESOURCE;
9826 else if (strcmp(value, "thin") == 0)
9827 lbp_ptr->prov_type = SVPD_LBP_THIN;
9829 lbp_ptr->prov_type = SVPD_LBP_THIN;
9832 ctl_set_success(ctsio);
9833 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9834 ctsio->be_move_done = ctl_config_move_done;
9835 ctl_datamove((union ctl_io *)ctsio);
9836 return (CTL_RETVAL_COMPLETE);
9840 * INQUIRY with the EVPD bit set.
9843 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
9845 struct ctl_lun *lun = CTL_LUN(ctsio);
9846 struct scsi_inquiry *cdb;
9847 int alloc_len, retval;
9849 cdb = (struct scsi_inquiry *)ctsio->cdb;
9850 alloc_len = scsi_2btoul(cdb->length);
9852 switch (cdb->page_code) {
9853 case SVPD_SUPPORTED_PAGES:
9854 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
9856 case SVPD_UNIT_SERIAL_NUMBER:
9857 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
9859 case SVPD_DEVICE_ID:
9860 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
9862 case SVPD_EXTENDED_INQUIRY_DATA:
9863 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
9865 case SVPD_MODE_PAGE_POLICY:
9866 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
9868 case SVPD_SCSI_PORTS:
9869 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
9872 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
9874 case SVPD_BLOCK_LIMITS:
9875 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
9877 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
9880 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
9882 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
9885 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
9887 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
9891 ctl_set_invalid_field(ctsio,
9897 ctl_done((union ctl_io *)ctsio);
9898 retval = CTL_RETVAL_COMPLETE;
9906 * Standard INQUIRY data.
9909 ctl_inquiry_std(struct ctl_scsiio *ctsio)
9911 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9912 struct ctl_port *port = CTL_PORT(ctsio);
9913 struct ctl_lun *lun = CTL_LUN(ctsio);
9914 struct scsi_inquiry_data *inq_ptr;
9915 struct scsi_inquiry *cdb;
9917 uint32_t alloc_len, data_len;
9918 ctl_port_type port_type;
9920 port_type = port->port_type;
9921 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
9922 port_type = CTL_PORT_SCSI;
9924 cdb = (struct scsi_inquiry *)ctsio->cdb;
9925 alloc_len = scsi_2btoul(cdb->length);
9928 * We malloc the full inquiry data size here and fill it
9929 * in. If the user only asks for less, we'll give him
9932 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
9933 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9934 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
9935 ctsio->kern_sg_entries = 0;
9936 ctsio->kern_rel_offset = 0;
9937 ctsio->kern_data_len = min(data_len, alloc_len);
9938 ctsio->kern_total_len = ctsio->kern_data_len;
9941 if ((lun->flags & CTL_LUN_PRIMARY_SC) ||
9942 softc->ha_link >= CTL_HA_LINK_UNKNOWN) {
9943 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9944 lun->be_lun->lun_type;
9946 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) |
9947 lun->be_lun->lun_type;
9949 if (lun->flags & CTL_LUN_REMOVABLE)
9950 inq_ptr->dev_qual2 |= SID_RMB;
9952 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
9954 /* RMB in byte 2 is 0 */
9955 inq_ptr->version = SCSI_REV_SPC5;
9958 * According to SAM-3, even if a device only supports a single
9959 * level of LUN addressing, it should still set the HISUP bit:
9961 * 4.9.1 Logical unit numbers overview
9963 * All logical unit number formats described in this standard are
9964 * hierarchical in structure even when only a single level in that
9965 * hierarchy is used. The HISUP bit shall be set to one in the
9966 * standard INQUIRY data (see SPC-2) when any logical unit number
9967 * format described in this standard is used. Non-hierarchical
9968 * formats are outside the scope of this standard.
9970 * Therefore we set the HiSup bit here.
9972 * The response format is 2, per SPC-3.
9974 inq_ptr->response_format = SID_HiSup | 2;
9976 inq_ptr->additional_length = data_len -
9977 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
9978 CTL_DEBUG_PRINT(("additional_length = %d\n",
9979 inq_ptr->additional_length));
9981 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
9982 if (port_type == CTL_PORT_SCSI)
9983 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
9984 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
9985 inq_ptr->flags = SID_CmdQue;
9986 if (port_type == CTL_PORT_SCSI)
9987 inq_ptr->flags |= SID_WBus16 | SID_Sync;
9990 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
9991 * We have 8 bytes for the vendor name, and 16 bytes for the device
9992 * name and 4 bytes for the revision.
9994 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
9995 "vendor")) == NULL) {
9996 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
9998 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
9999 strncpy(inq_ptr->vendor, val,
10000 min(sizeof(inq_ptr->vendor), strlen(val)));
10003 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10004 sizeof(inq_ptr->product));
10005 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10006 switch (lun->be_lun->lun_type) {
10008 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10009 sizeof(inq_ptr->product));
10012 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10013 sizeof(inq_ptr->product));
10016 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT,
10017 sizeof(inq_ptr->product));
10020 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10021 sizeof(inq_ptr->product));
10025 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10026 strncpy(inq_ptr->product, val,
10027 min(sizeof(inq_ptr->product), strlen(val)));
10031 * XXX make this a macro somewhere so it automatically gets
10032 * incremented when we make changes.
10034 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10035 "revision")) == NULL) {
10036 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10038 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10039 strncpy(inq_ptr->revision, val,
10040 min(sizeof(inq_ptr->revision), strlen(val)));
10044 * For parallel SCSI, we support double transition and single
10045 * transition clocking. We also support QAS (Quick Arbitration
10046 * and Selection) and Information Unit transfers on both the
10047 * control and array devices.
10049 if (port_type == CTL_PORT_SCSI)
10050 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10053 /* SAM-6 (no version claimed) */
10054 scsi_ulto2b(0x00C0, inq_ptr->version1);
10055 /* SPC-5 (no version claimed) */
10056 scsi_ulto2b(0x05C0, inq_ptr->version2);
10057 if (port_type == CTL_PORT_FC) {
10058 /* FCP-2 ANSI INCITS.350:2003 */
10059 scsi_ulto2b(0x0917, inq_ptr->version3);
10060 } else if (port_type == CTL_PORT_SCSI) {
10061 /* SPI-4 ANSI INCITS.362:200x */
10062 scsi_ulto2b(0x0B56, inq_ptr->version3);
10063 } else if (port_type == CTL_PORT_ISCSI) {
10064 /* iSCSI (no version claimed) */
10065 scsi_ulto2b(0x0960, inq_ptr->version3);
10066 } else if (port_type == CTL_PORT_SAS) {
10067 /* SAS (no version claimed) */
10068 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10072 /* SBC-4 (no version claimed) */
10073 scsi_ulto2b(0x0600, inq_ptr->version4);
10075 switch (lun->be_lun->lun_type) {
10077 /* SBC-4 (no version claimed) */
10078 scsi_ulto2b(0x0600, inq_ptr->version4);
10083 /* MMC-6 (no version claimed) */
10084 scsi_ulto2b(0x04E0, inq_ptr->version4);
10091 ctl_set_success(ctsio);
10092 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10093 ctsio->be_move_done = ctl_config_move_done;
10094 ctl_datamove((union ctl_io *)ctsio);
10095 return (CTL_RETVAL_COMPLETE);
10099 ctl_inquiry(struct ctl_scsiio *ctsio)
10101 struct scsi_inquiry *cdb;
10104 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10106 cdb = (struct scsi_inquiry *)ctsio->cdb;
10107 if (cdb->byte2 & SI_EVPD)
10108 retval = ctl_inquiry_evpd(ctsio);
10109 else if (cdb->page_code == 0)
10110 retval = ctl_inquiry_std(ctsio);
10112 ctl_set_invalid_field(ctsio,
10118 ctl_done((union ctl_io *)ctsio);
10119 return (CTL_RETVAL_COMPLETE);
10126 ctl_get_config(struct ctl_scsiio *ctsio)
10128 struct ctl_lun *lun = CTL_LUN(ctsio);
10129 struct scsi_get_config_header *hdr;
10130 struct scsi_get_config_feature *feature;
10131 struct scsi_get_config *cdb;
10132 uint32_t alloc_len, data_len;
10135 cdb = (struct scsi_get_config *)ctsio->cdb;
10136 rt = (cdb->rt & SGC_RT_MASK);
10137 starting = scsi_2btoul(cdb->starting_feature);
10138 alloc_len = scsi_2btoul(cdb->length);
10140 data_len = sizeof(struct scsi_get_config_header) +
10141 sizeof(struct scsi_get_config_feature) + 8 +
10142 sizeof(struct scsi_get_config_feature) + 8 +
10143 sizeof(struct scsi_get_config_feature) + 4 +
10144 sizeof(struct scsi_get_config_feature) + 4 +
10145 sizeof(struct scsi_get_config_feature) + 8 +
10146 sizeof(struct scsi_get_config_feature) +
10147 sizeof(struct scsi_get_config_feature) + 4 +
10148 sizeof(struct scsi_get_config_feature) + 4 +
10149 sizeof(struct scsi_get_config_feature) + 4 +
10150 sizeof(struct scsi_get_config_feature) + 4 +
10151 sizeof(struct scsi_get_config_feature) + 4 +
10152 sizeof(struct scsi_get_config_feature) + 4;
10153 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10154 ctsio->kern_sg_entries = 0;
10155 ctsio->kern_rel_offset = 0;
10157 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr;
10158 if (lun->flags & CTL_LUN_NO_MEDIA)
10159 scsi_ulto2b(0x0000, hdr->current_profile);
10161 scsi_ulto2b(0x0010, hdr->current_profile);
10162 feature = (struct scsi_get_config_feature *)(hdr + 1);
10164 if (starting > 0x003b)
10166 if (starting > 0x003a)
10168 if (starting > 0x002b)
10170 if (starting > 0x002a)
10172 if (starting > 0x001f)
10174 if (starting > 0x001e)
10176 if (starting > 0x001d)
10178 if (starting > 0x0010)
10180 if (starting > 0x0003)
10182 if (starting > 0x0002)
10184 if (starting > 0x0001)
10186 if (starting > 0x0000)
10190 scsi_ulto2b(0x0000, feature->feature_code);
10191 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT;
10192 feature->add_length = 8;
10193 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */
10194 feature->feature_data[2] = 0x00;
10195 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */
10196 feature->feature_data[6] = 0x01;
10197 feature = (struct scsi_get_config_feature *)
10198 &feature->feature_data[feature->add_length];
10201 scsi_ulto2b(0x0001, feature->feature_code);
10202 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10203 feature->add_length = 8;
10204 scsi_ulto4b(0x00000000, &feature->feature_data[0]);
10205 feature->feature_data[4] = 0x03;
10206 feature = (struct scsi_get_config_feature *)
10207 &feature->feature_data[feature->add_length];
10210 scsi_ulto2b(0x0002, feature->feature_code);
10211 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10212 feature->add_length = 4;
10213 feature->feature_data[0] = 0x02;
10214 feature = (struct scsi_get_config_feature *)
10215 &feature->feature_data[feature->add_length];
10217 f3: /* Removable Medium */
10218 scsi_ulto2b(0x0003, feature->feature_code);
10219 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10220 feature->add_length = 4;
10221 feature->feature_data[0] = 0x39;
10222 feature = (struct scsi_get_config_feature *)
10223 &feature->feature_data[feature->add_length];
10225 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA))
10228 f10: /* Random Read */
10229 scsi_ulto2b(0x0010, feature->feature_code);
10230 feature->flags = 0x00;
10231 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10232 feature->flags |= SGC_F_CURRENT;
10233 feature->add_length = 8;
10234 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]);
10235 scsi_ulto2b(1, &feature->feature_data[4]);
10236 feature->feature_data[6] = 0x00;
10237 feature = (struct scsi_get_config_feature *)
10238 &feature->feature_data[feature->add_length];
10240 f1d: /* Multi-Read */
10241 scsi_ulto2b(0x001D, feature->feature_code);
10242 feature->flags = 0x00;
10243 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10244 feature->flags |= SGC_F_CURRENT;
10245 feature->add_length = 0;
10246 feature = (struct scsi_get_config_feature *)
10247 &feature->feature_data[feature->add_length];
10250 scsi_ulto2b(0x001E, feature->feature_code);
10251 feature->flags = 0x00;
10252 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10253 feature->flags |= SGC_F_CURRENT;
10254 feature->add_length = 4;
10255 feature->feature_data[0] = 0x00;
10256 feature = (struct scsi_get_config_feature *)
10257 &feature->feature_data[feature->add_length];
10259 f1f: /* DVD Read */
10260 scsi_ulto2b(0x001F, feature->feature_code);
10261 feature->flags = 0x08;
10262 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10263 feature->flags |= SGC_F_CURRENT;
10264 feature->add_length = 4;
10265 feature->feature_data[0] = 0x01;
10266 feature->feature_data[2] = 0x03;
10267 feature = (struct scsi_get_config_feature *)
10268 &feature->feature_data[feature->add_length];
10271 scsi_ulto2b(0x002A, feature->feature_code);
10272 feature->flags = 0x04;
10273 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10274 feature->flags |= SGC_F_CURRENT;
10275 feature->add_length = 4;
10276 feature->feature_data[0] = 0x00;
10277 feature->feature_data[1] = 0x00;
10278 feature = (struct scsi_get_config_feature *)
10279 &feature->feature_data[feature->add_length];
10282 scsi_ulto2b(0x002B, feature->feature_code);
10283 feature->flags = 0x00;
10284 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10285 feature->flags |= SGC_F_CURRENT;
10286 feature->add_length = 4;
10287 feature->feature_data[0] = 0x00;
10288 feature = (struct scsi_get_config_feature *)
10289 &feature->feature_data[feature->add_length];
10291 f3a: /* DVD+RW Dual Layer */
10292 scsi_ulto2b(0x003A, feature->feature_code);
10293 feature->flags = 0x00;
10294 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10295 feature->flags |= SGC_F_CURRENT;
10296 feature->add_length = 4;
10297 feature->feature_data[0] = 0x00;
10298 feature->feature_data[1] = 0x00;
10299 feature = (struct scsi_get_config_feature *)
10300 &feature->feature_data[feature->add_length];
10302 f3b: /* DVD+R Dual Layer */
10303 scsi_ulto2b(0x003B, feature->feature_code);
10304 feature->flags = 0x00;
10305 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10306 feature->flags |= SGC_F_CURRENT;
10307 feature->add_length = 4;
10308 feature->feature_data[0] = 0x00;
10309 feature = (struct scsi_get_config_feature *)
10310 &feature->feature_data[feature->add_length];
10313 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10314 if (rt == SGC_RT_SPECIFIC && data_len > 4) {
10315 feature = (struct scsi_get_config_feature *)(hdr + 1);
10316 if (scsi_2btoul(feature->feature_code) == starting)
10317 feature = (struct scsi_get_config_feature *)
10318 &feature->feature_data[feature->add_length];
10319 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10321 scsi_ulto4b(data_len - 4, hdr->data_length);
10322 ctsio->kern_data_len = min(data_len, alloc_len);
10323 ctsio->kern_total_len = ctsio->kern_data_len;
10325 ctl_set_success(ctsio);
10326 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10327 ctsio->be_move_done = ctl_config_move_done;
10328 ctl_datamove((union ctl_io *)ctsio);
10329 return (CTL_RETVAL_COMPLETE);
10333 ctl_get_event_status(struct ctl_scsiio *ctsio)
10335 struct scsi_get_event_status_header *hdr;
10336 struct scsi_get_event_status *cdb;
10337 uint32_t alloc_len, data_len;
10340 cdb = (struct scsi_get_event_status *)ctsio->cdb;
10341 if ((cdb->byte2 & SGESN_POLLED) == 0) {
10342 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
10343 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
10344 ctl_done((union ctl_io *)ctsio);
10345 return (CTL_RETVAL_COMPLETE);
10347 notif_class = cdb->notif_class;
10348 alloc_len = scsi_2btoul(cdb->length);
10350 data_len = sizeof(struct scsi_get_event_status_header);
10351 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10352 ctsio->kern_sg_entries = 0;
10353 ctsio->kern_rel_offset = 0;
10354 ctsio->kern_data_len = min(data_len, alloc_len);
10355 ctsio->kern_total_len = ctsio->kern_data_len;
10357 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr;
10358 scsi_ulto2b(0, hdr->descr_length);
10359 hdr->nea_class = SGESN_NEA;
10360 hdr->supported_class = 0;
10362 ctl_set_success(ctsio);
10363 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10364 ctsio->be_move_done = ctl_config_move_done;
10365 ctl_datamove((union ctl_io *)ctsio);
10366 return (CTL_RETVAL_COMPLETE);
10370 ctl_mechanism_status(struct ctl_scsiio *ctsio)
10372 struct scsi_mechanism_status_header *hdr;
10373 struct scsi_mechanism_status *cdb;
10374 uint32_t alloc_len, data_len;
10376 cdb = (struct scsi_mechanism_status *)ctsio->cdb;
10377 alloc_len = scsi_2btoul(cdb->length);
10379 data_len = sizeof(struct scsi_mechanism_status_header);
10380 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10381 ctsio->kern_sg_entries = 0;
10382 ctsio->kern_rel_offset = 0;
10383 ctsio->kern_data_len = min(data_len, alloc_len);
10384 ctsio->kern_total_len = ctsio->kern_data_len;
10386 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr;
10387 hdr->state1 = 0x00;
10388 hdr->state2 = 0xe0;
10389 scsi_ulto3b(0, hdr->lba);
10390 hdr->slots_num = 0;
10391 scsi_ulto2b(0, hdr->slots_length);
10393 ctl_set_success(ctsio);
10394 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10395 ctsio->be_move_done = ctl_config_move_done;
10396 ctl_datamove((union ctl_io *)ctsio);
10397 return (CTL_RETVAL_COMPLETE);
10401 ctl_ultomsf(uint32_t lba, uint8_t *buf)
10406 buf[1] = bin2bcd((lba / 75) / 60);
10407 buf[2] = bin2bcd((lba / 75) % 60);
10408 buf[3] = bin2bcd(lba % 75);
10412 ctl_read_toc(struct ctl_scsiio *ctsio)
10414 struct ctl_lun *lun = CTL_LUN(ctsio);
10415 struct scsi_read_toc_hdr *hdr;
10416 struct scsi_read_toc_type01_descr *descr;
10417 struct scsi_read_toc *cdb;
10418 uint32_t alloc_len, data_len;
10421 cdb = (struct scsi_read_toc *)ctsio->cdb;
10422 msf = (cdb->byte2 & CD_MSF) != 0;
10423 format = cdb->format;
10424 alloc_len = scsi_2btoul(cdb->data_len);
10426 data_len = sizeof(struct scsi_read_toc_hdr);
10428 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr);
10430 data_len += sizeof(struct scsi_read_toc_type01_descr);
10431 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10432 ctsio->kern_sg_entries = 0;
10433 ctsio->kern_rel_offset = 0;
10434 ctsio->kern_data_len = min(data_len, alloc_len);
10435 ctsio->kern_total_len = ctsio->kern_data_len;
10437 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr;
10439 scsi_ulto2b(0x12, hdr->data_length);
10442 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10443 descr->addr_ctl = 0x14;
10444 descr->track_number = 1;
10446 ctl_ultomsf(0, descr->track_start);
10448 scsi_ulto4b(0, descr->track_start);
10450 descr->addr_ctl = 0x14;
10451 descr->track_number = 0xaa;
10453 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start);
10455 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start);
10457 scsi_ulto2b(0x0a, hdr->data_length);
10460 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10461 descr->addr_ctl = 0x14;
10462 descr->track_number = 1;
10464 ctl_ultomsf(0, descr->track_start);
10466 scsi_ulto4b(0, descr->track_start);
10469 ctl_set_success(ctsio);
10470 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10471 ctsio->be_move_done = ctl_config_move_done;
10472 ctl_datamove((union ctl_io *)ctsio);
10473 return (CTL_RETVAL_COMPLETE);
10477 * For known CDB types, parse the LBA and length.
10480 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10482 if (io->io_hdr.io_type != CTL_IO_SCSI)
10485 switch (io->scsiio.cdb[0]) {
10486 case COMPARE_AND_WRITE: {
10487 struct scsi_compare_and_write *cdb;
10489 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10491 *lba = scsi_8btou64(cdb->addr);
10492 *len = cdb->length;
10497 struct scsi_rw_6 *cdb;
10499 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10501 *lba = scsi_3btoul(cdb->addr);
10502 /* only 5 bits are valid in the most significant address byte */
10504 *len = cdb->length;
10509 struct scsi_rw_10 *cdb;
10511 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10513 *lba = scsi_4btoul(cdb->addr);
10514 *len = scsi_2btoul(cdb->length);
10517 case WRITE_VERIFY_10: {
10518 struct scsi_write_verify_10 *cdb;
10520 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10522 *lba = scsi_4btoul(cdb->addr);
10523 *len = scsi_2btoul(cdb->length);
10528 struct scsi_rw_12 *cdb;
10530 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10532 *lba = scsi_4btoul(cdb->addr);
10533 *len = scsi_4btoul(cdb->length);
10536 case WRITE_VERIFY_12: {
10537 struct scsi_write_verify_12 *cdb;
10539 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10541 *lba = scsi_4btoul(cdb->addr);
10542 *len = scsi_4btoul(cdb->length);
10547 struct scsi_rw_16 *cdb;
10549 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10551 *lba = scsi_8btou64(cdb->addr);
10552 *len = scsi_4btoul(cdb->length);
10555 case WRITE_ATOMIC_16: {
10556 struct scsi_write_atomic_16 *cdb;
10558 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb;
10560 *lba = scsi_8btou64(cdb->addr);
10561 *len = scsi_2btoul(cdb->length);
10564 case WRITE_VERIFY_16: {
10565 struct scsi_write_verify_16 *cdb;
10567 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10569 *lba = scsi_8btou64(cdb->addr);
10570 *len = scsi_4btoul(cdb->length);
10573 case WRITE_SAME_10: {
10574 struct scsi_write_same_10 *cdb;
10576 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10578 *lba = scsi_4btoul(cdb->addr);
10579 *len = scsi_2btoul(cdb->length);
10582 case WRITE_SAME_16: {
10583 struct scsi_write_same_16 *cdb;
10585 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10587 *lba = scsi_8btou64(cdb->addr);
10588 *len = scsi_4btoul(cdb->length);
10592 struct scsi_verify_10 *cdb;
10594 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10596 *lba = scsi_4btoul(cdb->addr);
10597 *len = scsi_2btoul(cdb->length);
10601 struct scsi_verify_12 *cdb;
10603 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10605 *lba = scsi_4btoul(cdb->addr);
10606 *len = scsi_4btoul(cdb->length);
10610 struct scsi_verify_16 *cdb;
10612 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10614 *lba = scsi_8btou64(cdb->addr);
10615 *len = scsi_4btoul(cdb->length);
10623 case SERVICE_ACTION_IN: { /* GET LBA STATUS */
10624 struct scsi_get_lba_status *cdb;
10626 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10627 *lba = scsi_8btou64(cdb->addr);
10633 break; /* NOTREACHED */
10640 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10643 uint64_t endlba1, endlba2;
10645 endlba1 = lba1 + len1 - (seq ? 0 : 1);
10646 endlba2 = lba2 + len2 - 1;
10648 if ((endlba1 < lba2) || (endlba2 < lba1))
10649 return (CTL_ACTION_PASS);
10651 return (CTL_ACTION_BLOCK);
10655 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10657 struct ctl_ptr_len_flags *ptrlen;
10658 struct scsi_unmap_desc *buf, *end, *range;
10662 /* If not UNMAP -- go other way. */
10663 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10664 io->scsiio.cdb[0] != UNMAP)
10665 return (CTL_ACTION_ERROR);
10667 /* If UNMAP without data -- block and wait for data. */
10668 ptrlen = (struct ctl_ptr_len_flags *)
10669 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10670 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10671 ptrlen->ptr == NULL)
10672 return (CTL_ACTION_BLOCK);
10674 /* UNMAP with data -- check for collision. */
10675 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10676 end = buf + ptrlen->len / sizeof(*buf);
10677 for (range = buf; range < end; range++) {
10678 lba = scsi_8btou64(range->lba);
10679 len = scsi_4btoul(range->length);
10680 if ((lba < lba2 + len2) && (lba + len > lba2))
10681 return (CTL_ACTION_BLOCK);
10683 return (CTL_ACTION_PASS);
10687 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10689 uint64_t lba1, lba2;
10690 uint64_t len1, len2;
10693 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10694 return (CTL_ACTION_ERROR);
10696 retval = ctl_extent_check_unmap(io1, lba2, len2);
10697 if (retval != CTL_ACTION_ERROR)
10700 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10701 return (CTL_ACTION_ERROR);
10703 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10705 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
10709 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2)
10711 uint64_t lba1, lba2;
10712 uint64_t len1, len2;
10714 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10715 return (CTL_ACTION_PASS);
10716 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10717 return (CTL_ACTION_ERROR);
10718 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10719 return (CTL_ACTION_ERROR);
10721 if (lba1 + len1 == lba2)
10722 return (CTL_ACTION_BLOCK);
10723 return (CTL_ACTION_PASS);
10727 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10728 union ctl_io *ooa_io)
10730 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10731 const ctl_serialize_action *serialize_row;
10734 * The initiator attempted multiple untagged commands at the same
10735 * time. Can't do that.
10737 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10738 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10739 && ((pending_io->io_hdr.nexus.targ_port ==
10740 ooa_io->io_hdr.nexus.targ_port)
10741 && (pending_io->io_hdr.nexus.initid ==
10742 ooa_io->io_hdr.nexus.initid))
10743 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10744 CTL_FLAG_STATUS_SENT)) == 0))
10745 return (CTL_ACTION_OVERLAP);
10748 * The initiator attempted to send multiple tagged commands with
10749 * the same ID. (It's fine if different initiators have the same
10752 * Even if all of those conditions are true, we don't kill the I/O
10753 * if the command ahead of us has been aborted. We won't end up
10754 * sending it to the FETD, and it's perfectly legal to resend a
10755 * command with the same tag number as long as the previous
10756 * instance of this tag number has been aborted somehow.
10758 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10759 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10760 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10761 && ((pending_io->io_hdr.nexus.targ_port ==
10762 ooa_io->io_hdr.nexus.targ_port)
10763 && (pending_io->io_hdr.nexus.initid ==
10764 ooa_io->io_hdr.nexus.initid))
10765 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10766 CTL_FLAG_STATUS_SENT)) == 0))
10767 return (CTL_ACTION_OVERLAP_TAG);
10770 * If we get a head of queue tag, SAM-3 says that we should
10771 * immediately execute it.
10773 * What happens if this command would normally block for some other
10774 * reason? e.g. a request sense with a head of queue tag
10775 * immediately after a write. Normally that would block, but this
10776 * will result in its getting executed immediately...
10778 * We currently return "pass" instead of "skip", so we'll end up
10779 * going through the rest of the queue to check for overlapped tags.
10781 * XXX KDM check for other types of blockage first??
10783 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10784 return (CTL_ACTION_PASS);
10787 * Ordered tags have to block until all items ahead of them
10788 * have completed. If we get called with an ordered tag, we always
10789 * block, if something else is ahead of us in the queue.
10791 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10792 return (CTL_ACTION_BLOCK);
10795 * Simple tags get blocked until all head of queue and ordered tags
10796 * ahead of them have completed. I'm lumping untagged commands in
10797 * with simple tags here. XXX KDM is that the right thing to do?
10799 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10800 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10801 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10802 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10803 return (CTL_ACTION_BLOCK);
10805 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
10806 KASSERT(pending_entry->seridx < CTL_SERIDX_COUNT,
10807 ("%s: Invalid seridx %d for pending CDB %02x %02x @ %p",
10808 __func__, pending_entry->seridx, pending_io->scsiio.cdb[0],
10809 pending_io->scsiio.cdb[1], pending_io));
10810 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
10811 if (ooa_entry->seridx == CTL_SERIDX_INVLD)
10812 return (CTL_ACTION_PASS); /* Unsupported command in OOA queue */
10813 KASSERT(ooa_entry->seridx < CTL_SERIDX_COUNT,
10814 ("%s: Invalid seridx %d for ooa CDB %02x %02x @ %p",
10815 __func__, ooa_entry->seridx, ooa_io->scsiio.cdb[0],
10816 ooa_io->scsiio.cdb[1], ooa_io));
10818 serialize_row = ctl_serialize_table[ooa_entry->seridx];
10820 switch (serialize_row[pending_entry->seridx]) {
10821 case CTL_SER_BLOCK:
10822 return (CTL_ACTION_BLOCK);
10823 case CTL_SER_EXTENT:
10824 return (ctl_extent_check(ooa_io, pending_io,
10825 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10826 case CTL_SER_EXTENTOPT:
10827 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
10828 SCP_QUEUE_ALG_UNRESTRICTED)
10829 return (ctl_extent_check(ooa_io, pending_io,
10831 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10832 return (CTL_ACTION_PASS);
10833 case CTL_SER_EXTENTSEQ:
10834 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF)
10835 return (ctl_extent_check_seq(ooa_io, pending_io));
10836 return (CTL_ACTION_PASS);
10838 return (CTL_ACTION_PASS);
10839 case CTL_SER_BLOCKOPT:
10840 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
10841 SCP_QUEUE_ALG_UNRESTRICTED)
10842 return (CTL_ACTION_BLOCK);
10843 return (CTL_ACTION_PASS);
10845 return (CTL_ACTION_SKIP);
10847 panic("%s: Invalid serialization value %d for %d => %d",
10848 __func__, serialize_row[pending_entry->seridx],
10849 pending_entry->seridx, ooa_entry->seridx);
10852 return (CTL_ACTION_ERROR);
10856 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
10858 * - pending_io is generally either incoming, or on the blocked queue
10859 * - starting I/O is the I/O we want to start the check with.
10862 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
10863 union ctl_io *starting_io)
10865 union ctl_io *ooa_io;
10868 mtx_assert(&lun->lun_lock, MA_OWNED);
10871 * Run back along the OOA queue, starting with the current
10872 * blocked I/O and going through every I/O before it on the
10873 * queue. If starting_io is NULL, we'll just end up returning
10876 for (ooa_io = starting_io; ooa_io != NULL;
10877 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
10881 * This routine just checks to see whether
10882 * cur_blocked is blocked by ooa_io, which is ahead
10883 * of it in the queue. It doesn't queue/dequeue
10886 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
10888 case CTL_ACTION_BLOCK:
10889 case CTL_ACTION_OVERLAP:
10890 case CTL_ACTION_OVERLAP_TAG:
10891 case CTL_ACTION_SKIP:
10892 case CTL_ACTION_ERROR:
10894 break; /* NOTREACHED */
10895 case CTL_ACTION_PASS:
10898 panic("%s: Invalid action %d\n", __func__, action);
10902 return (CTL_ACTION_PASS);
10907 * - An I/O has just completed, and has been removed from the per-LUN OOA
10908 * queue, so some items on the blocked queue may now be unblocked.
10911 ctl_check_blocked(struct ctl_lun *lun)
10913 struct ctl_softc *softc = lun->ctl_softc;
10914 union ctl_io *cur_blocked, *next_blocked;
10916 mtx_assert(&lun->lun_lock, MA_OWNED);
10919 * Run forward from the head of the blocked queue, checking each
10920 * entry against the I/Os prior to it on the OOA queue to see if
10921 * there is still any blockage.
10923 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
10924 * with our removing a variable on it while it is traversing the
10927 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
10928 cur_blocked != NULL; cur_blocked = next_blocked) {
10929 union ctl_io *prev_ooa;
10932 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
10935 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
10936 ctl_ooaq, ooa_links);
10939 * If cur_blocked happens to be the first item in the OOA
10940 * queue now, prev_ooa will be NULL, and the action
10941 * returned will just be CTL_ACTION_PASS.
10943 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
10946 case CTL_ACTION_BLOCK:
10947 /* Nothing to do here, still blocked */
10949 case CTL_ACTION_OVERLAP:
10950 case CTL_ACTION_OVERLAP_TAG:
10952 * This shouldn't happen! In theory we've already
10953 * checked this command for overlap...
10956 case CTL_ACTION_PASS:
10957 case CTL_ACTION_SKIP: {
10958 const struct ctl_cmd_entry *entry;
10961 * The skip case shouldn't happen, this transaction
10962 * should have never made it onto the blocked queue.
10965 * This I/O is no longer blocked, we can remove it
10966 * from the blocked queue. Since this is a TAILQ
10967 * (doubly linked list), we can do O(1) removals
10968 * from any place on the list.
10970 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
10972 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
10974 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
10975 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){
10977 * Need to send IO back to original side to
10980 union ctl_ha_msg msg_info;
10982 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
10983 msg_info.hdr.original_sc =
10984 cur_blocked->io_hdr.original_sc;
10985 msg_info.hdr.serializing_sc = cur_blocked;
10986 msg_info.hdr.msg_type = CTL_MSG_R2R;
10987 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
10988 sizeof(msg_info.hdr), M_NOWAIT);
10991 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
10994 * Check this I/O for LUN state changes that may
10995 * have happened while this command was blocked.
10996 * The LUN state may have been changed by a command
10997 * ahead of us in the queue, so we need to re-check
10998 * for any states that can be caused by SCSI
11001 if (ctl_scsiio_lun_check(lun, entry,
11002 &cur_blocked->scsiio) == 0) {
11003 cur_blocked->io_hdr.flags |=
11004 CTL_FLAG_IS_WAS_ON_RTR;
11005 ctl_enqueue_rtr(cur_blocked);
11007 ctl_done(cur_blocked);
11012 * This probably shouldn't happen -- we shouldn't
11013 * get CTL_ACTION_ERROR, or anything else.
11019 return (CTL_RETVAL_COMPLETE);
11023 * This routine (with one exception) checks LUN flags that can be set by
11024 * commands ahead of us in the OOA queue. These flags have to be checked
11025 * when a command initially comes in, and when we pull a command off the
11026 * blocked queue and are preparing to execute it. The reason we have to
11027 * check these flags for commands on the blocked queue is that the LUN
11028 * state may have been changed by a command ahead of us while we're on the
11031 * Ordering is somewhat important with these checks, so please pay
11032 * careful attention to the placement of any new checks.
11035 ctl_scsiio_lun_check(struct ctl_lun *lun,
11036 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11038 struct ctl_softc *softc = lun->ctl_softc;
11044 mtx_assert(&lun->lun_lock, MA_OWNED);
11047 * If this shelf is a secondary shelf controller, we may have to
11048 * reject some commands disallowed by HA mode and link state.
11050 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11051 if (softc->ha_link == CTL_HA_LINK_OFFLINE &&
11052 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11053 ctl_set_lun_unavail(ctsio);
11057 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 &&
11058 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11059 ctl_set_lun_transit(ctsio);
11063 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY &&
11064 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) {
11065 ctl_set_lun_standby(ctsio);
11070 /* The rest of checks are only done on executing side */
11071 if (softc->ha_mode == CTL_HA_MODE_XFER)
11075 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11077 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) {
11078 ctl_set_hw_write_protected(ctsio);
11082 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) {
11083 ctl_set_sense(ctsio, /*current_error*/ 1,
11084 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11085 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11092 * Check for a reservation conflict. If this command isn't allowed
11093 * even on reserved LUNs, and if this initiator isn't the one who
11094 * reserved us, reject the command with a reservation conflict.
11096 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11097 if ((lun->flags & CTL_LUN_RESERVED)
11098 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11099 if (lun->res_idx != residx) {
11100 ctl_set_reservation_conflict(ctsio);
11106 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11107 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11108 /* No reservation or command is allowed. */;
11109 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11110 (lun->pr_res_type == SPR_TYPE_WR_EX ||
11111 lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
11112 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) {
11113 /* The command is allowed for Write Exclusive resv. */;
11116 * if we aren't registered or it's a res holder type
11117 * reservation and this isn't the res holder then set a
11120 if (ctl_get_prkey(lun, residx) == 0 ||
11121 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) {
11122 ctl_set_reservation_conflict(ctsio);
11128 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) {
11129 if (lun->flags & CTL_LUN_EJECTED)
11130 ctl_set_lun_ejected(ctsio);
11131 else if (lun->flags & CTL_LUN_NO_MEDIA) {
11132 if (lun->flags & CTL_LUN_REMOVABLE)
11133 ctl_set_lun_no_media(ctsio);
11135 ctl_set_lun_int_reqd(ctsio);
11136 } else if (lun->flags & CTL_LUN_STOPPED)
11137 ctl_set_lun_stopped(ctsio);
11149 ctl_failover_io(union ctl_io *io, int have_lock)
11151 ctl_set_busy(&io->scsiio);
11156 ctl_failover_lun(union ctl_io *rio)
11158 struct ctl_softc *softc = CTL_SOFTC(rio);
11159 struct ctl_lun *lun;
11160 struct ctl_io_hdr *io, *next_io;
11163 targ_lun = rio->io_hdr.nexus.targ_mapped_lun;
11164 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun));
11166 /* Find and lock the LUN. */
11167 mtx_lock(&softc->ctl_lock);
11168 if (targ_lun > CTL_MAX_LUNS ||
11169 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11170 mtx_unlock(&softc->ctl_lock);
11173 mtx_lock(&lun->lun_lock);
11174 mtx_unlock(&softc->ctl_lock);
11175 if (lun->flags & CTL_LUN_DISABLED) {
11176 mtx_unlock(&lun->lun_lock);
11180 if (softc->ha_mode == CTL_HA_MODE_XFER) {
11181 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11182 /* We are master */
11183 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11184 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11185 io->flags |= CTL_FLAG_ABORT;
11186 io->flags |= CTL_FLAG_FAILOVER;
11187 } else { /* This can be only due to DATAMOVE */
11188 io->msg_type = CTL_MSG_DATAMOVE_DONE;
11189 io->flags &= ~CTL_FLAG_DMA_INPROG;
11190 io->flags |= CTL_FLAG_IO_ACTIVE;
11191 io->port_status = 31340;
11192 ctl_enqueue_isc((union ctl_io *)io);
11196 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11197 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11198 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11199 io->flags |= CTL_FLAG_FAILOVER;
11201 ctl_set_busy(&((union ctl_io *)io)->
11203 ctl_done((union ctl_io *)io);
11207 } else { /* SERIALIZE modes */
11208 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links,
11210 /* We are master */
11211 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11212 TAILQ_REMOVE(&lun->blocked_queue, io,
11214 io->flags &= ~CTL_FLAG_BLOCKED;
11215 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11216 ctl_free_io((union ctl_io *)io);
11219 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11220 /* We are master */
11221 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11222 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11223 ctl_free_io((union ctl_io *)io);
11226 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11227 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11228 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) {
11229 ctl_set_busy(&((union ctl_io *)io)->
11231 ctl_done((union ctl_io *)io);
11235 ctl_check_blocked(lun);
11237 mtx_unlock(&lun->lun_lock);
11241 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio)
11243 struct ctl_lun *lun;
11244 const struct ctl_cmd_entry *entry;
11245 uint32_t initidx, targ_lun;
11249 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11250 if (targ_lun < CTL_MAX_LUNS)
11251 lun = softc->ctl_luns[targ_lun];
11254 * If the LUN is invalid, pretend that it doesn't exist.
11255 * It will go away as soon as all pending I/O has been
11258 mtx_lock(&lun->lun_lock);
11259 if (lun->flags & CTL_LUN_DISABLED) {
11260 mtx_unlock(&lun->lun_lock);
11264 CTL_LUN(ctsio) = lun;
11266 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
11269 * Every I/O goes into the OOA queue for a particular LUN,
11270 * and stays there until completion.
11273 if (TAILQ_EMPTY(&lun->ooa_queue))
11274 lun->idle_time += getsbinuptime() - lun->last_busy;
11276 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
11279 /* Get command entry and return error if it is unsuppotyed. */
11280 entry = ctl_validate_command(ctsio);
11281 if (entry == NULL) {
11283 mtx_unlock(&lun->lun_lock);
11287 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11288 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11291 * Check to see whether we can send this command to LUNs that don't
11292 * exist. This should pretty much only be the case for inquiry
11293 * and request sense. Further checks, below, really require having
11294 * a LUN, so we can't really check the command anymore. Just put
11295 * it on the rtr queue.
11298 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) {
11299 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11300 ctl_enqueue_rtr((union ctl_io *)ctsio);
11304 ctl_set_unsupported_lun(ctsio);
11305 ctl_done((union ctl_io *)ctsio);
11306 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11310 * Make sure we support this particular command on this LUN.
11311 * e.g., we don't support writes to the control LUN.
11313 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11314 mtx_unlock(&lun->lun_lock);
11315 ctl_set_invalid_opcode(ctsio);
11316 ctl_done((union ctl_io *)ctsio);
11321 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11325 * If we've got a request sense, it'll clear the contingent
11326 * allegiance condition. Otherwise, if we have a CA condition for
11327 * this initiator, clear it, because it sent down a command other
11328 * than request sense.
11330 if ((ctsio->cdb[0] != REQUEST_SENSE)
11331 && (ctl_is_set(lun->have_ca, initidx)))
11332 ctl_clear_mask(lun->have_ca, initidx);
11336 * If the command has this flag set, it handles its own unit
11337 * attention reporting, we shouldn't do anything. Otherwise we
11338 * check for any pending unit attentions, and send them back to the
11339 * initiator. We only do this when a command initially comes in,
11340 * not when we pull it off the blocked queue.
11342 * According to SAM-3, section 5.3.2, the order that things get
11343 * presented back to the host is basically unit attentions caused
11344 * by some sort of reset event, busy status, reservation conflicts
11345 * or task set full, and finally any other status.
11347 * One issue here is that some of the unit attentions we report
11348 * don't fall into the "reset" category (e.g. "reported luns data
11349 * has changed"). So reporting it here, before the reservation
11350 * check, may be technically wrong. I guess the only thing to do
11351 * would be to check for and report the reset events here, and then
11352 * check for the other unit attention types after we check for a
11353 * reservation conflict.
11355 * XXX KDM need to fix this
11357 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11358 ctl_ua_type ua_type;
11359 u_int sense_len = 0;
11361 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11362 &sense_len, SSD_TYPE_NONE);
11363 if (ua_type != CTL_UA_NONE) {
11364 mtx_unlock(&lun->lun_lock);
11365 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11366 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11367 ctsio->sense_len = sense_len;
11368 ctl_done((union ctl_io *)ctsio);
11374 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11375 mtx_unlock(&lun->lun_lock);
11376 ctl_done((union ctl_io *)ctsio);
11381 * XXX CHD this is where we want to send IO to other side if
11382 * this LUN is secondary on this SC. We will need to make a copy
11383 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11384 * the copy we send as FROM_OTHER.
11385 * We also need to stuff the address of the original IO so we can
11386 * find it easily. Something similar will need be done on the other
11387 * side so when we are done we can find the copy.
11389 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
11390 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 &&
11391 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) {
11392 union ctl_ha_msg msg_info;
11395 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11396 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11397 mtx_unlock(&lun->lun_lock);
11399 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11400 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11401 msg_info.hdr.serializing_sc = NULL;
11402 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11403 msg_info.scsi.tag_num = ctsio->tag_num;
11404 msg_info.scsi.tag_type = ctsio->tag_type;
11405 msg_info.scsi.cdb_len = ctsio->cdb_len;
11406 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11408 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11409 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data),
11410 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) {
11411 ctl_set_busy(ctsio);
11412 ctl_done((union ctl_io *)ctsio);
11418 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11419 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11420 ctl_ooaq, ooa_links))) {
11421 case CTL_ACTION_BLOCK:
11422 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11423 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11425 mtx_unlock(&lun->lun_lock);
11427 case CTL_ACTION_PASS:
11428 case CTL_ACTION_SKIP:
11429 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11430 mtx_unlock(&lun->lun_lock);
11431 ctl_enqueue_rtr((union ctl_io *)ctsio);
11433 case CTL_ACTION_OVERLAP:
11434 mtx_unlock(&lun->lun_lock);
11435 ctl_set_overlapped_cmd(ctsio);
11436 ctl_done((union ctl_io *)ctsio);
11438 case CTL_ACTION_OVERLAP_TAG:
11439 mtx_unlock(&lun->lun_lock);
11440 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11441 ctl_done((union ctl_io *)ctsio);
11443 case CTL_ACTION_ERROR:
11445 mtx_unlock(&lun->lun_lock);
11446 ctl_set_internal_failure(ctsio,
11448 /*retry_count*/ 0);
11449 ctl_done((union ctl_io *)ctsio);
11455 const struct ctl_cmd_entry *
11456 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11458 const struct ctl_cmd_entry *entry;
11459 int service_action;
11461 entry = &ctl_cmd_table[ctsio->cdb[0]];
11463 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11464 if (entry->flags & CTL_CMD_FLAG_SA5) {
11465 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11466 entry = &((const struct ctl_cmd_entry *)
11467 entry->execute)[service_action];
11472 const struct ctl_cmd_entry *
11473 ctl_validate_command(struct ctl_scsiio *ctsio)
11475 const struct ctl_cmd_entry *entry;
11479 entry = ctl_get_cmd_entry(ctsio, &sa);
11480 if (entry->execute == NULL) {
11482 ctl_set_invalid_field(ctsio,
11489 ctl_set_invalid_opcode(ctsio);
11490 ctl_done((union ctl_io *)ctsio);
11493 KASSERT(entry->length > 0,
11494 ("Not defined length for command 0x%02x/0x%02x",
11495 ctsio->cdb[0], ctsio->cdb[1]));
11496 for (i = 1; i < entry->length; i++) {
11497 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11500 ctl_set_invalid_field(ctsio,
11505 /*bit*/ fls(diff) - 1);
11506 ctl_done((union ctl_io *)ctsio);
11513 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11516 switch (lun_type) {
11518 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0)
11522 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
11526 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0)
11536 ctl_scsiio(struct ctl_scsiio *ctsio)
11539 const struct ctl_cmd_entry *entry;
11541 retval = CTL_RETVAL_COMPLETE;
11543 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11545 entry = ctl_get_cmd_entry(ctsio, NULL);
11548 * If this I/O has been aborted, just send it straight to
11549 * ctl_done() without executing it.
11551 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11552 ctl_done((union ctl_io *)ctsio);
11557 * All the checks should have been handled by ctl_scsiio_precheck().
11558 * We should be clear now to just execute the I/O.
11560 retval = entry->execute(ctsio);
11567 * Since we only implement one target right now, a bus reset simply resets
11568 * our single target.
11571 ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io)
11573 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET));
11577 ctl_target_reset(struct ctl_softc *softc, union ctl_io *io,
11578 ctl_ua_type ua_type)
11580 struct ctl_port *port = CTL_PORT(io);
11581 struct ctl_lun *lun;
11584 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11585 union ctl_ha_msg msg_info;
11587 msg_info.hdr.nexus = io->io_hdr.nexus;
11588 if (ua_type==CTL_UA_TARG_RESET)
11589 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11591 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11592 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11593 msg_info.hdr.original_sc = NULL;
11594 msg_info.hdr.serializing_sc = NULL;
11595 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11596 sizeof(msg_info.task), M_WAITOK);
11600 mtx_lock(&softc->ctl_lock);
11601 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11602 if (port != NULL &&
11603 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
11605 retval += ctl_do_lun_reset(lun, io, ua_type);
11607 mtx_unlock(&softc->ctl_lock);
11608 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11613 * The LUN should always be set. The I/O is optional, and is used to
11614 * distinguish between I/Os sent by this initiator, and by other
11615 * initiators. We set unit attention for initiators other than this one.
11616 * SAM-3 is vague on this point. It does say that a unit attention should
11617 * be established for other initiators when a LUN is reset (see section
11618 * 5.7.3), but it doesn't specifically say that the unit attention should
11619 * be established for this particular initiator when a LUN is reset. Here
11620 * is the relevant text, from SAM-3 rev 8:
11622 * 5.7.2 When a SCSI initiator port aborts its own tasks
11624 * When a SCSI initiator port causes its own task(s) to be aborted, no
11625 * notification that the task(s) have been aborted shall be returned to
11626 * the SCSI initiator port other than the completion response for the
11627 * command or task management function action that caused the task(s) to
11628 * be aborted and notification(s) associated with related effects of the
11629 * action (e.g., a reset unit attention condition).
11631 * XXX KDM for now, we're setting unit attention for all initiators.
11634 ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11642 mtx_lock(&lun->lun_lock);
11644 * Run through the OOA queue and abort each I/O.
11646 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11647 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11648 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11652 * This version sets unit attention for every
11655 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11656 ctl_est_ua_all(lun, initidx, ua_type);
11658 ctl_est_ua_all(lun, -1, ua_type);
11662 * A reset (any kind, really) clears reservations established with
11663 * RESERVE/RELEASE. It does not clear reservations established
11664 * with PERSISTENT RESERVE OUT, but we don't support that at the
11665 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
11666 * reservations made with the RESERVE/RELEASE commands, because
11667 * those commands are obsolete in SPC-3.
11669 lun->flags &= ~CTL_LUN_RESERVED;
11672 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11673 ctl_clear_mask(lun->have_ca, i);
11675 lun->prevent_count = 0;
11676 if (lun->prevent) {
11677 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11678 ctl_clear_mask(lun->prevent, i);
11680 mtx_unlock(&lun->lun_lock);
11686 ctl_lun_reset(struct ctl_softc *softc, union ctl_io *io)
11688 struct ctl_lun *lun;
11692 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11693 mtx_lock(&softc->ctl_lock);
11694 if (targ_lun >= CTL_MAX_LUNS ||
11695 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11696 mtx_unlock(&softc->ctl_lock);
11697 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11700 retval = ctl_do_lun_reset(lun, io, CTL_UA_LUN_RESET);
11701 mtx_unlock(&softc->ctl_lock);
11702 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11704 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) {
11705 union ctl_ha_msg msg_info;
11707 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11708 msg_info.hdr.nexus = io->io_hdr.nexus;
11709 msg_info.task.task_action = CTL_TASK_LUN_RESET;
11710 msg_info.hdr.original_sc = NULL;
11711 msg_info.hdr.serializing_sc = NULL;
11712 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11713 sizeof(msg_info.task), M_WAITOK);
11719 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11724 mtx_assert(&lun->lun_lock, MA_OWNED);
11727 * Run through the OOA queue and attempt to find the given I/O.
11728 * The target port, initiator ID, tag type and tag number have to
11729 * match the values that we got from the initiator. If we have an
11730 * untagged command to abort, simply abort the first untagged command
11731 * we come to. We only allow one untagged command at a time of course.
11733 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11734 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11736 if ((targ_port == UINT32_MAX ||
11737 targ_port == xio->io_hdr.nexus.targ_port) &&
11738 (init_id == UINT32_MAX ||
11739 init_id == xio->io_hdr.nexus.initid)) {
11740 if (targ_port != xio->io_hdr.nexus.targ_port ||
11741 init_id != xio->io_hdr.nexus.initid)
11742 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
11743 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11744 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11745 union ctl_ha_msg msg_info;
11747 msg_info.hdr.nexus = xio->io_hdr.nexus;
11748 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11749 msg_info.task.tag_num = xio->scsiio.tag_num;
11750 msg_info.task.tag_type = xio->scsiio.tag_type;
11751 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11752 msg_info.hdr.original_sc = NULL;
11753 msg_info.hdr.serializing_sc = NULL;
11754 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11755 sizeof(msg_info.task), M_NOWAIT);
11762 ctl_abort_task_set(union ctl_io *io)
11764 struct ctl_softc *softc = CTL_SOFTC(io);
11765 struct ctl_lun *lun;
11771 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11772 mtx_lock(&softc->ctl_lock);
11773 if (targ_lun >= CTL_MAX_LUNS ||
11774 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11775 mtx_unlock(&softc->ctl_lock);
11776 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11780 mtx_lock(&lun->lun_lock);
11781 mtx_unlock(&softc->ctl_lock);
11782 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
11783 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11784 io->io_hdr.nexus.initid,
11785 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11786 } else { /* CTL_TASK_CLEAR_TASK_SET */
11787 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
11788 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11790 mtx_unlock(&lun->lun_lock);
11791 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11796 ctl_i_t_nexus_reset(union ctl_io *io)
11798 struct ctl_softc *softc = CTL_SOFTC(io);
11799 struct ctl_lun *lun;
11802 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11803 union ctl_ha_msg msg_info;
11805 msg_info.hdr.nexus = io->io_hdr.nexus;
11806 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET;
11807 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11808 msg_info.hdr.original_sc = NULL;
11809 msg_info.hdr.serializing_sc = NULL;
11810 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11811 sizeof(msg_info.task), M_WAITOK);
11814 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11815 mtx_lock(&softc->ctl_lock);
11816 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11817 mtx_lock(&lun->lun_lock);
11818 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11819 io->io_hdr.nexus.initid, 1);
11821 ctl_clear_mask(lun->have_ca, initidx);
11823 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx))
11824 lun->flags &= ~CTL_LUN_RESERVED;
11825 if (lun->prevent && ctl_is_set(lun->prevent, initidx)) {
11826 ctl_clear_mask(lun->prevent, initidx);
11827 lun->prevent_count--;
11829 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS);
11830 mtx_unlock(&lun->lun_lock);
11832 mtx_unlock(&softc->ctl_lock);
11833 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11838 ctl_abort_task(union ctl_io *io)
11840 struct ctl_softc *softc = CTL_SOFTC(io);
11842 struct ctl_lun *lun;
11845 char printbuf[128];
11855 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11856 mtx_lock(&softc->ctl_lock);
11857 if (targ_lun >= CTL_MAX_LUNS ||
11858 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11859 mtx_unlock(&softc->ctl_lock);
11860 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11865 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
11866 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
11869 mtx_lock(&lun->lun_lock);
11870 mtx_unlock(&softc->ctl_lock);
11872 * Run through the OOA queue and attempt to find the given I/O.
11873 * The target port, initiator ID, tag type and tag number have to
11874 * match the values that we got from the initiator. If we have an
11875 * untagged command to abort, simply abort the first untagged command
11876 * we come to. We only allow one untagged command at a time of course.
11878 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11879 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11881 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
11883 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
11884 lun->lun, xio->scsiio.tag_num,
11885 xio->scsiio.tag_type,
11886 (xio->io_hdr.blocked_links.tqe_prev
11887 == NULL) ? "" : " BLOCKED",
11888 (xio->io_hdr.flags &
11889 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
11890 (xio->io_hdr.flags &
11891 CTL_FLAG_ABORT) ? " ABORT" : "",
11892 (xio->io_hdr.flags &
11893 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
11894 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
11896 printf("%s\n", sbuf_data(&sb));
11899 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
11900 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
11901 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
11905 * If the abort says that the task is untagged, the
11906 * task in the queue must be untagged. Otherwise,
11907 * we just check to see whether the tag numbers
11908 * match. This is because the QLogic firmware
11909 * doesn't pass back the tag type in an abort
11913 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
11914 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
11915 || (xio->scsiio.tag_num == io->taskio.tag_num))
11918 * XXX KDM we've got problems with FC, because it
11919 * doesn't send down a tag type with aborts. So we
11920 * can only really go by the tag number...
11921 * This may cause problems with parallel SCSI.
11922 * Need to figure that out!!
11924 if (xio->scsiio.tag_num == io->taskio.tag_num) {
11925 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11927 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
11928 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11929 union ctl_ha_msg msg_info;
11931 msg_info.hdr.nexus = io->io_hdr.nexus;
11932 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11933 msg_info.task.tag_num = io->taskio.tag_num;
11934 msg_info.task.tag_type = io->taskio.tag_type;
11935 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11936 msg_info.hdr.original_sc = NULL;
11937 msg_info.hdr.serializing_sc = NULL;
11939 printf("Sent Abort to other side\n");
11941 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11942 sizeof(msg_info.task), M_NOWAIT);
11945 printf("ctl_abort_task: found I/O to abort\n");
11949 mtx_unlock(&lun->lun_lock);
11953 * This isn't really an error. It's entirely possible for
11954 * the abort and command completion to cross on the wire.
11955 * This is more of an informative/diagnostic error.
11958 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
11959 "%u:%u:%u tag %d type %d\n",
11960 io->io_hdr.nexus.initid,
11961 io->io_hdr.nexus.targ_port,
11962 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
11963 io->taskio.tag_type);
11966 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11971 ctl_query_task(union ctl_io *io, int task_set)
11973 struct ctl_softc *softc = CTL_SOFTC(io);
11975 struct ctl_lun *lun;
11979 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11980 mtx_lock(&softc->ctl_lock);
11981 if (targ_lun >= CTL_MAX_LUNS ||
11982 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11983 mtx_unlock(&softc->ctl_lock);
11984 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11987 mtx_lock(&lun->lun_lock);
11988 mtx_unlock(&softc->ctl_lock);
11989 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11990 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
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))
11997 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) {
12002 mtx_unlock(&lun->lun_lock);
12004 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12006 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12011 ctl_query_async_event(union ctl_io *io)
12013 struct ctl_softc *softc = CTL_SOFTC(io);
12014 struct ctl_lun *lun;
12016 uint32_t targ_lun, initidx;
12018 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12019 mtx_lock(&softc->ctl_lock);
12020 if (targ_lun >= CTL_MAX_LUNS ||
12021 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12022 mtx_unlock(&softc->ctl_lock);
12023 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12026 mtx_lock(&lun->lun_lock);
12027 mtx_unlock(&softc->ctl_lock);
12028 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12029 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp);
12030 mtx_unlock(&lun->lun_lock);
12031 if (ua != CTL_UA_NONE)
12032 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12034 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12039 ctl_run_task(union ctl_io *io)
12041 struct ctl_softc *softc = CTL_SOFTC(io);
12044 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12045 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12046 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type));
12047 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED;
12048 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp));
12049 switch (io->taskio.task_action) {
12050 case CTL_TASK_ABORT_TASK:
12051 retval = ctl_abort_task(io);
12053 case CTL_TASK_ABORT_TASK_SET:
12054 case CTL_TASK_CLEAR_TASK_SET:
12055 retval = ctl_abort_task_set(io);
12057 case CTL_TASK_CLEAR_ACA:
12059 case CTL_TASK_I_T_NEXUS_RESET:
12060 retval = ctl_i_t_nexus_reset(io);
12062 case CTL_TASK_LUN_RESET:
12063 retval = ctl_lun_reset(softc, io);
12065 case CTL_TASK_TARGET_RESET:
12066 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET);
12068 case CTL_TASK_BUS_RESET:
12069 retval = ctl_bus_reset(softc, io);
12071 case CTL_TASK_PORT_LOGIN:
12073 case CTL_TASK_PORT_LOGOUT:
12075 case CTL_TASK_QUERY_TASK:
12076 retval = ctl_query_task(io, 0);
12078 case CTL_TASK_QUERY_TASK_SET:
12079 retval = ctl_query_task(io, 1);
12081 case CTL_TASK_QUERY_ASYNC_EVENT:
12082 retval = ctl_query_async_event(io);
12085 printf("%s: got unknown task management event %d\n",
12086 __func__, io->taskio.task_action);
12090 io->io_hdr.status = CTL_SUCCESS;
12092 io->io_hdr.status = CTL_ERROR;
12097 * For HA operation. Handle commands that come in from the other
12101 ctl_handle_isc(union ctl_io *io)
12103 struct ctl_softc *softc = CTL_SOFTC(io);
12104 struct ctl_lun *lun;
12105 const struct ctl_cmd_entry *entry;
12108 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12109 switch (io->io_hdr.msg_type) {
12110 case CTL_MSG_SERIALIZE:
12111 ctl_serialize_other_sc_cmd(&io->scsiio);
12113 case CTL_MSG_R2R: /* Only used in SER_ONLY mode. */
12114 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12115 if (targ_lun >= CTL_MAX_LUNS ||
12116 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12120 mtx_lock(&lun->lun_lock);
12121 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) {
12122 mtx_unlock(&lun->lun_lock);
12126 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12127 mtx_unlock(&lun->lun_lock);
12128 ctl_enqueue_rtr(io);
12130 case CTL_MSG_FINISH_IO:
12131 if (softc->ha_mode == CTL_HA_MODE_XFER) {
12135 if (targ_lun >= CTL_MAX_LUNS ||
12136 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12140 mtx_lock(&lun->lun_lock);
12141 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
12142 ctl_check_blocked(lun);
12143 mtx_unlock(&lun->lun_lock);
12146 case CTL_MSG_PERS_ACTION:
12147 ctl_hndl_per_res_out_on_other_sc(io);
12150 case CTL_MSG_BAD_JUJU:
12153 case CTL_MSG_DATAMOVE: /* Only used in XFER mode */
12154 ctl_datamove_remote(io);
12156 case CTL_MSG_DATAMOVE_DONE: /* Only used in XFER mode */
12157 io->scsiio.be_move_done(io);
12159 case CTL_MSG_FAILOVER:
12160 ctl_failover_lun(io);
12164 printf("%s: Invalid message type %d\n",
12165 __func__, io->io_hdr.msg_type);
12174 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12175 * there is no match.
12177 static ctl_lun_error_pattern
12178 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12180 const struct ctl_cmd_entry *entry;
12181 ctl_lun_error_pattern filtered_pattern, pattern;
12183 pattern = desc->error_pattern;
12186 * XXX KDM we need more data passed into this function to match a
12187 * custom pattern, and we actually need to implement custom pattern
12190 if (pattern & CTL_LUN_PAT_CMD)
12191 return (CTL_LUN_PAT_CMD);
12193 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12194 return (CTL_LUN_PAT_ANY);
12196 entry = ctl_get_cmd_entry(ctsio, NULL);
12198 filtered_pattern = entry->pattern & pattern;
12201 * If the user requested specific flags in the pattern (e.g.
12202 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12205 * If the user did not specify any flags, it doesn't matter whether
12206 * or not the command supports the flags.
12208 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12209 (pattern & ~CTL_LUN_PAT_MASK))
12210 return (CTL_LUN_PAT_NONE);
12213 * If the user asked for a range check, see if the requested LBA
12214 * range overlaps with this command's LBA range.
12216 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12222 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12224 return (CTL_LUN_PAT_NONE);
12226 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12227 desc->lba_range.len, FALSE);
12229 * A "pass" means that the LBA ranges don't overlap, so
12230 * this doesn't match the user's range criteria.
12232 if (action == CTL_ACTION_PASS)
12233 return (CTL_LUN_PAT_NONE);
12236 return (filtered_pattern);
12240 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12242 struct ctl_error_desc *desc, *desc2;
12244 mtx_assert(&lun->lun_lock, MA_OWNED);
12246 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12247 ctl_lun_error_pattern pattern;
12249 * Check to see whether this particular command matches
12250 * the pattern in the descriptor.
12252 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12253 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12256 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12257 case CTL_LUN_INJ_ABORTED:
12258 ctl_set_aborted(&io->scsiio);
12260 case CTL_LUN_INJ_MEDIUM_ERR:
12261 ctl_set_medium_error(&io->scsiio,
12262 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) !=
12263 CTL_FLAG_DATA_OUT);
12265 case CTL_LUN_INJ_UA:
12266 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12268 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12270 case CTL_LUN_INJ_CUSTOM:
12272 * We're assuming the user knows what he is doing.
12273 * Just copy the sense information without doing
12276 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12277 MIN(sizeof(desc->custom_sense),
12278 sizeof(io->scsiio.sense_data)));
12279 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12280 io->scsiio.sense_len = SSD_FULL_SIZE;
12281 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12283 case CTL_LUN_INJ_NONE:
12286 * If this is an error injection type we don't know
12287 * about, clear the continuous flag (if it is set)
12288 * so it will get deleted below.
12290 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12294 * By default, each error injection action is a one-shot
12296 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12299 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12305 #ifdef CTL_IO_DELAY
12307 ctl_datamove_timer_wakeup(void *arg)
12311 io = (union ctl_io *)arg;
12315 #endif /* CTL_IO_DELAY */
12318 ctl_datamove(union ctl_io *io)
12320 void (*fe_datamove)(union ctl_io *io);
12322 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
12324 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12326 /* No data transferred yet. Frontend must update this when done. */
12327 io->scsiio.kern_data_resid = io->scsiio.kern_data_len;
12330 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12335 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12336 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12338 sbuf_cat(&sb, path_str);
12339 switch (io->io_hdr.io_type) {
12341 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12342 sbuf_printf(&sb, "\n");
12343 sbuf_cat(&sb, path_str);
12344 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12345 io->scsiio.tag_num, io->scsiio.tag_type);
12348 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12349 "Tag Type: %d\n", io->taskio.task_action,
12350 io->taskio.tag_num, io->taskio.tag_type);
12353 panic("%s: Invalid CTL I/O type %d\n",
12354 __func__, io->io_hdr.io_type);
12356 sbuf_cat(&sb, path_str);
12357 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12358 (intmax_t)time_uptime - io->io_hdr.start_time);
12360 printf("%s", sbuf_data(&sb));
12362 #endif /* CTL_TIME_IO */
12364 #ifdef CTL_IO_DELAY
12365 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12366 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12369 && (lun->delay_info.datamove_delay > 0)) {
12371 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
12372 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12373 callout_reset(&io->io_hdr.delay_callout,
12374 lun->delay_info.datamove_delay * hz,
12375 ctl_datamove_timer_wakeup, io);
12376 if (lun->delay_info.datamove_type ==
12377 CTL_DELAY_TYPE_ONESHOT)
12378 lun->delay_info.datamove_delay = 0;
12385 * This command has been aborted. Set the port status, so we fail
12388 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12389 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n",
12390 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12391 io->io_hdr.nexus.targ_port,
12392 io->io_hdr.nexus.targ_lun);
12393 io->io_hdr.port_status = 31337;
12395 * Note that the backend, in this case, will get the
12396 * callback in its context. In other cases it may get
12397 * called in the frontend's interrupt thread context.
12399 io->scsiio.be_move_done(io);
12403 /* Don't confuse frontend with zero length data move. */
12404 if (io->scsiio.kern_data_len == 0) {
12405 io->scsiio.be_move_done(io);
12409 fe_datamove = CTL_PORT(io)->fe_datamove;
12414 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12416 union ctl_ha_msg msg;
12418 struct bintime cur_bt;
12421 memset(&msg, 0, sizeof(msg));
12422 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12423 msg.hdr.original_sc = io;
12424 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12425 msg.hdr.nexus = io->io_hdr.nexus;
12426 msg.hdr.status = io->io_hdr.status;
12427 msg.scsi.kern_data_resid = io->scsiio.kern_data_resid;
12428 msg.scsi.tag_num = io->scsiio.tag_num;
12429 msg.scsi.tag_type = io->scsiio.tag_type;
12430 msg.scsi.scsi_status = io->scsiio.scsi_status;
12431 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12432 io->scsiio.sense_len);
12433 msg.scsi.sense_len = io->scsiio.sense_len;
12434 msg.scsi.port_status = io->io_hdr.port_status;
12435 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12436 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12437 ctl_failover_io(io, /*have_lock*/ have_lock);
12440 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12441 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12442 msg.scsi.sense_len, M_WAITOK);
12445 getbinuptime(&cur_bt);
12446 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
12447 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
12449 io->io_hdr.num_dmas++;
12453 * The DMA to the remote side is done, now we need to tell the other side
12454 * we're done so it can continue with its data movement.
12457 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12464 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12465 printf("%s: ISC DMA write failed with error %d", __func__,
12467 ctl_set_internal_failure(&io->scsiio,
12469 /*retry_count*/ rq->ret);
12472 ctl_dt_req_free(rq);
12474 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12475 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12476 free(io->io_hdr.remote_sglist, M_CTL);
12477 io->io_hdr.remote_sglist = NULL;
12478 io->io_hdr.local_sglist = NULL;
12481 * The data is in local and remote memory, so now we need to send
12482 * status (good or back) back to the other side.
12484 ctl_send_datamove_done(io, /*have_lock*/ 0);
12488 * We've moved the data from the host/controller into local memory. Now we
12489 * need to push it over to the remote controller's memory.
12492 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12496 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12497 ctl_datamove_remote_write_cb);
12502 ctl_datamove_remote_write(union ctl_io *io)
12505 void (*fe_datamove)(union ctl_io *io);
12508 * - Get the data from the host/HBA into local memory.
12509 * - DMA memory from the local controller to the remote controller.
12510 * - Send status back to the remote controller.
12513 retval = ctl_datamove_remote_sgl_setup(io);
12517 /* Switch the pointer over so the FETD knows what to do */
12518 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12521 * Use a custom move done callback, since we need to send completion
12522 * back to the other controller, not to the backend on this side.
12524 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12526 fe_datamove = CTL_PORT(io)->fe_datamove;
12531 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12540 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12541 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12542 free(io->io_hdr.remote_sglist, M_CTL);
12543 io->io_hdr.remote_sglist = NULL;
12544 io->io_hdr.local_sglist = NULL;
12547 scsi_path_string(io, path_str, sizeof(path_str));
12548 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12549 sbuf_cat(&sb, path_str);
12550 scsi_command_string(&io->scsiio, NULL, &sb);
12551 sbuf_printf(&sb, "\n");
12552 sbuf_cat(&sb, path_str);
12553 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12554 io->scsiio.tag_num, io->scsiio.tag_type);
12555 sbuf_cat(&sb, path_str);
12556 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12557 io->io_hdr.flags, io->io_hdr.status);
12559 printk("%s", sbuf_data(&sb));
12564 * The read is done, now we need to send status (good or bad) back
12565 * to the other side.
12567 ctl_send_datamove_done(io, /*have_lock*/ 0);
12573 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12576 void (*fe_datamove)(union ctl_io *io);
12580 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12581 printf("%s: ISC DMA read failed with error %d\n", __func__,
12583 ctl_set_internal_failure(&io->scsiio,
12585 /*retry_count*/ rq->ret);
12588 ctl_dt_req_free(rq);
12590 /* Switch the pointer over so the FETD knows what to do */
12591 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12594 * Use a custom move done callback, since we need to send completion
12595 * back to the other controller, not to the backend on this side.
12597 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12599 /* XXX KDM add checks like the ones in ctl_datamove? */
12601 fe_datamove = CTL_PORT(io)->fe_datamove;
12606 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12608 struct ctl_sg_entry *local_sglist;
12609 uint32_t len_to_go;
12614 local_sglist = io->io_hdr.local_sglist;
12615 len_to_go = io->scsiio.kern_data_len;
12618 * The difficult thing here is that the size of the various
12619 * S/G segments may be different than the size from the
12620 * remote controller. That'll make it harder when DMAing
12621 * the data back to the other side.
12623 for (i = 0; len_to_go > 0; i++) {
12624 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT);
12625 local_sglist[i].addr =
12626 malloc(local_sglist[i].len, M_CTL, M_WAITOK);
12628 len_to_go -= local_sglist[i].len;
12631 * Reset the number of S/G entries accordingly. The original
12632 * number of S/G entries is available in rem_sg_entries.
12634 io->scsiio.kern_sg_entries = i;
12637 printf("%s: kern_sg_entries = %d\n", __func__,
12638 io->scsiio.kern_sg_entries);
12639 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12640 printf("%s: sg[%d] = %p, %lu\n", __func__, i,
12641 local_sglist[i].addr, local_sglist[i].len);
12648 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12649 ctl_ha_dt_cb callback)
12651 struct ctl_ha_dt_req *rq;
12652 struct ctl_sg_entry *remote_sglist, *local_sglist;
12653 uint32_t local_used, remote_used, total_used;
12656 rq = ctl_dt_req_alloc();
12659 * If we failed to allocate the request, and if the DMA didn't fail
12660 * anyway, set busy status. This is just a resource allocation
12664 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12665 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS))
12666 ctl_set_busy(&io->scsiio);
12668 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12669 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
12672 ctl_dt_req_free(rq);
12675 * The data move failed. We need to return status back
12676 * to the other controller. No point in trying to DMA
12677 * data to the remote controller.
12680 ctl_send_datamove_done(io, /*have_lock*/ 0);
12685 local_sglist = io->io_hdr.local_sglist;
12686 remote_sglist = io->io_hdr.remote_sglist;
12692 * Pull/push the data over the wire from/to the other controller.
12693 * This takes into account the possibility that the local and
12694 * remote sglists may not be identical in terms of the size of
12695 * the elements and the number of elements.
12697 * One fundamental assumption here is that the length allocated for
12698 * both the local and remote sglists is identical. Otherwise, we've
12699 * essentially got a coding error of some sort.
12701 isc_ret = CTL_HA_STATUS_SUCCESS;
12702 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12706 rq->command = command;
12710 * Both pointers should be aligned. But it is possible
12711 * that the allocation length is not. They should both
12712 * also have enough slack left over at the end, though,
12713 * to round up to the next 8 byte boundary.
12715 cur_len = MIN(local_sglist[i].len - local_used,
12716 remote_sglist[j].len - remote_used);
12717 rq->size = cur_len;
12719 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12720 tmp_ptr += local_used;
12723 /* Use physical addresses when talking to ISC hardware */
12724 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12725 /* XXX KDM use busdma */
12726 rq->local = vtophys(tmp_ptr);
12728 rq->local = tmp_ptr;
12730 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
12731 ("HA does not support BUS_ADDR"));
12732 rq->local = tmp_ptr;
12735 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12736 tmp_ptr += remote_used;
12737 rq->remote = tmp_ptr;
12739 rq->callback = NULL;
12741 local_used += cur_len;
12742 if (local_used >= local_sglist[i].len) {
12747 remote_used += cur_len;
12748 if (remote_used >= remote_sglist[j].len) {
12752 total_used += cur_len;
12754 if (total_used >= io->scsiio.kern_data_len)
12755 rq->callback = callback;
12758 printf("%s: %s: local %p remote %p size %d\n", __func__,
12759 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
12760 rq->local, rq->remote, rq->size);
12763 isc_ret = ctl_dt_single(rq);
12764 if (isc_ret > CTL_HA_STATUS_SUCCESS)
12767 if (isc_ret != CTL_HA_STATUS_WAIT) {
12776 ctl_datamove_remote_read(union ctl_io *io)
12782 * This will send an error to the other controller in the case of a
12785 retval = ctl_datamove_remote_sgl_setup(io);
12789 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12790 ctl_datamove_remote_read_cb);
12793 * Make sure we free memory if there was an error.. The
12794 * ctl_datamove_remote_xfer() function will send the
12795 * datamove done message, or call the callback with an
12796 * error if there is a problem.
12798 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12799 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12800 free(io->io_hdr.remote_sglist, M_CTL);
12801 io->io_hdr.remote_sglist = NULL;
12802 io->io_hdr.local_sglist = NULL;
12807 * Process a datamove request from the other controller. This is used for
12808 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
12809 * first. Once that is complete, the data gets DMAed into the remote
12810 * controller's memory. For reads, we DMA from the remote controller's
12811 * memory into our memory first, and then move it out to the FETD.
12814 ctl_datamove_remote(union ctl_io *io)
12817 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
12819 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12820 ctl_failover_io(io, /*have_lock*/ 0);
12825 * Note that we look for an aborted I/O here, but don't do some of
12826 * the other checks that ctl_datamove() normally does.
12827 * We don't need to run the datamove delay code, since that should
12828 * have been done if need be on the other controller.
12830 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12831 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__,
12832 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12833 io->io_hdr.nexus.targ_port,
12834 io->io_hdr.nexus.targ_lun);
12835 io->io_hdr.port_status = 31338;
12836 ctl_send_datamove_done(io, /*have_lock*/ 0);
12840 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
12841 ctl_datamove_remote_write(io);
12842 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
12843 ctl_datamove_remote_read(io);
12845 io->io_hdr.port_status = 31339;
12846 ctl_send_datamove_done(io, /*have_lock*/ 0);
12851 ctl_process_done(union ctl_io *io)
12853 struct ctl_softc *softc = CTL_SOFTC(io);
12854 struct ctl_port *port = CTL_PORT(io);
12855 struct ctl_lun *lun = CTL_LUN(io);
12856 void (*fe_done)(union ctl_io *io);
12857 union ctl_ha_msg msg;
12859 CTL_DEBUG_PRINT(("ctl_process_done\n"));
12860 fe_done = port->fe_done;
12863 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12868 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12869 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12871 sbuf_cat(&sb, path_str);
12872 switch (io->io_hdr.io_type) {
12874 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12875 sbuf_printf(&sb, "\n");
12876 sbuf_cat(&sb, path_str);
12877 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12878 io->scsiio.tag_num, io->scsiio.tag_type);
12881 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12882 "Tag Type: %d\n", io->taskio.task_action,
12883 io->taskio.tag_num, io->taskio.tag_type);
12886 panic("%s: Invalid CTL I/O type %d\n",
12887 __func__, io->io_hdr.io_type);
12889 sbuf_cat(&sb, path_str);
12890 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
12891 (intmax_t)time_uptime - io->io_hdr.start_time);
12893 printf("%s", sbuf_data(&sb));
12895 #endif /* CTL_TIME_IO */
12897 switch (io->io_hdr.io_type) {
12901 if (ctl_debug & CTL_DEBUG_INFO)
12902 ctl_io_error_print(io, NULL);
12906 panic("%s: Invalid CTL I/O type %d\n",
12907 __func__, io->io_hdr.io_type);
12911 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
12912 io->io_hdr.nexus.targ_mapped_lun));
12916 mtx_lock(&lun->lun_lock);
12919 * Check to see if we have any informational exception and status
12920 * of this command can be modified to report it in form of either
12921 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field.
12923 if (lun->ie_reported == 0 && lun->ie_asc != 0 &&
12924 io->io_hdr.status == CTL_SUCCESS &&
12925 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) {
12926 uint8_t mrie = lun->MODE_IE.mrie;
12927 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) ||
12928 (lun->MODE_VER.byte3 & SMS_VER_PER));
12929 if (((mrie == SIEP_MRIE_REC_COND && per) ||
12930 mrie == SIEP_MRIE_REC_UNCOND ||
12931 mrie == SIEP_MRIE_NO_SENSE) &&
12932 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags &
12933 CTL_CMD_FLAG_NO_SENSE) == 0) {
12934 ctl_set_sense(&io->scsiio,
12935 /*current_error*/ 1,
12936 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ?
12937 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR,
12938 /*asc*/ lun->ie_asc,
12939 /*ascq*/ lun->ie_ascq,
12941 lun->ie_reported = 1;
12943 } else if (lun->ie_reported < 0)
12944 lun->ie_reported = 0;
12947 * Check to see if we have any errors to inject here. We only
12948 * inject errors for commands that don't already have errors set.
12950 if (!STAILQ_EMPTY(&lun->error_list) &&
12951 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
12952 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
12953 ctl_inject_error(lun, io);
12956 * XXX KDM how do we treat commands that aren't completed
12959 * XXX KDM should we also track I/O latency?
12961 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
12962 io->io_hdr.io_type == CTL_IO_SCSI) {
12968 bintime_sub(&bt, &io->io_hdr.start_bt);
12970 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12972 type = CTL_STATS_READ;
12973 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12975 type = CTL_STATS_WRITE;
12977 type = CTL_STATS_NO_IO;
12979 #ifdef CTL_LEGACY_STATS
12980 uint32_t targ_port = port->targ_port;
12981 lun->legacy_stats.ports[targ_port].bytes[type] +=
12982 io->scsiio.kern_total_len;
12983 lun->legacy_stats.ports[targ_port].operations[type] ++;
12984 lun->legacy_stats.ports[targ_port].num_dmas[type] +=
12985 io->io_hdr.num_dmas;
12987 bintime_add(&lun->legacy_stats.ports[targ_port].dma_time[type],
12988 &io->io_hdr.dma_bt);
12989 bintime_add(&lun->legacy_stats.ports[targ_port].time[type],
12992 #endif /* CTL_LEGACY_STATS */
12994 lun->stats.bytes[type] += io->scsiio.kern_total_len;
12995 lun->stats.operations[type] ++;
12996 lun->stats.dmas[type] += io->io_hdr.num_dmas;
12998 bintime_add(&lun->stats.dma_time[type], &io->io_hdr.dma_bt);
12999 bintime_add(&lun->stats.time[type], &bt);
13002 mtx_lock(&port->port_lock);
13003 port->stats.bytes[type] += io->scsiio.kern_total_len;
13004 port->stats.operations[type] ++;
13005 port->stats.dmas[type] += io->io_hdr.num_dmas;
13007 bintime_add(&port->stats.dma_time[type], &io->io_hdr.dma_bt);
13008 bintime_add(&port->stats.time[type], &bt);
13010 mtx_unlock(&port->port_lock);
13014 * Remove this from the OOA queue.
13016 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13018 if (TAILQ_EMPTY(&lun->ooa_queue))
13019 lun->last_busy = getsbinuptime();
13023 * Run through the blocked queue on this LUN and see if anything
13024 * has become unblocked, now that this transaction is done.
13026 ctl_check_blocked(lun);
13029 * If the LUN has been invalidated, free it if there is nothing
13030 * left on its OOA queue.
13032 if ((lun->flags & CTL_LUN_INVALID)
13033 && TAILQ_EMPTY(&lun->ooa_queue)) {
13034 mtx_unlock(&lun->lun_lock);
13035 mtx_lock(&softc->ctl_lock);
13037 mtx_unlock(&softc->ctl_lock);
13039 mtx_unlock(&lun->lun_lock);
13044 * If this command has been aborted, make sure we set the status
13045 * properly. The FETD is responsible for freeing the I/O and doing
13046 * whatever it needs to do to clean up its state.
13048 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13049 ctl_set_task_aborted(&io->scsiio);
13052 * If enabled, print command error status.
13054 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
13055 (ctl_debug & CTL_DEBUG_INFO) != 0)
13056 ctl_io_error_print(io, NULL);
13059 * Tell the FETD or the other shelf controller we're done with this
13060 * command. Note that only SCSI commands get to this point. Task
13061 * management commands are completed above.
13063 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
13064 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) {
13065 memset(&msg, 0, sizeof(msg));
13066 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13067 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
13068 msg.hdr.nexus = io->io_hdr.nexus;
13069 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13070 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data),
13079 * Front end should call this if it doesn't do autosense. When the request
13080 * sense comes back in from the initiator, we'll dequeue this and send it.
13083 ctl_queue_sense(union ctl_io *io)
13085 struct ctl_softc *softc = CTL_SOFTC(io);
13086 struct ctl_port *port = CTL_PORT(io);
13087 struct ctl_lun *lun;
13088 uint32_t initidx, targ_lun;
13090 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13092 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13095 * LUN lookup will likely move to the ctl_work_thread() once we
13096 * have our new queueing infrastructure (that doesn't put things on
13097 * a per-LUN queue initially). That is so that we can handle
13098 * things like an INQUIRY to a LUN that we don't have enabled. We
13099 * can't deal with that right now.
13100 * If we don't have a LUN for this, just toss the sense information.
13102 mtx_lock(&softc->ctl_lock);
13103 if (targ_lun >= CTL_MAX_LUNS ||
13104 (lun = softc->ctl_luns[targ_lun]) == NULL) {
13105 mtx_unlock(&softc->ctl_lock);
13108 mtx_lock(&lun->lun_lock);
13109 mtx_unlock(&softc->ctl_lock);
13112 * Already have CA set for this LUN...toss the sense information.
13114 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13115 if (ctl_is_set(lun->have_ca, initidx)) {
13116 mtx_unlock(&lun->lun_lock);
13120 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data,
13121 MIN(sizeof(lun->pending_sense[initidx]),
13122 sizeof(io->scsiio.sense_data)));
13123 ctl_set_mask(lun->have_ca, initidx);
13124 mtx_unlock(&lun->lun_lock);
13128 return (CTL_RETVAL_COMPLETE);
13133 * Primary command inlet from frontend ports. All SCSI and task I/O
13134 * requests must go through this function.
13137 ctl_queue(union ctl_io *io)
13139 struct ctl_port *port = CTL_PORT(io);
13141 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13144 io->io_hdr.start_time = time_uptime;
13145 getbinuptime(&io->io_hdr.start_bt);
13146 #endif /* CTL_TIME_IO */
13148 /* Map FE-specific LUN ID into global one. */
13149 io->io_hdr.nexus.targ_mapped_lun =
13150 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13152 switch (io->io_hdr.io_type) {
13155 if (ctl_debug & CTL_DEBUG_CDB)
13157 ctl_enqueue_incoming(io);
13160 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13164 return (CTL_RETVAL_COMPLETE);
13167 #ifdef CTL_IO_DELAY
13169 ctl_done_timer_wakeup(void *arg)
13173 io = (union ctl_io *)arg;
13176 #endif /* CTL_IO_DELAY */
13179 ctl_serseq_done(union ctl_io *io)
13181 struct ctl_lun *lun = CTL_LUN(io);;
13183 if (lun->be_lun == NULL ||
13184 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF)
13186 mtx_lock(&lun->lun_lock);
13187 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE;
13188 ctl_check_blocked(lun);
13189 mtx_unlock(&lun->lun_lock);
13193 ctl_done(union ctl_io *io)
13197 * Enable this to catch duplicate completion issues.
13200 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13201 printf("%s: type %d msg %d cdb %x iptl: "
13202 "%u:%u:%u tag 0x%04x "
13203 "flag %#x status %x\n",
13205 io->io_hdr.io_type,
13206 io->io_hdr.msg_type,
13208 io->io_hdr.nexus.initid,
13209 io->io_hdr.nexus.targ_port,
13210 io->io_hdr.nexus.targ_lun,
13211 (io->io_hdr.io_type ==
13213 io->taskio.tag_num :
13214 io->scsiio.tag_num,
13216 io->io_hdr.status);
13218 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13222 * This is an internal copy of an I/O, and should not go through
13223 * the normal done processing logic.
13225 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13228 #ifdef CTL_IO_DELAY
13229 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13230 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13232 struct ctl_lun *lun = CTL_LUN(io);
13235 && (lun->delay_info.done_delay > 0)) {
13237 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
13238 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13239 callout_reset(&io->io_hdr.delay_callout,
13240 lun->delay_info.done_delay * hz,
13241 ctl_done_timer_wakeup, io);
13242 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13243 lun->delay_info.done_delay = 0;
13247 #endif /* CTL_IO_DELAY */
13249 ctl_enqueue_done(io);
13253 ctl_work_thread(void *arg)
13255 struct ctl_thread *thr = (struct ctl_thread *)arg;
13256 struct ctl_softc *softc = thr->ctl_softc;
13260 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13264 * We handle the queues in this order:
13266 * - done queue (to free up resources, unblock other commands)
13270 * If those queues are empty, we break out of the loop and
13273 mtx_lock(&thr->queue_lock);
13274 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13276 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13277 mtx_unlock(&thr->queue_lock);
13278 ctl_handle_isc(io);
13281 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13283 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13284 /* clear any blocked commands, call fe_done */
13285 mtx_unlock(&thr->queue_lock);
13286 ctl_process_done(io);
13289 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13291 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13292 mtx_unlock(&thr->queue_lock);
13293 if (io->io_hdr.io_type == CTL_IO_TASK)
13296 ctl_scsiio_precheck(softc, &io->scsiio);
13299 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13301 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13302 mtx_unlock(&thr->queue_lock);
13303 retval = ctl_scsiio(&io->scsiio);
13304 if (retval != CTL_RETVAL_COMPLETE)
13305 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13309 /* Sleep until we have something to do. */
13310 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13315 ctl_lun_thread(void *arg)
13317 struct ctl_softc *softc = (struct ctl_softc *)arg;
13318 struct ctl_be_lun *be_lun;
13320 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13323 mtx_lock(&softc->ctl_lock);
13324 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13325 if (be_lun != NULL) {
13326 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13327 mtx_unlock(&softc->ctl_lock);
13328 ctl_create_lun(be_lun);
13332 /* Sleep until we have something to do. */
13333 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13334 PDROP | PRIBIO, "-", 0);
13339 ctl_thresh_thread(void *arg)
13341 struct ctl_softc *softc = (struct ctl_softc *)arg;
13342 struct ctl_lun *lun;
13343 struct ctl_logical_block_provisioning_page *page;
13345 union ctl_ha_msg msg;
13346 uint64_t thres, val;
13349 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13352 mtx_lock(&softc->ctl_lock);
13353 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13354 if ((lun->flags & CTL_LUN_DISABLED) ||
13355 (lun->flags & CTL_LUN_NO_MEDIA) ||
13356 lun->backend->lun_attr == NULL)
13358 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
13359 softc->ha_mode == CTL_HA_MODE_XFER)
13361 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0)
13364 page = &lun->MODE_LBP;
13365 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13366 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13368 thres = scsi_4btoul(page->descr[i].count);
13369 thres <<= CTL_LBP_EXPONENT;
13370 switch (page->descr[i].resource) {
13372 attr = "blocksavail";
13375 attr = "blocksused";
13378 attr = "poolblocksavail";
13381 attr = "poolblocksused";
13386 mtx_unlock(&softc->ctl_lock); // XXX
13387 val = lun->backend->lun_attr(
13388 lun->be_lun->be_lun, attr);
13389 mtx_lock(&softc->ctl_lock);
13390 if (val == UINT64_MAX)
13392 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13393 == SLBPPD_ARMING_INC)
13394 e = (val >= thres);
13396 e = (val <= thres);
13400 mtx_lock(&lun->lun_lock);
13402 scsi_u64to8b((uint8_t *)&page->descr[i] -
13403 (uint8_t *)page, lun->ua_tpt_info);
13404 if (lun->lasttpt == 0 ||
13405 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13406 lun->lasttpt = time_uptime;
13407 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13413 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13416 mtx_unlock(&lun->lun_lock);
13418 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
13419 /* Send msg to other side. */
13420 bzero(&msg.ua, sizeof(msg.ua));
13421 msg.hdr.msg_type = CTL_MSG_UA;
13422 msg.hdr.nexus.initid = -1;
13423 msg.hdr.nexus.targ_port = -1;
13424 msg.hdr.nexus.targ_lun = lun->lun;
13425 msg.hdr.nexus.targ_mapped_lun = lun->lun;
13427 msg.ua.ua_set = (set > 0);
13428 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES;
13429 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8);
13430 mtx_unlock(&softc->ctl_lock); // XXX
13431 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13432 sizeof(msg.ua), M_WAITOK);
13433 mtx_lock(&softc->ctl_lock);
13436 mtx_unlock(&softc->ctl_lock);
13437 pause("-", CTL_LBP_PERIOD * hz);
13442 ctl_enqueue_incoming(union ctl_io *io)
13444 struct ctl_softc *softc = CTL_SOFTC(io);
13445 struct ctl_thread *thr;
13448 idx = (io->io_hdr.nexus.targ_port * 127 +
13449 io->io_hdr.nexus.initid) % worker_threads;
13450 thr = &softc->threads[idx];
13451 mtx_lock(&thr->queue_lock);
13452 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13453 mtx_unlock(&thr->queue_lock);
13458 ctl_enqueue_rtr(union ctl_io *io)
13460 struct ctl_softc *softc = CTL_SOFTC(io);
13461 struct ctl_thread *thr;
13463 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13464 mtx_lock(&thr->queue_lock);
13465 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13466 mtx_unlock(&thr->queue_lock);
13471 ctl_enqueue_done(union ctl_io *io)
13473 struct ctl_softc *softc = CTL_SOFTC(io);
13474 struct ctl_thread *thr;
13476 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13477 mtx_lock(&thr->queue_lock);
13478 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13479 mtx_unlock(&thr->queue_lock);
13484 ctl_enqueue_isc(union ctl_io *io)
13486 struct ctl_softc *softc = CTL_SOFTC(io);
13487 struct ctl_thread *thr;
13489 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13490 mtx_lock(&thr->queue_lock);
13491 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13492 mtx_unlock(&thr->queue_lock);