2 * Copyright (c) 2003-2009 Silicon Graphics International Corp.
3 * Copyright (c) 2012 The FreeBSD Foundation
4 * Copyright (c) 2015 Alexander Motin <mav@FreeBSD.org>
7 * Portions of this software were developed by Edward Tomasz Napierala
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions, and the following disclaimer,
15 * without modification.
16 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17 * substantially similar to the "NO WARRANTY" disclaimer below
18 * ("Disclaimer") and any redistribution must be conditioned upon
19 * including a substantially similar Disclaimer requirement for further
20 * binary redistribution.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGES.
38 * CAM Target Layer, a SCSI device emulation subsystem.
40 * Author: Ken Merry <ken@FreeBSD.org>
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/ctype.h>
51 #include <sys/kernel.h>
52 #include <sys/types.h>
53 #include <sys/kthread.h>
55 #include <sys/fcntl.h>
57 #include <sys/module.h>
58 #include <sys/mutex.h>
59 #include <sys/condvar.h>
60 #include <sys/malloc.h>
62 #include <sys/ioccom.h>
63 #include <sys/queue.h>
66 #include <sys/endian.h>
67 #include <sys/sysctl.h>
71 #include <cam/scsi/scsi_all.h>
72 #include <cam/scsi/scsi_da.h>
73 #include <cam/ctl/ctl_io.h>
74 #include <cam/ctl/ctl.h>
75 #include <cam/ctl/ctl_frontend.h>
76 #include <cam/ctl/ctl_util.h>
77 #include <cam/ctl/ctl_backend.h>
78 #include <cam/ctl/ctl_ioctl.h>
79 #include <cam/ctl/ctl_ha.h>
80 #include <cam/ctl/ctl_private.h>
81 #include <cam/ctl/ctl_debug.h>
82 #include <cam/ctl/ctl_scsi_all.h>
83 #include <cam/ctl/ctl_error.h>
85 struct ctl_softc *control_softc = NULL;
88 * Template mode pages.
92 * Note that these are default values only. The actual values will be
93 * filled in when the user does a mode sense.
95 const static struct copan_debugconf_subpage debugconf_page_default = {
96 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
97 DBGCNF_SUBPAGE_CODE, /* subpage */
98 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
99 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
100 DBGCNF_VERSION, /* page_version */
101 {CTL_TIME_IO_DEFAULT_SECS>>8,
102 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */
105 const static struct copan_debugconf_subpage debugconf_page_changeable = {
106 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
107 DBGCNF_SUBPAGE_CODE, /* subpage */
108 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
109 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
110 0, /* page_version */
111 {0xff,0xff}, /* ctl_time_io_secs */
114 const static struct scsi_da_rw_recovery_page rw_er_page_default = {
115 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
116 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
117 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE,
118 /*read_retry_count*/0,
119 /*correction_span*/0,
120 /*head_offset_count*/0,
121 /*data_strobe_offset_cnt*/0,
122 /*byte8*/SMS_RWER_LBPERE,
123 /*write_retry_count*/0,
125 /*recovery_time_limit*/{0, 0},
128 const static struct scsi_da_rw_recovery_page rw_er_page_changeable = {
129 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
130 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
132 /*read_retry_count*/0,
133 /*correction_span*/0,
134 /*head_offset_count*/0,
135 /*data_strobe_offset_cnt*/0,
137 /*write_retry_count*/0,
139 /*recovery_time_limit*/{0, 0},
142 const static struct scsi_format_page format_page_default = {
143 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
144 /*page_length*/sizeof(struct scsi_format_page) - 2,
145 /*tracks_per_zone*/ {0, 0},
146 /*alt_sectors_per_zone*/ {0, 0},
147 /*alt_tracks_per_zone*/ {0, 0},
148 /*alt_tracks_per_lun*/ {0, 0},
149 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
150 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
151 /*bytes_per_sector*/ {0, 0},
152 /*interleave*/ {0, 0},
153 /*track_skew*/ {0, 0},
154 /*cylinder_skew*/ {0, 0},
156 /*reserved*/ {0, 0, 0}
159 const static struct scsi_format_page format_page_changeable = {
160 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
161 /*page_length*/sizeof(struct scsi_format_page) - 2,
162 /*tracks_per_zone*/ {0, 0},
163 /*alt_sectors_per_zone*/ {0, 0},
164 /*alt_tracks_per_zone*/ {0, 0},
165 /*alt_tracks_per_lun*/ {0, 0},
166 /*sectors_per_track*/ {0, 0},
167 /*bytes_per_sector*/ {0, 0},
168 /*interleave*/ {0, 0},
169 /*track_skew*/ {0, 0},
170 /*cylinder_skew*/ {0, 0},
172 /*reserved*/ {0, 0, 0}
175 const static struct scsi_rigid_disk_page rigid_disk_page_default = {
176 /*page_code*/SMS_RIGID_DISK_PAGE,
177 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
178 /*cylinders*/ {0, 0, 0},
179 /*heads*/ CTL_DEFAULT_HEADS,
180 /*start_write_precomp*/ {0, 0, 0},
181 /*start_reduced_current*/ {0, 0, 0},
182 /*step_rate*/ {0, 0},
183 /*landing_zone_cylinder*/ {0, 0, 0},
184 /*rpl*/ SRDP_RPL_DISABLED,
185 /*rotational_offset*/ 0,
187 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
188 CTL_DEFAULT_ROTATION_RATE & 0xff},
192 const static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
193 /*page_code*/SMS_RIGID_DISK_PAGE,
194 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
195 /*cylinders*/ {0, 0, 0},
197 /*start_write_precomp*/ {0, 0, 0},
198 /*start_reduced_current*/ {0, 0, 0},
199 /*step_rate*/ {0, 0},
200 /*landing_zone_cylinder*/ {0, 0, 0},
202 /*rotational_offset*/ 0,
204 /*rotation_rate*/ {0, 0},
208 const static struct scsi_caching_page caching_page_default = {
209 /*page_code*/SMS_CACHING_PAGE,
210 /*page_length*/sizeof(struct scsi_caching_page) - 2,
211 /*flags1*/ SCP_DISC | SCP_WCE,
213 /*disable_pf_transfer_len*/ {0xff, 0xff},
214 /*min_prefetch*/ {0, 0},
215 /*max_prefetch*/ {0xff, 0xff},
216 /*max_pf_ceiling*/ {0xff, 0xff},
218 /*cache_segments*/ 0,
219 /*cache_seg_size*/ {0, 0},
221 /*non_cache_seg_size*/ {0, 0, 0}
224 const static struct scsi_caching_page caching_page_changeable = {
225 /*page_code*/SMS_CACHING_PAGE,
226 /*page_length*/sizeof(struct scsi_caching_page) - 2,
227 /*flags1*/ SCP_WCE | SCP_RCD,
229 /*disable_pf_transfer_len*/ {0, 0},
230 /*min_prefetch*/ {0, 0},
231 /*max_prefetch*/ {0, 0},
232 /*max_pf_ceiling*/ {0, 0},
234 /*cache_segments*/ 0,
235 /*cache_seg_size*/ {0, 0},
237 /*non_cache_seg_size*/ {0, 0, 0}
240 const static struct scsi_control_page control_page_default = {
241 /*page_code*/SMS_CONTROL_MODE_PAGE,
242 /*page_length*/sizeof(struct scsi_control_page) - 2,
244 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED,
247 /*aen_holdoff_period*/{0, 0},
248 /*busy_timeout_period*/{0, 0},
249 /*extended_selftest_completion_time*/{0, 0}
252 const static struct scsi_control_page control_page_changeable = {
253 /*page_code*/SMS_CONTROL_MODE_PAGE,
254 /*page_length*/sizeof(struct scsi_control_page) - 2,
256 /*queue_flags*/SCP_QUEUE_ALG_MASK,
257 /*eca_and_aen*/SCP_SWP,
259 /*aen_holdoff_period*/{0, 0},
260 /*busy_timeout_period*/{0, 0},
261 /*extended_selftest_completion_time*/{0, 0}
264 const static struct scsi_info_exceptions_page ie_page_default = {
265 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
266 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
267 /*info_flags*/SIEP_FLAGS_DEXCPT,
269 /*interval_timer*/{0, 0, 0, 0},
270 /*report_count*/{0, 0, 0, 0}
273 const static struct scsi_info_exceptions_page ie_page_changeable = {
274 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
275 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
278 /*interval_timer*/{0, 0, 0, 0},
279 /*report_count*/{0, 0, 0, 0}
282 #define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4)
284 const static struct ctl_logical_block_provisioning_page lbp_page_default = {{
285 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
286 /*subpage_code*/0x02,
287 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
289 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
294 /*count*/{0, 0, 0, 0}},
298 /*count*/{0, 0, 0, 0}},
302 /*count*/{0, 0, 0, 0}},
306 /*count*/{0, 0, 0, 0}}
310 const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{
311 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
312 /*subpage_code*/0x02,
313 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
315 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
320 /*count*/{0, 0, 0, 0}},
324 /*count*/{0, 0, 0, 0}},
328 /*count*/{0, 0, 0, 0}},
332 /*count*/{0, 0, 0, 0}}
336 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
337 static int worker_threads = -1;
338 TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads);
339 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
340 &worker_threads, 1, "Number of worker threads");
341 static int ctl_debug = CTL_DEBUG_NONE;
342 TUNABLE_INT("kern.cam.ctl.debug", &ctl_debug);
343 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN,
344 &ctl_debug, 0, "Enabled debug flags");
347 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
348 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
349 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0),
350 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
352 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 10
354 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
356 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
357 static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest);
358 static int ctl_init(void);
359 void ctl_shutdown(void);
360 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
361 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
362 static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
363 static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
364 struct ctl_ooa *ooa_hdr,
365 struct ctl_ooa_entry *kern_entries);
366 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
368 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
369 struct ctl_be_lun *be_lun);
370 static int ctl_free_lun(struct ctl_lun *lun);
371 static void ctl_create_lun(struct ctl_be_lun *be_lun);
372 static struct ctl_port * ctl_io_port(struct ctl_io_hdr *io_hdr);
374 static int ctl_do_mode_select(union ctl_io *io);
375 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
376 uint64_t res_key, uint64_t sa_res_key,
377 uint8_t type, uint32_t residx,
378 struct ctl_scsiio *ctsio,
379 struct scsi_per_res_out *cdb,
380 struct scsi_per_res_out_parms* param);
381 static void ctl_pro_preempt_other(struct ctl_lun *lun,
382 union ctl_ha_msg *msg);
383 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
384 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
385 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
386 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
387 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
388 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
389 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
391 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
393 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
394 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
395 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
396 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
397 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
398 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2,
400 static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2);
401 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
402 union ctl_io *pending_io, union ctl_io *ooa_io);
403 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
404 union ctl_io *starting_io);
405 static int ctl_check_blocked(struct ctl_lun *lun);
406 static int ctl_scsiio_lun_check(struct ctl_lun *lun,
407 const struct ctl_cmd_entry *entry,
408 struct ctl_scsiio *ctsio);
409 static void ctl_failover_lun(struct ctl_lun *lun);
410 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
411 struct ctl_scsiio *ctsio);
412 static int ctl_scsiio(struct ctl_scsiio *ctsio);
414 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
415 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
416 ctl_ua_type ua_type);
417 static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io,
418 ctl_ua_type ua_type);
419 static int ctl_abort_task(union ctl_io *io);
420 static int ctl_abort_task_set(union ctl_io *io);
421 static int ctl_i_t_nexus_reset(union ctl_io *io);
422 static void ctl_run_task(union ctl_io *io);
424 static void ctl_datamove_timer_wakeup(void *arg);
425 static void ctl_done_timer_wakeup(void *arg);
426 #endif /* CTL_IO_DELAY */
428 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
429 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
430 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
431 static void ctl_datamove_remote_write(union ctl_io *io);
432 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
433 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
434 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
435 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
436 ctl_ha_dt_cb callback);
437 static void ctl_datamove_remote_read(union ctl_io *io);
438 static void ctl_datamove_remote(union ctl_io *io);
439 static int ctl_process_done(union ctl_io *io);
440 static void ctl_lun_thread(void *arg);
441 static void ctl_thresh_thread(void *arg);
442 static void ctl_work_thread(void *arg);
443 static void ctl_enqueue_incoming(union ctl_io *io);
444 static void ctl_enqueue_rtr(union ctl_io *io);
445 static void ctl_enqueue_done(union ctl_io *io);
446 static void ctl_enqueue_isc(union ctl_io *io);
447 static const struct ctl_cmd_entry *
448 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
449 static const struct ctl_cmd_entry *
450 ctl_validate_command(struct ctl_scsiio *ctsio);
451 static int ctl_cmd_applicable(uint8_t lun_type,
452 const struct ctl_cmd_entry *entry);
454 static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx);
455 static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx);
456 static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx);
457 static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key);
460 * Load the serialization table. This isn't very pretty, but is probably
461 * the easiest way to do it.
463 #include "ctl_ser_table.c"
466 * We only need to define open, close and ioctl routines for this driver.
468 static struct cdevsw ctl_cdevsw = {
469 .d_version = D_VERSION,
472 .d_close = ctl_close,
473 .d_ioctl = ctl_ioctl,
478 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
480 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
482 static moduledata_t ctl_moduledata = {
484 ctl_module_event_handler,
488 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
489 MODULE_VERSION(ctl, 1);
491 static struct ctl_frontend ha_frontend =
497 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
498 union ctl_ha_msg *msg_info)
500 struct ctl_scsiio *ctsio;
502 if (msg_info->hdr.original_sc == NULL) {
503 printf("%s: original_sc == NULL!\n", __func__);
504 /* XXX KDM now what? */
508 ctsio = &msg_info->hdr.original_sc->scsiio;
509 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
510 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
511 ctsio->io_hdr.status = msg_info->hdr.status;
512 ctsio->scsi_status = msg_info->scsi.scsi_status;
513 ctsio->sense_len = msg_info->scsi.sense_len;
514 ctsio->sense_residual = msg_info->scsi.sense_residual;
515 ctsio->residual = msg_info->scsi.residual;
516 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
517 msg_info->scsi.sense_len);
518 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
519 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen));
520 ctl_enqueue_isc((union ctl_io *)ctsio);
524 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
525 union ctl_ha_msg *msg_info)
527 struct ctl_scsiio *ctsio;
529 if (msg_info->hdr.serializing_sc == NULL) {
530 printf("%s: serializing_sc == NULL!\n", __func__);
531 /* XXX KDM now what? */
535 ctsio = &msg_info->hdr.serializing_sc->scsiio;
536 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
537 ctl_enqueue_isc((union ctl_io *)ctsio);
541 ctl_isc_announce_lun(struct ctl_lun *lun)
543 struct ctl_softc *softc = lun->ctl_softc;
544 union ctl_ha_msg *msg;
545 struct ctl_ha_msg_lun_pr_key pr_key;
548 if (softc->ha_link != CTL_HA_LINK_ONLINE)
550 mtx_lock(&lun->lun_lock);
551 i = sizeof(msg->lun);
553 i += lun->lun_devid->len;
554 i += sizeof(pr_key) * lun->pr_key_count;
556 mtx_unlock(&lun->lun_lock);
557 msg = malloc(i, M_CTL, M_WAITOK);
558 mtx_lock(&lun->lun_lock);
559 k = sizeof(msg->lun);
561 k += lun->lun_devid->len;
562 k += sizeof(pr_key) * lun->pr_key_count;
568 bzero(&msg->lun, sizeof(msg->lun));
569 msg->hdr.msg_type = CTL_MSG_LUN_SYNC;
570 msg->hdr.nexus.targ_lun = lun->lun;
571 msg->hdr.nexus.targ_mapped_lun = lun->lun;
572 msg->lun.flags = lun->flags;
573 msg->lun.pr_generation = lun->PRGeneration;
574 msg->lun.pr_res_idx = lun->pr_res_idx;
575 msg->lun.pr_res_type = lun->res_type;
576 msg->lun.pr_key_count = lun->pr_key_count;
578 if (lun->lun_devid) {
579 msg->lun.lun_devid_len = lun->lun_devid->len;
580 memcpy(&msg->lun.data[i], lun->lun_devid->data,
581 msg->lun.lun_devid_len);
582 i += msg->lun.lun_devid_len;
584 for (k = 0; k < CTL_MAX_INITIATORS; k++) {
585 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0)
588 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key));
591 mtx_unlock(&lun->lun_lock);
592 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
598 ctl_isc_announce_port(struct ctl_port *port)
600 struct ctl_softc *softc = control_softc;
601 union ctl_ha_msg *msg;
604 if (port->targ_port < softc->port_min ||
605 port->targ_port >= softc->port_max ||
606 softc->ha_link != CTL_HA_LINK_ONLINE)
608 i = sizeof(msg->port) + strlen(port->port_name) + 1;
610 i += sizeof(uint32_t) * CTL_MAX_LUNS;
611 if (port->port_devid)
612 i += port->port_devid->len;
613 if (port->target_devid)
614 i += port->target_devid->len;
615 msg = malloc(i, M_CTL, M_WAITOK);
616 bzero(&msg->port, sizeof(msg->port));
617 msg->hdr.msg_type = CTL_MSG_PORT_SYNC;
618 msg->hdr.nexus.targ_port = port->targ_port;
619 msg->port.port_type = port->port_type;
620 msg->port.physical_port = port->physical_port;
621 msg->port.virtual_port = port->virtual_port;
622 msg->port.status = port->status;
624 msg->port.name_len = sprintf(&msg->port.data[i],
625 "%d:%s", softc->ha_id, port->port_name) + 1;
626 i += msg->port.name_len;
628 msg->port.lun_map_len = sizeof(uint32_t) * CTL_MAX_LUNS;
629 memcpy(&msg->port.data[i], port->lun_map,
630 msg->port.lun_map_len);
631 i += msg->port.lun_map_len;
633 if (port->port_devid) {
634 msg->port.port_devid_len = port->port_devid->len;
635 memcpy(&msg->port.data[i], port->port_devid->data,
636 msg->port.port_devid_len);
637 i += msg->port.port_devid_len;
639 if (port->target_devid) {
640 msg->port.target_devid_len = port->target_devid->len;
641 memcpy(&msg->port.data[i], port->target_devid->data,
642 msg->port.target_devid_len);
643 i += msg->port.target_devid_len;
645 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
651 ctl_isc_ha_link_up(struct ctl_softc *softc)
653 struct ctl_port *port;
656 STAILQ_FOREACH(port, &softc->port_list, links)
657 ctl_isc_announce_port(port);
658 STAILQ_FOREACH(lun, &softc->lun_list, links)
659 ctl_isc_announce_lun(lun);
663 ctl_isc_ha_link_down(struct ctl_softc *softc)
665 struct ctl_port *port;
669 mtx_lock(&softc->ctl_lock);
670 STAILQ_FOREACH(lun, &softc->lun_list, links) {
671 mtx_lock(&lun->lun_lock);
672 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) {
673 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
674 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
676 mtx_unlock(&lun->lun_lock);
678 mtx_unlock(&softc->ctl_lock);
679 io = ctl_alloc_io(softc->othersc_pool);
680 mtx_lock(&softc->ctl_lock);
682 io->io_hdr.msg_type = CTL_MSG_FAILOVER;
683 io->io_hdr.nexus.targ_mapped_lun = lun->lun;
687 STAILQ_FOREACH(port, &softc->port_list, links) {
688 if (port->targ_port >= softc->port_min &&
689 port->targ_port < softc->port_max)
691 port->status &= ~CTL_PORT_STATUS_ONLINE;
693 mtx_unlock(&softc->ctl_lock);
697 ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
700 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus);
702 mtx_lock(&softc->ctl_lock);
703 if (msg->hdr.nexus.targ_lun < CTL_MAX_LUNS &&
704 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) != NULL) {
705 mtx_lock(&lun->lun_lock);
706 mtx_unlock(&softc->ctl_lock);
707 if (msg->ua.ua_all) {
709 ctl_est_ua_all(lun, iid, msg->ua.ua_type);
711 ctl_clr_ua_all(lun, iid, msg->ua.ua_type);
714 ctl_est_ua(lun, iid, msg->ua.ua_type);
716 ctl_clr_ua(lun, iid, msg->ua.ua_type);
718 mtx_unlock(&lun->lun_lock);
720 mtx_unlock(&softc->ctl_lock);
724 ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
727 struct ctl_ha_msg_lun_pr_key pr_key;
729 ctl_lun_flags oflags;
732 targ_lun = msg->hdr.nexus.targ_mapped_lun;
733 mtx_lock(&softc->ctl_lock);
734 if ((targ_lun >= CTL_MAX_LUNS) ||
735 ((lun = softc->ctl_luns[targ_lun]) == NULL)) {
736 mtx_unlock(&softc->ctl_lock);
739 mtx_lock(&lun->lun_lock);
740 mtx_unlock(&softc->ctl_lock);
741 if (lun->flags & CTL_LUN_DISABLED) {
742 mtx_unlock(&lun->lun_lock);
745 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0;
746 if (msg->lun.lun_devid_len != i || (i > 0 &&
747 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) {
748 mtx_unlock(&lun->lun_lock);
749 printf("%s: Received conflicting HA LUN %d\n",
750 __func__, msg->hdr.nexus.targ_lun);
753 /* Record whether peer is primary. */
755 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) &&
756 (msg->lun.flags & CTL_LUN_DISABLED) == 0)
757 lun->flags |= CTL_LUN_PEER_SC_PRIMARY;
759 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
760 if (oflags != lun->flags)
761 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
763 /* If peer is primary and we are not -- use data */
764 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
765 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) {
766 lun->PRGeneration = msg->lun.pr_generation;
767 lun->pr_res_idx = msg->lun.pr_res_idx;
768 lun->res_type = msg->lun.pr_res_type;
769 lun->pr_key_count = msg->lun.pr_key_count;
770 for (k = 0; k < CTL_MAX_INITIATORS; k++)
771 ctl_clr_prkey(lun, k);
772 for (k = 0; k < msg->lun.pr_key_count; k++) {
773 memcpy(&pr_key, &msg->lun.data[i],
775 ctl_alloc_prkey(lun, pr_key.pr_iid);
776 ctl_set_prkey(lun, pr_key.pr_iid,
782 mtx_unlock(&lun->lun_lock);
783 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n",
784 __func__, msg->hdr.nexus.targ_lun,
785 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ?
786 "primary" : "secondary"));
788 /* If we are primary but peer doesn't know -- notify */
789 if ((lun->flags & CTL_LUN_PRIMARY_SC) &&
790 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0)
791 ctl_isc_announce_lun(lun);
796 ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
798 struct ctl_port *port;
802 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
804 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__,
805 msg->hdr.nexus.targ_port));
807 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO);
808 port->frontend = &ha_frontend;
809 port->targ_port = msg->hdr.nexus.targ_port;
810 } else if (port->frontend == &ha_frontend) {
811 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__,
812 msg->hdr.nexus.targ_port));
815 printf("%s: Received conflicting HA port %d\n",
816 __func__, msg->hdr.nexus.targ_port);
819 port->port_type = msg->port.port_type;
820 port->physical_port = msg->port.physical_port;
821 port->virtual_port = msg->port.virtual_port;
822 port->status = msg->port.status;
824 free(port->port_name, M_CTL);
825 port->port_name = strndup(&msg->port.data[i], msg->port.name_len,
827 i += msg->port.name_len;
828 if (msg->port.lun_map_len != 0) {
829 if (port->lun_map == NULL)
830 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS,
832 memcpy(port->lun_map, &msg->port.data[i],
833 sizeof(uint32_t) * CTL_MAX_LUNS);
834 i += msg->port.lun_map_len;
836 free(port->lun_map, M_CTL);
837 port->lun_map = NULL;
839 if (msg->port.port_devid_len != 0) {
840 if (port->port_devid == NULL ||
841 port->port_devid->len != msg->port.port_devid_len) {
842 free(port->port_devid, M_CTL);
843 port->port_devid = malloc(sizeof(struct ctl_devid) +
844 msg->port.port_devid_len, M_CTL, M_WAITOK);
846 memcpy(port->port_devid->data, &msg->port.data[i],
847 msg->port.port_devid_len);
848 port->port_devid->len = msg->port.port_devid_len;
849 i += msg->port.port_devid_len;
851 free(port->port_devid, M_CTL);
852 port->port_devid = NULL;
854 if (msg->port.target_devid_len != 0) {
855 if (port->target_devid == NULL ||
856 port->target_devid->len != msg->port.target_devid_len) {
857 free(port->target_devid, M_CTL);
858 port->target_devid = malloc(sizeof(struct ctl_devid) +
859 msg->port.target_devid_len, M_CTL, M_WAITOK);
861 memcpy(port->target_devid->data, &msg->port.data[i],
862 msg->port.target_devid_len);
863 port->target_devid->len = msg->port.target_devid_len;
864 i += msg->port.target_devid_len;
866 free(port->port_devid, M_CTL);
867 port->port_devid = NULL;
870 if (ctl_port_register(port) != 0) {
871 printf("%s: ctl_port_register() failed with error\n",
875 mtx_lock(&softc->ctl_lock);
876 STAILQ_FOREACH(lun, &softc->lun_list, links) {
877 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
879 mtx_lock(&lun->lun_lock);
880 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
881 mtx_unlock(&lun->lun_lock);
883 mtx_unlock(&softc->ctl_lock);
887 * ISC (Inter Shelf Communication) event handler. Events from the HA
888 * subsystem come in here.
891 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
893 struct ctl_softc *softc;
895 struct ctl_prio *presio;
896 ctl_ha_status isc_status;
898 softc = control_softc;
899 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event));
900 if (event == CTL_HA_EVT_MSG_RECV) {
901 union ctl_ha_msg *msg, msgbuf;
903 if (param > sizeof(msgbuf))
904 msg = malloc(param, M_CTL, M_WAITOK);
907 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param,
909 if (isc_status != CTL_HA_STATUS_SUCCESS) {
910 printf("%s: Error receiving message: %d\n",
911 __func__, isc_status);
917 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type));
918 switch (msg->hdr.msg_type) {
919 case CTL_MSG_SERIALIZE:
920 io = ctl_alloc_io(softc->othersc_pool);
922 // populate ctsio from msg
923 io->io_hdr.io_type = CTL_IO_SCSI;
924 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
925 io->io_hdr.original_sc = msg->hdr.original_sc;
926 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
929 * If we're in serialization-only mode, we don't
930 * want to go through full done processing. Thus
933 * XXX KDM add another flag that is more specific.
935 if (softc->ha_mode != CTL_HA_MODE_XFER)
936 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
937 io->io_hdr.nexus = msg->hdr.nexus;
939 printf("port %u, iid %u, lun %u\n",
940 io->io_hdr.nexus.targ_port,
941 io->io_hdr.nexus.initid,
942 io->io_hdr.nexus.targ_lun);
944 io->scsiio.tag_num = msg->scsi.tag_num;
945 io->scsiio.tag_type = msg->scsi.tag_type;
947 io->io_hdr.start_time = time_uptime;
948 getbintime(&io->io_hdr.start_bt);
949 #endif /* CTL_TIME_IO */
950 io->scsiio.cdb_len = msg->scsi.cdb_len;
951 memcpy(io->scsiio.cdb, msg->scsi.cdb,
953 if (softc->ha_mode == CTL_HA_MODE_XFER) {
954 const struct ctl_cmd_entry *entry;
956 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
957 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
959 entry->flags & CTL_FLAG_DATA_MASK;
964 /* Performed on the Originating SC, XFER mode only */
965 case CTL_MSG_DATAMOVE: {
966 struct ctl_sg_entry *sgl;
969 io = msg->hdr.original_sc;
971 printf("%s: original_sc == NULL!\n", __func__);
972 /* XXX KDM do something here */
975 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
976 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
978 * Keep track of this, we need to send it back over
979 * when the datamove is complete.
981 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
983 if (msg->dt.sg_sequence == 0) {
984 i = msg->dt.kern_sg_entries +
985 io->scsiio.kern_data_len /
986 CTL_HA_DATAMOVE_SEGMENT + 1;
987 sgl = malloc(sizeof(*sgl) * i, M_CTL,
989 io->io_hdr.remote_sglist = sgl;
990 io->io_hdr.local_sglist =
991 &sgl[msg->dt.kern_sg_entries];
993 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
995 io->scsiio.kern_sg_entries =
996 msg->dt.kern_sg_entries;
997 io->scsiio.rem_sg_entries =
998 msg->dt.kern_sg_entries;
999 io->scsiio.kern_data_len =
1000 msg->dt.kern_data_len;
1001 io->scsiio.kern_total_len =
1002 msg->dt.kern_total_len;
1003 io->scsiio.kern_data_resid =
1004 msg->dt.kern_data_resid;
1005 io->scsiio.kern_rel_offset =
1006 msg->dt.kern_rel_offset;
1007 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR;
1008 io->io_hdr.flags |= msg->dt.flags &
1011 sgl = (struct ctl_sg_entry *)
1012 io->scsiio.kern_data_ptr;
1014 for (i = msg->dt.sent_sg_entries, j = 0;
1015 i < (msg->dt.sent_sg_entries +
1016 msg->dt.cur_sg_entries); i++, j++) {
1017 sgl[i].addr = msg->dt.sg_list[j].addr;
1018 sgl[i].len = msg->dt.sg_list[j].len;
1021 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n",
1023 msg->dt.sg_list[j].addr,
1024 msg->dt.sg_list[j].len,
1025 sgl[i].addr, sgl[i].len, j, i);
1030 * If this is the last piece of the I/O, we've got
1031 * the full S/G list. Queue processing in the thread.
1032 * Otherwise wait for the next piece.
1034 if (msg->dt.sg_last != 0)
1035 ctl_enqueue_isc(io);
1038 /* Performed on the Serializing (primary) SC, XFER mode only */
1039 case CTL_MSG_DATAMOVE_DONE: {
1040 if (msg->hdr.serializing_sc == NULL) {
1041 printf("%s: serializing_sc == NULL!\n",
1043 /* XXX KDM now what? */
1047 * We grab the sense information here in case
1048 * there was a failure, so we can return status
1049 * back to the initiator.
1051 io = msg->hdr.serializing_sc;
1052 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
1053 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1054 io->io_hdr.port_status = msg->scsi.fetd_status;
1055 io->scsiio.residual = msg->scsi.residual;
1056 if (msg->hdr.status != CTL_STATUS_NONE) {
1057 io->io_hdr.status = msg->hdr.status;
1058 io->scsiio.scsi_status = msg->scsi.scsi_status;
1059 io->scsiio.sense_len = msg->scsi.sense_len;
1060 io->scsiio.sense_residual =msg->scsi.sense_residual;
1061 memcpy(&io->scsiio.sense_data,
1062 &msg->scsi.sense_data,
1063 msg->scsi.sense_len);
1065 ctl_enqueue_isc(io);
1069 /* Preformed on Originating SC, SER_ONLY mode */
1071 io = msg->hdr.original_sc;
1073 printf("%s: original_sc == NULL!\n",
1077 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1078 io->io_hdr.msg_type = CTL_MSG_R2R;
1079 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1080 ctl_enqueue_isc(io);
1084 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
1086 * Performed on the Originating (i.e. secondary) SC in XFER
1089 case CTL_MSG_FINISH_IO:
1090 if (softc->ha_mode == CTL_HA_MODE_XFER)
1091 ctl_isc_handler_finish_xfer(softc, msg);
1093 ctl_isc_handler_finish_ser_only(softc, msg);
1096 /* Preformed on Originating SC */
1097 case CTL_MSG_BAD_JUJU:
1098 io = msg->hdr.original_sc;
1100 printf("%s: Bad JUJU!, original_sc is NULL!\n",
1104 ctl_copy_sense_data(msg, io);
1106 * IO should have already been cleaned up on other
1107 * SC so clear this flag so we won't send a message
1108 * back to finish the IO there.
1110 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
1111 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1113 /* io = msg->hdr.serializing_sc; */
1114 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
1115 ctl_enqueue_isc(io);
1118 /* Handle resets sent from the other side */
1119 case CTL_MSG_MANAGE_TASKS: {
1120 struct ctl_taskio *taskio;
1121 taskio = (struct ctl_taskio *)ctl_alloc_io(
1122 softc->othersc_pool);
1123 ctl_zero_io((union ctl_io *)taskio);
1124 taskio->io_hdr.io_type = CTL_IO_TASK;
1125 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1126 taskio->io_hdr.nexus = msg->hdr.nexus;
1127 taskio->task_action = msg->task.task_action;
1128 taskio->tag_num = msg->task.tag_num;
1129 taskio->tag_type = msg->task.tag_type;
1131 taskio->io_hdr.start_time = time_uptime;
1132 getbintime(&taskio->io_hdr.start_bt);
1133 #endif /* CTL_TIME_IO */
1134 ctl_run_task((union ctl_io *)taskio);
1137 /* Persistent Reserve action which needs attention */
1138 case CTL_MSG_PERS_ACTION:
1139 presio = (struct ctl_prio *)ctl_alloc_io(
1140 softc->othersc_pool);
1141 ctl_zero_io((union ctl_io *)presio);
1142 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
1143 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1144 presio->io_hdr.nexus = msg->hdr.nexus;
1145 presio->pr_msg = msg->pr;
1146 ctl_enqueue_isc((union ctl_io *)presio);
1149 ctl_isc_ua(softc, msg, param);
1151 case CTL_MSG_PORT_SYNC:
1152 ctl_isc_port_sync(softc, msg, param);
1154 case CTL_MSG_LUN_SYNC:
1155 ctl_isc_lun_sync(softc, msg, param);
1158 printf("Received HA message of unknown type %d\n",
1164 } else if (event == CTL_HA_EVT_LINK_CHANGE) {
1165 printf("CTL: HA link status changed from %d to %d\n",
1166 softc->ha_link, param);
1167 if (param == softc->ha_link)
1169 if (softc->ha_link == CTL_HA_LINK_ONLINE) {
1170 softc->ha_link = param;
1171 ctl_isc_ha_link_down(softc);
1173 softc->ha_link = param;
1174 if (softc->ha_link == CTL_HA_LINK_ONLINE)
1175 ctl_isc_ha_link_up(softc);
1179 printf("ctl_isc_event_handler: Unknown event %d\n", event);
1185 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
1188 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data,
1189 src->scsi.sense_len);
1190 dest->scsiio.scsi_status = src->scsi.scsi_status;
1191 dest->scsiio.sense_len = src->scsi.sense_len;
1192 dest->io_hdr.status = src->hdr.status;
1196 ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest)
1199 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data,
1200 src->scsiio.sense_len);
1201 dest->scsi.scsi_status = src->scsiio.scsi_status;
1202 dest->scsi.sense_len = src->scsiio.sense_len;
1203 dest->hdr.status = src->io_hdr.status;
1207 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1209 struct ctl_softc *softc = lun->ctl_softc;
1212 if (initidx < softc->init_min || initidx >= softc->init_max)
1214 mtx_assert(&lun->lun_lock, MA_OWNED);
1215 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1218 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua;
1222 ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua)
1226 mtx_assert(&lun->lun_lock, MA_OWNED);
1227 if (lun->pending_ua[port] == NULL)
1229 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1230 if (port * CTL_MAX_INIT_PER_PORT + i == except)
1232 lun->pending_ua[port][i] |= ua;
1237 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1239 struct ctl_softc *softc = lun->ctl_softc;
1242 mtx_assert(&lun->lun_lock, MA_OWNED);
1243 for (i = softc->port_min; i < softc->port_max; i++)
1244 ctl_est_ua_port(lun, i, except, ua);
1248 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1250 struct ctl_softc *softc = lun->ctl_softc;
1253 if (initidx < softc->init_min || initidx >= softc->init_max)
1255 mtx_assert(&lun->lun_lock, MA_OWNED);
1256 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1259 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua;
1263 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1265 struct ctl_softc *softc = lun->ctl_softc;
1268 mtx_assert(&lun->lun_lock, MA_OWNED);
1269 for (i = softc->port_min; i < softc->port_max; i++) {
1270 if (lun->pending_ua[i] == NULL)
1272 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
1273 if (i * CTL_MAX_INIT_PER_PORT + j == except)
1275 lun->pending_ua[i][j] &= ~ua;
1281 ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx,
1282 ctl_ua_type ua_type)
1284 struct ctl_lun *lun;
1286 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
1287 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) {
1288 mtx_lock(&lun->lun_lock);
1289 ctl_clr_ua(lun, initidx, ua_type);
1290 mtx_unlock(&lun->lun_lock);
1295 ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS)
1297 struct ctl_softc *softc = (struct ctl_softc *)arg1;
1298 struct ctl_lun *lun;
1299 struct ctl_lun_req ireq;
1302 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1;
1303 error = sysctl_handle_int(oidp, &value, 0, req);
1304 if ((error != 0) || (req->newptr == NULL))
1307 mtx_lock(&softc->ctl_lock);
1309 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1311 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
1312 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1313 mtx_unlock(&softc->ctl_lock);
1314 bzero(&ireq, sizeof(ireq));
1315 ireq.reqtype = CTL_LUNREQ_MODIFY;
1316 ireq.reqdata.modify.lun_id = lun->lun;
1317 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0,
1319 if (ireq.status != CTL_LUN_OK) {
1320 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n",
1321 __func__, ireq.status, ireq.error_str);
1323 mtx_lock(&softc->ctl_lock);
1325 mtx_unlock(&softc->ctl_lock);
1332 struct ctl_softc *softc;
1334 int i, error, retval;
1337 control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1339 softc = control_softc;
1341 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
1344 softc->dev->si_drv1 = softc;
1346 sysctl_ctx_init(&softc->sysctl_ctx);
1347 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1348 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1349 CTLFLAG_RD, 0, "CAM Target Layer");
1351 if (softc->sysctl_tree == NULL) {
1352 printf("%s: unable to allocate sysctl tree\n", __func__);
1353 destroy_dev(softc->dev);
1354 free(control_softc, M_DEVBUF);
1355 control_softc = NULL;
1359 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1360 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
1361 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1362 softc->open_count = 0;
1365 * Default to actually sending a SYNCHRONIZE CACHE command down to
1368 softc->flags = CTL_FLAG_REAL_SYNC;
1370 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1371 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0,
1372 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)");
1375 * In Copan's HA scheme, the "master" and "slave" roles are
1376 * figured out through the slot the controller is in. Although it
1377 * is an active/active system, someone has to be in charge.
1379 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1380 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1381 "HA head ID (0 - no HA)");
1382 if (softc->ha_id == 0 || softc->ha_id > NUM_TARGET_PORT_GROUPS) {
1383 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1384 softc->is_single = 1;
1385 softc->port_cnt = CTL_MAX_PORTS;
1386 softc->port_min = 0;
1388 softc->port_cnt = CTL_MAX_PORTS / NUM_TARGET_PORT_GROUPS;
1389 softc->port_min = (softc->ha_id - 1) * softc->port_cnt;
1391 softc->port_max = softc->port_min + softc->port_cnt;
1392 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT;
1393 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT;
1395 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1396 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0,
1397 "HA link state (0 - offline, 1 - unknown, 2 - online)");
1399 STAILQ_INIT(&softc->lun_list);
1400 STAILQ_INIT(&softc->pending_lun_queue);
1401 STAILQ_INIT(&softc->fe_list);
1402 STAILQ_INIT(&softc->port_list);
1403 STAILQ_INIT(&softc->be_list);
1404 ctl_tpc_init(softc);
1406 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
1409 printf("ctl: can't allocate %d entry other SC pool, "
1410 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1413 softc->othersc_pool = other_pool;
1415 if (worker_threads <= 0)
1416 worker_threads = max(1, mp_ncpus / 4);
1417 if (worker_threads > CTL_MAX_THREADS)
1418 worker_threads = CTL_MAX_THREADS;
1420 for (i = 0; i < worker_threads; i++) {
1421 struct ctl_thread *thr = &softc->threads[i];
1423 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1424 thr->ctl_softc = softc;
1425 STAILQ_INIT(&thr->incoming_queue);
1426 STAILQ_INIT(&thr->rtr_queue);
1427 STAILQ_INIT(&thr->done_queue);
1428 STAILQ_INIT(&thr->isc_queue);
1430 error = kproc_kthread_add(ctl_work_thread, thr,
1431 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1433 printf("error creating CTL work thread!\n");
1434 ctl_pool_free(other_pool);
1438 error = kproc_kthread_add(ctl_lun_thread, softc,
1439 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
1441 printf("error creating CTL lun thread!\n");
1442 ctl_pool_free(other_pool);
1445 error = kproc_kthread_add(ctl_thresh_thread, softc,
1446 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh");
1448 printf("error creating CTL threshold thread!\n");
1449 ctl_pool_free(other_pool);
1453 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1454 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN,
1455 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head");
1457 if (softc->is_single == 0) {
1458 ctl_frontend_register(&ha_frontend);
1459 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) {
1460 printf("ctl_init: ctl_ha_msg_init failed.\n");
1461 softc->is_single = 1;
1463 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
1464 != CTL_HA_STATUS_SUCCESS) {
1465 printf("ctl_init: ctl_ha_msg_register failed.\n");
1466 softc->is_single = 1;
1475 struct ctl_softc *softc;
1476 struct ctl_lun *lun, *next_lun;
1478 softc = (struct ctl_softc *)control_softc;
1480 if (softc->is_single == 0) {
1481 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL)
1482 != CTL_HA_STATUS_SUCCESS) {
1483 printf("ctl_shutdown: ctl_ha_msg_deregister failed.\n");
1485 if (ctl_ha_msg_shutdown(softc) != CTL_HA_STATUS_SUCCESS) {
1486 printf("ctl_shutdown: ctl_ha_msg_shutdown failed.\n");
1488 ctl_frontend_deregister(&ha_frontend);
1491 mtx_lock(&softc->ctl_lock);
1496 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
1497 next_lun = STAILQ_NEXT(lun, links);
1501 mtx_unlock(&softc->ctl_lock);
1504 ctl_shutdown_thread(softc->work_thread);
1505 mtx_destroy(&softc->queue_lock);
1508 ctl_tpc_shutdown(softc);
1509 uma_zdestroy(softc->io_zone);
1510 mtx_destroy(&softc->ctl_lock);
1512 destroy_dev(softc->dev);
1514 sysctl_ctx_free(&softc->sysctl_ctx);
1516 free(control_softc, M_DEVBUF);
1517 control_softc = NULL;
1521 ctl_module_event_handler(module_t mod, int what, void *arg)
1526 return (ctl_init());
1530 return (EOPNOTSUPP);
1535 * XXX KDM should we do some access checks here? Bump a reference count to
1536 * prevent a CTL module from being unloaded while someone has it open?
1539 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1545 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1551 * Remove an initiator by port number and initiator ID.
1552 * Returns 0 for success, -1 for failure.
1555 ctl_remove_initiator(struct ctl_port *port, int iid)
1557 struct ctl_softc *softc = control_softc;
1559 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1561 if (iid > CTL_MAX_INIT_PER_PORT) {
1562 printf("%s: initiator ID %u > maximun %u!\n",
1563 __func__, iid, CTL_MAX_INIT_PER_PORT);
1567 mtx_lock(&softc->ctl_lock);
1568 port->wwpn_iid[iid].in_use--;
1569 port->wwpn_iid[iid].last_use = time_uptime;
1570 mtx_unlock(&softc->ctl_lock);
1576 * Add an initiator to the initiator map.
1577 * Returns iid for success, < 0 for failure.
1580 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
1582 struct ctl_softc *softc = control_softc;
1586 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1588 if (iid >= CTL_MAX_INIT_PER_PORT) {
1589 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
1590 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
1595 mtx_lock(&softc->ctl_lock);
1597 if (iid < 0 && (wwpn != 0 || name != NULL)) {
1598 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1599 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
1603 if (name != NULL && port->wwpn_iid[i].name != NULL &&
1604 strcmp(name, port->wwpn_iid[i].name) == 0) {
1612 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1613 if (port->wwpn_iid[i].in_use == 0 &&
1614 port->wwpn_iid[i].wwpn == 0 &&
1615 port->wwpn_iid[i].name == NULL) {
1624 best_time = INT32_MAX;
1625 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1626 if (port->wwpn_iid[i].in_use == 0) {
1627 if (port->wwpn_iid[i].last_use < best_time) {
1629 best_time = port->wwpn_iid[i].last_use;
1637 mtx_unlock(&softc->ctl_lock);
1642 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
1644 * This is not an error yet.
1646 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
1648 printf("%s: port %d iid %u WWPN %#jx arrived"
1649 " again\n", __func__, port->targ_port,
1650 iid, (uintmax_t)wwpn);
1654 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
1655 strcmp(name, port->wwpn_iid[iid].name) == 0) {
1657 printf("%s: port %d iid %u name '%s' arrived"
1658 " again\n", __func__, port->targ_port,
1665 * This is an error, but what do we do about it? The
1666 * driver is telling us we have a new WWPN for this
1667 * initiator ID, so we pretty much need to use it.
1669 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
1670 " but WWPN %#jx '%s' is still at that address\n",
1671 __func__, port->targ_port, iid, wwpn, name,
1672 (uintmax_t)port->wwpn_iid[iid].wwpn,
1673 port->wwpn_iid[iid].name);
1676 * XXX KDM clear have_ca and ua_pending on each LUN for
1681 free(port->wwpn_iid[iid].name, M_CTL);
1682 port->wwpn_iid[iid].name = name;
1683 port->wwpn_iid[iid].wwpn = wwpn;
1684 port->wwpn_iid[iid].in_use++;
1685 mtx_unlock(&softc->ctl_lock);
1691 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
1695 switch (port->port_type) {
1698 struct scsi_transportid_fcp *id =
1699 (struct scsi_transportid_fcp *)buf;
1700 if (port->wwpn_iid[iid].wwpn == 0)
1702 memset(id, 0, sizeof(*id));
1703 id->format_protocol = SCSI_PROTO_FC;
1704 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
1705 return (sizeof(*id));
1707 case CTL_PORT_ISCSI:
1709 struct scsi_transportid_iscsi_port *id =
1710 (struct scsi_transportid_iscsi_port *)buf;
1711 if (port->wwpn_iid[iid].name == NULL)
1714 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
1716 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
1717 len = roundup2(min(len, 252), 4);
1718 scsi_ulto2b(len, id->additional_length);
1719 return (sizeof(*id) + len);
1723 struct scsi_transportid_sas *id =
1724 (struct scsi_transportid_sas *)buf;
1725 if (port->wwpn_iid[iid].wwpn == 0)
1727 memset(id, 0, sizeof(*id));
1728 id->format_protocol = SCSI_PROTO_SAS;
1729 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
1730 return (sizeof(*id));
1734 struct scsi_transportid_spi *id =
1735 (struct scsi_transportid_spi *)buf;
1736 memset(id, 0, sizeof(*id));
1737 id->format_protocol = SCSI_PROTO_SPI;
1738 scsi_ulto2b(iid, id->scsi_addr);
1739 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
1740 return (sizeof(*id));
1746 * Serialize a command that went down the "wrong" side, and so was sent to
1747 * this controller for execution. The logic is a little different than the
1748 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
1749 * sent back to the other side, but in the success case, we execute the
1750 * command on this side (XFER mode) or tell the other side to execute it
1754 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
1756 struct ctl_softc *softc;
1757 union ctl_ha_msg msg_info;
1758 struct ctl_lun *lun;
1759 const struct ctl_cmd_entry *entry;
1763 softc = control_softc;
1765 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
1766 mtx_lock(&softc->ctl_lock);
1767 if ((targ_lun < CTL_MAX_LUNS) &&
1768 ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
1769 mtx_lock(&lun->lun_lock);
1770 mtx_unlock(&softc->ctl_lock);
1772 * If the LUN is invalid, pretend that it doesn't exist.
1773 * It will go away as soon as all pending I/O has been
1776 if (lun->flags & CTL_LUN_DISABLED) {
1777 mtx_unlock(&lun->lun_lock);
1781 mtx_unlock(&softc->ctl_lock);
1786 * The other node would not send this request to us unless
1787 * received announce that we are primary node for this LUN.
1788 * If this LUN does not exist now, it is probably result of
1789 * a race, so respond to initiator in the most opaque way.
1791 ctl_set_busy(ctsio);
1792 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
1793 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1794 msg_info.hdr.serializing_sc = NULL;
1795 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1796 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1797 sizeof(msg_info.scsi), M_WAITOK);
1801 entry = ctl_get_cmd_entry(ctsio, NULL);
1802 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
1803 mtx_unlock(&lun->lun_lock);
1804 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
1805 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1806 msg_info.hdr.serializing_sc = NULL;
1807 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1808 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1809 sizeof(msg_info.scsi), M_WAITOK);
1813 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
1814 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = lun->be_lun;
1817 * Every I/O goes into the OOA queue for a
1818 * particular LUN, and stays there until completion.
1821 if (TAILQ_EMPTY(&lun->ooa_queue))
1822 lun->idle_time += getsbinuptime() - lun->last_busy;
1824 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1826 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
1827 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
1829 case CTL_ACTION_BLOCK:
1830 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
1831 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
1833 mtx_unlock(&lun->lun_lock);
1835 case CTL_ACTION_PASS:
1836 case CTL_ACTION_SKIP:
1837 if (softc->ha_mode == CTL_HA_MODE_XFER) {
1838 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
1839 ctl_enqueue_rtr((union ctl_io *)ctsio);
1840 mtx_unlock(&lun->lun_lock);
1842 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
1843 mtx_unlock(&lun->lun_lock);
1845 /* send msg back to other side */
1846 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1847 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
1848 msg_info.hdr.msg_type = CTL_MSG_R2R;
1849 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1850 sizeof(msg_info.hdr), M_WAITOK);
1853 case CTL_ACTION_OVERLAP:
1854 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1855 mtx_unlock(&lun->lun_lock);
1858 ctl_set_overlapped_cmd(ctsio);
1859 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
1860 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1861 msg_info.hdr.serializing_sc = NULL;
1862 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1863 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1864 sizeof(msg_info.scsi), M_WAITOK);
1866 case CTL_ACTION_OVERLAP_TAG:
1867 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1868 mtx_unlock(&lun->lun_lock);
1870 ctl_set_overlapped_tag(ctsio, ctsio->tag_num);
1871 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
1872 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1873 msg_info.hdr.serializing_sc = NULL;
1874 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1875 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1876 sizeof(msg_info.scsi), M_WAITOK);
1878 case CTL_ACTION_ERROR:
1880 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1881 mtx_unlock(&lun->lun_lock);
1884 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
1886 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
1887 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1888 msg_info.hdr.serializing_sc = NULL;
1889 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1890 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1891 sizeof(msg_info.scsi), M_WAITOK);
1898 * Returns 0 for success, errno for failure.
1901 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
1902 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
1909 mtx_lock(&lun->lun_lock);
1910 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
1911 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
1913 struct ctl_ooa_entry *entry;
1916 * If we've got more than we can fit, just count the
1917 * remaining entries.
1919 if (*cur_fill_num >= ooa_hdr->alloc_num)
1922 entry = &kern_entries[*cur_fill_num];
1924 entry->tag_num = io->scsiio.tag_num;
1925 entry->lun_num = lun->lun;
1927 entry->start_bt = io->io_hdr.start_bt;
1929 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
1930 entry->cdb_len = io->scsiio.cdb_len;
1931 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
1932 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
1934 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
1935 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
1937 if (io->io_hdr.flags & CTL_FLAG_ABORT)
1938 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
1940 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
1941 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
1943 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
1944 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
1946 mtx_unlock(&lun->lun_lock);
1952 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
1953 size_t error_str_len)
1957 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
1959 if (copyin(user_addr, kptr, len) != 0) {
1960 snprintf(error_str, error_str_len, "Error copying %d bytes "
1961 "from user address %p to kernel address %p", len,
1971 ctl_free_args(int num_args, struct ctl_be_arg *args)
1978 for (i = 0; i < num_args; i++) {
1979 free(args[i].kname, M_CTL);
1980 free(args[i].kvalue, M_CTL);
1986 static struct ctl_be_arg *
1987 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
1988 char *error_str, size_t error_str_len)
1990 struct ctl_be_arg *args;
1993 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
1994 error_str, error_str_len);
1999 for (i = 0; i < num_args; i++) {
2000 args[i].kname = NULL;
2001 args[i].kvalue = NULL;
2004 for (i = 0; i < num_args; i++) {
2007 args[i].kname = ctl_copyin_alloc(args[i].name,
2008 args[i].namelen, error_str, error_str_len);
2009 if (args[i].kname == NULL)
2012 if (args[i].kname[args[i].namelen - 1] != '\0') {
2013 snprintf(error_str, error_str_len, "Argument %d "
2014 "name is not NUL-terminated", i);
2018 if (args[i].flags & CTL_BEARG_RD) {
2019 tmpptr = ctl_copyin_alloc(args[i].value,
2020 args[i].vallen, error_str, error_str_len);
2023 if ((args[i].flags & CTL_BEARG_ASCII)
2024 && (tmpptr[args[i].vallen - 1] != '\0')) {
2025 snprintf(error_str, error_str_len, "Argument "
2026 "%d value is not NUL-terminated", i);
2029 args[i].kvalue = tmpptr;
2031 args[i].kvalue = malloc(args[i].vallen,
2032 M_CTL, M_WAITOK | M_ZERO);
2039 ctl_free_args(num_args, args);
2045 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2049 for (i = 0; i < num_args; i++) {
2050 if (args[i].flags & CTL_BEARG_WR)
2051 copyout(args[i].kvalue, args[i].value, args[i].vallen);
2056 * Escape characters that are illegal or not recommended in XML.
2059 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2061 char *end = str + size;
2066 for (; *str && str < end; str++) {
2069 retval = sbuf_printf(sb, "&");
2072 retval = sbuf_printf(sb, ">");
2075 retval = sbuf_printf(sb, "<");
2078 retval = sbuf_putc(sb, *str);
2091 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2093 struct scsi_vpd_id_descriptor *desc;
2096 if (id == NULL || id->len < 4)
2098 desc = (struct scsi_vpd_id_descriptor *)id->data;
2099 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2100 case SVPD_ID_TYPE_T10:
2101 sbuf_printf(sb, "t10.");
2103 case SVPD_ID_TYPE_EUI64:
2104 sbuf_printf(sb, "eui.");
2106 case SVPD_ID_TYPE_NAA:
2107 sbuf_printf(sb, "naa.");
2109 case SVPD_ID_TYPE_SCSI_NAME:
2112 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2113 case SVPD_ID_CODESET_BINARY:
2114 for (i = 0; i < desc->length; i++)
2115 sbuf_printf(sb, "%02x", desc->identifier[i]);
2117 case SVPD_ID_CODESET_ASCII:
2118 sbuf_printf(sb, "%.*s", (int)desc->length,
2119 (char *)desc->identifier);
2121 case SVPD_ID_CODESET_UTF8:
2122 sbuf_printf(sb, "%s", (char *)desc->identifier);
2128 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2131 struct ctl_softc *softc;
2132 struct ctl_lun *lun;
2135 softc = control_softc;
2141 retval = ctl_ioctl_io(dev, cmd, addr, flag, td);
2143 case CTL_ENABLE_PORT:
2144 case CTL_DISABLE_PORT:
2145 case CTL_SET_PORT_WWNS: {
2146 struct ctl_port *port;
2147 struct ctl_port_entry *entry;
2149 entry = (struct ctl_port_entry *)addr;
2151 mtx_lock(&softc->ctl_lock);
2152 STAILQ_FOREACH(port, &softc->port_list, links) {
2155 if (port->targ_port < softc->port_min ||
2156 port->targ_port >= softc->port_max)
2161 if ((entry->port_type == CTL_PORT_NONE)
2162 && (entry->targ_port == port->targ_port)) {
2164 * If the user only wants to enable or
2165 * disable or set WWNs on a specific port,
2166 * do the operation and we're done.
2170 } else if (entry->port_type & port->port_type) {
2172 * Compare the user's type mask with the
2173 * particular frontend type to see if we
2180 * Make sure the user isn't trying to set
2181 * WWNs on multiple ports at the same time.
2183 if (cmd == CTL_SET_PORT_WWNS) {
2184 printf("%s: Can't set WWNs on "
2185 "multiple ports\n", __func__);
2194 * XXX KDM we have to drop the lock here, because
2195 * the online/offline operations can potentially
2196 * block. We need to reference count the frontends
2197 * so they can't go away,
2199 if (cmd == CTL_ENABLE_PORT) {
2200 mtx_unlock(&softc->ctl_lock);
2201 ctl_port_online(port);
2202 mtx_lock(&softc->ctl_lock);
2203 } else if (cmd == CTL_DISABLE_PORT) {
2204 mtx_unlock(&softc->ctl_lock);
2205 ctl_port_offline(port);
2206 mtx_lock(&softc->ctl_lock);
2207 } else if (cmd == CTL_SET_PORT_WWNS) {
2208 ctl_port_set_wwns(port,
2209 (entry->flags & CTL_PORT_WWNN_VALID) ?
2211 (entry->flags & CTL_PORT_WWPN_VALID) ?
2212 1 : 0, entry->wwpn);
2217 mtx_unlock(&softc->ctl_lock);
2220 case CTL_GET_PORT_LIST: {
2221 struct ctl_port *port;
2222 struct ctl_port_list *list;
2225 list = (struct ctl_port_list *)addr;
2227 if (list->alloc_len != (list->alloc_num *
2228 sizeof(struct ctl_port_entry))) {
2229 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != "
2230 "alloc_num %u * sizeof(struct ctl_port_entry) "
2231 "%zu\n", __func__, list->alloc_len,
2232 list->alloc_num, sizeof(struct ctl_port_entry));
2238 list->dropped_num = 0;
2240 mtx_lock(&softc->ctl_lock);
2241 STAILQ_FOREACH(port, &softc->port_list, links) {
2242 struct ctl_port_entry entry, *list_entry;
2244 if (list->fill_num >= list->alloc_num) {
2245 list->dropped_num++;
2249 entry.port_type = port->port_type;
2250 strlcpy(entry.port_name, port->port_name,
2251 sizeof(entry.port_name));
2252 entry.targ_port = port->targ_port;
2253 entry.physical_port = port->physical_port;
2254 entry.virtual_port = port->virtual_port;
2255 entry.wwnn = port->wwnn;
2256 entry.wwpn = port->wwpn;
2257 if (port->status & CTL_PORT_STATUS_ONLINE)
2262 list_entry = &list->entries[i];
2264 retval = copyout(&entry, list_entry, sizeof(entry));
2266 printf("%s: CTL_GET_PORT_LIST: copyout "
2267 "returned %d\n", __func__, retval);
2272 list->fill_len += sizeof(entry);
2274 mtx_unlock(&softc->ctl_lock);
2277 * If this is non-zero, we had a copyout fault, so there's
2278 * probably no point in attempting to set the status inside
2284 if (list->dropped_num > 0)
2285 list->status = CTL_PORT_LIST_NEED_MORE_SPACE;
2287 list->status = CTL_PORT_LIST_OK;
2290 case CTL_DUMP_OOA: {
2295 mtx_lock(&softc->ctl_lock);
2296 printf("Dumping OOA queues:\n");
2297 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2298 mtx_lock(&lun->lun_lock);
2299 for (io = (union ctl_io *)TAILQ_FIRST(
2300 &lun->ooa_queue); io != NULL;
2301 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2303 sbuf_new(&sb, printbuf, sizeof(printbuf),
2305 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ",
2309 CTL_FLAG_BLOCKED) ? "" : " BLOCKED",
2311 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
2313 CTL_FLAG_ABORT) ? " ABORT" : "",
2315 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : "");
2316 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
2318 printf("%s\n", sbuf_data(&sb));
2320 mtx_unlock(&lun->lun_lock);
2322 printf("OOA queues dump done\n");
2323 mtx_unlock(&softc->ctl_lock);
2327 struct ctl_ooa *ooa_hdr;
2328 struct ctl_ooa_entry *entries;
2329 uint32_t cur_fill_num;
2331 ooa_hdr = (struct ctl_ooa *)addr;
2333 if ((ooa_hdr->alloc_len == 0)
2334 || (ooa_hdr->alloc_num == 0)) {
2335 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2336 "must be non-zero\n", __func__,
2337 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2342 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2343 sizeof(struct ctl_ooa_entry))) {
2344 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2345 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2346 __func__, ooa_hdr->alloc_len,
2347 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2352 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2353 if (entries == NULL) {
2354 printf("%s: could not allocate %d bytes for OOA "
2355 "dump\n", __func__, ooa_hdr->alloc_len);
2360 mtx_lock(&softc->ctl_lock);
2361 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2362 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS)
2363 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2364 mtx_unlock(&softc->ctl_lock);
2365 free(entries, M_CTL);
2366 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2367 __func__, (uintmax_t)ooa_hdr->lun_num);
2374 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2375 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2376 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2382 mtx_unlock(&softc->ctl_lock);
2383 free(entries, M_CTL);
2387 lun = softc->ctl_luns[ooa_hdr->lun_num];
2389 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr,
2392 mtx_unlock(&softc->ctl_lock);
2394 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2395 ooa_hdr->fill_len = ooa_hdr->fill_num *
2396 sizeof(struct ctl_ooa_entry);
2397 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2399 printf("%s: error copying out %d bytes for OOA dump\n",
2400 __func__, ooa_hdr->fill_len);
2403 getbintime(&ooa_hdr->cur_bt);
2405 if (cur_fill_num > ooa_hdr->alloc_num) {
2406 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2407 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2409 ooa_hdr->dropped_num = 0;
2410 ooa_hdr->status = CTL_OOA_OK;
2413 free(entries, M_CTL);
2416 case CTL_CHECK_OOA: {
2418 struct ctl_ooa_info *ooa_info;
2421 ooa_info = (struct ctl_ooa_info *)addr;
2423 if (ooa_info->lun_id >= CTL_MAX_LUNS) {
2424 ooa_info->status = CTL_OOA_INVALID_LUN;
2427 mtx_lock(&softc->ctl_lock);
2428 lun = softc->ctl_luns[ooa_info->lun_id];
2430 mtx_unlock(&softc->ctl_lock);
2431 ooa_info->status = CTL_OOA_INVALID_LUN;
2434 mtx_lock(&lun->lun_lock);
2435 mtx_unlock(&softc->ctl_lock);
2436 ooa_info->num_entries = 0;
2437 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
2438 io != NULL; io = (union ctl_io *)TAILQ_NEXT(
2439 &io->io_hdr, ooa_links)) {
2440 ooa_info->num_entries++;
2442 mtx_unlock(&lun->lun_lock);
2444 ooa_info->status = CTL_OOA_SUCCESS;
2448 case CTL_DELAY_IO: {
2449 struct ctl_io_delay_info *delay_info;
2451 delay_info = (struct ctl_io_delay_info *)addr;
2454 mtx_lock(&softc->ctl_lock);
2456 if ((delay_info->lun_id >= CTL_MAX_LUNS)
2457 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2458 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2460 lun = softc->ctl_luns[delay_info->lun_id];
2461 mtx_lock(&lun->lun_lock);
2463 delay_info->status = CTL_DELAY_STATUS_OK;
2465 switch (delay_info->delay_type) {
2466 case CTL_DELAY_TYPE_CONT:
2468 case CTL_DELAY_TYPE_ONESHOT:
2471 delay_info->status =
2472 CTL_DELAY_STATUS_INVALID_TYPE;
2476 switch (delay_info->delay_loc) {
2477 case CTL_DELAY_LOC_DATAMOVE:
2478 lun->delay_info.datamove_type =
2479 delay_info->delay_type;
2480 lun->delay_info.datamove_delay =
2481 delay_info->delay_secs;
2483 case CTL_DELAY_LOC_DONE:
2484 lun->delay_info.done_type =
2485 delay_info->delay_type;
2486 lun->delay_info.done_delay =
2487 delay_info->delay_secs;
2490 delay_info->status =
2491 CTL_DELAY_STATUS_INVALID_LOC;
2494 mtx_unlock(&lun->lun_lock);
2497 mtx_unlock(&softc->ctl_lock);
2499 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2500 #endif /* CTL_IO_DELAY */
2503 case CTL_REALSYNC_SET: {
2506 syncstate = (int *)addr;
2508 mtx_lock(&softc->ctl_lock);
2509 switch (*syncstate) {
2511 softc->flags &= ~CTL_FLAG_REAL_SYNC;
2514 softc->flags |= CTL_FLAG_REAL_SYNC;
2520 mtx_unlock(&softc->ctl_lock);
2523 case CTL_REALSYNC_GET: {
2526 syncstate = (int*)addr;
2528 mtx_lock(&softc->ctl_lock);
2529 if (softc->flags & CTL_FLAG_REAL_SYNC)
2533 mtx_unlock(&softc->ctl_lock);
2539 struct ctl_sync_info *sync_info;
2541 sync_info = (struct ctl_sync_info *)addr;
2543 mtx_lock(&softc->ctl_lock);
2544 lun = softc->ctl_luns[sync_info->lun_id];
2546 mtx_unlock(&softc->ctl_lock);
2547 sync_info->status = CTL_GS_SYNC_NO_LUN;
2551 * Get or set the sync interval. We're not bounds checking
2552 * in the set case, hopefully the user won't do something
2555 mtx_lock(&lun->lun_lock);
2556 mtx_unlock(&softc->ctl_lock);
2557 if (cmd == CTL_GETSYNC)
2558 sync_info->sync_interval = lun->sync_interval;
2560 lun->sync_interval = sync_info->sync_interval;
2561 mtx_unlock(&lun->lun_lock);
2563 sync_info->status = CTL_GS_SYNC_OK;
2567 case CTL_GETSTATS: {
2568 struct ctl_stats *stats;
2571 stats = (struct ctl_stats *)addr;
2573 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2575 stats->status = CTL_SS_NEED_MORE_SPACE;
2576 stats->num_luns = softc->num_luns;
2580 * XXX KDM no locking here. If the LUN list changes,
2581 * things can blow up.
2583 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
2584 i++, lun = STAILQ_NEXT(lun, links)) {
2585 retval = copyout(&lun->stats, &stats->lun_stats[i],
2586 sizeof(lun->stats));
2590 stats->num_luns = softc->num_luns;
2591 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2593 stats->status = CTL_SS_OK;
2595 stats->flags = CTL_STATS_FLAG_TIME_VALID;
2597 stats->flags = CTL_STATS_FLAG_NONE;
2599 getnanouptime(&stats->timestamp);
2602 case CTL_ERROR_INJECT: {
2603 struct ctl_error_desc *err_desc, *new_err_desc;
2605 err_desc = (struct ctl_error_desc *)addr;
2607 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2609 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2611 mtx_lock(&softc->ctl_lock);
2612 lun = softc->ctl_luns[err_desc->lun_id];
2614 mtx_unlock(&softc->ctl_lock);
2615 free(new_err_desc, M_CTL);
2616 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2617 __func__, (uintmax_t)err_desc->lun_id);
2621 mtx_lock(&lun->lun_lock);
2622 mtx_unlock(&softc->ctl_lock);
2625 * We could do some checking here to verify the validity
2626 * of the request, but given the complexity of error
2627 * injection requests, the checking logic would be fairly
2630 * For now, if the request is invalid, it just won't get
2631 * executed and might get deleted.
2633 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2636 * XXX KDM check to make sure the serial number is unique,
2637 * in case we somehow manage to wrap. That shouldn't
2638 * happen for a very long time, but it's the right thing to
2641 new_err_desc->serial = lun->error_serial;
2642 err_desc->serial = lun->error_serial;
2643 lun->error_serial++;
2645 mtx_unlock(&lun->lun_lock);
2648 case CTL_ERROR_INJECT_DELETE: {
2649 struct ctl_error_desc *delete_desc, *desc, *desc2;
2652 delete_desc = (struct ctl_error_desc *)addr;
2655 mtx_lock(&softc->ctl_lock);
2656 lun = softc->ctl_luns[delete_desc->lun_id];
2658 mtx_unlock(&softc->ctl_lock);
2659 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2660 __func__, (uintmax_t)delete_desc->lun_id);
2664 mtx_lock(&lun->lun_lock);
2665 mtx_unlock(&softc->ctl_lock);
2666 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2667 if (desc->serial != delete_desc->serial)
2670 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2675 mtx_unlock(&lun->lun_lock);
2676 if (delete_done == 0) {
2677 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2678 "error serial %ju on LUN %u\n", __func__,
2679 delete_desc->serial, delete_desc->lun_id);
2685 case CTL_DUMP_STRUCTS: {
2687 struct ctl_port *port;
2688 struct ctl_frontend *fe;
2690 mtx_lock(&softc->ctl_lock);
2691 printf("CTL Persistent Reservation information start:\n");
2692 for (i = 0; i < CTL_MAX_LUNS; i++) {
2693 lun = softc->ctl_luns[i];
2696 || ((lun->flags & CTL_LUN_DISABLED) != 0))
2699 for (j = 0; j < CTL_MAX_PORTS; j++) {
2700 if (lun->pr_keys[j] == NULL)
2702 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2703 if (lun->pr_keys[j][k] == 0)
2705 printf(" LUN %d port %d iid %d key "
2707 (uintmax_t)lun->pr_keys[j][k]);
2711 printf("CTL Persistent Reservation information end\n");
2712 printf("CTL Ports:\n");
2713 STAILQ_FOREACH(port, &softc->port_list, links) {
2714 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
2715 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
2716 port->frontend->name, port->port_type,
2717 port->physical_port, port->virtual_port,
2718 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
2719 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2720 if (port->wwpn_iid[j].in_use == 0 &&
2721 port->wwpn_iid[j].wwpn == 0 &&
2722 port->wwpn_iid[j].name == NULL)
2725 printf(" iid %u use %d WWPN %#jx '%s'\n",
2726 j, port->wwpn_iid[j].in_use,
2727 (uintmax_t)port->wwpn_iid[j].wwpn,
2728 port->wwpn_iid[j].name);
2731 printf("CTL Port information end\n");
2732 mtx_unlock(&softc->ctl_lock);
2734 * XXX KDM calling this without a lock. We'd likely want
2735 * to drop the lock before calling the frontend's dump
2738 printf("CTL Frontends:\n");
2739 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2740 printf(" Frontend '%s'\n", fe->name);
2741 if (fe->fe_dump != NULL)
2744 printf("CTL Frontend information end\n");
2748 struct ctl_lun_req *lun_req;
2749 struct ctl_backend_driver *backend;
2751 lun_req = (struct ctl_lun_req *)addr;
2753 backend = ctl_backend_find(lun_req->backend);
2754 if (backend == NULL) {
2755 lun_req->status = CTL_LUN_ERROR;
2756 snprintf(lun_req->error_str,
2757 sizeof(lun_req->error_str),
2758 "Backend \"%s\" not found.",
2762 if (lun_req->num_be_args > 0) {
2763 lun_req->kern_be_args = ctl_copyin_args(
2764 lun_req->num_be_args,
2767 sizeof(lun_req->error_str));
2768 if (lun_req->kern_be_args == NULL) {
2769 lun_req->status = CTL_LUN_ERROR;
2774 retval = backend->ioctl(dev, cmd, addr, flag, td);
2776 if (lun_req->num_be_args > 0) {
2777 ctl_copyout_args(lun_req->num_be_args,
2778 lun_req->kern_be_args);
2779 ctl_free_args(lun_req->num_be_args,
2780 lun_req->kern_be_args);
2784 case CTL_LUN_LIST: {
2786 struct ctl_lun_list *list;
2787 struct ctl_option *opt;
2789 list = (struct ctl_lun_list *)addr;
2792 * Allocate a fixed length sbuf here, based on the length
2793 * of the user's buffer. We could allocate an auto-extending
2794 * buffer, and then tell the user how much larger our
2795 * amount of data is than his buffer, but that presents
2798 * 1. The sbuf(9) routines use a blocking malloc, and so
2799 * we can't hold a lock while calling them with an
2800 * auto-extending buffer.
2802 * 2. There is not currently a LUN reference counting
2803 * mechanism, outside of outstanding transactions on
2804 * the LUN's OOA queue. So a LUN could go away on us
2805 * while we're getting the LUN number, backend-specific
2806 * information, etc. Thus, given the way things
2807 * currently work, we need to hold the CTL lock while
2808 * grabbing LUN information.
2810 * So, from the user's standpoint, the best thing to do is
2811 * allocate what he thinks is a reasonable buffer length,
2812 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
2813 * double the buffer length and try again. (And repeat
2814 * that until he succeeds.)
2816 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
2818 list->status = CTL_LUN_LIST_ERROR;
2819 snprintf(list->error_str, sizeof(list->error_str),
2820 "Unable to allocate %d bytes for LUN list",
2825 sbuf_printf(sb, "<ctllunlist>\n");
2827 mtx_lock(&softc->ctl_lock);
2828 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2829 mtx_lock(&lun->lun_lock);
2830 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
2831 (uintmax_t)lun->lun);
2834 * Bail out as soon as we see that we've overfilled
2840 retval = sbuf_printf(sb, "\t<backend_type>%s"
2841 "</backend_type>\n",
2842 (lun->backend == NULL) ? "none" :
2843 lun->backend->name);
2848 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
2849 lun->be_lun->lun_type);
2854 if (lun->backend == NULL) {
2855 retval = sbuf_printf(sb, "</lun>\n");
2861 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
2862 (lun->be_lun->maxlba > 0) ?
2863 lun->be_lun->maxlba + 1 : 0);
2868 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
2869 lun->be_lun->blocksize);
2874 retval = sbuf_printf(sb, "\t<serial_number>");
2879 retval = ctl_sbuf_printf_esc(sb,
2880 lun->be_lun->serial_num,
2881 sizeof(lun->be_lun->serial_num));
2886 retval = sbuf_printf(sb, "</serial_number>\n");
2891 retval = sbuf_printf(sb, "\t<device_id>");
2896 retval = ctl_sbuf_printf_esc(sb,
2897 lun->be_lun->device_id,
2898 sizeof(lun->be_lun->device_id));
2903 retval = sbuf_printf(sb, "</device_id>\n");
2908 if (lun->backend->lun_info != NULL) {
2909 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
2913 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
2914 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
2915 opt->name, opt->value, opt->name);
2920 retval = sbuf_printf(sb, "</lun>\n");
2924 mtx_unlock(&lun->lun_lock);
2927 mtx_unlock(&lun->lun_lock);
2928 mtx_unlock(&softc->ctl_lock);
2931 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
2934 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
2935 snprintf(list->error_str, sizeof(list->error_str),
2936 "Out of space, %d bytes is too small",
2943 retval = copyout(sbuf_data(sb), list->lun_xml,
2946 list->fill_len = sbuf_len(sb) + 1;
2947 list->status = CTL_LUN_LIST_OK;
2952 struct ctl_iscsi *ci;
2953 struct ctl_frontend *fe;
2955 ci = (struct ctl_iscsi *)addr;
2957 fe = ctl_frontend_find("iscsi");
2959 ci->status = CTL_ISCSI_ERROR;
2960 snprintf(ci->error_str, sizeof(ci->error_str),
2961 "Frontend \"iscsi\" not found.");
2965 retval = fe->ioctl(dev, cmd, addr, flag, td);
2968 case CTL_PORT_REQ: {
2969 struct ctl_req *req;
2970 struct ctl_frontend *fe;
2972 req = (struct ctl_req *)addr;
2974 fe = ctl_frontend_find(req->driver);
2976 req->status = CTL_LUN_ERROR;
2977 snprintf(req->error_str, sizeof(req->error_str),
2978 "Frontend \"%s\" not found.", req->driver);
2981 if (req->num_args > 0) {
2982 req->kern_args = ctl_copyin_args(req->num_args,
2983 req->args, req->error_str, sizeof(req->error_str));
2984 if (req->kern_args == NULL) {
2985 req->status = CTL_LUN_ERROR;
2991 retval = fe->ioctl(dev, cmd, addr, flag, td);
2995 if (req->num_args > 0) {
2996 ctl_copyout_args(req->num_args, req->kern_args);
2997 ctl_free_args(req->num_args, req->kern_args);
3001 case CTL_PORT_LIST: {
3003 struct ctl_port *port;
3004 struct ctl_lun_list *list;
3005 struct ctl_option *opt;
3009 list = (struct ctl_lun_list *)addr;
3011 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3013 list->status = CTL_LUN_LIST_ERROR;
3014 snprintf(list->error_str, sizeof(list->error_str),
3015 "Unable to allocate %d bytes for LUN list",
3020 sbuf_printf(sb, "<ctlportlist>\n");
3022 mtx_lock(&softc->ctl_lock);
3023 STAILQ_FOREACH(port, &softc->port_list, links) {
3024 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3025 (uintmax_t)port->targ_port);
3028 * Bail out as soon as we see that we've overfilled
3034 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3035 "</frontend_type>\n", port->frontend->name);
3039 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3044 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3045 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3049 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3054 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3055 port->physical_port);
3059 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3060 port->virtual_port);
3064 if (port->target_devid != NULL) {
3065 sbuf_printf(sb, "\t<target>");
3066 ctl_id_sbuf(port->target_devid, sb);
3067 sbuf_printf(sb, "</target>\n");
3070 if (port->port_devid != NULL) {
3071 sbuf_printf(sb, "\t<port>");
3072 ctl_id_sbuf(port->port_devid, sb);
3073 sbuf_printf(sb, "</port>\n");
3076 if (port->port_info != NULL) {
3077 retval = port->port_info(port->onoff_arg, sb);
3081 STAILQ_FOREACH(opt, &port->options, links) {
3082 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3083 opt->name, opt->value, opt->name);
3088 if (port->lun_map != NULL) {
3089 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n");
3090 for (j = 0; j < CTL_MAX_LUNS; j++) {
3091 plun = ctl_lun_map_from_port(port, j);
3092 if (plun >= CTL_MAX_LUNS)
3095 "\t<lun id=\"%u\">%u</lun>\n",
3100 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3101 if (port->wwpn_iid[j].in_use == 0 ||
3102 (port->wwpn_iid[j].wwpn == 0 &&
3103 port->wwpn_iid[j].name == NULL))
3106 if (port->wwpn_iid[j].name != NULL)
3107 retval = sbuf_printf(sb,
3108 "\t<initiator id=\"%u\">%s</initiator>\n",
3109 j, port->wwpn_iid[j].name);
3111 retval = sbuf_printf(sb,
3112 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3113 j, port->wwpn_iid[j].wwpn);
3120 retval = sbuf_printf(sb, "</targ_port>\n");
3124 mtx_unlock(&softc->ctl_lock);
3127 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3130 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3131 snprintf(list->error_str, sizeof(list->error_str),
3132 "Out of space, %d bytes is too small",
3139 retval = copyout(sbuf_data(sb), list->lun_xml,
3142 list->fill_len = sbuf_len(sb) + 1;
3143 list->status = CTL_LUN_LIST_OK;
3148 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr;
3149 struct ctl_port *port;
3151 mtx_lock(&softc->ctl_lock);
3152 if (lm->port < softc->port_min ||
3153 lm->port >= softc->port_max ||
3154 (port = softc->ctl_ports[lm->port]) == NULL) {
3155 mtx_unlock(&softc->ctl_lock);
3158 if (port->status & CTL_PORT_STATUS_ONLINE) {
3159 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3160 if (ctl_lun_map_to_port(port, lun->lun) >=
3163 mtx_lock(&lun->lun_lock);
3164 ctl_est_ua_port(lun, lm->port, -1,
3166 mtx_unlock(&lun->lun_lock);
3169 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps
3170 if (lm->plun < CTL_MAX_LUNS) {
3171 if (lm->lun == UINT32_MAX)
3172 retval = ctl_lun_map_unset(port, lm->plun);
3173 else if (lm->lun < CTL_MAX_LUNS &&
3174 softc->ctl_luns[lm->lun] != NULL)
3175 retval = ctl_lun_map_set(port, lm->plun, lm->lun);
3178 } else if (lm->plun == UINT32_MAX) {
3179 if (lm->lun == UINT32_MAX)
3180 retval = ctl_lun_map_deinit(port);
3182 retval = ctl_lun_map_init(port);
3185 if (port->status & CTL_PORT_STATUS_ONLINE)
3186 ctl_isc_announce_port(port);
3190 /* XXX KDM should we fix this? */
3192 struct ctl_backend_driver *backend;
3199 * We encode the backend type as the ioctl type for backend
3200 * ioctls. So parse it out here, and then search for a
3201 * backend of this type.
3203 type = _IOC_TYPE(cmd);
3205 STAILQ_FOREACH(backend, &softc->be_list, links) {
3206 if (backend->type == type) {
3212 printf("ctl: unknown ioctl command %#lx or backend "
3217 retval = backend->ioctl(dev, cmd, addr, flag, td);
3227 ctl_get_initindex(struct ctl_nexus *nexus)
3229 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3233 ctl_lun_map_init(struct ctl_port *port)
3235 struct ctl_softc *softc = control_softc;
3236 struct ctl_lun *lun;
3239 if (port->lun_map == NULL)
3240 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS,
3242 if (port->lun_map == NULL)
3244 for (i = 0; i < CTL_MAX_LUNS; i++)
3245 port->lun_map[i] = UINT32_MAX;
3246 if (port->status & CTL_PORT_STATUS_ONLINE) {
3247 if (port->lun_disable != NULL) {
3248 STAILQ_FOREACH(lun, &softc->lun_list, links)
3249 port->lun_disable(port->targ_lun_arg, lun->lun);
3251 ctl_isc_announce_port(port);
3257 ctl_lun_map_deinit(struct ctl_port *port)
3259 struct ctl_softc *softc = control_softc;
3260 struct ctl_lun *lun;
3262 if (port->lun_map == NULL)
3264 free(port->lun_map, M_CTL);
3265 port->lun_map = NULL;
3266 if (port->status & CTL_PORT_STATUS_ONLINE) {
3267 if (port->lun_enable != NULL) {
3268 STAILQ_FOREACH(lun, &softc->lun_list, links)
3269 port->lun_enable(port->targ_lun_arg, lun->lun);
3271 ctl_isc_announce_port(port);
3277 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun)
3282 if (port->lun_map == NULL) {
3283 status = ctl_lun_map_init(port);
3287 old = port->lun_map[plun];
3288 port->lun_map[plun] = glun;
3289 if ((port->status & CTL_PORT_STATUS_ONLINE) && old >= CTL_MAX_LUNS) {
3290 if (port->lun_enable != NULL)
3291 port->lun_enable(port->targ_lun_arg, plun);
3292 ctl_isc_announce_port(port);
3298 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun)
3302 if (port->lun_map == NULL)
3304 old = port->lun_map[plun];
3305 port->lun_map[plun] = UINT32_MAX;
3306 if ((port->status & CTL_PORT_STATUS_ONLINE) && old < CTL_MAX_LUNS) {
3307 if (port->lun_disable != NULL)
3308 port->lun_disable(port->targ_lun_arg, plun);
3309 ctl_isc_announce_port(port);
3315 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
3319 return (UINT32_MAX);
3320 if (port->lun_map == NULL || lun_id >= CTL_MAX_LUNS)
3322 return (port->lun_map[lun_id]);
3326 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
3331 return (UINT32_MAX);
3332 if (port->lun_map == NULL)
3334 for (i = 0; i < CTL_MAX_LUNS; i++) {
3335 if (port->lun_map[i] == lun_id)
3338 return (UINT32_MAX);
3341 static struct ctl_port *
3342 ctl_io_port(struct ctl_io_hdr *io_hdr)
3345 return (control_softc->ctl_ports[io_hdr->nexus.targ_port]);
3349 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last)
3353 for (i = first; i < last; i++) {
3354 if ((mask[i / 32] & (1 << (i % 32))) == 0)
3361 ctl_set_mask(uint32_t *mask, uint32_t bit)
3363 uint32_t chunk, piece;
3366 piece = bit % (sizeof(uint32_t) * 8);
3368 if ((mask[chunk] & (1 << piece)) != 0)
3371 mask[chunk] |= (1 << piece);
3377 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3379 uint32_t chunk, piece;
3382 piece = bit % (sizeof(uint32_t) * 8);
3384 if ((mask[chunk] & (1 << piece)) == 0)
3387 mask[chunk] &= ~(1 << piece);
3393 ctl_is_set(uint32_t *mask, uint32_t bit)
3395 uint32_t chunk, piece;
3398 piece = bit % (sizeof(uint32_t) * 8);
3400 if ((mask[chunk] & (1 << piece)) == 0)
3407 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3411 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3414 return (t[residx % CTL_MAX_INIT_PER_PORT]);
3418 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3422 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3425 t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3429 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3434 i = residx/CTL_MAX_INIT_PER_PORT;
3435 if (lun->pr_keys[i] != NULL)
3437 mtx_unlock(&lun->lun_lock);
3438 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3440 mtx_lock(&lun->lun_lock);
3441 if (lun->pr_keys[i] == NULL)
3442 lun->pr_keys[i] = p;
3448 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3452 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3453 KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3454 t[residx % CTL_MAX_INIT_PER_PORT] = key;
3458 * ctl_softc, pool_name, total_ctl_io are passed in.
3459 * npool is passed out.
3462 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3463 uint32_t total_ctl_io, void **npool)
3466 struct ctl_io_pool *pool;
3468 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3473 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3474 pool->ctl_softc = ctl_softc;
3475 pool->zone = uma_zsecond_create(pool->name, NULL,
3476 NULL, NULL, NULL, ctl_softc->io_zone);
3477 /* uma_prealloc(pool->zone, total_ctl_io); */
3481 *npool = ctl_softc->io_zone;
3487 ctl_pool_free(struct ctl_io_pool *pool)
3494 uma_zdestroy(pool->zone);
3500 ctl_alloc_io(void *pool_ref)
3504 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3506 io = uma_zalloc(pool->zone, M_WAITOK);
3508 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK);
3511 io->io_hdr.pool = pool_ref;
3516 ctl_alloc_io_nowait(void *pool_ref)
3520 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3522 io = uma_zalloc(pool->zone, M_NOWAIT);
3524 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT);
3527 io->io_hdr.pool = pool_ref;
3532 ctl_free_io(union ctl_io *io)
3535 struct ctl_io_pool *pool;
3542 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3543 uma_zfree(pool->zone, io);
3545 uma_zfree((uma_zone_t)io->io_hdr.pool, io);
3550 ctl_zero_io(union ctl_io *io)
3558 * May need to preserve linked list pointers at some point too.
3560 pool_ref = io->io_hdr.pool;
3561 memset(io, 0, sizeof(*io));
3562 io->io_hdr.pool = pool_ref;
3566 * This routine is currently used for internal copies of ctl_ios that need
3567 * to persist for some reason after we've already returned status to the
3568 * FETD. (Thus the flag set.)
3571 * Note that this makes a blind copy of all fields in the ctl_io, except
3572 * for the pool reference. This includes any memory that has been
3573 * allocated! That memory will no longer be valid after done has been
3574 * called, so this would be VERY DANGEROUS for command that actually does
3575 * any reads or writes. Right now (11/7/2005), this is only used for immediate
3576 * start and stop commands, which don't transfer any data, so this is not a
3577 * problem. If it is used for anything else, the caller would also need to
3578 * allocate data buffer space and this routine would need to be modified to
3579 * copy the data buffer(s) as well.
3582 ctl_copy_io(union ctl_io *src, union ctl_io *dest)
3591 * May need to preserve linked list pointers at some point too.
3593 pool_ref = dest->io_hdr.pool;
3595 memcpy(dest, src, MIN(sizeof(*src), sizeof(*dest)));
3597 dest->io_hdr.pool = pool_ref;
3599 * We need to know that this is an internal copy, and doesn't need
3600 * to get passed back to the FETD that allocated it.
3602 dest->io_hdr.flags |= CTL_FLAG_INT_COPY;
3606 ctl_expand_number(const char *buf, uint64_t *num)
3612 number = strtoq(buf, &endptr, 0);
3614 switch (tolower((unsigned char)*endptr)) {
3634 case '\0': /* No unit. */
3638 /* Unrecognized unit. */
3642 if ((number << shift) >> shift != number) {
3646 *num = number << shift;
3652 * This routine could be used in the future to load default and/or saved
3653 * mode page parameters for a particuar lun.
3656 ctl_init_page_index(struct ctl_lun *lun)
3659 struct ctl_page_index *page_index;
3663 memcpy(&lun->mode_pages.index, page_index_template,
3664 sizeof(page_index_template));
3666 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3668 page_index = &lun->mode_pages.index[i];
3670 * If this is a disk-only mode page, there's no point in
3671 * setting it up. For some pages, we have to have some
3672 * basic information about the disk in order to calculate the
3675 if ((lun->be_lun->lun_type != T_DIRECT)
3676 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
3679 switch (page_index->page_code & SMPH_PC_MASK) {
3680 case SMS_RW_ERROR_RECOVERY_PAGE: {
3681 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3682 panic("subpage is incorrect!");
3683 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
3684 &rw_er_page_default,
3685 sizeof(rw_er_page_default));
3686 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
3687 &rw_er_page_changeable,
3688 sizeof(rw_er_page_changeable));
3689 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
3690 &rw_er_page_default,
3691 sizeof(rw_er_page_default));
3692 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
3693 &rw_er_page_default,
3694 sizeof(rw_er_page_default));
3695 page_index->page_data =
3696 (uint8_t *)lun->mode_pages.rw_er_page;
3699 case SMS_FORMAT_DEVICE_PAGE: {
3700 struct scsi_format_page *format_page;
3702 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3703 panic("subpage is incorrect!");
3706 * Sectors per track are set above. Bytes per
3707 * sector need to be set here on a per-LUN basis.
3709 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
3710 &format_page_default,
3711 sizeof(format_page_default));
3712 memcpy(&lun->mode_pages.format_page[
3713 CTL_PAGE_CHANGEABLE], &format_page_changeable,
3714 sizeof(format_page_changeable));
3715 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
3716 &format_page_default,
3717 sizeof(format_page_default));
3718 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
3719 &format_page_default,
3720 sizeof(format_page_default));
3722 format_page = &lun->mode_pages.format_page[
3724 scsi_ulto2b(lun->be_lun->blocksize,
3725 format_page->bytes_per_sector);
3727 format_page = &lun->mode_pages.format_page[
3729 scsi_ulto2b(lun->be_lun->blocksize,
3730 format_page->bytes_per_sector);
3732 format_page = &lun->mode_pages.format_page[
3734 scsi_ulto2b(lun->be_lun->blocksize,
3735 format_page->bytes_per_sector);
3737 page_index->page_data =
3738 (uint8_t *)lun->mode_pages.format_page;
3741 case SMS_RIGID_DISK_PAGE: {
3742 struct scsi_rigid_disk_page *rigid_disk_page;
3743 uint32_t sectors_per_cylinder;
3747 #endif /* !__XSCALE__ */
3749 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3750 panic("invalid subpage value %d",
3751 page_index->subpage);
3754 * Rotation rate and sectors per track are set
3755 * above. We calculate the cylinders here based on
3756 * capacity. Due to the number of heads and
3757 * sectors per track we're using, smaller arrays
3758 * may turn out to have 0 cylinders. Linux and
3759 * FreeBSD don't pay attention to these mode pages
3760 * to figure out capacity, but Solaris does. It
3761 * seems to deal with 0 cylinders just fine, and
3762 * works out a fake geometry based on the capacity.
3764 memcpy(&lun->mode_pages.rigid_disk_page[
3765 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
3766 sizeof(rigid_disk_page_default));
3767 memcpy(&lun->mode_pages.rigid_disk_page[
3768 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
3769 sizeof(rigid_disk_page_changeable));
3771 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
3775 * The divide method here will be more accurate,
3776 * probably, but results in floating point being
3777 * used in the kernel on i386 (__udivdi3()). On the
3778 * XScale, though, __udivdi3() is implemented in
3781 * The shift method for cylinder calculation is
3782 * accurate if sectors_per_cylinder is a power of
3783 * 2. Otherwise it might be slightly off -- you
3784 * might have a bit of a truncation problem.
3787 cylinders = (lun->be_lun->maxlba + 1) /
3788 sectors_per_cylinder;
3790 for (shift = 31; shift > 0; shift--) {
3791 if (sectors_per_cylinder & (1 << shift))
3794 cylinders = (lun->be_lun->maxlba + 1) >> shift;
3798 * We've basically got 3 bytes, or 24 bits for the
3799 * cylinder size in the mode page. If we're over,
3800 * just round down to 2^24.
3802 if (cylinders > 0xffffff)
3803 cylinders = 0xffffff;
3805 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
3807 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
3809 if ((value = ctl_get_opt(&lun->be_lun->options,
3811 scsi_ulto2b(strtol(value, NULL, 0),
3812 rigid_disk_page->rotation_rate);
3815 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
3816 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
3817 sizeof(rigid_disk_page_default));
3818 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
3819 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
3820 sizeof(rigid_disk_page_default));
3822 page_index->page_data =
3823 (uint8_t *)lun->mode_pages.rigid_disk_page;
3826 case SMS_CACHING_PAGE: {
3827 struct scsi_caching_page *caching_page;
3829 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3830 panic("invalid subpage value %d",
3831 page_index->subpage);
3832 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
3833 &caching_page_default,
3834 sizeof(caching_page_default));
3835 memcpy(&lun->mode_pages.caching_page[
3836 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
3837 sizeof(caching_page_changeable));
3838 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
3839 &caching_page_default,
3840 sizeof(caching_page_default));
3841 caching_page = &lun->mode_pages.caching_page[
3843 value = ctl_get_opt(&lun->be_lun->options, "writecache");
3844 if (value != NULL && strcmp(value, "off") == 0)
3845 caching_page->flags1 &= ~SCP_WCE;
3846 value = ctl_get_opt(&lun->be_lun->options, "readcache");
3847 if (value != NULL && strcmp(value, "off") == 0)
3848 caching_page->flags1 |= SCP_RCD;
3849 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
3850 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
3851 sizeof(caching_page_default));
3852 page_index->page_data =
3853 (uint8_t *)lun->mode_pages.caching_page;
3856 case SMS_CONTROL_MODE_PAGE: {
3857 struct scsi_control_page *control_page;
3859 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3860 panic("invalid subpage value %d",
3861 page_index->subpage);
3863 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT],
3864 &control_page_default,
3865 sizeof(control_page_default));
3866 memcpy(&lun->mode_pages.control_page[
3867 CTL_PAGE_CHANGEABLE], &control_page_changeable,
3868 sizeof(control_page_changeable));
3869 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED],
3870 &control_page_default,
3871 sizeof(control_page_default));
3872 control_page = &lun->mode_pages.control_page[
3874 value = ctl_get_opt(&lun->be_lun->options, "reordering");
3875 if (value != NULL && strcmp(value, "unrestricted") == 0) {
3876 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK;
3877 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED;
3879 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT],
3880 &lun->mode_pages.control_page[CTL_PAGE_SAVED],
3881 sizeof(control_page_default));
3882 page_index->page_data =
3883 (uint8_t *)lun->mode_pages.control_page;
3887 case SMS_INFO_EXCEPTIONS_PAGE: {
3888 switch (page_index->subpage) {
3889 case SMS_SUBPAGE_PAGE_0:
3890 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
3892 sizeof(ie_page_default));
3893 memcpy(&lun->mode_pages.ie_page[
3894 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
3895 sizeof(ie_page_changeable));
3896 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
3898 sizeof(ie_page_default));
3899 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
3901 sizeof(ie_page_default));
3902 page_index->page_data =
3903 (uint8_t *)lun->mode_pages.ie_page;
3906 struct ctl_logical_block_provisioning_page *page;
3908 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
3910 sizeof(lbp_page_default));
3911 memcpy(&lun->mode_pages.lbp_page[
3912 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
3913 sizeof(lbp_page_changeable));
3914 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
3916 sizeof(lbp_page_default));
3917 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
3918 value = ctl_get_opt(&lun->be_lun->options,
3920 if (value != NULL &&
3921 ctl_expand_number(value, &ival) == 0) {
3922 page->descr[0].flags |= SLBPPD_ENABLED |
3924 if (lun->be_lun->blocksize)
3925 ival /= lun->be_lun->blocksize;
3928 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
3929 page->descr[0].count);
3931 value = ctl_get_opt(&lun->be_lun->options,
3933 if (value != NULL &&
3934 ctl_expand_number(value, &ival) == 0) {
3935 page->descr[1].flags |= SLBPPD_ENABLED |
3937 if (lun->be_lun->blocksize)
3938 ival /= lun->be_lun->blocksize;
3941 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
3942 page->descr[1].count);
3944 value = ctl_get_opt(&lun->be_lun->options,
3945 "pool-avail-threshold");
3946 if (value != NULL &&
3947 ctl_expand_number(value, &ival) == 0) {
3948 page->descr[2].flags |= SLBPPD_ENABLED |
3950 if (lun->be_lun->blocksize)
3951 ival /= lun->be_lun->blocksize;
3954 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
3955 page->descr[2].count);
3957 value = ctl_get_opt(&lun->be_lun->options,
3958 "pool-used-threshold");
3959 if (value != NULL &&
3960 ctl_expand_number(value, &ival) == 0) {
3961 page->descr[3].flags |= SLBPPD_ENABLED |
3963 if (lun->be_lun->blocksize)
3964 ival /= lun->be_lun->blocksize;
3967 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
3968 page->descr[3].count);
3970 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
3971 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
3972 sizeof(lbp_page_default));
3973 page_index->page_data =
3974 (uint8_t *)lun->mode_pages.lbp_page;
3978 case SMS_VENDOR_SPECIFIC_PAGE:{
3979 switch (page_index->subpage) {
3980 case DBGCNF_SUBPAGE_CODE: {
3981 struct copan_debugconf_subpage *current_page,
3984 memcpy(&lun->mode_pages.debugconf_subpage[
3986 &debugconf_page_default,
3987 sizeof(debugconf_page_default));
3988 memcpy(&lun->mode_pages.debugconf_subpage[
3989 CTL_PAGE_CHANGEABLE],
3990 &debugconf_page_changeable,
3991 sizeof(debugconf_page_changeable));
3992 memcpy(&lun->mode_pages.debugconf_subpage[
3994 &debugconf_page_default,
3995 sizeof(debugconf_page_default));
3996 memcpy(&lun->mode_pages.debugconf_subpage[
3998 &debugconf_page_default,
3999 sizeof(debugconf_page_default));
4000 page_index->page_data =
4001 (uint8_t *)lun->mode_pages.debugconf_subpage;
4003 current_page = (struct copan_debugconf_subpage *)
4004 (page_index->page_data +
4005 (page_index->page_len *
4007 saved_page = (struct copan_debugconf_subpage *)
4008 (page_index->page_data +
4009 (page_index->page_len *
4014 panic("invalid subpage value %d",
4015 page_index->subpage);
4021 panic("invalid page value %d",
4022 page_index->page_code & SMPH_PC_MASK);
4027 return (CTL_RETVAL_COMPLETE);
4031 ctl_init_log_page_index(struct ctl_lun *lun)
4033 struct ctl_page_index *page_index;
4036 memcpy(&lun->log_pages.index, log_page_index_template,
4037 sizeof(log_page_index_template));
4040 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4042 page_index = &lun->log_pages.index[i];
4044 * If this is a disk-only mode page, there's no point in
4045 * setting it up. For some pages, we have to have some
4046 * basic information about the disk in order to calculate the
4049 if ((lun->be_lun->lun_type != T_DIRECT)
4050 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
4053 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4054 lun->backend->lun_attr == NULL)
4057 if (page_index->page_code != prev) {
4058 lun->log_pages.pages_page[j] = page_index->page_code;
4059 prev = page_index->page_code;
4062 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4063 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4066 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4067 lun->log_pages.index[0].page_len = j;
4068 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4069 lun->log_pages.index[1].page_len = k * 2;
4070 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4071 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4072 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page;
4073 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page);
4075 return (CTL_RETVAL_COMPLETE);
4079 hex2bin(const char *str, uint8_t *buf, int buf_size)
4084 memset(buf, 0, buf_size);
4085 while (isspace(str[0]))
4087 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4090 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4094 else if (isalpha(c))
4095 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4101 buf[i / 2] |= (c << 4);
4105 return ((i + 1) / 2);
4112 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4113 * wants us to allocate the LUN and he can block.
4114 * - ctl_softc is always set
4115 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4117 * Returns 0 for success, non-zero (errno) for failure.
4120 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4121 struct ctl_be_lun *const be_lun)
4123 struct ctl_lun *nlun, *lun;
4124 struct scsi_vpd_id_descriptor *desc;
4125 struct scsi_vpd_id_t10 *t10id;
4126 const char *eui, *naa, *scsiname, *vendor;
4127 int lun_number, i, lun_malloced;
4128 int devidlen, idlen1, idlen2 = 0, len;
4134 * We currently only support Direct Access or Processor LUN types.
4136 switch (be_lun->lun_type) {
4144 be_lun->lun_config_status(be_lun->be_lun,
4145 CTL_LUN_CONFIG_FAILURE);
4148 if (ctl_lun == NULL) {
4149 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4156 memset(lun, 0, sizeof(*lun));
4158 lun->flags = CTL_LUN_MALLOCED;
4160 /* Generate LUN ID. */
4161 devidlen = max(CTL_DEVID_MIN_LEN,
4162 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4163 idlen1 = sizeof(*t10id) + devidlen;
4164 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4165 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4166 if (scsiname != NULL) {
4167 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4168 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4170 eui = ctl_get_opt(&be_lun->options, "eui");
4172 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4174 naa = ctl_get_opt(&be_lun->options, "naa");
4176 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4178 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4179 M_CTL, M_WAITOK | M_ZERO);
4180 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4181 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4182 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4183 desc->length = idlen1;
4184 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4185 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4186 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4187 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4189 strncpy(t10id->vendor, vendor,
4190 min(sizeof(t10id->vendor), strlen(vendor)));
4192 strncpy((char *)t10id->vendor_spec_id,
4193 (char *)be_lun->device_id, devidlen);
4194 if (scsiname != NULL) {
4195 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4197 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4198 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4199 SVPD_ID_TYPE_SCSI_NAME;
4200 desc->length = idlen2;
4201 strlcpy(desc->identifier, scsiname, idlen2);
4204 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4206 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4207 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4209 desc->length = hex2bin(eui, desc->identifier, 16);
4210 desc->length = desc->length > 12 ? 16 :
4211 (desc->length > 8 ? 12 : 8);
4212 len -= 16 - desc->length;
4215 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4217 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4218 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4220 desc->length = hex2bin(naa, desc->identifier, 16);
4221 desc->length = desc->length > 8 ? 16 : 8;
4222 len -= 16 - desc->length;
4224 lun->lun_devid->len = len;
4226 mtx_lock(&ctl_softc->ctl_lock);
4228 * See if the caller requested a particular LUN number. If so, see
4229 * if it is available. Otherwise, allocate the first available LUN.
4231 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4232 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4233 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4234 mtx_unlock(&ctl_softc->ctl_lock);
4235 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4236 printf("ctl: requested LUN ID %d is higher "
4237 "than CTL_MAX_LUNS - 1 (%d)\n",
4238 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4241 * XXX KDM return an error, or just assign
4242 * another LUN ID in this case??
4244 printf("ctl: requested LUN ID %d is already "
4245 "in use\n", be_lun->req_lun_id);
4247 if (lun->flags & CTL_LUN_MALLOCED)
4249 be_lun->lun_config_status(be_lun->be_lun,
4250 CTL_LUN_CONFIG_FAILURE);
4253 lun_number = be_lun->req_lun_id;
4255 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, CTL_MAX_LUNS);
4256 if (lun_number == -1) {
4257 mtx_unlock(&ctl_softc->ctl_lock);
4258 printf("ctl: can't allocate LUN, out of LUNs\n");
4259 if (lun->flags & CTL_LUN_MALLOCED)
4261 be_lun->lun_config_status(be_lun->be_lun,
4262 CTL_LUN_CONFIG_FAILURE);
4266 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4268 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4269 lun->lun = lun_number;
4270 lun->be_lun = be_lun;
4272 * The processor LUN is always enabled. Disk LUNs come on line
4273 * disabled, and must be enabled by the backend.
4275 lun->flags |= CTL_LUN_DISABLED;
4276 lun->backend = be_lun->be;
4277 be_lun->ctl_lun = lun;
4278 be_lun->lun_id = lun_number;
4279 atomic_add_int(&be_lun->be->num_luns, 1);
4280 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE)
4281 lun->flags |= CTL_LUN_OFFLINE;
4283 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4284 lun->flags |= CTL_LUN_STOPPED;
4286 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4287 lun->flags |= CTL_LUN_INOPERABLE;
4289 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4290 lun->flags |= CTL_LUN_PRIMARY_SC;
4292 lun->ctl_softc = ctl_softc;
4294 lun->last_busy = getsbinuptime();
4296 TAILQ_INIT(&lun->ooa_queue);
4297 TAILQ_INIT(&lun->blocked_queue);
4298 STAILQ_INIT(&lun->error_list);
4299 ctl_tpc_lun_init(lun);
4302 * Initialize the mode and log page index.
4304 ctl_init_page_index(lun);
4305 ctl_init_log_page_index(lun);
4308 * Now, before we insert this lun on the lun list, set the lun
4309 * inventory changed UA for all other luns.
4311 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4312 mtx_lock(&nlun->lun_lock);
4313 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4314 mtx_unlock(&nlun->lun_lock);
4317 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4319 ctl_softc->ctl_luns[lun_number] = lun;
4321 ctl_softc->num_luns++;
4323 /* Setup statistics gathering */
4324 lun->stats.device_type = be_lun->lun_type;
4325 lun->stats.lun_number = lun_number;
4326 if (lun->stats.device_type == T_DIRECT)
4327 lun->stats.blocksize = be_lun->blocksize;
4329 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4330 for (i = 0;i < CTL_MAX_PORTS;i++)
4331 lun->stats.ports[i].targ_port = i;
4333 mtx_unlock(&ctl_softc->ctl_lock);
4335 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4342 * - LUN has already been marked invalid and any pending I/O has been taken
4346 ctl_free_lun(struct ctl_lun *lun)
4348 struct ctl_softc *softc;
4349 struct ctl_lun *nlun;
4352 softc = lun->ctl_softc;
4354 mtx_assert(&softc->ctl_lock, MA_OWNED);
4356 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4358 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4360 softc->ctl_luns[lun->lun] = NULL;
4362 if (!TAILQ_EMPTY(&lun->ooa_queue))
4363 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4368 * Tell the backend to free resources, if this LUN has a backend.
4370 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4371 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4373 ctl_tpc_lun_shutdown(lun);
4374 mtx_destroy(&lun->lun_lock);
4375 free(lun->lun_devid, M_CTL);
4376 for (i = 0; i < CTL_MAX_PORTS; i++)
4377 free(lun->pending_ua[i], M_CTL);
4378 for (i = 0; i < CTL_MAX_PORTS; i++)
4379 free(lun->pr_keys[i], M_CTL);
4380 free(lun->write_buffer, M_CTL);
4381 if (lun->flags & CTL_LUN_MALLOCED)
4384 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4385 mtx_lock(&nlun->lun_lock);
4386 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4387 mtx_unlock(&nlun->lun_lock);
4394 ctl_create_lun(struct ctl_be_lun *be_lun)
4396 struct ctl_softc *softc;
4398 softc = control_softc;
4401 * ctl_alloc_lun() should handle all potential failure cases.
4403 ctl_alloc_lun(softc, NULL, be_lun);
4407 ctl_add_lun(struct ctl_be_lun *be_lun)
4409 struct ctl_softc *softc = control_softc;
4411 mtx_lock(&softc->ctl_lock);
4412 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links);
4413 mtx_unlock(&softc->ctl_lock);
4414 wakeup(&softc->pending_lun_queue);
4420 ctl_enable_lun(struct ctl_be_lun *be_lun)
4422 struct ctl_softc *softc;
4423 struct ctl_port *port, *nport;
4424 struct ctl_lun *lun;
4427 lun = (struct ctl_lun *)be_lun->ctl_lun;
4428 softc = lun->ctl_softc;
4430 mtx_lock(&softc->ctl_lock);
4431 mtx_lock(&lun->lun_lock);
4432 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4434 * eh? Why did we get called if the LUN is already
4437 mtx_unlock(&lun->lun_lock);
4438 mtx_unlock(&softc->ctl_lock);
4441 lun->flags &= ~CTL_LUN_DISABLED;
4442 mtx_unlock(&lun->lun_lock);
4444 for (port = STAILQ_FIRST(&softc->port_list); port != NULL; port = nport) {
4445 nport = STAILQ_NEXT(port, links);
4446 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4447 port->lun_map != NULL || port->lun_enable == NULL)
4451 * Drop the lock while we call the FETD's enable routine.
4452 * This can lead to a callback into CTL (at least in the
4453 * case of the internal initiator frontend.
4455 mtx_unlock(&softc->ctl_lock);
4456 retval = port->lun_enable(port->targ_lun_arg, lun->lun);
4457 mtx_lock(&softc->ctl_lock);
4459 printf("%s: FETD %s port %d returned error "
4460 "%d for lun_enable on lun %jd\n",
4461 __func__, port->port_name, port->targ_port,
4462 retval, (intmax_t)lun->lun);
4466 mtx_unlock(&softc->ctl_lock);
4467 ctl_isc_announce_lun(lun);
4473 ctl_disable_lun(struct ctl_be_lun *be_lun)
4475 struct ctl_softc *softc;
4476 struct ctl_port *port;
4477 struct ctl_lun *lun;
4480 lun = (struct ctl_lun *)be_lun->ctl_lun;
4481 softc = lun->ctl_softc;
4483 mtx_lock(&softc->ctl_lock);
4484 mtx_lock(&lun->lun_lock);
4485 if (lun->flags & CTL_LUN_DISABLED) {
4486 mtx_unlock(&lun->lun_lock);
4487 mtx_unlock(&softc->ctl_lock);
4490 lun->flags |= CTL_LUN_DISABLED;
4491 mtx_unlock(&lun->lun_lock);
4493 STAILQ_FOREACH(port, &softc->port_list, links) {
4494 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4495 port->lun_map != NULL || port->lun_disable == NULL)
4499 * Drop the lock before we call the frontend's disable
4500 * routine, to avoid lock order reversals.
4502 * XXX KDM what happens if the frontend list changes while
4503 * we're traversing it? It's unlikely, but should be handled.
4505 mtx_unlock(&softc->ctl_lock);
4506 retval = port->lun_disable(port->targ_lun_arg, lun->lun);
4507 mtx_lock(&softc->ctl_lock);
4509 printf("%s: FETD %s port %d returned error "
4510 "%d for lun_disable on lun %jd\n",
4511 __func__, port->port_name, port->targ_port,
4512 retval, (intmax_t)lun->lun);
4516 mtx_unlock(&softc->ctl_lock);
4517 ctl_isc_announce_lun(lun);
4523 ctl_start_lun(struct ctl_be_lun *be_lun)
4525 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4527 mtx_lock(&lun->lun_lock);
4528 lun->flags &= ~CTL_LUN_STOPPED;
4529 mtx_unlock(&lun->lun_lock);
4534 ctl_stop_lun(struct ctl_be_lun *be_lun)
4536 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4538 mtx_lock(&lun->lun_lock);
4539 lun->flags |= CTL_LUN_STOPPED;
4540 mtx_unlock(&lun->lun_lock);
4545 ctl_lun_offline(struct ctl_be_lun *be_lun)
4547 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4549 mtx_lock(&lun->lun_lock);
4550 lun->flags |= CTL_LUN_OFFLINE;
4551 mtx_unlock(&lun->lun_lock);
4556 ctl_lun_online(struct ctl_be_lun *be_lun)
4558 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4560 mtx_lock(&lun->lun_lock);
4561 lun->flags &= ~CTL_LUN_OFFLINE;
4562 mtx_unlock(&lun->lun_lock);
4567 ctl_lun_primary(struct ctl_be_lun *be_lun)
4569 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4571 mtx_lock(&lun->lun_lock);
4572 lun->flags |= CTL_LUN_PRIMARY_SC;
4573 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4574 mtx_unlock(&lun->lun_lock);
4575 ctl_isc_announce_lun(lun);
4580 ctl_lun_secondary(struct ctl_be_lun *be_lun)
4582 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4584 mtx_lock(&lun->lun_lock);
4585 lun->flags &= ~CTL_LUN_PRIMARY_SC;
4586 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4587 mtx_unlock(&lun->lun_lock);
4588 ctl_isc_announce_lun(lun);
4593 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4595 struct ctl_softc *softc;
4596 struct ctl_lun *lun;
4598 lun = (struct ctl_lun *)be_lun->ctl_lun;
4599 softc = lun->ctl_softc;
4601 mtx_lock(&lun->lun_lock);
4604 * The LUN needs to be disabled before it can be marked invalid.
4606 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4607 mtx_unlock(&lun->lun_lock);
4611 * Mark the LUN invalid.
4613 lun->flags |= CTL_LUN_INVALID;
4616 * If there is nothing in the OOA queue, go ahead and free the LUN.
4617 * If we have something in the OOA queue, we'll free it when the
4618 * last I/O completes.
4620 if (TAILQ_EMPTY(&lun->ooa_queue)) {
4621 mtx_unlock(&lun->lun_lock);
4622 mtx_lock(&softc->ctl_lock);
4624 mtx_unlock(&softc->ctl_lock);
4626 mtx_unlock(&lun->lun_lock);
4632 ctl_lun_inoperable(struct ctl_be_lun *be_lun)
4634 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4636 mtx_lock(&lun->lun_lock);
4637 lun->flags |= CTL_LUN_INOPERABLE;
4638 mtx_unlock(&lun->lun_lock);
4643 ctl_lun_operable(struct ctl_be_lun *be_lun)
4645 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4647 mtx_lock(&lun->lun_lock);
4648 lun->flags &= ~CTL_LUN_INOPERABLE;
4649 mtx_unlock(&lun->lun_lock);
4654 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
4656 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4657 union ctl_ha_msg msg;
4659 mtx_lock(&lun->lun_lock);
4660 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGED);
4661 mtx_unlock(&lun->lun_lock);
4662 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4663 /* Send msg to other side. */
4664 bzero(&msg.ua, sizeof(msg.ua));
4665 msg.hdr.msg_type = CTL_MSG_UA;
4666 msg.hdr.nexus.initid = -1;
4667 msg.hdr.nexus.targ_port = -1;
4668 msg.hdr.nexus.targ_lun = lun->lun;
4669 msg.hdr.nexus.targ_mapped_lun = lun->lun;
4672 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGED;
4673 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4679 * Backend "memory move is complete" callback for requests that never
4680 * make it down to say RAIDCore's configuration code.
4683 ctl_config_move_done(union ctl_io *io)
4687 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
4688 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
4689 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
4691 if ((io->io_hdr.port_status != 0) &&
4692 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
4693 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
4695 * For hardware error sense keys, the sense key
4696 * specific value is defined to be a retry count,
4697 * but we use it to pass back an internal FETD
4698 * error code. XXX KDM Hopefully the FETD is only
4699 * using 16 bits for an error code, since that's
4700 * all the space we have in the sks field.
4702 ctl_set_internal_failure(&io->scsiio,
4705 io->io_hdr.port_status);
4708 if (ctl_debug & CTL_DEBUG_CDB_DATA)
4710 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
4711 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
4712 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
4713 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
4715 * XXX KDM just assuming a single pointer here, and not a
4716 * S/G list. If we start using S/G lists for config data,
4717 * we'll need to know how to clean them up here as well.
4719 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
4720 free(io->scsiio.kern_data_ptr, M_CTL);
4722 retval = CTL_RETVAL_COMPLETE;
4725 * XXX KDM now we need to continue data movement. Some
4727 * - call ctl_scsiio() again? We don't do this for data
4728 * writes, because for those at least we know ahead of
4729 * time where the write will go and how long it is. For
4730 * config writes, though, that information is largely
4731 * contained within the write itself, thus we need to
4732 * parse out the data again.
4734 * - Call some other function once the data is in?
4738 * XXX KDM call ctl_scsiio() again for now, and check flag
4739 * bits to see whether we're allocated or not.
4741 retval = ctl_scsiio(&io->scsiio);
4747 * This gets called by a backend driver when it is done with a
4748 * data_submit method.
4751 ctl_data_submit_done(union ctl_io *io)
4754 * If the IO_CONT flag is set, we need to call the supplied
4755 * function to continue processing the I/O, instead of completing
4758 * If there is an error, though, we don't want to keep processing.
4759 * Instead, just send status back to the initiator.
4761 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
4762 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
4763 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
4764 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
4765 io->scsiio.io_cont(io);
4772 * This gets called by a backend driver when it is done with a
4773 * configuration write.
4776 ctl_config_write_done(union ctl_io *io)
4781 * If the IO_CONT flag is set, we need to call the supplied
4782 * function to continue processing the I/O, instead of completing
4785 * If there is an error, though, we don't want to keep processing.
4786 * Instead, just send status back to the initiator.
4788 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
4789 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
4790 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
4791 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
4792 io->scsiio.io_cont(io);
4796 * Since a configuration write can be done for commands that actually
4797 * have data allocated, like write buffer, and commands that have
4798 * no data, like start/stop unit, we need to check here.
4800 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
4801 buf = io->scsiio.kern_data_ptr;
4810 ctl_config_read_done(union ctl_io *io)
4815 * If there is some error -- we are done, skip data transfer.
4817 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
4818 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
4819 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
4820 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
4821 buf = io->scsiio.kern_data_ptr;
4831 * If the IO_CONT flag is set, we need to call the supplied
4832 * function to continue processing the I/O, instead of completing
4835 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
4836 io->scsiio.io_cont(io);
4844 * SCSI release command.
4847 ctl_scsi_release(struct ctl_scsiio *ctsio)
4849 int length, longid, thirdparty_id, resv_id;
4850 struct ctl_lun *lun;
4856 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
4858 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
4859 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
4861 switch (ctsio->cdb[0]) {
4863 struct scsi_release_10 *cdb;
4865 cdb = (struct scsi_release_10 *)ctsio->cdb;
4867 if (cdb->byte2 & SR10_LONGID)
4870 thirdparty_id = cdb->thirdparty_id;
4872 resv_id = cdb->resv_id;
4873 length = scsi_2btoul(cdb->length);
4880 * XXX KDM right now, we only support LUN reservation. We don't
4881 * support 3rd party reservations, or extent reservations, which
4882 * might actually need the parameter list. If we've gotten this
4883 * far, we've got a LUN reservation. Anything else got kicked out
4884 * above. So, according to SPC, ignore the length.
4888 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
4890 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
4891 ctsio->kern_data_len = length;
4892 ctsio->kern_total_len = length;
4893 ctsio->kern_data_resid = 0;
4894 ctsio->kern_rel_offset = 0;
4895 ctsio->kern_sg_entries = 0;
4896 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
4897 ctsio->be_move_done = ctl_config_move_done;
4898 ctl_datamove((union ctl_io *)ctsio);
4900 return (CTL_RETVAL_COMPLETE);
4904 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
4906 mtx_lock(&lun->lun_lock);
4909 * According to SPC, it is not an error for an intiator to attempt
4910 * to release a reservation on a LUN that isn't reserved, or that
4911 * is reserved by another initiator. The reservation can only be
4912 * released, though, by the initiator who made it or by one of
4913 * several reset type events.
4915 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
4916 lun->flags &= ~CTL_LUN_RESERVED;
4918 mtx_unlock(&lun->lun_lock);
4920 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
4921 free(ctsio->kern_data_ptr, M_CTL);
4922 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
4925 ctl_set_success(ctsio);
4926 ctl_done((union ctl_io *)ctsio);
4927 return (CTL_RETVAL_COMPLETE);
4931 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
4933 int extent, thirdparty, longid;
4934 int resv_id, length;
4935 uint64_t thirdparty_id;
4936 struct ctl_lun *lun;
4946 CTL_DEBUG_PRINT(("ctl_reserve\n"));
4948 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
4949 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
4951 switch (ctsio->cdb[0]) {
4953 struct scsi_reserve_10 *cdb;
4955 cdb = (struct scsi_reserve_10 *)ctsio->cdb;
4957 if (cdb->byte2 & SR10_LONGID)
4960 thirdparty_id = cdb->thirdparty_id;
4962 resv_id = cdb->resv_id;
4963 length = scsi_2btoul(cdb->length);
4969 * XXX KDM right now, we only support LUN reservation. We don't
4970 * support 3rd party reservations, or extent reservations, which
4971 * might actually need the parameter list. If we've gotten this
4972 * far, we've got a LUN reservation. Anything else got kicked out
4973 * above. So, according to SPC, ignore the length.
4977 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
4979 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
4980 ctsio->kern_data_len = length;
4981 ctsio->kern_total_len = length;
4982 ctsio->kern_data_resid = 0;
4983 ctsio->kern_rel_offset = 0;
4984 ctsio->kern_sg_entries = 0;
4985 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
4986 ctsio->be_move_done = ctl_config_move_done;
4987 ctl_datamove((union ctl_io *)ctsio);
4989 return (CTL_RETVAL_COMPLETE);
4993 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
4995 mtx_lock(&lun->lun_lock);
4996 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
4997 ctl_set_reservation_conflict(ctsio);
5001 lun->flags |= CTL_LUN_RESERVED;
5002 lun->res_idx = residx;
5004 ctl_set_success(ctsio);
5007 mtx_unlock(&lun->lun_lock);
5009 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5010 free(ctsio->kern_data_ptr, M_CTL);
5011 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5014 ctl_done((union ctl_io *)ctsio);
5015 return (CTL_RETVAL_COMPLETE);
5019 ctl_start_stop(struct ctl_scsiio *ctsio)
5021 struct scsi_start_stop_unit *cdb;
5022 struct ctl_lun *lun;
5025 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5027 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5030 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5034 * We don't support the immediate bit on a stop unit. In order to
5035 * do that, we would need to code up a way to know that a stop is
5036 * pending, and hold off any new commands until it completes, one
5037 * way or another. Then we could accept or reject those commands
5038 * depending on its status. We would almost need to do the reverse
5039 * of what we do below for an immediate start -- return the copy of
5040 * the ctl_io to the FETD with status to send to the host (and to
5041 * free the copy!) and then free the original I/O once the stop
5042 * actually completes. That way, the OOA queue mechanism can work
5043 * to block commands that shouldn't proceed. Another alternative
5044 * would be to put the copy in the queue in place of the original,
5045 * and return the original back to the caller. That could be
5048 if ((cdb->byte2 & SSS_IMMED)
5049 && ((cdb->how & SSS_START) == 0)) {
5050 ctl_set_invalid_field(ctsio,
5056 ctl_done((union ctl_io *)ctsio);
5057 return (CTL_RETVAL_COMPLETE);
5060 if ((lun->flags & CTL_LUN_PR_RESERVED)
5061 && ((cdb->how & SSS_START)==0)) {
5064 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5065 if (ctl_get_prkey(lun, residx) == 0
5066 || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5068 ctl_set_reservation_conflict(ctsio);
5069 ctl_done((union ctl_io *)ctsio);
5070 return (CTL_RETVAL_COMPLETE);
5075 * If there is no backend on this device, we can't start or stop
5076 * it. In theory we shouldn't get any start/stop commands in the
5077 * first place at this level if the LUN doesn't have a backend.
5078 * That should get stopped by the command decode code.
5080 if (lun->backend == NULL) {
5081 ctl_set_invalid_opcode(ctsio);
5082 ctl_done((union ctl_io *)ctsio);
5083 return (CTL_RETVAL_COMPLETE);
5087 * XXX KDM Copan-specific offline behavior.
5088 * Figure out a reasonable way to port this?
5091 mtx_lock(&lun->lun_lock);
5093 if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
5094 && (lun->flags & CTL_LUN_OFFLINE)) {
5096 * If the LUN is offline, and the on/offline bit isn't set,
5097 * reject the start or stop. Otherwise, let it through.
5099 mtx_unlock(&lun->lun_lock);
5100 ctl_set_lun_not_ready(ctsio);
5101 ctl_done((union ctl_io *)ctsio);
5103 mtx_unlock(&lun->lun_lock);
5104 #endif /* NEEDTOPORT */
5106 * This could be a start or a stop when we're online,
5107 * or a stop/offline or start/online. A start or stop when
5108 * we're offline is covered in the case above.
5111 * In the non-immediate case, we send the request to
5112 * the backend and return status to the user when
5115 * In the immediate case, we allocate a new ctl_io
5116 * to hold a copy of the request, and send that to
5117 * the backend. We then set good status on the
5118 * user's request and return it immediately.
5120 if (cdb->byte2 & SSS_IMMED) {
5121 union ctl_io *new_io;
5123 new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5124 ctl_copy_io((union ctl_io *)ctsio, new_io);
5125 retval = lun->backend->config_write(new_io);
5126 ctl_set_success(ctsio);
5127 ctl_done((union ctl_io *)ctsio);
5129 retval = lun->backend->config_write(
5130 (union ctl_io *)ctsio);
5139 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5140 * we don't really do anything with the LBA and length fields if the user
5141 * passes them in. Instead we'll just flush out the cache for the entire
5145 ctl_sync_cache(struct ctl_scsiio *ctsio)
5147 struct ctl_lun *lun;
5148 struct ctl_softc *softc;
5149 struct ctl_lba_len_flags *lbalen;
5150 uint64_t starting_lba;
5151 uint32_t block_count;
5155 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5157 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5158 softc = lun->ctl_softc;
5161 switch (ctsio->cdb[0]) {
5162 case SYNCHRONIZE_CACHE: {
5163 struct scsi_sync_cache *cdb;
5164 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5166 starting_lba = scsi_4btoul(cdb->begin_lba);
5167 block_count = scsi_2btoul(cdb->lb_count);
5171 case SYNCHRONIZE_CACHE_16: {
5172 struct scsi_sync_cache_16 *cdb;
5173 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5175 starting_lba = scsi_8btou64(cdb->begin_lba);
5176 block_count = scsi_4btoul(cdb->lb_count);
5181 ctl_set_invalid_opcode(ctsio);
5182 ctl_done((union ctl_io *)ctsio);
5184 break; /* NOTREACHED */
5188 * We check the LBA and length, but don't do anything with them.
5189 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5190 * get flushed. This check will just help satisfy anyone who wants
5191 * to see an error for an out of range LBA.
5193 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5194 ctl_set_lba_out_of_range(ctsio);
5195 ctl_done((union ctl_io *)ctsio);
5200 * If this LUN has no backend, we can't flush the cache anyway.
5202 if (lun->backend == NULL) {
5203 ctl_set_invalid_opcode(ctsio);
5204 ctl_done((union ctl_io *)ctsio);
5208 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5209 lbalen->lba = starting_lba;
5210 lbalen->len = block_count;
5211 lbalen->flags = byte2;
5214 * Check to see whether we're configured to send the SYNCHRONIZE
5215 * CACHE command directly to the back end.
5217 mtx_lock(&lun->lun_lock);
5218 if ((softc->flags & CTL_FLAG_REAL_SYNC)
5219 && (++(lun->sync_count) >= lun->sync_interval)) {
5220 lun->sync_count = 0;
5221 mtx_unlock(&lun->lun_lock);
5222 retval = lun->backend->config_write((union ctl_io *)ctsio);
5224 mtx_unlock(&lun->lun_lock);
5225 ctl_set_success(ctsio);
5226 ctl_done((union ctl_io *)ctsio);
5235 ctl_format(struct ctl_scsiio *ctsio)
5237 struct scsi_format *cdb;
5238 struct ctl_lun *lun;
5239 int length, defect_list_len;
5241 CTL_DEBUG_PRINT(("ctl_format\n"));
5243 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5245 cdb = (struct scsi_format *)ctsio->cdb;
5248 if (cdb->byte2 & SF_FMTDATA) {
5249 if (cdb->byte2 & SF_LONGLIST)
5250 length = sizeof(struct scsi_format_header_long);
5252 length = sizeof(struct scsi_format_header_short);
5255 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5257 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5258 ctsio->kern_data_len = length;
5259 ctsio->kern_total_len = length;
5260 ctsio->kern_data_resid = 0;
5261 ctsio->kern_rel_offset = 0;
5262 ctsio->kern_sg_entries = 0;
5263 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5264 ctsio->be_move_done = ctl_config_move_done;
5265 ctl_datamove((union ctl_io *)ctsio);
5267 return (CTL_RETVAL_COMPLETE);
5270 defect_list_len = 0;
5272 if (cdb->byte2 & SF_FMTDATA) {
5273 if (cdb->byte2 & SF_LONGLIST) {
5274 struct scsi_format_header_long *header;
5276 header = (struct scsi_format_header_long *)
5277 ctsio->kern_data_ptr;
5279 defect_list_len = scsi_4btoul(header->defect_list_len);
5280 if (defect_list_len != 0) {
5281 ctl_set_invalid_field(ctsio,
5290 struct scsi_format_header_short *header;
5292 header = (struct scsi_format_header_short *)
5293 ctsio->kern_data_ptr;
5295 defect_list_len = scsi_2btoul(header->defect_list_len);
5296 if (defect_list_len != 0) {
5297 ctl_set_invalid_field(ctsio,
5309 * The format command will clear out the "Medium format corrupted"
5310 * status if set by the configuration code. That status is really
5311 * just a way to notify the host that we have lost the media, and
5312 * get them to issue a command that will basically make them think
5313 * they're blowing away the media.
5315 mtx_lock(&lun->lun_lock);
5316 lun->flags &= ~CTL_LUN_INOPERABLE;
5317 mtx_unlock(&lun->lun_lock);
5319 ctl_set_success(ctsio);
5322 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5323 free(ctsio->kern_data_ptr, M_CTL);
5324 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5327 ctl_done((union ctl_io *)ctsio);
5328 return (CTL_RETVAL_COMPLETE);
5332 ctl_read_buffer(struct ctl_scsiio *ctsio)
5334 struct scsi_read_buffer *cdb;
5335 struct ctl_lun *lun;
5336 int buffer_offset, len;
5337 static uint8_t descr[4];
5338 static uint8_t echo_descr[4] = { 0 };
5340 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5342 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5343 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5345 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA &&
5346 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR &&
5347 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) {
5348 ctl_set_invalid_field(ctsio,
5354 ctl_done((union ctl_io *)ctsio);
5355 return (CTL_RETVAL_COMPLETE);
5358 len = scsi_3btoul(cdb->length);
5359 buffer_offset = scsi_3btoul(cdb->offset);
5361 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5362 ctl_set_invalid_field(ctsio,
5368 ctl_done((union ctl_io *)ctsio);
5369 return (CTL_RETVAL_COMPLETE);
5372 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5374 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5375 ctsio->kern_data_ptr = descr;
5376 len = min(len, sizeof(descr));
5377 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5378 ctsio->kern_data_ptr = echo_descr;
5379 len = min(len, sizeof(echo_descr));
5381 if (lun->write_buffer == NULL) {
5382 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5385 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5387 ctsio->kern_data_len = len;
5388 ctsio->kern_total_len = len;
5389 ctsio->kern_data_resid = 0;
5390 ctsio->kern_rel_offset = 0;
5391 ctsio->kern_sg_entries = 0;
5392 ctl_set_success(ctsio);
5393 ctsio->be_move_done = ctl_config_move_done;
5394 ctl_datamove((union ctl_io *)ctsio);
5395 return (CTL_RETVAL_COMPLETE);
5399 ctl_write_buffer(struct ctl_scsiio *ctsio)
5401 struct scsi_write_buffer *cdb;
5402 struct ctl_lun *lun;
5403 int buffer_offset, len;
5405 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5407 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5408 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5410 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5411 ctl_set_invalid_field(ctsio,
5417 ctl_done((union ctl_io *)ctsio);
5418 return (CTL_RETVAL_COMPLETE);
5421 len = scsi_3btoul(cdb->length);
5422 buffer_offset = scsi_3btoul(cdb->offset);
5424 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5425 ctl_set_invalid_field(ctsio,
5431 ctl_done((union ctl_io *)ctsio);
5432 return (CTL_RETVAL_COMPLETE);
5436 * If we've got a kernel request that hasn't been malloced yet,
5437 * malloc it and tell the caller the data buffer is here.
5439 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5440 if (lun->write_buffer == NULL) {
5441 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5444 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5445 ctsio->kern_data_len = len;
5446 ctsio->kern_total_len = len;
5447 ctsio->kern_data_resid = 0;
5448 ctsio->kern_rel_offset = 0;
5449 ctsio->kern_sg_entries = 0;
5450 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5451 ctsio->be_move_done = ctl_config_move_done;
5452 ctl_datamove((union ctl_io *)ctsio);
5454 return (CTL_RETVAL_COMPLETE);
5457 ctl_set_success(ctsio);
5458 ctl_done((union ctl_io *)ctsio);
5459 return (CTL_RETVAL_COMPLETE);
5463 ctl_write_same(struct ctl_scsiio *ctsio)
5465 struct ctl_lun *lun;
5466 struct ctl_lba_len_flags *lbalen;
5468 uint32_t num_blocks;
5472 retval = CTL_RETVAL_COMPLETE;
5474 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5476 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5478 switch (ctsio->cdb[0]) {
5479 case WRITE_SAME_10: {
5480 struct scsi_write_same_10 *cdb;
5482 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5484 lba = scsi_4btoul(cdb->addr);
5485 num_blocks = scsi_2btoul(cdb->length);
5489 case WRITE_SAME_16: {
5490 struct scsi_write_same_16 *cdb;
5492 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5494 lba = scsi_8btou64(cdb->addr);
5495 num_blocks = scsi_4btoul(cdb->length);
5501 * We got a command we don't support. This shouldn't
5502 * happen, commands should be filtered out above us.
5504 ctl_set_invalid_opcode(ctsio);
5505 ctl_done((union ctl_io *)ctsio);
5507 return (CTL_RETVAL_COMPLETE);
5508 break; /* NOTREACHED */
5511 /* NDOB and ANCHOR flags can be used only together with UNMAP */
5512 if ((byte2 & SWS_UNMAP) == 0 &&
5513 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) {
5514 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5515 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5516 ctl_done((union ctl_io *)ctsio);
5517 return (CTL_RETVAL_COMPLETE);
5521 * The first check is to make sure we're in bounds, the second
5522 * check is to catch wrap-around problems. If the lba + num blocks
5523 * is less than the lba, then we've wrapped around and the block
5524 * range is invalid anyway.
5526 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5527 || ((lba + num_blocks) < lba)) {
5528 ctl_set_lba_out_of_range(ctsio);
5529 ctl_done((union ctl_io *)ctsio);
5530 return (CTL_RETVAL_COMPLETE);
5533 /* Zero number of blocks means "to the last logical block" */
5534 if (num_blocks == 0) {
5535 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5536 ctl_set_invalid_field(ctsio,
5542 ctl_done((union ctl_io *)ctsio);
5543 return (CTL_RETVAL_COMPLETE);
5545 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5548 len = lun->be_lun->blocksize;
5551 * If we've got a kernel request that hasn't been malloced yet,
5552 * malloc it and tell the caller the data buffer is here.
5554 if ((byte2 & SWS_NDOB) == 0 &&
5555 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5556 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5557 ctsio->kern_data_len = len;
5558 ctsio->kern_total_len = len;
5559 ctsio->kern_data_resid = 0;
5560 ctsio->kern_rel_offset = 0;
5561 ctsio->kern_sg_entries = 0;
5562 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5563 ctsio->be_move_done = ctl_config_move_done;
5564 ctl_datamove((union ctl_io *)ctsio);
5566 return (CTL_RETVAL_COMPLETE);
5569 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5571 lbalen->len = num_blocks;
5572 lbalen->flags = byte2;
5573 retval = lun->backend->config_write((union ctl_io *)ctsio);
5579 ctl_unmap(struct ctl_scsiio *ctsio)
5581 struct ctl_lun *lun;
5582 struct scsi_unmap *cdb;
5583 struct ctl_ptr_len_flags *ptrlen;
5584 struct scsi_unmap_header *hdr;
5585 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5587 uint32_t num_blocks;
5591 retval = CTL_RETVAL_COMPLETE;
5593 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5595 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5596 cdb = (struct scsi_unmap *)ctsio->cdb;
5598 len = scsi_2btoul(cdb->length);
5602 * If we've got a kernel request that hasn't been malloced yet,
5603 * malloc it and tell the caller the data buffer is here.
5605 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5606 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5607 ctsio->kern_data_len = len;
5608 ctsio->kern_total_len = len;
5609 ctsio->kern_data_resid = 0;
5610 ctsio->kern_rel_offset = 0;
5611 ctsio->kern_sg_entries = 0;
5612 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5613 ctsio->be_move_done = ctl_config_move_done;
5614 ctl_datamove((union ctl_io *)ctsio);
5616 return (CTL_RETVAL_COMPLETE);
5619 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5620 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5621 if (len < sizeof (*hdr) ||
5622 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5623 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5624 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5625 ctl_set_invalid_field(ctsio,
5633 len = scsi_2btoul(hdr->desc_length);
5634 buf = (struct scsi_unmap_desc *)(hdr + 1);
5635 end = buf + len / sizeof(*buf);
5638 for (range = buf; range < end; range++) {
5639 lba = scsi_8btou64(range->lba);
5640 num_blocks = scsi_4btoul(range->length);
5641 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5642 || ((lba + num_blocks) < lba)) {
5643 ctl_set_lba_out_of_range(ctsio);
5644 ctl_done((union ctl_io *)ctsio);
5645 return (CTL_RETVAL_COMPLETE);
5647 if (num_blocks != 0)
5652 * Block backend can not handle zero last range.
5653 * Filter it out and return if there is nothing left.
5655 len = (uint8_t *)endnz - (uint8_t *)buf;
5657 ctl_set_success(ctsio);
5661 mtx_lock(&lun->lun_lock);
5662 ptrlen = (struct ctl_ptr_len_flags *)
5663 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5664 ptrlen->ptr = (void *)buf;
5666 ptrlen->flags = byte2;
5667 ctl_check_blocked(lun);
5668 mtx_unlock(&lun->lun_lock);
5670 retval = lun->backend->config_write((union ctl_io *)ctsio);
5674 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5675 free(ctsio->kern_data_ptr, M_CTL);
5676 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5678 ctl_done((union ctl_io *)ctsio);
5679 return (CTL_RETVAL_COMPLETE);
5683 * Note that this function currently doesn't actually do anything inside
5684 * CTL to enforce things if the DQue bit is turned on.
5686 * Also note that this function can't be used in the default case, because
5687 * the DQue bit isn't set in the changeable mask for the control mode page
5688 * anyway. This is just here as an example for how to implement a page
5689 * handler, and a placeholder in case we want to allow the user to turn
5690 * tagged queueing on and off.
5692 * The D_SENSE bit handling is functional, however, and will turn
5693 * descriptor sense on and off for a given LUN.
5696 ctl_control_page_handler(struct ctl_scsiio *ctsio,
5697 struct ctl_page_index *page_index, uint8_t *page_ptr)
5699 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
5700 struct ctl_lun *lun;
5704 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5705 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5708 user_cp = (struct scsi_control_page *)page_ptr;
5709 current_cp = (struct scsi_control_page *)
5710 (page_index->page_data + (page_index->page_len *
5712 saved_cp = (struct scsi_control_page *)
5713 (page_index->page_data + (page_index->page_len *
5716 mtx_lock(&lun->lun_lock);
5717 if (((current_cp->rlec & SCP_DSENSE) == 0)
5718 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
5720 * Descriptor sense is currently turned off and the user
5721 * wants to turn it on.
5723 current_cp->rlec |= SCP_DSENSE;
5724 saved_cp->rlec |= SCP_DSENSE;
5725 lun->flags |= CTL_LUN_SENSE_DESC;
5727 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
5728 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
5730 * Descriptor sense is currently turned on, and the user
5731 * wants to turn it off.
5733 current_cp->rlec &= ~SCP_DSENSE;
5734 saved_cp->rlec &= ~SCP_DSENSE;
5735 lun->flags &= ~CTL_LUN_SENSE_DESC;
5738 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) !=
5739 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) {
5740 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
5741 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
5742 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
5743 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
5746 if ((current_cp->eca_and_aen & SCP_SWP) !=
5747 (user_cp->eca_and_aen & SCP_SWP)) {
5748 current_cp->eca_and_aen &= ~SCP_SWP;
5749 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
5750 saved_cp->eca_and_aen &= ~SCP_SWP;
5751 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
5755 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5756 mtx_unlock(&lun->lun_lock);
5762 ctl_caching_sp_handler(struct ctl_scsiio *ctsio,
5763 struct ctl_page_index *page_index, uint8_t *page_ptr)
5765 struct scsi_caching_page *current_cp, *saved_cp, *user_cp;
5766 struct ctl_lun *lun;
5770 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5771 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5774 user_cp = (struct scsi_caching_page *)page_ptr;
5775 current_cp = (struct scsi_caching_page *)
5776 (page_index->page_data + (page_index->page_len *
5778 saved_cp = (struct scsi_caching_page *)
5779 (page_index->page_data + (page_index->page_len *
5782 mtx_lock(&lun->lun_lock);
5783 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) !=
5784 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) {
5785 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
5786 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
5787 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
5788 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
5792 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5793 mtx_unlock(&lun->lun_lock);
5799 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
5800 struct ctl_page_index *page_index,
5806 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
5811 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
5812 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
5813 printf("page data:");
5815 printf(" %.2x",page_ptr[i]);
5821 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
5822 struct ctl_page_index *page_index,
5825 struct copan_debugconf_subpage *page;
5827 page = (struct copan_debugconf_subpage *)page_index->page_data +
5828 (page_index->page_len * pc);
5831 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
5832 case SMS_PAGE_CTRL_DEFAULT >> 6:
5833 case SMS_PAGE_CTRL_SAVED >> 6:
5835 * We don't update the changable or default bits for this page.
5838 case SMS_PAGE_CTRL_CURRENT >> 6:
5839 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
5840 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
5844 EPRINT(0, "Invalid PC %d!!", pc);
5845 #endif /* NEEDTOPORT */
5853 ctl_do_mode_select(union ctl_io *io)
5855 struct scsi_mode_page_header *page_header;
5856 struct ctl_page_index *page_index;
5857 struct ctl_scsiio *ctsio;
5858 int control_dev, page_len;
5859 int page_len_offset, page_len_size;
5860 union ctl_modepage_info *modepage_info;
5861 struct ctl_lun *lun;
5862 int *len_left, *len_used;
5865 ctsio = &io->scsiio;
5868 retval = CTL_RETVAL_COMPLETE;
5870 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5872 if (lun->be_lun->lun_type != T_DIRECT)
5877 modepage_info = (union ctl_modepage_info *)
5878 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
5879 len_left = &modepage_info->header.len_left;
5880 len_used = &modepage_info->header.len_used;
5884 page_header = (struct scsi_mode_page_header *)
5885 (ctsio->kern_data_ptr + *len_used);
5887 if (*len_left == 0) {
5888 free(ctsio->kern_data_ptr, M_CTL);
5889 ctl_set_success(ctsio);
5890 ctl_done((union ctl_io *)ctsio);
5891 return (CTL_RETVAL_COMPLETE);
5892 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
5894 free(ctsio->kern_data_ptr, M_CTL);
5895 ctl_set_param_len_error(ctsio);
5896 ctl_done((union ctl_io *)ctsio);
5897 return (CTL_RETVAL_COMPLETE);
5899 } else if ((page_header->page_code & SMPH_SPF)
5900 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
5902 free(ctsio->kern_data_ptr, M_CTL);
5903 ctl_set_param_len_error(ctsio);
5904 ctl_done((union ctl_io *)ctsio);
5905 return (CTL_RETVAL_COMPLETE);
5910 * XXX KDM should we do something with the block descriptor?
5912 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
5914 if ((control_dev != 0)
5915 && (lun->mode_pages.index[i].page_flags &
5916 CTL_PAGE_FLAG_DISK_ONLY))
5919 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
5920 (page_header->page_code & SMPH_PC_MASK))
5924 * If neither page has a subpage code, then we've got a
5927 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
5928 && ((page_header->page_code & SMPH_SPF) == 0)) {
5929 page_index = &lun->mode_pages.index[i];
5930 page_len = page_header->page_length;
5935 * If both pages have subpages, then the subpage numbers
5938 if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
5939 && (page_header->page_code & SMPH_SPF)) {
5940 struct scsi_mode_page_header_sp *sph;
5942 sph = (struct scsi_mode_page_header_sp *)page_header;
5944 if (lun->mode_pages.index[i].subpage ==
5946 page_index = &lun->mode_pages.index[i];
5947 page_len = scsi_2btoul(sph->page_length);
5954 * If we couldn't find the page, or if we don't have a mode select
5955 * handler for it, send back an error to the user.
5957 if ((page_index == NULL)
5958 || (page_index->select_handler == NULL)) {
5959 ctl_set_invalid_field(ctsio,
5962 /*field*/ *len_used,
5965 free(ctsio->kern_data_ptr, M_CTL);
5966 ctl_done((union ctl_io *)ctsio);
5967 return (CTL_RETVAL_COMPLETE);
5970 if (page_index->page_code & SMPH_SPF) {
5971 page_len_offset = 2;
5975 page_len_offset = 1;
5979 * If the length the initiator gives us isn't the one we specify in
5980 * the mode page header, or if they didn't specify enough data in
5981 * the CDB to avoid truncating this page, kick out the request.
5983 if ((page_len != (page_index->page_len - page_len_offset -
5985 || (*len_left < page_index->page_len)) {
5988 ctl_set_invalid_field(ctsio,
5991 /*field*/ *len_used + page_len_offset,
5994 free(ctsio->kern_data_ptr, M_CTL);
5995 ctl_done((union ctl_io *)ctsio);
5996 return (CTL_RETVAL_COMPLETE);
6000 * Run through the mode page, checking to make sure that the bits
6001 * the user changed are actually legal for him to change.
6003 for (i = 0; i < page_index->page_len; i++) {
6004 uint8_t *user_byte, *change_mask, *current_byte;
6008 user_byte = (uint8_t *)page_header + i;
6009 change_mask = page_index->page_data +
6010 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6011 current_byte = page_index->page_data +
6012 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6015 * Check to see whether the user set any bits in this byte
6016 * that he is not allowed to set.
6018 if ((*user_byte & ~(*change_mask)) ==
6019 (*current_byte & ~(*change_mask)))
6023 * Go through bit by bit to determine which one is illegal.
6026 for (j = 7; j >= 0; j--) {
6027 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6028 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6033 ctl_set_invalid_field(ctsio,
6036 /*field*/ *len_used + i,
6039 free(ctsio->kern_data_ptr, M_CTL);
6040 ctl_done((union ctl_io *)ctsio);
6041 return (CTL_RETVAL_COMPLETE);
6045 * Decrement these before we call the page handler, since we may
6046 * end up getting called back one way or another before the handler
6047 * returns to this context.
6049 *len_left -= page_index->page_len;
6050 *len_used += page_index->page_len;
6052 retval = page_index->select_handler(ctsio, page_index,
6053 (uint8_t *)page_header);
6056 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6057 * wait until this queued command completes to finish processing
6058 * the mode page. If it returns anything other than
6059 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6060 * already set the sense information, freed the data pointer, and
6061 * completed the io for us.
6063 if (retval != CTL_RETVAL_COMPLETE)
6064 goto bailout_no_done;
6067 * If the initiator sent us more than one page, parse the next one.
6072 ctl_set_success(ctsio);
6073 free(ctsio->kern_data_ptr, M_CTL);
6074 ctl_done((union ctl_io *)ctsio);
6078 return (CTL_RETVAL_COMPLETE);
6083 ctl_mode_select(struct ctl_scsiio *ctsio)
6085 int param_len, pf, sp;
6086 int header_size, bd_len;
6087 int len_left, len_used;
6088 struct ctl_page_index *page_index;
6089 struct ctl_lun *lun;
6090 int control_dev, page_len;
6091 union ctl_modepage_info *modepage_info;
6103 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6105 if (lun->be_lun->lun_type != T_DIRECT)
6110 switch (ctsio->cdb[0]) {
6111 case MODE_SELECT_6: {
6112 struct scsi_mode_select_6 *cdb;
6114 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6116 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6117 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6119 param_len = cdb->length;
6120 header_size = sizeof(struct scsi_mode_header_6);
6123 case MODE_SELECT_10: {
6124 struct scsi_mode_select_10 *cdb;
6126 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6128 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6129 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6131 param_len = scsi_2btoul(cdb->length);
6132 header_size = sizeof(struct scsi_mode_header_10);
6136 ctl_set_invalid_opcode(ctsio);
6137 ctl_done((union ctl_io *)ctsio);
6138 return (CTL_RETVAL_COMPLETE);
6139 break; /* NOTREACHED */
6144 * "A parameter list length of zero indicates that the Data-Out Buffer
6145 * shall be empty. This condition shall not be considered as an error."
6147 if (param_len == 0) {
6148 ctl_set_success(ctsio);
6149 ctl_done((union ctl_io *)ctsio);
6150 return (CTL_RETVAL_COMPLETE);
6154 * Since we'll hit this the first time through, prior to
6155 * allocation, we don't need to free a data buffer here.
6157 if (param_len < header_size) {
6158 ctl_set_param_len_error(ctsio);
6159 ctl_done((union ctl_io *)ctsio);
6160 return (CTL_RETVAL_COMPLETE);
6164 * Allocate the data buffer and grab the user's data. In theory,
6165 * we shouldn't have to sanity check the parameter list length here
6166 * because the maximum size is 64K. We should be able to malloc
6167 * that much without too many problems.
6169 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6170 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6171 ctsio->kern_data_len = param_len;
6172 ctsio->kern_total_len = param_len;
6173 ctsio->kern_data_resid = 0;
6174 ctsio->kern_rel_offset = 0;
6175 ctsio->kern_sg_entries = 0;
6176 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6177 ctsio->be_move_done = ctl_config_move_done;
6178 ctl_datamove((union ctl_io *)ctsio);
6180 return (CTL_RETVAL_COMPLETE);
6183 switch (ctsio->cdb[0]) {
6184 case MODE_SELECT_6: {
6185 struct scsi_mode_header_6 *mh6;
6187 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6188 bd_len = mh6->blk_desc_len;
6191 case MODE_SELECT_10: {
6192 struct scsi_mode_header_10 *mh10;
6194 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6195 bd_len = scsi_2btoul(mh10->blk_desc_len);
6199 panic("Invalid CDB type %#x", ctsio->cdb[0]);
6203 if (param_len < (header_size + bd_len)) {
6204 free(ctsio->kern_data_ptr, M_CTL);
6205 ctl_set_param_len_error(ctsio);
6206 ctl_done((union ctl_io *)ctsio);
6207 return (CTL_RETVAL_COMPLETE);
6211 * Set the IO_CONT flag, so that if this I/O gets passed to
6212 * ctl_config_write_done(), it'll get passed back to
6213 * ctl_do_mode_select() for further processing, or completion if
6216 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6217 ctsio->io_cont = ctl_do_mode_select;
6219 modepage_info = (union ctl_modepage_info *)
6220 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6222 memset(modepage_info, 0, sizeof(*modepage_info));
6224 len_left = param_len - header_size - bd_len;
6225 len_used = header_size + bd_len;
6227 modepage_info->header.len_left = len_left;
6228 modepage_info->header.len_used = len_used;
6230 return (ctl_do_mode_select((union ctl_io *)ctsio));
6234 ctl_mode_sense(struct ctl_scsiio *ctsio)
6236 struct ctl_lun *lun;
6237 int pc, page_code, dbd, llba, subpage;
6238 int alloc_len, page_len, header_len, total_len;
6239 struct scsi_mode_block_descr *block_desc;
6240 struct ctl_page_index *page_index;
6248 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6250 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6252 if (lun->be_lun->lun_type != T_DIRECT)
6257 switch (ctsio->cdb[0]) {
6258 case MODE_SENSE_6: {
6259 struct scsi_mode_sense_6 *cdb;
6261 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6263 header_len = sizeof(struct scsi_mode_hdr_6);
6264 if (cdb->byte2 & SMS_DBD)
6267 header_len += sizeof(struct scsi_mode_block_descr);
6269 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6270 page_code = cdb->page & SMS_PAGE_CODE;
6271 subpage = cdb->subpage;
6272 alloc_len = cdb->length;
6275 case MODE_SENSE_10: {
6276 struct scsi_mode_sense_10 *cdb;
6278 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6280 header_len = sizeof(struct scsi_mode_hdr_10);
6282 if (cdb->byte2 & SMS_DBD)
6285 header_len += sizeof(struct scsi_mode_block_descr);
6286 if (cdb->byte2 & SMS10_LLBAA)
6288 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6289 page_code = cdb->page & SMS_PAGE_CODE;
6290 subpage = cdb->subpage;
6291 alloc_len = scsi_2btoul(cdb->length);
6295 ctl_set_invalid_opcode(ctsio);
6296 ctl_done((union ctl_io *)ctsio);
6297 return (CTL_RETVAL_COMPLETE);
6298 break; /* NOTREACHED */
6302 * We have to make a first pass through to calculate the size of
6303 * the pages that match the user's query. Then we allocate enough
6304 * memory to hold it, and actually copy the data into the buffer.
6306 switch (page_code) {
6307 case SMS_ALL_PAGES_PAGE: {
6313 * At the moment, values other than 0 and 0xff here are
6314 * reserved according to SPC-3.
6316 if ((subpage != SMS_SUBPAGE_PAGE_0)
6317 && (subpage != SMS_SUBPAGE_ALL)) {
6318 ctl_set_invalid_field(ctsio,
6324 ctl_done((union ctl_io *)ctsio);
6325 return (CTL_RETVAL_COMPLETE);
6328 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6329 if ((control_dev != 0)
6330 && (lun->mode_pages.index[i].page_flags &
6331 CTL_PAGE_FLAG_DISK_ONLY))
6335 * We don't use this subpage if the user didn't
6336 * request all subpages.
6338 if ((lun->mode_pages.index[i].subpage != 0)
6339 && (subpage == SMS_SUBPAGE_PAGE_0))
6343 printf("found page %#x len %d\n",
6344 lun->mode_pages.index[i].page_code &
6346 lun->mode_pages.index[i].page_len);
6348 page_len += lun->mode_pages.index[i].page_len;
6357 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6358 /* Look for the right page code */
6359 if ((lun->mode_pages.index[i].page_code &
6360 SMPH_PC_MASK) != page_code)
6363 /* Look for the right subpage or the subpage wildcard*/
6364 if ((lun->mode_pages.index[i].subpage != subpage)
6365 && (subpage != SMS_SUBPAGE_ALL))
6368 /* Make sure the page is supported for this dev type */
6369 if ((control_dev != 0)
6370 && (lun->mode_pages.index[i].page_flags &
6371 CTL_PAGE_FLAG_DISK_ONLY))
6375 printf("found page %#x len %d\n",
6376 lun->mode_pages.index[i].page_code &
6378 lun->mode_pages.index[i].page_len);
6381 page_len += lun->mode_pages.index[i].page_len;
6384 if (page_len == 0) {
6385 ctl_set_invalid_field(ctsio,
6391 ctl_done((union ctl_io *)ctsio);
6392 return (CTL_RETVAL_COMPLETE);
6398 total_len = header_len + page_len;
6400 printf("header_len = %d, page_len = %d, total_len = %d\n",
6401 header_len, page_len, total_len);
6404 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6405 ctsio->kern_sg_entries = 0;
6406 ctsio->kern_data_resid = 0;
6407 ctsio->kern_rel_offset = 0;
6408 if (total_len < alloc_len) {
6409 ctsio->residual = alloc_len - total_len;
6410 ctsio->kern_data_len = total_len;
6411 ctsio->kern_total_len = total_len;
6413 ctsio->residual = 0;
6414 ctsio->kern_data_len = alloc_len;
6415 ctsio->kern_total_len = alloc_len;
6418 switch (ctsio->cdb[0]) {
6419 case MODE_SENSE_6: {
6420 struct scsi_mode_hdr_6 *header;
6422 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6424 header->datalen = MIN(total_len - 1, 254);
6425 if (control_dev == 0) {
6426 header->dev_specific = 0x10; /* DPOFUA */
6427 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6428 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6429 .eca_and_aen & SCP_SWP) != 0)
6430 header->dev_specific |= 0x80; /* WP */
6433 header->block_descr_len = 0;
6435 header->block_descr_len =
6436 sizeof(struct scsi_mode_block_descr);
6437 block_desc = (struct scsi_mode_block_descr *)&header[1];
6440 case MODE_SENSE_10: {
6441 struct scsi_mode_hdr_10 *header;
6444 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6446 datalen = MIN(total_len - 2, 65533);
6447 scsi_ulto2b(datalen, header->datalen);
6448 if (control_dev == 0) {
6449 header->dev_specific = 0x10; /* DPOFUA */
6450 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6451 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6452 .eca_and_aen & SCP_SWP) != 0)
6453 header->dev_specific |= 0x80; /* WP */
6456 scsi_ulto2b(0, header->block_descr_len);
6458 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6459 header->block_descr_len);
6460 block_desc = (struct scsi_mode_block_descr *)&header[1];
6464 panic("invalid CDB type %#x", ctsio->cdb[0]);
6465 break; /* NOTREACHED */
6469 * If we've got a disk, use its blocksize in the block
6470 * descriptor. Otherwise, just set it to 0.
6473 if (control_dev == 0)
6474 scsi_ulto3b(lun->be_lun->blocksize,
6475 block_desc->block_len);
6477 scsi_ulto3b(0, block_desc->block_len);
6480 switch (page_code) {
6481 case SMS_ALL_PAGES_PAGE: {
6484 data_used = header_len;
6485 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6486 struct ctl_page_index *page_index;
6488 page_index = &lun->mode_pages.index[i];
6490 if ((control_dev != 0)
6491 && (page_index->page_flags &
6492 CTL_PAGE_FLAG_DISK_ONLY))
6496 * We don't use this subpage if the user didn't
6497 * request all subpages. We already checked (above)
6498 * to make sure the user only specified a subpage
6499 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6501 if ((page_index->subpage != 0)
6502 && (subpage == SMS_SUBPAGE_PAGE_0))
6506 * Call the handler, if it exists, to update the
6507 * page to the latest values.
6509 if (page_index->sense_handler != NULL)
6510 page_index->sense_handler(ctsio, page_index,pc);
6512 memcpy(ctsio->kern_data_ptr + data_used,
6513 page_index->page_data +
6514 (page_index->page_len * pc),
6515 page_index->page_len);
6516 data_used += page_index->page_len;
6523 data_used = header_len;
6525 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6526 struct ctl_page_index *page_index;
6528 page_index = &lun->mode_pages.index[i];
6530 /* Look for the right page code */
6531 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6534 /* Look for the right subpage or the subpage wildcard*/
6535 if ((page_index->subpage != subpage)
6536 && (subpage != SMS_SUBPAGE_ALL))
6539 /* Make sure the page is supported for this dev type */
6540 if ((control_dev != 0)
6541 && (page_index->page_flags &
6542 CTL_PAGE_FLAG_DISK_ONLY))
6546 * Call the handler, if it exists, to update the
6547 * page to the latest values.
6549 if (page_index->sense_handler != NULL)
6550 page_index->sense_handler(ctsio, page_index,pc);
6552 memcpy(ctsio->kern_data_ptr + data_used,
6553 page_index->page_data +
6554 (page_index->page_len * pc),
6555 page_index->page_len);
6556 data_used += page_index->page_len;
6562 ctl_set_success(ctsio);
6563 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6564 ctsio->be_move_done = ctl_config_move_done;
6565 ctl_datamove((union ctl_io *)ctsio);
6566 return (CTL_RETVAL_COMPLETE);
6570 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6571 struct ctl_page_index *page_index,
6574 struct ctl_lun *lun;
6575 struct scsi_log_param_header *phdr;
6579 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6580 data = page_index->page_data;
6582 if (lun->backend->lun_attr != NULL &&
6583 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6585 phdr = (struct scsi_log_param_header *)data;
6586 scsi_ulto2b(0x0001, phdr->param_code);
6587 phdr->param_control = SLP_LBIN | SLP_LP;
6588 phdr->param_len = 8;
6589 data = (uint8_t *)(phdr + 1);
6590 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6591 data[4] = 0x02; /* per-pool */
6592 data += phdr->param_len;
6595 if (lun->backend->lun_attr != NULL &&
6596 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6598 phdr = (struct scsi_log_param_header *)data;
6599 scsi_ulto2b(0x0002, phdr->param_code);
6600 phdr->param_control = SLP_LBIN | SLP_LP;
6601 phdr->param_len = 8;
6602 data = (uint8_t *)(phdr + 1);
6603 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6604 data[4] = 0x01; /* per-LUN */
6605 data += phdr->param_len;
6608 if (lun->backend->lun_attr != NULL &&
6609 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6611 phdr = (struct scsi_log_param_header *)data;
6612 scsi_ulto2b(0x00f1, phdr->param_code);
6613 phdr->param_control = SLP_LBIN | SLP_LP;
6614 phdr->param_len = 8;
6615 data = (uint8_t *)(phdr + 1);
6616 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6617 data[4] = 0x02; /* per-pool */
6618 data += phdr->param_len;
6621 if (lun->backend->lun_attr != NULL &&
6622 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6624 phdr = (struct scsi_log_param_header *)data;
6625 scsi_ulto2b(0x00f2, phdr->param_code);
6626 phdr->param_control = SLP_LBIN | SLP_LP;
6627 phdr->param_len = 8;
6628 data = (uint8_t *)(phdr + 1);
6629 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6630 data[4] = 0x02; /* per-pool */
6631 data += phdr->param_len;
6634 page_index->page_len = data - page_index->page_data;
6639 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
6640 struct ctl_page_index *page_index,
6643 struct ctl_lun *lun;
6644 struct stat_page *data;
6645 uint64_t rn, wn, rb, wb;
6646 struct bintime rt, wt;
6649 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6650 data = (struct stat_page *)page_index->page_data;
6652 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
6653 data->sap.hdr.param_control = SLP_LBIN;
6654 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
6655 sizeof(struct scsi_log_param_header);
6656 rn = wn = rb = wb = 0;
6659 for (i = 0; i < CTL_MAX_PORTS; i++) {
6660 rn += lun->stats.ports[i].operations[CTL_STATS_READ];
6661 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE];
6662 rb += lun->stats.ports[i].bytes[CTL_STATS_READ];
6663 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE];
6664 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]);
6665 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]);
6667 scsi_u64to8b(rn, data->sap.read_num);
6668 scsi_u64to8b(wn, data->sap.write_num);
6669 if (lun->stats.blocksize > 0) {
6670 scsi_u64to8b(wb / lun->stats.blocksize,
6671 data->sap.recvieved_lba);
6672 scsi_u64to8b(rb / lun->stats.blocksize,
6673 data->sap.transmitted_lba);
6675 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000),
6676 data->sap.read_int);
6677 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000),
6678 data->sap.write_int);
6679 scsi_u64to8b(0, data->sap.weighted_num);
6680 scsi_u64to8b(0, data->sap.weighted_int);
6681 scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
6682 data->it.hdr.param_control = SLP_LBIN;
6683 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
6684 sizeof(struct scsi_log_param_header);
6686 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
6688 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
6689 data->it.hdr.param_control = SLP_LBIN;
6690 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
6691 sizeof(struct scsi_log_param_header);
6692 scsi_ulto4b(3, data->ti.exponent);
6693 scsi_ulto4b(1, data->ti.integer);
6695 page_index->page_len = sizeof(*data);
6700 ctl_log_sense(struct ctl_scsiio *ctsio)
6702 struct ctl_lun *lun;
6703 int i, pc, page_code, subpage;
6704 int alloc_len, total_len;
6705 struct ctl_page_index *page_index;
6706 struct scsi_log_sense *cdb;
6707 struct scsi_log_header *header;
6709 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6711 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6712 cdb = (struct scsi_log_sense *)ctsio->cdb;
6713 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6714 page_code = cdb->page & SLS_PAGE_CODE;
6715 subpage = cdb->subpage;
6716 alloc_len = scsi_2btoul(cdb->length);
6719 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6720 page_index = &lun->log_pages.index[i];
6722 /* Look for the right page code */
6723 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6726 /* Look for the right subpage or the subpage wildcard*/
6727 if (page_index->subpage != subpage)
6732 if (i >= CTL_NUM_LOG_PAGES) {
6733 ctl_set_invalid_field(ctsio,
6739 ctl_done((union ctl_io *)ctsio);
6740 return (CTL_RETVAL_COMPLETE);
6743 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6745 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6746 ctsio->kern_sg_entries = 0;
6747 ctsio->kern_data_resid = 0;
6748 ctsio->kern_rel_offset = 0;
6749 if (total_len < alloc_len) {
6750 ctsio->residual = alloc_len - total_len;
6751 ctsio->kern_data_len = total_len;
6752 ctsio->kern_total_len = total_len;
6754 ctsio->residual = 0;
6755 ctsio->kern_data_len = alloc_len;
6756 ctsio->kern_total_len = alloc_len;
6759 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
6760 header->page = page_index->page_code;
6761 if (page_index->subpage) {
6762 header->page |= SL_SPF;
6763 header->subpage = page_index->subpage;
6765 scsi_ulto2b(page_index->page_len, header->datalen);
6768 * Call the handler, if it exists, to update the
6769 * page to the latest values.
6771 if (page_index->sense_handler != NULL)
6772 page_index->sense_handler(ctsio, page_index, pc);
6774 memcpy(header + 1, page_index->page_data, page_index->page_len);
6776 ctl_set_success(ctsio);
6777 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6778 ctsio->be_move_done = ctl_config_move_done;
6779 ctl_datamove((union ctl_io *)ctsio);
6780 return (CTL_RETVAL_COMPLETE);
6784 ctl_read_capacity(struct ctl_scsiio *ctsio)
6786 struct scsi_read_capacity *cdb;
6787 struct scsi_read_capacity_data *data;
6788 struct ctl_lun *lun;
6791 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6793 cdb = (struct scsi_read_capacity *)ctsio->cdb;
6795 lba = scsi_4btoul(cdb->addr);
6796 if (((cdb->pmi & SRC_PMI) == 0)
6798 ctl_set_invalid_field(/*ctsio*/ ctsio,
6804 ctl_done((union ctl_io *)ctsio);
6805 return (CTL_RETVAL_COMPLETE);
6808 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6810 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6811 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6812 ctsio->residual = 0;
6813 ctsio->kern_data_len = sizeof(*data);
6814 ctsio->kern_total_len = sizeof(*data);
6815 ctsio->kern_data_resid = 0;
6816 ctsio->kern_rel_offset = 0;
6817 ctsio->kern_sg_entries = 0;
6820 * If the maximum LBA is greater than 0xfffffffe, the user must
6821 * issue a SERVICE ACTION IN (16) command, with the read capacity
6822 * serivce action set.
6824 if (lun->be_lun->maxlba > 0xfffffffe)
6825 scsi_ulto4b(0xffffffff, data->addr);
6827 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6830 * XXX KDM this may not be 512 bytes...
6832 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6834 ctl_set_success(ctsio);
6835 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6836 ctsio->be_move_done = ctl_config_move_done;
6837 ctl_datamove((union ctl_io *)ctsio);
6838 return (CTL_RETVAL_COMPLETE);
6842 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
6844 struct scsi_read_capacity_16 *cdb;
6845 struct scsi_read_capacity_data_long *data;
6846 struct ctl_lun *lun;
6850 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
6852 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
6854 alloc_len = scsi_4btoul(cdb->alloc_len);
6855 lba = scsi_8btou64(cdb->addr);
6857 if ((cdb->reladr & SRC16_PMI)
6859 ctl_set_invalid_field(/*ctsio*/ ctsio,
6865 ctl_done((union ctl_io *)ctsio);
6866 return (CTL_RETVAL_COMPLETE);
6869 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6871 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6872 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
6874 if (sizeof(*data) < alloc_len) {
6875 ctsio->residual = alloc_len - sizeof(*data);
6876 ctsio->kern_data_len = sizeof(*data);
6877 ctsio->kern_total_len = sizeof(*data);
6879 ctsio->residual = 0;
6880 ctsio->kern_data_len = alloc_len;
6881 ctsio->kern_total_len = alloc_len;
6883 ctsio->kern_data_resid = 0;
6884 ctsio->kern_rel_offset = 0;
6885 ctsio->kern_sg_entries = 0;
6887 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
6888 /* XXX KDM this may not be 512 bytes... */
6889 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6890 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
6891 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
6892 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
6893 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
6895 ctl_set_success(ctsio);
6896 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6897 ctsio->be_move_done = ctl_config_move_done;
6898 ctl_datamove((union ctl_io *)ctsio);
6899 return (CTL_RETVAL_COMPLETE);
6903 ctl_get_lba_status(struct ctl_scsiio *ctsio)
6905 struct scsi_get_lba_status *cdb;
6906 struct scsi_get_lba_status_data *data;
6907 struct ctl_lun *lun;
6908 struct ctl_lba_len_flags *lbalen;
6910 uint32_t alloc_len, total_len;
6913 CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
6915 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6916 cdb = (struct scsi_get_lba_status *)ctsio->cdb;
6917 lba = scsi_8btou64(cdb->addr);
6918 alloc_len = scsi_4btoul(cdb->alloc_len);
6920 if (lba > lun->be_lun->maxlba) {
6921 ctl_set_lba_out_of_range(ctsio);
6922 ctl_done((union ctl_io *)ctsio);
6923 return (CTL_RETVAL_COMPLETE);
6926 total_len = sizeof(*data) + sizeof(data->descr[0]);
6927 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6928 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
6930 if (total_len < alloc_len) {
6931 ctsio->residual = alloc_len - total_len;
6932 ctsio->kern_data_len = total_len;
6933 ctsio->kern_total_len = total_len;
6935 ctsio->residual = 0;
6936 ctsio->kern_data_len = alloc_len;
6937 ctsio->kern_total_len = alloc_len;
6939 ctsio->kern_data_resid = 0;
6940 ctsio->kern_rel_offset = 0;
6941 ctsio->kern_sg_entries = 0;
6943 /* Fill dummy data in case backend can't tell anything. */
6944 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
6945 scsi_u64to8b(lba, data->descr[0].addr);
6946 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
6947 data->descr[0].length);
6948 data->descr[0].status = 0; /* Mapped or unknown. */
6950 ctl_set_success(ctsio);
6951 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6952 ctsio->be_move_done = ctl_config_move_done;
6954 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
6956 lbalen->len = total_len;
6958 retval = lun->backend->config_read((union ctl_io *)ctsio);
6959 return (CTL_RETVAL_COMPLETE);
6963 ctl_read_defect(struct ctl_scsiio *ctsio)
6965 struct scsi_read_defect_data_10 *ccb10;
6966 struct scsi_read_defect_data_12 *ccb12;
6967 struct scsi_read_defect_data_hdr_10 *data10;
6968 struct scsi_read_defect_data_hdr_12 *data12;
6969 uint32_t alloc_len, data_len;
6972 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
6974 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
6975 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
6976 format = ccb10->format;
6977 alloc_len = scsi_2btoul(ccb10->alloc_length);
6978 data_len = sizeof(*data10);
6980 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
6981 format = ccb12->format;
6982 alloc_len = scsi_4btoul(ccb12->alloc_length);
6983 data_len = sizeof(*data12);
6985 if (alloc_len == 0) {
6986 ctl_set_success(ctsio);
6987 ctl_done((union ctl_io *)ctsio);
6988 return (CTL_RETVAL_COMPLETE);
6991 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
6992 if (data_len < alloc_len) {
6993 ctsio->residual = alloc_len - data_len;
6994 ctsio->kern_data_len = data_len;
6995 ctsio->kern_total_len = data_len;
6997 ctsio->residual = 0;
6998 ctsio->kern_data_len = alloc_len;
6999 ctsio->kern_total_len = alloc_len;
7001 ctsio->kern_data_resid = 0;
7002 ctsio->kern_rel_offset = 0;
7003 ctsio->kern_sg_entries = 0;
7005 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7006 data10 = (struct scsi_read_defect_data_hdr_10 *)
7007 ctsio->kern_data_ptr;
7008 data10->format = format;
7009 scsi_ulto2b(0, data10->length);
7011 data12 = (struct scsi_read_defect_data_hdr_12 *)
7012 ctsio->kern_data_ptr;
7013 data12->format = format;
7014 scsi_ulto2b(0, data12->generation);
7015 scsi_ulto4b(0, data12->length);
7018 ctl_set_success(ctsio);
7019 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7020 ctsio->be_move_done = ctl_config_move_done;
7021 ctl_datamove((union ctl_io *)ctsio);
7022 return (CTL_RETVAL_COMPLETE);
7026 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7028 struct scsi_maintenance_in *cdb;
7030 int alloc_len, ext, total_len = 0, g, pc, pg, gs, os;
7031 int num_target_port_groups, num_target_ports;
7032 struct ctl_lun *lun;
7033 struct ctl_softc *softc;
7034 struct ctl_port *port;
7035 struct scsi_target_group_data *rtg_ptr;
7036 struct scsi_target_group_data_extended *rtg_ext_ptr;
7037 struct scsi_target_port_group_descriptor *tpg_desc;
7039 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7041 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7042 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7043 softc = lun->ctl_softc;
7045 retval = CTL_RETVAL_COMPLETE;
7047 switch (cdb->byte2 & STG_PDF_MASK) {
7048 case STG_PDF_LENGTH:
7051 case STG_PDF_EXTENDED:
7055 ctl_set_invalid_field(/*ctsio*/ ctsio,
7061 ctl_done((union ctl_io *)ctsio);
7065 if (softc->is_single)
7066 num_target_port_groups = 1;
7068 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
7069 num_target_ports = 0;
7070 mtx_lock(&softc->ctl_lock);
7071 STAILQ_FOREACH(port, &softc->port_list, links) {
7072 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7074 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7078 mtx_unlock(&softc->ctl_lock);
7081 total_len = sizeof(struct scsi_target_group_data_extended);
7083 total_len = sizeof(struct scsi_target_group_data);
7084 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7085 num_target_port_groups +
7086 sizeof(struct scsi_target_port_descriptor) * num_target_ports;
7088 alloc_len = scsi_4btoul(cdb->length);
7090 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7092 ctsio->kern_sg_entries = 0;
7094 if (total_len < alloc_len) {
7095 ctsio->residual = alloc_len - total_len;
7096 ctsio->kern_data_len = total_len;
7097 ctsio->kern_total_len = total_len;
7099 ctsio->residual = 0;
7100 ctsio->kern_data_len = alloc_len;
7101 ctsio->kern_total_len = alloc_len;
7103 ctsio->kern_data_resid = 0;
7104 ctsio->kern_rel_offset = 0;
7107 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7108 ctsio->kern_data_ptr;
7109 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7110 rtg_ext_ptr->format_type = 0x10;
7111 rtg_ext_ptr->implicit_transition_time = 0;
7112 tpg_desc = &rtg_ext_ptr->groups[0];
7114 rtg_ptr = (struct scsi_target_group_data *)
7115 ctsio->kern_data_ptr;
7116 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7117 tpg_desc = &rtg_ptr->groups[0];
7120 mtx_lock(&softc->ctl_lock);
7121 pg = softc->port_min / softc->port_cnt;
7122 if (softc->ha_link == CTL_HA_LINK_OFFLINE)
7123 gs = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7124 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN)
7125 gs = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7126 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY)
7127 gs = TPG_ASYMMETRIC_ACCESS_STANDBY;
7129 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7130 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7132 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7134 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7135 for (g = 0; g < num_target_port_groups; g++) {
7136 tpg_desc->pref_state = (g == pg) ? gs : os;
7137 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7138 TPG_U_SUP | TPG_T_SUP;
7139 scsi_ulto2b(g + 1, tpg_desc->target_port_group);
7140 tpg_desc->status = TPG_IMPLICIT;
7142 STAILQ_FOREACH(port, &softc->port_list, links) {
7143 if (port->targ_port < g * softc->port_cnt ||
7144 port->targ_port >= (g + 1) * softc->port_cnt)
7146 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7148 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7150 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7151 relative_target_port_identifier);
7154 tpg_desc->target_port_count = pc;
7155 tpg_desc = (struct scsi_target_port_group_descriptor *)
7156 &tpg_desc->descriptors[pc];
7158 mtx_unlock(&softc->ctl_lock);
7160 ctl_set_success(ctsio);
7161 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7162 ctsio->be_move_done = ctl_config_move_done;
7163 ctl_datamove((union ctl_io *)ctsio);
7168 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7170 struct ctl_lun *lun;
7171 struct scsi_report_supported_opcodes *cdb;
7172 const struct ctl_cmd_entry *entry, *sentry;
7173 struct scsi_report_supported_opcodes_all *all;
7174 struct scsi_report_supported_opcodes_descr *descr;
7175 struct scsi_report_supported_opcodes_one *one;
7177 int alloc_len, total_len;
7178 int opcode, service_action, i, j, num;
7180 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7182 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7183 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7185 retval = CTL_RETVAL_COMPLETE;
7187 opcode = cdb->requested_opcode;
7188 service_action = scsi_2btoul(cdb->requested_service_action);
7189 switch (cdb->options & RSO_OPTIONS_MASK) {
7190 case RSO_OPTIONS_ALL:
7192 for (i = 0; i < 256; i++) {
7193 entry = &ctl_cmd_table[i];
7194 if (entry->flags & CTL_CMD_FLAG_SA5) {
7195 for (j = 0; j < 32; j++) {
7196 sentry = &((const struct ctl_cmd_entry *)
7198 if (ctl_cmd_applicable(
7199 lun->be_lun->lun_type, sentry))
7203 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7208 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7209 num * sizeof(struct scsi_report_supported_opcodes_descr);
7211 case RSO_OPTIONS_OC:
7212 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7213 ctl_set_invalid_field(/*ctsio*/ ctsio,
7219 ctl_done((union ctl_io *)ctsio);
7220 return (CTL_RETVAL_COMPLETE);
7222 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7224 case RSO_OPTIONS_OC_SA:
7225 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7226 service_action >= 32) {
7227 ctl_set_invalid_field(/*ctsio*/ ctsio,
7233 ctl_done((union ctl_io *)ctsio);
7234 return (CTL_RETVAL_COMPLETE);
7236 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7239 ctl_set_invalid_field(/*ctsio*/ ctsio,
7245 ctl_done((union ctl_io *)ctsio);
7246 return (CTL_RETVAL_COMPLETE);
7249 alloc_len = scsi_4btoul(cdb->length);
7251 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7253 ctsio->kern_sg_entries = 0;
7255 if (total_len < alloc_len) {
7256 ctsio->residual = alloc_len - total_len;
7257 ctsio->kern_data_len = total_len;
7258 ctsio->kern_total_len = total_len;
7260 ctsio->residual = 0;
7261 ctsio->kern_data_len = alloc_len;
7262 ctsio->kern_total_len = alloc_len;
7264 ctsio->kern_data_resid = 0;
7265 ctsio->kern_rel_offset = 0;
7267 switch (cdb->options & RSO_OPTIONS_MASK) {
7268 case RSO_OPTIONS_ALL:
7269 all = (struct scsi_report_supported_opcodes_all *)
7270 ctsio->kern_data_ptr;
7272 for (i = 0; i < 256; i++) {
7273 entry = &ctl_cmd_table[i];
7274 if (entry->flags & CTL_CMD_FLAG_SA5) {
7275 for (j = 0; j < 32; j++) {
7276 sentry = &((const struct ctl_cmd_entry *)
7278 if (!ctl_cmd_applicable(
7279 lun->be_lun->lun_type, sentry))
7281 descr = &all->descr[num++];
7283 scsi_ulto2b(j, descr->service_action);
7284 descr->flags = RSO_SERVACTV;
7285 scsi_ulto2b(sentry->length,
7289 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7292 descr = &all->descr[num++];
7294 scsi_ulto2b(0, descr->service_action);
7296 scsi_ulto2b(entry->length, descr->cdb_length);
7300 num * sizeof(struct scsi_report_supported_opcodes_descr),
7303 case RSO_OPTIONS_OC:
7304 one = (struct scsi_report_supported_opcodes_one *)
7305 ctsio->kern_data_ptr;
7306 entry = &ctl_cmd_table[opcode];
7308 case RSO_OPTIONS_OC_SA:
7309 one = (struct scsi_report_supported_opcodes_one *)
7310 ctsio->kern_data_ptr;
7311 entry = &ctl_cmd_table[opcode];
7312 entry = &((const struct ctl_cmd_entry *)
7313 entry->execute)[service_action];
7315 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7317 scsi_ulto2b(entry->length, one->cdb_length);
7318 one->cdb_usage[0] = opcode;
7319 memcpy(&one->cdb_usage[1], entry->usage,
7326 ctl_set_success(ctsio);
7327 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7328 ctsio->be_move_done = ctl_config_move_done;
7329 ctl_datamove((union ctl_io *)ctsio);
7334 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7336 struct scsi_report_supported_tmf *cdb;
7337 struct scsi_report_supported_tmf_data *data;
7339 int alloc_len, total_len;
7341 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7343 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7345 retval = CTL_RETVAL_COMPLETE;
7347 total_len = sizeof(struct scsi_report_supported_tmf_data);
7348 alloc_len = scsi_4btoul(cdb->length);
7350 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7352 ctsio->kern_sg_entries = 0;
7354 if (total_len < alloc_len) {
7355 ctsio->residual = alloc_len - total_len;
7356 ctsio->kern_data_len = total_len;
7357 ctsio->kern_total_len = total_len;
7359 ctsio->residual = 0;
7360 ctsio->kern_data_len = alloc_len;
7361 ctsio->kern_total_len = alloc_len;
7363 ctsio->kern_data_resid = 0;
7364 ctsio->kern_rel_offset = 0;
7366 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr;
7367 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS;
7368 data->byte2 |= RST_ITNRS;
7370 ctl_set_success(ctsio);
7371 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7372 ctsio->be_move_done = ctl_config_move_done;
7373 ctl_datamove((union ctl_io *)ctsio);
7378 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7380 struct scsi_report_timestamp *cdb;
7381 struct scsi_report_timestamp_data *data;
7385 int alloc_len, total_len;
7387 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7389 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7391 retval = CTL_RETVAL_COMPLETE;
7393 total_len = sizeof(struct scsi_report_timestamp_data);
7394 alloc_len = scsi_4btoul(cdb->length);
7396 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7398 ctsio->kern_sg_entries = 0;
7400 if (total_len < alloc_len) {
7401 ctsio->residual = alloc_len - total_len;
7402 ctsio->kern_data_len = total_len;
7403 ctsio->kern_total_len = total_len;
7405 ctsio->residual = 0;
7406 ctsio->kern_data_len = alloc_len;
7407 ctsio->kern_total_len = alloc_len;
7409 ctsio->kern_data_resid = 0;
7410 ctsio->kern_rel_offset = 0;
7412 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7413 scsi_ulto2b(sizeof(*data) - 2, data->length);
7414 data->origin = RTS_ORIG_OUTSIDE;
7416 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7417 scsi_ulto4b(timestamp >> 16, data->timestamp);
7418 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7420 ctl_set_success(ctsio);
7421 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7422 ctsio->be_move_done = ctl_config_move_done;
7423 ctl_datamove((union ctl_io *)ctsio);
7428 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7430 struct scsi_per_res_in *cdb;
7431 int alloc_len, total_len = 0;
7432 /* struct scsi_per_res_in_rsrv in_data; */
7433 struct ctl_lun *lun;
7434 struct ctl_softc *softc;
7437 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7439 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7441 alloc_len = scsi_2btoul(cdb->length);
7443 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7444 softc = lun->ctl_softc;
7447 mtx_lock(&lun->lun_lock);
7448 switch (cdb->action) {
7449 case SPRI_RK: /* read keys */
7450 total_len = sizeof(struct scsi_per_res_in_keys) +
7452 sizeof(struct scsi_per_res_key);
7454 case SPRI_RR: /* read reservation */
7455 if (lun->flags & CTL_LUN_PR_RESERVED)
7456 total_len = sizeof(struct scsi_per_res_in_rsrv);
7458 total_len = sizeof(struct scsi_per_res_in_header);
7460 case SPRI_RC: /* report capabilities */
7461 total_len = sizeof(struct scsi_per_res_cap);
7463 case SPRI_RS: /* read full status */
7464 total_len = sizeof(struct scsi_per_res_in_header) +
7465 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7469 panic("Invalid PR type %x", cdb->action);
7471 mtx_unlock(&lun->lun_lock);
7473 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7475 if (total_len < alloc_len) {
7476 ctsio->residual = alloc_len - total_len;
7477 ctsio->kern_data_len = total_len;
7478 ctsio->kern_total_len = total_len;
7480 ctsio->residual = 0;
7481 ctsio->kern_data_len = alloc_len;
7482 ctsio->kern_total_len = alloc_len;
7485 ctsio->kern_data_resid = 0;
7486 ctsio->kern_rel_offset = 0;
7487 ctsio->kern_sg_entries = 0;
7489 mtx_lock(&lun->lun_lock);
7490 switch (cdb->action) {
7491 case SPRI_RK: { // read keys
7492 struct scsi_per_res_in_keys *res_keys;
7495 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7498 * We had to drop the lock to allocate our buffer, which
7499 * leaves time for someone to come in with another
7500 * persistent reservation. (That is unlikely, though,
7501 * since this should be the only persistent reservation
7502 * command active right now.)
7504 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7505 (lun->pr_key_count *
7506 sizeof(struct scsi_per_res_key)))){
7507 mtx_unlock(&lun->lun_lock);
7508 free(ctsio->kern_data_ptr, M_CTL);
7509 printf("%s: reservation length changed, retrying\n",
7514 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7516 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7517 lun->pr_key_count, res_keys->header.length);
7519 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) {
7520 if ((key = ctl_get_prkey(lun, i)) == 0)
7524 * We used lun->pr_key_count to calculate the
7525 * size to allocate. If it turns out the number of
7526 * initiators with the registered flag set is
7527 * larger than that (i.e. they haven't been kept in
7528 * sync), we've got a problem.
7530 if (key_count >= lun->pr_key_count) {
7532 csevent_log(CSC_CTL | CSC_SHELF_SW |
7534 csevent_LogType_Fault,
7535 csevent_AlertLevel_Yellow,
7536 csevent_FRU_ShelfController,
7537 csevent_FRU_Firmware,
7538 csevent_FRU_Unknown,
7539 "registered keys %d >= key "
7540 "count %d", key_count,
7546 scsi_u64to8b(key, res_keys->keys[key_count].key);
7551 case SPRI_RR: { // read reservation
7552 struct scsi_per_res_in_rsrv *res;
7553 int tmp_len, header_only;
7555 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7557 scsi_ulto4b(lun->PRGeneration, res->header.generation);
7559 if (lun->flags & CTL_LUN_PR_RESERVED)
7561 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7562 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7563 res->header.length);
7566 tmp_len = sizeof(struct scsi_per_res_in_header);
7567 scsi_ulto4b(0, res->header.length);
7572 * We had to drop the lock to allocate our buffer, which
7573 * leaves time for someone to come in with another
7574 * persistent reservation. (That is unlikely, though,
7575 * since this should be the only persistent reservation
7576 * command active right now.)
7578 if (tmp_len != total_len) {
7579 mtx_unlock(&lun->lun_lock);
7580 free(ctsio->kern_data_ptr, M_CTL);
7581 printf("%s: reservation status changed, retrying\n",
7587 * No reservation held, so we're done.
7589 if (header_only != 0)
7593 * If the registration is an All Registrants type, the key
7594 * is 0, since it doesn't really matter.
7596 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7597 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7598 res->data.reservation);
7600 res->data.scopetype = lun->res_type;
7603 case SPRI_RC: //report capabilities
7605 struct scsi_per_res_cap *res_cap;
7608 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7609 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7610 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5;
7611 type_mask = SPRI_TM_WR_EX_AR |
7617 scsi_ulto2b(type_mask, res_cap->type_mask);
7620 case SPRI_RS: { // read full status
7621 struct scsi_per_res_in_full *res_status;
7622 struct scsi_per_res_in_full_desc *res_desc;
7623 struct ctl_port *port;
7626 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7629 * We had to drop the lock to allocate our buffer, which
7630 * leaves time for someone to come in with another
7631 * persistent reservation. (That is unlikely, though,
7632 * since this should be the only persistent reservation
7633 * command active right now.)
7635 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7636 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7637 lun->pr_key_count)){
7638 mtx_unlock(&lun->lun_lock);
7639 free(ctsio->kern_data_ptr, M_CTL);
7640 printf("%s: reservation length changed, retrying\n",
7645 scsi_ulto4b(lun->PRGeneration, res_status->header.generation);
7647 res_desc = &res_status->desc[0];
7648 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7649 if ((key = ctl_get_prkey(lun, i)) == 0)
7652 scsi_u64to8b(key, res_desc->res_key.key);
7653 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7654 (lun->pr_res_idx == i ||
7655 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7656 res_desc->flags = SPRI_FULL_R_HOLDER;
7657 res_desc->scopetype = lun->res_type;
7659 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7660 res_desc->rel_trgt_port_id);
7662 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7664 len = ctl_create_iid(port,
7665 i % CTL_MAX_INIT_PER_PORT,
7666 res_desc->transport_id);
7667 scsi_ulto4b(len, res_desc->additional_length);
7668 res_desc = (struct scsi_per_res_in_full_desc *)
7669 &res_desc->transport_id[len];
7671 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7672 res_status->header.length);
7677 * This is a bug, because we just checked for this above,
7678 * and should have returned an error.
7680 panic("Invalid PR type %x", cdb->action);
7681 break; /* NOTREACHED */
7683 mtx_unlock(&lun->lun_lock);
7685 ctl_set_success(ctsio);
7686 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7687 ctsio->be_move_done = ctl_config_move_done;
7688 ctl_datamove((union ctl_io *)ctsio);
7689 return (CTL_RETVAL_COMPLETE);
7693 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7697 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7698 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7699 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7700 struct scsi_per_res_out_parms* param)
7702 union ctl_ha_msg persis_io;
7705 mtx_lock(&lun->lun_lock);
7706 if (sa_res_key == 0) {
7707 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7708 /* validate scope and type */
7709 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7711 mtx_unlock(&lun->lun_lock);
7712 ctl_set_invalid_field(/*ctsio*/ ctsio,
7718 ctl_done((union ctl_io *)ctsio);
7722 if (type>8 || type==2 || type==4 || type==0) {
7723 mtx_unlock(&lun->lun_lock);
7724 ctl_set_invalid_field(/*ctsio*/ ctsio,
7730 ctl_done((union ctl_io *)ctsio);
7735 * Unregister everybody else and build UA for
7738 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7739 if (i == residx || ctl_get_prkey(lun, i) == 0)
7742 ctl_clr_prkey(lun, i);
7743 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7745 lun->pr_key_count = 1;
7746 lun->res_type = type;
7747 if (lun->res_type != SPR_TYPE_WR_EX_AR
7748 && lun->res_type != SPR_TYPE_EX_AC_AR)
7749 lun->pr_res_idx = residx;
7750 lun->PRGeneration++;
7751 mtx_unlock(&lun->lun_lock);
7753 /* send msg to other side */
7754 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7755 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7756 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7757 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7758 persis_io.pr.pr_info.res_type = type;
7759 memcpy(persis_io.pr.pr_info.sa_res_key,
7760 param->serv_act_res_key,
7761 sizeof(param->serv_act_res_key));
7762 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7763 sizeof(persis_io.pr), M_WAITOK);
7765 /* not all registrants */
7766 mtx_unlock(&lun->lun_lock);
7767 free(ctsio->kern_data_ptr, M_CTL);
7768 ctl_set_invalid_field(ctsio,
7774 ctl_done((union ctl_io *)ctsio);
7777 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7778 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7781 if (res_key == sa_res_key) {
7784 * The spec implies this is not good but doesn't
7785 * say what to do. There are two choices either
7786 * generate a res conflict or check condition
7787 * with illegal field in parameter data. Since
7788 * that is what is done when the sa_res_key is
7789 * zero I'll take that approach since this has
7790 * to do with the sa_res_key.
7792 mtx_unlock(&lun->lun_lock);
7793 free(ctsio->kern_data_ptr, M_CTL);
7794 ctl_set_invalid_field(ctsio,
7800 ctl_done((union ctl_io *)ctsio);
7804 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7805 if (ctl_get_prkey(lun, i) != sa_res_key)
7809 ctl_clr_prkey(lun, i);
7810 lun->pr_key_count--;
7811 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7814 mtx_unlock(&lun->lun_lock);
7815 free(ctsio->kern_data_ptr, M_CTL);
7816 ctl_set_reservation_conflict(ctsio);
7817 ctl_done((union ctl_io *)ctsio);
7818 return (CTL_RETVAL_COMPLETE);
7820 lun->PRGeneration++;
7821 mtx_unlock(&lun->lun_lock);
7823 /* send msg to other side */
7824 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7825 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7826 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7827 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7828 persis_io.pr.pr_info.res_type = type;
7829 memcpy(persis_io.pr.pr_info.sa_res_key,
7830 param->serv_act_res_key,
7831 sizeof(param->serv_act_res_key));
7832 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7833 sizeof(persis_io.pr), M_WAITOK);
7835 /* Reserved but not all registrants */
7836 /* sa_res_key is res holder */
7837 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
7838 /* validate scope and type */
7839 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7841 mtx_unlock(&lun->lun_lock);
7842 ctl_set_invalid_field(/*ctsio*/ ctsio,
7848 ctl_done((union ctl_io *)ctsio);
7852 if (type>8 || type==2 || type==4 || type==0) {
7853 mtx_unlock(&lun->lun_lock);
7854 ctl_set_invalid_field(/*ctsio*/ ctsio,
7860 ctl_done((union ctl_io *)ctsio);
7866 * if sa_res_key != res_key remove all
7867 * registrants w/sa_res_key and generate UA
7868 * for these registrants(Registrations
7869 * Preempted) if it wasn't an exclusive
7870 * reservation generate UA(Reservations
7871 * Preempted) for all other registered nexuses
7872 * if the type has changed. Establish the new
7873 * reservation and holder. If res_key and
7874 * sa_res_key are the same do the above
7875 * except don't unregister the res holder.
7878 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7879 if (i == residx || ctl_get_prkey(lun, i) == 0)
7882 if (sa_res_key == ctl_get_prkey(lun, i)) {
7883 ctl_clr_prkey(lun, i);
7884 lun->pr_key_count--;
7885 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7886 } else if (type != lun->res_type
7887 && (lun->res_type == SPR_TYPE_WR_EX_RO
7888 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
7889 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
7892 lun->res_type = type;
7893 if (lun->res_type != SPR_TYPE_WR_EX_AR
7894 && lun->res_type != SPR_TYPE_EX_AC_AR)
7895 lun->pr_res_idx = residx;
7897 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
7898 lun->PRGeneration++;
7899 mtx_unlock(&lun->lun_lock);
7901 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7902 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7903 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7904 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7905 persis_io.pr.pr_info.res_type = type;
7906 memcpy(persis_io.pr.pr_info.sa_res_key,
7907 param->serv_act_res_key,
7908 sizeof(param->serv_act_res_key));
7909 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7910 sizeof(persis_io.pr), M_WAITOK);
7913 * sa_res_key is not the res holder just
7914 * remove registrants
7918 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7919 if (sa_res_key != ctl_get_prkey(lun, i))
7923 ctl_clr_prkey(lun, i);
7924 lun->pr_key_count--;
7925 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7929 mtx_unlock(&lun->lun_lock);
7930 free(ctsio->kern_data_ptr, M_CTL);
7931 ctl_set_reservation_conflict(ctsio);
7932 ctl_done((union ctl_io *)ctsio);
7935 lun->PRGeneration++;
7936 mtx_unlock(&lun->lun_lock);
7938 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7939 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7940 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7941 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7942 persis_io.pr.pr_info.res_type = type;
7943 memcpy(persis_io.pr.pr_info.sa_res_key,
7944 param->serv_act_res_key,
7945 sizeof(param->serv_act_res_key));
7946 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7947 sizeof(persis_io.pr), M_WAITOK);
7954 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
7956 uint64_t sa_res_key;
7959 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
7961 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7962 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
7963 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
7964 if (sa_res_key == 0) {
7966 * Unregister everybody else and build UA for
7969 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7970 if (i == msg->pr.pr_info.residx ||
7971 ctl_get_prkey(lun, i) == 0)
7974 ctl_clr_prkey(lun, i);
7975 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7978 lun->pr_key_count = 1;
7979 lun->res_type = msg->pr.pr_info.res_type;
7980 if (lun->res_type != SPR_TYPE_WR_EX_AR
7981 && lun->res_type != SPR_TYPE_EX_AC_AR)
7982 lun->pr_res_idx = msg->pr.pr_info.residx;
7984 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7985 if (sa_res_key == ctl_get_prkey(lun, i))
7988 ctl_clr_prkey(lun, i);
7989 lun->pr_key_count--;
7990 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7994 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7995 if (i == msg->pr.pr_info.residx ||
7996 ctl_get_prkey(lun, i) == 0)
7999 if (sa_res_key == ctl_get_prkey(lun, i)) {
8000 ctl_clr_prkey(lun, i);
8001 lun->pr_key_count--;
8002 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8003 } else if (msg->pr.pr_info.res_type != lun->res_type
8004 && (lun->res_type == SPR_TYPE_WR_EX_RO
8005 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
8006 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8009 lun->res_type = msg->pr.pr_info.res_type;
8010 if (lun->res_type != SPR_TYPE_WR_EX_AR
8011 && lun->res_type != SPR_TYPE_EX_AC_AR)
8012 lun->pr_res_idx = msg->pr.pr_info.residx;
8014 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8016 lun->PRGeneration++;
8022 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8025 u_int32_t param_len;
8026 struct scsi_per_res_out *cdb;
8027 struct ctl_lun *lun;
8028 struct scsi_per_res_out_parms* param;
8029 struct ctl_softc *softc;
8031 uint64_t res_key, sa_res_key, key;
8033 union ctl_ha_msg persis_io;
8036 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8038 retval = CTL_RETVAL_COMPLETE;
8040 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8041 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8042 softc = lun->ctl_softc;
8045 * We only support whole-LUN scope. The scope & type are ignored for
8046 * register, register and ignore existing key and clear.
8047 * We sometimes ignore scope and type on preempts too!!
8048 * Verify reservation type here as well.
8050 type = cdb->scope_type & SPR_TYPE_MASK;
8051 if ((cdb->action == SPRO_RESERVE)
8052 || (cdb->action == SPRO_RELEASE)) {
8053 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8054 ctl_set_invalid_field(/*ctsio*/ ctsio,
8060 ctl_done((union ctl_io *)ctsio);
8061 return (CTL_RETVAL_COMPLETE);
8064 if (type>8 || type==2 || type==4 || type==0) {
8065 ctl_set_invalid_field(/*ctsio*/ ctsio,
8071 ctl_done((union ctl_io *)ctsio);
8072 return (CTL_RETVAL_COMPLETE);
8076 param_len = scsi_4btoul(cdb->length);
8078 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8079 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8080 ctsio->kern_data_len = param_len;
8081 ctsio->kern_total_len = param_len;
8082 ctsio->kern_data_resid = 0;
8083 ctsio->kern_rel_offset = 0;
8084 ctsio->kern_sg_entries = 0;
8085 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8086 ctsio->be_move_done = ctl_config_move_done;
8087 ctl_datamove((union ctl_io *)ctsio);
8089 return (CTL_RETVAL_COMPLETE);
8092 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8094 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8095 res_key = scsi_8btou64(param->res_key.key);
8096 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8099 * Validate the reservation key here except for SPRO_REG_IGNO
8100 * This must be done for all other service actions
8102 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8103 mtx_lock(&lun->lun_lock);
8104 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8105 if (res_key != key) {
8107 * The current key passed in doesn't match
8108 * the one the initiator previously
8111 mtx_unlock(&lun->lun_lock);
8112 free(ctsio->kern_data_ptr, M_CTL);
8113 ctl_set_reservation_conflict(ctsio);
8114 ctl_done((union ctl_io *)ctsio);
8115 return (CTL_RETVAL_COMPLETE);
8117 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8119 * We are not registered
8121 mtx_unlock(&lun->lun_lock);
8122 free(ctsio->kern_data_ptr, M_CTL);
8123 ctl_set_reservation_conflict(ctsio);
8124 ctl_done((union ctl_io *)ctsio);
8125 return (CTL_RETVAL_COMPLETE);
8126 } else if (res_key != 0) {
8128 * We are not registered and trying to register but
8129 * the register key isn't zero.
8131 mtx_unlock(&lun->lun_lock);
8132 free(ctsio->kern_data_ptr, M_CTL);
8133 ctl_set_reservation_conflict(ctsio);
8134 ctl_done((union ctl_io *)ctsio);
8135 return (CTL_RETVAL_COMPLETE);
8137 mtx_unlock(&lun->lun_lock);
8140 switch (cdb->action & SPRO_ACTION_MASK) {
8142 case SPRO_REG_IGNO: {
8145 printf("Registration received\n");
8149 * We don't support any of these options, as we report in
8150 * the read capabilities request (see
8151 * ctl_persistent_reserve_in(), above).
8153 if ((param->flags & SPR_SPEC_I_PT)
8154 || (param->flags & SPR_ALL_TG_PT)
8155 || (param->flags & SPR_APTPL)) {
8158 if (param->flags & SPR_APTPL)
8160 else if (param->flags & SPR_ALL_TG_PT)
8162 else /* SPR_SPEC_I_PT */
8165 free(ctsio->kern_data_ptr, M_CTL);
8166 ctl_set_invalid_field(ctsio,
8172 ctl_done((union ctl_io *)ctsio);
8173 return (CTL_RETVAL_COMPLETE);
8176 mtx_lock(&lun->lun_lock);
8179 * The initiator wants to clear the
8182 if (sa_res_key == 0) {
8184 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8185 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8186 && ctl_get_prkey(lun, residx) == 0)) {
8187 mtx_unlock(&lun->lun_lock);
8191 ctl_clr_prkey(lun, residx);
8192 lun->pr_key_count--;
8194 if (residx == lun->pr_res_idx) {
8195 lun->flags &= ~CTL_LUN_PR_RESERVED;
8196 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8198 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8199 || lun->res_type == SPR_TYPE_EX_AC_RO)
8200 && lun->pr_key_count) {
8202 * If the reservation is a registrants
8203 * only type we need to generate a UA
8204 * for other registered inits. The
8205 * sense code should be RESERVATIONS
8209 for (i = softc->init_min; i < softc->init_max; i++){
8210 if (ctl_get_prkey(lun, i) == 0)
8213 CTL_UA_RES_RELEASE);
8217 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8218 if (lun->pr_key_count==0) {
8219 lun->flags &= ~CTL_LUN_PR_RESERVED;
8221 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8224 lun->PRGeneration++;
8225 mtx_unlock(&lun->lun_lock);
8227 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8228 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8229 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8230 persis_io.pr.pr_info.residx = residx;
8231 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8232 sizeof(persis_io.pr), M_WAITOK);
8233 } else /* sa_res_key != 0 */ {
8236 * If we aren't registered currently then increment
8237 * the key count and set the registered flag.
8239 ctl_alloc_prkey(lun, residx);
8240 if (ctl_get_prkey(lun, residx) == 0)
8241 lun->pr_key_count++;
8242 ctl_set_prkey(lun, residx, sa_res_key);
8243 lun->PRGeneration++;
8244 mtx_unlock(&lun->lun_lock);
8246 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8247 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8248 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8249 persis_io.pr.pr_info.residx = residx;
8250 memcpy(persis_io.pr.pr_info.sa_res_key,
8251 param->serv_act_res_key,
8252 sizeof(param->serv_act_res_key));
8253 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8254 sizeof(persis_io.pr), M_WAITOK);
8261 printf("Reserve executed type %d\n", type);
8263 mtx_lock(&lun->lun_lock);
8264 if (lun->flags & CTL_LUN_PR_RESERVED) {
8266 * if this isn't the reservation holder and it's
8267 * not a "all registrants" type or if the type is
8268 * different then we have a conflict
8270 if ((lun->pr_res_idx != residx
8271 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8272 || lun->res_type != type) {
8273 mtx_unlock(&lun->lun_lock);
8274 free(ctsio->kern_data_ptr, M_CTL);
8275 ctl_set_reservation_conflict(ctsio);
8276 ctl_done((union ctl_io *)ctsio);
8277 return (CTL_RETVAL_COMPLETE);
8279 mtx_unlock(&lun->lun_lock);
8280 } else /* create a reservation */ {
8282 * If it's not an "all registrants" type record
8283 * reservation holder
8285 if (type != SPR_TYPE_WR_EX_AR
8286 && type != SPR_TYPE_EX_AC_AR)
8287 lun->pr_res_idx = residx; /* Res holder */
8289 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8291 lun->flags |= CTL_LUN_PR_RESERVED;
8292 lun->res_type = type;
8294 mtx_unlock(&lun->lun_lock);
8296 /* send msg to other side */
8297 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8298 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8299 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8300 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8301 persis_io.pr.pr_info.res_type = type;
8302 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8303 sizeof(persis_io.pr), M_WAITOK);
8308 mtx_lock(&lun->lun_lock);
8309 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8310 /* No reservation exists return good status */
8311 mtx_unlock(&lun->lun_lock);
8315 * Is this nexus a reservation holder?
8317 if (lun->pr_res_idx != residx
8318 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8320 * not a res holder return good status but
8323 mtx_unlock(&lun->lun_lock);
8327 if (lun->res_type != type) {
8328 mtx_unlock(&lun->lun_lock);
8329 free(ctsio->kern_data_ptr, M_CTL);
8330 ctl_set_illegal_pr_release(ctsio);
8331 ctl_done((union ctl_io *)ctsio);
8332 return (CTL_RETVAL_COMPLETE);
8335 /* okay to release */
8336 lun->flags &= ~CTL_LUN_PR_RESERVED;
8337 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8341 * if this isn't an exclusive access
8342 * res generate UA for all other
8345 if (type != SPR_TYPE_EX_AC
8346 && type != SPR_TYPE_WR_EX) {
8347 for (i = softc->init_min; i < softc->init_max; i++) {
8348 if (i == residx || ctl_get_prkey(lun, i) == 0)
8350 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8353 mtx_unlock(&lun->lun_lock);
8355 /* Send msg to other side */
8356 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8357 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8358 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8359 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8360 sizeof(persis_io.pr), M_WAITOK);
8364 /* send msg to other side */
8366 mtx_lock(&lun->lun_lock);
8367 lun->flags &= ~CTL_LUN_PR_RESERVED;
8369 lun->pr_key_count = 0;
8370 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8372 ctl_clr_prkey(lun, residx);
8373 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8374 if (ctl_get_prkey(lun, i) != 0) {
8375 ctl_clr_prkey(lun, i);
8376 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8378 lun->PRGeneration++;
8379 mtx_unlock(&lun->lun_lock);
8381 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8382 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8383 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8384 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8385 sizeof(persis_io.pr), M_WAITOK);
8389 case SPRO_PRE_ABO: {
8392 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8393 residx, ctsio, cdb, param);
8395 return (CTL_RETVAL_COMPLETE);
8399 panic("Invalid PR type %x", cdb->action);
8403 free(ctsio->kern_data_ptr, M_CTL);
8404 ctl_set_success(ctsio);
8405 ctl_done((union ctl_io *)ctsio);
8411 * This routine is for handling a message from the other SC pertaining to
8412 * persistent reserve out. All the error checking will have been done
8413 * so only perorming the action need be done here to keep the two
8417 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8419 struct ctl_lun *lun;
8420 struct ctl_softc *softc;
8422 uint32_t residx, targ_lun;
8424 softc = control_softc;
8425 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8426 mtx_lock(&softc->ctl_lock);
8427 if ((targ_lun >= CTL_MAX_LUNS) ||
8428 ((lun = softc->ctl_luns[targ_lun]) == NULL)) {
8429 mtx_unlock(&softc->ctl_lock);
8432 mtx_lock(&lun->lun_lock);
8433 mtx_unlock(&softc->ctl_lock);
8434 if (lun->flags & CTL_LUN_DISABLED) {
8435 mtx_unlock(&lun->lun_lock);
8438 residx = ctl_get_initindex(&msg->hdr.nexus);
8439 switch(msg->pr.pr_info.action) {
8440 case CTL_PR_REG_KEY:
8441 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8442 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8443 lun->pr_key_count++;
8444 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8445 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8446 lun->PRGeneration++;
8449 case CTL_PR_UNREG_KEY:
8450 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8451 lun->pr_key_count--;
8453 /* XXX Need to see if the reservation has been released */
8454 /* if so do we need to generate UA? */
8455 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8456 lun->flags &= ~CTL_LUN_PR_RESERVED;
8457 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8459 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8460 || lun->res_type == SPR_TYPE_EX_AC_RO)
8461 && lun->pr_key_count) {
8463 * If the reservation is a registrants
8464 * only type we need to generate a UA
8465 * for other registered inits. The
8466 * sense code should be RESERVATIONS
8470 for (i = softc->init_min; i < softc->init_max; i++) {
8471 if (ctl_get_prkey(lun, i) == 0)
8474 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8478 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8479 if (lun->pr_key_count==0) {
8480 lun->flags &= ~CTL_LUN_PR_RESERVED;
8482 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8485 lun->PRGeneration++;
8488 case CTL_PR_RESERVE:
8489 lun->flags |= CTL_LUN_PR_RESERVED;
8490 lun->res_type = msg->pr.pr_info.res_type;
8491 lun->pr_res_idx = msg->pr.pr_info.residx;
8495 case CTL_PR_RELEASE:
8497 * if this isn't an exclusive access res generate UA for all
8498 * other registrants.
8500 if (lun->res_type != SPR_TYPE_EX_AC
8501 && lun->res_type != SPR_TYPE_WR_EX) {
8502 for (i = softc->init_min; i < softc->init_max; i++)
8503 if (i == residx || ctl_get_prkey(lun, i) == 0)
8505 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8508 lun->flags &= ~CTL_LUN_PR_RESERVED;
8509 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8513 case CTL_PR_PREEMPT:
8514 ctl_pro_preempt_other(lun, msg);
8517 lun->flags &= ~CTL_LUN_PR_RESERVED;
8519 lun->pr_key_count = 0;
8520 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8522 for (i=0; i < CTL_MAX_INITIATORS; i++) {
8523 if (ctl_get_prkey(lun, i) == 0)
8525 ctl_clr_prkey(lun, i);
8526 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8528 lun->PRGeneration++;
8532 mtx_unlock(&lun->lun_lock);
8536 ctl_read_write(struct ctl_scsiio *ctsio)
8538 struct ctl_lun *lun;
8539 struct ctl_lba_len_flags *lbalen;
8541 uint32_t num_blocks;
8545 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8547 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8550 retval = CTL_RETVAL_COMPLETE;
8552 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8553 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8554 switch (ctsio->cdb[0]) {
8557 struct scsi_rw_6 *cdb;
8559 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8561 lba = scsi_3btoul(cdb->addr);
8562 /* only 5 bits are valid in the most significant address byte */
8564 num_blocks = cdb->length;
8566 * This is correct according to SBC-2.
8568 if (num_blocks == 0)
8574 struct scsi_rw_10 *cdb;
8576 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8577 if (cdb->byte2 & SRW10_FUA)
8578 flags |= CTL_LLF_FUA;
8579 if (cdb->byte2 & SRW10_DPO)
8580 flags |= CTL_LLF_DPO;
8581 lba = scsi_4btoul(cdb->addr);
8582 num_blocks = scsi_2btoul(cdb->length);
8585 case WRITE_VERIFY_10: {
8586 struct scsi_write_verify_10 *cdb;
8588 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8589 flags |= CTL_LLF_FUA;
8590 if (cdb->byte2 & SWV_DPO)
8591 flags |= CTL_LLF_DPO;
8592 lba = scsi_4btoul(cdb->addr);
8593 num_blocks = scsi_2btoul(cdb->length);
8598 struct scsi_rw_12 *cdb;
8600 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8601 if (cdb->byte2 & SRW12_FUA)
8602 flags |= CTL_LLF_FUA;
8603 if (cdb->byte2 & SRW12_DPO)
8604 flags |= CTL_LLF_DPO;
8605 lba = scsi_4btoul(cdb->addr);
8606 num_blocks = scsi_4btoul(cdb->length);
8609 case WRITE_VERIFY_12: {
8610 struct scsi_write_verify_12 *cdb;
8612 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8613 flags |= CTL_LLF_FUA;
8614 if (cdb->byte2 & SWV_DPO)
8615 flags |= CTL_LLF_DPO;
8616 lba = scsi_4btoul(cdb->addr);
8617 num_blocks = scsi_4btoul(cdb->length);
8622 struct scsi_rw_16 *cdb;
8624 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8625 if (cdb->byte2 & SRW12_FUA)
8626 flags |= CTL_LLF_FUA;
8627 if (cdb->byte2 & SRW12_DPO)
8628 flags |= CTL_LLF_DPO;
8629 lba = scsi_8btou64(cdb->addr);
8630 num_blocks = scsi_4btoul(cdb->length);
8633 case WRITE_ATOMIC_16: {
8634 struct scsi_rw_16 *cdb;
8636 if (lun->be_lun->atomicblock == 0) {
8637 ctl_set_invalid_opcode(ctsio);
8638 ctl_done((union ctl_io *)ctsio);
8639 return (CTL_RETVAL_COMPLETE);
8642 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8643 if (cdb->byte2 & SRW12_FUA)
8644 flags |= CTL_LLF_FUA;
8645 if (cdb->byte2 & SRW12_DPO)
8646 flags |= CTL_LLF_DPO;
8647 lba = scsi_8btou64(cdb->addr);
8648 num_blocks = scsi_4btoul(cdb->length);
8649 if (num_blocks > lun->be_lun->atomicblock) {
8650 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8651 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8653 ctl_done((union ctl_io *)ctsio);
8654 return (CTL_RETVAL_COMPLETE);
8658 case WRITE_VERIFY_16: {
8659 struct scsi_write_verify_16 *cdb;
8661 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8662 flags |= CTL_LLF_FUA;
8663 if (cdb->byte2 & SWV_DPO)
8664 flags |= CTL_LLF_DPO;
8665 lba = scsi_8btou64(cdb->addr);
8666 num_blocks = scsi_4btoul(cdb->length);
8671 * We got a command we don't support. This shouldn't
8672 * happen, commands should be filtered out above us.
8674 ctl_set_invalid_opcode(ctsio);
8675 ctl_done((union ctl_io *)ctsio);
8677 return (CTL_RETVAL_COMPLETE);
8678 break; /* NOTREACHED */
8682 * The first check is to make sure we're in bounds, the second
8683 * check is to catch wrap-around problems. If the lba + num blocks
8684 * is less than the lba, then we've wrapped around and the block
8685 * range is invalid anyway.
8687 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8688 || ((lba + num_blocks) < lba)) {
8689 ctl_set_lba_out_of_range(ctsio);
8690 ctl_done((union ctl_io *)ctsio);
8691 return (CTL_RETVAL_COMPLETE);
8695 * According to SBC-3, a transfer length of 0 is not an error.
8696 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8697 * translates to 256 blocks for those commands.
8699 if (num_blocks == 0) {
8700 ctl_set_success(ctsio);
8701 ctl_done((union ctl_io *)ctsio);
8702 return (CTL_RETVAL_COMPLETE);
8705 /* Set FUA and/or DPO if caches are disabled. */
8707 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8709 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8711 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8713 flags |= CTL_LLF_FUA;
8716 lbalen = (struct ctl_lba_len_flags *)
8717 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8719 lbalen->len = num_blocks;
8720 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8722 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8723 ctsio->kern_rel_offset = 0;
8725 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8727 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8733 ctl_cnw_cont(union ctl_io *io)
8735 struct ctl_scsiio *ctsio;
8736 struct ctl_lun *lun;
8737 struct ctl_lba_len_flags *lbalen;
8740 ctsio = &io->scsiio;
8741 ctsio->io_hdr.status = CTL_STATUS_NONE;
8742 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8743 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8744 lbalen = (struct ctl_lba_len_flags *)
8745 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8746 lbalen->flags &= ~CTL_LLF_COMPARE;
8747 lbalen->flags |= CTL_LLF_WRITE;
8749 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8750 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8755 ctl_cnw(struct ctl_scsiio *ctsio)
8757 struct ctl_lun *lun;
8758 struct ctl_lba_len_flags *lbalen;
8760 uint32_t num_blocks;
8763 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8765 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8768 retval = CTL_RETVAL_COMPLETE;
8770 switch (ctsio->cdb[0]) {
8771 case COMPARE_AND_WRITE: {
8772 struct scsi_compare_and_write *cdb;
8774 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8775 if (cdb->byte2 & SRW10_FUA)
8776 flags |= CTL_LLF_FUA;
8777 if (cdb->byte2 & SRW10_DPO)
8778 flags |= CTL_LLF_DPO;
8779 lba = scsi_8btou64(cdb->addr);
8780 num_blocks = cdb->length;
8785 * We got a command we don't support. This shouldn't
8786 * happen, commands should be filtered out above us.
8788 ctl_set_invalid_opcode(ctsio);
8789 ctl_done((union ctl_io *)ctsio);
8791 return (CTL_RETVAL_COMPLETE);
8792 break; /* NOTREACHED */
8796 * The first check is to make sure we're in bounds, the second
8797 * check is to catch wrap-around problems. If the lba + num blocks
8798 * is less than the lba, then we've wrapped around and the block
8799 * range is invalid anyway.
8801 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8802 || ((lba + num_blocks) < lba)) {
8803 ctl_set_lba_out_of_range(ctsio);
8804 ctl_done((union ctl_io *)ctsio);
8805 return (CTL_RETVAL_COMPLETE);
8809 * According to SBC-3, a transfer length of 0 is not an error.
8811 if (num_blocks == 0) {
8812 ctl_set_success(ctsio);
8813 ctl_done((union ctl_io *)ctsio);
8814 return (CTL_RETVAL_COMPLETE);
8817 /* Set FUA if write cache is disabled. */
8818 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8820 flags |= CTL_LLF_FUA;
8822 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
8823 ctsio->kern_rel_offset = 0;
8826 * Set the IO_CONT flag, so that if this I/O gets passed to
8827 * ctl_data_submit_done(), it'll get passed back to
8828 * ctl_ctl_cnw_cont() for further processing.
8830 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
8831 ctsio->io_cont = ctl_cnw_cont;
8833 lbalen = (struct ctl_lba_len_flags *)
8834 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8836 lbalen->len = num_blocks;
8837 lbalen->flags = CTL_LLF_COMPARE | flags;
8839 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
8840 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8845 ctl_verify(struct ctl_scsiio *ctsio)
8847 struct ctl_lun *lun;
8848 struct ctl_lba_len_flags *lbalen;
8850 uint32_t num_blocks;
8854 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8856 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
8859 flags = CTL_LLF_FUA;
8860 retval = CTL_RETVAL_COMPLETE;
8862 switch (ctsio->cdb[0]) {
8864 struct scsi_verify_10 *cdb;
8866 cdb = (struct scsi_verify_10 *)ctsio->cdb;
8867 if (cdb->byte2 & SVFY_BYTCHK)
8869 if (cdb->byte2 & SVFY_DPO)
8870 flags |= CTL_LLF_DPO;
8871 lba = scsi_4btoul(cdb->addr);
8872 num_blocks = scsi_2btoul(cdb->length);
8876 struct scsi_verify_12 *cdb;
8878 cdb = (struct scsi_verify_12 *)ctsio->cdb;
8879 if (cdb->byte2 & SVFY_BYTCHK)
8881 if (cdb->byte2 & SVFY_DPO)
8882 flags |= CTL_LLF_DPO;
8883 lba = scsi_4btoul(cdb->addr);
8884 num_blocks = scsi_4btoul(cdb->length);
8888 struct scsi_rw_16 *cdb;
8890 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8891 if (cdb->byte2 & SVFY_BYTCHK)
8893 if (cdb->byte2 & SVFY_DPO)
8894 flags |= CTL_LLF_DPO;
8895 lba = scsi_8btou64(cdb->addr);
8896 num_blocks = scsi_4btoul(cdb->length);
8901 * We got a command we don't support. This shouldn't
8902 * happen, commands should be filtered out above us.
8904 ctl_set_invalid_opcode(ctsio);
8905 ctl_done((union ctl_io *)ctsio);
8906 return (CTL_RETVAL_COMPLETE);
8910 * The first check is to make sure we're in bounds, the second
8911 * check is to catch wrap-around problems. If the lba + num blocks
8912 * is less than the lba, then we've wrapped around and the block
8913 * range is invalid anyway.
8915 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8916 || ((lba + num_blocks) < lba)) {
8917 ctl_set_lba_out_of_range(ctsio);
8918 ctl_done((union ctl_io *)ctsio);
8919 return (CTL_RETVAL_COMPLETE);
8923 * According to SBC-3, a transfer length of 0 is not an error.
8925 if (num_blocks == 0) {
8926 ctl_set_success(ctsio);
8927 ctl_done((union ctl_io *)ctsio);
8928 return (CTL_RETVAL_COMPLETE);
8931 lbalen = (struct ctl_lba_len_flags *)
8932 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8934 lbalen->len = num_blocks;
8936 lbalen->flags = CTL_LLF_COMPARE | flags;
8937 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8939 lbalen->flags = CTL_LLF_VERIFY | flags;
8940 ctsio->kern_total_len = 0;
8942 ctsio->kern_rel_offset = 0;
8944 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
8945 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8950 ctl_report_luns(struct ctl_scsiio *ctsio)
8952 struct ctl_softc *softc = control_softc;
8953 struct scsi_report_luns *cdb;
8954 struct scsi_report_luns_data *lun_data;
8955 struct ctl_lun *lun, *request_lun;
8956 struct ctl_port *port;
8957 int num_luns, retval;
8958 uint32_t alloc_len, lun_datalen;
8959 int num_filled, well_known;
8960 uint32_t initidx, targ_lun_id, lun_id;
8962 retval = CTL_RETVAL_COMPLETE;
8965 cdb = (struct scsi_report_luns *)ctsio->cdb;
8966 port = ctl_io_port(&ctsio->io_hdr);
8968 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
8970 mtx_lock(&softc->ctl_lock);
8972 for (targ_lun_id = 0; targ_lun_id < CTL_MAX_LUNS; targ_lun_id++) {
8973 if (ctl_lun_map_from_port(port, targ_lun_id) < CTL_MAX_LUNS)
8976 mtx_unlock(&softc->ctl_lock);
8978 switch (cdb->select_report) {
8979 case RPL_REPORT_DEFAULT:
8980 case RPL_REPORT_ALL:
8982 case RPL_REPORT_WELLKNOWN:
8987 ctl_set_invalid_field(ctsio,
8993 ctl_done((union ctl_io *)ctsio);
8995 break; /* NOTREACHED */
8998 alloc_len = scsi_4btoul(cdb->length);
9000 * The initiator has to allocate at least 16 bytes for this request,
9001 * so he can at least get the header and the first LUN. Otherwise
9002 * we reject the request (per SPC-3 rev 14, section 6.21).
9004 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9005 sizeof(struct scsi_report_luns_lundata))) {
9006 ctl_set_invalid_field(ctsio,
9012 ctl_done((union ctl_io *)ctsio);
9016 request_lun = (struct ctl_lun *)
9017 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9019 lun_datalen = sizeof(*lun_data) +
9020 (num_luns * sizeof(struct scsi_report_luns_lundata));
9022 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9023 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9024 ctsio->kern_sg_entries = 0;
9026 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9028 mtx_lock(&softc->ctl_lock);
9029 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9030 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9031 if (lun_id >= CTL_MAX_LUNS)
9033 lun = softc->ctl_luns[lun_id];
9037 if (targ_lun_id <= 0xff) {
9039 * Peripheral addressing method, bus number 0.
9041 lun_data->luns[num_filled].lundata[0] =
9042 RPL_LUNDATA_ATYP_PERIPH;
9043 lun_data->luns[num_filled].lundata[1] = targ_lun_id;
9045 } else if (targ_lun_id <= 0x3fff) {
9047 * Flat addressing method.
9049 lun_data->luns[num_filled].lundata[0] =
9050 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8);
9051 lun_data->luns[num_filled].lundata[1] =
9052 (targ_lun_id & 0xff);
9054 } else if (targ_lun_id <= 0xffffff) {
9056 * Extended flat addressing method.
9058 lun_data->luns[num_filled].lundata[0] =
9059 RPL_LUNDATA_ATYP_EXTLUN | 0x12;
9060 scsi_ulto3b(targ_lun_id,
9061 &lun_data->luns[num_filled].lundata[1]);
9064 printf("ctl_report_luns: bogus LUN number %jd, "
9065 "skipping\n", (intmax_t)targ_lun_id);
9068 * According to SPC-3, rev 14 section 6.21:
9070 * "The execution of a REPORT LUNS command to any valid and
9071 * installed logical unit shall clear the REPORTED LUNS DATA
9072 * HAS CHANGED unit attention condition for all logical
9073 * units of that target with respect to the requesting
9074 * initiator. A valid and installed logical unit is one
9075 * having a PERIPHERAL QUALIFIER of 000b in the standard
9076 * INQUIRY data (see 6.4.2)."
9078 * If request_lun is NULL, the LUN this report luns command
9079 * was issued to is either disabled or doesn't exist. In that
9080 * case, we shouldn't clear any pending lun change unit
9083 if (request_lun != NULL) {
9084 mtx_lock(&lun->lun_lock);
9085 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE);
9086 mtx_unlock(&lun->lun_lock);
9089 mtx_unlock(&softc->ctl_lock);
9092 * It's quite possible that we've returned fewer LUNs than we allocated
9093 * space for. Trim it.
9095 lun_datalen = sizeof(*lun_data) +
9096 (num_filled * sizeof(struct scsi_report_luns_lundata));
9098 if (lun_datalen < alloc_len) {
9099 ctsio->residual = alloc_len - lun_datalen;
9100 ctsio->kern_data_len = lun_datalen;
9101 ctsio->kern_total_len = lun_datalen;
9103 ctsio->residual = 0;
9104 ctsio->kern_data_len = alloc_len;
9105 ctsio->kern_total_len = alloc_len;
9107 ctsio->kern_data_resid = 0;
9108 ctsio->kern_rel_offset = 0;
9109 ctsio->kern_sg_entries = 0;
9112 * We set this to the actual data length, regardless of how much
9113 * space we actually have to return results. If the user looks at
9114 * this value, he'll know whether or not he allocated enough space
9115 * and reissue the command if necessary. We don't support well
9116 * known logical units, so if the user asks for that, return none.
9118 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9121 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9124 ctl_set_success(ctsio);
9125 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9126 ctsio->be_move_done = ctl_config_move_done;
9127 ctl_datamove((union ctl_io *)ctsio);
9132 ctl_request_sense(struct ctl_scsiio *ctsio)
9134 struct scsi_request_sense *cdb;
9135 struct scsi_sense_data *sense_ptr;
9136 struct ctl_softc *ctl_softc;
9137 struct ctl_lun *lun;
9140 scsi_sense_data_type sense_format;
9141 ctl_ua_type ua_type;
9143 cdb = (struct scsi_request_sense *)ctsio->cdb;
9145 ctl_softc = control_softc;
9146 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9148 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9151 * Determine which sense format the user wants.
9153 if (cdb->byte2 & SRS_DESC)
9154 sense_format = SSD_TYPE_DESC;
9156 sense_format = SSD_TYPE_FIXED;
9158 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9159 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9160 ctsio->kern_sg_entries = 0;
9163 * struct scsi_sense_data, which is currently set to 256 bytes, is
9164 * larger than the largest allowed value for the length field in the
9165 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9167 ctsio->residual = 0;
9168 ctsio->kern_data_len = cdb->length;
9169 ctsio->kern_total_len = cdb->length;
9171 ctsio->kern_data_resid = 0;
9172 ctsio->kern_rel_offset = 0;
9173 ctsio->kern_sg_entries = 0;
9176 * If we don't have a LUN, we don't have any pending sense.
9182 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9184 * Check for pending sense, and then for pending unit attentions.
9185 * Pending sense gets returned first, then pending unit attentions.
9187 mtx_lock(&lun->lun_lock);
9189 if (ctl_is_set(lun->have_ca, initidx)) {
9190 scsi_sense_data_type stored_format;
9193 * Check to see which sense format was used for the stored
9196 stored_format = scsi_sense_type(&lun->pending_sense[initidx]);
9199 * If the user requested a different sense format than the
9200 * one we stored, then we need to convert it to the other
9201 * format. If we're going from descriptor to fixed format
9202 * sense data, we may lose things in translation, depending
9203 * on what options were used.
9205 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9206 * for some reason we'll just copy it out as-is.
9208 if ((stored_format == SSD_TYPE_FIXED)
9209 && (sense_format == SSD_TYPE_DESC))
9210 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9211 &lun->pending_sense[initidx],
9212 (struct scsi_sense_data_desc *)sense_ptr);
9213 else if ((stored_format == SSD_TYPE_DESC)
9214 && (sense_format == SSD_TYPE_FIXED))
9215 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9216 &lun->pending_sense[initidx],
9217 (struct scsi_sense_data_fixed *)sense_ptr);
9219 memcpy(sense_ptr, &lun->pending_sense[initidx],
9220 MIN(sizeof(*sense_ptr),
9221 sizeof(lun->pending_sense[initidx])));
9223 ctl_clear_mask(lun->have_ca, initidx);
9228 ua_type = ctl_build_ua(lun, initidx, sense_ptr, sense_format);
9229 if (ua_type != CTL_UA_NONE)
9231 if (ua_type == CTL_UA_LUN_CHANGE) {
9232 mtx_unlock(&lun->lun_lock);
9233 mtx_lock(&ctl_softc->ctl_lock);
9234 ctl_clr_ua_allluns(ctl_softc, initidx, ua_type);
9235 mtx_unlock(&ctl_softc->ctl_lock);
9236 mtx_lock(&lun->lun_lock);
9240 mtx_unlock(&lun->lun_lock);
9243 * We already have a pending error, return it.
9245 if (have_error != 0) {
9247 * We report the SCSI status as OK, since the status of the
9248 * request sense command itself is OK.
9249 * We report 0 for the sense length, because we aren't doing
9250 * autosense in this case. We're reporting sense as
9253 ctl_set_success(ctsio);
9254 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9255 ctsio->be_move_done = ctl_config_move_done;
9256 ctl_datamove((union ctl_io *)ctsio);
9257 return (CTL_RETVAL_COMPLETE);
9263 * No sense information to report, so we report that everything is
9266 ctl_set_sense_data(sense_ptr,
9269 /*current_error*/ 1,
9270 /*sense_key*/ SSD_KEY_NO_SENSE,
9276 * We report 0 for the sense length, because we aren't doing
9277 * autosense in this case. We're reporting sense as parameter data.
9279 ctl_set_success(ctsio);
9280 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9281 ctsio->be_move_done = ctl_config_move_done;
9282 ctl_datamove((union ctl_io *)ctsio);
9283 return (CTL_RETVAL_COMPLETE);
9287 ctl_tur(struct ctl_scsiio *ctsio)
9290 CTL_DEBUG_PRINT(("ctl_tur\n"));
9292 ctl_set_success(ctsio);
9293 ctl_done((union ctl_io *)ctsio);
9295 return (CTL_RETVAL_COMPLETE);
9299 * SCSI VPD page 0x00, the Supported VPD Pages page.
9302 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9304 struct scsi_vpd_supported_pages *pages;
9306 struct ctl_lun *lun;
9309 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9311 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9312 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9313 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9314 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9315 ctsio->kern_sg_entries = 0;
9317 if (sup_page_size < alloc_len) {
9318 ctsio->residual = alloc_len - sup_page_size;
9319 ctsio->kern_data_len = sup_page_size;
9320 ctsio->kern_total_len = sup_page_size;
9322 ctsio->residual = 0;
9323 ctsio->kern_data_len = alloc_len;
9324 ctsio->kern_total_len = alloc_len;
9326 ctsio->kern_data_resid = 0;
9327 ctsio->kern_rel_offset = 0;
9328 ctsio->kern_sg_entries = 0;
9331 * The control device is always connected. The disk device, on the
9332 * other hand, may not be online all the time. Need to change this
9333 * to figure out whether the disk device is actually online or not.
9336 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9337 lun->be_lun->lun_type;
9339 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9342 /* Supported VPD pages */
9343 pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9345 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9346 /* Device Identification */
9347 pages->page_list[p++] = SVPD_DEVICE_ID;
9348 /* Extended INQUIRY Data */
9349 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9350 /* Mode Page Policy */
9351 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9353 pages->page_list[p++] = SVPD_SCSI_PORTS;
9354 /* Third-party Copy */
9355 pages->page_list[p++] = SVPD_SCSI_TPC;
9356 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9358 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9359 /* Block Device Characteristics */
9360 pages->page_list[p++] = SVPD_BDC;
9361 /* Logical Block Provisioning */
9362 pages->page_list[p++] = SVPD_LBP;
9366 ctl_set_success(ctsio);
9367 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9368 ctsio->be_move_done = ctl_config_move_done;
9369 ctl_datamove((union ctl_io *)ctsio);
9370 return (CTL_RETVAL_COMPLETE);
9374 * SCSI VPD page 0x80, the Unit Serial Number page.
9377 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9379 struct scsi_vpd_unit_serial_number *sn_ptr;
9380 struct ctl_lun *lun;
9383 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9385 data_len = 4 + CTL_SN_LEN;
9386 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9387 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9388 if (data_len < alloc_len) {
9389 ctsio->residual = alloc_len - data_len;
9390 ctsio->kern_data_len = data_len;
9391 ctsio->kern_total_len = data_len;
9393 ctsio->residual = 0;
9394 ctsio->kern_data_len = alloc_len;
9395 ctsio->kern_total_len = alloc_len;
9397 ctsio->kern_data_resid = 0;
9398 ctsio->kern_rel_offset = 0;
9399 ctsio->kern_sg_entries = 0;
9402 * The control device is always connected. The disk device, on the
9403 * other hand, may not be online all the time. Need to change this
9404 * to figure out whether the disk device is actually online or not.
9407 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9408 lun->be_lun->lun_type;
9410 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9412 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9413 sn_ptr->length = CTL_SN_LEN;
9415 * If we don't have a LUN, we just leave the serial number as
9419 strncpy((char *)sn_ptr->serial_num,
9420 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9422 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9424 ctl_set_success(ctsio);
9425 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9426 ctsio->be_move_done = ctl_config_move_done;
9427 ctl_datamove((union ctl_io *)ctsio);
9428 return (CTL_RETVAL_COMPLETE);
9433 * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9436 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9438 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9439 struct ctl_lun *lun;
9442 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9444 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9445 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9446 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9447 ctsio->kern_sg_entries = 0;
9449 if (data_len < alloc_len) {
9450 ctsio->residual = alloc_len - data_len;
9451 ctsio->kern_data_len = data_len;
9452 ctsio->kern_total_len = data_len;
9454 ctsio->residual = 0;
9455 ctsio->kern_data_len = alloc_len;
9456 ctsio->kern_total_len = alloc_len;
9458 ctsio->kern_data_resid = 0;
9459 ctsio->kern_rel_offset = 0;
9460 ctsio->kern_sg_entries = 0;
9463 * The control device is always connected. The disk device, on the
9464 * other hand, may not be online all the time.
9467 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9468 lun->be_lun->lun_type;
9470 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9471 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9472 scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9474 * We support head of queue, ordered and simple tags.
9476 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9478 * Volatile cache supported.
9480 eid_ptr->flags3 = SVPD_EID_V_SUP;
9483 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9484 * attention for a particular IT nexus on all LUNs once we report
9485 * it to that nexus once. This bit is required as of SPC-4.
9487 eid_ptr->flags4 = SVPD_EID_LUICLT;
9490 * XXX KDM in order to correctly answer this, we would need
9491 * information from the SIM to determine how much sense data it
9492 * can send. So this would really be a path inquiry field, most
9493 * likely. This can be set to a maximum of 252 according to SPC-4,
9494 * but the hardware may or may not be able to support that much.
9495 * 0 just means that the maximum sense data length is not reported.
9497 eid_ptr->max_sense_length = 0;
9499 ctl_set_success(ctsio);
9500 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9501 ctsio->be_move_done = ctl_config_move_done;
9502 ctl_datamove((union ctl_io *)ctsio);
9503 return (CTL_RETVAL_COMPLETE);
9507 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9509 struct scsi_vpd_mode_page_policy *mpp_ptr;
9510 struct ctl_lun *lun;
9513 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9515 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9516 sizeof(struct scsi_vpd_mode_page_policy_descr);
9518 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9519 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9520 ctsio->kern_sg_entries = 0;
9522 if (data_len < alloc_len) {
9523 ctsio->residual = alloc_len - data_len;
9524 ctsio->kern_data_len = data_len;
9525 ctsio->kern_total_len = data_len;
9527 ctsio->residual = 0;
9528 ctsio->kern_data_len = alloc_len;
9529 ctsio->kern_total_len = alloc_len;
9531 ctsio->kern_data_resid = 0;
9532 ctsio->kern_rel_offset = 0;
9533 ctsio->kern_sg_entries = 0;
9536 * The control device is always connected. The disk device, on the
9537 * other hand, may not be online all the time.
9540 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9541 lun->be_lun->lun_type;
9543 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9544 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9545 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9546 mpp_ptr->descr[0].page_code = 0x3f;
9547 mpp_ptr->descr[0].subpage_code = 0xff;
9548 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9550 ctl_set_success(ctsio);
9551 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9552 ctsio->be_move_done = ctl_config_move_done;
9553 ctl_datamove((union ctl_io *)ctsio);
9554 return (CTL_RETVAL_COMPLETE);
9558 * SCSI VPD page 0x83, the Device Identification page.
9561 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9563 struct scsi_vpd_device_id *devid_ptr;
9564 struct scsi_vpd_id_descriptor *desc;
9565 struct ctl_softc *softc;
9566 struct ctl_lun *lun;
9567 struct ctl_port *port;
9571 softc = control_softc;
9573 port = ctl_io_port(&ctsio->io_hdr);
9574 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9576 data_len = sizeof(struct scsi_vpd_device_id) +
9577 sizeof(struct scsi_vpd_id_descriptor) +
9578 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9579 sizeof(struct scsi_vpd_id_descriptor) +
9580 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9581 if (lun && lun->lun_devid)
9582 data_len += lun->lun_devid->len;
9583 if (port && port->port_devid)
9584 data_len += port->port_devid->len;
9585 if (port && port->target_devid)
9586 data_len += port->target_devid->len;
9588 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9589 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9590 ctsio->kern_sg_entries = 0;
9592 if (data_len < alloc_len) {
9593 ctsio->residual = alloc_len - data_len;
9594 ctsio->kern_data_len = data_len;
9595 ctsio->kern_total_len = data_len;
9597 ctsio->residual = 0;
9598 ctsio->kern_data_len = alloc_len;
9599 ctsio->kern_total_len = alloc_len;
9601 ctsio->kern_data_resid = 0;
9602 ctsio->kern_rel_offset = 0;
9603 ctsio->kern_sg_entries = 0;
9606 * The control device is always connected. The disk device, on the
9607 * other hand, may not be online all the time.
9610 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9611 lun->be_lun->lun_type;
9613 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9614 devid_ptr->page_code = SVPD_DEVICE_ID;
9615 scsi_ulto2b(data_len - 4, devid_ptr->length);
9617 if (port && port->port_type == CTL_PORT_FC)
9618 proto = SCSI_PROTO_FC << 4;
9619 else if (port && port->port_type == CTL_PORT_ISCSI)
9620 proto = SCSI_PROTO_ISCSI << 4;
9622 proto = SCSI_PROTO_SPI << 4;
9623 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9626 * We're using a LUN association here. i.e., this device ID is a
9627 * per-LUN identifier.
9629 if (lun && lun->lun_devid) {
9630 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9631 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9632 lun->lun_devid->len);
9636 * This is for the WWPN which is a port association.
9638 if (port && port->port_devid) {
9639 memcpy(desc, port->port_devid->data, port->port_devid->len);
9640 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9641 port->port_devid->len);
9645 * This is for the Relative Target Port(type 4h) identifier
9647 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9648 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9649 SVPD_ID_TYPE_RELTARG;
9651 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9652 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9653 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9656 * This is for the Target Port Group(type 5h) identifier
9658 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9659 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9660 SVPD_ID_TYPE_TPORTGRP;
9662 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / softc->port_cnt + 1,
9663 &desc->identifier[2]);
9664 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9665 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9668 * This is for the Target identifier
9670 if (port && port->target_devid) {
9671 memcpy(desc, port->target_devid->data, port->target_devid->len);
9674 ctl_set_success(ctsio);
9675 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9676 ctsio->be_move_done = ctl_config_move_done;
9677 ctl_datamove((union ctl_io *)ctsio);
9678 return (CTL_RETVAL_COMPLETE);
9682 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9684 struct ctl_softc *softc = control_softc;
9685 struct scsi_vpd_scsi_ports *sp;
9686 struct scsi_vpd_port_designation *pd;
9687 struct scsi_vpd_port_designation_cont *pdc;
9688 struct ctl_lun *lun;
9689 struct ctl_port *port;
9690 int data_len, num_target_ports, iid_len, id_len;
9692 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9694 num_target_ports = 0;
9697 mtx_lock(&softc->ctl_lock);
9698 STAILQ_FOREACH(port, &softc->port_list, links) {
9699 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9702 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
9705 if (port->init_devid)
9706 iid_len += port->init_devid->len;
9707 if (port->port_devid)
9708 id_len += port->port_devid->len;
9710 mtx_unlock(&softc->ctl_lock);
9712 data_len = sizeof(struct scsi_vpd_scsi_ports) +
9713 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9714 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9715 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9716 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9717 ctsio->kern_sg_entries = 0;
9719 if (data_len < alloc_len) {
9720 ctsio->residual = alloc_len - data_len;
9721 ctsio->kern_data_len = data_len;
9722 ctsio->kern_total_len = data_len;
9724 ctsio->residual = 0;
9725 ctsio->kern_data_len = alloc_len;
9726 ctsio->kern_total_len = alloc_len;
9728 ctsio->kern_data_resid = 0;
9729 ctsio->kern_rel_offset = 0;
9730 ctsio->kern_sg_entries = 0;
9733 * The control device is always connected. The disk device, on the
9734 * other hand, may not be online all the time. Need to change this
9735 * to figure out whether the disk device is actually online or not.
9738 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9739 lun->be_lun->lun_type;
9741 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9743 sp->page_code = SVPD_SCSI_PORTS;
9744 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9746 pd = &sp->design[0];
9748 mtx_lock(&softc->ctl_lock);
9749 STAILQ_FOREACH(port, &softc->port_list, links) {
9750 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9753 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
9755 scsi_ulto2b(port->targ_port, pd->relative_port_id);
9756 if (port->init_devid) {
9757 iid_len = port->init_devid->len;
9758 memcpy(pd->initiator_transportid,
9759 port->init_devid->data, port->init_devid->len);
9762 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9763 pdc = (struct scsi_vpd_port_designation_cont *)
9764 (&pd->initiator_transportid[iid_len]);
9765 if (port->port_devid) {
9766 id_len = port->port_devid->len;
9767 memcpy(pdc->target_port_descriptors,
9768 port->port_devid->data, port->port_devid->len);
9771 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9772 pd = (struct scsi_vpd_port_designation *)
9773 ((uint8_t *)pdc->target_port_descriptors + id_len);
9775 mtx_unlock(&softc->ctl_lock);
9777 ctl_set_success(ctsio);
9778 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9779 ctsio->be_move_done = ctl_config_move_done;
9780 ctl_datamove((union ctl_io *)ctsio);
9781 return (CTL_RETVAL_COMPLETE);
9785 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9787 struct scsi_vpd_block_limits *bl_ptr;
9788 struct ctl_lun *lun;
9791 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9793 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9794 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9795 ctsio->kern_sg_entries = 0;
9797 if (sizeof(*bl_ptr) < alloc_len) {
9798 ctsio->residual = alloc_len - sizeof(*bl_ptr);
9799 ctsio->kern_data_len = sizeof(*bl_ptr);
9800 ctsio->kern_total_len = sizeof(*bl_ptr);
9802 ctsio->residual = 0;
9803 ctsio->kern_data_len = alloc_len;
9804 ctsio->kern_total_len = alloc_len;
9806 ctsio->kern_data_resid = 0;
9807 ctsio->kern_rel_offset = 0;
9808 ctsio->kern_sg_entries = 0;
9811 * The control device is always connected. The disk device, on the
9812 * other hand, may not be online all the time. Need to change this
9813 * to figure out whether the disk device is actually online or not.
9816 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9817 lun->be_lun->lun_type;
9819 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9821 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9822 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
9823 bl_ptr->max_cmp_write_len = 0xff;
9824 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9826 bs = lun->be_lun->blocksize;
9827 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
9828 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9829 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
9830 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
9831 if (lun->be_lun->ublockexp != 0) {
9832 scsi_ulto4b((1 << lun->be_lun->ublockexp),
9833 bl_ptr->opt_unmap_grain);
9834 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
9835 bl_ptr->unmap_grain_align);
9838 scsi_ulto4b(lun->be_lun->atomicblock,
9839 bl_ptr->max_atomic_transfer_length);
9840 scsi_ulto4b(0, bl_ptr->atomic_alignment);
9841 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
9843 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
9845 ctl_set_success(ctsio);
9846 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9847 ctsio->be_move_done = ctl_config_move_done;
9848 ctl_datamove((union ctl_io *)ctsio);
9849 return (CTL_RETVAL_COMPLETE);
9853 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
9855 struct scsi_vpd_block_device_characteristics *bdc_ptr;
9856 struct ctl_lun *lun;
9860 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9862 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
9863 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
9864 ctsio->kern_sg_entries = 0;
9866 if (sizeof(*bdc_ptr) < alloc_len) {
9867 ctsio->residual = alloc_len - sizeof(*bdc_ptr);
9868 ctsio->kern_data_len = sizeof(*bdc_ptr);
9869 ctsio->kern_total_len = sizeof(*bdc_ptr);
9871 ctsio->residual = 0;
9872 ctsio->kern_data_len = alloc_len;
9873 ctsio->kern_total_len = alloc_len;
9875 ctsio->kern_data_resid = 0;
9876 ctsio->kern_rel_offset = 0;
9877 ctsio->kern_sg_entries = 0;
9880 * The control device is always connected. The disk device, on the
9881 * other hand, may not be online all the time. Need to change this
9882 * to figure out whether the disk device is actually online or not.
9885 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9886 lun->be_lun->lun_type;
9888 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9889 bdc_ptr->page_code = SVPD_BDC;
9890 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
9892 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL)
9893 i = strtol(value, NULL, 0);
9895 i = CTL_DEFAULT_ROTATION_RATE;
9896 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
9898 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL)
9899 i = strtol(value, NULL, 0);
9902 bdc_ptr->wab_wac_ff = (i & 0x0f);
9903 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
9905 ctl_set_success(ctsio);
9906 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9907 ctsio->be_move_done = ctl_config_move_done;
9908 ctl_datamove((union ctl_io *)ctsio);
9909 return (CTL_RETVAL_COMPLETE);
9913 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
9915 struct scsi_vpd_logical_block_prov *lbp_ptr;
9916 struct ctl_lun *lun;
9918 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9920 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
9921 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
9922 ctsio->kern_sg_entries = 0;
9924 if (sizeof(*lbp_ptr) < alloc_len) {
9925 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
9926 ctsio->kern_data_len = sizeof(*lbp_ptr);
9927 ctsio->kern_total_len = sizeof(*lbp_ptr);
9929 ctsio->residual = 0;
9930 ctsio->kern_data_len = alloc_len;
9931 ctsio->kern_total_len = alloc_len;
9933 ctsio->kern_data_resid = 0;
9934 ctsio->kern_rel_offset = 0;
9935 ctsio->kern_sg_entries = 0;
9938 * The control device is always connected. The disk device, on the
9939 * other hand, may not be online all the time. Need to change this
9940 * to figure out whether the disk device is actually online or not.
9943 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9944 lun->be_lun->lun_type;
9946 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9948 lbp_ptr->page_code = SVPD_LBP;
9949 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
9950 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
9951 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9952 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
9953 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
9954 lbp_ptr->prov_type = SVPD_LBP_THIN;
9957 ctl_set_success(ctsio);
9958 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9959 ctsio->be_move_done = ctl_config_move_done;
9960 ctl_datamove((union ctl_io *)ctsio);
9961 return (CTL_RETVAL_COMPLETE);
9965 * INQUIRY with the EVPD bit set.
9968 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
9970 struct ctl_lun *lun;
9971 struct scsi_inquiry *cdb;
9972 int alloc_len, retval;
9974 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9975 cdb = (struct scsi_inquiry *)ctsio->cdb;
9976 alloc_len = scsi_2btoul(cdb->length);
9978 switch (cdb->page_code) {
9979 case SVPD_SUPPORTED_PAGES:
9980 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
9982 case SVPD_UNIT_SERIAL_NUMBER:
9983 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
9985 case SVPD_DEVICE_ID:
9986 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
9988 case SVPD_EXTENDED_INQUIRY_DATA:
9989 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
9991 case SVPD_MODE_PAGE_POLICY:
9992 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
9994 case SVPD_SCSI_PORTS:
9995 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
9998 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10000 case SVPD_BLOCK_LIMITS:
10001 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10003 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10006 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10008 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10011 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10013 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10017 ctl_set_invalid_field(ctsio,
10023 ctl_done((union ctl_io *)ctsio);
10024 retval = CTL_RETVAL_COMPLETE;
10032 * Standard INQUIRY data.
10035 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10037 struct scsi_inquiry_data *inq_ptr;
10038 struct scsi_inquiry *cdb;
10039 struct ctl_softc *softc;
10040 struct ctl_port *port;
10041 struct ctl_lun *lun;
10043 uint32_t alloc_len, data_len;
10044 ctl_port_type port_type;
10046 softc = control_softc;
10049 * Figure out whether we're talking to a Fibre Channel port or not.
10050 * We treat the ioctl front end, and any SCSI adapters, as packetized
10053 port = ctl_io_port(&ctsio->io_hdr);
10055 port_type = port->port_type;
10057 port_type = CTL_PORT_SCSI;
10058 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10059 port_type = CTL_PORT_SCSI;
10061 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10062 cdb = (struct scsi_inquiry *)ctsio->cdb;
10063 alloc_len = scsi_2btoul(cdb->length);
10066 * We malloc the full inquiry data size here and fill it
10067 * in. If the user only asks for less, we'll give him
10070 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10071 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10072 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10073 ctsio->kern_sg_entries = 0;
10074 ctsio->kern_data_resid = 0;
10075 ctsio->kern_rel_offset = 0;
10077 if (data_len < alloc_len) {
10078 ctsio->residual = alloc_len - data_len;
10079 ctsio->kern_data_len = data_len;
10080 ctsio->kern_total_len = data_len;
10082 ctsio->residual = 0;
10083 ctsio->kern_data_len = alloc_len;
10084 ctsio->kern_total_len = alloc_len;
10088 if ((lun->flags & CTL_LUN_PRIMARY_SC) ||
10089 softc->ha_link >= CTL_HA_LINK_UNKNOWN) {
10090 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10091 lun->be_lun->lun_type;
10093 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) |
10094 lun->be_lun->lun_type;
10097 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10099 /* RMB in byte 2 is 0 */
10100 inq_ptr->version = SCSI_REV_SPC4;
10103 * According to SAM-3, even if a device only supports a single
10104 * level of LUN addressing, it should still set the HISUP bit:
10106 * 4.9.1 Logical unit numbers overview
10108 * All logical unit number formats described in this standard are
10109 * hierarchical in structure even when only a single level in that
10110 * hierarchy is used. The HISUP bit shall be set to one in the
10111 * standard INQUIRY data (see SPC-2) when any logical unit number
10112 * format described in this standard is used. Non-hierarchical
10113 * formats are outside the scope of this standard.
10115 * Therefore we set the HiSup bit here.
10117 * The reponse format is 2, per SPC-3.
10119 inq_ptr->response_format = SID_HiSup | 2;
10121 inq_ptr->additional_length = data_len -
10122 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10123 CTL_DEBUG_PRINT(("additional_length = %d\n",
10124 inq_ptr->additional_length));
10126 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10127 /* 16 bit addressing */
10128 if (port_type == CTL_PORT_SCSI)
10129 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10130 /* XXX set the SID_MultiP bit here if we're actually going to
10131 respond on multiple ports */
10132 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10134 /* 16 bit data bus, synchronous transfers */
10135 if (port_type == CTL_PORT_SCSI)
10136 inq_ptr->flags = SID_WBus16 | SID_Sync;
10138 * XXX KDM do we want to support tagged queueing on the control
10142 || (lun->be_lun->lun_type != T_PROCESSOR))
10143 inq_ptr->flags |= SID_CmdQue;
10145 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10146 * We have 8 bytes for the vendor name, and 16 bytes for the device
10147 * name and 4 bytes for the revision.
10149 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10150 "vendor")) == NULL) {
10151 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10153 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10154 strncpy(inq_ptr->vendor, val,
10155 min(sizeof(inq_ptr->vendor), strlen(val)));
10158 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10159 sizeof(inq_ptr->product));
10160 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10161 switch (lun->be_lun->lun_type) {
10163 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10164 sizeof(inq_ptr->product));
10167 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10168 sizeof(inq_ptr->product));
10171 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10172 sizeof(inq_ptr->product));
10176 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10177 strncpy(inq_ptr->product, val,
10178 min(sizeof(inq_ptr->product), strlen(val)));
10182 * XXX make this a macro somewhere so it automatically gets
10183 * incremented when we make changes.
10185 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10186 "revision")) == NULL) {
10187 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10189 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10190 strncpy(inq_ptr->revision, val,
10191 min(sizeof(inq_ptr->revision), strlen(val)));
10195 * For parallel SCSI, we support double transition and single
10196 * transition clocking. We also support QAS (Quick Arbitration
10197 * and Selection) and Information Unit transfers on both the
10198 * control and array devices.
10200 if (port_type == CTL_PORT_SCSI)
10201 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10204 /* SAM-5 (no version claimed) */
10205 scsi_ulto2b(0x00A0, inq_ptr->version1);
10206 /* SPC-4 (no version claimed) */
10207 scsi_ulto2b(0x0460, inq_ptr->version2);
10208 if (port_type == CTL_PORT_FC) {
10209 /* FCP-2 ANSI INCITS.350:2003 */
10210 scsi_ulto2b(0x0917, inq_ptr->version3);
10211 } else if (port_type == CTL_PORT_SCSI) {
10212 /* SPI-4 ANSI INCITS.362:200x */
10213 scsi_ulto2b(0x0B56, inq_ptr->version3);
10214 } else if (port_type == CTL_PORT_ISCSI) {
10215 /* iSCSI (no version claimed) */
10216 scsi_ulto2b(0x0960, inq_ptr->version3);
10217 } else if (port_type == CTL_PORT_SAS) {
10218 /* SAS (no version claimed) */
10219 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10223 /* SBC-4 (no version claimed) */
10224 scsi_ulto2b(0x0600, inq_ptr->version4);
10226 switch (lun->be_lun->lun_type) {
10228 /* SBC-4 (no version claimed) */
10229 scsi_ulto2b(0x0600, inq_ptr->version4);
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 ctl_inquiry(struct ctl_scsiio *ctsio)
10247 struct scsi_inquiry *cdb;
10250 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10252 cdb = (struct scsi_inquiry *)ctsio->cdb;
10253 if (cdb->byte2 & SI_EVPD)
10254 retval = ctl_inquiry_evpd(ctsio);
10255 else if (cdb->page_code == 0)
10256 retval = ctl_inquiry_std(ctsio);
10258 ctl_set_invalid_field(ctsio,
10264 ctl_done((union ctl_io *)ctsio);
10265 return (CTL_RETVAL_COMPLETE);
10272 * For known CDB types, parse the LBA and length.
10275 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10277 if (io->io_hdr.io_type != CTL_IO_SCSI)
10280 switch (io->scsiio.cdb[0]) {
10281 case COMPARE_AND_WRITE: {
10282 struct scsi_compare_and_write *cdb;
10284 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10286 *lba = scsi_8btou64(cdb->addr);
10287 *len = cdb->length;
10292 struct scsi_rw_6 *cdb;
10294 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10296 *lba = scsi_3btoul(cdb->addr);
10297 /* only 5 bits are valid in the most significant address byte */
10299 *len = cdb->length;
10304 struct scsi_rw_10 *cdb;
10306 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10308 *lba = scsi_4btoul(cdb->addr);
10309 *len = scsi_2btoul(cdb->length);
10312 case WRITE_VERIFY_10: {
10313 struct scsi_write_verify_10 *cdb;
10315 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10317 *lba = scsi_4btoul(cdb->addr);
10318 *len = scsi_2btoul(cdb->length);
10323 struct scsi_rw_12 *cdb;
10325 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10327 *lba = scsi_4btoul(cdb->addr);
10328 *len = scsi_4btoul(cdb->length);
10331 case WRITE_VERIFY_12: {
10332 struct scsi_write_verify_12 *cdb;
10334 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10336 *lba = scsi_4btoul(cdb->addr);
10337 *len = scsi_4btoul(cdb->length);
10342 case WRITE_ATOMIC_16: {
10343 struct scsi_rw_16 *cdb;
10345 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10347 *lba = scsi_8btou64(cdb->addr);
10348 *len = scsi_4btoul(cdb->length);
10351 case WRITE_VERIFY_16: {
10352 struct scsi_write_verify_16 *cdb;
10354 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10356 *lba = scsi_8btou64(cdb->addr);
10357 *len = scsi_4btoul(cdb->length);
10360 case WRITE_SAME_10: {
10361 struct scsi_write_same_10 *cdb;
10363 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10365 *lba = scsi_4btoul(cdb->addr);
10366 *len = scsi_2btoul(cdb->length);
10369 case WRITE_SAME_16: {
10370 struct scsi_write_same_16 *cdb;
10372 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10374 *lba = scsi_8btou64(cdb->addr);
10375 *len = scsi_4btoul(cdb->length);
10379 struct scsi_verify_10 *cdb;
10381 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10383 *lba = scsi_4btoul(cdb->addr);
10384 *len = scsi_2btoul(cdb->length);
10388 struct scsi_verify_12 *cdb;
10390 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10392 *lba = scsi_4btoul(cdb->addr);
10393 *len = scsi_4btoul(cdb->length);
10397 struct scsi_verify_16 *cdb;
10399 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10401 *lba = scsi_8btou64(cdb->addr);
10402 *len = scsi_4btoul(cdb->length);
10410 case SERVICE_ACTION_IN: { /* GET LBA STATUS */
10411 struct scsi_get_lba_status *cdb;
10413 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10414 *lba = scsi_8btou64(cdb->addr);
10420 break; /* NOTREACHED */
10427 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10430 uint64_t endlba1, endlba2;
10432 endlba1 = lba1 + len1 - (seq ? 0 : 1);
10433 endlba2 = lba2 + len2 - 1;
10435 if ((endlba1 < lba2) || (endlba2 < lba1))
10436 return (CTL_ACTION_PASS);
10438 return (CTL_ACTION_BLOCK);
10442 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10444 struct ctl_ptr_len_flags *ptrlen;
10445 struct scsi_unmap_desc *buf, *end, *range;
10449 /* If not UNMAP -- go other way. */
10450 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10451 io->scsiio.cdb[0] != UNMAP)
10452 return (CTL_ACTION_ERROR);
10454 /* If UNMAP without data -- block and wait for data. */
10455 ptrlen = (struct ctl_ptr_len_flags *)
10456 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10457 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10458 ptrlen->ptr == NULL)
10459 return (CTL_ACTION_BLOCK);
10461 /* UNMAP with data -- check for collision. */
10462 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10463 end = buf + ptrlen->len / sizeof(*buf);
10464 for (range = buf; range < end; range++) {
10465 lba = scsi_8btou64(range->lba);
10466 len = scsi_4btoul(range->length);
10467 if ((lba < lba2 + len2) && (lba + len > lba2))
10468 return (CTL_ACTION_BLOCK);
10470 return (CTL_ACTION_PASS);
10474 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10476 uint64_t lba1, lba2;
10477 uint64_t len1, len2;
10480 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10481 return (CTL_ACTION_ERROR);
10483 retval = ctl_extent_check_unmap(io1, lba2, len2);
10484 if (retval != CTL_ACTION_ERROR)
10487 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10488 return (CTL_ACTION_ERROR);
10490 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
10494 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2)
10496 uint64_t lba1, lba2;
10497 uint64_t len1, len2;
10499 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10500 return (CTL_ACTION_ERROR);
10501 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10502 return (CTL_ACTION_ERROR);
10504 if (lba1 + len1 == lba2)
10505 return (CTL_ACTION_BLOCK);
10506 return (CTL_ACTION_PASS);
10510 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10511 union ctl_io *ooa_io)
10513 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10514 ctl_serialize_action *serialize_row;
10517 * The initiator attempted multiple untagged commands at the same
10518 * time. Can't do that.
10520 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10521 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10522 && ((pending_io->io_hdr.nexus.targ_port ==
10523 ooa_io->io_hdr.nexus.targ_port)
10524 && (pending_io->io_hdr.nexus.initid ==
10525 ooa_io->io_hdr.nexus.initid))
10526 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10527 CTL_FLAG_STATUS_SENT)) == 0))
10528 return (CTL_ACTION_OVERLAP);
10531 * The initiator attempted to send multiple tagged commands with
10532 * the same ID. (It's fine if different initiators have the same
10535 * Even if all of those conditions are true, we don't kill the I/O
10536 * if the command ahead of us has been aborted. We won't end up
10537 * sending it to the FETD, and it's perfectly legal to resend a
10538 * command with the same tag number as long as the previous
10539 * instance of this tag number has been aborted somehow.
10541 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10542 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10543 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10544 && ((pending_io->io_hdr.nexus.targ_port ==
10545 ooa_io->io_hdr.nexus.targ_port)
10546 && (pending_io->io_hdr.nexus.initid ==
10547 ooa_io->io_hdr.nexus.initid))
10548 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10549 CTL_FLAG_STATUS_SENT)) == 0))
10550 return (CTL_ACTION_OVERLAP_TAG);
10553 * If we get a head of queue tag, SAM-3 says that we should
10554 * immediately execute it.
10556 * What happens if this command would normally block for some other
10557 * reason? e.g. a request sense with a head of queue tag
10558 * immediately after a write. Normally that would block, but this
10559 * will result in its getting executed immediately...
10561 * We currently return "pass" instead of "skip", so we'll end up
10562 * going through the rest of the queue to check for overlapped tags.
10564 * XXX KDM check for other types of blockage first??
10566 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10567 return (CTL_ACTION_PASS);
10570 * Ordered tags have to block until all items ahead of them
10571 * have completed. If we get called with an ordered tag, we always
10572 * block, if something else is ahead of us in the queue.
10574 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10575 return (CTL_ACTION_BLOCK);
10578 * Simple tags get blocked until all head of queue and ordered tags
10579 * ahead of them have completed. I'm lumping untagged commands in
10580 * with simple tags here. XXX KDM is that the right thing to do?
10582 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10583 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10584 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10585 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10586 return (CTL_ACTION_BLOCK);
10588 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
10589 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
10591 serialize_row = ctl_serialize_table[ooa_entry->seridx];
10593 switch (serialize_row[pending_entry->seridx]) {
10594 case CTL_SER_BLOCK:
10595 return (CTL_ACTION_BLOCK);
10596 case CTL_SER_EXTENT:
10597 return (ctl_extent_check(ooa_io, pending_io,
10598 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10599 case CTL_SER_EXTENTOPT:
10600 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
10601 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
10602 return (ctl_extent_check(ooa_io, pending_io,
10604 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10605 return (CTL_ACTION_PASS);
10606 case CTL_SER_EXTENTSEQ:
10607 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF)
10608 return (ctl_extent_check_seq(ooa_io, pending_io));
10609 return (CTL_ACTION_PASS);
10611 return (CTL_ACTION_PASS);
10612 case CTL_SER_BLOCKOPT:
10613 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
10614 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
10615 return (CTL_ACTION_BLOCK);
10616 return (CTL_ACTION_PASS);
10618 return (CTL_ACTION_SKIP);
10620 panic("invalid serialization value %d",
10621 serialize_row[pending_entry->seridx]);
10624 return (CTL_ACTION_ERROR);
10628 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
10630 * - pending_io is generally either incoming, or on the blocked queue
10631 * - starting I/O is the I/O we want to start the check with.
10634 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
10635 union ctl_io *starting_io)
10637 union ctl_io *ooa_io;
10640 mtx_assert(&lun->lun_lock, MA_OWNED);
10643 * Run back along the OOA queue, starting with the current
10644 * blocked I/O and going through every I/O before it on the
10645 * queue. If starting_io is NULL, we'll just end up returning
10648 for (ooa_io = starting_io; ooa_io != NULL;
10649 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
10653 * This routine just checks to see whether
10654 * cur_blocked is blocked by ooa_io, which is ahead
10655 * of it in the queue. It doesn't queue/dequeue
10658 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
10660 case CTL_ACTION_BLOCK:
10661 case CTL_ACTION_OVERLAP:
10662 case CTL_ACTION_OVERLAP_TAG:
10663 case CTL_ACTION_SKIP:
10664 case CTL_ACTION_ERROR:
10666 break; /* NOTREACHED */
10667 case CTL_ACTION_PASS:
10670 panic("invalid action %d", action);
10671 break; /* NOTREACHED */
10675 return (CTL_ACTION_PASS);
10680 * - An I/O has just completed, and has been removed from the per-LUN OOA
10681 * queue, so some items on the blocked queue may now be unblocked.
10684 ctl_check_blocked(struct ctl_lun *lun)
10686 struct ctl_softc *softc = lun->ctl_softc;
10687 union ctl_io *cur_blocked, *next_blocked;
10689 mtx_assert(&lun->lun_lock, MA_OWNED);
10692 * Run forward from the head of the blocked queue, checking each
10693 * entry against the I/Os prior to it on the OOA queue to see if
10694 * there is still any blockage.
10696 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
10697 * with our removing a variable on it while it is traversing the
10700 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
10701 cur_blocked != NULL; cur_blocked = next_blocked) {
10702 union ctl_io *prev_ooa;
10705 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
10708 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
10709 ctl_ooaq, ooa_links);
10712 * If cur_blocked happens to be the first item in the OOA
10713 * queue now, prev_ooa will be NULL, and the action
10714 * returned will just be CTL_ACTION_PASS.
10716 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
10719 case CTL_ACTION_BLOCK:
10720 /* Nothing to do here, still blocked */
10722 case CTL_ACTION_OVERLAP:
10723 case CTL_ACTION_OVERLAP_TAG:
10725 * This shouldn't happen! In theory we've already
10726 * checked this command for overlap...
10729 case CTL_ACTION_PASS:
10730 case CTL_ACTION_SKIP: {
10731 const struct ctl_cmd_entry *entry;
10734 * The skip case shouldn't happen, this transaction
10735 * should have never made it onto the blocked queue.
10738 * This I/O is no longer blocked, we can remove it
10739 * from the blocked queue. Since this is a TAILQ
10740 * (doubly linked list), we can do O(1) removals
10741 * from any place on the list.
10743 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
10745 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
10747 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
10748 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){
10750 * Need to send IO back to original side to
10753 union ctl_ha_msg msg_info;
10755 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
10756 msg_info.hdr.original_sc =
10757 cur_blocked->io_hdr.original_sc;
10758 msg_info.hdr.serializing_sc = cur_blocked;
10759 msg_info.hdr.msg_type = CTL_MSG_R2R;
10760 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
10761 sizeof(msg_info.hdr), M_NOWAIT);
10764 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
10767 * Check this I/O for LUN state changes that may
10768 * have happened while this command was blocked.
10769 * The LUN state may have been changed by a command
10770 * ahead of us in the queue, so we need to re-check
10771 * for any states that can be caused by SCSI
10774 if (ctl_scsiio_lun_check(lun, entry,
10775 &cur_blocked->scsiio) == 0) {
10776 cur_blocked->io_hdr.flags |=
10777 CTL_FLAG_IS_WAS_ON_RTR;
10778 ctl_enqueue_rtr(cur_blocked);
10780 ctl_done(cur_blocked);
10785 * This probably shouldn't happen -- we shouldn't
10786 * get CTL_ACTION_ERROR, or anything else.
10792 return (CTL_RETVAL_COMPLETE);
10796 * This routine (with one exception) checks LUN flags that can be set by
10797 * commands ahead of us in the OOA queue. These flags have to be checked
10798 * when a command initially comes in, and when we pull a command off the
10799 * blocked queue and are preparing to execute it. The reason we have to
10800 * check these flags for commands on the blocked queue is that the LUN
10801 * state may have been changed by a command ahead of us while we're on the
10804 * Ordering is somewhat important with these checks, so please pay
10805 * careful attention to the placement of any new checks.
10808 ctl_scsiio_lun_check(struct ctl_lun *lun,
10809 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
10811 struct ctl_softc *softc = lun->ctl_softc;
10817 mtx_assert(&lun->lun_lock, MA_OWNED);
10820 * If this shelf is a secondary shelf controller, we may have to
10821 * reject some commands disallowed by HA mode and link state.
10823 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
10824 if (softc->ha_link == CTL_HA_LINK_OFFLINE &&
10825 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
10826 ctl_set_lun_unavail(ctsio);
10830 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 &&
10831 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
10832 ctl_set_lun_transit(ctsio);
10836 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY &&
10837 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) {
10838 ctl_set_lun_standby(ctsio);
10843 /* The rest of checks are only done on executing side */
10844 if (softc->ha_mode == CTL_HA_MODE_XFER)
10848 if (entry->pattern & CTL_LUN_PAT_WRITE) {
10850 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) {
10851 ctl_set_hw_write_protected(ctsio);
10855 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT]
10856 .eca_and_aen & SCP_SWP) != 0) {
10857 ctl_set_sense(ctsio, /*current_error*/ 1,
10858 /*sense_key*/ SSD_KEY_DATA_PROTECT,
10859 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
10866 * Check for a reservation conflict. If this command isn't allowed
10867 * even on reserved LUNs, and if this initiator isn't the one who
10868 * reserved us, reject the command with a reservation conflict.
10870 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
10871 if ((lun->flags & CTL_LUN_RESERVED)
10872 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
10873 if (lun->res_idx != residx) {
10874 ctl_set_reservation_conflict(ctsio);
10880 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
10881 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
10882 /* No reservation or command is allowed. */;
10883 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
10884 (lun->res_type == SPR_TYPE_WR_EX ||
10885 lun->res_type == SPR_TYPE_WR_EX_RO ||
10886 lun->res_type == SPR_TYPE_WR_EX_AR)) {
10887 /* The command is allowed for Write Exclusive resv. */;
10890 * if we aren't registered or it's a res holder type
10891 * reservation and this isn't the res holder then set a
10894 if (ctl_get_prkey(lun, residx) == 0
10895 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
10896 ctl_set_reservation_conflict(ctsio);
10902 if ((lun->flags & CTL_LUN_OFFLINE)
10903 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0)) {
10904 ctl_set_lun_not_ready(ctsio);
10909 if ((lun->flags & CTL_LUN_STOPPED)
10910 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
10911 /* "Logical unit not ready, initializing cmd. required" */
10912 ctl_set_lun_stopped(ctsio);
10917 if ((lun->flags & CTL_LUN_INOPERABLE)
10918 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
10919 /* "Medium format corrupted" */
10920 ctl_set_medium_format_corrupted(ctsio);
10930 ctl_failover_io(union ctl_io *io, int have_lock)
10932 ctl_set_busy(&io->scsiio);
10937 ctl_failover_lun(struct ctl_lun *lun)
10939 struct ctl_softc *softc = lun->ctl_softc;
10940 struct ctl_io_hdr *io, *next_io;
10942 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", lun->lun));
10943 if (softc->ha_mode == CTL_HA_MODE_XFER) {
10944 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
10945 /* We are master */
10946 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
10947 if (io->flags & CTL_FLAG_IO_ACTIVE) {
10948 io->flags |= CTL_FLAG_ABORT;
10949 } else { /* This can be only due to DATAMOVE */
10950 io->msg_type = CTL_MSG_DATAMOVE_DONE;
10951 io->flags |= CTL_FLAG_IO_ACTIVE;
10952 io->port_status = 31340;
10953 ctl_enqueue_isc((union ctl_io *)io);
10957 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
10958 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
10959 if (io->flags & CTL_FLAG_IO_ACTIVE) {
10960 io->flags |= CTL_FLAG_FAILOVER;
10962 ctl_set_busy(&((union ctl_io *)io)->
10964 ctl_done((union ctl_io *)io);
10968 } else { /* SERIALIZE modes */
10969 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links,
10971 /* We are master */
10972 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
10973 TAILQ_REMOVE(&lun->blocked_queue, io,
10975 io->flags &= ~CTL_FLAG_BLOCKED;
10976 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
10977 ctl_free_io((union ctl_io *)io);
10980 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
10981 /* We are master */
10982 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
10983 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
10984 ctl_free_io((union ctl_io *)io);
10987 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
10988 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
10989 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) {
10990 ctl_set_busy(&((union ctl_io *)io)->
10992 ctl_done((union ctl_io *)io);
10996 ctl_check_blocked(lun);
11001 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio)
11003 struct ctl_lun *lun;
11004 const struct ctl_cmd_entry *entry;
11005 uint32_t initidx, targ_lun;
11012 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11013 if ((targ_lun < CTL_MAX_LUNS)
11014 && ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
11016 * If the LUN is invalid, pretend that it doesn't exist.
11017 * It will go away as soon as all pending I/O has been
11020 mtx_lock(&lun->lun_lock);
11021 if (lun->flags & CTL_LUN_DISABLED) {
11022 mtx_unlock(&lun->lun_lock);
11024 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11025 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11027 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
11028 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
11032 * Every I/O goes into the OOA queue for a
11033 * particular LUN, and stays there until completion.
11036 if (TAILQ_EMPTY(&lun->ooa_queue)) {
11037 lun->idle_time += getsbinuptime() -
11041 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr,
11045 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11046 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11049 /* Get command entry and return error if it is unsuppotyed. */
11050 entry = ctl_validate_command(ctsio);
11051 if (entry == NULL) {
11053 mtx_unlock(&lun->lun_lock);
11057 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11058 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11061 * Check to see whether we can send this command to LUNs that don't
11062 * exist. This should pretty much only be the case for inquiry
11063 * and request sense. Further checks, below, really require having
11064 * a LUN, so we can't really check the command anymore. Just put
11065 * it on the rtr queue.
11068 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) {
11069 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11070 ctl_enqueue_rtr((union ctl_io *)ctsio);
11074 ctl_set_unsupported_lun(ctsio);
11075 ctl_done((union ctl_io *)ctsio);
11076 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11080 * Make sure we support this particular command on this LUN.
11081 * e.g., we don't support writes to the control LUN.
11083 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11084 mtx_unlock(&lun->lun_lock);
11085 ctl_set_invalid_opcode(ctsio);
11086 ctl_done((union ctl_io *)ctsio);
11091 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11095 * If we've got a request sense, it'll clear the contingent
11096 * allegiance condition. Otherwise, if we have a CA condition for
11097 * this initiator, clear it, because it sent down a command other
11098 * than request sense.
11100 if ((ctsio->cdb[0] != REQUEST_SENSE)
11101 && (ctl_is_set(lun->have_ca, initidx)))
11102 ctl_clear_mask(lun->have_ca, initidx);
11106 * If the command has this flag set, it handles its own unit
11107 * attention reporting, we shouldn't do anything. Otherwise we
11108 * check for any pending unit attentions, and send them back to the
11109 * initiator. We only do this when a command initially comes in,
11110 * not when we pull it off the blocked queue.
11112 * According to SAM-3, section 5.3.2, the order that things get
11113 * presented back to the host is basically unit attentions caused
11114 * by some sort of reset event, busy status, reservation conflicts
11115 * or task set full, and finally any other status.
11117 * One issue here is that some of the unit attentions we report
11118 * don't fall into the "reset" category (e.g. "reported luns data
11119 * has changed"). So reporting it here, before the reservation
11120 * check, may be technically wrong. I guess the only thing to do
11121 * would be to check for and report the reset events here, and then
11122 * check for the other unit attention types after we check for a
11123 * reservation conflict.
11125 * XXX KDM need to fix this
11127 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11128 ctl_ua_type ua_type;
11129 scsi_sense_data_type sense_format;
11131 if (lun->flags & CTL_LUN_SENSE_DESC)
11132 sense_format = SSD_TYPE_DESC;
11134 sense_format = SSD_TYPE_FIXED;
11136 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11138 if (ua_type != CTL_UA_NONE) {
11139 mtx_unlock(&lun->lun_lock);
11140 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11141 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11142 ctsio->sense_len = SSD_FULL_SIZE;
11143 ctl_done((union ctl_io *)ctsio);
11149 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11150 mtx_unlock(&lun->lun_lock);
11151 ctl_done((union ctl_io *)ctsio);
11156 * XXX CHD this is where we want to send IO to other side if
11157 * this LUN is secondary on this SC. We will need to make a copy
11158 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11159 * the copy we send as FROM_OTHER.
11160 * We also need to stuff the address of the original IO so we can
11161 * find it easily. Something similar will need be done on the other
11162 * side so when we are done we can find the copy.
11164 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
11165 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0) {
11166 union ctl_ha_msg msg_info;
11169 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11170 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11171 mtx_unlock(&lun->lun_lock);
11173 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11174 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11175 msg_info.hdr.serializing_sc = NULL;
11176 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11177 msg_info.scsi.tag_num = ctsio->tag_num;
11178 msg_info.scsi.tag_type = ctsio->tag_type;
11179 msg_info.scsi.cdb_len = ctsio->cdb_len;
11180 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11182 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11183 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data),
11184 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) {
11185 ctl_set_busy(ctsio);
11186 ctl_done((union ctl_io *)ctsio);
11192 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11193 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11194 ctl_ooaq, ooa_links))) {
11195 case CTL_ACTION_BLOCK:
11196 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11197 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11199 mtx_unlock(&lun->lun_lock);
11201 case CTL_ACTION_PASS:
11202 case CTL_ACTION_SKIP:
11203 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11204 mtx_unlock(&lun->lun_lock);
11205 ctl_enqueue_rtr((union ctl_io *)ctsio);
11207 case CTL_ACTION_OVERLAP:
11208 mtx_unlock(&lun->lun_lock);
11209 ctl_set_overlapped_cmd(ctsio);
11210 ctl_done((union ctl_io *)ctsio);
11212 case CTL_ACTION_OVERLAP_TAG:
11213 mtx_unlock(&lun->lun_lock);
11214 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11215 ctl_done((union ctl_io *)ctsio);
11217 case CTL_ACTION_ERROR:
11219 mtx_unlock(&lun->lun_lock);
11220 ctl_set_internal_failure(ctsio,
11222 /*retry_count*/ 0);
11223 ctl_done((union ctl_io *)ctsio);
11229 const struct ctl_cmd_entry *
11230 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11232 const struct ctl_cmd_entry *entry;
11233 int service_action;
11235 entry = &ctl_cmd_table[ctsio->cdb[0]];
11237 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11238 if (entry->flags & CTL_CMD_FLAG_SA5) {
11239 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11240 entry = &((const struct ctl_cmd_entry *)
11241 entry->execute)[service_action];
11246 const struct ctl_cmd_entry *
11247 ctl_validate_command(struct ctl_scsiio *ctsio)
11249 const struct ctl_cmd_entry *entry;
11253 entry = ctl_get_cmd_entry(ctsio, &sa);
11254 if (entry->execute == NULL) {
11256 ctl_set_invalid_field(ctsio,
11263 ctl_set_invalid_opcode(ctsio);
11264 ctl_done((union ctl_io *)ctsio);
11267 KASSERT(entry->length > 0,
11268 ("Not defined length for command 0x%02x/0x%02x",
11269 ctsio->cdb[0], ctsio->cdb[1]));
11270 for (i = 1; i < entry->length; i++) {
11271 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11274 ctl_set_invalid_field(ctsio,
11279 /*bit*/ fls(diff) - 1);
11280 ctl_done((union ctl_io *)ctsio);
11287 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11290 switch (lun_type) {
11292 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) &&
11293 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11297 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) &&
11298 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11308 ctl_scsiio(struct ctl_scsiio *ctsio)
11311 const struct ctl_cmd_entry *entry;
11313 retval = CTL_RETVAL_COMPLETE;
11315 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11317 entry = ctl_get_cmd_entry(ctsio, NULL);
11320 * If this I/O has been aborted, just send it straight to
11321 * ctl_done() without executing it.
11323 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11324 ctl_done((union ctl_io *)ctsio);
11329 * All the checks should have been handled by ctl_scsiio_precheck().
11330 * We should be clear now to just execute the I/O.
11332 retval = entry->execute(ctsio);
11339 * Since we only implement one target right now, a bus reset simply resets
11340 * our single target.
11343 ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io)
11345 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET));
11349 ctl_target_reset(struct ctl_softc *softc, union ctl_io *io,
11350 ctl_ua_type ua_type)
11352 struct ctl_port *port;
11353 struct ctl_lun *lun;
11356 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11357 union ctl_ha_msg msg_info;
11359 msg_info.hdr.nexus = io->io_hdr.nexus;
11360 if (ua_type==CTL_UA_TARG_RESET)
11361 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11363 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11364 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11365 msg_info.hdr.original_sc = NULL;
11366 msg_info.hdr.serializing_sc = NULL;
11367 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11368 sizeof(msg_info.task), M_WAITOK);
11372 mtx_lock(&softc->ctl_lock);
11373 port = softc->ctl_ports[io->io_hdr.nexus.targ_port];
11374 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11375 if (port != NULL &&
11376 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
11378 retval += ctl_lun_reset(lun, io, ua_type);
11380 mtx_unlock(&softc->ctl_lock);
11386 * The LUN should always be set. The I/O is optional, and is used to
11387 * distinguish between I/Os sent by this initiator, and by other
11388 * initiators. We set unit attention for initiators other than this one.
11389 * SAM-3 is vague on this point. It does say that a unit attention should
11390 * be established for other initiators when a LUN is reset (see section
11391 * 5.7.3), but it doesn't specifically say that the unit attention should
11392 * be established for this particular initiator when a LUN is reset. Here
11393 * is the relevant text, from SAM-3 rev 8:
11395 * 5.7.2 When a SCSI initiator port aborts its own tasks
11397 * When a SCSI initiator port causes its own task(s) to be aborted, no
11398 * notification that the task(s) have been aborted shall be returned to
11399 * the SCSI initiator port other than the completion response for the
11400 * command or task management function action that caused the task(s) to
11401 * be aborted and notification(s) associated with related effects of the
11402 * action (e.g., a reset unit attention condition).
11404 * XXX KDM for now, we're setting unit attention for all initiators.
11407 ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11417 mtx_lock(&lun->lun_lock);
11419 * Run through the OOA queue and abort each I/O.
11421 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11422 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11423 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11427 * This version sets unit attention for every
11430 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11431 ctl_est_ua_all(lun, initidx, ua_type);
11433 ctl_est_ua_all(lun, -1, ua_type);
11437 * A reset (any kind, really) clears reservations established with
11438 * RESERVE/RELEASE. It does not clear reservations established
11439 * with PERSISTENT RESERVE OUT, but we don't support that at the
11440 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
11441 * reservations made with the RESERVE/RELEASE commands, because
11442 * those commands are obsolete in SPC-3.
11444 lun->flags &= ~CTL_LUN_RESERVED;
11447 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11448 ctl_clear_mask(lun->have_ca, i);
11450 mtx_unlock(&lun->lun_lock);
11456 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11461 mtx_assert(&lun->lun_lock, MA_OWNED);
11464 * Run through the OOA queue and attempt to find the given I/O.
11465 * The target port, initiator ID, tag type and tag number have to
11466 * match the values that we got from the initiator. If we have an
11467 * untagged command to abort, simply abort the first untagged command
11468 * we come to. We only allow one untagged command at a time of course.
11470 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11471 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11473 if ((targ_port == UINT32_MAX ||
11474 targ_port == xio->io_hdr.nexus.targ_port) &&
11475 (init_id == UINT32_MAX ||
11476 init_id == xio->io_hdr.nexus.initid)) {
11477 if (targ_port != xio->io_hdr.nexus.targ_port ||
11478 init_id != xio->io_hdr.nexus.initid)
11479 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
11480 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11481 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11482 union ctl_ha_msg msg_info;
11484 msg_info.hdr.nexus = xio->io_hdr.nexus;
11485 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11486 msg_info.task.tag_num = xio->scsiio.tag_num;
11487 msg_info.task.tag_type = xio->scsiio.tag_type;
11488 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11489 msg_info.hdr.original_sc = NULL;
11490 msg_info.hdr.serializing_sc = NULL;
11491 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11492 sizeof(msg_info.task), M_NOWAIT);
11499 ctl_abort_task_set(union ctl_io *io)
11501 struct ctl_softc *softc = control_softc;
11502 struct ctl_lun *lun;
11508 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11509 mtx_lock(&softc->ctl_lock);
11510 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL))
11511 lun = softc->ctl_luns[targ_lun];
11513 mtx_unlock(&softc->ctl_lock);
11517 mtx_lock(&lun->lun_lock);
11518 mtx_unlock(&softc->ctl_lock);
11519 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
11520 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11521 io->io_hdr.nexus.initid,
11522 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11523 } else { /* CTL_TASK_CLEAR_TASK_SET */
11524 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
11525 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11527 mtx_unlock(&lun->lun_lock);
11532 ctl_i_t_nexus_reset(union ctl_io *io)
11534 struct ctl_softc *softc = control_softc;
11535 struct ctl_lun *lun;
11538 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11539 union ctl_ha_msg msg_info;
11541 msg_info.hdr.nexus = io->io_hdr.nexus;
11542 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET;
11543 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11544 msg_info.hdr.original_sc = NULL;
11545 msg_info.hdr.serializing_sc = NULL;
11546 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11547 sizeof(msg_info.task), M_WAITOK);
11550 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11551 mtx_lock(&softc->ctl_lock);
11552 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11553 mtx_lock(&lun->lun_lock);
11554 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11555 io->io_hdr.nexus.initid, 1);
11557 ctl_clear_mask(lun->have_ca, initidx);
11559 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx))
11560 lun->flags &= ~CTL_LUN_RESERVED;
11561 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS);
11562 mtx_unlock(&lun->lun_lock);
11564 mtx_unlock(&softc->ctl_lock);
11569 ctl_abort_task(union ctl_io *io)
11572 struct ctl_lun *lun;
11573 struct ctl_softc *softc;
11576 char printbuf[128];
11581 softc = control_softc;
11587 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11588 mtx_lock(&softc->ctl_lock);
11589 if ((targ_lun < CTL_MAX_LUNS)
11590 && (softc->ctl_luns[targ_lun] != NULL))
11591 lun = softc->ctl_luns[targ_lun];
11593 mtx_unlock(&softc->ctl_lock);
11598 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
11599 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
11602 mtx_lock(&lun->lun_lock);
11603 mtx_unlock(&softc->ctl_lock);
11605 * Run through the OOA queue and attempt to find the given I/O.
11606 * The target port, initiator ID, tag type and tag number have to
11607 * match the values that we got from the initiator. If we have an
11608 * untagged command to abort, simply abort the first untagged command
11609 * we come to. We only allow one untagged command at a time of course.
11611 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11612 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11614 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
11616 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
11617 lun->lun, xio->scsiio.tag_num,
11618 xio->scsiio.tag_type,
11619 (xio->io_hdr.blocked_links.tqe_prev
11620 == NULL) ? "" : " BLOCKED",
11621 (xio->io_hdr.flags &
11622 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
11623 (xio->io_hdr.flags &
11624 CTL_FLAG_ABORT) ? " ABORT" : "",
11625 (xio->io_hdr.flags &
11626 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
11627 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
11629 printf("%s\n", sbuf_data(&sb));
11632 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
11633 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
11634 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
11638 * If the abort says that the task is untagged, the
11639 * task in the queue must be untagged. Otherwise,
11640 * we just check to see whether the tag numbers
11641 * match. This is because the QLogic firmware
11642 * doesn't pass back the tag type in an abort
11646 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
11647 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
11648 || (xio->scsiio.tag_num == io->taskio.tag_num))
11651 * XXX KDM we've got problems with FC, because it
11652 * doesn't send down a tag type with aborts. So we
11653 * can only really go by the tag number...
11654 * This may cause problems with parallel SCSI.
11655 * Need to figure that out!!
11657 if (xio->scsiio.tag_num == io->taskio.tag_num) {
11658 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11660 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
11661 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11662 union ctl_ha_msg msg_info;
11664 msg_info.hdr.nexus = io->io_hdr.nexus;
11665 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11666 msg_info.task.tag_num = io->taskio.tag_num;
11667 msg_info.task.tag_type = io->taskio.tag_type;
11668 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11669 msg_info.hdr.original_sc = NULL;
11670 msg_info.hdr.serializing_sc = NULL;
11672 printf("Sent Abort to other side\n");
11674 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11675 sizeof(msg_info.task), M_NOWAIT);
11678 printf("ctl_abort_task: found I/O to abort\n");
11682 mtx_unlock(&lun->lun_lock);
11686 * This isn't really an error. It's entirely possible for
11687 * the abort and command completion to cross on the wire.
11688 * This is more of an informative/diagnostic error.
11691 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
11692 "%u:%u:%u tag %d type %d\n",
11693 io->io_hdr.nexus.initid,
11694 io->io_hdr.nexus.targ_port,
11695 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
11696 io->taskio.tag_type);
11703 ctl_run_task(union ctl_io *io)
11705 struct ctl_softc *softc = control_softc;
11707 const char *task_desc;
11709 CTL_DEBUG_PRINT(("ctl_run_task\n"));
11711 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
11712 ("ctl_run_task: Unextected io_type %d\n",
11713 io->io_hdr.io_type));
11715 task_desc = ctl_scsi_task_string(&io->taskio);
11716 if (task_desc != NULL) {
11718 csevent_log(CSC_CTL | CSC_SHELF_SW |
11720 csevent_LogType_Trace,
11721 csevent_Severity_Information,
11722 csevent_AlertLevel_Green,
11723 csevent_FRU_Firmware,
11724 csevent_FRU_Unknown,
11725 "CTL: received task: %s",task_desc);
11729 csevent_log(CSC_CTL | CSC_SHELF_SW |
11731 csevent_LogType_Trace,
11732 csevent_Severity_Information,
11733 csevent_AlertLevel_Green,
11734 csevent_FRU_Firmware,
11735 csevent_FRU_Unknown,
11736 "CTL: received unknown task "
11738 io->taskio.task_action,
11739 io->taskio.task_action);
11742 switch (io->taskio.task_action) {
11743 case CTL_TASK_ABORT_TASK:
11744 retval = ctl_abort_task(io);
11746 case CTL_TASK_ABORT_TASK_SET:
11747 case CTL_TASK_CLEAR_TASK_SET:
11748 retval = ctl_abort_task_set(io);
11750 case CTL_TASK_CLEAR_ACA:
11752 case CTL_TASK_I_T_NEXUS_RESET:
11753 retval = ctl_i_t_nexus_reset(io);
11755 case CTL_TASK_LUN_RESET: {
11756 struct ctl_lun *lun;
11759 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11760 mtx_lock(&softc->ctl_lock);
11761 if ((targ_lun < CTL_MAX_LUNS)
11762 && (softc->ctl_luns[targ_lun] != NULL))
11763 lun = softc->ctl_luns[targ_lun];
11765 mtx_unlock(&softc->ctl_lock);
11769 retval = ctl_lun_reset(lun, io, CTL_UA_LUN_RESET);
11770 mtx_unlock(&softc->ctl_lock);
11772 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) {
11773 union ctl_ha_msg msg_info;
11775 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11776 msg_info.hdr.nexus = io->io_hdr.nexus;
11777 msg_info.task.task_action = CTL_TASK_LUN_RESET;
11778 msg_info.hdr.original_sc = NULL;
11779 msg_info.hdr.serializing_sc = NULL;
11780 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11781 sizeof(msg_info.task), M_WAITOK);
11785 case CTL_TASK_TARGET_RESET:
11786 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET);
11788 case CTL_TASK_BUS_RESET:
11789 retval = ctl_bus_reset(softc, io);
11791 case CTL_TASK_PORT_LOGIN:
11793 case CTL_TASK_PORT_LOGOUT:
11796 printf("ctl_run_task: got unknown task management event %d\n",
11797 io->taskio.task_action);
11801 io->io_hdr.status = CTL_SUCCESS;
11803 io->io_hdr.status = CTL_ERROR;
11808 * For HA operation. Handle commands that come in from the other
11812 ctl_handle_isc(union ctl_io *io)
11815 struct ctl_lun *lun;
11816 struct ctl_softc *softc;
11819 softc = control_softc;
11821 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11822 lun = softc->ctl_luns[targ_lun];
11824 switch (io->io_hdr.msg_type) {
11825 case CTL_MSG_SERIALIZE:
11826 free_io = ctl_serialize_other_sc_cmd(&io->scsiio);
11828 case CTL_MSG_R2R: {
11829 const struct ctl_cmd_entry *entry;
11832 * This is only used in SER_ONLY mode.
11835 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
11836 mtx_lock(&lun->lun_lock);
11837 if (ctl_scsiio_lun_check(lun,
11838 entry, (struct ctl_scsiio *)io) != 0) {
11839 mtx_unlock(&lun->lun_lock);
11843 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11844 mtx_unlock(&lun->lun_lock);
11845 ctl_enqueue_rtr(io);
11848 case CTL_MSG_FINISH_IO:
11849 if (softc->ha_mode == CTL_HA_MODE_XFER) {
11854 mtx_lock(&lun->lun_lock);
11855 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
11857 ctl_check_blocked(lun);
11858 mtx_unlock(&lun->lun_lock);
11861 case CTL_MSG_PERS_ACTION:
11862 ctl_hndl_per_res_out_on_other_sc(
11863 (union ctl_ha_msg *)&io->presio.pr_msg);
11866 case CTL_MSG_BAD_JUJU:
11870 case CTL_MSG_DATAMOVE:
11871 /* Only used in XFER mode */
11873 ctl_datamove_remote(io);
11875 case CTL_MSG_DATAMOVE_DONE:
11876 /* Only used in XFER mode */
11878 io->scsiio.be_move_done(io);
11880 case CTL_MSG_FAILOVER:
11881 mtx_lock(&lun->lun_lock);
11882 ctl_failover_lun(lun);
11883 mtx_unlock(&lun->lun_lock);
11888 printf("%s: Invalid message type %d\n",
11889 __func__, io->io_hdr.msg_type);
11899 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
11900 * there is no match.
11902 static ctl_lun_error_pattern
11903 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
11905 const struct ctl_cmd_entry *entry;
11906 ctl_lun_error_pattern filtered_pattern, pattern;
11908 pattern = desc->error_pattern;
11911 * XXX KDM we need more data passed into this function to match a
11912 * custom pattern, and we actually need to implement custom pattern
11915 if (pattern & CTL_LUN_PAT_CMD)
11916 return (CTL_LUN_PAT_CMD);
11918 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
11919 return (CTL_LUN_PAT_ANY);
11921 entry = ctl_get_cmd_entry(ctsio, NULL);
11923 filtered_pattern = entry->pattern & pattern;
11926 * If the user requested specific flags in the pattern (e.g.
11927 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
11930 * If the user did not specify any flags, it doesn't matter whether
11931 * or not the command supports the flags.
11933 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
11934 (pattern & ~CTL_LUN_PAT_MASK))
11935 return (CTL_LUN_PAT_NONE);
11938 * If the user asked for a range check, see if the requested LBA
11939 * range overlaps with this command's LBA range.
11941 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
11947 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
11949 return (CTL_LUN_PAT_NONE);
11951 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
11952 desc->lba_range.len, FALSE);
11954 * A "pass" means that the LBA ranges don't overlap, so
11955 * this doesn't match the user's range criteria.
11957 if (action == CTL_ACTION_PASS)
11958 return (CTL_LUN_PAT_NONE);
11961 return (filtered_pattern);
11965 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
11967 struct ctl_error_desc *desc, *desc2;
11969 mtx_assert(&lun->lun_lock, MA_OWNED);
11971 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
11972 ctl_lun_error_pattern pattern;
11974 * Check to see whether this particular command matches
11975 * the pattern in the descriptor.
11977 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
11978 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
11981 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
11982 case CTL_LUN_INJ_ABORTED:
11983 ctl_set_aborted(&io->scsiio);
11985 case CTL_LUN_INJ_MEDIUM_ERR:
11986 ctl_set_medium_error(&io->scsiio);
11988 case CTL_LUN_INJ_UA:
11989 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
11991 ctl_set_ua(&io->scsiio, 0x29, 0x00);
11993 case CTL_LUN_INJ_CUSTOM:
11995 * We're assuming the user knows what he is doing.
11996 * Just copy the sense information without doing
11999 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12000 MIN(sizeof(desc->custom_sense),
12001 sizeof(io->scsiio.sense_data)));
12002 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12003 io->scsiio.sense_len = SSD_FULL_SIZE;
12004 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12006 case CTL_LUN_INJ_NONE:
12009 * If this is an error injection type we don't know
12010 * about, clear the continuous flag (if it is set)
12011 * so it will get deleted below.
12013 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12017 * By default, each error injection action is a one-shot
12019 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12022 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12028 #ifdef CTL_IO_DELAY
12030 ctl_datamove_timer_wakeup(void *arg)
12034 io = (union ctl_io *)arg;
12038 #endif /* CTL_IO_DELAY */
12041 ctl_datamove(union ctl_io *io)
12043 void (*fe_datamove)(union ctl_io *io);
12045 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12047 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12050 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12055 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12056 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12058 sbuf_cat(&sb, path_str);
12059 switch (io->io_hdr.io_type) {
12061 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12062 sbuf_printf(&sb, "\n");
12063 sbuf_cat(&sb, path_str);
12064 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12065 io->scsiio.tag_num, io->scsiio.tag_type);
12068 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12069 "Tag Type: %d\n", io->taskio.task_action,
12070 io->taskio.tag_num, io->taskio.tag_type);
12073 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12074 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12077 sbuf_cat(&sb, path_str);
12078 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12079 (intmax_t)time_uptime - io->io_hdr.start_time);
12081 printf("%s", sbuf_data(&sb));
12083 #endif /* CTL_TIME_IO */
12085 #ifdef CTL_IO_DELAY
12086 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12087 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12089 struct ctl_lun *lun;
12091 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12093 && (lun->delay_info.datamove_delay > 0)) {
12095 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
12096 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12097 callout_reset(&io->io_hdr.delay_callout,
12098 lun->delay_info.datamove_delay * hz,
12099 ctl_datamove_timer_wakeup, io);
12100 if (lun->delay_info.datamove_type ==
12101 CTL_DELAY_TYPE_ONESHOT)
12102 lun->delay_info.datamove_delay = 0;
12109 * This command has been aborted. Set the port status, so we fail
12112 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12113 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n",
12114 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12115 io->io_hdr.nexus.targ_port,
12116 io->io_hdr.nexus.targ_lun);
12117 io->io_hdr.port_status = 31337;
12119 * Note that the backend, in this case, will get the
12120 * callback in its context. In other cases it may get
12121 * called in the frontend's interrupt thread context.
12123 io->scsiio.be_move_done(io);
12127 /* Don't confuse frontend with zero length data move. */
12128 if (io->scsiio.kern_data_len == 0) {
12129 io->scsiio.be_move_done(io);
12134 * If we're in XFER mode and this I/O is from the other shelf
12135 * controller, we need to send the DMA to the other side to
12136 * actually transfer the data to/from the host. In serialize only
12137 * mode the transfer happens below CTL and ctl_datamove() is only
12138 * called on the machine that originally received the I/O.
12140 if ((control_softc->ha_mode == CTL_HA_MODE_XFER)
12141 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12142 union ctl_ha_msg msg;
12143 uint32_t sg_entries_sent;
12147 memset(&msg, 0, sizeof(msg));
12148 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
12149 msg.hdr.original_sc = io->io_hdr.original_sc;
12150 msg.hdr.serializing_sc = io;
12151 msg.hdr.nexus = io->io_hdr.nexus;
12152 msg.dt.flags = io->io_hdr.flags;
12154 * We convert everything into a S/G list here. We can't
12155 * pass by reference, only by value between controllers.
12156 * So we can't pass a pointer to the S/G list, only as many
12157 * S/G entries as we can fit in here. If it's possible for
12158 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
12159 * then we need to break this up into multiple transfers.
12161 if (io->scsiio.kern_sg_entries == 0) {
12162 msg.dt.kern_sg_entries = 1;
12165 * Convert to a physical address if this is a
12168 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
12169 msg.dt.sg_list[0].addr =
12170 io->scsiio.kern_data_ptr;
12173 * XXX KDM use busdma here!
12175 msg.dt.sg_list[0].addr = (void *)
12176 vtophys(io->scsiio.kern_data_ptr);
12179 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
12180 ("HA does not support BUS_ADDR"));
12181 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
12184 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
12187 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
12191 msg.dt.kern_data_len = io->scsiio.kern_data_len;
12192 msg.dt.kern_total_len = io->scsiio.kern_total_len;
12193 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
12194 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
12195 msg.dt.sg_sequence = 0;
12198 * Loop until we've sent all of the S/G entries. On the
12199 * other end, we'll recompose these S/G entries into one
12200 * contiguous list before passing it to the
12202 for (sg_entries_sent = 0; sg_entries_sent <
12203 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) {
12204 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list)/
12205 sizeof(msg.dt.sg_list[0])),
12206 msg.dt.kern_sg_entries - sg_entries_sent);
12208 if (do_sg_copy != 0) {
12209 struct ctl_sg_entry *sgl;
12212 sgl = (struct ctl_sg_entry *)
12213 io->scsiio.kern_data_ptr;
12215 * If this is in cached memory, flush the cache
12216 * before we send the DMA request to the other
12217 * controller. We want to do this in either
12218 * the * read or the write case. The read
12219 * case is straightforward. In the write
12220 * case, we want to make sure nothing is
12221 * in the local cache that could overwrite
12225 for (i = sg_entries_sent, j = 0;
12226 i < msg.dt.cur_sg_entries; i++, j++) {
12228 if ((io->io_hdr.flags &
12229 CTL_FLAG_BUS_ADDR) == 0) {
12231 * XXX KDM use busdma.
12233 msg.dt.sg_list[j].addr =(void *)
12234 vtophys(sgl[i].addr);
12236 msg.dt.sg_list[j].addr =
12240 KASSERT((io->io_hdr.flags &
12241 CTL_FLAG_BUS_ADDR) == 0,
12242 ("HA does not support BUS_ADDR"));
12243 msg.dt.sg_list[j].addr = sgl[i].addr;
12245 msg.dt.sg_list[j].len = sgl[i].len;
12249 sg_entries_sent += msg.dt.cur_sg_entries;
12250 if (sg_entries_sent >= msg.dt.kern_sg_entries)
12251 msg.dt.sg_last = 1;
12253 msg.dt.sg_last = 0;
12255 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12256 sizeof(msg.dt) - sizeof(msg.dt.sg_list) +
12257 sizeof(struct ctl_sg_entry)*msg.dt.cur_sg_entries,
12258 M_WAITOK) > CTL_HA_STATUS_SUCCESS) {
12259 io->io_hdr.port_status = 31341;
12260 io->scsiio.be_move_done(io);
12264 msg.dt.sent_sg_entries = sg_entries_sent;
12266 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12270 * Lookup the fe_datamove() function for this particular
12273 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12280 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12282 union ctl_ha_msg msg;
12284 memset(&msg, 0, sizeof(msg));
12286 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12287 msg.hdr.original_sc = io;
12288 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12289 msg.hdr.nexus = io->io_hdr.nexus;
12290 msg.hdr.status = io->io_hdr.status;
12291 msg.scsi.tag_num = io->scsiio.tag_num;
12292 msg.scsi.tag_type = io->scsiio.tag_type;
12293 msg.scsi.scsi_status = io->scsiio.scsi_status;
12294 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12295 io->scsiio.sense_len);
12296 msg.scsi.sense_len = io->scsiio.sense_len;
12297 msg.scsi.sense_residual = io->scsiio.sense_residual;
12298 msg.scsi.fetd_status = io->io_hdr.port_status;
12299 msg.scsi.residual = io->scsiio.residual;
12300 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12302 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12303 ctl_failover_io(io, /*have_lock*/ have_lock);
12307 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12308 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12309 msg.scsi.sense_len, M_WAITOK);
12313 * The DMA to the remote side is done, now we need to tell the other side
12314 * we're done so it can continue with its data movement.
12317 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12324 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12325 printf("%s: ISC DMA write failed with error %d", __func__,
12327 ctl_set_internal_failure(&io->scsiio,
12329 /*retry_count*/ rq->ret);
12332 ctl_dt_req_free(rq);
12334 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12335 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12336 free(io->io_hdr.remote_sglist, M_CTL);
12337 io->io_hdr.remote_sglist = NULL;
12338 io->io_hdr.local_sglist = NULL;
12341 * The data is in local and remote memory, so now we need to send
12342 * status (good or back) back to the other side.
12344 ctl_send_datamove_done(io, /*have_lock*/ 0);
12348 * We've moved the data from the host/controller into local memory. Now we
12349 * need to push it over to the remote controller's memory.
12352 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12358 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12359 ctl_datamove_remote_write_cb);
12365 ctl_datamove_remote_write(union ctl_io *io)
12368 void (*fe_datamove)(union ctl_io *io);
12371 * - Get the data from the host/HBA into local memory.
12372 * - DMA memory from the local controller to the remote controller.
12373 * - Send status back to the remote controller.
12376 retval = ctl_datamove_remote_sgl_setup(io);
12380 /* Switch the pointer over so the FETD knows what to do */
12381 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12384 * Use a custom move done callback, since we need to send completion
12385 * back to the other controller, not to the backend on this side.
12387 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12389 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12398 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12407 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12408 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12409 free(io->io_hdr.remote_sglist, M_CTL);
12410 io->io_hdr.remote_sglist = NULL;
12411 io->io_hdr.local_sglist = NULL;
12414 scsi_path_string(io, path_str, sizeof(path_str));
12415 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12416 sbuf_cat(&sb, path_str);
12417 scsi_command_string(&io->scsiio, NULL, &sb);
12418 sbuf_printf(&sb, "\n");
12419 sbuf_cat(&sb, path_str);
12420 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12421 io->scsiio.tag_num, io->scsiio.tag_type);
12422 sbuf_cat(&sb, path_str);
12423 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12424 io->io_hdr.flags, io->io_hdr.status);
12426 printk("%s", sbuf_data(&sb));
12431 * The read is done, now we need to send status (good or bad) back
12432 * to the other side.
12434 ctl_send_datamove_done(io, /*have_lock*/ 0);
12440 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12443 void (*fe_datamove)(union ctl_io *io);
12447 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12448 printf("%s: ISC DMA read failed with error %d\n", __func__,
12450 ctl_set_internal_failure(&io->scsiio,
12452 /*retry_count*/ rq->ret);
12455 ctl_dt_req_free(rq);
12457 /* Switch the pointer over so the FETD knows what to do */
12458 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12461 * Use a custom move done callback, since we need to send completion
12462 * back to the other controller, not to the backend on this side.
12464 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12466 /* XXX KDM add checks like the ones in ctl_datamove? */
12468 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12474 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12476 struct ctl_sg_entry *local_sglist, *remote_sglist;
12477 struct ctl_softc *softc;
12478 uint32_t len_to_go;
12483 softc = control_softc;
12484 local_sglist = io->io_hdr.local_sglist;
12485 remote_sglist = io->io_hdr.remote_sglist;
12486 len_to_go = io->scsiio.kern_data_len;
12489 * The difficult thing here is that the size of the various
12490 * S/G segments may be different than the size from the
12491 * remote controller. That'll make it harder when DMAing
12492 * the data back to the other side.
12494 for (i = 0; len_to_go > 0; i++) {
12495 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT);
12496 local_sglist[i].addr =
12497 malloc(local_sglist[i].len, M_CTL, M_WAITOK);
12499 len_to_go -= local_sglist[i].len;
12502 * Reset the number of S/G entries accordingly. The original
12503 * number of S/G entries is available in rem_sg_entries.
12505 io->scsiio.kern_sg_entries = i;
12508 printf("%s: kern_sg_entries = %d\n", __func__,
12509 io->scsiio.kern_sg_entries);
12510 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12511 printf("%s: sg[%d] = %p, %d\n", __func__, i,
12512 local_sglist[i].addr, local_sglist[i].len);
12519 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12520 ctl_ha_dt_cb callback)
12522 struct ctl_ha_dt_req *rq;
12523 struct ctl_sg_entry *remote_sglist, *local_sglist;
12524 uint32_t local_used, remote_used, total_used;
12527 rq = ctl_dt_req_alloc();
12530 * If we failed to allocate the request, and if the DMA didn't fail
12531 * anyway, set busy status. This is just a resource allocation
12535 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE))
12536 ctl_set_busy(&io->scsiio);
12538 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
12541 ctl_dt_req_free(rq);
12544 * The data move failed. We need to return status back
12545 * to the other controller. No point in trying to DMA
12546 * data to the remote controller.
12549 ctl_send_datamove_done(io, /*have_lock*/ 0);
12554 local_sglist = io->io_hdr.local_sglist;
12555 remote_sglist = io->io_hdr.remote_sglist;
12561 * Pull/push the data over the wire from/to the other controller.
12562 * This takes into account the possibility that the local and
12563 * remote sglists may not be identical in terms of the size of
12564 * the elements and the number of elements.
12566 * One fundamental assumption here is that the length allocated for
12567 * both the local and remote sglists is identical. Otherwise, we've
12568 * essentially got a coding error of some sort.
12570 isc_ret = CTL_HA_STATUS_SUCCESS;
12571 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12575 rq->command = command;
12579 * Both pointers should be aligned. But it is possible
12580 * that the allocation length is not. They should both
12581 * also have enough slack left over at the end, though,
12582 * to round up to the next 8 byte boundary.
12584 cur_len = MIN(local_sglist[i].len - local_used,
12585 remote_sglist[j].len - remote_used);
12586 rq->size = cur_len;
12588 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12589 tmp_ptr += local_used;
12592 /* Use physical addresses when talking to ISC hardware */
12593 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12594 /* XXX KDM use busdma */
12595 rq->local = vtophys(tmp_ptr);
12597 rq->local = tmp_ptr;
12599 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
12600 ("HA does not support BUS_ADDR"));
12601 rq->local = tmp_ptr;
12604 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12605 tmp_ptr += remote_used;
12606 rq->remote = tmp_ptr;
12608 rq->callback = NULL;
12610 local_used += cur_len;
12611 if (local_used >= local_sglist[i].len) {
12616 remote_used += cur_len;
12617 if (remote_used >= remote_sglist[j].len) {
12621 total_used += cur_len;
12623 if (total_used >= io->scsiio.kern_data_len)
12624 rq->callback = callback;
12627 printf("%s: %s: local %#x remote %#x size %d\n", __func__,
12628 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
12629 rq->local, rq->remote, rq->size);
12632 isc_ret = ctl_dt_single(rq);
12633 if (isc_ret > CTL_HA_STATUS_SUCCESS)
12636 if (isc_ret != CTL_HA_STATUS_WAIT) {
12645 ctl_datamove_remote_read(union ctl_io *io)
12651 * This will send an error to the other controller in the case of a
12654 retval = ctl_datamove_remote_sgl_setup(io);
12658 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12659 ctl_datamove_remote_read_cb);
12662 * Make sure we free memory if there was an error.. The
12663 * ctl_datamove_remote_xfer() function will send the
12664 * datamove done message, or call the callback with an
12665 * error if there is a problem.
12667 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12668 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12669 free(io->io_hdr.remote_sglist, M_CTL);
12670 io->io_hdr.remote_sglist = NULL;
12671 io->io_hdr.local_sglist = NULL;
12678 * Process a datamove request from the other controller. This is used for
12679 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
12680 * first. Once that is complete, the data gets DMAed into the remote
12681 * controller's memory. For reads, we DMA from the remote controller's
12682 * memory into our memory first, and then move it out to the FETD.
12685 ctl_datamove_remote(union ctl_io *io)
12688 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12690 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12691 ctl_failover_io(io, /*have_lock*/ 0);
12696 * Note that we look for an aborted I/O here, but don't do some of
12697 * the other checks that ctl_datamove() normally does.
12698 * We don't need to run the datamove delay code, since that should
12699 * have been done if need be on the other controller.
12701 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12702 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__,
12703 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12704 io->io_hdr.nexus.targ_port,
12705 io->io_hdr.nexus.targ_lun);
12706 io->io_hdr.port_status = 31338;
12707 ctl_send_datamove_done(io, /*have_lock*/ 0);
12711 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
12712 ctl_datamove_remote_write(io);
12713 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
12714 ctl_datamove_remote_read(io);
12716 io->io_hdr.port_status = 31339;
12717 ctl_send_datamove_done(io, /*have_lock*/ 0);
12722 ctl_process_done(union ctl_io *io)
12724 struct ctl_lun *lun;
12725 struct ctl_softc *softc = control_softc;
12726 void (*fe_done)(union ctl_io *io);
12727 union ctl_ha_msg msg;
12728 uint32_t targ_port = io->io_hdr.nexus.targ_port;
12730 CTL_DEBUG_PRINT(("ctl_process_done\n"));
12732 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0)
12733 fe_done = softc->ctl_ports[targ_port]->fe_done;
12738 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12743 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12744 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12746 sbuf_cat(&sb, path_str);
12747 switch (io->io_hdr.io_type) {
12749 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12750 sbuf_printf(&sb, "\n");
12751 sbuf_cat(&sb, path_str);
12752 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12753 io->scsiio.tag_num, io->scsiio.tag_type);
12756 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12757 "Tag Type: %d\n", io->taskio.task_action,
12758 io->taskio.tag_num, io->taskio.tag_type);
12761 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12762 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12765 sbuf_cat(&sb, path_str);
12766 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
12767 (intmax_t)time_uptime - io->io_hdr.start_time);
12769 printf("%s", sbuf_data(&sb));
12771 #endif /* CTL_TIME_IO */
12773 switch (io->io_hdr.io_type) {
12777 if (ctl_debug & CTL_DEBUG_INFO)
12778 ctl_io_error_print(io, NULL);
12779 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
12783 return (CTL_RETVAL_COMPLETE);
12785 panic("ctl_process_done: invalid io type %d\n",
12786 io->io_hdr.io_type);
12787 break; /* NOTREACHED */
12790 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12792 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
12793 io->io_hdr.nexus.targ_mapped_lun));
12797 mtx_lock(&lun->lun_lock);
12800 * Check to see if we have any errors to inject here. We only
12801 * inject errors for commands that don't already have errors set.
12803 if ((STAILQ_FIRST(&lun->error_list) != NULL) &&
12804 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
12805 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
12806 ctl_inject_error(lun, io);
12809 * XXX KDM how do we treat commands that aren't completed
12812 * XXX KDM should we also track I/O latency?
12814 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
12815 io->io_hdr.io_type == CTL_IO_SCSI) {
12817 struct bintime cur_bt;
12821 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12823 type = CTL_STATS_READ;
12824 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12826 type = CTL_STATS_WRITE;
12828 type = CTL_STATS_NO_IO;
12830 lun->stats.ports[targ_port].bytes[type] +=
12831 io->scsiio.kern_total_len;
12832 lun->stats.ports[targ_port].operations[type]++;
12834 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
12835 &io->io_hdr.dma_bt);
12836 lun->stats.ports[targ_port].num_dmas[type] +=
12837 io->io_hdr.num_dmas;
12838 getbintime(&cur_bt);
12839 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
12840 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
12845 * Remove this from the OOA queue.
12847 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
12849 if (TAILQ_EMPTY(&lun->ooa_queue))
12850 lun->last_busy = getsbinuptime();
12854 * Run through the blocked queue on this LUN and see if anything
12855 * has become unblocked, now that this transaction is done.
12857 ctl_check_blocked(lun);
12860 * If the LUN has been invalidated, free it if there is nothing
12861 * left on its OOA queue.
12863 if ((lun->flags & CTL_LUN_INVALID)
12864 && TAILQ_EMPTY(&lun->ooa_queue)) {
12865 mtx_unlock(&lun->lun_lock);
12866 mtx_lock(&softc->ctl_lock);
12868 mtx_unlock(&softc->ctl_lock);
12870 mtx_unlock(&lun->lun_lock);
12875 * If this command has been aborted, make sure we set the status
12876 * properly. The FETD is responsible for freeing the I/O and doing
12877 * whatever it needs to do to clean up its state.
12879 if (io->io_hdr.flags & CTL_FLAG_ABORT)
12880 ctl_set_task_aborted(&io->scsiio);
12883 * If enabled, print command error status.
12885 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
12886 (ctl_debug & CTL_DEBUG_INFO) != 0)
12887 ctl_io_error_print(io, NULL);
12890 * Tell the FETD or the other shelf controller we're done with this
12891 * command. Note that only SCSI commands get to this point. Task
12892 * management commands are completed above.
12894 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
12895 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) {
12896 memset(&msg, 0, sizeof(msg));
12897 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
12898 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12899 msg.hdr.nexus = io->io_hdr.nexus;
12900 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12901 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data),
12904 if ((softc->ha_mode == CTL_HA_MODE_XFER)
12905 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12906 memset(&msg, 0, sizeof(msg));
12907 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
12908 msg.hdr.original_sc = io->io_hdr.original_sc;
12909 msg.hdr.nexus = io->io_hdr.nexus;
12910 msg.hdr.status = io->io_hdr.status;
12911 msg.scsi.scsi_status = io->scsiio.scsi_status;
12912 msg.scsi.tag_num = io->scsiio.tag_num;
12913 msg.scsi.tag_type = io->scsiio.tag_type;
12914 msg.scsi.sense_len = io->scsiio.sense_len;
12915 msg.scsi.sense_residual = io->scsiio.sense_residual;
12916 msg.scsi.residual = io->scsiio.residual;
12917 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12918 io->scsiio.sense_len);
12920 * We copy this whether or not this is an I/O-related
12921 * command. Otherwise, we'd have to go and check to see
12922 * whether it's a read/write command, and it really isn't
12925 memcpy(&msg.scsi.lbalen,
12926 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
12927 sizeof(msg.scsi.lbalen));
12929 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12930 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12931 msg.scsi.sense_len, M_WAITOK);
12936 return (CTL_RETVAL_COMPLETE);
12941 * Front end should call this if it doesn't do autosense. When the request
12942 * sense comes back in from the initiator, we'll dequeue this and send it.
12945 ctl_queue_sense(union ctl_io *io)
12947 struct ctl_lun *lun;
12948 struct ctl_port *port;
12949 struct ctl_softc *softc;
12950 uint32_t initidx, targ_lun;
12952 softc = control_softc;
12954 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
12957 * LUN lookup will likely move to the ctl_work_thread() once we
12958 * have our new queueing infrastructure (that doesn't put things on
12959 * a per-LUN queue initially). That is so that we can handle
12960 * things like an INQUIRY to a LUN that we don't have enabled. We
12961 * can't deal with that right now.
12963 mtx_lock(&softc->ctl_lock);
12966 * If we don't have a LUN for this, just toss the sense
12969 port = ctl_io_port(&ctsio->io_hdr);
12970 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
12971 if ((targ_lun < CTL_MAX_LUNS)
12972 && (softc->ctl_luns[targ_lun] != NULL))
12973 lun = softc->ctl_luns[targ_lun];
12977 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12979 mtx_lock(&lun->lun_lock);
12981 * Already have CA set for this LUN...toss the sense information.
12983 if (ctl_is_set(lun->have_ca, initidx)) {
12984 mtx_unlock(&lun->lun_lock);
12988 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data,
12989 MIN(sizeof(lun->pending_sense[initidx]),
12990 sizeof(io->scsiio.sense_data)));
12991 ctl_set_mask(lun->have_ca, initidx);
12992 mtx_unlock(&lun->lun_lock);
12995 mtx_unlock(&softc->ctl_lock);
12999 return (CTL_RETVAL_COMPLETE);
13004 * Primary command inlet from frontend ports. All SCSI and task I/O
13005 * requests must go through this function.
13008 ctl_queue(union ctl_io *io)
13010 struct ctl_port *port;
13012 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13015 io->io_hdr.start_time = time_uptime;
13016 getbintime(&io->io_hdr.start_bt);
13017 #endif /* CTL_TIME_IO */
13019 /* Map FE-specific LUN ID into global one. */
13020 port = ctl_io_port(&io->io_hdr);
13021 io->io_hdr.nexus.targ_mapped_lun =
13022 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13024 switch (io->io_hdr.io_type) {
13027 if (ctl_debug & CTL_DEBUG_CDB)
13029 ctl_enqueue_incoming(io);
13032 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13036 return (CTL_RETVAL_COMPLETE);
13039 #ifdef CTL_IO_DELAY
13041 ctl_done_timer_wakeup(void *arg)
13045 io = (union ctl_io *)arg;
13048 #endif /* CTL_IO_DELAY */
13051 ctl_done(union ctl_io *io)
13055 * Enable this to catch duplicate completion issues.
13058 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13059 printf("%s: type %d msg %d cdb %x iptl: "
13060 "%u:%u:%u tag 0x%04x "
13061 "flag %#x status %x\n",
13063 io->io_hdr.io_type,
13064 io->io_hdr.msg_type,
13066 io->io_hdr.nexus.initid,
13067 io->io_hdr.nexus.targ_port,
13068 io->io_hdr.nexus.targ_lun,
13069 (io->io_hdr.io_type ==
13071 io->taskio.tag_num :
13072 io->scsiio.tag_num,
13074 io->io_hdr.status);
13076 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13080 * This is an internal copy of an I/O, and should not go through
13081 * the normal done processing logic.
13083 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13086 #ifdef CTL_IO_DELAY
13087 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13088 struct ctl_lun *lun;
13090 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13092 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13094 struct ctl_lun *lun;
13096 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13099 && (lun->delay_info.done_delay > 0)) {
13101 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
13102 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13103 callout_reset(&io->io_hdr.delay_callout,
13104 lun->delay_info.done_delay * hz,
13105 ctl_done_timer_wakeup, io);
13106 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13107 lun->delay_info.done_delay = 0;
13111 #endif /* CTL_IO_DELAY */
13113 ctl_enqueue_done(io);
13117 ctl_work_thread(void *arg)
13119 struct ctl_thread *thr = (struct ctl_thread *)arg;
13120 struct ctl_softc *softc = thr->ctl_softc;
13124 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13130 * We handle the queues in this order:
13132 * - done queue (to free up resources, unblock other commands)
13136 * If those queues are empty, we break out of the loop and
13139 mtx_lock(&thr->queue_lock);
13140 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13142 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13143 mtx_unlock(&thr->queue_lock);
13144 ctl_handle_isc(io);
13147 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13149 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13150 /* clear any blocked commands, call fe_done */
13151 mtx_unlock(&thr->queue_lock);
13152 retval = ctl_process_done(io);
13155 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13157 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13158 mtx_unlock(&thr->queue_lock);
13159 if (io->io_hdr.io_type == CTL_IO_TASK)
13162 ctl_scsiio_precheck(softc, &io->scsiio);
13165 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13167 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13168 mtx_unlock(&thr->queue_lock);
13169 retval = ctl_scsiio(&io->scsiio);
13170 if (retval != CTL_RETVAL_COMPLETE)
13171 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13175 /* Sleep until we have something to do. */
13176 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13181 ctl_lun_thread(void *arg)
13183 struct ctl_softc *softc = (struct ctl_softc *)arg;
13184 struct ctl_be_lun *be_lun;
13187 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13191 mtx_lock(&softc->ctl_lock);
13192 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13193 if (be_lun != NULL) {
13194 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13195 mtx_unlock(&softc->ctl_lock);
13196 ctl_create_lun(be_lun);
13200 /* Sleep until we have something to do. */
13201 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13202 PDROP | PRIBIO, "-", 0);
13207 ctl_thresh_thread(void *arg)
13209 struct ctl_softc *softc = (struct ctl_softc *)arg;
13210 struct ctl_lun *lun;
13211 struct ctl_be_lun *be_lun;
13212 struct scsi_da_rw_recovery_page *rwpage;
13213 struct ctl_logical_block_provisioning_page *page;
13215 union ctl_ha_msg msg;
13216 uint64_t thres, val;
13219 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13222 mtx_lock(&softc->ctl_lock);
13223 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13224 be_lun = lun->be_lun;
13225 if ((lun->flags & CTL_LUN_DISABLED) ||
13226 (lun->flags & CTL_LUN_OFFLINE) ||
13227 lun->backend->lun_attr == NULL)
13229 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
13230 softc->ha_mode == CTL_HA_MODE_XFER)
13232 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT];
13233 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0)
13236 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT];
13237 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13238 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13240 thres = scsi_4btoul(page->descr[i].count);
13241 thres <<= CTL_LBP_EXPONENT;
13242 switch (page->descr[i].resource) {
13244 attr = "blocksavail";
13247 attr = "blocksused";
13250 attr = "poolblocksavail";
13253 attr = "poolblocksused";
13258 mtx_unlock(&softc->ctl_lock); // XXX
13259 val = lun->backend->lun_attr(
13260 lun->be_lun->be_lun, attr);
13261 mtx_lock(&softc->ctl_lock);
13262 if (val == UINT64_MAX)
13264 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13265 == SLBPPD_ARMING_INC)
13266 e |= (val >= thres);
13268 e |= (val <= thres);
13270 mtx_lock(&lun->lun_lock);
13272 if (lun->lasttpt == 0 ||
13273 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13274 lun->lasttpt = time_uptime;
13275 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13281 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13284 mtx_unlock(&lun->lun_lock);
13286 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
13287 /* Send msg to other side. */
13288 bzero(&msg.ua, sizeof(msg.ua));
13289 msg.hdr.msg_type = CTL_MSG_UA;
13290 msg.hdr.nexus.initid = -1;
13291 msg.hdr.nexus.targ_port = -1;
13292 msg.hdr.nexus.targ_lun = lun->lun;
13293 msg.hdr.nexus.targ_mapped_lun = lun->lun;
13295 msg.ua.ua_set = (set > 0);
13296 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES;
13297 mtx_unlock(&softc->ctl_lock); // XXX
13298 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13299 sizeof(msg.ua), M_WAITOK);
13300 mtx_lock(&softc->ctl_lock);
13303 mtx_unlock(&softc->ctl_lock);
13304 pause("-", CTL_LBP_PERIOD * hz);
13309 ctl_enqueue_incoming(union ctl_io *io)
13311 struct ctl_softc *softc = control_softc;
13312 struct ctl_thread *thr;
13315 idx = (io->io_hdr.nexus.targ_port * 127 +
13316 io->io_hdr.nexus.initid) % worker_threads;
13317 thr = &softc->threads[idx];
13318 mtx_lock(&thr->queue_lock);
13319 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13320 mtx_unlock(&thr->queue_lock);
13325 ctl_enqueue_rtr(union ctl_io *io)
13327 struct ctl_softc *softc = control_softc;
13328 struct ctl_thread *thr;
13330 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13331 mtx_lock(&thr->queue_lock);
13332 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13333 mtx_unlock(&thr->queue_lock);
13338 ctl_enqueue_done(union ctl_io *io)
13340 struct ctl_softc *softc = control_softc;
13341 struct ctl_thread *thr;
13343 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13344 mtx_lock(&thr->queue_lock);
13345 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13346 mtx_unlock(&thr->queue_lock);
13351 ctl_enqueue_isc(union ctl_io *io)
13353 struct ctl_softc *softc = control_softc;
13354 struct ctl_thread *thr;
13356 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13357 mtx_lock(&thr->queue_lock);
13358 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13359 mtx_unlock(&thr->queue_lock);