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 if (port->init_devid)
617 i += port->init_devid->len;
618 msg = malloc(i, M_CTL, M_WAITOK);
619 bzero(&msg->port, sizeof(msg->port));
620 msg->hdr.msg_type = CTL_MSG_PORT_SYNC;
621 msg->hdr.nexus.targ_port = port->targ_port;
622 msg->port.port_type = port->port_type;
623 msg->port.physical_port = port->physical_port;
624 msg->port.virtual_port = port->virtual_port;
625 msg->port.status = port->status;
627 msg->port.name_len = sprintf(&msg->port.data[i],
628 "%d:%s", softc->ha_id, port->port_name) + 1;
629 i += msg->port.name_len;
631 msg->port.lun_map_len = sizeof(uint32_t) * CTL_MAX_LUNS;
632 memcpy(&msg->port.data[i], port->lun_map,
633 msg->port.lun_map_len);
634 i += msg->port.lun_map_len;
636 if (port->port_devid) {
637 msg->port.port_devid_len = port->port_devid->len;
638 memcpy(&msg->port.data[i], port->port_devid->data,
639 msg->port.port_devid_len);
640 i += msg->port.port_devid_len;
642 if (port->target_devid) {
643 msg->port.target_devid_len = port->target_devid->len;
644 memcpy(&msg->port.data[i], port->target_devid->data,
645 msg->port.target_devid_len);
646 i += msg->port.target_devid_len;
648 if (port->init_devid) {
649 msg->port.init_devid_len = port->init_devid->len;
650 memcpy(&msg->port.data[i], port->init_devid->data,
651 msg->port.init_devid_len);
652 i += msg->port.init_devid_len;
654 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
660 ctl_isc_announce_iid(struct ctl_port *port, int iid)
662 struct ctl_softc *softc = control_softc;
663 union ctl_ha_msg *msg;
666 if (port->targ_port < softc->port_min ||
667 port->targ_port >= softc->port_max ||
668 softc->ha_link != CTL_HA_LINK_ONLINE)
670 mtx_lock(&softc->ctl_lock);
671 i = sizeof(msg->iid);
673 if (port->wwpn_iid[iid].name)
674 l = strlen(port->wwpn_iid[iid].name) + 1;
676 msg = malloc(i, M_CTL, M_NOWAIT);
678 mtx_unlock(&softc->ctl_lock);
681 bzero(&msg->iid, sizeof(msg->iid));
682 msg->hdr.msg_type = CTL_MSG_IID_SYNC;
683 msg->hdr.nexus.targ_port = port->targ_port;
684 msg->hdr.nexus.initid = iid;
685 msg->iid.in_use = port->wwpn_iid[iid].in_use;
686 msg->iid.name_len = l;
687 msg->iid.wwpn = port->wwpn_iid[iid].wwpn;
688 if (port->wwpn_iid[iid].name)
689 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l);
690 mtx_unlock(&softc->ctl_lock);
691 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT);
696 ctl_isc_ha_link_up(struct ctl_softc *softc)
698 struct ctl_port *port;
702 STAILQ_FOREACH(port, &softc->port_list, links) {
703 ctl_isc_announce_port(port);
704 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
705 if (port->wwpn_iid[i].in_use)
706 ctl_isc_announce_iid(port, i);
709 STAILQ_FOREACH(lun, &softc->lun_list, links)
710 ctl_isc_announce_lun(lun);
714 ctl_isc_ha_link_down(struct ctl_softc *softc)
716 struct ctl_port *port;
721 mtx_lock(&softc->ctl_lock);
722 STAILQ_FOREACH(lun, &softc->lun_list, links) {
723 mtx_lock(&lun->lun_lock);
724 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) {
725 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
726 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
728 mtx_unlock(&lun->lun_lock);
730 mtx_unlock(&softc->ctl_lock);
731 io = ctl_alloc_io(softc->othersc_pool);
732 mtx_lock(&softc->ctl_lock);
734 io->io_hdr.msg_type = CTL_MSG_FAILOVER;
735 io->io_hdr.nexus.targ_mapped_lun = lun->lun;
739 STAILQ_FOREACH(port, &softc->port_list, links) {
740 if (port->targ_port >= softc->port_min &&
741 port->targ_port < softc->port_max)
743 port->status &= ~CTL_PORT_STATUS_ONLINE;
744 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
745 port->wwpn_iid[i].in_use = 0;
746 free(port->wwpn_iid[i].name, M_CTL);
747 port->wwpn_iid[i].name = NULL;
750 mtx_unlock(&softc->ctl_lock);
754 ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
757 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus);
759 mtx_lock(&softc->ctl_lock);
760 if (msg->hdr.nexus.targ_lun < CTL_MAX_LUNS &&
761 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) != NULL) {
762 mtx_lock(&lun->lun_lock);
763 mtx_unlock(&softc->ctl_lock);
764 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES &&
766 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8);
767 if (msg->ua.ua_all) {
769 ctl_est_ua_all(lun, iid, msg->ua.ua_type);
771 ctl_clr_ua_all(lun, iid, msg->ua.ua_type);
774 ctl_est_ua(lun, iid, msg->ua.ua_type);
776 ctl_clr_ua(lun, iid, msg->ua.ua_type);
778 mtx_unlock(&lun->lun_lock);
780 mtx_unlock(&softc->ctl_lock);
784 ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
787 struct ctl_ha_msg_lun_pr_key pr_key;
789 ctl_lun_flags oflags;
792 targ_lun = msg->hdr.nexus.targ_mapped_lun;
793 mtx_lock(&softc->ctl_lock);
794 if ((targ_lun >= CTL_MAX_LUNS) ||
795 ((lun = softc->ctl_luns[targ_lun]) == NULL)) {
796 mtx_unlock(&softc->ctl_lock);
799 mtx_lock(&lun->lun_lock);
800 mtx_unlock(&softc->ctl_lock);
801 if (lun->flags & CTL_LUN_DISABLED) {
802 mtx_unlock(&lun->lun_lock);
805 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0;
806 if (msg->lun.lun_devid_len != i || (i > 0 &&
807 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) {
808 mtx_unlock(&lun->lun_lock);
809 printf("%s: Received conflicting HA LUN %d\n",
810 __func__, msg->hdr.nexus.targ_lun);
813 /* Record whether peer is primary. */
815 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) &&
816 (msg->lun.flags & CTL_LUN_DISABLED) == 0)
817 lun->flags |= CTL_LUN_PEER_SC_PRIMARY;
819 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
820 if (oflags != lun->flags)
821 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
823 /* If peer is primary and we are not -- use data */
824 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
825 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) {
826 lun->PRGeneration = msg->lun.pr_generation;
827 lun->pr_res_idx = msg->lun.pr_res_idx;
828 lun->res_type = msg->lun.pr_res_type;
829 lun->pr_key_count = msg->lun.pr_key_count;
830 for (k = 0; k < CTL_MAX_INITIATORS; k++)
831 ctl_clr_prkey(lun, k);
832 for (k = 0; k < msg->lun.pr_key_count; k++) {
833 memcpy(&pr_key, &msg->lun.data[i],
835 ctl_alloc_prkey(lun, pr_key.pr_iid);
836 ctl_set_prkey(lun, pr_key.pr_iid,
842 mtx_unlock(&lun->lun_lock);
843 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n",
844 __func__, msg->hdr.nexus.targ_lun,
845 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ?
846 "primary" : "secondary"));
848 /* If we are primary but peer doesn't know -- notify */
849 if ((lun->flags & CTL_LUN_PRIMARY_SC) &&
850 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0)
851 ctl_isc_announce_lun(lun);
856 ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
858 struct ctl_port *port;
862 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
864 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__,
865 msg->hdr.nexus.targ_port));
867 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO);
868 port->frontend = &ha_frontend;
869 port->targ_port = msg->hdr.nexus.targ_port;
870 } else if (port->frontend == &ha_frontend) {
871 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__,
872 msg->hdr.nexus.targ_port));
875 printf("%s: Received conflicting HA port %d\n",
876 __func__, msg->hdr.nexus.targ_port);
879 port->port_type = msg->port.port_type;
880 port->physical_port = msg->port.physical_port;
881 port->virtual_port = msg->port.virtual_port;
882 port->status = msg->port.status;
884 free(port->port_name, M_CTL);
885 port->port_name = strndup(&msg->port.data[i], msg->port.name_len,
887 i += msg->port.name_len;
888 if (msg->port.lun_map_len != 0) {
889 if (port->lun_map == NULL)
890 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS,
892 memcpy(port->lun_map, &msg->port.data[i],
893 sizeof(uint32_t) * CTL_MAX_LUNS);
894 i += msg->port.lun_map_len;
896 free(port->lun_map, M_CTL);
897 port->lun_map = NULL;
899 if (msg->port.port_devid_len != 0) {
900 if (port->port_devid == NULL ||
901 port->port_devid->len != msg->port.port_devid_len) {
902 free(port->port_devid, M_CTL);
903 port->port_devid = malloc(sizeof(struct ctl_devid) +
904 msg->port.port_devid_len, M_CTL, M_WAITOK);
906 memcpy(port->port_devid->data, &msg->port.data[i],
907 msg->port.port_devid_len);
908 port->port_devid->len = msg->port.port_devid_len;
909 i += msg->port.port_devid_len;
911 free(port->port_devid, M_CTL);
912 port->port_devid = NULL;
914 if (msg->port.target_devid_len != 0) {
915 if (port->target_devid == NULL ||
916 port->target_devid->len != msg->port.target_devid_len) {
917 free(port->target_devid, M_CTL);
918 port->target_devid = malloc(sizeof(struct ctl_devid) +
919 msg->port.target_devid_len, M_CTL, M_WAITOK);
921 memcpy(port->target_devid->data, &msg->port.data[i],
922 msg->port.target_devid_len);
923 port->target_devid->len = msg->port.target_devid_len;
924 i += msg->port.target_devid_len;
926 free(port->target_devid, M_CTL);
927 port->target_devid = NULL;
929 if (msg->port.init_devid_len != 0) {
930 if (port->init_devid == NULL ||
931 port->init_devid->len != msg->port.init_devid_len) {
932 free(port->init_devid, M_CTL);
933 port->init_devid = malloc(sizeof(struct ctl_devid) +
934 msg->port.init_devid_len, M_CTL, M_WAITOK);
936 memcpy(port->init_devid->data, &msg->port.data[i],
937 msg->port.init_devid_len);
938 port->init_devid->len = msg->port.init_devid_len;
939 i += msg->port.init_devid_len;
941 free(port->init_devid, M_CTL);
942 port->init_devid = NULL;
945 if (ctl_port_register(port) != 0) {
946 printf("%s: ctl_port_register() failed with error\n",
950 mtx_lock(&softc->ctl_lock);
951 STAILQ_FOREACH(lun, &softc->lun_list, links) {
952 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
954 mtx_lock(&lun->lun_lock);
955 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
956 mtx_unlock(&lun->lun_lock);
958 mtx_unlock(&softc->ctl_lock);
962 ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
964 struct ctl_port *port;
967 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
969 printf("%s: Received IID for unknown port %d\n",
970 __func__, msg->hdr.nexus.targ_port);
973 iid = msg->hdr.nexus.initid;
974 port->wwpn_iid[iid].in_use = msg->iid.in_use;
975 port->wwpn_iid[iid].wwpn = msg->iid.wwpn;
976 free(port->wwpn_iid[iid].name, M_CTL);
977 if (msg->iid.name_len) {
978 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0],
979 msg->iid.name_len, M_CTL);
981 port->wwpn_iid[iid].name = NULL;
985 * ISC (Inter Shelf Communication) event handler. Events from the HA
986 * subsystem come in here.
989 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
991 struct ctl_softc *softc;
993 struct ctl_prio *presio;
994 ctl_ha_status isc_status;
996 softc = control_softc;
997 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event));
998 if (event == CTL_HA_EVT_MSG_RECV) {
999 union ctl_ha_msg *msg, msgbuf;
1001 if (param > sizeof(msgbuf))
1002 msg = malloc(param, M_CTL, M_WAITOK);
1005 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param,
1007 if (isc_status != CTL_HA_STATUS_SUCCESS) {
1008 printf("%s: Error receiving message: %d\n",
1009 __func__, isc_status);
1015 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type));
1016 switch (msg->hdr.msg_type) {
1017 case CTL_MSG_SERIALIZE:
1018 io = ctl_alloc_io(softc->othersc_pool);
1020 // populate ctsio from msg
1021 io->io_hdr.io_type = CTL_IO_SCSI;
1022 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
1023 io->io_hdr.original_sc = msg->hdr.original_sc;
1024 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
1027 * If we're in serialization-only mode, we don't
1028 * want to go through full done processing. Thus
1031 * XXX KDM add another flag that is more specific.
1033 if (softc->ha_mode != CTL_HA_MODE_XFER)
1034 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
1035 io->io_hdr.nexus = msg->hdr.nexus;
1037 printf("port %u, iid %u, lun %u\n",
1038 io->io_hdr.nexus.targ_port,
1039 io->io_hdr.nexus.initid,
1040 io->io_hdr.nexus.targ_lun);
1042 io->scsiio.tag_num = msg->scsi.tag_num;
1043 io->scsiio.tag_type = msg->scsi.tag_type;
1045 io->io_hdr.start_time = time_uptime;
1046 getbintime(&io->io_hdr.start_bt);
1047 #endif /* CTL_TIME_IO */
1048 io->scsiio.cdb_len = msg->scsi.cdb_len;
1049 memcpy(io->scsiio.cdb, msg->scsi.cdb,
1051 if (softc->ha_mode == CTL_HA_MODE_XFER) {
1052 const struct ctl_cmd_entry *entry;
1054 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
1055 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
1057 entry->flags & CTL_FLAG_DATA_MASK;
1059 ctl_enqueue_isc(io);
1062 /* Performed on the Originating SC, XFER mode only */
1063 case CTL_MSG_DATAMOVE: {
1064 struct ctl_sg_entry *sgl;
1067 io = msg->hdr.original_sc;
1069 printf("%s: original_sc == NULL!\n", __func__);
1070 /* XXX KDM do something here */
1073 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
1074 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1076 * Keep track of this, we need to send it back over
1077 * when the datamove is complete.
1079 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1080 if (msg->hdr.status == CTL_SUCCESS)
1081 io->io_hdr.status = msg->hdr.status;
1083 if (msg->dt.sg_sequence == 0) {
1084 i = msg->dt.kern_sg_entries +
1085 msg->dt.kern_data_len /
1086 CTL_HA_DATAMOVE_SEGMENT + 1;
1087 sgl = malloc(sizeof(*sgl) * i, M_CTL,
1089 io->io_hdr.remote_sglist = sgl;
1090 io->io_hdr.local_sglist =
1091 &sgl[msg->dt.kern_sg_entries];
1093 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
1095 io->scsiio.kern_sg_entries =
1096 msg->dt.kern_sg_entries;
1097 io->scsiio.rem_sg_entries =
1098 msg->dt.kern_sg_entries;
1099 io->scsiio.kern_data_len =
1100 msg->dt.kern_data_len;
1101 io->scsiio.kern_total_len =
1102 msg->dt.kern_total_len;
1103 io->scsiio.kern_data_resid =
1104 msg->dt.kern_data_resid;
1105 io->scsiio.kern_rel_offset =
1106 msg->dt.kern_rel_offset;
1107 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR;
1108 io->io_hdr.flags |= msg->dt.flags &
1111 sgl = (struct ctl_sg_entry *)
1112 io->scsiio.kern_data_ptr;
1114 for (i = msg->dt.sent_sg_entries, j = 0;
1115 i < (msg->dt.sent_sg_entries +
1116 msg->dt.cur_sg_entries); i++, j++) {
1117 sgl[i].addr = msg->dt.sg_list[j].addr;
1118 sgl[i].len = msg->dt.sg_list[j].len;
1121 printf("%s: DATAMOVE: %p,%lu j=%d, i=%d\n",
1122 __func__, sgl[i].addr, sgl[i].len, j, i);
1127 * If this is the last piece of the I/O, we've got
1128 * the full S/G list. Queue processing in the thread.
1129 * Otherwise wait for the next piece.
1131 if (msg->dt.sg_last != 0)
1132 ctl_enqueue_isc(io);
1135 /* Performed on the Serializing (primary) SC, XFER mode only */
1136 case CTL_MSG_DATAMOVE_DONE: {
1137 if (msg->hdr.serializing_sc == NULL) {
1138 printf("%s: serializing_sc == NULL!\n",
1140 /* XXX KDM now what? */
1144 * We grab the sense information here in case
1145 * there was a failure, so we can return status
1146 * back to the initiator.
1148 io = msg->hdr.serializing_sc;
1149 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
1150 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1151 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1152 io->io_hdr.port_status = msg->scsi.fetd_status;
1153 io->scsiio.residual = msg->scsi.residual;
1154 if (msg->hdr.status != CTL_STATUS_NONE) {
1155 io->io_hdr.status = msg->hdr.status;
1156 io->scsiio.scsi_status = msg->scsi.scsi_status;
1157 io->scsiio.sense_len = msg->scsi.sense_len;
1158 io->scsiio.sense_residual =msg->scsi.sense_residual;
1159 memcpy(&io->scsiio.sense_data,
1160 &msg->scsi.sense_data,
1161 msg->scsi.sense_len);
1162 if (msg->hdr.status == CTL_SUCCESS)
1163 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1165 ctl_enqueue_isc(io);
1169 /* Preformed on Originating SC, SER_ONLY mode */
1171 io = msg->hdr.original_sc;
1173 printf("%s: original_sc == NULL!\n",
1177 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1178 io->io_hdr.msg_type = CTL_MSG_R2R;
1179 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1180 ctl_enqueue_isc(io);
1184 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
1186 * Performed on the Originating (i.e. secondary) SC in XFER
1189 case CTL_MSG_FINISH_IO:
1190 if (softc->ha_mode == CTL_HA_MODE_XFER)
1191 ctl_isc_handler_finish_xfer(softc, msg);
1193 ctl_isc_handler_finish_ser_only(softc, msg);
1196 /* Preformed on Originating SC */
1197 case CTL_MSG_BAD_JUJU:
1198 io = msg->hdr.original_sc;
1200 printf("%s: Bad JUJU!, original_sc is NULL!\n",
1204 ctl_copy_sense_data(msg, io);
1206 * IO should have already been cleaned up on other
1207 * SC so clear this flag so we won't send a message
1208 * back to finish the IO there.
1210 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
1211 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1213 /* io = msg->hdr.serializing_sc; */
1214 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
1215 ctl_enqueue_isc(io);
1218 /* Handle resets sent from the other side */
1219 case CTL_MSG_MANAGE_TASKS: {
1220 struct ctl_taskio *taskio;
1221 taskio = (struct ctl_taskio *)ctl_alloc_io(
1222 softc->othersc_pool);
1223 ctl_zero_io((union ctl_io *)taskio);
1224 taskio->io_hdr.io_type = CTL_IO_TASK;
1225 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1226 taskio->io_hdr.nexus = msg->hdr.nexus;
1227 taskio->task_action = msg->task.task_action;
1228 taskio->tag_num = msg->task.tag_num;
1229 taskio->tag_type = msg->task.tag_type;
1231 taskio->io_hdr.start_time = time_uptime;
1232 getbintime(&taskio->io_hdr.start_bt);
1233 #endif /* CTL_TIME_IO */
1234 ctl_run_task((union ctl_io *)taskio);
1237 /* Persistent Reserve action which needs attention */
1238 case CTL_MSG_PERS_ACTION:
1239 presio = (struct ctl_prio *)ctl_alloc_io(
1240 softc->othersc_pool);
1241 ctl_zero_io((union ctl_io *)presio);
1242 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
1243 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1244 presio->io_hdr.nexus = msg->hdr.nexus;
1245 presio->pr_msg = msg->pr;
1246 ctl_enqueue_isc((union ctl_io *)presio);
1249 ctl_isc_ua(softc, msg, param);
1251 case CTL_MSG_PORT_SYNC:
1252 ctl_isc_port_sync(softc, msg, param);
1254 case CTL_MSG_LUN_SYNC:
1255 ctl_isc_lun_sync(softc, msg, param);
1257 case CTL_MSG_IID_SYNC:
1258 ctl_isc_iid_sync(softc, msg, param);
1261 printf("Received HA message of unknown type %d\n",
1267 } else if (event == CTL_HA_EVT_LINK_CHANGE) {
1268 printf("CTL: HA link status changed from %d to %d\n",
1269 softc->ha_link, param);
1270 if (param == softc->ha_link)
1272 if (softc->ha_link == CTL_HA_LINK_ONLINE) {
1273 softc->ha_link = param;
1274 ctl_isc_ha_link_down(softc);
1276 softc->ha_link = param;
1277 if (softc->ha_link == CTL_HA_LINK_ONLINE)
1278 ctl_isc_ha_link_up(softc);
1282 printf("ctl_isc_event_handler: Unknown event %d\n", event);
1288 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
1291 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data,
1292 src->scsi.sense_len);
1293 dest->scsiio.scsi_status = src->scsi.scsi_status;
1294 dest->scsiio.sense_len = src->scsi.sense_len;
1295 dest->io_hdr.status = src->hdr.status;
1299 ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest)
1302 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data,
1303 src->scsiio.sense_len);
1304 dest->scsi.scsi_status = src->scsiio.scsi_status;
1305 dest->scsi.sense_len = src->scsiio.sense_len;
1306 dest->hdr.status = src->io_hdr.status;
1310 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1312 struct ctl_softc *softc = lun->ctl_softc;
1315 if (initidx < softc->init_min || initidx >= softc->init_max)
1317 mtx_assert(&lun->lun_lock, MA_OWNED);
1318 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1321 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua;
1325 ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua)
1329 mtx_assert(&lun->lun_lock, MA_OWNED);
1330 if (lun->pending_ua[port] == NULL)
1332 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1333 if (port * CTL_MAX_INIT_PER_PORT + i == except)
1335 lun->pending_ua[port][i] |= ua;
1340 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1342 struct ctl_softc *softc = lun->ctl_softc;
1345 mtx_assert(&lun->lun_lock, MA_OWNED);
1346 for (i = softc->port_min; i < softc->port_max; i++)
1347 ctl_est_ua_port(lun, i, except, ua);
1351 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1353 struct ctl_softc *softc = lun->ctl_softc;
1356 if (initidx < softc->init_min || initidx >= softc->init_max)
1358 mtx_assert(&lun->lun_lock, MA_OWNED);
1359 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1362 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua;
1366 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1368 struct ctl_softc *softc = lun->ctl_softc;
1371 mtx_assert(&lun->lun_lock, MA_OWNED);
1372 for (i = softc->port_min; i < softc->port_max; i++) {
1373 if (lun->pending_ua[i] == NULL)
1375 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
1376 if (i * CTL_MAX_INIT_PER_PORT + j == except)
1378 lun->pending_ua[i][j] &= ~ua;
1384 ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx,
1385 ctl_ua_type ua_type)
1387 struct ctl_lun *lun;
1389 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
1390 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) {
1391 mtx_lock(&lun->lun_lock);
1392 ctl_clr_ua(lun, initidx, ua_type);
1393 mtx_unlock(&lun->lun_lock);
1398 ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS)
1400 struct ctl_softc *softc = (struct ctl_softc *)arg1;
1401 struct ctl_lun *lun;
1402 struct ctl_lun_req ireq;
1405 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1;
1406 error = sysctl_handle_int(oidp, &value, 0, req);
1407 if ((error != 0) || (req->newptr == NULL))
1410 mtx_lock(&softc->ctl_lock);
1412 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1414 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
1415 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1416 mtx_unlock(&softc->ctl_lock);
1417 bzero(&ireq, sizeof(ireq));
1418 ireq.reqtype = CTL_LUNREQ_MODIFY;
1419 ireq.reqdata.modify.lun_id = lun->lun;
1420 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0,
1422 if (ireq.status != CTL_LUN_OK) {
1423 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n",
1424 __func__, ireq.status, ireq.error_str);
1426 mtx_lock(&softc->ctl_lock);
1428 mtx_unlock(&softc->ctl_lock);
1435 struct ctl_softc *softc;
1437 int i, error, retval;
1440 control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1442 softc = control_softc;
1444 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
1447 softc->dev->si_drv1 = softc;
1449 sysctl_ctx_init(&softc->sysctl_ctx);
1450 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1451 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1452 CTLFLAG_RD, 0, "CAM Target Layer");
1454 if (softc->sysctl_tree == NULL) {
1455 printf("%s: unable to allocate sysctl tree\n", __func__);
1456 destroy_dev(softc->dev);
1457 free(control_softc, M_DEVBUF);
1458 control_softc = NULL;
1462 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1463 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
1464 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1465 softc->open_count = 0;
1468 * Default to actually sending a SYNCHRONIZE CACHE command down to
1471 softc->flags = CTL_FLAG_REAL_SYNC;
1473 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1474 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0,
1475 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)");
1478 * In Copan's HA scheme, the "master" and "slave" roles are
1479 * figured out through the slot the controller is in. Although it
1480 * is an active/active system, someone has to be in charge.
1482 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1483 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1484 "HA head ID (0 - no HA)");
1485 if (softc->ha_id == 0 || softc->ha_id > NUM_TARGET_PORT_GROUPS) {
1486 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1487 softc->is_single = 1;
1488 softc->port_cnt = CTL_MAX_PORTS;
1489 softc->port_min = 0;
1491 softc->port_cnt = CTL_MAX_PORTS / NUM_TARGET_PORT_GROUPS;
1492 softc->port_min = (softc->ha_id - 1) * softc->port_cnt;
1494 softc->port_max = softc->port_min + softc->port_cnt;
1495 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT;
1496 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT;
1498 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1499 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0,
1500 "HA link state (0 - offline, 1 - unknown, 2 - online)");
1502 STAILQ_INIT(&softc->lun_list);
1503 STAILQ_INIT(&softc->pending_lun_queue);
1504 STAILQ_INIT(&softc->fe_list);
1505 STAILQ_INIT(&softc->port_list);
1506 STAILQ_INIT(&softc->be_list);
1507 ctl_tpc_init(softc);
1509 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
1512 printf("ctl: can't allocate %d entry other SC pool, "
1513 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1516 softc->othersc_pool = other_pool;
1518 if (worker_threads <= 0)
1519 worker_threads = max(1, mp_ncpus / 4);
1520 if (worker_threads > CTL_MAX_THREADS)
1521 worker_threads = CTL_MAX_THREADS;
1523 for (i = 0; i < worker_threads; i++) {
1524 struct ctl_thread *thr = &softc->threads[i];
1526 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1527 thr->ctl_softc = softc;
1528 STAILQ_INIT(&thr->incoming_queue);
1529 STAILQ_INIT(&thr->rtr_queue);
1530 STAILQ_INIT(&thr->done_queue);
1531 STAILQ_INIT(&thr->isc_queue);
1533 error = kproc_kthread_add(ctl_work_thread, thr,
1534 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1536 printf("error creating CTL work thread!\n");
1537 ctl_pool_free(other_pool);
1541 error = kproc_kthread_add(ctl_lun_thread, softc,
1542 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
1544 printf("error creating CTL lun thread!\n");
1545 ctl_pool_free(other_pool);
1548 error = kproc_kthread_add(ctl_thresh_thread, softc,
1549 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh");
1551 printf("error creating CTL threshold thread!\n");
1552 ctl_pool_free(other_pool);
1556 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1557 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN,
1558 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head");
1560 if (softc->is_single == 0) {
1561 ctl_frontend_register(&ha_frontend);
1562 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) {
1563 printf("ctl_init: ctl_ha_msg_init failed.\n");
1564 softc->is_single = 1;
1566 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
1567 != CTL_HA_STATUS_SUCCESS) {
1568 printf("ctl_init: ctl_ha_msg_register failed.\n");
1569 softc->is_single = 1;
1578 struct ctl_softc *softc;
1579 struct ctl_lun *lun, *next_lun;
1581 softc = (struct ctl_softc *)control_softc;
1583 if (softc->is_single == 0) {
1584 ctl_ha_msg_shutdown(softc);
1585 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL)
1586 != CTL_HA_STATUS_SUCCESS)
1587 printf("%s: ctl_ha_msg_deregister failed.\n", __func__);
1588 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS)
1589 printf("%s: ctl_ha_msg_destroy failed.\n", __func__);
1590 ctl_frontend_deregister(&ha_frontend);
1593 mtx_lock(&softc->ctl_lock);
1598 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
1599 next_lun = STAILQ_NEXT(lun, links);
1603 mtx_unlock(&softc->ctl_lock);
1606 ctl_shutdown_thread(softc->work_thread);
1607 mtx_destroy(&softc->queue_lock);
1610 ctl_tpc_shutdown(softc);
1611 uma_zdestroy(softc->io_zone);
1612 mtx_destroy(&softc->ctl_lock);
1614 destroy_dev(softc->dev);
1616 sysctl_ctx_free(&softc->sysctl_ctx);
1618 free(control_softc, M_DEVBUF);
1619 control_softc = NULL;
1623 ctl_module_event_handler(module_t mod, int what, void *arg)
1628 return (ctl_init());
1632 return (EOPNOTSUPP);
1637 * XXX KDM should we do some access checks here? Bump a reference count to
1638 * prevent a CTL module from being unloaded while someone has it open?
1641 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1647 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1653 * Remove an initiator by port number and initiator ID.
1654 * Returns 0 for success, -1 for failure.
1657 ctl_remove_initiator(struct ctl_port *port, int iid)
1659 struct ctl_softc *softc = control_softc;
1661 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1663 if (iid > CTL_MAX_INIT_PER_PORT) {
1664 printf("%s: initiator ID %u > maximun %u!\n",
1665 __func__, iid, CTL_MAX_INIT_PER_PORT);
1669 mtx_lock(&softc->ctl_lock);
1670 port->wwpn_iid[iid].in_use--;
1671 port->wwpn_iid[iid].last_use = time_uptime;
1672 mtx_unlock(&softc->ctl_lock);
1673 ctl_isc_announce_iid(port, iid);
1679 * Add an initiator to the initiator map.
1680 * Returns iid for success, < 0 for failure.
1683 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
1685 struct ctl_softc *softc = control_softc;
1689 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1691 if (iid >= CTL_MAX_INIT_PER_PORT) {
1692 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
1693 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
1698 mtx_lock(&softc->ctl_lock);
1700 if (iid < 0 && (wwpn != 0 || name != NULL)) {
1701 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1702 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
1706 if (name != NULL && port->wwpn_iid[i].name != NULL &&
1707 strcmp(name, port->wwpn_iid[i].name) == 0) {
1715 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1716 if (port->wwpn_iid[i].in_use == 0 &&
1717 port->wwpn_iid[i].wwpn == 0 &&
1718 port->wwpn_iid[i].name == NULL) {
1727 best_time = INT32_MAX;
1728 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1729 if (port->wwpn_iid[i].in_use == 0) {
1730 if (port->wwpn_iid[i].last_use < best_time) {
1732 best_time = port->wwpn_iid[i].last_use;
1740 mtx_unlock(&softc->ctl_lock);
1745 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
1747 * This is not an error yet.
1749 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
1751 printf("%s: port %d iid %u WWPN %#jx arrived"
1752 " again\n", __func__, port->targ_port,
1753 iid, (uintmax_t)wwpn);
1757 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
1758 strcmp(name, port->wwpn_iid[iid].name) == 0) {
1760 printf("%s: port %d iid %u name '%s' arrived"
1761 " again\n", __func__, port->targ_port,
1768 * This is an error, but what do we do about it? The
1769 * driver is telling us we have a new WWPN for this
1770 * initiator ID, so we pretty much need to use it.
1772 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
1773 " but WWPN %#jx '%s' is still at that address\n",
1774 __func__, port->targ_port, iid, wwpn, name,
1775 (uintmax_t)port->wwpn_iid[iid].wwpn,
1776 port->wwpn_iid[iid].name);
1779 * XXX KDM clear have_ca and ua_pending on each LUN for
1784 free(port->wwpn_iid[iid].name, M_CTL);
1785 port->wwpn_iid[iid].name = name;
1786 port->wwpn_iid[iid].wwpn = wwpn;
1787 port->wwpn_iid[iid].in_use++;
1788 mtx_unlock(&softc->ctl_lock);
1789 ctl_isc_announce_iid(port, iid);
1795 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
1799 switch (port->port_type) {
1802 struct scsi_transportid_fcp *id =
1803 (struct scsi_transportid_fcp *)buf;
1804 if (port->wwpn_iid[iid].wwpn == 0)
1806 memset(id, 0, sizeof(*id));
1807 id->format_protocol = SCSI_PROTO_FC;
1808 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
1809 return (sizeof(*id));
1811 case CTL_PORT_ISCSI:
1813 struct scsi_transportid_iscsi_port *id =
1814 (struct scsi_transportid_iscsi_port *)buf;
1815 if (port->wwpn_iid[iid].name == NULL)
1818 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
1820 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
1821 len = roundup2(min(len, 252), 4);
1822 scsi_ulto2b(len, id->additional_length);
1823 return (sizeof(*id) + len);
1827 struct scsi_transportid_sas *id =
1828 (struct scsi_transportid_sas *)buf;
1829 if (port->wwpn_iid[iid].wwpn == 0)
1831 memset(id, 0, sizeof(*id));
1832 id->format_protocol = SCSI_PROTO_SAS;
1833 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
1834 return (sizeof(*id));
1838 struct scsi_transportid_spi *id =
1839 (struct scsi_transportid_spi *)buf;
1840 memset(id, 0, sizeof(*id));
1841 id->format_protocol = SCSI_PROTO_SPI;
1842 scsi_ulto2b(iid, id->scsi_addr);
1843 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
1844 return (sizeof(*id));
1850 * Serialize a command that went down the "wrong" side, and so was sent to
1851 * this controller for execution. The logic is a little different than the
1852 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
1853 * sent back to the other side, but in the success case, we execute the
1854 * command on this side (XFER mode) or tell the other side to execute it
1858 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
1860 struct ctl_softc *softc;
1861 union ctl_ha_msg msg_info;
1862 struct ctl_lun *lun;
1863 const struct ctl_cmd_entry *entry;
1867 softc = control_softc;
1869 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
1870 mtx_lock(&softc->ctl_lock);
1871 if ((targ_lun < CTL_MAX_LUNS) &&
1872 ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
1873 mtx_lock(&lun->lun_lock);
1874 mtx_unlock(&softc->ctl_lock);
1876 * If the LUN is invalid, pretend that it doesn't exist.
1877 * It will go away as soon as all pending I/O has been
1880 if (lun->flags & CTL_LUN_DISABLED) {
1881 mtx_unlock(&lun->lun_lock);
1885 mtx_unlock(&softc->ctl_lock);
1890 * The other node would not send this request to us unless
1891 * received announce that we are primary node for this LUN.
1892 * If this LUN does not exist now, it is probably result of
1893 * a race, so respond to initiator in the most opaque way.
1895 ctl_set_busy(ctsio);
1896 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
1897 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1898 msg_info.hdr.serializing_sc = NULL;
1899 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1900 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1901 sizeof(msg_info.scsi), M_WAITOK);
1905 entry = ctl_get_cmd_entry(ctsio, NULL);
1906 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
1907 mtx_unlock(&lun->lun_lock);
1908 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
1909 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1910 msg_info.hdr.serializing_sc = NULL;
1911 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1912 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1913 sizeof(msg_info.scsi), M_WAITOK);
1917 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
1918 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = lun->be_lun;
1921 * Every I/O goes into the OOA queue for a
1922 * particular LUN, and stays there until completion.
1925 if (TAILQ_EMPTY(&lun->ooa_queue))
1926 lun->idle_time += getsbinuptime() - lun->last_busy;
1928 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1930 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
1931 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
1933 case CTL_ACTION_BLOCK:
1934 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
1935 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
1937 mtx_unlock(&lun->lun_lock);
1939 case CTL_ACTION_PASS:
1940 case CTL_ACTION_SKIP:
1941 if (softc->ha_mode == CTL_HA_MODE_XFER) {
1942 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
1943 ctl_enqueue_rtr((union ctl_io *)ctsio);
1944 mtx_unlock(&lun->lun_lock);
1946 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
1947 mtx_unlock(&lun->lun_lock);
1949 /* send msg back to other side */
1950 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1951 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
1952 msg_info.hdr.msg_type = CTL_MSG_R2R;
1953 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1954 sizeof(msg_info.hdr), M_WAITOK);
1957 case CTL_ACTION_OVERLAP:
1958 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1959 mtx_unlock(&lun->lun_lock);
1962 ctl_set_overlapped_cmd(ctsio);
1963 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
1964 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1965 msg_info.hdr.serializing_sc = NULL;
1966 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1967 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1968 sizeof(msg_info.scsi), M_WAITOK);
1970 case CTL_ACTION_OVERLAP_TAG:
1971 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1972 mtx_unlock(&lun->lun_lock);
1974 ctl_set_overlapped_tag(ctsio, ctsio->tag_num);
1975 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
1976 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1977 msg_info.hdr.serializing_sc = NULL;
1978 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1979 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1980 sizeof(msg_info.scsi), M_WAITOK);
1982 case CTL_ACTION_ERROR:
1984 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1985 mtx_unlock(&lun->lun_lock);
1988 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
1990 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
1991 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1992 msg_info.hdr.serializing_sc = NULL;
1993 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1994 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1995 sizeof(msg_info.scsi), M_WAITOK);
2002 * Returns 0 for success, errno for failure.
2005 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2006 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2013 mtx_lock(&lun->lun_lock);
2014 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2015 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2017 struct ctl_ooa_entry *entry;
2020 * If we've got more than we can fit, just count the
2021 * remaining entries.
2023 if (*cur_fill_num >= ooa_hdr->alloc_num)
2026 entry = &kern_entries[*cur_fill_num];
2028 entry->tag_num = io->scsiio.tag_num;
2029 entry->lun_num = lun->lun;
2031 entry->start_bt = io->io_hdr.start_bt;
2033 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2034 entry->cdb_len = io->scsiio.cdb_len;
2035 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2036 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2038 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2039 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2041 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2042 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2044 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2045 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2047 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2048 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2050 mtx_unlock(&lun->lun_lock);
2056 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2057 size_t error_str_len)
2061 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2063 if (copyin(user_addr, kptr, len) != 0) {
2064 snprintf(error_str, error_str_len, "Error copying %d bytes "
2065 "from user address %p to kernel address %p", len,
2075 ctl_free_args(int num_args, struct ctl_be_arg *args)
2082 for (i = 0; i < num_args; i++) {
2083 free(args[i].kname, M_CTL);
2084 free(args[i].kvalue, M_CTL);
2090 static struct ctl_be_arg *
2091 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
2092 char *error_str, size_t error_str_len)
2094 struct ctl_be_arg *args;
2097 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
2098 error_str, error_str_len);
2103 for (i = 0; i < num_args; i++) {
2104 args[i].kname = NULL;
2105 args[i].kvalue = NULL;
2108 for (i = 0; i < num_args; i++) {
2111 args[i].kname = ctl_copyin_alloc(args[i].name,
2112 args[i].namelen, error_str, error_str_len);
2113 if (args[i].kname == NULL)
2116 if (args[i].kname[args[i].namelen - 1] != '\0') {
2117 snprintf(error_str, error_str_len, "Argument %d "
2118 "name is not NUL-terminated", i);
2122 if (args[i].flags & CTL_BEARG_RD) {
2123 tmpptr = ctl_copyin_alloc(args[i].value,
2124 args[i].vallen, error_str, error_str_len);
2127 if ((args[i].flags & CTL_BEARG_ASCII)
2128 && (tmpptr[args[i].vallen - 1] != '\0')) {
2129 snprintf(error_str, error_str_len, "Argument "
2130 "%d value is not NUL-terminated", i);
2133 args[i].kvalue = tmpptr;
2135 args[i].kvalue = malloc(args[i].vallen,
2136 M_CTL, M_WAITOK | M_ZERO);
2143 ctl_free_args(num_args, args);
2149 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2153 for (i = 0; i < num_args; i++) {
2154 if (args[i].flags & CTL_BEARG_WR)
2155 copyout(args[i].kvalue, args[i].value, args[i].vallen);
2160 * Escape characters that are illegal or not recommended in XML.
2163 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2165 char *end = str + size;
2170 for (; *str && str < end; str++) {
2173 retval = sbuf_printf(sb, "&");
2176 retval = sbuf_printf(sb, ">");
2179 retval = sbuf_printf(sb, "<");
2182 retval = sbuf_putc(sb, *str);
2195 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2197 struct scsi_vpd_id_descriptor *desc;
2200 if (id == NULL || id->len < 4)
2202 desc = (struct scsi_vpd_id_descriptor *)id->data;
2203 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2204 case SVPD_ID_TYPE_T10:
2205 sbuf_printf(sb, "t10.");
2207 case SVPD_ID_TYPE_EUI64:
2208 sbuf_printf(sb, "eui.");
2210 case SVPD_ID_TYPE_NAA:
2211 sbuf_printf(sb, "naa.");
2213 case SVPD_ID_TYPE_SCSI_NAME:
2216 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2217 case SVPD_ID_CODESET_BINARY:
2218 for (i = 0; i < desc->length; i++)
2219 sbuf_printf(sb, "%02x", desc->identifier[i]);
2221 case SVPD_ID_CODESET_ASCII:
2222 sbuf_printf(sb, "%.*s", (int)desc->length,
2223 (char *)desc->identifier);
2225 case SVPD_ID_CODESET_UTF8:
2226 sbuf_printf(sb, "%s", (char *)desc->identifier);
2232 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2235 struct ctl_softc *softc;
2236 struct ctl_lun *lun;
2239 softc = control_softc;
2245 retval = ctl_ioctl_io(dev, cmd, addr, flag, td);
2247 case CTL_ENABLE_PORT:
2248 case CTL_DISABLE_PORT:
2249 case CTL_SET_PORT_WWNS: {
2250 struct ctl_port *port;
2251 struct ctl_port_entry *entry;
2253 entry = (struct ctl_port_entry *)addr;
2255 mtx_lock(&softc->ctl_lock);
2256 STAILQ_FOREACH(port, &softc->port_list, links) {
2259 if (port->targ_port < softc->port_min ||
2260 port->targ_port >= softc->port_max)
2265 if ((entry->port_type == CTL_PORT_NONE)
2266 && (entry->targ_port == port->targ_port)) {
2268 * If the user only wants to enable or
2269 * disable or set WWNs on a specific port,
2270 * do the operation and we're done.
2274 } else if (entry->port_type & port->port_type) {
2276 * Compare the user's type mask with the
2277 * particular frontend type to see if we
2284 * Make sure the user isn't trying to set
2285 * WWNs on multiple ports at the same time.
2287 if (cmd == CTL_SET_PORT_WWNS) {
2288 printf("%s: Can't set WWNs on "
2289 "multiple ports\n", __func__);
2298 * XXX KDM we have to drop the lock here, because
2299 * the online/offline operations can potentially
2300 * block. We need to reference count the frontends
2301 * so they can't go away,
2303 if (cmd == CTL_ENABLE_PORT) {
2304 mtx_unlock(&softc->ctl_lock);
2305 ctl_port_online(port);
2306 mtx_lock(&softc->ctl_lock);
2307 } else if (cmd == CTL_DISABLE_PORT) {
2308 mtx_unlock(&softc->ctl_lock);
2309 ctl_port_offline(port);
2310 mtx_lock(&softc->ctl_lock);
2311 } else if (cmd == CTL_SET_PORT_WWNS) {
2312 ctl_port_set_wwns(port,
2313 (entry->flags & CTL_PORT_WWNN_VALID) ?
2315 (entry->flags & CTL_PORT_WWPN_VALID) ?
2316 1 : 0, entry->wwpn);
2321 mtx_unlock(&softc->ctl_lock);
2324 case CTL_GET_PORT_LIST: {
2325 struct ctl_port *port;
2326 struct ctl_port_list *list;
2329 list = (struct ctl_port_list *)addr;
2331 if (list->alloc_len != (list->alloc_num *
2332 sizeof(struct ctl_port_entry))) {
2333 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != "
2334 "alloc_num %u * sizeof(struct ctl_port_entry) "
2335 "%zu\n", __func__, list->alloc_len,
2336 list->alloc_num, sizeof(struct ctl_port_entry));
2342 list->dropped_num = 0;
2344 mtx_lock(&softc->ctl_lock);
2345 STAILQ_FOREACH(port, &softc->port_list, links) {
2346 struct ctl_port_entry entry, *list_entry;
2348 if (list->fill_num >= list->alloc_num) {
2349 list->dropped_num++;
2353 entry.port_type = port->port_type;
2354 strlcpy(entry.port_name, port->port_name,
2355 sizeof(entry.port_name));
2356 entry.targ_port = port->targ_port;
2357 entry.physical_port = port->physical_port;
2358 entry.virtual_port = port->virtual_port;
2359 entry.wwnn = port->wwnn;
2360 entry.wwpn = port->wwpn;
2361 if (port->status & CTL_PORT_STATUS_ONLINE)
2366 list_entry = &list->entries[i];
2368 retval = copyout(&entry, list_entry, sizeof(entry));
2370 printf("%s: CTL_GET_PORT_LIST: copyout "
2371 "returned %d\n", __func__, retval);
2376 list->fill_len += sizeof(entry);
2378 mtx_unlock(&softc->ctl_lock);
2381 * If this is non-zero, we had a copyout fault, so there's
2382 * probably no point in attempting to set the status inside
2388 if (list->dropped_num > 0)
2389 list->status = CTL_PORT_LIST_NEED_MORE_SPACE;
2391 list->status = CTL_PORT_LIST_OK;
2394 case CTL_DUMP_OOA: {
2399 mtx_lock(&softc->ctl_lock);
2400 printf("Dumping OOA queues:\n");
2401 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2402 mtx_lock(&lun->lun_lock);
2403 for (io = (union ctl_io *)TAILQ_FIRST(
2404 &lun->ooa_queue); io != NULL;
2405 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2407 sbuf_new(&sb, printbuf, sizeof(printbuf),
2409 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ",
2413 CTL_FLAG_BLOCKED) ? "" : " BLOCKED",
2415 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
2417 CTL_FLAG_ABORT) ? " ABORT" : "",
2419 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : "");
2420 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
2422 printf("%s\n", sbuf_data(&sb));
2424 mtx_unlock(&lun->lun_lock);
2426 printf("OOA queues dump done\n");
2427 mtx_unlock(&softc->ctl_lock);
2431 struct ctl_ooa *ooa_hdr;
2432 struct ctl_ooa_entry *entries;
2433 uint32_t cur_fill_num;
2435 ooa_hdr = (struct ctl_ooa *)addr;
2437 if ((ooa_hdr->alloc_len == 0)
2438 || (ooa_hdr->alloc_num == 0)) {
2439 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2440 "must be non-zero\n", __func__,
2441 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2446 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2447 sizeof(struct ctl_ooa_entry))) {
2448 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2449 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2450 __func__, ooa_hdr->alloc_len,
2451 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2456 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2457 if (entries == NULL) {
2458 printf("%s: could not allocate %d bytes for OOA "
2459 "dump\n", __func__, ooa_hdr->alloc_len);
2464 mtx_lock(&softc->ctl_lock);
2465 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2466 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS)
2467 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2468 mtx_unlock(&softc->ctl_lock);
2469 free(entries, M_CTL);
2470 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2471 __func__, (uintmax_t)ooa_hdr->lun_num);
2478 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2479 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2480 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2486 mtx_unlock(&softc->ctl_lock);
2487 free(entries, M_CTL);
2491 lun = softc->ctl_luns[ooa_hdr->lun_num];
2493 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr,
2496 mtx_unlock(&softc->ctl_lock);
2498 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2499 ooa_hdr->fill_len = ooa_hdr->fill_num *
2500 sizeof(struct ctl_ooa_entry);
2501 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2503 printf("%s: error copying out %d bytes for OOA dump\n",
2504 __func__, ooa_hdr->fill_len);
2507 getbintime(&ooa_hdr->cur_bt);
2509 if (cur_fill_num > ooa_hdr->alloc_num) {
2510 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2511 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2513 ooa_hdr->dropped_num = 0;
2514 ooa_hdr->status = CTL_OOA_OK;
2517 free(entries, M_CTL);
2520 case CTL_CHECK_OOA: {
2522 struct ctl_ooa_info *ooa_info;
2525 ooa_info = (struct ctl_ooa_info *)addr;
2527 if (ooa_info->lun_id >= CTL_MAX_LUNS) {
2528 ooa_info->status = CTL_OOA_INVALID_LUN;
2531 mtx_lock(&softc->ctl_lock);
2532 lun = softc->ctl_luns[ooa_info->lun_id];
2534 mtx_unlock(&softc->ctl_lock);
2535 ooa_info->status = CTL_OOA_INVALID_LUN;
2538 mtx_lock(&lun->lun_lock);
2539 mtx_unlock(&softc->ctl_lock);
2540 ooa_info->num_entries = 0;
2541 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
2542 io != NULL; io = (union ctl_io *)TAILQ_NEXT(
2543 &io->io_hdr, ooa_links)) {
2544 ooa_info->num_entries++;
2546 mtx_unlock(&lun->lun_lock);
2548 ooa_info->status = CTL_OOA_SUCCESS;
2552 case CTL_DELAY_IO: {
2553 struct ctl_io_delay_info *delay_info;
2555 delay_info = (struct ctl_io_delay_info *)addr;
2558 mtx_lock(&softc->ctl_lock);
2560 if ((delay_info->lun_id >= CTL_MAX_LUNS)
2561 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2562 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2564 lun = softc->ctl_luns[delay_info->lun_id];
2565 mtx_lock(&lun->lun_lock);
2567 delay_info->status = CTL_DELAY_STATUS_OK;
2569 switch (delay_info->delay_type) {
2570 case CTL_DELAY_TYPE_CONT:
2572 case CTL_DELAY_TYPE_ONESHOT:
2575 delay_info->status =
2576 CTL_DELAY_STATUS_INVALID_TYPE;
2580 switch (delay_info->delay_loc) {
2581 case CTL_DELAY_LOC_DATAMOVE:
2582 lun->delay_info.datamove_type =
2583 delay_info->delay_type;
2584 lun->delay_info.datamove_delay =
2585 delay_info->delay_secs;
2587 case CTL_DELAY_LOC_DONE:
2588 lun->delay_info.done_type =
2589 delay_info->delay_type;
2590 lun->delay_info.done_delay =
2591 delay_info->delay_secs;
2594 delay_info->status =
2595 CTL_DELAY_STATUS_INVALID_LOC;
2598 mtx_unlock(&lun->lun_lock);
2601 mtx_unlock(&softc->ctl_lock);
2603 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2604 #endif /* CTL_IO_DELAY */
2607 case CTL_REALSYNC_SET: {
2610 syncstate = (int *)addr;
2612 mtx_lock(&softc->ctl_lock);
2613 switch (*syncstate) {
2615 softc->flags &= ~CTL_FLAG_REAL_SYNC;
2618 softc->flags |= CTL_FLAG_REAL_SYNC;
2624 mtx_unlock(&softc->ctl_lock);
2627 case CTL_REALSYNC_GET: {
2630 syncstate = (int*)addr;
2632 mtx_lock(&softc->ctl_lock);
2633 if (softc->flags & CTL_FLAG_REAL_SYNC)
2637 mtx_unlock(&softc->ctl_lock);
2643 struct ctl_sync_info *sync_info;
2645 sync_info = (struct ctl_sync_info *)addr;
2647 mtx_lock(&softc->ctl_lock);
2648 lun = softc->ctl_luns[sync_info->lun_id];
2650 mtx_unlock(&softc->ctl_lock);
2651 sync_info->status = CTL_GS_SYNC_NO_LUN;
2655 * Get or set the sync interval. We're not bounds checking
2656 * in the set case, hopefully the user won't do something
2659 mtx_lock(&lun->lun_lock);
2660 mtx_unlock(&softc->ctl_lock);
2661 if (cmd == CTL_GETSYNC)
2662 sync_info->sync_interval = lun->sync_interval;
2664 lun->sync_interval = sync_info->sync_interval;
2665 mtx_unlock(&lun->lun_lock);
2667 sync_info->status = CTL_GS_SYNC_OK;
2671 case CTL_GETSTATS: {
2672 struct ctl_stats *stats;
2675 stats = (struct ctl_stats *)addr;
2677 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2679 stats->status = CTL_SS_NEED_MORE_SPACE;
2680 stats->num_luns = softc->num_luns;
2684 * XXX KDM no locking here. If the LUN list changes,
2685 * things can blow up.
2687 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
2688 i++, lun = STAILQ_NEXT(lun, links)) {
2689 retval = copyout(&lun->stats, &stats->lun_stats[i],
2690 sizeof(lun->stats));
2694 stats->num_luns = softc->num_luns;
2695 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2697 stats->status = CTL_SS_OK;
2699 stats->flags = CTL_STATS_FLAG_TIME_VALID;
2701 stats->flags = CTL_STATS_FLAG_NONE;
2703 getnanouptime(&stats->timestamp);
2706 case CTL_ERROR_INJECT: {
2707 struct ctl_error_desc *err_desc, *new_err_desc;
2709 err_desc = (struct ctl_error_desc *)addr;
2711 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2713 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2715 mtx_lock(&softc->ctl_lock);
2716 lun = softc->ctl_luns[err_desc->lun_id];
2718 mtx_unlock(&softc->ctl_lock);
2719 free(new_err_desc, M_CTL);
2720 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2721 __func__, (uintmax_t)err_desc->lun_id);
2725 mtx_lock(&lun->lun_lock);
2726 mtx_unlock(&softc->ctl_lock);
2729 * We could do some checking here to verify the validity
2730 * of the request, but given the complexity of error
2731 * injection requests, the checking logic would be fairly
2734 * For now, if the request is invalid, it just won't get
2735 * executed and might get deleted.
2737 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2740 * XXX KDM check to make sure the serial number is unique,
2741 * in case we somehow manage to wrap. That shouldn't
2742 * happen for a very long time, but it's the right thing to
2745 new_err_desc->serial = lun->error_serial;
2746 err_desc->serial = lun->error_serial;
2747 lun->error_serial++;
2749 mtx_unlock(&lun->lun_lock);
2752 case CTL_ERROR_INJECT_DELETE: {
2753 struct ctl_error_desc *delete_desc, *desc, *desc2;
2756 delete_desc = (struct ctl_error_desc *)addr;
2759 mtx_lock(&softc->ctl_lock);
2760 lun = softc->ctl_luns[delete_desc->lun_id];
2762 mtx_unlock(&softc->ctl_lock);
2763 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2764 __func__, (uintmax_t)delete_desc->lun_id);
2768 mtx_lock(&lun->lun_lock);
2769 mtx_unlock(&softc->ctl_lock);
2770 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2771 if (desc->serial != delete_desc->serial)
2774 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2779 mtx_unlock(&lun->lun_lock);
2780 if (delete_done == 0) {
2781 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2782 "error serial %ju on LUN %u\n", __func__,
2783 delete_desc->serial, delete_desc->lun_id);
2789 case CTL_DUMP_STRUCTS: {
2791 struct ctl_port *port;
2792 struct ctl_frontend *fe;
2794 mtx_lock(&softc->ctl_lock);
2795 printf("CTL Persistent Reservation information start:\n");
2796 for (i = 0; i < CTL_MAX_LUNS; i++) {
2797 lun = softc->ctl_luns[i];
2800 || ((lun->flags & CTL_LUN_DISABLED) != 0))
2803 for (j = 0; j < CTL_MAX_PORTS; j++) {
2804 if (lun->pr_keys[j] == NULL)
2806 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2807 if (lun->pr_keys[j][k] == 0)
2809 printf(" LUN %d port %d iid %d key "
2811 (uintmax_t)lun->pr_keys[j][k]);
2815 printf("CTL Persistent Reservation information end\n");
2816 printf("CTL Ports:\n");
2817 STAILQ_FOREACH(port, &softc->port_list, links) {
2818 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
2819 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
2820 port->frontend->name, port->port_type,
2821 port->physical_port, port->virtual_port,
2822 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
2823 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2824 if (port->wwpn_iid[j].in_use == 0 &&
2825 port->wwpn_iid[j].wwpn == 0 &&
2826 port->wwpn_iid[j].name == NULL)
2829 printf(" iid %u use %d WWPN %#jx '%s'\n",
2830 j, port->wwpn_iid[j].in_use,
2831 (uintmax_t)port->wwpn_iid[j].wwpn,
2832 port->wwpn_iid[j].name);
2835 printf("CTL Port information end\n");
2836 mtx_unlock(&softc->ctl_lock);
2838 * XXX KDM calling this without a lock. We'd likely want
2839 * to drop the lock before calling the frontend's dump
2842 printf("CTL Frontends:\n");
2843 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2844 printf(" Frontend '%s'\n", fe->name);
2845 if (fe->fe_dump != NULL)
2848 printf("CTL Frontend information end\n");
2852 struct ctl_lun_req *lun_req;
2853 struct ctl_backend_driver *backend;
2855 lun_req = (struct ctl_lun_req *)addr;
2857 backend = ctl_backend_find(lun_req->backend);
2858 if (backend == NULL) {
2859 lun_req->status = CTL_LUN_ERROR;
2860 snprintf(lun_req->error_str,
2861 sizeof(lun_req->error_str),
2862 "Backend \"%s\" not found.",
2866 if (lun_req->num_be_args > 0) {
2867 lun_req->kern_be_args = ctl_copyin_args(
2868 lun_req->num_be_args,
2871 sizeof(lun_req->error_str));
2872 if (lun_req->kern_be_args == NULL) {
2873 lun_req->status = CTL_LUN_ERROR;
2878 retval = backend->ioctl(dev, cmd, addr, flag, td);
2880 if (lun_req->num_be_args > 0) {
2881 ctl_copyout_args(lun_req->num_be_args,
2882 lun_req->kern_be_args);
2883 ctl_free_args(lun_req->num_be_args,
2884 lun_req->kern_be_args);
2888 case CTL_LUN_LIST: {
2890 struct ctl_lun_list *list;
2891 struct ctl_option *opt;
2893 list = (struct ctl_lun_list *)addr;
2896 * Allocate a fixed length sbuf here, based on the length
2897 * of the user's buffer. We could allocate an auto-extending
2898 * buffer, and then tell the user how much larger our
2899 * amount of data is than his buffer, but that presents
2902 * 1. The sbuf(9) routines use a blocking malloc, and so
2903 * we can't hold a lock while calling them with an
2904 * auto-extending buffer.
2906 * 2. There is not currently a LUN reference counting
2907 * mechanism, outside of outstanding transactions on
2908 * the LUN's OOA queue. So a LUN could go away on us
2909 * while we're getting the LUN number, backend-specific
2910 * information, etc. Thus, given the way things
2911 * currently work, we need to hold the CTL lock while
2912 * grabbing LUN information.
2914 * So, from the user's standpoint, the best thing to do is
2915 * allocate what he thinks is a reasonable buffer length,
2916 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
2917 * double the buffer length and try again. (And repeat
2918 * that until he succeeds.)
2920 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
2922 list->status = CTL_LUN_LIST_ERROR;
2923 snprintf(list->error_str, sizeof(list->error_str),
2924 "Unable to allocate %d bytes for LUN list",
2929 sbuf_printf(sb, "<ctllunlist>\n");
2931 mtx_lock(&softc->ctl_lock);
2932 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2933 mtx_lock(&lun->lun_lock);
2934 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
2935 (uintmax_t)lun->lun);
2938 * Bail out as soon as we see that we've overfilled
2944 retval = sbuf_printf(sb, "\t<backend_type>%s"
2945 "</backend_type>\n",
2946 (lun->backend == NULL) ? "none" :
2947 lun->backend->name);
2952 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
2953 lun->be_lun->lun_type);
2958 if (lun->backend == NULL) {
2959 retval = sbuf_printf(sb, "</lun>\n");
2965 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
2966 (lun->be_lun->maxlba > 0) ?
2967 lun->be_lun->maxlba + 1 : 0);
2972 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
2973 lun->be_lun->blocksize);
2978 retval = sbuf_printf(sb, "\t<serial_number>");
2983 retval = ctl_sbuf_printf_esc(sb,
2984 lun->be_lun->serial_num,
2985 sizeof(lun->be_lun->serial_num));
2990 retval = sbuf_printf(sb, "</serial_number>\n");
2995 retval = sbuf_printf(sb, "\t<device_id>");
3000 retval = ctl_sbuf_printf_esc(sb,
3001 lun->be_lun->device_id,
3002 sizeof(lun->be_lun->device_id));
3007 retval = sbuf_printf(sb, "</device_id>\n");
3012 if (lun->backend->lun_info != NULL) {
3013 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3017 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3018 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3019 opt->name, opt->value, opt->name);
3024 retval = sbuf_printf(sb, "</lun>\n");
3028 mtx_unlock(&lun->lun_lock);
3031 mtx_unlock(&lun->lun_lock);
3032 mtx_unlock(&softc->ctl_lock);
3035 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3038 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3039 snprintf(list->error_str, sizeof(list->error_str),
3040 "Out of space, %d bytes is too small",
3047 retval = copyout(sbuf_data(sb), list->lun_xml,
3050 list->fill_len = sbuf_len(sb) + 1;
3051 list->status = CTL_LUN_LIST_OK;
3056 struct ctl_iscsi *ci;
3057 struct ctl_frontend *fe;
3059 ci = (struct ctl_iscsi *)addr;
3061 fe = ctl_frontend_find("iscsi");
3063 ci->status = CTL_ISCSI_ERROR;
3064 snprintf(ci->error_str, sizeof(ci->error_str),
3065 "Frontend \"iscsi\" not found.");
3069 retval = fe->ioctl(dev, cmd, addr, flag, td);
3072 case CTL_PORT_REQ: {
3073 struct ctl_req *req;
3074 struct ctl_frontend *fe;
3076 req = (struct ctl_req *)addr;
3078 fe = ctl_frontend_find(req->driver);
3080 req->status = CTL_LUN_ERROR;
3081 snprintf(req->error_str, sizeof(req->error_str),
3082 "Frontend \"%s\" not found.", req->driver);
3085 if (req->num_args > 0) {
3086 req->kern_args = ctl_copyin_args(req->num_args,
3087 req->args, req->error_str, sizeof(req->error_str));
3088 if (req->kern_args == NULL) {
3089 req->status = CTL_LUN_ERROR;
3095 retval = fe->ioctl(dev, cmd, addr, flag, td);
3099 if (req->num_args > 0) {
3100 ctl_copyout_args(req->num_args, req->kern_args);
3101 ctl_free_args(req->num_args, req->kern_args);
3105 case CTL_PORT_LIST: {
3107 struct ctl_port *port;
3108 struct ctl_lun_list *list;
3109 struct ctl_option *opt;
3113 list = (struct ctl_lun_list *)addr;
3115 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3117 list->status = CTL_LUN_LIST_ERROR;
3118 snprintf(list->error_str, sizeof(list->error_str),
3119 "Unable to allocate %d bytes for LUN list",
3124 sbuf_printf(sb, "<ctlportlist>\n");
3126 mtx_lock(&softc->ctl_lock);
3127 STAILQ_FOREACH(port, &softc->port_list, links) {
3128 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3129 (uintmax_t)port->targ_port);
3132 * Bail out as soon as we see that we've overfilled
3138 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3139 "</frontend_type>\n", port->frontend->name);
3143 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3148 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3149 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3153 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3158 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3159 port->physical_port);
3163 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3164 port->virtual_port);
3168 if (port->target_devid != NULL) {
3169 sbuf_printf(sb, "\t<target>");
3170 ctl_id_sbuf(port->target_devid, sb);
3171 sbuf_printf(sb, "</target>\n");
3174 if (port->port_devid != NULL) {
3175 sbuf_printf(sb, "\t<port>");
3176 ctl_id_sbuf(port->port_devid, sb);
3177 sbuf_printf(sb, "</port>\n");
3180 if (port->port_info != NULL) {
3181 retval = port->port_info(port->onoff_arg, sb);
3185 STAILQ_FOREACH(opt, &port->options, links) {
3186 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3187 opt->name, opt->value, opt->name);
3192 if (port->lun_map != NULL) {
3193 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n");
3194 for (j = 0; j < CTL_MAX_LUNS; j++) {
3195 plun = ctl_lun_map_from_port(port, j);
3196 if (plun >= CTL_MAX_LUNS)
3199 "\t<lun id=\"%u\">%u</lun>\n",
3204 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3205 if (port->wwpn_iid[j].in_use == 0 ||
3206 (port->wwpn_iid[j].wwpn == 0 &&
3207 port->wwpn_iid[j].name == NULL))
3210 if (port->wwpn_iid[j].name != NULL)
3211 retval = sbuf_printf(sb,
3212 "\t<initiator id=\"%u\">%s</initiator>\n",
3213 j, port->wwpn_iid[j].name);
3215 retval = sbuf_printf(sb,
3216 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3217 j, port->wwpn_iid[j].wwpn);
3224 retval = sbuf_printf(sb, "</targ_port>\n");
3228 mtx_unlock(&softc->ctl_lock);
3231 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3234 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3235 snprintf(list->error_str, sizeof(list->error_str),
3236 "Out of space, %d bytes is too small",
3243 retval = copyout(sbuf_data(sb), list->lun_xml,
3246 list->fill_len = sbuf_len(sb) + 1;
3247 list->status = CTL_LUN_LIST_OK;
3252 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr;
3253 struct ctl_port *port;
3255 mtx_lock(&softc->ctl_lock);
3256 if (lm->port < softc->port_min ||
3257 lm->port >= softc->port_max ||
3258 (port = softc->ctl_ports[lm->port]) == NULL) {
3259 mtx_unlock(&softc->ctl_lock);
3262 if (port->status & CTL_PORT_STATUS_ONLINE) {
3263 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3264 if (ctl_lun_map_to_port(port, lun->lun) >=
3267 mtx_lock(&lun->lun_lock);
3268 ctl_est_ua_port(lun, lm->port, -1,
3270 mtx_unlock(&lun->lun_lock);
3273 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps
3274 if (lm->plun < CTL_MAX_LUNS) {
3275 if (lm->lun == UINT32_MAX)
3276 retval = ctl_lun_map_unset(port, lm->plun);
3277 else if (lm->lun < CTL_MAX_LUNS &&
3278 softc->ctl_luns[lm->lun] != NULL)
3279 retval = ctl_lun_map_set(port, lm->plun, lm->lun);
3282 } else if (lm->plun == UINT32_MAX) {
3283 if (lm->lun == UINT32_MAX)
3284 retval = ctl_lun_map_deinit(port);
3286 retval = ctl_lun_map_init(port);
3289 if (port->status & CTL_PORT_STATUS_ONLINE)
3290 ctl_isc_announce_port(port);
3294 /* XXX KDM should we fix this? */
3296 struct ctl_backend_driver *backend;
3303 * We encode the backend type as the ioctl type for backend
3304 * ioctls. So parse it out here, and then search for a
3305 * backend of this type.
3307 type = _IOC_TYPE(cmd);
3309 STAILQ_FOREACH(backend, &softc->be_list, links) {
3310 if (backend->type == type) {
3316 printf("ctl: unknown ioctl command %#lx or backend "
3321 retval = backend->ioctl(dev, cmd, addr, flag, td);
3331 ctl_get_initindex(struct ctl_nexus *nexus)
3333 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3337 ctl_lun_map_init(struct ctl_port *port)
3339 struct ctl_softc *softc = control_softc;
3340 struct ctl_lun *lun;
3343 if (port->lun_map == NULL)
3344 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS,
3346 if (port->lun_map == NULL)
3348 for (i = 0; i < CTL_MAX_LUNS; i++)
3349 port->lun_map[i] = UINT32_MAX;
3350 if (port->status & CTL_PORT_STATUS_ONLINE) {
3351 if (port->lun_disable != NULL) {
3352 STAILQ_FOREACH(lun, &softc->lun_list, links)
3353 port->lun_disable(port->targ_lun_arg, lun->lun);
3355 ctl_isc_announce_port(port);
3361 ctl_lun_map_deinit(struct ctl_port *port)
3363 struct ctl_softc *softc = control_softc;
3364 struct ctl_lun *lun;
3366 if (port->lun_map == NULL)
3368 free(port->lun_map, M_CTL);
3369 port->lun_map = NULL;
3370 if (port->status & CTL_PORT_STATUS_ONLINE) {
3371 if (port->lun_enable != NULL) {
3372 STAILQ_FOREACH(lun, &softc->lun_list, links)
3373 port->lun_enable(port->targ_lun_arg, lun->lun);
3375 ctl_isc_announce_port(port);
3381 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun)
3386 if (port->lun_map == NULL) {
3387 status = ctl_lun_map_init(port);
3391 old = port->lun_map[plun];
3392 port->lun_map[plun] = glun;
3393 if ((port->status & CTL_PORT_STATUS_ONLINE) && old >= CTL_MAX_LUNS) {
3394 if (port->lun_enable != NULL)
3395 port->lun_enable(port->targ_lun_arg, plun);
3396 ctl_isc_announce_port(port);
3402 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun)
3406 if (port->lun_map == NULL)
3408 old = port->lun_map[plun];
3409 port->lun_map[plun] = UINT32_MAX;
3410 if ((port->status & CTL_PORT_STATUS_ONLINE) && old < CTL_MAX_LUNS) {
3411 if (port->lun_disable != NULL)
3412 port->lun_disable(port->targ_lun_arg, plun);
3413 ctl_isc_announce_port(port);
3419 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
3423 return (UINT32_MAX);
3424 if (port->lun_map == NULL || lun_id >= CTL_MAX_LUNS)
3426 return (port->lun_map[lun_id]);
3430 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
3435 return (UINT32_MAX);
3436 if (port->lun_map == NULL)
3438 for (i = 0; i < CTL_MAX_LUNS; i++) {
3439 if (port->lun_map[i] == lun_id)
3442 return (UINT32_MAX);
3445 static struct ctl_port *
3446 ctl_io_port(struct ctl_io_hdr *io_hdr)
3449 return (control_softc->ctl_ports[io_hdr->nexus.targ_port]);
3453 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last)
3457 for (i = first; i < last; i++) {
3458 if ((mask[i / 32] & (1 << (i % 32))) == 0)
3465 ctl_set_mask(uint32_t *mask, uint32_t bit)
3467 uint32_t chunk, piece;
3470 piece = bit % (sizeof(uint32_t) * 8);
3472 if ((mask[chunk] & (1 << piece)) != 0)
3475 mask[chunk] |= (1 << piece);
3481 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3483 uint32_t chunk, piece;
3486 piece = bit % (sizeof(uint32_t) * 8);
3488 if ((mask[chunk] & (1 << piece)) == 0)
3491 mask[chunk] &= ~(1 << piece);
3497 ctl_is_set(uint32_t *mask, uint32_t bit)
3499 uint32_t chunk, piece;
3502 piece = bit % (sizeof(uint32_t) * 8);
3504 if ((mask[chunk] & (1 << piece)) == 0)
3511 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3515 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3518 return (t[residx % CTL_MAX_INIT_PER_PORT]);
3522 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3526 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3529 t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3533 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3538 i = residx/CTL_MAX_INIT_PER_PORT;
3539 if (lun->pr_keys[i] != NULL)
3541 mtx_unlock(&lun->lun_lock);
3542 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3544 mtx_lock(&lun->lun_lock);
3545 if (lun->pr_keys[i] == NULL)
3546 lun->pr_keys[i] = p;
3552 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3556 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3557 KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3558 t[residx % CTL_MAX_INIT_PER_PORT] = key;
3562 * ctl_softc, pool_name, total_ctl_io are passed in.
3563 * npool is passed out.
3566 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3567 uint32_t total_ctl_io, void **npool)
3570 struct ctl_io_pool *pool;
3572 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3577 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3578 pool->ctl_softc = ctl_softc;
3579 pool->zone = uma_zsecond_create(pool->name, NULL,
3580 NULL, NULL, NULL, ctl_softc->io_zone);
3581 /* uma_prealloc(pool->zone, total_ctl_io); */
3585 *npool = ctl_softc->io_zone;
3591 ctl_pool_free(struct ctl_io_pool *pool)
3598 uma_zdestroy(pool->zone);
3604 ctl_alloc_io(void *pool_ref)
3608 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3610 io = uma_zalloc(pool->zone, M_WAITOK);
3612 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK);
3615 io->io_hdr.pool = pool_ref;
3620 ctl_alloc_io_nowait(void *pool_ref)
3624 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3626 io = uma_zalloc(pool->zone, M_NOWAIT);
3628 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT);
3631 io->io_hdr.pool = pool_ref;
3636 ctl_free_io(union ctl_io *io)
3639 struct ctl_io_pool *pool;
3646 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3647 uma_zfree(pool->zone, io);
3649 uma_zfree((uma_zone_t)io->io_hdr.pool, io);
3654 ctl_zero_io(union ctl_io *io)
3662 * May need to preserve linked list pointers at some point too.
3664 pool_ref = io->io_hdr.pool;
3665 memset(io, 0, sizeof(*io));
3666 io->io_hdr.pool = pool_ref;
3670 * This routine is currently used for internal copies of ctl_ios that need
3671 * to persist for some reason after we've already returned status to the
3672 * FETD. (Thus the flag set.)
3675 * Note that this makes a blind copy of all fields in the ctl_io, except
3676 * for the pool reference. This includes any memory that has been
3677 * allocated! That memory will no longer be valid after done has been
3678 * called, so this would be VERY DANGEROUS for command that actually does
3679 * any reads or writes. Right now (11/7/2005), this is only used for immediate
3680 * start and stop commands, which don't transfer any data, so this is not a
3681 * problem. If it is used for anything else, the caller would also need to
3682 * allocate data buffer space and this routine would need to be modified to
3683 * copy the data buffer(s) as well.
3686 ctl_copy_io(union ctl_io *src, union ctl_io *dest)
3695 * May need to preserve linked list pointers at some point too.
3697 pool_ref = dest->io_hdr.pool;
3699 memcpy(dest, src, MIN(sizeof(*src), sizeof(*dest)));
3701 dest->io_hdr.pool = pool_ref;
3703 * We need to know that this is an internal copy, and doesn't need
3704 * to get passed back to the FETD that allocated it.
3706 dest->io_hdr.flags |= CTL_FLAG_INT_COPY;
3710 ctl_expand_number(const char *buf, uint64_t *num)
3716 number = strtoq(buf, &endptr, 0);
3718 switch (tolower((unsigned char)*endptr)) {
3738 case '\0': /* No unit. */
3742 /* Unrecognized unit. */
3746 if ((number << shift) >> shift != number) {
3750 *num = number << shift;
3756 * This routine could be used in the future to load default and/or saved
3757 * mode page parameters for a particuar lun.
3760 ctl_init_page_index(struct ctl_lun *lun)
3763 struct ctl_page_index *page_index;
3767 memcpy(&lun->mode_pages.index, page_index_template,
3768 sizeof(page_index_template));
3770 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3772 page_index = &lun->mode_pages.index[i];
3774 * If this is a disk-only mode page, there's no point in
3775 * setting it up. For some pages, we have to have some
3776 * basic information about the disk in order to calculate the
3779 if ((lun->be_lun->lun_type != T_DIRECT)
3780 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
3783 switch (page_index->page_code & SMPH_PC_MASK) {
3784 case SMS_RW_ERROR_RECOVERY_PAGE: {
3785 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3786 panic("subpage is incorrect!");
3787 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
3788 &rw_er_page_default,
3789 sizeof(rw_er_page_default));
3790 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
3791 &rw_er_page_changeable,
3792 sizeof(rw_er_page_changeable));
3793 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
3794 &rw_er_page_default,
3795 sizeof(rw_er_page_default));
3796 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
3797 &rw_er_page_default,
3798 sizeof(rw_er_page_default));
3799 page_index->page_data =
3800 (uint8_t *)lun->mode_pages.rw_er_page;
3803 case SMS_FORMAT_DEVICE_PAGE: {
3804 struct scsi_format_page *format_page;
3806 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3807 panic("subpage is incorrect!");
3810 * Sectors per track are set above. Bytes per
3811 * sector need to be set here on a per-LUN basis.
3813 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
3814 &format_page_default,
3815 sizeof(format_page_default));
3816 memcpy(&lun->mode_pages.format_page[
3817 CTL_PAGE_CHANGEABLE], &format_page_changeable,
3818 sizeof(format_page_changeable));
3819 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
3820 &format_page_default,
3821 sizeof(format_page_default));
3822 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
3823 &format_page_default,
3824 sizeof(format_page_default));
3826 format_page = &lun->mode_pages.format_page[
3828 scsi_ulto2b(lun->be_lun->blocksize,
3829 format_page->bytes_per_sector);
3831 format_page = &lun->mode_pages.format_page[
3833 scsi_ulto2b(lun->be_lun->blocksize,
3834 format_page->bytes_per_sector);
3836 format_page = &lun->mode_pages.format_page[
3838 scsi_ulto2b(lun->be_lun->blocksize,
3839 format_page->bytes_per_sector);
3841 page_index->page_data =
3842 (uint8_t *)lun->mode_pages.format_page;
3845 case SMS_RIGID_DISK_PAGE: {
3846 struct scsi_rigid_disk_page *rigid_disk_page;
3847 uint32_t sectors_per_cylinder;
3851 #endif /* !__XSCALE__ */
3853 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3854 panic("invalid subpage value %d",
3855 page_index->subpage);
3858 * Rotation rate and sectors per track are set
3859 * above. We calculate the cylinders here based on
3860 * capacity. Due to the number of heads and
3861 * sectors per track we're using, smaller arrays
3862 * may turn out to have 0 cylinders. Linux and
3863 * FreeBSD don't pay attention to these mode pages
3864 * to figure out capacity, but Solaris does. It
3865 * seems to deal with 0 cylinders just fine, and
3866 * works out a fake geometry based on the capacity.
3868 memcpy(&lun->mode_pages.rigid_disk_page[
3869 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
3870 sizeof(rigid_disk_page_default));
3871 memcpy(&lun->mode_pages.rigid_disk_page[
3872 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
3873 sizeof(rigid_disk_page_changeable));
3875 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
3879 * The divide method here will be more accurate,
3880 * probably, but results in floating point being
3881 * used in the kernel on i386 (__udivdi3()). On the
3882 * XScale, though, __udivdi3() is implemented in
3885 * The shift method for cylinder calculation is
3886 * accurate if sectors_per_cylinder is a power of
3887 * 2. Otherwise it might be slightly off -- you
3888 * might have a bit of a truncation problem.
3891 cylinders = (lun->be_lun->maxlba + 1) /
3892 sectors_per_cylinder;
3894 for (shift = 31; shift > 0; shift--) {
3895 if (sectors_per_cylinder & (1 << shift))
3898 cylinders = (lun->be_lun->maxlba + 1) >> shift;
3902 * We've basically got 3 bytes, or 24 bits for the
3903 * cylinder size in the mode page. If we're over,
3904 * just round down to 2^24.
3906 if (cylinders > 0xffffff)
3907 cylinders = 0xffffff;
3909 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
3911 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
3913 if ((value = ctl_get_opt(&lun->be_lun->options,
3915 scsi_ulto2b(strtol(value, NULL, 0),
3916 rigid_disk_page->rotation_rate);
3919 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
3920 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
3921 sizeof(rigid_disk_page_default));
3922 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
3923 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
3924 sizeof(rigid_disk_page_default));
3926 page_index->page_data =
3927 (uint8_t *)lun->mode_pages.rigid_disk_page;
3930 case SMS_CACHING_PAGE: {
3931 struct scsi_caching_page *caching_page;
3933 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3934 panic("invalid subpage value %d",
3935 page_index->subpage);
3936 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
3937 &caching_page_default,
3938 sizeof(caching_page_default));
3939 memcpy(&lun->mode_pages.caching_page[
3940 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
3941 sizeof(caching_page_changeable));
3942 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
3943 &caching_page_default,
3944 sizeof(caching_page_default));
3945 caching_page = &lun->mode_pages.caching_page[
3947 value = ctl_get_opt(&lun->be_lun->options, "writecache");
3948 if (value != NULL && strcmp(value, "off") == 0)
3949 caching_page->flags1 &= ~SCP_WCE;
3950 value = ctl_get_opt(&lun->be_lun->options, "readcache");
3951 if (value != NULL && strcmp(value, "off") == 0)
3952 caching_page->flags1 |= SCP_RCD;
3953 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
3954 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
3955 sizeof(caching_page_default));
3956 page_index->page_data =
3957 (uint8_t *)lun->mode_pages.caching_page;
3960 case SMS_CONTROL_MODE_PAGE: {
3961 struct scsi_control_page *control_page;
3963 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3964 panic("invalid subpage value %d",
3965 page_index->subpage);
3967 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT],
3968 &control_page_default,
3969 sizeof(control_page_default));
3970 memcpy(&lun->mode_pages.control_page[
3971 CTL_PAGE_CHANGEABLE], &control_page_changeable,
3972 sizeof(control_page_changeable));
3973 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED],
3974 &control_page_default,
3975 sizeof(control_page_default));
3976 control_page = &lun->mode_pages.control_page[
3978 value = ctl_get_opt(&lun->be_lun->options, "reordering");
3979 if (value != NULL && strcmp(value, "unrestricted") == 0) {
3980 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK;
3981 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED;
3983 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT],
3984 &lun->mode_pages.control_page[CTL_PAGE_SAVED],
3985 sizeof(control_page_default));
3986 page_index->page_data =
3987 (uint8_t *)lun->mode_pages.control_page;
3991 case SMS_INFO_EXCEPTIONS_PAGE: {
3992 switch (page_index->subpage) {
3993 case SMS_SUBPAGE_PAGE_0:
3994 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
3996 sizeof(ie_page_default));
3997 memcpy(&lun->mode_pages.ie_page[
3998 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
3999 sizeof(ie_page_changeable));
4000 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4002 sizeof(ie_page_default));
4003 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4005 sizeof(ie_page_default));
4006 page_index->page_data =
4007 (uint8_t *)lun->mode_pages.ie_page;
4010 struct ctl_logical_block_provisioning_page *page;
4012 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4014 sizeof(lbp_page_default));
4015 memcpy(&lun->mode_pages.lbp_page[
4016 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4017 sizeof(lbp_page_changeable));
4018 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4020 sizeof(lbp_page_default));
4021 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4022 value = ctl_get_opt(&lun->be_lun->options,
4024 if (value != NULL &&
4025 ctl_expand_number(value, &ival) == 0) {
4026 page->descr[0].flags |= SLBPPD_ENABLED |
4028 if (lun->be_lun->blocksize)
4029 ival /= lun->be_lun->blocksize;
4032 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4033 page->descr[0].count);
4035 value = ctl_get_opt(&lun->be_lun->options,
4037 if (value != NULL &&
4038 ctl_expand_number(value, &ival) == 0) {
4039 page->descr[1].flags |= SLBPPD_ENABLED |
4041 if (lun->be_lun->blocksize)
4042 ival /= lun->be_lun->blocksize;
4045 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4046 page->descr[1].count);
4048 value = ctl_get_opt(&lun->be_lun->options,
4049 "pool-avail-threshold");
4050 if (value != NULL &&
4051 ctl_expand_number(value, &ival) == 0) {
4052 page->descr[2].flags |= SLBPPD_ENABLED |
4054 if (lun->be_lun->blocksize)
4055 ival /= lun->be_lun->blocksize;
4058 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4059 page->descr[2].count);
4061 value = ctl_get_opt(&lun->be_lun->options,
4062 "pool-used-threshold");
4063 if (value != NULL &&
4064 ctl_expand_number(value, &ival) == 0) {
4065 page->descr[3].flags |= SLBPPD_ENABLED |
4067 if (lun->be_lun->blocksize)
4068 ival /= lun->be_lun->blocksize;
4071 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4072 page->descr[3].count);
4074 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4075 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4076 sizeof(lbp_page_default));
4077 page_index->page_data =
4078 (uint8_t *)lun->mode_pages.lbp_page;
4082 case SMS_VENDOR_SPECIFIC_PAGE:{
4083 switch (page_index->subpage) {
4084 case DBGCNF_SUBPAGE_CODE: {
4085 struct copan_debugconf_subpage *current_page,
4088 memcpy(&lun->mode_pages.debugconf_subpage[
4090 &debugconf_page_default,
4091 sizeof(debugconf_page_default));
4092 memcpy(&lun->mode_pages.debugconf_subpage[
4093 CTL_PAGE_CHANGEABLE],
4094 &debugconf_page_changeable,
4095 sizeof(debugconf_page_changeable));
4096 memcpy(&lun->mode_pages.debugconf_subpage[
4098 &debugconf_page_default,
4099 sizeof(debugconf_page_default));
4100 memcpy(&lun->mode_pages.debugconf_subpage[
4102 &debugconf_page_default,
4103 sizeof(debugconf_page_default));
4104 page_index->page_data =
4105 (uint8_t *)lun->mode_pages.debugconf_subpage;
4107 current_page = (struct copan_debugconf_subpage *)
4108 (page_index->page_data +
4109 (page_index->page_len *
4111 saved_page = (struct copan_debugconf_subpage *)
4112 (page_index->page_data +
4113 (page_index->page_len *
4118 panic("invalid subpage value %d",
4119 page_index->subpage);
4125 panic("invalid page value %d",
4126 page_index->page_code & SMPH_PC_MASK);
4131 return (CTL_RETVAL_COMPLETE);
4135 ctl_init_log_page_index(struct ctl_lun *lun)
4137 struct ctl_page_index *page_index;
4140 memcpy(&lun->log_pages.index, log_page_index_template,
4141 sizeof(log_page_index_template));
4144 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4146 page_index = &lun->log_pages.index[i];
4148 * If this is a disk-only mode page, there's no point in
4149 * setting it up. For some pages, we have to have some
4150 * basic information about the disk in order to calculate the
4153 if ((lun->be_lun->lun_type != T_DIRECT)
4154 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
4157 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4158 lun->backend->lun_attr == NULL)
4161 if (page_index->page_code != prev) {
4162 lun->log_pages.pages_page[j] = page_index->page_code;
4163 prev = page_index->page_code;
4166 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4167 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4170 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4171 lun->log_pages.index[0].page_len = j;
4172 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4173 lun->log_pages.index[1].page_len = k * 2;
4174 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4175 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4176 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page;
4177 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page);
4179 return (CTL_RETVAL_COMPLETE);
4183 hex2bin(const char *str, uint8_t *buf, int buf_size)
4188 memset(buf, 0, buf_size);
4189 while (isspace(str[0]))
4191 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4194 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4198 else if (isalpha(c))
4199 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4205 buf[i / 2] |= (c << 4);
4209 return ((i + 1) / 2);
4216 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4217 * wants us to allocate the LUN and he can block.
4218 * - ctl_softc is always set
4219 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4221 * Returns 0 for success, non-zero (errno) for failure.
4224 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4225 struct ctl_be_lun *const be_lun)
4227 struct ctl_lun *nlun, *lun;
4228 struct scsi_vpd_id_descriptor *desc;
4229 struct scsi_vpd_id_t10 *t10id;
4230 const char *eui, *naa, *scsiname, *vendor;
4231 int lun_number, i, lun_malloced;
4232 int devidlen, idlen1, idlen2 = 0, len;
4238 * We currently only support Direct Access or Processor LUN types.
4240 switch (be_lun->lun_type) {
4248 be_lun->lun_config_status(be_lun->be_lun,
4249 CTL_LUN_CONFIG_FAILURE);
4252 if (ctl_lun == NULL) {
4253 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4260 memset(lun, 0, sizeof(*lun));
4262 lun->flags = CTL_LUN_MALLOCED;
4264 /* Generate LUN ID. */
4265 devidlen = max(CTL_DEVID_MIN_LEN,
4266 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4267 idlen1 = sizeof(*t10id) + devidlen;
4268 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4269 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4270 if (scsiname != NULL) {
4271 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4272 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4274 eui = ctl_get_opt(&be_lun->options, "eui");
4276 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4278 naa = ctl_get_opt(&be_lun->options, "naa");
4280 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4282 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4283 M_CTL, M_WAITOK | M_ZERO);
4284 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4285 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4286 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4287 desc->length = idlen1;
4288 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4289 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4290 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4291 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4293 strncpy(t10id->vendor, vendor,
4294 min(sizeof(t10id->vendor), strlen(vendor)));
4296 strncpy((char *)t10id->vendor_spec_id,
4297 (char *)be_lun->device_id, devidlen);
4298 if (scsiname != NULL) {
4299 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4301 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4302 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4303 SVPD_ID_TYPE_SCSI_NAME;
4304 desc->length = idlen2;
4305 strlcpy(desc->identifier, scsiname, idlen2);
4308 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4310 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4311 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4313 desc->length = hex2bin(eui, desc->identifier, 16);
4314 desc->length = desc->length > 12 ? 16 :
4315 (desc->length > 8 ? 12 : 8);
4316 len -= 16 - desc->length;
4319 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4321 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4322 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4324 desc->length = hex2bin(naa, desc->identifier, 16);
4325 desc->length = desc->length > 8 ? 16 : 8;
4326 len -= 16 - desc->length;
4328 lun->lun_devid->len = len;
4330 mtx_lock(&ctl_softc->ctl_lock);
4332 * See if the caller requested a particular LUN number. If so, see
4333 * if it is available. Otherwise, allocate the first available LUN.
4335 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4336 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4337 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4338 mtx_unlock(&ctl_softc->ctl_lock);
4339 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4340 printf("ctl: requested LUN ID %d is higher "
4341 "than CTL_MAX_LUNS - 1 (%d)\n",
4342 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4345 * XXX KDM return an error, or just assign
4346 * another LUN ID in this case??
4348 printf("ctl: requested LUN ID %d is already "
4349 "in use\n", be_lun->req_lun_id);
4351 if (lun->flags & CTL_LUN_MALLOCED)
4353 be_lun->lun_config_status(be_lun->be_lun,
4354 CTL_LUN_CONFIG_FAILURE);
4357 lun_number = be_lun->req_lun_id;
4359 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, CTL_MAX_LUNS);
4360 if (lun_number == -1) {
4361 mtx_unlock(&ctl_softc->ctl_lock);
4362 printf("ctl: can't allocate LUN, out of LUNs\n");
4363 if (lun->flags & CTL_LUN_MALLOCED)
4365 be_lun->lun_config_status(be_lun->be_lun,
4366 CTL_LUN_CONFIG_FAILURE);
4370 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4372 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4373 lun->lun = lun_number;
4374 lun->be_lun = be_lun;
4376 * The processor LUN is always enabled. Disk LUNs come on line
4377 * disabled, and must be enabled by the backend.
4379 lun->flags |= CTL_LUN_DISABLED;
4380 lun->backend = be_lun->be;
4381 be_lun->ctl_lun = lun;
4382 be_lun->lun_id = lun_number;
4383 atomic_add_int(&be_lun->be->num_luns, 1);
4384 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE)
4385 lun->flags |= CTL_LUN_OFFLINE;
4387 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4388 lun->flags |= CTL_LUN_STOPPED;
4390 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4391 lun->flags |= CTL_LUN_INOPERABLE;
4393 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4394 lun->flags |= CTL_LUN_PRIMARY_SC;
4396 lun->ctl_softc = ctl_softc;
4398 lun->last_busy = getsbinuptime();
4400 TAILQ_INIT(&lun->ooa_queue);
4401 TAILQ_INIT(&lun->blocked_queue);
4402 STAILQ_INIT(&lun->error_list);
4403 ctl_tpc_lun_init(lun);
4406 * Initialize the mode and log page index.
4408 ctl_init_page_index(lun);
4409 ctl_init_log_page_index(lun);
4412 * Now, before we insert this lun on the lun list, set the lun
4413 * inventory changed UA for all other luns.
4415 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4416 mtx_lock(&nlun->lun_lock);
4417 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4418 mtx_unlock(&nlun->lun_lock);
4421 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4423 ctl_softc->ctl_luns[lun_number] = lun;
4425 ctl_softc->num_luns++;
4427 /* Setup statistics gathering */
4428 lun->stats.device_type = be_lun->lun_type;
4429 lun->stats.lun_number = lun_number;
4430 if (lun->stats.device_type == T_DIRECT)
4431 lun->stats.blocksize = be_lun->blocksize;
4433 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4434 for (i = 0;i < CTL_MAX_PORTS;i++)
4435 lun->stats.ports[i].targ_port = i;
4437 mtx_unlock(&ctl_softc->ctl_lock);
4439 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4446 * - LUN has already been marked invalid and any pending I/O has been taken
4450 ctl_free_lun(struct ctl_lun *lun)
4452 struct ctl_softc *softc;
4453 struct ctl_lun *nlun;
4456 softc = lun->ctl_softc;
4458 mtx_assert(&softc->ctl_lock, MA_OWNED);
4460 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4462 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4464 softc->ctl_luns[lun->lun] = NULL;
4466 if (!TAILQ_EMPTY(&lun->ooa_queue))
4467 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4472 * Tell the backend to free resources, if this LUN has a backend.
4474 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4475 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4477 ctl_tpc_lun_shutdown(lun);
4478 mtx_destroy(&lun->lun_lock);
4479 free(lun->lun_devid, M_CTL);
4480 for (i = 0; i < CTL_MAX_PORTS; i++)
4481 free(lun->pending_ua[i], M_CTL);
4482 for (i = 0; i < CTL_MAX_PORTS; i++)
4483 free(lun->pr_keys[i], M_CTL);
4484 free(lun->write_buffer, M_CTL);
4485 if (lun->flags & CTL_LUN_MALLOCED)
4488 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4489 mtx_lock(&nlun->lun_lock);
4490 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4491 mtx_unlock(&nlun->lun_lock);
4498 ctl_create_lun(struct ctl_be_lun *be_lun)
4500 struct ctl_softc *softc;
4502 softc = control_softc;
4505 * ctl_alloc_lun() should handle all potential failure cases.
4507 ctl_alloc_lun(softc, NULL, be_lun);
4511 ctl_add_lun(struct ctl_be_lun *be_lun)
4513 struct ctl_softc *softc = control_softc;
4515 mtx_lock(&softc->ctl_lock);
4516 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links);
4517 mtx_unlock(&softc->ctl_lock);
4518 wakeup(&softc->pending_lun_queue);
4524 ctl_enable_lun(struct ctl_be_lun *be_lun)
4526 struct ctl_softc *softc;
4527 struct ctl_port *port, *nport;
4528 struct ctl_lun *lun;
4531 lun = (struct ctl_lun *)be_lun->ctl_lun;
4532 softc = lun->ctl_softc;
4534 mtx_lock(&softc->ctl_lock);
4535 mtx_lock(&lun->lun_lock);
4536 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4538 * eh? Why did we get called if the LUN is already
4541 mtx_unlock(&lun->lun_lock);
4542 mtx_unlock(&softc->ctl_lock);
4545 lun->flags &= ~CTL_LUN_DISABLED;
4546 mtx_unlock(&lun->lun_lock);
4548 for (port = STAILQ_FIRST(&softc->port_list); port != NULL; port = nport) {
4549 nport = STAILQ_NEXT(port, links);
4550 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4551 port->lun_map != NULL || port->lun_enable == NULL)
4555 * Drop the lock while we call the FETD's enable routine.
4556 * This can lead to a callback into CTL (at least in the
4557 * case of the internal initiator frontend.
4559 mtx_unlock(&softc->ctl_lock);
4560 retval = port->lun_enable(port->targ_lun_arg, lun->lun);
4561 mtx_lock(&softc->ctl_lock);
4563 printf("%s: FETD %s port %d returned error "
4564 "%d for lun_enable on lun %jd\n",
4565 __func__, port->port_name, port->targ_port,
4566 retval, (intmax_t)lun->lun);
4570 mtx_unlock(&softc->ctl_lock);
4571 ctl_isc_announce_lun(lun);
4577 ctl_disable_lun(struct ctl_be_lun *be_lun)
4579 struct ctl_softc *softc;
4580 struct ctl_port *port;
4581 struct ctl_lun *lun;
4584 lun = (struct ctl_lun *)be_lun->ctl_lun;
4585 softc = lun->ctl_softc;
4587 mtx_lock(&softc->ctl_lock);
4588 mtx_lock(&lun->lun_lock);
4589 if (lun->flags & CTL_LUN_DISABLED) {
4590 mtx_unlock(&lun->lun_lock);
4591 mtx_unlock(&softc->ctl_lock);
4594 lun->flags |= CTL_LUN_DISABLED;
4595 mtx_unlock(&lun->lun_lock);
4597 STAILQ_FOREACH(port, &softc->port_list, links) {
4598 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4599 port->lun_map != NULL || port->lun_disable == NULL)
4603 * Drop the lock before we call the frontend's disable
4604 * routine, to avoid lock order reversals.
4606 * XXX KDM what happens if the frontend list changes while
4607 * we're traversing it? It's unlikely, but should be handled.
4609 mtx_unlock(&softc->ctl_lock);
4610 retval = port->lun_disable(port->targ_lun_arg, lun->lun);
4611 mtx_lock(&softc->ctl_lock);
4613 printf("%s: FETD %s port %d returned error "
4614 "%d for lun_disable on lun %jd\n",
4615 __func__, port->port_name, port->targ_port,
4616 retval, (intmax_t)lun->lun);
4620 mtx_unlock(&softc->ctl_lock);
4621 ctl_isc_announce_lun(lun);
4627 ctl_start_lun(struct ctl_be_lun *be_lun)
4629 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4631 mtx_lock(&lun->lun_lock);
4632 lun->flags &= ~CTL_LUN_STOPPED;
4633 mtx_unlock(&lun->lun_lock);
4638 ctl_stop_lun(struct ctl_be_lun *be_lun)
4640 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4642 mtx_lock(&lun->lun_lock);
4643 lun->flags |= CTL_LUN_STOPPED;
4644 mtx_unlock(&lun->lun_lock);
4649 ctl_lun_offline(struct ctl_be_lun *be_lun)
4651 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4653 mtx_lock(&lun->lun_lock);
4654 lun->flags |= CTL_LUN_OFFLINE;
4655 mtx_unlock(&lun->lun_lock);
4660 ctl_lun_online(struct ctl_be_lun *be_lun)
4662 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4664 mtx_lock(&lun->lun_lock);
4665 lun->flags &= ~CTL_LUN_OFFLINE;
4666 mtx_unlock(&lun->lun_lock);
4671 ctl_lun_primary(struct ctl_be_lun *be_lun)
4673 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4675 mtx_lock(&lun->lun_lock);
4676 lun->flags |= CTL_LUN_PRIMARY_SC;
4677 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4678 mtx_unlock(&lun->lun_lock);
4679 ctl_isc_announce_lun(lun);
4684 ctl_lun_secondary(struct ctl_be_lun *be_lun)
4686 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4688 mtx_lock(&lun->lun_lock);
4689 lun->flags &= ~CTL_LUN_PRIMARY_SC;
4690 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4691 mtx_unlock(&lun->lun_lock);
4692 ctl_isc_announce_lun(lun);
4697 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4699 struct ctl_softc *softc;
4700 struct ctl_lun *lun;
4702 lun = (struct ctl_lun *)be_lun->ctl_lun;
4703 softc = lun->ctl_softc;
4705 mtx_lock(&lun->lun_lock);
4708 * The LUN needs to be disabled before it can be marked invalid.
4710 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4711 mtx_unlock(&lun->lun_lock);
4715 * Mark the LUN invalid.
4717 lun->flags |= CTL_LUN_INVALID;
4720 * If there is nothing in the OOA queue, go ahead and free the LUN.
4721 * If we have something in the OOA queue, we'll free it when the
4722 * last I/O completes.
4724 if (TAILQ_EMPTY(&lun->ooa_queue)) {
4725 mtx_unlock(&lun->lun_lock);
4726 mtx_lock(&softc->ctl_lock);
4728 mtx_unlock(&softc->ctl_lock);
4730 mtx_unlock(&lun->lun_lock);
4736 ctl_lun_inoperable(struct ctl_be_lun *be_lun)
4738 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4740 mtx_lock(&lun->lun_lock);
4741 lun->flags |= CTL_LUN_INOPERABLE;
4742 mtx_unlock(&lun->lun_lock);
4747 ctl_lun_operable(struct ctl_be_lun *be_lun)
4749 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4751 mtx_lock(&lun->lun_lock);
4752 lun->flags &= ~CTL_LUN_INOPERABLE;
4753 mtx_unlock(&lun->lun_lock);
4758 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
4760 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4761 union ctl_ha_msg msg;
4763 mtx_lock(&lun->lun_lock);
4764 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGED);
4765 mtx_unlock(&lun->lun_lock);
4766 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4767 /* Send msg to other side. */
4768 bzero(&msg.ua, sizeof(msg.ua));
4769 msg.hdr.msg_type = CTL_MSG_UA;
4770 msg.hdr.nexus.initid = -1;
4771 msg.hdr.nexus.targ_port = -1;
4772 msg.hdr.nexus.targ_lun = lun->lun;
4773 msg.hdr.nexus.targ_mapped_lun = lun->lun;
4776 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGED;
4777 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4783 * Backend "memory move is complete" callback for requests that never
4784 * make it down to say RAIDCore's configuration code.
4787 ctl_config_move_done(union ctl_io *io)
4791 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
4792 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
4793 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
4795 if ((io->io_hdr.port_status != 0) &&
4796 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
4797 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
4799 * For hardware error sense keys, the sense key
4800 * specific value is defined to be a retry count,
4801 * but we use it to pass back an internal FETD
4802 * error code. XXX KDM Hopefully the FETD is only
4803 * using 16 bits for an error code, since that's
4804 * all the space we have in the sks field.
4806 ctl_set_internal_failure(&io->scsiio,
4809 io->io_hdr.port_status);
4812 if (ctl_debug & CTL_DEBUG_CDB_DATA)
4814 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
4815 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
4816 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
4817 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
4819 * XXX KDM just assuming a single pointer here, and not a
4820 * S/G list. If we start using S/G lists for config data,
4821 * we'll need to know how to clean them up here as well.
4823 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
4824 free(io->scsiio.kern_data_ptr, M_CTL);
4826 retval = CTL_RETVAL_COMPLETE;
4829 * XXX KDM now we need to continue data movement. Some
4831 * - call ctl_scsiio() again? We don't do this for data
4832 * writes, because for those at least we know ahead of
4833 * time where the write will go and how long it is. For
4834 * config writes, though, that information is largely
4835 * contained within the write itself, thus we need to
4836 * parse out the data again.
4838 * - Call some other function once the data is in?
4842 * XXX KDM call ctl_scsiio() again for now, and check flag
4843 * bits to see whether we're allocated or not.
4845 retval = ctl_scsiio(&io->scsiio);
4851 * This gets called by a backend driver when it is done with a
4852 * data_submit method.
4855 ctl_data_submit_done(union ctl_io *io)
4858 * If the IO_CONT flag is set, we need to call the supplied
4859 * function to continue processing the I/O, instead of completing
4862 * If there is an error, though, we don't want to keep processing.
4863 * Instead, just send status back to the initiator.
4865 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
4866 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
4867 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
4868 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
4869 io->scsiio.io_cont(io);
4876 * This gets called by a backend driver when it is done with a
4877 * configuration write.
4880 ctl_config_write_done(union ctl_io *io)
4885 * If the IO_CONT flag is set, we need to call the supplied
4886 * function to continue processing the I/O, instead of completing
4889 * If there is an error, though, we don't want to keep processing.
4890 * Instead, just send status back to the initiator.
4892 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
4893 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
4894 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
4895 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
4896 io->scsiio.io_cont(io);
4900 * Since a configuration write can be done for commands that actually
4901 * have data allocated, like write buffer, and commands that have
4902 * no data, like start/stop unit, we need to check here.
4904 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
4905 buf = io->scsiio.kern_data_ptr;
4914 ctl_config_read_done(union ctl_io *io)
4919 * If there is some error -- we are done, skip data transfer.
4921 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
4922 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
4923 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
4924 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
4925 buf = io->scsiio.kern_data_ptr;
4935 * If the IO_CONT flag is set, we need to call the supplied
4936 * function to continue processing the I/O, instead of completing
4939 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
4940 io->scsiio.io_cont(io);
4948 * SCSI release command.
4951 ctl_scsi_release(struct ctl_scsiio *ctsio)
4953 int length, longid, thirdparty_id, resv_id;
4954 struct ctl_lun *lun;
4960 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
4962 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
4963 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
4965 switch (ctsio->cdb[0]) {
4967 struct scsi_release_10 *cdb;
4969 cdb = (struct scsi_release_10 *)ctsio->cdb;
4971 if (cdb->byte2 & SR10_LONGID)
4974 thirdparty_id = cdb->thirdparty_id;
4976 resv_id = cdb->resv_id;
4977 length = scsi_2btoul(cdb->length);
4984 * XXX KDM right now, we only support LUN reservation. We don't
4985 * support 3rd party reservations, or extent reservations, which
4986 * might actually need the parameter list. If we've gotten this
4987 * far, we've got a LUN reservation. Anything else got kicked out
4988 * above. So, according to SPC, ignore the length.
4992 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
4994 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
4995 ctsio->kern_data_len = length;
4996 ctsio->kern_total_len = length;
4997 ctsio->kern_data_resid = 0;
4998 ctsio->kern_rel_offset = 0;
4999 ctsio->kern_sg_entries = 0;
5000 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5001 ctsio->be_move_done = ctl_config_move_done;
5002 ctl_datamove((union ctl_io *)ctsio);
5004 return (CTL_RETVAL_COMPLETE);
5008 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5010 mtx_lock(&lun->lun_lock);
5013 * According to SPC, it is not an error for an intiator to attempt
5014 * to release a reservation on a LUN that isn't reserved, or that
5015 * is reserved by another initiator. The reservation can only be
5016 * released, though, by the initiator who made it or by one of
5017 * several reset type events.
5019 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5020 lun->flags &= ~CTL_LUN_RESERVED;
5022 mtx_unlock(&lun->lun_lock);
5024 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5025 free(ctsio->kern_data_ptr, M_CTL);
5026 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5029 ctl_set_success(ctsio);
5030 ctl_done((union ctl_io *)ctsio);
5031 return (CTL_RETVAL_COMPLETE);
5035 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5037 int extent, thirdparty, longid;
5038 int resv_id, length;
5039 uint64_t thirdparty_id;
5040 struct ctl_lun *lun;
5050 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5052 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5053 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5055 switch (ctsio->cdb[0]) {
5057 struct scsi_reserve_10 *cdb;
5059 cdb = (struct scsi_reserve_10 *)ctsio->cdb;
5061 if (cdb->byte2 & SR10_LONGID)
5064 thirdparty_id = cdb->thirdparty_id;
5066 resv_id = cdb->resv_id;
5067 length = scsi_2btoul(cdb->length);
5073 * XXX KDM right now, we only support LUN reservation. We don't
5074 * support 3rd party reservations, or extent reservations, which
5075 * might actually need the parameter list. If we've gotten this
5076 * far, we've got a LUN reservation. Anything else got kicked out
5077 * above. So, according to SPC, ignore the length.
5081 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5083 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5084 ctsio->kern_data_len = length;
5085 ctsio->kern_total_len = length;
5086 ctsio->kern_data_resid = 0;
5087 ctsio->kern_rel_offset = 0;
5088 ctsio->kern_sg_entries = 0;
5089 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5090 ctsio->be_move_done = ctl_config_move_done;
5091 ctl_datamove((union ctl_io *)ctsio);
5093 return (CTL_RETVAL_COMPLETE);
5097 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5099 mtx_lock(&lun->lun_lock);
5100 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5101 ctl_set_reservation_conflict(ctsio);
5105 lun->flags |= CTL_LUN_RESERVED;
5106 lun->res_idx = residx;
5108 ctl_set_success(ctsio);
5111 mtx_unlock(&lun->lun_lock);
5113 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5114 free(ctsio->kern_data_ptr, M_CTL);
5115 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5118 ctl_done((union ctl_io *)ctsio);
5119 return (CTL_RETVAL_COMPLETE);
5123 ctl_start_stop(struct ctl_scsiio *ctsio)
5125 struct scsi_start_stop_unit *cdb;
5126 struct ctl_lun *lun;
5129 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5131 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5134 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5138 * We don't support the immediate bit on a stop unit. In order to
5139 * do that, we would need to code up a way to know that a stop is
5140 * pending, and hold off any new commands until it completes, one
5141 * way or another. Then we could accept or reject those commands
5142 * depending on its status. We would almost need to do the reverse
5143 * of what we do below for an immediate start -- return the copy of
5144 * the ctl_io to the FETD with status to send to the host (and to
5145 * free the copy!) and then free the original I/O once the stop
5146 * actually completes. That way, the OOA queue mechanism can work
5147 * to block commands that shouldn't proceed. Another alternative
5148 * would be to put the copy in the queue in place of the original,
5149 * and return the original back to the caller. That could be
5152 if ((cdb->byte2 & SSS_IMMED)
5153 && ((cdb->how & SSS_START) == 0)) {
5154 ctl_set_invalid_field(ctsio,
5160 ctl_done((union ctl_io *)ctsio);
5161 return (CTL_RETVAL_COMPLETE);
5164 if ((lun->flags & CTL_LUN_PR_RESERVED)
5165 && ((cdb->how & SSS_START)==0)) {
5168 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5169 if (ctl_get_prkey(lun, residx) == 0
5170 || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5172 ctl_set_reservation_conflict(ctsio);
5173 ctl_done((union ctl_io *)ctsio);
5174 return (CTL_RETVAL_COMPLETE);
5179 * If there is no backend on this device, we can't start or stop
5180 * it. In theory we shouldn't get any start/stop commands in the
5181 * first place at this level if the LUN doesn't have a backend.
5182 * That should get stopped by the command decode code.
5184 if (lun->backend == NULL) {
5185 ctl_set_invalid_opcode(ctsio);
5186 ctl_done((union ctl_io *)ctsio);
5187 return (CTL_RETVAL_COMPLETE);
5191 * XXX KDM Copan-specific offline behavior.
5192 * Figure out a reasonable way to port this?
5195 mtx_lock(&lun->lun_lock);
5197 if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
5198 && (lun->flags & CTL_LUN_OFFLINE)) {
5200 * If the LUN is offline, and the on/offline bit isn't set,
5201 * reject the start or stop. Otherwise, let it through.
5203 mtx_unlock(&lun->lun_lock);
5204 ctl_set_lun_not_ready(ctsio);
5205 ctl_done((union ctl_io *)ctsio);
5207 mtx_unlock(&lun->lun_lock);
5208 #endif /* NEEDTOPORT */
5210 * This could be a start or a stop when we're online,
5211 * or a stop/offline or start/online. A start or stop when
5212 * we're offline is covered in the case above.
5215 * In the non-immediate case, we send the request to
5216 * the backend and return status to the user when
5219 * In the immediate case, we allocate a new ctl_io
5220 * to hold a copy of the request, and send that to
5221 * the backend. We then set good status on the
5222 * user's request and return it immediately.
5224 if (cdb->byte2 & SSS_IMMED) {
5225 union ctl_io *new_io;
5227 new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5228 ctl_copy_io((union ctl_io *)ctsio, new_io);
5229 retval = lun->backend->config_write(new_io);
5230 ctl_set_success(ctsio);
5231 ctl_done((union ctl_io *)ctsio);
5233 retval = lun->backend->config_write(
5234 (union ctl_io *)ctsio);
5243 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5244 * we don't really do anything with the LBA and length fields if the user
5245 * passes them in. Instead we'll just flush out the cache for the entire
5249 ctl_sync_cache(struct ctl_scsiio *ctsio)
5251 struct ctl_lun *lun;
5252 struct ctl_softc *softc;
5253 struct ctl_lba_len_flags *lbalen;
5254 uint64_t starting_lba;
5255 uint32_t block_count;
5259 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5261 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5262 softc = lun->ctl_softc;
5265 switch (ctsio->cdb[0]) {
5266 case SYNCHRONIZE_CACHE: {
5267 struct scsi_sync_cache *cdb;
5268 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5270 starting_lba = scsi_4btoul(cdb->begin_lba);
5271 block_count = scsi_2btoul(cdb->lb_count);
5275 case SYNCHRONIZE_CACHE_16: {
5276 struct scsi_sync_cache_16 *cdb;
5277 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5279 starting_lba = scsi_8btou64(cdb->begin_lba);
5280 block_count = scsi_4btoul(cdb->lb_count);
5285 ctl_set_invalid_opcode(ctsio);
5286 ctl_done((union ctl_io *)ctsio);
5288 break; /* NOTREACHED */
5292 * We check the LBA and length, but don't do anything with them.
5293 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5294 * get flushed. This check will just help satisfy anyone who wants
5295 * to see an error for an out of range LBA.
5297 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5298 ctl_set_lba_out_of_range(ctsio);
5299 ctl_done((union ctl_io *)ctsio);
5304 * If this LUN has no backend, we can't flush the cache anyway.
5306 if (lun->backend == NULL) {
5307 ctl_set_invalid_opcode(ctsio);
5308 ctl_done((union ctl_io *)ctsio);
5312 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5313 lbalen->lba = starting_lba;
5314 lbalen->len = block_count;
5315 lbalen->flags = byte2;
5318 * Check to see whether we're configured to send the SYNCHRONIZE
5319 * CACHE command directly to the back end.
5321 mtx_lock(&lun->lun_lock);
5322 if ((softc->flags & CTL_FLAG_REAL_SYNC)
5323 && (++(lun->sync_count) >= lun->sync_interval)) {
5324 lun->sync_count = 0;
5325 mtx_unlock(&lun->lun_lock);
5326 retval = lun->backend->config_write((union ctl_io *)ctsio);
5328 mtx_unlock(&lun->lun_lock);
5329 ctl_set_success(ctsio);
5330 ctl_done((union ctl_io *)ctsio);
5339 ctl_format(struct ctl_scsiio *ctsio)
5341 struct scsi_format *cdb;
5342 struct ctl_lun *lun;
5343 int length, defect_list_len;
5345 CTL_DEBUG_PRINT(("ctl_format\n"));
5347 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5349 cdb = (struct scsi_format *)ctsio->cdb;
5352 if (cdb->byte2 & SF_FMTDATA) {
5353 if (cdb->byte2 & SF_LONGLIST)
5354 length = sizeof(struct scsi_format_header_long);
5356 length = sizeof(struct scsi_format_header_short);
5359 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5361 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5362 ctsio->kern_data_len = length;
5363 ctsio->kern_total_len = length;
5364 ctsio->kern_data_resid = 0;
5365 ctsio->kern_rel_offset = 0;
5366 ctsio->kern_sg_entries = 0;
5367 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5368 ctsio->be_move_done = ctl_config_move_done;
5369 ctl_datamove((union ctl_io *)ctsio);
5371 return (CTL_RETVAL_COMPLETE);
5374 defect_list_len = 0;
5376 if (cdb->byte2 & SF_FMTDATA) {
5377 if (cdb->byte2 & SF_LONGLIST) {
5378 struct scsi_format_header_long *header;
5380 header = (struct scsi_format_header_long *)
5381 ctsio->kern_data_ptr;
5383 defect_list_len = scsi_4btoul(header->defect_list_len);
5384 if (defect_list_len != 0) {
5385 ctl_set_invalid_field(ctsio,
5394 struct scsi_format_header_short *header;
5396 header = (struct scsi_format_header_short *)
5397 ctsio->kern_data_ptr;
5399 defect_list_len = scsi_2btoul(header->defect_list_len);
5400 if (defect_list_len != 0) {
5401 ctl_set_invalid_field(ctsio,
5413 * The format command will clear out the "Medium format corrupted"
5414 * status if set by the configuration code. That status is really
5415 * just a way to notify the host that we have lost the media, and
5416 * get them to issue a command that will basically make them think
5417 * they're blowing away the media.
5419 mtx_lock(&lun->lun_lock);
5420 lun->flags &= ~CTL_LUN_INOPERABLE;
5421 mtx_unlock(&lun->lun_lock);
5423 ctl_set_success(ctsio);
5426 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5427 free(ctsio->kern_data_ptr, M_CTL);
5428 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5431 ctl_done((union ctl_io *)ctsio);
5432 return (CTL_RETVAL_COMPLETE);
5436 ctl_read_buffer(struct ctl_scsiio *ctsio)
5438 struct scsi_read_buffer *cdb;
5439 struct ctl_lun *lun;
5440 int buffer_offset, len;
5441 static uint8_t descr[4];
5442 static uint8_t echo_descr[4] = { 0 };
5444 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5446 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5447 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5449 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA &&
5450 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR &&
5451 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) {
5452 ctl_set_invalid_field(ctsio,
5458 ctl_done((union ctl_io *)ctsio);
5459 return (CTL_RETVAL_COMPLETE);
5462 len = scsi_3btoul(cdb->length);
5463 buffer_offset = scsi_3btoul(cdb->offset);
5465 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5466 ctl_set_invalid_field(ctsio,
5472 ctl_done((union ctl_io *)ctsio);
5473 return (CTL_RETVAL_COMPLETE);
5476 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5478 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5479 ctsio->kern_data_ptr = descr;
5480 len = min(len, sizeof(descr));
5481 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5482 ctsio->kern_data_ptr = echo_descr;
5483 len = min(len, sizeof(echo_descr));
5485 if (lun->write_buffer == NULL) {
5486 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5489 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5491 ctsio->kern_data_len = len;
5492 ctsio->kern_total_len = len;
5493 ctsio->kern_data_resid = 0;
5494 ctsio->kern_rel_offset = 0;
5495 ctsio->kern_sg_entries = 0;
5496 ctl_set_success(ctsio);
5497 ctsio->be_move_done = ctl_config_move_done;
5498 ctl_datamove((union ctl_io *)ctsio);
5499 return (CTL_RETVAL_COMPLETE);
5503 ctl_write_buffer(struct ctl_scsiio *ctsio)
5505 struct scsi_write_buffer *cdb;
5506 struct ctl_lun *lun;
5507 int buffer_offset, len;
5509 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5511 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5512 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5514 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5515 ctl_set_invalid_field(ctsio,
5521 ctl_done((union ctl_io *)ctsio);
5522 return (CTL_RETVAL_COMPLETE);
5525 len = scsi_3btoul(cdb->length);
5526 buffer_offset = scsi_3btoul(cdb->offset);
5528 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5529 ctl_set_invalid_field(ctsio,
5535 ctl_done((union ctl_io *)ctsio);
5536 return (CTL_RETVAL_COMPLETE);
5540 * If we've got a kernel request that hasn't been malloced yet,
5541 * malloc it and tell the caller the data buffer is here.
5543 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5544 if (lun->write_buffer == NULL) {
5545 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5548 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5549 ctsio->kern_data_len = len;
5550 ctsio->kern_total_len = len;
5551 ctsio->kern_data_resid = 0;
5552 ctsio->kern_rel_offset = 0;
5553 ctsio->kern_sg_entries = 0;
5554 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5555 ctsio->be_move_done = ctl_config_move_done;
5556 ctl_datamove((union ctl_io *)ctsio);
5558 return (CTL_RETVAL_COMPLETE);
5561 ctl_set_success(ctsio);
5562 ctl_done((union ctl_io *)ctsio);
5563 return (CTL_RETVAL_COMPLETE);
5567 ctl_write_same(struct ctl_scsiio *ctsio)
5569 struct ctl_lun *lun;
5570 struct ctl_lba_len_flags *lbalen;
5572 uint32_t num_blocks;
5576 retval = CTL_RETVAL_COMPLETE;
5578 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5580 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5582 switch (ctsio->cdb[0]) {
5583 case WRITE_SAME_10: {
5584 struct scsi_write_same_10 *cdb;
5586 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5588 lba = scsi_4btoul(cdb->addr);
5589 num_blocks = scsi_2btoul(cdb->length);
5593 case WRITE_SAME_16: {
5594 struct scsi_write_same_16 *cdb;
5596 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5598 lba = scsi_8btou64(cdb->addr);
5599 num_blocks = scsi_4btoul(cdb->length);
5605 * We got a command we don't support. This shouldn't
5606 * happen, commands should be filtered out above us.
5608 ctl_set_invalid_opcode(ctsio);
5609 ctl_done((union ctl_io *)ctsio);
5611 return (CTL_RETVAL_COMPLETE);
5612 break; /* NOTREACHED */
5615 /* NDOB and ANCHOR flags can be used only together with UNMAP */
5616 if ((byte2 & SWS_UNMAP) == 0 &&
5617 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) {
5618 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5619 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5620 ctl_done((union ctl_io *)ctsio);
5621 return (CTL_RETVAL_COMPLETE);
5625 * The first check is to make sure we're in bounds, the second
5626 * check is to catch wrap-around problems. If the lba + num blocks
5627 * is less than the lba, then we've wrapped around and the block
5628 * range is invalid anyway.
5630 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5631 || ((lba + num_blocks) < lba)) {
5632 ctl_set_lba_out_of_range(ctsio);
5633 ctl_done((union ctl_io *)ctsio);
5634 return (CTL_RETVAL_COMPLETE);
5637 /* Zero number of blocks means "to the last logical block" */
5638 if (num_blocks == 0) {
5639 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5640 ctl_set_invalid_field(ctsio,
5646 ctl_done((union ctl_io *)ctsio);
5647 return (CTL_RETVAL_COMPLETE);
5649 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5652 len = lun->be_lun->blocksize;
5655 * If we've got a kernel request that hasn't been malloced yet,
5656 * malloc it and tell the caller the data buffer is here.
5658 if ((byte2 & SWS_NDOB) == 0 &&
5659 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5660 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5661 ctsio->kern_data_len = len;
5662 ctsio->kern_total_len = len;
5663 ctsio->kern_data_resid = 0;
5664 ctsio->kern_rel_offset = 0;
5665 ctsio->kern_sg_entries = 0;
5666 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5667 ctsio->be_move_done = ctl_config_move_done;
5668 ctl_datamove((union ctl_io *)ctsio);
5670 return (CTL_RETVAL_COMPLETE);
5673 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5675 lbalen->len = num_blocks;
5676 lbalen->flags = byte2;
5677 retval = lun->backend->config_write((union ctl_io *)ctsio);
5683 ctl_unmap(struct ctl_scsiio *ctsio)
5685 struct ctl_lun *lun;
5686 struct scsi_unmap *cdb;
5687 struct ctl_ptr_len_flags *ptrlen;
5688 struct scsi_unmap_header *hdr;
5689 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5691 uint32_t num_blocks;
5695 retval = CTL_RETVAL_COMPLETE;
5697 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5699 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5700 cdb = (struct scsi_unmap *)ctsio->cdb;
5702 len = scsi_2btoul(cdb->length);
5706 * If we've got a kernel request that hasn't been malloced yet,
5707 * malloc it and tell the caller the data buffer is here.
5709 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5710 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5711 ctsio->kern_data_len = len;
5712 ctsio->kern_total_len = len;
5713 ctsio->kern_data_resid = 0;
5714 ctsio->kern_rel_offset = 0;
5715 ctsio->kern_sg_entries = 0;
5716 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5717 ctsio->be_move_done = ctl_config_move_done;
5718 ctl_datamove((union ctl_io *)ctsio);
5720 return (CTL_RETVAL_COMPLETE);
5723 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5724 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5725 if (len < sizeof (*hdr) ||
5726 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5727 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5728 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5729 ctl_set_invalid_field(ctsio,
5737 len = scsi_2btoul(hdr->desc_length);
5738 buf = (struct scsi_unmap_desc *)(hdr + 1);
5739 end = buf + len / sizeof(*buf);
5742 for (range = buf; range < end; range++) {
5743 lba = scsi_8btou64(range->lba);
5744 num_blocks = scsi_4btoul(range->length);
5745 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5746 || ((lba + num_blocks) < lba)) {
5747 ctl_set_lba_out_of_range(ctsio);
5748 ctl_done((union ctl_io *)ctsio);
5749 return (CTL_RETVAL_COMPLETE);
5751 if (num_blocks != 0)
5756 * Block backend can not handle zero last range.
5757 * Filter it out and return if there is nothing left.
5759 len = (uint8_t *)endnz - (uint8_t *)buf;
5761 ctl_set_success(ctsio);
5765 mtx_lock(&lun->lun_lock);
5766 ptrlen = (struct ctl_ptr_len_flags *)
5767 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5768 ptrlen->ptr = (void *)buf;
5770 ptrlen->flags = byte2;
5771 ctl_check_blocked(lun);
5772 mtx_unlock(&lun->lun_lock);
5774 retval = lun->backend->config_write((union ctl_io *)ctsio);
5778 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5779 free(ctsio->kern_data_ptr, M_CTL);
5780 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5782 ctl_done((union ctl_io *)ctsio);
5783 return (CTL_RETVAL_COMPLETE);
5787 * Note that this function currently doesn't actually do anything inside
5788 * CTL to enforce things if the DQue bit is turned on.
5790 * Also note that this function can't be used in the default case, because
5791 * the DQue bit isn't set in the changeable mask for the control mode page
5792 * anyway. This is just here as an example for how to implement a page
5793 * handler, and a placeholder in case we want to allow the user to turn
5794 * tagged queueing on and off.
5796 * The D_SENSE bit handling is functional, however, and will turn
5797 * descriptor sense on and off for a given LUN.
5800 ctl_control_page_handler(struct ctl_scsiio *ctsio,
5801 struct ctl_page_index *page_index, uint8_t *page_ptr)
5803 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
5804 struct ctl_lun *lun;
5808 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5809 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5812 user_cp = (struct scsi_control_page *)page_ptr;
5813 current_cp = (struct scsi_control_page *)
5814 (page_index->page_data + (page_index->page_len *
5816 saved_cp = (struct scsi_control_page *)
5817 (page_index->page_data + (page_index->page_len *
5820 mtx_lock(&lun->lun_lock);
5821 if (((current_cp->rlec & SCP_DSENSE) == 0)
5822 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
5824 * Descriptor sense is currently turned off and the user
5825 * wants to turn it on.
5827 current_cp->rlec |= SCP_DSENSE;
5828 saved_cp->rlec |= SCP_DSENSE;
5829 lun->flags |= CTL_LUN_SENSE_DESC;
5831 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
5832 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
5834 * Descriptor sense is currently turned on, and the user
5835 * wants to turn it off.
5837 current_cp->rlec &= ~SCP_DSENSE;
5838 saved_cp->rlec &= ~SCP_DSENSE;
5839 lun->flags &= ~CTL_LUN_SENSE_DESC;
5842 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) !=
5843 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) {
5844 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
5845 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
5846 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
5847 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
5850 if ((current_cp->eca_and_aen & SCP_SWP) !=
5851 (user_cp->eca_and_aen & SCP_SWP)) {
5852 current_cp->eca_and_aen &= ~SCP_SWP;
5853 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
5854 saved_cp->eca_and_aen &= ~SCP_SWP;
5855 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
5859 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5860 mtx_unlock(&lun->lun_lock);
5866 ctl_caching_sp_handler(struct ctl_scsiio *ctsio,
5867 struct ctl_page_index *page_index, uint8_t *page_ptr)
5869 struct scsi_caching_page *current_cp, *saved_cp, *user_cp;
5870 struct ctl_lun *lun;
5874 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5875 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5878 user_cp = (struct scsi_caching_page *)page_ptr;
5879 current_cp = (struct scsi_caching_page *)
5880 (page_index->page_data + (page_index->page_len *
5882 saved_cp = (struct scsi_caching_page *)
5883 (page_index->page_data + (page_index->page_len *
5886 mtx_lock(&lun->lun_lock);
5887 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) !=
5888 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) {
5889 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
5890 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
5891 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
5892 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
5896 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5897 mtx_unlock(&lun->lun_lock);
5903 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
5904 struct ctl_page_index *page_index,
5910 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
5915 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
5916 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
5917 printf("page data:");
5919 printf(" %.2x",page_ptr[i]);
5925 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
5926 struct ctl_page_index *page_index,
5929 struct copan_debugconf_subpage *page;
5931 page = (struct copan_debugconf_subpage *)page_index->page_data +
5932 (page_index->page_len * pc);
5935 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
5936 case SMS_PAGE_CTRL_DEFAULT >> 6:
5937 case SMS_PAGE_CTRL_SAVED >> 6:
5939 * We don't update the changable or default bits for this page.
5942 case SMS_PAGE_CTRL_CURRENT >> 6:
5943 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
5944 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
5948 EPRINT(0, "Invalid PC %d!!", pc);
5949 #endif /* NEEDTOPORT */
5957 ctl_do_mode_select(union ctl_io *io)
5959 struct scsi_mode_page_header *page_header;
5960 struct ctl_page_index *page_index;
5961 struct ctl_scsiio *ctsio;
5962 int control_dev, page_len;
5963 int page_len_offset, page_len_size;
5964 union ctl_modepage_info *modepage_info;
5965 struct ctl_lun *lun;
5966 int *len_left, *len_used;
5969 ctsio = &io->scsiio;
5972 retval = CTL_RETVAL_COMPLETE;
5974 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5976 if (lun->be_lun->lun_type != T_DIRECT)
5981 modepage_info = (union ctl_modepage_info *)
5982 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
5983 len_left = &modepage_info->header.len_left;
5984 len_used = &modepage_info->header.len_used;
5988 page_header = (struct scsi_mode_page_header *)
5989 (ctsio->kern_data_ptr + *len_used);
5991 if (*len_left == 0) {
5992 free(ctsio->kern_data_ptr, M_CTL);
5993 ctl_set_success(ctsio);
5994 ctl_done((union ctl_io *)ctsio);
5995 return (CTL_RETVAL_COMPLETE);
5996 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
5998 free(ctsio->kern_data_ptr, M_CTL);
5999 ctl_set_param_len_error(ctsio);
6000 ctl_done((union ctl_io *)ctsio);
6001 return (CTL_RETVAL_COMPLETE);
6003 } else if ((page_header->page_code & SMPH_SPF)
6004 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6006 free(ctsio->kern_data_ptr, M_CTL);
6007 ctl_set_param_len_error(ctsio);
6008 ctl_done((union ctl_io *)ctsio);
6009 return (CTL_RETVAL_COMPLETE);
6014 * XXX KDM should we do something with the block descriptor?
6016 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6018 if ((control_dev != 0)
6019 && (lun->mode_pages.index[i].page_flags &
6020 CTL_PAGE_FLAG_DISK_ONLY))
6023 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
6024 (page_header->page_code & SMPH_PC_MASK))
6028 * If neither page has a subpage code, then we've got a
6031 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
6032 && ((page_header->page_code & SMPH_SPF) == 0)) {
6033 page_index = &lun->mode_pages.index[i];
6034 page_len = page_header->page_length;
6039 * If both pages have subpages, then the subpage numbers
6042 if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
6043 && (page_header->page_code & SMPH_SPF)) {
6044 struct scsi_mode_page_header_sp *sph;
6046 sph = (struct scsi_mode_page_header_sp *)page_header;
6048 if (lun->mode_pages.index[i].subpage ==
6050 page_index = &lun->mode_pages.index[i];
6051 page_len = scsi_2btoul(sph->page_length);
6058 * If we couldn't find the page, or if we don't have a mode select
6059 * handler for it, send back an error to the user.
6061 if ((page_index == NULL)
6062 || (page_index->select_handler == NULL)) {
6063 ctl_set_invalid_field(ctsio,
6066 /*field*/ *len_used,
6069 free(ctsio->kern_data_ptr, M_CTL);
6070 ctl_done((union ctl_io *)ctsio);
6071 return (CTL_RETVAL_COMPLETE);
6074 if (page_index->page_code & SMPH_SPF) {
6075 page_len_offset = 2;
6079 page_len_offset = 1;
6083 * If the length the initiator gives us isn't the one we specify in
6084 * the mode page header, or if they didn't specify enough data in
6085 * the CDB to avoid truncating this page, kick out the request.
6087 if ((page_len != (page_index->page_len - page_len_offset -
6089 || (*len_left < page_index->page_len)) {
6092 ctl_set_invalid_field(ctsio,
6095 /*field*/ *len_used + page_len_offset,
6098 free(ctsio->kern_data_ptr, M_CTL);
6099 ctl_done((union ctl_io *)ctsio);
6100 return (CTL_RETVAL_COMPLETE);
6104 * Run through the mode page, checking to make sure that the bits
6105 * the user changed are actually legal for him to change.
6107 for (i = 0; i < page_index->page_len; i++) {
6108 uint8_t *user_byte, *change_mask, *current_byte;
6112 user_byte = (uint8_t *)page_header + i;
6113 change_mask = page_index->page_data +
6114 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6115 current_byte = page_index->page_data +
6116 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6119 * Check to see whether the user set any bits in this byte
6120 * that he is not allowed to set.
6122 if ((*user_byte & ~(*change_mask)) ==
6123 (*current_byte & ~(*change_mask)))
6127 * Go through bit by bit to determine which one is illegal.
6130 for (j = 7; j >= 0; j--) {
6131 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6132 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6137 ctl_set_invalid_field(ctsio,
6140 /*field*/ *len_used + i,
6143 free(ctsio->kern_data_ptr, M_CTL);
6144 ctl_done((union ctl_io *)ctsio);
6145 return (CTL_RETVAL_COMPLETE);
6149 * Decrement these before we call the page handler, since we may
6150 * end up getting called back one way or another before the handler
6151 * returns to this context.
6153 *len_left -= page_index->page_len;
6154 *len_used += page_index->page_len;
6156 retval = page_index->select_handler(ctsio, page_index,
6157 (uint8_t *)page_header);
6160 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6161 * wait until this queued command completes to finish processing
6162 * the mode page. If it returns anything other than
6163 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6164 * already set the sense information, freed the data pointer, and
6165 * completed the io for us.
6167 if (retval != CTL_RETVAL_COMPLETE)
6168 goto bailout_no_done;
6171 * If the initiator sent us more than one page, parse the next one.
6176 ctl_set_success(ctsio);
6177 free(ctsio->kern_data_ptr, M_CTL);
6178 ctl_done((union ctl_io *)ctsio);
6182 return (CTL_RETVAL_COMPLETE);
6187 ctl_mode_select(struct ctl_scsiio *ctsio)
6189 int param_len, pf, sp;
6190 int header_size, bd_len;
6191 int len_left, len_used;
6192 struct ctl_page_index *page_index;
6193 struct ctl_lun *lun;
6194 int control_dev, page_len;
6195 union ctl_modepage_info *modepage_info;
6207 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6209 if (lun->be_lun->lun_type != T_DIRECT)
6214 switch (ctsio->cdb[0]) {
6215 case MODE_SELECT_6: {
6216 struct scsi_mode_select_6 *cdb;
6218 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6220 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6221 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6223 param_len = cdb->length;
6224 header_size = sizeof(struct scsi_mode_header_6);
6227 case MODE_SELECT_10: {
6228 struct scsi_mode_select_10 *cdb;
6230 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6232 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6233 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6235 param_len = scsi_2btoul(cdb->length);
6236 header_size = sizeof(struct scsi_mode_header_10);
6240 ctl_set_invalid_opcode(ctsio);
6241 ctl_done((union ctl_io *)ctsio);
6242 return (CTL_RETVAL_COMPLETE);
6243 break; /* NOTREACHED */
6248 * "A parameter list length of zero indicates that the Data-Out Buffer
6249 * shall be empty. This condition shall not be considered as an error."
6251 if (param_len == 0) {
6252 ctl_set_success(ctsio);
6253 ctl_done((union ctl_io *)ctsio);
6254 return (CTL_RETVAL_COMPLETE);
6258 * Since we'll hit this the first time through, prior to
6259 * allocation, we don't need to free a data buffer here.
6261 if (param_len < header_size) {
6262 ctl_set_param_len_error(ctsio);
6263 ctl_done((union ctl_io *)ctsio);
6264 return (CTL_RETVAL_COMPLETE);
6268 * Allocate the data buffer and grab the user's data. In theory,
6269 * we shouldn't have to sanity check the parameter list length here
6270 * because the maximum size is 64K. We should be able to malloc
6271 * that much without too many problems.
6273 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6274 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6275 ctsio->kern_data_len = param_len;
6276 ctsio->kern_total_len = param_len;
6277 ctsio->kern_data_resid = 0;
6278 ctsio->kern_rel_offset = 0;
6279 ctsio->kern_sg_entries = 0;
6280 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6281 ctsio->be_move_done = ctl_config_move_done;
6282 ctl_datamove((union ctl_io *)ctsio);
6284 return (CTL_RETVAL_COMPLETE);
6287 switch (ctsio->cdb[0]) {
6288 case MODE_SELECT_6: {
6289 struct scsi_mode_header_6 *mh6;
6291 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6292 bd_len = mh6->blk_desc_len;
6295 case MODE_SELECT_10: {
6296 struct scsi_mode_header_10 *mh10;
6298 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6299 bd_len = scsi_2btoul(mh10->blk_desc_len);
6303 panic("Invalid CDB type %#x", ctsio->cdb[0]);
6307 if (param_len < (header_size + bd_len)) {
6308 free(ctsio->kern_data_ptr, M_CTL);
6309 ctl_set_param_len_error(ctsio);
6310 ctl_done((union ctl_io *)ctsio);
6311 return (CTL_RETVAL_COMPLETE);
6315 * Set the IO_CONT flag, so that if this I/O gets passed to
6316 * ctl_config_write_done(), it'll get passed back to
6317 * ctl_do_mode_select() for further processing, or completion if
6320 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6321 ctsio->io_cont = ctl_do_mode_select;
6323 modepage_info = (union ctl_modepage_info *)
6324 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6326 memset(modepage_info, 0, sizeof(*modepage_info));
6328 len_left = param_len - header_size - bd_len;
6329 len_used = header_size + bd_len;
6331 modepage_info->header.len_left = len_left;
6332 modepage_info->header.len_used = len_used;
6334 return (ctl_do_mode_select((union ctl_io *)ctsio));
6338 ctl_mode_sense(struct ctl_scsiio *ctsio)
6340 struct ctl_lun *lun;
6341 int pc, page_code, dbd, llba, subpage;
6342 int alloc_len, page_len, header_len, total_len;
6343 struct scsi_mode_block_descr *block_desc;
6344 struct ctl_page_index *page_index;
6352 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6354 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6356 if (lun->be_lun->lun_type != T_DIRECT)
6361 switch (ctsio->cdb[0]) {
6362 case MODE_SENSE_6: {
6363 struct scsi_mode_sense_6 *cdb;
6365 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6367 header_len = sizeof(struct scsi_mode_hdr_6);
6368 if (cdb->byte2 & SMS_DBD)
6371 header_len += sizeof(struct scsi_mode_block_descr);
6373 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6374 page_code = cdb->page & SMS_PAGE_CODE;
6375 subpage = cdb->subpage;
6376 alloc_len = cdb->length;
6379 case MODE_SENSE_10: {
6380 struct scsi_mode_sense_10 *cdb;
6382 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6384 header_len = sizeof(struct scsi_mode_hdr_10);
6386 if (cdb->byte2 & SMS_DBD)
6389 header_len += sizeof(struct scsi_mode_block_descr);
6390 if (cdb->byte2 & SMS10_LLBAA)
6392 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6393 page_code = cdb->page & SMS_PAGE_CODE;
6394 subpage = cdb->subpage;
6395 alloc_len = scsi_2btoul(cdb->length);
6399 ctl_set_invalid_opcode(ctsio);
6400 ctl_done((union ctl_io *)ctsio);
6401 return (CTL_RETVAL_COMPLETE);
6402 break; /* NOTREACHED */
6406 * We have to make a first pass through to calculate the size of
6407 * the pages that match the user's query. Then we allocate enough
6408 * memory to hold it, and actually copy the data into the buffer.
6410 switch (page_code) {
6411 case SMS_ALL_PAGES_PAGE: {
6417 * At the moment, values other than 0 and 0xff here are
6418 * reserved according to SPC-3.
6420 if ((subpage != SMS_SUBPAGE_PAGE_0)
6421 && (subpage != SMS_SUBPAGE_ALL)) {
6422 ctl_set_invalid_field(ctsio,
6428 ctl_done((union ctl_io *)ctsio);
6429 return (CTL_RETVAL_COMPLETE);
6432 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6433 if ((control_dev != 0)
6434 && (lun->mode_pages.index[i].page_flags &
6435 CTL_PAGE_FLAG_DISK_ONLY))
6439 * We don't use this subpage if the user didn't
6440 * request all subpages.
6442 if ((lun->mode_pages.index[i].subpage != 0)
6443 && (subpage == SMS_SUBPAGE_PAGE_0))
6447 printf("found page %#x len %d\n",
6448 lun->mode_pages.index[i].page_code &
6450 lun->mode_pages.index[i].page_len);
6452 page_len += lun->mode_pages.index[i].page_len;
6461 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6462 /* Look for the right page code */
6463 if ((lun->mode_pages.index[i].page_code &
6464 SMPH_PC_MASK) != page_code)
6467 /* Look for the right subpage or the subpage wildcard*/
6468 if ((lun->mode_pages.index[i].subpage != subpage)
6469 && (subpage != SMS_SUBPAGE_ALL))
6472 /* Make sure the page is supported for this dev type */
6473 if ((control_dev != 0)
6474 && (lun->mode_pages.index[i].page_flags &
6475 CTL_PAGE_FLAG_DISK_ONLY))
6479 printf("found page %#x len %d\n",
6480 lun->mode_pages.index[i].page_code &
6482 lun->mode_pages.index[i].page_len);
6485 page_len += lun->mode_pages.index[i].page_len;
6488 if (page_len == 0) {
6489 ctl_set_invalid_field(ctsio,
6495 ctl_done((union ctl_io *)ctsio);
6496 return (CTL_RETVAL_COMPLETE);
6502 total_len = header_len + page_len;
6504 printf("header_len = %d, page_len = %d, total_len = %d\n",
6505 header_len, page_len, total_len);
6508 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6509 ctsio->kern_sg_entries = 0;
6510 ctsio->kern_data_resid = 0;
6511 ctsio->kern_rel_offset = 0;
6512 if (total_len < alloc_len) {
6513 ctsio->residual = alloc_len - total_len;
6514 ctsio->kern_data_len = total_len;
6515 ctsio->kern_total_len = total_len;
6517 ctsio->residual = 0;
6518 ctsio->kern_data_len = alloc_len;
6519 ctsio->kern_total_len = alloc_len;
6522 switch (ctsio->cdb[0]) {
6523 case MODE_SENSE_6: {
6524 struct scsi_mode_hdr_6 *header;
6526 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6528 header->datalen = MIN(total_len - 1, 254);
6529 if (control_dev == 0) {
6530 header->dev_specific = 0x10; /* DPOFUA */
6531 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6532 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6533 .eca_and_aen & SCP_SWP) != 0)
6534 header->dev_specific |= 0x80; /* WP */
6537 header->block_descr_len = 0;
6539 header->block_descr_len =
6540 sizeof(struct scsi_mode_block_descr);
6541 block_desc = (struct scsi_mode_block_descr *)&header[1];
6544 case MODE_SENSE_10: {
6545 struct scsi_mode_hdr_10 *header;
6548 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6550 datalen = MIN(total_len - 2, 65533);
6551 scsi_ulto2b(datalen, header->datalen);
6552 if (control_dev == 0) {
6553 header->dev_specific = 0x10; /* DPOFUA */
6554 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6555 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6556 .eca_and_aen & SCP_SWP) != 0)
6557 header->dev_specific |= 0x80; /* WP */
6560 scsi_ulto2b(0, header->block_descr_len);
6562 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6563 header->block_descr_len);
6564 block_desc = (struct scsi_mode_block_descr *)&header[1];
6568 panic("invalid CDB type %#x", ctsio->cdb[0]);
6569 break; /* NOTREACHED */
6573 * If we've got a disk, use its blocksize in the block
6574 * descriptor. Otherwise, just set it to 0.
6577 if (control_dev == 0)
6578 scsi_ulto3b(lun->be_lun->blocksize,
6579 block_desc->block_len);
6581 scsi_ulto3b(0, block_desc->block_len);
6584 switch (page_code) {
6585 case SMS_ALL_PAGES_PAGE: {
6588 data_used = header_len;
6589 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6590 struct ctl_page_index *page_index;
6592 page_index = &lun->mode_pages.index[i];
6594 if ((control_dev != 0)
6595 && (page_index->page_flags &
6596 CTL_PAGE_FLAG_DISK_ONLY))
6600 * We don't use this subpage if the user didn't
6601 * request all subpages. We already checked (above)
6602 * to make sure the user only specified a subpage
6603 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6605 if ((page_index->subpage != 0)
6606 && (subpage == SMS_SUBPAGE_PAGE_0))
6610 * Call the handler, if it exists, to update the
6611 * page to the latest values.
6613 if (page_index->sense_handler != NULL)
6614 page_index->sense_handler(ctsio, page_index,pc);
6616 memcpy(ctsio->kern_data_ptr + data_used,
6617 page_index->page_data +
6618 (page_index->page_len * pc),
6619 page_index->page_len);
6620 data_used += page_index->page_len;
6627 data_used = header_len;
6629 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6630 struct ctl_page_index *page_index;
6632 page_index = &lun->mode_pages.index[i];
6634 /* Look for the right page code */
6635 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6638 /* Look for the right subpage or the subpage wildcard*/
6639 if ((page_index->subpage != subpage)
6640 && (subpage != SMS_SUBPAGE_ALL))
6643 /* Make sure the page is supported for this dev type */
6644 if ((control_dev != 0)
6645 && (page_index->page_flags &
6646 CTL_PAGE_FLAG_DISK_ONLY))
6650 * Call the handler, if it exists, to update the
6651 * page to the latest values.
6653 if (page_index->sense_handler != NULL)
6654 page_index->sense_handler(ctsio, page_index,pc);
6656 memcpy(ctsio->kern_data_ptr + data_used,
6657 page_index->page_data +
6658 (page_index->page_len * pc),
6659 page_index->page_len);
6660 data_used += page_index->page_len;
6666 ctl_set_success(ctsio);
6667 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6668 ctsio->be_move_done = ctl_config_move_done;
6669 ctl_datamove((union ctl_io *)ctsio);
6670 return (CTL_RETVAL_COMPLETE);
6674 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6675 struct ctl_page_index *page_index,
6678 struct ctl_lun *lun;
6679 struct scsi_log_param_header *phdr;
6683 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6684 data = page_index->page_data;
6686 if (lun->backend->lun_attr != NULL &&
6687 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6689 phdr = (struct scsi_log_param_header *)data;
6690 scsi_ulto2b(0x0001, phdr->param_code);
6691 phdr->param_control = SLP_LBIN | SLP_LP;
6692 phdr->param_len = 8;
6693 data = (uint8_t *)(phdr + 1);
6694 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6695 data[4] = 0x02; /* per-pool */
6696 data += phdr->param_len;
6699 if (lun->backend->lun_attr != NULL &&
6700 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6702 phdr = (struct scsi_log_param_header *)data;
6703 scsi_ulto2b(0x0002, phdr->param_code);
6704 phdr->param_control = SLP_LBIN | SLP_LP;
6705 phdr->param_len = 8;
6706 data = (uint8_t *)(phdr + 1);
6707 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6708 data[4] = 0x01; /* per-LUN */
6709 data += phdr->param_len;
6712 if (lun->backend->lun_attr != NULL &&
6713 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6715 phdr = (struct scsi_log_param_header *)data;
6716 scsi_ulto2b(0x00f1, phdr->param_code);
6717 phdr->param_control = SLP_LBIN | SLP_LP;
6718 phdr->param_len = 8;
6719 data = (uint8_t *)(phdr + 1);
6720 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6721 data[4] = 0x02; /* per-pool */
6722 data += phdr->param_len;
6725 if (lun->backend->lun_attr != NULL &&
6726 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6728 phdr = (struct scsi_log_param_header *)data;
6729 scsi_ulto2b(0x00f2, phdr->param_code);
6730 phdr->param_control = SLP_LBIN | SLP_LP;
6731 phdr->param_len = 8;
6732 data = (uint8_t *)(phdr + 1);
6733 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6734 data[4] = 0x02; /* per-pool */
6735 data += phdr->param_len;
6738 page_index->page_len = data - page_index->page_data;
6743 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
6744 struct ctl_page_index *page_index,
6747 struct ctl_lun *lun;
6748 struct stat_page *data;
6749 uint64_t rn, wn, rb, wb;
6750 struct bintime rt, wt;
6753 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6754 data = (struct stat_page *)page_index->page_data;
6756 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
6757 data->sap.hdr.param_control = SLP_LBIN;
6758 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
6759 sizeof(struct scsi_log_param_header);
6760 rn = wn = rb = wb = 0;
6763 for (i = 0; i < CTL_MAX_PORTS; i++) {
6764 rn += lun->stats.ports[i].operations[CTL_STATS_READ];
6765 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE];
6766 rb += lun->stats.ports[i].bytes[CTL_STATS_READ];
6767 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE];
6768 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]);
6769 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]);
6771 scsi_u64to8b(rn, data->sap.read_num);
6772 scsi_u64to8b(wn, data->sap.write_num);
6773 if (lun->stats.blocksize > 0) {
6774 scsi_u64to8b(wb / lun->stats.blocksize,
6775 data->sap.recvieved_lba);
6776 scsi_u64to8b(rb / lun->stats.blocksize,
6777 data->sap.transmitted_lba);
6779 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000),
6780 data->sap.read_int);
6781 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000),
6782 data->sap.write_int);
6783 scsi_u64to8b(0, data->sap.weighted_num);
6784 scsi_u64to8b(0, data->sap.weighted_int);
6785 scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
6786 data->it.hdr.param_control = SLP_LBIN;
6787 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
6788 sizeof(struct scsi_log_param_header);
6790 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
6792 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
6793 data->it.hdr.param_control = SLP_LBIN;
6794 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
6795 sizeof(struct scsi_log_param_header);
6796 scsi_ulto4b(3, data->ti.exponent);
6797 scsi_ulto4b(1, data->ti.integer);
6799 page_index->page_len = sizeof(*data);
6804 ctl_log_sense(struct ctl_scsiio *ctsio)
6806 struct ctl_lun *lun;
6807 int i, pc, page_code, subpage;
6808 int alloc_len, total_len;
6809 struct ctl_page_index *page_index;
6810 struct scsi_log_sense *cdb;
6811 struct scsi_log_header *header;
6813 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6815 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6816 cdb = (struct scsi_log_sense *)ctsio->cdb;
6817 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6818 page_code = cdb->page & SLS_PAGE_CODE;
6819 subpage = cdb->subpage;
6820 alloc_len = scsi_2btoul(cdb->length);
6823 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6824 page_index = &lun->log_pages.index[i];
6826 /* Look for the right page code */
6827 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6830 /* Look for the right subpage or the subpage wildcard*/
6831 if (page_index->subpage != subpage)
6836 if (i >= CTL_NUM_LOG_PAGES) {
6837 ctl_set_invalid_field(ctsio,
6843 ctl_done((union ctl_io *)ctsio);
6844 return (CTL_RETVAL_COMPLETE);
6847 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6849 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6850 ctsio->kern_sg_entries = 0;
6851 ctsio->kern_data_resid = 0;
6852 ctsio->kern_rel_offset = 0;
6853 if (total_len < alloc_len) {
6854 ctsio->residual = alloc_len - total_len;
6855 ctsio->kern_data_len = total_len;
6856 ctsio->kern_total_len = total_len;
6858 ctsio->residual = 0;
6859 ctsio->kern_data_len = alloc_len;
6860 ctsio->kern_total_len = alloc_len;
6863 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
6864 header->page = page_index->page_code;
6865 if (page_index->subpage) {
6866 header->page |= SL_SPF;
6867 header->subpage = page_index->subpage;
6869 scsi_ulto2b(page_index->page_len, header->datalen);
6872 * Call the handler, if it exists, to update the
6873 * page to the latest values.
6875 if (page_index->sense_handler != NULL)
6876 page_index->sense_handler(ctsio, page_index, pc);
6878 memcpy(header + 1, page_index->page_data, page_index->page_len);
6880 ctl_set_success(ctsio);
6881 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6882 ctsio->be_move_done = ctl_config_move_done;
6883 ctl_datamove((union ctl_io *)ctsio);
6884 return (CTL_RETVAL_COMPLETE);
6888 ctl_read_capacity(struct ctl_scsiio *ctsio)
6890 struct scsi_read_capacity *cdb;
6891 struct scsi_read_capacity_data *data;
6892 struct ctl_lun *lun;
6895 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6897 cdb = (struct scsi_read_capacity *)ctsio->cdb;
6899 lba = scsi_4btoul(cdb->addr);
6900 if (((cdb->pmi & SRC_PMI) == 0)
6902 ctl_set_invalid_field(/*ctsio*/ ctsio,
6908 ctl_done((union ctl_io *)ctsio);
6909 return (CTL_RETVAL_COMPLETE);
6912 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6914 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6915 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6916 ctsio->residual = 0;
6917 ctsio->kern_data_len = sizeof(*data);
6918 ctsio->kern_total_len = sizeof(*data);
6919 ctsio->kern_data_resid = 0;
6920 ctsio->kern_rel_offset = 0;
6921 ctsio->kern_sg_entries = 0;
6924 * If the maximum LBA is greater than 0xfffffffe, the user must
6925 * issue a SERVICE ACTION IN (16) command, with the read capacity
6926 * serivce action set.
6928 if (lun->be_lun->maxlba > 0xfffffffe)
6929 scsi_ulto4b(0xffffffff, data->addr);
6931 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6934 * XXX KDM this may not be 512 bytes...
6936 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6938 ctl_set_success(ctsio);
6939 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6940 ctsio->be_move_done = ctl_config_move_done;
6941 ctl_datamove((union ctl_io *)ctsio);
6942 return (CTL_RETVAL_COMPLETE);
6946 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
6948 struct scsi_read_capacity_16 *cdb;
6949 struct scsi_read_capacity_data_long *data;
6950 struct ctl_lun *lun;
6954 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
6956 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
6958 alloc_len = scsi_4btoul(cdb->alloc_len);
6959 lba = scsi_8btou64(cdb->addr);
6961 if ((cdb->reladr & SRC16_PMI)
6963 ctl_set_invalid_field(/*ctsio*/ ctsio,
6969 ctl_done((union ctl_io *)ctsio);
6970 return (CTL_RETVAL_COMPLETE);
6973 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6975 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6976 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
6978 if (sizeof(*data) < alloc_len) {
6979 ctsio->residual = alloc_len - sizeof(*data);
6980 ctsio->kern_data_len = sizeof(*data);
6981 ctsio->kern_total_len = sizeof(*data);
6983 ctsio->residual = 0;
6984 ctsio->kern_data_len = alloc_len;
6985 ctsio->kern_total_len = alloc_len;
6987 ctsio->kern_data_resid = 0;
6988 ctsio->kern_rel_offset = 0;
6989 ctsio->kern_sg_entries = 0;
6991 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
6992 /* XXX KDM this may not be 512 bytes... */
6993 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6994 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
6995 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
6996 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
6997 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
6999 ctl_set_success(ctsio);
7000 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7001 ctsio->be_move_done = ctl_config_move_done;
7002 ctl_datamove((union ctl_io *)ctsio);
7003 return (CTL_RETVAL_COMPLETE);
7007 ctl_get_lba_status(struct ctl_scsiio *ctsio)
7009 struct scsi_get_lba_status *cdb;
7010 struct scsi_get_lba_status_data *data;
7011 struct ctl_lun *lun;
7012 struct ctl_lba_len_flags *lbalen;
7014 uint32_t alloc_len, total_len;
7017 CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
7019 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7020 cdb = (struct scsi_get_lba_status *)ctsio->cdb;
7021 lba = scsi_8btou64(cdb->addr);
7022 alloc_len = scsi_4btoul(cdb->alloc_len);
7024 if (lba > lun->be_lun->maxlba) {
7025 ctl_set_lba_out_of_range(ctsio);
7026 ctl_done((union ctl_io *)ctsio);
7027 return (CTL_RETVAL_COMPLETE);
7030 total_len = sizeof(*data) + sizeof(data->descr[0]);
7031 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7032 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
7034 if (total_len < alloc_len) {
7035 ctsio->residual = alloc_len - total_len;
7036 ctsio->kern_data_len = total_len;
7037 ctsio->kern_total_len = total_len;
7039 ctsio->residual = 0;
7040 ctsio->kern_data_len = alloc_len;
7041 ctsio->kern_total_len = alloc_len;
7043 ctsio->kern_data_resid = 0;
7044 ctsio->kern_rel_offset = 0;
7045 ctsio->kern_sg_entries = 0;
7047 /* Fill dummy data in case backend can't tell anything. */
7048 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
7049 scsi_u64to8b(lba, data->descr[0].addr);
7050 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
7051 data->descr[0].length);
7052 data->descr[0].status = 0; /* Mapped or unknown. */
7054 ctl_set_success(ctsio);
7055 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7056 ctsio->be_move_done = ctl_config_move_done;
7058 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
7060 lbalen->len = total_len;
7062 retval = lun->backend->config_read((union ctl_io *)ctsio);
7063 return (CTL_RETVAL_COMPLETE);
7067 ctl_read_defect(struct ctl_scsiio *ctsio)
7069 struct scsi_read_defect_data_10 *ccb10;
7070 struct scsi_read_defect_data_12 *ccb12;
7071 struct scsi_read_defect_data_hdr_10 *data10;
7072 struct scsi_read_defect_data_hdr_12 *data12;
7073 uint32_t alloc_len, data_len;
7076 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7078 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7079 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7080 format = ccb10->format;
7081 alloc_len = scsi_2btoul(ccb10->alloc_length);
7082 data_len = sizeof(*data10);
7084 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7085 format = ccb12->format;
7086 alloc_len = scsi_4btoul(ccb12->alloc_length);
7087 data_len = sizeof(*data12);
7089 if (alloc_len == 0) {
7090 ctl_set_success(ctsio);
7091 ctl_done((union ctl_io *)ctsio);
7092 return (CTL_RETVAL_COMPLETE);
7095 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7096 if (data_len < alloc_len) {
7097 ctsio->residual = alloc_len - data_len;
7098 ctsio->kern_data_len = data_len;
7099 ctsio->kern_total_len = data_len;
7101 ctsio->residual = 0;
7102 ctsio->kern_data_len = alloc_len;
7103 ctsio->kern_total_len = alloc_len;
7105 ctsio->kern_data_resid = 0;
7106 ctsio->kern_rel_offset = 0;
7107 ctsio->kern_sg_entries = 0;
7109 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7110 data10 = (struct scsi_read_defect_data_hdr_10 *)
7111 ctsio->kern_data_ptr;
7112 data10->format = format;
7113 scsi_ulto2b(0, data10->length);
7115 data12 = (struct scsi_read_defect_data_hdr_12 *)
7116 ctsio->kern_data_ptr;
7117 data12->format = format;
7118 scsi_ulto2b(0, data12->generation);
7119 scsi_ulto4b(0, data12->length);
7122 ctl_set_success(ctsio);
7123 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7124 ctsio->be_move_done = ctl_config_move_done;
7125 ctl_datamove((union ctl_io *)ctsio);
7126 return (CTL_RETVAL_COMPLETE);
7130 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7132 struct scsi_maintenance_in *cdb;
7134 int alloc_len, ext, total_len = 0, g, pc, pg, gs, os;
7135 int num_target_port_groups, num_target_ports;
7136 struct ctl_lun *lun;
7137 struct ctl_softc *softc;
7138 struct ctl_port *port;
7139 struct scsi_target_group_data *rtg_ptr;
7140 struct scsi_target_group_data_extended *rtg_ext_ptr;
7141 struct scsi_target_port_group_descriptor *tpg_desc;
7143 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7145 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7146 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7147 softc = lun->ctl_softc;
7149 retval = CTL_RETVAL_COMPLETE;
7151 switch (cdb->byte2 & STG_PDF_MASK) {
7152 case STG_PDF_LENGTH:
7155 case STG_PDF_EXTENDED:
7159 ctl_set_invalid_field(/*ctsio*/ ctsio,
7165 ctl_done((union ctl_io *)ctsio);
7169 if (softc->is_single)
7170 num_target_port_groups = 1;
7172 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
7173 num_target_ports = 0;
7174 mtx_lock(&softc->ctl_lock);
7175 STAILQ_FOREACH(port, &softc->port_list, links) {
7176 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7178 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7182 mtx_unlock(&softc->ctl_lock);
7185 total_len = sizeof(struct scsi_target_group_data_extended);
7187 total_len = sizeof(struct scsi_target_group_data);
7188 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7189 num_target_port_groups +
7190 sizeof(struct scsi_target_port_descriptor) * num_target_ports;
7192 alloc_len = scsi_4btoul(cdb->length);
7194 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7196 ctsio->kern_sg_entries = 0;
7198 if (total_len < alloc_len) {
7199 ctsio->residual = alloc_len - total_len;
7200 ctsio->kern_data_len = total_len;
7201 ctsio->kern_total_len = total_len;
7203 ctsio->residual = 0;
7204 ctsio->kern_data_len = alloc_len;
7205 ctsio->kern_total_len = alloc_len;
7207 ctsio->kern_data_resid = 0;
7208 ctsio->kern_rel_offset = 0;
7211 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7212 ctsio->kern_data_ptr;
7213 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7214 rtg_ext_ptr->format_type = 0x10;
7215 rtg_ext_ptr->implicit_transition_time = 0;
7216 tpg_desc = &rtg_ext_ptr->groups[0];
7218 rtg_ptr = (struct scsi_target_group_data *)
7219 ctsio->kern_data_ptr;
7220 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7221 tpg_desc = &rtg_ptr->groups[0];
7224 mtx_lock(&softc->ctl_lock);
7225 pg = softc->port_min / softc->port_cnt;
7226 if (softc->ha_link == CTL_HA_LINK_OFFLINE)
7227 gs = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7228 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN)
7229 gs = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7230 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY)
7231 gs = TPG_ASYMMETRIC_ACCESS_STANDBY;
7233 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7234 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7236 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7238 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7239 for (g = 0; g < num_target_port_groups; g++) {
7240 tpg_desc->pref_state = (g == pg) ? gs : os;
7241 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7242 TPG_U_SUP | TPG_T_SUP;
7243 scsi_ulto2b(g + 1, tpg_desc->target_port_group);
7244 tpg_desc->status = TPG_IMPLICIT;
7246 STAILQ_FOREACH(port, &softc->port_list, links) {
7247 if (port->targ_port < g * softc->port_cnt ||
7248 port->targ_port >= (g + 1) * softc->port_cnt)
7250 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7252 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7254 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7255 relative_target_port_identifier);
7258 tpg_desc->target_port_count = pc;
7259 tpg_desc = (struct scsi_target_port_group_descriptor *)
7260 &tpg_desc->descriptors[pc];
7262 mtx_unlock(&softc->ctl_lock);
7264 ctl_set_success(ctsio);
7265 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7266 ctsio->be_move_done = ctl_config_move_done;
7267 ctl_datamove((union ctl_io *)ctsio);
7272 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7274 struct ctl_lun *lun;
7275 struct scsi_report_supported_opcodes *cdb;
7276 const struct ctl_cmd_entry *entry, *sentry;
7277 struct scsi_report_supported_opcodes_all *all;
7278 struct scsi_report_supported_opcodes_descr *descr;
7279 struct scsi_report_supported_opcodes_one *one;
7281 int alloc_len, total_len;
7282 int opcode, service_action, i, j, num;
7284 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7286 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7287 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7289 retval = CTL_RETVAL_COMPLETE;
7291 opcode = cdb->requested_opcode;
7292 service_action = scsi_2btoul(cdb->requested_service_action);
7293 switch (cdb->options & RSO_OPTIONS_MASK) {
7294 case RSO_OPTIONS_ALL:
7296 for (i = 0; i < 256; i++) {
7297 entry = &ctl_cmd_table[i];
7298 if (entry->flags & CTL_CMD_FLAG_SA5) {
7299 for (j = 0; j < 32; j++) {
7300 sentry = &((const struct ctl_cmd_entry *)
7302 if (ctl_cmd_applicable(
7303 lun->be_lun->lun_type, sentry))
7307 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7312 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7313 num * sizeof(struct scsi_report_supported_opcodes_descr);
7315 case RSO_OPTIONS_OC:
7316 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7317 ctl_set_invalid_field(/*ctsio*/ ctsio,
7323 ctl_done((union ctl_io *)ctsio);
7324 return (CTL_RETVAL_COMPLETE);
7326 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7328 case RSO_OPTIONS_OC_SA:
7329 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7330 service_action >= 32) {
7331 ctl_set_invalid_field(/*ctsio*/ ctsio,
7337 ctl_done((union ctl_io *)ctsio);
7338 return (CTL_RETVAL_COMPLETE);
7340 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7343 ctl_set_invalid_field(/*ctsio*/ ctsio,
7349 ctl_done((union ctl_io *)ctsio);
7350 return (CTL_RETVAL_COMPLETE);
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 switch (cdb->options & RSO_OPTIONS_MASK) {
7372 case RSO_OPTIONS_ALL:
7373 all = (struct scsi_report_supported_opcodes_all *)
7374 ctsio->kern_data_ptr;
7376 for (i = 0; i < 256; i++) {
7377 entry = &ctl_cmd_table[i];
7378 if (entry->flags & CTL_CMD_FLAG_SA5) {
7379 for (j = 0; j < 32; j++) {
7380 sentry = &((const struct ctl_cmd_entry *)
7382 if (!ctl_cmd_applicable(
7383 lun->be_lun->lun_type, sentry))
7385 descr = &all->descr[num++];
7387 scsi_ulto2b(j, descr->service_action);
7388 descr->flags = RSO_SERVACTV;
7389 scsi_ulto2b(sentry->length,
7393 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7396 descr = &all->descr[num++];
7398 scsi_ulto2b(0, descr->service_action);
7400 scsi_ulto2b(entry->length, descr->cdb_length);
7404 num * sizeof(struct scsi_report_supported_opcodes_descr),
7407 case RSO_OPTIONS_OC:
7408 one = (struct scsi_report_supported_opcodes_one *)
7409 ctsio->kern_data_ptr;
7410 entry = &ctl_cmd_table[opcode];
7412 case RSO_OPTIONS_OC_SA:
7413 one = (struct scsi_report_supported_opcodes_one *)
7414 ctsio->kern_data_ptr;
7415 entry = &ctl_cmd_table[opcode];
7416 entry = &((const struct ctl_cmd_entry *)
7417 entry->execute)[service_action];
7419 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7421 scsi_ulto2b(entry->length, one->cdb_length);
7422 one->cdb_usage[0] = opcode;
7423 memcpy(&one->cdb_usage[1], entry->usage,
7430 ctl_set_success(ctsio);
7431 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7432 ctsio->be_move_done = ctl_config_move_done;
7433 ctl_datamove((union ctl_io *)ctsio);
7438 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7440 struct scsi_report_supported_tmf *cdb;
7441 struct scsi_report_supported_tmf_data *data;
7443 int alloc_len, total_len;
7445 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7447 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7449 retval = CTL_RETVAL_COMPLETE;
7451 total_len = sizeof(struct scsi_report_supported_tmf_data);
7452 alloc_len = scsi_4btoul(cdb->length);
7454 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7456 ctsio->kern_sg_entries = 0;
7458 if (total_len < alloc_len) {
7459 ctsio->residual = alloc_len - total_len;
7460 ctsio->kern_data_len = total_len;
7461 ctsio->kern_total_len = total_len;
7463 ctsio->residual = 0;
7464 ctsio->kern_data_len = alloc_len;
7465 ctsio->kern_total_len = alloc_len;
7467 ctsio->kern_data_resid = 0;
7468 ctsio->kern_rel_offset = 0;
7470 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr;
7471 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS |
7473 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS;
7475 ctl_set_success(ctsio);
7476 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7477 ctsio->be_move_done = ctl_config_move_done;
7478 ctl_datamove((union ctl_io *)ctsio);
7483 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7485 struct scsi_report_timestamp *cdb;
7486 struct scsi_report_timestamp_data *data;
7490 int alloc_len, total_len;
7492 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7494 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7496 retval = CTL_RETVAL_COMPLETE;
7498 total_len = sizeof(struct scsi_report_timestamp_data);
7499 alloc_len = scsi_4btoul(cdb->length);
7501 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7503 ctsio->kern_sg_entries = 0;
7505 if (total_len < alloc_len) {
7506 ctsio->residual = alloc_len - total_len;
7507 ctsio->kern_data_len = total_len;
7508 ctsio->kern_total_len = total_len;
7510 ctsio->residual = 0;
7511 ctsio->kern_data_len = alloc_len;
7512 ctsio->kern_total_len = alloc_len;
7514 ctsio->kern_data_resid = 0;
7515 ctsio->kern_rel_offset = 0;
7517 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7518 scsi_ulto2b(sizeof(*data) - 2, data->length);
7519 data->origin = RTS_ORIG_OUTSIDE;
7521 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7522 scsi_ulto4b(timestamp >> 16, data->timestamp);
7523 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7525 ctl_set_success(ctsio);
7526 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7527 ctsio->be_move_done = ctl_config_move_done;
7528 ctl_datamove((union ctl_io *)ctsio);
7533 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7535 struct scsi_per_res_in *cdb;
7536 int alloc_len, total_len = 0;
7537 /* struct scsi_per_res_in_rsrv in_data; */
7538 struct ctl_lun *lun;
7539 struct ctl_softc *softc;
7542 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7544 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7546 alloc_len = scsi_2btoul(cdb->length);
7548 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7549 softc = lun->ctl_softc;
7552 mtx_lock(&lun->lun_lock);
7553 switch (cdb->action) {
7554 case SPRI_RK: /* read keys */
7555 total_len = sizeof(struct scsi_per_res_in_keys) +
7557 sizeof(struct scsi_per_res_key);
7559 case SPRI_RR: /* read reservation */
7560 if (lun->flags & CTL_LUN_PR_RESERVED)
7561 total_len = sizeof(struct scsi_per_res_in_rsrv);
7563 total_len = sizeof(struct scsi_per_res_in_header);
7565 case SPRI_RC: /* report capabilities */
7566 total_len = sizeof(struct scsi_per_res_cap);
7568 case SPRI_RS: /* read full status */
7569 total_len = sizeof(struct scsi_per_res_in_header) +
7570 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7574 panic("Invalid PR type %x", cdb->action);
7576 mtx_unlock(&lun->lun_lock);
7578 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7580 if (total_len < alloc_len) {
7581 ctsio->residual = alloc_len - total_len;
7582 ctsio->kern_data_len = total_len;
7583 ctsio->kern_total_len = total_len;
7585 ctsio->residual = 0;
7586 ctsio->kern_data_len = alloc_len;
7587 ctsio->kern_total_len = alloc_len;
7590 ctsio->kern_data_resid = 0;
7591 ctsio->kern_rel_offset = 0;
7592 ctsio->kern_sg_entries = 0;
7594 mtx_lock(&lun->lun_lock);
7595 switch (cdb->action) {
7596 case SPRI_RK: { // read keys
7597 struct scsi_per_res_in_keys *res_keys;
7600 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7603 * We had to drop the lock to allocate our buffer, which
7604 * leaves time for someone to come in with another
7605 * persistent reservation. (That is unlikely, though,
7606 * since this should be the only persistent reservation
7607 * command active right now.)
7609 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7610 (lun->pr_key_count *
7611 sizeof(struct scsi_per_res_key)))){
7612 mtx_unlock(&lun->lun_lock);
7613 free(ctsio->kern_data_ptr, M_CTL);
7614 printf("%s: reservation length changed, retrying\n",
7619 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7621 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7622 lun->pr_key_count, res_keys->header.length);
7624 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) {
7625 if ((key = ctl_get_prkey(lun, i)) == 0)
7629 * We used lun->pr_key_count to calculate the
7630 * size to allocate. If it turns out the number of
7631 * initiators with the registered flag set is
7632 * larger than that (i.e. they haven't been kept in
7633 * sync), we've got a problem.
7635 if (key_count >= lun->pr_key_count) {
7637 csevent_log(CSC_CTL | CSC_SHELF_SW |
7639 csevent_LogType_Fault,
7640 csevent_AlertLevel_Yellow,
7641 csevent_FRU_ShelfController,
7642 csevent_FRU_Firmware,
7643 csevent_FRU_Unknown,
7644 "registered keys %d >= key "
7645 "count %d", key_count,
7651 scsi_u64to8b(key, res_keys->keys[key_count].key);
7656 case SPRI_RR: { // read reservation
7657 struct scsi_per_res_in_rsrv *res;
7658 int tmp_len, header_only;
7660 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7662 scsi_ulto4b(lun->PRGeneration, res->header.generation);
7664 if (lun->flags & CTL_LUN_PR_RESERVED)
7666 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7667 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7668 res->header.length);
7671 tmp_len = sizeof(struct scsi_per_res_in_header);
7672 scsi_ulto4b(0, res->header.length);
7677 * We had to drop the lock to allocate our buffer, which
7678 * leaves time for someone to come in with another
7679 * persistent reservation. (That is unlikely, though,
7680 * since this should be the only persistent reservation
7681 * command active right now.)
7683 if (tmp_len != total_len) {
7684 mtx_unlock(&lun->lun_lock);
7685 free(ctsio->kern_data_ptr, M_CTL);
7686 printf("%s: reservation status changed, retrying\n",
7692 * No reservation held, so we're done.
7694 if (header_only != 0)
7698 * If the registration is an All Registrants type, the key
7699 * is 0, since it doesn't really matter.
7701 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7702 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7703 res->data.reservation);
7705 res->data.scopetype = lun->res_type;
7708 case SPRI_RC: //report capabilities
7710 struct scsi_per_res_cap *res_cap;
7713 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7714 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7715 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5;
7716 type_mask = SPRI_TM_WR_EX_AR |
7722 scsi_ulto2b(type_mask, res_cap->type_mask);
7725 case SPRI_RS: { // read full status
7726 struct scsi_per_res_in_full *res_status;
7727 struct scsi_per_res_in_full_desc *res_desc;
7728 struct ctl_port *port;
7731 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7734 * We had to drop the lock to allocate our buffer, which
7735 * leaves time for someone to come in with another
7736 * persistent reservation. (That is unlikely, though,
7737 * since this should be the only persistent reservation
7738 * command active right now.)
7740 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7741 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7742 lun->pr_key_count)){
7743 mtx_unlock(&lun->lun_lock);
7744 free(ctsio->kern_data_ptr, M_CTL);
7745 printf("%s: reservation length changed, retrying\n",
7750 scsi_ulto4b(lun->PRGeneration, res_status->header.generation);
7752 res_desc = &res_status->desc[0];
7753 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7754 if ((key = ctl_get_prkey(lun, i)) == 0)
7757 scsi_u64to8b(key, res_desc->res_key.key);
7758 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7759 (lun->pr_res_idx == i ||
7760 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7761 res_desc->flags = SPRI_FULL_R_HOLDER;
7762 res_desc->scopetype = lun->res_type;
7764 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7765 res_desc->rel_trgt_port_id);
7767 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7769 len = ctl_create_iid(port,
7770 i % CTL_MAX_INIT_PER_PORT,
7771 res_desc->transport_id);
7772 scsi_ulto4b(len, res_desc->additional_length);
7773 res_desc = (struct scsi_per_res_in_full_desc *)
7774 &res_desc->transport_id[len];
7776 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7777 res_status->header.length);
7782 * This is a bug, because we just checked for this above,
7783 * and should have returned an error.
7785 panic("Invalid PR type %x", cdb->action);
7786 break; /* NOTREACHED */
7788 mtx_unlock(&lun->lun_lock);
7790 ctl_set_success(ctsio);
7791 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7792 ctsio->be_move_done = ctl_config_move_done;
7793 ctl_datamove((union ctl_io *)ctsio);
7794 return (CTL_RETVAL_COMPLETE);
7798 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7802 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7803 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7804 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7805 struct scsi_per_res_out_parms* param)
7807 union ctl_ha_msg persis_io;
7810 mtx_lock(&lun->lun_lock);
7811 if (sa_res_key == 0) {
7812 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7813 /* validate scope and type */
7814 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7816 mtx_unlock(&lun->lun_lock);
7817 ctl_set_invalid_field(/*ctsio*/ ctsio,
7823 ctl_done((union ctl_io *)ctsio);
7827 if (type>8 || type==2 || type==4 || type==0) {
7828 mtx_unlock(&lun->lun_lock);
7829 ctl_set_invalid_field(/*ctsio*/ ctsio,
7835 ctl_done((union ctl_io *)ctsio);
7840 * Unregister everybody else and build UA for
7843 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7844 if (i == residx || ctl_get_prkey(lun, i) == 0)
7847 ctl_clr_prkey(lun, i);
7848 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7850 lun->pr_key_count = 1;
7851 lun->res_type = type;
7852 if (lun->res_type != SPR_TYPE_WR_EX_AR
7853 && lun->res_type != SPR_TYPE_EX_AC_AR)
7854 lun->pr_res_idx = residx;
7855 lun->PRGeneration++;
7856 mtx_unlock(&lun->lun_lock);
7858 /* send msg to other side */
7859 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7860 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7861 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7862 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7863 persis_io.pr.pr_info.res_type = type;
7864 memcpy(persis_io.pr.pr_info.sa_res_key,
7865 param->serv_act_res_key,
7866 sizeof(param->serv_act_res_key));
7867 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7868 sizeof(persis_io.pr), M_WAITOK);
7870 /* not all registrants */
7871 mtx_unlock(&lun->lun_lock);
7872 free(ctsio->kern_data_ptr, M_CTL);
7873 ctl_set_invalid_field(ctsio,
7879 ctl_done((union ctl_io *)ctsio);
7882 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7883 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7886 if (res_key == sa_res_key) {
7889 * The spec implies this is not good but doesn't
7890 * say what to do. There are two choices either
7891 * generate a res conflict or check condition
7892 * with illegal field in parameter data. Since
7893 * that is what is done when the sa_res_key is
7894 * zero I'll take that approach since this has
7895 * to do with the sa_res_key.
7897 mtx_unlock(&lun->lun_lock);
7898 free(ctsio->kern_data_ptr, M_CTL);
7899 ctl_set_invalid_field(ctsio,
7905 ctl_done((union ctl_io *)ctsio);
7909 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7910 if (ctl_get_prkey(lun, i) != sa_res_key)
7914 ctl_clr_prkey(lun, i);
7915 lun->pr_key_count--;
7916 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7919 mtx_unlock(&lun->lun_lock);
7920 free(ctsio->kern_data_ptr, M_CTL);
7921 ctl_set_reservation_conflict(ctsio);
7922 ctl_done((union ctl_io *)ctsio);
7923 return (CTL_RETVAL_COMPLETE);
7925 lun->PRGeneration++;
7926 mtx_unlock(&lun->lun_lock);
7928 /* send msg to other side */
7929 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7930 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7931 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7932 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7933 persis_io.pr.pr_info.res_type = type;
7934 memcpy(persis_io.pr.pr_info.sa_res_key,
7935 param->serv_act_res_key,
7936 sizeof(param->serv_act_res_key));
7937 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7938 sizeof(persis_io.pr), M_WAITOK);
7940 /* Reserved but not all registrants */
7941 /* sa_res_key is res holder */
7942 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
7943 /* validate scope and type */
7944 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7946 mtx_unlock(&lun->lun_lock);
7947 ctl_set_invalid_field(/*ctsio*/ ctsio,
7953 ctl_done((union ctl_io *)ctsio);
7957 if (type>8 || type==2 || type==4 || type==0) {
7958 mtx_unlock(&lun->lun_lock);
7959 ctl_set_invalid_field(/*ctsio*/ ctsio,
7965 ctl_done((union ctl_io *)ctsio);
7971 * if sa_res_key != res_key remove all
7972 * registrants w/sa_res_key and generate UA
7973 * for these registrants(Registrations
7974 * Preempted) if it wasn't an exclusive
7975 * reservation generate UA(Reservations
7976 * Preempted) for all other registered nexuses
7977 * if the type has changed. Establish the new
7978 * reservation and holder. If res_key and
7979 * sa_res_key are the same do the above
7980 * except don't unregister the res holder.
7983 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7984 if (i == residx || ctl_get_prkey(lun, i) == 0)
7987 if (sa_res_key == ctl_get_prkey(lun, i)) {
7988 ctl_clr_prkey(lun, i);
7989 lun->pr_key_count--;
7990 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7991 } else if (type != lun->res_type
7992 && (lun->res_type == SPR_TYPE_WR_EX_RO
7993 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
7994 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
7997 lun->res_type = type;
7998 if (lun->res_type != SPR_TYPE_WR_EX_AR
7999 && lun->res_type != SPR_TYPE_EX_AC_AR)
8000 lun->pr_res_idx = residx;
8002 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8003 lun->PRGeneration++;
8004 mtx_unlock(&lun->lun_lock);
8006 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8007 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8008 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8009 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8010 persis_io.pr.pr_info.res_type = type;
8011 memcpy(persis_io.pr.pr_info.sa_res_key,
8012 param->serv_act_res_key,
8013 sizeof(param->serv_act_res_key));
8014 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8015 sizeof(persis_io.pr), M_WAITOK);
8018 * sa_res_key is not the res holder just
8019 * remove registrants
8023 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8024 if (sa_res_key != ctl_get_prkey(lun, i))
8028 ctl_clr_prkey(lun, i);
8029 lun->pr_key_count--;
8030 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8034 mtx_unlock(&lun->lun_lock);
8035 free(ctsio->kern_data_ptr, M_CTL);
8036 ctl_set_reservation_conflict(ctsio);
8037 ctl_done((union ctl_io *)ctsio);
8040 lun->PRGeneration++;
8041 mtx_unlock(&lun->lun_lock);
8043 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8044 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8045 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8046 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8047 persis_io.pr.pr_info.res_type = type;
8048 memcpy(persis_io.pr.pr_info.sa_res_key,
8049 param->serv_act_res_key,
8050 sizeof(param->serv_act_res_key));
8051 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8052 sizeof(persis_io.pr), M_WAITOK);
8059 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8061 uint64_t sa_res_key;
8064 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8066 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8067 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8068 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
8069 if (sa_res_key == 0) {
8071 * Unregister everybody else and build UA for
8074 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8075 if (i == msg->pr.pr_info.residx ||
8076 ctl_get_prkey(lun, i) == 0)
8079 ctl_clr_prkey(lun, i);
8080 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8083 lun->pr_key_count = 1;
8084 lun->res_type = msg->pr.pr_info.res_type;
8085 if (lun->res_type != SPR_TYPE_WR_EX_AR
8086 && lun->res_type != SPR_TYPE_EX_AC_AR)
8087 lun->pr_res_idx = msg->pr.pr_info.residx;
8089 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8090 if (sa_res_key == ctl_get_prkey(lun, i))
8093 ctl_clr_prkey(lun, i);
8094 lun->pr_key_count--;
8095 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8099 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8100 if (i == msg->pr.pr_info.residx ||
8101 ctl_get_prkey(lun, i) == 0)
8104 if (sa_res_key == ctl_get_prkey(lun, i)) {
8105 ctl_clr_prkey(lun, i);
8106 lun->pr_key_count--;
8107 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8108 } else if (msg->pr.pr_info.res_type != lun->res_type
8109 && (lun->res_type == SPR_TYPE_WR_EX_RO
8110 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
8111 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8114 lun->res_type = msg->pr.pr_info.res_type;
8115 if (lun->res_type != SPR_TYPE_WR_EX_AR
8116 && lun->res_type != SPR_TYPE_EX_AC_AR)
8117 lun->pr_res_idx = msg->pr.pr_info.residx;
8119 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8121 lun->PRGeneration++;
8127 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8130 u_int32_t param_len;
8131 struct scsi_per_res_out *cdb;
8132 struct ctl_lun *lun;
8133 struct scsi_per_res_out_parms* param;
8134 struct ctl_softc *softc;
8136 uint64_t res_key, sa_res_key, key;
8138 union ctl_ha_msg persis_io;
8141 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8143 retval = CTL_RETVAL_COMPLETE;
8145 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8146 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8147 softc = lun->ctl_softc;
8150 * We only support whole-LUN scope. The scope & type are ignored for
8151 * register, register and ignore existing key and clear.
8152 * We sometimes ignore scope and type on preempts too!!
8153 * Verify reservation type here as well.
8155 type = cdb->scope_type & SPR_TYPE_MASK;
8156 if ((cdb->action == SPRO_RESERVE)
8157 || (cdb->action == SPRO_RELEASE)) {
8158 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8159 ctl_set_invalid_field(/*ctsio*/ ctsio,
8165 ctl_done((union ctl_io *)ctsio);
8166 return (CTL_RETVAL_COMPLETE);
8169 if (type>8 || type==2 || type==4 || type==0) {
8170 ctl_set_invalid_field(/*ctsio*/ ctsio,
8176 ctl_done((union ctl_io *)ctsio);
8177 return (CTL_RETVAL_COMPLETE);
8181 param_len = scsi_4btoul(cdb->length);
8183 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8184 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8185 ctsio->kern_data_len = param_len;
8186 ctsio->kern_total_len = param_len;
8187 ctsio->kern_data_resid = 0;
8188 ctsio->kern_rel_offset = 0;
8189 ctsio->kern_sg_entries = 0;
8190 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8191 ctsio->be_move_done = ctl_config_move_done;
8192 ctl_datamove((union ctl_io *)ctsio);
8194 return (CTL_RETVAL_COMPLETE);
8197 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8199 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8200 res_key = scsi_8btou64(param->res_key.key);
8201 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8204 * Validate the reservation key here except for SPRO_REG_IGNO
8205 * This must be done for all other service actions
8207 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8208 mtx_lock(&lun->lun_lock);
8209 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8210 if (res_key != key) {
8212 * The current key passed in doesn't match
8213 * the one the initiator previously
8216 mtx_unlock(&lun->lun_lock);
8217 free(ctsio->kern_data_ptr, M_CTL);
8218 ctl_set_reservation_conflict(ctsio);
8219 ctl_done((union ctl_io *)ctsio);
8220 return (CTL_RETVAL_COMPLETE);
8222 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8224 * We are not registered
8226 mtx_unlock(&lun->lun_lock);
8227 free(ctsio->kern_data_ptr, M_CTL);
8228 ctl_set_reservation_conflict(ctsio);
8229 ctl_done((union ctl_io *)ctsio);
8230 return (CTL_RETVAL_COMPLETE);
8231 } else if (res_key != 0) {
8233 * We are not registered and trying to register but
8234 * the register key isn't zero.
8236 mtx_unlock(&lun->lun_lock);
8237 free(ctsio->kern_data_ptr, M_CTL);
8238 ctl_set_reservation_conflict(ctsio);
8239 ctl_done((union ctl_io *)ctsio);
8240 return (CTL_RETVAL_COMPLETE);
8242 mtx_unlock(&lun->lun_lock);
8245 switch (cdb->action & SPRO_ACTION_MASK) {
8247 case SPRO_REG_IGNO: {
8250 printf("Registration received\n");
8254 * We don't support any of these options, as we report in
8255 * the read capabilities request (see
8256 * ctl_persistent_reserve_in(), above).
8258 if ((param->flags & SPR_SPEC_I_PT)
8259 || (param->flags & SPR_ALL_TG_PT)
8260 || (param->flags & SPR_APTPL)) {
8263 if (param->flags & SPR_APTPL)
8265 else if (param->flags & SPR_ALL_TG_PT)
8267 else /* SPR_SPEC_I_PT */
8270 free(ctsio->kern_data_ptr, M_CTL);
8271 ctl_set_invalid_field(ctsio,
8277 ctl_done((union ctl_io *)ctsio);
8278 return (CTL_RETVAL_COMPLETE);
8281 mtx_lock(&lun->lun_lock);
8284 * The initiator wants to clear the
8287 if (sa_res_key == 0) {
8289 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8290 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8291 && ctl_get_prkey(lun, residx) == 0)) {
8292 mtx_unlock(&lun->lun_lock);
8296 ctl_clr_prkey(lun, residx);
8297 lun->pr_key_count--;
8299 if (residx == lun->pr_res_idx) {
8300 lun->flags &= ~CTL_LUN_PR_RESERVED;
8301 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8303 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8304 || lun->res_type == SPR_TYPE_EX_AC_RO)
8305 && lun->pr_key_count) {
8307 * If the reservation is a registrants
8308 * only type we need to generate a UA
8309 * for other registered inits. The
8310 * sense code should be RESERVATIONS
8314 for (i = softc->init_min; i < softc->init_max; i++){
8315 if (ctl_get_prkey(lun, i) == 0)
8318 CTL_UA_RES_RELEASE);
8322 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8323 if (lun->pr_key_count==0) {
8324 lun->flags &= ~CTL_LUN_PR_RESERVED;
8326 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8329 lun->PRGeneration++;
8330 mtx_unlock(&lun->lun_lock);
8332 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8333 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8334 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8335 persis_io.pr.pr_info.residx = residx;
8336 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8337 sizeof(persis_io.pr), M_WAITOK);
8338 } else /* sa_res_key != 0 */ {
8341 * If we aren't registered currently then increment
8342 * the key count and set the registered flag.
8344 ctl_alloc_prkey(lun, residx);
8345 if (ctl_get_prkey(lun, residx) == 0)
8346 lun->pr_key_count++;
8347 ctl_set_prkey(lun, residx, sa_res_key);
8348 lun->PRGeneration++;
8349 mtx_unlock(&lun->lun_lock);
8351 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8352 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8353 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8354 persis_io.pr.pr_info.residx = residx;
8355 memcpy(persis_io.pr.pr_info.sa_res_key,
8356 param->serv_act_res_key,
8357 sizeof(param->serv_act_res_key));
8358 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8359 sizeof(persis_io.pr), M_WAITOK);
8366 printf("Reserve executed type %d\n", type);
8368 mtx_lock(&lun->lun_lock);
8369 if (lun->flags & CTL_LUN_PR_RESERVED) {
8371 * if this isn't the reservation holder and it's
8372 * not a "all registrants" type or if the type is
8373 * different then we have a conflict
8375 if ((lun->pr_res_idx != residx
8376 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8377 || lun->res_type != type) {
8378 mtx_unlock(&lun->lun_lock);
8379 free(ctsio->kern_data_ptr, M_CTL);
8380 ctl_set_reservation_conflict(ctsio);
8381 ctl_done((union ctl_io *)ctsio);
8382 return (CTL_RETVAL_COMPLETE);
8384 mtx_unlock(&lun->lun_lock);
8385 } else /* create a reservation */ {
8387 * If it's not an "all registrants" type record
8388 * reservation holder
8390 if (type != SPR_TYPE_WR_EX_AR
8391 && type != SPR_TYPE_EX_AC_AR)
8392 lun->pr_res_idx = residx; /* Res holder */
8394 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8396 lun->flags |= CTL_LUN_PR_RESERVED;
8397 lun->res_type = type;
8399 mtx_unlock(&lun->lun_lock);
8401 /* send msg to other side */
8402 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8403 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8404 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8405 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8406 persis_io.pr.pr_info.res_type = type;
8407 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8408 sizeof(persis_io.pr), M_WAITOK);
8413 mtx_lock(&lun->lun_lock);
8414 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8415 /* No reservation exists return good status */
8416 mtx_unlock(&lun->lun_lock);
8420 * Is this nexus a reservation holder?
8422 if (lun->pr_res_idx != residx
8423 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8425 * not a res holder return good status but
8428 mtx_unlock(&lun->lun_lock);
8432 if (lun->res_type != type) {
8433 mtx_unlock(&lun->lun_lock);
8434 free(ctsio->kern_data_ptr, M_CTL);
8435 ctl_set_illegal_pr_release(ctsio);
8436 ctl_done((union ctl_io *)ctsio);
8437 return (CTL_RETVAL_COMPLETE);
8440 /* okay to release */
8441 lun->flags &= ~CTL_LUN_PR_RESERVED;
8442 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8446 * if this isn't an exclusive access
8447 * res generate UA for all other
8450 if (type != SPR_TYPE_EX_AC
8451 && type != SPR_TYPE_WR_EX) {
8452 for (i = softc->init_min; i < softc->init_max; i++) {
8453 if (i == residx || ctl_get_prkey(lun, i) == 0)
8455 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8458 mtx_unlock(&lun->lun_lock);
8460 /* Send msg to other side */
8461 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8462 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8463 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8464 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8465 sizeof(persis_io.pr), M_WAITOK);
8469 /* send msg to other side */
8471 mtx_lock(&lun->lun_lock);
8472 lun->flags &= ~CTL_LUN_PR_RESERVED;
8474 lun->pr_key_count = 0;
8475 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8477 ctl_clr_prkey(lun, residx);
8478 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8479 if (ctl_get_prkey(lun, i) != 0) {
8480 ctl_clr_prkey(lun, i);
8481 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8483 lun->PRGeneration++;
8484 mtx_unlock(&lun->lun_lock);
8486 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8487 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8488 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8489 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8490 sizeof(persis_io.pr), M_WAITOK);
8494 case SPRO_PRE_ABO: {
8497 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8498 residx, ctsio, cdb, param);
8500 return (CTL_RETVAL_COMPLETE);
8504 panic("Invalid PR type %x", cdb->action);
8508 free(ctsio->kern_data_ptr, M_CTL);
8509 ctl_set_success(ctsio);
8510 ctl_done((union ctl_io *)ctsio);
8516 * This routine is for handling a message from the other SC pertaining to
8517 * persistent reserve out. All the error checking will have been done
8518 * so only perorming the action need be done here to keep the two
8522 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8524 struct ctl_lun *lun;
8525 struct ctl_softc *softc;
8527 uint32_t residx, targ_lun;
8529 softc = control_softc;
8530 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8531 mtx_lock(&softc->ctl_lock);
8532 if ((targ_lun >= CTL_MAX_LUNS) ||
8533 ((lun = softc->ctl_luns[targ_lun]) == NULL)) {
8534 mtx_unlock(&softc->ctl_lock);
8537 mtx_lock(&lun->lun_lock);
8538 mtx_unlock(&softc->ctl_lock);
8539 if (lun->flags & CTL_LUN_DISABLED) {
8540 mtx_unlock(&lun->lun_lock);
8543 residx = ctl_get_initindex(&msg->hdr.nexus);
8544 switch(msg->pr.pr_info.action) {
8545 case CTL_PR_REG_KEY:
8546 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8547 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8548 lun->pr_key_count++;
8549 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8550 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8551 lun->PRGeneration++;
8554 case CTL_PR_UNREG_KEY:
8555 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8556 lun->pr_key_count--;
8558 /* XXX Need to see if the reservation has been released */
8559 /* if so do we need to generate UA? */
8560 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8561 lun->flags &= ~CTL_LUN_PR_RESERVED;
8562 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8564 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8565 || lun->res_type == SPR_TYPE_EX_AC_RO)
8566 && lun->pr_key_count) {
8568 * If the reservation is a registrants
8569 * only type we need to generate a UA
8570 * for other registered inits. The
8571 * sense code should be RESERVATIONS
8575 for (i = softc->init_min; i < softc->init_max; i++) {
8576 if (ctl_get_prkey(lun, i) == 0)
8579 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8583 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8584 if (lun->pr_key_count==0) {
8585 lun->flags &= ~CTL_LUN_PR_RESERVED;
8587 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8590 lun->PRGeneration++;
8593 case CTL_PR_RESERVE:
8594 lun->flags |= CTL_LUN_PR_RESERVED;
8595 lun->res_type = msg->pr.pr_info.res_type;
8596 lun->pr_res_idx = msg->pr.pr_info.residx;
8600 case CTL_PR_RELEASE:
8602 * if this isn't an exclusive access res generate UA for all
8603 * other registrants.
8605 if (lun->res_type != SPR_TYPE_EX_AC
8606 && lun->res_type != SPR_TYPE_WR_EX) {
8607 for (i = softc->init_min; i < softc->init_max; i++)
8608 if (i == residx || ctl_get_prkey(lun, i) == 0)
8610 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8613 lun->flags &= ~CTL_LUN_PR_RESERVED;
8614 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8618 case CTL_PR_PREEMPT:
8619 ctl_pro_preempt_other(lun, msg);
8622 lun->flags &= ~CTL_LUN_PR_RESERVED;
8624 lun->pr_key_count = 0;
8625 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8627 for (i=0; i < CTL_MAX_INITIATORS; i++) {
8628 if (ctl_get_prkey(lun, i) == 0)
8630 ctl_clr_prkey(lun, i);
8631 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8633 lun->PRGeneration++;
8637 mtx_unlock(&lun->lun_lock);
8641 ctl_read_write(struct ctl_scsiio *ctsio)
8643 struct ctl_lun *lun;
8644 struct ctl_lba_len_flags *lbalen;
8646 uint32_t num_blocks;
8650 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8652 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8655 retval = CTL_RETVAL_COMPLETE;
8657 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8658 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8659 switch (ctsio->cdb[0]) {
8662 struct scsi_rw_6 *cdb;
8664 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8666 lba = scsi_3btoul(cdb->addr);
8667 /* only 5 bits are valid in the most significant address byte */
8669 num_blocks = cdb->length;
8671 * This is correct according to SBC-2.
8673 if (num_blocks == 0)
8679 struct scsi_rw_10 *cdb;
8681 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8682 if (cdb->byte2 & SRW10_FUA)
8683 flags |= CTL_LLF_FUA;
8684 if (cdb->byte2 & SRW10_DPO)
8685 flags |= CTL_LLF_DPO;
8686 lba = scsi_4btoul(cdb->addr);
8687 num_blocks = scsi_2btoul(cdb->length);
8690 case WRITE_VERIFY_10: {
8691 struct scsi_write_verify_10 *cdb;
8693 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8694 flags |= CTL_LLF_FUA;
8695 if (cdb->byte2 & SWV_DPO)
8696 flags |= CTL_LLF_DPO;
8697 lba = scsi_4btoul(cdb->addr);
8698 num_blocks = scsi_2btoul(cdb->length);
8703 struct scsi_rw_12 *cdb;
8705 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8706 if (cdb->byte2 & SRW12_FUA)
8707 flags |= CTL_LLF_FUA;
8708 if (cdb->byte2 & SRW12_DPO)
8709 flags |= CTL_LLF_DPO;
8710 lba = scsi_4btoul(cdb->addr);
8711 num_blocks = scsi_4btoul(cdb->length);
8714 case WRITE_VERIFY_12: {
8715 struct scsi_write_verify_12 *cdb;
8717 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8718 flags |= CTL_LLF_FUA;
8719 if (cdb->byte2 & SWV_DPO)
8720 flags |= CTL_LLF_DPO;
8721 lba = scsi_4btoul(cdb->addr);
8722 num_blocks = scsi_4btoul(cdb->length);
8727 struct scsi_rw_16 *cdb;
8729 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8730 if (cdb->byte2 & SRW12_FUA)
8731 flags |= CTL_LLF_FUA;
8732 if (cdb->byte2 & SRW12_DPO)
8733 flags |= CTL_LLF_DPO;
8734 lba = scsi_8btou64(cdb->addr);
8735 num_blocks = scsi_4btoul(cdb->length);
8738 case WRITE_ATOMIC_16: {
8739 struct scsi_rw_16 *cdb;
8741 if (lun->be_lun->atomicblock == 0) {
8742 ctl_set_invalid_opcode(ctsio);
8743 ctl_done((union ctl_io *)ctsio);
8744 return (CTL_RETVAL_COMPLETE);
8747 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8748 if (cdb->byte2 & SRW12_FUA)
8749 flags |= CTL_LLF_FUA;
8750 if (cdb->byte2 & SRW12_DPO)
8751 flags |= CTL_LLF_DPO;
8752 lba = scsi_8btou64(cdb->addr);
8753 num_blocks = scsi_4btoul(cdb->length);
8754 if (num_blocks > lun->be_lun->atomicblock) {
8755 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8756 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8758 ctl_done((union ctl_io *)ctsio);
8759 return (CTL_RETVAL_COMPLETE);
8763 case WRITE_VERIFY_16: {
8764 struct scsi_write_verify_16 *cdb;
8766 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8767 flags |= CTL_LLF_FUA;
8768 if (cdb->byte2 & SWV_DPO)
8769 flags |= CTL_LLF_DPO;
8770 lba = scsi_8btou64(cdb->addr);
8771 num_blocks = scsi_4btoul(cdb->length);
8776 * We got a command we don't support. This shouldn't
8777 * happen, commands should be filtered out above us.
8779 ctl_set_invalid_opcode(ctsio);
8780 ctl_done((union ctl_io *)ctsio);
8782 return (CTL_RETVAL_COMPLETE);
8783 break; /* NOTREACHED */
8787 * The first check is to make sure we're in bounds, the second
8788 * check is to catch wrap-around problems. If the lba + num blocks
8789 * is less than the lba, then we've wrapped around and the block
8790 * range is invalid anyway.
8792 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8793 || ((lba + num_blocks) < lba)) {
8794 ctl_set_lba_out_of_range(ctsio);
8795 ctl_done((union ctl_io *)ctsio);
8796 return (CTL_RETVAL_COMPLETE);
8800 * According to SBC-3, a transfer length of 0 is not an error.
8801 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8802 * translates to 256 blocks for those commands.
8804 if (num_blocks == 0) {
8805 ctl_set_success(ctsio);
8806 ctl_done((union ctl_io *)ctsio);
8807 return (CTL_RETVAL_COMPLETE);
8810 /* Set FUA and/or DPO if caches are disabled. */
8812 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8814 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8816 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8818 flags |= CTL_LLF_FUA;
8821 lbalen = (struct ctl_lba_len_flags *)
8822 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8824 lbalen->len = num_blocks;
8825 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8827 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8828 ctsio->kern_rel_offset = 0;
8830 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8832 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8838 ctl_cnw_cont(union ctl_io *io)
8840 struct ctl_scsiio *ctsio;
8841 struct ctl_lun *lun;
8842 struct ctl_lba_len_flags *lbalen;
8845 ctsio = &io->scsiio;
8846 ctsio->io_hdr.status = CTL_STATUS_NONE;
8847 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8848 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8849 lbalen = (struct ctl_lba_len_flags *)
8850 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8851 lbalen->flags &= ~CTL_LLF_COMPARE;
8852 lbalen->flags |= CTL_LLF_WRITE;
8854 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8855 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8860 ctl_cnw(struct ctl_scsiio *ctsio)
8862 struct ctl_lun *lun;
8863 struct ctl_lba_len_flags *lbalen;
8865 uint32_t num_blocks;
8868 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8870 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8873 retval = CTL_RETVAL_COMPLETE;
8875 switch (ctsio->cdb[0]) {
8876 case COMPARE_AND_WRITE: {
8877 struct scsi_compare_and_write *cdb;
8879 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8880 if (cdb->byte2 & SRW10_FUA)
8881 flags |= CTL_LLF_FUA;
8882 if (cdb->byte2 & SRW10_DPO)
8883 flags |= CTL_LLF_DPO;
8884 lba = scsi_8btou64(cdb->addr);
8885 num_blocks = cdb->length;
8890 * We got a command we don't support. This shouldn't
8891 * happen, commands should be filtered out above us.
8893 ctl_set_invalid_opcode(ctsio);
8894 ctl_done((union ctl_io *)ctsio);
8896 return (CTL_RETVAL_COMPLETE);
8897 break; /* NOTREACHED */
8901 * The first check is to make sure we're in bounds, the second
8902 * check is to catch wrap-around problems. If the lba + num blocks
8903 * is less than the lba, then we've wrapped around and the block
8904 * range is invalid anyway.
8906 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8907 || ((lba + num_blocks) < lba)) {
8908 ctl_set_lba_out_of_range(ctsio);
8909 ctl_done((union ctl_io *)ctsio);
8910 return (CTL_RETVAL_COMPLETE);
8914 * According to SBC-3, a transfer length of 0 is not an error.
8916 if (num_blocks == 0) {
8917 ctl_set_success(ctsio);
8918 ctl_done((union ctl_io *)ctsio);
8919 return (CTL_RETVAL_COMPLETE);
8922 /* Set FUA if write cache is disabled. */
8923 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8925 flags |= CTL_LLF_FUA;
8927 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
8928 ctsio->kern_rel_offset = 0;
8931 * Set the IO_CONT flag, so that if this I/O gets passed to
8932 * ctl_data_submit_done(), it'll get passed back to
8933 * ctl_ctl_cnw_cont() for further processing.
8935 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
8936 ctsio->io_cont = ctl_cnw_cont;
8938 lbalen = (struct ctl_lba_len_flags *)
8939 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8941 lbalen->len = num_blocks;
8942 lbalen->flags = CTL_LLF_COMPARE | flags;
8944 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
8945 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8950 ctl_verify(struct ctl_scsiio *ctsio)
8952 struct ctl_lun *lun;
8953 struct ctl_lba_len_flags *lbalen;
8955 uint32_t num_blocks;
8959 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8961 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
8964 flags = CTL_LLF_FUA;
8965 retval = CTL_RETVAL_COMPLETE;
8967 switch (ctsio->cdb[0]) {
8969 struct scsi_verify_10 *cdb;
8971 cdb = (struct scsi_verify_10 *)ctsio->cdb;
8972 if (cdb->byte2 & SVFY_BYTCHK)
8974 if (cdb->byte2 & SVFY_DPO)
8975 flags |= CTL_LLF_DPO;
8976 lba = scsi_4btoul(cdb->addr);
8977 num_blocks = scsi_2btoul(cdb->length);
8981 struct scsi_verify_12 *cdb;
8983 cdb = (struct scsi_verify_12 *)ctsio->cdb;
8984 if (cdb->byte2 & SVFY_BYTCHK)
8986 if (cdb->byte2 & SVFY_DPO)
8987 flags |= CTL_LLF_DPO;
8988 lba = scsi_4btoul(cdb->addr);
8989 num_blocks = scsi_4btoul(cdb->length);
8993 struct scsi_rw_16 *cdb;
8995 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8996 if (cdb->byte2 & SVFY_BYTCHK)
8998 if (cdb->byte2 & SVFY_DPO)
8999 flags |= CTL_LLF_DPO;
9000 lba = scsi_8btou64(cdb->addr);
9001 num_blocks = scsi_4btoul(cdb->length);
9006 * We got a command we don't support. This shouldn't
9007 * happen, commands should be filtered out above us.
9009 ctl_set_invalid_opcode(ctsio);
9010 ctl_done((union ctl_io *)ctsio);
9011 return (CTL_RETVAL_COMPLETE);
9015 * The first check is to make sure we're in bounds, the second
9016 * check is to catch wrap-around problems. If the lba + num blocks
9017 * is less than the lba, then we've wrapped around and the block
9018 * range is invalid anyway.
9020 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9021 || ((lba + num_blocks) < lba)) {
9022 ctl_set_lba_out_of_range(ctsio);
9023 ctl_done((union ctl_io *)ctsio);
9024 return (CTL_RETVAL_COMPLETE);
9028 * According to SBC-3, a transfer length of 0 is not an error.
9030 if (num_blocks == 0) {
9031 ctl_set_success(ctsio);
9032 ctl_done((union ctl_io *)ctsio);
9033 return (CTL_RETVAL_COMPLETE);
9036 lbalen = (struct ctl_lba_len_flags *)
9037 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9039 lbalen->len = num_blocks;
9041 lbalen->flags = CTL_LLF_COMPARE | flags;
9042 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9044 lbalen->flags = CTL_LLF_VERIFY | flags;
9045 ctsio->kern_total_len = 0;
9047 ctsio->kern_rel_offset = 0;
9049 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9050 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9055 ctl_report_luns(struct ctl_scsiio *ctsio)
9057 struct ctl_softc *softc = control_softc;
9058 struct scsi_report_luns *cdb;
9059 struct scsi_report_luns_data *lun_data;
9060 struct ctl_lun *lun, *request_lun;
9061 struct ctl_port *port;
9062 int num_luns, retval;
9063 uint32_t alloc_len, lun_datalen;
9064 int num_filled, well_known;
9065 uint32_t initidx, targ_lun_id, lun_id;
9067 retval = CTL_RETVAL_COMPLETE;
9070 cdb = (struct scsi_report_luns *)ctsio->cdb;
9071 port = ctl_io_port(&ctsio->io_hdr);
9073 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9075 mtx_lock(&softc->ctl_lock);
9077 for (targ_lun_id = 0; targ_lun_id < CTL_MAX_LUNS; targ_lun_id++) {
9078 if (ctl_lun_map_from_port(port, targ_lun_id) < CTL_MAX_LUNS)
9081 mtx_unlock(&softc->ctl_lock);
9083 switch (cdb->select_report) {
9084 case RPL_REPORT_DEFAULT:
9085 case RPL_REPORT_ALL:
9087 case RPL_REPORT_WELLKNOWN:
9092 ctl_set_invalid_field(ctsio,
9098 ctl_done((union ctl_io *)ctsio);
9100 break; /* NOTREACHED */
9103 alloc_len = scsi_4btoul(cdb->length);
9105 * The initiator has to allocate at least 16 bytes for this request,
9106 * so he can at least get the header and the first LUN. Otherwise
9107 * we reject the request (per SPC-3 rev 14, section 6.21).
9109 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9110 sizeof(struct scsi_report_luns_lundata))) {
9111 ctl_set_invalid_field(ctsio,
9117 ctl_done((union ctl_io *)ctsio);
9121 request_lun = (struct ctl_lun *)
9122 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9124 lun_datalen = sizeof(*lun_data) +
9125 (num_luns * sizeof(struct scsi_report_luns_lundata));
9127 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9128 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9129 ctsio->kern_sg_entries = 0;
9131 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9133 mtx_lock(&softc->ctl_lock);
9134 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9135 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9136 if (lun_id >= CTL_MAX_LUNS)
9138 lun = softc->ctl_luns[lun_id];
9142 if (targ_lun_id <= 0xff) {
9144 * Peripheral addressing method, bus number 0.
9146 lun_data->luns[num_filled].lundata[0] =
9147 RPL_LUNDATA_ATYP_PERIPH;
9148 lun_data->luns[num_filled].lundata[1] = targ_lun_id;
9150 } else if (targ_lun_id <= 0x3fff) {
9152 * Flat addressing method.
9154 lun_data->luns[num_filled].lundata[0] =
9155 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8);
9156 lun_data->luns[num_filled].lundata[1] =
9157 (targ_lun_id & 0xff);
9159 } else if (targ_lun_id <= 0xffffff) {
9161 * Extended flat addressing method.
9163 lun_data->luns[num_filled].lundata[0] =
9164 RPL_LUNDATA_ATYP_EXTLUN | 0x12;
9165 scsi_ulto3b(targ_lun_id,
9166 &lun_data->luns[num_filled].lundata[1]);
9169 printf("ctl_report_luns: bogus LUN number %jd, "
9170 "skipping\n", (intmax_t)targ_lun_id);
9173 * According to SPC-3, rev 14 section 6.21:
9175 * "The execution of a REPORT LUNS command to any valid and
9176 * installed logical unit shall clear the REPORTED LUNS DATA
9177 * HAS CHANGED unit attention condition for all logical
9178 * units of that target with respect to the requesting
9179 * initiator. A valid and installed logical unit is one
9180 * having a PERIPHERAL QUALIFIER of 000b in the standard
9181 * INQUIRY data (see 6.4.2)."
9183 * If request_lun is NULL, the LUN this report luns command
9184 * was issued to is either disabled or doesn't exist. In that
9185 * case, we shouldn't clear any pending lun change unit
9188 if (request_lun != NULL) {
9189 mtx_lock(&lun->lun_lock);
9190 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE);
9191 mtx_unlock(&lun->lun_lock);
9194 mtx_unlock(&softc->ctl_lock);
9197 * It's quite possible that we've returned fewer LUNs than we allocated
9198 * space for. Trim it.
9200 lun_datalen = sizeof(*lun_data) +
9201 (num_filled * sizeof(struct scsi_report_luns_lundata));
9203 if (lun_datalen < alloc_len) {
9204 ctsio->residual = alloc_len - lun_datalen;
9205 ctsio->kern_data_len = lun_datalen;
9206 ctsio->kern_total_len = lun_datalen;
9208 ctsio->residual = 0;
9209 ctsio->kern_data_len = alloc_len;
9210 ctsio->kern_total_len = alloc_len;
9212 ctsio->kern_data_resid = 0;
9213 ctsio->kern_rel_offset = 0;
9214 ctsio->kern_sg_entries = 0;
9217 * We set this to the actual data length, regardless of how much
9218 * space we actually have to return results. If the user looks at
9219 * this value, he'll know whether or not he allocated enough space
9220 * and reissue the command if necessary. We don't support well
9221 * known logical units, so if the user asks for that, return none.
9223 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9226 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9229 ctl_set_success(ctsio);
9230 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9231 ctsio->be_move_done = ctl_config_move_done;
9232 ctl_datamove((union ctl_io *)ctsio);
9237 ctl_request_sense(struct ctl_scsiio *ctsio)
9239 struct scsi_request_sense *cdb;
9240 struct scsi_sense_data *sense_ptr;
9241 struct ctl_softc *ctl_softc;
9242 struct ctl_lun *lun;
9245 scsi_sense_data_type sense_format;
9246 ctl_ua_type ua_type;
9248 cdb = (struct scsi_request_sense *)ctsio->cdb;
9250 ctl_softc = control_softc;
9251 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9253 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9256 * Determine which sense format the user wants.
9258 if (cdb->byte2 & SRS_DESC)
9259 sense_format = SSD_TYPE_DESC;
9261 sense_format = SSD_TYPE_FIXED;
9263 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9264 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9265 ctsio->kern_sg_entries = 0;
9268 * struct scsi_sense_data, which is currently set to 256 bytes, is
9269 * larger than the largest allowed value for the length field in the
9270 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9272 ctsio->residual = 0;
9273 ctsio->kern_data_len = cdb->length;
9274 ctsio->kern_total_len = cdb->length;
9276 ctsio->kern_data_resid = 0;
9277 ctsio->kern_rel_offset = 0;
9278 ctsio->kern_sg_entries = 0;
9281 * If we don't have a LUN, we don't have any pending sense.
9287 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9289 * Check for pending sense, and then for pending unit attentions.
9290 * Pending sense gets returned first, then pending unit attentions.
9292 mtx_lock(&lun->lun_lock);
9294 if (ctl_is_set(lun->have_ca, initidx)) {
9295 scsi_sense_data_type stored_format;
9298 * Check to see which sense format was used for the stored
9301 stored_format = scsi_sense_type(&lun->pending_sense[initidx]);
9304 * If the user requested a different sense format than the
9305 * one we stored, then we need to convert it to the other
9306 * format. If we're going from descriptor to fixed format
9307 * sense data, we may lose things in translation, depending
9308 * on what options were used.
9310 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9311 * for some reason we'll just copy it out as-is.
9313 if ((stored_format == SSD_TYPE_FIXED)
9314 && (sense_format == SSD_TYPE_DESC))
9315 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9316 &lun->pending_sense[initidx],
9317 (struct scsi_sense_data_desc *)sense_ptr);
9318 else if ((stored_format == SSD_TYPE_DESC)
9319 && (sense_format == SSD_TYPE_FIXED))
9320 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9321 &lun->pending_sense[initidx],
9322 (struct scsi_sense_data_fixed *)sense_ptr);
9324 memcpy(sense_ptr, &lun->pending_sense[initidx],
9325 MIN(sizeof(*sense_ptr),
9326 sizeof(lun->pending_sense[initidx])));
9328 ctl_clear_mask(lun->have_ca, initidx);
9333 ua_type = ctl_build_ua(lun, initidx, sense_ptr, sense_format);
9334 if (ua_type != CTL_UA_NONE)
9336 if (ua_type == CTL_UA_LUN_CHANGE) {
9337 mtx_unlock(&lun->lun_lock);
9338 mtx_lock(&ctl_softc->ctl_lock);
9339 ctl_clr_ua_allluns(ctl_softc, initidx, ua_type);
9340 mtx_unlock(&ctl_softc->ctl_lock);
9341 mtx_lock(&lun->lun_lock);
9345 mtx_unlock(&lun->lun_lock);
9348 * We already have a pending error, return it.
9350 if (have_error != 0) {
9352 * We report the SCSI status as OK, since the status of the
9353 * request sense command itself is OK.
9354 * We report 0 for the sense length, because we aren't doing
9355 * autosense in this case. We're reporting sense as
9358 ctl_set_success(ctsio);
9359 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9360 ctsio->be_move_done = ctl_config_move_done;
9361 ctl_datamove((union ctl_io *)ctsio);
9362 return (CTL_RETVAL_COMPLETE);
9368 * No sense information to report, so we report that everything is
9371 ctl_set_sense_data(sense_ptr,
9374 /*current_error*/ 1,
9375 /*sense_key*/ SSD_KEY_NO_SENSE,
9381 * We report 0 for the sense length, because we aren't doing
9382 * autosense in this case. We're reporting sense as parameter data.
9384 ctl_set_success(ctsio);
9385 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9386 ctsio->be_move_done = ctl_config_move_done;
9387 ctl_datamove((union ctl_io *)ctsio);
9388 return (CTL_RETVAL_COMPLETE);
9392 ctl_tur(struct ctl_scsiio *ctsio)
9395 CTL_DEBUG_PRINT(("ctl_tur\n"));
9397 ctl_set_success(ctsio);
9398 ctl_done((union ctl_io *)ctsio);
9400 return (CTL_RETVAL_COMPLETE);
9404 * SCSI VPD page 0x00, the Supported VPD Pages page.
9407 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9409 struct scsi_vpd_supported_pages *pages;
9411 struct ctl_lun *lun;
9414 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9416 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9417 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9418 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9419 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9420 ctsio->kern_sg_entries = 0;
9422 if (sup_page_size < alloc_len) {
9423 ctsio->residual = alloc_len - sup_page_size;
9424 ctsio->kern_data_len = sup_page_size;
9425 ctsio->kern_total_len = sup_page_size;
9427 ctsio->residual = 0;
9428 ctsio->kern_data_len = alloc_len;
9429 ctsio->kern_total_len = alloc_len;
9431 ctsio->kern_data_resid = 0;
9432 ctsio->kern_rel_offset = 0;
9433 ctsio->kern_sg_entries = 0;
9436 * The control device is always connected. The disk device, on the
9437 * other hand, may not be online all the time. Need to change this
9438 * to figure out whether the disk device is actually online or not.
9441 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9442 lun->be_lun->lun_type;
9444 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9447 /* Supported VPD pages */
9448 pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9450 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9451 /* Device Identification */
9452 pages->page_list[p++] = SVPD_DEVICE_ID;
9453 /* Extended INQUIRY Data */
9454 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9455 /* Mode Page Policy */
9456 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9458 pages->page_list[p++] = SVPD_SCSI_PORTS;
9459 /* Third-party Copy */
9460 pages->page_list[p++] = SVPD_SCSI_TPC;
9461 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9463 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9464 /* Block Device Characteristics */
9465 pages->page_list[p++] = SVPD_BDC;
9466 /* Logical Block Provisioning */
9467 pages->page_list[p++] = SVPD_LBP;
9471 ctl_set_success(ctsio);
9472 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9473 ctsio->be_move_done = ctl_config_move_done;
9474 ctl_datamove((union ctl_io *)ctsio);
9475 return (CTL_RETVAL_COMPLETE);
9479 * SCSI VPD page 0x80, the Unit Serial Number page.
9482 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9484 struct scsi_vpd_unit_serial_number *sn_ptr;
9485 struct ctl_lun *lun;
9488 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9490 data_len = 4 + CTL_SN_LEN;
9491 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9492 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9493 if (data_len < alloc_len) {
9494 ctsio->residual = alloc_len - data_len;
9495 ctsio->kern_data_len = data_len;
9496 ctsio->kern_total_len = data_len;
9498 ctsio->residual = 0;
9499 ctsio->kern_data_len = alloc_len;
9500 ctsio->kern_total_len = alloc_len;
9502 ctsio->kern_data_resid = 0;
9503 ctsio->kern_rel_offset = 0;
9504 ctsio->kern_sg_entries = 0;
9507 * The control device is always connected. The disk device, on the
9508 * other hand, may not be online all the time. Need to change this
9509 * to figure out whether the disk device is actually online or not.
9512 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9513 lun->be_lun->lun_type;
9515 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9517 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9518 sn_ptr->length = CTL_SN_LEN;
9520 * If we don't have a LUN, we just leave the serial number as
9524 strncpy((char *)sn_ptr->serial_num,
9525 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9527 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9529 ctl_set_success(ctsio);
9530 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9531 ctsio->be_move_done = ctl_config_move_done;
9532 ctl_datamove((union ctl_io *)ctsio);
9533 return (CTL_RETVAL_COMPLETE);
9538 * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9541 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9543 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9544 struct ctl_lun *lun;
9547 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9549 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9550 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9551 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9552 ctsio->kern_sg_entries = 0;
9554 if (data_len < alloc_len) {
9555 ctsio->residual = alloc_len - data_len;
9556 ctsio->kern_data_len = data_len;
9557 ctsio->kern_total_len = data_len;
9559 ctsio->residual = 0;
9560 ctsio->kern_data_len = alloc_len;
9561 ctsio->kern_total_len = alloc_len;
9563 ctsio->kern_data_resid = 0;
9564 ctsio->kern_rel_offset = 0;
9565 ctsio->kern_sg_entries = 0;
9568 * The control device is always connected. The disk device, on the
9569 * other hand, may not be online all the time.
9572 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9573 lun->be_lun->lun_type;
9575 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9576 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9577 scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9579 * We support head of queue, ordered and simple tags.
9581 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9583 * Volatile cache supported.
9585 eid_ptr->flags3 = SVPD_EID_V_SUP;
9588 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9589 * attention for a particular IT nexus on all LUNs once we report
9590 * it to that nexus once. This bit is required as of SPC-4.
9592 eid_ptr->flags4 = SVPD_EID_LUICLT;
9595 * XXX KDM in order to correctly answer this, we would need
9596 * information from the SIM to determine how much sense data it
9597 * can send. So this would really be a path inquiry field, most
9598 * likely. This can be set to a maximum of 252 according to SPC-4,
9599 * but the hardware may or may not be able to support that much.
9600 * 0 just means that the maximum sense data length is not reported.
9602 eid_ptr->max_sense_length = 0;
9604 ctl_set_success(ctsio);
9605 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9606 ctsio->be_move_done = ctl_config_move_done;
9607 ctl_datamove((union ctl_io *)ctsio);
9608 return (CTL_RETVAL_COMPLETE);
9612 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9614 struct scsi_vpd_mode_page_policy *mpp_ptr;
9615 struct ctl_lun *lun;
9618 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9620 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9621 sizeof(struct scsi_vpd_mode_page_policy_descr);
9623 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9624 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9625 ctsio->kern_sg_entries = 0;
9627 if (data_len < alloc_len) {
9628 ctsio->residual = alloc_len - data_len;
9629 ctsio->kern_data_len = data_len;
9630 ctsio->kern_total_len = data_len;
9632 ctsio->residual = 0;
9633 ctsio->kern_data_len = alloc_len;
9634 ctsio->kern_total_len = alloc_len;
9636 ctsio->kern_data_resid = 0;
9637 ctsio->kern_rel_offset = 0;
9638 ctsio->kern_sg_entries = 0;
9641 * The control device is always connected. The disk device, on the
9642 * other hand, may not be online all the time.
9645 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9646 lun->be_lun->lun_type;
9648 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9649 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9650 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9651 mpp_ptr->descr[0].page_code = 0x3f;
9652 mpp_ptr->descr[0].subpage_code = 0xff;
9653 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9655 ctl_set_success(ctsio);
9656 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9657 ctsio->be_move_done = ctl_config_move_done;
9658 ctl_datamove((union ctl_io *)ctsio);
9659 return (CTL_RETVAL_COMPLETE);
9663 * SCSI VPD page 0x83, the Device Identification page.
9666 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9668 struct scsi_vpd_device_id *devid_ptr;
9669 struct scsi_vpd_id_descriptor *desc;
9670 struct ctl_softc *softc;
9671 struct ctl_lun *lun;
9672 struct ctl_port *port;
9676 softc = control_softc;
9678 port = ctl_io_port(&ctsio->io_hdr);
9679 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9681 data_len = sizeof(struct scsi_vpd_device_id) +
9682 sizeof(struct scsi_vpd_id_descriptor) +
9683 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9684 sizeof(struct scsi_vpd_id_descriptor) +
9685 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9686 if (lun && lun->lun_devid)
9687 data_len += lun->lun_devid->len;
9688 if (port && port->port_devid)
9689 data_len += port->port_devid->len;
9690 if (port && port->target_devid)
9691 data_len += port->target_devid->len;
9693 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9694 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9695 ctsio->kern_sg_entries = 0;
9697 if (data_len < alloc_len) {
9698 ctsio->residual = alloc_len - data_len;
9699 ctsio->kern_data_len = data_len;
9700 ctsio->kern_total_len = data_len;
9702 ctsio->residual = 0;
9703 ctsio->kern_data_len = alloc_len;
9704 ctsio->kern_total_len = alloc_len;
9706 ctsio->kern_data_resid = 0;
9707 ctsio->kern_rel_offset = 0;
9708 ctsio->kern_sg_entries = 0;
9711 * The control device is always connected. The disk device, on the
9712 * other hand, may not be online all the time.
9715 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9716 lun->be_lun->lun_type;
9718 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9719 devid_ptr->page_code = SVPD_DEVICE_ID;
9720 scsi_ulto2b(data_len - 4, devid_ptr->length);
9722 if (port && port->port_type == CTL_PORT_FC)
9723 proto = SCSI_PROTO_FC << 4;
9724 else if (port && port->port_type == CTL_PORT_ISCSI)
9725 proto = SCSI_PROTO_ISCSI << 4;
9727 proto = SCSI_PROTO_SPI << 4;
9728 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9731 * We're using a LUN association here. i.e., this device ID is a
9732 * per-LUN identifier.
9734 if (lun && lun->lun_devid) {
9735 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9736 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9737 lun->lun_devid->len);
9741 * This is for the WWPN which is a port association.
9743 if (port && port->port_devid) {
9744 memcpy(desc, port->port_devid->data, port->port_devid->len);
9745 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9746 port->port_devid->len);
9750 * This is for the Relative Target Port(type 4h) identifier
9752 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9753 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9754 SVPD_ID_TYPE_RELTARG;
9756 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9757 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9758 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9761 * This is for the Target Port Group(type 5h) identifier
9763 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9764 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9765 SVPD_ID_TYPE_TPORTGRP;
9767 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / softc->port_cnt + 1,
9768 &desc->identifier[2]);
9769 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9770 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9773 * This is for the Target identifier
9775 if (port && port->target_devid) {
9776 memcpy(desc, port->target_devid->data, port->target_devid->len);
9779 ctl_set_success(ctsio);
9780 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9781 ctsio->be_move_done = ctl_config_move_done;
9782 ctl_datamove((union ctl_io *)ctsio);
9783 return (CTL_RETVAL_COMPLETE);
9787 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9789 struct ctl_softc *softc = control_softc;
9790 struct scsi_vpd_scsi_ports *sp;
9791 struct scsi_vpd_port_designation *pd;
9792 struct scsi_vpd_port_designation_cont *pdc;
9793 struct ctl_lun *lun;
9794 struct ctl_port *port;
9795 int data_len, num_target_ports, iid_len, id_len;
9797 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9799 num_target_ports = 0;
9802 mtx_lock(&softc->ctl_lock);
9803 STAILQ_FOREACH(port, &softc->port_list, links) {
9804 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9807 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
9810 if (port->init_devid)
9811 iid_len += port->init_devid->len;
9812 if (port->port_devid)
9813 id_len += port->port_devid->len;
9815 mtx_unlock(&softc->ctl_lock);
9817 data_len = sizeof(struct scsi_vpd_scsi_ports) +
9818 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9819 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9820 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9821 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9822 ctsio->kern_sg_entries = 0;
9824 if (data_len < alloc_len) {
9825 ctsio->residual = alloc_len - data_len;
9826 ctsio->kern_data_len = data_len;
9827 ctsio->kern_total_len = data_len;
9829 ctsio->residual = 0;
9830 ctsio->kern_data_len = alloc_len;
9831 ctsio->kern_total_len = alloc_len;
9833 ctsio->kern_data_resid = 0;
9834 ctsio->kern_rel_offset = 0;
9835 ctsio->kern_sg_entries = 0;
9838 * The control device is always connected. The disk device, on the
9839 * other hand, may not be online all the time. Need to change this
9840 * to figure out whether the disk device is actually online or not.
9843 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9844 lun->be_lun->lun_type;
9846 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9848 sp->page_code = SVPD_SCSI_PORTS;
9849 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9851 pd = &sp->design[0];
9853 mtx_lock(&softc->ctl_lock);
9854 STAILQ_FOREACH(port, &softc->port_list, links) {
9855 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9858 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
9860 scsi_ulto2b(port->targ_port, pd->relative_port_id);
9861 if (port->init_devid) {
9862 iid_len = port->init_devid->len;
9863 memcpy(pd->initiator_transportid,
9864 port->init_devid->data, port->init_devid->len);
9867 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9868 pdc = (struct scsi_vpd_port_designation_cont *)
9869 (&pd->initiator_transportid[iid_len]);
9870 if (port->port_devid) {
9871 id_len = port->port_devid->len;
9872 memcpy(pdc->target_port_descriptors,
9873 port->port_devid->data, port->port_devid->len);
9876 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9877 pd = (struct scsi_vpd_port_designation *)
9878 ((uint8_t *)pdc->target_port_descriptors + id_len);
9880 mtx_unlock(&softc->ctl_lock);
9882 ctl_set_success(ctsio);
9883 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9884 ctsio->be_move_done = ctl_config_move_done;
9885 ctl_datamove((union ctl_io *)ctsio);
9886 return (CTL_RETVAL_COMPLETE);
9890 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9892 struct scsi_vpd_block_limits *bl_ptr;
9893 struct ctl_lun *lun;
9896 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9898 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9899 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9900 ctsio->kern_sg_entries = 0;
9902 if (sizeof(*bl_ptr) < alloc_len) {
9903 ctsio->residual = alloc_len - sizeof(*bl_ptr);
9904 ctsio->kern_data_len = sizeof(*bl_ptr);
9905 ctsio->kern_total_len = sizeof(*bl_ptr);
9907 ctsio->residual = 0;
9908 ctsio->kern_data_len = alloc_len;
9909 ctsio->kern_total_len = alloc_len;
9911 ctsio->kern_data_resid = 0;
9912 ctsio->kern_rel_offset = 0;
9913 ctsio->kern_sg_entries = 0;
9916 * The control device is always connected. The disk device, on the
9917 * other hand, may not be online all the time. Need to change this
9918 * to figure out whether the disk device is actually online or not.
9921 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9922 lun->be_lun->lun_type;
9924 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9926 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9927 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
9928 bl_ptr->max_cmp_write_len = 0xff;
9929 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9931 bs = lun->be_lun->blocksize;
9932 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
9933 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9934 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
9935 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
9936 if (lun->be_lun->ublockexp != 0) {
9937 scsi_ulto4b((1 << lun->be_lun->ublockexp),
9938 bl_ptr->opt_unmap_grain);
9939 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
9940 bl_ptr->unmap_grain_align);
9943 scsi_ulto4b(lun->be_lun->atomicblock,
9944 bl_ptr->max_atomic_transfer_length);
9945 scsi_ulto4b(0, bl_ptr->atomic_alignment);
9946 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
9948 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
9950 ctl_set_success(ctsio);
9951 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9952 ctsio->be_move_done = ctl_config_move_done;
9953 ctl_datamove((union ctl_io *)ctsio);
9954 return (CTL_RETVAL_COMPLETE);
9958 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
9960 struct scsi_vpd_block_device_characteristics *bdc_ptr;
9961 struct ctl_lun *lun;
9965 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9967 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
9968 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
9969 ctsio->kern_sg_entries = 0;
9971 if (sizeof(*bdc_ptr) < alloc_len) {
9972 ctsio->residual = alloc_len - sizeof(*bdc_ptr);
9973 ctsio->kern_data_len = sizeof(*bdc_ptr);
9974 ctsio->kern_total_len = sizeof(*bdc_ptr);
9976 ctsio->residual = 0;
9977 ctsio->kern_data_len = alloc_len;
9978 ctsio->kern_total_len = alloc_len;
9980 ctsio->kern_data_resid = 0;
9981 ctsio->kern_rel_offset = 0;
9982 ctsio->kern_sg_entries = 0;
9985 * The control device is always connected. The disk device, on the
9986 * other hand, may not be online all the time. Need to change this
9987 * to figure out whether the disk device is actually online or not.
9990 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9991 lun->be_lun->lun_type;
9993 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9994 bdc_ptr->page_code = SVPD_BDC;
9995 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
9997 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL)
9998 i = strtol(value, NULL, 0);
10000 i = CTL_DEFAULT_ROTATION_RATE;
10001 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
10003 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL)
10004 i = strtol(value, NULL, 0);
10007 bdc_ptr->wab_wac_ff = (i & 0x0f);
10008 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
10010 ctl_set_success(ctsio);
10011 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10012 ctsio->be_move_done = ctl_config_move_done;
10013 ctl_datamove((union ctl_io *)ctsio);
10014 return (CTL_RETVAL_COMPLETE);
10018 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
10020 struct scsi_vpd_logical_block_prov *lbp_ptr;
10021 struct ctl_lun *lun;
10023 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10025 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
10026 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
10027 ctsio->kern_sg_entries = 0;
10029 if (sizeof(*lbp_ptr) < alloc_len) {
10030 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
10031 ctsio->kern_data_len = sizeof(*lbp_ptr);
10032 ctsio->kern_total_len = sizeof(*lbp_ptr);
10034 ctsio->residual = 0;
10035 ctsio->kern_data_len = alloc_len;
10036 ctsio->kern_total_len = alloc_len;
10038 ctsio->kern_data_resid = 0;
10039 ctsio->kern_rel_offset = 0;
10040 ctsio->kern_sg_entries = 0;
10043 * The control device is always connected. The disk device, on the
10044 * other hand, may not be online all the time. Need to change this
10045 * to figure out whether the disk device is actually online or not.
10048 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10049 lun->be_lun->lun_type;
10051 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10053 lbp_ptr->page_code = SVPD_LBP;
10054 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
10055 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
10056 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10057 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
10058 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
10059 lbp_ptr->prov_type = SVPD_LBP_THIN;
10062 ctl_set_success(ctsio);
10063 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10064 ctsio->be_move_done = ctl_config_move_done;
10065 ctl_datamove((union ctl_io *)ctsio);
10066 return (CTL_RETVAL_COMPLETE);
10070 * INQUIRY with the EVPD bit set.
10073 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
10075 struct ctl_lun *lun;
10076 struct scsi_inquiry *cdb;
10077 int alloc_len, retval;
10079 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10080 cdb = (struct scsi_inquiry *)ctsio->cdb;
10081 alloc_len = scsi_2btoul(cdb->length);
10083 switch (cdb->page_code) {
10084 case SVPD_SUPPORTED_PAGES:
10085 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
10087 case SVPD_UNIT_SERIAL_NUMBER:
10088 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
10090 case SVPD_DEVICE_ID:
10091 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
10093 case SVPD_EXTENDED_INQUIRY_DATA:
10094 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
10096 case SVPD_MODE_PAGE_POLICY:
10097 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
10099 case SVPD_SCSI_PORTS:
10100 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10102 case SVPD_SCSI_TPC:
10103 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10105 case SVPD_BLOCK_LIMITS:
10106 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10108 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10111 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10113 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10116 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10118 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10122 ctl_set_invalid_field(ctsio,
10128 ctl_done((union ctl_io *)ctsio);
10129 retval = CTL_RETVAL_COMPLETE;
10137 * Standard INQUIRY data.
10140 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10142 struct scsi_inquiry_data *inq_ptr;
10143 struct scsi_inquiry *cdb;
10144 struct ctl_softc *softc;
10145 struct ctl_port *port;
10146 struct ctl_lun *lun;
10148 uint32_t alloc_len, data_len;
10149 ctl_port_type port_type;
10151 softc = control_softc;
10154 * Figure out whether we're talking to a Fibre Channel port or not.
10155 * We treat the ioctl front end, and any SCSI adapters, as packetized
10158 port = ctl_io_port(&ctsio->io_hdr);
10160 port_type = port->port_type;
10162 port_type = CTL_PORT_SCSI;
10163 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10164 port_type = CTL_PORT_SCSI;
10166 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10167 cdb = (struct scsi_inquiry *)ctsio->cdb;
10168 alloc_len = scsi_2btoul(cdb->length);
10171 * We malloc the full inquiry data size here and fill it
10172 * in. If the user only asks for less, we'll give him
10175 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10176 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10177 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10178 ctsio->kern_sg_entries = 0;
10179 ctsio->kern_data_resid = 0;
10180 ctsio->kern_rel_offset = 0;
10182 if (data_len < alloc_len) {
10183 ctsio->residual = alloc_len - data_len;
10184 ctsio->kern_data_len = data_len;
10185 ctsio->kern_total_len = data_len;
10187 ctsio->residual = 0;
10188 ctsio->kern_data_len = alloc_len;
10189 ctsio->kern_total_len = alloc_len;
10193 if ((lun->flags & CTL_LUN_PRIMARY_SC) ||
10194 softc->ha_link >= CTL_HA_LINK_UNKNOWN) {
10195 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10196 lun->be_lun->lun_type;
10198 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) |
10199 lun->be_lun->lun_type;
10202 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10204 /* RMB in byte 2 is 0 */
10205 inq_ptr->version = SCSI_REV_SPC4;
10208 * According to SAM-3, even if a device only supports a single
10209 * level of LUN addressing, it should still set the HISUP bit:
10211 * 4.9.1 Logical unit numbers overview
10213 * All logical unit number formats described in this standard are
10214 * hierarchical in structure even when only a single level in that
10215 * hierarchy is used. The HISUP bit shall be set to one in the
10216 * standard INQUIRY data (see SPC-2) when any logical unit number
10217 * format described in this standard is used. Non-hierarchical
10218 * formats are outside the scope of this standard.
10220 * Therefore we set the HiSup bit here.
10222 * The reponse format is 2, per SPC-3.
10224 inq_ptr->response_format = SID_HiSup | 2;
10226 inq_ptr->additional_length = data_len -
10227 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10228 CTL_DEBUG_PRINT(("additional_length = %d\n",
10229 inq_ptr->additional_length));
10231 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10232 /* 16 bit addressing */
10233 if (port_type == CTL_PORT_SCSI)
10234 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10235 /* XXX set the SID_MultiP bit here if we're actually going to
10236 respond on multiple ports */
10237 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10239 /* 16 bit data bus, synchronous transfers */
10240 if (port_type == CTL_PORT_SCSI)
10241 inq_ptr->flags = SID_WBus16 | SID_Sync;
10243 * XXX KDM do we want to support tagged queueing on the control
10247 || (lun->be_lun->lun_type != T_PROCESSOR))
10248 inq_ptr->flags |= SID_CmdQue;
10250 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10251 * We have 8 bytes for the vendor name, and 16 bytes for the device
10252 * name and 4 bytes for the revision.
10254 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10255 "vendor")) == NULL) {
10256 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10258 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10259 strncpy(inq_ptr->vendor, val,
10260 min(sizeof(inq_ptr->vendor), strlen(val)));
10263 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10264 sizeof(inq_ptr->product));
10265 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10266 switch (lun->be_lun->lun_type) {
10268 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10269 sizeof(inq_ptr->product));
10272 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10273 sizeof(inq_ptr->product));
10276 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10277 sizeof(inq_ptr->product));
10281 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10282 strncpy(inq_ptr->product, val,
10283 min(sizeof(inq_ptr->product), strlen(val)));
10287 * XXX make this a macro somewhere so it automatically gets
10288 * incremented when we make changes.
10290 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10291 "revision")) == NULL) {
10292 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10294 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10295 strncpy(inq_ptr->revision, val,
10296 min(sizeof(inq_ptr->revision), strlen(val)));
10300 * For parallel SCSI, we support double transition and single
10301 * transition clocking. We also support QAS (Quick Arbitration
10302 * and Selection) and Information Unit transfers on both the
10303 * control and array devices.
10305 if (port_type == CTL_PORT_SCSI)
10306 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10309 /* SAM-5 (no version claimed) */
10310 scsi_ulto2b(0x00A0, inq_ptr->version1);
10311 /* SPC-4 (no version claimed) */
10312 scsi_ulto2b(0x0460, inq_ptr->version2);
10313 if (port_type == CTL_PORT_FC) {
10314 /* FCP-2 ANSI INCITS.350:2003 */
10315 scsi_ulto2b(0x0917, inq_ptr->version3);
10316 } else if (port_type == CTL_PORT_SCSI) {
10317 /* SPI-4 ANSI INCITS.362:200x */
10318 scsi_ulto2b(0x0B56, inq_ptr->version3);
10319 } else if (port_type == CTL_PORT_ISCSI) {
10320 /* iSCSI (no version claimed) */
10321 scsi_ulto2b(0x0960, inq_ptr->version3);
10322 } else if (port_type == CTL_PORT_SAS) {
10323 /* SAS (no version claimed) */
10324 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10328 /* SBC-4 (no version claimed) */
10329 scsi_ulto2b(0x0600, inq_ptr->version4);
10331 switch (lun->be_lun->lun_type) {
10333 /* SBC-4 (no version claimed) */
10334 scsi_ulto2b(0x0600, inq_ptr->version4);
10342 ctl_set_success(ctsio);
10343 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10344 ctsio->be_move_done = ctl_config_move_done;
10345 ctl_datamove((union ctl_io *)ctsio);
10346 return (CTL_RETVAL_COMPLETE);
10350 ctl_inquiry(struct ctl_scsiio *ctsio)
10352 struct scsi_inquiry *cdb;
10355 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10357 cdb = (struct scsi_inquiry *)ctsio->cdb;
10358 if (cdb->byte2 & SI_EVPD)
10359 retval = ctl_inquiry_evpd(ctsio);
10360 else if (cdb->page_code == 0)
10361 retval = ctl_inquiry_std(ctsio);
10363 ctl_set_invalid_field(ctsio,
10369 ctl_done((union ctl_io *)ctsio);
10370 return (CTL_RETVAL_COMPLETE);
10377 * For known CDB types, parse the LBA and length.
10380 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10382 if (io->io_hdr.io_type != CTL_IO_SCSI)
10385 switch (io->scsiio.cdb[0]) {
10386 case COMPARE_AND_WRITE: {
10387 struct scsi_compare_and_write *cdb;
10389 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10391 *lba = scsi_8btou64(cdb->addr);
10392 *len = cdb->length;
10397 struct scsi_rw_6 *cdb;
10399 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10401 *lba = scsi_3btoul(cdb->addr);
10402 /* only 5 bits are valid in the most significant address byte */
10404 *len = cdb->length;
10409 struct scsi_rw_10 *cdb;
10411 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10413 *lba = scsi_4btoul(cdb->addr);
10414 *len = scsi_2btoul(cdb->length);
10417 case WRITE_VERIFY_10: {
10418 struct scsi_write_verify_10 *cdb;
10420 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10422 *lba = scsi_4btoul(cdb->addr);
10423 *len = scsi_2btoul(cdb->length);
10428 struct scsi_rw_12 *cdb;
10430 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10432 *lba = scsi_4btoul(cdb->addr);
10433 *len = scsi_4btoul(cdb->length);
10436 case WRITE_VERIFY_12: {
10437 struct scsi_write_verify_12 *cdb;
10439 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10441 *lba = scsi_4btoul(cdb->addr);
10442 *len = scsi_4btoul(cdb->length);
10447 case WRITE_ATOMIC_16: {
10448 struct scsi_rw_16 *cdb;
10450 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10452 *lba = scsi_8btou64(cdb->addr);
10453 *len = scsi_4btoul(cdb->length);
10456 case WRITE_VERIFY_16: {
10457 struct scsi_write_verify_16 *cdb;
10459 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10461 *lba = scsi_8btou64(cdb->addr);
10462 *len = scsi_4btoul(cdb->length);
10465 case WRITE_SAME_10: {
10466 struct scsi_write_same_10 *cdb;
10468 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10470 *lba = scsi_4btoul(cdb->addr);
10471 *len = scsi_2btoul(cdb->length);
10474 case WRITE_SAME_16: {
10475 struct scsi_write_same_16 *cdb;
10477 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10479 *lba = scsi_8btou64(cdb->addr);
10480 *len = scsi_4btoul(cdb->length);
10484 struct scsi_verify_10 *cdb;
10486 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10488 *lba = scsi_4btoul(cdb->addr);
10489 *len = scsi_2btoul(cdb->length);
10493 struct scsi_verify_12 *cdb;
10495 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10497 *lba = scsi_4btoul(cdb->addr);
10498 *len = scsi_4btoul(cdb->length);
10502 struct scsi_verify_16 *cdb;
10504 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10506 *lba = scsi_8btou64(cdb->addr);
10507 *len = scsi_4btoul(cdb->length);
10515 case SERVICE_ACTION_IN: { /* GET LBA STATUS */
10516 struct scsi_get_lba_status *cdb;
10518 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10519 *lba = scsi_8btou64(cdb->addr);
10525 break; /* NOTREACHED */
10532 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10535 uint64_t endlba1, endlba2;
10537 endlba1 = lba1 + len1 - (seq ? 0 : 1);
10538 endlba2 = lba2 + len2 - 1;
10540 if ((endlba1 < lba2) || (endlba2 < lba1))
10541 return (CTL_ACTION_PASS);
10543 return (CTL_ACTION_BLOCK);
10547 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10549 struct ctl_ptr_len_flags *ptrlen;
10550 struct scsi_unmap_desc *buf, *end, *range;
10554 /* If not UNMAP -- go other way. */
10555 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10556 io->scsiio.cdb[0] != UNMAP)
10557 return (CTL_ACTION_ERROR);
10559 /* If UNMAP without data -- block and wait for data. */
10560 ptrlen = (struct ctl_ptr_len_flags *)
10561 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10562 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10563 ptrlen->ptr == NULL)
10564 return (CTL_ACTION_BLOCK);
10566 /* UNMAP with data -- check for collision. */
10567 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10568 end = buf + ptrlen->len / sizeof(*buf);
10569 for (range = buf; range < end; range++) {
10570 lba = scsi_8btou64(range->lba);
10571 len = scsi_4btoul(range->length);
10572 if ((lba < lba2 + len2) && (lba + len > lba2))
10573 return (CTL_ACTION_BLOCK);
10575 return (CTL_ACTION_PASS);
10579 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10581 uint64_t lba1, lba2;
10582 uint64_t len1, len2;
10585 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10586 return (CTL_ACTION_ERROR);
10588 retval = ctl_extent_check_unmap(io1, lba2, len2);
10589 if (retval != CTL_ACTION_ERROR)
10592 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10593 return (CTL_ACTION_ERROR);
10595 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10597 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
10601 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2)
10603 uint64_t lba1, lba2;
10604 uint64_t len1, len2;
10606 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10607 return (CTL_ACTION_PASS);
10608 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10609 return (CTL_ACTION_ERROR);
10610 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10611 return (CTL_ACTION_ERROR);
10613 if (lba1 + len1 == lba2)
10614 return (CTL_ACTION_BLOCK);
10615 return (CTL_ACTION_PASS);
10619 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10620 union ctl_io *ooa_io)
10622 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10623 ctl_serialize_action *serialize_row;
10626 * The initiator attempted multiple untagged commands at the same
10627 * time. Can't do that.
10629 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10630 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10631 && ((pending_io->io_hdr.nexus.targ_port ==
10632 ooa_io->io_hdr.nexus.targ_port)
10633 && (pending_io->io_hdr.nexus.initid ==
10634 ooa_io->io_hdr.nexus.initid))
10635 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10636 CTL_FLAG_STATUS_SENT)) == 0))
10637 return (CTL_ACTION_OVERLAP);
10640 * The initiator attempted to send multiple tagged commands with
10641 * the same ID. (It's fine if different initiators have the same
10644 * Even if all of those conditions are true, we don't kill the I/O
10645 * if the command ahead of us has been aborted. We won't end up
10646 * sending it to the FETD, and it's perfectly legal to resend a
10647 * command with the same tag number as long as the previous
10648 * instance of this tag number has been aborted somehow.
10650 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10651 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10652 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10653 && ((pending_io->io_hdr.nexus.targ_port ==
10654 ooa_io->io_hdr.nexus.targ_port)
10655 && (pending_io->io_hdr.nexus.initid ==
10656 ooa_io->io_hdr.nexus.initid))
10657 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10658 CTL_FLAG_STATUS_SENT)) == 0))
10659 return (CTL_ACTION_OVERLAP_TAG);
10662 * If we get a head of queue tag, SAM-3 says that we should
10663 * immediately execute it.
10665 * What happens if this command would normally block for some other
10666 * reason? e.g. a request sense with a head of queue tag
10667 * immediately after a write. Normally that would block, but this
10668 * will result in its getting executed immediately...
10670 * We currently return "pass" instead of "skip", so we'll end up
10671 * going through the rest of the queue to check for overlapped tags.
10673 * XXX KDM check for other types of blockage first??
10675 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10676 return (CTL_ACTION_PASS);
10679 * Ordered tags have to block until all items ahead of them
10680 * have completed. If we get called with an ordered tag, we always
10681 * block, if something else is ahead of us in the queue.
10683 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10684 return (CTL_ACTION_BLOCK);
10687 * Simple tags get blocked until all head of queue and ordered tags
10688 * ahead of them have completed. I'm lumping untagged commands in
10689 * with simple tags here. XXX KDM is that the right thing to do?
10691 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10692 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10693 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10694 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10695 return (CTL_ACTION_BLOCK);
10697 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
10698 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
10700 serialize_row = ctl_serialize_table[ooa_entry->seridx];
10702 switch (serialize_row[pending_entry->seridx]) {
10703 case CTL_SER_BLOCK:
10704 return (CTL_ACTION_BLOCK);
10705 case CTL_SER_EXTENT:
10706 return (ctl_extent_check(ooa_io, pending_io,
10707 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10708 case CTL_SER_EXTENTOPT:
10709 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
10710 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
10711 return (ctl_extent_check(ooa_io, pending_io,
10713 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10714 return (CTL_ACTION_PASS);
10715 case CTL_SER_EXTENTSEQ:
10716 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF)
10717 return (ctl_extent_check_seq(ooa_io, pending_io));
10718 return (CTL_ACTION_PASS);
10720 return (CTL_ACTION_PASS);
10721 case CTL_SER_BLOCKOPT:
10722 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
10723 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
10724 return (CTL_ACTION_BLOCK);
10725 return (CTL_ACTION_PASS);
10727 return (CTL_ACTION_SKIP);
10729 panic("invalid serialization value %d",
10730 serialize_row[pending_entry->seridx]);
10733 return (CTL_ACTION_ERROR);
10737 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
10739 * - pending_io is generally either incoming, or on the blocked queue
10740 * - starting I/O is the I/O we want to start the check with.
10743 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
10744 union ctl_io *starting_io)
10746 union ctl_io *ooa_io;
10749 mtx_assert(&lun->lun_lock, MA_OWNED);
10752 * Run back along the OOA queue, starting with the current
10753 * blocked I/O and going through every I/O before it on the
10754 * queue. If starting_io is NULL, we'll just end up returning
10757 for (ooa_io = starting_io; ooa_io != NULL;
10758 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
10762 * This routine just checks to see whether
10763 * cur_blocked is blocked by ooa_io, which is ahead
10764 * of it in the queue. It doesn't queue/dequeue
10767 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
10769 case CTL_ACTION_BLOCK:
10770 case CTL_ACTION_OVERLAP:
10771 case CTL_ACTION_OVERLAP_TAG:
10772 case CTL_ACTION_SKIP:
10773 case CTL_ACTION_ERROR:
10775 break; /* NOTREACHED */
10776 case CTL_ACTION_PASS:
10779 panic("invalid action %d", action);
10780 break; /* NOTREACHED */
10784 return (CTL_ACTION_PASS);
10789 * - An I/O has just completed, and has been removed from the per-LUN OOA
10790 * queue, so some items on the blocked queue may now be unblocked.
10793 ctl_check_blocked(struct ctl_lun *lun)
10795 struct ctl_softc *softc = lun->ctl_softc;
10796 union ctl_io *cur_blocked, *next_blocked;
10798 mtx_assert(&lun->lun_lock, MA_OWNED);
10801 * Run forward from the head of the blocked queue, checking each
10802 * entry against the I/Os prior to it on the OOA queue to see if
10803 * there is still any blockage.
10805 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
10806 * with our removing a variable on it while it is traversing the
10809 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
10810 cur_blocked != NULL; cur_blocked = next_blocked) {
10811 union ctl_io *prev_ooa;
10814 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
10817 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
10818 ctl_ooaq, ooa_links);
10821 * If cur_blocked happens to be the first item in the OOA
10822 * queue now, prev_ooa will be NULL, and the action
10823 * returned will just be CTL_ACTION_PASS.
10825 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
10828 case CTL_ACTION_BLOCK:
10829 /* Nothing to do here, still blocked */
10831 case CTL_ACTION_OVERLAP:
10832 case CTL_ACTION_OVERLAP_TAG:
10834 * This shouldn't happen! In theory we've already
10835 * checked this command for overlap...
10838 case CTL_ACTION_PASS:
10839 case CTL_ACTION_SKIP: {
10840 const struct ctl_cmd_entry *entry;
10843 * The skip case shouldn't happen, this transaction
10844 * should have never made it onto the blocked queue.
10847 * This I/O is no longer blocked, we can remove it
10848 * from the blocked queue. Since this is a TAILQ
10849 * (doubly linked list), we can do O(1) removals
10850 * from any place on the list.
10852 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
10854 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
10856 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
10857 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){
10859 * Need to send IO back to original side to
10862 union ctl_ha_msg msg_info;
10864 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
10865 msg_info.hdr.original_sc =
10866 cur_blocked->io_hdr.original_sc;
10867 msg_info.hdr.serializing_sc = cur_blocked;
10868 msg_info.hdr.msg_type = CTL_MSG_R2R;
10869 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
10870 sizeof(msg_info.hdr), M_NOWAIT);
10873 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
10876 * Check this I/O for LUN state changes that may
10877 * have happened while this command was blocked.
10878 * The LUN state may have been changed by a command
10879 * ahead of us in the queue, so we need to re-check
10880 * for any states that can be caused by SCSI
10883 if (ctl_scsiio_lun_check(lun, entry,
10884 &cur_blocked->scsiio) == 0) {
10885 cur_blocked->io_hdr.flags |=
10886 CTL_FLAG_IS_WAS_ON_RTR;
10887 ctl_enqueue_rtr(cur_blocked);
10889 ctl_done(cur_blocked);
10894 * This probably shouldn't happen -- we shouldn't
10895 * get CTL_ACTION_ERROR, or anything else.
10901 return (CTL_RETVAL_COMPLETE);
10905 * This routine (with one exception) checks LUN flags that can be set by
10906 * commands ahead of us in the OOA queue. These flags have to be checked
10907 * when a command initially comes in, and when we pull a command off the
10908 * blocked queue and are preparing to execute it. The reason we have to
10909 * check these flags for commands on the blocked queue is that the LUN
10910 * state may have been changed by a command ahead of us while we're on the
10913 * Ordering is somewhat important with these checks, so please pay
10914 * careful attention to the placement of any new checks.
10917 ctl_scsiio_lun_check(struct ctl_lun *lun,
10918 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
10920 struct ctl_softc *softc = lun->ctl_softc;
10926 mtx_assert(&lun->lun_lock, MA_OWNED);
10929 * If this shelf is a secondary shelf controller, we may have to
10930 * reject some commands disallowed by HA mode and link state.
10932 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
10933 if (softc->ha_link == CTL_HA_LINK_OFFLINE &&
10934 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
10935 ctl_set_lun_unavail(ctsio);
10939 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 &&
10940 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
10941 ctl_set_lun_transit(ctsio);
10945 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY &&
10946 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) {
10947 ctl_set_lun_standby(ctsio);
10952 /* The rest of checks are only done on executing side */
10953 if (softc->ha_mode == CTL_HA_MODE_XFER)
10957 if (entry->pattern & CTL_LUN_PAT_WRITE) {
10959 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) {
10960 ctl_set_hw_write_protected(ctsio);
10964 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT]
10965 .eca_and_aen & SCP_SWP) != 0) {
10966 ctl_set_sense(ctsio, /*current_error*/ 1,
10967 /*sense_key*/ SSD_KEY_DATA_PROTECT,
10968 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
10975 * Check for a reservation conflict. If this command isn't allowed
10976 * even on reserved LUNs, and if this initiator isn't the one who
10977 * reserved us, reject the command with a reservation conflict.
10979 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
10980 if ((lun->flags & CTL_LUN_RESERVED)
10981 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
10982 if (lun->res_idx != residx) {
10983 ctl_set_reservation_conflict(ctsio);
10989 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
10990 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
10991 /* No reservation or command is allowed. */;
10992 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
10993 (lun->res_type == SPR_TYPE_WR_EX ||
10994 lun->res_type == SPR_TYPE_WR_EX_RO ||
10995 lun->res_type == SPR_TYPE_WR_EX_AR)) {
10996 /* The command is allowed for Write Exclusive resv. */;
10999 * if we aren't registered or it's a res holder type
11000 * reservation and this isn't the res holder then set a
11003 if (ctl_get_prkey(lun, residx) == 0
11004 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
11005 ctl_set_reservation_conflict(ctsio);
11011 if ((lun->flags & CTL_LUN_OFFLINE)
11012 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0)) {
11013 ctl_set_lun_not_ready(ctsio);
11018 if ((lun->flags & CTL_LUN_STOPPED)
11019 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
11020 /* "Logical unit not ready, initializing cmd. required" */
11021 ctl_set_lun_stopped(ctsio);
11026 if ((lun->flags & CTL_LUN_INOPERABLE)
11027 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
11028 /* "Medium format corrupted" */
11029 ctl_set_medium_format_corrupted(ctsio);
11039 ctl_failover_io(union ctl_io *io, int have_lock)
11041 ctl_set_busy(&io->scsiio);
11046 ctl_failover_lun(struct ctl_lun *lun)
11048 struct ctl_softc *softc = lun->ctl_softc;
11049 struct ctl_io_hdr *io, *next_io;
11051 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", lun->lun));
11052 if (softc->ha_mode == CTL_HA_MODE_XFER) {
11053 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11054 /* We are master */
11055 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11056 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11057 io->flags |= CTL_FLAG_ABORT;
11058 io->flags |= CTL_FLAG_FAILOVER;
11059 } else { /* This can be only due to DATAMOVE */
11060 io->msg_type = CTL_MSG_DATAMOVE_DONE;
11061 io->flags &= ~CTL_FLAG_DMA_INPROG;
11062 io->flags |= CTL_FLAG_IO_ACTIVE;
11063 io->port_status = 31340;
11064 ctl_enqueue_isc((union ctl_io *)io);
11068 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11069 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11070 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11071 io->flags |= CTL_FLAG_FAILOVER;
11073 ctl_set_busy(&((union ctl_io *)io)->
11075 ctl_done((union ctl_io *)io);
11079 } else { /* SERIALIZE modes */
11080 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links,
11082 /* We are master */
11083 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11084 TAILQ_REMOVE(&lun->blocked_queue, io,
11086 io->flags &= ~CTL_FLAG_BLOCKED;
11087 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11088 ctl_free_io((union ctl_io *)io);
11091 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11092 /* We are master */
11093 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11094 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11095 ctl_free_io((union ctl_io *)io);
11098 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11099 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11100 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) {
11101 ctl_set_busy(&((union ctl_io *)io)->
11103 ctl_done((union ctl_io *)io);
11107 ctl_check_blocked(lun);
11112 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio)
11114 struct ctl_lun *lun;
11115 const struct ctl_cmd_entry *entry;
11116 uint32_t initidx, targ_lun;
11123 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11124 if ((targ_lun < CTL_MAX_LUNS)
11125 && ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
11127 * If the LUN is invalid, pretend that it doesn't exist.
11128 * It will go away as soon as all pending I/O has been
11131 mtx_lock(&lun->lun_lock);
11132 if (lun->flags & CTL_LUN_DISABLED) {
11133 mtx_unlock(&lun->lun_lock);
11135 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11136 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11138 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
11139 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
11143 * Every I/O goes into the OOA queue for a
11144 * particular LUN, and stays there until completion.
11147 if (TAILQ_EMPTY(&lun->ooa_queue)) {
11148 lun->idle_time += getsbinuptime() -
11152 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr,
11156 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11157 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11160 /* Get command entry and return error if it is unsuppotyed. */
11161 entry = ctl_validate_command(ctsio);
11162 if (entry == NULL) {
11164 mtx_unlock(&lun->lun_lock);
11168 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11169 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11172 * Check to see whether we can send this command to LUNs that don't
11173 * exist. This should pretty much only be the case for inquiry
11174 * and request sense. Further checks, below, really require having
11175 * a LUN, so we can't really check the command anymore. Just put
11176 * it on the rtr queue.
11179 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) {
11180 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11181 ctl_enqueue_rtr((union ctl_io *)ctsio);
11185 ctl_set_unsupported_lun(ctsio);
11186 ctl_done((union ctl_io *)ctsio);
11187 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11191 * Make sure we support this particular command on this LUN.
11192 * e.g., we don't support writes to the control LUN.
11194 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11195 mtx_unlock(&lun->lun_lock);
11196 ctl_set_invalid_opcode(ctsio);
11197 ctl_done((union ctl_io *)ctsio);
11202 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11206 * If we've got a request sense, it'll clear the contingent
11207 * allegiance condition. Otherwise, if we have a CA condition for
11208 * this initiator, clear it, because it sent down a command other
11209 * than request sense.
11211 if ((ctsio->cdb[0] != REQUEST_SENSE)
11212 && (ctl_is_set(lun->have_ca, initidx)))
11213 ctl_clear_mask(lun->have_ca, initidx);
11217 * If the command has this flag set, it handles its own unit
11218 * attention reporting, we shouldn't do anything. Otherwise we
11219 * check for any pending unit attentions, and send them back to the
11220 * initiator. We only do this when a command initially comes in,
11221 * not when we pull it off the blocked queue.
11223 * According to SAM-3, section 5.3.2, the order that things get
11224 * presented back to the host is basically unit attentions caused
11225 * by some sort of reset event, busy status, reservation conflicts
11226 * or task set full, and finally any other status.
11228 * One issue here is that some of the unit attentions we report
11229 * don't fall into the "reset" category (e.g. "reported luns data
11230 * has changed"). So reporting it here, before the reservation
11231 * check, may be technically wrong. I guess the only thing to do
11232 * would be to check for and report the reset events here, and then
11233 * check for the other unit attention types after we check for a
11234 * reservation conflict.
11236 * XXX KDM need to fix this
11238 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11239 ctl_ua_type ua_type;
11241 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11243 if (ua_type != CTL_UA_NONE) {
11244 mtx_unlock(&lun->lun_lock);
11245 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11246 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11247 ctsio->sense_len = SSD_FULL_SIZE;
11248 ctl_done((union ctl_io *)ctsio);
11254 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11255 mtx_unlock(&lun->lun_lock);
11256 ctl_done((union ctl_io *)ctsio);
11261 * XXX CHD this is where we want to send IO to other side if
11262 * this LUN is secondary on this SC. We will need to make a copy
11263 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11264 * the copy we send as FROM_OTHER.
11265 * We also need to stuff the address of the original IO so we can
11266 * find it easily. Something similar will need be done on the other
11267 * side so when we are done we can find the copy.
11269 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
11270 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 &&
11271 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) {
11272 union ctl_ha_msg msg_info;
11275 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11276 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11277 mtx_unlock(&lun->lun_lock);
11279 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11280 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11281 msg_info.hdr.serializing_sc = NULL;
11282 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11283 msg_info.scsi.tag_num = ctsio->tag_num;
11284 msg_info.scsi.tag_type = ctsio->tag_type;
11285 msg_info.scsi.cdb_len = ctsio->cdb_len;
11286 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11288 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11289 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data),
11290 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) {
11291 ctl_set_busy(ctsio);
11292 ctl_done((union ctl_io *)ctsio);
11298 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11299 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11300 ctl_ooaq, ooa_links))) {
11301 case CTL_ACTION_BLOCK:
11302 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11303 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11305 mtx_unlock(&lun->lun_lock);
11307 case CTL_ACTION_PASS:
11308 case CTL_ACTION_SKIP:
11309 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11310 mtx_unlock(&lun->lun_lock);
11311 ctl_enqueue_rtr((union ctl_io *)ctsio);
11313 case CTL_ACTION_OVERLAP:
11314 mtx_unlock(&lun->lun_lock);
11315 ctl_set_overlapped_cmd(ctsio);
11316 ctl_done((union ctl_io *)ctsio);
11318 case CTL_ACTION_OVERLAP_TAG:
11319 mtx_unlock(&lun->lun_lock);
11320 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11321 ctl_done((union ctl_io *)ctsio);
11323 case CTL_ACTION_ERROR:
11325 mtx_unlock(&lun->lun_lock);
11326 ctl_set_internal_failure(ctsio,
11328 /*retry_count*/ 0);
11329 ctl_done((union ctl_io *)ctsio);
11335 const struct ctl_cmd_entry *
11336 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11338 const struct ctl_cmd_entry *entry;
11339 int service_action;
11341 entry = &ctl_cmd_table[ctsio->cdb[0]];
11343 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11344 if (entry->flags & CTL_CMD_FLAG_SA5) {
11345 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11346 entry = &((const struct ctl_cmd_entry *)
11347 entry->execute)[service_action];
11352 const struct ctl_cmd_entry *
11353 ctl_validate_command(struct ctl_scsiio *ctsio)
11355 const struct ctl_cmd_entry *entry;
11359 entry = ctl_get_cmd_entry(ctsio, &sa);
11360 if (entry->execute == NULL) {
11362 ctl_set_invalid_field(ctsio,
11369 ctl_set_invalid_opcode(ctsio);
11370 ctl_done((union ctl_io *)ctsio);
11373 KASSERT(entry->length > 0,
11374 ("Not defined length for command 0x%02x/0x%02x",
11375 ctsio->cdb[0], ctsio->cdb[1]));
11376 for (i = 1; i < entry->length; i++) {
11377 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11380 ctl_set_invalid_field(ctsio,
11385 /*bit*/ fls(diff) - 1);
11386 ctl_done((union ctl_io *)ctsio);
11393 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11396 switch (lun_type) {
11398 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
11402 if ((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0)
11412 ctl_scsiio(struct ctl_scsiio *ctsio)
11415 const struct ctl_cmd_entry *entry;
11417 retval = CTL_RETVAL_COMPLETE;
11419 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11421 entry = ctl_get_cmd_entry(ctsio, NULL);
11424 * If this I/O has been aborted, just send it straight to
11425 * ctl_done() without executing it.
11427 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11428 ctl_done((union ctl_io *)ctsio);
11433 * All the checks should have been handled by ctl_scsiio_precheck().
11434 * We should be clear now to just execute the I/O.
11436 retval = entry->execute(ctsio);
11443 * Since we only implement one target right now, a bus reset simply resets
11444 * our single target.
11447 ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io)
11449 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET));
11453 ctl_target_reset(struct ctl_softc *softc, union ctl_io *io,
11454 ctl_ua_type ua_type)
11456 struct ctl_port *port;
11457 struct ctl_lun *lun;
11460 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11461 union ctl_ha_msg msg_info;
11463 msg_info.hdr.nexus = io->io_hdr.nexus;
11464 if (ua_type==CTL_UA_TARG_RESET)
11465 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11467 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11468 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11469 msg_info.hdr.original_sc = NULL;
11470 msg_info.hdr.serializing_sc = NULL;
11471 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11472 sizeof(msg_info.task), M_WAITOK);
11476 mtx_lock(&softc->ctl_lock);
11477 port = softc->ctl_ports[io->io_hdr.nexus.targ_port];
11478 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11479 if (port != NULL &&
11480 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
11482 retval += ctl_do_lun_reset(lun, io, ua_type);
11484 mtx_unlock(&softc->ctl_lock);
11485 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11490 * The LUN should always be set. The I/O is optional, and is used to
11491 * distinguish between I/Os sent by this initiator, and by other
11492 * initiators. We set unit attention for initiators other than this one.
11493 * SAM-3 is vague on this point. It does say that a unit attention should
11494 * be established for other initiators when a LUN is reset (see section
11495 * 5.7.3), but it doesn't specifically say that the unit attention should
11496 * be established for this particular initiator when a LUN is reset. Here
11497 * is the relevant text, from SAM-3 rev 8:
11499 * 5.7.2 When a SCSI initiator port aborts its own tasks
11501 * When a SCSI initiator port causes its own task(s) to be aborted, no
11502 * notification that the task(s) have been aborted shall be returned to
11503 * the SCSI initiator port other than the completion response for the
11504 * command or task management function action that caused the task(s) to
11505 * be aborted and notification(s) associated with related effects of the
11506 * action (e.g., a reset unit attention condition).
11508 * XXX KDM for now, we're setting unit attention for all initiators.
11511 ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11521 mtx_lock(&lun->lun_lock);
11523 * Run through the OOA queue and abort each I/O.
11525 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11526 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11527 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11531 * This version sets unit attention for every
11534 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11535 ctl_est_ua_all(lun, initidx, ua_type);
11537 ctl_est_ua_all(lun, -1, ua_type);
11541 * A reset (any kind, really) clears reservations established with
11542 * RESERVE/RELEASE. It does not clear reservations established
11543 * with PERSISTENT RESERVE OUT, but we don't support that at the
11544 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
11545 * reservations made with the RESERVE/RELEASE commands, because
11546 * those commands are obsolete in SPC-3.
11548 lun->flags &= ~CTL_LUN_RESERVED;
11551 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11552 ctl_clear_mask(lun->have_ca, i);
11554 mtx_unlock(&lun->lun_lock);
11560 ctl_lun_reset(struct ctl_softc *softc, union ctl_io *io)
11562 struct ctl_lun *lun;
11566 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11567 mtx_lock(&softc->ctl_lock);
11568 if ((targ_lun >= CTL_MAX_LUNS) ||
11569 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11570 mtx_unlock(&softc->ctl_lock);
11571 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11574 retval = ctl_do_lun_reset(lun, io, CTL_UA_LUN_RESET);
11575 mtx_unlock(&softc->ctl_lock);
11576 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11578 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) {
11579 union ctl_ha_msg msg_info;
11581 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11582 msg_info.hdr.nexus = io->io_hdr.nexus;
11583 msg_info.task.task_action = CTL_TASK_LUN_RESET;
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);
11593 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11598 mtx_assert(&lun->lun_lock, MA_OWNED);
11601 * Run through the OOA queue and attempt to find the given I/O.
11602 * The target port, initiator ID, tag type and tag number have to
11603 * match the values that we got from the initiator. If we have an
11604 * untagged command to abort, simply abort the first untagged command
11605 * we come to. We only allow one untagged command at a time of course.
11607 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11608 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11610 if ((targ_port == UINT32_MAX ||
11611 targ_port == xio->io_hdr.nexus.targ_port) &&
11612 (init_id == UINT32_MAX ||
11613 init_id == xio->io_hdr.nexus.initid)) {
11614 if (targ_port != xio->io_hdr.nexus.targ_port ||
11615 init_id != xio->io_hdr.nexus.initid)
11616 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
11617 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11618 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11619 union ctl_ha_msg msg_info;
11621 msg_info.hdr.nexus = xio->io_hdr.nexus;
11622 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11623 msg_info.task.tag_num = xio->scsiio.tag_num;
11624 msg_info.task.tag_type = xio->scsiio.tag_type;
11625 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11626 msg_info.hdr.original_sc = NULL;
11627 msg_info.hdr.serializing_sc = NULL;
11628 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11629 sizeof(msg_info.task), M_NOWAIT);
11636 ctl_abort_task_set(union ctl_io *io)
11638 struct ctl_softc *softc = control_softc;
11639 struct ctl_lun *lun;
11645 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11646 mtx_lock(&softc->ctl_lock);
11647 if ((targ_lun >= CTL_MAX_LUNS) ||
11648 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11649 mtx_unlock(&softc->ctl_lock);
11650 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11654 mtx_lock(&lun->lun_lock);
11655 mtx_unlock(&softc->ctl_lock);
11656 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
11657 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11658 io->io_hdr.nexus.initid,
11659 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11660 } else { /* CTL_TASK_CLEAR_TASK_SET */
11661 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
11662 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11664 mtx_unlock(&lun->lun_lock);
11665 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11670 ctl_i_t_nexus_reset(union ctl_io *io)
11672 struct ctl_softc *softc = control_softc;
11673 struct ctl_lun *lun;
11676 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11677 union ctl_ha_msg msg_info;
11679 msg_info.hdr.nexus = io->io_hdr.nexus;
11680 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET;
11681 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11682 msg_info.hdr.original_sc = NULL;
11683 msg_info.hdr.serializing_sc = NULL;
11684 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11685 sizeof(msg_info.task), M_WAITOK);
11688 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11689 mtx_lock(&softc->ctl_lock);
11690 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11691 mtx_lock(&lun->lun_lock);
11692 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11693 io->io_hdr.nexus.initid, 1);
11695 ctl_clear_mask(lun->have_ca, initidx);
11697 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx))
11698 lun->flags &= ~CTL_LUN_RESERVED;
11699 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS);
11700 mtx_unlock(&lun->lun_lock);
11702 mtx_unlock(&softc->ctl_lock);
11703 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11708 ctl_abort_task(union ctl_io *io)
11711 struct ctl_lun *lun;
11712 struct ctl_softc *softc;
11715 char printbuf[128];
11720 softc = control_softc;
11726 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11727 mtx_lock(&softc->ctl_lock);
11728 if ((targ_lun >= CTL_MAX_LUNS) ||
11729 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11730 mtx_unlock(&softc->ctl_lock);
11731 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11736 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
11737 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
11740 mtx_lock(&lun->lun_lock);
11741 mtx_unlock(&softc->ctl_lock);
11743 * Run through the OOA queue and attempt to find the given I/O.
11744 * The target port, initiator ID, tag type and tag number have to
11745 * match the values that we got from the initiator. If we have an
11746 * untagged command to abort, simply abort the first untagged command
11747 * we come to. We only allow one untagged command at a time of course.
11749 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11750 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11752 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
11754 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
11755 lun->lun, xio->scsiio.tag_num,
11756 xio->scsiio.tag_type,
11757 (xio->io_hdr.blocked_links.tqe_prev
11758 == NULL) ? "" : " BLOCKED",
11759 (xio->io_hdr.flags &
11760 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
11761 (xio->io_hdr.flags &
11762 CTL_FLAG_ABORT) ? " ABORT" : "",
11763 (xio->io_hdr.flags &
11764 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
11765 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
11767 printf("%s\n", sbuf_data(&sb));
11770 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
11771 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
11772 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
11776 * If the abort says that the task is untagged, the
11777 * task in the queue must be untagged. Otherwise,
11778 * we just check to see whether the tag numbers
11779 * match. This is because the QLogic firmware
11780 * doesn't pass back the tag type in an abort
11784 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
11785 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
11786 || (xio->scsiio.tag_num == io->taskio.tag_num))
11789 * XXX KDM we've got problems with FC, because it
11790 * doesn't send down a tag type with aborts. So we
11791 * can only really go by the tag number...
11792 * This may cause problems with parallel SCSI.
11793 * Need to figure that out!!
11795 if (xio->scsiio.tag_num == io->taskio.tag_num) {
11796 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11798 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
11799 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11800 union ctl_ha_msg msg_info;
11802 msg_info.hdr.nexus = io->io_hdr.nexus;
11803 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11804 msg_info.task.tag_num = io->taskio.tag_num;
11805 msg_info.task.tag_type = io->taskio.tag_type;
11806 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11807 msg_info.hdr.original_sc = NULL;
11808 msg_info.hdr.serializing_sc = NULL;
11810 printf("Sent Abort to other side\n");
11812 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11813 sizeof(msg_info.task), M_NOWAIT);
11816 printf("ctl_abort_task: found I/O to abort\n");
11820 mtx_unlock(&lun->lun_lock);
11824 * This isn't really an error. It's entirely possible for
11825 * the abort and command completion to cross on the wire.
11826 * This is more of an informative/diagnostic error.
11829 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
11830 "%u:%u:%u tag %d type %d\n",
11831 io->io_hdr.nexus.initid,
11832 io->io_hdr.nexus.targ_port,
11833 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
11834 io->taskio.tag_type);
11837 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11842 ctl_query_task(union ctl_io *io, int task_set)
11845 struct ctl_lun *lun;
11846 struct ctl_softc *softc;
11850 softc = control_softc;
11851 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11852 mtx_lock(&softc->ctl_lock);
11853 if ((targ_lun >= CTL_MAX_LUNS) ||
11854 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11855 mtx_unlock(&softc->ctl_lock);
11856 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11859 mtx_lock(&lun->lun_lock);
11860 mtx_unlock(&softc->ctl_lock);
11861 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11862 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11864 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
11865 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
11866 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
11869 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) {
11874 mtx_unlock(&lun->lun_lock);
11876 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
11878 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11883 ctl_query_async_event(union ctl_io *io)
11885 struct ctl_lun *lun;
11886 struct ctl_softc *softc;
11888 uint32_t targ_lun, initidx;
11890 softc = control_softc;
11891 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11892 mtx_lock(&softc->ctl_lock);
11893 if ((targ_lun >= CTL_MAX_LUNS) ||
11894 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11895 mtx_unlock(&softc->ctl_lock);
11896 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11899 mtx_lock(&lun->lun_lock);
11900 mtx_unlock(&softc->ctl_lock);
11901 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11902 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp);
11903 mtx_unlock(&lun->lun_lock);
11904 if (ua != CTL_UA_NONE)
11905 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
11907 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11912 ctl_run_task(union ctl_io *io)
11914 struct ctl_softc *softc = control_softc;
11917 CTL_DEBUG_PRINT(("ctl_run_task\n"));
11918 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
11919 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type));
11920 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED;
11921 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp));
11922 switch (io->taskio.task_action) {
11923 case CTL_TASK_ABORT_TASK:
11924 retval = ctl_abort_task(io);
11926 case CTL_TASK_ABORT_TASK_SET:
11927 case CTL_TASK_CLEAR_TASK_SET:
11928 retval = ctl_abort_task_set(io);
11930 case CTL_TASK_CLEAR_ACA:
11932 case CTL_TASK_I_T_NEXUS_RESET:
11933 retval = ctl_i_t_nexus_reset(io);
11935 case CTL_TASK_LUN_RESET:
11936 retval = ctl_lun_reset(softc, io);
11938 case CTL_TASK_TARGET_RESET:
11939 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET);
11941 case CTL_TASK_BUS_RESET:
11942 retval = ctl_bus_reset(softc, io);
11944 case CTL_TASK_PORT_LOGIN:
11946 case CTL_TASK_PORT_LOGOUT:
11948 case CTL_TASK_QUERY_TASK:
11949 retval = ctl_query_task(io, 0);
11951 case CTL_TASK_QUERY_TASK_SET:
11952 retval = ctl_query_task(io, 1);
11954 case CTL_TASK_QUERY_ASYNC_EVENT:
11955 retval = ctl_query_async_event(io);
11958 printf("%s: got unknown task management event %d\n",
11959 __func__, io->taskio.task_action);
11963 io->io_hdr.status = CTL_SUCCESS;
11965 io->io_hdr.status = CTL_ERROR;
11970 * For HA operation. Handle commands that come in from the other
11974 ctl_handle_isc(union ctl_io *io)
11977 struct ctl_lun *lun;
11978 struct ctl_softc *softc;
11981 softc = control_softc;
11983 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11984 lun = softc->ctl_luns[targ_lun];
11986 switch (io->io_hdr.msg_type) {
11987 case CTL_MSG_SERIALIZE:
11988 free_io = ctl_serialize_other_sc_cmd(&io->scsiio);
11990 case CTL_MSG_R2R: {
11991 const struct ctl_cmd_entry *entry;
11994 * This is only used in SER_ONLY mode.
11997 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
11998 mtx_lock(&lun->lun_lock);
11999 if (ctl_scsiio_lun_check(lun,
12000 entry, (struct ctl_scsiio *)io) != 0) {
12001 mtx_unlock(&lun->lun_lock);
12005 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12006 mtx_unlock(&lun->lun_lock);
12007 ctl_enqueue_rtr(io);
12010 case CTL_MSG_FINISH_IO:
12011 if (softc->ha_mode == CTL_HA_MODE_XFER) {
12016 mtx_lock(&lun->lun_lock);
12017 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
12019 ctl_check_blocked(lun);
12020 mtx_unlock(&lun->lun_lock);
12023 case CTL_MSG_PERS_ACTION:
12024 ctl_hndl_per_res_out_on_other_sc(
12025 (union ctl_ha_msg *)&io->presio.pr_msg);
12028 case CTL_MSG_BAD_JUJU:
12032 case CTL_MSG_DATAMOVE:
12033 /* Only used in XFER mode */
12035 ctl_datamove_remote(io);
12037 case CTL_MSG_DATAMOVE_DONE:
12038 /* Only used in XFER mode */
12040 io->scsiio.be_move_done(io);
12042 case CTL_MSG_FAILOVER:
12043 mtx_lock(&lun->lun_lock);
12044 ctl_failover_lun(lun);
12045 mtx_unlock(&lun->lun_lock);
12050 printf("%s: Invalid message type %d\n",
12051 __func__, io->io_hdr.msg_type);
12061 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12062 * there is no match.
12064 static ctl_lun_error_pattern
12065 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12067 const struct ctl_cmd_entry *entry;
12068 ctl_lun_error_pattern filtered_pattern, pattern;
12070 pattern = desc->error_pattern;
12073 * XXX KDM we need more data passed into this function to match a
12074 * custom pattern, and we actually need to implement custom pattern
12077 if (pattern & CTL_LUN_PAT_CMD)
12078 return (CTL_LUN_PAT_CMD);
12080 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12081 return (CTL_LUN_PAT_ANY);
12083 entry = ctl_get_cmd_entry(ctsio, NULL);
12085 filtered_pattern = entry->pattern & pattern;
12088 * If the user requested specific flags in the pattern (e.g.
12089 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12092 * If the user did not specify any flags, it doesn't matter whether
12093 * or not the command supports the flags.
12095 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12096 (pattern & ~CTL_LUN_PAT_MASK))
12097 return (CTL_LUN_PAT_NONE);
12100 * If the user asked for a range check, see if the requested LBA
12101 * range overlaps with this command's LBA range.
12103 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12109 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12111 return (CTL_LUN_PAT_NONE);
12113 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12114 desc->lba_range.len, FALSE);
12116 * A "pass" means that the LBA ranges don't overlap, so
12117 * this doesn't match the user's range criteria.
12119 if (action == CTL_ACTION_PASS)
12120 return (CTL_LUN_PAT_NONE);
12123 return (filtered_pattern);
12127 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12129 struct ctl_error_desc *desc, *desc2;
12131 mtx_assert(&lun->lun_lock, MA_OWNED);
12133 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12134 ctl_lun_error_pattern pattern;
12136 * Check to see whether this particular command matches
12137 * the pattern in the descriptor.
12139 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12140 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12143 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12144 case CTL_LUN_INJ_ABORTED:
12145 ctl_set_aborted(&io->scsiio);
12147 case CTL_LUN_INJ_MEDIUM_ERR:
12148 ctl_set_medium_error(&io->scsiio,
12149 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) !=
12150 CTL_FLAG_DATA_OUT);
12152 case CTL_LUN_INJ_UA:
12153 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12155 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12157 case CTL_LUN_INJ_CUSTOM:
12159 * We're assuming the user knows what he is doing.
12160 * Just copy the sense information without doing
12163 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12164 MIN(sizeof(desc->custom_sense),
12165 sizeof(io->scsiio.sense_data)));
12166 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12167 io->scsiio.sense_len = SSD_FULL_SIZE;
12168 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12170 case CTL_LUN_INJ_NONE:
12173 * If this is an error injection type we don't know
12174 * about, clear the continuous flag (if it is set)
12175 * so it will get deleted below.
12177 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12181 * By default, each error injection action is a one-shot
12183 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12186 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12192 #ifdef CTL_IO_DELAY
12194 ctl_datamove_timer_wakeup(void *arg)
12198 io = (union ctl_io *)arg;
12202 #endif /* CTL_IO_DELAY */
12205 ctl_datamove(union ctl_io *io)
12207 struct ctl_lun *lun;
12208 void (*fe_datamove)(union ctl_io *io);
12210 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12212 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12214 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12216 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12221 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12222 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12224 sbuf_cat(&sb, path_str);
12225 switch (io->io_hdr.io_type) {
12227 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12228 sbuf_printf(&sb, "\n");
12229 sbuf_cat(&sb, path_str);
12230 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12231 io->scsiio.tag_num, io->scsiio.tag_type);
12234 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12235 "Tag Type: %d\n", io->taskio.task_action,
12236 io->taskio.tag_num, io->taskio.tag_type);
12239 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12240 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12243 sbuf_cat(&sb, path_str);
12244 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12245 (intmax_t)time_uptime - io->io_hdr.start_time);
12247 printf("%s", sbuf_data(&sb));
12249 #endif /* CTL_TIME_IO */
12251 #ifdef CTL_IO_DELAY
12252 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12253 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12256 && (lun->delay_info.datamove_delay > 0)) {
12258 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
12259 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12260 callout_reset(&io->io_hdr.delay_callout,
12261 lun->delay_info.datamove_delay * hz,
12262 ctl_datamove_timer_wakeup, io);
12263 if (lun->delay_info.datamove_type ==
12264 CTL_DELAY_TYPE_ONESHOT)
12265 lun->delay_info.datamove_delay = 0;
12272 * This command has been aborted. Set the port status, so we fail
12275 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12276 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n",
12277 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12278 io->io_hdr.nexus.targ_port,
12279 io->io_hdr.nexus.targ_lun);
12280 io->io_hdr.port_status = 31337;
12282 * Note that the backend, in this case, will get the
12283 * callback in its context. In other cases it may get
12284 * called in the frontend's interrupt thread context.
12286 io->scsiio.be_move_done(io);
12290 /* Don't confuse frontend with zero length data move. */
12291 if (io->scsiio.kern_data_len == 0) {
12292 io->scsiio.be_move_done(io);
12297 * If we're in XFER mode and this I/O is from the other shelf
12298 * controller, we need to send the DMA to the other side to
12299 * actually transfer the data to/from the host. In serialize only
12300 * mode the transfer happens below CTL and ctl_datamove() is only
12301 * called on the machine that originally received the I/O.
12303 if ((control_softc->ha_mode == CTL_HA_MODE_XFER)
12304 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12305 union ctl_ha_msg msg;
12306 uint32_t sg_entries_sent;
12310 memset(&msg, 0, sizeof(msg));
12311 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
12312 msg.hdr.original_sc = io->io_hdr.original_sc;
12313 msg.hdr.serializing_sc = io;
12314 msg.hdr.nexus = io->io_hdr.nexus;
12315 msg.hdr.status = io->io_hdr.status;
12316 msg.dt.flags = io->io_hdr.flags;
12318 * We convert everything into a S/G list here. We can't
12319 * pass by reference, only by value between controllers.
12320 * So we can't pass a pointer to the S/G list, only as many
12321 * S/G entries as we can fit in here. If it's possible for
12322 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
12323 * then we need to break this up into multiple transfers.
12325 if (io->scsiio.kern_sg_entries == 0) {
12326 msg.dt.kern_sg_entries = 1;
12329 * Convert to a physical address if this is a
12332 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
12333 msg.dt.sg_list[0].addr =
12334 io->scsiio.kern_data_ptr;
12337 * XXX KDM use busdma here!
12339 msg.dt.sg_list[0].addr = (void *)
12340 vtophys(io->scsiio.kern_data_ptr);
12343 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
12344 ("HA does not support BUS_ADDR"));
12345 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
12348 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
12351 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
12355 msg.dt.kern_data_len = io->scsiio.kern_data_len;
12356 msg.dt.kern_total_len = io->scsiio.kern_total_len;
12357 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
12358 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
12359 msg.dt.sg_sequence = 0;
12362 * Loop until we've sent all of the S/G entries. On the
12363 * other end, we'll recompose these S/G entries into one
12364 * contiguous list before passing it to the
12366 for (sg_entries_sent = 0; sg_entries_sent <
12367 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) {
12368 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list)/
12369 sizeof(msg.dt.sg_list[0])),
12370 msg.dt.kern_sg_entries - sg_entries_sent);
12372 if (do_sg_copy != 0) {
12373 struct ctl_sg_entry *sgl;
12376 sgl = (struct ctl_sg_entry *)
12377 io->scsiio.kern_data_ptr;
12379 * If this is in cached memory, flush the cache
12380 * before we send the DMA request to the other
12381 * controller. We want to do this in either
12382 * the * read or the write case. The read
12383 * case is straightforward. In the write
12384 * case, we want to make sure nothing is
12385 * in the local cache that could overwrite
12389 for (i = sg_entries_sent, j = 0;
12390 i < msg.dt.cur_sg_entries; i++, j++) {
12392 if ((io->io_hdr.flags &
12393 CTL_FLAG_BUS_ADDR) == 0) {
12395 * XXX KDM use busdma.
12397 msg.dt.sg_list[j].addr =(void *)
12398 vtophys(sgl[i].addr);
12400 msg.dt.sg_list[j].addr =
12404 KASSERT((io->io_hdr.flags &
12405 CTL_FLAG_BUS_ADDR) == 0,
12406 ("HA does not support BUS_ADDR"));
12407 msg.dt.sg_list[j].addr = sgl[i].addr;
12409 msg.dt.sg_list[j].len = sgl[i].len;
12413 sg_entries_sent += msg.dt.cur_sg_entries;
12414 if (sg_entries_sent >= msg.dt.kern_sg_entries)
12415 msg.dt.sg_last = 1;
12417 msg.dt.sg_last = 0;
12419 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12420 sizeof(msg.dt) - sizeof(msg.dt.sg_list) +
12421 sizeof(struct ctl_sg_entry)*msg.dt.cur_sg_entries,
12422 M_WAITOK) > CTL_HA_STATUS_SUCCESS) {
12423 io->io_hdr.port_status = 31341;
12424 io->scsiio.be_move_done(io);
12428 msg.dt.sent_sg_entries = sg_entries_sent;
12432 * Officially handover the request from us to peer.
12433 * If failover has just happened, then we must return error.
12434 * If failover happen just after, then it is not our problem.
12437 mtx_lock(&lun->lun_lock);
12438 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12440 mtx_unlock(&lun->lun_lock);
12441 io->io_hdr.port_status = 31342;
12442 io->scsiio.be_move_done(io);
12445 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12446 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
12448 mtx_unlock(&lun->lun_lock);
12452 * Lookup the fe_datamove() function for this particular
12455 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12462 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12464 union ctl_ha_msg msg;
12466 memset(&msg, 0, sizeof(msg));
12468 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12469 msg.hdr.original_sc = io;
12470 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12471 msg.hdr.nexus = io->io_hdr.nexus;
12472 msg.hdr.status = io->io_hdr.status;
12473 msg.scsi.tag_num = io->scsiio.tag_num;
12474 msg.scsi.tag_type = io->scsiio.tag_type;
12475 msg.scsi.scsi_status = io->scsiio.scsi_status;
12476 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12477 io->scsiio.sense_len);
12478 msg.scsi.sense_len = io->scsiio.sense_len;
12479 msg.scsi.sense_residual = io->scsiio.sense_residual;
12480 msg.scsi.fetd_status = io->io_hdr.port_status;
12481 msg.scsi.residual = io->scsiio.residual;
12482 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12484 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12485 ctl_failover_io(io, /*have_lock*/ have_lock);
12489 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12490 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12491 msg.scsi.sense_len, M_WAITOK);
12495 * The DMA to the remote side is done, now we need to tell the other side
12496 * we're done so it can continue with its data movement.
12499 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12506 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12507 printf("%s: ISC DMA write failed with error %d", __func__,
12509 ctl_set_internal_failure(&io->scsiio,
12511 /*retry_count*/ rq->ret);
12514 ctl_dt_req_free(rq);
12516 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12517 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12518 free(io->io_hdr.remote_sglist, M_CTL);
12519 io->io_hdr.remote_sglist = NULL;
12520 io->io_hdr.local_sglist = NULL;
12523 * The data is in local and remote memory, so now we need to send
12524 * status (good or back) back to the other side.
12526 ctl_send_datamove_done(io, /*have_lock*/ 0);
12530 * We've moved the data from the host/controller into local memory. Now we
12531 * need to push it over to the remote controller's memory.
12534 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12538 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12539 ctl_datamove_remote_write_cb);
12544 ctl_datamove_remote_write(union ctl_io *io)
12547 void (*fe_datamove)(union ctl_io *io);
12550 * - Get the data from the host/HBA into local memory.
12551 * - DMA memory from the local controller to the remote controller.
12552 * - Send status back to the remote controller.
12555 retval = ctl_datamove_remote_sgl_setup(io);
12559 /* Switch the pointer over so the FETD knows what to do */
12560 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12563 * Use a custom move done callback, since we need to send completion
12564 * back to the other controller, not to the backend on this side.
12566 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12568 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12573 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12582 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12583 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12584 free(io->io_hdr.remote_sglist, M_CTL);
12585 io->io_hdr.remote_sglist = NULL;
12586 io->io_hdr.local_sglist = NULL;
12589 scsi_path_string(io, path_str, sizeof(path_str));
12590 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12591 sbuf_cat(&sb, path_str);
12592 scsi_command_string(&io->scsiio, NULL, &sb);
12593 sbuf_printf(&sb, "\n");
12594 sbuf_cat(&sb, path_str);
12595 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12596 io->scsiio.tag_num, io->scsiio.tag_type);
12597 sbuf_cat(&sb, path_str);
12598 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12599 io->io_hdr.flags, io->io_hdr.status);
12601 printk("%s", sbuf_data(&sb));
12606 * The read is done, now we need to send status (good or bad) back
12607 * to the other side.
12609 ctl_send_datamove_done(io, /*have_lock*/ 0);
12615 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12618 void (*fe_datamove)(union ctl_io *io);
12622 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12623 printf("%s: ISC DMA read failed with error %d\n", __func__,
12625 ctl_set_internal_failure(&io->scsiio,
12627 /*retry_count*/ rq->ret);
12630 ctl_dt_req_free(rq);
12632 /* Switch the pointer over so the FETD knows what to do */
12633 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12636 * Use a custom move done callback, since we need to send completion
12637 * back to the other controller, not to the backend on this side.
12639 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12641 /* XXX KDM add checks like the ones in ctl_datamove? */
12643 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12648 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12650 struct ctl_sg_entry *local_sglist;
12651 struct ctl_softc *softc;
12652 uint32_t len_to_go;
12657 softc = control_softc;
12658 local_sglist = io->io_hdr.local_sglist;
12659 len_to_go = io->scsiio.kern_data_len;
12662 * The difficult thing here is that the size of the various
12663 * S/G segments may be different than the size from the
12664 * remote controller. That'll make it harder when DMAing
12665 * the data back to the other side.
12667 for (i = 0; len_to_go > 0; i++) {
12668 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT);
12669 local_sglist[i].addr =
12670 malloc(local_sglist[i].len, M_CTL, M_WAITOK);
12672 len_to_go -= local_sglist[i].len;
12675 * Reset the number of S/G entries accordingly. The original
12676 * number of S/G entries is available in rem_sg_entries.
12678 io->scsiio.kern_sg_entries = i;
12681 printf("%s: kern_sg_entries = %d\n", __func__,
12682 io->scsiio.kern_sg_entries);
12683 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12684 printf("%s: sg[%d] = %p, %lu\n", __func__, i,
12685 local_sglist[i].addr, local_sglist[i].len);
12692 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12693 ctl_ha_dt_cb callback)
12695 struct ctl_ha_dt_req *rq;
12696 struct ctl_sg_entry *remote_sglist, *local_sglist;
12697 uint32_t local_used, remote_used, total_used;
12700 rq = ctl_dt_req_alloc();
12703 * If we failed to allocate the request, and if the DMA didn't fail
12704 * anyway, set busy status. This is just a resource allocation
12708 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12709 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS))
12710 ctl_set_busy(&io->scsiio);
12712 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12713 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
12716 ctl_dt_req_free(rq);
12719 * The data move failed. We need to return status back
12720 * to the other controller. No point in trying to DMA
12721 * data to the remote controller.
12724 ctl_send_datamove_done(io, /*have_lock*/ 0);
12729 local_sglist = io->io_hdr.local_sglist;
12730 remote_sglist = io->io_hdr.remote_sglist;
12736 * Pull/push the data over the wire from/to the other controller.
12737 * This takes into account the possibility that the local and
12738 * remote sglists may not be identical in terms of the size of
12739 * the elements and the number of elements.
12741 * One fundamental assumption here is that the length allocated for
12742 * both the local and remote sglists is identical. Otherwise, we've
12743 * essentially got a coding error of some sort.
12745 isc_ret = CTL_HA_STATUS_SUCCESS;
12746 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12750 rq->command = command;
12754 * Both pointers should be aligned. But it is possible
12755 * that the allocation length is not. They should both
12756 * also have enough slack left over at the end, though,
12757 * to round up to the next 8 byte boundary.
12759 cur_len = MIN(local_sglist[i].len - local_used,
12760 remote_sglist[j].len - remote_used);
12761 rq->size = cur_len;
12763 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12764 tmp_ptr += local_used;
12767 /* Use physical addresses when talking to ISC hardware */
12768 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12769 /* XXX KDM use busdma */
12770 rq->local = vtophys(tmp_ptr);
12772 rq->local = tmp_ptr;
12774 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
12775 ("HA does not support BUS_ADDR"));
12776 rq->local = tmp_ptr;
12779 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12780 tmp_ptr += remote_used;
12781 rq->remote = tmp_ptr;
12783 rq->callback = NULL;
12785 local_used += cur_len;
12786 if (local_used >= local_sglist[i].len) {
12791 remote_used += cur_len;
12792 if (remote_used >= remote_sglist[j].len) {
12796 total_used += cur_len;
12798 if (total_used >= io->scsiio.kern_data_len)
12799 rq->callback = callback;
12802 printf("%s: %s: local %p remote %p size %d\n", __func__,
12803 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
12804 rq->local, rq->remote, rq->size);
12807 isc_ret = ctl_dt_single(rq);
12808 if (isc_ret > CTL_HA_STATUS_SUCCESS)
12811 if (isc_ret != CTL_HA_STATUS_WAIT) {
12820 ctl_datamove_remote_read(union ctl_io *io)
12826 * This will send an error to the other controller in the case of a
12829 retval = ctl_datamove_remote_sgl_setup(io);
12833 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12834 ctl_datamove_remote_read_cb);
12837 * Make sure we free memory if there was an error.. The
12838 * ctl_datamove_remote_xfer() function will send the
12839 * datamove done message, or call the callback with an
12840 * error if there is a problem.
12842 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12843 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12844 free(io->io_hdr.remote_sglist, M_CTL);
12845 io->io_hdr.remote_sglist = NULL;
12846 io->io_hdr.local_sglist = NULL;
12851 * Process a datamove request from the other controller. This is used for
12852 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
12853 * first. Once that is complete, the data gets DMAed into the remote
12854 * controller's memory. For reads, we DMA from the remote controller's
12855 * memory into our memory first, and then move it out to the FETD.
12858 ctl_datamove_remote(union ctl_io *io)
12861 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12863 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12864 ctl_failover_io(io, /*have_lock*/ 0);
12869 * Note that we look for an aborted I/O here, but don't do some of
12870 * the other checks that ctl_datamove() normally does.
12871 * We don't need to run the datamove delay code, since that should
12872 * have been done if need be on the other controller.
12874 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12875 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__,
12876 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12877 io->io_hdr.nexus.targ_port,
12878 io->io_hdr.nexus.targ_lun);
12879 io->io_hdr.port_status = 31338;
12880 ctl_send_datamove_done(io, /*have_lock*/ 0);
12884 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
12885 ctl_datamove_remote_write(io);
12886 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
12887 ctl_datamove_remote_read(io);
12889 io->io_hdr.port_status = 31339;
12890 ctl_send_datamove_done(io, /*have_lock*/ 0);
12895 ctl_process_done(union ctl_io *io)
12897 struct ctl_lun *lun;
12898 struct ctl_softc *softc = control_softc;
12899 void (*fe_done)(union ctl_io *io);
12900 union ctl_ha_msg msg;
12901 uint32_t targ_port = io->io_hdr.nexus.targ_port;
12903 CTL_DEBUG_PRINT(("ctl_process_done\n"));
12905 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0)
12906 fe_done = softc->ctl_ports[targ_port]->fe_done;
12911 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12916 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12917 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12919 sbuf_cat(&sb, path_str);
12920 switch (io->io_hdr.io_type) {
12922 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12923 sbuf_printf(&sb, "\n");
12924 sbuf_cat(&sb, path_str);
12925 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12926 io->scsiio.tag_num, io->scsiio.tag_type);
12929 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12930 "Tag Type: %d\n", io->taskio.task_action,
12931 io->taskio.tag_num, io->taskio.tag_type);
12934 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12935 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12938 sbuf_cat(&sb, path_str);
12939 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
12940 (intmax_t)time_uptime - io->io_hdr.start_time);
12942 printf("%s", sbuf_data(&sb));
12944 #endif /* CTL_TIME_IO */
12946 switch (io->io_hdr.io_type) {
12950 if (ctl_debug & CTL_DEBUG_INFO)
12951 ctl_io_error_print(io, NULL);
12952 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
12956 return (CTL_RETVAL_COMPLETE);
12958 panic("ctl_process_done: invalid io type %d\n",
12959 io->io_hdr.io_type);
12960 break; /* NOTREACHED */
12963 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12965 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
12966 io->io_hdr.nexus.targ_mapped_lun));
12970 mtx_lock(&lun->lun_lock);
12973 * Check to see if we have any errors to inject here. We only
12974 * inject errors for commands that don't already have errors set.
12976 if ((STAILQ_FIRST(&lun->error_list) != NULL) &&
12977 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
12978 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
12979 ctl_inject_error(lun, io);
12982 * XXX KDM how do we treat commands that aren't completed
12985 * XXX KDM should we also track I/O latency?
12987 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
12988 io->io_hdr.io_type == CTL_IO_SCSI) {
12990 struct bintime cur_bt;
12994 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12996 type = CTL_STATS_READ;
12997 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12999 type = CTL_STATS_WRITE;
13001 type = CTL_STATS_NO_IO;
13003 lun->stats.ports[targ_port].bytes[type] +=
13004 io->scsiio.kern_total_len;
13005 lun->stats.ports[targ_port].operations[type]++;
13007 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
13008 &io->io_hdr.dma_bt);
13009 lun->stats.ports[targ_port].num_dmas[type] +=
13010 io->io_hdr.num_dmas;
13011 getbintime(&cur_bt);
13012 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
13013 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
13018 * Remove this from the OOA queue.
13020 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13022 if (TAILQ_EMPTY(&lun->ooa_queue))
13023 lun->last_busy = getsbinuptime();
13027 * Run through the blocked queue on this LUN and see if anything
13028 * has become unblocked, now that this transaction is done.
13030 ctl_check_blocked(lun);
13033 * If the LUN has been invalidated, free it if there is nothing
13034 * left on its OOA queue.
13036 if ((lun->flags & CTL_LUN_INVALID)
13037 && TAILQ_EMPTY(&lun->ooa_queue)) {
13038 mtx_unlock(&lun->lun_lock);
13039 mtx_lock(&softc->ctl_lock);
13041 mtx_unlock(&softc->ctl_lock);
13043 mtx_unlock(&lun->lun_lock);
13048 * If this command has been aborted, make sure we set the status
13049 * properly. The FETD is responsible for freeing the I/O and doing
13050 * whatever it needs to do to clean up its state.
13052 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13053 ctl_set_task_aborted(&io->scsiio);
13056 * If enabled, print command error status.
13058 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
13059 (ctl_debug & CTL_DEBUG_INFO) != 0)
13060 ctl_io_error_print(io, NULL);
13063 * Tell the FETD or the other shelf controller we're done with this
13064 * command. Note that only SCSI commands get to this point. Task
13065 * management commands are completed above.
13067 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
13068 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) {
13069 memset(&msg, 0, sizeof(msg));
13070 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13071 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
13072 msg.hdr.nexus = io->io_hdr.nexus;
13073 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13074 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data),
13077 if ((softc->ha_mode == CTL_HA_MODE_XFER)
13078 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
13079 memset(&msg, 0, sizeof(msg));
13080 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13081 msg.hdr.original_sc = io->io_hdr.original_sc;
13082 msg.hdr.nexus = io->io_hdr.nexus;
13083 msg.hdr.status = io->io_hdr.status;
13084 msg.scsi.scsi_status = io->scsiio.scsi_status;
13085 msg.scsi.tag_num = io->scsiio.tag_num;
13086 msg.scsi.tag_type = io->scsiio.tag_type;
13087 msg.scsi.sense_len = io->scsiio.sense_len;
13088 msg.scsi.sense_residual = io->scsiio.sense_residual;
13089 msg.scsi.residual = io->scsiio.residual;
13090 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
13091 io->scsiio.sense_len);
13093 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13094 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
13095 msg.scsi.sense_len, M_WAITOK);
13100 return (CTL_RETVAL_COMPLETE);
13105 * Front end should call this if it doesn't do autosense. When the request
13106 * sense comes back in from the initiator, we'll dequeue this and send it.
13109 ctl_queue_sense(union ctl_io *io)
13111 struct ctl_lun *lun;
13112 struct ctl_port *port;
13113 struct ctl_softc *softc;
13114 uint32_t initidx, targ_lun;
13116 softc = control_softc;
13118 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13121 * LUN lookup will likely move to the ctl_work_thread() once we
13122 * have our new queueing infrastructure (that doesn't put things on
13123 * a per-LUN queue initially). That is so that we can handle
13124 * things like an INQUIRY to a LUN that we don't have enabled. We
13125 * can't deal with that right now.
13127 mtx_lock(&softc->ctl_lock);
13130 * If we don't have a LUN for this, just toss the sense
13133 port = ctl_io_port(&ctsio->io_hdr);
13134 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13135 if ((targ_lun < CTL_MAX_LUNS)
13136 && (softc->ctl_luns[targ_lun] != NULL))
13137 lun = softc->ctl_luns[targ_lun];
13141 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13143 mtx_lock(&lun->lun_lock);
13145 * Already have CA set for this LUN...toss the sense information.
13147 if (ctl_is_set(lun->have_ca, initidx)) {
13148 mtx_unlock(&lun->lun_lock);
13152 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data,
13153 MIN(sizeof(lun->pending_sense[initidx]),
13154 sizeof(io->scsiio.sense_data)));
13155 ctl_set_mask(lun->have_ca, initidx);
13156 mtx_unlock(&lun->lun_lock);
13159 mtx_unlock(&softc->ctl_lock);
13163 return (CTL_RETVAL_COMPLETE);
13168 * Primary command inlet from frontend ports. All SCSI and task I/O
13169 * requests must go through this function.
13172 ctl_queue(union ctl_io *io)
13174 struct ctl_port *port;
13176 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13179 io->io_hdr.start_time = time_uptime;
13180 getbintime(&io->io_hdr.start_bt);
13181 #endif /* CTL_TIME_IO */
13183 /* Map FE-specific LUN ID into global one. */
13184 port = ctl_io_port(&io->io_hdr);
13185 io->io_hdr.nexus.targ_mapped_lun =
13186 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13188 switch (io->io_hdr.io_type) {
13191 if (ctl_debug & CTL_DEBUG_CDB)
13193 ctl_enqueue_incoming(io);
13196 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13200 return (CTL_RETVAL_COMPLETE);
13203 #ifdef CTL_IO_DELAY
13205 ctl_done_timer_wakeup(void *arg)
13209 io = (union ctl_io *)arg;
13212 #endif /* CTL_IO_DELAY */
13215 ctl_serseq_done(union ctl_io *io)
13217 struct ctl_lun *lun;
13219 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13220 if (lun->be_lun == NULL ||
13221 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF)
13223 mtx_lock(&lun->lun_lock);
13224 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE;
13225 ctl_check_blocked(lun);
13226 mtx_unlock(&lun->lun_lock);
13230 ctl_done(union ctl_io *io)
13234 * Enable this to catch duplicate completion issues.
13237 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13238 printf("%s: type %d msg %d cdb %x iptl: "
13239 "%u:%u:%u tag 0x%04x "
13240 "flag %#x status %x\n",
13242 io->io_hdr.io_type,
13243 io->io_hdr.msg_type,
13245 io->io_hdr.nexus.initid,
13246 io->io_hdr.nexus.targ_port,
13247 io->io_hdr.nexus.targ_lun,
13248 (io->io_hdr.io_type ==
13250 io->taskio.tag_num :
13251 io->scsiio.tag_num,
13253 io->io_hdr.status);
13255 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13259 * This is an internal copy of an I/O, and should not go through
13260 * the normal done processing logic.
13262 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13265 #ifdef CTL_IO_DELAY
13266 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13267 struct ctl_lun *lun;
13269 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13271 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13273 struct ctl_lun *lun;
13275 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13278 && (lun->delay_info.done_delay > 0)) {
13280 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
13281 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13282 callout_reset(&io->io_hdr.delay_callout,
13283 lun->delay_info.done_delay * hz,
13284 ctl_done_timer_wakeup, io);
13285 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13286 lun->delay_info.done_delay = 0;
13290 #endif /* CTL_IO_DELAY */
13292 ctl_enqueue_done(io);
13296 ctl_work_thread(void *arg)
13298 struct ctl_thread *thr = (struct ctl_thread *)arg;
13299 struct ctl_softc *softc = thr->ctl_softc;
13303 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13309 * We handle the queues in this order:
13311 * - done queue (to free up resources, unblock other commands)
13315 * If those queues are empty, we break out of the loop and
13318 mtx_lock(&thr->queue_lock);
13319 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13321 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13322 mtx_unlock(&thr->queue_lock);
13323 ctl_handle_isc(io);
13326 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13328 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13329 /* clear any blocked commands, call fe_done */
13330 mtx_unlock(&thr->queue_lock);
13331 retval = ctl_process_done(io);
13334 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13336 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13337 mtx_unlock(&thr->queue_lock);
13338 if (io->io_hdr.io_type == CTL_IO_TASK)
13341 ctl_scsiio_precheck(softc, &io->scsiio);
13344 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13346 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13347 mtx_unlock(&thr->queue_lock);
13348 retval = ctl_scsiio(&io->scsiio);
13349 if (retval != CTL_RETVAL_COMPLETE)
13350 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13354 /* Sleep until we have something to do. */
13355 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13360 ctl_lun_thread(void *arg)
13362 struct ctl_softc *softc = (struct ctl_softc *)arg;
13363 struct ctl_be_lun *be_lun;
13366 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13370 mtx_lock(&softc->ctl_lock);
13371 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13372 if (be_lun != NULL) {
13373 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13374 mtx_unlock(&softc->ctl_lock);
13375 ctl_create_lun(be_lun);
13379 /* Sleep until we have something to do. */
13380 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13381 PDROP | PRIBIO, "-", 0);
13386 ctl_thresh_thread(void *arg)
13388 struct ctl_softc *softc = (struct ctl_softc *)arg;
13389 struct ctl_lun *lun;
13390 struct ctl_be_lun *be_lun;
13391 struct scsi_da_rw_recovery_page *rwpage;
13392 struct ctl_logical_block_provisioning_page *page;
13394 union ctl_ha_msg msg;
13395 uint64_t thres, val;
13398 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13401 mtx_lock(&softc->ctl_lock);
13402 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13403 be_lun = lun->be_lun;
13404 if ((lun->flags & CTL_LUN_DISABLED) ||
13405 (lun->flags & CTL_LUN_OFFLINE) ||
13406 lun->backend->lun_attr == NULL)
13408 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
13409 softc->ha_mode == CTL_HA_MODE_XFER)
13411 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT];
13412 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0)
13415 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT];
13416 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13417 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13419 thres = scsi_4btoul(page->descr[i].count);
13420 thres <<= CTL_LBP_EXPONENT;
13421 switch (page->descr[i].resource) {
13423 attr = "blocksavail";
13426 attr = "blocksused";
13429 attr = "poolblocksavail";
13432 attr = "poolblocksused";
13437 mtx_unlock(&softc->ctl_lock); // XXX
13438 val = lun->backend->lun_attr(
13439 lun->be_lun->be_lun, attr);
13440 mtx_lock(&softc->ctl_lock);
13441 if (val == UINT64_MAX)
13443 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13444 == SLBPPD_ARMING_INC)
13445 e = (val >= thres);
13447 e = (val <= thres);
13451 mtx_lock(&lun->lun_lock);
13453 scsi_u64to8b((uint8_t *)&page->descr[i] -
13454 (uint8_t *)page, lun->ua_tpt_info);
13455 if (lun->lasttpt == 0 ||
13456 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13457 lun->lasttpt = time_uptime;
13458 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13464 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13467 mtx_unlock(&lun->lun_lock);
13469 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
13470 /* Send msg to other side. */
13471 bzero(&msg.ua, sizeof(msg.ua));
13472 msg.hdr.msg_type = CTL_MSG_UA;
13473 msg.hdr.nexus.initid = -1;
13474 msg.hdr.nexus.targ_port = -1;
13475 msg.hdr.nexus.targ_lun = lun->lun;
13476 msg.hdr.nexus.targ_mapped_lun = lun->lun;
13478 msg.ua.ua_set = (set > 0);
13479 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES;
13480 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8);
13481 mtx_unlock(&softc->ctl_lock); // XXX
13482 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13483 sizeof(msg.ua), M_WAITOK);
13484 mtx_lock(&softc->ctl_lock);
13487 mtx_unlock(&softc->ctl_lock);
13488 pause("-", CTL_LBP_PERIOD * hz);
13493 ctl_enqueue_incoming(union ctl_io *io)
13495 struct ctl_softc *softc = control_softc;
13496 struct ctl_thread *thr;
13499 idx = (io->io_hdr.nexus.targ_port * 127 +
13500 io->io_hdr.nexus.initid) % worker_threads;
13501 thr = &softc->threads[idx];
13502 mtx_lock(&thr->queue_lock);
13503 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13504 mtx_unlock(&thr->queue_lock);
13509 ctl_enqueue_rtr(union ctl_io *io)
13511 struct ctl_softc *softc = control_softc;
13512 struct ctl_thread *thr;
13514 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13515 mtx_lock(&thr->queue_lock);
13516 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13517 mtx_unlock(&thr->queue_lock);
13522 ctl_enqueue_done(union ctl_io *io)
13524 struct ctl_softc *softc = control_softc;
13525 struct ctl_thread *thr;
13527 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13528 mtx_lock(&thr->queue_lock);
13529 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13530 mtx_unlock(&thr->queue_lock);
13535 ctl_enqueue_isc(union ctl_io *io)
13537 struct ctl_softc *softc = control_softc;
13538 struct ctl_thread *thr;
13540 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13541 mtx_lock(&thr->queue_lock);
13542 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13543 mtx_unlock(&thr->queue_lock);