2 * Copyright (c) 2003-2009 Silicon Graphics International Corp.
3 * Copyright (c) 2012 The FreeBSD Foundation
6 * Portions of this software were developed by Edward Tomasz Napierala
7 * under sponsorship from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
16 * substantially similar to the "NO WARRANTY" disclaimer below
17 * ("Disclaimer") and any redistribution must be conditioned upon
18 * including a substantially similar Disclaimer requirement for further
19 * binary redistribution.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGES.
37 * CAM Target Layer, a SCSI device emulation subsystem.
39 * Author: Ken Merry <ken@FreeBSD.org>
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/ctype.h>
50 #include <sys/kernel.h>
51 #include <sys/types.h>
52 #include <sys/kthread.h>
54 #include <sys/fcntl.h>
56 #include <sys/module.h>
57 #include <sys/mutex.h>
58 #include <sys/condvar.h>
59 #include <sys/malloc.h>
61 #include <sys/ioccom.h>
62 #include <sys/queue.h>
65 #include <sys/endian.h>
66 #include <sys/sysctl.h>
70 #include <cam/scsi/scsi_all.h>
71 #include <cam/scsi/scsi_da.h>
72 #include <cam/ctl/ctl_io.h>
73 #include <cam/ctl/ctl.h>
74 #include <cam/ctl/ctl_frontend.h>
75 #include <cam/ctl/ctl_util.h>
76 #include <cam/ctl/ctl_backend.h>
77 #include <cam/ctl/ctl_ioctl.h>
78 #include <cam/ctl/ctl_ha.h>
79 #include <cam/ctl/ctl_private.h>
80 #include <cam/ctl/ctl_debug.h>
81 #include <cam/ctl/ctl_scsi_all.h>
82 #include <cam/ctl/ctl_error.h>
84 struct ctl_softc *control_softc = NULL;
87 * Size and alignment macros needed for Copan-specific HA hardware. These
88 * can go away when the HA code is re-written, and uses busdma for any
91 #define CTL_ALIGN_8B(target, source, type) \
92 if (((uint32_t)source & 0x7) != 0) \
93 target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\
95 target = (type)source;
97 #define CTL_SIZE_8B(target, size) \
98 if ((size & 0x7) != 0) \
99 target = size + (0x8 - (size & 0x7)); \
103 #define CTL_ALIGN_8B_MARGIN 16
106 * Template mode pages.
110 * Note that these are default values only. The actual values will be
111 * filled in when the user does a mode sense.
113 const static struct copan_debugconf_subpage debugconf_page_default = {
114 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
115 DBGCNF_SUBPAGE_CODE, /* subpage */
116 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
117 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
118 DBGCNF_VERSION, /* page_version */
119 {CTL_TIME_IO_DEFAULT_SECS>>8,
120 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */
123 const static struct copan_debugconf_subpage debugconf_page_changeable = {
124 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
125 DBGCNF_SUBPAGE_CODE, /* subpage */
126 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
127 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
128 0, /* page_version */
129 {0xff,0xff}, /* ctl_time_io_secs */
132 const static struct scsi_da_rw_recovery_page rw_er_page_default = {
133 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
134 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
135 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE,
136 /*read_retry_count*/0,
137 /*correction_span*/0,
138 /*head_offset_count*/0,
139 /*data_strobe_offset_cnt*/0,
140 /*byte8*/SMS_RWER_LBPERE,
141 /*write_retry_count*/0,
143 /*recovery_time_limit*/{0, 0},
146 const static struct scsi_da_rw_recovery_page rw_er_page_changeable = {
147 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
148 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
150 /*read_retry_count*/0,
151 /*correction_span*/0,
152 /*head_offset_count*/0,
153 /*data_strobe_offset_cnt*/0,
155 /*write_retry_count*/0,
157 /*recovery_time_limit*/{0, 0},
160 const static struct scsi_format_page format_page_default = {
161 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
162 /*page_length*/sizeof(struct scsi_format_page) - 2,
163 /*tracks_per_zone*/ {0, 0},
164 /*alt_sectors_per_zone*/ {0, 0},
165 /*alt_tracks_per_zone*/ {0, 0},
166 /*alt_tracks_per_lun*/ {0, 0},
167 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
168 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
169 /*bytes_per_sector*/ {0, 0},
170 /*interleave*/ {0, 0},
171 /*track_skew*/ {0, 0},
172 /*cylinder_skew*/ {0, 0},
174 /*reserved*/ {0, 0, 0}
177 const static struct scsi_format_page format_page_changeable = {
178 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
179 /*page_length*/sizeof(struct scsi_format_page) - 2,
180 /*tracks_per_zone*/ {0, 0},
181 /*alt_sectors_per_zone*/ {0, 0},
182 /*alt_tracks_per_zone*/ {0, 0},
183 /*alt_tracks_per_lun*/ {0, 0},
184 /*sectors_per_track*/ {0, 0},
185 /*bytes_per_sector*/ {0, 0},
186 /*interleave*/ {0, 0},
187 /*track_skew*/ {0, 0},
188 /*cylinder_skew*/ {0, 0},
190 /*reserved*/ {0, 0, 0}
193 const static struct scsi_rigid_disk_page rigid_disk_page_default = {
194 /*page_code*/SMS_RIGID_DISK_PAGE,
195 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
196 /*cylinders*/ {0, 0, 0},
197 /*heads*/ CTL_DEFAULT_HEADS,
198 /*start_write_precomp*/ {0, 0, 0},
199 /*start_reduced_current*/ {0, 0, 0},
200 /*step_rate*/ {0, 0},
201 /*landing_zone_cylinder*/ {0, 0, 0},
202 /*rpl*/ SRDP_RPL_DISABLED,
203 /*rotational_offset*/ 0,
205 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
206 CTL_DEFAULT_ROTATION_RATE & 0xff},
210 const static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
211 /*page_code*/SMS_RIGID_DISK_PAGE,
212 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
213 /*cylinders*/ {0, 0, 0},
215 /*start_write_precomp*/ {0, 0, 0},
216 /*start_reduced_current*/ {0, 0, 0},
217 /*step_rate*/ {0, 0},
218 /*landing_zone_cylinder*/ {0, 0, 0},
220 /*rotational_offset*/ 0,
222 /*rotation_rate*/ {0, 0},
226 const static struct scsi_caching_page caching_page_default = {
227 /*page_code*/SMS_CACHING_PAGE,
228 /*page_length*/sizeof(struct scsi_caching_page) - 2,
229 /*flags1*/ SCP_DISC | SCP_WCE,
231 /*disable_pf_transfer_len*/ {0xff, 0xff},
232 /*min_prefetch*/ {0, 0},
233 /*max_prefetch*/ {0xff, 0xff},
234 /*max_pf_ceiling*/ {0xff, 0xff},
236 /*cache_segments*/ 0,
237 /*cache_seg_size*/ {0, 0},
239 /*non_cache_seg_size*/ {0, 0, 0}
242 const static struct scsi_caching_page caching_page_changeable = {
243 /*page_code*/SMS_CACHING_PAGE,
244 /*page_length*/sizeof(struct scsi_caching_page) - 2,
245 /*flags1*/ SCP_WCE | SCP_RCD,
247 /*disable_pf_transfer_len*/ {0, 0},
248 /*min_prefetch*/ {0, 0},
249 /*max_prefetch*/ {0, 0},
250 /*max_pf_ceiling*/ {0, 0},
252 /*cache_segments*/ 0,
253 /*cache_seg_size*/ {0, 0},
255 /*non_cache_seg_size*/ {0, 0, 0}
258 const static struct scsi_control_page control_page_default = {
259 /*page_code*/SMS_CONTROL_MODE_PAGE,
260 /*page_length*/sizeof(struct scsi_control_page) - 2,
262 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED,
265 /*aen_holdoff_period*/{0, 0},
266 /*busy_timeout_period*/{0, 0},
267 /*extended_selftest_completion_time*/{0, 0}
270 const static struct scsi_control_page control_page_changeable = {
271 /*page_code*/SMS_CONTROL_MODE_PAGE,
272 /*page_length*/sizeof(struct scsi_control_page) - 2,
274 /*queue_flags*/SCP_QUEUE_ALG_MASK,
275 /*eca_and_aen*/SCP_SWP,
277 /*aen_holdoff_period*/{0, 0},
278 /*busy_timeout_period*/{0, 0},
279 /*extended_selftest_completion_time*/{0, 0}
282 const static struct scsi_info_exceptions_page ie_page_default = {
283 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
284 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
285 /*info_flags*/SIEP_FLAGS_DEXCPT,
287 /*interval_timer*/{0, 0, 0, 0},
288 /*report_count*/{0, 0, 0, 0}
291 const static struct scsi_info_exceptions_page ie_page_changeable = {
292 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
293 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
296 /*interval_timer*/{0, 0, 0, 0},
297 /*report_count*/{0, 0, 0, 0}
300 #define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4)
302 const static struct ctl_logical_block_provisioning_page lbp_page_default = {{
303 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
304 /*subpage_code*/0x02,
305 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
307 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
312 /*count*/{0, 0, 0, 0}},
316 /*count*/{0, 0, 0, 0}},
320 /*count*/{0, 0, 0, 0}},
324 /*count*/{0, 0, 0, 0}}
328 const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{
329 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
330 /*subpage_code*/0x02,
331 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
333 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
338 /*count*/{0, 0, 0, 0}},
342 /*count*/{0, 0, 0, 0}},
346 /*count*/{0, 0, 0, 0}},
350 /*count*/{0, 0, 0, 0}}
355 * XXX KDM move these into the softc.
357 static int rcv_sync_msg;
358 static uint8_t ctl_pause_rtr;
360 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
361 static int worker_threads = -1;
362 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
363 &worker_threads, 1, "Number of worker threads");
364 static int ctl_debug = CTL_DEBUG_NONE;
365 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN,
366 &ctl_debug, 0, "Enabled debug flags");
369 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
370 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
371 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0),
372 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
374 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 10
377 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
379 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
381 static int ctl_init(void);
382 void ctl_shutdown(void);
383 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
384 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
385 static void ctl_ioctl_online(void *arg);
386 static void ctl_ioctl_offline(void *arg);
387 static int ctl_ioctl_lun_enable(void *arg, int lun_id);
388 static int ctl_ioctl_lun_disable(void *arg, int lun_id);
389 static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio);
390 static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
391 static int ctl_ioctl_submit_wait(union ctl_io *io);
392 static void ctl_ioctl_datamove(union ctl_io *io);
393 static void ctl_ioctl_done(union ctl_io *io);
394 static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
395 struct ctl_ooa *ooa_hdr,
396 struct ctl_ooa_entry *kern_entries);
397 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
399 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
400 struct ctl_be_lun *be_lun);
401 static int ctl_free_lun(struct ctl_lun *lun);
402 static void ctl_create_lun(struct ctl_be_lun *be_lun);
403 static struct ctl_port * ctl_io_port(struct ctl_io_hdr *io_hdr);
405 static void ctl_failover_change_pages(struct ctl_softc *softc,
406 struct ctl_scsiio *ctsio, int master);
409 static int ctl_do_mode_select(union ctl_io *io);
410 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
411 uint64_t res_key, uint64_t sa_res_key,
412 uint8_t type, uint32_t residx,
413 struct ctl_scsiio *ctsio,
414 struct scsi_per_res_out *cdb,
415 struct scsi_per_res_out_parms* param);
416 static void ctl_pro_preempt_other(struct ctl_lun *lun,
417 union ctl_ha_msg *msg);
418 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
419 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
420 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
421 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
422 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
423 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
424 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
426 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
428 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
429 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
430 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
431 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
432 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
433 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2,
435 static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2);
436 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
437 union ctl_io *pending_io, union ctl_io *ooa_io);
438 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
439 union ctl_io *starting_io);
440 static int ctl_check_blocked(struct ctl_lun *lun);
441 static int ctl_scsiio_lun_check(struct ctl_lun *lun,
442 const struct ctl_cmd_entry *entry,
443 struct ctl_scsiio *ctsio);
444 //static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc);
446 static void ctl_failover(void);
448 static void ctl_clear_ua(struct ctl_softc *ctl_softc, uint32_t initidx,
449 ctl_ua_type ua_type);
450 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
451 struct ctl_scsiio *ctsio);
452 static int ctl_scsiio(struct ctl_scsiio *ctsio);
454 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
455 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
456 ctl_ua_type ua_type);
457 static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io,
458 ctl_ua_type ua_type);
459 static int ctl_abort_task(union ctl_io *io);
460 static int ctl_abort_task_set(union ctl_io *io);
461 static int ctl_i_t_nexus_reset(union ctl_io *io);
462 static void ctl_run_task(union ctl_io *io);
464 static void ctl_datamove_timer_wakeup(void *arg);
465 static void ctl_done_timer_wakeup(void *arg);
466 #endif /* CTL_IO_DELAY */
468 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
469 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
470 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
471 static void ctl_datamove_remote_write(union ctl_io *io);
472 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
473 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
474 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
475 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
476 ctl_ha_dt_cb callback);
477 static void ctl_datamove_remote_read(union ctl_io *io);
478 static void ctl_datamove_remote(union ctl_io *io);
479 static int ctl_process_done(union ctl_io *io);
480 static void ctl_lun_thread(void *arg);
481 static void ctl_thresh_thread(void *arg);
482 static void ctl_work_thread(void *arg);
483 static void ctl_enqueue_incoming(union ctl_io *io);
484 static void ctl_enqueue_rtr(union ctl_io *io);
485 static void ctl_enqueue_done(union ctl_io *io);
487 static void ctl_enqueue_isc(union ctl_io *io);
489 static const struct ctl_cmd_entry *
490 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
491 static const struct ctl_cmd_entry *
492 ctl_validate_command(struct ctl_scsiio *ctsio);
493 static int ctl_cmd_applicable(uint8_t lun_type,
494 const struct ctl_cmd_entry *entry);
497 * Load the serialization table. This isn't very pretty, but is probably
498 * the easiest way to do it.
500 #include "ctl_ser_table.c"
503 * We only need to define open, close and ioctl routines for this driver.
505 static struct cdevsw ctl_cdevsw = {
506 .d_version = D_VERSION,
509 .d_close = ctl_close,
510 .d_ioctl = ctl_ioctl,
515 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
517 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
519 static moduledata_t ctl_moduledata = {
521 ctl_module_event_handler,
525 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
526 MODULE_VERSION(ctl, 1);
528 static struct ctl_frontend ioctl_frontend =
535 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
536 union ctl_ha_msg *msg_info)
538 struct ctl_scsiio *ctsio;
540 if (msg_info->hdr.original_sc == NULL) {
541 printf("%s: original_sc == NULL!\n", __func__);
542 /* XXX KDM now what? */
546 ctsio = &msg_info->hdr.original_sc->scsiio;
547 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
548 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
549 ctsio->io_hdr.status = msg_info->hdr.status;
550 ctsio->scsi_status = msg_info->scsi.scsi_status;
551 ctsio->sense_len = msg_info->scsi.sense_len;
552 ctsio->sense_residual = msg_info->scsi.sense_residual;
553 ctsio->residual = msg_info->scsi.residual;
554 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
555 sizeof(ctsio->sense_data));
556 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
557 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen));
558 ctl_enqueue_isc((union ctl_io *)ctsio);
562 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
563 union ctl_ha_msg *msg_info)
565 struct ctl_scsiio *ctsio;
567 if (msg_info->hdr.serializing_sc == NULL) {
568 printf("%s: serializing_sc == NULL!\n", __func__);
569 /* XXX KDM now what? */
573 ctsio = &msg_info->hdr.serializing_sc->scsiio;
576 * Attempt to catch the situation where an I/O has
577 * been freed, and we're using it again.
579 if (ctsio->io_hdr.io_type == 0xff) {
580 union ctl_io *tmp_io;
581 tmp_io = (union ctl_io *)ctsio;
582 printf("%s: %p use after free!\n", __func__,
584 printf("%s: type %d msg %d cdb %x iptl: "
585 "%d:%d:%d:%d tag 0x%04x "
586 "flag %#x status %x\n",
588 tmp_io->io_hdr.io_type,
589 tmp_io->io_hdr.msg_type,
590 tmp_io->scsiio.cdb[0],
591 tmp_io->io_hdr.nexus.initid.id,
592 tmp_io->io_hdr.nexus.targ_port,
593 tmp_io->io_hdr.nexus.targ_target.id,
594 tmp_io->io_hdr.nexus.targ_lun,
595 (tmp_io->io_hdr.io_type ==
597 tmp_io->taskio.tag_num :
598 tmp_io->scsiio.tag_num,
599 tmp_io->io_hdr.flags,
600 tmp_io->io_hdr.status);
603 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
604 ctl_enqueue_isc((union ctl_io *)ctsio);
608 * ISC (Inter Shelf Communication) event handler. Events from the HA
609 * subsystem come in here.
612 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
614 struct ctl_softc *softc;
616 struct ctl_prio *presio;
617 ctl_ha_status isc_status;
619 softc = control_softc;
624 printf("CTL: Isc Msg event %d\n", event);
626 if (event == CTL_HA_EVT_MSG_RECV) {
627 union ctl_ha_msg msg_info;
629 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
630 sizeof(msg_info), /*wait*/ 0);
632 printf("CTL: msg_type %d\n", msg_info.msg_type);
634 if (isc_status != 0) {
635 printf("Error receiving message, status = %d\n",
640 switch (msg_info.hdr.msg_type) {
641 case CTL_MSG_SERIALIZE:
643 printf("Serialize\n");
645 io = ctl_alloc_io_nowait(softc->othersc_pool);
647 printf("ctl_isc_event_handler: can't allocate "
650 /* Need to set busy and send msg back */
651 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
652 msg_info.hdr.status = CTL_SCSI_ERROR;
653 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY;
654 msg_info.scsi.sense_len = 0;
655 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
656 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){
661 // populate ctsio from msg_info
662 io->io_hdr.io_type = CTL_IO_SCSI;
663 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
664 io->io_hdr.original_sc = msg_info.hdr.original_sc;
666 printf("pOrig %x\n", (int)msg_info.original_sc);
668 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
671 * If we're in serialization-only mode, we don't
672 * want to go through full done processing. Thus
675 * XXX KDM add another flag that is more specific.
677 if (softc->ha_mode == CTL_HA_MODE_SER_ONLY)
678 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
679 io->io_hdr.nexus = msg_info.hdr.nexus;
681 printf("targ %d, port %d, iid %d, lun %d\n",
682 io->io_hdr.nexus.targ_target.id,
683 io->io_hdr.nexus.targ_port,
684 io->io_hdr.nexus.initid.id,
685 io->io_hdr.nexus.targ_lun);
687 io->scsiio.tag_num = msg_info.scsi.tag_num;
688 io->scsiio.tag_type = msg_info.scsi.tag_type;
689 memcpy(io->scsiio.cdb, msg_info.scsi.cdb,
691 if (softc->ha_mode == CTL_HA_MODE_XFER) {
692 const struct ctl_cmd_entry *entry;
694 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
695 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
697 entry->flags & CTL_FLAG_DATA_MASK;
702 /* Performed on the Originating SC, XFER mode only */
703 case CTL_MSG_DATAMOVE: {
704 struct ctl_sg_entry *sgl;
707 io = msg_info.hdr.original_sc;
709 printf("%s: original_sc == NULL!\n", __func__);
710 /* XXX KDM do something here */
713 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
714 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
716 * Keep track of this, we need to send it back over
717 * when the datamove is complete.
719 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
721 if (msg_info.dt.sg_sequence == 0) {
723 * XXX KDM we use the preallocated S/G list
724 * here, but we'll need to change this to
725 * dynamic allocation if we need larger S/G
728 if (msg_info.dt.kern_sg_entries >
729 sizeof(io->io_hdr.remote_sglist) /
730 sizeof(io->io_hdr.remote_sglist[0])) {
731 printf("%s: number of S/G entries "
732 "needed %u > allocated num %zd\n",
734 msg_info.dt.kern_sg_entries,
735 sizeof(io->io_hdr.remote_sglist)/
736 sizeof(io->io_hdr.remote_sglist[0]));
739 * XXX KDM send a message back to
740 * the other side to shut down the
741 * DMA. The error will come back
742 * through via the normal channel.
746 sgl = io->io_hdr.remote_sglist;
748 sizeof(io->io_hdr.remote_sglist));
750 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
752 io->scsiio.kern_sg_entries =
753 msg_info.dt.kern_sg_entries;
754 io->scsiio.rem_sg_entries =
755 msg_info.dt.kern_sg_entries;
756 io->scsiio.kern_data_len =
757 msg_info.dt.kern_data_len;
758 io->scsiio.kern_total_len =
759 msg_info.dt.kern_total_len;
760 io->scsiio.kern_data_resid =
761 msg_info.dt.kern_data_resid;
762 io->scsiio.kern_rel_offset =
763 msg_info.dt.kern_rel_offset;
765 * Clear out per-DMA flags.
767 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK;
769 * Add per-DMA flags that are set for this
770 * particular DMA request.
772 io->io_hdr.flags |= msg_info.dt.flags &
775 sgl = (struct ctl_sg_entry *)
776 io->scsiio.kern_data_ptr;
778 for (i = msg_info.dt.sent_sg_entries, j = 0;
779 i < (msg_info.dt.sent_sg_entries +
780 msg_info.dt.cur_sg_entries); i++, j++) {
781 sgl[i].addr = msg_info.dt.sg_list[j].addr;
782 sgl[i].len = msg_info.dt.sg_list[j].len;
785 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n",
787 msg_info.dt.sg_list[j].addr,
788 msg_info.dt.sg_list[j].len,
789 sgl[i].addr, sgl[i].len, j, i);
793 memcpy(&sgl[msg_info.dt.sent_sg_entries],
795 sizeof(*sgl) * msg_info.dt.cur_sg_entries);
799 * If this is the last piece of the I/O, we've got
800 * the full S/G list. Queue processing in the thread.
801 * Otherwise wait for the next piece.
803 if (msg_info.dt.sg_last != 0)
807 /* Performed on the Serializing (primary) SC, XFER mode only */
808 case CTL_MSG_DATAMOVE_DONE: {
809 if (msg_info.hdr.serializing_sc == NULL) {
810 printf("%s: serializing_sc == NULL!\n",
812 /* XXX KDM now what? */
816 * We grab the sense information here in case
817 * there was a failure, so we can return status
818 * back to the initiator.
820 io = msg_info.hdr.serializing_sc;
821 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
822 io->io_hdr.status = msg_info.hdr.status;
823 io->scsiio.scsi_status = msg_info.scsi.scsi_status;
824 io->scsiio.sense_len = msg_info.scsi.sense_len;
825 io->scsiio.sense_residual =msg_info.scsi.sense_residual;
826 io->io_hdr.port_status = msg_info.scsi.fetd_status;
827 io->scsiio.residual = msg_info.scsi.residual;
828 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data,
829 sizeof(io->scsiio.sense_data));
834 /* Preformed on Originating SC, SER_ONLY mode */
836 io = msg_info.hdr.original_sc;
838 printf("%s: Major Bummer\n", __func__);
842 printf("pOrig %x\n",(int) ctsio);
845 io->io_hdr.msg_type = CTL_MSG_R2R;
846 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
851 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
853 * Performed on the Originating (i.e. secondary) SC in XFER
856 case CTL_MSG_FINISH_IO:
857 if (softc->ha_mode == CTL_HA_MODE_XFER)
858 ctl_isc_handler_finish_xfer(softc,
861 ctl_isc_handler_finish_ser_only(softc,
865 /* Preformed on Originating SC */
866 case CTL_MSG_BAD_JUJU:
867 io = msg_info.hdr.original_sc;
869 printf("%s: Bad JUJU!, original_sc is NULL!\n",
873 ctl_copy_sense_data(&msg_info, io);
875 * IO should have already been cleaned up on other
876 * SC so clear this flag so we won't send a message
877 * back to finish the IO there.
879 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
880 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
882 /* io = msg_info.hdr.serializing_sc; */
883 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
887 /* Handle resets sent from the other side */
888 case CTL_MSG_MANAGE_TASKS: {
889 struct ctl_taskio *taskio;
890 taskio = (struct ctl_taskio *)ctl_alloc_io_nowait(
891 softc->othersc_pool);
892 if (taskio == NULL) {
893 printf("ctl_isc_event_handler: can't allocate "
896 /* should I just call the proper reset func
900 ctl_zero_io((union ctl_io *)taskio);
901 taskio->io_hdr.io_type = CTL_IO_TASK;
902 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
903 taskio->io_hdr.nexus = msg_info.hdr.nexus;
904 taskio->task_action = msg_info.task.task_action;
905 taskio->tag_num = msg_info.task.tag_num;
906 taskio->tag_type = msg_info.task.tag_type;
908 taskio->io_hdr.start_time = time_uptime;
909 getbintime(&taskio->io_hdr.start_bt);
911 cs_prof_gettime(&taskio->io_hdr.start_ticks);
913 #endif /* CTL_TIME_IO */
914 ctl_run_task((union ctl_io *)taskio);
917 /* Persistent Reserve action which needs attention */
918 case CTL_MSG_PERS_ACTION:
919 presio = (struct ctl_prio *)ctl_alloc_io_nowait(
920 softc->othersc_pool);
921 if (presio == NULL) {
922 printf("ctl_isc_event_handler: can't allocate "
925 /* Need to set busy and send msg back */
928 ctl_zero_io((union ctl_io *)presio);
929 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
930 presio->pr_msg = msg_info.pr;
931 ctl_enqueue_isc((union ctl_io *)presio);
933 case CTL_MSG_SYNC_FE:
937 printf("How did I get here?\n");
939 } else if (event == CTL_HA_EVT_MSG_SENT) {
940 if (param != CTL_HA_STATUS_SUCCESS) {
941 printf("Bad status from ctl_ha_msg_send status %d\n",
945 } else if (event == CTL_HA_EVT_DISCONNECT) {
946 printf("CTL: Got a disconnect from Isc\n");
949 printf("ctl_isc_event_handler: Unknown event %d\n", event);
958 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
960 struct scsi_sense_data *sense;
962 sense = &dest->scsiio.sense_data;
963 bcopy(&src->scsi.sense_data, sense, sizeof(*sense));
964 dest->scsiio.scsi_status = src->scsi.scsi_status;
965 dest->scsiio.sense_len = src->scsi.sense_len;
966 dest->io_hdr.status = src->hdr.status;
971 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
975 mtx_assert(&lun->lun_lock, MA_OWNED);
976 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
979 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua;
983 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
987 mtx_assert(&lun->lun_lock, MA_OWNED);
988 for (i = 0; i < CTL_MAX_PORTS; i++) {
989 if (lun->pending_ua[i] == NULL)
991 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
992 if (i * CTL_MAX_INIT_PER_PORT + j == except)
994 lun->pending_ua[i][j] |= ua;
1000 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1004 mtx_assert(&lun->lun_lock, MA_OWNED);
1005 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1008 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua;
1012 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1016 mtx_assert(&lun->lun_lock, MA_OWNED);
1017 for (i = 0; i < CTL_MAX_PORTS; i++) {
1018 if (lun->pending_ua[i] == NULL)
1020 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
1021 if (i * CTL_MAX_INIT_PER_PORT + j == except)
1023 lun->pending_ua[i][j] &= ~ua;
1029 ctl_ha_state_sysctl(SYSCTL_HANDLER_ARGS)
1031 struct ctl_softc *softc = (struct ctl_softc *)arg1;
1032 struct ctl_lun *lun;
1035 if (softc->flags & CTL_FLAG_ACTIVE_SHELF)
1040 error = sysctl_handle_int(oidp, &value, 0, req);
1041 if ((error != 0) || (req->newptr == NULL))
1044 mtx_lock(&softc->ctl_lock);
1046 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1048 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
1049 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1050 mtx_lock(&lun->lun_lock);
1051 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1052 mtx_unlock(&lun->lun_lock);
1054 mtx_unlock(&softc->ctl_lock);
1061 struct ctl_softc *softc;
1063 struct ctl_port *port;
1064 int i, error, retval;
1071 control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1073 softc = control_softc;
1075 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
1078 softc->dev->si_drv1 = softc;
1081 * By default, return a "bad LUN" peripheral qualifier for unknown
1082 * LUNs. The user can override this default using the tunable or
1083 * sysctl. See the comment in ctl_inquiry_std() for more details.
1085 softc->inquiry_pq_no_lun = 1;
1086 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun",
1087 &softc->inquiry_pq_no_lun);
1088 sysctl_ctx_init(&softc->sysctl_ctx);
1089 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1090 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1091 CTLFLAG_RD, 0, "CAM Target Layer");
1093 if (softc->sysctl_tree == NULL) {
1094 printf("%s: unable to allocate sysctl tree\n", __func__);
1095 destroy_dev(softc->dev);
1096 free(control_softc, M_DEVBUF);
1097 control_softc = NULL;
1101 SYSCTL_ADD_INT(&softc->sysctl_ctx,
1102 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
1103 "inquiry_pq_no_lun", CTLFLAG_RW,
1104 &softc->inquiry_pq_no_lun, 0,
1105 "Report no lun possible for invalid LUNs");
1107 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1108 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
1109 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1110 softc->open_count = 0;
1113 * Default to actually sending a SYNCHRONIZE CACHE command down to
1116 softc->flags = CTL_FLAG_REAL_SYNC;
1119 * In Copan's HA scheme, the "master" and "slave" roles are
1120 * figured out through the slot the controller is in. Although it
1121 * is an active/active system, someone has to be in charge.
1123 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1124 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1125 "HA head ID (0 - no HA)");
1126 if (softc->ha_id == 0) {
1127 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1128 softc->is_single = 1;
1129 softc->port_offset = 0;
1131 softc->port_offset = (softc->ha_id - 1) * CTL_MAX_PORTS;
1132 softc->persis_offset = softc->port_offset * CTL_MAX_INIT_PER_PORT;
1134 STAILQ_INIT(&softc->lun_list);
1135 STAILQ_INIT(&softc->pending_lun_queue);
1136 STAILQ_INIT(&softc->fe_list);
1137 STAILQ_INIT(&softc->port_list);
1138 STAILQ_INIT(&softc->be_list);
1139 ctl_tpc_init(softc);
1141 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
1144 printf("ctl: can't allocate %d entry other SC pool, "
1145 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1148 softc->othersc_pool = other_pool;
1150 if (worker_threads <= 0)
1151 worker_threads = max(1, mp_ncpus / 4);
1152 if (worker_threads > CTL_MAX_THREADS)
1153 worker_threads = CTL_MAX_THREADS;
1155 for (i = 0; i < worker_threads; i++) {
1156 struct ctl_thread *thr = &softc->threads[i];
1158 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1159 thr->ctl_softc = softc;
1160 STAILQ_INIT(&thr->incoming_queue);
1161 STAILQ_INIT(&thr->rtr_queue);
1162 STAILQ_INIT(&thr->done_queue);
1163 STAILQ_INIT(&thr->isc_queue);
1165 error = kproc_kthread_add(ctl_work_thread, thr,
1166 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1168 printf("error creating CTL work thread!\n");
1169 ctl_pool_free(other_pool);
1173 error = kproc_kthread_add(ctl_lun_thread, softc,
1174 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
1176 printf("error creating CTL lun thread!\n");
1177 ctl_pool_free(other_pool);
1180 error = kproc_kthread_add(ctl_thresh_thread, softc,
1181 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh");
1183 printf("error creating CTL threshold thread!\n");
1184 ctl_pool_free(other_pool);
1189 * Initialize the ioctl front end.
1191 ctl_frontend_register(&ioctl_frontend);
1192 port = &softc->ioctl_info.port;
1193 port->frontend = &ioctl_frontend;
1194 sprintf(softc->ioctl_info.port_name, "ioctl");
1195 port->port_type = CTL_PORT_IOCTL;
1196 port->num_requested_ctl_io = 100;
1197 port->port_name = softc->ioctl_info.port_name;
1198 port->port_online = ctl_ioctl_online;
1199 port->port_offline = ctl_ioctl_offline;
1200 port->onoff_arg = &softc->ioctl_info;
1201 port->lun_enable = ctl_ioctl_lun_enable;
1202 port->lun_disable = ctl_ioctl_lun_disable;
1203 port->targ_lun_arg = &softc->ioctl_info;
1204 port->fe_datamove = ctl_ioctl_datamove;
1205 port->fe_done = ctl_ioctl_done;
1206 port->max_targets = 15;
1207 port->max_target_id = 15;
1209 if (ctl_port_register(&softc->ioctl_info.port) != 0) {
1210 printf("ctl: ioctl front end registration failed, will "
1211 "continue anyway\n");
1214 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1215 OID_AUTO, "ha_state", CTLTYPE_INT | CTLFLAG_RWTUN,
1216 softc, 0, ctl_ha_state_sysctl, "I", "HA state for this head");
1219 if (sizeof(struct callout) > CTL_TIMER_BYTES) {
1220 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n",
1221 sizeof(struct callout), CTL_TIMER_BYTES);
1224 #endif /* CTL_IO_DELAY */
1232 struct ctl_softc *softc;
1233 struct ctl_lun *lun, *next_lun;
1235 softc = (struct ctl_softc *)control_softc;
1237 if (ctl_port_deregister(&softc->ioctl_info.port) != 0)
1238 printf("ctl: ioctl front end deregistration failed\n");
1240 mtx_lock(&softc->ctl_lock);
1245 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
1246 next_lun = STAILQ_NEXT(lun, links);
1250 mtx_unlock(&softc->ctl_lock);
1252 ctl_frontend_deregister(&ioctl_frontend);
1255 ctl_shutdown_thread(softc->work_thread);
1256 mtx_destroy(&softc->queue_lock);
1259 ctl_tpc_shutdown(softc);
1260 uma_zdestroy(softc->io_zone);
1261 mtx_destroy(&softc->ctl_lock);
1263 destroy_dev(softc->dev);
1265 sysctl_ctx_free(&softc->sysctl_ctx);
1267 free(control_softc, M_DEVBUF);
1268 control_softc = NULL;
1272 ctl_module_event_handler(module_t mod, int what, void *arg)
1277 return (ctl_init());
1281 return (EOPNOTSUPP);
1286 * XXX KDM should we do some access checks here? Bump a reference count to
1287 * prevent a CTL module from being unloaded while someone has it open?
1290 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1296 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1302 ctl_port_enable(ctl_port_type port_type)
1304 struct ctl_softc *softc = control_softc;
1305 struct ctl_port *port;
1307 if (softc->is_single == 0) {
1308 union ctl_ha_msg msg_info;
1312 printf("%s: HA mode, synchronizing frontend enable\n",
1315 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE;
1316 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1317 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) {
1318 printf("Sync msg send error retval %d\n", isc_retval);
1320 if (!rcv_sync_msg) {
1321 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
1322 sizeof(msg_info), 1);
1325 printf("CTL:Frontend Enable\n");
1327 printf("%s: single mode, skipping frontend synchronization\n",
1332 STAILQ_FOREACH(port, &softc->port_list, links) {
1333 if (port_type & port->port_type)
1336 printf("port %d\n", port->targ_port);
1338 ctl_port_online(port);
1346 ctl_port_disable(ctl_port_type port_type)
1348 struct ctl_softc *softc;
1349 struct ctl_port *port;
1351 softc = control_softc;
1353 STAILQ_FOREACH(port, &softc->port_list, links) {
1354 if (port_type & port->port_type)
1355 ctl_port_offline(port);
1362 * Returns 0 for success, 1 for failure.
1363 * Currently the only failure mode is if there aren't enough entries
1364 * allocated. So, in case of a failure, look at num_entries_dropped,
1365 * reallocate and try again.
1368 ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced,
1369 int *num_entries_filled, int *num_entries_dropped,
1370 ctl_port_type port_type, int no_virtual)
1372 struct ctl_softc *softc;
1373 struct ctl_port *port;
1374 int entries_dropped, entries_filled;
1378 softc = control_softc;
1382 entries_dropped = 0;
1385 mtx_lock(&softc->ctl_lock);
1386 STAILQ_FOREACH(port, &softc->port_list, links) {
1387 struct ctl_port_entry *entry;
1389 if ((port->port_type & port_type) == 0)
1392 if ((no_virtual != 0)
1393 && (port->virtual_port != 0))
1396 if (entries_filled >= num_entries_alloced) {
1400 entry = &entries[i];
1402 entry->port_type = port->port_type;
1403 strlcpy(entry->port_name, port->port_name,
1404 sizeof(entry->port_name));
1405 entry->physical_port = port->physical_port;
1406 entry->virtual_port = port->virtual_port;
1407 entry->wwnn = port->wwnn;
1408 entry->wwpn = port->wwpn;
1414 mtx_unlock(&softc->ctl_lock);
1416 if (entries_dropped > 0)
1419 *num_entries_dropped = entries_dropped;
1420 *num_entries_filled = entries_filled;
1426 ctl_ioctl_online(void *arg)
1428 struct ctl_ioctl_info *ioctl_info;
1430 ioctl_info = (struct ctl_ioctl_info *)arg;
1432 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED;
1436 ctl_ioctl_offline(void *arg)
1438 struct ctl_ioctl_info *ioctl_info;
1440 ioctl_info = (struct ctl_ioctl_info *)arg;
1442 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED;
1446 * Remove an initiator by port number and initiator ID.
1447 * Returns 0 for success, -1 for failure.
1450 ctl_remove_initiator(struct ctl_port *port, int iid)
1452 struct ctl_softc *softc = control_softc;
1454 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1456 if (iid > CTL_MAX_INIT_PER_PORT) {
1457 printf("%s: initiator ID %u > maximun %u!\n",
1458 __func__, iid, CTL_MAX_INIT_PER_PORT);
1462 mtx_lock(&softc->ctl_lock);
1463 port->wwpn_iid[iid].in_use--;
1464 port->wwpn_iid[iid].last_use = time_uptime;
1465 mtx_unlock(&softc->ctl_lock);
1471 * Add an initiator to the initiator map.
1472 * Returns iid for success, < 0 for failure.
1475 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
1477 struct ctl_softc *softc = control_softc;
1481 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1483 if (iid >= CTL_MAX_INIT_PER_PORT) {
1484 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
1485 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
1490 mtx_lock(&softc->ctl_lock);
1492 if (iid < 0 && (wwpn != 0 || name != NULL)) {
1493 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1494 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
1498 if (name != NULL && port->wwpn_iid[i].name != NULL &&
1499 strcmp(name, port->wwpn_iid[i].name) == 0) {
1507 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1508 if (port->wwpn_iid[i].in_use == 0 &&
1509 port->wwpn_iid[i].wwpn == 0 &&
1510 port->wwpn_iid[i].name == NULL) {
1519 best_time = INT32_MAX;
1520 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1521 if (port->wwpn_iid[i].in_use == 0) {
1522 if (port->wwpn_iid[i].last_use < best_time) {
1524 best_time = port->wwpn_iid[i].last_use;
1532 mtx_unlock(&softc->ctl_lock);
1537 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
1539 * This is not an error yet.
1541 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
1543 printf("%s: port %d iid %u WWPN %#jx arrived"
1544 " again\n", __func__, port->targ_port,
1545 iid, (uintmax_t)wwpn);
1549 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
1550 strcmp(name, port->wwpn_iid[iid].name) == 0) {
1552 printf("%s: port %d iid %u name '%s' arrived"
1553 " again\n", __func__, port->targ_port,
1560 * This is an error, but what do we do about it? The
1561 * driver is telling us we have a new WWPN for this
1562 * initiator ID, so we pretty much need to use it.
1564 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
1565 " but WWPN %#jx '%s' is still at that address\n",
1566 __func__, port->targ_port, iid, wwpn, name,
1567 (uintmax_t)port->wwpn_iid[iid].wwpn,
1568 port->wwpn_iid[iid].name);
1571 * XXX KDM clear have_ca and ua_pending on each LUN for
1576 free(port->wwpn_iid[iid].name, M_CTL);
1577 port->wwpn_iid[iid].name = name;
1578 port->wwpn_iid[iid].wwpn = wwpn;
1579 port->wwpn_iid[iid].in_use++;
1580 mtx_unlock(&softc->ctl_lock);
1586 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
1590 switch (port->port_type) {
1593 struct scsi_transportid_fcp *id =
1594 (struct scsi_transportid_fcp *)buf;
1595 if (port->wwpn_iid[iid].wwpn == 0)
1597 memset(id, 0, sizeof(*id));
1598 id->format_protocol = SCSI_PROTO_FC;
1599 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
1600 return (sizeof(*id));
1602 case CTL_PORT_ISCSI:
1604 struct scsi_transportid_iscsi_port *id =
1605 (struct scsi_transportid_iscsi_port *)buf;
1606 if (port->wwpn_iid[iid].name == NULL)
1609 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
1611 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
1612 len = roundup2(min(len, 252), 4);
1613 scsi_ulto2b(len, id->additional_length);
1614 return (sizeof(*id) + len);
1618 struct scsi_transportid_sas *id =
1619 (struct scsi_transportid_sas *)buf;
1620 if (port->wwpn_iid[iid].wwpn == 0)
1622 memset(id, 0, sizeof(*id));
1623 id->format_protocol = SCSI_PROTO_SAS;
1624 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
1625 return (sizeof(*id));
1629 struct scsi_transportid_spi *id =
1630 (struct scsi_transportid_spi *)buf;
1631 memset(id, 0, sizeof(*id));
1632 id->format_protocol = SCSI_PROTO_SPI;
1633 scsi_ulto2b(iid, id->scsi_addr);
1634 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
1635 return (sizeof(*id));
1641 ctl_ioctl_lun_enable(void *arg, int lun_id)
1647 ctl_ioctl_lun_disable(void *arg, int lun_id)
1653 * Data movement routine for the CTL ioctl frontend port.
1656 ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio)
1658 struct ctl_sg_entry *ext_sglist, *kern_sglist;
1659 struct ctl_sg_entry ext_entry, kern_entry;
1660 int ext_sglen, ext_sg_entries, kern_sg_entries;
1661 int ext_sg_start, ext_offset;
1662 int len_to_copy, len_copied;
1663 int kern_watermark, ext_watermark;
1664 int ext_sglist_malloced;
1667 ext_sglist_malloced = 0;
1671 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n"));
1674 * If this flag is set, fake the data transfer.
1676 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) {
1677 ctsio->ext_data_filled = ctsio->ext_data_len;
1682 * To simplify things here, if we have a single buffer, stick it in
1683 * a S/G entry and just make it a single entry S/G list.
1685 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) {
1688 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist);
1690 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL,
1692 ext_sglist_malloced = 1;
1693 if (copyin(ctsio->ext_data_ptr, ext_sglist,
1695 ctl_set_internal_failure(ctsio,
1700 ext_sg_entries = ctsio->ext_sg_entries;
1702 for (i = 0; i < ext_sg_entries; i++) {
1703 if ((len_seen + ext_sglist[i].len) >=
1704 ctsio->ext_data_filled) {
1706 ext_offset = ctsio->ext_data_filled - len_seen;
1709 len_seen += ext_sglist[i].len;
1712 ext_sglist = &ext_entry;
1713 ext_sglist->addr = ctsio->ext_data_ptr;
1714 ext_sglist->len = ctsio->ext_data_len;
1717 ext_offset = ctsio->ext_data_filled;
1720 if (ctsio->kern_sg_entries > 0) {
1721 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
1722 kern_sg_entries = ctsio->kern_sg_entries;
1724 kern_sglist = &kern_entry;
1725 kern_sglist->addr = ctsio->kern_data_ptr;
1726 kern_sglist->len = ctsio->kern_data_len;
1727 kern_sg_entries = 1;
1732 ext_watermark = ext_offset;
1734 for (i = ext_sg_start, j = 0;
1735 i < ext_sg_entries && j < kern_sg_entries;) {
1736 uint8_t *ext_ptr, *kern_ptr;
1738 len_to_copy = MIN(ext_sglist[i].len - ext_watermark,
1739 kern_sglist[j].len - kern_watermark);
1741 ext_ptr = (uint8_t *)ext_sglist[i].addr;
1742 ext_ptr = ext_ptr + ext_watermark;
1743 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
1747 panic("need to implement bus address support");
1749 kern_ptr = bus_to_virt(kern_sglist[j].addr);
1752 kern_ptr = (uint8_t *)kern_sglist[j].addr;
1753 kern_ptr = kern_ptr + kern_watermark;
1755 kern_watermark += len_to_copy;
1756 ext_watermark += len_to_copy;
1758 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
1760 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1761 "bytes to user\n", len_to_copy));
1762 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1763 "to %p\n", kern_ptr, ext_ptr));
1764 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) {
1765 ctl_set_internal_failure(ctsio,
1771 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1772 "bytes from user\n", len_to_copy));
1773 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1774 "to %p\n", ext_ptr, kern_ptr));
1775 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){
1776 ctl_set_internal_failure(ctsio,
1783 len_copied += len_to_copy;
1785 if (ext_sglist[i].len == ext_watermark) {
1790 if (kern_sglist[j].len == kern_watermark) {
1796 ctsio->ext_data_filled += len_copied;
1798 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, "
1799 "kern_sg_entries: %d\n", ext_sg_entries,
1801 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, "
1802 "kern_data_len = %d\n", ctsio->ext_data_len,
1803 ctsio->kern_data_len));
1806 /* XXX KDM set residual?? */
1809 if (ext_sglist_malloced != 0)
1810 free(ext_sglist, M_CTL);
1812 return (CTL_RETVAL_COMPLETE);
1816 * Serialize a command that went down the "wrong" side, and so was sent to
1817 * this controller for execution. The logic is a little different than the
1818 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
1819 * sent back to the other side, but in the success case, we execute the
1820 * command on this side (XFER mode) or tell the other side to execute it
1824 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
1826 struct ctl_softc *softc;
1827 union ctl_ha_msg msg_info;
1828 struct ctl_lun *lun;
1832 softc = control_softc;
1834 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
1835 lun = softc->ctl_luns[targ_lun];
1839 * Why isn't LUN defined? The other side wouldn't
1840 * send a cmd if the LUN is undefined.
1842 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__);
1844 /* "Logical unit not supported" */
1845 ctl_set_sense_data(&msg_info.scsi.sense_data,
1847 /*sense_format*/SSD_TYPE_NONE,
1848 /*current_error*/ 1,
1849 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1854 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1855 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1856 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1857 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1858 msg_info.hdr.serializing_sc = NULL;
1859 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1860 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1861 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1867 mtx_lock(&lun->lun_lock);
1868 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1870 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
1871 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
1873 case CTL_ACTION_BLOCK:
1874 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
1875 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
1878 case CTL_ACTION_PASS:
1879 case CTL_ACTION_SKIP:
1880 if (softc->ha_mode == CTL_HA_MODE_XFER) {
1881 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
1882 ctl_enqueue_rtr((union ctl_io *)ctsio);
1885 /* send msg back to other side */
1886 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1887 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
1888 msg_info.hdr.msg_type = CTL_MSG_R2R;
1890 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc);
1892 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1893 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1897 case CTL_ACTION_OVERLAP:
1898 /* OVERLAPPED COMMANDS ATTEMPTED */
1899 ctl_set_sense_data(&msg_info.scsi.sense_data,
1901 /*sense_format*/SSD_TYPE_NONE,
1902 /*current_error*/ 1,
1903 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1908 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1909 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1910 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1911 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1912 msg_info.hdr.serializing_sc = NULL;
1913 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1915 printf("BAD JUJU:Major Bummer Overlap\n");
1917 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1919 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1920 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1923 case CTL_ACTION_OVERLAP_TAG:
1924 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */
1925 ctl_set_sense_data(&msg_info.scsi.sense_data,
1927 /*sense_format*/SSD_TYPE_NONE,
1928 /*current_error*/ 1,
1929 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1931 /*ascq*/ ctsio->tag_num & 0xff,
1934 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1935 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1936 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1937 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1938 msg_info.hdr.serializing_sc = NULL;
1939 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1941 printf("BAD JUJU:Major Bummer Overlap Tag\n");
1943 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1945 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1946 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1949 case CTL_ACTION_ERROR:
1951 /* "Internal target failure" */
1952 ctl_set_sense_data(&msg_info.scsi.sense_data,
1954 /*sense_format*/SSD_TYPE_NONE,
1955 /*current_error*/ 1,
1956 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
1961 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1962 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1963 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1964 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1965 msg_info.hdr.serializing_sc = NULL;
1966 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1968 printf("BAD JUJU:Major Bummer HW Error\n");
1970 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1972 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1973 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1977 mtx_unlock(&lun->lun_lock);
1982 ctl_ioctl_submit_wait(union ctl_io *io)
1984 struct ctl_fe_ioctl_params params;
1985 ctl_fe_ioctl_state last_state;
1990 bzero(¶ms, sizeof(params));
1992 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF);
1993 cv_init(¶ms.sem, "ctlioccv");
1994 params.state = CTL_IOCTL_INPROG;
1995 last_state = params.state;
1997 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms;
1999 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n"));
2001 /* This shouldn't happen */
2002 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE)
2008 mtx_lock(¶ms.ioctl_mtx);
2010 * Check the state here, and don't sleep if the state has
2011 * already changed (i.e. wakeup has already occured, but we
2012 * weren't waiting yet).
2014 if (params.state == last_state) {
2015 /* XXX KDM cv_wait_sig instead? */
2016 cv_wait(¶ms.sem, ¶ms.ioctl_mtx);
2018 last_state = params.state;
2020 switch (params.state) {
2021 case CTL_IOCTL_INPROG:
2022 /* Why did we wake up? */
2023 /* XXX KDM error here? */
2024 mtx_unlock(¶ms.ioctl_mtx);
2026 case CTL_IOCTL_DATAMOVE:
2027 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n"));
2030 * change last_state back to INPROG to avoid
2031 * deadlock on subsequent data moves.
2033 params.state = last_state = CTL_IOCTL_INPROG;
2035 mtx_unlock(¶ms.ioctl_mtx);
2036 ctl_ioctl_do_datamove(&io->scsiio);
2038 * Note that in some cases, most notably writes,
2039 * this will queue the I/O and call us back later.
2040 * In other cases, generally reads, this routine
2041 * will immediately call back and wake us up,
2042 * probably using our own context.
2044 io->scsiio.be_move_done(io);
2046 case CTL_IOCTL_DONE:
2047 mtx_unlock(¶ms.ioctl_mtx);
2048 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n"));
2052 mtx_unlock(¶ms.ioctl_mtx);
2053 /* XXX KDM error here? */
2056 } while (done == 0);
2058 mtx_destroy(¶ms.ioctl_mtx);
2059 cv_destroy(¶ms.sem);
2061 return (CTL_RETVAL_COMPLETE);
2065 ctl_ioctl_datamove(union ctl_io *io)
2067 struct ctl_fe_ioctl_params *params;
2069 params = (struct ctl_fe_ioctl_params *)
2070 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2072 mtx_lock(¶ms->ioctl_mtx);
2073 params->state = CTL_IOCTL_DATAMOVE;
2074 cv_broadcast(¶ms->sem);
2075 mtx_unlock(¶ms->ioctl_mtx);
2079 ctl_ioctl_done(union ctl_io *io)
2081 struct ctl_fe_ioctl_params *params;
2083 params = (struct ctl_fe_ioctl_params *)
2084 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2086 mtx_lock(¶ms->ioctl_mtx);
2087 params->state = CTL_IOCTL_DONE;
2088 cv_broadcast(¶ms->sem);
2089 mtx_unlock(¶ms->ioctl_mtx);
2093 * Returns 0 for success, errno for failure.
2096 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2097 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2104 mtx_lock(&lun->lun_lock);
2105 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2106 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2108 struct ctl_ooa_entry *entry;
2111 * If we've got more than we can fit, just count the
2112 * remaining entries.
2114 if (*cur_fill_num >= ooa_hdr->alloc_num)
2117 entry = &kern_entries[*cur_fill_num];
2119 entry->tag_num = io->scsiio.tag_num;
2120 entry->lun_num = lun->lun;
2122 entry->start_bt = io->io_hdr.start_bt;
2124 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2125 entry->cdb_len = io->scsiio.cdb_len;
2126 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2127 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2129 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2130 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2132 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2133 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2135 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2136 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2138 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2139 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2141 mtx_unlock(&lun->lun_lock);
2147 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2148 size_t error_str_len)
2152 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2154 if (copyin(user_addr, kptr, len) != 0) {
2155 snprintf(error_str, error_str_len, "Error copying %d bytes "
2156 "from user address %p to kernel address %p", len,
2166 ctl_free_args(int num_args, struct ctl_be_arg *args)
2173 for (i = 0; i < num_args; i++) {
2174 free(args[i].kname, M_CTL);
2175 free(args[i].kvalue, M_CTL);
2181 static struct ctl_be_arg *
2182 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
2183 char *error_str, size_t error_str_len)
2185 struct ctl_be_arg *args;
2188 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
2189 error_str, error_str_len);
2194 for (i = 0; i < num_args; i++) {
2195 args[i].kname = NULL;
2196 args[i].kvalue = NULL;
2199 for (i = 0; i < num_args; i++) {
2202 args[i].kname = ctl_copyin_alloc(args[i].name,
2203 args[i].namelen, error_str, error_str_len);
2204 if (args[i].kname == NULL)
2207 if (args[i].kname[args[i].namelen - 1] != '\0') {
2208 snprintf(error_str, error_str_len, "Argument %d "
2209 "name is not NUL-terminated", i);
2213 if (args[i].flags & CTL_BEARG_RD) {
2214 tmpptr = ctl_copyin_alloc(args[i].value,
2215 args[i].vallen, error_str, error_str_len);
2218 if ((args[i].flags & CTL_BEARG_ASCII)
2219 && (tmpptr[args[i].vallen - 1] != '\0')) {
2220 snprintf(error_str, error_str_len, "Argument "
2221 "%d value is not NUL-terminated", i);
2224 args[i].kvalue = tmpptr;
2226 args[i].kvalue = malloc(args[i].vallen,
2227 M_CTL, M_WAITOK | M_ZERO);
2234 ctl_free_args(num_args, args);
2240 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2244 for (i = 0; i < num_args; i++) {
2245 if (args[i].flags & CTL_BEARG_WR)
2246 copyout(args[i].kvalue, args[i].value, args[i].vallen);
2251 * Escape characters that are illegal or not recommended in XML.
2254 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2256 char *end = str + size;
2261 for (; *str && str < end; str++) {
2264 retval = sbuf_printf(sb, "&");
2267 retval = sbuf_printf(sb, ">");
2270 retval = sbuf_printf(sb, "<");
2273 retval = sbuf_putc(sb, *str);
2286 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2288 struct scsi_vpd_id_descriptor *desc;
2291 if (id == NULL || id->len < 4)
2293 desc = (struct scsi_vpd_id_descriptor *)id->data;
2294 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2295 case SVPD_ID_TYPE_T10:
2296 sbuf_printf(sb, "t10.");
2298 case SVPD_ID_TYPE_EUI64:
2299 sbuf_printf(sb, "eui.");
2301 case SVPD_ID_TYPE_NAA:
2302 sbuf_printf(sb, "naa.");
2304 case SVPD_ID_TYPE_SCSI_NAME:
2307 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2308 case SVPD_ID_CODESET_BINARY:
2309 for (i = 0; i < desc->length; i++)
2310 sbuf_printf(sb, "%02x", desc->identifier[i]);
2312 case SVPD_ID_CODESET_ASCII:
2313 sbuf_printf(sb, "%.*s", (int)desc->length,
2314 (char *)desc->identifier);
2316 case SVPD_ID_CODESET_UTF8:
2317 sbuf_printf(sb, "%s", (char *)desc->identifier);
2323 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2326 struct ctl_softc *softc;
2329 softc = control_softc;
2339 * If we haven't been "enabled", don't allow any SCSI I/O
2342 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) {
2347 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref);
2350 * Need to save the pool reference so it doesn't get
2351 * spammed by the user's ctl_io.
2353 pool_tmp = io->io_hdr.pool;
2354 memcpy(io, (void *)addr, sizeof(*io));
2355 io->io_hdr.pool = pool_tmp;
2358 * No status yet, so make sure the status is set properly.
2360 io->io_hdr.status = CTL_STATUS_NONE;
2363 * The user sets the initiator ID, target and LUN IDs.
2365 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port;
2366 io->io_hdr.flags |= CTL_FLAG_USER_REQ;
2367 if ((io->io_hdr.io_type == CTL_IO_SCSI)
2368 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED))
2369 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++;
2371 retval = ctl_ioctl_submit_wait(io);
2378 memcpy((void *)addr, io, sizeof(*io));
2380 /* return this to our pool */
2385 case CTL_ENABLE_PORT:
2386 case CTL_DISABLE_PORT:
2387 case CTL_SET_PORT_WWNS: {
2388 struct ctl_port *port;
2389 struct ctl_port_entry *entry;
2391 entry = (struct ctl_port_entry *)addr;
2393 mtx_lock(&softc->ctl_lock);
2394 STAILQ_FOREACH(port, &softc->port_list, links) {
2400 if ((entry->port_type == CTL_PORT_NONE)
2401 && (entry->targ_port == port->targ_port)) {
2403 * If the user only wants to enable or
2404 * disable or set WWNs on a specific port,
2405 * do the operation and we're done.
2409 } else if (entry->port_type & port->port_type) {
2411 * Compare the user's type mask with the
2412 * particular frontend type to see if we
2419 * Make sure the user isn't trying to set
2420 * WWNs on multiple ports at the same time.
2422 if (cmd == CTL_SET_PORT_WWNS) {
2423 printf("%s: Can't set WWNs on "
2424 "multiple ports\n", __func__);
2431 * XXX KDM we have to drop the lock here,
2432 * because the online/offline operations
2433 * can potentially block. We need to
2434 * reference count the frontends so they
2437 mtx_unlock(&softc->ctl_lock);
2439 if (cmd == CTL_ENABLE_PORT) {
2440 ctl_port_online(port);
2441 } else if (cmd == CTL_DISABLE_PORT) {
2442 ctl_port_offline(port);
2445 mtx_lock(&softc->ctl_lock);
2447 if (cmd == CTL_SET_PORT_WWNS)
2448 ctl_port_set_wwns(port,
2449 (entry->flags & CTL_PORT_WWNN_VALID) ?
2451 (entry->flags & CTL_PORT_WWPN_VALID) ?
2452 1 : 0, entry->wwpn);
2457 mtx_unlock(&softc->ctl_lock);
2460 case CTL_GET_PORT_LIST: {
2461 struct ctl_port *port;
2462 struct ctl_port_list *list;
2465 list = (struct ctl_port_list *)addr;
2467 if (list->alloc_len != (list->alloc_num *
2468 sizeof(struct ctl_port_entry))) {
2469 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != "
2470 "alloc_num %u * sizeof(struct ctl_port_entry) "
2471 "%zu\n", __func__, list->alloc_len,
2472 list->alloc_num, sizeof(struct ctl_port_entry));
2478 list->dropped_num = 0;
2480 mtx_lock(&softc->ctl_lock);
2481 STAILQ_FOREACH(port, &softc->port_list, links) {
2482 struct ctl_port_entry entry, *list_entry;
2484 if (list->fill_num >= list->alloc_num) {
2485 list->dropped_num++;
2489 entry.port_type = port->port_type;
2490 strlcpy(entry.port_name, port->port_name,
2491 sizeof(entry.port_name));
2492 entry.targ_port = port->targ_port;
2493 entry.physical_port = port->physical_port;
2494 entry.virtual_port = port->virtual_port;
2495 entry.wwnn = port->wwnn;
2496 entry.wwpn = port->wwpn;
2497 if (port->status & CTL_PORT_STATUS_ONLINE)
2502 list_entry = &list->entries[i];
2504 retval = copyout(&entry, list_entry, sizeof(entry));
2506 printf("%s: CTL_GET_PORT_LIST: copyout "
2507 "returned %d\n", __func__, retval);
2512 list->fill_len += sizeof(entry);
2514 mtx_unlock(&softc->ctl_lock);
2517 * If this is non-zero, we had a copyout fault, so there's
2518 * probably no point in attempting to set the status inside
2524 if (list->dropped_num > 0)
2525 list->status = CTL_PORT_LIST_NEED_MORE_SPACE;
2527 list->status = CTL_PORT_LIST_OK;
2530 case CTL_DUMP_OOA: {
2531 struct ctl_lun *lun;
2536 mtx_lock(&softc->ctl_lock);
2537 printf("Dumping OOA queues:\n");
2538 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2539 mtx_lock(&lun->lun_lock);
2540 for (io = (union ctl_io *)TAILQ_FIRST(
2541 &lun->ooa_queue); io != NULL;
2542 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2544 sbuf_new(&sb, printbuf, sizeof(printbuf),
2546 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ",
2550 CTL_FLAG_BLOCKED) ? "" : " BLOCKED",
2552 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
2554 CTL_FLAG_ABORT) ? " ABORT" : "",
2556 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : "");
2557 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
2559 printf("%s\n", sbuf_data(&sb));
2561 mtx_unlock(&lun->lun_lock);
2563 printf("OOA queues dump done\n");
2564 mtx_unlock(&softc->ctl_lock);
2568 struct ctl_lun *lun;
2569 struct ctl_ooa *ooa_hdr;
2570 struct ctl_ooa_entry *entries;
2571 uint32_t cur_fill_num;
2573 ooa_hdr = (struct ctl_ooa *)addr;
2575 if ((ooa_hdr->alloc_len == 0)
2576 || (ooa_hdr->alloc_num == 0)) {
2577 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2578 "must be non-zero\n", __func__,
2579 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2584 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2585 sizeof(struct ctl_ooa_entry))) {
2586 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2587 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2588 __func__, ooa_hdr->alloc_len,
2589 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2594 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2595 if (entries == NULL) {
2596 printf("%s: could not allocate %d bytes for OOA "
2597 "dump\n", __func__, ooa_hdr->alloc_len);
2602 mtx_lock(&softc->ctl_lock);
2603 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2604 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS)
2605 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2606 mtx_unlock(&softc->ctl_lock);
2607 free(entries, M_CTL);
2608 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2609 __func__, (uintmax_t)ooa_hdr->lun_num);
2616 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2617 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2618 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2624 mtx_unlock(&softc->ctl_lock);
2625 free(entries, M_CTL);
2629 lun = softc->ctl_luns[ooa_hdr->lun_num];
2631 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr,
2634 mtx_unlock(&softc->ctl_lock);
2636 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2637 ooa_hdr->fill_len = ooa_hdr->fill_num *
2638 sizeof(struct ctl_ooa_entry);
2639 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2641 printf("%s: error copying out %d bytes for OOA dump\n",
2642 __func__, ooa_hdr->fill_len);
2645 getbintime(&ooa_hdr->cur_bt);
2647 if (cur_fill_num > ooa_hdr->alloc_num) {
2648 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2649 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2651 ooa_hdr->dropped_num = 0;
2652 ooa_hdr->status = CTL_OOA_OK;
2655 free(entries, M_CTL);
2658 case CTL_CHECK_OOA: {
2660 struct ctl_lun *lun;
2661 struct ctl_ooa_info *ooa_info;
2664 ooa_info = (struct ctl_ooa_info *)addr;
2666 if (ooa_info->lun_id >= CTL_MAX_LUNS) {
2667 ooa_info->status = CTL_OOA_INVALID_LUN;
2670 mtx_lock(&softc->ctl_lock);
2671 lun = softc->ctl_luns[ooa_info->lun_id];
2673 mtx_unlock(&softc->ctl_lock);
2674 ooa_info->status = CTL_OOA_INVALID_LUN;
2677 mtx_lock(&lun->lun_lock);
2678 mtx_unlock(&softc->ctl_lock);
2679 ooa_info->num_entries = 0;
2680 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
2681 io != NULL; io = (union ctl_io *)TAILQ_NEXT(
2682 &io->io_hdr, ooa_links)) {
2683 ooa_info->num_entries++;
2685 mtx_unlock(&lun->lun_lock);
2687 ooa_info->status = CTL_OOA_SUCCESS;
2691 case CTL_DELAY_IO: {
2692 struct ctl_io_delay_info *delay_info;
2694 struct ctl_lun *lun;
2695 #endif /* CTL_IO_DELAY */
2697 delay_info = (struct ctl_io_delay_info *)addr;
2700 mtx_lock(&softc->ctl_lock);
2702 if ((delay_info->lun_id >= CTL_MAX_LUNS)
2703 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2704 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2706 lun = softc->ctl_luns[delay_info->lun_id];
2707 mtx_lock(&lun->lun_lock);
2709 delay_info->status = CTL_DELAY_STATUS_OK;
2711 switch (delay_info->delay_type) {
2712 case CTL_DELAY_TYPE_CONT:
2714 case CTL_DELAY_TYPE_ONESHOT:
2717 delay_info->status =
2718 CTL_DELAY_STATUS_INVALID_TYPE;
2722 switch (delay_info->delay_loc) {
2723 case CTL_DELAY_LOC_DATAMOVE:
2724 lun->delay_info.datamove_type =
2725 delay_info->delay_type;
2726 lun->delay_info.datamove_delay =
2727 delay_info->delay_secs;
2729 case CTL_DELAY_LOC_DONE:
2730 lun->delay_info.done_type =
2731 delay_info->delay_type;
2732 lun->delay_info.done_delay =
2733 delay_info->delay_secs;
2736 delay_info->status =
2737 CTL_DELAY_STATUS_INVALID_LOC;
2740 mtx_unlock(&lun->lun_lock);
2743 mtx_unlock(&softc->ctl_lock);
2745 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2746 #endif /* CTL_IO_DELAY */
2749 case CTL_REALSYNC_SET: {
2752 syncstate = (int *)addr;
2754 mtx_lock(&softc->ctl_lock);
2755 switch (*syncstate) {
2757 softc->flags &= ~CTL_FLAG_REAL_SYNC;
2760 softc->flags |= CTL_FLAG_REAL_SYNC;
2766 mtx_unlock(&softc->ctl_lock);
2769 case CTL_REALSYNC_GET: {
2772 syncstate = (int*)addr;
2774 mtx_lock(&softc->ctl_lock);
2775 if (softc->flags & CTL_FLAG_REAL_SYNC)
2779 mtx_unlock(&softc->ctl_lock);
2785 struct ctl_sync_info *sync_info;
2786 struct ctl_lun *lun;
2788 sync_info = (struct ctl_sync_info *)addr;
2790 mtx_lock(&softc->ctl_lock);
2791 lun = softc->ctl_luns[sync_info->lun_id];
2793 mtx_unlock(&softc->ctl_lock);
2794 sync_info->status = CTL_GS_SYNC_NO_LUN;
2797 * Get or set the sync interval. We're not bounds checking
2798 * in the set case, hopefully the user won't do something
2801 mtx_lock(&lun->lun_lock);
2802 mtx_unlock(&softc->ctl_lock);
2803 if (cmd == CTL_GETSYNC)
2804 sync_info->sync_interval = lun->sync_interval;
2806 lun->sync_interval = sync_info->sync_interval;
2807 mtx_unlock(&lun->lun_lock);
2809 sync_info->status = CTL_GS_SYNC_OK;
2813 case CTL_GETSTATS: {
2814 struct ctl_stats *stats;
2815 struct ctl_lun *lun;
2818 stats = (struct ctl_stats *)addr;
2820 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2822 stats->status = CTL_SS_NEED_MORE_SPACE;
2823 stats->num_luns = softc->num_luns;
2827 * XXX KDM no locking here. If the LUN list changes,
2828 * things can blow up.
2830 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
2831 i++, lun = STAILQ_NEXT(lun, links)) {
2832 retval = copyout(&lun->stats, &stats->lun_stats[i],
2833 sizeof(lun->stats));
2837 stats->num_luns = softc->num_luns;
2838 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2840 stats->status = CTL_SS_OK;
2842 stats->flags = CTL_STATS_FLAG_TIME_VALID;
2844 stats->flags = CTL_STATS_FLAG_NONE;
2846 getnanouptime(&stats->timestamp);
2849 case CTL_ERROR_INJECT: {
2850 struct ctl_error_desc *err_desc, *new_err_desc;
2851 struct ctl_lun *lun;
2853 err_desc = (struct ctl_error_desc *)addr;
2855 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2857 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2859 mtx_lock(&softc->ctl_lock);
2860 lun = softc->ctl_luns[err_desc->lun_id];
2862 mtx_unlock(&softc->ctl_lock);
2863 free(new_err_desc, M_CTL);
2864 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2865 __func__, (uintmax_t)err_desc->lun_id);
2869 mtx_lock(&lun->lun_lock);
2870 mtx_unlock(&softc->ctl_lock);
2873 * We could do some checking here to verify the validity
2874 * of the request, but given the complexity of error
2875 * injection requests, the checking logic would be fairly
2878 * For now, if the request is invalid, it just won't get
2879 * executed and might get deleted.
2881 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2884 * XXX KDM check to make sure the serial number is unique,
2885 * in case we somehow manage to wrap. That shouldn't
2886 * happen for a very long time, but it's the right thing to
2889 new_err_desc->serial = lun->error_serial;
2890 err_desc->serial = lun->error_serial;
2891 lun->error_serial++;
2893 mtx_unlock(&lun->lun_lock);
2896 case CTL_ERROR_INJECT_DELETE: {
2897 struct ctl_error_desc *delete_desc, *desc, *desc2;
2898 struct ctl_lun *lun;
2901 delete_desc = (struct ctl_error_desc *)addr;
2904 mtx_lock(&softc->ctl_lock);
2905 lun = softc->ctl_luns[delete_desc->lun_id];
2907 mtx_unlock(&softc->ctl_lock);
2908 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2909 __func__, (uintmax_t)delete_desc->lun_id);
2913 mtx_lock(&lun->lun_lock);
2914 mtx_unlock(&softc->ctl_lock);
2915 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2916 if (desc->serial != delete_desc->serial)
2919 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2924 mtx_unlock(&lun->lun_lock);
2925 if (delete_done == 0) {
2926 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2927 "error serial %ju on LUN %u\n", __func__,
2928 delete_desc->serial, delete_desc->lun_id);
2934 case CTL_DUMP_STRUCTS: {
2936 struct ctl_port *port;
2937 struct ctl_frontend *fe;
2939 mtx_lock(&softc->ctl_lock);
2940 printf("CTL Persistent Reservation information start:\n");
2941 for (i = 0; i < CTL_MAX_LUNS; i++) {
2942 struct ctl_lun *lun;
2944 lun = softc->ctl_luns[i];
2947 || ((lun->flags & CTL_LUN_DISABLED) != 0))
2950 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) {
2951 if (lun->pr_keys[j] == NULL)
2953 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2954 if (lun->pr_keys[j][k] == 0)
2956 printf(" LUN %d port %d iid %d key "
2958 (uintmax_t)lun->pr_keys[j][k]);
2962 printf("CTL Persistent Reservation information end\n");
2963 printf("CTL Ports:\n");
2964 STAILQ_FOREACH(port, &softc->port_list, links) {
2965 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
2966 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
2967 port->frontend->name, port->port_type,
2968 port->physical_port, port->virtual_port,
2969 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
2970 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2971 if (port->wwpn_iid[j].in_use == 0 &&
2972 port->wwpn_iid[j].wwpn == 0 &&
2973 port->wwpn_iid[j].name == NULL)
2976 printf(" iid %u use %d WWPN %#jx '%s'\n",
2977 j, port->wwpn_iid[j].in_use,
2978 (uintmax_t)port->wwpn_iid[j].wwpn,
2979 port->wwpn_iid[j].name);
2982 printf("CTL Port information end\n");
2983 mtx_unlock(&softc->ctl_lock);
2985 * XXX KDM calling this without a lock. We'd likely want
2986 * to drop the lock before calling the frontend's dump
2989 printf("CTL Frontends:\n");
2990 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2991 printf(" Frontend '%s'\n", fe->name);
2992 if (fe->fe_dump != NULL)
2995 printf("CTL Frontend information end\n");
2999 struct ctl_lun_req *lun_req;
3000 struct ctl_backend_driver *backend;
3002 lun_req = (struct ctl_lun_req *)addr;
3004 backend = ctl_backend_find(lun_req->backend);
3005 if (backend == NULL) {
3006 lun_req->status = CTL_LUN_ERROR;
3007 snprintf(lun_req->error_str,
3008 sizeof(lun_req->error_str),
3009 "Backend \"%s\" not found.",
3013 if (lun_req->num_be_args > 0) {
3014 lun_req->kern_be_args = ctl_copyin_args(
3015 lun_req->num_be_args,
3018 sizeof(lun_req->error_str));
3019 if (lun_req->kern_be_args == NULL) {
3020 lun_req->status = CTL_LUN_ERROR;
3025 retval = backend->ioctl(dev, cmd, addr, flag, td);
3027 if (lun_req->num_be_args > 0) {
3028 ctl_copyout_args(lun_req->num_be_args,
3029 lun_req->kern_be_args);
3030 ctl_free_args(lun_req->num_be_args,
3031 lun_req->kern_be_args);
3035 case CTL_LUN_LIST: {
3037 struct ctl_lun *lun;
3038 struct ctl_lun_list *list;
3039 struct ctl_option *opt;
3041 list = (struct ctl_lun_list *)addr;
3044 * Allocate a fixed length sbuf here, based on the length
3045 * of the user's buffer. We could allocate an auto-extending
3046 * buffer, and then tell the user how much larger our
3047 * amount of data is than his buffer, but that presents
3050 * 1. The sbuf(9) routines use a blocking malloc, and so
3051 * we can't hold a lock while calling them with an
3052 * auto-extending buffer.
3054 * 2. There is not currently a LUN reference counting
3055 * mechanism, outside of outstanding transactions on
3056 * the LUN's OOA queue. So a LUN could go away on us
3057 * while we're getting the LUN number, backend-specific
3058 * information, etc. Thus, given the way things
3059 * currently work, we need to hold the CTL lock while
3060 * grabbing LUN information.
3062 * So, from the user's standpoint, the best thing to do is
3063 * allocate what he thinks is a reasonable buffer length,
3064 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3065 * double the buffer length and try again. (And repeat
3066 * that until he succeeds.)
3068 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3070 list->status = CTL_LUN_LIST_ERROR;
3071 snprintf(list->error_str, sizeof(list->error_str),
3072 "Unable to allocate %d bytes for LUN list",
3077 sbuf_printf(sb, "<ctllunlist>\n");
3079 mtx_lock(&softc->ctl_lock);
3080 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3081 mtx_lock(&lun->lun_lock);
3082 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3083 (uintmax_t)lun->lun);
3086 * Bail out as soon as we see that we've overfilled
3092 retval = sbuf_printf(sb, "\t<backend_type>%s"
3093 "</backend_type>\n",
3094 (lun->backend == NULL) ? "none" :
3095 lun->backend->name);
3100 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3101 lun->be_lun->lun_type);
3106 if (lun->backend == NULL) {
3107 retval = sbuf_printf(sb, "</lun>\n");
3113 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3114 (lun->be_lun->maxlba > 0) ?
3115 lun->be_lun->maxlba + 1 : 0);
3120 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3121 lun->be_lun->blocksize);
3126 retval = sbuf_printf(sb, "\t<serial_number>");
3131 retval = ctl_sbuf_printf_esc(sb,
3132 lun->be_lun->serial_num,
3133 sizeof(lun->be_lun->serial_num));
3138 retval = sbuf_printf(sb, "</serial_number>\n");
3143 retval = sbuf_printf(sb, "\t<device_id>");
3148 retval = ctl_sbuf_printf_esc(sb,
3149 lun->be_lun->device_id,
3150 sizeof(lun->be_lun->device_id));
3155 retval = sbuf_printf(sb, "</device_id>\n");
3160 if (lun->backend->lun_info != NULL) {
3161 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3165 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3166 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3167 opt->name, opt->value, opt->name);
3172 retval = sbuf_printf(sb, "</lun>\n");
3176 mtx_unlock(&lun->lun_lock);
3179 mtx_unlock(&lun->lun_lock);
3180 mtx_unlock(&softc->ctl_lock);
3183 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3186 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3187 snprintf(list->error_str, sizeof(list->error_str),
3188 "Out of space, %d bytes is too small",
3195 retval = copyout(sbuf_data(sb), list->lun_xml,
3198 list->fill_len = sbuf_len(sb) + 1;
3199 list->status = CTL_LUN_LIST_OK;
3204 struct ctl_iscsi *ci;
3205 struct ctl_frontend *fe;
3207 ci = (struct ctl_iscsi *)addr;
3209 fe = ctl_frontend_find("iscsi");
3211 ci->status = CTL_ISCSI_ERROR;
3212 snprintf(ci->error_str, sizeof(ci->error_str),
3213 "Frontend \"iscsi\" not found.");
3217 retval = fe->ioctl(dev, cmd, addr, flag, td);
3220 case CTL_PORT_REQ: {
3221 struct ctl_req *req;
3222 struct ctl_frontend *fe;
3224 req = (struct ctl_req *)addr;
3226 fe = ctl_frontend_find(req->driver);
3228 req->status = CTL_LUN_ERROR;
3229 snprintf(req->error_str, sizeof(req->error_str),
3230 "Frontend \"%s\" not found.", req->driver);
3233 if (req->num_args > 0) {
3234 req->kern_args = ctl_copyin_args(req->num_args,
3235 req->args, req->error_str, sizeof(req->error_str));
3236 if (req->kern_args == NULL) {
3237 req->status = CTL_LUN_ERROR;
3242 retval = fe->ioctl(dev, cmd, addr, flag, td);
3244 if (req->num_args > 0) {
3245 ctl_copyout_args(req->num_args, req->kern_args);
3246 ctl_free_args(req->num_args, req->kern_args);
3250 case CTL_PORT_LIST: {
3252 struct ctl_port *port;
3253 struct ctl_lun_list *list;
3254 struct ctl_option *opt;
3258 list = (struct ctl_lun_list *)addr;
3260 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3262 list->status = CTL_LUN_LIST_ERROR;
3263 snprintf(list->error_str, sizeof(list->error_str),
3264 "Unable to allocate %d bytes for LUN list",
3269 sbuf_printf(sb, "<ctlportlist>\n");
3271 mtx_lock(&softc->ctl_lock);
3272 STAILQ_FOREACH(port, &softc->port_list, links) {
3273 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3274 (uintmax_t)port->targ_port);
3277 * Bail out as soon as we see that we've overfilled
3283 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3284 "</frontend_type>\n", port->frontend->name);
3288 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3293 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3294 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3298 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3303 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3304 port->physical_port);
3308 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3309 port->virtual_port);
3313 if (port->target_devid != NULL) {
3314 sbuf_printf(sb, "\t<target>");
3315 ctl_id_sbuf(port->target_devid, sb);
3316 sbuf_printf(sb, "</target>\n");
3319 if (port->port_devid != NULL) {
3320 sbuf_printf(sb, "\t<port>");
3321 ctl_id_sbuf(port->port_devid, sb);
3322 sbuf_printf(sb, "</port>\n");
3325 if (port->port_info != NULL) {
3326 retval = port->port_info(port->onoff_arg, sb);
3330 STAILQ_FOREACH(opt, &port->options, links) {
3331 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3332 opt->name, opt->value, opt->name);
3337 if (port->lun_map != NULL) {
3338 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n");
3339 for (j = 0; j < CTL_MAX_LUNS; j++) {
3340 plun = ctl_lun_map_from_port(port, j);
3341 if (plun >= CTL_MAX_LUNS)
3344 "\t<lun id=\"%u\">%u</lun>\n",
3349 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3350 if (port->wwpn_iid[j].in_use == 0 ||
3351 (port->wwpn_iid[j].wwpn == 0 &&
3352 port->wwpn_iid[j].name == NULL))
3355 if (port->wwpn_iid[j].name != NULL)
3356 retval = sbuf_printf(sb,
3357 "\t<initiator id=\"%u\">%s</initiator>\n",
3358 j, port->wwpn_iid[j].name);
3360 retval = sbuf_printf(sb,
3361 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3362 j, port->wwpn_iid[j].wwpn);
3369 retval = sbuf_printf(sb, "</targ_port>\n");
3373 mtx_unlock(&softc->ctl_lock);
3376 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3379 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3380 snprintf(list->error_str, sizeof(list->error_str),
3381 "Out of space, %d bytes is too small",
3388 retval = copyout(sbuf_data(sb), list->lun_xml,
3391 list->fill_len = sbuf_len(sb) + 1;
3392 list->status = CTL_LUN_LIST_OK;
3397 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr;
3398 struct ctl_port *port;
3400 mtx_lock(&softc->ctl_lock);
3401 if (lm->port >= CTL_MAX_PORTS ||
3402 (port = softc->ctl_ports[lm->port]) == NULL) {
3403 mtx_unlock(&softc->ctl_lock);
3406 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps
3407 if (lm->plun < CTL_MAX_LUNS) {
3408 if (lm->lun == UINT32_MAX)
3409 retval = ctl_lun_map_unset(port, lm->plun);
3410 else if (lm->lun < CTL_MAX_LUNS &&
3411 softc->ctl_luns[lm->lun] != NULL)
3412 retval = ctl_lun_map_set(port, lm->plun, lm->lun);
3415 } else if (lm->plun == UINT32_MAX) {
3416 if (lm->lun == UINT32_MAX)
3417 retval = ctl_lun_map_deinit(port);
3419 retval = ctl_lun_map_init(port);
3425 /* XXX KDM should we fix this? */
3427 struct ctl_backend_driver *backend;
3434 * We encode the backend type as the ioctl type for backend
3435 * ioctls. So parse it out here, and then search for a
3436 * backend of this type.
3438 type = _IOC_TYPE(cmd);
3440 STAILQ_FOREACH(backend, &softc->be_list, links) {
3441 if (backend->type == type) {
3447 printf("ctl: unknown ioctl command %#lx or backend "
3452 retval = backend->ioctl(dev, cmd, addr, flag, td);
3462 ctl_get_initindex(struct ctl_nexus *nexus)
3464 if (nexus->targ_port < CTL_MAX_PORTS)
3465 return (nexus->initid.id +
3466 (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3468 return (nexus->initid.id +
3469 ((nexus->targ_port - CTL_MAX_PORTS) *
3470 CTL_MAX_INIT_PER_PORT));
3474 ctl_get_resindex(struct ctl_nexus *nexus)
3476 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3480 ctl_port_idx(int port_num)
3482 if (port_num < CTL_MAX_PORTS)
3485 return(port_num - CTL_MAX_PORTS);
3489 ctl_lun_map_init(struct ctl_port *port)
3491 struct ctl_softc *softc = control_softc;
3492 struct ctl_lun *lun;
3495 if (port->lun_map == NULL)
3496 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS,
3498 if (port->lun_map == NULL)
3500 for (i = 0; i < CTL_MAX_LUNS; i++)
3501 port->lun_map[i] = UINT32_MAX;
3502 if (port->status & CTL_PORT_STATUS_ONLINE) {
3503 STAILQ_FOREACH(lun, &softc->lun_list, links)
3504 port->lun_disable(port->targ_lun_arg, lun->lun);
3510 ctl_lun_map_deinit(struct ctl_port *port)
3512 struct ctl_softc *softc = control_softc;
3513 struct ctl_lun *lun;
3515 if (port->lun_map == NULL)
3517 free(port->lun_map, M_CTL);
3518 port->lun_map = NULL;
3519 if (port->status & CTL_PORT_STATUS_ONLINE) {
3520 STAILQ_FOREACH(lun, &softc->lun_list, links)
3521 port->lun_enable(port->targ_lun_arg, lun->lun);
3527 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun)
3532 if (port->lun_map == NULL) {
3533 status = ctl_lun_map_init(port);
3537 old = port->lun_map[plun];
3538 port->lun_map[plun] = glun;
3539 if ((port->status & CTL_PORT_STATUS_ONLINE) && old >= CTL_MAX_LUNS)
3540 port->lun_enable(port->targ_lun_arg, plun);
3545 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun)
3549 if (port->lun_map == NULL)
3551 old = port->lun_map[plun];
3552 port->lun_map[plun] = UINT32_MAX;
3553 if ((port->status & CTL_PORT_STATUS_ONLINE) && old < CTL_MAX_LUNS)
3554 port->lun_disable(port->targ_lun_arg, plun);
3559 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
3563 return (UINT32_MAX);
3564 if (port->lun_map == NULL || lun_id >= CTL_MAX_LUNS)
3566 return (port->lun_map[lun_id]);
3570 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
3575 return (UINT32_MAX);
3576 if (port->lun_map == NULL)
3578 for (i = 0; i < CTL_MAX_LUNS; i++) {
3579 if (port->lun_map[i] == lun_id)
3582 return (UINT32_MAX);
3585 static struct ctl_port *
3586 ctl_io_port(struct ctl_io_hdr *io_hdr)
3590 port_num = io_hdr->nexus.targ_port;
3591 return (control_softc->ctl_ports[ctl_port_idx(port_num)]);
3595 * Note: This only works for bitmask sizes that are at least 32 bits, and
3596 * that are a power of 2.
3599 ctl_ffz(uint32_t *mask, uint32_t size)
3601 uint32_t num_chunks, num_pieces;
3604 num_chunks = (size >> 5);
3605 if (num_chunks == 0)
3607 num_pieces = MIN((sizeof(uint32_t) * 8), size);
3609 for (i = 0; i < num_chunks; i++) {
3610 for (j = 0; j < num_pieces; j++) {
3611 if ((mask[i] & (1 << j)) == 0)
3612 return ((i << 5) + j);
3620 ctl_set_mask(uint32_t *mask, uint32_t bit)
3622 uint32_t chunk, piece;
3625 piece = bit % (sizeof(uint32_t) * 8);
3627 if ((mask[chunk] & (1 << piece)) != 0)
3630 mask[chunk] |= (1 << piece);
3636 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3638 uint32_t chunk, piece;
3641 piece = bit % (sizeof(uint32_t) * 8);
3643 if ((mask[chunk] & (1 << piece)) == 0)
3646 mask[chunk] &= ~(1 << piece);
3652 ctl_is_set(uint32_t *mask, uint32_t bit)
3654 uint32_t chunk, piece;
3657 piece = bit % (sizeof(uint32_t) * 8);
3659 if ((mask[chunk] & (1 << piece)) == 0)
3666 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3670 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3673 return (t[residx % CTL_MAX_INIT_PER_PORT]);
3677 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3681 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3684 t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3688 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3693 i = residx/CTL_MAX_INIT_PER_PORT;
3694 if (lun->pr_keys[i] != NULL)
3696 mtx_unlock(&lun->lun_lock);
3697 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3699 mtx_lock(&lun->lun_lock);
3700 if (lun->pr_keys[i] == NULL)
3701 lun->pr_keys[i] = p;
3707 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3711 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3712 KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3713 t[residx % CTL_MAX_INIT_PER_PORT] = key;
3717 * ctl_softc, pool_name, total_ctl_io are passed in.
3718 * npool is passed out.
3721 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3722 uint32_t total_ctl_io, void **npool)
3725 struct ctl_io_pool *pool;
3727 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3732 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3733 pool->ctl_softc = ctl_softc;
3734 pool->zone = uma_zsecond_create(pool->name, NULL,
3735 NULL, NULL, NULL, ctl_softc->io_zone);
3736 /* uma_prealloc(pool->zone, total_ctl_io); */
3740 *npool = ctl_softc->io_zone;
3746 ctl_pool_free(struct ctl_io_pool *pool)
3753 uma_zdestroy(pool->zone);
3759 ctl_alloc_io(void *pool_ref)
3763 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3765 io = uma_zalloc(pool->zone, M_WAITOK);
3767 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK);
3770 io->io_hdr.pool = pool_ref;
3775 ctl_alloc_io_nowait(void *pool_ref)
3779 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3781 io = uma_zalloc(pool->zone, M_NOWAIT);
3783 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT);
3786 io->io_hdr.pool = pool_ref;
3791 ctl_free_io(union ctl_io *io)
3794 struct ctl_io_pool *pool;
3801 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3802 uma_zfree(pool->zone, io);
3804 uma_zfree((uma_zone_t)io->io_hdr.pool, io);
3809 ctl_zero_io(union ctl_io *io)
3817 * May need to preserve linked list pointers at some point too.
3819 pool_ref = io->io_hdr.pool;
3820 memset(io, 0, sizeof(*io));
3821 io->io_hdr.pool = pool_ref;
3825 * This routine is currently used for internal copies of ctl_ios that need
3826 * to persist for some reason after we've already returned status to the
3827 * FETD. (Thus the flag set.)
3830 * Note that this makes a blind copy of all fields in the ctl_io, except
3831 * for the pool reference. This includes any memory that has been
3832 * allocated! That memory will no longer be valid after done has been
3833 * called, so this would be VERY DANGEROUS for command that actually does
3834 * any reads or writes. Right now (11/7/2005), this is only used for immediate
3835 * start and stop commands, which don't transfer any data, so this is not a
3836 * problem. If it is used for anything else, the caller would also need to
3837 * allocate data buffer space and this routine would need to be modified to
3838 * copy the data buffer(s) as well.
3841 ctl_copy_io(union ctl_io *src, union ctl_io *dest)
3850 * May need to preserve linked list pointers at some point too.
3852 pool_ref = dest->io_hdr.pool;
3854 memcpy(dest, src, MIN(sizeof(*src), sizeof(*dest)));
3856 dest->io_hdr.pool = pool_ref;
3858 * We need to know that this is an internal copy, and doesn't need
3859 * to get passed back to the FETD that allocated it.
3861 dest->io_hdr.flags |= CTL_FLAG_INT_COPY;
3865 ctl_expand_number(const char *buf, uint64_t *num)
3871 number = strtoq(buf, &endptr, 0);
3873 switch (tolower((unsigned char)*endptr)) {
3893 case '\0': /* No unit. */
3897 /* Unrecognized unit. */
3901 if ((number << shift) >> shift != number) {
3905 *num = number << shift;
3911 * This routine could be used in the future to load default and/or saved
3912 * mode page parameters for a particuar lun.
3915 ctl_init_page_index(struct ctl_lun *lun)
3918 struct ctl_page_index *page_index;
3922 memcpy(&lun->mode_pages.index, page_index_template,
3923 sizeof(page_index_template));
3925 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3927 page_index = &lun->mode_pages.index[i];
3929 * If this is a disk-only mode page, there's no point in
3930 * setting it up. For some pages, we have to have some
3931 * basic information about the disk in order to calculate the
3934 if ((lun->be_lun->lun_type != T_DIRECT)
3935 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
3938 switch (page_index->page_code & SMPH_PC_MASK) {
3939 case SMS_RW_ERROR_RECOVERY_PAGE: {
3940 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3941 panic("subpage is incorrect!");
3942 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
3943 &rw_er_page_default,
3944 sizeof(rw_er_page_default));
3945 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
3946 &rw_er_page_changeable,
3947 sizeof(rw_er_page_changeable));
3948 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
3949 &rw_er_page_default,
3950 sizeof(rw_er_page_default));
3951 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
3952 &rw_er_page_default,
3953 sizeof(rw_er_page_default));
3954 page_index->page_data =
3955 (uint8_t *)lun->mode_pages.rw_er_page;
3958 case SMS_FORMAT_DEVICE_PAGE: {
3959 struct scsi_format_page *format_page;
3961 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3962 panic("subpage is incorrect!");
3965 * Sectors per track are set above. Bytes per
3966 * sector need to be set here on a per-LUN basis.
3968 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
3969 &format_page_default,
3970 sizeof(format_page_default));
3971 memcpy(&lun->mode_pages.format_page[
3972 CTL_PAGE_CHANGEABLE], &format_page_changeable,
3973 sizeof(format_page_changeable));
3974 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
3975 &format_page_default,
3976 sizeof(format_page_default));
3977 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
3978 &format_page_default,
3979 sizeof(format_page_default));
3981 format_page = &lun->mode_pages.format_page[
3983 scsi_ulto2b(lun->be_lun->blocksize,
3984 format_page->bytes_per_sector);
3986 format_page = &lun->mode_pages.format_page[
3988 scsi_ulto2b(lun->be_lun->blocksize,
3989 format_page->bytes_per_sector);
3991 format_page = &lun->mode_pages.format_page[
3993 scsi_ulto2b(lun->be_lun->blocksize,
3994 format_page->bytes_per_sector);
3996 page_index->page_data =
3997 (uint8_t *)lun->mode_pages.format_page;
4000 case SMS_RIGID_DISK_PAGE: {
4001 struct scsi_rigid_disk_page *rigid_disk_page;
4002 uint32_t sectors_per_cylinder;
4006 #endif /* !__XSCALE__ */
4008 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4009 panic("invalid subpage value %d",
4010 page_index->subpage);
4013 * Rotation rate and sectors per track are set
4014 * above. We calculate the cylinders here based on
4015 * capacity. Due to the number of heads and
4016 * sectors per track we're using, smaller arrays
4017 * may turn out to have 0 cylinders. Linux and
4018 * FreeBSD don't pay attention to these mode pages
4019 * to figure out capacity, but Solaris does. It
4020 * seems to deal with 0 cylinders just fine, and
4021 * works out a fake geometry based on the capacity.
4023 memcpy(&lun->mode_pages.rigid_disk_page[
4024 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4025 sizeof(rigid_disk_page_default));
4026 memcpy(&lun->mode_pages.rigid_disk_page[
4027 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4028 sizeof(rigid_disk_page_changeable));
4030 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4034 * The divide method here will be more accurate,
4035 * probably, but results in floating point being
4036 * used in the kernel on i386 (__udivdi3()). On the
4037 * XScale, though, __udivdi3() is implemented in
4040 * The shift method for cylinder calculation is
4041 * accurate if sectors_per_cylinder is a power of
4042 * 2. Otherwise it might be slightly off -- you
4043 * might have a bit of a truncation problem.
4046 cylinders = (lun->be_lun->maxlba + 1) /
4047 sectors_per_cylinder;
4049 for (shift = 31; shift > 0; shift--) {
4050 if (sectors_per_cylinder & (1 << shift))
4053 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4057 * We've basically got 3 bytes, or 24 bits for the
4058 * cylinder size in the mode page. If we're over,
4059 * just round down to 2^24.
4061 if (cylinders > 0xffffff)
4062 cylinders = 0xffffff;
4064 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4066 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4068 if ((value = ctl_get_opt(&lun->be_lun->options,
4070 scsi_ulto2b(strtol(value, NULL, 0),
4071 rigid_disk_page->rotation_rate);
4074 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
4075 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4076 sizeof(rigid_disk_page_default));
4077 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
4078 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4079 sizeof(rigid_disk_page_default));
4081 page_index->page_data =
4082 (uint8_t *)lun->mode_pages.rigid_disk_page;
4085 case SMS_CACHING_PAGE: {
4086 struct scsi_caching_page *caching_page;
4088 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4089 panic("invalid subpage value %d",
4090 page_index->subpage);
4091 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4092 &caching_page_default,
4093 sizeof(caching_page_default));
4094 memcpy(&lun->mode_pages.caching_page[
4095 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4096 sizeof(caching_page_changeable));
4097 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4098 &caching_page_default,
4099 sizeof(caching_page_default));
4100 caching_page = &lun->mode_pages.caching_page[
4102 value = ctl_get_opt(&lun->be_lun->options, "writecache");
4103 if (value != NULL && strcmp(value, "off") == 0)
4104 caching_page->flags1 &= ~SCP_WCE;
4105 value = ctl_get_opt(&lun->be_lun->options, "readcache");
4106 if (value != NULL && strcmp(value, "off") == 0)
4107 caching_page->flags1 |= SCP_RCD;
4108 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4109 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4110 sizeof(caching_page_default));
4111 page_index->page_data =
4112 (uint8_t *)lun->mode_pages.caching_page;
4115 case SMS_CONTROL_MODE_PAGE: {
4116 struct scsi_control_page *control_page;
4118 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4119 panic("invalid subpage value %d",
4120 page_index->subpage);
4122 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT],
4123 &control_page_default,
4124 sizeof(control_page_default));
4125 memcpy(&lun->mode_pages.control_page[
4126 CTL_PAGE_CHANGEABLE], &control_page_changeable,
4127 sizeof(control_page_changeable));
4128 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED],
4129 &control_page_default,
4130 sizeof(control_page_default));
4131 control_page = &lun->mode_pages.control_page[
4133 value = ctl_get_opt(&lun->be_lun->options, "reordering");
4134 if (value != NULL && strcmp(value, "unrestricted") == 0) {
4135 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK;
4136 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED;
4138 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT],
4139 &lun->mode_pages.control_page[CTL_PAGE_SAVED],
4140 sizeof(control_page_default));
4141 page_index->page_data =
4142 (uint8_t *)lun->mode_pages.control_page;
4146 case SMS_INFO_EXCEPTIONS_PAGE: {
4147 switch (page_index->subpage) {
4148 case SMS_SUBPAGE_PAGE_0:
4149 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4151 sizeof(ie_page_default));
4152 memcpy(&lun->mode_pages.ie_page[
4153 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4154 sizeof(ie_page_changeable));
4155 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4157 sizeof(ie_page_default));
4158 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4160 sizeof(ie_page_default));
4161 page_index->page_data =
4162 (uint8_t *)lun->mode_pages.ie_page;
4165 struct ctl_logical_block_provisioning_page *page;
4167 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4169 sizeof(lbp_page_default));
4170 memcpy(&lun->mode_pages.lbp_page[
4171 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4172 sizeof(lbp_page_changeable));
4173 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4175 sizeof(lbp_page_default));
4176 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4177 value = ctl_get_opt(&lun->be_lun->options,
4179 if (value != NULL &&
4180 ctl_expand_number(value, &ival) == 0) {
4181 page->descr[0].flags |= SLBPPD_ENABLED |
4183 if (lun->be_lun->blocksize)
4184 ival /= lun->be_lun->blocksize;
4187 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4188 page->descr[0].count);
4190 value = ctl_get_opt(&lun->be_lun->options,
4192 if (value != NULL &&
4193 ctl_expand_number(value, &ival) == 0) {
4194 page->descr[1].flags |= SLBPPD_ENABLED |
4196 if (lun->be_lun->blocksize)
4197 ival /= lun->be_lun->blocksize;
4200 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4201 page->descr[1].count);
4203 value = ctl_get_opt(&lun->be_lun->options,
4204 "pool-avail-threshold");
4205 if (value != NULL &&
4206 ctl_expand_number(value, &ival) == 0) {
4207 page->descr[2].flags |= SLBPPD_ENABLED |
4209 if (lun->be_lun->blocksize)
4210 ival /= lun->be_lun->blocksize;
4213 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4214 page->descr[2].count);
4216 value = ctl_get_opt(&lun->be_lun->options,
4217 "pool-used-threshold");
4218 if (value != NULL &&
4219 ctl_expand_number(value, &ival) == 0) {
4220 page->descr[3].flags |= SLBPPD_ENABLED |
4222 if (lun->be_lun->blocksize)
4223 ival /= lun->be_lun->blocksize;
4226 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4227 page->descr[3].count);
4229 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4230 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4231 sizeof(lbp_page_default));
4232 page_index->page_data =
4233 (uint8_t *)lun->mode_pages.lbp_page;
4237 case SMS_VENDOR_SPECIFIC_PAGE:{
4238 switch (page_index->subpage) {
4239 case DBGCNF_SUBPAGE_CODE: {
4240 struct copan_debugconf_subpage *current_page,
4243 memcpy(&lun->mode_pages.debugconf_subpage[
4245 &debugconf_page_default,
4246 sizeof(debugconf_page_default));
4247 memcpy(&lun->mode_pages.debugconf_subpage[
4248 CTL_PAGE_CHANGEABLE],
4249 &debugconf_page_changeable,
4250 sizeof(debugconf_page_changeable));
4251 memcpy(&lun->mode_pages.debugconf_subpage[
4253 &debugconf_page_default,
4254 sizeof(debugconf_page_default));
4255 memcpy(&lun->mode_pages.debugconf_subpage[
4257 &debugconf_page_default,
4258 sizeof(debugconf_page_default));
4259 page_index->page_data =
4260 (uint8_t *)lun->mode_pages.debugconf_subpage;
4262 current_page = (struct copan_debugconf_subpage *)
4263 (page_index->page_data +
4264 (page_index->page_len *
4266 saved_page = (struct copan_debugconf_subpage *)
4267 (page_index->page_data +
4268 (page_index->page_len *
4273 panic("invalid subpage value %d",
4274 page_index->subpage);
4280 panic("invalid page value %d",
4281 page_index->page_code & SMPH_PC_MASK);
4286 return (CTL_RETVAL_COMPLETE);
4290 ctl_init_log_page_index(struct ctl_lun *lun)
4292 struct ctl_page_index *page_index;
4295 memcpy(&lun->log_pages.index, log_page_index_template,
4296 sizeof(log_page_index_template));
4299 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4301 page_index = &lun->log_pages.index[i];
4303 * If this is a disk-only mode page, there's no point in
4304 * setting it up. For some pages, we have to have some
4305 * basic information about the disk in order to calculate the
4308 if ((lun->be_lun->lun_type != T_DIRECT)
4309 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
4312 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4313 lun->backend->lun_attr == NULL)
4316 if (page_index->page_code != prev) {
4317 lun->log_pages.pages_page[j] = page_index->page_code;
4318 prev = page_index->page_code;
4321 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4322 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4325 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4326 lun->log_pages.index[0].page_len = j;
4327 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4328 lun->log_pages.index[1].page_len = k * 2;
4329 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4330 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4331 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page;
4332 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page);
4334 return (CTL_RETVAL_COMPLETE);
4338 hex2bin(const char *str, uint8_t *buf, int buf_size)
4343 memset(buf, 0, buf_size);
4344 while (isspace(str[0]))
4346 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4349 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4353 else if (isalpha(c))
4354 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4360 buf[i / 2] |= (c << 4);
4364 return ((i + 1) / 2);
4371 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4372 * wants us to allocate the LUN and he can block.
4373 * - ctl_softc is always set
4374 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4376 * Returns 0 for success, non-zero (errno) for failure.
4379 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4380 struct ctl_be_lun *const be_lun)
4382 struct ctl_lun *nlun, *lun;
4383 struct scsi_vpd_id_descriptor *desc;
4384 struct scsi_vpd_id_t10 *t10id;
4385 const char *eui, *naa, *scsiname, *vendor, *value;
4386 int lun_number, i, lun_malloced;
4387 int devidlen, idlen1, idlen2 = 0, len;
4393 * We currently only support Direct Access or Processor LUN types.
4395 switch (be_lun->lun_type) {
4403 be_lun->lun_config_status(be_lun->be_lun,
4404 CTL_LUN_CONFIG_FAILURE);
4407 if (ctl_lun == NULL) {
4408 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4415 memset(lun, 0, sizeof(*lun));
4417 lun->flags = CTL_LUN_MALLOCED;
4419 /* Generate LUN ID. */
4420 devidlen = max(CTL_DEVID_MIN_LEN,
4421 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4422 idlen1 = sizeof(*t10id) + devidlen;
4423 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4424 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4425 if (scsiname != NULL) {
4426 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4427 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4429 eui = ctl_get_opt(&be_lun->options, "eui");
4431 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4433 naa = ctl_get_opt(&be_lun->options, "naa");
4435 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4437 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4438 M_CTL, M_WAITOK | M_ZERO);
4439 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4440 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4441 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4442 desc->length = idlen1;
4443 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4444 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4445 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4446 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4448 strncpy(t10id->vendor, vendor,
4449 min(sizeof(t10id->vendor), strlen(vendor)));
4451 strncpy((char *)t10id->vendor_spec_id,
4452 (char *)be_lun->device_id, devidlen);
4453 if (scsiname != NULL) {
4454 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4456 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4457 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4458 SVPD_ID_TYPE_SCSI_NAME;
4459 desc->length = idlen2;
4460 strlcpy(desc->identifier, scsiname, idlen2);
4463 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4465 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4466 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4468 desc->length = hex2bin(eui, desc->identifier, 16);
4469 desc->length = desc->length > 12 ? 16 :
4470 (desc->length > 8 ? 12 : 8);
4471 len -= 16 - desc->length;
4474 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4476 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4477 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4479 desc->length = hex2bin(naa, desc->identifier, 16);
4480 desc->length = desc->length > 8 ? 16 : 8;
4481 len -= 16 - desc->length;
4483 lun->lun_devid->len = len;
4485 mtx_lock(&ctl_softc->ctl_lock);
4487 * See if the caller requested a particular LUN number. If so, see
4488 * if it is available. Otherwise, allocate the first available LUN.
4490 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4491 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4492 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4493 mtx_unlock(&ctl_softc->ctl_lock);
4494 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4495 printf("ctl: requested LUN ID %d is higher "
4496 "than CTL_MAX_LUNS - 1 (%d)\n",
4497 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4500 * XXX KDM return an error, or just assign
4501 * another LUN ID in this case??
4503 printf("ctl: requested LUN ID %d is already "
4504 "in use\n", be_lun->req_lun_id);
4506 if (lun->flags & CTL_LUN_MALLOCED)
4508 be_lun->lun_config_status(be_lun->be_lun,
4509 CTL_LUN_CONFIG_FAILURE);
4512 lun_number = be_lun->req_lun_id;
4514 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS);
4515 if (lun_number == -1) {
4516 mtx_unlock(&ctl_softc->ctl_lock);
4517 printf("ctl: can't allocate LUN, out of LUNs\n");
4518 if (lun->flags & CTL_LUN_MALLOCED)
4520 be_lun->lun_config_status(be_lun->be_lun,
4521 CTL_LUN_CONFIG_FAILURE);
4525 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4527 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4528 lun->lun = lun_number;
4529 lun->be_lun = be_lun;
4531 * The processor LUN is always enabled. Disk LUNs come on line
4532 * disabled, and must be enabled by the backend.
4534 lun->flags |= CTL_LUN_DISABLED;
4535 lun->backend = be_lun->be;
4536 be_lun->ctl_lun = lun;
4537 be_lun->lun_id = lun_number;
4538 atomic_add_int(&be_lun->be->num_luns, 1);
4539 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE)
4540 lun->flags |= CTL_LUN_OFFLINE;
4542 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4543 lun->flags |= CTL_LUN_STOPPED;
4545 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4546 lun->flags |= CTL_LUN_INOPERABLE;
4548 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4549 lun->flags |= CTL_LUN_PRIMARY_SC;
4551 value = ctl_get_opt(&be_lun->options, "readonly");
4552 if (value != NULL && strcmp(value, "on") == 0)
4553 lun->flags |= CTL_LUN_READONLY;
4555 lun->serseq = CTL_LUN_SERSEQ_OFF;
4556 if (be_lun->flags & CTL_LUN_FLAG_SERSEQ_READ)
4557 lun->serseq = CTL_LUN_SERSEQ_READ;
4558 value = ctl_get_opt(&be_lun->options, "serseq");
4559 if (value != NULL && strcmp(value, "on") == 0)
4560 lun->serseq = CTL_LUN_SERSEQ_ON;
4561 else if (value != NULL && strcmp(value, "read") == 0)
4562 lun->serseq = CTL_LUN_SERSEQ_READ;
4563 else if (value != NULL && strcmp(value, "off") == 0)
4564 lun->serseq = CTL_LUN_SERSEQ_OFF;
4566 lun->ctl_softc = ctl_softc;
4568 lun->last_busy = getsbinuptime();
4570 TAILQ_INIT(&lun->ooa_queue);
4571 TAILQ_INIT(&lun->blocked_queue);
4572 STAILQ_INIT(&lun->error_list);
4573 ctl_tpc_lun_init(lun);
4576 * Initialize the mode and log page index.
4578 ctl_init_page_index(lun);
4579 ctl_init_log_page_index(lun);
4582 * Now, before we insert this lun on the lun list, set the lun
4583 * inventory changed UA for all other luns.
4585 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4586 mtx_lock(&nlun->lun_lock);
4587 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4588 mtx_unlock(&nlun->lun_lock);
4591 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4593 ctl_softc->ctl_luns[lun_number] = lun;
4595 ctl_softc->num_luns++;
4597 /* Setup statistics gathering */
4598 lun->stats.device_type = be_lun->lun_type;
4599 lun->stats.lun_number = lun_number;
4600 if (lun->stats.device_type == T_DIRECT)
4601 lun->stats.blocksize = be_lun->blocksize;
4603 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4604 for (i = 0;i < CTL_MAX_PORTS;i++)
4605 lun->stats.ports[i].targ_port = i;
4607 mtx_unlock(&ctl_softc->ctl_lock);
4609 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4616 * - LUN has already been marked invalid and any pending I/O has been taken
4620 ctl_free_lun(struct ctl_lun *lun)
4622 struct ctl_softc *softc;
4623 struct ctl_lun *nlun;
4626 softc = lun->ctl_softc;
4628 mtx_assert(&softc->ctl_lock, MA_OWNED);
4630 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4632 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4634 softc->ctl_luns[lun->lun] = NULL;
4636 if (!TAILQ_EMPTY(&lun->ooa_queue))
4637 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4642 * Tell the backend to free resources, if this LUN has a backend.
4644 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4645 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4647 ctl_tpc_lun_shutdown(lun);
4648 mtx_destroy(&lun->lun_lock);
4649 free(lun->lun_devid, M_CTL);
4650 for (i = 0; i < CTL_MAX_PORTS; i++)
4651 free(lun->pending_ua[i], M_CTL);
4652 for (i = 0; i < 2 * CTL_MAX_PORTS; i++)
4653 free(lun->pr_keys[i], M_CTL);
4654 free(lun->write_buffer, M_CTL);
4655 if (lun->flags & CTL_LUN_MALLOCED)
4658 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4659 mtx_lock(&nlun->lun_lock);
4660 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4661 mtx_unlock(&nlun->lun_lock);
4668 ctl_create_lun(struct ctl_be_lun *be_lun)
4670 struct ctl_softc *softc;
4672 softc = control_softc;
4675 * ctl_alloc_lun() should handle all potential failure cases.
4677 ctl_alloc_lun(softc, NULL, be_lun);
4681 ctl_add_lun(struct ctl_be_lun *be_lun)
4683 struct ctl_softc *softc = control_softc;
4685 mtx_lock(&softc->ctl_lock);
4686 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links);
4687 mtx_unlock(&softc->ctl_lock);
4688 wakeup(&softc->pending_lun_queue);
4694 ctl_enable_lun(struct ctl_be_lun *be_lun)
4696 struct ctl_softc *softc;
4697 struct ctl_port *port, *nport;
4698 struct ctl_lun *lun;
4701 lun = (struct ctl_lun *)be_lun->ctl_lun;
4702 softc = lun->ctl_softc;
4704 mtx_lock(&softc->ctl_lock);
4705 mtx_lock(&lun->lun_lock);
4706 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4708 * eh? Why did we get called if the LUN is already
4711 mtx_unlock(&lun->lun_lock);
4712 mtx_unlock(&softc->ctl_lock);
4715 lun->flags &= ~CTL_LUN_DISABLED;
4716 mtx_unlock(&lun->lun_lock);
4718 for (port = STAILQ_FIRST(&softc->port_list); port != NULL; port = nport) {
4719 nport = STAILQ_NEXT(port, links);
4720 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4721 port->lun_map != NULL)
4725 * Drop the lock while we call the FETD's enable routine.
4726 * This can lead to a callback into CTL (at least in the
4727 * case of the internal initiator frontend.
4729 mtx_unlock(&softc->ctl_lock);
4730 retval = port->lun_enable(port->targ_lun_arg, lun->lun);
4731 mtx_lock(&softc->ctl_lock);
4733 printf("%s: FETD %s port %d returned error "
4734 "%d for lun_enable on lun %jd\n",
4735 __func__, port->port_name, port->targ_port,
4736 retval, (intmax_t)lun->lun);
4740 mtx_unlock(&softc->ctl_lock);
4746 ctl_disable_lun(struct ctl_be_lun *be_lun)
4748 struct ctl_softc *softc;
4749 struct ctl_port *port;
4750 struct ctl_lun *lun;
4753 lun = (struct ctl_lun *)be_lun->ctl_lun;
4754 softc = lun->ctl_softc;
4756 mtx_lock(&softc->ctl_lock);
4757 mtx_lock(&lun->lun_lock);
4758 if (lun->flags & CTL_LUN_DISABLED) {
4759 mtx_unlock(&lun->lun_lock);
4760 mtx_unlock(&softc->ctl_lock);
4763 lun->flags |= CTL_LUN_DISABLED;
4764 mtx_unlock(&lun->lun_lock);
4766 STAILQ_FOREACH(port, &softc->port_list, links) {
4767 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4768 port->lun_map != NULL)
4770 mtx_unlock(&softc->ctl_lock);
4772 * Drop the lock before we call the frontend's disable
4773 * routine, to avoid lock order reversals.
4775 * XXX KDM what happens if the frontend list changes while
4776 * we're traversing it? It's unlikely, but should be handled.
4778 retval = port->lun_disable(port->targ_lun_arg, lun->lun);
4779 mtx_lock(&softc->ctl_lock);
4781 printf("%s: FETD %s port %d returned error "
4782 "%d for lun_disable on lun %jd\n",
4783 __func__, port->port_name, port->targ_port,
4784 retval, (intmax_t)lun->lun);
4788 mtx_unlock(&softc->ctl_lock);
4794 ctl_start_lun(struct ctl_be_lun *be_lun)
4796 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4798 mtx_lock(&lun->lun_lock);
4799 lun->flags &= ~CTL_LUN_STOPPED;
4800 mtx_unlock(&lun->lun_lock);
4805 ctl_stop_lun(struct ctl_be_lun *be_lun)
4807 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4809 mtx_lock(&lun->lun_lock);
4810 lun->flags |= CTL_LUN_STOPPED;
4811 mtx_unlock(&lun->lun_lock);
4816 ctl_lun_offline(struct ctl_be_lun *be_lun)
4818 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4820 mtx_lock(&lun->lun_lock);
4821 lun->flags |= CTL_LUN_OFFLINE;
4822 mtx_unlock(&lun->lun_lock);
4827 ctl_lun_online(struct ctl_be_lun *be_lun)
4829 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4831 mtx_lock(&lun->lun_lock);
4832 lun->flags &= ~CTL_LUN_OFFLINE;
4833 mtx_unlock(&lun->lun_lock);
4838 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4840 struct ctl_softc *softc;
4841 struct ctl_lun *lun;
4843 lun = (struct ctl_lun *)be_lun->ctl_lun;
4844 softc = lun->ctl_softc;
4846 mtx_lock(&lun->lun_lock);
4849 * The LUN needs to be disabled before it can be marked invalid.
4851 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4852 mtx_unlock(&lun->lun_lock);
4856 * Mark the LUN invalid.
4858 lun->flags |= CTL_LUN_INVALID;
4861 * If there is nothing in the OOA queue, go ahead and free the LUN.
4862 * If we have something in the OOA queue, we'll free it when the
4863 * last I/O completes.
4865 if (TAILQ_EMPTY(&lun->ooa_queue)) {
4866 mtx_unlock(&lun->lun_lock);
4867 mtx_lock(&softc->ctl_lock);
4869 mtx_unlock(&softc->ctl_lock);
4871 mtx_unlock(&lun->lun_lock);
4877 ctl_lun_inoperable(struct ctl_be_lun *be_lun)
4879 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4881 mtx_lock(&lun->lun_lock);
4882 lun->flags |= CTL_LUN_INOPERABLE;
4883 mtx_unlock(&lun->lun_lock);
4888 ctl_lun_operable(struct ctl_be_lun *be_lun)
4890 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4892 mtx_lock(&lun->lun_lock);
4893 lun->flags &= ~CTL_LUN_INOPERABLE;
4894 mtx_unlock(&lun->lun_lock);
4899 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
4901 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4903 mtx_lock(&lun->lun_lock);
4904 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGED);
4905 mtx_unlock(&lun->lun_lock);
4909 * Backend "memory move is complete" callback for requests that never
4910 * make it down to say RAIDCore's configuration code.
4913 ctl_config_move_done(union ctl_io *io)
4917 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
4918 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
4919 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
4921 if ((io->io_hdr.port_status != 0) &&
4922 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
4923 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
4925 * For hardware error sense keys, the sense key
4926 * specific value is defined to be a retry count,
4927 * but we use it to pass back an internal FETD
4928 * error code. XXX KDM Hopefully the FETD is only
4929 * using 16 bits for an error code, since that's
4930 * all the space we have in the sks field.
4932 ctl_set_internal_failure(&io->scsiio,
4935 io->io_hdr.port_status);
4938 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
4939 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
4940 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
4941 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
4943 * XXX KDM just assuming a single pointer here, and not a
4944 * S/G list. If we start using S/G lists for config data,
4945 * we'll need to know how to clean them up here as well.
4947 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
4948 free(io->scsiio.kern_data_ptr, M_CTL);
4950 retval = CTL_RETVAL_COMPLETE;
4953 * XXX KDM now we need to continue data movement. Some
4955 * - call ctl_scsiio() again? We don't do this for data
4956 * writes, because for those at least we know ahead of
4957 * time where the write will go and how long it is. For
4958 * config writes, though, that information is largely
4959 * contained within the write itself, thus we need to
4960 * parse out the data again.
4962 * - Call some other function once the data is in?
4964 if (ctl_debug & CTL_DEBUG_CDB_DATA)
4968 * XXX KDM call ctl_scsiio() again for now, and check flag
4969 * bits to see whether we're allocated or not.
4971 retval = ctl_scsiio(&io->scsiio);
4977 * This gets called by a backend driver when it is done with a
4978 * data_submit method.
4981 ctl_data_submit_done(union ctl_io *io)
4984 * If the IO_CONT flag is set, we need to call the supplied
4985 * function to continue processing the I/O, instead of completing
4988 * If there is an error, though, we don't want to keep processing.
4989 * Instead, just send status back to the initiator.
4991 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
4992 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
4993 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
4994 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
4995 io->scsiio.io_cont(io);
5002 * This gets called by a backend driver when it is done with a
5003 * configuration write.
5006 ctl_config_write_done(union ctl_io *io)
5011 * If the IO_CONT flag is set, we need to call the supplied
5012 * function to continue processing the I/O, instead of completing
5015 * If there is an error, though, we don't want to keep processing.
5016 * Instead, just send status back to the initiator.
5018 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5019 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5020 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5021 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5022 io->scsiio.io_cont(io);
5026 * Since a configuration write can be done for commands that actually
5027 * have data allocated, like write buffer, and commands that have
5028 * no data, like start/stop unit, we need to check here.
5030 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5031 buf = io->scsiio.kern_data_ptr;
5040 ctl_config_read_done(union ctl_io *io)
5045 * If there is some error -- we are done, skip data transfer.
5047 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
5048 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5049 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
5050 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5051 buf = io->scsiio.kern_data_ptr;
5061 * If the IO_CONT flag is set, we need to call the supplied
5062 * function to continue processing the I/O, instead of completing
5065 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
5066 io->scsiio.io_cont(io);
5074 * SCSI release command.
5077 ctl_scsi_release(struct ctl_scsiio *ctsio)
5079 int length, longid, thirdparty_id, resv_id;
5080 struct ctl_lun *lun;
5086 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5088 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5089 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5091 switch (ctsio->cdb[0]) {
5093 struct scsi_release_10 *cdb;
5095 cdb = (struct scsi_release_10 *)ctsio->cdb;
5097 if (cdb->byte2 & SR10_LONGID)
5100 thirdparty_id = cdb->thirdparty_id;
5102 resv_id = cdb->resv_id;
5103 length = scsi_2btoul(cdb->length);
5110 * XXX KDM right now, we only support LUN reservation. We don't
5111 * support 3rd party reservations, or extent reservations, which
5112 * might actually need the parameter list. If we've gotten this
5113 * far, we've got a LUN reservation. Anything else got kicked out
5114 * above. So, according to SPC, ignore the length.
5118 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5120 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5121 ctsio->kern_data_len = length;
5122 ctsio->kern_total_len = length;
5123 ctsio->kern_data_resid = 0;
5124 ctsio->kern_rel_offset = 0;
5125 ctsio->kern_sg_entries = 0;
5126 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5127 ctsio->be_move_done = ctl_config_move_done;
5128 ctl_datamove((union ctl_io *)ctsio);
5130 return (CTL_RETVAL_COMPLETE);
5134 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5136 mtx_lock(&lun->lun_lock);
5139 * According to SPC, it is not an error for an intiator to attempt
5140 * to release a reservation on a LUN that isn't reserved, or that
5141 * is reserved by another initiator. The reservation can only be
5142 * released, though, by the initiator who made it or by one of
5143 * several reset type events.
5145 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5146 lun->flags &= ~CTL_LUN_RESERVED;
5148 mtx_unlock(&lun->lun_lock);
5150 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5151 free(ctsio->kern_data_ptr, M_CTL);
5152 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5155 ctl_set_success(ctsio);
5156 ctl_done((union ctl_io *)ctsio);
5157 return (CTL_RETVAL_COMPLETE);
5161 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5163 int extent, thirdparty, longid;
5164 int resv_id, length;
5165 uint64_t thirdparty_id;
5166 struct ctl_lun *lun;
5176 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5178 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5179 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5181 switch (ctsio->cdb[0]) {
5183 struct scsi_reserve_10 *cdb;
5185 cdb = (struct scsi_reserve_10 *)ctsio->cdb;
5187 if (cdb->byte2 & SR10_LONGID)
5190 thirdparty_id = cdb->thirdparty_id;
5192 resv_id = cdb->resv_id;
5193 length = scsi_2btoul(cdb->length);
5199 * XXX KDM right now, we only support LUN reservation. We don't
5200 * support 3rd party reservations, or extent reservations, which
5201 * might actually need the parameter list. If we've gotten this
5202 * far, we've got a LUN reservation. Anything else got kicked out
5203 * above. So, according to SPC, ignore the length.
5207 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5209 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5210 ctsio->kern_data_len = length;
5211 ctsio->kern_total_len = length;
5212 ctsio->kern_data_resid = 0;
5213 ctsio->kern_rel_offset = 0;
5214 ctsio->kern_sg_entries = 0;
5215 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5216 ctsio->be_move_done = ctl_config_move_done;
5217 ctl_datamove((union ctl_io *)ctsio);
5219 return (CTL_RETVAL_COMPLETE);
5223 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5225 mtx_lock(&lun->lun_lock);
5226 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5227 ctl_set_reservation_conflict(ctsio);
5231 lun->flags |= CTL_LUN_RESERVED;
5232 lun->res_idx = residx;
5234 ctl_set_success(ctsio);
5237 mtx_unlock(&lun->lun_lock);
5239 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5240 free(ctsio->kern_data_ptr, M_CTL);
5241 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5244 ctl_done((union ctl_io *)ctsio);
5245 return (CTL_RETVAL_COMPLETE);
5249 ctl_start_stop(struct ctl_scsiio *ctsio)
5251 struct scsi_start_stop_unit *cdb;
5252 struct ctl_lun *lun;
5255 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5257 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5260 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5264 * We don't support the immediate bit on a stop unit. In order to
5265 * do that, we would need to code up a way to know that a stop is
5266 * pending, and hold off any new commands until it completes, one
5267 * way or another. Then we could accept or reject those commands
5268 * depending on its status. We would almost need to do the reverse
5269 * of what we do below for an immediate start -- return the copy of
5270 * the ctl_io to the FETD with status to send to the host (and to
5271 * free the copy!) and then free the original I/O once the stop
5272 * actually completes. That way, the OOA queue mechanism can work
5273 * to block commands that shouldn't proceed. Another alternative
5274 * would be to put the copy in the queue in place of the original,
5275 * and return the original back to the caller. That could be
5278 if ((cdb->byte2 & SSS_IMMED)
5279 && ((cdb->how & SSS_START) == 0)) {
5280 ctl_set_invalid_field(ctsio,
5286 ctl_done((union ctl_io *)ctsio);
5287 return (CTL_RETVAL_COMPLETE);
5290 if ((lun->flags & CTL_LUN_PR_RESERVED)
5291 && ((cdb->how & SSS_START)==0)) {
5294 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5295 if (ctl_get_prkey(lun, residx) == 0
5296 || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5298 ctl_set_reservation_conflict(ctsio);
5299 ctl_done((union ctl_io *)ctsio);
5300 return (CTL_RETVAL_COMPLETE);
5305 * If there is no backend on this device, we can't start or stop
5306 * it. In theory we shouldn't get any start/stop commands in the
5307 * first place at this level if the LUN doesn't have a backend.
5308 * That should get stopped by the command decode code.
5310 if (lun->backend == NULL) {
5311 ctl_set_invalid_opcode(ctsio);
5312 ctl_done((union ctl_io *)ctsio);
5313 return (CTL_RETVAL_COMPLETE);
5317 * XXX KDM Copan-specific offline behavior.
5318 * Figure out a reasonable way to port this?
5321 mtx_lock(&lun->lun_lock);
5323 if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
5324 && (lun->flags & CTL_LUN_OFFLINE)) {
5326 * If the LUN is offline, and the on/offline bit isn't set,
5327 * reject the start or stop. Otherwise, let it through.
5329 mtx_unlock(&lun->lun_lock);
5330 ctl_set_lun_not_ready(ctsio);
5331 ctl_done((union ctl_io *)ctsio);
5333 mtx_unlock(&lun->lun_lock);
5334 #endif /* NEEDTOPORT */
5336 * This could be a start or a stop when we're online,
5337 * or a stop/offline or start/online. A start or stop when
5338 * we're offline is covered in the case above.
5341 * In the non-immediate case, we send the request to
5342 * the backend and return status to the user when
5345 * In the immediate case, we allocate a new ctl_io
5346 * to hold a copy of the request, and send that to
5347 * the backend. We then set good status on the
5348 * user's request and return it immediately.
5350 if (cdb->byte2 & SSS_IMMED) {
5351 union ctl_io *new_io;
5353 new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5354 ctl_copy_io((union ctl_io *)ctsio, new_io);
5355 retval = lun->backend->config_write(new_io);
5356 ctl_set_success(ctsio);
5357 ctl_done((union ctl_io *)ctsio);
5359 retval = lun->backend->config_write(
5360 (union ctl_io *)ctsio);
5369 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5370 * we don't really do anything with the LBA and length fields if the user
5371 * passes them in. Instead we'll just flush out the cache for the entire
5375 ctl_sync_cache(struct ctl_scsiio *ctsio)
5377 struct ctl_lun *lun;
5378 struct ctl_softc *softc;
5379 struct ctl_lba_len_flags *lbalen;
5380 uint64_t starting_lba;
5381 uint32_t block_count;
5385 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5387 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5388 softc = lun->ctl_softc;
5391 switch (ctsio->cdb[0]) {
5392 case SYNCHRONIZE_CACHE: {
5393 struct scsi_sync_cache *cdb;
5394 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5396 starting_lba = scsi_4btoul(cdb->begin_lba);
5397 block_count = scsi_2btoul(cdb->lb_count);
5401 case SYNCHRONIZE_CACHE_16: {
5402 struct scsi_sync_cache_16 *cdb;
5403 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5405 starting_lba = scsi_8btou64(cdb->begin_lba);
5406 block_count = scsi_4btoul(cdb->lb_count);
5411 ctl_set_invalid_opcode(ctsio);
5412 ctl_done((union ctl_io *)ctsio);
5414 break; /* NOTREACHED */
5418 * We check the LBA and length, but don't do anything with them.
5419 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5420 * get flushed. This check will just help satisfy anyone who wants
5421 * to see an error for an out of range LBA.
5423 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5424 ctl_set_lba_out_of_range(ctsio);
5425 ctl_done((union ctl_io *)ctsio);
5430 * If this LUN has no backend, we can't flush the cache anyway.
5432 if (lun->backend == NULL) {
5433 ctl_set_invalid_opcode(ctsio);
5434 ctl_done((union ctl_io *)ctsio);
5438 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5439 lbalen->lba = starting_lba;
5440 lbalen->len = block_count;
5441 lbalen->flags = byte2;
5444 * Check to see whether we're configured to send the SYNCHRONIZE
5445 * CACHE command directly to the back end.
5447 mtx_lock(&lun->lun_lock);
5448 if ((softc->flags & CTL_FLAG_REAL_SYNC)
5449 && (++(lun->sync_count) >= lun->sync_interval)) {
5450 lun->sync_count = 0;
5451 mtx_unlock(&lun->lun_lock);
5452 retval = lun->backend->config_write((union ctl_io *)ctsio);
5454 mtx_unlock(&lun->lun_lock);
5455 ctl_set_success(ctsio);
5456 ctl_done((union ctl_io *)ctsio);
5465 ctl_format(struct ctl_scsiio *ctsio)
5467 struct scsi_format *cdb;
5468 struct ctl_lun *lun;
5469 int length, defect_list_len;
5471 CTL_DEBUG_PRINT(("ctl_format\n"));
5473 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5475 cdb = (struct scsi_format *)ctsio->cdb;
5478 if (cdb->byte2 & SF_FMTDATA) {
5479 if (cdb->byte2 & SF_LONGLIST)
5480 length = sizeof(struct scsi_format_header_long);
5482 length = sizeof(struct scsi_format_header_short);
5485 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5487 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5488 ctsio->kern_data_len = length;
5489 ctsio->kern_total_len = length;
5490 ctsio->kern_data_resid = 0;
5491 ctsio->kern_rel_offset = 0;
5492 ctsio->kern_sg_entries = 0;
5493 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5494 ctsio->be_move_done = ctl_config_move_done;
5495 ctl_datamove((union ctl_io *)ctsio);
5497 return (CTL_RETVAL_COMPLETE);
5500 defect_list_len = 0;
5502 if (cdb->byte2 & SF_FMTDATA) {
5503 if (cdb->byte2 & SF_LONGLIST) {
5504 struct scsi_format_header_long *header;
5506 header = (struct scsi_format_header_long *)
5507 ctsio->kern_data_ptr;
5509 defect_list_len = scsi_4btoul(header->defect_list_len);
5510 if (defect_list_len != 0) {
5511 ctl_set_invalid_field(ctsio,
5520 struct scsi_format_header_short *header;
5522 header = (struct scsi_format_header_short *)
5523 ctsio->kern_data_ptr;
5525 defect_list_len = scsi_2btoul(header->defect_list_len);
5526 if (defect_list_len != 0) {
5527 ctl_set_invalid_field(ctsio,
5539 * The format command will clear out the "Medium format corrupted"
5540 * status if set by the configuration code. That status is really
5541 * just a way to notify the host that we have lost the media, and
5542 * get them to issue a command that will basically make them think
5543 * they're blowing away the media.
5545 mtx_lock(&lun->lun_lock);
5546 lun->flags &= ~CTL_LUN_INOPERABLE;
5547 mtx_unlock(&lun->lun_lock);
5549 ctl_set_success(ctsio);
5552 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5553 free(ctsio->kern_data_ptr, M_CTL);
5554 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5557 ctl_done((union ctl_io *)ctsio);
5558 return (CTL_RETVAL_COMPLETE);
5562 ctl_read_buffer(struct ctl_scsiio *ctsio)
5564 struct scsi_read_buffer *cdb;
5565 struct ctl_lun *lun;
5566 int buffer_offset, len;
5567 static uint8_t descr[4];
5568 static uint8_t echo_descr[4] = { 0 };
5570 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5572 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5573 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5575 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA &&
5576 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR &&
5577 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) {
5578 ctl_set_invalid_field(ctsio,
5584 ctl_done((union ctl_io *)ctsio);
5585 return (CTL_RETVAL_COMPLETE);
5588 len = scsi_3btoul(cdb->length);
5589 buffer_offset = scsi_3btoul(cdb->offset);
5591 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5592 ctl_set_invalid_field(ctsio,
5598 ctl_done((union ctl_io *)ctsio);
5599 return (CTL_RETVAL_COMPLETE);
5602 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5604 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5605 ctsio->kern_data_ptr = descr;
5606 len = min(len, sizeof(descr));
5607 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5608 ctsio->kern_data_ptr = echo_descr;
5609 len = min(len, sizeof(echo_descr));
5611 if (lun->write_buffer == NULL) {
5612 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5615 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5617 ctsio->kern_data_len = len;
5618 ctsio->kern_total_len = len;
5619 ctsio->kern_data_resid = 0;
5620 ctsio->kern_rel_offset = 0;
5621 ctsio->kern_sg_entries = 0;
5622 ctl_set_success(ctsio);
5623 ctsio->be_move_done = ctl_config_move_done;
5624 ctl_datamove((union ctl_io *)ctsio);
5625 return (CTL_RETVAL_COMPLETE);
5629 ctl_write_buffer(struct ctl_scsiio *ctsio)
5631 struct scsi_write_buffer *cdb;
5632 struct ctl_lun *lun;
5633 int buffer_offset, len;
5635 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5637 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5638 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5640 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5641 ctl_set_invalid_field(ctsio,
5647 ctl_done((union ctl_io *)ctsio);
5648 return (CTL_RETVAL_COMPLETE);
5651 len = scsi_3btoul(cdb->length);
5652 buffer_offset = scsi_3btoul(cdb->offset);
5654 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5655 ctl_set_invalid_field(ctsio,
5661 ctl_done((union ctl_io *)ctsio);
5662 return (CTL_RETVAL_COMPLETE);
5666 * If we've got a kernel request that hasn't been malloced yet,
5667 * malloc it and tell the caller the data buffer is here.
5669 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5670 if (lun->write_buffer == NULL) {
5671 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5674 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5675 ctsio->kern_data_len = len;
5676 ctsio->kern_total_len = len;
5677 ctsio->kern_data_resid = 0;
5678 ctsio->kern_rel_offset = 0;
5679 ctsio->kern_sg_entries = 0;
5680 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5681 ctsio->be_move_done = ctl_config_move_done;
5682 ctl_datamove((union ctl_io *)ctsio);
5684 return (CTL_RETVAL_COMPLETE);
5687 ctl_set_success(ctsio);
5688 ctl_done((union ctl_io *)ctsio);
5689 return (CTL_RETVAL_COMPLETE);
5693 ctl_write_same(struct ctl_scsiio *ctsio)
5695 struct ctl_lun *lun;
5696 struct ctl_lba_len_flags *lbalen;
5698 uint32_t num_blocks;
5702 retval = CTL_RETVAL_COMPLETE;
5704 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5706 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5708 switch (ctsio->cdb[0]) {
5709 case WRITE_SAME_10: {
5710 struct scsi_write_same_10 *cdb;
5712 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5714 lba = scsi_4btoul(cdb->addr);
5715 num_blocks = scsi_2btoul(cdb->length);
5719 case WRITE_SAME_16: {
5720 struct scsi_write_same_16 *cdb;
5722 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5724 lba = scsi_8btou64(cdb->addr);
5725 num_blocks = scsi_4btoul(cdb->length);
5731 * We got a command we don't support. This shouldn't
5732 * happen, commands should be filtered out above us.
5734 ctl_set_invalid_opcode(ctsio);
5735 ctl_done((union ctl_io *)ctsio);
5737 return (CTL_RETVAL_COMPLETE);
5738 break; /* NOTREACHED */
5741 /* NDOB and ANCHOR flags can be used only together with UNMAP */
5742 if ((byte2 & SWS_UNMAP) == 0 &&
5743 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) {
5744 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5745 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5746 ctl_done((union ctl_io *)ctsio);
5747 return (CTL_RETVAL_COMPLETE);
5751 * The first check is to make sure we're in bounds, the second
5752 * check is to catch wrap-around problems. If the lba + num blocks
5753 * is less than the lba, then we've wrapped around and the block
5754 * range is invalid anyway.
5756 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5757 || ((lba + num_blocks) < lba)) {
5758 ctl_set_lba_out_of_range(ctsio);
5759 ctl_done((union ctl_io *)ctsio);
5760 return (CTL_RETVAL_COMPLETE);
5763 /* Zero number of blocks means "to the last logical block" */
5764 if (num_blocks == 0) {
5765 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5766 ctl_set_invalid_field(ctsio,
5772 ctl_done((union ctl_io *)ctsio);
5773 return (CTL_RETVAL_COMPLETE);
5775 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5778 len = lun->be_lun->blocksize;
5781 * If we've got a kernel request that hasn't been malloced yet,
5782 * malloc it and tell the caller the data buffer is here.
5784 if ((byte2 & SWS_NDOB) == 0 &&
5785 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5786 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5787 ctsio->kern_data_len = len;
5788 ctsio->kern_total_len = len;
5789 ctsio->kern_data_resid = 0;
5790 ctsio->kern_rel_offset = 0;
5791 ctsio->kern_sg_entries = 0;
5792 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5793 ctsio->be_move_done = ctl_config_move_done;
5794 ctl_datamove((union ctl_io *)ctsio);
5796 return (CTL_RETVAL_COMPLETE);
5799 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5801 lbalen->len = num_blocks;
5802 lbalen->flags = byte2;
5803 retval = lun->backend->config_write((union ctl_io *)ctsio);
5809 ctl_unmap(struct ctl_scsiio *ctsio)
5811 struct ctl_lun *lun;
5812 struct scsi_unmap *cdb;
5813 struct ctl_ptr_len_flags *ptrlen;
5814 struct scsi_unmap_header *hdr;
5815 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5817 uint32_t num_blocks;
5821 retval = CTL_RETVAL_COMPLETE;
5823 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5825 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5826 cdb = (struct scsi_unmap *)ctsio->cdb;
5828 len = scsi_2btoul(cdb->length);
5832 * If we've got a kernel request that hasn't been malloced yet,
5833 * malloc it and tell the caller the data buffer is here.
5835 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5836 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5837 ctsio->kern_data_len = len;
5838 ctsio->kern_total_len = len;
5839 ctsio->kern_data_resid = 0;
5840 ctsio->kern_rel_offset = 0;
5841 ctsio->kern_sg_entries = 0;
5842 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5843 ctsio->be_move_done = ctl_config_move_done;
5844 ctl_datamove((union ctl_io *)ctsio);
5846 return (CTL_RETVAL_COMPLETE);
5849 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5850 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5851 if (len < sizeof (*hdr) ||
5852 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5853 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5854 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5855 ctl_set_invalid_field(ctsio,
5863 len = scsi_2btoul(hdr->desc_length);
5864 buf = (struct scsi_unmap_desc *)(hdr + 1);
5865 end = buf + len / sizeof(*buf);
5868 for (range = buf; range < end; range++) {
5869 lba = scsi_8btou64(range->lba);
5870 num_blocks = scsi_4btoul(range->length);
5871 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5872 || ((lba + num_blocks) < lba)) {
5873 ctl_set_lba_out_of_range(ctsio);
5874 ctl_done((union ctl_io *)ctsio);
5875 return (CTL_RETVAL_COMPLETE);
5877 if (num_blocks != 0)
5882 * Block backend can not handle zero last range.
5883 * Filter it out and return if there is nothing left.
5885 len = (uint8_t *)endnz - (uint8_t *)buf;
5887 ctl_set_success(ctsio);
5891 mtx_lock(&lun->lun_lock);
5892 ptrlen = (struct ctl_ptr_len_flags *)
5893 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5894 ptrlen->ptr = (void *)buf;
5896 ptrlen->flags = byte2;
5897 ctl_check_blocked(lun);
5898 mtx_unlock(&lun->lun_lock);
5900 retval = lun->backend->config_write((union ctl_io *)ctsio);
5904 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5905 free(ctsio->kern_data_ptr, M_CTL);
5906 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5908 ctl_done((union ctl_io *)ctsio);
5909 return (CTL_RETVAL_COMPLETE);
5913 * Note that this function currently doesn't actually do anything inside
5914 * CTL to enforce things if the DQue bit is turned on.
5916 * Also note that this function can't be used in the default case, because
5917 * the DQue bit isn't set in the changeable mask for the control mode page
5918 * anyway. This is just here as an example for how to implement a page
5919 * handler, and a placeholder in case we want to allow the user to turn
5920 * tagged queueing on and off.
5922 * The D_SENSE bit handling is functional, however, and will turn
5923 * descriptor sense on and off for a given LUN.
5926 ctl_control_page_handler(struct ctl_scsiio *ctsio,
5927 struct ctl_page_index *page_index, uint8_t *page_ptr)
5929 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
5930 struct ctl_lun *lun;
5934 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5935 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5938 user_cp = (struct scsi_control_page *)page_ptr;
5939 current_cp = (struct scsi_control_page *)
5940 (page_index->page_data + (page_index->page_len *
5942 saved_cp = (struct scsi_control_page *)
5943 (page_index->page_data + (page_index->page_len *
5946 mtx_lock(&lun->lun_lock);
5947 if (((current_cp->rlec & SCP_DSENSE) == 0)
5948 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
5950 * Descriptor sense is currently turned off and the user
5951 * wants to turn it on.
5953 current_cp->rlec |= SCP_DSENSE;
5954 saved_cp->rlec |= SCP_DSENSE;
5955 lun->flags |= CTL_LUN_SENSE_DESC;
5957 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
5958 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
5960 * Descriptor sense is currently turned on, and the user
5961 * wants to turn it off.
5963 current_cp->rlec &= ~SCP_DSENSE;
5964 saved_cp->rlec &= ~SCP_DSENSE;
5965 lun->flags &= ~CTL_LUN_SENSE_DESC;
5968 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) !=
5969 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) {
5970 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
5971 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
5972 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
5973 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
5976 if ((current_cp->eca_and_aen & SCP_SWP) !=
5977 (user_cp->eca_and_aen & SCP_SWP)) {
5978 current_cp->eca_and_aen &= ~SCP_SWP;
5979 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
5980 saved_cp->eca_and_aen &= ~SCP_SWP;
5981 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
5985 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5986 mtx_unlock(&lun->lun_lock);
5992 ctl_caching_sp_handler(struct ctl_scsiio *ctsio,
5993 struct ctl_page_index *page_index, uint8_t *page_ptr)
5995 struct scsi_caching_page *current_cp, *saved_cp, *user_cp;
5996 struct ctl_lun *lun;
6000 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6001 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6004 user_cp = (struct scsi_caching_page *)page_ptr;
6005 current_cp = (struct scsi_caching_page *)
6006 (page_index->page_data + (page_index->page_len *
6008 saved_cp = (struct scsi_caching_page *)
6009 (page_index->page_data + (page_index->page_len *
6012 mtx_lock(&lun->lun_lock);
6013 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) !=
6014 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) {
6015 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
6016 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
6017 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
6018 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
6022 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
6023 mtx_unlock(&lun->lun_lock);
6029 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
6030 struct ctl_page_index *page_index,
6036 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
6041 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
6042 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
6043 printf("page data:");
6045 printf(" %.2x",page_ptr[i]);
6051 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
6052 struct ctl_page_index *page_index,
6055 struct copan_debugconf_subpage *page;
6057 page = (struct copan_debugconf_subpage *)page_index->page_data +
6058 (page_index->page_len * pc);
6061 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6062 case SMS_PAGE_CTRL_DEFAULT >> 6:
6063 case SMS_PAGE_CTRL_SAVED >> 6:
6065 * We don't update the changable or default bits for this page.
6068 case SMS_PAGE_CTRL_CURRENT >> 6:
6069 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
6070 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
6074 EPRINT(0, "Invalid PC %d!!", pc);
6075 #endif /* NEEDTOPORT */
6083 ctl_do_mode_select(union ctl_io *io)
6085 struct scsi_mode_page_header *page_header;
6086 struct ctl_page_index *page_index;
6087 struct ctl_scsiio *ctsio;
6088 int control_dev, page_len;
6089 int page_len_offset, page_len_size;
6090 union ctl_modepage_info *modepage_info;
6091 struct ctl_lun *lun;
6092 int *len_left, *len_used;
6095 ctsio = &io->scsiio;
6098 retval = CTL_RETVAL_COMPLETE;
6100 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6102 if (lun->be_lun->lun_type != T_DIRECT)
6107 modepage_info = (union ctl_modepage_info *)
6108 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6109 len_left = &modepage_info->header.len_left;
6110 len_used = &modepage_info->header.len_used;
6114 page_header = (struct scsi_mode_page_header *)
6115 (ctsio->kern_data_ptr + *len_used);
6117 if (*len_left == 0) {
6118 free(ctsio->kern_data_ptr, M_CTL);
6119 ctl_set_success(ctsio);
6120 ctl_done((union ctl_io *)ctsio);
6121 return (CTL_RETVAL_COMPLETE);
6122 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6124 free(ctsio->kern_data_ptr, M_CTL);
6125 ctl_set_param_len_error(ctsio);
6126 ctl_done((union ctl_io *)ctsio);
6127 return (CTL_RETVAL_COMPLETE);
6129 } else if ((page_header->page_code & SMPH_SPF)
6130 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6132 free(ctsio->kern_data_ptr, M_CTL);
6133 ctl_set_param_len_error(ctsio);
6134 ctl_done((union ctl_io *)ctsio);
6135 return (CTL_RETVAL_COMPLETE);
6140 * XXX KDM should we do something with the block descriptor?
6142 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6144 if ((control_dev != 0)
6145 && (lun->mode_pages.index[i].page_flags &
6146 CTL_PAGE_FLAG_DISK_ONLY))
6149 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
6150 (page_header->page_code & SMPH_PC_MASK))
6154 * If neither page has a subpage code, then we've got a
6157 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
6158 && ((page_header->page_code & SMPH_SPF) == 0)) {
6159 page_index = &lun->mode_pages.index[i];
6160 page_len = page_header->page_length;
6165 * If both pages have subpages, then the subpage numbers
6168 if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
6169 && (page_header->page_code & SMPH_SPF)) {
6170 struct scsi_mode_page_header_sp *sph;
6172 sph = (struct scsi_mode_page_header_sp *)page_header;
6174 if (lun->mode_pages.index[i].subpage ==
6176 page_index = &lun->mode_pages.index[i];
6177 page_len = scsi_2btoul(sph->page_length);
6184 * If we couldn't find the page, or if we don't have a mode select
6185 * handler for it, send back an error to the user.
6187 if ((page_index == NULL)
6188 || (page_index->select_handler == NULL)) {
6189 ctl_set_invalid_field(ctsio,
6192 /*field*/ *len_used,
6195 free(ctsio->kern_data_ptr, M_CTL);
6196 ctl_done((union ctl_io *)ctsio);
6197 return (CTL_RETVAL_COMPLETE);
6200 if (page_index->page_code & SMPH_SPF) {
6201 page_len_offset = 2;
6205 page_len_offset = 1;
6209 * If the length the initiator gives us isn't the one we specify in
6210 * the mode page header, or if they didn't specify enough data in
6211 * the CDB to avoid truncating this page, kick out the request.
6213 if ((page_len != (page_index->page_len - page_len_offset -
6215 || (*len_left < page_index->page_len)) {
6218 ctl_set_invalid_field(ctsio,
6221 /*field*/ *len_used + page_len_offset,
6224 free(ctsio->kern_data_ptr, M_CTL);
6225 ctl_done((union ctl_io *)ctsio);
6226 return (CTL_RETVAL_COMPLETE);
6230 * Run through the mode page, checking to make sure that the bits
6231 * the user changed are actually legal for him to change.
6233 for (i = 0; i < page_index->page_len; i++) {
6234 uint8_t *user_byte, *change_mask, *current_byte;
6238 user_byte = (uint8_t *)page_header + i;
6239 change_mask = page_index->page_data +
6240 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6241 current_byte = page_index->page_data +
6242 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6245 * Check to see whether the user set any bits in this byte
6246 * that he is not allowed to set.
6248 if ((*user_byte & ~(*change_mask)) ==
6249 (*current_byte & ~(*change_mask)))
6253 * Go through bit by bit to determine which one is illegal.
6256 for (j = 7; j >= 0; j--) {
6257 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6258 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6263 ctl_set_invalid_field(ctsio,
6266 /*field*/ *len_used + i,
6269 free(ctsio->kern_data_ptr, M_CTL);
6270 ctl_done((union ctl_io *)ctsio);
6271 return (CTL_RETVAL_COMPLETE);
6275 * Decrement these before we call the page handler, since we may
6276 * end up getting called back one way or another before the handler
6277 * returns to this context.
6279 *len_left -= page_index->page_len;
6280 *len_used += page_index->page_len;
6282 retval = page_index->select_handler(ctsio, page_index,
6283 (uint8_t *)page_header);
6286 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6287 * wait until this queued command completes to finish processing
6288 * the mode page. If it returns anything other than
6289 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6290 * already set the sense information, freed the data pointer, and
6291 * completed the io for us.
6293 if (retval != CTL_RETVAL_COMPLETE)
6294 goto bailout_no_done;
6297 * If the initiator sent us more than one page, parse the next one.
6302 ctl_set_success(ctsio);
6303 free(ctsio->kern_data_ptr, M_CTL);
6304 ctl_done((union ctl_io *)ctsio);
6308 return (CTL_RETVAL_COMPLETE);
6313 ctl_mode_select(struct ctl_scsiio *ctsio)
6315 int param_len, pf, sp;
6316 int header_size, bd_len;
6317 int len_left, len_used;
6318 struct ctl_page_index *page_index;
6319 struct ctl_lun *lun;
6320 int control_dev, page_len;
6321 union ctl_modepage_info *modepage_info;
6333 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6335 if (lun->be_lun->lun_type != T_DIRECT)
6340 switch (ctsio->cdb[0]) {
6341 case MODE_SELECT_6: {
6342 struct scsi_mode_select_6 *cdb;
6344 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6346 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6347 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6349 param_len = cdb->length;
6350 header_size = sizeof(struct scsi_mode_header_6);
6353 case MODE_SELECT_10: {
6354 struct scsi_mode_select_10 *cdb;
6356 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6358 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6359 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6361 param_len = scsi_2btoul(cdb->length);
6362 header_size = sizeof(struct scsi_mode_header_10);
6366 ctl_set_invalid_opcode(ctsio);
6367 ctl_done((union ctl_io *)ctsio);
6368 return (CTL_RETVAL_COMPLETE);
6369 break; /* NOTREACHED */
6374 * "A parameter list length of zero indicates that the Data-Out Buffer
6375 * shall be empty. This condition shall not be considered as an error."
6377 if (param_len == 0) {
6378 ctl_set_success(ctsio);
6379 ctl_done((union ctl_io *)ctsio);
6380 return (CTL_RETVAL_COMPLETE);
6384 * Since we'll hit this the first time through, prior to
6385 * allocation, we don't need to free a data buffer here.
6387 if (param_len < header_size) {
6388 ctl_set_param_len_error(ctsio);
6389 ctl_done((union ctl_io *)ctsio);
6390 return (CTL_RETVAL_COMPLETE);
6394 * Allocate the data buffer and grab the user's data. In theory,
6395 * we shouldn't have to sanity check the parameter list length here
6396 * because the maximum size is 64K. We should be able to malloc
6397 * that much without too many problems.
6399 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6400 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6401 ctsio->kern_data_len = param_len;
6402 ctsio->kern_total_len = param_len;
6403 ctsio->kern_data_resid = 0;
6404 ctsio->kern_rel_offset = 0;
6405 ctsio->kern_sg_entries = 0;
6406 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6407 ctsio->be_move_done = ctl_config_move_done;
6408 ctl_datamove((union ctl_io *)ctsio);
6410 return (CTL_RETVAL_COMPLETE);
6413 switch (ctsio->cdb[0]) {
6414 case MODE_SELECT_6: {
6415 struct scsi_mode_header_6 *mh6;
6417 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6418 bd_len = mh6->blk_desc_len;
6421 case MODE_SELECT_10: {
6422 struct scsi_mode_header_10 *mh10;
6424 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6425 bd_len = scsi_2btoul(mh10->blk_desc_len);
6429 panic("Invalid CDB type %#x", ctsio->cdb[0]);
6433 if (param_len < (header_size + bd_len)) {
6434 free(ctsio->kern_data_ptr, M_CTL);
6435 ctl_set_param_len_error(ctsio);
6436 ctl_done((union ctl_io *)ctsio);
6437 return (CTL_RETVAL_COMPLETE);
6441 * Set the IO_CONT flag, so that if this I/O gets passed to
6442 * ctl_config_write_done(), it'll get passed back to
6443 * ctl_do_mode_select() for further processing, or completion if
6446 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6447 ctsio->io_cont = ctl_do_mode_select;
6449 modepage_info = (union ctl_modepage_info *)
6450 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6452 memset(modepage_info, 0, sizeof(*modepage_info));
6454 len_left = param_len - header_size - bd_len;
6455 len_used = header_size + bd_len;
6457 modepage_info->header.len_left = len_left;
6458 modepage_info->header.len_used = len_used;
6460 return (ctl_do_mode_select((union ctl_io *)ctsio));
6464 ctl_mode_sense(struct ctl_scsiio *ctsio)
6466 struct ctl_lun *lun;
6467 int pc, page_code, dbd, llba, subpage;
6468 int alloc_len, page_len, header_len, total_len;
6469 struct scsi_mode_block_descr *block_desc;
6470 struct ctl_page_index *page_index;
6478 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6480 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6482 if (lun->be_lun->lun_type != T_DIRECT)
6487 switch (ctsio->cdb[0]) {
6488 case MODE_SENSE_6: {
6489 struct scsi_mode_sense_6 *cdb;
6491 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6493 header_len = sizeof(struct scsi_mode_hdr_6);
6494 if (cdb->byte2 & SMS_DBD)
6497 header_len += sizeof(struct scsi_mode_block_descr);
6499 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6500 page_code = cdb->page & SMS_PAGE_CODE;
6501 subpage = cdb->subpage;
6502 alloc_len = cdb->length;
6505 case MODE_SENSE_10: {
6506 struct scsi_mode_sense_10 *cdb;
6508 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6510 header_len = sizeof(struct scsi_mode_hdr_10);
6512 if (cdb->byte2 & SMS_DBD)
6515 header_len += sizeof(struct scsi_mode_block_descr);
6516 if (cdb->byte2 & SMS10_LLBAA)
6518 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6519 page_code = cdb->page & SMS_PAGE_CODE;
6520 subpage = cdb->subpage;
6521 alloc_len = scsi_2btoul(cdb->length);
6525 ctl_set_invalid_opcode(ctsio);
6526 ctl_done((union ctl_io *)ctsio);
6527 return (CTL_RETVAL_COMPLETE);
6528 break; /* NOTREACHED */
6532 * We have to make a first pass through to calculate the size of
6533 * the pages that match the user's query. Then we allocate enough
6534 * memory to hold it, and actually copy the data into the buffer.
6536 switch (page_code) {
6537 case SMS_ALL_PAGES_PAGE: {
6543 * At the moment, values other than 0 and 0xff here are
6544 * reserved according to SPC-3.
6546 if ((subpage != SMS_SUBPAGE_PAGE_0)
6547 && (subpage != SMS_SUBPAGE_ALL)) {
6548 ctl_set_invalid_field(ctsio,
6554 ctl_done((union ctl_io *)ctsio);
6555 return (CTL_RETVAL_COMPLETE);
6558 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6559 if ((control_dev != 0)
6560 && (lun->mode_pages.index[i].page_flags &
6561 CTL_PAGE_FLAG_DISK_ONLY))
6565 * We don't use this subpage if the user didn't
6566 * request all subpages.
6568 if ((lun->mode_pages.index[i].subpage != 0)
6569 && (subpage == SMS_SUBPAGE_PAGE_0))
6573 printf("found page %#x len %d\n",
6574 lun->mode_pages.index[i].page_code &
6576 lun->mode_pages.index[i].page_len);
6578 page_len += lun->mode_pages.index[i].page_len;
6587 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6588 /* Look for the right page code */
6589 if ((lun->mode_pages.index[i].page_code &
6590 SMPH_PC_MASK) != page_code)
6593 /* Look for the right subpage or the subpage wildcard*/
6594 if ((lun->mode_pages.index[i].subpage != subpage)
6595 && (subpage != SMS_SUBPAGE_ALL))
6598 /* Make sure the page is supported for this dev type */
6599 if ((control_dev != 0)
6600 && (lun->mode_pages.index[i].page_flags &
6601 CTL_PAGE_FLAG_DISK_ONLY))
6605 printf("found page %#x len %d\n",
6606 lun->mode_pages.index[i].page_code &
6608 lun->mode_pages.index[i].page_len);
6611 page_len += lun->mode_pages.index[i].page_len;
6614 if (page_len == 0) {
6615 ctl_set_invalid_field(ctsio,
6621 ctl_done((union ctl_io *)ctsio);
6622 return (CTL_RETVAL_COMPLETE);
6628 total_len = header_len + page_len;
6630 printf("header_len = %d, page_len = %d, total_len = %d\n",
6631 header_len, page_len, total_len);
6634 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6635 ctsio->kern_sg_entries = 0;
6636 ctsio->kern_data_resid = 0;
6637 ctsio->kern_rel_offset = 0;
6638 if (total_len < alloc_len) {
6639 ctsio->residual = alloc_len - total_len;
6640 ctsio->kern_data_len = total_len;
6641 ctsio->kern_total_len = total_len;
6643 ctsio->residual = 0;
6644 ctsio->kern_data_len = alloc_len;
6645 ctsio->kern_total_len = alloc_len;
6648 switch (ctsio->cdb[0]) {
6649 case MODE_SENSE_6: {
6650 struct scsi_mode_hdr_6 *header;
6652 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6654 header->datalen = MIN(total_len - 1, 254);
6655 if (control_dev == 0) {
6656 header->dev_specific = 0x10; /* DPOFUA */
6657 if ((lun->flags & CTL_LUN_READONLY) ||
6658 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6659 .eca_and_aen & SCP_SWP) != 0)
6660 header->dev_specific |= 0x80; /* WP */
6663 header->block_descr_len = 0;
6665 header->block_descr_len =
6666 sizeof(struct scsi_mode_block_descr);
6667 block_desc = (struct scsi_mode_block_descr *)&header[1];
6670 case MODE_SENSE_10: {
6671 struct scsi_mode_hdr_10 *header;
6674 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6676 datalen = MIN(total_len - 2, 65533);
6677 scsi_ulto2b(datalen, header->datalen);
6678 if (control_dev == 0) {
6679 header->dev_specific = 0x10; /* DPOFUA */
6680 if ((lun->flags & CTL_LUN_READONLY) ||
6681 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6682 .eca_and_aen & SCP_SWP) != 0)
6683 header->dev_specific |= 0x80; /* WP */
6686 scsi_ulto2b(0, header->block_descr_len);
6688 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6689 header->block_descr_len);
6690 block_desc = (struct scsi_mode_block_descr *)&header[1];
6694 panic("invalid CDB type %#x", ctsio->cdb[0]);
6695 break; /* NOTREACHED */
6699 * If we've got a disk, use its blocksize in the block
6700 * descriptor. Otherwise, just set it to 0.
6703 if (control_dev == 0)
6704 scsi_ulto3b(lun->be_lun->blocksize,
6705 block_desc->block_len);
6707 scsi_ulto3b(0, block_desc->block_len);
6710 switch (page_code) {
6711 case SMS_ALL_PAGES_PAGE: {
6714 data_used = header_len;
6715 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6716 struct ctl_page_index *page_index;
6718 page_index = &lun->mode_pages.index[i];
6720 if ((control_dev != 0)
6721 && (page_index->page_flags &
6722 CTL_PAGE_FLAG_DISK_ONLY))
6726 * We don't use this subpage if the user didn't
6727 * request all subpages. We already checked (above)
6728 * to make sure the user only specified a subpage
6729 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6731 if ((page_index->subpage != 0)
6732 && (subpage == SMS_SUBPAGE_PAGE_0))
6736 * Call the handler, if it exists, to update the
6737 * page to the latest values.
6739 if (page_index->sense_handler != NULL)
6740 page_index->sense_handler(ctsio, page_index,pc);
6742 memcpy(ctsio->kern_data_ptr + data_used,
6743 page_index->page_data +
6744 (page_index->page_len * pc),
6745 page_index->page_len);
6746 data_used += page_index->page_len;
6753 data_used = header_len;
6755 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6756 struct ctl_page_index *page_index;
6758 page_index = &lun->mode_pages.index[i];
6760 /* Look for the right page code */
6761 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6764 /* Look for the right subpage or the subpage wildcard*/
6765 if ((page_index->subpage != subpage)
6766 && (subpage != SMS_SUBPAGE_ALL))
6769 /* Make sure the page is supported for this dev type */
6770 if ((control_dev != 0)
6771 && (page_index->page_flags &
6772 CTL_PAGE_FLAG_DISK_ONLY))
6776 * Call the handler, if it exists, to update the
6777 * page to the latest values.
6779 if (page_index->sense_handler != NULL)
6780 page_index->sense_handler(ctsio, page_index,pc);
6782 memcpy(ctsio->kern_data_ptr + data_used,
6783 page_index->page_data +
6784 (page_index->page_len * pc),
6785 page_index->page_len);
6786 data_used += page_index->page_len;
6792 ctl_set_success(ctsio);
6793 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6794 ctsio->be_move_done = ctl_config_move_done;
6795 ctl_datamove((union ctl_io *)ctsio);
6796 return (CTL_RETVAL_COMPLETE);
6800 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6801 struct ctl_page_index *page_index,
6804 struct ctl_lun *lun;
6805 struct scsi_log_param_header *phdr;
6809 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6810 data = page_index->page_data;
6812 if (lun->backend->lun_attr != NULL &&
6813 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6815 phdr = (struct scsi_log_param_header *)data;
6816 scsi_ulto2b(0x0001, phdr->param_code);
6817 phdr->param_control = SLP_LBIN | SLP_LP;
6818 phdr->param_len = 8;
6819 data = (uint8_t *)(phdr + 1);
6820 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6821 data[4] = 0x02; /* per-pool */
6822 data += phdr->param_len;
6825 if (lun->backend->lun_attr != NULL &&
6826 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6828 phdr = (struct scsi_log_param_header *)data;
6829 scsi_ulto2b(0x0002, phdr->param_code);
6830 phdr->param_control = SLP_LBIN | SLP_LP;
6831 phdr->param_len = 8;
6832 data = (uint8_t *)(phdr + 1);
6833 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6834 data[4] = 0x01; /* per-LUN */
6835 data += phdr->param_len;
6838 if (lun->backend->lun_attr != NULL &&
6839 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6841 phdr = (struct scsi_log_param_header *)data;
6842 scsi_ulto2b(0x00f1, phdr->param_code);
6843 phdr->param_control = SLP_LBIN | SLP_LP;
6844 phdr->param_len = 8;
6845 data = (uint8_t *)(phdr + 1);
6846 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6847 data[4] = 0x02; /* per-pool */
6848 data += phdr->param_len;
6851 if (lun->backend->lun_attr != NULL &&
6852 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6854 phdr = (struct scsi_log_param_header *)data;
6855 scsi_ulto2b(0x00f2, phdr->param_code);
6856 phdr->param_control = SLP_LBIN | SLP_LP;
6857 phdr->param_len = 8;
6858 data = (uint8_t *)(phdr + 1);
6859 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6860 data[4] = 0x02; /* per-pool */
6861 data += phdr->param_len;
6864 page_index->page_len = data - page_index->page_data;
6869 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
6870 struct ctl_page_index *page_index,
6873 struct ctl_lun *lun;
6874 struct stat_page *data;
6875 uint64_t rn, wn, rb, wb;
6876 struct bintime rt, wt;
6879 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6880 data = (struct stat_page *)page_index->page_data;
6882 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
6883 data->sap.hdr.param_control = SLP_LBIN;
6884 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
6885 sizeof(struct scsi_log_param_header);
6886 rn = wn = rb = wb = 0;
6889 for (i = 0; i < CTL_MAX_PORTS; i++) {
6890 rn += lun->stats.ports[i].operations[CTL_STATS_READ];
6891 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE];
6892 rb += lun->stats.ports[i].bytes[CTL_STATS_READ];
6893 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE];
6894 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]);
6895 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]);
6897 scsi_u64to8b(rn, data->sap.read_num);
6898 scsi_u64to8b(wn, data->sap.write_num);
6899 if (lun->stats.blocksize > 0) {
6900 scsi_u64to8b(wb / lun->stats.blocksize,
6901 data->sap.recvieved_lba);
6902 scsi_u64to8b(rb / lun->stats.blocksize,
6903 data->sap.transmitted_lba);
6905 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000),
6906 data->sap.read_int);
6907 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000),
6908 data->sap.write_int);
6909 scsi_u64to8b(0, data->sap.weighted_num);
6910 scsi_u64to8b(0, data->sap.weighted_int);
6911 scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
6912 data->it.hdr.param_control = SLP_LBIN;
6913 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
6914 sizeof(struct scsi_log_param_header);
6916 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
6918 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
6919 data->it.hdr.param_control = SLP_LBIN;
6920 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
6921 sizeof(struct scsi_log_param_header);
6922 scsi_ulto4b(3, data->ti.exponent);
6923 scsi_ulto4b(1, data->ti.integer);
6925 page_index->page_len = sizeof(*data);
6930 ctl_log_sense(struct ctl_scsiio *ctsio)
6932 struct ctl_lun *lun;
6933 int i, pc, page_code, subpage;
6934 int alloc_len, total_len;
6935 struct ctl_page_index *page_index;
6936 struct scsi_log_sense *cdb;
6937 struct scsi_log_header *header;
6939 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6941 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6942 cdb = (struct scsi_log_sense *)ctsio->cdb;
6943 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6944 page_code = cdb->page & SLS_PAGE_CODE;
6945 subpage = cdb->subpage;
6946 alloc_len = scsi_2btoul(cdb->length);
6949 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6950 page_index = &lun->log_pages.index[i];
6952 /* Look for the right page code */
6953 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6956 /* Look for the right subpage or the subpage wildcard*/
6957 if (page_index->subpage != subpage)
6962 if (i >= CTL_NUM_LOG_PAGES) {
6963 ctl_set_invalid_field(ctsio,
6969 ctl_done((union ctl_io *)ctsio);
6970 return (CTL_RETVAL_COMPLETE);
6973 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6975 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6976 ctsio->kern_sg_entries = 0;
6977 ctsio->kern_data_resid = 0;
6978 ctsio->kern_rel_offset = 0;
6979 if (total_len < alloc_len) {
6980 ctsio->residual = alloc_len - total_len;
6981 ctsio->kern_data_len = total_len;
6982 ctsio->kern_total_len = total_len;
6984 ctsio->residual = 0;
6985 ctsio->kern_data_len = alloc_len;
6986 ctsio->kern_total_len = alloc_len;
6989 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
6990 header->page = page_index->page_code;
6991 if (page_index->subpage) {
6992 header->page |= SL_SPF;
6993 header->subpage = page_index->subpage;
6995 scsi_ulto2b(page_index->page_len, header->datalen);
6998 * Call the handler, if it exists, to update the
6999 * page to the latest values.
7001 if (page_index->sense_handler != NULL)
7002 page_index->sense_handler(ctsio, page_index, pc);
7004 memcpy(header + 1, page_index->page_data, page_index->page_len);
7006 ctl_set_success(ctsio);
7007 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7008 ctsio->be_move_done = ctl_config_move_done;
7009 ctl_datamove((union ctl_io *)ctsio);
7010 return (CTL_RETVAL_COMPLETE);
7014 ctl_read_capacity(struct ctl_scsiio *ctsio)
7016 struct scsi_read_capacity *cdb;
7017 struct scsi_read_capacity_data *data;
7018 struct ctl_lun *lun;
7021 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
7023 cdb = (struct scsi_read_capacity *)ctsio->cdb;
7025 lba = scsi_4btoul(cdb->addr);
7026 if (((cdb->pmi & SRC_PMI) == 0)
7028 ctl_set_invalid_field(/*ctsio*/ ctsio,
7034 ctl_done((union ctl_io *)ctsio);
7035 return (CTL_RETVAL_COMPLETE);
7038 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7040 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7041 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
7042 ctsio->residual = 0;
7043 ctsio->kern_data_len = sizeof(*data);
7044 ctsio->kern_total_len = sizeof(*data);
7045 ctsio->kern_data_resid = 0;
7046 ctsio->kern_rel_offset = 0;
7047 ctsio->kern_sg_entries = 0;
7050 * If the maximum LBA is greater than 0xfffffffe, the user must
7051 * issue a SERVICE ACTION IN (16) command, with the read capacity
7052 * serivce action set.
7054 if (lun->be_lun->maxlba > 0xfffffffe)
7055 scsi_ulto4b(0xffffffff, data->addr);
7057 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
7060 * XXX KDM this may not be 512 bytes...
7062 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7064 ctl_set_success(ctsio);
7065 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7066 ctsio->be_move_done = ctl_config_move_done;
7067 ctl_datamove((union ctl_io *)ctsio);
7068 return (CTL_RETVAL_COMPLETE);
7072 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
7074 struct scsi_read_capacity_16 *cdb;
7075 struct scsi_read_capacity_data_long *data;
7076 struct ctl_lun *lun;
7080 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
7082 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
7084 alloc_len = scsi_4btoul(cdb->alloc_len);
7085 lba = scsi_8btou64(cdb->addr);
7087 if ((cdb->reladr & SRC16_PMI)
7089 ctl_set_invalid_field(/*ctsio*/ ctsio,
7095 ctl_done((union ctl_io *)ctsio);
7096 return (CTL_RETVAL_COMPLETE);
7099 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7101 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7102 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
7104 if (sizeof(*data) < alloc_len) {
7105 ctsio->residual = alloc_len - sizeof(*data);
7106 ctsio->kern_data_len = sizeof(*data);
7107 ctsio->kern_total_len = sizeof(*data);
7109 ctsio->residual = 0;
7110 ctsio->kern_data_len = alloc_len;
7111 ctsio->kern_total_len = alloc_len;
7113 ctsio->kern_data_resid = 0;
7114 ctsio->kern_rel_offset = 0;
7115 ctsio->kern_sg_entries = 0;
7117 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7118 /* XXX KDM this may not be 512 bytes... */
7119 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7120 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7121 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7122 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7123 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
7125 ctl_set_success(ctsio);
7126 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7127 ctsio->be_move_done = ctl_config_move_done;
7128 ctl_datamove((union ctl_io *)ctsio);
7129 return (CTL_RETVAL_COMPLETE);
7133 ctl_get_lba_status(struct ctl_scsiio *ctsio)
7135 struct scsi_get_lba_status *cdb;
7136 struct scsi_get_lba_status_data *data;
7137 struct ctl_lun *lun;
7138 struct ctl_lba_len_flags *lbalen;
7140 uint32_t alloc_len, total_len;
7143 CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
7145 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7146 cdb = (struct scsi_get_lba_status *)ctsio->cdb;
7147 lba = scsi_8btou64(cdb->addr);
7148 alloc_len = scsi_4btoul(cdb->alloc_len);
7150 if (lba > lun->be_lun->maxlba) {
7151 ctl_set_lba_out_of_range(ctsio);
7152 ctl_done((union ctl_io *)ctsio);
7153 return (CTL_RETVAL_COMPLETE);
7156 total_len = sizeof(*data) + sizeof(data->descr[0]);
7157 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7158 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
7160 if (total_len < alloc_len) {
7161 ctsio->residual = alloc_len - total_len;
7162 ctsio->kern_data_len = total_len;
7163 ctsio->kern_total_len = total_len;
7165 ctsio->residual = 0;
7166 ctsio->kern_data_len = alloc_len;
7167 ctsio->kern_total_len = alloc_len;
7169 ctsio->kern_data_resid = 0;
7170 ctsio->kern_rel_offset = 0;
7171 ctsio->kern_sg_entries = 0;
7173 /* Fill dummy data in case backend can't tell anything. */
7174 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
7175 scsi_u64to8b(lba, data->descr[0].addr);
7176 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
7177 data->descr[0].length);
7178 data->descr[0].status = 0; /* Mapped or unknown. */
7180 ctl_set_success(ctsio);
7181 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7182 ctsio->be_move_done = ctl_config_move_done;
7184 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
7186 lbalen->len = total_len;
7188 retval = lun->backend->config_read((union ctl_io *)ctsio);
7189 return (CTL_RETVAL_COMPLETE);
7193 ctl_read_defect(struct ctl_scsiio *ctsio)
7195 struct scsi_read_defect_data_10 *ccb10;
7196 struct scsi_read_defect_data_12 *ccb12;
7197 struct scsi_read_defect_data_hdr_10 *data10;
7198 struct scsi_read_defect_data_hdr_12 *data12;
7199 uint32_t alloc_len, data_len;
7202 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7204 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7205 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7206 format = ccb10->format;
7207 alloc_len = scsi_2btoul(ccb10->alloc_length);
7208 data_len = sizeof(*data10);
7210 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7211 format = ccb12->format;
7212 alloc_len = scsi_4btoul(ccb12->alloc_length);
7213 data_len = sizeof(*data12);
7215 if (alloc_len == 0) {
7216 ctl_set_success(ctsio);
7217 ctl_done((union ctl_io *)ctsio);
7218 return (CTL_RETVAL_COMPLETE);
7221 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7222 if (data_len < alloc_len) {
7223 ctsio->residual = alloc_len - data_len;
7224 ctsio->kern_data_len = data_len;
7225 ctsio->kern_total_len = data_len;
7227 ctsio->residual = 0;
7228 ctsio->kern_data_len = alloc_len;
7229 ctsio->kern_total_len = alloc_len;
7231 ctsio->kern_data_resid = 0;
7232 ctsio->kern_rel_offset = 0;
7233 ctsio->kern_sg_entries = 0;
7235 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7236 data10 = (struct scsi_read_defect_data_hdr_10 *)
7237 ctsio->kern_data_ptr;
7238 data10->format = format;
7239 scsi_ulto2b(0, data10->length);
7241 data12 = (struct scsi_read_defect_data_hdr_12 *)
7242 ctsio->kern_data_ptr;
7243 data12->format = format;
7244 scsi_ulto2b(0, data12->generation);
7245 scsi_ulto4b(0, data12->length);
7248 ctl_set_success(ctsio);
7249 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7250 ctsio->be_move_done = ctl_config_move_done;
7251 ctl_datamove((union ctl_io *)ctsio);
7252 return (CTL_RETVAL_COMPLETE);
7256 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7258 struct scsi_maintenance_in *cdb;
7260 int alloc_len, ext, total_len = 0, g, p, pc, pg, gs, os;
7261 int num_target_port_groups, num_target_ports;
7262 struct ctl_lun *lun;
7263 struct ctl_softc *softc;
7264 struct ctl_port *port;
7265 struct scsi_target_group_data *rtg_ptr;
7266 struct scsi_target_group_data_extended *rtg_ext_ptr;
7267 struct scsi_target_port_group_descriptor *tpg_desc;
7269 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7271 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7272 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7273 softc = lun->ctl_softc;
7275 retval = CTL_RETVAL_COMPLETE;
7277 switch (cdb->byte2 & STG_PDF_MASK) {
7278 case STG_PDF_LENGTH:
7281 case STG_PDF_EXTENDED:
7285 ctl_set_invalid_field(/*ctsio*/ ctsio,
7291 ctl_done((union ctl_io *)ctsio);
7295 if (softc->is_single)
7296 num_target_port_groups = 1;
7298 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
7299 num_target_ports = 0;
7300 mtx_lock(&softc->ctl_lock);
7301 STAILQ_FOREACH(port, &softc->port_list, links) {
7302 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7304 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7308 mtx_unlock(&softc->ctl_lock);
7311 total_len = sizeof(struct scsi_target_group_data_extended);
7313 total_len = sizeof(struct scsi_target_group_data);
7314 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7315 num_target_port_groups +
7316 sizeof(struct scsi_target_port_descriptor) *
7317 num_target_ports * num_target_port_groups;
7319 alloc_len = scsi_4btoul(cdb->length);
7321 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7323 ctsio->kern_sg_entries = 0;
7325 if (total_len < alloc_len) {
7326 ctsio->residual = alloc_len - total_len;
7327 ctsio->kern_data_len = total_len;
7328 ctsio->kern_total_len = total_len;
7330 ctsio->residual = 0;
7331 ctsio->kern_data_len = alloc_len;
7332 ctsio->kern_total_len = alloc_len;
7334 ctsio->kern_data_resid = 0;
7335 ctsio->kern_rel_offset = 0;
7338 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7339 ctsio->kern_data_ptr;
7340 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7341 rtg_ext_ptr->format_type = 0x10;
7342 rtg_ext_ptr->implicit_transition_time = 0;
7343 tpg_desc = &rtg_ext_ptr->groups[0];
7345 rtg_ptr = (struct scsi_target_group_data *)
7346 ctsio->kern_data_ptr;
7347 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7348 tpg_desc = &rtg_ptr->groups[0];
7351 mtx_lock(&softc->ctl_lock);
7352 pg = softc->port_offset / CTL_MAX_PORTS;
7353 if (softc->flags & CTL_FLAG_ACTIVE_SHELF) {
7354 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) {
7355 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7356 os = TPG_ASYMMETRIC_ACCESS_STANDBY;
7357 } else if (lun->flags & CTL_LUN_PRIMARY_SC) {
7358 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7359 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7361 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7362 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7365 gs = TPG_ASYMMETRIC_ACCESS_STANDBY;
7366 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7368 for (g = 0; g < num_target_port_groups; g++) {
7369 tpg_desc->pref_state = (g == pg) ? gs : os;
7370 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP;
7371 scsi_ulto2b(g + 1, tpg_desc->target_port_group);
7372 tpg_desc->status = TPG_IMPLICIT;
7374 STAILQ_FOREACH(port, &softc->port_list, links) {
7375 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7377 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7379 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS;
7380 scsi_ulto2b(p, tpg_desc->descriptors[pc].
7381 relative_target_port_identifier);
7384 tpg_desc->target_port_count = pc;
7385 tpg_desc = (struct scsi_target_port_group_descriptor *)
7386 &tpg_desc->descriptors[pc];
7388 mtx_unlock(&softc->ctl_lock);
7390 ctl_set_success(ctsio);
7391 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7392 ctsio->be_move_done = ctl_config_move_done;
7393 ctl_datamove((union ctl_io *)ctsio);
7398 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7400 struct ctl_lun *lun;
7401 struct scsi_report_supported_opcodes *cdb;
7402 const struct ctl_cmd_entry *entry, *sentry;
7403 struct scsi_report_supported_opcodes_all *all;
7404 struct scsi_report_supported_opcodes_descr *descr;
7405 struct scsi_report_supported_opcodes_one *one;
7407 int alloc_len, total_len;
7408 int opcode, service_action, i, j, num;
7410 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7412 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7413 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7415 retval = CTL_RETVAL_COMPLETE;
7417 opcode = cdb->requested_opcode;
7418 service_action = scsi_2btoul(cdb->requested_service_action);
7419 switch (cdb->options & RSO_OPTIONS_MASK) {
7420 case RSO_OPTIONS_ALL:
7422 for (i = 0; i < 256; i++) {
7423 entry = &ctl_cmd_table[i];
7424 if (entry->flags & CTL_CMD_FLAG_SA5) {
7425 for (j = 0; j < 32; j++) {
7426 sentry = &((const struct ctl_cmd_entry *)
7428 if (ctl_cmd_applicable(
7429 lun->be_lun->lun_type, sentry))
7433 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7438 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7439 num * sizeof(struct scsi_report_supported_opcodes_descr);
7441 case RSO_OPTIONS_OC:
7442 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7443 ctl_set_invalid_field(/*ctsio*/ ctsio,
7449 ctl_done((union ctl_io *)ctsio);
7450 return (CTL_RETVAL_COMPLETE);
7452 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7454 case RSO_OPTIONS_OC_SA:
7455 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7456 service_action >= 32) {
7457 ctl_set_invalid_field(/*ctsio*/ ctsio,
7463 ctl_done((union ctl_io *)ctsio);
7464 return (CTL_RETVAL_COMPLETE);
7466 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7469 ctl_set_invalid_field(/*ctsio*/ ctsio,
7475 ctl_done((union ctl_io *)ctsio);
7476 return (CTL_RETVAL_COMPLETE);
7479 alloc_len = scsi_4btoul(cdb->length);
7481 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7483 ctsio->kern_sg_entries = 0;
7485 if (total_len < alloc_len) {
7486 ctsio->residual = alloc_len - total_len;
7487 ctsio->kern_data_len = total_len;
7488 ctsio->kern_total_len = total_len;
7490 ctsio->residual = 0;
7491 ctsio->kern_data_len = alloc_len;
7492 ctsio->kern_total_len = alloc_len;
7494 ctsio->kern_data_resid = 0;
7495 ctsio->kern_rel_offset = 0;
7497 switch (cdb->options & RSO_OPTIONS_MASK) {
7498 case RSO_OPTIONS_ALL:
7499 all = (struct scsi_report_supported_opcodes_all *)
7500 ctsio->kern_data_ptr;
7502 for (i = 0; i < 256; i++) {
7503 entry = &ctl_cmd_table[i];
7504 if (entry->flags & CTL_CMD_FLAG_SA5) {
7505 for (j = 0; j < 32; j++) {
7506 sentry = &((const struct ctl_cmd_entry *)
7508 if (!ctl_cmd_applicable(
7509 lun->be_lun->lun_type, sentry))
7511 descr = &all->descr[num++];
7513 scsi_ulto2b(j, descr->service_action);
7514 descr->flags = RSO_SERVACTV;
7515 scsi_ulto2b(sentry->length,
7519 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7522 descr = &all->descr[num++];
7524 scsi_ulto2b(0, descr->service_action);
7526 scsi_ulto2b(entry->length, descr->cdb_length);
7530 num * sizeof(struct scsi_report_supported_opcodes_descr),
7533 case RSO_OPTIONS_OC:
7534 one = (struct scsi_report_supported_opcodes_one *)
7535 ctsio->kern_data_ptr;
7536 entry = &ctl_cmd_table[opcode];
7538 case RSO_OPTIONS_OC_SA:
7539 one = (struct scsi_report_supported_opcodes_one *)
7540 ctsio->kern_data_ptr;
7541 entry = &ctl_cmd_table[opcode];
7542 entry = &((const struct ctl_cmd_entry *)
7543 entry->execute)[service_action];
7545 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7547 scsi_ulto2b(entry->length, one->cdb_length);
7548 one->cdb_usage[0] = opcode;
7549 memcpy(&one->cdb_usage[1], entry->usage,
7556 ctl_set_success(ctsio);
7557 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7558 ctsio->be_move_done = ctl_config_move_done;
7559 ctl_datamove((union ctl_io *)ctsio);
7564 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7566 struct scsi_report_supported_tmf *cdb;
7567 struct scsi_report_supported_tmf_data *data;
7569 int alloc_len, total_len;
7571 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7573 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7575 retval = CTL_RETVAL_COMPLETE;
7577 total_len = sizeof(struct scsi_report_supported_tmf_data);
7578 alloc_len = scsi_4btoul(cdb->length);
7580 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7582 ctsio->kern_sg_entries = 0;
7584 if (total_len < alloc_len) {
7585 ctsio->residual = alloc_len - total_len;
7586 ctsio->kern_data_len = total_len;
7587 ctsio->kern_total_len = total_len;
7589 ctsio->residual = 0;
7590 ctsio->kern_data_len = alloc_len;
7591 ctsio->kern_total_len = alloc_len;
7593 ctsio->kern_data_resid = 0;
7594 ctsio->kern_rel_offset = 0;
7596 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr;
7597 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS;
7598 data->byte2 |= RST_ITNRS;
7600 ctl_set_success(ctsio);
7601 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7602 ctsio->be_move_done = ctl_config_move_done;
7603 ctl_datamove((union ctl_io *)ctsio);
7608 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7610 struct scsi_report_timestamp *cdb;
7611 struct scsi_report_timestamp_data *data;
7615 int alloc_len, total_len;
7617 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7619 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7621 retval = CTL_RETVAL_COMPLETE;
7623 total_len = sizeof(struct scsi_report_timestamp_data);
7624 alloc_len = scsi_4btoul(cdb->length);
7626 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7628 ctsio->kern_sg_entries = 0;
7630 if (total_len < alloc_len) {
7631 ctsio->residual = alloc_len - total_len;
7632 ctsio->kern_data_len = total_len;
7633 ctsio->kern_total_len = total_len;
7635 ctsio->residual = 0;
7636 ctsio->kern_data_len = alloc_len;
7637 ctsio->kern_total_len = alloc_len;
7639 ctsio->kern_data_resid = 0;
7640 ctsio->kern_rel_offset = 0;
7642 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7643 scsi_ulto2b(sizeof(*data) - 2, data->length);
7644 data->origin = RTS_ORIG_OUTSIDE;
7646 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7647 scsi_ulto4b(timestamp >> 16, data->timestamp);
7648 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7650 ctl_set_success(ctsio);
7651 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7652 ctsio->be_move_done = ctl_config_move_done;
7653 ctl_datamove((union ctl_io *)ctsio);
7658 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7660 struct scsi_per_res_in *cdb;
7661 int alloc_len, total_len = 0;
7662 /* struct scsi_per_res_in_rsrv in_data; */
7663 struct ctl_lun *lun;
7664 struct ctl_softc *softc;
7667 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7669 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7671 alloc_len = scsi_2btoul(cdb->length);
7673 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7674 softc = lun->ctl_softc;
7677 mtx_lock(&lun->lun_lock);
7678 switch (cdb->action) {
7679 case SPRI_RK: /* read keys */
7680 total_len = sizeof(struct scsi_per_res_in_keys) +
7682 sizeof(struct scsi_per_res_key);
7684 case SPRI_RR: /* read reservation */
7685 if (lun->flags & CTL_LUN_PR_RESERVED)
7686 total_len = sizeof(struct scsi_per_res_in_rsrv);
7688 total_len = sizeof(struct scsi_per_res_in_header);
7690 case SPRI_RC: /* report capabilities */
7691 total_len = sizeof(struct scsi_per_res_cap);
7693 case SPRI_RS: /* read full status */
7694 total_len = sizeof(struct scsi_per_res_in_header) +
7695 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7699 panic("Invalid PR type %x", cdb->action);
7701 mtx_unlock(&lun->lun_lock);
7703 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7705 if (total_len < alloc_len) {
7706 ctsio->residual = alloc_len - total_len;
7707 ctsio->kern_data_len = total_len;
7708 ctsio->kern_total_len = total_len;
7710 ctsio->residual = 0;
7711 ctsio->kern_data_len = alloc_len;
7712 ctsio->kern_total_len = alloc_len;
7715 ctsio->kern_data_resid = 0;
7716 ctsio->kern_rel_offset = 0;
7717 ctsio->kern_sg_entries = 0;
7719 mtx_lock(&lun->lun_lock);
7720 switch (cdb->action) {
7721 case SPRI_RK: { // read keys
7722 struct scsi_per_res_in_keys *res_keys;
7725 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7728 * We had to drop the lock to allocate our buffer, which
7729 * leaves time for someone to come in with another
7730 * persistent reservation. (That is unlikely, though,
7731 * since this should be the only persistent reservation
7732 * command active right now.)
7734 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7735 (lun->pr_key_count *
7736 sizeof(struct scsi_per_res_key)))){
7737 mtx_unlock(&lun->lun_lock);
7738 free(ctsio->kern_data_ptr, M_CTL);
7739 printf("%s: reservation length changed, retrying\n",
7744 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7746 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7747 lun->pr_key_count, res_keys->header.length);
7749 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7750 if ((key = ctl_get_prkey(lun, i)) == 0)
7754 * We used lun->pr_key_count to calculate the
7755 * size to allocate. If it turns out the number of
7756 * initiators with the registered flag set is
7757 * larger than that (i.e. they haven't been kept in
7758 * sync), we've got a problem.
7760 if (key_count >= lun->pr_key_count) {
7762 csevent_log(CSC_CTL | CSC_SHELF_SW |
7764 csevent_LogType_Fault,
7765 csevent_AlertLevel_Yellow,
7766 csevent_FRU_ShelfController,
7767 csevent_FRU_Firmware,
7768 csevent_FRU_Unknown,
7769 "registered keys %d >= key "
7770 "count %d", key_count,
7776 scsi_u64to8b(key, res_keys->keys[key_count].key);
7781 case SPRI_RR: { // read reservation
7782 struct scsi_per_res_in_rsrv *res;
7783 int tmp_len, header_only;
7785 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7787 scsi_ulto4b(lun->PRGeneration, res->header.generation);
7789 if (lun->flags & CTL_LUN_PR_RESERVED)
7791 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7792 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7793 res->header.length);
7796 tmp_len = sizeof(struct scsi_per_res_in_header);
7797 scsi_ulto4b(0, res->header.length);
7802 * We had to drop the lock to allocate our buffer, which
7803 * leaves time for someone to come in with another
7804 * persistent reservation. (That is unlikely, though,
7805 * since this should be the only persistent reservation
7806 * command active right now.)
7808 if (tmp_len != total_len) {
7809 mtx_unlock(&lun->lun_lock);
7810 free(ctsio->kern_data_ptr, M_CTL);
7811 printf("%s: reservation status changed, retrying\n",
7817 * No reservation held, so we're done.
7819 if (header_only != 0)
7823 * If the registration is an All Registrants type, the key
7824 * is 0, since it doesn't really matter.
7826 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7827 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7828 res->data.reservation);
7830 res->data.scopetype = lun->res_type;
7833 case SPRI_RC: //report capabilities
7835 struct scsi_per_res_cap *res_cap;
7838 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7839 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7840 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5;
7841 type_mask = SPRI_TM_WR_EX_AR |
7847 scsi_ulto2b(type_mask, res_cap->type_mask);
7850 case SPRI_RS: { // read full status
7851 struct scsi_per_res_in_full *res_status;
7852 struct scsi_per_res_in_full_desc *res_desc;
7853 struct ctl_port *port;
7856 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7859 * We had to drop the lock to allocate our buffer, which
7860 * leaves time for someone to come in with another
7861 * persistent reservation. (That is unlikely, though,
7862 * since this should be the only persistent reservation
7863 * command active right now.)
7865 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7866 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7867 lun->pr_key_count)){
7868 mtx_unlock(&lun->lun_lock);
7869 free(ctsio->kern_data_ptr, M_CTL);
7870 printf("%s: reservation length changed, retrying\n",
7875 scsi_ulto4b(lun->PRGeneration, res_status->header.generation);
7877 res_desc = &res_status->desc[0];
7878 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7879 if ((key = ctl_get_prkey(lun, i)) == 0)
7882 scsi_u64to8b(key, res_desc->res_key.key);
7883 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7884 (lun->pr_res_idx == i ||
7885 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7886 res_desc->flags = SPRI_FULL_R_HOLDER;
7887 res_desc->scopetype = lun->res_type;
7889 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7890 res_desc->rel_trgt_port_id);
7892 port = softc->ctl_ports[
7893 ctl_port_idx(i / CTL_MAX_INIT_PER_PORT)];
7895 len = ctl_create_iid(port,
7896 i % CTL_MAX_INIT_PER_PORT,
7897 res_desc->transport_id);
7898 scsi_ulto4b(len, res_desc->additional_length);
7899 res_desc = (struct scsi_per_res_in_full_desc *)
7900 &res_desc->transport_id[len];
7902 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7903 res_status->header.length);
7908 * This is a bug, because we just checked for this above,
7909 * and should have returned an error.
7911 panic("Invalid PR type %x", cdb->action);
7912 break; /* NOTREACHED */
7914 mtx_unlock(&lun->lun_lock);
7916 ctl_set_success(ctsio);
7917 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7918 ctsio->be_move_done = ctl_config_move_done;
7919 ctl_datamove((union ctl_io *)ctsio);
7920 return (CTL_RETVAL_COMPLETE);
7924 ctl_est_res_ua(struct ctl_lun *lun, uint32_t residx, ctl_ua_type ua)
7926 int off = lun->ctl_softc->persis_offset;
7928 if (residx >= off && residx < off + CTL_MAX_INITIATORS)
7929 ctl_est_ua(lun, residx - off, ua);
7933 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7937 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7938 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7939 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7940 struct scsi_per_res_out_parms* param)
7942 union ctl_ha_msg persis_io;
7948 mtx_lock(&lun->lun_lock);
7949 if (sa_res_key == 0) {
7950 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7951 /* validate scope and type */
7952 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7954 mtx_unlock(&lun->lun_lock);
7955 ctl_set_invalid_field(/*ctsio*/ ctsio,
7961 ctl_done((union ctl_io *)ctsio);
7965 if (type>8 || type==2 || type==4 || type==0) {
7966 mtx_unlock(&lun->lun_lock);
7967 ctl_set_invalid_field(/*ctsio*/ ctsio,
7973 ctl_done((union ctl_io *)ctsio);
7978 * Unregister everybody else and build UA for
7981 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7982 if (i == residx || ctl_get_prkey(lun, i) == 0)
7985 ctl_clr_prkey(lun, i);
7986 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT);
7988 lun->pr_key_count = 1;
7989 lun->res_type = type;
7990 if (lun->res_type != SPR_TYPE_WR_EX_AR
7991 && lun->res_type != SPR_TYPE_EX_AC_AR)
7992 lun->pr_res_idx = residx;
7994 /* send msg to other side */
7995 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7996 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7997 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7998 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7999 persis_io.pr.pr_info.res_type = type;
8000 memcpy(persis_io.pr.pr_info.sa_res_key,
8001 param->serv_act_res_key,
8002 sizeof(param->serv_act_res_key));
8003 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8004 &persis_io, sizeof(persis_io), 0)) >
8005 CTL_HA_STATUS_SUCCESS) {
8006 printf("CTL:Persis Out error returned "
8007 "from ctl_ha_msg_send %d\n",
8011 /* not all registrants */
8012 mtx_unlock(&lun->lun_lock);
8013 free(ctsio->kern_data_ptr, M_CTL);
8014 ctl_set_invalid_field(ctsio,
8020 ctl_done((union ctl_io *)ctsio);
8023 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8024 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
8027 if (res_key == sa_res_key) {
8030 * The spec implies this is not good but doesn't
8031 * say what to do. There are two choices either
8032 * generate a res conflict or check condition
8033 * with illegal field in parameter data. Since
8034 * that is what is done when the sa_res_key is
8035 * zero I'll take that approach since this has
8036 * to do with the sa_res_key.
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);
8050 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8051 if (ctl_get_prkey(lun, i) != sa_res_key)
8055 ctl_clr_prkey(lun, i);
8056 lun->pr_key_count--;
8057 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8060 mtx_unlock(&lun->lun_lock);
8061 free(ctsio->kern_data_ptr, M_CTL);
8062 ctl_set_reservation_conflict(ctsio);
8063 ctl_done((union ctl_io *)ctsio);
8064 return (CTL_RETVAL_COMPLETE);
8066 /* send msg to other side */
8067 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8068 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8069 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8070 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8071 persis_io.pr.pr_info.res_type = type;
8072 memcpy(persis_io.pr.pr_info.sa_res_key,
8073 param->serv_act_res_key,
8074 sizeof(param->serv_act_res_key));
8075 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8076 &persis_io, sizeof(persis_io), 0)) >
8077 CTL_HA_STATUS_SUCCESS) {
8078 printf("CTL:Persis Out error returned from "
8079 "ctl_ha_msg_send %d\n", isc_retval);
8082 /* Reserved but not all registrants */
8083 /* sa_res_key is res holder */
8084 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
8085 /* validate scope and type */
8086 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
8088 mtx_unlock(&lun->lun_lock);
8089 ctl_set_invalid_field(/*ctsio*/ ctsio,
8095 ctl_done((union ctl_io *)ctsio);
8099 if (type>8 || type==2 || type==4 || type==0) {
8100 mtx_unlock(&lun->lun_lock);
8101 ctl_set_invalid_field(/*ctsio*/ ctsio,
8107 ctl_done((union ctl_io *)ctsio);
8113 * if sa_res_key != res_key remove all
8114 * registrants w/sa_res_key and generate UA
8115 * for these registrants(Registrations
8116 * Preempted) if it wasn't an exclusive
8117 * reservation generate UA(Reservations
8118 * Preempted) for all other registered nexuses
8119 * if the type has changed. Establish the new
8120 * reservation and holder. If res_key and
8121 * sa_res_key are the same do the above
8122 * except don't unregister the res holder.
8125 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8126 if (i == residx || ctl_get_prkey(lun, i) == 0)
8129 if (sa_res_key == ctl_get_prkey(lun, i)) {
8130 ctl_clr_prkey(lun, i);
8131 lun->pr_key_count--;
8132 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8133 } else if (type != lun->res_type
8134 && (lun->res_type == SPR_TYPE_WR_EX_RO
8135 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
8136 ctl_est_res_ua(lun, i, CTL_UA_RES_RELEASE);
8139 lun->res_type = type;
8140 if (lun->res_type != SPR_TYPE_WR_EX_AR
8141 && lun->res_type != SPR_TYPE_EX_AC_AR)
8142 lun->pr_res_idx = residx;
8144 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8146 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8147 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8148 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8149 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8150 persis_io.pr.pr_info.res_type = type;
8151 memcpy(persis_io.pr.pr_info.sa_res_key,
8152 param->serv_act_res_key,
8153 sizeof(param->serv_act_res_key));
8154 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8155 &persis_io, sizeof(persis_io), 0)) >
8156 CTL_HA_STATUS_SUCCESS) {
8157 printf("CTL:Persis Out error returned "
8158 "from ctl_ha_msg_send %d\n",
8163 * sa_res_key is not the res holder just
8164 * remove registrants
8168 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8169 if (sa_res_key != ctl_get_prkey(lun, i))
8173 ctl_clr_prkey(lun, i);
8174 lun->pr_key_count--;
8175 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8179 mtx_unlock(&lun->lun_lock);
8180 free(ctsio->kern_data_ptr, M_CTL);
8181 ctl_set_reservation_conflict(ctsio);
8182 ctl_done((union ctl_io *)ctsio);
8185 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8186 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8187 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8188 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8189 persis_io.pr.pr_info.res_type = type;
8190 memcpy(persis_io.pr.pr_info.sa_res_key,
8191 param->serv_act_res_key,
8192 sizeof(param->serv_act_res_key));
8193 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8194 &persis_io, sizeof(persis_io), 0)) >
8195 CTL_HA_STATUS_SUCCESS) {
8196 printf("CTL:Persis Out error returned "
8197 "from ctl_ha_msg_send %d\n",
8203 lun->PRGeneration++;
8204 mtx_unlock(&lun->lun_lock);
8210 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8212 uint64_t sa_res_key;
8215 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8217 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8218 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8219 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
8220 if (sa_res_key == 0) {
8222 * Unregister everybody else and build UA for
8225 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8226 if (i == msg->pr.pr_info.residx ||
8227 ctl_get_prkey(lun, i) == 0)
8230 ctl_clr_prkey(lun, i);
8231 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8234 lun->pr_key_count = 1;
8235 lun->res_type = msg->pr.pr_info.res_type;
8236 if (lun->res_type != SPR_TYPE_WR_EX_AR
8237 && lun->res_type != SPR_TYPE_EX_AC_AR)
8238 lun->pr_res_idx = msg->pr.pr_info.residx;
8240 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8241 if (sa_res_key == ctl_get_prkey(lun, i))
8244 ctl_clr_prkey(lun, i);
8245 lun->pr_key_count--;
8246 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8250 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8251 if (i == msg->pr.pr_info.residx ||
8252 ctl_get_prkey(lun, i) == 0)
8255 if (sa_res_key == ctl_get_prkey(lun, i)) {
8256 ctl_clr_prkey(lun, i);
8257 lun->pr_key_count--;
8258 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8259 } else if (msg->pr.pr_info.res_type != lun->res_type
8260 && (lun->res_type == SPR_TYPE_WR_EX_RO
8261 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
8262 ctl_est_res_ua(lun, i, CTL_UA_RES_RELEASE);
8265 lun->res_type = msg->pr.pr_info.res_type;
8266 if (lun->res_type != SPR_TYPE_WR_EX_AR
8267 && lun->res_type != SPR_TYPE_EX_AC_AR)
8268 lun->pr_res_idx = msg->pr.pr_info.residx;
8270 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8272 lun->PRGeneration++;
8278 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8282 u_int32_t param_len;
8283 struct scsi_per_res_out *cdb;
8284 struct ctl_lun *lun;
8285 struct scsi_per_res_out_parms* param;
8286 struct ctl_softc *softc;
8288 uint64_t res_key, sa_res_key, key;
8290 union ctl_ha_msg persis_io;
8293 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8295 retval = CTL_RETVAL_COMPLETE;
8297 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8298 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8299 softc = lun->ctl_softc;
8302 * We only support whole-LUN scope. The scope & type are ignored for
8303 * register, register and ignore existing key and clear.
8304 * We sometimes ignore scope and type on preempts too!!
8305 * Verify reservation type here as well.
8307 type = cdb->scope_type & SPR_TYPE_MASK;
8308 if ((cdb->action == SPRO_RESERVE)
8309 || (cdb->action == SPRO_RELEASE)) {
8310 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8311 ctl_set_invalid_field(/*ctsio*/ ctsio,
8317 ctl_done((union ctl_io *)ctsio);
8318 return (CTL_RETVAL_COMPLETE);
8321 if (type>8 || type==2 || type==4 || type==0) {
8322 ctl_set_invalid_field(/*ctsio*/ ctsio,
8328 ctl_done((union ctl_io *)ctsio);
8329 return (CTL_RETVAL_COMPLETE);
8333 param_len = scsi_4btoul(cdb->length);
8335 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8336 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8337 ctsio->kern_data_len = param_len;
8338 ctsio->kern_total_len = param_len;
8339 ctsio->kern_data_resid = 0;
8340 ctsio->kern_rel_offset = 0;
8341 ctsio->kern_sg_entries = 0;
8342 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8343 ctsio->be_move_done = ctl_config_move_done;
8344 ctl_datamove((union ctl_io *)ctsio);
8346 return (CTL_RETVAL_COMPLETE);
8349 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8351 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8352 res_key = scsi_8btou64(param->res_key.key);
8353 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8356 * Validate the reservation key here except for SPRO_REG_IGNO
8357 * This must be done for all other service actions
8359 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8360 mtx_lock(&lun->lun_lock);
8361 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8362 if (res_key != key) {
8364 * The current key passed in doesn't match
8365 * the one the initiator previously
8368 mtx_unlock(&lun->lun_lock);
8369 free(ctsio->kern_data_ptr, M_CTL);
8370 ctl_set_reservation_conflict(ctsio);
8371 ctl_done((union ctl_io *)ctsio);
8372 return (CTL_RETVAL_COMPLETE);
8374 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8376 * We are not registered
8378 mtx_unlock(&lun->lun_lock);
8379 free(ctsio->kern_data_ptr, M_CTL);
8380 ctl_set_reservation_conflict(ctsio);
8381 ctl_done((union ctl_io *)ctsio);
8382 return (CTL_RETVAL_COMPLETE);
8383 } else if (res_key != 0) {
8385 * We are not registered and trying to register but
8386 * the register key isn't zero.
8388 mtx_unlock(&lun->lun_lock);
8389 free(ctsio->kern_data_ptr, M_CTL);
8390 ctl_set_reservation_conflict(ctsio);
8391 ctl_done((union ctl_io *)ctsio);
8392 return (CTL_RETVAL_COMPLETE);
8394 mtx_unlock(&lun->lun_lock);
8397 switch (cdb->action & SPRO_ACTION_MASK) {
8399 case SPRO_REG_IGNO: {
8402 printf("Registration received\n");
8406 * We don't support any of these options, as we report in
8407 * the read capabilities request (see
8408 * ctl_persistent_reserve_in(), above).
8410 if ((param->flags & SPR_SPEC_I_PT)
8411 || (param->flags & SPR_ALL_TG_PT)
8412 || (param->flags & SPR_APTPL)) {
8415 if (param->flags & SPR_APTPL)
8417 else if (param->flags & SPR_ALL_TG_PT)
8419 else /* SPR_SPEC_I_PT */
8422 free(ctsio->kern_data_ptr, M_CTL);
8423 ctl_set_invalid_field(ctsio,
8429 ctl_done((union ctl_io *)ctsio);
8430 return (CTL_RETVAL_COMPLETE);
8433 mtx_lock(&lun->lun_lock);
8436 * The initiator wants to clear the
8439 if (sa_res_key == 0) {
8441 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8442 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8443 && ctl_get_prkey(lun, residx) == 0)) {
8444 mtx_unlock(&lun->lun_lock);
8448 ctl_clr_prkey(lun, residx);
8449 lun->pr_key_count--;
8451 if (residx == lun->pr_res_idx) {
8452 lun->flags &= ~CTL_LUN_PR_RESERVED;
8453 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8455 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8456 || lun->res_type == SPR_TYPE_EX_AC_RO)
8457 && lun->pr_key_count) {
8459 * If the reservation is a registrants
8460 * only type we need to generate a UA
8461 * for other registered inits. The
8462 * sense code should be RESERVATIONS
8466 for (i = 0; i < CTL_MAX_INITIATORS;i++){
8467 if (ctl_get_prkey(lun, i +
8468 softc->persis_offset) == 0)
8471 CTL_UA_RES_RELEASE);
8475 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8476 if (lun->pr_key_count==0) {
8477 lun->flags &= ~CTL_LUN_PR_RESERVED;
8479 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8482 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8483 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8484 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8485 persis_io.pr.pr_info.residx = residx;
8486 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8487 &persis_io, sizeof(persis_io), 0 )) >
8488 CTL_HA_STATUS_SUCCESS) {
8489 printf("CTL:Persis Out error returned from "
8490 "ctl_ha_msg_send %d\n", isc_retval);
8492 } else /* sa_res_key != 0 */ {
8495 * If we aren't registered currently then increment
8496 * the key count and set the registered flag.
8498 ctl_alloc_prkey(lun, residx);
8499 if (ctl_get_prkey(lun, residx) == 0)
8500 lun->pr_key_count++;
8501 ctl_set_prkey(lun, residx, sa_res_key);
8503 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8504 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8505 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8506 persis_io.pr.pr_info.residx = residx;
8507 memcpy(persis_io.pr.pr_info.sa_res_key,
8508 param->serv_act_res_key,
8509 sizeof(param->serv_act_res_key));
8510 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8511 &persis_io, sizeof(persis_io), 0)) >
8512 CTL_HA_STATUS_SUCCESS) {
8513 printf("CTL:Persis Out error returned from "
8514 "ctl_ha_msg_send %d\n", isc_retval);
8517 lun->PRGeneration++;
8518 mtx_unlock(&lun->lun_lock);
8524 printf("Reserve executed type %d\n", type);
8526 mtx_lock(&lun->lun_lock);
8527 if (lun->flags & CTL_LUN_PR_RESERVED) {
8529 * if this isn't the reservation holder and it's
8530 * not a "all registrants" type or if the type is
8531 * different then we have a conflict
8533 if ((lun->pr_res_idx != residx
8534 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8535 || lun->res_type != type) {
8536 mtx_unlock(&lun->lun_lock);
8537 free(ctsio->kern_data_ptr, M_CTL);
8538 ctl_set_reservation_conflict(ctsio);
8539 ctl_done((union ctl_io *)ctsio);
8540 return (CTL_RETVAL_COMPLETE);
8542 mtx_unlock(&lun->lun_lock);
8543 } else /* create a reservation */ {
8545 * If it's not an "all registrants" type record
8546 * reservation holder
8548 if (type != SPR_TYPE_WR_EX_AR
8549 && type != SPR_TYPE_EX_AC_AR)
8550 lun->pr_res_idx = residx; /* Res holder */
8552 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8554 lun->flags |= CTL_LUN_PR_RESERVED;
8555 lun->res_type = type;
8557 mtx_unlock(&lun->lun_lock);
8559 /* send msg to other side */
8560 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8561 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8562 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8563 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8564 persis_io.pr.pr_info.res_type = type;
8565 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8566 &persis_io, sizeof(persis_io), 0)) >
8567 CTL_HA_STATUS_SUCCESS) {
8568 printf("CTL:Persis Out error returned from "
8569 "ctl_ha_msg_send %d\n", isc_retval);
8575 mtx_lock(&lun->lun_lock);
8576 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8577 /* No reservation exists return good status */
8578 mtx_unlock(&lun->lun_lock);
8582 * Is this nexus a reservation holder?
8584 if (lun->pr_res_idx != residx
8585 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8587 * not a res holder return good status but
8590 mtx_unlock(&lun->lun_lock);
8594 if (lun->res_type != type) {
8595 mtx_unlock(&lun->lun_lock);
8596 free(ctsio->kern_data_ptr, M_CTL);
8597 ctl_set_illegal_pr_release(ctsio);
8598 ctl_done((union ctl_io *)ctsio);
8599 return (CTL_RETVAL_COMPLETE);
8602 /* okay to release */
8603 lun->flags &= ~CTL_LUN_PR_RESERVED;
8604 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8608 * if this isn't an exclusive access
8609 * res generate UA for all other
8612 if (type != SPR_TYPE_EX_AC
8613 && type != SPR_TYPE_WR_EX) {
8614 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8617 i + softc->persis_offset) == 0)
8619 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8622 mtx_unlock(&lun->lun_lock);
8623 /* Send msg to other side */
8624 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8625 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8626 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8627 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io,
8628 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8629 printf("CTL:Persis Out error returned from "
8630 "ctl_ha_msg_send %d\n", isc_retval);
8635 /* send msg to other side */
8637 mtx_lock(&lun->lun_lock);
8638 lun->flags &= ~CTL_LUN_PR_RESERVED;
8640 lun->pr_key_count = 0;
8641 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8643 ctl_clr_prkey(lun, residx);
8644 for (i=0; i < 2*CTL_MAX_INITIATORS; i++)
8645 if (ctl_get_prkey(lun, i) != 0) {
8646 ctl_clr_prkey(lun, i);
8647 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8649 lun->PRGeneration++;
8650 mtx_unlock(&lun->lun_lock);
8651 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8652 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8653 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8654 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8655 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8656 printf("CTL:Persis Out error returned from "
8657 "ctl_ha_msg_send %d\n", isc_retval);
8662 case SPRO_PRE_ABO: {
8665 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8666 residx, ctsio, cdb, param);
8668 return (CTL_RETVAL_COMPLETE);
8672 panic("Invalid PR type %x", cdb->action);
8676 free(ctsio->kern_data_ptr, M_CTL);
8677 ctl_set_success(ctsio);
8678 ctl_done((union ctl_io *)ctsio);
8684 * This routine is for handling a message from the other SC pertaining to
8685 * persistent reserve out. All the error checking will have been done
8686 * so only perorming the action need be done here to keep the two
8690 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8692 struct ctl_lun *lun;
8693 struct ctl_softc *softc;
8697 softc = control_softc;
8699 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8700 lun = softc->ctl_luns[targ_lun];
8701 mtx_lock(&lun->lun_lock);
8702 switch(msg->pr.pr_info.action) {
8703 case CTL_PR_REG_KEY:
8704 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8705 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8706 lun->pr_key_count++;
8707 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8708 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8709 lun->PRGeneration++;
8712 case CTL_PR_UNREG_KEY:
8713 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8714 lun->pr_key_count--;
8716 /* XXX Need to see if the reservation has been released */
8717 /* if so do we need to generate UA? */
8718 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8719 lun->flags &= ~CTL_LUN_PR_RESERVED;
8720 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8722 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8723 || lun->res_type == SPR_TYPE_EX_AC_RO)
8724 && lun->pr_key_count) {
8726 * If the reservation is a registrants
8727 * only type we need to generate a UA
8728 * for other registered inits. The
8729 * sense code should be RESERVATIONS
8733 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8734 if (ctl_get_prkey(lun, i +
8735 softc->persis_offset) == 0)
8738 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8742 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8743 if (lun->pr_key_count==0) {
8744 lun->flags &= ~CTL_LUN_PR_RESERVED;
8746 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8749 lun->PRGeneration++;
8752 case CTL_PR_RESERVE:
8753 lun->flags |= CTL_LUN_PR_RESERVED;
8754 lun->res_type = msg->pr.pr_info.res_type;
8755 lun->pr_res_idx = msg->pr.pr_info.residx;
8759 case CTL_PR_RELEASE:
8761 * if this isn't an exclusive access res generate UA for all
8762 * other registrants.
8764 if (lun->res_type != SPR_TYPE_EX_AC
8765 && lun->res_type != SPR_TYPE_WR_EX) {
8766 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8767 if (ctl_get_prkey(lun, i + softc->persis_offset) != 0)
8768 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8771 lun->flags &= ~CTL_LUN_PR_RESERVED;
8772 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8776 case CTL_PR_PREEMPT:
8777 ctl_pro_preempt_other(lun, msg);
8780 lun->flags &= ~CTL_LUN_PR_RESERVED;
8782 lun->pr_key_count = 0;
8783 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8785 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8786 if (ctl_get_prkey(lun, i) == 0)
8788 ctl_clr_prkey(lun, i);
8789 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8791 lun->PRGeneration++;
8795 mtx_unlock(&lun->lun_lock);
8799 ctl_read_write(struct ctl_scsiio *ctsio)
8801 struct ctl_lun *lun;
8802 struct ctl_lba_len_flags *lbalen;
8804 uint32_t num_blocks;
8808 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8810 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8813 retval = CTL_RETVAL_COMPLETE;
8815 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8816 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8817 switch (ctsio->cdb[0]) {
8820 struct scsi_rw_6 *cdb;
8822 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8824 lba = scsi_3btoul(cdb->addr);
8825 /* only 5 bits are valid in the most significant address byte */
8827 num_blocks = cdb->length;
8829 * This is correct according to SBC-2.
8831 if (num_blocks == 0)
8837 struct scsi_rw_10 *cdb;
8839 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8840 if (cdb->byte2 & SRW10_FUA)
8841 flags |= CTL_LLF_FUA;
8842 if (cdb->byte2 & SRW10_DPO)
8843 flags |= CTL_LLF_DPO;
8844 lba = scsi_4btoul(cdb->addr);
8845 num_blocks = scsi_2btoul(cdb->length);
8848 case WRITE_VERIFY_10: {
8849 struct scsi_write_verify_10 *cdb;
8851 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8852 flags |= CTL_LLF_FUA;
8853 if (cdb->byte2 & SWV_DPO)
8854 flags |= CTL_LLF_DPO;
8855 lba = scsi_4btoul(cdb->addr);
8856 num_blocks = scsi_2btoul(cdb->length);
8861 struct scsi_rw_12 *cdb;
8863 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8864 if (cdb->byte2 & SRW12_FUA)
8865 flags |= CTL_LLF_FUA;
8866 if (cdb->byte2 & SRW12_DPO)
8867 flags |= CTL_LLF_DPO;
8868 lba = scsi_4btoul(cdb->addr);
8869 num_blocks = scsi_4btoul(cdb->length);
8872 case WRITE_VERIFY_12: {
8873 struct scsi_write_verify_12 *cdb;
8875 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8876 flags |= CTL_LLF_FUA;
8877 if (cdb->byte2 & SWV_DPO)
8878 flags |= CTL_LLF_DPO;
8879 lba = scsi_4btoul(cdb->addr);
8880 num_blocks = scsi_4btoul(cdb->length);
8885 struct scsi_rw_16 *cdb;
8887 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8888 if (cdb->byte2 & SRW12_FUA)
8889 flags |= CTL_LLF_FUA;
8890 if (cdb->byte2 & SRW12_DPO)
8891 flags |= CTL_LLF_DPO;
8892 lba = scsi_8btou64(cdb->addr);
8893 num_blocks = scsi_4btoul(cdb->length);
8896 case WRITE_ATOMIC_16: {
8897 struct scsi_rw_16 *cdb;
8899 if (lun->be_lun->atomicblock == 0) {
8900 ctl_set_invalid_opcode(ctsio);
8901 ctl_done((union ctl_io *)ctsio);
8902 return (CTL_RETVAL_COMPLETE);
8905 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8906 if (cdb->byte2 & SRW12_FUA)
8907 flags |= CTL_LLF_FUA;
8908 if (cdb->byte2 & SRW12_DPO)
8909 flags |= CTL_LLF_DPO;
8910 lba = scsi_8btou64(cdb->addr);
8911 num_blocks = scsi_4btoul(cdb->length);
8912 if (num_blocks > lun->be_lun->atomicblock) {
8913 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8914 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8916 ctl_done((union ctl_io *)ctsio);
8917 return (CTL_RETVAL_COMPLETE);
8921 case WRITE_VERIFY_16: {
8922 struct scsi_write_verify_16 *cdb;
8924 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8925 flags |= CTL_LLF_FUA;
8926 if (cdb->byte2 & SWV_DPO)
8927 flags |= CTL_LLF_DPO;
8928 lba = scsi_8btou64(cdb->addr);
8929 num_blocks = scsi_4btoul(cdb->length);
8934 * We got a command we don't support. This shouldn't
8935 * happen, commands should be filtered out above us.
8937 ctl_set_invalid_opcode(ctsio);
8938 ctl_done((union ctl_io *)ctsio);
8940 return (CTL_RETVAL_COMPLETE);
8941 break; /* NOTREACHED */
8945 * The first check is to make sure we're in bounds, the second
8946 * check is to catch wrap-around problems. If the lba + num blocks
8947 * is less than the lba, then we've wrapped around and the block
8948 * range is invalid anyway.
8950 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8951 || ((lba + num_blocks) < lba)) {
8952 ctl_set_lba_out_of_range(ctsio);
8953 ctl_done((union ctl_io *)ctsio);
8954 return (CTL_RETVAL_COMPLETE);
8958 * According to SBC-3, a transfer length of 0 is not an error.
8959 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8960 * translates to 256 blocks for those commands.
8962 if (num_blocks == 0) {
8963 ctl_set_success(ctsio);
8964 ctl_done((union ctl_io *)ctsio);
8965 return (CTL_RETVAL_COMPLETE);
8968 /* Set FUA and/or DPO if caches are disabled. */
8970 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8972 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8974 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8976 flags |= CTL_LLF_FUA;
8979 lbalen = (struct ctl_lba_len_flags *)
8980 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8982 lbalen->len = num_blocks;
8983 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8985 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8986 ctsio->kern_rel_offset = 0;
8988 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8990 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8996 ctl_cnw_cont(union ctl_io *io)
8998 struct ctl_scsiio *ctsio;
8999 struct ctl_lun *lun;
9000 struct ctl_lba_len_flags *lbalen;
9003 ctsio = &io->scsiio;
9004 ctsio->io_hdr.status = CTL_STATUS_NONE;
9005 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
9006 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9007 lbalen = (struct ctl_lba_len_flags *)
9008 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9009 lbalen->flags &= ~CTL_LLF_COMPARE;
9010 lbalen->flags |= CTL_LLF_WRITE;
9012 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
9013 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9018 ctl_cnw(struct ctl_scsiio *ctsio)
9020 struct ctl_lun *lun;
9021 struct ctl_lba_len_flags *lbalen;
9023 uint32_t num_blocks;
9026 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9028 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
9031 retval = CTL_RETVAL_COMPLETE;
9033 switch (ctsio->cdb[0]) {
9034 case COMPARE_AND_WRITE: {
9035 struct scsi_compare_and_write *cdb;
9037 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
9038 if (cdb->byte2 & SRW10_FUA)
9039 flags |= CTL_LLF_FUA;
9040 if (cdb->byte2 & SRW10_DPO)
9041 flags |= CTL_LLF_DPO;
9042 lba = scsi_8btou64(cdb->addr);
9043 num_blocks = cdb->length;
9048 * We got a command we don't support. This shouldn't
9049 * happen, commands should be filtered out above us.
9051 ctl_set_invalid_opcode(ctsio);
9052 ctl_done((union ctl_io *)ctsio);
9054 return (CTL_RETVAL_COMPLETE);
9055 break; /* NOTREACHED */
9059 * The first check is to make sure we're in bounds, the second
9060 * check is to catch wrap-around problems. If the lba + num blocks
9061 * is less than the lba, then we've wrapped around and the block
9062 * range is invalid anyway.
9064 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9065 || ((lba + num_blocks) < lba)) {
9066 ctl_set_lba_out_of_range(ctsio);
9067 ctl_done((union ctl_io *)ctsio);
9068 return (CTL_RETVAL_COMPLETE);
9072 * According to SBC-3, a transfer length of 0 is not an error.
9074 if (num_blocks == 0) {
9075 ctl_set_success(ctsio);
9076 ctl_done((union ctl_io *)ctsio);
9077 return (CTL_RETVAL_COMPLETE);
9080 /* Set FUA if write cache is disabled. */
9081 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9083 flags |= CTL_LLF_FUA;
9085 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
9086 ctsio->kern_rel_offset = 0;
9089 * Set the IO_CONT flag, so that if this I/O gets passed to
9090 * ctl_data_submit_done(), it'll get passed back to
9091 * ctl_ctl_cnw_cont() for further processing.
9093 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
9094 ctsio->io_cont = ctl_cnw_cont;
9096 lbalen = (struct ctl_lba_len_flags *)
9097 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9099 lbalen->len = num_blocks;
9100 lbalen->flags = CTL_LLF_COMPARE | flags;
9102 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
9103 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9108 ctl_verify(struct ctl_scsiio *ctsio)
9110 struct ctl_lun *lun;
9111 struct ctl_lba_len_flags *lbalen;
9113 uint32_t num_blocks;
9117 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9119 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
9122 flags = CTL_LLF_FUA;
9123 retval = CTL_RETVAL_COMPLETE;
9125 switch (ctsio->cdb[0]) {
9127 struct scsi_verify_10 *cdb;
9129 cdb = (struct scsi_verify_10 *)ctsio->cdb;
9130 if (cdb->byte2 & SVFY_BYTCHK)
9132 if (cdb->byte2 & SVFY_DPO)
9133 flags |= CTL_LLF_DPO;
9134 lba = scsi_4btoul(cdb->addr);
9135 num_blocks = scsi_2btoul(cdb->length);
9139 struct scsi_verify_12 *cdb;
9141 cdb = (struct scsi_verify_12 *)ctsio->cdb;
9142 if (cdb->byte2 & SVFY_BYTCHK)
9144 if (cdb->byte2 & SVFY_DPO)
9145 flags |= CTL_LLF_DPO;
9146 lba = scsi_4btoul(cdb->addr);
9147 num_blocks = scsi_4btoul(cdb->length);
9151 struct scsi_rw_16 *cdb;
9153 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9154 if (cdb->byte2 & SVFY_BYTCHK)
9156 if (cdb->byte2 & SVFY_DPO)
9157 flags |= CTL_LLF_DPO;
9158 lba = scsi_8btou64(cdb->addr);
9159 num_blocks = scsi_4btoul(cdb->length);
9164 * We got a command we don't support. This shouldn't
9165 * happen, commands should be filtered out above us.
9167 ctl_set_invalid_opcode(ctsio);
9168 ctl_done((union ctl_io *)ctsio);
9169 return (CTL_RETVAL_COMPLETE);
9173 * The first check is to make sure we're in bounds, the second
9174 * check is to catch wrap-around problems. If the lba + num blocks
9175 * is less than the lba, then we've wrapped around and the block
9176 * range is invalid anyway.
9178 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9179 || ((lba + num_blocks) < lba)) {
9180 ctl_set_lba_out_of_range(ctsio);
9181 ctl_done((union ctl_io *)ctsio);
9182 return (CTL_RETVAL_COMPLETE);
9186 * According to SBC-3, a transfer length of 0 is not an error.
9188 if (num_blocks == 0) {
9189 ctl_set_success(ctsio);
9190 ctl_done((union ctl_io *)ctsio);
9191 return (CTL_RETVAL_COMPLETE);
9194 lbalen = (struct ctl_lba_len_flags *)
9195 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9197 lbalen->len = num_blocks;
9199 lbalen->flags = CTL_LLF_COMPARE | flags;
9200 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9202 lbalen->flags = CTL_LLF_VERIFY | flags;
9203 ctsio->kern_total_len = 0;
9205 ctsio->kern_rel_offset = 0;
9207 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9208 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9213 ctl_report_luns(struct ctl_scsiio *ctsio)
9215 struct ctl_softc *softc = control_softc;
9216 struct scsi_report_luns *cdb;
9217 struct scsi_report_luns_data *lun_data;
9218 struct ctl_lun *lun, *request_lun;
9219 struct ctl_port *port;
9220 int num_luns, retval;
9221 uint32_t alloc_len, lun_datalen;
9222 int num_filled, well_known;
9223 uint32_t initidx, targ_lun_id, lun_id;
9225 retval = CTL_RETVAL_COMPLETE;
9228 cdb = (struct scsi_report_luns *)ctsio->cdb;
9229 port = ctl_io_port(&ctsio->io_hdr);
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:
9245 case RPL_REPORT_WELLKNOWN:
9250 ctl_set_invalid_field(ctsio,
9256 ctl_done((union ctl_io *)ctsio);
9258 break; /* NOTREACHED */
9261 alloc_len = scsi_4btoul(cdb->length);
9263 * The initiator has to allocate at least 16 bytes for this request,
9264 * so he can at least get the header and the first LUN. Otherwise
9265 * we reject the request (per SPC-3 rev 14, section 6.21).
9267 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9268 sizeof(struct scsi_report_luns_lundata))) {
9269 ctl_set_invalid_field(ctsio,
9275 ctl_done((union ctl_io *)ctsio);
9279 request_lun = (struct ctl_lun *)
9280 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9282 lun_datalen = sizeof(*lun_data) +
9283 (num_luns * sizeof(struct scsi_report_luns_lundata));
9285 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9286 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9287 ctsio->kern_sg_entries = 0;
9289 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9291 mtx_lock(&softc->ctl_lock);
9292 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9293 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9294 if (lun_id >= CTL_MAX_LUNS)
9296 lun = softc->ctl_luns[lun_id];
9300 if (targ_lun_id <= 0xff) {
9302 * Peripheral addressing method, bus number 0.
9304 lun_data->luns[num_filled].lundata[0] =
9305 RPL_LUNDATA_ATYP_PERIPH;
9306 lun_data->luns[num_filled].lundata[1] = targ_lun_id;
9308 } else if (targ_lun_id <= 0x3fff) {
9310 * Flat addressing method.
9312 lun_data->luns[num_filled].lundata[0] =
9313 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8);
9314 lun_data->luns[num_filled].lundata[1] =
9315 (targ_lun_id & 0xff);
9317 } else if (targ_lun_id <= 0xffffff) {
9319 * Extended flat addressing method.
9321 lun_data->luns[num_filled].lundata[0] =
9322 RPL_LUNDATA_ATYP_EXTLUN | 0x12;
9323 scsi_ulto3b(targ_lun_id,
9324 &lun_data->luns[num_filled].lundata[1]);
9327 printf("ctl_report_luns: bogus LUN number %jd, "
9328 "skipping\n", (intmax_t)targ_lun_id);
9331 * According to SPC-3, rev 14 section 6.21:
9333 * "The execution of a REPORT LUNS command to any valid and
9334 * installed logical unit shall clear the REPORTED LUNS DATA
9335 * HAS CHANGED unit attention condition for all logical
9336 * units of that target with respect to the requesting
9337 * initiator. A valid and installed logical unit is one
9338 * having a PERIPHERAL QUALIFIER of 000b in the standard
9339 * INQUIRY data (see 6.4.2)."
9341 * If request_lun is NULL, the LUN this report luns command
9342 * was issued to is either disabled or doesn't exist. In that
9343 * case, we shouldn't clear any pending lun change unit
9346 if (request_lun != NULL) {
9347 mtx_lock(&lun->lun_lock);
9348 ctl_clr_ua(lun, initidx, CTL_UA_RES_RELEASE);
9349 mtx_unlock(&lun->lun_lock);
9352 mtx_unlock(&softc->ctl_lock);
9355 * It's quite possible that we've returned fewer LUNs than we allocated
9356 * space for. Trim it.
9358 lun_datalen = sizeof(*lun_data) +
9359 (num_filled * sizeof(struct scsi_report_luns_lundata));
9361 if (lun_datalen < alloc_len) {
9362 ctsio->residual = alloc_len - lun_datalen;
9363 ctsio->kern_data_len = lun_datalen;
9364 ctsio->kern_total_len = lun_datalen;
9366 ctsio->residual = 0;
9367 ctsio->kern_data_len = alloc_len;
9368 ctsio->kern_total_len = alloc_len;
9370 ctsio->kern_data_resid = 0;
9371 ctsio->kern_rel_offset = 0;
9372 ctsio->kern_sg_entries = 0;
9375 * We set this to the actual data length, regardless of how much
9376 * space we actually have to return results. If the user looks at
9377 * this value, he'll know whether or not he allocated enough space
9378 * and reissue the command if necessary. We don't support well
9379 * known logical units, so if the user asks for that, return none.
9381 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9384 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9387 ctl_set_success(ctsio);
9388 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9389 ctsio->be_move_done = ctl_config_move_done;
9390 ctl_datamove((union ctl_io *)ctsio);
9395 ctl_request_sense(struct ctl_scsiio *ctsio)
9397 struct scsi_request_sense *cdb;
9398 struct scsi_sense_data *sense_ptr;
9399 struct ctl_softc *ctl_softc;
9400 struct ctl_lun *lun;
9403 scsi_sense_data_type sense_format;
9404 ctl_ua_type ua_type;
9406 cdb = (struct scsi_request_sense *)ctsio->cdb;
9408 ctl_softc = control_softc;
9409 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9411 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9414 * Determine which sense format the user wants.
9416 if (cdb->byte2 & SRS_DESC)
9417 sense_format = SSD_TYPE_DESC;
9419 sense_format = SSD_TYPE_FIXED;
9421 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9422 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9423 ctsio->kern_sg_entries = 0;
9426 * struct scsi_sense_data, which is currently set to 256 bytes, is
9427 * larger than the largest allowed value for the length field in the
9428 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9430 ctsio->residual = 0;
9431 ctsio->kern_data_len = cdb->length;
9432 ctsio->kern_total_len = cdb->length;
9434 ctsio->kern_data_resid = 0;
9435 ctsio->kern_rel_offset = 0;
9436 ctsio->kern_sg_entries = 0;
9439 * If we don't have a LUN, we don't have any pending sense.
9445 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9447 * Check for pending sense, and then for pending unit attentions.
9448 * Pending sense gets returned first, then pending unit attentions.
9450 mtx_lock(&lun->lun_lock);
9452 if (ctl_is_set(lun->have_ca, initidx)) {
9453 scsi_sense_data_type stored_format;
9456 * Check to see which sense format was used for the stored
9459 stored_format = scsi_sense_type(&lun->pending_sense[initidx]);
9462 * If the user requested a different sense format than the
9463 * one we stored, then we need to convert it to the other
9464 * format. If we're going from descriptor to fixed format
9465 * sense data, we may lose things in translation, depending
9466 * on what options were used.
9468 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9469 * for some reason we'll just copy it out as-is.
9471 if ((stored_format == SSD_TYPE_FIXED)
9472 && (sense_format == SSD_TYPE_DESC))
9473 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9474 &lun->pending_sense[initidx],
9475 (struct scsi_sense_data_desc *)sense_ptr);
9476 else if ((stored_format == SSD_TYPE_DESC)
9477 && (sense_format == SSD_TYPE_FIXED))
9478 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9479 &lun->pending_sense[initidx],
9480 (struct scsi_sense_data_fixed *)sense_ptr);
9482 memcpy(sense_ptr, &lun->pending_sense[initidx],
9483 MIN(sizeof(*sense_ptr),
9484 sizeof(lun->pending_sense[initidx])));
9486 ctl_clear_mask(lun->have_ca, initidx);
9491 ua_type = ctl_build_ua(lun, initidx, sense_ptr, sense_format);
9492 if (ua_type != CTL_UA_NONE)
9494 if (ua_type == CTL_UA_LUN_CHANGE) {
9495 mtx_unlock(&lun->lun_lock);
9496 mtx_lock(&ctl_softc->ctl_lock);
9497 ctl_clear_ua(ctl_softc, initidx, ua_type);
9498 mtx_unlock(&ctl_softc->ctl_lock);
9499 mtx_lock(&lun->lun_lock);
9503 mtx_unlock(&lun->lun_lock);
9506 * We already have a pending error, return it.
9508 if (have_error != 0) {
9510 * We report the SCSI status as OK, since the status of the
9511 * request sense command itself is OK.
9512 * We report 0 for the sense length, because we aren't doing
9513 * autosense in this case. We're reporting sense as
9516 ctl_set_success(ctsio);
9517 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9518 ctsio->be_move_done = ctl_config_move_done;
9519 ctl_datamove((union ctl_io *)ctsio);
9520 return (CTL_RETVAL_COMPLETE);
9526 * No sense information to report, so we report that everything is
9529 ctl_set_sense_data(sense_ptr,
9532 /*current_error*/ 1,
9533 /*sense_key*/ SSD_KEY_NO_SENSE,
9539 * We report 0 for the sense length, because we aren't doing
9540 * autosense in this case. We're reporting sense as parameter data.
9542 ctl_set_success(ctsio);
9543 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9544 ctsio->be_move_done = ctl_config_move_done;
9545 ctl_datamove((union ctl_io *)ctsio);
9546 return (CTL_RETVAL_COMPLETE);
9550 ctl_tur(struct ctl_scsiio *ctsio)
9553 CTL_DEBUG_PRINT(("ctl_tur\n"));
9555 ctl_set_success(ctsio);
9556 ctl_done((union ctl_io *)ctsio);
9558 return (CTL_RETVAL_COMPLETE);
9563 ctl_cmddt_inquiry(struct ctl_scsiio *ctsio)
9570 * SCSI VPD page 0x00, the Supported VPD Pages page.
9573 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9575 struct scsi_vpd_supported_pages *pages;
9577 struct ctl_lun *lun;
9580 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9582 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9583 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9584 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9585 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9586 ctsio->kern_sg_entries = 0;
9588 if (sup_page_size < alloc_len) {
9589 ctsio->residual = alloc_len - sup_page_size;
9590 ctsio->kern_data_len = sup_page_size;
9591 ctsio->kern_total_len = sup_page_size;
9593 ctsio->residual = 0;
9594 ctsio->kern_data_len = alloc_len;
9595 ctsio->kern_total_len = alloc_len;
9597 ctsio->kern_data_resid = 0;
9598 ctsio->kern_rel_offset = 0;
9599 ctsio->kern_sg_entries = 0;
9602 * The control device is always connected. The disk device, on the
9603 * other hand, may not be online all the time. Need to change this
9604 * to figure out whether the disk device is actually online or not.
9607 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9608 lun->be_lun->lun_type;
9610 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9613 /* Supported VPD pages */
9614 pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9616 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9617 /* Device Identification */
9618 pages->page_list[p++] = SVPD_DEVICE_ID;
9619 /* Extended INQUIRY Data */
9620 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9621 /* Mode Page Policy */
9622 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9624 pages->page_list[p++] = SVPD_SCSI_PORTS;
9625 /* Third-party Copy */
9626 pages->page_list[p++] = SVPD_SCSI_TPC;
9627 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9629 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9630 /* Block Device Characteristics */
9631 pages->page_list[p++] = SVPD_BDC;
9632 /* Logical Block Provisioning */
9633 pages->page_list[p++] = SVPD_LBP;
9637 ctl_set_success(ctsio);
9638 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9639 ctsio->be_move_done = ctl_config_move_done;
9640 ctl_datamove((union ctl_io *)ctsio);
9641 return (CTL_RETVAL_COMPLETE);
9645 * SCSI VPD page 0x80, the Unit Serial Number page.
9648 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9650 struct scsi_vpd_unit_serial_number *sn_ptr;
9651 struct ctl_lun *lun;
9654 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9656 data_len = 4 + CTL_SN_LEN;
9657 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9658 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9659 if (data_len < alloc_len) {
9660 ctsio->residual = alloc_len - data_len;
9661 ctsio->kern_data_len = data_len;
9662 ctsio->kern_total_len = data_len;
9664 ctsio->residual = 0;
9665 ctsio->kern_data_len = alloc_len;
9666 ctsio->kern_total_len = alloc_len;
9668 ctsio->kern_data_resid = 0;
9669 ctsio->kern_rel_offset = 0;
9670 ctsio->kern_sg_entries = 0;
9673 * The control device is always connected. The disk device, on the
9674 * other hand, may not be online all the time. Need to change this
9675 * to figure out whether the disk device is actually online or not.
9678 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9679 lun->be_lun->lun_type;
9681 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9683 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9684 sn_ptr->length = CTL_SN_LEN;
9686 * If we don't have a LUN, we just leave the serial number as
9690 strncpy((char *)sn_ptr->serial_num,
9691 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9693 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9695 ctl_set_success(ctsio);
9696 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9697 ctsio->be_move_done = ctl_config_move_done;
9698 ctl_datamove((union ctl_io *)ctsio);
9699 return (CTL_RETVAL_COMPLETE);
9704 * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9707 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9709 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9710 struct ctl_lun *lun;
9713 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9715 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9716 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9717 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9718 ctsio->kern_sg_entries = 0;
9720 if (data_len < alloc_len) {
9721 ctsio->residual = alloc_len - data_len;
9722 ctsio->kern_data_len = data_len;
9723 ctsio->kern_total_len = data_len;
9725 ctsio->residual = 0;
9726 ctsio->kern_data_len = alloc_len;
9727 ctsio->kern_total_len = alloc_len;
9729 ctsio->kern_data_resid = 0;
9730 ctsio->kern_rel_offset = 0;
9731 ctsio->kern_sg_entries = 0;
9734 * The control device is always connected. The disk device, on the
9735 * other hand, may not be online all the time.
9738 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9739 lun->be_lun->lun_type;
9741 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9742 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9743 scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9745 * We support head of queue, ordered and simple tags.
9747 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9749 * Volatile cache supported.
9751 eid_ptr->flags3 = SVPD_EID_V_SUP;
9754 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9755 * attention for a particular IT nexus on all LUNs once we report
9756 * it to that nexus once. This bit is required as of SPC-4.
9758 eid_ptr->flags4 = SVPD_EID_LUICLT;
9761 * XXX KDM in order to correctly answer this, we would need
9762 * information from the SIM to determine how much sense data it
9763 * can send. So this would really be a path inquiry field, most
9764 * likely. This can be set to a maximum of 252 according to SPC-4,
9765 * but the hardware may or may not be able to support that much.
9766 * 0 just means that the maximum sense data length is not reported.
9768 eid_ptr->max_sense_length = 0;
9770 ctl_set_success(ctsio);
9771 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9772 ctsio->be_move_done = ctl_config_move_done;
9773 ctl_datamove((union ctl_io *)ctsio);
9774 return (CTL_RETVAL_COMPLETE);
9778 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9780 struct scsi_vpd_mode_page_policy *mpp_ptr;
9781 struct ctl_lun *lun;
9784 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9786 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9787 sizeof(struct scsi_vpd_mode_page_policy_descr);
9789 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9790 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9791 ctsio->kern_sg_entries = 0;
9793 if (data_len < alloc_len) {
9794 ctsio->residual = alloc_len - data_len;
9795 ctsio->kern_data_len = data_len;
9796 ctsio->kern_total_len = data_len;
9798 ctsio->residual = 0;
9799 ctsio->kern_data_len = alloc_len;
9800 ctsio->kern_total_len = alloc_len;
9802 ctsio->kern_data_resid = 0;
9803 ctsio->kern_rel_offset = 0;
9804 ctsio->kern_sg_entries = 0;
9807 * The control device is always connected. The disk device, on the
9808 * other hand, may not be online all the time.
9811 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9812 lun->be_lun->lun_type;
9814 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9815 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9816 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9817 mpp_ptr->descr[0].page_code = 0x3f;
9818 mpp_ptr->descr[0].subpage_code = 0xff;
9819 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9821 ctl_set_success(ctsio);
9822 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9823 ctsio->be_move_done = ctl_config_move_done;
9824 ctl_datamove((union ctl_io *)ctsio);
9825 return (CTL_RETVAL_COMPLETE);
9829 * SCSI VPD page 0x83, the Device Identification page.
9832 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9834 struct scsi_vpd_device_id *devid_ptr;
9835 struct scsi_vpd_id_descriptor *desc;
9836 struct ctl_softc *softc;
9837 struct ctl_lun *lun;
9838 struct ctl_port *port;
9842 softc = control_softc;
9844 port = softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)];
9845 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9847 data_len = sizeof(struct scsi_vpd_device_id) +
9848 sizeof(struct scsi_vpd_id_descriptor) +
9849 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9850 sizeof(struct scsi_vpd_id_descriptor) +
9851 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9852 if (lun && lun->lun_devid)
9853 data_len += lun->lun_devid->len;
9854 if (port->port_devid)
9855 data_len += port->port_devid->len;
9856 if (port->target_devid)
9857 data_len += port->target_devid->len;
9859 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9860 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9861 ctsio->kern_sg_entries = 0;
9863 if (data_len < alloc_len) {
9864 ctsio->residual = alloc_len - data_len;
9865 ctsio->kern_data_len = data_len;
9866 ctsio->kern_total_len = data_len;
9868 ctsio->residual = 0;
9869 ctsio->kern_data_len = alloc_len;
9870 ctsio->kern_total_len = alloc_len;
9872 ctsio->kern_data_resid = 0;
9873 ctsio->kern_rel_offset = 0;
9874 ctsio->kern_sg_entries = 0;
9877 * The control device is always connected. The disk device, on the
9878 * other hand, may not be online all the time.
9881 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9882 lun->be_lun->lun_type;
9884 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9885 devid_ptr->page_code = SVPD_DEVICE_ID;
9886 scsi_ulto2b(data_len - 4, devid_ptr->length);
9888 if (port->port_type == CTL_PORT_FC)
9889 proto = SCSI_PROTO_FC << 4;
9890 else if (port->port_type == CTL_PORT_ISCSI)
9891 proto = SCSI_PROTO_ISCSI << 4;
9893 proto = SCSI_PROTO_SPI << 4;
9894 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9897 * We're using a LUN association here. i.e., this device ID is a
9898 * per-LUN identifier.
9900 if (lun && lun->lun_devid) {
9901 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9902 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9903 lun->lun_devid->len);
9907 * This is for the WWPN which is a port association.
9909 if (port->port_devid) {
9910 memcpy(desc, port->port_devid->data, port->port_devid->len);
9911 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9912 port->port_devid->len);
9916 * This is for the Relative Target Port(type 4h) identifier
9918 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9919 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9920 SVPD_ID_TYPE_RELTARG;
9922 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9923 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9924 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9927 * This is for the Target Port Group(type 5h) identifier
9929 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9930 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9931 SVPD_ID_TYPE_TPORTGRP;
9933 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1,
9934 &desc->identifier[2]);
9935 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9936 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9939 * This is for the Target identifier
9941 if (port->target_devid) {
9942 memcpy(desc, port->target_devid->data, port->target_devid->len);
9945 ctl_set_success(ctsio);
9946 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9947 ctsio->be_move_done = ctl_config_move_done;
9948 ctl_datamove((union ctl_io *)ctsio);
9949 return (CTL_RETVAL_COMPLETE);
9953 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9955 struct ctl_softc *softc = control_softc;
9956 struct scsi_vpd_scsi_ports *sp;
9957 struct scsi_vpd_port_designation *pd;
9958 struct scsi_vpd_port_designation_cont *pdc;
9959 struct ctl_lun *lun;
9960 struct ctl_port *port;
9961 int data_len, num_target_ports, iid_len, id_len, g, pg, p;
9962 int num_target_port_groups;
9964 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9966 if (softc->is_single)
9967 num_target_port_groups = 1;
9969 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
9970 num_target_ports = 0;
9973 mtx_lock(&softc->ctl_lock);
9974 STAILQ_FOREACH(port, &softc->port_list, links) {
9975 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9978 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
9981 if (port->init_devid)
9982 iid_len += port->init_devid->len;
9983 if (port->port_devid)
9984 id_len += port->port_devid->len;
9986 mtx_unlock(&softc->ctl_lock);
9988 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups *
9989 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9990 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9991 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9992 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9993 ctsio->kern_sg_entries = 0;
9995 if (data_len < alloc_len) {
9996 ctsio->residual = alloc_len - data_len;
9997 ctsio->kern_data_len = data_len;
9998 ctsio->kern_total_len = data_len;
10000 ctsio->residual = 0;
10001 ctsio->kern_data_len = alloc_len;
10002 ctsio->kern_total_len = alloc_len;
10004 ctsio->kern_data_resid = 0;
10005 ctsio->kern_rel_offset = 0;
10006 ctsio->kern_sg_entries = 0;
10009 * The control device is always connected. The disk device, on the
10010 * other hand, may not be online all the time. Need to change this
10011 * to figure out whether the disk device is actually online or not.
10014 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
10015 lun->be_lun->lun_type;
10017 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10019 sp->page_code = SVPD_SCSI_PORTS;
10020 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
10022 pd = &sp->design[0];
10024 mtx_lock(&softc->ctl_lock);
10025 pg = softc->port_offset / CTL_MAX_PORTS;
10026 for (g = 0; g < num_target_port_groups; g++) {
10027 STAILQ_FOREACH(port, &softc->port_list, links) {
10028 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
10031 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
10033 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS;
10034 scsi_ulto2b(p, pd->relative_port_id);
10035 if (port->init_devid && g == pg) {
10036 iid_len = port->init_devid->len;
10037 memcpy(pd->initiator_transportid,
10038 port->init_devid->data, port->init_devid->len);
10041 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
10042 pdc = (struct scsi_vpd_port_designation_cont *)
10043 (&pd->initiator_transportid[iid_len]);
10044 if (port->port_devid && g == pg) {
10045 id_len = port->port_devid->len;
10046 memcpy(pdc->target_port_descriptors,
10047 port->port_devid->data, port->port_devid->len);
10050 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
10051 pd = (struct scsi_vpd_port_designation *)
10052 ((uint8_t *)pdc->target_port_descriptors + id_len);
10055 mtx_unlock(&softc->ctl_lock);
10057 ctl_set_success(ctsio);
10058 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10059 ctsio->be_move_done = ctl_config_move_done;
10060 ctl_datamove((union ctl_io *)ctsio);
10061 return (CTL_RETVAL_COMPLETE);
10065 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
10067 struct scsi_vpd_block_limits *bl_ptr;
10068 struct ctl_lun *lun;
10071 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10073 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
10074 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
10075 ctsio->kern_sg_entries = 0;
10077 if (sizeof(*bl_ptr) < alloc_len) {
10078 ctsio->residual = alloc_len - sizeof(*bl_ptr);
10079 ctsio->kern_data_len = sizeof(*bl_ptr);
10080 ctsio->kern_total_len = sizeof(*bl_ptr);
10082 ctsio->residual = 0;
10083 ctsio->kern_data_len = alloc_len;
10084 ctsio->kern_total_len = alloc_len;
10086 ctsio->kern_data_resid = 0;
10087 ctsio->kern_rel_offset = 0;
10088 ctsio->kern_sg_entries = 0;
10091 * The control device is always connected. The disk device, on the
10092 * other hand, may not be online all the time. Need to change this
10093 * to figure out whether the disk device is actually online or not.
10096 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10097 lun->be_lun->lun_type;
10099 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10101 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
10102 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
10103 bl_ptr->max_cmp_write_len = 0xff;
10104 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
10106 bs = lun->be_lun->blocksize;
10107 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
10108 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10109 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
10110 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
10111 if (lun->be_lun->ublockexp != 0) {
10112 scsi_ulto4b((1 << lun->be_lun->ublockexp),
10113 bl_ptr->opt_unmap_grain);
10114 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
10115 bl_ptr->unmap_grain_align);
10118 scsi_ulto4b(lun->be_lun->atomicblock,
10119 bl_ptr->max_atomic_transfer_length);
10120 scsi_ulto4b(0, bl_ptr->atomic_alignment);
10121 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
10123 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
10125 ctl_set_success(ctsio);
10126 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10127 ctsio->be_move_done = ctl_config_move_done;
10128 ctl_datamove((union ctl_io *)ctsio);
10129 return (CTL_RETVAL_COMPLETE);
10133 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
10135 struct scsi_vpd_block_device_characteristics *bdc_ptr;
10136 struct ctl_lun *lun;
10140 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10142 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
10143 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
10144 ctsio->kern_sg_entries = 0;
10146 if (sizeof(*bdc_ptr) < alloc_len) {
10147 ctsio->residual = alloc_len - sizeof(*bdc_ptr);
10148 ctsio->kern_data_len = sizeof(*bdc_ptr);
10149 ctsio->kern_total_len = sizeof(*bdc_ptr);
10151 ctsio->residual = 0;
10152 ctsio->kern_data_len = alloc_len;
10153 ctsio->kern_total_len = alloc_len;
10155 ctsio->kern_data_resid = 0;
10156 ctsio->kern_rel_offset = 0;
10157 ctsio->kern_sg_entries = 0;
10160 * The control device is always connected. The disk device, on the
10161 * other hand, may not be online all the time. Need to change this
10162 * to figure out whether the disk device is actually online or not.
10165 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10166 lun->be_lun->lun_type;
10168 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10169 bdc_ptr->page_code = SVPD_BDC;
10170 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
10172 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL)
10173 i = strtol(value, NULL, 0);
10175 i = CTL_DEFAULT_ROTATION_RATE;
10176 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
10178 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL)
10179 i = strtol(value, NULL, 0);
10182 bdc_ptr->wab_wac_ff = (i & 0x0f);
10183 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
10185 ctl_set_success(ctsio);
10186 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10187 ctsio->be_move_done = ctl_config_move_done;
10188 ctl_datamove((union ctl_io *)ctsio);
10189 return (CTL_RETVAL_COMPLETE);
10193 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
10195 struct scsi_vpd_logical_block_prov *lbp_ptr;
10196 struct ctl_lun *lun;
10198 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10200 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
10201 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
10202 ctsio->kern_sg_entries = 0;
10204 if (sizeof(*lbp_ptr) < alloc_len) {
10205 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
10206 ctsio->kern_data_len = sizeof(*lbp_ptr);
10207 ctsio->kern_total_len = sizeof(*lbp_ptr);
10209 ctsio->residual = 0;
10210 ctsio->kern_data_len = alloc_len;
10211 ctsio->kern_total_len = alloc_len;
10213 ctsio->kern_data_resid = 0;
10214 ctsio->kern_rel_offset = 0;
10215 ctsio->kern_sg_entries = 0;
10218 * The control device is always connected. The disk device, on the
10219 * other hand, may not be online all the time. Need to change this
10220 * to figure out whether the disk device is actually online or not.
10223 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10224 lun->be_lun->lun_type;
10226 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10228 lbp_ptr->page_code = SVPD_LBP;
10229 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
10230 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
10231 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10232 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
10233 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
10234 lbp_ptr->prov_type = SVPD_LBP_THIN;
10237 ctl_set_success(ctsio);
10238 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10239 ctsio->be_move_done = ctl_config_move_done;
10240 ctl_datamove((union ctl_io *)ctsio);
10241 return (CTL_RETVAL_COMPLETE);
10245 * INQUIRY with the EVPD bit set.
10248 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
10250 struct ctl_lun *lun;
10251 struct scsi_inquiry *cdb;
10252 int alloc_len, retval;
10254 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10255 cdb = (struct scsi_inquiry *)ctsio->cdb;
10256 alloc_len = scsi_2btoul(cdb->length);
10258 switch (cdb->page_code) {
10259 case SVPD_SUPPORTED_PAGES:
10260 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
10262 case SVPD_UNIT_SERIAL_NUMBER:
10263 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
10265 case SVPD_DEVICE_ID:
10266 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
10268 case SVPD_EXTENDED_INQUIRY_DATA:
10269 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
10271 case SVPD_MODE_PAGE_POLICY:
10272 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
10274 case SVPD_SCSI_PORTS:
10275 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10277 case SVPD_SCSI_TPC:
10278 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10280 case SVPD_BLOCK_LIMITS:
10281 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10283 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10286 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10288 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10291 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10293 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10297 ctl_set_invalid_field(ctsio,
10303 ctl_done((union ctl_io *)ctsio);
10304 retval = CTL_RETVAL_COMPLETE;
10312 * Standard INQUIRY data.
10315 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10317 struct scsi_inquiry_data *inq_ptr;
10318 struct scsi_inquiry *cdb;
10319 struct ctl_softc *softc;
10320 struct ctl_lun *lun;
10322 uint32_t alloc_len, data_len;
10323 ctl_port_type port_type;
10325 softc = control_softc;
10328 * Figure out whether we're talking to a Fibre Channel port or not.
10329 * We treat the ioctl front end, and any SCSI adapters, as packetized
10332 port_type = softc->ctl_ports[
10333 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type;
10334 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10335 port_type = CTL_PORT_SCSI;
10337 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10338 cdb = (struct scsi_inquiry *)ctsio->cdb;
10339 alloc_len = scsi_2btoul(cdb->length);
10342 * We malloc the full inquiry data size here and fill it
10343 * in. If the user only asks for less, we'll give him
10346 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10347 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10348 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10349 ctsio->kern_sg_entries = 0;
10350 ctsio->kern_data_resid = 0;
10351 ctsio->kern_rel_offset = 0;
10353 if (data_len < alloc_len) {
10354 ctsio->residual = alloc_len - data_len;
10355 ctsio->kern_data_len = data_len;
10356 ctsio->kern_total_len = data_len;
10358 ctsio->residual = 0;
10359 ctsio->kern_data_len = alloc_len;
10360 ctsio->kern_total_len = alloc_len;
10364 * If we have a LUN configured, report it as connected. Otherwise,
10365 * report that it is offline or no device is supported, depending
10366 * on the value of inquiry_pq_no_lun.
10368 * According to the spec (SPC-4 r34), the peripheral qualifier
10369 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario:
10371 * "A peripheral device having the specified peripheral device type
10372 * is not connected to this logical unit. However, the device
10373 * server is capable of supporting the specified peripheral device
10374 * type on this logical unit."
10376 * According to the same spec, the peripheral qualifier
10377 * SID_QUAL_BAD_LU (011b) is used in this scenario:
10379 * "The device server is not capable of supporting a peripheral
10380 * device on this logical unit. For this peripheral qualifier the
10381 * peripheral device type shall be set to 1Fh. All other peripheral
10382 * device type values are reserved for this peripheral qualifier."
10384 * Given the text, it would seem that we probably want to report that
10385 * the LUN is offline here. There is no LUN connected, but we can
10386 * support a LUN at the given LUN number.
10388 * In the real world, though, it sounds like things are a little
10391 * - Linux, when presented with a LUN with the offline peripheral
10392 * qualifier, will create an sg driver instance for it. So when
10393 * you attach it to CTL, you wind up with a ton of sg driver
10394 * instances. (One for every LUN that Linux bothered to probe.)
10395 * Linux does this despite the fact that it issues a REPORT LUNs
10396 * to LUN 0 to get the inventory of supported LUNs.
10398 * - There is other anecdotal evidence (from Emulex folks) about
10399 * arrays that use the offline peripheral qualifier for LUNs that
10400 * are on the "passive" path in an active/passive array.
10402 * So the solution is provide a hopefully reasonable default
10403 * (return bad/no LUN) and allow the user to change the behavior
10404 * with a tunable/sysctl variable.
10407 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10408 lun->be_lun->lun_type;
10409 else if (softc->inquiry_pq_no_lun == 0)
10410 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10412 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10414 /* RMB in byte 2 is 0 */
10415 inq_ptr->version = SCSI_REV_SPC4;
10418 * According to SAM-3, even if a device only supports a single
10419 * level of LUN addressing, it should still set the HISUP bit:
10421 * 4.9.1 Logical unit numbers overview
10423 * All logical unit number formats described in this standard are
10424 * hierarchical in structure even when only a single level in that
10425 * hierarchy is used. The HISUP bit shall be set to one in the
10426 * standard INQUIRY data (see SPC-2) when any logical unit number
10427 * format described in this standard is used. Non-hierarchical
10428 * formats are outside the scope of this standard.
10430 * Therefore we set the HiSup bit here.
10432 * The reponse format is 2, per SPC-3.
10434 inq_ptr->response_format = SID_HiSup | 2;
10436 inq_ptr->additional_length = data_len -
10437 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10438 CTL_DEBUG_PRINT(("additional_length = %d\n",
10439 inq_ptr->additional_length));
10441 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10442 /* 16 bit addressing */
10443 if (port_type == CTL_PORT_SCSI)
10444 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10445 /* XXX set the SID_MultiP bit here if we're actually going to
10446 respond on multiple ports */
10447 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10449 /* 16 bit data bus, synchronous transfers */
10450 if (port_type == CTL_PORT_SCSI)
10451 inq_ptr->flags = SID_WBus16 | SID_Sync;
10453 * XXX KDM do we want to support tagged queueing on the control
10457 || (lun->be_lun->lun_type != T_PROCESSOR))
10458 inq_ptr->flags |= SID_CmdQue;
10460 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10461 * We have 8 bytes for the vendor name, and 16 bytes for the device
10462 * name and 4 bytes for the revision.
10464 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10465 "vendor")) == NULL) {
10466 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10468 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10469 strncpy(inq_ptr->vendor, val,
10470 min(sizeof(inq_ptr->vendor), strlen(val)));
10473 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10474 sizeof(inq_ptr->product));
10475 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10476 switch (lun->be_lun->lun_type) {
10478 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10479 sizeof(inq_ptr->product));
10482 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10483 sizeof(inq_ptr->product));
10486 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10487 sizeof(inq_ptr->product));
10491 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10492 strncpy(inq_ptr->product, val,
10493 min(sizeof(inq_ptr->product), strlen(val)));
10497 * XXX make this a macro somewhere so it automatically gets
10498 * incremented when we make changes.
10500 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10501 "revision")) == NULL) {
10502 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10504 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10505 strncpy(inq_ptr->revision, val,
10506 min(sizeof(inq_ptr->revision), strlen(val)));
10510 * For parallel SCSI, we support double transition and single
10511 * transition clocking. We also support QAS (Quick Arbitration
10512 * and Selection) and Information Unit transfers on both the
10513 * control and array devices.
10515 if (port_type == CTL_PORT_SCSI)
10516 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10519 /* SAM-5 (no version claimed) */
10520 scsi_ulto2b(0x00A0, inq_ptr->version1);
10521 /* SPC-4 (no version claimed) */
10522 scsi_ulto2b(0x0460, inq_ptr->version2);
10523 if (port_type == CTL_PORT_FC) {
10524 /* FCP-2 ANSI INCITS.350:2003 */
10525 scsi_ulto2b(0x0917, inq_ptr->version3);
10526 } else if (port_type == CTL_PORT_SCSI) {
10527 /* SPI-4 ANSI INCITS.362:200x */
10528 scsi_ulto2b(0x0B56, inq_ptr->version3);
10529 } else if (port_type == CTL_PORT_ISCSI) {
10530 /* iSCSI (no version claimed) */
10531 scsi_ulto2b(0x0960, inq_ptr->version3);
10532 } else if (port_type == CTL_PORT_SAS) {
10533 /* SAS (no version claimed) */
10534 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10538 /* SBC-4 (no version claimed) */
10539 scsi_ulto2b(0x0600, inq_ptr->version4);
10541 switch (lun->be_lun->lun_type) {
10543 /* SBC-4 (no version claimed) */
10544 scsi_ulto2b(0x0600, inq_ptr->version4);
10552 ctl_set_success(ctsio);
10553 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10554 ctsio->be_move_done = ctl_config_move_done;
10555 ctl_datamove((union ctl_io *)ctsio);
10556 return (CTL_RETVAL_COMPLETE);
10560 ctl_inquiry(struct ctl_scsiio *ctsio)
10562 struct scsi_inquiry *cdb;
10565 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10567 cdb = (struct scsi_inquiry *)ctsio->cdb;
10568 if (cdb->byte2 & SI_EVPD)
10569 retval = ctl_inquiry_evpd(ctsio);
10570 else if (cdb->page_code == 0)
10571 retval = ctl_inquiry_std(ctsio);
10573 ctl_set_invalid_field(ctsio,
10579 ctl_done((union ctl_io *)ctsio);
10580 return (CTL_RETVAL_COMPLETE);
10587 * For known CDB types, parse the LBA and length.
10590 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10592 if (io->io_hdr.io_type != CTL_IO_SCSI)
10595 switch (io->scsiio.cdb[0]) {
10596 case COMPARE_AND_WRITE: {
10597 struct scsi_compare_and_write *cdb;
10599 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10601 *lba = scsi_8btou64(cdb->addr);
10602 *len = cdb->length;
10607 struct scsi_rw_6 *cdb;
10609 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10611 *lba = scsi_3btoul(cdb->addr);
10612 /* only 5 bits are valid in the most significant address byte */
10614 *len = cdb->length;
10619 struct scsi_rw_10 *cdb;
10621 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10623 *lba = scsi_4btoul(cdb->addr);
10624 *len = scsi_2btoul(cdb->length);
10627 case WRITE_VERIFY_10: {
10628 struct scsi_write_verify_10 *cdb;
10630 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10632 *lba = scsi_4btoul(cdb->addr);
10633 *len = scsi_2btoul(cdb->length);
10638 struct scsi_rw_12 *cdb;
10640 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10642 *lba = scsi_4btoul(cdb->addr);
10643 *len = scsi_4btoul(cdb->length);
10646 case WRITE_VERIFY_12: {
10647 struct scsi_write_verify_12 *cdb;
10649 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10651 *lba = scsi_4btoul(cdb->addr);
10652 *len = scsi_4btoul(cdb->length);
10657 case WRITE_ATOMIC_16: {
10658 struct scsi_rw_16 *cdb;
10660 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10662 *lba = scsi_8btou64(cdb->addr);
10663 *len = scsi_4btoul(cdb->length);
10666 case WRITE_VERIFY_16: {
10667 struct scsi_write_verify_16 *cdb;
10669 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10671 *lba = scsi_8btou64(cdb->addr);
10672 *len = scsi_4btoul(cdb->length);
10675 case WRITE_SAME_10: {
10676 struct scsi_write_same_10 *cdb;
10678 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10680 *lba = scsi_4btoul(cdb->addr);
10681 *len = scsi_2btoul(cdb->length);
10684 case WRITE_SAME_16: {
10685 struct scsi_write_same_16 *cdb;
10687 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10689 *lba = scsi_8btou64(cdb->addr);
10690 *len = scsi_4btoul(cdb->length);
10694 struct scsi_verify_10 *cdb;
10696 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10698 *lba = scsi_4btoul(cdb->addr);
10699 *len = scsi_2btoul(cdb->length);
10703 struct scsi_verify_12 *cdb;
10705 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10707 *lba = scsi_4btoul(cdb->addr);
10708 *len = scsi_4btoul(cdb->length);
10712 struct scsi_verify_16 *cdb;
10714 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10716 *lba = scsi_8btou64(cdb->addr);
10717 *len = scsi_4btoul(cdb->length);
10725 case SERVICE_ACTION_IN: { /* GET LBA STATUS */
10726 struct scsi_get_lba_status *cdb;
10728 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10729 *lba = scsi_8btou64(cdb->addr);
10735 break; /* NOTREACHED */
10742 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10745 uint64_t endlba1, endlba2;
10747 endlba1 = lba1 + len1 - (seq ? 0 : 1);
10748 endlba2 = lba2 + len2 - 1;
10750 if ((endlba1 < lba2) || (endlba2 < lba1))
10751 return (CTL_ACTION_PASS);
10753 return (CTL_ACTION_BLOCK);
10757 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10759 struct ctl_ptr_len_flags *ptrlen;
10760 struct scsi_unmap_desc *buf, *end, *range;
10764 /* If not UNMAP -- go other way. */
10765 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10766 io->scsiio.cdb[0] != UNMAP)
10767 return (CTL_ACTION_ERROR);
10769 /* If UNMAP without data -- block and wait for data. */
10770 ptrlen = (struct ctl_ptr_len_flags *)
10771 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10772 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10773 ptrlen->ptr == NULL)
10774 return (CTL_ACTION_BLOCK);
10776 /* UNMAP with data -- check for collision. */
10777 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10778 end = buf + ptrlen->len / sizeof(*buf);
10779 for (range = buf; range < end; range++) {
10780 lba = scsi_8btou64(range->lba);
10781 len = scsi_4btoul(range->length);
10782 if ((lba < lba2 + len2) && (lba + len > lba2))
10783 return (CTL_ACTION_BLOCK);
10785 return (CTL_ACTION_PASS);
10789 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10791 uint64_t lba1, lba2;
10792 uint64_t len1, len2;
10795 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10796 return (CTL_ACTION_ERROR);
10798 retval = ctl_extent_check_unmap(io1, lba2, len2);
10799 if (retval != CTL_ACTION_ERROR)
10802 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10803 return (CTL_ACTION_ERROR);
10805 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
10809 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2)
10811 uint64_t lba1, lba2;
10812 uint64_t len1, len2;
10814 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10815 return (CTL_ACTION_ERROR);
10816 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10817 return (CTL_ACTION_ERROR);
10819 if (lba1 + len1 == lba2)
10820 return (CTL_ACTION_BLOCK);
10821 return (CTL_ACTION_PASS);
10825 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10826 union ctl_io *ooa_io)
10828 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10829 ctl_serialize_action *serialize_row;
10832 * The initiator attempted multiple untagged commands at the same
10833 * time. Can't do that.
10835 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10836 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10837 && ((pending_io->io_hdr.nexus.targ_port ==
10838 ooa_io->io_hdr.nexus.targ_port)
10839 && (pending_io->io_hdr.nexus.initid.id ==
10840 ooa_io->io_hdr.nexus.initid.id))
10841 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10842 CTL_FLAG_STATUS_SENT)) == 0))
10843 return (CTL_ACTION_OVERLAP);
10846 * The initiator attempted to send multiple tagged commands with
10847 * the same ID. (It's fine if different initiators have the same
10850 * Even if all of those conditions are true, we don't kill the I/O
10851 * if the command ahead of us has been aborted. We won't end up
10852 * sending it to the FETD, and it's perfectly legal to resend a
10853 * command with the same tag number as long as the previous
10854 * instance of this tag number has been aborted somehow.
10856 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10857 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10858 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10859 && ((pending_io->io_hdr.nexus.targ_port ==
10860 ooa_io->io_hdr.nexus.targ_port)
10861 && (pending_io->io_hdr.nexus.initid.id ==
10862 ooa_io->io_hdr.nexus.initid.id))
10863 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10864 CTL_FLAG_STATUS_SENT)) == 0))
10865 return (CTL_ACTION_OVERLAP_TAG);
10868 * If we get a head of queue tag, SAM-3 says that we should
10869 * immediately execute it.
10871 * What happens if this command would normally block for some other
10872 * reason? e.g. a request sense with a head of queue tag
10873 * immediately after a write. Normally that would block, but this
10874 * will result in its getting executed immediately...
10876 * We currently return "pass" instead of "skip", so we'll end up
10877 * going through the rest of the queue to check for overlapped tags.
10879 * XXX KDM check for other types of blockage first??
10881 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10882 return (CTL_ACTION_PASS);
10885 * Ordered tags have to block until all items ahead of them
10886 * have completed. If we get called with an ordered tag, we always
10887 * block, if something else is ahead of us in the queue.
10889 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10890 return (CTL_ACTION_BLOCK);
10893 * Simple tags get blocked until all head of queue and ordered tags
10894 * ahead of them have completed. I'm lumping untagged commands in
10895 * with simple tags here. XXX KDM is that the right thing to do?
10897 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10898 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10899 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10900 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10901 return (CTL_ACTION_BLOCK);
10903 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
10904 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
10906 serialize_row = ctl_serialize_table[ooa_entry->seridx];
10908 switch (serialize_row[pending_entry->seridx]) {
10909 case CTL_SER_BLOCK:
10910 return (CTL_ACTION_BLOCK);
10911 case CTL_SER_EXTENT:
10912 return (ctl_extent_check(ooa_io, pending_io,
10913 (lun->serseq == CTL_LUN_SERSEQ_ON)));
10914 case CTL_SER_EXTENTOPT:
10915 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
10916 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
10917 return (ctl_extent_check(ooa_io, pending_io,
10918 (lun->serseq == CTL_LUN_SERSEQ_ON)));
10919 return (CTL_ACTION_PASS);
10920 case CTL_SER_EXTENTSEQ:
10921 if (lun->serseq != CTL_LUN_SERSEQ_OFF)
10922 return (ctl_extent_check_seq(ooa_io, pending_io));
10923 return (CTL_ACTION_PASS);
10925 return (CTL_ACTION_PASS);
10926 case CTL_SER_BLOCKOPT:
10927 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
10928 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
10929 return (CTL_ACTION_BLOCK);
10930 return (CTL_ACTION_PASS);
10932 return (CTL_ACTION_SKIP);
10934 panic("invalid serialization value %d",
10935 serialize_row[pending_entry->seridx]);
10938 return (CTL_ACTION_ERROR);
10942 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
10944 * - pending_io is generally either incoming, or on the blocked queue
10945 * - starting I/O is the I/O we want to start the check with.
10948 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
10949 union ctl_io *starting_io)
10951 union ctl_io *ooa_io;
10954 mtx_assert(&lun->lun_lock, MA_OWNED);
10957 * Run back along the OOA queue, starting with the current
10958 * blocked I/O and going through every I/O before it on the
10959 * queue. If starting_io is NULL, we'll just end up returning
10962 for (ooa_io = starting_io; ooa_io != NULL;
10963 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
10967 * This routine just checks to see whether
10968 * cur_blocked is blocked by ooa_io, which is ahead
10969 * of it in the queue. It doesn't queue/dequeue
10972 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
10974 case CTL_ACTION_BLOCK:
10975 case CTL_ACTION_OVERLAP:
10976 case CTL_ACTION_OVERLAP_TAG:
10977 case CTL_ACTION_SKIP:
10978 case CTL_ACTION_ERROR:
10980 break; /* NOTREACHED */
10981 case CTL_ACTION_PASS:
10984 panic("invalid action %d", action);
10985 break; /* NOTREACHED */
10989 return (CTL_ACTION_PASS);
10994 * - An I/O has just completed, and has been removed from the per-LUN OOA
10995 * queue, so some items on the blocked queue may now be unblocked.
10998 ctl_check_blocked(struct ctl_lun *lun)
11000 union ctl_io *cur_blocked, *next_blocked;
11002 mtx_assert(&lun->lun_lock, MA_OWNED);
11005 * Run forward from the head of the blocked queue, checking each
11006 * entry against the I/Os prior to it on the OOA queue to see if
11007 * there is still any blockage.
11009 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
11010 * with our removing a variable on it while it is traversing the
11013 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
11014 cur_blocked != NULL; cur_blocked = next_blocked) {
11015 union ctl_io *prev_ooa;
11018 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
11021 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
11022 ctl_ooaq, ooa_links);
11025 * If cur_blocked happens to be the first item in the OOA
11026 * queue now, prev_ooa will be NULL, and the action
11027 * returned will just be CTL_ACTION_PASS.
11029 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
11032 case CTL_ACTION_BLOCK:
11033 /* Nothing to do here, still blocked */
11035 case CTL_ACTION_OVERLAP:
11036 case CTL_ACTION_OVERLAP_TAG:
11038 * This shouldn't happen! In theory we've already
11039 * checked this command for overlap...
11042 case CTL_ACTION_PASS:
11043 case CTL_ACTION_SKIP: {
11044 const struct ctl_cmd_entry *entry;
11048 * The skip case shouldn't happen, this transaction
11049 * should have never made it onto the blocked queue.
11052 * This I/O is no longer blocked, we can remove it
11053 * from the blocked queue. Since this is a TAILQ
11054 * (doubly linked list), we can do O(1) removals
11055 * from any place on the list.
11057 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
11059 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11061 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){
11063 * Need to send IO back to original side to
11066 union ctl_ha_msg msg_info;
11068 msg_info.hdr.original_sc =
11069 cur_blocked->io_hdr.original_sc;
11070 msg_info.hdr.serializing_sc = cur_blocked;
11071 msg_info.hdr.msg_type = CTL_MSG_R2R;
11072 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11073 &msg_info, sizeof(msg_info), 0)) >
11074 CTL_HA_STATUS_SUCCESS) {
11075 printf("CTL:Check Blocked error from "
11076 "ctl_ha_msg_send %d\n",
11081 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
11084 * Check this I/O for LUN state changes that may
11085 * have happened while this command was blocked.
11086 * The LUN state may have been changed by a command
11087 * ahead of us in the queue, so we need to re-check
11088 * for any states that can be caused by SCSI
11091 if (ctl_scsiio_lun_check(lun, entry,
11092 &cur_blocked->scsiio) == 0) {
11093 cur_blocked->io_hdr.flags |=
11094 CTL_FLAG_IS_WAS_ON_RTR;
11095 ctl_enqueue_rtr(cur_blocked);
11097 ctl_done(cur_blocked);
11102 * This probably shouldn't happen -- we shouldn't
11103 * get CTL_ACTION_ERROR, or anything else.
11109 return (CTL_RETVAL_COMPLETE);
11113 * This routine (with one exception) checks LUN flags that can be set by
11114 * commands ahead of us in the OOA queue. These flags have to be checked
11115 * when a command initially comes in, and when we pull a command off the
11116 * blocked queue and are preparing to execute it. The reason we have to
11117 * check these flags for commands on the blocked queue is that the LUN
11118 * state may have been changed by a command ahead of us while we're on the
11121 * Ordering is somewhat important with these checks, so please pay
11122 * careful attention to the placement of any new checks.
11125 ctl_scsiio_lun_check(struct ctl_lun *lun,
11126 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11128 struct ctl_softc *softc = lun->ctl_softc;
11134 mtx_assert(&lun->lun_lock, MA_OWNED);
11137 * If this shelf is a secondary shelf controller, we have to reject
11138 * any media access commands.
11140 if ((softc->flags & CTL_FLAG_ACTIVE_SHELF) == 0 &&
11141 (entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0) {
11142 ctl_set_lun_standby(ctsio);
11147 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11148 if (lun->flags & CTL_LUN_READONLY) {
11149 ctl_set_sense(ctsio, /*current_error*/ 1,
11150 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11151 /*asc*/ 0x27, /*ascq*/ 0x01, SSD_ELEM_NONE);
11155 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT]
11156 .eca_and_aen & SCP_SWP) != 0) {
11157 ctl_set_sense(ctsio, /*current_error*/ 1,
11158 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11159 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11166 * Check for a reservation conflict. If this command isn't allowed
11167 * even on reserved LUNs, and if this initiator isn't the one who
11168 * reserved us, reject the command with a reservation conflict.
11170 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
11171 if ((lun->flags & CTL_LUN_RESERVED)
11172 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11173 if (lun->res_idx != residx) {
11174 ctl_set_reservation_conflict(ctsio);
11180 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11181 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11182 /* No reservation or command is allowed. */;
11183 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11184 (lun->res_type == SPR_TYPE_WR_EX ||
11185 lun->res_type == SPR_TYPE_WR_EX_RO ||
11186 lun->res_type == SPR_TYPE_WR_EX_AR)) {
11187 /* The command is allowed for Write Exclusive resv. */;
11190 * if we aren't registered or it's a res holder type
11191 * reservation and this isn't the res holder then set a
11194 if (ctl_get_prkey(lun, residx) == 0
11195 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
11196 ctl_set_reservation_conflict(ctsio);
11203 if ((lun->flags & CTL_LUN_OFFLINE)
11204 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) {
11205 ctl_set_lun_not_ready(ctsio);
11211 * If the LUN is stopped, see if this particular command is allowed
11212 * for a stopped lun. Otherwise, reject it with 0x04,0x02.
11214 if ((lun->flags & CTL_LUN_STOPPED)
11215 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
11216 /* "Logical unit not ready, initializing cmd. required" */
11217 ctl_set_lun_stopped(ctsio);
11222 if ((lun->flags & CTL_LUN_INOPERABLE)
11223 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
11224 /* "Medium format corrupted" */
11225 ctl_set_medium_format_corrupted(ctsio);
11236 ctl_failover_io(union ctl_io *io, int have_lock)
11238 ctl_set_busy(&io->scsiio);
11246 struct ctl_lun *lun;
11247 struct ctl_softc *softc;
11248 union ctl_io *next_io, *pending_io;
11252 softc = control_softc;
11254 mtx_lock(&softc->ctl_lock);
11256 * Remove any cmds from the other SC from the rtr queue. These
11257 * will obviously only be for LUNs for which we're the primary.
11258 * We can't send status or get/send data for these commands.
11259 * Since they haven't been executed yet, we can just remove them.
11260 * We'll either abort them or delete them below, depending on
11261 * which HA mode we're in.
11264 mtx_lock(&softc->queue_lock);
11265 for (io = (union ctl_io *)STAILQ_FIRST(&softc->rtr_queue);
11266 io != NULL; io = next_io) {
11267 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
11268 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
11269 STAILQ_REMOVE(&softc->rtr_queue, &io->io_hdr,
11270 ctl_io_hdr, links);
11272 mtx_unlock(&softc->queue_lock);
11275 for (lun_idx=0; lun_idx < softc->num_luns; lun_idx++) {
11276 lun = softc->ctl_luns[lun_idx];
11281 * Processor LUNs are primary on both sides.
11282 * XXX will this always be true?
11284 if (lun->be_lun->lun_type == T_PROCESSOR)
11287 if ((lun->flags & CTL_LUN_PRIMARY_SC)
11288 && (softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
11289 printf("FAILOVER: primary lun %d\n", lun_idx);
11291 * Remove all commands from the other SC. First from the
11292 * blocked queue then from the ooa queue. Once we have
11293 * removed them. Call ctl_check_blocked to see if there
11294 * is anything that can run.
11296 for (io = (union ctl_io *)TAILQ_FIRST(
11297 &lun->blocked_queue); io != NULL; io = next_io) {
11299 next_io = (union ctl_io *)TAILQ_NEXT(
11300 &io->io_hdr, blocked_links);
11302 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
11303 TAILQ_REMOVE(&lun->blocked_queue,
11304 &io->io_hdr,blocked_links);
11305 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11306 TAILQ_REMOVE(&lun->ooa_queue,
11307 &io->io_hdr, ooa_links);
11313 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
11314 io != NULL; io = next_io) {
11316 next_io = (union ctl_io *)TAILQ_NEXT(
11317 &io->io_hdr, ooa_links);
11319 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
11321 TAILQ_REMOVE(&lun->ooa_queue,
11328 ctl_check_blocked(lun);
11329 } else if ((lun->flags & CTL_LUN_PRIMARY_SC)
11330 && (softc->ha_mode == CTL_HA_MODE_XFER)) {
11332 printf("FAILOVER: primary lun %d\n", lun_idx);
11334 * Abort all commands from the other SC. We can't
11335 * send status back for them now. These should get
11336 * cleaned up when they are completed or come out
11337 * for a datamove operation.
11339 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
11340 io != NULL; io = next_io) {
11341 next_io = (union ctl_io *)TAILQ_NEXT(
11342 &io->io_hdr, ooa_links);
11344 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
11345 io->io_hdr.flags |= CTL_FLAG_ABORT;
11347 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
11348 && (softc->ha_mode == CTL_HA_MODE_XFER)) {
11350 printf("FAILOVER: secondary lun %d\n", lun_idx);
11352 lun->flags |= CTL_LUN_PRIMARY_SC;
11355 * We send all I/O that was sent to this controller
11356 * and redirected to the other side back with
11357 * busy status, and have the initiator retry it.
11358 * Figuring out how much data has been transferred,
11359 * etc. and picking up where we left off would be
11362 * XXX KDM need to remove I/O from the blocked
11365 for (pending_io = (union ctl_io *)TAILQ_FIRST(
11366 &lun->ooa_queue); pending_io != NULL;
11367 pending_io = next_io) {
11369 next_io = (union ctl_io *)TAILQ_NEXT(
11370 &pending_io->io_hdr, ooa_links);
11372 pending_io->io_hdr.flags &=
11373 ~CTL_FLAG_SENT_2OTHER_SC;
11375 if (pending_io->io_hdr.flags &
11376 CTL_FLAG_IO_ACTIVE) {
11377 pending_io->io_hdr.flags |=
11380 ctl_set_busy(&pending_io->scsiio);
11381 ctl_done(pending_io);
11385 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
11386 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
11387 && (softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
11388 printf("FAILOVER: secondary lun %d\n", lun_idx);
11390 * if the first io on the OOA is not on the RtR queue
11393 lun->flags |= CTL_LUN_PRIMARY_SC;
11395 pending_io = (union ctl_io *)TAILQ_FIRST(
11397 if (pending_io==NULL) {
11398 printf("Nothing on OOA queue\n");
11402 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11403 if ((pending_io->io_hdr.flags &
11404 CTL_FLAG_IS_WAS_ON_RTR) == 0) {
11405 pending_io->io_hdr.flags |=
11406 CTL_FLAG_IS_WAS_ON_RTR;
11407 ctl_enqueue_rtr(pending_io);
11412 printf("Tag 0x%04x is running\n",
11413 pending_io->scsiio.tag_num);
11417 next_io = (union ctl_io *)TAILQ_NEXT(
11418 &pending_io->io_hdr, ooa_links);
11419 for (pending_io=next_io; pending_io != NULL;
11420 pending_io = next_io) {
11421 pending_io->io_hdr.flags &=
11422 ~CTL_FLAG_SENT_2OTHER_SC;
11423 next_io = (union ctl_io *)TAILQ_NEXT(
11424 &pending_io->io_hdr, ooa_links);
11425 if (pending_io->io_hdr.flags &
11426 CTL_FLAG_IS_WAS_ON_RTR) {
11428 printf("Tag 0x%04x is running\n",
11429 pending_io->scsiio.tag_num);
11434 switch (ctl_check_ooa(lun, pending_io,
11435 (union ctl_io *)TAILQ_PREV(
11436 &pending_io->io_hdr, ctl_ooaq,
11439 case CTL_ACTION_BLOCK:
11440 TAILQ_INSERT_TAIL(&lun->blocked_queue,
11441 &pending_io->io_hdr,
11443 pending_io->io_hdr.flags |=
11446 case CTL_ACTION_PASS:
11447 case CTL_ACTION_SKIP:
11448 pending_io->io_hdr.flags |=
11449 CTL_FLAG_IS_WAS_ON_RTR;
11450 ctl_enqueue_rtr(pending_io);
11452 case CTL_ACTION_OVERLAP:
11453 ctl_set_overlapped_cmd(
11454 (struct ctl_scsiio *)pending_io);
11455 ctl_done(pending_io);
11457 case CTL_ACTION_OVERLAP_TAG:
11458 ctl_set_overlapped_tag(
11459 (struct ctl_scsiio *)pending_io,
11460 pending_io->scsiio.tag_num & 0xff);
11461 ctl_done(pending_io);
11463 case CTL_ACTION_ERROR:
11465 ctl_set_internal_failure(
11466 (struct ctl_scsiio *)pending_io,
11469 ctl_done(pending_io);
11474 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
11476 panic("Unhandled HA mode failover, LUN flags = %#x, "
11477 "ha_mode = #%x", lun->flags, softc->ha_mode);
11481 mtx_unlock(&softc->ctl_lock);
11486 ctl_clear_ua(struct ctl_softc *ctl_softc, uint32_t initidx,
11487 ctl_ua_type ua_type)
11489 struct ctl_lun *lun;
11492 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
11494 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) {
11495 mtx_lock(&lun->lun_lock);
11496 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
11498 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua_type;
11499 mtx_unlock(&lun->lun_lock);
11504 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio)
11506 struct ctl_lun *lun;
11507 const struct ctl_cmd_entry *entry;
11508 uint32_t initidx, targ_lun;
11515 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11516 if ((targ_lun < CTL_MAX_LUNS)
11517 && ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
11519 * If the LUN is invalid, pretend that it doesn't exist.
11520 * It will go away as soon as all pending I/O has been
11523 mtx_lock(&lun->lun_lock);
11524 if (lun->flags & CTL_LUN_DISABLED) {
11525 mtx_unlock(&lun->lun_lock);
11527 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11528 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11530 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
11531 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
11533 if (lun->be_lun->lun_type == T_PROCESSOR) {
11534 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV;
11538 * Every I/O goes into the OOA queue for a
11539 * particular LUN, and stays there until completion.
11542 if (TAILQ_EMPTY(&lun->ooa_queue)) {
11543 lun->idle_time += getsbinuptime() -
11547 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr,
11551 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11552 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11555 /* Get command entry and return error if it is unsuppotyed. */
11556 entry = ctl_validate_command(ctsio);
11557 if (entry == NULL) {
11559 mtx_unlock(&lun->lun_lock);
11563 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11564 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11567 * Check to see whether we can send this command to LUNs that don't
11568 * exist. This should pretty much only be the case for inquiry
11569 * and request sense. Further checks, below, really require having
11570 * a LUN, so we can't really check the command anymore. Just put
11571 * it on the rtr queue.
11574 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) {
11575 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11576 ctl_enqueue_rtr((union ctl_io *)ctsio);
11580 ctl_set_unsupported_lun(ctsio);
11581 ctl_done((union ctl_io *)ctsio);
11582 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11586 * Make sure we support this particular command on this LUN.
11587 * e.g., we don't support writes to the control LUN.
11589 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11590 mtx_unlock(&lun->lun_lock);
11591 ctl_set_invalid_opcode(ctsio);
11592 ctl_done((union ctl_io *)ctsio);
11597 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11601 * If we've got a request sense, it'll clear the contingent
11602 * allegiance condition. Otherwise, if we have a CA condition for
11603 * this initiator, clear it, because it sent down a command other
11604 * than request sense.
11606 if ((ctsio->cdb[0] != REQUEST_SENSE)
11607 && (ctl_is_set(lun->have_ca, initidx)))
11608 ctl_clear_mask(lun->have_ca, initidx);
11612 * If the command has this flag set, it handles its own unit
11613 * attention reporting, we shouldn't do anything. Otherwise we
11614 * check for any pending unit attentions, and send them back to the
11615 * initiator. We only do this when a command initially comes in,
11616 * not when we pull it off the blocked queue.
11618 * According to SAM-3, section 5.3.2, the order that things get
11619 * presented back to the host is basically unit attentions caused
11620 * by some sort of reset event, busy status, reservation conflicts
11621 * or task set full, and finally any other status.
11623 * One issue here is that some of the unit attentions we report
11624 * don't fall into the "reset" category (e.g. "reported luns data
11625 * has changed"). So reporting it here, before the reservation
11626 * check, may be technically wrong. I guess the only thing to do
11627 * would be to check for and report the reset events here, and then
11628 * check for the other unit attention types after we check for a
11629 * reservation conflict.
11631 * XXX KDM need to fix this
11633 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11634 ctl_ua_type ua_type;
11635 scsi_sense_data_type sense_format;
11637 if (lun->flags & CTL_LUN_SENSE_DESC)
11638 sense_format = SSD_TYPE_DESC;
11640 sense_format = SSD_TYPE_FIXED;
11642 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11644 if (ua_type != CTL_UA_NONE) {
11645 mtx_unlock(&lun->lun_lock);
11646 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11647 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11648 ctsio->sense_len = SSD_FULL_SIZE;
11649 ctl_done((union ctl_io *)ctsio);
11655 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11656 mtx_unlock(&lun->lun_lock);
11657 ctl_done((union ctl_io *)ctsio);
11662 * XXX CHD this is where we want to send IO to other side if
11663 * this LUN is secondary on this SC. We will need to make a copy
11664 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11665 * the copy we send as FROM_OTHER.
11666 * We also need to stuff the address of the original IO so we can
11667 * find it easily. Something similar will need be done on the other
11668 * side so when we are done we can find the copy.
11670 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11671 union ctl_ha_msg msg_info;
11674 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11676 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11677 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11679 printf("1. ctsio %p\n", ctsio);
11681 msg_info.hdr.serializing_sc = NULL;
11682 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11683 msg_info.scsi.tag_num = ctsio->tag_num;
11684 msg_info.scsi.tag_type = ctsio->tag_type;
11685 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11687 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11689 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11690 (void *)&msg_info, sizeof(msg_info), 0)) >
11691 CTL_HA_STATUS_SUCCESS) {
11692 printf("CTL:precheck, ctl_ha_msg_send returned %d\n",
11694 printf("CTL:opcode is %x\n", ctsio->cdb[0]);
11697 printf("CTL:Precheck sent msg, opcode is %x\n",opcode);
11702 * XXX KDM this I/O is off the incoming queue, but hasn't
11703 * been inserted on any other queue. We may need to come
11704 * up with a holding queue while we wait for serialization
11705 * so that we have an idea of what we're waiting for from
11708 mtx_unlock(&lun->lun_lock);
11712 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11713 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11714 ctl_ooaq, ooa_links))) {
11715 case CTL_ACTION_BLOCK:
11716 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11717 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11719 mtx_unlock(&lun->lun_lock);
11721 case CTL_ACTION_PASS:
11722 case CTL_ACTION_SKIP:
11723 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11724 mtx_unlock(&lun->lun_lock);
11725 ctl_enqueue_rtr((union ctl_io *)ctsio);
11727 case CTL_ACTION_OVERLAP:
11728 mtx_unlock(&lun->lun_lock);
11729 ctl_set_overlapped_cmd(ctsio);
11730 ctl_done((union ctl_io *)ctsio);
11732 case CTL_ACTION_OVERLAP_TAG:
11733 mtx_unlock(&lun->lun_lock);
11734 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11735 ctl_done((union ctl_io *)ctsio);
11737 case CTL_ACTION_ERROR:
11739 mtx_unlock(&lun->lun_lock);
11740 ctl_set_internal_failure(ctsio,
11742 /*retry_count*/ 0);
11743 ctl_done((union ctl_io *)ctsio);
11749 const struct ctl_cmd_entry *
11750 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11752 const struct ctl_cmd_entry *entry;
11753 int service_action;
11755 entry = &ctl_cmd_table[ctsio->cdb[0]];
11757 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11758 if (entry->flags & CTL_CMD_FLAG_SA5) {
11759 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11760 entry = &((const struct ctl_cmd_entry *)
11761 entry->execute)[service_action];
11766 const struct ctl_cmd_entry *
11767 ctl_validate_command(struct ctl_scsiio *ctsio)
11769 const struct ctl_cmd_entry *entry;
11773 entry = ctl_get_cmd_entry(ctsio, &sa);
11774 if (entry->execute == NULL) {
11776 ctl_set_invalid_field(ctsio,
11783 ctl_set_invalid_opcode(ctsio);
11784 ctl_done((union ctl_io *)ctsio);
11787 KASSERT(entry->length > 0,
11788 ("Not defined length for command 0x%02x/0x%02x",
11789 ctsio->cdb[0], ctsio->cdb[1]));
11790 for (i = 1; i < entry->length; i++) {
11791 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11794 ctl_set_invalid_field(ctsio,
11799 /*bit*/ fls(diff) - 1);
11800 ctl_done((union ctl_io *)ctsio);
11807 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11810 switch (lun_type) {
11812 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) &&
11813 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11817 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) &&
11818 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11828 ctl_scsiio(struct ctl_scsiio *ctsio)
11831 const struct ctl_cmd_entry *entry;
11833 retval = CTL_RETVAL_COMPLETE;
11835 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11837 entry = ctl_get_cmd_entry(ctsio, NULL);
11840 * If this I/O has been aborted, just send it straight to
11841 * ctl_done() without executing it.
11843 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11844 ctl_done((union ctl_io *)ctsio);
11849 * All the checks should have been handled by ctl_scsiio_precheck().
11850 * We should be clear now to just execute the I/O.
11852 retval = entry->execute(ctsio);
11859 * Since we only implement one target right now, a bus reset simply resets
11860 * our single target.
11863 ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io)
11865 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET));
11869 ctl_target_reset(struct ctl_softc *softc, union ctl_io *io,
11870 ctl_ua_type ua_type)
11872 struct ctl_lun *lun;
11875 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11876 union ctl_ha_msg msg_info;
11878 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11879 msg_info.hdr.nexus = io->io_hdr.nexus;
11880 if (ua_type==CTL_UA_TARG_RESET)
11881 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11883 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11884 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11885 msg_info.hdr.original_sc = NULL;
11886 msg_info.hdr.serializing_sc = NULL;
11887 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11888 (void *)&msg_info, sizeof(msg_info), 0)) {
11893 mtx_lock(&softc->ctl_lock);
11894 STAILQ_FOREACH(lun, &softc->lun_list, links)
11895 retval += ctl_lun_reset(lun, io, ua_type);
11896 mtx_unlock(&softc->ctl_lock);
11902 * The LUN should always be set. The I/O is optional, and is used to
11903 * distinguish between I/Os sent by this initiator, and by other
11904 * initiators. We set unit attention for initiators other than this one.
11905 * SAM-3 is vague on this point. It does say that a unit attention should
11906 * be established for other initiators when a LUN is reset (see section
11907 * 5.7.3), but it doesn't specifically say that the unit attention should
11908 * be established for this particular initiator when a LUN is reset. Here
11909 * is the relevant text, from SAM-3 rev 8:
11911 * 5.7.2 When a SCSI initiator port aborts its own tasks
11913 * When a SCSI initiator port causes its own task(s) to be aborted, no
11914 * notification that the task(s) have been aborted shall be returned to
11915 * the SCSI initiator port other than the completion response for the
11916 * command or task management function action that caused the task(s) to
11917 * be aborted and notification(s) associated with related effects of the
11918 * action (e.g., a reset unit attention condition).
11920 * XXX KDM for now, we're setting unit attention for all initiators.
11923 ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11933 mtx_lock(&lun->lun_lock);
11935 * Run through the OOA queue and abort each I/O.
11937 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11938 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11939 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11943 * This version sets unit attention for every
11946 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11947 ctl_est_ua_all(lun, initidx, ua_type);
11949 ctl_est_ua_all(lun, -1, ua_type);
11953 * A reset (any kind, really) clears reservations established with
11954 * RESERVE/RELEASE. It does not clear reservations established
11955 * with PERSISTENT RESERVE OUT, but we don't support that at the
11956 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
11957 * reservations made with the RESERVE/RELEASE commands, because
11958 * those commands are obsolete in SPC-3.
11960 lun->flags &= ~CTL_LUN_RESERVED;
11963 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11964 ctl_clear_mask(lun->have_ca, i);
11966 mtx_unlock(&lun->lun_lock);
11972 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11977 mtx_assert(&lun->lun_lock, MA_OWNED);
11980 * Run through the OOA queue and attempt to find the given I/O.
11981 * The target port, initiator ID, tag type and tag number have to
11982 * match the values that we got from the initiator. If we have an
11983 * untagged command to abort, simply abort the first untagged command
11984 * we come to. We only allow one untagged command at a time of course.
11986 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11987 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11989 if ((targ_port == UINT32_MAX ||
11990 targ_port == xio->io_hdr.nexus.targ_port) &&
11991 (init_id == UINT32_MAX ||
11992 init_id == xio->io_hdr.nexus.initid.id)) {
11993 if (targ_port != xio->io_hdr.nexus.targ_port ||
11994 init_id != xio->io_hdr.nexus.initid.id)
11995 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
11996 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11997 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11998 union ctl_ha_msg msg_info;
12000 msg_info.hdr.nexus = xio->io_hdr.nexus;
12001 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12002 msg_info.task.tag_num = xio->scsiio.tag_num;
12003 msg_info.task.tag_type = xio->scsiio.tag_type;
12004 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12005 msg_info.hdr.original_sc = NULL;
12006 msg_info.hdr.serializing_sc = NULL;
12007 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12008 (void *)&msg_info, sizeof(msg_info), 0);
12015 ctl_abort_task_set(union ctl_io *io)
12017 struct ctl_softc *softc = control_softc;
12018 struct ctl_lun *lun;
12024 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12025 mtx_lock(&softc->ctl_lock);
12026 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL))
12027 lun = softc->ctl_luns[targ_lun];
12029 mtx_unlock(&softc->ctl_lock);
12033 mtx_lock(&lun->lun_lock);
12034 mtx_unlock(&softc->ctl_lock);
12035 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
12036 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12037 io->io_hdr.nexus.initid.id,
12038 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12039 } else { /* CTL_TASK_CLEAR_TASK_SET */
12040 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
12041 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12043 mtx_unlock(&lun->lun_lock);
12048 ctl_i_t_nexus_reset(union ctl_io *io)
12050 struct ctl_softc *softc = control_softc;
12051 struct ctl_lun *lun;
12052 uint32_t initidx, residx;
12054 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12055 residx = ctl_get_resindex(&io->io_hdr.nexus);
12056 mtx_lock(&softc->ctl_lock);
12057 STAILQ_FOREACH(lun, &softc->lun_list, links) {
12058 mtx_lock(&lun->lun_lock);
12059 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12060 io->io_hdr.nexus.initid.id,
12061 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12063 ctl_clear_mask(lun->have_ca, initidx);
12065 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
12066 lun->flags &= ~CTL_LUN_RESERVED;
12067 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS);
12068 mtx_unlock(&lun->lun_lock);
12070 mtx_unlock(&softc->ctl_lock);
12075 ctl_abort_task(union ctl_io *io)
12078 struct ctl_lun *lun;
12079 struct ctl_softc *softc;
12082 char printbuf[128];
12087 softc = control_softc;
12093 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12094 mtx_lock(&softc->ctl_lock);
12095 if ((targ_lun < CTL_MAX_LUNS)
12096 && (softc->ctl_luns[targ_lun] != NULL))
12097 lun = softc->ctl_luns[targ_lun];
12099 mtx_unlock(&softc->ctl_lock);
12104 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
12105 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
12108 mtx_lock(&lun->lun_lock);
12109 mtx_unlock(&softc->ctl_lock);
12111 * Run through the OOA queue and attempt to find the given I/O.
12112 * The target port, initiator ID, tag type and tag number have to
12113 * match the values that we got from the initiator. If we have an
12114 * untagged command to abort, simply abort the first untagged command
12115 * we come to. We only allow one untagged command at a time of course.
12117 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12118 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12120 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
12122 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
12123 lun->lun, xio->scsiio.tag_num,
12124 xio->scsiio.tag_type,
12125 (xio->io_hdr.blocked_links.tqe_prev
12126 == NULL) ? "" : " BLOCKED",
12127 (xio->io_hdr.flags &
12128 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
12129 (xio->io_hdr.flags &
12130 CTL_FLAG_ABORT) ? " ABORT" : "",
12131 (xio->io_hdr.flags &
12132 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
12133 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
12135 printf("%s\n", sbuf_data(&sb));
12138 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12139 || (xio->io_hdr.nexus.initid.id != io->io_hdr.nexus.initid.id)
12140 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12144 * If the abort says that the task is untagged, the
12145 * task in the queue must be untagged. Otherwise,
12146 * we just check to see whether the tag numbers
12147 * match. This is because the QLogic firmware
12148 * doesn't pass back the tag type in an abort
12152 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12153 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12154 || (xio->scsiio.tag_num == io->taskio.tag_num))
12157 * XXX KDM we've got problems with FC, because it
12158 * doesn't send down a tag type with aborts. So we
12159 * can only really go by the tag number...
12160 * This may cause problems with parallel SCSI.
12161 * Need to figure that out!!
12163 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12164 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12166 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
12167 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12168 union ctl_ha_msg msg_info;
12170 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
12171 msg_info.hdr.nexus = io->io_hdr.nexus;
12172 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12173 msg_info.task.tag_num = io->taskio.tag_num;
12174 msg_info.task.tag_type = io->taskio.tag_type;
12175 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12176 msg_info.hdr.original_sc = NULL;
12177 msg_info.hdr.serializing_sc = NULL;
12179 printf("Sent Abort to other side\n");
12181 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12182 (void *)&msg_info, sizeof(msg_info), 0) !=
12183 CTL_HA_STATUS_SUCCESS) {
12187 printf("ctl_abort_task: found I/O to abort\n");
12191 mtx_unlock(&lun->lun_lock);
12195 * This isn't really an error. It's entirely possible for
12196 * the abort and command completion to cross on the wire.
12197 * This is more of an informative/diagnostic error.
12200 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
12201 "%d:%d:%d:%d tag %d type %d\n",
12202 io->io_hdr.nexus.initid.id,
12203 io->io_hdr.nexus.targ_port,
12204 io->io_hdr.nexus.targ_target.id,
12205 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
12206 io->taskio.tag_type);
12213 ctl_run_task(union ctl_io *io)
12215 struct ctl_softc *softc = control_softc;
12217 const char *task_desc;
12219 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12221 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12222 ("ctl_run_task: Unextected io_type %d\n",
12223 io->io_hdr.io_type));
12225 task_desc = ctl_scsi_task_string(&io->taskio);
12226 if (task_desc != NULL) {
12228 csevent_log(CSC_CTL | CSC_SHELF_SW |
12230 csevent_LogType_Trace,
12231 csevent_Severity_Information,
12232 csevent_AlertLevel_Green,
12233 csevent_FRU_Firmware,
12234 csevent_FRU_Unknown,
12235 "CTL: received task: %s",task_desc);
12239 csevent_log(CSC_CTL | CSC_SHELF_SW |
12241 csevent_LogType_Trace,
12242 csevent_Severity_Information,
12243 csevent_AlertLevel_Green,
12244 csevent_FRU_Firmware,
12245 csevent_FRU_Unknown,
12246 "CTL: received unknown task "
12248 io->taskio.task_action,
12249 io->taskio.task_action);
12252 switch (io->taskio.task_action) {
12253 case CTL_TASK_ABORT_TASK:
12254 retval = ctl_abort_task(io);
12256 case CTL_TASK_ABORT_TASK_SET:
12257 case CTL_TASK_CLEAR_TASK_SET:
12258 retval = ctl_abort_task_set(io);
12260 case CTL_TASK_CLEAR_ACA:
12262 case CTL_TASK_I_T_NEXUS_RESET:
12263 retval = ctl_i_t_nexus_reset(io);
12265 case CTL_TASK_LUN_RESET: {
12266 struct ctl_lun *lun;
12269 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12270 mtx_lock(&softc->ctl_lock);
12271 if ((targ_lun < CTL_MAX_LUNS)
12272 && (softc->ctl_luns[targ_lun] != NULL))
12273 lun = softc->ctl_luns[targ_lun];
12275 mtx_unlock(&softc->ctl_lock);
12280 if (!(io->io_hdr.flags &
12281 CTL_FLAG_FROM_OTHER_SC)) {
12282 union ctl_ha_msg msg_info;
12284 io->io_hdr.flags |=
12285 CTL_FLAG_SENT_2OTHER_SC;
12286 msg_info.hdr.msg_type =
12287 CTL_MSG_MANAGE_TASKS;
12288 msg_info.hdr.nexus = io->io_hdr.nexus;
12289 msg_info.task.task_action =
12290 CTL_TASK_LUN_RESET;
12291 msg_info.hdr.original_sc = NULL;
12292 msg_info.hdr.serializing_sc = NULL;
12293 if (CTL_HA_STATUS_SUCCESS !=
12294 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12296 sizeof(msg_info), 0)) {
12300 retval = ctl_lun_reset(lun, io,
12302 mtx_unlock(&softc->ctl_lock);
12305 case CTL_TASK_TARGET_RESET:
12306 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET);
12308 case CTL_TASK_BUS_RESET:
12309 retval = ctl_bus_reset(softc, io);
12311 case CTL_TASK_PORT_LOGIN:
12313 case CTL_TASK_PORT_LOGOUT:
12316 printf("ctl_run_task: got unknown task management event %d\n",
12317 io->taskio.task_action);
12321 io->io_hdr.status = CTL_SUCCESS;
12323 io->io_hdr.status = CTL_ERROR;
12328 * For HA operation. Handle commands that come in from the other
12332 ctl_handle_isc(union ctl_io *io)
12335 struct ctl_lun *lun;
12336 struct ctl_softc *softc;
12339 softc = control_softc;
12341 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12342 lun = softc->ctl_luns[targ_lun];
12344 switch (io->io_hdr.msg_type) {
12345 case CTL_MSG_SERIALIZE:
12346 free_io = ctl_serialize_other_sc_cmd(&io->scsiio);
12348 case CTL_MSG_R2R: {
12349 const struct ctl_cmd_entry *entry;
12352 * This is only used in SER_ONLY mode.
12355 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12356 mtx_lock(&lun->lun_lock);
12357 if (ctl_scsiio_lun_check(lun,
12358 entry, (struct ctl_scsiio *)io) != 0) {
12359 mtx_unlock(&lun->lun_lock);
12363 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12364 mtx_unlock(&lun->lun_lock);
12365 ctl_enqueue_rtr(io);
12368 case CTL_MSG_FINISH_IO:
12369 if (softc->ha_mode == CTL_HA_MODE_XFER) {
12374 mtx_lock(&lun->lun_lock);
12375 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
12377 ctl_check_blocked(lun);
12378 mtx_unlock(&lun->lun_lock);
12381 case CTL_MSG_PERS_ACTION:
12382 ctl_hndl_per_res_out_on_other_sc(
12383 (union ctl_ha_msg *)&io->presio.pr_msg);
12386 case CTL_MSG_BAD_JUJU:
12390 case CTL_MSG_DATAMOVE:
12391 /* Only used in XFER mode */
12393 ctl_datamove_remote(io);
12395 case CTL_MSG_DATAMOVE_DONE:
12396 /* Only used in XFER mode */
12398 io->scsiio.be_move_done(io);
12402 printf("%s: Invalid message type %d\n",
12403 __func__, io->io_hdr.msg_type);
12413 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12414 * there is no match.
12416 static ctl_lun_error_pattern
12417 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12419 const struct ctl_cmd_entry *entry;
12420 ctl_lun_error_pattern filtered_pattern, pattern;
12422 pattern = desc->error_pattern;
12425 * XXX KDM we need more data passed into this function to match a
12426 * custom pattern, and we actually need to implement custom pattern
12429 if (pattern & CTL_LUN_PAT_CMD)
12430 return (CTL_LUN_PAT_CMD);
12432 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12433 return (CTL_LUN_PAT_ANY);
12435 entry = ctl_get_cmd_entry(ctsio, NULL);
12437 filtered_pattern = entry->pattern & pattern;
12440 * If the user requested specific flags in the pattern (e.g.
12441 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12444 * If the user did not specify any flags, it doesn't matter whether
12445 * or not the command supports the flags.
12447 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12448 (pattern & ~CTL_LUN_PAT_MASK))
12449 return (CTL_LUN_PAT_NONE);
12452 * If the user asked for a range check, see if the requested LBA
12453 * range overlaps with this command's LBA range.
12455 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12461 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12463 return (CTL_LUN_PAT_NONE);
12465 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12466 desc->lba_range.len, FALSE);
12468 * A "pass" means that the LBA ranges don't overlap, so
12469 * this doesn't match the user's range criteria.
12471 if (action == CTL_ACTION_PASS)
12472 return (CTL_LUN_PAT_NONE);
12475 return (filtered_pattern);
12479 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12481 struct ctl_error_desc *desc, *desc2;
12483 mtx_assert(&lun->lun_lock, MA_OWNED);
12485 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12486 ctl_lun_error_pattern pattern;
12488 * Check to see whether this particular command matches
12489 * the pattern in the descriptor.
12491 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12492 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12495 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12496 case CTL_LUN_INJ_ABORTED:
12497 ctl_set_aborted(&io->scsiio);
12499 case CTL_LUN_INJ_MEDIUM_ERR:
12500 ctl_set_medium_error(&io->scsiio);
12502 case CTL_LUN_INJ_UA:
12503 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12505 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12507 case CTL_LUN_INJ_CUSTOM:
12509 * We're assuming the user knows what he is doing.
12510 * Just copy the sense information without doing
12513 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12514 MIN(sizeof(desc->custom_sense),
12515 sizeof(io->scsiio.sense_data)));
12516 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12517 io->scsiio.sense_len = SSD_FULL_SIZE;
12518 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12520 case CTL_LUN_INJ_NONE:
12523 * If this is an error injection type we don't know
12524 * about, clear the continuous flag (if it is set)
12525 * so it will get deleted below.
12527 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12531 * By default, each error injection action is a one-shot
12533 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12536 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12542 #ifdef CTL_IO_DELAY
12544 ctl_datamove_timer_wakeup(void *arg)
12548 io = (union ctl_io *)arg;
12552 #endif /* CTL_IO_DELAY */
12555 ctl_datamove(union ctl_io *io)
12557 void (*fe_datamove)(union ctl_io *io);
12559 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12561 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12564 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12569 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12570 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12572 sbuf_cat(&sb, path_str);
12573 switch (io->io_hdr.io_type) {
12575 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12576 sbuf_printf(&sb, "\n");
12577 sbuf_cat(&sb, path_str);
12578 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12579 io->scsiio.tag_num, io->scsiio.tag_type);
12582 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12583 "Tag Type: %d\n", io->taskio.task_action,
12584 io->taskio.tag_num, io->taskio.tag_type);
12587 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12588 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12591 sbuf_cat(&sb, path_str);
12592 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12593 (intmax_t)time_uptime - io->io_hdr.start_time);
12595 printf("%s", sbuf_data(&sb));
12597 #endif /* CTL_TIME_IO */
12599 #ifdef CTL_IO_DELAY
12600 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12601 struct ctl_lun *lun;
12603 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12605 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12607 struct ctl_lun *lun;
12609 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12611 && (lun->delay_info.datamove_delay > 0)) {
12612 struct callout *callout;
12614 callout = (struct callout *)&io->io_hdr.timer_bytes;
12615 callout_init(callout, /*mpsafe*/ 1);
12616 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12617 callout_reset(callout,
12618 lun->delay_info.datamove_delay * hz,
12619 ctl_datamove_timer_wakeup, io);
12620 if (lun->delay_info.datamove_type ==
12621 CTL_DELAY_TYPE_ONESHOT)
12622 lun->delay_info.datamove_delay = 0;
12629 * This command has been aborted. Set the port status, so we fail
12632 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12633 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n",
12634 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id,
12635 io->io_hdr.nexus.targ_port,
12636 (uintmax_t)io->io_hdr.nexus.targ_target.id,
12637 io->io_hdr.nexus.targ_lun);
12638 io->io_hdr.port_status = 31337;
12640 * Note that the backend, in this case, will get the
12641 * callback in its context. In other cases it may get
12642 * called in the frontend's interrupt thread context.
12644 io->scsiio.be_move_done(io);
12648 /* Don't confuse frontend with zero length data move. */
12649 if (io->scsiio.kern_data_len == 0) {
12650 io->scsiio.be_move_done(io);
12655 * If we're in XFER mode and this I/O is from the other shelf
12656 * controller, we need to send the DMA to the other side to
12657 * actually transfer the data to/from the host. In serialize only
12658 * mode the transfer happens below CTL and ctl_datamove() is only
12659 * called on the machine that originally received the I/O.
12661 if ((control_softc->ha_mode == CTL_HA_MODE_XFER)
12662 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12663 union ctl_ha_msg msg;
12664 uint32_t sg_entries_sent;
12668 memset(&msg, 0, sizeof(msg));
12669 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
12670 msg.hdr.original_sc = io->io_hdr.original_sc;
12671 msg.hdr.serializing_sc = io;
12672 msg.hdr.nexus = io->io_hdr.nexus;
12673 msg.dt.flags = io->io_hdr.flags;
12675 * We convert everything into a S/G list here. We can't
12676 * pass by reference, only by value between controllers.
12677 * So we can't pass a pointer to the S/G list, only as many
12678 * S/G entries as we can fit in here. If it's possible for
12679 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
12680 * then we need to break this up into multiple transfers.
12682 if (io->scsiio.kern_sg_entries == 0) {
12683 msg.dt.kern_sg_entries = 1;
12685 * If this is in cached memory, flush the cache
12686 * before we send the DMA request to the other
12687 * controller. We want to do this in either the
12688 * read or the write case. The read case is
12689 * straightforward. In the write case, we want to
12690 * make sure nothing is in the local cache that
12691 * could overwrite the DMAed data.
12693 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12695 * XXX KDM use bus_dmamap_sync() here.
12700 * Convert to a physical address if this is a
12703 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
12704 msg.dt.sg_list[0].addr =
12705 io->scsiio.kern_data_ptr;
12708 * XXX KDM use busdma here!
12711 msg.dt.sg_list[0].addr = (void *)
12712 vtophys(io->scsiio.kern_data_ptr);
12716 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
12719 struct ctl_sg_entry *sgl;
12722 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
12723 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
12724 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12726 * XXX KDM use bus_dmamap_sync() here.
12731 msg.dt.kern_data_len = io->scsiio.kern_data_len;
12732 msg.dt.kern_total_len = io->scsiio.kern_total_len;
12733 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
12734 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
12735 msg.dt.sg_sequence = 0;
12738 * Loop until we've sent all of the S/G entries. On the
12739 * other end, we'll recompose these S/G entries into one
12740 * contiguous list before passing it to the
12742 for (sg_entries_sent = 0; sg_entries_sent <
12743 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) {
12744 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list)/
12745 sizeof(msg.dt.sg_list[0])),
12746 msg.dt.kern_sg_entries - sg_entries_sent);
12748 if (do_sg_copy != 0) {
12749 struct ctl_sg_entry *sgl;
12752 sgl = (struct ctl_sg_entry *)
12753 io->scsiio.kern_data_ptr;
12755 * If this is in cached memory, flush the cache
12756 * before we send the DMA request to the other
12757 * controller. We want to do this in either
12758 * the * read or the write case. The read
12759 * case is straightforward. In the write
12760 * case, we want to make sure nothing is
12761 * in the local cache that could overwrite
12765 for (i = sg_entries_sent, j = 0;
12766 i < msg.dt.cur_sg_entries; i++, j++) {
12767 if ((io->io_hdr.flags &
12768 CTL_FLAG_NO_DATASYNC) == 0) {
12770 * XXX KDM use bus_dmamap_sync()
12773 if ((io->io_hdr.flags &
12774 CTL_FLAG_BUS_ADDR) == 0) {
12776 * XXX KDM use busdma.
12779 msg.dt.sg_list[j].addr =(void *)
12780 vtophys(sgl[i].addr);
12783 msg.dt.sg_list[j].addr =
12786 msg.dt.sg_list[j].len = sgl[i].len;
12790 sg_entries_sent += msg.dt.cur_sg_entries;
12791 if (sg_entries_sent >= msg.dt.kern_sg_entries)
12792 msg.dt.sg_last = 1;
12794 msg.dt.sg_last = 0;
12797 * XXX KDM drop and reacquire the lock here?
12799 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12800 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
12802 * XXX do something here.
12806 msg.dt.sent_sg_entries = sg_entries_sent;
12808 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12809 if (io->io_hdr.flags & CTL_FLAG_FAILOVER)
12810 ctl_failover_io(io, /*have_lock*/ 0);
12815 * Lookup the fe_datamove() function for this particular
12819 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12826 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12828 union ctl_ha_msg msg;
12831 memset(&msg, 0, sizeof(msg));
12833 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12834 msg.hdr.original_sc = io;
12835 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12836 msg.hdr.nexus = io->io_hdr.nexus;
12837 msg.hdr.status = io->io_hdr.status;
12838 msg.scsi.tag_num = io->scsiio.tag_num;
12839 msg.scsi.tag_type = io->scsiio.tag_type;
12840 msg.scsi.scsi_status = io->scsiio.scsi_status;
12841 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12842 sizeof(io->scsiio.sense_data));
12843 msg.scsi.sense_len = io->scsiio.sense_len;
12844 msg.scsi.sense_residual = io->scsiio.sense_residual;
12845 msg.scsi.fetd_status = io->io_hdr.port_status;
12846 msg.scsi.residual = io->scsiio.residual;
12847 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12849 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12850 ctl_failover_io(io, /*have_lock*/ have_lock);
12854 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0);
12855 if (isc_status > CTL_HA_STATUS_SUCCESS) {
12856 /* XXX do something if this fails */
12862 * The DMA to the remote side is done, now we need to tell the other side
12863 * we're done so it can continue with its data movement.
12866 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12872 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12873 printf("%s: ISC DMA write failed with error %d", __func__,
12875 ctl_set_internal_failure(&io->scsiio,
12877 /*retry_count*/ rq->ret);
12880 ctl_dt_req_free(rq);
12883 * In this case, we had to malloc the memory locally. Free it.
12885 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12887 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12888 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12891 * The data is in local and remote memory, so now we need to send
12892 * status (good or back) back to the other side.
12894 ctl_send_datamove_done(io, /*have_lock*/ 0);
12898 * We've moved the data from the host/controller into local memory. Now we
12899 * need to push it over to the remote controller's memory.
12902 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12908 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12909 ctl_datamove_remote_write_cb);
12915 ctl_datamove_remote_write(union ctl_io *io)
12918 void (*fe_datamove)(union ctl_io *io);
12921 * - Get the data from the host/HBA into local memory.
12922 * - DMA memory from the local controller to the remote controller.
12923 * - Send status back to the remote controller.
12926 retval = ctl_datamove_remote_sgl_setup(io);
12930 /* Switch the pointer over so the FETD knows what to do */
12931 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12934 * Use a custom move done callback, since we need to send completion
12935 * back to the other controller, not to the backend on this side.
12937 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12939 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12948 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12957 * In this case, we had to malloc the memory locally. Free it.
12959 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12961 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12962 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12966 scsi_path_string(io, path_str, sizeof(path_str));
12967 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12968 sbuf_cat(&sb, path_str);
12969 scsi_command_string(&io->scsiio, NULL, &sb);
12970 sbuf_printf(&sb, "\n");
12971 sbuf_cat(&sb, path_str);
12972 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12973 io->scsiio.tag_num, io->scsiio.tag_type);
12974 sbuf_cat(&sb, path_str);
12975 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12976 io->io_hdr.flags, io->io_hdr.status);
12978 printk("%s", sbuf_data(&sb));
12983 * The read is done, now we need to send status (good or bad) back
12984 * to the other side.
12986 ctl_send_datamove_done(io, /*have_lock*/ 0);
12992 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12995 void (*fe_datamove)(union ctl_io *io);
12999 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
13000 printf("%s: ISC DMA read failed with error %d", __func__,
13002 ctl_set_internal_failure(&io->scsiio,
13004 /*retry_count*/ rq->ret);
13007 ctl_dt_req_free(rq);
13009 /* Switch the pointer over so the FETD knows what to do */
13010 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
13013 * Use a custom move done callback, since we need to send completion
13014 * back to the other controller, not to the backend on this side.
13016 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
13018 /* XXX KDM add checks like the ones in ctl_datamove? */
13020 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
13026 ctl_datamove_remote_sgl_setup(union ctl_io *io)
13028 struct ctl_sg_entry *local_sglist, *remote_sglist;
13029 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist;
13030 struct ctl_softc *softc;
13035 softc = control_softc;
13037 local_sglist = io->io_hdr.local_sglist;
13038 local_dma_sglist = io->io_hdr.local_dma_sglist;
13039 remote_sglist = io->io_hdr.remote_sglist;
13040 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
13042 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) {
13043 for (i = 0; i < io->scsiio.kern_sg_entries; i++) {
13044 local_sglist[i].len = remote_sglist[i].len;
13047 * XXX Detect the situation where the RS-level I/O
13048 * redirector on the other side has already read the
13049 * data off of the AOR RS on this side, and
13050 * transferred it to remote (mirror) memory on the
13051 * other side. Since we already have the data in
13052 * memory here, we just need to use it.
13054 * XXX KDM this can probably be removed once we
13055 * get the cache device code in and take the
13056 * current AOR implementation out.
13059 if ((remote_sglist[i].addr >=
13060 (void *)vtophys(softc->mirr->addr))
13061 && (remote_sglist[i].addr <
13062 ((void *)vtophys(softc->mirr->addr) +
13063 CacheMirrorOffset))) {
13064 local_sglist[i].addr = remote_sglist[i].addr -
13066 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13068 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE;
13070 local_sglist[i].addr = remote_sglist[i].addr +
13075 printf("%s: local %p, remote %p, len %d\n",
13076 __func__, local_sglist[i].addr,
13077 remote_sglist[i].addr, local_sglist[i].len);
13081 uint32_t len_to_go;
13084 * In this case, we don't have automatically allocated
13085 * memory for this I/O on this controller. This typically
13086 * happens with internal CTL I/O -- e.g. inquiry, mode
13087 * sense, etc. Anything coming from RAIDCore will have
13088 * a mirror area available.
13090 len_to_go = io->scsiio.kern_data_len;
13093 * Clear the no datasync flag, we have to use malloced
13096 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC;
13099 * The difficult thing here is that the size of the various
13100 * S/G segments may be different than the size from the
13101 * remote controller. That'll make it harder when DMAing
13102 * the data back to the other side.
13104 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) /
13105 sizeof(io->io_hdr.remote_sglist[0])) &&
13106 (len_to_go > 0); i++) {
13107 local_sglist[i].len = MIN(len_to_go, 131072);
13108 CTL_SIZE_8B(local_dma_sglist[i].len,
13109 local_sglist[i].len);
13110 local_sglist[i].addr =
13111 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK);
13113 local_dma_sglist[i].addr = local_sglist[i].addr;
13115 if (local_sglist[i].addr == NULL) {
13118 printf("malloc failed for %zd bytes!",
13119 local_dma_sglist[i].len);
13120 for (j = 0; j < i; j++) {
13121 free(local_sglist[j].addr, M_CTL);
13123 ctl_set_internal_failure(&io->scsiio,
13125 /*retry_count*/ 4857);
13127 goto bailout_error;
13130 /* XXX KDM do we need a sync here? */
13132 len_to_go -= local_sglist[i].len;
13135 * Reset the number of S/G entries accordingly. The
13136 * original number of S/G entries is available in
13139 io->scsiio.kern_sg_entries = i;
13142 printf("%s: kern_sg_entries = %d\n", __func__,
13143 io->scsiio.kern_sg_entries);
13144 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13145 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i,
13146 local_sglist[i].addr, local_sglist[i].len,
13147 local_dma_sglist[i].len);
13156 ctl_send_datamove_done(io, /*have_lock*/ 0);
13162 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
13163 ctl_ha_dt_cb callback)
13165 struct ctl_ha_dt_req *rq;
13166 struct ctl_sg_entry *remote_sglist, *local_sglist;
13167 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist;
13168 uint32_t local_used, remote_used, total_used;
13174 rq = ctl_dt_req_alloc();
13177 * If we failed to allocate the request, and if the DMA didn't fail
13178 * anyway, set busy status. This is just a resource allocation
13182 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE))
13183 ctl_set_busy(&io->scsiio);
13185 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
13188 ctl_dt_req_free(rq);
13191 * The data move failed. We need to return status back
13192 * to the other controller. No point in trying to DMA
13193 * data to the remote controller.
13196 ctl_send_datamove_done(io, /*have_lock*/ 0);
13203 local_sglist = io->io_hdr.local_sglist;
13204 local_dma_sglist = io->io_hdr.local_dma_sglist;
13205 remote_sglist = io->io_hdr.remote_sglist;
13206 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
13211 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) {
13212 rq->ret = CTL_HA_STATUS_SUCCESS;
13219 * Pull/push the data over the wire from/to the other controller.
13220 * This takes into account the possibility that the local and
13221 * remote sglists may not be identical in terms of the size of
13222 * the elements and the number of elements.
13224 * One fundamental assumption here is that the length allocated for
13225 * both the local and remote sglists is identical. Otherwise, we've
13226 * essentially got a coding error of some sort.
13228 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
13230 uint32_t cur_len, dma_length;
13233 rq->id = CTL_HA_DATA_CTL;
13234 rq->command = command;
13238 * Both pointers should be aligned. But it is possible
13239 * that the allocation length is not. They should both
13240 * also have enough slack left over at the end, though,
13241 * to round up to the next 8 byte boundary.
13243 cur_len = MIN(local_sglist[i].len - local_used,
13244 remote_sglist[j].len - remote_used);
13247 * In this case, we have a size issue and need to decrease
13248 * the size, except in the case where we actually have less
13249 * than 8 bytes left. In that case, we need to increase
13250 * the DMA length to get the last bit.
13252 if ((cur_len & 0x7) != 0) {
13253 if (cur_len > 0x7) {
13254 cur_len = cur_len - (cur_len & 0x7);
13255 dma_length = cur_len;
13257 CTL_SIZE_8B(dma_length, cur_len);
13261 dma_length = cur_len;
13264 * If we had to allocate memory for this I/O, instead of using
13265 * the non-cached mirror memory, we'll need to flush the cache
13266 * before trying to DMA to the other controller.
13268 * We could end up doing this multiple times for the same
13269 * segment if we have a larger local segment than remote
13270 * segment. That shouldn't be an issue.
13272 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
13274 * XXX KDM use bus_dmamap_sync() here.
13278 rq->size = dma_length;
13280 tmp_ptr = (uint8_t *)local_sglist[i].addr;
13281 tmp_ptr += local_used;
13283 /* Use physical addresses when talking to ISC hardware */
13284 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
13285 /* XXX KDM use busdma */
13287 rq->local = vtophys(tmp_ptr);
13290 rq->local = tmp_ptr;
13292 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
13293 tmp_ptr += remote_used;
13294 rq->remote = tmp_ptr;
13296 rq->callback = NULL;
13298 local_used += cur_len;
13299 if (local_used >= local_sglist[i].len) {
13304 remote_used += cur_len;
13305 if (remote_used >= remote_sglist[j].len) {
13309 total_used += cur_len;
13311 if (total_used >= io->scsiio.kern_data_len)
13312 rq->callback = callback;
13314 if ((rq->size & 0x7) != 0) {
13315 printf("%s: warning: size %d is not on 8b boundary\n",
13316 __func__, rq->size);
13318 if (((uintptr_t)rq->local & 0x7) != 0) {
13319 printf("%s: warning: local %p not on 8b boundary\n",
13320 __func__, rq->local);
13322 if (((uintptr_t)rq->remote & 0x7) != 0) {
13323 printf("%s: warning: remote %p not on 8b boundary\n",
13324 __func__, rq->local);
13327 printf("%s: %s: local %#x remote %#x size %d\n", __func__,
13328 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
13329 rq->local, rq->remote, rq->size);
13332 isc_ret = ctl_dt_single(rq);
13333 if (isc_ret == CTL_HA_STATUS_WAIT)
13336 if (isc_ret == CTL_HA_STATUS_DISCONNECT) {
13337 rq->ret = CTL_HA_STATUS_SUCCESS;
13351 ctl_datamove_remote_read(union ctl_io *io)
13357 * This will send an error to the other controller in the case of a
13360 retval = ctl_datamove_remote_sgl_setup(io);
13364 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
13365 ctl_datamove_remote_read_cb);
13367 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) {
13369 * Make sure we free memory if there was an error.. The
13370 * ctl_datamove_remote_xfer() function will send the
13371 * datamove done message, or call the callback with an
13372 * error if there is a problem.
13374 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13375 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13382 * Process a datamove request from the other controller. This is used for
13383 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
13384 * first. Once that is complete, the data gets DMAed into the remote
13385 * controller's memory. For reads, we DMA from the remote controller's
13386 * memory into our memory first, and then move it out to the FETD.
13389 ctl_datamove_remote(union ctl_io *io)
13391 struct ctl_softc *softc;
13393 softc = control_softc;
13395 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
13398 * Note that we look for an aborted I/O here, but don't do some of
13399 * the other checks that ctl_datamove() normally does.
13400 * We don't need to run the datamove delay code, since that should
13401 * have been done if need be on the other controller.
13403 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
13404 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__,
13405 io->scsiio.tag_num, io->io_hdr.nexus.initid.id,
13406 io->io_hdr.nexus.targ_port,
13407 io->io_hdr.nexus.targ_target.id,
13408 io->io_hdr.nexus.targ_lun);
13409 io->io_hdr.port_status = 31338;
13410 ctl_send_datamove_done(io, /*have_lock*/ 0);
13414 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) {
13415 ctl_datamove_remote_write(io);
13416 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){
13417 ctl_datamove_remote_read(io);
13419 union ctl_ha_msg msg;
13420 struct scsi_sense_data *sense;
13424 memset(&msg, 0, sizeof(msg));
13426 msg.hdr.msg_type = CTL_MSG_BAD_JUJU;
13427 msg.hdr.status = CTL_SCSI_ERROR;
13428 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
13430 retry_count = 4243;
13432 sense = &msg.scsi.sense_data;
13433 sks[0] = SSD_SCS_VALID;
13434 sks[1] = (retry_count >> 8) & 0xff;
13435 sks[2] = retry_count & 0xff;
13437 /* "Internal target failure" */
13438 scsi_set_sense_data(sense,
13439 /*sense_format*/ SSD_TYPE_NONE,
13440 /*current_error*/ 1,
13441 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
13444 /*type*/ SSD_ELEM_SKS,
13445 /*size*/ sizeof(sks),
13449 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
13450 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
13451 ctl_failover_io(io, /*have_lock*/ 1);
13455 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) >
13456 CTL_HA_STATUS_SUCCESS) {
13457 /* XXX KDM what to do if this fails? */
13465 ctl_process_done(union ctl_io *io)
13467 struct ctl_lun *lun;
13468 struct ctl_softc *softc = control_softc;
13469 void (*fe_done)(union ctl_io *io);
13470 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port);
13472 CTL_DEBUG_PRINT(("ctl_process_done\n"));
13474 fe_done = softc->ctl_ports[targ_port]->fe_done;
13477 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
13482 ctl_scsi_path_string(io, path_str, sizeof(path_str));
13483 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13485 sbuf_cat(&sb, path_str);
13486 switch (io->io_hdr.io_type) {
13488 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
13489 sbuf_printf(&sb, "\n");
13490 sbuf_cat(&sb, path_str);
13491 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13492 io->scsiio.tag_num, io->scsiio.tag_type);
13495 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
13496 "Tag Type: %d\n", io->taskio.task_action,
13497 io->taskio.tag_num, io->taskio.tag_type);
13500 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13501 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13504 sbuf_cat(&sb, path_str);
13505 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
13506 (intmax_t)time_uptime - io->io_hdr.start_time);
13508 printf("%s", sbuf_data(&sb));
13510 #endif /* CTL_TIME_IO */
13512 switch (io->io_hdr.io_type) {
13516 if (ctl_debug & CTL_DEBUG_INFO)
13517 ctl_io_error_print(io, NULL);
13518 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
13522 return (CTL_RETVAL_COMPLETE);
13524 panic("ctl_process_done: invalid io type %d\n",
13525 io->io_hdr.io_type);
13526 break; /* NOTREACHED */
13529 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13531 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13532 io->io_hdr.nexus.targ_mapped_lun));
13536 mtx_lock(&lun->lun_lock);
13539 * Check to see if we have any errors to inject here. We only
13540 * inject errors for commands that don't already have errors set.
13542 if ((STAILQ_FIRST(&lun->error_list) != NULL) &&
13543 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
13544 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
13545 ctl_inject_error(lun, io);
13548 * XXX KDM how do we treat commands that aren't completed
13551 * XXX KDM should we also track I/O latency?
13553 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13554 io->io_hdr.io_type == CTL_IO_SCSI) {
13556 struct bintime cur_bt;
13560 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13562 type = CTL_STATS_READ;
13563 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13565 type = CTL_STATS_WRITE;
13567 type = CTL_STATS_NO_IO;
13569 lun->stats.ports[targ_port].bytes[type] +=
13570 io->scsiio.kern_total_len;
13571 lun->stats.ports[targ_port].operations[type]++;
13573 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
13574 &io->io_hdr.dma_bt);
13575 lun->stats.ports[targ_port].num_dmas[type] +=
13576 io->io_hdr.num_dmas;
13577 getbintime(&cur_bt);
13578 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
13579 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
13584 * Remove this from the OOA queue.
13586 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13588 if (TAILQ_EMPTY(&lun->ooa_queue))
13589 lun->last_busy = getsbinuptime();
13593 * Run through the blocked queue on this LUN and see if anything
13594 * has become unblocked, now that this transaction is done.
13596 ctl_check_blocked(lun);
13599 * If the LUN has been invalidated, free it if there is nothing
13600 * left on its OOA queue.
13602 if ((lun->flags & CTL_LUN_INVALID)
13603 && TAILQ_EMPTY(&lun->ooa_queue)) {
13604 mtx_unlock(&lun->lun_lock);
13605 mtx_lock(&softc->ctl_lock);
13607 mtx_unlock(&softc->ctl_lock);
13609 mtx_unlock(&lun->lun_lock);
13614 * If this command has been aborted, make sure we set the status
13615 * properly. The FETD is responsible for freeing the I/O and doing
13616 * whatever it needs to do to clean up its state.
13618 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13619 ctl_set_task_aborted(&io->scsiio);
13622 * If enabled, print command error status.
13624 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
13625 (ctl_debug & CTL_DEBUG_INFO) != 0)
13626 ctl_io_error_print(io, NULL);
13629 * Tell the FETD or the other shelf controller we're done with this
13630 * command. Note that only SCSI commands get to this point. Task
13631 * management commands are completed above.
13633 * We only send status to the other controller if we're in XFER
13634 * mode. In SER_ONLY mode, the I/O is done on the controller that
13635 * received the I/O (from CTL's perspective), and so the status is
13638 * XXX KDM if we hold the lock here, we could cause a deadlock
13639 * if the frontend comes back in in this context to queue
13642 if ((softc->ha_mode == CTL_HA_MODE_XFER)
13643 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
13644 union ctl_ha_msg msg;
13646 memset(&msg, 0, sizeof(msg));
13647 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13648 msg.hdr.original_sc = io->io_hdr.original_sc;
13649 msg.hdr.nexus = io->io_hdr.nexus;
13650 msg.hdr.status = io->io_hdr.status;
13651 msg.scsi.scsi_status = io->scsiio.scsi_status;
13652 msg.scsi.tag_num = io->scsiio.tag_num;
13653 msg.scsi.tag_type = io->scsiio.tag_type;
13654 msg.scsi.sense_len = io->scsiio.sense_len;
13655 msg.scsi.sense_residual = io->scsiio.sense_residual;
13656 msg.scsi.residual = io->scsiio.residual;
13657 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
13658 sizeof(io->scsiio.sense_data));
13660 * We copy this whether or not this is an I/O-related
13661 * command. Otherwise, we'd have to go and check to see
13662 * whether it's a read/write command, and it really isn't
13665 memcpy(&msg.scsi.lbalen,
13666 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
13667 sizeof(msg.scsi.lbalen));
13669 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13670 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
13671 /* XXX do something here */
13678 return (CTL_RETVAL_COMPLETE);
13683 * Front end should call this if it doesn't do autosense. When the request
13684 * sense comes back in from the initiator, we'll dequeue this and send it.
13687 ctl_queue_sense(union ctl_io *io)
13689 struct ctl_lun *lun;
13690 struct ctl_port *port;
13691 struct ctl_softc *softc;
13692 uint32_t initidx, targ_lun;
13694 softc = control_softc;
13696 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13699 * LUN lookup will likely move to the ctl_work_thread() once we
13700 * have our new queueing infrastructure (that doesn't put things on
13701 * a per-LUN queue initially). That is so that we can handle
13702 * things like an INQUIRY to a LUN that we don't have enabled. We
13703 * can't deal with that right now.
13705 mtx_lock(&softc->ctl_lock);
13708 * If we don't have a LUN for this, just toss the sense
13711 port = ctl_io_port(&ctsio->io_hdr);
13712 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13713 if ((targ_lun < CTL_MAX_LUNS)
13714 && (softc->ctl_luns[targ_lun] != NULL))
13715 lun = softc->ctl_luns[targ_lun];
13719 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13721 mtx_lock(&lun->lun_lock);
13723 * Already have CA set for this LUN...toss the sense information.
13725 if (ctl_is_set(lun->have_ca, initidx)) {
13726 mtx_unlock(&lun->lun_lock);
13730 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data,
13731 MIN(sizeof(lun->pending_sense[initidx]),
13732 sizeof(io->scsiio.sense_data)));
13733 ctl_set_mask(lun->have_ca, initidx);
13734 mtx_unlock(&lun->lun_lock);
13737 mtx_unlock(&softc->ctl_lock);
13741 return (CTL_RETVAL_COMPLETE);
13746 * Primary command inlet from frontend ports. All SCSI and task I/O
13747 * requests must go through this function.
13750 ctl_queue(union ctl_io *io)
13752 struct ctl_port *port;
13754 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13757 io->io_hdr.start_time = time_uptime;
13758 getbintime(&io->io_hdr.start_bt);
13759 #endif /* CTL_TIME_IO */
13761 /* Map FE-specific LUN ID into global one. */
13762 port = ctl_io_port(&io->io_hdr);
13763 io->io_hdr.nexus.targ_mapped_lun =
13764 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13766 switch (io->io_hdr.io_type) {
13769 if (ctl_debug & CTL_DEBUG_CDB)
13771 ctl_enqueue_incoming(io);
13774 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13778 return (CTL_RETVAL_COMPLETE);
13781 #ifdef CTL_IO_DELAY
13783 ctl_done_timer_wakeup(void *arg)
13787 io = (union ctl_io *)arg;
13790 #endif /* CTL_IO_DELAY */
13793 ctl_done(union ctl_io *io)
13797 * Enable this to catch duplicate completion issues.
13800 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13801 printf("%s: type %d msg %d cdb %x iptl: "
13802 "%d:%d:%d:%d tag 0x%04x "
13803 "flag %#x status %x\n",
13805 io->io_hdr.io_type,
13806 io->io_hdr.msg_type,
13808 io->io_hdr.nexus.initid.id,
13809 io->io_hdr.nexus.targ_port,
13810 io->io_hdr.nexus.targ_target.id,
13811 io->io_hdr.nexus.targ_lun,
13812 (io->io_hdr.io_type ==
13814 io->taskio.tag_num :
13815 io->scsiio.tag_num,
13817 io->io_hdr.status);
13819 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13823 * This is an internal copy of an I/O, and should not go through
13824 * the normal done processing logic.
13826 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13830 * We need to send a msg to the serializing shelf to finish the IO
13831 * as well. We don't send a finish message to the other shelf if
13832 * this is a task management command. Task management commands
13833 * aren't serialized in the OOA queue, but rather just executed on
13834 * both shelf controllers for commands that originated on that
13837 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)
13838 && (io->io_hdr.io_type != CTL_IO_TASK)) {
13839 union ctl_ha_msg msg_io;
13841 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO;
13842 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc;
13843 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io,
13844 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) {
13846 /* continue on to finish IO */
13848 #ifdef CTL_IO_DELAY
13849 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13850 struct ctl_lun *lun;
13852 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13854 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13856 struct ctl_lun *lun;
13858 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13861 && (lun->delay_info.done_delay > 0)) {
13862 struct callout *callout;
13864 callout = (struct callout *)&io->io_hdr.timer_bytes;
13865 callout_init(callout, /*mpsafe*/ 1);
13866 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13867 callout_reset(callout,
13868 lun->delay_info.done_delay * hz,
13869 ctl_done_timer_wakeup, io);
13870 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13871 lun->delay_info.done_delay = 0;
13875 #endif /* CTL_IO_DELAY */
13877 ctl_enqueue_done(io);
13881 ctl_isc(struct ctl_scsiio *ctsio)
13883 struct ctl_lun *lun;
13886 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13888 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0]));
13890 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n"));
13892 retval = lun->backend->data_submit((union ctl_io *)ctsio);
13899 ctl_work_thread(void *arg)
13901 struct ctl_thread *thr = (struct ctl_thread *)arg;
13902 struct ctl_softc *softc = thr->ctl_softc;
13906 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13912 * We handle the queues in this order:
13914 * - done queue (to free up resources, unblock other commands)
13918 * If those queues are empty, we break out of the loop and
13921 mtx_lock(&thr->queue_lock);
13922 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13924 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13925 mtx_unlock(&thr->queue_lock);
13926 ctl_handle_isc(io);
13929 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13931 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13932 /* clear any blocked commands, call fe_done */
13933 mtx_unlock(&thr->queue_lock);
13934 retval = ctl_process_done(io);
13937 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13939 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13940 mtx_unlock(&thr->queue_lock);
13941 if (io->io_hdr.io_type == CTL_IO_TASK)
13944 ctl_scsiio_precheck(softc, &io->scsiio);
13947 if (!ctl_pause_rtr) {
13948 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13950 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13951 mtx_unlock(&thr->queue_lock);
13952 retval = ctl_scsiio(&io->scsiio);
13953 if (retval != CTL_RETVAL_COMPLETE)
13954 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13959 /* Sleep until we have something to do. */
13960 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13965 ctl_lun_thread(void *arg)
13967 struct ctl_softc *softc = (struct ctl_softc *)arg;
13968 struct ctl_be_lun *be_lun;
13971 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13975 mtx_lock(&softc->ctl_lock);
13976 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13977 if (be_lun != NULL) {
13978 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13979 mtx_unlock(&softc->ctl_lock);
13980 ctl_create_lun(be_lun);
13984 /* Sleep until we have something to do. */
13985 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13986 PDROP | PRIBIO, "-", 0);
13991 ctl_thresh_thread(void *arg)
13993 struct ctl_softc *softc = (struct ctl_softc *)arg;
13994 struct ctl_lun *lun;
13995 struct ctl_be_lun *be_lun;
13996 struct scsi_da_rw_recovery_page *rwpage;
13997 struct ctl_logical_block_provisioning_page *page;
13999 uint64_t thres, val;
14002 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
14005 mtx_lock(&softc->ctl_lock);
14006 STAILQ_FOREACH(lun, &softc->lun_list, links) {
14007 be_lun = lun->be_lun;
14008 if ((lun->flags & CTL_LUN_DISABLED) ||
14009 (lun->flags & CTL_LUN_OFFLINE) ||
14010 lun->backend->lun_attr == NULL)
14012 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT];
14013 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0)
14016 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT];
14017 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
14018 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
14020 thres = scsi_4btoul(page->descr[i].count);
14021 thres <<= CTL_LBP_EXPONENT;
14022 switch (page->descr[i].resource) {
14024 attr = "blocksavail";
14027 attr = "blocksused";
14030 attr = "poolblocksavail";
14033 attr = "poolblocksused";
14038 mtx_unlock(&softc->ctl_lock); // XXX
14039 val = lun->backend->lun_attr(
14040 lun->be_lun->be_lun, attr);
14041 mtx_lock(&softc->ctl_lock);
14042 if (val == UINT64_MAX)
14044 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
14045 == SLBPPD_ARMING_INC)
14046 e |= (val >= thres);
14048 e |= (val <= thres);
14050 mtx_lock(&lun->lun_lock);
14052 if (lun->lasttpt == 0 ||
14053 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
14054 lun->lasttpt = time_uptime;
14055 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
14059 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
14061 mtx_unlock(&lun->lun_lock);
14063 mtx_unlock(&softc->ctl_lock);
14064 pause("-", CTL_LBP_PERIOD * hz);
14069 ctl_enqueue_incoming(union ctl_io *io)
14071 struct ctl_softc *softc = control_softc;
14072 struct ctl_thread *thr;
14075 idx = (io->io_hdr.nexus.targ_port * 127 +
14076 io->io_hdr.nexus.initid.id) % worker_threads;
14077 thr = &softc->threads[idx];
14078 mtx_lock(&thr->queue_lock);
14079 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
14080 mtx_unlock(&thr->queue_lock);
14085 ctl_enqueue_rtr(union ctl_io *io)
14087 struct ctl_softc *softc = control_softc;
14088 struct ctl_thread *thr;
14090 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14091 mtx_lock(&thr->queue_lock);
14092 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
14093 mtx_unlock(&thr->queue_lock);
14098 ctl_enqueue_done(union ctl_io *io)
14100 struct ctl_softc *softc = control_softc;
14101 struct ctl_thread *thr;
14103 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14104 mtx_lock(&thr->queue_lock);
14105 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
14106 mtx_unlock(&thr->queue_lock);
14112 ctl_enqueue_isc(union ctl_io *io)
14114 struct ctl_softc *softc = control_softc;
14115 struct ctl_thread *thr;
14117 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14118 mtx_lock(&thr->queue_lock);
14119 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
14120 mtx_unlock(&thr->queue_lock);
14124 /* Initialization and failover */
14127 ctl_init_isc_msg(void)
14129 printf("CTL: Still calling this thing\n");
14134 * Initializes component into configuration defined by bootMode
14136 * returns hasc_Status:
14138 * ERROR - fatal error
14140 static ctl_ha_comp_status
14141 ctl_isc_init(struct ctl_ha_component *c)
14143 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
14150 * Starts component in state requested. If component starts successfully,
14151 * it must set its own state to the requestrd state
14152 * When requested state is HASC_STATE_HA, the component may refine it
14153 * by adding _SLAVE or _MASTER flags.
14154 * Currently allowed state transitions are:
14155 * UNKNOWN->HA - initial startup
14156 * UNKNOWN->SINGLE - initial startup when no parter detected
14157 * HA->SINGLE - failover
14158 * returns ctl_ha_comp_status:
14159 * OK - component successfully started in requested state
14160 * FAILED - could not start the requested state, failover may
14162 * ERROR - fatal error detected, no future startup possible
14164 static ctl_ha_comp_status
14165 ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state)
14167 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
14169 printf("%s: go\n", __func__);
14171 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap)
14172 if (c->state == CTL_HA_STATE_UNKNOWN ) {
14173 control_softc->is_single = 0;
14174 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
14175 != CTL_HA_STATUS_SUCCESS) {
14176 printf("ctl_isc_start: ctl_ha_msg_create failed.\n");
14177 ret = CTL_HA_COMP_STATUS_ERROR;
14179 } else if (CTL_HA_STATE_IS_HA(c->state)
14180 && CTL_HA_STATE_IS_SINGLE(state)){
14181 // HA->SINGLE transition
14183 control_softc->is_single = 1;
14185 printf("ctl_isc_start:Invalid state transition %X->%X\n",
14187 ret = CTL_HA_COMP_STATUS_ERROR;
14189 if (CTL_HA_STATE_IS_SINGLE(state))
14190 control_softc->is_single = 1;
14198 * Quiesce component
14199 * The component must clear any error conditions (set status to OK) and
14200 * prepare itself to another Start call
14201 * returns ctl_ha_comp_status:
14205 static ctl_ha_comp_status
14206 ctl_isc_quiesce(struct ctl_ha_component *c)
14208 int ret = CTL_HA_COMP_STATUS_OK;
14215 struct ctl_ha_component ctl_ha_component_ctlisc =
14218 .state = CTL_HA_STATE_UNKNOWN,
14219 .init = ctl_isc_init,
14220 .start = ctl_isc_start,
14221 .quiesce = ctl_isc_quiesce