2 * Copyright (c) 2003-2009 Silicon Graphics International Corp.
3 * Copyright (c) 2012 The FreeBSD Foundation
4 * Copyright (c) 2015 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_da.h>
73 #include <cam/ctl/ctl_io.h>
74 #include <cam/ctl/ctl.h>
75 #include <cam/ctl/ctl_frontend.h>
76 #include <cam/ctl/ctl_util.h>
77 #include <cam/ctl/ctl_backend.h>
78 #include <cam/ctl/ctl_ioctl.h>
79 #include <cam/ctl/ctl_ha.h>
80 #include <cam/ctl/ctl_private.h>
81 #include <cam/ctl/ctl_debug.h>
82 #include <cam/ctl/ctl_scsi_all.h>
83 #include <cam/ctl/ctl_error.h>
85 struct ctl_softc *control_softc = NULL;
88 * Template mode pages.
92 * Note that these are default values only. The actual values will be
93 * filled in when the user does a mode sense.
95 const static struct copan_debugconf_subpage debugconf_page_default = {
96 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
97 DBGCNF_SUBPAGE_CODE, /* subpage */
98 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
99 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
100 DBGCNF_VERSION, /* page_version */
101 {CTL_TIME_IO_DEFAULT_SECS>>8,
102 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */
105 const static struct copan_debugconf_subpage debugconf_page_changeable = {
106 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
107 DBGCNF_SUBPAGE_CODE, /* subpage */
108 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
109 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
110 0, /* page_version */
111 {0xff,0xff}, /* ctl_time_io_secs */
114 const static struct scsi_da_rw_recovery_page rw_er_page_default = {
115 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
116 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
117 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE,
118 /*read_retry_count*/0,
119 /*correction_span*/0,
120 /*head_offset_count*/0,
121 /*data_strobe_offset_cnt*/0,
122 /*byte8*/SMS_RWER_LBPERE,
123 /*write_retry_count*/0,
125 /*recovery_time_limit*/{0, 0},
128 const static struct scsi_da_rw_recovery_page rw_er_page_changeable = {
129 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
130 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
132 /*read_retry_count*/0,
133 /*correction_span*/0,
134 /*head_offset_count*/0,
135 /*data_strobe_offset_cnt*/0,
137 /*write_retry_count*/0,
139 /*recovery_time_limit*/{0, 0},
142 const static struct scsi_format_page format_page_default = {
143 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
144 /*page_length*/sizeof(struct scsi_format_page) - 2,
145 /*tracks_per_zone*/ {0, 0},
146 /*alt_sectors_per_zone*/ {0, 0},
147 /*alt_tracks_per_zone*/ {0, 0},
148 /*alt_tracks_per_lun*/ {0, 0},
149 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
150 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
151 /*bytes_per_sector*/ {0, 0},
152 /*interleave*/ {0, 0},
153 /*track_skew*/ {0, 0},
154 /*cylinder_skew*/ {0, 0},
156 /*reserved*/ {0, 0, 0}
159 const static struct scsi_format_page format_page_changeable = {
160 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
161 /*page_length*/sizeof(struct scsi_format_page) - 2,
162 /*tracks_per_zone*/ {0, 0},
163 /*alt_sectors_per_zone*/ {0, 0},
164 /*alt_tracks_per_zone*/ {0, 0},
165 /*alt_tracks_per_lun*/ {0, 0},
166 /*sectors_per_track*/ {0, 0},
167 /*bytes_per_sector*/ {0, 0},
168 /*interleave*/ {0, 0},
169 /*track_skew*/ {0, 0},
170 /*cylinder_skew*/ {0, 0},
172 /*reserved*/ {0, 0, 0}
175 const static struct scsi_rigid_disk_page rigid_disk_page_default = {
176 /*page_code*/SMS_RIGID_DISK_PAGE,
177 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
178 /*cylinders*/ {0, 0, 0},
179 /*heads*/ CTL_DEFAULT_HEADS,
180 /*start_write_precomp*/ {0, 0, 0},
181 /*start_reduced_current*/ {0, 0, 0},
182 /*step_rate*/ {0, 0},
183 /*landing_zone_cylinder*/ {0, 0, 0},
184 /*rpl*/ SRDP_RPL_DISABLED,
185 /*rotational_offset*/ 0,
187 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
188 CTL_DEFAULT_ROTATION_RATE & 0xff},
192 const static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
193 /*page_code*/SMS_RIGID_DISK_PAGE,
194 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
195 /*cylinders*/ {0, 0, 0},
197 /*start_write_precomp*/ {0, 0, 0},
198 /*start_reduced_current*/ {0, 0, 0},
199 /*step_rate*/ {0, 0},
200 /*landing_zone_cylinder*/ {0, 0, 0},
202 /*rotational_offset*/ 0,
204 /*rotation_rate*/ {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,
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_DEXCPT,
289 /*interval_timer*/{0, 0, 0, 0},
290 /*report_count*/{0, 0, 0, 0}
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,
298 /*interval_timer*/{0, 0, 0, 0},
299 /*report_count*/{0, 0, 0, 0}
302 #define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4)
304 const static struct ctl_logical_block_provisioning_page lbp_page_default = {{
305 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
306 /*subpage_code*/0x02,
307 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
309 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
314 /*count*/{0, 0, 0, 0}},
318 /*count*/{0, 0, 0, 0}},
322 /*count*/{0, 0, 0, 0}},
326 /*count*/{0, 0, 0, 0}}
330 const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{
331 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
332 /*subpage_code*/0x02,
333 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
335 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
340 /*count*/{0, 0, 0, 0}},
344 /*count*/{0, 0, 0, 0}},
348 /*count*/{0, 0, 0, 0}},
352 /*count*/{0, 0, 0, 0}}
356 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
357 static int worker_threads = -1;
358 TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads);
359 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
360 &worker_threads, 1, "Number of worker threads");
361 static int ctl_debug = CTL_DEBUG_NONE;
362 TUNABLE_INT("kern.cam.ctl.debug", &ctl_debug);
363 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN,
364 &ctl_debug, 0, "Enabled debug flags");
367 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
368 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
369 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0),
370 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
372 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 10
374 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
376 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
377 static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest);
378 static int ctl_init(void);
379 void ctl_shutdown(void);
380 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
381 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
382 static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
383 static void ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
384 struct ctl_ooa *ooa_hdr,
385 struct ctl_ooa_entry *kern_entries);
386 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
388 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
389 struct ctl_be_lun *be_lun);
390 static int ctl_free_lun(struct ctl_lun *lun);
391 static void ctl_create_lun(struct ctl_be_lun *be_lun);
392 static struct ctl_port * ctl_io_port(struct ctl_io_hdr *io_hdr);
394 static int ctl_do_mode_select(union ctl_io *io);
395 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
396 uint64_t res_key, uint64_t sa_res_key,
397 uint8_t type, uint32_t residx,
398 struct ctl_scsiio *ctsio,
399 struct scsi_per_res_out *cdb,
400 struct scsi_per_res_out_parms* param);
401 static void ctl_pro_preempt_other(struct ctl_lun *lun,
402 union ctl_ha_msg *msg);
403 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
404 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
405 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
406 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
407 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
408 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
409 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
411 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
413 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
414 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
415 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
416 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
417 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
418 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2,
420 static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2);
421 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
422 union ctl_io *pending_io, union ctl_io *ooa_io);
423 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
424 union ctl_io *starting_io);
425 static int ctl_check_blocked(struct ctl_lun *lun);
426 static int ctl_scsiio_lun_check(struct ctl_lun *lun,
427 const struct ctl_cmd_entry *entry,
428 struct ctl_scsiio *ctsio);
429 static void ctl_failover_lun(union ctl_io *io);
430 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
431 struct ctl_scsiio *ctsio);
432 static int ctl_scsiio(struct ctl_scsiio *ctsio);
434 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
435 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
436 ctl_ua_type ua_type);
437 static int ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io,
438 ctl_ua_type ua_type);
439 static int ctl_lun_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
440 static int ctl_abort_task(union ctl_io *io);
441 static int ctl_abort_task_set(union ctl_io *io);
442 static int ctl_query_task(union ctl_io *io, int task_set);
443 static int ctl_i_t_nexus_reset(union ctl_io *io);
444 static int ctl_query_async_event(union ctl_io *io);
445 static void ctl_run_task(union ctl_io *io);
447 static void ctl_datamove_timer_wakeup(void *arg);
448 static void ctl_done_timer_wakeup(void *arg);
449 #endif /* CTL_IO_DELAY */
451 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
452 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
453 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
454 static void ctl_datamove_remote_write(union ctl_io *io);
455 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
456 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
457 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
458 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
459 ctl_ha_dt_cb callback);
460 static void ctl_datamove_remote_read(union ctl_io *io);
461 static void ctl_datamove_remote(union ctl_io *io);
462 static void ctl_process_done(union ctl_io *io);
463 static void ctl_lun_thread(void *arg);
464 static void ctl_thresh_thread(void *arg);
465 static void ctl_work_thread(void *arg);
466 static void ctl_enqueue_incoming(union ctl_io *io);
467 static void ctl_enqueue_rtr(union ctl_io *io);
468 static void ctl_enqueue_done(union ctl_io *io);
469 static void ctl_enqueue_isc(union ctl_io *io);
470 static const struct ctl_cmd_entry *
471 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
472 static const struct ctl_cmd_entry *
473 ctl_validate_command(struct ctl_scsiio *ctsio);
474 static int ctl_cmd_applicable(uint8_t lun_type,
475 const struct ctl_cmd_entry *entry);
477 static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx);
478 static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx);
479 static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx);
480 static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key);
483 * Load the serialization table. This isn't very pretty, but is probably
484 * the easiest way to do it.
486 #include "ctl_ser_table.c"
489 * We only need to define open, close and ioctl routines for this driver.
491 static struct cdevsw ctl_cdevsw = {
492 .d_version = D_VERSION,
495 .d_close = ctl_close,
496 .d_ioctl = ctl_ioctl,
501 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
503 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
505 static moduledata_t ctl_moduledata = {
507 ctl_module_event_handler,
511 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
512 MODULE_VERSION(ctl, 1);
514 static struct ctl_frontend ha_frontend =
520 ctl_ha_datamove(union ctl_io *io)
523 struct ctl_sg_entry *sgl;
524 union ctl_ha_msg msg;
525 uint32_t sg_entries_sent;
526 int do_sg_copy, i, j;
528 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
529 memset(&msg.dt, 0, sizeof(msg.dt));
530 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
531 msg.hdr.original_sc = io->io_hdr.original_sc;
532 msg.hdr.serializing_sc = io;
533 msg.hdr.nexus = io->io_hdr.nexus;
534 msg.hdr.status = io->io_hdr.status;
535 msg.dt.flags = io->io_hdr.flags;
538 * We convert everything into a S/G list here. We can't
539 * pass by reference, only by value between controllers.
540 * So we can't pass a pointer to the S/G list, only as many
541 * S/G entries as we can fit in here. If it's possible for
542 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
543 * then we need to break this up into multiple transfers.
545 if (io->scsiio.kern_sg_entries == 0) {
546 msg.dt.kern_sg_entries = 1;
548 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
549 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
551 /* XXX KDM use busdma here! */
552 msg.dt.sg_list[0].addr =
553 (void *)vtophys(io->scsiio.kern_data_ptr);
556 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
557 ("HA does not support BUS_ADDR"));
558 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
560 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
563 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
567 msg.dt.kern_data_len = io->scsiio.kern_data_len;
568 msg.dt.kern_total_len = io->scsiio.kern_total_len;
569 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
570 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
571 msg.dt.sg_sequence = 0;
574 * Loop until we've sent all of the S/G entries. On the
575 * other end, we'll recompose these S/G entries into one
576 * contiguous list before processing.
578 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries;
579 msg.dt.sg_sequence++) {
580 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) /
581 sizeof(msg.dt.sg_list[0])),
582 msg.dt.kern_sg_entries - sg_entries_sent);
583 if (do_sg_copy != 0) {
584 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
585 for (i = sg_entries_sent, j = 0;
586 i < msg.dt.cur_sg_entries; i++, j++) {
588 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
589 msg.dt.sg_list[j].addr = sgl[i].addr;
591 /* XXX KDM use busdma here! */
592 msg.dt.sg_list[j].addr =
593 (void *)vtophys(sgl[i].addr);
596 KASSERT((io->io_hdr.flags &
597 CTL_FLAG_BUS_ADDR) == 0,
598 ("HA does not support BUS_ADDR"));
599 msg.dt.sg_list[j].addr = sgl[i].addr;
601 msg.dt.sg_list[j].len = sgl[i].len;
605 sg_entries_sent += msg.dt.cur_sg_entries;
606 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries);
607 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
608 sizeof(msg.dt) - sizeof(msg.dt.sg_list) +
609 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries,
610 M_WAITOK) > CTL_HA_STATUS_SUCCESS) {
611 io->io_hdr.port_status = 31341;
612 io->scsiio.be_move_done(io);
615 msg.dt.sent_sg_entries = sg_entries_sent;
619 * Officially handover the request from us to peer.
620 * If failover has just happened, then we must return error.
621 * If failover happen just after, then it is not our problem.
624 mtx_lock(&lun->lun_lock);
625 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
627 mtx_unlock(&lun->lun_lock);
628 io->io_hdr.port_status = 31342;
629 io->scsiio.be_move_done(io);
632 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
633 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
635 mtx_unlock(&lun->lun_lock);
639 ctl_ha_done(union ctl_io *io)
641 union ctl_ha_msg msg;
643 if (io->io_hdr.io_type == CTL_IO_SCSI) {
644 memset(&msg, 0, sizeof(msg));
645 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
646 msg.hdr.original_sc = io->io_hdr.original_sc;
647 msg.hdr.nexus = io->io_hdr.nexus;
648 msg.hdr.status = io->io_hdr.status;
649 msg.scsi.scsi_status = io->scsiio.scsi_status;
650 msg.scsi.tag_num = io->scsiio.tag_num;
651 msg.scsi.tag_type = io->scsiio.tag_type;
652 msg.scsi.sense_len = io->scsiio.sense_len;
653 msg.scsi.sense_residual = io->scsiio.sense_residual;
654 msg.scsi.residual = io->scsiio.residual;
655 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
656 io->scsiio.sense_len);
657 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
658 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
659 msg.scsi.sense_len, M_WAITOK);
665 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
666 union ctl_ha_msg *msg_info)
668 struct ctl_scsiio *ctsio;
670 if (msg_info->hdr.original_sc == NULL) {
671 printf("%s: original_sc == NULL!\n", __func__);
672 /* XXX KDM now what? */
676 ctsio = &msg_info->hdr.original_sc->scsiio;
677 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
678 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
679 ctsio->io_hdr.status = msg_info->hdr.status;
680 ctsio->scsi_status = msg_info->scsi.scsi_status;
681 ctsio->sense_len = msg_info->scsi.sense_len;
682 ctsio->sense_residual = msg_info->scsi.sense_residual;
683 ctsio->residual = msg_info->scsi.residual;
684 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
685 msg_info->scsi.sense_len);
686 ctl_enqueue_isc((union ctl_io *)ctsio);
690 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
691 union ctl_ha_msg *msg_info)
693 struct ctl_scsiio *ctsio;
695 if (msg_info->hdr.serializing_sc == NULL) {
696 printf("%s: serializing_sc == NULL!\n", __func__);
697 /* XXX KDM now what? */
701 ctsio = &msg_info->hdr.serializing_sc->scsiio;
702 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
703 ctl_enqueue_isc((union ctl_io *)ctsio);
707 ctl_isc_announce_lun(struct ctl_lun *lun)
709 struct ctl_softc *softc = lun->ctl_softc;
710 union ctl_ha_msg *msg;
711 struct ctl_ha_msg_lun_pr_key pr_key;
714 if (softc->ha_link != CTL_HA_LINK_ONLINE)
716 mtx_lock(&lun->lun_lock);
717 i = sizeof(msg->lun);
719 i += lun->lun_devid->len;
720 i += sizeof(pr_key) * lun->pr_key_count;
722 mtx_unlock(&lun->lun_lock);
723 msg = malloc(i, M_CTL, M_WAITOK);
724 mtx_lock(&lun->lun_lock);
725 k = sizeof(msg->lun);
727 k += lun->lun_devid->len;
728 k += sizeof(pr_key) * lun->pr_key_count;
734 bzero(&msg->lun, sizeof(msg->lun));
735 msg->hdr.msg_type = CTL_MSG_LUN_SYNC;
736 msg->hdr.nexus.targ_lun = lun->lun;
737 msg->hdr.nexus.targ_mapped_lun = lun->lun;
738 msg->lun.flags = lun->flags;
739 msg->lun.pr_generation = lun->PRGeneration;
740 msg->lun.pr_res_idx = lun->pr_res_idx;
741 msg->lun.pr_res_type = lun->res_type;
742 msg->lun.pr_key_count = lun->pr_key_count;
744 if (lun->lun_devid) {
745 msg->lun.lun_devid_len = lun->lun_devid->len;
746 memcpy(&msg->lun.data[i], lun->lun_devid->data,
747 msg->lun.lun_devid_len);
748 i += msg->lun.lun_devid_len;
750 for (k = 0; k < CTL_MAX_INITIATORS; k++) {
751 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0)
754 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key));
757 mtx_unlock(&lun->lun_lock);
758 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
762 if (lun->flags & CTL_LUN_PRIMARY_SC) {
763 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
764 ctl_isc_announce_mode(lun, -1,
765 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
766 lun->mode_pages.index[i].subpage);
772 ctl_isc_announce_port(struct ctl_port *port)
774 struct ctl_softc *softc = port->ctl_softc;
775 union ctl_ha_msg *msg;
778 if (port->targ_port < softc->port_min ||
779 port->targ_port >= softc->port_max ||
780 softc->ha_link != CTL_HA_LINK_ONLINE)
782 i = sizeof(msg->port) + strlen(port->port_name) + 1;
784 i += sizeof(uint32_t) * CTL_MAX_LUNS;
785 if (port->port_devid)
786 i += port->port_devid->len;
787 if (port->target_devid)
788 i += port->target_devid->len;
789 if (port->init_devid)
790 i += port->init_devid->len;
791 msg = malloc(i, M_CTL, M_WAITOK);
792 bzero(&msg->port, sizeof(msg->port));
793 msg->hdr.msg_type = CTL_MSG_PORT_SYNC;
794 msg->hdr.nexus.targ_port = port->targ_port;
795 msg->port.port_type = port->port_type;
796 msg->port.physical_port = port->physical_port;
797 msg->port.virtual_port = port->virtual_port;
798 msg->port.status = port->status;
800 msg->port.name_len = sprintf(&msg->port.data[i],
801 "%d:%s", softc->ha_id, port->port_name) + 1;
802 i += msg->port.name_len;
804 msg->port.lun_map_len = sizeof(uint32_t) * CTL_MAX_LUNS;
805 memcpy(&msg->port.data[i], port->lun_map,
806 msg->port.lun_map_len);
807 i += msg->port.lun_map_len;
809 if (port->port_devid) {
810 msg->port.port_devid_len = port->port_devid->len;
811 memcpy(&msg->port.data[i], port->port_devid->data,
812 msg->port.port_devid_len);
813 i += msg->port.port_devid_len;
815 if (port->target_devid) {
816 msg->port.target_devid_len = port->target_devid->len;
817 memcpy(&msg->port.data[i], port->target_devid->data,
818 msg->port.target_devid_len);
819 i += msg->port.target_devid_len;
821 if (port->init_devid) {
822 msg->port.init_devid_len = port->init_devid->len;
823 memcpy(&msg->port.data[i], port->init_devid->data,
824 msg->port.init_devid_len);
825 i += msg->port.init_devid_len;
827 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
833 ctl_isc_announce_iid(struct ctl_port *port, int iid)
835 struct ctl_softc *softc = port->ctl_softc;
836 union ctl_ha_msg *msg;
839 if (port->targ_port < softc->port_min ||
840 port->targ_port >= softc->port_max ||
841 softc->ha_link != CTL_HA_LINK_ONLINE)
843 mtx_lock(&softc->ctl_lock);
844 i = sizeof(msg->iid);
846 if (port->wwpn_iid[iid].name)
847 l = strlen(port->wwpn_iid[iid].name) + 1;
849 msg = malloc(i, M_CTL, M_NOWAIT);
851 mtx_unlock(&softc->ctl_lock);
854 bzero(&msg->iid, sizeof(msg->iid));
855 msg->hdr.msg_type = CTL_MSG_IID_SYNC;
856 msg->hdr.nexus.targ_port = port->targ_port;
857 msg->hdr.nexus.initid = iid;
858 msg->iid.in_use = port->wwpn_iid[iid].in_use;
859 msg->iid.name_len = l;
860 msg->iid.wwpn = port->wwpn_iid[iid].wwpn;
861 if (port->wwpn_iid[iid].name)
862 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l);
863 mtx_unlock(&softc->ctl_lock);
864 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT);
869 ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx,
870 uint8_t page, uint8_t subpage)
872 struct ctl_softc *softc = lun->ctl_softc;
873 union ctl_ha_msg msg;
876 if (softc->ha_link != CTL_HA_LINK_ONLINE)
878 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
879 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
880 page && lun->mode_pages.index[i].subpage == subpage)
883 if (i == CTL_NUM_MODE_PAGES)
885 bzero(&msg.mode, sizeof(msg.mode));
886 msg.hdr.msg_type = CTL_MSG_MODE_SYNC;
887 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT;
888 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT;
889 msg.hdr.nexus.targ_lun = lun->lun;
890 msg.hdr.nexus.targ_mapped_lun = lun->lun;
891 msg.mode.page_code = page;
892 msg.mode.subpage = subpage;
893 msg.mode.page_len = lun->mode_pages.index[i].page_len;
894 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data,
896 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode),
901 ctl_isc_ha_link_up(struct ctl_softc *softc)
903 struct ctl_port *port;
905 union ctl_ha_msg msg;
908 /* Announce this node parameters to peer for validation. */
909 msg.login.msg_type = CTL_MSG_LOGIN;
910 msg.login.version = CTL_HA_VERSION;
911 msg.login.ha_mode = softc->ha_mode;
912 msg.login.ha_id = softc->ha_id;
913 msg.login.max_luns = CTL_MAX_LUNS;
914 msg.login.max_ports = CTL_MAX_PORTS;
915 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT;
916 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login),
919 STAILQ_FOREACH(port, &softc->port_list, links) {
920 ctl_isc_announce_port(port);
921 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
922 if (port->wwpn_iid[i].in_use)
923 ctl_isc_announce_iid(port, i);
926 STAILQ_FOREACH(lun, &softc->lun_list, links)
927 ctl_isc_announce_lun(lun);
931 ctl_isc_ha_link_down(struct ctl_softc *softc)
933 struct ctl_port *port;
938 mtx_lock(&softc->ctl_lock);
939 STAILQ_FOREACH(lun, &softc->lun_list, links) {
940 mtx_lock(&lun->lun_lock);
941 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) {
942 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
943 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
945 mtx_unlock(&lun->lun_lock);
947 mtx_unlock(&softc->ctl_lock);
948 io = ctl_alloc_io(softc->othersc_pool);
949 mtx_lock(&softc->ctl_lock);
951 io->io_hdr.msg_type = CTL_MSG_FAILOVER;
952 io->io_hdr.nexus.targ_mapped_lun = lun->lun;
956 STAILQ_FOREACH(port, &softc->port_list, links) {
957 if (port->targ_port >= softc->port_min &&
958 port->targ_port < softc->port_max)
960 port->status &= ~CTL_PORT_STATUS_ONLINE;
961 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
962 port->wwpn_iid[i].in_use = 0;
963 free(port->wwpn_iid[i].name, M_CTL);
964 port->wwpn_iid[i].name = NULL;
967 mtx_unlock(&softc->ctl_lock);
971 ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
974 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus);
976 mtx_lock(&softc->ctl_lock);
977 if (msg->hdr.nexus.targ_lun < CTL_MAX_LUNS &&
978 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) != NULL) {
979 mtx_lock(&lun->lun_lock);
980 mtx_unlock(&softc->ctl_lock);
981 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES &&
983 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8);
984 if (msg->ua.ua_all) {
986 ctl_est_ua_all(lun, iid, msg->ua.ua_type);
988 ctl_clr_ua_all(lun, iid, msg->ua.ua_type);
991 ctl_est_ua(lun, iid, msg->ua.ua_type);
993 ctl_clr_ua(lun, iid, msg->ua.ua_type);
995 mtx_unlock(&lun->lun_lock);
997 mtx_unlock(&softc->ctl_lock);
1001 ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1003 struct ctl_lun *lun;
1004 struct ctl_ha_msg_lun_pr_key pr_key;
1006 ctl_lun_flags oflags;
1009 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1010 mtx_lock(&softc->ctl_lock);
1011 if ((targ_lun >= CTL_MAX_LUNS) ||
1012 ((lun = softc->ctl_luns[targ_lun]) == NULL)) {
1013 mtx_unlock(&softc->ctl_lock);
1016 mtx_lock(&lun->lun_lock);
1017 mtx_unlock(&softc->ctl_lock);
1018 if (lun->flags & CTL_LUN_DISABLED) {
1019 mtx_unlock(&lun->lun_lock);
1022 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0;
1023 if (msg->lun.lun_devid_len != i || (i > 0 &&
1024 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) {
1025 mtx_unlock(&lun->lun_lock);
1026 printf("%s: Received conflicting HA LUN %d\n",
1027 __func__, msg->hdr.nexus.targ_lun);
1030 /* Record whether peer is primary. */
1031 oflags = lun->flags;
1032 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) &&
1033 (msg->lun.flags & CTL_LUN_DISABLED) == 0)
1034 lun->flags |= CTL_LUN_PEER_SC_PRIMARY;
1036 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1037 if (oflags != lun->flags)
1038 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1040 /* If peer is primary and we are not -- use data */
1041 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
1042 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) {
1043 lun->PRGeneration = msg->lun.pr_generation;
1044 lun->pr_res_idx = msg->lun.pr_res_idx;
1045 lun->res_type = msg->lun.pr_res_type;
1046 lun->pr_key_count = msg->lun.pr_key_count;
1047 for (k = 0; k < CTL_MAX_INITIATORS; k++)
1048 ctl_clr_prkey(lun, k);
1049 for (k = 0; k < msg->lun.pr_key_count; k++) {
1050 memcpy(&pr_key, &msg->lun.data[i],
1052 ctl_alloc_prkey(lun, pr_key.pr_iid);
1053 ctl_set_prkey(lun, pr_key.pr_iid,
1055 i += sizeof(pr_key);
1059 mtx_unlock(&lun->lun_lock);
1060 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n",
1061 __func__, msg->hdr.nexus.targ_lun,
1062 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ?
1063 "primary" : "secondary"));
1065 /* If we are primary but peer doesn't know -- notify */
1066 if ((lun->flags & CTL_LUN_PRIMARY_SC) &&
1067 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0)
1068 ctl_isc_announce_lun(lun);
1073 ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1075 struct ctl_port *port;
1076 struct ctl_lun *lun;
1079 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1081 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__,
1082 msg->hdr.nexus.targ_port));
1084 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO);
1085 port->frontend = &ha_frontend;
1086 port->targ_port = msg->hdr.nexus.targ_port;
1087 port->fe_datamove = ctl_ha_datamove;
1088 port->fe_done = ctl_ha_done;
1089 } else if (port->frontend == &ha_frontend) {
1090 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__,
1091 msg->hdr.nexus.targ_port));
1094 printf("%s: Received conflicting HA port %d\n",
1095 __func__, msg->hdr.nexus.targ_port);
1098 port->port_type = msg->port.port_type;
1099 port->physical_port = msg->port.physical_port;
1100 port->virtual_port = msg->port.virtual_port;
1101 port->status = msg->port.status;
1103 free(port->port_name, M_CTL);
1104 port->port_name = strndup(&msg->port.data[i], msg->port.name_len,
1106 i += msg->port.name_len;
1107 if (msg->port.lun_map_len != 0) {
1108 if (port->lun_map == NULL)
1109 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS,
1111 memcpy(port->lun_map, &msg->port.data[i],
1112 sizeof(uint32_t) * CTL_MAX_LUNS);
1113 i += msg->port.lun_map_len;
1115 free(port->lun_map, M_CTL);
1116 port->lun_map = NULL;
1118 if (msg->port.port_devid_len != 0) {
1119 if (port->port_devid == NULL ||
1120 port->port_devid->len != msg->port.port_devid_len) {
1121 free(port->port_devid, M_CTL);
1122 port->port_devid = malloc(sizeof(struct ctl_devid) +
1123 msg->port.port_devid_len, M_CTL, M_WAITOK);
1125 memcpy(port->port_devid->data, &msg->port.data[i],
1126 msg->port.port_devid_len);
1127 port->port_devid->len = msg->port.port_devid_len;
1128 i += msg->port.port_devid_len;
1130 free(port->port_devid, M_CTL);
1131 port->port_devid = NULL;
1133 if (msg->port.target_devid_len != 0) {
1134 if (port->target_devid == NULL ||
1135 port->target_devid->len != msg->port.target_devid_len) {
1136 free(port->target_devid, M_CTL);
1137 port->target_devid = malloc(sizeof(struct ctl_devid) +
1138 msg->port.target_devid_len, M_CTL, M_WAITOK);
1140 memcpy(port->target_devid->data, &msg->port.data[i],
1141 msg->port.target_devid_len);
1142 port->target_devid->len = msg->port.target_devid_len;
1143 i += msg->port.target_devid_len;
1145 free(port->target_devid, M_CTL);
1146 port->target_devid = NULL;
1148 if (msg->port.init_devid_len != 0) {
1149 if (port->init_devid == NULL ||
1150 port->init_devid->len != msg->port.init_devid_len) {
1151 free(port->init_devid, M_CTL);
1152 port->init_devid = malloc(sizeof(struct ctl_devid) +
1153 msg->port.init_devid_len, M_CTL, M_WAITOK);
1155 memcpy(port->init_devid->data, &msg->port.data[i],
1156 msg->port.init_devid_len);
1157 port->init_devid->len = msg->port.init_devid_len;
1158 i += msg->port.init_devid_len;
1160 free(port->init_devid, M_CTL);
1161 port->init_devid = NULL;
1164 if (ctl_port_register(port) != 0) {
1165 printf("%s: ctl_port_register() failed with error\n",
1169 mtx_lock(&softc->ctl_lock);
1170 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1171 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
1173 mtx_lock(&lun->lun_lock);
1174 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
1175 mtx_unlock(&lun->lun_lock);
1177 mtx_unlock(&softc->ctl_lock);
1181 ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1183 struct ctl_port *port;
1186 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1188 printf("%s: Received IID for unknown port %d\n",
1189 __func__, msg->hdr.nexus.targ_port);
1192 iid = msg->hdr.nexus.initid;
1193 port->wwpn_iid[iid].in_use = msg->iid.in_use;
1194 port->wwpn_iid[iid].wwpn = msg->iid.wwpn;
1195 free(port->wwpn_iid[iid].name, M_CTL);
1196 if (msg->iid.name_len) {
1197 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0],
1198 msg->iid.name_len, M_CTL);
1200 port->wwpn_iid[iid].name = NULL;
1204 ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1207 if (msg->login.version != CTL_HA_VERSION) {
1208 printf("CTL HA peers have different versions %d != %d\n",
1209 msg->login.version, CTL_HA_VERSION);
1210 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1213 if (msg->login.ha_mode != softc->ha_mode) {
1214 printf("CTL HA peers have different ha_mode %d != %d\n",
1215 msg->login.ha_mode, softc->ha_mode);
1216 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1219 if (msg->login.ha_id == softc->ha_id) {
1220 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id);
1221 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1224 if (msg->login.max_luns != CTL_MAX_LUNS ||
1225 msg->login.max_ports != CTL_MAX_PORTS ||
1226 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) {
1227 printf("CTL HA peers have different limits\n");
1228 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1234 ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1236 struct ctl_lun *lun;
1238 uint32_t initidx, targ_lun;
1240 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1241 mtx_lock(&softc->ctl_lock);
1242 if ((targ_lun >= CTL_MAX_LUNS) ||
1243 ((lun = softc->ctl_luns[targ_lun]) == NULL)) {
1244 mtx_unlock(&softc->ctl_lock);
1247 mtx_lock(&lun->lun_lock);
1248 mtx_unlock(&softc->ctl_lock);
1249 if (lun->flags & CTL_LUN_DISABLED) {
1250 mtx_unlock(&lun->lun_lock);
1253 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
1254 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
1255 msg->mode.page_code &&
1256 lun->mode_pages.index[i].subpage == msg->mode.subpage)
1259 if (i == CTL_NUM_MODE_PAGES) {
1260 mtx_unlock(&lun->lun_lock);
1263 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data,
1264 lun->mode_pages.index[i].page_len);
1265 initidx = ctl_get_initindex(&msg->hdr.nexus);
1267 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
1268 mtx_unlock(&lun->lun_lock);
1272 * ISC (Inter Shelf Communication) event handler. Events from the HA
1273 * subsystem come in here.
1276 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
1278 struct ctl_softc *softc = control_softc;
1280 struct ctl_prio *presio;
1281 ctl_ha_status isc_status;
1283 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event));
1284 if (event == CTL_HA_EVT_MSG_RECV) {
1285 union ctl_ha_msg *msg, msgbuf;
1287 if (param > sizeof(msgbuf))
1288 msg = malloc(param, M_CTL, M_WAITOK);
1291 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param,
1293 if (isc_status != CTL_HA_STATUS_SUCCESS) {
1294 printf("%s: Error receiving message: %d\n",
1295 __func__, isc_status);
1301 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type));
1302 switch (msg->hdr.msg_type) {
1303 case CTL_MSG_SERIALIZE:
1304 io = ctl_alloc_io(softc->othersc_pool);
1306 // populate ctsio from msg
1307 io->io_hdr.io_type = CTL_IO_SCSI;
1308 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
1309 io->io_hdr.original_sc = msg->hdr.original_sc;
1310 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
1313 * If we're in serialization-only mode, we don't
1314 * want to go through full done processing. Thus
1317 * XXX KDM add another flag that is more specific.
1319 if (softc->ha_mode != CTL_HA_MODE_XFER)
1320 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
1321 io->io_hdr.nexus = msg->hdr.nexus;
1323 printf("port %u, iid %u, lun %u\n",
1324 io->io_hdr.nexus.targ_port,
1325 io->io_hdr.nexus.initid,
1326 io->io_hdr.nexus.targ_lun);
1328 io->scsiio.tag_num = msg->scsi.tag_num;
1329 io->scsiio.tag_type = msg->scsi.tag_type;
1331 io->io_hdr.start_time = time_uptime;
1332 getbinuptime(&io->io_hdr.start_bt);
1333 #endif /* CTL_TIME_IO */
1334 io->scsiio.cdb_len = msg->scsi.cdb_len;
1335 memcpy(io->scsiio.cdb, msg->scsi.cdb,
1337 if (softc->ha_mode == CTL_HA_MODE_XFER) {
1338 const struct ctl_cmd_entry *entry;
1340 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
1341 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
1343 entry->flags & CTL_FLAG_DATA_MASK;
1345 ctl_enqueue_isc(io);
1348 /* Performed on the Originating SC, XFER mode only */
1349 case CTL_MSG_DATAMOVE: {
1350 struct ctl_sg_entry *sgl;
1353 io = msg->hdr.original_sc;
1355 printf("%s: original_sc == NULL!\n", __func__);
1356 /* XXX KDM do something here */
1359 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
1360 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1362 * Keep track of this, we need to send it back over
1363 * when the datamove is complete.
1365 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1366 if (msg->hdr.status == CTL_SUCCESS)
1367 io->io_hdr.status = msg->hdr.status;
1369 if (msg->dt.sg_sequence == 0) {
1371 getbinuptime(&io->io_hdr.dma_start_bt);
1373 i = msg->dt.kern_sg_entries +
1374 msg->dt.kern_data_len /
1375 CTL_HA_DATAMOVE_SEGMENT + 1;
1376 sgl = malloc(sizeof(*sgl) * i, M_CTL,
1378 io->io_hdr.remote_sglist = sgl;
1379 io->io_hdr.local_sglist =
1380 &sgl[msg->dt.kern_sg_entries];
1382 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
1384 io->scsiio.kern_sg_entries =
1385 msg->dt.kern_sg_entries;
1386 io->scsiio.rem_sg_entries =
1387 msg->dt.kern_sg_entries;
1388 io->scsiio.kern_data_len =
1389 msg->dt.kern_data_len;
1390 io->scsiio.kern_total_len =
1391 msg->dt.kern_total_len;
1392 io->scsiio.kern_data_resid =
1393 msg->dt.kern_data_resid;
1394 io->scsiio.kern_rel_offset =
1395 msg->dt.kern_rel_offset;
1396 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR;
1397 io->io_hdr.flags |= msg->dt.flags &
1400 sgl = (struct ctl_sg_entry *)
1401 io->scsiio.kern_data_ptr;
1403 for (i = msg->dt.sent_sg_entries, j = 0;
1404 i < (msg->dt.sent_sg_entries +
1405 msg->dt.cur_sg_entries); i++, j++) {
1406 sgl[i].addr = msg->dt.sg_list[j].addr;
1407 sgl[i].len = msg->dt.sg_list[j].len;
1410 printf("%s: DATAMOVE: %p,%lu j=%d, i=%d\n",
1411 __func__, sgl[i].addr, sgl[i].len, j, i);
1416 * If this is the last piece of the I/O, we've got
1417 * the full S/G list. Queue processing in the thread.
1418 * Otherwise wait for the next piece.
1420 if (msg->dt.sg_last != 0)
1421 ctl_enqueue_isc(io);
1424 /* Performed on the Serializing (primary) SC, XFER mode only */
1425 case CTL_MSG_DATAMOVE_DONE: {
1426 if (msg->hdr.serializing_sc == NULL) {
1427 printf("%s: serializing_sc == NULL!\n",
1429 /* XXX KDM now what? */
1433 * We grab the sense information here in case
1434 * there was a failure, so we can return status
1435 * back to the initiator.
1437 io = msg->hdr.serializing_sc;
1438 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
1439 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1440 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1441 io->io_hdr.port_status = msg->scsi.fetd_status;
1442 io->scsiio.residual = msg->scsi.residual;
1443 if (msg->hdr.status != CTL_STATUS_NONE) {
1444 io->io_hdr.status = msg->hdr.status;
1445 io->scsiio.scsi_status = msg->scsi.scsi_status;
1446 io->scsiio.sense_len = msg->scsi.sense_len;
1447 io->scsiio.sense_residual =msg->scsi.sense_residual;
1448 memcpy(&io->scsiio.sense_data,
1449 &msg->scsi.sense_data,
1450 msg->scsi.sense_len);
1451 if (msg->hdr.status == CTL_SUCCESS)
1452 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1454 ctl_enqueue_isc(io);
1458 /* Preformed on Originating SC, SER_ONLY mode */
1460 io = msg->hdr.original_sc;
1462 printf("%s: original_sc == NULL!\n",
1466 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1467 io->io_hdr.msg_type = CTL_MSG_R2R;
1468 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1469 ctl_enqueue_isc(io);
1473 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
1475 * Performed on the Originating (i.e. secondary) SC in XFER
1478 case CTL_MSG_FINISH_IO:
1479 if (softc->ha_mode == CTL_HA_MODE_XFER)
1480 ctl_isc_handler_finish_xfer(softc, msg);
1482 ctl_isc_handler_finish_ser_only(softc, msg);
1485 /* Preformed on Originating SC */
1486 case CTL_MSG_BAD_JUJU:
1487 io = msg->hdr.original_sc;
1489 printf("%s: Bad JUJU!, original_sc is NULL!\n",
1493 ctl_copy_sense_data(msg, io);
1495 * IO should have already been cleaned up on other
1496 * SC so clear this flag so we won't send a message
1497 * back to finish the IO there.
1499 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
1500 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1502 /* io = msg->hdr.serializing_sc; */
1503 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
1504 ctl_enqueue_isc(io);
1507 /* Handle resets sent from the other side */
1508 case CTL_MSG_MANAGE_TASKS: {
1509 struct ctl_taskio *taskio;
1510 taskio = (struct ctl_taskio *)ctl_alloc_io(
1511 softc->othersc_pool);
1512 ctl_zero_io((union ctl_io *)taskio);
1513 taskio->io_hdr.io_type = CTL_IO_TASK;
1514 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1515 taskio->io_hdr.nexus = msg->hdr.nexus;
1516 taskio->task_action = msg->task.task_action;
1517 taskio->tag_num = msg->task.tag_num;
1518 taskio->tag_type = msg->task.tag_type;
1520 taskio->io_hdr.start_time = time_uptime;
1521 getbinuptime(&taskio->io_hdr.start_bt);
1522 #endif /* CTL_TIME_IO */
1523 ctl_run_task((union ctl_io *)taskio);
1526 /* Persistent Reserve action which needs attention */
1527 case CTL_MSG_PERS_ACTION:
1528 presio = (struct ctl_prio *)ctl_alloc_io(
1529 softc->othersc_pool);
1530 ctl_zero_io((union ctl_io *)presio);
1531 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
1532 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1533 presio->io_hdr.nexus = msg->hdr.nexus;
1534 presio->pr_msg = msg->pr;
1535 ctl_enqueue_isc((union ctl_io *)presio);
1538 ctl_isc_ua(softc, msg, param);
1540 case CTL_MSG_PORT_SYNC:
1541 ctl_isc_port_sync(softc, msg, param);
1543 case CTL_MSG_LUN_SYNC:
1544 ctl_isc_lun_sync(softc, msg, param);
1546 case CTL_MSG_IID_SYNC:
1547 ctl_isc_iid_sync(softc, msg, param);
1550 ctl_isc_login(softc, msg, param);
1552 case CTL_MSG_MODE_SYNC:
1553 ctl_isc_mode_sync(softc, msg, param);
1556 printf("Received HA message of unknown type %d\n",
1558 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1563 } else if (event == CTL_HA_EVT_LINK_CHANGE) {
1564 printf("CTL: HA link status changed from %d to %d\n",
1565 softc->ha_link, param);
1566 if (param == softc->ha_link)
1568 if (softc->ha_link == CTL_HA_LINK_ONLINE) {
1569 softc->ha_link = param;
1570 ctl_isc_ha_link_down(softc);
1572 softc->ha_link = param;
1573 if (softc->ha_link == CTL_HA_LINK_ONLINE)
1574 ctl_isc_ha_link_up(softc);
1578 printf("ctl_isc_event_handler: Unknown event %d\n", event);
1584 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
1587 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data,
1588 src->scsi.sense_len);
1589 dest->scsiio.scsi_status = src->scsi.scsi_status;
1590 dest->scsiio.sense_len = src->scsi.sense_len;
1591 dest->io_hdr.status = src->hdr.status;
1595 ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest)
1598 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data,
1599 src->scsiio.sense_len);
1600 dest->scsi.scsi_status = src->scsiio.scsi_status;
1601 dest->scsi.sense_len = src->scsiio.sense_len;
1602 dest->hdr.status = src->io_hdr.status;
1606 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1608 struct ctl_softc *softc = lun->ctl_softc;
1611 if (initidx < softc->init_min || initidx >= softc->init_max)
1613 mtx_assert(&lun->lun_lock, MA_OWNED);
1614 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1617 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua;
1621 ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua)
1625 mtx_assert(&lun->lun_lock, MA_OWNED);
1626 if (lun->pending_ua[port] == NULL)
1628 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1629 if (port * CTL_MAX_INIT_PER_PORT + i == except)
1631 lun->pending_ua[port][i] |= ua;
1636 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1638 struct ctl_softc *softc = lun->ctl_softc;
1641 mtx_assert(&lun->lun_lock, MA_OWNED);
1642 for (i = softc->port_min; i < softc->port_max; i++)
1643 ctl_est_ua_port(lun, i, except, ua);
1647 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1649 struct ctl_softc *softc = lun->ctl_softc;
1652 if (initidx < softc->init_min || initidx >= softc->init_max)
1654 mtx_assert(&lun->lun_lock, MA_OWNED);
1655 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1658 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua;
1662 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1664 struct ctl_softc *softc = lun->ctl_softc;
1667 mtx_assert(&lun->lun_lock, MA_OWNED);
1668 for (i = softc->port_min; i < softc->port_max; i++) {
1669 if (lun->pending_ua[i] == NULL)
1671 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
1672 if (i * CTL_MAX_INIT_PER_PORT + j == except)
1674 lun->pending_ua[i][j] &= ~ua;
1680 ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx,
1681 ctl_ua_type ua_type)
1683 struct ctl_lun *lun;
1685 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
1686 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) {
1687 mtx_lock(&lun->lun_lock);
1688 ctl_clr_ua(lun, initidx, ua_type);
1689 mtx_unlock(&lun->lun_lock);
1694 ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS)
1696 struct ctl_softc *softc = (struct ctl_softc *)arg1;
1697 struct ctl_lun *lun;
1698 struct ctl_lun_req ireq;
1701 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1;
1702 error = sysctl_handle_int(oidp, &value, 0, req);
1703 if ((error != 0) || (req->newptr == NULL))
1706 mtx_lock(&softc->ctl_lock);
1708 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1710 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
1711 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1712 mtx_unlock(&softc->ctl_lock);
1713 bzero(&ireq, sizeof(ireq));
1714 ireq.reqtype = CTL_LUNREQ_MODIFY;
1715 ireq.reqdata.modify.lun_id = lun->lun;
1716 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0,
1718 if (ireq.status != CTL_LUN_OK) {
1719 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n",
1720 __func__, ireq.status, ireq.error_str);
1722 mtx_lock(&softc->ctl_lock);
1724 mtx_unlock(&softc->ctl_lock);
1731 struct ctl_softc *softc;
1735 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1738 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
1740 softc->dev->si_drv1 = softc;
1742 sysctl_ctx_init(&softc->sysctl_ctx);
1743 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1744 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1745 CTLFLAG_RD, 0, "CAM Target Layer");
1747 if (softc->sysctl_tree == NULL) {
1748 printf("%s: unable to allocate sysctl tree\n", __func__);
1749 destroy_dev(softc->dev);
1750 free(control_softc, M_DEVBUF);
1751 control_softc = NULL;
1755 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1756 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
1757 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1760 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1761 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0,
1762 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)");
1765 * In Copan's HA scheme, the "master" and "slave" roles are
1766 * figured out through the slot the controller is in. Although it
1767 * is an active/active system, someone has to be in charge.
1769 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1770 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1771 "HA head ID (0 - no HA)");
1772 if (softc->ha_id == 0 || softc->ha_id > NUM_TARGET_PORT_GROUPS) {
1773 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1774 softc->is_single = 1;
1775 softc->port_cnt = CTL_MAX_PORTS;
1776 softc->port_min = 0;
1778 softc->port_cnt = CTL_MAX_PORTS / NUM_TARGET_PORT_GROUPS;
1779 softc->port_min = (softc->ha_id - 1) * softc->port_cnt;
1781 softc->port_max = softc->port_min + softc->port_cnt;
1782 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT;
1783 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT;
1785 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1786 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0,
1787 "HA link state (0 - offline, 1 - unknown, 2 - online)");
1789 STAILQ_INIT(&softc->lun_list);
1790 STAILQ_INIT(&softc->pending_lun_queue);
1791 STAILQ_INIT(&softc->fe_list);
1792 STAILQ_INIT(&softc->port_list);
1793 STAILQ_INIT(&softc->be_list);
1794 ctl_tpc_init(softc);
1796 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
1799 printf("ctl: can't allocate %d entry other SC pool, "
1800 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1803 softc->othersc_pool = other_pool;
1805 if (worker_threads <= 0)
1806 worker_threads = max(1, mp_ncpus / 4);
1807 if (worker_threads > CTL_MAX_THREADS)
1808 worker_threads = CTL_MAX_THREADS;
1810 for (i = 0; i < worker_threads; i++) {
1811 struct ctl_thread *thr = &softc->threads[i];
1813 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1814 thr->ctl_softc = softc;
1815 STAILQ_INIT(&thr->incoming_queue);
1816 STAILQ_INIT(&thr->rtr_queue);
1817 STAILQ_INIT(&thr->done_queue);
1818 STAILQ_INIT(&thr->isc_queue);
1820 error = kproc_kthread_add(ctl_work_thread, thr,
1821 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1823 printf("error creating CTL work thread!\n");
1824 ctl_pool_free(other_pool);
1828 error = kproc_kthread_add(ctl_lun_thread, softc,
1829 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
1831 printf("error creating CTL lun thread!\n");
1832 ctl_pool_free(other_pool);
1835 error = kproc_kthread_add(ctl_thresh_thread, softc,
1836 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh");
1838 printf("error creating CTL threshold thread!\n");
1839 ctl_pool_free(other_pool);
1843 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1844 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN,
1845 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head");
1847 if (softc->is_single == 0) {
1848 ctl_frontend_register(&ha_frontend);
1849 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) {
1850 printf("ctl_init: ctl_ha_msg_init failed.\n");
1851 softc->is_single = 1;
1853 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
1854 != CTL_HA_STATUS_SUCCESS) {
1855 printf("ctl_init: ctl_ha_msg_register failed.\n");
1856 softc->is_single = 1;
1865 struct ctl_softc *softc = control_softc;
1866 struct ctl_lun *lun, *next_lun;
1868 if (softc->is_single == 0) {
1869 ctl_ha_msg_shutdown(softc);
1870 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL)
1871 != CTL_HA_STATUS_SUCCESS)
1872 printf("%s: ctl_ha_msg_deregister failed.\n", __func__);
1873 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS)
1874 printf("%s: ctl_ha_msg_destroy failed.\n", __func__);
1875 ctl_frontend_deregister(&ha_frontend);
1878 mtx_lock(&softc->ctl_lock);
1883 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
1884 next_lun = STAILQ_NEXT(lun, links);
1888 mtx_unlock(&softc->ctl_lock);
1891 ctl_shutdown_thread(softc->work_thread);
1892 mtx_destroy(&softc->queue_lock);
1895 ctl_tpc_shutdown(softc);
1896 uma_zdestroy(softc->io_zone);
1897 mtx_destroy(&softc->ctl_lock);
1899 destroy_dev(softc->dev);
1901 sysctl_ctx_free(&softc->sysctl_ctx);
1903 free(control_softc, M_DEVBUF);
1904 control_softc = NULL;
1908 ctl_module_event_handler(module_t mod, int what, void *arg)
1913 return (ctl_init());
1917 return (EOPNOTSUPP);
1922 * XXX KDM should we do some access checks here? Bump a reference count to
1923 * prevent a CTL module from being unloaded while someone has it open?
1926 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1932 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1938 * Remove an initiator by port number and initiator ID.
1939 * Returns 0 for success, -1 for failure.
1942 ctl_remove_initiator(struct ctl_port *port, int iid)
1944 struct ctl_softc *softc = port->ctl_softc;
1946 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1948 if (iid > CTL_MAX_INIT_PER_PORT) {
1949 printf("%s: initiator ID %u > maximun %u!\n",
1950 __func__, iid, CTL_MAX_INIT_PER_PORT);
1954 mtx_lock(&softc->ctl_lock);
1955 port->wwpn_iid[iid].in_use--;
1956 port->wwpn_iid[iid].last_use = time_uptime;
1957 mtx_unlock(&softc->ctl_lock);
1958 ctl_isc_announce_iid(port, iid);
1964 * Add an initiator to the initiator map.
1965 * Returns iid for success, < 0 for failure.
1968 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
1970 struct ctl_softc *softc = port->ctl_softc;
1974 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1976 if (iid >= CTL_MAX_INIT_PER_PORT) {
1977 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
1978 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
1983 mtx_lock(&softc->ctl_lock);
1985 if (iid < 0 && (wwpn != 0 || name != NULL)) {
1986 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1987 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
1991 if (name != NULL && port->wwpn_iid[i].name != NULL &&
1992 strcmp(name, port->wwpn_iid[i].name) == 0) {
2000 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2001 if (port->wwpn_iid[i].in_use == 0 &&
2002 port->wwpn_iid[i].wwpn == 0 &&
2003 port->wwpn_iid[i].name == NULL) {
2012 best_time = INT32_MAX;
2013 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2014 if (port->wwpn_iid[i].in_use == 0) {
2015 if (port->wwpn_iid[i].last_use < best_time) {
2017 best_time = port->wwpn_iid[i].last_use;
2025 mtx_unlock(&softc->ctl_lock);
2030 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
2032 * This is not an error yet.
2034 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
2036 printf("%s: port %d iid %u WWPN %#jx arrived"
2037 " again\n", __func__, port->targ_port,
2038 iid, (uintmax_t)wwpn);
2042 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
2043 strcmp(name, port->wwpn_iid[iid].name) == 0) {
2045 printf("%s: port %d iid %u name '%s' arrived"
2046 " again\n", __func__, port->targ_port,
2053 * This is an error, but what do we do about it? The
2054 * driver is telling us we have a new WWPN for this
2055 * initiator ID, so we pretty much need to use it.
2057 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
2058 " but WWPN %#jx '%s' is still at that address\n",
2059 __func__, port->targ_port, iid, wwpn, name,
2060 (uintmax_t)port->wwpn_iid[iid].wwpn,
2061 port->wwpn_iid[iid].name);
2064 * XXX KDM clear have_ca and ua_pending on each LUN for
2069 free(port->wwpn_iid[iid].name, M_CTL);
2070 port->wwpn_iid[iid].name = name;
2071 port->wwpn_iid[iid].wwpn = wwpn;
2072 port->wwpn_iid[iid].in_use++;
2073 mtx_unlock(&softc->ctl_lock);
2074 ctl_isc_announce_iid(port, iid);
2080 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
2084 switch (port->port_type) {
2087 struct scsi_transportid_fcp *id =
2088 (struct scsi_transportid_fcp *)buf;
2089 if (port->wwpn_iid[iid].wwpn == 0)
2091 memset(id, 0, sizeof(*id));
2092 id->format_protocol = SCSI_PROTO_FC;
2093 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
2094 return (sizeof(*id));
2096 case CTL_PORT_ISCSI:
2098 struct scsi_transportid_iscsi_port *id =
2099 (struct scsi_transportid_iscsi_port *)buf;
2100 if (port->wwpn_iid[iid].name == NULL)
2103 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
2105 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
2106 len = roundup2(min(len, 252), 4);
2107 scsi_ulto2b(len, id->additional_length);
2108 return (sizeof(*id) + len);
2112 struct scsi_transportid_sas *id =
2113 (struct scsi_transportid_sas *)buf;
2114 if (port->wwpn_iid[iid].wwpn == 0)
2116 memset(id, 0, sizeof(*id));
2117 id->format_protocol = SCSI_PROTO_SAS;
2118 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
2119 return (sizeof(*id));
2123 struct scsi_transportid_spi *id =
2124 (struct scsi_transportid_spi *)buf;
2125 memset(id, 0, sizeof(*id));
2126 id->format_protocol = SCSI_PROTO_SPI;
2127 scsi_ulto2b(iid, id->scsi_addr);
2128 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
2129 return (sizeof(*id));
2135 * Serialize a command that went down the "wrong" side, and so was sent to
2136 * this controller for execution. The logic is a little different than the
2137 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
2138 * sent back to the other side, but in the success case, we execute the
2139 * command on this side (XFER mode) or tell the other side to execute it
2143 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
2145 struct ctl_softc *softc = control_softc;
2146 union ctl_ha_msg msg_info;
2147 struct ctl_port *port;
2148 struct ctl_lun *lun;
2149 const struct ctl_cmd_entry *entry;
2153 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
2154 mtx_lock(&softc->ctl_lock);
2156 /* Make sure that we know about this port. */
2157 port = ctl_io_port(&ctsio->io_hdr);
2158 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) {
2159 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2164 /* Make sure that we know about this LUN. */
2165 if ((targ_lun < CTL_MAX_LUNS) &&
2166 ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
2167 mtx_lock(&lun->lun_lock);
2168 mtx_unlock(&softc->ctl_lock);
2170 * If the LUN is invalid, pretend that it doesn't exist.
2171 * It will go away as soon as all pending I/O has been
2174 if (lun->flags & CTL_LUN_DISABLED) {
2175 mtx_unlock(&lun->lun_lock);
2179 mtx_unlock(&softc->ctl_lock);
2184 * The other node would not send this request to us unless
2185 * received announce that we are primary node for this LUN.
2186 * If this LUN does not exist now, it is probably result of
2187 * a race, so respond to initiator in the most opaque way.
2189 ctl_set_busy(ctsio);
2193 entry = ctl_get_cmd_entry(ctsio, NULL);
2194 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
2195 mtx_unlock(&lun->lun_lock);
2199 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
2200 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = lun->be_lun;
2203 * Every I/O goes into the OOA queue for a
2204 * particular LUN, and stays there until completion.
2207 if (TAILQ_EMPTY(&lun->ooa_queue))
2208 lun->idle_time += getsbinuptime() - lun->last_busy;
2210 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2212 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
2213 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
2215 case CTL_ACTION_BLOCK:
2216 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
2217 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
2219 mtx_unlock(&lun->lun_lock);
2221 case CTL_ACTION_PASS:
2222 case CTL_ACTION_SKIP:
2223 if (softc->ha_mode == CTL_HA_MODE_XFER) {
2224 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
2225 ctl_enqueue_rtr((union ctl_io *)ctsio);
2226 mtx_unlock(&lun->lun_lock);
2228 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
2229 mtx_unlock(&lun->lun_lock);
2231 /* send msg back to other side */
2232 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2233 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
2234 msg_info.hdr.msg_type = CTL_MSG_R2R;
2235 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2236 sizeof(msg_info.hdr), M_WAITOK);
2239 case CTL_ACTION_OVERLAP:
2240 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2241 mtx_unlock(&lun->lun_lock);
2242 ctl_set_overlapped_cmd(ctsio);
2244 case CTL_ACTION_OVERLAP_TAG:
2245 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2246 mtx_unlock(&lun->lun_lock);
2247 ctl_set_overlapped_tag(ctsio, ctsio->tag_num);
2249 case CTL_ACTION_ERROR:
2251 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2252 mtx_unlock(&lun->lun_lock);
2254 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2257 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
2258 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2259 msg_info.hdr.serializing_sc = NULL;
2260 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
2261 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2262 sizeof(msg_info.scsi), M_WAITOK);
2270 * Returns 0 for success, errno for failure.
2273 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2274 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2278 mtx_lock(&lun->lun_lock);
2279 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2280 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2282 struct ctl_ooa_entry *entry;
2285 * If we've got more than we can fit, just count the
2286 * remaining entries.
2288 if (*cur_fill_num >= ooa_hdr->alloc_num)
2291 entry = &kern_entries[*cur_fill_num];
2293 entry->tag_num = io->scsiio.tag_num;
2294 entry->lun_num = lun->lun;
2296 entry->start_bt = io->io_hdr.start_bt;
2298 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2299 entry->cdb_len = io->scsiio.cdb_len;
2300 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2301 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2303 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2304 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2306 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2307 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2309 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2310 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2312 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2313 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2315 mtx_unlock(&lun->lun_lock);
2319 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2320 size_t error_str_len)
2324 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2326 if (copyin(user_addr, kptr, len) != 0) {
2327 snprintf(error_str, error_str_len, "Error copying %d bytes "
2328 "from user address %p to kernel address %p", len,
2338 ctl_free_args(int num_args, struct ctl_be_arg *args)
2345 for (i = 0; i < num_args; i++) {
2346 free(args[i].kname, M_CTL);
2347 free(args[i].kvalue, M_CTL);
2353 static struct ctl_be_arg *
2354 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
2355 char *error_str, size_t error_str_len)
2357 struct ctl_be_arg *args;
2360 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
2361 error_str, error_str_len);
2366 for (i = 0; i < num_args; i++) {
2367 args[i].kname = NULL;
2368 args[i].kvalue = NULL;
2371 for (i = 0; i < num_args; i++) {
2374 args[i].kname = ctl_copyin_alloc(args[i].name,
2375 args[i].namelen, error_str, error_str_len);
2376 if (args[i].kname == NULL)
2379 if (args[i].kname[args[i].namelen - 1] != '\0') {
2380 snprintf(error_str, error_str_len, "Argument %d "
2381 "name is not NUL-terminated", i);
2385 if (args[i].flags & CTL_BEARG_RD) {
2386 tmpptr = ctl_copyin_alloc(args[i].value,
2387 args[i].vallen, error_str, error_str_len);
2390 if ((args[i].flags & CTL_BEARG_ASCII)
2391 && (tmpptr[args[i].vallen - 1] != '\0')) {
2392 snprintf(error_str, error_str_len, "Argument "
2393 "%d value is not NUL-terminated", i);
2396 args[i].kvalue = tmpptr;
2398 args[i].kvalue = malloc(args[i].vallen,
2399 M_CTL, M_WAITOK | M_ZERO);
2406 ctl_free_args(num_args, args);
2412 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2416 for (i = 0; i < num_args; i++) {
2417 if (args[i].flags & CTL_BEARG_WR)
2418 copyout(args[i].kvalue, args[i].value, args[i].vallen);
2423 * Escape characters that are illegal or not recommended in XML.
2426 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2428 char *end = str + size;
2433 for (; *str && str < end; str++) {
2436 retval = sbuf_printf(sb, "&");
2439 retval = sbuf_printf(sb, ">");
2442 retval = sbuf_printf(sb, "<");
2445 retval = sbuf_putc(sb, *str);
2458 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2460 struct scsi_vpd_id_descriptor *desc;
2463 if (id == NULL || id->len < 4)
2465 desc = (struct scsi_vpd_id_descriptor *)id->data;
2466 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2467 case SVPD_ID_TYPE_T10:
2468 sbuf_printf(sb, "t10.");
2470 case SVPD_ID_TYPE_EUI64:
2471 sbuf_printf(sb, "eui.");
2473 case SVPD_ID_TYPE_NAA:
2474 sbuf_printf(sb, "naa.");
2476 case SVPD_ID_TYPE_SCSI_NAME:
2479 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2480 case SVPD_ID_CODESET_BINARY:
2481 for (i = 0; i < desc->length; i++)
2482 sbuf_printf(sb, "%02x", desc->identifier[i]);
2484 case SVPD_ID_CODESET_ASCII:
2485 sbuf_printf(sb, "%.*s", (int)desc->length,
2486 (char *)desc->identifier);
2488 case SVPD_ID_CODESET_UTF8:
2489 sbuf_printf(sb, "%s", (char *)desc->identifier);
2495 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2498 struct ctl_softc *softc = dev->si_drv1;
2499 struct ctl_lun *lun;
2506 retval = ctl_ioctl_io(dev, cmd, addr, flag, td);
2508 case CTL_ENABLE_PORT:
2509 case CTL_DISABLE_PORT:
2510 case CTL_SET_PORT_WWNS: {
2511 struct ctl_port *port;
2512 struct ctl_port_entry *entry;
2514 entry = (struct ctl_port_entry *)addr;
2516 mtx_lock(&softc->ctl_lock);
2517 STAILQ_FOREACH(port, &softc->port_list, links) {
2520 if (port->targ_port < softc->port_min ||
2521 port->targ_port >= softc->port_max)
2526 if ((entry->port_type == CTL_PORT_NONE)
2527 && (entry->targ_port == port->targ_port)) {
2529 * If the user only wants to enable or
2530 * disable or set WWNs on a specific port,
2531 * do the operation and we're done.
2535 } else if (entry->port_type & port->port_type) {
2537 * Compare the user's type mask with the
2538 * particular frontend type to see if we
2545 * Make sure the user isn't trying to set
2546 * WWNs on multiple ports at the same time.
2548 if (cmd == CTL_SET_PORT_WWNS) {
2549 printf("%s: Can't set WWNs on "
2550 "multiple ports\n", __func__);
2559 * XXX KDM we have to drop the lock here, because
2560 * the online/offline operations can potentially
2561 * block. We need to reference count the frontends
2562 * so they can't go away,
2564 if (cmd == CTL_ENABLE_PORT) {
2565 mtx_unlock(&softc->ctl_lock);
2566 ctl_port_online(port);
2567 mtx_lock(&softc->ctl_lock);
2568 } else if (cmd == CTL_DISABLE_PORT) {
2569 mtx_unlock(&softc->ctl_lock);
2570 ctl_port_offline(port);
2571 mtx_lock(&softc->ctl_lock);
2572 } else if (cmd == CTL_SET_PORT_WWNS) {
2573 ctl_port_set_wwns(port,
2574 (entry->flags & CTL_PORT_WWNN_VALID) ?
2576 (entry->flags & CTL_PORT_WWPN_VALID) ?
2577 1 : 0, entry->wwpn);
2582 mtx_unlock(&softc->ctl_lock);
2586 struct ctl_ooa *ooa_hdr;
2587 struct ctl_ooa_entry *entries;
2588 uint32_t cur_fill_num;
2590 ooa_hdr = (struct ctl_ooa *)addr;
2592 if ((ooa_hdr->alloc_len == 0)
2593 || (ooa_hdr->alloc_num == 0)) {
2594 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2595 "must be non-zero\n", __func__,
2596 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2601 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2602 sizeof(struct ctl_ooa_entry))) {
2603 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2604 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2605 __func__, ooa_hdr->alloc_len,
2606 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2611 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2612 if (entries == NULL) {
2613 printf("%s: could not allocate %d bytes for OOA "
2614 "dump\n", __func__, ooa_hdr->alloc_len);
2619 mtx_lock(&softc->ctl_lock);
2620 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2621 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS)
2622 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2623 mtx_unlock(&softc->ctl_lock);
2624 free(entries, M_CTL);
2625 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2626 __func__, (uintmax_t)ooa_hdr->lun_num);
2633 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2634 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2635 ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2639 lun = softc->ctl_luns[ooa_hdr->lun_num];
2640 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr,
2643 mtx_unlock(&softc->ctl_lock);
2645 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2646 ooa_hdr->fill_len = ooa_hdr->fill_num *
2647 sizeof(struct ctl_ooa_entry);
2648 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2650 printf("%s: error copying out %d bytes for OOA dump\n",
2651 __func__, ooa_hdr->fill_len);
2654 getbinuptime(&ooa_hdr->cur_bt);
2656 if (cur_fill_num > ooa_hdr->alloc_num) {
2657 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2658 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2660 ooa_hdr->dropped_num = 0;
2661 ooa_hdr->status = CTL_OOA_OK;
2664 free(entries, M_CTL);
2667 case CTL_DELAY_IO: {
2668 struct ctl_io_delay_info *delay_info;
2670 delay_info = (struct ctl_io_delay_info *)addr;
2673 mtx_lock(&softc->ctl_lock);
2675 if ((delay_info->lun_id >= CTL_MAX_LUNS)
2676 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2677 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2679 lun = softc->ctl_luns[delay_info->lun_id];
2680 mtx_lock(&lun->lun_lock);
2682 delay_info->status = CTL_DELAY_STATUS_OK;
2684 switch (delay_info->delay_type) {
2685 case CTL_DELAY_TYPE_CONT:
2687 case CTL_DELAY_TYPE_ONESHOT:
2690 delay_info->status =
2691 CTL_DELAY_STATUS_INVALID_TYPE;
2695 switch (delay_info->delay_loc) {
2696 case CTL_DELAY_LOC_DATAMOVE:
2697 lun->delay_info.datamove_type =
2698 delay_info->delay_type;
2699 lun->delay_info.datamove_delay =
2700 delay_info->delay_secs;
2702 case CTL_DELAY_LOC_DONE:
2703 lun->delay_info.done_type =
2704 delay_info->delay_type;
2705 lun->delay_info.done_delay =
2706 delay_info->delay_secs;
2709 delay_info->status =
2710 CTL_DELAY_STATUS_INVALID_LOC;
2713 mtx_unlock(&lun->lun_lock);
2716 mtx_unlock(&softc->ctl_lock);
2718 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2719 #endif /* CTL_IO_DELAY */
2722 case CTL_GETSTATS: {
2723 struct ctl_stats *stats;
2726 stats = (struct ctl_stats *)addr;
2728 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2730 stats->status = CTL_SS_NEED_MORE_SPACE;
2731 stats->num_luns = softc->num_luns;
2735 * XXX KDM no locking here. If the LUN list changes,
2736 * things can blow up.
2738 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
2739 i++, lun = STAILQ_NEXT(lun, links)) {
2740 retval = copyout(&lun->stats, &stats->lun_stats[i],
2741 sizeof(lun->stats));
2745 stats->num_luns = softc->num_luns;
2746 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2748 stats->status = CTL_SS_OK;
2750 stats->flags = CTL_STATS_FLAG_TIME_VALID;
2752 stats->flags = CTL_STATS_FLAG_NONE;
2754 getnanouptime(&stats->timestamp);
2757 case CTL_ERROR_INJECT: {
2758 struct ctl_error_desc *err_desc, *new_err_desc;
2760 err_desc = (struct ctl_error_desc *)addr;
2762 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2764 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2766 mtx_lock(&softc->ctl_lock);
2767 lun = softc->ctl_luns[err_desc->lun_id];
2769 mtx_unlock(&softc->ctl_lock);
2770 free(new_err_desc, M_CTL);
2771 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2772 __func__, (uintmax_t)err_desc->lun_id);
2776 mtx_lock(&lun->lun_lock);
2777 mtx_unlock(&softc->ctl_lock);
2780 * We could do some checking here to verify the validity
2781 * of the request, but given the complexity of error
2782 * injection requests, the checking logic would be fairly
2785 * For now, if the request is invalid, it just won't get
2786 * executed and might get deleted.
2788 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2791 * XXX KDM check to make sure the serial number is unique,
2792 * in case we somehow manage to wrap. That shouldn't
2793 * happen for a very long time, but it's the right thing to
2796 new_err_desc->serial = lun->error_serial;
2797 err_desc->serial = lun->error_serial;
2798 lun->error_serial++;
2800 mtx_unlock(&lun->lun_lock);
2803 case CTL_ERROR_INJECT_DELETE: {
2804 struct ctl_error_desc *delete_desc, *desc, *desc2;
2807 delete_desc = (struct ctl_error_desc *)addr;
2810 mtx_lock(&softc->ctl_lock);
2811 lun = softc->ctl_luns[delete_desc->lun_id];
2813 mtx_unlock(&softc->ctl_lock);
2814 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2815 __func__, (uintmax_t)delete_desc->lun_id);
2819 mtx_lock(&lun->lun_lock);
2820 mtx_unlock(&softc->ctl_lock);
2821 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2822 if (desc->serial != delete_desc->serial)
2825 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2830 mtx_unlock(&lun->lun_lock);
2831 if (delete_done == 0) {
2832 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2833 "error serial %ju on LUN %u\n", __func__,
2834 delete_desc->serial, delete_desc->lun_id);
2840 case CTL_DUMP_STRUCTS: {
2842 struct ctl_port *port;
2843 struct ctl_frontend *fe;
2845 mtx_lock(&softc->ctl_lock);
2846 printf("CTL Persistent Reservation information start:\n");
2847 for (i = 0; i < CTL_MAX_LUNS; i++) {
2848 lun = softc->ctl_luns[i];
2851 || ((lun->flags & CTL_LUN_DISABLED) != 0))
2854 for (j = 0; j < CTL_MAX_PORTS; j++) {
2855 if (lun->pr_keys[j] == NULL)
2857 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2858 if (lun->pr_keys[j][k] == 0)
2860 printf(" LUN %d port %d iid %d key "
2862 (uintmax_t)lun->pr_keys[j][k]);
2866 printf("CTL Persistent Reservation information end\n");
2867 printf("CTL Ports:\n");
2868 STAILQ_FOREACH(port, &softc->port_list, links) {
2869 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
2870 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
2871 port->frontend->name, port->port_type,
2872 port->physical_port, port->virtual_port,
2873 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
2874 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2875 if (port->wwpn_iid[j].in_use == 0 &&
2876 port->wwpn_iid[j].wwpn == 0 &&
2877 port->wwpn_iid[j].name == NULL)
2880 printf(" iid %u use %d WWPN %#jx '%s'\n",
2881 j, port->wwpn_iid[j].in_use,
2882 (uintmax_t)port->wwpn_iid[j].wwpn,
2883 port->wwpn_iid[j].name);
2886 printf("CTL Port information end\n");
2887 mtx_unlock(&softc->ctl_lock);
2889 * XXX KDM calling this without a lock. We'd likely want
2890 * to drop the lock before calling the frontend's dump
2893 printf("CTL Frontends:\n");
2894 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2895 printf(" Frontend '%s'\n", fe->name);
2896 if (fe->fe_dump != NULL)
2899 printf("CTL Frontend information end\n");
2903 struct ctl_lun_req *lun_req;
2904 struct ctl_backend_driver *backend;
2906 lun_req = (struct ctl_lun_req *)addr;
2908 backend = ctl_backend_find(lun_req->backend);
2909 if (backend == NULL) {
2910 lun_req->status = CTL_LUN_ERROR;
2911 snprintf(lun_req->error_str,
2912 sizeof(lun_req->error_str),
2913 "Backend \"%s\" not found.",
2917 if (lun_req->num_be_args > 0) {
2918 lun_req->kern_be_args = ctl_copyin_args(
2919 lun_req->num_be_args,
2922 sizeof(lun_req->error_str));
2923 if (lun_req->kern_be_args == NULL) {
2924 lun_req->status = CTL_LUN_ERROR;
2929 retval = backend->ioctl(dev, cmd, addr, flag, td);
2931 if (lun_req->num_be_args > 0) {
2932 ctl_copyout_args(lun_req->num_be_args,
2933 lun_req->kern_be_args);
2934 ctl_free_args(lun_req->num_be_args,
2935 lun_req->kern_be_args);
2939 case CTL_LUN_LIST: {
2941 struct ctl_lun_list *list;
2942 struct ctl_option *opt;
2944 list = (struct ctl_lun_list *)addr;
2947 * Allocate a fixed length sbuf here, based on the length
2948 * of the user's buffer. We could allocate an auto-extending
2949 * buffer, and then tell the user how much larger our
2950 * amount of data is than his buffer, but that presents
2953 * 1. The sbuf(9) routines use a blocking malloc, and so
2954 * we can't hold a lock while calling them with an
2955 * auto-extending buffer.
2957 * 2. There is not currently a LUN reference counting
2958 * mechanism, outside of outstanding transactions on
2959 * the LUN's OOA queue. So a LUN could go away on us
2960 * while we're getting the LUN number, backend-specific
2961 * information, etc. Thus, given the way things
2962 * currently work, we need to hold the CTL lock while
2963 * grabbing LUN information.
2965 * So, from the user's standpoint, the best thing to do is
2966 * allocate what he thinks is a reasonable buffer length,
2967 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
2968 * double the buffer length and try again. (And repeat
2969 * that until he succeeds.)
2971 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
2973 list->status = CTL_LUN_LIST_ERROR;
2974 snprintf(list->error_str, sizeof(list->error_str),
2975 "Unable to allocate %d bytes for LUN list",
2980 sbuf_printf(sb, "<ctllunlist>\n");
2982 mtx_lock(&softc->ctl_lock);
2983 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2984 mtx_lock(&lun->lun_lock);
2985 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
2986 (uintmax_t)lun->lun);
2989 * Bail out as soon as we see that we've overfilled
2995 retval = sbuf_printf(sb, "\t<backend_type>%s"
2996 "</backend_type>\n",
2997 (lun->backend == NULL) ? "none" :
2998 lun->backend->name);
3003 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3004 lun->be_lun->lun_type);
3009 if (lun->backend == NULL) {
3010 retval = sbuf_printf(sb, "</lun>\n");
3016 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3017 (lun->be_lun->maxlba > 0) ?
3018 lun->be_lun->maxlba + 1 : 0);
3023 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3024 lun->be_lun->blocksize);
3029 retval = sbuf_printf(sb, "\t<serial_number>");
3034 retval = ctl_sbuf_printf_esc(sb,
3035 lun->be_lun->serial_num,
3036 sizeof(lun->be_lun->serial_num));
3041 retval = sbuf_printf(sb, "</serial_number>\n");
3046 retval = sbuf_printf(sb, "\t<device_id>");
3051 retval = ctl_sbuf_printf_esc(sb,
3052 lun->be_lun->device_id,
3053 sizeof(lun->be_lun->device_id));
3058 retval = sbuf_printf(sb, "</device_id>\n");
3063 if (lun->backend->lun_info != NULL) {
3064 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3068 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3069 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3070 opt->name, opt->value, opt->name);
3075 retval = sbuf_printf(sb, "</lun>\n");
3079 mtx_unlock(&lun->lun_lock);
3082 mtx_unlock(&lun->lun_lock);
3083 mtx_unlock(&softc->ctl_lock);
3086 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3089 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3090 snprintf(list->error_str, sizeof(list->error_str),
3091 "Out of space, %d bytes is too small",
3098 retval = copyout(sbuf_data(sb), list->lun_xml,
3101 list->fill_len = sbuf_len(sb) + 1;
3102 list->status = CTL_LUN_LIST_OK;
3107 struct ctl_iscsi *ci;
3108 struct ctl_frontend *fe;
3110 ci = (struct ctl_iscsi *)addr;
3112 fe = ctl_frontend_find("iscsi");
3114 ci->status = CTL_ISCSI_ERROR;
3115 snprintf(ci->error_str, sizeof(ci->error_str),
3116 "Frontend \"iscsi\" not found.");
3120 retval = fe->ioctl(dev, cmd, addr, flag, td);
3123 case CTL_PORT_REQ: {
3124 struct ctl_req *req;
3125 struct ctl_frontend *fe;
3127 req = (struct ctl_req *)addr;
3129 fe = ctl_frontend_find(req->driver);
3131 req->status = CTL_LUN_ERROR;
3132 snprintf(req->error_str, sizeof(req->error_str),
3133 "Frontend \"%s\" not found.", req->driver);
3136 if (req->num_args > 0) {
3137 req->kern_args = ctl_copyin_args(req->num_args,
3138 req->args, req->error_str, sizeof(req->error_str));
3139 if (req->kern_args == NULL) {
3140 req->status = CTL_LUN_ERROR;
3146 retval = fe->ioctl(dev, cmd, addr, flag, td);
3150 if (req->num_args > 0) {
3151 ctl_copyout_args(req->num_args, req->kern_args);
3152 ctl_free_args(req->num_args, req->kern_args);
3156 case CTL_PORT_LIST: {
3158 struct ctl_port *port;
3159 struct ctl_lun_list *list;
3160 struct ctl_option *opt;
3164 list = (struct ctl_lun_list *)addr;
3166 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3168 list->status = CTL_LUN_LIST_ERROR;
3169 snprintf(list->error_str, sizeof(list->error_str),
3170 "Unable to allocate %d bytes for LUN list",
3175 sbuf_printf(sb, "<ctlportlist>\n");
3177 mtx_lock(&softc->ctl_lock);
3178 STAILQ_FOREACH(port, &softc->port_list, links) {
3179 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3180 (uintmax_t)port->targ_port);
3183 * Bail out as soon as we see that we've overfilled
3189 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3190 "</frontend_type>\n", port->frontend->name);
3194 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3199 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3200 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3204 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3209 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3210 port->physical_port);
3214 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3215 port->virtual_port);
3219 if (port->target_devid != NULL) {
3220 sbuf_printf(sb, "\t<target>");
3221 ctl_id_sbuf(port->target_devid, sb);
3222 sbuf_printf(sb, "</target>\n");
3225 if (port->port_devid != NULL) {
3226 sbuf_printf(sb, "\t<port>");
3227 ctl_id_sbuf(port->port_devid, sb);
3228 sbuf_printf(sb, "</port>\n");
3231 if (port->port_info != NULL) {
3232 retval = port->port_info(port->onoff_arg, sb);
3236 STAILQ_FOREACH(opt, &port->options, links) {
3237 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3238 opt->name, opt->value, opt->name);
3243 if (port->lun_map != NULL) {
3244 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n");
3245 for (j = 0; j < CTL_MAX_LUNS; j++) {
3246 plun = ctl_lun_map_from_port(port, j);
3247 if (plun >= CTL_MAX_LUNS)
3250 "\t<lun id=\"%u\">%u</lun>\n",
3255 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3256 if (port->wwpn_iid[j].in_use == 0 ||
3257 (port->wwpn_iid[j].wwpn == 0 &&
3258 port->wwpn_iid[j].name == NULL))
3261 if (port->wwpn_iid[j].name != NULL)
3262 retval = sbuf_printf(sb,
3263 "\t<initiator id=\"%u\">%s</initiator>\n",
3264 j, port->wwpn_iid[j].name);
3266 retval = sbuf_printf(sb,
3267 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3268 j, port->wwpn_iid[j].wwpn);
3275 retval = sbuf_printf(sb, "</targ_port>\n");
3279 mtx_unlock(&softc->ctl_lock);
3282 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3285 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3286 snprintf(list->error_str, sizeof(list->error_str),
3287 "Out of space, %d bytes is too small",
3294 retval = copyout(sbuf_data(sb), list->lun_xml,
3297 list->fill_len = sbuf_len(sb) + 1;
3298 list->status = CTL_LUN_LIST_OK;
3303 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr;
3304 struct ctl_port *port;
3306 mtx_lock(&softc->ctl_lock);
3307 if (lm->port < softc->port_min ||
3308 lm->port >= softc->port_max ||
3309 (port = softc->ctl_ports[lm->port]) == NULL) {
3310 mtx_unlock(&softc->ctl_lock);
3313 if (port->status & CTL_PORT_STATUS_ONLINE) {
3314 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3315 if (ctl_lun_map_to_port(port, lun->lun) >=
3318 mtx_lock(&lun->lun_lock);
3319 ctl_est_ua_port(lun, lm->port, -1,
3321 mtx_unlock(&lun->lun_lock);
3324 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps
3325 if (lm->plun < CTL_MAX_LUNS) {
3326 if (lm->lun == UINT32_MAX)
3327 retval = ctl_lun_map_unset(port, lm->plun);
3328 else if (lm->lun < CTL_MAX_LUNS &&
3329 softc->ctl_luns[lm->lun] != NULL)
3330 retval = ctl_lun_map_set(port, lm->plun, lm->lun);
3333 } else if (lm->plun == UINT32_MAX) {
3334 if (lm->lun == UINT32_MAX)
3335 retval = ctl_lun_map_deinit(port);
3337 retval = ctl_lun_map_init(port);
3340 if (port->status & CTL_PORT_STATUS_ONLINE)
3341 ctl_isc_announce_port(port);
3345 /* XXX KDM should we fix this? */
3347 struct ctl_backend_driver *backend;
3354 * We encode the backend type as the ioctl type for backend
3355 * ioctls. So parse it out here, and then search for a
3356 * backend of this type.
3358 type = _IOC_TYPE(cmd);
3360 STAILQ_FOREACH(backend, &softc->be_list, links) {
3361 if (backend->type == type) {
3367 printf("ctl: unknown ioctl command %#lx or backend "
3372 retval = backend->ioctl(dev, cmd, addr, flag, td);
3382 ctl_get_initindex(struct ctl_nexus *nexus)
3384 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3388 ctl_lun_map_init(struct ctl_port *port)
3390 struct ctl_softc *softc = port->ctl_softc;
3391 struct ctl_lun *lun;
3394 if (port->lun_map == NULL)
3395 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS,
3397 if (port->lun_map == NULL)
3399 for (i = 0; i < CTL_MAX_LUNS; i++)
3400 port->lun_map[i] = UINT32_MAX;
3401 if (port->status & CTL_PORT_STATUS_ONLINE) {
3402 if (port->lun_disable != NULL) {
3403 STAILQ_FOREACH(lun, &softc->lun_list, links)
3404 port->lun_disable(port->targ_lun_arg, lun->lun);
3406 ctl_isc_announce_port(port);
3412 ctl_lun_map_deinit(struct ctl_port *port)
3414 struct ctl_softc *softc = port->ctl_softc;
3415 struct ctl_lun *lun;
3417 if (port->lun_map == NULL)
3419 free(port->lun_map, M_CTL);
3420 port->lun_map = NULL;
3421 if (port->status & CTL_PORT_STATUS_ONLINE) {
3422 if (port->lun_enable != NULL) {
3423 STAILQ_FOREACH(lun, &softc->lun_list, links)
3424 port->lun_enable(port->targ_lun_arg, lun->lun);
3426 ctl_isc_announce_port(port);
3432 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun)
3437 if (port->lun_map == NULL) {
3438 status = ctl_lun_map_init(port);
3442 old = port->lun_map[plun];
3443 port->lun_map[plun] = glun;
3444 if ((port->status & CTL_PORT_STATUS_ONLINE) && old >= CTL_MAX_LUNS) {
3445 if (port->lun_enable != NULL)
3446 port->lun_enable(port->targ_lun_arg, plun);
3447 ctl_isc_announce_port(port);
3453 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun)
3457 if (port->lun_map == NULL)
3459 old = port->lun_map[plun];
3460 port->lun_map[plun] = UINT32_MAX;
3461 if ((port->status & CTL_PORT_STATUS_ONLINE) && old < CTL_MAX_LUNS) {
3462 if (port->lun_disable != NULL)
3463 port->lun_disable(port->targ_lun_arg, plun);
3464 ctl_isc_announce_port(port);
3470 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
3474 return (UINT32_MAX);
3475 if (port->lun_map == NULL || lun_id >= CTL_MAX_LUNS)
3477 return (port->lun_map[lun_id]);
3481 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
3486 return (UINT32_MAX);
3487 if (port->lun_map == NULL)
3489 for (i = 0; i < CTL_MAX_LUNS; i++) {
3490 if (port->lun_map[i] == lun_id)
3493 return (UINT32_MAX);
3496 static struct ctl_port *
3497 ctl_io_port(struct ctl_io_hdr *io_hdr)
3500 return (control_softc->ctl_ports[io_hdr->nexus.targ_port]);
3504 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last)
3508 for (i = first; i < last; i++) {
3509 if ((mask[i / 32] & (1 << (i % 32))) == 0)
3516 ctl_set_mask(uint32_t *mask, uint32_t bit)
3518 uint32_t chunk, piece;
3521 piece = bit % (sizeof(uint32_t) * 8);
3523 if ((mask[chunk] & (1 << piece)) != 0)
3526 mask[chunk] |= (1 << piece);
3532 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3534 uint32_t chunk, piece;
3537 piece = bit % (sizeof(uint32_t) * 8);
3539 if ((mask[chunk] & (1 << piece)) == 0)
3542 mask[chunk] &= ~(1 << piece);
3548 ctl_is_set(uint32_t *mask, uint32_t bit)
3550 uint32_t chunk, piece;
3553 piece = bit % (sizeof(uint32_t) * 8);
3555 if ((mask[chunk] & (1 << piece)) == 0)
3562 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3566 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3569 return (t[residx % CTL_MAX_INIT_PER_PORT]);
3573 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3577 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3580 t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3584 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3589 i = residx/CTL_MAX_INIT_PER_PORT;
3590 if (lun->pr_keys[i] != NULL)
3592 mtx_unlock(&lun->lun_lock);
3593 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3595 mtx_lock(&lun->lun_lock);
3596 if (lun->pr_keys[i] == NULL)
3597 lun->pr_keys[i] = p;
3603 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3607 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3608 KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3609 t[residx % CTL_MAX_INIT_PER_PORT] = key;
3613 * ctl_softc, pool_name, total_ctl_io are passed in.
3614 * npool is passed out.
3617 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3618 uint32_t total_ctl_io, void **npool)
3621 struct ctl_io_pool *pool;
3623 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3628 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3629 pool->ctl_softc = ctl_softc;
3630 pool->zone = uma_zsecond_create(pool->name, NULL,
3631 NULL, NULL, NULL, ctl_softc->io_zone);
3632 /* uma_prealloc(pool->zone, total_ctl_io); */
3636 *npool = ctl_softc->io_zone;
3642 ctl_pool_free(struct ctl_io_pool *pool)
3649 uma_zdestroy(pool->zone);
3655 ctl_alloc_io(void *pool_ref)
3659 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3661 io = uma_zalloc(pool->zone, M_WAITOK);
3663 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK);
3666 io->io_hdr.pool = pool_ref;
3671 ctl_alloc_io_nowait(void *pool_ref)
3675 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3677 io = uma_zalloc(pool->zone, M_NOWAIT);
3679 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT);
3682 io->io_hdr.pool = pool_ref;
3687 ctl_free_io(union ctl_io *io)
3690 struct ctl_io_pool *pool;
3697 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3698 uma_zfree(pool->zone, io);
3700 uma_zfree((uma_zone_t)io->io_hdr.pool, io);
3705 ctl_zero_io(union ctl_io *io)
3713 * May need to preserve linked list pointers at some point too.
3715 pool_ref = io->io_hdr.pool;
3716 memset(io, 0, sizeof(*io));
3717 io->io_hdr.pool = pool_ref;
3721 * This routine is currently used for internal copies of ctl_ios that need
3722 * to persist for some reason after we've already returned status to the
3723 * FETD. (Thus the flag set.)
3726 * Note that this makes a blind copy of all fields in the ctl_io, except
3727 * for the pool reference. This includes any memory that has been
3728 * allocated! That memory will no longer be valid after done has been
3729 * called, so this would be VERY DANGEROUS for command that actually does
3730 * any reads or writes. Right now (11/7/2005), this is only used for immediate
3731 * start and stop commands, which don't transfer any data, so this is not a
3732 * problem. If it is used for anything else, the caller would also need to
3733 * allocate data buffer space and this routine would need to be modified to
3734 * copy the data buffer(s) as well.
3737 ctl_copy_io(union ctl_io *src, union ctl_io *dest)
3746 * May need to preserve linked list pointers at some point too.
3748 pool_ref = dest->io_hdr.pool;
3750 memcpy(dest, src, MIN(sizeof(*src), sizeof(*dest)));
3752 dest->io_hdr.pool = pool_ref;
3754 * We need to know that this is an internal copy, and doesn't need
3755 * to get passed back to the FETD that allocated it.
3757 dest->io_hdr.flags |= CTL_FLAG_INT_COPY;
3761 ctl_expand_number(const char *buf, uint64_t *num)
3767 number = strtoq(buf, &endptr, 0);
3769 switch (tolower((unsigned char)*endptr)) {
3789 case '\0': /* No unit. */
3793 /* Unrecognized unit. */
3797 if ((number << shift) >> shift != number) {
3801 *num = number << shift;
3807 * This routine could be used in the future to load default and/or saved
3808 * mode page parameters for a particuar lun.
3811 ctl_init_page_index(struct ctl_lun *lun)
3814 struct ctl_page_index *page_index;
3818 memcpy(&lun->mode_pages.index, page_index_template,
3819 sizeof(page_index_template));
3821 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3823 page_index = &lun->mode_pages.index[i];
3825 * If this is a disk-only mode page, there's no point in
3826 * setting it up. For some pages, we have to have some
3827 * basic information about the disk in order to calculate the
3830 if ((lun->be_lun->lun_type != T_DIRECT)
3831 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
3834 switch (page_index->page_code & SMPH_PC_MASK) {
3835 case SMS_RW_ERROR_RECOVERY_PAGE: {
3836 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3837 panic("subpage is incorrect!");
3838 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
3839 &rw_er_page_default,
3840 sizeof(rw_er_page_default));
3841 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
3842 &rw_er_page_changeable,
3843 sizeof(rw_er_page_changeable));
3844 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
3845 &rw_er_page_default,
3846 sizeof(rw_er_page_default));
3847 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
3848 &rw_er_page_default,
3849 sizeof(rw_er_page_default));
3850 page_index->page_data =
3851 (uint8_t *)lun->mode_pages.rw_er_page;
3854 case SMS_FORMAT_DEVICE_PAGE: {
3855 struct scsi_format_page *format_page;
3857 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3858 panic("subpage is incorrect!");
3861 * Sectors per track are set above. Bytes per
3862 * sector need to be set here on a per-LUN basis.
3864 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
3865 &format_page_default,
3866 sizeof(format_page_default));
3867 memcpy(&lun->mode_pages.format_page[
3868 CTL_PAGE_CHANGEABLE], &format_page_changeable,
3869 sizeof(format_page_changeable));
3870 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
3871 &format_page_default,
3872 sizeof(format_page_default));
3873 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
3874 &format_page_default,
3875 sizeof(format_page_default));
3877 format_page = &lun->mode_pages.format_page[
3879 scsi_ulto2b(lun->be_lun->blocksize,
3880 format_page->bytes_per_sector);
3882 format_page = &lun->mode_pages.format_page[
3884 scsi_ulto2b(lun->be_lun->blocksize,
3885 format_page->bytes_per_sector);
3887 format_page = &lun->mode_pages.format_page[
3889 scsi_ulto2b(lun->be_lun->blocksize,
3890 format_page->bytes_per_sector);
3892 page_index->page_data =
3893 (uint8_t *)lun->mode_pages.format_page;
3896 case SMS_RIGID_DISK_PAGE: {
3897 struct scsi_rigid_disk_page *rigid_disk_page;
3898 uint32_t sectors_per_cylinder;
3902 #endif /* !__XSCALE__ */
3904 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3905 panic("invalid subpage value %d",
3906 page_index->subpage);
3909 * Rotation rate and sectors per track are set
3910 * above. We calculate the cylinders here based on
3911 * capacity. Due to the number of heads and
3912 * sectors per track we're using, smaller arrays
3913 * may turn out to have 0 cylinders. Linux and
3914 * FreeBSD don't pay attention to these mode pages
3915 * to figure out capacity, but Solaris does. It
3916 * seems to deal with 0 cylinders just fine, and
3917 * works out a fake geometry based on the capacity.
3919 memcpy(&lun->mode_pages.rigid_disk_page[
3920 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
3921 sizeof(rigid_disk_page_default));
3922 memcpy(&lun->mode_pages.rigid_disk_page[
3923 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
3924 sizeof(rigid_disk_page_changeable));
3926 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
3930 * The divide method here will be more accurate,
3931 * probably, but results in floating point being
3932 * used in the kernel on i386 (__udivdi3()). On the
3933 * XScale, though, __udivdi3() is implemented in
3936 * The shift method for cylinder calculation is
3937 * accurate if sectors_per_cylinder is a power of
3938 * 2. Otherwise it might be slightly off -- you
3939 * might have a bit of a truncation problem.
3942 cylinders = (lun->be_lun->maxlba + 1) /
3943 sectors_per_cylinder;
3945 for (shift = 31; shift > 0; shift--) {
3946 if (sectors_per_cylinder & (1 << shift))
3949 cylinders = (lun->be_lun->maxlba + 1) >> shift;
3953 * We've basically got 3 bytes, or 24 bits for the
3954 * cylinder size in the mode page. If we're over,
3955 * just round down to 2^24.
3957 if (cylinders > 0xffffff)
3958 cylinders = 0xffffff;
3960 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
3962 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
3964 if ((value = ctl_get_opt(&lun->be_lun->options,
3966 scsi_ulto2b(strtol(value, NULL, 0),
3967 rigid_disk_page->rotation_rate);
3970 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
3971 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
3972 sizeof(rigid_disk_page_default));
3973 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
3974 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
3975 sizeof(rigid_disk_page_default));
3977 page_index->page_data =
3978 (uint8_t *)lun->mode_pages.rigid_disk_page;
3981 case SMS_CACHING_PAGE: {
3982 struct scsi_caching_page *caching_page;
3984 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3985 panic("invalid subpage value %d",
3986 page_index->subpage);
3987 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
3988 &caching_page_default,
3989 sizeof(caching_page_default));
3990 memcpy(&lun->mode_pages.caching_page[
3991 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
3992 sizeof(caching_page_changeable));
3993 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
3994 &caching_page_default,
3995 sizeof(caching_page_default));
3996 caching_page = &lun->mode_pages.caching_page[
3998 value = ctl_get_opt(&lun->be_lun->options, "writecache");
3999 if (value != NULL && strcmp(value, "off") == 0)
4000 caching_page->flags1 &= ~SCP_WCE;
4001 value = ctl_get_opt(&lun->be_lun->options, "readcache");
4002 if (value != NULL && strcmp(value, "off") == 0)
4003 caching_page->flags1 |= SCP_RCD;
4004 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4005 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4006 sizeof(caching_page_default));
4007 page_index->page_data =
4008 (uint8_t *)lun->mode_pages.caching_page;
4011 case SMS_CONTROL_MODE_PAGE: {
4012 switch (page_index->subpage) {
4013 case SMS_SUBPAGE_PAGE_0: {
4014 struct scsi_control_page *control_page;
4016 memcpy(&lun->mode_pages.control_page[
4018 &control_page_default,
4019 sizeof(control_page_default));
4020 memcpy(&lun->mode_pages.control_page[
4021 CTL_PAGE_CHANGEABLE],
4022 &control_page_changeable,
4023 sizeof(control_page_changeable));
4024 memcpy(&lun->mode_pages.control_page[
4026 &control_page_default,
4027 sizeof(control_page_default));
4028 control_page = &lun->mode_pages.control_page[
4030 value = ctl_get_opt(&lun->be_lun->options,
4032 if (value != NULL &&
4033 strcmp(value, "unrestricted") == 0) {
4034 control_page->queue_flags &=
4035 ~SCP_QUEUE_ALG_MASK;
4036 control_page->queue_flags |=
4037 SCP_QUEUE_ALG_UNRESTRICTED;
4039 memcpy(&lun->mode_pages.control_page[
4041 &lun->mode_pages.control_page[
4043 sizeof(control_page_default));
4044 page_index->page_data =
4045 (uint8_t *)lun->mode_pages.control_page;
4049 memcpy(&lun->mode_pages.control_ext_page[
4051 &control_ext_page_default,
4052 sizeof(control_ext_page_default));
4053 memcpy(&lun->mode_pages.control_ext_page[
4054 CTL_PAGE_CHANGEABLE],
4055 &control_ext_page_changeable,
4056 sizeof(control_ext_page_changeable));
4057 memcpy(&lun->mode_pages.control_ext_page[
4059 &control_ext_page_default,
4060 sizeof(control_ext_page_default));
4061 memcpy(&lun->mode_pages.control_ext_page[
4063 &lun->mode_pages.control_ext_page[
4065 sizeof(control_ext_page_default));
4066 page_index->page_data =
4067 (uint8_t *)lun->mode_pages.control_ext_page;
4072 case SMS_INFO_EXCEPTIONS_PAGE: {
4073 switch (page_index->subpage) {
4074 case SMS_SUBPAGE_PAGE_0:
4075 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4077 sizeof(ie_page_default));
4078 memcpy(&lun->mode_pages.ie_page[
4079 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4080 sizeof(ie_page_changeable));
4081 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4083 sizeof(ie_page_default));
4084 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4086 sizeof(ie_page_default));
4087 page_index->page_data =
4088 (uint8_t *)lun->mode_pages.ie_page;
4091 struct ctl_logical_block_provisioning_page *page;
4093 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4095 sizeof(lbp_page_default));
4096 memcpy(&lun->mode_pages.lbp_page[
4097 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4098 sizeof(lbp_page_changeable));
4099 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4101 sizeof(lbp_page_default));
4102 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4103 value = ctl_get_opt(&lun->be_lun->options,
4105 if (value != NULL &&
4106 ctl_expand_number(value, &ival) == 0) {
4107 page->descr[0].flags |= SLBPPD_ENABLED |
4109 if (lun->be_lun->blocksize)
4110 ival /= lun->be_lun->blocksize;
4113 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4114 page->descr[0].count);
4116 value = ctl_get_opt(&lun->be_lun->options,
4118 if (value != NULL &&
4119 ctl_expand_number(value, &ival) == 0) {
4120 page->descr[1].flags |= SLBPPD_ENABLED |
4122 if (lun->be_lun->blocksize)
4123 ival /= lun->be_lun->blocksize;
4126 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4127 page->descr[1].count);
4129 value = ctl_get_opt(&lun->be_lun->options,
4130 "pool-avail-threshold");
4131 if (value != NULL &&
4132 ctl_expand_number(value, &ival) == 0) {
4133 page->descr[2].flags |= SLBPPD_ENABLED |
4135 if (lun->be_lun->blocksize)
4136 ival /= lun->be_lun->blocksize;
4139 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4140 page->descr[2].count);
4142 value = ctl_get_opt(&lun->be_lun->options,
4143 "pool-used-threshold");
4144 if (value != NULL &&
4145 ctl_expand_number(value, &ival) == 0) {
4146 page->descr[3].flags |= SLBPPD_ENABLED |
4148 if (lun->be_lun->blocksize)
4149 ival /= lun->be_lun->blocksize;
4152 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4153 page->descr[3].count);
4155 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4156 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4157 sizeof(lbp_page_default));
4158 page_index->page_data =
4159 (uint8_t *)lun->mode_pages.lbp_page;
4163 case SMS_VENDOR_SPECIFIC_PAGE:{
4164 switch (page_index->subpage) {
4165 case DBGCNF_SUBPAGE_CODE: {
4166 memcpy(&lun->mode_pages.debugconf_subpage[
4168 &debugconf_page_default,
4169 sizeof(debugconf_page_default));
4170 memcpy(&lun->mode_pages.debugconf_subpage[
4171 CTL_PAGE_CHANGEABLE],
4172 &debugconf_page_changeable,
4173 sizeof(debugconf_page_changeable));
4174 memcpy(&lun->mode_pages.debugconf_subpage[
4176 &debugconf_page_default,
4177 sizeof(debugconf_page_default));
4178 memcpy(&lun->mode_pages.debugconf_subpage[
4180 &debugconf_page_default,
4181 sizeof(debugconf_page_default));
4182 page_index->page_data =
4183 (uint8_t *)lun->mode_pages.debugconf_subpage;
4187 panic("invalid subpage value %d",
4188 page_index->subpage);
4194 panic("invalid page value %d",
4195 page_index->page_code & SMPH_PC_MASK);
4200 return (CTL_RETVAL_COMPLETE);
4204 ctl_init_log_page_index(struct ctl_lun *lun)
4206 struct ctl_page_index *page_index;
4209 memcpy(&lun->log_pages.index, log_page_index_template,
4210 sizeof(log_page_index_template));
4213 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4215 page_index = &lun->log_pages.index[i];
4217 * If this is a disk-only mode page, there's no point in
4218 * setting it up. For some pages, we have to have some
4219 * basic information about the disk in order to calculate the
4222 if ((lun->be_lun->lun_type != T_DIRECT)
4223 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
4226 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4227 lun->backend->lun_attr == NULL)
4230 if (page_index->page_code != prev) {
4231 lun->log_pages.pages_page[j] = page_index->page_code;
4232 prev = page_index->page_code;
4235 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4236 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4239 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4240 lun->log_pages.index[0].page_len = j;
4241 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4242 lun->log_pages.index[1].page_len = k * 2;
4243 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4244 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4245 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page;
4246 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page);
4248 return (CTL_RETVAL_COMPLETE);
4252 hex2bin(const char *str, uint8_t *buf, int buf_size)
4257 memset(buf, 0, buf_size);
4258 while (isspace(str[0]))
4260 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4263 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4267 else if (isalpha(c))
4268 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4274 buf[i / 2] |= (c << 4);
4278 return ((i + 1) / 2);
4285 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4286 * wants us to allocate the LUN and he can block.
4287 * - ctl_softc is always set
4288 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4290 * Returns 0 for success, non-zero (errno) for failure.
4293 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4294 struct ctl_be_lun *const be_lun)
4296 struct ctl_lun *nlun, *lun;
4297 struct scsi_vpd_id_descriptor *desc;
4298 struct scsi_vpd_id_t10 *t10id;
4299 const char *eui, *naa, *scsiname, *vendor;
4300 int lun_number, i, lun_malloced;
4301 int devidlen, idlen1, idlen2 = 0, len;
4307 * We currently only support Direct Access or Processor LUN types.
4309 switch (be_lun->lun_type) {
4317 be_lun->lun_config_status(be_lun->be_lun,
4318 CTL_LUN_CONFIG_FAILURE);
4321 if (ctl_lun == NULL) {
4322 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4329 memset(lun, 0, sizeof(*lun));
4331 lun->flags = CTL_LUN_MALLOCED;
4333 /* Generate LUN ID. */
4334 devidlen = max(CTL_DEVID_MIN_LEN,
4335 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4336 idlen1 = sizeof(*t10id) + devidlen;
4337 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4338 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4339 if (scsiname != NULL) {
4340 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4341 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4343 eui = ctl_get_opt(&be_lun->options, "eui");
4345 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4347 naa = ctl_get_opt(&be_lun->options, "naa");
4349 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4351 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4352 M_CTL, M_WAITOK | M_ZERO);
4353 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4354 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4355 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4356 desc->length = idlen1;
4357 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4358 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4359 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4360 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4362 strncpy(t10id->vendor, vendor,
4363 min(sizeof(t10id->vendor), strlen(vendor)));
4365 strncpy((char *)t10id->vendor_spec_id,
4366 (char *)be_lun->device_id, devidlen);
4367 if (scsiname != NULL) {
4368 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4370 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4371 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4372 SVPD_ID_TYPE_SCSI_NAME;
4373 desc->length = idlen2;
4374 strlcpy(desc->identifier, scsiname, idlen2);
4377 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4379 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4380 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4382 desc->length = hex2bin(eui, desc->identifier, 16);
4383 desc->length = desc->length > 12 ? 16 :
4384 (desc->length > 8 ? 12 : 8);
4385 len -= 16 - desc->length;
4388 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4390 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4391 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4393 desc->length = hex2bin(naa, desc->identifier, 16);
4394 desc->length = desc->length > 8 ? 16 : 8;
4395 len -= 16 - desc->length;
4397 lun->lun_devid->len = len;
4399 mtx_lock(&ctl_softc->ctl_lock);
4401 * See if the caller requested a particular LUN number. If so, see
4402 * if it is available. Otherwise, allocate the first available LUN.
4404 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4405 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4406 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4407 mtx_unlock(&ctl_softc->ctl_lock);
4408 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4409 printf("ctl: requested LUN ID %d is higher "
4410 "than CTL_MAX_LUNS - 1 (%d)\n",
4411 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4414 * XXX KDM return an error, or just assign
4415 * another LUN ID in this case??
4417 printf("ctl: requested LUN ID %d is already "
4418 "in use\n", be_lun->req_lun_id);
4420 if (lun->flags & CTL_LUN_MALLOCED)
4422 be_lun->lun_config_status(be_lun->be_lun,
4423 CTL_LUN_CONFIG_FAILURE);
4426 lun_number = be_lun->req_lun_id;
4428 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, CTL_MAX_LUNS);
4429 if (lun_number == -1) {
4430 mtx_unlock(&ctl_softc->ctl_lock);
4431 printf("ctl: can't allocate LUN, out of LUNs\n");
4432 if (lun->flags & CTL_LUN_MALLOCED)
4434 be_lun->lun_config_status(be_lun->be_lun,
4435 CTL_LUN_CONFIG_FAILURE);
4439 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4441 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4442 lun->lun = lun_number;
4443 lun->be_lun = be_lun;
4445 * The processor LUN is always enabled. Disk LUNs come on line
4446 * disabled, and must be enabled by the backend.
4448 lun->flags |= CTL_LUN_DISABLED;
4449 lun->backend = be_lun->be;
4450 be_lun->ctl_lun = lun;
4451 be_lun->lun_id = lun_number;
4452 atomic_add_int(&be_lun->be->num_luns, 1);
4453 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE)
4454 lun->flags |= CTL_LUN_OFFLINE;
4456 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4457 lun->flags |= CTL_LUN_STOPPED;
4459 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4460 lun->flags |= CTL_LUN_INOPERABLE;
4462 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4463 lun->flags |= CTL_LUN_PRIMARY_SC;
4465 lun->ctl_softc = ctl_softc;
4467 lun->last_busy = getsbinuptime();
4469 TAILQ_INIT(&lun->ooa_queue);
4470 TAILQ_INIT(&lun->blocked_queue);
4471 STAILQ_INIT(&lun->error_list);
4472 ctl_tpc_lun_init(lun);
4475 * Initialize the mode and log page index.
4477 ctl_init_page_index(lun);
4478 ctl_init_log_page_index(lun);
4481 * Now, before we insert this lun on the lun list, set the lun
4482 * inventory changed UA for all other luns.
4484 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4485 mtx_lock(&nlun->lun_lock);
4486 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4487 mtx_unlock(&nlun->lun_lock);
4490 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4492 ctl_softc->ctl_luns[lun_number] = lun;
4494 ctl_softc->num_luns++;
4496 /* Setup statistics gathering */
4497 lun->stats.device_type = be_lun->lun_type;
4498 lun->stats.lun_number = lun_number;
4499 if (lun->stats.device_type == T_DIRECT)
4500 lun->stats.blocksize = be_lun->blocksize;
4502 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4503 for (i = 0;i < CTL_MAX_PORTS;i++)
4504 lun->stats.ports[i].targ_port = i;
4506 mtx_unlock(&ctl_softc->ctl_lock);
4508 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4515 * - LUN has already been marked invalid and any pending I/O has been taken
4519 ctl_free_lun(struct ctl_lun *lun)
4521 struct ctl_softc *softc;
4522 struct ctl_lun *nlun;
4525 softc = lun->ctl_softc;
4527 mtx_assert(&softc->ctl_lock, MA_OWNED);
4529 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4531 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4533 softc->ctl_luns[lun->lun] = NULL;
4535 if (!TAILQ_EMPTY(&lun->ooa_queue))
4536 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4541 * Tell the backend to free resources, if this LUN has a backend.
4543 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4544 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4546 ctl_tpc_lun_shutdown(lun);
4547 mtx_destroy(&lun->lun_lock);
4548 free(lun->lun_devid, M_CTL);
4549 for (i = 0; i < CTL_MAX_PORTS; i++)
4550 free(lun->pending_ua[i], M_CTL);
4551 for (i = 0; i < CTL_MAX_PORTS; i++)
4552 free(lun->pr_keys[i], M_CTL);
4553 free(lun->write_buffer, M_CTL);
4554 if (lun->flags & CTL_LUN_MALLOCED)
4557 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4558 mtx_lock(&nlun->lun_lock);
4559 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4560 mtx_unlock(&nlun->lun_lock);
4567 ctl_create_lun(struct ctl_be_lun *be_lun)
4571 * ctl_alloc_lun() should handle all potential failure cases.
4573 ctl_alloc_lun(control_softc, NULL, be_lun);
4577 ctl_add_lun(struct ctl_be_lun *be_lun)
4579 struct ctl_softc *softc = control_softc;
4581 mtx_lock(&softc->ctl_lock);
4582 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links);
4583 mtx_unlock(&softc->ctl_lock);
4584 wakeup(&softc->pending_lun_queue);
4590 ctl_enable_lun(struct ctl_be_lun *be_lun)
4592 struct ctl_softc *softc;
4593 struct ctl_port *port, *nport;
4594 struct ctl_lun *lun;
4597 lun = (struct ctl_lun *)be_lun->ctl_lun;
4598 softc = lun->ctl_softc;
4600 mtx_lock(&softc->ctl_lock);
4601 mtx_lock(&lun->lun_lock);
4602 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4604 * eh? Why did we get called if the LUN is already
4607 mtx_unlock(&lun->lun_lock);
4608 mtx_unlock(&softc->ctl_lock);
4611 lun->flags &= ~CTL_LUN_DISABLED;
4612 mtx_unlock(&lun->lun_lock);
4614 for (port = STAILQ_FIRST(&softc->port_list); port != NULL; port = nport) {
4615 nport = STAILQ_NEXT(port, links);
4616 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4617 port->lun_map != NULL || port->lun_enable == NULL)
4621 * Drop the lock while we call the FETD's enable routine.
4622 * This can lead to a callback into CTL (at least in the
4623 * case of the internal initiator frontend.
4625 mtx_unlock(&softc->ctl_lock);
4626 retval = port->lun_enable(port->targ_lun_arg, lun->lun);
4627 mtx_lock(&softc->ctl_lock);
4629 printf("%s: FETD %s port %d returned error "
4630 "%d for lun_enable on lun %jd\n",
4631 __func__, port->port_name, port->targ_port,
4632 retval, (intmax_t)lun->lun);
4636 mtx_unlock(&softc->ctl_lock);
4637 ctl_isc_announce_lun(lun);
4643 ctl_disable_lun(struct ctl_be_lun *be_lun)
4645 struct ctl_softc *softc;
4646 struct ctl_port *port;
4647 struct ctl_lun *lun;
4650 lun = (struct ctl_lun *)be_lun->ctl_lun;
4651 softc = lun->ctl_softc;
4653 mtx_lock(&softc->ctl_lock);
4654 mtx_lock(&lun->lun_lock);
4655 if (lun->flags & CTL_LUN_DISABLED) {
4656 mtx_unlock(&lun->lun_lock);
4657 mtx_unlock(&softc->ctl_lock);
4660 lun->flags |= CTL_LUN_DISABLED;
4661 mtx_unlock(&lun->lun_lock);
4663 STAILQ_FOREACH(port, &softc->port_list, links) {
4664 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4665 port->lun_map != NULL || port->lun_disable == NULL)
4669 * Drop the lock before we call the frontend's disable
4670 * routine, to avoid lock order reversals.
4672 * XXX KDM what happens if the frontend list changes while
4673 * we're traversing it? It's unlikely, but should be handled.
4675 mtx_unlock(&softc->ctl_lock);
4676 retval = port->lun_disable(port->targ_lun_arg, lun->lun);
4677 mtx_lock(&softc->ctl_lock);
4679 printf("%s: FETD %s port %d returned error "
4680 "%d for lun_disable on lun %jd\n",
4681 __func__, port->port_name, port->targ_port,
4682 retval, (intmax_t)lun->lun);
4686 mtx_unlock(&softc->ctl_lock);
4687 ctl_isc_announce_lun(lun);
4693 ctl_start_lun(struct ctl_be_lun *be_lun)
4695 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4697 mtx_lock(&lun->lun_lock);
4698 lun->flags &= ~CTL_LUN_STOPPED;
4699 mtx_unlock(&lun->lun_lock);
4704 ctl_stop_lun(struct ctl_be_lun *be_lun)
4706 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4708 mtx_lock(&lun->lun_lock);
4709 lun->flags |= CTL_LUN_STOPPED;
4710 mtx_unlock(&lun->lun_lock);
4715 ctl_lun_offline(struct ctl_be_lun *be_lun)
4717 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4719 mtx_lock(&lun->lun_lock);
4720 lun->flags |= CTL_LUN_OFFLINE;
4721 mtx_unlock(&lun->lun_lock);
4726 ctl_lun_online(struct ctl_be_lun *be_lun)
4728 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4730 mtx_lock(&lun->lun_lock);
4731 lun->flags &= ~CTL_LUN_OFFLINE;
4732 mtx_unlock(&lun->lun_lock);
4737 ctl_lun_primary(struct ctl_be_lun *be_lun)
4739 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4741 mtx_lock(&lun->lun_lock);
4742 lun->flags |= CTL_LUN_PRIMARY_SC;
4743 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4744 mtx_unlock(&lun->lun_lock);
4745 ctl_isc_announce_lun(lun);
4750 ctl_lun_secondary(struct ctl_be_lun *be_lun)
4752 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4754 mtx_lock(&lun->lun_lock);
4755 lun->flags &= ~CTL_LUN_PRIMARY_SC;
4756 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4757 mtx_unlock(&lun->lun_lock);
4758 ctl_isc_announce_lun(lun);
4763 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4765 struct ctl_softc *softc;
4766 struct ctl_lun *lun;
4768 lun = (struct ctl_lun *)be_lun->ctl_lun;
4769 softc = lun->ctl_softc;
4771 mtx_lock(&lun->lun_lock);
4774 * The LUN needs to be disabled before it can be marked invalid.
4776 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4777 mtx_unlock(&lun->lun_lock);
4781 * Mark the LUN invalid.
4783 lun->flags |= CTL_LUN_INVALID;
4786 * If there is nothing in the OOA queue, go ahead and free the LUN.
4787 * If we have something in the OOA queue, we'll free it when the
4788 * last I/O completes.
4790 if (TAILQ_EMPTY(&lun->ooa_queue)) {
4791 mtx_unlock(&lun->lun_lock);
4792 mtx_lock(&softc->ctl_lock);
4794 mtx_unlock(&softc->ctl_lock);
4796 mtx_unlock(&lun->lun_lock);
4802 ctl_lun_inoperable(struct ctl_be_lun *be_lun)
4804 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4806 mtx_lock(&lun->lun_lock);
4807 lun->flags |= CTL_LUN_INOPERABLE;
4808 mtx_unlock(&lun->lun_lock);
4813 ctl_lun_operable(struct ctl_be_lun *be_lun)
4815 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4817 mtx_lock(&lun->lun_lock);
4818 lun->flags &= ~CTL_LUN_INOPERABLE;
4819 mtx_unlock(&lun->lun_lock);
4824 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
4826 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4827 union ctl_ha_msg msg;
4829 mtx_lock(&lun->lun_lock);
4830 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGED);
4831 mtx_unlock(&lun->lun_lock);
4832 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4833 /* Send msg to other side. */
4834 bzero(&msg.ua, sizeof(msg.ua));
4835 msg.hdr.msg_type = CTL_MSG_UA;
4836 msg.hdr.nexus.initid = -1;
4837 msg.hdr.nexus.targ_port = -1;
4838 msg.hdr.nexus.targ_lun = lun->lun;
4839 msg.hdr.nexus.targ_mapped_lun = lun->lun;
4842 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGED;
4843 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4849 * Backend "memory move is complete" callback for requests that never
4850 * make it down to say RAIDCore's configuration code.
4853 ctl_config_move_done(union ctl_io *io)
4857 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
4858 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
4859 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
4861 if ((io->io_hdr.port_status != 0) &&
4862 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
4863 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
4865 * For hardware error sense keys, the sense key
4866 * specific value is defined to be a retry count,
4867 * but we use it to pass back an internal FETD
4868 * error code. XXX KDM Hopefully the FETD is only
4869 * using 16 bits for an error code, since that's
4870 * all the space we have in the sks field.
4872 ctl_set_internal_failure(&io->scsiio,
4875 io->io_hdr.port_status);
4878 if (ctl_debug & CTL_DEBUG_CDB_DATA)
4880 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
4881 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
4882 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
4883 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
4885 * XXX KDM just assuming a single pointer here, and not a
4886 * S/G list. If we start using S/G lists for config data,
4887 * we'll need to know how to clean them up here as well.
4889 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
4890 free(io->scsiio.kern_data_ptr, M_CTL);
4892 retval = CTL_RETVAL_COMPLETE;
4895 * XXX KDM now we need to continue data movement. Some
4897 * - call ctl_scsiio() again? We don't do this for data
4898 * writes, because for those at least we know ahead of
4899 * time where the write will go and how long it is. For
4900 * config writes, though, that information is largely
4901 * contained within the write itself, thus we need to
4902 * parse out the data again.
4904 * - Call some other function once the data is in?
4908 * XXX KDM call ctl_scsiio() again for now, and check flag
4909 * bits to see whether we're allocated or not.
4911 retval = ctl_scsiio(&io->scsiio);
4917 * This gets called by a backend driver when it is done with a
4918 * data_submit method.
4921 ctl_data_submit_done(union ctl_io *io)
4924 * If the IO_CONT flag is set, we need to call the supplied
4925 * function to continue processing the I/O, instead of completing
4928 * If there is an error, though, we don't want to keep processing.
4929 * Instead, just send status back to the initiator.
4931 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
4932 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
4933 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
4934 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
4935 io->scsiio.io_cont(io);
4942 * This gets called by a backend driver when it is done with a
4943 * configuration write.
4946 ctl_config_write_done(union ctl_io *io)
4951 * If the IO_CONT flag is set, we need to call the supplied
4952 * function to continue processing the I/O, instead of completing
4955 * If there is an error, though, we don't want to keep processing.
4956 * Instead, just send status back to the initiator.
4958 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
4959 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
4960 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
4961 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
4962 io->scsiio.io_cont(io);
4966 * Since a configuration write can be done for commands that actually
4967 * have data allocated, like write buffer, and commands that have
4968 * no data, like start/stop unit, we need to check here.
4970 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
4971 buf = io->scsiio.kern_data_ptr;
4980 ctl_config_read_done(union ctl_io *io)
4985 * If there is some error -- we are done, skip data transfer.
4987 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
4988 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
4989 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
4990 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
4991 buf = io->scsiio.kern_data_ptr;
5001 * If the IO_CONT flag is set, we need to call the supplied
5002 * function to continue processing the I/O, instead of completing
5005 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
5006 io->scsiio.io_cont(io);
5014 * SCSI release command.
5017 ctl_scsi_release(struct ctl_scsiio *ctsio)
5019 struct ctl_lun *lun;
5022 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5024 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5025 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5028 * XXX KDM right now, we only support LUN reservation. We don't
5029 * support 3rd party reservations, or extent reservations, which
5030 * might actually need the parameter list. If we've gotten this
5031 * far, we've got a LUN reservation. Anything else got kicked out
5032 * above. So, according to SPC, ignore the length.
5035 mtx_lock(&lun->lun_lock);
5038 * According to SPC, it is not an error for an intiator to attempt
5039 * to release a reservation on a LUN that isn't reserved, or that
5040 * is reserved by another initiator. The reservation can only be
5041 * released, though, by the initiator who made it or by one of
5042 * several reset type events.
5044 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5045 lun->flags &= ~CTL_LUN_RESERVED;
5047 mtx_unlock(&lun->lun_lock);
5049 ctl_set_success(ctsio);
5050 ctl_done((union ctl_io *)ctsio);
5051 return (CTL_RETVAL_COMPLETE);
5055 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5057 struct ctl_lun *lun;
5060 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5062 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5063 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5066 * XXX KDM right now, we only support LUN reservation. We don't
5067 * support 3rd party reservations, or extent reservations, which
5068 * might actually need the parameter list. If we've gotten this
5069 * far, we've got a LUN reservation. Anything else got kicked out
5070 * above. So, according to SPC, ignore the length.
5073 mtx_lock(&lun->lun_lock);
5074 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5075 ctl_set_reservation_conflict(ctsio);
5078 lun->flags |= CTL_LUN_RESERVED;
5079 lun->res_idx = residx;
5080 ctl_set_success(ctsio);
5083 mtx_unlock(&lun->lun_lock);
5084 ctl_done((union ctl_io *)ctsio);
5085 return (CTL_RETVAL_COMPLETE);
5089 ctl_start_stop(struct ctl_scsiio *ctsio)
5091 struct scsi_start_stop_unit *cdb;
5092 struct ctl_lun *lun;
5095 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5097 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5098 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5102 * We don't support the immediate bit on a stop unit. In order to
5103 * do that, we would need to code up a way to know that a stop is
5104 * pending, and hold off any new commands until it completes, one
5105 * way or another. Then we could accept or reject those commands
5106 * depending on its status. We would almost need to do the reverse
5107 * of what we do below for an immediate start -- return the copy of
5108 * the ctl_io to the FETD with status to send to the host (and to
5109 * free the copy!) and then free the original I/O once the stop
5110 * actually completes. That way, the OOA queue mechanism can work
5111 * to block commands that shouldn't proceed. Another alternative
5112 * would be to put the copy in the queue in place of the original,
5113 * and return the original back to the caller. That could be
5116 if ((cdb->byte2 & SSS_IMMED)
5117 && ((cdb->how & SSS_START) == 0)) {
5118 ctl_set_invalid_field(ctsio,
5124 ctl_done((union ctl_io *)ctsio);
5125 return (CTL_RETVAL_COMPLETE);
5128 if ((lun->flags & CTL_LUN_PR_RESERVED)
5129 && ((cdb->how & SSS_START)==0)) {
5132 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5133 if (ctl_get_prkey(lun, residx) == 0
5134 || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5136 ctl_set_reservation_conflict(ctsio);
5137 ctl_done((union ctl_io *)ctsio);
5138 return (CTL_RETVAL_COMPLETE);
5143 * If there is no backend on this device, we can't start or stop
5144 * it. In theory we shouldn't get any start/stop commands in the
5145 * first place at this level if the LUN doesn't have a backend.
5146 * That should get stopped by the command decode code.
5148 if (lun->backend == NULL) {
5149 ctl_set_invalid_opcode(ctsio);
5150 ctl_done((union ctl_io *)ctsio);
5151 return (CTL_RETVAL_COMPLETE);
5155 * In the non-immediate case, we send the request to
5156 * the backend and return status to the user when
5159 * In the immediate case, we allocate a new ctl_io
5160 * to hold a copy of the request, and send that to
5161 * the backend. We then set good status on the
5162 * user's request and return it immediately.
5164 if (cdb->byte2 & SSS_IMMED) {
5165 union ctl_io *new_io;
5167 new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5168 ctl_copy_io((union ctl_io *)ctsio, new_io);
5169 retval = lun->backend->config_write(new_io);
5170 ctl_set_success(ctsio);
5171 ctl_done((union ctl_io *)ctsio);
5173 retval = lun->backend->config_write(
5174 (union ctl_io *)ctsio);
5180 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5181 * we don't really do anything with the LBA and length fields if the user
5182 * passes them in. Instead we'll just flush out the cache for the entire
5186 ctl_sync_cache(struct ctl_scsiio *ctsio)
5188 struct ctl_lun *lun;
5189 struct ctl_softc *softc;
5190 struct ctl_lba_len_flags *lbalen;
5191 uint64_t starting_lba;
5192 uint32_t block_count;
5196 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5198 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5199 softc = lun->ctl_softc;
5202 switch (ctsio->cdb[0]) {
5203 case SYNCHRONIZE_CACHE: {
5204 struct scsi_sync_cache *cdb;
5205 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5207 starting_lba = scsi_4btoul(cdb->begin_lba);
5208 block_count = scsi_2btoul(cdb->lb_count);
5212 case SYNCHRONIZE_CACHE_16: {
5213 struct scsi_sync_cache_16 *cdb;
5214 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5216 starting_lba = scsi_8btou64(cdb->begin_lba);
5217 block_count = scsi_4btoul(cdb->lb_count);
5222 ctl_set_invalid_opcode(ctsio);
5223 ctl_done((union ctl_io *)ctsio);
5225 break; /* NOTREACHED */
5229 * We check the LBA and length, but don't do anything with them.
5230 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5231 * get flushed. This check will just help satisfy anyone who wants
5232 * to see an error for an out of range LBA.
5234 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5235 ctl_set_lba_out_of_range(ctsio);
5236 ctl_done((union ctl_io *)ctsio);
5241 * If this LUN has no backend, we can't flush the cache anyway.
5243 if (lun->backend == NULL) {
5244 ctl_set_invalid_opcode(ctsio);
5245 ctl_done((union ctl_io *)ctsio);
5249 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5250 lbalen->lba = starting_lba;
5251 lbalen->len = block_count;
5252 lbalen->flags = byte2;
5253 retval = lun->backend->config_write((union ctl_io *)ctsio);
5260 ctl_format(struct ctl_scsiio *ctsio)
5262 struct scsi_format *cdb;
5263 struct ctl_lun *lun;
5264 int length, defect_list_len;
5266 CTL_DEBUG_PRINT(("ctl_format\n"));
5268 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5270 cdb = (struct scsi_format *)ctsio->cdb;
5273 if (cdb->byte2 & SF_FMTDATA) {
5274 if (cdb->byte2 & SF_LONGLIST)
5275 length = sizeof(struct scsi_format_header_long);
5277 length = sizeof(struct scsi_format_header_short);
5280 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5282 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5283 ctsio->kern_data_len = length;
5284 ctsio->kern_total_len = length;
5285 ctsio->kern_data_resid = 0;
5286 ctsio->kern_rel_offset = 0;
5287 ctsio->kern_sg_entries = 0;
5288 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5289 ctsio->be_move_done = ctl_config_move_done;
5290 ctl_datamove((union ctl_io *)ctsio);
5292 return (CTL_RETVAL_COMPLETE);
5295 defect_list_len = 0;
5297 if (cdb->byte2 & SF_FMTDATA) {
5298 if (cdb->byte2 & SF_LONGLIST) {
5299 struct scsi_format_header_long *header;
5301 header = (struct scsi_format_header_long *)
5302 ctsio->kern_data_ptr;
5304 defect_list_len = scsi_4btoul(header->defect_list_len);
5305 if (defect_list_len != 0) {
5306 ctl_set_invalid_field(ctsio,
5315 struct scsi_format_header_short *header;
5317 header = (struct scsi_format_header_short *)
5318 ctsio->kern_data_ptr;
5320 defect_list_len = scsi_2btoul(header->defect_list_len);
5321 if (defect_list_len != 0) {
5322 ctl_set_invalid_field(ctsio,
5334 * The format command will clear out the "Medium format corrupted"
5335 * status if set by the configuration code. That status is really
5336 * just a way to notify the host that we have lost the media, and
5337 * get them to issue a command that will basically make them think
5338 * they're blowing away the media.
5340 mtx_lock(&lun->lun_lock);
5341 lun->flags &= ~CTL_LUN_INOPERABLE;
5342 mtx_unlock(&lun->lun_lock);
5344 ctl_set_success(ctsio);
5347 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5348 free(ctsio->kern_data_ptr, M_CTL);
5349 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5352 ctl_done((union ctl_io *)ctsio);
5353 return (CTL_RETVAL_COMPLETE);
5357 ctl_read_buffer(struct ctl_scsiio *ctsio)
5359 struct ctl_lun *lun;
5360 uint64_t buffer_offset;
5363 static uint8_t descr[4];
5364 static uint8_t echo_descr[4] = { 0 };
5366 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5367 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5368 switch (ctsio->cdb[0]) {
5370 struct scsi_read_buffer *cdb;
5372 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5373 buffer_offset = scsi_3btoul(cdb->offset);
5374 len = scsi_3btoul(cdb->length);
5378 case READ_BUFFER_16: {
5379 struct scsi_read_buffer_16 *cdb;
5381 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb;
5382 buffer_offset = scsi_8btou64(cdb->offset);
5383 len = scsi_4btoul(cdb->length);
5387 default: /* This shouldn't happen. */
5388 ctl_set_invalid_opcode(ctsio);
5389 ctl_done((union ctl_io *)ctsio);
5390 return (CTL_RETVAL_COMPLETE);
5393 if ((byte2 & RWB_MODE) != RWB_MODE_DATA &&
5394 (byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR &&
5395 (byte2 & RWB_MODE) != RWB_MODE_DESCR) {
5396 ctl_set_invalid_field(ctsio,
5402 ctl_done((union ctl_io *)ctsio);
5403 return (CTL_RETVAL_COMPLETE);
5406 if (buffer_offset > CTL_WRITE_BUFFER_SIZE ||
5407 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5408 ctl_set_invalid_field(ctsio,
5414 ctl_done((union ctl_io *)ctsio);
5415 return (CTL_RETVAL_COMPLETE);
5418 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5420 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5421 ctsio->kern_data_ptr = descr;
5422 len = min(len, sizeof(descr));
5423 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5424 ctsio->kern_data_ptr = echo_descr;
5425 len = min(len, sizeof(echo_descr));
5427 if (lun->write_buffer == NULL) {
5428 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5431 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5433 ctsio->kern_data_len = len;
5434 ctsio->kern_total_len = len;
5435 ctsio->kern_data_resid = 0;
5436 ctsio->kern_rel_offset = 0;
5437 ctsio->kern_sg_entries = 0;
5438 ctl_set_success(ctsio);
5439 ctsio->be_move_done = ctl_config_move_done;
5440 ctl_datamove((union ctl_io *)ctsio);
5441 return (CTL_RETVAL_COMPLETE);
5445 ctl_write_buffer(struct ctl_scsiio *ctsio)
5447 struct scsi_write_buffer *cdb;
5448 struct ctl_lun *lun;
5449 int buffer_offset, len;
5451 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5453 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5454 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5456 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5457 ctl_set_invalid_field(ctsio,
5463 ctl_done((union ctl_io *)ctsio);
5464 return (CTL_RETVAL_COMPLETE);
5467 len = scsi_3btoul(cdb->length);
5468 buffer_offset = scsi_3btoul(cdb->offset);
5470 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5471 ctl_set_invalid_field(ctsio,
5477 ctl_done((union ctl_io *)ctsio);
5478 return (CTL_RETVAL_COMPLETE);
5482 * If we've got a kernel request that hasn't been malloced yet,
5483 * malloc it and tell the caller the data buffer is here.
5485 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5486 if (lun->write_buffer == NULL) {
5487 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5490 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5491 ctsio->kern_data_len = len;
5492 ctsio->kern_total_len = len;
5493 ctsio->kern_data_resid = 0;
5494 ctsio->kern_rel_offset = 0;
5495 ctsio->kern_sg_entries = 0;
5496 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5497 ctsio->be_move_done = ctl_config_move_done;
5498 ctl_datamove((union ctl_io *)ctsio);
5500 return (CTL_RETVAL_COMPLETE);
5503 ctl_set_success(ctsio);
5504 ctl_done((union ctl_io *)ctsio);
5505 return (CTL_RETVAL_COMPLETE);
5509 ctl_write_same(struct ctl_scsiio *ctsio)
5511 struct ctl_lun *lun;
5512 struct ctl_lba_len_flags *lbalen;
5514 uint32_t num_blocks;
5518 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5520 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5522 switch (ctsio->cdb[0]) {
5523 case WRITE_SAME_10: {
5524 struct scsi_write_same_10 *cdb;
5526 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5528 lba = scsi_4btoul(cdb->addr);
5529 num_blocks = scsi_2btoul(cdb->length);
5533 case WRITE_SAME_16: {
5534 struct scsi_write_same_16 *cdb;
5536 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5538 lba = scsi_8btou64(cdb->addr);
5539 num_blocks = scsi_4btoul(cdb->length);
5545 * We got a command we don't support. This shouldn't
5546 * happen, commands should be filtered out above us.
5548 ctl_set_invalid_opcode(ctsio);
5549 ctl_done((union ctl_io *)ctsio);
5551 return (CTL_RETVAL_COMPLETE);
5552 break; /* NOTREACHED */
5555 /* ANCHOR flag can be used only together with UNMAP */
5556 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) {
5557 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5558 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5559 ctl_done((union ctl_io *)ctsio);
5560 return (CTL_RETVAL_COMPLETE);
5564 * The first check is to make sure we're in bounds, the second
5565 * check is to catch wrap-around problems. If the lba + num blocks
5566 * is less than the lba, then we've wrapped around and the block
5567 * range is invalid anyway.
5569 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5570 || ((lba + num_blocks) < lba)) {
5571 ctl_set_lba_out_of_range(ctsio);
5572 ctl_done((union ctl_io *)ctsio);
5573 return (CTL_RETVAL_COMPLETE);
5576 /* Zero number of blocks means "to the last logical block" */
5577 if (num_blocks == 0) {
5578 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5579 ctl_set_invalid_field(ctsio,
5585 ctl_done((union ctl_io *)ctsio);
5586 return (CTL_RETVAL_COMPLETE);
5588 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5591 len = lun->be_lun->blocksize;
5594 * If we've got a kernel request that hasn't been malloced yet,
5595 * malloc it and tell the caller the data buffer is here.
5597 if ((byte2 & SWS_NDOB) == 0 &&
5598 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5599 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5600 ctsio->kern_data_len = len;
5601 ctsio->kern_total_len = len;
5602 ctsio->kern_data_resid = 0;
5603 ctsio->kern_rel_offset = 0;
5604 ctsio->kern_sg_entries = 0;
5605 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5606 ctsio->be_move_done = ctl_config_move_done;
5607 ctl_datamove((union ctl_io *)ctsio);
5609 return (CTL_RETVAL_COMPLETE);
5612 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5614 lbalen->len = num_blocks;
5615 lbalen->flags = byte2;
5616 retval = lun->backend->config_write((union ctl_io *)ctsio);
5622 ctl_unmap(struct ctl_scsiio *ctsio)
5624 struct ctl_lun *lun;
5625 struct scsi_unmap *cdb;
5626 struct ctl_ptr_len_flags *ptrlen;
5627 struct scsi_unmap_header *hdr;
5628 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5630 uint32_t num_blocks;
5634 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5636 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5637 cdb = (struct scsi_unmap *)ctsio->cdb;
5639 len = scsi_2btoul(cdb->length);
5643 * If we've got a kernel request that hasn't been malloced yet,
5644 * malloc it and tell the caller the data buffer is here.
5646 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5647 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5648 ctsio->kern_data_len = len;
5649 ctsio->kern_total_len = len;
5650 ctsio->kern_data_resid = 0;
5651 ctsio->kern_rel_offset = 0;
5652 ctsio->kern_sg_entries = 0;
5653 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5654 ctsio->be_move_done = ctl_config_move_done;
5655 ctl_datamove((union ctl_io *)ctsio);
5657 return (CTL_RETVAL_COMPLETE);
5660 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5661 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5662 if (len < sizeof (*hdr) ||
5663 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5664 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5665 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5666 ctl_set_invalid_field(ctsio,
5674 len = scsi_2btoul(hdr->desc_length);
5675 buf = (struct scsi_unmap_desc *)(hdr + 1);
5676 end = buf + len / sizeof(*buf);
5679 for (range = buf; range < end; range++) {
5680 lba = scsi_8btou64(range->lba);
5681 num_blocks = scsi_4btoul(range->length);
5682 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5683 || ((lba + num_blocks) < lba)) {
5684 ctl_set_lba_out_of_range(ctsio);
5685 ctl_done((union ctl_io *)ctsio);
5686 return (CTL_RETVAL_COMPLETE);
5688 if (num_blocks != 0)
5693 * Block backend can not handle zero last range.
5694 * Filter it out and return if there is nothing left.
5696 len = (uint8_t *)endnz - (uint8_t *)buf;
5698 ctl_set_success(ctsio);
5702 mtx_lock(&lun->lun_lock);
5703 ptrlen = (struct ctl_ptr_len_flags *)
5704 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5705 ptrlen->ptr = (void *)buf;
5707 ptrlen->flags = byte2;
5708 ctl_check_blocked(lun);
5709 mtx_unlock(&lun->lun_lock);
5711 retval = lun->backend->config_write((union ctl_io *)ctsio);
5715 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5716 free(ctsio->kern_data_ptr, M_CTL);
5717 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5719 ctl_done((union ctl_io *)ctsio);
5720 return (CTL_RETVAL_COMPLETE);
5724 * Note that this function currently doesn't actually do anything inside
5725 * CTL to enforce things if the DQue bit is turned on.
5727 * Also note that this function can't be used in the default case, because
5728 * the DQue bit isn't set in the changeable mask for the control mode page
5729 * anyway. This is just here as an example for how to implement a page
5730 * handler, and a placeholder in case we want to allow the user to turn
5731 * tagged queueing on and off.
5733 * The D_SENSE bit handling is functional, however, and will turn
5734 * descriptor sense on and off for a given LUN.
5737 ctl_control_page_handler(struct ctl_scsiio *ctsio,
5738 struct ctl_page_index *page_index, uint8_t *page_ptr)
5740 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
5741 struct ctl_lun *lun;
5745 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5746 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5749 user_cp = (struct scsi_control_page *)page_ptr;
5750 current_cp = (struct scsi_control_page *)
5751 (page_index->page_data + (page_index->page_len *
5753 saved_cp = (struct scsi_control_page *)
5754 (page_index->page_data + (page_index->page_len *
5757 mtx_lock(&lun->lun_lock);
5758 if (((current_cp->rlec & SCP_DSENSE) == 0)
5759 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
5761 * Descriptor sense is currently turned off and the user
5762 * wants to turn it on.
5764 current_cp->rlec |= SCP_DSENSE;
5765 saved_cp->rlec |= SCP_DSENSE;
5766 lun->flags |= CTL_LUN_SENSE_DESC;
5768 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
5769 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
5771 * Descriptor sense is currently turned on, and the user
5772 * wants to turn it off.
5774 current_cp->rlec &= ~SCP_DSENSE;
5775 saved_cp->rlec &= ~SCP_DSENSE;
5776 lun->flags &= ~CTL_LUN_SENSE_DESC;
5779 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) !=
5780 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) {
5781 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
5782 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
5783 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
5784 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
5787 if ((current_cp->eca_and_aen & SCP_SWP) !=
5788 (user_cp->eca_and_aen & SCP_SWP)) {
5789 current_cp->eca_and_aen &= ~SCP_SWP;
5790 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
5791 saved_cp->eca_and_aen &= ~SCP_SWP;
5792 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
5796 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5797 mtx_unlock(&lun->lun_lock);
5799 ctl_isc_announce_mode(lun,
5800 ctl_get_initindex(&ctsio->io_hdr.nexus),
5801 page_index->page_code, page_index->subpage);
5807 ctl_caching_sp_handler(struct ctl_scsiio *ctsio,
5808 struct ctl_page_index *page_index, uint8_t *page_ptr)
5810 struct scsi_caching_page *current_cp, *saved_cp, *user_cp;
5811 struct ctl_lun *lun;
5815 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5816 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5819 user_cp = (struct scsi_caching_page *)page_ptr;
5820 current_cp = (struct scsi_caching_page *)
5821 (page_index->page_data + (page_index->page_len *
5823 saved_cp = (struct scsi_caching_page *)
5824 (page_index->page_data + (page_index->page_len *
5827 mtx_lock(&lun->lun_lock);
5828 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) !=
5829 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) {
5830 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
5831 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
5832 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
5833 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
5837 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5838 mtx_unlock(&lun->lun_lock);
5840 ctl_isc_announce_mode(lun,
5841 ctl_get_initindex(&ctsio->io_hdr.nexus),
5842 page_index->page_code, page_index->subpage);
5848 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
5849 struct ctl_page_index *page_index,
5855 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
5860 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
5861 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
5862 printf("page data:");
5864 printf(" %.2x",page_ptr[i]);
5870 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
5871 struct ctl_page_index *page_index,
5874 struct copan_debugconf_subpage *page;
5876 page = (struct copan_debugconf_subpage *)page_index->page_data +
5877 (page_index->page_len * pc);
5880 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
5881 case SMS_PAGE_CTRL_DEFAULT >> 6:
5882 case SMS_PAGE_CTRL_SAVED >> 6:
5884 * We don't update the changable or default bits for this page.
5887 case SMS_PAGE_CTRL_CURRENT >> 6:
5888 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
5889 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
5899 ctl_do_mode_select(union ctl_io *io)
5901 struct scsi_mode_page_header *page_header;
5902 struct ctl_page_index *page_index;
5903 struct ctl_scsiio *ctsio;
5904 int control_dev, page_len;
5905 int page_len_offset, page_len_size;
5906 union ctl_modepage_info *modepage_info;
5907 struct ctl_lun *lun;
5908 int *len_left, *len_used;
5911 ctsio = &io->scsiio;
5914 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5916 if (lun->be_lun->lun_type != T_DIRECT)
5921 modepage_info = (union ctl_modepage_info *)
5922 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
5923 len_left = &modepage_info->header.len_left;
5924 len_used = &modepage_info->header.len_used;
5928 page_header = (struct scsi_mode_page_header *)
5929 (ctsio->kern_data_ptr + *len_used);
5931 if (*len_left == 0) {
5932 free(ctsio->kern_data_ptr, M_CTL);
5933 ctl_set_success(ctsio);
5934 ctl_done((union ctl_io *)ctsio);
5935 return (CTL_RETVAL_COMPLETE);
5936 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
5938 free(ctsio->kern_data_ptr, M_CTL);
5939 ctl_set_param_len_error(ctsio);
5940 ctl_done((union ctl_io *)ctsio);
5941 return (CTL_RETVAL_COMPLETE);
5943 } else if ((page_header->page_code & SMPH_SPF)
5944 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
5946 free(ctsio->kern_data_ptr, M_CTL);
5947 ctl_set_param_len_error(ctsio);
5948 ctl_done((union ctl_io *)ctsio);
5949 return (CTL_RETVAL_COMPLETE);
5954 * XXX KDM should we do something with the block descriptor?
5956 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
5958 if ((control_dev != 0)
5959 && (lun->mode_pages.index[i].page_flags &
5960 CTL_PAGE_FLAG_DISK_ONLY))
5963 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
5964 (page_header->page_code & SMPH_PC_MASK))
5968 * If neither page has a subpage code, then we've got a
5971 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
5972 && ((page_header->page_code & SMPH_SPF) == 0)) {
5973 page_index = &lun->mode_pages.index[i];
5974 page_len = page_header->page_length;
5979 * If both pages have subpages, then the subpage numbers
5982 if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
5983 && (page_header->page_code & SMPH_SPF)) {
5984 struct scsi_mode_page_header_sp *sph;
5986 sph = (struct scsi_mode_page_header_sp *)page_header;
5988 if (lun->mode_pages.index[i].subpage ==
5990 page_index = &lun->mode_pages.index[i];
5991 page_len = scsi_2btoul(sph->page_length);
5998 * If we couldn't find the page, or if we don't have a mode select
5999 * handler for it, send back an error to the user.
6001 if ((page_index == NULL)
6002 || (page_index->select_handler == NULL)) {
6003 ctl_set_invalid_field(ctsio,
6006 /*field*/ *len_used,
6009 free(ctsio->kern_data_ptr, M_CTL);
6010 ctl_done((union ctl_io *)ctsio);
6011 return (CTL_RETVAL_COMPLETE);
6014 if (page_index->page_code & SMPH_SPF) {
6015 page_len_offset = 2;
6019 page_len_offset = 1;
6023 * If the length the initiator gives us isn't the one we specify in
6024 * the mode page header, or if they didn't specify enough data in
6025 * the CDB to avoid truncating this page, kick out the request.
6027 if ((page_len != (page_index->page_len - page_len_offset -
6029 || (*len_left < page_index->page_len)) {
6032 ctl_set_invalid_field(ctsio,
6035 /*field*/ *len_used + page_len_offset,
6038 free(ctsio->kern_data_ptr, M_CTL);
6039 ctl_done((union ctl_io *)ctsio);
6040 return (CTL_RETVAL_COMPLETE);
6044 * Run through the mode page, checking to make sure that the bits
6045 * the user changed are actually legal for him to change.
6047 for (i = 0; i < page_index->page_len; i++) {
6048 uint8_t *user_byte, *change_mask, *current_byte;
6052 user_byte = (uint8_t *)page_header + i;
6053 change_mask = page_index->page_data +
6054 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6055 current_byte = page_index->page_data +
6056 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6059 * Check to see whether the user set any bits in this byte
6060 * that he is not allowed to set.
6062 if ((*user_byte & ~(*change_mask)) ==
6063 (*current_byte & ~(*change_mask)))
6067 * Go through bit by bit to determine which one is illegal.
6070 for (j = 7; j >= 0; j--) {
6071 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6072 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6077 ctl_set_invalid_field(ctsio,
6080 /*field*/ *len_used + i,
6083 free(ctsio->kern_data_ptr, M_CTL);
6084 ctl_done((union ctl_io *)ctsio);
6085 return (CTL_RETVAL_COMPLETE);
6089 * Decrement these before we call the page handler, since we may
6090 * end up getting called back one way or another before the handler
6091 * returns to this context.
6093 *len_left -= page_index->page_len;
6094 *len_used += page_index->page_len;
6096 retval = page_index->select_handler(ctsio, page_index,
6097 (uint8_t *)page_header);
6100 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6101 * wait until this queued command completes to finish processing
6102 * the mode page. If it returns anything other than
6103 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6104 * already set the sense information, freed the data pointer, and
6105 * completed the io for us.
6107 if (retval != CTL_RETVAL_COMPLETE)
6108 goto bailout_no_done;
6111 * If the initiator sent us more than one page, parse the next one.
6116 ctl_set_success(ctsio);
6117 free(ctsio->kern_data_ptr, M_CTL);
6118 ctl_done((union ctl_io *)ctsio);
6122 return (CTL_RETVAL_COMPLETE);
6127 ctl_mode_select(struct ctl_scsiio *ctsio)
6129 int param_len, pf, sp;
6130 int header_size, bd_len;
6131 union ctl_modepage_info *modepage_info;
6133 switch (ctsio->cdb[0]) {
6134 case MODE_SELECT_6: {
6135 struct scsi_mode_select_6 *cdb;
6137 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6139 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6140 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6141 param_len = cdb->length;
6142 header_size = sizeof(struct scsi_mode_header_6);
6145 case MODE_SELECT_10: {
6146 struct scsi_mode_select_10 *cdb;
6148 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6150 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6151 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6152 param_len = scsi_2btoul(cdb->length);
6153 header_size = sizeof(struct scsi_mode_header_10);
6157 ctl_set_invalid_opcode(ctsio);
6158 ctl_done((union ctl_io *)ctsio);
6159 return (CTL_RETVAL_COMPLETE);
6164 * "A parameter list length of zero indicates that the Data-Out Buffer
6165 * shall be empty. This condition shall not be considered as an error."
6167 if (param_len == 0) {
6168 ctl_set_success(ctsio);
6169 ctl_done((union ctl_io *)ctsio);
6170 return (CTL_RETVAL_COMPLETE);
6174 * Since we'll hit this the first time through, prior to
6175 * allocation, we don't need to free a data buffer here.
6177 if (param_len < header_size) {
6178 ctl_set_param_len_error(ctsio);
6179 ctl_done((union ctl_io *)ctsio);
6180 return (CTL_RETVAL_COMPLETE);
6184 * Allocate the data buffer and grab the user's data. In theory,
6185 * we shouldn't have to sanity check the parameter list length here
6186 * because the maximum size is 64K. We should be able to malloc
6187 * that much without too many problems.
6189 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6190 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6191 ctsio->kern_data_len = param_len;
6192 ctsio->kern_total_len = param_len;
6193 ctsio->kern_data_resid = 0;
6194 ctsio->kern_rel_offset = 0;
6195 ctsio->kern_sg_entries = 0;
6196 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6197 ctsio->be_move_done = ctl_config_move_done;
6198 ctl_datamove((union ctl_io *)ctsio);
6200 return (CTL_RETVAL_COMPLETE);
6203 switch (ctsio->cdb[0]) {
6204 case MODE_SELECT_6: {
6205 struct scsi_mode_header_6 *mh6;
6207 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6208 bd_len = mh6->blk_desc_len;
6211 case MODE_SELECT_10: {
6212 struct scsi_mode_header_10 *mh10;
6214 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6215 bd_len = scsi_2btoul(mh10->blk_desc_len);
6219 panic("Invalid CDB type %#x", ctsio->cdb[0]);
6223 if (param_len < (header_size + bd_len)) {
6224 free(ctsio->kern_data_ptr, M_CTL);
6225 ctl_set_param_len_error(ctsio);
6226 ctl_done((union ctl_io *)ctsio);
6227 return (CTL_RETVAL_COMPLETE);
6231 * Set the IO_CONT flag, so that if this I/O gets passed to
6232 * ctl_config_write_done(), it'll get passed back to
6233 * ctl_do_mode_select() for further processing, or completion if
6236 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6237 ctsio->io_cont = ctl_do_mode_select;
6239 modepage_info = (union ctl_modepage_info *)
6240 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6241 memset(modepage_info, 0, sizeof(*modepage_info));
6242 modepage_info->header.len_left = param_len - header_size - bd_len;
6243 modepage_info->header.len_used = header_size + bd_len;
6245 return (ctl_do_mode_select((union ctl_io *)ctsio));
6249 ctl_mode_sense(struct ctl_scsiio *ctsio)
6251 struct ctl_lun *lun;
6252 int pc, page_code, dbd, llba, subpage;
6253 int alloc_len, page_len, header_len, total_len;
6254 struct scsi_mode_block_descr *block_desc;
6255 struct ctl_page_index *page_index;
6263 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6265 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6267 if (lun->be_lun->lun_type != T_DIRECT)
6272 switch (ctsio->cdb[0]) {
6273 case MODE_SENSE_6: {
6274 struct scsi_mode_sense_6 *cdb;
6276 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6278 header_len = sizeof(struct scsi_mode_hdr_6);
6279 if (cdb->byte2 & SMS_DBD)
6282 header_len += sizeof(struct scsi_mode_block_descr);
6284 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6285 page_code = cdb->page & SMS_PAGE_CODE;
6286 subpage = cdb->subpage;
6287 alloc_len = cdb->length;
6290 case MODE_SENSE_10: {
6291 struct scsi_mode_sense_10 *cdb;
6293 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6295 header_len = sizeof(struct scsi_mode_hdr_10);
6297 if (cdb->byte2 & SMS_DBD)
6300 header_len += sizeof(struct scsi_mode_block_descr);
6301 if (cdb->byte2 & SMS10_LLBAA)
6303 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6304 page_code = cdb->page & SMS_PAGE_CODE;
6305 subpage = cdb->subpage;
6306 alloc_len = scsi_2btoul(cdb->length);
6310 ctl_set_invalid_opcode(ctsio);
6311 ctl_done((union ctl_io *)ctsio);
6312 return (CTL_RETVAL_COMPLETE);
6313 break; /* NOTREACHED */
6317 * We have to make a first pass through to calculate the size of
6318 * the pages that match the user's query. Then we allocate enough
6319 * memory to hold it, and actually copy the data into the buffer.
6321 switch (page_code) {
6322 case SMS_ALL_PAGES_PAGE: {
6328 * At the moment, values other than 0 and 0xff here are
6329 * reserved according to SPC-3.
6331 if ((subpage != SMS_SUBPAGE_PAGE_0)
6332 && (subpage != SMS_SUBPAGE_ALL)) {
6333 ctl_set_invalid_field(ctsio,
6339 ctl_done((union ctl_io *)ctsio);
6340 return (CTL_RETVAL_COMPLETE);
6343 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6344 if ((control_dev != 0)
6345 && (lun->mode_pages.index[i].page_flags &
6346 CTL_PAGE_FLAG_DISK_ONLY))
6350 * We don't use this subpage if the user didn't
6351 * request all subpages.
6353 if ((lun->mode_pages.index[i].subpage != 0)
6354 && (subpage == SMS_SUBPAGE_PAGE_0))
6358 printf("found page %#x len %d\n",
6359 lun->mode_pages.index[i].page_code &
6361 lun->mode_pages.index[i].page_len);
6363 page_len += lun->mode_pages.index[i].page_len;
6372 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6373 /* Look for the right page code */
6374 if ((lun->mode_pages.index[i].page_code &
6375 SMPH_PC_MASK) != page_code)
6378 /* Look for the right subpage or the subpage wildcard*/
6379 if ((lun->mode_pages.index[i].subpage != subpage)
6380 && (subpage != SMS_SUBPAGE_ALL))
6383 /* Make sure the page is supported for this dev type */
6384 if ((control_dev != 0)
6385 && (lun->mode_pages.index[i].page_flags &
6386 CTL_PAGE_FLAG_DISK_ONLY))
6390 printf("found page %#x len %d\n",
6391 lun->mode_pages.index[i].page_code &
6393 lun->mode_pages.index[i].page_len);
6396 page_len += lun->mode_pages.index[i].page_len;
6399 if (page_len == 0) {
6400 ctl_set_invalid_field(ctsio,
6406 ctl_done((union ctl_io *)ctsio);
6407 return (CTL_RETVAL_COMPLETE);
6413 total_len = header_len + page_len;
6415 printf("header_len = %d, page_len = %d, total_len = %d\n",
6416 header_len, page_len, total_len);
6419 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6420 ctsio->kern_sg_entries = 0;
6421 ctsio->kern_data_resid = 0;
6422 ctsio->kern_rel_offset = 0;
6423 if (total_len < alloc_len) {
6424 ctsio->residual = alloc_len - total_len;
6425 ctsio->kern_data_len = total_len;
6426 ctsio->kern_total_len = total_len;
6428 ctsio->residual = 0;
6429 ctsio->kern_data_len = alloc_len;
6430 ctsio->kern_total_len = alloc_len;
6433 switch (ctsio->cdb[0]) {
6434 case MODE_SENSE_6: {
6435 struct scsi_mode_hdr_6 *header;
6437 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6439 header->datalen = MIN(total_len - 1, 254);
6440 if (control_dev == 0) {
6441 header->dev_specific = 0x10; /* DPOFUA */
6442 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6443 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6444 .eca_and_aen & SCP_SWP) != 0)
6445 header->dev_specific |= 0x80; /* WP */
6448 header->block_descr_len = 0;
6450 header->block_descr_len =
6451 sizeof(struct scsi_mode_block_descr);
6452 block_desc = (struct scsi_mode_block_descr *)&header[1];
6455 case MODE_SENSE_10: {
6456 struct scsi_mode_hdr_10 *header;
6459 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6461 datalen = MIN(total_len - 2, 65533);
6462 scsi_ulto2b(datalen, header->datalen);
6463 if (control_dev == 0) {
6464 header->dev_specific = 0x10; /* DPOFUA */
6465 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6466 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6467 .eca_and_aen & SCP_SWP) != 0)
6468 header->dev_specific |= 0x80; /* WP */
6471 scsi_ulto2b(0, header->block_descr_len);
6473 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6474 header->block_descr_len);
6475 block_desc = (struct scsi_mode_block_descr *)&header[1];
6479 panic("invalid CDB type %#x", ctsio->cdb[0]);
6480 break; /* NOTREACHED */
6484 * If we've got a disk, use its blocksize in the block
6485 * descriptor. Otherwise, just set it to 0.
6488 if (control_dev == 0)
6489 scsi_ulto3b(lun->be_lun->blocksize,
6490 block_desc->block_len);
6492 scsi_ulto3b(0, block_desc->block_len);
6495 switch (page_code) {
6496 case SMS_ALL_PAGES_PAGE: {
6499 data_used = header_len;
6500 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6501 struct ctl_page_index *page_index;
6503 page_index = &lun->mode_pages.index[i];
6505 if ((control_dev != 0)
6506 && (page_index->page_flags &
6507 CTL_PAGE_FLAG_DISK_ONLY))
6511 * We don't use this subpage if the user didn't
6512 * request all subpages. We already checked (above)
6513 * to make sure the user only specified a subpage
6514 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6516 if ((page_index->subpage != 0)
6517 && (subpage == SMS_SUBPAGE_PAGE_0))
6521 * Call the handler, if it exists, to update the
6522 * page to the latest values.
6524 if (page_index->sense_handler != NULL)
6525 page_index->sense_handler(ctsio, page_index,pc);
6527 memcpy(ctsio->kern_data_ptr + data_used,
6528 page_index->page_data +
6529 (page_index->page_len * pc),
6530 page_index->page_len);
6531 data_used += page_index->page_len;
6538 data_used = header_len;
6540 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6541 struct ctl_page_index *page_index;
6543 page_index = &lun->mode_pages.index[i];
6545 /* Look for the right page code */
6546 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6549 /* Look for the right subpage or the subpage wildcard*/
6550 if ((page_index->subpage != subpage)
6551 && (subpage != SMS_SUBPAGE_ALL))
6554 /* Make sure the page is supported for this dev type */
6555 if ((control_dev != 0)
6556 && (page_index->page_flags &
6557 CTL_PAGE_FLAG_DISK_ONLY))
6561 * Call the handler, if it exists, to update the
6562 * page to the latest values.
6564 if (page_index->sense_handler != NULL)
6565 page_index->sense_handler(ctsio, page_index,pc);
6567 memcpy(ctsio->kern_data_ptr + data_used,
6568 page_index->page_data +
6569 (page_index->page_len * pc),
6570 page_index->page_len);
6571 data_used += page_index->page_len;
6577 ctl_set_success(ctsio);
6578 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6579 ctsio->be_move_done = ctl_config_move_done;
6580 ctl_datamove((union ctl_io *)ctsio);
6581 return (CTL_RETVAL_COMPLETE);
6585 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6586 struct ctl_page_index *page_index,
6589 struct ctl_lun *lun;
6590 struct scsi_log_param_header *phdr;
6594 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6595 data = page_index->page_data;
6597 if (lun->backend->lun_attr != NULL &&
6598 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6600 phdr = (struct scsi_log_param_header *)data;
6601 scsi_ulto2b(0x0001, phdr->param_code);
6602 phdr->param_control = SLP_LBIN | SLP_LP;
6603 phdr->param_len = 8;
6604 data = (uint8_t *)(phdr + 1);
6605 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6606 data[4] = 0x02; /* per-pool */
6607 data += phdr->param_len;
6610 if (lun->backend->lun_attr != NULL &&
6611 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6613 phdr = (struct scsi_log_param_header *)data;
6614 scsi_ulto2b(0x0002, phdr->param_code);
6615 phdr->param_control = SLP_LBIN | SLP_LP;
6616 phdr->param_len = 8;
6617 data = (uint8_t *)(phdr + 1);
6618 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6619 data[4] = 0x01; /* per-LUN */
6620 data += phdr->param_len;
6623 if (lun->backend->lun_attr != NULL &&
6624 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6626 phdr = (struct scsi_log_param_header *)data;
6627 scsi_ulto2b(0x00f1, phdr->param_code);
6628 phdr->param_control = SLP_LBIN | SLP_LP;
6629 phdr->param_len = 8;
6630 data = (uint8_t *)(phdr + 1);
6631 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6632 data[4] = 0x02; /* per-pool */
6633 data += phdr->param_len;
6636 if (lun->backend->lun_attr != NULL &&
6637 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6639 phdr = (struct scsi_log_param_header *)data;
6640 scsi_ulto2b(0x00f2, phdr->param_code);
6641 phdr->param_control = SLP_LBIN | SLP_LP;
6642 phdr->param_len = 8;
6643 data = (uint8_t *)(phdr + 1);
6644 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6645 data[4] = 0x02; /* per-pool */
6646 data += phdr->param_len;
6649 page_index->page_len = data - page_index->page_data;
6654 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
6655 struct ctl_page_index *page_index,
6658 struct ctl_lun *lun;
6659 struct stat_page *data;
6660 uint64_t rn, wn, rb, wb;
6661 struct bintime rt, wt;
6664 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6665 data = (struct stat_page *)page_index->page_data;
6667 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
6668 data->sap.hdr.param_control = SLP_LBIN;
6669 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
6670 sizeof(struct scsi_log_param_header);
6671 rn = wn = rb = wb = 0;
6674 for (i = 0; i < CTL_MAX_PORTS; i++) {
6675 rn += lun->stats.ports[i].operations[CTL_STATS_READ];
6676 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE];
6677 rb += lun->stats.ports[i].bytes[CTL_STATS_READ];
6678 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE];
6679 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]);
6680 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]);
6682 scsi_u64to8b(rn, data->sap.read_num);
6683 scsi_u64to8b(wn, data->sap.write_num);
6684 if (lun->stats.blocksize > 0) {
6685 scsi_u64to8b(wb / lun->stats.blocksize,
6686 data->sap.recvieved_lba);
6687 scsi_u64to8b(rb / lun->stats.blocksize,
6688 data->sap.transmitted_lba);
6690 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000),
6691 data->sap.read_int);
6692 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000),
6693 data->sap.write_int);
6694 scsi_u64to8b(0, data->sap.weighted_num);
6695 scsi_u64to8b(0, data->sap.weighted_int);
6696 scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
6697 data->it.hdr.param_control = SLP_LBIN;
6698 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
6699 sizeof(struct scsi_log_param_header);
6701 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
6703 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
6704 data->it.hdr.param_control = SLP_LBIN;
6705 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
6706 sizeof(struct scsi_log_param_header);
6707 scsi_ulto4b(3, data->ti.exponent);
6708 scsi_ulto4b(1, data->ti.integer);
6710 page_index->page_len = sizeof(*data);
6715 ctl_log_sense(struct ctl_scsiio *ctsio)
6717 struct ctl_lun *lun;
6718 int i, pc, page_code, subpage;
6719 int alloc_len, total_len;
6720 struct ctl_page_index *page_index;
6721 struct scsi_log_sense *cdb;
6722 struct scsi_log_header *header;
6724 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6726 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6727 cdb = (struct scsi_log_sense *)ctsio->cdb;
6728 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6729 page_code = cdb->page & SLS_PAGE_CODE;
6730 subpage = cdb->subpage;
6731 alloc_len = scsi_2btoul(cdb->length);
6734 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6735 page_index = &lun->log_pages.index[i];
6737 /* Look for the right page code */
6738 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6741 /* Look for the right subpage or the subpage wildcard*/
6742 if (page_index->subpage != subpage)
6747 if (i >= CTL_NUM_LOG_PAGES) {
6748 ctl_set_invalid_field(ctsio,
6754 ctl_done((union ctl_io *)ctsio);
6755 return (CTL_RETVAL_COMPLETE);
6758 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6760 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6761 ctsio->kern_sg_entries = 0;
6762 ctsio->kern_data_resid = 0;
6763 ctsio->kern_rel_offset = 0;
6764 if (total_len < alloc_len) {
6765 ctsio->residual = alloc_len - total_len;
6766 ctsio->kern_data_len = total_len;
6767 ctsio->kern_total_len = total_len;
6769 ctsio->residual = 0;
6770 ctsio->kern_data_len = alloc_len;
6771 ctsio->kern_total_len = alloc_len;
6774 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
6775 header->page = page_index->page_code;
6776 if (page_index->subpage) {
6777 header->page |= SL_SPF;
6778 header->subpage = page_index->subpage;
6780 scsi_ulto2b(page_index->page_len, header->datalen);
6783 * Call the handler, if it exists, to update the
6784 * page to the latest values.
6786 if (page_index->sense_handler != NULL)
6787 page_index->sense_handler(ctsio, page_index, pc);
6789 memcpy(header + 1, page_index->page_data, page_index->page_len);
6791 ctl_set_success(ctsio);
6792 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6793 ctsio->be_move_done = ctl_config_move_done;
6794 ctl_datamove((union ctl_io *)ctsio);
6795 return (CTL_RETVAL_COMPLETE);
6799 ctl_read_capacity(struct ctl_scsiio *ctsio)
6801 struct scsi_read_capacity *cdb;
6802 struct scsi_read_capacity_data *data;
6803 struct ctl_lun *lun;
6806 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6808 cdb = (struct scsi_read_capacity *)ctsio->cdb;
6810 lba = scsi_4btoul(cdb->addr);
6811 if (((cdb->pmi & SRC_PMI) == 0)
6813 ctl_set_invalid_field(/*ctsio*/ ctsio,
6819 ctl_done((union ctl_io *)ctsio);
6820 return (CTL_RETVAL_COMPLETE);
6823 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6825 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6826 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6827 ctsio->residual = 0;
6828 ctsio->kern_data_len = sizeof(*data);
6829 ctsio->kern_total_len = sizeof(*data);
6830 ctsio->kern_data_resid = 0;
6831 ctsio->kern_rel_offset = 0;
6832 ctsio->kern_sg_entries = 0;
6835 * If the maximum LBA is greater than 0xfffffffe, the user must
6836 * issue a SERVICE ACTION IN (16) command, with the read capacity
6837 * serivce action set.
6839 if (lun->be_lun->maxlba > 0xfffffffe)
6840 scsi_ulto4b(0xffffffff, data->addr);
6842 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6845 * XXX KDM this may not be 512 bytes...
6847 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6849 ctl_set_success(ctsio);
6850 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6851 ctsio->be_move_done = ctl_config_move_done;
6852 ctl_datamove((union ctl_io *)ctsio);
6853 return (CTL_RETVAL_COMPLETE);
6857 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
6859 struct scsi_read_capacity_16 *cdb;
6860 struct scsi_read_capacity_data_long *data;
6861 struct ctl_lun *lun;
6865 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
6867 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
6869 alloc_len = scsi_4btoul(cdb->alloc_len);
6870 lba = scsi_8btou64(cdb->addr);
6872 if ((cdb->reladr & SRC16_PMI)
6874 ctl_set_invalid_field(/*ctsio*/ ctsio,
6880 ctl_done((union ctl_io *)ctsio);
6881 return (CTL_RETVAL_COMPLETE);
6884 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6886 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6887 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
6889 if (sizeof(*data) < alloc_len) {
6890 ctsio->residual = alloc_len - sizeof(*data);
6891 ctsio->kern_data_len = sizeof(*data);
6892 ctsio->kern_total_len = sizeof(*data);
6894 ctsio->residual = 0;
6895 ctsio->kern_data_len = alloc_len;
6896 ctsio->kern_total_len = alloc_len;
6898 ctsio->kern_data_resid = 0;
6899 ctsio->kern_rel_offset = 0;
6900 ctsio->kern_sg_entries = 0;
6902 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
6903 /* XXX KDM this may not be 512 bytes... */
6904 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6905 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
6906 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
6907 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
6908 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
6910 ctl_set_success(ctsio);
6911 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6912 ctsio->be_move_done = ctl_config_move_done;
6913 ctl_datamove((union ctl_io *)ctsio);
6914 return (CTL_RETVAL_COMPLETE);
6918 ctl_get_lba_status(struct ctl_scsiio *ctsio)
6920 struct scsi_get_lba_status *cdb;
6921 struct scsi_get_lba_status_data *data;
6922 struct ctl_lun *lun;
6923 struct ctl_lba_len_flags *lbalen;
6925 uint32_t alloc_len, total_len;
6928 CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
6930 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6931 cdb = (struct scsi_get_lba_status *)ctsio->cdb;
6932 lba = scsi_8btou64(cdb->addr);
6933 alloc_len = scsi_4btoul(cdb->alloc_len);
6935 if (lba > lun->be_lun->maxlba) {
6936 ctl_set_lba_out_of_range(ctsio);
6937 ctl_done((union ctl_io *)ctsio);
6938 return (CTL_RETVAL_COMPLETE);
6941 total_len = sizeof(*data) + sizeof(data->descr[0]);
6942 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6943 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
6945 if (total_len < alloc_len) {
6946 ctsio->residual = alloc_len - total_len;
6947 ctsio->kern_data_len = total_len;
6948 ctsio->kern_total_len = total_len;
6950 ctsio->residual = 0;
6951 ctsio->kern_data_len = alloc_len;
6952 ctsio->kern_total_len = alloc_len;
6954 ctsio->kern_data_resid = 0;
6955 ctsio->kern_rel_offset = 0;
6956 ctsio->kern_sg_entries = 0;
6958 /* Fill dummy data in case backend can't tell anything. */
6959 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
6960 scsi_u64to8b(lba, data->descr[0].addr);
6961 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
6962 data->descr[0].length);
6963 data->descr[0].status = 0; /* Mapped or unknown. */
6965 ctl_set_success(ctsio);
6966 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6967 ctsio->be_move_done = ctl_config_move_done;
6969 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
6971 lbalen->len = total_len;
6973 retval = lun->backend->config_read((union ctl_io *)ctsio);
6974 return (CTL_RETVAL_COMPLETE);
6978 ctl_read_defect(struct ctl_scsiio *ctsio)
6980 struct scsi_read_defect_data_10 *ccb10;
6981 struct scsi_read_defect_data_12 *ccb12;
6982 struct scsi_read_defect_data_hdr_10 *data10;
6983 struct scsi_read_defect_data_hdr_12 *data12;
6984 uint32_t alloc_len, data_len;
6987 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
6989 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
6990 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
6991 format = ccb10->format;
6992 alloc_len = scsi_2btoul(ccb10->alloc_length);
6993 data_len = sizeof(*data10);
6995 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
6996 format = ccb12->format;
6997 alloc_len = scsi_4btoul(ccb12->alloc_length);
6998 data_len = sizeof(*data12);
7000 if (alloc_len == 0) {
7001 ctl_set_success(ctsio);
7002 ctl_done((union ctl_io *)ctsio);
7003 return (CTL_RETVAL_COMPLETE);
7006 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7007 if (data_len < alloc_len) {
7008 ctsio->residual = alloc_len - data_len;
7009 ctsio->kern_data_len = data_len;
7010 ctsio->kern_total_len = data_len;
7012 ctsio->residual = 0;
7013 ctsio->kern_data_len = alloc_len;
7014 ctsio->kern_total_len = alloc_len;
7016 ctsio->kern_data_resid = 0;
7017 ctsio->kern_rel_offset = 0;
7018 ctsio->kern_sg_entries = 0;
7020 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7021 data10 = (struct scsi_read_defect_data_hdr_10 *)
7022 ctsio->kern_data_ptr;
7023 data10->format = format;
7024 scsi_ulto2b(0, data10->length);
7026 data12 = (struct scsi_read_defect_data_hdr_12 *)
7027 ctsio->kern_data_ptr;
7028 data12->format = format;
7029 scsi_ulto2b(0, data12->generation);
7030 scsi_ulto4b(0, data12->length);
7033 ctl_set_success(ctsio);
7034 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7035 ctsio->be_move_done = ctl_config_move_done;
7036 ctl_datamove((union ctl_io *)ctsio);
7037 return (CTL_RETVAL_COMPLETE);
7041 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7043 struct scsi_maintenance_in *cdb;
7045 int alloc_len, ext, total_len = 0, g, pc, pg, gs, os;
7046 int num_target_port_groups, num_target_ports;
7047 struct ctl_lun *lun;
7048 struct ctl_softc *softc;
7049 struct ctl_port *port;
7050 struct scsi_target_group_data *rtg_ptr;
7051 struct scsi_target_group_data_extended *rtg_ext_ptr;
7052 struct scsi_target_port_group_descriptor *tpg_desc;
7054 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7056 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7057 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7058 softc = lun->ctl_softc;
7060 retval = CTL_RETVAL_COMPLETE;
7062 switch (cdb->byte2 & STG_PDF_MASK) {
7063 case STG_PDF_LENGTH:
7066 case STG_PDF_EXTENDED:
7070 ctl_set_invalid_field(/*ctsio*/ ctsio,
7076 ctl_done((union ctl_io *)ctsio);
7080 if (softc->is_single)
7081 num_target_port_groups = 1;
7083 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
7084 num_target_ports = 0;
7085 mtx_lock(&softc->ctl_lock);
7086 STAILQ_FOREACH(port, &softc->port_list, links) {
7087 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7089 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7093 mtx_unlock(&softc->ctl_lock);
7096 total_len = sizeof(struct scsi_target_group_data_extended);
7098 total_len = sizeof(struct scsi_target_group_data);
7099 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7100 num_target_port_groups +
7101 sizeof(struct scsi_target_port_descriptor) * num_target_ports;
7103 alloc_len = scsi_4btoul(cdb->length);
7105 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7107 ctsio->kern_sg_entries = 0;
7109 if (total_len < alloc_len) {
7110 ctsio->residual = alloc_len - total_len;
7111 ctsio->kern_data_len = total_len;
7112 ctsio->kern_total_len = total_len;
7114 ctsio->residual = 0;
7115 ctsio->kern_data_len = alloc_len;
7116 ctsio->kern_total_len = alloc_len;
7118 ctsio->kern_data_resid = 0;
7119 ctsio->kern_rel_offset = 0;
7122 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7123 ctsio->kern_data_ptr;
7124 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7125 rtg_ext_ptr->format_type = 0x10;
7126 rtg_ext_ptr->implicit_transition_time = 0;
7127 tpg_desc = &rtg_ext_ptr->groups[0];
7129 rtg_ptr = (struct scsi_target_group_data *)
7130 ctsio->kern_data_ptr;
7131 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7132 tpg_desc = &rtg_ptr->groups[0];
7135 mtx_lock(&softc->ctl_lock);
7136 pg = softc->port_min / softc->port_cnt;
7137 if (softc->ha_link == CTL_HA_LINK_OFFLINE)
7138 gs = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7139 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN)
7140 gs = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7141 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY)
7142 gs = TPG_ASYMMETRIC_ACCESS_STANDBY;
7144 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7145 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7147 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7149 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7150 for (g = 0; g < num_target_port_groups; g++) {
7151 tpg_desc->pref_state = (g == pg) ? gs : os;
7152 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7153 TPG_U_SUP | TPG_T_SUP;
7154 scsi_ulto2b(g + 1, tpg_desc->target_port_group);
7155 tpg_desc->status = TPG_IMPLICIT;
7157 STAILQ_FOREACH(port, &softc->port_list, links) {
7158 if (port->targ_port < g * softc->port_cnt ||
7159 port->targ_port >= (g + 1) * softc->port_cnt)
7161 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7163 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7165 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7166 relative_target_port_identifier);
7169 tpg_desc->target_port_count = pc;
7170 tpg_desc = (struct scsi_target_port_group_descriptor *)
7171 &tpg_desc->descriptors[pc];
7173 mtx_unlock(&softc->ctl_lock);
7175 ctl_set_success(ctsio);
7176 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7177 ctsio->be_move_done = ctl_config_move_done;
7178 ctl_datamove((union ctl_io *)ctsio);
7183 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7185 struct ctl_lun *lun;
7186 struct scsi_report_supported_opcodes *cdb;
7187 const struct ctl_cmd_entry *entry, *sentry;
7188 struct scsi_report_supported_opcodes_all *all;
7189 struct scsi_report_supported_opcodes_descr *descr;
7190 struct scsi_report_supported_opcodes_one *one;
7192 int alloc_len, total_len;
7193 int opcode, service_action, i, j, num;
7195 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7197 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7198 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7200 retval = CTL_RETVAL_COMPLETE;
7202 opcode = cdb->requested_opcode;
7203 service_action = scsi_2btoul(cdb->requested_service_action);
7204 switch (cdb->options & RSO_OPTIONS_MASK) {
7205 case RSO_OPTIONS_ALL:
7207 for (i = 0; i < 256; i++) {
7208 entry = &ctl_cmd_table[i];
7209 if (entry->flags & CTL_CMD_FLAG_SA5) {
7210 for (j = 0; j < 32; j++) {
7211 sentry = &((const struct ctl_cmd_entry *)
7213 if (ctl_cmd_applicable(
7214 lun->be_lun->lun_type, sentry))
7218 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7223 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7224 num * sizeof(struct scsi_report_supported_opcodes_descr);
7226 case RSO_OPTIONS_OC:
7227 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7228 ctl_set_invalid_field(/*ctsio*/ ctsio,
7234 ctl_done((union ctl_io *)ctsio);
7235 return (CTL_RETVAL_COMPLETE);
7237 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7239 case RSO_OPTIONS_OC_SA:
7240 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7241 service_action >= 32) {
7242 ctl_set_invalid_field(/*ctsio*/ ctsio,
7248 ctl_done((union ctl_io *)ctsio);
7249 return (CTL_RETVAL_COMPLETE);
7251 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7254 ctl_set_invalid_field(/*ctsio*/ ctsio,
7260 ctl_done((union ctl_io *)ctsio);
7261 return (CTL_RETVAL_COMPLETE);
7264 alloc_len = scsi_4btoul(cdb->length);
7266 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7268 ctsio->kern_sg_entries = 0;
7270 if (total_len < alloc_len) {
7271 ctsio->residual = alloc_len - total_len;
7272 ctsio->kern_data_len = total_len;
7273 ctsio->kern_total_len = total_len;
7275 ctsio->residual = 0;
7276 ctsio->kern_data_len = alloc_len;
7277 ctsio->kern_total_len = alloc_len;
7279 ctsio->kern_data_resid = 0;
7280 ctsio->kern_rel_offset = 0;
7282 switch (cdb->options & RSO_OPTIONS_MASK) {
7283 case RSO_OPTIONS_ALL:
7284 all = (struct scsi_report_supported_opcodes_all *)
7285 ctsio->kern_data_ptr;
7287 for (i = 0; i < 256; i++) {
7288 entry = &ctl_cmd_table[i];
7289 if (entry->flags & CTL_CMD_FLAG_SA5) {
7290 for (j = 0; j < 32; j++) {
7291 sentry = &((const struct ctl_cmd_entry *)
7293 if (!ctl_cmd_applicable(
7294 lun->be_lun->lun_type, sentry))
7296 descr = &all->descr[num++];
7298 scsi_ulto2b(j, descr->service_action);
7299 descr->flags = RSO_SERVACTV;
7300 scsi_ulto2b(sentry->length,
7304 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7307 descr = &all->descr[num++];
7309 scsi_ulto2b(0, descr->service_action);
7311 scsi_ulto2b(entry->length, descr->cdb_length);
7315 num * sizeof(struct scsi_report_supported_opcodes_descr),
7318 case RSO_OPTIONS_OC:
7319 one = (struct scsi_report_supported_opcodes_one *)
7320 ctsio->kern_data_ptr;
7321 entry = &ctl_cmd_table[opcode];
7323 case RSO_OPTIONS_OC_SA:
7324 one = (struct scsi_report_supported_opcodes_one *)
7325 ctsio->kern_data_ptr;
7326 entry = &ctl_cmd_table[opcode];
7327 entry = &((const struct ctl_cmd_entry *)
7328 entry->execute)[service_action];
7330 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7332 scsi_ulto2b(entry->length, one->cdb_length);
7333 one->cdb_usage[0] = opcode;
7334 memcpy(&one->cdb_usage[1], entry->usage,
7341 ctl_set_success(ctsio);
7342 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7343 ctsio->be_move_done = ctl_config_move_done;
7344 ctl_datamove((union ctl_io *)ctsio);
7349 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7351 struct scsi_report_supported_tmf *cdb;
7352 struct scsi_report_supported_tmf_data *data;
7354 int alloc_len, total_len;
7356 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7358 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7360 retval = CTL_RETVAL_COMPLETE;
7362 total_len = sizeof(struct scsi_report_supported_tmf_data);
7363 alloc_len = scsi_4btoul(cdb->length);
7365 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7367 ctsio->kern_sg_entries = 0;
7369 if (total_len < alloc_len) {
7370 ctsio->residual = alloc_len - total_len;
7371 ctsio->kern_data_len = total_len;
7372 ctsio->kern_total_len = total_len;
7374 ctsio->residual = 0;
7375 ctsio->kern_data_len = alloc_len;
7376 ctsio->kern_total_len = alloc_len;
7378 ctsio->kern_data_resid = 0;
7379 ctsio->kern_rel_offset = 0;
7381 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr;
7382 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS |
7384 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS;
7386 ctl_set_success(ctsio);
7387 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7388 ctsio->be_move_done = ctl_config_move_done;
7389 ctl_datamove((union ctl_io *)ctsio);
7394 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7396 struct scsi_report_timestamp *cdb;
7397 struct scsi_report_timestamp_data *data;
7401 int alloc_len, total_len;
7403 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7405 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7407 retval = CTL_RETVAL_COMPLETE;
7409 total_len = sizeof(struct scsi_report_timestamp_data);
7410 alloc_len = scsi_4btoul(cdb->length);
7412 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7414 ctsio->kern_sg_entries = 0;
7416 if (total_len < alloc_len) {
7417 ctsio->residual = alloc_len - total_len;
7418 ctsio->kern_data_len = total_len;
7419 ctsio->kern_total_len = total_len;
7421 ctsio->residual = 0;
7422 ctsio->kern_data_len = alloc_len;
7423 ctsio->kern_total_len = alloc_len;
7425 ctsio->kern_data_resid = 0;
7426 ctsio->kern_rel_offset = 0;
7428 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7429 scsi_ulto2b(sizeof(*data) - 2, data->length);
7430 data->origin = RTS_ORIG_OUTSIDE;
7432 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7433 scsi_ulto4b(timestamp >> 16, data->timestamp);
7434 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7436 ctl_set_success(ctsio);
7437 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7438 ctsio->be_move_done = ctl_config_move_done;
7439 ctl_datamove((union ctl_io *)ctsio);
7444 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7446 struct scsi_per_res_in *cdb;
7447 int alloc_len, total_len = 0;
7448 /* struct scsi_per_res_in_rsrv in_data; */
7449 struct ctl_lun *lun;
7450 struct ctl_softc *softc;
7453 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7455 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7457 alloc_len = scsi_2btoul(cdb->length);
7459 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7460 softc = lun->ctl_softc;
7463 mtx_lock(&lun->lun_lock);
7464 switch (cdb->action) {
7465 case SPRI_RK: /* read keys */
7466 total_len = sizeof(struct scsi_per_res_in_keys) +
7468 sizeof(struct scsi_per_res_key);
7470 case SPRI_RR: /* read reservation */
7471 if (lun->flags & CTL_LUN_PR_RESERVED)
7472 total_len = sizeof(struct scsi_per_res_in_rsrv);
7474 total_len = sizeof(struct scsi_per_res_in_header);
7476 case SPRI_RC: /* report capabilities */
7477 total_len = sizeof(struct scsi_per_res_cap);
7479 case SPRI_RS: /* read full status */
7480 total_len = sizeof(struct scsi_per_res_in_header) +
7481 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7485 panic("Invalid PR type %x", cdb->action);
7487 mtx_unlock(&lun->lun_lock);
7489 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7491 if (total_len < alloc_len) {
7492 ctsio->residual = alloc_len - total_len;
7493 ctsio->kern_data_len = total_len;
7494 ctsio->kern_total_len = total_len;
7496 ctsio->residual = 0;
7497 ctsio->kern_data_len = alloc_len;
7498 ctsio->kern_total_len = alloc_len;
7501 ctsio->kern_data_resid = 0;
7502 ctsio->kern_rel_offset = 0;
7503 ctsio->kern_sg_entries = 0;
7505 mtx_lock(&lun->lun_lock);
7506 switch (cdb->action) {
7507 case SPRI_RK: { // read keys
7508 struct scsi_per_res_in_keys *res_keys;
7511 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7514 * We had to drop the lock to allocate our buffer, which
7515 * leaves time for someone to come in with another
7516 * persistent reservation. (That is unlikely, though,
7517 * since this should be the only persistent reservation
7518 * command active right now.)
7520 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7521 (lun->pr_key_count *
7522 sizeof(struct scsi_per_res_key)))){
7523 mtx_unlock(&lun->lun_lock);
7524 free(ctsio->kern_data_ptr, M_CTL);
7525 printf("%s: reservation length changed, retrying\n",
7530 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7532 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7533 lun->pr_key_count, res_keys->header.length);
7535 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) {
7536 if ((key = ctl_get_prkey(lun, i)) == 0)
7540 * We used lun->pr_key_count to calculate the
7541 * size to allocate. If it turns out the number of
7542 * initiators with the registered flag set is
7543 * larger than that (i.e. they haven't been kept in
7544 * sync), we've got a problem.
7546 if (key_count >= lun->pr_key_count) {
7550 scsi_u64to8b(key, res_keys->keys[key_count].key);
7555 case SPRI_RR: { // read reservation
7556 struct scsi_per_res_in_rsrv *res;
7557 int tmp_len, header_only;
7559 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7561 scsi_ulto4b(lun->PRGeneration, res->header.generation);
7563 if (lun->flags & CTL_LUN_PR_RESERVED)
7565 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7566 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7567 res->header.length);
7570 tmp_len = sizeof(struct scsi_per_res_in_header);
7571 scsi_ulto4b(0, res->header.length);
7576 * We had to drop the lock to allocate our buffer, which
7577 * leaves time for someone to come in with another
7578 * persistent reservation. (That is unlikely, though,
7579 * since this should be the only persistent reservation
7580 * command active right now.)
7582 if (tmp_len != total_len) {
7583 mtx_unlock(&lun->lun_lock);
7584 free(ctsio->kern_data_ptr, M_CTL);
7585 printf("%s: reservation status changed, retrying\n",
7591 * No reservation held, so we're done.
7593 if (header_only != 0)
7597 * If the registration is an All Registrants type, the key
7598 * is 0, since it doesn't really matter.
7600 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7601 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7602 res->data.reservation);
7604 res->data.scopetype = lun->res_type;
7607 case SPRI_RC: //report capabilities
7609 struct scsi_per_res_cap *res_cap;
7612 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7613 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7614 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5;
7615 type_mask = SPRI_TM_WR_EX_AR |
7621 scsi_ulto2b(type_mask, res_cap->type_mask);
7624 case SPRI_RS: { // read full status
7625 struct scsi_per_res_in_full *res_status;
7626 struct scsi_per_res_in_full_desc *res_desc;
7627 struct ctl_port *port;
7630 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7633 * We had to drop the lock to allocate our buffer, which
7634 * leaves time for someone to come in with another
7635 * persistent reservation. (That is unlikely, though,
7636 * since this should be the only persistent reservation
7637 * command active right now.)
7639 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7640 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7641 lun->pr_key_count)){
7642 mtx_unlock(&lun->lun_lock);
7643 free(ctsio->kern_data_ptr, M_CTL);
7644 printf("%s: reservation length changed, retrying\n",
7649 scsi_ulto4b(lun->PRGeneration, res_status->header.generation);
7651 res_desc = &res_status->desc[0];
7652 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7653 if ((key = ctl_get_prkey(lun, i)) == 0)
7656 scsi_u64to8b(key, res_desc->res_key.key);
7657 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7658 (lun->pr_res_idx == i ||
7659 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7660 res_desc->flags = SPRI_FULL_R_HOLDER;
7661 res_desc->scopetype = lun->res_type;
7663 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7664 res_desc->rel_trgt_port_id);
7666 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7668 len = ctl_create_iid(port,
7669 i % CTL_MAX_INIT_PER_PORT,
7670 res_desc->transport_id);
7671 scsi_ulto4b(len, res_desc->additional_length);
7672 res_desc = (struct scsi_per_res_in_full_desc *)
7673 &res_desc->transport_id[len];
7675 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7676 res_status->header.length);
7681 * This is a bug, because we just checked for this above,
7682 * and should have returned an error.
7684 panic("Invalid PR type %x", cdb->action);
7685 break; /* NOTREACHED */
7687 mtx_unlock(&lun->lun_lock);
7689 ctl_set_success(ctsio);
7690 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7691 ctsio->be_move_done = ctl_config_move_done;
7692 ctl_datamove((union ctl_io *)ctsio);
7693 return (CTL_RETVAL_COMPLETE);
7697 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7701 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7702 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7703 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7704 struct scsi_per_res_out_parms* param)
7706 union ctl_ha_msg persis_io;
7709 mtx_lock(&lun->lun_lock);
7710 if (sa_res_key == 0) {
7711 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7712 /* validate scope and type */
7713 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7715 mtx_unlock(&lun->lun_lock);
7716 ctl_set_invalid_field(/*ctsio*/ ctsio,
7722 ctl_done((union ctl_io *)ctsio);
7726 if (type>8 || type==2 || type==4 || type==0) {
7727 mtx_unlock(&lun->lun_lock);
7728 ctl_set_invalid_field(/*ctsio*/ ctsio,
7734 ctl_done((union ctl_io *)ctsio);
7739 * Unregister everybody else and build UA for
7742 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7743 if (i == residx || ctl_get_prkey(lun, i) == 0)
7746 ctl_clr_prkey(lun, i);
7747 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7749 lun->pr_key_count = 1;
7750 lun->res_type = type;
7751 if (lun->res_type != SPR_TYPE_WR_EX_AR
7752 && lun->res_type != SPR_TYPE_EX_AC_AR)
7753 lun->pr_res_idx = residx;
7754 lun->PRGeneration++;
7755 mtx_unlock(&lun->lun_lock);
7757 /* send msg to other side */
7758 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7759 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7760 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7761 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7762 persis_io.pr.pr_info.res_type = type;
7763 memcpy(persis_io.pr.pr_info.sa_res_key,
7764 param->serv_act_res_key,
7765 sizeof(param->serv_act_res_key));
7766 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7767 sizeof(persis_io.pr), M_WAITOK);
7769 /* not all registrants */
7770 mtx_unlock(&lun->lun_lock);
7771 free(ctsio->kern_data_ptr, M_CTL);
7772 ctl_set_invalid_field(ctsio,
7778 ctl_done((union ctl_io *)ctsio);
7781 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7782 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7785 if (res_key == sa_res_key) {
7788 * The spec implies this is not good but doesn't
7789 * say what to do. There are two choices either
7790 * generate a res conflict or check condition
7791 * with illegal field in parameter data. Since
7792 * that is what is done when the sa_res_key is
7793 * zero I'll take that approach since this has
7794 * to do with the sa_res_key.
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);
7808 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7809 if (ctl_get_prkey(lun, i) != sa_res_key)
7813 ctl_clr_prkey(lun, i);
7814 lun->pr_key_count--;
7815 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7818 mtx_unlock(&lun->lun_lock);
7819 free(ctsio->kern_data_ptr, M_CTL);
7820 ctl_set_reservation_conflict(ctsio);
7821 ctl_done((union ctl_io *)ctsio);
7822 return (CTL_RETVAL_COMPLETE);
7824 lun->PRGeneration++;
7825 mtx_unlock(&lun->lun_lock);
7827 /* send msg to other side */
7828 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7829 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7830 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7831 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7832 persis_io.pr.pr_info.res_type = type;
7833 memcpy(persis_io.pr.pr_info.sa_res_key,
7834 param->serv_act_res_key,
7835 sizeof(param->serv_act_res_key));
7836 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7837 sizeof(persis_io.pr), M_WAITOK);
7839 /* Reserved but not all registrants */
7840 /* sa_res_key is res holder */
7841 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
7842 /* validate scope and type */
7843 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7845 mtx_unlock(&lun->lun_lock);
7846 ctl_set_invalid_field(/*ctsio*/ ctsio,
7852 ctl_done((union ctl_io *)ctsio);
7856 if (type>8 || type==2 || type==4 || type==0) {
7857 mtx_unlock(&lun->lun_lock);
7858 ctl_set_invalid_field(/*ctsio*/ ctsio,
7864 ctl_done((union ctl_io *)ctsio);
7870 * if sa_res_key != res_key remove all
7871 * registrants w/sa_res_key and generate UA
7872 * for these registrants(Registrations
7873 * Preempted) if it wasn't an exclusive
7874 * reservation generate UA(Reservations
7875 * Preempted) for all other registered nexuses
7876 * if the type has changed. Establish the new
7877 * reservation and holder. If res_key and
7878 * sa_res_key are the same do the above
7879 * except don't unregister the res holder.
7882 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7883 if (i == residx || ctl_get_prkey(lun, i) == 0)
7886 if (sa_res_key == ctl_get_prkey(lun, i)) {
7887 ctl_clr_prkey(lun, i);
7888 lun->pr_key_count--;
7889 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7890 } else if (type != lun->res_type
7891 && (lun->res_type == SPR_TYPE_WR_EX_RO
7892 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
7893 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
7896 lun->res_type = type;
7897 if (lun->res_type != SPR_TYPE_WR_EX_AR
7898 && lun->res_type != SPR_TYPE_EX_AC_AR)
7899 lun->pr_res_idx = residx;
7901 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
7902 lun->PRGeneration++;
7903 mtx_unlock(&lun->lun_lock);
7905 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7906 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7907 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7908 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7909 persis_io.pr.pr_info.res_type = type;
7910 memcpy(persis_io.pr.pr_info.sa_res_key,
7911 param->serv_act_res_key,
7912 sizeof(param->serv_act_res_key));
7913 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7914 sizeof(persis_io.pr), M_WAITOK);
7917 * sa_res_key is not the res holder just
7918 * remove registrants
7922 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7923 if (sa_res_key != ctl_get_prkey(lun, i))
7927 ctl_clr_prkey(lun, i);
7928 lun->pr_key_count--;
7929 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7933 mtx_unlock(&lun->lun_lock);
7934 free(ctsio->kern_data_ptr, M_CTL);
7935 ctl_set_reservation_conflict(ctsio);
7936 ctl_done((union ctl_io *)ctsio);
7939 lun->PRGeneration++;
7940 mtx_unlock(&lun->lun_lock);
7942 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7943 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7944 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7945 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7946 persis_io.pr.pr_info.res_type = type;
7947 memcpy(persis_io.pr.pr_info.sa_res_key,
7948 param->serv_act_res_key,
7949 sizeof(param->serv_act_res_key));
7950 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7951 sizeof(persis_io.pr), M_WAITOK);
7958 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
7960 uint64_t sa_res_key;
7963 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
7965 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7966 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
7967 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
7968 if (sa_res_key == 0) {
7970 * Unregister everybody else and build UA for
7973 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7974 if (i == msg->pr.pr_info.residx ||
7975 ctl_get_prkey(lun, i) == 0)
7978 ctl_clr_prkey(lun, i);
7979 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7982 lun->pr_key_count = 1;
7983 lun->res_type = msg->pr.pr_info.res_type;
7984 if (lun->res_type != SPR_TYPE_WR_EX_AR
7985 && lun->res_type != SPR_TYPE_EX_AC_AR)
7986 lun->pr_res_idx = msg->pr.pr_info.residx;
7988 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7989 if (sa_res_key == ctl_get_prkey(lun, i))
7992 ctl_clr_prkey(lun, i);
7993 lun->pr_key_count--;
7994 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7998 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7999 if (i == msg->pr.pr_info.residx ||
8000 ctl_get_prkey(lun, i) == 0)
8003 if (sa_res_key == ctl_get_prkey(lun, i)) {
8004 ctl_clr_prkey(lun, i);
8005 lun->pr_key_count--;
8006 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8007 } else if (msg->pr.pr_info.res_type != lun->res_type
8008 && (lun->res_type == SPR_TYPE_WR_EX_RO
8009 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
8010 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8013 lun->res_type = msg->pr.pr_info.res_type;
8014 if (lun->res_type != SPR_TYPE_WR_EX_AR
8015 && lun->res_type != SPR_TYPE_EX_AC_AR)
8016 lun->pr_res_idx = msg->pr.pr_info.residx;
8018 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8020 lun->PRGeneration++;
8026 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8029 u_int32_t param_len;
8030 struct scsi_per_res_out *cdb;
8031 struct ctl_lun *lun;
8032 struct scsi_per_res_out_parms* param;
8033 struct ctl_softc *softc;
8035 uint64_t res_key, sa_res_key, key;
8037 union ctl_ha_msg persis_io;
8040 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8042 retval = CTL_RETVAL_COMPLETE;
8044 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8045 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8046 softc = lun->ctl_softc;
8049 * We only support whole-LUN scope. The scope & type are ignored for
8050 * register, register and ignore existing key and clear.
8051 * We sometimes ignore scope and type on preempts too!!
8052 * Verify reservation type here as well.
8054 type = cdb->scope_type & SPR_TYPE_MASK;
8055 if ((cdb->action == SPRO_RESERVE)
8056 || (cdb->action == SPRO_RELEASE)) {
8057 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8058 ctl_set_invalid_field(/*ctsio*/ ctsio,
8064 ctl_done((union ctl_io *)ctsio);
8065 return (CTL_RETVAL_COMPLETE);
8068 if (type>8 || type==2 || type==4 || type==0) {
8069 ctl_set_invalid_field(/*ctsio*/ ctsio,
8075 ctl_done((union ctl_io *)ctsio);
8076 return (CTL_RETVAL_COMPLETE);
8080 param_len = scsi_4btoul(cdb->length);
8082 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8083 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8084 ctsio->kern_data_len = param_len;
8085 ctsio->kern_total_len = param_len;
8086 ctsio->kern_data_resid = 0;
8087 ctsio->kern_rel_offset = 0;
8088 ctsio->kern_sg_entries = 0;
8089 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8090 ctsio->be_move_done = ctl_config_move_done;
8091 ctl_datamove((union ctl_io *)ctsio);
8093 return (CTL_RETVAL_COMPLETE);
8096 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8098 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8099 res_key = scsi_8btou64(param->res_key.key);
8100 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8103 * Validate the reservation key here except for SPRO_REG_IGNO
8104 * This must be done for all other service actions
8106 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8107 mtx_lock(&lun->lun_lock);
8108 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8109 if (res_key != key) {
8111 * The current key passed in doesn't match
8112 * the one the initiator previously
8115 mtx_unlock(&lun->lun_lock);
8116 free(ctsio->kern_data_ptr, M_CTL);
8117 ctl_set_reservation_conflict(ctsio);
8118 ctl_done((union ctl_io *)ctsio);
8119 return (CTL_RETVAL_COMPLETE);
8121 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8123 * We are not registered
8125 mtx_unlock(&lun->lun_lock);
8126 free(ctsio->kern_data_ptr, M_CTL);
8127 ctl_set_reservation_conflict(ctsio);
8128 ctl_done((union ctl_io *)ctsio);
8129 return (CTL_RETVAL_COMPLETE);
8130 } else if (res_key != 0) {
8132 * We are not registered and trying to register but
8133 * the register key isn't zero.
8135 mtx_unlock(&lun->lun_lock);
8136 free(ctsio->kern_data_ptr, M_CTL);
8137 ctl_set_reservation_conflict(ctsio);
8138 ctl_done((union ctl_io *)ctsio);
8139 return (CTL_RETVAL_COMPLETE);
8141 mtx_unlock(&lun->lun_lock);
8144 switch (cdb->action & SPRO_ACTION_MASK) {
8146 case SPRO_REG_IGNO: {
8149 printf("Registration received\n");
8153 * We don't support any of these options, as we report in
8154 * the read capabilities request (see
8155 * ctl_persistent_reserve_in(), above).
8157 if ((param->flags & SPR_SPEC_I_PT)
8158 || (param->flags & SPR_ALL_TG_PT)
8159 || (param->flags & SPR_APTPL)) {
8162 if (param->flags & SPR_APTPL)
8164 else if (param->flags & SPR_ALL_TG_PT)
8166 else /* SPR_SPEC_I_PT */
8169 free(ctsio->kern_data_ptr, M_CTL);
8170 ctl_set_invalid_field(ctsio,
8176 ctl_done((union ctl_io *)ctsio);
8177 return (CTL_RETVAL_COMPLETE);
8180 mtx_lock(&lun->lun_lock);
8183 * The initiator wants to clear the
8186 if (sa_res_key == 0) {
8188 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8189 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8190 && ctl_get_prkey(lun, residx) == 0)) {
8191 mtx_unlock(&lun->lun_lock);
8195 ctl_clr_prkey(lun, residx);
8196 lun->pr_key_count--;
8198 if (residx == lun->pr_res_idx) {
8199 lun->flags &= ~CTL_LUN_PR_RESERVED;
8200 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8202 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8203 || lun->res_type == SPR_TYPE_EX_AC_RO)
8204 && lun->pr_key_count) {
8206 * If the reservation is a registrants
8207 * only type we need to generate a UA
8208 * for other registered inits. The
8209 * sense code should be RESERVATIONS
8213 for (i = softc->init_min; i < softc->init_max; i++){
8214 if (ctl_get_prkey(lun, i) == 0)
8217 CTL_UA_RES_RELEASE);
8221 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8222 if (lun->pr_key_count==0) {
8223 lun->flags &= ~CTL_LUN_PR_RESERVED;
8225 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8228 lun->PRGeneration++;
8229 mtx_unlock(&lun->lun_lock);
8231 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8232 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8233 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8234 persis_io.pr.pr_info.residx = residx;
8235 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8236 sizeof(persis_io.pr), M_WAITOK);
8237 } else /* sa_res_key != 0 */ {
8240 * If we aren't registered currently then increment
8241 * the key count and set the registered flag.
8243 ctl_alloc_prkey(lun, residx);
8244 if (ctl_get_prkey(lun, residx) == 0)
8245 lun->pr_key_count++;
8246 ctl_set_prkey(lun, residx, sa_res_key);
8247 lun->PRGeneration++;
8248 mtx_unlock(&lun->lun_lock);
8250 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8251 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8252 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8253 persis_io.pr.pr_info.residx = residx;
8254 memcpy(persis_io.pr.pr_info.sa_res_key,
8255 param->serv_act_res_key,
8256 sizeof(param->serv_act_res_key));
8257 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8258 sizeof(persis_io.pr), M_WAITOK);
8265 printf("Reserve executed type %d\n", type);
8267 mtx_lock(&lun->lun_lock);
8268 if (lun->flags & CTL_LUN_PR_RESERVED) {
8270 * if this isn't the reservation holder and it's
8271 * not a "all registrants" type or if the type is
8272 * different then we have a conflict
8274 if ((lun->pr_res_idx != residx
8275 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8276 || lun->res_type != type) {
8277 mtx_unlock(&lun->lun_lock);
8278 free(ctsio->kern_data_ptr, M_CTL);
8279 ctl_set_reservation_conflict(ctsio);
8280 ctl_done((union ctl_io *)ctsio);
8281 return (CTL_RETVAL_COMPLETE);
8283 mtx_unlock(&lun->lun_lock);
8284 } else /* create a reservation */ {
8286 * If it's not an "all registrants" type record
8287 * reservation holder
8289 if (type != SPR_TYPE_WR_EX_AR
8290 && type != SPR_TYPE_EX_AC_AR)
8291 lun->pr_res_idx = residx; /* Res holder */
8293 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8295 lun->flags |= CTL_LUN_PR_RESERVED;
8296 lun->res_type = type;
8298 mtx_unlock(&lun->lun_lock);
8300 /* send msg to other side */
8301 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8302 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8303 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8304 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8305 persis_io.pr.pr_info.res_type = type;
8306 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8307 sizeof(persis_io.pr), M_WAITOK);
8312 mtx_lock(&lun->lun_lock);
8313 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8314 /* No reservation exists return good status */
8315 mtx_unlock(&lun->lun_lock);
8319 * Is this nexus a reservation holder?
8321 if (lun->pr_res_idx != residx
8322 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8324 * not a res holder return good status but
8327 mtx_unlock(&lun->lun_lock);
8331 if (lun->res_type != type) {
8332 mtx_unlock(&lun->lun_lock);
8333 free(ctsio->kern_data_ptr, M_CTL);
8334 ctl_set_illegal_pr_release(ctsio);
8335 ctl_done((union ctl_io *)ctsio);
8336 return (CTL_RETVAL_COMPLETE);
8339 /* okay to release */
8340 lun->flags &= ~CTL_LUN_PR_RESERVED;
8341 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8345 * if this isn't an exclusive access
8346 * res generate UA for all other
8349 if (type != SPR_TYPE_EX_AC
8350 && type != SPR_TYPE_WR_EX) {
8351 for (i = softc->init_min; i < softc->init_max; i++) {
8352 if (i == residx || ctl_get_prkey(lun, i) == 0)
8354 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8357 mtx_unlock(&lun->lun_lock);
8359 /* Send msg to other side */
8360 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8361 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8362 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8363 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8364 sizeof(persis_io.pr), M_WAITOK);
8368 /* send msg to other side */
8370 mtx_lock(&lun->lun_lock);
8371 lun->flags &= ~CTL_LUN_PR_RESERVED;
8373 lun->pr_key_count = 0;
8374 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8376 ctl_clr_prkey(lun, residx);
8377 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8378 if (ctl_get_prkey(lun, i) != 0) {
8379 ctl_clr_prkey(lun, i);
8380 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8382 lun->PRGeneration++;
8383 mtx_unlock(&lun->lun_lock);
8385 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8386 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8387 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8388 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8389 sizeof(persis_io.pr), M_WAITOK);
8393 case SPRO_PRE_ABO: {
8396 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8397 residx, ctsio, cdb, param);
8399 return (CTL_RETVAL_COMPLETE);
8403 panic("Invalid PR type %x", cdb->action);
8407 free(ctsio->kern_data_ptr, M_CTL);
8408 ctl_set_success(ctsio);
8409 ctl_done((union ctl_io *)ctsio);
8415 * This routine is for handling a message from the other SC pertaining to
8416 * persistent reserve out. All the error checking will have been done
8417 * so only perorming the action need be done here to keep the two
8421 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8423 struct ctl_softc *softc = control_softc;
8424 struct ctl_lun *lun;
8426 uint32_t residx, targ_lun;
8428 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8429 mtx_lock(&softc->ctl_lock);
8430 if ((targ_lun >= CTL_MAX_LUNS) ||
8431 ((lun = softc->ctl_luns[targ_lun]) == NULL)) {
8432 mtx_unlock(&softc->ctl_lock);
8435 mtx_lock(&lun->lun_lock);
8436 mtx_unlock(&softc->ctl_lock);
8437 if (lun->flags & CTL_LUN_DISABLED) {
8438 mtx_unlock(&lun->lun_lock);
8441 residx = ctl_get_initindex(&msg->hdr.nexus);
8442 switch(msg->pr.pr_info.action) {
8443 case CTL_PR_REG_KEY:
8444 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8445 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8446 lun->pr_key_count++;
8447 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8448 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8449 lun->PRGeneration++;
8452 case CTL_PR_UNREG_KEY:
8453 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8454 lun->pr_key_count--;
8456 /* XXX Need to see if the reservation has been released */
8457 /* if so do we need to generate UA? */
8458 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8459 lun->flags &= ~CTL_LUN_PR_RESERVED;
8460 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8462 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8463 || lun->res_type == SPR_TYPE_EX_AC_RO)
8464 && lun->pr_key_count) {
8466 * If the reservation is a registrants
8467 * only type we need to generate a UA
8468 * for other registered inits. The
8469 * sense code should be RESERVATIONS
8473 for (i = softc->init_min; i < softc->init_max; i++) {
8474 if (ctl_get_prkey(lun, i) == 0)
8477 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8481 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8482 if (lun->pr_key_count==0) {
8483 lun->flags &= ~CTL_LUN_PR_RESERVED;
8485 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8488 lun->PRGeneration++;
8491 case CTL_PR_RESERVE:
8492 lun->flags |= CTL_LUN_PR_RESERVED;
8493 lun->res_type = msg->pr.pr_info.res_type;
8494 lun->pr_res_idx = msg->pr.pr_info.residx;
8498 case CTL_PR_RELEASE:
8500 * if this isn't an exclusive access res generate UA for all
8501 * other registrants.
8503 if (lun->res_type != SPR_TYPE_EX_AC
8504 && lun->res_type != SPR_TYPE_WR_EX) {
8505 for (i = softc->init_min; i < softc->init_max; i++)
8506 if (i == residx || ctl_get_prkey(lun, i) == 0)
8508 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8511 lun->flags &= ~CTL_LUN_PR_RESERVED;
8512 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8516 case CTL_PR_PREEMPT:
8517 ctl_pro_preempt_other(lun, msg);
8520 lun->flags &= ~CTL_LUN_PR_RESERVED;
8522 lun->pr_key_count = 0;
8523 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8525 for (i=0; i < CTL_MAX_INITIATORS; i++) {
8526 if (ctl_get_prkey(lun, i) == 0)
8528 ctl_clr_prkey(lun, i);
8529 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8531 lun->PRGeneration++;
8535 mtx_unlock(&lun->lun_lock);
8539 ctl_read_write(struct ctl_scsiio *ctsio)
8541 struct ctl_lun *lun;
8542 struct ctl_lba_len_flags *lbalen;
8544 uint32_t num_blocks;
8548 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8550 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8553 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8554 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8555 switch (ctsio->cdb[0]) {
8558 struct scsi_rw_6 *cdb;
8560 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8562 lba = scsi_3btoul(cdb->addr);
8563 /* only 5 bits are valid in the most significant address byte */
8565 num_blocks = cdb->length;
8567 * This is correct according to SBC-2.
8569 if (num_blocks == 0)
8575 struct scsi_rw_10 *cdb;
8577 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8578 if (cdb->byte2 & SRW10_FUA)
8579 flags |= CTL_LLF_FUA;
8580 if (cdb->byte2 & SRW10_DPO)
8581 flags |= CTL_LLF_DPO;
8582 lba = scsi_4btoul(cdb->addr);
8583 num_blocks = scsi_2btoul(cdb->length);
8586 case WRITE_VERIFY_10: {
8587 struct scsi_write_verify_10 *cdb;
8589 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8590 flags |= CTL_LLF_FUA;
8591 if (cdb->byte2 & SWV_DPO)
8592 flags |= CTL_LLF_DPO;
8593 lba = scsi_4btoul(cdb->addr);
8594 num_blocks = scsi_2btoul(cdb->length);
8599 struct scsi_rw_12 *cdb;
8601 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8602 if (cdb->byte2 & SRW12_FUA)
8603 flags |= CTL_LLF_FUA;
8604 if (cdb->byte2 & SRW12_DPO)
8605 flags |= CTL_LLF_DPO;
8606 lba = scsi_4btoul(cdb->addr);
8607 num_blocks = scsi_4btoul(cdb->length);
8610 case WRITE_VERIFY_12: {
8611 struct scsi_write_verify_12 *cdb;
8613 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8614 flags |= CTL_LLF_FUA;
8615 if (cdb->byte2 & SWV_DPO)
8616 flags |= CTL_LLF_DPO;
8617 lba = scsi_4btoul(cdb->addr);
8618 num_blocks = scsi_4btoul(cdb->length);
8623 struct scsi_rw_16 *cdb;
8625 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8626 if (cdb->byte2 & SRW12_FUA)
8627 flags |= CTL_LLF_FUA;
8628 if (cdb->byte2 & SRW12_DPO)
8629 flags |= CTL_LLF_DPO;
8630 lba = scsi_8btou64(cdb->addr);
8631 num_blocks = scsi_4btoul(cdb->length);
8634 case WRITE_ATOMIC_16: {
8635 struct scsi_write_atomic_16 *cdb;
8637 if (lun->be_lun->atomicblock == 0) {
8638 ctl_set_invalid_opcode(ctsio);
8639 ctl_done((union ctl_io *)ctsio);
8640 return (CTL_RETVAL_COMPLETE);
8643 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb;
8644 if (cdb->byte2 & SRW12_FUA)
8645 flags |= CTL_LLF_FUA;
8646 if (cdb->byte2 & SRW12_DPO)
8647 flags |= CTL_LLF_DPO;
8648 lba = scsi_8btou64(cdb->addr);
8649 num_blocks = scsi_2btoul(cdb->length);
8650 if (num_blocks > lun->be_lun->atomicblock) {
8651 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8652 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8654 ctl_done((union ctl_io *)ctsio);
8655 return (CTL_RETVAL_COMPLETE);
8659 case WRITE_VERIFY_16: {
8660 struct scsi_write_verify_16 *cdb;
8662 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8663 flags |= CTL_LLF_FUA;
8664 if (cdb->byte2 & SWV_DPO)
8665 flags |= CTL_LLF_DPO;
8666 lba = scsi_8btou64(cdb->addr);
8667 num_blocks = scsi_4btoul(cdb->length);
8672 * We got a command we don't support. This shouldn't
8673 * happen, commands should be filtered out above us.
8675 ctl_set_invalid_opcode(ctsio);
8676 ctl_done((union ctl_io *)ctsio);
8678 return (CTL_RETVAL_COMPLETE);
8679 break; /* NOTREACHED */
8683 * The first check is to make sure we're in bounds, the second
8684 * check is to catch wrap-around problems. If the lba + num blocks
8685 * is less than the lba, then we've wrapped around and the block
8686 * range is invalid anyway.
8688 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8689 || ((lba + num_blocks) < lba)) {
8690 ctl_set_lba_out_of_range(ctsio);
8691 ctl_done((union ctl_io *)ctsio);
8692 return (CTL_RETVAL_COMPLETE);
8696 * According to SBC-3, a transfer length of 0 is not an error.
8697 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8698 * translates to 256 blocks for those commands.
8700 if (num_blocks == 0) {
8701 ctl_set_success(ctsio);
8702 ctl_done((union ctl_io *)ctsio);
8703 return (CTL_RETVAL_COMPLETE);
8706 /* Set FUA and/or DPO if caches are disabled. */
8708 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8710 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8712 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8714 flags |= CTL_LLF_FUA;
8717 lbalen = (struct ctl_lba_len_flags *)
8718 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8720 lbalen->len = num_blocks;
8721 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8723 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8724 ctsio->kern_rel_offset = 0;
8726 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8728 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8733 ctl_cnw_cont(union ctl_io *io)
8735 struct ctl_scsiio *ctsio;
8736 struct ctl_lun *lun;
8737 struct ctl_lba_len_flags *lbalen;
8740 ctsio = &io->scsiio;
8741 ctsio->io_hdr.status = CTL_STATUS_NONE;
8742 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8743 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8744 lbalen = (struct ctl_lba_len_flags *)
8745 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8746 lbalen->flags &= ~CTL_LLF_COMPARE;
8747 lbalen->flags |= CTL_LLF_WRITE;
8749 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8750 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8755 ctl_cnw(struct ctl_scsiio *ctsio)
8757 struct ctl_lun *lun;
8758 struct ctl_lba_len_flags *lbalen;
8760 uint32_t num_blocks;
8763 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8765 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8768 switch (ctsio->cdb[0]) {
8769 case COMPARE_AND_WRITE: {
8770 struct scsi_compare_and_write *cdb;
8772 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8773 if (cdb->byte2 & SRW10_FUA)
8774 flags |= CTL_LLF_FUA;
8775 if (cdb->byte2 & SRW10_DPO)
8776 flags |= CTL_LLF_DPO;
8777 lba = scsi_8btou64(cdb->addr);
8778 num_blocks = cdb->length;
8783 * We got a command we don't support. This shouldn't
8784 * happen, commands should be filtered out above us.
8786 ctl_set_invalid_opcode(ctsio);
8787 ctl_done((union ctl_io *)ctsio);
8789 return (CTL_RETVAL_COMPLETE);
8790 break; /* NOTREACHED */
8794 * The first check is to make sure we're in bounds, the second
8795 * check is to catch wrap-around problems. If the lba + num blocks
8796 * is less than the lba, then we've wrapped around and the block
8797 * range is invalid anyway.
8799 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8800 || ((lba + num_blocks) < lba)) {
8801 ctl_set_lba_out_of_range(ctsio);
8802 ctl_done((union ctl_io *)ctsio);
8803 return (CTL_RETVAL_COMPLETE);
8807 * According to SBC-3, a transfer length of 0 is not an error.
8809 if (num_blocks == 0) {
8810 ctl_set_success(ctsio);
8811 ctl_done((union ctl_io *)ctsio);
8812 return (CTL_RETVAL_COMPLETE);
8815 /* Set FUA if write cache is disabled. */
8816 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8818 flags |= CTL_LLF_FUA;
8820 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
8821 ctsio->kern_rel_offset = 0;
8824 * Set the IO_CONT flag, so that if this I/O gets passed to
8825 * ctl_data_submit_done(), it'll get passed back to
8826 * ctl_ctl_cnw_cont() for further processing.
8828 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
8829 ctsio->io_cont = ctl_cnw_cont;
8831 lbalen = (struct ctl_lba_len_flags *)
8832 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8834 lbalen->len = num_blocks;
8835 lbalen->flags = CTL_LLF_COMPARE | flags;
8837 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
8838 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8843 ctl_verify(struct ctl_scsiio *ctsio)
8845 struct ctl_lun *lun;
8846 struct ctl_lba_len_flags *lbalen;
8848 uint32_t num_blocks;
8852 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8854 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
8857 flags = CTL_LLF_FUA;
8858 switch (ctsio->cdb[0]) {
8860 struct scsi_verify_10 *cdb;
8862 cdb = (struct scsi_verify_10 *)ctsio->cdb;
8863 if (cdb->byte2 & SVFY_BYTCHK)
8865 if (cdb->byte2 & SVFY_DPO)
8866 flags |= CTL_LLF_DPO;
8867 lba = scsi_4btoul(cdb->addr);
8868 num_blocks = scsi_2btoul(cdb->length);
8872 struct scsi_verify_12 *cdb;
8874 cdb = (struct scsi_verify_12 *)ctsio->cdb;
8875 if (cdb->byte2 & SVFY_BYTCHK)
8877 if (cdb->byte2 & SVFY_DPO)
8878 flags |= CTL_LLF_DPO;
8879 lba = scsi_4btoul(cdb->addr);
8880 num_blocks = scsi_4btoul(cdb->length);
8884 struct scsi_rw_16 *cdb;
8886 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8887 if (cdb->byte2 & SVFY_BYTCHK)
8889 if (cdb->byte2 & SVFY_DPO)
8890 flags |= CTL_LLF_DPO;
8891 lba = scsi_8btou64(cdb->addr);
8892 num_blocks = scsi_4btoul(cdb->length);
8897 * We got a command we don't support. This shouldn't
8898 * happen, commands should be filtered out above us.
8900 ctl_set_invalid_opcode(ctsio);
8901 ctl_done((union ctl_io *)ctsio);
8902 return (CTL_RETVAL_COMPLETE);
8906 * The first check is to make sure we're in bounds, the second
8907 * check is to catch wrap-around problems. If the lba + num blocks
8908 * is less than the lba, then we've wrapped around and the block
8909 * range is invalid anyway.
8911 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8912 || ((lba + num_blocks) < lba)) {
8913 ctl_set_lba_out_of_range(ctsio);
8914 ctl_done((union ctl_io *)ctsio);
8915 return (CTL_RETVAL_COMPLETE);
8919 * According to SBC-3, a transfer length of 0 is not an error.
8921 if (num_blocks == 0) {
8922 ctl_set_success(ctsio);
8923 ctl_done((union ctl_io *)ctsio);
8924 return (CTL_RETVAL_COMPLETE);
8927 lbalen = (struct ctl_lba_len_flags *)
8928 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8930 lbalen->len = num_blocks;
8932 lbalen->flags = CTL_LLF_COMPARE | flags;
8933 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8935 lbalen->flags = CTL_LLF_VERIFY | flags;
8936 ctsio->kern_total_len = 0;
8938 ctsio->kern_rel_offset = 0;
8940 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
8941 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8946 ctl_report_luns(struct ctl_scsiio *ctsio)
8948 struct ctl_softc *softc;
8949 struct scsi_report_luns *cdb;
8950 struct scsi_report_luns_data *lun_data;
8951 struct ctl_lun *lun, *request_lun;
8952 struct ctl_port *port;
8953 int num_luns, retval;
8954 uint32_t alloc_len, lun_datalen;
8956 uint32_t initidx, targ_lun_id, lun_id;
8958 retval = CTL_RETVAL_COMPLETE;
8959 cdb = (struct scsi_report_luns *)ctsio->cdb;
8960 port = ctl_io_port(&ctsio->io_hdr);
8961 softc = port->ctl_softc;
8963 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
8965 mtx_lock(&softc->ctl_lock);
8967 for (targ_lun_id = 0; targ_lun_id < CTL_MAX_LUNS; targ_lun_id++) {
8968 if (ctl_lun_map_from_port(port, targ_lun_id) < CTL_MAX_LUNS)
8971 mtx_unlock(&softc->ctl_lock);
8973 switch (cdb->select_report) {
8974 case RPL_REPORT_DEFAULT:
8975 case RPL_REPORT_ALL:
8976 case RPL_REPORT_NONSUBSID:
8978 case RPL_REPORT_WELLKNOWN:
8979 case RPL_REPORT_ADMIN:
8980 case RPL_REPORT_CONGLOM:
8984 ctl_set_invalid_field(ctsio,
8990 ctl_done((union ctl_io *)ctsio);
8992 break; /* NOTREACHED */
8995 alloc_len = scsi_4btoul(cdb->length);
8997 * The initiator has to allocate at least 16 bytes for this request,
8998 * so he can at least get the header and the first LUN. Otherwise
8999 * we reject the request (per SPC-3 rev 14, section 6.21).
9001 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9002 sizeof(struct scsi_report_luns_lundata))) {
9003 ctl_set_invalid_field(ctsio,
9009 ctl_done((union ctl_io *)ctsio);
9013 request_lun = (struct ctl_lun *)
9014 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9016 lun_datalen = sizeof(*lun_data) +
9017 (num_luns * sizeof(struct scsi_report_luns_lundata));
9019 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9020 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9021 ctsio->kern_sg_entries = 0;
9023 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9025 mtx_lock(&softc->ctl_lock);
9026 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9027 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9028 if (lun_id >= CTL_MAX_LUNS)
9030 lun = softc->ctl_luns[lun_id];
9034 if (targ_lun_id <= 0xff) {
9036 * Peripheral addressing method, bus number 0.
9038 lun_data->luns[num_filled].lundata[0] =
9039 RPL_LUNDATA_ATYP_PERIPH;
9040 lun_data->luns[num_filled].lundata[1] = targ_lun_id;
9042 } else if (targ_lun_id <= 0x3fff) {
9044 * Flat addressing method.
9046 lun_data->luns[num_filled].lundata[0] =
9047 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8);
9048 lun_data->luns[num_filled].lundata[1] =
9049 (targ_lun_id & 0xff);
9051 } else if (targ_lun_id <= 0xffffff) {
9053 * Extended flat addressing method.
9055 lun_data->luns[num_filled].lundata[0] =
9056 RPL_LUNDATA_ATYP_EXTLUN | 0x12;
9057 scsi_ulto3b(targ_lun_id,
9058 &lun_data->luns[num_filled].lundata[1]);
9061 printf("ctl_report_luns: bogus LUN number %jd, "
9062 "skipping\n", (intmax_t)targ_lun_id);
9065 * According to SPC-3, rev 14 section 6.21:
9067 * "The execution of a REPORT LUNS command to any valid and
9068 * installed logical unit shall clear the REPORTED LUNS DATA
9069 * HAS CHANGED unit attention condition for all logical
9070 * units of that target with respect to the requesting
9071 * initiator. A valid and installed logical unit is one
9072 * having a PERIPHERAL QUALIFIER of 000b in the standard
9073 * INQUIRY data (see 6.4.2)."
9075 * If request_lun is NULL, the LUN this report luns command
9076 * was issued to is either disabled or doesn't exist. In that
9077 * case, we shouldn't clear any pending lun change unit
9080 if (request_lun != NULL) {
9081 mtx_lock(&lun->lun_lock);
9082 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE);
9083 mtx_unlock(&lun->lun_lock);
9086 mtx_unlock(&softc->ctl_lock);
9089 * It's quite possible that we've returned fewer LUNs than we allocated
9090 * space for. Trim it.
9092 lun_datalen = sizeof(*lun_data) +
9093 (num_filled * sizeof(struct scsi_report_luns_lundata));
9095 if (lun_datalen < alloc_len) {
9096 ctsio->residual = alloc_len - lun_datalen;
9097 ctsio->kern_data_len = lun_datalen;
9098 ctsio->kern_total_len = lun_datalen;
9100 ctsio->residual = 0;
9101 ctsio->kern_data_len = alloc_len;
9102 ctsio->kern_total_len = alloc_len;
9104 ctsio->kern_data_resid = 0;
9105 ctsio->kern_rel_offset = 0;
9106 ctsio->kern_sg_entries = 0;
9109 * We set this to the actual data length, regardless of how much
9110 * space we actually have to return results. If the user looks at
9111 * this value, he'll know whether or not he allocated enough space
9112 * and reissue the command if necessary. We don't support well
9113 * known logical units, so if the user asks for that, return none.
9115 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9118 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9121 ctl_set_success(ctsio);
9122 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9123 ctsio->be_move_done = ctl_config_move_done;
9124 ctl_datamove((union ctl_io *)ctsio);
9129 ctl_request_sense(struct ctl_scsiio *ctsio)
9131 struct scsi_request_sense *cdb;
9132 struct scsi_sense_data *sense_ptr;
9133 struct ctl_softc *ctl_softc;
9134 struct ctl_lun *lun;
9137 scsi_sense_data_type sense_format;
9138 ctl_ua_type ua_type;
9140 cdb = (struct scsi_request_sense *)ctsio->cdb;
9142 ctl_softc = control_softc;
9143 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9145 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9148 * Determine which sense format the user wants.
9150 if (cdb->byte2 & SRS_DESC)
9151 sense_format = SSD_TYPE_DESC;
9153 sense_format = SSD_TYPE_FIXED;
9155 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9156 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9157 ctsio->kern_sg_entries = 0;
9160 * struct scsi_sense_data, which is currently set to 256 bytes, is
9161 * larger than the largest allowed value for the length field in the
9162 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9164 ctsio->residual = 0;
9165 ctsio->kern_data_len = cdb->length;
9166 ctsio->kern_total_len = cdb->length;
9168 ctsio->kern_data_resid = 0;
9169 ctsio->kern_rel_offset = 0;
9170 ctsio->kern_sg_entries = 0;
9173 * If we don't have a LUN, we don't have any pending sense.
9179 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9181 * Check for pending sense, and then for pending unit attentions.
9182 * Pending sense gets returned first, then pending unit attentions.
9184 mtx_lock(&lun->lun_lock);
9186 if (ctl_is_set(lun->have_ca, initidx)) {
9187 scsi_sense_data_type stored_format;
9190 * Check to see which sense format was used for the stored
9193 stored_format = scsi_sense_type(&lun->pending_sense[initidx]);
9196 * If the user requested a different sense format than the
9197 * one we stored, then we need to convert it to the other
9198 * format. If we're going from descriptor to fixed format
9199 * sense data, we may lose things in translation, depending
9200 * on what options were used.
9202 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9203 * for some reason we'll just copy it out as-is.
9205 if ((stored_format == SSD_TYPE_FIXED)
9206 && (sense_format == SSD_TYPE_DESC))
9207 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9208 &lun->pending_sense[initidx],
9209 (struct scsi_sense_data_desc *)sense_ptr);
9210 else if ((stored_format == SSD_TYPE_DESC)
9211 && (sense_format == SSD_TYPE_FIXED))
9212 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9213 &lun->pending_sense[initidx],
9214 (struct scsi_sense_data_fixed *)sense_ptr);
9216 memcpy(sense_ptr, &lun->pending_sense[initidx],
9217 MIN(sizeof(*sense_ptr),
9218 sizeof(lun->pending_sense[initidx])));
9220 ctl_clear_mask(lun->have_ca, initidx);
9225 ua_type = ctl_build_ua(lun, initidx, sense_ptr, sense_format);
9226 if (ua_type != CTL_UA_NONE)
9228 if (ua_type == CTL_UA_LUN_CHANGE) {
9229 mtx_unlock(&lun->lun_lock);
9230 mtx_lock(&ctl_softc->ctl_lock);
9231 ctl_clr_ua_allluns(ctl_softc, initidx, ua_type);
9232 mtx_unlock(&ctl_softc->ctl_lock);
9233 mtx_lock(&lun->lun_lock);
9237 mtx_unlock(&lun->lun_lock);
9240 * We already have a pending error, return it.
9242 if (have_error != 0) {
9244 * We report the SCSI status as OK, since the status of the
9245 * request sense command itself is OK.
9246 * We report 0 for the sense length, because we aren't doing
9247 * autosense in this case. We're reporting sense as
9250 ctl_set_success(ctsio);
9251 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9252 ctsio->be_move_done = ctl_config_move_done;
9253 ctl_datamove((union ctl_io *)ctsio);
9254 return (CTL_RETVAL_COMPLETE);
9260 * No sense information to report, so we report that everything is
9263 ctl_set_sense_data(sense_ptr,
9266 /*current_error*/ 1,
9267 /*sense_key*/ SSD_KEY_NO_SENSE,
9273 * We report 0 for the sense length, because we aren't doing
9274 * autosense in this case. We're reporting sense as parameter data.
9276 ctl_set_success(ctsio);
9277 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9278 ctsio->be_move_done = ctl_config_move_done;
9279 ctl_datamove((union ctl_io *)ctsio);
9280 return (CTL_RETVAL_COMPLETE);
9284 ctl_tur(struct ctl_scsiio *ctsio)
9287 CTL_DEBUG_PRINT(("ctl_tur\n"));
9289 ctl_set_success(ctsio);
9290 ctl_done((union ctl_io *)ctsio);
9292 return (CTL_RETVAL_COMPLETE);
9296 * SCSI VPD page 0x00, the Supported VPD Pages page.
9299 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9301 struct scsi_vpd_supported_pages *pages;
9303 struct ctl_lun *lun;
9306 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9308 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9309 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9310 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9311 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9312 ctsio->kern_sg_entries = 0;
9314 if (sup_page_size < alloc_len) {
9315 ctsio->residual = alloc_len - sup_page_size;
9316 ctsio->kern_data_len = sup_page_size;
9317 ctsio->kern_total_len = sup_page_size;
9319 ctsio->residual = 0;
9320 ctsio->kern_data_len = alloc_len;
9321 ctsio->kern_total_len = alloc_len;
9323 ctsio->kern_data_resid = 0;
9324 ctsio->kern_rel_offset = 0;
9325 ctsio->kern_sg_entries = 0;
9328 * The control device is always connected. The disk device, on the
9329 * other hand, may not be online all the time. Need to change this
9330 * to figure out whether the disk device is actually online or not.
9333 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9334 lun->be_lun->lun_type;
9336 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9339 /* Supported VPD pages */
9340 pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9342 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9343 /* Device Identification */
9344 pages->page_list[p++] = SVPD_DEVICE_ID;
9345 /* Extended INQUIRY Data */
9346 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9347 /* Mode Page Policy */
9348 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9350 pages->page_list[p++] = SVPD_SCSI_PORTS;
9351 /* Third-party Copy */
9352 pages->page_list[p++] = SVPD_SCSI_TPC;
9353 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9355 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9356 /* Block Device Characteristics */
9357 pages->page_list[p++] = SVPD_BDC;
9358 /* Logical Block Provisioning */
9359 pages->page_list[p++] = SVPD_LBP;
9363 ctl_set_success(ctsio);
9364 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9365 ctsio->be_move_done = ctl_config_move_done;
9366 ctl_datamove((union ctl_io *)ctsio);
9367 return (CTL_RETVAL_COMPLETE);
9371 * SCSI VPD page 0x80, the Unit Serial Number page.
9374 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9376 struct scsi_vpd_unit_serial_number *sn_ptr;
9377 struct ctl_lun *lun;
9380 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9382 data_len = 4 + CTL_SN_LEN;
9383 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9384 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9385 if (data_len < alloc_len) {
9386 ctsio->residual = alloc_len - data_len;
9387 ctsio->kern_data_len = data_len;
9388 ctsio->kern_total_len = data_len;
9390 ctsio->residual = 0;
9391 ctsio->kern_data_len = alloc_len;
9392 ctsio->kern_total_len = alloc_len;
9394 ctsio->kern_data_resid = 0;
9395 ctsio->kern_rel_offset = 0;
9396 ctsio->kern_sg_entries = 0;
9399 * The control device is always connected. The disk device, on the
9400 * other hand, may not be online all the time. Need to change this
9401 * to figure out whether the disk device is actually online or not.
9404 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9405 lun->be_lun->lun_type;
9407 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9409 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9410 sn_ptr->length = CTL_SN_LEN;
9412 * If we don't have a LUN, we just leave the serial number as
9416 strncpy((char *)sn_ptr->serial_num,
9417 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9419 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9421 ctl_set_success(ctsio);
9422 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9423 ctsio->be_move_done = ctl_config_move_done;
9424 ctl_datamove((union ctl_io *)ctsio);
9425 return (CTL_RETVAL_COMPLETE);
9430 * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9433 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9435 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9436 struct ctl_lun *lun;
9439 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9441 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9442 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9443 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9444 ctsio->kern_sg_entries = 0;
9446 if (data_len < alloc_len) {
9447 ctsio->residual = alloc_len - data_len;
9448 ctsio->kern_data_len = data_len;
9449 ctsio->kern_total_len = data_len;
9451 ctsio->residual = 0;
9452 ctsio->kern_data_len = alloc_len;
9453 ctsio->kern_total_len = alloc_len;
9455 ctsio->kern_data_resid = 0;
9456 ctsio->kern_rel_offset = 0;
9457 ctsio->kern_sg_entries = 0;
9460 * The control device is always connected. The disk device, on the
9461 * other hand, may not be online all the time.
9464 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9465 lun->be_lun->lun_type;
9467 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9468 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9469 scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9471 * We support head of queue, ordered and simple tags.
9473 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9475 * Volatile cache supported.
9477 eid_ptr->flags3 = SVPD_EID_V_SUP;
9480 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9481 * attention for a particular IT nexus on all LUNs once we report
9482 * it to that nexus once. This bit is required as of SPC-4.
9484 eid_ptr->flags4 = SVPD_EID_LUICLT;
9487 * XXX KDM in order to correctly answer this, we would need
9488 * information from the SIM to determine how much sense data it
9489 * can send. So this would really be a path inquiry field, most
9490 * likely. This can be set to a maximum of 252 according to SPC-4,
9491 * but the hardware may or may not be able to support that much.
9492 * 0 just means that the maximum sense data length is not reported.
9494 eid_ptr->max_sense_length = 0;
9496 ctl_set_success(ctsio);
9497 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9498 ctsio->be_move_done = ctl_config_move_done;
9499 ctl_datamove((union ctl_io *)ctsio);
9500 return (CTL_RETVAL_COMPLETE);
9504 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9506 struct scsi_vpd_mode_page_policy *mpp_ptr;
9507 struct ctl_lun *lun;
9510 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9512 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9513 sizeof(struct scsi_vpd_mode_page_policy_descr);
9515 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9516 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9517 ctsio->kern_sg_entries = 0;
9519 if (data_len < alloc_len) {
9520 ctsio->residual = alloc_len - data_len;
9521 ctsio->kern_data_len = data_len;
9522 ctsio->kern_total_len = data_len;
9524 ctsio->residual = 0;
9525 ctsio->kern_data_len = alloc_len;
9526 ctsio->kern_total_len = alloc_len;
9528 ctsio->kern_data_resid = 0;
9529 ctsio->kern_rel_offset = 0;
9530 ctsio->kern_sg_entries = 0;
9533 * The control device is always connected. The disk device, on the
9534 * other hand, may not be online all the time.
9537 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9538 lun->be_lun->lun_type;
9540 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9541 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9542 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9543 mpp_ptr->descr[0].page_code = 0x3f;
9544 mpp_ptr->descr[0].subpage_code = 0xff;
9545 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9547 ctl_set_success(ctsio);
9548 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9549 ctsio->be_move_done = ctl_config_move_done;
9550 ctl_datamove((union ctl_io *)ctsio);
9551 return (CTL_RETVAL_COMPLETE);
9555 * SCSI VPD page 0x83, the Device Identification page.
9558 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9560 struct scsi_vpd_device_id *devid_ptr;
9561 struct scsi_vpd_id_descriptor *desc;
9562 struct ctl_softc *softc;
9563 struct ctl_lun *lun;
9564 struct ctl_port *port;
9568 softc = control_softc;
9570 port = ctl_io_port(&ctsio->io_hdr);
9571 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9573 data_len = sizeof(struct scsi_vpd_device_id) +
9574 sizeof(struct scsi_vpd_id_descriptor) +
9575 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9576 sizeof(struct scsi_vpd_id_descriptor) +
9577 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9578 if (lun && lun->lun_devid)
9579 data_len += lun->lun_devid->len;
9580 if (port && port->port_devid)
9581 data_len += port->port_devid->len;
9582 if (port && port->target_devid)
9583 data_len += port->target_devid->len;
9585 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9586 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9587 ctsio->kern_sg_entries = 0;
9589 if (data_len < alloc_len) {
9590 ctsio->residual = alloc_len - data_len;
9591 ctsio->kern_data_len = data_len;
9592 ctsio->kern_total_len = data_len;
9594 ctsio->residual = 0;
9595 ctsio->kern_data_len = alloc_len;
9596 ctsio->kern_total_len = alloc_len;
9598 ctsio->kern_data_resid = 0;
9599 ctsio->kern_rel_offset = 0;
9600 ctsio->kern_sg_entries = 0;
9603 * The control device is always connected. The disk device, on the
9604 * other hand, may not be online all the time.
9607 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9608 lun->be_lun->lun_type;
9610 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9611 devid_ptr->page_code = SVPD_DEVICE_ID;
9612 scsi_ulto2b(data_len - 4, devid_ptr->length);
9614 if (port && port->port_type == CTL_PORT_FC)
9615 proto = SCSI_PROTO_FC << 4;
9616 else if (port && port->port_type == CTL_PORT_ISCSI)
9617 proto = SCSI_PROTO_ISCSI << 4;
9619 proto = SCSI_PROTO_SPI << 4;
9620 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9623 * We're using a LUN association here. i.e., this device ID is a
9624 * per-LUN identifier.
9626 if (lun && lun->lun_devid) {
9627 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9628 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9629 lun->lun_devid->len);
9633 * This is for the WWPN which is a port association.
9635 if (port && port->port_devid) {
9636 memcpy(desc, port->port_devid->data, port->port_devid->len);
9637 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9638 port->port_devid->len);
9642 * This is for the Relative Target Port(type 4h) identifier
9644 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9645 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9646 SVPD_ID_TYPE_RELTARG;
9648 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9649 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9650 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9653 * This is for the Target Port Group(type 5h) identifier
9655 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9656 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9657 SVPD_ID_TYPE_TPORTGRP;
9659 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / softc->port_cnt + 1,
9660 &desc->identifier[2]);
9661 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9662 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9665 * This is for the Target identifier
9667 if (port && port->target_devid) {
9668 memcpy(desc, port->target_devid->data, port->target_devid->len);
9671 ctl_set_success(ctsio);
9672 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9673 ctsio->be_move_done = ctl_config_move_done;
9674 ctl_datamove((union ctl_io *)ctsio);
9675 return (CTL_RETVAL_COMPLETE);
9679 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9681 struct ctl_softc *softc = control_softc;
9682 struct scsi_vpd_scsi_ports *sp;
9683 struct scsi_vpd_port_designation *pd;
9684 struct scsi_vpd_port_designation_cont *pdc;
9685 struct ctl_lun *lun;
9686 struct ctl_port *port;
9687 int data_len, num_target_ports, iid_len, id_len;
9689 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9691 num_target_ports = 0;
9694 mtx_lock(&softc->ctl_lock);
9695 STAILQ_FOREACH(port, &softc->port_list, links) {
9696 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9699 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
9702 if (port->init_devid)
9703 iid_len += port->init_devid->len;
9704 if (port->port_devid)
9705 id_len += port->port_devid->len;
9707 mtx_unlock(&softc->ctl_lock);
9709 data_len = sizeof(struct scsi_vpd_scsi_ports) +
9710 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9711 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9712 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9713 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9714 ctsio->kern_sg_entries = 0;
9716 if (data_len < alloc_len) {
9717 ctsio->residual = alloc_len - data_len;
9718 ctsio->kern_data_len = data_len;
9719 ctsio->kern_total_len = data_len;
9721 ctsio->residual = 0;
9722 ctsio->kern_data_len = alloc_len;
9723 ctsio->kern_total_len = alloc_len;
9725 ctsio->kern_data_resid = 0;
9726 ctsio->kern_rel_offset = 0;
9727 ctsio->kern_sg_entries = 0;
9730 * The control device is always connected. The disk device, on the
9731 * other hand, may not be online all the time. Need to change this
9732 * to figure out whether the disk device is actually online or not.
9735 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9736 lun->be_lun->lun_type;
9738 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9740 sp->page_code = SVPD_SCSI_PORTS;
9741 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9743 pd = &sp->design[0];
9745 mtx_lock(&softc->ctl_lock);
9746 STAILQ_FOREACH(port, &softc->port_list, links) {
9747 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9750 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
9752 scsi_ulto2b(port->targ_port, pd->relative_port_id);
9753 if (port->init_devid) {
9754 iid_len = port->init_devid->len;
9755 memcpy(pd->initiator_transportid,
9756 port->init_devid->data, port->init_devid->len);
9759 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9760 pdc = (struct scsi_vpd_port_designation_cont *)
9761 (&pd->initiator_transportid[iid_len]);
9762 if (port->port_devid) {
9763 id_len = port->port_devid->len;
9764 memcpy(pdc->target_port_descriptors,
9765 port->port_devid->data, port->port_devid->len);
9768 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9769 pd = (struct scsi_vpd_port_designation *)
9770 ((uint8_t *)pdc->target_port_descriptors + id_len);
9772 mtx_unlock(&softc->ctl_lock);
9774 ctl_set_success(ctsio);
9775 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9776 ctsio->be_move_done = ctl_config_move_done;
9777 ctl_datamove((union ctl_io *)ctsio);
9778 return (CTL_RETVAL_COMPLETE);
9782 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9784 struct scsi_vpd_block_limits *bl_ptr;
9785 struct ctl_lun *lun;
9787 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9789 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9790 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9791 ctsio->kern_sg_entries = 0;
9793 if (sizeof(*bl_ptr) < alloc_len) {
9794 ctsio->residual = alloc_len - sizeof(*bl_ptr);
9795 ctsio->kern_data_len = sizeof(*bl_ptr);
9796 ctsio->kern_total_len = sizeof(*bl_ptr);
9798 ctsio->residual = 0;
9799 ctsio->kern_data_len = alloc_len;
9800 ctsio->kern_total_len = alloc_len;
9802 ctsio->kern_data_resid = 0;
9803 ctsio->kern_rel_offset = 0;
9804 ctsio->kern_sg_entries = 0;
9807 * The control device is always connected. The disk device, on the
9808 * other hand, may not be online all the time. Need to change this
9809 * to figure out whether the disk device is actually online or not.
9812 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9813 lun->be_lun->lun_type;
9815 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9817 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9818 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
9819 bl_ptr->max_cmp_write_len = 0xff;
9820 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9822 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
9823 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9824 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
9825 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
9826 if (lun->be_lun->ublockexp != 0) {
9827 scsi_ulto4b((1 << lun->be_lun->ublockexp),
9828 bl_ptr->opt_unmap_grain);
9829 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
9830 bl_ptr->unmap_grain_align);
9833 scsi_ulto4b(lun->be_lun->atomicblock,
9834 bl_ptr->max_atomic_transfer_length);
9835 scsi_ulto4b(0, bl_ptr->atomic_alignment);
9836 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
9837 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary);
9838 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size);
9840 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
9842 ctl_set_success(ctsio);
9843 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9844 ctsio->be_move_done = ctl_config_move_done;
9845 ctl_datamove((union ctl_io *)ctsio);
9846 return (CTL_RETVAL_COMPLETE);
9850 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
9852 struct scsi_vpd_block_device_characteristics *bdc_ptr;
9853 struct ctl_lun *lun;
9857 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9859 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
9860 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
9861 ctsio->kern_sg_entries = 0;
9863 if (sizeof(*bdc_ptr) < alloc_len) {
9864 ctsio->residual = alloc_len - sizeof(*bdc_ptr);
9865 ctsio->kern_data_len = sizeof(*bdc_ptr);
9866 ctsio->kern_total_len = sizeof(*bdc_ptr);
9868 ctsio->residual = 0;
9869 ctsio->kern_data_len = alloc_len;
9870 ctsio->kern_total_len = alloc_len;
9872 ctsio->kern_data_resid = 0;
9873 ctsio->kern_rel_offset = 0;
9874 ctsio->kern_sg_entries = 0;
9877 * The control device is always connected. The disk device, on the
9878 * other hand, may not be online all the time. Need to change this
9879 * to figure out whether the disk device is actually online or not.
9882 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9883 lun->be_lun->lun_type;
9885 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9886 bdc_ptr->page_code = SVPD_BDC;
9887 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
9889 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL)
9890 i = strtol(value, NULL, 0);
9892 i = CTL_DEFAULT_ROTATION_RATE;
9893 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
9895 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL)
9896 i = strtol(value, NULL, 0);
9899 bdc_ptr->wab_wac_ff = (i & 0x0f);
9900 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
9902 ctl_set_success(ctsio);
9903 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9904 ctsio->be_move_done = ctl_config_move_done;
9905 ctl_datamove((union ctl_io *)ctsio);
9906 return (CTL_RETVAL_COMPLETE);
9910 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
9912 struct scsi_vpd_logical_block_prov *lbp_ptr;
9913 struct ctl_lun *lun;
9915 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9917 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
9918 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
9919 ctsio->kern_sg_entries = 0;
9921 if (sizeof(*lbp_ptr) < alloc_len) {
9922 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
9923 ctsio->kern_data_len = sizeof(*lbp_ptr);
9924 ctsio->kern_total_len = sizeof(*lbp_ptr);
9926 ctsio->residual = 0;
9927 ctsio->kern_data_len = alloc_len;
9928 ctsio->kern_total_len = alloc_len;
9930 ctsio->kern_data_resid = 0;
9931 ctsio->kern_rel_offset = 0;
9932 ctsio->kern_sg_entries = 0;
9935 * The control device is always connected. The disk device, on the
9936 * other hand, may not be online all the time. Need to change this
9937 * to figure out whether the disk device is actually online or not.
9940 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9941 lun->be_lun->lun_type;
9943 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9945 lbp_ptr->page_code = SVPD_LBP;
9946 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
9947 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
9948 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9949 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
9950 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
9951 lbp_ptr->prov_type = SVPD_LBP_THIN;
9954 ctl_set_success(ctsio);
9955 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9956 ctsio->be_move_done = ctl_config_move_done;
9957 ctl_datamove((union ctl_io *)ctsio);
9958 return (CTL_RETVAL_COMPLETE);
9962 * INQUIRY with the EVPD bit set.
9965 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
9967 struct ctl_lun *lun;
9968 struct scsi_inquiry *cdb;
9969 int alloc_len, retval;
9971 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9972 cdb = (struct scsi_inquiry *)ctsio->cdb;
9973 alloc_len = scsi_2btoul(cdb->length);
9975 switch (cdb->page_code) {
9976 case SVPD_SUPPORTED_PAGES:
9977 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
9979 case SVPD_UNIT_SERIAL_NUMBER:
9980 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
9982 case SVPD_DEVICE_ID:
9983 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
9985 case SVPD_EXTENDED_INQUIRY_DATA:
9986 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
9988 case SVPD_MODE_PAGE_POLICY:
9989 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
9991 case SVPD_SCSI_PORTS:
9992 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
9995 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
9997 case SVPD_BLOCK_LIMITS:
9998 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10000 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10003 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10005 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10008 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10010 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10014 ctl_set_invalid_field(ctsio,
10020 ctl_done((union ctl_io *)ctsio);
10021 retval = CTL_RETVAL_COMPLETE;
10029 * Standard INQUIRY data.
10032 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10034 struct scsi_inquiry_data *inq_ptr;
10035 struct scsi_inquiry *cdb;
10036 struct ctl_softc *softc = control_softc;
10037 struct ctl_port *port;
10038 struct ctl_lun *lun;
10040 uint32_t alloc_len, data_len;
10041 ctl_port_type port_type;
10043 port = ctl_io_port(&ctsio->io_hdr);
10044 port_type = port->port_type;
10045 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10046 port_type = CTL_PORT_SCSI;
10048 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10049 cdb = (struct scsi_inquiry *)ctsio->cdb;
10050 alloc_len = scsi_2btoul(cdb->length);
10053 * We malloc the full inquiry data size here and fill it
10054 * in. If the user only asks for less, we'll give him
10057 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10058 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10059 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10060 ctsio->kern_sg_entries = 0;
10061 ctsio->kern_data_resid = 0;
10062 ctsio->kern_rel_offset = 0;
10064 if (data_len < alloc_len) {
10065 ctsio->residual = alloc_len - data_len;
10066 ctsio->kern_data_len = data_len;
10067 ctsio->kern_total_len = data_len;
10069 ctsio->residual = 0;
10070 ctsio->kern_data_len = alloc_len;
10071 ctsio->kern_total_len = alloc_len;
10075 if ((lun->flags & CTL_LUN_PRIMARY_SC) ||
10076 softc->ha_link >= CTL_HA_LINK_UNKNOWN) {
10077 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10078 lun->be_lun->lun_type;
10080 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) |
10081 lun->be_lun->lun_type;
10084 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10086 /* RMB in byte 2 is 0 */
10087 inq_ptr->version = SCSI_REV_SPC4;
10090 * According to SAM-3, even if a device only supports a single
10091 * level of LUN addressing, it should still set the HISUP bit:
10093 * 4.9.1 Logical unit numbers overview
10095 * All logical unit number formats described in this standard are
10096 * hierarchical in structure even when only a single level in that
10097 * hierarchy is used. The HISUP bit shall be set to one in the
10098 * standard INQUIRY data (see SPC-2) when any logical unit number
10099 * format described in this standard is used. Non-hierarchical
10100 * formats are outside the scope of this standard.
10102 * Therefore we set the HiSup bit here.
10104 * The reponse format is 2, per SPC-3.
10106 inq_ptr->response_format = SID_HiSup | 2;
10108 inq_ptr->additional_length = data_len -
10109 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10110 CTL_DEBUG_PRINT(("additional_length = %d\n",
10111 inq_ptr->additional_length));
10113 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10114 /* 16 bit addressing */
10115 if (port_type == CTL_PORT_SCSI)
10116 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10117 /* XXX set the SID_MultiP bit here if we're actually going to
10118 respond on multiple ports */
10119 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10121 /* 16 bit data bus, synchronous transfers */
10122 if (port_type == CTL_PORT_SCSI)
10123 inq_ptr->flags = SID_WBus16 | SID_Sync;
10125 * XXX KDM do we want to support tagged queueing on the control
10129 || (lun->be_lun->lun_type != T_PROCESSOR))
10130 inq_ptr->flags |= SID_CmdQue;
10132 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10133 * We have 8 bytes for the vendor name, and 16 bytes for the device
10134 * name and 4 bytes for the revision.
10136 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10137 "vendor")) == NULL) {
10138 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10140 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10141 strncpy(inq_ptr->vendor, val,
10142 min(sizeof(inq_ptr->vendor), strlen(val)));
10145 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10146 sizeof(inq_ptr->product));
10147 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10148 switch (lun->be_lun->lun_type) {
10150 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10151 sizeof(inq_ptr->product));
10154 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10155 sizeof(inq_ptr->product));
10158 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10159 sizeof(inq_ptr->product));
10163 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10164 strncpy(inq_ptr->product, val,
10165 min(sizeof(inq_ptr->product), strlen(val)));
10169 * XXX make this a macro somewhere so it automatically gets
10170 * incremented when we make changes.
10172 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10173 "revision")) == NULL) {
10174 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10176 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10177 strncpy(inq_ptr->revision, val,
10178 min(sizeof(inq_ptr->revision), strlen(val)));
10182 * For parallel SCSI, we support double transition and single
10183 * transition clocking. We also support QAS (Quick Arbitration
10184 * and Selection) and Information Unit transfers on both the
10185 * control and array devices.
10187 if (port_type == CTL_PORT_SCSI)
10188 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10191 /* SAM-5 (no version claimed) */
10192 scsi_ulto2b(0x00A0, inq_ptr->version1);
10193 /* SPC-4 (no version claimed) */
10194 scsi_ulto2b(0x0460, inq_ptr->version2);
10195 if (port_type == CTL_PORT_FC) {
10196 /* FCP-2 ANSI INCITS.350:2003 */
10197 scsi_ulto2b(0x0917, inq_ptr->version3);
10198 } else if (port_type == CTL_PORT_SCSI) {
10199 /* SPI-4 ANSI INCITS.362:200x */
10200 scsi_ulto2b(0x0B56, inq_ptr->version3);
10201 } else if (port_type == CTL_PORT_ISCSI) {
10202 /* iSCSI (no version claimed) */
10203 scsi_ulto2b(0x0960, inq_ptr->version3);
10204 } else if (port_type == CTL_PORT_SAS) {
10205 /* SAS (no version claimed) */
10206 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10210 /* SBC-4 (no version claimed) */
10211 scsi_ulto2b(0x0600, inq_ptr->version4);
10213 switch (lun->be_lun->lun_type) {
10215 /* SBC-4 (no version claimed) */
10216 scsi_ulto2b(0x0600, inq_ptr->version4);
10224 ctl_set_success(ctsio);
10225 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10226 ctsio->be_move_done = ctl_config_move_done;
10227 ctl_datamove((union ctl_io *)ctsio);
10228 return (CTL_RETVAL_COMPLETE);
10232 ctl_inquiry(struct ctl_scsiio *ctsio)
10234 struct scsi_inquiry *cdb;
10237 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10239 cdb = (struct scsi_inquiry *)ctsio->cdb;
10240 if (cdb->byte2 & SI_EVPD)
10241 retval = ctl_inquiry_evpd(ctsio);
10242 else if (cdb->page_code == 0)
10243 retval = ctl_inquiry_std(ctsio);
10245 ctl_set_invalid_field(ctsio,
10251 ctl_done((union ctl_io *)ctsio);
10252 return (CTL_RETVAL_COMPLETE);
10259 * For known CDB types, parse the LBA and length.
10262 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10264 if (io->io_hdr.io_type != CTL_IO_SCSI)
10267 switch (io->scsiio.cdb[0]) {
10268 case COMPARE_AND_WRITE: {
10269 struct scsi_compare_and_write *cdb;
10271 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10273 *lba = scsi_8btou64(cdb->addr);
10274 *len = cdb->length;
10279 struct scsi_rw_6 *cdb;
10281 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10283 *lba = scsi_3btoul(cdb->addr);
10284 /* only 5 bits are valid in the most significant address byte */
10286 *len = cdb->length;
10291 struct scsi_rw_10 *cdb;
10293 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10295 *lba = scsi_4btoul(cdb->addr);
10296 *len = scsi_2btoul(cdb->length);
10299 case WRITE_VERIFY_10: {
10300 struct scsi_write_verify_10 *cdb;
10302 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10304 *lba = scsi_4btoul(cdb->addr);
10305 *len = scsi_2btoul(cdb->length);
10310 struct scsi_rw_12 *cdb;
10312 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10314 *lba = scsi_4btoul(cdb->addr);
10315 *len = scsi_4btoul(cdb->length);
10318 case WRITE_VERIFY_12: {
10319 struct scsi_write_verify_12 *cdb;
10321 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10323 *lba = scsi_4btoul(cdb->addr);
10324 *len = scsi_4btoul(cdb->length);
10329 struct scsi_rw_16 *cdb;
10331 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10333 *lba = scsi_8btou64(cdb->addr);
10334 *len = scsi_4btoul(cdb->length);
10337 case WRITE_ATOMIC_16: {
10338 struct scsi_write_atomic_16 *cdb;
10340 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb;
10342 *lba = scsi_8btou64(cdb->addr);
10343 *len = scsi_2btoul(cdb->length);
10346 case WRITE_VERIFY_16: {
10347 struct scsi_write_verify_16 *cdb;
10349 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10351 *lba = scsi_8btou64(cdb->addr);
10352 *len = scsi_4btoul(cdb->length);
10355 case WRITE_SAME_10: {
10356 struct scsi_write_same_10 *cdb;
10358 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10360 *lba = scsi_4btoul(cdb->addr);
10361 *len = scsi_2btoul(cdb->length);
10364 case WRITE_SAME_16: {
10365 struct scsi_write_same_16 *cdb;
10367 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10369 *lba = scsi_8btou64(cdb->addr);
10370 *len = scsi_4btoul(cdb->length);
10374 struct scsi_verify_10 *cdb;
10376 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10378 *lba = scsi_4btoul(cdb->addr);
10379 *len = scsi_2btoul(cdb->length);
10383 struct scsi_verify_12 *cdb;
10385 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10387 *lba = scsi_4btoul(cdb->addr);
10388 *len = scsi_4btoul(cdb->length);
10392 struct scsi_verify_16 *cdb;
10394 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10396 *lba = scsi_8btou64(cdb->addr);
10397 *len = scsi_4btoul(cdb->length);
10405 case SERVICE_ACTION_IN: { /* GET LBA STATUS */
10406 struct scsi_get_lba_status *cdb;
10408 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10409 *lba = scsi_8btou64(cdb->addr);
10415 break; /* NOTREACHED */
10422 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10425 uint64_t endlba1, endlba2;
10427 endlba1 = lba1 + len1 - (seq ? 0 : 1);
10428 endlba2 = lba2 + len2 - 1;
10430 if ((endlba1 < lba2) || (endlba2 < lba1))
10431 return (CTL_ACTION_PASS);
10433 return (CTL_ACTION_BLOCK);
10437 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10439 struct ctl_ptr_len_flags *ptrlen;
10440 struct scsi_unmap_desc *buf, *end, *range;
10444 /* If not UNMAP -- go other way. */
10445 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10446 io->scsiio.cdb[0] != UNMAP)
10447 return (CTL_ACTION_ERROR);
10449 /* If UNMAP without data -- block and wait for data. */
10450 ptrlen = (struct ctl_ptr_len_flags *)
10451 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10452 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10453 ptrlen->ptr == NULL)
10454 return (CTL_ACTION_BLOCK);
10456 /* UNMAP with data -- check for collision. */
10457 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10458 end = buf + ptrlen->len / sizeof(*buf);
10459 for (range = buf; range < end; range++) {
10460 lba = scsi_8btou64(range->lba);
10461 len = scsi_4btoul(range->length);
10462 if ((lba < lba2 + len2) && (lba + len > lba2))
10463 return (CTL_ACTION_BLOCK);
10465 return (CTL_ACTION_PASS);
10469 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10471 uint64_t lba1, lba2;
10472 uint64_t len1, len2;
10475 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10476 return (CTL_ACTION_ERROR);
10478 retval = ctl_extent_check_unmap(io1, lba2, len2);
10479 if (retval != CTL_ACTION_ERROR)
10482 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10483 return (CTL_ACTION_ERROR);
10485 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10487 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
10491 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2)
10493 uint64_t lba1, lba2;
10494 uint64_t len1, len2;
10496 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10497 return (CTL_ACTION_PASS);
10498 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10499 return (CTL_ACTION_ERROR);
10500 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10501 return (CTL_ACTION_ERROR);
10503 if (lba1 + len1 == lba2)
10504 return (CTL_ACTION_BLOCK);
10505 return (CTL_ACTION_PASS);
10509 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10510 union ctl_io *ooa_io)
10512 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10513 const ctl_serialize_action *serialize_row;
10516 * The initiator attempted multiple untagged commands at the same
10517 * time. Can't do that.
10519 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10520 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10521 && ((pending_io->io_hdr.nexus.targ_port ==
10522 ooa_io->io_hdr.nexus.targ_port)
10523 && (pending_io->io_hdr.nexus.initid ==
10524 ooa_io->io_hdr.nexus.initid))
10525 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10526 CTL_FLAG_STATUS_SENT)) == 0))
10527 return (CTL_ACTION_OVERLAP);
10530 * The initiator attempted to send multiple tagged commands with
10531 * the same ID. (It's fine if different initiators have the same
10534 * Even if all of those conditions are true, we don't kill the I/O
10535 * if the command ahead of us has been aborted. We won't end up
10536 * sending it to the FETD, and it's perfectly legal to resend a
10537 * command with the same tag number as long as the previous
10538 * instance of this tag number has been aborted somehow.
10540 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10541 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10542 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10543 && ((pending_io->io_hdr.nexus.targ_port ==
10544 ooa_io->io_hdr.nexus.targ_port)
10545 && (pending_io->io_hdr.nexus.initid ==
10546 ooa_io->io_hdr.nexus.initid))
10547 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10548 CTL_FLAG_STATUS_SENT)) == 0))
10549 return (CTL_ACTION_OVERLAP_TAG);
10552 * If we get a head of queue tag, SAM-3 says that we should
10553 * immediately execute it.
10555 * What happens if this command would normally block for some other
10556 * reason? e.g. a request sense with a head of queue tag
10557 * immediately after a write. Normally that would block, but this
10558 * will result in its getting executed immediately...
10560 * We currently return "pass" instead of "skip", so we'll end up
10561 * going through the rest of the queue to check for overlapped tags.
10563 * XXX KDM check for other types of blockage first??
10565 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10566 return (CTL_ACTION_PASS);
10569 * Ordered tags have to block until all items ahead of them
10570 * have completed. If we get called with an ordered tag, we always
10571 * block, if something else is ahead of us in the queue.
10573 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10574 return (CTL_ACTION_BLOCK);
10577 * Simple tags get blocked until all head of queue and ordered tags
10578 * ahead of them have completed. I'm lumping untagged commands in
10579 * with simple tags here. XXX KDM is that the right thing to do?
10581 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10582 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10583 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10584 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10585 return (CTL_ACTION_BLOCK);
10587 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
10588 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
10590 serialize_row = ctl_serialize_table[ooa_entry->seridx];
10592 switch (serialize_row[pending_entry->seridx]) {
10593 case CTL_SER_BLOCK:
10594 return (CTL_ACTION_BLOCK);
10595 case CTL_SER_EXTENT:
10596 return (ctl_extent_check(ooa_io, pending_io,
10597 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10598 case CTL_SER_EXTENTOPT:
10599 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
10600 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
10601 return (ctl_extent_check(ooa_io, pending_io,
10603 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10604 return (CTL_ACTION_PASS);
10605 case CTL_SER_EXTENTSEQ:
10606 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF)
10607 return (ctl_extent_check_seq(ooa_io, pending_io));
10608 return (CTL_ACTION_PASS);
10610 return (CTL_ACTION_PASS);
10611 case CTL_SER_BLOCKOPT:
10612 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
10613 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
10614 return (CTL_ACTION_BLOCK);
10615 return (CTL_ACTION_PASS);
10617 return (CTL_ACTION_SKIP);
10619 panic("invalid serialization value %d",
10620 serialize_row[pending_entry->seridx]);
10623 return (CTL_ACTION_ERROR);
10627 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
10629 * - pending_io is generally either incoming, or on the blocked queue
10630 * - starting I/O is the I/O we want to start the check with.
10633 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
10634 union ctl_io *starting_io)
10636 union ctl_io *ooa_io;
10639 mtx_assert(&lun->lun_lock, MA_OWNED);
10642 * Run back along the OOA queue, starting with the current
10643 * blocked I/O and going through every I/O before it on the
10644 * queue. If starting_io is NULL, we'll just end up returning
10647 for (ooa_io = starting_io; ooa_io != NULL;
10648 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
10652 * This routine just checks to see whether
10653 * cur_blocked is blocked by ooa_io, which is ahead
10654 * of it in the queue. It doesn't queue/dequeue
10657 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
10659 case CTL_ACTION_BLOCK:
10660 case CTL_ACTION_OVERLAP:
10661 case CTL_ACTION_OVERLAP_TAG:
10662 case CTL_ACTION_SKIP:
10663 case CTL_ACTION_ERROR:
10665 break; /* NOTREACHED */
10666 case CTL_ACTION_PASS:
10669 panic("invalid action %d", action);
10670 break; /* NOTREACHED */
10674 return (CTL_ACTION_PASS);
10679 * - An I/O has just completed, and has been removed from the per-LUN OOA
10680 * queue, so some items on the blocked queue may now be unblocked.
10683 ctl_check_blocked(struct ctl_lun *lun)
10685 struct ctl_softc *softc = lun->ctl_softc;
10686 union ctl_io *cur_blocked, *next_blocked;
10688 mtx_assert(&lun->lun_lock, MA_OWNED);
10691 * Run forward from the head of the blocked queue, checking each
10692 * entry against the I/Os prior to it on the OOA queue to see if
10693 * there is still any blockage.
10695 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
10696 * with our removing a variable on it while it is traversing the
10699 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
10700 cur_blocked != NULL; cur_blocked = next_blocked) {
10701 union ctl_io *prev_ooa;
10704 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
10707 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
10708 ctl_ooaq, ooa_links);
10711 * If cur_blocked happens to be the first item in the OOA
10712 * queue now, prev_ooa will be NULL, and the action
10713 * returned will just be CTL_ACTION_PASS.
10715 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
10718 case CTL_ACTION_BLOCK:
10719 /* Nothing to do here, still blocked */
10721 case CTL_ACTION_OVERLAP:
10722 case CTL_ACTION_OVERLAP_TAG:
10724 * This shouldn't happen! In theory we've already
10725 * checked this command for overlap...
10728 case CTL_ACTION_PASS:
10729 case CTL_ACTION_SKIP: {
10730 const struct ctl_cmd_entry *entry;
10733 * The skip case shouldn't happen, this transaction
10734 * should have never made it onto the blocked queue.
10737 * This I/O is no longer blocked, we can remove it
10738 * from the blocked queue. Since this is a TAILQ
10739 * (doubly linked list), we can do O(1) removals
10740 * from any place on the list.
10742 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
10744 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
10746 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
10747 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){
10749 * Need to send IO back to original side to
10752 union ctl_ha_msg msg_info;
10754 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
10755 msg_info.hdr.original_sc =
10756 cur_blocked->io_hdr.original_sc;
10757 msg_info.hdr.serializing_sc = cur_blocked;
10758 msg_info.hdr.msg_type = CTL_MSG_R2R;
10759 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
10760 sizeof(msg_info.hdr), M_NOWAIT);
10763 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
10766 * Check this I/O for LUN state changes that may
10767 * have happened while this command was blocked.
10768 * The LUN state may have been changed by a command
10769 * ahead of us in the queue, so we need to re-check
10770 * for any states that can be caused by SCSI
10773 if (ctl_scsiio_lun_check(lun, entry,
10774 &cur_blocked->scsiio) == 0) {
10775 cur_blocked->io_hdr.flags |=
10776 CTL_FLAG_IS_WAS_ON_RTR;
10777 ctl_enqueue_rtr(cur_blocked);
10779 ctl_done(cur_blocked);
10784 * This probably shouldn't happen -- we shouldn't
10785 * get CTL_ACTION_ERROR, or anything else.
10791 return (CTL_RETVAL_COMPLETE);
10795 * This routine (with one exception) checks LUN flags that can be set by
10796 * commands ahead of us in the OOA queue. These flags have to be checked
10797 * when a command initially comes in, and when we pull a command off the
10798 * blocked queue and are preparing to execute it. The reason we have to
10799 * check these flags for commands on the blocked queue is that the LUN
10800 * state may have been changed by a command ahead of us while we're on the
10803 * Ordering is somewhat important with these checks, so please pay
10804 * careful attention to the placement of any new checks.
10807 ctl_scsiio_lun_check(struct ctl_lun *lun,
10808 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
10810 struct ctl_softc *softc = lun->ctl_softc;
10816 mtx_assert(&lun->lun_lock, MA_OWNED);
10819 * If this shelf is a secondary shelf controller, we may have to
10820 * reject some commands disallowed by HA mode and link state.
10822 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
10823 if (softc->ha_link == CTL_HA_LINK_OFFLINE &&
10824 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
10825 ctl_set_lun_unavail(ctsio);
10829 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 &&
10830 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
10831 ctl_set_lun_transit(ctsio);
10835 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY &&
10836 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) {
10837 ctl_set_lun_standby(ctsio);
10842 /* The rest of checks are only done on executing side */
10843 if (softc->ha_mode == CTL_HA_MODE_XFER)
10847 if (entry->pattern & CTL_LUN_PAT_WRITE) {
10849 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) {
10850 ctl_set_hw_write_protected(ctsio);
10854 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT]
10855 .eca_and_aen & SCP_SWP) != 0) {
10856 ctl_set_sense(ctsio, /*current_error*/ 1,
10857 /*sense_key*/ SSD_KEY_DATA_PROTECT,
10858 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
10865 * Check for a reservation conflict. If this command isn't allowed
10866 * even on reserved LUNs, and if this initiator isn't the one who
10867 * reserved us, reject the command with a reservation conflict.
10869 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
10870 if ((lun->flags & CTL_LUN_RESERVED)
10871 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
10872 if (lun->res_idx != residx) {
10873 ctl_set_reservation_conflict(ctsio);
10879 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
10880 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
10881 /* No reservation or command is allowed. */;
10882 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
10883 (lun->res_type == SPR_TYPE_WR_EX ||
10884 lun->res_type == SPR_TYPE_WR_EX_RO ||
10885 lun->res_type == SPR_TYPE_WR_EX_AR)) {
10886 /* The command is allowed for Write Exclusive resv. */;
10889 * if we aren't registered or it's a res holder type
10890 * reservation and this isn't the res holder then set a
10893 if (ctl_get_prkey(lun, residx) == 0
10894 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
10895 ctl_set_reservation_conflict(ctsio);
10901 if ((lun->flags & CTL_LUN_OFFLINE)
10902 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0)) {
10903 ctl_set_lun_not_ready(ctsio);
10908 if ((lun->flags & CTL_LUN_STOPPED)
10909 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
10910 /* "Logical unit not ready, initializing cmd. required" */
10911 ctl_set_lun_stopped(ctsio);
10916 if ((lun->flags & CTL_LUN_INOPERABLE)
10917 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
10918 /* "Medium format corrupted" */
10919 ctl_set_medium_format_corrupted(ctsio);
10929 ctl_failover_io(union ctl_io *io, int have_lock)
10931 ctl_set_busy(&io->scsiio);
10936 ctl_failover_lun(union ctl_io *rio)
10938 struct ctl_softc *softc = control_softc;
10939 struct ctl_lun *lun;
10940 struct ctl_io_hdr *io, *next_io;
10943 targ_lun = rio->io_hdr.nexus.targ_mapped_lun;
10944 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun));
10946 /* Find and lock the LUN. */
10947 mtx_lock(&softc->ctl_lock);
10948 if ((targ_lun < CTL_MAX_LUNS) &&
10949 ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
10950 mtx_lock(&lun->lun_lock);
10951 mtx_unlock(&softc->ctl_lock);
10952 if (lun->flags & CTL_LUN_DISABLED) {
10953 mtx_unlock(&lun->lun_lock);
10957 mtx_unlock(&softc->ctl_lock);
10961 if (softc->ha_mode == CTL_HA_MODE_XFER) {
10962 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
10963 /* We are master */
10964 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
10965 if (io->flags & CTL_FLAG_IO_ACTIVE) {
10966 io->flags |= CTL_FLAG_ABORT;
10967 io->flags |= CTL_FLAG_FAILOVER;
10968 } else { /* This can be only due to DATAMOVE */
10969 io->msg_type = CTL_MSG_DATAMOVE_DONE;
10970 io->flags &= ~CTL_FLAG_DMA_INPROG;
10971 io->flags |= CTL_FLAG_IO_ACTIVE;
10972 io->port_status = 31340;
10973 ctl_enqueue_isc((union ctl_io *)io);
10977 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
10978 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
10979 if (io->flags & CTL_FLAG_IO_ACTIVE) {
10980 io->flags |= CTL_FLAG_FAILOVER;
10982 ctl_set_busy(&((union ctl_io *)io)->
10984 ctl_done((union ctl_io *)io);
10988 } else { /* SERIALIZE modes */
10989 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links,
10991 /* We are master */
10992 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
10993 TAILQ_REMOVE(&lun->blocked_queue, io,
10995 io->flags &= ~CTL_FLAG_BLOCKED;
10996 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
10997 ctl_free_io((union ctl_io *)io);
11000 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11001 /* We are master */
11002 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11003 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11004 ctl_free_io((union ctl_io *)io);
11007 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11008 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11009 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) {
11010 ctl_set_busy(&((union ctl_io *)io)->
11012 ctl_done((union ctl_io *)io);
11016 ctl_check_blocked(lun);
11018 mtx_unlock(&lun->lun_lock);
11022 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio)
11024 struct ctl_lun *lun;
11025 const struct ctl_cmd_entry *entry;
11026 uint32_t initidx, targ_lun;
11033 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11034 if ((targ_lun < CTL_MAX_LUNS)
11035 && ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
11037 * If the LUN is invalid, pretend that it doesn't exist.
11038 * It will go away as soon as all pending I/O has been
11041 mtx_lock(&lun->lun_lock);
11042 if (lun->flags & CTL_LUN_DISABLED) {
11043 mtx_unlock(&lun->lun_lock);
11045 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11046 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11048 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
11049 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
11053 * Every I/O goes into the OOA queue for a
11054 * particular LUN, and stays there until completion.
11057 if (TAILQ_EMPTY(&lun->ooa_queue)) {
11058 lun->idle_time += getsbinuptime() -
11062 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr,
11066 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11067 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11070 /* Get command entry and return error if it is unsuppotyed. */
11071 entry = ctl_validate_command(ctsio);
11072 if (entry == NULL) {
11074 mtx_unlock(&lun->lun_lock);
11078 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11079 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11082 * Check to see whether we can send this command to LUNs that don't
11083 * exist. This should pretty much only be the case for inquiry
11084 * and request sense. Further checks, below, really require having
11085 * a LUN, so we can't really check the command anymore. Just put
11086 * it on the rtr queue.
11089 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) {
11090 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11091 ctl_enqueue_rtr((union ctl_io *)ctsio);
11095 ctl_set_unsupported_lun(ctsio);
11096 ctl_done((union ctl_io *)ctsio);
11097 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11101 * Make sure we support this particular command on this LUN.
11102 * e.g., we don't support writes to the control LUN.
11104 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11105 mtx_unlock(&lun->lun_lock);
11106 ctl_set_invalid_opcode(ctsio);
11107 ctl_done((union ctl_io *)ctsio);
11112 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11116 * If we've got a request sense, it'll clear the contingent
11117 * allegiance condition. Otherwise, if we have a CA condition for
11118 * this initiator, clear it, because it sent down a command other
11119 * than request sense.
11121 if ((ctsio->cdb[0] != REQUEST_SENSE)
11122 && (ctl_is_set(lun->have_ca, initidx)))
11123 ctl_clear_mask(lun->have_ca, initidx);
11127 * If the command has this flag set, it handles its own unit
11128 * attention reporting, we shouldn't do anything. Otherwise we
11129 * check for any pending unit attentions, and send them back to the
11130 * initiator. We only do this when a command initially comes in,
11131 * not when we pull it off the blocked queue.
11133 * According to SAM-3, section 5.3.2, the order that things get
11134 * presented back to the host is basically unit attentions caused
11135 * by some sort of reset event, busy status, reservation conflicts
11136 * or task set full, and finally any other status.
11138 * One issue here is that some of the unit attentions we report
11139 * don't fall into the "reset" category (e.g. "reported luns data
11140 * has changed"). So reporting it here, before the reservation
11141 * check, may be technically wrong. I guess the only thing to do
11142 * would be to check for and report the reset events here, and then
11143 * check for the other unit attention types after we check for a
11144 * reservation conflict.
11146 * XXX KDM need to fix this
11148 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11149 ctl_ua_type ua_type;
11151 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11153 if (ua_type != CTL_UA_NONE) {
11154 mtx_unlock(&lun->lun_lock);
11155 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11156 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11157 ctsio->sense_len = SSD_FULL_SIZE;
11158 ctl_done((union ctl_io *)ctsio);
11164 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11165 mtx_unlock(&lun->lun_lock);
11166 ctl_done((union ctl_io *)ctsio);
11171 * XXX CHD this is where we want to send IO to other side if
11172 * this LUN is secondary on this SC. We will need to make a copy
11173 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11174 * the copy we send as FROM_OTHER.
11175 * We also need to stuff the address of the original IO so we can
11176 * find it easily. Something similar will need be done on the other
11177 * side so when we are done we can find the copy.
11179 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
11180 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 &&
11181 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) {
11182 union ctl_ha_msg msg_info;
11185 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11186 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11187 mtx_unlock(&lun->lun_lock);
11189 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11190 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11191 msg_info.hdr.serializing_sc = NULL;
11192 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11193 msg_info.scsi.tag_num = ctsio->tag_num;
11194 msg_info.scsi.tag_type = ctsio->tag_type;
11195 msg_info.scsi.cdb_len = ctsio->cdb_len;
11196 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11198 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11199 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data),
11200 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) {
11201 ctl_set_busy(ctsio);
11202 ctl_done((union ctl_io *)ctsio);
11208 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11209 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11210 ctl_ooaq, ooa_links))) {
11211 case CTL_ACTION_BLOCK:
11212 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11213 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11215 mtx_unlock(&lun->lun_lock);
11217 case CTL_ACTION_PASS:
11218 case CTL_ACTION_SKIP:
11219 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11220 mtx_unlock(&lun->lun_lock);
11221 ctl_enqueue_rtr((union ctl_io *)ctsio);
11223 case CTL_ACTION_OVERLAP:
11224 mtx_unlock(&lun->lun_lock);
11225 ctl_set_overlapped_cmd(ctsio);
11226 ctl_done((union ctl_io *)ctsio);
11228 case CTL_ACTION_OVERLAP_TAG:
11229 mtx_unlock(&lun->lun_lock);
11230 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11231 ctl_done((union ctl_io *)ctsio);
11233 case CTL_ACTION_ERROR:
11235 mtx_unlock(&lun->lun_lock);
11236 ctl_set_internal_failure(ctsio,
11238 /*retry_count*/ 0);
11239 ctl_done((union ctl_io *)ctsio);
11245 const struct ctl_cmd_entry *
11246 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11248 const struct ctl_cmd_entry *entry;
11249 int service_action;
11251 entry = &ctl_cmd_table[ctsio->cdb[0]];
11253 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11254 if (entry->flags & CTL_CMD_FLAG_SA5) {
11255 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11256 entry = &((const struct ctl_cmd_entry *)
11257 entry->execute)[service_action];
11262 const struct ctl_cmd_entry *
11263 ctl_validate_command(struct ctl_scsiio *ctsio)
11265 const struct ctl_cmd_entry *entry;
11269 entry = ctl_get_cmd_entry(ctsio, &sa);
11270 if (entry->execute == NULL) {
11272 ctl_set_invalid_field(ctsio,
11279 ctl_set_invalid_opcode(ctsio);
11280 ctl_done((union ctl_io *)ctsio);
11283 KASSERT(entry->length > 0,
11284 ("Not defined length for command 0x%02x/0x%02x",
11285 ctsio->cdb[0], ctsio->cdb[1]));
11286 for (i = 1; i < entry->length; i++) {
11287 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11290 ctl_set_invalid_field(ctsio,
11295 /*bit*/ fls(diff) - 1);
11296 ctl_done((union ctl_io *)ctsio);
11303 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11306 switch (lun_type) {
11308 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
11312 if ((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0)
11322 ctl_scsiio(struct ctl_scsiio *ctsio)
11325 const struct ctl_cmd_entry *entry;
11327 retval = CTL_RETVAL_COMPLETE;
11329 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11331 entry = ctl_get_cmd_entry(ctsio, NULL);
11334 * If this I/O has been aborted, just send it straight to
11335 * ctl_done() without executing it.
11337 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11338 ctl_done((union ctl_io *)ctsio);
11343 * All the checks should have been handled by ctl_scsiio_precheck().
11344 * We should be clear now to just execute the I/O.
11346 retval = entry->execute(ctsio);
11353 * Since we only implement one target right now, a bus reset simply resets
11354 * our single target.
11357 ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io)
11359 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET));
11363 ctl_target_reset(struct ctl_softc *softc, union ctl_io *io,
11364 ctl_ua_type ua_type)
11366 struct ctl_port *port;
11367 struct ctl_lun *lun;
11370 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11371 union ctl_ha_msg msg_info;
11373 msg_info.hdr.nexus = io->io_hdr.nexus;
11374 if (ua_type==CTL_UA_TARG_RESET)
11375 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11377 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11378 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11379 msg_info.hdr.original_sc = NULL;
11380 msg_info.hdr.serializing_sc = NULL;
11381 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11382 sizeof(msg_info.task), M_WAITOK);
11386 mtx_lock(&softc->ctl_lock);
11387 port = ctl_io_port(&io->io_hdr);
11388 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11389 if (port != NULL &&
11390 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
11392 retval += ctl_do_lun_reset(lun, io, ua_type);
11394 mtx_unlock(&softc->ctl_lock);
11395 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11400 * The LUN should always be set. The I/O is optional, and is used to
11401 * distinguish between I/Os sent by this initiator, and by other
11402 * initiators. We set unit attention for initiators other than this one.
11403 * SAM-3 is vague on this point. It does say that a unit attention should
11404 * be established for other initiators when a LUN is reset (see section
11405 * 5.7.3), but it doesn't specifically say that the unit attention should
11406 * be established for this particular initiator when a LUN is reset. Here
11407 * is the relevant text, from SAM-3 rev 8:
11409 * 5.7.2 When a SCSI initiator port aborts its own tasks
11411 * When a SCSI initiator port causes its own task(s) to be aborted, no
11412 * notification that the task(s) have been aborted shall be returned to
11413 * the SCSI initiator port other than the completion response for the
11414 * command or task management function action that caused the task(s) to
11415 * be aborted and notification(s) associated with related effects of the
11416 * action (e.g., a reset unit attention condition).
11418 * XXX KDM for now, we're setting unit attention for all initiators.
11421 ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11431 mtx_lock(&lun->lun_lock);
11433 * Run through the OOA queue and abort each I/O.
11435 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11436 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11437 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11441 * This version sets unit attention for every
11444 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11445 ctl_est_ua_all(lun, initidx, ua_type);
11447 ctl_est_ua_all(lun, -1, ua_type);
11451 * A reset (any kind, really) clears reservations established with
11452 * RESERVE/RELEASE. It does not clear reservations established
11453 * with PERSISTENT RESERVE OUT, but we don't support that at the
11454 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
11455 * reservations made with the RESERVE/RELEASE commands, because
11456 * those commands are obsolete in SPC-3.
11458 lun->flags &= ~CTL_LUN_RESERVED;
11461 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11462 ctl_clear_mask(lun->have_ca, i);
11464 mtx_unlock(&lun->lun_lock);
11470 ctl_lun_reset(struct ctl_softc *softc, union ctl_io *io)
11472 struct ctl_lun *lun;
11476 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11477 mtx_lock(&softc->ctl_lock);
11478 if ((targ_lun >= CTL_MAX_LUNS) ||
11479 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11480 mtx_unlock(&softc->ctl_lock);
11481 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11484 retval = ctl_do_lun_reset(lun, io, CTL_UA_LUN_RESET);
11485 mtx_unlock(&softc->ctl_lock);
11486 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11488 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) {
11489 union ctl_ha_msg msg_info;
11491 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11492 msg_info.hdr.nexus = io->io_hdr.nexus;
11493 msg_info.task.task_action = CTL_TASK_LUN_RESET;
11494 msg_info.hdr.original_sc = NULL;
11495 msg_info.hdr.serializing_sc = NULL;
11496 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11497 sizeof(msg_info.task), M_WAITOK);
11503 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11508 mtx_assert(&lun->lun_lock, MA_OWNED);
11511 * Run through the OOA queue and attempt to find the given I/O.
11512 * The target port, initiator ID, tag type and tag number have to
11513 * match the values that we got from the initiator. If we have an
11514 * untagged command to abort, simply abort the first untagged command
11515 * we come to. We only allow one untagged command at a time of course.
11517 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11518 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11520 if ((targ_port == UINT32_MAX ||
11521 targ_port == xio->io_hdr.nexus.targ_port) &&
11522 (init_id == UINT32_MAX ||
11523 init_id == xio->io_hdr.nexus.initid)) {
11524 if (targ_port != xio->io_hdr.nexus.targ_port ||
11525 init_id != xio->io_hdr.nexus.initid)
11526 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
11527 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11528 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11529 union ctl_ha_msg msg_info;
11531 msg_info.hdr.nexus = xio->io_hdr.nexus;
11532 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11533 msg_info.task.tag_num = xio->scsiio.tag_num;
11534 msg_info.task.tag_type = xio->scsiio.tag_type;
11535 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11536 msg_info.hdr.original_sc = NULL;
11537 msg_info.hdr.serializing_sc = NULL;
11538 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11539 sizeof(msg_info.task), M_NOWAIT);
11546 ctl_abort_task_set(union ctl_io *io)
11548 struct ctl_softc *softc = control_softc;
11549 struct ctl_lun *lun;
11555 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11556 mtx_lock(&softc->ctl_lock);
11557 if ((targ_lun >= CTL_MAX_LUNS) ||
11558 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11559 mtx_unlock(&softc->ctl_lock);
11560 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11564 mtx_lock(&lun->lun_lock);
11565 mtx_unlock(&softc->ctl_lock);
11566 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
11567 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11568 io->io_hdr.nexus.initid,
11569 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11570 } else { /* CTL_TASK_CLEAR_TASK_SET */
11571 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
11572 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11574 mtx_unlock(&lun->lun_lock);
11575 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11580 ctl_i_t_nexus_reset(union ctl_io *io)
11582 struct ctl_softc *softc = control_softc;
11583 struct ctl_lun *lun;
11586 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11587 union ctl_ha_msg msg_info;
11589 msg_info.hdr.nexus = io->io_hdr.nexus;
11590 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET;
11591 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11592 msg_info.hdr.original_sc = NULL;
11593 msg_info.hdr.serializing_sc = NULL;
11594 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11595 sizeof(msg_info.task), M_WAITOK);
11598 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11599 mtx_lock(&softc->ctl_lock);
11600 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11601 mtx_lock(&lun->lun_lock);
11602 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11603 io->io_hdr.nexus.initid, 1);
11605 ctl_clear_mask(lun->have_ca, initidx);
11607 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx))
11608 lun->flags &= ~CTL_LUN_RESERVED;
11609 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS);
11610 mtx_unlock(&lun->lun_lock);
11612 mtx_unlock(&softc->ctl_lock);
11613 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11618 ctl_abort_task(union ctl_io *io)
11621 struct ctl_lun *lun;
11622 struct ctl_softc *softc;
11625 char printbuf[128];
11630 softc = control_softc;
11636 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11637 mtx_lock(&softc->ctl_lock);
11638 if ((targ_lun >= CTL_MAX_LUNS) ||
11639 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11640 mtx_unlock(&softc->ctl_lock);
11641 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11646 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
11647 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
11650 mtx_lock(&lun->lun_lock);
11651 mtx_unlock(&softc->ctl_lock);
11653 * Run through the OOA queue and attempt to find the given I/O.
11654 * The target port, initiator ID, tag type and tag number have to
11655 * match the values that we got from the initiator. If we have an
11656 * untagged command to abort, simply abort the first untagged command
11657 * we come to. We only allow one untagged command at a time of course.
11659 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11660 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11662 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
11664 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
11665 lun->lun, xio->scsiio.tag_num,
11666 xio->scsiio.tag_type,
11667 (xio->io_hdr.blocked_links.tqe_prev
11668 == NULL) ? "" : " BLOCKED",
11669 (xio->io_hdr.flags &
11670 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
11671 (xio->io_hdr.flags &
11672 CTL_FLAG_ABORT) ? " ABORT" : "",
11673 (xio->io_hdr.flags &
11674 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
11675 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
11677 printf("%s\n", sbuf_data(&sb));
11680 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
11681 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
11682 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
11686 * If the abort says that the task is untagged, the
11687 * task in the queue must be untagged. Otherwise,
11688 * we just check to see whether the tag numbers
11689 * match. This is because the QLogic firmware
11690 * doesn't pass back the tag type in an abort
11694 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
11695 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
11696 || (xio->scsiio.tag_num == io->taskio.tag_num))
11699 * XXX KDM we've got problems with FC, because it
11700 * doesn't send down a tag type with aborts. So we
11701 * can only really go by the tag number...
11702 * This may cause problems with parallel SCSI.
11703 * Need to figure that out!!
11705 if (xio->scsiio.tag_num == io->taskio.tag_num) {
11706 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11708 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
11709 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11710 union ctl_ha_msg msg_info;
11712 msg_info.hdr.nexus = io->io_hdr.nexus;
11713 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11714 msg_info.task.tag_num = io->taskio.tag_num;
11715 msg_info.task.tag_type = io->taskio.tag_type;
11716 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11717 msg_info.hdr.original_sc = NULL;
11718 msg_info.hdr.serializing_sc = NULL;
11720 printf("Sent Abort to other side\n");
11722 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11723 sizeof(msg_info.task), M_NOWAIT);
11726 printf("ctl_abort_task: found I/O to abort\n");
11730 mtx_unlock(&lun->lun_lock);
11734 * This isn't really an error. It's entirely possible for
11735 * the abort and command completion to cross on the wire.
11736 * This is more of an informative/diagnostic error.
11739 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
11740 "%u:%u:%u tag %d type %d\n",
11741 io->io_hdr.nexus.initid,
11742 io->io_hdr.nexus.targ_port,
11743 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
11744 io->taskio.tag_type);
11747 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11752 ctl_query_task(union ctl_io *io, int task_set)
11755 struct ctl_lun *lun;
11756 struct ctl_softc *softc;
11760 softc = control_softc;
11761 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11762 mtx_lock(&softc->ctl_lock);
11763 if ((targ_lun >= CTL_MAX_LUNS) ||
11764 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11765 mtx_unlock(&softc->ctl_lock);
11766 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11769 mtx_lock(&lun->lun_lock);
11770 mtx_unlock(&softc->ctl_lock);
11771 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11772 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11774 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
11775 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
11776 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
11779 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) {
11784 mtx_unlock(&lun->lun_lock);
11786 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
11788 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11793 ctl_query_async_event(union ctl_io *io)
11795 struct ctl_lun *lun;
11796 struct ctl_softc *softc;
11798 uint32_t targ_lun, initidx;
11800 softc = control_softc;
11801 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11802 mtx_lock(&softc->ctl_lock);
11803 if ((targ_lun >= CTL_MAX_LUNS) ||
11804 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11805 mtx_unlock(&softc->ctl_lock);
11806 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11809 mtx_lock(&lun->lun_lock);
11810 mtx_unlock(&softc->ctl_lock);
11811 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11812 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp);
11813 mtx_unlock(&lun->lun_lock);
11814 if (ua != CTL_UA_NONE)
11815 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
11817 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11822 ctl_run_task(union ctl_io *io)
11824 struct ctl_softc *softc = control_softc;
11827 CTL_DEBUG_PRINT(("ctl_run_task\n"));
11828 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
11829 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type));
11830 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED;
11831 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp));
11832 switch (io->taskio.task_action) {
11833 case CTL_TASK_ABORT_TASK:
11834 retval = ctl_abort_task(io);
11836 case CTL_TASK_ABORT_TASK_SET:
11837 case CTL_TASK_CLEAR_TASK_SET:
11838 retval = ctl_abort_task_set(io);
11840 case CTL_TASK_CLEAR_ACA:
11842 case CTL_TASK_I_T_NEXUS_RESET:
11843 retval = ctl_i_t_nexus_reset(io);
11845 case CTL_TASK_LUN_RESET:
11846 retval = ctl_lun_reset(softc, io);
11848 case CTL_TASK_TARGET_RESET:
11849 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET);
11851 case CTL_TASK_BUS_RESET:
11852 retval = ctl_bus_reset(softc, io);
11854 case CTL_TASK_PORT_LOGIN:
11856 case CTL_TASK_PORT_LOGOUT:
11858 case CTL_TASK_QUERY_TASK:
11859 retval = ctl_query_task(io, 0);
11861 case CTL_TASK_QUERY_TASK_SET:
11862 retval = ctl_query_task(io, 1);
11864 case CTL_TASK_QUERY_ASYNC_EVENT:
11865 retval = ctl_query_async_event(io);
11868 printf("%s: got unknown task management event %d\n",
11869 __func__, io->taskio.task_action);
11873 io->io_hdr.status = CTL_SUCCESS;
11875 io->io_hdr.status = CTL_ERROR;
11880 * For HA operation. Handle commands that come in from the other
11884 ctl_handle_isc(union ctl_io *io)
11887 struct ctl_lun *lun;
11888 struct ctl_softc *softc = control_softc;
11891 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11892 lun = softc->ctl_luns[targ_lun];
11894 switch (io->io_hdr.msg_type) {
11895 case CTL_MSG_SERIALIZE:
11896 free_io = ctl_serialize_other_sc_cmd(&io->scsiio);
11898 case CTL_MSG_R2R: {
11899 const struct ctl_cmd_entry *entry;
11902 * This is only used in SER_ONLY mode.
11905 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
11906 mtx_lock(&lun->lun_lock);
11907 if (ctl_scsiio_lun_check(lun,
11908 entry, (struct ctl_scsiio *)io) != 0) {
11909 mtx_unlock(&lun->lun_lock);
11913 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11914 mtx_unlock(&lun->lun_lock);
11915 ctl_enqueue_rtr(io);
11918 case CTL_MSG_FINISH_IO:
11919 if (softc->ha_mode == CTL_HA_MODE_XFER) {
11924 mtx_lock(&lun->lun_lock);
11925 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
11927 ctl_check_blocked(lun);
11928 mtx_unlock(&lun->lun_lock);
11931 case CTL_MSG_PERS_ACTION:
11932 ctl_hndl_per_res_out_on_other_sc(
11933 (union ctl_ha_msg *)&io->presio.pr_msg);
11936 case CTL_MSG_BAD_JUJU:
11940 case CTL_MSG_DATAMOVE:
11941 /* Only used in XFER mode */
11943 ctl_datamove_remote(io);
11945 case CTL_MSG_DATAMOVE_DONE:
11946 /* Only used in XFER mode */
11948 io->scsiio.be_move_done(io);
11950 case CTL_MSG_FAILOVER:
11951 ctl_failover_lun(io);
11956 printf("%s: Invalid message type %d\n",
11957 __func__, io->io_hdr.msg_type);
11967 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
11968 * there is no match.
11970 static ctl_lun_error_pattern
11971 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
11973 const struct ctl_cmd_entry *entry;
11974 ctl_lun_error_pattern filtered_pattern, pattern;
11976 pattern = desc->error_pattern;
11979 * XXX KDM we need more data passed into this function to match a
11980 * custom pattern, and we actually need to implement custom pattern
11983 if (pattern & CTL_LUN_PAT_CMD)
11984 return (CTL_LUN_PAT_CMD);
11986 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
11987 return (CTL_LUN_PAT_ANY);
11989 entry = ctl_get_cmd_entry(ctsio, NULL);
11991 filtered_pattern = entry->pattern & pattern;
11994 * If the user requested specific flags in the pattern (e.g.
11995 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
11998 * If the user did not specify any flags, it doesn't matter whether
11999 * or not the command supports the flags.
12001 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12002 (pattern & ~CTL_LUN_PAT_MASK))
12003 return (CTL_LUN_PAT_NONE);
12006 * If the user asked for a range check, see if the requested LBA
12007 * range overlaps with this command's LBA range.
12009 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12015 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12017 return (CTL_LUN_PAT_NONE);
12019 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12020 desc->lba_range.len, FALSE);
12022 * A "pass" means that the LBA ranges don't overlap, so
12023 * this doesn't match the user's range criteria.
12025 if (action == CTL_ACTION_PASS)
12026 return (CTL_LUN_PAT_NONE);
12029 return (filtered_pattern);
12033 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12035 struct ctl_error_desc *desc, *desc2;
12037 mtx_assert(&lun->lun_lock, MA_OWNED);
12039 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12040 ctl_lun_error_pattern pattern;
12042 * Check to see whether this particular command matches
12043 * the pattern in the descriptor.
12045 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12046 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12049 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12050 case CTL_LUN_INJ_ABORTED:
12051 ctl_set_aborted(&io->scsiio);
12053 case CTL_LUN_INJ_MEDIUM_ERR:
12054 ctl_set_medium_error(&io->scsiio,
12055 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) !=
12056 CTL_FLAG_DATA_OUT);
12058 case CTL_LUN_INJ_UA:
12059 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12061 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12063 case CTL_LUN_INJ_CUSTOM:
12065 * We're assuming the user knows what he is doing.
12066 * Just copy the sense information without doing
12069 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12070 MIN(sizeof(desc->custom_sense),
12071 sizeof(io->scsiio.sense_data)));
12072 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12073 io->scsiio.sense_len = SSD_FULL_SIZE;
12074 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12076 case CTL_LUN_INJ_NONE:
12079 * If this is an error injection type we don't know
12080 * about, clear the continuous flag (if it is set)
12081 * so it will get deleted below.
12083 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12087 * By default, each error injection action is a one-shot
12089 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12092 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12098 #ifdef CTL_IO_DELAY
12100 ctl_datamove_timer_wakeup(void *arg)
12104 io = (union ctl_io *)arg;
12108 #endif /* CTL_IO_DELAY */
12111 ctl_datamove(union ctl_io *io)
12113 struct ctl_lun *lun;
12114 void (*fe_datamove)(union ctl_io *io);
12116 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12118 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12120 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12122 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12127 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12128 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12130 sbuf_cat(&sb, path_str);
12131 switch (io->io_hdr.io_type) {
12133 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12134 sbuf_printf(&sb, "\n");
12135 sbuf_cat(&sb, path_str);
12136 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12137 io->scsiio.tag_num, io->scsiio.tag_type);
12140 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12141 "Tag Type: %d\n", io->taskio.task_action,
12142 io->taskio.tag_num, io->taskio.tag_type);
12145 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12146 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12149 sbuf_cat(&sb, path_str);
12150 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12151 (intmax_t)time_uptime - io->io_hdr.start_time);
12153 printf("%s", sbuf_data(&sb));
12155 #endif /* CTL_TIME_IO */
12157 #ifdef CTL_IO_DELAY
12158 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12159 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12162 && (lun->delay_info.datamove_delay > 0)) {
12164 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
12165 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12166 callout_reset(&io->io_hdr.delay_callout,
12167 lun->delay_info.datamove_delay * hz,
12168 ctl_datamove_timer_wakeup, io);
12169 if (lun->delay_info.datamove_type ==
12170 CTL_DELAY_TYPE_ONESHOT)
12171 lun->delay_info.datamove_delay = 0;
12178 * This command has been aborted. Set the port status, so we fail
12181 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12182 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n",
12183 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12184 io->io_hdr.nexus.targ_port,
12185 io->io_hdr.nexus.targ_lun);
12186 io->io_hdr.port_status = 31337;
12188 * Note that the backend, in this case, will get the
12189 * callback in its context. In other cases it may get
12190 * called in the frontend's interrupt thread context.
12192 io->scsiio.be_move_done(io);
12196 /* Don't confuse frontend with zero length data move. */
12197 if (io->scsiio.kern_data_len == 0) {
12198 io->scsiio.be_move_done(io);
12202 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12207 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12209 union ctl_ha_msg msg;
12211 struct bintime cur_bt;
12214 memset(&msg, 0, sizeof(msg));
12215 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12216 msg.hdr.original_sc = io;
12217 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12218 msg.hdr.nexus = io->io_hdr.nexus;
12219 msg.hdr.status = io->io_hdr.status;
12220 msg.scsi.tag_num = io->scsiio.tag_num;
12221 msg.scsi.tag_type = io->scsiio.tag_type;
12222 msg.scsi.scsi_status = io->scsiio.scsi_status;
12223 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12224 io->scsiio.sense_len);
12225 msg.scsi.sense_len = io->scsiio.sense_len;
12226 msg.scsi.sense_residual = io->scsiio.sense_residual;
12227 msg.scsi.fetd_status = io->io_hdr.port_status;
12228 msg.scsi.residual = io->scsiio.residual;
12229 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12230 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12231 ctl_failover_io(io, /*have_lock*/ have_lock);
12234 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12235 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12236 msg.scsi.sense_len, M_WAITOK);
12239 getbinuptime(&cur_bt);
12240 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
12241 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
12243 io->io_hdr.num_dmas++;
12247 * The DMA to the remote side is done, now we need to tell the other side
12248 * we're done so it can continue with its data movement.
12251 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12258 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12259 printf("%s: ISC DMA write failed with error %d", __func__,
12261 ctl_set_internal_failure(&io->scsiio,
12263 /*retry_count*/ rq->ret);
12266 ctl_dt_req_free(rq);
12268 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12269 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12270 free(io->io_hdr.remote_sglist, M_CTL);
12271 io->io_hdr.remote_sglist = NULL;
12272 io->io_hdr.local_sglist = NULL;
12275 * The data is in local and remote memory, so now we need to send
12276 * status (good or back) back to the other side.
12278 ctl_send_datamove_done(io, /*have_lock*/ 0);
12282 * We've moved the data from the host/controller into local memory. Now we
12283 * need to push it over to the remote controller's memory.
12286 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12290 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12291 ctl_datamove_remote_write_cb);
12296 ctl_datamove_remote_write(union ctl_io *io)
12299 void (*fe_datamove)(union ctl_io *io);
12302 * - Get the data from the host/HBA into local memory.
12303 * - DMA memory from the local controller to the remote controller.
12304 * - Send status back to the remote controller.
12307 retval = ctl_datamove_remote_sgl_setup(io);
12311 /* Switch the pointer over so the FETD knows what to do */
12312 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12315 * Use a custom move done callback, since we need to send completion
12316 * back to the other controller, not to the backend on this side.
12318 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12320 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12325 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12334 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12335 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12336 free(io->io_hdr.remote_sglist, M_CTL);
12337 io->io_hdr.remote_sglist = NULL;
12338 io->io_hdr.local_sglist = NULL;
12341 scsi_path_string(io, path_str, sizeof(path_str));
12342 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12343 sbuf_cat(&sb, path_str);
12344 scsi_command_string(&io->scsiio, NULL, &sb);
12345 sbuf_printf(&sb, "\n");
12346 sbuf_cat(&sb, path_str);
12347 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12348 io->scsiio.tag_num, io->scsiio.tag_type);
12349 sbuf_cat(&sb, path_str);
12350 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12351 io->io_hdr.flags, io->io_hdr.status);
12353 printk("%s", sbuf_data(&sb));
12358 * The read is done, now we need to send status (good or bad) back
12359 * to the other side.
12361 ctl_send_datamove_done(io, /*have_lock*/ 0);
12367 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12370 void (*fe_datamove)(union ctl_io *io);
12374 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12375 printf("%s: ISC DMA read failed with error %d\n", __func__,
12377 ctl_set_internal_failure(&io->scsiio,
12379 /*retry_count*/ rq->ret);
12382 ctl_dt_req_free(rq);
12384 /* Switch the pointer over so the FETD knows what to do */
12385 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12388 * Use a custom move done callback, since we need to send completion
12389 * back to the other controller, not to the backend on this side.
12391 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12393 /* XXX KDM add checks like the ones in ctl_datamove? */
12395 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12400 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12402 struct ctl_sg_entry *local_sglist;
12403 uint32_t len_to_go;
12408 local_sglist = io->io_hdr.local_sglist;
12409 len_to_go = io->scsiio.kern_data_len;
12412 * The difficult thing here is that the size of the various
12413 * S/G segments may be different than the size from the
12414 * remote controller. That'll make it harder when DMAing
12415 * the data back to the other side.
12417 for (i = 0; len_to_go > 0; i++) {
12418 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT);
12419 local_sglist[i].addr =
12420 malloc(local_sglist[i].len, M_CTL, M_WAITOK);
12422 len_to_go -= local_sglist[i].len;
12425 * Reset the number of S/G entries accordingly. The original
12426 * number of S/G entries is available in rem_sg_entries.
12428 io->scsiio.kern_sg_entries = i;
12431 printf("%s: kern_sg_entries = %d\n", __func__,
12432 io->scsiio.kern_sg_entries);
12433 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12434 printf("%s: sg[%d] = %p, %lu\n", __func__, i,
12435 local_sglist[i].addr, local_sglist[i].len);
12442 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12443 ctl_ha_dt_cb callback)
12445 struct ctl_ha_dt_req *rq;
12446 struct ctl_sg_entry *remote_sglist, *local_sglist;
12447 uint32_t local_used, remote_used, total_used;
12450 rq = ctl_dt_req_alloc();
12453 * If we failed to allocate the request, and if the DMA didn't fail
12454 * anyway, set busy status. This is just a resource allocation
12458 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12459 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS))
12460 ctl_set_busy(&io->scsiio);
12462 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12463 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
12466 ctl_dt_req_free(rq);
12469 * The data move failed. We need to return status back
12470 * to the other controller. No point in trying to DMA
12471 * data to the remote controller.
12474 ctl_send_datamove_done(io, /*have_lock*/ 0);
12479 local_sglist = io->io_hdr.local_sglist;
12480 remote_sglist = io->io_hdr.remote_sglist;
12486 * Pull/push the data over the wire from/to the other controller.
12487 * This takes into account the possibility that the local and
12488 * remote sglists may not be identical in terms of the size of
12489 * the elements and the number of elements.
12491 * One fundamental assumption here is that the length allocated for
12492 * both the local and remote sglists is identical. Otherwise, we've
12493 * essentially got a coding error of some sort.
12495 isc_ret = CTL_HA_STATUS_SUCCESS;
12496 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12500 rq->command = command;
12504 * Both pointers should be aligned. But it is possible
12505 * that the allocation length is not. They should both
12506 * also have enough slack left over at the end, though,
12507 * to round up to the next 8 byte boundary.
12509 cur_len = MIN(local_sglist[i].len - local_used,
12510 remote_sglist[j].len - remote_used);
12511 rq->size = cur_len;
12513 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12514 tmp_ptr += local_used;
12517 /* Use physical addresses when talking to ISC hardware */
12518 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12519 /* XXX KDM use busdma */
12520 rq->local = vtophys(tmp_ptr);
12522 rq->local = tmp_ptr;
12524 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
12525 ("HA does not support BUS_ADDR"));
12526 rq->local = tmp_ptr;
12529 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12530 tmp_ptr += remote_used;
12531 rq->remote = tmp_ptr;
12533 rq->callback = NULL;
12535 local_used += cur_len;
12536 if (local_used >= local_sglist[i].len) {
12541 remote_used += cur_len;
12542 if (remote_used >= remote_sglist[j].len) {
12546 total_used += cur_len;
12548 if (total_used >= io->scsiio.kern_data_len)
12549 rq->callback = callback;
12552 printf("%s: %s: local %p remote %p size %d\n", __func__,
12553 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
12554 rq->local, rq->remote, rq->size);
12557 isc_ret = ctl_dt_single(rq);
12558 if (isc_ret > CTL_HA_STATUS_SUCCESS)
12561 if (isc_ret != CTL_HA_STATUS_WAIT) {
12570 ctl_datamove_remote_read(union ctl_io *io)
12576 * This will send an error to the other controller in the case of a
12579 retval = ctl_datamove_remote_sgl_setup(io);
12583 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12584 ctl_datamove_remote_read_cb);
12587 * Make sure we free memory if there was an error.. The
12588 * ctl_datamove_remote_xfer() function will send the
12589 * datamove done message, or call the callback with an
12590 * error if there is a problem.
12592 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12593 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12594 free(io->io_hdr.remote_sglist, M_CTL);
12595 io->io_hdr.remote_sglist = NULL;
12596 io->io_hdr.local_sglist = NULL;
12601 * Process a datamove request from the other controller. This is used for
12602 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
12603 * first. Once that is complete, the data gets DMAed into the remote
12604 * controller's memory. For reads, we DMA from the remote controller's
12605 * memory into our memory first, and then move it out to the FETD.
12608 ctl_datamove_remote(union ctl_io *io)
12611 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12613 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12614 ctl_failover_io(io, /*have_lock*/ 0);
12619 * Note that we look for an aborted I/O here, but don't do some of
12620 * the other checks that ctl_datamove() normally does.
12621 * We don't need to run the datamove delay code, since that should
12622 * have been done if need be on the other controller.
12624 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12625 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__,
12626 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12627 io->io_hdr.nexus.targ_port,
12628 io->io_hdr.nexus.targ_lun);
12629 io->io_hdr.port_status = 31338;
12630 ctl_send_datamove_done(io, /*have_lock*/ 0);
12634 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
12635 ctl_datamove_remote_write(io);
12636 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
12637 ctl_datamove_remote_read(io);
12639 io->io_hdr.port_status = 31339;
12640 ctl_send_datamove_done(io, /*have_lock*/ 0);
12645 ctl_process_done(union ctl_io *io)
12647 struct ctl_lun *lun;
12648 struct ctl_softc *softc = control_softc;
12649 void (*fe_done)(union ctl_io *io);
12650 union ctl_ha_msg msg;
12651 uint32_t targ_port = io->io_hdr.nexus.targ_port;
12653 CTL_DEBUG_PRINT(("ctl_process_done\n"));
12654 fe_done = softc->ctl_ports[targ_port]->fe_done;
12657 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12662 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12663 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12665 sbuf_cat(&sb, path_str);
12666 switch (io->io_hdr.io_type) {
12668 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12669 sbuf_printf(&sb, "\n");
12670 sbuf_cat(&sb, path_str);
12671 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12672 io->scsiio.tag_num, io->scsiio.tag_type);
12675 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12676 "Tag Type: %d\n", io->taskio.task_action,
12677 io->taskio.tag_num, io->taskio.tag_type);
12680 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12681 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12684 sbuf_cat(&sb, path_str);
12685 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
12686 (intmax_t)time_uptime - io->io_hdr.start_time);
12688 printf("%s", sbuf_data(&sb));
12690 #endif /* CTL_TIME_IO */
12692 switch (io->io_hdr.io_type) {
12696 if (ctl_debug & CTL_DEBUG_INFO)
12697 ctl_io_error_print(io, NULL);
12701 panic("ctl_process_done: invalid io type %d\n",
12702 io->io_hdr.io_type);
12703 break; /* NOTREACHED */
12706 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12708 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
12709 io->io_hdr.nexus.targ_mapped_lun));
12713 mtx_lock(&lun->lun_lock);
12716 * Check to see if we have any errors to inject here. We only
12717 * inject errors for commands that don't already have errors set.
12719 if ((STAILQ_FIRST(&lun->error_list) != NULL) &&
12720 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
12721 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
12722 ctl_inject_error(lun, io);
12725 * XXX KDM how do we treat commands that aren't completed
12728 * XXX KDM should we also track I/O latency?
12730 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
12731 io->io_hdr.io_type == CTL_IO_SCSI) {
12733 struct bintime cur_bt;
12737 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12739 type = CTL_STATS_READ;
12740 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12742 type = CTL_STATS_WRITE;
12744 type = CTL_STATS_NO_IO;
12746 lun->stats.ports[targ_port].bytes[type] +=
12747 io->scsiio.kern_total_len;
12748 lun->stats.ports[targ_port].operations[type]++;
12750 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
12751 &io->io_hdr.dma_bt);
12752 getbinuptime(&cur_bt);
12753 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
12754 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
12756 lun->stats.ports[targ_port].num_dmas[type] +=
12757 io->io_hdr.num_dmas;
12761 * Remove this from the OOA queue.
12763 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
12765 if (TAILQ_EMPTY(&lun->ooa_queue))
12766 lun->last_busy = getsbinuptime();
12770 * Run through the blocked queue on this LUN and see if anything
12771 * has become unblocked, now that this transaction is done.
12773 ctl_check_blocked(lun);
12776 * If the LUN has been invalidated, free it if there is nothing
12777 * left on its OOA queue.
12779 if ((lun->flags & CTL_LUN_INVALID)
12780 && TAILQ_EMPTY(&lun->ooa_queue)) {
12781 mtx_unlock(&lun->lun_lock);
12782 mtx_lock(&softc->ctl_lock);
12784 mtx_unlock(&softc->ctl_lock);
12786 mtx_unlock(&lun->lun_lock);
12791 * If this command has been aborted, make sure we set the status
12792 * properly. The FETD is responsible for freeing the I/O and doing
12793 * whatever it needs to do to clean up its state.
12795 if (io->io_hdr.flags & CTL_FLAG_ABORT)
12796 ctl_set_task_aborted(&io->scsiio);
12799 * If enabled, print command error status.
12801 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
12802 (ctl_debug & CTL_DEBUG_INFO) != 0)
12803 ctl_io_error_print(io, NULL);
12806 * Tell the FETD or the other shelf controller we're done with this
12807 * command. Note that only SCSI commands get to this point. Task
12808 * management commands are completed above.
12810 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
12811 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) {
12812 memset(&msg, 0, sizeof(msg));
12813 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
12814 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12815 msg.hdr.nexus = io->io_hdr.nexus;
12816 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12817 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data),
12826 * Front end should call this if it doesn't do autosense. When the request
12827 * sense comes back in from the initiator, we'll dequeue this and send it.
12830 ctl_queue_sense(union ctl_io *io)
12832 struct ctl_lun *lun;
12833 struct ctl_port *port;
12834 struct ctl_softc *softc;
12835 uint32_t initidx, targ_lun;
12837 softc = control_softc;
12839 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
12842 * LUN lookup will likely move to the ctl_work_thread() once we
12843 * have our new queueing infrastructure (that doesn't put things on
12844 * a per-LUN queue initially). That is so that we can handle
12845 * things like an INQUIRY to a LUN that we don't have enabled. We
12846 * can't deal with that right now.
12848 mtx_lock(&softc->ctl_lock);
12851 * If we don't have a LUN for this, just toss the sense
12854 port = ctl_io_port(&ctsio->io_hdr);
12855 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
12856 if ((targ_lun < CTL_MAX_LUNS)
12857 && (softc->ctl_luns[targ_lun] != NULL))
12858 lun = softc->ctl_luns[targ_lun];
12862 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12864 mtx_lock(&lun->lun_lock);
12866 * Already have CA set for this LUN...toss the sense information.
12868 if (ctl_is_set(lun->have_ca, initidx)) {
12869 mtx_unlock(&lun->lun_lock);
12873 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data,
12874 MIN(sizeof(lun->pending_sense[initidx]),
12875 sizeof(io->scsiio.sense_data)));
12876 ctl_set_mask(lun->have_ca, initidx);
12877 mtx_unlock(&lun->lun_lock);
12880 mtx_unlock(&softc->ctl_lock);
12884 return (CTL_RETVAL_COMPLETE);
12889 * Primary command inlet from frontend ports. All SCSI and task I/O
12890 * requests must go through this function.
12893 ctl_queue(union ctl_io *io)
12895 struct ctl_port *port;
12897 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
12900 io->io_hdr.start_time = time_uptime;
12901 getbinuptime(&io->io_hdr.start_bt);
12902 #endif /* CTL_TIME_IO */
12904 /* Map FE-specific LUN ID into global one. */
12905 port = ctl_io_port(&io->io_hdr);
12906 io->io_hdr.nexus.targ_mapped_lun =
12907 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
12909 switch (io->io_hdr.io_type) {
12912 if (ctl_debug & CTL_DEBUG_CDB)
12914 ctl_enqueue_incoming(io);
12917 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
12921 return (CTL_RETVAL_COMPLETE);
12924 #ifdef CTL_IO_DELAY
12926 ctl_done_timer_wakeup(void *arg)
12930 io = (union ctl_io *)arg;
12933 #endif /* CTL_IO_DELAY */
12936 ctl_serseq_done(union ctl_io *io)
12938 struct ctl_lun *lun;
12940 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12941 if (lun->be_lun == NULL ||
12942 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF)
12944 mtx_lock(&lun->lun_lock);
12945 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE;
12946 ctl_check_blocked(lun);
12947 mtx_unlock(&lun->lun_lock);
12951 ctl_done(union ctl_io *io)
12955 * Enable this to catch duplicate completion issues.
12958 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
12959 printf("%s: type %d msg %d cdb %x iptl: "
12960 "%u:%u:%u tag 0x%04x "
12961 "flag %#x status %x\n",
12963 io->io_hdr.io_type,
12964 io->io_hdr.msg_type,
12966 io->io_hdr.nexus.initid,
12967 io->io_hdr.nexus.targ_port,
12968 io->io_hdr.nexus.targ_lun,
12969 (io->io_hdr.io_type ==
12971 io->taskio.tag_num :
12972 io->scsiio.tag_num,
12974 io->io_hdr.status);
12976 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
12980 * This is an internal copy of an I/O, and should not go through
12981 * the normal done processing logic.
12983 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
12986 #ifdef CTL_IO_DELAY
12987 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12988 struct ctl_lun *lun;
12990 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12992 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12994 struct ctl_lun *lun;
12996 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12999 && (lun->delay_info.done_delay > 0)) {
13001 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
13002 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13003 callout_reset(&io->io_hdr.delay_callout,
13004 lun->delay_info.done_delay * hz,
13005 ctl_done_timer_wakeup, io);
13006 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13007 lun->delay_info.done_delay = 0;
13011 #endif /* CTL_IO_DELAY */
13013 ctl_enqueue_done(io);
13017 ctl_work_thread(void *arg)
13019 struct ctl_thread *thr = (struct ctl_thread *)arg;
13020 struct ctl_softc *softc = thr->ctl_softc;
13024 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13028 * We handle the queues in this order:
13030 * - done queue (to free up resources, unblock other commands)
13034 * If those queues are empty, we break out of the loop and
13037 mtx_lock(&thr->queue_lock);
13038 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13040 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13041 mtx_unlock(&thr->queue_lock);
13042 ctl_handle_isc(io);
13045 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13047 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13048 /* clear any blocked commands, call fe_done */
13049 mtx_unlock(&thr->queue_lock);
13050 ctl_process_done(io);
13053 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13055 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13056 mtx_unlock(&thr->queue_lock);
13057 if (io->io_hdr.io_type == CTL_IO_TASK)
13060 ctl_scsiio_precheck(softc, &io->scsiio);
13063 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13065 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13066 mtx_unlock(&thr->queue_lock);
13067 retval = ctl_scsiio(&io->scsiio);
13068 if (retval != CTL_RETVAL_COMPLETE)
13069 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13073 /* Sleep until we have something to do. */
13074 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13079 ctl_lun_thread(void *arg)
13081 struct ctl_softc *softc = (struct ctl_softc *)arg;
13082 struct ctl_be_lun *be_lun;
13084 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13087 mtx_lock(&softc->ctl_lock);
13088 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13089 if (be_lun != NULL) {
13090 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13091 mtx_unlock(&softc->ctl_lock);
13092 ctl_create_lun(be_lun);
13096 /* Sleep until we have something to do. */
13097 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13098 PDROP | PRIBIO, "-", 0);
13103 ctl_thresh_thread(void *arg)
13105 struct ctl_softc *softc = (struct ctl_softc *)arg;
13106 struct ctl_lun *lun;
13107 struct scsi_da_rw_recovery_page *rwpage;
13108 struct ctl_logical_block_provisioning_page *page;
13110 union ctl_ha_msg msg;
13111 uint64_t thres, val;
13114 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13117 mtx_lock(&softc->ctl_lock);
13118 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13119 if ((lun->flags & CTL_LUN_DISABLED) ||
13120 (lun->flags & CTL_LUN_OFFLINE) ||
13121 lun->backend->lun_attr == NULL)
13123 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
13124 softc->ha_mode == CTL_HA_MODE_XFER)
13126 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT];
13127 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0)
13130 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT];
13131 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13132 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13134 thres = scsi_4btoul(page->descr[i].count);
13135 thres <<= CTL_LBP_EXPONENT;
13136 switch (page->descr[i].resource) {
13138 attr = "blocksavail";
13141 attr = "blocksused";
13144 attr = "poolblocksavail";
13147 attr = "poolblocksused";
13152 mtx_unlock(&softc->ctl_lock); // XXX
13153 val = lun->backend->lun_attr(
13154 lun->be_lun->be_lun, attr);
13155 mtx_lock(&softc->ctl_lock);
13156 if (val == UINT64_MAX)
13158 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13159 == SLBPPD_ARMING_INC)
13160 e = (val >= thres);
13162 e = (val <= thres);
13166 mtx_lock(&lun->lun_lock);
13168 scsi_u64to8b((uint8_t *)&page->descr[i] -
13169 (uint8_t *)page, lun->ua_tpt_info);
13170 if (lun->lasttpt == 0 ||
13171 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13172 lun->lasttpt = time_uptime;
13173 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13179 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13182 mtx_unlock(&lun->lun_lock);
13184 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
13185 /* Send msg to other side. */
13186 bzero(&msg.ua, sizeof(msg.ua));
13187 msg.hdr.msg_type = CTL_MSG_UA;
13188 msg.hdr.nexus.initid = -1;
13189 msg.hdr.nexus.targ_port = -1;
13190 msg.hdr.nexus.targ_lun = lun->lun;
13191 msg.hdr.nexus.targ_mapped_lun = lun->lun;
13193 msg.ua.ua_set = (set > 0);
13194 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES;
13195 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8);
13196 mtx_unlock(&softc->ctl_lock); // XXX
13197 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13198 sizeof(msg.ua), M_WAITOK);
13199 mtx_lock(&softc->ctl_lock);
13202 mtx_unlock(&softc->ctl_lock);
13203 pause("-", CTL_LBP_PERIOD * hz);
13208 ctl_enqueue_incoming(union ctl_io *io)
13210 struct ctl_softc *softc = control_softc;
13211 struct ctl_thread *thr;
13214 idx = (io->io_hdr.nexus.targ_port * 127 +
13215 io->io_hdr.nexus.initid) % worker_threads;
13216 thr = &softc->threads[idx];
13217 mtx_lock(&thr->queue_lock);
13218 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13219 mtx_unlock(&thr->queue_lock);
13224 ctl_enqueue_rtr(union ctl_io *io)
13226 struct ctl_softc *softc = control_softc;
13227 struct ctl_thread *thr;
13229 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13230 mtx_lock(&thr->queue_lock);
13231 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13232 mtx_unlock(&thr->queue_lock);
13237 ctl_enqueue_done(union ctl_io *io)
13239 struct ctl_softc *softc = control_softc;
13240 struct ctl_thread *thr;
13242 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13243 mtx_lock(&thr->queue_lock);
13244 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13245 mtx_unlock(&thr->queue_lock);
13250 ctl_enqueue_isc(union ctl_io *io)
13252 struct ctl_softc *softc = control_softc;
13253 struct ctl_thread *thr;
13255 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13256 mtx_lock(&thr->queue_lock);
13257 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13258 mtx_unlock(&thr->queue_lock);