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>
70 #include <cam/scsi/scsi_all.h>
71 #include <cam/scsi/scsi_da.h>
72 #include <cam/ctl/ctl_io.h>
73 #include <cam/ctl/ctl.h>
74 #include <cam/ctl/ctl_frontend.h>
75 #include <cam/ctl/ctl_frontend_internal.h>
76 #include <cam/ctl/ctl_util.h>
77 #include <cam/ctl/ctl_backend.h>
78 #include <cam/ctl/ctl_ioctl.h>
79 #include <cam/ctl/ctl_ha.h>
80 #include <cam/ctl/ctl_private.h>
81 #include <cam/ctl/ctl_debug.h>
82 #include <cam/ctl/ctl_scsi_all.h>
83 #include <cam/ctl/ctl_error.h>
85 struct ctl_softc *control_softc = NULL;
88 * Size and alignment macros needed for Copan-specific HA hardware. These
89 * can go away when the HA code is re-written, and uses busdma for any
92 #define CTL_ALIGN_8B(target, source, type) \
93 if (((uint32_t)source & 0x7) != 0) \
94 target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\
96 target = (type)source;
98 #define CTL_SIZE_8B(target, size) \
99 if ((size & 0x7) != 0) \
100 target = size + (0x8 - (size & 0x7)); \
104 #define CTL_ALIGN_8B_MARGIN 16
107 * Template mode pages.
111 * Note that these are default values only. The actual values will be
112 * filled in when the user does a mode sense.
114 static struct copan_debugconf_subpage debugconf_page_default = {
115 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
116 DBGCNF_SUBPAGE_CODE, /* subpage */
117 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
118 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
119 DBGCNF_VERSION, /* page_version */
120 {CTL_TIME_IO_DEFAULT_SECS>>8,
121 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */
124 static struct copan_debugconf_subpage debugconf_page_changeable = {
125 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
126 DBGCNF_SUBPAGE_CODE, /* subpage */
127 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
128 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
129 0, /* page_version */
130 {0xff,0xff}, /* ctl_time_io_secs */
133 static struct scsi_da_rw_recovery_page rw_er_page_default = {
134 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
135 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
136 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE,
137 /*read_retry_count*/0,
138 /*correction_span*/0,
139 /*head_offset_count*/0,
140 /*data_strobe_offset_cnt*/0,
141 /*byte8*/SMS_RWER_LBPERE,
142 /*write_retry_count*/0,
144 /*recovery_time_limit*/{0, 0},
147 static struct scsi_da_rw_recovery_page rw_er_page_changeable = {
148 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
149 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
151 /*read_retry_count*/0,
152 /*correction_span*/0,
153 /*head_offset_count*/0,
154 /*data_strobe_offset_cnt*/0,
156 /*write_retry_count*/0,
158 /*recovery_time_limit*/{0, 0},
161 static struct scsi_format_page format_page_default = {
162 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
163 /*page_length*/sizeof(struct scsi_format_page) - 2,
164 /*tracks_per_zone*/ {0, 0},
165 /*alt_sectors_per_zone*/ {0, 0},
166 /*alt_tracks_per_zone*/ {0, 0},
167 /*alt_tracks_per_lun*/ {0, 0},
168 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
169 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
170 /*bytes_per_sector*/ {0, 0},
171 /*interleave*/ {0, 0},
172 /*track_skew*/ {0, 0},
173 /*cylinder_skew*/ {0, 0},
175 /*reserved*/ {0, 0, 0}
178 static struct scsi_format_page format_page_changeable = {
179 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
180 /*page_length*/sizeof(struct scsi_format_page) - 2,
181 /*tracks_per_zone*/ {0, 0},
182 /*alt_sectors_per_zone*/ {0, 0},
183 /*alt_tracks_per_zone*/ {0, 0},
184 /*alt_tracks_per_lun*/ {0, 0},
185 /*sectors_per_track*/ {0, 0},
186 /*bytes_per_sector*/ {0, 0},
187 /*interleave*/ {0, 0},
188 /*track_skew*/ {0, 0},
189 /*cylinder_skew*/ {0, 0},
191 /*reserved*/ {0, 0, 0}
194 static struct scsi_rigid_disk_page rigid_disk_page_default = {
195 /*page_code*/SMS_RIGID_DISK_PAGE,
196 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
197 /*cylinders*/ {0, 0, 0},
198 /*heads*/ CTL_DEFAULT_HEADS,
199 /*start_write_precomp*/ {0, 0, 0},
200 /*start_reduced_current*/ {0, 0, 0},
201 /*step_rate*/ {0, 0},
202 /*landing_zone_cylinder*/ {0, 0, 0},
203 /*rpl*/ SRDP_RPL_DISABLED,
204 /*rotational_offset*/ 0,
206 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
207 CTL_DEFAULT_ROTATION_RATE & 0xff},
211 static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
212 /*page_code*/SMS_RIGID_DISK_PAGE,
213 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
214 /*cylinders*/ {0, 0, 0},
216 /*start_write_precomp*/ {0, 0, 0},
217 /*start_reduced_current*/ {0, 0, 0},
218 /*step_rate*/ {0, 0},
219 /*landing_zone_cylinder*/ {0, 0, 0},
221 /*rotational_offset*/ 0,
223 /*rotation_rate*/ {0, 0},
227 static struct scsi_caching_page caching_page_default = {
228 /*page_code*/SMS_CACHING_PAGE,
229 /*page_length*/sizeof(struct scsi_caching_page) - 2,
230 /*flags1*/ SCP_DISC | SCP_WCE,
232 /*disable_pf_transfer_len*/ {0xff, 0xff},
233 /*min_prefetch*/ {0, 0},
234 /*max_prefetch*/ {0xff, 0xff},
235 /*max_pf_ceiling*/ {0xff, 0xff},
237 /*cache_segments*/ 0,
238 /*cache_seg_size*/ {0, 0},
240 /*non_cache_seg_size*/ {0, 0, 0}
243 static struct scsi_caching_page caching_page_changeable = {
244 /*page_code*/SMS_CACHING_PAGE,
245 /*page_length*/sizeof(struct scsi_caching_page) - 2,
246 /*flags1*/ SCP_WCE | SCP_RCD,
248 /*disable_pf_transfer_len*/ {0, 0},
249 /*min_prefetch*/ {0, 0},
250 /*max_prefetch*/ {0, 0},
251 /*max_pf_ceiling*/ {0, 0},
253 /*cache_segments*/ 0,
254 /*cache_seg_size*/ {0, 0},
256 /*non_cache_seg_size*/ {0, 0, 0}
259 static struct scsi_control_page control_page_default = {
260 /*page_code*/SMS_CONTROL_MODE_PAGE,
261 /*page_length*/sizeof(struct scsi_control_page) - 2,
263 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED,
266 /*aen_holdoff_period*/{0, 0},
267 /*busy_timeout_period*/{0, 0},
268 /*extended_selftest_completion_time*/{0, 0}
271 static struct scsi_control_page control_page_changeable = {
272 /*page_code*/SMS_CONTROL_MODE_PAGE,
273 /*page_length*/sizeof(struct scsi_control_page) - 2,
275 /*queue_flags*/SCP_QUEUE_ALG_MASK,
276 /*eca_and_aen*/SCP_SWP,
278 /*aen_holdoff_period*/{0, 0},
279 /*busy_timeout_period*/{0, 0},
280 /*extended_selftest_completion_time*/{0, 0}
283 static struct scsi_info_exceptions_page ie_page_default = {
284 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
285 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
286 /*info_flags*/SIEP_FLAGS_DEXCPT,
288 /*interval_timer*/{0, 0, 0, 0},
289 /*report_count*/{0, 0, 0, 0}
292 static struct scsi_info_exceptions_page ie_page_changeable = {
293 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
294 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
297 /*interval_timer*/{0, 0, 0, 0},
298 /*report_count*/{0, 0, 0, 0}
301 #define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4)
303 static struct ctl_logical_block_provisioning_page lbp_page_default = {{
304 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
305 /*subpage_code*/0x02,
306 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
308 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
313 /*count*/{0, 0, 0, 0}},
317 /*count*/{0, 0, 0, 0}},
321 /*count*/{0, 0, 0, 0}},
325 /*count*/{0, 0, 0, 0}}
329 static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{
330 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
331 /*subpage_code*/0x02,
332 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
334 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
339 /*count*/{0, 0, 0, 0}},
343 /*count*/{0, 0, 0, 0}},
347 /*count*/{0, 0, 0, 0}},
351 /*count*/{0, 0, 0, 0}}
356 * XXX KDM move these into the softc.
358 static int rcv_sync_msg;
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);
405 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
406 struct ctl_be_lun *be_lun, struct ctl_id target_id);
407 static int ctl_free_lun(struct ctl_lun *lun);
408 static void ctl_create_lun(struct ctl_be_lun *be_lun);
410 static void ctl_failover_change_pages(struct ctl_softc *softc,
411 struct ctl_scsiio *ctsio, int master);
414 static int ctl_do_mode_select(union ctl_io *io);
415 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
416 uint64_t res_key, uint64_t sa_res_key,
417 uint8_t type, uint32_t residx,
418 struct ctl_scsiio *ctsio,
419 struct scsi_per_res_out *cdb,
420 struct scsi_per_res_out_parms* param);
421 static void ctl_pro_preempt_other(struct ctl_lun *lun,
422 union ctl_ha_msg *msg);
423 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
424 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
425 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
426 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
427 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
428 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
429 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
431 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
433 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
434 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
435 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
436 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
437 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
438 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2);
439 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
440 union ctl_io *pending_io, union ctl_io *ooa_io);
441 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
442 union ctl_io *starting_io);
443 static int ctl_check_blocked(struct ctl_lun *lun);
444 static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc,
446 const struct ctl_cmd_entry *entry,
447 struct ctl_scsiio *ctsio);
448 //static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc);
449 static void ctl_failover(void);
450 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
451 struct ctl_scsiio *ctsio);
452 static int ctl_scsiio(struct ctl_scsiio *ctsio);
454 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
455 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
456 ctl_ua_type ua_type);
457 static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io,
458 ctl_ua_type ua_type);
459 static int ctl_abort_task(union ctl_io *io);
460 static int ctl_abort_task_set(union ctl_io *io);
461 static int ctl_i_t_nexus_reset(union ctl_io *io);
462 static void ctl_run_task(union ctl_io *io);
464 static void ctl_datamove_timer_wakeup(void *arg);
465 static void ctl_done_timer_wakeup(void *arg);
466 #endif /* CTL_IO_DELAY */
468 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
469 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
470 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
471 static void ctl_datamove_remote_write(union ctl_io *io);
472 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
473 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
474 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
475 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
476 ctl_ha_dt_cb callback);
477 static void ctl_datamove_remote_read(union ctl_io *io);
478 static void ctl_datamove_remote(union ctl_io *io);
479 static int ctl_process_done(union ctl_io *io);
480 static void ctl_lun_thread(void *arg);
481 static void ctl_thresh_thread(void *arg);
482 static void ctl_work_thread(void *arg);
483 static void ctl_enqueue_incoming(union ctl_io *io);
484 static void ctl_enqueue_rtr(union ctl_io *io);
485 static void ctl_enqueue_done(union ctl_io *io);
486 static void ctl_enqueue_isc(union ctl_io *io);
487 static const struct ctl_cmd_entry *
488 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
489 static const struct ctl_cmd_entry *
490 ctl_validate_command(struct ctl_scsiio *ctsio);
491 static int ctl_cmd_applicable(uint8_t lun_type,
492 const struct ctl_cmd_entry *entry);
495 * Load the serialization table. This isn't very pretty, but is probably
496 * the easiest way to do it.
498 #include "ctl_ser_table.c"
501 * We only need to define open, close and ioctl routines for this driver.
503 static struct cdevsw ctl_cdevsw = {
504 .d_version = D_VERSION,
507 .d_close = ctl_close,
508 .d_ioctl = ctl_ioctl,
513 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
514 MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests");
516 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
518 static moduledata_t ctl_moduledata = {
520 ctl_module_event_handler,
524 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
525 MODULE_VERSION(ctl, 1);
527 static struct ctl_frontend ioctl_frontend =
533 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
534 union ctl_ha_msg *msg_info)
536 struct ctl_scsiio *ctsio;
538 if (msg_info->hdr.original_sc == NULL) {
539 printf("%s: original_sc == NULL!\n", __func__);
540 /* XXX KDM now what? */
544 ctsio = &msg_info->hdr.original_sc->scsiio;
545 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
546 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
547 ctsio->io_hdr.status = msg_info->hdr.status;
548 ctsio->scsi_status = msg_info->scsi.scsi_status;
549 ctsio->sense_len = msg_info->scsi.sense_len;
550 ctsio->sense_residual = msg_info->scsi.sense_residual;
551 ctsio->residual = msg_info->scsi.residual;
552 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
553 sizeof(ctsio->sense_data));
554 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
555 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen));
556 ctl_enqueue_isc((union ctl_io *)ctsio);
560 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
561 union ctl_ha_msg *msg_info)
563 struct ctl_scsiio *ctsio;
565 if (msg_info->hdr.serializing_sc == NULL) {
566 printf("%s: serializing_sc == NULL!\n", __func__);
567 /* XXX KDM now what? */
571 ctsio = &msg_info->hdr.serializing_sc->scsiio;
574 * Attempt to catch the situation where an I/O has
575 * been freed, and we're using it again.
577 if (ctsio->io_hdr.io_type == 0xff) {
578 union ctl_io *tmp_io;
579 tmp_io = (union ctl_io *)ctsio;
580 printf("%s: %p use after free!\n", __func__,
582 printf("%s: type %d msg %d cdb %x iptl: "
583 "%d:%d:%d:%d tag 0x%04x "
584 "flag %#x status %x\n",
586 tmp_io->io_hdr.io_type,
587 tmp_io->io_hdr.msg_type,
588 tmp_io->scsiio.cdb[0],
589 tmp_io->io_hdr.nexus.initid.id,
590 tmp_io->io_hdr.nexus.targ_port,
591 tmp_io->io_hdr.nexus.targ_target.id,
592 tmp_io->io_hdr.nexus.targ_lun,
593 (tmp_io->io_hdr.io_type ==
595 tmp_io->taskio.tag_num :
596 tmp_io->scsiio.tag_num,
597 tmp_io->io_hdr.flags,
598 tmp_io->io_hdr.status);
601 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
602 ctl_enqueue_isc((union ctl_io *)ctsio);
606 * ISC (Inter Shelf Communication) event handler. Events from the HA
607 * subsystem come in here.
610 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
612 struct ctl_softc *ctl_softc;
614 struct ctl_prio *presio;
615 ctl_ha_status isc_status;
617 ctl_softc = control_softc;
622 printf("CTL: Isc Msg event %d\n", event);
624 if (event == CTL_HA_EVT_MSG_RECV) {
625 union ctl_ha_msg msg_info;
627 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
628 sizeof(msg_info), /*wait*/ 0);
630 printf("CTL: msg_type %d\n", msg_info.msg_type);
632 if (isc_status != 0) {
633 printf("Error receiving message, status = %d\n",
638 switch (msg_info.hdr.msg_type) {
639 case CTL_MSG_SERIALIZE:
641 printf("Serialize\n");
643 io = ctl_alloc_io_nowait(ctl_softc->othersc_pool);
645 printf("ctl_isc_event_handler: can't allocate "
648 /* Need to set busy and send msg back */
649 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
650 msg_info.hdr.status = CTL_SCSI_ERROR;
651 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY;
652 msg_info.scsi.sense_len = 0;
653 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
654 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){
659 // populate ctsio from msg_info
660 io->io_hdr.io_type = CTL_IO_SCSI;
661 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
662 io->io_hdr.original_sc = msg_info.hdr.original_sc;
664 printf("pOrig %x\n", (int)msg_info.original_sc);
666 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
669 * If we're in serialization-only mode, we don't
670 * want to go through full done processing. Thus
673 * XXX KDM add another flag that is more specific.
675 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)
676 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
677 io->io_hdr.nexus = msg_info.hdr.nexus;
679 printf("targ %d, port %d, iid %d, lun %d\n",
680 io->io_hdr.nexus.targ_target.id,
681 io->io_hdr.nexus.targ_port,
682 io->io_hdr.nexus.initid.id,
683 io->io_hdr.nexus.targ_lun);
685 io->scsiio.tag_num = msg_info.scsi.tag_num;
686 io->scsiio.tag_type = msg_info.scsi.tag_type;
687 memcpy(io->scsiio.cdb, msg_info.scsi.cdb,
689 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
690 const struct ctl_cmd_entry *entry;
692 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
693 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
695 entry->flags & CTL_FLAG_DATA_MASK;
700 /* Performed on the Originating SC, XFER mode only */
701 case CTL_MSG_DATAMOVE: {
702 struct ctl_sg_entry *sgl;
705 io = msg_info.hdr.original_sc;
707 printf("%s: original_sc == NULL!\n", __func__);
708 /* XXX KDM do something here */
711 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
712 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
714 * Keep track of this, we need to send it back over
715 * when the datamove is complete.
717 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
719 if (msg_info.dt.sg_sequence == 0) {
721 * XXX KDM we use the preallocated S/G list
722 * here, but we'll need to change this to
723 * dynamic allocation if we need larger S/G
726 if (msg_info.dt.kern_sg_entries >
727 sizeof(io->io_hdr.remote_sglist) /
728 sizeof(io->io_hdr.remote_sglist[0])) {
729 printf("%s: number of S/G entries "
730 "needed %u > allocated num %zd\n",
732 msg_info.dt.kern_sg_entries,
733 sizeof(io->io_hdr.remote_sglist)/
734 sizeof(io->io_hdr.remote_sglist[0]));
737 * XXX KDM send a message back to
738 * the other side to shut down the
739 * DMA. The error will come back
740 * through via the normal channel.
744 sgl = io->io_hdr.remote_sglist;
746 sizeof(io->io_hdr.remote_sglist));
748 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
750 io->scsiio.kern_sg_entries =
751 msg_info.dt.kern_sg_entries;
752 io->scsiio.rem_sg_entries =
753 msg_info.dt.kern_sg_entries;
754 io->scsiio.kern_data_len =
755 msg_info.dt.kern_data_len;
756 io->scsiio.kern_total_len =
757 msg_info.dt.kern_total_len;
758 io->scsiio.kern_data_resid =
759 msg_info.dt.kern_data_resid;
760 io->scsiio.kern_rel_offset =
761 msg_info.dt.kern_rel_offset;
763 * Clear out per-DMA flags.
765 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK;
767 * Add per-DMA flags that are set for this
768 * particular DMA request.
770 io->io_hdr.flags |= msg_info.dt.flags &
773 sgl = (struct ctl_sg_entry *)
774 io->scsiio.kern_data_ptr;
776 for (i = msg_info.dt.sent_sg_entries, j = 0;
777 i < (msg_info.dt.sent_sg_entries +
778 msg_info.dt.cur_sg_entries); i++, j++) {
779 sgl[i].addr = msg_info.dt.sg_list[j].addr;
780 sgl[i].len = msg_info.dt.sg_list[j].len;
783 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n",
785 msg_info.dt.sg_list[j].addr,
786 msg_info.dt.sg_list[j].len,
787 sgl[i].addr, sgl[i].len, j, i);
791 memcpy(&sgl[msg_info.dt.sent_sg_entries],
793 sizeof(*sgl) * msg_info.dt.cur_sg_entries);
797 * If this is the last piece of the I/O, we've got
798 * the full S/G list. Queue processing in the thread.
799 * Otherwise wait for the next piece.
801 if (msg_info.dt.sg_last != 0)
805 /* Performed on the Serializing (primary) SC, XFER mode only */
806 case CTL_MSG_DATAMOVE_DONE: {
807 if (msg_info.hdr.serializing_sc == NULL) {
808 printf("%s: serializing_sc == NULL!\n",
810 /* XXX KDM now what? */
814 * We grab the sense information here in case
815 * there was a failure, so we can return status
816 * back to the initiator.
818 io = msg_info.hdr.serializing_sc;
819 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
820 io->io_hdr.status = msg_info.hdr.status;
821 io->scsiio.scsi_status = msg_info.scsi.scsi_status;
822 io->scsiio.sense_len = msg_info.scsi.sense_len;
823 io->scsiio.sense_residual =msg_info.scsi.sense_residual;
824 io->io_hdr.port_status = msg_info.scsi.fetd_status;
825 io->scsiio.residual = msg_info.scsi.residual;
826 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data,
827 sizeof(io->scsiio.sense_data));
832 /* Preformed on Originating SC, SER_ONLY mode */
834 io = msg_info.hdr.original_sc;
836 printf("%s: Major Bummer\n", __func__);
840 printf("pOrig %x\n",(int) ctsio);
843 io->io_hdr.msg_type = CTL_MSG_R2R;
844 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
849 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
851 * Performed on the Originating (i.e. secondary) SC in XFER
854 case CTL_MSG_FINISH_IO:
855 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER)
856 ctl_isc_handler_finish_xfer(ctl_softc,
859 ctl_isc_handler_finish_ser_only(ctl_softc,
863 /* Preformed on Originating SC */
864 case CTL_MSG_BAD_JUJU:
865 io = msg_info.hdr.original_sc;
867 printf("%s: Bad JUJU!, original_sc is NULL!\n",
871 ctl_copy_sense_data(&msg_info, io);
873 * IO should have already been cleaned up on other
874 * SC so clear this flag so we won't send a message
875 * back to finish the IO there.
877 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
878 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
880 /* io = msg_info.hdr.serializing_sc; */
881 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
885 /* Handle resets sent from the other side */
886 case CTL_MSG_MANAGE_TASKS: {
887 struct ctl_taskio *taskio;
888 taskio = (struct ctl_taskio *)ctl_alloc_io_nowait(
889 ctl_softc->othersc_pool);
890 if (taskio == NULL) {
891 printf("ctl_isc_event_handler: can't allocate "
894 /* should I just call the proper reset func
898 ctl_zero_io((union ctl_io *)taskio);
899 taskio->io_hdr.io_type = CTL_IO_TASK;
900 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
901 taskio->io_hdr.nexus = msg_info.hdr.nexus;
902 taskio->task_action = msg_info.task.task_action;
903 taskio->tag_num = msg_info.task.tag_num;
904 taskio->tag_type = msg_info.task.tag_type;
906 taskio->io_hdr.start_time = time_uptime;
907 getbintime(&taskio->io_hdr.start_bt);
909 cs_prof_gettime(&taskio->io_hdr.start_ticks);
911 #endif /* CTL_TIME_IO */
912 ctl_run_task((union ctl_io *)taskio);
915 /* Persistent Reserve action which needs attention */
916 case CTL_MSG_PERS_ACTION:
917 presio = (struct ctl_prio *)ctl_alloc_io_nowait(
918 ctl_softc->othersc_pool);
919 if (presio == NULL) {
920 printf("ctl_isc_event_handler: can't allocate "
923 /* Need to set busy and send msg back */
926 ctl_zero_io((union ctl_io *)presio);
927 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
928 presio->pr_msg = msg_info.pr;
929 ctl_enqueue_isc((union ctl_io *)presio);
931 case CTL_MSG_SYNC_FE:
935 printf("How did I get here?\n");
937 } else if (event == CTL_HA_EVT_MSG_SENT) {
938 if (param != CTL_HA_STATUS_SUCCESS) {
939 printf("Bad status from ctl_ha_msg_send status %d\n",
943 } else if (event == CTL_HA_EVT_DISCONNECT) {
944 printf("CTL: Got a disconnect from Isc\n");
947 printf("ctl_isc_event_handler: Unknown event %d\n", event);
956 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
958 struct scsi_sense_data *sense;
960 sense = &dest->scsiio.sense_data;
961 bcopy(&src->scsi.sense_data, sense, sizeof(*sense));
962 dest->scsiio.scsi_status = src->scsi.scsi_status;
963 dest->scsiio.sense_len = src->scsi.sense_len;
964 dest->io_hdr.status = src->hdr.status;
968 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
972 mtx_assert(&lun->lun_lock, MA_OWNED);
973 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
976 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua;
980 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
984 mtx_assert(&lun->lun_lock, MA_OWNED);
985 for (i = 0; i < CTL_MAX_PORTS; i++) {
986 if (lun->pending_ua[i] == NULL)
988 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
989 if (i * CTL_MAX_INIT_PER_PORT + j == except)
991 lun->pending_ua[i][j] |= ua;
997 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1001 mtx_assert(&lun->lun_lock, MA_OWNED);
1002 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1005 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua;
1009 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1013 mtx_assert(&lun->lun_lock, MA_OWNED);
1014 for (i = 0; i < CTL_MAX_PORTS; i++) {
1015 if (lun->pending_ua[i] == NULL)
1017 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
1018 if (i * CTL_MAX_INIT_PER_PORT + j == except)
1020 lun->pending_ua[i][j] &= ~ua;
1026 ctl_ha_state_sysctl(SYSCTL_HANDLER_ARGS)
1028 struct ctl_softc *softc = (struct ctl_softc *)arg1;
1029 struct ctl_lun *lun;
1032 if (softc->flags & CTL_FLAG_ACTIVE_SHELF)
1037 error = sysctl_handle_int(oidp, &value, 0, req);
1038 if ((error != 0) || (req->newptr == NULL))
1041 mtx_lock(&softc->ctl_lock);
1043 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1045 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
1046 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1047 mtx_lock(&lun->lun_lock);
1048 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1049 mtx_unlock(&lun->lun_lock);
1051 mtx_unlock(&softc->ctl_lock);
1058 struct ctl_softc *softc;
1060 struct ctl_port *port;
1061 int i, error, retval;
1068 control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1070 softc = control_softc;
1072 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
1075 softc->dev->si_drv1 = softc;
1078 * By default, return a "bad LUN" peripheral qualifier for unknown
1079 * LUNs. The user can override this default using the tunable or
1080 * sysctl. See the comment in ctl_inquiry_std() for more details.
1082 softc->inquiry_pq_no_lun = 1;
1083 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun",
1084 &softc->inquiry_pq_no_lun);
1085 sysctl_ctx_init(&softc->sysctl_ctx);
1086 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1087 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1088 CTLFLAG_RD, 0, "CAM Target Layer");
1090 if (softc->sysctl_tree == NULL) {
1091 printf("%s: unable to allocate sysctl tree\n", __func__);
1092 destroy_dev(softc->dev);
1093 free(control_softc, M_DEVBUF);
1094 control_softc = NULL;
1098 SYSCTL_ADD_INT(&softc->sysctl_ctx,
1099 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
1100 "inquiry_pq_no_lun", CTLFLAG_RW,
1101 &softc->inquiry_pq_no_lun, 0,
1102 "Report no lun possible for invalid LUNs");
1104 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1105 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
1106 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1107 softc->open_count = 0;
1110 * Default to actually sending a SYNCHRONIZE CACHE command down to
1113 softc->flags = CTL_FLAG_REAL_SYNC;
1116 * In Copan's HA scheme, the "master" and "slave" roles are
1117 * figured out through the slot the controller is in. Although it
1118 * is an active/active system, someone has to be in charge.
1120 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1121 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1122 "HA head ID (0 - no HA)");
1123 if (softc->ha_id == 0) {
1124 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1125 softc->is_single = 1;
1126 softc->port_offset = 0;
1128 softc->port_offset = (softc->ha_id - 1) * CTL_MAX_PORTS;
1129 softc->persis_offset = softc->port_offset * CTL_MAX_INIT_PER_PORT;
1132 * XXX KDM need to figure out where we want to get our target ID
1133 * and WWID. Is it different on each port?
1135 softc->target.id = 0;
1136 softc->target.wwid[0] = 0x12345678;
1137 softc->target.wwid[1] = 0x87654321;
1138 STAILQ_INIT(&softc->lun_list);
1139 STAILQ_INIT(&softc->pending_lun_queue);
1140 STAILQ_INIT(&softc->fe_list);
1141 STAILQ_INIT(&softc->port_list);
1142 STAILQ_INIT(&softc->be_list);
1143 ctl_tpc_init(softc);
1145 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
1148 printf("ctl: can't allocate %d entry other SC pool, "
1149 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1152 softc->othersc_pool = other_pool;
1154 if (worker_threads <= 0)
1155 worker_threads = max(1, mp_ncpus / 4);
1156 if (worker_threads > CTL_MAX_THREADS)
1157 worker_threads = CTL_MAX_THREADS;
1159 for (i = 0; i < worker_threads; i++) {
1160 struct ctl_thread *thr = &softc->threads[i];
1162 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1163 thr->ctl_softc = softc;
1164 STAILQ_INIT(&thr->incoming_queue);
1165 STAILQ_INIT(&thr->rtr_queue);
1166 STAILQ_INIT(&thr->done_queue);
1167 STAILQ_INIT(&thr->isc_queue);
1169 error = kproc_kthread_add(ctl_work_thread, thr,
1170 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1172 printf("error creating CTL work thread!\n");
1173 ctl_pool_free(other_pool);
1177 error = kproc_kthread_add(ctl_lun_thread, softc,
1178 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
1180 printf("error creating CTL lun thread!\n");
1181 ctl_pool_free(other_pool);
1184 error = kproc_kthread_add(ctl_thresh_thread, softc,
1185 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh");
1187 printf("error creating CTL threshold thread!\n");
1188 ctl_pool_free(other_pool);
1192 printf("ctl: CAM Target Layer loaded\n");
1195 * Initialize the ioctl front end.
1197 ctl_frontend_register(&ioctl_frontend);
1198 port = &softc->ioctl_info.port;
1199 port->frontend = &ioctl_frontend;
1200 sprintf(softc->ioctl_info.port_name, "ioctl");
1201 port->port_type = CTL_PORT_IOCTL;
1202 port->num_requested_ctl_io = 100;
1203 port->port_name = softc->ioctl_info.port_name;
1204 port->port_online = ctl_ioctl_online;
1205 port->port_offline = ctl_ioctl_offline;
1206 port->onoff_arg = &softc->ioctl_info;
1207 port->lun_enable = ctl_ioctl_lun_enable;
1208 port->lun_disable = ctl_ioctl_lun_disable;
1209 port->targ_lun_arg = &softc->ioctl_info;
1210 port->fe_datamove = ctl_ioctl_datamove;
1211 port->fe_done = ctl_ioctl_done;
1212 port->max_targets = 15;
1213 port->max_target_id = 15;
1215 if (ctl_port_register(&softc->ioctl_info.port) != 0) {
1216 printf("ctl: ioctl front end registration failed, will "
1217 "continue anyway\n");
1220 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1221 OID_AUTO, "ha_state", CTLTYPE_INT | CTLFLAG_RWTUN,
1222 softc, 0, ctl_ha_state_sysctl, "I", "HA state for this head");
1225 if (sizeof(struct callout) > CTL_TIMER_BYTES) {
1226 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n",
1227 sizeof(struct callout), CTL_TIMER_BYTES);
1230 #endif /* CTL_IO_DELAY */
1238 struct ctl_softc *softc;
1239 struct ctl_lun *lun, *next_lun;
1241 softc = (struct ctl_softc *)control_softc;
1243 if (ctl_port_deregister(&softc->ioctl_info.port) != 0)
1244 printf("ctl: ioctl front end deregistration failed\n");
1246 mtx_lock(&softc->ctl_lock);
1251 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
1252 next_lun = STAILQ_NEXT(lun, links);
1256 mtx_unlock(&softc->ctl_lock);
1258 ctl_frontend_deregister(&ioctl_frontend);
1261 ctl_shutdown_thread(softc->work_thread);
1262 mtx_destroy(&softc->queue_lock);
1265 ctl_tpc_shutdown(softc);
1266 uma_zdestroy(softc->io_zone);
1267 mtx_destroy(&softc->ctl_lock);
1269 destroy_dev(softc->dev);
1271 sysctl_ctx_free(&softc->sysctl_ctx);
1273 free(control_softc, M_DEVBUF);
1274 control_softc = NULL;
1277 printf("ctl: CAM Target Layer unloaded\n");
1281 ctl_module_event_handler(module_t mod, int what, void *arg)
1286 return (ctl_init());
1290 return (EOPNOTSUPP);
1295 * XXX KDM should we do some access checks here? Bump a reference count to
1296 * prevent a CTL module from being unloaded while someone has it open?
1299 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1305 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1311 ctl_port_enable(ctl_port_type port_type)
1313 struct ctl_softc *softc = control_softc;
1314 struct ctl_port *port;
1316 if (softc->is_single == 0) {
1317 union ctl_ha_msg msg_info;
1321 printf("%s: HA mode, synchronizing frontend enable\n",
1324 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE;
1325 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1326 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) {
1327 printf("Sync msg send error retval %d\n", isc_retval);
1329 if (!rcv_sync_msg) {
1330 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
1331 sizeof(msg_info), 1);
1334 printf("CTL:Frontend Enable\n");
1336 printf("%s: single mode, skipping frontend synchronization\n",
1341 STAILQ_FOREACH(port, &softc->port_list, links) {
1342 if (port_type & port->port_type)
1345 printf("port %d\n", port->targ_port);
1347 ctl_port_online(port);
1355 ctl_port_disable(ctl_port_type port_type)
1357 struct ctl_softc *softc;
1358 struct ctl_port *port;
1360 softc = control_softc;
1362 STAILQ_FOREACH(port, &softc->port_list, links) {
1363 if (port_type & port->port_type)
1364 ctl_port_offline(port);
1371 * Returns 0 for success, 1 for failure.
1372 * Currently the only failure mode is if there aren't enough entries
1373 * allocated. So, in case of a failure, look at num_entries_dropped,
1374 * reallocate and try again.
1377 ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced,
1378 int *num_entries_filled, int *num_entries_dropped,
1379 ctl_port_type port_type, int no_virtual)
1381 struct ctl_softc *softc;
1382 struct ctl_port *port;
1383 int entries_dropped, entries_filled;
1387 softc = control_softc;
1391 entries_dropped = 0;
1394 mtx_lock(&softc->ctl_lock);
1395 STAILQ_FOREACH(port, &softc->port_list, links) {
1396 struct ctl_port_entry *entry;
1398 if ((port->port_type & port_type) == 0)
1401 if ((no_virtual != 0)
1402 && (port->virtual_port != 0))
1405 if (entries_filled >= num_entries_alloced) {
1409 entry = &entries[i];
1411 entry->port_type = port->port_type;
1412 strlcpy(entry->port_name, port->port_name,
1413 sizeof(entry->port_name));
1414 entry->physical_port = port->physical_port;
1415 entry->virtual_port = port->virtual_port;
1416 entry->wwnn = port->wwnn;
1417 entry->wwpn = port->wwpn;
1423 mtx_unlock(&softc->ctl_lock);
1425 if (entries_dropped > 0)
1428 *num_entries_dropped = entries_dropped;
1429 *num_entries_filled = entries_filled;
1435 ctl_ioctl_online(void *arg)
1437 struct ctl_ioctl_info *ioctl_info;
1439 ioctl_info = (struct ctl_ioctl_info *)arg;
1441 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED;
1445 ctl_ioctl_offline(void *arg)
1447 struct ctl_ioctl_info *ioctl_info;
1449 ioctl_info = (struct ctl_ioctl_info *)arg;
1451 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED;
1455 * Remove an initiator by port number and initiator ID.
1456 * Returns 0 for success, -1 for failure.
1459 ctl_remove_initiator(struct ctl_port *port, int iid)
1461 struct ctl_softc *softc = control_softc;
1463 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1465 if (iid > CTL_MAX_INIT_PER_PORT) {
1466 printf("%s: initiator ID %u > maximun %u!\n",
1467 __func__, iid, CTL_MAX_INIT_PER_PORT);
1471 mtx_lock(&softc->ctl_lock);
1472 port->wwpn_iid[iid].in_use--;
1473 port->wwpn_iid[iid].last_use = time_uptime;
1474 mtx_unlock(&softc->ctl_lock);
1480 * Add an initiator to the initiator map.
1481 * Returns iid for success, < 0 for failure.
1484 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
1486 struct ctl_softc *softc = control_softc;
1490 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1492 if (iid >= CTL_MAX_INIT_PER_PORT) {
1493 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
1494 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
1499 mtx_lock(&softc->ctl_lock);
1501 if (iid < 0 && (wwpn != 0 || name != NULL)) {
1502 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1503 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
1507 if (name != NULL && port->wwpn_iid[i].name != NULL &&
1508 strcmp(name, port->wwpn_iid[i].name) == 0) {
1516 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1517 if (port->wwpn_iid[i].in_use == 0 &&
1518 port->wwpn_iid[i].wwpn == 0 &&
1519 port->wwpn_iid[i].name == NULL) {
1528 best_time = INT32_MAX;
1529 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1530 if (port->wwpn_iid[i].in_use == 0) {
1531 if (port->wwpn_iid[i].last_use < best_time) {
1533 best_time = port->wwpn_iid[i].last_use;
1541 mtx_unlock(&softc->ctl_lock);
1546 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
1548 * This is not an error yet.
1550 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
1552 printf("%s: port %d iid %u WWPN %#jx arrived"
1553 " again\n", __func__, port->targ_port,
1554 iid, (uintmax_t)wwpn);
1558 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
1559 strcmp(name, port->wwpn_iid[iid].name) == 0) {
1561 printf("%s: port %d iid %u name '%s' arrived"
1562 " again\n", __func__, port->targ_port,
1569 * This is an error, but what do we do about it? The
1570 * driver is telling us we have a new WWPN for this
1571 * initiator ID, so we pretty much need to use it.
1573 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
1574 " but WWPN %#jx '%s' is still at that address\n",
1575 __func__, port->targ_port, iid, wwpn, name,
1576 (uintmax_t)port->wwpn_iid[iid].wwpn,
1577 port->wwpn_iid[iid].name);
1580 * XXX KDM clear have_ca and ua_pending on each LUN for
1585 free(port->wwpn_iid[iid].name, M_CTL);
1586 port->wwpn_iid[iid].name = name;
1587 port->wwpn_iid[iid].wwpn = wwpn;
1588 port->wwpn_iid[iid].in_use++;
1589 mtx_unlock(&softc->ctl_lock);
1595 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
1599 switch (port->port_type) {
1602 struct scsi_transportid_fcp *id =
1603 (struct scsi_transportid_fcp *)buf;
1604 if (port->wwpn_iid[iid].wwpn == 0)
1606 memset(id, 0, sizeof(*id));
1607 id->format_protocol = SCSI_PROTO_FC;
1608 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
1609 return (sizeof(*id));
1611 case CTL_PORT_ISCSI:
1613 struct scsi_transportid_iscsi_port *id =
1614 (struct scsi_transportid_iscsi_port *)buf;
1615 if (port->wwpn_iid[iid].name == NULL)
1618 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
1620 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
1621 len = roundup2(min(len, 252), 4);
1622 scsi_ulto2b(len, id->additional_length);
1623 return (sizeof(*id) + len);
1627 struct scsi_transportid_sas *id =
1628 (struct scsi_transportid_sas *)buf;
1629 if (port->wwpn_iid[iid].wwpn == 0)
1631 memset(id, 0, sizeof(*id));
1632 id->format_protocol = SCSI_PROTO_SAS;
1633 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
1634 return (sizeof(*id));
1638 struct scsi_transportid_spi *id =
1639 (struct scsi_transportid_spi *)buf;
1640 memset(id, 0, sizeof(*id));
1641 id->format_protocol = SCSI_PROTO_SPI;
1642 scsi_ulto2b(iid, id->scsi_addr);
1643 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
1644 return (sizeof(*id));
1650 ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id)
1656 ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id)
1662 * Data movement routine for the CTL ioctl frontend port.
1665 ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio)
1667 struct ctl_sg_entry *ext_sglist, *kern_sglist;
1668 struct ctl_sg_entry ext_entry, kern_entry;
1669 int ext_sglen, ext_sg_entries, kern_sg_entries;
1670 int ext_sg_start, ext_offset;
1671 int len_to_copy, len_copied;
1672 int kern_watermark, ext_watermark;
1673 int ext_sglist_malloced;
1676 ext_sglist_malloced = 0;
1680 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n"));
1683 * If this flag is set, fake the data transfer.
1685 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) {
1686 ctsio->ext_data_filled = ctsio->ext_data_len;
1691 * To simplify things here, if we have a single buffer, stick it in
1692 * a S/G entry and just make it a single entry S/G list.
1694 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) {
1697 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist);
1699 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL,
1701 ext_sglist_malloced = 1;
1702 if (copyin(ctsio->ext_data_ptr, ext_sglist,
1704 ctl_set_internal_failure(ctsio,
1709 ext_sg_entries = ctsio->ext_sg_entries;
1711 for (i = 0; i < ext_sg_entries; i++) {
1712 if ((len_seen + ext_sglist[i].len) >=
1713 ctsio->ext_data_filled) {
1715 ext_offset = ctsio->ext_data_filled - len_seen;
1718 len_seen += ext_sglist[i].len;
1721 ext_sglist = &ext_entry;
1722 ext_sglist->addr = ctsio->ext_data_ptr;
1723 ext_sglist->len = ctsio->ext_data_len;
1726 ext_offset = ctsio->ext_data_filled;
1729 if (ctsio->kern_sg_entries > 0) {
1730 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
1731 kern_sg_entries = ctsio->kern_sg_entries;
1733 kern_sglist = &kern_entry;
1734 kern_sglist->addr = ctsio->kern_data_ptr;
1735 kern_sglist->len = ctsio->kern_data_len;
1736 kern_sg_entries = 1;
1741 ext_watermark = ext_offset;
1743 for (i = ext_sg_start, j = 0;
1744 i < ext_sg_entries && j < kern_sg_entries;) {
1745 uint8_t *ext_ptr, *kern_ptr;
1747 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark,
1748 kern_sglist[j].len - kern_watermark);
1750 ext_ptr = (uint8_t *)ext_sglist[i].addr;
1751 ext_ptr = ext_ptr + ext_watermark;
1752 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
1756 panic("need to implement bus address support");
1758 kern_ptr = bus_to_virt(kern_sglist[j].addr);
1761 kern_ptr = (uint8_t *)kern_sglist[j].addr;
1762 kern_ptr = kern_ptr + kern_watermark;
1764 kern_watermark += len_to_copy;
1765 ext_watermark += len_to_copy;
1767 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
1769 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1770 "bytes to user\n", len_to_copy));
1771 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1772 "to %p\n", kern_ptr, ext_ptr));
1773 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) {
1774 ctl_set_internal_failure(ctsio,
1780 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1781 "bytes from user\n", len_to_copy));
1782 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1783 "to %p\n", ext_ptr, kern_ptr));
1784 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){
1785 ctl_set_internal_failure(ctsio,
1792 len_copied += len_to_copy;
1794 if (ext_sglist[i].len == ext_watermark) {
1799 if (kern_sglist[j].len == kern_watermark) {
1805 ctsio->ext_data_filled += len_copied;
1807 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, "
1808 "kern_sg_entries: %d\n", ext_sg_entries,
1810 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, "
1811 "kern_data_len = %d\n", ctsio->ext_data_len,
1812 ctsio->kern_data_len));
1815 /* XXX KDM set residual?? */
1818 if (ext_sglist_malloced != 0)
1819 free(ext_sglist, M_CTL);
1821 return (CTL_RETVAL_COMPLETE);
1825 * Serialize a command that went down the "wrong" side, and so was sent to
1826 * this controller for execution. The logic is a little different than the
1827 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
1828 * sent back to the other side, but in the success case, we execute the
1829 * command on this side (XFER mode) or tell the other side to execute it
1833 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
1835 struct ctl_softc *ctl_softc;
1836 union ctl_ha_msg msg_info;
1837 struct ctl_lun *lun;
1841 ctl_softc = control_softc;
1843 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
1844 lun = ctl_softc->ctl_luns[targ_lun];
1848 * Why isn't LUN defined? The other side wouldn't
1849 * send a cmd if the LUN is undefined.
1851 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__);
1853 /* "Logical unit not supported" */
1854 ctl_set_sense_data(&msg_info.scsi.sense_data,
1856 /*sense_format*/SSD_TYPE_NONE,
1857 /*current_error*/ 1,
1858 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1863 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1864 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1865 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1866 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1867 msg_info.hdr.serializing_sc = NULL;
1868 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1869 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1870 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1876 mtx_lock(&lun->lun_lock);
1877 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1879 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
1880 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
1882 case CTL_ACTION_BLOCK:
1883 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
1884 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
1887 case CTL_ACTION_PASS:
1888 case CTL_ACTION_SKIP:
1889 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
1890 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
1891 ctl_enqueue_rtr((union ctl_io *)ctsio);
1894 /* send msg back to other side */
1895 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1896 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
1897 msg_info.hdr.msg_type = CTL_MSG_R2R;
1899 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc);
1901 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1902 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1906 case CTL_ACTION_OVERLAP:
1907 /* OVERLAPPED COMMANDS ATTEMPTED */
1908 ctl_set_sense_data(&msg_info.scsi.sense_data,
1910 /*sense_format*/SSD_TYPE_NONE,
1911 /*current_error*/ 1,
1912 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1917 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1918 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1919 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1920 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1921 msg_info.hdr.serializing_sc = NULL;
1922 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1924 printf("BAD JUJU:Major Bummer Overlap\n");
1926 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1928 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1929 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1932 case CTL_ACTION_OVERLAP_TAG:
1933 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */
1934 ctl_set_sense_data(&msg_info.scsi.sense_data,
1936 /*sense_format*/SSD_TYPE_NONE,
1937 /*current_error*/ 1,
1938 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1940 /*ascq*/ ctsio->tag_num & 0xff,
1943 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1944 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1945 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1946 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1947 msg_info.hdr.serializing_sc = NULL;
1948 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1950 printf("BAD JUJU:Major Bummer Overlap Tag\n");
1952 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1954 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1955 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1958 case CTL_ACTION_ERROR:
1960 /* "Internal target failure" */
1961 ctl_set_sense_data(&msg_info.scsi.sense_data,
1963 /*sense_format*/SSD_TYPE_NONE,
1964 /*current_error*/ 1,
1965 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
1970 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1971 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1972 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1973 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1974 msg_info.hdr.serializing_sc = NULL;
1975 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1977 printf("BAD JUJU:Major Bummer HW Error\n");
1979 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1981 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1982 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1986 mtx_unlock(&lun->lun_lock);
1991 ctl_ioctl_submit_wait(union ctl_io *io)
1993 struct ctl_fe_ioctl_params params;
1994 ctl_fe_ioctl_state last_state;
1999 bzero(¶ms, sizeof(params));
2001 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF);
2002 cv_init(¶ms.sem, "ctlioccv");
2003 params.state = CTL_IOCTL_INPROG;
2004 last_state = params.state;
2006 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms;
2008 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n"));
2010 /* This shouldn't happen */
2011 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE)
2017 mtx_lock(¶ms.ioctl_mtx);
2019 * Check the state here, and don't sleep if the state has
2020 * already changed (i.e. wakeup has already occured, but we
2021 * weren't waiting yet).
2023 if (params.state == last_state) {
2024 /* XXX KDM cv_wait_sig instead? */
2025 cv_wait(¶ms.sem, ¶ms.ioctl_mtx);
2027 last_state = params.state;
2029 switch (params.state) {
2030 case CTL_IOCTL_INPROG:
2031 /* Why did we wake up? */
2032 /* XXX KDM error here? */
2033 mtx_unlock(¶ms.ioctl_mtx);
2035 case CTL_IOCTL_DATAMOVE:
2036 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n"));
2039 * change last_state back to INPROG to avoid
2040 * deadlock on subsequent data moves.
2042 params.state = last_state = CTL_IOCTL_INPROG;
2044 mtx_unlock(¶ms.ioctl_mtx);
2045 ctl_ioctl_do_datamove(&io->scsiio);
2047 * Note that in some cases, most notably writes,
2048 * this will queue the I/O and call us back later.
2049 * In other cases, generally reads, this routine
2050 * will immediately call back and wake us up,
2051 * probably using our own context.
2053 io->scsiio.be_move_done(io);
2055 case CTL_IOCTL_DONE:
2056 mtx_unlock(¶ms.ioctl_mtx);
2057 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n"));
2061 mtx_unlock(¶ms.ioctl_mtx);
2062 /* XXX KDM error here? */
2065 } while (done == 0);
2067 mtx_destroy(¶ms.ioctl_mtx);
2068 cv_destroy(¶ms.sem);
2070 return (CTL_RETVAL_COMPLETE);
2074 ctl_ioctl_datamove(union ctl_io *io)
2076 struct ctl_fe_ioctl_params *params;
2078 params = (struct ctl_fe_ioctl_params *)
2079 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2081 mtx_lock(¶ms->ioctl_mtx);
2082 params->state = CTL_IOCTL_DATAMOVE;
2083 cv_broadcast(¶ms->sem);
2084 mtx_unlock(¶ms->ioctl_mtx);
2088 ctl_ioctl_done(union ctl_io *io)
2090 struct ctl_fe_ioctl_params *params;
2092 params = (struct ctl_fe_ioctl_params *)
2093 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2095 mtx_lock(¶ms->ioctl_mtx);
2096 params->state = CTL_IOCTL_DONE;
2097 cv_broadcast(¶ms->sem);
2098 mtx_unlock(¶ms->ioctl_mtx);
2102 ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask)
2104 struct ctl_fe_ioctl_startstop_info *sd_info;
2106 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg;
2108 sd_info->hs_info.status = metatask->status;
2109 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns;
2110 sd_info->hs_info.luns_complete =
2111 metatask->taskinfo.startstop.luns_complete;
2112 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed;
2114 cv_broadcast(&sd_info->sem);
2118 ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask)
2120 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info;
2122 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg;
2124 mtx_lock(fe_bbr_info->lock);
2125 fe_bbr_info->bbr_info->status = metatask->status;
2126 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2127 fe_bbr_info->wakeup_done = 1;
2128 mtx_unlock(fe_bbr_info->lock);
2130 cv_broadcast(&fe_bbr_info->sem);
2134 * Returns 0 for success, errno for failure.
2137 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2138 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2145 mtx_lock(&lun->lun_lock);
2146 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2147 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2149 struct ctl_ooa_entry *entry;
2152 * If we've got more than we can fit, just count the
2153 * remaining entries.
2155 if (*cur_fill_num >= ooa_hdr->alloc_num)
2158 entry = &kern_entries[*cur_fill_num];
2160 entry->tag_num = io->scsiio.tag_num;
2161 entry->lun_num = lun->lun;
2163 entry->start_bt = io->io_hdr.start_bt;
2165 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2166 entry->cdb_len = io->scsiio.cdb_len;
2167 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2168 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2170 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2171 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2173 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2174 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2176 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2177 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2179 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2180 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2182 mtx_unlock(&lun->lun_lock);
2188 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2189 size_t error_str_len)
2193 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2195 if (copyin(user_addr, kptr, len) != 0) {
2196 snprintf(error_str, error_str_len, "Error copying %d bytes "
2197 "from user address %p to kernel address %p", len,
2207 ctl_free_args(int num_args, struct ctl_be_arg *args)
2214 for (i = 0; i < num_args; i++) {
2215 free(args[i].kname, M_CTL);
2216 free(args[i].kvalue, M_CTL);
2222 static struct ctl_be_arg *
2223 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
2224 char *error_str, size_t error_str_len)
2226 struct ctl_be_arg *args;
2229 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
2230 error_str, error_str_len);
2235 for (i = 0; i < num_args; i++) {
2236 args[i].kname = NULL;
2237 args[i].kvalue = NULL;
2240 for (i = 0; i < num_args; i++) {
2243 args[i].kname = ctl_copyin_alloc(args[i].name,
2244 args[i].namelen, error_str, error_str_len);
2245 if (args[i].kname == NULL)
2248 if (args[i].kname[args[i].namelen - 1] != '\0') {
2249 snprintf(error_str, error_str_len, "Argument %d "
2250 "name is not NUL-terminated", i);
2254 if (args[i].flags & CTL_BEARG_RD) {
2255 tmpptr = ctl_copyin_alloc(args[i].value,
2256 args[i].vallen, error_str, error_str_len);
2259 if ((args[i].flags & CTL_BEARG_ASCII)
2260 && (tmpptr[args[i].vallen - 1] != '\0')) {
2261 snprintf(error_str, error_str_len, "Argument "
2262 "%d value is not NUL-terminated", i);
2265 args[i].kvalue = tmpptr;
2267 args[i].kvalue = malloc(args[i].vallen,
2268 M_CTL, M_WAITOK | M_ZERO);
2275 ctl_free_args(num_args, args);
2281 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2285 for (i = 0; i < num_args; i++) {
2286 if (args[i].flags & CTL_BEARG_WR)
2287 copyout(args[i].kvalue, args[i].value, args[i].vallen);
2292 * Escape characters that are illegal or not recommended in XML.
2295 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2297 char *end = str + size;
2302 for (; *str && str < end; str++) {
2305 retval = sbuf_printf(sb, "&");
2308 retval = sbuf_printf(sb, ">");
2311 retval = sbuf_printf(sb, "<");
2314 retval = sbuf_putc(sb, *str);
2327 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2329 struct scsi_vpd_id_descriptor *desc;
2332 if (id == NULL || id->len < 4)
2334 desc = (struct scsi_vpd_id_descriptor *)id->data;
2335 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2336 case SVPD_ID_TYPE_T10:
2337 sbuf_printf(sb, "t10.");
2339 case SVPD_ID_TYPE_EUI64:
2340 sbuf_printf(sb, "eui.");
2342 case SVPD_ID_TYPE_NAA:
2343 sbuf_printf(sb, "naa.");
2345 case SVPD_ID_TYPE_SCSI_NAME:
2348 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2349 case SVPD_ID_CODESET_BINARY:
2350 for (i = 0; i < desc->length; i++)
2351 sbuf_printf(sb, "%02x", desc->identifier[i]);
2353 case SVPD_ID_CODESET_ASCII:
2354 sbuf_printf(sb, "%.*s", (int)desc->length,
2355 (char *)desc->identifier);
2357 case SVPD_ID_CODESET_UTF8:
2358 sbuf_printf(sb, "%s", (char *)desc->identifier);
2364 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2367 struct ctl_softc *softc;
2370 softc = control_softc;
2380 * If we haven't been "enabled", don't allow any SCSI I/O
2383 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) {
2388 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref);
2391 * Need to save the pool reference so it doesn't get
2392 * spammed by the user's ctl_io.
2394 pool_tmp = io->io_hdr.pool;
2395 memcpy(io, (void *)addr, sizeof(*io));
2396 io->io_hdr.pool = pool_tmp;
2399 * No status yet, so make sure the status is set properly.
2401 io->io_hdr.status = CTL_STATUS_NONE;
2404 * The user sets the initiator ID, target and LUN IDs.
2406 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port;
2407 io->io_hdr.flags |= CTL_FLAG_USER_REQ;
2408 if ((io->io_hdr.io_type == CTL_IO_SCSI)
2409 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED))
2410 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++;
2412 retval = ctl_ioctl_submit_wait(io);
2419 memcpy((void *)addr, io, sizeof(*io));
2421 /* return this to our pool */
2426 case CTL_ENABLE_PORT:
2427 case CTL_DISABLE_PORT:
2428 case CTL_SET_PORT_WWNS: {
2429 struct ctl_port *port;
2430 struct ctl_port_entry *entry;
2432 entry = (struct ctl_port_entry *)addr;
2434 mtx_lock(&softc->ctl_lock);
2435 STAILQ_FOREACH(port, &softc->port_list, links) {
2441 if ((entry->port_type == CTL_PORT_NONE)
2442 && (entry->targ_port == port->targ_port)) {
2444 * If the user only wants to enable or
2445 * disable or set WWNs on a specific port,
2446 * do the operation and we're done.
2450 } else if (entry->port_type & port->port_type) {
2452 * Compare the user's type mask with the
2453 * particular frontend type to see if we
2460 * Make sure the user isn't trying to set
2461 * WWNs on multiple ports at the same time.
2463 if (cmd == CTL_SET_PORT_WWNS) {
2464 printf("%s: Can't set WWNs on "
2465 "multiple ports\n", __func__);
2472 * XXX KDM we have to drop the lock here,
2473 * because the online/offline operations
2474 * can potentially block. We need to
2475 * reference count the frontends so they
2478 mtx_unlock(&softc->ctl_lock);
2480 if (cmd == CTL_ENABLE_PORT) {
2481 struct ctl_lun *lun;
2483 STAILQ_FOREACH(lun, &softc->lun_list,
2485 port->lun_enable(port->targ_lun_arg,
2490 ctl_port_online(port);
2491 } else if (cmd == CTL_DISABLE_PORT) {
2492 struct ctl_lun *lun;
2494 ctl_port_offline(port);
2496 STAILQ_FOREACH(lun, &softc->lun_list,
2505 mtx_lock(&softc->ctl_lock);
2507 if (cmd == CTL_SET_PORT_WWNS)
2508 ctl_port_set_wwns(port,
2509 (entry->flags & CTL_PORT_WWNN_VALID) ?
2511 (entry->flags & CTL_PORT_WWPN_VALID) ?
2512 1 : 0, entry->wwpn);
2517 mtx_unlock(&softc->ctl_lock);
2520 case CTL_GET_PORT_LIST: {
2521 struct ctl_port *port;
2522 struct ctl_port_list *list;
2525 list = (struct ctl_port_list *)addr;
2527 if (list->alloc_len != (list->alloc_num *
2528 sizeof(struct ctl_port_entry))) {
2529 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != "
2530 "alloc_num %u * sizeof(struct ctl_port_entry) "
2531 "%zu\n", __func__, list->alloc_len,
2532 list->alloc_num, sizeof(struct ctl_port_entry));
2538 list->dropped_num = 0;
2540 mtx_lock(&softc->ctl_lock);
2541 STAILQ_FOREACH(port, &softc->port_list, links) {
2542 struct ctl_port_entry entry, *list_entry;
2544 if (list->fill_num >= list->alloc_num) {
2545 list->dropped_num++;
2549 entry.port_type = port->port_type;
2550 strlcpy(entry.port_name, port->port_name,
2551 sizeof(entry.port_name));
2552 entry.targ_port = port->targ_port;
2553 entry.physical_port = port->physical_port;
2554 entry.virtual_port = port->virtual_port;
2555 entry.wwnn = port->wwnn;
2556 entry.wwpn = port->wwpn;
2557 if (port->status & CTL_PORT_STATUS_ONLINE)
2562 list_entry = &list->entries[i];
2564 retval = copyout(&entry, list_entry, sizeof(entry));
2566 printf("%s: CTL_GET_PORT_LIST: copyout "
2567 "returned %d\n", __func__, retval);
2572 list->fill_len += sizeof(entry);
2574 mtx_unlock(&softc->ctl_lock);
2577 * If this is non-zero, we had a copyout fault, so there's
2578 * probably no point in attempting to set the status inside
2584 if (list->dropped_num > 0)
2585 list->status = CTL_PORT_LIST_NEED_MORE_SPACE;
2587 list->status = CTL_PORT_LIST_OK;
2590 case CTL_DUMP_OOA: {
2591 struct ctl_lun *lun;
2596 mtx_lock(&softc->ctl_lock);
2597 printf("Dumping OOA queues:\n");
2598 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2599 mtx_lock(&lun->lun_lock);
2600 for (io = (union ctl_io *)TAILQ_FIRST(
2601 &lun->ooa_queue); io != NULL;
2602 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2604 sbuf_new(&sb, printbuf, sizeof(printbuf),
2606 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ",
2610 CTL_FLAG_BLOCKED) ? "" : " BLOCKED",
2612 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
2614 CTL_FLAG_ABORT) ? " ABORT" : "",
2616 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : "");
2617 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
2619 printf("%s\n", sbuf_data(&sb));
2621 mtx_unlock(&lun->lun_lock);
2623 printf("OOA queues dump done\n");
2624 mtx_unlock(&softc->ctl_lock);
2628 struct ctl_lun *lun;
2629 struct ctl_ooa *ooa_hdr;
2630 struct ctl_ooa_entry *entries;
2631 uint32_t cur_fill_num;
2633 ooa_hdr = (struct ctl_ooa *)addr;
2635 if ((ooa_hdr->alloc_len == 0)
2636 || (ooa_hdr->alloc_num == 0)) {
2637 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2638 "must be non-zero\n", __func__,
2639 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2644 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2645 sizeof(struct ctl_ooa_entry))) {
2646 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2647 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2648 __func__, ooa_hdr->alloc_len,
2649 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2654 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2655 if (entries == NULL) {
2656 printf("%s: could not allocate %d bytes for OOA "
2657 "dump\n", __func__, ooa_hdr->alloc_len);
2662 mtx_lock(&softc->ctl_lock);
2663 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2664 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS)
2665 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2666 mtx_unlock(&softc->ctl_lock);
2667 free(entries, M_CTL);
2668 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2669 __func__, (uintmax_t)ooa_hdr->lun_num);
2676 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2677 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2678 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2684 mtx_unlock(&softc->ctl_lock);
2685 free(entries, M_CTL);
2689 lun = softc->ctl_luns[ooa_hdr->lun_num];
2691 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr,
2694 mtx_unlock(&softc->ctl_lock);
2696 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2697 ooa_hdr->fill_len = ooa_hdr->fill_num *
2698 sizeof(struct ctl_ooa_entry);
2699 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2701 printf("%s: error copying out %d bytes for OOA dump\n",
2702 __func__, ooa_hdr->fill_len);
2705 getbintime(&ooa_hdr->cur_bt);
2707 if (cur_fill_num > ooa_hdr->alloc_num) {
2708 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2709 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2711 ooa_hdr->dropped_num = 0;
2712 ooa_hdr->status = CTL_OOA_OK;
2715 free(entries, M_CTL);
2718 case CTL_CHECK_OOA: {
2720 struct ctl_lun *lun;
2721 struct ctl_ooa_info *ooa_info;
2724 ooa_info = (struct ctl_ooa_info *)addr;
2726 if (ooa_info->lun_id >= CTL_MAX_LUNS) {
2727 ooa_info->status = CTL_OOA_INVALID_LUN;
2730 mtx_lock(&softc->ctl_lock);
2731 lun = softc->ctl_luns[ooa_info->lun_id];
2733 mtx_unlock(&softc->ctl_lock);
2734 ooa_info->status = CTL_OOA_INVALID_LUN;
2737 mtx_lock(&lun->lun_lock);
2738 mtx_unlock(&softc->ctl_lock);
2739 ooa_info->num_entries = 0;
2740 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
2741 io != NULL; io = (union ctl_io *)TAILQ_NEXT(
2742 &io->io_hdr, ooa_links)) {
2743 ooa_info->num_entries++;
2745 mtx_unlock(&lun->lun_lock);
2747 ooa_info->status = CTL_OOA_SUCCESS;
2751 case CTL_HARD_START:
2752 case CTL_HARD_STOP: {
2753 struct ctl_fe_ioctl_startstop_info ss_info;
2754 struct cfi_metatask *metatask;
2757 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF);
2759 cv_init(&ss_info.sem, "hard start/stop cv" );
2761 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2762 if (metatask == NULL) {
2764 mtx_destroy(&hs_mtx);
2768 if (cmd == CTL_HARD_START)
2769 metatask->tasktype = CFI_TASK_STARTUP;
2771 metatask->tasktype = CFI_TASK_SHUTDOWN;
2773 metatask->callback = ctl_ioctl_hard_startstop_callback;
2774 metatask->callback_arg = &ss_info;
2776 cfi_action(metatask);
2778 /* Wait for the callback */
2780 cv_wait_sig(&ss_info.sem, &hs_mtx);
2781 mtx_unlock(&hs_mtx);
2784 * All information has been copied from the metatask by the
2785 * time cv_broadcast() is called, so we free the metatask here.
2787 cfi_free_metatask(metatask);
2789 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info));
2791 mtx_destroy(&hs_mtx);
2795 struct ctl_bbrread_info *bbr_info;
2796 struct ctl_fe_ioctl_bbrread_info fe_bbr_info;
2798 struct cfi_metatask *metatask;
2800 bbr_info = (struct ctl_bbrread_info *)addr;
2802 bzero(&fe_bbr_info, sizeof(fe_bbr_info));
2804 bzero(&bbr_mtx, sizeof(bbr_mtx));
2805 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF);
2807 fe_bbr_info.bbr_info = bbr_info;
2808 fe_bbr_info.lock = &bbr_mtx;
2810 cv_init(&fe_bbr_info.sem, "BBR read cv");
2811 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2813 if (metatask == NULL) {
2814 mtx_destroy(&bbr_mtx);
2815 cv_destroy(&fe_bbr_info.sem);
2819 metatask->tasktype = CFI_TASK_BBRREAD;
2820 metatask->callback = ctl_ioctl_bbrread_callback;
2821 metatask->callback_arg = &fe_bbr_info;
2822 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num;
2823 metatask->taskinfo.bbrread.lba = bbr_info->lba;
2824 metatask->taskinfo.bbrread.len = bbr_info->len;
2826 cfi_action(metatask);
2829 while (fe_bbr_info.wakeup_done == 0)
2830 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx);
2831 mtx_unlock(&bbr_mtx);
2833 bbr_info->status = metatask->status;
2834 bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2835 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status;
2836 memcpy(&bbr_info->sense_data,
2837 &metatask->taskinfo.bbrread.sense_data,
2838 ctl_min(sizeof(bbr_info->sense_data),
2839 sizeof(metatask->taskinfo.bbrread.sense_data)));
2841 cfi_free_metatask(metatask);
2843 mtx_destroy(&bbr_mtx);
2844 cv_destroy(&fe_bbr_info.sem);
2848 case CTL_DELAY_IO: {
2849 struct ctl_io_delay_info *delay_info;
2851 struct ctl_lun *lun;
2852 #endif /* CTL_IO_DELAY */
2854 delay_info = (struct ctl_io_delay_info *)addr;
2857 mtx_lock(&softc->ctl_lock);
2859 if ((delay_info->lun_id >= CTL_MAX_LUNS)
2860 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2861 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2863 lun = softc->ctl_luns[delay_info->lun_id];
2864 mtx_lock(&lun->lun_lock);
2866 delay_info->status = CTL_DELAY_STATUS_OK;
2868 switch (delay_info->delay_type) {
2869 case CTL_DELAY_TYPE_CONT:
2871 case CTL_DELAY_TYPE_ONESHOT:
2874 delay_info->status =
2875 CTL_DELAY_STATUS_INVALID_TYPE;
2879 switch (delay_info->delay_loc) {
2880 case CTL_DELAY_LOC_DATAMOVE:
2881 lun->delay_info.datamove_type =
2882 delay_info->delay_type;
2883 lun->delay_info.datamove_delay =
2884 delay_info->delay_secs;
2886 case CTL_DELAY_LOC_DONE:
2887 lun->delay_info.done_type =
2888 delay_info->delay_type;
2889 lun->delay_info.done_delay =
2890 delay_info->delay_secs;
2893 delay_info->status =
2894 CTL_DELAY_STATUS_INVALID_LOC;
2897 mtx_unlock(&lun->lun_lock);
2900 mtx_unlock(&softc->ctl_lock);
2902 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2903 #endif /* CTL_IO_DELAY */
2906 case CTL_REALSYNC_SET: {
2909 syncstate = (int *)addr;
2911 mtx_lock(&softc->ctl_lock);
2912 switch (*syncstate) {
2914 softc->flags &= ~CTL_FLAG_REAL_SYNC;
2917 softc->flags |= CTL_FLAG_REAL_SYNC;
2923 mtx_unlock(&softc->ctl_lock);
2926 case CTL_REALSYNC_GET: {
2929 syncstate = (int*)addr;
2931 mtx_lock(&softc->ctl_lock);
2932 if (softc->flags & CTL_FLAG_REAL_SYNC)
2936 mtx_unlock(&softc->ctl_lock);
2942 struct ctl_sync_info *sync_info;
2943 struct ctl_lun *lun;
2945 sync_info = (struct ctl_sync_info *)addr;
2947 mtx_lock(&softc->ctl_lock);
2948 lun = softc->ctl_luns[sync_info->lun_id];
2950 mtx_unlock(&softc->ctl_lock);
2951 sync_info->status = CTL_GS_SYNC_NO_LUN;
2954 * Get or set the sync interval. We're not bounds checking
2955 * in the set case, hopefully the user won't do something
2958 mtx_lock(&lun->lun_lock);
2959 mtx_unlock(&softc->ctl_lock);
2960 if (cmd == CTL_GETSYNC)
2961 sync_info->sync_interval = lun->sync_interval;
2963 lun->sync_interval = sync_info->sync_interval;
2964 mtx_unlock(&lun->lun_lock);
2966 sync_info->status = CTL_GS_SYNC_OK;
2970 case CTL_GETSTATS: {
2971 struct ctl_stats *stats;
2972 struct ctl_lun *lun;
2975 stats = (struct ctl_stats *)addr;
2977 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2979 stats->status = CTL_SS_NEED_MORE_SPACE;
2980 stats->num_luns = softc->num_luns;
2984 * XXX KDM no locking here. If the LUN list changes,
2985 * things can blow up.
2987 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
2988 i++, lun = STAILQ_NEXT(lun, links)) {
2989 retval = copyout(&lun->stats, &stats->lun_stats[i],
2990 sizeof(lun->stats));
2994 stats->num_luns = softc->num_luns;
2995 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2997 stats->status = CTL_SS_OK;
2999 stats->flags = CTL_STATS_FLAG_TIME_VALID;
3001 stats->flags = CTL_STATS_FLAG_NONE;
3003 getnanouptime(&stats->timestamp);
3006 case CTL_ERROR_INJECT: {
3007 struct ctl_error_desc *err_desc, *new_err_desc;
3008 struct ctl_lun *lun;
3010 err_desc = (struct ctl_error_desc *)addr;
3012 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
3014 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
3016 mtx_lock(&softc->ctl_lock);
3017 lun = softc->ctl_luns[err_desc->lun_id];
3019 mtx_unlock(&softc->ctl_lock);
3020 free(new_err_desc, M_CTL);
3021 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
3022 __func__, (uintmax_t)err_desc->lun_id);
3026 mtx_lock(&lun->lun_lock);
3027 mtx_unlock(&softc->ctl_lock);
3030 * We could do some checking here to verify the validity
3031 * of the request, but given the complexity of error
3032 * injection requests, the checking logic would be fairly
3035 * For now, if the request is invalid, it just won't get
3036 * executed and might get deleted.
3038 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
3041 * XXX KDM check to make sure the serial number is unique,
3042 * in case we somehow manage to wrap. That shouldn't
3043 * happen for a very long time, but it's the right thing to
3046 new_err_desc->serial = lun->error_serial;
3047 err_desc->serial = lun->error_serial;
3048 lun->error_serial++;
3050 mtx_unlock(&lun->lun_lock);
3053 case CTL_ERROR_INJECT_DELETE: {
3054 struct ctl_error_desc *delete_desc, *desc, *desc2;
3055 struct ctl_lun *lun;
3058 delete_desc = (struct ctl_error_desc *)addr;
3061 mtx_lock(&softc->ctl_lock);
3062 lun = softc->ctl_luns[delete_desc->lun_id];
3064 mtx_unlock(&softc->ctl_lock);
3065 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
3066 __func__, (uintmax_t)delete_desc->lun_id);
3070 mtx_lock(&lun->lun_lock);
3071 mtx_unlock(&softc->ctl_lock);
3072 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
3073 if (desc->serial != delete_desc->serial)
3076 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
3081 mtx_unlock(&lun->lun_lock);
3082 if (delete_done == 0) {
3083 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
3084 "error serial %ju on LUN %u\n", __func__,
3085 delete_desc->serial, delete_desc->lun_id);
3091 case CTL_DUMP_STRUCTS: {
3093 struct ctl_port *port;
3094 struct ctl_frontend *fe;
3096 mtx_lock(&softc->ctl_lock);
3097 printf("CTL Persistent Reservation information start:\n");
3098 for (i = 0; i < CTL_MAX_LUNS; i++) {
3099 struct ctl_lun *lun;
3101 lun = softc->ctl_luns[i];
3104 || ((lun->flags & CTL_LUN_DISABLED) != 0))
3107 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) {
3108 if (lun->pr_keys[j] == NULL)
3110 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
3111 if (lun->pr_keys[j][k] == 0)
3113 printf(" LUN %d port %d iid %d key "
3115 (uintmax_t)lun->pr_keys[j][k]);
3119 printf("CTL Persistent Reservation information end\n");
3120 printf("CTL Ports:\n");
3121 STAILQ_FOREACH(port, &softc->port_list, links) {
3122 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
3123 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
3124 port->frontend->name, port->port_type,
3125 port->physical_port, port->virtual_port,
3126 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
3127 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3128 if (port->wwpn_iid[j].in_use == 0 &&
3129 port->wwpn_iid[j].wwpn == 0 &&
3130 port->wwpn_iid[j].name == NULL)
3133 printf(" iid %u use %d WWPN %#jx '%s'\n",
3134 j, port->wwpn_iid[j].in_use,
3135 (uintmax_t)port->wwpn_iid[j].wwpn,
3136 port->wwpn_iid[j].name);
3139 printf("CTL Port information end\n");
3140 mtx_unlock(&softc->ctl_lock);
3142 * XXX KDM calling this without a lock. We'd likely want
3143 * to drop the lock before calling the frontend's dump
3146 printf("CTL Frontends:\n");
3147 STAILQ_FOREACH(fe, &softc->fe_list, links) {
3148 printf(" Frontend '%s'\n", fe->name);
3149 if (fe->fe_dump != NULL)
3152 printf("CTL Frontend information end\n");
3156 struct ctl_lun_req *lun_req;
3157 struct ctl_backend_driver *backend;
3159 lun_req = (struct ctl_lun_req *)addr;
3161 backend = ctl_backend_find(lun_req->backend);
3162 if (backend == NULL) {
3163 lun_req->status = CTL_LUN_ERROR;
3164 snprintf(lun_req->error_str,
3165 sizeof(lun_req->error_str),
3166 "Backend \"%s\" not found.",
3170 if (lun_req->num_be_args > 0) {
3171 lun_req->kern_be_args = ctl_copyin_args(
3172 lun_req->num_be_args,
3175 sizeof(lun_req->error_str));
3176 if (lun_req->kern_be_args == NULL) {
3177 lun_req->status = CTL_LUN_ERROR;
3182 retval = backend->ioctl(dev, cmd, addr, flag, td);
3184 if (lun_req->num_be_args > 0) {
3185 ctl_copyout_args(lun_req->num_be_args,
3186 lun_req->kern_be_args);
3187 ctl_free_args(lun_req->num_be_args,
3188 lun_req->kern_be_args);
3192 case CTL_LUN_LIST: {
3194 struct ctl_lun *lun;
3195 struct ctl_lun_list *list;
3196 struct ctl_option *opt;
3198 list = (struct ctl_lun_list *)addr;
3201 * Allocate a fixed length sbuf here, based on the length
3202 * of the user's buffer. We could allocate an auto-extending
3203 * buffer, and then tell the user how much larger our
3204 * amount of data is than his buffer, but that presents
3207 * 1. The sbuf(9) routines use a blocking malloc, and so
3208 * we can't hold a lock while calling them with an
3209 * auto-extending buffer.
3211 * 2. There is not currently a LUN reference counting
3212 * mechanism, outside of outstanding transactions on
3213 * the LUN's OOA queue. So a LUN could go away on us
3214 * while we're getting the LUN number, backend-specific
3215 * information, etc. Thus, given the way things
3216 * currently work, we need to hold the CTL lock while
3217 * grabbing LUN information.
3219 * So, from the user's standpoint, the best thing to do is
3220 * allocate what he thinks is a reasonable buffer length,
3221 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3222 * double the buffer length and try again. (And repeat
3223 * that until he succeeds.)
3225 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3227 list->status = CTL_LUN_LIST_ERROR;
3228 snprintf(list->error_str, sizeof(list->error_str),
3229 "Unable to allocate %d bytes for LUN list",
3234 sbuf_printf(sb, "<ctllunlist>\n");
3236 mtx_lock(&softc->ctl_lock);
3237 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3238 mtx_lock(&lun->lun_lock);
3239 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3240 (uintmax_t)lun->lun);
3243 * Bail out as soon as we see that we've overfilled
3249 retval = sbuf_printf(sb, "\t<backend_type>%s"
3250 "</backend_type>\n",
3251 (lun->backend == NULL) ? "none" :
3252 lun->backend->name);
3257 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3258 lun->be_lun->lun_type);
3263 if (lun->backend == NULL) {
3264 retval = sbuf_printf(sb, "</lun>\n");
3270 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3271 (lun->be_lun->maxlba > 0) ?
3272 lun->be_lun->maxlba + 1 : 0);
3277 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3278 lun->be_lun->blocksize);
3283 retval = sbuf_printf(sb, "\t<serial_number>");
3288 retval = ctl_sbuf_printf_esc(sb,
3289 lun->be_lun->serial_num,
3290 sizeof(lun->be_lun->serial_num));
3295 retval = sbuf_printf(sb, "</serial_number>\n");
3300 retval = sbuf_printf(sb, "\t<device_id>");
3305 retval = ctl_sbuf_printf_esc(sb,
3306 lun->be_lun->device_id,
3307 sizeof(lun->be_lun->device_id));
3312 retval = sbuf_printf(sb, "</device_id>\n");
3317 if (lun->backend->lun_info != NULL) {
3318 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3322 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3323 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3324 opt->name, opt->value, opt->name);
3329 retval = sbuf_printf(sb, "</lun>\n");
3333 mtx_unlock(&lun->lun_lock);
3336 mtx_unlock(&lun->lun_lock);
3337 mtx_unlock(&softc->ctl_lock);
3340 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3343 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3344 snprintf(list->error_str, sizeof(list->error_str),
3345 "Out of space, %d bytes is too small",
3352 retval = copyout(sbuf_data(sb), list->lun_xml,
3355 list->fill_len = sbuf_len(sb) + 1;
3356 list->status = CTL_LUN_LIST_OK;
3361 struct ctl_iscsi *ci;
3362 struct ctl_frontend *fe;
3364 ci = (struct ctl_iscsi *)addr;
3366 fe = ctl_frontend_find("iscsi");
3368 ci->status = CTL_ISCSI_ERROR;
3369 snprintf(ci->error_str, sizeof(ci->error_str),
3370 "Frontend \"iscsi\" not found.");
3374 retval = fe->ioctl(dev, cmd, addr, flag, td);
3377 case CTL_PORT_REQ: {
3378 struct ctl_req *req;
3379 struct ctl_frontend *fe;
3381 req = (struct ctl_req *)addr;
3383 fe = ctl_frontend_find(req->driver);
3385 req->status = CTL_LUN_ERROR;
3386 snprintf(req->error_str, sizeof(req->error_str),
3387 "Frontend \"%s\" not found.", req->driver);
3390 if (req->num_args > 0) {
3391 req->kern_args = ctl_copyin_args(req->num_args,
3392 req->args, req->error_str, sizeof(req->error_str));
3393 if (req->kern_args == NULL) {
3394 req->status = CTL_LUN_ERROR;
3399 retval = fe->ioctl(dev, cmd, addr, flag, td);
3401 if (req->num_args > 0) {
3402 ctl_copyout_args(req->num_args, req->kern_args);
3403 ctl_free_args(req->num_args, req->kern_args);
3407 case CTL_PORT_LIST: {
3409 struct ctl_port *port;
3410 struct ctl_lun_list *list;
3411 struct ctl_option *opt;
3414 list = (struct ctl_lun_list *)addr;
3416 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3418 list->status = CTL_LUN_LIST_ERROR;
3419 snprintf(list->error_str, sizeof(list->error_str),
3420 "Unable to allocate %d bytes for LUN list",
3425 sbuf_printf(sb, "<ctlportlist>\n");
3427 mtx_lock(&softc->ctl_lock);
3428 STAILQ_FOREACH(port, &softc->port_list, links) {
3429 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3430 (uintmax_t)port->targ_port);
3433 * Bail out as soon as we see that we've overfilled
3439 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3440 "</frontend_type>\n", port->frontend->name);
3444 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3449 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3450 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3454 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3459 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3460 port->physical_port);
3464 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3465 port->virtual_port);
3469 if (port->target_devid != NULL) {
3470 sbuf_printf(sb, "\t<target>");
3471 ctl_id_sbuf(port->target_devid, sb);
3472 sbuf_printf(sb, "</target>\n");
3475 if (port->port_devid != NULL) {
3476 sbuf_printf(sb, "\t<port>");
3477 ctl_id_sbuf(port->port_devid, sb);
3478 sbuf_printf(sb, "</port>\n");
3481 if (port->port_info != NULL) {
3482 retval = port->port_info(port->onoff_arg, sb);
3486 STAILQ_FOREACH(opt, &port->options, links) {
3487 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3488 opt->name, opt->value, opt->name);
3493 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3494 if (port->wwpn_iid[j].in_use == 0 ||
3495 (port->wwpn_iid[j].wwpn == 0 &&
3496 port->wwpn_iid[j].name == NULL))
3499 if (port->wwpn_iid[j].name != NULL)
3500 retval = sbuf_printf(sb,
3501 "\t<initiator>%u %s</initiator>\n",
3502 j, port->wwpn_iid[j].name);
3504 retval = sbuf_printf(sb,
3505 "\t<initiator>%u naa.%08jx</initiator>\n",
3506 j, port->wwpn_iid[j].wwpn);
3513 retval = sbuf_printf(sb, "</targ_port>\n");
3517 mtx_unlock(&softc->ctl_lock);
3520 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3523 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3524 snprintf(list->error_str, sizeof(list->error_str),
3525 "Out of space, %d bytes is too small",
3532 retval = copyout(sbuf_data(sb), list->lun_xml,
3535 list->fill_len = sbuf_len(sb) + 1;
3536 list->status = CTL_LUN_LIST_OK;
3541 /* XXX KDM should we fix this? */
3543 struct ctl_backend_driver *backend;
3550 * We encode the backend type as the ioctl type for backend
3551 * ioctls. So parse it out here, and then search for a
3552 * backend of this type.
3554 type = _IOC_TYPE(cmd);
3556 STAILQ_FOREACH(backend, &softc->be_list, links) {
3557 if (backend->type == type) {
3563 printf("ctl: unknown ioctl command %#lx or backend "
3568 retval = backend->ioctl(dev, cmd, addr, flag, td);
3578 ctl_get_initindex(struct ctl_nexus *nexus)
3580 if (nexus->targ_port < CTL_MAX_PORTS)
3581 return (nexus->initid.id +
3582 (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3584 return (nexus->initid.id +
3585 ((nexus->targ_port - CTL_MAX_PORTS) *
3586 CTL_MAX_INIT_PER_PORT));
3590 ctl_get_resindex(struct ctl_nexus *nexus)
3592 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3596 ctl_port_idx(int port_num)
3598 if (port_num < CTL_MAX_PORTS)
3601 return(port_num - CTL_MAX_PORTS);
3605 ctl_map_lun(int port_num, uint32_t lun_id)
3607 struct ctl_port *port;
3609 port = control_softc->ctl_ports[ctl_port_idx(port_num)];
3611 return (UINT32_MAX);
3612 if (port->lun_map == NULL)
3614 return (port->lun_map(port->targ_lun_arg, lun_id));
3618 ctl_map_lun_back(int port_num, uint32_t lun_id)
3620 struct ctl_port *port;
3623 port = control_softc->ctl_ports[ctl_port_idx(port_num)];
3624 if (port->lun_map == NULL)
3626 for (i = 0; i < CTL_MAX_LUNS; i++) {
3627 if (port->lun_map(port->targ_lun_arg, i) == lun_id)
3630 return (UINT32_MAX);
3634 * Note: This only works for bitmask sizes that are at least 32 bits, and
3635 * that are a power of 2.
3638 ctl_ffz(uint32_t *mask, uint32_t size)
3640 uint32_t num_chunks, num_pieces;
3643 num_chunks = (size >> 5);
3644 if (num_chunks == 0)
3646 num_pieces = ctl_min((sizeof(uint32_t) * 8), size);
3648 for (i = 0; i < num_chunks; i++) {
3649 for (j = 0; j < num_pieces; j++) {
3650 if ((mask[i] & (1 << j)) == 0)
3651 return ((i << 5) + j);
3659 ctl_set_mask(uint32_t *mask, uint32_t bit)
3661 uint32_t chunk, piece;
3664 piece = bit % (sizeof(uint32_t) * 8);
3666 if ((mask[chunk] & (1 << piece)) != 0)
3669 mask[chunk] |= (1 << piece);
3675 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3677 uint32_t chunk, piece;
3680 piece = bit % (sizeof(uint32_t) * 8);
3682 if ((mask[chunk] & (1 << piece)) == 0)
3685 mask[chunk] &= ~(1 << piece);
3691 ctl_is_set(uint32_t *mask, uint32_t bit)
3693 uint32_t chunk, piece;
3696 piece = bit % (sizeof(uint32_t) * 8);
3698 if ((mask[chunk] & (1 << piece)) == 0)
3705 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3709 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3712 return (t[residx % CTL_MAX_INIT_PER_PORT]);
3716 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3720 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3723 t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3727 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3732 i = residx/CTL_MAX_INIT_PER_PORT;
3733 if (lun->pr_keys[i] != NULL)
3735 mtx_unlock(&lun->lun_lock);
3736 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3738 mtx_lock(&lun->lun_lock);
3739 if (lun->pr_keys[i] == NULL)
3740 lun->pr_keys[i] = p;
3746 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3750 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3751 KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3752 t[residx % CTL_MAX_INIT_PER_PORT] = key;
3756 * ctl_softc, pool_name, total_ctl_io are passed in.
3757 * npool is passed out.
3760 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3761 uint32_t total_ctl_io, void **npool)
3764 struct ctl_io_pool *pool;
3766 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3771 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3772 pool->ctl_softc = ctl_softc;
3773 pool->zone = uma_zsecond_create(pool->name, NULL,
3774 NULL, NULL, NULL, ctl_softc->io_zone);
3775 /* uma_prealloc(pool->zone, total_ctl_io); */
3779 *npool = ctl_softc->io_zone;
3785 ctl_pool_free(struct ctl_io_pool *pool)
3792 uma_zdestroy(pool->zone);
3798 ctl_alloc_io(void *pool_ref)
3802 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3804 io = uma_zalloc(pool->zone, M_WAITOK);
3806 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK);
3809 io->io_hdr.pool = pool_ref;
3814 ctl_alloc_io_nowait(void *pool_ref)
3818 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3820 io = uma_zalloc(pool->zone, M_NOWAIT);
3822 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT);
3825 io->io_hdr.pool = pool_ref;
3830 ctl_free_io(union ctl_io *io)
3833 struct ctl_io_pool *pool;
3840 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3841 uma_zfree(pool->zone, io);
3843 uma_zfree((uma_zone_t)io->io_hdr.pool, io);
3848 ctl_zero_io(union ctl_io *io)
3856 * May need to preserve linked list pointers at some point too.
3858 pool_ref = io->io_hdr.pool;
3859 memset(io, 0, sizeof(*io));
3860 io->io_hdr.pool = pool_ref;
3864 * This routine is currently used for internal copies of ctl_ios that need
3865 * to persist for some reason after we've already returned status to the
3866 * FETD. (Thus the flag set.)
3869 * Note that this makes a blind copy of all fields in the ctl_io, except
3870 * for the pool reference. This includes any memory that has been
3871 * allocated! That memory will no longer be valid after done has been
3872 * called, so this would be VERY DANGEROUS for command that actually does
3873 * any reads or writes. Right now (11/7/2005), this is only used for immediate
3874 * start and stop commands, which don't transfer any data, so this is not a
3875 * problem. If it is used for anything else, the caller would also need to
3876 * allocate data buffer space and this routine would need to be modified to
3877 * copy the data buffer(s) as well.
3880 ctl_copy_io(union ctl_io *src, union ctl_io *dest)
3889 * May need to preserve linked list pointers at some point too.
3891 pool_ref = dest->io_hdr.pool;
3893 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest)));
3895 dest->io_hdr.pool = pool_ref;
3897 * We need to know that this is an internal copy, and doesn't need
3898 * to get passed back to the FETD that allocated it.
3900 dest->io_hdr.flags |= CTL_FLAG_INT_COPY;
3904 ctl_expand_number(const char *buf, uint64_t *num)
3910 number = strtoq(buf, &endptr, 0);
3912 switch (tolower((unsigned char)*endptr)) {
3932 case '\0': /* No unit. */
3936 /* Unrecognized unit. */
3940 if ((number << shift) >> shift != number) {
3944 *num = number << shift;
3950 * This routine could be used in the future to load default and/or saved
3951 * mode page parameters for a particuar lun.
3954 ctl_init_page_index(struct ctl_lun *lun)
3957 struct ctl_page_index *page_index;
3961 memcpy(&lun->mode_pages.index, page_index_template,
3962 sizeof(page_index_template));
3964 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3966 page_index = &lun->mode_pages.index[i];
3968 * If this is a disk-only mode page, there's no point in
3969 * setting it up. For some pages, we have to have some
3970 * basic information about the disk in order to calculate the
3973 if ((lun->be_lun->lun_type != T_DIRECT)
3974 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
3977 switch (page_index->page_code & SMPH_PC_MASK) {
3978 case SMS_RW_ERROR_RECOVERY_PAGE: {
3979 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3980 panic("subpage is incorrect!");
3981 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
3982 &rw_er_page_default,
3983 sizeof(rw_er_page_default));
3984 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
3985 &rw_er_page_changeable,
3986 sizeof(rw_er_page_changeable));
3987 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
3988 &rw_er_page_default,
3989 sizeof(rw_er_page_default));
3990 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
3991 &rw_er_page_default,
3992 sizeof(rw_er_page_default));
3993 page_index->page_data =
3994 (uint8_t *)lun->mode_pages.rw_er_page;
3997 case SMS_FORMAT_DEVICE_PAGE: {
3998 struct scsi_format_page *format_page;
4000 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4001 panic("subpage is incorrect!");
4004 * Sectors per track are set above. Bytes per
4005 * sector need to be set here on a per-LUN basis.
4007 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
4008 &format_page_default,
4009 sizeof(format_page_default));
4010 memcpy(&lun->mode_pages.format_page[
4011 CTL_PAGE_CHANGEABLE], &format_page_changeable,
4012 sizeof(format_page_changeable));
4013 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
4014 &format_page_default,
4015 sizeof(format_page_default));
4016 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
4017 &format_page_default,
4018 sizeof(format_page_default));
4020 format_page = &lun->mode_pages.format_page[
4022 scsi_ulto2b(lun->be_lun->blocksize,
4023 format_page->bytes_per_sector);
4025 format_page = &lun->mode_pages.format_page[
4027 scsi_ulto2b(lun->be_lun->blocksize,
4028 format_page->bytes_per_sector);
4030 format_page = &lun->mode_pages.format_page[
4032 scsi_ulto2b(lun->be_lun->blocksize,
4033 format_page->bytes_per_sector);
4035 page_index->page_data =
4036 (uint8_t *)lun->mode_pages.format_page;
4039 case SMS_RIGID_DISK_PAGE: {
4040 struct scsi_rigid_disk_page *rigid_disk_page;
4041 uint32_t sectors_per_cylinder;
4045 #endif /* !__XSCALE__ */
4047 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4048 panic("invalid subpage value %d",
4049 page_index->subpage);
4052 * Rotation rate and sectors per track are set
4053 * above. We calculate the cylinders here based on
4054 * capacity. Due to the number of heads and
4055 * sectors per track we're using, smaller arrays
4056 * may turn out to have 0 cylinders. Linux and
4057 * FreeBSD don't pay attention to these mode pages
4058 * to figure out capacity, but Solaris does. It
4059 * seems to deal with 0 cylinders just fine, and
4060 * works out a fake geometry based on the capacity.
4062 memcpy(&lun->mode_pages.rigid_disk_page[
4063 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4064 sizeof(rigid_disk_page_default));
4065 memcpy(&lun->mode_pages.rigid_disk_page[
4066 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4067 sizeof(rigid_disk_page_changeable));
4069 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4073 * The divide method here will be more accurate,
4074 * probably, but results in floating point being
4075 * used in the kernel on i386 (__udivdi3()). On the
4076 * XScale, though, __udivdi3() is implemented in
4079 * The shift method for cylinder calculation is
4080 * accurate if sectors_per_cylinder is a power of
4081 * 2. Otherwise it might be slightly off -- you
4082 * might have a bit of a truncation problem.
4085 cylinders = (lun->be_lun->maxlba + 1) /
4086 sectors_per_cylinder;
4088 for (shift = 31; shift > 0; shift--) {
4089 if (sectors_per_cylinder & (1 << shift))
4092 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4096 * We've basically got 3 bytes, or 24 bits for the
4097 * cylinder size in the mode page. If we're over,
4098 * just round down to 2^24.
4100 if (cylinders > 0xffffff)
4101 cylinders = 0xffffff;
4103 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4105 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4107 if ((value = ctl_get_opt(&lun->be_lun->options,
4109 scsi_ulto2b(strtol(value, NULL, 0),
4110 rigid_disk_page->rotation_rate);
4113 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
4114 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4115 sizeof(rigid_disk_page_default));
4116 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
4117 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4118 sizeof(rigid_disk_page_default));
4120 page_index->page_data =
4121 (uint8_t *)lun->mode_pages.rigid_disk_page;
4124 case SMS_CACHING_PAGE: {
4125 struct scsi_caching_page *caching_page;
4127 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4128 panic("invalid subpage value %d",
4129 page_index->subpage);
4130 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4131 &caching_page_default,
4132 sizeof(caching_page_default));
4133 memcpy(&lun->mode_pages.caching_page[
4134 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4135 sizeof(caching_page_changeable));
4136 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4137 &caching_page_default,
4138 sizeof(caching_page_default));
4139 caching_page = &lun->mode_pages.caching_page[
4141 value = ctl_get_opt(&lun->be_lun->options, "writecache");
4142 if (value != NULL && strcmp(value, "off") == 0)
4143 caching_page->flags1 &= ~SCP_WCE;
4144 value = ctl_get_opt(&lun->be_lun->options, "readcache");
4145 if (value != NULL && strcmp(value, "off") == 0)
4146 caching_page->flags1 |= SCP_RCD;
4147 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4148 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4149 sizeof(caching_page_default));
4150 page_index->page_data =
4151 (uint8_t *)lun->mode_pages.caching_page;
4154 case SMS_CONTROL_MODE_PAGE: {
4155 struct scsi_control_page *control_page;
4157 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4158 panic("invalid subpage value %d",
4159 page_index->subpage);
4161 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT],
4162 &control_page_default,
4163 sizeof(control_page_default));
4164 memcpy(&lun->mode_pages.control_page[
4165 CTL_PAGE_CHANGEABLE], &control_page_changeable,
4166 sizeof(control_page_changeable));
4167 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED],
4168 &control_page_default,
4169 sizeof(control_page_default));
4170 control_page = &lun->mode_pages.control_page[
4172 value = ctl_get_opt(&lun->be_lun->options, "reordering");
4173 if (value != NULL && strcmp(value, "unrestricted") == 0) {
4174 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK;
4175 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED;
4177 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT],
4178 &lun->mode_pages.control_page[CTL_PAGE_SAVED],
4179 sizeof(control_page_default));
4180 page_index->page_data =
4181 (uint8_t *)lun->mode_pages.control_page;
4185 case SMS_INFO_EXCEPTIONS_PAGE: {
4186 switch (page_index->subpage) {
4187 case SMS_SUBPAGE_PAGE_0:
4188 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4190 sizeof(ie_page_default));
4191 memcpy(&lun->mode_pages.ie_page[
4192 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4193 sizeof(ie_page_changeable));
4194 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4196 sizeof(ie_page_default));
4197 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4199 sizeof(ie_page_default));
4200 page_index->page_data =
4201 (uint8_t *)lun->mode_pages.ie_page;
4204 struct ctl_logical_block_provisioning_page *page;
4206 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4208 sizeof(lbp_page_default));
4209 memcpy(&lun->mode_pages.lbp_page[
4210 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4211 sizeof(lbp_page_changeable));
4212 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4214 sizeof(lbp_page_default));
4215 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4216 value = ctl_get_opt(&lun->be_lun->options,
4218 if (value != NULL &&
4219 ctl_expand_number(value, &ival) == 0) {
4220 page->descr[0].flags |= SLBPPD_ENABLED |
4222 if (lun->be_lun->blocksize)
4223 ival /= lun->be_lun->blocksize;
4226 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4227 page->descr[0].count);
4229 value = ctl_get_opt(&lun->be_lun->options,
4231 if (value != NULL &&
4232 ctl_expand_number(value, &ival) == 0) {
4233 page->descr[1].flags |= SLBPPD_ENABLED |
4235 if (lun->be_lun->blocksize)
4236 ival /= lun->be_lun->blocksize;
4239 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4240 page->descr[1].count);
4242 value = ctl_get_opt(&lun->be_lun->options,
4243 "pool-avail-threshold");
4244 if (value != NULL &&
4245 ctl_expand_number(value, &ival) == 0) {
4246 page->descr[2].flags |= SLBPPD_ENABLED |
4248 if (lun->be_lun->blocksize)
4249 ival /= lun->be_lun->blocksize;
4252 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4253 page->descr[2].count);
4255 value = ctl_get_opt(&lun->be_lun->options,
4256 "pool-used-threshold");
4257 if (value != NULL &&
4258 ctl_expand_number(value, &ival) == 0) {
4259 page->descr[3].flags |= SLBPPD_ENABLED |
4261 if (lun->be_lun->blocksize)
4262 ival /= lun->be_lun->blocksize;
4265 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4266 page->descr[3].count);
4268 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4269 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4270 sizeof(lbp_page_default));
4271 page_index->page_data =
4272 (uint8_t *)lun->mode_pages.lbp_page;
4276 case SMS_VENDOR_SPECIFIC_PAGE:{
4277 switch (page_index->subpage) {
4278 case DBGCNF_SUBPAGE_CODE: {
4279 struct copan_debugconf_subpage *current_page,
4282 memcpy(&lun->mode_pages.debugconf_subpage[
4284 &debugconf_page_default,
4285 sizeof(debugconf_page_default));
4286 memcpy(&lun->mode_pages.debugconf_subpage[
4287 CTL_PAGE_CHANGEABLE],
4288 &debugconf_page_changeable,
4289 sizeof(debugconf_page_changeable));
4290 memcpy(&lun->mode_pages.debugconf_subpage[
4292 &debugconf_page_default,
4293 sizeof(debugconf_page_default));
4294 memcpy(&lun->mode_pages.debugconf_subpage[
4296 &debugconf_page_default,
4297 sizeof(debugconf_page_default));
4298 page_index->page_data =
4299 (uint8_t *)lun->mode_pages.debugconf_subpage;
4301 current_page = (struct copan_debugconf_subpage *)
4302 (page_index->page_data +
4303 (page_index->page_len *
4305 saved_page = (struct copan_debugconf_subpage *)
4306 (page_index->page_data +
4307 (page_index->page_len *
4312 panic("invalid subpage value %d",
4313 page_index->subpage);
4319 panic("invalid page value %d",
4320 page_index->page_code & SMPH_PC_MASK);
4325 return (CTL_RETVAL_COMPLETE);
4329 ctl_init_log_page_index(struct ctl_lun *lun)
4331 struct ctl_page_index *page_index;
4334 memcpy(&lun->log_pages.index, log_page_index_template,
4335 sizeof(log_page_index_template));
4338 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4340 page_index = &lun->log_pages.index[i];
4342 * If this is a disk-only mode page, there's no point in
4343 * setting it up. For some pages, we have to have some
4344 * basic information about the disk in order to calculate the
4347 if ((lun->be_lun->lun_type != T_DIRECT)
4348 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
4351 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4352 ((lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) == 0 ||
4353 lun->backend->lun_attr == NULL))
4356 if (page_index->page_code != prev) {
4357 lun->log_pages.pages_page[j] = page_index->page_code;
4358 prev = page_index->page_code;
4361 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4362 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4365 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4366 lun->log_pages.index[0].page_len = j;
4367 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4368 lun->log_pages.index[1].page_len = k * 2;
4369 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4370 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4372 return (CTL_RETVAL_COMPLETE);
4376 hex2bin(const char *str, uint8_t *buf, int buf_size)
4381 memset(buf, 0, buf_size);
4382 while (isspace(str[0]))
4384 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4387 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4391 else if (isalpha(c))
4392 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4398 buf[i / 2] |= (c << 4);
4402 return ((i + 1) / 2);
4409 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4410 * wants us to allocate the LUN and he can block.
4411 * - ctl_softc is always set
4412 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4414 * Returns 0 for success, non-zero (errno) for failure.
4417 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4418 struct ctl_be_lun *const be_lun, struct ctl_id target_id)
4420 struct ctl_lun *nlun, *lun;
4421 struct ctl_port *port;
4422 struct scsi_vpd_id_descriptor *desc;
4423 struct scsi_vpd_id_t10 *t10id;
4424 const char *eui, *naa, *scsiname, *vendor, *value;
4425 int lun_number, i, lun_malloced;
4426 int devidlen, idlen1, idlen2 = 0, len;
4432 * We currently only support Direct Access or Processor LUN types.
4434 switch (be_lun->lun_type) {
4442 be_lun->lun_config_status(be_lun->be_lun,
4443 CTL_LUN_CONFIG_FAILURE);
4446 if (ctl_lun == NULL) {
4447 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4454 memset(lun, 0, sizeof(*lun));
4456 lun->flags = CTL_LUN_MALLOCED;
4458 /* Generate LUN ID. */
4459 devidlen = max(CTL_DEVID_MIN_LEN,
4460 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4461 idlen1 = sizeof(*t10id) + devidlen;
4462 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4463 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4464 if (scsiname != NULL) {
4465 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4466 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4468 eui = ctl_get_opt(&be_lun->options, "eui");
4470 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4472 naa = ctl_get_opt(&be_lun->options, "naa");
4474 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4476 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4477 M_CTL, M_WAITOK | M_ZERO);
4478 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4479 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4480 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4481 desc->length = idlen1;
4482 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4483 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4484 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4485 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4487 strncpy(t10id->vendor, vendor,
4488 min(sizeof(t10id->vendor), strlen(vendor)));
4490 strncpy((char *)t10id->vendor_spec_id,
4491 (char *)be_lun->device_id, devidlen);
4492 if (scsiname != NULL) {
4493 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4495 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4496 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4497 SVPD_ID_TYPE_SCSI_NAME;
4498 desc->length = idlen2;
4499 strlcpy(desc->identifier, scsiname, idlen2);
4502 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4504 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4505 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4507 desc->length = hex2bin(eui, desc->identifier, 16);
4508 desc->length = desc->length > 12 ? 16 :
4509 (desc->length > 8 ? 12 : 8);
4510 len -= 16 - desc->length;
4513 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4515 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4516 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4518 desc->length = hex2bin(naa, desc->identifier, 16);
4519 desc->length = desc->length > 8 ? 16 : 8;
4520 len -= 16 - desc->length;
4522 lun->lun_devid->len = len;
4524 mtx_lock(&ctl_softc->ctl_lock);
4526 * See if the caller requested a particular LUN number. If so, see
4527 * if it is available. Otherwise, allocate the first available LUN.
4529 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4530 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4531 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4532 mtx_unlock(&ctl_softc->ctl_lock);
4533 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4534 printf("ctl: requested LUN ID %d is higher "
4535 "than CTL_MAX_LUNS - 1 (%d)\n",
4536 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4539 * XXX KDM return an error, or just assign
4540 * another LUN ID in this case??
4542 printf("ctl: requested LUN ID %d is already "
4543 "in use\n", be_lun->req_lun_id);
4545 if (lun->flags & CTL_LUN_MALLOCED)
4547 be_lun->lun_config_status(be_lun->be_lun,
4548 CTL_LUN_CONFIG_FAILURE);
4551 lun_number = be_lun->req_lun_id;
4553 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS);
4554 if (lun_number == -1) {
4555 mtx_unlock(&ctl_softc->ctl_lock);
4556 printf("ctl: can't allocate LUN on target %ju, out of "
4557 "LUNs\n", (uintmax_t)target_id.id);
4558 if (lun->flags & CTL_LUN_MALLOCED)
4560 be_lun->lun_config_status(be_lun->be_lun,
4561 CTL_LUN_CONFIG_FAILURE);
4565 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4567 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4568 lun->target = target_id;
4569 lun->lun = lun_number;
4570 lun->be_lun = be_lun;
4572 * The processor LUN is always enabled. Disk LUNs come on line
4573 * disabled, and must be enabled by the backend.
4575 lun->flags |= CTL_LUN_DISABLED;
4576 lun->backend = be_lun->be;
4577 be_lun->ctl_lun = lun;
4578 be_lun->lun_id = lun_number;
4579 atomic_add_int(&be_lun->be->num_luns, 1);
4580 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE)
4581 lun->flags |= CTL_LUN_OFFLINE;
4583 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4584 lun->flags |= CTL_LUN_STOPPED;
4586 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4587 lun->flags |= CTL_LUN_INOPERABLE;
4589 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4590 lun->flags |= CTL_LUN_PRIMARY_SC;
4592 value = ctl_get_opt(&be_lun->options, "readonly");
4593 if (value != NULL && strcmp(value, "on") == 0)
4594 lun->flags |= CTL_LUN_READONLY;
4596 lun->ctl_softc = ctl_softc;
4597 TAILQ_INIT(&lun->ooa_queue);
4598 TAILQ_INIT(&lun->blocked_queue);
4599 STAILQ_INIT(&lun->error_list);
4600 ctl_tpc_lun_init(lun);
4603 * Initialize the mode and log page index.
4605 ctl_init_page_index(lun);
4606 ctl_init_log_page_index(lun);
4609 * Now, before we insert this lun on the lun list, set the lun
4610 * inventory changed UA for all other luns.
4612 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4613 mtx_lock(&nlun->lun_lock);
4614 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4615 mtx_unlock(&nlun->lun_lock);
4618 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4620 ctl_softc->ctl_luns[lun_number] = lun;
4622 ctl_softc->num_luns++;
4624 /* Setup statistics gathering */
4625 lun->stats.device_type = be_lun->lun_type;
4626 lun->stats.lun_number = lun_number;
4627 if (lun->stats.device_type == T_DIRECT)
4628 lun->stats.blocksize = be_lun->blocksize;
4630 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4631 for (i = 0;i < CTL_MAX_PORTS;i++)
4632 lun->stats.ports[i].targ_port = i;
4634 mtx_unlock(&ctl_softc->ctl_lock);
4636 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4639 * Run through each registered FETD and bring it online if it isn't
4640 * already. Enable the target ID if it hasn't been enabled, and
4641 * enable this particular LUN.
4643 STAILQ_FOREACH(port, &ctl_softc->port_list, links) {
4646 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number);
4648 printf("ctl_alloc_lun: FETD %s port %d returned error "
4649 "%d for lun_enable on target %ju lun %d\n",
4650 port->port_name, port->targ_port, retval,
4651 (uintmax_t)target_id.id, lun_number);
4653 port->status |= CTL_PORT_STATUS_LUN_ONLINE;
4661 * - LUN has already been marked invalid and any pending I/O has been taken
4665 ctl_free_lun(struct ctl_lun *lun)
4667 struct ctl_softc *softc;
4669 struct ctl_port *port;
4671 struct ctl_lun *nlun;
4674 softc = lun->ctl_softc;
4676 mtx_assert(&softc->ctl_lock, MA_OWNED);
4678 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4680 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4682 softc->ctl_luns[lun->lun] = NULL;
4684 if (!TAILQ_EMPTY(&lun->ooa_queue))
4685 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4690 * XXX KDM this scheme only works for a single target/multiple LUN
4691 * setup. It needs to be revamped for a multiple target scheme.
4693 * XXX KDM this results in port->lun_disable() getting called twice,
4694 * once when ctl_disable_lun() is called, and a second time here.
4695 * We really need to re-think the LUN disable semantics. There
4696 * should probably be several steps/levels to LUN removal:
4701 * Right now we only have a disable method when communicating to
4702 * the front end ports, at least for individual LUNs.
4705 STAILQ_FOREACH(port, &softc->port_list, links) {
4708 retval = port->lun_disable(port->targ_lun_arg, lun->target,
4711 printf("ctl_free_lun: FETD %s port %d returned error "
4712 "%d for lun_disable on target %ju lun %jd\n",
4713 port->port_name, port->targ_port, retval,
4714 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4717 if (STAILQ_FIRST(&softc->lun_list) == NULL) {
4718 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE;
4720 retval = port->targ_disable(port->targ_lun_arg,lun->target);
4722 printf("ctl_free_lun: FETD %s port %d "
4723 "returned error %d for targ_disable on "
4724 "target %ju\n", port->port_name,
4725 port->targ_port, retval,
4726 (uintmax_t)lun->target.id);
4728 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE;
4730 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0)
4734 port->port_offline(port->onoff_arg);
4735 port->status &= ~CTL_PORT_STATUS_ONLINE;
4742 * Tell the backend to free resources, if this LUN has a backend.
4744 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4745 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4747 ctl_tpc_lun_shutdown(lun);
4748 mtx_destroy(&lun->lun_lock);
4749 free(lun->lun_devid, M_CTL);
4750 for (i = 0; i < CTL_MAX_PORTS; i++)
4751 free(lun->pending_ua[i], M_CTL);
4752 for (i = 0; i < 2 * CTL_MAX_PORTS; i++)
4753 free(lun->pr_keys[i], M_CTL);
4754 free(lun->write_buffer, M_CTL);
4755 if (lun->flags & CTL_LUN_MALLOCED)
4758 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4759 mtx_lock(&nlun->lun_lock);
4760 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4761 mtx_unlock(&nlun->lun_lock);
4768 ctl_create_lun(struct ctl_be_lun *be_lun)
4770 struct ctl_softc *ctl_softc;
4772 ctl_softc = control_softc;
4775 * ctl_alloc_lun() should handle all potential failure cases.
4777 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target);
4781 ctl_add_lun(struct ctl_be_lun *be_lun)
4783 struct ctl_softc *ctl_softc = control_softc;
4785 mtx_lock(&ctl_softc->ctl_lock);
4786 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links);
4787 mtx_unlock(&ctl_softc->ctl_lock);
4788 wakeup(&ctl_softc->pending_lun_queue);
4794 ctl_enable_lun(struct ctl_be_lun *be_lun)
4796 struct ctl_softc *ctl_softc;
4797 struct ctl_port *port, *nport;
4798 struct ctl_lun *lun;
4801 ctl_softc = control_softc;
4803 lun = (struct ctl_lun *)be_lun->ctl_lun;
4805 mtx_lock(&ctl_softc->ctl_lock);
4806 mtx_lock(&lun->lun_lock);
4807 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4809 * eh? Why did we get called if the LUN is already
4812 mtx_unlock(&lun->lun_lock);
4813 mtx_unlock(&ctl_softc->ctl_lock);
4816 lun->flags &= ~CTL_LUN_DISABLED;
4817 mtx_unlock(&lun->lun_lock);
4819 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) {
4820 nport = STAILQ_NEXT(port, links);
4823 * Drop the lock while we call the FETD's enable routine.
4824 * This can lead to a callback into CTL (at least in the
4825 * case of the internal initiator frontend.
4827 mtx_unlock(&ctl_softc->ctl_lock);
4828 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun);
4829 mtx_lock(&ctl_softc->ctl_lock);
4831 printf("%s: FETD %s port %d returned error "
4832 "%d for lun_enable on target %ju lun %jd\n",
4833 __func__, port->port_name, port->targ_port, retval,
4834 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4838 /* NOTE: TODO: why does lun enable affect port status? */
4839 port->status |= CTL_PORT_STATUS_LUN_ONLINE;
4844 mtx_unlock(&ctl_softc->ctl_lock);
4850 ctl_disable_lun(struct ctl_be_lun *be_lun)
4852 struct ctl_softc *ctl_softc;
4853 struct ctl_port *port;
4854 struct ctl_lun *lun;
4857 ctl_softc = control_softc;
4859 lun = (struct ctl_lun *)be_lun->ctl_lun;
4861 mtx_lock(&ctl_softc->ctl_lock);
4862 mtx_lock(&lun->lun_lock);
4863 if (lun->flags & CTL_LUN_DISABLED) {
4864 mtx_unlock(&lun->lun_lock);
4865 mtx_unlock(&ctl_softc->ctl_lock);
4868 lun->flags |= CTL_LUN_DISABLED;
4869 mtx_unlock(&lun->lun_lock);
4871 STAILQ_FOREACH(port, &ctl_softc->port_list, links) {
4872 mtx_unlock(&ctl_softc->ctl_lock);
4874 * Drop the lock before we call the frontend's disable
4875 * routine, to avoid lock order reversals.
4877 * XXX KDM what happens if the frontend list changes while
4878 * we're traversing it? It's unlikely, but should be handled.
4880 retval = port->lun_disable(port->targ_lun_arg, lun->target,
4882 mtx_lock(&ctl_softc->ctl_lock);
4884 printf("ctl_alloc_lun: FETD %s port %d returned error "
4885 "%d for lun_disable on target %ju lun %jd\n",
4886 port->port_name, port->targ_port, retval,
4887 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4891 mtx_unlock(&ctl_softc->ctl_lock);
4897 ctl_start_lun(struct ctl_be_lun *be_lun)
4899 struct ctl_softc *ctl_softc;
4900 struct ctl_lun *lun;
4902 ctl_softc = control_softc;
4904 lun = (struct ctl_lun *)be_lun->ctl_lun;
4906 mtx_lock(&lun->lun_lock);
4907 lun->flags &= ~CTL_LUN_STOPPED;
4908 mtx_unlock(&lun->lun_lock);
4914 ctl_stop_lun(struct ctl_be_lun *be_lun)
4916 struct ctl_softc *ctl_softc;
4917 struct ctl_lun *lun;
4919 ctl_softc = control_softc;
4921 lun = (struct ctl_lun *)be_lun->ctl_lun;
4923 mtx_lock(&lun->lun_lock);
4924 lun->flags |= CTL_LUN_STOPPED;
4925 mtx_unlock(&lun->lun_lock);
4931 ctl_lun_offline(struct ctl_be_lun *be_lun)
4933 struct ctl_softc *ctl_softc;
4934 struct ctl_lun *lun;
4936 ctl_softc = control_softc;
4938 lun = (struct ctl_lun *)be_lun->ctl_lun;
4940 mtx_lock(&lun->lun_lock);
4941 lun->flags |= CTL_LUN_OFFLINE;
4942 mtx_unlock(&lun->lun_lock);
4948 ctl_lun_online(struct ctl_be_lun *be_lun)
4950 struct ctl_softc *ctl_softc;
4951 struct ctl_lun *lun;
4953 ctl_softc = control_softc;
4955 lun = (struct ctl_lun *)be_lun->ctl_lun;
4957 mtx_lock(&lun->lun_lock);
4958 lun->flags &= ~CTL_LUN_OFFLINE;
4959 mtx_unlock(&lun->lun_lock);
4965 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4967 struct ctl_softc *ctl_softc;
4968 struct ctl_lun *lun;
4970 ctl_softc = control_softc;
4972 lun = (struct ctl_lun *)be_lun->ctl_lun;
4974 mtx_lock(&lun->lun_lock);
4977 * The LUN needs to be disabled before it can be marked invalid.
4979 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4980 mtx_unlock(&lun->lun_lock);
4984 * Mark the LUN invalid.
4986 lun->flags |= CTL_LUN_INVALID;
4989 * If there is nothing in the OOA queue, go ahead and free the LUN.
4990 * If we have something in the OOA queue, we'll free it when the
4991 * last I/O completes.
4993 if (TAILQ_EMPTY(&lun->ooa_queue)) {
4994 mtx_unlock(&lun->lun_lock);
4995 mtx_lock(&ctl_softc->ctl_lock);
4997 mtx_unlock(&ctl_softc->ctl_lock);
4999 mtx_unlock(&lun->lun_lock);
5005 ctl_lun_inoperable(struct ctl_be_lun *be_lun)
5007 struct ctl_softc *ctl_softc;
5008 struct ctl_lun *lun;
5010 ctl_softc = control_softc;
5011 lun = (struct ctl_lun *)be_lun->ctl_lun;
5013 mtx_lock(&lun->lun_lock);
5014 lun->flags |= CTL_LUN_INOPERABLE;
5015 mtx_unlock(&lun->lun_lock);
5021 ctl_lun_operable(struct ctl_be_lun *be_lun)
5023 struct ctl_softc *ctl_softc;
5024 struct ctl_lun *lun;
5026 ctl_softc = control_softc;
5027 lun = (struct ctl_lun *)be_lun->ctl_lun;
5029 mtx_lock(&lun->lun_lock);
5030 lun->flags &= ~CTL_LUN_INOPERABLE;
5031 mtx_unlock(&lun->lun_lock);
5037 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
5039 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5041 mtx_lock(&lun->lun_lock);
5042 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGED);
5043 mtx_unlock(&lun->lun_lock);
5047 * Backend "memory move is complete" callback for requests that never
5048 * make it down to say RAIDCore's configuration code.
5051 ctl_config_move_done(union ctl_io *io)
5055 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
5056 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
5057 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
5059 if ((io->io_hdr.port_status != 0) &&
5060 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5061 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5063 * For hardware error sense keys, the sense key
5064 * specific value is defined to be a retry count,
5065 * but we use it to pass back an internal FETD
5066 * error code. XXX KDM Hopefully the FETD is only
5067 * using 16 bits for an error code, since that's
5068 * all the space we have in the sks field.
5070 ctl_set_internal_failure(&io->scsiio,
5073 io->io_hdr.port_status);
5076 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
5077 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5078 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
5079 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5081 * XXX KDM just assuming a single pointer here, and not a
5082 * S/G list. If we start using S/G lists for config data,
5083 * we'll need to know how to clean them up here as well.
5085 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5086 free(io->scsiio.kern_data_ptr, M_CTL);
5088 retval = CTL_RETVAL_COMPLETE;
5091 * XXX KDM now we need to continue data movement. Some
5093 * - call ctl_scsiio() again? We don't do this for data
5094 * writes, because for those at least we know ahead of
5095 * time where the write will go and how long it is. For
5096 * config writes, though, that information is largely
5097 * contained within the write itself, thus we need to
5098 * parse out the data again.
5100 * - Call some other function once the data is in?
5102 if (ctl_debug & CTL_DEBUG_CDB_DATA)
5106 * XXX KDM call ctl_scsiio() again for now, and check flag
5107 * bits to see whether we're allocated or not.
5109 retval = ctl_scsiio(&io->scsiio);
5115 * This gets called by a backend driver when it is done with a
5116 * data_submit method.
5119 ctl_data_submit_done(union ctl_io *io)
5122 * If the IO_CONT flag is set, we need to call the supplied
5123 * function to continue processing the I/O, instead of completing
5126 * If there is an error, though, we don't want to keep processing.
5127 * Instead, just send status back to the initiator.
5129 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5130 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5131 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5132 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5133 io->scsiio.io_cont(io);
5140 * This gets called by a backend driver when it is done with a
5141 * configuration write.
5144 ctl_config_write_done(union ctl_io *io)
5149 * If the IO_CONT flag is set, we need to call the supplied
5150 * function to continue processing the I/O, instead of completing
5153 * If there is an error, though, we don't want to keep processing.
5154 * Instead, just send status back to the initiator.
5156 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5157 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5158 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5159 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5160 io->scsiio.io_cont(io);
5164 * Since a configuration write can be done for commands that actually
5165 * have data allocated, like write buffer, and commands that have
5166 * no data, like start/stop unit, we need to check here.
5168 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5169 buf = io->scsiio.kern_data_ptr;
5178 * SCSI release command.
5181 ctl_scsi_release(struct ctl_scsiio *ctsio)
5183 int length, longid, thirdparty_id, resv_id;
5184 struct ctl_softc *ctl_softc;
5185 struct ctl_lun *lun;
5191 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5193 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5194 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5195 ctl_softc = control_softc;
5197 switch (ctsio->cdb[0]) {
5199 struct scsi_release_10 *cdb;
5201 cdb = (struct scsi_release_10 *)ctsio->cdb;
5203 if (cdb->byte2 & SR10_LONGID)
5206 thirdparty_id = cdb->thirdparty_id;
5208 resv_id = cdb->resv_id;
5209 length = scsi_2btoul(cdb->length);
5216 * XXX KDM right now, we only support LUN reservation. We don't
5217 * support 3rd party reservations, or extent reservations, which
5218 * might actually need the parameter list. If we've gotten this
5219 * far, we've got a LUN reservation. Anything else got kicked out
5220 * above. So, according to SPC, ignore the length.
5224 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5226 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5227 ctsio->kern_data_len = length;
5228 ctsio->kern_total_len = length;
5229 ctsio->kern_data_resid = 0;
5230 ctsio->kern_rel_offset = 0;
5231 ctsio->kern_sg_entries = 0;
5232 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5233 ctsio->be_move_done = ctl_config_move_done;
5234 ctl_datamove((union ctl_io *)ctsio);
5236 return (CTL_RETVAL_COMPLETE);
5240 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5242 mtx_lock(&lun->lun_lock);
5245 * According to SPC, it is not an error for an intiator to attempt
5246 * to release a reservation on a LUN that isn't reserved, or that
5247 * is reserved by another initiator. The reservation can only be
5248 * released, though, by the initiator who made it or by one of
5249 * several reset type events.
5251 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5252 lun->flags &= ~CTL_LUN_RESERVED;
5254 mtx_unlock(&lun->lun_lock);
5256 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5257 free(ctsio->kern_data_ptr, M_CTL);
5258 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5261 ctl_set_success(ctsio);
5262 ctl_done((union ctl_io *)ctsio);
5263 return (CTL_RETVAL_COMPLETE);
5267 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5269 int extent, thirdparty, longid;
5270 int resv_id, length;
5271 uint64_t thirdparty_id;
5272 struct ctl_softc *ctl_softc;
5273 struct ctl_lun *lun;
5283 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5285 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5286 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5287 ctl_softc = control_softc;
5289 switch (ctsio->cdb[0]) {
5291 struct scsi_reserve_10 *cdb;
5293 cdb = (struct scsi_reserve_10 *)ctsio->cdb;
5295 if (cdb->byte2 & SR10_LONGID)
5298 thirdparty_id = cdb->thirdparty_id;
5300 resv_id = cdb->resv_id;
5301 length = scsi_2btoul(cdb->length);
5307 * XXX KDM right now, we only support LUN reservation. We don't
5308 * support 3rd party reservations, or extent reservations, which
5309 * might actually need the parameter list. If we've gotten this
5310 * far, we've got a LUN reservation. Anything else got kicked out
5311 * above. So, according to SPC, ignore the length.
5315 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5317 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5318 ctsio->kern_data_len = length;
5319 ctsio->kern_total_len = length;
5320 ctsio->kern_data_resid = 0;
5321 ctsio->kern_rel_offset = 0;
5322 ctsio->kern_sg_entries = 0;
5323 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5324 ctsio->be_move_done = ctl_config_move_done;
5325 ctl_datamove((union ctl_io *)ctsio);
5327 return (CTL_RETVAL_COMPLETE);
5331 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5333 mtx_lock(&lun->lun_lock);
5334 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5335 ctl_set_reservation_conflict(ctsio);
5339 lun->flags |= CTL_LUN_RESERVED;
5340 lun->res_idx = residx;
5342 ctl_set_success(ctsio);
5345 mtx_unlock(&lun->lun_lock);
5347 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5348 free(ctsio->kern_data_ptr, M_CTL);
5349 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5352 ctl_done((union ctl_io *)ctsio);
5353 return (CTL_RETVAL_COMPLETE);
5357 ctl_start_stop(struct ctl_scsiio *ctsio)
5359 struct scsi_start_stop_unit *cdb;
5360 struct ctl_lun *lun;
5361 struct ctl_softc *ctl_softc;
5364 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5366 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5367 ctl_softc = control_softc;
5370 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5374 * We don't support the immediate bit on a stop unit. In order to
5375 * do that, we would need to code up a way to know that a stop is
5376 * pending, and hold off any new commands until it completes, one
5377 * way or another. Then we could accept or reject those commands
5378 * depending on its status. We would almost need to do the reverse
5379 * of what we do below for an immediate start -- return the copy of
5380 * the ctl_io to the FETD with status to send to the host (and to
5381 * free the copy!) and then free the original I/O once the stop
5382 * actually completes. That way, the OOA queue mechanism can work
5383 * to block commands that shouldn't proceed. Another alternative
5384 * would be to put the copy in the queue in place of the original,
5385 * and return the original back to the caller. That could be
5388 if ((cdb->byte2 & SSS_IMMED)
5389 && ((cdb->how & SSS_START) == 0)) {
5390 ctl_set_invalid_field(ctsio,
5396 ctl_done((union ctl_io *)ctsio);
5397 return (CTL_RETVAL_COMPLETE);
5400 if ((lun->flags & CTL_LUN_PR_RESERVED)
5401 && ((cdb->how & SSS_START)==0)) {
5404 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5405 if (ctl_get_prkey(lun, residx) == 0
5406 || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5408 ctl_set_reservation_conflict(ctsio);
5409 ctl_done((union ctl_io *)ctsio);
5410 return (CTL_RETVAL_COMPLETE);
5415 * If there is no backend on this device, we can't start or stop
5416 * it. In theory we shouldn't get any start/stop commands in the
5417 * first place at this level if the LUN doesn't have a backend.
5418 * That should get stopped by the command decode code.
5420 if (lun->backend == NULL) {
5421 ctl_set_invalid_opcode(ctsio);
5422 ctl_done((union ctl_io *)ctsio);
5423 return (CTL_RETVAL_COMPLETE);
5427 * XXX KDM Copan-specific offline behavior.
5428 * Figure out a reasonable way to port this?
5431 mtx_lock(&lun->lun_lock);
5433 if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
5434 && (lun->flags & CTL_LUN_OFFLINE)) {
5436 * If the LUN is offline, and the on/offline bit isn't set,
5437 * reject the start or stop. Otherwise, let it through.
5439 mtx_unlock(&lun->lun_lock);
5440 ctl_set_lun_not_ready(ctsio);
5441 ctl_done((union ctl_io *)ctsio);
5443 mtx_unlock(&lun->lun_lock);
5444 #endif /* NEEDTOPORT */
5446 * This could be a start or a stop when we're online,
5447 * or a stop/offline or start/online. A start or stop when
5448 * we're offline is covered in the case above.
5451 * In the non-immediate case, we send the request to
5452 * the backend and return status to the user when
5455 * In the immediate case, we allocate a new ctl_io
5456 * to hold a copy of the request, and send that to
5457 * the backend. We then set good status on the
5458 * user's request and return it immediately.
5460 if (cdb->byte2 & SSS_IMMED) {
5461 union ctl_io *new_io;
5463 new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5464 ctl_copy_io((union ctl_io *)ctsio, new_io);
5465 retval = lun->backend->config_write(new_io);
5466 ctl_set_success(ctsio);
5467 ctl_done((union ctl_io *)ctsio);
5469 retval = lun->backend->config_write(
5470 (union ctl_io *)ctsio);
5479 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5480 * we don't really do anything with the LBA and length fields if the user
5481 * passes them in. Instead we'll just flush out the cache for the entire
5485 ctl_sync_cache(struct ctl_scsiio *ctsio)
5487 struct ctl_lun *lun;
5488 struct ctl_softc *ctl_softc;
5489 uint64_t starting_lba;
5490 uint32_t block_count;
5493 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5495 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5496 ctl_softc = control_softc;
5499 switch (ctsio->cdb[0]) {
5500 case SYNCHRONIZE_CACHE: {
5501 struct scsi_sync_cache *cdb;
5502 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5504 starting_lba = scsi_4btoul(cdb->begin_lba);
5505 block_count = scsi_2btoul(cdb->lb_count);
5508 case SYNCHRONIZE_CACHE_16: {
5509 struct scsi_sync_cache_16 *cdb;
5510 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5512 starting_lba = scsi_8btou64(cdb->begin_lba);
5513 block_count = scsi_4btoul(cdb->lb_count);
5517 ctl_set_invalid_opcode(ctsio);
5518 ctl_done((union ctl_io *)ctsio);
5520 break; /* NOTREACHED */
5524 * We check the LBA and length, but don't do anything with them.
5525 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5526 * get flushed. This check will just help satisfy anyone who wants
5527 * to see an error for an out of range LBA.
5529 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5530 ctl_set_lba_out_of_range(ctsio);
5531 ctl_done((union ctl_io *)ctsio);
5536 * If this LUN has no backend, we can't flush the cache anyway.
5538 if (lun->backend == NULL) {
5539 ctl_set_invalid_opcode(ctsio);
5540 ctl_done((union ctl_io *)ctsio);
5545 * Check to see whether we're configured to send the SYNCHRONIZE
5546 * CACHE command directly to the back end.
5548 mtx_lock(&lun->lun_lock);
5549 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC)
5550 && (++(lun->sync_count) >= lun->sync_interval)) {
5551 lun->sync_count = 0;
5552 mtx_unlock(&lun->lun_lock);
5553 retval = lun->backend->config_write((union ctl_io *)ctsio);
5555 mtx_unlock(&lun->lun_lock);
5556 ctl_set_success(ctsio);
5557 ctl_done((union ctl_io *)ctsio);
5566 ctl_format(struct ctl_scsiio *ctsio)
5568 struct scsi_format *cdb;
5569 struct ctl_lun *lun;
5570 struct ctl_softc *ctl_softc;
5571 int length, defect_list_len;
5573 CTL_DEBUG_PRINT(("ctl_format\n"));
5575 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5576 ctl_softc = control_softc;
5578 cdb = (struct scsi_format *)ctsio->cdb;
5581 if (cdb->byte2 & SF_FMTDATA) {
5582 if (cdb->byte2 & SF_LONGLIST)
5583 length = sizeof(struct scsi_format_header_long);
5585 length = sizeof(struct scsi_format_header_short);
5588 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5590 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5591 ctsio->kern_data_len = length;
5592 ctsio->kern_total_len = length;
5593 ctsio->kern_data_resid = 0;
5594 ctsio->kern_rel_offset = 0;
5595 ctsio->kern_sg_entries = 0;
5596 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5597 ctsio->be_move_done = ctl_config_move_done;
5598 ctl_datamove((union ctl_io *)ctsio);
5600 return (CTL_RETVAL_COMPLETE);
5603 defect_list_len = 0;
5605 if (cdb->byte2 & SF_FMTDATA) {
5606 if (cdb->byte2 & SF_LONGLIST) {
5607 struct scsi_format_header_long *header;
5609 header = (struct scsi_format_header_long *)
5610 ctsio->kern_data_ptr;
5612 defect_list_len = scsi_4btoul(header->defect_list_len);
5613 if (defect_list_len != 0) {
5614 ctl_set_invalid_field(ctsio,
5623 struct scsi_format_header_short *header;
5625 header = (struct scsi_format_header_short *)
5626 ctsio->kern_data_ptr;
5628 defect_list_len = scsi_2btoul(header->defect_list_len);
5629 if (defect_list_len != 0) {
5630 ctl_set_invalid_field(ctsio,
5642 * The format command will clear out the "Medium format corrupted"
5643 * status if set by the configuration code. That status is really
5644 * just a way to notify the host that we have lost the media, and
5645 * get them to issue a command that will basically make them think
5646 * they're blowing away the media.
5648 mtx_lock(&lun->lun_lock);
5649 lun->flags &= ~CTL_LUN_INOPERABLE;
5650 mtx_unlock(&lun->lun_lock);
5652 ctl_set_success(ctsio);
5655 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5656 free(ctsio->kern_data_ptr, M_CTL);
5657 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5660 ctl_done((union ctl_io *)ctsio);
5661 return (CTL_RETVAL_COMPLETE);
5665 ctl_read_buffer(struct ctl_scsiio *ctsio)
5667 struct scsi_read_buffer *cdb;
5668 struct ctl_lun *lun;
5669 int buffer_offset, len;
5670 static uint8_t descr[4];
5671 static uint8_t echo_descr[4] = { 0 };
5673 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5675 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5676 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5678 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA &&
5679 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR &&
5680 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) {
5681 ctl_set_invalid_field(ctsio,
5687 ctl_done((union ctl_io *)ctsio);
5688 return (CTL_RETVAL_COMPLETE);
5691 len = scsi_3btoul(cdb->length);
5692 buffer_offset = scsi_3btoul(cdb->offset);
5694 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5695 ctl_set_invalid_field(ctsio,
5701 ctl_done((union ctl_io *)ctsio);
5702 return (CTL_RETVAL_COMPLETE);
5705 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5707 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5708 ctsio->kern_data_ptr = descr;
5709 len = min(len, sizeof(descr));
5710 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5711 ctsio->kern_data_ptr = echo_descr;
5712 len = min(len, sizeof(echo_descr));
5714 if (lun->write_buffer == NULL) {
5715 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5718 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5720 ctsio->kern_data_len = len;
5721 ctsio->kern_total_len = len;
5722 ctsio->kern_data_resid = 0;
5723 ctsio->kern_rel_offset = 0;
5724 ctsio->kern_sg_entries = 0;
5725 ctl_set_success(ctsio);
5726 ctsio->be_move_done = ctl_config_move_done;
5727 ctl_datamove((union ctl_io *)ctsio);
5728 return (CTL_RETVAL_COMPLETE);
5732 ctl_write_buffer(struct ctl_scsiio *ctsio)
5734 struct scsi_write_buffer *cdb;
5735 struct ctl_lun *lun;
5736 int buffer_offset, len;
5738 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5740 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5741 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5743 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5744 ctl_set_invalid_field(ctsio,
5750 ctl_done((union ctl_io *)ctsio);
5751 return (CTL_RETVAL_COMPLETE);
5754 len = scsi_3btoul(cdb->length);
5755 buffer_offset = scsi_3btoul(cdb->offset);
5757 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5758 ctl_set_invalid_field(ctsio,
5764 ctl_done((union ctl_io *)ctsio);
5765 return (CTL_RETVAL_COMPLETE);
5769 * If we've got a kernel request that hasn't been malloced yet,
5770 * malloc it and tell the caller the data buffer is here.
5772 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5773 if (lun->write_buffer == NULL) {
5774 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5777 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5778 ctsio->kern_data_len = len;
5779 ctsio->kern_total_len = len;
5780 ctsio->kern_data_resid = 0;
5781 ctsio->kern_rel_offset = 0;
5782 ctsio->kern_sg_entries = 0;
5783 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5784 ctsio->be_move_done = ctl_config_move_done;
5785 ctl_datamove((union ctl_io *)ctsio);
5787 return (CTL_RETVAL_COMPLETE);
5790 ctl_set_success(ctsio);
5791 ctl_done((union ctl_io *)ctsio);
5792 return (CTL_RETVAL_COMPLETE);
5796 ctl_write_same(struct ctl_scsiio *ctsio)
5798 struct ctl_lun *lun;
5799 struct ctl_lba_len_flags *lbalen;
5801 uint32_t num_blocks;
5805 retval = CTL_RETVAL_COMPLETE;
5807 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5809 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5811 switch (ctsio->cdb[0]) {
5812 case WRITE_SAME_10: {
5813 struct scsi_write_same_10 *cdb;
5815 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5817 lba = scsi_4btoul(cdb->addr);
5818 num_blocks = scsi_2btoul(cdb->length);
5822 case WRITE_SAME_16: {
5823 struct scsi_write_same_16 *cdb;
5825 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5827 lba = scsi_8btou64(cdb->addr);
5828 num_blocks = scsi_4btoul(cdb->length);
5834 * We got a command we don't support. This shouldn't
5835 * happen, commands should be filtered out above us.
5837 ctl_set_invalid_opcode(ctsio);
5838 ctl_done((union ctl_io *)ctsio);
5840 return (CTL_RETVAL_COMPLETE);
5841 break; /* NOTREACHED */
5844 /* NDOB and ANCHOR flags can be used only together with UNMAP */
5845 if ((byte2 & SWS_UNMAP) == 0 &&
5846 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) {
5847 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5848 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5849 ctl_done((union ctl_io *)ctsio);
5850 return (CTL_RETVAL_COMPLETE);
5854 * The first check is to make sure we're in bounds, the second
5855 * check is to catch wrap-around problems. If the lba + num blocks
5856 * is less than the lba, then we've wrapped around and the block
5857 * range is invalid anyway.
5859 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5860 || ((lba + num_blocks) < lba)) {
5861 ctl_set_lba_out_of_range(ctsio);
5862 ctl_done((union ctl_io *)ctsio);
5863 return (CTL_RETVAL_COMPLETE);
5866 /* Zero number of blocks means "to the last logical block" */
5867 if (num_blocks == 0) {
5868 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5869 ctl_set_invalid_field(ctsio,
5875 ctl_done((union ctl_io *)ctsio);
5876 return (CTL_RETVAL_COMPLETE);
5878 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5881 len = lun->be_lun->blocksize;
5884 * If we've got a kernel request that hasn't been malloced yet,
5885 * malloc it and tell the caller the data buffer is here.
5887 if ((byte2 & SWS_NDOB) == 0 &&
5888 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5889 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5890 ctsio->kern_data_len = len;
5891 ctsio->kern_total_len = len;
5892 ctsio->kern_data_resid = 0;
5893 ctsio->kern_rel_offset = 0;
5894 ctsio->kern_sg_entries = 0;
5895 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5896 ctsio->be_move_done = ctl_config_move_done;
5897 ctl_datamove((union ctl_io *)ctsio);
5899 return (CTL_RETVAL_COMPLETE);
5902 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5904 lbalen->len = num_blocks;
5905 lbalen->flags = byte2;
5906 retval = lun->backend->config_write((union ctl_io *)ctsio);
5912 ctl_unmap(struct ctl_scsiio *ctsio)
5914 struct ctl_lun *lun;
5915 struct scsi_unmap *cdb;
5916 struct ctl_ptr_len_flags *ptrlen;
5917 struct scsi_unmap_header *hdr;
5918 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5920 uint32_t num_blocks;
5924 retval = CTL_RETVAL_COMPLETE;
5926 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5928 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5929 cdb = (struct scsi_unmap *)ctsio->cdb;
5931 len = scsi_2btoul(cdb->length);
5935 * If we've got a kernel request that hasn't been malloced yet,
5936 * malloc it and tell the caller the data buffer is here.
5938 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5939 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5940 ctsio->kern_data_len = len;
5941 ctsio->kern_total_len = len;
5942 ctsio->kern_data_resid = 0;
5943 ctsio->kern_rel_offset = 0;
5944 ctsio->kern_sg_entries = 0;
5945 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5946 ctsio->be_move_done = ctl_config_move_done;
5947 ctl_datamove((union ctl_io *)ctsio);
5949 return (CTL_RETVAL_COMPLETE);
5952 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5953 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5954 if (len < sizeof (*hdr) ||
5955 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5956 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5957 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5958 ctl_set_invalid_field(ctsio,
5966 len = scsi_2btoul(hdr->desc_length);
5967 buf = (struct scsi_unmap_desc *)(hdr + 1);
5968 end = buf + len / sizeof(*buf);
5971 for (range = buf; range < end; range++) {
5972 lba = scsi_8btou64(range->lba);
5973 num_blocks = scsi_4btoul(range->length);
5974 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5975 || ((lba + num_blocks) < lba)) {
5976 ctl_set_lba_out_of_range(ctsio);
5977 ctl_done((union ctl_io *)ctsio);
5978 return (CTL_RETVAL_COMPLETE);
5980 if (num_blocks != 0)
5985 * Block backend can not handle zero last range.
5986 * Filter it out and return if there is nothing left.
5988 len = (uint8_t *)endnz - (uint8_t *)buf;
5990 ctl_set_success(ctsio);
5994 mtx_lock(&lun->lun_lock);
5995 ptrlen = (struct ctl_ptr_len_flags *)
5996 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5997 ptrlen->ptr = (void *)buf;
5999 ptrlen->flags = byte2;
6000 ctl_check_blocked(lun);
6001 mtx_unlock(&lun->lun_lock);
6003 retval = lun->backend->config_write((union ctl_io *)ctsio);
6007 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
6008 free(ctsio->kern_data_ptr, M_CTL);
6009 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
6011 ctl_done((union ctl_io *)ctsio);
6012 return (CTL_RETVAL_COMPLETE);
6016 * Note that this function currently doesn't actually do anything inside
6017 * CTL to enforce things if the DQue bit is turned on.
6019 * Also note that this function can't be used in the default case, because
6020 * the DQue bit isn't set in the changeable mask for the control mode page
6021 * anyway. This is just here as an example for how to implement a page
6022 * handler, and a placeholder in case we want to allow the user to turn
6023 * tagged queueing on and off.
6025 * The D_SENSE bit handling is functional, however, and will turn
6026 * descriptor sense on and off for a given LUN.
6029 ctl_control_page_handler(struct ctl_scsiio *ctsio,
6030 struct ctl_page_index *page_index, uint8_t *page_ptr)
6032 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
6033 struct ctl_lun *lun;
6034 struct ctl_softc *softc;
6038 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6039 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6042 user_cp = (struct scsi_control_page *)page_ptr;
6043 current_cp = (struct scsi_control_page *)
6044 (page_index->page_data + (page_index->page_len *
6046 saved_cp = (struct scsi_control_page *)
6047 (page_index->page_data + (page_index->page_len *
6050 softc = control_softc;
6052 mtx_lock(&lun->lun_lock);
6053 if (((current_cp->rlec & SCP_DSENSE) == 0)
6054 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
6056 * Descriptor sense is currently turned off and the user
6057 * wants to turn it on.
6059 current_cp->rlec |= SCP_DSENSE;
6060 saved_cp->rlec |= SCP_DSENSE;
6061 lun->flags |= CTL_LUN_SENSE_DESC;
6063 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
6064 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
6066 * Descriptor sense is currently turned on, and the user
6067 * wants to turn it off.
6069 current_cp->rlec &= ~SCP_DSENSE;
6070 saved_cp->rlec &= ~SCP_DSENSE;
6071 lun->flags &= ~CTL_LUN_SENSE_DESC;
6074 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) !=
6075 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) {
6076 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
6077 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
6078 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
6079 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
6082 if ((current_cp->eca_and_aen & SCP_SWP) !=
6083 (user_cp->eca_and_aen & SCP_SWP)) {
6084 current_cp->eca_and_aen &= ~SCP_SWP;
6085 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
6086 saved_cp->eca_and_aen &= ~SCP_SWP;
6087 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
6091 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
6092 mtx_unlock(&lun->lun_lock);
6098 ctl_caching_sp_handler(struct ctl_scsiio *ctsio,
6099 struct ctl_page_index *page_index, uint8_t *page_ptr)
6101 struct scsi_caching_page *current_cp, *saved_cp, *user_cp;
6102 struct ctl_lun *lun;
6106 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6107 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6110 user_cp = (struct scsi_caching_page *)page_ptr;
6111 current_cp = (struct scsi_caching_page *)
6112 (page_index->page_data + (page_index->page_len *
6114 saved_cp = (struct scsi_caching_page *)
6115 (page_index->page_data + (page_index->page_len *
6118 mtx_lock(&lun->lun_lock);
6119 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) !=
6120 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) {
6121 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
6122 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
6123 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
6124 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
6128 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
6129 mtx_unlock(&lun->lun_lock);
6135 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
6136 struct ctl_page_index *page_index,
6142 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
6147 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
6148 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
6149 printf("page data:");
6151 printf(" %.2x",page_ptr[i]);
6157 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
6158 struct ctl_page_index *page_index,
6161 struct copan_debugconf_subpage *page;
6163 page = (struct copan_debugconf_subpage *)page_index->page_data +
6164 (page_index->page_len * pc);
6167 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6168 case SMS_PAGE_CTRL_DEFAULT >> 6:
6169 case SMS_PAGE_CTRL_SAVED >> 6:
6171 * We don't update the changable or default bits for this page.
6174 case SMS_PAGE_CTRL_CURRENT >> 6:
6175 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
6176 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
6180 EPRINT(0, "Invalid PC %d!!", pc);
6181 #endif /* NEEDTOPORT */
6189 ctl_do_mode_select(union ctl_io *io)
6191 struct scsi_mode_page_header *page_header;
6192 struct ctl_page_index *page_index;
6193 struct ctl_scsiio *ctsio;
6194 int control_dev, page_len;
6195 int page_len_offset, page_len_size;
6196 union ctl_modepage_info *modepage_info;
6197 struct ctl_lun *lun;
6198 int *len_left, *len_used;
6201 ctsio = &io->scsiio;
6204 retval = CTL_RETVAL_COMPLETE;
6206 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6208 if (lun->be_lun->lun_type != T_DIRECT)
6213 modepage_info = (union ctl_modepage_info *)
6214 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6215 len_left = &modepage_info->header.len_left;
6216 len_used = &modepage_info->header.len_used;
6220 page_header = (struct scsi_mode_page_header *)
6221 (ctsio->kern_data_ptr + *len_used);
6223 if (*len_left == 0) {
6224 free(ctsio->kern_data_ptr, M_CTL);
6225 ctl_set_success(ctsio);
6226 ctl_done((union ctl_io *)ctsio);
6227 return (CTL_RETVAL_COMPLETE);
6228 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6230 free(ctsio->kern_data_ptr, M_CTL);
6231 ctl_set_param_len_error(ctsio);
6232 ctl_done((union ctl_io *)ctsio);
6233 return (CTL_RETVAL_COMPLETE);
6235 } else if ((page_header->page_code & SMPH_SPF)
6236 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6238 free(ctsio->kern_data_ptr, M_CTL);
6239 ctl_set_param_len_error(ctsio);
6240 ctl_done((union ctl_io *)ctsio);
6241 return (CTL_RETVAL_COMPLETE);
6246 * XXX KDM should we do something with the block descriptor?
6248 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6250 if ((control_dev != 0)
6251 && (lun->mode_pages.index[i].page_flags &
6252 CTL_PAGE_FLAG_DISK_ONLY))
6255 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
6256 (page_header->page_code & SMPH_PC_MASK))
6260 * If neither page has a subpage code, then we've got a
6263 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
6264 && ((page_header->page_code & SMPH_SPF) == 0)) {
6265 page_index = &lun->mode_pages.index[i];
6266 page_len = page_header->page_length;
6271 * If both pages have subpages, then the subpage numbers
6274 if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
6275 && (page_header->page_code & SMPH_SPF)) {
6276 struct scsi_mode_page_header_sp *sph;
6278 sph = (struct scsi_mode_page_header_sp *)page_header;
6280 if (lun->mode_pages.index[i].subpage ==
6282 page_index = &lun->mode_pages.index[i];
6283 page_len = scsi_2btoul(sph->page_length);
6290 * If we couldn't find the page, or if we don't have a mode select
6291 * handler for it, send back an error to the user.
6293 if ((page_index == NULL)
6294 || (page_index->select_handler == NULL)) {
6295 ctl_set_invalid_field(ctsio,
6298 /*field*/ *len_used,
6301 free(ctsio->kern_data_ptr, M_CTL);
6302 ctl_done((union ctl_io *)ctsio);
6303 return (CTL_RETVAL_COMPLETE);
6306 if (page_index->page_code & SMPH_SPF) {
6307 page_len_offset = 2;
6311 page_len_offset = 1;
6315 * If the length the initiator gives us isn't the one we specify in
6316 * the mode page header, or if they didn't specify enough data in
6317 * the CDB to avoid truncating this page, kick out the request.
6319 if ((page_len != (page_index->page_len - page_len_offset -
6321 || (*len_left < page_index->page_len)) {
6324 ctl_set_invalid_field(ctsio,
6327 /*field*/ *len_used + page_len_offset,
6330 free(ctsio->kern_data_ptr, M_CTL);
6331 ctl_done((union ctl_io *)ctsio);
6332 return (CTL_RETVAL_COMPLETE);
6336 * Run through the mode page, checking to make sure that the bits
6337 * the user changed are actually legal for him to change.
6339 for (i = 0; i < page_index->page_len; i++) {
6340 uint8_t *user_byte, *change_mask, *current_byte;
6344 user_byte = (uint8_t *)page_header + i;
6345 change_mask = page_index->page_data +
6346 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6347 current_byte = page_index->page_data +
6348 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6351 * Check to see whether the user set any bits in this byte
6352 * that he is not allowed to set.
6354 if ((*user_byte & ~(*change_mask)) ==
6355 (*current_byte & ~(*change_mask)))
6359 * Go through bit by bit to determine which one is illegal.
6362 for (j = 7; j >= 0; j--) {
6363 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6364 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6369 ctl_set_invalid_field(ctsio,
6372 /*field*/ *len_used + i,
6375 free(ctsio->kern_data_ptr, M_CTL);
6376 ctl_done((union ctl_io *)ctsio);
6377 return (CTL_RETVAL_COMPLETE);
6381 * Decrement these before we call the page handler, since we may
6382 * end up getting called back one way or another before the handler
6383 * returns to this context.
6385 *len_left -= page_index->page_len;
6386 *len_used += page_index->page_len;
6388 retval = page_index->select_handler(ctsio, page_index,
6389 (uint8_t *)page_header);
6392 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6393 * wait until this queued command completes to finish processing
6394 * the mode page. If it returns anything other than
6395 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6396 * already set the sense information, freed the data pointer, and
6397 * completed the io for us.
6399 if (retval != CTL_RETVAL_COMPLETE)
6400 goto bailout_no_done;
6403 * If the initiator sent us more than one page, parse the next one.
6408 ctl_set_success(ctsio);
6409 free(ctsio->kern_data_ptr, M_CTL);
6410 ctl_done((union ctl_io *)ctsio);
6414 return (CTL_RETVAL_COMPLETE);
6419 ctl_mode_select(struct ctl_scsiio *ctsio)
6421 int param_len, pf, sp;
6422 int header_size, bd_len;
6423 int len_left, len_used;
6424 struct ctl_page_index *page_index;
6425 struct ctl_lun *lun;
6426 int control_dev, page_len;
6427 union ctl_modepage_info *modepage_info;
6439 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6441 if (lun->be_lun->lun_type != T_DIRECT)
6446 switch (ctsio->cdb[0]) {
6447 case MODE_SELECT_6: {
6448 struct scsi_mode_select_6 *cdb;
6450 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6452 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6453 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6455 param_len = cdb->length;
6456 header_size = sizeof(struct scsi_mode_header_6);
6459 case MODE_SELECT_10: {
6460 struct scsi_mode_select_10 *cdb;
6462 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6464 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6465 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6467 param_len = scsi_2btoul(cdb->length);
6468 header_size = sizeof(struct scsi_mode_header_10);
6472 ctl_set_invalid_opcode(ctsio);
6473 ctl_done((union ctl_io *)ctsio);
6474 return (CTL_RETVAL_COMPLETE);
6475 break; /* NOTREACHED */
6480 * "A parameter list length of zero indicates that the Data-Out Buffer
6481 * shall be empty. This condition shall not be considered as an error."
6483 if (param_len == 0) {
6484 ctl_set_success(ctsio);
6485 ctl_done((union ctl_io *)ctsio);
6486 return (CTL_RETVAL_COMPLETE);
6490 * Since we'll hit this the first time through, prior to
6491 * allocation, we don't need to free a data buffer here.
6493 if (param_len < header_size) {
6494 ctl_set_param_len_error(ctsio);
6495 ctl_done((union ctl_io *)ctsio);
6496 return (CTL_RETVAL_COMPLETE);
6500 * Allocate the data buffer and grab the user's data. In theory,
6501 * we shouldn't have to sanity check the parameter list length here
6502 * because the maximum size is 64K. We should be able to malloc
6503 * that much without too many problems.
6505 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6506 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6507 ctsio->kern_data_len = param_len;
6508 ctsio->kern_total_len = param_len;
6509 ctsio->kern_data_resid = 0;
6510 ctsio->kern_rel_offset = 0;
6511 ctsio->kern_sg_entries = 0;
6512 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6513 ctsio->be_move_done = ctl_config_move_done;
6514 ctl_datamove((union ctl_io *)ctsio);
6516 return (CTL_RETVAL_COMPLETE);
6519 switch (ctsio->cdb[0]) {
6520 case MODE_SELECT_6: {
6521 struct scsi_mode_header_6 *mh6;
6523 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6524 bd_len = mh6->blk_desc_len;
6527 case MODE_SELECT_10: {
6528 struct scsi_mode_header_10 *mh10;
6530 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6531 bd_len = scsi_2btoul(mh10->blk_desc_len);
6535 panic("Invalid CDB type %#x", ctsio->cdb[0]);
6539 if (param_len < (header_size + bd_len)) {
6540 free(ctsio->kern_data_ptr, M_CTL);
6541 ctl_set_param_len_error(ctsio);
6542 ctl_done((union ctl_io *)ctsio);
6543 return (CTL_RETVAL_COMPLETE);
6547 * Set the IO_CONT flag, so that if this I/O gets passed to
6548 * ctl_config_write_done(), it'll get passed back to
6549 * ctl_do_mode_select() for further processing, or completion if
6552 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6553 ctsio->io_cont = ctl_do_mode_select;
6555 modepage_info = (union ctl_modepage_info *)
6556 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6558 memset(modepage_info, 0, sizeof(*modepage_info));
6560 len_left = param_len - header_size - bd_len;
6561 len_used = header_size + bd_len;
6563 modepage_info->header.len_left = len_left;
6564 modepage_info->header.len_used = len_used;
6566 return (ctl_do_mode_select((union ctl_io *)ctsio));
6570 ctl_mode_sense(struct ctl_scsiio *ctsio)
6572 struct ctl_lun *lun;
6573 int pc, page_code, dbd, llba, subpage;
6574 int alloc_len, page_len, header_len, total_len;
6575 struct scsi_mode_block_descr *block_desc;
6576 struct ctl_page_index *page_index;
6584 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6586 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6588 if (lun->be_lun->lun_type != T_DIRECT)
6593 switch (ctsio->cdb[0]) {
6594 case MODE_SENSE_6: {
6595 struct scsi_mode_sense_6 *cdb;
6597 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6599 header_len = sizeof(struct scsi_mode_hdr_6);
6600 if (cdb->byte2 & SMS_DBD)
6603 header_len += sizeof(struct scsi_mode_block_descr);
6605 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6606 page_code = cdb->page & SMS_PAGE_CODE;
6607 subpage = cdb->subpage;
6608 alloc_len = cdb->length;
6611 case MODE_SENSE_10: {
6612 struct scsi_mode_sense_10 *cdb;
6614 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6616 header_len = sizeof(struct scsi_mode_hdr_10);
6618 if (cdb->byte2 & SMS_DBD)
6621 header_len += sizeof(struct scsi_mode_block_descr);
6622 if (cdb->byte2 & SMS10_LLBAA)
6624 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6625 page_code = cdb->page & SMS_PAGE_CODE;
6626 subpage = cdb->subpage;
6627 alloc_len = scsi_2btoul(cdb->length);
6631 ctl_set_invalid_opcode(ctsio);
6632 ctl_done((union ctl_io *)ctsio);
6633 return (CTL_RETVAL_COMPLETE);
6634 break; /* NOTREACHED */
6638 * We have to make a first pass through to calculate the size of
6639 * the pages that match the user's query. Then we allocate enough
6640 * memory to hold it, and actually copy the data into the buffer.
6642 switch (page_code) {
6643 case SMS_ALL_PAGES_PAGE: {
6649 * At the moment, values other than 0 and 0xff here are
6650 * reserved according to SPC-3.
6652 if ((subpage != SMS_SUBPAGE_PAGE_0)
6653 && (subpage != SMS_SUBPAGE_ALL)) {
6654 ctl_set_invalid_field(ctsio,
6660 ctl_done((union ctl_io *)ctsio);
6661 return (CTL_RETVAL_COMPLETE);
6664 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6665 if ((control_dev != 0)
6666 && (lun->mode_pages.index[i].page_flags &
6667 CTL_PAGE_FLAG_DISK_ONLY))
6671 * We don't use this subpage if the user didn't
6672 * request all subpages.
6674 if ((lun->mode_pages.index[i].subpage != 0)
6675 && (subpage == SMS_SUBPAGE_PAGE_0))
6679 printf("found page %#x len %d\n",
6680 lun->mode_pages.index[i].page_code &
6682 lun->mode_pages.index[i].page_len);
6684 page_len += lun->mode_pages.index[i].page_len;
6693 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6694 /* Look for the right page code */
6695 if ((lun->mode_pages.index[i].page_code &
6696 SMPH_PC_MASK) != page_code)
6699 /* Look for the right subpage or the subpage wildcard*/
6700 if ((lun->mode_pages.index[i].subpage != subpage)
6701 && (subpage != SMS_SUBPAGE_ALL))
6704 /* Make sure the page is supported for this dev type */
6705 if ((control_dev != 0)
6706 && (lun->mode_pages.index[i].page_flags &
6707 CTL_PAGE_FLAG_DISK_ONLY))
6711 printf("found page %#x len %d\n",
6712 lun->mode_pages.index[i].page_code &
6714 lun->mode_pages.index[i].page_len);
6717 page_len += lun->mode_pages.index[i].page_len;
6720 if (page_len == 0) {
6721 ctl_set_invalid_field(ctsio,
6727 ctl_done((union ctl_io *)ctsio);
6728 return (CTL_RETVAL_COMPLETE);
6734 total_len = header_len + page_len;
6736 printf("header_len = %d, page_len = %d, total_len = %d\n",
6737 header_len, page_len, total_len);
6740 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6741 ctsio->kern_sg_entries = 0;
6742 ctsio->kern_data_resid = 0;
6743 ctsio->kern_rel_offset = 0;
6744 if (total_len < alloc_len) {
6745 ctsio->residual = alloc_len - total_len;
6746 ctsio->kern_data_len = total_len;
6747 ctsio->kern_total_len = total_len;
6749 ctsio->residual = 0;
6750 ctsio->kern_data_len = alloc_len;
6751 ctsio->kern_total_len = alloc_len;
6754 switch (ctsio->cdb[0]) {
6755 case MODE_SENSE_6: {
6756 struct scsi_mode_hdr_6 *header;
6758 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6760 header->datalen = ctl_min(total_len - 1, 254);
6761 if (control_dev == 0) {
6762 header->dev_specific = 0x10; /* DPOFUA */
6763 if ((lun->flags & CTL_LUN_READONLY) ||
6764 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6765 .eca_and_aen & SCP_SWP) != 0)
6766 header->dev_specific |= 0x80; /* WP */
6769 header->block_descr_len = 0;
6771 header->block_descr_len =
6772 sizeof(struct scsi_mode_block_descr);
6773 block_desc = (struct scsi_mode_block_descr *)&header[1];
6776 case MODE_SENSE_10: {
6777 struct scsi_mode_hdr_10 *header;
6780 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6782 datalen = ctl_min(total_len - 2, 65533);
6783 scsi_ulto2b(datalen, header->datalen);
6784 if (control_dev == 0) {
6785 header->dev_specific = 0x10; /* DPOFUA */
6786 if ((lun->flags & CTL_LUN_READONLY) ||
6787 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6788 .eca_and_aen & SCP_SWP) != 0)
6789 header->dev_specific |= 0x80; /* WP */
6792 scsi_ulto2b(0, header->block_descr_len);
6794 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6795 header->block_descr_len);
6796 block_desc = (struct scsi_mode_block_descr *)&header[1];
6800 panic("invalid CDB type %#x", ctsio->cdb[0]);
6801 break; /* NOTREACHED */
6805 * If we've got a disk, use its blocksize in the block
6806 * descriptor. Otherwise, just set it to 0.
6809 if (control_dev == 0)
6810 scsi_ulto3b(lun->be_lun->blocksize,
6811 block_desc->block_len);
6813 scsi_ulto3b(0, block_desc->block_len);
6816 switch (page_code) {
6817 case SMS_ALL_PAGES_PAGE: {
6820 data_used = header_len;
6821 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6822 struct ctl_page_index *page_index;
6824 page_index = &lun->mode_pages.index[i];
6826 if ((control_dev != 0)
6827 && (page_index->page_flags &
6828 CTL_PAGE_FLAG_DISK_ONLY))
6832 * We don't use this subpage if the user didn't
6833 * request all subpages. We already checked (above)
6834 * to make sure the user only specified a subpage
6835 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6837 if ((page_index->subpage != 0)
6838 && (subpage == SMS_SUBPAGE_PAGE_0))
6842 * Call the handler, if it exists, to update the
6843 * page to the latest values.
6845 if (page_index->sense_handler != NULL)
6846 page_index->sense_handler(ctsio, page_index,pc);
6848 memcpy(ctsio->kern_data_ptr + data_used,
6849 page_index->page_data +
6850 (page_index->page_len * pc),
6851 page_index->page_len);
6852 data_used += page_index->page_len;
6859 data_used = header_len;
6861 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6862 struct ctl_page_index *page_index;
6864 page_index = &lun->mode_pages.index[i];
6866 /* Look for the right page code */
6867 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6870 /* Look for the right subpage or the subpage wildcard*/
6871 if ((page_index->subpage != subpage)
6872 && (subpage != SMS_SUBPAGE_ALL))
6875 /* Make sure the page is supported for this dev type */
6876 if ((control_dev != 0)
6877 && (page_index->page_flags &
6878 CTL_PAGE_FLAG_DISK_ONLY))
6882 * Call the handler, if it exists, to update the
6883 * page to the latest values.
6885 if (page_index->sense_handler != NULL)
6886 page_index->sense_handler(ctsio, page_index,pc);
6888 memcpy(ctsio->kern_data_ptr + data_used,
6889 page_index->page_data +
6890 (page_index->page_len * pc),
6891 page_index->page_len);
6892 data_used += page_index->page_len;
6898 ctl_set_success(ctsio);
6899 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6900 ctsio->be_move_done = ctl_config_move_done;
6901 ctl_datamove((union ctl_io *)ctsio);
6902 return (CTL_RETVAL_COMPLETE);
6906 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6907 struct ctl_page_index *page_index,
6910 struct ctl_lun *lun;
6911 struct scsi_log_param_header *phdr;
6915 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6916 data = page_index->page_data;
6918 if (lun->backend->lun_attr != NULL &&
6919 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6921 phdr = (struct scsi_log_param_header *)data;
6922 scsi_ulto2b(0x0001, phdr->param_code);
6923 phdr->param_control = SLP_LBIN | SLP_LP;
6924 phdr->param_len = 8;
6925 data = (uint8_t *)(phdr + 1);
6926 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6927 data[4] = 0x02; /* per-pool */
6928 data += phdr->param_len;
6931 if (lun->backend->lun_attr != NULL &&
6932 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6934 phdr = (struct scsi_log_param_header *)data;
6935 scsi_ulto2b(0x0002, phdr->param_code);
6936 phdr->param_control = SLP_LBIN | SLP_LP;
6937 phdr->param_len = 8;
6938 data = (uint8_t *)(phdr + 1);
6939 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6940 data[4] = 0x01; /* per-LUN */
6941 data += phdr->param_len;
6944 if (lun->backend->lun_attr != NULL &&
6945 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6947 phdr = (struct scsi_log_param_header *)data;
6948 scsi_ulto2b(0x00f1, phdr->param_code);
6949 phdr->param_control = SLP_LBIN | SLP_LP;
6950 phdr->param_len = 8;
6951 data = (uint8_t *)(phdr + 1);
6952 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6953 data[4] = 0x02; /* per-pool */
6954 data += phdr->param_len;
6957 if (lun->backend->lun_attr != NULL &&
6958 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6960 phdr = (struct scsi_log_param_header *)data;
6961 scsi_ulto2b(0x00f2, phdr->param_code);
6962 phdr->param_control = SLP_LBIN | SLP_LP;
6963 phdr->param_len = 8;
6964 data = (uint8_t *)(phdr + 1);
6965 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6966 data[4] = 0x02; /* per-pool */
6967 data += phdr->param_len;
6970 page_index->page_len = data - page_index->page_data;
6975 ctl_log_sense(struct ctl_scsiio *ctsio)
6977 struct ctl_lun *lun;
6978 int i, pc, page_code, subpage;
6979 int alloc_len, total_len;
6980 struct ctl_page_index *page_index;
6981 struct scsi_log_sense *cdb;
6982 struct scsi_log_header *header;
6984 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6986 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6987 cdb = (struct scsi_log_sense *)ctsio->cdb;
6988 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6989 page_code = cdb->page & SLS_PAGE_CODE;
6990 subpage = cdb->subpage;
6991 alloc_len = scsi_2btoul(cdb->length);
6994 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6995 page_index = &lun->log_pages.index[i];
6997 /* Look for the right page code */
6998 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
7001 /* Look for the right subpage or the subpage wildcard*/
7002 if (page_index->subpage != subpage)
7007 if (i >= CTL_NUM_LOG_PAGES) {
7008 ctl_set_invalid_field(ctsio,
7014 ctl_done((union ctl_io *)ctsio);
7015 return (CTL_RETVAL_COMPLETE);
7018 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
7020 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7021 ctsio->kern_sg_entries = 0;
7022 ctsio->kern_data_resid = 0;
7023 ctsio->kern_rel_offset = 0;
7024 if (total_len < alloc_len) {
7025 ctsio->residual = alloc_len - total_len;
7026 ctsio->kern_data_len = total_len;
7027 ctsio->kern_total_len = total_len;
7029 ctsio->residual = 0;
7030 ctsio->kern_data_len = alloc_len;
7031 ctsio->kern_total_len = alloc_len;
7034 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
7035 header->page = page_index->page_code;
7036 if (page_index->subpage) {
7037 header->page |= SL_SPF;
7038 header->subpage = page_index->subpage;
7040 scsi_ulto2b(page_index->page_len, header->datalen);
7043 * Call the handler, if it exists, to update the
7044 * page to the latest values.
7046 if (page_index->sense_handler != NULL)
7047 page_index->sense_handler(ctsio, page_index, pc);
7049 memcpy(header + 1, page_index->page_data, page_index->page_len);
7051 ctl_set_success(ctsio);
7052 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7053 ctsio->be_move_done = ctl_config_move_done;
7054 ctl_datamove((union ctl_io *)ctsio);
7055 return (CTL_RETVAL_COMPLETE);
7059 ctl_read_capacity(struct ctl_scsiio *ctsio)
7061 struct scsi_read_capacity *cdb;
7062 struct scsi_read_capacity_data *data;
7063 struct ctl_lun *lun;
7066 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
7068 cdb = (struct scsi_read_capacity *)ctsio->cdb;
7070 lba = scsi_4btoul(cdb->addr);
7071 if (((cdb->pmi & SRC_PMI) == 0)
7073 ctl_set_invalid_field(/*ctsio*/ ctsio,
7079 ctl_done((union ctl_io *)ctsio);
7080 return (CTL_RETVAL_COMPLETE);
7083 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7085 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7086 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
7087 ctsio->residual = 0;
7088 ctsio->kern_data_len = sizeof(*data);
7089 ctsio->kern_total_len = sizeof(*data);
7090 ctsio->kern_data_resid = 0;
7091 ctsio->kern_rel_offset = 0;
7092 ctsio->kern_sg_entries = 0;
7095 * If the maximum LBA is greater than 0xfffffffe, the user must
7096 * issue a SERVICE ACTION IN (16) command, with the read capacity
7097 * serivce action set.
7099 if (lun->be_lun->maxlba > 0xfffffffe)
7100 scsi_ulto4b(0xffffffff, data->addr);
7102 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
7105 * XXX KDM this may not be 512 bytes...
7107 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7109 ctl_set_success(ctsio);
7110 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7111 ctsio->be_move_done = ctl_config_move_done;
7112 ctl_datamove((union ctl_io *)ctsio);
7113 return (CTL_RETVAL_COMPLETE);
7117 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
7119 struct scsi_read_capacity_16 *cdb;
7120 struct scsi_read_capacity_data_long *data;
7121 struct ctl_lun *lun;
7125 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
7127 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
7129 alloc_len = scsi_4btoul(cdb->alloc_len);
7130 lba = scsi_8btou64(cdb->addr);
7132 if ((cdb->reladr & SRC16_PMI)
7134 ctl_set_invalid_field(/*ctsio*/ ctsio,
7140 ctl_done((union ctl_io *)ctsio);
7141 return (CTL_RETVAL_COMPLETE);
7144 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7146 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7147 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
7149 if (sizeof(*data) < alloc_len) {
7150 ctsio->residual = alloc_len - sizeof(*data);
7151 ctsio->kern_data_len = sizeof(*data);
7152 ctsio->kern_total_len = sizeof(*data);
7154 ctsio->residual = 0;
7155 ctsio->kern_data_len = alloc_len;
7156 ctsio->kern_total_len = alloc_len;
7158 ctsio->kern_data_resid = 0;
7159 ctsio->kern_rel_offset = 0;
7160 ctsio->kern_sg_entries = 0;
7162 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7163 /* XXX KDM this may not be 512 bytes... */
7164 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7165 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7166 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7167 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7168 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
7170 ctl_set_success(ctsio);
7171 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7172 ctsio->be_move_done = ctl_config_move_done;
7173 ctl_datamove((union ctl_io *)ctsio);
7174 return (CTL_RETVAL_COMPLETE);
7178 ctl_read_defect(struct ctl_scsiio *ctsio)
7180 struct scsi_read_defect_data_10 *ccb10;
7181 struct scsi_read_defect_data_12 *ccb12;
7182 struct scsi_read_defect_data_hdr_10 *data10;
7183 struct scsi_read_defect_data_hdr_12 *data12;
7184 uint32_t alloc_len, data_len;
7187 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7189 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7190 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7191 format = ccb10->format;
7192 alloc_len = scsi_2btoul(ccb10->alloc_length);
7193 data_len = sizeof(*data10);
7195 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7196 format = ccb12->format;
7197 alloc_len = scsi_4btoul(ccb12->alloc_length);
7198 data_len = sizeof(*data12);
7200 if (alloc_len == 0) {
7201 ctl_set_success(ctsio);
7202 ctl_done((union ctl_io *)ctsio);
7203 return (CTL_RETVAL_COMPLETE);
7206 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7207 if (data_len < alloc_len) {
7208 ctsio->residual = alloc_len - data_len;
7209 ctsio->kern_data_len = data_len;
7210 ctsio->kern_total_len = data_len;
7212 ctsio->residual = 0;
7213 ctsio->kern_data_len = alloc_len;
7214 ctsio->kern_total_len = alloc_len;
7216 ctsio->kern_data_resid = 0;
7217 ctsio->kern_rel_offset = 0;
7218 ctsio->kern_sg_entries = 0;
7220 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7221 data10 = (struct scsi_read_defect_data_hdr_10 *)
7222 ctsio->kern_data_ptr;
7223 data10->format = format;
7224 scsi_ulto2b(0, data10->length);
7226 data12 = (struct scsi_read_defect_data_hdr_12 *)
7227 ctsio->kern_data_ptr;
7228 data12->format = format;
7229 scsi_ulto2b(0, data12->generation);
7230 scsi_ulto4b(0, data12->length);
7233 ctl_set_success(ctsio);
7234 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7235 ctsio->be_move_done = ctl_config_move_done;
7236 ctl_datamove((union ctl_io *)ctsio);
7237 return (CTL_RETVAL_COMPLETE);
7241 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7243 struct scsi_maintenance_in *cdb;
7245 int alloc_len, ext, total_len = 0, g, p, pc, pg, gs, os;
7246 int num_target_port_groups, num_target_ports;
7247 struct ctl_lun *lun;
7248 struct ctl_softc *softc;
7249 struct ctl_port *port;
7250 struct scsi_target_group_data *rtg_ptr;
7251 struct scsi_target_group_data_extended *rtg_ext_ptr;
7252 struct scsi_target_port_group_descriptor *tpg_desc;
7254 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7256 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7257 softc = control_softc;
7258 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7260 retval = CTL_RETVAL_COMPLETE;
7262 switch (cdb->byte2 & STG_PDF_MASK) {
7263 case STG_PDF_LENGTH:
7266 case STG_PDF_EXTENDED:
7270 ctl_set_invalid_field(/*ctsio*/ ctsio,
7276 ctl_done((union ctl_io *)ctsio);
7280 if (softc->is_single)
7281 num_target_port_groups = 1;
7283 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
7284 num_target_ports = 0;
7285 mtx_lock(&softc->ctl_lock);
7286 STAILQ_FOREACH(port, &softc->port_list, links) {
7287 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7289 if (ctl_map_lun_back(port->targ_port, lun->lun) >= CTL_MAX_LUNS)
7293 mtx_unlock(&softc->ctl_lock);
7296 total_len = sizeof(struct scsi_target_group_data_extended);
7298 total_len = sizeof(struct scsi_target_group_data);
7299 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7300 num_target_port_groups +
7301 sizeof(struct scsi_target_port_descriptor) *
7302 num_target_ports * num_target_port_groups;
7304 alloc_len = scsi_4btoul(cdb->length);
7306 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7308 ctsio->kern_sg_entries = 0;
7310 if (total_len < alloc_len) {
7311 ctsio->residual = alloc_len - total_len;
7312 ctsio->kern_data_len = total_len;
7313 ctsio->kern_total_len = total_len;
7315 ctsio->residual = 0;
7316 ctsio->kern_data_len = alloc_len;
7317 ctsio->kern_total_len = alloc_len;
7319 ctsio->kern_data_resid = 0;
7320 ctsio->kern_rel_offset = 0;
7323 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7324 ctsio->kern_data_ptr;
7325 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7326 rtg_ext_ptr->format_type = 0x10;
7327 rtg_ext_ptr->implicit_transition_time = 0;
7328 tpg_desc = &rtg_ext_ptr->groups[0];
7330 rtg_ptr = (struct scsi_target_group_data *)
7331 ctsio->kern_data_ptr;
7332 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7333 tpg_desc = &rtg_ptr->groups[0];
7336 mtx_lock(&softc->ctl_lock);
7337 pg = softc->port_offset / CTL_MAX_PORTS;
7338 if (softc->flags & CTL_FLAG_ACTIVE_SHELF) {
7339 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) {
7340 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7341 os = TPG_ASYMMETRIC_ACCESS_STANDBY;
7342 } else if (lun->flags & CTL_LUN_PRIMARY_SC) {
7343 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7344 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7346 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7347 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7350 gs = TPG_ASYMMETRIC_ACCESS_STANDBY;
7351 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7353 for (g = 0; g < num_target_port_groups; g++) {
7354 tpg_desc->pref_state = (g == pg) ? gs : os;
7355 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP;
7356 scsi_ulto2b(g + 1, tpg_desc->target_port_group);
7357 tpg_desc->status = TPG_IMPLICIT;
7359 STAILQ_FOREACH(port, &softc->port_list, links) {
7360 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7362 if (ctl_map_lun_back(port->targ_port, lun->lun) >=
7365 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS;
7366 scsi_ulto2b(p, tpg_desc->descriptors[pc].
7367 relative_target_port_identifier);
7370 tpg_desc->target_port_count = pc;
7371 tpg_desc = (struct scsi_target_port_group_descriptor *)
7372 &tpg_desc->descriptors[pc];
7374 mtx_unlock(&softc->ctl_lock);
7376 ctl_set_success(ctsio);
7377 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7378 ctsio->be_move_done = ctl_config_move_done;
7379 ctl_datamove((union ctl_io *)ctsio);
7384 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7386 struct ctl_lun *lun;
7387 struct scsi_report_supported_opcodes *cdb;
7388 const struct ctl_cmd_entry *entry, *sentry;
7389 struct scsi_report_supported_opcodes_all *all;
7390 struct scsi_report_supported_opcodes_descr *descr;
7391 struct scsi_report_supported_opcodes_one *one;
7393 int alloc_len, total_len;
7394 int opcode, service_action, i, j, num;
7396 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7398 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7399 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7401 retval = CTL_RETVAL_COMPLETE;
7403 opcode = cdb->requested_opcode;
7404 service_action = scsi_2btoul(cdb->requested_service_action);
7405 switch (cdb->options & RSO_OPTIONS_MASK) {
7406 case RSO_OPTIONS_ALL:
7408 for (i = 0; i < 256; i++) {
7409 entry = &ctl_cmd_table[i];
7410 if (entry->flags & CTL_CMD_FLAG_SA5) {
7411 for (j = 0; j < 32; j++) {
7412 sentry = &((const struct ctl_cmd_entry *)
7414 if (ctl_cmd_applicable(
7415 lun->be_lun->lun_type, sentry))
7419 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7424 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7425 num * sizeof(struct scsi_report_supported_opcodes_descr);
7427 case RSO_OPTIONS_OC:
7428 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7429 ctl_set_invalid_field(/*ctsio*/ ctsio,
7435 ctl_done((union ctl_io *)ctsio);
7436 return (CTL_RETVAL_COMPLETE);
7438 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7440 case RSO_OPTIONS_OC_SA:
7441 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7442 service_action >= 32) {
7443 ctl_set_invalid_field(/*ctsio*/ ctsio,
7449 ctl_done((union ctl_io *)ctsio);
7450 return (CTL_RETVAL_COMPLETE);
7452 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7455 ctl_set_invalid_field(/*ctsio*/ ctsio,
7461 ctl_done((union ctl_io *)ctsio);
7462 return (CTL_RETVAL_COMPLETE);
7465 alloc_len = scsi_4btoul(cdb->length);
7467 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7469 ctsio->kern_sg_entries = 0;
7471 if (total_len < alloc_len) {
7472 ctsio->residual = alloc_len - total_len;
7473 ctsio->kern_data_len = total_len;
7474 ctsio->kern_total_len = total_len;
7476 ctsio->residual = 0;
7477 ctsio->kern_data_len = alloc_len;
7478 ctsio->kern_total_len = alloc_len;
7480 ctsio->kern_data_resid = 0;
7481 ctsio->kern_rel_offset = 0;
7483 switch (cdb->options & RSO_OPTIONS_MASK) {
7484 case RSO_OPTIONS_ALL:
7485 all = (struct scsi_report_supported_opcodes_all *)
7486 ctsio->kern_data_ptr;
7488 for (i = 0; i < 256; i++) {
7489 entry = &ctl_cmd_table[i];
7490 if (entry->flags & CTL_CMD_FLAG_SA5) {
7491 for (j = 0; j < 32; j++) {
7492 sentry = &((const struct ctl_cmd_entry *)
7494 if (!ctl_cmd_applicable(
7495 lun->be_lun->lun_type, sentry))
7497 descr = &all->descr[num++];
7499 scsi_ulto2b(j, descr->service_action);
7500 descr->flags = RSO_SERVACTV;
7501 scsi_ulto2b(sentry->length,
7505 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7508 descr = &all->descr[num++];
7510 scsi_ulto2b(0, descr->service_action);
7512 scsi_ulto2b(entry->length, descr->cdb_length);
7516 num * sizeof(struct scsi_report_supported_opcodes_descr),
7519 case RSO_OPTIONS_OC:
7520 one = (struct scsi_report_supported_opcodes_one *)
7521 ctsio->kern_data_ptr;
7522 entry = &ctl_cmd_table[opcode];
7524 case RSO_OPTIONS_OC_SA:
7525 one = (struct scsi_report_supported_opcodes_one *)
7526 ctsio->kern_data_ptr;
7527 entry = &ctl_cmd_table[opcode];
7528 entry = &((const struct ctl_cmd_entry *)
7529 entry->execute)[service_action];
7531 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7533 scsi_ulto2b(entry->length, one->cdb_length);
7534 one->cdb_usage[0] = opcode;
7535 memcpy(&one->cdb_usage[1], entry->usage,
7542 ctl_set_success(ctsio);
7543 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7544 ctsio->be_move_done = ctl_config_move_done;
7545 ctl_datamove((union ctl_io *)ctsio);
7550 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7552 struct scsi_report_supported_tmf *cdb;
7553 struct scsi_report_supported_tmf_data *data;
7555 int alloc_len, total_len;
7557 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7559 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7561 retval = CTL_RETVAL_COMPLETE;
7563 total_len = sizeof(struct scsi_report_supported_tmf_data);
7564 alloc_len = scsi_4btoul(cdb->length);
7566 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7568 ctsio->kern_sg_entries = 0;
7570 if (total_len < alloc_len) {
7571 ctsio->residual = alloc_len - total_len;
7572 ctsio->kern_data_len = total_len;
7573 ctsio->kern_total_len = total_len;
7575 ctsio->residual = 0;
7576 ctsio->kern_data_len = alloc_len;
7577 ctsio->kern_total_len = alloc_len;
7579 ctsio->kern_data_resid = 0;
7580 ctsio->kern_rel_offset = 0;
7582 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr;
7583 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS;
7584 data->byte2 |= RST_ITNRS;
7586 ctl_set_success(ctsio);
7587 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7588 ctsio->be_move_done = ctl_config_move_done;
7589 ctl_datamove((union ctl_io *)ctsio);
7594 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7596 struct scsi_report_timestamp *cdb;
7597 struct scsi_report_timestamp_data *data;
7601 int alloc_len, total_len;
7603 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7605 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7607 retval = CTL_RETVAL_COMPLETE;
7609 total_len = sizeof(struct scsi_report_timestamp_data);
7610 alloc_len = scsi_4btoul(cdb->length);
7612 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7614 ctsio->kern_sg_entries = 0;
7616 if (total_len < alloc_len) {
7617 ctsio->residual = alloc_len - total_len;
7618 ctsio->kern_data_len = total_len;
7619 ctsio->kern_total_len = total_len;
7621 ctsio->residual = 0;
7622 ctsio->kern_data_len = alloc_len;
7623 ctsio->kern_total_len = alloc_len;
7625 ctsio->kern_data_resid = 0;
7626 ctsio->kern_rel_offset = 0;
7628 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7629 scsi_ulto2b(sizeof(*data) - 2, data->length);
7630 data->origin = RTS_ORIG_OUTSIDE;
7632 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7633 scsi_ulto4b(timestamp >> 16, data->timestamp);
7634 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7636 ctl_set_success(ctsio);
7637 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7638 ctsio->be_move_done = ctl_config_move_done;
7639 ctl_datamove((union ctl_io *)ctsio);
7644 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7646 struct scsi_per_res_in *cdb;
7647 int alloc_len, total_len = 0;
7648 /* struct scsi_per_res_in_rsrv in_data; */
7649 struct ctl_lun *lun;
7650 struct ctl_softc *softc;
7653 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7655 softc = control_softc;
7657 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7659 alloc_len = scsi_2btoul(cdb->length);
7661 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7664 mtx_lock(&lun->lun_lock);
7665 switch (cdb->action) {
7666 case SPRI_RK: /* read keys */
7667 total_len = sizeof(struct scsi_per_res_in_keys) +
7669 sizeof(struct scsi_per_res_key);
7671 case SPRI_RR: /* read reservation */
7672 if (lun->flags & CTL_LUN_PR_RESERVED)
7673 total_len = sizeof(struct scsi_per_res_in_rsrv);
7675 total_len = sizeof(struct scsi_per_res_in_header);
7677 case SPRI_RC: /* report capabilities */
7678 total_len = sizeof(struct scsi_per_res_cap);
7680 case SPRI_RS: /* read full status */
7681 total_len = sizeof(struct scsi_per_res_in_header) +
7682 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7686 panic("Invalid PR type %x", cdb->action);
7688 mtx_unlock(&lun->lun_lock);
7690 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7692 if (total_len < alloc_len) {
7693 ctsio->residual = alloc_len - total_len;
7694 ctsio->kern_data_len = total_len;
7695 ctsio->kern_total_len = total_len;
7697 ctsio->residual = 0;
7698 ctsio->kern_data_len = alloc_len;
7699 ctsio->kern_total_len = alloc_len;
7702 ctsio->kern_data_resid = 0;
7703 ctsio->kern_rel_offset = 0;
7704 ctsio->kern_sg_entries = 0;
7706 mtx_lock(&lun->lun_lock);
7707 switch (cdb->action) {
7708 case SPRI_RK: { // read keys
7709 struct scsi_per_res_in_keys *res_keys;
7712 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7715 * We had to drop the lock to allocate our buffer, which
7716 * leaves time for someone to come in with another
7717 * persistent reservation. (That is unlikely, though,
7718 * since this should be the only persistent reservation
7719 * command active right now.)
7721 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7722 (lun->pr_key_count *
7723 sizeof(struct scsi_per_res_key)))){
7724 mtx_unlock(&lun->lun_lock);
7725 free(ctsio->kern_data_ptr, M_CTL);
7726 printf("%s: reservation length changed, retrying\n",
7731 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7733 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7734 lun->pr_key_count, res_keys->header.length);
7736 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7737 if ((key = ctl_get_prkey(lun, i)) == 0)
7741 * We used lun->pr_key_count to calculate the
7742 * size to allocate. If it turns out the number of
7743 * initiators with the registered flag set is
7744 * larger than that (i.e. they haven't been kept in
7745 * sync), we've got a problem.
7747 if (key_count >= lun->pr_key_count) {
7749 csevent_log(CSC_CTL | CSC_SHELF_SW |
7751 csevent_LogType_Fault,
7752 csevent_AlertLevel_Yellow,
7753 csevent_FRU_ShelfController,
7754 csevent_FRU_Firmware,
7755 csevent_FRU_Unknown,
7756 "registered keys %d >= key "
7757 "count %d", key_count,
7763 scsi_u64to8b(key, res_keys->keys[key_count].key);
7768 case SPRI_RR: { // read reservation
7769 struct scsi_per_res_in_rsrv *res;
7770 int tmp_len, header_only;
7772 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7774 scsi_ulto4b(lun->PRGeneration, res->header.generation);
7776 if (lun->flags & CTL_LUN_PR_RESERVED)
7778 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7779 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7780 res->header.length);
7783 tmp_len = sizeof(struct scsi_per_res_in_header);
7784 scsi_ulto4b(0, res->header.length);
7789 * We had to drop the lock to allocate our buffer, which
7790 * leaves time for someone to come in with another
7791 * persistent reservation. (That is unlikely, though,
7792 * since this should be the only persistent reservation
7793 * command active right now.)
7795 if (tmp_len != total_len) {
7796 mtx_unlock(&lun->lun_lock);
7797 free(ctsio->kern_data_ptr, M_CTL);
7798 printf("%s: reservation status changed, retrying\n",
7804 * No reservation held, so we're done.
7806 if (header_only != 0)
7810 * If the registration is an All Registrants type, the key
7811 * is 0, since it doesn't really matter.
7813 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7814 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7815 res->data.reservation);
7817 res->data.scopetype = lun->res_type;
7820 case SPRI_RC: //report capabilities
7822 struct scsi_per_res_cap *res_cap;
7825 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7826 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7827 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5;
7828 type_mask = SPRI_TM_WR_EX_AR |
7834 scsi_ulto2b(type_mask, res_cap->type_mask);
7837 case SPRI_RS: { // read full status
7838 struct scsi_per_res_in_full *res_status;
7839 struct scsi_per_res_in_full_desc *res_desc;
7840 struct ctl_port *port;
7843 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7846 * We had to drop the lock to allocate our buffer, which
7847 * leaves time for someone to come in with another
7848 * persistent reservation. (That is unlikely, though,
7849 * since this should be the only persistent reservation
7850 * command active right now.)
7852 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7853 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7854 lun->pr_key_count)){
7855 mtx_unlock(&lun->lun_lock);
7856 free(ctsio->kern_data_ptr, M_CTL);
7857 printf("%s: reservation length changed, retrying\n",
7862 scsi_ulto4b(lun->PRGeneration, res_status->header.generation);
7864 res_desc = &res_status->desc[0];
7865 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7866 if ((key = ctl_get_prkey(lun, i)) == 0)
7869 scsi_u64to8b(key, res_desc->res_key.key);
7870 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7871 (lun->pr_res_idx == i ||
7872 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7873 res_desc->flags = SPRI_FULL_R_HOLDER;
7874 res_desc->scopetype = lun->res_type;
7876 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7877 res_desc->rel_trgt_port_id);
7879 port = softc->ctl_ports[
7880 ctl_port_idx(i / CTL_MAX_INIT_PER_PORT)];
7882 len = ctl_create_iid(port,
7883 i % CTL_MAX_INIT_PER_PORT,
7884 res_desc->transport_id);
7885 scsi_ulto4b(len, res_desc->additional_length);
7886 res_desc = (struct scsi_per_res_in_full_desc *)
7887 &res_desc->transport_id[len];
7889 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7890 res_status->header.length);
7895 * This is a bug, because we just checked for this above,
7896 * and should have returned an error.
7898 panic("Invalid PR type %x", cdb->action);
7899 break; /* NOTREACHED */
7901 mtx_unlock(&lun->lun_lock);
7903 ctl_set_success(ctsio);
7904 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7905 ctsio->be_move_done = ctl_config_move_done;
7906 ctl_datamove((union ctl_io *)ctsio);
7907 return (CTL_RETVAL_COMPLETE);
7911 ctl_set_res_ua(struct ctl_lun *lun, uint32_t residx, ctl_ua_type ua)
7913 int off = lun->ctl_softc->persis_offset;
7915 if (residx >= off && residx < off + CTL_MAX_INITIATORS)
7916 ctl_est_ua(lun, residx - off, ua);
7920 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7924 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7925 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7926 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7927 struct scsi_per_res_out_parms* param)
7929 union ctl_ha_msg persis_io;
7935 mtx_lock(&lun->lun_lock);
7936 if (sa_res_key == 0) {
7937 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7938 /* validate scope and type */
7939 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7941 mtx_unlock(&lun->lun_lock);
7942 ctl_set_invalid_field(/*ctsio*/ ctsio,
7948 ctl_done((union ctl_io *)ctsio);
7952 if (type>8 || type==2 || type==4 || type==0) {
7953 mtx_unlock(&lun->lun_lock);
7954 ctl_set_invalid_field(/*ctsio*/ ctsio,
7960 ctl_done((union ctl_io *)ctsio);
7965 * Unregister everybody else and build UA for
7968 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7969 if (i == residx || ctl_get_prkey(lun, i) == 0)
7972 ctl_clr_prkey(lun, i);
7973 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT);
7975 lun->pr_key_count = 1;
7976 lun->res_type = type;
7977 if (lun->res_type != SPR_TYPE_WR_EX_AR
7978 && lun->res_type != SPR_TYPE_EX_AC_AR)
7979 lun->pr_res_idx = residx;
7981 /* send msg to other side */
7982 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7983 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7984 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7985 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7986 persis_io.pr.pr_info.res_type = type;
7987 memcpy(persis_io.pr.pr_info.sa_res_key,
7988 param->serv_act_res_key,
7989 sizeof(param->serv_act_res_key));
7990 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7991 &persis_io, sizeof(persis_io), 0)) >
7992 CTL_HA_STATUS_SUCCESS) {
7993 printf("CTL:Persis Out error returned "
7994 "from ctl_ha_msg_send %d\n",
7998 /* not all registrants */
7999 mtx_unlock(&lun->lun_lock);
8000 free(ctsio->kern_data_ptr, M_CTL);
8001 ctl_set_invalid_field(ctsio,
8007 ctl_done((union ctl_io *)ctsio);
8010 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8011 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
8014 if (res_key == sa_res_key) {
8017 * The spec implies this is not good but doesn't
8018 * say what to do. There are two choices either
8019 * generate a res conflict or check condition
8020 * with illegal field in parameter data. Since
8021 * that is what is done when the sa_res_key is
8022 * zero I'll take that approach since this has
8023 * to do with the sa_res_key.
8025 mtx_unlock(&lun->lun_lock);
8026 free(ctsio->kern_data_ptr, M_CTL);
8027 ctl_set_invalid_field(ctsio,
8033 ctl_done((union ctl_io *)ctsio);
8037 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8038 if (ctl_get_prkey(lun, i) != sa_res_key)
8042 ctl_clr_prkey(lun, i);
8043 lun->pr_key_count--;
8044 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8047 mtx_unlock(&lun->lun_lock);
8048 free(ctsio->kern_data_ptr, M_CTL);
8049 ctl_set_reservation_conflict(ctsio);
8050 ctl_done((union ctl_io *)ctsio);
8051 return (CTL_RETVAL_COMPLETE);
8053 /* send msg to other side */
8054 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8055 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8056 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8057 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8058 persis_io.pr.pr_info.res_type = type;
8059 memcpy(persis_io.pr.pr_info.sa_res_key,
8060 param->serv_act_res_key,
8061 sizeof(param->serv_act_res_key));
8062 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8063 &persis_io, sizeof(persis_io), 0)) >
8064 CTL_HA_STATUS_SUCCESS) {
8065 printf("CTL:Persis Out error returned from "
8066 "ctl_ha_msg_send %d\n", isc_retval);
8069 /* Reserved but not all registrants */
8070 /* sa_res_key is res holder */
8071 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
8072 /* validate scope and type */
8073 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
8075 mtx_unlock(&lun->lun_lock);
8076 ctl_set_invalid_field(/*ctsio*/ ctsio,
8082 ctl_done((union ctl_io *)ctsio);
8086 if (type>8 || type==2 || type==4 || type==0) {
8087 mtx_unlock(&lun->lun_lock);
8088 ctl_set_invalid_field(/*ctsio*/ ctsio,
8094 ctl_done((union ctl_io *)ctsio);
8100 * if sa_res_key != res_key remove all
8101 * registrants w/sa_res_key and generate UA
8102 * for these registrants(Registrations
8103 * Preempted) if it wasn't an exclusive
8104 * reservation generate UA(Reservations
8105 * Preempted) for all other registered nexuses
8106 * if the type has changed. Establish the new
8107 * reservation and holder. If res_key and
8108 * sa_res_key are the same do the above
8109 * except don't unregister the res holder.
8112 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8113 if (i == residx || ctl_get_prkey(lun, i) == 0)
8116 if (sa_res_key == ctl_get_prkey(lun, i)) {
8117 ctl_clr_prkey(lun, i);
8118 lun->pr_key_count--;
8119 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8120 } else if (type != lun->res_type
8121 && (lun->res_type == SPR_TYPE_WR_EX_RO
8122 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
8123 ctl_set_res_ua(lun, i, CTL_UA_RES_RELEASE);
8126 lun->res_type = type;
8127 if (lun->res_type != SPR_TYPE_WR_EX_AR
8128 && lun->res_type != SPR_TYPE_EX_AC_AR)
8129 lun->pr_res_idx = residx;
8131 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8133 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8134 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8135 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8136 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8137 persis_io.pr.pr_info.res_type = type;
8138 memcpy(persis_io.pr.pr_info.sa_res_key,
8139 param->serv_act_res_key,
8140 sizeof(param->serv_act_res_key));
8141 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8142 &persis_io, sizeof(persis_io), 0)) >
8143 CTL_HA_STATUS_SUCCESS) {
8144 printf("CTL:Persis Out error returned "
8145 "from ctl_ha_msg_send %d\n",
8150 * sa_res_key is not the res holder just
8151 * remove registrants
8155 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8156 if (sa_res_key != ctl_get_prkey(lun, i))
8160 ctl_clr_prkey(lun, i);
8161 lun->pr_key_count--;
8162 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8166 mtx_unlock(&lun->lun_lock);
8167 free(ctsio->kern_data_ptr, M_CTL);
8168 ctl_set_reservation_conflict(ctsio);
8169 ctl_done((union ctl_io *)ctsio);
8172 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8173 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8174 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8175 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8176 persis_io.pr.pr_info.res_type = type;
8177 memcpy(persis_io.pr.pr_info.sa_res_key,
8178 param->serv_act_res_key,
8179 sizeof(param->serv_act_res_key));
8180 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8181 &persis_io, sizeof(persis_io), 0)) >
8182 CTL_HA_STATUS_SUCCESS) {
8183 printf("CTL:Persis Out error returned "
8184 "from ctl_ha_msg_send %d\n",
8190 lun->PRGeneration++;
8191 mtx_unlock(&lun->lun_lock);
8197 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8199 uint64_t sa_res_key;
8202 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8204 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8205 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8206 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
8207 if (sa_res_key == 0) {
8209 * Unregister everybody else and build UA for
8212 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8213 if (i == msg->pr.pr_info.residx ||
8214 ctl_get_prkey(lun, i) == 0)
8217 ctl_clr_prkey(lun, i);
8218 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8221 lun->pr_key_count = 1;
8222 lun->res_type = msg->pr.pr_info.res_type;
8223 if (lun->res_type != SPR_TYPE_WR_EX_AR
8224 && lun->res_type != SPR_TYPE_EX_AC_AR)
8225 lun->pr_res_idx = msg->pr.pr_info.residx;
8227 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8228 if (sa_res_key == ctl_get_prkey(lun, i))
8231 ctl_clr_prkey(lun, i);
8232 lun->pr_key_count--;
8233 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8237 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8238 if (i == msg->pr.pr_info.residx ||
8239 ctl_get_prkey(lun, i) == 0)
8242 if (sa_res_key == ctl_get_prkey(lun, i)) {
8243 ctl_clr_prkey(lun, i);
8244 lun->pr_key_count--;
8245 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8246 } else if (msg->pr.pr_info.res_type != lun->res_type
8247 && (lun->res_type == SPR_TYPE_WR_EX_RO
8248 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
8249 ctl_set_res_ua(lun, i, CTL_UA_RES_RELEASE);
8252 lun->res_type = msg->pr.pr_info.res_type;
8253 if (lun->res_type != SPR_TYPE_WR_EX_AR
8254 && lun->res_type != SPR_TYPE_EX_AC_AR)
8255 lun->pr_res_idx = msg->pr.pr_info.residx;
8257 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8259 lun->PRGeneration++;
8265 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8269 u_int32_t param_len;
8270 struct scsi_per_res_out *cdb;
8271 struct ctl_lun *lun;
8272 struct scsi_per_res_out_parms* param;
8273 struct ctl_softc *softc;
8275 uint64_t res_key, sa_res_key, key;
8277 union ctl_ha_msg persis_io;
8280 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8282 retval = CTL_RETVAL_COMPLETE;
8284 softc = control_softc;
8286 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8287 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8290 * We only support whole-LUN scope. The scope & type are ignored for
8291 * register, register and ignore existing key and clear.
8292 * We sometimes ignore scope and type on preempts too!!
8293 * Verify reservation type here as well.
8295 type = cdb->scope_type & SPR_TYPE_MASK;
8296 if ((cdb->action == SPRO_RESERVE)
8297 || (cdb->action == SPRO_RELEASE)) {
8298 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8299 ctl_set_invalid_field(/*ctsio*/ ctsio,
8305 ctl_done((union ctl_io *)ctsio);
8306 return (CTL_RETVAL_COMPLETE);
8309 if (type>8 || type==2 || type==4 || type==0) {
8310 ctl_set_invalid_field(/*ctsio*/ ctsio,
8316 ctl_done((union ctl_io *)ctsio);
8317 return (CTL_RETVAL_COMPLETE);
8321 param_len = scsi_4btoul(cdb->length);
8323 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8324 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8325 ctsio->kern_data_len = param_len;
8326 ctsio->kern_total_len = param_len;
8327 ctsio->kern_data_resid = 0;
8328 ctsio->kern_rel_offset = 0;
8329 ctsio->kern_sg_entries = 0;
8330 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8331 ctsio->be_move_done = ctl_config_move_done;
8332 ctl_datamove((union ctl_io *)ctsio);
8334 return (CTL_RETVAL_COMPLETE);
8337 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8339 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8340 res_key = scsi_8btou64(param->res_key.key);
8341 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8344 * Validate the reservation key here except for SPRO_REG_IGNO
8345 * This must be done for all other service actions
8347 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8348 mtx_lock(&lun->lun_lock);
8349 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8350 if (res_key != key) {
8352 * The current key passed in doesn't match
8353 * the one the initiator previously
8356 mtx_unlock(&lun->lun_lock);
8357 free(ctsio->kern_data_ptr, M_CTL);
8358 ctl_set_reservation_conflict(ctsio);
8359 ctl_done((union ctl_io *)ctsio);
8360 return (CTL_RETVAL_COMPLETE);
8362 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8364 * We are not registered
8366 mtx_unlock(&lun->lun_lock);
8367 free(ctsio->kern_data_ptr, M_CTL);
8368 ctl_set_reservation_conflict(ctsio);
8369 ctl_done((union ctl_io *)ctsio);
8370 return (CTL_RETVAL_COMPLETE);
8371 } else if (res_key != 0) {
8373 * We are not registered and trying to register but
8374 * the register key isn't zero.
8376 mtx_unlock(&lun->lun_lock);
8377 free(ctsio->kern_data_ptr, M_CTL);
8378 ctl_set_reservation_conflict(ctsio);
8379 ctl_done((union ctl_io *)ctsio);
8380 return (CTL_RETVAL_COMPLETE);
8382 mtx_unlock(&lun->lun_lock);
8385 switch (cdb->action & SPRO_ACTION_MASK) {
8387 case SPRO_REG_IGNO: {
8390 printf("Registration received\n");
8394 * We don't support any of these options, as we report in
8395 * the read capabilities request (see
8396 * ctl_persistent_reserve_in(), above).
8398 if ((param->flags & SPR_SPEC_I_PT)
8399 || (param->flags & SPR_ALL_TG_PT)
8400 || (param->flags & SPR_APTPL)) {
8403 if (param->flags & SPR_APTPL)
8405 else if (param->flags & SPR_ALL_TG_PT)
8407 else /* SPR_SPEC_I_PT */
8410 free(ctsio->kern_data_ptr, M_CTL);
8411 ctl_set_invalid_field(ctsio,
8417 ctl_done((union ctl_io *)ctsio);
8418 return (CTL_RETVAL_COMPLETE);
8421 mtx_lock(&lun->lun_lock);
8424 * The initiator wants to clear the
8427 if (sa_res_key == 0) {
8429 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8430 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8431 && ctl_get_prkey(lun, residx) == 0)) {
8432 mtx_unlock(&lun->lun_lock);
8436 ctl_clr_prkey(lun, residx);
8437 lun->pr_key_count--;
8439 if (residx == lun->pr_res_idx) {
8440 lun->flags &= ~CTL_LUN_PR_RESERVED;
8441 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8443 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8444 || lun->res_type == SPR_TYPE_EX_AC_RO)
8445 && lun->pr_key_count) {
8447 * If the reservation is a registrants
8448 * only type we need to generate a UA
8449 * for other registered inits. The
8450 * sense code should be RESERVATIONS
8454 for (i = 0; i < CTL_MAX_INITIATORS;i++){
8455 if (ctl_get_prkey(lun, i +
8456 softc->persis_offset) == 0)
8459 CTL_UA_RES_RELEASE);
8463 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8464 if (lun->pr_key_count==0) {
8465 lun->flags &= ~CTL_LUN_PR_RESERVED;
8467 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8470 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8471 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8472 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8473 persis_io.pr.pr_info.residx = residx;
8474 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8475 &persis_io, sizeof(persis_io), 0 )) >
8476 CTL_HA_STATUS_SUCCESS) {
8477 printf("CTL:Persis Out error returned from "
8478 "ctl_ha_msg_send %d\n", isc_retval);
8480 } else /* sa_res_key != 0 */ {
8483 * If we aren't registered currently then increment
8484 * the key count and set the registered flag.
8486 ctl_alloc_prkey(lun, residx);
8487 if (ctl_get_prkey(lun, residx) == 0)
8488 lun->pr_key_count++;
8489 ctl_set_prkey(lun, residx, sa_res_key);
8491 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8492 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8493 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8494 persis_io.pr.pr_info.residx = residx;
8495 memcpy(persis_io.pr.pr_info.sa_res_key,
8496 param->serv_act_res_key,
8497 sizeof(param->serv_act_res_key));
8498 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8499 &persis_io, sizeof(persis_io), 0)) >
8500 CTL_HA_STATUS_SUCCESS) {
8501 printf("CTL:Persis Out error returned from "
8502 "ctl_ha_msg_send %d\n", isc_retval);
8505 lun->PRGeneration++;
8506 mtx_unlock(&lun->lun_lock);
8512 printf("Reserve executed type %d\n", type);
8514 mtx_lock(&lun->lun_lock);
8515 if (lun->flags & CTL_LUN_PR_RESERVED) {
8517 * if this isn't the reservation holder and it's
8518 * not a "all registrants" type or if the type is
8519 * different then we have a conflict
8521 if ((lun->pr_res_idx != residx
8522 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8523 || lun->res_type != type) {
8524 mtx_unlock(&lun->lun_lock);
8525 free(ctsio->kern_data_ptr, M_CTL);
8526 ctl_set_reservation_conflict(ctsio);
8527 ctl_done((union ctl_io *)ctsio);
8528 return (CTL_RETVAL_COMPLETE);
8530 mtx_unlock(&lun->lun_lock);
8531 } else /* create a reservation */ {
8533 * If it's not an "all registrants" type record
8534 * reservation holder
8536 if (type != SPR_TYPE_WR_EX_AR
8537 && type != SPR_TYPE_EX_AC_AR)
8538 lun->pr_res_idx = residx; /* Res holder */
8540 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8542 lun->flags |= CTL_LUN_PR_RESERVED;
8543 lun->res_type = type;
8545 mtx_unlock(&lun->lun_lock);
8547 /* send msg to other side */
8548 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8549 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8550 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8551 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8552 persis_io.pr.pr_info.res_type = type;
8553 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8554 &persis_io, sizeof(persis_io), 0)) >
8555 CTL_HA_STATUS_SUCCESS) {
8556 printf("CTL:Persis Out error returned from "
8557 "ctl_ha_msg_send %d\n", isc_retval);
8563 mtx_lock(&lun->lun_lock);
8564 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8565 /* No reservation exists return good status */
8566 mtx_unlock(&lun->lun_lock);
8570 * Is this nexus a reservation holder?
8572 if (lun->pr_res_idx != residx
8573 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8575 * not a res holder return good status but
8578 mtx_unlock(&lun->lun_lock);
8582 if (lun->res_type != type) {
8583 mtx_unlock(&lun->lun_lock);
8584 free(ctsio->kern_data_ptr, M_CTL);
8585 ctl_set_illegal_pr_release(ctsio);
8586 ctl_done((union ctl_io *)ctsio);
8587 return (CTL_RETVAL_COMPLETE);
8590 /* okay to release */
8591 lun->flags &= ~CTL_LUN_PR_RESERVED;
8592 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8596 * if this isn't an exclusive access
8597 * res generate UA for all other
8600 if (type != SPR_TYPE_EX_AC
8601 && type != SPR_TYPE_WR_EX) {
8602 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8605 i + softc->persis_offset) == 0)
8607 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8610 mtx_unlock(&lun->lun_lock);
8611 /* Send msg to other side */
8612 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8613 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8614 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8615 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io,
8616 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8617 printf("CTL:Persis Out error returned from "
8618 "ctl_ha_msg_send %d\n", isc_retval);
8623 /* send msg to other side */
8625 mtx_lock(&lun->lun_lock);
8626 lun->flags &= ~CTL_LUN_PR_RESERVED;
8628 lun->pr_key_count = 0;
8629 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8631 ctl_clr_prkey(lun, residx);
8632 for (i=0; i < 2*CTL_MAX_INITIATORS; i++)
8633 if (ctl_get_prkey(lun, i) != 0) {
8634 ctl_clr_prkey(lun, i);
8635 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8637 lun->PRGeneration++;
8638 mtx_unlock(&lun->lun_lock);
8639 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8640 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8641 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8642 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8643 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8644 printf("CTL:Persis Out error returned from "
8645 "ctl_ha_msg_send %d\n", isc_retval);
8650 case SPRO_PRE_ABO: {
8653 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8654 residx, ctsio, cdb, param);
8656 return (CTL_RETVAL_COMPLETE);
8660 panic("Invalid PR type %x", cdb->action);
8664 free(ctsio->kern_data_ptr, M_CTL);
8665 ctl_set_success(ctsio);
8666 ctl_done((union ctl_io *)ctsio);
8672 * This routine is for handling a message from the other SC pertaining to
8673 * persistent reserve out. All the error checking will have been done
8674 * so only perorming the action need be done here to keep the two
8678 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8680 struct ctl_lun *lun;
8681 struct ctl_softc *softc;
8685 softc = control_softc;
8687 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8688 lun = softc->ctl_luns[targ_lun];
8689 mtx_lock(&lun->lun_lock);
8690 switch(msg->pr.pr_info.action) {
8691 case CTL_PR_REG_KEY:
8692 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8693 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8694 lun->pr_key_count++;
8695 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8696 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8697 lun->PRGeneration++;
8700 case CTL_PR_UNREG_KEY:
8701 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8702 lun->pr_key_count--;
8704 /* XXX Need to see if the reservation has been released */
8705 /* if so do we need to generate UA? */
8706 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8707 lun->flags &= ~CTL_LUN_PR_RESERVED;
8708 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8710 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8711 || lun->res_type == SPR_TYPE_EX_AC_RO)
8712 && lun->pr_key_count) {
8714 * If the reservation is a registrants
8715 * only type we need to generate a UA
8716 * for other registered inits. The
8717 * sense code should be RESERVATIONS
8721 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8722 if (ctl_get_prkey(lun, i +
8723 softc->persis_offset) == 0)
8726 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8730 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8731 if (lun->pr_key_count==0) {
8732 lun->flags &= ~CTL_LUN_PR_RESERVED;
8734 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8737 lun->PRGeneration++;
8740 case CTL_PR_RESERVE:
8741 lun->flags |= CTL_LUN_PR_RESERVED;
8742 lun->res_type = msg->pr.pr_info.res_type;
8743 lun->pr_res_idx = msg->pr.pr_info.residx;
8747 case CTL_PR_RELEASE:
8749 * if this isn't an exclusive access res generate UA for all
8750 * other registrants.
8752 if (lun->res_type != SPR_TYPE_EX_AC
8753 && lun->res_type != SPR_TYPE_WR_EX) {
8754 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8755 if (ctl_get_prkey(lun, i + softc->persis_offset) != 0)
8756 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8759 lun->flags &= ~CTL_LUN_PR_RESERVED;
8760 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8764 case CTL_PR_PREEMPT:
8765 ctl_pro_preempt_other(lun, msg);
8768 lun->flags &= ~CTL_LUN_PR_RESERVED;
8770 lun->pr_key_count = 0;
8771 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8773 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8774 if (ctl_get_prkey(lun, i) == 0)
8776 ctl_clr_prkey(lun, i);
8777 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8779 lun->PRGeneration++;
8783 mtx_unlock(&lun->lun_lock);
8787 ctl_read_write(struct ctl_scsiio *ctsio)
8789 struct ctl_lun *lun;
8790 struct ctl_lba_len_flags *lbalen;
8792 uint32_t num_blocks;
8796 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8798 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8801 retval = CTL_RETVAL_COMPLETE;
8803 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8804 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8805 switch (ctsio->cdb[0]) {
8808 struct scsi_rw_6 *cdb;
8810 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8812 lba = scsi_3btoul(cdb->addr);
8813 /* only 5 bits are valid in the most significant address byte */
8815 num_blocks = cdb->length;
8817 * This is correct according to SBC-2.
8819 if (num_blocks == 0)
8825 struct scsi_rw_10 *cdb;
8827 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8828 if (cdb->byte2 & SRW10_FUA)
8829 flags |= CTL_LLF_FUA;
8830 if (cdb->byte2 & SRW10_DPO)
8831 flags |= CTL_LLF_DPO;
8832 lba = scsi_4btoul(cdb->addr);
8833 num_blocks = scsi_2btoul(cdb->length);
8836 case WRITE_VERIFY_10: {
8837 struct scsi_write_verify_10 *cdb;
8839 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8840 flags |= CTL_LLF_FUA;
8841 if (cdb->byte2 & SWV_DPO)
8842 flags |= CTL_LLF_DPO;
8843 lba = scsi_4btoul(cdb->addr);
8844 num_blocks = scsi_2btoul(cdb->length);
8849 struct scsi_rw_12 *cdb;
8851 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8852 if (cdb->byte2 & SRW12_FUA)
8853 flags |= CTL_LLF_FUA;
8854 if (cdb->byte2 & SRW12_DPO)
8855 flags |= CTL_LLF_DPO;
8856 lba = scsi_4btoul(cdb->addr);
8857 num_blocks = scsi_4btoul(cdb->length);
8860 case WRITE_VERIFY_12: {
8861 struct scsi_write_verify_12 *cdb;
8863 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8864 flags |= CTL_LLF_FUA;
8865 if (cdb->byte2 & SWV_DPO)
8866 flags |= CTL_LLF_DPO;
8867 lba = scsi_4btoul(cdb->addr);
8868 num_blocks = scsi_4btoul(cdb->length);
8873 struct scsi_rw_16 *cdb;
8875 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8876 if (cdb->byte2 & SRW12_FUA)
8877 flags |= CTL_LLF_FUA;
8878 if (cdb->byte2 & SRW12_DPO)
8879 flags |= CTL_LLF_DPO;
8880 lba = scsi_8btou64(cdb->addr);
8881 num_blocks = scsi_4btoul(cdb->length);
8884 case WRITE_ATOMIC_16: {
8885 struct scsi_rw_16 *cdb;
8887 if (lun->be_lun->atomicblock == 0) {
8888 ctl_set_invalid_opcode(ctsio);
8889 ctl_done((union ctl_io *)ctsio);
8890 return (CTL_RETVAL_COMPLETE);
8893 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8894 if (cdb->byte2 & SRW12_FUA)
8895 flags |= CTL_LLF_FUA;
8896 if (cdb->byte2 & SRW12_DPO)
8897 flags |= CTL_LLF_DPO;
8898 lba = scsi_8btou64(cdb->addr);
8899 num_blocks = scsi_4btoul(cdb->length);
8900 if (num_blocks > lun->be_lun->atomicblock) {
8901 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8902 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8904 ctl_done((union ctl_io *)ctsio);
8905 return (CTL_RETVAL_COMPLETE);
8909 case WRITE_VERIFY_16: {
8910 struct scsi_write_verify_16 *cdb;
8912 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8913 flags |= CTL_LLF_FUA;
8914 if (cdb->byte2 & SWV_DPO)
8915 flags |= CTL_LLF_DPO;
8916 lba = scsi_8btou64(cdb->addr);
8917 num_blocks = scsi_4btoul(cdb->length);
8922 * We got a command we don't support. This shouldn't
8923 * happen, commands should be filtered out above us.
8925 ctl_set_invalid_opcode(ctsio);
8926 ctl_done((union ctl_io *)ctsio);
8928 return (CTL_RETVAL_COMPLETE);
8929 break; /* NOTREACHED */
8933 * The first check is to make sure we're in bounds, the second
8934 * check is to catch wrap-around problems. If the lba + num blocks
8935 * is less than the lba, then we've wrapped around and the block
8936 * range is invalid anyway.
8938 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8939 || ((lba + num_blocks) < lba)) {
8940 ctl_set_lba_out_of_range(ctsio);
8941 ctl_done((union ctl_io *)ctsio);
8942 return (CTL_RETVAL_COMPLETE);
8946 * According to SBC-3, a transfer length of 0 is not an error.
8947 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8948 * translates to 256 blocks for those commands.
8950 if (num_blocks == 0) {
8951 ctl_set_success(ctsio);
8952 ctl_done((union ctl_io *)ctsio);
8953 return (CTL_RETVAL_COMPLETE);
8956 /* Set FUA and/or DPO if caches are disabled. */
8958 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8960 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8962 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8964 flags |= CTL_LLF_FUA;
8967 lbalen = (struct ctl_lba_len_flags *)
8968 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8970 lbalen->len = num_blocks;
8971 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8973 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8974 ctsio->kern_rel_offset = 0;
8976 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8978 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8984 ctl_cnw_cont(union ctl_io *io)
8986 struct ctl_scsiio *ctsio;
8987 struct ctl_lun *lun;
8988 struct ctl_lba_len_flags *lbalen;
8991 ctsio = &io->scsiio;
8992 ctsio->io_hdr.status = CTL_STATUS_NONE;
8993 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8994 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8995 lbalen = (struct ctl_lba_len_flags *)
8996 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8997 lbalen->flags &= ~CTL_LLF_COMPARE;
8998 lbalen->flags |= CTL_LLF_WRITE;
9000 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
9001 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9006 ctl_cnw(struct ctl_scsiio *ctsio)
9008 struct ctl_lun *lun;
9009 struct ctl_lba_len_flags *lbalen;
9011 uint32_t num_blocks;
9014 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9016 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
9019 retval = CTL_RETVAL_COMPLETE;
9021 switch (ctsio->cdb[0]) {
9022 case COMPARE_AND_WRITE: {
9023 struct scsi_compare_and_write *cdb;
9025 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
9026 if (cdb->byte2 & SRW10_FUA)
9027 flags |= CTL_LLF_FUA;
9028 if (cdb->byte2 & SRW10_DPO)
9029 flags |= CTL_LLF_DPO;
9030 lba = scsi_8btou64(cdb->addr);
9031 num_blocks = cdb->length;
9036 * We got a command we don't support. This shouldn't
9037 * happen, commands should be filtered out above us.
9039 ctl_set_invalid_opcode(ctsio);
9040 ctl_done((union ctl_io *)ctsio);
9042 return (CTL_RETVAL_COMPLETE);
9043 break; /* NOTREACHED */
9047 * The first check is to make sure we're in bounds, the second
9048 * check is to catch wrap-around problems. If the lba + num blocks
9049 * is less than the lba, then we've wrapped around and the block
9050 * range is invalid anyway.
9052 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9053 || ((lba + num_blocks) < lba)) {
9054 ctl_set_lba_out_of_range(ctsio);
9055 ctl_done((union ctl_io *)ctsio);
9056 return (CTL_RETVAL_COMPLETE);
9060 * According to SBC-3, a transfer length of 0 is not an error.
9062 if (num_blocks == 0) {
9063 ctl_set_success(ctsio);
9064 ctl_done((union ctl_io *)ctsio);
9065 return (CTL_RETVAL_COMPLETE);
9068 /* Set FUA if write cache is disabled. */
9069 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9071 flags |= CTL_LLF_FUA;
9073 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
9074 ctsio->kern_rel_offset = 0;
9077 * Set the IO_CONT flag, so that if this I/O gets passed to
9078 * ctl_data_submit_done(), it'll get passed back to
9079 * ctl_ctl_cnw_cont() for further processing.
9081 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
9082 ctsio->io_cont = ctl_cnw_cont;
9084 lbalen = (struct ctl_lba_len_flags *)
9085 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9087 lbalen->len = num_blocks;
9088 lbalen->flags = CTL_LLF_COMPARE | flags;
9090 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
9091 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9096 ctl_verify(struct ctl_scsiio *ctsio)
9098 struct ctl_lun *lun;
9099 struct ctl_lba_len_flags *lbalen;
9101 uint32_t num_blocks;
9105 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9107 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
9110 flags = CTL_LLF_FUA;
9111 retval = CTL_RETVAL_COMPLETE;
9113 switch (ctsio->cdb[0]) {
9115 struct scsi_verify_10 *cdb;
9117 cdb = (struct scsi_verify_10 *)ctsio->cdb;
9118 if (cdb->byte2 & SVFY_BYTCHK)
9120 if (cdb->byte2 & SVFY_DPO)
9121 flags |= CTL_LLF_DPO;
9122 lba = scsi_4btoul(cdb->addr);
9123 num_blocks = scsi_2btoul(cdb->length);
9127 struct scsi_verify_12 *cdb;
9129 cdb = (struct scsi_verify_12 *)ctsio->cdb;
9130 if (cdb->byte2 & SVFY_BYTCHK)
9132 if (cdb->byte2 & SVFY_DPO)
9133 flags |= CTL_LLF_DPO;
9134 lba = scsi_4btoul(cdb->addr);
9135 num_blocks = scsi_4btoul(cdb->length);
9139 struct scsi_rw_16 *cdb;
9141 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9142 if (cdb->byte2 & SVFY_BYTCHK)
9144 if (cdb->byte2 & SVFY_DPO)
9145 flags |= CTL_LLF_DPO;
9146 lba = scsi_8btou64(cdb->addr);
9147 num_blocks = scsi_4btoul(cdb->length);
9152 * We got a command we don't support. This shouldn't
9153 * happen, commands should be filtered out above us.
9155 ctl_set_invalid_opcode(ctsio);
9156 ctl_done((union ctl_io *)ctsio);
9157 return (CTL_RETVAL_COMPLETE);
9161 * The first check is to make sure we're in bounds, the second
9162 * check is to catch wrap-around problems. If the lba + num blocks
9163 * is less than the lba, then we've wrapped around and the block
9164 * range is invalid anyway.
9166 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9167 || ((lba + num_blocks) < lba)) {
9168 ctl_set_lba_out_of_range(ctsio);
9169 ctl_done((union ctl_io *)ctsio);
9170 return (CTL_RETVAL_COMPLETE);
9174 * According to SBC-3, a transfer length of 0 is not an error.
9176 if (num_blocks == 0) {
9177 ctl_set_success(ctsio);
9178 ctl_done((union ctl_io *)ctsio);
9179 return (CTL_RETVAL_COMPLETE);
9182 lbalen = (struct ctl_lba_len_flags *)
9183 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9185 lbalen->len = num_blocks;
9187 lbalen->flags = CTL_LLF_COMPARE | flags;
9188 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9190 lbalen->flags = CTL_LLF_VERIFY | flags;
9191 ctsio->kern_total_len = 0;
9193 ctsio->kern_rel_offset = 0;
9195 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9196 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9201 ctl_report_luns(struct ctl_scsiio *ctsio)
9203 struct scsi_report_luns *cdb;
9204 struct scsi_report_luns_data *lun_data;
9205 struct ctl_lun *lun, *request_lun;
9206 int num_luns, retval;
9207 uint32_t alloc_len, lun_datalen;
9208 int num_filled, well_known;
9209 uint32_t initidx, targ_lun_id, lun_id;
9211 retval = CTL_RETVAL_COMPLETE;
9214 cdb = (struct scsi_report_luns *)ctsio->cdb;
9216 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9218 mtx_lock(&control_softc->ctl_lock);
9219 num_luns = control_softc->num_luns;
9220 mtx_unlock(&control_softc->ctl_lock);
9222 switch (cdb->select_report) {
9223 case RPL_REPORT_DEFAULT:
9224 case RPL_REPORT_ALL:
9226 case RPL_REPORT_WELLKNOWN:
9231 ctl_set_invalid_field(ctsio,
9237 ctl_done((union ctl_io *)ctsio);
9239 break; /* NOTREACHED */
9242 alloc_len = scsi_4btoul(cdb->length);
9244 * The initiator has to allocate at least 16 bytes for this request,
9245 * so he can at least get the header and the first LUN. Otherwise
9246 * we reject the request (per SPC-3 rev 14, section 6.21).
9248 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9249 sizeof(struct scsi_report_luns_lundata))) {
9250 ctl_set_invalid_field(ctsio,
9256 ctl_done((union ctl_io *)ctsio);
9260 request_lun = (struct ctl_lun *)
9261 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9263 lun_datalen = sizeof(*lun_data) +
9264 (num_luns * sizeof(struct scsi_report_luns_lundata));
9266 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9267 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9268 ctsio->kern_sg_entries = 0;
9270 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9272 mtx_lock(&control_softc->ctl_lock);
9273 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9274 lun_id = ctl_map_lun(ctsio->io_hdr.nexus.targ_port, targ_lun_id);
9275 if (lun_id >= CTL_MAX_LUNS)
9277 lun = control_softc->ctl_luns[lun_id];
9281 if (targ_lun_id <= 0xff) {
9283 * Peripheral addressing method, bus number 0.
9285 lun_data->luns[num_filled].lundata[0] =
9286 RPL_LUNDATA_ATYP_PERIPH;
9287 lun_data->luns[num_filled].lundata[1] = targ_lun_id;
9289 } else if (targ_lun_id <= 0x3fff) {
9291 * Flat addressing method.
9293 lun_data->luns[num_filled].lundata[0] =
9294 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8);
9295 lun_data->luns[num_filled].lundata[1] =
9296 (targ_lun_id & 0xff);
9298 } else if (targ_lun_id <= 0xffffff) {
9300 * Extended flat addressing method.
9302 lun_data->luns[num_filled].lundata[0] =
9303 RPL_LUNDATA_ATYP_EXTLUN | 0x12;
9304 scsi_ulto3b(targ_lun_id,
9305 &lun_data->luns[num_filled].lundata[1]);
9308 printf("ctl_report_luns: bogus LUN number %jd, "
9309 "skipping\n", (intmax_t)targ_lun_id);
9312 * According to SPC-3, rev 14 section 6.21:
9314 * "The execution of a REPORT LUNS command to any valid and
9315 * installed logical unit shall clear the REPORTED LUNS DATA
9316 * HAS CHANGED unit attention condition for all logical
9317 * units of that target with respect to the requesting
9318 * initiator. A valid and installed logical unit is one
9319 * having a PERIPHERAL QUALIFIER of 000b in the standard
9320 * INQUIRY data (see 6.4.2)."
9322 * If request_lun is NULL, the LUN this report luns command
9323 * was issued to is either disabled or doesn't exist. In that
9324 * case, we shouldn't clear any pending lun change unit
9327 if (request_lun != NULL) {
9328 mtx_lock(&lun->lun_lock);
9329 ctl_clr_ua(lun, initidx, CTL_UA_RES_RELEASE);
9330 mtx_unlock(&lun->lun_lock);
9333 mtx_unlock(&control_softc->ctl_lock);
9336 * It's quite possible that we've returned fewer LUNs than we allocated
9337 * space for. Trim it.
9339 lun_datalen = sizeof(*lun_data) +
9340 (num_filled * sizeof(struct scsi_report_luns_lundata));
9342 if (lun_datalen < alloc_len) {
9343 ctsio->residual = alloc_len - lun_datalen;
9344 ctsio->kern_data_len = lun_datalen;
9345 ctsio->kern_total_len = lun_datalen;
9347 ctsio->residual = 0;
9348 ctsio->kern_data_len = alloc_len;
9349 ctsio->kern_total_len = alloc_len;
9351 ctsio->kern_data_resid = 0;
9352 ctsio->kern_rel_offset = 0;
9353 ctsio->kern_sg_entries = 0;
9356 * We set this to the actual data length, regardless of how much
9357 * space we actually have to return results. If the user looks at
9358 * this value, he'll know whether or not he allocated enough space
9359 * and reissue the command if necessary. We don't support well
9360 * known logical units, so if the user asks for that, return none.
9362 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9365 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9368 ctl_set_success(ctsio);
9369 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9370 ctsio->be_move_done = ctl_config_move_done;
9371 ctl_datamove((union ctl_io *)ctsio);
9376 ctl_request_sense(struct ctl_scsiio *ctsio)
9378 struct scsi_request_sense *cdb;
9379 struct scsi_sense_data *sense_ptr;
9380 struct ctl_lun *lun;
9383 scsi_sense_data_type sense_format;
9384 ctl_ua_type ua_type;
9386 cdb = (struct scsi_request_sense *)ctsio->cdb;
9388 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9390 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9393 * Determine which sense format the user wants.
9395 if (cdb->byte2 & SRS_DESC)
9396 sense_format = SSD_TYPE_DESC;
9398 sense_format = SSD_TYPE_FIXED;
9400 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9401 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9402 ctsio->kern_sg_entries = 0;
9405 * struct scsi_sense_data, which is currently set to 256 bytes, is
9406 * larger than the largest allowed value for the length field in the
9407 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9409 ctsio->residual = 0;
9410 ctsio->kern_data_len = cdb->length;
9411 ctsio->kern_total_len = cdb->length;
9413 ctsio->kern_data_resid = 0;
9414 ctsio->kern_rel_offset = 0;
9415 ctsio->kern_sg_entries = 0;
9418 * If we don't have a LUN, we don't have any pending sense.
9424 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9426 * Check for pending sense, and then for pending unit attentions.
9427 * Pending sense gets returned first, then pending unit attentions.
9429 mtx_lock(&lun->lun_lock);
9431 if (ctl_is_set(lun->have_ca, initidx)) {
9432 scsi_sense_data_type stored_format;
9435 * Check to see which sense format was used for the stored
9438 stored_format = scsi_sense_type(&lun->pending_sense[initidx]);
9441 * If the user requested a different sense format than the
9442 * one we stored, then we need to convert it to the other
9443 * format. If we're going from descriptor to fixed format
9444 * sense data, we may lose things in translation, depending
9445 * on what options were used.
9447 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9448 * for some reason we'll just copy it out as-is.
9450 if ((stored_format == SSD_TYPE_FIXED)
9451 && (sense_format == SSD_TYPE_DESC))
9452 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9453 &lun->pending_sense[initidx],
9454 (struct scsi_sense_data_desc *)sense_ptr);
9455 else if ((stored_format == SSD_TYPE_DESC)
9456 && (sense_format == SSD_TYPE_FIXED))
9457 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9458 &lun->pending_sense[initidx],
9459 (struct scsi_sense_data_fixed *)sense_ptr);
9461 memcpy(sense_ptr, &lun->pending_sense[initidx],
9462 ctl_min(sizeof(*sense_ptr),
9463 sizeof(lun->pending_sense[initidx])));
9465 ctl_clear_mask(lun->have_ca, initidx);
9470 ua_type = ctl_build_ua(lun, initidx, sense_ptr, sense_format);
9471 if (ua_type != CTL_UA_NONE)
9474 mtx_unlock(&lun->lun_lock);
9477 * We already have a pending error, return it.
9479 if (have_error != 0) {
9481 * We report the SCSI status as OK, since the status of the
9482 * request sense command itself is OK.
9483 * We report 0 for the sense length, because we aren't doing
9484 * autosense in this case. We're reporting sense as
9487 ctl_set_success(ctsio);
9488 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9489 ctsio->be_move_done = ctl_config_move_done;
9490 ctl_datamove((union ctl_io *)ctsio);
9491 return (CTL_RETVAL_COMPLETE);
9497 * No sense information to report, so we report that everything is
9500 ctl_set_sense_data(sense_ptr,
9503 /*current_error*/ 1,
9504 /*sense_key*/ SSD_KEY_NO_SENSE,
9510 * We report 0 for the sense length, because we aren't doing
9511 * autosense in this case. We're reporting sense as parameter data.
9513 ctl_set_success(ctsio);
9514 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9515 ctsio->be_move_done = ctl_config_move_done;
9516 ctl_datamove((union ctl_io *)ctsio);
9517 return (CTL_RETVAL_COMPLETE);
9521 ctl_tur(struct ctl_scsiio *ctsio)
9524 CTL_DEBUG_PRINT(("ctl_tur\n"));
9526 ctl_set_success(ctsio);
9527 ctl_done((union ctl_io *)ctsio);
9529 return (CTL_RETVAL_COMPLETE);
9534 ctl_cmddt_inquiry(struct ctl_scsiio *ctsio)
9541 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9543 struct scsi_vpd_supported_pages *pages;
9545 struct ctl_lun *lun;
9547 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9549 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9550 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9551 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9552 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9553 ctsio->kern_sg_entries = 0;
9555 if (sup_page_size < alloc_len) {
9556 ctsio->residual = alloc_len - sup_page_size;
9557 ctsio->kern_data_len = sup_page_size;
9558 ctsio->kern_total_len = sup_page_size;
9560 ctsio->residual = 0;
9561 ctsio->kern_data_len = alloc_len;
9562 ctsio->kern_total_len = alloc_len;
9564 ctsio->kern_data_resid = 0;
9565 ctsio->kern_rel_offset = 0;
9566 ctsio->kern_sg_entries = 0;
9569 * The control device is always connected. The disk device, on the
9570 * other hand, may not be online all the time. Need to change this
9571 * to figure out whether the disk device is actually online or not.
9574 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9575 lun->be_lun->lun_type;
9577 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9579 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES;
9580 /* Supported VPD pages */
9581 pages->page_list[0] = SVPD_SUPPORTED_PAGES;
9583 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER;
9584 /* Device Identification */
9585 pages->page_list[2] = SVPD_DEVICE_ID;
9586 /* Extended INQUIRY Data */
9587 pages->page_list[3] = SVPD_EXTENDED_INQUIRY_DATA;
9588 /* Mode Page Policy */
9589 pages->page_list[4] = SVPD_MODE_PAGE_POLICY;
9591 pages->page_list[5] = SVPD_SCSI_PORTS;
9592 /* Third-party Copy */
9593 pages->page_list[6] = SVPD_SCSI_TPC;
9595 pages->page_list[7] = SVPD_BLOCK_LIMITS;
9596 /* Block Device Characteristics */
9597 pages->page_list[8] = SVPD_BDC;
9598 /* Logical Block Provisioning */
9599 pages->page_list[9] = SVPD_LBP;
9601 ctl_set_success(ctsio);
9602 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9603 ctsio->be_move_done = ctl_config_move_done;
9604 ctl_datamove((union ctl_io *)ctsio);
9605 return (CTL_RETVAL_COMPLETE);
9609 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9611 struct scsi_vpd_unit_serial_number *sn_ptr;
9612 struct ctl_lun *lun;
9615 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9617 data_len = 4 + CTL_SN_LEN;
9618 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9619 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9620 if (data_len < alloc_len) {
9621 ctsio->residual = alloc_len - data_len;
9622 ctsio->kern_data_len = data_len;
9623 ctsio->kern_total_len = data_len;
9625 ctsio->residual = 0;
9626 ctsio->kern_data_len = alloc_len;
9627 ctsio->kern_total_len = alloc_len;
9629 ctsio->kern_data_resid = 0;
9630 ctsio->kern_rel_offset = 0;
9631 ctsio->kern_sg_entries = 0;
9634 * The control device is always connected. The disk device, on the
9635 * other hand, may not be online all the time. Need to change this
9636 * to figure out whether the disk device is actually online or not.
9639 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9640 lun->be_lun->lun_type;
9642 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9644 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9645 sn_ptr->length = CTL_SN_LEN;
9647 * If we don't have a LUN, we just leave the serial number as
9651 strncpy((char *)sn_ptr->serial_num,
9652 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9654 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9656 ctl_set_success(ctsio);
9657 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9658 ctsio->be_move_done = ctl_config_move_done;
9659 ctl_datamove((union ctl_io *)ctsio);
9660 return (CTL_RETVAL_COMPLETE);
9665 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9667 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9668 struct ctl_lun *lun;
9671 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9673 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9674 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9675 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9676 ctsio->kern_sg_entries = 0;
9678 if (data_len < alloc_len) {
9679 ctsio->residual = alloc_len - data_len;
9680 ctsio->kern_data_len = data_len;
9681 ctsio->kern_total_len = data_len;
9683 ctsio->residual = 0;
9684 ctsio->kern_data_len = alloc_len;
9685 ctsio->kern_total_len = alloc_len;
9687 ctsio->kern_data_resid = 0;
9688 ctsio->kern_rel_offset = 0;
9689 ctsio->kern_sg_entries = 0;
9692 * The control device is always connected. The disk device, on the
9693 * other hand, may not be online all the time.
9696 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9697 lun->be_lun->lun_type;
9699 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9700 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9701 eid_ptr->page_length = data_len - 4;
9702 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9703 eid_ptr->flags3 = SVPD_EID_V_SUP;
9705 ctl_set_success(ctsio);
9706 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9707 ctsio->be_move_done = ctl_config_move_done;
9708 ctl_datamove((union ctl_io *)ctsio);
9709 return (CTL_RETVAL_COMPLETE);
9713 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9715 struct scsi_vpd_mode_page_policy *mpp_ptr;
9716 struct ctl_lun *lun;
9719 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9721 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9722 sizeof(struct scsi_vpd_mode_page_policy_descr);
9724 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9725 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9726 ctsio->kern_sg_entries = 0;
9728 if (data_len < alloc_len) {
9729 ctsio->residual = alloc_len - data_len;
9730 ctsio->kern_data_len = data_len;
9731 ctsio->kern_total_len = data_len;
9733 ctsio->residual = 0;
9734 ctsio->kern_data_len = alloc_len;
9735 ctsio->kern_total_len = alloc_len;
9737 ctsio->kern_data_resid = 0;
9738 ctsio->kern_rel_offset = 0;
9739 ctsio->kern_sg_entries = 0;
9742 * The control device is always connected. The disk device, on the
9743 * other hand, may not be online all the time.
9746 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9747 lun->be_lun->lun_type;
9749 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9750 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9751 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9752 mpp_ptr->descr[0].page_code = 0x3f;
9753 mpp_ptr->descr[0].subpage_code = 0xff;
9754 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9756 ctl_set_success(ctsio);
9757 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9758 ctsio->be_move_done = ctl_config_move_done;
9759 ctl_datamove((union ctl_io *)ctsio);
9760 return (CTL_RETVAL_COMPLETE);
9764 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9766 struct scsi_vpd_device_id *devid_ptr;
9767 struct scsi_vpd_id_descriptor *desc;
9768 struct ctl_softc *ctl_softc;
9769 struct ctl_lun *lun;
9770 struct ctl_port *port;
9774 ctl_softc = control_softc;
9776 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)];
9777 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9779 data_len = sizeof(struct scsi_vpd_device_id) +
9780 sizeof(struct scsi_vpd_id_descriptor) +
9781 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9782 sizeof(struct scsi_vpd_id_descriptor) +
9783 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9784 if (lun && lun->lun_devid)
9785 data_len += lun->lun_devid->len;
9786 if (port->port_devid)
9787 data_len += port->port_devid->len;
9788 if (port->target_devid)
9789 data_len += port->target_devid->len;
9791 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9792 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9793 ctsio->kern_sg_entries = 0;
9795 if (data_len < alloc_len) {
9796 ctsio->residual = alloc_len - data_len;
9797 ctsio->kern_data_len = data_len;
9798 ctsio->kern_total_len = data_len;
9800 ctsio->residual = 0;
9801 ctsio->kern_data_len = alloc_len;
9802 ctsio->kern_total_len = alloc_len;
9804 ctsio->kern_data_resid = 0;
9805 ctsio->kern_rel_offset = 0;
9806 ctsio->kern_sg_entries = 0;
9809 * The control device is always connected. The disk device, on the
9810 * other hand, may not be online all the time.
9813 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9814 lun->be_lun->lun_type;
9816 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9817 devid_ptr->page_code = SVPD_DEVICE_ID;
9818 scsi_ulto2b(data_len - 4, devid_ptr->length);
9820 if (port->port_type == CTL_PORT_FC)
9821 proto = SCSI_PROTO_FC << 4;
9822 else if (port->port_type == CTL_PORT_ISCSI)
9823 proto = SCSI_PROTO_ISCSI << 4;
9825 proto = SCSI_PROTO_SPI << 4;
9826 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9829 * We're using a LUN association here. i.e., this device ID is a
9830 * per-LUN identifier.
9832 if (lun && lun->lun_devid) {
9833 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9834 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9835 lun->lun_devid->len);
9839 * This is for the WWPN which is a port association.
9841 if (port->port_devid) {
9842 memcpy(desc, port->port_devid->data, port->port_devid->len);
9843 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9844 port->port_devid->len);
9848 * This is for the Relative Target Port(type 4h) identifier
9850 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9851 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9852 SVPD_ID_TYPE_RELTARG;
9854 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9855 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9856 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9859 * This is for the Target Port Group(type 5h) identifier
9861 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9862 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9863 SVPD_ID_TYPE_TPORTGRP;
9865 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1,
9866 &desc->identifier[2]);
9867 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9868 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9871 * This is for the Target identifier
9873 if (port->target_devid) {
9874 memcpy(desc, port->target_devid->data, port->target_devid->len);
9877 ctl_set_success(ctsio);
9878 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9879 ctsio->be_move_done = ctl_config_move_done;
9880 ctl_datamove((union ctl_io *)ctsio);
9881 return (CTL_RETVAL_COMPLETE);
9885 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9887 struct ctl_softc *softc = control_softc;
9888 struct scsi_vpd_scsi_ports *sp;
9889 struct scsi_vpd_port_designation *pd;
9890 struct scsi_vpd_port_designation_cont *pdc;
9891 struct ctl_lun *lun;
9892 struct ctl_port *port;
9893 int data_len, num_target_ports, iid_len, id_len, g, pg, p;
9894 int num_target_port_groups;
9896 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9898 if (softc->is_single)
9899 num_target_port_groups = 1;
9901 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
9902 num_target_ports = 0;
9905 mtx_lock(&softc->ctl_lock);
9906 STAILQ_FOREACH(port, &softc->port_list, links) {
9907 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9910 ctl_map_lun_back(port->targ_port, lun->lun) >=
9914 if (port->init_devid)
9915 iid_len += port->init_devid->len;
9916 if (port->port_devid)
9917 id_len += port->port_devid->len;
9919 mtx_unlock(&softc->ctl_lock);
9921 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups *
9922 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9923 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9924 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9925 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9926 ctsio->kern_sg_entries = 0;
9928 if (data_len < alloc_len) {
9929 ctsio->residual = alloc_len - data_len;
9930 ctsio->kern_data_len = data_len;
9931 ctsio->kern_total_len = data_len;
9933 ctsio->residual = 0;
9934 ctsio->kern_data_len = alloc_len;
9935 ctsio->kern_total_len = alloc_len;
9937 ctsio->kern_data_resid = 0;
9938 ctsio->kern_rel_offset = 0;
9939 ctsio->kern_sg_entries = 0;
9942 * The control device is always connected. The disk device, on the
9943 * other hand, may not be online all the time. Need to change this
9944 * to figure out whether the disk device is actually online or not.
9947 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9948 lun->be_lun->lun_type;
9950 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9952 sp->page_code = SVPD_SCSI_PORTS;
9953 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9955 pd = &sp->design[0];
9957 mtx_lock(&softc->ctl_lock);
9958 pg = softc->port_offset / CTL_MAX_PORTS;
9959 for (g = 0; g < num_target_port_groups; g++) {
9960 STAILQ_FOREACH(port, &softc->port_list, links) {
9961 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9964 ctl_map_lun_back(port->targ_port, lun->lun) >=
9967 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS;
9968 scsi_ulto2b(p, pd->relative_port_id);
9969 if (port->init_devid && g == pg) {
9970 iid_len = port->init_devid->len;
9971 memcpy(pd->initiator_transportid,
9972 port->init_devid->data, port->init_devid->len);
9975 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9976 pdc = (struct scsi_vpd_port_designation_cont *)
9977 (&pd->initiator_transportid[iid_len]);
9978 if (port->port_devid && g == pg) {
9979 id_len = port->port_devid->len;
9980 memcpy(pdc->target_port_descriptors,
9981 port->port_devid->data, port->port_devid->len);
9984 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9985 pd = (struct scsi_vpd_port_designation *)
9986 ((uint8_t *)pdc->target_port_descriptors + id_len);
9989 mtx_unlock(&softc->ctl_lock);
9991 ctl_set_success(ctsio);
9992 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9993 ctsio->be_move_done = ctl_config_move_done;
9994 ctl_datamove((union ctl_io *)ctsio);
9995 return (CTL_RETVAL_COMPLETE);
9999 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
10001 struct scsi_vpd_block_limits *bl_ptr;
10002 struct ctl_lun *lun;
10005 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10007 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
10008 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
10009 ctsio->kern_sg_entries = 0;
10011 if (sizeof(*bl_ptr) < alloc_len) {
10012 ctsio->residual = alloc_len - sizeof(*bl_ptr);
10013 ctsio->kern_data_len = sizeof(*bl_ptr);
10014 ctsio->kern_total_len = sizeof(*bl_ptr);
10016 ctsio->residual = 0;
10017 ctsio->kern_data_len = alloc_len;
10018 ctsio->kern_total_len = alloc_len;
10020 ctsio->kern_data_resid = 0;
10021 ctsio->kern_rel_offset = 0;
10022 ctsio->kern_sg_entries = 0;
10025 * The control device is always connected. The disk device, on the
10026 * other hand, may not be online all the time. Need to change this
10027 * to figure out whether the disk device is actually online or not.
10030 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10031 lun->be_lun->lun_type;
10033 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10035 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
10036 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
10037 bl_ptr->max_cmp_write_len = 0xff;
10038 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
10040 bs = lun->be_lun->blocksize;
10041 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len);
10042 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10043 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
10044 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
10045 if (lun->be_lun->pblockexp != 0) {
10046 scsi_ulto4b((1 << lun->be_lun->pblockexp),
10047 bl_ptr->opt_unmap_grain);
10048 scsi_ulto4b(0x80000000 | lun->be_lun->pblockoff,
10049 bl_ptr->unmap_grain_align);
10052 scsi_ulto4b(lun->be_lun->atomicblock,
10053 bl_ptr->max_atomic_transfer_length);
10054 scsi_ulto4b(0, bl_ptr->atomic_alignment);
10055 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
10057 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
10059 ctl_set_success(ctsio);
10060 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10061 ctsio->be_move_done = ctl_config_move_done;
10062 ctl_datamove((union ctl_io *)ctsio);
10063 return (CTL_RETVAL_COMPLETE);
10067 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
10069 struct scsi_vpd_block_device_characteristics *bdc_ptr;
10070 struct ctl_lun *lun;
10074 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10076 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
10077 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
10078 ctsio->kern_sg_entries = 0;
10080 if (sizeof(*bdc_ptr) < alloc_len) {
10081 ctsio->residual = alloc_len - sizeof(*bdc_ptr);
10082 ctsio->kern_data_len = sizeof(*bdc_ptr);
10083 ctsio->kern_total_len = sizeof(*bdc_ptr);
10085 ctsio->residual = 0;
10086 ctsio->kern_data_len = alloc_len;
10087 ctsio->kern_total_len = alloc_len;
10089 ctsio->kern_data_resid = 0;
10090 ctsio->kern_rel_offset = 0;
10091 ctsio->kern_sg_entries = 0;
10094 * The control device is always connected. The disk device, on the
10095 * other hand, may not be online all the time. Need to change this
10096 * to figure out whether the disk device is actually online or not.
10099 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10100 lun->be_lun->lun_type;
10102 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10103 bdc_ptr->page_code = SVPD_BDC;
10104 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
10106 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL)
10107 i = strtol(value, NULL, 0);
10109 i = CTL_DEFAULT_ROTATION_RATE;
10110 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
10112 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL)
10113 i = strtol(value, NULL, 0);
10116 bdc_ptr->wab_wac_ff = (i & 0x0f);
10117 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
10119 ctl_set_success(ctsio);
10120 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10121 ctsio->be_move_done = ctl_config_move_done;
10122 ctl_datamove((union ctl_io *)ctsio);
10123 return (CTL_RETVAL_COMPLETE);
10127 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
10129 struct scsi_vpd_logical_block_prov *lbp_ptr;
10130 struct ctl_lun *lun;
10132 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10134 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
10135 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
10136 ctsio->kern_sg_entries = 0;
10138 if (sizeof(*lbp_ptr) < alloc_len) {
10139 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
10140 ctsio->kern_data_len = sizeof(*lbp_ptr);
10141 ctsio->kern_total_len = sizeof(*lbp_ptr);
10143 ctsio->residual = 0;
10144 ctsio->kern_data_len = alloc_len;
10145 ctsio->kern_total_len = alloc_len;
10147 ctsio->kern_data_resid = 0;
10148 ctsio->kern_rel_offset = 0;
10149 ctsio->kern_sg_entries = 0;
10152 * The control device is always connected. The disk device, on the
10153 * other hand, may not be online all the time. Need to change this
10154 * to figure out whether the disk device is actually online or not.
10157 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10158 lun->be_lun->lun_type;
10160 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10162 lbp_ptr->page_code = SVPD_LBP;
10163 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
10164 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10165 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
10166 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
10167 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
10168 lbp_ptr->prov_type = SVPD_LBP_THIN;
10171 ctl_set_success(ctsio);
10172 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10173 ctsio->be_move_done = ctl_config_move_done;
10174 ctl_datamove((union ctl_io *)ctsio);
10175 return (CTL_RETVAL_COMPLETE);
10179 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
10181 struct scsi_inquiry *cdb;
10182 int alloc_len, retval;
10184 cdb = (struct scsi_inquiry *)ctsio->cdb;
10186 retval = CTL_RETVAL_COMPLETE;
10188 alloc_len = scsi_2btoul(cdb->length);
10190 switch (cdb->page_code) {
10191 case SVPD_SUPPORTED_PAGES:
10192 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
10194 case SVPD_UNIT_SERIAL_NUMBER:
10195 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
10197 case SVPD_DEVICE_ID:
10198 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
10200 case SVPD_EXTENDED_INQUIRY_DATA:
10201 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
10203 case SVPD_MODE_PAGE_POLICY:
10204 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
10206 case SVPD_SCSI_PORTS:
10207 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10209 case SVPD_SCSI_TPC:
10210 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10212 case SVPD_BLOCK_LIMITS:
10213 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10216 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10219 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10222 ctl_set_invalid_field(ctsio,
10228 ctl_done((union ctl_io *)ctsio);
10229 retval = CTL_RETVAL_COMPLETE;
10237 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10239 struct scsi_inquiry_data *inq_ptr;
10240 struct scsi_inquiry *cdb;
10241 struct ctl_softc *ctl_softc;
10242 struct ctl_lun *lun;
10244 uint32_t alloc_len, data_len;
10245 ctl_port_type port_type;
10247 ctl_softc = control_softc;
10250 * Figure out whether we're talking to a Fibre Channel port or not.
10251 * We treat the ioctl front end, and any SCSI adapters, as packetized
10254 port_type = ctl_softc->ctl_ports[
10255 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type;
10256 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10257 port_type = CTL_PORT_SCSI;
10259 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10260 cdb = (struct scsi_inquiry *)ctsio->cdb;
10261 alloc_len = scsi_2btoul(cdb->length);
10264 * We malloc the full inquiry data size here and fill it
10265 * in. If the user only asks for less, we'll give him
10268 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10269 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10270 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10271 ctsio->kern_sg_entries = 0;
10272 ctsio->kern_data_resid = 0;
10273 ctsio->kern_rel_offset = 0;
10275 if (data_len < alloc_len) {
10276 ctsio->residual = alloc_len - data_len;
10277 ctsio->kern_data_len = data_len;
10278 ctsio->kern_total_len = data_len;
10280 ctsio->residual = 0;
10281 ctsio->kern_data_len = alloc_len;
10282 ctsio->kern_total_len = alloc_len;
10286 * If we have a LUN configured, report it as connected. Otherwise,
10287 * report that it is offline or no device is supported, depending
10288 * on the value of inquiry_pq_no_lun.
10290 * According to the spec (SPC-4 r34), the peripheral qualifier
10291 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario:
10293 * "A peripheral device having the specified peripheral device type
10294 * is not connected to this logical unit. However, the device
10295 * server is capable of supporting the specified peripheral device
10296 * type on this logical unit."
10298 * According to the same spec, the peripheral qualifier
10299 * SID_QUAL_BAD_LU (011b) is used in this scenario:
10301 * "The device server is not capable of supporting a peripheral
10302 * device on this logical unit. For this peripheral qualifier the
10303 * peripheral device type shall be set to 1Fh. All other peripheral
10304 * device type values are reserved for this peripheral qualifier."
10306 * Given the text, it would seem that we probably want to report that
10307 * the LUN is offline here. There is no LUN connected, but we can
10308 * support a LUN at the given LUN number.
10310 * In the real world, though, it sounds like things are a little
10313 * - Linux, when presented with a LUN with the offline peripheral
10314 * qualifier, will create an sg driver instance for it. So when
10315 * you attach it to CTL, you wind up with a ton of sg driver
10316 * instances. (One for every LUN that Linux bothered to probe.)
10317 * Linux does this despite the fact that it issues a REPORT LUNs
10318 * to LUN 0 to get the inventory of supported LUNs.
10320 * - There is other anecdotal evidence (from Emulex folks) about
10321 * arrays that use the offline peripheral qualifier for LUNs that
10322 * are on the "passive" path in an active/passive array.
10324 * So the solution is provide a hopefully reasonable default
10325 * (return bad/no LUN) and allow the user to change the behavior
10326 * with a tunable/sysctl variable.
10329 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10330 lun->be_lun->lun_type;
10331 else if (ctl_softc->inquiry_pq_no_lun == 0)
10332 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10334 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10336 /* RMB in byte 2 is 0 */
10337 inq_ptr->version = SCSI_REV_SPC4;
10340 * According to SAM-3, even if a device only supports a single
10341 * level of LUN addressing, it should still set the HISUP bit:
10343 * 4.9.1 Logical unit numbers overview
10345 * All logical unit number formats described in this standard are
10346 * hierarchical in structure even when only a single level in that
10347 * hierarchy is used. The HISUP bit shall be set to one in the
10348 * standard INQUIRY data (see SPC-2) when any logical unit number
10349 * format described in this standard is used. Non-hierarchical
10350 * formats are outside the scope of this standard.
10352 * Therefore we set the HiSup bit here.
10354 * The reponse format is 2, per SPC-3.
10356 inq_ptr->response_format = SID_HiSup | 2;
10358 inq_ptr->additional_length = data_len -
10359 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10360 CTL_DEBUG_PRINT(("additional_length = %d\n",
10361 inq_ptr->additional_length));
10363 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10364 /* 16 bit addressing */
10365 if (port_type == CTL_PORT_SCSI)
10366 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10367 /* XXX set the SID_MultiP bit here if we're actually going to
10368 respond on multiple ports */
10369 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10371 /* 16 bit data bus, synchronous transfers */
10372 if (port_type == CTL_PORT_SCSI)
10373 inq_ptr->flags = SID_WBus16 | SID_Sync;
10375 * XXX KDM do we want to support tagged queueing on the control
10379 || (lun->be_lun->lun_type != T_PROCESSOR))
10380 inq_ptr->flags |= SID_CmdQue;
10382 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10383 * We have 8 bytes for the vendor name, and 16 bytes for the device
10384 * name and 4 bytes for the revision.
10386 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10387 "vendor")) == NULL) {
10388 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10390 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10391 strncpy(inq_ptr->vendor, val,
10392 min(sizeof(inq_ptr->vendor), strlen(val)));
10395 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10396 sizeof(inq_ptr->product));
10397 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10398 switch (lun->be_lun->lun_type) {
10400 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10401 sizeof(inq_ptr->product));
10404 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10405 sizeof(inq_ptr->product));
10408 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10409 sizeof(inq_ptr->product));
10413 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10414 strncpy(inq_ptr->product, val,
10415 min(sizeof(inq_ptr->product), strlen(val)));
10419 * XXX make this a macro somewhere so it automatically gets
10420 * incremented when we make changes.
10422 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10423 "revision")) == NULL) {
10424 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10426 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10427 strncpy(inq_ptr->revision, val,
10428 min(sizeof(inq_ptr->revision), strlen(val)));
10432 * For parallel SCSI, we support double transition and single
10433 * transition clocking. We also support QAS (Quick Arbitration
10434 * and Selection) and Information Unit transfers on both the
10435 * control and array devices.
10437 if (port_type == CTL_PORT_SCSI)
10438 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10441 /* SAM-5 (no version claimed) */
10442 scsi_ulto2b(0x00A0, inq_ptr->version1);
10443 /* SPC-4 (no version claimed) */
10444 scsi_ulto2b(0x0460, inq_ptr->version2);
10445 if (port_type == CTL_PORT_FC) {
10446 /* FCP-2 ANSI INCITS.350:2003 */
10447 scsi_ulto2b(0x0917, inq_ptr->version3);
10448 } else if (port_type == CTL_PORT_SCSI) {
10449 /* SPI-4 ANSI INCITS.362:200x */
10450 scsi_ulto2b(0x0B56, inq_ptr->version3);
10451 } else if (port_type == CTL_PORT_ISCSI) {
10452 /* iSCSI (no version claimed) */
10453 scsi_ulto2b(0x0960, inq_ptr->version3);
10454 } else if (port_type == CTL_PORT_SAS) {
10455 /* SAS (no version claimed) */
10456 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10460 /* SBC-4 (no version claimed) */
10461 scsi_ulto2b(0x0600, inq_ptr->version4);
10463 switch (lun->be_lun->lun_type) {
10465 /* SBC-4 (no version claimed) */
10466 scsi_ulto2b(0x0600, inq_ptr->version4);
10474 ctl_set_success(ctsio);
10475 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10476 ctsio->be_move_done = ctl_config_move_done;
10477 ctl_datamove((union ctl_io *)ctsio);
10478 return (CTL_RETVAL_COMPLETE);
10482 ctl_inquiry(struct ctl_scsiio *ctsio)
10484 struct scsi_inquiry *cdb;
10487 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10489 cdb = (struct scsi_inquiry *)ctsio->cdb;
10490 if (cdb->byte2 & SI_EVPD)
10491 retval = ctl_inquiry_evpd(ctsio);
10492 else if (cdb->page_code == 0)
10493 retval = ctl_inquiry_std(ctsio);
10495 ctl_set_invalid_field(ctsio,
10501 ctl_done((union ctl_io *)ctsio);
10502 return (CTL_RETVAL_COMPLETE);
10509 * For known CDB types, parse the LBA and length.
10512 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10514 if (io->io_hdr.io_type != CTL_IO_SCSI)
10517 switch (io->scsiio.cdb[0]) {
10518 case COMPARE_AND_WRITE: {
10519 struct scsi_compare_and_write *cdb;
10521 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10523 *lba = scsi_8btou64(cdb->addr);
10524 *len = cdb->length;
10529 struct scsi_rw_6 *cdb;
10531 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10533 *lba = scsi_3btoul(cdb->addr);
10534 /* only 5 bits are valid in the most significant address byte */
10536 *len = cdb->length;
10541 struct scsi_rw_10 *cdb;
10543 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10545 *lba = scsi_4btoul(cdb->addr);
10546 *len = scsi_2btoul(cdb->length);
10549 case WRITE_VERIFY_10: {
10550 struct scsi_write_verify_10 *cdb;
10552 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10554 *lba = scsi_4btoul(cdb->addr);
10555 *len = scsi_2btoul(cdb->length);
10560 struct scsi_rw_12 *cdb;
10562 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10564 *lba = scsi_4btoul(cdb->addr);
10565 *len = scsi_4btoul(cdb->length);
10568 case WRITE_VERIFY_12: {
10569 struct scsi_write_verify_12 *cdb;
10571 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10573 *lba = scsi_4btoul(cdb->addr);
10574 *len = scsi_4btoul(cdb->length);
10579 case WRITE_ATOMIC_16: {
10580 struct scsi_rw_16 *cdb;
10582 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10584 *lba = scsi_8btou64(cdb->addr);
10585 *len = scsi_4btoul(cdb->length);
10588 case WRITE_VERIFY_16: {
10589 struct scsi_write_verify_16 *cdb;
10591 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10593 *lba = scsi_8btou64(cdb->addr);
10594 *len = scsi_4btoul(cdb->length);
10597 case WRITE_SAME_10: {
10598 struct scsi_write_same_10 *cdb;
10600 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10602 *lba = scsi_4btoul(cdb->addr);
10603 *len = scsi_2btoul(cdb->length);
10606 case WRITE_SAME_16: {
10607 struct scsi_write_same_16 *cdb;
10609 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10611 *lba = scsi_8btou64(cdb->addr);
10612 *len = scsi_4btoul(cdb->length);
10616 struct scsi_verify_10 *cdb;
10618 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10620 *lba = scsi_4btoul(cdb->addr);
10621 *len = scsi_2btoul(cdb->length);
10625 struct scsi_verify_12 *cdb;
10627 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10629 *lba = scsi_4btoul(cdb->addr);
10630 *len = scsi_4btoul(cdb->length);
10634 struct scsi_verify_16 *cdb;
10636 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10638 *lba = scsi_8btou64(cdb->addr);
10639 *len = scsi_4btoul(cdb->length);
10649 break; /* NOTREACHED */
10656 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2)
10658 uint64_t endlba1, endlba2;
10660 endlba1 = lba1 + len1 - 1;
10661 endlba2 = lba2 + len2 - 1;
10663 if ((endlba1 < lba2)
10664 || (endlba2 < lba1))
10665 return (CTL_ACTION_PASS);
10667 return (CTL_ACTION_BLOCK);
10671 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10673 struct ctl_ptr_len_flags *ptrlen;
10674 struct scsi_unmap_desc *buf, *end, *range;
10678 /* If not UNMAP -- go other way. */
10679 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10680 io->scsiio.cdb[0] != UNMAP)
10681 return (CTL_ACTION_ERROR);
10683 /* If UNMAP without data -- block and wait for data. */
10684 ptrlen = (struct ctl_ptr_len_flags *)
10685 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10686 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10687 ptrlen->ptr == NULL)
10688 return (CTL_ACTION_BLOCK);
10690 /* UNMAP with data -- check for collision. */
10691 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10692 end = buf + ptrlen->len / sizeof(*buf);
10693 for (range = buf; range < end; range++) {
10694 lba = scsi_8btou64(range->lba);
10695 len = scsi_4btoul(range->length);
10696 if ((lba < lba2 + len2) && (lba + len > lba2))
10697 return (CTL_ACTION_BLOCK);
10699 return (CTL_ACTION_PASS);
10703 ctl_extent_check(union ctl_io *io1, union ctl_io *io2)
10705 uint64_t lba1, lba2;
10706 uint64_t len1, len2;
10709 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10710 return (CTL_ACTION_ERROR);
10712 retval = ctl_extent_check_unmap(io2, lba1, len1);
10713 if (retval != CTL_ACTION_ERROR)
10716 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10717 return (CTL_ACTION_ERROR);
10719 return (ctl_extent_check_lba(lba1, len1, lba2, len2));
10723 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10724 union ctl_io *ooa_io)
10726 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10727 ctl_serialize_action *serialize_row;
10730 * The initiator attempted multiple untagged commands at the same
10731 * time. Can't do that.
10733 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10734 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10735 && ((pending_io->io_hdr.nexus.targ_port ==
10736 ooa_io->io_hdr.nexus.targ_port)
10737 && (pending_io->io_hdr.nexus.initid.id ==
10738 ooa_io->io_hdr.nexus.initid.id))
10739 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10740 return (CTL_ACTION_OVERLAP);
10743 * The initiator attempted to send multiple tagged commands with
10744 * the same ID. (It's fine if different initiators have the same
10747 * Even if all of those conditions are true, we don't kill the I/O
10748 * if the command ahead of us has been aborted. We won't end up
10749 * sending it to the FETD, and it's perfectly legal to resend a
10750 * command with the same tag number as long as the previous
10751 * instance of this tag number has been aborted somehow.
10753 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10754 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10755 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10756 && ((pending_io->io_hdr.nexus.targ_port ==
10757 ooa_io->io_hdr.nexus.targ_port)
10758 && (pending_io->io_hdr.nexus.initid.id ==
10759 ooa_io->io_hdr.nexus.initid.id))
10760 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10761 return (CTL_ACTION_OVERLAP_TAG);
10764 * If we get a head of queue tag, SAM-3 says that we should
10765 * immediately execute it.
10767 * What happens if this command would normally block for some other
10768 * reason? e.g. a request sense with a head of queue tag
10769 * immediately after a write. Normally that would block, but this
10770 * will result in its getting executed immediately...
10772 * We currently return "pass" instead of "skip", so we'll end up
10773 * going through the rest of the queue to check for overlapped tags.
10775 * XXX KDM check for other types of blockage first??
10777 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10778 return (CTL_ACTION_PASS);
10781 * Ordered tags have to block until all items ahead of them
10782 * have completed. If we get called with an ordered tag, we always
10783 * block, if something else is ahead of us in the queue.
10785 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10786 return (CTL_ACTION_BLOCK);
10789 * Simple tags get blocked until all head of queue and ordered tags
10790 * ahead of them have completed. I'm lumping untagged commands in
10791 * with simple tags here. XXX KDM is that the right thing to do?
10793 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10794 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10795 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10796 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10797 return (CTL_ACTION_BLOCK);
10799 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
10800 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
10802 serialize_row = ctl_serialize_table[ooa_entry->seridx];
10804 switch (serialize_row[pending_entry->seridx]) {
10805 case CTL_SER_BLOCK:
10806 return (CTL_ACTION_BLOCK);
10807 case CTL_SER_EXTENT:
10808 return (ctl_extent_check(pending_io, ooa_io));
10809 case CTL_SER_EXTENTOPT:
10810 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
10811 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
10812 return (ctl_extent_check(pending_io, ooa_io));
10815 return (CTL_ACTION_PASS);
10816 case CTL_SER_BLOCKOPT:
10817 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
10818 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
10819 return (CTL_ACTION_BLOCK);
10820 return (CTL_ACTION_PASS);
10822 return (CTL_ACTION_SKIP);
10824 panic("invalid serialization value %d",
10825 serialize_row[pending_entry->seridx]);
10828 return (CTL_ACTION_ERROR);
10832 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
10834 * - pending_io is generally either incoming, or on the blocked queue
10835 * - starting I/O is the I/O we want to start the check with.
10838 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
10839 union ctl_io *starting_io)
10841 union ctl_io *ooa_io;
10844 mtx_assert(&lun->lun_lock, MA_OWNED);
10847 * Run back along the OOA queue, starting with the current
10848 * blocked I/O and going through every I/O before it on the
10849 * queue. If starting_io is NULL, we'll just end up returning
10852 for (ooa_io = starting_io; ooa_io != NULL;
10853 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
10857 * This routine just checks to see whether
10858 * cur_blocked is blocked by ooa_io, which is ahead
10859 * of it in the queue. It doesn't queue/dequeue
10862 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
10864 case CTL_ACTION_BLOCK:
10865 case CTL_ACTION_OVERLAP:
10866 case CTL_ACTION_OVERLAP_TAG:
10867 case CTL_ACTION_SKIP:
10868 case CTL_ACTION_ERROR:
10870 break; /* NOTREACHED */
10871 case CTL_ACTION_PASS:
10874 panic("invalid action %d", action);
10875 break; /* NOTREACHED */
10879 return (CTL_ACTION_PASS);
10884 * - An I/O has just completed, and has been removed from the per-LUN OOA
10885 * queue, so some items on the blocked queue may now be unblocked.
10888 ctl_check_blocked(struct ctl_lun *lun)
10890 union ctl_io *cur_blocked, *next_blocked;
10892 mtx_assert(&lun->lun_lock, MA_OWNED);
10895 * Run forward from the head of the blocked queue, checking each
10896 * entry against the I/Os prior to it on the OOA queue to see if
10897 * there is still any blockage.
10899 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
10900 * with our removing a variable on it while it is traversing the
10903 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
10904 cur_blocked != NULL; cur_blocked = next_blocked) {
10905 union ctl_io *prev_ooa;
10908 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
10911 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
10912 ctl_ooaq, ooa_links);
10915 * If cur_blocked happens to be the first item in the OOA
10916 * queue now, prev_ooa will be NULL, and the action
10917 * returned will just be CTL_ACTION_PASS.
10919 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
10922 case CTL_ACTION_BLOCK:
10923 /* Nothing to do here, still blocked */
10925 case CTL_ACTION_OVERLAP:
10926 case CTL_ACTION_OVERLAP_TAG:
10928 * This shouldn't happen! In theory we've already
10929 * checked this command for overlap...
10932 case CTL_ACTION_PASS:
10933 case CTL_ACTION_SKIP: {
10934 struct ctl_softc *softc;
10935 const struct ctl_cmd_entry *entry;
10939 * The skip case shouldn't happen, this transaction
10940 * should have never made it onto the blocked queue.
10943 * This I/O is no longer blocked, we can remove it
10944 * from the blocked queue. Since this is a TAILQ
10945 * (doubly linked list), we can do O(1) removals
10946 * from any place on the list.
10948 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
10950 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
10952 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){
10954 * Need to send IO back to original side to
10957 union ctl_ha_msg msg_info;
10959 msg_info.hdr.original_sc =
10960 cur_blocked->io_hdr.original_sc;
10961 msg_info.hdr.serializing_sc = cur_blocked;
10962 msg_info.hdr.msg_type = CTL_MSG_R2R;
10963 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
10964 &msg_info, sizeof(msg_info), 0)) >
10965 CTL_HA_STATUS_SUCCESS) {
10966 printf("CTL:Check Blocked error from "
10967 "ctl_ha_msg_send %d\n",
10972 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
10973 softc = control_softc;
10976 * Check this I/O for LUN state changes that may
10977 * have happened while this command was blocked.
10978 * The LUN state may have been changed by a command
10979 * ahead of us in the queue, so we need to re-check
10980 * for any states that can be caused by SCSI
10983 if (ctl_scsiio_lun_check(softc, lun, entry,
10984 &cur_blocked->scsiio) == 0) {
10985 cur_blocked->io_hdr.flags |=
10986 CTL_FLAG_IS_WAS_ON_RTR;
10987 ctl_enqueue_rtr(cur_blocked);
10989 ctl_done(cur_blocked);
10994 * This probably shouldn't happen -- we shouldn't
10995 * get CTL_ACTION_ERROR, or anything else.
11001 return (CTL_RETVAL_COMPLETE);
11005 * This routine (with one exception) checks LUN flags that can be set by
11006 * commands ahead of us in the OOA queue. These flags have to be checked
11007 * when a command initially comes in, and when we pull a command off the
11008 * blocked queue and are preparing to execute it. The reason we have to
11009 * check these flags for commands on the blocked queue is that the LUN
11010 * state may have been changed by a command ahead of us while we're on the
11013 * Ordering is somewhat important with these checks, so please pay
11014 * careful attention to the placement of any new checks.
11017 ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
11018 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11025 mtx_assert(&lun->lun_lock, MA_OWNED);
11028 * If this shelf is a secondary shelf controller, we have to reject
11029 * any media access commands.
11031 if ((ctl_softc->flags & CTL_FLAG_ACTIVE_SHELF) == 0 &&
11032 (entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0) {
11033 ctl_set_lun_standby(ctsio);
11038 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11039 if (lun->flags & CTL_LUN_READONLY) {
11040 ctl_set_sense(ctsio, /*current_error*/ 1,
11041 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11042 /*asc*/ 0x27, /*ascq*/ 0x01, SSD_ELEM_NONE);
11046 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT]
11047 .eca_and_aen & SCP_SWP) != 0) {
11048 ctl_set_sense(ctsio, /*current_error*/ 1,
11049 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11050 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11057 * Check for a reservation conflict. If this command isn't allowed
11058 * even on reserved LUNs, and if this initiator isn't the one who
11059 * reserved us, reject the command with a reservation conflict.
11061 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
11062 if ((lun->flags & CTL_LUN_RESERVED)
11063 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11064 if (lun->res_idx != residx) {
11065 ctl_set_reservation_conflict(ctsio);
11071 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11072 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11073 /* No reservation or command is allowed. */;
11074 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11075 (lun->res_type == SPR_TYPE_WR_EX ||
11076 lun->res_type == SPR_TYPE_WR_EX_RO ||
11077 lun->res_type == SPR_TYPE_WR_EX_AR)) {
11078 /* The command is allowed for Write Exclusive resv. */;
11081 * if we aren't registered or it's a res holder type
11082 * reservation and this isn't the res holder then set a
11085 if (ctl_get_prkey(lun, residx) == 0
11086 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
11087 ctl_set_reservation_conflict(ctsio);
11094 if ((lun->flags & CTL_LUN_OFFLINE)
11095 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) {
11096 ctl_set_lun_not_ready(ctsio);
11102 * If the LUN is stopped, see if this particular command is allowed
11103 * for a stopped lun. Otherwise, reject it with 0x04,0x02.
11105 if ((lun->flags & CTL_LUN_STOPPED)
11106 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
11107 /* "Logical unit not ready, initializing cmd. required" */
11108 ctl_set_lun_stopped(ctsio);
11113 if ((lun->flags & CTL_LUN_INOPERABLE)
11114 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
11115 /* "Medium format corrupted" */
11116 ctl_set_medium_format_corrupted(ctsio);
11127 ctl_failover_io(union ctl_io *io, int have_lock)
11129 ctl_set_busy(&io->scsiio);
11136 struct ctl_lun *lun;
11137 struct ctl_softc *ctl_softc;
11138 union ctl_io *next_io, *pending_io;
11142 ctl_softc = control_softc;
11144 mtx_lock(&ctl_softc->ctl_lock);
11146 * Remove any cmds from the other SC from the rtr queue. These
11147 * will obviously only be for LUNs for which we're the primary.
11148 * We can't send status or get/send data for these commands.
11149 * Since they haven't been executed yet, we can just remove them.
11150 * We'll either abort them or delete them below, depending on
11151 * which HA mode we're in.
11154 mtx_lock(&ctl_softc->queue_lock);
11155 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue);
11156 io != NULL; io = next_io) {
11157 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
11158 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
11159 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr,
11160 ctl_io_hdr, links);
11162 mtx_unlock(&ctl_softc->queue_lock);
11165 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) {
11166 lun = ctl_softc->ctl_luns[lun_idx];
11171 * Processor LUNs are primary on both sides.
11172 * XXX will this always be true?
11174 if (lun->be_lun->lun_type == T_PROCESSOR)
11177 if ((lun->flags & CTL_LUN_PRIMARY_SC)
11178 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
11179 printf("FAILOVER: primary lun %d\n", lun_idx);
11181 * Remove all commands from the other SC. First from the
11182 * blocked queue then from the ooa queue. Once we have
11183 * removed them. Call ctl_check_blocked to see if there
11184 * is anything that can run.
11186 for (io = (union ctl_io *)TAILQ_FIRST(
11187 &lun->blocked_queue); io != NULL; io = next_io) {
11189 next_io = (union ctl_io *)TAILQ_NEXT(
11190 &io->io_hdr, blocked_links);
11192 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
11193 TAILQ_REMOVE(&lun->blocked_queue,
11194 &io->io_hdr,blocked_links);
11195 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11196 TAILQ_REMOVE(&lun->ooa_queue,
11197 &io->io_hdr, ooa_links);
11203 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
11204 io != NULL; io = next_io) {
11206 next_io = (union ctl_io *)TAILQ_NEXT(
11207 &io->io_hdr, ooa_links);
11209 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
11211 TAILQ_REMOVE(&lun->ooa_queue,
11218 ctl_check_blocked(lun);
11219 } else if ((lun->flags & CTL_LUN_PRIMARY_SC)
11220 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
11222 printf("FAILOVER: primary lun %d\n", lun_idx);
11224 * Abort all commands from the other SC. We can't
11225 * send status back for them now. These should get
11226 * cleaned up when they are completed or come out
11227 * for a datamove operation.
11229 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
11230 io != NULL; io = next_io) {
11231 next_io = (union ctl_io *)TAILQ_NEXT(
11232 &io->io_hdr, ooa_links);
11234 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
11235 io->io_hdr.flags |= CTL_FLAG_ABORT;
11237 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
11238 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
11240 printf("FAILOVER: secondary lun %d\n", lun_idx);
11242 lun->flags |= CTL_LUN_PRIMARY_SC;
11245 * We send all I/O that was sent to this controller
11246 * and redirected to the other side back with
11247 * busy status, and have the initiator retry it.
11248 * Figuring out how much data has been transferred,
11249 * etc. and picking up where we left off would be
11252 * XXX KDM need to remove I/O from the blocked
11255 for (pending_io = (union ctl_io *)TAILQ_FIRST(
11256 &lun->ooa_queue); pending_io != NULL;
11257 pending_io = next_io) {
11259 next_io = (union ctl_io *)TAILQ_NEXT(
11260 &pending_io->io_hdr, ooa_links);
11262 pending_io->io_hdr.flags &=
11263 ~CTL_FLAG_SENT_2OTHER_SC;
11265 if (pending_io->io_hdr.flags &
11266 CTL_FLAG_IO_ACTIVE) {
11267 pending_io->io_hdr.flags |=
11270 ctl_set_busy(&pending_io->scsiio);
11271 ctl_done(pending_io);
11275 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
11276 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
11277 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
11278 printf("FAILOVER: secondary lun %d\n", lun_idx);
11280 * if the first io on the OOA is not on the RtR queue
11283 lun->flags |= CTL_LUN_PRIMARY_SC;
11285 pending_io = (union ctl_io *)TAILQ_FIRST(
11287 if (pending_io==NULL) {
11288 printf("Nothing on OOA queue\n");
11292 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11293 if ((pending_io->io_hdr.flags &
11294 CTL_FLAG_IS_WAS_ON_RTR) == 0) {
11295 pending_io->io_hdr.flags |=
11296 CTL_FLAG_IS_WAS_ON_RTR;
11297 ctl_enqueue_rtr(pending_io);
11302 printf("Tag 0x%04x is running\n",
11303 pending_io->scsiio.tag_num);
11307 next_io = (union ctl_io *)TAILQ_NEXT(
11308 &pending_io->io_hdr, ooa_links);
11309 for (pending_io=next_io; pending_io != NULL;
11310 pending_io = next_io) {
11311 pending_io->io_hdr.flags &=
11312 ~CTL_FLAG_SENT_2OTHER_SC;
11313 next_io = (union ctl_io *)TAILQ_NEXT(
11314 &pending_io->io_hdr, ooa_links);
11315 if (pending_io->io_hdr.flags &
11316 CTL_FLAG_IS_WAS_ON_RTR) {
11318 printf("Tag 0x%04x is running\n",
11319 pending_io->scsiio.tag_num);
11324 switch (ctl_check_ooa(lun, pending_io,
11325 (union ctl_io *)TAILQ_PREV(
11326 &pending_io->io_hdr, ctl_ooaq,
11329 case CTL_ACTION_BLOCK:
11330 TAILQ_INSERT_TAIL(&lun->blocked_queue,
11331 &pending_io->io_hdr,
11333 pending_io->io_hdr.flags |=
11336 case CTL_ACTION_PASS:
11337 case CTL_ACTION_SKIP:
11338 pending_io->io_hdr.flags |=
11339 CTL_FLAG_IS_WAS_ON_RTR;
11340 ctl_enqueue_rtr(pending_io);
11342 case CTL_ACTION_OVERLAP:
11343 ctl_set_overlapped_cmd(
11344 (struct ctl_scsiio *)pending_io);
11345 ctl_done(pending_io);
11347 case CTL_ACTION_OVERLAP_TAG:
11348 ctl_set_overlapped_tag(
11349 (struct ctl_scsiio *)pending_io,
11350 pending_io->scsiio.tag_num & 0xff);
11351 ctl_done(pending_io);
11353 case CTL_ACTION_ERROR:
11355 ctl_set_internal_failure(
11356 (struct ctl_scsiio *)pending_io,
11359 ctl_done(pending_io);
11364 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
11366 panic("Unhandled HA mode failover, LUN flags = %#x, "
11367 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode);
11371 mtx_unlock(&ctl_softc->ctl_lock);
11375 ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio)
11377 struct ctl_lun *lun;
11378 const struct ctl_cmd_entry *entry;
11379 uint32_t initidx, targ_lun;
11386 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11387 if ((targ_lun < CTL_MAX_LUNS)
11388 && ((lun = ctl_softc->ctl_luns[targ_lun]) != NULL)) {
11390 * If the LUN is invalid, pretend that it doesn't exist.
11391 * It will go away as soon as all pending I/O has been
11394 mtx_lock(&lun->lun_lock);
11395 if (lun->flags & CTL_LUN_DISABLED) {
11396 mtx_unlock(&lun->lun_lock);
11398 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11399 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11401 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
11402 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
11404 if (lun->be_lun->lun_type == T_PROCESSOR) {
11405 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV;
11409 * Every I/O goes into the OOA queue for a
11410 * particular LUN, and stays there until completion.
11412 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr,
11416 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11417 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11420 /* Get command entry and return error if it is unsuppotyed. */
11421 entry = ctl_validate_command(ctsio);
11422 if (entry == NULL) {
11424 mtx_unlock(&lun->lun_lock);
11428 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11429 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11432 * Check to see whether we can send this command to LUNs that don't
11433 * exist. This should pretty much only be the case for inquiry
11434 * and request sense. Further checks, below, really require having
11435 * a LUN, so we can't really check the command anymore. Just put
11436 * it on the rtr queue.
11439 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) {
11440 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11441 ctl_enqueue_rtr((union ctl_io *)ctsio);
11445 ctl_set_unsupported_lun(ctsio);
11446 ctl_done((union ctl_io *)ctsio);
11447 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11451 * Make sure we support this particular command on this LUN.
11452 * e.g., we don't support writes to the control LUN.
11454 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11455 mtx_unlock(&lun->lun_lock);
11456 ctl_set_invalid_opcode(ctsio);
11457 ctl_done((union ctl_io *)ctsio);
11462 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11466 * If we've got a request sense, it'll clear the contingent
11467 * allegiance condition. Otherwise, if we have a CA condition for
11468 * this initiator, clear it, because it sent down a command other
11469 * than request sense.
11471 if ((ctsio->cdb[0] != REQUEST_SENSE)
11472 && (ctl_is_set(lun->have_ca, initidx)))
11473 ctl_clear_mask(lun->have_ca, initidx);
11477 * If the command has this flag set, it handles its own unit
11478 * attention reporting, we shouldn't do anything. Otherwise we
11479 * check for any pending unit attentions, and send them back to the
11480 * initiator. We only do this when a command initially comes in,
11481 * not when we pull it off the blocked queue.
11483 * According to SAM-3, section 5.3.2, the order that things get
11484 * presented back to the host is basically unit attentions caused
11485 * by some sort of reset event, busy status, reservation conflicts
11486 * or task set full, and finally any other status.
11488 * One issue here is that some of the unit attentions we report
11489 * don't fall into the "reset" category (e.g. "reported luns data
11490 * has changed"). So reporting it here, before the reservation
11491 * check, may be technically wrong. I guess the only thing to do
11492 * would be to check for and report the reset events here, and then
11493 * check for the other unit attention types after we check for a
11494 * reservation conflict.
11496 * XXX KDM need to fix this
11498 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11499 ctl_ua_type ua_type;
11500 scsi_sense_data_type sense_format;
11502 if (lun->flags & CTL_LUN_SENSE_DESC)
11503 sense_format = SSD_TYPE_DESC;
11505 sense_format = SSD_TYPE_FIXED;
11507 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11509 if (ua_type != CTL_UA_NONE) {
11510 mtx_unlock(&lun->lun_lock);
11511 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11512 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11513 ctsio->sense_len = SSD_FULL_SIZE;
11514 ctl_done((union ctl_io *)ctsio);
11520 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) {
11521 mtx_unlock(&lun->lun_lock);
11522 ctl_done((union ctl_io *)ctsio);
11527 * XXX CHD this is where we want to send IO to other side if
11528 * this LUN is secondary on this SC. We will need to make a copy
11529 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11530 * the copy we send as FROM_OTHER.
11531 * We also need to stuff the address of the original IO so we can
11532 * find it easily. Something similar will need be done on the other
11533 * side so when we are done we can find the copy.
11535 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11536 union ctl_ha_msg msg_info;
11539 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11541 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11542 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11544 printf("1. ctsio %p\n", ctsio);
11546 msg_info.hdr.serializing_sc = NULL;
11547 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11548 msg_info.scsi.tag_num = ctsio->tag_num;
11549 msg_info.scsi.tag_type = ctsio->tag_type;
11550 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11552 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11554 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11555 (void *)&msg_info, sizeof(msg_info), 0)) >
11556 CTL_HA_STATUS_SUCCESS) {
11557 printf("CTL:precheck, ctl_ha_msg_send returned %d\n",
11559 printf("CTL:opcode is %x\n", ctsio->cdb[0]);
11562 printf("CTL:Precheck sent msg, opcode is %x\n",opcode);
11567 * XXX KDM this I/O is off the incoming queue, but hasn't
11568 * been inserted on any other queue. We may need to come
11569 * up with a holding queue while we wait for serialization
11570 * so that we have an idea of what we're waiting for from
11573 mtx_unlock(&lun->lun_lock);
11577 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11578 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11579 ctl_ooaq, ooa_links))) {
11580 case CTL_ACTION_BLOCK:
11581 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11582 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11584 mtx_unlock(&lun->lun_lock);
11586 case CTL_ACTION_PASS:
11587 case CTL_ACTION_SKIP:
11588 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11589 mtx_unlock(&lun->lun_lock);
11590 ctl_enqueue_rtr((union ctl_io *)ctsio);
11592 case CTL_ACTION_OVERLAP:
11593 mtx_unlock(&lun->lun_lock);
11594 ctl_set_overlapped_cmd(ctsio);
11595 ctl_done((union ctl_io *)ctsio);
11597 case CTL_ACTION_OVERLAP_TAG:
11598 mtx_unlock(&lun->lun_lock);
11599 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11600 ctl_done((union ctl_io *)ctsio);
11602 case CTL_ACTION_ERROR:
11604 mtx_unlock(&lun->lun_lock);
11605 ctl_set_internal_failure(ctsio,
11607 /*retry_count*/ 0);
11608 ctl_done((union ctl_io *)ctsio);
11614 const struct ctl_cmd_entry *
11615 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11617 const struct ctl_cmd_entry *entry;
11618 int service_action;
11620 entry = &ctl_cmd_table[ctsio->cdb[0]];
11622 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11623 if (entry->flags & CTL_CMD_FLAG_SA5) {
11624 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11625 entry = &((const struct ctl_cmd_entry *)
11626 entry->execute)[service_action];
11631 const struct ctl_cmd_entry *
11632 ctl_validate_command(struct ctl_scsiio *ctsio)
11634 const struct ctl_cmd_entry *entry;
11638 entry = ctl_get_cmd_entry(ctsio, &sa);
11639 if (entry->execute == NULL) {
11641 ctl_set_invalid_field(ctsio,
11648 ctl_set_invalid_opcode(ctsio);
11649 ctl_done((union ctl_io *)ctsio);
11652 KASSERT(entry->length > 0,
11653 ("Not defined length for command 0x%02x/0x%02x",
11654 ctsio->cdb[0], ctsio->cdb[1]));
11655 for (i = 1; i < entry->length; i++) {
11656 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11659 ctl_set_invalid_field(ctsio,
11664 /*bit*/ fls(diff) - 1);
11665 ctl_done((union ctl_io *)ctsio);
11672 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11675 switch (lun_type) {
11677 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) &&
11678 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11682 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) &&
11683 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11693 ctl_scsiio(struct ctl_scsiio *ctsio)
11696 const struct ctl_cmd_entry *entry;
11698 retval = CTL_RETVAL_COMPLETE;
11700 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11702 entry = ctl_get_cmd_entry(ctsio, NULL);
11705 * If this I/O has been aborted, just send it straight to
11706 * ctl_done() without executing it.
11708 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11709 ctl_done((union ctl_io *)ctsio);
11714 * All the checks should have been handled by ctl_scsiio_precheck().
11715 * We should be clear now to just execute the I/O.
11717 retval = entry->execute(ctsio);
11724 * Since we only implement one target right now, a bus reset simply resets
11725 * our single target.
11728 ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io)
11730 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET));
11734 ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
11735 ctl_ua_type ua_type)
11737 struct ctl_lun *lun;
11740 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11741 union ctl_ha_msg msg_info;
11743 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11744 msg_info.hdr.nexus = io->io_hdr.nexus;
11745 if (ua_type==CTL_UA_TARG_RESET)
11746 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11748 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11749 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11750 msg_info.hdr.original_sc = NULL;
11751 msg_info.hdr.serializing_sc = NULL;
11752 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11753 (void *)&msg_info, sizeof(msg_info), 0)) {
11758 mtx_lock(&ctl_softc->ctl_lock);
11759 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links)
11760 retval += ctl_lun_reset(lun, io, ua_type);
11761 mtx_unlock(&ctl_softc->ctl_lock);
11767 * The LUN should always be set. The I/O is optional, and is used to
11768 * distinguish between I/Os sent by this initiator, and by other
11769 * initiators. We set unit attention for initiators other than this one.
11770 * SAM-3 is vague on this point. It does say that a unit attention should
11771 * be established for other initiators when a LUN is reset (see section
11772 * 5.7.3), but it doesn't specifically say that the unit attention should
11773 * be established for this particular initiator when a LUN is reset. Here
11774 * is the relevant text, from SAM-3 rev 8:
11776 * 5.7.2 When a SCSI initiator port aborts its own tasks
11778 * When a SCSI initiator port causes its own task(s) to be aborted, no
11779 * notification that the task(s) have been aborted shall be returned to
11780 * the SCSI initiator port other than the completion response for the
11781 * command or task management function action that caused the task(s) to
11782 * be aborted and notification(s) associated with related effects of the
11783 * action (e.g., a reset unit attention condition).
11785 * XXX KDM for now, we're setting unit attention for all initiators.
11788 ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11798 mtx_lock(&lun->lun_lock);
11800 * Run through the OOA queue and abort each I/O.
11803 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
11805 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11806 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11807 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11811 * This version sets unit attention for every
11814 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11815 ctl_est_ua_all(lun, initidx, ua_type);
11817 ctl_est_ua_all(lun, -1, ua_type);
11821 * A reset (any kind, really) clears reservations established with
11822 * RESERVE/RELEASE. It does not clear reservations established
11823 * with PERSISTENT RESERVE OUT, but we don't support that at the
11824 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
11825 * reservations made with the RESERVE/RELEASE commands, because
11826 * those commands are obsolete in SPC-3.
11828 lun->flags &= ~CTL_LUN_RESERVED;
11831 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11832 ctl_clear_mask(lun->have_ca, i);
11834 mtx_unlock(&lun->lun_lock);
11840 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11845 mtx_assert(&lun->lun_lock, MA_OWNED);
11848 * Run through the OOA queue and attempt to find the given I/O.
11849 * The target port, initiator ID, tag type and tag number have to
11850 * match the values that we got from the initiator. If we have an
11851 * untagged command to abort, simply abort the first untagged command
11852 * we come to. We only allow one untagged command at a time of course.
11854 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11855 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11857 if ((targ_port == UINT32_MAX ||
11858 targ_port == xio->io_hdr.nexus.targ_port) &&
11859 (init_id == UINT32_MAX ||
11860 init_id == xio->io_hdr.nexus.initid.id)) {
11861 if (targ_port != xio->io_hdr.nexus.targ_port ||
11862 init_id != xio->io_hdr.nexus.initid.id)
11863 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
11864 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11865 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11866 union ctl_ha_msg msg_info;
11868 msg_info.hdr.nexus = xio->io_hdr.nexus;
11869 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11870 msg_info.task.tag_num = xio->scsiio.tag_num;
11871 msg_info.task.tag_type = xio->scsiio.tag_type;
11872 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11873 msg_info.hdr.original_sc = NULL;
11874 msg_info.hdr.serializing_sc = NULL;
11875 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11876 (void *)&msg_info, sizeof(msg_info), 0);
11883 ctl_abort_task_set(union ctl_io *io)
11885 struct ctl_softc *softc = control_softc;
11886 struct ctl_lun *lun;
11892 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11893 mtx_lock(&softc->ctl_lock);
11894 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL))
11895 lun = softc->ctl_luns[targ_lun];
11897 mtx_unlock(&softc->ctl_lock);
11901 mtx_lock(&lun->lun_lock);
11902 mtx_unlock(&softc->ctl_lock);
11903 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
11904 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11905 io->io_hdr.nexus.initid.id,
11906 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11907 } else { /* CTL_TASK_CLEAR_TASK_SET */
11908 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
11909 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11911 mtx_unlock(&lun->lun_lock);
11916 ctl_i_t_nexus_reset(union ctl_io *io)
11918 struct ctl_softc *softc = control_softc;
11919 struct ctl_lun *lun;
11920 uint32_t initidx, residx;
11922 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11923 residx = ctl_get_resindex(&io->io_hdr.nexus);
11924 mtx_lock(&softc->ctl_lock);
11925 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11926 mtx_lock(&lun->lun_lock);
11927 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11928 io->io_hdr.nexus.initid.id,
11929 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11931 ctl_clear_mask(lun->have_ca, initidx);
11933 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
11934 lun->flags &= ~CTL_LUN_RESERVED;
11935 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS);
11936 mtx_unlock(&lun->lun_lock);
11938 mtx_unlock(&softc->ctl_lock);
11943 ctl_abort_task(union ctl_io *io)
11946 struct ctl_lun *lun;
11947 struct ctl_softc *ctl_softc;
11950 char printbuf[128];
11955 ctl_softc = control_softc;
11961 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11962 mtx_lock(&ctl_softc->ctl_lock);
11963 if ((targ_lun < CTL_MAX_LUNS)
11964 && (ctl_softc->ctl_luns[targ_lun] != NULL))
11965 lun = ctl_softc->ctl_luns[targ_lun];
11967 mtx_unlock(&ctl_softc->ctl_lock);
11972 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
11973 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
11976 mtx_lock(&lun->lun_lock);
11977 mtx_unlock(&ctl_softc->ctl_lock);
11979 * Run through the OOA queue and attempt to find the given I/O.
11980 * The target port, initiator ID, tag type and tag number have to
11981 * match the values that we got from the initiator. If we have an
11982 * untagged command to abort, simply abort the first untagged command
11983 * we come to. We only allow one untagged command at a time of course.
11986 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
11988 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11989 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11991 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
11993 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
11994 lun->lun, xio->scsiio.tag_num,
11995 xio->scsiio.tag_type,
11996 (xio->io_hdr.blocked_links.tqe_prev
11997 == NULL) ? "" : " BLOCKED",
11998 (xio->io_hdr.flags &
11999 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
12000 (xio->io_hdr.flags &
12001 CTL_FLAG_ABORT) ? " ABORT" : "",
12002 (xio->io_hdr.flags &
12003 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
12004 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
12006 printf("%s\n", sbuf_data(&sb));
12009 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port)
12010 && (xio->io_hdr.nexus.initid.id ==
12011 io->io_hdr.nexus.initid.id)) {
12013 * If the abort says that the task is untagged, the
12014 * task in the queue must be untagged. Otherwise,
12015 * we just check to see whether the tag numbers
12016 * match. This is because the QLogic firmware
12017 * doesn't pass back the tag type in an abort
12021 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12022 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12023 || (xio->scsiio.tag_num == io->taskio.tag_num)) {
12026 * XXX KDM we've got problems with FC, because it
12027 * doesn't send down a tag type with aborts. So we
12028 * can only really go by the tag number...
12029 * This may cause problems with parallel SCSI.
12030 * Need to figure that out!!
12032 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12033 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12035 if ((io->io_hdr.flags &
12036 CTL_FLAG_FROM_OTHER_SC) == 0 &&
12037 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12038 union ctl_ha_msg msg_info;
12040 io->io_hdr.flags |=
12041 CTL_FLAG_SENT_2OTHER_SC;
12042 msg_info.hdr.nexus = io->io_hdr.nexus;
12043 msg_info.task.task_action =
12044 CTL_TASK_ABORT_TASK;
12045 msg_info.task.tag_num =
12046 io->taskio.tag_num;
12047 msg_info.task.tag_type =
12048 io->taskio.tag_type;
12049 msg_info.hdr.msg_type =
12050 CTL_MSG_MANAGE_TASKS;
12051 msg_info.hdr.original_sc = NULL;
12052 msg_info.hdr.serializing_sc = NULL;
12054 printf("Sent Abort to other side\n");
12056 if (CTL_HA_STATUS_SUCCESS !=
12057 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12059 sizeof(msg_info), 0)) {
12063 printf("ctl_abort_task: found I/O to abort\n");
12069 mtx_unlock(&lun->lun_lock);
12073 * This isn't really an error. It's entirely possible for
12074 * the abort and command completion to cross on the wire.
12075 * This is more of an informative/diagnostic error.
12078 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
12079 "%d:%d:%d:%d tag %d type %d\n",
12080 io->io_hdr.nexus.initid.id,
12081 io->io_hdr.nexus.targ_port,
12082 io->io_hdr.nexus.targ_target.id,
12083 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
12084 io->taskio.tag_type);
12091 ctl_run_task(union ctl_io *io)
12093 struct ctl_softc *ctl_softc = control_softc;
12095 const char *task_desc;
12097 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12099 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12100 ("ctl_run_task: Unextected io_type %d\n",
12101 io->io_hdr.io_type));
12103 task_desc = ctl_scsi_task_string(&io->taskio);
12104 if (task_desc != NULL) {
12106 csevent_log(CSC_CTL | CSC_SHELF_SW |
12108 csevent_LogType_Trace,
12109 csevent_Severity_Information,
12110 csevent_AlertLevel_Green,
12111 csevent_FRU_Firmware,
12112 csevent_FRU_Unknown,
12113 "CTL: received task: %s",task_desc);
12117 csevent_log(CSC_CTL | CSC_SHELF_SW |
12119 csevent_LogType_Trace,
12120 csevent_Severity_Information,
12121 csevent_AlertLevel_Green,
12122 csevent_FRU_Firmware,
12123 csevent_FRU_Unknown,
12124 "CTL: received unknown task "
12126 io->taskio.task_action,
12127 io->taskio.task_action);
12130 switch (io->taskio.task_action) {
12131 case CTL_TASK_ABORT_TASK:
12132 retval = ctl_abort_task(io);
12134 case CTL_TASK_ABORT_TASK_SET:
12135 case CTL_TASK_CLEAR_TASK_SET:
12136 retval = ctl_abort_task_set(io);
12138 case CTL_TASK_CLEAR_ACA:
12140 case CTL_TASK_I_T_NEXUS_RESET:
12141 retval = ctl_i_t_nexus_reset(io);
12143 case CTL_TASK_LUN_RESET: {
12144 struct ctl_lun *lun;
12147 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12148 mtx_lock(&ctl_softc->ctl_lock);
12149 if ((targ_lun < CTL_MAX_LUNS)
12150 && (ctl_softc->ctl_luns[targ_lun] != NULL))
12151 lun = ctl_softc->ctl_luns[targ_lun];
12153 mtx_unlock(&ctl_softc->ctl_lock);
12158 if (!(io->io_hdr.flags &
12159 CTL_FLAG_FROM_OTHER_SC)) {
12160 union ctl_ha_msg msg_info;
12162 io->io_hdr.flags |=
12163 CTL_FLAG_SENT_2OTHER_SC;
12164 msg_info.hdr.msg_type =
12165 CTL_MSG_MANAGE_TASKS;
12166 msg_info.hdr.nexus = io->io_hdr.nexus;
12167 msg_info.task.task_action =
12168 CTL_TASK_LUN_RESET;
12169 msg_info.hdr.original_sc = NULL;
12170 msg_info.hdr.serializing_sc = NULL;
12171 if (CTL_HA_STATUS_SUCCESS !=
12172 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12174 sizeof(msg_info), 0)) {
12178 retval = ctl_lun_reset(lun, io,
12180 mtx_unlock(&ctl_softc->ctl_lock);
12183 case CTL_TASK_TARGET_RESET:
12184 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET);
12186 case CTL_TASK_BUS_RESET:
12187 retval = ctl_bus_reset(ctl_softc, io);
12189 case CTL_TASK_PORT_LOGIN:
12191 case CTL_TASK_PORT_LOGOUT:
12194 printf("ctl_run_task: got unknown task management event %d\n",
12195 io->taskio.task_action);
12199 io->io_hdr.status = CTL_SUCCESS;
12201 io->io_hdr.status = CTL_ERROR;
12206 * For HA operation. Handle commands that come in from the other
12210 ctl_handle_isc(union ctl_io *io)
12213 struct ctl_lun *lun;
12214 struct ctl_softc *ctl_softc;
12217 ctl_softc = control_softc;
12219 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12220 lun = ctl_softc->ctl_luns[targ_lun];
12222 switch (io->io_hdr.msg_type) {
12223 case CTL_MSG_SERIALIZE:
12224 free_io = ctl_serialize_other_sc_cmd(&io->scsiio);
12226 case CTL_MSG_R2R: {
12227 const struct ctl_cmd_entry *entry;
12230 * This is only used in SER_ONLY mode.
12233 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12234 mtx_lock(&lun->lun_lock);
12235 if (ctl_scsiio_lun_check(ctl_softc, lun,
12236 entry, (struct ctl_scsiio *)io) != 0) {
12237 mtx_unlock(&lun->lun_lock);
12241 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12242 mtx_unlock(&lun->lun_lock);
12243 ctl_enqueue_rtr(io);
12246 case CTL_MSG_FINISH_IO:
12247 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
12252 mtx_lock(&lun->lun_lock);
12253 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
12255 ctl_check_blocked(lun);
12256 mtx_unlock(&lun->lun_lock);
12259 case CTL_MSG_PERS_ACTION:
12260 ctl_hndl_per_res_out_on_other_sc(
12261 (union ctl_ha_msg *)&io->presio.pr_msg);
12264 case CTL_MSG_BAD_JUJU:
12268 case CTL_MSG_DATAMOVE:
12269 /* Only used in XFER mode */
12271 ctl_datamove_remote(io);
12273 case CTL_MSG_DATAMOVE_DONE:
12274 /* Only used in XFER mode */
12276 io->scsiio.be_move_done(io);
12280 printf("%s: Invalid message type %d\n",
12281 __func__, io->io_hdr.msg_type);
12291 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12292 * there is no match.
12294 static ctl_lun_error_pattern
12295 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12297 const struct ctl_cmd_entry *entry;
12298 ctl_lun_error_pattern filtered_pattern, pattern;
12300 pattern = desc->error_pattern;
12303 * XXX KDM we need more data passed into this function to match a
12304 * custom pattern, and we actually need to implement custom pattern
12307 if (pattern & CTL_LUN_PAT_CMD)
12308 return (CTL_LUN_PAT_CMD);
12310 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12311 return (CTL_LUN_PAT_ANY);
12313 entry = ctl_get_cmd_entry(ctsio, NULL);
12315 filtered_pattern = entry->pattern & pattern;
12318 * If the user requested specific flags in the pattern (e.g.
12319 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12322 * If the user did not specify any flags, it doesn't matter whether
12323 * or not the command supports the flags.
12325 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12326 (pattern & ~CTL_LUN_PAT_MASK))
12327 return (CTL_LUN_PAT_NONE);
12330 * If the user asked for a range check, see if the requested LBA
12331 * range overlaps with this command's LBA range.
12333 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12339 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12341 return (CTL_LUN_PAT_NONE);
12343 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12344 desc->lba_range.len);
12346 * A "pass" means that the LBA ranges don't overlap, so
12347 * this doesn't match the user's range criteria.
12349 if (action == CTL_ACTION_PASS)
12350 return (CTL_LUN_PAT_NONE);
12353 return (filtered_pattern);
12357 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12359 struct ctl_error_desc *desc, *desc2;
12361 mtx_assert(&lun->lun_lock, MA_OWNED);
12363 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12364 ctl_lun_error_pattern pattern;
12366 * Check to see whether this particular command matches
12367 * the pattern in the descriptor.
12369 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12370 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12373 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12374 case CTL_LUN_INJ_ABORTED:
12375 ctl_set_aborted(&io->scsiio);
12377 case CTL_LUN_INJ_MEDIUM_ERR:
12378 ctl_set_medium_error(&io->scsiio);
12380 case CTL_LUN_INJ_UA:
12381 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12383 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12385 case CTL_LUN_INJ_CUSTOM:
12387 * We're assuming the user knows what he is doing.
12388 * Just copy the sense information without doing
12391 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12392 ctl_min(sizeof(desc->custom_sense),
12393 sizeof(io->scsiio.sense_data)));
12394 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12395 io->scsiio.sense_len = SSD_FULL_SIZE;
12396 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12398 case CTL_LUN_INJ_NONE:
12401 * If this is an error injection type we don't know
12402 * about, clear the continuous flag (if it is set)
12403 * so it will get deleted below.
12405 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12409 * By default, each error injection action is a one-shot
12411 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12414 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12420 #ifdef CTL_IO_DELAY
12422 ctl_datamove_timer_wakeup(void *arg)
12426 io = (union ctl_io *)arg;
12430 #endif /* CTL_IO_DELAY */
12433 ctl_datamove(union ctl_io *io)
12435 void (*fe_datamove)(union ctl_io *io);
12437 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12439 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12442 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12447 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12448 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12450 sbuf_cat(&sb, path_str);
12451 switch (io->io_hdr.io_type) {
12453 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12454 sbuf_printf(&sb, "\n");
12455 sbuf_cat(&sb, path_str);
12456 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12457 io->scsiio.tag_num, io->scsiio.tag_type);
12460 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12461 "Tag Type: %d\n", io->taskio.task_action,
12462 io->taskio.tag_num, io->taskio.tag_type);
12465 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12466 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12469 sbuf_cat(&sb, path_str);
12470 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12471 (intmax_t)time_uptime - io->io_hdr.start_time);
12473 printf("%s", sbuf_data(&sb));
12475 #endif /* CTL_TIME_IO */
12477 #ifdef CTL_IO_DELAY
12478 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12479 struct ctl_lun *lun;
12481 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12483 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12485 struct ctl_lun *lun;
12487 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12489 && (lun->delay_info.datamove_delay > 0)) {
12490 struct callout *callout;
12492 callout = (struct callout *)&io->io_hdr.timer_bytes;
12493 callout_init(callout, /*mpsafe*/ 1);
12494 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12495 callout_reset(callout,
12496 lun->delay_info.datamove_delay * hz,
12497 ctl_datamove_timer_wakeup, io);
12498 if (lun->delay_info.datamove_type ==
12499 CTL_DELAY_TYPE_ONESHOT)
12500 lun->delay_info.datamove_delay = 0;
12507 * This command has been aborted. Set the port status, so we fail
12510 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12511 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n",
12512 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id,
12513 io->io_hdr.nexus.targ_port,
12514 (uintmax_t)io->io_hdr.nexus.targ_target.id,
12515 io->io_hdr.nexus.targ_lun);
12516 io->io_hdr.port_status = 31337;
12518 * Note that the backend, in this case, will get the
12519 * callback in its context. In other cases it may get
12520 * called in the frontend's interrupt thread context.
12522 io->scsiio.be_move_done(io);
12526 /* Don't confuse frontend with zero length data move. */
12527 if (io->scsiio.kern_data_len == 0) {
12528 io->scsiio.be_move_done(io);
12533 * If we're in XFER mode and this I/O is from the other shelf
12534 * controller, we need to send the DMA to the other side to
12535 * actually transfer the data to/from the host. In serialize only
12536 * mode the transfer happens below CTL and ctl_datamove() is only
12537 * called on the machine that originally received the I/O.
12539 if ((control_softc->ha_mode == CTL_HA_MODE_XFER)
12540 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12541 union ctl_ha_msg msg;
12542 uint32_t sg_entries_sent;
12546 memset(&msg, 0, sizeof(msg));
12547 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
12548 msg.hdr.original_sc = io->io_hdr.original_sc;
12549 msg.hdr.serializing_sc = io;
12550 msg.hdr.nexus = io->io_hdr.nexus;
12551 msg.dt.flags = io->io_hdr.flags;
12553 * We convert everything into a S/G list here. We can't
12554 * pass by reference, only by value between controllers.
12555 * So we can't pass a pointer to the S/G list, only as many
12556 * S/G entries as we can fit in here. If it's possible for
12557 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
12558 * then we need to break this up into multiple transfers.
12560 if (io->scsiio.kern_sg_entries == 0) {
12561 msg.dt.kern_sg_entries = 1;
12563 * If this is in cached memory, flush the cache
12564 * before we send the DMA request to the other
12565 * controller. We want to do this in either the
12566 * read or the write case. The read case is
12567 * straightforward. In the write case, we want to
12568 * make sure nothing is in the local cache that
12569 * could overwrite the DMAed data.
12571 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12573 * XXX KDM use bus_dmamap_sync() here.
12578 * Convert to a physical address if this is a
12581 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
12582 msg.dt.sg_list[0].addr =
12583 io->scsiio.kern_data_ptr;
12586 * XXX KDM use busdma here!
12589 msg.dt.sg_list[0].addr = (void *)
12590 vtophys(io->scsiio.kern_data_ptr);
12594 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
12597 struct ctl_sg_entry *sgl;
12600 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
12601 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
12602 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12604 * XXX KDM use bus_dmamap_sync() here.
12609 msg.dt.kern_data_len = io->scsiio.kern_data_len;
12610 msg.dt.kern_total_len = io->scsiio.kern_total_len;
12611 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
12612 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
12613 msg.dt.sg_sequence = 0;
12616 * Loop until we've sent all of the S/G entries. On the
12617 * other end, we'll recompose these S/G entries into one
12618 * contiguous list before passing it to the
12620 for (sg_entries_sent = 0; sg_entries_sent <
12621 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) {
12622 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/
12623 sizeof(msg.dt.sg_list[0])),
12624 msg.dt.kern_sg_entries - sg_entries_sent);
12626 if (do_sg_copy != 0) {
12627 struct ctl_sg_entry *sgl;
12630 sgl = (struct ctl_sg_entry *)
12631 io->scsiio.kern_data_ptr;
12633 * If this is in cached memory, flush the cache
12634 * before we send the DMA request to the other
12635 * controller. We want to do this in either
12636 * the * read or the write case. The read
12637 * case is straightforward. In the write
12638 * case, we want to make sure nothing is
12639 * in the local cache that could overwrite
12643 for (i = sg_entries_sent, j = 0;
12644 i < msg.dt.cur_sg_entries; i++, j++) {
12645 if ((io->io_hdr.flags &
12646 CTL_FLAG_NO_DATASYNC) == 0) {
12648 * XXX KDM use bus_dmamap_sync()
12651 if ((io->io_hdr.flags &
12652 CTL_FLAG_BUS_ADDR) == 0) {
12654 * XXX KDM use busdma.
12657 msg.dt.sg_list[j].addr =(void *)
12658 vtophys(sgl[i].addr);
12661 msg.dt.sg_list[j].addr =
12664 msg.dt.sg_list[j].len = sgl[i].len;
12668 sg_entries_sent += msg.dt.cur_sg_entries;
12669 if (sg_entries_sent >= msg.dt.kern_sg_entries)
12670 msg.dt.sg_last = 1;
12672 msg.dt.sg_last = 0;
12675 * XXX KDM drop and reacquire the lock here?
12677 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12678 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
12680 * XXX do something here.
12684 msg.dt.sent_sg_entries = sg_entries_sent;
12686 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12687 if (io->io_hdr.flags & CTL_FLAG_FAILOVER)
12688 ctl_failover_io(io, /*have_lock*/ 0);
12693 * Lookup the fe_datamove() function for this particular
12697 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12704 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12706 union ctl_ha_msg msg;
12709 memset(&msg, 0, sizeof(msg));
12711 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12712 msg.hdr.original_sc = io;
12713 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12714 msg.hdr.nexus = io->io_hdr.nexus;
12715 msg.hdr.status = io->io_hdr.status;
12716 msg.scsi.tag_num = io->scsiio.tag_num;
12717 msg.scsi.tag_type = io->scsiio.tag_type;
12718 msg.scsi.scsi_status = io->scsiio.scsi_status;
12719 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12720 sizeof(io->scsiio.sense_data));
12721 msg.scsi.sense_len = io->scsiio.sense_len;
12722 msg.scsi.sense_residual = io->scsiio.sense_residual;
12723 msg.scsi.fetd_status = io->io_hdr.port_status;
12724 msg.scsi.residual = io->scsiio.residual;
12725 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12727 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12728 ctl_failover_io(io, /*have_lock*/ have_lock);
12732 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0);
12733 if (isc_status > CTL_HA_STATUS_SUCCESS) {
12734 /* XXX do something if this fails */
12740 * The DMA to the remote side is done, now we need to tell the other side
12741 * we're done so it can continue with its data movement.
12744 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12750 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12751 printf("%s: ISC DMA write failed with error %d", __func__,
12753 ctl_set_internal_failure(&io->scsiio,
12755 /*retry_count*/ rq->ret);
12758 ctl_dt_req_free(rq);
12761 * In this case, we had to malloc the memory locally. Free it.
12763 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12765 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12766 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12769 * The data is in local and remote memory, so now we need to send
12770 * status (good or back) back to the other side.
12772 ctl_send_datamove_done(io, /*have_lock*/ 0);
12776 * We've moved the data from the host/controller into local memory. Now we
12777 * need to push it over to the remote controller's memory.
12780 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12786 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12787 ctl_datamove_remote_write_cb);
12793 ctl_datamove_remote_write(union ctl_io *io)
12796 void (*fe_datamove)(union ctl_io *io);
12799 * - Get the data from the host/HBA into local memory.
12800 * - DMA memory from the local controller to the remote controller.
12801 * - Send status back to the remote controller.
12804 retval = ctl_datamove_remote_sgl_setup(io);
12808 /* Switch the pointer over so the FETD knows what to do */
12809 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12812 * Use a custom move done callback, since we need to send completion
12813 * back to the other controller, not to the backend on this side.
12815 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12817 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12826 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12835 * In this case, we had to malloc the memory locally. Free it.
12837 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12839 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12840 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12844 scsi_path_string(io, path_str, sizeof(path_str));
12845 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12846 sbuf_cat(&sb, path_str);
12847 scsi_command_string(&io->scsiio, NULL, &sb);
12848 sbuf_printf(&sb, "\n");
12849 sbuf_cat(&sb, path_str);
12850 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12851 io->scsiio.tag_num, io->scsiio.tag_type);
12852 sbuf_cat(&sb, path_str);
12853 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12854 io->io_hdr.flags, io->io_hdr.status);
12856 printk("%s", sbuf_data(&sb));
12861 * The read is done, now we need to send status (good or bad) back
12862 * to the other side.
12864 ctl_send_datamove_done(io, /*have_lock*/ 0);
12870 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12873 void (*fe_datamove)(union ctl_io *io);
12877 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12878 printf("%s: ISC DMA read failed with error %d", __func__,
12880 ctl_set_internal_failure(&io->scsiio,
12882 /*retry_count*/ rq->ret);
12885 ctl_dt_req_free(rq);
12887 /* Switch the pointer over so the FETD knows what to do */
12888 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12891 * Use a custom move done callback, since we need to send completion
12892 * back to the other controller, not to the backend on this side.
12894 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12896 /* XXX KDM add checks like the ones in ctl_datamove? */
12898 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12904 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12906 struct ctl_sg_entry *local_sglist, *remote_sglist;
12907 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist;
12908 struct ctl_softc *softc;
12913 softc = control_softc;
12915 local_sglist = io->io_hdr.local_sglist;
12916 local_dma_sglist = io->io_hdr.local_dma_sglist;
12917 remote_sglist = io->io_hdr.remote_sglist;
12918 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
12920 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) {
12921 for (i = 0; i < io->scsiio.kern_sg_entries; i++) {
12922 local_sglist[i].len = remote_sglist[i].len;
12925 * XXX Detect the situation where the RS-level I/O
12926 * redirector on the other side has already read the
12927 * data off of the AOR RS on this side, and
12928 * transferred it to remote (mirror) memory on the
12929 * other side. Since we already have the data in
12930 * memory here, we just need to use it.
12932 * XXX KDM this can probably be removed once we
12933 * get the cache device code in and take the
12934 * current AOR implementation out.
12937 if ((remote_sglist[i].addr >=
12938 (void *)vtophys(softc->mirr->addr))
12939 && (remote_sglist[i].addr <
12940 ((void *)vtophys(softc->mirr->addr) +
12941 CacheMirrorOffset))) {
12942 local_sglist[i].addr = remote_sglist[i].addr -
12944 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12946 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE;
12948 local_sglist[i].addr = remote_sglist[i].addr +
12953 printf("%s: local %p, remote %p, len %d\n",
12954 __func__, local_sglist[i].addr,
12955 remote_sglist[i].addr, local_sglist[i].len);
12959 uint32_t len_to_go;
12962 * In this case, we don't have automatically allocated
12963 * memory for this I/O on this controller. This typically
12964 * happens with internal CTL I/O -- e.g. inquiry, mode
12965 * sense, etc. Anything coming from RAIDCore will have
12966 * a mirror area available.
12968 len_to_go = io->scsiio.kern_data_len;
12971 * Clear the no datasync flag, we have to use malloced
12974 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC;
12977 * The difficult thing here is that the size of the various
12978 * S/G segments may be different than the size from the
12979 * remote controller. That'll make it harder when DMAing
12980 * the data back to the other side.
12982 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) /
12983 sizeof(io->io_hdr.remote_sglist[0])) &&
12984 (len_to_go > 0); i++) {
12985 local_sglist[i].len = ctl_min(len_to_go, 131072);
12986 CTL_SIZE_8B(local_dma_sglist[i].len,
12987 local_sglist[i].len);
12988 local_sglist[i].addr =
12989 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK);
12991 local_dma_sglist[i].addr = local_sglist[i].addr;
12993 if (local_sglist[i].addr == NULL) {
12996 printf("malloc failed for %zd bytes!",
12997 local_dma_sglist[i].len);
12998 for (j = 0; j < i; j++) {
12999 free(local_sglist[j].addr, M_CTL);
13001 ctl_set_internal_failure(&io->scsiio,
13003 /*retry_count*/ 4857);
13005 goto bailout_error;
13008 /* XXX KDM do we need a sync here? */
13010 len_to_go -= local_sglist[i].len;
13013 * Reset the number of S/G entries accordingly. The
13014 * original number of S/G entries is available in
13017 io->scsiio.kern_sg_entries = i;
13020 printf("%s: kern_sg_entries = %d\n", __func__,
13021 io->scsiio.kern_sg_entries);
13022 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13023 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i,
13024 local_sglist[i].addr, local_sglist[i].len,
13025 local_dma_sglist[i].len);
13034 ctl_send_datamove_done(io, /*have_lock*/ 0);
13040 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
13041 ctl_ha_dt_cb callback)
13043 struct ctl_ha_dt_req *rq;
13044 struct ctl_sg_entry *remote_sglist, *local_sglist;
13045 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist;
13046 uint32_t local_used, remote_used, total_used;
13052 rq = ctl_dt_req_alloc();
13055 * If we failed to allocate the request, and if the DMA didn't fail
13056 * anyway, set busy status. This is just a resource allocation
13060 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE))
13061 ctl_set_busy(&io->scsiio);
13063 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
13066 ctl_dt_req_free(rq);
13069 * The data move failed. We need to return status back
13070 * to the other controller. No point in trying to DMA
13071 * data to the remote controller.
13074 ctl_send_datamove_done(io, /*have_lock*/ 0);
13081 local_sglist = io->io_hdr.local_sglist;
13082 local_dma_sglist = io->io_hdr.local_dma_sglist;
13083 remote_sglist = io->io_hdr.remote_sglist;
13084 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
13089 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) {
13090 rq->ret = CTL_HA_STATUS_SUCCESS;
13097 * Pull/push the data over the wire from/to the other controller.
13098 * This takes into account the possibility that the local and
13099 * remote sglists may not be identical in terms of the size of
13100 * the elements and the number of elements.
13102 * One fundamental assumption here is that the length allocated for
13103 * both the local and remote sglists is identical. Otherwise, we've
13104 * essentially got a coding error of some sort.
13106 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
13108 uint32_t cur_len, dma_length;
13111 rq->id = CTL_HA_DATA_CTL;
13112 rq->command = command;
13116 * Both pointers should be aligned. But it is possible
13117 * that the allocation length is not. They should both
13118 * also have enough slack left over at the end, though,
13119 * to round up to the next 8 byte boundary.
13121 cur_len = ctl_min(local_sglist[i].len - local_used,
13122 remote_sglist[j].len - remote_used);
13125 * In this case, we have a size issue and need to decrease
13126 * the size, except in the case where we actually have less
13127 * than 8 bytes left. In that case, we need to increase
13128 * the DMA length to get the last bit.
13130 if ((cur_len & 0x7) != 0) {
13131 if (cur_len > 0x7) {
13132 cur_len = cur_len - (cur_len & 0x7);
13133 dma_length = cur_len;
13135 CTL_SIZE_8B(dma_length, cur_len);
13139 dma_length = cur_len;
13142 * If we had to allocate memory for this I/O, instead of using
13143 * the non-cached mirror memory, we'll need to flush the cache
13144 * before trying to DMA to the other controller.
13146 * We could end up doing this multiple times for the same
13147 * segment if we have a larger local segment than remote
13148 * segment. That shouldn't be an issue.
13150 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
13152 * XXX KDM use bus_dmamap_sync() here.
13156 rq->size = dma_length;
13158 tmp_ptr = (uint8_t *)local_sglist[i].addr;
13159 tmp_ptr += local_used;
13161 /* Use physical addresses when talking to ISC hardware */
13162 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
13163 /* XXX KDM use busdma */
13165 rq->local = vtophys(tmp_ptr);
13168 rq->local = tmp_ptr;
13170 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
13171 tmp_ptr += remote_used;
13172 rq->remote = tmp_ptr;
13174 rq->callback = NULL;
13176 local_used += cur_len;
13177 if (local_used >= local_sglist[i].len) {
13182 remote_used += cur_len;
13183 if (remote_used >= remote_sglist[j].len) {
13187 total_used += cur_len;
13189 if (total_used >= io->scsiio.kern_data_len)
13190 rq->callback = callback;
13192 if ((rq->size & 0x7) != 0) {
13193 printf("%s: warning: size %d is not on 8b boundary\n",
13194 __func__, rq->size);
13196 if (((uintptr_t)rq->local & 0x7) != 0) {
13197 printf("%s: warning: local %p not on 8b boundary\n",
13198 __func__, rq->local);
13200 if (((uintptr_t)rq->remote & 0x7) != 0) {
13201 printf("%s: warning: remote %p not on 8b boundary\n",
13202 __func__, rq->local);
13205 printf("%s: %s: local %#x remote %#x size %d\n", __func__,
13206 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
13207 rq->local, rq->remote, rq->size);
13210 isc_ret = ctl_dt_single(rq);
13211 if (isc_ret == CTL_HA_STATUS_WAIT)
13214 if (isc_ret == CTL_HA_STATUS_DISCONNECT) {
13215 rq->ret = CTL_HA_STATUS_SUCCESS;
13229 ctl_datamove_remote_read(union ctl_io *io)
13235 * This will send an error to the other controller in the case of a
13238 retval = ctl_datamove_remote_sgl_setup(io);
13242 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
13243 ctl_datamove_remote_read_cb);
13245 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) {
13247 * Make sure we free memory if there was an error.. The
13248 * ctl_datamove_remote_xfer() function will send the
13249 * datamove done message, or call the callback with an
13250 * error if there is a problem.
13252 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13253 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13260 * Process a datamove request from the other controller. This is used for
13261 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
13262 * first. Once that is complete, the data gets DMAed into the remote
13263 * controller's memory. For reads, we DMA from the remote controller's
13264 * memory into our memory first, and then move it out to the FETD.
13267 ctl_datamove_remote(union ctl_io *io)
13269 struct ctl_softc *softc;
13271 softc = control_softc;
13273 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
13276 * Note that we look for an aborted I/O here, but don't do some of
13277 * the other checks that ctl_datamove() normally does.
13278 * We don't need to run the datamove delay code, since that should
13279 * have been done if need be on the other controller.
13281 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
13282 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__,
13283 io->scsiio.tag_num, io->io_hdr.nexus.initid.id,
13284 io->io_hdr.nexus.targ_port,
13285 io->io_hdr.nexus.targ_target.id,
13286 io->io_hdr.nexus.targ_lun);
13287 io->io_hdr.port_status = 31338;
13288 ctl_send_datamove_done(io, /*have_lock*/ 0);
13292 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) {
13293 ctl_datamove_remote_write(io);
13294 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){
13295 ctl_datamove_remote_read(io);
13297 union ctl_ha_msg msg;
13298 struct scsi_sense_data *sense;
13302 memset(&msg, 0, sizeof(msg));
13304 msg.hdr.msg_type = CTL_MSG_BAD_JUJU;
13305 msg.hdr.status = CTL_SCSI_ERROR;
13306 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
13308 retry_count = 4243;
13310 sense = &msg.scsi.sense_data;
13311 sks[0] = SSD_SCS_VALID;
13312 sks[1] = (retry_count >> 8) & 0xff;
13313 sks[2] = retry_count & 0xff;
13315 /* "Internal target failure" */
13316 scsi_set_sense_data(sense,
13317 /*sense_format*/ SSD_TYPE_NONE,
13318 /*current_error*/ 1,
13319 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
13322 /*type*/ SSD_ELEM_SKS,
13323 /*size*/ sizeof(sks),
13327 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
13328 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
13329 ctl_failover_io(io, /*have_lock*/ 1);
13333 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) >
13334 CTL_HA_STATUS_SUCCESS) {
13335 /* XXX KDM what to do if this fails? */
13343 ctl_process_done(union ctl_io *io)
13345 struct ctl_lun *lun;
13346 struct ctl_softc *ctl_softc = control_softc;
13347 void (*fe_done)(union ctl_io *io);
13348 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port);
13350 CTL_DEBUG_PRINT(("ctl_process_done\n"));
13353 control_softc->ctl_ports[targ_port]->fe_done;
13356 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
13361 ctl_scsi_path_string(io, path_str, sizeof(path_str));
13362 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13364 sbuf_cat(&sb, path_str);
13365 switch (io->io_hdr.io_type) {
13367 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
13368 sbuf_printf(&sb, "\n");
13369 sbuf_cat(&sb, path_str);
13370 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13371 io->scsiio.tag_num, io->scsiio.tag_type);
13374 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
13375 "Tag Type: %d\n", io->taskio.task_action,
13376 io->taskio.tag_num, io->taskio.tag_type);
13379 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13380 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13383 sbuf_cat(&sb, path_str);
13384 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
13385 (intmax_t)time_uptime - io->io_hdr.start_time);
13387 printf("%s", sbuf_data(&sb));
13389 #endif /* CTL_TIME_IO */
13391 switch (io->io_hdr.io_type) {
13395 if (bootverbose || (ctl_debug & CTL_DEBUG_INFO))
13396 ctl_io_error_print(io, NULL);
13397 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
13401 return (CTL_RETVAL_COMPLETE);
13403 panic("ctl_process_done: invalid io type %d\n",
13404 io->io_hdr.io_type);
13405 break; /* NOTREACHED */
13408 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13410 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13411 io->io_hdr.nexus.targ_mapped_lun));
13415 mtx_lock(&lun->lun_lock);
13418 * Check to see if we have any errors to inject here. We only
13419 * inject errors for commands that don't already have errors set.
13421 if ((STAILQ_FIRST(&lun->error_list) != NULL) &&
13422 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
13423 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
13424 ctl_inject_error(lun, io);
13427 * XXX KDM how do we treat commands that aren't completed
13430 * XXX KDM should we also track I/O latency?
13432 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13433 io->io_hdr.io_type == CTL_IO_SCSI) {
13435 struct bintime cur_bt;
13439 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13441 type = CTL_STATS_READ;
13442 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13444 type = CTL_STATS_WRITE;
13446 type = CTL_STATS_NO_IO;
13448 lun->stats.ports[targ_port].bytes[type] +=
13449 io->scsiio.kern_total_len;
13450 lun->stats.ports[targ_port].operations[type]++;
13452 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
13453 &io->io_hdr.dma_bt);
13454 lun->stats.ports[targ_port].num_dmas[type] +=
13455 io->io_hdr.num_dmas;
13456 getbintime(&cur_bt);
13457 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
13458 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
13463 * Remove this from the OOA queue.
13465 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13468 * Run through the blocked queue on this LUN and see if anything
13469 * has become unblocked, now that this transaction is done.
13471 ctl_check_blocked(lun);
13474 * If the LUN has been invalidated, free it if there is nothing
13475 * left on its OOA queue.
13477 if ((lun->flags & CTL_LUN_INVALID)
13478 && TAILQ_EMPTY(&lun->ooa_queue)) {
13479 mtx_unlock(&lun->lun_lock);
13480 mtx_lock(&ctl_softc->ctl_lock);
13482 mtx_unlock(&ctl_softc->ctl_lock);
13484 mtx_unlock(&lun->lun_lock);
13489 * If this command has been aborted, make sure we set the status
13490 * properly. The FETD is responsible for freeing the I/O and doing
13491 * whatever it needs to do to clean up its state.
13493 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13494 ctl_set_task_aborted(&io->scsiio);
13497 * If enabled, print command error status.
13498 * We don't print UAs unless debugging was enabled explicitly.
13501 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
13503 if (!bootverbose && (ctl_debug & CTL_DEBUG_INFO) == 0)
13505 if ((ctl_debug & CTL_DEBUG_INFO) == 0 &&
13506 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) &&
13507 (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) {
13508 int error_code, sense_key, asc, ascq;
13510 scsi_extract_sense_len(&io->scsiio.sense_data,
13511 io->scsiio.sense_len, &error_code, &sense_key,
13512 &asc, &ascq, /*show_errors*/ 0);
13513 if (sense_key == SSD_KEY_UNIT_ATTENTION)
13517 ctl_io_error_print(io, NULL);
13521 * Tell the FETD or the other shelf controller we're done with this
13522 * command. Note that only SCSI commands get to this point. Task
13523 * management commands are completed above.
13525 * We only send status to the other controller if we're in XFER
13526 * mode. In SER_ONLY mode, the I/O is done on the controller that
13527 * received the I/O (from CTL's perspective), and so the status is
13530 * XXX KDM if we hold the lock here, we could cause a deadlock
13531 * if the frontend comes back in in this context to queue
13534 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER)
13535 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
13536 union ctl_ha_msg msg;
13538 memset(&msg, 0, sizeof(msg));
13539 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13540 msg.hdr.original_sc = io->io_hdr.original_sc;
13541 msg.hdr.nexus = io->io_hdr.nexus;
13542 msg.hdr.status = io->io_hdr.status;
13543 msg.scsi.scsi_status = io->scsiio.scsi_status;
13544 msg.scsi.tag_num = io->scsiio.tag_num;
13545 msg.scsi.tag_type = io->scsiio.tag_type;
13546 msg.scsi.sense_len = io->scsiio.sense_len;
13547 msg.scsi.sense_residual = io->scsiio.sense_residual;
13548 msg.scsi.residual = io->scsiio.residual;
13549 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
13550 sizeof(io->scsiio.sense_data));
13552 * We copy this whether or not this is an I/O-related
13553 * command. Otherwise, we'd have to go and check to see
13554 * whether it's a read/write command, and it really isn't
13557 memcpy(&msg.scsi.lbalen,
13558 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
13559 sizeof(msg.scsi.lbalen));
13561 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13562 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
13563 /* XXX do something here */
13570 return (CTL_RETVAL_COMPLETE);
13575 * Front end should call this if it doesn't do autosense. When the request
13576 * sense comes back in from the initiator, we'll dequeue this and send it.
13579 ctl_queue_sense(union ctl_io *io)
13581 struct ctl_lun *lun;
13582 struct ctl_softc *ctl_softc;
13583 uint32_t initidx, targ_lun;
13585 ctl_softc = control_softc;
13587 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13590 * LUN lookup will likely move to the ctl_work_thread() once we
13591 * have our new queueing infrastructure (that doesn't put things on
13592 * a per-LUN queue initially). That is so that we can handle
13593 * things like an INQUIRY to a LUN that we don't have enabled. We
13594 * can't deal with that right now.
13596 mtx_lock(&ctl_softc->ctl_lock);
13599 * If we don't have a LUN for this, just toss the sense
13602 targ_lun = io->io_hdr.nexus.targ_lun;
13603 targ_lun = ctl_map_lun(io->io_hdr.nexus.targ_port, targ_lun);
13604 if ((targ_lun < CTL_MAX_LUNS)
13605 && (ctl_softc->ctl_luns[targ_lun] != NULL))
13606 lun = ctl_softc->ctl_luns[targ_lun];
13610 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13612 mtx_lock(&lun->lun_lock);
13614 * Already have CA set for this LUN...toss the sense information.
13616 if (ctl_is_set(lun->have_ca, initidx)) {
13617 mtx_unlock(&lun->lun_lock);
13621 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data,
13622 ctl_min(sizeof(lun->pending_sense[initidx]),
13623 sizeof(io->scsiio.sense_data)));
13624 ctl_set_mask(lun->have_ca, initidx);
13625 mtx_unlock(&lun->lun_lock);
13628 mtx_unlock(&ctl_softc->ctl_lock);
13632 return (CTL_RETVAL_COMPLETE);
13637 * Primary command inlet from frontend ports. All SCSI and task I/O
13638 * requests must go through this function.
13641 ctl_queue(union ctl_io *io)
13643 struct ctl_softc *ctl_softc;
13645 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13647 ctl_softc = control_softc;
13650 io->io_hdr.start_time = time_uptime;
13651 getbintime(&io->io_hdr.start_bt);
13652 #endif /* CTL_TIME_IO */
13654 /* Map FE-specific LUN ID into global one. */
13655 io->io_hdr.nexus.targ_mapped_lun =
13656 ctl_map_lun(io->io_hdr.nexus.targ_port, io->io_hdr.nexus.targ_lun);
13658 switch (io->io_hdr.io_type) {
13661 if (ctl_debug & CTL_DEBUG_CDB)
13663 ctl_enqueue_incoming(io);
13666 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13670 return (CTL_RETVAL_COMPLETE);
13673 #ifdef CTL_IO_DELAY
13675 ctl_done_timer_wakeup(void *arg)
13679 io = (union ctl_io *)arg;
13682 #endif /* CTL_IO_DELAY */
13685 ctl_done(union ctl_io *io)
13687 struct ctl_softc *ctl_softc;
13689 ctl_softc = control_softc;
13692 * Enable this to catch duplicate completion issues.
13695 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13696 printf("%s: type %d msg %d cdb %x iptl: "
13697 "%d:%d:%d:%d tag 0x%04x "
13698 "flag %#x status %x\n",
13700 io->io_hdr.io_type,
13701 io->io_hdr.msg_type,
13703 io->io_hdr.nexus.initid.id,
13704 io->io_hdr.nexus.targ_port,
13705 io->io_hdr.nexus.targ_target.id,
13706 io->io_hdr.nexus.targ_lun,
13707 (io->io_hdr.io_type ==
13709 io->taskio.tag_num :
13710 io->scsiio.tag_num,
13712 io->io_hdr.status);
13714 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13718 * This is an internal copy of an I/O, and should not go through
13719 * the normal done processing logic.
13721 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13725 * We need to send a msg to the serializing shelf to finish the IO
13726 * as well. We don't send a finish message to the other shelf if
13727 * this is a task management command. Task management commands
13728 * aren't serialized in the OOA queue, but rather just executed on
13729 * both shelf controllers for commands that originated on that
13732 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)
13733 && (io->io_hdr.io_type != CTL_IO_TASK)) {
13734 union ctl_ha_msg msg_io;
13736 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO;
13737 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc;
13738 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io,
13739 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) {
13741 /* continue on to finish IO */
13743 #ifdef CTL_IO_DELAY
13744 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13745 struct ctl_lun *lun;
13747 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13749 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13751 struct ctl_lun *lun;
13753 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13756 && (lun->delay_info.done_delay > 0)) {
13757 struct callout *callout;
13759 callout = (struct callout *)&io->io_hdr.timer_bytes;
13760 callout_init(callout, /*mpsafe*/ 1);
13761 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13762 callout_reset(callout,
13763 lun->delay_info.done_delay * hz,
13764 ctl_done_timer_wakeup, io);
13765 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13766 lun->delay_info.done_delay = 0;
13770 #endif /* CTL_IO_DELAY */
13772 ctl_enqueue_done(io);
13776 ctl_isc(struct ctl_scsiio *ctsio)
13778 struct ctl_lun *lun;
13781 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13783 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0]));
13785 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n"));
13787 retval = lun->backend->data_submit((union ctl_io *)ctsio);
13794 ctl_work_thread(void *arg)
13796 struct ctl_thread *thr = (struct ctl_thread *)arg;
13797 struct ctl_softc *softc = thr->ctl_softc;
13801 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13807 * We handle the queues in this order:
13809 * - done queue (to free up resources, unblock other commands)
13813 * If those queues are empty, we break out of the loop and
13816 mtx_lock(&thr->queue_lock);
13817 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13819 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13820 mtx_unlock(&thr->queue_lock);
13821 ctl_handle_isc(io);
13824 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13826 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13827 /* clear any blocked commands, call fe_done */
13828 mtx_unlock(&thr->queue_lock);
13829 retval = ctl_process_done(io);
13832 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13834 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13835 mtx_unlock(&thr->queue_lock);
13836 if (io->io_hdr.io_type == CTL_IO_TASK)
13839 ctl_scsiio_precheck(softc, &io->scsiio);
13842 if (!ctl_pause_rtr) {
13843 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13845 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13846 mtx_unlock(&thr->queue_lock);
13847 retval = ctl_scsiio(&io->scsiio);
13848 if (retval != CTL_RETVAL_COMPLETE)
13849 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13854 /* Sleep until we have something to do. */
13855 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13860 ctl_lun_thread(void *arg)
13862 struct ctl_softc *softc = (struct ctl_softc *)arg;
13863 struct ctl_be_lun *be_lun;
13866 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13870 mtx_lock(&softc->ctl_lock);
13871 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13872 if (be_lun != NULL) {
13873 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13874 mtx_unlock(&softc->ctl_lock);
13875 ctl_create_lun(be_lun);
13879 /* Sleep until we have something to do. */
13880 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13881 PDROP | PRIBIO, "-", 0);
13886 ctl_thresh_thread(void *arg)
13888 struct ctl_softc *softc = (struct ctl_softc *)arg;
13889 struct ctl_lun *lun;
13890 struct ctl_be_lun *be_lun;
13891 struct scsi_da_rw_recovery_page *rwpage;
13892 struct ctl_logical_block_provisioning_page *page;
13894 uint64_t thres, val;
13897 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13900 mtx_lock(&softc->ctl_lock);
13901 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13902 be_lun = lun->be_lun;
13903 if ((lun->flags & CTL_LUN_DISABLED) ||
13904 (lun->flags & CTL_LUN_OFFLINE) ||
13905 (be_lun->flags & CTL_LUN_FLAG_UNMAP) == 0 ||
13906 lun->backend->lun_attr == NULL)
13908 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT];
13909 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0)
13912 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT];
13913 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13914 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13916 thres = scsi_4btoul(page->descr[i].count);
13917 thres <<= CTL_LBP_EXPONENT;
13918 switch (page->descr[i].resource) {
13920 attr = "blocksavail";
13923 attr = "blocksused";
13926 attr = "poolblocksavail";
13929 attr = "poolblocksused";
13934 mtx_unlock(&softc->ctl_lock); // XXX
13935 val = lun->backend->lun_attr(
13936 lun->be_lun->be_lun, attr);
13937 mtx_lock(&softc->ctl_lock);
13938 if (val == UINT64_MAX)
13940 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13941 == SLBPPD_ARMING_INC)
13942 e |= (val >= thres);
13944 e |= (val <= thres);
13946 mtx_lock(&lun->lun_lock);
13948 if (lun->lasttpt == 0 ||
13949 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13950 lun->lasttpt = time_uptime;
13951 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13955 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13957 mtx_unlock(&lun->lun_lock);
13959 mtx_unlock(&softc->ctl_lock);
13960 pause("-", CTL_LBP_PERIOD * hz);
13965 ctl_enqueue_incoming(union ctl_io *io)
13967 struct ctl_softc *softc = control_softc;
13968 struct ctl_thread *thr;
13971 idx = (io->io_hdr.nexus.targ_port * 127 +
13972 io->io_hdr.nexus.initid.id) % worker_threads;
13973 thr = &softc->threads[idx];
13974 mtx_lock(&thr->queue_lock);
13975 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13976 mtx_unlock(&thr->queue_lock);
13981 ctl_enqueue_rtr(union ctl_io *io)
13983 struct ctl_softc *softc = control_softc;
13984 struct ctl_thread *thr;
13986 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13987 mtx_lock(&thr->queue_lock);
13988 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13989 mtx_unlock(&thr->queue_lock);
13994 ctl_enqueue_done(union ctl_io *io)
13996 struct ctl_softc *softc = control_softc;
13997 struct ctl_thread *thr;
13999 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14000 mtx_lock(&thr->queue_lock);
14001 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
14002 mtx_unlock(&thr->queue_lock);
14007 ctl_enqueue_isc(union ctl_io *io)
14009 struct ctl_softc *softc = control_softc;
14010 struct ctl_thread *thr;
14012 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14013 mtx_lock(&thr->queue_lock);
14014 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
14015 mtx_unlock(&thr->queue_lock);
14019 /* Initialization and failover */
14022 ctl_init_isc_msg(void)
14024 printf("CTL: Still calling this thing\n");
14029 * Initializes component into configuration defined by bootMode
14031 * returns hasc_Status:
14033 * ERROR - fatal error
14035 static ctl_ha_comp_status
14036 ctl_isc_init(struct ctl_ha_component *c)
14038 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
14045 * Starts component in state requested. If component starts successfully,
14046 * it must set its own state to the requestrd state
14047 * When requested state is HASC_STATE_HA, the component may refine it
14048 * by adding _SLAVE or _MASTER flags.
14049 * Currently allowed state transitions are:
14050 * UNKNOWN->HA - initial startup
14051 * UNKNOWN->SINGLE - initial startup when no parter detected
14052 * HA->SINGLE - failover
14053 * returns ctl_ha_comp_status:
14054 * OK - component successfully started in requested state
14055 * FAILED - could not start the requested state, failover may
14057 * ERROR - fatal error detected, no future startup possible
14059 static ctl_ha_comp_status
14060 ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state)
14062 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
14064 printf("%s: go\n", __func__);
14066 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap)
14067 if (c->state == CTL_HA_STATE_UNKNOWN ) {
14068 control_softc->is_single = 0;
14069 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
14070 != CTL_HA_STATUS_SUCCESS) {
14071 printf("ctl_isc_start: ctl_ha_msg_create failed.\n");
14072 ret = CTL_HA_COMP_STATUS_ERROR;
14074 } else if (CTL_HA_STATE_IS_HA(c->state)
14075 && CTL_HA_STATE_IS_SINGLE(state)){
14076 // HA->SINGLE transition
14078 control_softc->is_single = 1;
14080 printf("ctl_isc_start:Invalid state transition %X->%X\n",
14082 ret = CTL_HA_COMP_STATUS_ERROR;
14084 if (CTL_HA_STATE_IS_SINGLE(state))
14085 control_softc->is_single = 1;
14093 * Quiesce component
14094 * The component must clear any error conditions (set status to OK) and
14095 * prepare itself to another Start call
14096 * returns ctl_ha_comp_status:
14100 static ctl_ha_comp_status
14101 ctl_isc_quiesce(struct ctl_ha_component *c)
14103 int ret = CTL_HA_COMP_STATUS_OK;
14110 struct ctl_ha_component ctl_ha_component_ctlisc =
14113 .state = CTL_HA_STATE_UNKNOWN,
14114 .init = ctl_isc_init,
14115 .start = ctl_isc_start,
14116 .quiesce = ctl_isc_quiesce