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);
1758 softc->open_count = 0;
1761 * Default to actually sending a SYNCHRONIZE CACHE command down to
1764 softc->flags = CTL_FLAG_REAL_SYNC;
1766 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1767 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0,
1768 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)");
1771 * In Copan's HA scheme, the "master" and "slave" roles are
1772 * figured out through the slot the controller is in. Although it
1773 * is an active/active system, someone has to be in charge.
1775 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1776 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1777 "HA head ID (0 - no HA)");
1778 if (softc->ha_id == 0 || softc->ha_id > NUM_TARGET_PORT_GROUPS) {
1779 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1780 softc->is_single = 1;
1781 softc->port_cnt = CTL_MAX_PORTS;
1782 softc->port_min = 0;
1784 softc->port_cnt = CTL_MAX_PORTS / NUM_TARGET_PORT_GROUPS;
1785 softc->port_min = (softc->ha_id - 1) * softc->port_cnt;
1787 softc->port_max = softc->port_min + softc->port_cnt;
1788 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT;
1789 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT;
1791 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1792 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0,
1793 "HA link state (0 - offline, 1 - unknown, 2 - online)");
1795 STAILQ_INIT(&softc->lun_list);
1796 STAILQ_INIT(&softc->pending_lun_queue);
1797 STAILQ_INIT(&softc->fe_list);
1798 STAILQ_INIT(&softc->port_list);
1799 STAILQ_INIT(&softc->be_list);
1800 ctl_tpc_init(softc);
1802 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
1805 printf("ctl: can't allocate %d entry other SC pool, "
1806 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1809 softc->othersc_pool = other_pool;
1811 if (worker_threads <= 0)
1812 worker_threads = max(1, mp_ncpus / 4);
1813 if (worker_threads > CTL_MAX_THREADS)
1814 worker_threads = CTL_MAX_THREADS;
1816 for (i = 0; i < worker_threads; i++) {
1817 struct ctl_thread *thr = &softc->threads[i];
1819 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1820 thr->ctl_softc = softc;
1821 STAILQ_INIT(&thr->incoming_queue);
1822 STAILQ_INIT(&thr->rtr_queue);
1823 STAILQ_INIT(&thr->done_queue);
1824 STAILQ_INIT(&thr->isc_queue);
1826 error = kproc_kthread_add(ctl_work_thread, thr,
1827 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1829 printf("error creating CTL work thread!\n");
1830 ctl_pool_free(other_pool);
1834 error = kproc_kthread_add(ctl_lun_thread, softc,
1835 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
1837 printf("error creating CTL lun thread!\n");
1838 ctl_pool_free(other_pool);
1841 error = kproc_kthread_add(ctl_thresh_thread, softc,
1842 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh");
1844 printf("error creating CTL threshold thread!\n");
1845 ctl_pool_free(other_pool);
1849 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1850 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN,
1851 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head");
1853 if (softc->is_single == 0) {
1854 ctl_frontend_register(&ha_frontend);
1855 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) {
1856 printf("ctl_init: ctl_ha_msg_init failed.\n");
1857 softc->is_single = 1;
1859 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
1860 != CTL_HA_STATUS_SUCCESS) {
1861 printf("ctl_init: ctl_ha_msg_register failed.\n");
1862 softc->is_single = 1;
1871 struct ctl_softc *softc = control_softc;
1872 struct ctl_lun *lun, *next_lun;
1874 if (softc->is_single == 0) {
1875 ctl_ha_msg_shutdown(softc);
1876 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL)
1877 != CTL_HA_STATUS_SUCCESS)
1878 printf("%s: ctl_ha_msg_deregister failed.\n", __func__);
1879 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS)
1880 printf("%s: ctl_ha_msg_destroy failed.\n", __func__);
1881 ctl_frontend_deregister(&ha_frontend);
1884 mtx_lock(&softc->ctl_lock);
1889 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
1890 next_lun = STAILQ_NEXT(lun, links);
1894 mtx_unlock(&softc->ctl_lock);
1897 ctl_shutdown_thread(softc->work_thread);
1898 mtx_destroy(&softc->queue_lock);
1901 ctl_tpc_shutdown(softc);
1902 uma_zdestroy(softc->io_zone);
1903 mtx_destroy(&softc->ctl_lock);
1905 destroy_dev(softc->dev);
1907 sysctl_ctx_free(&softc->sysctl_ctx);
1909 free(control_softc, M_DEVBUF);
1910 control_softc = NULL;
1914 ctl_module_event_handler(module_t mod, int what, void *arg)
1919 return (ctl_init());
1923 return (EOPNOTSUPP);
1928 * XXX KDM should we do some access checks here? Bump a reference count to
1929 * prevent a CTL module from being unloaded while someone has it open?
1932 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1938 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1944 * Remove an initiator by port number and initiator ID.
1945 * Returns 0 for success, -1 for failure.
1948 ctl_remove_initiator(struct ctl_port *port, int iid)
1950 struct ctl_softc *softc = port->ctl_softc;
1952 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1954 if (iid > CTL_MAX_INIT_PER_PORT) {
1955 printf("%s: initiator ID %u > maximun %u!\n",
1956 __func__, iid, CTL_MAX_INIT_PER_PORT);
1960 mtx_lock(&softc->ctl_lock);
1961 port->wwpn_iid[iid].in_use--;
1962 port->wwpn_iid[iid].last_use = time_uptime;
1963 mtx_unlock(&softc->ctl_lock);
1964 ctl_isc_announce_iid(port, iid);
1970 * Add an initiator to the initiator map.
1971 * Returns iid for success, < 0 for failure.
1974 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
1976 struct ctl_softc *softc = port->ctl_softc;
1980 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1982 if (iid >= CTL_MAX_INIT_PER_PORT) {
1983 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
1984 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
1989 mtx_lock(&softc->ctl_lock);
1991 if (iid < 0 && (wwpn != 0 || name != NULL)) {
1992 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1993 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
1997 if (name != NULL && port->wwpn_iid[i].name != NULL &&
1998 strcmp(name, port->wwpn_iid[i].name) == 0) {
2006 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2007 if (port->wwpn_iid[i].in_use == 0 &&
2008 port->wwpn_iid[i].wwpn == 0 &&
2009 port->wwpn_iid[i].name == NULL) {
2018 best_time = INT32_MAX;
2019 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2020 if (port->wwpn_iid[i].in_use == 0) {
2021 if (port->wwpn_iid[i].last_use < best_time) {
2023 best_time = port->wwpn_iid[i].last_use;
2031 mtx_unlock(&softc->ctl_lock);
2036 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
2038 * This is not an error yet.
2040 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
2042 printf("%s: port %d iid %u WWPN %#jx arrived"
2043 " again\n", __func__, port->targ_port,
2044 iid, (uintmax_t)wwpn);
2048 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
2049 strcmp(name, port->wwpn_iid[iid].name) == 0) {
2051 printf("%s: port %d iid %u name '%s' arrived"
2052 " again\n", __func__, port->targ_port,
2059 * This is an error, but what do we do about it? The
2060 * driver is telling us we have a new WWPN for this
2061 * initiator ID, so we pretty much need to use it.
2063 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
2064 " but WWPN %#jx '%s' is still at that address\n",
2065 __func__, port->targ_port, iid, wwpn, name,
2066 (uintmax_t)port->wwpn_iid[iid].wwpn,
2067 port->wwpn_iid[iid].name);
2070 * XXX KDM clear have_ca and ua_pending on each LUN for
2075 free(port->wwpn_iid[iid].name, M_CTL);
2076 port->wwpn_iid[iid].name = name;
2077 port->wwpn_iid[iid].wwpn = wwpn;
2078 port->wwpn_iid[iid].in_use++;
2079 mtx_unlock(&softc->ctl_lock);
2080 ctl_isc_announce_iid(port, iid);
2086 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
2090 switch (port->port_type) {
2093 struct scsi_transportid_fcp *id =
2094 (struct scsi_transportid_fcp *)buf;
2095 if (port->wwpn_iid[iid].wwpn == 0)
2097 memset(id, 0, sizeof(*id));
2098 id->format_protocol = SCSI_PROTO_FC;
2099 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
2100 return (sizeof(*id));
2102 case CTL_PORT_ISCSI:
2104 struct scsi_transportid_iscsi_port *id =
2105 (struct scsi_transportid_iscsi_port *)buf;
2106 if (port->wwpn_iid[iid].name == NULL)
2109 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
2111 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
2112 len = roundup2(min(len, 252), 4);
2113 scsi_ulto2b(len, id->additional_length);
2114 return (sizeof(*id) + len);
2118 struct scsi_transportid_sas *id =
2119 (struct scsi_transportid_sas *)buf;
2120 if (port->wwpn_iid[iid].wwpn == 0)
2122 memset(id, 0, sizeof(*id));
2123 id->format_protocol = SCSI_PROTO_SAS;
2124 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
2125 return (sizeof(*id));
2129 struct scsi_transportid_spi *id =
2130 (struct scsi_transportid_spi *)buf;
2131 memset(id, 0, sizeof(*id));
2132 id->format_protocol = SCSI_PROTO_SPI;
2133 scsi_ulto2b(iid, id->scsi_addr);
2134 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
2135 return (sizeof(*id));
2141 * Serialize a command that went down the "wrong" side, and so was sent to
2142 * this controller for execution. The logic is a little different than the
2143 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
2144 * sent back to the other side, but in the success case, we execute the
2145 * command on this side (XFER mode) or tell the other side to execute it
2149 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
2151 struct ctl_softc *softc = control_softc;
2152 union ctl_ha_msg msg_info;
2153 struct ctl_port *port;
2154 struct ctl_lun *lun;
2155 const struct ctl_cmd_entry *entry;
2159 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
2160 mtx_lock(&softc->ctl_lock);
2162 /* Make sure that we know about this port. */
2163 port = ctl_io_port(&ctsio->io_hdr);
2164 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) {
2165 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2170 /* Make sure that we know about this LUN. */
2171 if ((targ_lun < CTL_MAX_LUNS) &&
2172 ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
2173 mtx_lock(&lun->lun_lock);
2174 mtx_unlock(&softc->ctl_lock);
2176 * If the LUN is invalid, pretend that it doesn't exist.
2177 * It will go away as soon as all pending I/O has been
2180 if (lun->flags & CTL_LUN_DISABLED) {
2181 mtx_unlock(&lun->lun_lock);
2185 mtx_unlock(&softc->ctl_lock);
2190 * The other node would not send this request to us unless
2191 * received announce that we are primary node for this LUN.
2192 * If this LUN does not exist now, it is probably result of
2193 * a race, so respond to initiator in the most opaque way.
2195 ctl_set_busy(ctsio);
2199 entry = ctl_get_cmd_entry(ctsio, NULL);
2200 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
2201 mtx_unlock(&lun->lun_lock);
2205 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
2206 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = lun->be_lun;
2209 * Every I/O goes into the OOA queue for a
2210 * particular LUN, and stays there until completion.
2213 if (TAILQ_EMPTY(&lun->ooa_queue))
2214 lun->idle_time += getsbinuptime() - lun->last_busy;
2216 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2218 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
2219 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
2221 case CTL_ACTION_BLOCK:
2222 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
2223 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
2225 mtx_unlock(&lun->lun_lock);
2227 case CTL_ACTION_PASS:
2228 case CTL_ACTION_SKIP:
2229 if (softc->ha_mode == CTL_HA_MODE_XFER) {
2230 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
2231 ctl_enqueue_rtr((union ctl_io *)ctsio);
2232 mtx_unlock(&lun->lun_lock);
2234 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
2235 mtx_unlock(&lun->lun_lock);
2237 /* send msg back to other side */
2238 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2239 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
2240 msg_info.hdr.msg_type = CTL_MSG_R2R;
2241 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2242 sizeof(msg_info.hdr), M_WAITOK);
2245 case CTL_ACTION_OVERLAP:
2246 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2247 mtx_unlock(&lun->lun_lock);
2248 ctl_set_overlapped_cmd(ctsio);
2250 case CTL_ACTION_OVERLAP_TAG:
2251 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2252 mtx_unlock(&lun->lun_lock);
2253 ctl_set_overlapped_tag(ctsio, ctsio->tag_num);
2255 case CTL_ACTION_ERROR:
2257 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2258 mtx_unlock(&lun->lun_lock);
2260 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2263 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
2264 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2265 msg_info.hdr.serializing_sc = NULL;
2266 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
2267 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2268 sizeof(msg_info.scsi), M_WAITOK);
2276 * Returns 0 for success, errno for failure.
2279 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2280 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2284 mtx_lock(&lun->lun_lock);
2285 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2286 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2288 struct ctl_ooa_entry *entry;
2291 * If we've got more than we can fit, just count the
2292 * remaining entries.
2294 if (*cur_fill_num >= ooa_hdr->alloc_num)
2297 entry = &kern_entries[*cur_fill_num];
2299 entry->tag_num = io->scsiio.tag_num;
2300 entry->lun_num = lun->lun;
2302 entry->start_bt = io->io_hdr.start_bt;
2304 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2305 entry->cdb_len = io->scsiio.cdb_len;
2306 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2307 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2309 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2310 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2312 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2313 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2315 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2316 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2318 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2319 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2321 mtx_unlock(&lun->lun_lock);
2325 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2326 size_t error_str_len)
2330 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2332 if (copyin(user_addr, kptr, len) != 0) {
2333 snprintf(error_str, error_str_len, "Error copying %d bytes "
2334 "from user address %p to kernel address %p", len,
2344 ctl_free_args(int num_args, struct ctl_be_arg *args)
2351 for (i = 0; i < num_args; i++) {
2352 free(args[i].kname, M_CTL);
2353 free(args[i].kvalue, M_CTL);
2359 static struct ctl_be_arg *
2360 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
2361 char *error_str, size_t error_str_len)
2363 struct ctl_be_arg *args;
2366 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
2367 error_str, error_str_len);
2372 for (i = 0; i < num_args; i++) {
2373 args[i].kname = NULL;
2374 args[i].kvalue = NULL;
2377 for (i = 0; i < num_args; i++) {
2380 args[i].kname = ctl_copyin_alloc(args[i].name,
2381 args[i].namelen, error_str, error_str_len);
2382 if (args[i].kname == NULL)
2385 if (args[i].kname[args[i].namelen - 1] != '\0') {
2386 snprintf(error_str, error_str_len, "Argument %d "
2387 "name is not NUL-terminated", i);
2391 if (args[i].flags & CTL_BEARG_RD) {
2392 tmpptr = ctl_copyin_alloc(args[i].value,
2393 args[i].vallen, error_str, error_str_len);
2396 if ((args[i].flags & CTL_BEARG_ASCII)
2397 && (tmpptr[args[i].vallen - 1] != '\0')) {
2398 snprintf(error_str, error_str_len, "Argument "
2399 "%d value is not NUL-terminated", i);
2402 args[i].kvalue = tmpptr;
2404 args[i].kvalue = malloc(args[i].vallen,
2405 M_CTL, M_WAITOK | M_ZERO);
2412 ctl_free_args(num_args, args);
2418 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2422 for (i = 0; i < num_args; i++) {
2423 if (args[i].flags & CTL_BEARG_WR)
2424 copyout(args[i].kvalue, args[i].value, args[i].vallen);
2429 * Escape characters that are illegal or not recommended in XML.
2432 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2434 char *end = str + size;
2439 for (; *str && str < end; str++) {
2442 retval = sbuf_printf(sb, "&");
2445 retval = sbuf_printf(sb, ">");
2448 retval = sbuf_printf(sb, "<");
2451 retval = sbuf_putc(sb, *str);
2464 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2466 struct scsi_vpd_id_descriptor *desc;
2469 if (id == NULL || id->len < 4)
2471 desc = (struct scsi_vpd_id_descriptor *)id->data;
2472 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2473 case SVPD_ID_TYPE_T10:
2474 sbuf_printf(sb, "t10.");
2476 case SVPD_ID_TYPE_EUI64:
2477 sbuf_printf(sb, "eui.");
2479 case SVPD_ID_TYPE_NAA:
2480 sbuf_printf(sb, "naa.");
2482 case SVPD_ID_TYPE_SCSI_NAME:
2485 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2486 case SVPD_ID_CODESET_BINARY:
2487 for (i = 0; i < desc->length; i++)
2488 sbuf_printf(sb, "%02x", desc->identifier[i]);
2490 case SVPD_ID_CODESET_ASCII:
2491 sbuf_printf(sb, "%.*s", (int)desc->length,
2492 (char *)desc->identifier);
2494 case SVPD_ID_CODESET_UTF8:
2495 sbuf_printf(sb, "%s", (char *)desc->identifier);
2501 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2504 struct ctl_softc *softc = dev->si_drv1;
2505 struct ctl_lun *lun;
2512 retval = ctl_ioctl_io(dev, cmd, addr, flag, td);
2514 case CTL_ENABLE_PORT:
2515 case CTL_DISABLE_PORT:
2516 case CTL_SET_PORT_WWNS: {
2517 struct ctl_port *port;
2518 struct ctl_port_entry *entry;
2520 entry = (struct ctl_port_entry *)addr;
2522 mtx_lock(&softc->ctl_lock);
2523 STAILQ_FOREACH(port, &softc->port_list, links) {
2526 if (port->targ_port < softc->port_min ||
2527 port->targ_port >= softc->port_max)
2532 if ((entry->port_type == CTL_PORT_NONE)
2533 && (entry->targ_port == port->targ_port)) {
2535 * If the user only wants to enable or
2536 * disable or set WWNs on a specific port,
2537 * do the operation and we're done.
2541 } else if (entry->port_type & port->port_type) {
2543 * Compare the user's type mask with the
2544 * particular frontend type to see if we
2551 * Make sure the user isn't trying to set
2552 * WWNs on multiple ports at the same time.
2554 if (cmd == CTL_SET_PORT_WWNS) {
2555 printf("%s: Can't set WWNs on "
2556 "multiple ports\n", __func__);
2565 * XXX KDM we have to drop the lock here, because
2566 * the online/offline operations can potentially
2567 * block. We need to reference count the frontends
2568 * so they can't go away,
2570 if (cmd == CTL_ENABLE_PORT) {
2571 mtx_unlock(&softc->ctl_lock);
2572 ctl_port_online(port);
2573 mtx_lock(&softc->ctl_lock);
2574 } else if (cmd == CTL_DISABLE_PORT) {
2575 mtx_unlock(&softc->ctl_lock);
2576 ctl_port_offline(port);
2577 mtx_lock(&softc->ctl_lock);
2578 } else if (cmd == CTL_SET_PORT_WWNS) {
2579 ctl_port_set_wwns(port,
2580 (entry->flags & CTL_PORT_WWNN_VALID) ?
2582 (entry->flags & CTL_PORT_WWPN_VALID) ?
2583 1 : 0, entry->wwpn);
2588 mtx_unlock(&softc->ctl_lock);
2591 case CTL_GET_PORT_LIST: {
2592 struct ctl_port *port;
2593 struct ctl_port_list *list;
2596 list = (struct ctl_port_list *)addr;
2598 if (list->alloc_len != (list->alloc_num *
2599 sizeof(struct ctl_port_entry))) {
2600 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != "
2601 "alloc_num %u * sizeof(struct ctl_port_entry) "
2602 "%zu\n", __func__, list->alloc_len,
2603 list->alloc_num, sizeof(struct ctl_port_entry));
2609 list->dropped_num = 0;
2611 mtx_lock(&softc->ctl_lock);
2612 STAILQ_FOREACH(port, &softc->port_list, links) {
2613 struct ctl_port_entry entry, *list_entry;
2615 if (list->fill_num >= list->alloc_num) {
2616 list->dropped_num++;
2620 entry.port_type = port->port_type;
2621 strlcpy(entry.port_name, port->port_name,
2622 sizeof(entry.port_name));
2623 entry.targ_port = port->targ_port;
2624 entry.physical_port = port->physical_port;
2625 entry.virtual_port = port->virtual_port;
2626 entry.wwnn = port->wwnn;
2627 entry.wwpn = port->wwpn;
2628 if (port->status & CTL_PORT_STATUS_ONLINE)
2633 list_entry = &list->entries[i];
2635 retval = copyout(&entry, list_entry, sizeof(entry));
2637 printf("%s: CTL_GET_PORT_LIST: copyout "
2638 "returned %d\n", __func__, retval);
2643 list->fill_len += sizeof(entry);
2645 mtx_unlock(&softc->ctl_lock);
2648 * If this is non-zero, we had a copyout fault, so there's
2649 * probably no point in attempting to set the status inside
2655 if (list->dropped_num > 0)
2656 list->status = CTL_PORT_LIST_NEED_MORE_SPACE;
2658 list->status = CTL_PORT_LIST_OK;
2661 case CTL_DUMP_OOA: {
2666 mtx_lock(&softc->ctl_lock);
2667 printf("Dumping OOA queues:\n");
2668 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2669 mtx_lock(&lun->lun_lock);
2670 for (io = (union ctl_io *)TAILQ_FIRST(
2671 &lun->ooa_queue); io != NULL;
2672 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2674 sbuf_new(&sb, printbuf, sizeof(printbuf),
2676 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ",
2680 CTL_FLAG_BLOCKED) ? "" : " BLOCKED",
2682 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
2684 CTL_FLAG_ABORT) ? " ABORT" : "",
2686 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : "");
2687 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
2689 printf("%s\n", sbuf_data(&sb));
2691 mtx_unlock(&lun->lun_lock);
2693 printf("OOA queues dump done\n");
2694 mtx_unlock(&softc->ctl_lock);
2698 struct ctl_ooa *ooa_hdr;
2699 struct ctl_ooa_entry *entries;
2700 uint32_t cur_fill_num;
2702 ooa_hdr = (struct ctl_ooa *)addr;
2704 if ((ooa_hdr->alloc_len == 0)
2705 || (ooa_hdr->alloc_num == 0)) {
2706 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2707 "must be non-zero\n", __func__,
2708 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2713 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2714 sizeof(struct ctl_ooa_entry))) {
2715 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2716 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2717 __func__, ooa_hdr->alloc_len,
2718 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2723 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2724 if (entries == NULL) {
2725 printf("%s: could not allocate %d bytes for OOA "
2726 "dump\n", __func__, ooa_hdr->alloc_len);
2731 mtx_lock(&softc->ctl_lock);
2732 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2733 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS)
2734 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2735 mtx_unlock(&softc->ctl_lock);
2736 free(entries, M_CTL);
2737 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2738 __func__, (uintmax_t)ooa_hdr->lun_num);
2745 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2746 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2747 ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2751 lun = softc->ctl_luns[ooa_hdr->lun_num];
2752 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr,
2755 mtx_unlock(&softc->ctl_lock);
2757 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2758 ooa_hdr->fill_len = ooa_hdr->fill_num *
2759 sizeof(struct ctl_ooa_entry);
2760 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2762 printf("%s: error copying out %d bytes for OOA dump\n",
2763 __func__, ooa_hdr->fill_len);
2766 getbinuptime(&ooa_hdr->cur_bt);
2768 if (cur_fill_num > ooa_hdr->alloc_num) {
2769 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2770 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2772 ooa_hdr->dropped_num = 0;
2773 ooa_hdr->status = CTL_OOA_OK;
2776 free(entries, M_CTL);
2779 case CTL_CHECK_OOA: {
2781 struct ctl_ooa_info *ooa_info;
2784 ooa_info = (struct ctl_ooa_info *)addr;
2786 if (ooa_info->lun_id >= CTL_MAX_LUNS) {
2787 ooa_info->status = CTL_OOA_INVALID_LUN;
2790 mtx_lock(&softc->ctl_lock);
2791 lun = softc->ctl_luns[ooa_info->lun_id];
2793 mtx_unlock(&softc->ctl_lock);
2794 ooa_info->status = CTL_OOA_INVALID_LUN;
2797 mtx_lock(&lun->lun_lock);
2798 mtx_unlock(&softc->ctl_lock);
2799 ooa_info->num_entries = 0;
2800 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
2801 io != NULL; io = (union ctl_io *)TAILQ_NEXT(
2802 &io->io_hdr, ooa_links)) {
2803 ooa_info->num_entries++;
2805 mtx_unlock(&lun->lun_lock);
2807 ooa_info->status = CTL_OOA_SUCCESS;
2811 case CTL_DELAY_IO: {
2812 struct ctl_io_delay_info *delay_info;
2814 delay_info = (struct ctl_io_delay_info *)addr;
2817 mtx_lock(&softc->ctl_lock);
2819 if ((delay_info->lun_id >= CTL_MAX_LUNS)
2820 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2821 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2823 lun = softc->ctl_luns[delay_info->lun_id];
2824 mtx_lock(&lun->lun_lock);
2826 delay_info->status = CTL_DELAY_STATUS_OK;
2828 switch (delay_info->delay_type) {
2829 case CTL_DELAY_TYPE_CONT:
2831 case CTL_DELAY_TYPE_ONESHOT:
2834 delay_info->status =
2835 CTL_DELAY_STATUS_INVALID_TYPE;
2839 switch (delay_info->delay_loc) {
2840 case CTL_DELAY_LOC_DATAMOVE:
2841 lun->delay_info.datamove_type =
2842 delay_info->delay_type;
2843 lun->delay_info.datamove_delay =
2844 delay_info->delay_secs;
2846 case CTL_DELAY_LOC_DONE:
2847 lun->delay_info.done_type =
2848 delay_info->delay_type;
2849 lun->delay_info.done_delay =
2850 delay_info->delay_secs;
2853 delay_info->status =
2854 CTL_DELAY_STATUS_INVALID_LOC;
2857 mtx_unlock(&lun->lun_lock);
2860 mtx_unlock(&softc->ctl_lock);
2862 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2863 #endif /* CTL_IO_DELAY */
2866 case CTL_REALSYNC_SET: {
2869 syncstate = (int *)addr;
2871 mtx_lock(&softc->ctl_lock);
2872 switch (*syncstate) {
2874 softc->flags &= ~CTL_FLAG_REAL_SYNC;
2877 softc->flags |= CTL_FLAG_REAL_SYNC;
2883 mtx_unlock(&softc->ctl_lock);
2886 case CTL_REALSYNC_GET: {
2889 syncstate = (int*)addr;
2891 mtx_lock(&softc->ctl_lock);
2892 if (softc->flags & CTL_FLAG_REAL_SYNC)
2896 mtx_unlock(&softc->ctl_lock);
2902 struct ctl_sync_info *sync_info;
2904 sync_info = (struct ctl_sync_info *)addr;
2906 mtx_lock(&softc->ctl_lock);
2907 lun = softc->ctl_luns[sync_info->lun_id];
2909 mtx_unlock(&softc->ctl_lock);
2910 sync_info->status = CTL_GS_SYNC_NO_LUN;
2914 * Get or set the sync interval. We're not bounds checking
2915 * in the set case, hopefully the user won't do something
2918 mtx_lock(&lun->lun_lock);
2919 mtx_unlock(&softc->ctl_lock);
2920 if (cmd == CTL_GETSYNC)
2921 sync_info->sync_interval = lun->sync_interval;
2923 lun->sync_interval = sync_info->sync_interval;
2924 mtx_unlock(&lun->lun_lock);
2926 sync_info->status = CTL_GS_SYNC_OK;
2930 case CTL_GETSTATS: {
2931 struct ctl_stats *stats;
2934 stats = (struct ctl_stats *)addr;
2936 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2938 stats->status = CTL_SS_NEED_MORE_SPACE;
2939 stats->num_luns = softc->num_luns;
2943 * XXX KDM no locking here. If the LUN list changes,
2944 * things can blow up.
2946 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
2947 i++, lun = STAILQ_NEXT(lun, links)) {
2948 retval = copyout(&lun->stats, &stats->lun_stats[i],
2949 sizeof(lun->stats));
2953 stats->num_luns = softc->num_luns;
2954 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2956 stats->status = CTL_SS_OK;
2958 stats->flags = CTL_STATS_FLAG_TIME_VALID;
2960 stats->flags = CTL_STATS_FLAG_NONE;
2962 getnanouptime(&stats->timestamp);
2965 case CTL_ERROR_INJECT: {
2966 struct ctl_error_desc *err_desc, *new_err_desc;
2968 err_desc = (struct ctl_error_desc *)addr;
2970 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2972 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2974 mtx_lock(&softc->ctl_lock);
2975 lun = softc->ctl_luns[err_desc->lun_id];
2977 mtx_unlock(&softc->ctl_lock);
2978 free(new_err_desc, M_CTL);
2979 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2980 __func__, (uintmax_t)err_desc->lun_id);
2984 mtx_lock(&lun->lun_lock);
2985 mtx_unlock(&softc->ctl_lock);
2988 * We could do some checking here to verify the validity
2989 * of the request, but given the complexity of error
2990 * injection requests, the checking logic would be fairly
2993 * For now, if the request is invalid, it just won't get
2994 * executed and might get deleted.
2996 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2999 * XXX KDM check to make sure the serial number is unique,
3000 * in case we somehow manage to wrap. That shouldn't
3001 * happen for a very long time, but it's the right thing to
3004 new_err_desc->serial = lun->error_serial;
3005 err_desc->serial = lun->error_serial;
3006 lun->error_serial++;
3008 mtx_unlock(&lun->lun_lock);
3011 case CTL_ERROR_INJECT_DELETE: {
3012 struct ctl_error_desc *delete_desc, *desc, *desc2;
3015 delete_desc = (struct ctl_error_desc *)addr;
3018 mtx_lock(&softc->ctl_lock);
3019 lun = softc->ctl_luns[delete_desc->lun_id];
3021 mtx_unlock(&softc->ctl_lock);
3022 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
3023 __func__, (uintmax_t)delete_desc->lun_id);
3027 mtx_lock(&lun->lun_lock);
3028 mtx_unlock(&softc->ctl_lock);
3029 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
3030 if (desc->serial != delete_desc->serial)
3033 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
3038 mtx_unlock(&lun->lun_lock);
3039 if (delete_done == 0) {
3040 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
3041 "error serial %ju on LUN %u\n", __func__,
3042 delete_desc->serial, delete_desc->lun_id);
3048 case CTL_DUMP_STRUCTS: {
3050 struct ctl_port *port;
3051 struct ctl_frontend *fe;
3053 mtx_lock(&softc->ctl_lock);
3054 printf("CTL Persistent Reservation information start:\n");
3055 for (i = 0; i < CTL_MAX_LUNS; i++) {
3056 lun = softc->ctl_luns[i];
3059 || ((lun->flags & CTL_LUN_DISABLED) != 0))
3062 for (j = 0; j < CTL_MAX_PORTS; j++) {
3063 if (lun->pr_keys[j] == NULL)
3065 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
3066 if (lun->pr_keys[j][k] == 0)
3068 printf(" LUN %d port %d iid %d key "
3070 (uintmax_t)lun->pr_keys[j][k]);
3074 printf("CTL Persistent Reservation information end\n");
3075 printf("CTL Ports:\n");
3076 STAILQ_FOREACH(port, &softc->port_list, links) {
3077 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
3078 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
3079 port->frontend->name, port->port_type,
3080 port->physical_port, port->virtual_port,
3081 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
3082 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3083 if (port->wwpn_iid[j].in_use == 0 &&
3084 port->wwpn_iid[j].wwpn == 0 &&
3085 port->wwpn_iid[j].name == NULL)
3088 printf(" iid %u use %d WWPN %#jx '%s'\n",
3089 j, port->wwpn_iid[j].in_use,
3090 (uintmax_t)port->wwpn_iid[j].wwpn,
3091 port->wwpn_iid[j].name);
3094 printf("CTL Port information end\n");
3095 mtx_unlock(&softc->ctl_lock);
3097 * XXX KDM calling this without a lock. We'd likely want
3098 * to drop the lock before calling the frontend's dump
3101 printf("CTL Frontends:\n");
3102 STAILQ_FOREACH(fe, &softc->fe_list, links) {
3103 printf(" Frontend '%s'\n", fe->name);
3104 if (fe->fe_dump != NULL)
3107 printf("CTL Frontend information end\n");
3111 struct ctl_lun_req *lun_req;
3112 struct ctl_backend_driver *backend;
3114 lun_req = (struct ctl_lun_req *)addr;
3116 backend = ctl_backend_find(lun_req->backend);
3117 if (backend == NULL) {
3118 lun_req->status = CTL_LUN_ERROR;
3119 snprintf(lun_req->error_str,
3120 sizeof(lun_req->error_str),
3121 "Backend \"%s\" not found.",
3125 if (lun_req->num_be_args > 0) {
3126 lun_req->kern_be_args = ctl_copyin_args(
3127 lun_req->num_be_args,
3130 sizeof(lun_req->error_str));
3131 if (lun_req->kern_be_args == NULL) {
3132 lun_req->status = CTL_LUN_ERROR;
3137 retval = backend->ioctl(dev, cmd, addr, flag, td);
3139 if (lun_req->num_be_args > 0) {
3140 ctl_copyout_args(lun_req->num_be_args,
3141 lun_req->kern_be_args);
3142 ctl_free_args(lun_req->num_be_args,
3143 lun_req->kern_be_args);
3147 case CTL_LUN_LIST: {
3149 struct ctl_lun_list *list;
3150 struct ctl_option *opt;
3152 list = (struct ctl_lun_list *)addr;
3155 * Allocate a fixed length sbuf here, based on the length
3156 * of the user's buffer. We could allocate an auto-extending
3157 * buffer, and then tell the user how much larger our
3158 * amount of data is than his buffer, but that presents
3161 * 1. The sbuf(9) routines use a blocking malloc, and so
3162 * we can't hold a lock while calling them with an
3163 * auto-extending buffer.
3165 * 2. There is not currently a LUN reference counting
3166 * mechanism, outside of outstanding transactions on
3167 * the LUN's OOA queue. So a LUN could go away on us
3168 * while we're getting the LUN number, backend-specific
3169 * information, etc. Thus, given the way things
3170 * currently work, we need to hold the CTL lock while
3171 * grabbing LUN information.
3173 * So, from the user's standpoint, the best thing to do is
3174 * allocate what he thinks is a reasonable buffer length,
3175 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3176 * double the buffer length and try again. (And repeat
3177 * that until he succeeds.)
3179 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3181 list->status = CTL_LUN_LIST_ERROR;
3182 snprintf(list->error_str, sizeof(list->error_str),
3183 "Unable to allocate %d bytes for LUN list",
3188 sbuf_printf(sb, "<ctllunlist>\n");
3190 mtx_lock(&softc->ctl_lock);
3191 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3192 mtx_lock(&lun->lun_lock);
3193 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3194 (uintmax_t)lun->lun);
3197 * Bail out as soon as we see that we've overfilled
3203 retval = sbuf_printf(sb, "\t<backend_type>%s"
3204 "</backend_type>\n",
3205 (lun->backend == NULL) ? "none" :
3206 lun->backend->name);
3211 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3212 lun->be_lun->lun_type);
3217 if (lun->backend == NULL) {
3218 retval = sbuf_printf(sb, "</lun>\n");
3224 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3225 (lun->be_lun->maxlba > 0) ?
3226 lun->be_lun->maxlba + 1 : 0);
3231 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3232 lun->be_lun->blocksize);
3237 retval = sbuf_printf(sb, "\t<serial_number>");
3242 retval = ctl_sbuf_printf_esc(sb,
3243 lun->be_lun->serial_num,
3244 sizeof(lun->be_lun->serial_num));
3249 retval = sbuf_printf(sb, "</serial_number>\n");
3254 retval = sbuf_printf(sb, "\t<device_id>");
3259 retval = ctl_sbuf_printf_esc(sb,
3260 lun->be_lun->device_id,
3261 sizeof(lun->be_lun->device_id));
3266 retval = sbuf_printf(sb, "</device_id>\n");
3271 if (lun->backend->lun_info != NULL) {
3272 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3276 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3277 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3278 opt->name, opt->value, opt->name);
3283 retval = sbuf_printf(sb, "</lun>\n");
3287 mtx_unlock(&lun->lun_lock);
3290 mtx_unlock(&lun->lun_lock);
3291 mtx_unlock(&softc->ctl_lock);
3294 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3297 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3298 snprintf(list->error_str, sizeof(list->error_str),
3299 "Out of space, %d bytes is too small",
3306 retval = copyout(sbuf_data(sb), list->lun_xml,
3309 list->fill_len = sbuf_len(sb) + 1;
3310 list->status = CTL_LUN_LIST_OK;
3315 struct ctl_iscsi *ci;
3316 struct ctl_frontend *fe;
3318 ci = (struct ctl_iscsi *)addr;
3320 fe = ctl_frontend_find("iscsi");
3322 ci->status = CTL_ISCSI_ERROR;
3323 snprintf(ci->error_str, sizeof(ci->error_str),
3324 "Frontend \"iscsi\" not found.");
3328 retval = fe->ioctl(dev, cmd, addr, flag, td);
3331 case CTL_PORT_REQ: {
3332 struct ctl_req *req;
3333 struct ctl_frontend *fe;
3335 req = (struct ctl_req *)addr;
3337 fe = ctl_frontend_find(req->driver);
3339 req->status = CTL_LUN_ERROR;
3340 snprintf(req->error_str, sizeof(req->error_str),
3341 "Frontend \"%s\" not found.", req->driver);
3344 if (req->num_args > 0) {
3345 req->kern_args = ctl_copyin_args(req->num_args,
3346 req->args, req->error_str, sizeof(req->error_str));
3347 if (req->kern_args == NULL) {
3348 req->status = CTL_LUN_ERROR;
3354 retval = fe->ioctl(dev, cmd, addr, flag, td);
3358 if (req->num_args > 0) {
3359 ctl_copyout_args(req->num_args, req->kern_args);
3360 ctl_free_args(req->num_args, req->kern_args);
3364 case CTL_PORT_LIST: {
3366 struct ctl_port *port;
3367 struct ctl_lun_list *list;
3368 struct ctl_option *opt;
3372 list = (struct ctl_lun_list *)addr;
3374 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3376 list->status = CTL_LUN_LIST_ERROR;
3377 snprintf(list->error_str, sizeof(list->error_str),
3378 "Unable to allocate %d bytes for LUN list",
3383 sbuf_printf(sb, "<ctlportlist>\n");
3385 mtx_lock(&softc->ctl_lock);
3386 STAILQ_FOREACH(port, &softc->port_list, links) {
3387 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3388 (uintmax_t)port->targ_port);
3391 * Bail out as soon as we see that we've overfilled
3397 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3398 "</frontend_type>\n", port->frontend->name);
3402 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3407 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3408 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3412 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3417 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3418 port->physical_port);
3422 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3423 port->virtual_port);
3427 if (port->target_devid != NULL) {
3428 sbuf_printf(sb, "\t<target>");
3429 ctl_id_sbuf(port->target_devid, sb);
3430 sbuf_printf(sb, "</target>\n");
3433 if (port->port_devid != NULL) {
3434 sbuf_printf(sb, "\t<port>");
3435 ctl_id_sbuf(port->port_devid, sb);
3436 sbuf_printf(sb, "</port>\n");
3439 if (port->port_info != NULL) {
3440 retval = port->port_info(port->onoff_arg, sb);
3444 STAILQ_FOREACH(opt, &port->options, links) {
3445 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3446 opt->name, opt->value, opt->name);
3451 if (port->lun_map != NULL) {
3452 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n");
3453 for (j = 0; j < CTL_MAX_LUNS; j++) {
3454 plun = ctl_lun_map_from_port(port, j);
3455 if (plun >= CTL_MAX_LUNS)
3458 "\t<lun id=\"%u\">%u</lun>\n",
3463 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3464 if (port->wwpn_iid[j].in_use == 0 ||
3465 (port->wwpn_iid[j].wwpn == 0 &&
3466 port->wwpn_iid[j].name == NULL))
3469 if (port->wwpn_iid[j].name != NULL)
3470 retval = sbuf_printf(sb,
3471 "\t<initiator id=\"%u\">%s</initiator>\n",
3472 j, port->wwpn_iid[j].name);
3474 retval = sbuf_printf(sb,
3475 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3476 j, port->wwpn_iid[j].wwpn);
3483 retval = sbuf_printf(sb, "</targ_port>\n");
3487 mtx_unlock(&softc->ctl_lock);
3490 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3493 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3494 snprintf(list->error_str, sizeof(list->error_str),
3495 "Out of space, %d bytes is too small",
3502 retval = copyout(sbuf_data(sb), list->lun_xml,
3505 list->fill_len = sbuf_len(sb) + 1;
3506 list->status = CTL_LUN_LIST_OK;
3511 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr;
3512 struct ctl_port *port;
3514 mtx_lock(&softc->ctl_lock);
3515 if (lm->port < softc->port_min ||
3516 lm->port >= softc->port_max ||
3517 (port = softc->ctl_ports[lm->port]) == NULL) {
3518 mtx_unlock(&softc->ctl_lock);
3521 if (port->status & CTL_PORT_STATUS_ONLINE) {
3522 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3523 if (ctl_lun_map_to_port(port, lun->lun) >=
3526 mtx_lock(&lun->lun_lock);
3527 ctl_est_ua_port(lun, lm->port, -1,
3529 mtx_unlock(&lun->lun_lock);
3532 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps
3533 if (lm->plun < CTL_MAX_LUNS) {
3534 if (lm->lun == UINT32_MAX)
3535 retval = ctl_lun_map_unset(port, lm->plun);
3536 else if (lm->lun < CTL_MAX_LUNS &&
3537 softc->ctl_luns[lm->lun] != NULL)
3538 retval = ctl_lun_map_set(port, lm->plun, lm->lun);
3541 } else if (lm->plun == UINT32_MAX) {
3542 if (lm->lun == UINT32_MAX)
3543 retval = ctl_lun_map_deinit(port);
3545 retval = ctl_lun_map_init(port);
3548 if (port->status & CTL_PORT_STATUS_ONLINE)
3549 ctl_isc_announce_port(port);
3553 /* XXX KDM should we fix this? */
3555 struct ctl_backend_driver *backend;
3562 * We encode the backend type as the ioctl type for backend
3563 * ioctls. So parse it out here, and then search for a
3564 * backend of this type.
3566 type = _IOC_TYPE(cmd);
3568 STAILQ_FOREACH(backend, &softc->be_list, links) {
3569 if (backend->type == type) {
3575 printf("ctl: unknown ioctl command %#lx or backend "
3580 retval = backend->ioctl(dev, cmd, addr, flag, td);
3590 ctl_get_initindex(struct ctl_nexus *nexus)
3592 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3596 ctl_lun_map_init(struct ctl_port *port)
3598 struct ctl_softc *softc = port->ctl_softc;
3599 struct ctl_lun *lun;
3602 if (port->lun_map == NULL)
3603 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS,
3605 if (port->lun_map == NULL)
3607 for (i = 0; i < CTL_MAX_LUNS; i++)
3608 port->lun_map[i] = UINT32_MAX;
3609 if (port->status & CTL_PORT_STATUS_ONLINE) {
3610 if (port->lun_disable != NULL) {
3611 STAILQ_FOREACH(lun, &softc->lun_list, links)
3612 port->lun_disable(port->targ_lun_arg, lun->lun);
3614 ctl_isc_announce_port(port);
3620 ctl_lun_map_deinit(struct ctl_port *port)
3622 struct ctl_softc *softc = port->ctl_softc;
3623 struct ctl_lun *lun;
3625 if (port->lun_map == NULL)
3627 free(port->lun_map, M_CTL);
3628 port->lun_map = NULL;
3629 if (port->status & CTL_PORT_STATUS_ONLINE) {
3630 if (port->lun_enable != NULL) {
3631 STAILQ_FOREACH(lun, &softc->lun_list, links)
3632 port->lun_enable(port->targ_lun_arg, lun->lun);
3634 ctl_isc_announce_port(port);
3640 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun)
3645 if (port->lun_map == NULL) {
3646 status = ctl_lun_map_init(port);
3650 old = port->lun_map[plun];
3651 port->lun_map[plun] = glun;
3652 if ((port->status & CTL_PORT_STATUS_ONLINE) && old >= CTL_MAX_LUNS) {
3653 if (port->lun_enable != NULL)
3654 port->lun_enable(port->targ_lun_arg, plun);
3655 ctl_isc_announce_port(port);
3661 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun)
3665 if (port->lun_map == NULL)
3667 old = port->lun_map[plun];
3668 port->lun_map[plun] = UINT32_MAX;
3669 if ((port->status & CTL_PORT_STATUS_ONLINE) && old < CTL_MAX_LUNS) {
3670 if (port->lun_disable != NULL)
3671 port->lun_disable(port->targ_lun_arg, plun);
3672 ctl_isc_announce_port(port);
3678 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
3682 return (UINT32_MAX);
3683 if (port->lun_map == NULL || lun_id >= CTL_MAX_LUNS)
3685 return (port->lun_map[lun_id]);
3689 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
3694 return (UINT32_MAX);
3695 if (port->lun_map == NULL)
3697 for (i = 0; i < CTL_MAX_LUNS; i++) {
3698 if (port->lun_map[i] == lun_id)
3701 return (UINT32_MAX);
3704 static struct ctl_port *
3705 ctl_io_port(struct ctl_io_hdr *io_hdr)
3708 return (control_softc->ctl_ports[io_hdr->nexus.targ_port]);
3712 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last)
3716 for (i = first; i < last; i++) {
3717 if ((mask[i / 32] & (1 << (i % 32))) == 0)
3724 ctl_set_mask(uint32_t *mask, uint32_t bit)
3726 uint32_t chunk, piece;
3729 piece = bit % (sizeof(uint32_t) * 8);
3731 if ((mask[chunk] & (1 << piece)) != 0)
3734 mask[chunk] |= (1 << piece);
3740 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3742 uint32_t chunk, piece;
3745 piece = bit % (sizeof(uint32_t) * 8);
3747 if ((mask[chunk] & (1 << piece)) == 0)
3750 mask[chunk] &= ~(1 << piece);
3756 ctl_is_set(uint32_t *mask, uint32_t bit)
3758 uint32_t chunk, piece;
3761 piece = bit % (sizeof(uint32_t) * 8);
3763 if ((mask[chunk] & (1 << piece)) == 0)
3770 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3774 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3777 return (t[residx % CTL_MAX_INIT_PER_PORT]);
3781 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3785 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3788 t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3792 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3797 i = residx/CTL_MAX_INIT_PER_PORT;
3798 if (lun->pr_keys[i] != NULL)
3800 mtx_unlock(&lun->lun_lock);
3801 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3803 mtx_lock(&lun->lun_lock);
3804 if (lun->pr_keys[i] == NULL)
3805 lun->pr_keys[i] = p;
3811 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3815 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3816 KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3817 t[residx % CTL_MAX_INIT_PER_PORT] = key;
3821 * ctl_softc, pool_name, total_ctl_io are passed in.
3822 * npool is passed out.
3825 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3826 uint32_t total_ctl_io, void **npool)
3829 struct ctl_io_pool *pool;
3831 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3836 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3837 pool->ctl_softc = ctl_softc;
3838 pool->zone = uma_zsecond_create(pool->name, NULL,
3839 NULL, NULL, NULL, ctl_softc->io_zone);
3840 /* uma_prealloc(pool->zone, total_ctl_io); */
3844 *npool = ctl_softc->io_zone;
3850 ctl_pool_free(struct ctl_io_pool *pool)
3857 uma_zdestroy(pool->zone);
3863 ctl_alloc_io(void *pool_ref)
3867 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3869 io = uma_zalloc(pool->zone, M_WAITOK);
3871 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK);
3874 io->io_hdr.pool = pool_ref;
3879 ctl_alloc_io_nowait(void *pool_ref)
3883 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3885 io = uma_zalloc(pool->zone, M_NOWAIT);
3887 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT);
3890 io->io_hdr.pool = pool_ref;
3895 ctl_free_io(union ctl_io *io)
3898 struct ctl_io_pool *pool;
3905 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3906 uma_zfree(pool->zone, io);
3908 uma_zfree((uma_zone_t)io->io_hdr.pool, io);
3913 ctl_zero_io(union ctl_io *io)
3921 * May need to preserve linked list pointers at some point too.
3923 pool_ref = io->io_hdr.pool;
3924 memset(io, 0, sizeof(*io));
3925 io->io_hdr.pool = pool_ref;
3929 * This routine is currently used for internal copies of ctl_ios that need
3930 * to persist for some reason after we've already returned status to the
3931 * FETD. (Thus the flag set.)
3934 * Note that this makes a blind copy of all fields in the ctl_io, except
3935 * for the pool reference. This includes any memory that has been
3936 * allocated! That memory will no longer be valid after done has been
3937 * called, so this would be VERY DANGEROUS for command that actually does
3938 * any reads or writes. Right now (11/7/2005), this is only used for immediate
3939 * start and stop commands, which don't transfer any data, so this is not a
3940 * problem. If it is used for anything else, the caller would also need to
3941 * allocate data buffer space and this routine would need to be modified to
3942 * copy the data buffer(s) as well.
3945 ctl_copy_io(union ctl_io *src, union ctl_io *dest)
3954 * May need to preserve linked list pointers at some point too.
3956 pool_ref = dest->io_hdr.pool;
3958 memcpy(dest, src, MIN(sizeof(*src), sizeof(*dest)));
3960 dest->io_hdr.pool = pool_ref;
3962 * We need to know that this is an internal copy, and doesn't need
3963 * to get passed back to the FETD that allocated it.
3965 dest->io_hdr.flags |= CTL_FLAG_INT_COPY;
3969 ctl_expand_number(const char *buf, uint64_t *num)
3975 number = strtoq(buf, &endptr, 0);
3977 switch (tolower((unsigned char)*endptr)) {
3997 case '\0': /* No unit. */
4001 /* Unrecognized unit. */
4005 if ((number << shift) >> shift != number) {
4009 *num = number << shift;
4015 * This routine could be used in the future to load default and/or saved
4016 * mode page parameters for a particuar lun.
4019 ctl_init_page_index(struct ctl_lun *lun)
4022 struct ctl_page_index *page_index;
4026 memcpy(&lun->mode_pages.index, page_index_template,
4027 sizeof(page_index_template));
4029 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4031 page_index = &lun->mode_pages.index[i];
4033 * If this is a disk-only mode page, there's no point in
4034 * setting it up. For some pages, we have to have some
4035 * basic information about the disk in order to calculate the
4038 if ((lun->be_lun->lun_type != T_DIRECT)
4039 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
4042 switch (page_index->page_code & SMPH_PC_MASK) {
4043 case SMS_RW_ERROR_RECOVERY_PAGE: {
4044 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4045 panic("subpage is incorrect!");
4046 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
4047 &rw_er_page_default,
4048 sizeof(rw_er_page_default));
4049 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
4050 &rw_er_page_changeable,
4051 sizeof(rw_er_page_changeable));
4052 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
4053 &rw_er_page_default,
4054 sizeof(rw_er_page_default));
4055 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
4056 &rw_er_page_default,
4057 sizeof(rw_er_page_default));
4058 page_index->page_data =
4059 (uint8_t *)lun->mode_pages.rw_er_page;
4062 case SMS_FORMAT_DEVICE_PAGE: {
4063 struct scsi_format_page *format_page;
4065 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4066 panic("subpage is incorrect!");
4069 * Sectors per track are set above. Bytes per
4070 * sector need to be set here on a per-LUN basis.
4072 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
4073 &format_page_default,
4074 sizeof(format_page_default));
4075 memcpy(&lun->mode_pages.format_page[
4076 CTL_PAGE_CHANGEABLE], &format_page_changeable,
4077 sizeof(format_page_changeable));
4078 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
4079 &format_page_default,
4080 sizeof(format_page_default));
4081 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
4082 &format_page_default,
4083 sizeof(format_page_default));
4085 format_page = &lun->mode_pages.format_page[
4087 scsi_ulto2b(lun->be_lun->blocksize,
4088 format_page->bytes_per_sector);
4090 format_page = &lun->mode_pages.format_page[
4092 scsi_ulto2b(lun->be_lun->blocksize,
4093 format_page->bytes_per_sector);
4095 format_page = &lun->mode_pages.format_page[
4097 scsi_ulto2b(lun->be_lun->blocksize,
4098 format_page->bytes_per_sector);
4100 page_index->page_data =
4101 (uint8_t *)lun->mode_pages.format_page;
4104 case SMS_RIGID_DISK_PAGE: {
4105 struct scsi_rigid_disk_page *rigid_disk_page;
4106 uint32_t sectors_per_cylinder;
4110 #endif /* !__XSCALE__ */
4112 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4113 panic("invalid subpage value %d",
4114 page_index->subpage);
4117 * Rotation rate and sectors per track are set
4118 * above. We calculate the cylinders here based on
4119 * capacity. Due to the number of heads and
4120 * sectors per track we're using, smaller arrays
4121 * may turn out to have 0 cylinders. Linux and
4122 * FreeBSD don't pay attention to these mode pages
4123 * to figure out capacity, but Solaris does. It
4124 * seems to deal with 0 cylinders just fine, and
4125 * works out a fake geometry based on the capacity.
4127 memcpy(&lun->mode_pages.rigid_disk_page[
4128 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4129 sizeof(rigid_disk_page_default));
4130 memcpy(&lun->mode_pages.rigid_disk_page[
4131 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4132 sizeof(rigid_disk_page_changeable));
4134 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4138 * The divide method here will be more accurate,
4139 * probably, but results in floating point being
4140 * used in the kernel on i386 (__udivdi3()). On the
4141 * XScale, though, __udivdi3() is implemented in
4144 * The shift method for cylinder calculation is
4145 * accurate if sectors_per_cylinder is a power of
4146 * 2. Otherwise it might be slightly off -- you
4147 * might have a bit of a truncation problem.
4150 cylinders = (lun->be_lun->maxlba + 1) /
4151 sectors_per_cylinder;
4153 for (shift = 31; shift > 0; shift--) {
4154 if (sectors_per_cylinder & (1 << shift))
4157 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4161 * We've basically got 3 bytes, or 24 bits for the
4162 * cylinder size in the mode page. If we're over,
4163 * just round down to 2^24.
4165 if (cylinders > 0xffffff)
4166 cylinders = 0xffffff;
4168 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4170 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4172 if ((value = ctl_get_opt(&lun->be_lun->options,
4174 scsi_ulto2b(strtol(value, NULL, 0),
4175 rigid_disk_page->rotation_rate);
4178 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
4179 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4180 sizeof(rigid_disk_page_default));
4181 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
4182 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4183 sizeof(rigid_disk_page_default));
4185 page_index->page_data =
4186 (uint8_t *)lun->mode_pages.rigid_disk_page;
4189 case SMS_CACHING_PAGE: {
4190 struct scsi_caching_page *caching_page;
4192 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4193 panic("invalid subpage value %d",
4194 page_index->subpage);
4195 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4196 &caching_page_default,
4197 sizeof(caching_page_default));
4198 memcpy(&lun->mode_pages.caching_page[
4199 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4200 sizeof(caching_page_changeable));
4201 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4202 &caching_page_default,
4203 sizeof(caching_page_default));
4204 caching_page = &lun->mode_pages.caching_page[
4206 value = ctl_get_opt(&lun->be_lun->options, "writecache");
4207 if (value != NULL && strcmp(value, "off") == 0)
4208 caching_page->flags1 &= ~SCP_WCE;
4209 value = ctl_get_opt(&lun->be_lun->options, "readcache");
4210 if (value != NULL && strcmp(value, "off") == 0)
4211 caching_page->flags1 |= SCP_RCD;
4212 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4213 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4214 sizeof(caching_page_default));
4215 page_index->page_data =
4216 (uint8_t *)lun->mode_pages.caching_page;
4219 case SMS_CONTROL_MODE_PAGE: {
4220 switch (page_index->subpage) {
4221 case SMS_SUBPAGE_PAGE_0: {
4222 struct scsi_control_page *control_page;
4224 memcpy(&lun->mode_pages.control_page[
4226 &control_page_default,
4227 sizeof(control_page_default));
4228 memcpy(&lun->mode_pages.control_page[
4229 CTL_PAGE_CHANGEABLE],
4230 &control_page_changeable,
4231 sizeof(control_page_changeable));
4232 memcpy(&lun->mode_pages.control_page[
4234 &control_page_default,
4235 sizeof(control_page_default));
4236 control_page = &lun->mode_pages.control_page[
4238 value = ctl_get_opt(&lun->be_lun->options,
4240 if (value != NULL &&
4241 strcmp(value, "unrestricted") == 0) {
4242 control_page->queue_flags &=
4243 ~SCP_QUEUE_ALG_MASK;
4244 control_page->queue_flags |=
4245 SCP_QUEUE_ALG_UNRESTRICTED;
4247 memcpy(&lun->mode_pages.control_page[
4249 &lun->mode_pages.control_page[
4251 sizeof(control_page_default));
4252 page_index->page_data =
4253 (uint8_t *)lun->mode_pages.control_page;
4257 memcpy(&lun->mode_pages.control_ext_page[
4259 &control_ext_page_default,
4260 sizeof(control_ext_page_default));
4261 memcpy(&lun->mode_pages.control_ext_page[
4262 CTL_PAGE_CHANGEABLE],
4263 &control_ext_page_changeable,
4264 sizeof(control_ext_page_changeable));
4265 memcpy(&lun->mode_pages.control_ext_page[
4267 &control_ext_page_default,
4268 sizeof(control_ext_page_default));
4269 memcpy(&lun->mode_pages.control_ext_page[
4271 &lun->mode_pages.control_ext_page[
4273 sizeof(control_ext_page_default));
4274 page_index->page_data =
4275 (uint8_t *)lun->mode_pages.control_ext_page;
4280 case SMS_INFO_EXCEPTIONS_PAGE: {
4281 switch (page_index->subpage) {
4282 case SMS_SUBPAGE_PAGE_0:
4283 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4285 sizeof(ie_page_default));
4286 memcpy(&lun->mode_pages.ie_page[
4287 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4288 sizeof(ie_page_changeable));
4289 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4291 sizeof(ie_page_default));
4292 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4294 sizeof(ie_page_default));
4295 page_index->page_data =
4296 (uint8_t *)lun->mode_pages.ie_page;
4299 struct ctl_logical_block_provisioning_page *page;
4301 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4303 sizeof(lbp_page_default));
4304 memcpy(&lun->mode_pages.lbp_page[
4305 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4306 sizeof(lbp_page_changeable));
4307 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4309 sizeof(lbp_page_default));
4310 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4311 value = ctl_get_opt(&lun->be_lun->options,
4313 if (value != NULL &&
4314 ctl_expand_number(value, &ival) == 0) {
4315 page->descr[0].flags |= SLBPPD_ENABLED |
4317 if (lun->be_lun->blocksize)
4318 ival /= lun->be_lun->blocksize;
4321 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4322 page->descr[0].count);
4324 value = ctl_get_opt(&lun->be_lun->options,
4326 if (value != NULL &&
4327 ctl_expand_number(value, &ival) == 0) {
4328 page->descr[1].flags |= SLBPPD_ENABLED |
4330 if (lun->be_lun->blocksize)
4331 ival /= lun->be_lun->blocksize;
4334 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4335 page->descr[1].count);
4337 value = ctl_get_opt(&lun->be_lun->options,
4338 "pool-avail-threshold");
4339 if (value != NULL &&
4340 ctl_expand_number(value, &ival) == 0) {
4341 page->descr[2].flags |= SLBPPD_ENABLED |
4343 if (lun->be_lun->blocksize)
4344 ival /= lun->be_lun->blocksize;
4347 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4348 page->descr[2].count);
4350 value = ctl_get_opt(&lun->be_lun->options,
4351 "pool-used-threshold");
4352 if (value != NULL &&
4353 ctl_expand_number(value, &ival) == 0) {
4354 page->descr[3].flags |= SLBPPD_ENABLED |
4356 if (lun->be_lun->blocksize)
4357 ival /= lun->be_lun->blocksize;
4360 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4361 page->descr[3].count);
4363 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4364 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4365 sizeof(lbp_page_default));
4366 page_index->page_data =
4367 (uint8_t *)lun->mode_pages.lbp_page;
4371 case SMS_VENDOR_SPECIFIC_PAGE:{
4372 switch (page_index->subpage) {
4373 case DBGCNF_SUBPAGE_CODE: {
4374 memcpy(&lun->mode_pages.debugconf_subpage[
4376 &debugconf_page_default,
4377 sizeof(debugconf_page_default));
4378 memcpy(&lun->mode_pages.debugconf_subpage[
4379 CTL_PAGE_CHANGEABLE],
4380 &debugconf_page_changeable,
4381 sizeof(debugconf_page_changeable));
4382 memcpy(&lun->mode_pages.debugconf_subpage[
4384 &debugconf_page_default,
4385 sizeof(debugconf_page_default));
4386 memcpy(&lun->mode_pages.debugconf_subpage[
4388 &debugconf_page_default,
4389 sizeof(debugconf_page_default));
4390 page_index->page_data =
4391 (uint8_t *)lun->mode_pages.debugconf_subpage;
4395 panic("invalid subpage value %d",
4396 page_index->subpage);
4402 panic("invalid page value %d",
4403 page_index->page_code & SMPH_PC_MASK);
4408 return (CTL_RETVAL_COMPLETE);
4412 ctl_init_log_page_index(struct ctl_lun *lun)
4414 struct ctl_page_index *page_index;
4417 memcpy(&lun->log_pages.index, log_page_index_template,
4418 sizeof(log_page_index_template));
4421 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4423 page_index = &lun->log_pages.index[i];
4425 * If this is a disk-only mode page, there's no point in
4426 * setting it up. For some pages, we have to have some
4427 * basic information about the disk in order to calculate the
4430 if ((lun->be_lun->lun_type != T_DIRECT)
4431 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
4434 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4435 lun->backend->lun_attr == NULL)
4438 if (page_index->page_code != prev) {
4439 lun->log_pages.pages_page[j] = page_index->page_code;
4440 prev = page_index->page_code;
4443 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4444 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4447 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4448 lun->log_pages.index[0].page_len = j;
4449 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4450 lun->log_pages.index[1].page_len = k * 2;
4451 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4452 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4453 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page;
4454 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page);
4456 return (CTL_RETVAL_COMPLETE);
4460 hex2bin(const char *str, uint8_t *buf, int buf_size)
4465 memset(buf, 0, buf_size);
4466 while (isspace(str[0]))
4468 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4471 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4475 else if (isalpha(c))
4476 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4482 buf[i / 2] |= (c << 4);
4486 return ((i + 1) / 2);
4493 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4494 * wants us to allocate the LUN and he can block.
4495 * - ctl_softc is always set
4496 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4498 * Returns 0 for success, non-zero (errno) for failure.
4501 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4502 struct ctl_be_lun *const be_lun)
4504 struct ctl_lun *nlun, *lun;
4505 struct scsi_vpd_id_descriptor *desc;
4506 struct scsi_vpd_id_t10 *t10id;
4507 const char *eui, *naa, *scsiname, *vendor;
4508 int lun_number, i, lun_malloced;
4509 int devidlen, idlen1, idlen2 = 0, len;
4515 * We currently only support Direct Access or Processor LUN types.
4517 switch (be_lun->lun_type) {
4525 be_lun->lun_config_status(be_lun->be_lun,
4526 CTL_LUN_CONFIG_FAILURE);
4529 if (ctl_lun == NULL) {
4530 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4537 memset(lun, 0, sizeof(*lun));
4539 lun->flags = CTL_LUN_MALLOCED;
4541 /* Generate LUN ID. */
4542 devidlen = max(CTL_DEVID_MIN_LEN,
4543 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4544 idlen1 = sizeof(*t10id) + devidlen;
4545 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4546 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4547 if (scsiname != NULL) {
4548 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4549 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4551 eui = ctl_get_opt(&be_lun->options, "eui");
4553 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4555 naa = ctl_get_opt(&be_lun->options, "naa");
4557 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4559 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4560 M_CTL, M_WAITOK | M_ZERO);
4561 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4562 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4563 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4564 desc->length = idlen1;
4565 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4566 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4567 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4568 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4570 strncpy(t10id->vendor, vendor,
4571 min(sizeof(t10id->vendor), strlen(vendor)));
4573 strncpy((char *)t10id->vendor_spec_id,
4574 (char *)be_lun->device_id, devidlen);
4575 if (scsiname != NULL) {
4576 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4578 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4579 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4580 SVPD_ID_TYPE_SCSI_NAME;
4581 desc->length = idlen2;
4582 strlcpy(desc->identifier, scsiname, idlen2);
4585 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4587 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4588 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4590 desc->length = hex2bin(eui, desc->identifier, 16);
4591 desc->length = desc->length > 12 ? 16 :
4592 (desc->length > 8 ? 12 : 8);
4593 len -= 16 - desc->length;
4596 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4598 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4599 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4601 desc->length = hex2bin(naa, desc->identifier, 16);
4602 desc->length = desc->length > 8 ? 16 : 8;
4603 len -= 16 - desc->length;
4605 lun->lun_devid->len = len;
4607 mtx_lock(&ctl_softc->ctl_lock);
4609 * See if the caller requested a particular LUN number. If so, see
4610 * if it is available. Otherwise, allocate the first available LUN.
4612 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4613 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4614 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4615 mtx_unlock(&ctl_softc->ctl_lock);
4616 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4617 printf("ctl: requested LUN ID %d is higher "
4618 "than CTL_MAX_LUNS - 1 (%d)\n",
4619 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4622 * XXX KDM return an error, or just assign
4623 * another LUN ID in this case??
4625 printf("ctl: requested LUN ID %d is already "
4626 "in use\n", be_lun->req_lun_id);
4628 if (lun->flags & CTL_LUN_MALLOCED)
4630 be_lun->lun_config_status(be_lun->be_lun,
4631 CTL_LUN_CONFIG_FAILURE);
4634 lun_number = be_lun->req_lun_id;
4636 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, CTL_MAX_LUNS);
4637 if (lun_number == -1) {
4638 mtx_unlock(&ctl_softc->ctl_lock);
4639 printf("ctl: can't allocate LUN, out of LUNs\n");
4640 if (lun->flags & CTL_LUN_MALLOCED)
4642 be_lun->lun_config_status(be_lun->be_lun,
4643 CTL_LUN_CONFIG_FAILURE);
4647 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4649 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4650 lun->lun = lun_number;
4651 lun->be_lun = be_lun;
4653 * The processor LUN is always enabled. Disk LUNs come on line
4654 * disabled, and must be enabled by the backend.
4656 lun->flags |= CTL_LUN_DISABLED;
4657 lun->backend = be_lun->be;
4658 be_lun->ctl_lun = lun;
4659 be_lun->lun_id = lun_number;
4660 atomic_add_int(&be_lun->be->num_luns, 1);
4661 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE)
4662 lun->flags |= CTL_LUN_OFFLINE;
4664 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4665 lun->flags |= CTL_LUN_STOPPED;
4667 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4668 lun->flags |= CTL_LUN_INOPERABLE;
4670 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4671 lun->flags |= CTL_LUN_PRIMARY_SC;
4673 lun->ctl_softc = ctl_softc;
4675 lun->last_busy = getsbinuptime();
4677 TAILQ_INIT(&lun->ooa_queue);
4678 TAILQ_INIT(&lun->blocked_queue);
4679 STAILQ_INIT(&lun->error_list);
4680 ctl_tpc_lun_init(lun);
4683 * Initialize the mode and log page index.
4685 ctl_init_page_index(lun);
4686 ctl_init_log_page_index(lun);
4689 * Now, before we insert this lun on the lun list, set the lun
4690 * inventory changed UA for all other luns.
4692 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4693 mtx_lock(&nlun->lun_lock);
4694 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4695 mtx_unlock(&nlun->lun_lock);
4698 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4700 ctl_softc->ctl_luns[lun_number] = lun;
4702 ctl_softc->num_luns++;
4704 /* Setup statistics gathering */
4705 lun->stats.device_type = be_lun->lun_type;
4706 lun->stats.lun_number = lun_number;
4707 if (lun->stats.device_type == T_DIRECT)
4708 lun->stats.blocksize = be_lun->blocksize;
4710 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4711 for (i = 0;i < CTL_MAX_PORTS;i++)
4712 lun->stats.ports[i].targ_port = i;
4714 mtx_unlock(&ctl_softc->ctl_lock);
4716 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4723 * - LUN has already been marked invalid and any pending I/O has been taken
4727 ctl_free_lun(struct ctl_lun *lun)
4729 struct ctl_softc *softc;
4730 struct ctl_lun *nlun;
4733 softc = lun->ctl_softc;
4735 mtx_assert(&softc->ctl_lock, MA_OWNED);
4737 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4739 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4741 softc->ctl_luns[lun->lun] = NULL;
4743 if (!TAILQ_EMPTY(&lun->ooa_queue))
4744 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4749 * Tell the backend to free resources, if this LUN has a backend.
4751 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4752 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4754 ctl_tpc_lun_shutdown(lun);
4755 mtx_destroy(&lun->lun_lock);
4756 free(lun->lun_devid, M_CTL);
4757 for (i = 0; i < CTL_MAX_PORTS; i++)
4758 free(lun->pending_ua[i], M_CTL);
4759 for (i = 0; i < CTL_MAX_PORTS; i++)
4760 free(lun->pr_keys[i], M_CTL);
4761 free(lun->write_buffer, M_CTL);
4762 if (lun->flags & CTL_LUN_MALLOCED)
4765 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4766 mtx_lock(&nlun->lun_lock);
4767 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4768 mtx_unlock(&nlun->lun_lock);
4775 ctl_create_lun(struct ctl_be_lun *be_lun)
4779 * ctl_alloc_lun() should handle all potential failure cases.
4781 ctl_alloc_lun(control_softc, NULL, be_lun);
4785 ctl_add_lun(struct ctl_be_lun *be_lun)
4787 struct ctl_softc *softc = control_softc;
4789 mtx_lock(&softc->ctl_lock);
4790 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links);
4791 mtx_unlock(&softc->ctl_lock);
4792 wakeup(&softc->pending_lun_queue);
4798 ctl_enable_lun(struct ctl_be_lun *be_lun)
4800 struct ctl_softc *softc;
4801 struct ctl_port *port, *nport;
4802 struct ctl_lun *lun;
4805 lun = (struct ctl_lun *)be_lun->ctl_lun;
4806 softc = lun->ctl_softc;
4808 mtx_lock(&softc->ctl_lock);
4809 mtx_lock(&lun->lun_lock);
4810 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4812 * eh? Why did we get called if the LUN is already
4815 mtx_unlock(&lun->lun_lock);
4816 mtx_unlock(&softc->ctl_lock);
4819 lun->flags &= ~CTL_LUN_DISABLED;
4820 mtx_unlock(&lun->lun_lock);
4822 for (port = STAILQ_FIRST(&softc->port_list); port != NULL; port = nport) {
4823 nport = STAILQ_NEXT(port, links);
4824 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4825 port->lun_map != NULL || port->lun_enable == NULL)
4829 * Drop the lock while we call the FETD's enable routine.
4830 * This can lead to a callback into CTL (at least in the
4831 * case of the internal initiator frontend.
4833 mtx_unlock(&softc->ctl_lock);
4834 retval = port->lun_enable(port->targ_lun_arg, lun->lun);
4835 mtx_lock(&softc->ctl_lock);
4837 printf("%s: FETD %s port %d returned error "
4838 "%d for lun_enable on lun %jd\n",
4839 __func__, port->port_name, port->targ_port,
4840 retval, (intmax_t)lun->lun);
4844 mtx_unlock(&softc->ctl_lock);
4845 ctl_isc_announce_lun(lun);
4851 ctl_disable_lun(struct ctl_be_lun *be_lun)
4853 struct ctl_softc *softc;
4854 struct ctl_port *port;
4855 struct ctl_lun *lun;
4858 lun = (struct ctl_lun *)be_lun->ctl_lun;
4859 softc = lun->ctl_softc;
4861 mtx_lock(&softc->ctl_lock);
4862 mtx_lock(&lun->lun_lock);
4863 if (lun->flags & CTL_LUN_DISABLED) {
4864 mtx_unlock(&lun->lun_lock);
4865 mtx_unlock(&softc->ctl_lock);
4868 lun->flags |= CTL_LUN_DISABLED;
4869 mtx_unlock(&lun->lun_lock);
4871 STAILQ_FOREACH(port, &softc->port_list, links) {
4872 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4873 port->lun_map != NULL || port->lun_disable == NULL)
4877 * Drop the lock before we call the frontend's disable
4878 * routine, to avoid lock order reversals.
4880 * XXX KDM what happens if the frontend list changes while
4881 * we're traversing it? It's unlikely, but should be handled.
4883 mtx_unlock(&softc->ctl_lock);
4884 retval = port->lun_disable(port->targ_lun_arg, lun->lun);
4885 mtx_lock(&softc->ctl_lock);
4887 printf("%s: FETD %s port %d returned error "
4888 "%d for lun_disable on lun %jd\n",
4889 __func__, port->port_name, port->targ_port,
4890 retval, (intmax_t)lun->lun);
4894 mtx_unlock(&softc->ctl_lock);
4895 ctl_isc_announce_lun(lun);
4901 ctl_start_lun(struct ctl_be_lun *be_lun)
4903 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4905 mtx_lock(&lun->lun_lock);
4906 lun->flags &= ~CTL_LUN_STOPPED;
4907 mtx_unlock(&lun->lun_lock);
4912 ctl_stop_lun(struct ctl_be_lun *be_lun)
4914 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4916 mtx_lock(&lun->lun_lock);
4917 lun->flags |= CTL_LUN_STOPPED;
4918 mtx_unlock(&lun->lun_lock);
4923 ctl_lun_offline(struct ctl_be_lun *be_lun)
4925 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4927 mtx_lock(&lun->lun_lock);
4928 lun->flags |= CTL_LUN_OFFLINE;
4929 mtx_unlock(&lun->lun_lock);
4934 ctl_lun_online(struct ctl_be_lun *be_lun)
4936 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4938 mtx_lock(&lun->lun_lock);
4939 lun->flags &= ~CTL_LUN_OFFLINE;
4940 mtx_unlock(&lun->lun_lock);
4945 ctl_lun_primary(struct ctl_be_lun *be_lun)
4947 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4949 mtx_lock(&lun->lun_lock);
4950 lun->flags |= CTL_LUN_PRIMARY_SC;
4951 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4952 mtx_unlock(&lun->lun_lock);
4953 ctl_isc_announce_lun(lun);
4958 ctl_lun_secondary(struct ctl_be_lun *be_lun)
4960 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4962 mtx_lock(&lun->lun_lock);
4963 lun->flags &= ~CTL_LUN_PRIMARY_SC;
4964 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4965 mtx_unlock(&lun->lun_lock);
4966 ctl_isc_announce_lun(lun);
4971 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4973 struct ctl_softc *softc;
4974 struct ctl_lun *lun;
4976 lun = (struct ctl_lun *)be_lun->ctl_lun;
4977 softc = lun->ctl_softc;
4979 mtx_lock(&lun->lun_lock);
4982 * The LUN needs to be disabled before it can be marked invalid.
4984 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4985 mtx_unlock(&lun->lun_lock);
4989 * Mark the LUN invalid.
4991 lun->flags |= CTL_LUN_INVALID;
4994 * If there is nothing in the OOA queue, go ahead and free the LUN.
4995 * If we have something in the OOA queue, we'll free it when the
4996 * last I/O completes.
4998 if (TAILQ_EMPTY(&lun->ooa_queue)) {
4999 mtx_unlock(&lun->lun_lock);
5000 mtx_lock(&softc->ctl_lock);
5002 mtx_unlock(&softc->ctl_lock);
5004 mtx_unlock(&lun->lun_lock);
5010 ctl_lun_inoperable(struct ctl_be_lun *be_lun)
5012 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5014 mtx_lock(&lun->lun_lock);
5015 lun->flags |= CTL_LUN_INOPERABLE;
5016 mtx_unlock(&lun->lun_lock);
5021 ctl_lun_operable(struct ctl_be_lun *be_lun)
5023 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5025 mtx_lock(&lun->lun_lock);
5026 lun->flags &= ~CTL_LUN_INOPERABLE;
5027 mtx_unlock(&lun->lun_lock);
5032 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
5034 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5035 union ctl_ha_msg msg;
5037 mtx_lock(&lun->lun_lock);
5038 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGED);
5039 mtx_unlock(&lun->lun_lock);
5040 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
5041 /* Send msg to other side. */
5042 bzero(&msg.ua, sizeof(msg.ua));
5043 msg.hdr.msg_type = CTL_MSG_UA;
5044 msg.hdr.nexus.initid = -1;
5045 msg.hdr.nexus.targ_port = -1;
5046 msg.hdr.nexus.targ_lun = lun->lun;
5047 msg.hdr.nexus.targ_mapped_lun = lun->lun;
5050 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGED;
5051 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
5057 * Backend "memory move is complete" callback for requests that never
5058 * make it down to say RAIDCore's configuration code.
5061 ctl_config_move_done(union ctl_io *io)
5065 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
5066 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
5067 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
5069 if ((io->io_hdr.port_status != 0) &&
5070 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5071 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5073 * For hardware error sense keys, the sense key
5074 * specific value is defined to be a retry count,
5075 * but we use it to pass back an internal FETD
5076 * error code. XXX KDM Hopefully the FETD is only
5077 * using 16 bits for an error code, since that's
5078 * all the space we have in the sks field.
5080 ctl_set_internal_failure(&io->scsiio,
5083 io->io_hdr.port_status);
5086 if (ctl_debug & CTL_DEBUG_CDB_DATA)
5088 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
5089 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5090 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
5091 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5093 * XXX KDM just assuming a single pointer here, and not a
5094 * S/G list. If we start using S/G lists for config data,
5095 * we'll need to know how to clean them up here as well.
5097 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5098 free(io->scsiio.kern_data_ptr, M_CTL);
5100 retval = CTL_RETVAL_COMPLETE;
5103 * XXX KDM now we need to continue data movement. Some
5105 * - call ctl_scsiio() again? We don't do this for data
5106 * writes, because for those at least we know ahead of
5107 * time where the write will go and how long it is. For
5108 * config writes, though, that information is largely
5109 * contained within the write itself, thus we need to
5110 * parse out the data again.
5112 * - Call some other function once the data is in?
5116 * XXX KDM call ctl_scsiio() again for now, and check flag
5117 * bits to see whether we're allocated or not.
5119 retval = ctl_scsiio(&io->scsiio);
5125 * This gets called by a backend driver when it is done with a
5126 * data_submit method.
5129 ctl_data_submit_done(union ctl_io *io)
5132 * If the IO_CONT flag is set, we need to call the supplied
5133 * function to continue processing the I/O, instead of completing
5136 * If there is an error, though, we don't want to keep processing.
5137 * Instead, just send status back to the initiator.
5139 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5140 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5141 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5142 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5143 io->scsiio.io_cont(io);
5150 * This gets called by a backend driver when it is done with a
5151 * configuration write.
5154 ctl_config_write_done(union ctl_io *io)
5159 * If the IO_CONT flag is set, we need to call the supplied
5160 * function to continue processing the I/O, instead of completing
5163 * If there is an error, though, we don't want to keep processing.
5164 * Instead, just send status back to the initiator.
5166 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5167 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5168 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5169 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5170 io->scsiio.io_cont(io);
5174 * Since a configuration write can be done for commands that actually
5175 * have data allocated, like write buffer, and commands that have
5176 * no data, like start/stop unit, we need to check here.
5178 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5179 buf = io->scsiio.kern_data_ptr;
5188 ctl_config_read_done(union ctl_io *io)
5193 * If there is some error -- we are done, skip data transfer.
5195 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
5196 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5197 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
5198 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5199 buf = io->scsiio.kern_data_ptr;
5209 * If the IO_CONT flag is set, we need to call the supplied
5210 * function to continue processing the I/O, instead of completing
5213 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
5214 io->scsiio.io_cont(io);
5222 * SCSI release command.
5225 ctl_scsi_release(struct ctl_scsiio *ctsio)
5227 struct ctl_lun *lun;
5230 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5232 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5233 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5236 * XXX KDM right now, we only support LUN reservation. We don't
5237 * support 3rd party reservations, or extent reservations, which
5238 * might actually need the parameter list. If we've gotten this
5239 * far, we've got a LUN reservation. Anything else got kicked out
5240 * above. So, according to SPC, ignore the length.
5243 mtx_lock(&lun->lun_lock);
5246 * According to SPC, it is not an error for an intiator to attempt
5247 * to release a reservation on a LUN that isn't reserved, or that
5248 * is reserved by another initiator. The reservation can only be
5249 * released, though, by the initiator who made it or by one of
5250 * several reset type events.
5252 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5253 lun->flags &= ~CTL_LUN_RESERVED;
5255 mtx_unlock(&lun->lun_lock);
5257 ctl_set_success(ctsio);
5258 ctl_done((union ctl_io *)ctsio);
5259 return (CTL_RETVAL_COMPLETE);
5263 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5265 struct ctl_lun *lun;
5268 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5270 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5271 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5274 * XXX KDM right now, we only support LUN reservation. We don't
5275 * support 3rd party reservations, or extent reservations, which
5276 * might actually need the parameter list. If we've gotten this
5277 * far, we've got a LUN reservation. Anything else got kicked out
5278 * above. So, according to SPC, ignore the length.
5281 mtx_lock(&lun->lun_lock);
5282 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5283 ctl_set_reservation_conflict(ctsio);
5286 lun->flags |= CTL_LUN_RESERVED;
5287 lun->res_idx = residx;
5288 ctl_set_success(ctsio);
5291 mtx_unlock(&lun->lun_lock);
5292 ctl_done((union ctl_io *)ctsio);
5293 return (CTL_RETVAL_COMPLETE);
5297 ctl_start_stop(struct ctl_scsiio *ctsio)
5299 struct scsi_start_stop_unit *cdb;
5300 struct ctl_lun *lun;
5303 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5305 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5308 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5312 * We don't support the immediate bit on a stop unit. In order to
5313 * do that, we would need to code up a way to know that a stop is
5314 * pending, and hold off any new commands until it completes, one
5315 * way or another. Then we could accept or reject those commands
5316 * depending on its status. We would almost need to do the reverse
5317 * of what we do below for an immediate start -- return the copy of
5318 * the ctl_io to the FETD with status to send to the host (and to
5319 * free the copy!) and then free the original I/O once the stop
5320 * actually completes. That way, the OOA queue mechanism can work
5321 * to block commands that shouldn't proceed. Another alternative
5322 * would be to put the copy in the queue in place of the original,
5323 * and return the original back to the caller. That could be
5326 if ((cdb->byte2 & SSS_IMMED)
5327 && ((cdb->how & SSS_START) == 0)) {
5328 ctl_set_invalid_field(ctsio,
5334 ctl_done((union ctl_io *)ctsio);
5335 return (CTL_RETVAL_COMPLETE);
5338 if ((lun->flags & CTL_LUN_PR_RESERVED)
5339 && ((cdb->how & SSS_START)==0)) {
5342 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5343 if (ctl_get_prkey(lun, residx) == 0
5344 || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5346 ctl_set_reservation_conflict(ctsio);
5347 ctl_done((union ctl_io *)ctsio);
5348 return (CTL_RETVAL_COMPLETE);
5353 * If there is no backend on this device, we can't start or stop
5354 * it. In theory we shouldn't get any start/stop commands in the
5355 * first place at this level if the LUN doesn't have a backend.
5356 * That should get stopped by the command decode code.
5358 if (lun->backend == NULL) {
5359 ctl_set_invalid_opcode(ctsio);
5360 ctl_done((union ctl_io *)ctsio);
5361 return (CTL_RETVAL_COMPLETE);
5365 * XXX KDM Copan-specific offline behavior.
5366 * Figure out a reasonable way to port this?
5369 mtx_lock(&lun->lun_lock);
5371 if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
5372 && (lun->flags & CTL_LUN_OFFLINE)) {
5374 * If the LUN is offline, and the on/offline bit isn't set,
5375 * reject the start or stop. Otherwise, let it through.
5377 mtx_unlock(&lun->lun_lock);
5378 ctl_set_lun_not_ready(ctsio);
5379 ctl_done((union ctl_io *)ctsio);
5381 mtx_unlock(&lun->lun_lock);
5382 #endif /* NEEDTOPORT */
5384 * This could be a start or a stop when we're online,
5385 * or a stop/offline or start/online. A start or stop when
5386 * we're offline is covered in the case above.
5389 * In the non-immediate case, we send the request to
5390 * the backend and return status to the user when
5393 * In the immediate case, we allocate a new ctl_io
5394 * to hold a copy of the request, and send that to
5395 * the backend. We then set good status on the
5396 * user's request and return it immediately.
5398 if (cdb->byte2 & SSS_IMMED) {
5399 union ctl_io *new_io;
5401 new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5402 ctl_copy_io((union ctl_io *)ctsio, new_io);
5403 retval = lun->backend->config_write(new_io);
5404 ctl_set_success(ctsio);
5405 ctl_done((union ctl_io *)ctsio);
5407 retval = lun->backend->config_write(
5408 (union ctl_io *)ctsio);
5417 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5418 * we don't really do anything with the LBA and length fields if the user
5419 * passes them in. Instead we'll just flush out the cache for the entire
5423 ctl_sync_cache(struct ctl_scsiio *ctsio)
5425 struct ctl_lun *lun;
5426 struct ctl_softc *softc;
5427 struct ctl_lba_len_flags *lbalen;
5428 uint64_t starting_lba;
5429 uint32_t block_count;
5433 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5435 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5436 softc = lun->ctl_softc;
5439 switch (ctsio->cdb[0]) {
5440 case SYNCHRONIZE_CACHE: {
5441 struct scsi_sync_cache *cdb;
5442 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5444 starting_lba = scsi_4btoul(cdb->begin_lba);
5445 block_count = scsi_2btoul(cdb->lb_count);
5449 case SYNCHRONIZE_CACHE_16: {
5450 struct scsi_sync_cache_16 *cdb;
5451 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5453 starting_lba = scsi_8btou64(cdb->begin_lba);
5454 block_count = scsi_4btoul(cdb->lb_count);
5459 ctl_set_invalid_opcode(ctsio);
5460 ctl_done((union ctl_io *)ctsio);
5462 break; /* NOTREACHED */
5466 * We check the LBA and length, but don't do anything with them.
5467 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5468 * get flushed. This check will just help satisfy anyone who wants
5469 * to see an error for an out of range LBA.
5471 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5472 ctl_set_lba_out_of_range(ctsio);
5473 ctl_done((union ctl_io *)ctsio);
5478 * If this LUN has no backend, we can't flush the cache anyway.
5480 if (lun->backend == NULL) {
5481 ctl_set_invalid_opcode(ctsio);
5482 ctl_done((union ctl_io *)ctsio);
5486 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5487 lbalen->lba = starting_lba;
5488 lbalen->len = block_count;
5489 lbalen->flags = byte2;
5492 * Check to see whether we're configured to send the SYNCHRONIZE
5493 * CACHE command directly to the back end.
5495 mtx_lock(&lun->lun_lock);
5496 if ((softc->flags & CTL_FLAG_REAL_SYNC)
5497 && (++(lun->sync_count) >= lun->sync_interval)) {
5498 lun->sync_count = 0;
5499 mtx_unlock(&lun->lun_lock);
5500 retval = lun->backend->config_write((union ctl_io *)ctsio);
5502 mtx_unlock(&lun->lun_lock);
5503 ctl_set_success(ctsio);
5504 ctl_done((union ctl_io *)ctsio);
5513 ctl_format(struct ctl_scsiio *ctsio)
5515 struct scsi_format *cdb;
5516 struct ctl_lun *lun;
5517 int length, defect_list_len;
5519 CTL_DEBUG_PRINT(("ctl_format\n"));
5521 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5523 cdb = (struct scsi_format *)ctsio->cdb;
5526 if (cdb->byte2 & SF_FMTDATA) {
5527 if (cdb->byte2 & SF_LONGLIST)
5528 length = sizeof(struct scsi_format_header_long);
5530 length = sizeof(struct scsi_format_header_short);
5533 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5535 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5536 ctsio->kern_data_len = length;
5537 ctsio->kern_total_len = length;
5538 ctsio->kern_data_resid = 0;
5539 ctsio->kern_rel_offset = 0;
5540 ctsio->kern_sg_entries = 0;
5541 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5542 ctsio->be_move_done = ctl_config_move_done;
5543 ctl_datamove((union ctl_io *)ctsio);
5545 return (CTL_RETVAL_COMPLETE);
5548 defect_list_len = 0;
5550 if (cdb->byte2 & SF_FMTDATA) {
5551 if (cdb->byte2 & SF_LONGLIST) {
5552 struct scsi_format_header_long *header;
5554 header = (struct scsi_format_header_long *)
5555 ctsio->kern_data_ptr;
5557 defect_list_len = scsi_4btoul(header->defect_list_len);
5558 if (defect_list_len != 0) {
5559 ctl_set_invalid_field(ctsio,
5568 struct scsi_format_header_short *header;
5570 header = (struct scsi_format_header_short *)
5571 ctsio->kern_data_ptr;
5573 defect_list_len = scsi_2btoul(header->defect_list_len);
5574 if (defect_list_len != 0) {
5575 ctl_set_invalid_field(ctsio,
5587 * The format command will clear out the "Medium format corrupted"
5588 * status if set by the configuration code. That status is really
5589 * just a way to notify the host that we have lost the media, and
5590 * get them to issue a command that will basically make them think
5591 * they're blowing away the media.
5593 mtx_lock(&lun->lun_lock);
5594 lun->flags &= ~CTL_LUN_INOPERABLE;
5595 mtx_unlock(&lun->lun_lock);
5597 ctl_set_success(ctsio);
5600 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5601 free(ctsio->kern_data_ptr, M_CTL);
5602 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5605 ctl_done((union ctl_io *)ctsio);
5606 return (CTL_RETVAL_COMPLETE);
5610 ctl_read_buffer(struct ctl_scsiio *ctsio)
5612 struct ctl_lun *lun;
5613 uint64_t buffer_offset;
5616 static uint8_t descr[4];
5617 static uint8_t echo_descr[4] = { 0 };
5619 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5620 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5621 switch (ctsio->cdb[0]) {
5623 struct scsi_read_buffer *cdb;
5625 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5626 buffer_offset = scsi_3btoul(cdb->offset);
5627 len = scsi_3btoul(cdb->length);
5631 case READ_BUFFER_16: {
5632 struct scsi_read_buffer_16 *cdb;
5634 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb;
5635 buffer_offset = scsi_8btou64(cdb->offset);
5636 len = scsi_4btoul(cdb->length);
5640 default: /* This shouldn't happen. */
5641 ctl_set_invalid_opcode(ctsio);
5642 ctl_done((union ctl_io *)ctsio);
5643 return (CTL_RETVAL_COMPLETE);
5646 if ((byte2 & RWB_MODE) != RWB_MODE_DATA &&
5647 (byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR &&
5648 (byte2 & RWB_MODE) != RWB_MODE_DESCR) {
5649 ctl_set_invalid_field(ctsio,
5655 ctl_done((union ctl_io *)ctsio);
5656 return (CTL_RETVAL_COMPLETE);
5659 if (buffer_offset > CTL_WRITE_BUFFER_SIZE ||
5660 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5661 ctl_set_invalid_field(ctsio,
5667 ctl_done((union ctl_io *)ctsio);
5668 return (CTL_RETVAL_COMPLETE);
5671 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5673 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5674 ctsio->kern_data_ptr = descr;
5675 len = min(len, sizeof(descr));
5676 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5677 ctsio->kern_data_ptr = echo_descr;
5678 len = min(len, sizeof(echo_descr));
5680 if (lun->write_buffer == NULL) {
5681 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5684 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5686 ctsio->kern_data_len = len;
5687 ctsio->kern_total_len = len;
5688 ctsio->kern_data_resid = 0;
5689 ctsio->kern_rel_offset = 0;
5690 ctsio->kern_sg_entries = 0;
5691 ctl_set_success(ctsio);
5692 ctsio->be_move_done = ctl_config_move_done;
5693 ctl_datamove((union ctl_io *)ctsio);
5694 return (CTL_RETVAL_COMPLETE);
5698 ctl_write_buffer(struct ctl_scsiio *ctsio)
5700 struct scsi_write_buffer *cdb;
5701 struct ctl_lun *lun;
5702 int buffer_offset, len;
5704 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5706 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5707 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5709 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5710 ctl_set_invalid_field(ctsio,
5716 ctl_done((union ctl_io *)ctsio);
5717 return (CTL_RETVAL_COMPLETE);
5720 len = scsi_3btoul(cdb->length);
5721 buffer_offset = scsi_3btoul(cdb->offset);
5723 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5724 ctl_set_invalid_field(ctsio,
5730 ctl_done((union ctl_io *)ctsio);
5731 return (CTL_RETVAL_COMPLETE);
5735 * If we've got a kernel request that hasn't been malloced yet,
5736 * malloc it and tell the caller the data buffer is here.
5738 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5739 if (lun->write_buffer == NULL) {
5740 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5743 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5744 ctsio->kern_data_len = len;
5745 ctsio->kern_total_len = len;
5746 ctsio->kern_data_resid = 0;
5747 ctsio->kern_rel_offset = 0;
5748 ctsio->kern_sg_entries = 0;
5749 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5750 ctsio->be_move_done = ctl_config_move_done;
5751 ctl_datamove((union ctl_io *)ctsio);
5753 return (CTL_RETVAL_COMPLETE);
5756 ctl_set_success(ctsio);
5757 ctl_done((union ctl_io *)ctsio);
5758 return (CTL_RETVAL_COMPLETE);
5762 ctl_write_same(struct ctl_scsiio *ctsio)
5764 struct ctl_lun *lun;
5765 struct ctl_lba_len_flags *lbalen;
5767 uint32_t num_blocks;
5771 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5773 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5775 switch (ctsio->cdb[0]) {
5776 case WRITE_SAME_10: {
5777 struct scsi_write_same_10 *cdb;
5779 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5781 lba = scsi_4btoul(cdb->addr);
5782 num_blocks = scsi_2btoul(cdb->length);
5786 case WRITE_SAME_16: {
5787 struct scsi_write_same_16 *cdb;
5789 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5791 lba = scsi_8btou64(cdb->addr);
5792 num_blocks = scsi_4btoul(cdb->length);
5798 * We got a command we don't support. This shouldn't
5799 * happen, commands should be filtered out above us.
5801 ctl_set_invalid_opcode(ctsio);
5802 ctl_done((union ctl_io *)ctsio);
5804 return (CTL_RETVAL_COMPLETE);
5805 break; /* NOTREACHED */
5808 /* ANCHOR flag can be used only together with UNMAP */
5809 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) {
5810 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5811 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5812 ctl_done((union ctl_io *)ctsio);
5813 return (CTL_RETVAL_COMPLETE);
5817 * The first check is to make sure we're in bounds, the second
5818 * check is to catch wrap-around problems. If the lba + num blocks
5819 * is less than the lba, then we've wrapped around and the block
5820 * range is invalid anyway.
5822 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5823 || ((lba + num_blocks) < lba)) {
5824 ctl_set_lba_out_of_range(ctsio);
5825 ctl_done((union ctl_io *)ctsio);
5826 return (CTL_RETVAL_COMPLETE);
5829 /* Zero number of blocks means "to the last logical block" */
5830 if (num_blocks == 0) {
5831 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5832 ctl_set_invalid_field(ctsio,
5838 ctl_done((union ctl_io *)ctsio);
5839 return (CTL_RETVAL_COMPLETE);
5841 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5844 len = lun->be_lun->blocksize;
5847 * If we've got a kernel request that hasn't been malloced yet,
5848 * malloc it and tell the caller the data buffer is here.
5850 if ((byte2 & SWS_NDOB) == 0 &&
5851 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5852 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5853 ctsio->kern_data_len = len;
5854 ctsio->kern_total_len = len;
5855 ctsio->kern_data_resid = 0;
5856 ctsio->kern_rel_offset = 0;
5857 ctsio->kern_sg_entries = 0;
5858 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5859 ctsio->be_move_done = ctl_config_move_done;
5860 ctl_datamove((union ctl_io *)ctsio);
5862 return (CTL_RETVAL_COMPLETE);
5865 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5867 lbalen->len = num_blocks;
5868 lbalen->flags = byte2;
5869 retval = lun->backend->config_write((union ctl_io *)ctsio);
5875 ctl_unmap(struct ctl_scsiio *ctsio)
5877 struct ctl_lun *lun;
5878 struct scsi_unmap *cdb;
5879 struct ctl_ptr_len_flags *ptrlen;
5880 struct scsi_unmap_header *hdr;
5881 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5883 uint32_t num_blocks;
5887 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5889 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5890 cdb = (struct scsi_unmap *)ctsio->cdb;
5892 len = scsi_2btoul(cdb->length);
5896 * If we've got a kernel request that hasn't been malloced yet,
5897 * malloc it and tell the caller the data buffer is here.
5899 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5900 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5901 ctsio->kern_data_len = len;
5902 ctsio->kern_total_len = len;
5903 ctsio->kern_data_resid = 0;
5904 ctsio->kern_rel_offset = 0;
5905 ctsio->kern_sg_entries = 0;
5906 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5907 ctsio->be_move_done = ctl_config_move_done;
5908 ctl_datamove((union ctl_io *)ctsio);
5910 return (CTL_RETVAL_COMPLETE);
5913 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5914 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5915 if (len < sizeof (*hdr) ||
5916 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5917 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5918 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5919 ctl_set_invalid_field(ctsio,
5927 len = scsi_2btoul(hdr->desc_length);
5928 buf = (struct scsi_unmap_desc *)(hdr + 1);
5929 end = buf + len / sizeof(*buf);
5932 for (range = buf; range < end; range++) {
5933 lba = scsi_8btou64(range->lba);
5934 num_blocks = scsi_4btoul(range->length);
5935 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5936 || ((lba + num_blocks) < lba)) {
5937 ctl_set_lba_out_of_range(ctsio);
5938 ctl_done((union ctl_io *)ctsio);
5939 return (CTL_RETVAL_COMPLETE);
5941 if (num_blocks != 0)
5946 * Block backend can not handle zero last range.
5947 * Filter it out and return if there is nothing left.
5949 len = (uint8_t *)endnz - (uint8_t *)buf;
5951 ctl_set_success(ctsio);
5955 mtx_lock(&lun->lun_lock);
5956 ptrlen = (struct ctl_ptr_len_flags *)
5957 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5958 ptrlen->ptr = (void *)buf;
5960 ptrlen->flags = byte2;
5961 ctl_check_blocked(lun);
5962 mtx_unlock(&lun->lun_lock);
5964 retval = lun->backend->config_write((union ctl_io *)ctsio);
5968 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5969 free(ctsio->kern_data_ptr, M_CTL);
5970 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5972 ctl_done((union ctl_io *)ctsio);
5973 return (CTL_RETVAL_COMPLETE);
5977 * Note that this function currently doesn't actually do anything inside
5978 * CTL to enforce things if the DQue bit is turned on.
5980 * Also note that this function can't be used in the default case, because
5981 * the DQue bit isn't set in the changeable mask for the control mode page
5982 * anyway. This is just here as an example for how to implement a page
5983 * handler, and a placeholder in case we want to allow the user to turn
5984 * tagged queueing on and off.
5986 * The D_SENSE bit handling is functional, however, and will turn
5987 * descriptor sense on and off for a given LUN.
5990 ctl_control_page_handler(struct ctl_scsiio *ctsio,
5991 struct ctl_page_index *page_index, uint8_t *page_ptr)
5993 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
5994 struct ctl_lun *lun;
5998 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5999 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6002 user_cp = (struct scsi_control_page *)page_ptr;
6003 current_cp = (struct scsi_control_page *)
6004 (page_index->page_data + (page_index->page_len *
6006 saved_cp = (struct scsi_control_page *)
6007 (page_index->page_data + (page_index->page_len *
6010 mtx_lock(&lun->lun_lock);
6011 if (((current_cp->rlec & SCP_DSENSE) == 0)
6012 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
6014 * Descriptor sense is currently turned off and the user
6015 * wants to turn it on.
6017 current_cp->rlec |= SCP_DSENSE;
6018 saved_cp->rlec |= SCP_DSENSE;
6019 lun->flags |= CTL_LUN_SENSE_DESC;
6021 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
6022 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
6024 * Descriptor sense is currently turned on, and the user
6025 * wants to turn it off.
6027 current_cp->rlec &= ~SCP_DSENSE;
6028 saved_cp->rlec &= ~SCP_DSENSE;
6029 lun->flags &= ~CTL_LUN_SENSE_DESC;
6032 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) !=
6033 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) {
6034 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
6035 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
6036 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
6037 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
6040 if ((current_cp->eca_and_aen & SCP_SWP) !=
6041 (user_cp->eca_and_aen & SCP_SWP)) {
6042 current_cp->eca_and_aen &= ~SCP_SWP;
6043 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
6044 saved_cp->eca_and_aen &= ~SCP_SWP;
6045 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
6049 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
6050 mtx_unlock(&lun->lun_lock);
6052 ctl_isc_announce_mode(lun,
6053 ctl_get_initindex(&ctsio->io_hdr.nexus),
6054 page_index->page_code, page_index->subpage);
6060 ctl_caching_sp_handler(struct ctl_scsiio *ctsio,
6061 struct ctl_page_index *page_index, uint8_t *page_ptr)
6063 struct scsi_caching_page *current_cp, *saved_cp, *user_cp;
6064 struct ctl_lun *lun;
6068 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6069 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6072 user_cp = (struct scsi_caching_page *)page_ptr;
6073 current_cp = (struct scsi_caching_page *)
6074 (page_index->page_data + (page_index->page_len *
6076 saved_cp = (struct scsi_caching_page *)
6077 (page_index->page_data + (page_index->page_len *
6080 mtx_lock(&lun->lun_lock);
6081 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) !=
6082 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) {
6083 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
6084 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
6085 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
6086 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
6090 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
6091 mtx_unlock(&lun->lun_lock);
6093 ctl_isc_announce_mode(lun,
6094 ctl_get_initindex(&ctsio->io_hdr.nexus),
6095 page_index->page_code, page_index->subpage);
6101 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
6102 struct ctl_page_index *page_index,
6108 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
6113 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
6114 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
6115 printf("page data:");
6117 printf(" %.2x",page_ptr[i]);
6123 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
6124 struct ctl_page_index *page_index,
6127 struct copan_debugconf_subpage *page;
6129 page = (struct copan_debugconf_subpage *)page_index->page_data +
6130 (page_index->page_len * pc);
6133 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6134 case SMS_PAGE_CTRL_DEFAULT >> 6:
6135 case SMS_PAGE_CTRL_SAVED >> 6:
6137 * We don't update the changable or default bits for this page.
6140 case SMS_PAGE_CTRL_CURRENT >> 6:
6141 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
6142 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
6146 EPRINT(0, "Invalid PC %d!!", pc);
6147 #endif /* NEEDTOPORT */
6155 ctl_do_mode_select(union ctl_io *io)
6157 struct scsi_mode_page_header *page_header;
6158 struct ctl_page_index *page_index;
6159 struct ctl_scsiio *ctsio;
6160 int control_dev, page_len;
6161 int page_len_offset, page_len_size;
6162 union ctl_modepage_info *modepage_info;
6163 struct ctl_lun *lun;
6164 int *len_left, *len_used;
6167 ctsio = &io->scsiio;
6170 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6172 if (lun->be_lun->lun_type != T_DIRECT)
6177 modepage_info = (union ctl_modepage_info *)
6178 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6179 len_left = &modepage_info->header.len_left;
6180 len_used = &modepage_info->header.len_used;
6184 page_header = (struct scsi_mode_page_header *)
6185 (ctsio->kern_data_ptr + *len_used);
6187 if (*len_left == 0) {
6188 free(ctsio->kern_data_ptr, M_CTL);
6189 ctl_set_success(ctsio);
6190 ctl_done((union ctl_io *)ctsio);
6191 return (CTL_RETVAL_COMPLETE);
6192 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6194 free(ctsio->kern_data_ptr, M_CTL);
6195 ctl_set_param_len_error(ctsio);
6196 ctl_done((union ctl_io *)ctsio);
6197 return (CTL_RETVAL_COMPLETE);
6199 } else if ((page_header->page_code & SMPH_SPF)
6200 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6202 free(ctsio->kern_data_ptr, M_CTL);
6203 ctl_set_param_len_error(ctsio);
6204 ctl_done((union ctl_io *)ctsio);
6205 return (CTL_RETVAL_COMPLETE);
6210 * XXX KDM should we do something with the block descriptor?
6212 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6214 if ((control_dev != 0)
6215 && (lun->mode_pages.index[i].page_flags &
6216 CTL_PAGE_FLAG_DISK_ONLY))
6219 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
6220 (page_header->page_code & SMPH_PC_MASK))
6224 * If neither page has a subpage code, then we've got a
6227 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
6228 && ((page_header->page_code & SMPH_SPF) == 0)) {
6229 page_index = &lun->mode_pages.index[i];
6230 page_len = page_header->page_length;
6235 * If both pages have subpages, then the subpage numbers
6238 if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
6239 && (page_header->page_code & SMPH_SPF)) {
6240 struct scsi_mode_page_header_sp *sph;
6242 sph = (struct scsi_mode_page_header_sp *)page_header;
6244 if (lun->mode_pages.index[i].subpage ==
6246 page_index = &lun->mode_pages.index[i];
6247 page_len = scsi_2btoul(sph->page_length);
6254 * If we couldn't find the page, or if we don't have a mode select
6255 * handler for it, send back an error to the user.
6257 if ((page_index == NULL)
6258 || (page_index->select_handler == NULL)) {
6259 ctl_set_invalid_field(ctsio,
6262 /*field*/ *len_used,
6265 free(ctsio->kern_data_ptr, M_CTL);
6266 ctl_done((union ctl_io *)ctsio);
6267 return (CTL_RETVAL_COMPLETE);
6270 if (page_index->page_code & SMPH_SPF) {
6271 page_len_offset = 2;
6275 page_len_offset = 1;
6279 * If the length the initiator gives us isn't the one we specify in
6280 * the mode page header, or if they didn't specify enough data in
6281 * the CDB to avoid truncating this page, kick out the request.
6283 if ((page_len != (page_index->page_len - page_len_offset -
6285 || (*len_left < page_index->page_len)) {
6288 ctl_set_invalid_field(ctsio,
6291 /*field*/ *len_used + page_len_offset,
6294 free(ctsio->kern_data_ptr, M_CTL);
6295 ctl_done((union ctl_io *)ctsio);
6296 return (CTL_RETVAL_COMPLETE);
6300 * Run through the mode page, checking to make sure that the bits
6301 * the user changed are actually legal for him to change.
6303 for (i = 0; i < page_index->page_len; i++) {
6304 uint8_t *user_byte, *change_mask, *current_byte;
6308 user_byte = (uint8_t *)page_header + i;
6309 change_mask = page_index->page_data +
6310 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6311 current_byte = page_index->page_data +
6312 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6315 * Check to see whether the user set any bits in this byte
6316 * that he is not allowed to set.
6318 if ((*user_byte & ~(*change_mask)) ==
6319 (*current_byte & ~(*change_mask)))
6323 * Go through bit by bit to determine which one is illegal.
6326 for (j = 7; j >= 0; j--) {
6327 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6328 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6333 ctl_set_invalid_field(ctsio,
6336 /*field*/ *len_used + i,
6339 free(ctsio->kern_data_ptr, M_CTL);
6340 ctl_done((union ctl_io *)ctsio);
6341 return (CTL_RETVAL_COMPLETE);
6345 * Decrement these before we call the page handler, since we may
6346 * end up getting called back one way or another before the handler
6347 * returns to this context.
6349 *len_left -= page_index->page_len;
6350 *len_used += page_index->page_len;
6352 retval = page_index->select_handler(ctsio, page_index,
6353 (uint8_t *)page_header);
6356 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6357 * wait until this queued command completes to finish processing
6358 * the mode page. If it returns anything other than
6359 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6360 * already set the sense information, freed the data pointer, and
6361 * completed the io for us.
6363 if (retval != CTL_RETVAL_COMPLETE)
6364 goto bailout_no_done;
6367 * If the initiator sent us more than one page, parse the next one.
6372 ctl_set_success(ctsio);
6373 free(ctsio->kern_data_ptr, M_CTL);
6374 ctl_done((union ctl_io *)ctsio);
6378 return (CTL_RETVAL_COMPLETE);
6383 ctl_mode_select(struct ctl_scsiio *ctsio)
6385 int param_len, pf, sp;
6386 int header_size, bd_len;
6387 union ctl_modepage_info *modepage_info;
6389 switch (ctsio->cdb[0]) {
6390 case MODE_SELECT_6: {
6391 struct scsi_mode_select_6 *cdb;
6393 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6395 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6396 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6397 param_len = cdb->length;
6398 header_size = sizeof(struct scsi_mode_header_6);
6401 case MODE_SELECT_10: {
6402 struct scsi_mode_select_10 *cdb;
6404 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6406 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6407 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6408 param_len = scsi_2btoul(cdb->length);
6409 header_size = sizeof(struct scsi_mode_header_10);
6413 ctl_set_invalid_opcode(ctsio);
6414 ctl_done((union ctl_io *)ctsio);
6415 return (CTL_RETVAL_COMPLETE);
6420 * "A parameter list length of zero indicates that the Data-Out Buffer
6421 * shall be empty. This condition shall not be considered as an error."
6423 if (param_len == 0) {
6424 ctl_set_success(ctsio);
6425 ctl_done((union ctl_io *)ctsio);
6426 return (CTL_RETVAL_COMPLETE);
6430 * Since we'll hit this the first time through, prior to
6431 * allocation, we don't need to free a data buffer here.
6433 if (param_len < header_size) {
6434 ctl_set_param_len_error(ctsio);
6435 ctl_done((union ctl_io *)ctsio);
6436 return (CTL_RETVAL_COMPLETE);
6440 * Allocate the data buffer and grab the user's data. In theory,
6441 * we shouldn't have to sanity check the parameter list length here
6442 * because the maximum size is 64K. We should be able to malloc
6443 * that much without too many problems.
6445 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6446 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6447 ctsio->kern_data_len = param_len;
6448 ctsio->kern_total_len = param_len;
6449 ctsio->kern_data_resid = 0;
6450 ctsio->kern_rel_offset = 0;
6451 ctsio->kern_sg_entries = 0;
6452 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6453 ctsio->be_move_done = ctl_config_move_done;
6454 ctl_datamove((union ctl_io *)ctsio);
6456 return (CTL_RETVAL_COMPLETE);
6459 switch (ctsio->cdb[0]) {
6460 case MODE_SELECT_6: {
6461 struct scsi_mode_header_6 *mh6;
6463 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6464 bd_len = mh6->blk_desc_len;
6467 case MODE_SELECT_10: {
6468 struct scsi_mode_header_10 *mh10;
6470 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6471 bd_len = scsi_2btoul(mh10->blk_desc_len);
6475 panic("Invalid CDB type %#x", ctsio->cdb[0]);
6479 if (param_len < (header_size + bd_len)) {
6480 free(ctsio->kern_data_ptr, M_CTL);
6481 ctl_set_param_len_error(ctsio);
6482 ctl_done((union ctl_io *)ctsio);
6483 return (CTL_RETVAL_COMPLETE);
6487 * Set the IO_CONT flag, so that if this I/O gets passed to
6488 * ctl_config_write_done(), it'll get passed back to
6489 * ctl_do_mode_select() for further processing, or completion if
6492 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6493 ctsio->io_cont = ctl_do_mode_select;
6495 modepage_info = (union ctl_modepage_info *)
6496 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6497 memset(modepage_info, 0, sizeof(*modepage_info));
6498 modepage_info->header.len_left = param_len - header_size - bd_len;
6499 modepage_info->header.len_used = header_size + bd_len;
6501 return (ctl_do_mode_select((union ctl_io *)ctsio));
6505 ctl_mode_sense(struct ctl_scsiio *ctsio)
6507 struct ctl_lun *lun;
6508 int pc, page_code, dbd, llba, subpage;
6509 int alloc_len, page_len, header_len, total_len;
6510 struct scsi_mode_block_descr *block_desc;
6511 struct ctl_page_index *page_index;
6519 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6521 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6523 if (lun->be_lun->lun_type != T_DIRECT)
6528 switch (ctsio->cdb[0]) {
6529 case MODE_SENSE_6: {
6530 struct scsi_mode_sense_6 *cdb;
6532 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6534 header_len = sizeof(struct scsi_mode_hdr_6);
6535 if (cdb->byte2 & SMS_DBD)
6538 header_len += sizeof(struct scsi_mode_block_descr);
6540 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6541 page_code = cdb->page & SMS_PAGE_CODE;
6542 subpage = cdb->subpage;
6543 alloc_len = cdb->length;
6546 case MODE_SENSE_10: {
6547 struct scsi_mode_sense_10 *cdb;
6549 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6551 header_len = sizeof(struct scsi_mode_hdr_10);
6553 if (cdb->byte2 & SMS_DBD)
6556 header_len += sizeof(struct scsi_mode_block_descr);
6557 if (cdb->byte2 & SMS10_LLBAA)
6559 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6560 page_code = cdb->page & SMS_PAGE_CODE;
6561 subpage = cdb->subpage;
6562 alloc_len = scsi_2btoul(cdb->length);
6566 ctl_set_invalid_opcode(ctsio);
6567 ctl_done((union ctl_io *)ctsio);
6568 return (CTL_RETVAL_COMPLETE);
6569 break; /* NOTREACHED */
6573 * We have to make a first pass through to calculate the size of
6574 * the pages that match the user's query. Then we allocate enough
6575 * memory to hold it, and actually copy the data into the buffer.
6577 switch (page_code) {
6578 case SMS_ALL_PAGES_PAGE: {
6584 * At the moment, values other than 0 and 0xff here are
6585 * reserved according to SPC-3.
6587 if ((subpage != SMS_SUBPAGE_PAGE_0)
6588 && (subpage != SMS_SUBPAGE_ALL)) {
6589 ctl_set_invalid_field(ctsio,
6595 ctl_done((union ctl_io *)ctsio);
6596 return (CTL_RETVAL_COMPLETE);
6599 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6600 if ((control_dev != 0)
6601 && (lun->mode_pages.index[i].page_flags &
6602 CTL_PAGE_FLAG_DISK_ONLY))
6606 * We don't use this subpage if the user didn't
6607 * request all subpages.
6609 if ((lun->mode_pages.index[i].subpage != 0)
6610 && (subpage == SMS_SUBPAGE_PAGE_0))
6614 printf("found page %#x len %d\n",
6615 lun->mode_pages.index[i].page_code &
6617 lun->mode_pages.index[i].page_len);
6619 page_len += lun->mode_pages.index[i].page_len;
6628 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6629 /* Look for the right page code */
6630 if ((lun->mode_pages.index[i].page_code &
6631 SMPH_PC_MASK) != page_code)
6634 /* Look for the right subpage or the subpage wildcard*/
6635 if ((lun->mode_pages.index[i].subpage != subpage)
6636 && (subpage != SMS_SUBPAGE_ALL))
6639 /* Make sure the page is supported for this dev type */
6640 if ((control_dev != 0)
6641 && (lun->mode_pages.index[i].page_flags &
6642 CTL_PAGE_FLAG_DISK_ONLY))
6646 printf("found page %#x len %d\n",
6647 lun->mode_pages.index[i].page_code &
6649 lun->mode_pages.index[i].page_len);
6652 page_len += lun->mode_pages.index[i].page_len;
6655 if (page_len == 0) {
6656 ctl_set_invalid_field(ctsio,
6662 ctl_done((union ctl_io *)ctsio);
6663 return (CTL_RETVAL_COMPLETE);
6669 total_len = header_len + page_len;
6671 printf("header_len = %d, page_len = %d, total_len = %d\n",
6672 header_len, page_len, total_len);
6675 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6676 ctsio->kern_sg_entries = 0;
6677 ctsio->kern_data_resid = 0;
6678 ctsio->kern_rel_offset = 0;
6679 if (total_len < alloc_len) {
6680 ctsio->residual = alloc_len - total_len;
6681 ctsio->kern_data_len = total_len;
6682 ctsio->kern_total_len = total_len;
6684 ctsio->residual = 0;
6685 ctsio->kern_data_len = alloc_len;
6686 ctsio->kern_total_len = alloc_len;
6689 switch (ctsio->cdb[0]) {
6690 case MODE_SENSE_6: {
6691 struct scsi_mode_hdr_6 *header;
6693 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6695 header->datalen = MIN(total_len - 1, 254);
6696 if (control_dev == 0) {
6697 header->dev_specific = 0x10; /* DPOFUA */
6698 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6699 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6700 .eca_and_aen & SCP_SWP) != 0)
6701 header->dev_specific |= 0x80; /* WP */
6704 header->block_descr_len = 0;
6706 header->block_descr_len =
6707 sizeof(struct scsi_mode_block_descr);
6708 block_desc = (struct scsi_mode_block_descr *)&header[1];
6711 case MODE_SENSE_10: {
6712 struct scsi_mode_hdr_10 *header;
6715 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6717 datalen = MIN(total_len - 2, 65533);
6718 scsi_ulto2b(datalen, header->datalen);
6719 if (control_dev == 0) {
6720 header->dev_specific = 0x10; /* DPOFUA */
6721 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6722 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6723 .eca_and_aen & SCP_SWP) != 0)
6724 header->dev_specific |= 0x80; /* WP */
6727 scsi_ulto2b(0, header->block_descr_len);
6729 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6730 header->block_descr_len);
6731 block_desc = (struct scsi_mode_block_descr *)&header[1];
6735 panic("invalid CDB type %#x", ctsio->cdb[0]);
6736 break; /* NOTREACHED */
6740 * If we've got a disk, use its blocksize in the block
6741 * descriptor. Otherwise, just set it to 0.
6744 if (control_dev == 0)
6745 scsi_ulto3b(lun->be_lun->blocksize,
6746 block_desc->block_len);
6748 scsi_ulto3b(0, block_desc->block_len);
6751 switch (page_code) {
6752 case SMS_ALL_PAGES_PAGE: {
6755 data_used = header_len;
6756 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6757 struct ctl_page_index *page_index;
6759 page_index = &lun->mode_pages.index[i];
6761 if ((control_dev != 0)
6762 && (page_index->page_flags &
6763 CTL_PAGE_FLAG_DISK_ONLY))
6767 * We don't use this subpage if the user didn't
6768 * request all subpages. We already checked (above)
6769 * to make sure the user only specified a subpage
6770 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6772 if ((page_index->subpage != 0)
6773 && (subpage == SMS_SUBPAGE_PAGE_0))
6777 * Call the handler, if it exists, to update the
6778 * page to the latest values.
6780 if (page_index->sense_handler != NULL)
6781 page_index->sense_handler(ctsio, page_index,pc);
6783 memcpy(ctsio->kern_data_ptr + data_used,
6784 page_index->page_data +
6785 (page_index->page_len * pc),
6786 page_index->page_len);
6787 data_used += page_index->page_len;
6794 data_used = header_len;
6796 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6797 struct ctl_page_index *page_index;
6799 page_index = &lun->mode_pages.index[i];
6801 /* Look for the right page code */
6802 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6805 /* Look for the right subpage or the subpage wildcard*/
6806 if ((page_index->subpage != subpage)
6807 && (subpage != SMS_SUBPAGE_ALL))
6810 /* Make sure the page is supported for this dev type */
6811 if ((control_dev != 0)
6812 && (page_index->page_flags &
6813 CTL_PAGE_FLAG_DISK_ONLY))
6817 * Call the handler, if it exists, to update the
6818 * page to the latest values.
6820 if (page_index->sense_handler != NULL)
6821 page_index->sense_handler(ctsio, page_index,pc);
6823 memcpy(ctsio->kern_data_ptr + data_used,
6824 page_index->page_data +
6825 (page_index->page_len * pc),
6826 page_index->page_len);
6827 data_used += page_index->page_len;
6833 ctl_set_success(ctsio);
6834 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6835 ctsio->be_move_done = ctl_config_move_done;
6836 ctl_datamove((union ctl_io *)ctsio);
6837 return (CTL_RETVAL_COMPLETE);
6841 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6842 struct ctl_page_index *page_index,
6845 struct ctl_lun *lun;
6846 struct scsi_log_param_header *phdr;
6850 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6851 data = page_index->page_data;
6853 if (lun->backend->lun_attr != NULL &&
6854 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6856 phdr = (struct scsi_log_param_header *)data;
6857 scsi_ulto2b(0x0001, phdr->param_code);
6858 phdr->param_control = SLP_LBIN | SLP_LP;
6859 phdr->param_len = 8;
6860 data = (uint8_t *)(phdr + 1);
6861 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6862 data[4] = 0x02; /* per-pool */
6863 data += phdr->param_len;
6866 if (lun->backend->lun_attr != NULL &&
6867 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6869 phdr = (struct scsi_log_param_header *)data;
6870 scsi_ulto2b(0x0002, phdr->param_code);
6871 phdr->param_control = SLP_LBIN | SLP_LP;
6872 phdr->param_len = 8;
6873 data = (uint8_t *)(phdr + 1);
6874 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6875 data[4] = 0x01; /* per-LUN */
6876 data += phdr->param_len;
6879 if (lun->backend->lun_attr != NULL &&
6880 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6882 phdr = (struct scsi_log_param_header *)data;
6883 scsi_ulto2b(0x00f1, phdr->param_code);
6884 phdr->param_control = SLP_LBIN | SLP_LP;
6885 phdr->param_len = 8;
6886 data = (uint8_t *)(phdr + 1);
6887 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6888 data[4] = 0x02; /* per-pool */
6889 data += phdr->param_len;
6892 if (lun->backend->lun_attr != NULL &&
6893 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6895 phdr = (struct scsi_log_param_header *)data;
6896 scsi_ulto2b(0x00f2, phdr->param_code);
6897 phdr->param_control = SLP_LBIN | SLP_LP;
6898 phdr->param_len = 8;
6899 data = (uint8_t *)(phdr + 1);
6900 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6901 data[4] = 0x02; /* per-pool */
6902 data += phdr->param_len;
6905 page_index->page_len = data - page_index->page_data;
6910 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
6911 struct ctl_page_index *page_index,
6914 struct ctl_lun *lun;
6915 struct stat_page *data;
6916 uint64_t rn, wn, rb, wb;
6917 struct bintime rt, wt;
6920 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6921 data = (struct stat_page *)page_index->page_data;
6923 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
6924 data->sap.hdr.param_control = SLP_LBIN;
6925 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
6926 sizeof(struct scsi_log_param_header);
6927 rn = wn = rb = wb = 0;
6930 for (i = 0; i < CTL_MAX_PORTS; i++) {
6931 rn += lun->stats.ports[i].operations[CTL_STATS_READ];
6932 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE];
6933 rb += lun->stats.ports[i].bytes[CTL_STATS_READ];
6934 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE];
6935 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]);
6936 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]);
6938 scsi_u64to8b(rn, data->sap.read_num);
6939 scsi_u64to8b(wn, data->sap.write_num);
6940 if (lun->stats.blocksize > 0) {
6941 scsi_u64to8b(wb / lun->stats.blocksize,
6942 data->sap.recvieved_lba);
6943 scsi_u64to8b(rb / lun->stats.blocksize,
6944 data->sap.transmitted_lba);
6946 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000),
6947 data->sap.read_int);
6948 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000),
6949 data->sap.write_int);
6950 scsi_u64to8b(0, data->sap.weighted_num);
6951 scsi_u64to8b(0, data->sap.weighted_int);
6952 scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
6953 data->it.hdr.param_control = SLP_LBIN;
6954 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
6955 sizeof(struct scsi_log_param_header);
6957 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
6959 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
6960 data->it.hdr.param_control = SLP_LBIN;
6961 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
6962 sizeof(struct scsi_log_param_header);
6963 scsi_ulto4b(3, data->ti.exponent);
6964 scsi_ulto4b(1, data->ti.integer);
6966 page_index->page_len = sizeof(*data);
6971 ctl_log_sense(struct ctl_scsiio *ctsio)
6973 struct ctl_lun *lun;
6974 int i, pc, page_code, subpage;
6975 int alloc_len, total_len;
6976 struct ctl_page_index *page_index;
6977 struct scsi_log_sense *cdb;
6978 struct scsi_log_header *header;
6980 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6982 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6983 cdb = (struct scsi_log_sense *)ctsio->cdb;
6984 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6985 page_code = cdb->page & SLS_PAGE_CODE;
6986 subpage = cdb->subpage;
6987 alloc_len = scsi_2btoul(cdb->length);
6990 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6991 page_index = &lun->log_pages.index[i];
6993 /* Look for the right page code */
6994 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6997 /* Look for the right subpage or the subpage wildcard*/
6998 if (page_index->subpage != subpage)
7003 if (i >= CTL_NUM_LOG_PAGES) {
7004 ctl_set_invalid_field(ctsio,
7010 ctl_done((union ctl_io *)ctsio);
7011 return (CTL_RETVAL_COMPLETE);
7014 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
7016 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7017 ctsio->kern_sg_entries = 0;
7018 ctsio->kern_data_resid = 0;
7019 ctsio->kern_rel_offset = 0;
7020 if (total_len < alloc_len) {
7021 ctsio->residual = alloc_len - total_len;
7022 ctsio->kern_data_len = total_len;
7023 ctsio->kern_total_len = total_len;
7025 ctsio->residual = 0;
7026 ctsio->kern_data_len = alloc_len;
7027 ctsio->kern_total_len = alloc_len;
7030 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
7031 header->page = page_index->page_code;
7032 if (page_index->subpage) {
7033 header->page |= SL_SPF;
7034 header->subpage = page_index->subpage;
7036 scsi_ulto2b(page_index->page_len, header->datalen);
7039 * Call the handler, if it exists, to update the
7040 * page to the latest values.
7042 if (page_index->sense_handler != NULL)
7043 page_index->sense_handler(ctsio, page_index, pc);
7045 memcpy(header + 1, page_index->page_data, page_index->page_len);
7047 ctl_set_success(ctsio);
7048 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7049 ctsio->be_move_done = ctl_config_move_done;
7050 ctl_datamove((union ctl_io *)ctsio);
7051 return (CTL_RETVAL_COMPLETE);
7055 ctl_read_capacity(struct ctl_scsiio *ctsio)
7057 struct scsi_read_capacity *cdb;
7058 struct scsi_read_capacity_data *data;
7059 struct ctl_lun *lun;
7062 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
7064 cdb = (struct scsi_read_capacity *)ctsio->cdb;
7066 lba = scsi_4btoul(cdb->addr);
7067 if (((cdb->pmi & SRC_PMI) == 0)
7069 ctl_set_invalid_field(/*ctsio*/ ctsio,
7075 ctl_done((union ctl_io *)ctsio);
7076 return (CTL_RETVAL_COMPLETE);
7079 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7081 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7082 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
7083 ctsio->residual = 0;
7084 ctsio->kern_data_len = sizeof(*data);
7085 ctsio->kern_total_len = sizeof(*data);
7086 ctsio->kern_data_resid = 0;
7087 ctsio->kern_rel_offset = 0;
7088 ctsio->kern_sg_entries = 0;
7091 * If the maximum LBA is greater than 0xfffffffe, the user must
7092 * issue a SERVICE ACTION IN (16) command, with the read capacity
7093 * serivce action set.
7095 if (lun->be_lun->maxlba > 0xfffffffe)
7096 scsi_ulto4b(0xffffffff, data->addr);
7098 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
7101 * XXX KDM this may not be 512 bytes...
7103 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7105 ctl_set_success(ctsio);
7106 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7107 ctsio->be_move_done = ctl_config_move_done;
7108 ctl_datamove((union ctl_io *)ctsio);
7109 return (CTL_RETVAL_COMPLETE);
7113 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
7115 struct scsi_read_capacity_16 *cdb;
7116 struct scsi_read_capacity_data_long *data;
7117 struct ctl_lun *lun;
7121 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
7123 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
7125 alloc_len = scsi_4btoul(cdb->alloc_len);
7126 lba = scsi_8btou64(cdb->addr);
7128 if ((cdb->reladr & SRC16_PMI)
7130 ctl_set_invalid_field(/*ctsio*/ ctsio,
7136 ctl_done((union ctl_io *)ctsio);
7137 return (CTL_RETVAL_COMPLETE);
7140 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7142 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7143 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
7145 if (sizeof(*data) < alloc_len) {
7146 ctsio->residual = alloc_len - sizeof(*data);
7147 ctsio->kern_data_len = sizeof(*data);
7148 ctsio->kern_total_len = sizeof(*data);
7150 ctsio->residual = 0;
7151 ctsio->kern_data_len = alloc_len;
7152 ctsio->kern_total_len = alloc_len;
7154 ctsio->kern_data_resid = 0;
7155 ctsio->kern_rel_offset = 0;
7156 ctsio->kern_sg_entries = 0;
7158 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7159 /* XXX KDM this may not be 512 bytes... */
7160 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7161 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7162 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7163 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7164 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
7166 ctl_set_success(ctsio);
7167 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7168 ctsio->be_move_done = ctl_config_move_done;
7169 ctl_datamove((union ctl_io *)ctsio);
7170 return (CTL_RETVAL_COMPLETE);
7174 ctl_get_lba_status(struct ctl_scsiio *ctsio)
7176 struct scsi_get_lba_status *cdb;
7177 struct scsi_get_lba_status_data *data;
7178 struct ctl_lun *lun;
7179 struct ctl_lba_len_flags *lbalen;
7181 uint32_t alloc_len, total_len;
7184 CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
7186 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7187 cdb = (struct scsi_get_lba_status *)ctsio->cdb;
7188 lba = scsi_8btou64(cdb->addr);
7189 alloc_len = scsi_4btoul(cdb->alloc_len);
7191 if (lba > lun->be_lun->maxlba) {
7192 ctl_set_lba_out_of_range(ctsio);
7193 ctl_done((union ctl_io *)ctsio);
7194 return (CTL_RETVAL_COMPLETE);
7197 total_len = sizeof(*data) + sizeof(data->descr[0]);
7198 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7199 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
7201 if (total_len < alloc_len) {
7202 ctsio->residual = alloc_len - total_len;
7203 ctsio->kern_data_len = total_len;
7204 ctsio->kern_total_len = total_len;
7206 ctsio->residual = 0;
7207 ctsio->kern_data_len = alloc_len;
7208 ctsio->kern_total_len = alloc_len;
7210 ctsio->kern_data_resid = 0;
7211 ctsio->kern_rel_offset = 0;
7212 ctsio->kern_sg_entries = 0;
7214 /* Fill dummy data in case backend can't tell anything. */
7215 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
7216 scsi_u64to8b(lba, data->descr[0].addr);
7217 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
7218 data->descr[0].length);
7219 data->descr[0].status = 0; /* Mapped or unknown. */
7221 ctl_set_success(ctsio);
7222 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7223 ctsio->be_move_done = ctl_config_move_done;
7225 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
7227 lbalen->len = total_len;
7229 retval = lun->backend->config_read((union ctl_io *)ctsio);
7230 return (CTL_RETVAL_COMPLETE);
7234 ctl_read_defect(struct ctl_scsiio *ctsio)
7236 struct scsi_read_defect_data_10 *ccb10;
7237 struct scsi_read_defect_data_12 *ccb12;
7238 struct scsi_read_defect_data_hdr_10 *data10;
7239 struct scsi_read_defect_data_hdr_12 *data12;
7240 uint32_t alloc_len, data_len;
7243 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7245 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7246 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7247 format = ccb10->format;
7248 alloc_len = scsi_2btoul(ccb10->alloc_length);
7249 data_len = sizeof(*data10);
7251 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7252 format = ccb12->format;
7253 alloc_len = scsi_4btoul(ccb12->alloc_length);
7254 data_len = sizeof(*data12);
7256 if (alloc_len == 0) {
7257 ctl_set_success(ctsio);
7258 ctl_done((union ctl_io *)ctsio);
7259 return (CTL_RETVAL_COMPLETE);
7262 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7263 if (data_len < alloc_len) {
7264 ctsio->residual = alloc_len - data_len;
7265 ctsio->kern_data_len = data_len;
7266 ctsio->kern_total_len = data_len;
7268 ctsio->residual = 0;
7269 ctsio->kern_data_len = alloc_len;
7270 ctsio->kern_total_len = alloc_len;
7272 ctsio->kern_data_resid = 0;
7273 ctsio->kern_rel_offset = 0;
7274 ctsio->kern_sg_entries = 0;
7276 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7277 data10 = (struct scsi_read_defect_data_hdr_10 *)
7278 ctsio->kern_data_ptr;
7279 data10->format = format;
7280 scsi_ulto2b(0, data10->length);
7282 data12 = (struct scsi_read_defect_data_hdr_12 *)
7283 ctsio->kern_data_ptr;
7284 data12->format = format;
7285 scsi_ulto2b(0, data12->generation);
7286 scsi_ulto4b(0, data12->length);
7289 ctl_set_success(ctsio);
7290 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7291 ctsio->be_move_done = ctl_config_move_done;
7292 ctl_datamove((union ctl_io *)ctsio);
7293 return (CTL_RETVAL_COMPLETE);
7297 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7299 struct scsi_maintenance_in *cdb;
7301 int alloc_len, ext, total_len = 0, g, pc, pg, gs, os;
7302 int num_target_port_groups, num_target_ports;
7303 struct ctl_lun *lun;
7304 struct ctl_softc *softc;
7305 struct ctl_port *port;
7306 struct scsi_target_group_data *rtg_ptr;
7307 struct scsi_target_group_data_extended *rtg_ext_ptr;
7308 struct scsi_target_port_group_descriptor *tpg_desc;
7310 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7312 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7313 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7314 softc = lun->ctl_softc;
7316 retval = CTL_RETVAL_COMPLETE;
7318 switch (cdb->byte2 & STG_PDF_MASK) {
7319 case STG_PDF_LENGTH:
7322 case STG_PDF_EXTENDED:
7326 ctl_set_invalid_field(/*ctsio*/ ctsio,
7332 ctl_done((union ctl_io *)ctsio);
7336 if (softc->is_single)
7337 num_target_port_groups = 1;
7339 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
7340 num_target_ports = 0;
7341 mtx_lock(&softc->ctl_lock);
7342 STAILQ_FOREACH(port, &softc->port_list, links) {
7343 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7345 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7349 mtx_unlock(&softc->ctl_lock);
7352 total_len = sizeof(struct scsi_target_group_data_extended);
7354 total_len = sizeof(struct scsi_target_group_data);
7355 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7356 num_target_port_groups +
7357 sizeof(struct scsi_target_port_descriptor) * num_target_ports;
7359 alloc_len = scsi_4btoul(cdb->length);
7361 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7363 ctsio->kern_sg_entries = 0;
7365 if (total_len < alloc_len) {
7366 ctsio->residual = alloc_len - total_len;
7367 ctsio->kern_data_len = total_len;
7368 ctsio->kern_total_len = total_len;
7370 ctsio->residual = 0;
7371 ctsio->kern_data_len = alloc_len;
7372 ctsio->kern_total_len = alloc_len;
7374 ctsio->kern_data_resid = 0;
7375 ctsio->kern_rel_offset = 0;
7378 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7379 ctsio->kern_data_ptr;
7380 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7381 rtg_ext_ptr->format_type = 0x10;
7382 rtg_ext_ptr->implicit_transition_time = 0;
7383 tpg_desc = &rtg_ext_ptr->groups[0];
7385 rtg_ptr = (struct scsi_target_group_data *)
7386 ctsio->kern_data_ptr;
7387 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7388 tpg_desc = &rtg_ptr->groups[0];
7391 mtx_lock(&softc->ctl_lock);
7392 pg = softc->port_min / softc->port_cnt;
7393 if (softc->ha_link == CTL_HA_LINK_OFFLINE)
7394 gs = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7395 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN)
7396 gs = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7397 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY)
7398 gs = TPG_ASYMMETRIC_ACCESS_STANDBY;
7400 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7401 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7403 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7405 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7406 for (g = 0; g < num_target_port_groups; g++) {
7407 tpg_desc->pref_state = (g == pg) ? gs : os;
7408 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7409 TPG_U_SUP | TPG_T_SUP;
7410 scsi_ulto2b(g + 1, tpg_desc->target_port_group);
7411 tpg_desc->status = TPG_IMPLICIT;
7413 STAILQ_FOREACH(port, &softc->port_list, links) {
7414 if (port->targ_port < g * softc->port_cnt ||
7415 port->targ_port >= (g + 1) * softc->port_cnt)
7417 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7419 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7421 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7422 relative_target_port_identifier);
7425 tpg_desc->target_port_count = pc;
7426 tpg_desc = (struct scsi_target_port_group_descriptor *)
7427 &tpg_desc->descriptors[pc];
7429 mtx_unlock(&softc->ctl_lock);
7431 ctl_set_success(ctsio);
7432 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7433 ctsio->be_move_done = ctl_config_move_done;
7434 ctl_datamove((union ctl_io *)ctsio);
7439 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7441 struct ctl_lun *lun;
7442 struct scsi_report_supported_opcodes *cdb;
7443 const struct ctl_cmd_entry *entry, *sentry;
7444 struct scsi_report_supported_opcodes_all *all;
7445 struct scsi_report_supported_opcodes_descr *descr;
7446 struct scsi_report_supported_opcodes_one *one;
7448 int alloc_len, total_len;
7449 int opcode, service_action, i, j, num;
7451 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7453 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7454 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7456 retval = CTL_RETVAL_COMPLETE;
7458 opcode = cdb->requested_opcode;
7459 service_action = scsi_2btoul(cdb->requested_service_action);
7460 switch (cdb->options & RSO_OPTIONS_MASK) {
7461 case RSO_OPTIONS_ALL:
7463 for (i = 0; i < 256; i++) {
7464 entry = &ctl_cmd_table[i];
7465 if (entry->flags & CTL_CMD_FLAG_SA5) {
7466 for (j = 0; j < 32; j++) {
7467 sentry = &((const struct ctl_cmd_entry *)
7469 if (ctl_cmd_applicable(
7470 lun->be_lun->lun_type, sentry))
7474 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7479 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7480 num * sizeof(struct scsi_report_supported_opcodes_descr);
7482 case RSO_OPTIONS_OC:
7483 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7484 ctl_set_invalid_field(/*ctsio*/ ctsio,
7490 ctl_done((union ctl_io *)ctsio);
7491 return (CTL_RETVAL_COMPLETE);
7493 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7495 case RSO_OPTIONS_OC_SA:
7496 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7497 service_action >= 32) {
7498 ctl_set_invalid_field(/*ctsio*/ ctsio,
7504 ctl_done((union ctl_io *)ctsio);
7505 return (CTL_RETVAL_COMPLETE);
7507 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7510 ctl_set_invalid_field(/*ctsio*/ ctsio,
7516 ctl_done((union ctl_io *)ctsio);
7517 return (CTL_RETVAL_COMPLETE);
7520 alloc_len = scsi_4btoul(cdb->length);
7522 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7524 ctsio->kern_sg_entries = 0;
7526 if (total_len < alloc_len) {
7527 ctsio->residual = alloc_len - total_len;
7528 ctsio->kern_data_len = total_len;
7529 ctsio->kern_total_len = total_len;
7531 ctsio->residual = 0;
7532 ctsio->kern_data_len = alloc_len;
7533 ctsio->kern_total_len = alloc_len;
7535 ctsio->kern_data_resid = 0;
7536 ctsio->kern_rel_offset = 0;
7538 switch (cdb->options & RSO_OPTIONS_MASK) {
7539 case RSO_OPTIONS_ALL:
7540 all = (struct scsi_report_supported_opcodes_all *)
7541 ctsio->kern_data_ptr;
7543 for (i = 0; i < 256; i++) {
7544 entry = &ctl_cmd_table[i];
7545 if (entry->flags & CTL_CMD_FLAG_SA5) {
7546 for (j = 0; j < 32; j++) {
7547 sentry = &((const struct ctl_cmd_entry *)
7549 if (!ctl_cmd_applicable(
7550 lun->be_lun->lun_type, sentry))
7552 descr = &all->descr[num++];
7554 scsi_ulto2b(j, descr->service_action);
7555 descr->flags = RSO_SERVACTV;
7556 scsi_ulto2b(sentry->length,
7560 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7563 descr = &all->descr[num++];
7565 scsi_ulto2b(0, descr->service_action);
7567 scsi_ulto2b(entry->length, descr->cdb_length);
7571 num * sizeof(struct scsi_report_supported_opcodes_descr),
7574 case RSO_OPTIONS_OC:
7575 one = (struct scsi_report_supported_opcodes_one *)
7576 ctsio->kern_data_ptr;
7577 entry = &ctl_cmd_table[opcode];
7579 case RSO_OPTIONS_OC_SA:
7580 one = (struct scsi_report_supported_opcodes_one *)
7581 ctsio->kern_data_ptr;
7582 entry = &ctl_cmd_table[opcode];
7583 entry = &((const struct ctl_cmd_entry *)
7584 entry->execute)[service_action];
7586 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7588 scsi_ulto2b(entry->length, one->cdb_length);
7589 one->cdb_usage[0] = opcode;
7590 memcpy(&one->cdb_usage[1], entry->usage,
7597 ctl_set_success(ctsio);
7598 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7599 ctsio->be_move_done = ctl_config_move_done;
7600 ctl_datamove((union ctl_io *)ctsio);
7605 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7607 struct scsi_report_supported_tmf *cdb;
7608 struct scsi_report_supported_tmf_data *data;
7610 int alloc_len, total_len;
7612 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7614 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7616 retval = CTL_RETVAL_COMPLETE;
7618 total_len = sizeof(struct scsi_report_supported_tmf_data);
7619 alloc_len = scsi_4btoul(cdb->length);
7621 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7623 ctsio->kern_sg_entries = 0;
7625 if (total_len < alloc_len) {
7626 ctsio->residual = alloc_len - total_len;
7627 ctsio->kern_data_len = total_len;
7628 ctsio->kern_total_len = total_len;
7630 ctsio->residual = 0;
7631 ctsio->kern_data_len = alloc_len;
7632 ctsio->kern_total_len = alloc_len;
7634 ctsio->kern_data_resid = 0;
7635 ctsio->kern_rel_offset = 0;
7637 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr;
7638 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS |
7640 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS;
7642 ctl_set_success(ctsio);
7643 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7644 ctsio->be_move_done = ctl_config_move_done;
7645 ctl_datamove((union ctl_io *)ctsio);
7650 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7652 struct scsi_report_timestamp *cdb;
7653 struct scsi_report_timestamp_data *data;
7657 int alloc_len, total_len;
7659 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7661 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7663 retval = CTL_RETVAL_COMPLETE;
7665 total_len = sizeof(struct scsi_report_timestamp_data);
7666 alloc_len = scsi_4btoul(cdb->length);
7668 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7670 ctsio->kern_sg_entries = 0;
7672 if (total_len < alloc_len) {
7673 ctsio->residual = alloc_len - total_len;
7674 ctsio->kern_data_len = total_len;
7675 ctsio->kern_total_len = total_len;
7677 ctsio->residual = 0;
7678 ctsio->kern_data_len = alloc_len;
7679 ctsio->kern_total_len = alloc_len;
7681 ctsio->kern_data_resid = 0;
7682 ctsio->kern_rel_offset = 0;
7684 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7685 scsi_ulto2b(sizeof(*data) - 2, data->length);
7686 data->origin = RTS_ORIG_OUTSIDE;
7688 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7689 scsi_ulto4b(timestamp >> 16, data->timestamp);
7690 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7692 ctl_set_success(ctsio);
7693 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7694 ctsio->be_move_done = ctl_config_move_done;
7695 ctl_datamove((union ctl_io *)ctsio);
7700 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7702 struct scsi_per_res_in *cdb;
7703 int alloc_len, total_len = 0;
7704 /* struct scsi_per_res_in_rsrv in_data; */
7705 struct ctl_lun *lun;
7706 struct ctl_softc *softc;
7709 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7711 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7713 alloc_len = scsi_2btoul(cdb->length);
7715 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7716 softc = lun->ctl_softc;
7719 mtx_lock(&lun->lun_lock);
7720 switch (cdb->action) {
7721 case SPRI_RK: /* read keys */
7722 total_len = sizeof(struct scsi_per_res_in_keys) +
7724 sizeof(struct scsi_per_res_key);
7726 case SPRI_RR: /* read reservation */
7727 if (lun->flags & CTL_LUN_PR_RESERVED)
7728 total_len = sizeof(struct scsi_per_res_in_rsrv);
7730 total_len = sizeof(struct scsi_per_res_in_header);
7732 case SPRI_RC: /* report capabilities */
7733 total_len = sizeof(struct scsi_per_res_cap);
7735 case SPRI_RS: /* read full status */
7736 total_len = sizeof(struct scsi_per_res_in_header) +
7737 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7741 panic("Invalid PR type %x", cdb->action);
7743 mtx_unlock(&lun->lun_lock);
7745 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7747 if (total_len < alloc_len) {
7748 ctsio->residual = alloc_len - total_len;
7749 ctsio->kern_data_len = total_len;
7750 ctsio->kern_total_len = total_len;
7752 ctsio->residual = 0;
7753 ctsio->kern_data_len = alloc_len;
7754 ctsio->kern_total_len = alloc_len;
7757 ctsio->kern_data_resid = 0;
7758 ctsio->kern_rel_offset = 0;
7759 ctsio->kern_sg_entries = 0;
7761 mtx_lock(&lun->lun_lock);
7762 switch (cdb->action) {
7763 case SPRI_RK: { // read keys
7764 struct scsi_per_res_in_keys *res_keys;
7767 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7770 * We had to drop the lock to allocate our buffer, which
7771 * leaves time for someone to come in with another
7772 * persistent reservation. (That is unlikely, though,
7773 * since this should be the only persistent reservation
7774 * command active right now.)
7776 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7777 (lun->pr_key_count *
7778 sizeof(struct scsi_per_res_key)))){
7779 mtx_unlock(&lun->lun_lock);
7780 free(ctsio->kern_data_ptr, M_CTL);
7781 printf("%s: reservation length changed, retrying\n",
7786 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7788 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7789 lun->pr_key_count, res_keys->header.length);
7791 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) {
7792 if ((key = ctl_get_prkey(lun, i)) == 0)
7796 * We used lun->pr_key_count to calculate the
7797 * size to allocate. If it turns out the number of
7798 * initiators with the registered flag set is
7799 * larger than that (i.e. they haven't been kept in
7800 * sync), we've got a problem.
7802 if (key_count >= lun->pr_key_count) {
7804 csevent_log(CSC_CTL | CSC_SHELF_SW |
7806 csevent_LogType_Fault,
7807 csevent_AlertLevel_Yellow,
7808 csevent_FRU_ShelfController,
7809 csevent_FRU_Firmware,
7810 csevent_FRU_Unknown,
7811 "registered keys %d >= key "
7812 "count %d", key_count,
7818 scsi_u64to8b(key, res_keys->keys[key_count].key);
7823 case SPRI_RR: { // read reservation
7824 struct scsi_per_res_in_rsrv *res;
7825 int tmp_len, header_only;
7827 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7829 scsi_ulto4b(lun->PRGeneration, res->header.generation);
7831 if (lun->flags & CTL_LUN_PR_RESERVED)
7833 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7834 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7835 res->header.length);
7838 tmp_len = sizeof(struct scsi_per_res_in_header);
7839 scsi_ulto4b(0, res->header.length);
7844 * We had to drop the lock to allocate our buffer, which
7845 * leaves time for someone to come in with another
7846 * persistent reservation. (That is unlikely, though,
7847 * since this should be the only persistent reservation
7848 * command active right now.)
7850 if (tmp_len != total_len) {
7851 mtx_unlock(&lun->lun_lock);
7852 free(ctsio->kern_data_ptr, M_CTL);
7853 printf("%s: reservation status changed, retrying\n",
7859 * No reservation held, so we're done.
7861 if (header_only != 0)
7865 * If the registration is an All Registrants type, the key
7866 * is 0, since it doesn't really matter.
7868 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7869 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7870 res->data.reservation);
7872 res->data.scopetype = lun->res_type;
7875 case SPRI_RC: //report capabilities
7877 struct scsi_per_res_cap *res_cap;
7880 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7881 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7882 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5;
7883 type_mask = SPRI_TM_WR_EX_AR |
7889 scsi_ulto2b(type_mask, res_cap->type_mask);
7892 case SPRI_RS: { // read full status
7893 struct scsi_per_res_in_full *res_status;
7894 struct scsi_per_res_in_full_desc *res_desc;
7895 struct ctl_port *port;
7898 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7901 * We had to drop the lock to allocate our buffer, which
7902 * leaves time for someone to come in with another
7903 * persistent reservation. (That is unlikely, though,
7904 * since this should be the only persistent reservation
7905 * command active right now.)
7907 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7908 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7909 lun->pr_key_count)){
7910 mtx_unlock(&lun->lun_lock);
7911 free(ctsio->kern_data_ptr, M_CTL);
7912 printf("%s: reservation length changed, retrying\n",
7917 scsi_ulto4b(lun->PRGeneration, res_status->header.generation);
7919 res_desc = &res_status->desc[0];
7920 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7921 if ((key = ctl_get_prkey(lun, i)) == 0)
7924 scsi_u64to8b(key, res_desc->res_key.key);
7925 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7926 (lun->pr_res_idx == i ||
7927 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7928 res_desc->flags = SPRI_FULL_R_HOLDER;
7929 res_desc->scopetype = lun->res_type;
7931 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7932 res_desc->rel_trgt_port_id);
7934 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7936 len = ctl_create_iid(port,
7937 i % CTL_MAX_INIT_PER_PORT,
7938 res_desc->transport_id);
7939 scsi_ulto4b(len, res_desc->additional_length);
7940 res_desc = (struct scsi_per_res_in_full_desc *)
7941 &res_desc->transport_id[len];
7943 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7944 res_status->header.length);
7949 * This is a bug, because we just checked for this above,
7950 * and should have returned an error.
7952 panic("Invalid PR type %x", cdb->action);
7953 break; /* NOTREACHED */
7955 mtx_unlock(&lun->lun_lock);
7957 ctl_set_success(ctsio);
7958 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7959 ctsio->be_move_done = ctl_config_move_done;
7960 ctl_datamove((union ctl_io *)ctsio);
7961 return (CTL_RETVAL_COMPLETE);
7965 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7969 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7970 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7971 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7972 struct scsi_per_res_out_parms* param)
7974 union ctl_ha_msg persis_io;
7977 mtx_lock(&lun->lun_lock);
7978 if (sa_res_key == 0) {
7979 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7980 /* validate scope and type */
7981 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7983 mtx_unlock(&lun->lun_lock);
7984 ctl_set_invalid_field(/*ctsio*/ ctsio,
7990 ctl_done((union ctl_io *)ctsio);
7994 if (type>8 || type==2 || type==4 || type==0) {
7995 mtx_unlock(&lun->lun_lock);
7996 ctl_set_invalid_field(/*ctsio*/ ctsio,
8002 ctl_done((union ctl_io *)ctsio);
8007 * Unregister everybody else and build UA for
8010 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8011 if (i == residx || ctl_get_prkey(lun, i) == 0)
8014 ctl_clr_prkey(lun, i);
8015 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8017 lun->pr_key_count = 1;
8018 lun->res_type = type;
8019 if (lun->res_type != SPR_TYPE_WR_EX_AR
8020 && lun->res_type != SPR_TYPE_EX_AC_AR)
8021 lun->pr_res_idx = residx;
8022 lun->PRGeneration++;
8023 mtx_unlock(&lun->lun_lock);
8025 /* send msg to other side */
8026 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8027 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8028 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8029 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8030 persis_io.pr.pr_info.res_type = type;
8031 memcpy(persis_io.pr.pr_info.sa_res_key,
8032 param->serv_act_res_key,
8033 sizeof(param->serv_act_res_key));
8034 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8035 sizeof(persis_io.pr), M_WAITOK);
8037 /* not all registrants */
8038 mtx_unlock(&lun->lun_lock);
8039 free(ctsio->kern_data_ptr, M_CTL);
8040 ctl_set_invalid_field(ctsio,
8046 ctl_done((union ctl_io *)ctsio);
8049 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8050 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
8053 if (res_key == sa_res_key) {
8056 * The spec implies this is not good but doesn't
8057 * say what to do. There are two choices either
8058 * generate a res conflict or check condition
8059 * with illegal field in parameter data. Since
8060 * that is what is done when the sa_res_key is
8061 * zero I'll take that approach since this has
8062 * to do with the sa_res_key.
8064 mtx_unlock(&lun->lun_lock);
8065 free(ctsio->kern_data_ptr, M_CTL);
8066 ctl_set_invalid_field(ctsio,
8072 ctl_done((union ctl_io *)ctsio);
8076 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8077 if (ctl_get_prkey(lun, i) != sa_res_key)
8081 ctl_clr_prkey(lun, i);
8082 lun->pr_key_count--;
8083 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8086 mtx_unlock(&lun->lun_lock);
8087 free(ctsio->kern_data_ptr, M_CTL);
8088 ctl_set_reservation_conflict(ctsio);
8089 ctl_done((union ctl_io *)ctsio);
8090 return (CTL_RETVAL_COMPLETE);
8092 lun->PRGeneration++;
8093 mtx_unlock(&lun->lun_lock);
8095 /* send msg to other side */
8096 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8097 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8098 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8099 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8100 persis_io.pr.pr_info.res_type = type;
8101 memcpy(persis_io.pr.pr_info.sa_res_key,
8102 param->serv_act_res_key,
8103 sizeof(param->serv_act_res_key));
8104 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8105 sizeof(persis_io.pr), M_WAITOK);
8107 /* Reserved but not all registrants */
8108 /* sa_res_key is res holder */
8109 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
8110 /* validate scope and type */
8111 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
8113 mtx_unlock(&lun->lun_lock);
8114 ctl_set_invalid_field(/*ctsio*/ ctsio,
8120 ctl_done((union ctl_io *)ctsio);
8124 if (type>8 || type==2 || type==4 || type==0) {
8125 mtx_unlock(&lun->lun_lock);
8126 ctl_set_invalid_field(/*ctsio*/ ctsio,
8132 ctl_done((union ctl_io *)ctsio);
8138 * if sa_res_key != res_key remove all
8139 * registrants w/sa_res_key and generate UA
8140 * for these registrants(Registrations
8141 * Preempted) if it wasn't an exclusive
8142 * reservation generate UA(Reservations
8143 * Preempted) for all other registered nexuses
8144 * if the type has changed. Establish the new
8145 * reservation and holder. If res_key and
8146 * sa_res_key are the same do the above
8147 * except don't unregister the res holder.
8150 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8151 if (i == residx || ctl_get_prkey(lun, i) == 0)
8154 if (sa_res_key == ctl_get_prkey(lun, i)) {
8155 ctl_clr_prkey(lun, i);
8156 lun->pr_key_count--;
8157 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8158 } else if (type != lun->res_type
8159 && (lun->res_type == SPR_TYPE_WR_EX_RO
8160 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
8161 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8164 lun->res_type = type;
8165 if (lun->res_type != SPR_TYPE_WR_EX_AR
8166 && lun->res_type != SPR_TYPE_EX_AC_AR)
8167 lun->pr_res_idx = residx;
8169 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8170 lun->PRGeneration++;
8171 mtx_unlock(&lun->lun_lock);
8173 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8174 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8175 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8176 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8177 persis_io.pr.pr_info.res_type = type;
8178 memcpy(persis_io.pr.pr_info.sa_res_key,
8179 param->serv_act_res_key,
8180 sizeof(param->serv_act_res_key));
8181 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8182 sizeof(persis_io.pr), M_WAITOK);
8185 * sa_res_key is not the res holder just
8186 * remove registrants
8190 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8191 if (sa_res_key != ctl_get_prkey(lun, i))
8195 ctl_clr_prkey(lun, i);
8196 lun->pr_key_count--;
8197 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8201 mtx_unlock(&lun->lun_lock);
8202 free(ctsio->kern_data_ptr, M_CTL);
8203 ctl_set_reservation_conflict(ctsio);
8204 ctl_done((union ctl_io *)ctsio);
8207 lun->PRGeneration++;
8208 mtx_unlock(&lun->lun_lock);
8210 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8211 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8212 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8213 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8214 persis_io.pr.pr_info.res_type = type;
8215 memcpy(persis_io.pr.pr_info.sa_res_key,
8216 param->serv_act_res_key,
8217 sizeof(param->serv_act_res_key));
8218 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8219 sizeof(persis_io.pr), M_WAITOK);
8226 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8228 uint64_t sa_res_key;
8231 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8233 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8234 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8235 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
8236 if (sa_res_key == 0) {
8238 * Unregister everybody else and build UA for
8241 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8242 if (i == msg->pr.pr_info.residx ||
8243 ctl_get_prkey(lun, i) == 0)
8246 ctl_clr_prkey(lun, i);
8247 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8250 lun->pr_key_count = 1;
8251 lun->res_type = msg->pr.pr_info.res_type;
8252 if (lun->res_type != SPR_TYPE_WR_EX_AR
8253 && lun->res_type != SPR_TYPE_EX_AC_AR)
8254 lun->pr_res_idx = msg->pr.pr_info.residx;
8256 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8257 if (sa_res_key == ctl_get_prkey(lun, i))
8260 ctl_clr_prkey(lun, i);
8261 lun->pr_key_count--;
8262 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8266 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8267 if (i == msg->pr.pr_info.residx ||
8268 ctl_get_prkey(lun, i) == 0)
8271 if (sa_res_key == ctl_get_prkey(lun, i)) {
8272 ctl_clr_prkey(lun, i);
8273 lun->pr_key_count--;
8274 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8275 } else if (msg->pr.pr_info.res_type != lun->res_type
8276 && (lun->res_type == SPR_TYPE_WR_EX_RO
8277 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
8278 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8281 lun->res_type = msg->pr.pr_info.res_type;
8282 if (lun->res_type != SPR_TYPE_WR_EX_AR
8283 && lun->res_type != SPR_TYPE_EX_AC_AR)
8284 lun->pr_res_idx = msg->pr.pr_info.residx;
8286 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8288 lun->PRGeneration++;
8294 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8297 u_int32_t param_len;
8298 struct scsi_per_res_out *cdb;
8299 struct ctl_lun *lun;
8300 struct scsi_per_res_out_parms* param;
8301 struct ctl_softc *softc;
8303 uint64_t res_key, sa_res_key, key;
8305 union ctl_ha_msg persis_io;
8308 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8310 retval = CTL_RETVAL_COMPLETE;
8312 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8313 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8314 softc = lun->ctl_softc;
8317 * We only support whole-LUN scope. The scope & type are ignored for
8318 * register, register and ignore existing key and clear.
8319 * We sometimes ignore scope and type on preempts too!!
8320 * Verify reservation type here as well.
8322 type = cdb->scope_type & SPR_TYPE_MASK;
8323 if ((cdb->action == SPRO_RESERVE)
8324 || (cdb->action == SPRO_RELEASE)) {
8325 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8326 ctl_set_invalid_field(/*ctsio*/ ctsio,
8332 ctl_done((union ctl_io *)ctsio);
8333 return (CTL_RETVAL_COMPLETE);
8336 if (type>8 || type==2 || type==4 || type==0) {
8337 ctl_set_invalid_field(/*ctsio*/ ctsio,
8343 ctl_done((union ctl_io *)ctsio);
8344 return (CTL_RETVAL_COMPLETE);
8348 param_len = scsi_4btoul(cdb->length);
8350 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8351 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8352 ctsio->kern_data_len = param_len;
8353 ctsio->kern_total_len = param_len;
8354 ctsio->kern_data_resid = 0;
8355 ctsio->kern_rel_offset = 0;
8356 ctsio->kern_sg_entries = 0;
8357 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8358 ctsio->be_move_done = ctl_config_move_done;
8359 ctl_datamove((union ctl_io *)ctsio);
8361 return (CTL_RETVAL_COMPLETE);
8364 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8366 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8367 res_key = scsi_8btou64(param->res_key.key);
8368 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8371 * Validate the reservation key here except for SPRO_REG_IGNO
8372 * This must be done for all other service actions
8374 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8375 mtx_lock(&lun->lun_lock);
8376 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8377 if (res_key != key) {
8379 * The current key passed in doesn't match
8380 * the one the initiator previously
8383 mtx_unlock(&lun->lun_lock);
8384 free(ctsio->kern_data_ptr, M_CTL);
8385 ctl_set_reservation_conflict(ctsio);
8386 ctl_done((union ctl_io *)ctsio);
8387 return (CTL_RETVAL_COMPLETE);
8389 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8391 * We are not registered
8393 mtx_unlock(&lun->lun_lock);
8394 free(ctsio->kern_data_ptr, M_CTL);
8395 ctl_set_reservation_conflict(ctsio);
8396 ctl_done((union ctl_io *)ctsio);
8397 return (CTL_RETVAL_COMPLETE);
8398 } else if (res_key != 0) {
8400 * We are not registered and trying to register but
8401 * the register key isn't zero.
8403 mtx_unlock(&lun->lun_lock);
8404 free(ctsio->kern_data_ptr, M_CTL);
8405 ctl_set_reservation_conflict(ctsio);
8406 ctl_done((union ctl_io *)ctsio);
8407 return (CTL_RETVAL_COMPLETE);
8409 mtx_unlock(&lun->lun_lock);
8412 switch (cdb->action & SPRO_ACTION_MASK) {
8414 case SPRO_REG_IGNO: {
8417 printf("Registration received\n");
8421 * We don't support any of these options, as we report in
8422 * the read capabilities request (see
8423 * ctl_persistent_reserve_in(), above).
8425 if ((param->flags & SPR_SPEC_I_PT)
8426 || (param->flags & SPR_ALL_TG_PT)
8427 || (param->flags & SPR_APTPL)) {
8430 if (param->flags & SPR_APTPL)
8432 else if (param->flags & SPR_ALL_TG_PT)
8434 else /* SPR_SPEC_I_PT */
8437 free(ctsio->kern_data_ptr, M_CTL);
8438 ctl_set_invalid_field(ctsio,
8444 ctl_done((union ctl_io *)ctsio);
8445 return (CTL_RETVAL_COMPLETE);
8448 mtx_lock(&lun->lun_lock);
8451 * The initiator wants to clear the
8454 if (sa_res_key == 0) {
8456 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8457 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8458 && ctl_get_prkey(lun, residx) == 0)) {
8459 mtx_unlock(&lun->lun_lock);
8463 ctl_clr_prkey(lun, residx);
8464 lun->pr_key_count--;
8466 if (residx == lun->pr_res_idx) {
8467 lun->flags &= ~CTL_LUN_PR_RESERVED;
8468 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8470 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8471 || lun->res_type == SPR_TYPE_EX_AC_RO)
8472 && lun->pr_key_count) {
8474 * If the reservation is a registrants
8475 * only type we need to generate a UA
8476 * for other registered inits. The
8477 * sense code should be RESERVATIONS
8481 for (i = softc->init_min; i < softc->init_max; i++){
8482 if (ctl_get_prkey(lun, i) == 0)
8485 CTL_UA_RES_RELEASE);
8489 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8490 if (lun->pr_key_count==0) {
8491 lun->flags &= ~CTL_LUN_PR_RESERVED;
8493 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8496 lun->PRGeneration++;
8497 mtx_unlock(&lun->lun_lock);
8499 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8500 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8501 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8502 persis_io.pr.pr_info.residx = residx;
8503 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8504 sizeof(persis_io.pr), M_WAITOK);
8505 } else /* sa_res_key != 0 */ {
8508 * If we aren't registered currently then increment
8509 * the key count and set the registered flag.
8511 ctl_alloc_prkey(lun, residx);
8512 if (ctl_get_prkey(lun, residx) == 0)
8513 lun->pr_key_count++;
8514 ctl_set_prkey(lun, residx, sa_res_key);
8515 lun->PRGeneration++;
8516 mtx_unlock(&lun->lun_lock);
8518 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8519 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8520 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8521 persis_io.pr.pr_info.residx = residx;
8522 memcpy(persis_io.pr.pr_info.sa_res_key,
8523 param->serv_act_res_key,
8524 sizeof(param->serv_act_res_key));
8525 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8526 sizeof(persis_io.pr), M_WAITOK);
8533 printf("Reserve executed type %d\n", type);
8535 mtx_lock(&lun->lun_lock);
8536 if (lun->flags & CTL_LUN_PR_RESERVED) {
8538 * if this isn't the reservation holder and it's
8539 * not a "all registrants" type or if the type is
8540 * different then we have a conflict
8542 if ((lun->pr_res_idx != residx
8543 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8544 || lun->res_type != type) {
8545 mtx_unlock(&lun->lun_lock);
8546 free(ctsio->kern_data_ptr, M_CTL);
8547 ctl_set_reservation_conflict(ctsio);
8548 ctl_done((union ctl_io *)ctsio);
8549 return (CTL_RETVAL_COMPLETE);
8551 mtx_unlock(&lun->lun_lock);
8552 } else /* create a reservation */ {
8554 * If it's not an "all registrants" type record
8555 * reservation holder
8557 if (type != SPR_TYPE_WR_EX_AR
8558 && type != SPR_TYPE_EX_AC_AR)
8559 lun->pr_res_idx = residx; /* Res holder */
8561 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8563 lun->flags |= CTL_LUN_PR_RESERVED;
8564 lun->res_type = type;
8566 mtx_unlock(&lun->lun_lock);
8568 /* send msg to other side */
8569 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8570 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8571 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8572 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8573 persis_io.pr.pr_info.res_type = type;
8574 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8575 sizeof(persis_io.pr), M_WAITOK);
8580 mtx_lock(&lun->lun_lock);
8581 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8582 /* No reservation exists return good status */
8583 mtx_unlock(&lun->lun_lock);
8587 * Is this nexus a reservation holder?
8589 if (lun->pr_res_idx != residx
8590 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8592 * not a res holder return good status but
8595 mtx_unlock(&lun->lun_lock);
8599 if (lun->res_type != type) {
8600 mtx_unlock(&lun->lun_lock);
8601 free(ctsio->kern_data_ptr, M_CTL);
8602 ctl_set_illegal_pr_release(ctsio);
8603 ctl_done((union ctl_io *)ctsio);
8604 return (CTL_RETVAL_COMPLETE);
8607 /* okay to release */
8608 lun->flags &= ~CTL_LUN_PR_RESERVED;
8609 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8613 * if this isn't an exclusive access
8614 * res generate UA for all other
8617 if (type != SPR_TYPE_EX_AC
8618 && type != SPR_TYPE_WR_EX) {
8619 for (i = softc->init_min; i < softc->init_max; i++) {
8620 if (i == residx || ctl_get_prkey(lun, i) == 0)
8622 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8625 mtx_unlock(&lun->lun_lock);
8627 /* Send msg to other side */
8628 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8629 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8630 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8631 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8632 sizeof(persis_io.pr), M_WAITOK);
8636 /* send msg to other side */
8638 mtx_lock(&lun->lun_lock);
8639 lun->flags &= ~CTL_LUN_PR_RESERVED;
8641 lun->pr_key_count = 0;
8642 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8644 ctl_clr_prkey(lun, residx);
8645 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8646 if (ctl_get_prkey(lun, i) != 0) {
8647 ctl_clr_prkey(lun, i);
8648 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8650 lun->PRGeneration++;
8651 mtx_unlock(&lun->lun_lock);
8653 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8654 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8655 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8656 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8657 sizeof(persis_io.pr), M_WAITOK);
8661 case SPRO_PRE_ABO: {
8664 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8665 residx, ctsio, cdb, param);
8667 return (CTL_RETVAL_COMPLETE);
8671 panic("Invalid PR type %x", cdb->action);
8675 free(ctsio->kern_data_ptr, M_CTL);
8676 ctl_set_success(ctsio);
8677 ctl_done((union ctl_io *)ctsio);
8683 * This routine is for handling a message from the other SC pertaining to
8684 * persistent reserve out. All the error checking will have been done
8685 * so only perorming the action need be done here to keep the two
8689 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8691 struct ctl_softc *softc = control_softc;
8692 struct ctl_lun *lun;
8694 uint32_t residx, targ_lun;
8696 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8697 mtx_lock(&softc->ctl_lock);
8698 if ((targ_lun >= CTL_MAX_LUNS) ||
8699 ((lun = softc->ctl_luns[targ_lun]) == NULL)) {
8700 mtx_unlock(&softc->ctl_lock);
8703 mtx_lock(&lun->lun_lock);
8704 mtx_unlock(&softc->ctl_lock);
8705 if (lun->flags & CTL_LUN_DISABLED) {
8706 mtx_unlock(&lun->lun_lock);
8709 residx = ctl_get_initindex(&msg->hdr.nexus);
8710 switch(msg->pr.pr_info.action) {
8711 case CTL_PR_REG_KEY:
8712 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8713 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8714 lun->pr_key_count++;
8715 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8716 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8717 lun->PRGeneration++;
8720 case CTL_PR_UNREG_KEY:
8721 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8722 lun->pr_key_count--;
8724 /* XXX Need to see if the reservation has been released */
8725 /* if so do we need to generate UA? */
8726 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8727 lun->flags &= ~CTL_LUN_PR_RESERVED;
8728 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8730 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8731 || lun->res_type == SPR_TYPE_EX_AC_RO)
8732 && lun->pr_key_count) {
8734 * If the reservation is a registrants
8735 * only type we need to generate a UA
8736 * for other registered inits. The
8737 * sense code should be RESERVATIONS
8741 for (i = softc->init_min; i < softc->init_max; i++) {
8742 if (ctl_get_prkey(lun, i) == 0)
8745 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8749 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8750 if (lun->pr_key_count==0) {
8751 lun->flags &= ~CTL_LUN_PR_RESERVED;
8753 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8756 lun->PRGeneration++;
8759 case CTL_PR_RESERVE:
8760 lun->flags |= CTL_LUN_PR_RESERVED;
8761 lun->res_type = msg->pr.pr_info.res_type;
8762 lun->pr_res_idx = msg->pr.pr_info.residx;
8766 case CTL_PR_RELEASE:
8768 * if this isn't an exclusive access res generate UA for all
8769 * other registrants.
8771 if (lun->res_type != SPR_TYPE_EX_AC
8772 && lun->res_type != SPR_TYPE_WR_EX) {
8773 for (i = softc->init_min; i < softc->init_max; i++)
8774 if (i == residx || ctl_get_prkey(lun, i) == 0)
8776 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8779 lun->flags &= ~CTL_LUN_PR_RESERVED;
8780 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8784 case CTL_PR_PREEMPT:
8785 ctl_pro_preempt_other(lun, msg);
8788 lun->flags &= ~CTL_LUN_PR_RESERVED;
8790 lun->pr_key_count = 0;
8791 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8793 for (i=0; i < CTL_MAX_INITIATORS; i++) {
8794 if (ctl_get_prkey(lun, i) == 0)
8796 ctl_clr_prkey(lun, i);
8797 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8799 lun->PRGeneration++;
8803 mtx_unlock(&lun->lun_lock);
8807 ctl_read_write(struct ctl_scsiio *ctsio)
8809 struct ctl_lun *lun;
8810 struct ctl_lba_len_flags *lbalen;
8812 uint32_t num_blocks;
8816 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8818 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8821 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8822 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8823 switch (ctsio->cdb[0]) {
8826 struct scsi_rw_6 *cdb;
8828 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8830 lba = scsi_3btoul(cdb->addr);
8831 /* only 5 bits are valid in the most significant address byte */
8833 num_blocks = cdb->length;
8835 * This is correct according to SBC-2.
8837 if (num_blocks == 0)
8843 struct scsi_rw_10 *cdb;
8845 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8846 if (cdb->byte2 & SRW10_FUA)
8847 flags |= CTL_LLF_FUA;
8848 if (cdb->byte2 & SRW10_DPO)
8849 flags |= CTL_LLF_DPO;
8850 lba = scsi_4btoul(cdb->addr);
8851 num_blocks = scsi_2btoul(cdb->length);
8854 case WRITE_VERIFY_10: {
8855 struct scsi_write_verify_10 *cdb;
8857 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8858 flags |= CTL_LLF_FUA;
8859 if (cdb->byte2 & SWV_DPO)
8860 flags |= CTL_LLF_DPO;
8861 lba = scsi_4btoul(cdb->addr);
8862 num_blocks = scsi_2btoul(cdb->length);
8867 struct scsi_rw_12 *cdb;
8869 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8870 if (cdb->byte2 & SRW12_FUA)
8871 flags |= CTL_LLF_FUA;
8872 if (cdb->byte2 & SRW12_DPO)
8873 flags |= CTL_LLF_DPO;
8874 lba = scsi_4btoul(cdb->addr);
8875 num_blocks = scsi_4btoul(cdb->length);
8878 case WRITE_VERIFY_12: {
8879 struct scsi_write_verify_12 *cdb;
8881 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8882 flags |= CTL_LLF_FUA;
8883 if (cdb->byte2 & SWV_DPO)
8884 flags |= CTL_LLF_DPO;
8885 lba = scsi_4btoul(cdb->addr);
8886 num_blocks = scsi_4btoul(cdb->length);
8891 struct scsi_rw_16 *cdb;
8893 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8894 if (cdb->byte2 & SRW12_FUA)
8895 flags |= CTL_LLF_FUA;
8896 if (cdb->byte2 & SRW12_DPO)
8897 flags |= CTL_LLF_DPO;
8898 lba = scsi_8btou64(cdb->addr);
8899 num_blocks = scsi_4btoul(cdb->length);
8902 case WRITE_ATOMIC_16: {
8903 struct scsi_write_atomic_16 *cdb;
8905 if (lun->be_lun->atomicblock == 0) {
8906 ctl_set_invalid_opcode(ctsio);
8907 ctl_done((union ctl_io *)ctsio);
8908 return (CTL_RETVAL_COMPLETE);
8911 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb;
8912 if (cdb->byte2 & SRW12_FUA)
8913 flags |= CTL_LLF_FUA;
8914 if (cdb->byte2 & SRW12_DPO)
8915 flags |= CTL_LLF_DPO;
8916 lba = scsi_8btou64(cdb->addr);
8917 num_blocks = scsi_2btoul(cdb->length);
8918 if (num_blocks > lun->be_lun->atomicblock) {
8919 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8920 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8922 ctl_done((union ctl_io *)ctsio);
8923 return (CTL_RETVAL_COMPLETE);
8927 case WRITE_VERIFY_16: {
8928 struct scsi_write_verify_16 *cdb;
8930 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8931 flags |= CTL_LLF_FUA;
8932 if (cdb->byte2 & SWV_DPO)
8933 flags |= CTL_LLF_DPO;
8934 lba = scsi_8btou64(cdb->addr);
8935 num_blocks = scsi_4btoul(cdb->length);
8940 * We got a command we don't support. This shouldn't
8941 * happen, commands should be filtered out above us.
8943 ctl_set_invalid_opcode(ctsio);
8944 ctl_done((union ctl_io *)ctsio);
8946 return (CTL_RETVAL_COMPLETE);
8947 break; /* NOTREACHED */
8951 * The first check is to make sure we're in bounds, the second
8952 * check is to catch wrap-around problems. If the lba + num blocks
8953 * is less than the lba, then we've wrapped around and the block
8954 * range is invalid anyway.
8956 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8957 || ((lba + num_blocks) < lba)) {
8958 ctl_set_lba_out_of_range(ctsio);
8959 ctl_done((union ctl_io *)ctsio);
8960 return (CTL_RETVAL_COMPLETE);
8964 * According to SBC-3, a transfer length of 0 is not an error.
8965 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8966 * translates to 256 blocks for those commands.
8968 if (num_blocks == 0) {
8969 ctl_set_success(ctsio);
8970 ctl_done((union ctl_io *)ctsio);
8971 return (CTL_RETVAL_COMPLETE);
8974 /* Set FUA and/or DPO if caches are disabled. */
8976 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8978 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8980 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8982 flags |= CTL_LLF_FUA;
8985 lbalen = (struct ctl_lba_len_flags *)
8986 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8988 lbalen->len = num_blocks;
8989 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8991 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8992 ctsio->kern_rel_offset = 0;
8994 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8996 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9001 ctl_cnw_cont(union ctl_io *io)
9003 struct ctl_scsiio *ctsio;
9004 struct ctl_lun *lun;
9005 struct ctl_lba_len_flags *lbalen;
9008 ctsio = &io->scsiio;
9009 ctsio->io_hdr.status = CTL_STATUS_NONE;
9010 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
9011 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9012 lbalen = (struct ctl_lba_len_flags *)
9013 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9014 lbalen->flags &= ~CTL_LLF_COMPARE;
9015 lbalen->flags |= CTL_LLF_WRITE;
9017 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
9018 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9023 ctl_cnw(struct ctl_scsiio *ctsio)
9025 struct ctl_lun *lun;
9026 struct ctl_lba_len_flags *lbalen;
9028 uint32_t num_blocks;
9031 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9033 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
9036 switch (ctsio->cdb[0]) {
9037 case COMPARE_AND_WRITE: {
9038 struct scsi_compare_and_write *cdb;
9040 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
9041 if (cdb->byte2 & SRW10_FUA)
9042 flags |= CTL_LLF_FUA;
9043 if (cdb->byte2 & SRW10_DPO)
9044 flags |= CTL_LLF_DPO;
9045 lba = scsi_8btou64(cdb->addr);
9046 num_blocks = cdb->length;
9051 * We got a command we don't support. This shouldn't
9052 * happen, commands should be filtered out above us.
9054 ctl_set_invalid_opcode(ctsio);
9055 ctl_done((union ctl_io *)ctsio);
9057 return (CTL_RETVAL_COMPLETE);
9058 break; /* NOTREACHED */
9062 * The first check is to make sure we're in bounds, the second
9063 * check is to catch wrap-around problems. If the lba + num blocks
9064 * is less than the lba, then we've wrapped around and the block
9065 * range is invalid anyway.
9067 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9068 || ((lba + num_blocks) < lba)) {
9069 ctl_set_lba_out_of_range(ctsio);
9070 ctl_done((union ctl_io *)ctsio);
9071 return (CTL_RETVAL_COMPLETE);
9075 * According to SBC-3, a transfer length of 0 is not an error.
9077 if (num_blocks == 0) {
9078 ctl_set_success(ctsio);
9079 ctl_done((union ctl_io *)ctsio);
9080 return (CTL_RETVAL_COMPLETE);
9083 /* Set FUA if write cache is disabled. */
9084 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9086 flags |= CTL_LLF_FUA;
9088 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
9089 ctsio->kern_rel_offset = 0;
9092 * Set the IO_CONT flag, so that if this I/O gets passed to
9093 * ctl_data_submit_done(), it'll get passed back to
9094 * ctl_ctl_cnw_cont() for further processing.
9096 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
9097 ctsio->io_cont = ctl_cnw_cont;
9099 lbalen = (struct ctl_lba_len_flags *)
9100 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9102 lbalen->len = num_blocks;
9103 lbalen->flags = CTL_LLF_COMPARE | flags;
9105 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
9106 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9111 ctl_verify(struct ctl_scsiio *ctsio)
9113 struct ctl_lun *lun;
9114 struct ctl_lba_len_flags *lbalen;
9116 uint32_t num_blocks;
9120 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9122 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
9125 flags = CTL_LLF_FUA;
9126 switch (ctsio->cdb[0]) {
9128 struct scsi_verify_10 *cdb;
9130 cdb = (struct scsi_verify_10 *)ctsio->cdb;
9131 if (cdb->byte2 & SVFY_BYTCHK)
9133 if (cdb->byte2 & SVFY_DPO)
9134 flags |= CTL_LLF_DPO;
9135 lba = scsi_4btoul(cdb->addr);
9136 num_blocks = scsi_2btoul(cdb->length);
9140 struct scsi_verify_12 *cdb;
9142 cdb = (struct scsi_verify_12 *)ctsio->cdb;
9143 if (cdb->byte2 & SVFY_BYTCHK)
9145 if (cdb->byte2 & SVFY_DPO)
9146 flags |= CTL_LLF_DPO;
9147 lba = scsi_4btoul(cdb->addr);
9148 num_blocks = scsi_4btoul(cdb->length);
9152 struct scsi_rw_16 *cdb;
9154 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9155 if (cdb->byte2 & SVFY_BYTCHK)
9157 if (cdb->byte2 & SVFY_DPO)
9158 flags |= CTL_LLF_DPO;
9159 lba = scsi_8btou64(cdb->addr);
9160 num_blocks = scsi_4btoul(cdb->length);
9165 * We got a command we don't support. This shouldn't
9166 * happen, commands should be filtered out above us.
9168 ctl_set_invalid_opcode(ctsio);
9169 ctl_done((union ctl_io *)ctsio);
9170 return (CTL_RETVAL_COMPLETE);
9174 * The first check is to make sure we're in bounds, the second
9175 * check is to catch wrap-around problems. If the lba + num blocks
9176 * is less than the lba, then we've wrapped around and the block
9177 * range is invalid anyway.
9179 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9180 || ((lba + num_blocks) < lba)) {
9181 ctl_set_lba_out_of_range(ctsio);
9182 ctl_done((union ctl_io *)ctsio);
9183 return (CTL_RETVAL_COMPLETE);
9187 * According to SBC-3, a transfer length of 0 is not an error.
9189 if (num_blocks == 0) {
9190 ctl_set_success(ctsio);
9191 ctl_done((union ctl_io *)ctsio);
9192 return (CTL_RETVAL_COMPLETE);
9195 lbalen = (struct ctl_lba_len_flags *)
9196 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9198 lbalen->len = num_blocks;
9200 lbalen->flags = CTL_LLF_COMPARE | flags;
9201 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9203 lbalen->flags = CTL_LLF_VERIFY | flags;
9204 ctsio->kern_total_len = 0;
9206 ctsio->kern_rel_offset = 0;
9208 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9209 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9214 ctl_report_luns(struct ctl_scsiio *ctsio)
9216 struct ctl_softc *softc;
9217 struct scsi_report_luns *cdb;
9218 struct scsi_report_luns_data *lun_data;
9219 struct ctl_lun *lun, *request_lun;
9220 struct ctl_port *port;
9221 int num_luns, retval;
9222 uint32_t alloc_len, lun_datalen;
9224 uint32_t initidx, targ_lun_id, lun_id;
9226 retval = CTL_RETVAL_COMPLETE;
9227 cdb = (struct scsi_report_luns *)ctsio->cdb;
9228 port = ctl_io_port(&ctsio->io_hdr);
9229 softc = port->ctl_softc;
9231 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9233 mtx_lock(&softc->ctl_lock);
9235 for (targ_lun_id = 0; targ_lun_id < CTL_MAX_LUNS; targ_lun_id++) {
9236 if (ctl_lun_map_from_port(port, targ_lun_id) < CTL_MAX_LUNS)
9239 mtx_unlock(&softc->ctl_lock);
9241 switch (cdb->select_report) {
9242 case RPL_REPORT_DEFAULT:
9243 case RPL_REPORT_ALL:
9244 case RPL_REPORT_NONSUBSID:
9246 case RPL_REPORT_WELLKNOWN:
9247 case RPL_REPORT_ADMIN:
9248 case RPL_REPORT_CONGLOM:
9252 ctl_set_invalid_field(ctsio,
9258 ctl_done((union ctl_io *)ctsio);
9260 break; /* NOTREACHED */
9263 alloc_len = scsi_4btoul(cdb->length);
9265 * The initiator has to allocate at least 16 bytes for this request,
9266 * so he can at least get the header and the first LUN. Otherwise
9267 * we reject the request (per SPC-3 rev 14, section 6.21).
9269 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9270 sizeof(struct scsi_report_luns_lundata))) {
9271 ctl_set_invalid_field(ctsio,
9277 ctl_done((union ctl_io *)ctsio);
9281 request_lun = (struct ctl_lun *)
9282 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9284 lun_datalen = sizeof(*lun_data) +
9285 (num_luns * sizeof(struct scsi_report_luns_lundata));
9287 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9288 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9289 ctsio->kern_sg_entries = 0;
9291 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9293 mtx_lock(&softc->ctl_lock);
9294 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9295 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9296 if (lun_id >= CTL_MAX_LUNS)
9298 lun = softc->ctl_luns[lun_id];
9302 if (targ_lun_id <= 0xff) {
9304 * Peripheral addressing method, bus number 0.
9306 lun_data->luns[num_filled].lundata[0] =
9307 RPL_LUNDATA_ATYP_PERIPH;
9308 lun_data->luns[num_filled].lundata[1] = targ_lun_id;
9310 } else if (targ_lun_id <= 0x3fff) {
9312 * Flat addressing method.
9314 lun_data->luns[num_filled].lundata[0] =
9315 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8);
9316 lun_data->luns[num_filled].lundata[1] =
9317 (targ_lun_id & 0xff);
9319 } else if (targ_lun_id <= 0xffffff) {
9321 * Extended flat addressing method.
9323 lun_data->luns[num_filled].lundata[0] =
9324 RPL_LUNDATA_ATYP_EXTLUN | 0x12;
9325 scsi_ulto3b(targ_lun_id,
9326 &lun_data->luns[num_filled].lundata[1]);
9329 printf("ctl_report_luns: bogus LUN number %jd, "
9330 "skipping\n", (intmax_t)targ_lun_id);
9333 * According to SPC-3, rev 14 section 6.21:
9335 * "The execution of a REPORT LUNS command to any valid and
9336 * installed logical unit shall clear the REPORTED LUNS DATA
9337 * HAS CHANGED unit attention condition for all logical
9338 * units of that target with respect to the requesting
9339 * initiator. A valid and installed logical unit is one
9340 * having a PERIPHERAL QUALIFIER of 000b in the standard
9341 * INQUIRY data (see 6.4.2)."
9343 * If request_lun is NULL, the LUN this report luns command
9344 * was issued to is either disabled or doesn't exist. In that
9345 * case, we shouldn't clear any pending lun change unit
9348 if (request_lun != NULL) {
9349 mtx_lock(&lun->lun_lock);
9350 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE);
9351 mtx_unlock(&lun->lun_lock);
9354 mtx_unlock(&softc->ctl_lock);
9357 * It's quite possible that we've returned fewer LUNs than we allocated
9358 * space for. Trim it.
9360 lun_datalen = sizeof(*lun_data) +
9361 (num_filled * sizeof(struct scsi_report_luns_lundata));
9363 if (lun_datalen < alloc_len) {
9364 ctsio->residual = alloc_len - lun_datalen;
9365 ctsio->kern_data_len = lun_datalen;
9366 ctsio->kern_total_len = lun_datalen;
9368 ctsio->residual = 0;
9369 ctsio->kern_data_len = alloc_len;
9370 ctsio->kern_total_len = alloc_len;
9372 ctsio->kern_data_resid = 0;
9373 ctsio->kern_rel_offset = 0;
9374 ctsio->kern_sg_entries = 0;
9377 * We set this to the actual data length, regardless of how much
9378 * space we actually have to return results. If the user looks at
9379 * this value, he'll know whether or not he allocated enough space
9380 * and reissue the command if necessary. We don't support well
9381 * known logical units, so if the user asks for that, return none.
9383 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9386 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9389 ctl_set_success(ctsio);
9390 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9391 ctsio->be_move_done = ctl_config_move_done;
9392 ctl_datamove((union ctl_io *)ctsio);
9397 ctl_request_sense(struct ctl_scsiio *ctsio)
9399 struct scsi_request_sense *cdb;
9400 struct scsi_sense_data *sense_ptr;
9401 struct ctl_softc *ctl_softc;
9402 struct ctl_lun *lun;
9405 scsi_sense_data_type sense_format;
9406 ctl_ua_type ua_type;
9408 cdb = (struct scsi_request_sense *)ctsio->cdb;
9410 ctl_softc = control_softc;
9411 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9413 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9416 * Determine which sense format the user wants.
9418 if (cdb->byte2 & SRS_DESC)
9419 sense_format = SSD_TYPE_DESC;
9421 sense_format = SSD_TYPE_FIXED;
9423 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9424 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9425 ctsio->kern_sg_entries = 0;
9428 * struct scsi_sense_data, which is currently set to 256 bytes, is
9429 * larger than the largest allowed value for the length field in the
9430 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9432 ctsio->residual = 0;
9433 ctsio->kern_data_len = cdb->length;
9434 ctsio->kern_total_len = cdb->length;
9436 ctsio->kern_data_resid = 0;
9437 ctsio->kern_rel_offset = 0;
9438 ctsio->kern_sg_entries = 0;
9441 * If we don't have a LUN, we don't have any pending sense.
9447 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9449 * Check for pending sense, and then for pending unit attentions.
9450 * Pending sense gets returned first, then pending unit attentions.
9452 mtx_lock(&lun->lun_lock);
9454 if (ctl_is_set(lun->have_ca, initidx)) {
9455 scsi_sense_data_type stored_format;
9458 * Check to see which sense format was used for the stored
9461 stored_format = scsi_sense_type(&lun->pending_sense[initidx]);
9464 * If the user requested a different sense format than the
9465 * one we stored, then we need to convert it to the other
9466 * format. If we're going from descriptor to fixed format
9467 * sense data, we may lose things in translation, depending
9468 * on what options were used.
9470 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9471 * for some reason we'll just copy it out as-is.
9473 if ((stored_format == SSD_TYPE_FIXED)
9474 && (sense_format == SSD_TYPE_DESC))
9475 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9476 &lun->pending_sense[initidx],
9477 (struct scsi_sense_data_desc *)sense_ptr);
9478 else if ((stored_format == SSD_TYPE_DESC)
9479 && (sense_format == SSD_TYPE_FIXED))
9480 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9481 &lun->pending_sense[initidx],
9482 (struct scsi_sense_data_fixed *)sense_ptr);
9484 memcpy(sense_ptr, &lun->pending_sense[initidx],
9485 MIN(sizeof(*sense_ptr),
9486 sizeof(lun->pending_sense[initidx])));
9488 ctl_clear_mask(lun->have_ca, initidx);
9493 ua_type = ctl_build_ua(lun, initidx, sense_ptr, sense_format);
9494 if (ua_type != CTL_UA_NONE)
9496 if (ua_type == CTL_UA_LUN_CHANGE) {
9497 mtx_unlock(&lun->lun_lock);
9498 mtx_lock(&ctl_softc->ctl_lock);
9499 ctl_clr_ua_allluns(ctl_softc, initidx, ua_type);
9500 mtx_unlock(&ctl_softc->ctl_lock);
9501 mtx_lock(&lun->lun_lock);
9505 mtx_unlock(&lun->lun_lock);
9508 * We already have a pending error, return it.
9510 if (have_error != 0) {
9512 * We report the SCSI status as OK, since the status of the
9513 * request sense command itself is OK.
9514 * We report 0 for the sense length, because we aren't doing
9515 * autosense in this case. We're reporting sense as
9518 ctl_set_success(ctsio);
9519 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9520 ctsio->be_move_done = ctl_config_move_done;
9521 ctl_datamove((union ctl_io *)ctsio);
9522 return (CTL_RETVAL_COMPLETE);
9528 * No sense information to report, so we report that everything is
9531 ctl_set_sense_data(sense_ptr,
9534 /*current_error*/ 1,
9535 /*sense_key*/ SSD_KEY_NO_SENSE,
9541 * We report 0 for the sense length, because we aren't doing
9542 * autosense in this case. We're reporting sense as parameter data.
9544 ctl_set_success(ctsio);
9545 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9546 ctsio->be_move_done = ctl_config_move_done;
9547 ctl_datamove((union ctl_io *)ctsio);
9548 return (CTL_RETVAL_COMPLETE);
9552 ctl_tur(struct ctl_scsiio *ctsio)
9555 CTL_DEBUG_PRINT(("ctl_tur\n"));
9557 ctl_set_success(ctsio);
9558 ctl_done((union ctl_io *)ctsio);
9560 return (CTL_RETVAL_COMPLETE);
9564 * SCSI VPD page 0x00, the Supported VPD Pages page.
9567 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9569 struct scsi_vpd_supported_pages *pages;
9571 struct ctl_lun *lun;
9574 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9576 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9577 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9578 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9579 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9580 ctsio->kern_sg_entries = 0;
9582 if (sup_page_size < alloc_len) {
9583 ctsio->residual = alloc_len - sup_page_size;
9584 ctsio->kern_data_len = sup_page_size;
9585 ctsio->kern_total_len = sup_page_size;
9587 ctsio->residual = 0;
9588 ctsio->kern_data_len = alloc_len;
9589 ctsio->kern_total_len = alloc_len;
9591 ctsio->kern_data_resid = 0;
9592 ctsio->kern_rel_offset = 0;
9593 ctsio->kern_sg_entries = 0;
9596 * The control device is always connected. The disk device, on the
9597 * other hand, may not be online all the time. Need to change this
9598 * to figure out whether the disk device is actually online or not.
9601 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9602 lun->be_lun->lun_type;
9604 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9607 /* Supported VPD pages */
9608 pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9610 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9611 /* Device Identification */
9612 pages->page_list[p++] = SVPD_DEVICE_ID;
9613 /* Extended INQUIRY Data */
9614 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9615 /* Mode Page Policy */
9616 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9618 pages->page_list[p++] = SVPD_SCSI_PORTS;
9619 /* Third-party Copy */
9620 pages->page_list[p++] = SVPD_SCSI_TPC;
9621 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9623 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9624 /* Block Device Characteristics */
9625 pages->page_list[p++] = SVPD_BDC;
9626 /* Logical Block Provisioning */
9627 pages->page_list[p++] = SVPD_LBP;
9631 ctl_set_success(ctsio);
9632 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9633 ctsio->be_move_done = ctl_config_move_done;
9634 ctl_datamove((union ctl_io *)ctsio);
9635 return (CTL_RETVAL_COMPLETE);
9639 * SCSI VPD page 0x80, the Unit Serial Number page.
9642 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9644 struct scsi_vpd_unit_serial_number *sn_ptr;
9645 struct ctl_lun *lun;
9648 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9650 data_len = 4 + CTL_SN_LEN;
9651 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9652 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9653 if (data_len < alloc_len) {
9654 ctsio->residual = alloc_len - data_len;
9655 ctsio->kern_data_len = data_len;
9656 ctsio->kern_total_len = data_len;
9658 ctsio->residual = 0;
9659 ctsio->kern_data_len = alloc_len;
9660 ctsio->kern_total_len = alloc_len;
9662 ctsio->kern_data_resid = 0;
9663 ctsio->kern_rel_offset = 0;
9664 ctsio->kern_sg_entries = 0;
9667 * The control device is always connected. The disk device, on the
9668 * other hand, may not be online all the time. Need to change this
9669 * to figure out whether the disk device is actually online or not.
9672 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9673 lun->be_lun->lun_type;
9675 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9677 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9678 sn_ptr->length = CTL_SN_LEN;
9680 * If we don't have a LUN, we just leave the serial number as
9684 strncpy((char *)sn_ptr->serial_num,
9685 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9687 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9689 ctl_set_success(ctsio);
9690 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9691 ctsio->be_move_done = ctl_config_move_done;
9692 ctl_datamove((union ctl_io *)ctsio);
9693 return (CTL_RETVAL_COMPLETE);
9698 * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9701 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9703 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9704 struct ctl_lun *lun;
9707 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9709 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9710 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9711 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9712 ctsio->kern_sg_entries = 0;
9714 if (data_len < alloc_len) {
9715 ctsio->residual = alloc_len - data_len;
9716 ctsio->kern_data_len = data_len;
9717 ctsio->kern_total_len = data_len;
9719 ctsio->residual = 0;
9720 ctsio->kern_data_len = alloc_len;
9721 ctsio->kern_total_len = alloc_len;
9723 ctsio->kern_data_resid = 0;
9724 ctsio->kern_rel_offset = 0;
9725 ctsio->kern_sg_entries = 0;
9728 * The control device is always connected. The disk device, on the
9729 * other hand, may not be online all the time.
9732 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9733 lun->be_lun->lun_type;
9735 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9736 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9737 scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9739 * We support head of queue, ordered and simple tags.
9741 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9743 * Volatile cache supported.
9745 eid_ptr->flags3 = SVPD_EID_V_SUP;
9748 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9749 * attention for a particular IT nexus on all LUNs once we report
9750 * it to that nexus once. This bit is required as of SPC-4.
9752 eid_ptr->flags4 = SVPD_EID_LUICLT;
9755 * XXX KDM in order to correctly answer this, we would need
9756 * information from the SIM to determine how much sense data it
9757 * can send. So this would really be a path inquiry field, most
9758 * likely. This can be set to a maximum of 252 according to SPC-4,
9759 * but the hardware may or may not be able to support that much.
9760 * 0 just means that the maximum sense data length is not reported.
9762 eid_ptr->max_sense_length = 0;
9764 ctl_set_success(ctsio);
9765 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9766 ctsio->be_move_done = ctl_config_move_done;
9767 ctl_datamove((union ctl_io *)ctsio);
9768 return (CTL_RETVAL_COMPLETE);
9772 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9774 struct scsi_vpd_mode_page_policy *mpp_ptr;
9775 struct ctl_lun *lun;
9778 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9780 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9781 sizeof(struct scsi_vpd_mode_page_policy_descr);
9783 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9784 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9785 ctsio->kern_sg_entries = 0;
9787 if (data_len < alloc_len) {
9788 ctsio->residual = alloc_len - data_len;
9789 ctsio->kern_data_len = data_len;
9790 ctsio->kern_total_len = data_len;
9792 ctsio->residual = 0;
9793 ctsio->kern_data_len = alloc_len;
9794 ctsio->kern_total_len = alloc_len;
9796 ctsio->kern_data_resid = 0;
9797 ctsio->kern_rel_offset = 0;
9798 ctsio->kern_sg_entries = 0;
9801 * The control device is always connected. The disk device, on the
9802 * other hand, may not be online all the time.
9805 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9806 lun->be_lun->lun_type;
9808 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9809 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9810 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9811 mpp_ptr->descr[0].page_code = 0x3f;
9812 mpp_ptr->descr[0].subpage_code = 0xff;
9813 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9815 ctl_set_success(ctsio);
9816 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9817 ctsio->be_move_done = ctl_config_move_done;
9818 ctl_datamove((union ctl_io *)ctsio);
9819 return (CTL_RETVAL_COMPLETE);
9823 * SCSI VPD page 0x83, the Device Identification page.
9826 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9828 struct scsi_vpd_device_id *devid_ptr;
9829 struct scsi_vpd_id_descriptor *desc;
9830 struct ctl_softc *softc;
9831 struct ctl_lun *lun;
9832 struct ctl_port *port;
9836 softc = control_softc;
9838 port = ctl_io_port(&ctsio->io_hdr);
9839 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9841 data_len = sizeof(struct scsi_vpd_device_id) +
9842 sizeof(struct scsi_vpd_id_descriptor) +
9843 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9844 sizeof(struct scsi_vpd_id_descriptor) +
9845 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9846 if (lun && lun->lun_devid)
9847 data_len += lun->lun_devid->len;
9848 if (port && port->port_devid)
9849 data_len += port->port_devid->len;
9850 if (port && port->target_devid)
9851 data_len += port->target_devid->len;
9853 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9854 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9855 ctsio->kern_sg_entries = 0;
9857 if (data_len < alloc_len) {
9858 ctsio->residual = alloc_len - data_len;
9859 ctsio->kern_data_len = data_len;
9860 ctsio->kern_total_len = data_len;
9862 ctsio->residual = 0;
9863 ctsio->kern_data_len = alloc_len;
9864 ctsio->kern_total_len = alloc_len;
9866 ctsio->kern_data_resid = 0;
9867 ctsio->kern_rel_offset = 0;
9868 ctsio->kern_sg_entries = 0;
9871 * The control device is always connected. The disk device, on the
9872 * other hand, may not be online all the time.
9875 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9876 lun->be_lun->lun_type;
9878 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9879 devid_ptr->page_code = SVPD_DEVICE_ID;
9880 scsi_ulto2b(data_len - 4, devid_ptr->length);
9882 if (port && port->port_type == CTL_PORT_FC)
9883 proto = SCSI_PROTO_FC << 4;
9884 else if (port && port->port_type == CTL_PORT_ISCSI)
9885 proto = SCSI_PROTO_ISCSI << 4;
9887 proto = SCSI_PROTO_SPI << 4;
9888 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9891 * We're using a LUN association here. i.e., this device ID is a
9892 * per-LUN identifier.
9894 if (lun && lun->lun_devid) {
9895 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9896 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9897 lun->lun_devid->len);
9901 * This is for the WWPN which is a port association.
9903 if (port && port->port_devid) {
9904 memcpy(desc, port->port_devid->data, port->port_devid->len);
9905 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9906 port->port_devid->len);
9910 * This is for the Relative Target Port(type 4h) identifier
9912 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9913 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9914 SVPD_ID_TYPE_RELTARG;
9916 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9917 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9918 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9921 * This is for the Target Port Group(type 5h) identifier
9923 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9924 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9925 SVPD_ID_TYPE_TPORTGRP;
9927 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / softc->port_cnt + 1,
9928 &desc->identifier[2]);
9929 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9930 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9933 * This is for the Target identifier
9935 if (port && port->target_devid) {
9936 memcpy(desc, port->target_devid->data, port->target_devid->len);
9939 ctl_set_success(ctsio);
9940 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9941 ctsio->be_move_done = ctl_config_move_done;
9942 ctl_datamove((union ctl_io *)ctsio);
9943 return (CTL_RETVAL_COMPLETE);
9947 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9949 struct ctl_softc *softc = control_softc;
9950 struct scsi_vpd_scsi_ports *sp;
9951 struct scsi_vpd_port_designation *pd;
9952 struct scsi_vpd_port_designation_cont *pdc;
9953 struct ctl_lun *lun;
9954 struct ctl_port *port;
9955 int data_len, num_target_ports, iid_len, id_len;
9957 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9959 num_target_ports = 0;
9962 mtx_lock(&softc->ctl_lock);
9963 STAILQ_FOREACH(port, &softc->port_list, links) {
9964 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9967 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
9970 if (port->init_devid)
9971 iid_len += port->init_devid->len;
9972 if (port->port_devid)
9973 id_len += port->port_devid->len;
9975 mtx_unlock(&softc->ctl_lock);
9977 data_len = sizeof(struct scsi_vpd_scsi_ports) +
9978 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9979 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9980 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9981 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9982 ctsio->kern_sg_entries = 0;
9984 if (data_len < alloc_len) {
9985 ctsio->residual = alloc_len - data_len;
9986 ctsio->kern_data_len = data_len;
9987 ctsio->kern_total_len = data_len;
9989 ctsio->residual = 0;
9990 ctsio->kern_data_len = alloc_len;
9991 ctsio->kern_total_len = alloc_len;
9993 ctsio->kern_data_resid = 0;
9994 ctsio->kern_rel_offset = 0;
9995 ctsio->kern_sg_entries = 0;
9998 * The control device is always connected. The disk device, on the
9999 * other hand, may not be online all the time. Need to change this
10000 * to figure out whether the disk device is actually online or not.
10003 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
10004 lun->be_lun->lun_type;
10006 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10008 sp->page_code = SVPD_SCSI_PORTS;
10009 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
10011 pd = &sp->design[0];
10013 mtx_lock(&softc->ctl_lock);
10014 STAILQ_FOREACH(port, &softc->port_list, links) {
10015 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
10018 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
10020 scsi_ulto2b(port->targ_port, pd->relative_port_id);
10021 if (port->init_devid) {
10022 iid_len = port->init_devid->len;
10023 memcpy(pd->initiator_transportid,
10024 port->init_devid->data, port->init_devid->len);
10027 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
10028 pdc = (struct scsi_vpd_port_designation_cont *)
10029 (&pd->initiator_transportid[iid_len]);
10030 if (port->port_devid) {
10031 id_len = port->port_devid->len;
10032 memcpy(pdc->target_port_descriptors,
10033 port->port_devid->data, port->port_devid->len);
10036 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
10037 pd = (struct scsi_vpd_port_designation *)
10038 ((uint8_t *)pdc->target_port_descriptors + id_len);
10040 mtx_unlock(&softc->ctl_lock);
10042 ctl_set_success(ctsio);
10043 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10044 ctsio->be_move_done = ctl_config_move_done;
10045 ctl_datamove((union ctl_io *)ctsio);
10046 return (CTL_RETVAL_COMPLETE);
10050 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
10052 struct scsi_vpd_block_limits *bl_ptr;
10053 struct ctl_lun *lun;
10055 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10057 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
10058 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
10059 ctsio->kern_sg_entries = 0;
10061 if (sizeof(*bl_ptr) < alloc_len) {
10062 ctsio->residual = alloc_len - sizeof(*bl_ptr);
10063 ctsio->kern_data_len = sizeof(*bl_ptr);
10064 ctsio->kern_total_len = sizeof(*bl_ptr);
10066 ctsio->residual = 0;
10067 ctsio->kern_data_len = alloc_len;
10068 ctsio->kern_total_len = alloc_len;
10070 ctsio->kern_data_resid = 0;
10071 ctsio->kern_rel_offset = 0;
10072 ctsio->kern_sg_entries = 0;
10075 * The control device is always connected. The disk device, on the
10076 * other hand, may not be online all the time. Need to change this
10077 * to figure out whether the disk device is actually online or not.
10080 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10081 lun->be_lun->lun_type;
10083 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10085 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
10086 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
10087 bl_ptr->max_cmp_write_len = 0xff;
10088 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
10090 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
10091 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10092 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
10093 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
10094 if (lun->be_lun->ublockexp != 0) {
10095 scsi_ulto4b((1 << lun->be_lun->ublockexp),
10096 bl_ptr->opt_unmap_grain);
10097 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
10098 bl_ptr->unmap_grain_align);
10101 scsi_ulto4b(lun->be_lun->atomicblock,
10102 bl_ptr->max_atomic_transfer_length);
10103 scsi_ulto4b(0, bl_ptr->atomic_alignment);
10104 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
10105 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary);
10106 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size);
10108 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
10110 ctl_set_success(ctsio);
10111 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10112 ctsio->be_move_done = ctl_config_move_done;
10113 ctl_datamove((union ctl_io *)ctsio);
10114 return (CTL_RETVAL_COMPLETE);
10118 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
10120 struct scsi_vpd_block_device_characteristics *bdc_ptr;
10121 struct ctl_lun *lun;
10125 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10127 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
10128 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
10129 ctsio->kern_sg_entries = 0;
10131 if (sizeof(*bdc_ptr) < alloc_len) {
10132 ctsio->residual = alloc_len - sizeof(*bdc_ptr);
10133 ctsio->kern_data_len = sizeof(*bdc_ptr);
10134 ctsio->kern_total_len = sizeof(*bdc_ptr);
10136 ctsio->residual = 0;
10137 ctsio->kern_data_len = alloc_len;
10138 ctsio->kern_total_len = alloc_len;
10140 ctsio->kern_data_resid = 0;
10141 ctsio->kern_rel_offset = 0;
10142 ctsio->kern_sg_entries = 0;
10145 * The control device is always connected. The disk device, on the
10146 * other hand, may not be online all the time. Need to change this
10147 * to figure out whether the disk device is actually online or not.
10150 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10151 lun->be_lun->lun_type;
10153 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10154 bdc_ptr->page_code = SVPD_BDC;
10155 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
10157 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL)
10158 i = strtol(value, NULL, 0);
10160 i = CTL_DEFAULT_ROTATION_RATE;
10161 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
10163 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL)
10164 i = strtol(value, NULL, 0);
10167 bdc_ptr->wab_wac_ff = (i & 0x0f);
10168 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
10170 ctl_set_success(ctsio);
10171 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10172 ctsio->be_move_done = ctl_config_move_done;
10173 ctl_datamove((union ctl_io *)ctsio);
10174 return (CTL_RETVAL_COMPLETE);
10178 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
10180 struct scsi_vpd_logical_block_prov *lbp_ptr;
10181 struct ctl_lun *lun;
10183 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10185 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
10186 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
10187 ctsio->kern_sg_entries = 0;
10189 if (sizeof(*lbp_ptr) < alloc_len) {
10190 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
10191 ctsio->kern_data_len = sizeof(*lbp_ptr);
10192 ctsio->kern_total_len = sizeof(*lbp_ptr);
10194 ctsio->residual = 0;
10195 ctsio->kern_data_len = alloc_len;
10196 ctsio->kern_total_len = alloc_len;
10198 ctsio->kern_data_resid = 0;
10199 ctsio->kern_rel_offset = 0;
10200 ctsio->kern_sg_entries = 0;
10203 * The control device is always connected. The disk device, on the
10204 * other hand, may not be online all the time. Need to change this
10205 * to figure out whether the disk device is actually online or not.
10208 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10209 lun->be_lun->lun_type;
10211 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10213 lbp_ptr->page_code = SVPD_LBP;
10214 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
10215 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
10216 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10217 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
10218 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
10219 lbp_ptr->prov_type = SVPD_LBP_THIN;
10222 ctl_set_success(ctsio);
10223 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10224 ctsio->be_move_done = ctl_config_move_done;
10225 ctl_datamove((union ctl_io *)ctsio);
10226 return (CTL_RETVAL_COMPLETE);
10230 * INQUIRY with the EVPD bit set.
10233 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
10235 struct ctl_lun *lun;
10236 struct scsi_inquiry *cdb;
10237 int alloc_len, retval;
10239 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10240 cdb = (struct scsi_inquiry *)ctsio->cdb;
10241 alloc_len = scsi_2btoul(cdb->length);
10243 switch (cdb->page_code) {
10244 case SVPD_SUPPORTED_PAGES:
10245 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
10247 case SVPD_UNIT_SERIAL_NUMBER:
10248 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
10250 case SVPD_DEVICE_ID:
10251 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
10253 case SVPD_EXTENDED_INQUIRY_DATA:
10254 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
10256 case SVPD_MODE_PAGE_POLICY:
10257 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
10259 case SVPD_SCSI_PORTS:
10260 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10262 case SVPD_SCSI_TPC:
10263 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10265 case SVPD_BLOCK_LIMITS:
10266 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10268 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10271 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10273 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10276 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10278 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10282 ctl_set_invalid_field(ctsio,
10288 ctl_done((union ctl_io *)ctsio);
10289 retval = CTL_RETVAL_COMPLETE;
10297 * Standard INQUIRY data.
10300 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10302 struct scsi_inquiry_data *inq_ptr;
10303 struct scsi_inquiry *cdb;
10304 struct ctl_softc *softc = control_softc;
10305 struct ctl_port *port;
10306 struct ctl_lun *lun;
10308 uint32_t alloc_len, data_len;
10309 ctl_port_type port_type;
10311 port = ctl_io_port(&ctsio->io_hdr);
10312 port_type = port->port_type;
10313 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10314 port_type = CTL_PORT_SCSI;
10316 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10317 cdb = (struct scsi_inquiry *)ctsio->cdb;
10318 alloc_len = scsi_2btoul(cdb->length);
10321 * We malloc the full inquiry data size here and fill it
10322 * in. If the user only asks for less, we'll give him
10325 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10326 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10327 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10328 ctsio->kern_sg_entries = 0;
10329 ctsio->kern_data_resid = 0;
10330 ctsio->kern_rel_offset = 0;
10332 if (data_len < alloc_len) {
10333 ctsio->residual = alloc_len - data_len;
10334 ctsio->kern_data_len = data_len;
10335 ctsio->kern_total_len = data_len;
10337 ctsio->residual = 0;
10338 ctsio->kern_data_len = alloc_len;
10339 ctsio->kern_total_len = alloc_len;
10343 if ((lun->flags & CTL_LUN_PRIMARY_SC) ||
10344 softc->ha_link >= CTL_HA_LINK_UNKNOWN) {
10345 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10346 lun->be_lun->lun_type;
10348 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) |
10349 lun->be_lun->lun_type;
10352 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10354 /* RMB in byte 2 is 0 */
10355 inq_ptr->version = SCSI_REV_SPC4;
10358 * According to SAM-3, even if a device only supports a single
10359 * level of LUN addressing, it should still set the HISUP bit:
10361 * 4.9.1 Logical unit numbers overview
10363 * All logical unit number formats described in this standard are
10364 * hierarchical in structure even when only a single level in that
10365 * hierarchy is used. The HISUP bit shall be set to one in the
10366 * standard INQUIRY data (see SPC-2) when any logical unit number
10367 * format described in this standard is used. Non-hierarchical
10368 * formats are outside the scope of this standard.
10370 * Therefore we set the HiSup bit here.
10372 * The reponse format is 2, per SPC-3.
10374 inq_ptr->response_format = SID_HiSup | 2;
10376 inq_ptr->additional_length = data_len -
10377 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10378 CTL_DEBUG_PRINT(("additional_length = %d\n",
10379 inq_ptr->additional_length));
10381 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10382 /* 16 bit addressing */
10383 if (port_type == CTL_PORT_SCSI)
10384 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10385 /* XXX set the SID_MultiP bit here if we're actually going to
10386 respond on multiple ports */
10387 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10389 /* 16 bit data bus, synchronous transfers */
10390 if (port_type == CTL_PORT_SCSI)
10391 inq_ptr->flags = SID_WBus16 | SID_Sync;
10393 * XXX KDM do we want to support tagged queueing on the control
10397 || (lun->be_lun->lun_type != T_PROCESSOR))
10398 inq_ptr->flags |= SID_CmdQue;
10400 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10401 * We have 8 bytes for the vendor name, and 16 bytes for the device
10402 * name and 4 bytes for the revision.
10404 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10405 "vendor")) == NULL) {
10406 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10408 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10409 strncpy(inq_ptr->vendor, val,
10410 min(sizeof(inq_ptr->vendor), strlen(val)));
10413 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10414 sizeof(inq_ptr->product));
10415 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10416 switch (lun->be_lun->lun_type) {
10418 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10419 sizeof(inq_ptr->product));
10422 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10423 sizeof(inq_ptr->product));
10426 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10427 sizeof(inq_ptr->product));
10431 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10432 strncpy(inq_ptr->product, val,
10433 min(sizeof(inq_ptr->product), strlen(val)));
10437 * XXX make this a macro somewhere so it automatically gets
10438 * incremented when we make changes.
10440 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10441 "revision")) == NULL) {
10442 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10444 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10445 strncpy(inq_ptr->revision, val,
10446 min(sizeof(inq_ptr->revision), strlen(val)));
10450 * For parallel SCSI, we support double transition and single
10451 * transition clocking. We also support QAS (Quick Arbitration
10452 * and Selection) and Information Unit transfers on both the
10453 * control and array devices.
10455 if (port_type == CTL_PORT_SCSI)
10456 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10459 /* SAM-5 (no version claimed) */
10460 scsi_ulto2b(0x00A0, inq_ptr->version1);
10461 /* SPC-4 (no version claimed) */
10462 scsi_ulto2b(0x0460, inq_ptr->version2);
10463 if (port_type == CTL_PORT_FC) {
10464 /* FCP-2 ANSI INCITS.350:2003 */
10465 scsi_ulto2b(0x0917, inq_ptr->version3);
10466 } else if (port_type == CTL_PORT_SCSI) {
10467 /* SPI-4 ANSI INCITS.362:200x */
10468 scsi_ulto2b(0x0B56, inq_ptr->version3);
10469 } else if (port_type == CTL_PORT_ISCSI) {
10470 /* iSCSI (no version claimed) */
10471 scsi_ulto2b(0x0960, inq_ptr->version3);
10472 } else if (port_type == CTL_PORT_SAS) {
10473 /* SAS (no version claimed) */
10474 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10478 /* SBC-4 (no version claimed) */
10479 scsi_ulto2b(0x0600, inq_ptr->version4);
10481 switch (lun->be_lun->lun_type) {
10483 /* SBC-4 (no version claimed) */
10484 scsi_ulto2b(0x0600, inq_ptr->version4);
10492 ctl_set_success(ctsio);
10493 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10494 ctsio->be_move_done = ctl_config_move_done;
10495 ctl_datamove((union ctl_io *)ctsio);
10496 return (CTL_RETVAL_COMPLETE);
10500 ctl_inquiry(struct ctl_scsiio *ctsio)
10502 struct scsi_inquiry *cdb;
10505 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10507 cdb = (struct scsi_inquiry *)ctsio->cdb;
10508 if (cdb->byte2 & SI_EVPD)
10509 retval = ctl_inquiry_evpd(ctsio);
10510 else if (cdb->page_code == 0)
10511 retval = ctl_inquiry_std(ctsio);
10513 ctl_set_invalid_field(ctsio,
10519 ctl_done((union ctl_io *)ctsio);
10520 return (CTL_RETVAL_COMPLETE);
10527 * For known CDB types, parse the LBA and length.
10530 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10532 if (io->io_hdr.io_type != CTL_IO_SCSI)
10535 switch (io->scsiio.cdb[0]) {
10536 case COMPARE_AND_WRITE: {
10537 struct scsi_compare_and_write *cdb;
10539 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10541 *lba = scsi_8btou64(cdb->addr);
10542 *len = cdb->length;
10547 struct scsi_rw_6 *cdb;
10549 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10551 *lba = scsi_3btoul(cdb->addr);
10552 /* only 5 bits are valid in the most significant address byte */
10554 *len = cdb->length;
10559 struct scsi_rw_10 *cdb;
10561 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10563 *lba = scsi_4btoul(cdb->addr);
10564 *len = scsi_2btoul(cdb->length);
10567 case WRITE_VERIFY_10: {
10568 struct scsi_write_verify_10 *cdb;
10570 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10572 *lba = scsi_4btoul(cdb->addr);
10573 *len = scsi_2btoul(cdb->length);
10578 struct scsi_rw_12 *cdb;
10580 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10582 *lba = scsi_4btoul(cdb->addr);
10583 *len = scsi_4btoul(cdb->length);
10586 case WRITE_VERIFY_12: {
10587 struct scsi_write_verify_12 *cdb;
10589 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10591 *lba = scsi_4btoul(cdb->addr);
10592 *len = scsi_4btoul(cdb->length);
10597 struct scsi_rw_16 *cdb;
10599 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10601 *lba = scsi_8btou64(cdb->addr);
10602 *len = scsi_4btoul(cdb->length);
10605 case WRITE_ATOMIC_16: {
10606 struct scsi_write_atomic_16 *cdb;
10608 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb;
10610 *lba = scsi_8btou64(cdb->addr);
10611 *len = scsi_2btoul(cdb->length);
10614 case WRITE_VERIFY_16: {
10615 struct scsi_write_verify_16 *cdb;
10617 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10619 *lba = scsi_8btou64(cdb->addr);
10620 *len = scsi_4btoul(cdb->length);
10623 case WRITE_SAME_10: {
10624 struct scsi_write_same_10 *cdb;
10626 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10628 *lba = scsi_4btoul(cdb->addr);
10629 *len = scsi_2btoul(cdb->length);
10632 case WRITE_SAME_16: {
10633 struct scsi_write_same_16 *cdb;
10635 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10637 *lba = scsi_8btou64(cdb->addr);
10638 *len = scsi_4btoul(cdb->length);
10642 struct scsi_verify_10 *cdb;
10644 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10646 *lba = scsi_4btoul(cdb->addr);
10647 *len = scsi_2btoul(cdb->length);
10651 struct scsi_verify_12 *cdb;
10653 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10655 *lba = scsi_4btoul(cdb->addr);
10656 *len = scsi_4btoul(cdb->length);
10660 struct scsi_verify_16 *cdb;
10662 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10664 *lba = scsi_8btou64(cdb->addr);
10665 *len = scsi_4btoul(cdb->length);
10673 case SERVICE_ACTION_IN: { /* GET LBA STATUS */
10674 struct scsi_get_lba_status *cdb;
10676 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10677 *lba = scsi_8btou64(cdb->addr);
10683 break; /* NOTREACHED */
10690 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10693 uint64_t endlba1, endlba2;
10695 endlba1 = lba1 + len1 - (seq ? 0 : 1);
10696 endlba2 = lba2 + len2 - 1;
10698 if ((endlba1 < lba2) || (endlba2 < lba1))
10699 return (CTL_ACTION_PASS);
10701 return (CTL_ACTION_BLOCK);
10705 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10707 struct ctl_ptr_len_flags *ptrlen;
10708 struct scsi_unmap_desc *buf, *end, *range;
10712 /* If not UNMAP -- go other way. */
10713 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10714 io->scsiio.cdb[0] != UNMAP)
10715 return (CTL_ACTION_ERROR);
10717 /* If UNMAP without data -- block and wait for data. */
10718 ptrlen = (struct ctl_ptr_len_flags *)
10719 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10720 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10721 ptrlen->ptr == NULL)
10722 return (CTL_ACTION_BLOCK);
10724 /* UNMAP with data -- check for collision. */
10725 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10726 end = buf + ptrlen->len / sizeof(*buf);
10727 for (range = buf; range < end; range++) {
10728 lba = scsi_8btou64(range->lba);
10729 len = scsi_4btoul(range->length);
10730 if ((lba < lba2 + len2) && (lba + len > lba2))
10731 return (CTL_ACTION_BLOCK);
10733 return (CTL_ACTION_PASS);
10737 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10739 uint64_t lba1, lba2;
10740 uint64_t len1, len2;
10743 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10744 return (CTL_ACTION_ERROR);
10746 retval = ctl_extent_check_unmap(io1, lba2, len2);
10747 if (retval != CTL_ACTION_ERROR)
10750 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10751 return (CTL_ACTION_ERROR);
10753 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10755 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
10759 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2)
10761 uint64_t lba1, lba2;
10762 uint64_t len1, len2;
10764 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10765 return (CTL_ACTION_PASS);
10766 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10767 return (CTL_ACTION_ERROR);
10768 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10769 return (CTL_ACTION_ERROR);
10771 if (lba1 + len1 == lba2)
10772 return (CTL_ACTION_BLOCK);
10773 return (CTL_ACTION_PASS);
10777 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10778 union ctl_io *ooa_io)
10780 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10781 const ctl_serialize_action *serialize_row;
10784 * The initiator attempted multiple untagged commands at the same
10785 * time. Can't do that.
10787 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10788 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10789 && ((pending_io->io_hdr.nexus.targ_port ==
10790 ooa_io->io_hdr.nexus.targ_port)
10791 && (pending_io->io_hdr.nexus.initid ==
10792 ooa_io->io_hdr.nexus.initid))
10793 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10794 CTL_FLAG_STATUS_SENT)) == 0))
10795 return (CTL_ACTION_OVERLAP);
10798 * The initiator attempted to send multiple tagged commands with
10799 * the same ID. (It's fine if different initiators have the same
10802 * Even if all of those conditions are true, we don't kill the I/O
10803 * if the command ahead of us has been aborted. We won't end up
10804 * sending it to the FETD, and it's perfectly legal to resend a
10805 * command with the same tag number as long as the previous
10806 * instance of this tag number has been aborted somehow.
10808 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10809 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10810 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10811 && ((pending_io->io_hdr.nexus.targ_port ==
10812 ooa_io->io_hdr.nexus.targ_port)
10813 && (pending_io->io_hdr.nexus.initid ==
10814 ooa_io->io_hdr.nexus.initid))
10815 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10816 CTL_FLAG_STATUS_SENT)) == 0))
10817 return (CTL_ACTION_OVERLAP_TAG);
10820 * If we get a head of queue tag, SAM-3 says that we should
10821 * immediately execute it.
10823 * What happens if this command would normally block for some other
10824 * reason? e.g. a request sense with a head of queue tag
10825 * immediately after a write. Normally that would block, but this
10826 * will result in its getting executed immediately...
10828 * We currently return "pass" instead of "skip", so we'll end up
10829 * going through the rest of the queue to check for overlapped tags.
10831 * XXX KDM check for other types of blockage first??
10833 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10834 return (CTL_ACTION_PASS);
10837 * Ordered tags have to block until all items ahead of them
10838 * have completed. If we get called with an ordered tag, we always
10839 * block, if something else is ahead of us in the queue.
10841 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10842 return (CTL_ACTION_BLOCK);
10845 * Simple tags get blocked until all head of queue and ordered tags
10846 * ahead of them have completed. I'm lumping untagged commands in
10847 * with simple tags here. XXX KDM is that the right thing to do?
10849 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10850 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10851 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10852 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10853 return (CTL_ACTION_BLOCK);
10855 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
10856 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
10858 serialize_row = ctl_serialize_table[ooa_entry->seridx];
10860 switch (serialize_row[pending_entry->seridx]) {
10861 case CTL_SER_BLOCK:
10862 return (CTL_ACTION_BLOCK);
10863 case CTL_SER_EXTENT:
10864 return (ctl_extent_check(ooa_io, pending_io,
10865 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10866 case CTL_SER_EXTENTOPT:
10867 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
10868 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
10869 return (ctl_extent_check(ooa_io, pending_io,
10871 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10872 return (CTL_ACTION_PASS);
10873 case CTL_SER_EXTENTSEQ:
10874 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF)
10875 return (ctl_extent_check_seq(ooa_io, pending_io));
10876 return (CTL_ACTION_PASS);
10878 return (CTL_ACTION_PASS);
10879 case CTL_SER_BLOCKOPT:
10880 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
10881 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
10882 return (CTL_ACTION_BLOCK);
10883 return (CTL_ACTION_PASS);
10885 return (CTL_ACTION_SKIP);
10887 panic("invalid serialization value %d",
10888 serialize_row[pending_entry->seridx]);
10891 return (CTL_ACTION_ERROR);
10895 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
10897 * - pending_io is generally either incoming, or on the blocked queue
10898 * - starting I/O is the I/O we want to start the check with.
10901 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
10902 union ctl_io *starting_io)
10904 union ctl_io *ooa_io;
10907 mtx_assert(&lun->lun_lock, MA_OWNED);
10910 * Run back along the OOA queue, starting with the current
10911 * blocked I/O and going through every I/O before it on the
10912 * queue. If starting_io is NULL, we'll just end up returning
10915 for (ooa_io = starting_io; ooa_io != NULL;
10916 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
10920 * This routine just checks to see whether
10921 * cur_blocked is blocked by ooa_io, which is ahead
10922 * of it in the queue. It doesn't queue/dequeue
10925 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
10927 case CTL_ACTION_BLOCK:
10928 case CTL_ACTION_OVERLAP:
10929 case CTL_ACTION_OVERLAP_TAG:
10930 case CTL_ACTION_SKIP:
10931 case CTL_ACTION_ERROR:
10933 break; /* NOTREACHED */
10934 case CTL_ACTION_PASS:
10937 panic("invalid action %d", action);
10938 break; /* NOTREACHED */
10942 return (CTL_ACTION_PASS);
10947 * - An I/O has just completed, and has been removed from the per-LUN OOA
10948 * queue, so some items on the blocked queue may now be unblocked.
10951 ctl_check_blocked(struct ctl_lun *lun)
10953 struct ctl_softc *softc = lun->ctl_softc;
10954 union ctl_io *cur_blocked, *next_blocked;
10956 mtx_assert(&lun->lun_lock, MA_OWNED);
10959 * Run forward from the head of the blocked queue, checking each
10960 * entry against the I/Os prior to it on the OOA queue to see if
10961 * there is still any blockage.
10963 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
10964 * with our removing a variable on it while it is traversing the
10967 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
10968 cur_blocked != NULL; cur_blocked = next_blocked) {
10969 union ctl_io *prev_ooa;
10972 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
10975 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
10976 ctl_ooaq, ooa_links);
10979 * If cur_blocked happens to be the first item in the OOA
10980 * queue now, prev_ooa will be NULL, and the action
10981 * returned will just be CTL_ACTION_PASS.
10983 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
10986 case CTL_ACTION_BLOCK:
10987 /* Nothing to do here, still blocked */
10989 case CTL_ACTION_OVERLAP:
10990 case CTL_ACTION_OVERLAP_TAG:
10992 * This shouldn't happen! In theory we've already
10993 * checked this command for overlap...
10996 case CTL_ACTION_PASS:
10997 case CTL_ACTION_SKIP: {
10998 const struct ctl_cmd_entry *entry;
11001 * The skip case shouldn't happen, this transaction
11002 * should have never made it onto the blocked queue.
11005 * This I/O is no longer blocked, we can remove it
11006 * from the blocked queue. Since this is a TAILQ
11007 * (doubly linked list), we can do O(1) removals
11008 * from any place on the list.
11010 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
11012 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11014 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
11015 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){
11017 * Need to send IO back to original side to
11020 union ctl_ha_msg msg_info;
11022 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11023 msg_info.hdr.original_sc =
11024 cur_blocked->io_hdr.original_sc;
11025 msg_info.hdr.serializing_sc = cur_blocked;
11026 msg_info.hdr.msg_type = CTL_MSG_R2R;
11027 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11028 sizeof(msg_info.hdr), M_NOWAIT);
11031 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
11034 * Check this I/O for LUN state changes that may
11035 * have happened while this command was blocked.
11036 * The LUN state may have been changed by a command
11037 * ahead of us in the queue, so we need to re-check
11038 * for any states that can be caused by SCSI
11041 if (ctl_scsiio_lun_check(lun, entry,
11042 &cur_blocked->scsiio) == 0) {
11043 cur_blocked->io_hdr.flags |=
11044 CTL_FLAG_IS_WAS_ON_RTR;
11045 ctl_enqueue_rtr(cur_blocked);
11047 ctl_done(cur_blocked);
11052 * This probably shouldn't happen -- we shouldn't
11053 * get CTL_ACTION_ERROR, or anything else.
11059 return (CTL_RETVAL_COMPLETE);
11063 * This routine (with one exception) checks LUN flags that can be set by
11064 * commands ahead of us in the OOA queue. These flags have to be checked
11065 * when a command initially comes in, and when we pull a command off the
11066 * blocked queue and are preparing to execute it. The reason we have to
11067 * check these flags for commands on the blocked queue is that the LUN
11068 * state may have been changed by a command ahead of us while we're on the
11071 * Ordering is somewhat important with these checks, so please pay
11072 * careful attention to the placement of any new checks.
11075 ctl_scsiio_lun_check(struct ctl_lun *lun,
11076 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11078 struct ctl_softc *softc = lun->ctl_softc;
11084 mtx_assert(&lun->lun_lock, MA_OWNED);
11087 * If this shelf is a secondary shelf controller, we may have to
11088 * reject some commands disallowed by HA mode and link state.
11090 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11091 if (softc->ha_link == CTL_HA_LINK_OFFLINE &&
11092 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11093 ctl_set_lun_unavail(ctsio);
11097 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 &&
11098 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11099 ctl_set_lun_transit(ctsio);
11103 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY &&
11104 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) {
11105 ctl_set_lun_standby(ctsio);
11110 /* The rest of checks are only done on executing side */
11111 if (softc->ha_mode == CTL_HA_MODE_XFER)
11115 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11117 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) {
11118 ctl_set_hw_write_protected(ctsio);
11122 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT]
11123 .eca_and_aen & SCP_SWP) != 0) {
11124 ctl_set_sense(ctsio, /*current_error*/ 1,
11125 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11126 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11133 * Check for a reservation conflict. If this command isn't allowed
11134 * even on reserved LUNs, and if this initiator isn't the one who
11135 * reserved us, reject the command with a reservation conflict.
11137 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11138 if ((lun->flags & CTL_LUN_RESERVED)
11139 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11140 if (lun->res_idx != residx) {
11141 ctl_set_reservation_conflict(ctsio);
11147 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11148 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11149 /* No reservation or command is allowed. */;
11150 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11151 (lun->res_type == SPR_TYPE_WR_EX ||
11152 lun->res_type == SPR_TYPE_WR_EX_RO ||
11153 lun->res_type == SPR_TYPE_WR_EX_AR)) {
11154 /* The command is allowed for Write Exclusive resv. */;
11157 * if we aren't registered or it's a res holder type
11158 * reservation and this isn't the res holder then set a
11161 if (ctl_get_prkey(lun, residx) == 0
11162 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
11163 ctl_set_reservation_conflict(ctsio);
11169 if ((lun->flags & CTL_LUN_OFFLINE)
11170 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0)) {
11171 ctl_set_lun_not_ready(ctsio);
11176 if ((lun->flags & CTL_LUN_STOPPED)
11177 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
11178 /* "Logical unit not ready, initializing cmd. required" */
11179 ctl_set_lun_stopped(ctsio);
11184 if ((lun->flags & CTL_LUN_INOPERABLE)
11185 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
11186 /* "Medium format corrupted" */
11187 ctl_set_medium_format_corrupted(ctsio);
11197 ctl_failover_io(union ctl_io *io, int have_lock)
11199 ctl_set_busy(&io->scsiio);
11204 ctl_failover_lun(union ctl_io *rio)
11206 struct ctl_softc *softc = control_softc;
11207 struct ctl_lun *lun;
11208 struct ctl_io_hdr *io, *next_io;
11211 targ_lun = rio->io_hdr.nexus.targ_mapped_lun;
11212 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun));
11214 /* Find and lock the LUN. */
11215 mtx_lock(&softc->ctl_lock);
11216 if ((targ_lun < CTL_MAX_LUNS) &&
11217 ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
11218 mtx_lock(&lun->lun_lock);
11219 mtx_unlock(&softc->ctl_lock);
11220 if (lun->flags & CTL_LUN_DISABLED) {
11221 mtx_unlock(&lun->lun_lock);
11225 mtx_unlock(&softc->ctl_lock);
11229 if (softc->ha_mode == CTL_HA_MODE_XFER) {
11230 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11231 /* We are master */
11232 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11233 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11234 io->flags |= CTL_FLAG_ABORT;
11235 io->flags |= CTL_FLAG_FAILOVER;
11236 } else { /* This can be only due to DATAMOVE */
11237 io->msg_type = CTL_MSG_DATAMOVE_DONE;
11238 io->flags &= ~CTL_FLAG_DMA_INPROG;
11239 io->flags |= CTL_FLAG_IO_ACTIVE;
11240 io->port_status = 31340;
11241 ctl_enqueue_isc((union ctl_io *)io);
11245 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11246 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11247 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11248 io->flags |= CTL_FLAG_FAILOVER;
11250 ctl_set_busy(&((union ctl_io *)io)->
11252 ctl_done((union ctl_io *)io);
11256 } else { /* SERIALIZE modes */
11257 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links,
11259 /* We are master */
11260 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11261 TAILQ_REMOVE(&lun->blocked_queue, io,
11263 io->flags &= ~CTL_FLAG_BLOCKED;
11264 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11265 ctl_free_io((union ctl_io *)io);
11268 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11269 /* We are master */
11270 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11271 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11272 ctl_free_io((union ctl_io *)io);
11275 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11276 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11277 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) {
11278 ctl_set_busy(&((union ctl_io *)io)->
11280 ctl_done((union ctl_io *)io);
11284 ctl_check_blocked(lun);
11286 mtx_unlock(&lun->lun_lock);
11290 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio)
11292 struct ctl_lun *lun;
11293 const struct ctl_cmd_entry *entry;
11294 uint32_t initidx, targ_lun;
11301 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11302 if ((targ_lun < CTL_MAX_LUNS)
11303 && ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
11305 * If the LUN is invalid, pretend that it doesn't exist.
11306 * It will go away as soon as all pending I/O has been
11309 mtx_lock(&lun->lun_lock);
11310 if (lun->flags & CTL_LUN_DISABLED) {
11311 mtx_unlock(&lun->lun_lock);
11313 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11314 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11316 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
11317 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
11321 * Every I/O goes into the OOA queue for a
11322 * particular LUN, and stays there until completion.
11325 if (TAILQ_EMPTY(&lun->ooa_queue)) {
11326 lun->idle_time += getsbinuptime() -
11330 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr,
11334 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11335 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11338 /* Get command entry and return error if it is unsuppotyed. */
11339 entry = ctl_validate_command(ctsio);
11340 if (entry == NULL) {
11342 mtx_unlock(&lun->lun_lock);
11346 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11347 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11350 * Check to see whether we can send this command to LUNs that don't
11351 * exist. This should pretty much only be the case for inquiry
11352 * and request sense. Further checks, below, really require having
11353 * a LUN, so we can't really check the command anymore. Just put
11354 * it on the rtr queue.
11357 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) {
11358 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11359 ctl_enqueue_rtr((union ctl_io *)ctsio);
11363 ctl_set_unsupported_lun(ctsio);
11364 ctl_done((union ctl_io *)ctsio);
11365 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11369 * Make sure we support this particular command on this LUN.
11370 * e.g., we don't support writes to the control LUN.
11372 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11373 mtx_unlock(&lun->lun_lock);
11374 ctl_set_invalid_opcode(ctsio);
11375 ctl_done((union ctl_io *)ctsio);
11380 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11384 * If we've got a request sense, it'll clear the contingent
11385 * allegiance condition. Otherwise, if we have a CA condition for
11386 * this initiator, clear it, because it sent down a command other
11387 * than request sense.
11389 if ((ctsio->cdb[0] != REQUEST_SENSE)
11390 && (ctl_is_set(lun->have_ca, initidx)))
11391 ctl_clear_mask(lun->have_ca, initidx);
11395 * If the command has this flag set, it handles its own unit
11396 * attention reporting, we shouldn't do anything. Otherwise we
11397 * check for any pending unit attentions, and send them back to the
11398 * initiator. We only do this when a command initially comes in,
11399 * not when we pull it off the blocked queue.
11401 * According to SAM-3, section 5.3.2, the order that things get
11402 * presented back to the host is basically unit attentions caused
11403 * by some sort of reset event, busy status, reservation conflicts
11404 * or task set full, and finally any other status.
11406 * One issue here is that some of the unit attentions we report
11407 * don't fall into the "reset" category (e.g. "reported luns data
11408 * has changed"). So reporting it here, before the reservation
11409 * check, may be technically wrong. I guess the only thing to do
11410 * would be to check for and report the reset events here, and then
11411 * check for the other unit attention types after we check for a
11412 * reservation conflict.
11414 * XXX KDM need to fix this
11416 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11417 ctl_ua_type ua_type;
11419 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11421 if (ua_type != CTL_UA_NONE) {
11422 mtx_unlock(&lun->lun_lock);
11423 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11424 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11425 ctsio->sense_len = SSD_FULL_SIZE;
11426 ctl_done((union ctl_io *)ctsio);
11432 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11433 mtx_unlock(&lun->lun_lock);
11434 ctl_done((union ctl_io *)ctsio);
11439 * XXX CHD this is where we want to send IO to other side if
11440 * this LUN is secondary on this SC. We will need to make a copy
11441 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11442 * the copy we send as FROM_OTHER.
11443 * We also need to stuff the address of the original IO so we can
11444 * find it easily. Something similar will need be done on the other
11445 * side so when we are done we can find the copy.
11447 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
11448 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 &&
11449 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) {
11450 union ctl_ha_msg msg_info;
11453 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11454 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11455 mtx_unlock(&lun->lun_lock);
11457 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11458 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11459 msg_info.hdr.serializing_sc = NULL;
11460 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11461 msg_info.scsi.tag_num = ctsio->tag_num;
11462 msg_info.scsi.tag_type = ctsio->tag_type;
11463 msg_info.scsi.cdb_len = ctsio->cdb_len;
11464 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11466 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11467 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data),
11468 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) {
11469 ctl_set_busy(ctsio);
11470 ctl_done((union ctl_io *)ctsio);
11476 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11477 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11478 ctl_ooaq, ooa_links))) {
11479 case CTL_ACTION_BLOCK:
11480 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11481 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11483 mtx_unlock(&lun->lun_lock);
11485 case CTL_ACTION_PASS:
11486 case CTL_ACTION_SKIP:
11487 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11488 mtx_unlock(&lun->lun_lock);
11489 ctl_enqueue_rtr((union ctl_io *)ctsio);
11491 case CTL_ACTION_OVERLAP:
11492 mtx_unlock(&lun->lun_lock);
11493 ctl_set_overlapped_cmd(ctsio);
11494 ctl_done((union ctl_io *)ctsio);
11496 case CTL_ACTION_OVERLAP_TAG:
11497 mtx_unlock(&lun->lun_lock);
11498 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11499 ctl_done((union ctl_io *)ctsio);
11501 case CTL_ACTION_ERROR:
11503 mtx_unlock(&lun->lun_lock);
11504 ctl_set_internal_failure(ctsio,
11506 /*retry_count*/ 0);
11507 ctl_done((union ctl_io *)ctsio);
11513 const struct ctl_cmd_entry *
11514 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11516 const struct ctl_cmd_entry *entry;
11517 int service_action;
11519 entry = &ctl_cmd_table[ctsio->cdb[0]];
11521 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11522 if (entry->flags & CTL_CMD_FLAG_SA5) {
11523 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11524 entry = &((const struct ctl_cmd_entry *)
11525 entry->execute)[service_action];
11530 const struct ctl_cmd_entry *
11531 ctl_validate_command(struct ctl_scsiio *ctsio)
11533 const struct ctl_cmd_entry *entry;
11537 entry = ctl_get_cmd_entry(ctsio, &sa);
11538 if (entry->execute == NULL) {
11540 ctl_set_invalid_field(ctsio,
11547 ctl_set_invalid_opcode(ctsio);
11548 ctl_done((union ctl_io *)ctsio);
11551 KASSERT(entry->length > 0,
11552 ("Not defined length for command 0x%02x/0x%02x",
11553 ctsio->cdb[0], ctsio->cdb[1]));
11554 for (i = 1; i < entry->length; i++) {
11555 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11558 ctl_set_invalid_field(ctsio,
11563 /*bit*/ fls(diff) - 1);
11564 ctl_done((union ctl_io *)ctsio);
11571 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11574 switch (lun_type) {
11576 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
11580 if ((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0)
11590 ctl_scsiio(struct ctl_scsiio *ctsio)
11593 const struct ctl_cmd_entry *entry;
11595 retval = CTL_RETVAL_COMPLETE;
11597 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11599 entry = ctl_get_cmd_entry(ctsio, NULL);
11602 * If this I/O has been aborted, just send it straight to
11603 * ctl_done() without executing it.
11605 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11606 ctl_done((union ctl_io *)ctsio);
11611 * All the checks should have been handled by ctl_scsiio_precheck().
11612 * We should be clear now to just execute the I/O.
11614 retval = entry->execute(ctsio);
11621 * Since we only implement one target right now, a bus reset simply resets
11622 * our single target.
11625 ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io)
11627 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET));
11631 ctl_target_reset(struct ctl_softc *softc, union ctl_io *io,
11632 ctl_ua_type ua_type)
11634 struct ctl_port *port;
11635 struct ctl_lun *lun;
11638 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11639 union ctl_ha_msg msg_info;
11641 msg_info.hdr.nexus = io->io_hdr.nexus;
11642 if (ua_type==CTL_UA_TARG_RESET)
11643 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11645 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11646 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11647 msg_info.hdr.original_sc = NULL;
11648 msg_info.hdr.serializing_sc = NULL;
11649 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11650 sizeof(msg_info.task), M_WAITOK);
11654 mtx_lock(&softc->ctl_lock);
11655 port = ctl_io_port(&io->io_hdr);
11656 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11657 if (port != NULL &&
11658 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
11660 retval += ctl_do_lun_reset(lun, io, ua_type);
11662 mtx_unlock(&softc->ctl_lock);
11663 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11668 * The LUN should always be set. The I/O is optional, and is used to
11669 * distinguish between I/Os sent by this initiator, and by other
11670 * initiators. We set unit attention for initiators other than this one.
11671 * SAM-3 is vague on this point. It does say that a unit attention should
11672 * be established for other initiators when a LUN is reset (see section
11673 * 5.7.3), but it doesn't specifically say that the unit attention should
11674 * be established for this particular initiator when a LUN is reset. Here
11675 * is the relevant text, from SAM-3 rev 8:
11677 * 5.7.2 When a SCSI initiator port aborts its own tasks
11679 * When a SCSI initiator port causes its own task(s) to be aborted, no
11680 * notification that the task(s) have been aborted shall be returned to
11681 * the SCSI initiator port other than the completion response for the
11682 * command or task management function action that caused the task(s) to
11683 * be aborted and notification(s) associated with related effects of the
11684 * action (e.g., a reset unit attention condition).
11686 * XXX KDM for now, we're setting unit attention for all initiators.
11689 ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11699 mtx_lock(&lun->lun_lock);
11701 * Run through the OOA queue and abort each I/O.
11703 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11704 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11705 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11709 * This version sets unit attention for every
11712 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11713 ctl_est_ua_all(lun, initidx, ua_type);
11715 ctl_est_ua_all(lun, -1, ua_type);
11719 * A reset (any kind, really) clears reservations established with
11720 * RESERVE/RELEASE. It does not clear reservations established
11721 * with PERSISTENT RESERVE OUT, but we don't support that at the
11722 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
11723 * reservations made with the RESERVE/RELEASE commands, because
11724 * those commands are obsolete in SPC-3.
11726 lun->flags &= ~CTL_LUN_RESERVED;
11729 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11730 ctl_clear_mask(lun->have_ca, i);
11732 mtx_unlock(&lun->lun_lock);
11738 ctl_lun_reset(struct ctl_softc *softc, union ctl_io *io)
11740 struct ctl_lun *lun;
11744 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11745 mtx_lock(&softc->ctl_lock);
11746 if ((targ_lun >= CTL_MAX_LUNS) ||
11747 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11748 mtx_unlock(&softc->ctl_lock);
11749 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11752 retval = ctl_do_lun_reset(lun, io, CTL_UA_LUN_RESET);
11753 mtx_unlock(&softc->ctl_lock);
11754 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11756 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) {
11757 union ctl_ha_msg msg_info;
11759 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11760 msg_info.hdr.nexus = io->io_hdr.nexus;
11761 msg_info.task.task_action = CTL_TASK_LUN_RESET;
11762 msg_info.hdr.original_sc = NULL;
11763 msg_info.hdr.serializing_sc = NULL;
11764 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11765 sizeof(msg_info.task), M_WAITOK);
11771 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11776 mtx_assert(&lun->lun_lock, MA_OWNED);
11779 * Run through the OOA queue and attempt to find the given I/O.
11780 * The target port, initiator ID, tag type and tag number have to
11781 * match the values that we got from the initiator. If we have an
11782 * untagged command to abort, simply abort the first untagged command
11783 * we come to. We only allow one untagged command at a time of course.
11785 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11786 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11788 if ((targ_port == UINT32_MAX ||
11789 targ_port == xio->io_hdr.nexus.targ_port) &&
11790 (init_id == UINT32_MAX ||
11791 init_id == xio->io_hdr.nexus.initid)) {
11792 if (targ_port != xio->io_hdr.nexus.targ_port ||
11793 init_id != xio->io_hdr.nexus.initid)
11794 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
11795 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11796 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11797 union ctl_ha_msg msg_info;
11799 msg_info.hdr.nexus = xio->io_hdr.nexus;
11800 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11801 msg_info.task.tag_num = xio->scsiio.tag_num;
11802 msg_info.task.tag_type = xio->scsiio.tag_type;
11803 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11804 msg_info.hdr.original_sc = NULL;
11805 msg_info.hdr.serializing_sc = NULL;
11806 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11807 sizeof(msg_info.task), M_NOWAIT);
11814 ctl_abort_task_set(union ctl_io *io)
11816 struct ctl_softc *softc = control_softc;
11817 struct ctl_lun *lun;
11823 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11824 mtx_lock(&softc->ctl_lock);
11825 if ((targ_lun >= CTL_MAX_LUNS) ||
11826 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11827 mtx_unlock(&softc->ctl_lock);
11828 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11832 mtx_lock(&lun->lun_lock);
11833 mtx_unlock(&softc->ctl_lock);
11834 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
11835 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11836 io->io_hdr.nexus.initid,
11837 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11838 } else { /* CTL_TASK_CLEAR_TASK_SET */
11839 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
11840 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11842 mtx_unlock(&lun->lun_lock);
11843 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11848 ctl_i_t_nexus_reset(union ctl_io *io)
11850 struct ctl_softc *softc = control_softc;
11851 struct ctl_lun *lun;
11854 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11855 union ctl_ha_msg msg_info;
11857 msg_info.hdr.nexus = io->io_hdr.nexus;
11858 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET;
11859 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11860 msg_info.hdr.original_sc = NULL;
11861 msg_info.hdr.serializing_sc = NULL;
11862 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11863 sizeof(msg_info.task), M_WAITOK);
11866 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11867 mtx_lock(&softc->ctl_lock);
11868 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11869 mtx_lock(&lun->lun_lock);
11870 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11871 io->io_hdr.nexus.initid, 1);
11873 ctl_clear_mask(lun->have_ca, initidx);
11875 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx))
11876 lun->flags &= ~CTL_LUN_RESERVED;
11877 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS);
11878 mtx_unlock(&lun->lun_lock);
11880 mtx_unlock(&softc->ctl_lock);
11881 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11886 ctl_abort_task(union ctl_io *io)
11889 struct ctl_lun *lun;
11890 struct ctl_softc *softc;
11893 char printbuf[128];
11898 softc = control_softc;
11904 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11905 mtx_lock(&softc->ctl_lock);
11906 if ((targ_lun >= CTL_MAX_LUNS) ||
11907 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11908 mtx_unlock(&softc->ctl_lock);
11909 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11914 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
11915 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
11918 mtx_lock(&lun->lun_lock);
11919 mtx_unlock(&softc->ctl_lock);
11921 * Run through the OOA queue and attempt to find the given I/O.
11922 * The target port, initiator ID, tag type and tag number have to
11923 * match the values that we got from the initiator. If we have an
11924 * untagged command to abort, simply abort the first untagged command
11925 * we come to. We only allow one untagged command at a time of course.
11927 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11928 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11930 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
11932 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
11933 lun->lun, xio->scsiio.tag_num,
11934 xio->scsiio.tag_type,
11935 (xio->io_hdr.blocked_links.tqe_prev
11936 == NULL) ? "" : " BLOCKED",
11937 (xio->io_hdr.flags &
11938 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
11939 (xio->io_hdr.flags &
11940 CTL_FLAG_ABORT) ? " ABORT" : "",
11941 (xio->io_hdr.flags &
11942 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
11943 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
11945 printf("%s\n", sbuf_data(&sb));
11948 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
11949 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
11950 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
11954 * If the abort says that the task is untagged, the
11955 * task in the queue must be untagged. Otherwise,
11956 * we just check to see whether the tag numbers
11957 * match. This is because the QLogic firmware
11958 * doesn't pass back the tag type in an abort
11962 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
11963 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
11964 || (xio->scsiio.tag_num == io->taskio.tag_num))
11967 * XXX KDM we've got problems with FC, because it
11968 * doesn't send down a tag type with aborts. So we
11969 * can only really go by the tag number...
11970 * This may cause problems with parallel SCSI.
11971 * Need to figure that out!!
11973 if (xio->scsiio.tag_num == io->taskio.tag_num) {
11974 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11976 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
11977 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11978 union ctl_ha_msg msg_info;
11980 msg_info.hdr.nexus = io->io_hdr.nexus;
11981 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11982 msg_info.task.tag_num = io->taskio.tag_num;
11983 msg_info.task.tag_type = io->taskio.tag_type;
11984 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11985 msg_info.hdr.original_sc = NULL;
11986 msg_info.hdr.serializing_sc = NULL;
11988 printf("Sent Abort to other side\n");
11990 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11991 sizeof(msg_info.task), M_NOWAIT);
11994 printf("ctl_abort_task: found I/O to abort\n");
11998 mtx_unlock(&lun->lun_lock);
12002 * This isn't really an error. It's entirely possible for
12003 * the abort and command completion to cross on the wire.
12004 * This is more of an informative/diagnostic error.
12007 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
12008 "%u:%u:%u tag %d type %d\n",
12009 io->io_hdr.nexus.initid,
12010 io->io_hdr.nexus.targ_port,
12011 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
12012 io->taskio.tag_type);
12015 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12020 ctl_query_task(union ctl_io *io, int task_set)
12023 struct ctl_lun *lun;
12024 struct ctl_softc *softc;
12028 softc = control_softc;
12029 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12030 mtx_lock(&softc->ctl_lock);
12031 if ((targ_lun >= CTL_MAX_LUNS) ||
12032 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12033 mtx_unlock(&softc->ctl_lock);
12034 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12037 mtx_lock(&lun->lun_lock);
12038 mtx_unlock(&softc->ctl_lock);
12039 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12040 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12042 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12043 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12044 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12047 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) {
12052 mtx_unlock(&lun->lun_lock);
12054 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12056 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12061 ctl_query_async_event(union ctl_io *io)
12063 struct ctl_lun *lun;
12064 struct ctl_softc *softc;
12066 uint32_t targ_lun, initidx;
12068 softc = control_softc;
12069 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12070 mtx_lock(&softc->ctl_lock);
12071 if ((targ_lun >= CTL_MAX_LUNS) ||
12072 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12073 mtx_unlock(&softc->ctl_lock);
12074 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12077 mtx_lock(&lun->lun_lock);
12078 mtx_unlock(&softc->ctl_lock);
12079 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12080 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp);
12081 mtx_unlock(&lun->lun_lock);
12082 if (ua != CTL_UA_NONE)
12083 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12085 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12090 ctl_run_task(union ctl_io *io)
12092 struct ctl_softc *softc = control_softc;
12095 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12096 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12097 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type));
12098 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED;
12099 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp));
12100 switch (io->taskio.task_action) {
12101 case CTL_TASK_ABORT_TASK:
12102 retval = ctl_abort_task(io);
12104 case CTL_TASK_ABORT_TASK_SET:
12105 case CTL_TASK_CLEAR_TASK_SET:
12106 retval = ctl_abort_task_set(io);
12108 case CTL_TASK_CLEAR_ACA:
12110 case CTL_TASK_I_T_NEXUS_RESET:
12111 retval = ctl_i_t_nexus_reset(io);
12113 case CTL_TASK_LUN_RESET:
12114 retval = ctl_lun_reset(softc, io);
12116 case CTL_TASK_TARGET_RESET:
12117 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET);
12119 case CTL_TASK_BUS_RESET:
12120 retval = ctl_bus_reset(softc, io);
12122 case CTL_TASK_PORT_LOGIN:
12124 case CTL_TASK_PORT_LOGOUT:
12126 case CTL_TASK_QUERY_TASK:
12127 retval = ctl_query_task(io, 0);
12129 case CTL_TASK_QUERY_TASK_SET:
12130 retval = ctl_query_task(io, 1);
12132 case CTL_TASK_QUERY_ASYNC_EVENT:
12133 retval = ctl_query_async_event(io);
12136 printf("%s: got unknown task management event %d\n",
12137 __func__, io->taskio.task_action);
12141 io->io_hdr.status = CTL_SUCCESS;
12143 io->io_hdr.status = CTL_ERROR;
12148 * For HA operation. Handle commands that come in from the other
12152 ctl_handle_isc(union ctl_io *io)
12155 struct ctl_lun *lun;
12156 struct ctl_softc *softc = control_softc;
12159 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12160 lun = softc->ctl_luns[targ_lun];
12162 switch (io->io_hdr.msg_type) {
12163 case CTL_MSG_SERIALIZE:
12164 free_io = ctl_serialize_other_sc_cmd(&io->scsiio);
12166 case CTL_MSG_R2R: {
12167 const struct ctl_cmd_entry *entry;
12170 * This is only used in SER_ONLY mode.
12173 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12174 mtx_lock(&lun->lun_lock);
12175 if (ctl_scsiio_lun_check(lun,
12176 entry, (struct ctl_scsiio *)io) != 0) {
12177 mtx_unlock(&lun->lun_lock);
12181 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12182 mtx_unlock(&lun->lun_lock);
12183 ctl_enqueue_rtr(io);
12186 case CTL_MSG_FINISH_IO:
12187 if (softc->ha_mode == CTL_HA_MODE_XFER) {
12192 mtx_lock(&lun->lun_lock);
12193 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
12195 ctl_check_blocked(lun);
12196 mtx_unlock(&lun->lun_lock);
12199 case CTL_MSG_PERS_ACTION:
12200 ctl_hndl_per_res_out_on_other_sc(
12201 (union ctl_ha_msg *)&io->presio.pr_msg);
12204 case CTL_MSG_BAD_JUJU:
12208 case CTL_MSG_DATAMOVE:
12209 /* Only used in XFER mode */
12211 ctl_datamove_remote(io);
12213 case CTL_MSG_DATAMOVE_DONE:
12214 /* Only used in XFER mode */
12216 io->scsiio.be_move_done(io);
12218 case CTL_MSG_FAILOVER:
12219 ctl_failover_lun(io);
12224 printf("%s: Invalid message type %d\n",
12225 __func__, io->io_hdr.msg_type);
12235 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12236 * there is no match.
12238 static ctl_lun_error_pattern
12239 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12241 const struct ctl_cmd_entry *entry;
12242 ctl_lun_error_pattern filtered_pattern, pattern;
12244 pattern = desc->error_pattern;
12247 * XXX KDM we need more data passed into this function to match a
12248 * custom pattern, and we actually need to implement custom pattern
12251 if (pattern & CTL_LUN_PAT_CMD)
12252 return (CTL_LUN_PAT_CMD);
12254 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12255 return (CTL_LUN_PAT_ANY);
12257 entry = ctl_get_cmd_entry(ctsio, NULL);
12259 filtered_pattern = entry->pattern & pattern;
12262 * If the user requested specific flags in the pattern (e.g.
12263 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12266 * If the user did not specify any flags, it doesn't matter whether
12267 * or not the command supports the flags.
12269 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12270 (pattern & ~CTL_LUN_PAT_MASK))
12271 return (CTL_LUN_PAT_NONE);
12274 * If the user asked for a range check, see if the requested LBA
12275 * range overlaps with this command's LBA range.
12277 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12283 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12285 return (CTL_LUN_PAT_NONE);
12287 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12288 desc->lba_range.len, FALSE);
12290 * A "pass" means that the LBA ranges don't overlap, so
12291 * this doesn't match the user's range criteria.
12293 if (action == CTL_ACTION_PASS)
12294 return (CTL_LUN_PAT_NONE);
12297 return (filtered_pattern);
12301 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12303 struct ctl_error_desc *desc, *desc2;
12305 mtx_assert(&lun->lun_lock, MA_OWNED);
12307 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12308 ctl_lun_error_pattern pattern;
12310 * Check to see whether this particular command matches
12311 * the pattern in the descriptor.
12313 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12314 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12317 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12318 case CTL_LUN_INJ_ABORTED:
12319 ctl_set_aborted(&io->scsiio);
12321 case CTL_LUN_INJ_MEDIUM_ERR:
12322 ctl_set_medium_error(&io->scsiio,
12323 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) !=
12324 CTL_FLAG_DATA_OUT);
12326 case CTL_LUN_INJ_UA:
12327 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12329 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12331 case CTL_LUN_INJ_CUSTOM:
12333 * We're assuming the user knows what he is doing.
12334 * Just copy the sense information without doing
12337 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12338 MIN(sizeof(desc->custom_sense),
12339 sizeof(io->scsiio.sense_data)));
12340 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12341 io->scsiio.sense_len = SSD_FULL_SIZE;
12342 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12344 case CTL_LUN_INJ_NONE:
12347 * If this is an error injection type we don't know
12348 * about, clear the continuous flag (if it is set)
12349 * so it will get deleted below.
12351 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12355 * By default, each error injection action is a one-shot
12357 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12360 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12366 #ifdef CTL_IO_DELAY
12368 ctl_datamove_timer_wakeup(void *arg)
12372 io = (union ctl_io *)arg;
12376 #endif /* CTL_IO_DELAY */
12379 ctl_datamove(union ctl_io *io)
12381 struct ctl_lun *lun;
12382 void (*fe_datamove)(union ctl_io *io);
12384 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12386 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12388 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12390 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12395 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12396 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12398 sbuf_cat(&sb, path_str);
12399 switch (io->io_hdr.io_type) {
12401 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12402 sbuf_printf(&sb, "\n");
12403 sbuf_cat(&sb, path_str);
12404 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12405 io->scsiio.tag_num, io->scsiio.tag_type);
12408 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12409 "Tag Type: %d\n", io->taskio.task_action,
12410 io->taskio.tag_num, io->taskio.tag_type);
12413 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12414 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12417 sbuf_cat(&sb, path_str);
12418 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12419 (intmax_t)time_uptime - io->io_hdr.start_time);
12421 printf("%s", sbuf_data(&sb));
12423 #endif /* CTL_TIME_IO */
12425 #ifdef CTL_IO_DELAY
12426 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12427 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12430 && (lun->delay_info.datamove_delay > 0)) {
12432 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
12433 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12434 callout_reset(&io->io_hdr.delay_callout,
12435 lun->delay_info.datamove_delay * hz,
12436 ctl_datamove_timer_wakeup, io);
12437 if (lun->delay_info.datamove_type ==
12438 CTL_DELAY_TYPE_ONESHOT)
12439 lun->delay_info.datamove_delay = 0;
12446 * This command has been aborted. Set the port status, so we fail
12449 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12450 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n",
12451 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12452 io->io_hdr.nexus.targ_port,
12453 io->io_hdr.nexus.targ_lun);
12454 io->io_hdr.port_status = 31337;
12456 * Note that the backend, in this case, will get the
12457 * callback in its context. In other cases it may get
12458 * called in the frontend's interrupt thread context.
12460 io->scsiio.be_move_done(io);
12464 /* Don't confuse frontend with zero length data move. */
12465 if (io->scsiio.kern_data_len == 0) {
12466 io->scsiio.be_move_done(io);
12470 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12475 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12477 union ctl_ha_msg msg;
12479 struct bintime cur_bt;
12482 memset(&msg, 0, sizeof(msg));
12483 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12484 msg.hdr.original_sc = io;
12485 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12486 msg.hdr.nexus = io->io_hdr.nexus;
12487 msg.hdr.status = io->io_hdr.status;
12488 msg.scsi.tag_num = io->scsiio.tag_num;
12489 msg.scsi.tag_type = io->scsiio.tag_type;
12490 msg.scsi.scsi_status = io->scsiio.scsi_status;
12491 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12492 io->scsiio.sense_len);
12493 msg.scsi.sense_len = io->scsiio.sense_len;
12494 msg.scsi.sense_residual = io->scsiio.sense_residual;
12495 msg.scsi.fetd_status = io->io_hdr.port_status;
12496 msg.scsi.residual = io->scsiio.residual;
12497 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12498 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12499 ctl_failover_io(io, /*have_lock*/ have_lock);
12502 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12503 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12504 msg.scsi.sense_len, M_WAITOK);
12507 getbinuptime(&cur_bt);
12508 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
12509 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
12511 io->io_hdr.num_dmas++;
12515 * The DMA to the remote side is done, now we need to tell the other side
12516 * we're done so it can continue with its data movement.
12519 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12526 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12527 printf("%s: ISC DMA write failed with error %d", __func__,
12529 ctl_set_internal_failure(&io->scsiio,
12531 /*retry_count*/ rq->ret);
12534 ctl_dt_req_free(rq);
12536 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12537 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12538 free(io->io_hdr.remote_sglist, M_CTL);
12539 io->io_hdr.remote_sglist = NULL;
12540 io->io_hdr.local_sglist = NULL;
12543 * The data is in local and remote memory, so now we need to send
12544 * status (good or back) back to the other side.
12546 ctl_send_datamove_done(io, /*have_lock*/ 0);
12550 * We've moved the data from the host/controller into local memory. Now we
12551 * need to push it over to the remote controller's memory.
12554 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12558 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12559 ctl_datamove_remote_write_cb);
12564 ctl_datamove_remote_write(union ctl_io *io)
12567 void (*fe_datamove)(union ctl_io *io);
12570 * - Get the data from the host/HBA into local memory.
12571 * - DMA memory from the local controller to the remote controller.
12572 * - Send status back to the remote controller.
12575 retval = ctl_datamove_remote_sgl_setup(io);
12579 /* Switch the pointer over so the FETD knows what to do */
12580 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12583 * Use a custom move done callback, since we need to send completion
12584 * back to the other controller, not to the backend on this side.
12586 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12588 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12593 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12602 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12603 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12604 free(io->io_hdr.remote_sglist, M_CTL);
12605 io->io_hdr.remote_sglist = NULL;
12606 io->io_hdr.local_sglist = NULL;
12609 scsi_path_string(io, path_str, sizeof(path_str));
12610 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12611 sbuf_cat(&sb, path_str);
12612 scsi_command_string(&io->scsiio, NULL, &sb);
12613 sbuf_printf(&sb, "\n");
12614 sbuf_cat(&sb, path_str);
12615 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12616 io->scsiio.tag_num, io->scsiio.tag_type);
12617 sbuf_cat(&sb, path_str);
12618 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12619 io->io_hdr.flags, io->io_hdr.status);
12621 printk("%s", sbuf_data(&sb));
12626 * The read is done, now we need to send status (good or bad) back
12627 * to the other side.
12629 ctl_send_datamove_done(io, /*have_lock*/ 0);
12635 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12638 void (*fe_datamove)(union ctl_io *io);
12642 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12643 printf("%s: ISC DMA read failed with error %d\n", __func__,
12645 ctl_set_internal_failure(&io->scsiio,
12647 /*retry_count*/ rq->ret);
12650 ctl_dt_req_free(rq);
12652 /* Switch the pointer over so the FETD knows what to do */
12653 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12656 * Use a custom move done callback, since we need to send completion
12657 * back to the other controller, not to the backend on this side.
12659 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12661 /* XXX KDM add checks like the ones in ctl_datamove? */
12663 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12668 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12670 struct ctl_sg_entry *local_sglist;
12671 uint32_t len_to_go;
12676 local_sglist = io->io_hdr.local_sglist;
12677 len_to_go = io->scsiio.kern_data_len;
12680 * The difficult thing here is that the size of the various
12681 * S/G segments may be different than the size from the
12682 * remote controller. That'll make it harder when DMAing
12683 * the data back to the other side.
12685 for (i = 0; len_to_go > 0; i++) {
12686 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT);
12687 local_sglist[i].addr =
12688 malloc(local_sglist[i].len, M_CTL, M_WAITOK);
12690 len_to_go -= local_sglist[i].len;
12693 * Reset the number of S/G entries accordingly. The original
12694 * number of S/G entries is available in rem_sg_entries.
12696 io->scsiio.kern_sg_entries = i;
12699 printf("%s: kern_sg_entries = %d\n", __func__,
12700 io->scsiio.kern_sg_entries);
12701 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12702 printf("%s: sg[%d] = %p, %lu\n", __func__, i,
12703 local_sglist[i].addr, local_sglist[i].len);
12710 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12711 ctl_ha_dt_cb callback)
12713 struct ctl_ha_dt_req *rq;
12714 struct ctl_sg_entry *remote_sglist, *local_sglist;
12715 uint32_t local_used, remote_used, total_used;
12718 rq = ctl_dt_req_alloc();
12721 * If we failed to allocate the request, and if the DMA didn't fail
12722 * anyway, set busy status. This is just a resource allocation
12726 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12727 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS))
12728 ctl_set_busy(&io->scsiio);
12730 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12731 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
12734 ctl_dt_req_free(rq);
12737 * The data move failed. We need to return status back
12738 * to the other controller. No point in trying to DMA
12739 * data to the remote controller.
12742 ctl_send_datamove_done(io, /*have_lock*/ 0);
12747 local_sglist = io->io_hdr.local_sglist;
12748 remote_sglist = io->io_hdr.remote_sglist;
12754 * Pull/push the data over the wire from/to the other controller.
12755 * This takes into account the possibility that the local and
12756 * remote sglists may not be identical in terms of the size of
12757 * the elements and the number of elements.
12759 * One fundamental assumption here is that the length allocated for
12760 * both the local and remote sglists is identical. Otherwise, we've
12761 * essentially got a coding error of some sort.
12763 isc_ret = CTL_HA_STATUS_SUCCESS;
12764 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12768 rq->command = command;
12772 * Both pointers should be aligned. But it is possible
12773 * that the allocation length is not. They should both
12774 * also have enough slack left over at the end, though,
12775 * to round up to the next 8 byte boundary.
12777 cur_len = MIN(local_sglist[i].len - local_used,
12778 remote_sglist[j].len - remote_used);
12779 rq->size = cur_len;
12781 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12782 tmp_ptr += local_used;
12785 /* Use physical addresses when talking to ISC hardware */
12786 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12787 /* XXX KDM use busdma */
12788 rq->local = vtophys(tmp_ptr);
12790 rq->local = tmp_ptr;
12792 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
12793 ("HA does not support BUS_ADDR"));
12794 rq->local = tmp_ptr;
12797 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12798 tmp_ptr += remote_used;
12799 rq->remote = tmp_ptr;
12801 rq->callback = NULL;
12803 local_used += cur_len;
12804 if (local_used >= local_sglist[i].len) {
12809 remote_used += cur_len;
12810 if (remote_used >= remote_sglist[j].len) {
12814 total_used += cur_len;
12816 if (total_used >= io->scsiio.kern_data_len)
12817 rq->callback = callback;
12820 printf("%s: %s: local %p remote %p size %d\n", __func__,
12821 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
12822 rq->local, rq->remote, rq->size);
12825 isc_ret = ctl_dt_single(rq);
12826 if (isc_ret > CTL_HA_STATUS_SUCCESS)
12829 if (isc_ret != CTL_HA_STATUS_WAIT) {
12838 ctl_datamove_remote_read(union ctl_io *io)
12844 * This will send an error to the other controller in the case of a
12847 retval = ctl_datamove_remote_sgl_setup(io);
12851 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12852 ctl_datamove_remote_read_cb);
12855 * Make sure we free memory if there was an error.. The
12856 * ctl_datamove_remote_xfer() function will send the
12857 * datamove done message, or call the callback with an
12858 * error if there is a problem.
12860 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12861 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12862 free(io->io_hdr.remote_sglist, M_CTL);
12863 io->io_hdr.remote_sglist = NULL;
12864 io->io_hdr.local_sglist = NULL;
12869 * Process a datamove request from the other controller. This is used for
12870 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
12871 * first. Once that is complete, the data gets DMAed into the remote
12872 * controller's memory. For reads, we DMA from the remote controller's
12873 * memory into our memory first, and then move it out to the FETD.
12876 ctl_datamove_remote(union ctl_io *io)
12879 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12881 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12882 ctl_failover_io(io, /*have_lock*/ 0);
12887 * Note that we look for an aborted I/O here, but don't do some of
12888 * the other checks that ctl_datamove() normally does.
12889 * We don't need to run the datamove delay code, since that should
12890 * have been done if need be on the other controller.
12892 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12893 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__,
12894 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12895 io->io_hdr.nexus.targ_port,
12896 io->io_hdr.nexus.targ_lun);
12897 io->io_hdr.port_status = 31338;
12898 ctl_send_datamove_done(io, /*have_lock*/ 0);
12902 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
12903 ctl_datamove_remote_write(io);
12904 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
12905 ctl_datamove_remote_read(io);
12907 io->io_hdr.port_status = 31339;
12908 ctl_send_datamove_done(io, /*have_lock*/ 0);
12913 ctl_process_done(union ctl_io *io)
12915 struct ctl_lun *lun;
12916 struct ctl_softc *softc = control_softc;
12917 void (*fe_done)(union ctl_io *io);
12918 union ctl_ha_msg msg;
12919 uint32_t targ_port = io->io_hdr.nexus.targ_port;
12921 CTL_DEBUG_PRINT(("ctl_process_done\n"));
12922 fe_done = softc->ctl_ports[targ_port]->fe_done;
12925 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12930 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12931 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12933 sbuf_cat(&sb, path_str);
12934 switch (io->io_hdr.io_type) {
12936 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12937 sbuf_printf(&sb, "\n");
12938 sbuf_cat(&sb, path_str);
12939 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12940 io->scsiio.tag_num, io->scsiio.tag_type);
12943 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12944 "Tag Type: %d\n", io->taskio.task_action,
12945 io->taskio.tag_num, io->taskio.tag_type);
12948 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12949 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12952 sbuf_cat(&sb, path_str);
12953 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
12954 (intmax_t)time_uptime - io->io_hdr.start_time);
12956 printf("%s", sbuf_data(&sb));
12958 #endif /* CTL_TIME_IO */
12960 switch (io->io_hdr.io_type) {
12964 if (ctl_debug & CTL_DEBUG_INFO)
12965 ctl_io_error_print(io, NULL);
12969 panic("ctl_process_done: invalid io type %d\n",
12970 io->io_hdr.io_type);
12971 break; /* NOTREACHED */
12974 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12976 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
12977 io->io_hdr.nexus.targ_mapped_lun));
12981 mtx_lock(&lun->lun_lock);
12984 * Check to see if we have any errors to inject here. We only
12985 * inject errors for commands that don't already have errors set.
12987 if ((STAILQ_FIRST(&lun->error_list) != NULL) &&
12988 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
12989 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
12990 ctl_inject_error(lun, io);
12993 * XXX KDM how do we treat commands that aren't completed
12996 * XXX KDM should we also track I/O latency?
12998 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
12999 io->io_hdr.io_type == CTL_IO_SCSI) {
13001 struct bintime cur_bt;
13005 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13007 type = CTL_STATS_READ;
13008 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13010 type = CTL_STATS_WRITE;
13012 type = CTL_STATS_NO_IO;
13014 lun->stats.ports[targ_port].bytes[type] +=
13015 io->scsiio.kern_total_len;
13016 lun->stats.ports[targ_port].operations[type]++;
13018 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
13019 &io->io_hdr.dma_bt);
13020 getbinuptime(&cur_bt);
13021 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
13022 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
13024 lun->stats.ports[targ_port].num_dmas[type] +=
13025 io->io_hdr.num_dmas;
13029 * Remove this from the OOA queue.
13031 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13033 if (TAILQ_EMPTY(&lun->ooa_queue))
13034 lun->last_busy = getsbinuptime();
13038 * Run through the blocked queue on this LUN and see if anything
13039 * has become unblocked, now that this transaction is done.
13041 ctl_check_blocked(lun);
13044 * If the LUN has been invalidated, free it if there is nothing
13045 * left on its OOA queue.
13047 if ((lun->flags & CTL_LUN_INVALID)
13048 && TAILQ_EMPTY(&lun->ooa_queue)) {
13049 mtx_unlock(&lun->lun_lock);
13050 mtx_lock(&softc->ctl_lock);
13052 mtx_unlock(&softc->ctl_lock);
13054 mtx_unlock(&lun->lun_lock);
13059 * If this command has been aborted, make sure we set the status
13060 * properly. The FETD is responsible for freeing the I/O and doing
13061 * whatever it needs to do to clean up its state.
13063 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13064 ctl_set_task_aborted(&io->scsiio);
13067 * If enabled, print command error status.
13069 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
13070 (ctl_debug & CTL_DEBUG_INFO) != 0)
13071 ctl_io_error_print(io, NULL);
13074 * Tell the FETD or the other shelf controller we're done with this
13075 * command. Note that only SCSI commands get to this point. Task
13076 * management commands are completed above.
13078 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
13079 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) {
13080 memset(&msg, 0, sizeof(msg));
13081 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13082 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
13083 msg.hdr.nexus = io->io_hdr.nexus;
13084 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13085 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data),
13094 * Front end should call this if it doesn't do autosense. When the request
13095 * sense comes back in from the initiator, we'll dequeue this and send it.
13098 ctl_queue_sense(union ctl_io *io)
13100 struct ctl_lun *lun;
13101 struct ctl_port *port;
13102 struct ctl_softc *softc;
13103 uint32_t initidx, targ_lun;
13105 softc = control_softc;
13107 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13110 * LUN lookup will likely move to the ctl_work_thread() once we
13111 * have our new queueing infrastructure (that doesn't put things on
13112 * a per-LUN queue initially). That is so that we can handle
13113 * things like an INQUIRY to a LUN that we don't have enabled. We
13114 * can't deal with that right now.
13116 mtx_lock(&softc->ctl_lock);
13119 * If we don't have a LUN for this, just toss the sense
13122 port = ctl_io_port(&ctsio->io_hdr);
13123 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13124 if ((targ_lun < CTL_MAX_LUNS)
13125 && (softc->ctl_luns[targ_lun] != NULL))
13126 lun = softc->ctl_luns[targ_lun];
13130 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13132 mtx_lock(&lun->lun_lock);
13134 * Already have CA set for this LUN...toss the sense information.
13136 if (ctl_is_set(lun->have_ca, initidx)) {
13137 mtx_unlock(&lun->lun_lock);
13141 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data,
13142 MIN(sizeof(lun->pending_sense[initidx]),
13143 sizeof(io->scsiio.sense_data)));
13144 ctl_set_mask(lun->have_ca, initidx);
13145 mtx_unlock(&lun->lun_lock);
13148 mtx_unlock(&softc->ctl_lock);
13152 return (CTL_RETVAL_COMPLETE);
13157 * Primary command inlet from frontend ports. All SCSI and task I/O
13158 * requests must go through this function.
13161 ctl_queue(union ctl_io *io)
13163 struct ctl_port *port;
13165 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13168 io->io_hdr.start_time = time_uptime;
13169 getbinuptime(&io->io_hdr.start_bt);
13170 #endif /* CTL_TIME_IO */
13172 /* Map FE-specific LUN ID into global one. */
13173 port = ctl_io_port(&io->io_hdr);
13174 io->io_hdr.nexus.targ_mapped_lun =
13175 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13177 switch (io->io_hdr.io_type) {
13180 if (ctl_debug & CTL_DEBUG_CDB)
13182 ctl_enqueue_incoming(io);
13185 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13189 return (CTL_RETVAL_COMPLETE);
13192 #ifdef CTL_IO_DELAY
13194 ctl_done_timer_wakeup(void *arg)
13198 io = (union ctl_io *)arg;
13201 #endif /* CTL_IO_DELAY */
13204 ctl_serseq_done(union ctl_io *io)
13206 struct ctl_lun *lun;
13208 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13209 if (lun->be_lun == NULL ||
13210 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF)
13212 mtx_lock(&lun->lun_lock);
13213 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE;
13214 ctl_check_blocked(lun);
13215 mtx_unlock(&lun->lun_lock);
13219 ctl_done(union ctl_io *io)
13223 * Enable this to catch duplicate completion issues.
13226 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13227 printf("%s: type %d msg %d cdb %x iptl: "
13228 "%u:%u:%u tag 0x%04x "
13229 "flag %#x status %x\n",
13231 io->io_hdr.io_type,
13232 io->io_hdr.msg_type,
13234 io->io_hdr.nexus.initid,
13235 io->io_hdr.nexus.targ_port,
13236 io->io_hdr.nexus.targ_lun,
13237 (io->io_hdr.io_type ==
13239 io->taskio.tag_num :
13240 io->scsiio.tag_num,
13242 io->io_hdr.status);
13244 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13248 * This is an internal copy of an I/O, and should not go through
13249 * the normal done processing logic.
13251 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13254 #ifdef CTL_IO_DELAY
13255 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13256 struct ctl_lun *lun;
13258 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13260 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13262 struct ctl_lun *lun;
13264 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13267 && (lun->delay_info.done_delay > 0)) {
13269 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
13270 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13271 callout_reset(&io->io_hdr.delay_callout,
13272 lun->delay_info.done_delay * hz,
13273 ctl_done_timer_wakeup, io);
13274 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13275 lun->delay_info.done_delay = 0;
13279 #endif /* CTL_IO_DELAY */
13281 ctl_enqueue_done(io);
13285 ctl_work_thread(void *arg)
13287 struct ctl_thread *thr = (struct ctl_thread *)arg;
13288 struct ctl_softc *softc = thr->ctl_softc;
13292 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13296 * We handle the queues in this order:
13298 * - done queue (to free up resources, unblock other commands)
13302 * If those queues are empty, we break out of the loop and
13305 mtx_lock(&thr->queue_lock);
13306 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13308 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13309 mtx_unlock(&thr->queue_lock);
13310 ctl_handle_isc(io);
13313 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13315 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13316 /* clear any blocked commands, call fe_done */
13317 mtx_unlock(&thr->queue_lock);
13318 ctl_process_done(io);
13321 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13323 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13324 mtx_unlock(&thr->queue_lock);
13325 if (io->io_hdr.io_type == CTL_IO_TASK)
13328 ctl_scsiio_precheck(softc, &io->scsiio);
13331 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13333 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13334 mtx_unlock(&thr->queue_lock);
13335 retval = ctl_scsiio(&io->scsiio);
13336 if (retval != CTL_RETVAL_COMPLETE)
13337 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13341 /* Sleep until we have something to do. */
13342 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13347 ctl_lun_thread(void *arg)
13349 struct ctl_softc *softc = (struct ctl_softc *)arg;
13350 struct ctl_be_lun *be_lun;
13352 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13355 mtx_lock(&softc->ctl_lock);
13356 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13357 if (be_lun != NULL) {
13358 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13359 mtx_unlock(&softc->ctl_lock);
13360 ctl_create_lun(be_lun);
13364 /* Sleep until we have something to do. */
13365 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13366 PDROP | PRIBIO, "-", 0);
13371 ctl_thresh_thread(void *arg)
13373 struct ctl_softc *softc = (struct ctl_softc *)arg;
13374 struct ctl_lun *lun;
13375 struct scsi_da_rw_recovery_page *rwpage;
13376 struct ctl_logical_block_provisioning_page *page;
13378 union ctl_ha_msg msg;
13379 uint64_t thres, val;
13382 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13385 mtx_lock(&softc->ctl_lock);
13386 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13387 if ((lun->flags & CTL_LUN_DISABLED) ||
13388 (lun->flags & CTL_LUN_OFFLINE) ||
13389 lun->backend->lun_attr == NULL)
13391 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
13392 softc->ha_mode == CTL_HA_MODE_XFER)
13394 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT];
13395 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0)
13398 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT];
13399 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13400 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13402 thres = scsi_4btoul(page->descr[i].count);
13403 thres <<= CTL_LBP_EXPONENT;
13404 switch (page->descr[i].resource) {
13406 attr = "blocksavail";
13409 attr = "blocksused";
13412 attr = "poolblocksavail";
13415 attr = "poolblocksused";
13420 mtx_unlock(&softc->ctl_lock); // XXX
13421 val = lun->backend->lun_attr(
13422 lun->be_lun->be_lun, attr);
13423 mtx_lock(&softc->ctl_lock);
13424 if (val == UINT64_MAX)
13426 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13427 == SLBPPD_ARMING_INC)
13428 e = (val >= thres);
13430 e = (val <= thres);
13434 mtx_lock(&lun->lun_lock);
13436 scsi_u64to8b((uint8_t *)&page->descr[i] -
13437 (uint8_t *)page, lun->ua_tpt_info);
13438 if (lun->lasttpt == 0 ||
13439 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13440 lun->lasttpt = time_uptime;
13441 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13447 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13450 mtx_unlock(&lun->lun_lock);
13452 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
13453 /* Send msg to other side. */
13454 bzero(&msg.ua, sizeof(msg.ua));
13455 msg.hdr.msg_type = CTL_MSG_UA;
13456 msg.hdr.nexus.initid = -1;
13457 msg.hdr.nexus.targ_port = -1;
13458 msg.hdr.nexus.targ_lun = lun->lun;
13459 msg.hdr.nexus.targ_mapped_lun = lun->lun;
13461 msg.ua.ua_set = (set > 0);
13462 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES;
13463 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8);
13464 mtx_unlock(&softc->ctl_lock); // XXX
13465 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13466 sizeof(msg.ua), M_WAITOK);
13467 mtx_lock(&softc->ctl_lock);
13470 mtx_unlock(&softc->ctl_lock);
13471 pause("-", CTL_LBP_PERIOD * hz);
13476 ctl_enqueue_incoming(union ctl_io *io)
13478 struct ctl_softc *softc = control_softc;
13479 struct ctl_thread *thr;
13482 idx = (io->io_hdr.nexus.targ_port * 127 +
13483 io->io_hdr.nexus.initid) % worker_threads;
13484 thr = &softc->threads[idx];
13485 mtx_lock(&thr->queue_lock);
13486 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13487 mtx_unlock(&thr->queue_lock);
13492 ctl_enqueue_rtr(union ctl_io *io)
13494 struct ctl_softc *softc = control_softc;
13495 struct ctl_thread *thr;
13497 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13498 mtx_lock(&thr->queue_lock);
13499 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13500 mtx_unlock(&thr->queue_lock);
13505 ctl_enqueue_done(union ctl_io *io)
13507 struct ctl_softc *softc = control_softc;
13508 struct ctl_thread *thr;
13510 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13511 mtx_lock(&thr->queue_lock);
13512 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13513 mtx_unlock(&thr->queue_lock);
13518 ctl_enqueue_isc(union ctl_io *io)
13520 struct ctl_softc *softc = control_softc;
13521 struct ctl_thread *thr;
13523 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13524 mtx_lock(&thr->queue_lock);
13525 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13526 mtx_unlock(&thr->queue_lock);