2 * Copyright (c) 2003-2009 Silicon Graphics International Corp.
3 * Copyright (c) 2012 The FreeBSD Foundation
4 * Copyright (c) 2015 Alexander Motin <mav@FreeBSD.org>
7 * Portions of this software were developed by Edward Tomasz Napierala
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions, and the following disclaimer,
15 * without modification.
16 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17 * substantially similar to the "NO WARRANTY" disclaimer below
18 * ("Disclaimer") and any redistribution must be conditioned upon
19 * including a substantially similar Disclaimer requirement for further
20 * binary redistribution.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGES.
38 * CAM Target Layer, a SCSI device emulation subsystem.
40 * Author: Ken Merry <ken@FreeBSD.org>
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/ctype.h>
51 #include <sys/kernel.h>
52 #include <sys/types.h>
53 #include <sys/kthread.h>
55 #include <sys/fcntl.h>
57 #include <sys/module.h>
58 #include <sys/mutex.h>
59 #include <sys/condvar.h>
60 #include <sys/malloc.h>
62 #include <sys/ioccom.h>
63 #include <sys/queue.h>
66 #include <sys/endian.h>
67 #include <sys/sysctl.h>
71 #include <cam/scsi/scsi_all.h>
72 #include <cam/scsi/scsi_da.h>
73 #include <cam/ctl/ctl_io.h>
74 #include <cam/ctl/ctl.h>
75 #include <cam/ctl/ctl_frontend.h>
76 #include <cam/ctl/ctl_util.h>
77 #include <cam/ctl/ctl_backend.h>
78 #include <cam/ctl/ctl_ioctl.h>
79 #include <cam/ctl/ctl_ha.h>
80 #include <cam/ctl/ctl_private.h>
81 #include <cam/ctl/ctl_debug.h>
82 #include <cam/ctl/ctl_scsi_all.h>
83 #include <cam/ctl/ctl_error.h>
85 struct ctl_softc *control_softc = NULL;
88 * Template mode pages.
92 * Note that these are default values only. The actual values will be
93 * filled in when the user does a mode sense.
95 const static struct copan_debugconf_subpage debugconf_page_default = {
96 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
97 DBGCNF_SUBPAGE_CODE, /* subpage */
98 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
99 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
100 DBGCNF_VERSION, /* page_version */
101 {CTL_TIME_IO_DEFAULT_SECS>>8,
102 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */
105 const static struct copan_debugconf_subpage debugconf_page_changeable = {
106 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
107 DBGCNF_SUBPAGE_CODE, /* subpage */
108 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
109 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
110 0, /* page_version */
111 {0xff,0xff}, /* ctl_time_io_secs */
114 const static struct scsi_da_rw_recovery_page rw_er_page_default = {
115 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
116 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
117 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE,
118 /*read_retry_count*/0,
119 /*correction_span*/0,
120 /*head_offset_count*/0,
121 /*data_strobe_offset_cnt*/0,
122 /*byte8*/SMS_RWER_LBPERE,
123 /*write_retry_count*/0,
125 /*recovery_time_limit*/{0, 0},
128 const static struct scsi_da_rw_recovery_page rw_er_page_changeable = {
129 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
130 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
132 /*read_retry_count*/0,
133 /*correction_span*/0,
134 /*head_offset_count*/0,
135 /*data_strobe_offset_cnt*/0,
137 /*write_retry_count*/0,
139 /*recovery_time_limit*/{0, 0},
142 const static struct scsi_format_page format_page_default = {
143 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
144 /*page_length*/sizeof(struct scsi_format_page) - 2,
145 /*tracks_per_zone*/ {0, 0},
146 /*alt_sectors_per_zone*/ {0, 0},
147 /*alt_tracks_per_zone*/ {0, 0},
148 /*alt_tracks_per_lun*/ {0, 0},
149 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
150 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
151 /*bytes_per_sector*/ {0, 0},
152 /*interleave*/ {0, 0},
153 /*track_skew*/ {0, 0},
154 /*cylinder_skew*/ {0, 0},
156 /*reserved*/ {0, 0, 0}
159 const static struct scsi_format_page format_page_changeable = {
160 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
161 /*page_length*/sizeof(struct scsi_format_page) - 2,
162 /*tracks_per_zone*/ {0, 0},
163 /*alt_sectors_per_zone*/ {0, 0},
164 /*alt_tracks_per_zone*/ {0, 0},
165 /*alt_tracks_per_lun*/ {0, 0},
166 /*sectors_per_track*/ {0, 0},
167 /*bytes_per_sector*/ {0, 0},
168 /*interleave*/ {0, 0},
169 /*track_skew*/ {0, 0},
170 /*cylinder_skew*/ {0, 0},
172 /*reserved*/ {0, 0, 0}
175 const static struct scsi_rigid_disk_page rigid_disk_page_default = {
176 /*page_code*/SMS_RIGID_DISK_PAGE,
177 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
178 /*cylinders*/ {0, 0, 0},
179 /*heads*/ CTL_DEFAULT_HEADS,
180 /*start_write_precomp*/ {0, 0, 0},
181 /*start_reduced_current*/ {0, 0, 0},
182 /*step_rate*/ {0, 0},
183 /*landing_zone_cylinder*/ {0, 0, 0},
184 /*rpl*/ SRDP_RPL_DISABLED,
185 /*rotational_offset*/ 0,
187 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
188 CTL_DEFAULT_ROTATION_RATE & 0xff},
192 const static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
193 /*page_code*/SMS_RIGID_DISK_PAGE,
194 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
195 /*cylinders*/ {0, 0, 0},
197 /*start_write_precomp*/ {0, 0, 0},
198 /*start_reduced_current*/ {0, 0, 0},
199 /*step_rate*/ {0, 0},
200 /*landing_zone_cylinder*/ {0, 0, 0},
202 /*rotational_offset*/ 0,
204 /*rotation_rate*/ {0, 0},
208 const static struct scsi_caching_page caching_page_default = {
209 /*page_code*/SMS_CACHING_PAGE,
210 /*page_length*/sizeof(struct scsi_caching_page) - 2,
211 /*flags1*/ SCP_DISC | SCP_WCE,
213 /*disable_pf_transfer_len*/ {0xff, 0xff},
214 /*min_prefetch*/ {0, 0},
215 /*max_prefetch*/ {0xff, 0xff},
216 /*max_pf_ceiling*/ {0xff, 0xff},
218 /*cache_segments*/ 0,
219 /*cache_seg_size*/ {0, 0},
221 /*non_cache_seg_size*/ {0, 0, 0}
224 const static struct scsi_caching_page caching_page_changeable = {
225 /*page_code*/SMS_CACHING_PAGE,
226 /*page_length*/sizeof(struct scsi_caching_page) - 2,
227 /*flags1*/ SCP_WCE | SCP_RCD,
229 /*disable_pf_transfer_len*/ {0, 0},
230 /*min_prefetch*/ {0, 0},
231 /*max_prefetch*/ {0, 0},
232 /*max_pf_ceiling*/ {0, 0},
234 /*cache_segments*/ 0,
235 /*cache_seg_size*/ {0, 0},
237 /*non_cache_seg_size*/ {0, 0, 0}
240 const static struct scsi_control_page control_page_default = {
241 /*page_code*/SMS_CONTROL_MODE_PAGE,
242 /*page_length*/sizeof(struct scsi_control_page) - 2,
244 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED,
247 /*aen_holdoff_period*/{0, 0},
248 /*busy_timeout_period*/{0, 0},
249 /*extended_selftest_completion_time*/{0, 0}
252 const static struct scsi_control_page control_page_changeable = {
253 /*page_code*/SMS_CONTROL_MODE_PAGE,
254 /*page_length*/sizeof(struct scsi_control_page) - 2,
256 /*queue_flags*/SCP_QUEUE_ALG_MASK,
257 /*eca_and_aen*/SCP_SWP,
259 /*aen_holdoff_period*/{0, 0},
260 /*busy_timeout_period*/{0, 0},
261 /*extended_selftest_completion_time*/{0, 0}
264 #define CTL_CEM_LEN (sizeof(struct scsi_control_ext_page) - 4)
266 const static struct scsi_control_ext_page control_ext_page_default = {
267 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
268 /*subpage_code*/0x01,
269 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
275 const static struct scsi_control_ext_page control_ext_page_changeable = {
276 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
277 /*subpage_code*/0x01,
278 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
284 const static struct scsi_info_exceptions_page ie_page_default = {
285 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
286 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
287 /*info_flags*/SIEP_FLAGS_DEXCPT,
289 /*interval_timer*/{0, 0, 0, 0},
290 /*report_count*/{0, 0, 0, 0}
293 const static struct scsi_info_exceptions_page ie_page_changeable = {
294 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
295 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
298 /*interval_timer*/{0, 0, 0, 0},
299 /*report_count*/{0, 0, 0, 0}
302 #define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4)
304 const static struct ctl_logical_block_provisioning_page lbp_page_default = {{
305 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
306 /*subpage_code*/0x02,
307 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
309 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
314 /*count*/{0, 0, 0, 0}},
318 /*count*/{0, 0, 0, 0}},
322 /*count*/{0, 0, 0, 0}},
326 /*count*/{0, 0, 0, 0}}
330 const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{
331 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
332 /*subpage_code*/0x02,
333 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
335 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
340 /*count*/{0, 0, 0, 0}},
344 /*count*/{0, 0, 0, 0}},
348 /*count*/{0, 0, 0, 0}},
352 /*count*/{0, 0, 0, 0}}
356 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
357 static int worker_threads = -1;
358 TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads);
359 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
360 &worker_threads, 1, "Number of worker threads");
361 static int ctl_debug = CTL_DEBUG_NONE;
362 TUNABLE_INT("kern.cam.ctl.debug", &ctl_debug);
363 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN,
364 &ctl_debug, 0, "Enabled debug flags");
367 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
368 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
369 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0),
370 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
372 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 10
374 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
376 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
377 static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest);
378 static int ctl_init(void);
379 void ctl_shutdown(void);
380 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
381 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
382 static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
383 static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
384 struct ctl_ooa *ooa_hdr,
385 struct ctl_ooa_entry *kern_entries);
386 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
388 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
389 struct ctl_be_lun *be_lun);
390 static int ctl_free_lun(struct ctl_lun *lun);
391 static void ctl_create_lun(struct ctl_be_lun *be_lun);
392 static struct ctl_port * ctl_io_port(struct ctl_io_hdr *io_hdr);
394 static int ctl_do_mode_select(union ctl_io *io);
395 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
396 uint64_t res_key, uint64_t sa_res_key,
397 uint8_t type, uint32_t residx,
398 struct ctl_scsiio *ctsio,
399 struct scsi_per_res_out *cdb,
400 struct scsi_per_res_out_parms* param);
401 static void ctl_pro_preempt_other(struct ctl_lun *lun,
402 union ctl_ha_msg *msg);
403 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
404 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
405 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
406 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
407 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
408 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
409 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
411 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
413 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
414 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
415 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
416 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
417 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
418 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2,
420 static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2);
421 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
422 union ctl_io *pending_io, union ctl_io *ooa_io);
423 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
424 union ctl_io *starting_io);
425 static int ctl_check_blocked(struct ctl_lun *lun);
426 static int ctl_scsiio_lun_check(struct ctl_lun *lun,
427 const struct ctl_cmd_entry *entry,
428 struct ctl_scsiio *ctsio);
429 static void ctl_failover_lun(struct ctl_lun *lun);
430 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
431 struct ctl_scsiio *ctsio);
432 static int ctl_scsiio(struct ctl_scsiio *ctsio);
434 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
435 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
436 ctl_ua_type ua_type);
437 static int ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io,
438 ctl_ua_type ua_type);
439 static int ctl_lun_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
440 static int ctl_abort_task(union ctl_io *io);
441 static int ctl_abort_task_set(union ctl_io *io);
442 static int ctl_query_task(union ctl_io *io, int task_set);
443 static int ctl_i_t_nexus_reset(union ctl_io *io);
444 static int ctl_query_async_event(union ctl_io *io);
445 static void ctl_run_task(union ctl_io *io);
447 static void ctl_datamove_timer_wakeup(void *arg);
448 static void ctl_done_timer_wakeup(void *arg);
449 #endif /* CTL_IO_DELAY */
451 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
452 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
453 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
454 static void ctl_datamove_remote_write(union ctl_io *io);
455 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
456 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
457 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
458 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
459 ctl_ha_dt_cb callback);
460 static void ctl_datamove_remote_read(union ctl_io *io);
461 static void ctl_datamove_remote(union ctl_io *io);
462 static int ctl_process_done(union ctl_io *io);
463 static void ctl_lun_thread(void *arg);
464 static void ctl_thresh_thread(void *arg);
465 static void ctl_work_thread(void *arg);
466 static void ctl_enqueue_incoming(union ctl_io *io);
467 static void ctl_enqueue_rtr(union ctl_io *io);
468 static void ctl_enqueue_done(union ctl_io *io);
469 static void ctl_enqueue_isc(union ctl_io *io);
470 static const struct ctl_cmd_entry *
471 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
472 static const struct ctl_cmd_entry *
473 ctl_validate_command(struct ctl_scsiio *ctsio);
474 static int ctl_cmd_applicable(uint8_t lun_type,
475 const struct ctl_cmd_entry *entry);
477 static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx);
478 static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx);
479 static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx);
480 static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key);
483 * Load the serialization table. This isn't very pretty, but is probably
484 * the easiest way to do it.
486 #include "ctl_ser_table.c"
489 * We only need to define open, close and ioctl routines for this driver.
491 static struct cdevsw ctl_cdevsw = {
492 .d_version = D_VERSION,
495 .d_close = ctl_close,
496 .d_ioctl = ctl_ioctl,
501 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
503 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
505 static moduledata_t ctl_moduledata = {
507 ctl_module_event_handler,
511 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
512 MODULE_VERSION(ctl, 1);
514 static struct ctl_frontend ha_frontend =
520 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
521 union ctl_ha_msg *msg_info)
523 struct ctl_scsiio *ctsio;
525 if (msg_info->hdr.original_sc == NULL) {
526 printf("%s: original_sc == NULL!\n", __func__);
527 /* XXX KDM now what? */
531 ctsio = &msg_info->hdr.original_sc->scsiio;
532 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
533 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
534 ctsio->io_hdr.status = msg_info->hdr.status;
535 ctsio->scsi_status = msg_info->scsi.scsi_status;
536 ctsio->sense_len = msg_info->scsi.sense_len;
537 ctsio->sense_residual = msg_info->scsi.sense_residual;
538 ctsio->residual = msg_info->scsi.residual;
539 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
540 msg_info->scsi.sense_len);
541 ctl_enqueue_isc((union ctl_io *)ctsio);
545 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
546 union ctl_ha_msg *msg_info)
548 struct ctl_scsiio *ctsio;
550 if (msg_info->hdr.serializing_sc == NULL) {
551 printf("%s: serializing_sc == NULL!\n", __func__);
552 /* XXX KDM now what? */
556 ctsio = &msg_info->hdr.serializing_sc->scsiio;
557 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
558 ctl_enqueue_isc((union ctl_io *)ctsio);
562 ctl_isc_announce_lun(struct ctl_lun *lun)
564 struct ctl_softc *softc = lun->ctl_softc;
565 union ctl_ha_msg *msg;
566 struct ctl_ha_msg_lun_pr_key pr_key;
569 if (softc->ha_link != CTL_HA_LINK_ONLINE)
571 mtx_lock(&lun->lun_lock);
572 i = sizeof(msg->lun);
574 i += lun->lun_devid->len;
575 i += sizeof(pr_key) * lun->pr_key_count;
577 mtx_unlock(&lun->lun_lock);
578 msg = malloc(i, M_CTL, M_WAITOK);
579 mtx_lock(&lun->lun_lock);
580 k = sizeof(msg->lun);
582 k += lun->lun_devid->len;
583 k += sizeof(pr_key) * lun->pr_key_count;
589 bzero(&msg->lun, sizeof(msg->lun));
590 msg->hdr.msg_type = CTL_MSG_LUN_SYNC;
591 msg->hdr.nexus.targ_lun = lun->lun;
592 msg->hdr.nexus.targ_mapped_lun = lun->lun;
593 msg->lun.flags = lun->flags;
594 msg->lun.pr_generation = lun->PRGeneration;
595 msg->lun.pr_res_idx = lun->pr_res_idx;
596 msg->lun.pr_res_type = lun->res_type;
597 msg->lun.pr_key_count = lun->pr_key_count;
599 if (lun->lun_devid) {
600 msg->lun.lun_devid_len = lun->lun_devid->len;
601 memcpy(&msg->lun.data[i], lun->lun_devid->data,
602 msg->lun.lun_devid_len);
603 i += msg->lun.lun_devid_len;
605 for (k = 0; k < CTL_MAX_INITIATORS; k++) {
606 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0)
609 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key));
612 mtx_unlock(&lun->lun_lock);
613 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
617 if (lun->flags & CTL_LUN_PRIMARY_SC) {
618 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
619 ctl_isc_announce_mode(lun, -1,
620 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
621 lun->mode_pages.index[i].subpage);
627 ctl_isc_announce_port(struct ctl_port *port)
629 struct ctl_softc *softc = control_softc;
630 union ctl_ha_msg *msg;
633 if (port->targ_port < softc->port_min ||
634 port->targ_port >= softc->port_max ||
635 softc->ha_link != CTL_HA_LINK_ONLINE)
637 i = sizeof(msg->port) + strlen(port->port_name) + 1;
639 i += sizeof(uint32_t) * CTL_MAX_LUNS;
640 if (port->port_devid)
641 i += port->port_devid->len;
642 if (port->target_devid)
643 i += port->target_devid->len;
644 if (port->init_devid)
645 i += port->init_devid->len;
646 msg = malloc(i, M_CTL, M_WAITOK);
647 bzero(&msg->port, sizeof(msg->port));
648 msg->hdr.msg_type = CTL_MSG_PORT_SYNC;
649 msg->hdr.nexus.targ_port = port->targ_port;
650 msg->port.port_type = port->port_type;
651 msg->port.physical_port = port->physical_port;
652 msg->port.virtual_port = port->virtual_port;
653 msg->port.status = port->status;
655 msg->port.name_len = sprintf(&msg->port.data[i],
656 "%d:%s", softc->ha_id, port->port_name) + 1;
657 i += msg->port.name_len;
659 msg->port.lun_map_len = sizeof(uint32_t) * CTL_MAX_LUNS;
660 memcpy(&msg->port.data[i], port->lun_map,
661 msg->port.lun_map_len);
662 i += msg->port.lun_map_len;
664 if (port->port_devid) {
665 msg->port.port_devid_len = port->port_devid->len;
666 memcpy(&msg->port.data[i], port->port_devid->data,
667 msg->port.port_devid_len);
668 i += msg->port.port_devid_len;
670 if (port->target_devid) {
671 msg->port.target_devid_len = port->target_devid->len;
672 memcpy(&msg->port.data[i], port->target_devid->data,
673 msg->port.target_devid_len);
674 i += msg->port.target_devid_len;
676 if (port->init_devid) {
677 msg->port.init_devid_len = port->init_devid->len;
678 memcpy(&msg->port.data[i], port->init_devid->data,
679 msg->port.init_devid_len);
680 i += msg->port.init_devid_len;
682 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
688 ctl_isc_announce_iid(struct ctl_port *port, int iid)
690 struct ctl_softc *softc = control_softc;
691 union ctl_ha_msg *msg;
694 if (port->targ_port < softc->port_min ||
695 port->targ_port >= softc->port_max ||
696 softc->ha_link != CTL_HA_LINK_ONLINE)
698 mtx_lock(&softc->ctl_lock);
699 i = sizeof(msg->iid);
701 if (port->wwpn_iid[iid].name)
702 l = strlen(port->wwpn_iid[iid].name) + 1;
704 msg = malloc(i, M_CTL, M_NOWAIT);
706 mtx_unlock(&softc->ctl_lock);
709 bzero(&msg->iid, sizeof(msg->iid));
710 msg->hdr.msg_type = CTL_MSG_IID_SYNC;
711 msg->hdr.nexus.targ_port = port->targ_port;
712 msg->hdr.nexus.initid = iid;
713 msg->iid.in_use = port->wwpn_iid[iid].in_use;
714 msg->iid.name_len = l;
715 msg->iid.wwpn = port->wwpn_iid[iid].wwpn;
716 if (port->wwpn_iid[iid].name)
717 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l);
718 mtx_unlock(&softc->ctl_lock);
719 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT);
724 ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx,
725 uint8_t page, uint8_t subpage)
727 struct ctl_softc *softc = lun->ctl_softc;
728 union ctl_ha_msg msg;
731 if (softc->ha_link != CTL_HA_LINK_ONLINE)
733 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
734 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
735 page && lun->mode_pages.index[i].subpage == subpage)
738 if (i == CTL_NUM_MODE_PAGES)
740 bzero(&msg.mode, sizeof(msg.mode));
741 msg.hdr.msg_type = CTL_MSG_MODE_SYNC;
742 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT;
743 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT;
744 msg.hdr.nexus.targ_lun = lun->lun;
745 msg.hdr.nexus.targ_mapped_lun = lun->lun;
746 msg.mode.page_code = page;
747 msg.mode.subpage = subpage;
748 msg.mode.page_len = lun->mode_pages.index[i].page_len;
749 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data,
751 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode),
756 ctl_isc_ha_link_up(struct ctl_softc *softc)
758 struct ctl_port *port;
760 union ctl_ha_msg msg;
763 /* Announce this node parameters to peer for validation. */
764 msg.login.msg_type = CTL_MSG_LOGIN;
765 msg.login.version = CTL_HA_VERSION;
766 msg.login.ha_mode = softc->ha_mode;
767 msg.login.ha_id = softc->ha_id;
768 msg.login.max_luns = CTL_MAX_LUNS;
769 msg.login.max_ports = CTL_MAX_PORTS;
770 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT;
771 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login),
774 STAILQ_FOREACH(port, &softc->port_list, links) {
775 ctl_isc_announce_port(port);
776 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
777 if (port->wwpn_iid[i].in_use)
778 ctl_isc_announce_iid(port, i);
781 STAILQ_FOREACH(lun, &softc->lun_list, links)
782 ctl_isc_announce_lun(lun);
786 ctl_isc_ha_link_down(struct ctl_softc *softc)
788 struct ctl_port *port;
793 mtx_lock(&softc->ctl_lock);
794 STAILQ_FOREACH(lun, &softc->lun_list, links) {
795 mtx_lock(&lun->lun_lock);
796 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) {
797 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
798 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
800 mtx_unlock(&lun->lun_lock);
802 mtx_unlock(&softc->ctl_lock);
803 io = ctl_alloc_io(softc->othersc_pool);
804 mtx_lock(&softc->ctl_lock);
806 io->io_hdr.msg_type = CTL_MSG_FAILOVER;
807 io->io_hdr.nexus.targ_mapped_lun = lun->lun;
811 STAILQ_FOREACH(port, &softc->port_list, links) {
812 if (port->targ_port >= softc->port_min &&
813 port->targ_port < softc->port_max)
815 port->status &= ~CTL_PORT_STATUS_ONLINE;
816 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
817 port->wwpn_iid[i].in_use = 0;
818 free(port->wwpn_iid[i].name, M_CTL);
819 port->wwpn_iid[i].name = NULL;
822 mtx_unlock(&softc->ctl_lock);
826 ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
829 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus);
831 mtx_lock(&softc->ctl_lock);
832 if (msg->hdr.nexus.targ_lun < CTL_MAX_LUNS &&
833 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) != NULL) {
834 mtx_lock(&lun->lun_lock);
835 mtx_unlock(&softc->ctl_lock);
836 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES &&
838 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8);
839 if (msg->ua.ua_all) {
841 ctl_est_ua_all(lun, iid, msg->ua.ua_type);
843 ctl_clr_ua_all(lun, iid, msg->ua.ua_type);
846 ctl_est_ua(lun, iid, msg->ua.ua_type);
848 ctl_clr_ua(lun, iid, msg->ua.ua_type);
850 mtx_unlock(&lun->lun_lock);
852 mtx_unlock(&softc->ctl_lock);
856 ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
859 struct ctl_ha_msg_lun_pr_key pr_key;
861 ctl_lun_flags oflags;
864 targ_lun = msg->hdr.nexus.targ_mapped_lun;
865 mtx_lock(&softc->ctl_lock);
866 if ((targ_lun >= CTL_MAX_LUNS) ||
867 ((lun = softc->ctl_luns[targ_lun]) == NULL)) {
868 mtx_unlock(&softc->ctl_lock);
871 mtx_lock(&lun->lun_lock);
872 mtx_unlock(&softc->ctl_lock);
873 if (lun->flags & CTL_LUN_DISABLED) {
874 mtx_unlock(&lun->lun_lock);
877 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0;
878 if (msg->lun.lun_devid_len != i || (i > 0 &&
879 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) {
880 mtx_unlock(&lun->lun_lock);
881 printf("%s: Received conflicting HA LUN %d\n",
882 __func__, msg->hdr.nexus.targ_lun);
885 /* Record whether peer is primary. */
887 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) &&
888 (msg->lun.flags & CTL_LUN_DISABLED) == 0)
889 lun->flags |= CTL_LUN_PEER_SC_PRIMARY;
891 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
892 if (oflags != lun->flags)
893 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
895 /* If peer is primary and we are not -- use data */
896 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
897 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) {
898 lun->PRGeneration = msg->lun.pr_generation;
899 lun->pr_res_idx = msg->lun.pr_res_idx;
900 lun->res_type = msg->lun.pr_res_type;
901 lun->pr_key_count = msg->lun.pr_key_count;
902 for (k = 0; k < CTL_MAX_INITIATORS; k++)
903 ctl_clr_prkey(lun, k);
904 for (k = 0; k < msg->lun.pr_key_count; k++) {
905 memcpy(&pr_key, &msg->lun.data[i],
907 ctl_alloc_prkey(lun, pr_key.pr_iid);
908 ctl_set_prkey(lun, pr_key.pr_iid,
914 mtx_unlock(&lun->lun_lock);
915 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n",
916 __func__, msg->hdr.nexus.targ_lun,
917 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ?
918 "primary" : "secondary"));
920 /* If we are primary but peer doesn't know -- notify */
921 if ((lun->flags & CTL_LUN_PRIMARY_SC) &&
922 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0)
923 ctl_isc_announce_lun(lun);
928 ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
930 struct ctl_port *port;
934 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
936 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__,
937 msg->hdr.nexus.targ_port));
939 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO);
940 port->frontend = &ha_frontend;
941 port->targ_port = msg->hdr.nexus.targ_port;
942 } else if (port->frontend == &ha_frontend) {
943 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__,
944 msg->hdr.nexus.targ_port));
947 printf("%s: Received conflicting HA port %d\n",
948 __func__, msg->hdr.nexus.targ_port);
951 port->port_type = msg->port.port_type;
952 port->physical_port = msg->port.physical_port;
953 port->virtual_port = msg->port.virtual_port;
954 port->status = msg->port.status;
956 free(port->port_name, M_CTL);
957 port->port_name = strndup(&msg->port.data[i], msg->port.name_len,
959 i += msg->port.name_len;
960 if (msg->port.lun_map_len != 0) {
961 if (port->lun_map == NULL)
962 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS,
964 memcpy(port->lun_map, &msg->port.data[i],
965 sizeof(uint32_t) * CTL_MAX_LUNS);
966 i += msg->port.lun_map_len;
968 free(port->lun_map, M_CTL);
969 port->lun_map = NULL;
971 if (msg->port.port_devid_len != 0) {
972 if (port->port_devid == NULL ||
973 port->port_devid->len != msg->port.port_devid_len) {
974 free(port->port_devid, M_CTL);
975 port->port_devid = malloc(sizeof(struct ctl_devid) +
976 msg->port.port_devid_len, M_CTL, M_WAITOK);
978 memcpy(port->port_devid->data, &msg->port.data[i],
979 msg->port.port_devid_len);
980 port->port_devid->len = msg->port.port_devid_len;
981 i += msg->port.port_devid_len;
983 free(port->port_devid, M_CTL);
984 port->port_devid = NULL;
986 if (msg->port.target_devid_len != 0) {
987 if (port->target_devid == NULL ||
988 port->target_devid->len != msg->port.target_devid_len) {
989 free(port->target_devid, M_CTL);
990 port->target_devid = malloc(sizeof(struct ctl_devid) +
991 msg->port.target_devid_len, M_CTL, M_WAITOK);
993 memcpy(port->target_devid->data, &msg->port.data[i],
994 msg->port.target_devid_len);
995 port->target_devid->len = msg->port.target_devid_len;
996 i += msg->port.target_devid_len;
998 free(port->target_devid, M_CTL);
999 port->target_devid = NULL;
1001 if (msg->port.init_devid_len != 0) {
1002 if (port->init_devid == NULL ||
1003 port->init_devid->len != msg->port.init_devid_len) {
1004 free(port->init_devid, M_CTL);
1005 port->init_devid = malloc(sizeof(struct ctl_devid) +
1006 msg->port.init_devid_len, M_CTL, M_WAITOK);
1008 memcpy(port->init_devid->data, &msg->port.data[i],
1009 msg->port.init_devid_len);
1010 port->init_devid->len = msg->port.init_devid_len;
1011 i += msg->port.init_devid_len;
1013 free(port->init_devid, M_CTL);
1014 port->init_devid = NULL;
1017 if (ctl_port_register(port) != 0) {
1018 printf("%s: ctl_port_register() failed with error\n",
1022 mtx_lock(&softc->ctl_lock);
1023 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1024 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
1026 mtx_lock(&lun->lun_lock);
1027 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
1028 mtx_unlock(&lun->lun_lock);
1030 mtx_unlock(&softc->ctl_lock);
1034 ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1036 struct ctl_port *port;
1039 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1041 printf("%s: Received IID for unknown port %d\n",
1042 __func__, msg->hdr.nexus.targ_port);
1045 iid = msg->hdr.nexus.initid;
1046 port->wwpn_iid[iid].in_use = msg->iid.in_use;
1047 port->wwpn_iid[iid].wwpn = msg->iid.wwpn;
1048 free(port->wwpn_iid[iid].name, M_CTL);
1049 if (msg->iid.name_len) {
1050 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0],
1051 msg->iid.name_len, M_CTL);
1053 port->wwpn_iid[iid].name = NULL;
1057 ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1060 if (msg->login.version != CTL_HA_VERSION) {
1061 printf("CTL HA peers have different versions %d != %d\n",
1062 msg->login.version, CTL_HA_VERSION);
1063 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1066 if (msg->login.ha_mode != softc->ha_mode) {
1067 printf("CTL HA peers have different ha_mode %d != %d\n",
1068 msg->login.ha_mode, softc->ha_mode);
1069 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1072 if (msg->login.ha_id == softc->ha_id) {
1073 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id);
1074 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1077 if (msg->login.max_luns != CTL_MAX_LUNS ||
1078 msg->login.max_ports != CTL_MAX_PORTS ||
1079 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) {
1080 printf("CTL HA peers have different limits\n");
1081 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1087 ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1089 struct ctl_lun *lun;
1091 uint32_t initidx, targ_lun;
1093 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1094 mtx_lock(&softc->ctl_lock);
1095 if ((targ_lun >= CTL_MAX_LUNS) ||
1096 ((lun = softc->ctl_luns[targ_lun]) == NULL)) {
1097 mtx_unlock(&softc->ctl_lock);
1100 mtx_lock(&lun->lun_lock);
1101 mtx_unlock(&softc->ctl_lock);
1102 if (lun->flags & CTL_LUN_DISABLED) {
1103 mtx_unlock(&lun->lun_lock);
1106 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
1107 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
1108 msg->mode.page_code &&
1109 lun->mode_pages.index[i].subpage == msg->mode.subpage)
1112 if (i == CTL_NUM_MODE_PAGES) {
1113 mtx_unlock(&lun->lun_lock);
1116 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data,
1117 lun->mode_pages.index[i].page_len);
1118 initidx = ctl_get_initindex(&msg->hdr.nexus);
1120 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
1121 mtx_unlock(&lun->lun_lock);
1125 * ISC (Inter Shelf Communication) event handler. Events from the HA
1126 * subsystem come in here.
1129 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
1131 struct ctl_softc *softc;
1133 struct ctl_prio *presio;
1134 ctl_ha_status isc_status;
1136 softc = control_softc;
1137 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event));
1138 if (event == CTL_HA_EVT_MSG_RECV) {
1139 union ctl_ha_msg *msg, msgbuf;
1141 if (param > sizeof(msgbuf))
1142 msg = malloc(param, M_CTL, M_WAITOK);
1145 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param,
1147 if (isc_status != CTL_HA_STATUS_SUCCESS) {
1148 printf("%s: Error receiving message: %d\n",
1149 __func__, isc_status);
1155 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type));
1156 switch (msg->hdr.msg_type) {
1157 case CTL_MSG_SERIALIZE:
1158 io = ctl_alloc_io(softc->othersc_pool);
1160 // populate ctsio from msg
1161 io->io_hdr.io_type = CTL_IO_SCSI;
1162 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
1163 io->io_hdr.original_sc = msg->hdr.original_sc;
1164 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
1167 * If we're in serialization-only mode, we don't
1168 * want to go through full done processing. Thus
1171 * XXX KDM add another flag that is more specific.
1173 if (softc->ha_mode != CTL_HA_MODE_XFER)
1174 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
1175 io->io_hdr.nexus = msg->hdr.nexus;
1177 printf("port %u, iid %u, lun %u\n",
1178 io->io_hdr.nexus.targ_port,
1179 io->io_hdr.nexus.initid,
1180 io->io_hdr.nexus.targ_lun);
1182 io->scsiio.tag_num = msg->scsi.tag_num;
1183 io->scsiio.tag_type = msg->scsi.tag_type;
1185 io->io_hdr.start_time = time_uptime;
1186 getbintime(&io->io_hdr.start_bt);
1187 #endif /* CTL_TIME_IO */
1188 io->scsiio.cdb_len = msg->scsi.cdb_len;
1189 memcpy(io->scsiio.cdb, msg->scsi.cdb,
1191 if (softc->ha_mode == CTL_HA_MODE_XFER) {
1192 const struct ctl_cmd_entry *entry;
1194 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
1195 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
1197 entry->flags & CTL_FLAG_DATA_MASK;
1199 ctl_enqueue_isc(io);
1202 /* Performed on the Originating SC, XFER mode only */
1203 case CTL_MSG_DATAMOVE: {
1204 struct ctl_sg_entry *sgl;
1207 io = msg->hdr.original_sc;
1209 printf("%s: original_sc == NULL!\n", __func__);
1210 /* XXX KDM do something here */
1213 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
1214 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1216 * Keep track of this, we need to send it back over
1217 * when the datamove is complete.
1219 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1220 if (msg->hdr.status == CTL_SUCCESS)
1221 io->io_hdr.status = msg->hdr.status;
1223 if (msg->dt.sg_sequence == 0) {
1224 i = msg->dt.kern_sg_entries +
1225 msg->dt.kern_data_len /
1226 CTL_HA_DATAMOVE_SEGMENT + 1;
1227 sgl = malloc(sizeof(*sgl) * i, M_CTL,
1229 io->io_hdr.remote_sglist = sgl;
1230 io->io_hdr.local_sglist =
1231 &sgl[msg->dt.kern_sg_entries];
1233 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
1235 io->scsiio.kern_sg_entries =
1236 msg->dt.kern_sg_entries;
1237 io->scsiio.rem_sg_entries =
1238 msg->dt.kern_sg_entries;
1239 io->scsiio.kern_data_len =
1240 msg->dt.kern_data_len;
1241 io->scsiio.kern_total_len =
1242 msg->dt.kern_total_len;
1243 io->scsiio.kern_data_resid =
1244 msg->dt.kern_data_resid;
1245 io->scsiio.kern_rel_offset =
1246 msg->dt.kern_rel_offset;
1247 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR;
1248 io->io_hdr.flags |= msg->dt.flags &
1251 sgl = (struct ctl_sg_entry *)
1252 io->scsiio.kern_data_ptr;
1254 for (i = msg->dt.sent_sg_entries, j = 0;
1255 i < (msg->dt.sent_sg_entries +
1256 msg->dt.cur_sg_entries); i++, j++) {
1257 sgl[i].addr = msg->dt.sg_list[j].addr;
1258 sgl[i].len = msg->dt.sg_list[j].len;
1261 printf("%s: DATAMOVE: %p,%lu j=%d, i=%d\n",
1262 __func__, sgl[i].addr, sgl[i].len, j, i);
1267 * If this is the last piece of the I/O, we've got
1268 * the full S/G list. Queue processing in the thread.
1269 * Otherwise wait for the next piece.
1271 if (msg->dt.sg_last != 0)
1272 ctl_enqueue_isc(io);
1275 /* Performed on the Serializing (primary) SC, XFER mode only */
1276 case CTL_MSG_DATAMOVE_DONE: {
1277 if (msg->hdr.serializing_sc == NULL) {
1278 printf("%s: serializing_sc == NULL!\n",
1280 /* XXX KDM now what? */
1284 * We grab the sense information here in case
1285 * there was a failure, so we can return status
1286 * back to the initiator.
1288 io = msg->hdr.serializing_sc;
1289 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
1290 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1291 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1292 io->io_hdr.port_status = msg->scsi.fetd_status;
1293 io->scsiio.residual = msg->scsi.residual;
1294 if (msg->hdr.status != CTL_STATUS_NONE) {
1295 io->io_hdr.status = msg->hdr.status;
1296 io->scsiio.scsi_status = msg->scsi.scsi_status;
1297 io->scsiio.sense_len = msg->scsi.sense_len;
1298 io->scsiio.sense_residual =msg->scsi.sense_residual;
1299 memcpy(&io->scsiio.sense_data,
1300 &msg->scsi.sense_data,
1301 msg->scsi.sense_len);
1302 if (msg->hdr.status == CTL_SUCCESS)
1303 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1305 ctl_enqueue_isc(io);
1309 /* Preformed on Originating SC, SER_ONLY mode */
1311 io = msg->hdr.original_sc;
1313 printf("%s: original_sc == NULL!\n",
1317 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1318 io->io_hdr.msg_type = CTL_MSG_R2R;
1319 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1320 ctl_enqueue_isc(io);
1324 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
1326 * Performed on the Originating (i.e. secondary) SC in XFER
1329 case CTL_MSG_FINISH_IO:
1330 if (softc->ha_mode == CTL_HA_MODE_XFER)
1331 ctl_isc_handler_finish_xfer(softc, msg);
1333 ctl_isc_handler_finish_ser_only(softc, msg);
1336 /* Preformed on Originating SC */
1337 case CTL_MSG_BAD_JUJU:
1338 io = msg->hdr.original_sc;
1340 printf("%s: Bad JUJU!, original_sc is NULL!\n",
1344 ctl_copy_sense_data(msg, io);
1346 * IO should have already been cleaned up on other
1347 * SC so clear this flag so we won't send a message
1348 * back to finish the IO there.
1350 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
1351 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1353 /* io = msg->hdr.serializing_sc; */
1354 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
1355 ctl_enqueue_isc(io);
1358 /* Handle resets sent from the other side */
1359 case CTL_MSG_MANAGE_TASKS: {
1360 struct ctl_taskio *taskio;
1361 taskio = (struct ctl_taskio *)ctl_alloc_io(
1362 softc->othersc_pool);
1363 ctl_zero_io((union ctl_io *)taskio);
1364 taskio->io_hdr.io_type = CTL_IO_TASK;
1365 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1366 taskio->io_hdr.nexus = msg->hdr.nexus;
1367 taskio->task_action = msg->task.task_action;
1368 taskio->tag_num = msg->task.tag_num;
1369 taskio->tag_type = msg->task.tag_type;
1371 taskio->io_hdr.start_time = time_uptime;
1372 getbintime(&taskio->io_hdr.start_bt);
1373 #endif /* CTL_TIME_IO */
1374 ctl_run_task((union ctl_io *)taskio);
1377 /* Persistent Reserve action which needs attention */
1378 case CTL_MSG_PERS_ACTION:
1379 presio = (struct ctl_prio *)ctl_alloc_io(
1380 softc->othersc_pool);
1381 ctl_zero_io((union ctl_io *)presio);
1382 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
1383 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1384 presio->io_hdr.nexus = msg->hdr.nexus;
1385 presio->pr_msg = msg->pr;
1386 ctl_enqueue_isc((union ctl_io *)presio);
1389 ctl_isc_ua(softc, msg, param);
1391 case CTL_MSG_PORT_SYNC:
1392 ctl_isc_port_sync(softc, msg, param);
1394 case CTL_MSG_LUN_SYNC:
1395 ctl_isc_lun_sync(softc, msg, param);
1397 case CTL_MSG_IID_SYNC:
1398 ctl_isc_iid_sync(softc, msg, param);
1401 ctl_isc_login(softc, msg, param);
1403 case CTL_MSG_MODE_SYNC:
1404 ctl_isc_mode_sync(softc, msg, param);
1407 printf("Received HA message of unknown type %d\n",
1409 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1414 } else if (event == CTL_HA_EVT_LINK_CHANGE) {
1415 printf("CTL: HA link status changed from %d to %d\n",
1416 softc->ha_link, param);
1417 if (param == softc->ha_link)
1419 if (softc->ha_link == CTL_HA_LINK_ONLINE) {
1420 softc->ha_link = param;
1421 ctl_isc_ha_link_down(softc);
1423 softc->ha_link = param;
1424 if (softc->ha_link == CTL_HA_LINK_ONLINE)
1425 ctl_isc_ha_link_up(softc);
1429 printf("ctl_isc_event_handler: Unknown event %d\n", event);
1435 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
1438 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data,
1439 src->scsi.sense_len);
1440 dest->scsiio.scsi_status = src->scsi.scsi_status;
1441 dest->scsiio.sense_len = src->scsi.sense_len;
1442 dest->io_hdr.status = src->hdr.status;
1446 ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest)
1449 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data,
1450 src->scsiio.sense_len);
1451 dest->scsi.scsi_status = src->scsiio.scsi_status;
1452 dest->scsi.sense_len = src->scsiio.sense_len;
1453 dest->hdr.status = src->io_hdr.status;
1457 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1459 struct ctl_softc *softc = lun->ctl_softc;
1462 if (initidx < softc->init_min || initidx >= softc->init_max)
1464 mtx_assert(&lun->lun_lock, MA_OWNED);
1465 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1468 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua;
1472 ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua)
1476 mtx_assert(&lun->lun_lock, MA_OWNED);
1477 if (lun->pending_ua[port] == NULL)
1479 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1480 if (port * CTL_MAX_INIT_PER_PORT + i == except)
1482 lun->pending_ua[port][i] |= ua;
1487 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1489 struct ctl_softc *softc = lun->ctl_softc;
1492 mtx_assert(&lun->lun_lock, MA_OWNED);
1493 for (i = softc->port_min; i < softc->port_max; i++)
1494 ctl_est_ua_port(lun, i, except, ua);
1498 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1500 struct ctl_softc *softc = lun->ctl_softc;
1503 if (initidx < softc->init_min || initidx >= softc->init_max)
1505 mtx_assert(&lun->lun_lock, MA_OWNED);
1506 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1509 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua;
1513 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1515 struct ctl_softc *softc = lun->ctl_softc;
1518 mtx_assert(&lun->lun_lock, MA_OWNED);
1519 for (i = softc->port_min; i < softc->port_max; i++) {
1520 if (lun->pending_ua[i] == NULL)
1522 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
1523 if (i * CTL_MAX_INIT_PER_PORT + j == except)
1525 lun->pending_ua[i][j] &= ~ua;
1531 ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx,
1532 ctl_ua_type ua_type)
1534 struct ctl_lun *lun;
1536 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
1537 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) {
1538 mtx_lock(&lun->lun_lock);
1539 ctl_clr_ua(lun, initidx, ua_type);
1540 mtx_unlock(&lun->lun_lock);
1545 ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS)
1547 struct ctl_softc *softc = (struct ctl_softc *)arg1;
1548 struct ctl_lun *lun;
1549 struct ctl_lun_req ireq;
1552 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1;
1553 error = sysctl_handle_int(oidp, &value, 0, req);
1554 if ((error != 0) || (req->newptr == NULL))
1557 mtx_lock(&softc->ctl_lock);
1559 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1561 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
1562 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1563 mtx_unlock(&softc->ctl_lock);
1564 bzero(&ireq, sizeof(ireq));
1565 ireq.reqtype = CTL_LUNREQ_MODIFY;
1566 ireq.reqdata.modify.lun_id = lun->lun;
1567 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0,
1569 if (ireq.status != CTL_LUN_OK) {
1570 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n",
1571 __func__, ireq.status, ireq.error_str);
1573 mtx_lock(&softc->ctl_lock);
1575 mtx_unlock(&softc->ctl_lock);
1582 struct ctl_softc *softc;
1584 int i, error, retval;
1587 control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1589 softc = control_softc;
1591 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
1594 softc->dev->si_drv1 = softc;
1596 sysctl_ctx_init(&softc->sysctl_ctx);
1597 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1598 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1599 CTLFLAG_RD, 0, "CAM Target Layer");
1601 if (softc->sysctl_tree == NULL) {
1602 printf("%s: unable to allocate sysctl tree\n", __func__);
1603 destroy_dev(softc->dev);
1604 free(control_softc, M_DEVBUF);
1605 control_softc = NULL;
1609 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1610 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
1611 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1612 softc->open_count = 0;
1615 * Default to actually sending a SYNCHRONIZE CACHE command down to
1618 softc->flags = CTL_FLAG_REAL_SYNC;
1620 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1621 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0,
1622 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)");
1625 * In Copan's HA scheme, the "master" and "slave" roles are
1626 * figured out through the slot the controller is in. Although it
1627 * is an active/active system, someone has to be in charge.
1629 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1630 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1631 "HA head ID (0 - no HA)");
1632 if (softc->ha_id == 0 || softc->ha_id > NUM_TARGET_PORT_GROUPS) {
1633 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1634 softc->is_single = 1;
1635 softc->port_cnt = CTL_MAX_PORTS;
1636 softc->port_min = 0;
1638 softc->port_cnt = CTL_MAX_PORTS / NUM_TARGET_PORT_GROUPS;
1639 softc->port_min = (softc->ha_id - 1) * softc->port_cnt;
1641 softc->port_max = softc->port_min + softc->port_cnt;
1642 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT;
1643 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT;
1645 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1646 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0,
1647 "HA link state (0 - offline, 1 - unknown, 2 - online)");
1649 STAILQ_INIT(&softc->lun_list);
1650 STAILQ_INIT(&softc->pending_lun_queue);
1651 STAILQ_INIT(&softc->fe_list);
1652 STAILQ_INIT(&softc->port_list);
1653 STAILQ_INIT(&softc->be_list);
1654 ctl_tpc_init(softc);
1656 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
1659 printf("ctl: can't allocate %d entry other SC pool, "
1660 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1663 softc->othersc_pool = other_pool;
1665 if (worker_threads <= 0)
1666 worker_threads = max(1, mp_ncpus / 4);
1667 if (worker_threads > CTL_MAX_THREADS)
1668 worker_threads = CTL_MAX_THREADS;
1670 for (i = 0; i < worker_threads; i++) {
1671 struct ctl_thread *thr = &softc->threads[i];
1673 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1674 thr->ctl_softc = softc;
1675 STAILQ_INIT(&thr->incoming_queue);
1676 STAILQ_INIT(&thr->rtr_queue);
1677 STAILQ_INIT(&thr->done_queue);
1678 STAILQ_INIT(&thr->isc_queue);
1680 error = kproc_kthread_add(ctl_work_thread, thr,
1681 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1683 printf("error creating CTL work thread!\n");
1684 ctl_pool_free(other_pool);
1688 error = kproc_kthread_add(ctl_lun_thread, softc,
1689 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
1691 printf("error creating CTL lun thread!\n");
1692 ctl_pool_free(other_pool);
1695 error = kproc_kthread_add(ctl_thresh_thread, softc,
1696 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh");
1698 printf("error creating CTL threshold thread!\n");
1699 ctl_pool_free(other_pool);
1703 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1704 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN,
1705 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head");
1707 if (softc->is_single == 0) {
1708 ctl_frontend_register(&ha_frontend);
1709 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) {
1710 printf("ctl_init: ctl_ha_msg_init failed.\n");
1711 softc->is_single = 1;
1713 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
1714 != CTL_HA_STATUS_SUCCESS) {
1715 printf("ctl_init: ctl_ha_msg_register failed.\n");
1716 softc->is_single = 1;
1725 struct ctl_softc *softc;
1726 struct ctl_lun *lun, *next_lun;
1728 softc = (struct ctl_softc *)control_softc;
1730 if (softc->is_single == 0) {
1731 ctl_ha_msg_shutdown(softc);
1732 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL)
1733 != CTL_HA_STATUS_SUCCESS)
1734 printf("%s: ctl_ha_msg_deregister failed.\n", __func__);
1735 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS)
1736 printf("%s: ctl_ha_msg_destroy failed.\n", __func__);
1737 ctl_frontend_deregister(&ha_frontend);
1740 mtx_lock(&softc->ctl_lock);
1745 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
1746 next_lun = STAILQ_NEXT(lun, links);
1750 mtx_unlock(&softc->ctl_lock);
1753 ctl_shutdown_thread(softc->work_thread);
1754 mtx_destroy(&softc->queue_lock);
1757 ctl_tpc_shutdown(softc);
1758 uma_zdestroy(softc->io_zone);
1759 mtx_destroy(&softc->ctl_lock);
1761 destroy_dev(softc->dev);
1763 sysctl_ctx_free(&softc->sysctl_ctx);
1765 free(control_softc, M_DEVBUF);
1766 control_softc = NULL;
1770 ctl_module_event_handler(module_t mod, int what, void *arg)
1775 return (ctl_init());
1779 return (EOPNOTSUPP);
1784 * XXX KDM should we do some access checks here? Bump a reference count to
1785 * prevent a CTL module from being unloaded while someone has it open?
1788 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1794 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1800 * Remove an initiator by port number and initiator ID.
1801 * Returns 0 for success, -1 for failure.
1804 ctl_remove_initiator(struct ctl_port *port, int iid)
1806 struct ctl_softc *softc = control_softc;
1808 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1810 if (iid > CTL_MAX_INIT_PER_PORT) {
1811 printf("%s: initiator ID %u > maximun %u!\n",
1812 __func__, iid, CTL_MAX_INIT_PER_PORT);
1816 mtx_lock(&softc->ctl_lock);
1817 port->wwpn_iid[iid].in_use--;
1818 port->wwpn_iid[iid].last_use = time_uptime;
1819 mtx_unlock(&softc->ctl_lock);
1820 ctl_isc_announce_iid(port, iid);
1826 * Add an initiator to the initiator map.
1827 * Returns iid for success, < 0 for failure.
1830 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
1832 struct ctl_softc *softc = control_softc;
1836 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1838 if (iid >= CTL_MAX_INIT_PER_PORT) {
1839 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
1840 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
1845 mtx_lock(&softc->ctl_lock);
1847 if (iid < 0 && (wwpn != 0 || name != NULL)) {
1848 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1849 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
1853 if (name != NULL && port->wwpn_iid[i].name != NULL &&
1854 strcmp(name, port->wwpn_iid[i].name) == 0) {
1862 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1863 if (port->wwpn_iid[i].in_use == 0 &&
1864 port->wwpn_iid[i].wwpn == 0 &&
1865 port->wwpn_iid[i].name == NULL) {
1874 best_time = INT32_MAX;
1875 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1876 if (port->wwpn_iid[i].in_use == 0) {
1877 if (port->wwpn_iid[i].last_use < best_time) {
1879 best_time = port->wwpn_iid[i].last_use;
1887 mtx_unlock(&softc->ctl_lock);
1892 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
1894 * This is not an error yet.
1896 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
1898 printf("%s: port %d iid %u WWPN %#jx arrived"
1899 " again\n", __func__, port->targ_port,
1900 iid, (uintmax_t)wwpn);
1904 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
1905 strcmp(name, port->wwpn_iid[iid].name) == 0) {
1907 printf("%s: port %d iid %u name '%s' arrived"
1908 " again\n", __func__, port->targ_port,
1915 * This is an error, but what do we do about it? The
1916 * driver is telling us we have a new WWPN for this
1917 * initiator ID, so we pretty much need to use it.
1919 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
1920 " but WWPN %#jx '%s' is still at that address\n",
1921 __func__, port->targ_port, iid, wwpn, name,
1922 (uintmax_t)port->wwpn_iid[iid].wwpn,
1923 port->wwpn_iid[iid].name);
1926 * XXX KDM clear have_ca and ua_pending on each LUN for
1931 free(port->wwpn_iid[iid].name, M_CTL);
1932 port->wwpn_iid[iid].name = name;
1933 port->wwpn_iid[iid].wwpn = wwpn;
1934 port->wwpn_iid[iid].in_use++;
1935 mtx_unlock(&softc->ctl_lock);
1936 ctl_isc_announce_iid(port, iid);
1942 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
1946 switch (port->port_type) {
1949 struct scsi_transportid_fcp *id =
1950 (struct scsi_transportid_fcp *)buf;
1951 if (port->wwpn_iid[iid].wwpn == 0)
1953 memset(id, 0, sizeof(*id));
1954 id->format_protocol = SCSI_PROTO_FC;
1955 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
1956 return (sizeof(*id));
1958 case CTL_PORT_ISCSI:
1960 struct scsi_transportid_iscsi_port *id =
1961 (struct scsi_transportid_iscsi_port *)buf;
1962 if (port->wwpn_iid[iid].name == NULL)
1965 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
1967 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
1968 len = roundup2(min(len, 252), 4);
1969 scsi_ulto2b(len, id->additional_length);
1970 return (sizeof(*id) + len);
1974 struct scsi_transportid_sas *id =
1975 (struct scsi_transportid_sas *)buf;
1976 if (port->wwpn_iid[iid].wwpn == 0)
1978 memset(id, 0, sizeof(*id));
1979 id->format_protocol = SCSI_PROTO_SAS;
1980 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
1981 return (sizeof(*id));
1985 struct scsi_transportid_spi *id =
1986 (struct scsi_transportid_spi *)buf;
1987 memset(id, 0, sizeof(*id));
1988 id->format_protocol = SCSI_PROTO_SPI;
1989 scsi_ulto2b(iid, id->scsi_addr);
1990 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
1991 return (sizeof(*id));
1997 * Serialize a command that went down the "wrong" side, and so was sent to
1998 * this controller for execution. The logic is a little different than the
1999 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
2000 * sent back to the other side, but in the success case, we execute the
2001 * command on this side (XFER mode) or tell the other side to execute it
2005 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
2007 struct ctl_softc *softc;
2008 union ctl_ha_msg msg_info;
2009 struct ctl_lun *lun;
2010 const struct ctl_cmd_entry *entry;
2014 softc = control_softc;
2016 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
2017 mtx_lock(&softc->ctl_lock);
2018 if ((targ_lun < CTL_MAX_LUNS) &&
2019 ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
2020 mtx_lock(&lun->lun_lock);
2021 mtx_unlock(&softc->ctl_lock);
2023 * If the LUN is invalid, pretend that it doesn't exist.
2024 * It will go away as soon as all pending I/O has been
2027 if (lun->flags & CTL_LUN_DISABLED) {
2028 mtx_unlock(&lun->lun_lock);
2032 mtx_unlock(&softc->ctl_lock);
2037 * The other node would not send this request to us unless
2038 * received announce that we are primary node for this LUN.
2039 * If this LUN does not exist now, it is probably result of
2040 * a race, so respond to initiator in the most opaque way.
2042 ctl_set_busy(ctsio);
2043 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
2044 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2045 msg_info.hdr.serializing_sc = NULL;
2046 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
2047 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2048 sizeof(msg_info.scsi), M_WAITOK);
2052 entry = ctl_get_cmd_entry(ctsio, NULL);
2053 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
2054 mtx_unlock(&lun->lun_lock);
2055 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
2056 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2057 msg_info.hdr.serializing_sc = NULL;
2058 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
2059 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2060 sizeof(msg_info.scsi), M_WAITOK);
2064 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
2065 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = lun->be_lun;
2068 * Every I/O goes into the OOA queue for a
2069 * particular LUN, and stays there until completion.
2072 if (TAILQ_EMPTY(&lun->ooa_queue))
2073 lun->idle_time += getsbinuptime() - lun->last_busy;
2075 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2077 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
2078 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
2080 case CTL_ACTION_BLOCK:
2081 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
2082 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
2084 mtx_unlock(&lun->lun_lock);
2086 case CTL_ACTION_PASS:
2087 case CTL_ACTION_SKIP:
2088 if (softc->ha_mode == CTL_HA_MODE_XFER) {
2089 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
2090 ctl_enqueue_rtr((union ctl_io *)ctsio);
2091 mtx_unlock(&lun->lun_lock);
2093 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
2094 mtx_unlock(&lun->lun_lock);
2096 /* send msg back to other side */
2097 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2098 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
2099 msg_info.hdr.msg_type = CTL_MSG_R2R;
2100 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2101 sizeof(msg_info.hdr), M_WAITOK);
2104 case CTL_ACTION_OVERLAP:
2105 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2106 mtx_unlock(&lun->lun_lock);
2109 ctl_set_overlapped_cmd(ctsio);
2110 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
2111 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2112 msg_info.hdr.serializing_sc = NULL;
2113 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
2114 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2115 sizeof(msg_info.scsi), M_WAITOK);
2117 case CTL_ACTION_OVERLAP_TAG:
2118 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2119 mtx_unlock(&lun->lun_lock);
2121 ctl_set_overlapped_tag(ctsio, ctsio->tag_num);
2122 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
2123 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2124 msg_info.hdr.serializing_sc = NULL;
2125 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
2126 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2127 sizeof(msg_info.scsi), M_WAITOK);
2129 case CTL_ACTION_ERROR:
2131 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2132 mtx_unlock(&lun->lun_lock);
2135 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2137 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
2138 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2139 msg_info.hdr.serializing_sc = NULL;
2140 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
2141 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2142 sizeof(msg_info.scsi), M_WAITOK);
2149 * Returns 0 for success, errno for failure.
2152 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2153 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2160 mtx_lock(&lun->lun_lock);
2161 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2162 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2164 struct ctl_ooa_entry *entry;
2167 * If we've got more than we can fit, just count the
2168 * remaining entries.
2170 if (*cur_fill_num >= ooa_hdr->alloc_num)
2173 entry = &kern_entries[*cur_fill_num];
2175 entry->tag_num = io->scsiio.tag_num;
2176 entry->lun_num = lun->lun;
2178 entry->start_bt = io->io_hdr.start_bt;
2180 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2181 entry->cdb_len = io->scsiio.cdb_len;
2182 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2183 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2185 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2186 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2188 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2189 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2191 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2192 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2194 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2195 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2197 mtx_unlock(&lun->lun_lock);
2203 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2204 size_t error_str_len)
2208 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2210 if (copyin(user_addr, kptr, len) != 0) {
2211 snprintf(error_str, error_str_len, "Error copying %d bytes "
2212 "from user address %p to kernel address %p", len,
2222 ctl_free_args(int num_args, struct ctl_be_arg *args)
2229 for (i = 0; i < num_args; i++) {
2230 free(args[i].kname, M_CTL);
2231 free(args[i].kvalue, M_CTL);
2237 static struct ctl_be_arg *
2238 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
2239 char *error_str, size_t error_str_len)
2241 struct ctl_be_arg *args;
2244 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
2245 error_str, error_str_len);
2250 for (i = 0; i < num_args; i++) {
2251 args[i].kname = NULL;
2252 args[i].kvalue = NULL;
2255 for (i = 0; i < num_args; i++) {
2258 args[i].kname = ctl_copyin_alloc(args[i].name,
2259 args[i].namelen, error_str, error_str_len);
2260 if (args[i].kname == NULL)
2263 if (args[i].kname[args[i].namelen - 1] != '\0') {
2264 snprintf(error_str, error_str_len, "Argument %d "
2265 "name is not NUL-terminated", i);
2269 if (args[i].flags & CTL_BEARG_RD) {
2270 tmpptr = ctl_copyin_alloc(args[i].value,
2271 args[i].vallen, error_str, error_str_len);
2274 if ((args[i].flags & CTL_BEARG_ASCII)
2275 && (tmpptr[args[i].vallen - 1] != '\0')) {
2276 snprintf(error_str, error_str_len, "Argument "
2277 "%d value is not NUL-terminated", i);
2280 args[i].kvalue = tmpptr;
2282 args[i].kvalue = malloc(args[i].vallen,
2283 M_CTL, M_WAITOK | M_ZERO);
2290 ctl_free_args(num_args, args);
2296 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2300 for (i = 0; i < num_args; i++) {
2301 if (args[i].flags & CTL_BEARG_WR)
2302 copyout(args[i].kvalue, args[i].value, args[i].vallen);
2307 * Escape characters that are illegal or not recommended in XML.
2310 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2312 char *end = str + size;
2317 for (; *str && str < end; str++) {
2320 retval = sbuf_printf(sb, "&");
2323 retval = sbuf_printf(sb, ">");
2326 retval = sbuf_printf(sb, "<");
2329 retval = sbuf_putc(sb, *str);
2342 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2344 struct scsi_vpd_id_descriptor *desc;
2347 if (id == NULL || id->len < 4)
2349 desc = (struct scsi_vpd_id_descriptor *)id->data;
2350 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2351 case SVPD_ID_TYPE_T10:
2352 sbuf_printf(sb, "t10.");
2354 case SVPD_ID_TYPE_EUI64:
2355 sbuf_printf(sb, "eui.");
2357 case SVPD_ID_TYPE_NAA:
2358 sbuf_printf(sb, "naa.");
2360 case SVPD_ID_TYPE_SCSI_NAME:
2363 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2364 case SVPD_ID_CODESET_BINARY:
2365 for (i = 0; i < desc->length; i++)
2366 sbuf_printf(sb, "%02x", desc->identifier[i]);
2368 case SVPD_ID_CODESET_ASCII:
2369 sbuf_printf(sb, "%.*s", (int)desc->length,
2370 (char *)desc->identifier);
2372 case SVPD_ID_CODESET_UTF8:
2373 sbuf_printf(sb, "%s", (char *)desc->identifier);
2379 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2382 struct ctl_softc *softc;
2383 struct ctl_lun *lun;
2386 softc = control_softc;
2392 retval = ctl_ioctl_io(dev, cmd, addr, flag, td);
2394 case CTL_ENABLE_PORT:
2395 case CTL_DISABLE_PORT:
2396 case CTL_SET_PORT_WWNS: {
2397 struct ctl_port *port;
2398 struct ctl_port_entry *entry;
2400 entry = (struct ctl_port_entry *)addr;
2402 mtx_lock(&softc->ctl_lock);
2403 STAILQ_FOREACH(port, &softc->port_list, links) {
2406 if (port->targ_port < softc->port_min ||
2407 port->targ_port >= softc->port_max)
2412 if ((entry->port_type == CTL_PORT_NONE)
2413 && (entry->targ_port == port->targ_port)) {
2415 * If the user only wants to enable or
2416 * disable or set WWNs on a specific port,
2417 * do the operation and we're done.
2421 } else if (entry->port_type & port->port_type) {
2423 * Compare the user's type mask with the
2424 * particular frontend type to see if we
2431 * Make sure the user isn't trying to set
2432 * WWNs on multiple ports at the same time.
2434 if (cmd == CTL_SET_PORT_WWNS) {
2435 printf("%s: Can't set WWNs on "
2436 "multiple ports\n", __func__);
2445 * XXX KDM we have to drop the lock here, because
2446 * the online/offline operations can potentially
2447 * block. We need to reference count the frontends
2448 * so they can't go away,
2450 if (cmd == CTL_ENABLE_PORT) {
2451 mtx_unlock(&softc->ctl_lock);
2452 ctl_port_online(port);
2453 mtx_lock(&softc->ctl_lock);
2454 } else if (cmd == CTL_DISABLE_PORT) {
2455 mtx_unlock(&softc->ctl_lock);
2456 ctl_port_offline(port);
2457 mtx_lock(&softc->ctl_lock);
2458 } else if (cmd == CTL_SET_PORT_WWNS) {
2459 ctl_port_set_wwns(port,
2460 (entry->flags & CTL_PORT_WWNN_VALID) ?
2462 (entry->flags & CTL_PORT_WWPN_VALID) ?
2463 1 : 0, entry->wwpn);
2468 mtx_unlock(&softc->ctl_lock);
2471 case CTL_GET_PORT_LIST: {
2472 struct ctl_port *port;
2473 struct ctl_port_list *list;
2476 list = (struct ctl_port_list *)addr;
2478 if (list->alloc_len != (list->alloc_num *
2479 sizeof(struct ctl_port_entry))) {
2480 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != "
2481 "alloc_num %u * sizeof(struct ctl_port_entry) "
2482 "%zu\n", __func__, list->alloc_len,
2483 list->alloc_num, sizeof(struct ctl_port_entry));
2489 list->dropped_num = 0;
2491 mtx_lock(&softc->ctl_lock);
2492 STAILQ_FOREACH(port, &softc->port_list, links) {
2493 struct ctl_port_entry entry, *list_entry;
2495 if (list->fill_num >= list->alloc_num) {
2496 list->dropped_num++;
2500 entry.port_type = port->port_type;
2501 strlcpy(entry.port_name, port->port_name,
2502 sizeof(entry.port_name));
2503 entry.targ_port = port->targ_port;
2504 entry.physical_port = port->physical_port;
2505 entry.virtual_port = port->virtual_port;
2506 entry.wwnn = port->wwnn;
2507 entry.wwpn = port->wwpn;
2508 if (port->status & CTL_PORT_STATUS_ONLINE)
2513 list_entry = &list->entries[i];
2515 retval = copyout(&entry, list_entry, sizeof(entry));
2517 printf("%s: CTL_GET_PORT_LIST: copyout "
2518 "returned %d\n", __func__, retval);
2523 list->fill_len += sizeof(entry);
2525 mtx_unlock(&softc->ctl_lock);
2528 * If this is non-zero, we had a copyout fault, so there's
2529 * probably no point in attempting to set the status inside
2535 if (list->dropped_num > 0)
2536 list->status = CTL_PORT_LIST_NEED_MORE_SPACE;
2538 list->status = CTL_PORT_LIST_OK;
2541 case CTL_DUMP_OOA: {
2546 mtx_lock(&softc->ctl_lock);
2547 printf("Dumping OOA queues:\n");
2548 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2549 mtx_lock(&lun->lun_lock);
2550 for (io = (union ctl_io *)TAILQ_FIRST(
2551 &lun->ooa_queue); io != NULL;
2552 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2554 sbuf_new(&sb, printbuf, sizeof(printbuf),
2556 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ",
2560 CTL_FLAG_BLOCKED) ? "" : " BLOCKED",
2562 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
2564 CTL_FLAG_ABORT) ? " ABORT" : "",
2566 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : "");
2567 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
2569 printf("%s\n", sbuf_data(&sb));
2571 mtx_unlock(&lun->lun_lock);
2573 printf("OOA queues dump done\n");
2574 mtx_unlock(&softc->ctl_lock);
2578 struct ctl_ooa *ooa_hdr;
2579 struct ctl_ooa_entry *entries;
2580 uint32_t cur_fill_num;
2582 ooa_hdr = (struct ctl_ooa *)addr;
2584 if ((ooa_hdr->alloc_len == 0)
2585 || (ooa_hdr->alloc_num == 0)) {
2586 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2587 "must be non-zero\n", __func__,
2588 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2593 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2594 sizeof(struct ctl_ooa_entry))) {
2595 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2596 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2597 __func__, ooa_hdr->alloc_len,
2598 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2603 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2604 if (entries == NULL) {
2605 printf("%s: could not allocate %d bytes for OOA "
2606 "dump\n", __func__, ooa_hdr->alloc_len);
2611 mtx_lock(&softc->ctl_lock);
2612 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2613 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS)
2614 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2615 mtx_unlock(&softc->ctl_lock);
2616 free(entries, M_CTL);
2617 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2618 __func__, (uintmax_t)ooa_hdr->lun_num);
2625 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2626 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2627 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2633 mtx_unlock(&softc->ctl_lock);
2634 free(entries, M_CTL);
2638 lun = softc->ctl_luns[ooa_hdr->lun_num];
2640 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr,
2643 mtx_unlock(&softc->ctl_lock);
2645 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2646 ooa_hdr->fill_len = ooa_hdr->fill_num *
2647 sizeof(struct ctl_ooa_entry);
2648 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2650 printf("%s: error copying out %d bytes for OOA dump\n",
2651 __func__, ooa_hdr->fill_len);
2654 getbintime(&ooa_hdr->cur_bt);
2656 if (cur_fill_num > ooa_hdr->alloc_num) {
2657 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2658 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2660 ooa_hdr->dropped_num = 0;
2661 ooa_hdr->status = CTL_OOA_OK;
2664 free(entries, M_CTL);
2667 case CTL_CHECK_OOA: {
2669 struct ctl_ooa_info *ooa_info;
2672 ooa_info = (struct ctl_ooa_info *)addr;
2674 if (ooa_info->lun_id >= CTL_MAX_LUNS) {
2675 ooa_info->status = CTL_OOA_INVALID_LUN;
2678 mtx_lock(&softc->ctl_lock);
2679 lun = softc->ctl_luns[ooa_info->lun_id];
2681 mtx_unlock(&softc->ctl_lock);
2682 ooa_info->status = CTL_OOA_INVALID_LUN;
2685 mtx_lock(&lun->lun_lock);
2686 mtx_unlock(&softc->ctl_lock);
2687 ooa_info->num_entries = 0;
2688 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
2689 io != NULL; io = (union ctl_io *)TAILQ_NEXT(
2690 &io->io_hdr, ooa_links)) {
2691 ooa_info->num_entries++;
2693 mtx_unlock(&lun->lun_lock);
2695 ooa_info->status = CTL_OOA_SUCCESS;
2699 case CTL_DELAY_IO: {
2700 struct ctl_io_delay_info *delay_info;
2702 delay_info = (struct ctl_io_delay_info *)addr;
2705 mtx_lock(&softc->ctl_lock);
2707 if ((delay_info->lun_id >= CTL_MAX_LUNS)
2708 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2709 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2711 lun = softc->ctl_luns[delay_info->lun_id];
2712 mtx_lock(&lun->lun_lock);
2714 delay_info->status = CTL_DELAY_STATUS_OK;
2716 switch (delay_info->delay_type) {
2717 case CTL_DELAY_TYPE_CONT:
2719 case CTL_DELAY_TYPE_ONESHOT:
2722 delay_info->status =
2723 CTL_DELAY_STATUS_INVALID_TYPE;
2727 switch (delay_info->delay_loc) {
2728 case CTL_DELAY_LOC_DATAMOVE:
2729 lun->delay_info.datamove_type =
2730 delay_info->delay_type;
2731 lun->delay_info.datamove_delay =
2732 delay_info->delay_secs;
2734 case CTL_DELAY_LOC_DONE:
2735 lun->delay_info.done_type =
2736 delay_info->delay_type;
2737 lun->delay_info.done_delay =
2738 delay_info->delay_secs;
2741 delay_info->status =
2742 CTL_DELAY_STATUS_INVALID_LOC;
2745 mtx_unlock(&lun->lun_lock);
2748 mtx_unlock(&softc->ctl_lock);
2750 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2751 #endif /* CTL_IO_DELAY */
2754 case CTL_REALSYNC_SET: {
2757 syncstate = (int *)addr;
2759 mtx_lock(&softc->ctl_lock);
2760 switch (*syncstate) {
2762 softc->flags &= ~CTL_FLAG_REAL_SYNC;
2765 softc->flags |= CTL_FLAG_REAL_SYNC;
2771 mtx_unlock(&softc->ctl_lock);
2774 case CTL_REALSYNC_GET: {
2777 syncstate = (int*)addr;
2779 mtx_lock(&softc->ctl_lock);
2780 if (softc->flags & CTL_FLAG_REAL_SYNC)
2784 mtx_unlock(&softc->ctl_lock);
2790 struct ctl_sync_info *sync_info;
2792 sync_info = (struct ctl_sync_info *)addr;
2794 mtx_lock(&softc->ctl_lock);
2795 lun = softc->ctl_luns[sync_info->lun_id];
2797 mtx_unlock(&softc->ctl_lock);
2798 sync_info->status = CTL_GS_SYNC_NO_LUN;
2802 * Get or set the sync interval. We're not bounds checking
2803 * in the set case, hopefully the user won't do something
2806 mtx_lock(&lun->lun_lock);
2807 mtx_unlock(&softc->ctl_lock);
2808 if (cmd == CTL_GETSYNC)
2809 sync_info->sync_interval = lun->sync_interval;
2811 lun->sync_interval = sync_info->sync_interval;
2812 mtx_unlock(&lun->lun_lock);
2814 sync_info->status = CTL_GS_SYNC_OK;
2818 case CTL_GETSTATS: {
2819 struct ctl_stats *stats;
2822 stats = (struct ctl_stats *)addr;
2824 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2826 stats->status = CTL_SS_NEED_MORE_SPACE;
2827 stats->num_luns = softc->num_luns;
2831 * XXX KDM no locking here. If the LUN list changes,
2832 * things can blow up.
2834 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
2835 i++, lun = STAILQ_NEXT(lun, links)) {
2836 retval = copyout(&lun->stats, &stats->lun_stats[i],
2837 sizeof(lun->stats));
2841 stats->num_luns = softc->num_luns;
2842 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2844 stats->status = CTL_SS_OK;
2846 stats->flags = CTL_STATS_FLAG_TIME_VALID;
2848 stats->flags = CTL_STATS_FLAG_NONE;
2850 getnanouptime(&stats->timestamp);
2853 case CTL_ERROR_INJECT: {
2854 struct ctl_error_desc *err_desc, *new_err_desc;
2856 err_desc = (struct ctl_error_desc *)addr;
2858 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2860 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2862 mtx_lock(&softc->ctl_lock);
2863 lun = softc->ctl_luns[err_desc->lun_id];
2865 mtx_unlock(&softc->ctl_lock);
2866 free(new_err_desc, M_CTL);
2867 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2868 __func__, (uintmax_t)err_desc->lun_id);
2872 mtx_lock(&lun->lun_lock);
2873 mtx_unlock(&softc->ctl_lock);
2876 * We could do some checking here to verify the validity
2877 * of the request, but given the complexity of error
2878 * injection requests, the checking logic would be fairly
2881 * For now, if the request is invalid, it just won't get
2882 * executed and might get deleted.
2884 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2887 * XXX KDM check to make sure the serial number is unique,
2888 * in case we somehow manage to wrap. That shouldn't
2889 * happen for a very long time, but it's the right thing to
2892 new_err_desc->serial = lun->error_serial;
2893 err_desc->serial = lun->error_serial;
2894 lun->error_serial++;
2896 mtx_unlock(&lun->lun_lock);
2899 case CTL_ERROR_INJECT_DELETE: {
2900 struct ctl_error_desc *delete_desc, *desc, *desc2;
2903 delete_desc = (struct ctl_error_desc *)addr;
2906 mtx_lock(&softc->ctl_lock);
2907 lun = softc->ctl_luns[delete_desc->lun_id];
2909 mtx_unlock(&softc->ctl_lock);
2910 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2911 __func__, (uintmax_t)delete_desc->lun_id);
2915 mtx_lock(&lun->lun_lock);
2916 mtx_unlock(&softc->ctl_lock);
2917 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2918 if (desc->serial != delete_desc->serial)
2921 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2926 mtx_unlock(&lun->lun_lock);
2927 if (delete_done == 0) {
2928 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2929 "error serial %ju on LUN %u\n", __func__,
2930 delete_desc->serial, delete_desc->lun_id);
2936 case CTL_DUMP_STRUCTS: {
2938 struct ctl_port *port;
2939 struct ctl_frontend *fe;
2941 mtx_lock(&softc->ctl_lock);
2942 printf("CTL Persistent Reservation information start:\n");
2943 for (i = 0; i < CTL_MAX_LUNS; i++) {
2944 lun = softc->ctl_luns[i];
2947 || ((lun->flags & CTL_LUN_DISABLED) != 0))
2950 for (j = 0; j < CTL_MAX_PORTS; j++) {
2951 if (lun->pr_keys[j] == NULL)
2953 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2954 if (lun->pr_keys[j][k] == 0)
2956 printf(" LUN %d port %d iid %d key "
2958 (uintmax_t)lun->pr_keys[j][k]);
2962 printf("CTL Persistent Reservation information end\n");
2963 printf("CTL Ports:\n");
2964 STAILQ_FOREACH(port, &softc->port_list, links) {
2965 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
2966 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
2967 port->frontend->name, port->port_type,
2968 port->physical_port, port->virtual_port,
2969 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
2970 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2971 if (port->wwpn_iid[j].in_use == 0 &&
2972 port->wwpn_iid[j].wwpn == 0 &&
2973 port->wwpn_iid[j].name == NULL)
2976 printf(" iid %u use %d WWPN %#jx '%s'\n",
2977 j, port->wwpn_iid[j].in_use,
2978 (uintmax_t)port->wwpn_iid[j].wwpn,
2979 port->wwpn_iid[j].name);
2982 printf("CTL Port information end\n");
2983 mtx_unlock(&softc->ctl_lock);
2985 * XXX KDM calling this without a lock. We'd likely want
2986 * to drop the lock before calling the frontend's dump
2989 printf("CTL Frontends:\n");
2990 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2991 printf(" Frontend '%s'\n", fe->name);
2992 if (fe->fe_dump != NULL)
2995 printf("CTL Frontend information end\n");
2999 struct ctl_lun_req *lun_req;
3000 struct ctl_backend_driver *backend;
3002 lun_req = (struct ctl_lun_req *)addr;
3004 backend = ctl_backend_find(lun_req->backend);
3005 if (backend == NULL) {
3006 lun_req->status = CTL_LUN_ERROR;
3007 snprintf(lun_req->error_str,
3008 sizeof(lun_req->error_str),
3009 "Backend \"%s\" not found.",
3013 if (lun_req->num_be_args > 0) {
3014 lun_req->kern_be_args = ctl_copyin_args(
3015 lun_req->num_be_args,
3018 sizeof(lun_req->error_str));
3019 if (lun_req->kern_be_args == NULL) {
3020 lun_req->status = CTL_LUN_ERROR;
3025 retval = backend->ioctl(dev, cmd, addr, flag, td);
3027 if (lun_req->num_be_args > 0) {
3028 ctl_copyout_args(lun_req->num_be_args,
3029 lun_req->kern_be_args);
3030 ctl_free_args(lun_req->num_be_args,
3031 lun_req->kern_be_args);
3035 case CTL_LUN_LIST: {
3037 struct ctl_lun_list *list;
3038 struct ctl_option *opt;
3040 list = (struct ctl_lun_list *)addr;
3043 * Allocate a fixed length sbuf here, based on the length
3044 * of the user's buffer. We could allocate an auto-extending
3045 * buffer, and then tell the user how much larger our
3046 * amount of data is than his buffer, but that presents
3049 * 1. The sbuf(9) routines use a blocking malloc, and so
3050 * we can't hold a lock while calling them with an
3051 * auto-extending buffer.
3053 * 2. There is not currently a LUN reference counting
3054 * mechanism, outside of outstanding transactions on
3055 * the LUN's OOA queue. So a LUN could go away on us
3056 * while we're getting the LUN number, backend-specific
3057 * information, etc. Thus, given the way things
3058 * currently work, we need to hold the CTL lock while
3059 * grabbing LUN information.
3061 * So, from the user's standpoint, the best thing to do is
3062 * allocate what he thinks is a reasonable buffer length,
3063 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3064 * double the buffer length and try again. (And repeat
3065 * that until he succeeds.)
3067 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3069 list->status = CTL_LUN_LIST_ERROR;
3070 snprintf(list->error_str, sizeof(list->error_str),
3071 "Unable to allocate %d bytes for LUN list",
3076 sbuf_printf(sb, "<ctllunlist>\n");
3078 mtx_lock(&softc->ctl_lock);
3079 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3080 mtx_lock(&lun->lun_lock);
3081 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3082 (uintmax_t)lun->lun);
3085 * Bail out as soon as we see that we've overfilled
3091 retval = sbuf_printf(sb, "\t<backend_type>%s"
3092 "</backend_type>\n",
3093 (lun->backend == NULL) ? "none" :
3094 lun->backend->name);
3099 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3100 lun->be_lun->lun_type);
3105 if (lun->backend == NULL) {
3106 retval = sbuf_printf(sb, "</lun>\n");
3112 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3113 (lun->be_lun->maxlba > 0) ?
3114 lun->be_lun->maxlba + 1 : 0);
3119 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3120 lun->be_lun->blocksize);
3125 retval = sbuf_printf(sb, "\t<serial_number>");
3130 retval = ctl_sbuf_printf_esc(sb,
3131 lun->be_lun->serial_num,
3132 sizeof(lun->be_lun->serial_num));
3137 retval = sbuf_printf(sb, "</serial_number>\n");
3142 retval = sbuf_printf(sb, "\t<device_id>");
3147 retval = ctl_sbuf_printf_esc(sb,
3148 lun->be_lun->device_id,
3149 sizeof(lun->be_lun->device_id));
3154 retval = sbuf_printf(sb, "</device_id>\n");
3159 if (lun->backend->lun_info != NULL) {
3160 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3164 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3165 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3166 opt->name, opt->value, opt->name);
3171 retval = sbuf_printf(sb, "</lun>\n");
3175 mtx_unlock(&lun->lun_lock);
3178 mtx_unlock(&lun->lun_lock);
3179 mtx_unlock(&softc->ctl_lock);
3182 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3185 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3186 snprintf(list->error_str, sizeof(list->error_str),
3187 "Out of space, %d bytes is too small",
3194 retval = copyout(sbuf_data(sb), list->lun_xml,
3197 list->fill_len = sbuf_len(sb) + 1;
3198 list->status = CTL_LUN_LIST_OK;
3203 struct ctl_iscsi *ci;
3204 struct ctl_frontend *fe;
3206 ci = (struct ctl_iscsi *)addr;
3208 fe = ctl_frontend_find("iscsi");
3210 ci->status = CTL_ISCSI_ERROR;
3211 snprintf(ci->error_str, sizeof(ci->error_str),
3212 "Frontend \"iscsi\" not found.");
3216 retval = fe->ioctl(dev, cmd, addr, flag, td);
3219 case CTL_PORT_REQ: {
3220 struct ctl_req *req;
3221 struct ctl_frontend *fe;
3223 req = (struct ctl_req *)addr;
3225 fe = ctl_frontend_find(req->driver);
3227 req->status = CTL_LUN_ERROR;
3228 snprintf(req->error_str, sizeof(req->error_str),
3229 "Frontend \"%s\" not found.", req->driver);
3232 if (req->num_args > 0) {
3233 req->kern_args = ctl_copyin_args(req->num_args,
3234 req->args, req->error_str, sizeof(req->error_str));
3235 if (req->kern_args == NULL) {
3236 req->status = CTL_LUN_ERROR;
3242 retval = fe->ioctl(dev, cmd, addr, flag, td);
3246 if (req->num_args > 0) {
3247 ctl_copyout_args(req->num_args, req->kern_args);
3248 ctl_free_args(req->num_args, req->kern_args);
3252 case CTL_PORT_LIST: {
3254 struct ctl_port *port;
3255 struct ctl_lun_list *list;
3256 struct ctl_option *opt;
3260 list = (struct ctl_lun_list *)addr;
3262 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3264 list->status = CTL_LUN_LIST_ERROR;
3265 snprintf(list->error_str, sizeof(list->error_str),
3266 "Unable to allocate %d bytes for LUN list",
3271 sbuf_printf(sb, "<ctlportlist>\n");
3273 mtx_lock(&softc->ctl_lock);
3274 STAILQ_FOREACH(port, &softc->port_list, links) {
3275 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3276 (uintmax_t)port->targ_port);
3279 * Bail out as soon as we see that we've overfilled
3285 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3286 "</frontend_type>\n", port->frontend->name);
3290 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3295 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3296 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3300 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3305 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3306 port->physical_port);
3310 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3311 port->virtual_port);
3315 if (port->target_devid != NULL) {
3316 sbuf_printf(sb, "\t<target>");
3317 ctl_id_sbuf(port->target_devid, sb);
3318 sbuf_printf(sb, "</target>\n");
3321 if (port->port_devid != NULL) {
3322 sbuf_printf(sb, "\t<port>");
3323 ctl_id_sbuf(port->port_devid, sb);
3324 sbuf_printf(sb, "</port>\n");
3327 if (port->port_info != NULL) {
3328 retval = port->port_info(port->onoff_arg, sb);
3332 STAILQ_FOREACH(opt, &port->options, links) {
3333 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3334 opt->name, opt->value, opt->name);
3339 if (port->lun_map != NULL) {
3340 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n");
3341 for (j = 0; j < CTL_MAX_LUNS; j++) {
3342 plun = ctl_lun_map_from_port(port, j);
3343 if (plun >= CTL_MAX_LUNS)
3346 "\t<lun id=\"%u\">%u</lun>\n",
3351 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3352 if (port->wwpn_iid[j].in_use == 0 ||
3353 (port->wwpn_iid[j].wwpn == 0 &&
3354 port->wwpn_iid[j].name == NULL))
3357 if (port->wwpn_iid[j].name != NULL)
3358 retval = sbuf_printf(sb,
3359 "\t<initiator id=\"%u\">%s</initiator>\n",
3360 j, port->wwpn_iid[j].name);
3362 retval = sbuf_printf(sb,
3363 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3364 j, port->wwpn_iid[j].wwpn);
3371 retval = sbuf_printf(sb, "</targ_port>\n");
3375 mtx_unlock(&softc->ctl_lock);
3378 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3381 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3382 snprintf(list->error_str, sizeof(list->error_str),
3383 "Out of space, %d bytes is too small",
3390 retval = copyout(sbuf_data(sb), list->lun_xml,
3393 list->fill_len = sbuf_len(sb) + 1;
3394 list->status = CTL_LUN_LIST_OK;
3399 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr;
3400 struct ctl_port *port;
3402 mtx_lock(&softc->ctl_lock);
3403 if (lm->port < softc->port_min ||
3404 lm->port >= softc->port_max ||
3405 (port = softc->ctl_ports[lm->port]) == NULL) {
3406 mtx_unlock(&softc->ctl_lock);
3409 if (port->status & CTL_PORT_STATUS_ONLINE) {
3410 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3411 if (ctl_lun_map_to_port(port, lun->lun) >=
3414 mtx_lock(&lun->lun_lock);
3415 ctl_est_ua_port(lun, lm->port, -1,
3417 mtx_unlock(&lun->lun_lock);
3420 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps
3421 if (lm->plun < CTL_MAX_LUNS) {
3422 if (lm->lun == UINT32_MAX)
3423 retval = ctl_lun_map_unset(port, lm->plun);
3424 else if (lm->lun < CTL_MAX_LUNS &&
3425 softc->ctl_luns[lm->lun] != NULL)
3426 retval = ctl_lun_map_set(port, lm->plun, lm->lun);
3429 } else if (lm->plun == UINT32_MAX) {
3430 if (lm->lun == UINT32_MAX)
3431 retval = ctl_lun_map_deinit(port);
3433 retval = ctl_lun_map_init(port);
3436 if (port->status & CTL_PORT_STATUS_ONLINE)
3437 ctl_isc_announce_port(port);
3441 /* XXX KDM should we fix this? */
3443 struct ctl_backend_driver *backend;
3450 * We encode the backend type as the ioctl type for backend
3451 * ioctls. So parse it out here, and then search for a
3452 * backend of this type.
3454 type = _IOC_TYPE(cmd);
3456 STAILQ_FOREACH(backend, &softc->be_list, links) {
3457 if (backend->type == type) {
3463 printf("ctl: unknown ioctl command %#lx or backend "
3468 retval = backend->ioctl(dev, cmd, addr, flag, td);
3478 ctl_get_initindex(struct ctl_nexus *nexus)
3480 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3484 ctl_lun_map_init(struct ctl_port *port)
3486 struct ctl_softc *softc = control_softc;
3487 struct ctl_lun *lun;
3490 if (port->lun_map == NULL)
3491 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS,
3493 if (port->lun_map == NULL)
3495 for (i = 0; i < CTL_MAX_LUNS; i++)
3496 port->lun_map[i] = UINT32_MAX;
3497 if (port->status & CTL_PORT_STATUS_ONLINE) {
3498 if (port->lun_disable != NULL) {
3499 STAILQ_FOREACH(lun, &softc->lun_list, links)
3500 port->lun_disable(port->targ_lun_arg, lun->lun);
3502 ctl_isc_announce_port(port);
3508 ctl_lun_map_deinit(struct ctl_port *port)
3510 struct ctl_softc *softc = control_softc;
3511 struct ctl_lun *lun;
3513 if (port->lun_map == NULL)
3515 free(port->lun_map, M_CTL);
3516 port->lun_map = NULL;
3517 if (port->status & CTL_PORT_STATUS_ONLINE) {
3518 if (port->lun_enable != NULL) {
3519 STAILQ_FOREACH(lun, &softc->lun_list, links)
3520 port->lun_enable(port->targ_lun_arg, lun->lun);
3522 ctl_isc_announce_port(port);
3528 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun)
3533 if (port->lun_map == NULL) {
3534 status = ctl_lun_map_init(port);
3538 old = port->lun_map[plun];
3539 port->lun_map[plun] = glun;
3540 if ((port->status & CTL_PORT_STATUS_ONLINE) && old >= CTL_MAX_LUNS) {
3541 if (port->lun_enable != NULL)
3542 port->lun_enable(port->targ_lun_arg, plun);
3543 ctl_isc_announce_port(port);
3549 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun)
3553 if (port->lun_map == NULL)
3555 old = port->lun_map[plun];
3556 port->lun_map[plun] = UINT32_MAX;
3557 if ((port->status & CTL_PORT_STATUS_ONLINE) && old < CTL_MAX_LUNS) {
3558 if (port->lun_disable != NULL)
3559 port->lun_disable(port->targ_lun_arg, plun);
3560 ctl_isc_announce_port(port);
3566 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
3570 return (UINT32_MAX);
3571 if (port->lun_map == NULL || lun_id >= CTL_MAX_LUNS)
3573 return (port->lun_map[lun_id]);
3577 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
3582 return (UINT32_MAX);
3583 if (port->lun_map == NULL)
3585 for (i = 0; i < CTL_MAX_LUNS; i++) {
3586 if (port->lun_map[i] == lun_id)
3589 return (UINT32_MAX);
3592 static struct ctl_port *
3593 ctl_io_port(struct ctl_io_hdr *io_hdr)
3596 return (control_softc->ctl_ports[io_hdr->nexus.targ_port]);
3600 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last)
3604 for (i = first; i < last; i++) {
3605 if ((mask[i / 32] & (1 << (i % 32))) == 0)
3612 ctl_set_mask(uint32_t *mask, uint32_t bit)
3614 uint32_t chunk, piece;
3617 piece = bit % (sizeof(uint32_t) * 8);
3619 if ((mask[chunk] & (1 << piece)) != 0)
3622 mask[chunk] |= (1 << piece);
3628 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3630 uint32_t chunk, piece;
3633 piece = bit % (sizeof(uint32_t) * 8);
3635 if ((mask[chunk] & (1 << piece)) == 0)
3638 mask[chunk] &= ~(1 << piece);
3644 ctl_is_set(uint32_t *mask, uint32_t bit)
3646 uint32_t chunk, piece;
3649 piece = bit % (sizeof(uint32_t) * 8);
3651 if ((mask[chunk] & (1 << piece)) == 0)
3658 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3662 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3665 return (t[residx % CTL_MAX_INIT_PER_PORT]);
3669 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3673 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3676 t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3680 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3685 i = residx/CTL_MAX_INIT_PER_PORT;
3686 if (lun->pr_keys[i] != NULL)
3688 mtx_unlock(&lun->lun_lock);
3689 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3691 mtx_lock(&lun->lun_lock);
3692 if (lun->pr_keys[i] == NULL)
3693 lun->pr_keys[i] = p;
3699 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3703 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3704 KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3705 t[residx % CTL_MAX_INIT_PER_PORT] = key;
3709 * ctl_softc, pool_name, total_ctl_io are passed in.
3710 * npool is passed out.
3713 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3714 uint32_t total_ctl_io, void **npool)
3717 struct ctl_io_pool *pool;
3719 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3724 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3725 pool->ctl_softc = ctl_softc;
3726 pool->zone = uma_zsecond_create(pool->name, NULL,
3727 NULL, NULL, NULL, ctl_softc->io_zone);
3728 /* uma_prealloc(pool->zone, total_ctl_io); */
3732 *npool = ctl_softc->io_zone;
3738 ctl_pool_free(struct ctl_io_pool *pool)
3745 uma_zdestroy(pool->zone);
3751 ctl_alloc_io(void *pool_ref)
3755 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3757 io = uma_zalloc(pool->zone, M_WAITOK);
3759 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK);
3762 io->io_hdr.pool = pool_ref;
3767 ctl_alloc_io_nowait(void *pool_ref)
3771 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3773 io = uma_zalloc(pool->zone, M_NOWAIT);
3775 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT);
3778 io->io_hdr.pool = pool_ref;
3783 ctl_free_io(union ctl_io *io)
3786 struct ctl_io_pool *pool;
3793 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3794 uma_zfree(pool->zone, io);
3796 uma_zfree((uma_zone_t)io->io_hdr.pool, io);
3801 ctl_zero_io(union ctl_io *io)
3809 * May need to preserve linked list pointers at some point too.
3811 pool_ref = io->io_hdr.pool;
3812 memset(io, 0, sizeof(*io));
3813 io->io_hdr.pool = pool_ref;
3817 * This routine is currently used for internal copies of ctl_ios that need
3818 * to persist for some reason after we've already returned status to the
3819 * FETD. (Thus the flag set.)
3822 * Note that this makes a blind copy of all fields in the ctl_io, except
3823 * for the pool reference. This includes any memory that has been
3824 * allocated! That memory will no longer be valid after done has been
3825 * called, so this would be VERY DANGEROUS for command that actually does
3826 * any reads or writes. Right now (11/7/2005), this is only used for immediate
3827 * start and stop commands, which don't transfer any data, so this is not a
3828 * problem. If it is used for anything else, the caller would also need to
3829 * allocate data buffer space and this routine would need to be modified to
3830 * copy the data buffer(s) as well.
3833 ctl_copy_io(union ctl_io *src, union ctl_io *dest)
3842 * May need to preserve linked list pointers at some point too.
3844 pool_ref = dest->io_hdr.pool;
3846 memcpy(dest, src, MIN(sizeof(*src), sizeof(*dest)));
3848 dest->io_hdr.pool = pool_ref;
3850 * We need to know that this is an internal copy, and doesn't need
3851 * to get passed back to the FETD that allocated it.
3853 dest->io_hdr.flags |= CTL_FLAG_INT_COPY;
3857 ctl_expand_number(const char *buf, uint64_t *num)
3863 number = strtoq(buf, &endptr, 0);
3865 switch (tolower((unsigned char)*endptr)) {
3885 case '\0': /* No unit. */
3889 /* Unrecognized unit. */
3893 if ((number << shift) >> shift != number) {
3897 *num = number << shift;
3903 * This routine could be used in the future to load default and/or saved
3904 * mode page parameters for a particuar lun.
3907 ctl_init_page_index(struct ctl_lun *lun)
3910 struct ctl_page_index *page_index;
3914 memcpy(&lun->mode_pages.index, page_index_template,
3915 sizeof(page_index_template));
3917 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3919 page_index = &lun->mode_pages.index[i];
3921 * If this is a disk-only mode page, there's no point in
3922 * setting it up. For some pages, we have to have some
3923 * basic information about the disk in order to calculate the
3926 if ((lun->be_lun->lun_type != T_DIRECT)
3927 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
3930 switch (page_index->page_code & SMPH_PC_MASK) {
3931 case SMS_RW_ERROR_RECOVERY_PAGE: {
3932 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3933 panic("subpage is incorrect!");
3934 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
3935 &rw_er_page_default,
3936 sizeof(rw_er_page_default));
3937 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
3938 &rw_er_page_changeable,
3939 sizeof(rw_er_page_changeable));
3940 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
3941 &rw_er_page_default,
3942 sizeof(rw_er_page_default));
3943 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
3944 &rw_er_page_default,
3945 sizeof(rw_er_page_default));
3946 page_index->page_data =
3947 (uint8_t *)lun->mode_pages.rw_er_page;
3950 case SMS_FORMAT_DEVICE_PAGE: {
3951 struct scsi_format_page *format_page;
3953 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3954 panic("subpage is incorrect!");
3957 * Sectors per track are set above. Bytes per
3958 * sector need to be set here on a per-LUN basis.
3960 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
3961 &format_page_default,
3962 sizeof(format_page_default));
3963 memcpy(&lun->mode_pages.format_page[
3964 CTL_PAGE_CHANGEABLE], &format_page_changeable,
3965 sizeof(format_page_changeable));
3966 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
3967 &format_page_default,
3968 sizeof(format_page_default));
3969 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
3970 &format_page_default,
3971 sizeof(format_page_default));
3973 format_page = &lun->mode_pages.format_page[
3975 scsi_ulto2b(lun->be_lun->blocksize,
3976 format_page->bytes_per_sector);
3978 format_page = &lun->mode_pages.format_page[
3980 scsi_ulto2b(lun->be_lun->blocksize,
3981 format_page->bytes_per_sector);
3983 format_page = &lun->mode_pages.format_page[
3985 scsi_ulto2b(lun->be_lun->blocksize,
3986 format_page->bytes_per_sector);
3988 page_index->page_data =
3989 (uint8_t *)lun->mode_pages.format_page;
3992 case SMS_RIGID_DISK_PAGE: {
3993 struct scsi_rigid_disk_page *rigid_disk_page;
3994 uint32_t sectors_per_cylinder;
3998 #endif /* !__XSCALE__ */
4000 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4001 panic("invalid subpage value %d",
4002 page_index->subpage);
4005 * Rotation rate and sectors per track are set
4006 * above. We calculate the cylinders here based on
4007 * capacity. Due to the number of heads and
4008 * sectors per track we're using, smaller arrays
4009 * may turn out to have 0 cylinders. Linux and
4010 * FreeBSD don't pay attention to these mode pages
4011 * to figure out capacity, but Solaris does. It
4012 * seems to deal with 0 cylinders just fine, and
4013 * works out a fake geometry based on the capacity.
4015 memcpy(&lun->mode_pages.rigid_disk_page[
4016 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4017 sizeof(rigid_disk_page_default));
4018 memcpy(&lun->mode_pages.rigid_disk_page[
4019 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4020 sizeof(rigid_disk_page_changeable));
4022 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4026 * The divide method here will be more accurate,
4027 * probably, but results in floating point being
4028 * used in the kernel on i386 (__udivdi3()). On the
4029 * XScale, though, __udivdi3() is implemented in
4032 * The shift method for cylinder calculation is
4033 * accurate if sectors_per_cylinder is a power of
4034 * 2. Otherwise it might be slightly off -- you
4035 * might have a bit of a truncation problem.
4038 cylinders = (lun->be_lun->maxlba + 1) /
4039 sectors_per_cylinder;
4041 for (shift = 31; shift > 0; shift--) {
4042 if (sectors_per_cylinder & (1 << shift))
4045 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4049 * We've basically got 3 bytes, or 24 bits for the
4050 * cylinder size in the mode page. If we're over,
4051 * just round down to 2^24.
4053 if (cylinders > 0xffffff)
4054 cylinders = 0xffffff;
4056 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4058 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4060 if ((value = ctl_get_opt(&lun->be_lun->options,
4062 scsi_ulto2b(strtol(value, NULL, 0),
4063 rigid_disk_page->rotation_rate);
4066 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
4067 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4068 sizeof(rigid_disk_page_default));
4069 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
4070 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4071 sizeof(rigid_disk_page_default));
4073 page_index->page_data =
4074 (uint8_t *)lun->mode_pages.rigid_disk_page;
4077 case SMS_CACHING_PAGE: {
4078 struct scsi_caching_page *caching_page;
4080 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4081 panic("invalid subpage value %d",
4082 page_index->subpage);
4083 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4084 &caching_page_default,
4085 sizeof(caching_page_default));
4086 memcpy(&lun->mode_pages.caching_page[
4087 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4088 sizeof(caching_page_changeable));
4089 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4090 &caching_page_default,
4091 sizeof(caching_page_default));
4092 caching_page = &lun->mode_pages.caching_page[
4094 value = ctl_get_opt(&lun->be_lun->options, "writecache");
4095 if (value != NULL && strcmp(value, "off") == 0)
4096 caching_page->flags1 &= ~SCP_WCE;
4097 value = ctl_get_opt(&lun->be_lun->options, "readcache");
4098 if (value != NULL && strcmp(value, "off") == 0)
4099 caching_page->flags1 |= SCP_RCD;
4100 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4101 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4102 sizeof(caching_page_default));
4103 page_index->page_data =
4104 (uint8_t *)lun->mode_pages.caching_page;
4107 case SMS_CONTROL_MODE_PAGE: {
4108 switch (page_index->subpage) {
4109 case SMS_SUBPAGE_PAGE_0: {
4110 struct scsi_control_page *control_page;
4112 memcpy(&lun->mode_pages.control_page[
4114 &control_page_default,
4115 sizeof(control_page_default));
4116 memcpy(&lun->mode_pages.control_page[
4117 CTL_PAGE_CHANGEABLE],
4118 &control_page_changeable,
4119 sizeof(control_page_changeable));
4120 memcpy(&lun->mode_pages.control_page[
4122 &control_page_default,
4123 sizeof(control_page_default));
4124 control_page = &lun->mode_pages.control_page[
4126 value = ctl_get_opt(&lun->be_lun->options,
4128 if (value != NULL &&
4129 strcmp(value, "unrestricted") == 0) {
4130 control_page->queue_flags &=
4131 ~SCP_QUEUE_ALG_MASK;
4132 control_page->queue_flags |=
4133 SCP_QUEUE_ALG_UNRESTRICTED;
4135 memcpy(&lun->mode_pages.control_page[
4137 &lun->mode_pages.control_page[
4139 sizeof(control_page_default));
4140 page_index->page_data =
4141 (uint8_t *)lun->mode_pages.control_page;
4145 memcpy(&lun->mode_pages.control_ext_page[
4147 &control_ext_page_default,
4148 sizeof(control_ext_page_default));
4149 memcpy(&lun->mode_pages.control_ext_page[
4150 CTL_PAGE_CHANGEABLE],
4151 &control_ext_page_changeable,
4152 sizeof(control_ext_page_changeable));
4153 memcpy(&lun->mode_pages.control_ext_page[
4155 &control_ext_page_default,
4156 sizeof(control_ext_page_default));
4157 memcpy(&lun->mode_pages.control_ext_page[
4159 &lun->mode_pages.control_ext_page[
4161 sizeof(control_ext_page_default));
4162 page_index->page_data =
4163 (uint8_t *)lun->mode_pages.control_ext_page;
4168 case SMS_INFO_EXCEPTIONS_PAGE: {
4169 switch (page_index->subpage) {
4170 case SMS_SUBPAGE_PAGE_0:
4171 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4173 sizeof(ie_page_default));
4174 memcpy(&lun->mode_pages.ie_page[
4175 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4176 sizeof(ie_page_changeable));
4177 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4179 sizeof(ie_page_default));
4180 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4182 sizeof(ie_page_default));
4183 page_index->page_data =
4184 (uint8_t *)lun->mode_pages.ie_page;
4187 struct ctl_logical_block_provisioning_page *page;
4189 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4191 sizeof(lbp_page_default));
4192 memcpy(&lun->mode_pages.lbp_page[
4193 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4194 sizeof(lbp_page_changeable));
4195 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4197 sizeof(lbp_page_default));
4198 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4199 value = ctl_get_opt(&lun->be_lun->options,
4201 if (value != NULL &&
4202 ctl_expand_number(value, &ival) == 0) {
4203 page->descr[0].flags |= SLBPPD_ENABLED |
4205 if (lun->be_lun->blocksize)
4206 ival /= lun->be_lun->blocksize;
4209 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4210 page->descr[0].count);
4212 value = ctl_get_opt(&lun->be_lun->options,
4214 if (value != NULL &&
4215 ctl_expand_number(value, &ival) == 0) {
4216 page->descr[1].flags |= SLBPPD_ENABLED |
4218 if (lun->be_lun->blocksize)
4219 ival /= lun->be_lun->blocksize;
4222 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4223 page->descr[1].count);
4225 value = ctl_get_opt(&lun->be_lun->options,
4226 "pool-avail-threshold");
4227 if (value != NULL &&
4228 ctl_expand_number(value, &ival) == 0) {
4229 page->descr[2].flags |= SLBPPD_ENABLED |
4231 if (lun->be_lun->blocksize)
4232 ival /= lun->be_lun->blocksize;
4235 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4236 page->descr[2].count);
4238 value = ctl_get_opt(&lun->be_lun->options,
4239 "pool-used-threshold");
4240 if (value != NULL &&
4241 ctl_expand_number(value, &ival) == 0) {
4242 page->descr[3].flags |= SLBPPD_ENABLED |
4244 if (lun->be_lun->blocksize)
4245 ival /= lun->be_lun->blocksize;
4248 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4249 page->descr[3].count);
4251 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4252 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4253 sizeof(lbp_page_default));
4254 page_index->page_data =
4255 (uint8_t *)lun->mode_pages.lbp_page;
4259 case SMS_VENDOR_SPECIFIC_PAGE:{
4260 switch (page_index->subpage) {
4261 case DBGCNF_SUBPAGE_CODE: {
4262 struct copan_debugconf_subpage *current_page,
4265 memcpy(&lun->mode_pages.debugconf_subpage[
4267 &debugconf_page_default,
4268 sizeof(debugconf_page_default));
4269 memcpy(&lun->mode_pages.debugconf_subpage[
4270 CTL_PAGE_CHANGEABLE],
4271 &debugconf_page_changeable,
4272 sizeof(debugconf_page_changeable));
4273 memcpy(&lun->mode_pages.debugconf_subpage[
4275 &debugconf_page_default,
4276 sizeof(debugconf_page_default));
4277 memcpy(&lun->mode_pages.debugconf_subpage[
4279 &debugconf_page_default,
4280 sizeof(debugconf_page_default));
4281 page_index->page_data =
4282 (uint8_t *)lun->mode_pages.debugconf_subpage;
4284 current_page = (struct copan_debugconf_subpage *)
4285 (page_index->page_data +
4286 (page_index->page_len *
4288 saved_page = (struct copan_debugconf_subpage *)
4289 (page_index->page_data +
4290 (page_index->page_len *
4295 panic("invalid subpage value %d",
4296 page_index->subpage);
4302 panic("invalid page value %d",
4303 page_index->page_code & SMPH_PC_MASK);
4308 return (CTL_RETVAL_COMPLETE);
4312 ctl_init_log_page_index(struct ctl_lun *lun)
4314 struct ctl_page_index *page_index;
4317 memcpy(&lun->log_pages.index, log_page_index_template,
4318 sizeof(log_page_index_template));
4321 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4323 page_index = &lun->log_pages.index[i];
4325 * If this is a disk-only mode page, there's no point in
4326 * setting it up. For some pages, we have to have some
4327 * basic information about the disk in order to calculate the
4330 if ((lun->be_lun->lun_type != T_DIRECT)
4331 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
4334 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4335 lun->backend->lun_attr == NULL)
4338 if (page_index->page_code != prev) {
4339 lun->log_pages.pages_page[j] = page_index->page_code;
4340 prev = page_index->page_code;
4343 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4344 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4347 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4348 lun->log_pages.index[0].page_len = j;
4349 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4350 lun->log_pages.index[1].page_len = k * 2;
4351 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4352 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4353 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page;
4354 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page);
4356 return (CTL_RETVAL_COMPLETE);
4360 hex2bin(const char *str, uint8_t *buf, int buf_size)
4365 memset(buf, 0, buf_size);
4366 while (isspace(str[0]))
4368 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4371 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4375 else if (isalpha(c))
4376 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4382 buf[i / 2] |= (c << 4);
4386 return ((i + 1) / 2);
4393 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4394 * wants us to allocate the LUN and he can block.
4395 * - ctl_softc is always set
4396 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4398 * Returns 0 for success, non-zero (errno) for failure.
4401 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4402 struct ctl_be_lun *const be_lun)
4404 struct ctl_lun *nlun, *lun;
4405 struct scsi_vpd_id_descriptor *desc;
4406 struct scsi_vpd_id_t10 *t10id;
4407 const char *eui, *naa, *scsiname, *vendor;
4408 int lun_number, i, lun_malloced;
4409 int devidlen, idlen1, idlen2 = 0, len;
4415 * We currently only support Direct Access or Processor LUN types.
4417 switch (be_lun->lun_type) {
4425 be_lun->lun_config_status(be_lun->be_lun,
4426 CTL_LUN_CONFIG_FAILURE);
4429 if (ctl_lun == NULL) {
4430 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4437 memset(lun, 0, sizeof(*lun));
4439 lun->flags = CTL_LUN_MALLOCED;
4441 /* Generate LUN ID. */
4442 devidlen = max(CTL_DEVID_MIN_LEN,
4443 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4444 idlen1 = sizeof(*t10id) + devidlen;
4445 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4446 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4447 if (scsiname != NULL) {
4448 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4449 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4451 eui = ctl_get_opt(&be_lun->options, "eui");
4453 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4455 naa = ctl_get_opt(&be_lun->options, "naa");
4457 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4459 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4460 M_CTL, M_WAITOK | M_ZERO);
4461 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4462 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4463 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4464 desc->length = idlen1;
4465 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4466 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4467 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4468 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4470 strncpy(t10id->vendor, vendor,
4471 min(sizeof(t10id->vendor), strlen(vendor)));
4473 strncpy((char *)t10id->vendor_spec_id,
4474 (char *)be_lun->device_id, devidlen);
4475 if (scsiname != NULL) {
4476 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4478 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4479 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4480 SVPD_ID_TYPE_SCSI_NAME;
4481 desc->length = idlen2;
4482 strlcpy(desc->identifier, scsiname, idlen2);
4485 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4487 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4488 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4490 desc->length = hex2bin(eui, desc->identifier, 16);
4491 desc->length = desc->length > 12 ? 16 :
4492 (desc->length > 8 ? 12 : 8);
4493 len -= 16 - desc->length;
4496 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4498 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4499 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4501 desc->length = hex2bin(naa, desc->identifier, 16);
4502 desc->length = desc->length > 8 ? 16 : 8;
4503 len -= 16 - desc->length;
4505 lun->lun_devid->len = len;
4507 mtx_lock(&ctl_softc->ctl_lock);
4509 * See if the caller requested a particular LUN number. If so, see
4510 * if it is available. Otherwise, allocate the first available LUN.
4512 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4513 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4514 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4515 mtx_unlock(&ctl_softc->ctl_lock);
4516 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4517 printf("ctl: requested LUN ID %d is higher "
4518 "than CTL_MAX_LUNS - 1 (%d)\n",
4519 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4522 * XXX KDM return an error, or just assign
4523 * another LUN ID in this case??
4525 printf("ctl: requested LUN ID %d is already "
4526 "in use\n", be_lun->req_lun_id);
4528 if (lun->flags & CTL_LUN_MALLOCED)
4530 be_lun->lun_config_status(be_lun->be_lun,
4531 CTL_LUN_CONFIG_FAILURE);
4534 lun_number = be_lun->req_lun_id;
4536 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, CTL_MAX_LUNS);
4537 if (lun_number == -1) {
4538 mtx_unlock(&ctl_softc->ctl_lock);
4539 printf("ctl: can't allocate LUN, out of LUNs\n");
4540 if (lun->flags & CTL_LUN_MALLOCED)
4542 be_lun->lun_config_status(be_lun->be_lun,
4543 CTL_LUN_CONFIG_FAILURE);
4547 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4549 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4550 lun->lun = lun_number;
4551 lun->be_lun = be_lun;
4553 * The processor LUN is always enabled. Disk LUNs come on line
4554 * disabled, and must be enabled by the backend.
4556 lun->flags |= CTL_LUN_DISABLED;
4557 lun->backend = be_lun->be;
4558 be_lun->ctl_lun = lun;
4559 be_lun->lun_id = lun_number;
4560 atomic_add_int(&be_lun->be->num_luns, 1);
4561 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE)
4562 lun->flags |= CTL_LUN_OFFLINE;
4564 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4565 lun->flags |= CTL_LUN_STOPPED;
4567 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4568 lun->flags |= CTL_LUN_INOPERABLE;
4570 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4571 lun->flags |= CTL_LUN_PRIMARY_SC;
4573 lun->ctl_softc = ctl_softc;
4575 lun->last_busy = getsbinuptime();
4577 TAILQ_INIT(&lun->ooa_queue);
4578 TAILQ_INIT(&lun->blocked_queue);
4579 STAILQ_INIT(&lun->error_list);
4580 ctl_tpc_lun_init(lun);
4583 * Initialize the mode and log page index.
4585 ctl_init_page_index(lun);
4586 ctl_init_log_page_index(lun);
4589 * Now, before we insert this lun on the lun list, set the lun
4590 * inventory changed UA for all other luns.
4592 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4593 mtx_lock(&nlun->lun_lock);
4594 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4595 mtx_unlock(&nlun->lun_lock);
4598 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4600 ctl_softc->ctl_luns[lun_number] = lun;
4602 ctl_softc->num_luns++;
4604 /* Setup statistics gathering */
4605 lun->stats.device_type = be_lun->lun_type;
4606 lun->stats.lun_number = lun_number;
4607 if (lun->stats.device_type == T_DIRECT)
4608 lun->stats.blocksize = be_lun->blocksize;
4610 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4611 for (i = 0;i < CTL_MAX_PORTS;i++)
4612 lun->stats.ports[i].targ_port = i;
4614 mtx_unlock(&ctl_softc->ctl_lock);
4616 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4623 * - LUN has already been marked invalid and any pending I/O has been taken
4627 ctl_free_lun(struct ctl_lun *lun)
4629 struct ctl_softc *softc;
4630 struct ctl_lun *nlun;
4633 softc = lun->ctl_softc;
4635 mtx_assert(&softc->ctl_lock, MA_OWNED);
4637 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4639 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4641 softc->ctl_luns[lun->lun] = NULL;
4643 if (!TAILQ_EMPTY(&lun->ooa_queue))
4644 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4649 * Tell the backend to free resources, if this LUN has a backend.
4651 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4652 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4654 ctl_tpc_lun_shutdown(lun);
4655 mtx_destroy(&lun->lun_lock);
4656 free(lun->lun_devid, M_CTL);
4657 for (i = 0; i < CTL_MAX_PORTS; i++)
4658 free(lun->pending_ua[i], M_CTL);
4659 for (i = 0; i < CTL_MAX_PORTS; i++)
4660 free(lun->pr_keys[i], M_CTL);
4661 free(lun->write_buffer, M_CTL);
4662 if (lun->flags & CTL_LUN_MALLOCED)
4665 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4666 mtx_lock(&nlun->lun_lock);
4667 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4668 mtx_unlock(&nlun->lun_lock);
4675 ctl_create_lun(struct ctl_be_lun *be_lun)
4677 struct ctl_softc *softc;
4679 softc = control_softc;
4682 * ctl_alloc_lun() should handle all potential failure cases.
4684 ctl_alloc_lun(softc, NULL, be_lun);
4688 ctl_add_lun(struct ctl_be_lun *be_lun)
4690 struct ctl_softc *softc = control_softc;
4692 mtx_lock(&softc->ctl_lock);
4693 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links);
4694 mtx_unlock(&softc->ctl_lock);
4695 wakeup(&softc->pending_lun_queue);
4701 ctl_enable_lun(struct ctl_be_lun *be_lun)
4703 struct ctl_softc *softc;
4704 struct ctl_port *port, *nport;
4705 struct ctl_lun *lun;
4708 lun = (struct ctl_lun *)be_lun->ctl_lun;
4709 softc = lun->ctl_softc;
4711 mtx_lock(&softc->ctl_lock);
4712 mtx_lock(&lun->lun_lock);
4713 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4715 * eh? Why did we get called if the LUN is already
4718 mtx_unlock(&lun->lun_lock);
4719 mtx_unlock(&softc->ctl_lock);
4722 lun->flags &= ~CTL_LUN_DISABLED;
4723 mtx_unlock(&lun->lun_lock);
4725 for (port = STAILQ_FIRST(&softc->port_list); port != NULL; port = nport) {
4726 nport = STAILQ_NEXT(port, links);
4727 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4728 port->lun_map != NULL || port->lun_enable == NULL)
4732 * Drop the lock while we call the FETD's enable routine.
4733 * This can lead to a callback into CTL (at least in the
4734 * case of the internal initiator frontend.
4736 mtx_unlock(&softc->ctl_lock);
4737 retval = port->lun_enable(port->targ_lun_arg, lun->lun);
4738 mtx_lock(&softc->ctl_lock);
4740 printf("%s: FETD %s port %d returned error "
4741 "%d for lun_enable on lun %jd\n",
4742 __func__, port->port_name, port->targ_port,
4743 retval, (intmax_t)lun->lun);
4747 mtx_unlock(&softc->ctl_lock);
4748 ctl_isc_announce_lun(lun);
4754 ctl_disable_lun(struct ctl_be_lun *be_lun)
4756 struct ctl_softc *softc;
4757 struct ctl_port *port;
4758 struct ctl_lun *lun;
4761 lun = (struct ctl_lun *)be_lun->ctl_lun;
4762 softc = lun->ctl_softc;
4764 mtx_lock(&softc->ctl_lock);
4765 mtx_lock(&lun->lun_lock);
4766 if (lun->flags & CTL_LUN_DISABLED) {
4767 mtx_unlock(&lun->lun_lock);
4768 mtx_unlock(&softc->ctl_lock);
4771 lun->flags |= CTL_LUN_DISABLED;
4772 mtx_unlock(&lun->lun_lock);
4774 STAILQ_FOREACH(port, &softc->port_list, links) {
4775 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4776 port->lun_map != NULL || port->lun_disable == NULL)
4780 * Drop the lock before we call the frontend's disable
4781 * routine, to avoid lock order reversals.
4783 * XXX KDM what happens if the frontend list changes while
4784 * we're traversing it? It's unlikely, but should be handled.
4786 mtx_unlock(&softc->ctl_lock);
4787 retval = port->lun_disable(port->targ_lun_arg, lun->lun);
4788 mtx_lock(&softc->ctl_lock);
4790 printf("%s: FETD %s port %d returned error "
4791 "%d for lun_disable on lun %jd\n",
4792 __func__, port->port_name, port->targ_port,
4793 retval, (intmax_t)lun->lun);
4797 mtx_unlock(&softc->ctl_lock);
4798 ctl_isc_announce_lun(lun);
4804 ctl_start_lun(struct ctl_be_lun *be_lun)
4806 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4808 mtx_lock(&lun->lun_lock);
4809 lun->flags &= ~CTL_LUN_STOPPED;
4810 mtx_unlock(&lun->lun_lock);
4815 ctl_stop_lun(struct ctl_be_lun *be_lun)
4817 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4819 mtx_lock(&lun->lun_lock);
4820 lun->flags |= CTL_LUN_STOPPED;
4821 mtx_unlock(&lun->lun_lock);
4826 ctl_lun_offline(struct ctl_be_lun *be_lun)
4828 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4830 mtx_lock(&lun->lun_lock);
4831 lun->flags |= CTL_LUN_OFFLINE;
4832 mtx_unlock(&lun->lun_lock);
4837 ctl_lun_online(struct ctl_be_lun *be_lun)
4839 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4841 mtx_lock(&lun->lun_lock);
4842 lun->flags &= ~CTL_LUN_OFFLINE;
4843 mtx_unlock(&lun->lun_lock);
4848 ctl_lun_primary(struct ctl_be_lun *be_lun)
4850 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4852 mtx_lock(&lun->lun_lock);
4853 lun->flags |= CTL_LUN_PRIMARY_SC;
4854 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4855 mtx_unlock(&lun->lun_lock);
4856 ctl_isc_announce_lun(lun);
4861 ctl_lun_secondary(struct ctl_be_lun *be_lun)
4863 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4865 mtx_lock(&lun->lun_lock);
4866 lun->flags &= ~CTL_LUN_PRIMARY_SC;
4867 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4868 mtx_unlock(&lun->lun_lock);
4869 ctl_isc_announce_lun(lun);
4874 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4876 struct ctl_softc *softc;
4877 struct ctl_lun *lun;
4879 lun = (struct ctl_lun *)be_lun->ctl_lun;
4880 softc = lun->ctl_softc;
4882 mtx_lock(&lun->lun_lock);
4885 * The LUN needs to be disabled before it can be marked invalid.
4887 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4888 mtx_unlock(&lun->lun_lock);
4892 * Mark the LUN invalid.
4894 lun->flags |= CTL_LUN_INVALID;
4897 * If there is nothing in the OOA queue, go ahead and free the LUN.
4898 * If we have something in the OOA queue, we'll free it when the
4899 * last I/O completes.
4901 if (TAILQ_EMPTY(&lun->ooa_queue)) {
4902 mtx_unlock(&lun->lun_lock);
4903 mtx_lock(&softc->ctl_lock);
4905 mtx_unlock(&softc->ctl_lock);
4907 mtx_unlock(&lun->lun_lock);
4913 ctl_lun_inoperable(struct ctl_be_lun *be_lun)
4915 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4917 mtx_lock(&lun->lun_lock);
4918 lun->flags |= CTL_LUN_INOPERABLE;
4919 mtx_unlock(&lun->lun_lock);
4924 ctl_lun_operable(struct ctl_be_lun *be_lun)
4926 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4928 mtx_lock(&lun->lun_lock);
4929 lun->flags &= ~CTL_LUN_INOPERABLE;
4930 mtx_unlock(&lun->lun_lock);
4935 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
4937 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4938 union ctl_ha_msg msg;
4940 mtx_lock(&lun->lun_lock);
4941 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGED);
4942 mtx_unlock(&lun->lun_lock);
4943 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4944 /* Send msg to other side. */
4945 bzero(&msg.ua, sizeof(msg.ua));
4946 msg.hdr.msg_type = CTL_MSG_UA;
4947 msg.hdr.nexus.initid = -1;
4948 msg.hdr.nexus.targ_port = -1;
4949 msg.hdr.nexus.targ_lun = lun->lun;
4950 msg.hdr.nexus.targ_mapped_lun = lun->lun;
4953 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGED;
4954 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4960 * Backend "memory move is complete" callback for requests that never
4961 * make it down to say RAIDCore's configuration code.
4964 ctl_config_move_done(union ctl_io *io)
4968 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
4969 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
4970 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
4972 if ((io->io_hdr.port_status != 0) &&
4973 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
4974 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
4976 * For hardware error sense keys, the sense key
4977 * specific value is defined to be a retry count,
4978 * but we use it to pass back an internal FETD
4979 * error code. XXX KDM Hopefully the FETD is only
4980 * using 16 bits for an error code, since that's
4981 * all the space we have in the sks field.
4983 ctl_set_internal_failure(&io->scsiio,
4986 io->io_hdr.port_status);
4989 if (ctl_debug & CTL_DEBUG_CDB_DATA)
4991 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
4992 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
4993 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
4994 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
4996 * XXX KDM just assuming a single pointer here, and not a
4997 * S/G list. If we start using S/G lists for config data,
4998 * we'll need to know how to clean them up here as well.
5000 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5001 free(io->scsiio.kern_data_ptr, M_CTL);
5003 retval = CTL_RETVAL_COMPLETE;
5006 * XXX KDM now we need to continue data movement. Some
5008 * - call ctl_scsiio() again? We don't do this for data
5009 * writes, because for those at least we know ahead of
5010 * time where the write will go and how long it is. For
5011 * config writes, though, that information is largely
5012 * contained within the write itself, thus we need to
5013 * parse out the data again.
5015 * - Call some other function once the data is in?
5019 * XXX KDM call ctl_scsiio() again for now, and check flag
5020 * bits to see whether we're allocated or not.
5022 retval = ctl_scsiio(&io->scsiio);
5028 * This gets called by a backend driver when it is done with a
5029 * data_submit method.
5032 ctl_data_submit_done(union ctl_io *io)
5035 * If the IO_CONT flag is set, we need to call the supplied
5036 * function to continue processing the I/O, instead of completing
5039 * If there is an error, though, we don't want to keep processing.
5040 * Instead, just send status back to the initiator.
5042 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5043 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5044 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5045 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5046 io->scsiio.io_cont(io);
5053 * This gets called by a backend driver when it is done with a
5054 * configuration write.
5057 ctl_config_write_done(union ctl_io *io)
5062 * If the IO_CONT flag is set, we need to call the supplied
5063 * function to continue processing the I/O, instead of completing
5066 * If there is an error, though, we don't want to keep processing.
5067 * Instead, just send status back to the initiator.
5069 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5070 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5071 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5072 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5073 io->scsiio.io_cont(io);
5077 * Since a configuration write can be done for commands that actually
5078 * have data allocated, like write buffer, and commands that have
5079 * no data, like start/stop unit, we need to check here.
5081 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5082 buf = io->scsiio.kern_data_ptr;
5091 ctl_config_read_done(union ctl_io *io)
5096 * If there is some error -- we are done, skip data transfer.
5098 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
5099 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5100 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
5101 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5102 buf = io->scsiio.kern_data_ptr;
5112 * If the IO_CONT flag is set, we need to call the supplied
5113 * function to continue processing the I/O, instead of completing
5116 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
5117 io->scsiio.io_cont(io);
5125 * SCSI release command.
5128 ctl_scsi_release(struct ctl_scsiio *ctsio)
5130 int length, longid, thirdparty_id, resv_id;
5131 struct ctl_lun *lun;
5137 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5139 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5140 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5142 switch (ctsio->cdb[0]) {
5144 struct scsi_release_10 *cdb;
5146 cdb = (struct scsi_release_10 *)ctsio->cdb;
5148 if (cdb->byte2 & SR10_LONGID)
5151 thirdparty_id = cdb->thirdparty_id;
5153 resv_id = cdb->resv_id;
5154 length = scsi_2btoul(cdb->length);
5161 * XXX KDM right now, we only support LUN reservation. We don't
5162 * support 3rd party reservations, or extent reservations, which
5163 * might actually need the parameter list. If we've gotten this
5164 * far, we've got a LUN reservation. Anything else got kicked out
5165 * above. So, according to SPC, ignore the length.
5169 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5171 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5172 ctsio->kern_data_len = length;
5173 ctsio->kern_total_len = length;
5174 ctsio->kern_data_resid = 0;
5175 ctsio->kern_rel_offset = 0;
5176 ctsio->kern_sg_entries = 0;
5177 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5178 ctsio->be_move_done = ctl_config_move_done;
5179 ctl_datamove((union ctl_io *)ctsio);
5181 return (CTL_RETVAL_COMPLETE);
5185 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5187 mtx_lock(&lun->lun_lock);
5190 * According to SPC, it is not an error for an intiator to attempt
5191 * to release a reservation on a LUN that isn't reserved, or that
5192 * is reserved by another initiator. The reservation can only be
5193 * released, though, by the initiator who made it or by one of
5194 * several reset type events.
5196 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5197 lun->flags &= ~CTL_LUN_RESERVED;
5199 mtx_unlock(&lun->lun_lock);
5201 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5202 free(ctsio->kern_data_ptr, M_CTL);
5203 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5206 ctl_set_success(ctsio);
5207 ctl_done((union ctl_io *)ctsio);
5208 return (CTL_RETVAL_COMPLETE);
5212 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5214 int extent, thirdparty, longid;
5215 int resv_id, length;
5216 uint64_t thirdparty_id;
5217 struct ctl_lun *lun;
5227 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5229 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5230 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5232 switch (ctsio->cdb[0]) {
5234 struct scsi_reserve_10 *cdb;
5236 cdb = (struct scsi_reserve_10 *)ctsio->cdb;
5238 if (cdb->byte2 & SR10_LONGID)
5241 thirdparty_id = cdb->thirdparty_id;
5243 resv_id = cdb->resv_id;
5244 length = scsi_2btoul(cdb->length);
5250 * XXX KDM right now, we only support LUN reservation. We don't
5251 * support 3rd party reservations, or extent reservations, which
5252 * might actually need the parameter list. If we've gotten this
5253 * far, we've got a LUN reservation. Anything else got kicked out
5254 * above. So, according to SPC, ignore the length.
5258 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5260 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5261 ctsio->kern_data_len = length;
5262 ctsio->kern_total_len = length;
5263 ctsio->kern_data_resid = 0;
5264 ctsio->kern_rel_offset = 0;
5265 ctsio->kern_sg_entries = 0;
5266 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5267 ctsio->be_move_done = ctl_config_move_done;
5268 ctl_datamove((union ctl_io *)ctsio);
5270 return (CTL_RETVAL_COMPLETE);
5274 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5276 mtx_lock(&lun->lun_lock);
5277 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5278 ctl_set_reservation_conflict(ctsio);
5282 lun->flags |= CTL_LUN_RESERVED;
5283 lun->res_idx = residx;
5285 ctl_set_success(ctsio);
5288 mtx_unlock(&lun->lun_lock);
5290 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5291 free(ctsio->kern_data_ptr, M_CTL);
5292 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5295 ctl_done((union ctl_io *)ctsio);
5296 return (CTL_RETVAL_COMPLETE);
5300 ctl_start_stop(struct ctl_scsiio *ctsio)
5302 struct scsi_start_stop_unit *cdb;
5303 struct ctl_lun *lun;
5306 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5308 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5311 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5315 * We don't support the immediate bit on a stop unit. In order to
5316 * do that, we would need to code up a way to know that a stop is
5317 * pending, and hold off any new commands until it completes, one
5318 * way or another. Then we could accept or reject those commands
5319 * depending on its status. We would almost need to do the reverse
5320 * of what we do below for an immediate start -- return the copy of
5321 * the ctl_io to the FETD with status to send to the host (and to
5322 * free the copy!) and then free the original I/O once the stop
5323 * actually completes. That way, the OOA queue mechanism can work
5324 * to block commands that shouldn't proceed. Another alternative
5325 * would be to put the copy in the queue in place of the original,
5326 * and return the original back to the caller. That could be
5329 if ((cdb->byte2 & SSS_IMMED)
5330 && ((cdb->how & SSS_START) == 0)) {
5331 ctl_set_invalid_field(ctsio,
5337 ctl_done((union ctl_io *)ctsio);
5338 return (CTL_RETVAL_COMPLETE);
5341 if ((lun->flags & CTL_LUN_PR_RESERVED)
5342 && ((cdb->how & SSS_START)==0)) {
5345 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5346 if (ctl_get_prkey(lun, residx) == 0
5347 || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5349 ctl_set_reservation_conflict(ctsio);
5350 ctl_done((union ctl_io *)ctsio);
5351 return (CTL_RETVAL_COMPLETE);
5356 * If there is no backend on this device, we can't start or stop
5357 * it. In theory we shouldn't get any start/stop commands in the
5358 * first place at this level if the LUN doesn't have a backend.
5359 * That should get stopped by the command decode code.
5361 if (lun->backend == NULL) {
5362 ctl_set_invalid_opcode(ctsio);
5363 ctl_done((union ctl_io *)ctsio);
5364 return (CTL_RETVAL_COMPLETE);
5368 * XXX KDM Copan-specific offline behavior.
5369 * Figure out a reasonable way to port this?
5372 mtx_lock(&lun->lun_lock);
5374 if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
5375 && (lun->flags & CTL_LUN_OFFLINE)) {
5377 * If the LUN is offline, and the on/offline bit isn't set,
5378 * reject the start or stop. Otherwise, let it through.
5380 mtx_unlock(&lun->lun_lock);
5381 ctl_set_lun_not_ready(ctsio);
5382 ctl_done((union ctl_io *)ctsio);
5384 mtx_unlock(&lun->lun_lock);
5385 #endif /* NEEDTOPORT */
5387 * This could be a start or a stop when we're online,
5388 * or a stop/offline or start/online. A start or stop when
5389 * we're offline is covered in the case above.
5392 * In the non-immediate case, we send the request to
5393 * the backend and return status to the user when
5396 * In the immediate case, we allocate a new ctl_io
5397 * to hold a copy of the request, and send that to
5398 * the backend. We then set good status on the
5399 * user's request and return it immediately.
5401 if (cdb->byte2 & SSS_IMMED) {
5402 union ctl_io *new_io;
5404 new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5405 ctl_copy_io((union ctl_io *)ctsio, new_io);
5406 retval = lun->backend->config_write(new_io);
5407 ctl_set_success(ctsio);
5408 ctl_done((union ctl_io *)ctsio);
5410 retval = lun->backend->config_write(
5411 (union ctl_io *)ctsio);
5420 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5421 * we don't really do anything with the LBA and length fields if the user
5422 * passes them in. Instead we'll just flush out the cache for the entire
5426 ctl_sync_cache(struct ctl_scsiio *ctsio)
5428 struct ctl_lun *lun;
5429 struct ctl_softc *softc;
5430 struct ctl_lba_len_flags *lbalen;
5431 uint64_t starting_lba;
5432 uint32_t block_count;
5436 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5438 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5439 softc = lun->ctl_softc;
5442 switch (ctsio->cdb[0]) {
5443 case SYNCHRONIZE_CACHE: {
5444 struct scsi_sync_cache *cdb;
5445 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5447 starting_lba = scsi_4btoul(cdb->begin_lba);
5448 block_count = scsi_2btoul(cdb->lb_count);
5452 case SYNCHRONIZE_CACHE_16: {
5453 struct scsi_sync_cache_16 *cdb;
5454 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5456 starting_lba = scsi_8btou64(cdb->begin_lba);
5457 block_count = scsi_4btoul(cdb->lb_count);
5462 ctl_set_invalid_opcode(ctsio);
5463 ctl_done((union ctl_io *)ctsio);
5465 break; /* NOTREACHED */
5469 * We check the LBA and length, but don't do anything with them.
5470 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5471 * get flushed. This check will just help satisfy anyone who wants
5472 * to see an error for an out of range LBA.
5474 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5475 ctl_set_lba_out_of_range(ctsio);
5476 ctl_done((union ctl_io *)ctsio);
5481 * If this LUN has no backend, we can't flush the cache anyway.
5483 if (lun->backend == NULL) {
5484 ctl_set_invalid_opcode(ctsio);
5485 ctl_done((union ctl_io *)ctsio);
5489 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5490 lbalen->lba = starting_lba;
5491 lbalen->len = block_count;
5492 lbalen->flags = byte2;
5495 * Check to see whether we're configured to send the SYNCHRONIZE
5496 * CACHE command directly to the back end.
5498 mtx_lock(&lun->lun_lock);
5499 if ((softc->flags & CTL_FLAG_REAL_SYNC)
5500 && (++(lun->sync_count) >= lun->sync_interval)) {
5501 lun->sync_count = 0;
5502 mtx_unlock(&lun->lun_lock);
5503 retval = lun->backend->config_write((union ctl_io *)ctsio);
5505 mtx_unlock(&lun->lun_lock);
5506 ctl_set_success(ctsio);
5507 ctl_done((union ctl_io *)ctsio);
5516 ctl_format(struct ctl_scsiio *ctsio)
5518 struct scsi_format *cdb;
5519 struct ctl_lun *lun;
5520 int length, defect_list_len;
5522 CTL_DEBUG_PRINT(("ctl_format\n"));
5524 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5526 cdb = (struct scsi_format *)ctsio->cdb;
5529 if (cdb->byte2 & SF_FMTDATA) {
5530 if (cdb->byte2 & SF_LONGLIST)
5531 length = sizeof(struct scsi_format_header_long);
5533 length = sizeof(struct scsi_format_header_short);
5536 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5538 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5539 ctsio->kern_data_len = length;
5540 ctsio->kern_total_len = length;
5541 ctsio->kern_data_resid = 0;
5542 ctsio->kern_rel_offset = 0;
5543 ctsio->kern_sg_entries = 0;
5544 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5545 ctsio->be_move_done = ctl_config_move_done;
5546 ctl_datamove((union ctl_io *)ctsio);
5548 return (CTL_RETVAL_COMPLETE);
5551 defect_list_len = 0;
5553 if (cdb->byte2 & SF_FMTDATA) {
5554 if (cdb->byte2 & SF_LONGLIST) {
5555 struct scsi_format_header_long *header;
5557 header = (struct scsi_format_header_long *)
5558 ctsio->kern_data_ptr;
5560 defect_list_len = scsi_4btoul(header->defect_list_len);
5561 if (defect_list_len != 0) {
5562 ctl_set_invalid_field(ctsio,
5571 struct scsi_format_header_short *header;
5573 header = (struct scsi_format_header_short *)
5574 ctsio->kern_data_ptr;
5576 defect_list_len = scsi_2btoul(header->defect_list_len);
5577 if (defect_list_len != 0) {
5578 ctl_set_invalid_field(ctsio,
5590 * The format command will clear out the "Medium format corrupted"
5591 * status if set by the configuration code. That status is really
5592 * just a way to notify the host that we have lost the media, and
5593 * get them to issue a command that will basically make them think
5594 * they're blowing away the media.
5596 mtx_lock(&lun->lun_lock);
5597 lun->flags &= ~CTL_LUN_INOPERABLE;
5598 mtx_unlock(&lun->lun_lock);
5600 ctl_set_success(ctsio);
5603 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5604 free(ctsio->kern_data_ptr, M_CTL);
5605 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5608 ctl_done((union ctl_io *)ctsio);
5609 return (CTL_RETVAL_COMPLETE);
5613 ctl_read_buffer(struct ctl_scsiio *ctsio)
5615 struct ctl_lun *lun;
5616 uint64_t buffer_offset;
5619 static uint8_t descr[4];
5620 static uint8_t echo_descr[4] = { 0 };
5622 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5623 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5624 switch (ctsio->cdb[0]) {
5626 struct scsi_read_buffer *cdb;
5628 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5629 buffer_offset = scsi_3btoul(cdb->offset);
5630 len = scsi_3btoul(cdb->length);
5634 case READ_BUFFER_16: {
5635 struct scsi_read_buffer_16 *cdb;
5637 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb;
5638 buffer_offset = scsi_8btou64(cdb->offset);
5639 len = scsi_4btoul(cdb->length);
5643 default: /* This shouldn't happen. */
5644 ctl_set_invalid_opcode(ctsio);
5645 ctl_done((union ctl_io *)ctsio);
5646 return (CTL_RETVAL_COMPLETE);
5649 if ((byte2 & RWB_MODE) != RWB_MODE_DATA &&
5650 (byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR &&
5651 (byte2 & RWB_MODE) != RWB_MODE_DESCR) {
5652 ctl_set_invalid_field(ctsio,
5658 ctl_done((union ctl_io *)ctsio);
5659 return (CTL_RETVAL_COMPLETE);
5662 if (buffer_offset > CTL_WRITE_BUFFER_SIZE ||
5663 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5664 ctl_set_invalid_field(ctsio,
5670 ctl_done((union ctl_io *)ctsio);
5671 return (CTL_RETVAL_COMPLETE);
5674 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5676 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5677 ctsio->kern_data_ptr = descr;
5678 len = min(len, sizeof(descr));
5679 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5680 ctsio->kern_data_ptr = echo_descr;
5681 len = min(len, sizeof(echo_descr));
5683 if (lun->write_buffer == NULL) {
5684 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5687 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5689 ctsio->kern_data_len = len;
5690 ctsio->kern_total_len = len;
5691 ctsio->kern_data_resid = 0;
5692 ctsio->kern_rel_offset = 0;
5693 ctsio->kern_sg_entries = 0;
5694 ctl_set_success(ctsio);
5695 ctsio->be_move_done = ctl_config_move_done;
5696 ctl_datamove((union ctl_io *)ctsio);
5697 return (CTL_RETVAL_COMPLETE);
5701 ctl_write_buffer(struct ctl_scsiio *ctsio)
5703 struct scsi_write_buffer *cdb;
5704 struct ctl_lun *lun;
5705 int buffer_offset, len;
5707 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5709 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5710 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5712 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5713 ctl_set_invalid_field(ctsio,
5719 ctl_done((union ctl_io *)ctsio);
5720 return (CTL_RETVAL_COMPLETE);
5723 len = scsi_3btoul(cdb->length);
5724 buffer_offset = scsi_3btoul(cdb->offset);
5726 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5727 ctl_set_invalid_field(ctsio,
5733 ctl_done((union ctl_io *)ctsio);
5734 return (CTL_RETVAL_COMPLETE);
5738 * If we've got a kernel request that hasn't been malloced yet,
5739 * malloc it and tell the caller the data buffer is here.
5741 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5742 if (lun->write_buffer == NULL) {
5743 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5746 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5747 ctsio->kern_data_len = len;
5748 ctsio->kern_total_len = len;
5749 ctsio->kern_data_resid = 0;
5750 ctsio->kern_rel_offset = 0;
5751 ctsio->kern_sg_entries = 0;
5752 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5753 ctsio->be_move_done = ctl_config_move_done;
5754 ctl_datamove((union ctl_io *)ctsio);
5756 return (CTL_RETVAL_COMPLETE);
5759 ctl_set_success(ctsio);
5760 ctl_done((union ctl_io *)ctsio);
5761 return (CTL_RETVAL_COMPLETE);
5765 ctl_write_same(struct ctl_scsiio *ctsio)
5767 struct ctl_lun *lun;
5768 struct ctl_lba_len_flags *lbalen;
5770 uint32_t num_blocks;
5774 retval = CTL_RETVAL_COMPLETE;
5776 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5778 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5780 switch (ctsio->cdb[0]) {
5781 case WRITE_SAME_10: {
5782 struct scsi_write_same_10 *cdb;
5784 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5786 lba = scsi_4btoul(cdb->addr);
5787 num_blocks = scsi_2btoul(cdb->length);
5791 case WRITE_SAME_16: {
5792 struct scsi_write_same_16 *cdb;
5794 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5796 lba = scsi_8btou64(cdb->addr);
5797 num_blocks = scsi_4btoul(cdb->length);
5803 * We got a command we don't support. This shouldn't
5804 * happen, commands should be filtered out above us.
5806 ctl_set_invalid_opcode(ctsio);
5807 ctl_done((union ctl_io *)ctsio);
5809 return (CTL_RETVAL_COMPLETE);
5810 break; /* NOTREACHED */
5813 /* NDOB and ANCHOR flags can be used only together with UNMAP */
5814 if ((byte2 & SWS_UNMAP) == 0 &&
5815 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) {
5816 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5817 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5818 ctl_done((union ctl_io *)ctsio);
5819 return (CTL_RETVAL_COMPLETE);
5823 * The first check is to make sure we're in bounds, the second
5824 * check is to catch wrap-around problems. If the lba + num blocks
5825 * is less than the lba, then we've wrapped around and the block
5826 * range is invalid anyway.
5828 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5829 || ((lba + num_blocks) < lba)) {
5830 ctl_set_lba_out_of_range(ctsio);
5831 ctl_done((union ctl_io *)ctsio);
5832 return (CTL_RETVAL_COMPLETE);
5835 /* Zero number of blocks means "to the last logical block" */
5836 if (num_blocks == 0) {
5837 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5838 ctl_set_invalid_field(ctsio,
5844 ctl_done((union ctl_io *)ctsio);
5845 return (CTL_RETVAL_COMPLETE);
5847 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5850 len = lun->be_lun->blocksize;
5853 * If we've got a kernel request that hasn't been malloced yet,
5854 * malloc it and tell the caller the data buffer is here.
5856 if ((byte2 & SWS_NDOB) == 0 &&
5857 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5858 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5859 ctsio->kern_data_len = len;
5860 ctsio->kern_total_len = len;
5861 ctsio->kern_data_resid = 0;
5862 ctsio->kern_rel_offset = 0;
5863 ctsio->kern_sg_entries = 0;
5864 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5865 ctsio->be_move_done = ctl_config_move_done;
5866 ctl_datamove((union ctl_io *)ctsio);
5868 return (CTL_RETVAL_COMPLETE);
5871 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5873 lbalen->len = num_blocks;
5874 lbalen->flags = byte2;
5875 retval = lun->backend->config_write((union ctl_io *)ctsio);
5881 ctl_unmap(struct ctl_scsiio *ctsio)
5883 struct ctl_lun *lun;
5884 struct scsi_unmap *cdb;
5885 struct ctl_ptr_len_flags *ptrlen;
5886 struct scsi_unmap_header *hdr;
5887 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5889 uint32_t num_blocks;
5893 retval = CTL_RETVAL_COMPLETE;
5895 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5897 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5898 cdb = (struct scsi_unmap *)ctsio->cdb;
5900 len = scsi_2btoul(cdb->length);
5904 * If we've got a kernel request that hasn't been malloced yet,
5905 * malloc it and tell the caller the data buffer is here.
5907 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5908 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5909 ctsio->kern_data_len = len;
5910 ctsio->kern_total_len = len;
5911 ctsio->kern_data_resid = 0;
5912 ctsio->kern_rel_offset = 0;
5913 ctsio->kern_sg_entries = 0;
5914 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5915 ctsio->be_move_done = ctl_config_move_done;
5916 ctl_datamove((union ctl_io *)ctsio);
5918 return (CTL_RETVAL_COMPLETE);
5921 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5922 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5923 if (len < sizeof (*hdr) ||
5924 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5925 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5926 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5927 ctl_set_invalid_field(ctsio,
5935 len = scsi_2btoul(hdr->desc_length);
5936 buf = (struct scsi_unmap_desc *)(hdr + 1);
5937 end = buf + len / sizeof(*buf);
5940 for (range = buf; range < end; range++) {
5941 lba = scsi_8btou64(range->lba);
5942 num_blocks = scsi_4btoul(range->length);
5943 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5944 || ((lba + num_blocks) < lba)) {
5945 ctl_set_lba_out_of_range(ctsio);
5946 ctl_done((union ctl_io *)ctsio);
5947 return (CTL_RETVAL_COMPLETE);
5949 if (num_blocks != 0)
5954 * Block backend can not handle zero last range.
5955 * Filter it out and return if there is nothing left.
5957 len = (uint8_t *)endnz - (uint8_t *)buf;
5959 ctl_set_success(ctsio);
5963 mtx_lock(&lun->lun_lock);
5964 ptrlen = (struct ctl_ptr_len_flags *)
5965 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5966 ptrlen->ptr = (void *)buf;
5968 ptrlen->flags = byte2;
5969 ctl_check_blocked(lun);
5970 mtx_unlock(&lun->lun_lock);
5972 retval = lun->backend->config_write((union ctl_io *)ctsio);
5976 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5977 free(ctsio->kern_data_ptr, M_CTL);
5978 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5980 ctl_done((union ctl_io *)ctsio);
5981 return (CTL_RETVAL_COMPLETE);
5985 * Note that this function currently doesn't actually do anything inside
5986 * CTL to enforce things if the DQue bit is turned on.
5988 * Also note that this function can't be used in the default case, because
5989 * the DQue bit isn't set in the changeable mask for the control mode page
5990 * anyway. This is just here as an example for how to implement a page
5991 * handler, and a placeholder in case we want to allow the user to turn
5992 * tagged queueing on and off.
5994 * The D_SENSE bit handling is functional, however, and will turn
5995 * descriptor sense on and off for a given LUN.
5998 ctl_control_page_handler(struct ctl_scsiio *ctsio,
5999 struct ctl_page_index *page_index, uint8_t *page_ptr)
6001 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
6002 struct ctl_lun *lun;
6006 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6007 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6010 user_cp = (struct scsi_control_page *)page_ptr;
6011 current_cp = (struct scsi_control_page *)
6012 (page_index->page_data + (page_index->page_len *
6014 saved_cp = (struct scsi_control_page *)
6015 (page_index->page_data + (page_index->page_len *
6018 mtx_lock(&lun->lun_lock);
6019 if (((current_cp->rlec & SCP_DSENSE) == 0)
6020 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
6022 * Descriptor sense is currently turned off and the user
6023 * wants to turn it on.
6025 current_cp->rlec |= SCP_DSENSE;
6026 saved_cp->rlec |= SCP_DSENSE;
6027 lun->flags |= CTL_LUN_SENSE_DESC;
6029 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
6030 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
6032 * Descriptor sense is currently turned on, and the user
6033 * wants to turn it off.
6035 current_cp->rlec &= ~SCP_DSENSE;
6036 saved_cp->rlec &= ~SCP_DSENSE;
6037 lun->flags &= ~CTL_LUN_SENSE_DESC;
6040 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) !=
6041 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) {
6042 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
6043 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
6044 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
6045 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
6048 if ((current_cp->eca_and_aen & SCP_SWP) !=
6049 (user_cp->eca_and_aen & SCP_SWP)) {
6050 current_cp->eca_and_aen &= ~SCP_SWP;
6051 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
6052 saved_cp->eca_and_aen &= ~SCP_SWP;
6053 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
6057 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
6058 mtx_unlock(&lun->lun_lock);
6060 ctl_isc_announce_mode(lun,
6061 ctl_get_initindex(&ctsio->io_hdr.nexus),
6062 page_index->page_code, page_index->subpage);
6068 ctl_caching_sp_handler(struct ctl_scsiio *ctsio,
6069 struct ctl_page_index *page_index, uint8_t *page_ptr)
6071 struct scsi_caching_page *current_cp, *saved_cp, *user_cp;
6072 struct ctl_lun *lun;
6076 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6077 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6080 user_cp = (struct scsi_caching_page *)page_ptr;
6081 current_cp = (struct scsi_caching_page *)
6082 (page_index->page_data + (page_index->page_len *
6084 saved_cp = (struct scsi_caching_page *)
6085 (page_index->page_data + (page_index->page_len *
6088 mtx_lock(&lun->lun_lock);
6089 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) !=
6090 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) {
6091 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
6092 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
6093 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
6094 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
6098 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
6099 mtx_unlock(&lun->lun_lock);
6101 ctl_isc_announce_mode(lun,
6102 ctl_get_initindex(&ctsio->io_hdr.nexus),
6103 page_index->page_code, page_index->subpage);
6109 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
6110 struct ctl_page_index *page_index,
6116 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
6121 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
6122 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
6123 printf("page data:");
6125 printf(" %.2x",page_ptr[i]);
6131 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
6132 struct ctl_page_index *page_index,
6135 struct copan_debugconf_subpage *page;
6137 page = (struct copan_debugconf_subpage *)page_index->page_data +
6138 (page_index->page_len * pc);
6141 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6142 case SMS_PAGE_CTRL_DEFAULT >> 6:
6143 case SMS_PAGE_CTRL_SAVED >> 6:
6145 * We don't update the changable or default bits for this page.
6148 case SMS_PAGE_CTRL_CURRENT >> 6:
6149 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
6150 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
6154 EPRINT(0, "Invalid PC %d!!", pc);
6155 #endif /* NEEDTOPORT */
6163 ctl_do_mode_select(union ctl_io *io)
6165 struct scsi_mode_page_header *page_header;
6166 struct ctl_page_index *page_index;
6167 struct ctl_scsiio *ctsio;
6168 int control_dev, page_len;
6169 int page_len_offset, page_len_size;
6170 union ctl_modepage_info *modepage_info;
6171 struct ctl_lun *lun;
6172 int *len_left, *len_used;
6175 ctsio = &io->scsiio;
6178 retval = CTL_RETVAL_COMPLETE;
6180 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6182 if (lun->be_lun->lun_type != T_DIRECT)
6187 modepage_info = (union ctl_modepage_info *)
6188 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6189 len_left = &modepage_info->header.len_left;
6190 len_used = &modepage_info->header.len_used;
6194 page_header = (struct scsi_mode_page_header *)
6195 (ctsio->kern_data_ptr + *len_used);
6197 if (*len_left == 0) {
6198 free(ctsio->kern_data_ptr, M_CTL);
6199 ctl_set_success(ctsio);
6200 ctl_done((union ctl_io *)ctsio);
6201 return (CTL_RETVAL_COMPLETE);
6202 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6204 free(ctsio->kern_data_ptr, M_CTL);
6205 ctl_set_param_len_error(ctsio);
6206 ctl_done((union ctl_io *)ctsio);
6207 return (CTL_RETVAL_COMPLETE);
6209 } else if ((page_header->page_code & SMPH_SPF)
6210 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6212 free(ctsio->kern_data_ptr, M_CTL);
6213 ctl_set_param_len_error(ctsio);
6214 ctl_done((union ctl_io *)ctsio);
6215 return (CTL_RETVAL_COMPLETE);
6220 * XXX KDM should we do something with the block descriptor?
6222 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6224 if ((control_dev != 0)
6225 && (lun->mode_pages.index[i].page_flags &
6226 CTL_PAGE_FLAG_DISK_ONLY))
6229 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
6230 (page_header->page_code & SMPH_PC_MASK))
6234 * If neither page has a subpage code, then we've got a
6237 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
6238 && ((page_header->page_code & SMPH_SPF) == 0)) {
6239 page_index = &lun->mode_pages.index[i];
6240 page_len = page_header->page_length;
6245 * If both pages have subpages, then the subpage numbers
6248 if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
6249 && (page_header->page_code & SMPH_SPF)) {
6250 struct scsi_mode_page_header_sp *sph;
6252 sph = (struct scsi_mode_page_header_sp *)page_header;
6254 if (lun->mode_pages.index[i].subpage ==
6256 page_index = &lun->mode_pages.index[i];
6257 page_len = scsi_2btoul(sph->page_length);
6264 * If we couldn't find the page, or if we don't have a mode select
6265 * handler for it, send back an error to the user.
6267 if ((page_index == NULL)
6268 || (page_index->select_handler == NULL)) {
6269 ctl_set_invalid_field(ctsio,
6272 /*field*/ *len_used,
6275 free(ctsio->kern_data_ptr, M_CTL);
6276 ctl_done((union ctl_io *)ctsio);
6277 return (CTL_RETVAL_COMPLETE);
6280 if (page_index->page_code & SMPH_SPF) {
6281 page_len_offset = 2;
6285 page_len_offset = 1;
6289 * If the length the initiator gives us isn't the one we specify in
6290 * the mode page header, or if they didn't specify enough data in
6291 * the CDB to avoid truncating this page, kick out the request.
6293 if ((page_len != (page_index->page_len - page_len_offset -
6295 || (*len_left < page_index->page_len)) {
6298 ctl_set_invalid_field(ctsio,
6301 /*field*/ *len_used + page_len_offset,
6304 free(ctsio->kern_data_ptr, M_CTL);
6305 ctl_done((union ctl_io *)ctsio);
6306 return (CTL_RETVAL_COMPLETE);
6310 * Run through the mode page, checking to make sure that the bits
6311 * the user changed are actually legal for him to change.
6313 for (i = 0; i < page_index->page_len; i++) {
6314 uint8_t *user_byte, *change_mask, *current_byte;
6318 user_byte = (uint8_t *)page_header + i;
6319 change_mask = page_index->page_data +
6320 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6321 current_byte = page_index->page_data +
6322 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6325 * Check to see whether the user set any bits in this byte
6326 * that he is not allowed to set.
6328 if ((*user_byte & ~(*change_mask)) ==
6329 (*current_byte & ~(*change_mask)))
6333 * Go through bit by bit to determine which one is illegal.
6336 for (j = 7; j >= 0; j--) {
6337 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6338 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6343 ctl_set_invalid_field(ctsio,
6346 /*field*/ *len_used + i,
6349 free(ctsio->kern_data_ptr, M_CTL);
6350 ctl_done((union ctl_io *)ctsio);
6351 return (CTL_RETVAL_COMPLETE);
6355 * Decrement these before we call the page handler, since we may
6356 * end up getting called back one way or another before the handler
6357 * returns to this context.
6359 *len_left -= page_index->page_len;
6360 *len_used += page_index->page_len;
6362 retval = page_index->select_handler(ctsio, page_index,
6363 (uint8_t *)page_header);
6366 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6367 * wait until this queued command completes to finish processing
6368 * the mode page. If it returns anything other than
6369 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6370 * already set the sense information, freed the data pointer, and
6371 * completed the io for us.
6373 if (retval != CTL_RETVAL_COMPLETE)
6374 goto bailout_no_done;
6377 * If the initiator sent us more than one page, parse the next one.
6382 ctl_set_success(ctsio);
6383 free(ctsio->kern_data_ptr, M_CTL);
6384 ctl_done((union ctl_io *)ctsio);
6388 return (CTL_RETVAL_COMPLETE);
6393 ctl_mode_select(struct ctl_scsiio *ctsio)
6395 int param_len, pf, sp;
6396 int header_size, bd_len;
6397 int len_left, len_used;
6398 struct ctl_page_index *page_index;
6399 struct ctl_lun *lun;
6400 int control_dev, page_len;
6401 union ctl_modepage_info *modepage_info;
6413 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6415 if (lun->be_lun->lun_type != T_DIRECT)
6420 switch (ctsio->cdb[0]) {
6421 case MODE_SELECT_6: {
6422 struct scsi_mode_select_6 *cdb;
6424 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6426 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6427 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6429 param_len = cdb->length;
6430 header_size = sizeof(struct scsi_mode_header_6);
6433 case MODE_SELECT_10: {
6434 struct scsi_mode_select_10 *cdb;
6436 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6438 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6439 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6441 param_len = scsi_2btoul(cdb->length);
6442 header_size = sizeof(struct scsi_mode_header_10);
6446 ctl_set_invalid_opcode(ctsio);
6447 ctl_done((union ctl_io *)ctsio);
6448 return (CTL_RETVAL_COMPLETE);
6449 break; /* NOTREACHED */
6454 * "A parameter list length of zero indicates that the Data-Out Buffer
6455 * shall be empty. This condition shall not be considered as an error."
6457 if (param_len == 0) {
6458 ctl_set_success(ctsio);
6459 ctl_done((union ctl_io *)ctsio);
6460 return (CTL_RETVAL_COMPLETE);
6464 * Since we'll hit this the first time through, prior to
6465 * allocation, we don't need to free a data buffer here.
6467 if (param_len < header_size) {
6468 ctl_set_param_len_error(ctsio);
6469 ctl_done((union ctl_io *)ctsio);
6470 return (CTL_RETVAL_COMPLETE);
6474 * Allocate the data buffer and grab the user's data. In theory,
6475 * we shouldn't have to sanity check the parameter list length here
6476 * because the maximum size is 64K. We should be able to malloc
6477 * that much without too many problems.
6479 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6480 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6481 ctsio->kern_data_len = param_len;
6482 ctsio->kern_total_len = param_len;
6483 ctsio->kern_data_resid = 0;
6484 ctsio->kern_rel_offset = 0;
6485 ctsio->kern_sg_entries = 0;
6486 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6487 ctsio->be_move_done = ctl_config_move_done;
6488 ctl_datamove((union ctl_io *)ctsio);
6490 return (CTL_RETVAL_COMPLETE);
6493 switch (ctsio->cdb[0]) {
6494 case MODE_SELECT_6: {
6495 struct scsi_mode_header_6 *mh6;
6497 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6498 bd_len = mh6->blk_desc_len;
6501 case MODE_SELECT_10: {
6502 struct scsi_mode_header_10 *mh10;
6504 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6505 bd_len = scsi_2btoul(mh10->blk_desc_len);
6509 panic("Invalid CDB type %#x", ctsio->cdb[0]);
6513 if (param_len < (header_size + bd_len)) {
6514 free(ctsio->kern_data_ptr, M_CTL);
6515 ctl_set_param_len_error(ctsio);
6516 ctl_done((union ctl_io *)ctsio);
6517 return (CTL_RETVAL_COMPLETE);
6521 * Set the IO_CONT flag, so that if this I/O gets passed to
6522 * ctl_config_write_done(), it'll get passed back to
6523 * ctl_do_mode_select() for further processing, or completion if
6526 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6527 ctsio->io_cont = ctl_do_mode_select;
6529 modepage_info = (union ctl_modepage_info *)
6530 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6532 memset(modepage_info, 0, sizeof(*modepage_info));
6534 len_left = param_len - header_size - bd_len;
6535 len_used = header_size + bd_len;
6537 modepage_info->header.len_left = len_left;
6538 modepage_info->header.len_used = len_used;
6540 return (ctl_do_mode_select((union ctl_io *)ctsio));
6544 ctl_mode_sense(struct ctl_scsiio *ctsio)
6546 struct ctl_lun *lun;
6547 int pc, page_code, dbd, llba, subpage;
6548 int alloc_len, page_len, header_len, total_len;
6549 struct scsi_mode_block_descr *block_desc;
6550 struct ctl_page_index *page_index;
6558 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6560 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6562 if (lun->be_lun->lun_type != T_DIRECT)
6567 switch (ctsio->cdb[0]) {
6568 case MODE_SENSE_6: {
6569 struct scsi_mode_sense_6 *cdb;
6571 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6573 header_len = sizeof(struct scsi_mode_hdr_6);
6574 if (cdb->byte2 & SMS_DBD)
6577 header_len += sizeof(struct scsi_mode_block_descr);
6579 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6580 page_code = cdb->page & SMS_PAGE_CODE;
6581 subpage = cdb->subpage;
6582 alloc_len = cdb->length;
6585 case MODE_SENSE_10: {
6586 struct scsi_mode_sense_10 *cdb;
6588 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6590 header_len = sizeof(struct scsi_mode_hdr_10);
6592 if (cdb->byte2 & SMS_DBD)
6595 header_len += sizeof(struct scsi_mode_block_descr);
6596 if (cdb->byte2 & SMS10_LLBAA)
6598 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6599 page_code = cdb->page & SMS_PAGE_CODE;
6600 subpage = cdb->subpage;
6601 alloc_len = scsi_2btoul(cdb->length);
6605 ctl_set_invalid_opcode(ctsio);
6606 ctl_done((union ctl_io *)ctsio);
6607 return (CTL_RETVAL_COMPLETE);
6608 break; /* NOTREACHED */
6612 * We have to make a first pass through to calculate the size of
6613 * the pages that match the user's query. Then we allocate enough
6614 * memory to hold it, and actually copy the data into the buffer.
6616 switch (page_code) {
6617 case SMS_ALL_PAGES_PAGE: {
6623 * At the moment, values other than 0 and 0xff here are
6624 * reserved according to SPC-3.
6626 if ((subpage != SMS_SUBPAGE_PAGE_0)
6627 && (subpage != SMS_SUBPAGE_ALL)) {
6628 ctl_set_invalid_field(ctsio,
6634 ctl_done((union ctl_io *)ctsio);
6635 return (CTL_RETVAL_COMPLETE);
6638 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6639 if ((control_dev != 0)
6640 && (lun->mode_pages.index[i].page_flags &
6641 CTL_PAGE_FLAG_DISK_ONLY))
6645 * We don't use this subpage if the user didn't
6646 * request all subpages.
6648 if ((lun->mode_pages.index[i].subpage != 0)
6649 && (subpage == SMS_SUBPAGE_PAGE_0))
6653 printf("found page %#x len %d\n",
6654 lun->mode_pages.index[i].page_code &
6656 lun->mode_pages.index[i].page_len);
6658 page_len += lun->mode_pages.index[i].page_len;
6667 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6668 /* Look for the right page code */
6669 if ((lun->mode_pages.index[i].page_code &
6670 SMPH_PC_MASK) != page_code)
6673 /* Look for the right subpage or the subpage wildcard*/
6674 if ((lun->mode_pages.index[i].subpage != subpage)
6675 && (subpage != SMS_SUBPAGE_ALL))
6678 /* Make sure the page is supported for this dev type */
6679 if ((control_dev != 0)
6680 && (lun->mode_pages.index[i].page_flags &
6681 CTL_PAGE_FLAG_DISK_ONLY))
6685 printf("found page %#x len %d\n",
6686 lun->mode_pages.index[i].page_code &
6688 lun->mode_pages.index[i].page_len);
6691 page_len += lun->mode_pages.index[i].page_len;
6694 if (page_len == 0) {
6695 ctl_set_invalid_field(ctsio,
6701 ctl_done((union ctl_io *)ctsio);
6702 return (CTL_RETVAL_COMPLETE);
6708 total_len = header_len + page_len;
6710 printf("header_len = %d, page_len = %d, total_len = %d\n",
6711 header_len, page_len, total_len);
6714 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6715 ctsio->kern_sg_entries = 0;
6716 ctsio->kern_data_resid = 0;
6717 ctsio->kern_rel_offset = 0;
6718 if (total_len < alloc_len) {
6719 ctsio->residual = alloc_len - total_len;
6720 ctsio->kern_data_len = total_len;
6721 ctsio->kern_total_len = total_len;
6723 ctsio->residual = 0;
6724 ctsio->kern_data_len = alloc_len;
6725 ctsio->kern_total_len = alloc_len;
6728 switch (ctsio->cdb[0]) {
6729 case MODE_SENSE_6: {
6730 struct scsi_mode_hdr_6 *header;
6732 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6734 header->datalen = MIN(total_len - 1, 254);
6735 if (control_dev == 0) {
6736 header->dev_specific = 0x10; /* DPOFUA */
6737 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6738 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6739 .eca_and_aen & SCP_SWP) != 0)
6740 header->dev_specific |= 0x80; /* WP */
6743 header->block_descr_len = 0;
6745 header->block_descr_len =
6746 sizeof(struct scsi_mode_block_descr);
6747 block_desc = (struct scsi_mode_block_descr *)&header[1];
6750 case MODE_SENSE_10: {
6751 struct scsi_mode_hdr_10 *header;
6754 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6756 datalen = MIN(total_len - 2, 65533);
6757 scsi_ulto2b(datalen, header->datalen);
6758 if (control_dev == 0) {
6759 header->dev_specific = 0x10; /* DPOFUA */
6760 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6761 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6762 .eca_and_aen & SCP_SWP) != 0)
6763 header->dev_specific |= 0x80; /* WP */
6766 scsi_ulto2b(0, header->block_descr_len);
6768 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6769 header->block_descr_len);
6770 block_desc = (struct scsi_mode_block_descr *)&header[1];
6774 panic("invalid CDB type %#x", ctsio->cdb[0]);
6775 break; /* NOTREACHED */
6779 * If we've got a disk, use its blocksize in the block
6780 * descriptor. Otherwise, just set it to 0.
6783 if (control_dev == 0)
6784 scsi_ulto3b(lun->be_lun->blocksize,
6785 block_desc->block_len);
6787 scsi_ulto3b(0, block_desc->block_len);
6790 switch (page_code) {
6791 case SMS_ALL_PAGES_PAGE: {
6794 data_used = header_len;
6795 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6796 struct ctl_page_index *page_index;
6798 page_index = &lun->mode_pages.index[i];
6800 if ((control_dev != 0)
6801 && (page_index->page_flags &
6802 CTL_PAGE_FLAG_DISK_ONLY))
6806 * We don't use this subpage if the user didn't
6807 * request all subpages. We already checked (above)
6808 * to make sure the user only specified a subpage
6809 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6811 if ((page_index->subpage != 0)
6812 && (subpage == SMS_SUBPAGE_PAGE_0))
6816 * Call the handler, if it exists, to update the
6817 * page to the latest values.
6819 if (page_index->sense_handler != NULL)
6820 page_index->sense_handler(ctsio, page_index,pc);
6822 memcpy(ctsio->kern_data_ptr + data_used,
6823 page_index->page_data +
6824 (page_index->page_len * pc),
6825 page_index->page_len);
6826 data_used += page_index->page_len;
6833 data_used = header_len;
6835 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6836 struct ctl_page_index *page_index;
6838 page_index = &lun->mode_pages.index[i];
6840 /* Look for the right page code */
6841 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6844 /* Look for the right subpage or the subpage wildcard*/
6845 if ((page_index->subpage != subpage)
6846 && (subpage != SMS_SUBPAGE_ALL))
6849 /* Make sure the page is supported for this dev type */
6850 if ((control_dev != 0)
6851 && (page_index->page_flags &
6852 CTL_PAGE_FLAG_DISK_ONLY))
6856 * Call the handler, if it exists, to update the
6857 * page to the latest values.
6859 if (page_index->sense_handler != NULL)
6860 page_index->sense_handler(ctsio, page_index,pc);
6862 memcpy(ctsio->kern_data_ptr + data_used,
6863 page_index->page_data +
6864 (page_index->page_len * pc),
6865 page_index->page_len);
6866 data_used += page_index->page_len;
6872 ctl_set_success(ctsio);
6873 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6874 ctsio->be_move_done = ctl_config_move_done;
6875 ctl_datamove((union ctl_io *)ctsio);
6876 return (CTL_RETVAL_COMPLETE);
6880 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6881 struct ctl_page_index *page_index,
6884 struct ctl_lun *lun;
6885 struct scsi_log_param_header *phdr;
6889 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6890 data = page_index->page_data;
6892 if (lun->backend->lun_attr != NULL &&
6893 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6895 phdr = (struct scsi_log_param_header *)data;
6896 scsi_ulto2b(0x0001, phdr->param_code);
6897 phdr->param_control = SLP_LBIN | SLP_LP;
6898 phdr->param_len = 8;
6899 data = (uint8_t *)(phdr + 1);
6900 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6901 data[4] = 0x02; /* per-pool */
6902 data += phdr->param_len;
6905 if (lun->backend->lun_attr != NULL &&
6906 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6908 phdr = (struct scsi_log_param_header *)data;
6909 scsi_ulto2b(0x0002, phdr->param_code);
6910 phdr->param_control = SLP_LBIN | SLP_LP;
6911 phdr->param_len = 8;
6912 data = (uint8_t *)(phdr + 1);
6913 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6914 data[4] = 0x01; /* per-LUN */
6915 data += phdr->param_len;
6918 if (lun->backend->lun_attr != NULL &&
6919 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6921 phdr = (struct scsi_log_param_header *)data;
6922 scsi_ulto2b(0x00f1, phdr->param_code);
6923 phdr->param_control = SLP_LBIN | SLP_LP;
6924 phdr->param_len = 8;
6925 data = (uint8_t *)(phdr + 1);
6926 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6927 data[4] = 0x02; /* per-pool */
6928 data += phdr->param_len;
6931 if (lun->backend->lun_attr != NULL &&
6932 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6934 phdr = (struct scsi_log_param_header *)data;
6935 scsi_ulto2b(0x00f2, phdr->param_code);
6936 phdr->param_control = SLP_LBIN | SLP_LP;
6937 phdr->param_len = 8;
6938 data = (uint8_t *)(phdr + 1);
6939 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6940 data[4] = 0x02; /* per-pool */
6941 data += phdr->param_len;
6944 page_index->page_len = data - page_index->page_data;
6949 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
6950 struct ctl_page_index *page_index,
6953 struct ctl_lun *lun;
6954 struct stat_page *data;
6955 uint64_t rn, wn, rb, wb;
6956 struct bintime rt, wt;
6959 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6960 data = (struct stat_page *)page_index->page_data;
6962 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
6963 data->sap.hdr.param_control = SLP_LBIN;
6964 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
6965 sizeof(struct scsi_log_param_header);
6966 rn = wn = rb = wb = 0;
6969 for (i = 0; i < CTL_MAX_PORTS; i++) {
6970 rn += lun->stats.ports[i].operations[CTL_STATS_READ];
6971 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE];
6972 rb += lun->stats.ports[i].bytes[CTL_STATS_READ];
6973 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE];
6974 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]);
6975 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]);
6977 scsi_u64to8b(rn, data->sap.read_num);
6978 scsi_u64to8b(wn, data->sap.write_num);
6979 if (lun->stats.blocksize > 0) {
6980 scsi_u64to8b(wb / lun->stats.blocksize,
6981 data->sap.recvieved_lba);
6982 scsi_u64to8b(rb / lun->stats.blocksize,
6983 data->sap.transmitted_lba);
6985 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000),
6986 data->sap.read_int);
6987 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000),
6988 data->sap.write_int);
6989 scsi_u64to8b(0, data->sap.weighted_num);
6990 scsi_u64to8b(0, data->sap.weighted_int);
6991 scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
6992 data->it.hdr.param_control = SLP_LBIN;
6993 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
6994 sizeof(struct scsi_log_param_header);
6996 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
6998 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
6999 data->it.hdr.param_control = SLP_LBIN;
7000 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
7001 sizeof(struct scsi_log_param_header);
7002 scsi_ulto4b(3, data->ti.exponent);
7003 scsi_ulto4b(1, data->ti.integer);
7005 page_index->page_len = sizeof(*data);
7010 ctl_log_sense(struct ctl_scsiio *ctsio)
7012 struct ctl_lun *lun;
7013 int i, pc, page_code, subpage;
7014 int alloc_len, total_len;
7015 struct ctl_page_index *page_index;
7016 struct scsi_log_sense *cdb;
7017 struct scsi_log_header *header;
7019 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
7021 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7022 cdb = (struct scsi_log_sense *)ctsio->cdb;
7023 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
7024 page_code = cdb->page & SLS_PAGE_CODE;
7025 subpage = cdb->subpage;
7026 alloc_len = scsi_2btoul(cdb->length);
7029 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
7030 page_index = &lun->log_pages.index[i];
7032 /* Look for the right page code */
7033 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
7036 /* Look for the right subpage or the subpage wildcard*/
7037 if (page_index->subpage != subpage)
7042 if (i >= CTL_NUM_LOG_PAGES) {
7043 ctl_set_invalid_field(ctsio,
7049 ctl_done((union ctl_io *)ctsio);
7050 return (CTL_RETVAL_COMPLETE);
7053 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
7055 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7056 ctsio->kern_sg_entries = 0;
7057 ctsio->kern_data_resid = 0;
7058 ctsio->kern_rel_offset = 0;
7059 if (total_len < alloc_len) {
7060 ctsio->residual = alloc_len - total_len;
7061 ctsio->kern_data_len = total_len;
7062 ctsio->kern_total_len = total_len;
7064 ctsio->residual = 0;
7065 ctsio->kern_data_len = alloc_len;
7066 ctsio->kern_total_len = alloc_len;
7069 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
7070 header->page = page_index->page_code;
7071 if (page_index->subpage) {
7072 header->page |= SL_SPF;
7073 header->subpage = page_index->subpage;
7075 scsi_ulto2b(page_index->page_len, header->datalen);
7078 * Call the handler, if it exists, to update the
7079 * page to the latest values.
7081 if (page_index->sense_handler != NULL)
7082 page_index->sense_handler(ctsio, page_index, pc);
7084 memcpy(header + 1, page_index->page_data, page_index->page_len);
7086 ctl_set_success(ctsio);
7087 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7088 ctsio->be_move_done = ctl_config_move_done;
7089 ctl_datamove((union ctl_io *)ctsio);
7090 return (CTL_RETVAL_COMPLETE);
7094 ctl_read_capacity(struct ctl_scsiio *ctsio)
7096 struct scsi_read_capacity *cdb;
7097 struct scsi_read_capacity_data *data;
7098 struct ctl_lun *lun;
7101 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
7103 cdb = (struct scsi_read_capacity *)ctsio->cdb;
7105 lba = scsi_4btoul(cdb->addr);
7106 if (((cdb->pmi & SRC_PMI) == 0)
7108 ctl_set_invalid_field(/*ctsio*/ ctsio,
7114 ctl_done((union ctl_io *)ctsio);
7115 return (CTL_RETVAL_COMPLETE);
7118 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7120 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7121 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
7122 ctsio->residual = 0;
7123 ctsio->kern_data_len = sizeof(*data);
7124 ctsio->kern_total_len = sizeof(*data);
7125 ctsio->kern_data_resid = 0;
7126 ctsio->kern_rel_offset = 0;
7127 ctsio->kern_sg_entries = 0;
7130 * If the maximum LBA is greater than 0xfffffffe, the user must
7131 * issue a SERVICE ACTION IN (16) command, with the read capacity
7132 * serivce action set.
7134 if (lun->be_lun->maxlba > 0xfffffffe)
7135 scsi_ulto4b(0xffffffff, data->addr);
7137 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
7140 * XXX KDM this may not be 512 bytes...
7142 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7144 ctl_set_success(ctsio);
7145 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7146 ctsio->be_move_done = ctl_config_move_done;
7147 ctl_datamove((union ctl_io *)ctsio);
7148 return (CTL_RETVAL_COMPLETE);
7152 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
7154 struct scsi_read_capacity_16 *cdb;
7155 struct scsi_read_capacity_data_long *data;
7156 struct ctl_lun *lun;
7160 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
7162 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
7164 alloc_len = scsi_4btoul(cdb->alloc_len);
7165 lba = scsi_8btou64(cdb->addr);
7167 if ((cdb->reladr & SRC16_PMI)
7169 ctl_set_invalid_field(/*ctsio*/ ctsio,
7175 ctl_done((union ctl_io *)ctsio);
7176 return (CTL_RETVAL_COMPLETE);
7179 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7181 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7182 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
7184 if (sizeof(*data) < alloc_len) {
7185 ctsio->residual = alloc_len - sizeof(*data);
7186 ctsio->kern_data_len = sizeof(*data);
7187 ctsio->kern_total_len = sizeof(*data);
7189 ctsio->residual = 0;
7190 ctsio->kern_data_len = alloc_len;
7191 ctsio->kern_total_len = alloc_len;
7193 ctsio->kern_data_resid = 0;
7194 ctsio->kern_rel_offset = 0;
7195 ctsio->kern_sg_entries = 0;
7197 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7198 /* XXX KDM this may not be 512 bytes... */
7199 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7200 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7201 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7202 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7203 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
7205 ctl_set_success(ctsio);
7206 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7207 ctsio->be_move_done = ctl_config_move_done;
7208 ctl_datamove((union ctl_io *)ctsio);
7209 return (CTL_RETVAL_COMPLETE);
7213 ctl_get_lba_status(struct ctl_scsiio *ctsio)
7215 struct scsi_get_lba_status *cdb;
7216 struct scsi_get_lba_status_data *data;
7217 struct ctl_lun *lun;
7218 struct ctl_lba_len_flags *lbalen;
7220 uint32_t alloc_len, total_len;
7223 CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
7225 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7226 cdb = (struct scsi_get_lba_status *)ctsio->cdb;
7227 lba = scsi_8btou64(cdb->addr);
7228 alloc_len = scsi_4btoul(cdb->alloc_len);
7230 if (lba > lun->be_lun->maxlba) {
7231 ctl_set_lba_out_of_range(ctsio);
7232 ctl_done((union ctl_io *)ctsio);
7233 return (CTL_RETVAL_COMPLETE);
7236 total_len = sizeof(*data) + sizeof(data->descr[0]);
7237 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7238 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
7240 if (total_len < alloc_len) {
7241 ctsio->residual = alloc_len - total_len;
7242 ctsio->kern_data_len = total_len;
7243 ctsio->kern_total_len = total_len;
7245 ctsio->residual = 0;
7246 ctsio->kern_data_len = alloc_len;
7247 ctsio->kern_total_len = alloc_len;
7249 ctsio->kern_data_resid = 0;
7250 ctsio->kern_rel_offset = 0;
7251 ctsio->kern_sg_entries = 0;
7253 /* Fill dummy data in case backend can't tell anything. */
7254 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
7255 scsi_u64to8b(lba, data->descr[0].addr);
7256 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
7257 data->descr[0].length);
7258 data->descr[0].status = 0; /* Mapped or unknown. */
7260 ctl_set_success(ctsio);
7261 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7262 ctsio->be_move_done = ctl_config_move_done;
7264 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
7266 lbalen->len = total_len;
7268 retval = lun->backend->config_read((union ctl_io *)ctsio);
7269 return (CTL_RETVAL_COMPLETE);
7273 ctl_read_defect(struct ctl_scsiio *ctsio)
7275 struct scsi_read_defect_data_10 *ccb10;
7276 struct scsi_read_defect_data_12 *ccb12;
7277 struct scsi_read_defect_data_hdr_10 *data10;
7278 struct scsi_read_defect_data_hdr_12 *data12;
7279 uint32_t alloc_len, data_len;
7282 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7284 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7285 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7286 format = ccb10->format;
7287 alloc_len = scsi_2btoul(ccb10->alloc_length);
7288 data_len = sizeof(*data10);
7290 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7291 format = ccb12->format;
7292 alloc_len = scsi_4btoul(ccb12->alloc_length);
7293 data_len = sizeof(*data12);
7295 if (alloc_len == 0) {
7296 ctl_set_success(ctsio);
7297 ctl_done((union ctl_io *)ctsio);
7298 return (CTL_RETVAL_COMPLETE);
7301 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7302 if (data_len < alloc_len) {
7303 ctsio->residual = alloc_len - data_len;
7304 ctsio->kern_data_len = data_len;
7305 ctsio->kern_total_len = data_len;
7307 ctsio->residual = 0;
7308 ctsio->kern_data_len = alloc_len;
7309 ctsio->kern_total_len = alloc_len;
7311 ctsio->kern_data_resid = 0;
7312 ctsio->kern_rel_offset = 0;
7313 ctsio->kern_sg_entries = 0;
7315 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7316 data10 = (struct scsi_read_defect_data_hdr_10 *)
7317 ctsio->kern_data_ptr;
7318 data10->format = format;
7319 scsi_ulto2b(0, data10->length);
7321 data12 = (struct scsi_read_defect_data_hdr_12 *)
7322 ctsio->kern_data_ptr;
7323 data12->format = format;
7324 scsi_ulto2b(0, data12->generation);
7325 scsi_ulto4b(0, data12->length);
7328 ctl_set_success(ctsio);
7329 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7330 ctsio->be_move_done = ctl_config_move_done;
7331 ctl_datamove((union ctl_io *)ctsio);
7332 return (CTL_RETVAL_COMPLETE);
7336 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7338 struct scsi_maintenance_in *cdb;
7340 int alloc_len, ext, total_len = 0, g, pc, pg, gs, os;
7341 int num_target_port_groups, num_target_ports;
7342 struct ctl_lun *lun;
7343 struct ctl_softc *softc;
7344 struct ctl_port *port;
7345 struct scsi_target_group_data *rtg_ptr;
7346 struct scsi_target_group_data_extended *rtg_ext_ptr;
7347 struct scsi_target_port_group_descriptor *tpg_desc;
7349 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7351 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7352 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7353 softc = lun->ctl_softc;
7355 retval = CTL_RETVAL_COMPLETE;
7357 switch (cdb->byte2 & STG_PDF_MASK) {
7358 case STG_PDF_LENGTH:
7361 case STG_PDF_EXTENDED:
7365 ctl_set_invalid_field(/*ctsio*/ ctsio,
7371 ctl_done((union ctl_io *)ctsio);
7375 if (softc->is_single)
7376 num_target_port_groups = 1;
7378 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
7379 num_target_ports = 0;
7380 mtx_lock(&softc->ctl_lock);
7381 STAILQ_FOREACH(port, &softc->port_list, links) {
7382 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7384 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7388 mtx_unlock(&softc->ctl_lock);
7391 total_len = sizeof(struct scsi_target_group_data_extended);
7393 total_len = sizeof(struct scsi_target_group_data);
7394 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7395 num_target_port_groups +
7396 sizeof(struct scsi_target_port_descriptor) * num_target_ports;
7398 alloc_len = scsi_4btoul(cdb->length);
7400 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7402 ctsio->kern_sg_entries = 0;
7404 if (total_len < alloc_len) {
7405 ctsio->residual = alloc_len - total_len;
7406 ctsio->kern_data_len = total_len;
7407 ctsio->kern_total_len = total_len;
7409 ctsio->residual = 0;
7410 ctsio->kern_data_len = alloc_len;
7411 ctsio->kern_total_len = alloc_len;
7413 ctsio->kern_data_resid = 0;
7414 ctsio->kern_rel_offset = 0;
7417 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7418 ctsio->kern_data_ptr;
7419 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7420 rtg_ext_ptr->format_type = 0x10;
7421 rtg_ext_ptr->implicit_transition_time = 0;
7422 tpg_desc = &rtg_ext_ptr->groups[0];
7424 rtg_ptr = (struct scsi_target_group_data *)
7425 ctsio->kern_data_ptr;
7426 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7427 tpg_desc = &rtg_ptr->groups[0];
7430 mtx_lock(&softc->ctl_lock);
7431 pg = softc->port_min / softc->port_cnt;
7432 if (softc->ha_link == CTL_HA_LINK_OFFLINE)
7433 gs = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7434 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN)
7435 gs = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7436 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY)
7437 gs = TPG_ASYMMETRIC_ACCESS_STANDBY;
7439 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7440 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7442 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7444 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7445 for (g = 0; g < num_target_port_groups; g++) {
7446 tpg_desc->pref_state = (g == pg) ? gs : os;
7447 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7448 TPG_U_SUP | TPG_T_SUP;
7449 scsi_ulto2b(g + 1, tpg_desc->target_port_group);
7450 tpg_desc->status = TPG_IMPLICIT;
7452 STAILQ_FOREACH(port, &softc->port_list, links) {
7453 if (port->targ_port < g * softc->port_cnt ||
7454 port->targ_port >= (g + 1) * softc->port_cnt)
7456 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7458 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7460 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7461 relative_target_port_identifier);
7464 tpg_desc->target_port_count = pc;
7465 tpg_desc = (struct scsi_target_port_group_descriptor *)
7466 &tpg_desc->descriptors[pc];
7468 mtx_unlock(&softc->ctl_lock);
7470 ctl_set_success(ctsio);
7471 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7472 ctsio->be_move_done = ctl_config_move_done;
7473 ctl_datamove((union ctl_io *)ctsio);
7478 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7480 struct ctl_lun *lun;
7481 struct scsi_report_supported_opcodes *cdb;
7482 const struct ctl_cmd_entry *entry, *sentry;
7483 struct scsi_report_supported_opcodes_all *all;
7484 struct scsi_report_supported_opcodes_descr *descr;
7485 struct scsi_report_supported_opcodes_one *one;
7487 int alloc_len, total_len;
7488 int opcode, service_action, i, j, num;
7490 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7492 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7493 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7495 retval = CTL_RETVAL_COMPLETE;
7497 opcode = cdb->requested_opcode;
7498 service_action = scsi_2btoul(cdb->requested_service_action);
7499 switch (cdb->options & RSO_OPTIONS_MASK) {
7500 case RSO_OPTIONS_ALL:
7502 for (i = 0; i < 256; i++) {
7503 entry = &ctl_cmd_table[i];
7504 if (entry->flags & CTL_CMD_FLAG_SA5) {
7505 for (j = 0; j < 32; j++) {
7506 sentry = &((const struct ctl_cmd_entry *)
7508 if (ctl_cmd_applicable(
7509 lun->be_lun->lun_type, sentry))
7513 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7518 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7519 num * sizeof(struct scsi_report_supported_opcodes_descr);
7521 case RSO_OPTIONS_OC:
7522 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7523 ctl_set_invalid_field(/*ctsio*/ ctsio,
7529 ctl_done((union ctl_io *)ctsio);
7530 return (CTL_RETVAL_COMPLETE);
7532 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7534 case RSO_OPTIONS_OC_SA:
7535 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7536 service_action >= 32) {
7537 ctl_set_invalid_field(/*ctsio*/ ctsio,
7543 ctl_done((union ctl_io *)ctsio);
7544 return (CTL_RETVAL_COMPLETE);
7546 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7549 ctl_set_invalid_field(/*ctsio*/ ctsio,
7555 ctl_done((union ctl_io *)ctsio);
7556 return (CTL_RETVAL_COMPLETE);
7559 alloc_len = scsi_4btoul(cdb->length);
7561 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7563 ctsio->kern_sg_entries = 0;
7565 if (total_len < alloc_len) {
7566 ctsio->residual = alloc_len - total_len;
7567 ctsio->kern_data_len = total_len;
7568 ctsio->kern_total_len = total_len;
7570 ctsio->residual = 0;
7571 ctsio->kern_data_len = alloc_len;
7572 ctsio->kern_total_len = alloc_len;
7574 ctsio->kern_data_resid = 0;
7575 ctsio->kern_rel_offset = 0;
7577 switch (cdb->options & RSO_OPTIONS_MASK) {
7578 case RSO_OPTIONS_ALL:
7579 all = (struct scsi_report_supported_opcodes_all *)
7580 ctsio->kern_data_ptr;
7582 for (i = 0; i < 256; i++) {
7583 entry = &ctl_cmd_table[i];
7584 if (entry->flags & CTL_CMD_FLAG_SA5) {
7585 for (j = 0; j < 32; j++) {
7586 sentry = &((const struct ctl_cmd_entry *)
7588 if (!ctl_cmd_applicable(
7589 lun->be_lun->lun_type, sentry))
7591 descr = &all->descr[num++];
7593 scsi_ulto2b(j, descr->service_action);
7594 descr->flags = RSO_SERVACTV;
7595 scsi_ulto2b(sentry->length,
7599 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7602 descr = &all->descr[num++];
7604 scsi_ulto2b(0, descr->service_action);
7606 scsi_ulto2b(entry->length, descr->cdb_length);
7610 num * sizeof(struct scsi_report_supported_opcodes_descr),
7613 case RSO_OPTIONS_OC:
7614 one = (struct scsi_report_supported_opcodes_one *)
7615 ctsio->kern_data_ptr;
7616 entry = &ctl_cmd_table[opcode];
7618 case RSO_OPTIONS_OC_SA:
7619 one = (struct scsi_report_supported_opcodes_one *)
7620 ctsio->kern_data_ptr;
7621 entry = &ctl_cmd_table[opcode];
7622 entry = &((const struct ctl_cmd_entry *)
7623 entry->execute)[service_action];
7625 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7627 scsi_ulto2b(entry->length, one->cdb_length);
7628 one->cdb_usage[0] = opcode;
7629 memcpy(&one->cdb_usage[1], entry->usage,
7636 ctl_set_success(ctsio);
7637 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7638 ctsio->be_move_done = ctl_config_move_done;
7639 ctl_datamove((union ctl_io *)ctsio);
7644 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7646 struct scsi_report_supported_tmf *cdb;
7647 struct scsi_report_supported_tmf_data *data;
7649 int alloc_len, total_len;
7651 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7653 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7655 retval = CTL_RETVAL_COMPLETE;
7657 total_len = sizeof(struct scsi_report_supported_tmf_data);
7658 alloc_len = scsi_4btoul(cdb->length);
7660 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7662 ctsio->kern_sg_entries = 0;
7664 if (total_len < alloc_len) {
7665 ctsio->residual = alloc_len - total_len;
7666 ctsio->kern_data_len = total_len;
7667 ctsio->kern_total_len = total_len;
7669 ctsio->residual = 0;
7670 ctsio->kern_data_len = alloc_len;
7671 ctsio->kern_total_len = alloc_len;
7673 ctsio->kern_data_resid = 0;
7674 ctsio->kern_rel_offset = 0;
7676 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr;
7677 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS |
7679 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS;
7681 ctl_set_success(ctsio);
7682 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7683 ctsio->be_move_done = ctl_config_move_done;
7684 ctl_datamove((union ctl_io *)ctsio);
7689 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7691 struct scsi_report_timestamp *cdb;
7692 struct scsi_report_timestamp_data *data;
7696 int alloc_len, total_len;
7698 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7700 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7702 retval = CTL_RETVAL_COMPLETE;
7704 total_len = sizeof(struct scsi_report_timestamp_data);
7705 alloc_len = scsi_4btoul(cdb->length);
7707 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7709 ctsio->kern_sg_entries = 0;
7711 if (total_len < alloc_len) {
7712 ctsio->residual = alloc_len - total_len;
7713 ctsio->kern_data_len = total_len;
7714 ctsio->kern_total_len = total_len;
7716 ctsio->residual = 0;
7717 ctsio->kern_data_len = alloc_len;
7718 ctsio->kern_total_len = alloc_len;
7720 ctsio->kern_data_resid = 0;
7721 ctsio->kern_rel_offset = 0;
7723 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7724 scsi_ulto2b(sizeof(*data) - 2, data->length);
7725 data->origin = RTS_ORIG_OUTSIDE;
7727 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7728 scsi_ulto4b(timestamp >> 16, data->timestamp);
7729 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7731 ctl_set_success(ctsio);
7732 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7733 ctsio->be_move_done = ctl_config_move_done;
7734 ctl_datamove((union ctl_io *)ctsio);
7739 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7741 struct scsi_per_res_in *cdb;
7742 int alloc_len, total_len = 0;
7743 /* struct scsi_per_res_in_rsrv in_data; */
7744 struct ctl_lun *lun;
7745 struct ctl_softc *softc;
7748 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7750 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7752 alloc_len = scsi_2btoul(cdb->length);
7754 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7755 softc = lun->ctl_softc;
7758 mtx_lock(&lun->lun_lock);
7759 switch (cdb->action) {
7760 case SPRI_RK: /* read keys */
7761 total_len = sizeof(struct scsi_per_res_in_keys) +
7763 sizeof(struct scsi_per_res_key);
7765 case SPRI_RR: /* read reservation */
7766 if (lun->flags & CTL_LUN_PR_RESERVED)
7767 total_len = sizeof(struct scsi_per_res_in_rsrv);
7769 total_len = sizeof(struct scsi_per_res_in_header);
7771 case SPRI_RC: /* report capabilities */
7772 total_len = sizeof(struct scsi_per_res_cap);
7774 case SPRI_RS: /* read full status */
7775 total_len = sizeof(struct scsi_per_res_in_header) +
7776 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7780 panic("Invalid PR type %x", cdb->action);
7782 mtx_unlock(&lun->lun_lock);
7784 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7786 if (total_len < alloc_len) {
7787 ctsio->residual = alloc_len - total_len;
7788 ctsio->kern_data_len = total_len;
7789 ctsio->kern_total_len = total_len;
7791 ctsio->residual = 0;
7792 ctsio->kern_data_len = alloc_len;
7793 ctsio->kern_total_len = alloc_len;
7796 ctsio->kern_data_resid = 0;
7797 ctsio->kern_rel_offset = 0;
7798 ctsio->kern_sg_entries = 0;
7800 mtx_lock(&lun->lun_lock);
7801 switch (cdb->action) {
7802 case SPRI_RK: { // read keys
7803 struct scsi_per_res_in_keys *res_keys;
7806 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7809 * We had to drop the lock to allocate our buffer, which
7810 * leaves time for someone to come in with another
7811 * persistent reservation. (That is unlikely, though,
7812 * since this should be the only persistent reservation
7813 * command active right now.)
7815 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7816 (lun->pr_key_count *
7817 sizeof(struct scsi_per_res_key)))){
7818 mtx_unlock(&lun->lun_lock);
7819 free(ctsio->kern_data_ptr, M_CTL);
7820 printf("%s: reservation length changed, retrying\n",
7825 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7827 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7828 lun->pr_key_count, res_keys->header.length);
7830 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) {
7831 if ((key = ctl_get_prkey(lun, i)) == 0)
7835 * We used lun->pr_key_count to calculate the
7836 * size to allocate. If it turns out the number of
7837 * initiators with the registered flag set is
7838 * larger than that (i.e. they haven't been kept in
7839 * sync), we've got a problem.
7841 if (key_count >= lun->pr_key_count) {
7843 csevent_log(CSC_CTL | CSC_SHELF_SW |
7845 csevent_LogType_Fault,
7846 csevent_AlertLevel_Yellow,
7847 csevent_FRU_ShelfController,
7848 csevent_FRU_Firmware,
7849 csevent_FRU_Unknown,
7850 "registered keys %d >= key "
7851 "count %d", key_count,
7857 scsi_u64to8b(key, res_keys->keys[key_count].key);
7862 case SPRI_RR: { // read reservation
7863 struct scsi_per_res_in_rsrv *res;
7864 int tmp_len, header_only;
7866 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7868 scsi_ulto4b(lun->PRGeneration, res->header.generation);
7870 if (lun->flags & CTL_LUN_PR_RESERVED)
7872 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7873 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7874 res->header.length);
7877 tmp_len = sizeof(struct scsi_per_res_in_header);
7878 scsi_ulto4b(0, res->header.length);
7883 * We had to drop the lock to allocate our buffer, which
7884 * leaves time for someone to come in with another
7885 * persistent reservation. (That is unlikely, though,
7886 * since this should be the only persistent reservation
7887 * command active right now.)
7889 if (tmp_len != total_len) {
7890 mtx_unlock(&lun->lun_lock);
7891 free(ctsio->kern_data_ptr, M_CTL);
7892 printf("%s: reservation status changed, retrying\n",
7898 * No reservation held, so we're done.
7900 if (header_only != 0)
7904 * If the registration is an All Registrants type, the key
7905 * is 0, since it doesn't really matter.
7907 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7908 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7909 res->data.reservation);
7911 res->data.scopetype = lun->res_type;
7914 case SPRI_RC: //report capabilities
7916 struct scsi_per_res_cap *res_cap;
7919 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7920 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7921 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5;
7922 type_mask = SPRI_TM_WR_EX_AR |
7928 scsi_ulto2b(type_mask, res_cap->type_mask);
7931 case SPRI_RS: { // read full status
7932 struct scsi_per_res_in_full *res_status;
7933 struct scsi_per_res_in_full_desc *res_desc;
7934 struct ctl_port *port;
7937 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7940 * We had to drop the lock to allocate our buffer, which
7941 * leaves time for someone to come in with another
7942 * persistent reservation. (That is unlikely, though,
7943 * since this should be the only persistent reservation
7944 * command active right now.)
7946 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7947 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7948 lun->pr_key_count)){
7949 mtx_unlock(&lun->lun_lock);
7950 free(ctsio->kern_data_ptr, M_CTL);
7951 printf("%s: reservation length changed, retrying\n",
7956 scsi_ulto4b(lun->PRGeneration, res_status->header.generation);
7958 res_desc = &res_status->desc[0];
7959 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7960 if ((key = ctl_get_prkey(lun, i)) == 0)
7963 scsi_u64to8b(key, res_desc->res_key.key);
7964 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7965 (lun->pr_res_idx == i ||
7966 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7967 res_desc->flags = SPRI_FULL_R_HOLDER;
7968 res_desc->scopetype = lun->res_type;
7970 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7971 res_desc->rel_trgt_port_id);
7973 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7975 len = ctl_create_iid(port,
7976 i % CTL_MAX_INIT_PER_PORT,
7977 res_desc->transport_id);
7978 scsi_ulto4b(len, res_desc->additional_length);
7979 res_desc = (struct scsi_per_res_in_full_desc *)
7980 &res_desc->transport_id[len];
7982 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7983 res_status->header.length);
7988 * This is a bug, because we just checked for this above,
7989 * and should have returned an error.
7991 panic("Invalid PR type %x", cdb->action);
7992 break; /* NOTREACHED */
7994 mtx_unlock(&lun->lun_lock);
7996 ctl_set_success(ctsio);
7997 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7998 ctsio->be_move_done = ctl_config_move_done;
7999 ctl_datamove((union ctl_io *)ctsio);
8000 return (CTL_RETVAL_COMPLETE);
8004 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
8008 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
8009 uint64_t sa_res_key, uint8_t type, uint32_t residx,
8010 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
8011 struct scsi_per_res_out_parms* param)
8013 union ctl_ha_msg persis_io;
8016 mtx_lock(&lun->lun_lock);
8017 if (sa_res_key == 0) {
8018 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8019 /* validate scope and type */
8020 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
8022 mtx_unlock(&lun->lun_lock);
8023 ctl_set_invalid_field(/*ctsio*/ ctsio,
8029 ctl_done((union ctl_io *)ctsio);
8033 if (type>8 || type==2 || type==4 || type==0) {
8034 mtx_unlock(&lun->lun_lock);
8035 ctl_set_invalid_field(/*ctsio*/ ctsio,
8041 ctl_done((union ctl_io *)ctsio);
8046 * Unregister everybody else and build UA for
8049 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8050 if (i == residx || ctl_get_prkey(lun, i) == 0)
8053 ctl_clr_prkey(lun, i);
8054 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8056 lun->pr_key_count = 1;
8057 lun->res_type = type;
8058 if (lun->res_type != SPR_TYPE_WR_EX_AR
8059 && lun->res_type != SPR_TYPE_EX_AC_AR)
8060 lun->pr_res_idx = residx;
8061 lun->PRGeneration++;
8062 mtx_unlock(&lun->lun_lock);
8064 /* send msg to other side */
8065 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8066 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8067 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8068 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8069 persis_io.pr.pr_info.res_type = type;
8070 memcpy(persis_io.pr.pr_info.sa_res_key,
8071 param->serv_act_res_key,
8072 sizeof(param->serv_act_res_key));
8073 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8074 sizeof(persis_io.pr), M_WAITOK);
8076 /* not all registrants */
8077 mtx_unlock(&lun->lun_lock);
8078 free(ctsio->kern_data_ptr, M_CTL);
8079 ctl_set_invalid_field(ctsio,
8085 ctl_done((union ctl_io *)ctsio);
8088 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8089 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
8092 if (res_key == sa_res_key) {
8095 * The spec implies this is not good but doesn't
8096 * say what to do. There are two choices either
8097 * generate a res conflict or check condition
8098 * with illegal field in parameter data. Since
8099 * that is what is done when the sa_res_key is
8100 * zero I'll take that approach since this has
8101 * to do with the sa_res_key.
8103 mtx_unlock(&lun->lun_lock);
8104 free(ctsio->kern_data_ptr, M_CTL);
8105 ctl_set_invalid_field(ctsio,
8111 ctl_done((union ctl_io *)ctsio);
8115 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8116 if (ctl_get_prkey(lun, i) != sa_res_key)
8120 ctl_clr_prkey(lun, i);
8121 lun->pr_key_count--;
8122 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8125 mtx_unlock(&lun->lun_lock);
8126 free(ctsio->kern_data_ptr, M_CTL);
8127 ctl_set_reservation_conflict(ctsio);
8128 ctl_done((union ctl_io *)ctsio);
8129 return (CTL_RETVAL_COMPLETE);
8131 lun->PRGeneration++;
8132 mtx_unlock(&lun->lun_lock);
8134 /* send msg to other side */
8135 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8136 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8137 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8138 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8139 persis_io.pr.pr_info.res_type = type;
8140 memcpy(persis_io.pr.pr_info.sa_res_key,
8141 param->serv_act_res_key,
8142 sizeof(param->serv_act_res_key));
8143 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8144 sizeof(persis_io.pr), M_WAITOK);
8146 /* Reserved but not all registrants */
8147 /* sa_res_key is res holder */
8148 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
8149 /* validate scope and type */
8150 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
8152 mtx_unlock(&lun->lun_lock);
8153 ctl_set_invalid_field(/*ctsio*/ ctsio,
8159 ctl_done((union ctl_io *)ctsio);
8163 if (type>8 || type==2 || type==4 || type==0) {
8164 mtx_unlock(&lun->lun_lock);
8165 ctl_set_invalid_field(/*ctsio*/ ctsio,
8171 ctl_done((union ctl_io *)ctsio);
8177 * if sa_res_key != res_key remove all
8178 * registrants w/sa_res_key and generate UA
8179 * for these registrants(Registrations
8180 * Preempted) if it wasn't an exclusive
8181 * reservation generate UA(Reservations
8182 * Preempted) for all other registered nexuses
8183 * if the type has changed. Establish the new
8184 * reservation and holder. If res_key and
8185 * sa_res_key are the same do the above
8186 * except don't unregister the res holder.
8189 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8190 if (i == residx || ctl_get_prkey(lun, i) == 0)
8193 if (sa_res_key == ctl_get_prkey(lun, i)) {
8194 ctl_clr_prkey(lun, i);
8195 lun->pr_key_count--;
8196 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8197 } else if (type != lun->res_type
8198 && (lun->res_type == SPR_TYPE_WR_EX_RO
8199 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
8200 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8203 lun->res_type = type;
8204 if (lun->res_type != SPR_TYPE_WR_EX_AR
8205 && lun->res_type != SPR_TYPE_EX_AC_AR)
8206 lun->pr_res_idx = residx;
8208 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8209 lun->PRGeneration++;
8210 mtx_unlock(&lun->lun_lock);
8212 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8213 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8214 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8215 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8216 persis_io.pr.pr_info.res_type = type;
8217 memcpy(persis_io.pr.pr_info.sa_res_key,
8218 param->serv_act_res_key,
8219 sizeof(param->serv_act_res_key));
8220 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8221 sizeof(persis_io.pr), M_WAITOK);
8224 * sa_res_key is not the res holder just
8225 * remove registrants
8229 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8230 if (sa_res_key != ctl_get_prkey(lun, i))
8234 ctl_clr_prkey(lun, i);
8235 lun->pr_key_count--;
8236 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8240 mtx_unlock(&lun->lun_lock);
8241 free(ctsio->kern_data_ptr, M_CTL);
8242 ctl_set_reservation_conflict(ctsio);
8243 ctl_done((union ctl_io *)ctsio);
8246 lun->PRGeneration++;
8247 mtx_unlock(&lun->lun_lock);
8249 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8250 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8251 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8252 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8253 persis_io.pr.pr_info.res_type = type;
8254 memcpy(persis_io.pr.pr_info.sa_res_key,
8255 param->serv_act_res_key,
8256 sizeof(param->serv_act_res_key));
8257 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8258 sizeof(persis_io.pr), M_WAITOK);
8265 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8267 uint64_t sa_res_key;
8270 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8272 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8273 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8274 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
8275 if (sa_res_key == 0) {
8277 * Unregister everybody else and build UA for
8280 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8281 if (i == msg->pr.pr_info.residx ||
8282 ctl_get_prkey(lun, i) == 0)
8285 ctl_clr_prkey(lun, i);
8286 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8289 lun->pr_key_count = 1;
8290 lun->res_type = msg->pr.pr_info.res_type;
8291 if (lun->res_type != SPR_TYPE_WR_EX_AR
8292 && lun->res_type != SPR_TYPE_EX_AC_AR)
8293 lun->pr_res_idx = msg->pr.pr_info.residx;
8295 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8296 if (sa_res_key == ctl_get_prkey(lun, i))
8299 ctl_clr_prkey(lun, i);
8300 lun->pr_key_count--;
8301 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8305 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8306 if (i == msg->pr.pr_info.residx ||
8307 ctl_get_prkey(lun, i) == 0)
8310 if (sa_res_key == ctl_get_prkey(lun, i)) {
8311 ctl_clr_prkey(lun, i);
8312 lun->pr_key_count--;
8313 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8314 } else if (msg->pr.pr_info.res_type != lun->res_type
8315 && (lun->res_type == SPR_TYPE_WR_EX_RO
8316 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
8317 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8320 lun->res_type = msg->pr.pr_info.res_type;
8321 if (lun->res_type != SPR_TYPE_WR_EX_AR
8322 && lun->res_type != SPR_TYPE_EX_AC_AR)
8323 lun->pr_res_idx = msg->pr.pr_info.residx;
8325 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8327 lun->PRGeneration++;
8333 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8336 u_int32_t param_len;
8337 struct scsi_per_res_out *cdb;
8338 struct ctl_lun *lun;
8339 struct scsi_per_res_out_parms* param;
8340 struct ctl_softc *softc;
8342 uint64_t res_key, sa_res_key, key;
8344 union ctl_ha_msg persis_io;
8347 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8349 retval = CTL_RETVAL_COMPLETE;
8351 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8352 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8353 softc = lun->ctl_softc;
8356 * We only support whole-LUN scope. The scope & type are ignored for
8357 * register, register and ignore existing key and clear.
8358 * We sometimes ignore scope and type on preempts too!!
8359 * Verify reservation type here as well.
8361 type = cdb->scope_type & SPR_TYPE_MASK;
8362 if ((cdb->action == SPRO_RESERVE)
8363 || (cdb->action == SPRO_RELEASE)) {
8364 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8365 ctl_set_invalid_field(/*ctsio*/ ctsio,
8371 ctl_done((union ctl_io *)ctsio);
8372 return (CTL_RETVAL_COMPLETE);
8375 if (type>8 || type==2 || type==4 || type==0) {
8376 ctl_set_invalid_field(/*ctsio*/ ctsio,
8382 ctl_done((union ctl_io *)ctsio);
8383 return (CTL_RETVAL_COMPLETE);
8387 param_len = scsi_4btoul(cdb->length);
8389 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8390 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8391 ctsio->kern_data_len = param_len;
8392 ctsio->kern_total_len = param_len;
8393 ctsio->kern_data_resid = 0;
8394 ctsio->kern_rel_offset = 0;
8395 ctsio->kern_sg_entries = 0;
8396 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8397 ctsio->be_move_done = ctl_config_move_done;
8398 ctl_datamove((union ctl_io *)ctsio);
8400 return (CTL_RETVAL_COMPLETE);
8403 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8405 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8406 res_key = scsi_8btou64(param->res_key.key);
8407 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8410 * Validate the reservation key here except for SPRO_REG_IGNO
8411 * This must be done for all other service actions
8413 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8414 mtx_lock(&lun->lun_lock);
8415 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8416 if (res_key != key) {
8418 * The current key passed in doesn't match
8419 * the one the initiator previously
8422 mtx_unlock(&lun->lun_lock);
8423 free(ctsio->kern_data_ptr, M_CTL);
8424 ctl_set_reservation_conflict(ctsio);
8425 ctl_done((union ctl_io *)ctsio);
8426 return (CTL_RETVAL_COMPLETE);
8428 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8430 * We are not registered
8432 mtx_unlock(&lun->lun_lock);
8433 free(ctsio->kern_data_ptr, M_CTL);
8434 ctl_set_reservation_conflict(ctsio);
8435 ctl_done((union ctl_io *)ctsio);
8436 return (CTL_RETVAL_COMPLETE);
8437 } else if (res_key != 0) {
8439 * We are not registered and trying to register but
8440 * the register key isn't zero.
8442 mtx_unlock(&lun->lun_lock);
8443 free(ctsio->kern_data_ptr, M_CTL);
8444 ctl_set_reservation_conflict(ctsio);
8445 ctl_done((union ctl_io *)ctsio);
8446 return (CTL_RETVAL_COMPLETE);
8448 mtx_unlock(&lun->lun_lock);
8451 switch (cdb->action & SPRO_ACTION_MASK) {
8453 case SPRO_REG_IGNO: {
8456 printf("Registration received\n");
8460 * We don't support any of these options, as we report in
8461 * the read capabilities request (see
8462 * ctl_persistent_reserve_in(), above).
8464 if ((param->flags & SPR_SPEC_I_PT)
8465 || (param->flags & SPR_ALL_TG_PT)
8466 || (param->flags & SPR_APTPL)) {
8469 if (param->flags & SPR_APTPL)
8471 else if (param->flags & SPR_ALL_TG_PT)
8473 else /* SPR_SPEC_I_PT */
8476 free(ctsio->kern_data_ptr, M_CTL);
8477 ctl_set_invalid_field(ctsio,
8483 ctl_done((union ctl_io *)ctsio);
8484 return (CTL_RETVAL_COMPLETE);
8487 mtx_lock(&lun->lun_lock);
8490 * The initiator wants to clear the
8493 if (sa_res_key == 0) {
8495 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8496 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8497 && ctl_get_prkey(lun, residx) == 0)) {
8498 mtx_unlock(&lun->lun_lock);
8502 ctl_clr_prkey(lun, residx);
8503 lun->pr_key_count--;
8505 if (residx == lun->pr_res_idx) {
8506 lun->flags &= ~CTL_LUN_PR_RESERVED;
8507 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8509 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8510 || lun->res_type == SPR_TYPE_EX_AC_RO)
8511 && lun->pr_key_count) {
8513 * If the reservation is a registrants
8514 * only type we need to generate a UA
8515 * for other registered inits. The
8516 * sense code should be RESERVATIONS
8520 for (i = softc->init_min; i < softc->init_max; i++){
8521 if (ctl_get_prkey(lun, i) == 0)
8524 CTL_UA_RES_RELEASE);
8528 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8529 if (lun->pr_key_count==0) {
8530 lun->flags &= ~CTL_LUN_PR_RESERVED;
8532 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8535 lun->PRGeneration++;
8536 mtx_unlock(&lun->lun_lock);
8538 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8539 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8540 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8541 persis_io.pr.pr_info.residx = residx;
8542 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8543 sizeof(persis_io.pr), M_WAITOK);
8544 } else /* sa_res_key != 0 */ {
8547 * If we aren't registered currently then increment
8548 * the key count and set the registered flag.
8550 ctl_alloc_prkey(lun, residx);
8551 if (ctl_get_prkey(lun, residx) == 0)
8552 lun->pr_key_count++;
8553 ctl_set_prkey(lun, residx, sa_res_key);
8554 lun->PRGeneration++;
8555 mtx_unlock(&lun->lun_lock);
8557 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8558 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8559 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8560 persis_io.pr.pr_info.residx = residx;
8561 memcpy(persis_io.pr.pr_info.sa_res_key,
8562 param->serv_act_res_key,
8563 sizeof(param->serv_act_res_key));
8564 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8565 sizeof(persis_io.pr), M_WAITOK);
8572 printf("Reserve executed type %d\n", type);
8574 mtx_lock(&lun->lun_lock);
8575 if (lun->flags & CTL_LUN_PR_RESERVED) {
8577 * if this isn't the reservation holder and it's
8578 * not a "all registrants" type or if the type is
8579 * different then we have a conflict
8581 if ((lun->pr_res_idx != residx
8582 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8583 || lun->res_type != type) {
8584 mtx_unlock(&lun->lun_lock);
8585 free(ctsio->kern_data_ptr, M_CTL);
8586 ctl_set_reservation_conflict(ctsio);
8587 ctl_done((union ctl_io *)ctsio);
8588 return (CTL_RETVAL_COMPLETE);
8590 mtx_unlock(&lun->lun_lock);
8591 } else /* create a reservation */ {
8593 * If it's not an "all registrants" type record
8594 * reservation holder
8596 if (type != SPR_TYPE_WR_EX_AR
8597 && type != SPR_TYPE_EX_AC_AR)
8598 lun->pr_res_idx = residx; /* Res holder */
8600 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8602 lun->flags |= CTL_LUN_PR_RESERVED;
8603 lun->res_type = type;
8605 mtx_unlock(&lun->lun_lock);
8607 /* send msg to other side */
8608 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8609 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8610 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8611 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8612 persis_io.pr.pr_info.res_type = type;
8613 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8614 sizeof(persis_io.pr), M_WAITOK);
8619 mtx_lock(&lun->lun_lock);
8620 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8621 /* No reservation exists return good status */
8622 mtx_unlock(&lun->lun_lock);
8626 * Is this nexus a reservation holder?
8628 if (lun->pr_res_idx != residx
8629 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8631 * not a res holder return good status but
8634 mtx_unlock(&lun->lun_lock);
8638 if (lun->res_type != type) {
8639 mtx_unlock(&lun->lun_lock);
8640 free(ctsio->kern_data_ptr, M_CTL);
8641 ctl_set_illegal_pr_release(ctsio);
8642 ctl_done((union ctl_io *)ctsio);
8643 return (CTL_RETVAL_COMPLETE);
8646 /* okay to release */
8647 lun->flags &= ~CTL_LUN_PR_RESERVED;
8648 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8652 * if this isn't an exclusive access
8653 * res generate UA for all other
8656 if (type != SPR_TYPE_EX_AC
8657 && type != SPR_TYPE_WR_EX) {
8658 for (i = softc->init_min; i < softc->init_max; i++) {
8659 if (i == residx || ctl_get_prkey(lun, i) == 0)
8661 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8664 mtx_unlock(&lun->lun_lock);
8666 /* Send msg to other side */
8667 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8668 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8669 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8670 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8671 sizeof(persis_io.pr), M_WAITOK);
8675 /* send msg to other side */
8677 mtx_lock(&lun->lun_lock);
8678 lun->flags &= ~CTL_LUN_PR_RESERVED;
8680 lun->pr_key_count = 0;
8681 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8683 ctl_clr_prkey(lun, residx);
8684 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8685 if (ctl_get_prkey(lun, i) != 0) {
8686 ctl_clr_prkey(lun, i);
8687 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8689 lun->PRGeneration++;
8690 mtx_unlock(&lun->lun_lock);
8692 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8693 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8694 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8695 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8696 sizeof(persis_io.pr), M_WAITOK);
8700 case SPRO_PRE_ABO: {
8703 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8704 residx, ctsio, cdb, param);
8706 return (CTL_RETVAL_COMPLETE);
8710 panic("Invalid PR type %x", cdb->action);
8714 free(ctsio->kern_data_ptr, M_CTL);
8715 ctl_set_success(ctsio);
8716 ctl_done((union ctl_io *)ctsio);
8722 * This routine is for handling a message from the other SC pertaining to
8723 * persistent reserve out. All the error checking will have been done
8724 * so only perorming the action need be done here to keep the two
8728 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8730 struct ctl_lun *lun;
8731 struct ctl_softc *softc;
8733 uint32_t residx, targ_lun;
8735 softc = control_softc;
8736 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8737 mtx_lock(&softc->ctl_lock);
8738 if ((targ_lun >= CTL_MAX_LUNS) ||
8739 ((lun = softc->ctl_luns[targ_lun]) == NULL)) {
8740 mtx_unlock(&softc->ctl_lock);
8743 mtx_lock(&lun->lun_lock);
8744 mtx_unlock(&softc->ctl_lock);
8745 if (lun->flags & CTL_LUN_DISABLED) {
8746 mtx_unlock(&lun->lun_lock);
8749 residx = ctl_get_initindex(&msg->hdr.nexus);
8750 switch(msg->pr.pr_info.action) {
8751 case CTL_PR_REG_KEY:
8752 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8753 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8754 lun->pr_key_count++;
8755 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8756 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8757 lun->PRGeneration++;
8760 case CTL_PR_UNREG_KEY:
8761 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8762 lun->pr_key_count--;
8764 /* XXX Need to see if the reservation has been released */
8765 /* if so do we need to generate UA? */
8766 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8767 lun->flags &= ~CTL_LUN_PR_RESERVED;
8768 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8770 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8771 || lun->res_type == SPR_TYPE_EX_AC_RO)
8772 && lun->pr_key_count) {
8774 * If the reservation is a registrants
8775 * only type we need to generate a UA
8776 * for other registered inits. The
8777 * sense code should be RESERVATIONS
8781 for (i = softc->init_min; i < softc->init_max; i++) {
8782 if (ctl_get_prkey(lun, i) == 0)
8785 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8789 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8790 if (lun->pr_key_count==0) {
8791 lun->flags &= ~CTL_LUN_PR_RESERVED;
8793 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8796 lun->PRGeneration++;
8799 case CTL_PR_RESERVE:
8800 lun->flags |= CTL_LUN_PR_RESERVED;
8801 lun->res_type = msg->pr.pr_info.res_type;
8802 lun->pr_res_idx = msg->pr.pr_info.residx;
8806 case CTL_PR_RELEASE:
8808 * if this isn't an exclusive access res generate UA for all
8809 * other registrants.
8811 if (lun->res_type != SPR_TYPE_EX_AC
8812 && lun->res_type != SPR_TYPE_WR_EX) {
8813 for (i = softc->init_min; i < softc->init_max; i++)
8814 if (i == residx || ctl_get_prkey(lun, i) == 0)
8816 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8819 lun->flags &= ~CTL_LUN_PR_RESERVED;
8820 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8824 case CTL_PR_PREEMPT:
8825 ctl_pro_preempt_other(lun, msg);
8828 lun->flags &= ~CTL_LUN_PR_RESERVED;
8830 lun->pr_key_count = 0;
8831 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8833 for (i=0; i < CTL_MAX_INITIATORS; i++) {
8834 if (ctl_get_prkey(lun, i) == 0)
8836 ctl_clr_prkey(lun, i);
8837 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8839 lun->PRGeneration++;
8843 mtx_unlock(&lun->lun_lock);
8847 ctl_read_write(struct ctl_scsiio *ctsio)
8849 struct ctl_lun *lun;
8850 struct ctl_lba_len_flags *lbalen;
8852 uint32_t num_blocks;
8856 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8858 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8861 retval = CTL_RETVAL_COMPLETE;
8863 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8864 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8865 switch (ctsio->cdb[0]) {
8868 struct scsi_rw_6 *cdb;
8870 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8872 lba = scsi_3btoul(cdb->addr);
8873 /* only 5 bits are valid in the most significant address byte */
8875 num_blocks = cdb->length;
8877 * This is correct according to SBC-2.
8879 if (num_blocks == 0)
8885 struct scsi_rw_10 *cdb;
8887 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8888 if (cdb->byte2 & SRW10_FUA)
8889 flags |= CTL_LLF_FUA;
8890 if (cdb->byte2 & SRW10_DPO)
8891 flags |= CTL_LLF_DPO;
8892 lba = scsi_4btoul(cdb->addr);
8893 num_blocks = scsi_2btoul(cdb->length);
8896 case WRITE_VERIFY_10: {
8897 struct scsi_write_verify_10 *cdb;
8899 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8900 flags |= CTL_LLF_FUA;
8901 if (cdb->byte2 & SWV_DPO)
8902 flags |= CTL_LLF_DPO;
8903 lba = scsi_4btoul(cdb->addr);
8904 num_blocks = scsi_2btoul(cdb->length);
8909 struct scsi_rw_12 *cdb;
8911 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8912 if (cdb->byte2 & SRW12_FUA)
8913 flags |= CTL_LLF_FUA;
8914 if (cdb->byte2 & SRW12_DPO)
8915 flags |= CTL_LLF_DPO;
8916 lba = scsi_4btoul(cdb->addr);
8917 num_blocks = scsi_4btoul(cdb->length);
8920 case WRITE_VERIFY_12: {
8921 struct scsi_write_verify_12 *cdb;
8923 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8924 flags |= CTL_LLF_FUA;
8925 if (cdb->byte2 & SWV_DPO)
8926 flags |= CTL_LLF_DPO;
8927 lba = scsi_4btoul(cdb->addr);
8928 num_blocks = scsi_4btoul(cdb->length);
8933 struct scsi_rw_16 *cdb;
8935 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8936 if (cdb->byte2 & SRW12_FUA)
8937 flags |= CTL_LLF_FUA;
8938 if (cdb->byte2 & SRW12_DPO)
8939 flags |= CTL_LLF_DPO;
8940 lba = scsi_8btou64(cdb->addr);
8941 num_blocks = scsi_4btoul(cdb->length);
8944 case WRITE_ATOMIC_16: {
8945 struct scsi_write_atomic_16 *cdb;
8947 if (lun->be_lun->atomicblock == 0) {
8948 ctl_set_invalid_opcode(ctsio);
8949 ctl_done((union ctl_io *)ctsio);
8950 return (CTL_RETVAL_COMPLETE);
8953 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb;
8954 if (cdb->byte2 & SRW12_FUA)
8955 flags |= CTL_LLF_FUA;
8956 if (cdb->byte2 & SRW12_DPO)
8957 flags |= CTL_LLF_DPO;
8958 lba = scsi_8btou64(cdb->addr);
8959 num_blocks = scsi_2btoul(cdb->length);
8960 if (num_blocks > lun->be_lun->atomicblock) {
8961 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8962 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8964 ctl_done((union ctl_io *)ctsio);
8965 return (CTL_RETVAL_COMPLETE);
8969 case WRITE_VERIFY_16: {
8970 struct scsi_write_verify_16 *cdb;
8972 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8973 flags |= CTL_LLF_FUA;
8974 if (cdb->byte2 & SWV_DPO)
8975 flags |= CTL_LLF_DPO;
8976 lba = scsi_8btou64(cdb->addr);
8977 num_blocks = scsi_4btoul(cdb->length);
8982 * We got a command we don't support. This shouldn't
8983 * happen, commands should be filtered out above us.
8985 ctl_set_invalid_opcode(ctsio);
8986 ctl_done((union ctl_io *)ctsio);
8988 return (CTL_RETVAL_COMPLETE);
8989 break; /* NOTREACHED */
8993 * The first check is to make sure we're in bounds, the second
8994 * check is to catch wrap-around problems. If the lba + num blocks
8995 * is less than the lba, then we've wrapped around and the block
8996 * range is invalid anyway.
8998 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8999 || ((lba + num_blocks) < lba)) {
9000 ctl_set_lba_out_of_range(ctsio);
9001 ctl_done((union ctl_io *)ctsio);
9002 return (CTL_RETVAL_COMPLETE);
9006 * According to SBC-3, a transfer length of 0 is not an error.
9007 * Note that this cannot happen with WRITE(6) or READ(6), since 0
9008 * translates to 256 blocks for those commands.
9010 if (num_blocks == 0) {
9011 ctl_set_success(ctsio);
9012 ctl_done((union ctl_io *)ctsio);
9013 return (CTL_RETVAL_COMPLETE);
9016 /* Set FUA and/or DPO if caches are disabled. */
9018 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9020 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
9022 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9024 flags |= CTL_LLF_FUA;
9027 lbalen = (struct ctl_lba_len_flags *)
9028 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9030 lbalen->len = num_blocks;
9031 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
9033 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9034 ctsio->kern_rel_offset = 0;
9036 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
9038 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9044 ctl_cnw_cont(union ctl_io *io)
9046 struct ctl_scsiio *ctsio;
9047 struct ctl_lun *lun;
9048 struct ctl_lba_len_flags *lbalen;
9051 ctsio = &io->scsiio;
9052 ctsio->io_hdr.status = CTL_STATUS_NONE;
9053 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
9054 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9055 lbalen = (struct ctl_lba_len_flags *)
9056 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9057 lbalen->flags &= ~CTL_LLF_COMPARE;
9058 lbalen->flags |= CTL_LLF_WRITE;
9060 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
9061 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9066 ctl_cnw(struct ctl_scsiio *ctsio)
9068 struct ctl_lun *lun;
9069 struct ctl_lba_len_flags *lbalen;
9071 uint32_t num_blocks;
9074 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9076 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
9079 retval = CTL_RETVAL_COMPLETE;
9081 switch (ctsio->cdb[0]) {
9082 case COMPARE_AND_WRITE: {
9083 struct scsi_compare_and_write *cdb;
9085 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
9086 if (cdb->byte2 & SRW10_FUA)
9087 flags |= CTL_LLF_FUA;
9088 if (cdb->byte2 & SRW10_DPO)
9089 flags |= CTL_LLF_DPO;
9090 lba = scsi_8btou64(cdb->addr);
9091 num_blocks = cdb->length;
9096 * We got a command we don't support. This shouldn't
9097 * happen, commands should be filtered out above us.
9099 ctl_set_invalid_opcode(ctsio);
9100 ctl_done((union ctl_io *)ctsio);
9102 return (CTL_RETVAL_COMPLETE);
9103 break; /* NOTREACHED */
9107 * The first check is to make sure we're in bounds, the second
9108 * check is to catch wrap-around problems. If the lba + num blocks
9109 * is less than the lba, then we've wrapped around and the block
9110 * range is invalid anyway.
9112 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9113 || ((lba + num_blocks) < lba)) {
9114 ctl_set_lba_out_of_range(ctsio);
9115 ctl_done((union ctl_io *)ctsio);
9116 return (CTL_RETVAL_COMPLETE);
9120 * According to SBC-3, a transfer length of 0 is not an error.
9122 if (num_blocks == 0) {
9123 ctl_set_success(ctsio);
9124 ctl_done((union ctl_io *)ctsio);
9125 return (CTL_RETVAL_COMPLETE);
9128 /* Set FUA if write cache is disabled. */
9129 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9131 flags |= CTL_LLF_FUA;
9133 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
9134 ctsio->kern_rel_offset = 0;
9137 * Set the IO_CONT flag, so that if this I/O gets passed to
9138 * ctl_data_submit_done(), it'll get passed back to
9139 * ctl_ctl_cnw_cont() for further processing.
9141 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
9142 ctsio->io_cont = ctl_cnw_cont;
9144 lbalen = (struct ctl_lba_len_flags *)
9145 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9147 lbalen->len = num_blocks;
9148 lbalen->flags = CTL_LLF_COMPARE | flags;
9150 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
9151 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9156 ctl_verify(struct ctl_scsiio *ctsio)
9158 struct ctl_lun *lun;
9159 struct ctl_lba_len_flags *lbalen;
9161 uint32_t num_blocks;
9165 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9167 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
9170 flags = CTL_LLF_FUA;
9171 retval = CTL_RETVAL_COMPLETE;
9173 switch (ctsio->cdb[0]) {
9175 struct scsi_verify_10 *cdb;
9177 cdb = (struct scsi_verify_10 *)ctsio->cdb;
9178 if (cdb->byte2 & SVFY_BYTCHK)
9180 if (cdb->byte2 & SVFY_DPO)
9181 flags |= CTL_LLF_DPO;
9182 lba = scsi_4btoul(cdb->addr);
9183 num_blocks = scsi_2btoul(cdb->length);
9187 struct scsi_verify_12 *cdb;
9189 cdb = (struct scsi_verify_12 *)ctsio->cdb;
9190 if (cdb->byte2 & SVFY_BYTCHK)
9192 if (cdb->byte2 & SVFY_DPO)
9193 flags |= CTL_LLF_DPO;
9194 lba = scsi_4btoul(cdb->addr);
9195 num_blocks = scsi_4btoul(cdb->length);
9199 struct scsi_rw_16 *cdb;
9201 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9202 if (cdb->byte2 & SVFY_BYTCHK)
9204 if (cdb->byte2 & SVFY_DPO)
9205 flags |= CTL_LLF_DPO;
9206 lba = scsi_8btou64(cdb->addr);
9207 num_blocks = scsi_4btoul(cdb->length);
9212 * We got a command we don't support. This shouldn't
9213 * happen, commands should be filtered out above us.
9215 ctl_set_invalid_opcode(ctsio);
9216 ctl_done((union ctl_io *)ctsio);
9217 return (CTL_RETVAL_COMPLETE);
9221 * The first check is to make sure we're in bounds, the second
9222 * check is to catch wrap-around problems. If the lba + num blocks
9223 * is less than the lba, then we've wrapped around and the block
9224 * range is invalid anyway.
9226 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9227 || ((lba + num_blocks) < lba)) {
9228 ctl_set_lba_out_of_range(ctsio);
9229 ctl_done((union ctl_io *)ctsio);
9230 return (CTL_RETVAL_COMPLETE);
9234 * According to SBC-3, a transfer length of 0 is not an error.
9236 if (num_blocks == 0) {
9237 ctl_set_success(ctsio);
9238 ctl_done((union ctl_io *)ctsio);
9239 return (CTL_RETVAL_COMPLETE);
9242 lbalen = (struct ctl_lba_len_flags *)
9243 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9245 lbalen->len = num_blocks;
9247 lbalen->flags = CTL_LLF_COMPARE | flags;
9248 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9250 lbalen->flags = CTL_LLF_VERIFY | flags;
9251 ctsio->kern_total_len = 0;
9253 ctsio->kern_rel_offset = 0;
9255 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9256 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9261 ctl_report_luns(struct ctl_scsiio *ctsio)
9263 struct ctl_softc *softc = control_softc;
9264 struct scsi_report_luns *cdb;
9265 struct scsi_report_luns_data *lun_data;
9266 struct ctl_lun *lun, *request_lun;
9267 struct ctl_port *port;
9268 int num_luns, retval;
9269 uint32_t alloc_len, lun_datalen;
9271 uint32_t initidx, targ_lun_id, lun_id;
9273 retval = CTL_RETVAL_COMPLETE;
9274 cdb = (struct scsi_report_luns *)ctsio->cdb;
9275 port = ctl_io_port(&ctsio->io_hdr);
9277 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9279 mtx_lock(&softc->ctl_lock);
9281 for (targ_lun_id = 0; targ_lun_id < CTL_MAX_LUNS; targ_lun_id++) {
9282 if (ctl_lun_map_from_port(port, targ_lun_id) < CTL_MAX_LUNS)
9285 mtx_unlock(&softc->ctl_lock);
9287 switch (cdb->select_report) {
9288 case RPL_REPORT_DEFAULT:
9289 case RPL_REPORT_ALL:
9290 case RPL_REPORT_NONSUBSID:
9292 case RPL_REPORT_WELLKNOWN:
9293 case RPL_REPORT_ADMIN:
9294 case RPL_REPORT_CONGLOM:
9298 ctl_set_invalid_field(ctsio,
9304 ctl_done((union ctl_io *)ctsio);
9306 break; /* NOTREACHED */
9309 alloc_len = scsi_4btoul(cdb->length);
9311 * The initiator has to allocate at least 16 bytes for this request,
9312 * so he can at least get the header and the first LUN. Otherwise
9313 * we reject the request (per SPC-3 rev 14, section 6.21).
9315 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9316 sizeof(struct scsi_report_luns_lundata))) {
9317 ctl_set_invalid_field(ctsio,
9323 ctl_done((union ctl_io *)ctsio);
9327 request_lun = (struct ctl_lun *)
9328 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9330 lun_datalen = sizeof(*lun_data) +
9331 (num_luns * sizeof(struct scsi_report_luns_lundata));
9333 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9334 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9335 ctsio->kern_sg_entries = 0;
9337 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9339 mtx_lock(&softc->ctl_lock);
9340 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9341 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9342 if (lun_id >= CTL_MAX_LUNS)
9344 lun = softc->ctl_luns[lun_id];
9348 if (targ_lun_id <= 0xff) {
9350 * Peripheral addressing method, bus number 0.
9352 lun_data->luns[num_filled].lundata[0] =
9353 RPL_LUNDATA_ATYP_PERIPH;
9354 lun_data->luns[num_filled].lundata[1] = targ_lun_id;
9356 } else if (targ_lun_id <= 0x3fff) {
9358 * Flat addressing method.
9360 lun_data->luns[num_filled].lundata[0] =
9361 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8);
9362 lun_data->luns[num_filled].lundata[1] =
9363 (targ_lun_id & 0xff);
9365 } else if (targ_lun_id <= 0xffffff) {
9367 * Extended flat addressing method.
9369 lun_data->luns[num_filled].lundata[0] =
9370 RPL_LUNDATA_ATYP_EXTLUN | 0x12;
9371 scsi_ulto3b(targ_lun_id,
9372 &lun_data->luns[num_filled].lundata[1]);
9375 printf("ctl_report_luns: bogus LUN number %jd, "
9376 "skipping\n", (intmax_t)targ_lun_id);
9379 * According to SPC-3, rev 14 section 6.21:
9381 * "The execution of a REPORT LUNS command to any valid and
9382 * installed logical unit shall clear the REPORTED LUNS DATA
9383 * HAS CHANGED unit attention condition for all logical
9384 * units of that target with respect to the requesting
9385 * initiator. A valid and installed logical unit is one
9386 * having a PERIPHERAL QUALIFIER of 000b in the standard
9387 * INQUIRY data (see 6.4.2)."
9389 * If request_lun is NULL, the LUN this report luns command
9390 * was issued to is either disabled or doesn't exist. In that
9391 * case, we shouldn't clear any pending lun change unit
9394 if (request_lun != NULL) {
9395 mtx_lock(&lun->lun_lock);
9396 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE);
9397 mtx_unlock(&lun->lun_lock);
9400 mtx_unlock(&softc->ctl_lock);
9403 * It's quite possible that we've returned fewer LUNs than we allocated
9404 * space for. Trim it.
9406 lun_datalen = sizeof(*lun_data) +
9407 (num_filled * sizeof(struct scsi_report_luns_lundata));
9409 if (lun_datalen < alloc_len) {
9410 ctsio->residual = alloc_len - lun_datalen;
9411 ctsio->kern_data_len = lun_datalen;
9412 ctsio->kern_total_len = lun_datalen;
9414 ctsio->residual = 0;
9415 ctsio->kern_data_len = alloc_len;
9416 ctsio->kern_total_len = alloc_len;
9418 ctsio->kern_data_resid = 0;
9419 ctsio->kern_rel_offset = 0;
9420 ctsio->kern_sg_entries = 0;
9423 * We set this to the actual data length, regardless of how much
9424 * space we actually have to return results. If the user looks at
9425 * this value, he'll know whether or not he allocated enough space
9426 * and reissue the command if necessary. We don't support well
9427 * known logical units, so if the user asks for that, return none.
9429 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9432 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9435 ctl_set_success(ctsio);
9436 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9437 ctsio->be_move_done = ctl_config_move_done;
9438 ctl_datamove((union ctl_io *)ctsio);
9443 ctl_request_sense(struct ctl_scsiio *ctsio)
9445 struct scsi_request_sense *cdb;
9446 struct scsi_sense_data *sense_ptr;
9447 struct ctl_softc *ctl_softc;
9448 struct ctl_lun *lun;
9451 scsi_sense_data_type sense_format;
9452 ctl_ua_type ua_type;
9454 cdb = (struct scsi_request_sense *)ctsio->cdb;
9456 ctl_softc = control_softc;
9457 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9459 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9462 * Determine which sense format the user wants.
9464 if (cdb->byte2 & SRS_DESC)
9465 sense_format = SSD_TYPE_DESC;
9467 sense_format = SSD_TYPE_FIXED;
9469 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9470 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9471 ctsio->kern_sg_entries = 0;
9474 * struct scsi_sense_data, which is currently set to 256 bytes, is
9475 * larger than the largest allowed value for the length field in the
9476 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9478 ctsio->residual = 0;
9479 ctsio->kern_data_len = cdb->length;
9480 ctsio->kern_total_len = cdb->length;
9482 ctsio->kern_data_resid = 0;
9483 ctsio->kern_rel_offset = 0;
9484 ctsio->kern_sg_entries = 0;
9487 * If we don't have a LUN, we don't have any pending sense.
9493 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9495 * Check for pending sense, and then for pending unit attentions.
9496 * Pending sense gets returned first, then pending unit attentions.
9498 mtx_lock(&lun->lun_lock);
9500 if (ctl_is_set(lun->have_ca, initidx)) {
9501 scsi_sense_data_type stored_format;
9504 * Check to see which sense format was used for the stored
9507 stored_format = scsi_sense_type(&lun->pending_sense[initidx]);
9510 * If the user requested a different sense format than the
9511 * one we stored, then we need to convert it to the other
9512 * format. If we're going from descriptor to fixed format
9513 * sense data, we may lose things in translation, depending
9514 * on what options were used.
9516 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9517 * for some reason we'll just copy it out as-is.
9519 if ((stored_format == SSD_TYPE_FIXED)
9520 && (sense_format == SSD_TYPE_DESC))
9521 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9522 &lun->pending_sense[initidx],
9523 (struct scsi_sense_data_desc *)sense_ptr);
9524 else if ((stored_format == SSD_TYPE_DESC)
9525 && (sense_format == SSD_TYPE_FIXED))
9526 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9527 &lun->pending_sense[initidx],
9528 (struct scsi_sense_data_fixed *)sense_ptr);
9530 memcpy(sense_ptr, &lun->pending_sense[initidx],
9531 MIN(sizeof(*sense_ptr),
9532 sizeof(lun->pending_sense[initidx])));
9534 ctl_clear_mask(lun->have_ca, initidx);
9539 ua_type = ctl_build_ua(lun, initidx, sense_ptr, sense_format);
9540 if (ua_type != CTL_UA_NONE)
9542 if (ua_type == CTL_UA_LUN_CHANGE) {
9543 mtx_unlock(&lun->lun_lock);
9544 mtx_lock(&ctl_softc->ctl_lock);
9545 ctl_clr_ua_allluns(ctl_softc, initidx, ua_type);
9546 mtx_unlock(&ctl_softc->ctl_lock);
9547 mtx_lock(&lun->lun_lock);
9551 mtx_unlock(&lun->lun_lock);
9554 * We already have a pending error, return it.
9556 if (have_error != 0) {
9558 * We report the SCSI status as OK, since the status of the
9559 * request sense command itself is OK.
9560 * We report 0 for the sense length, because we aren't doing
9561 * autosense in this case. We're reporting sense as
9564 ctl_set_success(ctsio);
9565 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9566 ctsio->be_move_done = ctl_config_move_done;
9567 ctl_datamove((union ctl_io *)ctsio);
9568 return (CTL_RETVAL_COMPLETE);
9574 * No sense information to report, so we report that everything is
9577 ctl_set_sense_data(sense_ptr,
9580 /*current_error*/ 1,
9581 /*sense_key*/ SSD_KEY_NO_SENSE,
9587 * We report 0 for the sense length, because we aren't doing
9588 * autosense in this case. We're reporting sense as parameter data.
9590 ctl_set_success(ctsio);
9591 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9592 ctsio->be_move_done = ctl_config_move_done;
9593 ctl_datamove((union ctl_io *)ctsio);
9594 return (CTL_RETVAL_COMPLETE);
9598 ctl_tur(struct ctl_scsiio *ctsio)
9601 CTL_DEBUG_PRINT(("ctl_tur\n"));
9603 ctl_set_success(ctsio);
9604 ctl_done((union ctl_io *)ctsio);
9606 return (CTL_RETVAL_COMPLETE);
9610 * SCSI VPD page 0x00, the Supported VPD Pages page.
9613 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9615 struct scsi_vpd_supported_pages *pages;
9617 struct ctl_lun *lun;
9620 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9622 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9623 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9624 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9625 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9626 ctsio->kern_sg_entries = 0;
9628 if (sup_page_size < alloc_len) {
9629 ctsio->residual = alloc_len - sup_page_size;
9630 ctsio->kern_data_len = sup_page_size;
9631 ctsio->kern_total_len = sup_page_size;
9633 ctsio->residual = 0;
9634 ctsio->kern_data_len = alloc_len;
9635 ctsio->kern_total_len = alloc_len;
9637 ctsio->kern_data_resid = 0;
9638 ctsio->kern_rel_offset = 0;
9639 ctsio->kern_sg_entries = 0;
9642 * The control device is always connected. The disk device, on the
9643 * other hand, may not be online all the time. Need to change this
9644 * to figure out whether the disk device is actually online or not.
9647 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9648 lun->be_lun->lun_type;
9650 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9653 /* Supported VPD pages */
9654 pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9656 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9657 /* Device Identification */
9658 pages->page_list[p++] = SVPD_DEVICE_ID;
9659 /* Extended INQUIRY Data */
9660 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9661 /* Mode Page Policy */
9662 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9664 pages->page_list[p++] = SVPD_SCSI_PORTS;
9665 /* Third-party Copy */
9666 pages->page_list[p++] = SVPD_SCSI_TPC;
9667 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9669 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9670 /* Block Device Characteristics */
9671 pages->page_list[p++] = SVPD_BDC;
9672 /* Logical Block Provisioning */
9673 pages->page_list[p++] = SVPD_LBP;
9677 ctl_set_success(ctsio);
9678 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9679 ctsio->be_move_done = ctl_config_move_done;
9680 ctl_datamove((union ctl_io *)ctsio);
9681 return (CTL_RETVAL_COMPLETE);
9685 * SCSI VPD page 0x80, the Unit Serial Number page.
9688 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9690 struct scsi_vpd_unit_serial_number *sn_ptr;
9691 struct ctl_lun *lun;
9694 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9696 data_len = 4 + CTL_SN_LEN;
9697 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9698 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9699 if (data_len < alloc_len) {
9700 ctsio->residual = alloc_len - data_len;
9701 ctsio->kern_data_len = data_len;
9702 ctsio->kern_total_len = data_len;
9704 ctsio->residual = 0;
9705 ctsio->kern_data_len = alloc_len;
9706 ctsio->kern_total_len = alloc_len;
9708 ctsio->kern_data_resid = 0;
9709 ctsio->kern_rel_offset = 0;
9710 ctsio->kern_sg_entries = 0;
9713 * The control device is always connected. The disk device, on the
9714 * other hand, may not be online all the time. Need to change this
9715 * to figure out whether the disk device is actually online or not.
9718 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9719 lun->be_lun->lun_type;
9721 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9723 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9724 sn_ptr->length = CTL_SN_LEN;
9726 * If we don't have a LUN, we just leave the serial number as
9730 strncpy((char *)sn_ptr->serial_num,
9731 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9733 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9735 ctl_set_success(ctsio);
9736 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9737 ctsio->be_move_done = ctl_config_move_done;
9738 ctl_datamove((union ctl_io *)ctsio);
9739 return (CTL_RETVAL_COMPLETE);
9744 * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9747 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9749 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9750 struct ctl_lun *lun;
9753 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9755 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9756 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9757 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9758 ctsio->kern_sg_entries = 0;
9760 if (data_len < alloc_len) {
9761 ctsio->residual = alloc_len - data_len;
9762 ctsio->kern_data_len = data_len;
9763 ctsio->kern_total_len = data_len;
9765 ctsio->residual = 0;
9766 ctsio->kern_data_len = alloc_len;
9767 ctsio->kern_total_len = alloc_len;
9769 ctsio->kern_data_resid = 0;
9770 ctsio->kern_rel_offset = 0;
9771 ctsio->kern_sg_entries = 0;
9774 * The control device is always connected. The disk device, on the
9775 * other hand, may not be online all the time.
9778 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9779 lun->be_lun->lun_type;
9781 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9782 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9783 scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9785 * We support head of queue, ordered and simple tags.
9787 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9789 * Volatile cache supported.
9791 eid_ptr->flags3 = SVPD_EID_V_SUP;
9794 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9795 * attention for a particular IT nexus on all LUNs once we report
9796 * it to that nexus once. This bit is required as of SPC-4.
9798 eid_ptr->flags4 = SVPD_EID_LUICLT;
9801 * XXX KDM in order to correctly answer this, we would need
9802 * information from the SIM to determine how much sense data it
9803 * can send. So this would really be a path inquiry field, most
9804 * likely. This can be set to a maximum of 252 according to SPC-4,
9805 * but the hardware may or may not be able to support that much.
9806 * 0 just means that the maximum sense data length is not reported.
9808 eid_ptr->max_sense_length = 0;
9810 ctl_set_success(ctsio);
9811 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9812 ctsio->be_move_done = ctl_config_move_done;
9813 ctl_datamove((union ctl_io *)ctsio);
9814 return (CTL_RETVAL_COMPLETE);
9818 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9820 struct scsi_vpd_mode_page_policy *mpp_ptr;
9821 struct ctl_lun *lun;
9824 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9826 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9827 sizeof(struct scsi_vpd_mode_page_policy_descr);
9829 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9830 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9831 ctsio->kern_sg_entries = 0;
9833 if (data_len < alloc_len) {
9834 ctsio->residual = alloc_len - data_len;
9835 ctsio->kern_data_len = data_len;
9836 ctsio->kern_total_len = data_len;
9838 ctsio->residual = 0;
9839 ctsio->kern_data_len = alloc_len;
9840 ctsio->kern_total_len = alloc_len;
9842 ctsio->kern_data_resid = 0;
9843 ctsio->kern_rel_offset = 0;
9844 ctsio->kern_sg_entries = 0;
9847 * The control device is always connected. The disk device, on the
9848 * other hand, may not be online all the time.
9851 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9852 lun->be_lun->lun_type;
9854 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9855 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9856 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9857 mpp_ptr->descr[0].page_code = 0x3f;
9858 mpp_ptr->descr[0].subpage_code = 0xff;
9859 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9861 ctl_set_success(ctsio);
9862 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9863 ctsio->be_move_done = ctl_config_move_done;
9864 ctl_datamove((union ctl_io *)ctsio);
9865 return (CTL_RETVAL_COMPLETE);
9869 * SCSI VPD page 0x83, the Device Identification page.
9872 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9874 struct scsi_vpd_device_id *devid_ptr;
9875 struct scsi_vpd_id_descriptor *desc;
9876 struct ctl_softc *softc;
9877 struct ctl_lun *lun;
9878 struct ctl_port *port;
9882 softc = control_softc;
9884 port = ctl_io_port(&ctsio->io_hdr);
9885 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9887 data_len = sizeof(struct scsi_vpd_device_id) +
9888 sizeof(struct scsi_vpd_id_descriptor) +
9889 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9890 sizeof(struct scsi_vpd_id_descriptor) +
9891 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9892 if (lun && lun->lun_devid)
9893 data_len += lun->lun_devid->len;
9894 if (port && port->port_devid)
9895 data_len += port->port_devid->len;
9896 if (port && port->target_devid)
9897 data_len += port->target_devid->len;
9899 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9900 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9901 ctsio->kern_sg_entries = 0;
9903 if (data_len < alloc_len) {
9904 ctsio->residual = alloc_len - data_len;
9905 ctsio->kern_data_len = data_len;
9906 ctsio->kern_total_len = data_len;
9908 ctsio->residual = 0;
9909 ctsio->kern_data_len = alloc_len;
9910 ctsio->kern_total_len = alloc_len;
9912 ctsio->kern_data_resid = 0;
9913 ctsio->kern_rel_offset = 0;
9914 ctsio->kern_sg_entries = 0;
9917 * The control device is always connected. The disk device, on the
9918 * other hand, may not be online all the time.
9921 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9922 lun->be_lun->lun_type;
9924 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9925 devid_ptr->page_code = SVPD_DEVICE_ID;
9926 scsi_ulto2b(data_len - 4, devid_ptr->length);
9928 if (port && port->port_type == CTL_PORT_FC)
9929 proto = SCSI_PROTO_FC << 4;
9930 else if (port && port->port_type == CTL_PORT_ISCSI)
9931 proto = SCSI_PROTO_ISCSI << 4;
9933 proto = SCSI_PROTO_SPI << 4;
9934 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9937 * We're using a LUN association here. i.e., this device ID is a
9938 * per-LUN identifier.
9940 if (lun && lun->lun_devid) {
9941 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9942 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9943 lun->lun_devid->len);
9947 * This is for the WWPN which is a port association.
9949 if (port && port->port_devid) {
9950 memcpy(desc, port->port_devid->data, port->port_devid->len);
9951 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9952 port->port_devid->len);
9956 * This is for the Relative Target Port(type 4h) identifier
9958 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9959 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9960 SVPD_ID_TYPE_RELTARG;
9962 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9963 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9964 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9967 * This is for the Target Port Group(type 5h) identifier
9969 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9970 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9971 SVPD_ID_TYPE_TPORTGRP;
9973 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / softc->port_cnt + 1,
9974 &desc->identifier[2]);
9975 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9976 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9979 * This is for the Target identifier
9981 if (port && port->target_devid) {
9982 memcpy(desc, port->target_devid->data, port->target_devid->len);
9985 ctl_set_success(ctsio);
9986 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9987 ctsio->be_move_done = ctl_config_move_done;
9988 ctl_datamove((union ctl_io *)ctsio);
9989 return (CTL_RETVAL_COMPLETE);
9993 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9995 struct ctl_softc *softc = control_softc;
9996 struct scsi_vpd_scsi_ports *sp;
9997 struct scsi_vpd_port_designation *pd;
9998 struct scsi_vpd_port_designation_cont *pdc;
9999 struct ctl_lun *lun;
10000 struct ctl_port *port;
10001 int data_len, num_target_ports, iid_len, id_len;
10003 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10005 num_target_ports = 0;
10008 mtx_lock(&softc->ctl_lock);
10009 STAILQ_FOREACH(port, &softc->port_list, links) {
10010 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
10013 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
10015 num_target_ports++;
10016 if (port->init_devid)
10017 iid_len += port->init_devid->len;
10018 if (port->port_devid)
10019 id_len += port->port_devid->len;
10021 mtx_unlock(&softc->ctl_lock);
10023 data_len = sizeof(struct scsi_vpd_scsi_ports) +
10024 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
10025 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
10026 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10027 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
10028 ctsio->kern_sg_entries = 0;
10030 if (data_len < alloc_len) {
10031 ctsio->residual = alloc_len - data_len;
10032 ctsio->kern_data_len = data_len;
10033 ctsio->kern_total_len = data_len;
10035 ctsio->residual = 0;
10036 ctsio->kern_data_len = alloc_len;
10037 ctsio->kern_total_len = alloc_len;
10039 ctsio->kern_data_resid = 0;
10040 ctsio->kern_rel_offset = 0;
10041 ctsio->kern_sg_entries = 0;
10044 * The control device is always connected. The disk device, on the
10045 * other hand, may not be online all the time. Need to change this
10046 * to figure out whether the disk device is actually online or not.
10049 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
10050 lun->be_lun->lun_type;
10052 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10054 sp->page_code = SVPD_SCSI_PORTS;
10055 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
10057 pd = &sp->design[0];
10059 mtx_lock(&softc->ctl_lock);
10060 STAILQ_FOREACH(port, &softc->port_list, links) {
10061 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
10064 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
10066 scsi_ulto2b(port->targ_port, pd->relative_port_id);
10067 if (port->init_devid) {
10068 iid_len = port->init_devid->len;
10069 memcpy(pd->initiator_transportid,
10070 port->init_devid->data, port->init_devid->len);
10073 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
10074 pdc = (struct scsi_vpd_port_designation_cont *)
10075 (&pd->initiator_transportid[iid_len]);
10076 if (port->port_devid) {
10077 id_len = port->port_devid->len;
10078 memcpy(pdc->target_port_descriptors,
10079 port->port_devid->data, port->port_devid->len);
10082 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
10083 pd = (struct scsi_vpd_port_designation *)
10084 ((uint8_t *)pdc->target_port_descriptors + id_len);
10086 mtx_unlock(&softc->ctl_lock);
10088 ctl_set_success(ctsio);
10089 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10090 ctsio->be_move_done = ctl_config_move_done;
10091 ctl_datamove((union ctl_io *)ctsio);
10092 return (CTL_RETVAL_COMPLETE);
10096 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
10098 struct scsi_vpd_block_limits *bl_ptr;
10099 struct ctl_lun *lun;
10102 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10104 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
10105 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
10106 ctsio->kern_sg_entries = 0;
10108 if (sizeof(*bl_ptr) < alloc_len) {
10109 ctsio->residual = alloc_len - sizeof(*bl_ptr);
10110 ctsio->kern_data_len = sizeof(*bl_ptr);
10111 ctsio->kern_total_len = sizeof(*bl_ptr);
10113 ctsio->residual = 0;
10114 ctsio->kern_data_len = alloc_len;
10115 ctsio->kern_total_len = alloc_len;
10117 ctsio->kern_data_resid = 0;
10118 ctsio->kern_rel_offset = 0;
10119 ctsio->kern_sg_entries = 0;
10122 * The control device is always connected. The disk device, on the
10123 * other hand, may not be online all the time. Need to change this
10124 * to figure out whether the disk device is actually online or not.
10127 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10128 lun->be_lun->lun_type;
10130 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10132 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
10133 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
10134 bl_ptr->max_cmp_write_len = 0xff;
10135 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
10137 bs = lun->be_lun->blocksize;
10138 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
10139 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10140 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
10141 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
10142 if (lun->be_lun->ublockexp != 0) {
10143 scsi_ulto4b((1 << lun->be_lun->ublockexp),
10144 bl_ptr->opt_unmap_grain);
10145 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
10146 bl_ptr->unmap_grain_align);
10149 scsi_ulto4b(lun->be_lun->atomicblock,
10150 bl_ptr->max_atomic_transfer_length);
10151 scsi_ulto4b(0, bl_ptr->atomic_alignment);
10152 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
10153 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary);
10154 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size);
10156 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
10158 ctl_set_success(ctsio);
10159 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10160 ctsio->be_move_done = ctl_config_move_done;
10161 ctl_datamove((union ctl_io *)ctsio);
10162 return (CTL_RETVAL_COMPLETE);
10166 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
10168 struct scsi_vpd_block_device_characteristics *bdc_ptr;
10169 struct ctl_lun *lun;
10173 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10175 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
10176 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
10177 ctsio->kern_sg_entries = 0;
10179 if (sizeof(*bdc_ptr) < alloc_len) {
10180 ctsio->residual = alloc_len - sizeof(*bdc_ptr);
10181 ctsio->kern_data_len = sizeof(*bdc_ptr);
10182 ctsio->kern_total_len = sizeof(*bdc_ptr);
10184 ctsio->residual = 0;
10185 ctsio->kern_data_len = alloc_len;
10186 ctsio->kern_total_len = alloc_len;
10188 ctsio->kern_data_resid = 0;
10189 ctsio->kern_rel_offset = 0;
10190 ctsio->kern_sg_entries = 0;
10193 * The control device is always connected. The disk device, on the
10194 * other hand, may not be online all the time. Need to change this
10195 * to figure out whether the disk device is actually online or not.
10198 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10199 lun->be_lun->lun_type;
10201 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10202 bdc_ptr->page_code = SVPD_BDC;
10203 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
10205 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL)
10206 i = strtol(value, NULL, 0);
10208 i = CTL_DEFAULT_ROTATION_RATE;
10209 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
10211 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL)
10212 i = strtol(value, NULL, 0);
10215 bdc_ptr->wab_wac_ff = (i & 0x0f);
10216 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
10218 ctl_set_success(ctsio);
10219 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10220 ctsio->be_move_done = ctl_config_move_done;
10221 ctl_datamove((union ctl_io *)ctsio);
10222 return (CTL_RETVAL_COMPLETE);
10226 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
10228 struct scsi_vpd_logical_block_prov *lbp_ptr;
10229 struct ctl_lun *lun;
10231 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10233 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
10234 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
10235 ctsio->kern_sg_entries = 0;
10237 if (sizeof(*lbp_ptr) < alloc_len) {
10238 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
10239 ctsio->kern_data_len = sizeof(*lbp_ptr);
10240 ctsio->kern_total_len = sizeof(*lbp_ptr);
10242 ctsio->residual = 0;
10243 ctsio->kern_data_len = alloc_len;
10244 ctsio->kern_total_len = alloc_len;
10246 ctsio->kern_data_resid = 0;
10247 ctsio->kern_rel_offset = 0;
10248 ctsio->kern_sg_entries = 0;
10251 * The control device is always connected. The disk device, on the
10252 * other hand, may not be online all the time. Need to change this
10253 * to figure out whether the disk device is actually online or not.
10256 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10257 lun->be_lun->lun_type;
10259 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10261 lbp_ptr->page_code = SVPD_LBP;
10262 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
10263 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
10264 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10265 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
10266 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
10267 lbp_ptr->prov_type = SVPD_LBP_THIN;
10270 ctl_set_success(ctsio);
10271 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10272 ctsio->be_move_done = ctl_config_move_done;
10273 ctl_datamove((union ctl_io *)ctsio);
10274 return (CTL_RETVAL_COMPLETE);
10278 * INQUIRY with the EVPD bit set.
10281 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
10283 struct ctl_lun *lun;
10284 struct scsi_inquiry *cdb;
10285 int alloc_len, retval;
10287 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10288 cdb = (struct scsi_inquiry *)ctsio->cdb;
10289 alloc_len = scsi_2btoul(cdb->length);
10291 switch (cdb->page_code) {
10292 case SVPD_SUPPORTED_PAGES:
10293 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
10295 case SVPD_UNIT_SERIAL_NUMBER:
10296 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
10298 case SVPD_DEVICE_ID:
10299 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
10301 case SVPD_EXTENDED_INQUIRY_DATA:
10302 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
10304 case SVPD_MODE_PAGE_POLICY:
10305 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
10307 case SVPD_SCSI_PORTS:
10308 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10310 case SVPD_SCSI_TPC:
10311 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10313 case SVPD_BLOCK_LIMITS:
10314 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10316 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10319 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10321 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10324 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10326 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10330 ctl_set_invalid_field(ctsio,
10336 ctl_done((union ctl_io *)ctsio);
10337 retval = CTL_RETVAL_COMPLETE;
10345 * Standard INQUIRY data.
10348 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10350 struct scsi_inquiry_data *inq_ptr;
10351 struct scsi_inquiry *cdb;
10352 struct ctl_softc *softc;
10353 struct ctl_port *port;
10354 struct ctl_lun *lun;
10356 uint32_t alloc_len, data_len;
10357 ctl_port_type port_type;
10359 softc = control_softc;
10362 * Figure out whether we're talking to a Fibre Channel port or not.
10363 * We treat the ioctl front end, and any SCSI adapters, as packetized
10366 port = ctl_io_port(&ctsio->io_hdr);
10368 port_type = port->port_type;
10370 port_type = CTL_PORT_SCSI;
10371 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10372 port_type = CTL_PORT_SCSI;
10374 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10375 cdb = (struct scsi_inquiry *)ctsio->cdb;
10376 alloc_len = scsi_2btoul(cdb->length);
10379 * We malloc the full inquiry data size here and fill it
10380 * in. If the user only asks for less, we'll give him
10383 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10384 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10385 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10386 ctsio->kern_sg_entries = 0;
10387 ctsio->kern_data_resid = 0;
10388 ctsio->kern_rel_offset = 0;
10390 if (data_len < alloc_len) {
10391 ctsio->residual = alloc_len - data_len;
10392 ctsio->kern_data_len = data_len;
10393 ctsio->kern_total_len = data_len;
10395 ctsio->residual = 0;
10396 ctsio->kern_data_len = alloc_len;
10397 ctsio->kern_total_len = alloc_len;
10401 if ((lun->flags & CTL_LUN_PRIMARY_SC) ||
10402 softc->ha_link >= CTL_HA_LINK_UNKNOWN) {
10403 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10404 lun->be_lun->lun_type;
10406 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) |
10407 lun->be_lun->lun_type;
10410 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10412 /* RMB in byte 2 is 0 */
10413 inq_ptr->version = SCSI_REV_SPC4;
10416 * According to SAM-3, even if a device only supports a single
10417 * level of LUN addressing, it should still set the HISUP bit:
10419 * 4.9.1 Logical unit numbers overview
10421 * All logical unit number formats described in this standard are
10422 * hierarchical in structure even when only a single level in that
10423 * hierarchy is used. The HISUP bit shall be set to one in the
10424 * standard INQUIRY data (see SPC-2) when any logical unit number
10425 * format described in this standard is used. Non-hierarchical
10426 * formats are outside the scope of this standard.
10428 * Therefore we set the HiSup bit here.
10430 * The reponse format is 2, per SPC-3.
10432 inq_ptr->response_format = SID_HiSup | 2;
10434 inq_ptr->additional_length = data_len -
10435 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10436 CTL_DEBUG_PRINT(("additional_length = %d\n",
10437 inq_ptr->additional_length));
10439 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10440 /* 16 bit addressing */
10441 if (port_type == CTL_PORT_SCSI)
10442 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10443 /* XXX set the SID_MultiP bit here if we're actually going to
10444 respond on multiple ports */
10445 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10447 /* 16 bit data bus, synchronous transfers */
10448 if (port_type == CTL_PORT_SCSI)
10449 inq_ptr->flags = SID_WBus16 | SID_Sync;
10451 * XXX KDM do we want to support tagged queueing on the control
10455 || (lun->be_lun->lun_type != T_PROCESSOR))
10456 inq_ptr->flags |= SID_CmdQue;
10458 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10459 * We have 8 bytes for the vendor name, and 16 bytes for the device
10460 * name and 4 bytes for the revision.
10462 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10463 "vendor")) == NULL) {
10464 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10466 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10467 strncpy(inq_ptr->vendor, val,
10468 min(sizeof(inq_ptr->vendor), strlen(val)));
10471 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10472 sizeof(inq_ptr->product));
10473 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10474 switch (lun->be_lun->lun_type) {
10476 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10477 sizeof(inq_ptr->product));
10480 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10481 sizeof(inq_ptr->product));
10484 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10485 sizeof(inq_ptr->product));
10489 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10490 strncpy(inq_ptr->product, val,
10491 min(sizeof(inq_ptr->product), strlen(val)));
10495 * XXX make this a macro somewhere so it automatically gets
10496 * incremented when we make changes.
10498 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10499 "revision")) == NULL) {
10500 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10502 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10503 strncpy(inq_ptr->revision, val,
10504 min(sizeof(inq_ptr->revision), strlen(val)));
10508 * For parallel SCSI, we support double transition and single
10509 * transition clocking. We also support QAS (Quick Arbitration
10510 * and Selection) and Information Unit transfers on both the
10511 * control and array devices.
10513 if (port_type == CTL_PORT_SCSI)
10514 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10517 /* SAM-5 (no version claimed) */
10518 scsi_ulto2b(0x00A0, inq_ptr->version1);
10519 /* SPC-4 (no version claimed) */
10520 scsi_ulto2b(0x0460, inq_ptr->version2);
10521 if (port_type == CTL_PORT_FC) {
10522 /* FCP-2 ANSI INCITS.350:2003 */
10523 scsi_ulto2b(0x0917, inq_ptr->version3);
10524 } else if (port_type == CTL_PORT_SCSI) {
10525 /* SPI-4 ANSI INCITS.362:200x */
10526 scsi_ulto2b(0x0B56, inq_ptr->version3);
10527 } else if (port_type == CTL_PORT_ISCSI) {
10528 /* iSCSI (no version claimed) */
10529 scsi_ulto2b(0x0960, inq_ptr->version3);
10530 } else if (port_type == CTL_PORT_SAS) {
10531 /* SAS (no version claimed) */
10532 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10536 /* SBC-4 (no version claimed) */
10537 scsi_ulto2b(0x0600, inq_ptr->version4);
10539 switch (lun->be_lun->lun_type) {
10541 /* SBC-4 (no version claimed) */
10542 scsi_ulto2b(0x0600, inq_ptr->version4);
10550 ctl_set_success(ctsio);
10551 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10552 ctsio->be_move_done = ctl_config_move_done;
10553 ctl_datamove((union ctl_io *)ctsio);
10554 return (CTL_RETVAL_COMPLETE);
10558 ctl_inquiry(struct ctl_scsiio *ctsio)
10560 struct scsi_inquiry *cdb;
10563 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10565 cdb = (struct scsi_inquiry *)ctsio->cdb;
10566 if (cdb->byte2 & SI_EVPD)
10567 retval = ctl_inquiry_evpd(ctsio);
10568 else if (cdb->page_code == 0)
10569 retval = ctl_inquiry_std(ctsio);
10571 ctl_set_invalid_field(ctsio,
10577 ctl_done((union ctl_io *)ctsio);
10578 return (CTL_RETVAL_COMPLETE);
10585 * For known CDB types, parse the LBA and length.
10588 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10590 if (io->io_hdr.io_type != CTL_IO_SCSI)
10593 switch (io->scsiio.cdb[0]) {
10594 case COMPARE_AND_WRITE: {
10595 struct scsi_compare_and_write *cdb;
10597 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10599 *lba = scsi_8btou64(cdb->addr);
10600 *len = cdb->length;
10605 struct scsi_rw_6 *cdb;
10607 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10609 *lba = scsi_3btoul(cdb->addr);
10610 /* only 5 bits are valid in the most significant address byte */
10612 *len = cdb->length;
10617 struct scsi_rw_10 *cdb;
10619 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10621 *lba = scsi_4btoul(cdb->addr);
10622 *len = scsi_2btoul(cdb->length);
10625 case WRITE_VERIFY_10: {
10626 struct scsi_write_verify_10 *cdb;
10628 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10630 *lba = scsi_4btoul(cdb->addr);
10631 *len = scsi_2btoul(cdb->length);
10636 struct scsi_rw_12 *cdb;
10638 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10640 *lba = scsi_4btoul(cdb->addr);
10641 *len = scsi_4btoul(cdb->length);
10644 case WRITE_VERIFY_12: {
10645 struct scsi_write_verify_12 *cdb;
10647 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10649 *lba = scsi_4btoul(cdb->addr);
10650 *len = scsi_4btoul(cdb->length);
10655 struct scsi_rw_16 *cdb;
10657 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10659 *lba = scsi_8btou64(cdb->addr);
10660 *len = scsi_4btoul(cdb->length);
10663 case WRITE_ATOMIC_16: {
10664 struct scsi_write_atomic_16 *cdb;
10666 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb;
10668 *lba = scsi_8btou64(cdb->addr);
10669 *len = scsi_2btoul(cdb->length);
10672 case WRITE_VERIFY_16: {
10673 struct scsi_write_verify_16 *cdb;
10675 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10677 *lba = scsi_8btou64(cdb->addr);
10678 *len = scsi_4btoul(cdb->length);
10681 case WRITE_SAME_10: {
10682 struct scsi_write_same_10 *cdb;
10684 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10686 *lba = scsi_4btoul(cdb->addr);
10687 *len = scsi_2btoul(cdb->length);
10690 case WRITE_SAME_16: {
10691 struct scsi_write_same_16 *cdb;
10693 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10695 *lba = scsi_8btou64(cdb->addr);
10696 *len = scsi_4btoul(cdb->length);
10700 struct scsi_verify_10 *cdb;
10702 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10704 *lba = scsi_4btoul(cdb->addr);
10705 *len = scsi_2btoul(cdb->length);
10709 struct scsi_verify_12 *cdb;
10711 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10713 *lba = scsi_4btoul(cdb->addr);
10714 *len = scsi_4btoul(cdb->length);
10718 struct scsi_verify_16 *cdb;
10720 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10722 *lba = scsi_8btou64(cdb->addr);
10723 *len = scsi_4btoul(cdb->length);
10731 case SERVICE_ACTION_IN: { /* GET LBA STATUS */
10732 struct scsi_get_lba_status *cdb;
10734 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10735 *lba = scsi_8btou64(cdb->addr);
10741 break; /* NOTREACHED */
10748 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10751 uint64_t endlba1, endlba2;
10753 endlba1 = lba1 + len1 - (seq ? 0 : 1);
10754 endlba2 = lba2 + len2 - 1;
10756 if ((endlba1 < lba2) || (endlba2 < lba1))
10757 return (CTL_ACTION_PASS);
10759 return (CTL_ACTION_BLOCK);
10763 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10765 struct ctl_ptr_len_flags *ptrlen;
10766 struct scsi_unmap_desc *buf, *end, *range;
10770 /* If not UNMAP -- go other way. */
10771 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10772 io->scsiio.cdb[0] != UNMAP)
10773 return (CTL_ACTION_ERROR);
10775 /* If UNMAP without data -- block and wait for data. */
10776 ptrlen = (struct ctl_ptr_len_flags *)
10777 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10778 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10779 ptrlen->ptr == NULL)
10780 return (CTL_ACTION_BLOCK);
10782 /* UNMAP with data -- check for collision. */
10783 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10784 end = buf + ptrlen->len / sizeof(*buf);
10785 for (range = buf; range < end; range++) {
10786 lba = scsi_8btou64(range->lba);
10787 len = scsi_4btoul(range->length);
10788 if ((lba < lba2 + len2) && (lba + len > lba2))
10789 return (CTL_ACTION_BLOCK);
10791 return (CTL_ACTION_PASS);
10795 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10797 uint64_t lba1, lba2;
10798 uint64_t len1, len2;
10801 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10802 return (CTL_ACTION_ERROR);
10804 retval = ctl_extent_check_unmap(io1, lba2, len2);
10805 if (retval != CTL_ACTION_ERROR)
10808 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10809 return (CTL_ACTION_ERROR);
10811 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10813 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
10817 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2)
10819 uint64_t lba1, lba2;
10820 uint64_t len1, len2;
10822 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10823 return (CTL_ACTION_PASS);
10824 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10825 return (CTL_ACTION_ERROR);
10826 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10827 return (CTL_ACTION_ERROR);
10829 if (lba1 + len1 == lba2)
10830 return (CTL_ACTION_BLOCK);
10831 return (CTL_ACTION_PASS);
10835 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10836 union ctl_io *ooa_io)
10838 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10839 ctl_serialize_action *serialize_row;
10842 * The initiator attempted multiple untagged commands at the same
10843 * time. Can't do that.
10845 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10846 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10847 && ((pending_io->io_hdr.nexus.targ_port ==
10848 ooa_io->io_hdr.nexus.targ_port)
10849 && (pending_io->io_hdr.nexus.initid ==
10850 ooa_io->io_hdr.nexus.initid))
10851 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10852 CTL_FLAG_STATUS_SENT)) == 0))
10853 return (CTL_ACTION_OVERLAP);
10856 * The initiator attempted to send multiple tagged commands with
10857 * the same ID. (It's fine if different initiators have the same
10860 * Even if all of those conditions are true, we don't kill the I/O
10861 * if the command ahead of us has been aborted. We won't end up
10862 * sending it to the FETD, and it's perfectly legal to resend a
10863 * command with the same tag number as long as the previous
10864 * instance of this tag number has been aborted somehow.
10866 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10867 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10868 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10869 && ((pending_io->io_hdr.nexus.targ_port ==
10870 ooa_io->io_hdr.nexus.targ_port)
10871 && (pending_io->io_hdr.nexus.initid ==
10872 ooa_io->io_hdr.nexus.initid))
10873 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10874 CTL_FLAG_STATUS_SENT)) == 0))
10875 return (CTL_ACTION_OVERLAP_TAG);
10878 * If we get a head of queue tag, SAM-3 says that we should
10879 * immediately execute it.
10881 * What happens if this command would normally block for some other
10882 * reason? e.g. a request sense with a head of queue tag
10883 * immediately after a write. Normally that would block, but this
10884 * will result in its getting executed immediately...
10886 * We currently return "pass" instead of "skip", so we'll end up
10887 * going through the rest of the queue to check for overlapped tags.
10889 * XXX KDM check for other types of blockage first??
10891 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10892 return (CTL_ACTION_PASS);
10895 * Ordered tags have to block until all items ahead of them
10896 * have completed. If we get called with an ordered tag, we always
10897 * block, if something else is ahead of us in the queue.
10899 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10900 return (CTL_ACTION_BLOCK);
10903 * Simple tags get blocked until all head of queue and ordered tags
10904 * ahead of them have completed. I'm lumping untagged commands in
10905 * with simple tags here. XXX KDM is that the right thing to do?
10907 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10908 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10909 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10910 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10911 return (CTL_ACTION_BLOCK);
10913 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
10914 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
10916 serialize_row = ctl_serialize_table[ooa_entry->seridx];
10918 switch (serialize_row[pending_entry->seridx]) {
10919 case CTL_SER_BLOCK:
10920 return (CTL_ACTION_BLOCK);
10921 case CTL_SER_EXTENT:
10922 return (ctl_extent_check(ooa_io, pending_io,
10923 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10924 case CTL_SER_EXTENTOPT:
10925 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
10926 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
10927 return (ctl_extent_check(ooa_io, pending_io,
10929 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10930 return (CTL_ACTION_PASS);
10931 case CTL_SER_EXTENTSEQ:
10932 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF)
10933 return (ctl_extent_check_seq(ooa_io, pending_io));
10934 return (CTL_ACTION_PASS);
10936 return (CTL_ACTION_PASS);
10937 case CTL_SER_BLOCKOPT:
10938 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
10939 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
10940 return (CTL_ACTION_BLOCK);
10941 return (CTL_ACTION_PASS);
10943 return (CTL_ACTION_SKIP);
10945 panic("invalid serialization value %d",
10946 serialize_row[pending_entry->seridx]);
10949 return (CTL_ACTION_ERROR);
10953 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
10955 * - pending_io is generally either incoming, or on the blocked queue
10956 * - starting I/O is the I/O we want to start the check with.
10959 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
10960 union ctl_io *starting_io)
10962 union ctl_io *ooa_io;
10965 mtx_assert(&lun->lun_lock, MA_OWNED);
10968 * Run back along the OOA queue, starting with the current
10969 * blocked I/O and going through every I/O before it on the
10970 * queue. If starting_io is NULL, we'll just end up returning
10973 for (ooa_io = starting_io; ooa_io != NULL;
10974 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
10978 * This routine just checks to see whether
10979 * cur_blocked is blocked by ooa_io, which is ahead
10980 * of it in the queue. It doesn't queue/dequeue
10983 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
10985 case CTL_ACTION_BLOCK:
10986 case CTL_ACTION_OVERLAP:
10987 case CTL_ACTION_OVERLAP_TAG:
10988 case CTL_ACTION_SKIP:
10989 case CTL_ACTION_ERROR:
10991 break; /* NOTREACHED */
10992 case CTL_ACTION_PASS:
10995 panic("invalid action %d", action);
10996 break; /* NOTREACHED */
11000 return (CTL_ACTION_PASS);
11005 * - An I/O has just completed, and has been removed from the per-LUN OOA
11006 * queue, so some items on the blocked queue may now be unblocked.
11009 ctl_check_blocked(struct ctl_lun *lun)
11011 struct ctl_softc *softc = lun->ctl_softc;
11012 union ctl_io *cur_blocked, *next_blocked;
11014 mtx_assert(&lun->lun_lock, MA_OWNED);
11017 * Run forward from the head of the blocked queue, checking each
11018 * entry against the I/Os prior to it on the OOA queue to see if
11019 * there is still any blockage.
11021 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
11022 * with our removing a variable on it while it is traversing the
11025 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
11026 cur_blocked != NULL; cur_blocked = next_blocked) {
11027 union ctl_io *prev_ooa;
11030 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
11033 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
11034 ctl_ooaq, ooa_links);
11037 * If cur_blocked happens to be the first item in the OOA
11038 * queue now, prev_ooa will be NULL, and the action
11039 * returned will just be CTL_ACTION_PASS.
11041 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
11044 case CTL_ACTION_BLOCK:
11045 /* Nothing to do here, still blocked */
11047 case CTL_ACTION_OVERLAP:
11048 case CTL_ACTION_OVERLAP_TAG:
11050 * This shouldn't happen! In theory we've already
11051 * checked this command for overlap...
11054 case CTL_ACTION_PASS:
11055 case CTL_ACTION_SKIP: {
11056 const struct ctl_cmd_entry *entry;
11059 * The skip case shouldn't happen, this transaction
11060 * should have never made it onto the blocked queue.
11063 * This I/O is no longer blocked, we can remove it
11064 * from the blocked queue. Since this is a TAILQ
11065 * (doubly linked list), we can do O(1) removals
11066 * from any place on the list.
11068 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
11070 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11072 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
11073 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){
11075 * Need to send IO back to original side to
11078 union ctl_ha_msg msg_info;
11080 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11081 msg_info.hdr.original_sc =
11082 cur_blocked->io_hdr.original_sc;
11083 msg_info.hdr.serializing_sc = cur_blocked;
11084 msg_info.hdr.msg_type = CTL_MSG_R2R;
11085 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11086 sizeof(msg_info.hdr), M_NOWAIT);
11089 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
11092 * Check this I/O for LUN state changes that may
11093 * have happened while this command was blocked.
11094 * The LUN state may have been changed by a command
11095 * ahead of us in the queue, so we need to re-check
11096 * for any states that can be caused by SCSI
11099 if (ctl_scsiio_lun_check(lun, entry,
11100 &cur_blocked->scsiio) == 0) {
11101 cur_blocked->io_hdr.flags |=
11102 CTL_FLAG_IS_WAS_ON_RTR;
11103 ctl_enqueue_rtr(cur_blocked);
11105 ctl_done(cur_blocked);
11110 * This probably shouldn't happen -- we shouldn't
11111 * get CTL_ACTION_ERROR, or anything else.
11117 return (CTL_RETVAL_COMPLETE);
11121 * This routine (with one exception) checks LUN flags that can be set by
11122 * commands ahead of us in the OOA queue. These flags have to be checked
11123 * when a command initially comes in, and when we pull a command off the
11124 * blocked queue and are preparing to execute it. The reason we have to
11125 * check these flags for commands on the blocked queue is that the LUN
11126 * state may have been changed by a command ahead of us while we're on the
11129 * Ordering is somewhat important with these checks, so please pay
11130 * careful attention to the placement of any new checks.
11133 ctl_scsiio_lun_check(struct ctl_lun *lun,
11134 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11136 struct ctl_softc *softc = lun->ctl_softc;
11142 mtx_assert(&lun->lun_lock, MA_OWNED);
11145 * If this shelf is a secondary shelf controller, we may have to
11146 * reject some commands disallowed by HA mode and link state.
11148 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11149 if (softc->ha_link == CTL_HA_LINK_OFFLINE &&
11150 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11151 ctl_set_lun_unavail(ctsio);
11155 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 &&
11156 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11157 ctl_set_lun_transit(ctsio);
11161 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY &&
11162 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) {
11163 ctl_set_lun_standby(ctsio);
11168 /* The rest of checks are only done on executing side */
11169 if (softc->ha_mode == CTL_HA_MODE_XFER)
11173 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11175 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) {
11176 ctl_set_hw_write_protected(ctsio);
11180 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT]
11181 .eca_and_aen & SCP_SWP) != 0) {
11182 ctl_set_sense(ctsio, /*current_error*/ 1,
11183 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11184 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11191 * Check for a reservation conflict. If this command isn't allowed
11192 * even on reserved LUNs, and if this initiator isn't the one who
11193 * reserved us, reject the command with a reservation conflict.
11195 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11196 if ((lun->flags & CTL_LUN_RESERVED)
11197 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11198 if (lun->res_idx != residx) {
11199 ctl_set_reservation_conflict(ctsio);
11205 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11206 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11207 /* No reservation or command is allowed. */;
11208 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11209 (lun->res_type == SPR_TYPE_WR_EX ||
11210 lun->res_type == SPR_TYPE_WR_EX_RO ||
11211 lun->res_type == SPR_TYPE_WR_EX_AR)) {
11212 /* The command is allowed for Write Exclusive resv. */;
11215 * if we aren't registered or it's a res holder type
11216 * reservation and this isn't the res holder then set a
11219 if (ctl_get_prkey(lun, residx) == 0
11220 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
11221 ctl_set_reservation_conflict(ctsio);
11227 if ((lun->flags & CTL_LUN_OFFLINE)
11228 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0)) {
11229 ctl_set_lun_not_ready(ctsio);
11234 if ((lun->flags & CTL_LUN_STOPPED)
11235 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
11236 /* "Logical unit not ready, initializing cmd. required" */
11237 ctl_set_lun_stopped(ctsio);
11242 if ((lun->flags & CTL_LUN_INOPERABLE)
11243 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
11244 /* "Medium format corrupted" */
11245 ctl_set_medium_format_corrupted(ctsio);
11255 ctl_failover_io(union ctl_io *io, int have_lock)
11257 ctl_set_busy(&io->scsiio);
11262 ctl_failover_lun(struct ctl_lun *lun)
11264 struct ctl_softc *softc = lun->ctl_softc;
11265 struct ctl_io_hdr *io, *next_io;
11267 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", lun->lun));
11268 if (softc->ha_mode == CTL_HA_MODE_XFER) {
11269 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11270 /* We are master */
11271 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11272 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11273 io->flags |= CTL_FLAG_ABORT;
11274 io->flags |= CTL_FLAG_FAILOVER;
11275 } else { /* This can be only due to DATAMOVE */
11276 io->msg_type = CTL_MSG_DATAMOVE_DONE;
11277 io->flags &= ~CTL_FLAG_DMA_INPROG;
11278 io->flags |= CTL_FLAG_IO_ACTIVE;
11279 io->port_status = 31340;
11280 ctl_enqueue_isc((union ctl_io *)io);
11284 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11285 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11286 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11287 io->flags |= CTL_FLAG_FAILOVER;
11289 ctl_set_busy(&((union ctl_io *)io)->
11291 ctl_done((union ctl_io *)io);
11295 } else { /* SERIALIZE modes */
11296 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links,
11298 /* We are master */
11299 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11300 TAILQ_REMOVE(&lun->blocked_queue, io,
11302 io->flags &= ~CTL_FLAG_BLOCKED;
11303 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11304 ctl_free_io((union ctl_io *)io);
11307 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11308 /* We are master */
11309 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11310 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11311 ctl_free_io((union ctl_io *)io);
11314 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11315 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11316 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) {
11317 ctl_set_busy(&((union ctl_io *)io)->
11319 ctl_done((union ctl_io *)io);
11323 ctl_check_blocked(lun);
11328 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio)
11330 struct ctl_lun *lun;
11331 const struct ctl_cmd_entry *entry;
11332 uint32_t initidx, targ_lun;
11339 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11340 if ((targ_lun < CTL_MAX_LUNS)
11341 && ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
11343 * If the LUN is invalid, pretend that it doesn't exist.
11344 * It will go away as soon as all pending I/O has been
11347 mtx_lock(&lun->lun_lock);
11348 if (lun->flags & CTL_LUN_DISABLED) {
11349 mtx_unlock(&lun->lun_lock);
11351 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11352 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11354 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
11355 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
11359 * Every I/O goes into the OOA queue for a
11360 * particular LUN, and stays there until completion.
11363 if (TAILQ_EMPTY(&lun->ooa_queue)) {
11364 lun->idle_time += getsbinuptime() -
11368 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr,
11372 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11373 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11376 /* Get command entry and return error if it is unsuppotyed. */
11377 entry = ctl_validate_command(ctsio);
11378 if (entry == NULL) {
11380 mtx_unlock(&lun->lun_lock);
11384 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11385 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11388 * Check to see whether we can send this command to LUNs that don't
11389 * exist. This should pretty much only be the case for inquiry
11390 * and request sense. Further checks, below, really require having
11391 * a LUN, so we can't really check the command anymore. Just put
11392 * it on the rtr queue.
11395 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) {
11396 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11397 ctl_enqueue_rtr((union ctl_io *)ctsio);
11401 ctl_set_unsupported_lun(ctsio);
11402 ctl_done((union ctl_io *)ctsio);
11403 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11407 * Make sure we support this particular command on this LUN.
11408 * e.g., we don't support writes to the control LUN.
11410 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11411 mtx_unlock(&lun->lun_lock);
11412 ctl_set_invalid_opcode(ctsio);
11413 ctl_done((union ctl_io *)ctsio);
11418 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11422 * If we've got a request sense, it'll clear the contingent
11423 * allegiance condition. Otherwise, if we have a CA condition for
11424 * this initiator, clear it, because it sent down a command other
11425 * than request sense.
11427 if ((ctsio->cdb[0] != REQUEST_SENSE)
11428 && (ctl_is_set(lun->have_ca, initidx)))
11429 ctl_clear_mask(lun->have_ca, initidx);
11433 * If the command has this flag set, it handles its own unit
11434 * attention reporting, we shouldn't do anything. Otherwise we
11435 * check for any pending unit attentions, and send them back to the
11436 * initiator. We only do this when a command initially comes in,
11437 * not when we pull it off the blocked queue.
11439 * According to SAM-3, section 5.3.2, the order that things get
11440 * presented back to the host is basically unit attentions caused
11441 * by some sort of reset event, busy status, reservation conflicts
11442 * or task set full, and finally any other status.
11444 * One issue here is that some of the unit attentions we report
11445 * don't fall into the "reset" category (e.g. "reported luns data
11446 * has changed"). So reporting it here, before the reservation
11447 * check, may be technically wrong. I guess the only thing to do
11448 * would be to check for and report the reset events here, and then
11449 * check for the other unit attention types after we check for a
11450 * reservation conflict.
11452 * XXX KDM need to fix this
11454 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11455 ctl_ua_type ua_type;
11457 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11459 if (ua_type != CTL_UA_NONE) {
11460 mtx_unlock(&lun->lun_lock);
11461 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11462 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11463 ctsio->sense_len = SSD_FULL_SIZE;
11464 ctl_done((union ctl_io *)ctsio);
11470 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11471 mtx_unlock(&lun->lun_lock);
11472 ctl_done((union ctl_io *)ctsio);
11477 * XXX CHD this is where we want to send IO to other side if
11478 * this LUN is secondary on this SC. We will need to make a copy
11479 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11480 * the copy we send as FROM_OTHER.
11481 * We also need to stuff the address of the original IO so we can
11482 * find it easily. Something similar will need be done on the other
11483 * side so when we are done we can find the copy.
11485 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
11486 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 &&
11487 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) {
11488 union ctl_ha_msg msg_info;
11491 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11492 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11493 mtx_unlock(&lun->lun_lock);
11495 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11496 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11497 msg_info.hdr.serializing_sc = NULL;
11498 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11499 msg_info.scsi.tag_num = ctsio->tag_num;
11500 msg_info.scsi.tag_type = ctsio->tag_type;
11501 msg_info.scsi.cdb_len = ctsio->cdb_len;
11502 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11504 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11505 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data),
11506 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) {
11507 ctl_set_busy(ctsio);
11508 ctl_done((union ctl_io *)ctsio);
11514 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11515 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11516 ctl_ooaq, ooa_links))) {
11517 case CTL_ACTION_BLOCK:
11518 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11519 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11521 mtx_unlock(&lun->lun_lock);
11523 case CTL_ACTION_PASS:
11524 case CTL_ACTION_SKIP:
11525 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11526 mtx_unlock(&lun->lun_lock);
11527 ctl_enqueue_rtr((union ctl_io *)ctsio);
11529 case CTL_ACTION_OVERLAP:
11530 mtx_unlock(&lun->lun_lock);
11531 ctl_set_overlapped_cmd(ctsio);
11532 ctl_done((union ctl_io *)ctsio);
11534 case CTL_ACTION_OVERLAP_TAG:
11535 mtx_unlock(&lun->lun_lock);
11536 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11537 ctl_done((union ctl_io *)ctsio);
11539 case CTL_ACTION_ERROR:
11541 mtx_unlock(&lun->lun_lock);
11542 ctl_set_internal_failure(ctsio,
11544 /*retry_count*/ 0);
11545 ctl_done((union ctl_io *)ctsio);
11551 const struct ctl_cmd_entry *
11552 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11554 const struct ctl_cmd_entry *entry;
11555 int service_action;
11557 entry = &ctl_cmd_table[ctsio->cdb[0]];
11559 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11560 if (entry->flags & CTL_CMD_FLAG_SA5) {
11561 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11562 entry = &((const struct ctl_cmd_entry *)
11563 entry->execute)[service_action];
11568 const struct ctl_cmd_entry *
11569 ctl_validate_command(struct ctl_scsiio *ctsio)
11571 const struct ctl_cmd_entry *entry;
11575 entry = ctl_get_cmd_entry(ctsio, &sa);
11576 if (entry->execute == NULL) {
11578 ctl_set_invalid_field(ctsio,
11585 ctl_set_invalid_opcode(ctsio);
11586 ctl_done((union ctl_io *)ctsio);
11589 KASSERT(entry->length > 0,
11590 ("Not defined length for command 0x%02x/0x%02x",
11591 ctsio->cdb[0], ctsio->cdb[1]));
11592 for (i = 1; i < entry->length; i++) {
11593 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11596 ctl_set_invalid_field(ctsio,
11601 /*bit*/ fls(diff) - 1);
11602 ctl_done((union ctl_io *)ctsio);
11609 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11612 switch (lun_type) {
11614 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
11618 if ((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0)
11628 ctl_scsiio(struct ctl_scsiio *ctsio)
11631 const struct ctl_cmd_entry *entry;
11633 retval = CTL_RETVAL_COMPLETE;
11635 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11637 entry = ctl_get_cmd_entry(ctsio, NULL);
11640 * If this I/O has been aborted, just send it straight to
11641 * ctl_done() without executing it.
11643 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11644 ctl_done((union ctl_io *)ctsio);
11649 * All the checks should have been handled by ctl_scsiio_precheck().
11650 * We should be clear now to just execute the I/O.
11652 retval = entry->execute(ctsio);
11659 * Since we only implement one target right now, a bus reset simply resets
11660 * our single target.
11663 ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io)
11665 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET));
11669 ctl_target_reset(struct ctl_softc *softc, union ctl_io *io,
11670 ctl_ua_type ua_type)
11672 struct ctl_port *port;
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 if (ua_type==CTL_UA_TARG_RESET)
11681 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11683 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11684 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11685 msg_info.hdr.original_sc = NULL;
11686 msg_info.hdr.serializing_sc = NULL;
11687 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11688 sizeof(msg_info.task), M_WAITOK);
11692 mtx_lock(&softc->ctl_lock);
11693 port = softc->ctl_ports[io->io_hdr.nexus.targ_port];
11694 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11695 if (port != NULL &&
11696 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
11698 retval += ctl_do_lun_reset(lun, io, ua_type);
11700 mtx_unlock(&softc->ctl_lock);
11701 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11706 * The LUN should always be set. The I/O is optional, and is used to
11707 * distinguish between I/Os sent by this initiator, and by other
11708 * initiators. We set unit attention for initiators other than this one.
11709 * SAM-3 is vague on this point. It does say that a unit attention should
11710 * be established for other initiators when a LUN is reset (see section
11711 * 5.7.3), but it doesn't specifically say that the unit attention should
11712 * be established for this particular initiator when a LUN is reset. Here
11713 * is the relevant text, from SAM-3 rev 8:
11715 * 5.7.2 When a SCSI initiator port aborts its own tasks
11717 * When a SCSI initiator port causes its own task(s) to be aborted, no
11718 * notification that the task(s) have been aborted shall be returned to
11719 * the SCSI initiator port other than the completion response for the
11720 * command or task management function action that caused the task(s) to
11721 * be aborted and notification(s) associated with related effects of the
11722 * action (e.g., a reset unit attention condition).
11724 * XXX KDM for now, we're setting unit attention for all initiators.
11727 ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11737 mtx_lock(&lun->lun_lock);
11739 * Run through the OOA queue and abort each I/O.
11741 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11742 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11743 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11747 * This version sets unit attention for every
11750 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11751 ctl_est_ua_all(lun, initidx, ua_type);
11753 ctl_est_ua_all(lun, -1, ua_type);
11757 * A reset (any kind, really) clears reservations established with
11758 * RESERVE/RELEASE. It does not clear reservations established
11759 * with PERSISTENT RESERVE OUT, but we don't support that at the
11760 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
11761 * reservations made with the RESERVE/RELEASE commands, because
11762 * those commands are obsolete in SPC-3.
11764 lun->flags &= ~CTL_LUN_RESERVED;
11767 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11768 ctl_clear_mask(lun->have_ca, i);
11770 mtx_unlock(&lun->lun_lock);
11776 ctl_lun_reset(struct ctl_softc *softc, union ctl_io *io)
11778 struct ctl_lun *lun;
11782 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11783 mtx_lock(&softc->ctl_lock);
11784 if ((targ_lun >= CTL_MAX_LUNS) ||
11785 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11786 mtx_unlock(&softc->ctl_lock);
11787 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11790 retval = ctl_do_lun_reset(lun, io, CTL_UA_LUN_RESET);
11791 mtx_unlock(&softc->ctl_lock);
11792 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11794 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) {
11795 union ctl_ha_msg msg_info;
11797 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11798 msg_info.hdr.nexus = io->io_hdr.nexus;
11799 msg_info.task.task_action = CTL_TASK_LUN_RESET;
11800 msg_info.hdr.original_sc = NULL;
11801 msg_info.hdr.serializing_sc = NULL;
11802 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11803 sizeof(msg_info.task), M_WAITOK);
11809 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11814 mtx_assert(&lun->lun_lock, MA_OWNED);
11817 * Run through the OOA queue and attempt to find the given I/O.
11818 * The target port, initiator ID, tag type and tag number have to
11819 * match the values that we got from the initiator. If we have an
11820 * untagged command to abort, simply abort the first untagged command
11821 * we come to. We only allow one untagged command at a time of course.
11823 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11824 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11826 if ((targ_port == UINT32_MAX ||
11827 targ_port == xio->io_hdr.nexus.targ_port) &&
11828 (init_id == UINT32_MAX ||
11829 init_id == xio->io_hdr.nexus.initid)) {
11830 if (targ_port != xio->io_hdr.nexus.targ_port ||
11831 init_id != xio->io_hdr.nexus.initid)
11832 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
11833 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11834 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11835 union ctl_ha_msg msg_info;
11837 msg_info.hdr.nexus = xio->io_hdr.nexus;
11838 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11839 msg_info.task.tag_num = xio->scsiio.tag_num;
11840 msg_info.task.tag_type = xio->scsiio.tag_type;
11841 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11842 msg_info.hdr.original_sc = NULL;
11843 msg_info.hdr.serializing_sc = NULL;
11844 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11845 sizeof(msg_info.task), M_NOWAIT);
11852 ctl_abort_task_set(union ctl_io *io)
11854 struct ctl_softc *softc = control_softc;
11855 struct ctl_lun *lun;
11861 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11862 mtx_lock(&softc->ctl_lock);
11863 if ((targ_lun >= CTL_MAX_LUNS) ||
11864 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11865 mtx_unlock(&softc->ctl_lock);
11866 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11870 mtx_lock(&lun->lun_lock);
11871 mtx_unlock(&softc->ctl_lock);
11872 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
11873 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11874 io->io_hdr.nexus.initid,
11875 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11876 } else { /* CTL_TASK_CLEAR_TASK_SET */
11877 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
11878 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11880 mtx_unlock(&lun->lun_lock);
11881 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11886 ctl_i_t_nexus_reset(union ctl_io *io)
11888 struct ctl_softc *softc = control_softc;
11889 struct ctl_lun *lun;
11892 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11893 union ctl_ha_msg msg_info;
11895 msg_info.hdr.nexus = io->io_hdr.nexus;
11896 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET;
11897 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11898 msg_info.hdr.original_sc = NULL;
11899 msg_info.hdr.serializing_sc = NULL;
11900 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11901 sizeof(msg_info.task), M_WAITOK);
11904 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11905 mtx_lock(&softc->ctl_lock);
11906 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11907 mtx_lock(&lun->lun_lock);
11908 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11909 io->io_hdr.nexus.initid, 1);
11911 ctl_clear_mask(lun->have_ca, initidx);
11913 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx))
11914 lun->flags &= ~CTL_LUN_RESERVED;
11915 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS);
11916 mtx_unlock(&lun->lun_lock);
11918 mtx_unlock(&softc->ctl_lock);
11919 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11924 ctl_abort_task(union ctl_io *io)
11927 struct ctl_lun *lun;
11928 struct ctl_softc *softc;
11931 char printbuf[128];
11936 softc = control_softc;
11942 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11943 mtx_lock(&softc->ctl_lock);
11944 if ((targ_lun >= CTL_MAX_LUNS) ||
11945 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11946 mtx_unlock(&softc->ctl_lock);
11947 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11952 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
11953 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
11956 mtx_lock(&lun->lun_lock);
11957 mtx_unlock(&softc->ctl_lock);
11959 * Run through the OOA queue and attempt to find the given I/O.
11960 * The target port, initiator ID, tag type and tag number have to
11961 * match the values that we got from the initiator. If we have an
11962 * untagged command to abort, simply abort the first untagged command
11963 * we come to. We only allow one untagged command at a time of course.
11965 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11966 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11968 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
11970 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
11971 lun->lun, xio->scsiio.tag_num,
11972 xio->scsiio.tag_type,
11973 (xio->io_hdr.blocked_links.tqe_prev
11974 == NULL) ? "" : " BLOCKED",
11975 (xio->io_hdr.flags &
11976 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
11977 (xio->io_hdr.flags &
11978 CTL_FLAG_ABORT) ? " ABORT" : "",
11979 (xio->io_hdr.flags &
11980 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
11981 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
11983 printf("%s\n", sbuf_data(&sb));
11986 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
11987 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
11988 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
11992 * If the abort says that the task is untagged, the
11993 * task in the queue must be untagged. Otherwise,
11994 * we just check to see whether the tag numbers
11995 * match. This is because the QLogic firmware
11996 * doesn't pass back the tag type in an abort
12000 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12001 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12002 || (xio->scsiio.tag_num == io->taskio.tag_num))
12005 * XXX KDM we've got problems with FC, because it
12006 * doesn't send down a tag type with aborts. So we
12007 * can only really go by the tag number...
12008 * This may cause problems with parallel SCSI.
12009 * Need to figure that out!!
12011 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12012 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12014 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
12015 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12016 union ctl_ha_msg msg_info;
12018 msg_info.hdr.nexus = io->io_hdr.nexus;
12019 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12020 msg_info.task.tag_num = io->taskio.tag_num;
12021 msg_info.task.tag_type = io->taskio.tag_type;
12022 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12023 msg_info.hdr.original_sc = NULL;
12024 msg_info.hdr.serializing_sc = NULL;
12026 printf("Sent Abort to other side\n");
12028 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12029 sizeof(msg_info.task), M_NOWAIT);
12032 printf("ctl_abort_task: found I/O to abort\n");
12036 mtx_unlock(&lun->lun_lock);
12040 * This isn't really an error. It's entirely possible for
12041 * the abort and command completion to cross on the wire.
12042 * This is more of an informative/diagnostic error.
12045 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
12046 "%u:%u:%u tag %d type %d\n",
12047 io->io_hdr.nexus.initid,
12048 io->io_hdr.nexus.targ_port,
12049 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
12050 io->taskio.tag_type);
12053 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12058 ctl_query_task(union ctl_io *io, int task_set)
12061 struct ctl_lun *lun;
12062 struct ctl_softc *softc;
12066 softc = control_softc;
12067 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12068 mtx_lock(&softc->ctl_lock);
12069 if ((targ_lun >= CTL_MAX_LUNS) ||
12070 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12071 mtx_unlock(&softc->ctl_lock);
12072 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12075 mtx_lock(&lun->lun_lock);
12076 mtx_unlock(&softc->ctl_lock);
12077 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12078 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12080 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12081 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12082 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12085 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) {
12090 mtx_unlock(&lun->lun_lock);
12092 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12094 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12099 ctl_query_async_event(union ctl_io *io)
12101 struct ctl_lun *lun;
12102 struct ctl_softc *softc;
12104 uint32_t targ_lun, initidx;
12106 softc = control_softc;
12107 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12108 mtx_lock(&softc->ctl_lock);
12109 if ((targ_lun >= CTL_MAX_LUNS) ||
12110 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12111 mtx_unlock(&softc->ctl_lock);
12112 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12115 mtx_lock(&lun->lun_lock);
12116 mtx_unlock(&softc->ctl_lock);
12117 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12118 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp);
12119 mtx_unlock(&lun->lun_lock);
12120 if (ua != CTL_UA_NONE)
12121 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12123 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12128 ctl_run_task(union ctl_io *io)
12130 struct ctl_softc *softc = control_softc;
12133 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12134 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12135 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type));
12136 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED;
12137 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp));
12138 switch (io->taskio.task_action) {
12139 case CTL_TASK_ABORT_TASK:
12140 retval = ctl_abort_task(io);
12142 case CTL_TASK_ABORT_TASK_SET:
12143 case CTL_TASK_CLEAR_TASK_SET:
12144 retval = ctl_abort_task_set(io);
12146 case CTL_TASK_CLEAR_ACA:
12148 case CTL_TASK_I_T_NEXUS_RESET:
12149 retval = ctl_i_t_nexus_reset(io);
12151 case CTL_TASK_LUN_RESET:
12152 retval = ctl_lun_reset(softc, io);
12154 case CTL_TASK_TARGET_RESET:
12155 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET);
12157 case CTL_TASK_BUS_RESET:
12158 retval = ctl_bus_reset(softc, io);
12160 case CTL_TASK_PORT_LOGIN:
12162 case CTL_TASK_PORT_LOGOUT:
12164 case CTL_TASK_QUERY_TASK:
12165 retval = ctl_query_task(io, 0);
12167 case CTL_TASK_QUERY_TASK_SET:
12168 retval = ctl_query_task(io, 1);
12170 case CTL_TASK_QUERY_ASYNC_EVENT:
12171 retval = ctl_query_async_event(io);
12174 printf("%s: got unknown task management event %d\n",
12175 __func__, io->taskio.task_action);
12179 io->io_hdr.status = CTL_SUCCESS;
12181 io->io_hdr.status = CTL_ERROR;
12186 * For HA operation. Handle commands that come in from the other
12190 ctl_handle_isc(union ctl_io *io)
12193 struct ctl_lun *lun;
12194 struct ctl_softc *softc;
12197 softc = control_softc;
12199 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12200 lun = softc->ctl_luns[targ_lun];
12202 switch (io->io_hdr.msg_type) {
12203 case CTL_MSG_SERIALIZE:
12204 free_io = ctl_serialize_other_sc_cmd(&io->scsiio);
12206 case CTL_MSG_R2R: {
12207 const struct ctl_cmd_entry *entry;
12210 * This is only used in SER_ONLY mode.
12213 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12214 mtx_lock(&lun->lun_lock);
12215 if (ctl_scsiio_lun_check(lun,
12216 entry, (struct ctl_scsiio *)io) != 0) {
12217 mtx_unlock(&lun->lun_lock);
12221 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12222 mtx_unlock(&lun->lun_lock);
12223 ctl_enqueue_rtr(io);
12226 case CTL_MSG_FINISH_IO:
12227 if (softc->ha_mode == CTL_HA_MODE_XFER) {
12232 mtx_lock(&lun->lun_lock);
12233 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
12235 ctl_check_blocked(lun);
12236 mtx_unlock(&lun->lun_lock);
12239 case CTL_MSG_PERS_ACTION:
12240 ctl_hndl_per_res_out_on_other_sc(
12241 (union ctl_ha_msg *)&io->presio.pr_msg);
12244 case CTL_MSG_BAD_JUJU:
12248 case CTL_MSG_DATAMOVE:
12249 /* Only used in XFER mode */
12251 ctl_datamove_remote(io);
12253 case CTL_MSG_DATAMOVE_DONE:
12254 /* Only used in XFER mode */
12256 io->scsiio.be_move_done(io);
12258 case CTL_MSG_FAILOVER:
12259 mtx_lock(&lun->lun_lock);
12260 ctl_failover_lun(lun);
12261 mtx_unlock(&lun->lun_lock);
12266 printf("%s: Invalid message type %d\n",
12267 __func__, io->io_hdr.msg_type);
12277 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12278 * there is no match.
12280 static ctl_lun_error_pattern
12281 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12283 const struct ctl_cmd_entry *entry;
12284 ctl_lun_error_pattern filtered_pattern, pattern;
12286 pattern = desc->error_pattern;
12289 * XXX KDM we need more data passed into this function to match a
12290 * custom pattern, and we actually need to implement custom pattern
12293 if (pattern & CTL_LUN_PAT_CMD)
12294 return (CTL_LUN_PAT_CMD);
12296 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12297 return (CTL_LUN_PAT_ANY);
12299 entry = ctl_get_cmd_entry(ctsio, NULL);
12301 filtered_pattern = entry->pattern & pattern;
12304 * If the user requested specific flags in the pattern (e.g.
12305 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12308 * If the user did not specify any flags, it doesn't matter whether
12309 * or not the command supports the flags.
12311 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12312 (pattern & ~CTL_LUN_PAT_MASK))
12313 return (CTL_LUN_PAT_NONE);
12316 * If the user asked for a range check, see if the requested LBA
12317 * range overlaps with this command's LBA range.
12319 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12325 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12327 return (CTL_LUN_PAT_NONE);
12329 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12330 desc->lba_range.len, FALSE);
12332 * A "pass" means that the LBA ranges don't overlap, so
12333 * this doesn't match the user's range criteria.
12335 if (action == CTL_ACTION_PASS)
12336 return (CTL_LUN_PAT_NONE);
12339 return (filtered_pattern);
12343 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12345 struct ctl_error_desc *desc, *desc2;
12347 mtx_assert(&lun->lun_lock, MA_OWNED);
12349 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12350 ctl_lun_error_pattern pattern;
12352 * Check to see whether this particular command matches
12353 * the pattern in the descriptor.
12355 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12356 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12359 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12360 case CTL_LUN_INJ_ABORTED:
12361 ctl_set_aborted(&io->scsiio);
12363 case CTL_LUN_INJ_MEDIUM_ERR:
12364 ctl_set_medium_error(&io->scsiio,
12365 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) !=
12366 CTL_FLAG_DATA_OUT);
12368 case CTL_LUN_INJ_UA:
12369 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12371 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12373 case CTL_LUN_INJ_CUSTOM:
12375 * We're assuming the user knows what he is doing.
12376 * Just copy the sense information without doing
12379 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12380 MIN(sizeof(desc->custom_sense),
12381 sizeof(io->scsiio.sense_data)));
12382 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12383 io->scsiio.sense_len = SSD_FULL_SIZE;
12384 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12386 case CTL_LUN_INJ_NONE:
12389 * If this is an error injection type we don't know
12390 * about, clear the continuous flag (if it is set)
12391 * so it will get deleted below.
12393 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12397 * By default, each error injection action is a one-shot
12399 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12402 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12408 #ifdef CTL_IO_DELAY
12410 ctl_datamove_timer_wakeup(void *arg)
12414 io = (union ctl_io *)arg;
12418 #endif /* CTL_IO_DELAY */
12421 ctl_datamove(union ctl_io *io)
12423 struct ctl_lun *lun;
12424 void (*fe_datamove)(union ctl_io *io);
12426 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12428 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12430 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12432 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12437 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12438 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12440 sbuf_cat(&sb, path_str);
12441 switch (io->io_hdr.io_type) {
12443 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12444 sbuf_printf(&sb, "\n");
12445 sbuf_cat(&sb, path_str);
12446 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12447 io->scsiio.tag_num, io->scsiio.tag_type);
12450 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12451 "Tag Type: %d\n", io->taskio.task_action,
12452 io->taskio.tag_num, io->taskio.tag_type);
12455 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12456 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12459 sbuf_cat(&sb, path_str);
12460 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12461 (intmax_t)time_uptime - io->io_hdr.start_time);
12463 printf("%s", sbuf_data(&sb));
12465 #endif /* CTL_TIME_IO */
12467 #ifdef CTL_IO_DELAY
12468 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12469 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12472 && (lun->delay_info.datamove_delay > 0)) {
12474 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
12475 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12476 callout_reset(&io->io_hdr.delay_callout,
12477 lun->delay_info.datamove_delay * hz,
12478 ctl_datamove_timer_wakeup, io);
12479 if (lun->delay_info.datamove_type ==
12480 CTL_DELAY_TYPE_ONESHOT)
12481 lun->delay_info.datamove_delay = 0;
12488 * This command has been aborted. Set the port status, so we fail
12491 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12492 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n",
12493 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12494 io->io_hdr.nexus.targ_port,
12495 io->io_hdr.nexus.targ_lun);
12496 io->io_hdr.port_status = 31337;
12498 * Note that the backend, in this case, will get the
12499 * callback in its context. In other cases it may get
12500 * called in the frontend's interrupt thread context.
12502 io->scsiio.be_move_done(io);
12506 /* Don't confuse frontend with zero length data move. */
12507 if (io->scsiio.kern_data_len == 0) {
12508 io->scsiio.be_move_done(io);
12513 * If we're in XFER mode and this I/O is from the other shelf
12514 * controller, we need to send the DMA to the other side to
12515 * actually transfer the data to/from the host. In serialize only
12516 * mode the transfer happens below CTL and ctl_datamove() is only
12517 * called on the machine that originally received the I/O.
12519 if ((control_softc->ha_mode == CTL_HA_MODE_XFER)
12520 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12521 union ctl_ha_msg msg;
12522 uint32_t sg_entries_sent;
12526 memset(&msg, 0, sizeof(msg));
12527 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
12528 msg.hdr.original_sc = io->io_hdr.original_sc;
12529 msg.hdr.serializing_sc = io;
12530 msg.hdr.nexus = io->io_hdr.nexus;
12531 msg.hdr.status = io->io_hdr.status;
12532 msg.dt.flags = io->io_hdr.flags;
12534 * We convert everything into a S/G list here. We can't
12535 * pass by reference, only by value between controllers.
12536 * So we can't pass a pointer to the S/G list, only as many
12537 * S/G entries as we can fit in here. If it's possible for
12538 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
12539 * then we need to break this up into multiple transfers.
12541 if (io->scsiio.kern_sg_entries == 0) {
12542 msg.dt.kern_sg_entries = 1;
12545 * Convert to a physical address if this is a
12548 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
12549 msg.dt.sg_list[0].addr =
12550 io->scsiio.kern_data_ptr;
12553 * XXX KDM use busdma here!
12555 msg.dt.sg_list[0].addr = (void *)
12556 vtophys(io->scsiio.kern_data_ptr);
12559 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
12560 ("HA does not support BUS_ADDR"));
12561 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
12564 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
12567 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
12571 msg.dt.kern_data_len = io->scsiio.kern_data_len;
12572 msg.dt.kern_total_len = io->scsiio.kern_total_len;
12573 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
12574 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
12575 msg.dt.sg_sequence = 0;
12578 * Loop until we've sent all of the S/G entries. On the
12579 * other end, we'll recompose these S/G entries into one
12580 * contiguous list before passing it to the
12582 for (sg_entries_sent = 0; sg_entries_sent <
12583 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) {
12584 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list)/
12585 sizeof(msg.dt.sg_list[0])),
12586 msg.dt.kern_sg_entries - sg_entries_sent);
12588 if (do_sg_copy != 0) {
12589 struct ctl_sg_entry *sgl;
12592 sgl = (struct ctl_sg_entry *)
12593 io->scsiio.kern_data_ptr;
12595 * If this is in cached memory, flush the cache
12596 * before we send the DMA request to the other
12597 * controller. We want to do this in either
12598 * the * read or the write case. The read
12599 * case is straightforward. In the write
12600 * case, we want to make sure nothing is
12601 * in the local cache that could overwrite
12605 for (i = sg_entries_sent, j = 0;
12606 i < msg.dt.cur_sg_entries; i++, j++) {
12608 if ((io->io_hdr.flags &
12609 CTL_FLAG_BUS_ADDR) == 0) {
12611 * XXX KDM use busdma.
12613 msg.dt.sg_list[j].addr =(void *)
12614 vtophys(sgl[i].addr);
12616 msg.dt.sg_list[j].addr =
12620 KASSERT((io->io_hdr.flags &
12621 CTL_FLAG_BUS_ADDR) == 0,
12622 ("HA does not support BUS_ADDR"));
12623 msg.dt.sg_list[j].addr = sgl[i].addr;
12625 msg.dt.sg_list[j].len = sgl[i].len;
12629 sg_entries_sent += msg.dt.cur_sg_entries;
12630 if (sg_entries_sent >= msg.dt.kern_sg_entries)
12631 msg.dt.sg_last = 1;
12633 msg.dt.sg_last = 0;
12635 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12636 sizeof(msg.dt) - sizeof(msg.dt.sg_list) +
12637 sizeof(struct ctl_sg_entry)*msg.dt.cur_sg_entries,
12638 M_WAITOK) > CTL_HA_STATUS_SUCCESS) {
12639 io->io_hdr.port_status = 31341;
12640 io->scsiio.be_move_done(io);
12644 msg.dt.sent_sg_entries = sg_entries_sent;
12648 * Officially handover the request from us to peer.
12649 * If failover has just happened, then we must return error.
12650 * If failover happen just after, then it is not our problem.
12653 mtx_lock(&lun->lun_lock);
12654 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12656 mtx_unlock(&lun->lun_lock);
12657 io->io_hdr.port_status = 31342;
12658 io->scsiio.be_move_done(io);
12661 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12662 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
12664 mtx_unlock(&lun->lun_lock);
12668 * Lookup the fe_datamove() function for this particular
12671 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12678 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12680 union ctl_ha_msg msg;
12682 memset(&msg, 0, sizeof(msg));
12684 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12685 msg.hdr.original_sc = io;
12686 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12687 msg.hdr.nexus = io->io_hdr.nexus;
12688 msg.hdr.status = io->io_hdr.status;
12689 msg.scsi.tag_num = io->scsiio.tag_num;
12690 msg.scsi.tag_type = io->scsiio.tag_type;
12691 msg.scsi.scsi_status = io->scsiio.scsi_status;
12692 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12693 io->scsiio.sense_len);
12694 msg.scsi.sense_len = io->scsiio.sense_len;
12695 msg.scsi.sense_residual = io->scsiio.sense_residual;
12696 msg.scsi.fetd_status = io->io_hdr.port_status;
12697 msg.scsi.residual = io->scsiio.residual;
12698 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12700 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12701 ctl_failover_io(io, /*have_lock*/ have_lock);
12705 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12706 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12707 msg.scsi.sense_len, M_WAITOK);
12711 * The DMA to the remote side is done, now we need to tell the other side
12712 * we're done so it can continue with its data movement.
12715 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12722 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12723 printf("%s: ISC DMA write failed with error %d", __func__,
12725 ctl_set_internal_failure(&io->scsiio,
12727 /*retry_count*/ rq->ret);
12730 ctl_dt_req_free(rq);
12732 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12733 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12734 free(io->io_hdr.remote_sglist, M_CTL);
12735 io->io_hdr.remote_sglist = NULL;
12736 io->io_hdr.local_sglist = NULL;
12739 * The data is in local and remote memory, so now we need to send
12740 * status (good or back) back to the other side.
12742 ctl_send_datamove_done(io, /*have_lock*/ 0);
12746 * We've moved the data from the host/controller into local memory. Now we
12747 * need to push it over to the remote controller's memory.
12750 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12754 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12755 ctl_datamove_remote_write_cb);
12760 ctl_datamove_remote_write(union ctl_io *io)
12763 void (*fe_datamove)(union ctl_io *io);
12766 * - Get the data from the host/HBA into local memory.
12767 * - DMA memory from the local controller to the remote controller.
12768 * - Send status back to the remote controller.
12771 retval = ctl_datamove_remote_sgl_setup(io);
12775 /* Switch the pointer over so the FETD knows what to do */
12776 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12779 * Use a custom move done callback, since we need to send completion
12780 * back to the other controller, not to the backend on this side.
12782 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12784 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12789 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12798 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12799 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12800 free(io->io_hdr.remote_sglist, M_CTL);
12801 io->io_hdr.remote_sglist = NULL;
12802 io->io_hdr.local_sglist = NULL;
12805 scsi_path_string(io, path_str, sizeof(path_str));
12806 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12807 sbuf_cat(&sb, path_str);
12808 scsi_command_string(&io->scsiio, NULL, &sb);
12809 sbuf_printf(&sb, "\n");
12810 sbuf_cat(&sb, path_str);
12811 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12812 io->scsiio.tag_num, io->scsiio.tag_type);
12813 sbuf_cat(&sb, path_str);
12814 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12815 io->io_hdr.flags, io->io_hdr.status);
12817 printk("%s", sbuf_data(&sb));
12822 * The read is done, now we need to send status (good or bad) back
12823 * to the other side.
12825 ctl_send_datamove_done(io, /*have_lock*/ 0);
12831 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12834 void (*fe_datamove)(union ctl_io *io);
12838 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12839 printf("%s: ISC DMA read failed with error %d\n", __func__,
12841 ctl_set_internal_failure(&io->scsiio,
12843 /*retry_count*/ rq->ret);
12846 ctl_dt_req_free(rq);
12848 /* Switch the pointer over so the FETD knows what to do */
12849 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12852 * Use a custom move done callback, since we need to send completion
12853 * back to the other controller, not to the backend on this side.
12855 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12857 /* XXX KDM add checks like the ones in ctl_datamove? */
12859 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12864 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12866 struct ctl_sg_entry *local_sglist;
12867 struct ctl_softc *softc;
12868 uint32_t len_to_go;
12873 softc = control_softc;
12874 local_sglist = io->io_hdr.local_sglist;
12875 len_to_go = io->scsiio.kern_data_len;
12878 * The difficult thing here is that the size of the various
12879 * S/G segments may be different than the size from the
12880 * remote controller. That'll make it harder when DMAing
12881 * the data back to the other side.
12883 for (i = 0; len_to_go > 0; i++) {
12884 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT);
12885 local_sglist[i].addr =
12886 malloc(local_sglist[i].len, M_CTL, M_WAITOK);
12888 len_to_go -= local_sglist[i].len;
12891 * Reset the number of S/G entries accordingly. The original
12892 * number of S/G entries is available in rem_sg_entries.
12894 io->scsiio.kern_sg_entries = i;
12897 printf("%s: kern_sg_entries = %d\n", __func__,
12898 io->scsiio.kern_sg_entries);
12899 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12900 printf("%s: sg[%d] = %p, %lu\n", __func__, i,
12901 local_sglist[i].addr, local_sglist[i].len);
12908 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12909 ctl_ha_dt_cb callback)
12911 struct ctl_ha_dt_req *rq;
12912 struct ctl_sg_entry *remote_sglist, *local_sglist;
12913 uint32_t local_used, remote_used, total_used;
12916 rq = ctl_dt_req_alloc();
12919 * If we failed to allocate the request, and if the DMA didn't fail
12920 * anyway, set busy status. This is just a resource allocation
12924 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12925 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS))
12926 ctl_set_busy(&io->scsiio);
12928 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12929 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
12932 ctl_dt_req_free(rq);
12935 * The data move failed. We need to return status back
12936 * to the other controller. No point in trying to DMA
12937 * data to the remote controller.
12940 ctl_send_datamove_done(io, /*have_lock*/ 0);
12945 local_sglist = io->io_hdr.local_sglist;
12946 remote_sglist = io->io_hdr.remote_sglist;
12952 * Pull/push the data over the wire from/to the other controller.
12953 * This takes into account the possibility that the local and
12954 * remote sglists may not be identical in terms of the size of
12955 * the elements and the number of elements.
12957 * One fundamental assumption here is that the length allocated for
12958 * both the local and remote sglists is identical. Otherwise, we've
12959 * essentially got a coding error of some sort.
12961 isc_ret = CTL_HA_STATUS_SUCCESS;
12962 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12966 rq->command = command;
12970 * Both pointers should be aligned. But it is possible
12971 * that the allocation length is not. They should both
12972 * also have enough slack left over at the end, though,
12973 * to round up to the next 8 byte boundary.
12975 cur_len = MIN(local_sglist[i].len - local_used,
12976 remote_sglist[j].len - remote_used);
12977 rq->size = cur_len;
12979 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12980 tmp_ptr += local_used;
12983 /* Use physical addresses when talking to ISC hardware */
12984 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12985 /* XXX KDM use busdma */
12986 rq->local = vtophys(tmp_ptr);
12988 rq->local = tmp_ptr;
12990 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
12991 ("HA does not support BUS_ADDR"));
12992 rq->local = tmp_ptr;
12995 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12996 tmp_ptr += remote_used;
12997 rq->remote = tmp_ptr;
12999 rq->callback = NULL;
13001 local_used += cur_len;
13002 if (local_used >= local_sglist[i].len) {
13007 remote_used += cur_len;
13008 if (remote_used >= remote_sglist[j].len) {
13012 total_used += cur_len;
13014 if (total_used >= io->scsiio.kern_data_len)
13015 rq->callback = callback;
13018 printf("%s: %s: local %p remote %p size %d\n", __func__,
13019 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
13020 rq->local, rq->remote, rq->size);
13023 isc_ret = ctl_dt_single(rq);
13024 if (isc_ret > CTL_HA_STATUS_SUCCESS)
13027 if (isc_ret != CTL_HA_STATUS_WAIT) {
13036 ctl_datamove_remote_read(union ctl_io *io)
13042 * This will send an error to the other controller in the case of a
13045 retval = ctl_datamove_remote_sgl_setup(io);
13049 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
13050 ctl_datamove_remote_read_cb);
13053 * Make sure we free memory if there was an error.. The
13054 * ctl_datamove_remote_xfer() function will send the
13055 * datamove done message, or call the callback with an
13056 * error if there is a problem.
13058 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13059 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13060 free(io->io_hdr.remote_sglist, M_CTL);
13061 io->io_hdr.remote_sglist = NULL;
13062 io->io_hdr.local_sglist = NULL;
13067 * Process a datamove request from the other controller. This is used for
13068 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
13069 * first. Once that is complete, the data gets DMAed into the remote
13070 * controller's memory. For reads, we DMA from the remote controller's
13071 * memory into our memory first, and then move it out to the FETD.
13074 ctl_datamove_remote(union ctl_io *io)
13077 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
13079 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
13080 ctl_failover_io(io, /*have_lock*/ 0);
13085 * Note that we look for an aborted I/O here, but don't do some of
13086 * the other checks that ctl_datamove() normally does.
13087 * We don't need to run the datamove delay code, since that should
13088 * have been done if need be on the other controller.
13090 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
13091 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__,
13092 io->scsiio.tag_num, io->io_hdr.nexus.initid,
13093 io->io_hdr.nexus.targ_port,
13094 io->io_hdr.nexus.targ_lun);
13095 io->io_hdr.port_status = 31338;
13096 ctl_send_datamove_done(io, /*have_lock*/ 0);
13100 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
13101 ctl_datamove_remote_write(io);
13102 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
13103 ctl_datamove_remote_read(io);
13105 io->io_hdr.port_status = 31339;
13106 ctl_send_datamove_done(io, /*have_lock*/ 0);
13111 ctl_process_done(union ctl_io *io)
13113 struct ctl_lun *lun;
13114 struct ctl_softc *softc = control_softc;
13115 void (*fe_done)(union ctl_io *io);
13116 union ctl_ha_msg msg;
13117 uint32_t targ_port = io->io_hdr.nexus.targ_port;
13119 CTL_DEBUG_PRINT(("ctl_process_done\n"));
13121 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0)
13122 fe_done = softc->ctl_ports[targ_port]->fe_done;
13127 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
13132 ctl_scsi_path_string(io, path_str, sizeof(path_str));
13133 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13135 sbuf_cat(&sb, path_str);
13136 switch (io->io_hdr.io_type) {
13138 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
13139 sbuf_printf(&sb, "\n");
13140 sbuf_cat(&sb, path_str);
13141 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13142 io->scsiio.tag_num, io->scsiio.tag_type);
13145 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
13146 "Tag Type: %d\n", io->taskio.task_action,
13147 io->taskio.tag_num, io->taskio.tag_type);
13150 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13151 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13154 sbuf_cat(&sb, path_str);
13155 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
13156 (intmax_t)time_uptime - io->io_hdr.start_time);
13158 printf("%s", sbuf_data(&sb));
13160 #endif /* CTL_TIME_IO */
13162 switch (io->io_hdr.io_type) {
13166 if (ctl_debug & CTL_DEBUG_INFO)
13167 ctl_io_error_print(io, NULL);
13168 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
13172 return (CTL_RETVAL_COMPLETE);
13174 panic("ctl_process_done: invalid io type %d\n",
13175 io->io_hdr.io_type);
13176 break; /* NOTREACHED */
13179 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13181 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13182 io->io_hdr.nexus.targ_mapped_lun));
13186 mtx_lock(&lun->lun_lock);
13189 * Check to see if we have any errors to inject here. We only
13190 * inject errors for commands that don't already have errors set.
13192 if ((STAILQ_FIRST(&lun->error_list) != NULL) &&
13193 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
13194 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
13195 ctl_inject_error(lun, io);
13198 * XXX KDM how do we treat commands that aren't completed
13201 * XXX KDM should we also track I/O latency?
13203 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13204 io->io_hdr.io_type == CTL_IO_SCSI) {
13206 struct bintime cur_bt;
13210 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13212 type = CTL_STATS_READ;
13213 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13215 type = CTL_STATS_WRITE;
13217 type = CTL_STATS_NO_IO;
13219 lun->stats.ports[targ_port].bytes[type] +=
13220 io->scsiio.kern_total_len;
13221 lun->stats.ports[targ_port].operations[type]++;
13223 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
13224 &io->io_hdr.dma_bt);
13225 lun->stats.ports[targ_port].num_dmas[type] +=
13226 io->io_hdr.num_dmas;
13227 getbintime(&cur_bt);
13228 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
13229 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
13234 * Remove this from the OOA queue.
13236 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13238 if (TAILQ_EMPTY(&lun->ooa_queue))
13239 lun->last_busy = getsbinuptime();
13243 * Run through the blocked queue on this LUN and see if anything
13244 * has become unblocked, now that this transaction is done.
13246 ctl_check_blocked(lun);
13249 * If the LUN has been invalidated, free it if there is nothing
13250 * left on its OOA queue.
13252 if ((lun->flags & CTL_LUN_INVALID)
13253 && TAILQ_EMPTY(&lun->ooa_queue)) {
13254 mtx_unlock(&lun->lun_lock);
13255 mtx_lock(&softc->ctl_lock);
13257 mtx_unlock(&softc->ctl_lock);
13259 mtx_unlock(&lun->lun_lock);
13264 * If this command has been aborted, make sure we set the status
13265 * properly. The FETD is responsible for freeing the I/O and doing
13266 * whatever it needs to do to clean up its state.
13268 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13269 ctl_set_task_aborted(&io->scsiio);
13272 * If enabled, print command error status.
13274 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
13275 (ctl_debug & CTL_DEBUG_INFO) != 0)
13276 ctl_io_error_print(io, NULL);
13279 * Tell the FETD or the other shelf controller we're done with this
13280 * command. Note that only SCSI commands get to this point. Task
13281 * management commands are completed above.
13283 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
13284 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) {
13285 memset(&msg, 0, sizeof(msg));
13286 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13287 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
13288 msg.hdr.nexus = io->io_hdr.nexus;
13289 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13290 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data),
13293 if ((softc->ha_mode == CTL_HA_MODE_XFER)
13294 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
13295 memset(&msg, 0, sizeof(msg));
13296 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13297 msg.hdr.original_sc = io->io_hdr.original_sc;
13298 msg.hdr.nexus = io->io_hdr.nexus;
13299 msg.hdr.status = io->io_hdr.status;
13300 msg.scsi.scsi_status = io->scsiio.scsi_status;
13301 msg.scsi.tag_num = io->scsiio.tag_num;
13302 msg.scsi.tag_type = io->scsiio.tag_type;
13303 msg.scsi.sense_len = io->scsiio.sense_len;
13304 msg.scsi.sense_residual = io->scsiio.sense_residual;
13305 msg.scsi.residual = io->scsiio.residual;
13306 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
13307 io->scsiio.sense_len);
13309 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13310 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
13311 msg.scsi.sense_len, M_WAITOK);
13316 return (CTL_RETVAL_COMPLETE);
13321 * Front end should call this if it doesn't do autosense. When the request
13322 * sense comes back in from the initiator, we'll dequeue this and send it.
13325 ctl_queue_sense(union ctl_io *io)
13327 struct ctl_lun *lun;
13328 struct ctl_port *port;
13329 struct ctl_softc *softc;
13330 uint32_t initidx, targ_lun;
13332 softc = control_softc;
13334 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13337 * LUN lookup will likely move to the ctl_work_thread() once we
13338 * have our new queueing infrastructure (that doesn't put things on
13339 * a per-LUN queue initially). That is so that we can handle
13340 * things like an INQUIRY to a LUN that we don't have enabled. We
13341 * can't deal with that right now.
13343 mtx_lock(&softc->ctl_lock);
13346 * If we don't have a LUN for this, just toss the sense
13349 port = ctl_io_port(&ctsio->io_hdr);
13350 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13351 if ((targ_lun < CTL_MAX_LUNS)
13352 && (softc->ctl_luns[targ_lun] != NULL))
13353 lun = softc->ctl_luns[targ_lun];
13357 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13359 mtx_lock(&lun->lun_lock);
13361 * Already have CA set for this LUN...toss the sense information.
13363 if (ctl_is_set(lun->have_ca, initidx)) {
13364 mtx_unlock(&lun->lun_lock);
13368 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data,
13369 MIN(sizeof(lun->pending_sense[initidx]),
13370 sizeof(io->scsiio.sense_data)));
13371 ctl_set_mask(lun->have_ca, initidx);
13372 mtx_unlock(&lun->lun_lock);
13375 mtx_unlock(&softc->ctl_lock);
13379 return (CTL_RETVAL_COMPLETE);
13384 * Primary command inlet from frontend ports. All SCSI and task I/O
13385 * requests must go through this function.
13388 ctl_queue(union ctl_io *io)
13390 struct ctl_port *port;
13392 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13395 io->io_hdr.start_time = time_uptime;
13396 getbintime(&io->io_hdr.start_bt);
13397 #endif /* CTL_TIME_IO */
13399 /* Map FE-specific LUN ID into global one. */
13400 port = ctl_io_port(&io->io_hdr);
13401 io->io_hdr.nexus.targ_mapped_lun =
13402 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13404 switch (io->io_hdr.io_type) {
13407 if (ctl_debug & CTL_DEBUG_CDB)
13409 ctl_enqueue_incoming(io);
13412 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13416 return (CTL_RETVAL_COMPLETE);
13419 #ifdef CTL_IO_DELAY
13421 ctl_done_timer_wakeup(void *arg)
13425 io = (union ctl_io *)arg;
13428 #endif /* CTL_IO_DELAY */
13431 ctl_serseq_done(union ctl_io *io)
13433 struct ctl_lun *lun;
13435 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13436 if (lun->be_lun == NULL ||
13437 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF)
13439 mtx_lock(&lun->lun_lock);
13440 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE;
13441 ctl_check_blocked(lun);
13442 mtx_unlock(&lun->lun_lock);
13446 ctl_done(union ctl_io *io)
13450 * Enable this to catch duplicate completion issues.
13453 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13454 printf("%s: type %d msg %d cdb %x iptl: "
13455 "%u:%u:%u tag 0x%04x "
13456 "flag %#x status %x\n",
13458 io->io_hdr.io_type,
13459 io->io_hdr.msg_type,
13461 io->io_hdr.nexus.initid,
13462 io->io_hdr.nexus.targ_port,
13463 io->io_hdr.nexus.targ_lun,
13464 (io->io_hdr.io_type ==
13466 io->taskio.tag_num :
13467 io->scsiio.tag_num,
13469 io->io_hdr.status);
13471 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13475 * This is an internal copy of an I/O, and should not go through
13476 * the normal done processing logic.
13478 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13481 #ifdef CTL_IO_DELAY
13482 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13483 struct ctl_lun *lun;
13485 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13487 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13489 struct ctl_lun *lun;
13491 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13494 && (lun->delay_info.done_delay > 0)) {
13496 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
13497 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13498 callout_reset(&io->io_hdr.delay_callout,
13499 lun->delay_info.done_delay * hz,
13500 ctl_done_timer_wakeup, io);
13501 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13502 lun->delay_info.done_delay = 0;
13506 #endif /* CTL_IO_DELAY */
13508 ctl_enqueue_done(io);
13512 ctl_work_thread(void *arg)
13514 struct ctl_thread *thr = (struct ctl_thread *)arg;
13515 struct ctl_softc *softc = thr->ctl_softc;
13519 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13525 * We handle the queues in this order:
13527 * - done queue (to free up resources, unblock other commands)
13531 * If those queues are empty, we break out of the loop and
13534 mtx_lock(&thr->queue_lock);
13535 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13537 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13538 mtx_unlock(&thr->queue_lock);
13539 ctl_handle_isc(io);
13542 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13544 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13545 /* clear any blocked commands, call fe_done */
13546 mtx_unlock(&thr->queue_lock);
13547 retval = ctl_process_done(io);
13550 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13552 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13553 mtx_unlock(&thr->queue_lock);
13554 if (io->io_hdr.io_type == CTL_IO_TASK)
13557 ctl_scsiio_precheck(softc, &io->scsiio);
13560 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13562 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13563 mtx_unlock(&thr->queue_lock);
13564 retval = ctl_scsiio(&io->scsiio);
13565 if (retval != CTL_RETVAL_COMPLETE)
13566 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13570 /* Sleep until we have something to do. */
13571 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13576 ctl_lun_thread(void *arg)
13578 struct ctl_softc *softc = (struct ctl_softc *)arg;
13579 struct ctl_be_lun *be_lun;
13582 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13586 mtx_lock(&softc->ctl_lock);
13587 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13588 if (be_lun != NULL) {
13589 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13590 mtx_unlock(&softc->ctl_lock);
13591 ctl_create_lun(be_lun);
13595 /* Sleep until we have something to do. */
13596 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13597 PDROP | PRIBIO, "-", 0);
13602 ctl_thresh_thread(void *arg)
13604 struct ctl_softc *softc = (struct ctl_softc *)arg;
13605 struct ctl_lun *lun;
13606 struct ctl_be_lun *be_lun;
13607 struct scsi_da_rw_recovery_page *rwpage;
13608 struct ctl_logical_block_provisioning_page *page;
13610 union ctl_ha_msg msg;
13611 uint64_t thres, val;
13614 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13617 mtx_lock(&softc->ctl_lock);
13618 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13619 be_lun = lun->be_lun;
13620 if ((lun->flags & CTL_LUN_DISABLED) ||
13621 (lun->flags & CTL_LUN_OFFLINE) ||
13622 lun->backend->lun_attr == NULL)
13624 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
13625 softc->ha_mode == CTL_HA_MODE_XFER)
13627 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT];
13628 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0)
13631 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT];
13632 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13633 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13635 thres = scsi_4btoul(page->descr[i].count);
13636 thres <<= CTL_LBP_EXPONENT;
13637 switch (page->descr[i].resource) {
13639 attr = "blocksavail";
13642 attr = "blocksused";
13645 attr = "poolblocksavail";
13648 attr = "poolblocksused";
13653 mtx_unlock(&softc->ctl_lock); // XXX
13654 val = lun->backend->lun_attr(
13655 lun->be_lun->be_lun, attr);
13656 mtx_lock(&softc->ctl_lock);
13657 if (val == UINT64_MAX)
13659 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13660 == SLBPPD_ARMING_INC)
13661 e = (val >= thres);
13663 e = (val <= thres);
13667 mtx_lock(&lun->lun_lock);
13669 scsi_u64to8b((uint8_t *)&page->descr[i] -
13670 (uint8_t *)page, lun->ua_tpt_info);
13671 if (lun->lasttpt == 0 ||
13672 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13673 lun->lasttpt = time_uptime;
13674 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13680 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13683 mtx_unlock(&lun->lun_lock);
13685 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
13686 /* Send msg to other side. */
13687 bzero(&msg.ua, sizeof(msg.ua));
13688 msg.hdr.msg_type = CTL_MSG_UA;
13689 msg.hdr.nexus.initid = -1;
13690 msg.hdr.nexus.targ_port = -1;
13691 msg.hdr.nexus.targ_lun = lun->lun;
13692 msg.hdr.nexus.targ_mapped_lun = lun->lun;
13694 msg.ua.ua_set = (set > 0);
13695 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES;
13696 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8);
13697 mtx_unlock(&softc->ctl_lock); // XXX
13698 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13699 sizeof(msg.ua), M_WAITOK);
13700 mtx_lock(&softc->ctl_lock);
13703 mtx_unlock(&softc->ctl_lock);
13704 pause("-", CTL_LBP_PERIOD * hz);
13709 ctl_enqueue_incoming(union ctl_io *io)
13711 struct ctl_softc *softc = control_softc;
13712 struct ctl_thread *thr;
13715 idx = (io->io_hdr.nexus.targ_port * 127 +
13716 io->io_hdr.nexus.initid) % worker_threads;
13717 thr = &softc->threads[idx];
13718 mtx_lock(&thr->queue_lock);
13719 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13720 mtx_unlock(&thr->queue_lock);
13725 ctl_enqueue_rtr(union ctl_io *io)
13727 struct ctl_softc *softc = control_softc;
13728 struct ctl_thread *thr;
13730 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13731 mtx_lock(&thr->queue_lock);
13732 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13733 mtx_unlock(&thr->queue_lock);
13738 ctl_enqueue_done(union ctl_io *io)
13740 struct ctl_softc *softc = control_softc;
13741 struct ctl_thread *thr;
13743 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13744 mtx_lock(&thr->queue_lock);
13745 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13746 mtx_unlock(&thr->queue_lock);
13751 ctl_enqueue_isc(union ctl_io *io)
13753 struct ctl_softc *softc = control_softc;
13754 struct ctl_thread *thr;
13756 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13757 mtx_lock(&thr->queue_lock);
13758 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13759 mtx_unlock(&thr->queue_lock);