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_do_lun_reset(struct ctl_lun *lun, union ctl_io *io,
418 ctl_ua_type ua_type);
419 static int ctl_lun_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
420 static int ctl_abort_task(union ctl_io *io);
421 static int ctl_abort_task_set(union ctl_io *io);
422 static int ctl_query_task(union ctl_io *io, int task_set);
423 static int ctl_i_t_nexus_reset(union ctl_io *io);
424 static int ctl_query_async_event(union ctl_io *io);
425 static void ctl_run_task(union ctl_io *io);
427 static void ctl_datamove_timer_wakeup(void *arg);
428 static void ctl_done_timer_wakeup(void *arg);
429 #endif /* CTL_IO_DELAY */
431 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
432 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
433 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
434 static void ctl_datamove_remote_write(union ctl_io *io);
435 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
436 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
437 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
438 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
439 ctl_ha_dt_cb callback);
440 static void ctl_datamove_remote_read(union ctl_io *io);
441 static void ctl_datamove_remote(union ctl_io *io);
442 static int ctl_process_done(union ctl_io *io);
443 static void ctl_lun_thread(void *arg);
444 static void ctl_thresh_thread(void *arg);
445 static void ctl_work_thread(void *arg);
446 static void ctl_enqueue_incoming(union ctl_io *io);
447 static void ctl_enqueue_rtr(union ctl_io *io);
448 static void ctl_enqueue_done(union ctl_io *io);
449 static void ctl_enqueue_isc(union ctl_io *io);
450 static const struct ctl_cmd_entry *
451 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
452 static const struct ctl_cmd_entry *
453 ctl_validate_command(struct ctl_scsiio *ctsio);
454 static int ctl_cmd_applicable(uint8_t lun_type,
455 const struct ctl_cmd_entry *entry);
457 static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx);
458 static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx);
459 static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx);
460 static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key);
463 * Load the serialization table. This isn't very pretty, but is probably
464 * the easiest way to do it.
466 #include "ctl_ser_table.c"
469 * We only need to define open, close and ioctl routines for this driver.
471 static struct cdevsw ctl_cdevsw = {
472 .d_version = D_VERSION,
475 .d_close = ctl_close,
476 .d_ioctl = ctl_ioctl,
481 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
483 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
485 static moduledata_t ctl_moduledata = {
487 ctl_module_event_handler,
491 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
492 MODULE_VERSION(ctl, 1);
494 static struct ctl_frontend ha_frontend =
500 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
501 union ctl_ha_msg *msg_info)
503 struct ctl_scsiio *ctsio;
505 if (msg_info->hdr.original_sc == NULL) {
506 printf("%s: original_sc == NULL!\n", __func__);
507 /* XXX KDM now what? */
511 ctsio = &msg_info->hdr.original_sc->scsiio;
512 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
513 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
514 ctsio->io_hdr.status = msg_info->hdr.status;
515 ctsio->scsi_status = msg_info->scsi.scsi_status;
516 ctsio->sense_len = msg_info->scsi.sense_len;
517 ctsio->sense_residual = msg_info->scsi.sense_residual;
518 ctsio->residual = msg_info->scsi.residual;
519 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
520 msg_info->scsi.sense_len);
521 ctl_enqueue_isc((union ctl_io *)ctsio);
525 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
526 union ctl_ha_msg *msg_info)
528 struct ctl_scsiio *ctsio;
530 if (msg_info->hdr.serializing_sc == NULL) {
531 printf("%s: serializing_sc == NULL!\n", __func__);
532 /* XXX KDM now what? */
536 ctsio = &msg_info->hdr.serializing_sc->scsiio;
537 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
538 ctl_enqueue_isc((union ctl_io *)ctsio);
542 ctl_isc_announce_lun(struct ctl_lun *lun)
544 struct ctl_softc *softc = lun->ctl_softc;
545 union ctl_ha_msg *msg;
546 struct ctl_ha_msg_lun_pr_key pr_key;
549 if (softc->ha_link != CTL_HA_LINK_ONLINE)
551 mtx_lock(&lun->lun_lock);
552 i = sizeof(msg->lun);
554 i += lun->lun_devid->len;
555 i += sizeof(pr_key) * lun->pr_key_count;
557 mtx_unlock(&lun->lun_lock);
558 msg = malloc(i, M_CTL, M_WAITOK);
559 mtx_lock(&lun->lun_lock);
560 k = sizeof(msg->lun);
562 k += lun->lun_devid->len;
563 k += sizeof(pr_key) * lun->pr_key_count;
569 bzero(&msg->lun, sizeof(msg->lun));
570 msg->hdr.msg_type = CTL_MSG_LUN_SYNC;
571 msg->hdr.nexus.targ_lun = lun->lun;
572 msg->hdr.nexus.targ_mapped_lun = lun->lun;
573 msg->lun.flags = lun->flags;
574 msg->lun.pr_generation = lun->PRGeneration;
575 msg->lun.pr_res_idx = lun->pr_res_idx;
576 msg->lun.pr_res_type = lun->res_type;
577 msg->lun.pr_key_count = lun->pr_key_count;
579 if (lun->lun_devid) {
580 msg->lun.lun_devid_len = lun->lun_devid->len;
581 memcpy(&msg->lun.data[i], lun->lun_devid->data,
582 msg->lun.lun_devid_len);
583 i += msg->lun.lun_devid_len;
585 for (k = 0; k < CTL_MAX_INITIATORS; k++) {
586 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0)
589 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key));
592 mtx_unlock(&lun->lun_lock);
593 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
599 ctl_isc_announce_port(struct ctl_port *port)
601 struct ctl_softc *softc = control_softc;
602 union ctl_ha_msg *msg;
605 if (port->targ_port < softc->port_min ||
606 port->targ_port >= softc->port_max ||
607 softc->ha_link != CTL_HA_LINK_ONLINE)
609 i = sizeof(msg->port) + strlen(port->port_name) + 1;
611 i += sizeof(uint32_t) * CTL_MAX_LUNS;
612 if (port->port_devid)
613 i += port->port_devid->len;
614 if (port->target_devid)
615 i += port->target_devid->len;
616 msg = malloc(i, M_CTL, M_WAITOK);
617 bzero(&msg->port, sizeof(msg->port));
618 msg->hdr.msg_type = CTL_MSG_PORT_SYNC;
619 msg->hdr.nexus.targ_port = port->targ_port;
620 msg->port.port_type = port->port_type;
621 msg->port.physical_port = port->physical_port;
622 msg->port.virtual_port = port->virtual_port;
623 msg->port.status = port->status;
625 msg->port.name_len = sprintf(&msg->port.data[i],
626 "%d:%s", softc->ha_id, port->port_name) + 1;
627 i += msg->port.name_len;
629 msg->port.lun_map_len = sizeof(uint32_t) * CTL_MAX_LUNS;
630 memcpy(&msg->port.data[i], port->lun_map,
631 msg->port.lun_map_len);
632 i += msg->port.lun_map_len;
634 if (port->port_devid) {
635 msg->port.port_devid_len = port->port_devid->len;
636 memcpy(&msg->port.data[i], port->port_devid->data,
637 msg->port.port_devid_len);
638 i += msg->port.port_devid_len;
640 if (port->target_devid) {
641 msg->port.target_devid_len = port->target_devid->len;
642 memcpy(&msg->port.data[i], port->target_devid->data,
643 msg->port.target_devid_len);
644 i += msg->port.target_devid_len;
646 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
652 ctl_isc_ha_link_up(struct ctl_softc *softc)
654 struct ctl_port *port;
657 STAILQ_FOREACH(port, &softc->port_list, links)
658 ctl_isc_announce_port(port);
659 STAILQ_FOREACH(lun, &softc->lun_list, links)
660 ctl_isc_announce_lun(lun);
664 ctl_isc_ha_link_down(struct ctl_softc *softc)
666 struct ctl_port *port;
670 mtx_lock(&softc->ctl_lock);
671 STAILQ_FOREACH(lun, &softc->lun_list, links) {
672 mtx_lock(&lun->lun_lock);
673 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) {
674 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
675 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
677 mtx_unlock(&lun->lun_lock);
679 mtx_unlock(&softc->ctl_lock);
680 io = ctl_alloc_io(softc->othersc_pool);
681 mtx_lock(&softc->ctl_lock);
683 io->io_hdr.msg_type = CTL_MSG_FAILOVER;
684 io->io_hdr.nexus.targ_mapped_lun = lun->lun;
688 STAILQ_FOREACH(port, &softc->port_list, links) {
689 if (port->targ_port >= softc->port_min &&
690 port->targ_port < softc->port_max)
692 port->status &= ~CTL_PORT_STATUS_ONLINE;
694 mtx_unlock(&softc->ctl_lock);
698 ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
701 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus);
703 mtx_lock(&softc->ctl_lock);
704 if (msg->hdr.nexus.targ_lun < CTL_MAX_LUNS &&
705 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) != NULL) {
706 mtx_lock(&lun->lun_lock);
707 mtx_unlock(&softc->ctl_lock);
708 if (msg->ua.ua_all) {
710 ctl_est_ua_all(lun, iid, msg->ua.ua_type);
712 ctl_clr_ua_all(lun, iid, msg->ua.ua_type);
715 ctl_est_ua(lun, iid, msg->ua.ua_type);
717 ctl_clr_ua(lun, iid, msg->ua.ua_type);
719 mtx_unlock(&lun->lun_lock);
721 mtx_unlock(&softc->ctl_lock);
725 ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
728 struct ctl_ha_msg_lun_pr_key pr_key;
730 ctl_lun_flags oflags;
733 targ_lun = msg->hdr.nexus.targ_mapped_lun;
734 mtx_lock(&softc->ctl_lock);
735 if ((targ_lun >= CTL_MAX_LUNS) ||
736 ((lun = softc->ctl_luns[targ_lun]) == NULL)) {
737 mtx_unlock(&softc->ctl_lock);
740 mtx_lock(&lun->lun_lock);
741 mtx_unlock(&softc->ctl_lock);
742 if (lun->flags & CTL_LUN_DISABLED) {
743 mtx_unlock(&lun->lun_lock);
746 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0;
747 if (msg->lun.lun_devid_len != i || (i > 0 &&
748 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) {
749 mtx_unlock(&lun->lun_lock);
750 printf("%s: Received conflicting HA LUN %d\n",
751 __func__, msg->hdr.nexus.targ_lun);
754 /* Record whether peer is primary. */
756 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) &&
757 (msg->lun.flags & CTL_LUN_DISABLED) == 0)
758 lun->flags |= CTL_LUN_PEER_SC_PRIMARY;
760 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
761 if (oflags != lun->flags)
762 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
764 /* If peer is primary and we are not -- use data */
765 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
766 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) {
767 lun->PRGeneration = msg->lun.pr_generation;
768 lun->pr_res_idx = msg->lun.pr_res_idx;
769 lun->res_type = msg->lun.pr_res_type;
770 lun->pr_key_count = msg->lun.pr_key_count;
771 for (k = 0; k < CTL_MAX_INITIATORS; k++)
772 ctl_clr_prkey(lun, k);
773 for (k = 0; k < msg->lun.pr_key_count; k++) {
774 memcpy(&pr_key, &msg->lun.data[i],
776 ctl_alloc_prkey(lun, pr_key.pr_iid);
777 ctl_set_prkey(lun, pr_key.pr_iid,
783 mtx_unlock(&lun->lun_lock);
784 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n",
785 __func__, msg->hdr.nexus.targ_lun,
786 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ?
787 "primary" : "secondary"));
789 /* If we are primary but peer doesn't know -- notify */
790 if ((lun->flags & CTL_LUN_PRIMARY_SC) &&
791 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0)
792 ctl_isc_announce_lun(lun);
797 ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
799 struct ctl_port *port;
803 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
805 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__,
806 msg->hdr.nexus.targ_port));
808 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO);
809 port->frontend = &ha_frontend;
810 port->targ_port = msg->hdr.nexus.targ_port;
811 } else if (port->frontend == &ha_frontend) {
812 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__,
813 msg->hdr.nexus.targ_port));
816 printf("%s: Received conflicting HA port %d\n",
817 __func__, msg->hdr.nexus.targ_port);
820 port->port_type = msg->port.port_type;
821 port->physical_port = msg->port.physical_port;
822 port->virtual_port = msg->port.virtual_port;
823 port->status = msg->port.status;
825 free(port->port_name, M_CTL);
826 port->port_name = strndup(&msg->port.data[i], msg->port.name_len,
828 i += msg->port.name_len;
829 if (msg->port.lun_map_len != 0) {
830 if (port->lun_map == NULL)
831 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS,
833 memcpy(port->lun_map, &msg->port.data[i],
834 sizeof(uint32_t) * CTL_MAX_LUNS);
835 i += msg->port.lun_map_len;
837 free(port->lun_map, M_CTL);
838 port->lun_map = NULL;
840 if (msg->port.port_devid_len != 0) {
841 if (port->port_devid == NULL ||
842 port->port_devid->len != msg->port.port_devid_len) {
843 free(port->port_devid, M_CTL);
844 port->port_devid = malloc(sizeof(struct ctl_devid) +
845 msg->port.port_devid_len, M_CTL, M_WAITOK);
847 memcpy(port->port_devid->data, &msg->port.data[i],
848 msg->port.port_devid_len);
849 port->port_devid->len = msg->port.port_devid_len;
850 i += msg->port.port_devid_len;
852 free(port->port_devid, M_CTL);
853 port->port_devid = NULL;
855 if (msg->port.target_devid_len != 0) {
856 if (port->target_devid == NULL ||
857 port->target_devid->len != msg->port.target_devid_len) {
858 free(port->target_devid, M_CTL);
859 port->target_devid = malloc(sizeof(struct ctl_devid) +
860 msg->port.target_devid_len, M_CTL, M_WAITOK);
862 memcpy(port->target_devid->data, &msg->port.data[i],
863 msg->port.target_devid_len);
864 port->target_devid->len = msg->port.target_devid_len;
865 i += msg->port.target_devid_len;
867 free(port->port_devid, M_CTL);
868 port->port_devid = NULL;
871 if (ctl_port_register(port) != 0) {
872 printf("%s: ctl_port_register() failed with error\n",
876 mtx_lock(&softc->ctl_lock);
877 STAILQ_FOREACH(lun, &softc->lun_list, links) {
878 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
880 mtx_lock(&lun->lun_lock);
881 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
882 mtx_unlock(&lun->lun_lock);
884 mtx_unlock(&softc->ctl_lock);
888 * ISC (Inter Shelf Communication) event handler. Events from the HA
889 * subsystem come in here.
892 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
894 struct ctl_softc *softc;
896 struct ctl_prio *presio;
897 ctl_ha_status isc_status;
899 softc = control_softc;
900 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event));
901 if (event == CTL_HA_EVT_MSG_RECV) {
902 union ctl_ha_msg *msg, msgbuf;
904 if (param > sizeof(msgbuf))
905 msg = malloc(param, M_CTL, M_WAITOK);
908 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param,
910 if (isc_status != CTL_HA_STATUS_SUCCESS) {
911 printf("%s: Error receiving message: %d\n",
912 __func__, isc_status);
918 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type));
919 switch (msg->hdr.msg_type) {
920 case CTL_MSG_SERIALIZE:
921 io = ctl_alloc_io(softc->othersc_pool);
923 // populate ctsio from msg
924 io->io_hdr.io_type = CTL_IO_SCSI;
925 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
926 io->io_hdr.original_sc = msg->hdr.original_sc;
927 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
930 * If we're in serialization-only mode, we don't
931 * want to go through full done processing. Thus
934 * XXX KDM add another flag that is more specific.
936 if (softc->ha_mode != CTL_HA_MODE_XFER)
937 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
938 io->io_hdr.nexus = msg->hdr.nexus;
940 printf("port %u, iid %u, lun %u\n",
941 io->io_hdr.nexus.targ_port,
942 io->io_hdr.nexus.initid,
943 io->io_hdr.nexus.targ_lun);
945 io->scsiio.tag_num = msg->scsi.tag_num;
946 io->scsiio.tag_type = msg->scsi.tag_type;
948 io->io_hdr.start_time = time_uptime;
949 getbintime(&io->io_hdr.start_bt);
950 #endif /* CTL_TIME_IO */
951 io->scsiio.cdb_len = msg->scsi.cdb_len;
952 memcpy(io->scsiio.cdb, msg->scsi.cdb,
954 if (softc->ha_mode == CTL_HA_MODE_XFER) {
955 const struct ctl_cmd_entry *entry;
957 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
958 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
960 entry->flags & CTL_FLAG_DATA_MASK;
965 /* Performed on the Originating SC, XFER mode only */
966 case CTL_MSG_DATAMOVE: {
967 struct ctl_sg_entry *sgl;
970 io = msg->hdr.original_sc;
972 printf("%s: original_sc == NULL!\n", __func__);
973 /* XXX KDM do something here */
976 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
977 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
979 * Keep track of this, we need to send it back over
980 * when the datamove is complete.
982 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
983 if (msg->hdr.status == CTL_SUCCESS)
984 io->io_hdr.status = msg->hdr.status;
986 if (msg->dt.sg_sequence == 0) {
987 i = msg->dt.kern_sg_entries +
988 io->scsiio.kern_data_len /
989 CTL_HA_DATAMOVE_SEGMENT + 1;
990 sgl = malloc(sizeof(*sgl) * i, M_CTL,
992 io->io_hdr.remote_sglist = sgl;
993 io->io_hdr.local_sglist =
994 &sgl[msg->dt.kern_sg_entries];
996 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
998 io->scsiio.kern_sg_entries =
999 msg->dt.kern_sg_entries;
1000 io->scsiio.rem_sg_entries =
1001 msg->dt.kern_sg_entries;
1002 io->scsiio.kern_data_len =
1003 msg->dt.kern_data_len;
1004 io->scsiio.kern_total_len =
1005 msg->dt.kern_total_len;
1006 io->scsiio.kern_data_resid =
1007 msg->dt.kern_data_resid;
1008 io->scsiio.kern_rel_offset =
1009 msg->dt.kern_rel_offset;
1010 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR;
1011 io->io_hdr.flags |= msg->dt.flags &
1014 sgl = (struct ctl_sg_entry *)
1015 io->scsiio.kern_data_ptr;
1017 for (i = msg->dt.sent_sg_entries, j = 0;
1018 i < (msg->dt.sent_sg_entries +
1019 msg->dt.cur_sg_entries); i++, j++) {
1020 sgl[i].addr = msg->dt.sg_list[j].addr;
1021 sgl[i].len = msg->dt.sg_list[j].len;
1024 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n",
1026 msg->dt.sg_list[j].addr,
1027 msg->dt.sg_list[j].len,
1028 sgl[i].addr, sgl[i].len, j, i);
1033 * If this is the last piece of the I/O, we've got
1034 * the full S/G list. Queue processing in the thread.
1035 * Otherwise wait for the next piece.
1037 if (msg->dt.sg_last != 0)
1038 ctl_enqueue_isc(io);
1041 /* Performed on the Serializing (primary) SC, XFER mode only */
1042 case CTL_MSG_DATAMOVE_DONE: {
1043 if (msg->hdr.serializing_sc == NULL) {
1044 printf("%s: serializing_sc == NULL!\n",
1046 /* XXX KDM now what? */
1050 * We grab the sense information here in case
1051 * there was a failure, so we can return status
1052 * back to the initiator.
1054 io = msg->hdr.serializing_sc;
1055 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
1056 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1057 io->io_hdr.port_status = msg->scsi.fetd_status;
1058 io->scsiio.residual = msg->scsi.residual;
1059 if (msg->hdr.status != CTL_STATUS_NONE) {
1060 io->io_hdr.status = msg->hdr.status;
1061 io->scsiio.scsi_status = msg->scsi.scsi_status;
1062 io->scsiio.sense_len = msg->scsi.sense_len;
1063 io->scsiio.sense_residual =msg->scsi.sense_residual;
1064 memcpy(&io->scsiio.sense_data,
1065 &msg->scsi.sense_data,
1066 msg->scsi.sense_len);
1067 if (msg->hdr.status == CTL_SUCCESS)
1068 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1070 ctl_enqueue_isc(io);
1074 /* Preformed on Originating SC, SER_ONLY mode */
1076 io = msg->hdr.original_sc;
1078 printf("%s: original_sc == NULL!\n",
1082 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1083 io->io_hdr.msg_type = CTL_MSG_R2R;
1084 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1085 ctl_enqueue_isc(io);
1089 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
1091 * Performed on the Originating (i.e. secondary) SC in XFER
1094 case CTL_MSG_FINISH_IO:
1095 if (softc->ha_mode == CTL_HA_MODE_XFER)
1096 ctl_isc_handler_finish_xfer(softc, msg);
1098 ctl_isc_handler_finish_ser_only(softc, msg);
1101 /* Preformed on Originating SC */
1102 case CTL_MSG_BAD_JUJU:
1103 io = msg->hdr.original_sc;
1105 printf("%s: Bad JUJU!, original_sc is NULL!\n",
1109 ctl_copy_sense_data(msg, io);
1111 * IO should have already been cleaned up on other
1112 * SC so clear this flag so we won't send a message
1113 * back to finish the IO there.
1115 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
1116 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1118 /* io = msg->hdr.serializing_sc; */
1119 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
1120 ctl_enqueue_isc(io);
1123 /* Handle resets sent from the other side */
1124 case CTL_MSG_MANAGE_TASKS: {
1125 struct ctl_taskio *taskio;
1126 taskio = (struct ctl_taskio *)ctl_alloc_io(
1127 softc->othersc_pool);
1128 ctl_zero_io((union ctl_io *)taskio);
1129 taskio->io_hdr.io_type = CTL_IO_TASK;
1130 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1131 taskio->io_hdr.nexus = msg->hdr.nexus;
1132 taskio->task_action = msg->task.task_action;
1133 taskio->tag_num = msg->task.tag_num;
1134 taskio->tag_type = msg->task.tag_type;
1136 taskio->io_hdr.start_time = time_uptime;
1137 getbintime(&taskio->io_hdr.start_bt);
1138 #endif /* CTL_TIME_IO */
1139 ctl_run_task((union ctl_io *)taskio);
1142 /* Persistent Reserve action which needs attention */
1143 case CTL_MSG_PERS_ACTION:
1144 presio = (struct ctl_prio *)ctl_alloc_io(
1145 softc->othersc_pool);
1146 ctl_zero_io((union ctl_io *)presio);
1147 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
1148 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1149 presio->io_hdr.nexus = msg->hdr.nexus;
1150 presio->pr_msg = msg->pr;
1151 ctl_enqueue_isc((union ctl_io *)presio);
1154 ctl_isc_ua(softc, msg, param);
1156 case CTL_MSG_PORT_SYNC:
1157 ctl_isc_port_sync(softc, msg, param);
1159 case CTL_MSG_LUN_SYNC:
1160 ctl_isc_lun_sync(softc, msg, param);
1163 printf("Received HA message of unknown type %d\n",
1169 } else if (event == CTL_HA_EVT_LINK_CHANGE) {
1170 printf("CTL: HA link status changed from %d to %d\n",
1171 softc->ha_link, param);
1172 if (param == softc->ha_link)
1174 if (softc->ha_link == CTL_HA_LINK_ONLINE) {
1175 softc->ha_link = param;
1176 ctl_isc_ha_link_down(softc);
1178 softc->ha_link = param;
1179 if (softc->ha_link == CTL_HA_LINK_ONLINE)
1180 ctl_isc_ha_link_up(softc);
1184 printf("ctl_isc_event_handler: Unknown event %d\n", event);
1190 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
1193 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data,
1194 src->scsi.sense_len);
1195 dest->scsiio.scsi_status = src->scsi.scsi_status;
1196 dest->scsiio.sense_len = src->scsi.sense_len;
1197 dest->io_hdr.status = src->hdr.status;
1201 ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest)
1204 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data,
1205 src->scsiio.sense_len);
1206 dest->scsi.scsi_status = src->scsiio.scsi_status;
1207 dest->scsi.sense_len = src->scsiio.sense_len;
1208 dest->hdr.status = src->io_hdr.status;
1212 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1214 struct ctl_softc *softc = lun->ctl_softc;
1217 if (initidx < softc->init_min || initidx >= softc->init_max)
1219 mtx_assert(&lun->lun_lock, MA_OWNED);
1220 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1223 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua;
1227 ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua)
1231 mtx_assert(&lun->lun_lock, MA_OWNED);
1232 if (lun->pending_ua[port] == NULL)
1234 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1235 if (port * CTL_MAX_INIT_PER_PORT + i == except)
1237 lun->pending_ua[port][i] |= ua;
1242 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1244 struct ctl_softc *softc = lun->ctl_softc;
1247 mtx_assert(&lun->lun_lock, MA_OWNED);
1248 for (i = softc->port_min; i < softc->port_max; i++)
1249 ctl_est_ua_port(lun, i, except, ua);
1253 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1255 struct ctl_softc *softc = lun->ctl_softc;
1258 if (initidx < softc->init_min || initidx >= softc->init_max)
1260 mtx_assert(&lun->lun_lock, MA_OWNED);
1261 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1264 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua;
1268 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1270 struct ctl_softc *softc = lun->ctl_softc;
1273 mtx_assert(&lun->lun_lock, MA_OWNED);
1274 for (i = softc->port_min; i < softc->port_max; i++) {
1275 if (lun->pending_ua[i] == NULL)
1277 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
1278 if (i * CTL_MAX_INIT_PER_PORT + j == except)
1280 lun->pending_ua[i][j] &= ~ua;
1286 ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx,
1287 ctl_ua_type ua_type)
1289 struct ctl_lun *lun;
1291 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
1292 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) {
1293 mtx_lock(&lun->lun_lock);
1294 ctl_clr_ua(lun, initidx, ua_type);
1295 mtx_unlock(&lun->lun_lock);
1300 ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS)
1302 struct ctl_softc *softc = (struct ctl_softc *)arg1;
1303 struct ctl_lun *lun;
1304 struct ctl_lun_req ireq;
1307 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1;
1308 error = sysctl_handle_int(oidp, &value, 0, req);
1309 if ((error != 0) || (req->newptr == NULL))
1312 mtx_lock(&softc->ctl_lock);
1314 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1316 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
1317 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1318 mtx_unlock(&softc->ctl_lock);
1319 bzero(&ireq, sizeof(ireq));
1320 ireq.reqtype = CTL_LUNREQ_MODIFY;
1321 ireq.reqdata.modify.lun_id = lun->lun;
1322 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0,
1324 if (ireq.status != CTL_LUN_OK) {
1325 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n",
1326 __func__, ireq.status, ireq.error_str);
1328 mtx_lock(&softc->ctl_lock);
1330 mtx_unlock(&softc->ctl_lock);
1337 struct ctl_softc *softc;
1339 int i, error, retval;
1342 control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1344 softc = control_softc;
1346 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
1349 softc->dev->si_drv1 = softc;
1351 sysctl_ctx_init(&softc->sysctl_ctx);
1352 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1353 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1354 CTLFLAG_RD, 0, "CAM Target Layer");
1356 if (softc->sysctl_tree == NULL) {
1357 printf("%s: unable to allocate sysctl tree\n", __func__);
1358 destroy_dev(softc->dev);
1359 free(control_softc, M_DEVBUF);
1360 control_softc = NULL;
1364 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1365 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
1366 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1367 softc->open_count = 0;
1370 * Default to actually sending a SYNCHRONIZE CACHE command down to
1373 softc->flags = CTL_FLAG_REAL_SYNC;
1375 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1376 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0,
1377 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)");
1380 * In Copan's HA scheme, the "master" and "slave" roles are
1381 * figured out through the slot the controller is in. Although it
1382 * is an active/active system, someone has to be in charge.
1384 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1385 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1386 "HA head ID (0 - no HA)");
1387 if (softc->ha_id == 0 || softc->ha_id > NUM_TARGET_PORT_GROUPS) {
1388 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1389 softc->is_single = 1;
1390 softc->port_cnt = CTL_MAX_PORTS;
1391 softc->port_min = 0;
1393 softc->port_cnt = CTL_MAX_PORTS / NUM_TARGET_PORT_GROUPS;
1394 softc->port_min = (softc->ha_id - 1) * softc->port_cnt;
1396 softc->port_max = softc->port_min + softc->port_cnt;
1397 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT;
1398 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT;
1400 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1401 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0,
1402 "HA link state (0 - offline, 1 - unknown, 2 - online)");
1404 STAILQ_INIT(&softc->lun_list);
1405 STAILQ_INIT(&softc->pending_lun_queue);
1406 STAILQ_INIT(&softc->fe_list);
1407 STAILQ_INIT(&softc->port_list);
1408 STAILQ_INIT(&softc->be_list);
1409 ctl_tpc_init(softc);
1411 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
1414 printf("ctl: can't allocate %d entry other SC pool, "
1415 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1418 softc->othersc_pool = other_pool;
1420 if (worker_threads <= 0)
1421 worker_threads = max(1, mp_ncpus / 4);
1422 if (worker_threads > CTL_MAX_THREADS)
1423 worker_threads = CTL_MAX_THREADS;
1425 for (i = 0; i < worker_threads; i++) {
1426 struct ctl_thread *thr = &softc->threads[i];
1428 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1429 thr->ctl_softc = softc;
1430 STAILQ_INIT(&thr->incoming_queue);
1431 STAILQ_INIT(&thr->rtr_queue);
1432 STAILQ_INIT(&thr->done_queue);
1433 STAILQ_INIT(&thr->isc_queue);
1435 error = kproc_kthread_add(ctl_work_thread, thr,
1436 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1438 printf("error creating CTL work thread!\n");
1439 ctl_pool_free(other_pool);
1443 error = kproc_kthread_add(ctl_lun_thread, softc,
1444 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
1446 printf("error creating CTL lun thread!\n");
1447 ctl_pool_free(other_pool);
1450 error = kproc_kthread_add(ctl_thresh_thread, softc,
1451 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh");
1453 printf("error creating CTL threshold thread!\n");
1454 ctl_pool_free(other_pool);
1458 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1459 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN,
1460 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head");
1462 if (softc->is_single == 0) {
1463 ctl_frontend_register(&ha_frontend);
1464 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) {
1465 printf("ctl_init: ctl_ha_msg_init failed.\n");
1466 softc->is_single = 1;
1468 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
1469 != CTL_HA_STATUS_SUCCESS) {
1470 printf("ctl_init: ctl_ha_msg_register failed.\n");
1471 softc->is_single = 1;
1480 struct ctl_softc *softc;
1481 struct ctl_lun *lun, *next_lun;
1483 softc = (struct ctl_softc *)control_softc;
1485 if (softc->is_single == 0) {
1486 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL)
1487 != CTL_HA_STATUS_SUCCESS) {
1488 printf("ctl_shutdown: ctl_ha_msg_deregister failed.\n");
1490 if (ctl_ha_msg_shutdown(softc) != CTL_HA_STATUS_SUCCESS) {
1491 printf("ctl_shutdown: ctl_ha_msg_shutdown failed.\n");
1493 ctl_frontend_deregister(&ha_frontend);
1496 mtx_lock(&softc->ctl_lock);
1501 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
1502 next_lun = STAILQ_NEXT(lun, links);
1506 mtx_unlock(&softc->ctl_lock);
1509 ctl_shutdown_thread(softc->work_thread);
1510 mtx_destroy(&softc->queue_lock);
1513 ctl_tpc_shutdown(softc);
1514 uma_zdestroy(softc->io_zone);
1515 mtx_destroy(&softc->ctl_lock);
1517 destroy_dev(softc->dev);
1519 sysctl_ctx_free(&softc->sysctl_ctx);
1521 free(control_softc, M_DEVBUF);
1522 control_softc = NULL;
1526 ctl_module_event_handler(module_t mod, int what, void *arg)
1531 return (ctl_init());
1535 return (EOPNOTSUPP);
1540 * XXX KDM should we do some access checks here? Bump a reference count to
1541 * prevent a CTL module from being unloaded while someone has it open?
1544 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1550 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1556 * Remove an initiator by port number and initiator ID.
1557 * Returns 0 for success, -1 for failure.
1560 ctl_remove_initiator(struct ctl_port *port, int iid)
1562 struct ctl_softc *softc = control_softc;
1564 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1566 if (iid > CTL_MAX_INIT_PER_PORT) {
1567 printf("%s: initiator ID %u > maximun %u!\n",
1568 __func__, iid, CTL_MAX_INIT_PER_PORT);
1572 mtx_lock(&softc->ctl_lock);
1573 port->wwpn_iid[iid].in_use--;
1574 port->wwpn_iid[iid].last_use = time_uptime;
1575 mtx_unlock(&softc->ctl_lock);
1581 * Add an initiator to the initiator map.
1582 * Returns iid for success, < 0 for failure.
1585 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
1587 struct ctl_softc *softc = control_softc;
1591 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1593 if (iid >= CTL_MAX_INIT_PER_PORT) {
1594 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
1595 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
1600 mtx_lock(&softc->ctl_lock);
1602 if (iid < 0 && (wwpn != 0 || name != NULL)) {
1603 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1604 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
1608 if (name != NULL && port->wwpn_iid[i].name != NULL &&
1609 strcmp(name, port->wwpn_iid[i].name) == 0) {
1617 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1618 if (port->wwpn_iid[i].in_use == 0 &&
1619 port->wwpn_iid[i].wwpn == 0 &&
1620 port->wwpn_iid[i].name == NULL) {
1629 best_time = INT32_MAX;
1630 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1631 if (port->wwpn_iid[i].in_use == 0) {
1632 if (port->wwpn_iid[i].last_use < best_time) {
1634 best_time = port->wwpn_iid[i].last_use;
1642 mtx_unlock(&softc->ctl_lock);
1647 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
1649 * This is not an error yet.
1651 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
1653 printf("%s: port %d iid %u WWPN %#jx arrived"
1654 " again\n", __func__, port->targ_port,
1655 iid, (uintmax_t)wwpn);
1659 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
1660 strcmp(name, port->wwpn_iid[iid].name) == 0) {
1662 printf("%s: port %d iid %u name '%s' arrived"
1663 " again\n", __func__, port->targ_port,
1670 * This is an error, but what do we do about it? The
1671 * driver is telling us we have a new WWPN for this
1672 * initiator ID, so we pretty much need to use it.
1674 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
1675 " but WWPN %#jx '%s' is still at that address\n",
1676 __func__, port->targ_port, iid, wwpn, name,
1677 (uintmax_t)port->wwpn_iid[iid].wwpn,
1678 port->wwpn_iid[iid].name);
1681 * XXX KDM clear have_ca and ua_pending on each LUN for
1686 free(port->wwpn_iid[iid].name, M_CTL);
1687 port->wwpn_iid[iid].name = name;
1688 port->wwpn_iid[iid].wwpn = wwpn;
1689 port->wwpn_iid[iid].in_use++;
1690 mtx_unlock(&softc->ctl_lock);
1696 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
1700 switch (port->port_type) {
1703 struct scsi_transportid_fcp *id =
1704 (struct scsi_transportid_fcp *)buf;
1705 if (port->wwpn_iid[iid].wwpn == 0)
1707 memset(id, 0, sizeof(*id));
1708 id->format_protocol = SCSI_PROTO_FC;
1709 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
1710 return (sizeof(*id));
1712 case CTL_PORT_ISCSI:
1714 struct scsi_transportid_iscsi_port *id =
1715 (struct scsi_transportid_iscsi_port *)buf;
1716 if (port->wwpn_iid[iid].name == NULL)
1719 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
1721 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
1722 len = roundup2(min(len, 252), 4);
1723 scsi_ulto2b(len, id->additional_length);
1724 return (sizeof(*id) + len);
1728 struct scsi_transportid_sas *id =
1729 (struct scsi_transportid_sas *)buf;
1730 if (port->wwpn_iid[iid].wwpn == 0)
1732 memset(id, 0, sizeof(*id));
1733 id->format_protocol = SCSI_PROTO_SAS;
1734 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
1735 return (sizeof(*id));
1739 struct scsi_transportid_spi *id =
1740 (struct scsi_transportid_spi *)buf;
1741 memset(id, 0, sizeof(*id));
1742 id->format_protocol = SCSI_PROTO_SPI;
1743 scsi_ulto2b(iid, id->scsi_addr);
1744 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
1745 return (sizeof(*id));
1751 * Serialize a command that went down the "wrong" side, and so was sent to
1752 * this controller for execution. The logic is a little different than the
1753 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
1754 * sent back to the other side, but in the success case, we execute the
1755 * command on this side (XFER mode) or tell the other side to execute it
1759 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
1761 struct ctl_softc *softc;
1762 union ctl_ha_msg msg_info;
1763 struct ctl_lun *lun;
1764 const struct ctl_cmd_entry *entry;
1768 softc = control_softc;
1770 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
1771 mtx_lock(&softc->ctl_lock);
1772 if ((targ_lun < CTL_MAX_LUNS) &&
1773 ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
1774 mtx_lock(&lun->lun_lock);
1775 mtx_unlock(&softc->ctl_lock);
1777 * If the LUN is invalid, pretend that it doesn't exist.
1778 * It will go away as soon as all pending I/O has been
1781 if (lun->flags & CTL_LUN_DISABLED) {
1782 mtx_unlock(&lun->lun_lock);
1786 mtx_unlock(&softc->ctl_lock);
1791 * The other node would not send this request to us unless
1792 * received announce that we are primary node for this LUN.
1793 * If this LUN does not exist now, it is probably result of
1794 * a race, so respond to initiator in the most opaque way.
1796 ctl_set_busy(ctsio);
1797 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
1798 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1799 msg_info.hdr.serializing_sc = NULL;
1800 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1801 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1802 sizeof(msg_info.scsi), M_WAITOK);
1806 entry = ctl_get_cmd_entry(ctsio, NULL);
1807 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
1808 mtx_unlock(&lun->lun_lock);
1809 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
1810 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1811 msg_info.hdr.serializing_sc = NULL;
1812 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1813 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1814 sizeof(msg_info.scsi), M_WAITOK);
1818 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
1819 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = lun->be_lun;
1822 * Every I/O goes into the OOA queue for a
1823 * particular LUN, and stays there until completion.
1826 if (TAILQ_EMPTY(&lun->ooa_queue))
1827 lun->idle_time += getsbinuptime() - lun->last_busy;
1829 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1831 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
1832 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
1834 case CTL_ACTION_BLOCK:
1835 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
1836 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
1838 mtx_unlock(&lun->lun_lock);
1840 case CTL_ACTION_PASS:
1841 case CTL_ACTION_SKIP:
1842 if (softc->ha_mode == CTL_HA_MODE_XFER) {
1843 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
1844 ctl_enqueue_rtr((union ctl_io *)ctsio);
1845 mtx_unlock(&lun->lun_lock);
1847 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
1848 mtx_unlock(&lun->lun_lock);
1850 /* send msg back to other side */
1851 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1852 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
1853 msg_info.hdr.msg_type = CTL_MSG_R2R;
1854 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1855 sizeof(msg_info.hdr), M_WAITOK);
1858 case CTL_ACTION_OVERLAP:
1859 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1860 mtx_unlock(&lun->lun_lock);
1863 ctl_set_overlapped_cmd(ctsio);
1864 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
1865 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1866 msg_info.hdr.serializing_sc = NULL;
1867 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1868 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1869 sizeof(msg_info.scsi), M_WAITOK);
1871 case CTL_ACTION_OVERLAP_TAG:
1872 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1873 mtx_unlock(&lun->lun_lock);
1875 ctl_set_overlapped_tag(ctsio, ctsio->tag_num);
1876 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
1877 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1878 msg_info.hdr.serializing_sc = NULL;
1879 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1880 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1881 sizeof(msg_info.scsi), M_WAITOK);
1883 case CTL_ACTION_ERROR:
1885 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1886 mtx_unlock(&lun->lun_lock);
1889 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
1891 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
1892 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1893 msg_info.hdr.serializing_sc = NULL;
1894 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1895 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1896 sizeof(msg_info.scsi), M_WAITOK);
1903 * Returns 0 for success, errno for failure.
1906 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
1907 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
1914 mtx_lock(&lun->lun_lock);
1915 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
1916 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
1918 struct ctl_ooa_entry *entry;
1921 * If we've got more than we can fit, just count the
1922 * remaining entries.
1924 if (*cur_fill_num >= ooa_hdr->alloc_num)
1927 entry = &kern_entries[*cur_fill_num];
1929 entry->tag_num = io->scsiio.tag_num;
1930 entry->lun_num = lun->lun;
1932 entry->start_bt = io->io_hdr.start_bt;
1934 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
1935 entry->cdb_len = io->scsiio.cdb_len;
1936 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
1937 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
1939 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
1940 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
1942 if (io->io_hdr.flags & CTL_FLAG_ABORT)
1943 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
1945 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
1946 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
1948 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
1949 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
1951 mtx_unlock(&lun->lun_lock);
1957 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
1958 size_t error_str_len)
1962 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
1964 if (copyin(user_addr, kptr, len) != 0) {
1965 snprintf(error_str, error_str_len, "Error copying %d bytes "
1966 "from user address %p to kernel address %p", len,
1976 ctl_free_args(int num_args, struct ctl_be_arg *args)
1983 for (i = 0; i < num_args; i++) {
1984 free(args[i].kname, M_CTL);
1985 free(args[i].kvalue, M_CTL);
1991 static struct ctl_be_arg *
1992 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
1993 char *error_str, size_t error_str_len)
1995 struct ctl_be_arg *args;
1998 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
1999 error_str, error_str_len);
2004 for (i = 0; i < num_args; i++) {
2005 args[i].kname = NULL;
2006 args[i].kvalue = NULL;
2009 for (i = 0; i < num_args; i++) {
2012 args[i].kname = ctl_copyin_alloc(args[i].name,
2013 args[i].namelen, error_str, error_str_len);
2014 if (args[i].kname == NULL)
2017 if (args[i].kname[args[i].namelen - 1] != '\0') {
2018 snprintf(error_str, error_str_len, "Argument %d "
2019 "name is not NUL-terminated", i);
2023 if (args[i].flags & CTL_BEARG_RD) {
2024 tmpptr = ctl_copyin_alloc(args[i].value,
2025 args[i].vallen, error_str, error_str_len);
2028 if ((args[i].flags & CTL_BEARG_ASCII)
2029 && (tmpptr[args[i].vallen - 1] != '\0')) {
2030 snprintf(error_str, error_str_len, "Argument "
2031 "%d value is not NUL-terminated", i);
2034 args[i].kvalue = tmpptr;
2036 args[i].kvalue = malloc(args[i].vallen,
2037 M_CTL, M_WAITOK | M_ZERO);
2044 ctl_free_args(num_args, args);
2050 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2054 for (i = 0; i < num_args; i++) {
2055 if (args[i].flags & CTL_BEARG_WR)
2056 copyout(args[i].kvalue, args[i].value, args[i].vallen);
2061 * Escape characters that are illegal or not recommended in XML.
2064 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2066 char *end = str + size;
2071 for (; *str && str < end; str++) {
2074 retval = sbuf_printf(sb, "&");
2077 retval = sbuf_printf(sb, ">");
2080 retval = sbuf_printf(sb, "<");
2083 retval = sbuf_putc(sb, *str);
2096 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2098 struct scsi_vpd_id_descriptor *desc;
2101 if (id == NULL || id->len < 4)
2103 desc = (struct scsi_vpd_id_descriptor *)id->data;
2104 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2105 case SVPD_ID_TYPE_T10:
2106 sbuf_printf(sb, "t10.");
2108 case SVPD_ID_TYPE_EUI64:
2109 sbuf_printf(sb, "eui.");
2111 case SVPD_ID_TYPE_NAA:
2112 sbuf_printf(sb, "naa.");
2114 case SVPD_ID_TYPE_SCSI_NAME:
2117 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2118 case SVPD_ID_CODESET_BINARY:
2119 for (i = 0; i < desc->length; i++)
2120 sbuf_printf(sb, "%02x", desc->identifier[i]);
2122 case SVPD_ID_CODESET_ASCII:
2123 sbuf_printf(sb, "%.*s", (int)desc->length,
2124 (char *)desc->identifier);
2126 case SVPD_ID_CODESET_UTF8:
2127 sbuf_printf(sb, "%s", (char *)desc->identifier);
2133 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2136 struct ctl_softc *softc;
2137 struct ctl_lun *lun;
2140 softc = control_softc;
2146 retval = ctl_ioctl_io(dev, cmd, addr, flag, td);
2148 case CTL_ENABLE_PORT:
2149 case CTL_DISABLE_PORT:
2150 case CTL_SET_PORT_WWNS: {
2151 struct ctl_port *port;
2152 struct ctl_port_entry *entry;
2154 entry = (struct ctl_port_entry *)addr;
2156 mtx_lock(&softc->ctl_lock);
2157 STAILQ_FOREACH(port, &softc->port_list, links) {
2160 if (port->targ_port < softc->port_min ||
2161 port->targ_port >= softc->port_max)
2166 if ((entry->port_type == CTL_PORT_NONE)
2167 && (entry->targ_port == port->targ_port)) {
2169 * If the user only wants to enable or
2170 * disable or set WWNs on a specific port,
2171 * do the operation and we're done.
2175 } else if (entry->port_type & port->port_type) {
2177 * Compare the user's type mask with the
2178 * particular frontend type to see if we
2185 * Make sure the user isn't trying to set
2186 * WWNs on multiple ports at the same time.
2188 if (cmd == CTL_SET_PORT_WWNS) {
2189 printf("%s: Can't set WWNs on "
2190 "multiple ports\n", __func__);
2199 * XXX KDM we have to drop the lock here, because
2200 * the online/offline operations can potentially
2201 * block. We need to reference count the frontends
2202 * so they can't go away,
2204 if (cmd == CTL_ENABLE_PORT) {
2205 mtx_unlock(&softc->ctl_lock);
2206 ctl_port_online(port);
2207 mtx_lock(&softc->ctl_lock);
2208 } else if (cmd == CTL_DISABLE_PORT) {
2209 mtx_unlock(&softc->ctl_lock);
2210 ctl_port_offline(port);
2211 mtx_lock(&softc->ctl_lock);
2212 } else if (cmd == CTL_SET_PORT_WWNS) {
2213 ctl_port_set_wwns(port,
2214 (entry->flags & CTL_PORT_WWNN_VALID) ?
2216 (entry->flags & CTL_PORT_WWPN_VALID) ?
2217 1 : 0, entry->wwpn);
2222 mtx_unlock(&softc->ctl_lock);
2225 case CTL_GET_PORT_LIST: {
2226 struct ctl_port *port;
2227 struct ctl_port_list *list;
2230 list = (struct ctl_port_list *)addr;
2232 if (list->alloc_len != (list->alloc_num *
2233 sizeof(struct ctl_port_entry))) {
2234 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != "
2235 "alloc_num %u * sizeof(struct ctl_port_entry) "
2236 "%zu\n", __func__, list->alloc_len,
2237 list->alloc_num, sizeof(struct ctl_port_entry));
2243 list->dropped_num = 0;
2245 mtx_lock(&softc->ctl_lock);
2246 STAILQ_FOREACH(port, &softc->port_list, links) {
2247 struct ctl_port_entry entry, *list_entry;
2249 if (list->fill_num >= list->alloc_num) {
2250 list->dropped_num++;
2254 entry.port_type = port->port_type;
2255 strlcpy(entry.port_name, port->port_name,
2256 sizeof(entry.port_name));
2257 entry.targ_port = port->targ_port;
2258 entry.physical_port = port->physical_port;
2259 entry.virtual_port = port->virtual_port;
2260 entry.wwnn = port->wwnn;
2261 entry.wwpn = port->wwpn;
2262 if (port->status & CTL_PORT_STATUS_ONLINE)
2267 list_entry = &list->entries[i];
2269 retval = copyout(&entry, list_entry, sizeof(entry));
2271 printf("%s: CTL_GET_PORT_LIST: copyout "
2272 "returned %d\n", __func__, retval);
2277 list->fill_len += sizeof(entry);
2279 mtx_unlock(&softc->ctl_lock);
2282 * If this is non-zero, we had a copyout fault, so there's
2283 * probably no point in attempting to set the status inside
2289 if (list->dropped_num > 0)
2290 list->status = CTL_PORT_LIST_NEED_MORE_SPACE;
2292 list->status = CTL_PORT_LIST_OK;
2295 case CTL_DUMP_OOA: {
2300 mtx_lock(&softc->ctl_lock);
2301 printf("Dumping OOA queues:\n");
2302 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2303 mtx_lock(&lun->lun_lock);
2304 for (io = (union ctl_io *)TAILQ_FIRST(
2305 &lun->ooa_queue); io != NULL;
2306 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2308 sbuf_new(&sb, printbuf, sizeof(printbuf),
2310 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ",
2314 CTL_FLAG_BLOCKED) ? "" : " BLOCKED",
2316 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
2318 CTL_FLAG_ABORT) ? " ABORT" : "",
2320 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : "");
2321 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
2323 printf("%s\n", sbuf_data(&sb));
2325 mtx_unlock(&lun->lun_lock);
2327 printf("OOA queues dump done\n");
2328 mtx_unlock(&softc->ctl_lock);
2332 struct ctl_ooa *ooa_hdr;
2333 struct ctl_ooa_entry *entries;
2334 uint32_t cur_fill_num;
2336 ooa_hdr = (struct ctl_ooa *)addr;
2338 if ((ooa_hdr->alloc_len == 0)
2339 || (ooa_hdr->alloc_num == 0)) {
2340 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2341 "must be non-zero\n", __func__,
2342 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2347 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2348 sizeof(struct ctl_ooa_entry))) {
2349 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2350 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2351 __func__, ooa_hdr->alloc_len,
2352 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2357 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2358 if (entries == NULL) {
2359 printf("%s: could not allocate %d bytes for OOA "
2360 "dump\n", __func__, ooa_hdr->alloc_len);
2365 mtx_lock(&softc->ctl_lock);
2366 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2367 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS)
2368 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2369 mtx_unlock(&softc->ctl_lock);
2370 free(entries, M_CTL);
2371 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2372 __func__, (uintmax_t)ooa_hdr->lun_num);
2379 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2380 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2381 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2387 mtx_unlock(&softc->ctl_lock);
2388 free(entries, M_CTL);
2392 lun = softc->ctl_luns[ooa_hdr->lun_num];
2394 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr,
2397 mtx_unlock(&softc->ctl_lock);
2399 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2400 ooa_hdr->fill_len = ooa_hdr->fill_num *
2401 sizeof(struct ctl_ooa_entry);
2402 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2404 printf("%s: error copying out %d bytes for OOA dump\n",
2405 __func__, ooa_hdr->fill_len);
2408 getbintime(&ooa_hdr->cur_bt);
2410 if (cur_fill_num > ooa_hdr->alloc_num) {
2411 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2412 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2414 ooa_hdr->dropped_num = 0;
2415 ooa_hdr->status = CTL_OOA_OK;
2418 free(entries, M_CTL);
2421 case CTL_CHECK_OOA: {
2423 struct ctl_ooa_info *ooa_info;
2426 ooa_info = (struct ctl_ooa_info *)addr;
2428 if (ooa_info->lun_id >= CTL_MAX_LUNS) {
2429 ooa_info->status = CTL_OOA_INVALID_LUN;
2432 mtx_lock(&softc->ctl_lock);
2433 lun = softc->ctl_luns[ooa_info->lun_id];
2435 mtx_unlock(&softc->ctl_lock);
2436 ooa_info->status = CTL_OOA_INVALID_LUN;
2439 mtx_lock(&lun->lun_lock);
2440 mtx_unlock(&softc->ctl_lock);
2441 ooa_info->num_entries = 0;
2442 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
2443 io != NULL; io = (union ctl_io *)TAILQ_NEXT(
2444 &io->io_hdr, ooa_links)) {
2445 ooa_info->num_entries++;
2447 mtx_unlock(&lun->lun_lock);
2449 ooa_info->status = CTL_OOA_SUCCESS;
2453 case CTL_DELAY_IO: {
2454 struct ctl_io_delay_info *delay_info;
2456 delay_info = (struct ctl_io_delay_info *)addr;
2459 mtx_lock(&softc->ctl_lock);
2461 if ((delay_info->lun_id >= CTL_MAX_LUNS)
2462 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2463 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2465 lun = softc->ctl_luns[delay_info->lun_id];
2466 mtx_lock(&lun->lun_lock);
2468 delay_info->status = CTL_DELAY_STATUS_OK;
2470 switch (delay_info->delay_type) {
2471 case CTL_DELAY_TYPE_CONT:
2473 case CTL_DELAY_TYPE_ONESHOT:
2476 delay_info->status =
2477 CTL_DELAY_STATUS_INVALID_TYPE;
2481 switch (delay_info->delay_loc) {
2482 case CTL_DELAY_LOC_DATAMOVE:
2483 lun->delay_info.datamove_type =
2484 delay_info->delay_type;
2485 lun->delay_info.datamove_delay =
2486 delay_info->delay_secs;
2488 case CTL_DELAY_LOC_DONE:
2489 lun->delay_info.done_type =
2490 delay_info->delay_type;
2491 lun->delay_info.done_delay =
2492 delay_info->delay_secs;
2495 delay_info->status =
2496 CTL_DELAY_STATUS_INVALID_LOC;
2499 mtx_unlock(&lun->lun_lock);
2502 mtx_unlock(&softc->ctl_lock);
2504 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2505 #endif /* CTL_IO_DELAY */
2508 case CTL_REALSYNC_SET: {
2511 syncstate = (int *)addr;
2513 mtx_lock(&softc->ctl_lock);
2514 switch (*syncstate) {
2516 softc->flags &= ~CTL_FLAG_REAL_SYNC;
2519 softc->flags |= CTL_FLAG_REAL_SYNC;
2525 mtx_unlock(&softc->ctl_lock);
2528 case CTL_REALSYNC_GET: {
2531 syncstate = (int*)addr;
2533 mtx_lock(&softc->ctl_lock);
2534 if (softc->flags & CTL_FLAG_REAL_SYNC)
2538 mtx_unlock(&softc->ctl_lock);
2544 struct ctl_sync_info *sync_info;
2546 sync_info = (struct ctl_sync_info *)addr;
2548 mtx_lock(&softc->ctl_lock);
2549 lun = softc->ctl_luns[sync_info->lun_id];
2551 mtx_unlock(&softc->ctl_lock);
2552 sync_info->status = CTL_GS_SYNC_NO_LUN;
2556 * Get or set the sync interval. We're not bounds checking
2557 * in the set case, hopefully the user won't do something
2560 mtx_lock(&lun->lun_lock);
2561 mtx_unlock(&softc->ctl_lock);
2562 if (cmd == CTL_GETSYNC)
2563 sync_info->sync_interval = lun->sync_interval;
2565 lun->sync_interval = sync_info->sync_interval;
2566 mtx_unlock(&lun->lun_lock);
2568 sync_info->status = CTL_GS_SYNC_OK;
2572 case CTL_GETSTATS: {
2573 struct ctl_stats *stats;
2576 stats = (struct ctl_stats *)addr;
2578 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2580 stats->status = CTL_SS_NEED_MORE_SPACE;
2581 stats->num_luns = softc->num_luns;
2585 * XXX KDM no locking here. If the LUN list changes,
2586 * things can blow up.
2588 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
2589 i++, lun = STAILQ_NEXT(lun, links)) {
2590 retval = copyout(&lun->stats, &stats->lun_stats[i],
2591 sizeof(lun->stats));
2595 stats->num_luns = softc->num_luns;
2596 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2598 stats->status = CTL_SS_OK;
2600 stats->flags = CTL_STATS_FLAG_TIME_VALID;
2602 stats->flags = CTL_STATS_FLAG_NONE;
2604 getnanouptime(&stats->timestamp);
2607 case CTL_ERROR_INJECT: {
2608 struct ctl_error_desc *err_desc, *new_err_desc;
2610 err_desc = (struct ctl_error_desc *)addr;
2612 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2614 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2616 mtx_lock(&softc->ctl_lock);
2617 lun = softc->ctl_luns[err_desc->lun_id];
2619 mtx_unlock(&softc->ctl_lock);
2620 free(new_err_desc, M_CTL);
2621 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2622 __func__, (uintmax_t)err_desc->lun_id);
2626 mtx_lock(&lun->lun_lock);
2627 mtx_unlock(&softc->ctl_lock);
2630 * We could do some checking here to verify the validity
2631 * of the request, but given the complexity of error
2632 * injection requests, the checking logic would be fairly
2635 * For now, if the request is invalid, it just won't get
2636 * executed and might get deleted.
2638 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2641 * XXX KDM check to make sure the serial number is unique,
2642 * in case we somehow manage to wrap. That shouldn't
2643 * happen for a very long time, but it's the right thing to
2646 new_err_desc->serial = lun->error_serial;
2647 err_desc->serial = lun->error_serial;
2648 lun->error_serial++;
2650 mtx_unlock(&lun->lun_lock);
2653 case CTL_ERROR_INJECT_DELETE: {
2654 struct ctl_error_desc *delete_desc, *desc, *desc2;
2657 delete_desc = (struct ctl_error_desc *)addr;
2660 mtx_lock(&softc->ctl_lock);
2661 lun = softc->ctl_luns[delete_desc->lun_id];
2663 mtx_unlock(&softc->ctl_lock);
2664 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2665 __func__, (uintmax_t)delete_desc->lun_id);
2669 mtx_lock(&lun->lun_lock);
2670 mtx_unlock(&softc->ctl_lock);
2671 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2672 if (desc->serial != delete_desc->serial)
2675 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2680 mtx_unlock(&lun->lun_lock);
2681 if (delete_done == 0) {
2682 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2683 "error serial %ju on LUN %u\n", __func__,
2684 delete_desc->serial, delete_desc->lun_id);
2690 case CTL_DUMP_STRUCTS: {
2692 struct ctl_port *port;
2693 struct ctl_frontend *fe;
2695 mtx_lock(&softc->ctl_lock);
2696 printf("CTL Persistent Reservation information start:\n");
2697 for (i = 0; i < CTL_MAX_LUNS; i++) {
2698 lun = softc->ctl_luns[i];
2701 || ((lun->flags & CTL_LUN_DISABLED) != 0))
2704 for (j = 0; j < CTL_MAX_PORTS; j++) {
2705 if (lun->pr_keys[j] == NULL)
2707 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2708 if (lun->pr_keys[j][k] == 0)
2710 printf(" LUN %d port %d iid %d key "
2712 (uintmax_t)lun->pr_keys[j][k]);
2716 printf("CTL Persistent Reservation information end\n");
2717 printf("CTL Ports:\n");
2718 STAILQ_FOREACH(port, &softc->port_list, links) {
2719 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
2720 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
2721 port->frontend->name, port->port_type,
2722 port->physical_port, port->virtual_port,
2723 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
2724 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2725 if (port->wwpn_iid[j].in_use == 0 &&
2726 port->wwpn_iid[j].wwpn == 0 &&
2727 port->wwpn_iid[j].name == NULL)
2730 printf(" iid %u use %d WWPN %#jx '%s'\n",
2731 j, port->wwpn_iid[j].in_use,
2732 (uintmax_t)port->wwpn_iid[j].wwpn,
2733 port->wwpn_iid[j].name);
2736 printf("CTL Port information end\n");
2737 mtx_unlock(&softc->ctl_lock);
2739 * XXX KDM calling this without a lock. We'd likely want
2740 * to drop the lock before calling the frontend's dump
2743 printf("CTL Frontends:\n");
2744 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2745 printf(" Frontend '%s'\n", fe->name);
2746 if (fe->fe_dump != NULL)
2749 printf("CTL Frontend information end\n");
2753 struct ctl_lun_req *lun_req;
2754 struct ctl_backend_driver *backend;
2756 lun_req = (struct ctl_lun_req *)addr;
2758 backend = ctl_backend_find(lun_req->backend);
2759 if (backend == NULL) {
2760 lun_req->status = CTL_LUN_ERROR;
2761 snprintf(lun_req->error_str,
2762 sizeof(lun_req->error_str),
2763 "Backend \"%s\" not found.",
2767 if (lun_req->num_be_args > 0) {
2768 lun_req->kern_be_args = ctl_copyin_args(
2769 lun_req->num_be_args,
2772 sizeof(lun_req->error_str));
2773 if (lun_req->kern_be_args == NULL) {
2774 lun_req->status = CTL_LUN_ERROR;
2779 retval = backend->ioctl(dev, cmd, addr, flag, td);
2781 if (lun_req->num_be_args > 0) {
2782 ctl_copyout_args(lun_req->num_be_args,
2783 lun_req->kern_be_args);
2784 ctl_free_args(lun_req->num_be_args,
2785 lun_req->kern_be_args);
2789 case CTL_LUN_LIST: {
2791 struct ctl_lun_list *list;
2792 struct ctl_option *opt;
2794 list = (struct ctl_lun_list *)addr;
2797 * Allocate a fixed length sbuf here, based on the length
2798 * of the user's buffer. We could allocate an auto-extending
2799 * buffer, and then tell the user how much larger our
2800 * amount of data is than his buffer, but that presents
2803 * 1. The sbuf(9) routines use a blocking malloc, and so
2804 * we can't hold a lock while calling them with an
2805 * auto-extending buffer.
2807 * 2. There is not currently a LUN reference counting
2808 * mechanism, outside of outstanding transactions on
2809 * the LUN's OOA queue. So a LUN could go away on us
2810 * while we're getting the LUN number, backend-specific
2811 * information, etc. Thus, given the way things
2812 * currently work, we need to hold the CTL lock while
2813 * grabbing LUN information.
2815 * So, from the user's standpoint, the best thing to do is
2816 * allocate what he thinks is a reasonable buffer length,
2817 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
2818 * double the buffer length and try again. (And repeat
2819 * that until he succeeds.)
2821 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
2823 list->status = CTL_LUN_LIST_ERROR;
2824 snprintf(list->error_str, sizeof(list->error_str),
2825 "Unable to allocate %d bytes for LUN list",
2830 sbuf_printf(sb, "<ctllunlist>\n");
2832 mtx_lock(&softc->ctl_lock);
2833 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2834 mtx_lock(&lun->lun_lock);
2835 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
2836 (uintmax_t)lun->lun);
2839 * Bail out as soon as we see that we've overfilled
2845 retval = sbuf_printf(sb, "\t<backend_type>%s"
2846 "</backend_type>\n",
2847 (lun->backend == NULL) ? "none" :
2848 lun->backend->name);
2853 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
2854 lun->be_lun->lun_type);
2859 if (lun->backend == NULL) {
2860 retval = sbuf_printf(sb, "</lun>\n");
2866 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
2867 (lun->be_lun->maxlba > 0) ?
2868 lun->be_lun->maxlba + 1 : 0);
2873 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
2874 lun->be_lun->blocksize);
2879 retval = sbuf_printf(sb, "\t<serial_number>");
2884 retval = ctl_sbuf_printf_esc(sb,
2885 lun->be_lun->serial_num,
2886 sizeof(lun->be_lun->serial_num));
2891 retval = sbuf_printf(sb, "</serial_number>\n");
2896 retval = sbuf_printf(sb, "\t<device_id>");
2901 retval = ctl_sbuf_printf_esc(sb,
2902 lun->be_lun->device_id,
2903 sizeof(lun->be_lun->device_id));
2908 retval = sbuf_printf(sb, "</device_id>\n");
2913 if (lun->backend->lun_info != NULL) {
2914 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
2918 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
2919 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
2920 opt->name, opt->value, opt->name);
2925 retval = sbuf_printf(sb, "</lun>\n");
2929 mtx_unlock(&lun->lun_lock);
2932 mtx_unlock(&lun->lun_lock);
2933 mtx_unlock(&softc->ctl_lock);
2936 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
2939 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
2940 snprintf(list->error_str, sizeof(list->error_str),
2941 "Out of space, %d bytes is too small",
2948 retval = copyout(sbuf_data(sb), list->lun_xml,
2951 list->fill_len = sbuf_len(sb) + 1;
2952 list->status = CTL_LUN_LIST_OK;
2957 struct ctl_iscsi *ci;
2958 struct ctl_frontend *fe;
2960 ci = (struct ctl_iscsi *)addr;
2962 fe = ctl_frontend_find("iscsi");
2964 ci->status = CTL_ISCSI_ERROR;
2965 snprintf(ci->error_str, sizeof(ci->error_str),
2966 "Frontend \"iscsi\" not found.");
2970 retval = fe->ioctl(dev, cmd, addr, flag, td);
2973 case CTL_PORT_REQ: {
2974 struct ctl_req *req;
2975 struct ctl_frontend *fe;
2977 req = (struct ctl_req *)addr;
2979 fe = ctl_frontend_find(req->driver);
2981 req->status = CTL_LUN_ERROR;
2982 snprintf(req->error_str, sizeof(req->error_str),
2983 "Frontend \"%s\" not found.", req->driver);
2986 if (req->num_args > 0) {
2987 req->kern_args = ctl_copyin_args(req->num_args,
2988 req->args, req->error_str, sizeof(req->error_str));
2989 if (req->kern_args == NULL) {
2990 req->status = CTL_LUN_ERROR;
2996 retval = fe->ioctl(dev, cmd, addr, flag, td);
3000 if (req->num_args > 0) {
3001 ctl_copyout_args(req->num_args, req->kern_args);
3002 ctl_free_args(req->num_args, req->kern_args);
3006 case CTL_PORT_LIST: {
3008 struct ctl_port *port;
3009 struct ctl_lun_list *list;
3010 struct ctl_option *opt;
3014 list = (struct ctl_lun_list *)addr;
3016 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3018 list->status = CTL_LUN_LIST_ERROR;
3019 snprintf(list->error_str, sizeof(list->error_str),
3020 "Unable to allocate %d bytes for LUN list",
3025 sbuf_printf(sb, "<ctlportlist>\n");
3027 mtx_lock(&softc->ctl_lock);
3028 STAILQ_FOREACH(port, &softc->port_list, links) {
3029 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3030 (uintmax_t)port->targ_port);
3033 * Bail out as soon as we see that we've overfilled
3039 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3040 "</frontend_type>\n", port->frontend->name);
3044 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3049 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3050 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3054 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3059 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3060 port->physical_port);
3064 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3065 port->virtual_port);
3069 if (port->target_devid != NULL) {
3070 sbuf_printf(sb, "\t<target>");
3071 ctl_id_sbuf(port->target_devid, sb);
3072 sbuf_printf(sb, "</target>\n");
3075 if (port->port_devid != NULL) {
3076 sbuf_printf(sb, "\t<port>");
3077 ctl_id_sbuf(port->port_devid, sb);
3078 sbuf_printf(sb, "</port>\n");
3081 if (port->port_info != NULL) {
3082 retval = port->port_info(port->onoff_arg, sb);
3086 STAILQ_FOREACH(opt, &port->options, links) {
3087 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3088 opt->name, opt->value, opt->name);
3093 if (port->lun_map != NULL) {
3094 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n");
3095 for (j = 0; j < CTL_MAX_LUNS; j++) {
3096 plun = ctl_lun_map_from_port(port, j);
3097 if (plun >= CTL_MAX_LUNS)
3100 "\t<lun id=\"%u\">%u</lun>\n",
3105 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3106 if (port->wwpn_iid[j].in_use == 0 ||
3107 (port->wwpn_iid[j].wwpn == 0 &&
3108 port->wwpn_iid[j].name == NULL))
3111 if (port->wwpn_iid[j].name != NULL)
3112 retval = sbuf_printf(sb,
3113 "\t<initiator id=\"%u\">%s</initiator>\n",
3114 j, port->wwpn_iid[j].name);
3116 retval = sbuf_printf(sb,
3117 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3118 j, port->wwpn_iid[j].wwpn);
3125 retval = sbuf_printf(sb, "</targ_port>\n");
3129 mtx_unlock(&softc->ctl_lock);
3132 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3135 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3136 snprintf(list->error_str, sizeof(list->error_str),
3137 "Out of space, %d bytes is too small",
3144 retval = copyout(sbuf_data(sb), list->lun_xml,
3147 list->fill_len = sbuf_len(sb) + 1;
3148 list->status = CTL_LUN_LIST_OK;
3153 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr;
3154 struct ctl_port *port;
3156 mtx_lock(&softc->ctl_lock);
3157 if (lm->port < softc->port_min ||
3158 lm->port >= softc->port_max ||
3159 (port = softc->ctl_ports[lm->port]) == NULL) {
3160 mtx_unlock(&softc->ctl_lock);
3163 if (port->status & CTL_PORT_STATUS_ONLINE) {
3164 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3165 if (ctl_lun_map_to_port(port, lun->lun) >=
3168 mtx_lock(&lun->lun_lock);
3169 ctl_est_ua_port(lun, lm->port, -1,
3171 mtx_unlock(&lun->lun_lock);
3174 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps
3175 if (lm->plun < CTL_MAX_LUNS) {
3176 if (lm->lun == UINT32_MAX)
3177 retval = ctl_lun_map_unset(port, lm->plun);
3178 else if (lm->lun < CTL_MAX_LUNS &&
3179 softc->ctl_luns[lm->lun] != NULL)
3180 retval = ctl_lun_map_set(port, lm->plun, lm->lun);
3183 } else if (lm->plun == UINT32_MAX) {
3184 if (lm->lun == UINT32_MAX)
3185 retval = ctl_lun_map_deinit(port);
3187 retval = ctl_lun_map_init(port);
3190 if (port->status & CTL_PORT_STATUS_ONLINE)
3191 ctl_isc_announce_port(port);
3195 /* XXX KDM should we fix this? */
3197 struct ctl_backend_driver *backend;
3204 * We encode the backend type as the ioctl type for backend
3205 * ioctls. So parse it out here, and then search for a
3206 * backend of this type.
3208 type = _IOC_TYPE(cmd);
3210 STAILQ_FOREACH(backend, &softc->be_list, links) {
3211 if (backend->type == type) {
3217 printf("ctl: unknown ioctl command %#lx or backend "
3222 retval = backend->ioctl(dev, cmd, addr, flag, td);
3232 ctl_get_initindex(struct ctl_nexus *nexus)
3234 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3238 ctl_lun_map_init(struct ctl_port *port)
3240 struct ctl_softc *softc = control_softc;
3241 struct ctl_lun *lun;
3244 if (port->lun_map == NULL)
3245 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS,
3247 if (port->lun_map == NULL)
3249 for (i = 0; i < CTL_MAX_LUNS; i++)
3250 port->lun_map[i] = UINT32_MAX;
3251 if (port->status & CTL_PORT_STATUS_ONLINE) {
3252 if (port->lun_disable != NULL) {
3253 STAILQ_FOREACH(lun, &softc->lun_list, links)
3254 port->lun_disable(port->targ_lun_arg, lun->lun);
3256 ctl_isc_announce_port(port);
3262 ctl_lun_map_deinit(struct ctl_port *port)
3264 struct ctl_softc *softc = control_softc;
3265 struct ctl_lun *lun;
3267 if (port->lun_map == NULL)
3269 free(port->lun_map, M_CTL);
3270 port->lun_map = NULL;
3271 if (port->status & CTL_PORT_STATUS_ONLINE) {
3272 if (port->lun_enable != NULL) {
3273 STAILQ_FOREACH(lun, &softc->lun_list, links)
3274 port->lun_enable(port->targ_lun_arg, lun->lun);
3276 ctl_isc_announce_port(port);
3282 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun)
3287 if (port->lun_map == NULL) {
3288 status = ctl_lun_map_init(port);
3292 old = port->lun_map[plun];
3293 port->lun_map[plun] = glun;
3294 if ((port->status & CTL_PORT_STATUS_ONLINE) && old >= CTL_MAX_LUNS) {
3295 if (port->lun_enable != NULL)
3296 port->lun_enable(port->targ_lun_arg, plun);
3297 ctl_isc_announce_port(port);
3303 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun)
3307 if (port->lun_map == NULL)
3309 old = port->lun_map[plun];
3310 port->lun_map[plun] = UINT32_MAX;
3311 if ((port->status & CTL_PORT_STATUS_ONLINE) && old < CTL_MAX_LUNS) {
3312 if (port->lun_disable != NULL)
3313 port->lun_disable(port->targ_lun_arg, plun);
3314 ctl_isc_announce_port(port);
3320 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
3324 return (UINT32_MAX);
3325 if (port->lun_map == NULL || lun_id >= CTL_MAX_LUNS)
3327 return (port->lun_map[lun_id]);
3331 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
3336 return (UINT32_MAX);
3337 if (port->lun_map == NULL)
3339 for (i = 0; i < CTL_MAX_LUNS; i++) {
3340 if (port->lun_map[i] == lun_id)
3343 return (UINT32_MAX);
3346 static struct ctl_port *
3347 ctl_io_port(struct ctl_io_hdr *io_hdr)
3350 return (control_softc->ctl_ports[io_hdr->nexus.targ_port]);
3354 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last)
3358 for (i = first; i < last; i++) {
3359 if ((mask[i / 32] & (1 << (i % 32))) == 0)
3366 ctl_set_mask(uint32_t *mask, uint32_t bit)
3368 uint32_t chunk, piece;
3371 piece = bit % (sizeof(uint32_t) * 8);
3373 if ((mask[chunk] & (1 << piece)) != 0)
3376 mask[chunk] |= (1 << piece);
3382 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3384 uint32_t chunk, piece;
3387 piece = bit % (sizeof(uint32_t) * 8);
3389 if ((mask[chunk] & (1 << piece)) == 0)
3392 mask[chunk] &= ~(1 << piece);
3398 ctl_is_set(uint32_t *mask, uint32_t bit)
3400 uint32_t chunk, piece;
3403 piece = bit % (sizeof(uint32_t) * 8);
3405 if ((mask[chunk] & (1 << piece)) == 0)
3412 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3416 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3419 return (t[residx % CTL_MAX_INIT_PER_PORT]);
3423 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3427 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3430 t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3434 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3439 i = residx/CTL_MAX_INIT_PER_PORT;
3440 if (lun->pr_keys[i] != NULL)
3442 mtx_unlock(&lun->lun_lock);
3443 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3445 mtx_lock(&lun->lun_lock);
3446 if (lun->pr_keys[i] == NULL)
3447 lun->pr_keys[i] = p;
3453 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3457 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3458 KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3459 t[residx % CTL_MAX_INIT_PER_PORT] = key;
3463 * ctl_softc, pool_name, total_ctl_io are passed in.
3464 * npool is passed out.
3467 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3468 uint32_t total_ctl_io, void **npool)
3471 struct ctl_io_pool *pool;
3473 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3478 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3479 pool->ctl_softc = ctl_softc;
3480 pool->zone = uma_zsecond_create(pool->name, NULL,
3481 NULL, NULL, NULL, ctl_softc->io_zone);
3482 /* uma_prealloc(pool->zone, total_ctl_io); */
3486 *npool = ctl_softc->io_zone;
3492 ctl_pool_free(struct ctl_io_pool *pool)
3499 uma_zdestroy(pool->zone);
3505 ctl_alloc_io(void *pool_ref)
3509 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3511 io = uma_zalloc(pool->zone, M_WAITOK);
3513 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK);
3516 io->io_hdr.pool = pool_ref;
3521 ctl_alloc_io_nowait(void *pool_ref)
3525 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3527 io = uma_zalloc(pool->zone, M_NOWAIT);
3529 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT);
3532 io->io_hdr.pool = pool_ref;
3537 ctl_free_io(union ctl_io *io)
3540 struct ctl_io_pool *pool;
3547 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3548 uma_zfree(pool->zone, io);
3550 uma_zfree((uma_zone_t)io->io_hdr.pool, io);
3555 ctl_zero_io(union ctl_io *io)
3563 * May need to preserve linked list pointers at some point too.
3565 pool_ref = io->io_hdr.pool;
3566 memset(io, 0, sizeof(*io));
3567 io->io_hdr.pool = pool_ref;
3571 * This routine is currently used for internal copies of ctl_ios that need
3572 * to persist for some reason after we've already returned status to the
3573 * FETD. (Thus the flag set.)
3576 * Note that this makes a blind copy of all fields in the ctl_io, except
3577 * for the pool reference. This includes any memory that has been
3578 * allocated! That memory will no longer be valid after done has been
3579 * called, so this would be VERY DANGEROUS for command that actually does
3580 * any reads or writes. Right now (11/7/2005), this is only used for immediate
3581 * start and stop commands, which don't transfer any data, so this is not a
3582 * problem. If it is used for anything else, the caller would also need to
3583 * allocate data buffer space and this routine would need to be modified to
3584 * copy the data buffer(s) as well.
3587 ctl_copy_io(union ctl_io *src, union ctl_io *dest)
3596 * May need to preserve linked list pointers at some point too.
3598 pool_ref = dest->io_hdr.pool;
3600 memcpy(dest, src, MIN(sizeof(*src), sizeof(*dest)));
3602 dest->io_hdr.pool = pool_ref;
3604 * We need to know that this is an internal copy, and doesn't need
3605 * to get passed back to the FETD that allocated it.
3607 dest->io_hdr.flags |= CTL_FLAG_INT_COPY;
3611 ctl_expand_number(const char *buf, uint64_t *num)
3617 number = strtoq(buf, &endptr, 0);
3619 switch (tolower((unsigned char)*endptr)) {
3639 case '\0': /* No unit. */
3643 /* Unrecognized unit. */
3647 if ((number << shift) >> shift != number) {
3651 *num = number << shift;
3657 * This routine could be used in the future to load default and/or saved
3658 * mode page parameters for a particuar lun.
3661 ctl_init_page_index(struct ctl_lun *lun)
3664 struct ctl_page_index *page_index;
3668 memcpy(&lun->mode_pages.index, page_index_template,
3669 sizeof(page_index_template));
3671 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3673 page_index = &lun->mode_pages.index[i];
3675 * If this is a disk-only mode page, there's no point in
3676 * setting it up. For some pages, we have to have some
3677 * basic information about the disk in order to calculate the
3680 if ((lun->be_lun->lun_type != T_DIRECT)
3681 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
3684 switch (page_index->page_code & SMPH_PC_MASK) {
3685 case SMS_RW_ERROR_RECOVERY_PAGE: {
3686 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3687 panic("subpage is incorrect!");
3688 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
3689 &rw_er_page_default,
3690 sizeof(rw_er_page_default));
3691 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
3692 &rw_er_page_changeable,
3693 sizeof(rw_er_page_changeable));
3694 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
3695 &rw_er_page_default,
3696 sizeof(rw_er_page_default));
3697 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
3698 &rw_er_page_default,
3699 sizeof(rw_er_page_default));
3700 page_index->page_data =
3701 (uint8_t *)lun->mode_pages.rw_er_page;
3704 case SMS_FORMAT_DEVICE_PAGE: {
3705 struct scsi_format_page *format_page;
3707 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3708 panic("subpage is incorrect!");
3711 * Sectors per track are set above. Bytes per
3712 * sector need to be set here on a per-LUN basis.
3714 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
3715 &format_page_default,
3716 sizeof(format_page_default));
3717 memcpy(&lun->mode_pages.format_page[
3718 CTL_PAGE_CHANGEABLE], &format_page_changeable,
3719 sizeof(format_page_changeable));
3720 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
3721 &format_page_default,
3722 sizeof(format_page_default));
3723 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
3724 &format_page_default,
3725 sizeof(format_page_default));
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 format_page = &lun->mode_pages.format_page[
3739 scsi_ulto2b(lun->be_lun->blocksize,
3740 format_page->bytes_per_sector);
3742 page_index->page_data =
3743 (uint8_t *)lun->mode_pages.format_page;
3746 case SMS_RIGID_DISK_PAGE: {
3747 struct scsi_rigid_disk_page *rigid_disk_page;
3748 uint32_t sectors_per_cylinder;
3752 #endif /* !__XSCALE__ */
3754 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3755 panic("invalid subpage value %d",
3756 page_index->subpage);
3759 * Rotation rate and sectors per track are set
3760 * above. We calculate the cylinders here based on
3761 * capacity. Due to the number of heads and
3762 * sectors per track we're using, smaller arrays
3763 * may turn out to have 0 cylinders. Linux and
3764 * FreeBSD don't pay attention to these mode pages
3765 * to figure out capacity, but Solaris does. It
3766 * seems to deal with 0 cylinders just fine, and
3767 * works out a fake geometry based on the capacity.
3769 memcpy(&lun->mode_pages.rigid_disk_page[
3770 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
3771 sizeof(rigid_disk_page_default));
3772 memcpy(&lun->mode_pages.rigid_disk_page[
3773 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
3774 sizeof(rigid_disk_page_changeable));
3776 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
3780 * The divide method here will be more accurate,
3781 * probably, but results in floating point being
3782 * used in the kernel on i386 (__udivdi3()). On the
3783 * XScale, though, __udivdi3() is implemented in
3786 * The shift method for cylinder calculation is
3787 * accurate if sectors_per_cylinder is a power of
3788 * 2. Otherwise it might be slightly off -- you
3789 * might have a bit of a truncation problem.
3792 cylinders = (lun->be_lun->maxlba + 1) /
3793 sectors_per_cylinder;
3795 for (shift = 31; shift > 0; shift--) {
3796 if (sectors_per_cylinder & (1 << shift))
3799 cylinders = (lun->be_lun->maxlba + 1) >> shift;
3803 * We've basically got 3 bytes, or 24 bits for the
3804 * cylinder size in the mode page. If we're over,
3805 * just round down to 2^24.
3807 if (cylinders > 0xffffff)
3808 cylinders = 0xffffff;
3810 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
3812 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
3814 if ((value = ctl_get_opt(&lun->be_lun->options,
3816 scsi_ulto2b(strtol(value, NULL, 0),
3817 rigid_disk_page->rotation_rate);
3820 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
3821 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
3822 sizeof(rigid_disk_page_default));
3823 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
3824 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
3825 sizeof(rigid_disk_page_default));
3827 page_index->page_data =
3828 (uint8_t *)lun->mode_pages.rigid_disk_page;
3831 case SMS_CACHING_PAGE: {
3832 struct scsi_caching_page *caching_page;
3834 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3835 panic("invalid subpage value %d",
3836 page_index->subpage);
3837 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
3838 &caching_page_default,
3839 sizeof(caching_page_default));
3840 memcpy(&lun->mode_pages.caching_page[
3841 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
3842 sizeof(caching_page_changeable));
3843 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
3844 &caching_page_default,
3845 sizeof(caching_page_default));
3846 caching_page = &lun->mode_pages.caching_page[
3848 value = ctl_get_opt(&lun->be_lun->options, "writecache");
3849 if (value != NULL && strcmp(value, "off") == 0)
3850 caching_page->flags1 &= ~SCP_WCE;
3851 value = ctl_get_opt(&lun->be_lun->options, "readcache");
3852 if (value != NULL && strcmp(value, "off") == 0)
3853 caching_page->flags1 |= SCP_RCD;
3854 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
3855 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
3856 sizeof(caching_page_default));
3857 page_index->page_data =
3858 (uint8_t *)lun->mode_pages.caching_page;
3861 case SMS_CONTROL_MODE_PAGE: {
3862 struct scsi_control_page *control_page;
3864 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3865 panic("invalid subpage value %d",
3866 page_index->subpage);
3868 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT],
3869 &control_page_default,
3870 sizeof(control_page_default));
3871 memcpy(&lun->mode_pages.control_page[
3872 CTL_PAGE_CHANGEABLE], &control_page_changeable,
3873 sizeof(control_page_changeable));
3874 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED],
3875 &control_page_default,
3876 sizeof(control_page_default));
3877 control_page = &lun->mode_pages.control_page[
3879 value = ctl_get_opt(&lun->be_lun->options, "reordering");
3880 if (value != NULL && strcmp(value, "unrestricted") == 0) {
3881 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK;
3882 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED;
3884 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT],
3885 &lun->mode_pages.control_page[CTL_PAGE_SAVED],
3886 sizeof(control_page_default));
3887 page_index->page_data =
3888 (uint8_t *)lun->mode_pages.control_page;
3892 case SMS_INFO_EXCEPTIONS_PAGE: {
3893 switch (page_index->subpage) {
3894 case SMS_SUBPAGE_PAGE_0:
3895 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
3897 sizeof(ie_page_default));
3898 memcpy(&lun->mode_pages.ie_page[
3899 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
3900 sizeof(ie_page_changeable));
3901 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
3903 sizeof(ie_page_default));
3904 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
3906 sizeof(ie_page_default));
3907 page_index->page_data =
3908 (uint8_t *)lun->mode_pages.ie_page;
3911 struct ctl_logical_block_provisioning_page *page;
3913 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
3915 sizeof(lbp_page_default));
3916 memcpy(&lun->mode_pages.lbp_page[
3917 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
3918 sizeof(lbp_page_changeable));
3919 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
3921 sizeof(lbp_page_default));
3922 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
3923 value = ctl_get_opt(&lun->be_lun->options,
3925 if (value != NULL &&
3926 ctl_expand_number(value, &ival) == 0) {
3927 page->descr[0].flags |= SLBPPD_ENABLED |
3929 if (lun->be_lun->blocksize)
3930 ival /= lun->be_lun->blocksize;
3933 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
3934 page->descr[0].count);
3936 value = ctl_get_opt(&lun->be_lun->options,
3938 if (value != NULL &&
3939 ctl_expand_number(value, &ival) == 0) {
3940 page->descr[1].flags |= SLBPPD_ENABLED |
3942 if (lun->be_lun->blocksize)
3943 ival /= lun->be_lun->blocksize;
3946 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
3947 page->descr[1].count);
3949 value = ctl_get_opt(&lun->be_lun->options,
3950 "pool-avail-threshold");
3951 if (value != NULL &&
3952 ctl_expand_number(value, &ival) == 0) {
3953 page->descr[2].flags |= SLBPPD_ENABLED |
3955 if (lun->be_lun->blocksize)
3956 ival /= lun->be_lun->blocksize;
3959 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
3960 page->descr[2].count);
3962 value = ctl_get_opt(&lun->be_lun->options,
3963 "pool-used-threshold");
3964 if (value != NULL &&
3965 ctl_expand_number(value, &ival) == 0) {
3966 page->descr[3].flags |= SLBPPD_ENABLED |
3968 if (lun->be_lun->blocksize)
3969 ival /= lun->be_lun->blocksize;
3972 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
3973 page->descr[3].count);
3975 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
3976 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
3977 sizeof(lbp_page_default));
3978 page_index->page_data =
3979 (uint8_t *)lun->mode_pages.lbp_page;
3983 case SMS_VENDOR_SPECIFIC_PAGE:{
3984 switch (page_index->subpage) {
3985 case DBGCNF_SUBPAGE_CODE: {
3986 struct copan_debugconf_subpage *current_page,
3989 memcpy(&lun->mode_pages.debugconf_subpage[
3991 &debugconf_page_default,
3992 sizeof(debugconf_page_default));
3993 memcpy(&lun->mode_pages.debugconf_subpage[
3994 CTL_PAGE_CHANGEABLE],
3995 &debugconf_page_changeable,
3996 sizeof(debugconf_page_changeable));
3997 memcpy(&lun->mode_pages.debugconf_subpage[
3999 &debugconf_page_default,
4000 sizeof(debugconf_page_default));
4001 memcpy(&lun->mode_pages.debugconf_subpage[
4003 &debugconf_page_default,
4004 sizeof(debugconf_page_default));
4005 page_index->page_data =
4006 (uint8_t *)lun->mode_pages.debugconf_subpage;
4008 current_page = (struct copan_debugconf_subpage *)
4009 (page_index->page_data +
4010 (page_index->page_len *
4012 saved_page = (struct copan_debugconf_subpage *)
4013 (page_index->page_data +
4014 (page_index->page_len *
4019 panic("invalid subpage value %d",
4020 page_index->subpage);
4026 panic("invalid page value %d",
4027 page_index->page_code & SMPH_PC_MASK);
4032 return (CTL_RETVAL_COMPLETE);
4036 ctl_init_log_page_index(struct ctl_lun *lun)
4038 struct ctl_page_index *page_index;
4041 memcpy(&lun->log_pages.index, log_page_index_template,
4042 sizeof(log_page_index_template));
4045 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4047 page_index = &lun->log_pages.index[i];
4049 * If this is a disk-only mode page, there's no point in
4050 * setting it up. For some pages, we have to have some
4051 * basic information about the disk in order to calculate the
4054 if ((lun->be_lun->lun_type != T_DIRECT)
4055 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
4058 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4059 lun->backend->lun_attr == NULL)
4062 if (page_index->page_code != prev) {
4063 lun->log_pages.pages_page[j] = page_index->page_code;
4064 prev = page_index->page_code;
4067 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4068 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4071 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4072 lun->log_pages.index[0].page_len = j;
4073 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4074 lun->log_pages.index[1].page_len = k * 2;
4075 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4076 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4077 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page;
4078 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page);
4080 return (CTL_RETVAL_COMPLETE);
4084 hex2bin(const char *str, uint8_t *buf, int buf_size)
4089 memset(buf, 0, buf_size);
4090 while (isspace(str[0]))
4092 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4095 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4099 else if (isalpha(c))
4100 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4106 buf[i / 2] |= (c << 4);
4110 return ((i + 1) / 2);
4117 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4118 * wants us to allocate the LUN and he can block.
4119 * - ctl_softc is always set
4120 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4122 * Returns 0 for success, non-zero (errno) for failure.
4125 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4126 struct ctl_be_lun *const be_lun)
4128 struct ctl_lun *nlun, *lun;
4129 struct scsi_vpd_id_descriptor *desc;
4130 struct scsi_vpd_id_t10 *t10id;
4131 const char *eui, *naa, *scsiname, *vendor;
4132 int lun_number, i, lun_malloced;
4133 int devidlen, idlen1, idlen2 = 0, len;
4139 * We currently only support Direct Access or Processor LUN types.
4141 switch (be_lun->lun_type) {
4149 be_lun->lun_config_status(be_lun->be_lun,
4150 CTL_LUN_CONFIG_FAILURE);
4153 if (ctl_lun == NULL) {
4154 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4161 memset(lun, 0, sizeof(*lun));
4163 lun->flags = CTL_LUN_MALLOCED;
4165 /* Generate LUN ID. */
4166 devidlen = max(CTL_DEVID_MIN_LEN,
4167 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4168 idlen1 = sizeof(*t10id) + devidlen;
4169 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4170 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4171 if (scsiname != NULL) {
4172 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4173 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4175 eui = ctl_get_opt(&be_lun->options, "eui");
4177 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4179 naa = ctl_get_opt(&be_lun->options, "naa");
4181 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4183 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4184 M_CTL, M_WAITOK | M_ZERO);
4185 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4186 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4187 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4188 desc->length = idlen1;
4189 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4190 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4191 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4192 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4194 strncpy(t10id->vendor, vendor,
4195 min(sizeof(t10id->vendor), strlen(vendor)));
4197 strncpy((char *)t10id->vendor_spec_id,
4198 (char *)be_lun->device_id, devidlen);
4199 if (scsiname != NULL) {
4200 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4202 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4203 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4204 SVPD_ID_TYPE_SCSI_NAME;
4205 desc->length = idlen2;
4206 strlcpy(desc->identifier, scsiname, idlen2);
4209 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4211 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4212 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4214 desc->length = hex2bin(eui, desc->identifier, 16);
4215 desc->length = desc->length > 12 ? 16 :
4216 (desc->length > 8 ? 12 : 8);
4217 len -= 16 - desc->length;
4220 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4222 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4223 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4225 desc->length = hex2bin(naa, desc->identifier, 16);
4226 desc->length = desc->length > 8 ? 16 : 8;
4227 len -= 16 - desc->length;
4229 lun->lun_devid->len = len;
4231 mtx_lock(&ctl_softc->ctl_lock);
4233 * See if the caller requested a particular LUN number. If so, see
4234 * if it is available. Otherwise, allocate the first available LUN.
4236 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4237 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4238 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4239 mtx_unlock(&ctl_softc->ctl_lock);
4240 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4241 printf("ctl: requested LUN ID %d is higher "
4242 "than CTL_MAX_LUNS - 1 (%d)\n",
4243 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4246 * XXX KDM return an error, or just assign
4247 * another LUN ID in this case??
4249 printf("ctl: requested LUN ID %d is already "
4250 "in use\n", be_lun->req_lun_id);
4252 if (lun->flags & CTL_LUN_MALLOCED)
4254 be_lun->lun_config_status(be_lun->be_lun,
4255 CTL_LUN_CONFIG_FAILURE);
4258 lun_number = be_lun->req_lun_id;
4260 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, CTL_MAX_LUNS);
4261 if (lun_number == -1) {
4262 mtx_unlock(&ctl_softc->ctl_lock);
4263 printf("ctl: can't allocate LUN, out of LUNs\n");
4264 if (lun->flags & CTL_LUN_MALLOCED)
4266 be_lun->lun_config_status(be_lun->be_lun,
4267 CTL_LUN_CONFIG_FAILURE);
4271 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4273 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4274 lun->lun = lun_number;
4275 lun->be_lun = be_lun;
4277 * The processor LUN is always enabled. Disk LUNs come on line
4278 * disabled, and must be enabled by the backend.
4280 lun->flags |= CTL_LUN_DISABLED;
4281 lun->backend = be_lun->be;
4282 be_lun->ctl_lun = lun;
4283 be_lun->lun_id = lun_number;
4284 atomic_add_int(&be_lun->be->num_luns, 1);
4285 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE)
4286 lun->flags |= CTL_LUN_OFFLINE;
4288 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4289 lun->flags |= CTL_LUN_STOPPED;
4291 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4292 lun->flags |= CTL_LUN_INOPERABLE;
4294 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4295 lun->flags |= CTL_LUN_PRIMARY_SC;
4297 lun->ctl_softc = ctl_softc;
4299 lun->last_busy = getsbinuptime();
4301 TAILQ_INIT(&lun->ooa_queue);
4302 TAILQ_INIT(&lun->blocked_queue);
4303 STAILQ_INIT(&lun->error_list);
4304 ctl_tpc_lun_init(lun);
4307 * Initialize the mode and log page index.
4309 ctl_init_page_index(lun);
4310 ctl_init_log_page_index(lun);
4313 * Now, before we insert this lun on the lun list, set the lun
4314 * inventory changed UA for all other luns.
4316 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4317 mtx_lock(&nlun->lun_lock);
4318 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4319 mtx_unlock(&nlun->lun_lock);
4322 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4324 ctl_softc->ctl_luns[lun_number] = lun;
4326 ctl_softc->num_luns++;
4328 /* Setup statistics gathering */
4329 lun->stats.device_type = be_lun->lun_type;
4330 lun->stats.lun_number = lun_number;
4331 if (lun->stats.device_type == T_DIRECT)
4332 lun->stats.blocksize = be_lun->blocksize;
4334 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4335 for (i = 0;i < CTL_MAX_PORTS;i++)
4336 lun->stats.ports[i].targ_port = i;
4338 mtx_unlock(&ctl_softc->ctl_lock);
4340 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4347 * - LUN has already been marked invalid and any pending I/O has been taken
4351 ctl_free_lun(struct ctl_lun *lun)
4353 struct ctl_softc *softc;
4354 struct ctl_lun *nlun;
4357 softc = lun->ctl_softc;
4359 mtx_assert(&softc->ctl_lock, MA_OWNED);
4361 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4363 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4365 softc->ctl_luns[lun->lun] = NULL;
4367 if (!TAILQ_EMPTY(&lun->ooa_queue))
4368 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4373 * Tell the backend to free resources, if this LUN has a backend.
4375 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4376 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4378 ctl_tpc_lun_shutdown(lun);
4379 mtx_destroy(&lun->lun_lock);
4380 free(lun->lun_devid, M_CTL);
4381 for (i = 0; i < CTL_MAX_PORTS; i++)
4382 free(lun->pending_ua[i], M_CTL);
4383 for (i = 0; i < CTL_MAX_PORTS; i++)
4384 free(lun->pr_keys[i], M_CTL);
4385 free(lun->write_buffer, M_CTL);
4386 if (lun->flags & CTL_LUN_MALLOCED)
4389 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4390 mtx_lock(&nlun->lun_lock);
4391 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4392 mtx_unlock(&nlun->lun_lock);
4399 ctl_create_lun(struct ctl_be_lun *be_lun)
4401 struct ctl_softc *softc;
4403 softc = control_softc;
4406 * ctl_alloc_lun() should handle all potential failure cases.
4408 ctl_alloc_lun(softc, NULL, be_lun);
4412 ctl_add_lun(struct ctl_be_lun *be_lun)
4414 struct ctl_softc *softc = control_softc;
4416 mtx_lock(&softc->ctl_lock);
4417 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links);
4418 mtx_unlock(&softc->ctl_lock);
4419 wakeup(&softc->pending_lun_queue);
4425 ctl_enable_lun(struct ctl_be_lun *be_lun)
4427 struct ctl_softc *softc;
4428 struct ctl_port *port, *nport;
4429 struct ctl_lun *lun;
4432 lun = (struct ctl_lun *)be_lun->ctl_lun;
4433 softc = lun->ctl_softc;
4435 mtx_lock(&softc->ctl_lock);
4436 mtx_lock(&lun->lun_lock);
4437 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4439 * eh? Why did we get called if the LUN is already
4442 mtx_unlock(&lun->lun_lock);
4443 mtx_unlock(&softc->ctl_lock);
4446 lun->flags &= ~CTL_LUN_DISABLED;
4447 mtx_unlock(&lun->lun_lock);
4449 for (port = STAILQ_FIRST(&softc->port_list); port != NULL; port = nport) {
4450 nport = STAILQ_NEXT(port, links);
4451 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4452 port->lun_map != NULL || port->lun_enable == NULL)
4456 * Drop the lock while we call the FETD's enable routine.
4457 * This can lead to a callback into CTL (at least in the
4458 * case of the internal initiator frontend.
4460 mtx_unlock(&softc->ctl_lock);
4461 retval = port->lun_enable(port->targ_lun_arg, lun->lun);
4462 mtx_lock(&softc->ctl_lock);
4464 printf("%s: FETD %s port %d returned error "
4465 "%d for lun_enable on lun %jd\n",
4466 __func__, port->port_name, port->targ_port,
4467 retval, (intmax_t)lun->lun);
4471 mtx_unlock(&softc->ctl_lock);
4472 ctl_isc_announce_lun(lun);
4478 ctl_disable_lun(struct ctl_be_lun *be_lun)
4480 struct ctl_softc *softc;
4481 struct ctl_port *port;
4482 struct ctl_lun *lun;
4485 lun = (struct ctl_lun *)be_lun->ctl_lun;
4486 softc = lun->ctl_softc;
4488 mtx_lock(&softc->ctl_lock);
4489 mtx_lock(&lun->lun_lock);
4490 if (lun->flags & CTL_LUN_DISABLED) {
4491 mtx_unlock(&lun->lun_lock);
4492 mtx_unlock(&softc->ctl_lock);
4495 lun->flags |= CTL_LUN_DISABLED;
4496 mtx_unlock(&lun->lun_lock);
4498 STAILQ_FOREACH(port, &softc->port_list, links) {
4499 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4500 port->lun_map != NULL || port->lun_disable == NULL)
4504 * Drop the lock before we call the frontend's disable
4505 * routine, to avoid lock order reversals.
4507 * XXX KDM what happens if the frontend list changes while
4508 * we're traversing it? It's unlikely, but should be handled.
4510 mtx_unlock(&softc->ctl_lock);
4511 retval = port->lun_disable(port->targ_lun_arg, lun->lun);
4512 mtx_lock(&softc->ctl_lock);
4514 printf("%s: FETD %s port %d returned error "
4515 "%d for lun_disable on lun %jd\n",
4516 __func__, port->port_name, port->targ_port,
4517 retval, (intmax_t)lun->lun);
4521 mtx_unlock(&softc->ctl_lock);
4522 ctl_isc_announce_lun(lun);
4528 ctl_start_lun(struct ctl_be_lun *be_lun)
4530 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4532 mtx_lock(&lun->lun_lock);
4533 lun->flags &= ~CTL_LUN_STOPPED;
4534 mtx_unlock(&lun->lun_lock);
4539 ctl_stop_lun(struct ctl_be_lun *be_lun)
4541 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4543 mtx_lock(&lun->lun_lock);
4544 lun->flags |= CTL_LUN_STOPPED;
4545 mtx_unlock(&lun->lun_lock);
4550 ctl_lun_offline(struct ctl_be_lun *be_lun)
4552 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4554 mtx_lock(&lun->lun_lock);
4555 lun->flags |= CTL_LUN_OFFLINE;
4556 mtx_unlock(&lun->lun_lock);
4561 ctl_lun_online(struct ctl_be_lun *be_lun)
4563 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4565 mtx_lock(&lun->lun_lock);
4566 lun->flags &= ~CTL_LUN_OFFLINE;
4567 mtx_unlock(&lun->lun_lock);
4572 ctl_lun_primary(struct ctl_be_lun *be_lun)
4574 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4576 mtx_lock(&lun->lun_lock);
4577 lun->flags |= CTL_LUN_PRIMARY_SC;
4578 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4579 mtx_unlock(&lun->lun_lock);
4580 ctl_isc_announce_lun(lun);
4585 ctl_lun_secondary(struct ctl_be_lun *be_lun)
4587 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4589 mtx_lock(&lun->lun_lock);
4590 lun->flags &= ~CTL_LUN_PRIMARY_SC;
4591 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4592 mtx_unlock(&lun->lun_lock);
4593 ctl_isc_announce_lun(lun);
4598 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4600 struct ctl_softc *softc;
4601 struct ctl_lun *lun;
4603 lun = (struct ctl_lun *)be_lun->ctl_lun;
4604 softc = lun->ctl_softc;
4606 mtx_lock(&lun->lun_lock);
4609 * The LUN needs to be disabled before it can be marked invalid.
4611 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4612 mtx_unlock(&lun->lun_lock);
4616 * Mark the LUN invalid.
4618 lun->flags |= CTL_LUN_INVALID;
4621 * If there is nothing in the OOA queue, go ahead and free the LUN.
4622 * If we have something in the OOA queue, we'll free it when the
4623 * last I/O completes.
4625 if (TAILQ_EMPTY(&lun->ooa_queue)) {
4626 mtx_unlock(&lun->lun_lock);
4627 mtx_lock(&softc->ctl_lock);
4629 mtx_unlock(&softc->ctl_lock);
4631 mtx_unlock(&lun->lun_lock);
4637 ctl_lun_inoperable(struct ctl_be_lun *be_lun)
4639 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4641 mtx_lock(&lun->lun_lock);
4642 lun->flags |= CTL_LUN_INOPERABLE;
4643 mtx_unlock(&lun->lun_lock);
4648 ctl_lun_operable(struct ctl_be_lun *be_lun)
4650 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4652 mtx_lock(&lun->lun_lock);
4653 lun->flags &= ~CTL_LUN_INOPERABLE;
4654 mtx_unlock(&lun->lun_lock);
4659 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
4661 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4662 union ctl_ha_msg msg;
4664 mtx_lock(&lun->lun_lock);
4665 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGED);
4666 mtx_unlock(&lun->lun_lock);
4667 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4668 /* Send msg to other side. */
4669 bzero(&msg.ua, sizeof(msg.ua));
4670 msg.hdr.msg_type = CTL_MSG_UA;
4671 msg.hdr.nexus.initid = -1;
4672 msg.hdr.nexus.targ_port = -1;
4673 msg.hdr.nexus.targ_lun = lun->lun;
4674 msg.hdr.nexus.targ_mapped_lun = lun->lun;
4677 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGED;
4678 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4684 * Backend "memory move is complete" callback for requests that never
4685 * make it down to say RAIDCore's configuration code.
4688 ctl_config_move_done(union ctl_io *io)
4692 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
4693 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
4694 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
4696 if ((io->io_hdr.port_status != 0) &&
4697 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
4698 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
4700 * For hardware error sense keys, the sense key
4701 * specific value is defined to be a retry count,
4702 * but we use it to pass back an internal FETD
4703 * error code. XXX KDM Hopefully the FETD is only
4704 * using 16 bits for an error code, since that's
4705 * all the space we have in the sks field.
4707 ctl_set_internal_failure(&io->scsiio,
4710 io->io_hdr.port_status);
4713 if (ctl_debug & CTL_DEBUG_CDB_DATA)
4715 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
4716 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
4717 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
4718 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
4720 * XXX KDM just assuming a single pointer here, and not a
4721 * S/G list. If we start using S/G lists for config data,
4722 * we'll need to know how to clean them up here as well.
4724 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
4725 free(io->scsiio.kern_data_ptr, M_CTL);
4727 retval = CTL_RETVAL_COMPLETE;
4730 * XXX KDM now we need to continue data movement. Some
4732 * - call ctl_scsiio() again? We don't do this for data
4733 * writes, because for those at least we know ahead of
4734 * time where the write will go and how long it is. For
4735 * config writes, though, that information is largely
4736 * contained within the write itself, thus we need to
4737 * parse out the data again.
4739 * - Call some other function once the data is in?
4743 * XXX KDM call ctl_scsiio() again for now, and check flag
4744 * bits to see whether we're allocated or not.
4746 retval = ctl_scsiio(&io->scsiio);
4752 * This gets called by a backend driver when it is done with a
4753 * data_submit method.
4756 ctl_data_submit_done(union ctl_io *io)
4759 * If the IO_CONT flag is set, we need to call the supplied
4760 * function to continue processing the I/O, instead of completing
4763 * If there is an error, though, we don't want to keep processing.
4764 * Instead, just send status back to the initiator.
4766 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
4767 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
4768 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
4769 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
4770 io->scsiio.io_cont(io);
4777 * This gets called by a backend driver when it is done with a
4778 * configuration write.
4781 ctl_config_write_done(union ctl_io *io)
4786 * If the IO_CONT flag is set, we need to call the supplied
4787 * function to continue processing the I/O, instead of completing
4790 * If there is an error, though, we don't want to keep processing.
4791 * Instead, just send status back to the initiator.
4793 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
4794 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
4795 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
4796 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
4797 io->scsiio.io_cont(io);
4801 * Since a configuration write can be done for commands that actually
4802 * have data allocated, like write buffer, and commands that have
4803 * no data, like start/stop unit, we need to check here.
4805 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
4806 buf = io->scsiio.kern_data_ptr;
4815 ctl_config_read_done(union ctl_io *io)
4820 * If there is some error -- we are done, skip data transfer.
4822 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
4823 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
4824 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
4825 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
4826 buf = io->scsiio.kern_data_ptr;
4836 * If the IO_CONT flag is set, we need to call the supplied
4837 * function to continue processing the I/O, instead of completing
4840 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
4841 io->scsiio.io_cont(io);
4849 * SCSI release command.
4852 ctl_scsi_release(struct ctl_scsiio *ctsio)
4854 int length, longid, thirdparty_id, resv_id;
4855 struct ctl_lun *lun;
4861 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
4863 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
4864 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
4866 switch (ctsio->cdb[0]) {
4868 struct scsi_release_10 *cdb;
4870 cdb = (struct scsi_release_10 *)ctsio->cdb;
4872 if (cdb->byte2 & SR10_LONGID)
4875 thirdparty_id = cdb->thirdparty_id;
4877 resv_id = cdb->resv_id;
4878 length = scsi_2btoul(cdb->length);
4885 * XXX KDM right now, we only support LUN reservation. We don't
4886 * support 3rd party reservations, or extent reservations, which
4887 * might actually need the parameter list. If we've gotten this
4888 * far, we've got a LUN reservation. Anything else got kicked out
4889 * above. So, according to SPC, ignore the length.
4893 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
4895 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
4896 ctsio->kern_data_len = length;
4897 ctsio->kern_total_len = length;
4898 ctsio->kern_data_resid = 0;
4899 ctsio->kern_rel_offset = 0;
4900 ctsio->kern_sg_entries = 0;
4901 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
4902 ctsio->be_move_done = ctl_config_move_done;
4903 ctl_datamove((union ctl_io *)ctsio);
4905 return (CTL_RETVAL_COMPLETE);
4909 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
4911 mtx_lock(&lun->lun_lock);
4914 * According to SPC, it is not an error for an intiator to attempt
4915 * to release a reservation on a LUN that isn't reserved, or that
4916 * is reserved by another initiator. The reservation can only be
4917 * released, though, by the initiator who made it or by one of
4918 * several reset type events.
4920 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
4921 lun->flags &= ~CTL_LUN_RESERVED;
4923 mtx_unlock(&lun->lun_lock);
4925 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
4926 free(ctsio->kern_data_ptr, M_CTL);
4927 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
4930 ctl_set_success(ctsio);
4931 ctl_done((union ctl_io *)ctsio);
4932 return (CTL_RETVAL_COMPLETE);
4936 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
4938 int extent, thirdparty, longid;
4939 int resv_id, length;
4940 uint64_t thirdparty_id;
4941 struct ctl_lun *lun;
4951 CTL_DEBUG_PRINT(("ctl_reserve\n"));
4953 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
4954 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
4956 switch (ctsio->cdb[0]) {
4958 struct scsi_reserve_10 *cdb;
4960 cdb = (struct scsi_reserve_10 *)ctsio->cdb;
4962 if (cdb->byte2 & SR10_LONGID)
4965 thirdparty_id = cdb->thirdparty_id;
4967 resv_id = cdb->resv_id;
4968 length = scsi_2btoul(cdb->length);
4974 * XXX KDM right now, we only support LUN reservation. We don't
4975 * support 3rd party reservations, or extent reservations, which
4976 * might actually need the parameter list. If we've gotten this
4977 * far, we've got a LUN reservation. Anything else got kicked out
4978 * above. So, according to SPC, ignore the length.
4982 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
4984 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
4985 ctsio->kern_data_len = length;
4986 ctsio->kern_total_len = length;
4987 ctsio->kern_data_resid = 0;
4988 ctsio->kern_rel_offset = 0;
4989 ctsio->kern_sg_entries = 0;
4990 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
4991 ctsio->be_move_done = ctl_config_move_done;
4992 ctl_datamove((union ctl_io *)ctsio);
4994 return (CTL_RETVAL_COMPLETE);
4998 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5000 mtx_lock(&lun->lun_lock);
5001 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5002 ctl_set_reservation_conflict(ctsio);
5006 lun->flags |= CTL_LUN_RESERVED;
5007 lun->res_idx = residx;
5009 ctl_set_success(ctsio);
5012 mtx_unlock(&lun->lun_lock);
5014 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5015 free(ctsio->kern_data_ptr, M_CTL);
5016 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5019 ctl_done((union ctl_io *)ctsio);
5020 return (CTL_RETVAL_COMPLETE);
5024 ctl_start_stop(struct ctl_scsiio *ctsio)
5026 struct scsi_start_stop_unit *cdb;
5027 struct ctl_lun *lun;
5030 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5032 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5035 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5039 * We don't support the immediate bit on a stop unit. In order to
5040 * do that, we would need to code up a way to know that a stop is
5041 * pending, and hold off any new commands until it completes, one
5042 * way or another. Then we could accept or reject those commands
5043 * depending on its status. We would almost need to do the reverse
5044 * of what we do below for an immediate start -- return the copy of
5045 * the ctl_io to the FETD with status to send to the host (and to
5046 * free the copy!) and then free the original I/O once the stop
5047 * actually completes. That way, the OOA queue mechanism can work
5048 * to block commands that shouldn't proceed. Another alternative
5049 * would be to put the copy in the queue in place of the original,
5050 * and return the original back to the caller. That could be
5053 if ((cdb->byte2 & SSS_IMMED)
5054 && ((cdb->how & SSS_START) == 0)) {
5055 ctl_set_invalid_field(ctsio,
5061 ctl_done((union ctl_io *)ctsio);
5062 return (CTL_RETVAL_COMPLETE);
5065 if ((lun->flags & CTL_LUN_PR_RESERVED)
5066 && ((cdb->how & SSS_START)==0)) {
5069 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5070 if (ctl_get_prkey(lun, residx) == 0
5071 || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5073 ctl_set_reservation_conflict(ctsio);
5074 ctl_done((union ctl_io *)ctsio);
5075 return (CTL_RETVAL_COMPLETE);
5080 * If there is no backend on this device, we can't start or stop
5081 * it. In theory we shouldn't get any start/stop commands in the
5082 * first place at this level if the LUN doesn't have a backend.
5083 * That should get stopped by the command decode code.
5085 if (lun->backend == NULL) {
5086 ctl_set_invalid_opcode(ctsio);
5087 ctl_done((union ctl_io *)ctsio);
5088 return (CTL_RETVAL_COMPLETE);
5092 * XXX KDM Copan-specific offline behavior.
5093 * Figure out a reasonable way to port this?
5096 mtx_lock(&lun->lun_lock);
5098 if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
5099 && (lun->flags & CTL_LUN_OFFLINE)) {
5101 * If the LUN is offline, and the on/offline bit isn't set,
5102 * reject the start or stop. Otherwise, let it through.
5104 mtx_unlock(&lun->lun_lock);
5105 ctl_set_lun_not_ready(ctsio);
5106 ctl_done((union ctl_io *)ctsio);
5108 mtx_unlock(&lun->lun_lock);
5109 #endif /* NEEDTOPORT */
5111 * This could be a start or a stop when we're online,
5112 * or a stop/offline or start/online. A start or stop when
5113 * we're offline is covered in the case above.
5116 * In the non-immediate case, we send the request to
5117 * the backend and return status to the user when
5120 * In the immediate case, we allocate a new ctl_io
5121 * to hold a copy of the request, and send that to
5122 * the backend. We then set good status on the
5123 * user's request and return it immediately.
5125 if (cdb->byte2 & SSS_IMMED) {
5126 union ctl_io *new_io;
5128 new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5129 ctl_copy_io((union ctl_io *)ctsio, new_io);
5130 retval = lun->backend->config_write(new_io);
5131 ctl_set_success(ctsio);
5132 ctl_done((union ctl_io *)ctsio);
5134 retval = lun->backend->config_write(
5135 (union ctl_io *)ctsio);
5144 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5145 * we don't really do anything with the LBA and length fields if the user
5146 * passes them in. Instead we'll just flush out the cache for the entire
5150 ctl_sync_cache(struct ctl_scsiio *ctsio)
5152 struct ctl_lun *lun;
5153 struct ctl_softc *softc;
5154 struct ctl_lba_len_flags *lbalen;
5155 uint64_t starting_lba;
5156 uint32_t block_count;
5160 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5162 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5163 softc = lun->ctl_softc;
5166 switch (ctsio->cdb[0]) {
5167 case SYNCHRONIZE_CACHE: {
5168 struct scsi_sync_cache *cdb;
5169 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5171 starting_lba = scsi_4btoul(cdb->begin_lba);
5172 block_count = scsi_2btoul(cdb->lb_count);
5176 case SYNCHRONIZE_CACHE_16: {
5177 struct scsi_sync_cache_16 *cdb;
5178 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5180 starting_lba = scsi_8btou64(cdb->begin_lba);
5181 block_count = scsi_4btoul(cdb->lb_count);
5186 ctl_set_invalid_opcode(ctsio);
5187 ctl_done((union ctl_io *)ctsio);
5189 break; /* NOTREACHED */
5193 * We check the LBA and length, but don't do anything with them.
5194 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5195 * get flushed. This check will just help satisfy anyone who wants
5196 * to see an error for an out of range LBA.
5198 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5199 ctl_set_lba_out_of_range(ctsio);
5200 ctl_done((union ctl_io *)ctsio);
5205 * If this LUN has no backend, we can't flush the cache anyway.
5207 if (lun->backend == NULL) {
5208 ctl_set_invalid_opcode(ctsio);
5209 ctl_done((union ctl_io *)ctsio);
5213 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5214 lbalen->lba = starting_lba;
5215 lbalen->len = block_count;
5216 lbalen->flags = byte2;
5219 * Check to see whether we're configured to send the SYNCHRONIZE
5220 * CACHE command directly to the back end.
5222 mtx_lock(&lun->lun_lock);
5223 if ((softc->flags & CTL_FLAG_REAL_SYNC)
5224 && (++(lun->sync_count) >= lun->sync_interval)) {
5225 lun->sync_count = 0;
5226 mtx_unlock(&lun->lun_lock);
5227 retval = lun->backend->config_write((union ctl_io *)ctsio);
5229 mtx_unlock(&lun->lun_lock);
5230 ctl_set_success(ctsio);
5231 ctl_done((union ctl_io *)ctsio);
5240 ctl_format(struct ctl_scsiio *ctsio)
5242 struct scsi_format *cdb;
5243 struct ctl_lun *lun;
5244 int length, defect_list_len;
5246 CTL_DEBUG_PRINT(("ctl_format\n"));
5248 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5250 cdb = (struct scsi_format *)ctsio->cdb;
5253 if (cdb->byte2 & SF_FMTDATA) {
5254 if (cdb->byte2 & SF_LONGLIST)
5255 length = sizeof(struct scsi_format_header_long);
5257 length = sizeof(struct scsi_format_header_short);
5260 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5262 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5263 ctsio->kern_data_len = length;
5264 ctsio->kern_total_len = length;
5265 ctsio->kern_data_resid = 0;
5266 ctsio->kern_rel_offset = 0;
5267 ctsio->kern_sg_entries = 0;
5268 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5269 ctsio->be_move_done = ctl_config_move_done;
5270 ctl_datamove((union ctl_io *)ctsio);
5272 return (CTL_RETVAL_COMPLETE);
5275 defect_list_len = 0;
5277 if (cdb->byte2 & SF_FMTDATA) {
5278 if (cdb->byte2 & SF_LONGLIST) {
5279 struct scsi_format_header_long *header;
5281 header = (struct scsi_format_header_long *)
5282 ctsio->kern_data_ptr;
5284 defect_list_len = scsi_4btoul(header->defect_list_len);
5285 if (defect_list_len != 0) {
5286 ctl_set_invalid_field(ctsio,
5295 struct scsi_format_header_short *header;
5297 header = (struct scsi_format_header_short *)
5298 ctsio->kern_data_ptr;
5300 defect_list_len = scsi_2btoul(header->defect_list_len);
5301 if (defect_list_len != 0) {
5302 ctl_set_invalid_field(ctsio,
5314 * The format command will clear out the "Medium format corrupted"
5315 * status if set by the configuration code. That status is really
5316 * just a way to notify the host that we have lost the media, and
5317 * get them to issue a command that will basically make them think
5318 * they're blowing away the media.
5320 mtx_lock(&lun->lun_lock);
5321 lun->flags &= ~CTL_LUN_INOPERABLE;
5322 mtx_unlock(&lun->lun_lock);
5324 ctl_set_success(ctsio);
5327 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5328 free(ctsio->kern_data_ptr, M_CTL);
5329 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5332 ctl_done((union ctl_io *)ctsio);
5333 return (CTL_RETVAL_COMPLETE);
5337 ctl_read_buffer(struct ctl_scsiio *ctsio)
5339 struct scsi_read_buffer *cdb;
5340 struct ctl_lun *lun;
5341 int buffer_offset, len;
5342 static uint8_t descr[4];
5343 static uint8_t echo_descr[4] = { 0 };
5345 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5347 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5348 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5350 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA &&
5351 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR &&
5352 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) {
5353 ctl_set_invalid_field(ctsio,
5359 ctl_done((union ctl_io *)ctsio);
5360 return (CTL_RETVAL_COMPLETE);
5363 len = scsi_3btoul(cdb->length);
5364 buffer_offset = scsi_3btoul(cdb->offset);
5366 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5367 ctl_set_invalid_field(ctsio,
5373 ctl_done((union ctl_io *)ctsio);
5374 return (CTL_RETVAL_COMPLETE);
5377 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5379 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5380 ctsio->kern_data_ptr = descr;
5381 len = min(len, sizeof(descr));
5382 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5383 ctsio->kern_data_ptr = echo_descr;
5384 len = min(len, sizeof(echo_descr));
5386 if (lun->write_buffer == NULL) {
5387 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5390 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5392 ctsio->kern_data_len = len;
5393 ctsio->kern_total_len = len;
5394 ctsio->kern_data_resid = 0;
5395 ctsio->kern_rel_offset = 0;
5396 ctsio->kern_sg_entries = 0;
5397 ctl_set_success(ctsio);
5398 ctsio->be_move_done = ctl_config_move_done;
5399 ctl_datamove((union ctl_io *)ctsio);
5400 return (CTL_RETVAL_COMPLETE);
5404 ctl_write_buffer(struct ctl_scsiio *ctsio)
5406 struct scsi_write_buffer *cdb;
5407 struct ctl_lun *lun;
5408 int buffer_offset, len;
5410 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5412 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5413 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5415 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5416 ctl_set_invalid_field(ctsio,
5422 ctl_done((union ctl_io *)ctsio);
5423 return (CTL_RETVAL_COMPLETE);
5426 len = scsi_3btoul(cdb->length);
5427 buffer_offset = scsi_3btoul(cdb->offset);
5429 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5430 ctl_set_invalid_field(ctsio,
5436 ctl_done((union ctl_io *)ctsio);
5437 return (CTL_RETVAL_COMPLETE);
5441 * If we've got a kernel request that hasn't been malloced yet,
5442 * malloc it and tell the caller the data buffer is here.
5444 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5445 if (lun->write_buffer == NULL) {
5446 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5449 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5450 ctsio->kern_data_len = len;
5451 ctsio->kern_total_len = len;
5452 ctsio->kern_data_resid = 0;
5453 ctsio->kern_rel_offset = 0;
5454 ctsio->kern_sg_entries = 0;
5455 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5456 ctsio->be_move_done = ctl_config_move_done;
5457 ctl_datamove((union ctl_io *)ctsio);
5459 return (CTL_RETVAL_COMPLETE);
5462 ctl_set_success(ctsio);
5463 ctl_done((union ctl_io *)ctsio);
5464 return (CTL_RETVAL_COMPLETE);
5468 ctl_write_same(struct ctl_scsiio *ctsio)
5470 struct ctl_lun *lun;
5471 struct ctl_lba_len_flags *lbalen;
5473 uint32_t num_blocks;
5477 retval = CTL_RETVAL_COMPLETE;
5479 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5481 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5483 switch (ctsio->cdb[0]) {
5484 case WRITE_SAME_10: {
5485 struct scsi_write_same_10 *cdb;
5487 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5489 lba = scsi_4btoul(cdb->addr);
5490 num_blocks = scsi_2btoul(cdb->length);
5494 case WRITE_SAME_16: {
5495 struct scsi_write_same_16 *cdb;
5497 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5499 lba = scsi_8btou64(cdb->addr);
5500 num_blocks = scsi_4btoul(cdb->length);
5506 * We got a command we don't support. This shouldn't
5507 * happen, commands should be filtered out above us.
5509 ctl_set_invalid_opcode(ctsio);
5510 ctl_done((union ctl_io *)ctsio);
5512 return (CTL_RETVAL_COMPLETE);
5513 break; /* NOTREACHED */
5516 /* NDOB and ANCHOR flags can be used only together with UNMAP */
5517 if ((byte2 & SWS_UNMAP) == 0 &&
5518 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) {
5519 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5520 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5521 ctl_done((union ctl_io *)ctsio);
5522 return (CTL_RETVAL_COMPLETE);
5526 * The first check is to make sure we're in bounds, the second
5527 * check is to catch wrap-around problems. If the lba + num blocks
5528 * is less than the lba, then we've wrapped around and the block
5529 * range is invalid anyway.
5531 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5532 || ((lba + num_blocks) < lba)) {
5533 ctl_set_lba_out_of_range(ctsio);
5534 ctl_done((union ctl_io *)ctsio);
5535 return (CTL_RETVAL_COMPLETE);
5538 /* Zero number of blocks means "to the last logical block" */
5539 if (num_blocks == 0) {
5540 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5541 ctl_set_invalid_field(ctsio,
5547 ctl_done((union ctl_io *)ctsio);
5548 return (CTL_RETVAL_COMPLETE);
5550 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5553 len = lun->be_lun->blocksize;
5556 * If we've got a kernel request that hasn't been malloced yet,
5557 * malloc it and tell the caller the data buffer is here.
5559 if ((byte2 & SWS_NDOB) == 0 &&
5560 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5561 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5562 ctsio->kern_data_len = len;
5563 ctsio->kern_total_len = len;
5564 ctsio->kern_data_resid = 0;
5565 ctsio->kern_rel_offset = 0;
5566 ctsio->kern_sg_entries = 0;
5567 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5568 ctsio->be_move_done = ctl_config_move_done;
5569 ctl_datamove((union ctl_io *)ctsio);
5571 return (CTL_RETVAL_COMPLETE);
5574 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5576 lbalen->len = num_blocks;
5577 lbalen->flags = byte2;
5578 retval = lun->backend->config_write((union ctl_io *)ctsio);
5584 ctl_unmap(struct ctl_scsiio *ctsio)
5586 struct ctl_lun *lun;
5587 struct scsi_unmap *cdb;
5588 struct ctl_ptr_len_flags *ptrlen;
5589 struct scsi_unmap_header *hdr;
5590 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5592 uint32_t num_blocks;
5596 retval = CTL_RETVAL_COMPLETE;
5598 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5600 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5601 cdb = (struct scsi_unmap *)ctsio->cdb;
5603 len = scsi_2btoul(cdb->length);
5607 * If we've got a kernel request that hasn't been malloced yet,
5608 * malloc it and tell the caller the data buffer is here.
5610 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5611 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5612 ctsio->kern_data_len = len;
5613 ctsio->kern_total_len = len;
5614 ctsio->kern_data_resid = 0;
5615 ctsio->kern_rel_offset = 0;
5616 ctsio->kern_sg_entries = 0;
5617 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5618 ctsio->be_move_done = ctl_config_move_done;
5619 ctl_datamove((union ctl_io *)ctsio);
5621 return (CTL_RETVAL_COMPLETE);
5624 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5625 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5626 if (len < sizeof (*hdr) ||
5627 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5628 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5629 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5630 ctl_set_invalid_field(ctsio,
5638 len = scsi_2btoul(hdr->desc_length);
5639 buf = (struct scsi_unmap_desc *)(hdr + 1);
5640 end = buf + len / sizeof(*buf);
5643 for (range = buf; range < end; range++) {
5644 lba = scsi_8btou64(range->lba);
5645 num_blocks = scsi_4btoul(range->length);
5646 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5647 || ((lba + num_blocks) < lba)) {
5648 ctl_set_lba_out_of_range(ctsio);
5649 ctl_done((union ctl_io *)ctsio);
5650 return (CTL_RETVAL_COMPLETE);
5652 if (num_blocks != 0)
5657 * Block backend can not handle zero last range.
5658 * Filter it out and return if there is nothing left.
5660 len = (uint8_t *)endnz - (uint8_t *)buf;
5662 ctl_set_success(ctsio);
5666 mtx_lock(&lun->lun_lock);
5667 ptrlen = (struct ctl_ptr_len_flags *)
5668 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5669 ptrlen->ptr = (void *)buf;
5671 ptrlen->flags = byte2;
5672 ctl_check_blocked(lun);
5673 mtx_unlock(&lun->lun_lock);
5675 retval = lun->backend->config_write((union ctl_io *)ctsio);
5679 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5680 free(ctsio->kern_data_ptr, M_CTL);
5681 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5683 ctl_done((union ctl_io *)ctsio);
5684 return (CTL_RETVAL_COMPLETE);
5688 * Note that this function currently doesn't actually do anything inside
5689 * CTL to enforce things if the DQue bit is turned on.
5691 * Also note that this function can't be used in the default case, because
5692 * the DQue bit isn't set in the changeable mask for the control mode page
5693 * anyway. This is just here as an example for how to implement a page
5694 * handler, and a placeholder in case we want to allow the user to turn
5695 * tagged queueing on and off.
5697 * The D_SENSE bit handling is functional, however, and will turn
5698 * descriptor sense on and off for a given LUN.
5701 ctl_control_page_handler(struct ctl_scsiio *ctsio,
5702 struct ctl_page_index *page_index, uint8_t *page_ptr)
5704 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
5705 struct ctl_lun *lun;
5709 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5710 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5713 user_cp = (struct scsi_control_page *)page_ptr;
5714 current_cp = (struct scsi_control_page *)
5715 (page_index->page_data + (page_index->page_len *
5717 saved_cp = (struct scsi_control_page *)
5718 (page_index->page_data + (page_index->page_len *
5721 mtx_lock(&lun->lun_lock);
5722 if (((current_cp->rlec & SCP_DSENSE) == 0)
5723 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
5725 * Descriptor sense is currently turned off and the user
5726 * wants to turn it on.
5728 current_cp->rlec |= SCP_DSENSE;
5729 saved_cp->rlec |= SCP_DSENSE;
5730 lun->flags |= CTL_LUN_SENSE_DESC;
5732 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
5733 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
5735 * Descriptor sense is currently turned on, and the user
5736 * wants to turn it off.
5738 current_cp->rlec &= ~SCP_DSENSE;
5739 saved_cp->rlec &= ~SCP_DSENSE;
5740 lun->flags &= ~CTL_LUN_SENSE_DESC;
5743 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) !=
5744 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) {
5745 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
5746 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
5747 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
5748 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
5751 if ((current_cp->eca_and_aen & SCP_SWP) !=
5752 (user_cp->eca_and_aen & SCP_SWP)) {
5753 current_cp->eca_and_aen &= ~SCP_SWP;
5754 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
5755 saved_cp->eca_and_aen &= ~SCP_SWP;
5756 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
5760 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5761 mtx_unlock(&lun->lun_lock);
5767 ctl_caching_sp_handler(struct ctl_scsiio *ctsio,
5768 struct ctl_page_index *page_index, uint8_t *page_ptr)
5770 struct scsi_caching_page *current_cp, *saved_cp, *user_cp;
5771 struct ctl_lun *lun;
5775 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5776 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5779 user_cp = (struct scsi_caching_page *)page_ptr;
5780 current_cp = (struct scsi_caching_page *)
5781 (page_index->page_data + (page_index->page_len *
5783 saved_cp = (struct scsi_caching_page *)
5784 (page_index->page_data + (page_index->page_len *
5787 mtx_lock(&lun->lun_lock);
5788 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) !=
5789 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) {
5790 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
5791 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
5792 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
5793 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
5797 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5798 mtx_unlock(&lun->lun_lock);
5804 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
5805 struct ctl_page_index *page_index,
5811 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
5816 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
5817 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
5818 printf("page data:");
5820 printf(" %.2x",page_ptr[i]);
5826 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
5827 struct ctl_page_index *page_index,
5830 struct copan_debugconf_subpage *page;
5832 page = (struct copan_debugconf_subpage *)page_index->page_data +
5833 (page_index->page_len * pc);
5836 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
5837 case SMS_PAGE_CTRL_DEFAULT >> 6:
5838 case SMS_PAGE_CTRL_SAVED >> 6:
5840 * We don't update the changable or default bits for this page.
5843 case SMS_PAGE_CTRL_CURRENT >> 6:
5844 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
5845 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
5849 EPRINT(0, "Invalid PC %d!!", pc);
5850 #endif /* NEEDTOPORT */
5858 ctl_do_mode_select(union ctl_io *io)
5860 struct scsi_mode_page_header *page_header;
5861 struct ctl_page_index *page_index;
5862 struct ctl_scsiio *ctsio;
5863 int control_dev, page_len;
5864 int page_len_offset, page_len_size;
5865 union ctl_modepage_info *modepage_info;
5866 struct ctl_lun *lun;
5867 int *len_left, *len_used;
5870 ctsio = &io->scsiio;
5873 retval = CTL_RETVAL_COMPLETE;
5875 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5877 if (lun->be_lun->lun_type != T_DIRECT)
5882 modepage_info = (union ctl_modepage_info *)
5883 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
5884 len_left = &modepage_info->header.len_left;
5885 len_used = &modepage_info->header.len_used;
5889 page_header = (struct scsi_mode_page_header *)
5890 (ctsio->kern_data_ptr + *len_used);
5892 if (*len_left == 0) {
5893 free(ctsio->kern_data_ptr, M_CTL);
5894 ctl_set_success(ctsio);
5895 ctl_done((union ctl_io *)ctsio);
5896 return (CTL_RETVAL_COMPLETE);
5897 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
5899 free(ctsio->kern_data_ptr, M_CTL);
5900 ctl_set_param_len_error(ctsio);
5901 ctl_done((union ctl_io *)ctsio);
5902 return (CTL_RETVAL_COMPLETE);
5904 } else if ((page_header->page_code & SMPH_SPF)
5905 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
5907 free(ctsio->kern_data_ptr, M_CTL);
5908 ctl_set_param_len_error(ctsio);
5909 ctl_done((union ctl_io *)ctsio);
5910 return (CTL_RETVAL_COMPLETE);
5915 * XXX KDM should we do something with the block descriptor?
5917 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
5919 if ((control_dev != 0)
5920 && (lun->mode_pages.index[i].page_flags &
5921 CTL_PAGE_FLAG_DISK_ONLY))
5924 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
5925 (page_header->page_code & SMPH_PC_MASK))
5929 * If neither page has a subpage code, then we've got a
5932 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
5933 && ((page_header->page_code & SMPH_SPF) == 0)) {
5934 page_index = &lun->mode_pages.index[i];
5935 page_len = page_header->page_length;
5940 * If both pages have subpages, then the subpage numbers
5943 if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
5944 && (page_header->page_code & SMPH_SPF)) {
5945 struct scsi_mode_page_header_sp *sph;
5947 sph = (struct scsi_mode_page_header_sp *)page_header;
5949 if (lun->mode_pages.index[i].subpage ==
5951 page_index = &lun->mode_pages.index[i];
5952 page_len = scsi_2btoul(sph->page_length);
5959 * If we couldn't find the page, or if we don't have a mode select
5960 * handler for it, send back an error to the user.
5962 if ((page_index == NULL)
5963 || (page_index->select_handler == NULL)) {
5964 ctl_set_invalid_field(ctsio,
5967 /*field*/ *len_used,
5970 free(ctsio->kern_data_ptr, M_CTL);
5971 ctl_done((union ctl_io *)ctsio);
5972 return (CTL_RETVAL_COMPLETE);
5975 if (page_index->page_code & SMPH_SPF) {
5976 page_len_offset = 2;
5980 page_len_offset = 1;
5984 * If the length the initiator gives us isn't the one we specify in
5985 * the mode page header, or if they didn't specify enough data in
5986 * the CDB to avoid truncating this page, kick out the request.
5988 if ((page_len != (page_index->page_len - page_len_offset -
5990 || (*len_left < page_index->page_len)) {
5993 ctl_set_invalid_field(ctsio,
5996 /*field*/ *len_used + page_len_offset,
5999 free(ctsio->kern_data_ptr, M_CTL);
6000 ctl_done((union ctl_io *)ctsio);
6001 return (CTL_RETVAL_COMPLETE);
6005 * Run through the mode page, checking to make sure that the bits
6006 * the user changed are actually legal for him to change.
6008 for (i = 0; i < page_index->page_len; i++) {
6009 uint8_t *user_byte, *change_mask, *current_byte;
6013 user_byte = (uint8_t *)page_header + i;
6014 change_mask = page_index->page_data +
6015 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6016 current_byte = page_index->page_data +
6017 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6020 * Check to see whether the user set any bits in this byte
6021 * that he is not allowed to set.
6023 if ((*user_byte & ~(*change_mask)) ==
6024 (*current_byte & ~(*change_mask)))
6028 * Go through bit by bit to determine which one is illegal.
6031 for (j = 7; j >= 0; j--) {
6032 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6033 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6038 ctl_set_invalid_field(ctsio,
6041 /*field*/ *len_used + i,
6044 free(ctsio->kern_data_ptr, M_CTL);
6045 ctl_done((union ctl_io *)ctsio);
6046 return (CTL_RETVAL_COMPLETE);
6050 * Decrement these before we call the page handler, since we may
6051 * end up getting called back one way or another before the handler
6052 * returns to this context.
6054 *len_left -= page_index->page_len;
6055 *len_used += page_index->page_len;
6057 retval = page_index->select_handler(ctsio, page_index,
6058 (uint8_t *)page_header);
6061 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6062 * wait until this queued command completes to finish processing
6063 * the mode page. If it returns anything other than
6064 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6065 * already set the sense information, freed the data pointer, and
6066 * completed the io for us.
6068 if (retval != CTL_RETVAL_COMPLETE)
6069 goto bailout_no_done;
6072 * If the initiator sent us more than one page, parse the next one.
6077 ctl_set_success(ctsio);
6078 free(ctsio->kern_data_ptr, M_CTL);
6079 ctl_done((union ctl_io *)ctsio);
6083 return (CTL_RETVAL_COMPLETE);
6088 ctl_mode_select(struct ctl_scsiio *ctsio)
6090 int param_len, pf, sp;
6091 int header_size, bd_len;
6092 int len_left, len_used;
6093 struct ctl_page_index *page_index;
6094 struct ctl_lun *lun;
6095 int control_dev, page_len;
6096 union ctl_modepage_info *modepage_info;
6108 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6110 if (lun->be_lun->lun_type != T_DIRECT)
6115 switch (ctsio->cdb[0]) {
6116 case MODE_SELECT_6: {
6117 struct scsi_mode_select_6 *cdb;
6119 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6121 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6122 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6124 param_len = cdb->length;
6125 header_size = sizeof(struct scsi_mode_header_6);
6128 case MODE_SELECT_10: {
6129 struct scsi_mode_select_10 *cdb;
6131 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6133 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6134 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6136 param_len = scsi_2btoul(cdb->length);
6137 header_size = sizeof(struct scsi_mode_header_10);
6141 ctl_set_invalid_opcode(ctsio);
6142 ctl_done((union ctl_io *)ctsio);
6143 return (CTL_RETVAL_COMPLETE);
6144 break; /* NOTREACHED */
6149 * "A parameter list length of zero indicates that the Data-Out Buffer
6150 * shall be empty. This condition shall not be considered as an error."
6152 if (param_len == 0) {
6153 ctl_set_success(ctsio);
6154 ctl_done((union ctl_io *)ctsio);
6155 return (CTL_RETVAL_COMPLETE);
6159 * Since we'll hit this the first time through, prior to
6160 * allocation, we don't need to free a data buffer here.
6162 if (param_len < header_size) {
6163 ctl_set_param_len_error(ctsio);
6164 ctl_done((union ctl_io *)ctsio);
6165 return (CTL_RETVAL_COMPLETE);
6169 * Allocate the data buffer and grab the user's data. In theory,
6170 * we shouldn't have to sanity check the parameter list length here
6171 * because the maximum size is 64K. We should be able to malloc
6172 * that much without too many problems.
6174 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6175 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6176 ctsio->kern_data_len = param_len;
6177 ctsio->kern_total_len = param_len;
6178 ctsio->kern_data_resid = 0;
6179 ctsio->kern_rel_offset = 0;
6180 ctsio->kern_sg_entries = 0;
6181 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6182 ctsio->be_move_done = ctl_config_move_done;
6183 ctl_datamove((union ctl_io *)ctsio);
6185 return (CTL_RETVAL_COMPLETE);
6188 switch (ctsio->cdb[0]) {
6189 case MODE_SELECT_6: {
6190 struct scsi_mode_header_6 *mh6;
6192 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6193 bd_len = mh6->blk_desc_len;
6196 case MODE_SELECT_10: {
6197 struct scsi_mode_header_10 *mh10;
6199 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6200 bd_len = scsi_2btoul(mh10->blk_desc_len);
6204 panic("Invalid CDB type %#x", ctsio->cdb[0]);
6208 if (param_len < (header_size + bd_len)) {
6209 free(ctsio->kern_data_ptr, M_CTL);
6210 ctl_set_param_len_error(ctsio);
6211 ctl_done((union ctl_io *)ctsio);
6212 return (CTL_RETVAL_COMPLETE);
6216 * Set the IO_CONT flag, so that if this I/O gets passed to
6217 * ctl_config_write_done(), it'll get passed back to
6218 * ctl_do_mode_select() for further processing, or completion if
6221 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6222 ctsio->io_cont = ctl_do_mode_select;
6224 modepage_info = (union ctl_modepage_info *)
6225 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6227 memset(modepage_info, 0, sizeof(*modepage_info));
6229 len_left = param_len - header_size - bd_len;
6230 len_used = header_size + bd_len;
6232 modepage_info->header.len_left = len_left;
6233 modepage_info->header.len_used = len_used;
6235 return (ctl_do_mode_select((union ctl_io *)ctsio));
6239 ctl_mode_sense(struct ctl_scsiio *ctsio)
6241 struct ctl_lun *lun;
6242 int pc, page_code, dbd, llba, subpage;
6243 int alloc_len, page_len, header_len, total_len;
6244 struct scsi_mode_block_descr *block_desc;
6245 struct ctl_page_index *page_index;
6253 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6255 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6257 if (lun->be_lun->lun_type != T_DIRECT)
6262 switch (ctsio->cdb[0]) {
6263 case MODE_SENSE_6: {
6264 struct scsi_mode_sense_6 *cdb;
6266 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6268 header_len = sizeof(struct scsi_mode_hdr_6);
6269 if (cdb->byte2 & SMS_DBD)
6272 header_len += sizeof(struct scsi_mode_block_descr);
6274 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6275 page_code = cdb->page & SMS_PAGE_CODE;
6276 subpage = cdb->subpage;
6277 alloc_len = cdb->length;
6280 case MODE_SENSE_10: {
6281 struct scsi_mode_sense_10 *cdb;
6283 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6285 header_len = sizeof(struct scsi_mode_hdr_10);
6287 if (cdb->byte2 & SMS_DBD)
6290 header_len += sizeof(struct scsi_mode_block_descr);
6291 if (cdb->byte2 & SMS10_LLBAA)
6293 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6294 page_code = cdb->page & SMS_PAGE_CODE;
6295 subpage = cdb->subpage;
6296 alloc_len = scsi_2btoul(cdb->length);
6300 ctl_set_invalid_opcode(ctsio);
6301 ctl_done((union ctl_io *)ctsio);
6302 return (CTL_RETVAL_COMPLETE);
6303 break; /* NOTREACHED */
6307 * We have to make a first pass through to calculate the size of
6308 * the pages that match the user's query. Then we allocate enough
6309 * memory to hold it, and actually copy the data into the buffer.
6311 switch (page_code) {
6312 case SMS_ALL_PAGES_PAGE: {
6318 * At the moment, values other than 0 and 0xff here are
6319 * reserved according to SPC-3.
6321 if ((subpage != SMS_SUBPAGE_PAGE_0)
6322 && (subpage != SMS_SUBPAGE_ALL)) {
6323 ctl_set_invalid_field(ctsio,
6329 ctl_done((union ctl_io *)ctsio);
6330 return (CTL_RETVAL_COMPLETE);
6333 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6334 if ((control_dev != 0)
6335 && (lun->mode_pages.index[i].page_flags &
6336 CTL_PAGE_FLAG_DISK_ONLY))
6340 * We don't use this subpage if the user didn't
6341 * request all subpages.
6343 if ((lun->mode_pages.index[i].subpage != 0)
6344 && (subpage == SMS_SUBPAGE_PAGE_0))
6348 printf("found page %#x len %d\n",
6349 lun->mode_pages.index[i].page_code &
6351 lun->mode_pages.index[i].page_len);
6353 page_len += lun->mode_pages.index[i].page_len;
6362 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6363 /* Look for the right page code */
6364 if ((lun->mode_pages.index[i].page_code &
6365 SMPH_PC_MASK) != page_code)
6368 /* Look for the right subpage or the subpage wildcard*/
6369 if ((lun->mode_pages.index[i].subpage != subpage)
6370 && (subpage != SMS_SUBPAGE_ALL))
6373 /* Make sure the page is supported for this dev type */
6374 if ((control_dev != 0)
6375 && (lun->mode_pages.index[i].page_flags &
6376 CTL_PAGE_FLAG_DISK_ONLY))
6380 printf("found page %#x len %d\n",
6381 lun->mode_pages.index[i].page_code &
6383 lun->mode_pages.index[i].page_len);
6386 page_len += lun->mode_pages.index[i].page_len;
6389 if (page_len == 0) {
6390 ctl_set_invalid_field(ctsio,
6396 ctl_done((union ctl_io *)ctsio);
6397 return (CTL_RETVAL_COMPLETE);
6403 total_len = header_len + page_len;
6405 printf("header_len = %d, page_len = %d, total_len = %d\n",
6406 header_len, page_len, total_len);
6409 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6410 ctsio->kern_sg_entries = 0;
6411 ctsio->kern_data_resid = 0;
6412 ctsio->kern_rel_offset = 0;
6413 if (total_len < alloc_len) {
6414 ctsio->residual = alloc_len - total_len;
6415 ctsio->kern_data_len = total_len;
6416 ctsio->kern_total_len = total_len;
6418 ctsio->residual = 0;
6419 ctsio->kern_data_len = alloc_len;
6420 ctsio->kern_total_len = alloc_len;
6423 switch (ctsio->cdb[0]) {
6424 case MODE_SENSE_6: {
6425 struct scsi_mode_hdr_6 *header;
6427 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6429 header->datalen = MIN(total_len - 1, 254);
6430 if (control_dev == 0) {
6431 header->dev_specific = 0x10; /* DPOFUA */
6432 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6433 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6434 .eca_and_aen & SCP_SWP) != 0)
6435 header->dev_specific |= 0x80; /* WP */
6438 header->block_descr_len = 0;
6440 header->block_descr_len =
6441 sizeof(struct scsi_mode_block_descr);
6442 block_desc = (struct scsi_mode_block_descr *)&header[1];
6445 case MODE_SENSE_10: {
6446 struct scsi_mode_hdr_10 *header;
6449 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6451 datalen = MIN(total_len - 2, 65533);
6452 scsi_ulto2b(datalen, header->datalen);
6453 if (control_dev == 0) {
6454 header->dev_specific = 0x10; /* DPOFUA */
6455 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6456 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6457 .eca_and_aen & SCP_SWP) != 0)
6458 header->dev_specific |= 0x80; /* WP */
6461 scsi_ulto2b(0, header->block_descr_len);
6463 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6464 header->block_descr_len);
6465 block_desc = (struct scsi_mode_block_descr *)&header[1];
6469 panic("invalid CDB type %#x", ctsio->cdb[0]);
6470 break; /* NOTREACHED */
6474 * If we've got a disk, use its blocksize in the block
6475 * descriptor. Otherwise, just set it to 0.
6478 if (control_dev == 0)
6479 scsi_ulto3b(lun->be_lun->blocksize,
6480 block_desc->block_len);
6482 scsi_ulto3b(0, block_desc->block_len);
6485 switch (page_code) {
6486 case SMS_ALL_PAGES_PAGE: {
6489 data_used = header_len;
6490 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6491 struct ctl_page_index *page_index;
6493 page_index = &lun->mode_pages.index[i];
6495 if ((control_dev != 0)
6496 && (page_index->page_flags &
6497 CTL_PAGE_FLAG_DISK_ONLY))
6501 * We don't use this subpage if the user didn't
6502 * request all subpages. We already checked (above)
6503 * to make sure the user only specified a subpage
6504 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6506 if ((page_index->subpage != 0)
6507 && (subpage == SMS_SUBPAGE_PAGE_0))
6511 * Call the handler, if it exists, to update the
6512 * page to the latest values.
6514 if (page_index->sense_handler != NULL)
6515 page_index->sense_handler(ctsio, page_index,pc);
6517 memcpy(ctsio->kern_data_ptr + data_used,
6518 page_index->page_data +
6519 (page_index->page_len * pc),
6520 page_index->page_len);
6521 data_used += page_index->page_len;
6528 data_used = header_len;
6530 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6531 struct ctl_page_index *page_index;
6533 page_index = &lun->mode_pages.index[i];
6535 /* Look for the right page code */
6536 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6539 /* Look for the right subpage or the subpage wildcard*/
6540 if ((page_index->subpage != subpage)
6541 && (subpage != SMS_SUBPAGE_ALL))
6544 /* Make sure the page is supported for this dev type */
6545 if ((control_dev != 0)
6546 && (page_index->page_flags &
6547 CTL_PAGE_FLAG_DISK_ONLY))
6551 * Call the handler, if it exists, to update the
6552 * page to the latest values.
6554 if (page_index->sense_handler != NULL)
6555 page_index->sense_handler(ctsio, page_index,pc);
6557 memcpy(ctsio->kern_data_ptr + data_used,
6558 page_index->page_data +
6559 (page_index->page_len * pc),
6560 page_index->page_len);
6561 data_used += page_index->page_len;
6567 ctl_set_success(ctsio);
6568 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6569 ctsio->be_move_done = ctl_config_move_done;
6570 ctl_datamove((union ctl_io *)ctsio);
6571 return (CTL_RETVAL_COMPLETE);
6575 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6576 struct ctl_page_index *page_index,
6579 struct ctl_lun *lun;
6580 struct scsi_log_param_header *phdr;
6584 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6585 data = page_index->page_data;
6587 if (lun->backend->lun_attr != NULL &&
6588 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6590 phdr = (struct scsi_log_param_header *)data;
6591 scsi_ulto2b(0x0001, phdr->param_code);
6592 phdr->param_control = SLP_LBIN | SLP_LP;
6593 phdr->param_len = 8;
6594 data = (uint8_t *)(phdr + 1);
6595 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6596 data[4] = 0x02; /* per-pool */
6597 data += phdr->param_len;
6600 if (lun->backend->lun_attr != NULL &&
6601 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6603 phdr = (struct scsi_log_param_header *)data;
6604 scsi_ulto2b(0x0002, phdr->param_code);
6605 phdr->param_control = SLP_LBIN | SLP_LP;
6606 phdr->param_len = 8;
6607 data = (uint8_t *)(phdr + 1);
6608 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6609 data[4] = 0x01; /* per-LUN */
6610 data += phdr->param_len;
6613 if (lun->backend->lun_attr != NULL &&
6614 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6616 phdr = (struct scsi_log_param_header *)data;
6617 scsi_ulto2b(0x00f1, phdr->param_code);
6618 phdr->param_control = SLP_LBIN | SLP_LP;
6619 phdr->param_len = 8;
6620 data = (uint8_t *)(phdr + 1);
6621 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6622 data[4] = 0x02; /* per-pool */
6623 data += phdr->param_len;
6626 if (lun->backend->lun_attr != NULL &&
6627 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6629 phdr = (struct scsi_log_param_header *)data;
6630 scsi_ulto2b(0x00f2, phdr->param_code);
6631 phdr->param_control = SLP_LBIN | SLP_LP;
6632 phdr->param_len = 8;
6633 data = (uint8_t *)(phdr + 1);
6634 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6635 data[4] = 0x02; /* per-pool */
6636 data += phdr->param_len;
6639 page_index->page_len = data - page_index->page_data;
6644 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
6645 struct ctl_page_index *page_index,
6648 struct ctl_lun *lun;
6649 struct stat_page *data;
6650 uint64_t rn, wn, rb, wb;
6651 struct bintime rt, wt;
6654 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6655 data = (struct stat_page *)page_index->page_data;
6657 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
6658 data->sap.hdr.param_control = SLP_LBIN;
6659 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
6660 sizeof(struct scsi_log_param_header);
6661 rn = wn = rb = wb = 0;
6664 for (i = 0; i < CTL_MAX_PORTS; i++) {
6665 rn += lun->stats.ports[i].operations[CTL_STATS_READ];
6666 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE];
6667 rb += lun->stats.ports[i].bytes[CTL_STATS_READ];
6668 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE];
6669 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]);
6670 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]);
6672 scsi_u64to8b(rn, data->sap.read_num);
6673 scsi_u64to8b(wn, data->sap.write_num);
6674 if (lun->stats.blocksize > 0) {
6675 scsi_u64to8b(wb / lun->stats.blocksize,
6676 data->sap.recvieved_lba);
6677 scsi_u64to8b(rb / lun->stats.blocksize,
6678 data->sap.transmitted_lba);
6680 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000),
6681 data->sap.read_int);
6682 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000),
6683 data->sap.write_int);
6684 scsi_u64to8b(0, data->sap.weighted_num);
6685 scsi_u64to8b(0, data->sap.weighted_int);
6686 scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
6687 data->it.hdr.param_control = SLP_LBIN;
6688 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
6689 sizeof(struct scsi_log_param_header);
6691 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
6693 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
6694 data->it.hdr.param_control = SLP_LBIN;
6695 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
6696 sizeof(struct scsi_log_param_header);
6697 scsi_ulto4b(3, data->ti.exponent);
6698 scsi_ulto4b(1, data->ti.integer);
6700 page_index->page_len = sizeof(*data);
6705 ctl_log_sense(struct ctl_scsiio *ctsio)
6707 struct ctl_lun *lun;
6708 int i, pc, page_code, subpage;
6709 int alloc_len, total_len;
6710 struct ctl_page_index *page_index;
6711 struct scsi_log_sense *cdb;
6712 struct scsi_log_header *header;
6714 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6716 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6717 cdb = (struct scsi_log_sense *)ctsio->cdb;
6718 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6719 page_code = cdb->page & SLS_PAGE_CODE;
6720 subpage = cdb->subpage;
6721 alloc_len = scsi_2btoul(cdb->length);
6724 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6725 page_index = &lun->log_pages.index[i];
6727 /* Look for the right page code */
6728 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6731 /* Look for the right subpage or the subpage wildcard*/
6732 if (page_index->subpage != subpage)
6737 if (i >= CTL_NUM_LOG_PAGES) {
6738 ctl_set_invalid_field(ctsio,
6744 ctl_done((union ctl_io *)ctsio);
6745 return (CTL_RETVAL_COMPLETE);
6748 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6750 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6751 ctsio->kern_sg_entries = 0;
6752 ctsio->kern_data_resid = 0;
6753 ctsio->kern_rel_offset = 0;
6754 if (total_len < alloc_len) {
6755 ctsio->residual = alloc_len - total_len;
6756 ctsio->kern_data_len = total_len;
6757 ctsio->kern_total_len = total_len;
6759 ctsio->residual = 0;
6760 ctsio->kern_data_len = alloc_len;
6761 ctsio->kern_total_len = alloc_len;
6764 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
6765 header->page = page_index->page_code;
6766 if (page_index->subpage) {
6767 header->page |= SL_SPF;
6768 header->subpage = page_index->subpage;
6770 scsi_ulto2b(page_index->page_len, header->datalen);
6773 * Call the handler, if it exists, to update the
6774 * page to the latest values.
6776 if (page_index->sense_handler != NULL)
6777 page_index->sense_handler(ctsio, page_index, pc);
6779 memcpy(header + 1, page_index->page_data, page_index->page_len);
6781 ctl_set_success(ctsio);
6782 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6783 ctsio->be_move_done = ctl_config_move_done;
6784 ctl_datamove((union ctl_io *)ctsio);
6785 return (CTL_RETVAL_COMPLETE);
6789 ctl_read_capacity(struct ctl_scsiio *ctsio)
6791 struct scsi_read_capacity *cdb;
6792 struct scsi_read_capacity_data *data;
6793 struct ctl_lun *lun;
6796 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6798 cdb = (struct scsi_read_capacity *)ctsio->cdb;
6800 lba = scsi_4btoul(cdb->addr);
6801 if (((cdb->pmi & SRC_PMI) == 0)
6803 ctl_set_invalid_field(/*ctsio*/ ctsio,
6809 ctl_done((union ctl_io *)ctsio);
6810 return (CTL_RETVAL_COMPLETE);
6813 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6815 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6816 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6817 ctsio->residual = 0;
6818 ctsio->kern_data_len = sizeof(*data);
6819 ctsio->kern_total_len = sizeof(*data);
6820 ctsio->kern_data_resid = 0;
6821 ctsio->kern_rel_offset = 0;
6822 ctsio->kern_sg_entries = 0;
6825 * If the maximum LBA is greater than 0xfffffffe, the user must
6826 * issue a SERVICE ACTION IN (16) command, with the read capacity
6827 * serivce action set.
6829 if (lun->be_lun->maxlba > 0xfffffffe)
6830 scsi_ulto4b(0xffffffff, data->addr);
6832 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6835 * XXX KDM this may not be 512 bytes...
6837 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6839 ctl_set_success(ctsio);
6840 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6841 ctsio->be_move_done = ctl_config_move_done;
6842 ctl_datamove((union ctl_io *)ctsio);
6843 return (CTL_RETVAL_COMPLETE);
6847 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
6849 struct scsi_read_capacity_16 *cdb;
6850 struct scsi_read_capacity_data_long *data;
6851 struct ctl_lun *lun;
6855 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
6857 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
6859 alloc_len = scsi_4btoul(cdb->alloc_len);
6860 lba = scsi_8btou64(cdb->addr);
6862 if ((cdb->reladr & SRC16_PMI)
6864 ctl_set_invalid_field(/*ctsio*/ ctsio,
6870 ctl_done((union ctl_io *)ctsio);
6871 return (CTL_RETVAL_COMPLETE);
6874 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6876 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6877 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
6879 if (sizeof(*data) < alloc_len) {
6880 ctsio->residual = alloc_len - sizeof(*data);
6881 ctsio->kern_data_len = sizeof(*data);
6882 ctsio->kern_total_len = sizeof(*data);
6884 ctsio->residual = 0;
6885 ctsio->kern_data_len = alloc_len;
6886 ctsio->kern_total_len = alloc_len;
6888 ctsio->kern_data_resid = 0;
6889 ctsio->kern_rel_offset = 0;
6890 ctsio->kern_sg_entries = 0;
6892 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
6893 /* XXX KDM this may not be 512 bytes... */
6894 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6895 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
6896 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
6897 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
6898 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
6900 ctl_set_success(ctsio);
6901 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6902 ctsio->be_move_done = ctl_config_move_done;
6903 ctl_datamove((union ctl_io *)ctsio);
6904 return (CTL_RETVAL_COMPLETE);
6908 ctl_get_lba_status(struct ctl_scsiio *ctsio)
6910 struct scsi_get_lba_status *cdb;
6911 struct scsi_get_lba_status_data *data;
6912 struct ctl_lun *lun;
6913 struct ctl_lba_len_flags *lbalen;
6915 uint32_t alloc_len, total_len;
6918 CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
6920 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6921 cdb = (struct scsi_get_lba_status *)ctsio->cdb;
6922 lba = scsi_8btou64(cdb->addr);
6923 alloc_len = scsi_4btoul(cdb->alloc_len);
6925 if (lba > lun->be_lun->maxlba) {
6926 ctl_set_lba_out_of_range(ctsio);
6927 ctl_done((union ctl_io *)ctsio);
6928 return (CTL_RETVAL_COMPLETE);
6931 total_len = sizeof(*data) + sizeof(data->descr[0]);
6932 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6933 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
6935 if (total_len < alloc_len) {
6936 ctsio->residual = alloc_len - total_len;
6937 ctsio->kern_data_len = total_len;
6938 ctsio->kern_total_len = total_len;
6940 ctsio->residual = 0;
6941 ctsio->kern_data_len = alloc_len;
6942 ctsio->kern_total_len = alloc_len;
6944 ctsio->kern_data_resid = 0;
6945 ctsio->kern_rel_offset = 0;
6946 ctsio->kern_sg_entries = 0;
6948 /* Fill dummy data in case backend can't tell anything. */
6949 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
6950 scsi_u64to8b(lba, data->descr[0].addr);
6951 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
6952 data->descr[0].length);
6953 data->descr[0].status = 0; /* Mapped or unknown. */
6955 ctl_set_success(ctsio);
6956 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6957 ctsio->be_move_done = ctl_config_move_done;
6959 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
6961 lbalen->len = total_len;
6963 retval = lun->backend->config_read((union ctl_io *)ctsio);
6964 return (CTL_RETVAL_COMPLETE);
6968 ctl_read_defect(struct ctl_scsiio *ctsio)
6970 struct scsi_read_defect_data_10 *ccb10;
6971 struct scsi_read_defect_data_12 *ccb12;
6972 struct scsi_read_defect_data_hdr_10 *data10;
6973 struct scsi_read_defect_data_hdr_12 *data12;
6974 uint32_t alloc_len, data_len;
6977 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
6979 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
6980 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
6981 format = ccb10->format;
6982 alloc_len = scsi_2btoul(ccb10->alloc_length);
6983 data_len = sizeof(*data10);
6985 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
6986 format = ccb12->format;
6987 alloc_len = scsi_4btoul(ccb12->alloc_length);
6988 data_len = sizeof(*data12);
6990 if (alloc_len == 0) {
6991 ctl_set_success(ctsio);
6992 ctl_done((union ctl_io *)ctsio);
6993 return (CTL_RETVAL_COMPLETE);
6996 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
6997 if (data_len < alloc_len) {
6998 ctsio->residual = alloc_len - data_len;
6999 ctsio->kern_data_len = data_len;
7000 ctsio->kern_total_len = data_len;
7002 ctsio->residual = 0;
7003 ctsio->kern_data_len = alloc_len;
7004 ctsio->kern_total_len = alloc_len;
7006 ctsio->kern_data_resid = 0;
7007 ctsio->kern_rel_offset = 0;
7008 ctsio->kern_sg_entries = 0;
7010 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7011 data10 = (struct scsi_read_defect_data_hdr_10 *)
7012 ctsio->kern_data_ptr;
7013 data10->format = format;
7014 scsi_ulto2b(0, data10->length);
7016 data12 = (struct scsi_read_defect_data_hdr_12 *)
7017 ctsio->kern_data_ptr;
7018 data12->format = format;
7019 scsi_ulto2b(0, data12->generation);
7020 scsi_ulto4b(0, data12->length);
7023 ctl_set_success(ctsio);
7024 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7025 ctsio->be_move_done = ctl_config_move_done;
7026 ctl_datamove((union ctl_io *)ctsio);
7027 return (CTL_RETVAL_COMPLETE);
7031 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7033 struct scsi_maintenance_in *cdb;
7035 int alloc_len, ext, total_len = 0, g, pc, pg, gs, os;
7036 int num_target_port_groups, num_target_ports;
7037 struct ctl_lun *lun;
7038 struct ctl_softc *softc;
7039 struct ctl_port *port;
7040 struct scsi_target_group_data *rtg_ptr;
7041 struct scsi_target_group_data_extended *rtg_ext_ptr;
7042 struct scsi_target_port_group_descriptor *tpg_desc;
7044 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7046 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7047 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7048 softc = lun->ctl_softc;
7050 retval = CTL_RETVAL_COMPLETE;
7052 switch (cdb->byte2 & STG_PDF_MASK) {
7053 case STG_PDF_LENGTH:
7056 case STG_PDF_EXTENDED:
7060 ctl_set_invalid_field(/*ctsio*/ ctsio,
7066 ctl_done((union ctl_io *)ctsio);
7070 if (softc->is_single)
7071 num_target_port_groups = 1;
7073 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
7074 num_target_ports = 0;
7075 mtx_lock(&softc->ctl_lock);
7076 STAILQ_FOREACH(port, &softc->port_list, links) {
7077 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7079 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7083 mtx_unlock(&softc->ctl_lock);
7086 total_len = sizeof(struct scsi_target_group_data_extended);
7088 total_len = sizeof(struct scsi_target_group_data);
7089 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7090 num_target_port_groups +
7091 sizeof(struct scsi_target_port_descriptor) * num_target_ports;
7093 alloc_len = scsi_4btoul(cdb->length);
7095 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7097 ctsio->kern_sg_entries = 0;
7099 if (total_len < alloc_len) {
7100 ctsio->residual = alloc_len - total_len;
7101 ctsio->kern_data_len = total_len;
7102 ctsio->kern_total_len = total_len;
7104 ctsio->residual = 0;
7105 ctsio->kern_data_len = alloc_len;
7106 ctsio->kern_total_len = alloc_len;
7108 ctsio->kern_data_resid = 0;
7109 ctsio->kern_rel_offset = 0;
7112 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7113 ctsio->kern_data_ptr;
7114 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7115 rtg_ext_ptr->format_type = 0x10;
7116 rtg_ext_ptr->implicit_transition_time = 0;
7117 tpg_desc = &rtg_ext_ptr->groups[0];
7119 rtg_ptr = (struct scsi_target_group_data *)
7120 ctsio->kern_data_ptr;
7121 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7122 tpg_desc = &rtg_ptr->groups[0];
7125 mtx_lock(&softc->ctl_lock);
7126 pg = softc->port_min / softc->port_cnt;
7127 if (softc->ha_link == CTL_HA_LINK_OFFLINE)
7128 gs = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7129 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN)
7130 gs = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7131 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY)
7132 gs = TPG_ASYMMETRIC_ACCESS_STANDBY;
7134 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7135 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7137 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7139 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7140 for (g = 0; g < num_target_port_groups; g++) {
7141 tpg_desc->pref_state = (g == pg) ? gs : os;
7142 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7143 TPG_U_SUP | TPG_T_SUP;
7144 scsi_ulto2b(g + 1, tpg_desc->target_port_group);
7145 tpg_desc->status = TPG_IMPLICIT;
7147 STAILQ_FOREACH(port, &softc->port_list, links) {
7148 if (port->targ_port < g * softc->port_cnt ||
7149 port->targ_port >= (g + 1) * softc->port_cnt)
7151 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7153 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7155 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7156 relative_target_port_identifier);
7159 tpg_desc->target_port_count = pc;
7160 tpg_desc = (struct scsi_target_port_group_descriptor *)
7161 &tpg_desc->descriptors[pc];
7163 mtx_unlock(&softc->ctl_lock);
7165 ctl_set_success(ctsio);
7166 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7167 ctsio->be_move_done = ctl_config_move_done;
7168 ctl_datamove((union ctl_io *)ctsio);
7173 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7175 struct ctl_lun *lun;
7176 struct scsi_report_supported_opcodes *cdb;
7177 const struct ctl_cmd_entry *entry, *sentry;
7178 struct scsi_report_supported_opcodes_all *all;
7179 struct scsi_report_supported_opcodes_descr *descr;
7180 struct scsi_report_supported_opcodes_one *one;
7182 int alloc_len, total_len;
7183 int opcode, service_action, i, j, num;
7185 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7187 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7188 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7190 retval = CTL_RETVAL_COMPLETE;
7192 opcode = cdb->requested_opcode;
7193 service_action = scsi_2btoul(cdb->requested_service_action);
7194 switch (cdb->options & RSO_OPTIONS_MASK) {
7195 case RSO_OPTIONS_ALL:
7197 for (i = 0; i < 256; i++) {
7198 entry = &ctl_cmd_table[i];
7199 if (entry->flags & CTL_CMD_FLAG_SA5) {
7200 for (j = 0; j < 32; j++) {
7201 sentry = &((const struct ctl_cmd_entry *)
7203 if (ctl_cmd_applicable(
7204 lun->be_lun->lun_type, sentry))
7208 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7213 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7214 num * sizeof(struct scsi_report_supported_opcodes_descr);
7216 case RSO_OPTIONS_OC:
7217 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7218 ctl_set_invalid_field(/*ctsio*/ ctsio,
7224 ctl_done((union ctl_io *)ctsio);
7225 return (CTL_RETVAL_COMPLETE);
7227 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7229 case RSO_OPTIONS_OC_SA:
7230 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7231 service_action >= 32) {
7232 ctl_set_invalid_field(/*ctsio*/ ctsio,
7238 ctl_done((union ctl_io *)ctsio);
7239 return (CTL_RETVAL_COMPLETE);
7241 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7244 ctl_set_invalid_field(/*ctsio*/ ctsio,
7250 ctl_done((union ctl_io *)ctsio);
7251 return (CTL_RETVAL_COMPLETE);
7254 alloc_len = scsi_4btoul(cdb->length);
7256 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7258 ctsio->kern_sg_entries = 0;
7260 if (total_len < alloc_len) {
7261 ctsio->residual = alloc_len - total_len;
7262 ctsio->kern_data_len = total_len;
7263 ctsio->kern_total_len = total_len;
7265 ctsio->residual = 0;
7266 ctsio->kern_data_len = alloc_len;
7267 ctsio->kern_total_len = alloc_len;
7269 ctsio->kern_data_resid = 0;
7270 ctsio->kern_rel_offset = 0;
7272 switch (cdb->options & RSO_OPTIONS_MASK) {
7273 case RSO_OPTIONS_ALL:
7274 all = (struct scsi_report_supported_opcodes_all *)
7275 ctsio->kern_data_ptr;
7277 for (i = 0; i < 256; i++) {
7278 entry = &ctl_cmd_table[i];
7279 if (entry->flags & CTL_CMD_FLAG_SA5) {
7280 for (j = 0; j < 32; j++) {
7281 sentry = &((const struct ctl_cmd_entry *)
7283 if (!ctl_cmd_applicable(
7284 lun->be_lun->lun_type, sentry))
7286 descr = &all->descr[num++];
7288 scsi_ulto2b(j, descr->service_action);
7289 descr->flags = RSO_SERVACTV;
7290 scsi_ulto2b(sentry->length,
7294 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7297 descr = &all->descr[num++];
7299 scsi_ulto2b(0, descr->service_action);
7301 scsi_ulto2b(entry->length, descr->cdb_length);
7305 num * sizeof(struct scsi_report_supported_opcodes_descr),
7308 case RSO_OPTIONS_OC:
7309 one = (struct scsi_report_supported_opcodes_one *)
7310 ctsio->kern_data_ptr;
7311 entry = &ctl_cmd_table[opcode];
7313 case RSO_OPTIONS_OC_SA:
7314 one = (struct scsi_report_supported_opcodes_one *)
7315 ctsio->kern_data_ptr;
7316 entry = &ctl_cmd_table[opcode];
7317 entry = &((const struct ctl_cmd_entry *)
7318 entry->execute)[service_action];
7320 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7322 scsi_ulto2b(entry->length, one->cdb_length);
7323 one->cdb_usage[0] = opcode;
7324 memcpy(&one->cdb_usage[1], entry->usage,
7331 ctl_set_success(ctsio);
7332 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7333 ctsio->be_move_done = ctl_config_move_done;
7334 ctl_datamove((union ctl_io *)ctsio);
7339 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7341 struct scsi_report_supported_tmf *cdb;
7342 struct scsi_report_supported_tmf_data *data;
7344 int alloc_len, total_len;
7346 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7348 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7350 retval = CTL_RETVAL_COMPLETE;
7352 total_len = sizeof(struct scsi_report_supported_tmf_data);
7353 alloc_len = scsi_4btoul(cdb->length);
7355 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7357 ctsio->kern_sg_entries = 0;
7359 if (total_len < alloc_len) {
7360 ctsio->residual = alloc_len - total_len;
7361 ctsio->kern_data_len = total_len;
7362 ctsio->kern_total_len = total_len;
7364 ctsio->residual = 0;
7365 ctsio->kern_data_len = alloc_len;
7366 ctsio->kern_total_len = alloc_len;
7368 ctsio->kern_data_resid = 0;
7369 ctsio->kern_rel_offset = 0;
7371 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr;
7372 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS |
7374 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS;
7376 ctl_set_success(ctsio);
7377 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7378 ctsio->be_move_done = ctl_config_move_done;
7379 ctl_datamove((union ctl_io *)ctsio);
7384 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7386 struct scsi_report_timestamp *cdb;
7387 struct scsi_report_timestamp_data *data;
7391 int alloc_len, total_len;
7393 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7395 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7397 retval = CTL_RETVAL_COMPLETE;
7399 total_len = sizeof(struct scsi_report_timestamp_data);
7400 alloc_len = scsi_4btoul(cdb->length);
7402 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7404 ctsio->kern_sg_entries = 0;
7406 if (total_len < alloc_len) {
7407 ctsio->residual = alloc_len - total_len;
7408 ctsio->kern_data_len = total_len;
7409 ctsio->kern_total_len = total_len;
7411 ctsio->residual = 0;
7412 ctsio->kern_data_len = alloc_len;
7413 ctsio->kern_total_len = alloc_len;
7415 ctsio->kern_data_resid = 0;
7416 ctsio->kern_rel_offset = 0;
7418 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7419 scsi_ulto2b(sizeof(*data) - 2, data->length);
7420 data->origin = RTS_ORIG_OUTSIDE;
7422 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7423 scsi_ulto4b(timestamp >> 16, data->timestamp);
7424 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7426 ctl_set_success(ctsio);
7427 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7428 ctsio->be_move_done = ctl_config_move_done;
7429 ctl_datamove((union ctl_io *)ctsio);
7434 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7436 struct scsi_per_res_in *cdb;
7437 int alloc_len, total_len = 0;
7438 /* struct scsi_per_res_in_rsrv in_data; */
7439 struct ctl_lun *lun;
7440 struct ctl_softc *softc;
7443 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7445 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7447 alloc_len = scsi_2btoul(cdb->length);
7449 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7450 softc = lun->ctl_softc;
7453 mtx_lock(&lun->lun_lock);
7454 switch (cdb->action) {
7455 case SPRI_RK: /* read keys */
7456 total_len = sizeof(struct scsi_per_res_in_keys) +
7458 sizeof(struct scsi_per_res_key);
7460 case SPRI_RR: /* read reservation */
7461 if (lun->flags & CTL_LUN_PR_RESERVED)
7462 total_len = sizeof(struct scsi_per_res_in_rsrv);
7464 total_len = sizeof(struct scsi_per_res_in_header);
7466 case SPRI_RC: /* report capabilities */
7467 total_len = sizeof(struct scsi_per_res_cap);
7469 case SPRI_RS: /* read full status */
7470 total_len = sizeof(struct scsi_per_res_in_header) +
7471 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7475 panic("Invalid PR type %x", cdb->action);
7477 mtx_unlock(&lun->lun_lock);
7479 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7481 if (total_len < alloc_len) {
7482 ctsio->residual = alloc_len - total_len;
7483 ctsio->kern_data_len = total_len;
7484 ctsio->kern_total_len = total_len;
7486 ctsio->residual = 0;
7487 ctsio->kern_data_len = alloc_len;
7488 ctsio->kern_total_len = alloc_len;
7491 ctsio->kern_data_resid = 0;
7492 ctsio->kern_rel_offset = 0;
7493 ctsio->kern_sg_entries = 0;
7495 mtx_lock(&lun->lun_lock);
7496 switch (cdb->action) {
7497 case SPRI_RK: { // read keys
7498 struct scsi_per_res_in_keys *res_keys;
7501 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7504 * We had to drop the lock to allocate our buffer, which
7505 * leaves time for someone to come in with another
7506 * persistent reservation. (That is unlikely, though,
7507 * since this should be the only persistent reservation
7508 * command active right now.)
7510 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7511 (lun->pr_key_count *
7512 sizeof(struct scsi_per_res_key)))){
7513 mtx_unlock(&lun->lun_lock);
7514 free(ctsio->kern_data_ptr, M_CTL);
7515 printf("%s: reservation length changed, retrying\n",
7520 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7522 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7523 lun->pr_key_count, res_keys->header.length);
7525 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) {
7526 if ((key = ctl_get_prkey(lun, i)) == 0)
7530 * We used lun->pr_key_count to calculate the
7531 * size to allocate. If it turns out the number of
7532 * initiators with the registered flag set is
7533 * larger than that (i.e. they haven't been kept in
7534 * sync), we've got a problem.
7536 if (key_count >= lun->pr_key_count) {
7538 csevent_log(CSC_CTL | CSC_SHELF_SW |
7540 csevent_LogType_Fault,
7541 csevent_AlertLevel_Yellow,
7542 csevent_FRU_ShelfController,
7543 csevent_FRU_Firmware,
7544 csevent_FRU_Unknown,
7545 "registered keys %d >= key "
7546 "count %d", key_count,
7552 scsi_u64to8b(key, res_keys->keys[key_count].key);
7557 case SPRI_RR: { // read reservation
7558 struct scsi_per_res_in_rsrv *res;
7559 int tmp_len, header_only;
7561 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7563 scsi_ulto4b(lun->PRGeneration, res->header.generation);
7565 if (lun->flags & CTL_LUN_PR_RESERVED)
7567 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7568 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7569 res->header.length);
7572 tmp_len = sizeof(struct scsi_per_res_in_header);
7573 scsi_ulto4b(0, res->header.length);
7578 * We had to drop the lock to allocate our buffer, which
7579 * leaves time for someone to come in with another
7580 * persistent reservation. (That is unlikely, though,
7581 * since this should be the only persistent reservation
7582 * command active right now.)
7584 if (tmp_len != total_len) {
7585 mtx_unlock(&lun->lun_lock);
7586 free(ctsio->kern_data_ptr, M_CTL);
7587 printf("%s: reservation status changed, retrying\n",
7593 * No reservation held, so we're done.
7595 if (header_only != 0)
7599 * If the registration is an All Registrants type, the key
7600 * is 0, since it doesn't really matter.
7602 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7603 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7604 res->data.reservation);
7606 res->data.scopetype = lun->res_type;
7609 case SPRI_RC: //report capabilities
7611 struct scsi_per_res_cap *res_cap;
7614 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7615 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7616 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5;
7617 type_mask = SPRI_TM_WR_EX_AR |
7623 scsi_ulto2b(type_mask, res_cap->type_mask);
7626 case SPRI_RS: { // read full status
7627 struct scsi_per_res_in_full *res_status;
7628 struct scsi_per_res_in_full_desc *res_desc;
7629 struct ctl_port *port;
7632 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7635 * We had to drop the lock to allocate our buffer, which
7636 * leaves time for someone to come in with another
7637 * persistent reservation. (That is unlikely, though,
7638 * since this should be the only persistent reservation
7639 * command active right now.)
7641 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7642 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7643 lun->pr_key_count)){
7644 mtx_unlock(&lun->lun_lock);
7645 free(ctsio->kern_data_ptr, M_CTL);
7646 printf("%s: reservation length changed, retrying\n",
7651 scsi_ulto4b(lun->PRGeneration, res_status->header.generation);
7653 res_desc = &res_status->desc[0];
7654 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7655 if ((key = ctl_get_prkey(lun, i)) == 0)
7658 scsi_u64to8b(key, res_desc->res_key.key);
7659 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7660 (lun->pr_res_idx == i ||
7661 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7662 res_desc->flags = SPRI_FULL_R_HOLDER;
7663 res_desc->scopetype = lun->res_type;
7665 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7666 res_desc->rel_trgt_port_id);
7668 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7670 len = ctl_create_iid(port,
7671 i % CTL_MAX_INIT_PER_PORT,
7672 res_desc->transport_id);
7673 scsi_ulto4b(len, res_desc->additional_length);
7674 res_desc = (struct scsi_per_res_in_full_desc *)
7675 &res_desc->transport_id[len];
7677 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7678 res_status->header.length);
7683 * This is a bug, because we just checked for this above,
7684 * and should have returned an error.
7686 panic("Invalid PR type %x", cdb->action);
7687 break; /* NOTREACHED */
7689 mtx_unlock(&lun->lun_lock);
7691 ctl_set_success(ctsio);
7692 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7693 ctsio->be_move_done = ctl_config_move_done;
7694 ctl_datamove((union ctl_io *)ctsio);
7695 return (CTL_RETVAL_COMPLETE);
7699 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7703 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7704 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7705 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7706 struct scsi_per_res_out_parms* param)
7708 union ctl_ha_msg persis_io;
7711 mtx_lock(&lun->lun_lock);
7712 if (sa_res_key == 0) {
7713 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7714 /* validate scope and type */
7715 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7717 mtx_unlock(&lun->lun_lock);
7718 ctl_set_invalid_field(/*ctsio*/ ctsio,
7724 ctl_done((union ctl_io *)ctsio);
7728 if (type>8 || type==2 || type==4 || type==0) {
7729 mtx_unlock(&lun->lun_lock);
7730 ctl_set_invalid_field(/*ctsio*/ ctsio,
7736 ctl_done((union ctl_io *)ctsio);
7741 * Unregister everybody else and build UA for
7744 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7745 if (i == residx || ctl_get_prkey(lun, i) == 0)
7748 ctl_clr_prkey(lun, i);
7749 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7751 lun->pr_key_count = 1;
7752 lun->res_type = type;
7753 if (lun->res_type != SPR_TYPE_WR_EX_AR
7754 && lun->res_type != SPR_TYPE_EX_AC_AR)
7755 lun->pr_res_idx = residx;
7756 lun->PRGeneration++;
7757 mtx_unlock(&lun->lun_lock);
7759 /* send msg to other side */
7760 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7761 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7762 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7763 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7764 persis_io.pr.pr_info.res_type = type;
7765 memcpy(persis_io.pr.pr_info.sa_res_key,
7766 param->serv_act_res_key,
7767 sizeof(param->serv_act_res_key));
7768 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7769 sizeof(persis_io.pr), M_WAITOK);
7771 /* not all registrants */
7772 mtx_unlock(&lun->lun_lock);
7773 free(ctsio->kern_data_ptr, M_CTL);
7774 ctl_set_invalid_field(ctsio,
7780 ctl_done((union ctl_io *)ctsio);
7783 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7784 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7787 if (res_key == sa_res_key) {
7790 * The spec implies this is not good but doesn't
7791 * say what to do. There are two choices either
7792 * generate a res conflict or check condition
7793 * with illegal field in parameter data. Since
7794 * that is what is done when the sa_res_key is
7795 * zero I'll take that approach since this has
7796 * to do with the sa_res_key.
7798 mtx_unlock(&lun->lun_lock);
7799 free(ctsio->kern_data_ptr, M_CTL);
7800 ctl_set_invalid_field(ctsio,
7806 ctl_done((union ctl_io *)ctsio);
7810 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7811 if (ctl_get_prkey(lun, i) != sa_res_key)
7815 ctl_clr_prkey(lun, i);
7816 lun->pr_key_count--;
7817 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7820 mtx_unlock(&lun->lun_lock);
7821 free(ctsio->kern_data_ptr, M_CTL);
7822 ctl_set_reservation_conflict(ctsio);
7823 ctl_done((union ctl_io *)ctsio);
7824 return (CTL_RETVAL_COMPLETE);
7826 lun->PRGeneration++;
7827 mtx_unlock(&lun->lun_lock);
7829 /* send msg to other side */
7830 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7831 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7832 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7833 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7834 persis_io.pr.pr_info.res_type = type;
7835 memcpy(persis_io.pr.pr_info.sa_res_key,
7836 param->serv_act_res_key,
7837 sizeof(param->serv_act_res_key));
7838 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7839 sizeof(persis_io.pr), M_WAITOK);
7841 /* Reserved but not all registrants */
7842 /* sa_res_key is res holder */
7843 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
7844 /* validate scope and type */
7845 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7847 mtx_unlock(&lun->lun_lock);
7848 ctl_set_invalid_field(/*ctsio*/ ctsio,
7854 ctl_done((union ctl_io *)ctsio);
7858 if (type>8 || type==2 || type==4 || type==0) {
7859 mtx_unlock(&lun->lun_lock);
7860 ctl_set_invalid_field(/*ctsio*/ ctsio,
7866 ctl_done((union ctl_io *)ctsio);
7872 * if sa_res_key != res_key remove all
7873 * registrants w/sa_res_key and generate UA
7874 * for these registrants(Registrations
7875 * Preempted) if it wasn't an exclusive
7876 * reservation generate UA(Reservations
7877 * Preempted) for all other registered nexuses
7878 * if the type has changed. Establish the new
7879 * reservation and holder. If res_key and
7880 * sa_res_key are the same do the above
7881 * except don't unregister the res holder.
7884 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7885 if (i == residx || ctl_get_prkey(lun, i) == 0)
7888 if (sa_res_key == ctl_get_prkey(lun, i)) {
7889 ctl_clr_prkey(lun, i);
7890 lun->pr_key_count--;
7891 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7892 } else if (type != lun->res_type
7893 && (lun->res_type == SPR_TYPE_WR_EX_RO
7894 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
7895 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
7898 lun->res_type = type;
7899 if (lun->res_type != SPR_TYPE_WR_EX_AR
7900 && lun->res_type != SPR_TYPE_EX_AC_AR)
7901 lun->pr_res_idx = residx;
7903 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
7904 lun->PRGeneration++;
7905 mtx_unlock(&lun->lun_lock);
7907 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7908 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7909 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7910 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7911 persis_io.pr.pr_info.res_type = type;
7912 memcpy(persis_io.pr.pr_info.sa_res_key,
7913 param->serv_act_res_key,
7914 sizeof(param->serv_act_res_key));
7915 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7916 sizeof(persis_io.pr), M_WAITOK);
7919 * sa_res_key is not the res holder just
7920 * remove registrants
7924 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7925 if (sa_res_key != ctl_get_prkey(lun, i))
7929 ctl_clr_prkey(lun, i);
7930 lun->pr_key_count--;
7931 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7935 mtx_unlock(&lun->lun_lock);
7936 free(ctsio->kern_data_ptr, M_CTL);
7937 ctl_set_reservation_conflict(ctsio);
7938 ctl_done((union ctl_io *)ctsio);
7941 lun->PRGeneration++;
7942 mtx_unlock(&lun->lun_lock);
7944 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7945 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7946 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7947 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7948 persis_io.pr.pr_info.res_type = type;
7949 memcpy(persis_io.pr.pr_info.sa_res_key,
7950 param->serv_act_res_key,
7951 sizeof(param->serv_act_res_key));
7952 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7953 sizeof(persis_io.pr), M_WAITOK);
7960 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
7962 uint64_t sa_res_key;
7965 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
7967 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7968 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
7969 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
7970 if (sa_res_key == 0) {
7972 * Unregister everybody else and build UA for
7975 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7976 if (i == msg->pr.pr_info.residx ||
7977 ctl_get_prkey(lun, i) == 0)
7980 ctl_clr_prkey(lun, i);
7981 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7984 lun->pr_key_count = 1;
7985 lun->res_type = msg->pr.pr_info.res_type;
7986 if (lun->res_type != SPR_TYPE_WR_EX_AR
7987 && lun->res_type != SPR_TYPE_EX_AC_AR)
7988 lun->pr_res_idx = msg->pr.pr_info.residx;
7990 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7991 if (sa_res_key == ctl_get_prkey(lun, i))
7994 ctl_clr_prkey(lun, i);
7995 lun->pr_key_count--;
7996 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8000 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8001 if (i == msg->pr.pr_info.residx ||
8002 ctl_get_prkey(lun, i) == 0)
8005 if (sa_res_key == ctl_get_prkey(lun, i)) {
8006 ctl_clr_prkey(lun, i);
8007 lun->pr_key_count--;
8008 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8009 } else if (msg->pr.pr_info.res_type != lun->res_type
8010 && (lun->res_type == SPR_TYPE_WR_EX_RO
8011 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
8012 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8015 lun->res_type = msg->pr.pr_info.res_type;
8016 if (lun->res_type != SPR_TYPE_WR_EX_AR
8017 && lun->res_type != SPR_TYPE_EX_AC_AR)
8018 lun->pr_res_idx = msg->pr.pr_info.residx;
8020 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8022 lun->PRGeneration++;
8028 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8031 u_int32_t param_len;
8032 struct scsi_per_res_out *cdb;
8033 struct ctl_lun *lun;
8034 struct scsi_per_res_out_parms* param;
8035 struct ctl_softc *softc;
8037 uint64_t res_key, sa_res_key, key;
8039 union ctl_ha_msg persis_io;
8042 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8044 retval = CTL_RETVAL_COMPLETE;
8046 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8047 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8048 softc = lun->ctl_softc;
8051 * We only support whole-LUN scope. The scope & type are ignored for
8052 * register, register and ignore existing key and clear.
8053 * We sometimes ignore scope and type on preempts too!!
8054 * Verify reservation type here as well.
8056 type = cdb->scope_type & SPR_TYPE_MASK;
8057 if ((cdb->action == SPRO_RESERVE)
8058 || (cdb->action == SPRO_RELEASE)) {
8059 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8060 ctl_set_invalid_field(/*ctsio*/ ctsio,
8066 ctl_done((union ctl_io *)ctsio);
8067 return (CTL_RETVAL_COMPLETE);
8070 if (type>8 || type==2 || type==4 || type==0) {
8071 ctl_set_invalid_field(/*ctsio*/ ctsio,
8077 ctl_done((union ctl_io *)ctsio);
8078 return (CTL_RETVAL_COMPLETE);
8082 param_len = scsi_4btoul(cdb->length);
8084 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8085 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8086 ctsio->kern_data_len = param_len;
8087 ctsio->kern_total_len = param_len;
8088 ctsio->kern_data_resid = 0;
8089 ctsio->kern_rel_offset = 0;
8090 ctsio->kern_sg_entries = 0;
8091 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8092 ctsio->be_move_done = ctl_config_move_done;
8093 ctl_datamove((union ctl_io *)ctsio);
8095 return (CTL_RETVAL_COMPLETE);
8098 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8100 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8101 res_key = scsi_8btou64(param->res_key.key);
8102 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8105 * Validate the reservation key here except for SPRO_REG_IGNO
8106 * This must be done for all other service actions
8108 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8109 mtx_lock(&lun->lun_lock);
8110 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8111 if (res_key != key) {
8113 * The current key passed in doesn't match
8114 * the one the initiator previously
8117 mtx_unlock(&lun->lun_lock);
8118 free(ctsio->kern_data_ptr, M_CTL);
8119 ctl_set_reservation_conflict(ctsio);
8120 ctl_done((union ctl_io *)ctsio);
8121 return (CTL_RETVAL_COMPLETE);
8123 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8125 * We are not registered
8127 mtx_unlock(&lun->lun_lock);
8128 free(ctsio->kern_data_ptr, M_CTL);
8129 ctl_set_reservation_conflict(ctsio);
8130 ctl_done((union ctl_io *)ctsio);
8131 return (CTL_RETVAL_COMPLETE);
8132 } else if (res_key != 0) {
8134 * We are not registered and trying to register but
8135 * the register key isn't zero.
8137 mtx_unlock(&lun->lun_lock);
8138 free(ctsio->kern_data_ptr, M_CTL);
8139 ctl_set_reservation_conflict(ctsio);
8140 ctl_done((union ctl_io *)ctsio);
8141 return (CTL_RETVAL_COMPLETE);
8143 mtx_unlock(&lun->lun_lock);
8146 switch (cdb->action & SPRO_ACTION_MASK) {
8148 case SPRO_REG_IGNO: {
8151 printf("Registration received\n");
8155 * We don't support any of these options, as we report in
8156 * the read capabilities request (see
8157 * ctl_persistent_reserve_in(), above).
8159 if ((param->flags & SPR_SPEC_I_PT)
8160 || (param->flags & SPR_ALL_TG_PT)
8161 || (param->flags & SPR_APTPL)) {
8164 if (param->flags & SPR_APTPL)
8166 else if (param->flags & SPR_ALL_TG_PT)
8168 else /* SPR_SPEC_I_PT */
8171 free(ctsio->kern_data_ptr, M_CTL);
8172 ctl_set_invalid_field(ctsio,
8178 ctl_done((union ctl_io *)ctsio);
8179 return (CTL_RETVAL_COMPLETE);
8182 mtx_lock(&lun->lun_lock);
8185 * The initiator wants to clear the
8188 if (sa_res_key == 0) {
8190 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8191 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8192 && ctl_get_prkey(lun, residx) == 0)) {
8193 mtx_unlock(&lun->lun_lock);
8197 ctl_clr_prkey(lun, residx);
8198 lun->pr_key_count--;
8200 if (residx == lun->pr_res_idx) {
8201 lun->flags &= ~CTL_LUN_PR_RESERVED;
8202 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8204 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8205 || lun->res_type == SPR_TYPE_EX_AC_RO)
8206 && lun->pr_key_count) {
8208 * If the reservation is a registrants
8209 * only type we need to generate a UA
8210 * for other registered inits. The
8211 * sense code should be RESERVATIONS
8215 for (i = softc->init_min; i < softc->init_max; i++){
8216 if (ctl_get_prkey(lun, i) == 0)
8219 CTL_UA_RES_RELEASE);
8223 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8224 if (lun->pr_key_count==0) {
8225 lun->flags &= ~CTL_LUN_PR_RESERVED;
8227 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8230 lun->PRGeneration++;
8231 mtx_unlock(&lun->lun_lock);
8233 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8234 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8235 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8236 persis_io.pr.pr_info.residx = residx;
8237 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8238 sizeof(persis_io.pr), M_WAITOK);
8239 } else /* sa_res_key != 0 */ {
8242 * If we aren't registered currently then increment
8243 * the key count and set the registered flag.
8245 ctl_alloc_prkey(lun, residx);
8246 if (ctl_get_prkey(lun, residx) == 0)
8247 lun->pr_key_count++;
8248 ctl_set_prkey(lun, residx, sa_res_key);
8249 lun->PRGeneration++;
8250 mtx_unlock(&lun->lun_lock);
8252 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8253 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8254 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8255 persis_io.pr.pr_info.residx = residx;
8256 memcpy(persis_io.pr.pr_info.sa_res_key,
8257 param->serv_act_res_key,
8258 sizeof(param->serv_act_res_key));
8259 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8260 sizeof(persis_io.pr), M_WAITOK);
8267 printf("Reserve executed type %d\n", type);
8269 mtx_lock(&lun->lun_lock);
8270 if (lun->flags & CTL_LUN_PR_RESERVED) {
8272 * if this isn't the reservation holder and it's
8273 * not a "all registrants" type or if the type is
8274 * different then we have a conflict
8276 if ((lun->pr_res_idx != residx
8277 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8278 || lun->res_type != type) {
8279 mtx_unlock(&lun->lun_lock);
8280 free(ctsio->kern_data_ptr, M_CTL);
8281 ctl_set_reservation_conflict(ctsio);
8282 ctl_done((union ctl_io *)ctsio);
8283 return (CTL_RETVAL_COMPLETE);
8285 mtx_unlock(&lun->lun_lock);
8286 } else /* create a reservation */ {
8288 * If it's not an "all registrants" type record
8289 * reservation holder
8291 if (type != SPR_TYPE_WR_EX_AR
8292 && type != SPR_TYPE_EX_AC_AR)
8293 lun->pr_res_idx = residx; /* Res holder */
8295 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8297 lun->flags |= CTL_LUN_PR_RESERVED;
8298 lun->res_type = type;
8300 mtx_unlock(&lun->lun_lock);
8302 /* send msg to other side */
8303 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8304 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8305 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8306 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8307 persis_io.pr.pr_info.res_type = type;
8308 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8309 sizeof(persis_io.pr), M_WAITOK);
8314 mtx_lock(&lun->lun_lock);
8315 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8316 /* No reservation exists return good status */
8317 mtx_unlock(&lun->lun_lock);
8321 * Is this nexus a reservation holder?
8323 if (lun->pr_res_idx != residx
8324 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8326 * not a res holder return good status but
8329 mtx_unlock(&lun->lun_lock);
8333 if (lun->res_type != type) {
8334 mtx_unlock(&lun->lun_lock);
8335 free(ctsio->kern_data_ptr, M_CTL);
8336 ctl_set_illegal_pr_release(ctsio);
8337 ctl_done((union ctl_io *)ctsio);
8338 return (CTL_RETVAL_COMPLETE);
8341 /* okay to release */
8342 lun->flags &= ~CTL_LUN_PR_RESERVED;
8343 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8347 * if this isn't an exclusive access
8348 * res generate UA for all other
8351 if (type != SPR_TYPE_EX_AC
8352 && type != SPR_TYPE_WR_EX) {
8353 for (i = softc->init_min; i < softc->init_max; i++) {
8354 if (i == residx || ctl_get_prkey(lun, i) == 0)
8356 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8359 mtx_unlock(&lun->lun_lock);
8361 /* Send msg to other side */
8362 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8363 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8364 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8365 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8366 sizeof(persis_io.pr), M_WAITOK);
8370 /* send msg to other side */
8372 mtx_lock(&lun->lun_lock);
8373 lun->flags &= ~CTL_LUN_PR_RESERVED;
8375 lun->pr_key_count = 0;
8376 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8378 ctl_clr_prkey(lun, residx);
8379 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8380 if (ctl_get_prkey(lun, i) != 0) {
8381 ctl_clr_prkey(lun, i);
8382 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8384 lun->PRGeneration++;
8385 mtx_unlock(&lun->lun_lock);
8387 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8388 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8389 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8390 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8391 sizeof(persis_io.pr), M_WAITOK);
8395 case SPRO_PRE_ABO: {
8398 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8399 residx, ctsio, cdb, param);
8401 return (CTL_RETVAL_COMPLETE);
8405 panic("Invalid PR type %x", cdb->action);
8409 free(ctsio->kern_data_ptr, M_CTL);
8410 ctl_set_success(ctsio);
8411 ctl_done((union ctl_io *)ctsio);
8417 * This routine is for handling a message from the other SC pertaining to
8418 * persistent reserve out. All the error checking will have been done
8419 * so only perorming the action need be done here to keep the two
8423 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8425 struct ctl_lun *lun;
8426 struct ctl_softc *softc;
8428 uint32_t residx, targ_lun;
8430 softc = control_softc;
8431 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8432 mtx_lock(&softc->ctl_lock);
8433 if ((targ_lun >= CTL_MAX_LUNS) ||
8434 ((lun = softc->ctl_luns[targ_lun]) == NULL)) {
8435 mtx_unlock(&softc->ctl_lock);
8438 mtx_lock(&lun->lun_lock);
8439 mtx_unlock(&softc->ctl_lock);
8440 if (lun->flags & CTL_LUN_DISABLED) {
8441 mtx_unlock(&lun->lun_lock);
8444 residx = ctl_get_initindex(&msg->hdr.nexus);
8445 switch(msg->pr.pr_info.action) {
8446 case CTL_PR_REG_KEY:
8447 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8448 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8449 lun->pr_key_count++;
8450 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8451 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8452 lun->PRGeneration++;
8455 case CTL_PR_UNREG_KEY:
8456 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8457 lun->pr_key_count--;
8459 /* XXX Need to see if the reservation has been released */
8460 /* if so do we need to generate UA? */
8461 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8462 lun->flags &= ~CTL_LUN_PR_RESERVED;
8463 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8465 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8466 || lun->res_type == SPR_TYPE_EX_AC_RO)
8467 && lun->pr_key_count) {
8469 * If the reservation is a registrants
8470 * only type we need to generate a UA
8471 * for other registered inits. The
8472 * sense code should be RESERVATIONS
8476 for (i = softc->init_min; i < softc->init_max; i++) {
8477 if (ctl_get_prkey(lun, i) == 0)
8480 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8484 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8485 if (lun->pr_key_count==0) {
8486 lun->flags &= ~CTL_LUN_PR_RESERVED;
8488 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8491 lun->PRGeneration++;
8494 case CTL_PR_RESERVE:
8495 lun->flags |= CTL_LUN_PR_RESERVED;
8496 lun->res_type = msg->pr.pr_info.res_type;
8497 lun->pr_res_idx = msg->pr.pr_info.residx;
8501 case CTL_PR_RELEASE:
8503 * if this isn't an exclusive access res generate UA for all
8504 * other registrants.
8506 if (lun->res_type != SPR_TYPE_EX_AC
8507 && lun->res_type != SPR_TYPE_WR_EX) {
8508 for (i = softc->init_min; i < softc->init_max; i++)
8509 if (i == residx || ctl_get_prkey(lun, i) == 0)
8511 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8514 lun->flags &= ~CTL_LUN_PR_RESERVED;
8515 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8519 case CTL_PR_PREEMPT:
8520 ctl_pro_preempt_other(lun, msg);
8523 lun->flags &= ~CTL_LUN_PR_RESERVED;
8525 lun->pr_key_count = 0;
8526 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8528 for (i=0; i < CTL_MAX_INITIATORS; i++) {
8529 if (ctl_get_prkey(lun, i) == 0)
8531 ctl_clr_prkey(lun, i);
8532 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8534 lun->PRGeneration++;
8538 mtx_unlock(&lun->lun_lock);
8542 ctl_read_write(struct ctl_scsiio *ctsio)
8544 struct ctl_lun *lun;
8545 struct ctl_lba_len_flags *lbalen;
8547 uint32_t num_blocks;
8551 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8553 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8556 retval = CTL_RETVAL_COMPLETE;
8558 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8559 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8560 switch (ctsio->cdb[0]) {
8563 struct scsi_rw_6 *cdb;
8565 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8567 lba = scsi_3btoul(cdb->addr);
8568 /* only 5 bits are valid in the most significant address byte */
8570 num_blocks = cdb->length;
8572 * This is correct according to SBC-2.
8574 if (num_blocks == 0)
8580 struct scsi_rw_10 *cdb;
8582 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8583 if (cdb->byte2 & SRW10_FUA)
8584 flags |= CTL_LLF_FUA;
8585 if (cdb->byte2 & SRW10_DPO)
8586 flags |= CTL_LLF_DPO;
8587 lba = scsi_4btoul(cdb->addr);
8588 num_blocks = scsi_2btoul(cdb->length);
8591 case WRITE_VERIFY_10: {
8592 struct scsi_write_verify_10 *cdb;
8594 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8595 flags |= CTL_LLF_FUA;
8596 if (cdb->byte2 & SWV_DPO)
8597 flags |= CTL_LLF_DPO;
8598 lba = scsi_4btoul(cdb->addr);
8599 num_blocks = scsi_2btoul(cdb->length);
8604 struct scsi_rw_12 *cdb;
8606 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8607 if (cdb->byte2 & SRW12_FUA)
8608 flags |= CTL_LLF_FUA;
8609 if (cdb->byte2 & SRW12_DPO)
8610 flags |= CTL_LLF_DPO;
8611 lba = scsi_4btoul(cdb->addr);
8612 num_blocks = scsi_4btoul(cdb->length);
8615 case WRITE_VERIFY_12: {
8616 struct scsi_write_verify_12 *cdb;
8618 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8619 flags |= CTL_LLF_FUA;
8620 if (cdb->byte2 & SWV_DPO)
8621 flags |= CTL_LLF_DPO;
8622 lba = scsi_4btoul(cdb->addr);
8623 num_blocks = scsi_4btoul(cdb->length);
8628 struct scsi_rw_16 *cdb;
8630 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8631 if (cdb->byte2 & SRW12_FUA)
8632 flags |= CTL_LLF_FUA;
8633 if (cdb->byte2 & SRW12_DPO)
8634 flags |= CTL_LLF_DPO;
8635 lba = scsi_8btou64(cdb->addr);
8636 num_blocks = scsi_4btoul(cdb->length);
8639 case WRITE_ATOMIC_16: {
8640 struct scsi_rw_16 *cdb;
8642 if (lun->be_lun->atomicblock == 0) {
8643 ctl_set_invalid_opcode(ctsio);
8644 ctl_done((union ctl_io *)ctsio);
8645 return (CTL_RETVAL_COMPLETE);
8648 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8649 if (cdb->byte2 & SRW12_FUA)
8650 flags |= CTL_LLF_FUA;
8651 if (cdb->byte2 & SRW12_DPO)
8652 flags |= CTL_LLF_DPO;
8653 lba = scsi_8btou64(cdb->addr);
8654 num_blocks = scsi_4btoul(cdb->length);
8655 if (num_blocks > lun->be_lun->atomicblock) {
8656 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8657 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8659 ctl_done((union ctl_io *)ctsio);
8660 return (CTL_RETVAL_COMPLETE);
8664 case WRITE_VERIFY_16: {
8665 struct scsi_write_verify_16 *cdb;
8667 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8668 flags |= CTL_LLF_FUA;
8669 if (cdb->byte2 & SWV_DPO)
8670 flags |= CTL_LLF_DPO;
8671 lba = scsi_8btou64(cdb->addr);
8672 num_blocks = scsi_4btoul(cdb->length);
8677 * We got a command we don't support. This shouldn't
8678 * happen, commands should be filtered out above us.
8680 ctl_set_invalid_opcode(ctsio);
8681 ctl_done((union ctl_io *)ctsio);
8683 return (CTL_RETVAL_COMPLETE);
8684 break; /* NOTREACHED */
8688 * The first check is to make sure we're in bounds, the second
8689 * check is to catch wrap-around problems. If the lba + num blocks
8690 * is less than the lba, then we've wrapped around and the block
8691 * range is invalid anyway.
8693 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8694 || ((lba + num_blocks) < lba)) {
8695 ctl_set_lba_out_of_range(ctsio);
8696 ctl_done((union ctl_io *)ctsio);
8697 return (CTL_RETVAL_COMPLETE);
8701 * According to SBC-3, a transfer length of 0 is not an error.
8702 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8703 * translates to 256 blocks for those commands.
8705 if (num_blocks == 0) {
8706 ctl_set_success(ctsio);
8707 ctl_done((union ctl_io *)ctsio);
8708 return (CTL_RETVAL_COMPLETE);
8711 /* Set FUA and/or DPO if caches are disabled. */
8713 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8715 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8717 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8719 flags |= CTL_LLF_FUA;
8722 lbalen = (struct ctl_lba_len_flags *)
8723 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8725 lbalen->len = num_blocks;
8726 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8728 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8729 ctsio->kern_rel_offset = 0;
8731 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8733 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8739 ctl_cnw_cont(union ctl_io *io)
8741 struct ctl_scsiio *ctsio;
8742 struct ctl_lun *lun;
8743 struct ctl_lba_len_flags *lbalen;
8746 ctsio = &io->scsiio;
8747 ctsio->io_hdr.status = CTL_STATUS_NONE;
8748 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8749 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8750 lbalen = (struct ctl_lba_len_flags *)
8751 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8752 lbalen->flags &= ~CTL_LLF_COMPARE;
8753 lbalen->flags |= CTL_LLF_WRITE;
8755 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8756 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8761 ctl_cnw(struct ctl_scsiio *ctsio)
8763 struct ctl_lun *lun;
8764 struct ctl_lba_len_flags *lbalen;
8766 uint32_t num_blocks;
8769 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8771 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8774 retval = CTL_RETVAL_COMPLETE;
8776 switch (ctsio->cdb[0]) {
8777 case COMPARE_AND_WRITE: {
8778 struct scsi_compare_and_write *cdb;
8780 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8781 if (cdb->byte2 & SRW10_FUA)
8782 flags |= CTL_LLF_FUA;
8783 if (cdb->byte2 & SRW10_DPO)
8784 flags |= CTL_LLF_DPO;
8785 lba = scsi_8btou64(cdb->addr);
8786 num_blocks = cdb->length;
8791 * We got a command we don't support. This shouldn't
8792 * happen, commands should be filtered out above us.
8794 ctl_set_invalid_opcode(ctsio);
8795 ctl_done((union ctl_io *)ctsio);
8797 return (CTL_RETVAL_COMPLETE);
8798 break; /* NOTREACHED */
8802 * The first check is to make sure we're in bounds, the second
8803 * check is to catch wrap-around problems. If the lba + num blocks
8804 * is less than the lba, then we've wrapped around and the block
8805 * range is invalid anyway.
8807 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8808 || ((lba + num_blocks) < lba)) {
8809 ctl_set_lba_out_of_range(ctsio);
8810 ctl_done((union ctl_io *)ctsio);
8811 return (CTL_RETVAL_COMPLETE);
8815 * According to SBC-3, a transfer length of 0 is not an error.
8817 if (num_blocks == 0) {
8818 ctl_set_success(ctsio);
8819 ctl_done((union ctl_io *)ctsio);
8820 return (CTL_RETVAL_COMPLETE);
8823 /* Set FUA if write cache is disabled. */
8824 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8826 flags |= CTL_LLF_FUA;
8828 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
8829 ctsio->kern_rel_offset = 0;
8832 * Set the IO_CONT flag, so that if this I/O gets passed to
8833 * ctl_data_submit_done(), it'll get passed back to
8834 * ctl_ctl_cnw_cont() for further processing.
8836 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
8837 ctsio->io_cont = ctl_cnw_cont;
8839 lbalen = (struct ctl_lba_len_flags *)
8840 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8842 lbalen->len = num_blocks;
8843 lbalen->flags = CTL_LLF_COMPARE | flags;
8845 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
8846 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8851 ctl_verify(struct ctl_scsiio *ctsio)
8853 struct ctl_lun *lun;
8854 struct ctl_lba_len_flags *lbalen;
8856 uint32_t num_blocks;
8860 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8862 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
8865 flags = CTL_LLF_FUA;
8866 retval = CTL_RETVAL_COMPLETE;
8868 switch (ctsio->cdb[0]) {
8870 struct scsi_verify_10 *cdb;
8872 cdb = (struct scsi_verify_10 *)ctsio->cdb;
8873 if (cdb->byte2 & SVFY_BYTCHK)
8875 if (cdb->byte2 & SVFY_DPO)
8876 flags |= CTL_LLF_DPO;
8877 lba = scsi_4btoul(cdb->addr);
8878 num_blocks = scsi_2btoul(cdb->length);
8882 struct scsi_verify_12 *cdb;
8884 cdb = (struct scsi_verify_12 *)ctsio->cdb;
8885 if (cdb->byte2 & SVFY_BYTCHK)
8887 if (cdb->byte2 & SVFY_DPO)
8888 flags |= CTL_LLF_DPO;
8889 lba = scsi_4btoul(cdb->addr);
8890 num_blocks = scsi_4btoul(cdb->length);
8894 struct scsi_rw_16 *cdb;
8896 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8897 if (cdb->byte2 & SVFY_BYTCHK)
8899 if (cdb->byte2 & SVFY_DPO)
8900 flags |= CTL_LLF_DPO;
8901 lba = scsi_8btou64(cdb->addr);
8902 num_blocks = scsi_4btoul(cdb->length);
8907 * We got a command we don't support. This shouldn't
8908 * happen, commands should be filtered out above us.
8910 ctl_set_invalid_opcode(ctsio);
8911 ctl_done((union ctl_io *)ctsio);
8912 return (CTL_RETVAL_COMPLETE);
8916 * The first check is to make sure we're in bounds, the second
8917 * check is to catch wrap-around problems. If the lba + num blocks
8918 * is less than the lba, then we've wrapped around and the block
8919 * range is invalid anyway.
8921 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8922 || ((lba + num_blocks) < lba)) {
8923 ctl_set_lba_out_of_range(ctsio);
8924 ctl_done((union ctl_io *)ctsio);
8925 return (CTL_RETVAL_COMPLETE);
8929 * According to SBC-3, a transfer length of 0 is not an error.
8931 if (num_blocks == 0) {
8932 ctl_set_success(ctsio);
8933 ctl_done((union ctl_io *)ctsio);
8934 return (CTL_RETVAL_COMPLETE);
8937 lbalen = (struct ctl_lba_len_flags *)
8938 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8940 lbalen->len = num_blocks;
8942 lbalen->flags = CTL_LLF_COMPARE | flags;
8943 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8945 lbalen->flags = CTL_LLF_VERIFY | flags;
8946 ctsio->kern_total_len = 0;
8948 ctsio->kern_rel_offset = 0;
8950 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
8951 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8956 ctl_report_luns(struct ctl_scsiio *ctsio)
8958 struct ctl_softc *softc = control_softc;
8959 struct scsi_report_luns *cdb;
8960 struct scsi_report_luns_data *lun_data;
8961 struct ctl_lun *lun, *request_lun;
8962 struct ctl_port *port;
8963 int num_luns, retval;
8964 uint32_t alloc_len, lun_datalen;
8965 int num_filled, well_known;
8966 uint32_t initidx, targ_lun_id, lun_id;
8968 retval = CTL_RETVAL_COMPLETE;
8971 cdb = (struct scsi_report_luns *)ctsio->cdb;
8972 port = ctl_io_port(&ctsio->io_hdr);
8974 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
8976 mtx_lock(&softc->ctl_lock);
8978 for (targ_lun_id = 0; targ_lun_id < CTL_MAX_LUNS; targ_lun_id++) {
8979 if (ctl_lun_map_from_port(port, targ_lun_id) < CTL_MAX_LUNS)
8982 mtx_unlock(&softc->ctl_lock);
8984 switch (cdb->select_report) {
8985 case RPL_REPORT_DEFAULT:
8986 case RPL_REPORT_ALL:
8988 case RPL_REPORT_WELLKNOWN:
8993 ctl_set_invalid_field(ctsio,
8999 ctl_done((union ctl_io *)ctsio);
9001 break; /* NOTREACHED */
9004 alloc_len = scsi_4btoul(cdb->length);
9006 * The initiator has to allocate at least 16 bytes for this request,
9007 * so he can at least get the header and the first LUN. Otherwise
9008 * we reject the request (per SPC-3 rev 14, section 6.21).
9010 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9011 sizeof(struct scsi_report_luns_lundata))) {
9012 ctl_set_invalid_field(ctsio,
9018 ctl_done((union ctl_io *)ctsio);
9022 request_lun = (struct ctl_lun *)
9023 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9025 lun_datalen = sizeof(*lun_data) +
9026 (num_luns * sizeof(struct scsi_report_luns_lundata));
9028 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9029 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9030 ctsio->kern_sg_entries = 0;
9032 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9034 mtx_lock(&softc->ctl_lock);
9035 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9036 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9037 if (lun_id >= CTL_MAX_LUNS)
9039 lun = softc->ctl_luns[lun_id];
9043 if (targ_lun_id <= 0xff) {
9045 * Peripheral addressing method, bus number 0.
9047 lun_data->luns[num_filled].lundata[0] =
9048 RPL_LUNDATA_ATYP_PERIPH;
9049 lun_data->luns[num_filled].lundata[1] = targ_lun_id;
9051 } else if (targ_lun_id <= 0x3fff) {
9053 * Flat addressing method.
9055 lun_data->luns[num_filled].lundata[0] =
9056 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8);
9057 lun_data->luns[num_filled].lundata[1] =
9058 (targ_lun_id & 0xff);
9060 } else if (targ_lun_id <= 0xffffff) {
9062 * Extended flat addressing method.
9064 lun_data->luns[num_filled].lundata[0] =
9065 RPL_LUNDATA_ATYP_EXTLUN | 0x12;
9066 scsi_ulto3b(targ_lun_id,
9067 &lun_data->luns[num_filled].lundata[1]);
9070 printf("ctl_report_luns: bogus LUN number %jd, "
9071 "skipping\n", (intmax_t)targ_lun_id);
9074 * According to SPC-3, rev 14 section 6.21:
9076 * "The execution of a REPORT LUNS command to any valid and
9077 * installed logical unit shall clear the REPORTED LUNS DATA
9078 * HAS CHANGED unit attention condition for all logical
9079 * units of that target with respect to the requesting
9080 * initiator. A valid and installed logical unit is one
9081 * having a PERIPHERAL QUALIFIER of 000b in the standard
9082 * INQUIRY data (see 6.4.2)."
9084 * If request_lun is NULL, the LUN this report luns command
9085 * was issued to is either disabled or doesn't exist. In that
9086 * case, we shouldn't clear any pending lun change unit
9089 if (request_lun != NULL) {
9090 mtx_lock(&lun->lun_lock);
9091 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE);
9092 mtx_unlock(&lun->lun_lock);
9095 mtx_unlock(&softc->ctl_lock);
9098 * It's quite possible that we've returned fewer LUNs than we allocated
9099 * space for. Trim it.
9101 lun_datalen = sizeof(*lun_data) +
9102 (num_filled * sizeof(struct scsi_report_luns_lundata));
9104 if (lun_datalen < alloc_len) {
9105 ctsio->residual = alloc_len - lun_datalen;
9106 ctsio->kern_data_len = lun_datalen;
9107 ctsio->kern_total_len = lun_datalen;
9109 ctsio->residual = 0;
9110 ctsio->kern_data_len = alloc_len;
9111 ctsio->kern_total_len = alloc_len;
9113 ctsio->kern_data_resid = 0;
9114 ctsio->kern_rel_offset = 0;
9115 ctsio->kern_sg_entries = 0;
9118 * We set this to the actual data length, regardless of how much
9119 * space we actually have to return results. If the user looks at
9120 * this value, he'll know whether or not he allocated enough space
9121 * and reissue the command if necessary. We don't support well
9122 * known logical units, so if the user asks for that, return none.
9124 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9127 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9130 ctl_set_success(ctsio);
9131 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9132 ctsio->be_move_done = ctl_config_move_done;
9133 ctl_datamove((union ctl_io *)ctsio);
9138 ctl_request_sense(struct ctl_scsiio *ctsio)
9140 struct scsi_request_sense *cdb;
9141 struct scsi_sense_data *sense_ptr;
9142 struct ctl_softc *ctl_softc;
9143 struct ctl_lun *lun;
9146 scsi_sense_data_type sense_format;
9147 ctl_ua_type ua_type;
9149 cdb = (struct scsi_request_sense *)ctsio->cdb;
9151 ctl_softc = control_softc;
9152 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9154 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9157 * Determine which sense format the user wants.
9159 if (cdb->byte2 & SRS_DESC)
9160 sense_format = SSD_TYPE_DESC;
9162 sense_format = SSD_TYPE_FIXED;
9164 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9165 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9166 ctsio->kern_sg_entries = 0;
9169 * struct scsi_sense_data, which is currently set to 256 bytes, is
9170 * larger than the largest allowed value for the length field in the
9171 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9173 ctsio->residual = 0;
9174 ctsio->kern_data_len = cdb->length;
9175 ctsio->kern_total_len = cdb->length;
9177 ctsio->kern_data_resid = 0;
9178 ctsio->kern_rel_offset = 0;
9179 ctsio->kern_sg_entries = 0;
9182 * If we don't have a LUN, we don't have any pending sense.
9188 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9190 * Check for pending sense, and then for pending unit attentions.
9191 * Pending sense gets returned first, then pending unit attentions.
9193 mtx_lock(&lun->lun_lock);
9195 if (ctl_is_set(lun->have_ca, initidx)) {
9196 scsi_sense_data_type stored_format;
9199 * Check to see which sense format was used for the stored
9202 stored_format = scsi_sense_type(&lun->pending_sense[initidx]);
9205 * If the user requested a different sense format than the
9206 * one we stored, then we need to convert it to the other
9207 * format. If we're going from descriptor to fixed format
9208 * sense data, we may lose things in translation, depending
9209 * on what options were used.
9211 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9212 * for some reason we'll just copy it out as-is.
9214 if ((stored_format == SSD_TYPE_FIXED)
9215 && (sense_format == SSD_TYPE_DESC))
9216 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9217 &lun->pending_sense[initidx],
9218 (struct scsi_sense_data_desc *)sense_ptr);
9219 else if ((stored_format == SSD_TYPE_DESC)
9220 && (sense_format == SSD_TYPE_FIXED))
9221 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9222 &lun->pending_sense[initidx],
9223 (struct scsi_sense_data_fixed *)sense_ptr);
9225 memcpy(sense_ptr, &lun->pending_sense[initidx],
9226 MIN(sizeof(*sense_ptr),
9227 sizeof(lun->pending_sense[initidx])));
9229 ctl_clear_mask(lun->have_ca, initidx);
9234 ua_type = ctl_build_ua(lun, initidx, sense_ptr, sense_format);
9235 if (ua_type != CTL_UA_NONE)
9237 if (ua_type == CTL_UA_LUN_CHANGE) {
9238 mtx_unlock(&lun->lun_lock);
9239 mtx_lock(&ctl_softc->ctl_lock);
9240 ctl_clr_ua_allluns(ctl_softc, initidx, ua_type);
9241 mtx_unlock(&ctl_softc->ctl_lock);
9242 mtx_lock(&lun->lun_lock);
9246 mtx_unlock(&lun->lun_lock);
9249 * We already have a pending error, return it.
9251 if (have_error != 0) {
9253 * We report the SCSI status as OK, since the status of the
9254 * request sense command itself is OK.
9255 * We report 0 for the sense length, because we aren't doing
9256 * autosense in this case. We're reporting sense as
9259 ctl_set_success(ctsio);
9260 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9261 ctsio->be_move_done = ctl_config_move_done;
9262 ctl_datamove((union ctl_io *)ctsio);
9263 return (CTL_RETVAL_COMPLETE);
9269 * No sense information to report, so we report that everything is
9272 ctl_set_sense_data(sense_ptr,
9275 /*current_error*/ 1,
9276 /*sense_key*/ SSD_KEY_NO_SENSE,
9282 * We report 0 for the sense length, because we aren't doing
9283 * autosense in this case. We're reporting sense as parameter data.
9285 ctl_set_success(ctsio);
9286 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9287 ctsio->be_move_done = ctl_config_move_done;
9288 ctl_datamove((union ctl_io *)ctsio);
9289 return (CTL_RETVAL_COMPLETE);
9293 ctl_tur(struct ctl_scsiio *ctsio)
9296 CTL_DEBUG_PRINT(("ctl_tur\n"));
9298 ctl_set_success(ctsio);
9299 ctl_done((union ctl_io *)ctsio);
9301 return (CTL_RETVAL_COMPLETE);
9305 * SCSI VPD page 0x00, the Supported VPD Pages page.
9308 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9310 struct scsi_vpd_supported_pages *pages;
9312 struct ctl_lun *lun;
9315 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9317 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9318 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9319 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9320 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9321 ctsio->kern_sg_entries = 0;
9323 if (sup_page_size < alloc_len) {
9324 ctsio->residual = alloc_len - sup_page_size;
9325 ctsio->kern_data_len = sup_page_size;
9326 ctsio->kern_total_len = sup_page_size;
9328 ctsio->residual = 0;
9329 ctsio->kern_data_len = alloc_len;
9330 ctsio->kern_total_len = alloc_len;
9332 ctsio->kern_data_resid = 0;
9333 ctsio->kern_rel_offset = 0;
9334 ctsio->kern_sg_entries = 0;
9337 * The control device is always connected. The disk device, on the
9338 * other hand, may not be online all the time. Need to change this
9339 * to figure out whether the disk device is actually online or not.
9342 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9343 lun->be_lun->lun_type;
9345 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9348 /* Supported VPD pages */
9349 pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9351 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9352 /* Device Identification */
9353 pages->page_list[p++] = SVPD_DEVICE_ID;
9354 /* Extended INQUIRY Data */
9355 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9356 /* Mode Page Policy */
9357 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9359 pages->page_list[p++] = SVPD_SCSI_PORTS;
9360 /* Third-party Copy */
9361 pages->page_list[p++] = SVPD_SCSI_TPC;
9362 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9364 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9365 /* Block Device Characteristics */
9366 pages->page_list[p++] = SVPD_BDC;
9367 /* Logical Block Provisioning */
9368 pages->page_list[p++] = SVPD_LBP;
9372 ctl_set_success(ctsio);
9373 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9374 ctsio->be_move_done = ctl_config_move_done;
9375 ctl_datamove((union ctl_io *)ctsio);
9376 return (CTL_RETVAL_COMPLETE);
9380 * SCSI VPD page 0x80, the Unit Serial Number page.
9383 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9385 struct scsi_vpd_unit_serial_number *sn_ptr;
9386 struct ctl_lun *lun;
9389 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9391 data_len = 4 + CTL_SN_LEN;
9392 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9393 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9394 if (data_len < alloc_len) {
9395 ctsio->residual = alloc_len - data_len;
9396 ctsio->kern_data_len = data_len;
9397 ctsio->kern_total_len = data_len;
9399 ctsio->residual = 0;
9400 ctsio->kern_data_len = alloc_len;
9401 ctsio->kern_total_len = alloc_len;
9403 ctsio->kern_data_resid = 0;
9404 ctsio->kern_rel_offset = 0;
9405 ctsio->kern_sg_entries = 0;
9408 * The control device is always connected. The disk device, on the
9409 * other hand, may not be online all the time. Need to change this
9410 * to figure out whether the disk device is actually online or not.
9413 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9414 lun->be_lun->lun_type;
9416 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9418 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9419 sn_ptr->length = CTL_SN_LEN;
9421 * If we don't have a LUN, we just leave the serial number as
9425 strncpy((char *)sn_ptr->serial_num,
9426 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9428 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9430 ctl_set_success(ctsio);
9431 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9432 ctsio->be_move_done = ctl_config_move_done;
9433 ctl_datamove((union ctl_io *)ctsio);
9434 return (CTL_RETVAL_COMPLETE);
9439 * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9442 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9444 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9445 struct ctl_lun *lun;
9448 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9450 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9451 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9452 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9453 ctsio->kern_sg_entries = 0;
9455 if (data_len < alloc_len) {
9456 ctsio->residual = alloc_len - data_len;
9457 ctsio->kern_data_len = data_len;
9458 ctsio->kern_total_len = data_len;
9460 ctsio->residual = 0;
9461 ctsio->kern_data_len = alloc_len;
9462 ctsio->kern_total_len = alloc_len;
9464 ctsio->kern_data_resid = 0;
9465 ctsio->kern_rel_offset = 0;
9466 ctsio->kern_sg_entries = 0;
9469 * The control device is always connected. The disk device, on the
9470 * other hand, may not be online all the time.
9473 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9474 lun->be_lun->lun_type;
9476 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9477 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9478 scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9480 * We support head of queue, ordered and simple tags.
9482 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9484 * Volatile cache supported.
9486 eid_ptr->flags3 = SVPD_EID_V_SUP;
9489 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9490 * attention for a particular IT nexus on all LUNs once we report
9491 * it to that nexus once. This bit is required as of SPC-4.
9493 eid_ptr->flags4 = SVPD_EID_LUICLT;
9496 * XXX KDM in order to correctly answer this, we would need
9497 * information from the SIM to determine how much sense data it
9498 * can send. So this would really be a path inquiry field, most
9499 * likely. This can be set to a maximum of 252 according to SPC-4,
9500 * but the hardware may or may not be able to support that much.
9501 * 0 just means that the maximum sense data length is not reported.
9503 eid_ptr->max_sense_length = 0;
9505 ctl_set_success(ctsio);
9506 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9507 ctsio->be_move_done = ctl_config_move_done;
9508 ctl_datamove((union ctl_io *)ctsio);
9509 return (CTL_RETVAL_COMPLETE);
9513 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9515 struct scsi_vpd_mode_page_policy *mpp_ptr;
9516 struct ctl_lun *lun;
9519 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9521 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9522 sizeof(struct scsi_vpd_mode_page_policy_descr);
9524 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9525 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9526 ctsio->kern_sg_entries = 0;
9528 if (data_len < alloc_len) {
9529 ctsio->residual = alloc_len - data_len;
9530 ctsio->kern_data_len = data_len;
9531 ctsio->kern_total_len = data_len;
9533 ctsio->residual = 0;
9534 ctsio->kern_data_len = alloc_len;
9535 ctsio->kern_total_len = alloc_len;
9537 ctsio->kern_data_resid = 0;
9538 ctsio->kern_rel_offset = 0;
9539 ctsio->kern_sg_entries = 0;
9542 * The control device is always connected. The disk device, on the
9543 * other hand, may not be online all the time.
9546 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9547 lun->be_lun->lun_type;
9549 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9550 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9551 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9552 mpp_ptr->descr[0].page_code = 0x3f;
9553 mpp_ptr->descr[0].subpage_code = 0xff;
9554 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9556 ctl_set_success(ctsio);
9557 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9558 ctsio->be_move_done = ctl_config_move_done;
9559 ctl_datamove((union ctl_io *)ctsio);
9560 return (CTL_RETVAL_COMPLETE);
9564 * SCSI VPD page 0x83, the Device Identification page.
9567 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9569 struct scsi_vpd_device_id *devid_ptr;
9570 struct scsi_vpd_id_descriptor *desc;
9571 struct ctl_softc *softc;
9572 struct ctl_lun *lun;
9573 struct ctl_port *port;
9577 softc = control_softc;
9579 port = ctl_io_port(&ctsio->io_hdr);
9580 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9582 data_len = sizeof(struct scsi_vpd_device_id) +
9583 sizeof(struct scsi_vpd_id_descriptor) +
9584 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9585 sizeof(struct scsi_vpd_id_descriptor) +
9586 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9587 if (lun && lun->lun_devid)
9588 data_len += lun->lun_devid->len;
9589 if (port && port->port_devid)
9590 data_len += port->port_devid->len;
9591 if (port && port->target_devid)
9592 data_len += port->target_devid->len;
9594 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9595 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9596 ctsio->kern_sg_entries = 0;
9598 if (data_len < alloc_len) {
9599 ctsio->residual = alloc_len - data_len;
9600 ctsio->kern_data_len = data_len;
9601 ctsio->kern_total_len = data_len;
9603 ctsio->residual = 0;
9604 ctsio->kern_data_len = alloc_len;
9605 ctsio->kern_total_len = alloc_len;
9607 ctsio->kern_data_resid = 0;
9608 ctsio->kern_rel_offset = 0;
9609 ctsio->kern_sg_entries = 0;
9612 * The control device is always connected. The disk device, on the
9613 * other hand, may not be online all the time.
9616 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9617 lun->be_lun->lun_type;
9619 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9620 devid_ptr->page_code = SVPD_DEVICE_ID;
9621 scsi_ulto2b(data_len - 4, devid_ptr->length);
9623 if (port && port->port_type == CTL_PORT_FC)
9624 proto = SCSI_PROTO_FC << 4;
9625 else if (port && port->port_type == CTL_PORT_ISCSI)
9626 proto = SCSI_PROTO_ISCSI << 4;
9628 proto = SCSI_PROTO_SPI << 4;
9629 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9632 * We're using a LUN association here. i.e., this device ID is a
9633 * per-LUN identifier.
9635 if (lun && lun->lun_devid) {
9636 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9637 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9638 lun->lun_devid->len);
9642 * This is for the WWPN which is a port association.
9644 if (port && port->port_devid) {
9645 memcpy(desc, port->port_devid->data, port->port_devid->len);
9646 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9647 port->port_devid->len);
9651 * This is for the Relative Target Port(type 4h) identifier
9653 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9654 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9655 SVPD_ID_TYPE_RELTARG;
9657 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9658 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9659 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9662 * This is for the Target Port Group(type 5h) identifier
9664 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9665 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9666 SVPD_ID_TYPE_TPORTGRP;
9668 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / softc->port_cnt + 1,
9669 &desc->identifier[2]);
9670 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9671 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9674 * This is for the Target identifier
9676 if (port && port->target_devid) {
9677 memcpy(desc, port->target_devid->data, port->target_devid->len);
9680 ctl_set_success(ctsio);
9681 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9682 ctsio->be_move_done = ctl_config_move_done;
9683 ctl_datamove((union ctl_io *)ctsio);
9684 return (CTL_RETVAL_COMPLETE);
9688 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9690 struct ctl_softc *softc = control_softc;
9691 struct scsi_vpd_scsi_ports *sp;
9692 struct scsi_vpd_port_designation *pd;
9693 struct scsi_vpd_port_designation_cont *pdc;
9694 struct ctl_lun *lun;
9695 struct ctl_port *port;
9696 int data_len, num_target_ports, iid_len, id_len;
9698 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9700 num_target_ports = 0;
9703 mtx_lock(&softc->ctl_lock);
9704 STAILQ_FOREACH(port, &softc->port_list, links) {
9705 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9708 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
9711 if (port->init_devid)
9712 iid_len += port->init_devid->len;
9713 if (port->port_devid)
9714 id_len += port->port_devid->len;
9716 mtx_unlock(&softc->ctl_lock);
9718 data_len = sizeof(struct scsi_vpd_scsi_ports) +
9719 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9720 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9721 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9722 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9723 ctsio->kern_sg_entries = 0;
9725 if (data_len < alloc_len) {
9726 ctsio->residual = alloc_len - data_len;
9727 ctsio->kern_data_len = data_len;
9728 ctsio->kern_total_len = data_len;
9730 ctsio->residual = 0;
9731 ctsio->kern_data_len = alloc_len;
9732 ctsio->kern_total_len = alloc_len;
9734 ctsio->kern_data_resid = 0;
9735 ctsio->kern_rel_offset = 0;
9736 ctsio->kern_sg_entries = 0;
9739 * The control device is always connected. The disk device, on the
9740 * other hand, may not be online all the time. Need to change this
9741 * to figure out whether the disk device is actually online or not.
9744 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9745 lun->be_lun->lun_type;
9747 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9749 sp->page_code = SVPD_SCSI_PORTS;
9750 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9752 pd = &sp->design[0];
9754 mtx_lock(&softc->ctl_lock);
9755 STAILQ_FOREACH(port, &softc->port_list, links) {
9756 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9759 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
9761 scsi_ulto2b(port->targ_port, pd->relative_port_id);
9762 if (port->init_devid) {
9763 iid_len = port->init_devid->len;
9764 memcpy(pd->initiator_transportid,
9765 port->init_devid->data, port->init_devid->len);
9768 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9769 pdc = (struct scsi_vpd_port_designation_cont *)
9770 (&pd->initiator_transportid[iid_len]);
9771 if (port->port_devid) {
9772 id_len = port->port_devid->len;
9773 memcpy(pdc->target_port_descriptors,
9774 port->port_devid->data, port->port_devid->len);
9777 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9778 pd = (struct scsi_vpd_port_designation *)
9779 ((uint8_t *)pdc->target_port_descriptors + id_len);
9781 mtx_unlock(&softc->ctl_lock);
9783 ctl_set_success(ctsio);
9784 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9785 ctsio->be_move_done = ctl_config_move_done;
9786 ctl_datamove((union ctl_io *)ctsio);
9787 return (CTL_RETVAL_COMPLETE);
9791 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9793 struct scsi_vpd_block_limits *bl_ptr;
9794 struct ctl_lun *lun;
9797 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9799 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9800 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9801 ctsio->kern_sg_entries = 0;
9803 if (sizeof(*bl_ptr) < alloc_len) {
9804 ctsio->residual = alloc_len - sizeof(*bl_ptr);
9805 ctsio->kern_data_len = sizeof(*bl_ptr);
9806 ctsio->kern_total_len = sizeof(*bl_ptr);
9808 ctsio->residual = 0;
9809 ctsio->kern_data_len = alloc_len;
9810 ctsio->kern_total_len = alloc_len;
9812 ctsio->kern_data_resid = 0;
9813 ctsio->kern_rel_offset = 0;
9814 ctsio->kern_sg_entries = 0;
9817 * The control device is always connected. The disk device, on the
9818 * other hand, may not be online all the time. Need to change this
9819 * to figure out whether the disk device is actually online or not.
9822 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9823 lun->be_lun->lun_type;
9825 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9827 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9828 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
9829 bl_ptr->max_cmp_write_len = 0xff;
9830 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9832 bs = lun->be_lun->blocksize;
9833 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
9834 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9835 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
9836 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
9837 if (lun->be_lun->ublockexp != 0) {
9838 scsi_ulto4b((1 << lun->be_lun->ublockexp),
9839 bl_ptr->opt_unmap_grain);
9840 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
9841 bl_ptr->unmap_grain_align);
9844 scsi_ulto4b(lun->be_lun->atomicblock,
9845 bl_ptr->max_atomic_transfer_length);
9846 scsi_ulto4b(0, bl_ptr->atomic_alignment);
9847 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
9849 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
9851 ctl_set_success(ctsio);
9852 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9853 ctsio->be_move_done = ctl_config_move_done;
9854 ctl_datamove((union ctl_io *)ctsio);
9855 return (CTL_RETVAL_COMPLETE);
9859 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
9861 struct scsi_vpd_block_device_characteristics *bdc_ptr;
9862 struct ctl_lun *lun;
9866 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9868 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
9869 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
9870 ctsio->kern_sg_entries = 0;
9872 if (sizeof(*bdc_ptr) < alloc_len) {
9873 ctsio->residual = alloc_len - sizeof(*bdc_ptr);
9874 ctsio->kern_data_len = sizeof(*bdc_ptr);
9875 ctsio->kern_total_len = sizeof(*bdc_ptr);
9877 ctsio->residual = 0;
9878 ctsio->kern_data_len = alloc_len;
9879 ctsio->kern_total_len = alloc_len;
9881 ctsio->kern_data_resid = 0;
9882 ctsio->kern_rel_offset = 0;
9883 ctsio->kern_sg_entries = 0;
9886 * The control device is always connected. The disk device, on the
9887 * other hand, may not be online all the time. Need to change this
9888 * to figure out whether the disk device is actually online or not.
9891 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9892 lun->be_lun->lun_type;
9894 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9895 bdc_ptr->page_code = SVPD_BDC;
9896 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
9898 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL)
9899 i = strtol(value, NULL, 0);
9901 i = CTL_DEFAULT_ROTATION_RATE;
9902 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
9904 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL)
9905 i = strtol(value, NULL, 0);
9908 bdc_ptr->wab_wac_ff = (i & 0x0f);
9909 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
9911 ctl_set_success(ctsio);
9912 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9913 ctsio->be_move_done = ctl_config_move_done;
9914 ctl_datamove((union ctl_io *)ctsio);
9915 return (CTL_RETVAL_COMPLETE);
9919 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
9921 struct scsi_vpd_logical_block_prov *lbp_ptr;
9922 struct ctl_lun *lun;
9924 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9926 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
9927 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
9928 ctsio->kern_sg_entries = 0;
9930 if (sizeof(*lbp_ptr) < alloc_len) {
9931 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
9932 ctsio->kern_data_len = sizeof(*lbp_ptr);
9933 ctsio->kern_total_len = sizeof(*lbp_ptr);
9935 ctsio->residual = 0;
9936 ctsio->kern_data_len = alloc_len;
9937 ctsio->kern_total_len = alloc_len;
9939 ctsio->kern_data_resid = 0;
9940 ctsio->kern_rel_offset = 0;
9941 ctsio->kern_sg_entries = 0;
9944 * The control device is always connected. The disk device, on the
9945 * other hand, may not be online all the time. Need to change this
9946 * to figure out whether the disk device is actually online or not.
9949 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9950 lun->be_lun->lun_type;
9952 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9954 lbp_ptr->page_code = SVPD_LBP;
9955 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
9956 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
9957 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9958 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
9959 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
9960 lbp_ptr->prov_type = SVPD_LBP_THIN;
9963 ctl_set_success(ctsio);
9964 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9965 ctsio->be_move_done = ctl_config_move_done;
9966 ctl_datamove((union ctl_io *)ctsio);
9967 return (CTL_RETVAL_COMPLETE);
9971 * INQUIRY with the EVPD bit set.
9974 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
9976 struct ctl_lun *lun;
9977 struct scsi_inquiry *cdb;
9978 int alloc_len, retval;
9980 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9981 cdb = (struct scsi_inquiry *)ctsio->cdb;
9982 alloc_len = scsi_2btoul(cdb->length);
9984 switch (cdb->page_code) {
9985 case SVPD_SUPPORTED_PAGES:
9986 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
9988 case SVPD_UNIT_SERIAL_NUMBER:
9989 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
9991 case SVPD_DEVICE_ID:
9992 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
9994 case SVPD_EXTENDED_INQUIRY_DATA:
9995 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
9997 case SVPD_MODE_PAGE_POLICY:
9998 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
10000 case SVPD_SCSI_PORTS:
10001 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10003 case SVPD_SCSI_TPC:
10004 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10006 case SVPD_BLOCK_LIMITS:
10007 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10009 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10012 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10014 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10017 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10019 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10023 ctl_set_invalid_field(ctsio,
10029 ctl_done((union ctl_io *)ctsio);
10030 retval = CTL_RETVAL_COMPLETE;
10038 * Standard INQUIRY data.
10041 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10043 struct scsi_inquiry_data *inq_ptr;
10044 struct scsi_inquiry *cdb;
10045 struct ctl_softc *softc;
10046 struct ctl_port *port;
10047 struct ctl_lun *lun;
10049 uint32_t alloc_len, data_len;
10050 ctl_port_type port_type;
10052 softc = control_softc;
10055 * Figure out whether we're talking to a Fibre Channel port or not.
10056 * We treat the ioctl front end, and any SCSI adapters, as packetized
10059 port = ctl_io_port(&ctsio->io_hdr);
10061 port_type = port->port_type;
10063 port_type = CTL_PORT_SCSI;
10064 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10065 port_type = CTL_PORT_SCSI;
10067 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10068 cdb = (struct scsi_inquiry *)ctsio->cdb;
10069 alloc_len = scsi_2btoul(cdb->length);
10072 * We malloc the full inquiry data size here and fill it
10073 * in. If the user only asks for less, we'll give him
10076 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10077 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10078 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10079 ctsio->kern_sg_entries = 0;
10080 ctsio->kern_data_resid = 0;
10081 ctsio->kern_rel_offset = 0;
10083 if (data_len < alloc_len) {
10084 ctsio->residual = alloc_len - data_len;
10085 ctsio->kern_data_len = data_len;
10086 ctsio->kern_total_len = data_len;
10088 ctsio->residual = 0;
10089 ctsio->kern_data_len = alloc_len;
10090 ctsio->kern_total_len = alloc_len;
10094 if ((lun->flags & CTL_LUN_PRIMARY_SC) ||
10095 softc->ha_link >= CTL_HA_LINK_UNKNOWN) {
10096 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10097 lun->be_lun->lun_type;
10099 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) |
10100 lun->be_lun->lun_type;
10103 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10105 /* RMB in byte 2 is 0 */
10106 inq_ptr->version = SCSI_REV_SPC4;
10109 * According to SAM-3, even if a device only supports a single
10110 * level of LUN addressing, it should still set the HISUP bit:
10112 * 4.9.1 Logical unit numbers overview
10114 * All logical unit number formats described in this standard are
10115 * hierarchical in structure even when only a single level in that
10116 * hierarchy is used. The HISUP bit shall be set to one in the
10117 * standard INQUIRY data (see SPC-2) when any logical unit number
10118 * format described in this standard is used. Non-hierarchical
10119 * formats are outside the scope of this standard.
10121 * Therefore we set the HiSup bit here.
10123 * The reponse format is 2, per SPC-3.
10125 inq_ptr->response_format = SID_HiSup | 2;
10127 inq_ptr->additional_length = data_len -
10128 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10129 CTL_DEBUG_PRINT(("additional_length = %d\n",
10130 inq_ptr->additional_length));
10132 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10133 /* 16 bit addressing */
10134 if (port_type == CTL_PORT_SCSI)
10135 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10136 /* XXX set the SID_MultiP bit here if we're actually going to
10137 respond on multiple ports */
10138 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10140 /* 16 bit data bus, synchronous transfers */
10141 if (port_type == CTL_PORT_SCSI)
10142 inq_ptr->flags = SID_WBus16 | SID_Sync;
10144 * XXX KDM do we want to support tagged queueing on the control
10148 || (lun->be_lun->lun_type != T_PROCESSOR))
10149 inq_ptr->flags |= SID_CmdQue;
10151 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10152 * We have 8 bytes for the vendor name, and 16 bytes for the device
10153 * name and 4 bytes for the revision.
10155 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10156 "vendor")) == NULL) {
10157 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10159 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10160 strncpy(inq_ptr->vendor, val,
10161 min(sizeof(inq_ptr->vendor), strlen(val)));
10164 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10165 sizeof(inq_ptr->product));
10166 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10167 switch (lun->be_lun->lun_type) {
10169 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10170 sizeof(inq_ptr->product));
10173 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10174 sizeof(inq_ptr->product));
10177 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10178 sizeof(inq_ptr->product));
10182 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10183 strncpy(inq_ptr->product, val,
10184 min(sizeof(inq_ptr->product), strlen(val)));
10188 * XXX make this a macro somewhere so it automatically gets
10189 * incremented when we make changes.
10191 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10192 "revision")) == NULL) {
10193 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10195 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10196 strncpy(inq_ptr->revision, val,
10197 min(sizeof(inq_ptr->revision), strlen(val)));
10201 * For parallel SCSI, we support double transition and single
10202 * transition clocking. We also support QAS (Quick Arbitration
10203 * and Selection) and Information Unit transfers on both the
10204 * control and array devices.
10206 if (port_type == CTL_PORT_SCSI)
10207 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10210 /* SAM-5 (no version claimed) */
10211 scsi_ulto2b(0x00A0, inq_ptr->version1);
10212 /* SPC-4 (no version claimed) */
10213 scsi_ulto2b(0x0460, inq_ptr->version2);
10214 if (port_type == CTL_PORT_FC) {
10215 /* FCP-2 ANSI INCITS.350:2003 */
10216 scsi_ulto2b(0x0917, inq_ptr->version3);
10217 } else if (port_type == CTL_PORT_SCSI) {
10218 /* SPI-4 ANSI INCITS.362:200x */
10219 scsi_ulto2b(0x0B56, inq_ptr->version3);
10220 } else if (port_type == CTL_PORT_ISCSI) {
10221 /* iSCSI (no version claimed) */
10222 scsi_ulto2b(0x0960, inq_ptr->version3);
10223 } else if (port_type == CTL_PORT_SAS) {
10224 /* SAS (no version claimed) */
10225 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10229 /* SBC-4 (no version claimed) */
10230 scsi_ulto2b(0x0600, inq_ptr->version4);
10232 switch (lun->be_lun->lun_type) {
10234 /* SBC-4 (no version claimed) */
10235 scsi_ulto2b(0x0600, inq_ptr->version4);
10243 ctl_set_success(ctsio);
10244 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10245 ctsio->be_move_done = ctl_config_move_done;
10246 ctl_datamove((union ctl_io *)ctsio);
10247 return (CTL_RETVAL_COMPLETE);
10251 ctl_inquiry(struct ctl_scsiio *ctsio)
10253 struct scsi_inquiry *cdb;
10256 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10258 cdb = (struct scsi_inquiry *)ctsio->cdb;
10259 if (cdb->byte2 & SI_EVPD)
10260 retval = ctl_inquiry_evpd(ctsio);
10261 else if (cdb->page_code == 0)
10262 retval = ctl_inquiry_std(ctsio);
10264 ctl_set_invalid_field(ctsio,
10270 ctl_done((union ctl_io *)ctsio);
10271 return (CTL_RETVAL_COMPLETE);
10278 * For known CDB types, parse the LBA and length.
10281 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10283 if (io->io_hdr.io_type != CTL_IO_SCSI)
10286 switch (io->scsiio.cdb[0]) {
10287 case COMPARE_AND_WRITE: {
10288 struct scsi_compare_and_write *cdb;
10290 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10292 *lba = scsi_8btou64(cdb->addr);
10293 *len = cdb->length;
10298 struct scsi_rw_6 *cdb;
10300 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10302 *lba = scsi_3btoul(cdb->addr);
10303 /* only 5 bits are valid in the most significant address byte */
10305 *len = cdb->length;
10310 struct scsi_rw_10 *cdb;
10312 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10314 *lba = scsi_4btoul(cdb->addr);
10315 *len = scsi_2btoul(cdb->length);
10318 case WRITE_VERIFY_10: {
10319 struct scsi_write_verify_10 *cdb;
10321 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10323 *lba = scsi_4btoul(cdb->addr);
10324 *len = scsi_2btoul(cdb->length);
10329 struct scsi_rw_12 *cdb;
10331 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10333 *lba = scsi_4btoul(cdb->addr);
10334 *len = scsi_4btoul(cdb->length);
10337 case WRITE_VERIFY_12: {
10338 struct scsi_write_verify_12 *cdb;
10340 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10342 *lba = scsi_4btoul(cdb->addr);
10343 *len = scsi_4btoul(cdb->length);
10348 case WRITE_ATOMIC_16: {
10349 struct scsi_rw_16 *cdb;
10351 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10353 *lba = scsi_8btou64(cdb->addr);
10354 *len = scsi_4btoul(cdb->length);
10357 case WRITE_VERIFY_16: {
10358 struct scsi_write_verify_16 *cdb;
10360 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10362 *lba = scsi_8btou64(cdb->addr);
10363 *len = scsi_4btoul(cdb->length);
10366 case WRITE_SAME_10: {
10367 struct scsi_write_same_10 *cdb;
10369 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10371 *lba = scsi_4btoul(cdb->addr);
10372 *len = scsi_2btoul(cdb->length);
10375 case WRITE_SAME_16: {
10376 struct scsi_write_same_16 *cdb;
10378 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10380 *lba = scsi_8btou64(cdb->addr);
10381 *len = scsi_4btoul(cdb->length);
10385 struct scsi_verify_10 *cdb;
10387 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10389 *lba = scsi_4btoul(cdb->addr);
10390 *len = scsi_2btoul(cdb->length);
10394 struct scsi_verify_12 *cdb;
10396 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10398 *lba = scsi_4btoul(cdb->addr);
10399 *len = scsi_4btoul(cdb->length);
10403 struct scsi_verify_16 *cdb;
10405 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10407 *lba = scsi_8btou64(cdb->addr);
10408 *len = scsi_4btoul(cdb->length);
10416 case SERVICE_ACTION_IN: { /* GET LBA STATUS */
10417 struct scsi_get_lba_status *cdb;
10419 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10420 *lba = scsi_8btou64(cdb->addr);
10426 break; /* NOTREACHED */
10433 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10436 uint64_t endlba1, endlba2;
10438 endlba1 = lba1 + len1 - (seq ? 0 : 1);
10439 endlba2 = lba2 + len2 - 1;
10441 if ((endlba1 < lba2) || (endlba2 < lba1))
10442 return (CTL_ACTION_PASS);
10444 return (CTL_ACTION_BLOCK);
10448 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10450 struct ctl_ptr_len_flags *ptrlen;
10451 struct scsi_unmap_desc *buf, *end, *range;
10455 /* If not UNMAP -- go other way. */
10456 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10457 io->scsiio.cdb[0] != UNMAP)
10458 return (CTL_ACTION_ERROR);
10460 /* If UNMAP without data -- block and wait for data. */
10461 ptrlen = (struct ctl_ptr_len_flags *)
10462 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10463 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10464 ptrlen->ptr == NULL)
10465 return (CTL_ACTION_BLOCK);
10467 /* UNMAP with data -- check for collision. */
10468 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10469 end = buf + ptrlen->len / sizeof(*buf);
10470 for (range = buf; range < end; range++) {
10471 lba = scsi_8btou64(range->lba);
10472 len = scsi_4btoul(range->length);
10473 if ((lba < lba2 + len2) && (lba + len > lba2))
10474 return (CTL_ACTION_BLOCK);
10476 return (CTL_ACTION_PASS);
10480 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10482 uint64_t lba1, lba2;
10483 uint64_t len1, len2;
10486 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10487 return (CTL_ACTION_ERROR);
10489 retval = ctl_extent_check_unmap(io1, lba2, len2);
10490 if (retval != CTL_ACTION_ERROR)
10493 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10494 return (CTL_ACTION_ERROR);
10496 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
10500 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2)
10502 uint64_t lba1, lba2;
10503 uint64_t len1, len2;
10505 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10506 return (CTL_ACTION_ERROR);
10507 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10508 return (CTL_ACTION_ERROR);
10510 if (lba1 + len1 == lba2)
10511 return (CTL_ACTION_BLOCK);
10512 return (CTL_ACTION_PASS);
10516 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10517 union ctl_io *ooa_io)
10519 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10520 ctl_serialize_action *serialize_row;
10523 * The initiator attempted multiple untagged commands at the same
10524 * time. Can't do that.
10526 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10527 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10528 && ((pending_io->io_hdr.nexus.targ_port ==
10529 ooa_io->io_hdr.nexus.targ_port)
10530 && (pending_io->io_hdr.nexus.initid ==
10531 ooa_io->io_hdr.nexus.initid))
10532 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10533 CTL_FLAG_STATUS_SENT)) == 0))
10534 return (CTL_ACTION_OVERLAP);
10537 * The initiator attempted to send multiple tagged commands with
10538 * the same ID. (It's fine if different initiators have the same
10541 * Even if all of those conditions are true, we don't kill the I/O
10542 * if the command ahead of us has been aborted. We won't end up
10543 * sending it to the FETD, and it's perfectly legal to resend a
10544 * command with the same tag number as long as the previous
10545 * instance of this tag number has been aborted somehow.
10547 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10548 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10549 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10550 && ((pending_io->io_hdr.nexus.targ_port ==
10551 ooa_io->io_hdr.nexus.targ_port)
10552 && (pending_io->io_hdr.nexus.initid ==
10553 ooa_io->io_hdr.nexus.initid))
10554 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10555 CTL_FLAG_STATUS_SENT)) == 0))
10556 return (CTL_ACTION_OVERLAP_TAG);
10559 * If we get a head of queue tag, SAM-3 says that we should
10560 * immediately execute it.
10562 * What happens if this command would normally block for some other
10563 * reason? e.g. a request sense with a head of queue tag
10564 * immediately after a write. Normally that would block, but this
10565 * will result in its getting executed immediately...
10567 * We currently return "pass" instead of "skip", so we'll end up
10568 * going through the rest of the queue to check for overlapped tags.
10570 * XXX KDM check for other types of blockage first??
10572 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10573 return (CTL_ACTION_PASS);
10576 * Ordered tags have to block until all items ahead of them
10577 * have completed. If we get called with an ordered tag, we always
10578 * block, if something else is ahead of us in the queue.
10580 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10581 return (CTL_ACTION_BLOCK);
10584 * Simple tags get blocked until all head of queue and ordered tags
10585 * ahead of them have completed. I'm lumping untagged commands in
10586 * with simple tags here. XXX KDM is that the right thing to do?
10588 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10589 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10590 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10591 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10592 return (CTL_ACTION_BLOCK);
10594 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
10595 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
10597 serialize_row = ctl_serialize_table[ooa_entry->seridx];
10599 switch (serialize_row[pending_entry->seridx]) {
10600 case CTL_SER_BLOCK:
10601 return (CTL_ACTION_BLOCK);
10602 case CTL_SER_EXTENT:
10603 return (ctl_extent_check(ooa_io, pending_io,
10604 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10605 case CTL_SER_EXTENTOPT:
10606 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
10607 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
10608 return (ctl_extent_check(ooa_io, pending_io,
10610 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10611 return (CTL_ACTION_PASS);
10612 case CTL_SER_EXTENTSEQ:
10613 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF)
10614 return (ctl_extent_check_seq(ooa_io, pending_io));
10615 return (CTL_ACTION_PASS);
10617 return (CTL_ACTION_PASS);
10618 case CTL_SER_BLOCKOPT:
10619 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
10620 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
10621 return (CTL_ACTION_BLOCK);
10622 return (CTL_ACTION_PASS);
10624 return (CTL_ACTION_SKIP);
10626 panic("invalid serialization value %d",
10627 serialize_row[pending_entry->seridx]);
10630 return (CTL_ACTION_ERROR);
10634 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
10636 * - pending_io is generally either incoming, or on the blocked queue
10637 * - starting I/O is the I/O we want to start the check with.
10640 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
10641 union ctl_io *starting_io)
10643 union ctl_io *ooa_io;
10646 mtx_assert(&lun->lun_lock, MA_OWNED);
10649 * Run back along the OOA queue, starting with the current
10650 * blocked I/O and going through every I/O before it on the
10651 * queue. If starting_io is NULL, we'll just end up returning
10654 for (ooa_io = starting_io; ooa_io != NULL;
10655 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
10659 * This routine just checks to see whether
10660 * cur_blocked is blocked by ooa_io, which is ahead
10661 * of it in the queue. It doesn't queue/dequeue
10664 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
10666 case CTL_ACTION_BLOCK:
10667 case CTL_ACTION_OVERLAP:
10668 case CTL_ACTION_OVERLAP_TAG:
10669 case CTL_ACTION_SKIP:
10670 case CTL_ACTION_ERROR:
10672 break; /* NOTREACHED */
10673 case CTL_ACTION_PASS:
10676 panic("invalid action %d", action);
10677 break; /* NOTREACHED */
10681 return (CTL_ACTION_PASS);
10686 * - An I/O has just completed, and has been removed from the per-LUN OOA
10687 * queue, so some items on the blocked queue may now be unblocked.
10690 ctl_check_blocked(struct ctl_lun *lun)
10692 struct ctl_softc *softc = lun->ctl_softc;
10693 union ctl_io *cur_blocked, *next_blocked;
10695 mtx_assert(&lun->lun_lock, MA_OWNED);
10698 * Run forward from the head of the blocked queue, checking each
10699 * entry against the I/Os prior to it on the OOA queue to see if
10700 * there is still any blockage.
10702 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
10703 * with our removing a variable on it while it is traversing the
10706 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
10707 cur_blocked != NULL; cur_blocked = next_blocked) {
10708 union ctl_io *prev_ooa;
10711 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
10714 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
10715 ctl_ooaq, ooa_links);
10718 * If cur_blocked happens to be the first item in the OOA
10719 * queue now, prev_ooa will be NULL, and the action
10720 * returned will just be CTL_ACTION_PASS.
10722 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
10725 case CTL_ACTION_BLOCK:
10726 /* Nothing to do here, still blocked */
10728 case CTL_ACTION_OVERLAP:
10729 case CTL_ACTION_OVERLAP_TAG:
10731 * This shouldn't happen! In theory we've already
10732 * checked this command for overlap...
10735 case CTL_ACTION_PASS:
10736 case CTL_ACTION_SKIP: {
10737 const struct ctl_cmd_entry *entry;
10740 * The skip case shouldn't happen, this transaction
10741 * should have never made it onto the blocked queue.
10744 * This I/O is no longer blocked, we can remove it
10745 * from the blocked queue. Since this is a TAILQ
10746 * (doubly linked list), we can do O(1) removals
10747 * from any place on the list.
10749 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
10751 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
10753 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
10754 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){
10756 * Need to send IO back to original side to
10759 union ctl_ha_msg msg_info;
10761 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
10762 msg_info.hdr.original_sc =
10763 cur_blocked->io_hdr.original_sc;
10764 msg_info.hdr.serializing_sc = cur_blocked;
10765 msg_info.hdr.msg_type = CTL_MSG_R2R;
10766 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
10767 sizeof(msg_info.hdr), M_NOWAIT);
10770 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
10773 * Check this I/O for LUN state changes that may
10774 * have happened while this command was blocked.
10775 * The LUN state may have been changed by a command
10776 * ahead of us in the queue, so we need to re-check
10777 * for any states that can be caused by SCSI
10780 if (ctl_scsiio_lun_check(lun, entry,
10781 &cur_blocked->scsiio) == 0) {
10782 cur_blocked->io_hdr.flags |=
10783 CTL_FLAG_IS_WAS_ON_RTR;
10784 ctl_enqueue_rtr(cur_blocked);
10786 ctl_done(cur_blocked);
10791 * This probably shouldn't happen -- we shouldn't
10792 * get CTL_ACTION_ERROR, or anything else.
10798 return (CTL_RETVAL_COMPLETE);
10802 * This routine (with one exception) checks LUN flags that can be set by
10803 * commands ahead of us in the OOA queue. These flags have to be checked
10804 * when a command initially comes in, and when we pull a command off the
10805 * blocked queue and are preparing to execute it. The reason we have to
10806 * check these flags for commands on the blocked queue is that the LUN
10807 * state may have been changed by a command ahead of us while we're on the
10810 * Ordering is somewhat important with these checks, so please pay
10811 * careful attention to the placement of any new checks.
10814 ctl_scsiio_lun_check(struct ctl_lun *lun,
10815 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
10817 struct ctl_softc *softc = lun->ctl_softc;
10823 mtx_assert(&lun->lun_lock, MA_OWNED);
10826 * If this shelf is a secondary shelf controller, we may have to
10827 * reject some commands disallowed by HA mode and link state.
10829 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
10830 if (softc->ha_link == CTL_HA_LINK_OFFLINE &&
10831 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
10832 ctl_set_lun_unavail(ctsio);
10836 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 &&
10837 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
10838 ctl_set_lun_transit(ctsio);
10842 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY &&
10843 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) {
10844 ctl_set_lun_standby(ctsio);
10849 /* The rest of checks are only done on executing side */
10850 if (softc->ha_mode == CTL_HA_MODE_XFER)
10854 if (entry->pattern & CTL_LUN_PAT_WRITE) {
10856 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) {
10857 ctl_set_hw_write_protected(ctsio);
10861 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT]
10862 .eca_and_aen & SCP_SWP) != 0) {
10863 ctl_set_sense(ctsio, /*current_error*/ 1,
10864 /*sense_key*/ SSD_KEY_DATA_PROTECT,
10865 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
10872 * Check for a reservation conflict. If this command isn't allowed
10873 * even on reserved LUNs, and if this initiator isn't the one who
10874 * reserved us, reject the command with a reservation conflict.
10876 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
10877 if ((lun->flags & CTL_LUN_RESERVED)
10878 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
10879 if (lun->res_idx != residx) {
10880 ctl_set_reservation_conflict(ctsio);
10886 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
10887 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
10888 /* No reservation or command is allowed. */;
10889 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
10890 (lun->res_type == SPR_TYPE_WR_EX ||
10891 lun->res_type == SPR_TYPE_WR_EX_RO ||
10892 lun->res_type == SPR_TYPE_WR_EX_AR)) {
10893 /* The command is allowed for Write Exclusive resv. */;
10896 * if we aren't registered or it's a res holder type
10897 * reservation and this isn't the res holder then set a
10900 if (ctl_get_prkey(lun, residx) == 0
10901 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
10902 ctl_set_reservation_conflict(ctsio);
10908 if ((lun->flags & CTL_LUN_OFFLINE)
10909 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0)) {
10910 ctl_set_lun_not_ready(ctsio);
10915 if ((lun->flags & CTL_LUN_STOPPED)
10916 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
10917 /* "Logical unit not ready, initializing cmd. required" */
10918 ctl_set_lun_stopped(ctsio);
10923 if ((lun->flags & CTL_LUN_INOPERABLE)
10924 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
10925 /* "Medium format corrupted" */
10926 ctl_set_medium_format_corrupted(ctsio);
10936 ctl_failover_io(union ctl_io *io, int have_lock)
10938 ctl_set_busy(&io->scsiio);
10943 ctl_failover_lun(struct ctl_lun *lun)
10945 struct ctl_softc *softc = lun->ctl_softc;
10946 struct ctl_io_hdr *io, *next_io;
10948 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", lun->lun));
10949 if (softc->ha_mode == CTL_HA_MODE_XFER) {
10950 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
10951 /* We are master */
10952 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
10953 if (io->flags & CTL_FLAG_IO_ACTIVE) {
10954 io->flags |= CTL_FLAG_ABORT;
10955 } else { /* This can be only due to DATAMOVE */
10956 io->msg_type = CTL_MSG_DATAMOVE_DONE;
10957 io->flags |= CTL_FLAG_IO_ACTIVE;
10958 io->port_status = 31340;
10959 ctl_enqueue_isc((union ctl_io *)io);
10963 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
10964 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
10965 if (io->flags & CTL_FLAG_IO_ACTIVE) {
10966 io->flags |= CTL_FLAG_FAILOVER;
10968 ctl_set_busy(&((union ctl_io *)io)->
10970 ctl_done((union ctl_io *)io);
10974 } else { /* SERIALIZE modes */
10975 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links,
10977 /* We are master */
10978 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
10979 TAILQ_REMOVE(&lun->blocked_queue, io,
10981 io->flags &= ~CTL_FLAG_BLOCKED;
10982 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
10983 ctl_free_io((union ctl_io *)io);
10986 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
10987 /* We are master */
10988 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
10989 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
10990 ctl_free_io((union ctl_io *)io);
10993 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
10994 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
10995 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) {
10996 ctl_set_busy(&((union ctl_io *)io)->
10998 ctl_done((union ctl_io *)io);
11002 ctl_check_blocked(lun);
11007 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio)
11009 struct ctl_lun *lun;
11010 const struct ctl_cmd_entry *entry;
11011 uint32_t initidx, targ_lun;
11018 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11019 if ((targ_lun < CTL_MAX_LUNS)
11020 && ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
11022 * If the LUN is invalid, pretend that it doesn't exist.
11023 * It will go away as soon as all pending I/O has been
11026 mtx_lock(&lun->lun_lock);
11027 if (lun->flags & CTL_LUN_DISABLED) {
11028 mtx_unlock(&lun->lun_lock);
11030 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11031 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11033 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
11034 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
11038 * Every I/O goes into the OOA queue for a
11039 * particular LUN, and stays there until completion.
11042 if (TAILQ_EMPTY(&lun->ooa_queue)) {
11043 lun->idle_time += getsbinuptime() -
11047 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr,
11051 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11052 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11055 /* Get command entry and return error if it is unsuppotyed. */
11056 entry = ctl_validate_command(ctsio);
11057 if (entry == NULL) {
11059 mtx_unlock(&lun->lun_lock);
11063 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11064 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11067 * Check to see whether we can send this command to LUNs that don't
11068 * exist. This should pretty much only be the case for inquiry
11069 * and request sense. Further checks, below, really require having
11070 * a LUN, so we can't really check the command anymore. Just put
11071 * it on the rtr queue.
11074 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) {
11075 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11076 ctl_enqueue_rtr((union ctl_io *)ctsio);
11080 ctl_set_unsupported_lun(ctsio);
11081 ctl_done((union ctl_io *)ctsio);
11082 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11086 * Make sure we support this particular command on this LUN.
11087 * e.g., we don't support writes to the control LUN.
11089 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11090 mtx_unlock(&lun->lun_lock);
11091 ctl_set_invalid_opcode(ctsio);
11092 ctl_done((union ctl_io *)ctsio);
11097 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11101 * If we've got a request sense, it'll clear the contingent
11102 * allegiance condition. Otherwise, if we have a CA condition for
11103 * this initiator, clear it, because it sent down a command other
11104 * than request sense.
11106 if ((ctsio->cdb[0] != REQUEST_SENSE)
11107 && (ctl_is_set(lun->have_ca, initidx)))
11108 ctl_clear_mask(lun->have_ca, initidx);
11112 * If the command has this flag set, it handles its own unit
11113 * attention reporting, we shouldn't do anything. Otherwise we
11114 * check for any pending unit attentions, and send them back to the
11115 * initiator. We only do this when a command initially comes in,
11116 * not when we pull it off the blocked queue.
11118 * According to SAM-3, section 5.3.2, the order that things get
11119 * presented back to the host is basically unit attentions caused
11120 * by some sort of reset event, busy status, reservation conflicts
11121 * or task set full, and finally any other status.
11123 * One issue here is that some of the unit attentions we report
11124 * don't fall into the "reset" category (e.g. "reported luns data
11125 * has changed"). So reporting it here, before the reservation
11126 * check, may be technically wrong. I guess the only thing to do
11127 * would be to check for and report the reset events here, and then
11128 * check for the other unit attention types after we check for a
11129 * reservation conflict.
11131 * XXX KDM need to fix this
11133 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11134 ctl_ua_type ua_type;
11135 scsi_sense_data_type sense_format;
11137 if (lun->flags & CTL_LUN_SENSE_DESC)
11138 sense_format = SSD_TYPE_DESC;
11140 sense_format = SSD_TYPE_FIXED;
11142 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11144 if (ua_type != CTL_UA_NONE) {
11145 mtx_unlock(&lun->lun_lock);
11146 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11147 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11148 ctsio->sense_len = SSD_FULL_SIZE;
11149 ctl_done((union ctl_io *)ctsio);
11155 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11156 mtx_unlock(&lun->lun_lock);
11157 ctl_done((union ctl_io *)ctsio);
11162 * XXX CHD this is where we want to send IO to other side if
11163 * this LUN is secondary on this SC. We will need to make a copy
11164 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11165 * the copy we send as FROM_OTHER.
11166 * We also need to stuff the address of the original IO so we can
11167 * find it easily. Something similar will need be done on the other
11168 * side so when we are done we can find the copy.
11170 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
11171 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0) {
11172 union ctl_ha_msg msg_info;
11175 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11176 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11177 mtx_unlock(&lun->lun_lock);
11179 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11180 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11181 msg_info.hdr.serializing_sc = NULL;
11182 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11183 msg_info.scsi.tag_num = ctsio->tag_num;
11184 msg_info.scsi.tag_type = ctsio->tag_type;
11185 msg_info.scsi.cdb_len = ctsio->cdb_len;
11186 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11188 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11189 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data),
11190 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) {
11191 ctl_set_busy(ctsio);
11192 ctl_done((union ctl_io *)ctsio);
11198 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11199 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11200 ctl_ooaq, ooa_links))) {
11201 case CTL_ACTION_BLOCK:
11202 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11203 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11205 mtx_unlock(&lun->lun_lock);
11207 case CTL_ACTION_PASS:
11208 case CTL_ACTION_SKIP:
11209 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11210 mtx_unlock(&lun->lun_lock);
11211 ctl_enqueue_rtr((union ctl_io *)ctsio);
11213 case CTL_ACTION_OVERLAP:
11214 mtx_unlock(&lun->lun_lock);
11215 ctl_set_overlapped_cmd(ctsio);
11216 ctl_done((union ctl_io *)ctsio);
11218 case CTL_ACTION_OVERLAP_TAG:
11219 mtx_unlock(&lun->lun_lock);
11220 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11221 ctl_done((union ctl_io *)ctsio);
11223 case CTL_ACTION_ERROR:
11225 mtx_unlock(&lun->lun_lock);
11226 ctl_set_internal_failure(ctsio,
11228 /*retry_count*/ 0);
11229 ctl_done((union ctl_io *)ctsio);
11235 const struct ctl_cmd_entry *
11236 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11238 const struct ctl_cmd_entry *entry;
11239 int service_action;
11241 entry = &ctl_cmd_table[ctsio->cdb[0]];
11243 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11244 if (entry->flags & CTL_CMD_FLAG_SA5) {
11245 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11246 entry = &((const struct ctl_cmd_entry *)
11247 entry->execute)[service_action];
11252 const struct ctl_cmd_entry *
11253 ctl_validate_command(struct ctl_scsiio *ctsio)
11255 const struct ctl_cmd_entry *entry;
11259 entry = ctl_get_cmd_entry(ctsio, &sa);
11260 if (entry->execute == NULL) {
11262 ctl_set_invalid_field(ctsio,
11269 ctl_set_invalid_opcode(ctsio);
11270 ctl_done((union ctl_io *)ctsio);
11273 KASSERT(entry->length > 0,
11274 ("Not defined length for command 0x%02x/0x%02x",
11275 ctsio->cdb[0], ctsio->cdb[1]));
11276 for (i = 1; i < entry->length; i++) {
11277 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11280 ctl_set_invalid_field(ctsio,
11285 /*bit*/ fls(diff) - 1);
11286 ctl_done((union ctl_io *)ctsio);
11293 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11296 switch (lun_type) {
11298 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) &&
11299 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11303 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) &&
11304 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11314 ctl_scsiio(struct ctl_scsiio *ctsio)
11317 const struct ctl_cmd_entry *entry;
11319 retval = CTL_RETVAL_COMPLETE;
11321 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11323 entry = ctl_get_cmd_entry(ctsio, NULL);
11326 * If this I/O has been aborted, just send it straight to
11327 * ctl_done() without executing it.
11329 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11330 ctl_done((union ctl_io *)ctsio);
11335 * All the checks should have been handled by ctl_scsiio_precheck().
11336 * We should be clear now to just execute the I/O.
11338 retval = entry->execute(ctsio);
11345 * Since we only implement one target right now, a bus reset simply resets
11346 * our single target.
11349 ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io)
11351 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET));
11355 ctl_target_reset(struct ctl_softc *softc, union ctl_io *io,
11356 ctl_ua_type ua_type)
11358 struct ctl_port *port;
11359 struct ctl_lun *lun;
11362 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11363 union ctl_ha_msg msg_info;
11365 msg_info.hdr.nexus = io->io_hdr.nexus;
11366 if (ua_type==CTL_UA_TARG_RESET)
11367 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11369 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11370 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11371 msg_info.hdr.original_sc = NULL;
11372 msg_info.hdr.serializing_sc = NULL;
11373 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11374 sizeof(msg_info.task), M_WAITOK);
11378 mtx_lock(&softc->ctl_lock);
11379 port = softc->ctl_ports[io->io_hdr.nexus.targ_port];
11380 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11381 if (port != NULL &&
11382 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
11384 retval += ctl_do_lun_reset(lun, io, ua_type);
11386 mtx_unlock(&softc->ctl_lock);
11387 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11392 * The LUN should always be set. The I/O is optional, and is used to
11393 * distinguish between I/Os sent by this initiator, and by other
11394 * initiators. We set unit attention for initiators other than this one.
11395 * SAM-3 is vague on this point. It does say that a unit attention should
11396 * be established for other initiators when a LUN is reset (see section
11397 * 5.7.3), but it doesn't specifically say that the unit attention should
11398 * be established for this particular initiator when a LUN is reset. Here
11399 * is the relevant text, from SAM-3 rev 8:
11401 * 5.7.2 When a SCSI initiator port aborts its own tasks
11403 * When a SCSI initiator port causes its own task(s) to be aborted, no
11404 * notification that the task(s) have been aborted shall be returned to
11405 * the SCSI initiator port other than the completion response for the
11406 * command or task management function action that caused the task(s) to
11407 * be aborted and notification(s) associated with related effects of the
11408 * action (e.g., a reset unit attention condition).
11410 * XXX KDM for now, we're setting unit attention for all initiators.
11413 ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11423 mtx_lock(&lun->lun_lock);
11425 * Run through the OOA queue and abort each I/O.
11427 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11428 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11429 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11433 * This version sets unit attention for every
11436 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11437 ctl_est_ua_all(lun, initidx, ua_type);
11439 ctl_est_ua_all(lun, -1, ua_type);
11443 * A reset (any kind, really) clears reservations established with
11444 * RESERVE/RELEASE. It does not clear reservations established
11445 * with PERSISTENT RESERVE OUT, but we don't support that at the
11446 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
11447 * reservations made with the RESERVE/RELEASE commands, because
11448 * those commands are obsolete in SPC-3.
11450 lun->flags &= ~CTL_LUN_RESERVED;
11453 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11454 ctl_clear_mask(lun->have_ca, i);
11456 mtx_unlock(&lun->lun_lock);
11462 ctl_lun_reset(struct ctl_softc *softc, union ctl_io *io)
11464 struct ctl_lun *lun;
11468 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11469 mtx_lock(&softc->ctl_lock);
11470 if ((targ_lun >= CTL_MAX_LUNS) ||
11471 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11472 mtx_unlock(&softc->ctl_lock);
11473 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11476 retval = ctl_do_lun_reset(lun, io, CTL_UA_LUN_RESET);
11477 mtx_unlock(&softc->ctl_lock);
11478 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11480 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) {
11481 union ctl_ha_msg msg_info;
11483 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11484 msg_info.hdr.nexus = io->io_hdr.nexus;
11485 msg_info.task.task_action = CTL_TASK_LUN_RESET;
11486 msg_info.hdr.original_sc = NULL;
11487 msg_info.hdr.serializing_sc = NULL;
11488 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11489 sizeof(msg_info.task), M_WAITOK);
11495 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11500 mtx_assert(&lun->lun_lock, MA_OWNED);
11503 * Run through the OOA queue and attempt to find the given I/O.
11504 * The target port, initiator ID, tag type and tag number have to
11505 * match the values that we got from the initiator. If we have an
11506 * untagged command to abort, simply abort the first untagged command
11507 * we come to. We only allow one untagged command at a time of course.
11509 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11510 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11512 if ((targ_port == UINT32_MAX ||
11513 targ_port == xio->io_hdr.nexus.targ_port) &&
11514 (init_id == UINT32_MAX ||
11515 init_id == xio->io_hdr.nexus.initid)) {
11516 if (targ_port != xio->io_hdr.nexus.targ_port ||
11517 init_id != xio->io_hdr.nexus.initid)
11518 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
11519 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11520 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11521 union ctl_ha_msg msg_info;
11523 msg_info.hdr.nexus = xio->io_hdr.nexus;
11524 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11525 msg_info.task.tag_num = xio->scsiio.tag_num;
11526 msg_info.task.tag_type = xio->scsiio.tag_type;
11527 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11528 msg_info.hdr.original_sc = NULL;
11529 msg_info.hdr.serializing_sc = NULL;
11530 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11531 sizeof(msg_info.task), M_NOWAIT);
11538 ctl_abort_task_set(union ctl_io *io)
11540 struct ctl_softc *softc = control_softc;
11541 struct ctl_lun *lun;
11547 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11548 mtx_lock(&softc->ctl_lock);
11549 if ((targ_lun >= CTL_MAX_LUNS) ||
11550 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11551 mtx_unlock(&softc->ctl_lock);
11552 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11556 mtx_lock(&lun->lun_lock);
11557 mtx_unlock(&softc->ctl_lock);
11558 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
11559 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11560 io->io_hdr.nexus.initid,
11561 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11562 } else { /* CTL_TASK_CLEAR_TASK_SET */
11563 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
11564 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11566 mtx_unlock(&lun->lun_lock);
11567 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11572 ctl_i_t_nexus_reset(union ctl_io *io)
11574 struct ctl_softc *softc = control_softc;
11575 struct ctl_lun *lun;
11578 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11579 union ctl_ha_msg msg_info;
11581 msg_info.hdr.nexus = io->io_hdr.nexus;
11582 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET;
11583 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11584 msg_info.hdr.original_sc = NULL;
11585 msg_info.hdr.serializing_sc = NULL;
11586 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11587 sizeof(msg_info.task), M_WAITOK);
11590 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11591 mtx_lock(&softc->ctl_lock);
11592 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11593 mtx_lock(&lun->lun_lock);
11594 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11595 io->io_hdr.nexus.initid, 1);
11597 ctl_clear_mask(lun->have_ca, initidx);
11599 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx))
11600 lun->flags &= ~CTL_LUN_RESERVED;
11601 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS);
11602 mtx_unlock(&lun->lun_lock);
11604 mtx_unlock(&softc->ctl_lock);
11605 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11610 ctl_abort_task(union ctl_io *io)
11613 struct ctl_lun *lun;
11614 struct ctl_softc *softc;
11617 char printbuf[128];
11622 softc = control_softc;
11628 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11629 mtx_lock(&softc->ctl_lock);
11630 if ((targ_lun >= CTL_MAX_LUNS) ||
11631 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11632 mtx_unlock(&softc->ctl_lock);
11633 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11638 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
11639 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
11642 mtx_lock(&lun->lun_lock);
11643 mtx_unlock(&softc->ctl_lock);
11645 * Run through the OOA queue and attempt to find the given I/O.
11646 * The target port, initiator ID, tag type and tag number have to
11647 * match the values that we got from the initiator. If we have an
11648 * untagged command to abort, simply abort the first untagged command
11649 * we come to. We only allow one untagged command at a time of course.
11651 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11652 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11654 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
11656 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
11657 lun->lun, xio->scsiio.tag_num,
11658 xio->scsiio.tag_type,
11659 (xio->io_hdr.blocked_links.tqe_prev
11660 == NULL) ? "" : " BLOCKED",
11661 (xio->io_hdr.flags &
11662 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
11663 (xio->io_hdr.flags &
11664 CTL_FLAG_ABORT) ? " ABORT" : "",
11665 (xio->io_hdr.flags &
11666 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
11667 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
11669 printf("%s\n", sbuf_data(&sb));
11672 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
11673 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
11674 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
11678 * If the abort says that the task is untagged, the
11679 * task in the queue must be untagged. Otherwise,
11680 * we just check to see whether the tag numbers
11681 * match. This is because the QLogic firmware
11682 * doesn't pass back the tag type in an abort
11686 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
11687 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
11688 || (xio->scsiio.tag_num == io->taskio.tag_num))
11691 * XXX KDM we've got problems with FC, because it
11692 * doesn't send down a tag type with aborts. So we
11693 * can only really go by the tag number...
11694 * This may cause problems with parallel SCSI.
11695 * Need to figure that out!!
11697 if (xio->scsiio.tag_num == io->taskio.tag_num) {
11698 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11700 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
11701 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11702 union ctl_ha_msg msg_info;
11704 msg_info.hdr.nexus = io->io_hdr.nexus;
11705 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11706 msg_info.task.tag_num = io->taskio.tag_num;
11707 msg_info.task.tag_type = io->taskio.tag_type;
11708 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11709 msg_info.hdr.original_sc = NULL;
11710 msg_info.hdr.serializing_sc = NULL;
11712 printf("Sent Abort to other side\n");
11714 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11715 sizeof(msg_info.task), M_NOWAIT);
11718 printf("ctl_abort_task: found I/O to abort\n");
11722 mtx_unlock(&lun->lun_lock);
11726 * This isn't really an error. It's entirely possible for
11727 * the abort and command completion to cross on the wire.
11728 * This is more of an informative/diagnostic error.
11731 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
11732 "%u:%u:%u tag %d type %d\n",
11733 io->io_hdr.nexus.initid,
11734 io->io_hdr.nexus.targ_port,
11735 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
11736 io->taskio.tag_type);
11739 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11744 ctl_query_task(union ctl_io *io, int task_set)
11747 struct ctl_lun *lun;
11748 struct ctl_softc *softc;
11752 softc = control_softc;
11753 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11754 mtx_lock(&softc->ctl_lock);
11755 if ((targ_lun >= CTL_MAX_LUNS) ||
11756 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11757 mtx_unlock(&softc->ctl_lock);
11758 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11761 mtx_lock(&lun->lun_lock);
11762 mtx_unlock(&softc->ctl_lock);
11763 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11764 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11766 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
11767 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
11768 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
11771 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) {
11776 mtx_unlock(&lun->lun_lock);
11778 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
11780 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11785 ctl_query_async_event(union ctl_io *io)
11787 struct ctl_lun *lun;
11788 struct ctl_softc *softc;
11790 uint32_t targ_lun, initidx;
11792 softc = control_softc;
11793 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11794 mtx_lock(&softc->ctl_lock);
11795 if ((targ_lun >= CTL_MAX_LUNS) ||
11796 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11797 mtx_unlock(&softc->ctl_lock);
11798 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11801 mtx_lock(&lun->lun_lock);
11802 mtx_unlock(&softc->ctl_lock);
11803 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11804 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp);
11805 mtx_unlock(&lun->lun_lock);
11806 if (ua != CTL_UA_NONE)
11807 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
11809 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11814 ctl_run_task(union ctl_io *io)
11816 struct ctl_softc *softc = control_softc;
11819 CTL_DEBUG_PRINT(("ctl_run_task\n"));
11820 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
11821 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type));
11822 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED;
11823 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp));
11824 switch (io->taskio.task_action) {
11825 case CTL_TASK_ABORT_TASK:
11826 retval = ctl_abort_task(io);
11828 case CTL_TASK_ABORT_TASK_SET:
11829 case CTL_TASK_CLEAR_TASK_SET:
11830 retval = ctl_abort_task_set(io);
11832 case CTL_TASK_CLEAR_ACA:
11834 case CTL_TASK_I_T_NEXUS_RESET:
11835 retval = ctl_i_t_nexus_reset(io);
11837 case CTL_TASK_LUN_RESET:
11838 retval = ctl_lun_reset(softc, io);
11840 case CTL_TASK_TARGET_RESET:
11841 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET);
11843 case CTL_TASK_BUS_RESET:
11844 retval = ctl_bus_reset(softc, io);
11846 case CTL_TASK_PORT_LOGIN:
11848 case CTL_TASK_PORT_LOGOUT:
11850 case CTL_TASK_QUERY_TASK:
11851 retval = ctl_query_task(io, 0);
11853 case CTL_TASK_QUERY_TASK_SET:
11854 retval = ctl_query_task(io, 1);
11856 case CTL_TASK_QUERY_ASYNC_EVENT:
11857 retval = ctl_query_async_event(io);
11860 printf("%s: got unknown task management event %d\n",
11861 __func__, io->taskio.task_action);
11865 io->io_hdr.status = CTL_SUCCESS;
11867 io->io_hdr.status = CTL_ERROR;
11872 * For HA operation. Handle commands that come in from the other
11876 ctl_handle_isc(union ctl_io *io)
11879 struct ctl_lun *lun;
11880 struct ctl_softc *softc;
11883 softc = control_softc;
11885 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11886 lun = softc->ctl_luns[targ_lun];
11888 switch (io->io_hdr.msg_type) {
11889 case CTL_MSG_SERIALIZE:
11890 free_io = ctl_serialize_other_sc_cmd(&io->scsiio);
11892 case CTL_MSG_R2R: {
11893 const struct ctl_cmd_entry *entry;
11896 * This is only used in SER_ONLY mode.
11899 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
11900 mtx_lock(&lun->lun_lock);
11901 if (ctl_scsiio_lun_check(lun,
11902 entry, (struct ctl_scsiio *)io) != 0) {
11903 mtx_unlock(&lun->lun_lock);
11907 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11908 mtx_unlock(&lun->lun_lock);
11909 ctl_enqueue_rtr(io);
11912 case CTL_MSG_FINISH_IO:
11913 if (softc->ha_mode == CTL_HA_MODE_XFER) {
11918 mtx_lock(&lun->lun_lock);
11919 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
11921 ctl_check_blocked(lun);
11922 mtx_unlock(&lun->lun_lock);
11925 case CTL_MSG_PERS_ACTION:
11926 ctl_hndl_per_res_out_on_other_sc(
11927 (union ctl_ha_msg *)&io->presio.pr_msg);
11930 case CTL_MSG_BAD_JUJU:
11934 case CTL_MSG_DATAMOVE:
11935 /* Only used in XFER mode */
11937 ctl_datamove_remote(io);
11939 case CTL_MSG_DATAMOVE_DONE:
11940 /* Only used in XFER mode */
11942 io->scsiio.be_move_done(io);
11944 case CTL_MSG_FAILOVER:
11945 mtx_lock(&lun->lun_lock);
11946 ctl_failover_lun(lun);
11947 mtx_unlock(&lun->lun_lock);
11952 printf("%s: Invalid message type %d\n",
11953 __func__, io->io_hdr.msg_type);
11963 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
11964 * there is no match.
11966 static ctl_lun_error_pattern
11967 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
11969 const struct ctl_cmd_entry *entry;
11970 ctl_lun_error_pattern filtered_pattern, pattern;
11972 pattern = desc->error_pattern;
11975 * XXX KDM we need more data passed into this function to match a
11976 * custom pattern, and we actually need to implement custom pattern
11979 if (pattern & CTL_LUN_PAT_CMD)
11980 return (CTL_LUN_PAT_CMD);
11982 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
11983 return (CTL_LUN_PAT_ANY);
11985 entry = ctl_get_cmd_entry(ctsio, NULL);
11987 filtered_pattern = entry->pattern & pattern;
11990 * If the user requested specific flags in the pattern (e.g.
11991 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
11994 * If the user did not specify any flags, it doesn't matter whether
11995 * or not the command supports the flags.
11997 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
11998 (pattern & ~CTL_LUN_PAT_MASK))
11999 return (CTL_LUN_PAT_NONE);
12002 * If the user asked for a range check, see if the requested LBA
12003 * range overlaps with this command's LBA range.
12005 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12011 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12013 return (CTL_LUN_PAT_NONE);
12015 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12016 desc->lba_range.len, FALSE);
12018 * A "pass" means that the LBA ranges don't overlap, so
12019 * this doesn't match the user's range criteria.
12021 if (action == CTL_ACTION_PASS)
12022 return (CTL_LUN_PAT_NONE);
12025 return (filtered_pattern);
12029 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12031 struct ctl_error_desc *desc, *desc2;
12033 mtx_assert(&lun->lun_lock, MA_OWNED);
12035 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12036 ctl_lun_error_pattern pattern;
12038 * Check to see whether this particular command matches
12039 * the pattern in the descriptor.
12041 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12042 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12045 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12046 case CTL_LUN_INJ_ABORTED:
12047 ctl_set_aborted(&io->scsiio);
12049 case CTL_LUN_INJ_MEDIUM_ERR:
12050 ctl_set_medium_error(&io->scsiio);
12052 case CTL_LUN_INJ_UA:
12053 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12055 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12057 case CTL_LUN_INJ_CUSTOM:
12059 * We're assuming the user knows what he is doing.
12060 * Just copy the sense information without doing
12063 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12064 MIN(sizeof(desc->custom_sense),
12065 sizeof(io->scsiio.sense_data)));
12066 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12067 io->scsiio.sense_len = SSD_FULL_SIZE;
12068 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12070 case CTL_LUN_INJ_NONE:
12073 * If this is an error injection type we don't know
12074 * about, clear the continuous flag (if it is set)
12075 * so it will get deleted below.
12077 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12081 * By default, each error injection action is a one-shot
12083 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12086 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12092 #ifdef CTL_IO_DELAY
12094 ctl_datamove_timer_wakeup(void *arg)
12098 io = (union ctl_io *)arg;
12102 #endif /* CTL_IO_DELAY */
12105 ctl_datamove(union ctl_io *io)
12107 void (*fe_datamove)(union ctl_io *io);
12109 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12111 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12114 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12119 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12120 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12122 sbuf_cat(&sb, path_str);
12123 switch (io->io_hdr.io_type) {
12125 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12126 sbuf_printf(&sb, "\n");
12127 sbuf_cat(&sb, path_str);
12128 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12129 io->scsiio.tag_num, io->scsiio.tag_type);
12132 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12133 "Tag Type: %d\n", io->taskio.task_action,
12134 io->taskio.tag_num, io->taskio.tag_type);
12137 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12138 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12141 sbuf_cat(&sb, path_str);
12142 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12143 (intmax_t)time_uptime - io->io_hdr.start_time);
12145 printf("%s", sbuf_data(&sb));
12147 #endif /* CTL_TIME_IO */
12149 #ifdef CTL_IO_DELAY
12150 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12151 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12153 struct ctl_lun *lun;
12155 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12157 && (lun->delay_info.datamove_delay > 0)) {
12159 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
12160 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12161 callout_reset(&io->io_hdr.delay_callout,
12162 lun->delay_info.datamove_delay * hz,
12163 ctl_datamove_timer_wakeup, io);
12164 if (lun->delay_info.datamove_type ==
12165 CTL_DELAY_TYPE_ONESHOT)
12166 lun->delay_info.datamove_delay = 0;
12173 * This command has been aborted. Set the port status, so we fail
12176 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12177 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n",
12178 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12179 io->io_hdr.nexus.targ_port,
12180 io->io_hdr.nexus.targ_lun);
12181 io->io_hdr.port_status = 31337;
12183 * Note that the backend, in this case, will get the
12184 * callback in its context. In other cases it may get
12185 * called in the frontend's interrupt thread context.
12187 io->scsiio.be_move_done(io);
12191 /* Don't confuse frontend with zero length data move. */
12192 if (io->scsiio.kern_data_len == 0) {
12193 io->scsiio.be_move_done(io);
12198 * If we're in XFER mode and this I/O is from the other shelf
12199 * controller, we need to send the DMA to the other side to
12200 * actually transfer the data to/from the host. In serialize only
12201 * mode the transfer happens below CTL and ctl_datamove() is only
12202 * called on the machine that originally received the I/O.
12204 if ((control_softc->ha_mode == CTL_HA_MODE_XFER)
12205 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12206 union ctl_ha_msg msg;
12207 uint32_t sg_entries_sent;
12211 memset(&msg, 0, sizeof(msg));
12212 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
12213 msg.hdr.original_sc = io->io_hdr.original_sc;
12214 msg.hdr.serializing_sc = io;
12215 msg.hdr.nexus = io->io_hdr.nexus;
12216 msg.hdr.status = io->io_hdr.status;
12217 msg.dt.flags = io->io_hdr.flags;
12219 * We convert everything into a S/G list here. We can't
12220 * pass by reference, only by value between controllers.
12221 * So we can't pass a pointer to the S/G list, only as many
12222 * S/G entries as we can fit in here. If it's possible for
12223 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
12224 * then we need to break this up into multiple transfers.
12226 if (io->scsiio.kern_sg_entries == 0) {
12227 msg.dt.kern_sg_entries = 1;
12230 * Convert to a physical address if this is a
12233 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
12234 msg.dt.sg_list[0].addr =
12235 io->scsiio.kern_data_ptr;
12238 * XXX KDM use busdma here!
12240 msg.dt.sg_list[0].addr = (void *)
12241 vtophys(io->scsiio.kern_data_ptr);
12244 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
12245 ("HA does not support BUS_ADDR"));
12246 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
12249 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
12252 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
12256 msg.dt.kern_data_len = io->scsiio.kern_data_len;
12257 msg.dt.kern_total_len = io->scsiio.kern_total_len;
12258 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
12259 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
12260 msg.dt.sg_sequence = 0;
12263 * Loop until we've sent all of the S/G entries. On the
12264 * other end, we'll recompose these S/G entries into one
12265 * contiguous list before passing it to the
12267 for (sg_entries_sent = 0; sg_entries_sent <
12268 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) {
12269 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list)/
12270 sizeof(msg.dt.sg_list[0])),
12271 msg.dt.kern_sg_entries - sg_entries_sent);
12273 if (do_sg_copy != 0) {
12274 struct ctl_sg_entry *sgl;
12277 sgl = (struct ctl_sg_entry *)
12278 io->scsiio.kern_data_ptr;
12280 * If this is in cached memory, flush the cache
12281 * before we send the DMA request to the other
12282 * controller. We want to do this in either
12283 * the * read or the write case. The read
12284 * case is straightforward. In the write
12285 * case, we want to make sure nothing is
12286 * in the local cache that could overwrite
12290 for (i = sg_entries_sent, j = 0;
12291 i < msg.dt.cur_sg_entries; i++, j++) {
12293 if ((io->io_hdr.flags &
12294 CTL_FLAG_BUS_ADDR) == 0) {
12296 * XXX KDM use busdma.
12298 msg.dt.sg_list[j].addr =(void *)
12299 vtophys(sgl[i].addr);
12301 msg.dt.sg_list[j].addr =
12305 KASSERT((io->io_hdr.flags &
12306 CTL_FLAG_BUS_ADDR) == 0,
12307 ("HA does not support BUS_ADDR"));
12308 msg.dt.sg_list[j].addr = sgl[i].addr;
12310 msg.dt.sg_list[j].len = sgl[i].len;
12314 sg_entries_sent += msg.dt.cur_sg_entries;
12315 if (sg_entries_sent >= msg.dt.kern_sg_entries)
12316 msg.dt.sg_last = 1;
12318 msg.dt.sg_last = 0;
12320 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12321 sizeof(msg.dt) - sizeof(msg.dt.sg_list) +
12322 sizeof(struct ctl_sg_entry)*msg.dt.cur_sg_entries,
12323 M_WAITOK) > CTL_HA_STATUS_SUCCESS) {
12324 io->io_hdr.port_status = 31341;
12325 io->scsiio.be_move_done(io);
12329 msg.dt.sent_sg_entries = sg_entries_sent;
12331 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12335 * Lookup the fe_datamove() function for this particular
12338 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12345 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12347 union ctl_ha_msg msg;
12349 memset(&msg, 0, sizeof(msg));
12351 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12352 msg.hdr.original_sc = io;
12353 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12354 msg.hdr.nexus = io->io_hdr.nexus;
12355 msg.hdr.status = io->io_hdr.status;
12356 msg.scsi.tag_num = io->scsiio.tag_num;
12357 msg.scsi.tag_type = io->scsiio.tag_type;
12358 msg.scsi.scsi_status = io->scsiio.scsi_status;
12359 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12360 io->scsiio.sense_len);
12361 msg.scsi.sense_len = io->scsiio.sense_len;
12362 msg.scsi.sense_residual = io->scsiio.sense_residual;
12363 msg.scsi.fetd_status = io->io_hdr.port_status;
12364 msg.scsi.residual = io->scsiio.residual;
12365 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12367 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12368 ctl_failover_io(io, /*have_lock*/ have_lock);
12372 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12373 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12374 msg.scsi.sense_len, M_WAITOK);
12378 * The DMA to the remote side is done, now we need to tell the other side
12379 * we're done so it can continue with its data movement.
12382 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12389 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12390 printf("%s: ISC DMA write failed with error %d", __func__,
12392 ctl_set_internal_failure(&io->scsiio,
12394 /*retry_count*/ rq->ret);
12397 ctl_dt_req_free(rq);
12399 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12400 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12401 free(io->io_hdr.remote_sglist, M_CTL);
12402 io->io_hdr.remote_sglist = NULL;
12403 io->io_hdr.local_sglist = NULL;
12406 * The data is in local and remote memory, so now we need to send
12407 * status (good or back) back to the other side.
12409 ctl_send_datamove_done(io, /*have_lock*/ 0);
12413 * We've moved the data from the host/controller into local memory. Now we
12414 * need to push it over to the remote controller's memory.
12417 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12423 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12424 ctl_datamove_remote_write_cb);
12430 ctl_datamove_remote_write(union ctl_io *io)
12433 void (*fe_datamove)(union ctl_io *io);
12436 * - Get the data from the host/HBA into local memory.
12437 * - DMA memory from the local controller to the remote controller.
12438 * - Send status back to the remote controller.
12441 retval = ctl_datamove_remote_sgl_setup(io);
12445 /* Switch the pointer over so the FETD knows what to do */
12446 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12449 * Use a custom move done callback, since we need to send completion
12450 * back to the other controller, not to the backend on this side.
12452 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12454 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12463 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12472 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12473 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12474 free(io->io_hdr.remote_sglist, M_CTL);
12475 io->io_hdr.remote_sglist = NULL;
12476 io->io_hdr.local_sglist = NULL;
12479 scsi_path_string(io, path_str, sizeof(path_str));
12480 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12481 sbuf_cat(&sb, path_str);
12482 scsi_command_string(&io->scsiio, NULL, &sb);
12483 sbuf_printf(&sb, "\n");
12484 sbuf_cat(&sb, path_str);
12485 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12486 io->scsiio.tag_num, io->scsiio.tag_type);
12487 sbuf_cat(&sb, path_str);
12488 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12489 io->io_hdr.flags, io->io_hdr.status);
12491 printk("%s", sbuf_data(&sb));
12496 * The read is done, now we need to send status (good or bad) back
12497 * to the other side.
12499 ctl_send_datamove_done(io, /*have_lock*/ 0);
12505 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12508 void (*fe_datamove)(union ctl_io *io);
12512 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12513 printf("%s: ISC DMA read failed with error %d\n", __func__,
12515 ctl_set_internal_failure(&io->scsiio,
12517 /*retry_count*/ rq->ret);
12520 ctl_dt_req_free(rq);
12522 /* Switch the pointer over so the FETD knows what to do */
12523 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12526 * Use a custom move done callback, since we need to send completion
12527 * back to the other controller, not to the backend on this side.
12529 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12531 /* XXX KDM add checks like the ones in ctl_datamove? */
12533 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12539 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12541 struct ctl_sg_entry *local_sglist, *remote_sglist;
12542 struct ctl_softc *softc;
12543 uint32_t len_to_go;
12548 softc = control_softc;
12549 local_sglist = io->io_hdr.local_sglist;
12550 remote_sglist = io->io_hdr.remote_sglist;
12551 len_to_go = io->scsiio.kern_data_len;
12554 * The difficult thing here is that the size of the various
12555 * S/G segments may be different than the size from the
12556 * remote controller. That'll make it harder when DMAing
12557 * the data back to the other side.
12559 for (i = 0; len_to_go > 0; i++) {
12560 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT);
12561 local_sglist[i].addr =
12562 malloc(local_sglist[i].len, M_CTL, M_WAITOK);
12564 len_to_go -= local_sglist[i].len;
12567 * Reset the number of S/G entries accordingly. The original
12568 * number of S/G entries is available in rem_sg_entries.
12570 io->scsiio.kern_sg_entries = i;
12573 printf("%s: kern_sg_entries = %d\n", __func__,
12574 io->scsiio.kern_sg_entries);
12575 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12576 printf("%s: sg[%d] = %p, %d\n", __func__, i,
12577 local_sglist[i].addr, local_sglist[i].len);
12584 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12585 ctl_ha_dt_cb callback)
12587 struct ctl_ha_dt_req *rq;
12588 struct ctl_sg_entry *remote_sglist, *local_sglist;
12589 uint32_t local_used, remote_used, total_used;
12592 rq = ctl_dt_req_alloc();
12595 * If we failed to allocate the request, and if the DMA didn't fail
12596 * anyway, set busy status. This is just a resource allocation
12600 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12601 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS))
12602 ctl_set_busy(&io->scsiio);
12604 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12605 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
12608 ctl_dt_req_free(rq);
12611 * The data move failed. We need to return status back
12612 * to the other controller. No point in trying to DMA
12613 * data to the remote controller.
12616 ctl_send_datamove_done(io, /*have_lock*/ 0);
12621 local_sglist = io->io_hdr.local_sglist;
12622 remote_sglist = io->io_hdr.remote_sglist;
12628 * Pull/push the data over the wire from/to the other controller.
12629 * This takes into account the possibility that the local and
12630 * remote sglists may not be identical in terms of the size of
12631 * the elements and the number of elements.
12633 * One fundamental assumption here is that the length allocated for
12634 * both the local and remote sglists is identical. Otherwise, we've
12635 * essentially got a coding error of some sort.
12637 isc_ret = CTL_HA_STATUS_SUCCESS;
12638 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12642 rq->command = command;
12646 * Both pointers should be aligned. But it is possible
12647 * that the allocation length is not. They should both
12648 * also have enough slack left over at the end, though,
12649 * to round up to the next 8 byte boundary.
12651 cur_len = MIN(local_sglist[i].len - local_used,
12652 remote_sglist[j].len - remote_used);
12653 rq->size = cur_len;
12655 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12656 tmp_ptr += local_used;
12659 /* Use physical addresses when talking to ISC hardware */
12660 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12661 /* XXX KDM use busdma */
12662 rq->local = vtophys(tmp_ptr);
12664 rq->local = tmp_ptr;
12666 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
12667 ("HA does not support BUS_ADDR"));
12668 rq->local = tmp_ptr;
12671 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12672 tmp_ptr += remote_used;
12673 rq->remote = tmp_ptr;
12675 rq->callback = NULL;
12677 local_used += cur_len;
12678 if (local_used >= local_sglist[i].len) {
12683 remote_used += cur_len;
12684 if (remote_used >= remote_sglist[j].len) {
12688 total_used += cur_len;
12690 if (total_used >= io->scsiio.kern_data_len)
12691 rq->callback = callback;
12694 printf("%s: %s: local %#x remote %#x size %d\n", __func__,
12695 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
12696 rq->local, rq->remote, rq->size);
12699 isc_ret = ctl_dt_single(rq);
12700 if (isc_ret > CTL_HA_STATUS_SUCCESS)
12703 if (isc_ret != CTL_HA_STATUS_WAIT) {
12712 ctl_datamove_remote_read(union ctl_io *io)
12718 * This will send an error to the other controller in the case of a
12721 retval = ctl_datamove_remote_sgl_setup(io);
12725 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12726 ctl_datamove_remote_read_cb);
12729 * Make sure we free memory if there was an error.. The
12730 * ctl_datamove_remote_xfer() function will send the
12731 * datamove done message, or call the callback with an
12732 * error if there is a problem.
12734 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12735 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12736 free(io->io_hdr.remote_sglist, M_CTL);
12737 io->io_hdr.remote_sglist = NULL;
12738 io->io_hdr.local_sglist = NULL;
12745 * Process a datamove request from the other controller. This is used for
12746 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
12747 * first. Once that is complete, the data gets DMAed into the remote
12748 * controller's memory. For reads, we DMA from the remote controller's
12749 * memory into our memory first, and then move it out to the FETD.
12752 ctl_datamove_remote(union ctl_io *io)
12755 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12757 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12758 ctl_failover_io(io, /*have_lock*/ 0);
12763 * Note that we look for an aborted I/O here, but don't do some of
12764 * the other checks that ctl_datamove() normally does.
12765 * We don't need to run the datamove delay code, since that should
12766 * have been done if need be on the other controller.
12768 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12769 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__,
12770 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12771 io->io_hdr.nexus.targ_port,
12772 io->io_hdr.nexus.targ_lun);
12773 io->io_hdr.port_status = 31338;
12774 ctl_send_datamove_done(io, /*have_lock*/ 0);
12778 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
12779 ctl_datamove_remote_write(io);
12780 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
12781 ctl_datamove_remote_read(io);
12783 io->io_hdr.port_status = 31339;
12784 ctl_send_datamove_done(io, /*have_lock*/ 0);
12789 ctl_process_done(union ctl_io *io)
12791 struct ctl_lun *lun;
12792 struct ctl_softc *softc = control_softc;
12793 void (*fe_done)(union ctl_io *io);
12794 union ctl_ha_msg msg;
12795 uint32_t targ_port = io->io_hdr.nexus.targ_port;
12797 CTL_DEBUG_PRINT(("ctl_process_done\n"));
12799 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0)
12800 fe_done = softc->ctl_ports[targ_port]->fe_done;
12805 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12810 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12811 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12813 sbuf_cat(&sb, path_str);
12814 switch (io->io_hdr.io_type) {
12816 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12817 sbuf_printf(&sb, "\n");
12818 sbuf_cat(&sb, path_str);
12819 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12820 io->scsiio.tag_num, io->scsiio.tag_type);
12823 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12824 "Tag Type: %d\n", io->taskio.task_action,
12825 io->taskio.tag_num, io->taskio.tag_type);
12828 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12829 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12832 sbuf_cat(&sb, path_str);
12833 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
12834 (intmax_t)time_uptime - io->io_hdr.start_time);
12836 printf("%s", sbuf_data(&sb));
12838 #endif /* CTL_TIME_IO */
12840 switch (io->io_hdr.io_type) {
12844 if (ctl_debug & CTL_DEBUG_INFO)
12845 ctl_io_error_print(io, NULL);
12846 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
12850 return (CTL_RETVAL_COMPLETE);
12852 panic("ctl_process_done: invalid io type %d\n",
12853 io->io_hdr.io_type);
12854 break; /* NOTREACHED */
12857 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12859 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
12860 io->io_hdr.nexus.targ_mapped_lun));
12864 mtx_lock(&lun->lun_lock);
12867 * Check to see if we have any errors to inject here. We only
12868 * inject errors for commands that don't already have errors set.
12870 if ((STAILQ_FIRST(&lun->error_list) != NULL) &&
12871 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
12872 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
12873 ctl_inject_error(lun, io);
12876 * XXX KDM how do we treat commands that aren't completed
12879 * XXX KDM should we also track I/O latency?
12881 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
12882 io->io_hdr.io_type == CTL_IO_SCSI) {
12884 struct bintime cur_bt;
12888 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12890 type = CTL_STATS_READ;
12891 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12893 type = CTL_STATS_WRITE;
12895 type = CTL_STATS_NO_IO;
12897 lun->stats.ports[targ_port].bytes[type] +=
12898 io->scsiio.kern_total_len;
12899 lun->stats.ports[targ_port].operations[type]++;
12901 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
12902 &io->io_hdr.dma_bt);
12903 lun->stats.ports[targ_port].num_dmas[type] +=
12904 io->io_hdr.num_dmas;
12905 getbintime(&cur_bt);
12906 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
12907 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
12912 * Remove this from the OOA queue.
12914 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
12916 if (TAILQ_EMPTY(&lun->ooa_queue))
12917 lun->last_busy = getsbinuptime();
12921 * Run through the blocked queue on this LUN and see if anything
12922 * has become unblocked, now that this transaction is done.
12924 ctl_check_blocked(lun);
12927 * If the LUN has been invalidated, free it if there is nothing
12928 * left on its OOA queue.
12930 if ((lun->flags & CTL_LUN_INVALID)
12931 && TAILQ_EMPTY(&lun->ooa_queue)) {
12932 mtx_unlock(&lun->lun_lock);
12933 mtx_lock(&softc->ctl_lock);
12935 mtx_unlock(&softc->ctl_lock);
12937 mtx_unlock(&lun->lun_lock);
12942 * If this command has been aborted, make sure we set the status
12943 * properly. The FETD is responsible for freeing the I/O and doing
12944 * whatever it needs to do to clean up its state.
12946 if (io->io_hdr.flags & CTL_FLAG_ABORT)
12947 ctl_set_task_aborted(&io->scsiio);
12950 * If enabled, print command error status.
12952 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
12953 (ctl_debug & CTL_DEBUG_INFO) != 0)
12954 ctl_io_error_print(io, NULL);
12957 * Tell the FETD or the other shelf controller we're done with this
12958 * command. Note that only SCSI commands get to this point. Task
12959 * management commands are completed above.
12961 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
12962 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) {
12963 memset(&msg, 0, sizeof(msg));
12964 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
12965 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12966 msg.hdr.nexus = io->io_hdr.nexus;
12967 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12968 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data),
12971 if ((softc->ha_mode == CTL_HA_MODE_XFER)
12972 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12973 memset(&msg, 0, sizeof(msg));
12974 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
12975 msg.hdr.original_sc = io->io_hdr.original_sc;
12976 msg.hdr.nexus = io->io_hdr.nexus;
12977 msg.hdr.status = io->io_hdr.status;
12978 msg.scsi.scsi_status = io->scsiio.scsi_status;
12979 msg.scsi.tag_num = io->scsiio.tag_num;
12980 msg.scsi.tag_type = io->scsiio.tag_type;
12981 msg.scsi.sense_len = io->scsiio.sense_len;
12982 msg.scsi.sense_residual = io->scsiio.sense_residual;
12983 msg.scsi.residual = io->scsiio.residual;
12984 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12985 io->scsiio.sense_len);
12987 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12988 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12989 msg.scsi.sense_len, M_WAITOK);
12994 return (CTL_RETVAL_COMPLETE);
12999 * Front end should call this if it doesn't do autosense. When the request
13000 * sense comes back in from the initiator, we'll dequeue this and send it.
13003 ctl_queue_sense(union ctl_io *io)
13005 struct ctl_lun *lun;
13006 struct ctl_port *port;
13007 struct ctl_softc *softc;
13008 uint32_t initidx, targ_lun;
13010 softc = control_softc;
13012 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13015 * LUN lookup will likely move to the ctl_work_thread() once we
13016 * have our new queueing infrastructure (that doesn't put things on
13017 * a per-LUN queue initially). That is so that we can handle
13018 * things like an INQUIRY to a LUN that we don't have enabled. We
13019 * can't deal with that right now.
13021 mtx_lock(&softc->ctl_lock);
13024 * If we don't have a LUN for this, just toss the sense
13027 port = ctl_io_port(&ctsio->io_hdr);
13028 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13029 if ((targ_lun < CTL_MAX_LUNS)
13030 && (softc->ctl_luns[targ_lun] != NULL))
13031 lun = softc->ctl_luns[targ_lun];
13035 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13037 mtx_lock(&lun->lun_lock);
13039 * Already have CA set for this LUN...toss the sense information.
13041 if (ctl_is_set(lun->have_ca, initidx)) {
13042 mtx_unlock(&lun->lun_lock);
13046 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data,
13047 MIN(sizeof(lun->pending_sense[initidx]),
13048 sizeof(io->scsiio.sense_data)));
13049 ctl_set_mask(lun->have_ca, initidx);
13050 mtx_unlock(&lun->lun_lock);
13053 mtx_unlock(&softc->ctl_lock);
13057 return (CTL_RETVAL_COMPLETE);
13062 * Primary command inlet from frontend ports. All SCSI and task I/O
13063 * requests must go through this function.
13066 ctl_queue(union ctl_io *io)
13068 struct ctl_port *port;
13070 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13073 io->io_hdr.start_time = time_uptime;
13074 getbintime(&io->io_hdr.start_bt);
13075 #endif /* CTL_TIME_IO */
13077 /* Map FE-specific LUN ID into global one. */
13078 port = ctl_io_port(&io->io_hdr);
13079 io->io_hdr.nexus.targ_mapped_lun =
13080 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13082 switch (io->io_hdr.io_type) {
13085 if (ctl_debug & CTL_DEBUG_CDB)
13087 ctl_enqueue_incoming(io);
13090 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13094 return (CTL_RETVAL_COMPLETE);
13097 #ifdef CTL_IO_DELAY
13099 ctl_done_timer_wakeup(void *arg)
13103 io = (union ctl_io *)arg;
13106 #endif /* CTL_IO_DELAY */
13109 ctl_done(union ctl_io *io)
13113 * Enable this to catch duplicate completion issues.
13116 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13117 printf("%s: type %d msg %d cdb %x iptl: "
13118 "%u:%u:%u tag 0x%04x "
13119 "flag %#x status %x\n",
13121 io->io_hdr.io_type,
13122 io->io_hdr.msg_type,
13124 io->io_hdr.nexus.initid,
13125 io->io_hdr.nexus.targ_port,
13126 io->io_hdr.nexus.targ_lun,
13127 (io->io_hdr.io_type ==
13129 io->taskio.tag_num :
13130 io->scsiio.tag_num,
13132 io->io_hdr.status);
13134 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13138 * This is an internal copy of an I/O, and should not go through
13139 * the normal done processing logic.
13141 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13144 #ifdef CTL_IO_DELAY
13145 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13146 struct ctl_lun *lun;
13148 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13150 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13152 struct ctl_lun *lun;
13154 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13157 && (lun->delay_info.done_delay > 0)) {
13159 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
13160 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13161 callout_reset(&io->io_hdr.delay_callout,
13162 lun->delay_info.done_delay * hz,
13163 ctl_done_timer_wakeup, io);
13164 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13165 lun->delay_info.done_delay = 0;
13169 #endif /* CTL_IO_DELAY */
13171 ctl_enqueue_done(io);
13175 ctl_work_thread(void *arg)
13177 struct ctl_thread *thr = (struct ctl_thread *)arg;
13178 struct ctl_softc *softc = thr->ctl_softc;
13182 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13188 * We handle the queues in this order:
13190 * - done queue (to free up resources, unblock other commands)
13194 * If those queues are empty, we break out of the loop and
13197 mtx_lock(&thr->queue_lock);
13198 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13200 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13201 mtx_unlock(&thr->queue_lock);
13202 ctl_handle_isc(io);
13205 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13207 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13208 /* clear any blocked commands, call fe_done */
13209 mtx_unlock(&thr->queue_lock);
13210 retval = ctl_process_done(io);
13213 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13215 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13216 mtx_unlock(&thr->queue_lock);
13217 if (io->io_hdr.io_type == CTL_IO_TASK)
13220 ctl_scsiio_precheck(softc, &io->scsiio);
13223 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13225 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13226 mtx_unlock(&thr->queue_lock);
13227 retval = ctl_scsiio(&io->scsiio);
13228 if (retval != CTL_RETVAL_COMPLETE)
13229 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13233 /* Sleep until we have something to do. */
13234 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13239 ctl_lun_thread(void *arg)
13241 struct ctl_softc *softc = (struct ctl_softc *)arg;
13242 struct ctl_be_lun *be_lun;
13245 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13249 mtx_lock(&softc->ctl_lock);
13250 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13251 if (be_lun != NULL) {
13252 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13253 mtx_unlock(&softc->ctl_lock);
13254 ctl_create_lun(be_lun);
13258 /* Sleep until we have something to do. */
13259 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13260 PDROP | PRIBIO, "-", 0);
13265 ctl_thresh_thread(void *arg)
13267 struct ctl_softc *softc = (struct ctl_softc *)arg;
13268 struct ctl_lun *lun;
13269 struct ctl_be_lun *be_lun;
13270 struct scsi_da_rw_recovery_page *rwpage;
13271 struct ctl_logical_block_provisioning_page *page;
13273 union ctl_ha_msg msg;
13274 uint64_t thres, val;
13277 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13280 mtx_lock(&softc->ctl_lock);
13281 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13282 be_lun = lun->be_lun;
13283 if ((lun->flags & CTL_LUN_DISABLED) ||
13284 (lun->flags & CTL_LUN_OFFLINE) ||
13285 lun->backend->lun_attr == NULL)
13287 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
13288 softc->ha_mode == CTL_HA_MODE_XFER)
13290 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT];
13291 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0)
13294 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT];
13295 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13296 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13298 thres = scsi_4btoul(page->descr[i].count);
13299 thres <<= CTL_LBP_EXPONENT;
13300 switch (page->descr[i].resource) {
13302 attr = "blocksavail";
13305 attr = "blocksused";
13308 attr = "poolblocksavail";
13311 attr = "poolblocksused";
13316 mtx_unlock(&softc->ctl_lock); // XXX
13317 val = lun->backend->lun_attr(
13318 lun->be_lun->be_lun, attr);
13319 mtx_lock(&softc->ctl_lock);
13320 if (val == UINT64_MAX)
13322 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13323 == SLBPPD_ARMING_INC)
13324 e |= (val >= thres);
13326 e |= (val <= thres);
13328 mtx_lock(&lun->lun_lock);
13330 if (lun->lasttpt == 0 ||
13331 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13332 lun->lasttpt = time_uptime;
13333 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13339 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13342 mtx_unlock(&lun->lun_lock);
13344 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
13345 /* Send msg to other side. */
13346 bzero(&msg.ua, sizeof(msg.ua));
13347 msg.hdr.msg_type = CTL_MSG_UA;
13348 msg.hdr.nexus.initid = -1;
13349 msg.hdr.nexus.targ_port = -1;
13350 msg.hdr.nexus.targ_lun = lun->lun;
13351 msg.hdr.nexus.targ_mapped_lun = lun->lun;
13353 msg.ua.ua_set = (set > 0);
13354 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES;
13355 mtx_unlock(&softc->ctl_lock); // XXX
13356 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13357 sizeof(msg.ua), M_WAITOK);
13358 mtx_lock(&softc->ctl_lock);
13361 mtx_unlock(&softc->ctl_lock);
13362 pause("-", CTL_LBP_PERIOD * hz);
13367 ctl_enqueue_incoming(union ctl_io *io)
13369 struct ctl_softc *softc = control_softc;
13370 struct ctl_thread *thr;
13373 idx = (io->io_hdr.nexus.targ_port * 127 +
13374 io->io_hdr.nexus.initid) % worker_threads;
13375 thr = &softc->threads[idx];
13376 mtx_lock(&thr->queue_lock);
13377 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13378 mtx_unlock(&thr->queue_lock);
13383 ctl_enqueue_rtr(union ctl_io *io)
13385 struct ctl_softc *softc = control_softc;
13386 struct ctl_thread *thr;
13388 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13389 mtx_lock(&thr->queue_lock);
13390 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13391 mtx_unlock(&thr->queue_lock);
13396 ctl_enqueue_done(union ctl_io *io)
13398 struct ctl_softc *softc = control_softc;
13399 struct ctl_thread *thr;
13401 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13402 mtx_lock(&thr->queue_lock);
13403 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13404 mtx_unlock(&thr->queue_lock);
13409 ctl_enqueue_isc(union ctl_io *io)
13411 struct ctl_softc *softc = control_softc;
13412 struct ctl_thread *thr;
13414 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13415 mtx_lock(&thr->queue_lock);
13416 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13417 mtx_unlock(&thr->queue_lock);