2 * Copyright (c) 2003-2009 Silicon Graphics International Corp.
3 * Copyright (c) 2012 The FreeBSD Foundation
6 * Portions of this software were developed by Edward Tomasz Napierala
7 * under sponsorship from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
16 * substantially similar to the "NO WARRANTY" disclaimer below
17 * ("Disclaimer") and any redistribution must be conditioned upon
18 * including a substantially similar Disclaimer requirement for further
19 * binary redistribution.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGES.
34 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl.c#8 $
37 * CAM Target Layer, a SCSI device emulation subsystem.
39 * Author: Ken Merry <ken@FreeBSD.org>
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/ctype.h>
50 #include <sys/kernel.h>
51 #include <sys/types.h>
52 #include <sys/kthread.h>
54 #include <sys/fcntl.h>
56 #include <sys/module.h>
57 #include <sys/mutex.h>
58 #include <sys/condvar.h>
59 #include <sys/malloc.h>
61 #include <sys/ioccom.h>
62 #include <sys/queue.h>
65 #include <sys/endian.h>
66 #include <sys/sysctl.h>
69 #include <cam/scsi/scsi_all.h>
70 #include <cam/scsi/scsi_da.h>
71 #include <cam/ctl/ctl_io.h>
72 #include <cam/ctl/ctl.h>
73 #include <cam/ctl/ctl_frontend.h>
74 #include <cam/ctl/ctl_frontend_internal.h>
75 #include <cam/ctl/ctl_util.h>
76 #include <cam/ctl/ctl_backend.h>
77 #include <cam/ctl/ctl_ioctl.h>
78 #include <cam/ctl/ctl_ha.h>
79 #include <cam/ctl/ctl_private.h>
80 #include <cam/ctl/ctl_debug.h>
81 #include <cam/ctl/ctl_scsi_all.h>
82 #include <cam/ctl/ctl_error.h>
84 struct ctl_softc *control_softc = NULL;
87 * Size and alignment macros needed for Copan-specific HA hardware. These
88 * can go away when the HA code is re-written, and uses busdma for any
91 #define CTL_ALIGN_8B(target, source, type) \
92 if (((uint32_t)source & 0x7) != 0) \
93 target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\
95 target = (type)source;
97 #define CTL_SIZE_8B(target, size) \
98 if ((size & 0x7) != 0) \
99 target = size + (0x8 - (size & 0x7)); \
103 #define CTL_ALIGN_8B_MARGIN 16
106 * Template mode pages.
110 * Note that these are default values only. The actual values will be
111 * filled in when the user does a mode sense.
113 static struct copan_debugconf_subpage debugconf_page_default = {
114 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
115 DBGCNF_SUBPAGE_CODE, /* subpage */
116 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
117 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
118 DBGCNF_VERSION, /* page_version */
119 {CTL_TIME_IO_DEFAULT_SECS>>8,
120 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */
123 static struct copan_debugconf_subpage debugconf_page_changeable = {
124 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
125 DBGCNF_SUBPAGE_CODE, /* subpage */
126 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
127 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
128 0, /* page_version */
129 {0xff,0xff}, /* ctl_time_io_secs */
132 static struct scsi_da_rw_recovery_page rw_er_page_default = {
133 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
134 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
135 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE,
136 /*read_retry_count*/0,
137 /*correction_span*/0,
138 /*head_offset_count*/0,
139 /*data_strobe_offset_cnt*/0,
140 /*byte8*/SMS_RWER_LBPERE,
141 /*write_retry_count*/0,
143 /*recovery_time_limit*/{0, 0},
146 static struct scsi_da_rw_recovery_page rw_er_page_changeable = {
147 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
148 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
150 /*read_retry_count*/0,
151 /*correction_span*/0,
152 /*head_offset_count*/0,
153 /*data_strobe_offset_cnt*/0,
155 /*write_retry_count*/0,
157 /*recovery_time_limit*/{0, 0},
160 static struct scsi_format_page format_page_default = {
161 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
162 /*page_length*/sizeof(struct scsi_format_page) - 2,
163 /*tracks_per_zone*/ {0, 0},
164 /*alt_sectors_per_zone*/ {0, 0},
165 /*alt_tracks_per_zone*/ {0, 0},
166 /*alt_tracks_per_lun*/ {0, 0},
167 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
168 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
169 /*bytes_per_sector*/ {0, 0},
170 /*interleave*/ {0, 0},
171 /*track_skew*/ {0, 0},
172 /*cylinder_skew*/ {0, 0},
174 /*reserved*/ {0, 0, 0}
177 static struct scsi_format_page format_page_changeable = {
178 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
179 /*page_length*/sizeof(struct scsi_format_page) - 2,
180 /*tracks_per_zone*/ {0, 0},
181 /*alt_sectors_per_zone*/ {0, 0},
182 /*alt_tracks_per_zone*/ {0, 0},
183 /*alt_tracks_per_lun*/ {0, 0},
184 /*sectors_per_track*/ {0, 0},
185 /*bytes_per_sector*/ {0, 0},
186 /*interleave*/ {0, 0},
187 /*track_skew*/ {0, 0},
188 /*cylinder_skew*/ {0, 0},
190 /*reserved*/ {0, 0, 0}
193 static struct scsi_rigid_disk_page rigid_disk_page_default = {
194 /*page_code*/SMS_RIGID_DISK_PAGE,
195 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
196 /*cylinders*/ {0, 0, 0},
197 /*heads*/ CTL_DEFAULT_HEADS,
198 /*start_write_precomp*/ {0, 0, 0},
199 /*start_reduced_current*/ {0, 0, 0},
200 /*step_rate*/ {0, 0},
201 /*landing_zone_cylinder*/ {0, 0, 0},
202 /*rpl*/ SRDP_RPL_DISABLED,
203 /*rotational_offset*/ 0,
205 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
206 CTL_DEFAULT_ROTATION_RATE & 0xff},
210 static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
211 /*page_code*/SMS_RIGID_DISK_PAGE,
212 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
213 /*cylinders*/ {0, 0, 0},
215 /*start_write_precomp*/ {0, 0, 0},
216 /*start_reduced_current*/ {0, 0, 0},
217 /*step_rate*/ {0, 0},
218 /*landing_zone_cylinder*/ {0, 0, 0},
220 /*rotational_offset*/ 0,
222 /*rotation_rate*/ {0, 0},
226 static struct scsi_caching_page caching_page_default = {
227 /*page_code*/SMS_CACHING_PAGE,
228 /*page_length*/sizeof(struct scsi_caching_page) - 2,
229 /*flags1*/ SCP_DISC | SCP_WCE,
231 /*disable_pf_transfer_len*/ {0xff, 0xff},
232 /*min_prefetch*/ {0, 0},
233 /*max_prefetch*/ {0xff, 0xff},
234 /*max_pf_ceiling*/ {0xff, 0xff},
236 /*cache_segments*/ 0,
237 /*cache_seg_size*/ {0, 0},
239 /*non_cache_seg_size*/ {0, 0, 0}
242 static struct scsi_caching_page caching_page_changeable = {
243 /*page_code*/SMS_CACHING_PAGE,
244 /*page_length*/sizeof(struct scsi_caching_page) - 2,
245 /*flags1*/ SCP_WCE | SCP_RCD,
247 /*disable_pf_transfer_len*/ {0, 0},
248 /*min_prefetch*/ {0, 0},
249 /*max_prefetch*/ {0, 0},
250 /*max_pf_ceiling*/ {0, 0},
252 /*cache_segments*/ 0,
253 /*cache_seg_size*/ {0, 0},
255 /*non_cache_seg_size*/ {0, 0, 0}
258 static struct scsi_control_page control_page_default = {
259 /*page_code*/SMS_CONTROL_MODE_PAGE,
260 /*page_length*/sizeof(struct scsi_control_page) - 2,
262 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED,
265 /*aen_holdoff_period*/{0, 0},
266 /*busy_timeout_period*/{0, 0},
267 /*extended_selftest_completion_time*/{0, 0}
270 static struct scsi_control_page control_page_changeable = {
271 /*page_code*/SMS_CONTROL_MODE_PAGE,
272 /*page_length*/sizeof(struct scsi_control_page) - 2,
274 /*queue_flags*/SCP_QUEUE_ALG_MASK,
275 /*eca_and_aen*/SCP_SWP,
277 /*aen_holdoff_period*/{0, 0},
278 /*busy_timeout_period*/{0, 0},
279 /*extended_selftest_completion_time*/{0, 0}
282 static struct scsi_info_exceptions_page ie_page_default = {
283 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
284 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
285 /*info_flags*/SIEP_FLAGS_DEXCPT,
287 /*interval_timer*/{0, 0, 0, 0},
288 /*report_count*/{0, 0, 0, 0}
291 static struct scsi_info_exceptions_page ie_page_changeable = {
292 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
293 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
296 /*interval_timer*/{0, 0, 0, 0},
297 /*report_count*/{0, 0, 0, 0}
300 #define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4)
302 static struct ctl_logical_block_provisioning_page lbp_page_default = {{
303 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
304 /*subpage_code*/0x02,
305 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
307 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
312 /*count*/{0, 0, 0, 0}},
316 /*count*/{0, 0, 0, 0}},
320 /*count*/{0, 0, 0, 0}},
324 /*count*/{0, 0, 0, 0}}
328 static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{
329 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
330 /*subpage_code*/0x02,
331 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
333 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
338 /*count*/{0, 0, 0, 0}},
342 /*count*/{0, 0, 0, 0}},
346 /*count*/{0, 0, 0, 0}},
350 /*count*/{0, 0, 0, 0}}
355 * XXX KDM move these into the softc.
357 static int rcv_sync_msg;
358 static int persis_offset;
359 static uint8_t ctl_pause_rtr;
361 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
362 static int worker_threads = -1;
363 TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads);
364 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
365 &worker_threads, 1, "Number of worker threads");
366 static int ctl_debug = CTL_DEBUG_NONE;
367 TUNABLE_INT("kern.cam.ctl.debug", &ctl_debug);
368 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN,
369 &ctl_debug, 0, "Enabled debug flags");
372 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
373 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
374 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0),
375 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
377 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 10
379 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
381 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
382 static int ctl_init(void);
383 void ctl_shutdown(void);
384 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
385 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
386 static void ctl_ioctl_online(void *arg);
387 static void ctl_ioctl_offline(void *arg);
388 static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id);
389 static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id);
390 static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio);
391 static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
392 static int ctl_ioctl_submit_wait(union ctl_io *io);
393 static void ctl_ioctl_datamove(union ctl_io *io);
394 static void ctl_ioctl_done(union ctl_io *io);
395 static void ctl_ioctl_hard_startstop_callback(void *arg,
396 struct cfi_metatask *metatask);
397 static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask);
398 static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
399 struct ctl_ooa *ooa_hdr,
400 struct ctl_ooa_entry *kern_entries);
401 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
403 static uint32_t ctl_map_lun(int port_num, uint32_t lun);
404 static uint32_t ctl_map_lun_back(int port_num, uint32_t lun);
406 static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port,
407 uint32_t targ_target, uint32_t targ_lun,
409 static void ctl_kfree_io(union ctl_io *io);
411 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
412 struct ctl_be_lun *be_lun, struct ctl_id target_id);
413 static int ctl_free_lun(struct ctl_lun *lun);
414 static void ctl_create_lun(struct ctl_be_lun *be_lun);
416 static void ctl_failover_change_pages(struct ctl_softc *softc,
417 struct ctl_scsiio *ctsio, int master);
420 static int ctl_do_mode_select(union ctl_io *io);
421 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
422 uint64_t res_key, uint64_t sa_res_key,
423 uint8_t type, uint32_t residx,
424 struct ctl_scsiio *ctsio,
425 struct scsi_per_res_out *cdb,
426 struct scsi_per_res_out_parms* param);
427 static void ctl_pro_preempt_other(struct ctl_lun *lun,
428 union ctl_ha_msg *msg);
429 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
430 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
431 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
432 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
433 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
434 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
435 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
437 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
439 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
440 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
441 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
442 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
443 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
444 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2);
445 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
446 union ctl_io *pending_io, union ctl_io *ooa_io);
447 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
448 union ctl_io *starting_io);
449 static int ctl_check_blocked(struct ctl_lun *lun);
450 static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc,
452 const struct ctl_cmd_entry *entry,
453 struct ctl_scsiio *ctsio);
454 //static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc);
455 static void ctl_failover(void);
456 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
457 struct ctl_scsiio *ctsio);
458 static int ctl_scsiio(struct ctl_scsiio *ctsio);
460 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
461 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
462 ctl_ua_type ua_type);
463 static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io,
464 ctl_ua_type ua_type);
465 static int ctl_abort_task(union ctl_io *io);
466 static int ctl_abort_task_set(union ctl_io *io);
467 static int ctl_i_t_nexus_reset(union ctl_io *io);
468 static void ctl_run_task(union ctl_io *io);
470 static void ctl_datamove_timer_wakeup(void *arg);
471 static void ctl_done_timer_wakeup(void *arg);
472 #endif /* CTL_IO_DELAY */
474 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
475 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
476 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
477 static void ctl_datamove_remote_write(union ctl_io *io);
478 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
479 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
480 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
481 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
482 ctl_ha_dt_cb callback);
483 static void ctl_datamove_remote_read(union ctl_io *io);
484 static void ctl_datamove_remote(union ctl_io *io);
485 static int ctl_process_done(union ctl_io *io);
486 static void ctl_lun_thread(void *arg);
487 static void ctl_thresh_thread(void *arg);
488 static void ctl_work_thread(void *arg);
489 static void ctl_enqueue_incoming(union ctl_io *io);
490 static void ctl_enqueue_rtr(union ctl_io *io);
491 static void ctl_enqueue_done(union ctl_io *io);
492 static void ctl_enqueue_isc(union ctl_io *io);
493 static const struct ctl_cmd_entry *
494 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
495 static const struct ctl_cmd_entry *
496 ctl_validate_command(struct ctl_scsiio *ctsio);
497 static int ctl_cmd_applicable(uint8_t lun_type,
498 const struct ctl_cmd_entry *entry);
501 * Load the serialization table. This isn't very pretty, but is probably
502 * the easiest way to do it.
504 #include "ctl_ser_table.c"
507 * We only need to define open, close and ioctl routines for this driver.
509 static struct cdevsw ctl_cdevsw = {
510 .d_version = D_VERSION,
513 .d_close = ctl_close,
514 .d_ioctl = ctl_ioctl,
519 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
520 MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests");
522 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
524 static moduledata_t ctl_moduledata = {
526 ctl_module_event_handler,
530 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
531 MODULE_VERSION(ctl, 1);
533 static struct ctl_frontend ioctl_frontend =
539 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
540 union ctl_ha_msg *msg_info)
542 struct ctl_scsiio *ctsio;
544 if (msg_info->hdr.original_sc == NULL) {
545 printf("%s: original_sc == NULL!\n", __func__);
546 /* XXX KDM now what? */
550 ctsio = &msg_info->hdr.original_sc->scsiio;
551 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
552 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
553 ctsio->io_hdr.status = msg_info->hdr.status;
554 ctsio->scsi_status = msg_info->scsi.scsi_status;
555 ctsio->sense_len = msg_info->scsi.sense_len;
556 ctsio->sense_residual = msg_info->scsi.sense_residual;
557 ctsio->residual = msg_info->scsi.residual;
558 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
559 sizeof(ctsio->sense_data));
560 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
561 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen));
562 ctl_enqueue_isc((union ctl_io *)ctsio);
566 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
567 union ctl_ha_msg *msg_info)
569 struct ctl_scsiio *ctsio;
571 if (msg_info->hdr.serializing_sc == NULL) {
572 printf("%s: serializing_sc == NULL!\n", __func__);
573 /* XXX KDM now what? */
577 ctsio = &msg_info->hdr.serializing_sc->scsiio;
580 * Attempt to catch the situation where an I/O has
581 * been freed, and we're using it again.
583 if (ctsio->io_hdr.io_type == 0xff) {
584 union ctl_io *tmp_io;
585 tmp_io = (union ctl_io *)ctsio;
586 printf("%s: %p use after free!\n", __func__,
588 printf("%s: type %d msg %d cdb %x iptl: "
589 "%d:%d:%d:%d tag 0x%04x "
590 "flag %#x status %x\n",
592 tmp_io->io_hdr.io_type,
593 tmp_io->io_hdr.msg_type,
594 tmp_io->scsiio.cdb[0],
595 tmp_io->io_hdr.nexus.initid.id,
596 tmp_io->io_hdr.nexus.targ_port,
597 tmp_io->io_hdr.nexus.targ_target.id,
598 tmp_io->io_hdr.nexus.targ_lun,
599 (tmp_io->io_hdr.io_type ==
601 tmp_io->taskio.tag_num :
602 tmp_io->scsiio.tag_num,
603 tmp_io->io_hdr.flags,
604 tmp_io->io_hdr.status);
607 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
608 ctl_enqueue_isc((union ctl_io *)ctsio);
612 * ISC (Inter Shelf Communication) event handler. Events from the HA
613 * subsystem come in here.
616 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
618 struct ctl_softc *ctl_softc;
620 struct ctl_prio *presio;
621 ctl_ha_status isc_status;
623 ctl_softc = control_softc;
628 printf("CTL: Isc Msg event %d\n", event);
630 if (event == CTL_HA_EVT_MSG_RECV) {
631 union ctl_ha_msg msg_info;
633 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
634 sizeof(msg_info), /*wait*/ 0);
636 printf("CTL: msg_type %d\n", msg_info.msg_type);
638 if (isc_status != 0) {
639 printf("Error receiving message, status = %d\n",
644 switch (msg_info.hdr.msg_type) {
645 case CTL_MSG_SERIALIZE:
647 printf("Serialize\n");
649 io = ctl_alloc_io((void *)ctl_softc->othersc_pool);
651 printf("ctl_isc_event_handler: can't allocate "
654 /* Need to set busy and send msg back */
655 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
656 msg_info.hdr.status = CTL_SCSI_ERROR;
657 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY;
658 msg_info.scsi.sense_len = 0;
659 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
660 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){
665 // populate ctsio from msg_info
666 io->io_hdr.io_type = CTL_IO_SCSI;
667 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
668 io->io_hdr.original_sc = msg_info.hdr.original_sc;
670 printf("pOrig %x\n", (int)msg_info.original_sc);
672 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
675 * If we're in serialization-only mode, we don't
676 * want to go through full done processing. Thus
679 * XXX KDM add another flag that is more specific.
681 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)
682 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
683 io->io_hdr.nexus = msg_info.hdr.nexus;
685 printf("targ %d, port %d, iid %d, lun %d\n",
686 io->io_hdr.nexus.targ_target.id,
687 io->io_hdr.nexus.targ_port,
688 io->io_hdr.nexus.initid.id,
689 io->io_hdr.nexus.targ_lun);
691 io->scsiio.tag_num = msg_info.scsi.tag_num;
692 io->scsiio.tag_type = msg_info.scsi.tag_type;
693 memcpy(io->scsiio.cdb, msg_info.scsi.cdb,
695 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
696 const struct ctl_cmd_entry *entry;
698 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
699 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
701 entry->flags & CTL_FLAG_DATA_MASK;
706 /* Performed on the Originating SC, XFER mode only */
707 case CTL_MSG_DATAMOVE: {
708 struct ctl_sg_entry *sgl;
711 io = msg_info.hdr.original_sc;
713 printf("%s: original_sc == NULL!\n", __func__);
714 /* XXX KDM do something here */
717 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
718 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
720 * Keep track of this, we need to send it back over
721 * when the datamove is complete.
723 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
725 if (msg_info.dt.sg_sequence == 0) {
727 * XXX KDM we use the preallocated S/G list
728 * here, but we'll need to change this to
729 * dynamic allocation if we need larger S/G
732 if (msg_info.dt.kern_sg_entries >
733 sizeof(io->io_hdr.remote_sglist) /
734 sizeof(io->io_hdr.remote_sglist[0])) {
735 printf("%s: number of S/G entries "
736 "needed %u > allocated num %zd\n",
738 msg_info.dt.kern_sg_entries,
739 sizeof(io->io_hdr.remote_sglist)/
740 sizeof(io->io_hdr.remote_sglist[0]));
743 * XXX KDM send a message back to
744 * the other side to shut down the
745 * DMA. The error will come back
746 * through via the normal channel.
750 sgl = io->io_hdr.remote_sglist;
752 sizeof(io->io_hdr.remote_sglist));
754 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
756 io->scsiio.kern_sg_entries =
757 msg_info.dt.kern_sg_entries;
758 io->scsiio.rem_sg_entries =
759 msg_info.dt.kern_sg_entries;
760 io->scsiio.kern_data_len =
761 msg_info.dt.kern_data_len;
762 io->scsiio.kern_total_len =
763 msg_info.dt.kern_total_len;
764 io->scsiio.kern_data_resid =
765 msg_info.dt.kern_data_resid;
766 io->scsiio.kern_rel_offset =
767 msg_info.dt.kern_rel_offset;
769 * Clear out per-DMA flags.
771 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK;
773 * Add per-DMA flags that are set for this
774 * particular DMA request.
776 io->io_hdr.flags |= msg_info.dt.flags &
779 sgl = (struct ctl_sg_entry *)
780 io->scsiio.kern_data_ptr;
782 for (i = msg_info.dt.sent_sg_entries, j = 0;
783 i < (msg_info.dt.sent_sg_entries +
784 msg_info.dt.cur_sg_entries); i++, j++) {
785 sgl[i].addr = msg_info.dt.sg_list[j].addr;
786 sgl[i].len = msg_info.dt.sg_list[j].len;
789 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n",
791 msg_info.dt.sg_list[j].addr,
792 msg_info.dt.sg_list[j].len,
793 sgl[i].addr, sgl[i].len, j, i);
797 memcpy(&sgl[msg_info.dt.sent_sg_entries],
799 sizeof(*sgl) * msg_info.dt.cur_sg_entries);
803 * If this is the last piece of the I/O, we've got
804 * the full S/G list. Queue processing in the thread.
805 * Otherwise wait for the next piece.
807 if (msg_info.dt.sg_last != 0)
811 /* Performed on the Serializing (primary) SC, XFER mode only */
812 case CTL_MSG_DATAMOVE_DONE: {
813 if (msg_info.hdr.serializing_sc == NULL) {
814 printf("%s: serializing_sc == NULL!\n",
816 /* XXX KDM now what? */
820 * We grab the sense information here in case
821 * there was a failure, so we can return status
822 * back to the initiator.
824 io = msg_info.hdr.serializing_sc;
825 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
826 io->io_hdr.status = msg_info.hdr.status;
827 io->scsiio.scsi_status = msg_info.scsi.scsi_status;
828 io->scsiio.sense_len = msg_info.scsi.sense_len;
829 io->scsiio.sense_residual =msg_info.scsi.sense_residual;
830 io->io_hdr.port_status = msg_info.scsi.fetd_status;
831 io->scsiio.residual = msg_info.scsi.residual;
832 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data,
833 sizeof(io->scsiio.sense_data));
838 /* Preformed on Originating SC, SER_ONLY mode */
840 io = msg_info.hdr.original_sc;
842 printf("%s: Major Bummer\n", __func__);
846 printf("pOrig %x\n",(int) ctsio);
849 io->io_hdr.msg_type = CTL_MSG_R2R;
850 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
855 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
857 * Performed on the Originating (i.e. secondary) SC in XFER
860 case CTL_MSG_FINISH_IO:
861 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER)
862 ctl_isc_handler_finish_xfer(ctl_softc,
865 ctl_isc_handler_finish_ser_only(ctl_softc,
869 /* Preformed on Originating SC */
870 case CTL_MSG_BAD_JUJU:
871 io = msg_info.hdr.original_sc;
873 printf("%s: Bad JUJU!, original_sc is NULL!\n",
877 ctl_copy_sense_data(&msg_info, io);
879 * IO should have already been cleaned up on other
880 * SC so clear this flag so we won't send a message
881 * back to finish the IO there.
883 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
884 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
886 /* io = msg_info.hdr.serializing_sc; */
887 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
891 /* Handle resets sent from the other side */
892 case CTL_MSG_MANAGE_TASKS: {
893 struct ctl_taskio *taskio;
894 taskio = (struct ctl_taskio *)ctl_alloc_io(
895 (void *)ctl_softc->othersc_pool);
896 if (taskio == NULL) {
897 printf("ctl_isc_event_handler: can't allocate "
900 /* should I just call the proper reset func
904 ctl_zero_io((union ctl_io *)taskio);
905 taskio->io_hdr.io_type = CTL_IO_TASK;
906 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
907 taskio->io_hdr.nexus = msg_info.hdr.nexus;
908 taskio->task_action = msg_info.task.task_action;
909 taskio->tag_num = msg_info.task.tag_num;
910 taskio->tag_type = msg_info.task.tag_type;
912 taskio->io_hdr.start_time = time_uptime;
913 getbintime(&taskio->io_hdr.start_bt);
915 cs_prof_gettime(&taskio->io_hdr.start_ticks);
917 #endif /* CTL_TIME_IO */
918 ctl_run_task((union ctl_io *)taskio);
921 /* Persistent Reserve action which needs attention */
922 case CTL_MSG_PERS_ACTION:
923 presio = (struct ctl_prio *)ctl_alloc_io(
924 (void *)ctl_softc->othersc_pool);
925 if (presio == NULL) {
926 printf("ctl_isc_event_handler: can't allocate "
929 /* Need to set busy and send msg back */
932 ctl_zero_io((union ctl_io *)presio);
933 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
934 presio->pr_msg = msg_info.pr;
935 ctl_enqueue_isc((union ctl_io *)presio);
937 case CTL_MSG_SYNC_FE:
941 printf("How did I get here?\n");
943 } else if (event == CTL_HA_EVT_MSG_SENT) {
944 if (param != CTL_HA_STATUS_SUCCESS) {
945 printf("Bad status from ctl_ha_msg_send status %d\n",
949 } else if (event == CTL_HA_EVT_DISCONNECT) {
950 printf("CTL: Got a disconnect from Isc\n");
953 printf("ctl_isc_event_handler: Unknown event %d\n", event);
962 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
964 struct scsi_sense_data *sense;
966 sense = &dest->scsiio.sense_data;
967 bcopy(&src->scsi.sense_data, sense, sizeof(*sense));
968 dest->scsiio.scsi_status = src->scsi.scsi_status;
969 dest->scsiio.sense_len = src->scsi.sense_len;
970 dest->io_hdr.status = src->hdr.status;
974 ctl_ha_state_sysctl(SYSCTL_HANDLER_ARGS)
976 struct ctl_softc *softc = (struct ctl_softc *)arg1;
980 if (softc->flags & CTL_FLAG_ACTIVE_SHELF)
985 error = sysctl_handle_int(oidp, &value, 0, req);
986 if ((error != 0) || (req->newptr == NULL))
989 mtx_lock(&softc->ctl_lock);
991 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
993 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
994 STAILQ_FOREACH(lun, &softc->lun_list, links) {
995 mtx_lock(&lun->lun_lock);
996 for (i = 0; i < CTL_MAX_INITIATORS; i++)
997 lun->pending_ua[i] |= CTL_UA_ASYM_ACC_CHANGE;
998 mtx_unlock(&lun->lun_lock);
1000 mtx_unlock(&softc->ctl_lock);
1007 struct ctl_softc *softc;
1008 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool;
1009 struct ctl_port *port;
1010 int i, error, retval;
1017 control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1019 softc = control_softc;
1021 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
1024 softc->dev->si_drv1 = softc;
1027 * By default, return a "bad LUN" peripheral qualifier for unknown
1028 * LUNs. The user can override this default using the tunable or
1029 * sysctl. See the comment in ctl_inquiry_std() for more details.
1031 softc->inquiry_pq_no_lun = 1;
1032 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun",
1033 &softc->inquiry_pq_no_lun);
1034 sysctl_ctx_init(&softc->sysctl_ctx);
1035 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1036 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1037 CTLFLAG_RD, 0, "CAM Target Layer");
1039 if (softc->sysctl_tree == NULL) {
1040 printf("%s: unable to allocate sysctl tree\n", __func__);
1041 destroy_dev(softc->dev);
1042 free(control_softc, M_DEVBUF);
1043 control_softc = NULL;
1047 SYSCTL_ADD_INT(&softc->sysctl_ctx,
1048 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
1049 "inquiry_pq_no_lun", CTLFLAG_RW,
1050 &softc->inquiry_pq_no_lun, 0,
1051 "Report no lun possible for invalid LUNs");
1053 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1054 mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF);
1055 softc->open_count = 0;
1058 * Default to actually sending a SYNCHRONIZE CACHE command down to
1061 softc->flags = CTL_FLAG_REAL_SYNC;
1064 * In Copan's HA scheme, the "master" and "slave" roles are
1065 * figured out through the slot the controller is in. Although it
1066 * is an active/active system, someone has to be in charge.
1068 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1069 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1070 "HA head ID (0 - no HA)");
1071 if (softc->ha_id == 0) {
1072 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1073 softc->is_single = 1;
1074 softc->port_offset = 0;
1076 softc->port_offset = (softc->ha_id - 1) * CTL_MAX_PORTS;
1077 persis_offset = softc->port_offset * CTL_MAX_INIT_PER_PORT;
1080 * XXX KDM need to figure out where we want to get our target ID
1081 * and WWID. Is it different on each port?
1083 softc->target.id = 0;
1084 softc->target.wwid[0] = 0x12345678;
1085 softc->target.wwid[1] = 0x87654321;
1086 STAILQ_INIT(&softc->lun_list);
1087 STAILQ_INIT(&softc->pending_lun_queue);
1088 STAILQ_INIT(&softc->fe_list);
1089 STAILQ_INIT(&softc->port_list);
1090 STAILQ_INIT(&softc->be_list);
1091 STAILQ_INIT(&softc->io_pools);
1092 ctl_tpc_init(softc);
1094 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL,
1095 &internal_pool)!= 0){
1096 printf("ctl: can't allocate %d entry internal pool, "
1097 "exiting\n", CTL_POOL_ENTRIES_INTERNAL);
1101 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY,
1102 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) {
1103 printf("ctl: can't allocate %d entry emergency pool, "
1104 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY);
1105 ctl_pool_free(internal_pool);
1109 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC,
1112 printf("ctl: can't allocate %d entry other SC pool, "
1113 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1114 ctl_pool_free(internal_pool);
1115 ctl_pool_free(emergency_pool);
1119 softc->internal_pool = internal_pool;
1120 softc->emergency_pool = emergency_pool;
1121 softc->othersc_pool = other_pool;
1123 if (worker_threads <= 0)
1124 worker_threads = max(1, mp_ncpus / 4);
1125 if (worker_threads > CTL_MAX_THREADS)
1126 worker_threads = CTL_MAX_THREADS;
1128 for (i = 0; i < worker_threads; i++) {
1129 struct ctl_thread *thr = &softc->threads[i];
1131 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1132 thr->ctl_softc = softc;
1133 STAILQ_INIT(&thr->incoming_queue);
1134 STAILQ_INIT(&thr->rtr_queue);
1135 STAILQ_INIT(&thr->done_queue);
1136 STAILQ_INIT(&thr->isc_queue);
1138 error = kproc_kthread_add(ctl_work_thread, thr,
1139 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1141 printf("error creating CTL work thread!\n");
1142 ctl_pool_free(internal_pool);
1143 ctl_pool_free(emergency_pool);
1144 ctl_pool_free(other_pool);
1148 error = kproc_kthread_add(ctl_lun_thread, softc,
1149 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
1151 printf("error creating CTL lun thread!\n");
1152 ctl_pool_free(internal_pool);
1153 ctl_pool_free(emergency_pool);
1154 ctl_pool_free(other_pool);
1157 error = kproc_kthread_add(ctl_thresh_thread, softc,
1158 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh");
1160 printf("error creating CTL threshold thread!\n");
1161 ctl_pool_free(internal_pool);
1162 ctl_pool_free(emergency_pool);
1163 ctl_pool_free(other_pool);
1167 printf("ctl: CAM Target Layer loaded\n");
1170 * Initialize the ioctl front end.
1172 ctl_frontend_register(&ioctl_frontend);
1173 port = &softc->ioctl_info.port;
1174 port->frontend = &ioctl_frontend;
1175 sprintf(softc->ioctl_info.port_name, "ioctl");
1176 port->port_type = CTL_PORT_IOCTL;
1177 port->num_requested_ctl_io = 100;
1178 port->port_name = softc->ioctl_info.port_name;
1179 port->port_online = ctl_ioctl_online;
1180 port->port_offline = ctl_ioctl_offline;
1181 port->onoff_arg = &softc->ioctl_info;
1182 port->lun_enable = ctl_ioctl_lun_enable;
1183 port->lun_disable = ctl_ioctl_lun_disable;
1184 port->targ_lun_arg = &softc->ioctl_info;
1185 port->fe_datamove = ctl_ioctl_datamove;
1186 port->fe_done = ctl_ioctl_done;
1187 port->max_targets = 15;
1188 port->max_target_id = 15;
1190 if (ctl_port_register(&softc->ioctl_info.port) != 0) {
1191 printf("ctl: ioctl front end registration failed, will "
1192 "continue anyway\n");
1195 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1196 OID_AUTO, "ha_state", CTLTYPE_INT | CTLFLAG_RWTUN,
1197 softc, 0, ctl_ha_state_sysctl, "I", "HA state for this head");
1200 if (sizeof(struct callout) > CTL_TIMER_BYTES) {
1201 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n",
1202 sizeof(struct callout), CTL_TIMER_BYTES);
1205 #endif /* CTL_IO_DELAY */
1213 struct ctl_softc *softc;
1214 struct ctl_lun *lun, *next_lun;
1215 struct ctl_io_pool *pool;
1217 softc = (struct ctl_softc *)control_softc;
1219 if (ctl_port_deregister(&softc->ioctl_info.port) != 0)
1220 printf("ctl: ioctl front end deregistration failed\n");
1222 mtx_lock(&softc->ctl_lock);
1227 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
1228 next_lun = STAILQ_NEXT(lun, links);
1232 mtx_unlock(&softc->ctl_lock);
1234 ctl_frontend_deregister(&ioctl_frontend);
1237 * This will rip the rug out from under any FETDs or anyone else
1238 * that has a pool allocated. Since we increment our module
1239 * refcount any time someone outside the main CTL module allocates
1240 * a pool, we shouldn't have any problems here. The user won't be
1241 * able to unload the CTL module until client modules have
1242 * successfully unloaded.
1244 while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL)
1245 ctl_pool_free(pool);
1248 ctl_shutdown_thread(softc->work_thread);
1249 mtx_destroy(&softc->queue_lock);
1252 ctl_tpc_shutdown(softc);
1253 mtx_destroy(&softc->pool_lock);
1254 mtx_destroy(&softc->ctl_lock);
1256 destroy_dev(softc->dev);
1258 sysctl_ctx_free(&softc->sysctl_ctx);
1260 free(control_softc, M_DEVBUF);
1261 control_softc = NULL;
1264 printf("ctl: CAM Target Layer unloaded\n");
1268 ctl_module_event_handler(module_t mod, int what, void *arg)
1273 return (ctl_init());
1277 return (EOPNOTSUPP);
1282 * XXX KDM should we do some access checks here? Bump a reference count to
1283 * prevent a CTL module from being unloaded while someone has it open?
1286 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1292 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1298 ctl_port_enable(ctl_port_type port_type)
1300 struct ctl_softc *softc = control_softc;
1301 struct ctl_port *port;
1303 if (softc->is_single == 0) {
1304 union ctl_ha_msg msg_info;
1308 printf("%s: HA mode, synchronizing frontend enable\n",
1311 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE;
1312 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1313 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) {
1314 printf("Sync msg send error retval %d\n", isc_retval);
1316 if (!rcv_sync_msg) {
1317 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
1318 sizeof(msg_info), 1);
1321 printf("CTL:Frontend Enable\n");
1323 printf("%s: single mode, skipping frontend synchronization\n",
1328 STAILQ_FOREACH(port, &softc->port_list, links) {
1329 if (port_type & port->port_type)
1332 printf("port %d\n", port->targ_port);
1334 ctl_port_online(port);
1342 ctl_port_disable(ctl_port_type port_type)
1344 struct ctl_softc *softc;
1345 struct ctl_port *port;
1347 softc = control_softc;
1349 STAILQ_FOREACH(port, &softc->port_list, links) {
1350 if (port_type & port->port_type)
1351 ctl_port_offline(port);
1358 * Returns 0 for success, 1 for failure.
1359 * Currently the only failure mode is if there aren't enough entries
1360 * allocated. So, in case of a failure, look at num_entries_dropped,
1361 * reallocate and try again.
1364 ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced,
1365 int *num_entries_filled, int *num_entries_dropped,
1366 ctl_port_type port_type, int no_virtual)
1368 struct ctl_softc *softc;
1369 struct ctl_port *port;
1370 int entries_dropped, entries_filled;
1374 softc = control_softc;
1378 entries_dropped = 0;
1381 mtx_lock(&softc->ctl_lock);
1382 STAILQ_FOREACH(port, &softc->port_list, links) {
1383 struct ctl_port_entry *entry;
1385 if ((port->port_type & port_type) == 0)
1388 if ((no_virtual != 0)
1389 && (port->virtual_port != 0))
1392 if (entries_filled >= num_entries_alloced) {
1396 entry = &entries[i];
1398 entry->port_type = port->port_type;
1399 strlcpy(entry->port_name, port->port_name,
1400 sizeof(entry->port_name));
1401 entry->physical_port = port->physical_port;
1402 entry->virtual_port = port->virtual_port;
1403 entry->wwnn = port->wwnn;
1404 entry->wwpn = port->wwpn;
1410 mtx_unlock(&softc->ctl_lock);
1412 if (entries_dropped > 0)
1415 *num_entries_dropped = entries_dropped;
1416 *num_entries_filled = entries_filled;
1422 ctl_ioctl_online(void *arg)
1424 struct ctl_ioctl_info *ioctl_info;
1426 ioctl_info = (struct ctl_ioctl_info *)arg;
1428 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED;
1432 ctl_ioctl_offline(void *arg)
1434 struct ctl_ioctl_info *ioctl_info;
1436 ioctl_info = (struct ctl_ioctl_info *)arg;
1438 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED;
1442 * Remove an initiator by port number and initiator ID.
1443 * Returns 0 for success, -1 for failure.
1446 ctl_remove_initiator(struct ctl_port *port, int iid)
1448 struct ctl_softc *softc = control_softc;
1450 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1452 if (iid > CTL_MAX_INIT_PER_PORT) {
1453 printf("%s: initiator ID %u > maximun %u!\n",
1454 __func__, iid, CTL_MAX_INIT_PER_PORT);
1458 mtx_lock(&softc->ctl_lock);
1459 port->wwpn_iid[iid].in_use--;
1460 port->wwpn_iid[iid].last_use = time_uptime;
1461 mtx_unlock(&softc->ctl_lock);
1467 * Add an initiator to the initiator map.
1468 * Returns iid for success, < 0 for failure.
1471 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
1473 struct ctl_softc *softc = control_softc;
1477 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1479 if (iid >= CTL_MAX_INIT_PER_PORT) {
1480 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
1481 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
1486 mtx_lock(&softc->ctl_lock);
1488 if (iid < 0 && (wwpn != 0 || name != NULL)) {
1489 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1490 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
1494 if (name != NULL && port->wwpn_iid[i].name != NULL &&
1495 strcmp(name, port->wwpn_iid[i].name) == 0) {
1503 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1504 if (port->wwpn_iid[i].in_use == 0 &&
1505 port->wwpn_iid[i].wwpn == 0 &&
1506 port->wwpn_iid[i].name == NULL) {
1515 best_time = INT32_MAX;
1516 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1517 if (port->wwpn_iid[i].in_use == 0) {
1518 if (port->wwpn_iid[i].last_use < best_time) {
1520 best_time = port->wwpn_iid[i].last_use;
1528 mtx_unlock(&softc->ctl_lock);
1533 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
1535 * This is not an error yet.
1537 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
1539 printf("%s: port %d iid %u WWPN %#jx arrived"
1540 " again\n", __func__, port->targ_port,
1541 iid, (uintmax_t)wwpn);
1545 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
1546 strcmp(name, port->wwpn_iid[iid].name) == 0) {
1548 printf("%s: port %d iid %u name '%s' arrived"
1549 " again\n", __func__, port->targ_port,
1556 * This is an error, but what do we do about it? The
1557 * driver is telling us we have a new WWPN for this
1558 * initiator ID, so we pretty much need to use it.
1560 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
1561 " but WWPN %#jx '%s' is still at that address\n",
1562 __func__, port->targ_port, iid, wwpn, name,
1563 (uintmax_t)port->wwpn_iid[iid].wwpn,
1564 port->wwpn_iid[iid].name);
1567 * XXX KDM clear have_ca and ua_pending on each LUN for
1572 free(port->wwpn_iid[iid].name, M_CTL);
1573 port->wwpn_iid[iid].name = name;
1574 port->wwpn_iid[iid].wwpn = wwpn;
1575 port->wwpn_iid[iid].in_use++;
1576 mtx_unlock(&softc->ctl_lock);
1582 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
1586 switch (port->port_type) {
1589 struct scsi_transportid_fcp *id =
1590 (struct scsi_transportid_fcp *)buf;
1591 if (port->wwpn_iid[iid].wwpn == 0)
1593 memset(id, 0, sizeof(*id));
1594 id->format_protocol = SCSI_PROTO_FC;
1595 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
1596 return (sizeof(*id));
1598 case CTL_PORT_ISCSI:
1600 struct scsi_transportid_iscsi_port *id =
1601 (struct scsi_transportid_iscsi_port *)buf;
1602 if (port->wwpn_iid[iid].name == NULL)
1605 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
1607 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
1608 len = roundup2(min(len, 252), 4);
1609 scsi_ulto2b(len, id->additional_length);
1610 return (sizeof(*id) + len);
1614 struct scsi_transportid_sas *id =
1615 (struct scsi_transportid_sas *)buf;
1616 if (port->wwpn_iid[iid].wwpn == 0)
1618 memset(id, 0, sizeof(*id));
1619 id->format_protocol = SCSI_PROTO_SAS;
1620 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
1621 return (sizeof(*id));
1625 struct scsi_transportid_spi *id =
1626 (struct scsi_transportid_spi *)buf;
1627 memset(id, 0, sizeof(*id));
1628 id->format_protocol = SCSI_PROTO_SPI;
1629 scsi_ulto2b(iid, id->scsi_addr);
1630 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
1631 return (sizeof(*id));
1637 ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id)
1643 ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id)
1649 * Data movement routine for the CTL ioctl frontend port.
1652 ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio)
1654 struct ctl_sg_entry *ext_sglist, *kern_sglist;
1655 struct ctl_sg_entry ext_entry, kern_entry;
1656 int ext_sglen, ext_sg_entries, kern_sg_entries;
1657 int ext_sg_start, ext_offset;
1658 int len_to_copy, len_copied;
1659 int kern_watermark, ext_watermark;
1660 int ext_sglist_malloced;
1663 ext_sglist_malloced = 0;
1667 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n"));
1670 * If this flag is set, fake the data transfer.
1672 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) {
1673 ctsio->ext_data_filled = ctsio->ext_data_len;
1678 * To simplify things here, if we have a single buffer, stick it in
1679 * a S/G entry and just make it a single entry S/G list.
1681 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) {
1684 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist);
1686 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL,
1688 ext_sglist_malloced = 1;
1689 if (copyin(ctsio->ext_data_ptr, ext_sglist,
1691 ctl_set_internal_failure(ctsio,
1696 ext_sg_entries = ctsio->ext_sg_entries;
1698 for (i = 0; i < ext_sg_entries; i++) {
1699 if ((len_seen + ext_sglist[i].len) >=
1700 ctsio->ext_data_filled) {
1702 ext_offset = ctsio->ext_data_filled - len_seen;
1705 len_seen += ext_sglist[i].len;
1708 ext_sglist = &ext_entry;
1709 ext_sglist->addr = ctsio->ext_data_ptr;
1710 ext_sglist->len = ctsio->ext_data_len;
1713 ext_offset = ctsio->ext_data_filled;
1716 if (ctsio->kern_sg_entries > 0) {
1717 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
1718 kern_sg_entries = ctsio->kern_sg_entries;
1720 kern_sglist = &kern_entry;
1721 kern_sglist->addr = ctsio->kern_data_ptr;
1722 kern_sglist->len = ctsio->kern_data_len;
1723 kern_sg_entries = 1;
1728 ext_watermark = ext_offset;
1730 for (i = ext_sg_start, j = 0;
1731 i < ext_sg_entries && j < kern_sg_entries;) {
1732 uint8_t *ext_ptr, *kern_ptr;
1734 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark,
1735 kern_sglist[j].len - kern_watermark);
1737 ext_ptr = (uint8_t *)ext_sglist[i].addr;
1738 ext_ptr = ext_ptr + ext_watermark;
1739 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
1743 panic("need to implement bus address support");
1745 kern_ptr = bus_to_virt(kern_sglist[j].addr);
1748 kern_ptr = (uint8_t *)kern_sglist[j].addr;
1749 kern_ptr = kern_ptr + kern_watermark;
1751 kern_watermark += len_to_copy;
1752 ext_watermark += len_to_copy;
1754 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
1756 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1757 "bytes to user\n", len_to_copy));
1758 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1759 "to %p\n", kern_ptr, ext_ptr));
1760 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) {
1761 ctl_set_internal_failure(ctsio,
1767 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1768 "bytes from user\n", len_to_copy));
1769 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1770 "to %p\n", ext_ptr, kern_ptr));
1771 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){
1772 ctl_set_internal_failure(ctsio,
1779 len_copied += len_to_copy;
1781 if (ext_sglist[i].len == ext_watermark) {
1786 if (kern_sglist[j].len == kern_watermark) {
1792 ctsio->ext_data_filled += len_copied;
1794 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, "
1795 "kern_sg_entries: %d\n", ext_sg_entries,
1797 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, "
1798 "kern_data_len = %d\n", ctsio->ext_data_len,
1799 ctsio->kern_data_len));
1802 /* XXX KDM set residual?? */
1805 if (ext_sglist_malloced != 0)
1806 free(ext_sglist, M_CTL);
1808 return (CTL_RETVAL_COMPLETE);
1812 * Serialize a command that went down the "wrong" side, and so was sent to
1813 * this controller for execution. The logic is a little different than the
1814 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
1815 * sent back to the other side, but in the success case, we execute the
1816 * command on this side (XFER mode) or tell the other side to execute it
1820 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
1822 struct ctl_softc *ctl_softc;
1823 union ctl_ha_msg msg_info;
1824 struct ctl_lun *lun;
1828 ctl_softc = control_softc;
1830 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
1831 lun = ctl_softc->ctl_luns[targ_lun];
1835 * Why isn't LUN defined? The other side wouldn't
1836 * send a cmd if the LUN is undefined.
1838 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__);
1840 /* "Logical unit not supported" */
1841 ctl_set_sense_data(&msg_info.scsi.sense_data,
1843 /*sense_format*/SSD_TYPE_NONE,
1844 /*current_error*/ 1,
1845 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1850 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1851 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1852 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1853 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1854 msg_info.hdr.serializing_sc = NULL;
1855 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1856 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1857 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1863 mtx_lock(&lun->lun_lock);
1864 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1866 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
1867 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
1869 case CTL_ACTION_BLOCK:
1870 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
1871 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
1874 case CTL_ACTION_PASS:
1875 case CTL_ACTION_SKIP:
1876 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
1877 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
1878 ctl_enqueue_rtr((union ctl_io *)ctsio);
1881 /* send msg back to other side */
1882 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1883 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
1884 msg_info.hdr.msg_type = CTL_MSG_R2R;
1886 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc);
1888 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1889 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1893 case CTL_ACTION_OVERLAP:
1894 /* OVERLAPPED COMMANDS ATTEMPTED */
1895 ctl_set_sense_data(&msg_info.scsi.sense_data,
1897 /*sense_format*/SSD_TYPE_NONE,
1898 /*current_error*/ 1,
1899 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1904 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1905 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1906 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1907 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1908 msg_info.hdr.serializing_sc = NULL;
1909 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1911 printf("BAD JUJU:Major Bummer Overlap\n");
1913 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1915 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1916 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1919 case CTL_ACTION_OVERLAP_TAG:
1920 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */
1921 ctl_set_sense_data(&msg_info.scsi.sense_data,
1923 /*sense_format*/SSD_TYPE_NONE,
1924 /*current_error*/ 1,
1925 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1927 /*ascq*/ ctsio->tag_num & 0xff,
1930 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1931 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1932 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1933 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1934 msg_info.hdr.serializing_sc = NULL;
1935 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1937 printf("BAD JUJU:Major Bummer Overlap Tag\n");
1939 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1941 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1942 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1945 case CTL_ACTION_ERROR:
1947 /* "Internal target failure" */
1948 ctl_set_sense_data(&msg_info.scsi.sense_data,
1950 /*sense_format*/SSD_TYPE_NONE,
1951 /*current_error*/ 1,
1952 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
1957 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1958 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1959 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1960 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1961 msg_info.hdr.serializing_sc = NULL;
1962 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1964 printf("BAD JUJU:Major Bummer HW Error\n");
1966 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1968 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1969 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1973 mtx_unlock(&lun->lun_lock);
1978 ctl_ioctl_submit_wait(union ctl_io *io)
1980 struct ctl_fe_ioctl_params params;
1981 ctl_fe_ioctl_state last_state;
1986 bzero(¶ms, sizeof(params));
1988 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF);
1989 cv_init(¶ms.sem, "ctlioccv");
1990 params.state = CTL_IOCTL_INPROG;
1991 last_state = params.state;
1993 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms;
1995 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n"));
1997 /* This shouldn't happen */
1998 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE)
2004 mtx_lock(¶ms.ioctl_mtx);
2006 * Check the state here, and don't sleep if the state has
2007 * already changed (i.e. wakeup has already occured, but we
2008 * weren't waiting yet).
2010 if (params.state == last_state) {
2011 /* XXX KDM cv_wait_sig instead? */
2012 cv_wait(¶ms.sem, ¶ms.ioctl_mtx);
2014 last_state = params.state;
2016 switch (params.state) {
2017 case CTL_IOCTL_INPROG:
2018 /* Why did we wake up? */
2019 /* XXX KDM error here? */
2020 mtx_unlock(¶ms.ioctl_mtx);
2022 case CTL_IOCTL_DATAMOVE:
2023 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n"));
2026 * change last_state back to INPROG to avoid
2027 * deadlock on subsequent data moves.
2029 params.state = last_state = CTL_IOCTL_INPROG;
2031 mtx_unlock(¶ms.ioctl_mtx);
2032 ctl_ioctl_do_datamove(&io->scsiio);
2034 * Note that in some cases, most notably writes,
2035 * this will queue the I/O and call us back later.
2036 * In other cases, generally reads, this routine
2037 * will immediately call back and wake us up,
2038 * probably using our own context.
2040 io->scsiio.be_move_done(io);
2042 case CTL_IOCTL_DONE:
2043 mtx_unlock(¶ms.ioctl_mtx);
2044 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n"));
2048 mtx_unlock(¶ms.ioctl_mtx);
2049 /* XXX KDM error here? */
2052 } while (done == 0);
2054 mtx_destroy(¶ms.ioctl_mtx);
2055 cv_destroy(¶ms.sem);
2057 return (CTL_RETVAL_COMPLETE);
2061 ctl_ioctl_datamove(union ctl_io *io)
2063 struct ctl_fe_ioctl_params *params;
2065 params = (struct ctl_fe_ioctl_params *)
2066 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2068 mtx_lock(¶ms->ioctl_mtx);
2069 params->state = CTL_IOCTL_DATAMOVE;
2070 cv_broadcast(¶ms->sem);
2071 mtx_unlock(¶ms->ioctl_mtx);
2075 ctl_ioctl_done(union ctl_io *io)
2077 struct ctl_fe_ioctl_params *params;
2079 params = (struct ctl_fe_ioctl_params *)
2080 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2082 mtx_lock(¶ms->ioctl_mtx);
2083 params->state = CTL_IOCTL_DONE;
2084 cv_broadcast(¶ms->sem);
2085 mtx_unlock(¶ms->ioctl_mtx);
2089 ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask)
2091 struct ctl_fe_ioctl_startstop_info *sd_info;
2093 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg;
2095 sd_info->hs_info.status = metatask->status;
2096 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns;
2097 sd_info->hs_info.luns_complete =
2098 metatask->taskinfo.startstop.luns_complete;
2099 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed;
2101 cv_broadcast(&sd_info->sem);
2105 ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask)
2107 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info;
2109 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg;
2111 mtx_lock(fe_bbr_info->lock);
2112 fe_bbr_info->bbr_info->status = metatask->status;
2113 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2114 fe_bbr_info->wakeup_done = 1;
2115 mtx_unlock(fe_bbr_info->lock);
2117 cv_broadcast(&fe_bbr_info->sem);
2121 * Returns 0 for success, errno for failure.
2124 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2125 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2132 mtx_lock(&lun->lun_lock);
2133 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2134 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2136 struct ctl_ooa_entry *entry;
2139 * If we've got more than we can fit, just count the
2140 * remaining entries.
2142 if (*cur_fill_num >= ooa_hdr->alloc_num)
2145 entry = &kern_entries[*cur_fill_num];
2147 entry->tag_num = io->scsiio.tag_num;
2148 entry->lun_num = lun->lun;
2150 entry->start_bt = io->io_hdr.start_bt;
2152 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2153 entry->cdb_len = io->scsiio.cdb_len;
2154 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2155 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2157 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2158 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2160 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2161 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2163 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2164 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2166 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2167 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2169 mtx_unlock(&lun->lun_lock);
2175 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2176 size_t error_str_len)
2180 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2182 if (copyin(user_addr, kptr, len) != 0) {
2183 snprintf(error_str, error_str_len, "Error copying %d bytes "
2184 "from user address %p to kernel address %p", len,
2194 ctl_free_args(int num_args, struct ctl_be_arg *args)
2201 for (i = 0; i < num_args; i++) {
2202 free(args[i].kname, M_CTL);
2203 free(args[i].kvalue, M_CTL);
2209 static struct ctl_be_arg *
2210 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
2211 char *error_str, size_t error_str_len)
2213 struct ctl_be_arg *args;
2216 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
2217 error_str, error_str_len);
2222 for (i = 0; i < num_args; i++) {
2223 args[i].kname = NULL;
2224 args[i].kvalue = NULL;
2227 for (i = 0; i < num_args; i++) {
2230 args[i].kname = ctl_copyin_alloc(args[i].name,
2231 args[i].namelen, error_str, error_str_len);
2232 if (args[i].kname == NULL)
2235 if (args[i].kname[args[i].namelen - 1] != '\0') {
2236 snprintf(error_str, error_str_len, "Argument %d "
2237 "name is not NUL-terminated", i);
2241 if (args[i].flags & CTL_BEARG_RD) {
2242 tmpptr = ctl_copyin_alloc(args[i].value,
2243 args[i].vallen, error_str, error_str_len);
2246 if ((args[i].flags & CTL_BEARG_ASCII)
2247 && (tmpptr[args[i].vallen - 1] != '\0')) {
2248 snprintf(error_str, error_str_len, "Argument "
2249 "%d value is not NUL-terminated", i);
2252 args[i].kvalue = tmpptr;
2254 args[i].kvalue = malloc(args[i].vallen,
2255 M_CTL, M_WAITOK | M_ZERO);
2262 ctl_free_args(num_args, args);
2268 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2272 for (i = 0; i < num_args; i++) {
2273 if (args[i].flags & CTL_BEARG_WR)
2274 copyout(args[i].kvalue, args[i].value, args[i].vallen);
2279 * Escape characters that are illegal or not recommended in XML.
2282 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2284 char *end = str + size;
2289 for (; *str && str < end; str++) {
2292 retval = sbuf_printf(sb, "&");
2295 retval = sbuf_printf(sb, ">");
2298 retval = sbuf_printf(sb, "<");
2301 retval = sbuf_putc(sb, *str);
2314 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2316 struct scsi_vpd_id_descriptor *desc;
2319 if (id == NULL || id->len < 4)
2321 desc = (struct scsi_vpd_id_descriptor *)id->data;
2322 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2323 case SVPD_ID_TYPE_T10:
2324 sbuf_printf(sb, "t10.");
2326 case SVPD_ID_TYPE_EUI64:
2327 sbuf_printf(sb, "eui.");
2329 case SVPD_ID_TYPE_NAA:
2330 sbuf_printf(sb, "naa.");
2332 case SVPD_ID_TYPE_SCSI_NAME:
2335 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2336 case SVPD_ID_CODESET_BINARY:
2337 for (i = 0; i < desc->length; i++)
2338 sbuf_printf(sb, "%02x", desc->identifier[i]);
2340 case SVPD_ID_CODESET_ASCII:
2341 sbuf_printf(sb, "%.*s", (int)desc->length,
2342 (char *)desc->identifier);
2344 case SVPD_ID_CODESET_UTF8:
2345 sbuf_printf(sb, "%s", (char *)desc->identifier);
2351 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2354 struct ctl_softc *softc;
2357 softc = control_softc;
2367 * If we haven't been "enabled", don't allow any SCSI I/O
2370 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) {
2375 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref);
2377 printf("ctl_ioctl: can't allocate ctl_io!\n");
2383 * Need to save the pool reference so it doesn't get
2384 * spammed by the user's ctl_io.
2386 pool_tmp = io->io_hdr.pool;
2388 memcpy(io, (void *)addr, sizeof(*io));
2390 io->io_hdr.pool = pool_tmp;
2392 * No status yet, so make sure the status is set properly.
2394 io->io_hdr.status = CTL_STATUS_NONE;
2397 * The user sets the initiator ID, target and LUN IDs.
2399 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port;
2400 io->io_hdr.flags |= CTL_FLAG_USER_REQ;
2401 if ((io->io_hdr.io_type == CTL_IO_SCSI)
2402 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED))
2403 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++;
2405 retval = ctl_ioctl_submit_wait(io);
2412 memcpy((void *)addr, io, sizeof(*io));
2414 /* return this to our pool */
2419 case CTL_ENABLE_PORT:
2420 case CTL_DISABLE_PORT:
2421 case CTL_SET_PORT_WWNS: {
2422 struct ctl_port *port;
2423 struct ctl_port_entry *entry;
2425 entry = (struct ctl_port_entry *)addr;
2427 mtx_lock(&softc->ctl_lock);
2428 STAILQ_FOREACH(port, &softc->port_list, links) {
2434 if ((entry->port_type == CTL_PORT_NONE)
2435 && (entry->targ_port == port->targ_port)) {
2437 * If the user only wants to enable or
2438 * disable or set WWNs on a specific port,
2439 * do the operation and we're done.
2443 } else if (entry->port_type & port->port_type) {
2445 * Compare the user's type mask with the
2446 * particular frontend type to see if we
2453 * Make sure the user isn't trying to set
2454 * WWNs on multiple ports at the same time.
2456 if (cmd == CTL_SET_PORT_WWNS) {
2457 printf("%s: Can't set WWNs on "
2458 "multiple ports\n", __func__);
2465 * XXX KDM we have to drop the lock here,
2466 * because the online/offline operations
2467 * can potentially block. We need to
2468 * reference count the frontends so they
2471 mtx_unlock(&softc->ctl_lock);
2473 if (cmd == CTL_ENABLE_PORT) {
2474 struct ctl_lun *lun;
2476 STAILQ_FOREACH(lun, &softc->lun_list,
2478 port->lun_enable(port->targ_lun_arg,
2483 ctl_port_online(port);
2484 } else if (cmd == CTL_DISABLE_PORT) {
2485 struct ctl_lun *lun;
2487 ctl_port_offline(port);
2489 STAILQ_FOREACH(lun, &softc->lun_list,
2498 mtx_lock(&softc->ctl_lock);
2500 if (cmd == CTL_SET_PORT_WWNS)
2501 ctl_port_set_wwns(port,
2502 (entry->flags & CTL_PORT_WWNN_VALID) ?
2504 (entry->flags & CTL_PORT_WWPN_VALID) ?
2505 1 : 0, entry->wwpn);
2510 mtx_unlock(&softc->ctl_lock);
2513 case CTL_GET_PORT_LIST: {
2514 struct ctl_port *port;
2515 struct ctl_port_list *list;
2518 list = (struct ctl_port_list *)addr;
2520 if (list->alloc_len != (list->alloc_num *
2521 sizeof(struct ctl_port_entry))) {
2522 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != "
2523 "alloc_num %u * sizeof(struct ctl_port_entry) "
2524 "%zu\n", __func__, list->alloc_len,
2525 list->alloc_num, sizeof(struct ctl_port_entry));
2531 list->dropped_num = 0;
2533 mtx_lock(&softc->ctl_lock);
2534 STAILQ_FOREACH(port, &softc->port_list, links) {
2535 struct ctl_port_entry entry, *list_entry;
2537 if (list->fill_num >= list->alloc_num) {
2538 list->dropped_num++;
2542 entry.port_type = port->port_type;
2543 strlcpy(entry.port_name, port->port_name,
2544 sizeof(entry.port_name));
2545 entry.targ_port = port->targ_port;
2546 entry.physical_port = port->physical_port;
2547 entry.virtual_port = port->virtual_port;
2548 entry.wwnn = port->wwnn;
2549 entry.wwpn = port->wwpn;
2550 if (port->status & CTL_PORT_STATUS_ONLINE)
2555 list_entry = &list->entries[i];
2557 retval = copyout(&entry, list_entry, sizeof(entry));
2559 printf("%s: CTL_GET_PORT_LIST: copyout "
2560 "returned %d\n", __func__, retval);
2565 list->fill_len += sizeof(entry);
2567 mtx_unlock(&softc->ctl_lock);
2570 * If this is non-zero, we had a copyout fault, so there's
2571 * probably no point in attempting to set the status inside
2577 if (list->dropped_num > 0)
2578 list->status = CTL_PORT_LIST_NEED_MORE_SPACE;
2580 list->status = CTL_PORT_LIST_OK;
2583 case CTL_DUMP_OOA: {
2584 struct ctl_lun *lun;
2589 mtx_lock(&softc->ctl_lock);
2590 printf("Dumping OOA queues:\n");
2591 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2592 mtx_lock(&lun->lun_lock);
2593 for (io = (union ctl_io *)TAILQ_FIRST(
2594 &lun->ooa_queue); io != NULL;
2595 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2597 sbuf_new(&sb, printbuf, sizeof(printbuf),
2599 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ",
2603 CTL_FLAG_BLOCKED) ? "" : " BLOCKED",
2605 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
2607 CTL_FLAG_ABORT) ? " ABORT" : "",
2609 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : "");
2610 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
2612 printf("%s\n", sbuf_data(&sb));
2614 mtx_unlock(&lun->lun_lock);
2616 printf("OOA queues dump done\n");
2617 mtx_unlock(&softc->ctl_lock);
2621 struct ctl_lun *lun;
2622 struct ctl_ooa *ooa_hdr;
2623 struct ctl_ooa_entry *entries;
2624 uint32_t cur_fill_num;
2626 ooa_hdr = (struct ctl_ooa *)addr;
2628 if ((ooa_hdr->alloc_len == 0)
2629 || (ooa_hdr->alloc_num == 0)) {
2630 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2631 "must be non-zero\n", __func__,
2632 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2637 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2638 sizeof(struct ctl_ooa_entry))) {
2639 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2640 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2641 __func__, ooa_hdr->alloc_len,
2642 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2647 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2648 if (entries == NULL) {
2649 printf("%s: could not allocate %d bytes for OOA "
2650 "dump\n", __func__, ooa_hdr->alloc_len);
2655 mtx_lock(&softc->ctl_lock);
2656 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2657 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS)
2658 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2659 mtx_unlock(&softc->ctl_lock);
2660 free(entries, M_CTL);
2661 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2662 __func__, (uintmax_t)ooa_hdr->lun_num);
2669 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2670 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2671 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2677 mtx_unlock(&softc->ctl_lock);
2678 free(entries, M_CTL);
2682 lun = softc->ctl_luns[ooa_hdr->lun_num];
2684 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr,
2687 mtx_unlock(&softc->ctl_lock);
2689 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2690 ooa_hdr->fill_len = ooa_hdr->fill_num *
2691 sizeof(struct ctl_ooa_entry);
2692 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2694 printf("%s: error copying out %d bytes for OOA dump\n",
2695 __func__, ooa_hdr->fill_len);
2698 getbintime(&ooa_hdr->cur_bt);
2700 if (cur_fill_num > ooa_hdr->alloc_num) {
2701 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2702 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2704 ooa_hdr->dropped_num = 0;
2705 ooa_hdr->status = CTL_OOA_OK;
2708 free(entries, M_CTL);
2711 case CTL_CHECK_OOA: {
2713 struct ctl_lun *lun;
2714 struct ctl_ooa_info *ooa_info;
2717 ooa_info = (struct ctl_ooa_info *)addr;
2719 if (ooa_info->lun_id >= CTL_MAX_LUNS) {
2720 ooa_info->status = CTL_OOA_INVALID_LUN;
2723 mtx_lock(&softc->ctl_lock);
2724 lun = softc->ctl_luns[ooa_info->lun_id];
2726 mtx_unlock(&softc->ctl_lock);
2727 ooa_info->status = CTL_OOA_INVALID_LUN;
2730 mtx_lock(&lun->lun_lock);
2731 mtx_unlock(&softc->ctl_lock);
2732 ooa_info->num_entries = 0;
2733 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
2734 io != NULL; io = (union ctl_io *)TAILQ_NEXT(
2735 &io->io_hdr, ooa_links)) {
2736 ooa_info->num_entries++;
2738 mtx_unlock(&lun->lun_lock);
2740 ooa_info->status = CTL_OOA_SUCCESS;
2744 case CTL_HARD_START:
2745 case CTL_HARD_STOP: {
2746 struct ctl_fe_ioctl_startstop_info ss_info;
2747 struct cfi_metatask *metatask;
2750 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF);
2752 cv_init(&ss_info.sem, "hard start/stop cv" );
2754 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2755 if (metatask == NULL) {
2757 mtx_destroy(&hs_mtx);
2761 if (cmd == CTL_HARD_START)
2762 metatask->tasktype = CFI_TASK_STARTUP;
2764 metatask->tasktype = CFI_TASK_SHUTDOWN;
2766 metatask->callback = ctl_ioctl_hard_startstop_callback;
2767 metatask->callback_arg = &ss_info;
2769 cfi_action(metatask);
2771 /* Wait for the callback */
2773 cv_wait_sig(&ss_info.sem, &hs_mtx);
2774 mtx_unlock(&hs_mtx);
2777 * All information has been copied from the metatask by the
2778 * time cv_broadcast() is called, so we free the metatask here.
2780 cfi_free_metatask(metatask);
2782 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info));
2784 mtx_destroy(&hs_mtx);
2788 struct ctl_bbrread_info *bbr_info;
2789 struct ctl_fe_ioctl_bbrread_info fe_bbr_info;
2791 struct cfi_metatask *metatask;
2793 bbr_info = (struct ctl_bbrread_info *)addr;
2795 bzero(&fe_bbr_info, sizeof(fe_bbr_info));
2797 bzero(&bbr_mtx, sizeof(bbr_mtx));
2798 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF);
2800 fe_bbr_info.bbr_info = bbr_info;
2801 fe_bbr_info.lock = &bbr_mtx;
2803 cv_init(&fe_bbr_info.sem, "BBR read cv");
2804 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2806 if (metatask == NULL) {
2807 mtx_destroy(&bbr_mtx);
2808 cv_destroy(&fe_bbr_info.sem);
2812 metatask->tasktype = CFI_TASK_BBRREAD;
2813 metatask->callback = ctl_ioctl_bbrread_callback;
2814 metatask->callback_arg = &fe_bbr_info;
2815 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num;
2816 metatask->taskinfo.bbrread.lba = bbr_info->lba;
2817 metatask->taskinfo.bbrread.len = bbr_info->len;
2819 cfi_action(metatask);
2822 while (fe_bbr_info.wakeup_done == 0)
2823 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx);
2824 mtx_unlock(&bbr_mtx);
2826 bbr_info->status = metatask->status;
2827 bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2828 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status;
2829 memcpy(&bbr_info->sense_data,
2830 &metatask->taskinfo.bbrread.sense_data,
2831 ctl_min(sizeof(bbr_info->sense_data),
2832 sizeof(metatask->taskinfo.bbrread.sense_data)));
2834 cfi_free_metatask(metatask);
2836 mtx_destroy(&bbr_mtx);
2837 cv_destroy(&fe_bbr_info.sem);
2841 case CTL_DELAY_IO: {
2842 struct ctl_io_delay_info *delay_info;
2844 struct ctl_lun *lun;
2845 #endif /* CTL_IO_DELAY */
2847 delay_info = (struct ctl_io_delay_info *)addr;
2850 mtx_lock(&softc->ctl_lock);
2852 if ((delay_info->lun_id >= CTL_MAX_LUNS)
2853 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2854 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2856 lun = softc->ctl_luns[delay_info->lun_id];
2857 mtx_lock(&lun->lun_lock);
2859 delay_info->status = CTL_DELAY_STATUS_OK;
2861 switch (delay_info->delay_type) {
2862 case CTL_DELAY_TYPE_CONT:
2864 case CTL_DELAY_TYPE_ONESHOT:
2867 delay_info->status =
2868 CTL_DELAY_STATUS_INVALID_TYPE;
2872 switch (delay_info->delay_loc) {
2873 case CTL_DELAY_LOC_DATAMOVE:
2874 lun->delay_info.datamove_type =
2875 delay_info->delay_type;
2876 lun->delay_info.datamove_delay =
2877 delay_info->delay_secs;
2879 case CTL_DELAY_LOC_DONE:
2880 lun->delay_info.done_type =
2881 delay_info->delay_type;
2882 lun->delay_info.done_delay =
2883 delay_info->delay_secs;
2886 delay_info->status =
2887 CTL_DELAY_STATUS_INVALID_LOC;
2890 mtx_unlock(&lun->lun_lock);
2893 mtx_unlock(&softc->ctl_lock);
2895 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2896 #endif /* CTL_IO_DELAY */
2899 case CTL_REALSYNC_SET: {
2902 syncstate = (int *)addr;
2904 mtx_lock(&softc->ctl_lock);
2905 switch (*syncstate) {
2907 softc->flags &= ~CTL_FLAG_REAL_SYNC;
2910 softc->flags |= CTL_FLAG_REAL_SYNC;
2916 mtx_unlock(&softc->ctl_lock);
2919 case CTL_REALSYNC_GET: {
2922 syncstate = (int*)addr;
2924 mtx_lock(&softc->ctl_lock);
2925 if (softc->flags & CTL_FLAG_REAL_SYNC)
2929 mtx_unlock(&softc->ctl_lock);
2935 struct ctl_sync_info *sync_info;
2936 struct ctl_lun *lun;
2938 sync_info = (struct ctl_sync_info *)addr;
2940 mtx_lock(&softc->ctl_lock);
2941 lun = softc->ctl_luns[sync_info->lun_id];
2943 mtx_unlock(&softc->ctl_lock);
2944 sync_info->status = CTL_GS_SYNC_NO_LUN;
2947 * Get or set the sync interval. We're not bounds checking
2948 * in the set case, hopefully the user won't do something
2951 mtx_lock(&lun->lun_lock);
2952 mtx_unlock(&softc->ctl_lock);
2953 if (cmd == CTL_GETSYNC)
2954 sync_info->sync_interval = lun->sync_interval;
2956 lun->sync_interval = sync_info->sync_interval;
2957 mtx_unlock(&lun->lun_lock);
2959 sync_info->status = CTL_GS_SYNC_OK;
2963 case CTL_GETSTATS: {
2964 struct ctl_stats *stats;
2965 struct ctl_lun *lun;
2968 stats = (struct ctl_stats *)addr;
2970 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2972 stats->status = CTL_SS_NEED_MORE_SPACE;
2973 stats->num_luns = softc->num_luns;
2977 * XXX KDM no locking here. If the LUN list changes,
2978 * things can blow up.
2980 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
2981 i++, lun = STAILQ_NEXT(lun, links)) {
2982 retval = copyout(&lun->stats, &stats->lun_stats[i],
2983 sizeof(lun->stats));
2987 stats->num_luns = softc->num_luns;
2988 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2990 stats->status = CTL_SS_OK;
2992 stats->flags = CTL_STATS_FLAG_TIME_VALID;
2994 stats->flags = CTL_STATS_FLAG_NONE;
2996 getnanouptime(&stats->timestamp);
2999 case CTL_ERROR_INJECT: {
3000 struct ctl_error_desc *err_desc, *new_err_desc;
3001 struct ctl_lun *lun;
3003 err_desc = (struct ctl_error_desc *)addr;
3005 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
3007 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
3009 mtx_lock(&softc->ctl_lock);
3010 lun = softc->ctl_luns[err_desc->lun_id];
3012 mtx_unlock(&softc->ctl_lock);
3013 free(new_err_desc, M_CTL);
3014 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
3015 __func__, (uintmax_t)err_desc->lun_id);
3019 mtx_lock(&lun->lun_lock);
3020 mtx_unlock(&softc->ctl_lock);
3023 * We could do some checking here to verify the validity
3024 * of the request, but given the complexity of error
3025 * injection requests, the checking logic would be fairly
3028 * For now, if the request is invalid, it just won't get
3029 * executed and might get deleted.
3031 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
3034 * XXX KDM check to make sure the serial number is unique,
3035 * in case we somehow manage to wrap. That shouldn't
3036 * happen for a very long time, but it's the right thing to
3039 new_err_desc->serial = lun->error_serial;
3040 err_desc->serial = lun->error_serial;
3041 lun->error_serial++;
3043 mtx_unlock(&lun->lun_lock);
3046 case CTL_ERROR_INJECT_DELETE: {
3047 struct ctl_error_desc *delete_desc, *desc, *desc2;
3048 struct ctl_lun *lun;
3051 delete_desc = (struct ctl_error_desc *)addr;
3054 mtx_lock(&softc->ctl_lock);
3055 lun = softc->ctl_luns[delete_desc->lun_id];
3057 mtx_unlock(&softc->ctl_lock);
3058 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
3059 __func__, (uintmax_t)delete_desc->lun_id);
3063 mtx_lock(&lun->lun_lock);
3064 mtx_unlock(&softc->ctl_lock);
3065 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
3066 if (desc->serial != delete_desc->serial)
3069 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
3074 mtx_unlock(&lun->lun_lock);
3075 if (delete_done == 0) {
3076 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
3077 "error serial %ju on LUN %u\n", __func__,
3078 delete_desc->serial, delete_desc->lun_id);
3084 case CTL_DUMP_STRUCTS: {
3086 struct ctl_port *port;
3087 struct ctl_frontend *fe;
3089 mtx_lock(&softc->ctl_lock);
3090 printf("CTL Persistent Reservation information start:\n");
3091 for (i = 0; i < CTL_MAX_LUNS; i++) {
3092 struct ctl_lun *lun;
3094 lun = softc->ctl_luns[i];
3097 || ((lun->flags & CTL_LUN_DISABLED) != 0))
3100 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) {
3101 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
3102 idx = j * CTL_MAX_INIT_PER_PORT + k;
3103 if (lun->pr_keys[idx] == 0)
3105 printf(" LUN %d port %d iid %d key "
3107 (uintmax_t)lun->pr_keys[idx]);
3111 printf("CTL Persistent Reservation information end\n");
3112 printf("CTL Ports:\n");
3113 STAILQ_FOREACH(port, &softc->port_list, links) {
3114 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
3115 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
3116 port->frontend->name, port->port_type,
3117 port->physical_port, port->virtual_port,
3118 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
3119 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3120 if (port->wwpn_iid[j].in_use == 0 &&
3121 port->wwpn_iid[j].wwpn == 0 &&
3122 port->wwpn_iid[j].name == NULL)
3125 printf(" iid %u use %d WWPN %#jx '%s'\n",
3126 j, port->wwpn_iid[j].in_use,
3127 (uintmax_t)port->wwpn_iid[j].wwpn,
3128 port->wwpn_iid[j].name);
3131 printf("CTL Port information end\n");
3132 mtx_unlock(&softc->ctl_lock);
3134 * XXX KDM calling this without a lock. We'd likely want
3135 * to drop the lock before calling the frontend's dump
3138 printf("CTL Frontends:\n");
3139 STAILQ_FOREACH(fe, &softc->fe_list, links) {
3140 printf(" Frontend '%s'\n", fe->name);
3141 if (fe->fe_dump != NULL)
3144 printf("CTL Frontend information end\n");
3148 struct ctl_lun_req *lun_req;
3149 struct ctl_backend_driver *backend;
3151 lun_req = (struct ctl_lun_req *)addr;
3153 backend = ctl_backend_find(lun_req->backend);
3154 if (backend == NULL) {
3155 lun_req->status = CTL_LUN_ERROR;
3156 snprintf(lun_req->error_str,
3157 sizeof(lun_req->error_str),
3158 "Backend \"%s\" not found.",
3162 if (lun_req->num_be_args > 0) {
3163 lun_req->kern_be_args = ctl_copyin_args(
3164 lun_req->num_be_args,
3167 sizeof(lun_req->error_str));
3168 if (lun_req->kern_be_args == NULL) {
3169 lun_req->status = CTL_LUN_ERROR;
3174 retval = backend->ioctl(dev, cmd, addr, flag, td);
3176 if (lun_req->num_be_args > 0) {
3177 ctl_copyout_args(lun_req->num_be_args,
3178 lun_req->kern_be_args);
3179 ctl_free_args(lun_req->num_be_args,
3180 lun_req->kern_be_args);
3184 case CTL_LUN_LIST: {
3186 struct ctl_lun *lun;
3187 struct ctl_lun_list *list;
3188 struct ctl_option *opt;
3190 list = (struct ctl_lun_list *)addr;
3193 * Allocate a fixed length sbuf here, based on the length
3194 * of the user's buffer. We could allocate an auto-extending
3195 * buffer, and then tell the user how much larger our
3196 * amount of data is than his buffer, but that presents
3199 * 1. The sbuf(9) routines use a blocking malloc, and so
3200 * we can't hold a lock while calling them with an
3201 * auto-extending buffer.
3203 * 2. There is not currently a LUN reference counting
3204 * mechanism, outside of outstanding transactions on
3205 * the LUN's OOA queue. So a LUN could go away on us
3206 * while we're getting the LUN number, backend-specific
3207 * information, etc. Thus, given the way things
3208 * currently work, we need to hold the CTL lock while
3209 * grabbing LUN information.
3211 * So, from the user's standpoint, the best thing to do is
3212 * allocate what he thinks is a reasonable buffer length,
3213 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3214 * double the buffer length and try again. (And repeat
3215 * that until he succeeds.)
3217 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3219 list->status = CTL_LUN_LIST_ERROR;
3220 snprintf(list->error_str, sizeof(list->error_str),
3221 "Unable to allocate %d bytes for LUN list",
3226 sbuf_printf(sb, "<ctllunlist>\n");
3228 mtx_lock(&softc->ctl_lock);
3229 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3230 mtx_lock(&lun->lun_lock);
3231 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3232 (uintmax_t)lun->lun);
3235 * Bail out as soon as we see that we've overfilled
3241 retval = sbuf_printf(sb, "\t<backend_type>%s"
3242 "</backend_type>\n",
3243 (lun->backend == NULL) ? "none" :
3244 lun->backend->name);
3249 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3250 lun->be_lun->lun_type);
3255 if (lun->backend == NULL) {
3256 retval = sbuf_printf(sb, "</lun>\n");
3262 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3263 (lun->be_lun->maxlba > 0) ?
3264 lun->be_lun->maxlba + 1 : 0);
3269 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3270 lun->be_lun->blocksize);
3275 retval = sbuf_printf(sb, "\t<serial_number>");
3280 retval = ctl_sbuf_printf_esc(sb,
3281 lun->be_lun->serial_num,
3282 sizeof(lun->be_lun->serial_num));
3287 retval = sbuf_printf(sb, "</serial_number>\n");
3292 retval = sbuf_printf(sb, "\t<device_id>");
3297 retval = ctl_sbuf_printf_esc(sb,
3298 lun->be_lun->device_id,
3299 sizeof(lun->be_lun->device_id));
3304 retval = sbuf_printf(sb, "</device_id>\n");
3309 if (lun->backend->lun_info != NULL) {
3310 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3314 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3315 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3316 opt->name, opt->value, opt->name);
3321 retval = sbuf_printf(sb, "</lun>\n");
3325 mtx_unlock(&lun->lun_lock);
3328 mtx_unlock(&lun->lun_lock);
3329 mtx_unlock(&softc->ctl_lock);
3332 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3335 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3336 snprintf(list->error_str, sizeof(list->error_str),
3337 "Out of space, %d bytes is too small",
3344 retval = copyout(sbuf_data(sb), list->lun_xml,
3347 list->fill_len = sbuf_len(sb) + 1;
3348 list->status = CTL_LUN_LIST_OK;
3353 struct ctl_iscsi *ci;
3354 struct ctl_frontend *fe;
3356 ci = (struct ctl_iscsi *)addr;
3358 fe = ctl_frontend_find("iscsi");
3360 ci->status = CTL_ISCSI_ERROR;
3361 snprintf(ci->error_str, sizeof(ci->error_str),
3362 "Frontend \"iscsi\" not found.");
3366 retval = fe->ioctl(dev, cmd, addr, flag, td);
3369 case CTL_PORT_REQ: {
3370 struct ctl_req *req;
3371 struct ctl_frontend *fe;
3373 req = (struct ctl_req *)addr;
3375 fe = ctl_frontend_find(req->driver);
3377 req->status = CTL_LUN_ERROR;
3378 snprintf(req->error_str, sizeof(req->error_str),
3379 "Frontend \"%s\" not found.", req->driver);
3382 if (req->num_args > 0) {
3383 req->kern_args = ctl_copyin_args(req->num_args,
3384 req->args, req->error_str, sizeof(req->error_str));
3385 if (req->kern_args == NULL) {
3386 req->status = CTL_LUN_ERROR;
3391 retval = fe->ioctl(dev, cmd, addr, flag, td);
3393 if (req->num_args > 0) {
3394 ctl_copyout_args(req->num_args, req->kern_args);
3395 ctl_free_args(req->num_args, req->kern_args);
3399 case CTL_PORT_LIST: {
3401 struct ctl_port *port;
3402 struct ctl_lun_list *list;
3403 struct ctl_option *opt;
3406 list = (struct ctl_lun_list *)addr;
3408 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3410 list->status = CTL_LUN_LIST_ERROR;
3411 snprintf(list->error_str, sizeof(list->error_str),
3412 "Unable to allocate %d bytes for LUN list",
3417 sbuf_printf(sb, "<ctlportlist>\n");
3419 mtx_lock(&softc->ctl_lock);
3420 STAILQ_FOREACH(port, &softc->port_list, links) {
3421 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3422 (uintmax_t)port->targ_port);
3425 * Bail out as soon as we see that we've overfilled
3431 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3432 "</frontend_type>\n", port->frontend->name);
3436 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3441 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3442 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3446 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3451 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3452 port->physical_port);
3456 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3457 port->virtual_port);
3461 if (port->target_devid != NULL) {
3462 sbuf_printf(sb, "\t<target>");
3463 ctl_id_sbuf(port->target_devid, sb);
3464 sbuf_printf(sb, "</target>\n");
3467 if (port->port_devid != NULL) {
3468 sbuf_printf(sb, "\t<port>");
3469 ctl_id_sbuf(port->port_devid, sb);
3470 sbuf_printf(sb, "</port>\n");
3473 if (port->port_info != NULL) {
3474 retval = port->port_info(port->onoff_arg, sb);
3478 STAILQ_FOREACH(opt, &port->options, links) {
3479 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3480 opt->name, opt->value, opt->name);
3485 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3486 if (port->wwpn_iid[j].in_use == 0 ||
3487 (port->wwpn_iid[j].wwpn == 0 &&
3488 port->wwpn_iid[j].name == NULL))
3491 if (port->wwpn_iid[j].name != NULL)
3492 retval = sbuf_printf(sb,
3493 "\t<initiator>%u %s</initiator>\n",
3494 j, port->wwpn_iid[j].name);
3496 retval = sbuf_printf(sb,
3497 "\t<initiator>%u naa.%08jx</initiator>\n",
3498 j, port->wwpn_iid[j].wwpn);
3505 retval = sbuf_printf(sb, "</targ_port>\n");
3509 mtx_unlock(&softc->ctl_lock);
3512 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3515 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3516 snprintf(list->error_str, sizeof(list->error_str),
3517 "Out of space, %d bytes is too small",
3524 retval = copyout(sbuf_data(sb), list->lun_xml,
3527 list->fill_len = sbuf_len(sb) + 1;
3528 list->status = CTL_LUN_LIST_OK;
3533 /* XXX KDM should we fix this? */
3535 struct ctl_backend_driver *backend;
3542 * We encode the backend type as the ioctl type for backend
3543 * ioctls. So parse it out here, and then search for a
3544 * backend of this type.
3546 type = _IOC_TYPE(cmd);
3548 STAILQ_FOREACH(backend, &softc->be_list, links) {
3549 if (backend->type == type) {
3555 printf("ctl: unknown ioctl command %#lx or backend "
3560 retval = backend->ioctl(dev, cmd, addr, flag, td);
3570 ctl_get_initindex(struct ctl_nexus *nexus)
3572 if (nexus->targ_port < CTL_MAX_PORTS)
3573 return (nexus->initid.id +
3574 (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3576 return (nexus->initid.id +
3577 ((nexus->targ_port - CTL_MAX_PORTS) *
3578 CTL_MAX_INIT_PER_PORT));
3582 ctl_get_resindex(struct ctl_nexus *nexus)
3584 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3588 ctl_port_idx(int port_num)
3590 if (port_num < CTL_MAX_PORTS)
3593 return(port_num - CTL_MAX_PORTS);
3597 ctl_map_lun(int port_num, uint32_t lun_id)
3599 struct ctl_port *port;
3601 port = control_softc->ctl_ports[ctl_port_idx(port_num)];
3603 return (UINT32_MAX);
3604 if (port->lun_map == NULL)
3606 return (port->lun_map(port->targ_lun_arg, lun_id));
3610 ctl_map_lun_back(int port_num, uint32_t lun_id)
3612 struct ctl_port *port;
3615 port = control_softc->ctl_ports[ctl_port_idx(port_num)];
3616 if (port->lun_map == NULL)
3618 for (i = 0; i < CTL_MAX_LUNS; i++) {
3619 if (port->lun_map(port->targ_lun_arg, i) == lun_id)
3622 return (UINT32_MAX);
3626 * Note: This only works for bitmask sizes that are at least 32 bits, and
3627 * that are a power of 2.
3630 ctl_ffz(uint32_t *mask, uint32_t size)
3632 uint32_t num_chunks, num_pieces;
3635 num_chunks = (size >> 5);
3636 if (num_chunks == 0)
3638 num_pieces = ctl_min((sizeof(uint32_t) * 8), size);
3640 for (i = 0; i < num_chunks; i++) {
3641 for (j = 0; j < num_pieces; j++) {
3642 if ((mask[i] & (1 << j)) == 0)
3643 return ((i << 5) + j);
3651 ctl_set_mask(uint32_t *mask, uint32_t bit)
3653 uint32_t chunk, piece;
3656 piece = bit % (sizeof(uint32_t) * 8);
3658 if ((mask[chunk] & (1 << piece)) != 0)
3661 mask[chunk] |= (1 << piece);
3667 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3669 uint32_t chunk, piece;
3672 piece = bit % (sizeof(uint32_t) * 8);
3674 if ((mask[chunk] & (1 << piece)) == 0)
3677 mask[chunk] &= ~(1 << piece);
3683 ctl_is_set(uint32_t *mask, uint32_t bit)
3685 uint32_t chunk, piece;
3688 piece = bit % (sizeof(uint32_t) * 8);
3690 if ((mask[chunk] & (1 << piece)) == 0)
3698 * The bus, target and lun are optional, they can be filled in later.
3699 * can_wait is used to determine whether we can wait on the malloc or not.
3702 ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target,
3703 uint32_t targ_lun, int can_wait)
3708 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK);
3710 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT);
3713 io->io_hdr.io_type = io_type;
3714 io->io_hdr.targ_port = targ_port;
3716 * XXX KDM this needs to change/go away. We need to move
3717 * to a preallocated pool of ctl_scsiio structures.
3719 io->io_hdr.nexus.targ_target.id = targ_target;
3720 io->io_hdr.nexus.targ_lun = targ_lun;
3727 ctl_kfree_io(union ctl_io *io)
3734 * ctl_softc, pool_type, total_ctl_io are passed in.
3735 * npool is passed out.
3738 ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type,
3739 uint32_t total_ctl_io, struct ctl_io_pool **npool)
3742 union ctl_io *cur_io, *next_io;
3743 struct ctl_io_pool *pool;
3748 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3755 pool->type = pool_type;
3756 pool->ctl_softc = ctl_softc;
3758 mtx_lock(&ctl_softc->pool_lock);
3759 pool->id = ctl_softc->cur_pool_id++;
3760 mtx_unlock(&ctl_softc->pool_lock);
3762 pool->flags = CTL_POOL_FLAG_NONE;
3763 pool->refcount = 1; /* Reference for validity. */
3764 STAILQ_INIT(&pool->free_queue);
3767 * XXX KDM other options here:
3768 * - allocate a page at a time
3769 * - allocate one big chunk of memory.
3770 * Page allocation might work well, but would take a little more
3773 for (i = 0; i < total_ctl_io; i++) {
3774 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTLIO,
3776 if (cur_io == NULL) {
3780 cur_io->io_hdr.pool = pool;
3781 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links);
3782 pool->total_ctl_io++;
3783 pool->free_ctl_io++;
3787 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3788 cur_io != NULL; cur_io = next_io) {
3789 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr,
3791 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr,
3793 free(cur_io, M_CTLIO);
3799 mtx_lock(&ctl_softc->pool_lock);
3800 ctl_softc->num_pools++;
3801 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links);
3803 * Increment our usage count if this is an external consumer, so we
3804 * can't get unloaded until the external consumer (most likely a
3805 * FETD) unloads and frees his pool.
3807 * XXX KDM will this increment the caller's module use count, or
3811 if ((pool_type != CTL_POOL_EMERGENCY)
3812 && (pool_type != CTL_POOL_INTERNAL)
3813 && (pool_type != CTL_POOL_4OTHERSC))
3817 mtx_unlock(&ctl_softc->pool_lock);
3827 ctl_pool_acquire(struct ctl_io_pool *pool)
3830 mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED);
3832 if (pool->flags & CTL_POOL_FLAG_INVALID)
3841 ctl_pool_release(struct ctl_io_pool *pool)
3843 struct ctl_softc *ctl_softc = pool->ctl_softc;
3846 mtx_assert(&ctl_softc->pool_lock, MA_OWNED);
3848 if (--pool->refcount != 0)
3851 while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) {
3852 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr,
3857 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links);
3858 ctl_softc->num_pools--;
3861 * XXX KDM will this decrement the caller's usage count or mine?
3864 if ((pool->type != CTL_POOL_EMERGENCY)
3865 && (pool->type != CTL_POOL_INTERNAL)
3866 && (pool->type != CTL_POOL_4OTHERSC))
3874 ctl_pool_free(struct ctl_io_pool *pool)
3876 struct ctl_softc *ctl_softc;
3881 ctl_softc = pool->ctl_softc;
3882 mtx_lock(&ctl_softc->pool_lock);
3883 pool->flags |= CTL_POOL_FLAG_INVALID;
3884 ctl_pool_release(pool);
3885 mtx_unlock(&ctl_softc->pool_lock);
3889 * This routine does not block (except for spinlocks of course).
3890 * It tries to allocate a ctl_io union from the caller's pool as quickly as
3894 ctl_alloc_io(void *pool_ref)
3897 struct ctl_softc *ctl_softc;
3898 struct ctl_io_pool *pool, *npool;
3899 struct ctl_io_pool *emergency_pool;
3901 pool = (struct ctl_io_pool *)pool_ref;
3904 printf("%s: pool is NULL\n", __func__);
3908 emergency_pool = NULL;
3910 ctl_softc = pool->ctl_softc;
3912 mtx_lock(&ctl_softc->pool_lock);
3914 * First, try to get the io structure from the user's pool.
3916 if (ctl_pool_acquire(pool) == 0) {
3917 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3919 STAILQ_REMOVE_HEAD(&pool->free_queue, links);
3920 pool->total_allocated++;
3921 pool->free_ctl_io--;
3922 mtx_unlock(&ctl_softc->pool_lock);
3925 ctl_pool_release(pool);
3928 * If he doesn't have any io structures left, search for an
3929 * emergency pool and grab one from there.
3931 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) {
3932 if (npool->type != CTL_POOL_EMERGENCY)
3935 if (ctl_pool_acquire(npool) != 0)
3938 emergency_pool = npool;
3940 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue);
3942 STAILQ_REMOVE_HEAD(&npool->free_queue, links);
3943 npool->total_allocated++;
3944 npool->free_ctl_io--;
3945 mtx_unlock(&ctl_softc->pool_lock);
3948 ctl_pool_release(npool);
3951 /* Drop the spinlock before we malloc */
3952 mtx_unlock(&ctl_softc->pool_lock);
3955 * The emergency pool (if it exists) didn't have one, so try an
3956 * atomic (i.e. nonblocking) malloc and see if we get lucky.
3958 io = (union ctl_io *)malloc(sizeof(*io), M_CTLIO, M_NOWAIT);
3961 * If the emergency pool exists but is empty, add this
3962 * ctl_io to its list when it gets freed.
3964 if (emergency_pool != NULL) {
3965 mtx_lock(&ctl_softc->pool_lock);
3966 if (ctl_pool_acquire(emergency_pool) == 0) {
3967 io->io_hdr.pool = emergency_pool;
3968 emergency_pool->total_ctl_io++;
3970 * Need to bump this, otherwise
3971 * total_allocated and total_freed won't
3972 * match when we no longer have anything
3975 emergency_pool->total_allocated++;
3977 mtx_unlock(&ctl_softc->pool_lock);
3979 io->io_hdr.pool = NULL;
3986 ctl_free_io(union ctl_io *io)
3992 * If this ctl_io has a pool, return it to that pool.
3994 if (io->io_hdr.pool != NULL) {
3995 struct ctl_io_pool *pool;
3997 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3998 mtx_lock(&pool->ctl_softc->pool_lock);
3999 io->io_hdr.io_type = 0xff;
4000 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links);
4001 pool->total_freed++;
4002 pool->free_ctl_io++;
4003 ctl_pool_release(pool);
4004 mtx_unlock(&pool->ctl_softc->pool_lock);
4007 * Otherwise, just free it. We probably malloced it and
4008 * the emergency pool wasn't available.
4016 ctl_zero_io(union ctl_io *io)
4024 * May need to preserve linked list pointers at some point too.
4026 pool_ref = io->io_hdr.pool;
4028 memset(io, 0, sizeof(*io));
4030 io->io_hdr.pool = pool_ref;
4034 * This routine is currently used for internal copies of ctl_ios that need
4035 * to persist for some reason after we've already returned status to the
4036 * FETD. (Thus the flag set.)
4039 * Note that this makes a blind copy of all fields in the ctl_io, except
4040 * for the pool reference. This includes any memory that has been
4041 * allocated! That memory will no longer be valid after done has been
4042 * called, so this would be VERY DANGEROUS for command that actually does
4043 * any reads or writes. Right now (11/7/2005), this is only used for immediate
4044 * start and stop commands, which don't transfer any data, so this is not a
4045 * problem. If it is used for anything else, the caller would also need to
4046 * allocate data buffer space and this routine would need to be modified to
4047 * copy the data buffer(s) as well.
4050 ctl_copy_io(union ctl_io *src, union ctl_io *dest)
4059 * May need to preserve linked list pointers at some point too.
4061 pool_ref = dest->io_hdr.pool;
4063 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest)));
4065 dest->io_hdr.pool = pool_ref;
4067 * We need to know that this is an internal copy, and doesn't need
4068 * to get passed back to the FETD that allocated it.
4070 dest->io_hdr.flags |= CTL_FLAG_INT_COPY;
4074 ctl_expand_number(const char *buf, uint64_t *num)
4080 number = strtoq(buf, &endptr, 0);
4082 switch (tolower((unsigned char)*endptr)) {
4102 case '\0': /* No unit. */
4106 /* Unrecognized unit. */
4110 if ((number << shift) >> shift != number) {
4114 *num = number << shift;
4120 * This routine could be used in the future to load default and/or saved
4121 * mode page parameters for a particuar lun.
4124 ctl_init_page_index(struct ctl_lun *lun)
4127 struct ctl_page_index *page_index;
4131 memcpy(&lun->mode_pages.index, page_index_template,
4132 sizeof(page_index_template));
4134 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4136 page_index = &lun->mode_pages.index[i];
4138 * If this is a disk-only mode page, there's no point in
4139 * setting it up. For some pages, we have to have some
4140 * basic information about the disk in order to calculate the
4143 if ((lun->be_lun->lun_type != T_DIRECT)
4144 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
4147 switch (page_index->page_code & SMPH_PC_MASK) {
4148 case SMS_RW_ERROR_RECOVERY_PAGE: {
4149 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4150 panic("subpage is incorrect!");
4151 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
4152 &rw_er_page_default,
4153 sizeof(rw_er_page_default));
4154 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
4155 &rw_er_page_changeable,
4156 sizeof(rw_er_page_changeable));
4157 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
4158 &rw_er_page_default,
4159 sizeof(rw_er_page_default));
4160 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
4161 &rw_er_page_default,
4162 sizeof(rw_er_page_default));
4163 page_index->page_data =
4164 (uint8_t *)lun->mode_pages.rw_er_page;
4167 case SMS_FORMAT_DEVICE_PAGE: {
4168 struct scsi_format_page *format_page;
4170 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4171 panic("subpage is incorrect!");
4174 * Sectors per track are set above. Bytes per
4175 * sector need to be set here on a per-LUN basis.
4177 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
4178 &format_page_default,
4179 sizeof(format_page_default));
4180 memcpy(&lun->mode_pages.format_page[
4181 CTL_PAGE_CHANGEABLE], &format_page_changeable,
4182 sizeof(format_page_changeable));
4183 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
4184 &format_page_default,
4185 sizeof(format_page_default));
4186 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
4187 &format_page_default,
4188 sizeof(format_page_default));
4190 format_page = &lun->mode_pages.format_page[
4192 scsi_ulto2b(lun->be_lun->blocksize,
4193 format_page->bytes_per_sector);
4195 format_page = &lun->mode_pages.format_page[
4197 scsi_ulto2b(lun->be_lun->blocksize,
4198 format_page->bytes_per_sector);
4200 format_page = &lun->mode_pages.format_page[
4202 scsi_ulto2b(lun->be_lun->blocksize,
4203 format_page->bytes_per_sector);
4205 page_index->page_data =
4206 (uint8_t *)lun->mode_pages.format_page;
4209 case SMS_RIGID_DISK_PAGE: {
4210 struct scsi_rigid_disk_page *rigid_disk_page;
4211 uint32_t sectors_per_cylinder;
4215 #endif /* !__XSCALE__ */
4217 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4218 panic("invalid subpage value %d",
4219 page_index->subpage);
4222 * Rotation rate and sectors per track are set
4223 * above. We calculate the cylinders here based on
4224 * capacity. Due to the number of heads and
4225 * sectors per track we're using, smaller arrays
4226 * may turn out to have 0 cylinders. Linux and
4227 * FreeBSD don't pay attention to these mode pages
4228 * to figure out capacity, but Solaris does. It
4229 * seems to deal with 0 cylinders just fine, and
4230 * works out a fake geometry based on the capacity.
4232 memcpy(&lun->mode_pages.rigid_disk_page[
4233 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4234 sizeof(rigid_disk_page_default));
4235 memcpy(&lun->mode_pages.rigid_disk_page[
4236 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4237 sizeof(rigid_disk_page_changeable));
4239 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4243 * The divide method here will be more accurate,
4244 * probably, but results in floating point being
4245 * used in the kernel on i386 (__udivdi3()). On the
4246 * XScale, though, __udivdi3() is implemented in
4249 * The shift method for cylinder calculation is
4250 * accurate if sectors_per_cylinder is a power of
4251 * 2. Otherwise it might be slightly off -- you
4252 * might have a bit of a truncation problem.
4255 cylinders = (lun->be_lun->maxlba + 1) /
4256 sectors_per_cylinder;
4258 for (shift = 31; shift > 0; shift--) {
4259 if (sectors_per_cylinder & (1 << shift))
4262 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4266 * We've basically got 3 bytes, or 24 bits for the
4267 * cylinder size in the mode page. If we're over,
4268 * just round down to 2^24.
4270 if (cylinders > 0xffffff)
4271 cylinders = 0xffffff;
4273 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4275 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4277 if ((value = ctl_get_opt(&lun->be_lun->options,
4279 scsi_ulto2b(strtol(value, NULL, 0),
4280 rigid_disk_page->rotation_rate);
4283 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
4284 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4285 sizeof(rigid_disk_page_default));
4286 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
4287 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4288 sizeof(rigid_disk_page_default));
4290 page_index->page_data =
4291 (uint8_t *)lun->mode_pages.rigid_disk_page;
4294 case SMS_CACHING_PAGE: {
4295 struct scsi_caching_page *caching_page;
4297 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4298 panic("invalid subpage value %d",
4299 page_index->subpage);
4300 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4301 &caching_page_default,
4302 sizeof(caching_page_default));
4303 memcpy(&lun->mode_pages.caching_page[
4304 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4305 sizeof(caching_page_changeable));
4306 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4307 &caching_page_default,
4308 sizeof(caching_page_default));
4309 caching_page = &lun->mode_pages.caching_page[
4311 value = ctl_get_opt(&lun->be_lun->options, "writecache");
4312 if (value != NULL && strcmp(value, "off") == 0)
4313 caching_page->flags1 &= ~SCP_WCE;
4314 value = ctl_get_opt(&lun->be_lun->options, "readcache");
4315 if (value != NULL && strcmp(value, "off") == 0)
4316 caching_page->flags1 |= SCP_RCD;
4317 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4318 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4319 sizeof(caching_page_default));
4320 page_index->page_data =
4321 (uint8_t *)lun->mode_pages.caching_page;
4324 case SMS_CONTROL_MODE_PAGE: {
4325 struct scsi_control_page *control_page;
4327 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4328 panic("invalid subpage value %d",
4329 page_index->subpage);
4331 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT],
4332 &control_page_default,
4333 sizeof(control_page_default));
4334 memcpy(&lun->mode_pages.control_page[
4335 CTL_PAGE_CHANGEABLE], &control_page_changeable,
4336 sizeof(control_page_changeable));
4337 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED],
4338 &control_page_default,
4339 sizeof(control_page_default));
4340 control_page = &lun->mode_pages.control_page[
4342 value = ctl_get_opt(&lun->be_lun->options, "reordering");
4343 if (value != NULL && strcmp(value, "unrestricted") == 0) {
4344 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK;
4345 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED;
4347 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT],
4348 &lun->mode_pages.control_page[CTL_PAGE_SAVED],
4349 sizeof(control_page_default));
4350 page_index->page_data =
4351 (uint8_t *)lun->mode_pages.control_page;
4355 case SMS_INFO_EXCEPTIONS_PAGE: {
4356 switch (page_index->subpage) {
4357 case SMS_SUBPAGE_PAGE_0:
4358 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4360 sizeof(ie_page_default));
4361 memcpy(&lun->mode_pages.ie_page[
4362 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4363 sizeof(ie_page_changeable));
4364 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4366 sizeof(ie_page_default));
4367 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4369 sizeof(ie_page_default));
4370 page_index->page_data =
4371 (uint8_t *)lun->mode_pages.ie_page;
4374 struct ctl_logical_block_provisioning_page *page;
4376 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4378 sizeof(lbp_page_default));
4379 memcpy(&lun->mode_pages.lbp_page[
4380 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4381 sizeof(lbp_page_changeable));
4382 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4384 sizeof(lbp_page_default));
4385 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4386 value = ctl_get_opt(&lun->be_lun->options,
4388 if (value != NULL &&
4389 ctl_expand_number(value, &ival) == 0) {
4390 page->descr[0].flags |= SLBPPD_ENABLED |
4392 if (lun->be_lun->blocksize)
4393 ival /= lun->be_lun->blocksize;
4396 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4397 page->descr[0].count);
4399 value = ctl_get_opt(&lun->be_lun->options,
4401 if (value != NULL &&
4402 ctl_expand_number(value, &ival) == 0) {
4403 page->descr[1].flags |= SLBPPD_ENABLED |
4405 if (lun->be_lun->blocksize)
4406 ival /= lun->be_lun->blocksize;
4409 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4410 page->descr[1].count);
4412 value = ctl_get_opt(&lun->be_lun->options,
4413 "pool-avail-threshold");
4414 if (value != NULL &&
4415 ctl_expand_number(value, &ival) == 0) {
4416 page->descr[2].flags |= SLBPPD_ENABLED |
4418 if (lun->be_lun->blocksize)
4419 ival /= lun->be_lun->blocksize;
4422 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4423 page->descr[2].count);
4425 value = ctl_get_opt(&lun->be_lun->options,
4426 "pool-used-threshold");
4427 if (value != NULL &&
4428 ctl_expand_number(value, &ival) == 0) {
4429 page->descr[3].flags |= SLBPPD_ENABLED |
4431 if (lun->be_lun->blocksize)
4432 ival /= lun->be_lun->blocksize;
4435 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4436 page->descr[3].count);
4438 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4439 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4440 sizeof(lbp_page_default));
4441 page_index->page_data =
4442 (uint8_t *)lun->mode_pages.lbp_page;
4446 case SMS_VENDOR_SPECIFIC_PAGE:{
4447 switch (page_index->subpage) {
4448 case DBGCNF_SUBPAGE_CODE: {
4449 struct copan_debugconf_subpage *current_page,
4452 memcpy(&lun->mode_pages.debugconf_subpage[
4454 &debugconf_page_default,
4455 sizeof(debugconf_page_default));
4456 memcpy(&lun->mode_pages.debugconf_subpage[
4457 CTL_PAGE_CHANGEABLE],
4458 &debugconf_page_changeable,
4459 sizeof(debugconf_page_changeable));
4460 memcpy(&lun->mode_pages.debugconf_subpage[
4462 &debugconf_page_default,
4463 sizeof(debugconf_page_default));
4464 memcpy(&lun->mode_pages.debugconf_subpage[
4466 &debugconf_page_default,
4467 sizeof(debugconf_page_default));
4468 page_index->page_data =
4469 (uint8_t *)lun->mode_pages.debugconf_subpage;
4471 current_page = (struct copan_debugconf_subpage *)
4472 (page_index->page_data +
4473 (page_index->page_len *
4475 saved_page = (struct copan_debugconf_subpage *)
4476 (page_index->page_data +
4477 (page_index->page_len *
4482 panic("invalid subpage value %d",
4483 page_index->subpage);
4489 panic("invalid page value %d",
4490 page_index->page_code & SMPH_PC_MASK);
4495 return (CTL_RETVAL_COMPLETE);
4499 ctl_init_log_page_index(struct ctl_lun *lun)
4501 struct ctl_page_index *page_index;
4504 memcpy(&lun->log_pages.index, log_page_index_template,
4505 sizeof(log_page_index_template));
4508 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4510 page_index = &lun->log_pages.index[i];
4512 * If this is a disk-only mode page, there's no point in
4513 * setting it up. For some pages, we have to have some
4514 * basic information about the disk in order to calculate the
4517 if ((lun->be_lun->lun_type != T_DIRECT)
4518 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
4521 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4522 ((lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) == 0 ||
4523 lun->backend->lun_attr == NULL))
4526 if (page_index->page_code != prev) {
4527 lun->log_pages.pages_page[j] = page_index->page_code;
4528 prev = page_index->page_code;
4531 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4532 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4535 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4536 lun->log_pages.index[0].page_len = j;
4537 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4538 lun->log_pages.index[1].page_len = k * 2;
4539 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4540 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4542 return (CTL_RETVAL_COMPLETE);
4546 hex2bin(const char *str, uint8_t *buf, int buf_size)
4551 memset(buf, 0, buf_size);
4552 while (isspace(str[0]))
4554 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4557 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4561 else if (isalpha(c))
4562 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4568 buf[i / 2] |= (c << 4);
4572 return ((i + 1) / 2);
4579 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4580 * wants us to allocate the LUN and he can block.
4581 * - ctl_softc is always set
4582 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4584 * Returns 0 for success, non-zero (errno) for failure.
4587 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4588 struct ctl_be_lun *const be_lun, struct ctl_id target_id)
4590 struct ctl_lun *nlun, *lun;
4591 struct ctl_port *port;
4592 struct scsi_vpd_id_descriptor *desc;
4593 struct scsi_vpd_id_t10 *t10id;
4594 const char *eui, *naa, *scsiname, *vendor, *value;
4595 int lun_number, i, lun_malloced;
4596 int devidlen, idlen1, idlen2 = 0, len;
4602 * We currently only support Direct Access or Processor LUN types.
4604 switch (be_lun->lun_type) {
4612 be_lun->lun_config_status(be_lun->be_lun,
4613 CTL_LUN_CONFIG_FAILURE);
4616 if (ctl_lun == NULL) {
4617 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4624 memset(lun, 0, sizeof(*lun));
4626 lun->flags = CTL_LUN_MALLOCED;
4628 /* Generate LUN ID. */
4629 devidlen = max(CTL_DEVID_MIN_LEN,
4630 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4631 idlen1 = sizeof(*t10id) + devidlen;
4632 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4633 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4634 if (scsiname != NULL) {
4635 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4636 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4638 eui = ctl_get_opt(&be_lun->options, "eui");
4640 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4642 naa = ctl_get_opt(&be_lun->options, "naa");
4644 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4646 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4647 M_CTL, M_WAITOK | M_ZERO);
4648 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4649 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4650 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4651 desc->length = idlen1;
4652 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4653 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4654 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4655 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4657 strncpy(t10id->vendor, vendor,
4658 min(sizeof(t10id->vendor), strlen(vendor)));
4660 strncpy((char *)t10id->vendor_spec_id,
4661 (char *)be_lun->device_id, devidlen);
4662 if (scsiname != NULL) {
4663 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4665 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4666 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4667 SVPD_ID_TYPE_SCSI_NAME;
4668 desc->length = idlen2;
4669 strlcpy(desc->identifier, scsiname, idlen2);
4672 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4674 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4675 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4677 desc->length = hex2bin(eui, desc->identifier, 16);
4678 desc->length = desc->length > 12 ? 16 :
4679 (desc->length > 8 ? 12 : 8);
4680 len -= 16 - desc->length;
4683 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4685 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4686 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4688 desc->length = hex2bin(naa, desc->identifier, 16);
4689 desc->length = desc->length > 8 ? 16 : 8;
4690 len -= 16 - desc->length;
4692 lun->lun_devid->len = len;
4694 mtx_lock(&ctl_softc->ctl_lock);
4696 * See if the caller requested a particular LUN number. If so, see
4697 * if it is available. Otherwise, allocate the first available LUN.
4699 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4700 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4701 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4702 mtx_unlock(&ctl_softc->ctl_lock);
4703 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4704 printf("ctl: requested LUN ID %d is higher "
4705 "than CTL_MAX_LUNS - 1 (%d)\n",
4706 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4709 * XXX KDM return an error, or just assign
4710 * another LUN ID in this case??
4712 printf("ctl: requested LUN ID %d is already "
4713 "in use\n", be_lun->req_lun_id);
4715 if (lun->flags & CTL_LUN_MALLOCED)
4717 be_lun->lun_config_status(be_lun->be_lun,
4718 CTL_LUN_CONFIG_FAILURE);
4721 lun_number = be_lun->req_lun_id;
4723 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS);
4724 if (lun_number == -1) {
4725 mtx_unlock(&ctl_softc->ctl_lock);
4726 printf("ctl: can't allocate LUN on target %ju, out of "
4727 "LUNs\n", (uintmax_t)target_id.id);
4728 if (lun->flags & CTL_LUN_MALLOCED)
4730 be_lun->lun_config_status(be_lun->be_lun,
4731 CTL_LUN_CONFIG_FAILURE);
4735 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4737 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4738 lun->target = target_id;
4739 lun->lun = lun_number;
4740 lun->be_lun = be_lun;
4742 * The processor LUN is always enabled. Disk LUNs come on line
4743 * disabled, and must be enabled by the backend.
4745 lun->flags |= CTL_LUN_DISABLED;
4746 lun->backend = be_lun->be;
4747 be_lun->ctl_lun = lun;
4748 be_lun->lun_id = lun_number;
4749 atomic_add_int(&be_lun->be->num_luns, 1);
4750 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE)
4751 lun->flags |= CTL_LUN_OFFLINE;
4753 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4754 lun->flags |= CTL_LUN_STOPPED;
4756 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4757 lun->flags |= CTL_LUN_INOPERABLE;
4759 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4760 lun->flags |= CTL_LUN_PRIMARY_SC;
4762 value = ctl_get_opt(&be_lun->options, "readonly");
4763 if (value != NULL && strcmp(value, "on") == 0)
4764 lun->flags |= CTL_LUN_READONLY;
4766 lun->ctl_softc = ctl_softc;
4767 TAILQ_INIT(&lun->ooa_queue);
4768 TAILQ_INIT(&lun->blocked_queue);
4769 STAILQ_INIT(&lun->error_list);
4770 ctl_tpc_lun_init(lun);
4773 * Initialize the mode and log page index.
4775 ctl_init_page_index(lun);
4776 ctl_init_log_page_index(lun);
4779 * Set the poweron UA for all initiators on this LUN only.
4781 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4782 lun->pending_ua[i] = CTL_UA_POWERON;
4785 * Now, before we insert this lun on the lun list, set the lun
4786 * inventory changed UA for all other luns.
4788 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4789 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4790 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE;
4794 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4796 ctl_softc->ctl_luns[lun_number] = lun;
4798 ctl_softc->num_luns++;
4800 /* Setup statistics gathering */
4801 lun->stats.device_type = be_lun->lun_type;
4802 lun->stats.lun_number = lun_number;
4803 if (lun->stats.device_type == T_DIRECT)
4804 lun->stats.blocksize = be_lun->blocksize;
4806 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4807 for (i = 0;i < CTL_MAX_PORTS;i++)
4808 lun->stats.ports[i].targ_port = i;
4810 mtx_unlock(&ctl_softc->ctl_lock);
4812 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4815 * Run through each registered FETD and bring it online if it isn't
4816 * already. Enable the target ID if it hasn't been enabled, and
4817 * enable this particular LUN.
4819 STAILQ_FOREACH(port, &ctl_softc->port_list, links) {
4822 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number);
4824 printf("ctl_alloc_lun: FETD %s port %d returned error "
4825 "%d for lun_enable on target %ju lun %d\n",
4826 port->port_name, port->targ_port, retval,
4827 (uintmax_t)target_id.id, lun_number);
4829 port->status |= CTL_PORT_STATUS_LUN_ONLINE;
4837 * - LUN has already been marked invalid and any pending I/O has been taken
4841 ctl_free_lun(struct ctl_lun *lun)
4843 struct ctl_softc *softc;
4845 struct ctl_port *port;
4847 struct ctl_lun *nlun;
4850 softc = lun->ctl_softc;
4852 mtx_assert(&softc->ctl_lock, MA_OWNED);
4854 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4856 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4858 softc->ctl_luns[lun->lun] = NULL;
4860 if (!TAILQ_EMPTY(&lun->ooa_queue))
4861 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4866 * XXX KDM this scheme only works for a single target/multiple LUN
4867 * setup. It needs to be revamped for a multiple target scheme.
4869 * XXX KDM this results in port->lun_disable() getting called twice,
4870 * once when ctl_disable_lun() is called, and a second time here.
4871 * We really need to re-think the LUN disable semantics. There
4872 * should probably be several steps/levels to LUN removal:
4877 * Right now we only have a disable method when communicating to
4878 * the front end ports, at least for individual LUNs.
4881 STAILQ_FOREACH(port, &softc->port_list, links) {
4884 retval = port->lun_disable(port->targ_lun_arg, lun->target,
4887 printf("ctl_free_lun: FETD %s port %d returned error "
4888 "%d for lun_disable on target %ju lun %jd\n",
4889 port->port_name, port->targ_port, retval,
4890 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4893 if (STAILQ_FIRST(&softc->lun_list) == NULL) {
4894 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE;
4896 retval = port->targ_disable(port->targ_lun_arg,lun->target);
4898 printf("ctl_free_lun: FETD %s port %d "
4899 "returned error %d for targ_disable on "
4900 "target %ju\n", port->port_name,
4901 port->targ_port, retval,
4902 (uintmax_t)lun->target.id);
4904 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE;
4906 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0)
4910 port->port_offline(port->onoff_arg);
4911 port->status &= ~CTL_PORT_STATUS_ONLINE;
4918 * Tell the backend to free resources, if this LUN has a backend.
4920 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4921 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4923 ctl_tpc_lun_shutdown(lun);
4924 mtx_destroy(&lun->lun_lock);
4925 free(lun->lun_devid, M_CTL);
4926 free(lun->write_buffer, M_CTL);
4927 if (lun->flags & CTL_LUN_MALLOCED)
4930 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4931 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4932 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE;
4940 ctl_create_lun(struct ctl_be_lun *be_lun)
4942 struct ctl_softc *ctl_softc;
4944 ctl_softc = control_softc;
4947 * ctl_alloc_lun() should handle all potential failure cases.
4949 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target);
4953 ctl_add_lun(struct ctl_be_lun *be_lun)
4955 struct ctl_softc *ctl_softc = control_softc;
4957 mtx_lock(&ctl_softc->ctl_lock);
4958 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links);
4959 mtx_unlock(&ctl_softc->ctl_lock);
4960 wakeup(&ctl_softc->pending_lun_queue);
4966 ctl_enable_lun(struct ctl_be_lun *be_lun)
4968 struct ctl_softc *ctl_softc;
4969 struct ctl_port *port, *nport;
4970 struct ctl_lun *lun;
4973 ctl_softc = control_softc;
4975 lun = (struct ctl_lun *)be_lun->ctl_lun;
4977 mtx_lock(&ctl_softc->ctl_lock);
4978 mtx_lock(&lun->lun_lock);
4979 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4981 * eh? Why did we get called if the LUN is already
4984 mtx_unlock(&lun->lun_lock);
4985 mtx_unlock(&ctl_softc->ctl_lock);
4988 lun->flags &= ~CTL_LUN_DISABLED;
4989 mtx_unlock(&lun->lun_lock);
4991 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) {
4992 nport = STAILQ_NEXT(port, links);
4995 * Drop the lock while we call the FETD's enable routine.
4996 * This can lead to a callback into CTL (at least in the
4997 * case of the internal initiator frontend.
4999 mtx_unlock(&ctl_softc->ctl_lock);
5000 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun);
5001 mtx_lock(&ctl_softc->ctl_lock);
5003 printf("%s: FETD %s port %d returned error "
5004 "%d for lun_enable on target %ju lun %jd\n",
5005 __func__, port->port_name, port->targ_port, retval,
5006 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
5010 /* NOTE: TODO: why does lun enable affect port status? */
5011 port->status |= CTL_PORT_STATUS_LUN_ONLINE;
5016 mtx_unlock(&ctl_softc->ctl_lock);
5022 ctl_disable_lun(struct ctl_be_lun *be_lun)
5024 struct ctl_softc *ctl_softc;
5025 struct ctl_port *port;
5026 struct ctl_lun *lun;
5029 ctl_softc = control_softc;
5031 lun = (struct ctl_lun *)be_lun->ctl_lun;
5033 mtx_lock(&ctl_softc->ctl_lock);
5034 mtx_lock(&lun->lun_lock);
5035 if (lun->flags & CTL_LUN_DISABLED) {
5036 mtx_unlock(&lun->lun_lock);
5037 mtx_unlock(&ctl_softc->ctl_lock);
5040 lun->flags |= CTL_LUN_DISABLED;
5041 mtx_unlock(&lun->lun_lock);
5043 STAILQ_FOREACH(port, &ctl_softc->port_list, links) {
5044 mtx_unlock(&ctl_softc->ctl_lock);
5046 * Drop the lock before we call the frontend's disable
5047 * routine, to avoid lock order reversals.
5049 * XXX KDM what happens if the frontend list changes while
5050 * we're traversing it? It's unlikely, but should be handled.
5052 retval = port->lun_disable(port->targ_lun_arg, lun->target,
5054 mtx_lock(&ctl_softc->ctl_lock);
5056 printf("ctl_alloc_lun: FETD %s port %d returned error "
5057 "%d for lun_disable on target %ju lun %jd\n",
5058 port->port_name, port->targ_port, retval,
5059 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
5063 mtx_unlock(&ctl_softc->ctl_lock);
5069 ctl_start_lun(struct ctl_be_lun *be_lun)
5071 struct ctl_softc *ctl_softc;
5072 struct ctl_lun *lun;
5074 ctl_softc = control_softc;
5076 lun = (struct ctl_lun *)be_lun->ctl_lun;
5078 mtx_lock(&lun->lun_lock);
5079 lun->flags &= ~CTL_LUN_STOPPED;
5080 mtx_unlock(&lun->lun_lock);
5086 ctl_stop_lun(struct ctl_be_lun *be_lun)
5088 struct ctl_softc *ctl_softc;
5089 struct ctl_lun *lun;
5091 ctl_softc = control_softc;
5093 lun = (struct ctl_lun *)be_lun->ctl_lun;
5095 mtx_lock(&lun->lun_lock);
5096 lun->flags |= CTL_LUN_STOPPED;
5097 mtx_unlock(&lun->lun_lock);
5103 ctl_lun_offline(struct ctl_be_lun *be_lun)
5105 struct ctl_softc *ctl_softc;
5106 struct ctl_lun *lun;
5108 ctl_softc = control_softc;
5110 lun = (struct ctl_lun *)be_lun->ctl_lun;
5112 mtx_lock(&lun->lun_lock);
5113 lun->flags |= CTL_LUN_OFFLINE;
5114 mtx_unlock(&lun->lun_lock);
5120 ctl_lun_online(struct ctl_be_lun *be_lun)
5122 struct ctl_softc *ctl_softc;
5123 struct ctl_lun *lun;
5125 ctl_softc = control_softc;
5127 lun = (struct ctl_lun *)be_lun->ctl_lun;
5129 mtx_lock(&lun->lun_lock);
5130 lun->flags &= ~CTL_LUN_OFFLINE;
5131 mtx_unlock(&lun->lun_lock);
5137 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
5139 struct ctl_softc *ctl_softc;
5140 struct ctl_lun *lun;
5142 ctl_softc = control_softc;
5144 lun = (struct ctl_lun *)be_lun->ctl_lun;
5146 mtx_lock(&lun->lun_lock);
5149 * The LUN needs to be disabled before it can be marked invalid.
5151 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
5152 mtx_unlock(&lun->lun_lock);
5156 * Mark the LUN invalid.
5158 lun->flags |= CTL_LUN_INVALID;
5161 * If there is nothing in the OOA queue, go ahead and free the LUN.
5162 * If we have something in the OOA queue, we'll free it when the
5163 * last I/O completes.
5165 if (TAILQ_EMPTY(&lun->ooa_queue)) {
5166 mtx_unlock(&lun->lun_lock);
5167 mtx_lock(&ctl_softc->ctl_lock);
5169 mtx_unlock(&ctl_softc->ctl_lock);
5171 mtx_unlock(&lun->lun_lock);
5177 ctl_lun_inoperable(struct ctl_be_lun *be_lun)
5179 struct ctl_softc *ctl_softc;
5180 struct ctl_lun *lun;
5182 ctl_softc = control_softc;
5183 lun = (struct ctl_lun *)be_lun->ctl_lun;
5185 mtx_lock(&lun->lun_lock);
5186 lun->flags |= CTL_LUN_INOPERABLE;
5187 mtx_unlock(&lun->lun_lock);
5193 ctl_lun_operable(struct ctl_be_lun *be_lun)
5195 struct ctl_softc *ctl_softc;
5196 struct ctl_lun *lun;
5198 ctl_softc = control_softc;
5199 lun = (struct ctl_lun *)be_lun->ctl_lun;
5201 mtx_lock(&lun->lun_lock);
5202 lun->flags &= ~CTL_LUN_INOPERABLE;
5203 mtx_unlock(&lun->lun_lock);
5209 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
5211 struct ctl_lun *lun;
5212 struct ctl_softc *softc;
5215 softc = control_softc;
5217 lun = (struct ctl_lun *)be_lun->ctl_lun;
5219 mtx_lock(&lun->lun_lock);
5221 for (i = 0; i < CTL_MAX_INITIATORS; i++)
5222 lun->pending_ua[i] |= CTL_UA_CAPACITY_CHANGED;
5224 mtx_unlock(&lun->lun_lock);
5228 * Backend "memory move is complete" callback for requests that never
5229 * make it down to say RAIDCore's configuration code.
5232 ctl_config_move_done(union ctl_io *io)
5236 retval = CTL_RETVAL_COMPLETE;
5239 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
5241 * XXX KDM this shouldn't happen, but what if it does?
5243 if (io->io_hdr.io_type != CTL_IO_SCSI)
5244 panic("I/O type isn't CTL_IO_SCSI!");
5246 if ((io->io_hdr.port_status == 0)
5247 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
5248 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE))
5249 io->io_hdr.status = CTL_SUCCESS;
5250 else if ((io->io_hdr.port_status != 0)
5251 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
5252 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){
5254 * For hardware error sense keys, the sense key
5255 * specific value is defined to be a retry count,
5256 * but we use it to pass back an internal FETD
5257 * error code. XXX KDM Hopefully the FETD is only
5258 * using 16 bits for an error code, since that's
5259 * all the space we have in the sks field.
5261 ctl_set_internal_failure(&io->scsiio,
5264 io->io_hdr.port_status);
5265 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5266 free(io->scsiio.kern_data_ptr, M_CTL);
5271 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
5272 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
5273 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5275 * XXX KDM just assuming a single pointer here, and not a
5276 * S/G list. If we start using S/G lists for config data,
5277 * we'll need to know how to clean them up here as well.
5279 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5280 free(io->scsiio.kern_data_ptr, M_CTL);
5281 /* Hopefully the user has already set the status... */
5285 * XXX KDM now we need to continue data movement. Some
5287 * - call ctl_scsiio() again? We don't do this for data
5288 * writes, because for those at least we know ahead of
5289 * time where the write will go and how long it is. For
5290 * config writes, though, that information is largely
5291 * contained within the write itself, thus we need to
5292 * parse out the data again.
5294 * - Call some other function once the data is in?
5296 if (ctl_debug & CTL_DEBUG_CDB_DATA)
5300 * XXX KDM call ctl_scsiio() again for now, and check flag
5301 * bits to see whether we're allocated or not.
5303 retval = ctl_scsiio(&io->scsiio);
5310 * This gets called by a backend driver when it is done with a
5311 * data_submit method.
5314 ctl_data_submit_done(union ctl_io *io)
5317 * If the IO_CONT flag is set, we need to call the supplied
5318 * function to continue processing the I/O, instead of completing
5321 * If there is an error, though, we don't want to keep processing.
5322 * Instead, just send status back to the initiator.
5324 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5325 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5326 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5327 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5328 io->scsiio.io_cont(io);
5335 * This gets called by a backend driver when it is done with a
5336 * configuration write.
5339 ctl_config_write_done(union ctl_io *io)
5344 * If the IO_CONT flag is set, we need to call the supplied
5345 * function to continue processing the I/O, instead of completing
5348 * If there is an error, though, we don't want to keep processing.
5349 * Instead, just send status back to the initiator.
5351 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5352 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5353 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5354 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5355 io->scsiio.io_cont(io);
5359 * Since a configuration write can be done for commands that actually
5360 * have data allocated, like write buffer, and commands that have
5361 * no data, like start/stop unit, we need to check here.
5363 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5364 buf = io->scsiio.kern_data_ptr;
5373 * SCSI release command.
5376 ctl_scsi_release(struct ctl_scsiio *ctsio)
5378 int length, longid, thirdparty_id, resv_id;
5379 struct ctl_softc *ctl_softc;
5380 struct ctl_lun *lun;
5386 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5388 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5389 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5390 ctl_softc = control_softc;
5392 switch (ctsio->cdb[0]) {
5394 struct scsi_release_10 *cdb;
5396 cdb = (struct scsi_release_10 *)ctsio->cdb;
5398 if (cdb->byte2 & SR10_LONGID)
5401 thirdparty_id = cdb->thirdparty_id;
5403 resv_id = cdb->resv_id;
5404 length = scsi_2btoul(cdb->length);
5411 * XXX KDM right now, we only support LUN reservation. We don't
5412 * support 3rd party reservations, or extent reservations, which
5413 * might actually need the parameter list. If we've gotten this
5414 * far, we've got a LUN reservation. Anything else got kicked out
5415 * above. So, according to SPC, ignore the length.
5419 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5421 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5422 ctsio->kern_data_len = length;
5423 ctsio->kern_total_len = length;
5424 ctsio->kern_data_resid = 0;
5425 ctsio->kern_rel_offset = 0;
5426 ctsio->kern_sg_entries = 0;
5427 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5428 ctsio->be_move_done = ctl_config_move_done;
5429 ctl_datamove((union ctl_io *)ctsio);
5431 return (CTL_RETVAL_COMPLETE);
5435 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5437 mtx_lock(&lun->lun_lock);
5440 * According to SPC, it is not an error for an intiator to attempt
5441 * to release a reservation on a LUN that isn't reserved, or that
5442 * is reserved by another initiator. The reservation can only be
5443 * released, though, by the initiator who made it or by one of
5444 * several reset type events.
5446 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5447 lun->flags &= ~CTL_LUN_RESERVED;
5449 mtx_unlock(&lun->lun_lock);
5451 ctsio->scsi_status = SCSI_STATUS_OK;
5452 ctsio->io_hdr.status = CTL_SUCCESS;
5454 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5455 free(ctsio->kern_data_ptr, M_CTL);
5456 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5459 ctl_done((union ctl_io *)ctsio);
5460 return (CTL_RETVAL_COMPLETE);
5464 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5466 int extent, thirdparty, longid;
5467 int resv_id, length;
5468 uint64_t thirdparty_id;
5469 struct ctl_softc *ctl_softc;
5470 struct ctl_lun *lun;
5480 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5482 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5483 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5484 ctl_softc = control_softc;
5486 switch (ctsio->cdb[0]) {
5488 struct scsi_reserve_10 *cdb;
5490 cdb = (struct scsi_reserve_10 *)ctsio->cdb;
5492 if (cdb->byte2 & SR10_LONGID)
5495 thirdparty_id = cdb->thirdparty_id;
5497 resv_id = cdb->resv_id;
5498 length = scsi_2btoul(cdb->length);
5504 * XXX KDM right now, we only support LUN reservation. We don't
5505 * support 3rd party reservations, or extent reservations, which
5506 * might actually need the parameter list. If we've gotten this
5507 * far, we've got a LUN reservation. Anything else got kicked out
5508 * above. So, according to SPC, ignore the length.
5512 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5514 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5515 ctsio->kern_data_len = length;
5516 ctsio->kern_total_len = length;
5517 ctsio->kern_data_resid = 0;
5518 ctsio->kern_rel_offset = 0;
5519 ctsio->kern_sg_entries = 0;
5520 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5521 ctsio->be_move_done = ctl_config_move_done;
5522 ctl_datamove((union ctl_io *)ctsio);
5524 return (CTL_RETVAL_COMPLETE);
5528 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5530 mtx_lock(&lun->lun_lock);
5531 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5532 ctl_set_reservation_conflict(ctsio);
5536 lun->flags |= CTL_LUN_RESERVED;
5537 lun->res_idx = residx;
5539 ctsio->scsi_status = SCSI_STATUS_OK;
5540 ctsio->io_hdr.status = CTL_SUCCESS;
5543 mtx_unlock(&lun->lun_lock);
5545 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5546 free(ctsio->kern_data_ptr, M_CTL);
5547 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5550 ctl_done((union ctl_io *)ctsio);
5551 return (CTL_RETVAL_COMPLETE);
5555 ctl_start_stop(struct ctl_scsiio *ctsio)
5557 struct scsi_start_stop_unit *cdb;
5558 struct ctl_lun *lun;
5559 struct ctl_softc *ctl_softc;
5562 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5564 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5565 ctl_softc = control_softc;
5568 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5572 * We don't support the immediate bit on a stop unit. In order to
5573 * do that, we would need to code up a way to know that a stop is
5574 * pending, and hold off any new commands until it completes, one
5575 * way or another. Then we could accept or reject those commands
5576 * depending on its status. We would almost need to do the reverse
5577 * of what we do below for an immediate start -- return the copy of
5578 * the ctl_io to the FETD with status to send to the host (and to
5579 * free the copy!) and then free the original I/O once the stop
5580 * actually completes. That way, the OOA queue mechanism can work
5581 * to block commands that shouldn't proceed. Another alternative
5582 * would be to put the copy in the queue in place of the original,
5583 * and return the original back to the caller. That could be
5586 if ((cdb->byte2 & SSS_IMMED)
5587 && ((cdb->how & SSS_START) == 0)) {
5588 ctl_set_invalid_field(ctsio,
5594 ctl_done((union ctl_io *)ctsio);
5595 return (CTL_RETVAL_COMPLETE);
5598 if ((lun->flags & CTL_LUN_PR_RESERVED)
5599 && ((cdb->how & SSS_START)==0)) {
5602 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5603 if (lun->pr_keys[residx] == 0
5604 || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5606 ctl_set_reservation_conflict(ctsio);
5607 ctl_done((union ctl_io *)ctsio);
5608 return (CTL_RETVAL_COMPLETE);
5613 * If there is no backend on this device, we can't start or stop
5614 * it. In theory we shouldn't get any start/stop commands in the
5615 * first place at this level if the LUN doesn't have a backend.
5616 * That should get stopped by the command decode code.
5618 if (lun->backend == NULL) {
5619 ctl_set_invalid_opcode(ctsio);
5620 ctl_done((union ctl_io *)ctsio);
5621 return (CTL_RETVAL_COMPLETE);
5625 * XXX KDM Copan-specific offline behavior.
5626 * Figure out a reasonable way to port this?
5629 mtx_lock(&lun->lun_lock);
5631 if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
5632 && (lun->flags & CTL_LUN_OFFLINE)) {
5634 * If the LUN is offline, and the on/offline bit isn't set,
5635 * reject the start or stop. Otherwise, let it through.
5637 mtx_unlock(&lun->lun_lock);
5638 ctl_set_lun_not_ready(ctsio);
5639 ctl_done((union ctl_io *)ctsio);
5641 mtx_unlock(&lun->lun_lock);
5642 #endif /* NEEDTOPORT */
5644 * This could be a start or a stop when we're online,
5645 * or a stop/offline or start/online. A start or stop when
5646 * we're offline is covered in the case above.
5649 * In the non-immediate case, we send the request to
5650 * the backend and return status to the user when
5653 * In the immediate case, we allocate a new ctl_io
5654 * to hold a copy of the request, and send that to
5655 * the backend. We then set good status on the
5656 * user's request and return it immediately.
5658 if (cdb->byte2 & SSS_IMMED) {
5659 union ctl_io *new_io;
5661 new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5662 if (new_io == NULL) {
5663 ctl_set_busy(ctsio);
5664 ctl_done((union ctl_io *)ctsio);
5666 ctl_copy_io((union ctl_io *)ctsio,
5668 retval = lun->backend->config_write(new_io);
5669 ctl_set_success(ctsio);
5670 ctl_done((union ctl_io *)ctsio);
5673 retval = lun->backend->config_write(
5674 (union ctl_io *)ctsio);
5683 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5684 * we don't really do anything with the LBA and length fields if the user
5685 * passes them in. Instead we'll just flush out the cache for the entire
5689 ctl_sync_cache(struct ctl_scsiio *ctsio)
5691 struct ctl_lun *lun;
5692 struct ctl_softc *ctl_softc;
5693 uint64_t starting_lba;
5694 uint32_t block_count;
5697 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5699 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5700 ctl_softc = control_softc;
5703 switch (ctsio->cdb[0]) {
5704 case SYNCHRONIZE_CACHE: {
5705 struct scsi_sync_cache *cdb;
5706 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5708 starting_lba = scsi_4btoul(cdb->begin_lba);
5709 block_count = scsi_2btoul(cdb->lb_count);
5712 case SYNCHRONIZE_CACHE_16: {
5713 struct scsi_sync_cache_16 *cdb;
5714 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5716 starting_lba = scsi_8btou64(cdb->begin_lba);
5717 block_count = scsi_4btoul(cdb->lb_count);
5721 ctl_set_invalid_opcode(ctsio);
5722 ctl_done((union ctl_io *)ctsio);
5724 break; /* NOTREACHED */
5728 * We check the LBA and length, but don't do anything with them.
5729 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5730 * get flushed. This check will just help satisfy anyone who wants
5731 * to see an error for an out of range LBA.
5733 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5734 ctl_set_lba_out_of_range(ctsio);
5735 ctl_done((union ctl_io *)ctsio);
5740 * If this LUN has no backend, we can't flush the cache anyway.
5742 if (lun->backend == NULL) {
5743 ctl_set_invalid_opcode(ctsio);
5744 ctl_done((union ctl_io *)ctsio);
5749 * Check to see whether we're configured to send the SYNCHRONIZE
5750 * CACHE command directly to the back end.
5752 mtx_lock(&lun->lun_lock);
5753 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC)
5754 && (++(lun->sync_count) >= lun->sync_interval)) {
5755 lun->sync_count = 0;
5756 mtx_unlock(&lun->lun_lock);
5757 retval = lun->backend->config_write((union ctl_io *)ctsio);
5759 mtx_unlock(&lun->lun_lock);
5760 ctl_set_success(ctsio);
5761 ctl_done((union ctl_io *)ctsio);
5770 ctl_format(struct ctl_scsiio *ctsio)
5772 struct scsi_format *cdb;
5773 struct ctl_lun *lun;
5774 struct ctl_softc *ctl_softc;
5775 int length, defect_list_len;
5777 CTL_DEBUG_PRINT(("ctl_format\n"));
5779 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5780 ctl_softc = control_softc;
5782 cdb = (struct scsi_format *)ctsio->cdb;
5785 if (cdb->byte2 & SF_FMTDATA) {
5786 if (cdb->byte2 & SF_LONGLIST)
5787 length = sizeof(struct scsi_format_header_long);
5789 length = sizeof(struct scsi_format_header_short);
5792 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5794 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5795 ctsio->kern_data_len = length;
5796 ctsio->kern_total_len = length;
5797 ctsio->kern_data_resid = 0;
5798 ctsio->kern_rel_offset = 0;
5799 ctsio->kern_sg_entries = 0;
5800 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5801 ctsio->be_move_done = ctl_config_move_done;
5802 ctl_datamove((union ctl_io *)ctsio);
5804 return (CTL_RETVAL_COMPLETE);
5807 defect_list_len = 0;
5809 if (cdb->byte2 & SF_FMTDATA) {
5810 if (cdb->byte2 & SF_LONGLIST) {
5811 struct scsi_format_header_long *header;
5813 header = (struct scsi_format_header_long *)
5814 ctsio->kern_data_ptr;
5816 defect_list_len = scsi_4btoul(header->defect_list_len);
5817 if (defect_list_len != 0) {
5818 ctl_set_invalid_field(ctsio,
5827 struct scsi_format_header_short *header;
5829 header = (struct scsi_format_header_short *)
5830 ctsio->kern_data_ptr;
5832 defect_list_len = scsi_2btoul(header->defect_list_len);
5833 if (defect_list_len != 0) {
5834 ctl_set_invalid_field(ctsio,
5846 * The format command will clear out the "Medium format corrupted"
5847 * status if set by the configuration code. That status is really
5848 * just a way to notify the host that we have lost the media, and
5849 * get them to issue a command that will basically make them think
5850 * they're blowing away the media.
5852 mtx_lock(&lun->lun_lock);
5853 lun->flags &= ~CTL_LUN_INOPERABLE;
5854 mtx_unlock(&lun->lun_lock);
5856 ctsio->scsi_status = SCSI_STATUS_OK;
5857 ctsio->io_hdr.status = CTL_SUCCESS;
5860 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5861 free(ctsio->kern_data_ptr, M_CTL);
5862 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5865 ctl_done((union ctl_io *)ctsio);
5866 return (CTL_RETVAL_COMPLETE);
5870 ctl_read_buffer(struct ctl_scsiio *ctsio)
5872 struct scsi_read_buffer *cdb;
5873 struct ctl_lun *lun;
5874 int buffer_offset, len;
5875 static uint8_t descr[4];
5876 static uint8_t echo_descr[4] = { 0 };
5878 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5880 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5881 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5883 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA &&
5884 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR &&
5885 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) {
5886 ctl_set_invalid_field(ctsio,
5892 ctl_done((union ctl_io *)ctsio);
5893 return (CTL_RETVAL_COMPLETE);
5896 len = scsi_3btoul(cdb->length);
5897 buffer_offset = scsi_3btoul(cdb->offset);
5899 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5900 ctl_set_invalid_field(ctsio,
5906 ctl_done((union ctl_io *)ctsio);
5907 return (CTL_RETVAL_COMPLETE);
5910 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5912 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5913 ctsio->kern_data_ptr = descr;
5914 len = min(len, sizeof(descr));
5915 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5916 ctsio->kern_data_ptr = echo_descr;
5917 len = min(len, sizeof(echo_descr));
5919 if (lun->write_buffer == NULL) {
5920 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5923 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5925 ctsio->kern_data_len = len;
5926 ctsio->kern_total_len = len;
5927 ctsio->kern_data_resid = 0;
5928 ctsio->kern_rel_offset = 0;
5929 ctsio->kern_sg_entries = 0;
5930 ctsio->be_move_done = ctl_config_move_done;
5931 ctl_datamove((union ctl_io *)ctsio);
5933 return (CTL_RETVAL_COMPLETE);
5937 ctl_write_buffer(struct ctl_scsiio *ctsio)
5939 struct scsi_write_buffer *cdb;
5940 struct ctl_lun *lun;
5941 int buffer_offset, len;
5943 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5945 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5946 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5948 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5949 ctl_set_invalid_field(ctsio,
5955 ctl_done((union ctl_io *)ctsio);
5956 return (CTL_RETVAL_COMPLETE);
5959 len = scsi_3btoul(cdb->length);
5960 buffer_offset = scsi_3btoul(cdb->offset);
5962 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5963 ctl_set_invalid_field(ctsio,
5969 ctl_done((union ctl_io *)ctsio);
5970 return (CTL_RETVAL_COMPLETE);
5974 * If we've got a kernel request that hasn't been malloced yet,
5975 * malloc it and tell the caller the data buffer is here.
5977 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5978 if (lun->write_buffer == NULL) {
5979 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5982 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5983 ctsio->kern_data_len = len;
5984 ctsio->kern_total_len = len;
5985 ctsio->kern_data_resid = 0;
5986 ctsio->kern_rel_offset = 0;
5987 ctsio->kern_sg_entries = 0;
5988 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5989 ctsio->be_move_done = ctl_config_move_done;
5990 ctl_datamove((union ctl_io *)ctsio);
5992 return (CTL_RETVAL_COMPLETE);
5995 ctl_done((union ctl_io *)ctsio);
5997 return (CTL_RETVAL_COMPLETE);
6001 ctl_write_same(struct ctl_scsiio *ctsio)
6003 struct ctl_lun *lun;
6004 struct ctl_lba_len_flags *lbalen;
6006 uint32_t num_blocks;
6010 retval = CTL_RETVAL_COMPLETE;
6012 CTL_DEBUG_PRINT(("ctl_write_same\n"));
6014 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6016 switch (ctsio->cdb[0]) {
6017 case WRITE_SAME_10: {
6018 struct scsi_write_same_10 *cdb;
6020 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
6022 lba = scsi_4btoul(cdb->addr);
6023 num_blocks = scsi_2btoul(cdb->length);
6027 case WRITE_SAME_16: {
6028 struct scsi_write_same_16 *cdb;
6030 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
6032 lba = scsi_8btou64(cdb->addr);
6033 num_blocks = scsi_4btoul(cdb->length);
6039 * We got a command we don't support. This shouldn't
6040 * happen, commands should be filtered out above us.
6042 ctl_set_invalid_opcode(ctsio);
6043 ctl_done((union ctl_io *)ctsio);
6045 return (CTL_RETVAL_COMPLETE);
6046 break; /* NOTREACHED */
6049 /* NDOB and ANCHOR flags can be used only together with UNMAP */
6050 if ((byte2 & SWS_UNMAP) == 0 &&
6051 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) {
6052 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
6053 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
6054 ctl_done((union ctl_io *)ctsio);
6055 return (CTL_RETVAL_COMPLETE);
6059 * The first check is to make sure we're in bounds, the second
6060 * check is to catch wrap-around problems. If the lba + num blocks
6061 * is less than the lba, then we've wrapped around and the block
6062 * range is invalid anyway.
6064 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
6065 || ((lba + num_blocks) < lba)) {
6066 ctl_set_lba_out_of_range(ctsio);
6067 ctl_done((union ctl_io *)ctsio);
6068 return (CTL_RETVAL_COMPLETE);
6071 /* Zero number of blocks means "to the last logical block" */
6072 if (num_blocks == 0) {
6073 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
6074 ctl_set_invalid_field(ctsio,
6080 ctl_done((union ctl_io *)ctsio);
6081 return (CTL_RETVAL_COMPLETE);
6083 num_blocks = (lun->be_lun->maxlba + 1) - lba;
6086 len = lun->be_lun->blocksize;
6089 * If we've got a kernel request that hasn't been malloced yet,
6090 * malloc it and tell the caller the data buffer is here.
6092 if ((byte2 & SWS_NDOB) == 0 &&
6093 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6094 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
6095 ctsio->kern_data_len = len;
6096 ctsio->kern_total_len = len;
6097 ctsio->kern_data_resid = 0;
6098 ctsio->kern_rel_offset = 0;
6099 ctsio->kern_sg_entries = 0;
6100 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6101 ctsio->be_move_done = ctl_config_move_done;
6102 ctl_datamove((union ctl_io *)ctsio);
6104 return (CTL_RETVAL_COMPLETE);
6107 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
6109 lbalen->len = num_blocks;
6110 lbalen->flags = byte2;
6111 retval = lun->backend->config_write((union ctl_io *)ctsio);
6117 ctl_unmap(struct ctl_scsiio *ctsio)
6119 struct ctl_lun *lun;
6120 struct scsi_unmap *cdb;
6121 struct ctl_ptr_len_flags *ptrlen;
6122 struct scsi_unmap_header *hdr;
6123 struct scsi_unmap_desc *buf, *end, *endnz, *range;
6125 uint32_t num_blocks;
6129 retval = CTL_RETVAL_COMPLETE;
6131 CTL_DEBUG_PRINT(("ctl_unmap\n"));
6133 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6134 cdb = (struct scsi_unmap *)ctsio->cdb;
6136 len = scsi_2btoul(cdb->length);
6140 * If we've got a kernel request that hasn't been malloced yet,
6141 * malloc it and tell the caller the data buffer is here.
6143 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6144 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
6145 ctsio->kern_data_len = len;
6146 ctsio->kern_total_len = len;
6147 ctsio->kern_data_resid = 0;
6148 ctsio->kern_rel_offset = 0;
6149 ctsio->kern_sg_entries = 0;
6150 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6151 ctsio->be_move_done = ctl_config_move_done;
6152 ctl_datamove((union ctl_io *)ctsio);
6154 return (CTL_RETVAL_COMPLETE);
6157 len = ctsio->kern_total_len - ctsio->kern_data_resid;
6158 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
6159 if (len < sizeof (*hdr) ||
6160 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
6161 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
6162 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
6163 ctl_set_invalid_field(ctsio,
6171 len = scsi_2btoul(hdr->desc_length);
6172 buf = (struct scsi_unmap_desc *)(hdr + 1);
6173 end = buf + len / sizeof(*buf);
6176 for (range = buf; range < end; range++) {
6177 lba = scsi_8btou64(range->lba);
6178 num_blocks = scsi_4btoul(range->length);
6179 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
6180 || ((lba + num_blocks) < lba)) {
6181 ctl_set_lba_out_of_range(ctsio);
6182 ctl_done((union ctl_io *)ctsio);
6183 return (CTL_RETVAL_COMPLETE);
6185 if (num_blocks != 0)
6190 * Block backend can not handle zero last range.
6191 * Filter it out and return if there is nothing left.
6193 len = (uint8_t *)endnz - (uint8_t *)buf;
6195 ctl_set_success(ctsio);
6199 mtx_lock(&lun->lun_lock);
6200 ptrlen = (struct ctl_ptr_len_flags *)
6201 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
6202 ptrlen->ptr = (void *)buf;
6204 ptrlen->flags = byte2;
6205 ctl_check_blocked(lun);
6206 mtx_unlock(&lun->lun_lock);
6208 retval = lun->backend->config_write((union ctl_io *)ctsio);
6212 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
6213 free(ctsio->kern_data_ptr, M_CTL);
6214 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
6216 ctl_done((union ctl_io *)ctsio);
6217 return (CTL_RETVAL_COMPLETE);
6221 * Note that this function currently doesn't actually do anything inside
6222 * CTL to enforce things if the DQue bit is turned on.
6224 * Also note that this function can't be used in the default case, because
6225 * the DQue bit isn't set in the changeable mask for the control mode page
6226 * anyway. This is just here as an example for how to implement a page
6227 * handler, and a placeholder in case we want to allow the user to turn
6228 * tagged queueing on and off.
6230 * The D_SENSE bit handling is functional, however, and will turn
6231 * descriptor sense on and off for a given LUN.
6234 ctl_control_page_handler(struct ctl_scsiio *ctsio,
6235 struct ctl_page_index *page_index, uint8_t *page_ptr)
6237 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
6238 struct ctl_lun *lun;
6239 struct ctl_softc *softc;
6243 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6244 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6247 user_cp = (struct scsi_control_page *)page_ptr;
6248 current_cp = (struct scsi_control_page *)
6249 (page_index->page_data + (page_index->page_len *
6251 saved_cp = (struct scsi_control_page *)
6252 (page_index->page_data + (page_index->page_len *
6255 softc = control_softc;
6257 mtx_lock(&lun->lun_lock);
6258 if (((current_cp->rlec & SCP_DSENSE) == 0)
6259 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
6261 * Descriptor sense is currently turned off and the user
6262 * wants to turn it on.
6264 current_cp->rlec |= SCP_DSENSE;
6265 saved_cp->rlec |= SCP_DSENSE;
6266 lun->flags |= CTL_LUN_SENSE_DESC;
6268 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
6269 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
6271 * Descriptor sense is currently turned on, and the user
6272 * wants to turn it off.
6274 current_cp->rlec &= ~SCP_DSENSE;
6275 saved_cp->rlec &= ~SCP_DSENSE;
6276 lun->flags &= ~CTL_LUN_SENSE_DESC;
6279 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) !=
6280 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) {
6281 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
6282 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
6283 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
6284 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
6287 if ((current_cp->eca_and_aen & SCP_SWP) !=
6288 (user_cp->eca_and_aen & SCP_SWP)) {
6289 current_cp->eca_and_aen &= ~SCP_SWP;
6290 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
6291 saved_cp->eca_and_aen &= ~SCP_SWP;
6292 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
6298 * Let other initiators know that the mode
6299 * parameters for this LUN have changed.
6301 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
6305 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE;
6308 mtx_unlock(&lun->lun_lock);
6314 ctl_caching_sp_handler(struct ctl_scsiio *ctsio,
6315 struct ctl_page_index *page_index, uint8_t *page_ptr)
6317 struct scsi_caching_page *current_cp, *saved_cp, *user_cp;
6318 struct ctl_lun *lun;
6322 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6323 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6326 user_cp = (struct scsi_caching_page *)page_ptr;
6327 current_cp = (struct scsi_caching_page *)
6328 (page_index->page_data + (page_index->page_len *
6330 saved_cp = (struct scsi_caching_page *)
6331 (page_index->page_data + (page_index->page_len *
6334 mtx_lock(&lun->lun_lock);
6335 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) !=
6336 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) {
6337 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
6338 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
6339 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
6340 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
6346 * Let other initiators know that the mode
6347 * parameters for this LUN have changed.
6349 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
6353 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE;
6356 mtx_unlock(&lun->lun_lock);
6362 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
6363 struct ctl_page_index *page_index,
6369 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
6374 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
6375 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
6376 printf("page data:");
6378 printf(" %.2x",page_ptr[i]);
6384 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
6385 struct ctl_page_index *page_index,
6388 struct copan_debugconf_subpage *page;
6390 page = (struct copan_debugconf_subpage *)page_index->page_data +
6391 (page_index->page_len * pc);
6394 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6395 case SMS_PAGE_CTRL_DEFAULT >> 6:
6396 case SMS_PAGE_CTRL_SAVED >> 6:
6398 * We don't update the changable or default bits for this page.
6401 case SMS_PAGE_CTRL_CURRENT >> 6:
6402 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
6403 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
6407 EPRINT(0, "Invalid PC %d!!", pc);
6408 #endif /* NEEDTOPORT */
6416 ctl_do_mode_select(union ctl_io *io)
6418 struct scsi_mode_page_header *page_header;
6419 struct ctl_page_index *page_index;
6420 struct ctl_scsiio *ctsio;
6421 int control_dev, page_len;
6422 int page_len_offset, page_len_size;
6423 union ctl_modepage_info *modepage_info;
6424 struct ctl_lun *lun;
6425 int *len_left, *len_used;
6428 ctsio = &io->scsiio;
6431 retval = CTL_RETVAL_COMPLETE;
6433 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6435 if (lun->be_lun->lun_type != T_DIRECT)
6440 modepage_info = (union ctl_modepage_info *)
6441 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6442 len_left = &modepage_info->header.len_left;
6443 len_used = &modepage_info->header.len_used;
6447 page_header = (struct scsi_mode_page_header *)
6448 (ctsio->kern_data_ptr + *len_used);
6450 if (*len_left == 0) {
6451 free(ctsio->kern_data_ptr, M_CTL);
6452 ctl_set_success(ctsio);
6453 ctl_done((union ctl_io *)ctsio);
6454 return (CTL_RETVAL_COMPLETE);
6455 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6457 free(ctsio->kern_data_ptr, M_CTL);
6458 ctl_set_param_len_error(ctsio);
6459 ctl_done((union ctl_io *)ctsio);
6460 return (CTL_RETVAL_COMPLETE);
6462 } else if ((page_header->page_code & SMPH_SPF)
6463 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6465 free(ctsio->kern_data_ptr, M_CTL);
6466 ctl_set_param_len_error(ctsio);
6467 ctl_done((union ctl_io *)ctsio);
6468 return (CTL_RETVAL_COMPLETE);
6473 * XXX KDM should we do something with the block descriptor?
6475 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6477 if ((control_dev != 0)
6478 && (lun->mode_pages.index[i].page_flags &
6479 CTL_PAGE_FLAG_DISK_ONLY))
6482 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
6483 (page_header->page_code & SMPH_PC_MASK))
6487 * If neither page has a subpage code, then we've got a
6490 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
6491 && ((page_header->page_code & SMPH_SPF) == 0)) {
6492 page_index = &lun->mode_pages.index[i];
6493 page_len = page_header->page_length;
6498 * If both pages have subpages, then the subpage numbers
6501 if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
6502 && (page_header->page_code & SMPH_SPF)) {
6503 struct scsi_mode_page_header_sp *sph;
6505 sph = (struct scsi_mode_page_header_sp *)page_header;
6507 if (lun->mode_pages.index[i].subpage ==
6509 page_index = &lun->mode_pages.index[i];
6510 page_len = scsi_2btoul(sph->page_length);
6517 * If we couldn't find the page, or if we don't have a mode select
6518 * handler for it, send back an error to the user.
6520 if ((page_index == NULL)
6521 || (page_index->select_handler == NULL)) {
6522 ctl_set_invalid_field(ctsio,
6525 /*field*/ *len_used,
6528 free(ctsio->kern_data_ptr, M_CTL);
6529 ctl_done((union ctl_io *)ctsio);
6530 return (CTL_RETVAL_COMPLETE);
6533 if (page_index->page_code & SMPH_SPF) {
6534 page_len_offset = 2;
6538 page_len_offset = 1;
6542 * If the length the initiator gives us isn't the one we specify in
6543 * the mode page header, or if they didn't specify enough data in
6544 * the CDB to avoid truncating this page, kick out the request.
6546 if ((page_len != (page_index->page_len - page_len_offset -
6548 || (*len_left < page_index->page_len)) {
6551 ctl_set_invalid_field(ctsio,
6554 /*field*/ *len_used + page_len_offset,
6557 free(ctsio->kern_data_ptr, M_CTL);
6558 ctl_done((union ctl_io *)ctsio);
6559 return (CTL_RETVAL_COMPLETE);
6563 * Run through the mode page, checking to make sure that the bits
6564 * the user changed are actually legal for him to change.
6566 for (i = 0; i < page_index->page_len; i++) {
6567 uint8_t *user_byte, *change_mask, *current_byte;
6571 user_byte = (uint8_t *)page_header + i;
6572 change_mask = page_index->page_data +
6573 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6574 current_byte = page_index->page_data +
6575 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6578 * Check to see whether the user set any bits in this byte
6579 * that he is not allowed to set.
6581 if ((*user_byte & ~(*change_mask)) ==
6582 (*current_byte & ~(*change_mask)))
6586 * Go through bit by bit to determine which one is illegal.
6589 for (j = 7; j >= 0; j--) {
6590 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6591 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6596 ctl_set_invalid_field(ctsio,
6599 /*field*/ *len_used + i,
6602 free(ctsio->kern_data_ptr, M_CTL);
6603 ctl_done((union ctl_io *)ctsio);
6604 return (CTL_RETVAL_COMPLETE);
6608 * Decrement these before we call the page handler, since we may
6609 * end up getting called back one way or another before the handler
6610 * returns to this context.
6612 *len_left -= page_index->page_len;
6613 *len_used += page_index->page_len;
6615 retval = page_index->select_handler(ctsio, page_index,
6616 (uint8_t *)page_header);
6619 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6620 * wait until this queued command completes to finish processing
6621 * the mode page. If it returns anything other than
6622 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6623 * already set the sense information, freed the data pointer, and
6624 * completed the io for us.
6626 if (retval != CTL_RETVAL_COMPLETE)
6627 goto bailout_no_done;
6630 * If the initiator sent us more than one page, parse the next one.
6635 ctl_set_success(ctsio);
6636 free(ctsio->kern_data_ptr, M_CTL);
6637 ctl_done((union ctl_io *)ctsio);
6641 return (CTL_RETVAL_COMPLETE);
6646 ctl_mode_select(struct ctl_scsiio *ctsio)
6648 int param_len, pf, sp;
6649 int header_size, bd_len;
6650 int len_left, len_used;
6651 struct ctl_page_index *page_index;
6652 struct ctl_lun *lun;
6653 int control_dev, page_len;
6654 union ctl_modepage_info *modepage_info;
6666 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6668 if (lun->be_lun->lun_type != T_DIRECT)
6673 switch (ctsio->cdb[0]) {
6674 case MODE_SELECT_6: {
6675 struct scsi_mode_select_6 *cdb;
6677 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6679 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6680 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6682 param_len = cdb->length;
6683 header_size = sizeof(struct scsi_mode_header_6);
6686 case MODE_SELECT_10: {
6687 struct scsi_mode_select_10 *cdb;
6689 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6691 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6692 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6694 param_len = scsi_2btoul(cdb->length);
6695 header_size = sizeof(struct scsi_mode_header_10);
6699 ctl_set_invalid_opcode(ctsio);
6700 ctl_done((union ctl_io *)ctsio);
6701 return (CTL_RETVAL_COMPLETE);
6702 break; /* NOTREACHED */
6707 * "A parameter list length of zero indicates that the Data-Out Buffer
6708 * shall be empty. This condition shall not be considered as an error."
6710 if (param_len == 0) {
6711 ctl_set_success(ctsio);
6712 ctl_done((union ctl_io *)ctsio);
6713 return (CTL_RETVAL_COMPLETE);
6717 * Since we'll hit this the first time through, prior to
6718 * allocation, we don't need to free a data buffer here.
6720 if (param_len < header_size) {
6721 ctl_set_param_len_error(ctsio);
6722 ctl_done((union ctl_io *)ctsio);
6723 return (CTL_RETVAL_COMPLETE);
6727 * Allocate the data buffer and grab the user's data. In theory,
6728 * we shouldn't have to sanity check the parameter list length here
6729 * because the maximum size is 64K. We should be able to malloc
6730 * that much without too many problems.
6732 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6733 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6734 ctsio->kern_data_len = param_len;
6735 ctsio->kern_total_len = param_len;
6736 ctsio->kern_data_resid = 0;
6737 ctsio->kern_rel_offset = 0;
6738 ctsio->kern_sg_entries = 0;
6739 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6740 ctsio->be_move_done = ctl_config_move_done;
6741 ctl_datamove((union ctl_io *)ctsio);
6743 return (CTL_RETVAL_COMPLETE);
6746 switch (ctsio->cdb[0]) {
6747 case MODE_SELECT_6: {
6748 struct scsi_mode_header_6 *mh6;
6750 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6751 bd_len = mh6->blk_desc_len;
6754 case MODE_SELECT_10: {
6755 struct scsi_mode_header_10 *mh10;
6757 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6758 bd_len = scsi_2btoul(mh10->blk_desc_len);
6762 panic("Invalid CDB type %#x", ctsio->cdb[0]);
6766 if (param_len < (header_size + bd_len)) {
6767 free(ctsio->kern_data_ptr, M_CTL);
6768 ctl_set_param_len_error(ctsio);
6769 ctl_done((union ctl_io *)ctsio);
6770 return (CTL_RETVAL_COMPLETE);
6774 * Set the IO_CONT flag, so that if this I/O gets passed to
6775 * ctl_config_write_done(), it'll get passed back to
6776 * ctl_do_mode_select() for further processing, or completion if
6779 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6780 ctsio->io_cont = ctl_do_mode_select;
6782 modepage_info = (union ctl_modepage_info *)
6783 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6785 memset(modepage_info, 0, sizeof(*modepage_info));
6787 len_left = param_len - header_size - bd_len;
6788 len_used = header_size + bd_len;
6790 modepage_info->header.len_left = len_left;
6791 modepage_info->header.len_used = len_used;
6793 return (ctl_do_mode_select((union ctl_io *)ctsio));
6797 ctl_mode_sense(struct ctl_scsiio *ctsio)
6799 struct ctl_lun *lun;
6800 int pc, page_code, dbd, llba, subpage;
6801 int alloc_len, page_len, header_len, total_len;
6802 struct scsi_mode_block_descr *block_desc;
6803 struct ctl_page_index *page_index;
6811 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6813 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6815 if (lun->be_lun->lun_type != T_DIRECT)
6820 switch (ctsio->cdb[0]) {
6821 case MODE_SENSE_6: {
6822 struct scsi_mode_sense_6 *cdb;
6824 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6826 header_len = sizeof(struct scsi_mode_hdr_6);
6827 if (cdb->byte2 & SMS_DBD)
6830 header_len += sizeof(struct scsi_mode_block_descr);
6832 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6833 page_code = cdb->page & SMS_PAGE_CODE;
6834 subpage = cdb->subpage;
6835 alloc_len = cdb->length;
6838 case MODE_SENSE_10: {
6839 struct scsi_mode_sense_10 *cdb;
6841 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6843 header_len = sizeof(struct scsi_mode_hdr_10);
6845 if (cdb->byte2 & SMS_DBD)
6848 header_len += sizeof(struct scsi_mode_block_descr);
6849 if (cdb->byte2 & SMS10_LLBAA)
6851 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6852 page_code = cdb->page & SMS_PAGE_CODE;
6853 subpage = cdb->subpage;
6854 alloc_len = scsi_2btoul(cdb->length);
6858 ctl_set_invalid_opcode(ctsio);
6859 ctl_done((union ctl_io *)ctsio);
6860 return (CTL_RETVAL_COMPLETE);
6861 break; /* NOTREACHED */
6865 * We have to make a first pass through to calculate the size of
6866 * the pages that match the user's query. Then we allocate enough
6867 * memory to hold it, and actually copy the data into the buffer.
6869 switch (page_code) {
6870 case SMS_ALL_PAGES_PAGE: {
6876 * At the moment, values other than 0 and 0xff here are
6877 * reserved according to SPC-3.
6879 if ((subpage != SMS_SUBPAGE_PAGE_0)
6880 && (subpage != SMS_SUBPAGE_ALL)) {
6881 ctl_set_invalid_field(ctsio,
6887 ctl_done((union ctl_io *)ctsio);
6888 return (CTL_RETVAL_COMPLETE);
6891 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6892 if ((control_dev != 0)
6893 && (lun->mode_pages.index[i].page_flags &
6894 CTL_PAGE_FLAG_DISK_ONLY))
6898 * We don't use this subpage if the user didn't
6899 * request all subpages.
6901 if ((lun->mode_pages.index[i].subpage != 0)
6902 && (subpage == SMS_SUBPAGE_PAGE_0))
6906 printf("found page %#x len %d\n",
6907 lun->mode_pages.index[i].page_code &
6909 lun->mode_pages.index[i].page_len);
6911 page_len += lun->mode_pages.index[i].page_len;
6920 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6921 /* Look for the right page code */
6922 if ((lun->mode_pages.index[i].page_code &
6923 SMPH_PC_MASK) != page_code)
6926 /* Look for the right subpage or the subpage wildcard*/
6927 if ((lun->mode_pages.index[i].subpage != subpage)
6928 && (subpage != SMS_SUBPAGE_ALL))
6931 /* Make sure the page is supported for this dev type */
6932 if ((control_dev != 0)
6933 && (lun->mode_pages.index[i].page_flags &
6934 CTL_PAGE_FLAG_DISK_ONLY))
6938 printf("found page %#x len %d\n",
6939 lun->mode_pages.index[i].page_code &
6941 lun->mode_pages.index[i].page_len);
6944 page_len += lun->mode_pages.index[i].page_len;
6947 if (page_len == 0) {
6948 ctl_set_invalid_field(ctsio,
6954 ctl_done((union ctl_io *)ctsio);
6955 return (CTL_RETVAL_COMPLETE);
6961 total_len = header_len + page_len;
6963 printf("header_len = %d, page_len = %d, total_len = %d\n",
6964 header_len, page_len, total_len);
6967 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6968 ctsio->kern_sg_entries = 0;
6969 ctsio->kern_data_resid = 0;
6970 ctsio->kern_rel_offset = 0;
6971 if (total_len < alloc_len) {
6972 ctsio->residual = alloc_len - total_len;
6973 ctsio->kern_data_len = total_len;
6974 ctsio->kern_total_len = total_len;
6976 ctsio->residual = 0;
6977 ctsio->kern_data_len = alloc_len;
6978 ctsio->kern_total_len = alloc_len;
6981 switch (ctsio->cdb[0]) {
6982 case MODE_SENSE_6: {
6983 struct scsi_mode_hdr_6 *header;
6985 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6987 header->datalen = ctl_min(total_len - 1, 254);
6988 if (control_dev == 0) {
6989 header->dev_specific = 0x10; /* DPOFUA */
6990 if ((lun->flags & CTL_LUN_READONLY) ||
6991 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6992 .eca_and_aen & SCP_SWP) != 0)
6993 header->dev_specific |= 0x80; /* WP */
6996 header->block_descr_len = 0;
6998 header->block_descr_len =
6999 sizeof(struct scsi_mode_block_descr);
7000 block_desc = (struct scsi_mode_block_descr *)&header[1];
7003 case MODE_SENSE_10: {
7004 struct scsi_mode_hdr_10 *header;
7007 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
7009 datalen = ctl_min(total_len - 2, 65533);
7010 scsi_ulto2b(datalen, header->datalen);
7011 if (control_dev == 0) {
7012 header->dev_specific = 0x10; /* DPOFUA */
7013 if ((lun->flags & CTL_LUN_READONLY) ||
7014 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
7015 .eca_and_aen & SCP_SWP) != 0)
7016 header->dev_specific |= 0x80; /* WP */
7019 scsi_ulto2b(0, header->block_descr_len);
7021 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
7022 header->block_descr_len);
7023 block_desc = (struct scsi_mode_block_descr *)&header[1];
7027 panic("invalid CDB type %#x", ctsio->cdb[0]);
7028 break; /* NOTREACHED */
7032 * If we've got a disk, use its blocksize in the block
7033 * descriptor. Otherwise, just set it to 0.
7036 if (control_dev == 0)
7037 scsi_ulto3b(lun->be_lun->blocksize,
7038 block_desc->block_len);
7040 scsi_ulto3b(0, block_desc->block_len);
7043 switch (page_code) {
7044 case SMS_ALL_PAGES_PAGE: {
7047 data_used = header_len;
7048 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
7049 struct ctl_page_index *page_index;
7051 page_index = &lun->mode_pages.index[i];
7053 if ((control_dev != 0)
7054 && (page_index->page_flags &
7055 CTL_PAGE_FLAG_DISK_ONLY))
7059 * We don't use this subpage if the user didn't
7060 * request all subpages. We already checked (above)
7061 * to make sure the user only specified a subpage
7062 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
7064 if ((page_index->subpage != 0)
7065 && (subpage == SMS_SUBPAGE_PAGE_0))
7069 * Call the handler, if it exists, to update the
7070 * page to the latest values.
7072 if (page_index->sense_handler != NULL)
7073 page_index->sense_handler(ctsio, page_index,pc);
7075 memcpy(ctsio->kern_data_ptr + data_used,
7076 page_index->page_data +
7077 (page_index->page_len * pc),
7078 page_index->page_len);
7079 data_used += page_index->page_len;
7086 data_used = header_len;
7088 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
7089 struct ctl_page_index *page_index;
7091 page_index = &lun->mode_pages.index[i];
7093 /* Look for the right page code */
7094 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
7097 /* Look for the right subpage or the subpage wildcard*/
7098 if ((page_index->subpage != subpage)
7099 && (subpage != SMS_SUBPAGE_ALL))
7102 /* Make sure the page is supported for this dev type */
7103 if ((control_dev != 0)
7104 && (page_index->page_flags &
7105 CTL_PAGE_FLAG_DISK_ONLY))
7109 * Call the handler, if it exists, to update the
7110 * page to the latest values.
7112 if (page_index->sense_handler != NULL)
7113 page_index->sense_handler(ctsio, page_index,pc);
7115 memcpy(ctsio->kern_data_ptr + data_used,
7116 page_index->page_data +
7117 (page_index->page_len * pc),
7118 page_index->page_len);
7119 data_used += page_index->page_len;
7125 ctsio->scsi_status = SCSI_STATUS_OK;
7127 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7128 ctsio->be_move_done = ctl_config_move_done;
7129 ctl_datamove((union ctl_io *)ctsio);
7131 return (CTL_RETVAL_COMPLETE);
7135 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
7136 struct ctl_page_index *page_index,
7139 struct ctl_lun *lun;
7140 struct scsi_log_param_header *phdr;
7144 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7145 data = page_index->page_data;
7147 if (lun->backend->lun_attr != NULL &&
7148 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
7150 phdr = (struct scsi_log_param_header *)data;
7151 scsi_ulto2b(0x0001, phdr->param_code);
7152 phdr->param_control = SLP_LBIN | SLP_LP;
7153 phdr->param_len = 8;
7154 data = (uint8_t *)(phdr + 1);
7155 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
7156 data[4] = 0x02; /* per-pool */
7157 data += phdr->param_len;
7160 if (lun->backend->lun_attr != NULL &&
7161 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
7163 phdr = (struct scsi_log_param_header *)data;
7164 scsi_ulto2b(0x0002, phdr->param_code);
7165 phdr->param_control = SLP_LBIN | SLP_LP;
7166 phdr->param_len = 8;
7167 data = (uint8_t *)(phdr + 1);
7168 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
7169 data[4] = 0x01; /* per-LUN */
7170 data += phdr->param_len;
7173 if (lun->backend->lun_attr != NULL &&
7174 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
7176 phdr = (struct scsi_log_param_header *)data;
7177 scsi_ulto2b(0x00f1, phdr->param_code);
7178 phdr->param_control = SLP_LBIN | SLP_LP;
7179 phdr->param_len = 8;
7180 data = (uint8_t *)(phdr + 1);
7181 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
7182 data[4] = 0x02; /* per-pool */
7183 data += phdr->param_len;
7186 if (lun->backend->lun_attr != NULL &&
7187 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
7189 phdr = (struct scsi_log_param_header *)data;
7190 scsi_ulto2b(0x00f2, phdr->param_code);
7191 phdr->param_control = SLP_LBIN | SLP_LP;
7192 phdr->param_len = 8;
7193 data = (uint8_t *)(phdr + 1);
7194 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
7195 data[4] = 0x02; /* per-pool */
7196 data += phdr->param_len;
7199 page_index->page_len = data - page_index->page_data;
7204 ctl_log_sense(struct ctl_scsiio *ctsio)
7206 struct ctl_lun *lun;
7207 int i, pc, page_code, subpage;
7208 int alloc_len, total_len;
7209 struct ctl_page_index *page_index;
7210 struct scsi_log_sense *cdb;
7211 struct scsi_log_header *header;
7213 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
7215 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7216 cdb = (struct scsi_log_sense *)ctsio->cdb;
7217 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
7218 page_code = cdb->page & SLS_PAGE_CODE;
7219 subpage = cdb->subpage;
7220 alloc_len = scsi_2btoul(cdb->length);
7223 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
7224 page_index = &lun->log_pages.index[i];
7226 /* Look for the right page code */
7227 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
7230 /* Look for the right subpage or the subpage wildcard*/
7231 if (page_index->subpage != subpage)
7236 if (i >= CTL_NUM_LOG_PAGES) {
7237 ctl_set_invalid_field(ctsio,
7243 ctl_done((union ctl_io *)ctsio);
7244 return (CTL_RETVAL_COMPLETE);
7247 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
7249 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7250 ctsio->kern_sg_entries = 0;
7251 ctsio->kern_data_resid = 0;
7252 ctsio->kern_rel_offset = 0;
7253 if (total_len < alloc_len) {
7254 ctsio->residual = alloc_len - total_len;
7255 ctsio->kern_data_len = total_len;
7256 ctsio->kern_total_len = total_len;
7258 ctsio->residual = 0;
7259 ctsio->kern_data_len = alloc_len;
7260 ctsio->kern_total_len = alloc_len;
7263 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
7264 header->page = page_index->page_code;
7265 if (page_index->subpage) {
7266 header->page |= SL_SPF;
7267 header->subpage = page_index->subpage;
7269 scsi_ulto2b(page_index->page_len, header->datalen);
7272 * Call the handler, if it exists, to update the
7273 * page to the latest values.
7275 if (page_index->sense_handler != NULL)
7276 page_index->sense_handler(ctsio, page_index, pc);
7278 memcpy(header + 1, page_index->page_data, page_index->page_len);
7280 ctsio->scsi_status = SCSI_STATUS_OK;
7281 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7282 ctsio->be_move_done = ctl_config_move_done;
7283 ctl_datamove((union ctl_io *)ctsio);
7285 return (CTL_RETVAL_COMPLETE);
7289 ctl_read_capacity(struct ctl_scsiio *ctsio)
7291 struct scsi_read_capacity *cdb;
7292 struct scsi_read_capacity_data *data;
7293 struct ctl_lun *lun;
7296 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
7298 cdb = (struct scsi_read_capacity *)ctsio->cdb;
7300 lba = scsi_4btoul(cdb->addr);
7301 if (((cdb->pmi & SRC_PMI) == 0)
7303 ctl_set_invalid_field(/*ctsio*/ ctsio,
7309 ctl_done((union ctl_io *)ctsio);
7310 return (CTL_RETVAL_COMPLETE);
7313 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7315 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7316 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
7317 ctsio->residual = 0;
7318 ctsio->kern_data_len = sizeof(*data);
7319 ctsio->kern_total_len = sizeof(*data);
7320 ctsio->kern_data_resid = 0;
7321 ctsio->kern_rel_offset = 0;
7322 ctsio->kern_sg_entries = 0;
7325 * If the maximum LBA is greater than 0xfffffffe, the user must
7326 * issue a SERVICE ACTION IN (16) command, with the read capacity
7327 * serivce action set.
7329 if (lun->be_lun->maxlba > 0xfffffffe)
7330 scsi_ulto4b(0xffffffff, data->addr);
7332 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
7335 * XXX KDM this may not be 512 bytes...
7337 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7339 ctsio->scsi_status = SCSI_STATUS_OK;
7341 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7342 ctsio->be_move_done = ctl_config_move_done;
7343 ctl_datamove((union ctl_io *)ctsio);
7345 return (CTL_RETVAL_COMPLETE);
7349 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
7351 struct scsi_read_capacity_16 *cdb;
7352 struct scsi_read_capacity_data_long *data;
7353 struct ctl_lun *lun;
7357 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
7359 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
7361 alloc_len = scsi_4btoul(cdb->alloc_len);
7362 lba = scsi_8btou64(cdb->addr);
7364 if ((cdb->reladr & SRC16_PMI)
7366 ctl_set_invalid_field(/*ctsio*/ ctsio,
7372 ctl_done((union ctl_io *)ctsio);
7373 return (CTL_RETVAL_COMPLETE);
7376 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7378 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7379 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
7381 if (sizeof(*data) < alloc_len) {
7382 ctsio->residual = alloc_len - sizeof(*data);
7383 ctsio->kern_data_len = sizeof(*data);
7384 ctsio->kern_total_len = sizeof(*data);
7386 ctsio->residual = 0;
7387 ctsio->kern_data_len = alloc_len;
7388 ctsio->kern_total_len = alloc_len;
7390 ctsio->kern_data_resid = 0;
7391 ctsio->kern_rel_offset = 0;
7392 ctsio->kern_sg_entries = 0;
7394 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7395 /* XXX KDM this may not be 512 bytes... */
7396 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7397 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7398 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7399 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7400 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
7402 ctsio->scsi_status = SCSI_STATUS_OK;
7404 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7405 ctsio->be_move_done = ctl_config_move_done;
7406 ctl_datamove((union ctl_io *)ctsio);
7408 return (CTL_RETVAL_COMPLETE);
7412 ctl_read_defect(struct ctl_scsiio *ctsio)
7414 struct scsi_read_defect_data_10 *ccb10;
7415 struct scsi_read_defect_data_12 *ccb12;
7416 struct scsi_read_defect_data_hdr_10 *data10;
7417 struct scsi_read_defect_data_hdr_12 *data12;
7418 uint32_t alloc_len, data_len;
7421 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7423 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7424 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7425 format = ccb10->format;
7426 alloc_len = scsi_2btoul(ccb10->alloc_length);
7427 data_len = sizeof(*data10);
7429 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7430 format = ccb12->format;
7431 alloc_len = scsi_4btoul(ccb12->alloc_length);
7432 data_len = sizeof(*data12);
7434 if (alloc_len == 0) {
7435 ctl_set_success(ctsio);
7436 ctl_done((union ctl_io *)ctsio);
7437 return (CTL_RETVAL_COMPLETE);
7440 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7441 if (data_len < alloc_len) {
7442 ctsio->residual = alloc_len - data_len;
7443 ctsio->kern_data_len = data_len;
7444 ctsio->kern_total_len = data_len;
7446 ctsio->residual = 0;
7447 ctsio->kern_data_len = alloc_len;
7448 ctsio->kern_total_len = alloc_len;
7450 ctsio->kern_data_resid = 0;
7451 ctsio->kern_rel_offset = 0;
7452 ctsio->kern_sg_entries = 0;
7454 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7455 data10 = (struct scsi_read_defect_data_hdr_10 *)
7456 ctsio->kern_data_ptr;
7457 data10->format = format;
7458 scsi_ulto2b(0, data10->length);
7460 data12 = (struct scsi_read_defect_data_hdr_12 *)
7461 ctsio->kern_data_ptr;
7462 data12->format = format;
7463 scsi_ulto2b(0, data12->generation);
7464 scsi_ulto4b(0, data12->length);
7467 ctsio->scsi_status = SCSI_STATUS_OK;
7468 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7469 ctsio->be_move_done = ctl_config_move_done;
7470 ctl_datamove((union ctl_io *)ctsio);
7471 return (CTL_RETVAL_COMPLETE);
7475 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7477 struct scsi_maintenance_in *cdb;
7479 int alloc_len, ext, total_len = 0, g, p, pc, pg, gs, os;
7480 int num_target_port_groups, num_target_ports;
7481 struct ctl_lun *lun;
7482 struct ctl_softc *softc;
7483 struct ctl_port *port;
7484 struct scsi_target_group_data *rtg_ptr;
7485 struct scsi_target_group_data_extended *rtg_ext_ptr;
7486 struct scsi_target_port_group_descriptor *tpg_desc;
7488 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7490 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7491 softc = control_softc;
7492 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7494 retval = CTL_RETVAL_COMPLETE;
7496 switch (cdb->byte2 & STG_PDF_MASK) {
7497 case STG_PDF_LENGTH:
7500 case STG_PDF_EXTENDED:
7504 ctl_set_invalid_field(/*ctsio*/ ctsio,
7510 ctl_done((union ctl_io *)ctsio);
7514 if (softc->is_single)
7515 num_target_port_groups = 1;
7517 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
7518 num_target_ports = 0;
7519 mtx_lock(&softc->ctl_lock);
7520 STAILQ_FOREACH(port, &softc->port_list, links) {
7521 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7523 if (ctl_map_lun_back(port->targ_port, lun->lun) >= CTL_MAX_LUNS)
7527 mtx_unlock(&softc->ctl_lock);
7530 total_len = sizeof(struct scsi_target_group_data_extended);
7532 total_len = sizeof(struct scsi_target_group_data);
7533 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7534 num_target_port_groups +
7535 sizeof(struct scsi_target_port_descriptor) *
7536 num_target_ports * num_target_port_groups;
7538 alloc_len = scsi_4btoul(cdb->length);
7540 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7542 ctsio->kern_sg_entries = 0;
7544 if (total_len < alloc_len) {
7545 ctsio->residual = alloc_len - total_len;
7546 ctsio->kern_data_len = total_len;
7547 ctsio->kern_total_len = total_len;
7549 ctsio->residual = 0;
7550 ctsio->kern_data_len = alloc_len;
7551 ctsio->kern_total_len = alloc_len;
7553 ctsio->kern_data_resid = 0;
7554 ctsio->kern_rel_offset = 0;
7557 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7558 ctsio->kern_data_ptr;
7559 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7560 rtg_ext_ptr->format_type = 0x10;
7561 rtg_ext_ptr->implicit_transition_time = 0;
7562 tpg_desc = &rtg_ext_ptr->groups[0];
7564 rtg_ptr = (struct scsi_target_group_data *)
7565 ctsio->kern_data_ptr;
7566 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7567 tpg_desc = &rtg_ptr->groups[0];
7570 mtx_lock(&softc->ctl_lock);
7571 pg = softc->port_offset / CTL_MAX_PORTS;
7572 if (softc->flags & CTL_FLAG_ACTIVE_SHELF) {
7573 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) {
7574 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7575 os = TPG_ASYMMETRIC_ACCESS_STANDBY;
7576 } else if (lun->flags & CTL_LUN_PRIMARY_SC) {
7577 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7578 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7580 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7581 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7584 gs = TPG_ASYMMETRIC_ACCESS_STANDBY;
7585 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7587 for (g = 0; g < num_target_port_groups; g++) {
7588 tpg_desc->pref_state = (g == pg) ? gs : os;
7589 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP;
7590 scsi_ulto2b(g + 1, tpg_desc->target_port_group);
7591 tpg_desc->status = TPG_IMPLICIT;
7593 STAILQ_FOREACH(port, &softc->port_list, links) {
7594 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7596 if (ctl_map_lun_back(port->targ_port, lun->lun) >=
7599 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS;
7600 scsi_ulto2b(p, tpg_desc->descriptors[pc].
7601 relative_target_port_identifier);
7604 tpg_desc->target_port_count = pc;
7605 tpg_desc = (struct scsi_target_port_group_descriptor *)
7606 &tpg_desc->descriptors[pc];
7608 mtx_unlock(&softc->ctl_lock);
7610 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7611 ctsio->be_move_done = ctl_config_move_done;
7613 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7614 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7615 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7616 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7617 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7619 ctl_datamove((union ctl_io *)ctsio);
7624 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7626 struct ctl_lun *lun;
7627 struct scsi_report_supported_opcodes *cdb;
7628 const struct ctl_cmd_entry *entry, *sentry;
7629 struct scsi_report_supported_opcodes_all *all;
7630 struct scsi_report_supported_opcodes_descr *descr;
7631 struct scsi_report_supported_opcodes_one *one;
7633 int alloc_len, total_len;
7634 int opcode, service_action, i, j, num;
7636 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7638 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7639 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7641 retval = CTL_RETVAL_COMPLETE;
7643 opcode = cdb->requested_opcode;
7644 service_action = scsi_2btoul(cdb->requested_service_action);
7645 switch (cdb->options & RSO_OPTIONS_MASK) {
7646 case RSO_OPTIONS_ALL:
7648 for (i = 0; i < 256; i++) {
7649 entry = &ctl_cmd_table[i];
7650 if (entry->flags & CTL_CMD_FLAG_SA5) {
7651 for (j = 0; j < 32; j++) {
7652 sentry = &((const struct ctl_cmd_entry *)
7654 if (ctl_cmd_applicable(
7655 lun->be_lun->lun_type, sentry))
7659 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7664 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7665 num * sizeof(struct scsi_report_supported_opcodes_descr);
7667 case RSO_OPTIONS_OC:
7668 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7669 ctl_set_invalid_field(/*ctsio*/ ctsio,
7675 ctl_done((union ctl_io *)ctsio);
7676 return (CTL_RETVAL_COMPLETE);
7678 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7680 case RSO_OPTIONS_OC_SA:
7681 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7682 service_action >= 32) {
7683 ctl_set_invalid_field(/*ctsio*/ ctsio,
7689 ctl_done((union ctl_io *)ctsio);
7690 return (CTL_RETVAL_COMPLETE);
7692 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7695 ctl_set_invalid_field(/*ctsio*/ ctsio,
7701 ctl_done((union ctl_io *)ctsio);
7702 return (CTL_RETVAL_COMPLETE);
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 switch (cdb->options & RSO_OPTIONS_MASK) {
7724 case RSO_OPTIONS_ALL:
7725 all = (struct scsi_report_supported_opcodes_all *)
7726 ctsio->kern_data_ptr;
7728 for (i = 0; i < 256; i++) {
7729 entry = &ctl_cmd_table[i];
7730 if (entry->flags & CTL_CMD_FLAG_SA5) {
7731 for (j = 0; j < 32; j++) {
7732 sentry = &((const struct ctl_cmd_entry *)
7734 if (!ctl_cmd_applicable(
7735 lun->be_lun->lun_type, sentry))
7737 descr = &all->descr[num++];
7739 scsi_ulto2b(j, descr->service_action);
7740 descr->flags = RSO_SERVACTV;
7741 scsi_ulto2b(sentry->length,
7745 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7748 descr = &all->descr[num++];
7750 scsi_ulto2b(0, descr->service_action);
7752 scsi_ulto2b(entry->length, descr->cdb_length);
7756 num * sizeof(struct scsi_report_supported_opcodes_descr),
7759 case RSO_OPTIONS_OC:
7760 one = (struct scsi_report_supported_opcodes_one *)
7761 ctsio->kern_data_ptr;
7762 entry = &ctl_cmd_table[opcode];
7764 case RSO_OPTIONS_OC_SA:
7765 one = (struct scsi_report_supported_opcodes_one *)
7766 ctsio->kern_data_ptr;
7767 entry = &ctl_cmd_table[opcode];
7768 entry = &((const struct ctl_cmd_entry *)
7769 entry->execute)[service_action];
7771 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7773 scsi_ulto2b(entry->length, one->cdb_length);
7774 one->cdb_usage[0] = opcode;
7775 memcpy(&one->cdb_usage[1], entry->usage,
7782 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7783 ctsio->be_move_done = ctl_config_move_done;
7785 ctl_datamove((union ctl_io *)ctsio);
7790 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7792 struct scsi_report_supported_tmf *cdb;
7793 struct scsi_report_supported_tmf_data *data;
7795 int alloc_len, total_len;
7797 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7799 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7801 retval = CTL_RETVAL_COMPLETE;
7803 total_len = sizeof(struct scsi_report_supported_tmf_data);
7804 alloc_len = scsi_4btoul(cdb->length);
7806 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7808 ctsio->kern_sg_entries = 0;
7810 if (total_len < alloc_len) {
7811 ctsio->residual = alloc_len - total_len;
7812 ctsio->kern_data_len = total_len;
7813 ctsio->kern_total_len = total_len;
7815 ctsio->residual = 0;
7816 ctsio->kern_data_len = alloc_len;
7817 ctsio->kern_total_len = alloc_len;
7819 ctsio->kern_data_resid = 0;
7820 ctsio->kern_rel_offset = 0;
7822 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr;
7823 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS;
7824 data->byte2 |= RST_ITNRS;
7826 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7827 ctsio->be_move_done = ctl_config_move_done;
7829 ctl_datamove((union ctl_io *)ctsio);
7834 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7836 struct scsi_report_timestamp *cdb;
7837 struct scsi_report_timestamp_data *data;
7841 int alloc_len, total_len;
7843 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7845 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7847 retval = CTL_RETVAL_COMPLETE;
7849 total_len = sizeof(struct scsi_report_timestamp_data);
7850 alloc_len = scsi_4btoul(cdb->length);
7852 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7854 ctsio->kern_sg_entries = 0;
7856 if (total_len < alloc_len) {
7857 ctsio->residual = alloc_len - total_len;
7858 ctsio->kern_data_len = total_len;
7859 ctsio->kern_total_len = total_len;
7861 ctsio->residual = 0;
7862 ctsio->kern_data_len = alloc_len;
7863 ctsio->kern_total_len = alloc_len;
7865 ctsio->kern_data_resid = 0;
7866 ctsio->kern_rel_offset = 0;
7868 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7869 scsi_ulto2b(sizeof(*data) - 2, data->length);
7870 data->origin = RTS_ORIG_OUTSIDE;
7872 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7873 scsi_ulto4b(timestamp >> 16, data->timestamp);
7874 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7876 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7877 ctsio->be_move_done = ctl_config_move_done;
7879 ctl_datamove((union ctl_io *)ctsio);
7884 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7886 struct scsi_per_res_in *cdb;
7887 int alloc_len, total_len = 0;
7888 /* struct scsi_per_res_in_rsrv in_data; */
7889 struct ctl_lun *lun;
7890 struct ctl_softc *softc;
7892 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7894 softc = control_softc;
7896 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7898 alloc_len = scsi_2btoul(cdb->length);
7900 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7903 mtx_lock(&lun->lun_lock);
7904 switch (cdb->action) {
7905 case SPRI_RK: /* read keys */
7906 total_len = sizeof(struct scsi_per_res_in_keys) +
7908 sizeof(struct scsi_per_res_key);
7910 case SPRI_RR: /* read reservation */
7911 if (lun->flags & CTL_LUN_PR_RESERVED)
7912 total_len = sizeof(struct scsi_per_res_in_rsrv);
7914 total_len = sizeof(struct scsi_per_res_in_header);
7916 case SPRI_RC: /* report capabilities */
7917 total_len = sizeof(struct scsi_per_res_cap);
7919 case SPRI_RS: /* read full status */
7920 total_len = sizeof(struct scsi_per_res_in_header) +
7921 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7925 panic("Invalid PR type %x", cdb->action);
7927 mtx_unlock(&lun->lun_lock);
7929 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7931 if (total_len < alloc_len) {
7932 ctsio->residual = alloc_len - total_len;
7933 ctsio->kern_data_len = total_len;
7934 ctsio->kern_total_len = total_len;
7936 ctsio->residual = 0;
7937 ctsio->kern_data_len = alloc_len;
7938 ctsio->kern_total_len = alloc_len;
7941 ctsio->kern_data_resid = 0;
7942 ctsio->kern_rel_offset = 0;
7943 ctsio->kern_sg_entries = 0;
7945 mtx_lock(&lun->lun_lock);
7946 switch (cdb->action) {
7947 case SPRI_RK: { // read keys
7948 struct scsi_per_res_in_keys *res_keys;
7951 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7954 * We had to drop the lock to allocate our buffer, which
7955 * leaves time for someone to come in with another
7956 * persistent reservation. (That is unlikely, though,
7957 * since this should be the only persistent reservation
7958 * command active right now.)
7960 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7961 (lun->pr_key_count *
7962 sizeof(struct scsi_per_res_key)))){
7963 mtx_unlock(&lun->lun_lock);
7964 free(ctsio->kern_data_ptr, M_CTL);
7965 printf("%s: reservation length changed, retrying\n",
7970 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7972 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7973 lun->pr_key_count, res_keys->header.length);
7975 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7976 if (lun->pr_keys[i] == 0)
7980 * We used lun->pr_key_count to calculate the
7981 * size to allocate. If it turns out the number of
7982 * initiators with the registered flag set is
7983 * larger than that (i.e. they haven't been kept in
7984 * sync), we've got a problem.
7986 if (key_count >= lun->pr_key_count) {
7988 csevent_log(CSC_CTL | CSC_SHELF_SW |
7990 csevent_LogType_Fault,
7991 csevent_AlertLevel_Yellow,
7992 csevent_FRU_ShelfController,
7993 csevent_FRU_Firmware,
7994 csevent_FRU_Unknown,
7995 "registered keys %d >= key "
7996 "count %d", key_count,
8002 scsi_u64to8b(lun->pr_keys[i],
8003 res_keys->keys[key_count].key);
8008 case SPRI_RR: { // read reservation
8009 struct scsi_per_res_in_rsrv *res;
8010 int tmp_len, header_only;
8012 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
8014 scsi_ulto4b(lun->PRGeneration, res->header.generation);
8016 if (lun->flags & CTL_LUN_PR_RESERVED)
8018 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
8019 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
8020 res->header.length);
8023 tmp_len = sizeof(struct scsi_per_res_in_header);
8024 scsi_ulto4b(0, res->header.length);
8029 * We had to drop the lock to allocate our buffer, which
8030 * leaves time for someone to come in with another
8031 * persistent reservation. (That is unlikely, though,
8032 * since this should be the only persistent reservation
8033 * command active right now.)
8035 if (tmp_len != total_len) {
8036 mtx_unlock(&lun->lun_lock);
8037 free(ctsio->kern_data_ptr, M_CTL);
8038 printf("%s: reservation status changed, retrying\n",
8044 * No reservation held, so we're done.
8046 if (header_only != 0)
8050 * If the registration is an All Registrants type, the key
8051 * is 0, since it doesn't really matter.
8053 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8054 scsi_u64to8b(lun->pr_keys[lun->pr_res_idx],
8055 res->data.reservation);
8057 res->data.scopetype = lun->res_type;
8060 case SPRI_RC: //report capabilities
8062 struct scsi_per_res_cap *res_cap;
8065 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
8066 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
8067 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5;
8068 type_mask = SPRI_TM_WR_EX_AR |
8074 scsi_ulto2b(type_mask, res_cap->type_mask);
8077 case SPRI_RS: { // read full status
8078 struct scsi_per_res_in_full *res_status;
8079 struct scsi_per_res_in_full_desc *res_desc;
8080 struct ctl_port *port;
8083 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
8086 * We had to drop the lock to allocate our buffer, which
8087 * leaves time for someone to come in with another
8088 * persistent reservation. (That is unlikely, though,
8089 * since this should be the only persistent reservation
8090 * command active right now.)
8092 if (total_len < (sizeof(struct scsi_per_res_in_header) +
8093 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
8094 lun->pr_key_count)){
8095 mtx_unlock(&lun->lun_lock);
8096 free(ctsio->kern_data_ptr, M_CTL);
8097 printf("%s: reservation length changed, retrying\n",
8102 scsi_ulto4b(lun->PRGeneration, res_status->header.generation);
8104 res_desc = &res_status->desc[0];
8105 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) {
8106 if (lun->pr_keys[i] == 0)
8109 scsi_u64to8b(lun->pr_keys[i], res_desc->res_key.key);
8110 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
8111 (lun->pr_res_idx == i ||
8112 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
8113 res_desc->flags = SPRI_FULL_R_HOLDER;
8114 res_desc->scopetype = lun->res_type;
8116 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
8117 res_desc->rel_trgt_port_id);
8119 port = softc->ctl_ports[
8120 ctl_port_idx(i / CTL_MAX_INIT_PER_PORT)];
8122 len = ctl_create_iid(port,
8123 i % CTL_MAX_INIT_PER_PORT,
8124 res_desc->transport_id);
8125 scsi_ulto4b(len, res_desc->additional_length);
8126 res_desc = (struct scsi_per_res_in_full_desc *)
8127 &res_desc->transport_id[len];
8129 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
8130 res_status->header.length);
8135 * This is a bug, because we just checked for this above,
8136 * and should have returned an error.
8138 panic("Invalid PR type %x", cdb->action);
8139 break; /* NOTREACHED */
8141 mtx_unlock(&lun->lun_lock);
8143 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8144 ctsio->be_move_done = ctl_config_move_done;
8146 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
8147 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
8148 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
8149 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
8150 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
8152 ctl_datamove((union ctl_io *)ctsio);
8154 return (CTL_RETVAL_COMPLETE);
8158 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
8162 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
8163 uint64_t sa_res_key, uint8_t type, uint32_t residx,
8164 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
8165 struct scsi_per_res_out_parms* param)
8167 union ctl_ha_msg persis_io;
8173 mtx_lock(&lun->lun_lock);
8174 if (sa_res_key == 0) {
8175 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8176 /* validate scope and type */
8177 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
8179 mtx_unlock(&lun->lun_lock);
8180 ctl_set_invalid_field(/*ctsio*/ ctsio,
8186 ctl_done((union ctl_io *)ctsio);
8190 if (type>8 || type==2 || type==4 || type==0) {
8191 mtx_unlock(&lun->lun_lock);
8192 ctl_set_invalid_field(/*ctsio*/ ctsio,
8198 ctl_done((union ctl_io *)ctsio);
8203 * Unregister everybody else and build UA for
8206 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8207 if (i == residx || lun->pr_keys[i] == 0)
8211 && i <CTL_MAX_INITIATORS)
8212 lun->pending_ua[i] |=
8214 else if (persis_offset
8215 && i >= persis_offset)
8216 lun->pending_ua[i-persis_offset] |=
8218 lun->pr_keys[i] = 0;
8220 lun->pr_key_count = 1;
8221 lun->res_type = type;
8222 if (lun->res_type != SPR_TYPE_WR_EX_AR
8223 && lun->res_type != SPR_TYPE_EX_AC_AR)
8224 lun->pr_res_idx = residx;
8226 /* send msg to other side */
8227 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8228 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8229 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8230 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8231 persis_io.pr.pr_info.res_type = type;
8232 memcpy(persis_io.pr.pr_info.sa_res_key,
8233 param->serv_act_res_key,
8234 sizeof(param->serv_act_res_key));
8235 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8236 &persis_io, sizeof(persis_io), 0)) >
8237 CTL_HA_STATUS_SUCCESS) {
8238 printf("CTL:Persis Out error returned "
8239 "from ctl_ha_msg_send %d\n",
8243 /* not all registrants */
8244 mtx_unlock(&lun->lun_lock);
8245 free(ctsio->kern_data_ptr, M_CTL);
8246 ctl_set_invalid_field(ctsio,
8252 ctl_done((union ctl_io *)ctsio);
8255 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8256 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
8259 if (res_key == sa_res_key) {
8262 * The spec implies this is not good but doesn't
8263 * say what to do. There are two choices either
8264 * generate a res conflict or check condition
8265 * with illegal field in parameter data. Since
8266 * that is what is done when the sa_res_key is
8267 * zero I'll take that approach since this has
8268 * to do with the sa_res_key.
8270 mtx_unlock(&lun->lun_lock);
8271 free(ctsio->kern_data_ptr, M_CTL);
8272 ctl_set_invalid_field(ctsio,
8278 ctl_done((union ctl_io *)ctsio);
8282 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8283 if (lun->pr_keys[i] != sa_res_key)
8287 lun->pr_keys[i] = 0;
8288 lun->pr_key_count--;
8290 if (!persis_offset && i < CTL_MAX_INITIATORS)
8291 lun->pending_ua[i] |= CTL_UA_REG_PREEMPT;
8292 else if (persis_offset && i >= persis_offset)
8293 lun->pending_ua[i-persis_offset] |=
8297 mtx_unlock(&lun->lun_lock);
8298 free(ctsio->kern_data_ptr, M_CTL);
8299 ctl_set_reservation_conflict(ctsio);
8300 ctl_done((union ctl_io *)ctsio);
8301 return (CTL_RETVAL_COMPLETE);
8303 /* send msg to other side */
8304 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8305 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8306 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8307 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8308 persis_io.pr.pr_info.res_type = type;
8309 memcpy(persis_io.pr.pr_info.sa_res_key,
8310 param->serv_act_res_key,
8311 sizeof(param->serv_act_res_key));
8312 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8313 &persis_io, sizeof(persis_io), 0)) >
8314 CTL_HA_STATUS_SUCCESS) {
8315 printf("CTL:Persis Out error returned from "
8316 "ctl_ha_msg_send %d\n", isc_retval);
8319 /* Reserved but not all registrants */
8320 /* sa_res_key is res holder */
8321 if (sa_res_key == lun->pr_keys[lun->pr_res_idx]) {
8322 /* validate scope and type */
8323 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
8325 mtx_unlock(&lun->lun_lock);
8326 ctl_set_invalid_field(/*ctsio*/ ctsio,
8332 ctl_done((union ctl_io *)ctsio);
8336 if (type>8 || type==2 || type==4 || type==0) {
8337 mtx_unlock(&lun->lun_lock);
8338 ctl_set_invalid_field(/*ctsio*/ ctsio,
8344 ctl_done((union ctl_io *)ctsio);
8350 * if sa_res_key != res_key remove all
8351 * registrants w/sa_res_key and generate UA
8352 * for these registrants(Registrations
8353 * Preempted) if it wasn't an exclusive
8354 * reservation generate UA(Reservations
8355 * Preempted) for all other registered nexuses
8356 * if the type has changed. Establish the new
8357 * reservation and holder. If res_key and
8358 * sa_res_key are the same do the above
8359 * except don't unregister the res holder.
8362 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8363 if (i == residx || lun->pr_keys[i] == 0)
8366 if (sa_res_key == lun->pr_keys[i]) {
8367 lun->pr_keys[i] = 0;
8368 lun->pr_key_count--;
8371 && i < CTL_MAX_INITIATORS)
8372 lun->pending_ua[i] |=
8374 else if (persis_offset
8375 && i >= persis_offset)
8376 lun->pending_ua[i-persis_offset] |=
8378 } else if (type != lun->res_type
8379 && (lun->res_type == SPR_TYPE_WR_EX_RO
8380 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
8382 && i < CTL_MAX_INITIATORS)
8383 lun->pending_ua[i] |=
8385 else if (persis_offset
8386 && i >= persis_offset)
8392 lun->res_type = type;
8393 if (lun->res_type != SPR_TYPE_WR_EX_AR
8394 && lun->res_type != SPR_TYPE_EX_AC_AR)
8395 lun->pr_res_idx = residx;
8397 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8399 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8400 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8401 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8402 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8403 persis_io.pr.pr_info.res_type = type;
8404 memcpy(persis_io.pr.pr_info.sa_res_key,
8405 param->serv_act_res_key,
8406 sizeof(param->serv_act_res_key));
8407 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8408 &persis_io, sizeof(persis_io), 0)) >
8409 CTL_HA_STATUS_SUCCESS) {
8410 printf("CTL:Persis Out error returned "
8411 "from ctl_ha_msg_send %d\n",
8416 * sa_res_key is not the res holder just
8417 * remove registrants
8421 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8422 if (sa_res_key != lun->pr_keys[i])
8426 lun->pr_keys[i] = 0;
8427 lun->pr_key_count--;
8430 && i < CTL_MAX_INITIATORS)
8431 lun->pending_ua[i] |=
8433 else if (persis_offset
8434 && i >= persis_offset)
8435 lun->pending_ua[i-persis_offset] |=
8440 mtx_unlock(&lun->lun_lock);
8441 free(ctsio->kern_data_ptr, M_CTL);
8442 ctl_set_reservation_conflict(ctsio);
8443 ctl_done((union ctl_io *)ctsio);
8446 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8447 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8448 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8449 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8450 persis_io.pr.pr_info.res_type = type;
8451 memcpy(persis_io.pr.pr_info.sa_res_key,
8452 param->serv_act_res_key,
8453 sizeof(param->serv_act_res_key));
8454 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8455 &persis_io, sizeof(persis_io), 0)) >
8456 CTL_HA_STATUS_SUCCESS) {
8457 printf("CTL:Persis Out error returned "
8458 "from ctl_ha_msg_send %d\n",
8464 lun->PRGeneration++;
8465 mtx_unlock(&lun->lun_lock);
8471 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8473 uint64_t sa_res_key;
8476 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8478 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8479 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8480 || sa_res_key != lun->pr_keys[lun->pr_res_idx]) {
8481 if (sa_res_key == 0) {
8483 * Unregister everybody else and build UA for
8486 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8487 if (i == msg->pr.pr_info.residx ||
8488 lun->pr_keys[i] == 0)
8492 && i < CTL_MAX_INITIATORS)
8493 lun->pending_ua[i] |=
8495 else if (persis_offset && i >= persis_offset)
8496 lun->pending_ua[i - persis_offset] |=
8498 lun->pr_keys[i] = 0;
8501 lun->pr_key_count = 1;
8502 lun->res_type = msg->pr.pr_info.res_type;
8503 if (lun->res_type != SPR_TYPE_WR_EX_AR
8504 && lun->res_type != SPR_TYPE_EX_AC_AR)
8505 lun->pr_res_idx = msg->pr.pr_info.residx;
8507 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8508 if (sa_res_key == lun->pr_keys[i])
8511 lun->pr_keys[i] = 0;
8512 lun->pr_key_count--;
8515 && i < persis_offset)
8516 lun->pending_ua[i] |=
8518 else if (persis_offset
8519 && i >= persis_offset)
8520 lun->pending_ua[i - persis_offset] |=
8525 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8526 if (i == msg->pr.pr_info.residx ||
8527 lun->pr_keys[i] == 0)
8530 if (sa_res_key == lun->pr_keys[i]) {
8531 lun->pr_keys[i] = 0;
8532 lun->pr_key_count--;
8534 && i < CTL_MAX_INITIATORS)
8535 lun->pending_ua[i] |=
8537 else if (persis_offset
8538 && i >= persis_offset)
8539 lun->pending_ua[i - persis_offset] |=
8541 } else if (msg->pr.pr_info.res_type != lun->res_type
8542 && (lun->res_type == SPR_TYPE_WR_EX_RO
8543 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
8545 && i < persis_offset)
8546 lun->pending_ua[i] |=
8548 else if (persis_offset
8549 && i >= persis_offset)
8550 lun->pending_ua[i - persis_offset] |=
8554 lun->res_type = msg->pr.pr_info.res_type;
8555 if (lun->res_type != SPR_TYPE_WR_EX_AR
8556 && lun->res_type != SPR_TYPE_EX_AC_AR)
8557 lun->pr_res_idx = msg->pr.pr_info.residx;
8559 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8561 lun->PRGeneration++;
8567 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8571 u_int32_t param_len;
8572 struct scsi_per_res_out *cdb;
8573 struct ctl_lun *lun;
8574 struct scsi_per_res_out_parms* param;
8575 struct ctl_softc *softc;
8577 uint64_t res_key, sa_res_key;
8579 union ctl_ha_msg persis_io;
8582 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8584 retval = CTL_RETVAL_COMPLETE;
8586 softc = control_softc;
8588 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8589 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8592 * We only support whole-LUN scope. The scope & type are ignored for
8593 * register, register and ignore existing key and clear.
8594 * We sometimes ignore scope and type on preempts too!!
8595 * Verify reservation type here as well.
8597 type = cdb->scope_type & SPR_TYPE_MASK;
8598 if ((cdb->action == SPRO_RESERVE)
8599 || (cdb->action == SPRO_RELEASE)) {
8600 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8601 ctl_set_invalid_field(/*ctsio*/ ctsio,
8607 ctl_done((union ctl_io *)ctsio);
8608 return (CTL_RETVAL_COMPLETE);
8611 if (type>8 || type==2 || type==4 || type==0) {
8612 ctl_set_invalid_field(/*ctsio*/ ctsio,
8618 ctl_done((union ctl_io *)ctsio);
8619 return (CTL_RETVAL_COMPLETE);
8623 param_len = scsi_4btoul(cdb->length);
8625 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8626 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8627 ctsio->kern_data_len = param_len;
8628 ctsio->kern_total_len = param_len;
8629 ctsio->kern_data_resid = 0;
8630 ctsio->kern_rel_offset = 0;
8631 ctsio->kern_sg_entries = 0;
8632 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8633 ctsio->be_move_done = ctl_config_move_done;
8634 ctl_datamove((union ctl_io *)ctsio);
8636 return (CTL_RETVAL_COMPLETE);
8639 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8641 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8642 res_key = scsi_8btou64(param->res_key.key);
8643 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8646 * Validate the reservation key here except for SPRO_REG_IGNO
8647 * This must be done for all other service actions
8649 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8650 mtx_lock(&lun->lun_lock);
8651 if (lun->pr_keys[residx] != 0) {
8652 if (res_key != lun->pr_keys[residx]) {
8654 * The current key passed in doesn't match
8655 * the one the initiator previously
8658 mtx_unlock(&lun->lun_lock);
8659 free(ctsio->kern_data_ptr, M_CTL);
8660 ctl_set_reservation_conflict(ctsio);
8661 ctl_done((union ctl_io *)ctsio);
8662 return (CTL_RETVAL_COMPLETE);
8664 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8666 * We are not registered
8668 mtx_unlock(&lun->lun_lock);
8669 free(ctsio->kern_data_ptr, M_CTL);
8670 ctl_set_reservation_conflict(ctsio);
8671 ctl_done((union ctl_io *)ctsio);
8672 return (CTL_RETVAL_COMPLETE);
8673 } else if (res_key != 0) {
8675 * We are not registered and trying to register but
8676 * the register key isn't zero.
8678 mtx_unlock(&lun->lun_lock);
8679 free(ctsio->kern_data_ptr, M_CTL);
8680 ctl_set_reservation_conflict(ctsio);
8681 ctl_done((union ctl_io *)ctsio);
8682 return (CTL_RETVAL_COMPLETE);
8684 mtx_unlock(&lun->lun_lock);
8687 switch (cdb->action & SPRO_ACTION_MASK) {
8689 case SPRO_REG_IGNO: {
8692 printf("Registration received\n");
8696 * We don't support any of these options, as we report in
8697 * the read capabilities request (see
8698 * ctl_persistent_reserve_in(), above).
8700 if ((param->flags & SPR_SPEC_I_PT)
8701 || (param->flags & SPR_ALL_TG_PT)
8702 || (param->flags & SPR_APTPL)) {
8705 if (param->flags & SPR_APTPL)
8707 else if (param->flags & SPR_ALL_TG_PT)
8709 else /* SPR_SPEC_I_PT */
8712 free(ctsio->kern_data_ptr, M_CTL);
8713 ctl_set_invalid_field(ctsio,
8719 ctl_done((union ctl_io *)ctsio);
8720 return (CTL_RETVAL_COMPLETE);
8723 mtx_lock(&lun->lun_lock);
8726 * The initiator wants to clear the
8729 if (sa_res_key == 0) {
8731 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8732 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8733 && lun->pr_keys[residx] == 0)) {
8734 mtx_unlock(&lun->lun_lock);
8738 lun->pr_keys[residx] = 0;
8739 lun->pr_key_count--;
8741 if (residx == lun->pr_res_idx) {
8742 lun->flags &= ~CTL_LUN_PR_RESERVED;
8743 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8745 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8746 || lun->res_type == SPR_TYPE_EX_AC_RO)
8747 && lun->pr_key_count) {
8749 * If the reservation is a registrants
8750 * only type we need to generate a UA
8751 * for other registered inits. The
8752 * sense code should be RESERVATIONS
8756 for (i = 0; i < CTL_MAX_INITIATORS;i++){
8758 i + persis_offset] == 0)
8760 lun->pending_ua[i] |=
8765 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8766 if (lun->pr_key_count==0) {
8767 lun->flags &= ~CTL_LUN_PR_RESERVED;
8769 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8772 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8773 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8774 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8775 persis_io.pr.pr_info.residx = residx;
8776 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8777 &persis_io, sizeof(persis_io), 0 )) >
8778 CTL_HA_STATUS_SUCCESS) {
8779 printf("CTL:Persis Out error returned from "
8780 "ctl_ha_msg_send %d\n", isc_retval);
8782 } else /* sa_res_key != 0 */ {
8785 * If we aren't registered currently then increment
8786 * the key count and set the registered flag.
8788 if (lun->pr_keys[residx] == 0)
8789 lun->pr_key_count++;
8790 lun->pr_keys[residx] = sa_res_key;
8792 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8793 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8794 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8795 persis_io.pr.pr_info.residx = residx;
8796 memcpy(persis_io.pr.pr_info.sa_res_key,
8797 param->serv_act_res_key,
8798 sizeof(param->serv_act_res_key));
8799 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8800 &persis_io, sizeof(persis_io), 0)) >
8801 CTL_HA_STATUS_SUCCESS) {
8802 printf("CTL:Persis Out error returned from "
8803 "ctl_ha_msg_send %d\n", isc_retval);
8806 lun->PRGeneration++;
8807 mtx_unlock(&lun->lun_lock);
8813 printf("Reserve executed type %d\n", type);
8815 mtx_lock(&lun->lun_lock);
8816 if (lun->flags & CTL_LUN_PR_RESERVED) {
8818 * if this isn't the reservation holder and it's
8819 * not a "all registrants" type or if the type is
8820 * different then we have a conflict
8822 if ((lun->pr_res_idx != residx
8823 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8824 || lun->res_type != type) {
8825 mtx_unlock(&lun->lun_lock);
8826 free(ctsio->kern_data_ptr, M_CTL);
8827 ctl_set_reservation_conflict(ctsio);
8828 ctl_done((union ctl_io *)ctsio);
8829 return (CTL_RETVAL_COMPLETE);
8831 mtx_unlock(&lun->lun_lock);
8832 } else /* create a reservation */ {
8834 * If it's not an "all registrants" type record
8835 * reservation holder
8837 if (type != SPR_TYPE_WR_EX_AR
8838 && type != SPR_TYPE_EX_AC_AR)
8839 lun->pr_res_idx = residx; /* Res holder */
8841 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8843 lun->flags |= CTL_LUN_PR_RESERVED;
8844 lun->res_type = type;
8846 mtx_unlock(&lun->lun_lock);
8848 /* send msg to other side */
8849 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8850 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8851 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8852 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8853 persis_io.pr.pr_info.res_type = type;
8854 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8855 &persis_io, sizeof(persis_io), 0)) >
8856 CTL_HA_STATUS_SUCCESS) {
8857 printf("CTL:Persis Out error returned from "
8858 "ctl_ha_msg_send %d\n", isc_retval);
8864 mtx_lock(&lun->lun_lock);
8865 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8866 /* No reservation exists return good status */
8867 mtx_unlock(&lun->lun_lock);
8871 * Is this nexus a reservation holder?
8873 if (lun->pr_res_idx != residx
8874 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8876 * not a res holder return good status but
8879 mtx_unlock(&lun->lun_lock);
8883 if (lun->res_type != type) {
8884 mtx_unlock(&lun->lun_lock);
8885 free(ctsio->kern_data_ptr, M_CTL);
8886 ctl_set_illegal_pr_release(ctsio);
8887 ctl_done((union ctl_io *)ctsio);
8888 return (CTL_RETVAL_COMPLETE);
8891 /* okay to release */
8892 lun->flags &= ~CTL_LUN_PR_RESERVED;
8893 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8897 * if this isn't an exclusive access
8898 * res generate UA for all other
8901 if (type != SPR_TYPE_EX_AC
8902 && type != SPR_TYPE_WR_EX) {
8903 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8905 lun->pr_keys[i + persis_offset] == 0)
8907 lun->pending_ua[i] |= CTL_UA_RES_RELEASE;
8910 mtx_unlock(&lun->lun_lock);
8911 /* Send msg to other side */
8912 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8913 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8914 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8915 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io,
8916 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8917 printf("CTL:Persis Out error returned from "
8918 "ctl_ha_msg_send %d\n", isc_retval);
8923 /* send msg to other side */
8925 mtx_lock(&lun->lun_lock);
8926 lun->flags &= ~CTL_LUN_PR_RESERVED;
8928 lun->pr_key_count = 0;
8929 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8931 lun->pr_keys[residx] = 0;
8933 for (i=0; i < 2*CTL_MAX_INITIATORS; i++)
8934 if (lun->pr_keys[i] != 0) {
8935 if (!persis_offset && i < CTL_MAX_INITIATORS)
8936 lun->pending_ua[i] |=
8938 else if (persis_offset && i >= persis_offset)
8939 lun->pending_ua[i-persis_offset] |=
8942 lun->pr_keys[i] = 0;
8944 lun->PRGeneration++;
8945 mtx_unlock(&lun->lun_lock);
8946 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8947 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8948 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8949 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8950 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8951 printf("CTL:Persis Out error returned from "
8952 "ctl_ha_msg_send %d\n", isc_retval);
8957 case SPRO_PRE_ABO: {
8960 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8961 residx, ctsio, cdb, param);
8963 return (CTL_RETVAL_COMPLETE);
8967 panic("Invalid PR type %x", cdb->action);
8971 free(ctsio->kern_data_ptr, M_CTL);
8972 ctl_set_success(ctsio);
8973 ctl_done((union ctl_io *)ctsio);
8979 * This routine is for handling a message from the other SC pertaining to
8980 * persistent reserve out. All the error checking will have been done
8981 * so only perorming the action need be done here to keep the two
8985 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8987 struct ctl_lun *lun;
8988 struct ctl_softc *softc;
8992 softc = control_softc;
8994 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8995 lun = softc->ctl_luns[targ_lun];
8996 mtx_lock(&lun->lun_lock);
8997 switch(msg->pr.pr_info.action) {
8998 case CTL_PR_REG_KEY:
8999 if (lun->pr_keys[msg->pr.pr_info.residx] == 0)
9000 lun->pr_key_count++;
9001 lun->pr_keys[msg->pr.pr_info.residx] =
9002 scsi_8btou64(msg->pr.pr_info.sa_res_key);
9003 lun->PRGeneration++;
9006 case CTL_PR_UNREG_KEY:
9007 lun->pr_keys[msg->pr.pr_info.residx] = 0;
9008 lun->pr_key_count--;
9010 /* XXX Need to see if the reservation has been released */
9011 /* if so do we need to generate UA? */
9012 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
9013 lun->flags &= ~CTL_LUN_PR_RESERVED;
9014 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
9016 if ((lun->res_type == SPR_TYPE_WR_EX_RO
9017 || lun->res_type == SPR_TYPE_EX_AC_RO)
9018 && lun->pr_key_count) {
9020 * If the reservation is a registrants
9021 * only type we need to generate a UA
9022 * for other registered inits. The
9023 * sense code should be RESERVATIONS
9027 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
9029 persis_offset] == 0)
9032 lun->pending_ua[i] |=
9037 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
9038 if (lun->pr_key_count==0) {
9039 lun->flags &= ~CTL_LUN_PR_RESERVED;
9041 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
9044 lun->PRGeneration++;
9047 case CTL_PR_RESERVE:
9048 lun->flags |= CTL_LUN_PR_RESERVED;
9049 lun->res_type = msg->pr.pr_info.res_type;
9050 lun->pr_res_idx = msg->pr.pr_info.residx;
9054 case CTL_PR_RELEASE:
9056 * if this isn't an exclusive access res generate UA for all
9057 * other registrants.
9059 if (lun->res_type != SPR_TYPE_EX_AC
9060 && lun->res_type != SPR_TYPE_WR_EX) {
9061 for (i = 0; i < CTL_MAX_INITIATORS; i++)
9062 if (lun->pr_keys[i+persis_offset] != 0)
9063 lun->pending_ua[i] |=
9067 lun->flags &= ~CTL_LUN_PR_RESERVED;
9068 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
9072 case CTL_PR_PREEMPT:
9073 ctl_pro_preempt_other(lun, msg);
9076 lun->flags &= ~CTL_LUN_PR_RESERVED;
9078 lun->pr_key_count = 0;
9079 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
9081 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
9082 if (lun->pr_keys[i] == 0)
9085 && i < CTL_MAX_INITIATORS)
9086 lun->pending_ua[i] |= CTL_UA_RES_PREEMPT;
9087 else if (persis_offset
9088 && i >= persis_offset)
9089 lun->pending_ua[i-persis_offset] |=
9091 lun->pr_keys[i] = 0;
9093 lun->PRGeneration++;
9097 mtx_unlock(&lun->lun_lock);
9101 ctl_read_write(struct ctl_scsiio *ctsio)
9103 struct ctl_lun *lun;
9104 struct ctl_lba_len_flags *lbalen;
9106 uint32_t num_blocks;
9110 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9112 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
9115 retval = CTL_RETVAL_COMPLETE;
9117 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
9118 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
9119 switch (ctsio->cdb[0]) {
9122 struct scsi_rw_6 *cdb;
9124 cdb = (struct scsi_rw_6 *)ctsio->cdb;
9126 lba = scsi_3btoul(cdb->addr);
9127 /* only 5 bits are valid in the most significant address byte */
9129 num_blocks = cdb->length;
9131 * This is correct according to SBC-2.
9133 if (num_blocks == 0)
9139 struct scsi_rw_10 *cdb;
9141 cdb = (struct scsi_rw_10 *)ctsio->cdb;
9142 if (cdb->byte2 & SRW10_FUA)
9143 flags |= CTL_LLF_FUA;
9144 if (cdb->byte2 & SRW10_DPO)
9145 flags |= CTL_LLF_DPO;
9146 lba = scsi_4btoul(cdb->addr);
9147 num_blocks = scsi_2btoul(cdb->length);
9150 case WRITE_VERIFY_10: {
9151 struct scsi_write_verify_10 *cdb;
9153 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
9154 flags |= CTL_LLF_FUA;
9155 if (cdb->byte2 & SWV_DPO)
9156 flags |= CTL_LLF_DPO;
9157 lba = scsi_4btoul(cdb->addr);
9158 num_blocks = scsi_2btoul(cdb->length);
9163 struct scsi_rw_12 *cdb;
9165 cdb = (struct scsi_rw_12 *)ctsio->cdb;
9166 if (cdb->byte2 & SRW12_FUA)
9167 flags |= CTL_LLF_FUA;
9168 if (cdb->byte2 & SRW12_DPO)
9169 flags |= CTL_LLF_DPO;
9170 lba = scsi_4btoul(cdb->addr);
9171 num_blocks = scsi_4btoul(cdb->length);
9174 case WRITE_VERIFY_12: {
9175 struct scsi_write_verify_12 *cdb;
9177 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
9178 flags |= CTL_LLF_FUA;
9179 if (cdb->byte2 & SWV_DPO)
9180 flags |= CTL_LLF_DPO;
9181 lba = scsi_4btoul(cdb->addr);
9182 num_blocks = scsi_4btoul(cdb->length);
9187 struct scsi_rw_16 *cdb;
9189 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9190 if (cdb->byte2 & SRW12_FUA)
9191 flags |= CTL_LLF_FUA;
9192 if (cdb->byte2 & SRW12_DPO)
9193 flags |= CTL_LLF_DPO;
9194 lba = scsi_8btou64(cdb->addr);
9195 num_blocks = scsi_4btoul(cdb->length);
9198 case WRITE_ATOMIC_16: {
9199 struct scsi_rw_16 *cdb;
9201 if (lun->be_lun->atomicblock == 0) {
9202 ctl_set_invalid_opcode(ctsio);
9203 ctl_done((union ctl_io *)ctsio);
9204 return (CTL_RETVAL_COMPLETE);
9207 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9208 if (cdb->byte2 & SRW12_FUA)
9209 flags |= CTL_LLF_FUA;
9210 if (cdb->byte2 & SRW12_DPO)
9211 flags |= CTL_LLF_DPO;
9212 lba = scsi_8btou64(cdb->addr);
9213 num_blocks = scsi_4btoul(cdb->length);
9214 if (num_blocks > lun->be_lun->atomicblock) {
9215 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
9216 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
9218 ctl_done((union ctl_io *)ctsio);
9219 return (CTL_RETVAL_COMPLETE);
9223 case WRITE_VERIFY_16: {
9224 struct scsi_write_verify_16 *cdb;
9226 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
9227 flags |= CTL_LLF_FUA;
9228 if (cdb->byte2 & SWV_DPO)
9229 flags |= CTL_LLF_DPO;
9230 lba = scsi_8btou64(cdb->addr);
9231 num_blocks = scsi_4btoul(cdb->length);
9236 * We got a command we don't support. This shouldn't
9237 * happen, commands should be filtered out above us.
9239 ctl_set_invalid_opcode(ctsio);
9240 ctl_done((union ctl_io *)ctsio);
9242 return (CTL_RETVAL_COMPLETE);
9243 break; /* NOTREACHED */
9247 * The first check is to make sure we're in bounds, the second
9248 * check is to catch wrap-around problems. If the lba + num blocks
9249 * is less than the lba, then we've wrapped around and the block
9250 * range is invalid anyway.
9252 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9253 || ((lba + num_blocks) < lba)) {
9254 ctl_set_lba_out_of_range(ctsio);
9255 ctl_done((union ctl_io *)ctsio);
9256 return (CTL_RETVAL_COMPLETE);
9260 * According to SBC-3, a transfer length of 0 is not an error.
9261 * Note that this cannot happen with WRITE(6) or READ(6), since 0
9262 * translates to 256 blocks for those commands.
9264 if (num_blocks == 0) {
9265 ctl_set_success(ctsio);
9266 ctl_done((union ctl_io *)ctsio);
9267 return (CTL_RETVAL_COMPLETE);
9270 /* Set FUA and/or DPO if caches are disabled. */
9272 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9274 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
9276 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9278 flags |= CTL_LLF_FUA;
9281 lbalen = (struct ctl_lba_len_flags *)
9282 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9284 lbalen->len = num_blocks;
9285 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
9287 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9288 ctsio->kern_rel_offset = 0;
9290 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
9292 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9298 ctl_cnw_cont(union ctl_io *io)
9300 struct ctl_scsiio *ctsio;
9301 struct ctl_lun *lun;
9302 struct ctl_lba_len_flags *lbalen;
9305 ctsio = &io->scsiio;
9306 ctsio->io_hdr.status = CTL_STATUS_NONE;
9307 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
9308 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9309 lbalen = (struct ctl_lba_len_flags *)
9310 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9311 lbalen->flags &= ~CTL_LLF_COMPARE;
9312 lbalen->flags |= CTL_LLF_WRITE;
9314 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
9315 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9320 ctl_cnw(struct ctl_scsiio *ctsio)
9322 struct ctl_lun *lun;
9323 struct ctl_lba_len_flags *lbalen;
9325 uint32_t num_blocks;
9328 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9330 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
9333 retval = CTL_RETVAL_COMPLETE;
9335 switch (ctsio->cdb[0]) {
9336 case COMPARE_AND_WRITE: {
9337 struct scsi_compare_and_write *cdb;
9339 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
9340 if (cdb->byte2 & SRW10_FUA)
9341 flags |= CTL_LLF_FUA;
9342 if (cdb->byte2 & SRW10_DPO)
9343 flags |= CTL_LLF_DPO;
9344 lba = scsi_8btou64(cdb->addr);
9345 num_blocks = cdb->length;
9350 * We got a command we don't support. This shouldn't
9351 * happen, commands should be filtered out above us.
9353 ctl_set_invalid_opcode(ctsio);
9354 ctl_done((union ctl_io *)ctsio);
9356 return (CTL_RETVAL_COMPLETE);
9357 break; /* NOTREACHED */
9361 * The first check is to make sure we're in bounds, the second
9362 * check is to catch wrap-around problems. If the lba + num blocks
9363 * is less than the lba, then we've wrapped around and the block
9364 * range is invalid anyway.
9366 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9367 || ((lba + num_blocks) < lba)) {
9368 ctl_set_lba_out_of_range(ctsio);
9369 ctl_done((union ctl_io *)ctsio);
9370 return (CTL_RETVAL_COMPLETE);
9374 * According to SBC-3, a transfer length of 0 is not an error.
9376 if (num_blocks == 0) {
9377 ctl_set_success(ctsio);
9378 ctl_done((union ctl_io *)ctsio);
9379 return (CTL_RETVAL_COMPLETE);
9382 /* Set FUA if write cache is disabled. */
9383 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9385 flags |= CTL_LLF_FUA;
9387 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
9388 ctsio->kern_rel_offset = 0;
9391 * Set the IO_CONT flag, so that if this I/O gets passed to
9392 * ctl_data_submit_done(), it'll get passed back to
9393 * ctl_ctl_cnw_cont() for further processing.
9395 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
9396 ctsio->io_cont = ctl_cnw_cont;
9398 lbalen = (struct ctl_lba_len_flags *)
9399 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9401 lbalen->len = num_blocks;
9402 lbalen->flags = CTL_LLF_COMPARE | flags;
9404 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
9405 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9410 ctl_verify(struct ctl_scsiio *ctsio)
9412 struct ctl_lun *lun;
9413 struct ctl_lba_len_flags *lbalen;
9415 uint32_t num_blocks;
9419 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9421 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
9424 flags = CTL_LLF_FUA;
9425 retval = CTL_RETVAL_COMPLETE;
9427 switch (ctsio->cdb[0]) {
9429 struct scsi_verify_10 *cdb;
9431 cdb = (struct scsi_verify_10 *)ctsio->cdb;
9432 if (cdb->byte2 & SVFY_BYTCHK)
9434 if (cdb->byte2 & SVFY_DPO)
9435 flags |= CTL_LLF_DPO;
9436 lba = scsi_4btoul(cdb->addr);
9437 num_blocks = scsi_2btoul(cdb->length);
9441 struct scsi_verify_12 *cdb;
9443 cdb = (struct scsi_verify_12 *)ctsio->cdb;
9444 if (cdb->byte2 & SVFY_BYTCHK)
9446 if (cdb->byte2 & SVFY_DPO)
9447 flags |= CTL_LLF_DPO;
9448 lba = scsi_4btoul(cdb->addr);
9449 num_blocks = scsi_4btoul(cdb->length);
9453 struct scsi_rw_16 *cdb;
9455 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9456 if (cdb->byte2 & SVFY_BYTCHK)
9458 if (cdb->byte2 & SVFY_DPO)
9459 flags |= CTL_LLF_DPO;
9460 lba = scsi_8btou64(cdb->addr);
9461 num_blocks = scsi_4btoul(cdb->length);
9466 * We got a command we don't support. This shouldn't
9467 * happen, commands should be filtered out above us.
9469 ctl_set_invalid_opcode(ctsio);
9470 ctl_done((union ctl_io *)ctsio);
9471 return (CTL_RETVAL_COMPLETE);
9475 * The first check is to make sure we're in bounds, the second
9476 * check is to catch wrap-around problems. If the lba + num blocks
9477 * is less than the lba, then we've wrapped around and the block
9478 * range is invalid anyway.
9480 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9481 || ((lba + num_blocks) < lba)) {
9482 ctl_set_lba_out_of_range(ctsio);
9483 ctl_done((union ctl_io *)ctsio);
9484 return (CTL_RETVAL_COMPLETE);
9488 * According to SBC-3, a transfer length of 0 is not an error.
9490 if (num_blocks == 0) {
9491 ctl_set_success(ctsio);
9492 ctl_done((union ctl_io *)ctsio);
9493 return (CTL_RETVAL_COMPLETE);
9496 lbalen = (struct ctl_lba_len_flags *)
9497 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9499 lbalen->len = num_blocks;
9501 lbalen->flags = CTL_LLF_COMPARE | flags;
9502 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9504 lbalen->flags = CTL_LLF_VERIFY | flags;
9505 ctsio->kern_total_len = 0;
9507 ctsio->kern_rel_offset = 0;
9509 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9510 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9515 ctl_report_luns(struct ctl_scsiio *ctsio)
9517 struct scsi_report_luns *cdb;
9518 struct scsi_report_luns_data *lun_data;
9519 struct ctl_lun *lun, *request_lun;
9520 int num_luns, retval;
9521 uint32_t alloc_len, lun_datalen;
9522 int num_filled, well_known;
9523 uint32_t initidx, targ_lun_id, lun_id;
9525 retval = CTL_RETVAL_COMPLETE;
9528 cdb = (struct scsi_report_luns *)ctsio->cdb;
9530 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9532 mtx_lock(&control_softc->ctl_lock);
9533 num_luns = control_softc->num_luns;
9534 mtx_unlock(&control_softc->ctl_lock);
9536 switch (cdb->select_report) {
9537 case RPL_REPORT_DEFAULT:
9538 case RPL_REPORT_ALL:
9540 case RPL_REPORT_WELLKNOWN:
9545 ctl_set_invalid_field(ctsio,
9551 ctl_done((union ctl_io *)ctsio);
9553 break; /* NOTREACHED */
9556 alloc_len = scsi_4btoul(cdb->length);
9558 * The initiator has to allocate at least 16 bytes for this request,
9559 * so he can at least get the header and the first LUN. Otherwise
9560 * we reject the request (per SPC-3 rev 14, section 6.21).
9562 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9563 sizeof(struct scsi_report_luns_lundata))) {
9564 ctl_set_invalid_field(ctsio,
9570 ctl_done((union ctl_io *)ctsio);
9574 request_lun = (struct ctl_lun *)
9575 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9577 lun_datalen = sizeof(*lun_data) +
9578 (num_luns * sizeof(struct scsi_report_luns_lundata));
9580 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9581 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9582 ctsio->kern_sg_entries = 0;
9584 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9586 mtx_lock(&control_softc->ctl_lock);
9587 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9588 lun_id = ctl_map_lun(ctsio->io_hdr.nexus.targ_port, targ_lun_id);
9589 if (lun_id >= CTL_MAX_LUNS)
9591 lun = control_softc->ctl_luns[lun_id];
9595 if (targ_lun_id <= 0xff) {
9597 * Peripheral addressing method, bus number 0.
9599 lun_data->luns[num_filled].lundata[0] =
9600 RPL_LUNDATA_ATYP_PERIPH;
9601 lun_data->luns[num_filled].lundata[1] = targ_lun_id;
9603 } else if (targ_lun_id <= 0x3fff) {
9605 * Flat addressing method.
9607 lun_data->luns[num_filled].lundata[0] =
9608 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8);
9609 lun_data->luns[num_filled].lundata[1] =
9610 (targ_lun_id & 0xff);
9612 } else if (targ_lun_id <= 0xffffff) {
9614 * Extended flat addressing method.
9616 lun_data->luns[num_filled].lundata[0] =
9617 RPL_LUNDATA_ATYP_EXTLUN | 0x12;
9618 scsi_ulto3b(targ_lun_id,
9619 &lun_data->luns[num_filled].lundata[1]);
9622 printf("ctl_report_luns: bogus LUN number %jd, "
9623 "skipping\n", (intmax_t)targ_lun_id);
9626 * According to SPC-3, rev 14 section 6.21:
9628 * "The execution of a REPORT LUNS command to any valid and
9629 * installed logical unit shall clear the REPORTED LUNS DATA
9630 * HAS CHANGED unit attention condition for all logical
9631 * units of that target with respect to the requesting
9632 * initiator. A valid and installed logical unit is one
9633 * having a PERIPHERAL QUALIFIER of 000b in the standard
9634 * INQUIRY data (see 6.4.2)."
9636 * If request_lun is NULL, the LUN this report luns command
9637 * was issued to is either disabled or doesn't exist. In that
9638 * case, we shouldn't clear any pending lun change unit
9641 if (request_lun != NULL) {
9642 mtx_lock(&lun->lun_lock);
9643 lun->pending_ua[initidx] &= ~CTL_UA_LUN_CHANGE;
9644 mtx_unlock(&lun->lun_lock);
9647 mtx_unlock(&control_softc->ctl_lock);
9650 * It's quite possible that we've returned fewer LUNs than we allocated
9651 * space for. Trim it.
9653 lun_datalen = sizeof(*lun_data) +
9654 (num_filled * sizeof(struct scsi_report_luns_lundata));
9656 if (lun_datalen < alloc_len) {
9657 ctsio->residual = alloc_len - lun_datalen;
9658 ctsio->kern_data_len = lun_datalen;
9659 ctsio->kern_total_len = lun_datalen;
9661 ctsio->residual = 0;
9662 ctsio->kern_data_len = alloc_len;
9663 ctsio->kern_total_len = alloc_len;
9665 ctsio->kern_data_resid = 0;
9666 ctsio->kern_rel_offset = 0;
9667 ctsio->kern_sg_entries = 0;
9670 * We set this to the actual data length, regardless of how much
9671 * space we actually have to return results. If the user looks at
9672 * this value, he'll know whether or not he allocated enough space
9673 * and reissue the command if necessary. We don't support well
9674 * known logical units, so if the user asks for that, return none.
9676 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9679 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9682 ctsio->scsi_status = SCSI_STATUS_OK;
9684 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9685 ctsio->be_move_done = ctl_config_move_done;
9686 ctl_datamove((union ctl_io *)ctsio);
9692 ctl_request_sense(struct ctl_scsiio *ctsio)
9694 struct scsi_request_sense *cdb;
9695 struct scsi_sense_data *sense_ptr;
9696 struct ctl_lun *lun;
9699 scsi_sense_data_type sense_format;
9701 cdb = (struct scsi_request_sense *)ctsio->cdb;
9703 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9705 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9708 * Determine which sense format the user wants.
9710 if (cdb->byte2 & SRS_DESC)
9711 sense_format = SSD_TYPE_DESC;
9713 sense_format = SSD_TYPE_FIXED;
9715 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9716 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9717 ctsio->kern_sg_entries = 0;
9720 * struct scsi_sense_data, which is currently set to 256 bytes, is
9721 * larger than the largest allowed value for the length field in the
9722 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9724 ctsio->residual = 0;
9725 ctsio->kern_data_len = cdb->length;
9726 ctsio->kern_total_len = cdb->length;
9728 ctsio->kern_data_resid = 0;
9729 ctsio->kern_rel_offset = 0;
9730 ctsio->kern_sg_entries = 0;
9733 * If we don't have a LUN, we don't have any pending sense.
9739 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9741 * Check for pending sense, and then for pending unit attentions.
9742 * Pending sense gets returned first, then pending unit attentions.
9744 mtx_lock(&lun->lun_lock);
9746 if (ctl_is_set(lun->have_ca, initidx)) {
9747 scsi_sense_data_type stored_format;
9750 * Check to see which sense format was used for the stored
9753 stored_format = scsi_sense_type(&lun->pending_sense[initidx]);
9756 * If the user requested a different sense format than the
9757 * one we stored, then we need to convert it to the other
9758 * format. If we're going from descriptor to fixed format
9759 * sense data, we may lose things in translation, depending
9760 * on what options were used.
9762 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9763 * for some reason we'll just copy it out as-is.
9765 if ((stored_format == SSD_TYPE_FIXED)
9766 && (sense_format == SSD_TYPE_DESC))
9767 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9768 &lun->pending_sense[initidx],
9769 (struct scsi_sense_data_desc *)sense_ptr);
9770 else if ((stored_format == SSD_TYPE_DESC)
9771 && (sense_format == SSD_TYPE_FIXED))
9772 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9773 &lun->pending_sense[initidx],
9774 (struct scsi_sense_data_fixed *)sense_ptr);
9776 memcpy(sense_ptr, &lun->pending_sense[initidx],
9777 ctl_min(sizeof(*sense_ptr),
9778 sizeof(lun->pending_sense[initidx])));
9780 ctl_clear_mask(lun->have_ca, initidx);
9784 if (lun->pending_ua[initidx] != CTL_UA_NONE) {
9785 ctl_ua_type ua_type;
9787 ua_type = ctl_build_ua(&lun->pending_ua[initidx],
9788 sense_ptr, sense_format);
9789 if (ua_type != CTL_UA_NONE)
9792 mtx_unlock(&lun->lun_lock);
9795 * We already have a pending error, return it.
9797 if (have_error != 0) {
9799 * We report the SCSI status as OK, since the status of the
9800 * request sense command itself is OK.
9802 ctsio->scsi_status = SCSI_STATUS_OK;
9805 * We report 0 for the sense length, because we aren't doing
9806 * autosense in this case. We're reporting sense as
9809 ctsio->sense_len = 0;
9810 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9811 ctsio->be_move_done = ctl_config_move_done;
9812 ctl_datamove((union ctl_io *)ctsio);
9814 return (CTL_RETVAL_COMPLETE);
9820 * No sense information to report, so we report that everything is
9823 ctl_set_sense_data(sense_ptr,
9826 /*current_error*/ 1,
9827 /*sense_key*/ SSD_KEY_NO_SENSE,
9832 ctsio->scsi_status = SCSI_STATUS_OK;
9835 * We report 0 for the sense length, because we aren't doing
9836 * autosense in this case. We're reporting sense as parameter data.
9838 ctsio->sense_len = 0;
9839 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9840 ctsio->be_move_done = ctl_config_move_done;
9841 ctl_datamove((union ctl_io *)ctsio);
9843 return (CTL_RETVAL_COMPLETE);
9847 ctl_tur(struct ctl_scsiio *ctsio)
9850 CTL_DEBUG_PRINT(("ctl_tur\n"));
9852 ctsio->scsi_status = SCSI_STATUS_OK;
9853 ctsio->io_hdr.status = CTL_SUCCESS;
9855 ctl_done((union ctl_io *)ctsio);
9857 return (CTL_RETVAL_COMPLETE);
9862 ctl_cmddt_inquiry(struct ctl_scsiio *ctsio)
9869 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9871 struct scsi_vpd_supported_pages *pages;
9873 struct ctl_lun *lun;
9875 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9877 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9878 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9879 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9880 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9881 ctsio->kern_sg_entries = 0;
9883 if (sup_page_size < alloc_len) {
9884 ctsio->residual = alloc_len - sup_page_size;
9885 ctsio->kern_data_len = sup_page_size;
9886 ctsio->kern_total_len = sup_page_size;
9888 ctsio->residual = 0;
9889 ctsio->kern_data_len = alloc_len;
9890 ctsio->kern_total_len = alloc_len;
9892 ctsio->kern_data_resid = 0;
9893 ctsio->kern_rel_offset = 0;
9894 ctsio->kern_sg_entries = 0;
9897 * The control device is always connected. The disk device, on the
9898 * other hand, may not be online all the time. Need to change this
9899 * to figure out whether the disk device is actually online or not.
9902 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9903 lun->be_lun->lun_type;
9905 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9907 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES;
9908 /* Supported VPD pages */
9909 pages->page_list[0] = SVPD_SUPPORTED_PAGES;
9911 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER;
9912 /* Device Identification */
9913 pages->page_list[2] = SVPD_DEVICE_ID;
9914 /* Extended INQUIRY Data */
9915 pages->page_list[3] = SVPD_EXTENDED_INQUIRY_DATA;
9916 /* Mode Page Policy */
9917 pages->page_list[4] = SVPD_MODE_PAGE_POLICY;
9919 pages->page_list[5] = SVPD_SCSI_PORTS;
9920 /* Third-party Copy */
9921 pages->page_list[6] = SVPD_SCSI_TPC;
9923 pages->page_list[7] = SVPD_BLOCK_LIMITS;
9924 /* Block Device Characteristics */
9925 pages->page_list[8] = SVPD_BDC;
9926 /* Logical Block Provisioning */
9927 pages->page_list[9] = SVPD_LBP;
9929 ctsio->scsi_status = SCSI_STATUS_OK;
9931 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9932 ctsio->be_move_done = ctl_config_move_done;
9933 ctl_datamove((union ctl_io *)ctsio);
9935 return (CTL_RETVAL_COMPLETE);
9939 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9941 struct scsi_vpd_unit_serial_number *sn_ptr;
9942 struct ctl_lun *lun;
9945 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9947 data_len = 4 + CTL_SN_LEN;
9948 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9949 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9950 if (data_len < alloc_len) {
9951 ctsio->residual = alloc_len - data_len;
9952 ctsio->kern_data_len = data_len;
9953 ctsio->kern_total_len = data_len;
9955 ctsio->residual = 0;
9956 ctsio->kern_data_len = alloc_len;
9957 ctsio->kern_total_len = alloc_len;
9959 ctsio->kern_data_resid = 0;
9960 ctsio->kern_rel_offset = 0;
9961 ctsio->kern_sg_entries = 0;
9964 * The control device is always connected. The disk device, on the
9965 * other hand, may not be online all the time. Need to change this
9966 * to figure out whether the disk device is actually online or not.
9969 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9970 lun->be_lun->lun_type;
9972 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9974 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9975 sn_ptr->length = CTL_SN_LEN;
9977 * If we don't have a LUN, we just leave the serial number as
9981 strncpy((char *)sn_ptr->serial_num,
9982 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9984 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9985 ctsio->scsi_status = SCSI_STATUS_OK;
9987 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9988 ctsio->be_move_done = ctl_config_move_done;
9989 ctl_datamove((union ctl_io *)ctsio);
9991 return (CTL_RETVAL_COMPLETE);
9996 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9998 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9999 struct ctl_lun *lun;
10002 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10004 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
10005 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10006 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
10007 ctsio->kern_sg_entries = 0;
10009 if (data_len < alloc_len) {
10010 ctsio->residual = alloc_len - data_len;
10011 ctsio->kern_data_len = data_len;
10012 ctsio->kern_total_len = data_len;
10014 ctsio->residual = 0;
10015 ctsio->kern_data_len = alloc_len;
10016 ctsio->kern_total_len = alloc_len;
10018 ctsio->kern_data_resid = 0;
10019 ctsio->kern_rel_offset = 0;
10020 ctsio->kern_sg_entries = 0;
10023 * The control device is always connected. The disk device, on the
10024 * other hand, may not be online all the time.
10027 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10028 lun->be_lun->lun_type;
10030 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10031 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
10032 eid_ptr->page_length = data_len - 4;
10033 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
10034 eid_ptr->flags3 = SVPD_EID_V_SUP;
10036 ctsio->scsi_status = SCSI_STATUS_OK;
10037 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10038 ctsio->be_move_done = ctl_config_move_done;
10039 ctl_datamove((union ctl_io *)ctsio);
10041 return (CTL_RETVAL_COMPLETE);
10045 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
10047 struct scsi_vpd_mode_page_policy *mpp_ptr;
10048 struct ctl_lun *lun;
10051 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10053 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
10054 sizeof(struct scsi_vpd_mode_page_policy_descr);
10056 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10057 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
10058 ctsio->kern_sg_entries = 0;
10060 if (data_len < alloc_len) {
10061 ctsio->residual = alloc_len - data_len;
10062 ctsio->kern_data_len = data_len;
10063 ctsio->kern_total_len = data_len;
10065 ctsio->residual = 0;
10066 ctsio->kern_data_len = alloc_len;
10067 ctsio->kern_total_len = alloc_len;
10069 ctsio->kern_data_resid = 0;
10070 ctsio->kern_rel_offset = 0;
10071 ctsio->kern_sg_entries = 0;
10074 * The control device is always connected. The disk device, on the
10075 * other hand, may not be online all the time.
10078 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10079 lun->be_lun->lun_type;
10081 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10082 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
10083 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
10084 mpp_ptr->descr[0].page_code = 0x3f;
10085 mpp_ptr->descr[0].subpage_code = 0xff;
10086 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
10088 ctsio->scsi_status = SCSI_STATUS_OK;
10089 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10090 ctsio->be_move_done = ctl_config_move_done;
10091 ctl_datamove((union ctl_io *)ctsio);
10093 return (CTL_RETVAL_COMPLETE);
10097 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
10099 struct scsi_vpd_device_id *devid_ptr;
10100 struct scsi_vpd_id_descriptor *desc;
10101 struct ctl_softc *ctl_softc;
10102 struct ctl_lun *lun;
10103 struct ctl_port *port;
10107 ctl_softc = control_softc;
10109 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)];
10110 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10112 data_len = sizeof(struct scsi_vpd_device_id) +
10113 sizeof(struct scsi_vpd_id_descriptor) +
10114 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
10115 sizeof(struct scsi_vpd_id_descriptor) +
10116 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
10117 if (lun && lun->lun_devid)
10118 data_len += lun->lun_devid->len;
10119 if (port->port_devid)
10120 data_len += port->port_devid->len;
10121 if (port->target_devid)
10122 data_len += port->target_devid->len;
10124 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10125 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
10126 ctsio->kern_sg_entries = 0;
10128 if (data_len < alloc_len) {
10129 ctsio->residual = alloc_len - data_len;
10130 ctsio->kern_data_len = data_len;
10131 ctsio->kern_total_len = data_len;
10133 ctsio->residual = 0;
10134 ctsio->kern_data_len = alloc_len;
10135 ctsio->kern_total_len = alloc_len;
10137 ctsio->kern_data_resid = 0;
10138 ctsio->kern_rel_offset = 0;
10139 ctsio->kern_sg_entries = 0;
10142 * The control device is always connected. The disk device, on the
10143 * other hand, may not be online all the time.
10146 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10147 lun->be_lun->lun_type;
10149 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10150 devid_ptr->page_code = SVPD_DEVICE_ID;
10151 scsi_ulto2b(data_len - 4, devid_ptr->length);
10153 if (port->port_type == CTL_PORT_FC)
10154 proto = SCSI_PROTO_FC << 4;
10155 else if (port->port_type == CTL_PORT_ISCSI)
10156 proto = SCSI_PROTO_ISCSI << 4;
10158 proto = SCSI_PROTO_SPI << 4;
10159 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
10162 * We're using a LUN association here. i.e., this device ID is a
10163 * per-LUN identifier.
10165 if (lun && lun->lun_devid) {
10166 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
10167 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
10168 lun->lun_devid->len);
10172 * This is for the WWPN which is a port association.
10174 if (port->port_devid) {
10175 memcpy(desc, port->port_devid->data, port->port_devid->len);
10176 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
10177 port->port_devid->len);
10181 * This is for the Relative Target Port(type 4h) identifier
10183 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
10184 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
10185 SVPD_ID_TYPE_RELTARG;
10187 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
10188 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
10189 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
10192 * This is for the Target Port Group(type 5h) identifier
10194 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
10195 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
10196 SVPD_ID_TYPE_TPORTGRP;
10198 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1,
10199 &desc->identifier[2]);
10200 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
10201 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
10204 * This is for the Target identifier
10206 if (port->target_devid) {
10207 memcpy(desc, port->target_devid->data, port->target_devid->len);
10210 ctsio->scsi_status = SCSI_STATUS_OK;
10211 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10212 ctsio->be_move_done = ctl_config_move_done;
10213 ctl_datamove((union ctl_io *)ctsio);
10215 return (CTL_RETVAL_COMPLETE);
10219 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
10221 struct ctl_softc *softc = control_softc;
10222 struct scsi_vpd_scsi_ports *sp;
10223 struct scsi_vpd_port_designation *pd;
10224 struct scsi_vpd_port_designation_cont *pdc;
10225 struct ctl_lun *lun;
10226 struct ctl_port *port;
10227 int data_len, num_target_ports, iid_len, id_len, g, pg, p;
10228 int num_target_port_groups;
10230 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10232 if (softc->is_single)
10233 num_target_port_groups = 1;
10235 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
10236 num_target_ports = 0;
10239 mtx_lock(&softc->ctl_lock);
10240 STAILQ_FOREACH(port, &softc->port_list, links) {
10241 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
10244 ctl_map_lun_back(port->targ_port, lun->lun) >=
10247 num_target_ports++;
10248 if (port->init_devid)
10249 iid_len += port->init_devid->len;
10250 if (port->port_devid)
10251 id_len += port->port_devid->len;
10253 mtx_unlock(&softc->ctl_lock);
10255 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups *
10256 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
10257 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
10258 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10259 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
10260 ctsio->kern_sg_entries = 0;
10262 if (data_len < alloc_len) {
10263 ctsio->residual = alloc_len - data_len;
10264 ctsio->kern_data_len = data_len;
10265 ctsio->kern_total_len = data_len;
10267 ctsio->residual = 0;
10268 ctsio->kern_data_len = alloc_len;
10269 ctsio->kern_total_len = alloc_len;
10271 ctsio->kern_data_resid = 0;
10272 ctsio->kern_rel_offset = 0;
10273 ctsio->kern_sg_entries = 0;
10276 * The control device is always connected. The disk device, on the
10277 * other hand, may not be online all the time. Need to change this
10278 * to figure out whether the disk device is actually online or not.
10281 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
10282 lun->be_lun->lun_type;
10284 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10286 sp->page_code = SVPD_SCSI_PORTS;
10287 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
10289 pd = &sp->design[0];
10291 mtx_lock(&softc->ctl_lock);
10292 pg = softc->port_offset / CTL_MAX_PORTS;
10293 for (g = 0; g < num_target_port_groups; g++) {
10294 STAILQ_FOREACH(port, &softc->port_list, links) {
10295 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
10298 ctl_map_lun_back(port->targ_port, lun->lun) >=
10301 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS;
10302 scsi_ulto2b(p, pd->relative_port_id);
10303 if (port->init_devid && g == pg) {
10304 iid_len = port->init_devid->len;
10305 memcpy(pd->initiator_transportid,
10306 port->init_devid->data, port->init_devid->len);
10309 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
10310 pdc = (struct scsi_vpd_port_designation_cont *)
10311 (&pd->initiator_transportid[iid_len]);
10312 if (port->port_devid && g == pg) {
10313 id_len = port->port_devid->len;
10314 memcpy(pdc->target_port_descriptors,
10315 port->port_devid->data, port->port_devid->len);
10318 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
10319 pd = (struct scsi_vpd_port_designation *)
10320 ((uint8_t *)pdc->target_port_descriptors + id_len);
10323 mtx_unlock(&softc->ctl_lock);
10325 ctsio->scsi_status = SCSI_STATUS_OK;
10326 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10327 ctsio->be_move_done = ctl_config_move_done;
10328 ctl_datamove((union ctl_io *)ctsio);
10330 return (CTL_RETVAL_COMPLETE);
10334 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
10336 struct scsi_vpd_block_limits *bl_ptr;
10337 struct ctl_lun *lun;
10340 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10342 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
10343 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
10344 ctsio->kern_sg_entries = 0;
10346 if (sizeof(*bl_ptr) < alloc_len) {
10347 ctsio->residual = alloc_len - sizeof(*bl_ptr);
10348 ctsio->kern_data_len = sizeof(*bl_ptr);
10349 ctsio->kern_total_len = sizeof(*bl_ptr);
10351 ctsio->residual = 0;
10352 ctsio->kern_data_len = alloc_len;
10353 ctsio->kern_total_len = alloc_len;
10355 ctsio->kern_data_resid = 0;
10356 ctsio->kern_rel_offset = 0;
10357 ctsio->kern_sg_entries = 0;
10360 * The control device is always connected. The disk device, on the
10361 * other hand, may not be online all the time. Need to change this
10362 * to figure out whether the disk device is actually online or not.
10365 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10366 lun->be_lun->lun_type;
10368 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10370 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
10371 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
10372 bl_ptr->max_cmp_write_len = 0xff;
10373 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
10375 bs = lun->be_lun->blocksize;
10376 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len);
10377 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10378 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
10379 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
10380 if (lun->be_lun->pblockexp != 0) {
10381 scsi_ulto4b((1 << lun->be_lun->pblockexp),
10382 bl_ptr->opt_unmap_grain);
10383 scsi_ulto4b(0x80000000 | lun->be_lun->pblockoff,
10384 bl_ptr->unmap_grain_align);
10387 scsi_ulto4b(lun->be_lun->atomicblock,
10388 bl_ptr->max_atomic_transfer_length);
10389 scsi_ulto4b(0, bl_ptr->atomic_alignment);
10390 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
10392 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
10394 ctsio->scsi_status = SCSI_STATUS_OK;
10395 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10396 ctsio->be_move_done = ctl_config_move_done;
10397 ctl_datamove((union ctl_io *)ctsio);
10399 return (CTL_RETVAL_COMPLETE);
10403 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
10405 struct scsi_vpd_block_device_characteristics *bdc_ptr;
10406 struct ctl_lun *lun;
10410 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10412 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
10413 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
10414 ctsio->kern_sg_entries = 0;
10416 if (sizeof(*bdc_ptr) < alloc_len) {
10417 ctsio->residual = alloc_len - sizeof(*bdc_ptr);
10418 ctsio->kern_data_len = sizeof(*bdc_ptr);
10419 ctsio->kern_total_len = sizeof(*bdc_ptr);
10421 ctsio->residual = 0;
10422 ctsio->kern_data_len = alloc_len;
10423 ctsio->kern_total_len = alloc_len;
10425 ctsio->kern_data_resid = 0;
10426 ctsio->kern_rel_offset = 0;
10427 ctsio->kern_sg_entries = 0;
10430 * The control device is always connected. The disk device, on the
10431 * other hand, may not be online all the time. Need to change this
10432 * to figure out whether the disk device is actually online or not.
10435 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10436 lun->be_lun->lun_type;
10438 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10439 bdc_ptr->page_code = SVPD_BDC;
10440 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
10442 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL)
10443 i = strtol(value, NULL, 0);
10445 i = CTL_DEFAULT_ROTATION_RATE;
10446 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
10448 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL)
10449 i = strtol(value, NULL, 0);
10452 bdc_ptr->wab_wac_ff = (i & 0x0f);
10453 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
10455 ctsio->scsi_status = SCSI_STATUS_OK;
10456 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10457 ctsio->be_move_done = ctl_config_move_done;
10458 ctl_datamove((union ctl_io *)ctsio);
10460 return (CTL_RETVAL_COMPLETE);
10464 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
10466 struct scsi_vpd_logical_block_prov *lbp_ptr;
10467 struct ctl_lun *lun;
10469 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10471 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
10472 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
10473 ctsio->kern_sg_entries = 0;
10475 if (sizeof(*lbp_ptr) < alloc_len) {
10476 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
10477 ctsio->kern_data_len = sizeof(*lbp_ptr);
10478 ctsio->kern_total_len = sizeof(*lbp_ptr);
10480 ctsio->residual = 0;
10481 ctsio->kern_data_len = alloc_len;
10482 ctsio->kern_total_len = alloc_len;
10484 ctsio->kern_data_resid = 0;
10485 ctsio->kern_rel_offset = 0;
10486 ctsio->kern_sg_entries = 0;
10489 * The control device is always connected. The disk device, on the
10490 * other hand, may not be online all the time. Need to change this
10491 * to figure out whether the disk device is actually online or not.
10494 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10495 lun->be_lun->lun_type;
10497 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10499 lbp_ptr->page_code = SVPD_LBP;
10500 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
10501 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10502 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
10503 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
10504 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
10505 lbp_ptr->prov_type = SVPD_LBP_THIN;
10508 ctsio->scsi_status = SCSI_STATUS_OK;
10509 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10510 ctsio->be_move_done = ctl_config_move_done;
10511 ctl_datamove((union ctl_io *)ctsio);
10513 return (CTL_RETVAL_COMPLETE);
10517 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
10519 struct scsi_inquiry *cdb;
10520 int alloc_len, retval;
10522 cdb = (struct scsi_inquiry *)ctsio->cdb;
10524 retval = CTL_RETVAL_COMPLETE;
10526 alloc_len = scsi_2btoul(cdb->length);
10528 switch (cdb->page_code) {
10529 case SVPD_SUPPORTED_PAGES:
10530 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
10532 case SVPD_UNIT_SERIAL_NUMBER:
10533 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
10535 case SVPD_DEVICE_ID:
10536 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
10538 case SVPD_EXTENDED_INQUIRY_DATA:
10539 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
10541 case SVPD_MODE_PAGE_POLICY:
10542 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
10544 case SVPD_SCSI_PORTS:
10545 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10547 case SVPD_SCSI_TPC:
10548 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10550 case SVPD_BLOCK_LIMITS:
10551 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10554 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10557 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10560 ctl_set_invalid_field(ctsio,
10566 ctl_done((union ctl_io *)ctsio);
10567 retval = CTL_RETVAL_COMPLETE;
10575 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10577 struct scsi_inquiry_data *inq_ptr;
10578 struct scsi_inquiry *cdb;
10579 struct ctl_softc *ctl_softc;
10580 struct ctl_lun *lun;
10582 uint32_t alloc_len, data_len;
10583 ctl_port_type port_type;
10585 ctl_softc = control_softc;
10588 * Figure out whether we're talking to a Fibre Channel port or not.
10589 * We treat the ioctl front end, and any SCSI adapters, as packetized
10592 port_type = ctl_softc->ctl_ports[
10593 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type;
10594 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10595 port_type = CTL_PORT_SCSI;
10597 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10598 cdb = (struct scsi_inquiry *)ctsio->cdb;
10599 alloc_len = scsi_2btoul(cdb->length);
10602 * We malloc the full inquiry data size here and fill it
10603 * in. If the user only asks for less, we'll give him
10606 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10607 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10608 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10609 ctsio->kern_sg_entries = 0;
10610 ctsio->kern_data_resid = 0;
10611 ctsio->kern_rel_offset = 0;
10613 if (data_len < alloc_len) {
10614 ctsio->residual = alloc_len - data_len;
10615 ctsio->kern_data_len = data_len;
10616 ctsio->kern_total_len = data_len;
10618 ctsio->residual = 0;
10619 ctsio->kern_data_len = alloc_len;
10620 ctsio->kern_total_len = alloc_len;
10624 * If we have a LUN configured, report it as connected. Otherwise,
10625 * report that it is offline or no device is supported, depending
10626 * on the value of inquiry_pq_no_lun.
10628 * According to the spec (SPC-4 r34), the peripheral qualifier
10629 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario:
10631 * "A peripheral device having the specified peripheral device type
10632 * is not connected to this logical unit. However, the device
10633 * server is capable of supporting the specified peripheral device
10634 * type on this logical unit."
10636 * According to the same spec, the peripheral qualifier
10637 * SID_QUAL_BAD_LU (011b) is used in this scenario:
10639 * "The device server is not capable of supporting a peripheral
10640 * device on this logical unit. For this peripheral qualifier the
10641 * peripheral device type shall be set to 1Fh. All other peripheral
10642 * device type values are reserved for this peripheral qualifier."
10644 * Given the text, it would seem that we probably want to report that
10645 * the LUN is offline here. There is no LUN connected, but we can
10646 * support a LUN at the given LUN number.
10648 * In the real world, though, it sounds like things are a little
10651 * - Linux, when presented with a LUN with the offline peripheral
10652 * qualifier, will create an sg driver instance for it. So when
10653 * you attach it to CTL, you wind up with a ton of sg driver
10654 * instances. (One for every LUN that Linux bothered to probe.)
10655 * Linux does this despite the fact that it issues a REPORT LUNs
10656 * to LUN 0 to get the inventory of supported LUNs.
10658 * - There is other anecdotal evidence (from Emulex folks) about
10659 * arrays that use the offline peripheral qualifier for LUNs that
10660 * are on the "passive" path in an active/passive array.
10662 * So the solution is provide a hopefully reasonable default
10663 * (return bad/no LUN) and allow the user to change the behavior
10664 * with a tunable/sysctl variable.
10667 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10668 lun->be_lun->lun_type;
10669 else if (ctl_softc->inquiry_pq_no_lun == 0)
10670 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10672 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10674 /* RMB in byte 2 is 0 */
10675 inq_ptr->version = SCSI_REV_SPC4;
10678 * According to SAM-3, even if a device only supports a single
10679 * level of LUN addressing, it should still set the HISUP bit:
10681 * 4.9.1 Logical unit numbers overview
10683 * All logical unit number formats described in this standard are
10684 * hierarchical in structure even when only a single level in that
10685 * hierarchy is used. The HISUP bit shall be set to one in the
10686 * standard INQUIRY data (see SPC-2) when any logical unit number
10687 * format described in this standard is used. Non-hierarchical
10688 * formats are outside the scope of this standard.
10690 * Therefore we set the HiSup bit here.
10692 * The reponse format is 2, per SPC-3.
10694 inq_ptr->response_format = SID_HiSup | 2;
10696 inq_ptr->additional_length = data_len -
10697 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10698 CTL_DEBUG_PRINT(("additional_length = %d\n",
10699 inq_ptr->additional_length));
10701 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10702 /* 16 bit addressing */
10703 if (port_type == CTL_PORT_SCSI)
10704 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10705 /* XXX set the SID_MultiP bit here if we're actually going to
10706 respond on multiple ports */
10707 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10709 /* 16 bit data bus, synchronous transfers */
10710 if (port_type == CTL_PORT_SCSI)
10711 inq_ptr->flags = SID_WBus16 | SID_Sync;
10713 * XXX KDM do we want to support tagged queueing on the control
10717 || (lun->be_lun->lun_type != T_PROCESSOR))
10718 inq_ptr->flags |= SID_CmdQue;
10720 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10721 * We have 8 bytes for the vendor name, and 16 bytes for the device
10722 * name and 4 bytes for the revision.
10724 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10725 "vendor")) == NULL) {
10726 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10728 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10729 strncpy(inq_ptr->vendor, val,
10730 min(sizeof(inq_ptr->vendor), strlen(val)));
10733 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10734 sizeof(inq_ptr->product));
10735 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10736 switch (lun->be_lun->lun_type) {
10738 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10739 sizeof(inq_ptr->product));
10742 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10743 sizeof(inq_ptr->product));
10746 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10747 sizeof(inq_ptr->product));
10751 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10752 strncpy(inq_ptr->product, val,
10753 min(sizeof(inq_ptr->product), strlen(val)));
10757 * XXX make this a macro somewhere so it automatically gets
10758 * incremented when we make changes.
10760 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10761 "revision")) == NULL) {
10762 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10764 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10765 strncpy(inq_ptr->revision, val,
10766 min(sizeof(inq_ptr->revision), strlen(val)));
10770 * For parallel SCSI, we support double transition and single
10771 * transition clocking. We also support QAS (Quick Arbitration
10772 * and Selection) and Information Unit transfers on both the
10773 * control and array devices.
10775 if (port_type == CTL_PORT_SCSI)
10776 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10779 /* SAM-5 (no version claimed) */
10780 scsi_ulto2b(0x00A0, inq_ptr->version1);
10781 /* SPC-4 (no version claimed) */
10782 scsi_ulto2b(0x0460, inq_ptr->version2);
10783 if (port_type == CTL_PORT_FC) {
10784 /* FCP-2 ANSI INCITS.350:2003 */
10785 scsi_ulto2b(0x0917, inq_ptr->version3);
10786 } else if (port_type == CTL_PORT_SCSI) {
10787 /* SPI-4 ANSI INCITS.362:200x */
10788 scsi_ulto2b(0x0B56, inq_ptr->version3);
10789 } else if (port_type == CTL_PORT_ISCSI) {
10790 /* iSCSI (no version claimed) */
10791 scsi_ulto2b(0x0960, inq_ptr->version3);
10792 } else if (port_type == CTL_PORT_SAS) {
10793 /* SAS (no version claimed) */
10794 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10798 /* SBC-4 (no version claimed) */
10799 scsi_ulto2b(0x0600, inq_ptr->version4);
10801 switch (lun->be_lun->lun_type) {
10803 /* SBC-4 (no version claimed) */
10804 scsi_ulto2b(0x0600, inq_ptr->version4);
10812 ctsio->scsi_status = SCSI_STATUS_OK;
10813 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10814 ctsio->be_move_done = ctl_config_move_done;
10815 ctl_datamove((union ctl_io *)ctsio);
10816 return (CTL_RETVAL_COMPLETE);
10820 ctl_inquiry(struct ctl_scsiio *ctsio)
10822 struct scsi_inquiry *cdb;
10825 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10827 cdb = (struct scsi_inquiry *)ctsio->cdb;
10828 if (cdb->byte2 & SI_EVPD)
10829 retval = ctl_inquiry_evpd(ctsio);
10830 else if (cdb->page_code == 0)
10831 retval = ctl_inquiry_std(ctsio);
10833 ctl_set_invalid_field(ctsio,
10839 ctl_done((union ctl_io *)ctsio);
10840 return (CTL_RETVAL_COMPLETE);
10847 * For known CDB types, parse the LBA and length.
10850 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10852 if (io->io_hdr.io_type != CTL_IO_SCSI)
10855 switch (io->scsiio.cdb[0]) {
10856 case COMPARE_AND_WRITE: {
10857 struct scsi_compare_and_write *cdb;
10859 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10861 *lba = scsi_8btou64(cdb->addr);
10862 *len = cdb->length;
10867 struct scsi_rw_6 *cdb;
10869 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10871 *lba = scsi_3btoul(cdb->addr);
10872 /* only 5 bits are valid in the most significant address byte */
10874 *len = cdb->length;
10879 struct scsi_rw_10 *cdb;
10881 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10883 *lba = scsi_4btoul(cdb->addr);
10884 *len = scsi_2btoul(cdb->length);
10887 case WRITE_VERIFY_10: {
10888 struct scsi_write_verify_10 *cdb;
10890 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10892 *lba = scsi_4btoul(cdb->addr);
10893 *len = scsi_2btoul(cdb->length);
10898 struct scsi_rw_12 *cdb;
10900 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10902 *lba = scsi_4btoul(cdb->addr);
10903 *len = scsi_4btoul(cdb->length);
10906 case WRITE_VERIFY_12: {
10907 struct scsi_write_verify_12 *cdb;
10909 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10911 *lba = scsi_4btoul(cdb->addr);
10912 *len = scsi_4btoul(cdb->length);
10917 case WRITE_ATOMIC_16: {
10918 struct scsi_rw_16 *cdb;
10920 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10922 *lba = scsi_8btou64(cdb->addr);
10923 *len = scsi_4btoul(cdb->length);
10926 case WRITE_VERIFY_16: {
10927 struct scsi_write_verify_16 *cdb;
10929 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10931 *lba = scsi_8btou64(cdb->addr);
10932 *len = scsi_4btoul(cdb->length);
10935 case WRITE_SAME_10: {
10936 struct scsi_write_same_10 *cdb;
10938 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10940 *lba = scsi_4btoul(cdb->addr);
10941 *len = scsi_2btoul(cdb->length);
10944 case WRITE_SAME_16: {
10945 struct scsi_write_same_16 *cdb;
10947 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10949 *lba = scsi_8btou64(cdb->addr);
10950 *len = scsi_4btoul(cdb->length);
10954 struct scsi_verify_10 *cdb;
10956 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10958 *lba = scsi_4btoul(cdb->addr);
10959 *len = scsi_2btoul(cdb->length);
10963 struct scsi_verify_12 *cdb;
10965 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10967 *lba = scsi_4btoul(cdb->addr);
10968 *len = scsi_4btoul(cdb->length);
10972 struct scsi_verify_16 *cdb;
10974 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10976 *lba = scsi_8btou64(cdb->addr);
10977 *len = scsi_4btoul(cdb->length);
10987 break; /* NOTREACHED */
10994 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2)
10996 uint64_t endlba1, endlba2;
10998 endlba1 = lba1 + len1 - 1;
10999 endlba2 = lba2 + len2 - 1;
11001 if ((endlba1 < lba2)
11002 || (endlba2 < lba1))
11003 return (CTL_ACTION_PASS);
11005 return (CTL_ACTION_BLOCK);
11009 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
11011 struct ctl_ptr_len_flags *ptrlen;
11012 struct scsi_unmap_desc *buf, *end, *range;
11016 /* If not UNMAP -- go other way. */
11017 if (io->io_hdr.io_type != CTL_IO_SCSI ||
11018 io->scsiio.cdb[0] != UNMAP)
11019 return (CTL_ACTION_ERROR);
11021 /* If UNMAP without data -- block and wait for data. */
11022 ptrlen = (struct ctl_ptr_len_flags *)
11023 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
11024 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
11025 ptrlen->ptr == NULL)
11026 return (CTL_ACTION_BLOCK);
11028 /* UNMAP with data -- check for collision. */
11029 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
11030 end = buf + ptrlen->len / sizeof(*buf);
11031 for (range = buf; range < end; range++) {
11032 lba = scsi_8btou64(range->lba);
11033 len = scsi_4btoul(range->length);
11034 if ((lba < lba2 + len2) && (lba + len > lba2))
11035 return (CTL_ACTION_BLOCK);
11037 return (CTL_ACTION_PASS);
11041 ctl_extent_check(union ctl_io *io1, union ctl_io *io2)
11043 uint64_t lba1, lba2;
11044 uint64_t len1, len2;
11047 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
11048 return (CTL_ACTION_ERROR);
11050 retval = ctl_extent_check_unmap(io2, lba1, len1);
11051 if (retval != CTL_ACTION_ERROR)
11054 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
11055 return (CTL_ACTION_ERROR);
11057 return (ctl_extent_check_lba(lba1, len1, lba2, len2));
11061 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
11062 union ctl_io *ooa_io)
11064 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
11065 ctl_serialize_action *serialize_row;
11068 * The initiator attempted multiple untagged commands at the same
11069 * time. Can't do that.
11071 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11072 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11073 && ((pending_io->io_hdr.nexus.targ_port ==
11074 ooa_io->io_hdr.nexus.targ_port)
11075 && (pending_io->io_hdr.nexus.initid.id ==
11076 ooa_io->io_hdr.nexus.initid.id))
11077 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
11078 return (CTL_ACTION_OVERLAP);
11081 * The initiator attempted to send multiple tagged commands with
11082 * the same ID. (It's fine if different initiators have the same
11085 * Even if all of those conditions are true, we don't kill the I/O
11086 * if the command ahead of us has been aborted. We won't end up
11087 * sending it to the FETD, and it's perfectly legal to resend a
11088 * command with the same tag number as long as the previous
11089 * instance of this tag number has been aborted somehow.
11091 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
11092 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
11093 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
11094 && ((pending_io->io_hdr.nexus.targ_port ==
11095 ooa_io->io_hdr.nexus.targ_port)
11096 && (pending_io->io_hdr.nexus.initid.id ==
11097 ooa_io->io_hdr.nexus.initid.id))
11098 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
11099 return (CTL_ACTION_OVERLAP_TAG);
11102 * If we get a head of queue tag, SAM-3 says that we should
11103 * immediately execute it.
11105 * What happens if this command would normally block for some other
11106 * reason? e.g. a request sense with a head of queue tag
11107 * immediately after a write. Normally that would block, but this
11108 * will result in its getting executed immediately...
11110 * We currently return "pass" instead of "skip", so we'll end up
11111 * going through the rest of the queue to check for overlapped tags.
11113 * XXX KDM check for other types of blockage first??
11115 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
11116 return (CTL_ACTION_PASS);
11119 * Ordered tags have to block until all items ahead of them
11120 * have completed. If we get called with an ordered tag, we always
11121 * block, if something else is ahead of us in the queue.
11123 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
11124 return (CTL_ACTION_BLOCK);
11127 * Simple tags get blocked until all head of queue and ordered tags
11128 * ahead of them have completed. I'm lumping untagged commands in
11129 * with simple tags here. XXX KDM is that the right thing to do?
11131 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11132 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
11133 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
11134 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
11135 return (CTL_ACTION_BLOCK);
11137 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
11138 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
11140 serialize_row = ctl_serialize_table[ooa_entry->seridx];
11142 switch (serialize_row[pending_entry->seridx]) {
11143 case CTL_SER_BLOCK:
11144 return (CTL_ACTION_BLOCK);
11145 case CTL_SER_EXTENT:
11146 return (ctl_extent_check(pending_io, ooa_io));
11147 case CTL_SER_EXTENTOPT:
11148 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
11149 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
11150 return (ctl_extent_check(pending_io, ooa_io));
11153 return (CTL_ACTION_PASS);
11154 case CTL_SER_BLOCKOPT:
11155 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
11156 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
11157 return (CTL_ACTION_BLOCK);
11158 return (CTL_ACTION_PASS);
11160 return (CTL_ACTION_SKIP);
11162 panic("invalid serialization value %d",
11163 serialize_row[pending_entry->seridx]);
11166 return (CTL_ACTION_ERROR);
11170 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
11172 * - pending_io is generally either incoming, or on the blocked queue
11173 * - starting I/O is the I/O we want to start the check with.
11176 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
11177 union ctl_io *starting_io)
11179 union ctl_io *ooa_io;
11182 mtx_assert(&lun->lun_lock, MA_OWNED);
11185 * Run back along the OOA queue, starting with the current
11186 * blocked I/O and going through every I/O before it on the
11187 * queue. If starting_io is NULL, we'll just end up returning
11190 for (ooa_io = starting_io; ooa_io != NULL;
11191 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
11195 * This routine just checks to see whether
11196 * cur_blocked is blocked by ooa_io, which is ahead
11197 * of it in the queue. It doesn't queue/dequeue
11200 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
11202 case CTL_ACTION_BLOCK:
11203 case CTL_ACTION_OVERLAP:
11204 case CTL_ACTION_OVERLAP_TAG:
11205 case CTL_ACTION_SKIP:
11206 case CTL_ACTION_ERROR:
11208 break; /* NOTREACHED */
11209 case CTL_ACTION_PASS:
11212 panic("invalid action %d", action);
11213 break; /* NOTREACHED */
11217 return (CTL_ACTION_PASS);
11222 * - An I/O has just completed, and has been removed from the per-LUN OOA
11223 * queue, so some items on the blocked queue may now be unblocked.
11226 ctl_check_blocked(struct ctl_lun *lun)
11228 union ctl_io *cur_blocked, *next_blocked;
11230 mtx_assert(&lun->lun_lock, MA_OWNED);
11233 * Run forward from the head of the blocked queue, checking each
11234 * entry against the I/Os prior to it on the OOA queue to see if
11235 * there is still any blockage.
11237 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
11238 * with our removing a variable on it while it is traversing the
11241 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
11242 cur_blocked != NULL; cur_blocked = next_blocked) {
11243 union ctl_io *prev_ooa;
11246 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
11249 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
11250 ctl_ooaq, ooa_links);
11253 * If cur_blocked happens to be the first item in the OOA
11254 * queue now, prev_ooa will be NULL, and the action
11255 * returned will just be CTL_ACTION_PASS.
11257 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
11260 case CTL_ACTION_BLOCK:
11261 /* Nothing to do here, still blocked */
11263 case CTL_ACTION_OVERLAP:
11264 case CTL_ACTION_OVERLAP_TAG:
11266 * This shouldn't happen! In theory we've already
11267 * checked this command for overlap...
11270 case CTL_ACTION_PASS:
11271 case CTL_ACTION_SKIP: {
11272 struct ctl_softc *softc;
11273 const struct ctl_cmd_entry *entry;
11278 * The skip case shouldn't happen, this transaction
11279 * should have never made it onto the blocked queue.
11282 * This I/O is no longer blocked, we can remove it
11283 * from the blocked queue. Since this is a TAILQ
11284 * (doubly linked list), we can do O(1) removals
11285 * from any place on the list.
11287 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
11289 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11291 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){
11293 * Need to send IO back to original side to
11296 union ctl_ha_msg msg_info;
11298 msg_info.hdr.original_sc =
11299 cur_blocked->io_hdr.original_sc;
11300 msg_info.hdr.serializing_sc = cur_blocked;
11301 msg_info.hdr.msg_type = CTL_MSG_R2R;
11302 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11303 &msg_info, sizeof(msg_info), 0)) >
11304 CTL_HA_STATUS_SUCCESS) {
11305 printf("CTL:Check Blocked error from "
11306 "ctl_ha_msg_send %d\n",
11311 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
11312 softc = control_softc;
11314 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus);
11317 * Check this I/O for LUN state changes that may
11318 * have happened while this command was blocked.
11319 * The LUN state may have been changed by a command
11320 * ahead of us in the queue, so we need to re-check
11321 * for any states that can be caused by SCSI
11324 if (ctl_scsiio_lun_check(softc, lun, entry,
11325 &cur_blocked->scsiio) == 0) {
11326 cur_blocked->io_hdr.flags |=
11327 CTL_FLAG_IS_WAS_ON_RTR;
11328 ctl_enqueue_rtr(cur_blocked);
11330 ctl_done(cur_blocked);
11335 * This probably shouldn't happen -- we shouldn't
11336 * get CTL_ACTION_ERROR, or anything else.
11342 return (CTL_RETVAL_COMPLETE);
11346 * This routine (with one exception) checks LUN flags that can be set by
11347 * commands ahead of us in the OOA queue. These flags have to be checked
11348 * when a command initially comes in, and when we pull a command off the
11349 * blocked queue and are preparing to execute it. The reason we have to
11350 * check these flags for commands on the blocked queue is that the LUN
11351 * state may have been changed by a command ahead of us while we're on the
11354 * Ordering is somewhat important with these checks, so please pay
11355 * careful attention to the placement of any new checks.
11358 ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
11359 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11366 mtx_assert(&lun->lun_lock, MA_OWNED);
11369 * If this shelf is a secondary shelf controller, we have to reject
11370 * any media access commands.
11372 if ((ctl_softc->flags & CTL_FLAG_ACTIVE_SHELF) == 0 &&
11373 (entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0) {
11374 ctl_set_lun_standby(ctsio);
11379 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11380 if (lun->flags & CTL_LUN_READONLY) {
11381 ctl_set_sense(ctsio, /*current_error*/ 1,
11382 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11383 /*asc*/ 0x27, /*ascq*/ 0x01, SSD_ELEM_NONE);
11387 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT]
11388 .eca_and_aen & SCP_SWP) != 0) {
11389 ctl_set_sense(ctsio, /*current_error*/ 1,
11390 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11391 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11398 * Check for a reservation conflict. If this command isn't allowed
11399 * even on reserved LUNs, and if this initiator isn't the one who
11400 * reserved us, reject the command with a reservation conflict.
11402 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
11403 if ((lun->flags & CTL_LUN_RESERVED)
11404 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11405 if (lun->res_idx != residx) {
11406 ctl_set_reservation_conflict(ctsio);
11412 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11413 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11414 /* No reservation or command is allowed. */;
11415 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11416 (lun->res_type == SPR_TYPE_WR_EX ||
11417 lun->res_type == SPR_TYPE_WR_EX_RO ||
11418 lun->res_type == SPR_TYPE_WR_EX_AR)) {
11419 /* The command is allowed for Write Exclusive resv. */;
11422 * if we aren't registered or it's a res holder type
11423 * reservation and this isn't the res holder then set a
11426 if (lun->pr_keys[residx] == 0
11427 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
11428 ctl_set_reservation_conflict(ctsio);
11435 if ((lun->flags & CTL_LUN_OFFLINE)
11436 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) {
11437 ctl_set_lun_not_ready(ctsio);
11443 * If the LUN is stopped, see if this particular command is allowed
11444 * for a stopped lun. Otherwise, reject it with 0x04,0x02.
11446 if ((lun->flags & CTL_LUN_STOPPED)
11447 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
11448 /* "Logical unit not ready, initializing cmd. required" */
11449 ctl_set_lun_stopped(ctsio);
11454 if ((lun->flags & CTL_LUN_INOPERABLE)
11455 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
11456 /* "Medium format corrupted" */
11457 ctl_set_medium_format_corrupted(ctsio);
11468 ctl_failover_io(union ctl_io *io, int have_lock)
11470 ctl_set_busy(&io->scsiio);
11477 struct ctl_lun *lun;
11478 struct ctl_softc *ctl_softc;
11479 union ctl_io *next_io, *pending_io;
11484 ctl_softc = control_softc;
11486 mtx_lock(&ctl_softc->ctl_lock);
11488 * Remove any cmds from the other SC from the rtr queue. These
11489 * will obviously only be for LUNs for which we're the primary.
11490 * We can't send status or get/send data for these commands.
11491 * Since they haven't been executed yet, we can just remove them.
11492 * We'll either abort them or delete them below, depending on
11493 * which HA mode we're in.
11496 mtx_lock(&ctl_softc->queue_lock);
11497 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue);
11498 io != NULL; io = next_io) {
11499 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
11500 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
11501 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr,
11502 ctl_io_hdr, links);
11504 mtx_unlock(&ctl_softc->queue_lock);
11507 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) {
11508 lun = ctl_softc->ctl_luns[lun_idx];
11513 * Processor LUNs are primary on both sides.
11514 * XXX will this always be true?
11516 if (lun->be_lun->lun_type == T_PROCESSOR)
11519 if ((lun->flags & CTL_LUN_PRIMARY_SC)
11520 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
11521 printf("FAILOVER: primary lun %d\n", lun_idx);
11523 * Remove all commands from the other SC. First from the
11524 * blocked queue then from the ooa queue. Once we have
11525 * removed them. Call ctl_check_blocked to see if there
11526 * is anything that can run.
11528 for (io = (union ctl_io *)TAILQ_FIRST(
11529 &lun->blocked_queue); io != NULL; io = next_io) {
11531 next_io = (union ctl_io *)TAILQ_NEXT(
11532 &io->io_hdr, blocked_links);
11534 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
11535 TAILQ_REMOVE(&lun->blocked_queue,
11536 &io->io_hdr,blocked_links);
11537 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11538 TAILQ_REMOVE(&lun->ooa_queue,
11539 &io->io_hdr, ooa_links);
11545 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
11546 io != NULL; io = next_io) {
11548 next_io = (union ctl_io *)TAILQ_NEXT(
11549 &io->io_hdr, ooa_links);
11551 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
11553 TAILQ_REMOVE(&lun->ooa_queue,
11560 ctl_check_blocked(lun);
11561 } else if ((lun->flags & CTL_LUN_PRIMARY_SC)
11562 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
11564 printf("FAILOVER: primary lun %d\n", lun_idx);
11566 * Abort all commands from the other SC. We can't
11567 * send status back for them now. These should get
11568 * cleaned up when they are completed or come out
11569 * for a datamove operation.
11571 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
11572 io != NULL; io = next_io) {
11573 next_io = (union ctl_io *)TAILQ_NEXT(
11574 &io->io_hdr, ooa_links);
11576 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
11577 io->io_hdr.flags |= CTL_FLAG_ABORT;
11579 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
11580 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
11582 printf("FAILOVER: secondary lun %d\n", lun_idx);
11584 lun->flags |= CTL_LUN_PRIMARY_SC;
11587 * We send all I/O that was sent to this controller
11588 * and redirected to the other side back with
11589 * busy status, and have the initiator retry it.
11590 * Figuring out how much data has been transferred,
11591 * etc. and picking up where we left off would be
11594 * XXX KDM need to remove I/O from the blocked
11597 for (pending_io = (union ctl_io *)TAILQ_FIRST(
11598 &lun->ooa_queue); pending_io != NULL;
11599 pending_io = next_io) {
11601 next_io = (union ctl_io *)TAILQ_NEXT(
11602 &pending_io->io_hdr, ooa_links);
11604 pending_io->io_hdr.flags &=
11605 ~CTL_FLAG_SENT_2OTHER_SC;
11607 if (pending_io->io_hdr.flags &
11608 CTL_FLAG_IO_ACTIVE) {
11609 pending_io->io_hdr.flags |=
11612 ctl_set_busy(&pending_io->scsiio);
11613 ctl_done(pending_io);
11618 * Build Unit Attention
11620 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11621 lun->pending_ua[i] |=
11622 CTL_UA_ASYM_ACC_CHANGE;
11624 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
11625 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
11626 printf("FAILOVER: secondary lun %d\n", lun_idx);
11628 * if the first io on the OOA is not on the RtR queue
11631 lun->flags |= CTL_LUN_PRIMARY_SC;
11633 pending_io = (union ctl_io *)TAILQ_FIRST(
11635 if (pending_io==NULL) {
11636 printf("Nothing on OOA queue\n");
11640 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11641 if ((pending_io->io_hdr.flags &
11642 CTL_FLAG_IS_WAS_ON_RTR) == 0) {
11643 pending_io->io_hdr.flags |=
11644 CTL_FLAG_IS_WAS_ON_RTR;
11645 ctl_enqueue_rtr(pending_io);
11650 printf("Tag 0x%04x is running\n",
11651 pending_io->scsiio.tag_num);
11655 next_io = (union ctl_io *)TAILQ_NEXT(
11656 &pending_io->io_hdr, ooa_links);
11657 for (pending_io=next_io; pending_io != NULL;
11658 pending_io = next_io) {
11659 pending_io->io_hdr.flags &=
11660 ~CTL_FLAG_SENT_2OTHER_SC;
11661 next_io = (union ctl_io *)TAILQ_NEXT(
11662 &pending_io->io_hdr, ooa_links);
11663 if (pending_io->io_hdr.flags &
11664 CTL_FLAG_IS_WAS_ON_RTR) {
11666 printf("Tag 0x%04x is running\n",
11667 pending_io->scsiio.tag_num);
11672 switch (ctl_check_ooa(lun, pending_io,
11673 (union ctl_io *)TAILQ_PREV(
11674 &pending_io->io_hdr, ctl_ooaq,
11677 case CTL_ACTION_BLOCK:
11678 TAILQ_INSERT_TAIL(&lun->blocked_queue,
11679 &pending_io->io_hdr,
11681 pending_io->io_hdr.flags |=
11684 case CTL_ACTION_PASS:
11685 case CTL_ACTION_SKIP:
11686 pending_io->io_hdr.flags |=
11687 CTL_FLAG_IS_WAS_ON_RTR;
11688 ctl_enqueue_rtr(pending_io);
11690 case CTL_ACTION_OVERLAP:
11691 ctl_set_overlapped_cmd(
11692 (struct ctl_scsiio *)pending_io);
11693 ctl_done(pending_io);
11695 case CTL_ACTION_OVERLAP_TAG:
11696 ctl_set_overlapped_tag(
11697 (struct ctl_scsiio *)pending_io,
11698 pending_io->scsiio.tag_num & 0xff);
11699 ctl_done(pending_io);
11701 case CTL_ACTION_ERROR:
11703 ctl_set_internal_failure(
11704 (struct ctl_scsiio *)pending_io,
11707 ctl_done(pending_io);
11713 * Build Unit Attention
11715 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11716 lun->pending_ua[i] |=
11717 CTL_UA_ASYM_ACC_CHANGE;
11720 panic("Unhandled HA mode failover, LUN flags = %#x, "
11721 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode);
11725 mtx_unlock(&ctl_softc->ctl_lock);
11729 ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio)
11731 struct ctl_lun *lun;
11732 const struct ctl_cmd_entry *entry;
11733 uint32_t initidx, targ_lun;
11740 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11741 if ((targ_lun < CTL_MAX_LUNS)
11742 && ((lun = ctl_softc->ctl_luns[targ_lun]) != NULL)) {
11744 * If the LUN is invalid, pretend that it doesn't exist.
11745 * It will go away as soon as all pending I/O has been
11748 mtx_lock(&lun->lun_lock);
11749 if (lun->flags & CTL_LUN_DISABLED) {
11750 mtx_unlock(&lun->lun_lock);
11752 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11753 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11755 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
11756 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
11758 if (lun->be_lun->lun_type == T_PROCESSOR) {
11759 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV;
11763 * Every I/O goes into the OOA queue for a
11764 * particular LUN, and stays there until completion.
11766 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr,
11770 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11771 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11774 /* Get command entry and return error if it is unsuppotyed. */
11775 entry = ctl_validate_command(ctsio);
11776 if (entry == NULL) {
11778 mtx_unlock(&lun->lun_lock);
11782 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11783 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11786 * Check to see whether we can send this command to LUNs that don't
11787 * exist. This should pretty much only be the case for inquiry
11788 * and request sense. Further checks, below, really require having
11789 * a LUN, so we can't really check the command anymore. Just put
11790 * it on the rtr queue.
11793 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) {
11794 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11795 ctl_enqueue_rtr((union ctl_io *)ctsio);
11799 ctl_set_unsupported_lun(ctsio);
11800 ctl_done((union ctl_io *)ctsio);
11801 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11805 * Make sure we support this particular command on this LUN.
11806 * e.g., we don't support writes to the control LUN.
11808 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11809 mtx_unlock(&lun->lun_lock);
11810 ctl_set_invalid_opcode(ctsio);
11811 ctl_done((union ctl_io *)ctsio);
11816 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11820 * If we've got a request sense, it'll clear the contingent
11821 * allegiance condition. Otherwise, if we have a CA condition for
11822 * this initiator, clear it, because it sent down a command other
11823 * than request sense.
11825 if ((ctsio->cdb[0] != REQUEST_SENSE)
11826 && (ctl_is_set(lun->have_ca, initidx)))
11827 ctl_clear_mask(lun->have_ca, initidx);
11831 * If the command has this flag set, it handles its own unit
11832 * attention reporting, we shouldn't do anything. Otherwise we
11833 * check for any pending unit attentions, and send them back to the
11834 * initiator. We only do this when a command initially comes in,
11835 * not when we pull it off the blocked queue.
11837 * According to SAM-3, section 5.3.2, the order that things get
11838 * presented back to the host is basically unit attentions caused
11839 * by some sort of reset event, busy status, reservation conflicts
11840 * or task set full, and finally any other status.
11842 * One issue here is that some of the unit attentions we report
11843 * don't fall into the "reset" category (e.g. "reported luns data
11844 * has changed"). So reporting it here, before the reservation
11845 * check, may be technically wrong. I guess the only thing to do
11846 * would be to check for and report the reset events here, and then
11847 * check for the other unit attention types after we check for a
11848 * reservation conflict.
11850 * XXX KDM need to fix this
11852 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11853 ctl_ua_type ua_type;
11855 if (lun->pending_ua[initidx] != CTL_UA_NONE) {
11856 scsi_sense_data_type sense_format;
11859 sense_format = (lun->flags &
11860 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC :
11863 sense_format = SSD_TYPE_FIXED;
11865 ua_type = ctl_build_ua(&lun->pending_ua[initidx],
11866 &ctsio->sense_data, sense_format);
11867 if (ua_type != CTL_UA_NONE) {
11868 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11869 ctsio->io_hdr.status = CTL_SCSI_ERROR |
11871 ctsio->sense_len = SSD_FULL_SIZE;
11872 mtx_unlock(&lun->lun_lock);
11873 ctl_done((union ctl_io *)ctsio);
11880 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) {
11881 mtx_unlock(&lun->lun_lock);
11882 ctl_done((union ctl_io *)ctsio);
11887 * XXX CHD this is where we want to send IO to other side if
11888 * this LUN is secondary on this SC. We will need to make a copy
11889 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11890 * the copy we send as FROM_OTHER.
11891 * We also need to stuff the address of the original IO so we can
11892 * find it easily. Something similar will need be done on the other
11893 * side so when we are done we can find the copy.
11895 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11896 union ctl_ha_msg msg_info;
11899 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11901 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11902 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11904 printf("1. ctsio %p\n", ctsio);
11906 msg_info.hdr.serializing_sc = NULL;
11907 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11908 msg_info.scsi.tag_num = ctsio->tag_num;
11909 msg_info.scsi.tag_type = ctsio->tag_type;
11910 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11912 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11914 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11915 (void *)&msg_info, sizeof(msg_info), 0)) >
11916 CTL_HA_STATUS_SUCCESS) {
11917 printf("CTL:precheck, ctl_ha_msg_send returned %d\n",
11919 printf("CTL:opcode is %x\n", ctsio->cdb[0]);
11922 printf("CTL:Precheck sent msg, opcode is %x\n",opcode);
11927 * XXX KDM this I/O is off the incoming queue, but hasn't
11928 * been inserted on any other queue. We may need to come
11929 * up with a holding queue while we wait for serialization
11930 * so that we have an idea of what we're waiting for from
11933 mtx_unlock(&lun->lun_lock);
11937 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11938 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11939 ctl_ooaq, ooa_links))) {
11940 case CTL_ACTION_BLOCK:
11941 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11942 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11944 mtx_unlock(&lun->lun_lock);
11946 case CTL_ACTION_PASS:
11947 case CTL_ACTION_SKIP:
11948 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11949 mtx_unlock(&lun->lun_lock);
11950 ctl_enqueue_rtr((union ctl_io *)ctsio);
11952 case CTL_ACTION_OVERLAP:
11953 mtx_unlock(&lun->lun_lock);
11954 ctl_set_overlapped_cmd(ctsio);
11955 ctl_done((union ctl_io *)ctsio);
11957 case CTL_ACTION_OVERLAP_TAG:
11958 mtx_unlock(&lun->lun_lock);
11959 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11960 ctl_done((union ctl_io *)ctsio);
11962 case CTL_ACTION_ERROR:
11964 mtx_unlock(&lun->lun_lock);
11965 ctl_set_internal_failure(ctsio,
11967 /*retry_count*/ 0);
11968 ctl_done((union ctl_io *)ctsio);
11974 const struct ctl_cmd_entry *
11975 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11977 const struct ctl_cmd_entry *entry;
11978 int service_action;
11980 entry = &ctl_cmd_table[ctsio->cdb[0]];
11982 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11983 if (entry->flags & CTL_CMD_FLAG_SA5) {
11984 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11985 entry = &((const struct ctl_cmd_entry *)
11986 entry->execute)[service_action];
11991 const struct ctl_cmd_entry *
11992 ctl_validate_command(struct ctl_scsiio *ctsio)
11994 const struct ctl_cmd_entry *entry;
11998 entry = ctl_get_cmd_entry(ctsio, &sa);
11999 if (entry->execute == NULL) {
12001 ctl_set_invalid_field(ctsio,
12008 ctl_set_invalid_opcode(ctsio);
12009 ctl_done((union ctl_io *)ctsio);
12012 KASSERT(entry->length > 0,
12013 ("Not defined length for command 0x%02x/0x%02x",
12014 ctsio->cdb[0], ctsio->cdb[1]));
12015 for (i = 1; i < entry->length; i++) {
12016 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
12019 ctl_set_invalid_field(ctsio,
12024 /*bit*/ fls(diff) - 1);
12025 ctl_done((union ctl_io *)ctsio);
12032 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
12035 switch (lun_type) {
12037 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) &&
12038 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
12042 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) &&
12043 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
12053 ctl_scsiio(struct ctl_scsiio *ctsio)
12056 const struct ctl_cmd_entry *entry;
12058 retval = CTL_RETVAL_COMPLETE;
12060 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
12062 entry = ctl_get_cmd_entry(ctsio, NULL);
12065 * If this I/O has been aborted, just send it straight to
12066 * ctl_done() without executing it.
12068 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
12069 ctl_done((union ctl_io *)ctsio);
12074 * All the checks should have been handled by ctl_scsiio_precheck().
12075 * We should be clear now to just execute the I/O.
12077 retval = entry->execute(ctsio);
12084 * Since we only implement one target right now, a bus reset simply resets
12085 * our single target.
12088 ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io)
12090 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET));
12094 ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
12095 ctl_ua_type ua_type)
12097 struct ctl_lun *lun;
12100 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12101 union ctl_ha_msg msg_info;
12103 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
12104 msg_info.hdr.nexus = io->io_hdr.nexus;
12105 if (ua_type==CTL_UA_TARG_RESET)
12106 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
12108 msg_info.task.task_action = CTL_TASK_BUS_RESET;
12109 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12110 msg_info.hdr.original_sc = NULL;
12111 msg_info.hdr.serializing_sc = NULL;
12112 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12113 (void *)&msg_info, sizeof(msg_info), 0)) {
12118 mtx_lock(&ctl_softc->ctl_lock);
12119 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links)
12120 retval += ctl_lun_reset(lun, io, ua_type);
12121 mtx_unlock(&ctl_softc->ctl_lock);
12127 * The LUN should always be set. The I/O is optional, and is used to
12128 * distinguish between I/Os sent by this initiator, and by other
12129 * initiators. We set unit attention for initiators other than this one.
12130 * SAM-3 is vague on this point. It does say that a unit attention should
12131 * be established for other initiators when a LUN is reset (see section
12132 * 5.7.3), but it doesn't specifically say that the unit attention should
12133 * be established for this particular initiator when a LUN is reset. Here
12134 * is the relevant text, from SAM-3 rev 8:
12136 * 5.7.2 When a SCSI initiator port aborts its own tasks
12138 * When a SCSI initiator port causes its own task(s) to be aborted, no
12139 * notification that the task(s) have been aborted shall be returned to
12140 * the SCSI initiator port other than the completion response for the
12141 * command or task management function action that caused the task(s) to
12142 * be aborted and notification(s) associated with related effects of the
12143 * action (e.g., a reset unit attention condition).
12145 * XXX KDM for now, we're setting unit attention for all initiators.
12148 ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
12152 uint32_t initindex;
12156 mtx_lock(&lun->lun_lock);
12158 * Run through the OOA queue and abort each I/O.
12161 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
12163 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12164 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12165 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
12169 * This version sets unit attention for every
12172 initindex = ctl_get_initindex(&io->io_hdr.nexus);
12173 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
12174 if (initindex == i)
12176 lun->pending_ua[i] |= ua_type;
12181 * A reset (any kind, really) clears reservations established with
12182 * RESERVE/RELEASE. It does not clear reservations established
12183 * with PERSISTENT RESERVE OUT, but we don't support that at the
12184 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
12185 * reservations made with the RESERVE/RELEASE commands, because
12186 * those commands are obsolete in SPC-3.
12188 lun->flags &= ~CTL_LUN_RESERVED;
12190 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
12192 ctl_clear_mask(lun->have_ca, i);
12194 lun->pending_ua[i] |= ua_type;
12196 mtx_unlock(&lun->lun_lock);
12202 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
12207 mtx_assert(&lun->lun_lock, MA_OWNED);
12210 * Run through the OOA queue and attempt to find the given I/O.
12211 * The target port, initiator ID, tag type and tag number have to
12212 * match the values that we got from the initiator. If we have an
12213 * untagged command to abort, simply abort the first untagged command
12214 * we come to. We only allow one untagged command at a time of course.
12216 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12217 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12219 if ((targ_port == UINT32_MAX ||
12220 targ_port == xio->io_hdr.nexus.targ_port) &&
12221 (init_id == UINT32_MAX ||
12222 init_id == xio->io_hdr.nexus.initid.id)) {
12223 if (targ_port != xio->io_hdr.nexus.targ_port ||
12224 init_id != xio->io_hdr.nexus.initid.id)
12225 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
12226 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12227 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12228 union ctl_ha_msg msg_info;
12230 msg_info.hdr.nexus = xio->io_hdr.nexus;
12231 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12232 msg_info.task.tag_num = xio->scsiio.tag_num;
12233 msg_info.task.tag_type = xio->scsiio.tag_type;
12234 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12235 msg_info.hdr.original_sc = NULL;
12236 msg_info.hdr.serializing_sc = NULL;
12237 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12238 (void *)&msg_info, sizeof(msg_info), 0);
12245 ctl_abort_task_set(union ctl_io *io)
12247 struct ctl_softc *softc = control_softc;
12248 struct ctl_lun *lun;
12254 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12255 mtx_lock(&softc->ctl_lock);
12256 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL))
12257 lun = softc->ctl_luns[targ_lun];
12259 mtx_unlock(&softc->ctl_lock);
12263 mtx_lock(&lun->lun_lock);
12264 mtx_unlock(&softc->ctl_lock);
12265 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
12266 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12267 io->io_hdr.nexus.initid.id,
12268 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12269 } else { /* CTL_TASK_CLEAR_TASK_SET */
12270 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
12271 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12273 mtx_unlock(&lun->lun_lock);
12278 ctl_i_t_nexus_reset(union ctl_io *io)
12280 struct ctl_softc *softc = control_softc;
12281 struct ctl_lun *lun;
12282 uint32_t initindex, residx;
12284 initindex = ctl_get_initindex(&io->io_hdr.nexus);
12285 residx = ctl_get_resindex(&io->io_hdr.nexus);
12286 mtx_lock(&softc->ctl_lock);
12287 STAILQ_FOREACH(lun, &softc->lun_list, links) {
12288 mtx_lock(&lun->lun_lock);
12289 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12290 io->io_hdr.nexus.initid.id,
12291 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12293 ctl_clear_mask(lun->have_ca, initindex);
12295 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
12296 lun->flags &= ~CTL_LUN_RESERVED;
12297 lun->pending_ua[initindex] |= CTL_UA_I_T_NEXUS_LOSS;
12298 mtx_unlock(&lun->lun_lock);
12300 mtx_unlock(&softc->ctl_lock);
12305 ctl_abort_task(union ctl_io *io)
12308 struct ctl_lun *lun;
12309 struct ctl_softc *ctl_softc;
12312 char printbuf[128];
12317 ctl_softc = control_softc;
12323 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12324 mtx_lock(&ctl_softc->ctl_lock);
12325 if ((targ_lun < CTL_MAX_LUNS)
12326 && (ctl_softc->ctl_luns[targ_lun] != NULL))
12327 lun = ctl_softc->ctl_luns[targ_lun];
12329 mtx_unlock(&ctl_softc->ctl_lock);
12334 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
12335 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
12338 mtx_lock(&lun->lun_lock);
12339 mtx_unlock(&ctl_softc->ctl_lock);
12341 * Run through the OOA queue and attempt to find the given I/O.
12342 * The target port, initiator ID, tag type and tag number have to
12343 * match the values that we got from the initiator. If we have an
12344 * untagged command to abort, simply abort the first untagged command
12345 * we come to. We only allow one untagged command at a time of course.
12348 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
12350 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12351 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12353 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
12355 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
12356 lun->lun, xio->scsiio.tag_num,
12357 xio->scsiio.tag_type,
12358 (xio->io_hdr.blocked_links.tqe_prev
12359 == NULL) ? "" : " BLOCKED",
12360 (xio->io_hdr.flags &
12361 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
12362 (xio->io_hdr.flags &
12363 CTL_FLAG_ABORT) ? " ABORT" : "",
12364 (xio->io_hdr.flags &
12365 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
12366 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
12368 printf("%s\n", sbuf_data(&sb));
12371 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port)
12372 && (xio->io_hdr.nexus.initid.id ==
12373 io->io_hdr.nexus.initid.id)) {
12375 * If the abort says that the task is untagged, the
12376 * task in the queue must be untagged. Otherwise,
12377 * we just check to see whether the tag numbers
12378 * match. This is because the QLogic firmware
12379 * doesn't pass back the tag type in an abort
12383 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12384 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12385 || (xio->scsiio.tag_num == io->taskio.tag_num)) {
12388 * XXX KDM we've got problems with FC, because it
12389 * doesn't send down a tag type with aborts. So we
12390 * can only really go by the tag number...
12391 * This may cause problems with parallel SCSI.
12392 * Need to figure that out!!
12394 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12395 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12397 if ((io->io_hdr.flags &
12398 CTL_FLAG_FROM_OTHER_SC) == 0 &&
12399 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12400 union ctl_ha_msg msg_info;
12402 io->io_hdr.flags |=
12403 CTL_FLAG_SENT_2OTHER_SC;
12404 msg_info.hdr.nexus = io->io_hdr.nexus;
12405 msg_info.task.task_action =
12406 CTL_TASK_ABORT_TASK;
12407 msg_info.task.tag_num =
12408 io->taskio.tag_num;
12409 msg_info.task.tag_type =
12410 io->taskio.tag_type;
12411 msg_info.hdr.msg_type =
12412 CTL_MSG_MANAGE_TASKS;
12413 msg_info.hdr.original_sc = NULL;
12414 msg_info.hdr.serializing_sc = NULL;
12416 printf("Sent Abort to other side\n");
12418 if (CTL_HA_STATUS_SUCCESS !=
12419 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12421 sizeof(msg_info), 0)) {
12425 printf("ctl_abort_task: found I/O to abort\n");
12431 mtx_unlock(&lun->lun_lock);
12435 * This isn't really an error. It's entirely possible for
12436 * the abort and command completion to cross on the wire.
12437 * This is more of an informative/diagnostic error.
12440 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
12441 "%d:%d:%d:%d tag %d type %d\n",
12442 io->io_hdr.nexus.initid.id,
12443 io->io_hdr.nexus.targ_port,
12444 io->io_hdr.nexus.targ_target.id,
12445 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
12446 io->taskio.tag_type);
12453 ctl_run_task(union ctl_io *io)
12455 struct ctl_softc *ctl_softc = control_softc;
12457 const char *task_desc;
12459 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12461 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12462 ("ctl_run_task: Unextected io_type %d\n",
12463 io->io_hdr.io_type));
12465 task_desc = ctl_scsi_task_string(&io->taskio);
12466 if (task_desc != NULL) {
12468 csevent_log(CSC_CTL | CSC_SHELF_SW |
12470 csevent_LogType_Trace,
12471 csevent_Severity_Information,
12472 csevent_AlertLevel_Green,
12473 csevent_FRU_Firmware,
12474 csevent_FRU_Unknown,
12475 "CTL: received task: %s",task_desc);
12479 csevent_log(CSC_CTL | CSC_SHELF_SW |
12481 csevent_LogType_Trace,
12482 csevent_Severity_Information,
12483 csevent_AlertLevel_Green,
12484 csevent_FRU_Firmware,
12485 csevent_FRU_Unknown,
12486 "CTL: received unknown task "
12488 io->taskio.task_action,
12489 io->taskio.task_action);
12492 switch (io->taskio.task_action) {
12493 case CTL_TASK_ABORT_TASK:
12494 retval = ctl_abort_task(io);
12496 case CTL_TASK_ABORT_TASK_SET:
12497 case CTL_TASK_CLEAR_TASK_SET:
12498 retval = ctl_abort_task_set(io);
12500 case CTL_TASK_CLEAR_ACA:
12502 case CTL_TASK_I_T_NEXUS_RESET:
12503 retval = ctl_i_t_nexus_reset(io);
12505 case CTL_TASK_LUN_RESET: {
12506 struct ctl_lun *lun;
12509 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12510 mtx_lock(&ctl_softc->ctl_lock);
12511 if ((targ_lun < CTL_MAX_LUNS)
12512 && (ctl_softc->ctl_luns[targ_lun] != NULL))
12513 lun = ctl_softc->ctl_luns[targ_lun];
12515 mtx_unlock(&ctl_softc->ctl_lock);
12520 if (!(io->io_hdr.flags &
12521 CTL_FLAG_FROM_OTHER_SC)) {
12522 union ctl_ha_msg msg_info;
12524 io->io_hdr.flags |=
12525 CTL_FLAG_SENT_2OTHER_SC;
12526 msg_info.hdr.msg_type =
12527 CTL_MSG_MANAGE_TASKS;
12528 msg_info.hdr.nexus = io->io_hdr.nexus;
12529 msg_info.task.task_action =
12530 CTL_TASK_LUN_RESET;
12531 msg_info.hdr.original_sc = NULL;
12532 msg_info.hdr.serializing_sc = NULL;
12533 if (CTL_HA_STATUS_SUCCESS !=
12534 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12536 sizeof(msg_info), 0)) {
12540 retval = ctl_lun_reset(lun, io,
12542 mtx_unlock(&ctl_softc->ctl_lock);
12545 case CTL_TASK_TARGET_RESET:
12546 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET);
12548 case CTL_TASK_BUS_RESET:
12549 retval = ctl_bus_reset(ctl_softc, io);
12551 case CTL_TASK_PORT_LOGIN:
12553 case CTL_TASK_PORT_LOGOUT:
12556 printf("ctl_run_task: got unknown task management event %d\n",
12557 io->taskio.task_action);
12561 io->io_hdr.status = CTL_SUCCESS;
12563 io->io_hdr.status = CTL_ERROR;
12568 * For HA operation. Handle commands that come in from the other
12572 ctl_handle_isc(union ctl_io *io)
12575 struct ctl_lun *lun;
12576 struct ctl_softc *ctl_softc;
12579 ctl_softc = control_softc;
12581 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12582 lun = ctl_softc->ctl_luns[targ_lun];
12584 switch (io->io_hdr.msg_type) {
12585 case CTL_MSG_SERIALIZE:
12586 free_io = ctl_serialize_other_sc_cmd(&io->scsiio);
12588 case CTL_MSG_R2R: {
12589 const struct ctl_cmd_entry *entry;
12592 * This is only used in SER_ONLY mode.
12595 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12596 mtx_lock(&lun->lun_lock);
12597 if (ctl_scsiio_lun_check(ctl_softc, lun,
12598 entry, (struct ctl_scsiio *)io) != 0) {
12599 mtx_unlock(&lun->lun_lock);
12603 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12604 mtx_unlock(&lun->lun_lock);
12605 ctl_enqueue_rtr(io);
12608 case CTL_MSG_FINISH_IO:
12609 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
12614 mtx_lock(&lun->lun_lock);
12615 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
12617 ctl_check_blocked(lun);
12618 mtx_unlock(&lun->lun_lock);
12621 case CTL_MSG_PERS_ACTION:
12622 ctl_hndl_per_res_out_on_other_sc(
12623 (union ctl_ha_msg *)&io->presio.pr_msg);
12626 case CTL_MSG_BAD_JUJU:
12630 case CTL_MSG_DATAMOVE:
12631 /* Only used in XFER mode */
12633 ctl_datamove_remote(io);
12635 case CTL_MSG_DATAMOVE_DONE:
12636 /* Only used in XFER mode */
12638 io->scsiio.be_move_done(io);
12642 printf("%s: Invalid message type %d\n",
12643 __func__, io->io_hdr.msg_type);
12653 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12654 * there is no match.
12656 static ctl_lun_error_pattern
12657 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12659 const struct ctl_cmd_entry *entry;
12660 ctl_lun_error_pattern filtered_pattern, pattern;
12662 pattern = desc->error_pattern;
12665 * XXX KDM we need more data passed into this function to match a
12666 * custom pattern, and we actually need to implement custom pattern
12669 if (pattern & CTL_LUN_PAT_CMD)
12670 return (CTL_LUN_PAT_CMD);
12672 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12673 return (CTL_LUN_PAT_ANY);
12675 entry = ctl_get_cmd_entry(ctsio, NULL);
12677 filtered_pattern = entry->pattern & pattern;
12680 * If the user requested specific flags in the pattern (e.g.
12681 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12684 * If the user did not specify any flags, it doesn't matter whether
12685 * or not the command supports the flags.
12687 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12688 (pattern & ~CTL_LUN_PAT_MASK))
12689 return (CTL_LUN_PAT_NONE);
12692 * If the user asked for a range check, see if the requested LBA
12693 * range overlaps with this command's LBA range.
12695 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12701 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12703 return (CTL_LUN_PAT_NONE);
12705 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12706 desc->lba_range.len);
12708 * A "pass" means that the LBA ranges don't overlap, so
12709 * this doesn't match the user's range criteria.
12711 if (action == CTL_ACTION_PASS)
12712 return (CTL_LUN_PAT_NONE);
12715 return (filtered_pattern);
12719 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12721 struct ctl_error_desc *desc, *desc2;
12723 mtx_assert(&lun->lun_lock, MA_OWNED);
12725 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12726 ctl_lun_error_pattern pattern;
12728 * Check to see whether this particular command matches
12729 * the pattern in the descriptor.
12731 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12732 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12735 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12736 case CTL_LUN_INJ_ABORTED:
12737 ctl_set_aborted(&io->scsiio);
12739 case CTL_LUN_INJ_MEDIUM_ERR:
12740 ctl_set_medium_error(&io->scsiio);
12742 case CTL_LUN_INJ_UA:
12743 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12745 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12747 case CTL_LUN_INJ_CUSTOM:
12749 * We're assuming the user knows what he is doing.
12750 * Just copy the sense information without doing
12753 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12754 ctl_min(sizeof(desc->custom_sense),
12755 sizeof(io->scsiio.sense_data)));
12756 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12757 io->scsiio.sense_len = SSD_FULL_SIZE;
12758 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12760 case CTL_LUN_INJ_NONE:
12763 * If this is an error injection type we don't know
12764 * about, clear the continuous flag (if it is set)
12765 * so it will get deleted below.
12767 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12771 * By default, each error injection action is a one-shot
12773 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12776 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12782 #ifdef CTL_IO_DELAY
12784 ctl_datamove_timer_wakeup(void *arg)
12788 io = (union ctl_io *)arg;
12792 #endif /* CTL_IO_DELAY */
12795 ctl_datamove(union ctl_io *io)
12797 void (*fe_datamove)(union ctl_io *io);
12799 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12801 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12804 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12809 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12810 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12812 sbuf_cat(&sb, path_str);
12813 switch (io->io_hdr.io_type) {
12815 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12816 sbuf_printf(&sb, "\n");
12817 sbuf_cat(&sb, path_str);
12818 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12819 io->scsiio.tag_num, io->scsiio.tag_type);
12822 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12823 "Tag Type: %d\n", io->taskio.task_action,
12824 io->taskio.tag_num, io->taskio.tag_type);
12827 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12828 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12831 sbuf_cat(&sb, path_str);
12832 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12833 (intmax_t)time_uptime - io->io_hdr.start_time);
12835 printf("%s", sbuf_data(&sb));
12837 #endif /* CTL_TIME_IO */
12839 #ifdef CTL_IO_DELAY
12840 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12841 struct ctl_lun *lun;
12843 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12845 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12847 struct ctl_lun *lun;
12849 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12851 && (lun->delay_info.datamove_delay > 0)) {
12852 struct callout *callout;
12854 callout = (struct callout *)&io->io_hdr.timer_bytes;
12855 callout_init(callout, /*mpsafe*/ 1);
12856 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12857 callout_reset(callout,
12858 lun->delay_info.datamove_delay * hz,
12859 ctl_datamove_timer_wakeup, io);
12860 if (lun->delay_info.datamove_type ==
12861 CTL_DELAY_TYPE_ONESHOT)
12862 lun->delay_info.datamove_delay = 0;
12869 * This command has been aborted. Set the port status, so we fail
12872 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12873 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n",
12874 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id,
12875 io->io_hdr.nexus.targ_port,
12876 (uintmax_t)io->io_hdr.nexus.targ_target.id,
12877 io->io_hdr.nexus.targ_lun);
12878 io->io_hdr.port_status = 31337;
12880 * Note that the backend, in this case, will get the
12881 * callback in its context. In other cases it may get
12882 * called in the frontend's interrupt thread context.
12884 io->scsiio.be_move_done(io);
12888 /* Don't confuse frontend with zero length data move. */
12889 if (io->scsiio.kern_data_len == 0) {
12890 io->scsiio.be_move_done(io);
12895 * If we're in XFER mode and this I/O is from the other shelf
12896 * controller, we need to send the DMA to the other side to
12897 * actually transfer the data to/from the host. In serialize only
12898 * mode the transfer happens below CTL and ctl_datamove() is only
12899 * called on the machine that originally received the I/O.
12901 if ((control_softc->ha_mode == CTL_HA_MODE_XFER)
12902 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12903 union ctl_ha_msg msg;
12904 uint32_t sg_entries_sent;
12908 memset(&msg, 0, sizeof(msg));
12909 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
12910 msg.hdr.original_sc = io->io_hdr.original_sc;
12911 msg.hdr.serializing_sc = io;
12912 msg.hdr.nexus = io->io_hdr.nexus;
12913 msg.dt.flags = io->io_hdr.flags;
12915 * We convert everything into a S/G list here. We can't
12916 * pass by reference, only by value between controllers.
12917 * So we can't pass a pointer to the S/G list, only as many
12918 * S/G entries as we can fit in here. If it's possible for
12919 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
12920 * then we need to break this up into multiple transfers.
12922 if (io->scsiio.kern_sg_entries == 0) {
12923 msg.dt.kern_sg_entries = 1;
12925 * If this is in cached memory, flush the cache
12926 * before we send the DMA request to the other
12927 * controller. We want to do this in either the
12928 * read or the write case. The read case is
12929 * straightforward. In the write case, we want to
12930 * make sure nothing is in the local cache that
12931 * could overwrite the DMAed data.
12933 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12935 * XXX KDM use bus_dmamap_sync() here.
12940 * Convert to a physical address if this is a
12943 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
12944 msg.dt.sg_list[0].addr =
12945 io->scsiio.kern_data_ptr;
12948 * XXX KDM use busdma here!
12951 msg.dt.sg_list[0].addr = (void *)
12952 vtophys(io->scsiio.kern_data_ptr);
12956 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
12959 struct ctl_sg_entry *sgl;
12962 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
12963 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
12964 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12966 * XXX KDM use bus_dmamap_sync() here.
12971 msg.dt.kern_data_len = io->scsiio.kern_data_len;
12972 msg.dt.kern_total_len = io->scsiio.kern_total_len;
12973 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
12974 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
12975 msg.dt.sg_sequence = 0;
12978 * Loop until we've sent all of the S/G entries. On the
12979 * other end, we'll recompose these S/G entries into one
12980 * contiguous list before passing it to the
12982 for (sg_entries_sent = 0; sg_entries_sent <
12983 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) {
12984 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/
12985 sizeof(msg.dt.sg_list[0])),
12986 msg.dt.kern_sg_entries - sg_entries_sent);
12988 if (do_sg_copy != 0) {
12989 struct ctl_sg_entry *sgl;
12992 sgl = (struct ctl_sg_entry *)
12993 io->scsiio.kern_data_ptr;
12995 * If this is in cached memory, flush the cache
12996 * before we send the DMA request to the other
12997 * controller. We want to do this in either
12998 * the * read or the write case. The read
12999 * case is straightforward. In the write
13000 * case, we want to make sure nothing is
13001 * in the local cache that could overwrite
13005 for (i = sg_entries_sent, j = 0;
13006 i < msg.dt.cur_sg_entries; i++, j++) {
13007 if ((io->io_hdr.flags &
13008 CTL_FLAG_NO_DATASYNC) == 0) {
13010 * XXX KDM use bus_dmamap_sync()
13013 if ((io->io_hdr.flags &
13014 CTL_FLAG_BUS_ADDR) == 0) {
13016 * XXX KDM use busdma.
13019 msg.dt.sg_list[j].addr =(void *)
13020 vtophys(sgl[i].addr);
13023 msg.dt.sg_list[j].addr =
13026 msg.dt.sg_list[j].len = sgl[i].len;
13030 sg_entries_sent += msg.dt.cur_sg_entries;
13031 if (sg_entries_sent >= msg.dt.kern_sg_entries)
13032 msg.dt.sg_last = 1;
13034 msg.dt.sg_last = 0;
13037 * XXX KDM drop and reacquire the lock here?
13039 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13040 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
13042 * XXX do something here.
13046 msg.dt.sent_sg_entries = sg_entries_sent;
13048 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
13049 if (io->io_hdr.flags & CTL_FLAG_FAILOVER)
13050 ctl_failover_io(io, /*have_lock*/ 0);
13055 * Lookup the fe_datamove() function for this particular
13059 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
13066 ctl_send_datamove_done(union ctl_io *io, int have_lock)
13068 union ctl_ha_msg msg;
13071 memset(&msg, 0, sizeof(msg));
13073 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
13074 msg.hdr.original_sc = io;
13075 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
13076 msg.hdr.nexus = io->io_hdr.nexus;
13077 msg.hdr.status = io->io_hdr.status;
13078 msg.scsi.tag_num = io->scsiio.tag_num;
13079 msg.scsi.tag_type = io->scsiio.tag_type;
13080 msg.scsi.scsi_status = io->scsiio.scsi_status;
13081 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
13082 sizeof(io->scsiio.sense_data));
13083 msg.scsi.sense_len = io->scsiio.sense_len;
13084 msg.scsi.sense_residual = io->scsiio.sense_residual;
13085 msg.scsi.fetd_status = io->io_hdr.port_status;
13086 msg.scsi.residual = io->scsiio.residual;
13087 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
13089 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
13090 ctl_failover_io(io, /*have_lock*/ have_lock);
13094 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0);
13095 if (isc_status > CTL_HA_STATUS_SUCCESS) {
13096 /* XXX do something if this fails */
13102 * The DMA to the remote side is done, now we need to tell the other side
13103 * we're done so it can continue with its data movement.
13106 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
13112 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
13113 printf("%s: ISC DMA write failed with error %d", __func__,
13115 ctl_set_internal_failure(&io->scsiio,
13117 /*retry_count*/ rq->ret);
13120 ctl_dt_req_free(rq);
13123 * In this case, we had to malloc the memory locally. Free it.
13125 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
13127 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13128 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13131 * The data is in local and remote memory, so now we need to send
13132 * status (good or back) back to the other side.
13134 ctl_send_datamove_done(io, /*have_lock*/ 0);
13138 * We've moved the data from the host/controller into local memory. Now we
13139 * need to push it over to the remote controller's memory.
13142 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
13148 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
13149 ctl_datamove_remote_write_cb);
13155 ctl_datamove_remote_write(union ctl_io *io)
13158 void (*fe_datamove)(union ctl_io *io);
13161 * - Get the data from the host/HBA into local memory.
13162 * - DMA memory from the local controller to the remote controller.
13163 * - Send status back to the remote controller.
13166 retval = ctl_datamove_remote_sgl_setup(io);
13170 /* Switch the pointer over so the FETD knows what to do */
13171 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
13174 * Use a custom move done callback, since we need to send completion
13175 * back to the other controller, not to the backend on this side.
13177 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
13179 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
13188 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
13197 * In this case, we had to malloc the memory locally. Free it.
13199 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
13201 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13202 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13206 scsi_path_string(io, path_str, sizeof(path_str));
13207 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13208 sbuf_cat(&sb, path_str);
13209 scsi_command_string(&io->scsiio, NULL, &sb);
13210 sbuf_printf(&sb, "\n");
13211 sbuf_cat(&sb, path_str);
13212 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13213 io->scsiio.tag_num, io->scsiio.tag_type);
13214 sbuf_cat(&sb, path_str);
13215 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
13216 io->io_hdr.flags, io->io_hdr.status);
13218 printk("%s", sbuf_data(&sb));
13223 * The read is done, now we need to send status (good or bad) back
13224 * to the other side.
13226 ctl_send_datamove_done(io, /*have_lock*/ 0);
13232 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
13235 void (*fe_datamove)(union ctl_io *io);
13239 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
13240 printf("%s: ISC DMA read failed with error %d", __func__,
13242 ctl_set_internal_failure(&io->scsiio,
13244 /*retry_count*/ rq->ret);
13247 ctl_dt_req_free(rq);
13249 /* Switch the pointer over so the FETD knows what to do */
13250 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
13253 * Use a custom move done callback, since we need to send completion
13254 * back to the other controller, not to the backend on this side.
13256 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
13258 /* XXX KDM add checks like the ones in ctl_datamove? */
13260 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
13266 ctl_datamove_remote_sgl_setup(union ctl_io *io)
13268 struct ctl_sg_entry *local_sglist, *remote_sglist;
13269 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist;
13270 struct ctl_softc *softc;
13275 softc = control_softc;
13277 local_sglist = io->io_hdr.local_sglist;
13278 local_dma_sglist = io->io_hdr.local_dma_sglist;
13279 remote_sglist = io->io_hdr.remote_sglist;
13280 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
13282 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) {
13283 for (i = 0; i < io->scsiio.kern_sg_entries; i++) {
13284 local_sglist[i].len = remote_sglist[i].len;
13287 * XXX Detect the situation where the RS-level I/O
13288 * redirector on the other side has already read the
13289 * data off of the AOR RS on this side, and
13290 * transferred it to remote (mirror) memory on the
13291 * other side. Since we already have the data in
13292 * memory here, we just need to use it.
13294 * XXX KDM this can probably be removed once we
13295 * get the cache device code in and take the
13296 * current AOR implementation out.
13299 if ((remote_sglist[i].addr >=
13300 (void *)vtophys(softc->mirr->addr))
13301 && (remote_sglist[i].addr <
13302 ((void *)vtophys(softc->mirr->addr) +
13303 CacheMirrorOffset))) {
13304 local_sglist[i].addr = remote_sglist[i].addr -
13306 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13308 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE;
13310 local_sglist[i].addr = remote_sglist[i].addr +
13315 printf("%s: local %p, remote %p, len %d\n",
13316 __func__, local_sglist[i].addr,
13317 remote_sglist[i].addr, local_sglist[i].len);
13321 uint32_t len_to_go;
13324 * In this case, we don't have automatically allocated
13325 * memory for this I/O on this controller. This typically
13326 * happens with internal CTL I/O -- e.g. inquiry, mode
13327 * sense, etc. Anything coming from RAIDCore will have
13328 * a mirror area available.
13330 len_to_go = io->scsiio.kern_data_len;
13333 * Clear the no datasync flag, we have to use malloced
13336 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC;
13339 * The difficult thing here is that the size of the various
13340 * S/G segments may be different than the size from the
13341 * remote controller. That'll make it harder when DMAing
13342 * the data back to the other side.
13344 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) /
13345 sizeof(io->io_hdr.remote_sglist[0])) &&
13346 (len_to_go > 0); i++) {
13347 local_sglist[i].len = ctl_min(len_to_go, 131072);
13348 CTL_SIZE_8B(local_dma_sglist[i].len,
13349 local_sglist[i].len);
13350 local_sglist[i].addr =
13351 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK);
13353 local_dma_sglist[i].addr = local_sglist[i].addr;
13355 if (local_sglist[i].addr == NULL) {
13358 printf("malloc failed for %zd bytes!",
13359 local_dma_sglist[i].len);
13360 for (j = 0; j < i; j++) {
13361 free(local_sglist[j].addr, M_CTL);
13363 ctl_set_internal_failure(&io->scsiio,
13365 /*retry_count*/ 4857);
13367 goto bailout_error;
13370 /* XXX KDM do we need a sync here? */
13372 len_to_go -= local_sglist[i].len;
13375 * Reset the number of S/G entries accordingly. The
13376 * original number of S/G entries is available in
13379 io->scsiio.kern_sg_entries = i;
13382 printf("%s: kern_sg_entries = %d\n", __func__,
13383 io->scsiio.kern_sg_entries);
13384 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13385 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i,
13386 local_sglist[i].addr, local_sglist[i].len,
13387 local_dma_sglist[i].len);
13396 ctl_send_datamove_done(io, /*have_lock*/ 0);
13402 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
13403 ctl_ha_dt_cb callback)
13405 struct ctl_ha_dt_req *rq;
13406 struct ctl_sg_entry *remote_sglist, *local_sglist;
13407 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist;
13408 uint32_t local_used, remote_used, total_used;
13414 rq = ctl_dt_req_alloc();
13417 * If we failed to allocate the request, and if the DMA didn't fail
13418 * anyway, set busy status. This is just a resource allocation
13422 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE))
13423 ctl_set_busy(&io->scsiio);
13425 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
13428 ctl_dt_req_free(rq);
13431 * The data move failed. We need to return status back
13432 * to the other controller. No point in trying to DMA
13433 * data to the remote controller.
13436 ctl_send_datamove_done(io, /*have_lock*/ 0);
13443 local_sglist = io->io_hdr.local_sglist;
13444 local_dma_sglist = io->io_hdr.local_dma_sglist;
13445 remote_sglist = io->io_hdr.remote_sglist;
13446 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
13451 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) {
13452 rq->ret = CTL_HA_STATUS_SUCCESS;
13459 * Pull/push the data over the wire from/to the other controller.
13460 * This takes into account the possibility that the local and
13461 * remote sglists may not be identical in terms of the size of
13462 * the elements and the number of elements.
13464 * One fundamental assumption here is that the length allocated for
13465 * both the local and remote sglists is identical. Otherwise, we've
13466 * essentially got a coding error of some sort.
13468 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
13470 uint32_t cur_len, dma_length;
13473 rq->id = CTL_HA_DATA_CTL;
13474 rq->command = command;
13478 * Both pointers should be aligned. But it is possible
13479 * that the allocation length is not. They should both
13480 * also have enough slack left over at the end, though,
13481 * to round up to the next 8 byte boundary.
13483 cur_len = ctl_min(local_sglist[i].len - local_used,
13484 remote_sglist[j].len - remote_used);
13487 * In this case, we have a size issue and need to decrease
13488 * the size, except in the case where we actually have less
13489 * than 8 bytes left. In that case, we need to increase
13490 * the DMA length to get the last bit.
13492 if ((cur_len & 0x7) != 0) {
13493 if (cur_len > 0x7) {
13494 cur_len = cur_len - (cur_len & 0x7);
13495 dma_length = cur_len;
13497 CTL_SIZE_8B(dma_length, cur_len);
13501 dma_length = cur_len;
13504 * If we had to allocate memory for this I/O, instead of using
13505 * the non-cached mirror memory, we'll need to flush the cache
13506 * before trying to DMA to the other controller.
13508 * We could end up doing this multiple times for the same
13509 * segment if we have a larger local segment than remote
13510 * segment. That shouldn't be an issue.
13512 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
13514 * XXX KDM use bus_dmamap_sync() here.
13518 rq->size = dma_length;
13520 tmp_ptr = (uint8_t *)local_sglist[i].addr;
13521 tmp_ptr += local_used;
13523 /* Use physical addresses when talking to ISC hardware */
13524 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
13525 /* XXX KDM use busdma */
13527 rq->local = vtophys(tmp_ptr);
13530 rq->local = tmp_ptr;
13532 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
13533 tmp_ptr += remote_used;
13534 rq->remote = tmp_ptr;
13536 rq->callback = NULL;
13538 local_used += cur_len;
13539 if (local_used >= local_sglist[i].len) {
13544 remote_used += cur_len;
13545 if (remote_used >= remote_sglist[j].len) {
13549 total_used += cur_len;
13551 if (total_used >= io->scsiio.kern_data_len)
13552 rq->callback = callback;
13554 if ((rq->size & 0x7) != 0) {
13555 printf("%s: warning: size %d is not on 8b boundary\n",
13556 __func__, rq->size);
13558 if (((uintptr_t)rq->local & 0x7) != 0) {
13559 printf("%s: warning: local %p not on 8b boundary\n",
13560 __func__, rq->local);
13562 if (((uintptr_t)rq->remote & 0x7) != 0) {
13563 printf("%s: warning: remote %p not on 8b boundary\n",
13564 __func__, rq->local);
13567 printf("%s: %s: local %#x remote %#x size %d\n", __func__,
13568 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
13569 rq->local, rq->remote, rq->size);
13572 isc_ret = ctl_dt_single(rq);
13573 if (isc_ret == CTL_HA_STATUS_WAIT)
13576 if (isc_ret == CTL_HA_STATUS_DISCONNECT) {
13577 rq->ret = CTL_HA_STATUS_SUCCESS;
13591 ctl_datamove_remote_read(union ctl_io *io)
13597 * This will send an error to the other controller in the case of a
13600 retval = ctl_datamove_remote_sgl_setup(io);
13604 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
13605 ctl_datamove_remote_read_cb);
13607 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) {
13609 * Make sure we free memory if there was an error.. The
13610 * ctl_datamove_remote_xfer() function will send the
13611 * datamove done message, or call the callback with an
13612 * error if there is a problem.
13614 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13615 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13622 * Process a datamove request from the other controller. This is used for
13623 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
13624 * first. Once that is complete, the data gets DMAed into the remote
13625 * controller's memory. For reads, we DMA from the remote controller's
13626 * memory into our memory first, and then move it out to the FETD.
13629 ctl_datamove_remote(union ctl_io *io)
13631 struct ctl_softc *softc;
13633 softc = control_softc;
13635 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
13638 * Note that we look for an aborted I/O here, but don't do some of
13639 * the other checks that ctl_datamove() normally does.
13640 * We don't need to run the datamove delay code, since that should
13641 * have been done if need be on the other controller.
13643 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
13644 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__,
13645 io->scsiio.tag_num, io->io_hdr.nexus.initid.id,
13646 io->io_hdr.nexus.targ_port,
13647 io->io_hdr.nexus.targ_target.id,
13648 io->io_hdr.nexus.targ_lun);
13649 io->io_hdr.port_status = 31338;
13650 ctl_send_datamove_done(io, /*have_lock*/ 0);
13654 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) {
13655 ctl_datamove_remote_write(io);
13656 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){
13657 ctl_datamove_remote_read(io);
13659 union ctl_ha_msg msg;
13660 struct scsi_sense_data *sense;
13664 memset(&msg, 0, sizeof(msg));
13666 msg.hdr.msg_type = CTL_MSG_BAD_JUJU;
13667 msg.hdr.status = CTL_SCSI_ERROR;
13668 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
13670 retry_count = 4243;
13672 sense = &msg.scsi.sense_data;
13673 sks[0] = SSD_SCS_VALID;
13674 sks[1] = (retry_count >> 8) & 0xff;
13675 sks[2] = retry_count & 0xff;
13677 /* "Internal target failure" */
13678 scsi_set_sense_data(sense,
13679 /*sense_format*/ SSD_TYPE_NONE,
13680 /*current_error*/ 1,
13681 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
13684 /*type*/ SSD_ELEM_SKS,
13685 /*size*/ sizeof(sks),
13689 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
13690 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
13691 ctl_failover_io(io, /*have_lock*/ 1);
13695 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) >
13696 CTL_HA_STATUS_SUCCESS) {
13697 /* XXX KDM what to do if this fails? */
13705 ctl_process_done(union ctl_io *io)
13707 struct ctl_lun *lun;
13708 struct ctl_softc *ctl_softc = control_softc;
13709 void (*fe_done)(union ctl_io *io);
13710 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port);
13712 CTL_DEBUG_PRINT(("ctl_process_done\n"));
13715 control_softc->ctl_ports[targ_port]->fe_done;
13718 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
13723 ctl_scsi_path_string(io, path_str, sizeof(path_str));
13724 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13726 sbuf_cat(&sb, path_str);
13727 switch (io->io_hdr.io_type) {
13729 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
13730 sbuf_printf(&sb, "\n");
13731 sbuf_cat(&sb, path_str);
13732 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13733 io->scsiio.tag_num, io->scsiio.tag_type);
13736 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
13737 "Tag Type: %d\n", io->taskio.task_action,
13738 io->taskio.tag_num, io->taskio.tag_type);
13741 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13742 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13745 sbuf_cat(&sb, path_str);
13746 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
13747 (intmax_t)time_uptime - io->io_hdr.start_time);
13749 printf("%s", sbuf_data(&sb));
13751 #endif /* CTL_TIME_IO */
13753 switch (io->io_hdr.io_type) {
13757 if (bootverbose || (ctl_debug & CTL_DEBUG_INFO))
13758 ctl_io_error_print(io, NULL);
13759 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
13763 return (CTL_RETVAL_COMPLETE);
13765 panic("ctl_process_done: invalid io type %d\n",
13766 io->io_hdr.io_type);
13767 break; /* NOTREACHED */
13770 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13772 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13773 io->io_hdr.nexus.targ_mapped_lun));
13777 mtx_lock(&lun->lun_lock);
13780 * Check to see if we have any errors to inject here. We only
13781 * inject errors for commands that don't already have errors set.
13783 if ((STAILQ_FIRST(&lun->error_list) != NULL)
13784 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))
13785 ctl_inject_error(lun, io);
13788 * XXX KDM how do we treat commands that aren't completed
13791 * XXX KDM should we also track I/O latency?
13793 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13794 io->io_hdr.io_type == CTL_IO_SCSI) {
13796 struct bintime cur_bt;
13800 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13802 type = CTL_STATS_READ;
13803 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13805 type = CTL_STATS_WRITE;
13807 type = CTL_STATS_NO_IO;
13809 lun->stats.ports[targ_port].bytes[type] +=
13810 io->scsiio.kern_total_len;
13811 lun->stats.ports[targ_port].operations[type]++;
13813 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
13814 &io->io_hdr.dma_bt);
13815 lun->stats.ports[targ_port].num_dmas[type] +=
13816 io->io_hdr.num_dmas;
13817 getbintime(&cur_bt);
13818 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
13819 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
13824 * Remove this from the OOA queue.
13826 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13829 * Run through the blocked queue on this LUN and see if anything
13830 * has become unblocked, now that this transaction is done.
13832 ctl_check_blocked(lun);
13835 * If the LUN has been invalidated, free it if there is nothing
13836 * left on its OOA queue.
13838 if ((lun->flags & CTL_LUN_INVALID)
13839 && TAILQ_EMPTY(&lun->ooa_queue)) {
13840 mtx_unlock(&lun->lun_lock);
13841 mtx_lock(&ctl_softc->ctl_lock);
13843 mtx_unlock(&ctl_softc->ctl_lock);
13845 mtx_unlock(&lun->lun_lock);
13850 * If this command has been aborted, make sure we set the status
13851 * properly. The FETD is responsible for freeing the I/O and doing
13852 * whatever it needs to do to clean up its state.
13854 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13855 ctl_set_task_aborted(&io->scsiio);
13858 * If enabled, print command error status.
13859 * We don't print UAs unless debugging was enabled explicitly.
13862 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
13864 if (!bootverbose && (ctl_debug & CTL_DEBUG_INFO) == 0)
13866 if ((ctl_debug & CTL_DEBUG_INFO) == 0 &&
13867 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) &&
13868 (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) {
13869 int error_code, sense_key, asc, ascq;
13871 scsi_extract_sense_len(&io->scsiio.sense_data,
13872 io->scsiio.sense_len, &error_code, &sense_key,
13873 &asc, &ascq, /*show_errors*/ 0);
13874 if (sense_key == SSD_KEY_UNIT_ATTENTION)
13878 ctl_io_error_print(io, NULL);
13882 * Tell the FETD or the other shelf controller we're done with this
13883 * command. Note that only SCSI commands get to this point. Task
13884 * management commands are completed above.
13886 * We only send status to the other controller if we're in XFER
13887 * mode. In SER_ONLY mode, the I/O is done on the controller that
13888 * received the I/O (from CTL's perspective), and so the status is
13891 * XXX KDM if we hold the lock here, we could cause a deadlock
13892 * if the frontend comes back in in this context to queue
13895 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER)
13896 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
13897 union ctl_ha_msg msg;
13899 memset(&msg, 0, sizeof(msg));
13900 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13901 msg.hdr.original_sc = io->io_hdr.original_sc;
13902 msg.hdr.nexus = io->io_hdr.nexus;
13903 msg.hdr.status = io->io_hdr.status;
13904 msg.scsi.scsi_status = io->scsiio.scsi_status;
13905 msg.scsi.tag_num = io->scsiio.tag_num;
13906 msg.scsi.tag_type = io->scsiio.tag_type;
13907 msg.scsi.sense_len = io->scsiio.sense_len;
13908 msg.scsi.sense_residual = io->scsiio.sense_residual;
13909 msg.scsi.residual = io->scsiio.residual;
13910 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
13911 sizeof(io->scsiio.sense_data));
13913 * We copy this whether or not this is an I/O-related
13914 * command. Otherwise, we'd have to go and check to see
13915 * whether it's a read/write command, and it really isn't
13918 memcpy(&msg.scsi.lbalen,
13919 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
13920 sizeof(msg.scsi.lbalen));
13922 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13923 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
13924 /* XXX do something here */
13931 return (CTL_RETVAL_COMPLETE);
13936 * Front end should call this if it doesn't do autosense. When the request
13937 * sense comes back in from the initiator, we'll dequeue this and send it.
13940 ctl_queue_sense(union ctl_io *io)
13942 struct ctl_lun *lun;
13943 struct ctl_softc *ctl_softc;
13944 uint32_t initidx, targ_lun;
13946 ctl_softc = control_softc;
13948 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13951 * LUN lookup will likely move to the ctl_work_thread() once we
13952 * have our new queueing infrastructure (that doesn't put things on
13953 * a per-LUN queue initially). That is so that we can handle
13954 * things like an INQUIRY to a LUN that we don't have enabled. We
13955 * can't deal with that right now.
13957 mtx_lock(&ctl_softc->ctl_lock);
13960 * If we don't have a LUN for this, just toss the sense
13963 targ_lun = io->io_hdr.nexus.targ_lun;
13964 targ_lun = ctl_map_lun(io->io_hdr.nexus.targ_port, targ_lun);
13965 if ((targ_lun < CTL_MAX_LUNS)
13966 && (ctl_softc->ctl_luns[targ_lun] != NULL))
13967 lun = ctl_softc->ctl_luns[targ_lun];
13971 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13973 mtx_lock(&lun->lun_lock);
13975 * Already have CA set for this LUN...toss the sense information.
13977 if (ctl_is_set(lun->have_ca, initidx)) {
13978 mtx_unlock(&lun->lun_lock);
13982 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data,
13983 ctl_min(sizeof(lun->pending_sense[initidx]),
13984 sizeof(io->scsiio.sense_data)));
13985 ctl_set_mask(lun->have_ca, initidx);
13986 mtx_unlock(&lun->lun_lock);
13989 mtx_unlock(&ctl_softc->ctl_lock);
13993 return (CTL_RETVAL_COMPLETE);
13998 * Primary command inlet from frontend ports. All SCSI and task I/O
13999 * requests must go through this function.
14002 ctl_queue(union ctl_io *io)
14004 struct ctl_softc *ctl_softc;
14006 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
14008 ctl_softc = control_softc;
14011 io->io_hdr.start_time = time_uptime;
14012 getbintime(&io->io_hdr.start_bt);
14013 #endif /* CTL_TIME_IO */
14015 /* Map FE-specific LUN ID into global one. */
14016 io->io_hdr.nexus.targ_mapped_lun =
14017 ctl_map_lun(io->io_hdr.nexus.targ_port, io->io_hdr.nexus.targ_lun);
14019 switch (io->io_hdr.io_type) {
14022 if (ctl_debug & CTL_DEBUG_CDB)
14024 ctl_enqueue_incoming(io);
14027 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
14031 return (CTL_RETVAL_COMPLETE);
14034 #ifdef CTL_IO_DELAY
14036 ctl_done_timer_wakeup(void *arg)
14040 io = (union ctl_io *)arg;
14043 #endif /* CTL_IO_DELAY */
14046 ctl_done(union ctl_io *io)
14048 struct ctl_softc *ctl_softc;
14050 ctl_softc = control_softc;
14053 * Enable this to catch duplicate completion issues.
14056 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
14057 printf("%s: type %d msg %d cdb %x iptl: "
14058 "%d:%d:%d:%d tag 0x%04x "
14059 "flag %#x status %x\n",
14061 io->io_hdr.io_type,
14062 io->io_hdr.msg_type,
14064 io->io_hdr.nexus.initid.id,
14065 io->io_hdr.nexus.targ_port,
14066 io->io_hdr.nexus.targ_target.id,
14067 io->io_hdr.nexus.targ_lun,
14068 (io->io_hdr.io_type ==
14070 io->taskio.tag_num :
14071 io->scsiio.tag_num,
14073 io->io_hdr.status);
14075 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
14079 * This is an internal copy of an I/O, and should not go through
14080 * the normal done processing logic.
14082 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
14086 * We need to send a msg to the serializing shelf to finish the IO
14087 * as well. We don't send a finish message to the other shelf if
14088 * this is a task management command. Task management commands
14089 * aren't serialized in the OOA queue, but rather just executed on
14090 * both shelf controllers for commands that originated on that
14093 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)
14094 && (io->io_hdr.io_type != CTL_IO_TASK)) {
14095 union ctl_ha_msg msg_io;
14097 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO;
14098 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc;
14099 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io,
14100 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) {
14102 /* continue on to finish IO */
14104 #ifdef CTL_IO_DELAY
14105 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
14106 struct ctl_lun *lun;
14108 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
14110 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
14112 struct ctl_lun *lun;
14114 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
14117 && (lun->delay_info.done_delay > 0)) {
14118 struct callout *callout;
14120 callout = (struct callout *)&io->io_hdr.timer_bytes;
14121 callout_init(callout, /*mpsafe*/ 1);
14122 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
14123 callout_reset(callout,
14124 lun->delay_info.done_delay * hz,
14125 ctl_done_timer_wakeup, io);
14126 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
14127 lun->delay_info.done_delay = 0;
14131 #endif /* CTL_IO_DELAY */
14133 ctl_enqueue_done(io);
14137 ctl_isc(struct ctl_scsiio *ctsio)
14139 struct ctl_lun *lun;
14142 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
14144 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0]));
14146 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n"));
14148 retval = lun->backend->data_submit((union ctl_io *)ctsio);
14155 ctl_work_thread(void *arg)
14157 struct ctl_thread *thr = (struct ctl_thread *)arg;
14158 struct ctl_softc *softc = thr->ctl_softc;
14162 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
14168 * We handle the queues in this order:
14170 * - done queue (to free up resources, unblock other commands)
14174 * If those queues are empty, we break out of the loop and
14177 mtx_lock(&thr->queue_lock);
14178 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
14180 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
14181 mtx_unlock(&thr->queue_lock);
14182 ctl_handle_isc(io);
14185 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
14187 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
14188 /* clear any blocked commands, call fe_done */
14189 mtx_unlock(&thr->queue_lock);
14190 retval = ctl_process_done(io);
14193 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
14195 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
14196 mtx_unlock(&thr->queue_lock);
14197 if (io->io_hdr.io_type == CTL_IO_TASK)
14200 ctl_scsiio_precheck(softc, &io->scsiio);
14203 if (!ctl_pause_rtr) {
14204 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
14206 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
14207 mtx_unlock(&thr->queue_lock);
14208 retval = ctl_scsiio(&io->scsiio);
14209 if (retval != CTL_RETVAL_COMPLETE)
14210 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
14215 /* Sleep until we have something to do. */
14216 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
14221 ctl_lun_thread(void *arg)
14223 struct ctl_softc *softc = (struct ctl_softc *)arg;
14224 struct ctl_be_lun *be_lun;
14227 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
14231 mtx_lock(&softc->ctl_lock);
14232 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
14233 if (be_lun != NULL) {
14234 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
14235 mtx_unlock(&softc->ctl_lock);
14236 ctl_create_lun(be_lun);
14240 /* Sleep until we have something to do. */
14241 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
14242 PDROP | PRIBIO, "-", 0);
14247 ctl_thresh_thread(void *arg)
14249 struct ctl_softc *softc = (struct ctl_softc *)arg;
14250 struct ctl_lun *lun;
14251 struct ctl_be_lun *be_lun;
14252 struct scsi_da_rw_recovery_page *rwpage;
14253 struct ctl_logical_block_provisioning_page *page;
14255 uint64_t thres, val;
14258 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
14261 mtx_lock(&softc->ctl_lock);
14262 STAILQ_FOREACH(lun, &softc->lun_list, links) {
14263 be_lun = lun->be_lun;
14264 if ((lun->flags & CTL_LUN_DISABLED) ||
14265 (lun->flags & CTL_LUN_OFFLINE) ||
14266 (be_lun->flags & CTL_LUN_FLAG_UNMAP) == 0 ||
14267 lun->backend->lun_attr == NULL)
14269 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT];
14270 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0)
14273 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT];
14274 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
14275 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
14277 thres = scsi_4btoul(page->descr[i].count);
14278 thres <<= CTL_LBP_EXPONENT;
14279 switch (page->descr[i].resource) {
14281 attr = "blocksavail";
14284 attr = "blocksused";
14287 attr = "poolblocksavail";
14290 attr = "poolblocksused";
14295 mtx_unlock(&softc->ctl_lock); // XXX
14296 val = lun->backend->lun_attr(
14297 lun->be_lun->be_lun, attr);
14298 mtx_lock(&softc->ctl_lock);
14299 if (val == UINT64_MAX)
14301 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
14302 == SLBPPD_ARMING_INC)
14303 e |= (val >= thres);
14305 e |= (val <= thres);
14307 mtx_lock(&lun->lun_lock);
14309 if (lun->lasttpt == 0 ||
14310 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
14311 lun->lasttpt = time_uptime;
14312 for (i = 0; i < CTL_MAX_INITIATORS; i++)
14313 lun->pending_ua[i] |=
14314 CTL_UA_THIN_PROV_THRES;
14318 for (i = 0; i < CTL_MAX_INITIATORS; i++)
14319 lun->pending_ua[i] &= ~CTL_UA_THIN_PROV_THRES;
14321 mtx_unlock(&lun->lun_lock);
14323 mtx_unlock(&softc->ctl_lock);
14324 pause("-", CTL_LBP_PERIOD * hz);
14329 ctl_enqueue_incoming(union ctl_io *io)
14331 struct ctl_softc *softc = control_softc;
14332 struct ctl_thread *thr;
14335 idx = (io->io_hdr.nexus.targ_port * 127 +
14336 io->io_hdr.nexus.initid.id) % worker_threads;
14337 thr = &softc->threads[idx];
14338 mtx_lock(&thr->queue_lock);
14339 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
14340 mtx_unlock(&thr->queue_lock);
14345 ctl_enqueue_rtr(union ctl_io *io)
14347 struct ctl_softc *softc = control_softc;
14348 struct ctl_thread *thr;
14350 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14351 mtx_lock(&thr->queue_lock);
14352 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
14353 mtx_unlock(&thr->queue_lock);
14358 ctl_enqueue_done(union ctl_io *io)
14360 struct ctl_softc *softc = control_softc;
14361 struct ctl_thread *thr;
14363 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14364 mtx_lock(&thr->queue_lock);
14365 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
14366 mtx_unlock(&thr->queue_lock);
14371 ctl_enqueue_isc(union ctl_io *io)
14373 struct ctl_softc *softc = control_softc;
14374 struct ctl_thread *thr;
14376 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14377 mtx_lock(&thr->queue_lock);
14378 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
14379 mtx_unlock(&thr->queue_lock);
14383 /* Initialization and failover */
14386 ctl_init_isc_msg(void)
14388 printf("CTL: Still calling this thing\n");
14393 * Initializes component into configuration defined by bootMode
14395 * returns hasc_Status:
14397 * ERROR - fatal error
14399 static ctl_ha_comp_status
14400 ctl_isc_init(struct ctl_ha_component *c)
14402 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
14409 * Starts component in state requested. If component starts successfully,
14410 * it must set its own state to the requestrd state
14411 * When requested state is HASC_STATE_HA, the component may refine it
14412 * by adding _SLAVE or _MASTER flags.
14413 * Currently allowed state transitions are:
14414 * UNKNOWN->HA - initial startup
14415 * UNKNOWN->SINGLE - initial startup when no parter detected
14416 * HA->SINGLE - failover
14417 * returns ctl_ha_comp_status:
14418 * OK - component successfully started in requested state
14419 * FAILED - could not start the requested state, failover may
14421 * ERROR - fatal error detected, no future startup possible
14423 static ctl_ha_comp_status
14424 ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state)
14426 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
14428 printf("%s: go\n", __func__);
14430 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap)
14431 if (c->state == CTL_HA_STATE_UNKNOWN ) {
14432 control_softc->is_single = 0;
14433 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
14434 != CTL_HA_STATUS_SUCCESS) {
14435 printf("ctl_isc_start: ctl_ha_msg_create failed.\n");
14436 ret = CTL_HA_COMP_STATUS_ERROR;
14438 } else if (CTL_HA_STATE_IS_HA(c->state)
14439 && CTL_HA_STATE_IS_SINGLE(state)){
14440 // HA->SINGLE transition
14442 control_softc->is_single = 1;
14444 printf("ctl_isc_start:Invalid state transition %X->%X\n",
14446 ret = CTL_HA_COMP_STATUS_ERROR;
14448 if (CTL_HA_STATE_IS_SINGLE(state))
14449 control_softc->is_single = 1;
14457 * Quiesce component
14458 * The component must clear any error conditions (set status to OK) and
14459 * prepare itself to another Start call
14460 * returns ctl_ha_comp_status:
14464 static ctl_ha_comp_status
14465 ctl_isc_quiesce(struct ctl_ha_component *c)
14467 int ret = CTL_HA_COMP_STATUS_OK;
14474 struct ctl_ha_component ctl_ha_component_ctlisc =
14477 .state = CTL_HA_STATE_UNKNOWN,
14478 .init = ctl_isc_init,
14479 .start = ctl_isc_start,
14480 .quiesce = ctl_isc_quiesce