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.
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 const 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 const 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 const 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 const 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 const 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 const 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 const 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 const 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 const 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 const 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 const 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 const 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 const 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 const 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 const 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 const 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 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
364 &worker_threads, 1, "Number of worker threads");
365 static int ctl_debug = CTL_DEBUG_NONE;
366 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN,
367 &ctl_debug, 0, "Enabled debug flags");
370 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
371 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
372 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0),
373 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
375 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 10
377 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
379 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
380 static int ctl_init(void);
381 void ctl_shutdown(void);
382 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
383 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
384 static void ctl_ioctl_online(void *arg);
385 static void ctl_ioctl_offline(void *arg);
386 static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id);
387 static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id);
388 static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio);
389 static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
390 static int ctl_ioctl_submit_wait(union ctl_io *io);
391 static void ctl_ioctl_datamove(union ctl_io *io);
392 static void ctl_ioctl_done(union ctl_io *io);
393 static void ctl_ioctl_hard_startstop_callback(void *arg,
394 struct cfi_metatask *metatask);
395 static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask);
396 static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
397 struct ctl_ooa *ooa_hdr,
398 struct ctl_ooa_entry *kern_entries);
399 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
401 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
402 struct ctl_be_lun *be_lun, struct ctl_id target_id);
403 static int ctl_free_lun(struct ctl_lun *lun);
404 static void ctl_create_lun(struct ctl_be_lun *be_lun);
405 static struct ctl_port * ctl_io_port(struct ctl_io_hdr *io_hdr);
407 static void ctl_failover_change_pages(struct ctl_softc *softc,
408 struct ctl_scsiio *ctsio, int master);
411 static int ctl_do_mode_select(union ctl_io *io);
412 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
413 uint64_t res_key, uint64_t sa_res_key,
414 uint8_t type, uint32_t residx,
415 struct ctl_scsiio *ctsio,
416 struct scsi_per_res_out *cdb,
417 struct scsi_per_res_out_parms* param);
418 static void ctl_pro_preempt_other(struct ctl_lun *lun,
419 union ctl_ha_msg *msg);
420 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
421 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
422 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
423 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
424 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
425 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
426 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
428 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
430 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
431 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
432 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
433 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
434 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
435 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2,
437 static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2);
438 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
439 union ctl_io *pending_io, union ctl_io *ooa_io);
440 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
441 union ctl_io *starting_io);
442 static int ctl_check_blocked(struct ctl_lun *lun);
443 static int ctl_scsiio_lun_check(struct ctl_lun *lun,
444 const struct ctl_cmd_entry *entry,
445 struct ctl_scsiio *ctsio);
446 //static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc);
447 static void ctl_failover(void);
448 static void ctl_clear_ua(struct ctl_softc *ctl_softc, uint32_t initidx,
449 ctl_ua_type ua_type);
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 *softc;
614 struct ctl_prio *presio;
615 ctl_ha_status isc_status;
617 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(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 (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 (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 (softc->ha_mode == CTL_HA_MODE_XFER)
856 ctl_isc_handler_finish_xfer(softc,
859 ctl_isc_handler_finish_ser_only(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 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 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 = 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 *softc;
1836 union ctl_ha_msg msg_info;
1837 struct ctl_lun *lun;
1841 softc = control_softc;
1843 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
1844 lun = 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 (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 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;
3415 list = (struct ctl_lun_list *)addr;
3417 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3419 list->status = CTL_LUN_LIST_ERROR;
3420 snprintf(list->error_str, sizeof(list->error_str),
3421 "Unable to allocate %d bytes for LUN list",
3426 sbuf_printf(sb, "<ctlportlist>\n");
3428 mtx_lock(&softc->ctl_lock);
3429 STAILQ_FOREACH(port, &softc->port_list, links) {
3430 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3431 (uintmax_t)port->targ_port);
3434 * Bail out as soon as we see that we've overfilled
3440 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3441 "</frontend_type>\n", port->frontend->name);
3445 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3450 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3451 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3455 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3460 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3461 port->physical_port);
3465 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3466 port->virtual_port);
3470 if (port->target_devid != NULL) {
3471 sbuf_printf(sb, "\t<target>");
3472 ctl_id_sbuf(port->target_devid, sb);
3473 sbuf_printf(sb, "</target>\n");
3476 if (port->port_devid != NULL) {
3477 sbuf_printf(sb, "\t<port>");
3478 ctl_id_sbuf(port->port_devid, sb);
3479 sbuf_printf(sb, "</port>\n");
3482 if (port->port_info != NULL) {
3483 retval = port->port_info(port->onoff_arg, sb);
3487 STAILQ_FOREACH(opt, &port->options, links) {
3488 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3489 opt->name, opt->value, opt->name);
3494 if (port->lun_map != NULL) {
3495 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n");
3496 for (j = 0; j < CTL_MAX_LUNS; j++) {
3497 plun = ctl_lun_map_from_port(port, j);
3498 if (plun >= CTL_MAX_LUNS)
3501 "\t<lun id=\"%u\">%u</lun>\n",
3506 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3507 if (port->wwpn_iid[j].in_use == 0 ||
3508 (port->wwpn_iid[j].wwpn == 0 &&
3509 port->wwpn_iid[j].name == NULL))
3512 if (port->wwpn_iid[j].name != NULL)
3513 retval = sbuf_printf(sb,
3514 "\t<initiator id=\"%u\">%s</initiator>\n",
3515 j, port->wwpn_iid[j].name);
3517 retval = sbuf_printf(sb,
3518 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3519 j, port->wwpn_iid[j].wwpn);
3526 retval = sbuf_printf(sb, "</targ_port>\n");
3530 mtx_unlock(&softc->ctl_lock);
3533 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3536 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3537 snprintf(list->error_str, sizeof(list->error_str),
3538 "Out of space, %d bytes is too small",
3545 retval = copyout(sbuf_data(sb), list->lun_xml,
3548 list->fill_len = sbuf_len(sb) + 1;
3549 list->status = CTL_LUN_LIST_OK;
3554 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr;
3555 struct ctl_port *port;
3557 mtx_lock(&softc->ctl_lock);
3558 if (lm->port >= CTL_MAX_PORTS ||
3559 (port = softc->ctl_ports[lm->port]) == NULL) {
3560 mtx_unlock(&softc->ctl_lock);
3563 if (lm->plun < CTL_MAX_LUNS) {
3564 if (lm->lun == UINT32_MAX)
3565 retval = ctl_lun_map_unset(port, lm->plun);
3566 else if (lm->lun < CTL_MAX_LUNS &&
3567 softc->ctl_luns[lm->lun] != NULL)
3568 retval = ctl_lun_map_set(port, lm->plun, lm->lun);
3570 mtx_unlock(&softc->ctl_lock);
3573 } else if (lm->plun == UINT32_MAX) {
3574 if (lm->lun == UINT32_MAX)
3575 retval = ctl_lun_map_deinit(port);
3577 retval = ctl_lun_map_init(port);
3579 mtx_unlock(&softc->ctl_lock);
3582 mtx_unlock(&softc->ctl_lock);
3586 /* XXX KDM should we fix this? */
3588 struct ctl_backend_driver *backend;
3595 * We encode the backend type as the ioctl type for backend
3596 * ioctls. So parse it out here, and then search for a
3597 * backend of this type.
3599 type = _IOC_TYPE(cmd);
3601 STAILQ_FOREACH(backend, &softc->be_list, links) {
3602 if (backend->type == type) {
3608 printf("ctl: unknown ioctl command %#lx or backend "
3613 retval = backend->ioctl(dev, cmd, addr, flag, td);
3623 ctl_get_initindex(struct ctl_nexus *nexus)
3625 if (nexus->targ_port < CTL_MAX_PORTS)
3626 return (nexus->initid.id +
3627 (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3629 return (nexus->initid.id +
3630 ((nexus->targ_port - CTL_MAX_PORTS) *
3631 CTL_MAX_INIT_PER_PORT));
3635 ctl_get_resindex(struct ctl_nexus *nexus)
3637 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3641 ctl_port_idx(int port_num)
3643 if (port_num < CTL_MAX_PORTS)
3646 return(port_num - CTL_MAX_PORTS);
3650 ctl_lun_map_init(struct ctl_port *port)
3654 if (port->lun_map == NULL)
3655 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS,
3657 if (port->lun_map == NULL)
3659 for (i = 0; i < CTL_MAX_LUNS; i++)
3660 port->lun_map[i] = UINT32_MAX;
3665 ctl_lun_map_deinit(struct ctl_port *port)
3668 if (port->lun_map == NULL)
3670 free(port->lun_map, M_CTL);
3671 port->lun_map = NULL;
3676 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun)
3680 if (port->lun_map == NULL) {
3681 status = ctl_lun_map_init(port);
3685 port->lun_map[plun] = glun;
3690 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun)
3693 if (port->lun_map == NULL)
3695 port->lun_map[plun] = UINT32_MAX;
3700 ctl_lun_map_unsetg(struct ctl_port *port, uint32_t glun)
3704 if (port->lun_map == NULL)
3706 for (i = 0; i < CTL_MAX_LUNS; i++) {
3707 if (port->lun_map[i] == glun)
3708 port->lun_map[i] = UINT32_MAX;
3714 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
3718 return (UINT32_MAX);
3719 if (port->lun_map == NULL || lun_id >= CTL_MAX_LUNS)
3721 return (port->lun_map[lun_id]);
3725 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
3730 return (UINT32_MAX);
3731 if (port->lun_map == NULL)
3733 for (i = 0; i < CTL_MAX_LUNS; i++) {
3734 if (port->lun_map[i] == lun_id)
3737 return (UINT32_MAX);
3740 static struct ctl_port *
3741 ctl_io_port(struct ctl_io_hdr *io_hdr)
3745 port_num = io_hdr->nexus.targ_port;
3746 return (control_softc->ctl_ports[ctl_port_idx(port_num)]);
3750 * Note: This only works for bitmask sizes that are at least 32 bits, and
3751 * that are a power of 2.
3754 ctl_ffz(uint32_t *mask, uint32_t size)
3756 uint32_t num_chunks, num_pieces;
3759 num_chunks = (size >> 5);
3760 if (num_chunks == 0)
3762 num_pieces = MIN((sizeof(uint32_t) * 8), size);
3764 for (i = 0; i < num_chunks; i++) {
3765 for (j = 0; j < num_pieces; j++) {
3766 if ((mask[i] & (1 << j)) == 0)
3767 return ((i << 5) + j);
3775 ctl_set_mask(uint32_t *mask, uint32_t bit)
3777 uint32_t chunk, piece;
3780 piece = bit % (sizeof(uint32_t) * 8);
3782 if ((mask[chunk] & (1 << piece)) != 0)
3785 mask[chunk] |= (1 << piece);
3791 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3793 uint32_t chunk, piece;
3796 piece = bit % (sizeof(uint32_t) * 8);
3798 if ((mask[chunk] & (1 << piece)) == 0)
3801 mask[chunk] &= ~(1 << piece);
3807 ctl_is_set(uint32_t *mask, uint32_t bit)
3809 uint32_t chunk, piece;
3812 piece = bit % (sizeof(uint32_t) * 8);
3814 if ((mask[chunk] & (1 << piece)) == 0)
3821 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3825 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3828 return (t[residx % CTL_MAX_INIT_PER_PORT]);
3832 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3836 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3839 t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3843 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3848 i = residx/CTL_MAX_INIT_PER_PORT;
3849 if (lun->pr_keys[i] != NULL)
3851 mtx_unlock(&lun->lun_lock);
3852 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3854 mtx_lock(&lun->lun_lock);
3855 if (lun->pr_keys[i] == NULL)
3856 lun->pr_keys[i] = p;
3862 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3866 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3867 KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3868 t[residx % CTL_MAX_INIT_PER_PORT] = key;
3872 * ctl_softc, pool_name, total_ctl_io are passed in.
3873 * npool is passed out.
3876 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3877 uint32_t total_ctl_io, void **npool)
3880 struct ctl_io_pool *pool;
3882 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3887 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3888 pool->ctl_softc = ctl_softc;
3889 pool->zone = uma_zsecond_create(pool->name, NULL,
3890 NULL, NULL, NULL, ctl_softc->io_zone);
3891 /* uma_prealloc(pool->zone, total_ctl_io); */
3895 *npool = ctl_softc->io_zone;
3901 ctl_pool_free(struct ctl_io_pool *pool)
3908 uma_zdestroy(pool->zone);
3914 ctl_alloc_io(void *pool_ref)
3918 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3920 io = uma_zalloc(pool->zone, M_WAITOK);
3922 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK);
3925 io->io_hdr.pool = pool_ref;
3930 ctl_alloc_io_nowait(void *pool_ref)
3934 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3936 io = uma_zalloc(pool->zone, M_NOWAIT);
3938 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT);
3941 io->io_hdr.pool = pool_ref;
3946 ctl_free_io(union ctl_io *io)
3949 struct ctl_io_pool *pool;
3956 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3957 uma_zfree(pool->zone, io);
3959 uma_zfree((uma_zone_t)io->io_hdr.pool, io);
3964 ctl_zero_io(union ctl_io *io)
3972 * May need to preserve linked list pointers at some point too.
3974 pool_ref = io->io_hdr.pool;
3975 memset(io, 0, sizeof(*io));
3976 io->io_hdr.pool = pool_ref;
3980 * This routine is currently used for internal copies of ctl_ios that need
3981 * to persist for some reason after we've already returned status to the
3982 * FETD. (Thus the flag set.)
3985 * Note that this makes a blind copy of all fields in the ctl_io, except
3986 * for the pool reference. This includes any memory that has been
3987 * allocated! That memory will no longer be valid after done has been
3988 * called, so this would be VERY DANGEROUS for command that actually does
3989 * any reads or writes. Right now (11/7/2005), this is only used for immediate
3990 * start and stop commands, which don't transfer any data, so this is not a
3991 * problem. If it is used for anything else, the caller would also need to
3992 * allocate data buffer space and this routine would need to be modified to
3993 * copy the data buffer(s) as well.
3996 ctl_copy_io(union ctl_io *src, union ctl_io *dest)
4005 * May need to preserve linked list pointers at some point too.
4007 pool_ref = dest->io_hdr.pool;
4009 memcpy(dest, src, MIN(sizeof(*src), sizeof(*dest)));
4011 dest->io_hdr.pool = pool_ref;
4013 * We need to know that this is an internal copy, and doesn't need
4014 * to get passed back to the FETD that allocated it.
4016 dest->io_hdr.flags |= CTL_FLAG_INT_COPY;
4020 ctl_expand_number(const char *buf, uint64_t *num)
4026 number = strtoq(buf, &endptr, 0);
4028 switch (tolower((unsigned char)*endptr)) {
4048 case '\0': /* No unit. */
4052 /* Unrecognized unit. */
4056 if ((number << shift) >> shift != number) {
4060 *num = number << shift;
4066 * This routine could be used in the future to load default and/or saved
4067 * mode page parameters for a particuar lun.
4070 ctl_init_page_index(struct ctl_lun *lun)
4073 struct ctl_page_index *page_index;
4077 memcpy(&lun->mode_pages.index, page_index_template,
4078 sizeof(page_index_template));
4080 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4082 page_index = &lun->mode_pages.index[i];
4084 * If this is a disk-only mode page, there's no point in
4085 * setting it up. For some pages, we have to have some
4086 * basic information about the disk in order to calculate the
4089 if ((lun->be_lun->lun_type != T_DIRECT)
4090 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
4093 switch (page_index->page_code & SMPH_PC_MASK) {
4094 case SMS_RW_ERROR_RECOVERY_PAGE: {
4095 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4096 panic("subpage is incorrect!");
4097 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
4098 &rw_er_page_default,
4099 sizeof(rw_er_page_default));
4100 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
4101 &rw_er_page_changeable,
4102 sizeof(rw_er_page_changeable));
4103 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
4104 &rw_er_page_default,
4105 sizeof(rw_er_page_default));
4106 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
4107 &rw_er_page_default,
4108 sizeof(rw_er_page_default));
4109 page_index->page_data =
4110 (uint8_t *)lun->mode_pages.rw_er_page;
4113 case SMS_FORMAT_DEVICE_PAGE: {
4114 struct scsi_format_page *format_page;
4116 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4117 panic("subpage is incorrect!");
4120 * Sectors per track are set above. Bytes per
4121 * sector need to be set here on a per-LUN basis.
4123 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
4124 &format_page_default,
4125 sizeof(format_page_default));
4126 memcpy(&lun->mode_pages.format_page[
4127 CTL_PAGE_CHANGEABLE], &format_page_changeable,
4128 sizeof(format_page_changeable));
4129 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
4130 &format_page_default,
4131 sizeof(format_page_default));
4132 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
4133 &format_page_default,
4134 sizeof(format_page_default));
4136 format_page = &lun->mode_pages.format_page[
4138 scsi_ulto2b(lun->be_lun->blocksize,
4139 format_page->bytes_per_sector);
4141 format_page = &lun->mode_pages.format_page[
4143 scsi_ulto2b(lun->be_lun->blocksize,
4144 format_page->bytes_per_sector);
4146 format_page = &lun->mode_pages.format_page[
4148 scsi_ulto2b(lun->be_lun->blocksize,
4149 format_page->bytes_per_sector);
4151 page_index->page_data =
4152 (uint8_t *)lun->mode_pages.format_page;
4155 case SMS_RIGID_DISK_PAGE: {
4156 struct scsi_rigid_disk_page *rigid_disk_page;
4157 uint32_t sectors_per_cylinder;
4161 #endif /* !__XSCALE__ */
4163 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4164 panic("invalid subpage value %d",
4165 page_index->subpage);
4168 * Rotation rate and sectors per track are set
4169 * above. We calculate the cylinders here based on
4170 * capacity. Due to the number of heads and
4171 * sectors per track we're using, smaller arrays
4172 * may turn out to have 0 cylinders. Linux and
4173 * FreeBSD don't pay attention to these mode pages
4174 * to figure out capacity, but Solaris does. It
4175 * seems to deal with 0 cylinders just fine, and
4176 * works out a fake geometry based on the capacity.
4178 memcpy(&lun->mode_pages.rigid_disk_page[
4179 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4180 sizeof(rigid_disk_page_default));
4181 memcpy(&lun->mode_pages.rigid_disk_page[
4182 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4183 sizeof(rigid_disk_page_changeable));
4185 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4189 * The divide method here will be more accurate,
4190 * probably, but results in floating point being
4191 * used in the kernel on i386 (__udivdi3()). On the
4192 * XScale, though, __udivdi3() is implemented in
4195 * The shift method for cylinder calculation is
4196 * accurate if sectors_per_cylinder is a power of
4197 * 2. Otherwise it might be slightly off -- you
4198 * might have a bit of a truncation problem.
4201 cylinders = (lun->be_lun->maxlba + 1) /
4202 sectors_per_cylinder;
4204 for (shift = 31; shift > 0; shift--) {
4205 if (sectors_per_cylinder & (1 << shift))
4208 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4212 * We've basically got 3 bytes, or 24 bits for the
4213 * cylinder size in the mode page. If we're over,
4214 * just round down to 2^24.
4216 if (cylinders > 0xffffff)
4217 cylinders = 0xffffff;
4219 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4221 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4223 if ((value = ctl_get_opt(&lun->be_lun->options,
4225 scsi_ulto2b(strtol(value, NULL, 0),
4226 rigid_disk_page->rotation_rate);
4229 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
4230 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4231 sizeof(rigid_disk_page_default));
4232 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
4233 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4234 sizeof(rigid_disk_page_default));
4236 page_index->page_data =
4237 (uint8_t *)lun->mode_pages.rigid_disk_page;
4240 case SMS_CACHING_PAGE: {
4241 struct scsi_caching_page *caching_page;
4243 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4244 panic("invalid subpage value %d",
4245 page_index->subpage);
4246 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4247 &caching_page_default,
4248 sizeof(caching_page_default));
4249 memcpy(&lun->mode_pages.caching_page[
4250 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4251 sizeof(caching_page_changeable));
4252 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4253 &caching_page_default,
4254 sizeof(caching_page_default));
4255 caching_page = &lun->mode_pages.caching_page[
4257 value = ctl_get_opt(&lun->be_lun->options, "writecache");
4258 if (value != NULL && strcmp(value, "off") == 0)
4259 caching_page->flags1 &= ~SCP_WCE;
4260 value = ctl_get_opt(&lun->be_lun->options, "readcache");
4261 if (value != NULL && strcmp(value, "off") == 0)
4262 caching_page->flags1 |= SCP_RCD;
4263 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4264 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4265 sizeof(caching_page_default));
4266 page_index->page_data =
4267 (uint8_t *)lun->mode_pages.caching_page;
4270 case SMS_CONTROL_MODE_PAGE: {
4271 struct scsi_control_page *control_page;
4273 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4274 panic("invalid subpage value %d",
4275 page_index->subpage);
4277 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT],
4278 &control_page_default,
4279 sizeof(control_page_default));
4280 memcpy(&lun->mode_pages.control_page[
4281 CTL_PAGE_CHANGEABLE], &control_page_changeable,
4282 sizeof(control_page_changeable));
4283 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED],
4284 &control_page_default,
4285 sizeof(control_page_default));
4286 control_page = &lun->mode_pages.control_page[
4288 value = ctl_get_opt(&lun->be_lun->options, "reordering");
4289 if (value != NULL && strcmp(value, "unrestricted") == 0) {
4290 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK;
4291 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED;
4293 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT],
4294 &lun->mode_pages.control_page[CTL_PAGE_SAVED],
4295 sizeof(control_page_default));
4296 page_index->page_data =
4297 (uint8_t *)lun->mode_pages.control_page;
4301 case SMS_INFO_EXCEPTIONS_PAGE: {
4302 switch (page_index->subpage) {
4303 case SMS_SUBPAGE_PAGE_0:
4304 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4306 sizeof(ie_page_default));
4307 memcpy(&lun->mode_pages.ie_page[
4308 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4309 sizeof(ie_page_changeable));
4310 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4312 sizeof(ie_page_default));
4313 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4315 sizeof(ie_page_default));
4316 page_index->page_data =
4317 (uint8_t *)lun->mode_pages.ie_page;
4320 struct ctl_logical_block_provisioning_page *page;
4322 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4324 sizeof(lbp_page_default));
4325 memcpy(&lun->mode_pages.lbp_page[
4326 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4327 sizeof(lbp_page_changeable));
4328 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4330 sizeof(lbp_page_default));
4331 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4332 value = ctl_get_opt(&lun->be_lun->options,
4334 if (value != NULL &&
4335 ctl_expand_number(value, &ival) == 0) {
4336 page->descr[0].flags |= SLBPPD_ENABLED |
4338 if (lun->be_lun->blocksize)
4339 ival /= lun->be_lun->blocksize;
4342 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4343 page->descr[0].count);
4345 value = ctl_get_opt(&lun->be_lun->options,
4347 if (value != NULL &&
4348 ctl_expand_number(value, &ival) == 0) {
4349 page->descr[1].flags |= SLBPPD_ENABLED |
4351 if (lun->be_lun->blocksize)
4352 ival /= lun->be_lun->blocksize;
4355 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4356 page->descr[1].count);
4358 value = ctl_get_opt(&lun->be_lun->options,
4359 "pool-avail-threshold");
4360 if (value != NULL &&
4361 ctl_expand_number(value, &ival) == 0) {
4362 page->descr[2].flags |= SLBPPD_ENABLED |
4364 if (lun->be_lun->blocksize)
4365 ival /= lun->be_lun->blocksize;
4368 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4369 page->descr[2].count);
4371 value = ctl_get_opt(&lun->be_lun->options,
4372 "pool-used-threshold");
4373 if (value != NULL &&
4374 ctl_expand_number(value, &ival) == 0) {
4375 page->descr[3].flags |= SLBPPD_ENABLED |
4377 if (lun->be_lun->blocksize)
4378 ival /= lun->be_lun->blocksize;
4381 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4382 page->descr[3].count);
4384 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4385 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4386 sizeof(lbp_page_default));
4387 page_index->page_data =
4388 (uint8_t *)lun->mode_pages.lbp_page;
4392 case SMS_VENDOR_SPECIFIC_PAGE:{
4393 switch (page_index->subpage) {
4394 case DBGCNF_SUBPAGE_CODE: {
4395 struct copan_debugconf_subpage *current_page,
4398 memcpy(&lun->mode_pages.debugconf_subpage[
4400 &debugconf_page_default,
4401 sizeof(debugconf_page_default));
4402 memcpy(&lun->mode_pages.debugconf_subpage[
4403 CTL_PAGE_CHANGEABLE],
4404 &debugconf_page_changeable,
4405 sizeof(debugconf_page_changeable));
4406 memcpy(&lun->mode_pages.debugconf_subpage[
4408 &debugconf_page_default,
4409 sizeof(debugconf_page_default));
4410 memcpy(&lun->mode_pages.debugconf_subpage[
4412 &debugconf_page_default,
4413 sizeof(debugconf_page_default));
4414 page_index->page_data =
4415 (uint8_t *)lun->mode_pages.debugconf_subpage;
4417 current_page = (struct copan_debugconf_subpage *)
4418 (page_index->page_data +
4419 (page_index->page_len *
4421 saved_page = (struct copan_debugconf_subpage *)
4422 (page_index->page_data +
4423 (page_index->page_len *
4428 panic("invalid subpage value %d",
4429 page_index->subpage);
4435 panic("invalid page value %d",
4436 page_index->page_code & SMPH_PC_MASK);
4441 return (CTL_RETVAL_COMPLETE);
4445 ctl_init_log_page_index(struct ctl_lun *lun)
4447 struct ctl_page_index *page_index;
4450 memcpy(&lun->log_pages.index, log_page_index_template,
4451 sizeof(log_page_index_template));
4454 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4456 page_index = &lun->log_pages.index[i];
4458 * If this is a disk-only mode page, there's no point in
4459 * setting it up. For some pages, we have to have some
4460 * basic information about the disk in order to calculate the
4463 if ((lun->be_lun->lun_type != T_DIRECT)
4464 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
4467 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4468 lun->backend->lun_attr == NULL)
4471 if (page_index->page_code != prev) {
4472 lun->log_pages.pages_page[j] = page_index->page_code;
4473 prev = page_index->page_code;
4476 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4477 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4480 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4481 lun->log_pages.index[0].page_len = j;
4482 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4483 lun->log_pages.index[1].page_len = k * 2;
4484 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4485 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4486 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page;
4487 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page);
4489 return (CTL_RETVAL_COMPLETE);
4493 hex2bin(const char *str, uint8_t *buf, int buf_size)
4498 memset(buf, 0, buf_size);
4499 while (isspace(str[0]))
4501 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4504 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4508 else if (isalpha(c))
4509 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4515 buf[i / 2] |= (c << 4);
4519 return ((i + 1) / 2);
4526 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4527 * wants us to allocate the LUN and he can block.
4528 * - ctl_softc is always set
4529 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4531 * Returns 0 for success, non-zero (errno) for failure.
4534 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4535 struct ctl_be_lun *const be_lun, struct ctl_id target_id)
4537 struct ctl_lun *nlun, *lun;
4538 struct ctl_port *port;
4539 struct scsi_vpd_id_descriptor *desc;
4540 struct scsi_vpd_id_t10 *t10id;
4541 const char *eui, *naa, *scsiname, *vendor, *value;
4542 int lun_number, i, lun_malloced;
4543 int devidlen, idlen1, idlen2 = 0, len;
4549 * We currently only support Direct Access or Processor LUN types.
4551 switch (be_lun->lun_type) {
4559 be_lun->lun_config_status(be_lun->be_lun,
4560 CTL_LUN_CONFIG_FAILURE);
4563 if (ctl_lun == NULL) {
4564 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4571 memset(lun, 0, sizeof(*lun));
4573 lun->flags = CTL_LUN_MALLOCED;
4575 /* Generate LUN ID. */
4576 devidlen = max(CTL_DEVID_MIN_LEN,
4577 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4578 idlen1 = sizeof(*t10id) + devidlen;
4579 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4580 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4581 if (scsiname != NULL) {
4582 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4583 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4585 eui = ctl_get_opt(&be_lun->options, "eui");
4587 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4589 naa = ctl_get_opt(&be_lun->options, "naa");
4591 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4593 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4594 M_CTL, M_WAITOK | M_ZERO);
4595 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4596 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4597 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4598 desc->length = idlen1;
4599 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4600 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4601 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4602 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4604 strncpy(t10id->vendor, vendor,
4605 min(sizeof(t10id->vendor), strlen(vendor)));
4607 strncpy((char *)t10id->vendor_spec_id,
4608 (char *)be_lun->device_id, devidlen);
4609 if (scsiname != NULL) {
4610 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4612 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4613 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4614 SVPD_ID_TYPE_SCSI_NAME;
4615 desc->length = idlen2;
4616 strlcpy(desc->identifier, scsiname, idlen2);
4619 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4621 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4622 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4624 desc->length = hex2bin(eui, desc->identifier, 16);
4625 desc->length = desc->length > 12 ? 16 :
4626 (desc->length > 8 ? 12 : 8);
4627 len -= 16 - desc->length;
4630 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4632 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4633 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4635 desc->length = hex2bin(naa, desc->identifier, 16);
4636 desc->length = desc->length > 8 ? 16 : 8;
4637 len -= 16 - desc->length;
4639 lun->lun_devid->len = len;
4641 mtx_lock(&ctl_softc->ctl_lock);
4643 * See if the caller requested a particular LUN number. If so, see
4644 * if it is available. Otherwise, allocate the first available LUN.
4646 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4647 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4648 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4649 mtx_unlock(&ctl_softc->ctl_lock);
4650 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4651 printf("ctl: requested LUN ID %d is higher "
4652 "than CTL_MAX_LUNS - 1 (%d)\n",
4653 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4656 * XXX KDM return an error, or just assign
4657 * another LUN ID in this case??
4659 printf("ctl: requested LUN ID %d is already "
4660 "in use\n", be_lun->req_lun_id);
4662 if (lun->flags & CTL_LUN_MALLOCED)
4664 be_lun->lun_config_status(be_lun->be_lun,
4665 CTL_LUN_CONFIG_FAILURE);
4668 lun_number = be_lun->req_lun_id;
4670 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS);
4671 if (lun_number == -1) {
4672 mtx_unlock(&ctl_softc->ctl_lock);
4673 printf("ctl: can't allocate LUN on target %ju, out of "
4674 "LUNs\n", (uintmax_t)target_id.id);
4675 if (lun->flags & CTL_LUN_MALLOCED)
4677 be_lun->lun_config_status(be_lun->be_lun,
4678 CTL_LUN_CONFIG_FAILURE);
4682 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4684 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4685 lun->target = target_id;
4686 lun->lun = lun_number;
4687 lun->be_lun = be_lun;
4689 * The processor LUN is always enabled. Disk LUNs come on line
4690 * disabled, and must be enabled by the backend.
4692 lun->flags |= CTL_LUN_DISABLED;
4693 lun->backend = be_lun->be;
4694 be_lun->ctl_lun = lun;
4695 be_lun->lun_id = lun_number;
4696 atomic_add_int(&be_lun->be->num_luns, 1);
4697 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE)
4698 lun->flags |= CTL_LUN_OFFLINE;
4700 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4701 lun->flags |= CTL_LUN_STOPPED;
4703 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4704 lun->flags |= CTL_LUN_INOPERABLE;
4706 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4707 lun->flags |= CTL_LUN_PRIMARY_SC;
4709 value = ctl_get_opt(&be_lun->options, "readonly");
4710 if (value != NULL && strcmp(value, "on") == 0)
4711 lun->flags |= CTL_LUN_READONLY;
4713 lun->serseq = CTL_LUN_SERSEQ_OFF;
4714 if (be_lun->flags & CTL_LUN_FLAG_SERSEQ_READ)
4715 lun->serseq = CTL_LUN_SERSEQ_READ;
4716 value = ctl_get_opt(&be_lun->options, "serseq");
4717 if (value != NULL && strcmp(value, "on") == 0)
4718 lun->serseq = CTL_LUN_SERSEQ_ON;
4719 else if (value != NULL && strcmp(value, "read") == 0)
4720 lun->serseq = CTL_LUN_SERSEQ_READ;
4721 else if (value != NULL && strcmp(value, "off") == 0)
4722 lun->serseq = CTL_LUN_SERSEQ_OFF;
4724 lun->ctl_softc = ctl_softc;
4726 lun->last_busy = getsbinuptime();
4728 TAILQ_INIT(&lun->ooa_queue);
4729 TAILQ_INIT(&lun->blocked_queue);
4730 STAILQ_INIT(&lun->error_list);
4731 ctl_tpc_lun_init(lun);
4734 * Initialize the mode and log page index.
4736 ctl_init_page_index(lun);
4737 ctl_init_log_page_index(lun);
4740 * Now, before we insert this lun on the lun list, set the lun
4741 * inventory changed UA for all other luns.
4743 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4744 mtx_lock(&nlun->lun_lock);
4745 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4746 mtx_unlock(&nlun->lun_lock);
4749 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4751 ctl_softc->ctl_luns[lun_number] = lun;
4753 ctl_softc->num_luns++;
4755 /* Setup statistics gathering */
4756 lun->stats.device_type = be_lun->lun_type;
4757 lun->stats.lun_number = lun_number;
4758 if (lun->stats.device_type == T_DIRECT)
4759 lun->stats.blocksize = be_lun->blocksize;
4761 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4762 for (i = 0;i < CTL_MAX_PORTS;i++)
4763 lun->stats.ports[i].targ_port = i;
4765 mtx_unlock(&ctl_softc->ctl_lock);
4767 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4770 * Run through each registered FETD and bring it online if it isn't
4771 * already. Enable the target ID if it hasn't been enabled, and
4772 * enable this particular LUN.
4774 STAILQ_FOREACH(port, &ctl_softc->port_list, links) {
4777 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number);
4779 printf("ctl_alloc_lun: FETD %s port %d returned error "
4780 "%d for lun_enable on target %ju lun %d\n",
4781 port->port_name, port->targ_port, retval,
4782 (uintmax_t)target_id.id, lun_number);
4784 port->status |= CTL_PORT_STATUS_LUN_ONLINE;
4792 * - LUN has already been marked invalid and any pending I/O has been taken
4796 ctl_free_lun(struct ctl_lun *lun)
4798 struct ctl_softc *softc;
4799 struct ctl_port *port;
4800 struct ctl_lun *nlun;
4803 softc = lun->ctl_softc;
4805 mtx_assert(&softc->ctl_lock, MA_OWNED);
4807 STAILQ_FOREACH(port, &softc->port_list, links)
4808 ctl_lun_map_unsetg(port, lun->lun);
4810 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4812 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4814 softc->ctl_luns[lun->lun] = NULL;
4816 if (!TAILQ_EMPTY(&lun->ooa_queue))
4817 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4822 * XXX KDM this scheme only works for a single target/multiple LUN
4823 * setup. It needs to be revamped for a multiple target scheme.
4825 * XXX KDM this results in port->lun_disable() getting called twice,
4826 * once when ctl_disable_lun() is called, and a second time here.
4827 * We really need to re-think the LUN disable semantics. There
4828 * should probably be several steps/levels to LUN removal:
4833 * Right now we only have a disable method when communicating to
4834 * the front end ports, at least for individual LUNs.
4837 STAILQ_FOREACH(port, &softc->port_list, links) {
4840 retval = port->lun_disable(port->targ_lun_arg, lun->target,
4843 printf("ctl_free_lun: FETD %s port %d returned error "
4844 "%d for lun_disable on target %ju lun %jd\n",
4845 port->port_name, port->targ_port, retval,
4846 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4849 if (STAILQ_FIRST(&softc->lun_list) == NULL) {
4850 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE;
4852 retval = port->targ_disable(port->targ_lun_arg,lun->target);
4854 printf("ctl_free_lun: FETD %s port %d "
4855 "returned error %d for targ_disable on "
4856 "target %ju\n", port->port_name,
4857 port->targ_port, retval,
4858 (uintmax_t)lun->target.id);
4860 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE;
4862 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0)
4866 port->port_offline(port->onoff_arg);
4867 port->status &= ~CTL_PORT_STATUS_ONLINE;
4874 * Tell the backend to free resources, if this LUN has a backend.
4876 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4877 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4879 ctl_tpc_lun_shutdown(lun);
4880 mtx_destroy(&lun->lun_lock);
4881 free(lun->lun_devid, M_CTL);
4882 for (i = 0; i < CTL_MAX_PORTS; i++)
4883 free(lun->pending_ua[i], M_CTL);
4884 for (i = 0; i < 2 * CTL_MAX_PORTS; i++)
4885 free(lun->pr_keys[i], M_CTL);
4886 free(lun->write_buffer, M_CTL);
4887 if (lun->flags & CTL_LUN_MALLOCED)
4890 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4891 mtx_lock(&nlun->lun_lock);
4892 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4893 mtx_unlock(&nlun->lun_lock);
4900 ctl_create_lun(struct ctl_be_lun *be_lun)
4902 struct ctl_softc *softc;
4904 softc = control_softc;
4907 * ctl_alloc_lun() should handle all potential failure cases.
4909 ctl_alloc_lun(softc, NULL, be_lun, softc->target);
4913 ctl_add_lun(struct ctl_be_lun *be_lun)
4915 struct ctl_softc *softc = control_softc;
4917 mtx_lock(&softc->ctl_lock);
4918 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links);
4919 mtx_unlock(&softc->ctl_lock);
4920 wakeup(&softc->pending_lun_queue);
4926 ctl_enable_lun(struct ctl_be_lun *be_lun)
4928 struct ctl_softc *softc;
4929 struct ctl_port *port, *nport;
4930 struct ctl_lun *lun;
4933 lun = (struct ctl_lun *)be_lun->ctl_lun;
4934 softc = lun->ctl_softc;
4936 mtx_lock(&softc->ctl_lock);
4937 mtx_lock(&lun->lun_lock);
4938 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4940 * eh? Why did we get called if the LUN is already
4943 mtx_unlock(&lun->lun_lock);
4944 mtx_unlock(&softc->ctl_lock);
4947 lun->flags &= ~CTL_LUN_DISABLED;
4948 mtx_unlock(&lun->lun_lock);
4950 for (port = STAILQ_FIRST(&softc->port_list); port != NULL; port = nport) {
4951 nport = STAILQ_NEXT(port, links);
4954 * Drop the lock while we call the FETD's enable routine.
4955 * This can lead to a callback into CTL (at least in the
4956 * case of the internal initiator frontend.
4958 mtx_unlock(&softc->ctl_lock);
4959 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun);
4960 mtx_lock(&softc->ctl_lock);
4962 printf("%s: FETD %s port %d returned error "
4963 "%d for lun_enable on target %ju lun %jd\n",
4964 __func__, port->port_name, port->targ_port, retval,
4965 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4969 /* NOTE: TODO: why does lun enable affect port status? */
4970 port->status |= CTL_PORT_STATUS_LUN_ONLINE;
4975 mtx_unlock(&softc->ctl_lock);
4981 ctl_disable_lun(struct ctl_be_lun *be_lun)
4983 struct ctl_softc *softc;
4984 struct ctl_port *port;
4985 struct ctl_lun *lun;
4988 lun = (struct ctl_lun *)be_lun->ctl_lun;
4989 softc = lun->ctl_softc;
4991 mtx_lock(&softc->ctl_lock);
4992 mtx_lock(&lun->lun_lock);
4993 if (lun->flags & CTL_LUN_DISABLED) {
4994 mtx_unlock(&lun->lun_lock);
4995 mtx_unlock(&softc->ctl_lock);
4998 lun->flags |= CTL_LUN_DISABLED;
4999 mtx_unlock(&lun->lun_lock);
5001 STAILQ_FOREACH(port, &softc->port_list, links) {
5002 mtx_unlock(&softc->ctl_lock);
5004 * Drop the lock before we call the frontend's disable
5005 * routine, to avoid lock order reversals.
5007 * XXX KDM what happens if the frontend list changes while
5008 * we're traversing it? It's unlikely, but should be handled.
5010 retval = port->lun_disable(port->targ_lun_arg, lun->target,
5012 mtx_lock(&softc->ctl_lock);
5014 printf("ctl_alloc_lun: FETD %s port %d returned error "
5015 "%d for lun_disable on target %ju lun %jd\n",
5016 port->port_name, port->targ_port, retval,
5017 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
5021 mtx_unlock(&softc->ctl_lock);
5027 ctl_start_lun(struct ctl_be_lun *be_lun)
5029 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5031 mtx_lock(&lun->lun_lock);
5032 lun->flags &= ~CTL_LUN_STOPPED;
5033 mtx_unlock(&lun->lun_lock);
5038 ctl_stop_lun(struct ctl_be_lun *be_lun)
5040 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5042 mtx_lock(&lun->lun_lock);
5043 lun->flags |= CTL_LUN_STOPPED;
5044 mtx_unlock(&lun->lun_lock);
5049 ctl_lun_offline(struct ctl_be_lun *be_lun)
5051 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5053 mtx_lock(&lun->lun_lock);
5054 lun->flags |= CTL_LUN_OFFLINE;
5055 mtx_unlock(&lun->lun_lock);
5060 ctl_lun_online(struct ctl_be_lun *be_lun)
5062 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5064 mtx_lock(&lun->lun_lock);
5065 lun->flags &= ~CTL_LUN_OFFLINE;
5066 mtx_unlock(&lun->lun_lock);
5071 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
5073 struct ctl_softc *softc;
5074 struct ctl_lun *lun;
5076 lun = (struct ctl_lun *)be_lun->ctl_lun;
5077 softc = lun->ctl_softc;
5079 mtx_lock(&lun->lun_lock);
5082 * The LUN needs to be disabled before it can be marked invalid.
5084 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
5085 mtx_unlock(&lun->lun_lock);
5089 * Mark the LUN invalid.
5091 lun->flags |= CTL_LUN_INVALID;
5094 * If there is nothing in the OOA queue, go ahead and free the LUN.
5095 * If we have something in the OOA queue, we'll free it when the
5096 * last I/O completes.
5098 if (TAILQ_EMPTY(&lun->ooa_queue)) {
5099 mtx_unlock(&lun->lun_lock);
5100 mtx_lock(&softc->ctl_lock);
5102 mtx_unlock(&softc->ctl_lock);
5104 mtx_unlock(&lun->lun_lock);
5110 ctl_lun_inoperable(struct ctl_be_lun *be_lun)
5112 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5114 mtx_lock(&lun->lun_lock);
5115 lun->flags |= CTL_LUN_INOPERABLE;
5116 mtx_unlock(&lun->lun_lock);
5121 ctl_lun_operable(struct ctl_be_lun *be_lun)
5123 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5125 mtx_lock(&lun->lun_lock);
5126 lun->flags &= ~CTL_LUN_INOPERABLE;
5127 mtx_unlock(&lun->lun_lock);
5132 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
5134 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5136 mtx_lock(&lun->lun_lock);
5137 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGED);
5138 mtx_unlock(&lun->lun_lock);
5142 * Backend "memory move is complete" callback for requests that never
5143 * make it down to say RAIDCore's configuration code.
5146 ctl_config_move_done(union ctl_io *io)
5150 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
5151 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
5152 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
5154 if ((io->io_hdr.port_status != 0) &&
5155 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5156 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5158 * For hardware error sense keys, the sense key
5159 * specific value is defined to be a retry count,
5160 * but we use it to pass back an internal FETD
5161 * error code. XXX KDM Hopefully the FETD is only
5162 * using 16 bits for an error code, since that's
5163 * all the space we have in the sks field.
5165 ctl_set_internal_failure(&io->scsiio,
5168 io->io_hdr.port_status);
5171 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
5172 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5173 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
5174 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5176 * XXX KDM just assuming a single pointer here, and not a
5177 * S/G list. If we start using S/G lists for config data,
5178 * we'll need to know how to clean them up here as well.
5180 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5181 free(io->scsiio.kern_data_ptr, M_CTL);
5183 retval = CTL_RETVAL_COMPLETE;
5186 * XXX KDM now we need to continue data movement. Some
5188 * - call ctl_scsiio() again? We don't do this for data
5189 * writes, because for those at least we know ahead of
5190 * time where the write will go and how long it is. For
5191 * config writes, though, that information is largely
5192 * contained within the write itself, thus we need to
5193 * parse out the data again.
5195 * - Call some other function once the data is in?
5197 if (ctl_debug & CTL_DEBUG_CDB_DATA)
5201 * XXX KDM call ctl_scsiio() again for now, and check flag
5202 * bits to see whether we're allocated or not.
5204 retval = ctl_scsiio(&io->scsiio);
5210 * This gets called by a backend driver when it is done with a
5211 * data_submit method.
5214 ctl_data_submit_done(union ctl_io *io)
5217 * If the IO_CONT flag is set, we need to call the supplied
5218 * function to continue processing the I/O, instead of completing
5221 * If there is an error, though, we don't want to keep processing.
5222 * Instead, just send status back to the initiator.
5224 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5225 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5226 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5227 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5228 io->scsiio.io_cont(io);
5235 * This gets called by a backend driver when it is done with a
5236 * configuration write.
5239 ctl_config_write_done(union ctl_io *io)
5244 * If the IO_CONT flag is set, we need to call the supplied
5245 * function to continue processing the I/O, instead of completing
5248 * If there is an error, though, we don't want to keep processing.
5249 * Instead, just send status back to the initiator.
5251 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5252 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5253 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5254 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5255 io->scsiio.io_cont(io);
5259 * Since a configuration write can be done for commands that actually
5260 * have data allocated, like write buffer, and commands that have
5261 * no data, like start/stop unit, we need to check here.
5263 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5264 buf = io->scsiio.kern_data_ptr;
5273 ctl_config_read_done(union ctl_io *io)
5278 * If there is some error -- we are done, skip data transfer.
5280 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
5281 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5282 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
5283 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5284 buf = io->scsiio.kern_data_ptr;
5294 * If the IO_CONT flag is set, we need to call the supplied
5295 * function to continue processing the I/O, instead of completing
5298 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
5299 io->scsiio.io_cont(io);
5307 * SCSI release command.
5310 ctl_scsi_release(struct ctl_scsiio *ctsio)
5312 int length, longid, thirdparty_id, resv_id;
5313 struct ctl_lun *lun;
5319 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5321 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5322 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5324 switch (ctsio->cdb[0]) {
5326 struct scsi_release_10 *cdb;
5328 cdb = (struct scsi_release_10 *)ctsio->cdb;
5330 if (cdb->byte2 & SR10_LONGID)
5333 thirdparty_id = cdb->thirdparty_id;
5335 resv_id = cdb->resv_id;
5336 length = scsi_2btoul(cdb->length);
5343 * XXX KDM right now, we only support LUN reservation. We don't
5344 * support 3rd party reservations, or extent reservations, which
5345 * might actually need the parameter list. If we've gotten this
5346 * far, we've got a LUN reservation. Anything else got kicked out
5347 * above. So, according to SPC, ignore the length.
5351 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5353 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5354 ctsio->kern_data_len = length;
5355 ctsio->kern_total_len = length;
5356 ctsio->kern_data_resid = 0;
5357 ctsio->kern_rel_offset = 0;
5358 ctsio->kern_sg_entries = 0;
5359 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5360 ctsio->be_move_done = ctl_config_move_done;
5361 ctl_datamove((union ctl_io *)ctsio);
5363 return (CTL_RETVAL_COMPLETE);
5367 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5369 mtx_lock(&lun->lun_lock);
5372 * According to SPC, it is not an error for an intiator to attempt
5373 * to release a reservation on a LUN that isn't reserved, or that
5374 * is reserved by another initiator. The reservation can only be
5375 * released, though, by the initiator who made it or by one of
5376 * several reset type events.
5378 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5379 lun->flags &= ~CTL_LUN_RESERVED;
5381 mtx_unlock(&lun->lun_lock);
5383 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5384 free(ctsio->kern_data_ptr, M_CTL);
5385 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5388 ctl_set_success(ctsio);
5389 ctl_done((union ctl_io *)ctsio);
5390 return (CTL_RETVAL_COMPLETE);
5394 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5396 int extent, thirdparty, longid;
5397 int resv_id, length;
5398 uint64_t thirdparty_id;
5399 struct ctl_lun *lun;
5409 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5411 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5412 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5414 switch (ctsio->cdb[0]) {
5416 struct scsi_reserve_10 *cdb;
5418 cdb = (struct scsi_reserve_10 *)ctsio->cdb;
5420 if (cdb->byte2 & SR10_LONGID)
5423 thirdparty_id = cdb->thirdparty_id;
5425 resv_id = cdb->resv_id;
5426 length = scsi_2btoul(cdb->length);
5432 * XXX KDM right now, we only support LUN reservation. We don't
5433 * support 3rd party reservations, or extent reservations, which
5434 * might actually need the parameter list. If we've gotten this
5435 * far, we've got a LUN reservation. Anything else got kicked out
5436 * above. So, according to SPC, ignore the length.
5440 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5442 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5443 ctsio->kern_data_len = length;
5444 ctsio->kern_total_len = length;
5445 ctsio->kern_data_resid = 0;
5446 ctsio->kern_rel_offset = 0;
5447 ctsio->kern_sg_entries = 0;
5448 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5449 ctsio->be_move_done = ctl_config_move_done;
5450 ctl_datamove((union ctl_io *)ctsio);
5452 return (CTL_RETVAL_COMPLETE);
5456 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5458 mtx_lock(&lun->lun_lock);
5459 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5460 ctl_set_reservation_conflict(ctsio);
5464 lun->flags |= CTL_LUN_RESERVED;
5465 lun->res_idx = residx;
5467 ctl_set_success(ctsio);
5470 mtx_unlock(&lun->lun_lock);
5472 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5473 free(ctsio->kern_data_ptr, M_CTL);
5474 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5477 ctl_done((union ctl_io *)ctsio);
5478 return (CTL_RETVAL_COMPLETE);
5482 ctl_start_stop(struct ctl_scsiio *ctsio)
5484 struct scsi_start_stop_unit *cdb;
5485 struct ctl_lun *lun;
5488 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5490 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5493 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5497 * We don't support the immediate bit on a stop unit. In order to
5498 * do that, we would need to code up a way to know that a stop is
5499 * pending, and hold off any new commands until it completes, one
5500 * way or another. Then we could accept or reject those commands
5501 * depending on its status. We would almost need to do the reverse
5502 * of what we do below for an immediate start -- return the copy of
5503 * the ctl_io to the FETD with status to send to the host (and to
5504 * free the copy!) and then free the original I/O once the stop
5505 * actually completes. That way, the OOA queue mechanism can work
5506 * to block commands that shouldn't proceed. Another alternative
5507 * would be to put the copy in the queue in place of the original,
5508 * and return the original back to the caller. That could be
5511 if ((cdb->byte2 & SSS_IMMED)
5512 && ((cdb->how & SSS_START) == 0)) {
5513 ctl_set_invalid_field(ctsio,
5519 ctl_done((union ctl_io *)ctsio);
5520 return (CTL_RETVAL_COMPLETE);
5523 if ((lun->flags & CTL_LUN_PR_RESERVED)
5524 && ((cdb->how & SSS_START)==0)) {
5527 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5528 if (ctl_get_prkey(lun, residx) == 0
5529 || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5531 ctl_set_reservation_conflict(ctsio);
5532 ctl_done((union ctl_io *)ctsio);
5533 return (CTL_RETVAL_COMPLETE);
5538 * If there is no backend on this device, we can't start or stop
5539 * it. In theory we shouldn't get any start/stop commands in the
5540 * first place at this level if the LUN doesn't have a backend.
5541 * That should get stopped by the command decode code.
5543 if (lun->backend == NULL) {
5544 ctl_set_invalid_opcode(ctsio);
5545 ctl_done((union ctl_io *)ctsio);
5546 return (CTL_RETVAL_COMPLETE);
5550 * XXX KDM Copan-specific offline behavior.
5551 * Figure out a reasonable way to port this?
5554 mtx_lock(&lun->lun_lock);
5556 if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
5557 && (lun->flags & CTL_LUN_OFFLINE)) {
5559 * If the LUN is offline, and the on/offline bit isn't set,
5560 * reject the start or stop. Otherwise, let it through.
5562 mtx_unlock(&lun->lun_lock);
5563 ctl_set_lun_not_ready(ctsio);
5564 ctl_done((union ctl_io *)ctsio);
5566 mtx_unlock(&lun->lun_lock);
5567 #endif /* NEEDTOPORT */
5569 * This could be a start or a stop when we're online,
5570 * or a stop/offline or start/online. A start or stop when
5571 * we're offline is covered in the case above.
5574 * In the non-immediate case, we send the request to
5575 * the backend and return status to the user when
5578 * In the immediate case, we allocate a new ctl_io
5579 * to hold a copy of the request, and send that to
5580 * the backend. We then set good status on the
5581 * user's request and return it immediately.
5583 if (cdb->byte2 & SSS_IMMED) {
5584 union ctl_io *new_io;
5586 new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5587 ctl_copy_io((union ctl_io *)ctsio, new_io);
5588 retval = lun->backend->config_write(new_io);
5589 ctl_set_success(ctsio);
5590 ctl_done((union ctl_io *)ctsio);
5592 retval = lun->backend->config_write(
5593 (union ctl_io *)ctsio);
5602 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5603 * we don't really do anything with the LBA and length fields if the user
5604 * passes them in. Instead we'll just flush out the cache for the entire
5608 ctl_sync_cache(struct ctl_scsiio *ctsio)
5610 struct ctl_lun *lun;
5611 struct ctl_softc *softc;
5612 uint64_t starting_lba;
5613 uint32_t block_count;
5616 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5618 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5619 softc = lun->ctl_softc;
5622 switch (ctsio->cdb[0]) {
5623 case SYNCHRONIZE_CACHE: {
5624 struct scsi_sync_cache *cdb;
5625 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5627 starting_lba = scsi_4btoul(cdb->begin_lba);
5628 block_count = scsi_2btoul(cdb->lb_count);
5631 case SYNCHRONIZE_CACHE_16: {
5632 struct scsi_sync_cache_16 *cdb;
5633 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5635 starting_lba = scsi_8btou64(cdb->begin_lba);
5636 block_count = scsi_4btoul(cdb->lb_count);
5640 ctl_set_invalid_opcode(ctsio);
5641 ctl_done((union ctl_io *)ctsio);
5643 break; /* NOTREACHED */
5647 * We check the LBA and length, but don't do anything with them.
5648 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5649 * get flushed. This check will just help satisfy anyone who wants
5650 * to see an error for an out of range LBA.
5652 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5653 ctl_set_lba_out_of_range(ctsio);
5654 ctl_done((union ctl_io *)ctsio);
5659 * If this LUN has no backend, we can't flush the cache anyway.
5661 if (lun->backend == NULL) {
5662 ctl_set_invalid_opcode(ctsio);
5663 ctl_done((union ctl_io *)ctsio);
5668 * Check to see whether we're configured to send the SYNCHRONIZE
5669 * CACHE command directly to the back end.
5671 mtx_lock(&lun->lun_lock);
5672 if ((softc->flags & CTL_FLAG_REAL_SYNC)
5673 && (++(lun->sync_count) >= lun->sync_interval)) {
5674 lun->sync_count = 0;
5675 mtx_unlock(&lun->lun_lock);
5676 retval = lun->backend->config_write((union ctl_io *)ctsio);
5678 mtx_unlock(&lun->lun_lock);
5679 ctl_set_success(ctsio);
5680 ctl_done((union ctl_io *)ctsio);
5689 ctl_format(struct ctl_scsiio *ctsio)
5691 struct scsi_format *cdb;
5692 struct ctl_lun *lun;
5693 int length, defect_list_len;
5695 CTL_DEBUG_PRINT(("ctl_format\n"));
5697 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5699 cdb = (struct scsi_format *)ctsio->cdb;
5702 if (cdb->byte2 & SF_FMTDATA) {
5703 if (cdb->byte2 & SF_LONGLIST)
5704 length = sizeof(struct scsi_format_header_long);
5706 length = sizeof(struct scsi_format_header_short);
5709 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5711 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5712 ctsio->kern_data_len = length;
5713 ctsio->kern_total_len = length;
5714 ctsio->kern_data_resid = 0;
5715 ctsio->kern_rel_offset = 0;
5716 ctsio->kern_sg_entries = 0;
5717 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5718 ctsio->be_move_done = ctl_config_move_done;
5719 ctl_datamove((union ctl_io *)ctsio);
5721 return (CTL_RETVAL_COMPLETE);
5724 defect_list_len = 0;
5726 if (cdb->byte2 & SF_FMTDATA) {
5727 if (cdb->byte2 & SF_LONGLIST) {
5728 struct scsi_format_header_long *header;
5730 header = (struct scsi_format_header_long *)
5731 ctsio->kern_data_ptr;
5733 defect_list_len = scsi_4btoul(header->defect_list_len);
5734 if (defect_list_len != 0) {
5735 ctl_set_invalid_field(ctsio,
5744 struct scsi_format_header_short *header;
5746 header = (struct scsi_format_header_short *)
5747 ctsio->kern_data_ptr;
5749 defect_list_len = scsi_2btoul(header->defect_list_len);
5750 if (defect_list_len != 0) {
5751 ctl_set_invalid_field(ctsio,
5763 * The format command will clear out the "Medium format corrupted"
5764 * status if set by the configuration code. That status is really
5765 * just a way to notify the host that we have lost the media, and
5766 * get them to issue a command that will basically make them think
5767 * they're blowing away the media.
5769 mtx_lock(&lun->lun_lock);
5770 lun->flags &= ~CTL_LUN_INOPERABLE;
5771 mtx_unlock(&lun->lun_lock);
5773 ctl_set_success(ctsio);
5776 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5777 free(ctsio->kern_data_ptr, M_CTL);
5778 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5781 ctl_done((union ctl_io *)ctsio);
5782 return (CTL_RETVAL_COMPLETE);
5786 ctl_read_buffer(struct ctl_scsiio *ctsio)
5788 struct scsi_read_buffer *cdb;
5789 struct ctl_lun *lun;
5790 int buffer_offset, len;
5791 static uint8_t descr[4];
5792 static uint8_t echo_descr[4] = { 0 };
5794 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5796 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5797 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5799 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA &&
5800 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR &&
5801 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) {
5802 ctl_set_invalid_field(ctsio,
5808 ctl_done((union ctl_io *)ctsio);
5809 return (CTL_RETVAL_COMPLETE);
5812 len = scsi_3btoul(cdb->length);
5813 buffer_offset = scsi_3btoul(cdb->offset);
5815 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5816 ctl_set_invalid_field(ctsio,
5822 ctl_done((union ctl_io *)ctsio);
5823 return (CTL_RETVAL_COMPLETE);
5826 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5828 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5829 ctsio->kern_data_ptr = descr;
5830 len = min(len, sizeof(descr));
5831 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5832 ctsio->kern_data_ptr = echo_descr;
5833 len = min(len, sizeof(echo_descr));
5835 if (lun->write_buffer == NULL) {
5836 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5839 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5841 ctsio->kern_data_len = len;
5842 ctsio->kern_total_len = len;
5843 ctsio->kern_data_resid = 0;
5844 ctsio->kern_rel_offset = 0;
5845 ctsio->kern_sg_entries = 0;
5846 ctl_set_success(ctsio);
5847 ctsio->be_move_done = ctl_config_move_done;
5848 ctl_datamove((union ctl_io *)ctsio);
5849 return (CTL_RETVAL_COMPLETE);
5853 ctl_write_buffer(struct ctl_scsiio *ctsio)
5855 struct scsi_write_buffer *cdb;
5856 struct ctl_lun *lun;
5857 int buffer_offset, len;
5859 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5861 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5862 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5864 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5865 ctl_set_invalid_field(ctsio,
5871 ctl_done((union ctl_io *)ctsio);
5872 return (CTL_RETVAL_COMPLETE);
5875 len = scsi_3btoul(cdb->length);
5876 buffer_offset = scsi_3btoul(cdb->offset);
5878 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5879 ctl_set_invalid_field(ctsio,
5885 ctl_done((union ctl_io *)ctsio);
5886 return (CTL_RETVAL_COMPLETE);
5890 * If we've got a kernel request that hasn't been malloced yet,
5891 * malloc it and tell the caller the data buffer is here.
5893 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5894 if (lun->write_buffer == NULL) {
5895 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5898 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5899 ctsio->kern_data_len = len;
5900 ctsio->kern_total_len = len;
5901 ctsio->kern_data_resid = 0;
5902 ctsio->kern_rel_offset = 0;
5903 ctsio->kern_sg_entries = 0;
5904 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5905 ctsio->be_move_done = ctl_config_move_done;
5906 ctl_datamove((union ctl_io *)ctsio);
5908 return (CTL_RETVAL_COMPLETE);
5911 ctl_set_success(ctsio);
5912 ctl_done((union ctl_io *)ctsio);
5913 return (CTL_RETVAL_COMPLETE);
5917 ctl_write_same(struct ctl_scsiio *ctsio)
5919 struct ctl_lun *lun;
5920 struct ctl_lba_len_flags *lbalen;
5922 uint32_t num_blocks;
5926 retval = CTL_RETVAL_COMPLETE;
5928 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5930 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5932 switch (ctsio->cdb[0]) {
5933 case WRITE_SAME_10: {
5934 struct scsi_write_same_10 *cdb;
5936 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5938 lba = scsi_4btoul(cdb->addr);
5939 num_blocks = scsi_2btoul(cdb->length);
5943 case WRITE_SAME_16: {
5944 struct scsi_write_same_16 *cdb;
5946 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5948 lba = scsi_8btou64(cdb->addr);
5949 num_blocks = scsi_4btoul(cdb->length);
5955 * We got a command we don't support. This shouldn't
5956 * happen, commands should be filtered out above us.
5958 ctl_set_invalid_opcode(ctsio);
5959 ctl_done((union ctl_io *)ctsio);
5961 return (CTL_RETVAL_COMPLETE);
5962 break; /* NOTREACHED */
5965 /* NDOB and ANCHOR flags can be used only together with UNMAP */
5966 if ((byte2 & SWS_UNMAP) == 0 &&
5967 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) {
5968 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5969 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5970 ctl_done((union ctl_io *)ctsio);
5971 return (CTL_RETVAL_COMPLETE);
5975 * The first check is to make sure we're in bounds, the second
5976 * check is to catch wrap-around problems. If the lba + num blocks
5977 * is less than the lba, then we've wrapped around and the block
5978 * range is invalid anyway.
5980 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5981 || ((lba + num_blocks) < lba)) {
5982 ctl_set_lba_out_of_range(ctsio);
5983 ctl_done((union ctl_io *)ctsio);
5984 return (CTL_RETVAL_COMPLETE);
5987 /* Zero number of blocks means "to the last logical block" */
5988 if (num_blocks == 0) {
5989 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5990 ctl_set_invalid_field(ctsio,
5996 ctl_done((union ctl_io *)ctsio);
5997 return (CTL_RETVAL_COMPLETE);
5999 num_blocks = (lun->be_lun->maxlba + 1) - lba;
6002 len = lun->be_lun->blocksize;
6005 * If we've got a kernel request that hasn't been malloced yet,
6006 * malloc it and tell the caller the data buffer is here.
6008 if ((byte2 & SWS_NDOB) == 0 &&
6009 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6010 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
6011 ctsio->kern_data_len = len;
6012 ctsio->kern_total_len = len;
6013 ctsio->kern_data_resid = 0;
6014 ctsio->kern_rel_offset = 0;
6015 ctsio->kern_sg_entries = 0;
6016 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6017 ctsio->be_move_done = ctl_config_move_done;
6018 ctl_datamove((union ctl_io *)ctsio);
6020 return (CTL_RETVAL_COMPLETE);
6023 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
6025 lbalen->len = num_blocks;
6026 lbalen->flags = byte2;
6027 retval = lun->backend->config_write((union ctl_io *)ctsio);
6033 ctl_unmap(struct ctl_scsiio *ctsio)
6035 struct ctl_lun *lun;
6036 struct scsi_unmap *cdb;
6037 struct ctl_ptr_len_flags *ptrlen;
6038 struct scsi_unmap_header *hdr;
6039 struct scsi_unmap_desc *buf, *end, *endnz, *range;
6041 uint32_t num_blocks;
6045 retval = CTL_RETVAL_COMPLETE;
6047 CTL_DEBUG_PRINT(("ctl_unmap\n"));
6049 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6050 cdb = (struct scsi_unmap *)ctsio->cdb;
6052 len = scsi_2btoul(cdb->length);
6056 * If we've got a kernel request that hasn't been malloced yet,
6057 * malloc it and tell the caller the data buffer is here.
6059 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6060 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
6061 ctsio->kern_data_len = len;
6062 ctsio->kern_total_len = len;
6063 ctsio->kern_data_resid = 0;
6064 ctsio->kern_rel_offset = 0;
6065 ctsio->kern_sg_entries = 0;
6066 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6067 ctsio->be_move_done = ctl_config_move_done;
6068 ctl_datamove((union ctl_io *)ctsio);
6070 return (CTL_RETVAL_COMPLETE);
6073 len = ctsio->kern_total_len - ctsio->kern_data_resid;
6074 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
6075 if (len < sizeof (*hdr) ||
6076 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
6077 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
6078 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
6079 ctl_set_invalid_field(ctsio,
6087 len = scsi_2btoul(hdr->desc_length);
6088 buf = (struct scsi_unmap_desc *)(hdr + 1);
6089 end = buf + len / sizeof(*buf);
6092 for (range = buf; range < end; range++) {
6093 lba = scsi_8btou64(range->lba);
6094 num_blocks = scsi_4btoul(range->length);
6095 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
6096 || ((lba + num_blocks) < lba)) {
6097 ctl_set_lba_out_of_range(ctsio);
6098 ctl_done((union ctl_io *)ctsio);
6099 return (CTL_RETVAL_COMPLETE);
6101 if (num_blocks != 0)
6106 * Block backend can not handle zero last range.
6107 * Filter it out and return if there is nothing left.
6109 len = (uint8_t *)endnz - (uint8_t *)buf;
6111 ctl_set_success(ctsio);
6115 mtx_lock(&lun->lun_lock);
6116 ptrlen = (struct ctl_ptr_len_flags *)
6117 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
6118 ptrlen->ptr = (void *)buf;
6120 ptrlen->flags = byte2;
6121 ctl_check_blocked(lun);
6122 mtx_unlock(&lun->lun_lock);
6124 retval = lun->backend->config_write((union ctl_io *)ctsio);
6128 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
6129 free(ctsio->kern_data_ptr, M_CTL);
6130 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
6132 ctl_done((union ctl_io *)ctsio);
6133 return (CTL_RETVAL_COMPLETE);
6137 * Note that this function currently doesn't actually do anything inside
6138 * CTL to enforce things if the DQue bit is turned on.
6140 * Also note that this function can't be used in the default case, because
6141 * the DQue bit isn't set in the changeable mask for the control mode page
6142 * anyway. This is just here as an example for how to implement a page
6143 * handler, and a placeholder in case we want to allow the user to turn
6144 * tagged queueing on and off.
6146 * The D_SENSE bit handling is functional, however, and will turn
6147 * descriptor sense on and off for a given LUN.
6150 ctl_control_page_handler(struct ctl_scsiio *ctsio,
6151 struct ctl_page_index *page_index, uint8_t *page_ptr)
6153 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
6154 struct ctl_lun *lun;
6158 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6159 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6162 user_cp = (struct scsi_control_page *)page_ptr;
6163 current_cp = (struct scsi_control_page *)
6164 (page_index->page_data + (page_index->page_len *
6166 saved_cp = (struct scsi_control_page *)
6167 (page_index->page_data + (page_index->page_len *
6170 mtx_lock(&lun->lun_lock);
6171 if (((current_cp->rlec & SCP_DSENSE) == 0)
6172 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
6174 * Descriptor sense is currently turned off and the user
6175 * wants to turn it on.
6177 current_cp->rlec |= SCP_DSENSE;
6178 saved_cp->rlec |= SCP_DSENSE;
6179 lun->flags |= CTL_LUN_SENSE_DESC;
6181 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
6182 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
6184 * Descriptor sense is currently turned on, and the user
6185 * wants to turn it off.
6187 current_cp->rlec &= ~SCP_DSENSE;
6188 saved_cp->rlec &= ~SCP_DSENSE;
6189 lun->flags &= ~CTL_LUN_SENSE_DESC;
6192 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) !=
6193 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) {
6194 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
6195 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
6196 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
6197 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
6200 if ((current_cp->eca_and_aen & SCP_SWP) !=
6201 (user_cp->eca_and_aen & SCP_SWP)) {
6202 current_cp->eca_and_aen &= ~SCP_SWP;
6203 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
6204 saved_cp->eca_and_aen &= ~SCP_SWP;
6205 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
6209 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
6210 mtx_unlock(&lun->lun_lock);
6216 ctl_caching_sp_handler(struct ctl_scsiio *ctsio,
6217 struct ctl_page_index *page_index, uint8_t *page_ptr)
6219 struct scsi_caching_page *current_cp, *saved_cp, *user_cp;
6220 struct ctl_lun *lun;
6224 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6225 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6228 user_cp = (struct scsi_caching_page *)page_ptr;
6229 current_cp = (struct scsi_caching_page *)
6230 (page_index->page_data + (page_index->page_len *
6232 saved_cp = (struct scsi_caching_page *)
6233 (page_index->page_data + (page_index->page_len *
6236 mtx_lock(&lun->lun_lock);
6237 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) !=
6238 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) {
6239 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
6240 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
6241 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
6242 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
6246 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
6247 mtx_unlock(&lun->lun_lock);
6253 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
6254 struct ctl_page_index *page_index,
6260 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
6265 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
6266 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
6267 printf("page data:");
6269 printf(" %.2x",page_ptr[i]);
6275 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
6276 struct ctl_page_index *page_index,
6279 struct copan_debugconf_subpage *page;
6281 page = (struct copan_debugconf_subpage *)page_index->page_data +
6282 (page_index->page_len * pc);
6285 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6286 case SMS_PAGE_CTRL_DEFAULT >> 6:
6287 case SMS_PAGE_CTRL_SAVED >> 6:
6289 * We don't update the changable or default bits for this page.
6292 case SMS_PAGE_CTRL_CURRENT >> 6:
6293 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
6294 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
6298 EPRINT(0, "Invalid PC %d!!", pc);
6299 #endif /* NEEDTOPORT */
6307 ctl_do_mode_select(union ctl_io *io)
6309 struct scsi_mode_page_header *page_header;
6310 struct ctl_page_index *page_index;
6311 struct ctl_scsiio *ctsio;
6312 int control_dev, page_len;
6313 int page_len_offset, page_len_size;
6314 union ctl_modepage_info *modepage_info;
6315 struct ctl_lun *lun;
6316 int *len_left, *len_used;
6319 ctsio = &io->scsiio;
6322 retval = CTL_RETVAL_COMPLETE;
6324 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6326 if (lun->be_lun->lun_type != T_DIRECT)
6331 modepage_info = (union ctl_modepage_info *)
6332 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6333 len_left = &modepage_info->header.len_left;
6334 len_used = &modepage_info->header.len_used;
6338 page_header = (struct scsi_mode_page_header *)
6339 (ctsio->kern_data_ptr + *len_used);
6341 if (*len_left == 0) {
6342 free(ctsio->kern_data_ptr, M_CTL);
6343 ctl_set_success(ctsio);
6344 ctl_done((union ctl_io *)ctsio);
6345 return (CTL_RETVAL_COMPLETE);
6346 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6348 free(ctsio->kern_data_ptr, M_CTL);
6349 ctl_set_param_len_error(ctsio);
6350 ctl_done((union ctl_io *)ctsio);
6351 return (CTL_RETVAL_COMPLETE);
6353 } else if ((page_header->page_code & SMPH_SPF)
6354 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6356 free(ctsio->kern_data_ptr, M_CTL);
6357 ctl_set_param_len_error(ctsio);
6358 ctl_done((union ctl_io *)ctsio);
6359 return (CTL_RETVAL_COMPLETE);
6364 * XXX KDM should we do something with the block descriptor?
6366 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6368 if ((control_dev != 0)
6369 && (lun->mode_pages.index[i].page_flags &
6370 CTL_PAGE_FLAG_DISK_ONLY))
6373 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
6374 (page_header->page_code & SMPH_PC_MASK))
6378 * If neither page has a subpage code, then we've got a
6381 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
6382 && ((page_header->page_code & SMPH_SPF) == 0)) {
6383 page_index = &lun->mode_pages.index[i];
6384 page_len = page_header->page_length;
6389 * If both pages have subpages, then the subpage numbers
6392 if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
6393 && (page_header->page_code & SMPH_SPF)) {
6394 struct scsi_mode_page_header_sp *sph;
6396 sph = (struct scsi_mode_page_header_sp *)page_header;
6398 if (lun->mode_pages.index[i].subpage ==
6400 page_index = &lun->mode_pages.index[i];
6401 page_len = scsi_2btoul(sph->page_length);
6408 * If we couldn't find the page, or if we don't have a mode select
6409 * handler for it, send back an error to the user.
6411 if ((page_index == NULL)
6412 || (page_index->select_handler == NULL)) {
6413 ctl_set_invalid_field(ctsio,
6416 /*field*/ *len_used,
6419 free(ctsio->kern_data_ptr, M_CTL);
6420 ctl_done((union ctl_io *)ctsio);
6421 return (CTL_RETVAL_COMPLETE);
6424 if (page_index->page_code & SMPH_SPF) {
6425 page_len_offset = 2;
6429 page_len_offset = 1;
6433 * If the length the initiator gives us isn't the one we specify in
6434 * the mode page header, or if they didn't specify enough data in
6435 * the CDB to avoid truncating this page, kick out the request.
6437 if ((page_len != (page_index->page_len - page_len_offset -
6439 || (*len_left < page_index->page_len)) {
6442 ctl_set_invalid_field(ctsio,
6445 /*field*/ *len_used + page_len_offset,
6448 free(ctsio->kern_data_ptr, M_CTL);
6449 ctl_done((union ctl_io *)ctsio);
6450 return (CTL_RETVAL_COMPLETE);
6454 * Run through the mode page, checking to make sure that the bits
6455 * the user changed are actually legal for him to change.
6457 for (i = 0; i < page_index->page_len; i++) {
6458 uint8_t *user_byte, *change_mask, *current_byte;
6462 user_byte = (uint8_t *)page_header + i;
6463 change_mask = page_index->page_data +
6464 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6465 current_byte = page_index->page_data +
6466 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6469 * Check to see whether the user set any bits in this byte
6470 * that he is not allowed to set.
6472 if ((*user_byte & ~(*change_mask)) ==
6473 (*current_byte & ~(*change_mask)))
6477 * Go through bit by bit to determine which one is illegal.
6480 for (j = 7; j >= 0; j--) {
6481 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6482 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6487 ctl_set_invalid_field(ctsio,
6490 /*field*/ *len_used + i,
6493 free(ctsio->kern_data_ptr, M_CTL);
6494 ctl_done((union ctl_io *)ctsio);
6495 return (CTL_RETVAL_COMPLETE);
6499 * Decrement these before we call the page handler, since we may
6500 * end up getting called back one way or another before the handler
6501 * returns to this context.
6503 *len_left -= page_index->page_len;
6504 *len_used += page_index->page_len;
6506 retval = page_index->select_handler(ctsio, page_index,
6507 (uint8_t *)page_header);
6510 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6511 * wait until this queued command completes to finish processing
6512 * the mode page. If it returns anything other than
6513 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6514 * already set the sense information, freed the data pointer, and
6515 * completed the io for us.
6517 if (retval != CTL_RETVAL_COMPLETE)
6518 goto bailout_no_done;
6521 * If the initiator sent us more than one page, parse the next one.
6526 ctl_set_success(ctsio);
6527 free(ctsio->kern_data_ptr, M_CTL);
6528 ctl_done((union ctl_io *)ctsio);
6532 return (CTL_RETVAL_COMPLETE);
6537 ctl_mode_select(struct ctl_scsiio *ctsio)
6539 int param_len, pf, sp;
6540 int header_size, bd_len;
6541 int len_left, len_used;
6542 struct ctl_page_index *page_index;
6543 struct ctl_lun *lun;
6544 int control_dev, page_len;
6545 union ctl_modepage_info *modepage_info;
6557 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6559 if (lun->be_lun->lun_type != T_DIRECT)
6564 switch (ctsio->cdb[0]) {
6565 case MODE_SELECT_6: {
6566 struct scsi_mode_select_6 *cdb;
6568 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6570 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6571 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6573 param_len = cdb->length;
6574 header_size = sizeof(struct scsi_mode_header_6);
6577 case MODE_SELECT_10: {
6578 struct scsi_mode_select_10 *cdb;
6580 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6582 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6583 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6585 param_len = scsi_2btoul(cdb->length);
6586 header_size = sizeof(struct scsi_mode_header_10);
6590 ctl_set_invalid_opcode(ctsio);
6591 ctl_done((union ctl_io *)ctsio);
6592 return (CTL_RETVAL_COMPLETE);
6593 break; /* NOTREACHED */
6598 * "A parameter list length of zero indicates that the Data-Out Buffer
6599 * shall be empty. This condition shall not be considered as an error."
6601 if (param_len == 0) {
6602 ctl_set_success(ctsio);
6603 ctl_done((union ctl_io *)ctsio);
6604 return (CTL_RETVAL_COMPLETE);
6608 * Since we'll hit this the first time through, prior to
6609 * allocation, we don't need to free a data buffer here.
6611 if (param_len < header_size) {
6612 ctl_set_param_len_error(ctsio);
6613 ctl_done((union ctl_io *)ctsio);
6614 return (CTL_RETVAL_COMPLETE);
6618 * Allocate the data buffer and grab the user's data. In theory,
6619 * we shouldn't have to sanity check the parameter list length here
6620 * because the maximum size is 64K. We should be able to malloc
6621 * that much without too many problems.
6623 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6624 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6625 ctsio->kern_data_len = param_len;
6626 ctsio->kern_total_len = param_len;
6627 ctsio->kern_data_resid = 0;
6628 ctsio->kern_rel_offset = 0;
6629 ctsio->kern_sg_entries = 0;
6630 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6631 ctsio->be_move_done = ctl_config_move_done;
6632 ctl_datamove((union ctl_io *)ctsio);
6634 return (CTL_RETVAL_COMPLETE);
6637 switch (ctsio->cdb[0]) {
6638 case MODE_SELECT_6: {
6639 struct scsi_mode_header_6 *mh6;
6641 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6642 bd_len = mh6->blk_desc_len;
6645 case MODE_SELECT_10: {
6646 struct scsi_mode_header_10 *mh10;
6648 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6649 bd_len = scsi_2btoul(mh10->blk_desc_len);
6653 panic("Invalid CDB type %#x", ctsio->cdb[0]);
6657 if (param_len < (header_size + bd_len)) {
6658 free(ctsio->kern_data_ptr, M_CTL);
6659 ctl_set_param_len_error(ctsio);
6660 ctl_done((union ctl_io *)ctsio);
6661 return (CTL_RETVAL_COMPLETE);
6665 * Set the IO_CONT flag, so that if this I/O gets passed to
6666 * ctl_config_write_done(), it'll get passed back to
6667 * ctl_do_mode_select() for further processing, or completion if
6670 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6671 ctsio->io_cont = ctl_do_mode_select;
6673 modepage_info = (union ctl_modepage_info *)
6674 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6676 memset(modepage_info, 0, sizeof(*modepage_info));
6678 len_left = param_len - header_size - bd_len;
6679 len_used = header_size + bd_len;
6681 modepage_info->header.len_left = len_left;
6682 modepage_info->header.len_used = len_used;
6684 return (ctl_do_mode_select((union ctl_io *)ctsio));
6688 ctl_mode_sense(struct ctl_scsiio *ctsio)
6690 struct ctl_lun *lun;
6691 int pc, page_code, dbd, llba, subpage;
6692 int alloc_len, page_len, header_len, total_len;
6693 struct scsi_mode_block_descr *block_desc;
6694 struct ctl_page_index *page_index;
6702 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6704 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6706 if (lun->be_lun->lun_type != T_DIRECT)
6711 switch (ctsio->cdb[0]) {
6712 case MODE_SENSE_6: {
6713 struct scsi_mode_sense_6 *cdb;
6715 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6717 header_len = sizeof(struct scsi_mode_hdr_6);
6718 if (cdb->byte2 & SMS_DBD)
6721 header_len += sizeof(struct scsi_mode_block_descr);
6723 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6724 page_code = cdb->page & SMS_PAGE_CODE;
6725 subpage = cdb->subpage;
6726 alloc_len = cdb->length;
6729 case MODE_SENSE_10: {
6730 struct scsi_mode_sense_10 *cdb;
6732 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6734 header_len = sizeof(struct scsi_mode_hdr_10);
6736 if (cdb->byte2 & SMS_DBD)
6739 header_len += sizeof(struct scsi_mode_block_descr);
6740 if (cdb->byte2 & SMS10_LLBAA)
6742 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6743 page_code = cdb->page & SMS_PAGE_CODE;
6744 subpage = cdb->subpage;
6745 alloc_len = scsi_2btoul(cdb->length);
6749 ctl_set_invalid_opcode(ctsio);
6750 ctl_done((union ctl_io *)ctsio);
6751 return (CTL_RETVAL_COMPLETE);
6752 break; /* NOTREACHED */
6756 * We have to make a first pass through to calculate the size of
6757 * the pages that match the user's query. Then we allocate enough
6758 * memory to hold it, and actually copy the data into the buffer.
6760 switch (page_code) {
6761 case SMS_ALL_PAGES_PAGE: {
6767 * At the moment, values other than 0 and 0xff here are
6768 * reserved according to SPC-3.
6770 if ((subpage != SMS_SUBPAGE_PAGE_0)
6771 && (subpage != SMS_SUBPAGE_ALL)) {
6772 ctl_set_invalid_field(ctsio,
6778 ctl_done((union ctl_io *)ctsio);
6779 return (CTL_RETVAL_COMPLETE);
6782 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6783 if ((control_dev != 0)
6784 && (lun->mode_pages.index[i].page_flags &
6785 CTL_PAGE_FLAG_DISK_ONLY))
6789 * We don't use this subpage if the user didn't
6790 * request all subpages.
6792 if ((lun->mode_pages.index[i].subpage != 0)
6793 && (subpage == SMS_SUBPAGE_PAGE_0))
6797 printf("found page %#x len %d\n",
6798 lun->mode_pages.index[i].page_code &
6800 lun->mode_pages.index[i].page_len);
6802 page_len += lun->mode_pages.index[i].page_len;
6811 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6812 /* Look for the right page code */
6813 if ((lun->mode_pages.index[i].page_code &
6814 SMPH_PC_MASK) != page_code)
6817 /* Look for the right subpage or the subpage wildcard*/
6818 if ((lun->mode_pages.index[i].subpage != subpage)
6819 && (subpage != SMS_SUBPAGE_ALL))
6822 /* Make sure the page is supported for this dev type */
6823 if ((control_dev != 0)
6824 && (lun->mode_pages.index[i].page_flags &
6825 CTL_PAGE_FLAG_DISK_ONLY))
6829 printf("found page %#x len %d\n",
6830 lun->mode_pages.index[i].page_code &
6832 lun->mode_pages.index[i].page_len);
6835 page_len += lun->mode_pages.index[i].page_len;
6838 if (page_len == 0) {
6839 ctl_set_invalid_field(ctsio,
6845 ctl_done((union ctl_io *)ctsio);
6846 return (CTL_RETVAL_COMPLETE);
6852 total_len = header_len + page_len;
6854 printf("header_len = %d, page_len = %d, total_len = %d\n",
6855 header_len, page_len, total_len);
6858 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6859 ctsio->kern_sg_entries = 0;
6860 ctsio->kern_data_resid = 0;
6861 ctsio->kern_rel_offset = 0;
6862 if (total_len < alloc_len) {
6863 ctsio->residual = alloc_len - total_len;
6864 ctsio->kern_data_len = total_len;
6865 ctsio->kern_total_len = total_len;
6867 ctsio->residual = 0;
6868 ctsio->kern_data_len = alloc_len;
6869 ctsio->kern_total_len = alloc_len;
6872 switch (ctsio->cdb[0]) {
6873 case MODE_SENSE_6: {
6874 struct scsi_mode_hdr_6 *header;
6876 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6878 header->datalen = MIN(total_len - 1, 254);
6879 if (control_dev == 0) {
6880 header->dev_specific = 0x10; /* DPOFUA */
6881 if ((lun->flags & CTL_LUN_READONLY) ||
6882 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6883 .eca_and_aen & SCP_SWP) != 0)
6884 header->dev_specific |= 0x80; /* WP */
6887 header->block_descr_len = 0;
6889 header->block_descr_len =
6890 sizeof(struct scsi_mode_block_descr);
6891 block_desc = (struct scsi_mode_block_descr *)&header[1];
6894 case MODE_SENSE_10: {
6895 struct scsi_mode_hdr_10 *header;
6898 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6900 datalen = MIN(total_len - 2, 65533);
6901 scsi_ulto2b(datalen, header->datalen);
6902 if (control_dev == 0) {
6903 header->dev_specific = 0x10; /* DPOFUA */
6904 if ((lun->flags & CTL_LUN_READONLY) ||
6905 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6906 .eca_and_aen & SCP_SWP) != 0)
6907 header->dev_specific |= 0x80; /* WP */
6910 scsi_ulto2b(0, header->block_descr_len);
6912 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6913 header->block_descr_len);
6914 block_desc = (struct scsi_mode_block_descr *)&header[1];
6918 panic("invalid CDB type %#x", ctsio->cdb[0]);
6919 break; /* NOTREACHED */
6923 * If we've got a disk, use its blocksize in the block
6924 * descriptor. Otherwise, just set it to 0.
6927 if (control_dev == 0)
6928 scsi_ulto3b(lun->be_lun->blocksize,
6929 block_desc->block_len);
6931 scsi_ulto3b(0, block_desc->block_len);
6934 switch (page_code) {
6935 case SMS_ALL_PAGES_PAGE: {
6938 data_used = header_len;
6939 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6940 struct ctl_page_index *page_index;
6942 page_index = &lun->mode_pages.index[i];
6944 if ((control_dev != 0)
6945 && (page_index->page_flags &
6946 CTL_PAGE_FLAG_DISK_ONLY))
6950 * We don't use this subpage if the user didn't
6951 * request all subpages. We already checked (above)
6952 * to make sure the user only specified a subpage
6953 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6955 if ((page_index->subpage != 0)
6956 && (subpage == SMS_SUBPAGE_PAGE_0))
6960 * Call the handler, if it exists, to update the
6961 * page to the latest values.
6963 if (page_index->sense_handler != NULL)
6964 page_index->sense_handler(ctsio, page_index,pc);
6966 memcpy(ctsio->kern_data_ptr + data_used,
6967 page_index->page_data +
6968 (page_index->page_len * pc),
6969 page_index->page_len);
6970 data_used += page_index->page_len;
6977 data_used = header_len;
6979 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6980 struct ctl_page_index *page_index;
6982 page_index = &lun->mode_pages.index[i];
6984 /* Look for the right page code */
6985 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6988 /* Look for the right subpage or the subpage wildcard*/
6989 if ((page_index->subpage != subpage)
6990 && (subpage != SMS_SUBPAGE_ALL))
6993 /* Make sure the page is supported for this dev type */
6994 if ((control_dev != 0)
6995 && (page_index->page_flags &
6996 CTL_PAGE_FLAG_DISK_ONLY))
7000 * Call the handler, if it exists, to update the
7001 * page to the latest values.
7003 if (page_index->sense_handler != NULL)
7004 page_index->sense_handler(ctsio, page_index,pc);
7006 memcpy(ctsio->kern_data_ptr + data_used,
7007 page_index->page_data +
7008 (page_index->page_len * pc),
7009 page_index->page_len);
7010 data_used += page_index->page_len;
7016 ctl_set_success(ctsio);
7017 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7018 ctsio->be_move_done = ctl_config_move_done;
7019 ctl_datamove((union ctl_io *)ctsio);
7020 return (CTL_RETVAL_COMPLETE);
7024 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
7025 struct ctl_page_index *page_index,
7028 struct ctl_lun *lun;
7029 struct scsi_log_param_header *phdr;
7033 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7034 data = page_index->page_data;
7036 if (lun->backend->lun_attr != NULL &&
7037 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
7039 phdr = (struct scsi_log_param_header *)data;
7040 scsi_ulto2b(0x0001, phdr->param_code);
7041 phdr->param_control = SLP_LBIN | SLP_LP;
7042 phdr->param_len = 8;
7043 data = (uint8_t *)(phdr + 1);
7044 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
7045 data[4] = 0x02; /* per-pool */
7046 data += phdr->param_len;
7049 if (lun->backend->lun_attr != NULL &&
7050 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
7052 phdr = (struct scsi_log_param_header *)data;
7053 scsi_ulto2b(0x0002, phdr->param_code);
7054 phdr->param_control = SLP_LBIN | SLP_LP;
7055 phdr->param_len = 8;
7056 data = (uint8_t *)(phdr + 1);
7057 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
7058 data[4] = 0x01; /* per-LUN */
7059 data += phdr->param_len;
7062 if (lun->backend->lun_attr != NULL &&
7063 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
7065 phdr = (struct scsi_log_param_header *)data;
7066 scsi_ulto2b(0x00f1, phdr->param_code);
7067 phdr->param_control = SLP_LBIN | SLP_LP;
7068 phdr->param_len = 8;
7069 data = (uint8_t *)(phdr + 1);
7070 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
7071 data[4] = 0x02; /* per-pool */
7072 data += phdr->param_len;
7075 if (lun->backend->lun_attr != NULL &&
7076 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
7078 phdr = (struct scsi_log_param_header *)data;
7079 scsi_ulto2b(0x00f2, phdr->param_code);
7080 phdr->param_control = SLP_LBIN | SLP_LP;
7081 phdr->param_len = 8;
7082 data = (uint8_t *)(phdr + 1);
7083 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
7084 data[4] = 0x02; /* per-pool */
7085 data += phdr->param_len;
7088 page_index->page_len = data - page_index->page_data;
7093 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
7094 struct ctl_page_index *page_index,
7097 struct ctl_lun *lun;
7098 struct stat_page *data;
7099 uint64_t rn, wn, rb, wb;
7100 struct bintime rt, wt;
7103 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7104 data = (struct stat_page *)page_index->page_data;
7106 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
7107 data->sap.hdr.param_control = SLP_LBIN;
7108 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
7109 sizeof(struct scsi_log_param_header);
7110 rn = wn = rb = wb = 0;
7113 for (i = 0; i < CTL_MAX_PORTS; i++) {
7114 rn += lun->stats.ports[i].operations[CTL_STATS_READ];
7115 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE];
7116 rb += lun->stats.ports[i].bytes[CTL_STATS_READ];
7117 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE];
7118 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]);
7119 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]);
7121 scsi_u64to8b(rn, data->sap.read_num);
7122 scsi_u64to8b(wn, data->sap.write_num);
7123 if (lun->stats.blocksize > 0) {
7124 scsi_u64to8b(wb / lun->stats.blocksize,
7125 data->sap.recvieved_lba);
7126 scsi_u64to8b(rb / lun->stats.blocksize,
7127 data->sap.transmitted_lba);
7129 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000),
7130 data->sap.read_int);
7131 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000),
7132 data->sap.write_int);
7133 scsi_u64to8b(0, data->sap.weighted_num);
7134 scsi_u64to8b(0, data->sap.weighted_int);
7135 scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
7136 data->it.hdr.param_control = SLP_LBIN;
7137 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
7138 sizeof(struct scsi_log_param_header);
7140 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
7142 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
7143 data->it.hdr.param_control = SLP_LBIN;
7144 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
7145 sizeof(struct scsi_log_param_header);
7146 scsi_ulto4b(3, data->ti.exponent);
7147 scsi_ulto4b(1, data->ti.integer);
7149 page_index->page_len = sizeof(*data);
7154 ctl_log_sense(struct ctl_scsiio *ctsio)
7156 struct ctl_lun *lun;
7157 int i, pc, page_code, subpage;
7158 int alloc_len, total_len;
7159 struct ctl_page_index *page_index;
7160 struct scsi_log_sense *cdb;
7161 struct scsi_log_header *header;
7163 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
7165 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7166 cdb = (struct scsi_log_sense *)ctsio->cdb;
7167 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
7168 page_code = cdb->page & SLS_PAGE_CODE;
7169 subpage = cdb->subpage;
7170 alloc_len = scsi_2btoul(cdb->length);
7173 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
7174 page_index = &lun->log_pages.index[i];
7176 /* Look for the right page code */
7177 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
7180 /* Look for the right subpage or the subpage wildcard*/
7181 if (page_index->subpage != subpage)
7186 if (i >= CTL_NUM_LOG_PAGES) {
7187 ctl_set_invalid_field(ctsio,
7193 ctl_done((union ctl_io *)ctsio);
7194 return (CTL_RETVAL_COMPLETE);
7197 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
7199 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7200 ctsio->kern_sg_entries = 0;
7201 ctsio->kern_data_resid = 0;
7202 ctsio->kern_rel_offset = 0;
7203 if (total_len < alloc_len) {
7204 ctsio->residual = alloc_len - total_len;
7205 ctsio->kern_data_len = total_len;
7206 ctsio->kern_total_len = total_len;
7208 ctsio->residual = 0;
7209 ctsio->kern_data_len = alloc_len;
7210 ctsio->kern_total_len = alloc_len;
7213 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
7214 header->page = page_index->page_code;
7215 if (page_index->subpage) {
7216 header->page |= SL_SPF;
7217 header->subpage = page_index->subpage;
7219 scsi_ulto2b(page_index->page_len, header->datalen);
7222 * Call the handler, if it exists, to update the
7223 * page to the latest values.
7225 if (page_index->sense_handler != NULL)
7226 page_index->sense_handler(ctsio, page_index, pc);
7228 memcpy(header + 1, page_index->page_data, page_index->page_len);
7230 ctl_set_success(ctsio);
7231 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7232 ctsio->be_move_done = ctl_config_move_done;
7233 ctl_datamove((union ctl_io *)ctsio);
7234 return (CTL_RETVAL_COMPLETE);
7238 ctl_read_capacity(struct ctl_scsiio *ctsio)
7240 struct scsi_read_capacity *cdb;
7241 struct scsi_read_capacity_data *data;
7242 struct ctl_lun *lun;
7245 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
7247 cdb = (struct scsi_read_capacity *)ctsio->cdb;
7249 lba = scsi_4btoul(cdb->addr);
7250 if (((cdb->pmi & SRC_PMI) == 0)
7252 ctl_set_invalid_field(/*ctsio*/ ctsio,
7258 ctl_done((union ctl_io *)ctsio);
7259 return (CTL_RETVAL_COMPLETE);
7262 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7264 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7265 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
7266 ctsio->residual = 0;
7267 ctsio->kern_data_len = sizeof(*data);
7268 ctsio->kern_total_len = sizeof(*data);
7269 ctsio->kern_data_resid = 0;
7270 ctsio->kern_rel_offset = 0;
7271 ctsio->kern_sg_entries = 0;
7274 * If the maximum LBA is greater than 0xfffffffe, the user must
7275 * issue a SERVICE ACTION IN (16) command, with the read capacity
7276 * serivce action set.
7278 if (lun->be_lun->maxlba > 0xfffffffe)
7279 scsi_ulto4b(0xffffffff, data->addr);
7281 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
7284 * XXX KDM this may not be 512 bytes...
7286 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7288 ctl_set_success(ctsio);
7289 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7290 ctsio->be_move_done = ctl_config_move_done;
7291 ctl_datamove((union ctl_io *)ctsio);
7292 return (CTL_RETVAL_COMPLETE);
7296 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
7298 struct scsi_read_capacity_16 *cdb;
7299 struct scsi_read_capacity_data_long *data;
7300 struct ctl_lun *lun;
7304 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
7306 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
7308 alloc_len = scsi_4btoul(cdb->alloc_len);
7309 lba = scsi_8btou64(cdb->addr);
7311 if ((cdb->reladr & SRC16_PMI)
7313 ctl_set_invalid_field(/*ctsio*/ ctsio,
7319 ctl_done((union ctl_io *)ctsio);
7320 return (CTL_RETVAL_COMPLETE);
7323 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7325 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7326 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
7328 if (sizeof(*data) < alloc_len) {
7329 ctsio->residual = alloc_len - sizeof(*data);
7330 ctsio->kern_data_len = sizeof(*data);
7331 ctsio->kern_total_len = sizeof(*data);
7333 ctsio->residual = 0;
7334 ctsio->kern_data_len = alloc_len;
7335 ctsio->kern_total_len = alloc_len;
7337 ctsio->kern_data_resid = 0;
7338 ctsio->kern_rel_offset = 0;
7339 ctsio->kern_sg_entries = 0;
7341 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7342 /* XXX KDM this may not be 512 bytes... */
7343 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7344 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7345 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7346 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7347 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
7349 ctl_set_success(ctsio);
7350 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7351 ctsio->be_move_done = ctl_config_move_done;
7352 ctl_datamove((union ctl_io *)ctsio);
7353 return (CTL_RETVAL_COMPLETE);
7357 ctl_get_lba_status(struct ctl_scsiio *ctsio)
7359 struct scsi_get_lba_status *cdb;
7360 struct scsi_get_lba_status_data *data;
7361 struct ctl_lun *lun;
7362 struct ctl_lba_len_flags *lbalen;
7364 uint32_t alloc_len, total_len;
7367 CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
7369 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7370 cdb = (struct scsi_get_lba_status *)ctsio->cdb;
7371 lba = scsi_8btou64(cdb->addr);
7372 alloc_len = scsi_4btoul(cdb->alloc_len);
7374 if (lba > lun->be_lun->maxlba) {
7375 ctl_set_lba_out_of_range(ctsio);
7376 ctl_done((union ctl_io *)ctsio);
7377 return (CTL_RETVAL_COMPLETE);
7380 total_len = sizeof(*data) + sizeof(data->descr[0]);
7381 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7382 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
7384 if (total_len < alloc_len) {
7385 ctsio->residual = alloc_len - total_len;
7386 ctsio->kern_data_len = total_len;
7387 ctsio->kern_total_len = total_len;
7389 ctsio->residual = 0;
7390 ctsio->kern_data_len = alloc_len;
7391 ctsio->kern_total_len = alloc_len;
7393 ctsio->kern_data_resid = 0;
7394 ctsio->kern_rel_offset = 0;
7395 ctsio->kern_sg_entries = 0;
7397 /* Fill dummy data in case backend can't tell anything. */
7398 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
7399 scsi_u64to8b(lba, data->descr[0].addr);
7400 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
7401 data->descr[0].length);
7402 data->descr[0].status = 0; /* Mapped or unknown. */
7404 ctl_set_success(ctsio);
7405 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7406 ctsio->be_move_done = ctl_config_move_done;
7408 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
7410 lbalen->len = total_len;
7412 retval = lun->backend->config_read((union ctl_io *)ctsio);
7413 return (CTL_RETVAL_COMPLETE);
7417 ctl_read_defect(struct ctl_scsiio *ctsio)
7419 struct scsi_read_defect_data_10 *ccb10;
7420 struct scsi_read_defect_data_12 *ccb12;
7421 struct scsi_read_defect_data_hdr_10 *data10;
7422 struct scsi_read_defect_data_hdr_12 *data12;
7423 uint32_t alloc_len, data_len;
7426 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7428 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7429 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7430 format = ccb10->format;
7431 alloc_len = scsi_2btoul(ccb10->alloc_length);
7432 data_len = sizeof(*data10);
7434 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7435 format = ccb12->format;
7436 alloc_len = scsi_4btoul(ccb12->alloc_length);
7437 data_len = sizeof(*data12);
7439 if (alloc_len == 0) {
7440 ctl_set_success(ctsio);
7441 ctl_done((union ctl_io *)ctsio);
7442 return (CTL_RETVAL_COMPLETE);
7445 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7446 if (data_len < alloc_len) {
7447 ctsio->residual = alloc_len - data_len;
7448 ctsio->kern_data_len = data_len;
7449 ctsio->kern_total_len = data_len;
7451 ctsio->residual = 0;
7452 ctsio->kern_data_len = alloc_len;
7453 ctsio->kern_total_len = alloc_len;
7455 ctsio->kern_data_resid = 0;
7456 ctsio->kern_rel_offset = 0;
7457 ctsio->kern_sg_entries = 0;
7459 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7460 data10 = (struct scsi_read_defect_data_hdr_10 *)
7461 ctsio->kern_data_ptr;
7462 data10->format = format;
7463 scsi_ulto2b(0, data10->length);
7465 data12 = (struct scsi_read_defect_data_hdr_12 *)
7466 ctsio->kern_data_ptr;
7467 data12->format = format;
7468 scsi_ulto2b(0, data12->generation);
7469 scsi_ulto4b(0, data12->length);
7472 ctl_set_success(ctsio);
7473 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7474 ctsio->be_move_done = ctl_config_move_done;
7475 ctl_datamove((union ctl_io *)ctsio);
7476 return (CTL_RETVAL_COMPLETE);
7480 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7482 struct scsi_maintenance_in *cdb;
7484 int alloc_len, ext, total_len = 0, g, p, pc, pg, gs, os;
7485 int num_target_port_groups, num_target_ports;
7486 struct ctl_lun *lun;
7487 struct ctl_softc *softc;
7488 struct ctl_port *port;
7489 struct scsi_target_group_data *rtg_ptr;
7490 struct scsi_target_group_data_extended *rtg_ext_ptr;
7491 struct scsi_target_port_group_descriptor *tpg_desc;
7493 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7495 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7496 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7497 softc = lun->ctl_softc;
7499 retval = CTL_RETVAL_COMPLETE;
7501 switch (cdb->byte2 & STG_PDF_MASK) {
7502 case STG_PDF_LENGTH:
7505 case STG_PDF_EXTENDED:
7509 ctl_set_invalid_field(/*ctsio*/ ctsio,
7515 ctl_done((union ctl_io *)ctsio);
7519 if (softc->is_single)
7520 num_target_port_groups = 1;
7522 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
7523 num_target_ports = 0;
7524 mtx_lock(&softc->ctl_lock);
7525 STAILQ_FOREACH(port, &softc->port_list, links) {
7526 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7528 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7532 mtx_unlock(&softc->ctl_lock);
7535 total_len = sizeof(struct scsi_target_group_data_extended);
7537 total_len = sizeof(struct scsi_target_group_data);
7538 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7539 num_target_port_groups +
7540 sizeof(struct scsi_target_port_descriptor) *
7541 num_target_ports * num_target_port_groups;
7543 alloc_len = scsi_4btoul(cdb->length);
7545 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7547 ctsio->kern_sg_entries = 0;
7549 if (total_len < alloc_len) {
7550 ctsio->residual = alloc_len - total_len;
7551 ctsio->kern_data_len = total_len;
7552 ctsio->kern_total_len = total_len;
7554 ctsio->residual = 0;
7555 ctsio->kern_data_len = alloc_len;
7556 ctsio->kern_total_len = alloc_len;
7558 ctsio->kern_data_resid = 0;
7559 ctsio->kern_rel_offset = 0;
7562 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7563 ctsio->kern_data_ptr;
7564 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7565 rtg_ext_ptr->format_type = 0x10;
7566 rtg_ext_ptr->implicit_transition_time = 0;
7567 tpg_desc = &rtg_ext_ptr->groups[0];
7569 rtg_ptr = (struct scsi_target_group_data *)
7570 ctsio->kern_data_ptr;
7571 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7572 tpg_desc = &rtg_ptr->groups[0];
7575 mtx_lock(&softc->ctl_lock);
7576 pg = softc->port_offset / CTL_MAX_PORTS;
7577 if (softc->flags & CTL_FLAG_ACTIVE_SHELF) {
7578 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) {
7579 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7580 os = TPG_ASYMMETRIC_ACCESS_STANDBY;
7581 } else if (lun->flags & CTL_LUN_PRIMARY_SC) {
7582 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7583 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7585 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7586 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7589 gs = TPG_ASYMMETRIC_ACCESS_STANDBY;
7590 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7592 for (g = 0; g < num_target_port_groups; g++) {
7593 tpg_desc->pref_state = (g == pg) ? gs : os;
7594 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP;
7595 scsi_ulto2b(g + 1, tpg_desc->target_port_group);
7596 tpg_desc->status = TPG_IMPLICIT;
7598 STAILQ_FOREACH(port, &softc->port_list, links) {
7599 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7601 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7603 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS;
7604 scsi_ulto2b(p, tpg_desc->descriptors[pc].
7605 relative_target_port_identifier);
7608 tpg_desc->target_port_count = pc;
7609 tpg_desc = (struct scsi_target_port_group_descriptor *)
7610 &tpg_desc->descriptors[pc];
7612 mtx_unlock(&softc->ctl_lock);
7614 ctl_set_success(ctsio);
7615 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7616 ctsio->be_move_done = ctl_config_move_done;
7617 ctl_datamove((union ctl_io *)ctsio);
7622 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7624 struct ctl_lun *lun;
7625 struct scsi_report_supported_opcodes *cdb;
7626 const struct ctl_cmd_entry *entry, *sentry;
7627 struct scsi_report_supported_opcodes_all *all;
7628 struct scsi_report_supported_opcodes_descr *descr;
7629 struct scsi_report_supported_opcodes_one *one;
7631 int alloc_len, total_len;
7632 int opcode, service_action, i, j, num;
7634 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7636 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7637 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7639 retval = CTL_RETVAL_COMPLETE;
7641 opcode = cdb->requested_opcode;
7642 service_action = scsi_2btoul(cdb->requested_service_action);
7643 switch (cdb->options & RSO_OPTIONS_MASK) {
7644 case RSO_OPTIONS_ALL:
7646 for (i = 0; i < 256; i++) {
7647 entry = &ctl_cmd_table[i];
7648 if (entry->flags & CTL_CMD_FLAG_SA5) {
7649 for (j = 0; j < 32; j++) {
7650 sentry = &((const struct ctl_cmd_entry *)
7652 if (ctl_cmd_applicable(
7653 lun->be_lun->lun_type, sentry))
7657 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7662 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7663 num * sizeof(struct scsi_report_supported_opcodes_descr);
7665 case RSO_OPTIONS_OC:
7666 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7667 ctl_set_invalid_field(/*ctsio*/ ctsio,
7673 ctl_done((union ctl_io *)ctsio);
7674 return (CTL_RETVAL_COMPLETE);
7676 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7678 case RSO_OPTIONS_OC_SA:
7679 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7680 service_action >= 32) {
7681 ctl_set_invalid_field(/*ctsio*/ ctsio,
7687 ctl_done((union ctl_io *)ctsio);
7688 return (CTL_RETVAL_COMPLETE);
7690 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7693 ctl_set_invalid_field(/*ctsio*/ ctsio,
7699 ctl_done((union ctl_io *)ctsio);
7700 return (CTL_RETVAL_COMPLETE);
7703 alloc_len = scsi_4btoul(cdb->length);
7705 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7707 ctsio->kern_sg_entries = 0;
7709 if (total_len < alloc_len) {
7710 ctsio->residual = alloc_len - total_len;
7711 ctsio->kern_data_len = total_len;
7712 ctsio->kern_total_len = total_len;
7714 ctsio->residual = 0;
7715 ctsio->kern_data_len = alloc_len;
7716 ctsio->kern_total_len = alloc_len;
7718 ctsio->kern_data_resid = 0;
7719 ctsio->kern_rel_offset = 0;
7721 switch (cdb->options & RSO_OPTIONS_MASK) {
7722 case RSO_OPTIONS_ALL:
7723 all = (struct scsi_report_supported_opcodes_all *)
7724 ctsio->kern_data_ptr;
7726 for (i = 0; i < 256; i++) {
7727 entry = &ctl_cmd_table[i];
7728 if (entry->flags & CTL_CMD_FLAG_SA5) {
7729 for (j = 0; j < 32; j++) {
7730 sentry = &((const struct ctl_cmd_entry *)
7732 if (!ctl_cmd_applicable(
7733 lun->be_lun->lun_type, sentry))
7735 descr = &all->descr[num++];
7737 scsi_ulto2b(j, descr->service_action);
7738 descr->flags = RSO_SERVACTV;
7739 scsi_ulto2b(sentry->length,
7743 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7746 descr = &all->descr[num++];
7748 scsi_ulto2b(0, descr->service_action);
7750 scsi_ulto2b(entry->length, descr->cdb_length);
7754 num * sizeof(struct scsi_report_supported_opcodes_descr),
7757 case RSO_OPTIONS_OC:
7758 one = (struct scsi_report_supported_opcodes_one *)
7759 ctsio->kern_data_ptr;
7760 entry = &ctl_cmd_table[opcode];
7762 case RSO_OPTIONS_OC_SA:
7763 one = (struct scsi_report_supported_opcodes_one *)
7764 ctsio->kern_data_ptr;
7765 entry = &ctl_cmd_table[opcode];
7766 entry = &((const struct ctl_cmd_entry *)
7767 entry->execute)[service_action];
7769 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7771 scsi_ulto2b(entry->length, one->cdb_length);
7772 one->cdb_usage[0] = opcode;
7773 memcpy(&one->cdb_usage[1], entry->usage,
7780 ctl_set_success(ctsio);
7781 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7782 ctsio->be_move_done = ctl_config_move_done;
7783 ctl_datamove((union ctl_io *)ctsio);
7788 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7790 struct scsi_report_supported_tmf *cdb;
7791 struct scsi_report_supported_tmf_data *data;
7793 int alloc_len, total_len;
7795 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7797 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7799 retval = CTL_RETVAL_COMPLETE;
7801 total_len = sizeof(struct scsi_report_supported_tmf_data);
7802 alloc_len = scsi_4btoul(cdb->length);
7804 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7806 ctsio->kern_sg_entries = 0;
7808 if (total_len < alloc_len) {
7809 ctsio->residual = alloc_len - total_len;
7810 ctsio->kern_data_len = total_len;
7811 ctsio->kern_total_len = total_len;
7813 ctsio->residual = 0;
7814 ctsio->kern_data_len = alloc_len;
7815 ctsio->kern_total_len = alloc_len;
7817 ctsio->kern_data_resid = 0;
7818 ctsio->kern_rel_offset = 0;
7820 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr;
7821 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS;
7822 data->byte2 |= RST_ITNRS;
7824 ctl_set_success(ctsio);
7825 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7826 ctsio->be_move_done = ctl_config_move_done;
7827 ctl_datamove((union ctl_io *)ctsio);
7832 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7834 struct scsi_report_timestamp *cdb;
7835 struct scsi_report_timestamp_data *data;
7839 int alloc_len, total_len;
7841 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7843 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7845 retval = CTL_RETVAL_COMPLETE;
7847 total_len = sizeof(struct scsi_report_timestamp_data);
7848 alloc_len = scsi_4btoul(cdb->length);
7850 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7852 ctsio->kern_sg_entries = 0;
7854 if (total_len < alloc_len) {
7855 ctsio->residual = alloc_len - total_len;
7856 ctsio->kern_data_len = total_len;
7857 ctsio->kern_total_len = total_len;
7859 ctsio->residual = 0;
7860 ctsio->kern_data_len = alloc_len;
7861 ctsio->kern_total_len = alloc_len;
7863 ctsio->kern_data_resid = 0;
7864 ctsio->kern_rel_offset = 0;
7866 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7867 scsi_ulto2b(sizeof(*data) - 2, data->length);
7868 data->origin = RTS_ORIG_OUTSIDE;
7870 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7871 scsi_ulto4b(timestamp >> 16, data->timestamp);
7872 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7874 ctl_set_success(ctsio);
7875 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7876 ctsio->be_move_done = ctl_config_move_done;
7877 ctl_datamove((union ctl_io *)ctsio);
7882 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7884 struct scsi_per_res_in *cdb;
7885 int alloc_len, total_len = 0;
7886 /* struct scsi_per_res_in_rsrv in_data; */
7887 struct ctl_lun *lun;
7888 struct ctl_softc *softc;
7891 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7893 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7895 alloc_len = scsi_2btoul(cdb->length);
7897 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7898 softc = lun->ctl_softc;
7901 mtx_lock(&lun->lun_lock);
7902 switch (cdb->action) {
7903 case SPRI_RK: /* read keys */
7904 total_len = sizeof(struct scsi_per_res_in_keys) +
7906 sizeof(struct scsi_per_res_key);
7908 case SPRI_RR: /* read reservation */
7909 if (lun->flags & CTL_LUN_PR_RESERVED)
7910 total_len = sizeof(struct scsi_per_res_in_rsrv);
7912 total_len = sizeof(struct scsi_per_res_in_header);
7914 case SPRI_RC: /* report capabilities */
7915 total_len = sizeof(struct scsi_per_res_cap);
7917 case SPRI_RS: /* read full status */
7918 total_len = sizeof(struct scsi_per_res_in_header) +
7919 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7923 panic("Invalid PR type %x", cdb->action);
7925 mtx_unlock(&lun->lun_lock);
7927 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7929 if (total_len < alloc_len) {
7930 ctsio->residual = alloc_len - total_len;
7931 ctsio->kern_data_len = total_len;
7932 ctsio->kern_total_len = total_len;
7934 ctsio->residual = 0;
7935 ctsio->kern_data_len = alloc_len;
7936 ctsio->kern_total_len = alloc_len;
7939 ctsio->kern_data_resid = 0;
7940 ctsio->kern_rel_offset = 0;
7941 ctsio->kern_sg_entries = 0;
7943 mtx_lock(&lun->lun_lock);
7944 switch (cdb->action) {
7945 case SPRI_RK: { // read keys
7946 struct scsi_per_res_in_keys *res_keys;
7949 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7952 * We had to drop the lock to allocate our buffer, which
7953 * leaves time for someone to come in with another
7954 * persistent reservation. (That is unlikely, though,
7955 * since this should be the only persistent reservation
7956 * command active right now.)
7958 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7959 (lun->pr_key_count *
7960 sizeof(struct scsi_per_res_key)))){
7961 mtx_unlock(&lun->lun_lock);
7962 free(ctsio->kern_data_ptr, M_CTL);
7963 printf("%s: reservation length changed, retrying\n",
7968 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7970 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7971 lun->pr_key_count, res_keys->header.length);
7973 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7974 if ((key = ctl_get_prkey(lun, i)) == 0)
7978 * We used lun->pr_key_count to calculate the
7979 * size to allocate. If it turns out the number of
7980 * initiators with the registered flag set is
7981 * larger than that (i.e. they haven't been kept in
7982 * sync), we've got a problem.
7984 if (key_count >= lun->pr_key_count) {
7986 csevent_log(CSC_CTL | CSC_SHELF_SW |
7988 csevent_LogType_Fault,
7989 csevent_AlertLevel_Yellow,
7990 csevent_FRU_ShelfController,
7991 csevent_FRU_Firmware,
7992 csevent_FRU_Unknown,
7993 "registered keys %d >= key "
7994 "count %d", key_count,
8000 scsi_u64to8b(key, res_keys->keys[key_count].key);
8005 case SPRI_RR: { // read reservation
8006 struct scsi_per_res_in_rsrv *res;
8007 int tmp_len, header_only;
8009 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
8011 scsi_ulto4b(lun->PRGeneration, res->header.generation);
8013 if (lun->flags & CTL_LUN_PR_RESERVED)
8015 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
8016 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
8017 res->header.length);
8020 tmp_len = sizeof(struct scsi_per_res_in_header);
8021 scsi_ulto4b(0, res->header.length);
8026 * We had to drop the lock to allocate our buffer, which
8027 * leaves time for someone to come in with another
8028 * persistent reservation. (That is unlikely, though,
8029 * since this should be the only persistent reservation
8030 * command active right now.)
8032 if (tmp_len != total_len) {
8033 mtx_unlock(&lun->lun_lock);
8034 free(ctsio->kern_data_ptr, M_CTL);
8035 printf("%s: reservation status changed, retrying\n",
8041 * No reservation held, so we're done.
8043 if (header_only != 0)
8047 * If the registration is an All Registrants type, the key
8048 * is 0, since it doesn't really matter.
8050 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8051 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
8052 res->data.reservation);
8054 res->data.scopetype = lun->res_type;
8057 case SPRI_RC: //report capabilities
8059 struct scsi_per_res_cap *res_cap;
8062 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
8063 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
8064 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5;
8065 type_mask = SPRI_TM_WR_EX_AR |
8071 scsi_ulto2b(type_mask, res_cap->type_mask);
8074 case SPRI_RS: { // read full status
8075 struct scsi_per_res_in_full *res_status;
8076 struct scsi_per_res_in_full_desc *res_desc;
8077 struct ctl_port *port;
8080 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
8083 * We had to drop the lock to allocate our buffer, which
8084 * leaves time for someone to come in with another
8085 * persistent reservation. (That is unlikely, though,
8086 * since this should be the only persistent reservation
8087 * command active right now.)
8089 if (total_len < (sizeof(struct scsi_per_res_in_header) +
8090 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
8091 lun->pr_key_count)){
8092 mtx_unlock(&lun->lun_lock);
8093 free(ctsio->kern_data_ptr, M_CTL);
8094 printf("%s: reservation length changed, retrying\n",
8099 scsi_ulto4b(lun->PRGeneration, res_status->header.generation);
8101 res_desc = &res_status->desc[0];
8102 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) {
8103 if ((key = ctl_get_prkey(lun, i)) == 0)
8106 scsi_u64to8b(key, res_desc->res_key.key);
8107 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
8108 (lun->pr_res_idx == i ||
8109 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
8110 res_desc->flags = SPRI_FULL_R_HOLDER;
8111 res_desc->scopetype = lun->res_type;
8113 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
8114 res_desc->rel_trgt_port_id);
8116 port = softc->ctl_ports[
8117 ctl_port_idx(i / CTL_MAX_INIT_PER_PORT)];
8119 len = ctl_create_iid(port,
8120 i % CTL_MAX_INIT_PER_PORT,
8121 res_desc->transport_id);
8122 scsi_ulto4b(len, res_desc->additional_length);
8123 res_desc = (struct scsi_per_res_in_full_desc *)
8124 &res_desc->transport_id[len];
8126 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
8127 res_status->header.length);
8132 * This is a bug, because we just checked for this above,
8133 * and should have returned an error.
8135 panic("Invalid PR type %x", cdb->action);
8136 break; /* NOTREACHED */
8138 mtx_unlock(&lun->lun_lock);
8140 ctl_set_success(ctsio);
8141 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8142 ctsio->be_move_done = ctl_config_move_done;
8143 ctl_datamove((union ctl_io *)ctsio);
8144 return (CTL_RETVAL_COMPLETE);
8148 ctl_est_res_ua(struct ctl_lun *lun, uint32_t residx, ctl_ua_type ua)
8150 int off = lun->ctl_softc->persis_offset;
8152 if (residx >= off && residx < off + CTL_MAX_INITIATORS)
8153 ctl_est_ua(lun, residx - off, ua);
8157 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
8161 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
8162 uint64_t sa_res_key, uint8_t type, uint32_t residx,
8163 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
8164 struct scsi_per_res_out_parms* param)
8166 union ctl_ha_msg persis_io;
8172 mtx_lock(&lun->lun_lock);
8173 if (sa_res_key == 0) {
8174 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8175 /* validate scope and type */
8176 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
8178 mtx_unlock(&lun->lun_lock);
8179 ctl_set_invalid_field(/*ctsio*/ ctsio,
8185 ctl_done((union ctl_io *)ctsio);
8189 if (type>8 || type==2 || type==4 || type==0) {
8190 mtx_unlock(&lun->lun_lock);
8191 ctl_set_invalid_field(/*ctsio*/ ctsio,
8197 ctl_done((union ctl_io *)ctsio);
8202 * Unregister everybody else and build UA for
8205 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8206 if (i == residx || ctl_get_prkey(lun, i) == 0)
8209 ctl_clr_prkey(lun, i);
8210 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8212 lun->pr_key_count = 1;
8213 lun->res_type = type;
8214 if (lun->res_type != SPR_TYPE_WR_EX_AR
8215 && lun->res_type != SPR_TYPE_EX_AC_AR)
8216 lun->pr_res_idx = residx;
8218 /* send msg to other side */
8219 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8220 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8221 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8222 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8223 persis_io.pr.pr_info.res_type = type;
8224 memcpy(persis_io.pr.pr_info.sa_res_key,
8225 param->serv_act_res_key,
8226 sizeof(param->serv_act_res_key));
8227 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8228 &persis_io, sizeof(persis_io), 0)) >
8229 CTL_HA_STATUS_SUCCESS) {
8230 printf("CTL:Persis Out error returned "
8231 "from ctl_ha_msg_send %d\n",
8235 /* not all registrants */
8236 mtx_unlock(&lun->lun_lock);
8237 free(ctsio->kern_data_ptr, M_CTL);
8238 ctl_set_invalid_field(ctsio,
8244 ctl_done((union ctl_io *)ctsio);
8247 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8248 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
8251 if (res_key == sa_res_key) {
8254 * The spec implies this is not good but doesn't
8255 * say what to do. There are two choices either
8256 * generate a res conflict or check condition
8257 * with illegal field in parameter data. Since
8258 * that is what is done when the sa_res_key is
8259 * zero I'll take that approach since this has
8260 * to do with the sa_res_key.
8262 mtx_unlock(&lun->lun_lock);
8263 free(ctsio->kern_data_ptr, M_CTL);
8264 ctl_set_invalid_field(ctsio,
8270 ctl_done((union ctl_io *)ctsio);
8274 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8275 if (ctl_get_prkey(lun, i) != sa_res_key)
8279 ctl_clr_prkey(lun, i);
8280 lun->pr_key_count--;
8281 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8284 mtx_unlock(&lun->lun_lock);
8285 free(ctsio->kern_data_ptr, M_CTL);
8286 ctl_set_reservation_conflict(ctsio);
8287 ctl_done((union ctl_io *)ctsio);
8288 return (CTL_RETVAL_COMPLETE);
8290 /* send msg to other side */
8291 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8292 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8293 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8294 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8295 persis_io.pr.pr_info.res_type = type;
8296 memcpy(persis_io.pr.pr_info.sa_res_key,
8297 param->serv_act_res_key,
8298 sizeof(param->serv_act_res_key));
8299 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8300 &persis_io, sizeof(persis_io), 0)) >
8301 CTL_HA_STATUS_SUCCESS) {
8302 printf("CTL:Persis Out error returned from "
8303 "ctl_ha_msg_send %d\n", isc_retval);
8306 /* Reserved but not all registrants */
8307 /* sa_res_key is res holder */
8308 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
8309 /* validate scope and type */
8310 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
8312 mtx_unlock(&lun->lun_lock);
8313 ctl_set_invalid_field(/*ctsio*/ ctsio,
8319 ctl_done((union ctl_io *)ctsio);
8323 if (type>8 || type==2 || type==4 || type==0) {
8324 mtx_unlock(&lun->lun_lock);
8325 ctl_set_invalid_field(/*ctsio*/ ctsio,
8331 ctl_done((union ctl_io *)ctsio);
8337 * if sa_res_key != res_key remove all
8338 * registrants w/sa_res_key and generate UA
8339 * for these registrants(Registrations
8340 * Preempted) if it wasn't an exclusive
8341 * reservation generate UA(Reservations
8342 * Preempted) for all other registered nexuses
8343 * if the type has changed. Establish the new
8344 * reservation and holder. If res_key and
8345 * sa_res_key are the same do the above
8346 * except don't unregister the res holder.
8349 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8350 if (i == residx || ctl_get_prkey(lun, i) == 0)
8353 if (sa_res_key == ctl_get_prkey(lun, i)) {
8354 ctl_clr_prkey(lun, i);
8355 lun->pr_key_count--;
8356 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8357 } else if (type != lun->res_type
8358 && (lun->res_type == SPR_TYPE_WR_EX_RO
8359 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
8360 ctl_est_res_ua(lun, i, CTL_UA_RES_RELEASE);
8363 lun->res_type = type;
8364 if (lun->res_type != SPR_TYPE_WR_EX_AR
8365 && lun->res_type != SPR_TYPE_EX_AC_AR)
8366 lun->pr_res_idx = residx;
8368 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8370 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8371 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8372 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8373 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8374 persis_io.pr.pr_info.res_type = type;
8375 memcpy(persis_io.pr.pr_info.sa_res_key,
8376 param->serv_act_res_key,
8377 sizeof(param->serv_act_res_key));
8378 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8379 &persis_io, sizeof(persis_io), 0)) >
8380 CTL_HA_STATUS_SUCCESS) {
8381 printf("CTL:Persis Out error returned "
8382 "from ctl_ha_msg_send %d\n",
8387 * sa_res_key is not the res holder just
8388 * remove registrants
8392 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8393 if (sa_res_key != ctl_get_prkey(lun, i))
8397 ctl_clr_prkey(lun, i);
8398 lun->pr_key_count--;
8399 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8403 mtx_unlock(&lun->lun_lock);
8404 free(ctsio->kern_data_ptr, M_CTL);
8405 ctl_set_reservation_conflict(ctsio);
8406 ctl_done((union ctl_io *)ctsio);
8409 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8410 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8411 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8412 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8413 persis_io.pr.pr_info.res_type = type;
8414 memcpy(persis_io.pr.pr_info.sa_res_key,
8415 param->serv_act_res_key,
8416 sizeof(param->serv_act_res_key));
8417 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8418 &persis_io, sizeof(persis_io), 0)) >
8419 CTL_HA_STATUS_SUCCESS) {
8420 printf("CTL:Persis Out error returned "
8421 "from ctl_ha_msg_send %d\n",
8427 lun->PRGeneration++;
8428 mtx_unlock(&lun->lun_lock);
8434 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8436 uint64_t sa_res_key;
8439 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8441 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8442 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8443 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
8444 if (sa_res_key == 0) {
8446 * Unregister everybody else and build UA for
8449 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8450 if (i == msg->pr.pr_info.residx ||
8451 ctl_get_prkey(lun, i) == 0)
8454 ctl_clr_prkey(lun, i);
8455 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8458 lun->pr_key_count = 1;
8459 lun->res_type = msg->pr.pr_info.res_type;
8460 if (lun->res_type != SPR_TYPE_WR_EX_AR
8461 && lun->res_type != SPR_TYPE_EX_AC_AR)
8462 lun->pr_res_idx = msg->pr.pr_info.residx;
8464 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8465 if (sa_res_key == ctl_get_prkey(lun, i))
8468 ctl_clr_prkey(lun, i);
8469 lun->pr_key_count--;
8470 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8474 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8475 if (i == msg->pr.pr_info.residx ||
8476 ctl_get_prkey(lun, i) == 0)
8479 if (sa_res_key == ctl_get_prkey(lun, i)) {
8480 ctl_clr_prkey(lun, i);
8481 lun->pr_key_count--;
8482 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8483 } else if (msg->pr.pr_info.res_type != lun->res_type
8484 && (lun->res_type == SPR_TYPE_WR_EX_RO
8485 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
8486 ctl_est_res_ua(lun, i, CTL_UA_RES_RELEASE);
8489 lun->res_type = msg->pr.pr_info.res_type;
8490 if (lun->res_type != SPR_TYPE_WR_EX_AR
8491 && lun->res_type != SPR_TYPE_EX_AC_AR)
8492 lun->pr_res_idx = msg->pr.pr_info.residx;
8494 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8496 lun->PRGeneration++;
8502 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8506 u_int32_t param_len;
8507 struct scsi_per_res_out *cdb;
8508 struct ctl_lun *lun;
8509 struct scsi_per_res_out_parms* param;
8510 struct ctl_softc *softc;
8512 uint64_t res_key, sa_res_key, key;
8514 union ctl_ha_msg persis_io;
8517 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8519 retval = CTL_RETVAL_COMPLETE;
8521 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8522 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8523 softc = lun->ctl_softc;
8526 * We only support whole-LUN scope. The scope & type are ignored for
8527 * register, register and ignore existing key and clear.
8528 * We sometimes ignore scope and type on preempts too!!
8529 * Verify reservation type here as well.
8531 type = cdb->scope_type & SPR_TYPE_MASK;
8532 if ((cdb->action == SPRO_RESERVE)
8533 || (cdb->action == SPRO_RELEASE)) {
8534 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8535 ctl_set_invalid_field(/*ctsio*/ ctsio,
8541 ctl_done((union ctl_io *)ctsio);
8542 return (CTL_RETVAL_COMPLETE);
8545 if (type>8 || type==2 || type==4 || type==0) {
8546 ctl_set_invalid_field(/*ctsio*/ ctsio,
8552 ctl_done((union ctl_io *)ctsio);
8553 return (CTL_RETVAL_COMPLETE);
8557 param_len = scsi_4btoul(cdb->length);
8559 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8560 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8561 ctsio->kern_data_len = param_len;
8562 ctsio->kern_total_len = param_len;
8563 ctsio->kern_data_resid = 0;
8564 ctsio->kern_rel_offset = 0;
8565 ctsio->kern_sg_entries = 0;
8566 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8567 ctsio->be_move_done = ctl_config_move_done;
8568 ctl_datamove((union ctl_io *)ctsio);
8570 return (CTL_RETVAL_COMPLETE);
8573 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8575 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8576 res_key = scsi_8btou64(param->res_key.key);
8577 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8580 * Validate the reservation key here except for SPRO_REG_IGNO
8581 * This must be done for all other service actions
8583 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8584 mtx_lock(&lun->lun_lock);
8585 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8586 if (res_key != key) {
8588 * The current key passed in doesn't match
8589 * the one the initiator previously
8592 mtx_unlock(&lun->lun_lock);
8593 free(ctsio->kern_data_ptr, M_CTL);
8594 ctl_set_reservation_conflict(ctsio);
8595 ctl_done((union ctl_io *)ctsio);
8596 return (CTL_RETVAL_COMPLETE);
8598 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8600 * We are not registered
8602 mtx_unlock(&lun->lun_lock);
8603 free(ctsio->kern_data_ptr, M_CTL);
8604 ctl_set_reservation_conflict(ctsio);
8605 ctl_done((union ctl_io *)ctsio);
8606 return (CTL_RETVAL_COMPLETE);
8607 } else if (res_key != 0) {
8609 * We are not registered and trying to register but
8610 * the register key isn't zero.
8612 mtx_unlock(&lun->lun_lock);
8613 free(ctsio->kern_data_ptr, M_CTL);
8614 ctl_set_reservation_conflict(ctsio);
8615 ctl_done((union ctl_io *)ctsio);
8616 return (CTL_RETVAL_COMPLETE);
8618 mtx_unlock(&lun->lun_lock);
8621 switch (cdb->action & SPRO_ACTION_MASK) {
8623 case SPRO_REG_IGNO: {
8626 printf("Registration received\n");
8630 * We don't support any of these options, as we report in
8631 * the read capabilities request (see
8632 * ctl_persistent_reserve_in(), above).
8634 if ((param->flags & SPR_SPEC_I_PT)
8635 || (param->flags & SPR_ALL_TG_PT)
8636 || (param->flags & SPR_APTPL)) {
8639 if (param->flags & SPR_APTPL)
8641 else if (param->flags & SPR_ALL_TG_PT)
8643 else /* SPR_SPEC_I_PT */
8646 free(ctsio->kern_data_ptr, M_CTL);
8647 ctl_set_invalid_field(ctsio,
8653 ctl_done((union ctl_io *)ctsio);
8654 return (CTL_RETVAL_COMPLETE);
8657 mtx_lock(&lun->lun_lock);
8660 * The initiator wants to clear the
8663 if (sa_res_key == 0) {
8665 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8666 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8667 && ctl_get_prkey(lun, residx) == 0)) {
8668 mtx_unlock(&lun->lun_lock);
8672 ctl_clr_prkey(lun, residx);
8673 lun->pr_key_count--;
8675 if (residx == lun->pr_res_idx) {
8676 lun->flags &= ~CTL_LUN_PR_RESERVED;
8677 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8679 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8680 || lun->res_type == SPR_TYPE_EX_AC_RO)
8681 && lun->pr_key_count) {
8683 * If the reservation is a registrants
8684 * only type we need to generate a UA
8685 * for other registered inits. The
8686 * sense code should be RESERVATIONS
8690 for (i = 0; i < CTL_MAX_INITIATORS;i++){
8691 if (ctl_get_prkey(lun, i +
8692 softc->persis_offset) == 0)
8695 CTL_UA_RES_RELEASE);
8699 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8700 if (lun->pr_key_count==0) {
8701 lun->flags &= ~CTL_LUN_PR_RESERVED;
8703 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8706 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8707 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8708 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8709 persis_io.pr.pr_info.residx = residx;
8710 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8711 &persis_io, sizeof(persis_io), 0 )) >
8712 CTL_HA_STATUS_SUCCESS) {
8713 printf("CTL:Persis Out error returned from "
8714 "ctl_ha_msg_send %d\n", isc_retval);
8716 } else /* sa_res_key != 0 */ {
8719 * If we aren't registered currently then increment
8720 * the key count and set the registered flag.
8722 ctl_alloc_prkey(lun, residx);
8723 if (ctl_get_prkey(lun, residx) == 0)
8724 lun->pr_key_count++;
8725 ctl_set_prkey(lun, residx, sa_res_key);
8727 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8728 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8729 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8730 persis_io.pr.pr_info.residx = residx;
8731 memcpy(persis_io.pr.pr_info.sa_res_key,
8732 param->serv_act_res_key,
8733 sizeof(param->serv_act_res_key));
8734 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8735 &persis_io, sizeof(persis_io), 0)) >
8736 CTL_HA_STATUS_SUCCESS) {
8737 printf("CTL:Persis Out error returned from "
8738 "ctl_ha_msg_send %d\n", isc_retval);
8741 lun->PRGeneration++;
8742 mtx_unlock(&lun->lun_lock);
8748 printf("Reserve executed type %d\n", type);
8750 mtx_lock(&lun->lun_lock);
8751 if (lun->flags & CTL_LUN_PR_RESERVED) {
8753 * if this isn't the reservation holder and it's
8754 * not a "all registrants" type or if the type is
8755 * different then we have a conflict
8757 if ((lun->pr_res_idx != residx
8758 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8759 || lun->res_type != type) {
8760 mtx_unlock(&lun->lun_lock);
8761 free(ctsio->kern_data_ptr, M_CTL);
8762 ctl_set_reservation_conflict(ctsio);
8763 ctl_done((union ctl_io *)ctsio);
8764 return (CTL_RETVAL_COMPLETE);
8766 mtx_unlock(&lun->lun_lock);
8767 } else /* create a reservation */ {
8769 * If it's not an "all registrants" type record
8770 * reservation holder
8772 if (type != SPR_TYPE_WR_EX_AR
8773 && type != SPR_TYPE_EX_AC_AR)
8774 lun->pr_res_idx = residx; /* Res holder */
8776 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8778 lun->flags |= CTL_LUN_PR_RESERVED;
8779 lun->res_type = type;
8781 mtx_unlock(&lun->lun_lock);
8783 /* send msg to other side */
8784 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8785 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8786 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8787 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8788 persis_io.pr.pr_info.res_type = type;
8789 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8790 &persis_io, sizeof(persis_io), 0)) >
8791 CTL_HA_STATUS_SUCCESS) {
8792 printf("CTL:Persis Out error returned from "
8793 "ctl_ha_msg_send %d\n", isc_retval);
8799 mtx_lock(&lun->lun_lock);
8800 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8801 /* No reservation exists return good status */
8802 mtx_unlock(&lun->lun_lock);
8806 * Is this nexus a reservation holder?
8808 if (lun->pr_res_idx != residx
8809 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8811 * not a res holder return good status but
8814 mtx_unlock(&lun->lun_lock);
8818 if (lun->res_type != type) {
8819 mtx_unlock(&lun->lun_lock);
8820 free(ctsio->kern_data_ptr, M_CTL);
8821 ctl_set_illegal_pr_release(ctsio);
8822 ctl_done((union ctl_io *)ctsio);
8823 return (CTL_RETVAL_COMPLETE);
8826 /* okay to release */
8827 lun->flags &= ~CTL_LUN_PR_RESERVED;
8828 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8832 * if this isn't an exclusive access
8833 * res generate UA for all other
8836 if (type != SPR_TYPE_EX_AC
8837 && type != SPR_TYPE_WR_EX) {
8838 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8841 i + softc->persis_offset) == 0)
8843 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8846 mtx_unlock(&lun->lun_lock);
8847 /* Send msg to other side */
8848 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8849 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8850 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8851 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io,
8852 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8853 printf("CTL:Persis Out error returned from "
8854 "ctl_ha_msg_send %d\n", isc_retval);
8859 /* send msg to other side */
8861 mtx_lock(&lun->lun_lock);
8862 lun->flags &= ~CTL_LUN_PR_RESERVED;
8864 lun->pr_key_count = 0;
8865 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8867 ctl_clr_prkey(lun, residx);
8868 for (i=0; i < 2*CTL_MAX_INITIATORS; i++)
8869 if (ctl_get_prkey(lun, i) != 0) {
8870 ctl_clr_prkey(lun, i);
8871 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8873 lun->PRGeneration++;
8874 mtx_unlock(&lun->lun_lock);
8875 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8876 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8877 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8878 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8879 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8880 printf("CTL:Persis Out error returned from "
8881 "ctl_ha_msg_send %d\n", isc_retval);
8886 case SPRO_PRE_ABO: {
8889 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8890 residx, ctsio, cdb, param);
8892 return (CTL_RETVAL_COMPLETE);
8896 panic("Invalid PR type %x", cdb->action);
8900 free(ctsio->kern_data_ptr, M_CTL);
8901 ctl_set_success(ctsio);
8902 ctl_done((union ctl_io *)ctsio);
8908 * This routine is for handling a message from the other SC pertaining to
8909 * persistent reserve out. All the error checking will have been done
8910 * so only perorming the action need be done here to keep the two
8914 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8916 struct ctl_lun *lun;
8917 struct ctl_softc *softc;
8921 softc = control_softc;
8923 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8924 lun = softc->ctl_luns[targ_lun];
8925 mtx_lock(&lun->lun_lock);
8926 switch(msg->pr.pr_info.action) {
8927 case CTL_PR_REG_KEY:
8928 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8929 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8930 lun->pr_key_count++;
8931 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8932 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8933 lun->PRGeneration++;
8936 case CTL_PR_UNREG_KEY:
8937 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8938 lun->pr_key_count--;
8940 /* XXX Need to see if the reservation has been released */
8941 /* if so do we need to generate UA? */
8942 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8943 lun->flags &= ~CTL_LUN_PR_RESERVED;
8944 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8946 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8947 || lun->res_type == SPR_TYPE_EX_AC_RO)
8948 && lun->pr_key_count) {
8950 * If the reservation is a registrants
8951 * only type we need to generate a UA
8952 * for other registered inits. The
8953 * sense code should be RESERVATIONS
8957 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8958 if (ctl_get_prkey(lun, i +
8959 softc->persis_offset) == 0)
8962 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8966 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8967 if (lun->pr_key_count==0) {
8968 lun->flags &= ~CTL_LUN_PR_RESERVED;
8970 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8973 lun->PRGeneration++;
8976 case CTL_PR_RESERVE:
8977 lun->flags |= CTL_LUN_PR_RESERVED;
8978 lun->res_type = msg->pr.pr_info.res_type;
8979 lun->pr_res_idx = msg->pr.pr_info.residx;
8983 case CTL_PR_RELEASE:
8985 * if this isn't an exclusive access res generate UA for all
8986 * other registrants.
8988 if (lun->res_type != SPR_TYPE_EX_AC
8989 && lun->res_type != SPR_TYPE_WR_EX) {
8990 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8991 if (ctl_get_prkey(lun, i + softc->persis_offset) != 0)
8992 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8995 lun->flags &= ~CTL_LUN_PR_RESERVED;
8996 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
9000 case CTL_PR_PREEMPT:
9001 ctl_pro_preempt_other(lun, msg);
9004 lun->flags &= ~CTL_LUN_PR_RESERVED;
9006 lun->pr_key_count = 0;
9007 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
9009 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
9010 if (ctl_get_prkey(lun, i) == 0)
9012 ctl_clr_prkey(lun, i);
9013 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT);
9015 lun->PRGeneration++;
9019 mtx_unlock(&lun->lun_lock);
9023 ctl_read_write(struct ctl_scsiio *ctsio)
9025 struct ctl_lun *lun;
9026 struct ctl_lba_len_flags *lbalen;
9028 uint32_t num_blocks;
9032 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9034 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
9037 retval = CTL_RETVAL_COMPLETE;
9039 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
9040 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
9041 switch (ctsio->cdb[0]) {
9044 struct scsi_rw_6 *cdb;
9046 cdb = (struct scsi_rw_6 *)ctsio->cdb;
9048 lba = scsi_3btoul(cdb->addr);
9049 /* only 5 bits are valid in the most significant address byte */
9051 num_blocks = cdb->length;
9053 * This is correct according to SBC-2.
9055 if (num_blocks == 0)
9061 struct scsi_rw_10 *cdb;
9063 cdb = (struct scsi_rw_10 *)ctsio->cdb;
9064 if (cdb->byte2 & SRW10_FUA)
9065 flags |= CTL_LLF_FUA;
9066 if (cdb->byte2 & SRW10_DPO)
9067 flags |= CTL_LLF_DPO;
9068 lba = scsi_4btoul(cdb->addr);
9069 num_blocks = scsi_2btoul(cdb->length);
9072 case WRITE_VERIFY_10: {
9073 struct scsi_write_verify_10 *cdb;
9075 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
9076 flags |= CTL_LLF_FUA;
9077 if (cdb->byte2 & SWV_DPO)
9078 flags |= CTL_LLF_DPO;
9079 lba = scsi_4btoul(cdb->addr);
9080 num_blocks = scsi_2btoul(cdb->length);
9085 struct scsi_rw_12 *cdb;
9087 cdb = (struct scsi_rw_12 *)ctsio->cdb;
9088 if (cdb->byte2 & SRW12_FUA)
9089 flags |= CTL_LLF_FUA;
9090 if (cdb->byte2 & SRW12_DPO)
9091 flags |= CTL_LLF_DPO;
9092 lba = scsi_4btoul(cdb->addr);
9093 num_blocks = scsi_4btoul(cdb->length);
9096 case WRITE_VERIFY_12: {
9097 struct scsi_write_verify_12 *cdb;
9099 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
9100 flags |= CTL_LLF_FUA;
9101 if (cdb->byte2 & SWV_DPO)
9102 flags |= CTL_LLF_DPO;
9103 lba = scsi_4btoul(cdb->addr);
9104 num_blocks = scsi_4btoul(cdb->length);
9109 struct scsi_rw_16 *cdb;
9111 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9112 if (cdb->byte2 & SRW12_FUA)
9113 flags |= CTL_LLF_FUA;
9114 if (cdb->byte2 & SRW12_DPO)
9115 flags |= CTL_LLF_DPO;
9116 lba = scsi_8btou64(cdb->addr);
9117 num_blocks = scsi_4btoul(cdb->length);
9120 case WRITE_ATOMIC_16: {
9121 struct scsi_rw_16 *cdb;
9123 if (lun->be_lun->atomicblock == 0) {
9124 ctl_set_invalid_opcode(ctsio);
9125 ctl_done((union ctl_io *)ctsio);
9126 return (CTL_RETVAL_COMPLETE);
9129 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9130 if (cdb->byte2 & SRW12_FUA)
9131 flags |= CTL_LLF_FUA;
9132 if (cdb->byte2 & SRW12_DPO)
9133 flags |= CTL_LLF_DPO;
9134 lba = scsi_8btou64(cdb->addr);
9135 num_blocks = scsi_4btoul(cdb->length);
9136 if (num_blocks > lun->be_lun->atomicblock) {
9137 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
9138 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
9140 ctl_done((union ctl_io *)ctsio);
9141 return (CTL_RETVAL_COMPLETE);
9145 case WRITE_VERIFY_16: {
9146 struct scsi_write_verify_16 *cdb;
9148 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
9149 flags |= CTL_LLF_FUA;
9150 if (cdb->byte2 & SWV_DPO)
9151 flags |= CTL_LLF_DPO;
9152 lba = scsi_8btou64(cdb->addr);
9153 num_blocks = scsi_4btoul(cdb->length);
9158 * We got a command we don't support. This shouldn't
9159 * happen, commands should be filtered out above us.
9161 ctl_set_invalid_opcode(ctsio);
9162 ctl_done((union ctl_io *)ctsio);
9164 return (CTL_RETVAL_COMPLETE);
9165 break; /* NOTREACHED */
9169 * The first check is to make sure we're in bounds, the second
9170 * check is to catch wrap-around problems. If the lba + num blocks
9171 * is less than the lba, then we've wrapped around and the block
9172 * range is invalid anyway.
9174 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9175 || ((lba + num_blocks) < lba)) {
9176 ctl_set_lba_out_of_range(ctsio);
9177 ctl_done((union ctl_io *)ctsio);
9178 return (CTL_RETVAL_COMPLETE);
9182 * According to SBC-3, a transfer length of 0 is not an error.
9183 * Note that this cannot happen with WRITE(6) or READ(6), since 0
9184 * translates to 256 blocks for those commands.
9186 if (num_blocks == 0) {
9187 ctl_set_success(ctsio);
9188 ctl_done((union ctl_io *)ctsio);
9189 return (CTL_RETVAL_COMPLETE);
9192 /* Set FUA and/or DPO if caches are disabled. */
9194 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9196 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
9198 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9200 flags |= CTL_LLF_FUA;
9203 lbalen = (struct ctl_lba_len_flags *)
9204 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9206 lbalen->len = num_blocks;
9207 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
9209 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9210 ctsio->kern_rel_offset = 0;
9212 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
9214 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9220 ctl_cnw_cont(union ctl_io *io)
9222 struct ctl_scsiio *ctsio;
9223 struct ctl_lun *lun;
9224 struct ctl_lba_len_flags *lbalen;
9227 ctsio = &io->scsiio;
9228 ctsio->io_hdr.status = CTL_STATUS_NONE;
9229 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
9230 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9231 lbalen = (struct ctl_lba_len_flags *)
9232 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9233 lbalen->flags &= ~CTL_LLF_COMPARE;
9234 lbalen->flags |= CTL_LLF_WRITE;
9236 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
9237 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9242 ctl_cnw(struct ctl_scsiio *ctsio)
9244 struct ctl_lun *lun;
9245 struct ctl_lba_len_flags *lbalen;
9247 uint32_t num_blocks;
9250 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9252 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
9255 retval = CTL_RETVAL_COMPLETE;
9257 switch (ctsio->cdb[0]) {
9258 case COMPARE_AND_WRITE: {
9259 struct scsi_compare_and_write *cdb;
9261 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
9262 if (cdb->byte2 & SRW10_FUA)
9263 flags |= CTL_LLF_FUA;
9264 if (cdb->byte2 & SRW10_DPO)
9265 flags |= CTL_LLF_DPO;
9266 lba = scsi_8btou64(cdb->addr);
9267 num_blocks = cdb->length;
9272 * We got a command we don't support. This shouldn't
9273 * happen, commands should be filtered out above us.
9275 ctl_set_invalid_opcode(ctsio);
9276 ctl_done((union ctl_io *)ctsio);
9278 return (CTL_RETVAL_COMPLETE);
9279 break; /* NOTREACHED */
9283 * The first check is to make sure we're in bounds, the second
9284 * check is to catch wrap-around problems. If the lba + num blocks
9285 * is less than the lba, then we've wrapped around and the block
9286 * range is invalid anyway.
9288 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9289 || ((lba + num_blocks) < lba)) {
9290 ctl_set_lba_out_of_range(ctsio);
9291 ctl_done((union ctl_io *)ctsio);
9292 return (CTL_RETVAL_COMPLETE);
9296 * According to SBC-3, a transfer length of 0 is not an error.
9298 if (num_blocks == 0) {
9299 ctl_set_success(ctsio);
9300 ctl_done((union ctl_io *)ctsio);
9301 return (CTL_RETVAL_COMPLETE);
9304 /* Set FUA if write cache is disabled. */
9305 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9307 flags |= CTL_LLF_FUA;
9309 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
9310 ctsio->kern_rel_offset = 0;
9313 * Set the IO_CONT flag, so that if this I/O gets passed to
9314 * ctl_data_submit_done(), it'll get passed back to
9315 * ctl_ctl_cnw_cont() for further processing.
9317 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
9318 ctsio->io_cont = ctl_cnw_cont;
9320 lbalen = (struct ctl_lba_len_flags *)
9321 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9323 lbalen->len = num_blocks;
9324 lbalen->flags = CTL_LLF_COMPARE | flags;
9326 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
9327 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9332 ctl_verify(struct ctl_scsiio *ctsio)
9334 struct ctl_lun *lun;
9335 struct ctl_lba_len_flags *lbalen;
9337 uint32_t num_blocks;
9341 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9343 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
9346 flags = CTL_LLF_FUA;
9347 retval = CTL_RETVAL_COMPLETE;
9349 switch (ctsio->cdb[0]) {
9351 struct scsi_verify_10 *cdb;
9353 cdb = (struct scsi_verify_10 *)ctsio->cdb;
9354 if (cdb->byte2 & SVFY_BYTCHK)
9356 if (cdb->byte2 & SVFY_DPO)
9357 flags |= CTL_LLF_DPO;
9358 lba = scsi_4btoul(cdb->addr);
9359 num_blocks = scsi_2btoul(cdb->length);
9363 struct scsi_verify_12 *cdb;
9365 cdb = (struct scsi_verify_12 *)ctsio->cdb;
9366 if (cdb->byte2 & SVFY_BYTCHK)
9368 if (cdb->byte2 & SVFY_DPO)
9369 flags |= CTL_LLF_DPO;
9370 lba = scsi_4btoul(cdb->addr);
9371 num_blocks = scsi_4btoul(cdb->length);
9375 struct scsi_rw_16 *cdb;
9377 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9378 if (cdb->byte2 & SVFY_BYTCHK)
9380 if (cdb->byte2 & SVFY_DPO)
9381 flags |= CTL_LLF_DPO;
9382 lba = scsi_8btou64(cdb->addr);
9383 num_blocks = scsi_4btoul(cdb->length);
9388 * We got a command we don't support. This shouldn't
9389 * happen, commands should be filtered out above us.
9391 ctl_set_invalid_opcode(ctsio);
9392 ctl_done((union ctl_io *)ctsio);
9393 return (CTL_RETVAL_COMPLETE);
9397 * The first check is to make sure we're in bounds, the second
9398 * check is to catch wrap-around problems. If the lba + num blocks
9399 * is less than the lba, then we've wrapped around and the block
9400 * range is invalid anyway.
9402 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9403 || ((lba + num_blocks) < lba)) {
9404 ctl_set_lba_out_of_range(ctsio);
9405 ctl_done((union ctl_io *)ctsio);
9406 return (CTL_RETVAL_COMPLETE);
9410 * According to SBC-3, a transfer length of 0 is not an error.
9412 if (num_blocks == 0) {
9413 ctl_set_success(ctsio);
9414 ctl_done((union ctl_io *)ctsio);
9415 return (CTL_RETVAL_COMPLETE);
9418 lbalen = (struct ctl_lba_len_flags *)
9419 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9421 lbalen->len = num_blocks;
9423 lbalen->flags = CTL_LLF_COMPARE | flags;
9424 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9426 lbalen->flags = CTL_LLF_VERIFY | flags;
9427 ctsio->kern_total_len = 0;
9429 ctsio->kern_rel_offset = 0;
9431 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9432 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9437 ctl_report_luns(struct ctl_scsiio *ctsio)
9439 struct ctl_softc *softc = control_softc;
9440 struct scsi_report_luns *cdb;
9441 struct scsi_report_luns_data *lun_data;
9442 struct ctl_lun *lun, *request_lun;
9443 struct ctl_port *port;
9444 int num_luns, retval;
9445 uint32_t alloc_len, lun_datalen;
9446 int num_filled, well_known;
9447 uint32_t initidx, targ_lun_id, lun_id;
9449 retval = CTL_RETVAL_COMPLETE;
9452 cdb = (struct scsi_report_luns *)ctsio->cdb;
9454 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9456 mtx_lock(&softc->ctl_lock);
9457 num_luns = softc->num_luns;
9458 mtx_unlock(&softc->ctl_lock);
9460 switch (cdb->select_report) {
9461 case RPL_REPORT_DEFAULT:
9462 case RPL_REPORT_ALL:
9464 case RPL_REPORT_WELLKNOWN:
9469 ctl_set_invalid_field(ctsio,
9475 ctl_done((union ctl_io *)ctsio);
9477 break; /* NOTREACHED */
9480 alloc_len = scsi_4btoul(cdb->length);
9482 * The initiator has to allocate at least 16 bytes for this request,
9483 * so he can at least get the header and the first LUN. Otherwise
9484 * we reject the request (per SPC-3 rev 14, section 6.21).
9486 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9487 sizeof(struct scsi_report_luns_lundata))) {
9488 ctl_set_invalid_field(ctsio,
9494 ctl_done((union ctl_io *)ctsio);
9498 request_lun = (struct ctl_lun *)
9499 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9500 port = ctl_io_port(&ctsio->io_hdr);
9502 lun_datalen = sizeof(*lun_data) +
9503 (num_luns * sizeof(struct scsi_report_luns_lundata));
9505 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9506 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9507 ctsio->kern_sg_entries = 0;
9509 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9511 mtx_lock(&softc->ctl_lock);
9512 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9513 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9514 if (lun_id >= CTL_MAX_LUNS)
9516 lun = softc->ctl_luns[lun_id];
9520 if (targ_lun_id <= 0xff) {
9522 * Peripheral addressing method, bus number 0.
9524 lun_data->luns[num_filled].lundata[0] =
9525 RPL_LUNDATA_ATYP_PERIPH;
9526 lun_data->luns[num_filled].lundata[1] = targ_lun_id;
9528 } else if (targ_lun_id <= 0x3fff) {
9530 * Flat addressing method.
9532 lun_data->luns[num_filled].lundata[0] =
9533 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8);
9534 lun_data->luns[num_filled].lundata[1] =
9535 (targ_lun_id & 0xff);
9537 } else if (targ_lun_id <= 0xffffff) {
9539 * Extended flat addressing method.
9541 lun_data->luns[num_filled].lundata[0] =
9542 RPL_LUNDATA_ATYP_EXTLUN | 0x12;
9543 scsi_ulto3b(targ_lun_id,
9544 &lun_data->luns[num_filled].lundata[1]);
9547 printf("ctl_report_luns: bogus LUN number %jd, "
9548 "skipping\n", (intmax_t)targ_lun_id);
9551 * According to SPC-3, rev 14 section 6.21:
9553 * "The execution of a REPORT LUNS command to any valid and
9554 * installed logical unit shall clear the REPORTED LUNS DATA
9555 * HAS CHANGED unit attention condition for all logical
9556 * units of that target with respect to the requesting
9557 * initiator. A valid and installed logical unit is one
9558 * having a PERIPHERAL QUALIFIER of 000b in the standard
9559 * INQUIRY data (see 6.4.2)."
9561 * If request_lun is NULL, the LUN this report luns command
9562 * was issued to is either disabled or doesn't exist. In that
9563 * case, we shouldn't clear any pending lun change unit
9566 if (request_lun != NULL) {
9567 mtx_lock(&lun->lun_lock);
9568 ctl_clr_ua(lun, initidx, CTL_UA_RES_RELEASE);
9569 mtx_unlock(&lun->lun_lock);
9572 mtx_unlock(&softc->ctl_lock);
9575 * It's quite possible that we've returned fewer LUNs than we allocated
9576 * space for. Trim it.
9578 lun_datalen = sizeof(*lun_data) +
9579 (num_filled * sizeof(struct scsi_report_luns_lundata));
9581 if (lun_datalen < alloc_len) {
9582 ctsio->residual = alloc_len - lun_datalen;
9583 ctsio->kern_data_len = lun_datalen;
9584 ctsio->kern_total_len = lun_datalen;
9586 ctsio->residual = 0;
9587 ctsio->kern_data_len = alloc_len;
9588 ctsio->kern_total_len = alloc_len;
9590 ctsio->kern_data_resid = 0;
9591 ctsio->kern_rel_offset = 0;
9592 ctsio->kern_sg_entries = 0;
9595 * We set this to the actual data length, regardless of how much
9596 * space we actually have to return results. If the user looks at
9597 * this value, he'll know whether or not he allocated enough space
9598 * and reissue the command if necessary. We don't support well
9599 * known logical units, so if the user asks for that, return none.
9601 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9604 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9607 ctl_set_success(ctsio);
9608 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9609 ctsio->be_move_done = ctl_config_move_done;
9610 ctl_datamove((union ctl_io *)ctsio);
9615 ctl_request_sense(struct ctl_scsiio *ctsio)
9617 struct scsi_request_sense *cdb;
9618 struct scsi_sense_data *sense_ptr;
9619 struct ctl_softc *ctl_softc;
9620 struct ctl_lun *lun;
9623 scsi_sense_data_type sense_format;
9624 ctl_ua_type ua_type;
9626 cdb = (struct scsi_request_sense *)ctsio->cdb;
9628 ctl_softc = control_softc;
9629 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9631 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9634 * Determine which sense format the user wants.
9636 if (cdb->byte2 & SRS_DESC)
9637 sense_format = SSD_TYPE_DESC;
9639 sense_format = SSD_TYPE_FIXED;
9641 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9642 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9643 ctsio->kern_sg_entries = 0;
9646 * struct scsi_sense_data, which is currently set to 256 bytes, is
9647 * larger than the largest allowed value for the length field in the
9648 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9650 ctsio->residual = 0;
9651 ctsio->kern_data_len = cdb->length;
9652 ctsio->kern_total_len = cdb->length;
9654 ctsio->kern_data_resid = 0;
9655 ctsio->kern_rel_offset = 0;
9656 ctsio->kern_sg_entries = 0;
9659 * If we don't have a LUN, we don't have any pending sense.
9665 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9667 * Check for pending sense, and then for pending unit attentions.
9668 * Pending sense gets returned first, then pending unit attentions.
9670 mtx_lock(&lun->lun_lock);
9672 if (ctl_is_set(lun->have_ca, initidx)) {
9673 scsi_sense_data_type stored_format;
9676 * Check to see which sense format was used for the stored
9679 stored_format = scsi_sense_type(&lun->pending_sense[initidx]);
9682 * If the user requested a different sense format than the
9683 * one we stored, then we need to convert it to the other
9684 * format. If we're going from descriptor to fixed format
9685 * sense data, we may lose things in translation, depending
9686 * on what options were used.
9688 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9689 * for some reason we'll just copy it out as-is.
9691 if ((stored_format == SSD_TYPE_FIXED)
9692 && (sense_format == SSD_TYPE_DESC))
9693 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9694 &lun->pending_sense[initidx],
9695 (struct scsi_sense_data_desc *)sense_ptr);
9696 else if ((stored_format == SSD_TYPE_DESC)
9697 && (sense_format == SSD_TYPE_FIXED))
9698 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9699 &lun->pending_sense[initidx],
9700 (struct scsi_sense_data_fixed *)sense_ptr);
9702 memcpy(sense_ptr, &lun->pending_sense[initidx],
9703 MIN(sizeof(*sense_ptr),
9704 sizeof(lun->pending_sense[initidx])));
9706 ctl_clear_mask(lun->have_ca, initidx);
9711 ua_type = ctl_build_ua(lun, initidx, sense_ptr, sense_format);
9712 if (ua_type != CTL_UA_NONE)
9714 if (ua_type == CTL_UA_LUN_CHANGE) {
9715 mtx_unlock(&lun->lun_lock);
9716 mtx_lock(&ctl_softc->ctl_lock);
9717 ctl_clear_ua(ctl_softc, initidx, ua_type);
9718 mtx_unlock(&ctl_softc->ctl_lock);
9719 mtx_lock(&lun->lun_lock);
9723 mtx_unlock(&lun->lun_lock);
9726 * We already have a pending error, return it.
9728 if (have_error != 0) {
9730 * We report the SCSI status as OK, since the status of the
9731 * request sense command itself is OK.
9732 * We report 0 for the sense length, because we aren't doing
9733 * autosense in this case. We're reporting sense as
9736 ctl_set_success(ctsio);
9737 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9738 ctsio->be_move_done = ctl_config_move_done;
9739 ctl_datamove((union ctl_io *)ctsio);
9740 return (CTL_RETVAL_COMPLETE);
9746 * No sense information to report, so we report that everything is
9749 ctl_set_sense_data(sense_ptr,
9752 /*current_error*/ 1,
9753 /*sense_key*/ SSD_KEY_NO_SENSE,
9759 * We report 0 for the sense length, because we aren't doing
9760 * autosense in this case. We're reporting sense as parameter data.
9762 ctl_set_success(ctsio);
9763 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9764 ctsio->be_move_done = ctl_config_move_done;
9765 ctl_datamove((union ctl_io *)ctsio);
9766 return (CTL_RETVAL_COMPLETE);
9770 ctl_tur(struct ctl_scsiio *ctsio)
9773 CTL_DEBUG_PRINT(("ctl_tur\n"));
9775 ctl_set_success(ctsio);
9776 ctl_done((union ctl_io *)ctsio);
9778 return (CTL_RETVAL_COMPLETE);
9783 ctl_cmddt_inquiry(struct ctl_scsiio *ctsio)
9790 * SCSI VPD page 0x00, the Supported VPD Pages page.
9793 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9795 struct scsi_vpd_supported_pages *pages;
9797 struct ctl_lun *lun;
9800 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9802 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9803 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9804 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9805 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9806 ctsio->kern_sg_entries = 0;
9808 if (sup_page_size < alloc_len) {
9809 ctsio->residual = alloc_len - sup_page_size;
9810 ctsio->kern_data_len = sup_page_size;
9811 ctsio->kern_total_len = sup_page_size;
9813 ctsio->residual = 0;
9814 ctsio->kern_data_len = alloc_len;
9815 ctsio->kern_total_len = alloc_len;
9817 ctsio->kern_data_resid = 0;
9818 ctsio->kern_rel_offset = 0;
9819 ctsio->kern_sg_entries = 0;
9822 * The control device is always connected. The disk device, on the
9823 * other hand, may not be online all the time. Need to change this
9824 * to figure out whether the disk device is actually online or not.
9827 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9828 lun->be_lun->lun_type;
9830 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9833 /* Supported VPD pages */
9834 pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9836 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9837 /* Device Identification */
9838 pages->page_list[p++] = SVPD_DEVICE_ID;
9839 /* Extended INQUIRY Data */
9840 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9841 /* Mode Page Policy */
9842 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9844 pages->page_list[p++] = SVPD_SCSI_PORTS;
9845 /* Third-party Copy */
9846 pages->page_list[p++] = SVPD_SCSI_TPC;
9847 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9849 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9850 /* Block Device Characteristics */
9851 pages->page_list[p++] = SVPD_BDC;
9852 /* Logical Block Provisioning */
9853 pages->page_list[p++] = SVPD_LBP;
9857 ctl_set_success(ctsio);
9858 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9859 ctsio->be_move_done = ctl_config_move_done;
9860 ctl_datamove((union ctl_io *)ctsio);
9861 return (CTL_RETVAL_COMPLETE);
9865 * SCSI VPD page 0x80, the Unit Serial Number page.
9868 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9870 struct scsi_vpd_unit_serial_number *sn_ptr;
9871 struct ctl_lun *lun;
9874 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9876 data_len = 4 + CTL_SN_LEN;
9877 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9878 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9879 if (data_len < alloc_len) {
9880 ctsio->residual = alloc_len - data_len;
9881 ctsio->kern_data_len = data_len;
9882 ctsio->kern_total_len = data_len;
9884 ctsio->residual = 0;
9885 ctsio->kern_data_len = alloc_len;
9886 ctsio->kern_total_len = alloc_len;
9888 ctsio->kern_data_resid = 0;
9889 ctsio->kern_rel_offset = 0;
9890 ctsio->kern_sg_entries = 0;
9893 * The control device is always connected. The disk device, on the
9894 * other hand, may not be online all the time. Need to change this
9895 * to figure out whether the disk device is actually online or not.
9898 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9899 lun->be_lun->lun_type;
9901 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9903 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9904 sn_ptr->length = CTL_SN_LEN;
9906 * If we don't have a LUN, we just leave the serial number as
9910 strncpy((char *)sn_ptr->serial_num,
9911 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9913 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9915 ctl_set_success(ctsio);
9916 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9917 ctsio->be_move_done = ctl_config_move_done;
9918 ctl_datamove((union ctl_io *)ctsio);
9919 return (CTL_RETVAL_COMPLETE);
9924 * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9927 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9929 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9930 struct ctl_lun *lun;
9933 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9935 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9936 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9937 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9938 ctsio->kern_sg_entries = 0;
9940 if (data_len < alloc_len) {
9941 ctsio->residual = alloc_len - data_len;
9942 ctsio->kern_data_len = data_len;
9943 ctsio->kern_total_len = data_len;
9945 ctsio->residual = 0;
9946 ctsio->kern_data_len = alloc_len;
9947 ctsio->kern_total_len = alloc_len;
9949 ctsio->kern_data_resid = 0;
9950 ctsio->kern_rel_offset = 0;
9951 ctsio->kern_sg_entries = 0;
9954 * The control device is always connected. The disk device, on the
9955 * other hand, may not be online all the time.
9958 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9959 lun->be_lun->lun_type;
9961 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9962 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9963 scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9965 * We support head of queue, ordered and simple tags.
9967 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9969 * Volatile cache supported.
9971 eid_ptr->flags3 = SVPD_EID_V_SUP;
9974 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9975 * attention for a particular IT nexus on all LUNs once we report
9976 * it to that nexus once. This bit is required as of SPC-4.
9978 eid_ptr->flags4 = SVPD_EID_LUICLT;
9981 * XXX KDM in order to correctly answer this, we would need
9982 * information from the SIM to determine how much sense data it
9983 * can send. So this would really be a path inquiry field, most
9984 * likely. This can be set to a maximum of 252 according to SPC-4,
9985 * but the hardware may or may not be able to support that much.
9986 * 0 just means that the maximum sense data length is not reported.
9988 eid_ptr->max_sense_length = 0;
9990 ctl_set_success(ctsio);
9991 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9992 ctsio->be_move_done = ctl_config_move_done;
9993 ctl_datamove((union ctl_io *)ctsio);
9994 return (CTL_RETVAL_COMPLETE);
9998 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
10000 struct scsi_vpd_mode_page_policy *mpp_ptr;
10001 struct ctl_lun *lun;
10004 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10006 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
10007 sizeof(struct scsi_vpd_mode_page_policy_descr);
10009 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10010 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
10011 ctsio->kern_sg_entries = 0;
10013 if (data_len < alloc_len) {
10014 ctsio->residual = alloc_len - data_len;
10015 ctsio->kern_data_len = data_len;
10016 ctsio->kern_total_len = data_len;
10018 ctsio->residual = 0;
10019 ctsio->kern_data_len = alloc_len;
10020 ctsio->kern_total_len = alloc_len;
10022 ctsio->kern_data_resid = 0;
10023 ctsio->kern_rel_offset = 0;
10024 ctsio->kern_sg_entries = 0;
10027 * The control device is always connected. The disk device, on the
10028 * other hand, may not be online all the time.
10031 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10032 lun->be_lun->lun_type;
10034 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10035 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
10036 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
10037 mpp_ptr->descr[0].page_code = 0x3f;
10038 mpp_ptr->descr[0].subpage_code = 0xff;
10039 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
10041 ctl_set_success(ctsio);
10042 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10043 ctsio->be_move_done = ctl_config_move_done;
10044 ctl_datamove((union ctl_io *)ctsio);
10045 return (CTL_RETVAL_COMPLETE);
10049 * SCSI VPD page 0x83, the Device Identification page.
10052 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
10054 struct scsi_vpd_device_id *devid_ptr;
10055 struct scsi_vpd_id_descriptor *desc;
10056 struct ctl_softc *softc;
10057 struct ctl_lun *lun;
10058 struct ctl_port *port;
10062 softc = control_softc;
10064 port = softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)];
10065 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10067 data_len = sizeof(struct scsi_vpd_device_id) +
10068 sizeof(struct scsi_vpd_id_descriptor) +
10069 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
10070 sizeof(struct scsi_vpd_id_descriptor) +
10071 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
10072 if (lun && lun->lun_devid)
10073 data_len += lun->lun_devid->len;
10074 if (port->port_devid)
10075 data_len += port->port_devid->len;
10076 if (port->target_devid)
10077 data_len += port->target_devid->len;
10079 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10080 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
10081 ctsio->kern_sg_entries = 0;
10083 if (data_len < alloc_len) {
10084 ctsio->residual = alloc_len - data_len;
10085 ctsio->kern_data_len = data_len;
10086 ctsio->kern_total_len = data_len;
10088 ctsio->residual = 0;
10089 ctsio->kern_data_len = alloc_len;
10090 ctsio->kern_total_len = alloc_len;
10092 ctsio->kern_data_resid = 0;
10093 ctsio->kern_rel_offset = 0;
10094 ctsio->kern_sg_entries = 0;
10097 * The control device is always connected. The disk device, on the
10098 * other hand, may not be online all the time.
10101 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10102 lun->be_lun->lun_type;
10104 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10105 devid_ptr->page_code = SVPD_DEVICE_ID;
10106 scsi_ulto2b(data_len - 4, devid_ptr->length);
10108 if (port->port_type == CTL_PORT_FC)
10109 proto = SCSI_PROTO_FC << 4;
10110 else if (port->port_type == CTL_PORT_ISCSI)
10111 proto = SCSI_PROTO_ISCSI << 4;
10113 proto = SCSI_PROTO_SPI << 4;
10114 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
10117 * We're using a LUN association here. i.e., this device ID is a
10118 * per-LUN identifier.
10120 if (lun && lun->lun_devid) {
10121 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
10122 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
10123 lun->lun_devid->len);
10127 * This is for the WWPN which is a port association.
10129 if (port->port_devid) {
10130 memcpy(desc, port->port_devid->data, port->port_devid->len);
10131 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
10132 port->port_devid->len);
10136 * This is for the Relative Target Port(type 4h) identifier
10138 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
10139 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
10140 SVPD_ID_TYPE_RELTARG;
10142 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
10143 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
10144 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
10147 * This is for the Target Port Group(type 5h) identifier
10149 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
10150 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
10151 SVPD_ID_TYPE_TPORTGRP;
10153 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1,
10154 &desc->identifier[2]);
10155 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
10156 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
10159 * This is for the Target identifier
10161 if (port->target_devid) {
10162 memcpy(desc, port->target_devid->data, port->target_devid->len);
10165 ctl_set_success(ctsio);
10166 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10167 ctsio->be_move_done = ctl_config_move_done;
10168 ctl_datamove((union ctl_io *)ctsio);
10169 return (CTL_RETVAL_COMPLETE);
10173 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
10175 struct ctl_softc *softc = control_softc;
10176 struct scsi_vpd_scsi_ports *sp;
10177 struct scsi_vpd_port_designation *pd;
10178 struct scsi_vpd_port_designation_cont *pdc;
10179 struct ctl_lun *lun;
10180 struct ctl_port *port;
10181 int data_len, num_target_ports, iid_len, id_len, g, pg, p;
10182 int num_target_port_groups;
10184 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10186 if (softc->is_single)
10187 num_target_port_groups = 1;
10189 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
10190 num_target_ports = 0;
10193 mtx_lock(&softc->ctl_lock);
10194 STAILQ_FOREACH(port, &softc->port_list, links) {
10195 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
10198 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
10200 num_target_ports++;
10201 if (port->init_devid)
10202 iid_len += port->init_devid->len;
10203 if (port->port_devid)
10204 id_len += port->port_devid->len;
10206 mtx_unlock(&softc->ctl_lock);
10208 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups *
10209 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
10210 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
10211 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10212 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
10213 ctsio->kern_sg_entries = 0;
10215 if (data_len < alloc_len) {
10216 ctsio->residual = alloc_len - data_len;
10217 ctsio->kern_data_len = data_len;
10218 ctsio->kern_total_len = data_len;
10220 ctsio->residual = 0;
10221 ctsio->kern_data_len = alloc_len;
10222 ctsio->kern_total_len = alloc_len;
10224 ctsio->kern_data_resid = 0;
10225 ctsio->kern_rel_offset = 0;
10226 ctsio->kern_sg_entries = 0;
10229 * The control device is always connected. The disk device, on the
10230 * other hand, may not be online all the time. Need to change this
10231 * to figure out whether the disk device is actually online or not.
10234 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
10235 lun->be_lun->lun_type;
10237 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10239 sp->page_code = SVPD_SCSI_PORTS;
10240 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
10242 pd = &sp->design[0];
10244 mtx_lock(&softc->ctl_lock);
10245 pg = softc->port_offset / CTL_MAX_PORTS;
10246 for (g = 0; g < num_target_port_groups; g++) {
10247 STAILQ_FOREACH(port, &softc->port_list, links) {
10248 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
10251 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
10253 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS;
10254 scsi_ulto2b(p, pd->relative_port_id);
10255 if (port->init_devid && g == pg) {
10256 iid_len = port->init_devid->len;
10257 memcpy(pd->initiator_transportid,
10258 port->init_devid->data, port->init_devid->len);
10261 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
10262 pdc = (struct scsi_vpd_port_designation_cont *)
10263 (&pd->initiator_transportid[iid_len]);
10264 if (port->port_devid && g == pg) {
10265 id_len = port->port_devid->len;
10266 memcpy(pdc->target_port_descriptors,
10267 port->port_devid->data, port->port_devid->len);
10270 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
10271 pd = (struct scsi_vpd_port_designation *)
10272 ((uint8_t *)pdc->target_port_descriptors + id_len);
10275 mtx_unlock(&softc->ctl_lock);
10277 ctl_set_success(ctsio);
10278 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10279 ctsio->be_move_done = ctl_config_move_done;
10280 ctl_datamove((union ctl_io *)ctsio);
10281 return (CTL_RETVAL_COMPLETE);
10285 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
10287 struct scsi_vpd_block_limits *bl_ptr;
10288 struct ctl_lun *lun;
10291 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10293 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
10294 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
10295 ctsio->kern_sg_entries = 0;
10297 if (sizeof(*bl_ptr) < alloc_len) {
10298 ctsio->residual = alloc_len - sizeof(*bl_ptr);
10299 ctsio->kern_data_len = sizeof(*bl_ptr);
10300 ctsio->kern_total_len = sizeof(*bl_ptr);
10302 ctsio->residual = 0;
10303 ctsio->kern_data_len = alloc_len;
10304 ctsio->kern_total_len = alloc_len;
10306 ctsio->kern_data_resid = 0;
10307 ctsio->kern_rel_offset = 0;
10308 ctsio->kern_sg_entries = 0;
10311 * The control device is always connected. The disk device, on the
10312 * other hand, may not be online all the time. Need to change this
10313 * to figure out whether the disk device is actually online or not.
10316 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10317 lun->be_lun->lun_type;
10319 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10321 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
10322 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
10323 bl_ptr->max_cmp_write_len = 0xff;
10324 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
10326 bs = lun->be_lun->blocksize;
10327 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
10328 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10329 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
10330 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
10331 if (lun->be_lun->ublockexp != 0) {
10332 scsi_ulto4b((1 << lun->be_lun->ublockexp),
10333 bl_ptr->opt_unmap_grain);
10334 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
10335 bl_ptr->unmap_grain_align);
10338 scsi_ulto4b(lun->be_lun->atomicblock,
10339 bl_ptr->max_atomic_transfer_length);
10340 scsi_ulto4b(0, bl_ptr->atomic_alignment);
10341 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
10343 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
10345 ctl_set_success(ctsio);
10346 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10347 ctsio->be_move_done = ctl_config_move_done;
10348 ctl_datamove((union ctl_io *)ctsio);
10349 return (CTL_RETVAL_COMPLETE);
10353 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
10355 struct scsi_vpd_block_device_characteristics *bdc_ptr;
10356 struct ctl_lun *lun;
10360 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10362 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
10363 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
10364 ctsio->kern_sg_entries = 0;
10366 if (sizeof(*bdc_ptr) < alloc_len) {
10367 ctsio->residual = alloc_len - sizeof(*bdc_ptr);
10368 ctsio->kern_data_len = sizeof(*bdc_ptr);
10369 ctsio->kern_total_len = sizeof(*bdc_ptr);
10371 ctsio->residual = 0;
10372 ctsio->kern_data_len = alloc_len;
10373 ctsio->kern_total_len = alloc_len;
10375 ctsio->kern_data_resid = 0;
10376 ctsio->kern_rel_offset = 0;
10377 ctsio->kern_sg_entries = 0;
10380 * The control device is always connected. The disk device, on the
10381 * other hand, may not be online all the time. Need to change this
10382 * to figure out whether the disk device is actually online or not.
10385 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10386 lun->be_lun->lun_type;
10388 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10389 bdc_ptr->page_code = SVPD_BDC;
10390 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
10392 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL)
10393 i = strtol(value, NULL, 0);
10395 i = CTL_DEFAULT_ROTATION_RATE;
10396 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
10398 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL)
10399 i = strtol(value, NULL, 0);
10402 bdc_ptr->wab_wac_ff = (i & 0x0f);
10403 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
10405 ctl_set_success(ctsio);
10406 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10407 ctsio->be_move_done = ctl_config_move_done;
10408 ctl_datamove((union ctl_io *)ctsio);
10409 return (CTL_RETVAL_COMPLETE);
10413 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
10415 struct scsi_vpd_logical_block_prov *lbp_ptr;
10416 struct ctl_lun *lun;
10418 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10420 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
10421 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
10422 ctsio->kern_sg_entries = 0;
10424 if (sizeof(*lbp_ptr) < alloc_len) {
10425 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
10426 ctsio->kern_data_len = sizeof(*lbp_ptr);
10427 ctsio->kern_total_len = sizeof(*lbp_ptr);
10429 ctsio->residual = 0;
10430 ctsio->kern_data_len = alloc_len;
10431 ctsio->kern_total_len = alloc_len;
10433 ctsio->kern_data_resid = 0;
10434 ctsio->kern_rel_offset = 0;
10435 ctsio->kern_sg_entries = 0;
10438 * The control device is always connected. The disk device, on the
10439 * other hand, may not be online all the time. Need to change this
10440 * to figure out whether the disk device is actually online or not.
10443 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10444 lun->be_lun->lun_type;
10446 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10448 lbp_ptr->page_code = SVPD_LBP;
10449 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
10450 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
10451 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10452 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
10453 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
10454 lbp_ptr->prov_type = SVPD_LBP_THIN;
10457 ctl_set_success(ctsio);
10458 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10459 ctsio->be_move_done = ctl_config_move_done;
10460 ctl_datamove((union ctl_io *)ctsio);
10461 return (CTL_RETVAL_COMPLETE);
10465 * INQUIRY with the EVPD bit set.
10468 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
10470 struct ctl_lun *lun;
10471 struct scsi_inquiry *cdb;
10472 int alloc_len, retval;
10474 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10475 cdb = (struct scsi_inquiry *)ctsio->cdb;
10476 alloc_len = scsi_2btoul(cdb->length);
10478 switch (cdb->page_code) {
10479 case SVPD_SUPPORTED_PAGES:
10480 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
10482 case SVPD_UNIT_SERIAL_NUMBER:
10483 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
10485 case SVPD_DEVICE_ID:
10486 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
10488 case SVPD_EXTENDED_INQUIRY_DATA:
10489 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
10491 case SVPD_MODE_PAGE_POLICY:
10492 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
10494 case SVPD_SCSI_PORTS:
10495 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10497 case SVPD_SCSI_TPC:
10498 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10500 case SVPD_BLOCK_LIMITS:
10501 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10503 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10506 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10508 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10511 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10513 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10517 ctl_set_invalid_field(ctsio,
10523 ctl_done((union ctl_io *)ctsio);
10524 retval = CTL_RETVAL_COMPLETE;
10532 * Standard INQUIRY data.
10535 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10537 struct scsi_inquiry_data *inq_ptr;
10538 struct scsi_inquiry *cdb;
10539 struct ctl_softc *softc;
10540 struct ctl_lun *lun;
10542 uint32_t alloc_len, data_len;
10543 ctl_port_type port_type;
10545 softc = control_softc;
10548 * Figure out whether we're talking to a Fibre Channel port or not.
10549 * We treat the ioctl front end, and any SCSI adapters, as packetized
10552 port_type = softc->ctl_ports[
10553 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type;
10554 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10555 port_type = CTL_PORT_SCSI;
10557 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10558 cdb = (struct scsi_inquiry *)ctsio->cdb;
10559 alloc_len = scsi_2btoul(cdb->length);
10562 * We malloc the full inquiry data size here and fill it
10563 * in. If the user only asks for less, we'll give him
10566 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10567 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10568 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10569 ctsio->kern_sg_entries = 0;
10570 ctsio->kern_data_resid = 0;
10571 ctsio->kern_rel_offset = 0;
10573 if (data_len < alloc_len) {
10574 ctsio->residual = alloc_len - data_len;
10575 ctsio->kern_data_len = data_len;
10576 ctsio->kern_total_len = data_len;
10578 ctsio->residual = 0;
10579 ctsio->kern_data_len = alloc_len;
10580 ctsio->kern_total_len = alloc_len;
10584 * If we have a LUN configured, report it as connected. Otherwise,
10585 * report that it is offline or no device is supported, depending
10586 * on the value of inquiry_pq_no_lun.
10588 * According to the spec (SPC-4 r34), the peripheral qualifier
10589 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario:
10591 * "A peripheral device having the specified peripheral device type
10592 * is not connected to this logical unit. However, the device
10593 * server is capable of supporting the specified peripheral device
10594 * type on this logical unit."
10596 * According to the same spec, the peripheral qualifier
10597 * SID_QUAL_BAD_LU (011b) is used in this scenario:
10599 * "The device server is not capable of supporting a peripheral
10600 * device on this logical unit. For this peripheral qualifier the
10601 * peripheral device type shall be set to 1Fh. All other peripheral
10602 * device type values are reserved for this peripheral qualifier."
10604 * Given the text, it would seem that we probably want to report that
10605 * the LUN is offline here. There is no LUN connected, but we can
10606 * support a LUN at the given LUN number.
10608 * In the real world, though, it sounds like things are a little
10611 * - Linux, when presented with a LUN with the offline peripheral
10612 * qualifier, will create an sg driver instance for it. So when
10613 * you attach it to CTL, you wind up with a ton of sg driver
10614 * instances. (One for every LUN that Linux bothered to probe.)
10615 * Linux does this despite the fact that it issues a REPORT LUNs
10616 * to LUN 0 to get the inventory of supported LUNs.
10618 * - There is other anecdotal evidence (from Emulex folks) about
10619 * arrays that use the offline peripheral qualifier for LUNs that
10620 * are on the "passive" path in an active/passive array.
10622 * So the solution is provide a hopefully reasonable default
10623 * (return bad/no LUN) and allow the user to change the behavior
10624 * with a tunable/sysctl variable.
10627 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10628 lun->be_lun->lun_type;
10629 else if (softc->inquiry_pq_no_lun == 0)
10630 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10632 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10634 /* RMB in byte 2 is 0 */
10635 inq_ptr->version = SCSI_REV_SPC4;
10638 * According to SAM-3, even if a device only supports a single
10639 * level of LUN addressing, it should still set the HISUP bit:
10641 * 4.9.1 Logical unit numbers overview
10643 * All logical unit number formats described in this standard are
10644 * hierarchical in structure even when only a single level in that
10645 * hierarchy is used. The HISUP bit shall be set to one in the
10646 * standard INQUIRY data (see SPC-2) when any logical unit number
10647 * format described in this standard is used. Non-hierarchical
10648 * formats are outside the scope of this standard.
10650 * Therefore we set the HiSup bit here.
10652 * The reponse format is 2, per SPC-3.
10654 inq_ptr->response_format = SID_HiSup | 2;
10656 inq_ptr->additional_length = data_len -
10657 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10658 CTL_DEBUG_PRINT(("additional_length = %d\n",
10659 inq_ptr->additional_length));
10661 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10662 /* 16 bit addressing */
10663 if (port_type == CTL_PORT_SCSI)
10664 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10665 /* XXX set the SID_MultiP bit here if we're actually going to
10666 respond on multiple ports */
10667 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10669 /* 16 bit data bus, synchronous transfers */
10670 if (port_type == CTL_PORT_SCSI)
10671 inq_ptr->flags = SID_WBus16 | SID_Sync;
10673 * XXX KDM do we want to support tagged queueing on the control
10677 || (lun->be_lun->lun_type != T_PROCESSOR))
10678 inq_ptr->flags |= SID_CmdQue;
10680 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10681 * We have 8 bytes for the vendor name, and 16 bytes for the device
10682 * name and 4 bytes for the revision.
10684 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10685 "vendor")) == NULL) {
10686 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10688 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10689 strncpy(inq_ptr->vendor, val,
10690 min(sizeof(inq_ptr->vendor), strlen(val)));
10693 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10694 sizeof(inq_ptr->product));
10695 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10696 switch (lun->be_lun->lun_type) {
10698 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10699 sizeof(inq_ptr->product));
10702 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10703 sizeof(inq_ptr->product));
10706 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10707 sizeof(inq_ptr->product));
10711 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10712 strncpy(inq_ptr->product, val,
10713 min(sizeof(inq_ptr->product), strlen(val)));
10717 * XXX make this a macro somewhere so it automatically gets
10718 * incremented when we make changes.
10720 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10721 "revision")) == NULL) {
10722 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10724 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10725 strncpy(inq_ptr->revision, val,
10726 min(sizeof(inq_ptr->revision), strlen(val)));
10730 * For parallel SCSI, we support double transition and single
10731 * transition clocking. We also support QAS (Quick Arbitration
10732 * and Selection) and Information Unit transfers on both the
10733 * control and array devices.
10735 if (port_type == CTL_PORT_SCSI)
10736 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10739 /* SAM-5 (no version claimed) */
10740 scsi_ulto2b(0x00A0, inq_ptr->version1);
10741 /* SPC-4 (no version claimed) */
10742 scsi_ulto2b(0x0460, inq_ptr->version2);
10743 if (port_type == CTL_PORT_FC) {
10744 /* FCP-2 ANSI INCITS.350:2003 */
10745 scsi_ulto2b(0x0917, inq_ptr->version3);
10746 } else if (port_type == CTL_PORT_SCSI) {
10747 /* SPI-4 ANSI INCITS.362:200x */
10748 scsi_ulto2b(0x0B56, inq_ptr->version3);
10749 } else if (port_type == CTL_PORT_ISCSI) {
10750 /* iSCSI (no version claimed) */
10751 scsi_ulto2b(0x0960, inq_ptr->version3);
10752 } else if (port_type == CTL_PORT_SAS) {
10753 /* SAS (no version claimed) */
10754 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10758 /* SBC-4 (no version claimed) */
10759 scsi_ulto2b(0x0600, inq_ptr->version4);
10761 switch (lun->be_lun->lun_type) {
10763 /* SBC-4 (no version claimed) */
10764 scsi_ulto2b(0x0600, inq_ptr->version4);
10772 ctl_set_success(ctsio);
10773 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10774 ctsio->be_move_done = ctl_config_move_done;
10775 ctl_datamove((union ctl_io *)ctsio);
10776 return (CTL_RETVAL_COMPLETE);
10780 ctl_inquiry(struct ctl_scsiio *ctsio)
10782 struct scsi_inquiry *cdb;
10785 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10787 cdb = (struct scsi_inquiry *)ctsio->cdb;
10788 if (cdb->byte2 & SI_EVPD)
10789 retval = ctl_inquiry_evpd(ctsio);
10790 else if (cdb->page_code == 0)
10791 retval = ctl_inquiry_std(ctsio);
10793 ctl_set_invalid_field(ctsio,
10799 ctl_done((union ctl_io *)ctsio);
10800 return (CTL_RETVAL_COMPLETE);
10807 * For known CDB types, parse the LBA and length.
10810 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10812 if (io->io_hdr.io_type != CTL_IO_SCSI)
10815 switch (io->scsiio.cdb[0]) {
10816 case COMPARE_AND_WRITE: {
10817 struct scsi_compare_and_write *cdb;
10819 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10821 *lba = scsi_8btou64(cdb->addr);
10822 *len = cdb->length;
10827 struct scsi_rw_6 *cdb;
10829 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10831 *lba = scsi_3btoul(cdb->addr);
10832 /* only 5 bits are valid in the most significant address byte */
10834 *len = cdb->length;
10839 struct scsi_rw_10 *cdb;
10841 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10843 *lba = scsi_4btoul(cdb->addr);
10844 *len = scsi_2btoul(cdb->length);
10847 case WRITE_VERIFY_10: {
10848 struct scsi_write_verify_10 *cdb;
10850 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10852 *lba = scsi_4btoul(cdb->addr);
10853 *len = scsi_2btoul(cdb->length);
10858 struct scsi_rw_12 *cdb;
10860 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10862 *lba = scsi_4btoul(cdb->addr);
10863 *len = scsi_4btoul(cdb->length);
10866 case WRITE_VERIFY_12: {
10867 struct scsi_write_verify_12 *cdb;
10869 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10871 *lba = scsi_4btoul(cdb->addr);
10872 *len = scsi_4btoul(cdb->length);
10877 case WRITE_ATOMIC_16: {
10878 struct scsi_rw_16 *cdb;
10880 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10882 *lba = scsi_8btou64(cdb->addr);
10883 *len = scsi_4btoul(cdb->length);
10886 case WRITE_VERIFY_16: {
10887 struct scsi_write_verify_16 *cdb;
10889 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10891 *lba = scsi_8btou64(cdb->addr);
10892 *len = scsi_4btoul(cdb->length);
10895 case WRITE_SAME_10: {
10896 struct scsi_write_same_10 *cdb;
10898 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10900 *lba = scsi_4btoul(cdb->addr);
10901 *len = scsi_2btoul(cdb->length);
10904 case WRITE_SAME_16: {
10905 struct scsi_write_same_16 *cdb;
10907 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10909 *lba = scsi_8btou64(cdb->addr);
10910 *len = scsi_4btoul(cdb->length);
10914 struct scsi_verify_10 *cdb;
10916 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10918 *lba = scsi_4btoul(cdb->addr);
10919 *len = scsi_2btoul(cdb->length);
10923 struct scsi_verify_12 *cdb;
10925 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10927 *lba = scsi_4btoul(cdb->addr);
10928 *len = scsi_4btoul(cdb->length);
10932 struct scsi_verify_16 *cdb;
10934 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10936 *lba = scsi_8btou64(cdb->addr);
10937 *len = scsi_4btoul(cdb->length);
10945 case SERVICE_ACTION_IN: { /* GET LBA STATUS */
10946 struct scsi_get_lba_status *cdb;
10948 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10949 *lba = scsi_8btou64(cdb->addr);
10955 break; /* NOTREACHED */
10962 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10965 uint64_t endlba1, endlba2;
10967 endlba1 = lba1 + len1 - (seq ? 0 : 1);
10968 endlba2 = lba2 + len2 - 1;
10970 if ((endlba1 < lba2) || (endlba2 < lba1))
10971 return (CTL_ACTION_PASS);
10973 return (CTL_ACTION_BLOCK);
10977 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10979 struct ctl_ptr_len_flags *ptrlen;
10980 struct scsi_unmap_desc *buf, *end, *range;
10984 /* If not UNMAP -- go other way. */
10985 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10986 io->scsiio.cdb[0] != UNMAP)
10987 return (CTL_ACTION_ERROR);
10989 /* If UNMAP without data -- block and wait for data. */
10990 ptrlen = (struct ctl_ptr_len_flags *)
10991 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10992 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10993 ptrlen->ptr == NULL)
10994 return (CTL_ACTION_BLOCK);
10996 /* UNMAP with data -- check for collision. */
10997 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10998 end = buf + ptrlen->len / sizeof(*buf);
10999 for (range = buf; range < end; range++) {
11000 lba = scsi_8btou64(range->lba);
11001 len = scsi_4btoul(range->length);
11002 if ((lba < lba2 + len2) && (lba + len > lba2))
11003 return (CTL_ACTION_BLOCK);
11005 return (CTL_ACTION_PASS);
11009 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
11011 uint64_t lba1, lba2;
11012 uint64_t len1, len2;
11015 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
11016 return (CTL_ACTION_ERROR);
11018 retval = ctl_extent_check_unmap(io1, lba2, len2);
11019 if (retval != CTL_ACTION_ERROR)
11022 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
11023 return (CTL_ACTION_ERROR);
11025 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
11029 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2)
11031 uint64_t lba1, lba2;
11032 uint64_t len1, len2;
11034 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
11035 return (CTL_ACTION_ERROR);
11036 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
11037 return (CTL_ACTION_ERROR);
11039 if (lba1 + len1 == lba2)
11040 return (CTL_ACTION_BLOCK);
11041 return (CTL_ACTION_PASS);
11045 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
11046 union ctl_io *ooa_io)
11048 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
11049 ctl_serialize_action *serialize_row;
11052 * The initiator attempted multiple untagged commands at the same
11053 * time. Can't do that.
11055 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11056 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11057 && ((pending_io->io_hdr.nexus.targ_port ==
11058 ooa_io->io_hdr.nexus.targ_port)
11059 && (pending_io->io_hdr.nexus.initid.id ==
11060 ooa_io->io_hdr.nexus.initid.id))
11061 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
11062 CTL_FLAG_STATUS_SENT)) == 0))
11063 return (CTL_ACTION_OVERLAP);
11066 * The initiator attempted to send multiple tagged commands with
11067 * the same ID. (It's fine if different initiators have the same
11070 * Even if all of those conditions are true, we don't kill the I/O
11071 * if the command ahead of us has been aborted. We won't end up
11072 * sending it to the FETD, and it's perfectly legal to resend a
11073 * command with the same tag number as long as the previous
11074 * instance of this tag number has been aborted somehow.
11076 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
11077 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
11078 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
11079 && ((pending_io->io_hdr.nexus.targ_port ==
11080 ooa_io->io_hdr.nexus.targ_port)
11081 && (pending_io->io_hdr.nexus.initid.id ==
11082 ooa_io->io_hdr.nexus.initid.id))
11083 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
11084 CTL_FLAG_STATUS_SENT)) == 0))
11085 return (CTL_ACTION_OVERLAP_TAG);
11088 * If we get a head of queue tag, SAM-3 says that we should
11089 * immediately execute it.
11091 * What happens if this command would normally block for some other
11092 * reason? e.g. a request sense with a head of queue tag
11093 * immediately after a write. Normally that would block, but this
11094 * will result in its getting executed immediately...
11096 * We currently return "pass" instead of "skip", so we'll end up
11097 * going through the rest of the queue to check for overlapped tags.
11099 * XXX KDM check for other types of blockage first??
11101 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
11102 return (CTL_ACTION_PASS);
11105 * Ordered tags have to block until all items ahead of them
11106 * have completed. If we get called with an ordered tag, we always
11107 * block, if something else is ahead of us in the queue.
11109 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
11110 return (CTL_ACTION_BLOCK);
11113 * Simple tags get blocked until all head of queue and ordered tags
11114 * ahead of them have completed. I'm lumping untagged commands in
11115 * with simple tags here. XXX KDM is that the right thing to do?
11117 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11118 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
11119 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
11120 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
11121 return (CTL_ACTION_BLOCK);
11123 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
11124 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
11126 serialize_row = ctl_serialize_table[ooa_entry->seridx];
11128 switch (serialize_row[pending_entry->seridx]) {
11129 case CTL_SER_BLOCK:
11130 return (CTL_ACTION_BLOCK);
11131 case CTL_SER_EXTENT:
11132 return (ctl_extent_check(ooa_io, pending_io,
11133 (lun->serseq == CTL_LUN_SERSEQ_ON)));
11134 case CTL_SER_EXTENTOPT:
11135 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
11136 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
11137 return (ctl_extent_check(ooa_io, pending_io,
11138 (lun->serseq == CTL_LUN_SERSEQ_ON)));
11139 return (CTL_ACTION_PASS);
11140 case CTL_SER_EXTENTSEQ:
11141 if (lun->serseq != CTL_LUN_SERSEQ_OFF)
11142 return (ctl_extent_check_seq(ooa_io, pending_io));
11143 return (CTL_ACTION_PASS);
11145 return (CTL_ACTION_PASS);
11146 case CTL_SER_BLOCKOPT:
11147 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
11148 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
11149 return (CTL_ACTION_BLOCK);
11150 return (CTL_ACTION_PASS);
11152 return (CTL_ACTION_SKIP);
11154 panic("invalid serialization value %d",
11155 serialize_row[pending_entry->seridx]);
11158 return (CTL_ACTION_ERROR);
11162 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
11164 * - pending_io is generally either incoming, or on the blocked queue
11165 * - starting I/O is the I/O we want to start the check with.
11168 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
11169 union ctl_io *starting_io)
11171 union ctl_io *ooa_io;
11174 mtx_assert(&lun->lun_lock, MA_OWNED);
11177 * Run back along the OOA queue, starting with the current
11178 * blocked I/O and going through every I/O before it on the
11179 * queue. If starting_io is NULL, we'll just end up returning
11182 for (ooa_io = starting_io; ooa_io != NULL;
11183 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
11187 * This routine just checks to see whether
11188 * cur_blocked is blocked by ooa_io, which is ahead
11189 * of it in the queue. It doesn't queue/dequeue
11192 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
11194 case CTL_ACTION_BLOCK:
11195 case CTL_ACTION_OVERLAP:
11196 case CTL_ACTION_OVERLAP_TAG:
11197 case CTL_ACTION_SKIP:
11198 case CTL_ACTION_ERROR:
11200 break; /* NOTREACHED */
11201 case CTL_ACTION_PASS:
11204 panic("invalid action %d", action);
11205 break; /* NOTREACHED */
11209 return (CTL_ACTION_PASS);
11214 * - An I/O has just completed, and has been removed from the per-LUN OOA
11215 * queue, so some items on the blocked queue may now be unblocked.
11218 ctl_check_blocked(struct ctl_lun *lun)
11220 union ctl_io *cur_blocked, *next_blocked;
11222 mtx_assert(&lun->lun_lock, MA_OWNED);
11225 * Run forward from the head of the blocked queue, checking each
11226 * entry against the I/Os prior to it on the OOA queue to see if
11227 * there is still any blockage.
11229 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
11230 * with our removing a variable on it while it is traversing the
11233 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
11234 cur_blocked != NULL; cur_blocked = next_blocked) {
11235 union ctl_io *prev_ooa;
11238 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
11241 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
11242 ctl_ooaq, ooa_links);
11245 * If cur_blocked happens to be the first item in the OOA
11246 * queue now, prev_ooa will be NULL, and the action
11247 * returned will just be CTL_ACTION_PASS.
11249 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
11252 case CTL_ACTION_BLOCK:
11253 /* Nothing to do here, still blocked */
11255 case CTL_ACTION_OVERLAP:
11256 case CTL_ACTION_OVERLAP_TAG:
11258 * This shouldn't happen! In theory we've already
11259 * checked this command for overlap...
11262 case CTL_ACTION_PASS:
11263 case CTL_ACTION_SKIP: {
11264 const struct ctl_cmd_entry *entry;
11268 * The skip case shouldn't happen, this transaction
11269 * should have never made it onto the blocked queue.
11272 * This I/O is no longer blocked, we can remove it
11273 * from the blocked queue. Since this is a TAILQ
11274 * (doubly linked list), we can do O(1) removals
11275 * from any place on the list.
11277 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
11279 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11281 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){
11283 * Need to send IO back to original side to
11286 union ctl_ha_msg msg_info;
11288 msg_info.hdr.original_sc =
11289 cur_blocked->io_hdr.original_sc;
11290 msg_info.hdr.serializing_sc = cur_blocked;
11291 msg_info.hdr.msg_type = CTL_MSG_R2R;
11292 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11293 &msg_info, sizeof(msg_info), 0)) >
11294 CTL_HA_STATUS_SUCCESS) {
11295 printf("CTL:Check Blocked error from "
11296 "ctl_ha_msg_send %d\n",
11301 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
11304 * Check this I/O for LUN state changes that may
11305 * have happened while this command was blocked.
11306 * The LUN state may have been changed by a command
11307 * ahead of us in the queue, so we need to re-check
11308 * for any states that can be caused by SCSI
11311 if (ctl_scsiio_lun_check(lun, entry,
11312 &cur_blocked->scsiio) == 0) {
11313 cur_blocked->io_hdr.flags |=
11314 CTL_FLAG_IS_WAS_ON_RTR;
11315 ctl_enqueue_rtr(cur_blocked);
11317 ctl_done(cur_blocked);
11322 * This probably shouldn't happen -- we shouldn't
11323 * get CTL_ACTION_ERROR, or anything else.
11329 return (CTL_RETVAL_COMPLETE);
11333 * This routine (with one exception) checks LUN flags that can be set by
11334 * commands ahead of us in the OOA queue. These flags have to be checked
11335 * when a command initially comes in, and when we pull a command off the
11336 * blocked queue and are preparing to execute it. The reason we have to
11337 * check these flags for commands on the blocked queue is that the LUN
11338 * state may have been changed by a command ahead of us while we're on the
11341 * Ordering is somewhat important with these checks, so please pay
11342 * careful attention to the placement of any new checks.
11345 ctl_scsiio_lun_check(struct ctl_lun *lun,
11346 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11348 struct ctl_softc *softc = lun->ctl_softc;
11354 mtx_assert(&lun->lun_lock, MA_OWNED);
11357 * If this shelf is a secondary shelf controller, we have to reject
11358 * any media access commands.
11360 if ((softc->flags & CTL_FLAG_ACTIVE_SHELF) == 0 &&
11361 (entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0) {
11362 ctl_set_lun_standby(ctsio);
11367 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11368 if (lun->flags & CTL_LUN_READONLY) {
11369 ctl_set_sense(ctsio, /*current_error*/ 1,
11370 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11371 /*asc*/ 0x27, /*ascq*/ 0x01, SSD_ELEM_NONE);
11375 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT]
11376 .eca_and_aen & SCP_SWP) != 0) {
11377 ctl_set_sense(ctsio, /*current_error*/ 1,
11378 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11379 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11386 * Check for a reservation conflict. If this command isn't allowed
11387 * even on reserved LUNs, and if this initiator isn't the one who
11388 * reserved us, reject the command with a reservation conflict.
11390 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
11391 if ((lun->flags & CTL_LUN_RESERVED)
11392 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11393 if (lun->res_idx != residx) {
11394 ctl_set_reservation_conflict(ctsio);
11400 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11401 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11402 /* No reservation or command is allowed. */;
11403 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11404 (lun->res_type == SPR_TYPE_WR_EX ||
11405 lun->res_type == SPR_TYPE_WR_EX_RO ||
11406 lun->res_type == SPR_TYPE_WR_EX_AR)) {
11407 /* The command is allowed for Write Exclusive resv. */;
11410 * if we aren't registered or it's a res holder type
11411 * reservation and this isn't the res holder then set a
11414 if (ctl_get_prkey(lun, residx) == 0
11415 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
11416 ctl_set_reservation_conflict(ctsio);
11423 if ((lun->flags & CTL_LUN_OFFLINE)
11424 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) {
11425 ctl_set_lun_not_ready(ctsio);
11431 * If the LUN is stopped, see if this particular command is allowed
11432 * for a stopped lun. Otherwise, reject it with 0x04,0x02.
11434 if ((lun->flags & CTL_LUN_STOPPED)
11435 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
11436 /* "Logical unit not ready, initializing cmd. required" */
11437 ctl_set_lun_stopped(ctsio);
11442 if ((lun->flags & CTL_LUN_INOPERABLE)
11443 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
11444 /* "Medium format corrupted" */
11445 ctl_set_medium_format_corrupted(ctsio);
11456 ctl_failover_io(union ctl_io *io, int have_lock)
11458 ctl_set_busy(&io->scsiio);
11465 struct ctl_lun *lun;
11466 struct ctl_softc *softc;
11467 union ctl_io *next_io, *pending_io;
11471 softc = control_softc;
11473 mtx_lock(&softc->ctl_lock);
11475 * Remove any cmds from the other SC from the rtr queue. These
11476 * will obviously only be for LUNs for which we're the primary.
11477 * We can't send status or get/send data for these commands.
11478 * Since they haven't been executed yet, we can just remove them.
11479 * We'll either abort them or delete them below, depending on
11480 * which HA mode we're in.
11483 mtx_lock(&softc->queue_lock);
11484 for (io = (union ctl_io *)STAILQ_FIRST(&softc->rtr_queue);
11485 io != NULL; io = next_io) {
11486 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
11487 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
11488 STAILQ_REMOVE(&softc->rtr_queue, &io->io_hdr,
11489 ctl_io_hdr, links);
11491 mtx_unlock(&softc->queue_lock);
11494 for (lun_idx=0; lun_idx < softc->num_luns; lun_idx++) {
11495 lun = softc->ctl_luns[lun_idx];
11500 * Processor LUNs are primary on both sides.
11501 * XXX will this always be true?
11503 if (lun->be_lun->lun_type == T_PROCESSOR)
11506 if ((lun->flags & CTL_LUN_PRIMARY_SC)
11507 && (softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
11508 printf("FAILOVER: primary lun %d\n", lun_idx);
11510 * Remove all commands from the other SC. First from the
11511 * blocked queue then from the ooa queue. Once we have
11512 * removed them. Call ctl_check_blocked to see if there
11513 * is anything that can run.
11515 for (io = (union ctl_io *)TAILQ_FIRST(
11516 &lun->blocked_queue); io != NULL; io = next_io) {
11518 next_io = (union ctl_io *)TAILQ_NEXT(
11519 &io->io_hdr, blocked_links);
11521 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
11522 TAILQ_REMOVE(&lun->blocked_queue,
11523 &io->io_hdr,blocked_links);
11524 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11525 TAILQ_REMOVE(&lun->ooa_queue,
11526 &io->io_hdr, ooa_links);
11532 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
11533 io != NULL; io = next_io) {
11535 next_io = (union ctl_io *)TAILQ_NEXT(
11536 &io->io_hdr, ooa_links);
11538 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
11540 TAILQ_REMOVE(&lun->ooa_queue,
11547 ctl_check_blocked(lun);
11548 } else if ((lun->flags & CTL_LUN_PRIMARY_SC)
11549 && (softc->ha_mode == CTL_HA_MODE_XFER)) {
11551 printf("FAILOVER: primary lun %d\n", lun_idx);
11553 * Abort all commands from the other SC. We can't
11554 * send status back for them now. These should get
11555 * cleaned up when they are completed or come out
11556 * for a datamove operation.
11558 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
11559 io != NULL; io = next_io) {
11560 next_io = (union ctl_io *)TAILQ_NEXT(
11561 &io->io_hdr, ooa_links);
11563 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
11564 io->io_hdr.flags |= CTL_FLAG_ABORT;
11566 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
11567 && (softc->ha_mode == CTL_HA_MODE_XFER)) {
11569 printf("FAILOVER: secondary lun %d\n", lun_idx);
11571 lun->flags |= CTL_LUN_PRIMARY_SC;
11574 * We send all I/O that was sent to this controller
11575 * and redirected to the other side back with
11576 * busy status, and have the initiator retry it.
11577 * Figuring out how much data has been transferred,
11578 * etc. and picking up where we left off would be
11581 * XXX KDM need to remove I/O from the blocked
11584 for (pending_io = (union ctl_io *)TAILQ_FIRST(
11585 &lun->ooa_queue); pending_io != NULL;
11586 pending_io = next_io) {
11588 next_io = (union ctl_io *)TAILQ_NEXT(
11589 &pending_io->io_hdr, ooa_links);
11591 pending_io->io_hdr.flags &=
11592 ~CTL_FLAG_SENT_2OTHER_SC;
11594 if (pending_io->io_hdr.flags &
11595 CTL_FLAG_IO_ACTIVE) {
11596 pending_io->io_hdr.flags |=
11599 ctl_set_busy(&pending_io->scsiio);
11600 ctl_done(pending_io);
11604 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
11605 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
11606 && (softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
11607 printf("FAILOVER: secondary lun %d\n", lun_idx);
11609 * if the first io on the OOA is not on the RtR queue
11612 lun->flags |= CTL_LUN_PRIMARY_SC;
11614 pending_io = (union ctl_io *)TAILQ_FIRST(
11616 if (pending_io==NULL) {
11617 printf("Nothing on OOA queue\n");
11621 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11622 if ((pending_io->io_hdr.flags &
11623 CTL_FLAG_IS_WAS_ON_RTR) == 0) {
11624 pending_io->io_hdr.flags |=
11625 CTL_FLAG_IS_WAS_ON_RTR;
11626 ctl_enqueue_rtr(pending_io);
11631 printf("Tag 0x%04x is running\n",
11632 pending_io->scsiio.tag_num);
11636 next_io = (union ctl_io *)TAILQ_NEXT(
11637 &pending_io->io_hdr, ooa_links);
11638 for (pending_io=next_io; pending_io != NULL;
11639 pending_io = next_io) {
11640 pending_io->io_hdr.flags &=
11641 ~CTL_FLAG_SENT_2OTHER_SC;
11642 next_io = (union ctl_io *)TAILQ_NEXT(
11643 &pending_io->io_hdr, ooa_links);
11644 if (pending_io->io_hdr.flags &
11645 CTL_FLAG_IS_WAS_ON_RTR) {
11647 printf("Tag 0x%04x is running\n",
11648 pending_io->scsiio.tag_num);
11653 switch (ctl_check_ooa(lun, pending_io,
11654 (union ctl_io *)TAILQ_PREV(
11655 &pending_io->io_hdr, ctl_ooaq,
11658 case CTL_ACTION_BLOCK:
11659 TAILQ_INSERT_TAIL(&lun->blocked_queue,
11660 &pending_io->io_hdr,
11662 pending_io->io_hdr.flags |=
11665 case CTL_ACTION_PASS:
11666 case CTL_ACTION_SKIP:
11667 pending_io->io_hdr.flags |=
11668 CTL_FLAG_IS_WAS_ON_RTR;
11669 ctl_enqueue_rtr(pending_io);
11671 case CTL_ACTION_OVERLAP:
11672 ctl_set_overlapped_cmd(
11673 (struct ctl_scsiio *)pending_io);
11674 ctl_done(pending_io);
11676 case CTL_ACTION_OVERLAP_TAG:
11677 ctl_set_overlapped_tag(
11678 (struct ctl_scsiio *)pending_io,
11679 pending_io->scsiio.tag_num & 0xff);
11680 ctl_done(pending_io);
11682 case CTL_ACTION_ERROR:
11684 ctl_set_internal_failure(
11685 (struct ctl_scsiio *)pending_io,
11688 ctl_done(pending_io);
11693 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
11695 panic("Unhandled HA mode failover, LUN flags = %#x, "
11696 "ha_mode = #%x", lun->flags, softc->ha_mode);
11700 mtx_unlock(&softc->ctl_lock);
11704 ctl_clear_ua(struct ctl_softc *ctl_softc, uint32_t initidx,
11705 ctl_ua_type ua_type)
11707 struct ctl_lun *lun;
11710 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
11712 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) {
11713 mtx_lock(&lun->lun_lock);
11714 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
11716 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua_type;
11717 mtx_unlock(&lun->lun_lock);
11722 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio)
11724 struct ctl_lun *lun;
11725 const struct ctl_cmd_entry *entry;
11726 uint32_t initidx, targ_lun;
11733 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11734 if ((targ_lun < CTL_MAX_LUNS)
11735 && ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
11737 * If the LUN is invalid, pretend that it doesn't exist.
11738 * It will go away as soon as all pending I/O has been
11741 mtx_lock(&lun->lun_lock);
11742 if (lun->flags & CTL_LUN_DISABLED) {
11743 mtx_unlock(&lun->lun_lock);
11745 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11746 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11748 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
11749 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
11751 if (lun->be_lun->lun_type == T_PROCESSOR) {
11752 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV;
11756 * Every I/O goes into the OOA queue for a
11757 * particular LUN, and stays there until completion.
11760 if (TAILQ_EMPTY(&lun->ooa_queue)) {
11761 lun->idle_time += getsbinuptime() -
11765 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr,
11769 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11770 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11773 /* Get command entry and return error if it is unsuppotyed. */
11774 entry = ctl_validate_command(ctsio);
11775 if (entry == NULL) {
11777 mtx_unlock(&lun->lun_lock);
11781 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11782 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11785 * Check to see whether we can send this command to LUNs that don't
11786 * exist. This should pretty much only be the case for inquiry
11787 * and request sense. Further checks, below, really require having
11788 * a LUN, so we can't really check the command anymore. Just put
11789 * it on the rtr queue.
11792 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) {
11793 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11794 ctl_enqueue_rtr((union ctl_io *)ctsio);
11798 ctl_set_unsupported_lun(ctsio);
11799 ctl_done((union ctl_io *)ctsio);
11800 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11804 * Make sure we support this particular command on this LUN.
11805 * e.g., we don't support writes to the control LUN.
11807 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11808 mtx_unlock(&lun->lun_lock);
11809 ctl_set_invalid_opcode(ctsio);
11810 ctl_done((union ctl_io *)ctsio);
11815 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11819 * If we've got a request sense, it'll clear the contingent
11820 * allegiance condition. Otherwise, if we have a CA condition for
11821 * this initiator, clear it, because it sent down a command other
11822 * than request sense.
11824 if ((ctsio->cdb[0] != REQUEST_SENSE)
11825 && (ctl_is_set(lun->have_ca, initidx)))
11826 ctl_clear_mask(lun->have_ca, initidx);
11830 * If the command has this flag set, it handles its own unit
11831 * attention reporting, we shouldn't do anything. Otherwise we
11832 * check for any pending unit attentions, and send them back to the
11833 * initiator. We only do this when a command initially comes in,
11834 * not when we pull it off the blocked queue.
11836 * According to SAM-3, section 5.3.2, the order that things get
11837 * presented back to the host is basically unit attentions caused
11838 * by some sort of reset event, busy status, reservation conflicts
11839 * or task set full, and finally any other status.
11841 * One issue here is that some of the unit attentions we report
11842 * don't fall into the "reset" category (e.g. "reported luns data
11843 * has changed"). So reporting it here, before the reservation
11844 * check, may be technically wrong. I guess the only thing to do
11845 * would be to check for and report the reset events here, and then
11846 * check for the other unit attention types after we check for a
11847 * reservation conflict.
11849 * XXX KDM need to fix this
11851 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11852 ctl_ua_type ua_type;
11853 scsi_sense_data_type sense_format;
11855 if (lun->flags & CTL_LUN_SENSE_DESC)
11856 sense_format = SSD_TYPE_DESC;
11858 sense_format = SSD_TYPE_FIXED;
11860 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11862 if (ua_type != CTL_UA_NONE) {
11863 mtx_unlock(&lun->lun_lock);
11864 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11865 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11866 ctsio->sense_len = SSD_FULL_SIZE;
11867 ctl_done((union ctl_io *)ctsio);
11873 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11874 mtx_unlock(&lun->lun_lock);
11875 ctl_done((union ctl_io *)ctsio);
11880 * XXX CHD this is where we want to send IO to other side if
11881 * this LUN is secondary on this SC. We will need to make a copy
11882 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11883 * the copy we send as FROM_OTHER.
11884 * We also need to stuff the address of the original IO so we can
11885 * find it easily. Something similar will need be done on the other
11886 * side so when we are done we can find the copy.
11888 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11889 union ctl_ha_msg msg_info;
11892 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11894 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11895 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11897 printf("1. ctsio %p\n", ctsio);
11899 msg_info.hdr.serializing_sc = NULL;
11900 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11901 msg_info.scsi.tag_num = ctsio->tag_num;
11902 msg_info.scsi.tag_type = ctsio->tag_type;
11903 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11905 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11907 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11908 (void *)&msg_info, sizeof(msg_info), 0)) >
11909 CTL_HA_STATUS_SUCCESS) {
11910 printf("CTL:precheck, ctl_ha_msg_send returned %d\n",
11912 printf("CTL:opcode is %x\n", ctsio->cdb[0]);
11915 printf("CTL:Precheck sent msg, opcode is %x\n",opcode);
11920 * XXX KDM this I/O is off the incoming queue, but hasn't
11921 * been inserted on any other queue. We may need to come
11922 * up with a holding queue while we wait for serialization
11923 * so that we have an idea of what we're waiting for from
11926 mtx_unlock(&lun->lun_lock);
11930 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11931 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11932 ctl_ooaq, ooa_links))) {
11933 case CTL_ACTION_BLOCK:
11934 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11935 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11937 mtx_unlock(&lun->lun_lock);
11939 case CTL_ACTION_PASS:
11940 case CTL_ACTION_SKIP:
11941 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11942 mtx_unlock(&lun->lun_lock);
11943 ctl_enqueue_rtr((union ctl_io *)ctsio);
11945 case CTL_ACTION_OVERLAP:
11946 mtx_unlock(&lun->lun_lock);
11947 ctl_set_overlapped_cmd(ctsio);
11948 ctl_done((union ctl_io *)ctsio);
11950 case CTL_ACTION_OVERLAP_TAG:
11951 mtx_unlock(&lun->lun_lock);
11952 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11953 ctl_done((union ctl_io *)ctsio);
11955 case CTL_ACTION_ERROR:
11957 mtx_unlock(&lun->lun_lock);
11958 ctl_set_internal_failure(ctsio,
11960 /*retry_count*/ 0);
11961 ctl_done((union ctl_io *)ctsio);
11967 const struct ctl_cmd_entry *
11968 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11970 const struct ctl_cmd_entry *entry;
11971 int service_action;
11973 entry = &ctl_cmd_table[ctsio->cdb[0]];
11975 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11976 if (entry->flags & CTL_CMD_FLAG_SA5) {
11977 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11978 entry = &((const struct ctl_cmd_entry *)
11979 entry->execute)[service_action];
11984 const struct ctl_cmd_entry *
11985 ctl_validate_command(struct ctl_scsiio *ctsio)
11987 const struct ctl_cmd_entry *entry;
11991 entry = ctl_get_cmd_entry(ctsio, &sa);
11992 if (entry->execute == NULL) {
11994 ctl_set_invalid_field(ctsio,
12001 ctl_set_invalid_opcode(ctsio);
12002 ctl_done((union ctl_io *)ctsio);
12005 KASSERT(entry->length > 0,
12006 ("Not defined length for command 0x%02x/0x%02x",
12007 ctsio->cdb[0], ctsio->cdb[1]));
12008 for (i = 1; i < entry->length; i++) {
12009 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
12012 ctl_set_invalid_field(ctsio,
12017 /*bit*/ fls(diff) - 1);
12018 ctl_done((union ctl_io *)ctsio);
12025 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
12028 switch (lun_type) {
12030 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) &&
12031 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
12035 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) &&
12036 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
12046 ctl_scsiio(struct ctl_scsiio *ctsio)
12049 const struct ctl_cmd_entry *entry;
12051 retval = CTL_RETVAL_COMPLETE;
12053 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
12055 entry = ctl_get_cmd_entry(ctsio, NULL);
12058 * If this I/O has been aborted, just send it straight to
12059 * ctl_done() without executing it.
12061 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
12062 ctl_done((union ctl_io *)ctsio);
12067 * All the checks should have been handled by ctl_scsiio_precheck().
12068 * We should be clear now to just execute the I/O.
12070 retval = entry->execute(ctsio);
12077 * Since we only implement one target right now, a bus reset simply resets
12078 * our single target.
12081 ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io)
12083 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET));
12087 ctl_target_reset(struct ctl_softc *softc, union ctl_io *io,
12088 ctl_ua_type ua_type)
12090 struct ctl_lun *lun;
12093 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12094 union ctl_ha_msg msg_info;
12096 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
12097 msg_info.hdr.nexus = io->io_hdr.nexus;
12098 if (ua_type==CTL_UA_TARG_RESET)
12099 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
12101 msg_info.task.task_action = CTL_TASK_BUS_RESET;
12102 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12103 msg_info.hdr.original_sc = NULL;
12104 msg_info.hdr.serializing_sc = NULL;
12105 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12106 (void *)&msg_info, sizeof(msg_info), 0)) {
12111 mtx_lock(&softc->ctl_lock);
12112 STAILQ_FOREACH(lun, &softc->lun_list, links)
12113 retval += ctl_lun_reset(lun, io, ua_type);
12114 mtx_unlock(&softc->ctl_lock);
12120 * The LUN should always be set. The I/O is optional, and is used to
12121 * distinguish between I/Os sent by this initiator, and by other
12122 * initiators. We set unit attention for initiators other than this one.
12123 * SAM-3 is vague on this point. It does say that a unit attention should
12124 * be established for other initiators when a LUN is reset (see section
12125 * 5.7.3), but it doesn't specifically say that the unit attention should
12126 * be established for this particular initiator when a LUN is reset. Here
12127 * is the relevant text, from SAM-3 rev 8:
12129 * 5.7.2 When a SCSI initiator port aborts its own tasks
12131 * When a SCSI initiator port causes its own task(s) to be aborted, no
12132 * notification that the task(s) have been aborted shall be returned to
12133 * the SCSI initiator port other than the completion response for the
12134 * command or task management function action that caused the task(s) to
12135 * be aborted and notification(s) associated with related effects of the
12136 * action (e.g., a reset unit attention condition).
12138 * XXX KDM for now, we're setting unit attention for all initiators.
12141 ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
12151 mtx_lock(&lun->lun_lock);
12153 * Run through the OOA queue and abort each I/O.
12155 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12156 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12157 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
12161 * This version sets unit attention for every
12164 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12165 ctl_est_ua_all(lun, initidx, ua_type);
12167 ctl_est_ua_all(lun, -1, ua_type);
12171 * A reset (any kind, really) clears reservations established with
12172 * RESERVE/RELEASE. It does not clear reservations established
12173 * with PERSISTENT RESERVE OUT, but we don't support that at the
12174 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
12175 * reservations made with the RESERVE/RELEASE commands, because
12176 * those commands are obsolete in SPC-3.
12178 lun->flags &= ~CTL_LUN_RESERVED;
12181 for (i = 0; i < CTL_MAX_INITIATORS; i++)
12182 ctl_clear_mask(lun->have_ca, i);
12184 mtx_unlock(&lun->lun_lock);
12190 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
12195 mtx_assert(&lun->lun_lock, MA_OWNED);
12198 * Run through the OOA queue and attempt to find the given I/O.
12199 * The target port, initiator ID, tag type and tag number have to
12200 * match the values that we got from the initiator. If we have an
12201 * untagged command to abort, simply abort the first untagged command
12202 * we come to. We only allow one untagged command at a time of course.
12204 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12205 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12207 if ((targ_port == UINT32_MAX ||
12208 targ_port == xio->io_hdr.nexus.targ_port) &&
12209 (init_id == UINT32_MAX ||
12210 init_id == xio->io_hdr.nexus.initid.id)) {
12211 if (targ_port != xio->io_hdr.nexus.targ_port ||
12212 init_id != xio->io_hdr.nexus.initid.id)
12213 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
12214 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12215 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12216 union ctl_ha_msg msg_info;
12218 msg_info.hdr.nexus = xio->io_hdr.nexus;
12219 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12220 msg_info.task.tag_num = xio->scsiio.tag_num;
12221 msg_info.task.tag_type = xio->scsiio.tag_type;
12222 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12223 msg_info.hdr.original_sc = NULL;
12224 msg_info.hdr.serializing_sc = NULL;
12225 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12226 (void *)&msg_info, sizeof(msg_info), 0);
12233 ctl_abort_task_set(union ctl_io *io)
12235 struct ctl_softc *softc = control_softc;
12236 struct ctl_lun *lun;
12242 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12243 mtx_lock(&softc->ctl_lock);
12244 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL))
12245 lun = softc->ctl_luns[targ_lun];
12247 mtx_unlock(&softc->ctl_lock);
12251 mtx_lock(&lun->lun_lock);
12252 mtx_unlock(&softc->ctl_lock);
12253 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
12254 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12255 io->io_hdr.nexus.initid.id,
12256 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12257 } else { /* CTL_TASK_CLEAR_TASK_SET */
12258 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
12259 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12261 mtx_unlock(&lun->lun_lock);
12266 ctl_i_t_nexus_reset(union ctl_io *io)
12268 struct ctl_softc *softc = control_softc;
12269 struct ctl_lun *lun;
12270 uint32_t initidx, residx;
12272 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12273 residx = ctl_get_resindex(&io->io_hdr.nexus);
12274 mtx_lock(&softc->ctl_lock);
12275 STAILQ_FOREACH(lun, &softc->lun_list, links) {
12276 mtx_lock(&lun->lun_lock);
12277 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12278 io->io_hdr.nexus.initid.id,
12279 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12281 ctl_clear_mask(lun->have_ca, initidx);
12283 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
12284 lun->flags &= ~CTL_LUN_RESERVED;
12285 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS);
12286 mtx_unlock(&lun->lun_lock);
12288 mtx_unlock(&softc->ctl_lock);
12293 ctl_abort_task(union ctl_io *io)
12296 struct ctl_lun *lun;
12297 struct ctl_softc *softc;
12300 char printbuf[128];
12305 softc = control_softc;
12311 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12312 mtx_lock(&softc->ctl_lock);
12313 if ((targ_lun < CTL_MAX_LUNS)
12314 && (softc->ctl_luns[targ_lun] != NULL))
12315 lun = softc->ctl_luns[targ_lun];
12317 mtx_unlock(&softc->ctl_lock);
12322 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
12323 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
12326 mtx_lock(&lun->lun_lock);
12327 mtx_unlock(&softc->ctl_lock);
12329 * Run through the OOA queue and attempt to find the given I/O.
12330 * The target port, initiator ID, tag type and tag number have to
12331 * match the values that we got from the initiator. If we have an
12332 * untagged command to abort, simply abort the first untagged command
12333 * we come to. We only allow one untagged command at a time of course.
12335 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12336 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12338 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
12340 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
12341 lun->lun, xio->scsiio.tag_num,
12342 xio->scsiio.tag_type,
12343 (xio->io_hdr.blocked_links.tqe_prev
12344 == NULL) ? "" : " BLOCKED",
12345 (xio->io_hdr.flags &
12346 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
12347 (xio->io_hdr.flags &
12348 CTL_FLAG_ABORT) ? " ABORT" : "",
12349 (xio->io_hdr.flags &
12350 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
12351 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
12353 printf("%s\n", sbuf_data(&sb));
12356 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12357 || (xio->io_hdr.nexus.initid.id != io->io_hdr.nexus.initid.id)
12358 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12362 * If the abort says that the task is untagged, the
12363 * task in the queue must be untagged. Otherwise,
12364 * we just check to see whether the tag numbers
12365 * match. This is because the QLogic firmware
12366 * doesn't pass back the tag type in an abort
12370 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12371 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12372 || (xio->scsiio.tag_num == io->taskio.tag_num))
12375 * XXX KDM we've got problems with FC, because it
12376 * doesn't send down a tag type with aborts. So we
12377 * can only really go by the tag number...
12378 * This may cause problems with parallel SCSI.
12379 * Need to figure that out!!
12381 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12382 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12384 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
12385 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12386 union ctl_ha_msg msg_info;
12388 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
12389 msg_info.hdr.nexus = io->io_hdr.nexus;
12390 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12391 msg_info.task.tag_num = io->taskio.tag_num;
12392 msg_info.task.tag_type = io->taskio.tag_type;
12393 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12394 msg_info.hdr.original_sc = NULL;
12395 msg_info.hdr.serializing_sc = NULL;
12397 printf("Sent Abort to other side\n");
12399 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12400 (void *)&msg_info, sizeof(msg_info), 0) !=
12401 CTL_HA_STATUS_SUCCESS) {
12405 printf("ctl_abort_task: found I/O to abort\n");
12409 mtx_unlock(&lun->lun_lock);
12413 * This isn't really an error. It's entirely possible for
12414 * the abort and command completion to cross on the wire.
12415 * This is more of an informative/diagnostic error.
12418 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
12419 "%d:%d:%d:%d tag %d type %d\n",
12420 io->io_hdr.nexus.initid.id,
12421 io->io_hdr.nexus.targ_port,
12422 io->io_hdr.nexus.targ_target.id,
12423 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
12424 io->taskio.tag_type);
12431 ctl_run_task(union ctl_io *io)
12433 struct ctl_softc *softc = control_softc;
12435 const char *task_desc;
12437 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12439 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12440 ("ctl_run_task: Unextected io_type %d\n",
12441 io->io_hdr.io_type));
12443 task_desc = ctl_scsi_task_string(&io->taskio);
12444 if (task_desc != NULL) {
12446 csevent_log(CSC_CTL | CSC_SHELF_SW |
12448 csevent_LogType_Trace,
12449 csevent_Severity_Information,
12450 csevent_AlertLevel_Green,
12451 csevent_FRU_Firmware,
12452 csevent_FRU_Unknown,
12453 "CTL: received task: %s",task_desc);
12457 csevent_log(CSC_CTL | CSC_SHELF_SW |
12459 csevent_LogType_Trace,
12460 csevent_Severity_Information,
12461 csevent_AlertLevel_Green,
12462 csevent_FRU_Firmware,
12463 csevent_FRU_Unknown,
12464 "CTL: received unknown task "
12466 io->taskio.task_action,
12467 io->taskio.task_action);
12470 switch (io->taskio.task_action) {
12471 case CTL_TASK_ABORT_TASK:
12472 retval = ctl_abort_task(io);
12474 case CTL_TASK_ABORT_TASK_SET:
12475 case CTL_TASK_CLEAR_TASK_SET:
12476 retval = ctl_abort_task_set(io);
12478 case CTL_TASK_CLEAR_ACA:
12480 case CTL_TASK_I_T_NEXUS_RESET:
12481 retval = ctl_i_t_nexus_reset(io);
12483 case CTL_TASK_LUN_RESET: {
12484 struct ctl_lun *lun;
12487 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12488 mtx_lock(&softc->ctl_lock);
12489 if ((targ_lun < CTL_MAX_LUNS)
12490 && (softc->ctl_luns[targ_lun] != NULL))
12491 lun = softc->ctl_luns[targ_lun];
12493 mtx_unlock(&softc->ctl_lock);
12498 if (!(io->io_hdr.flags &
12499 CTL_FLAG_FROM_OTHER_SC)) {
12500 union ctl_ha_msg msg_info;
12502 io->io_hdr.flags |=
12503 CTL_FLAG_SENT_2OTHER_SC;
12504 msg_info.hdr.msg_type =
12505 CTL_MSG_MANAGE_TASKS;
12506 msg_info.hdr.nexus = io->io_hdr.nexus;
12507 msg_info.task.task_action =
12508 CTL_TASK_LUN_RESET;
12509 msg_info.hdr.original_sc = NULL;
12510 msg_info.hdr.serializing_sc = NULL;
12511 if (CTL_HA_STATUS_SUCCESS !=
12512 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12514 sizeof(msg_info), 0)) {
12518 retval = ctl_lun_reset(lun, io,
12520 mtx_unlock(&softc->ctl_lock);
12523 case CTL_TASK_TARGET_RESET:
12524 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET);
12526 case CTL_TASK_BUS_RESET:
12527 retval = ctl_bus_reset(softc, io);
12529 case CTL_TASK_PORT_LOGIN:
12531 case CTL_TASK_PORT_LOGOUT:
12534 printf("ctl_run_task: got unknown task management event %d\n",
12535 io->taskio.task_action);
12539 io->io_hdr.status = CTL_SUCCESS;
12541 io->io_hdr.status = CTL_ERROR;
12546 * For HA operation. Handle commands that come in from the other
12550 ctl_handle_isc(union ctl_io *io)
12553 struct ctl_lun *lun;
12554 struct ctl_softc *softc;
12557 softc = control_softc;
12559 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12560 lun = softc->ctl_luns[targ_lun];
12562 switch (io->io_hdr.msg_type) {
12563 case CTL_MSG_SERIALIZE:
12564 free_io = ctl_serialize_other_sc_cmd(&io->scsiio);
12566 case CTL_MSG_R2R: {
12567 const struct ctl_cmd_entry *entry;
12570 * This is only used in SER_ONLY mode.
12573 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12574 mtx_lock(&lun->lun_lock);
12575 if (ctl_scsiio_lun_check(lun,
12576 entry, (struct ctl_scsiio *)io) != 0) {
12577 mtx_unlock(&lun->lun_lock);
12581 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12582 mtx_unlock(&lun->lun_lock);
12583 ctl_enqueue_rtr(io);
12586 case CTL_MSG_FINISH_IO:
12587 if (softc->ha_mode == CTL_HA_MODE_XFER) {
12592 mtx_lock(&lun->lun_lock);
12593 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
12595 ctl_check_blocked(lun);
12596 mtx_unlock(&lun->lun_lock);
12599 case CTL_MSG_PERS_ACTION:
12600 ctl_hndl_per_res_out_on_other_sc(
12601 (union ctl_ha_msg *)&io->presio.pr_msg);
12604 case CTL_MSG_BAD_JUJU:
12608 case CTL_MSG_DATAMOVE:
12609 /* Only used in XFER mode */
12611 ctl_datamove_remote(io);
12613 case CTL_MSG_DATAMOVE_DONE:
12614 /* Only used in XFER mode */
12616 io->scsiio.be_move_done(io);
12620 printf("%s: Invalid message type %d\n",
12621 __func__, io->io_hdr.msg_type);
12631 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12632 * there is no match.
12634 static ctl_lun_error_pattern
12635 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12637 const struct ctl_cmd_entry *entry;
12638 ctl_lun_error_pattern filtered_pattern, pattern;
12640 pattern = desc->error_pattern;
12643 * XXX KDM we need more data passed into this function to match a
12644 * custom pattern, and we actually need to implement custom pattern
12647 if (pattern & CTL_LUN_PAT_CMD)
12648 return (CTL_LUN_PAT_CMD);
12650 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12651 return (CTL_LUN_PAT_ANY);
12653 entry = ctl_get_cmd_entry(ctsio, NULL);
12655 filtered_pattern = entry->pattern & pattern;
12658 * If the user requested specific flags in the pattern (e.g.
12659 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12662 * If the user did not specify any flags, it doesn't matter whether
12663 * or not the command supports the flags.
12665 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12666 (pattern & ~CTL_LUN_PAT_MASK))
12667 return (CTL_LUN_PAT_NONE);
12670 * If the user asked for a range check, see if the requested LBA
12671 * range overlaps with this command's LBA range.
12673 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12679 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12681 return (CTL_LUN_PAT_NONE);
12683 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12684 desc->lba_range.len, FALSE);
12686 * A "pass" means that the LBA ranges don't overlap, so
12687 * this doesn't match the user's range criteria.
12689 if (action == CTL_ACTION_PASS)
12690 return (CTL_LUN_PAT_NONE);
12693 return (filtered_pattern);
12697 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12699 struct ctl_error_desc *desc, *desc2;
12701 mtx_assert(&lun->lun_lock, MA_OWNED);
12703 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12704 ctl_lun_error_pattern pattern;
12706 * Check to see whether this particular command matches
12707 * the pattern in the descriptor.
12709 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12710 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12713 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12714 case CTL_LUN_INJ_ABORTED:
12715 ctl_set_aborted(&io->scsiio);
12717 case CTL_LUN_INJ_MEDIUM_ERR:
12718 ctl_set_medium_error(&io->scsiio);
12720 case CTL_LUN_INJ_UA:
12721 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12723 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12725 case CTL_LUN_INJ_CUSTOM:
12727 * We're assuming the user knows what he is doing.
12728 * Just copy the sense information without doing
12731 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12732 MIN(sizeof(desc->custom_sense),
12733 sizeof(io->scsiio.sense_data)));
12734 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12735 io->scsiio.sense_len = SSD_FULL_SIZE;
12736 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12738 case CTL_LUN_INJ_NONE:
12741 * If this is an error injection type we don't know
12742 * about, clear the continuous flag (if it is set)
12743 * so it will get deleted below.
12745 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12749 * By default, each error injection action is a one-shot
12751 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12754 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12760 #ifdef CTL_IO_DELAY
12762 ctl_datamove_timer_wakeup(void *arg)
12766 io = (union ctl_io *)arg;
12770 #endif /* CTL_IO_DELAY */
12773 ctl_datamove(union ctl_io *io)
12775 void (*fe_datamove)(union ctl_io *io);
12777 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12779 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12782 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12787 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12788 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12790 sbuf_cat(&sb, path_str);
12791 switch (io->io_hdr.io_type) {
12793 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12794 sbuf_printf(&sb, "\n");
12795 sbuf_cat(&sb, path_str);
12796 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12797 io->scsiio.tag_num, io->scsiio.tag_type);
12800 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12801 "Tag Type: %d\n", io->taskio.task_action,
12802 io->taskio.tag_num, io->taskio.tag_type);
12805 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12806 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12809 sbuf_cat(&sb, path_str);
12810 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12811 (intmax_t)time_uptime - io->io_hdr.start_time);
12813 printf("%s", sbuf_data(&sb));
12815 #endif /* CTL_TIME_IO */
12817 #ifdef CTL_IO_DELAY
12818 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12819 struct ctl_lun *lun;
12821 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12823 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12825 struct ctl_lun *lun;
12827 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12829 && (lun->delay_info.datamove_delay > 0)) {
12830 struct callout *callout;
12832 callout = (struct callout *)&io->io_hdr.timer_bytes;
12833 callout_init(callout, /*mpsafe*/ 1);
12834 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12835 callout_reset(callout,
12836 lun->delay_info.datamove_delay * hz,
12837 ctl_datamove_timer_wakeup, io);
12838 if (lun->delay_info.datamove_type ==
12839 CTL_DELAY_TYPE_ONESHOT)
12840 lun->delay_info.datamove_delay = 0;
12847 * This command has been aborted. Set the port status, so we fail
12850 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12851 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n",
12852 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id,
12853 io->io_hdr.nexus.targ_port,
12854 (uintmax_t)io->io_hdr.nexus.targ_target.id,
12855 io->io_hdr.nexus.targ_lun);
12856 io->io_hdr.port_status = 31337;
12858 * Note that the backend, in this case, will get the
12859 * callback in its context. In other cases it may get
12860 * called in the frontend's interrupt thread context.
12862 io->scsiio.be_move_done(io);
12866 /* Don't confuse frontend with zero length data move. */
12867 if (io->scsiio.kern_data_len == 0) {
12868 io->scsiio.be_move_done(io);
12873 * If we're in XFER mode and this I/O is from the other shelf
12874 * controller, we need to send the DMA to the other side to
12875 * actually transfer the data to/from the host. In serialize only
12876 * mode the transfer happens below CTL and ctl_datamove() is only
12877 * called on the machine that originally received the I/O.
12879 if ((control_softc->ha_mode == CTL_HA_MODE_XFER)
12880 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12881 union ctl_ha_msg msg;
12882 uint32_t sg_entries_sent;
12886 memset(&msg, 0, sizeof(msg));
12887 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
12888 msg.hdr.original_sc = io->io_hdr.original_sc;
12889 msg.hdr.serializing_sc = io;
12890 msg.hdr.nexus = io->io_hdr.nexus;
12891 msg.dt.flags = io->io_hdr.flags;
12893 * We convert everything into a S/G list here. We can't
12894 * pass by reference, only by value between controllers.
12895 * So we can't pass a pointer to the S/G list, only as many
12896 * S/G entries as we can fit in here. If it's possible for
12897 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
12898 * then we need to break this up into multiple transfers.
12900 if (io->scsiio.kern_sg_entries == 0) {
12901 msg.dt.kern_sg_entries = 1;
12903 * If this is in cached memory, flush the cache
12904 * before we send the DMA request to the other
12905 * controller. We want to do this in either the
12906 * read or the write case. The read case is
12907 * straightforward. In the write case, we want to
12908 * make sure nothing is in the local cache that
12909 * could overwrite the DMAed data.
12911 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12913 * XXX KDM use bus_dmamap_sync() here.
12918 * Convert to a physical address if this is a
12921 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
12922 msg.dt.sg_list[0].addr =
12923 io->scsiio.kern_data_ptr;
12926 * XXX KDM use busdma here!
12929 msg.dt.sg_list[0].addr = (void *)
12930 vtophys(io->scsiio.kern_data_ptr);
12934 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
12937 struct ctl_sg_entry *sgl;
12940 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
12941 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
12942 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12944 * XXX KDM use bus_dmamap_sync() here.
12949 msg.dt.kern_data_len = io->scsiio.kern_data_len;
12950 msg.dt.kern_total_len = io->scsiio.kern_total_len;
12951 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
12952 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
12953 msg.dt.sg_sequence = 0;
12956 * Loop until we've sent all of the S/G entries. On the
12957 * other end, we'll recompose these S/G entries into one
12958 * contiguous list before passing it to the
12960 for (sg_entries_sent = 0; sg_entries_sent <
12961 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) {
12962 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list)/
12963 sizeof(msg.dt.sg_list[0])),
12964 msg.dt.kern_sg_entries - sg_entries_sent);
12966 if (do_sg_copy != 0) {
12967 struct ctl_sg_entry *sgl;
12970 sgl = (struct ctl_sg_entry *)
12971 io->scsiio.kern_data_ptr;
12973 * If this is in cached memory, flush the cache
12974 * before we send the DMA request to the other
12975 * controller. We want to do this in either
12976 * the * read or the write case. The read
12977 * case is straightforward. In the write
12978 * case, we want to make sure nothing is
12979 * in the local cache that could overwrite
12983 for (i = sg_entries_sent, j = 0;
12984 i < msg.dt.cur_sg_entries; i++, j++) {
12985 if ((io->io_hdr.flags &
12986 CTL_FLAG_NO_DATASYNC) == 0) {
12988 * XXX KDM use bus_dmamap_sync()
12991 if ((io->io_hdr.flags &
12992 CTL_FLAG_BUS_ADDR) == 0) {
12994 * XXX KDM use busdma.
12997 msg.dt.sg_list[j].addr =(void *)
12998 vtophys(sgl[i].addr);
13001 msg.dt.sg_list[j].addr =
13004 msg.dt.sg_list[j].len = sgl[i].len;
13008 sg_entries_sent += msg.dt.cur_sg_entries;
13009 if (sg_entries_sent >= msg.dt.kern_sg_entries)
13010 msg.dt.sg_last = 1;
13012 msg.dt.sg_last = 0;
13015 * XXX KDM drop and reacquire the lock here?
13017 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13018 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
13020 * XXX do something here.
13024 msg.dt.sent_sg_entries = sg_entries_sent;
13026 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
13027 if (io->io_hdr.flags & CTL_FLAG_FAILOVER)
13028 ctl_failover_io(io, /*have_lock*/ 0);
13033 * Lookup the fe_datamove() function for this particular
13037 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
13044 ctl_send_datamove_done(union ctl_io *io, int have_lock)
13046 union ctl_ha_msg msg;
13049 memset(&msg, 0, sizeof(msg));
13051 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
13052 msg.hdr.original_sc = io;
13053 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
13054 msg.hdr.nexus = io->io_hdr.nexus;
13055 msg.hdr.status = io->io_hdr.status;
13056 msg.scsi.tag_num = io->scsiio.tag_num;
13057 msg.scsi.tag_type = io->scsiio.tag_type;
13058 msg.scsi.scsi_status = io->scsiio.scsi_status;
13059 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
13060 sizeof(io->scsiio.sense_data));
13061 msg.scsi.sense_len = io->scsiio.sense_len;
13062 msg.scsi.sense_residual = io->scsiio.sense_residual;
13063 msg.scsi.fetd_status = io->io_hdr.port_status;
13064 msg.scsi.residual = io->scsiio.residual;
13065 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
13067 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
13068 ctl_failover_io(io, /*have_lock*/ have_lock);
13072 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0);
13073 if (isc_status > CTL_HA_STATUS_SUCCESS) {
13074 /* XXX do something if this fails */
13080 * The DMA to the remote side is done, now we need to tell the other side
13081 * we're done so it can continue with its data movement.
13084 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
13090 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
13091 printf("%s: ISC DMA write failed with error %d", __func__,
13093 ctl_set_internal_failure(&io->scsiio,
13095 /*retry_count*/ rq->ret);
13098 ctl_dt_req_free(rq);
13101 * In this case, we had to malloc the memory locally. Free it.
13103 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
13105 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13106 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13109 * The data is in local and remote memory, so now we need to send
13110 * status (good or back) back to the other side.
13112 ctl_send_datamove_done(io, /*have_lock*/ 0);
13116 * We've moved the data from the host/controller into local memory. Now we
13117 * need to push it over to the remote controller's memory.
13120 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
13126 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
13127 ctl_datamove_remote_write_cb);
13133 ctl_datamove_remote_write(union ctl_io *io)
13136 void (*fe_datamove)(union ctl_io *io);
13139 * - Get the data from the host/HBA into local memory.
13140 * - DMA memory from the local controller to the remote controller.
13141 * - Send status back to the remote controller.
13144 retval = ctl_datamove_remote_sgl_setup(io);
13148 /* Switch the pointer over so the FETD knows what to do */
13149 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
13152 * Use a custom move done callback, since we need to send completion
13153 * back to the other controller, not to the backend on this side.
13155 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
13157 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
13166 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
13175 * In this case, we had to malloc the memory locally. Free it.
13177 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
13179 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13180 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13184 scsi_path_string(io, path_str, sizeof(path_str));
13185 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13186 sbuf_cat(&sb, path_str);
13187 scsi_command_string(&io->scsiio, NULL, &sb);
13188 sbuf_printf(&sb, "\n");
13189 sbuf_cat(&sb, path_str);
13190 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13191 io->scsiio.tag_num, io->scsiio.tag_type);
13192 sbuf_cat(&sb, path_str);
13193 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
13194 io->io_hdr.flags, io->io_hdr.status);
13196 printk("%s", sbuf_data(&sb));
13201 * The read is done, now we need to send status (good or bad) back
13202 * to the other side.
13204 ctl_send_datamove_done(io, /*have_lock*/ 0);
13210 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
13213 void (*fe_datamove)(union ctl_io *io);
13217 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
13218 printf("%s: ISC DMA read failed with error %d", __func__,
13220 ctl_set_internal_failure(&io->scsiio,
13222 /*retry_count*/ rq->ret);
13225 ctl_dt_req_free(rq);
13227 /* Switch the pointer over so the FETD knows what to do */
13228 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
13231 * Use a custom move done callback, since we need to send completion
13232 * back to the other controller, not to the backend on this side.
13234 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
13236 /* XXX KDM add checks like the ones in ctl_datamove? */
13238 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
13244 ctl_datamove_remote_sgl_setup(union ctl_io *io)
13246 struct ctl_sg_entry *local_sglist, *remote_sglist;
13247 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist;
13248 struct ctl_softc *softc;
13253 softc = control_softc;
13255 local_sglist = io->io_hdr.local_sglist;
13256 local_dma_sglist = io->io_hdr.local_dma_sglist;
13257 remote_sglist = io->io_hdr.remote_sglist;
13258 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
13260 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) {
13261 for (i = 0; i < io->scsiio.kern_sg_entries; i++) {
13262 local_sglist[i].len = remote_sglist[i].len;
13265 * XXX Detect the situation where the RS-level I/O
13266 * redirector on the other side has already read the
13267 * data off of the AOR RS on this side, and
13268 * transferred it to remote (mirror) memory on the
13269 * other side. Since we already have the data in
13270 * memory here, we just need to use it.
13272 * XXX KDM this can probably be removed once we
13273 * get the cache device code in and take the
13274 * current AOR implementation out.
13277 if ((remote_sglist[i].addr >=
13278 (void *)vtophys(softc->mirr->addr))
13279 && (remote_sglist[i].addr <
13280 ((void *)vtophys(softc->mirr->addr) +
13281 CacheMirrorOffset))) {
13282 local_sglist[i].addr = remote_sglist[i].addr -
13284 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13286 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE;
13288 local_sglist[i].addr = remote_sglist[i].addr +
13293 printf("%s: local %p, remote %p, len %d\n",
13294 __func__, local_sglist[i].addr,
13295 remote_sglist[i].addr, local_sglist[i].len);
13299 uint32_t len_to_go;
13302 * In this case, we don't have automatically allocated
13303 * memory for this I/O on this controller. This typically
13304 * happens with internal CTL I/O -- e.g. inquiry, mode
13305 * sense, etc. Anything coming from RAIDCore will have
13306 * a mirror area available.
13308 len_to_go = io->scsiio.kern_data_len;
13311 * Clear the no datasync flag, we have to use malloced
13314 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC;
13317 * The difficult thing here is that the size of the various
13318 * S/G segments may be different than the size from the
13319 * remote controller. That'll make it harder when DMAing
13320 * the data back to the other side.
13322 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) /
13323 sizeof(io->io_hdr.remote_sglist[0])) &&
13324 (len_to_go > 0); i++) {
13325 local_sglist[i].len = MIN(len_to_go, 131072);
13326 CTL_SIZE_8B(local_dma_sglist[i].len,
13327 local_sglist[i].len);
13328 local_sglist[i].addr =
13329 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK);
13331 local_dma_sglist[i].addr = local_sglist[i].addr;
13333 if (local_sglist[i].addr == NULL) {
13336 printf("malloc failed for %zd bytes!",
13337 local_dma_sglist[i].len);
13338 for (j = 0; j < i; j++) {
13339 free(local_sglist[j].addr, M_CTL);
13341 ctl_set_internal_failure(&io->scsiio,
13343 /*retry_count*/ 4857);
13345 goto bailout_error;
13348 /* XXX KDM do we need a sync here? */
13350 len_to_go -= local_sglist[i].len;
13353 * Reset the number of S/G entries accordingly. The
13354 * original number of S/G entries is available in
13357 io->scsiio.kern_sg_entries = i;
13360 printf("%s: kern_sg_entries = %d\n", __func__,
13361 io->scsiio.kern_sg_entries);
13362 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13363 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i,
13364 local_sglist[i].addr, local_sglist[i].len,
13365 local_dma_sglist[i].len);
13374 ctl_send_datamove_done(io, /*have_lock*/ 0);
13380 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
13381 ctl_ha_dt_cb callback)
13383 struct ctl_ha_dt_req *rq;
13384 struct ctl_sg_entry *remote_sglist, *local_sglist;
13385 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist;
13386 uint32_t local_used, remote_used, total_used;
13392 rq = ctl_dt_req_alloc();
13395 * If we failed to allocate the request, and if the DMA didn't fail
13396 * anyway, set busy status. This is just a resource allocation
13400 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE))
13401 ctl_set_busy(&io->scsiio);
13403 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
13406 ctl_dt_req_free(rq);
13409 * The data move failed. We need to return status back
13410 * to the other controller. No point in trying to DMA
13411 * data to the remote controller.
13414 ctl_send_datamove_done(io, /*have_lock*/ 0);
13421 local_sglist = io->io_hdr.local_sglist;
13422 local_dma_sglist = io->io_hdr.local_dma_sglist;
13423 remote_sglist = io->io_hdr.remote_sglist;
13424 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
13429 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) {
13430 rq->ret = CTL_HA_STATUS_SUCCESS;
13437 * Pull/push the data over the wire from/to the other controller.
13438 * This takes into account the possibility that the local and
13439 * remote sglists may not be identical in terms of the size of
13440 * the elements and the number of elements.
13442 * One fundamental assumption here is that the length allocated for
13443 * both the local and remote sglists is identical. Otherwise, we've
13444 * essentially got a coding error of some sort.
13446 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
13448 uint32_t cur_len, dma_length;
13451 rq->id = CTL_HA_DATA_CTL;
13452 rq->command = command;
13456 * Both pointers should be aligned. But it is possible
13457 * that the allocation length is not. They should both
13458 * also have enough slack left over at the end, though,
13459 * to round up to the next 8 byte boundary.
13461 cur_len = MIN(local_sglist[i].len - local_used,
13462 remote_sglist[j].len - remote_used);
13465 * In this case, we have a size issue and need to decrease
13466 * the size, except in the case where we actually have less
13467 * than 8 bytes left. In that case, we need to increase
13468 * the DMA length to get the last bit.
13470 if ((cur_len & 0x7) != 0) {
13471 if (cur_len > 0x7) {
13472 cur_len = cur_len - (cur_len & 0x7);
13473 dma_length = cur_len;
13475 CTL_SIZE_8B(dma_length, cur_len);
13479 dma_length = cur_len;
13482 * If we had to allocate memory for this I/O, instead of using
13483 * the non-cached mirror memory, we'll need to flush the cache
13484 * before trying to DMA to the other controller.
13486 * We could end up doing this multiple times for the same
13487 * segment if we have a larger local segment than remote
13488 * segment. That shouldn't be an issue.
13490 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
13492 * XXX KDM use bus_dmamap_sync() here.
13496 rq->size = dma_length;
13498 tmp_ptr = (uint8_t *)local_sglist[i].addr;
13499 tmp_ptr += local_used;
13501 /* Use physical addresses when talking to ISC hardware */
13502 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
13503 /* XXX KDM use busdma */
13505 rq->local = vtophys(tmp_ptr);
13508 rq->local = tmp_ptr;
13510 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
13511 tmp_ptr += remote_used;
13512 rq->remote = tmp_ptr;
13514 rq->callback = NULL;
13516 local_used += cur_len;
13517 if (local_used >= local_sglist[i].len) {
13522 remote_used += cur_len;
13523 if (remote_used >= remote_sglist[j].len) {
13527 total_used += cur_len;
13529 if (total_used >= io->scsiio.kern_data_len)
13530 rq->callback = callback;
13532 if ((rq->size & 0x7) != 0) {
13533 printf("%s: warning: size %d is not on 8b boundary\n",
13534 __func__, rq->size);
13536 if (((uintptr_t)rq->local & 0x7) != 0) {
13537 printf("%s: warning: local %p not on 8b boundary\n",
13538 __func__, rq->local);
13540 if (((uintptr_t)rq->remote & 0x7) != 0) {
13541 printf("%s: warning: remote %p not on 8b boundary\n",
13542 __func__, rq->local);
13545 printf("%s: %s: local %#x remote %#x size %d\n", __func__,
13546 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
13547 rq->local, rq->remote, rq->size);
13550 isc_ret = ctl_dt_single(rq);
13551 if (isc_ret == CTL_HA_STATUS_WAIT)
13554 if (isc_ret == CTL_HA_STATUS_DISCONNECT) {
13555 rq->ret = CTL_HA_STATUS_SUCCESS;
13569 ctl_datamove_remote_read(union ctl_io *io)
13575 * This will send an error to the other controller in the case of a
13578 retval = ctl_datamove_remote_sgl_setup(io);
13582 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
13583 ctl_datamove_remote_read_cb);
13585 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) {
13587 * Make sure we free memory if there was an error.. The
13588 * ctl_datamove_remote_xfer() function will send the
13589 * datamove done message, or call the callback with an
13590 * error if there is a problem.
13592 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13593 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13600 * Process a datamove request from the other controller. This is used for
13601 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
13602 * first. Once that is complete, the data gets DMAed into the remote
13603 * controller's memory. For reads, we DMA from the remote controller's
13604 * memory into our memory first, and then move it out to the FETD.
13607 ctl_datamove_remote(union ctl_io *io)
13609 struct ctl_softc *softc;
13611 softc = control_softc;
13613 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
13616 * Note that we look for an aborted I/O here, but don't do some of
13617 * the other checks that ctl_datamove() normally does.
13618 * We don't need to run the datamove delay code, since that should
13619 * have been done if need be on the other controller.
13621 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
13622 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__,
13623 io->scsiio.tag_num, io->io_hdr.nexus.initid.id,
13624 io->io_hdr.nexus.targ_port,
13625 io->io_hdr.nexus.targ_target.id,
13626 io->io_hdr.nexus.targ_lun);
13627 io->io_hdr.port_status = 31338;
13628 ctl_send_datamove_done(io, /*have_lock*/ 0);
13632 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) {
13633 ctl_datamove_remote_write(io);
13634 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){
13635 ctl_datamove_remote_read(io);
13637 union ctl_ha_msg msg;
13638 struct scsi_sense_data *sense;
13642 memset(&msg, 0, sizeof(msg));
13644 msg.hdr.msg_type = CTL_MSG_BAD_JUJU;
13645 msg.hdr.status = CTL_SCSI_ERROR;
13646 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
13648 retry_count = 4243;
13650 sense = &msg.scsi.sense_data;
13651 sks[0] = SSD_SCS_VALID;
13652 sks[1] = (retry_count >> 8) & 0xff;
13653 sks[2] = retry_count & 0xff;
13655 /* "Internal target failure" */
13656 scsi_set_sense_data(sense,
13657 /*sense_format*/ SSD_TYPE_NONE,
13658 /*current_error*/ 1,
13659 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
13662 /*type*/ SSD_ELEM_SKS,
13663 /*size*/ sizeof(sks),
13667 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
13668 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
13669 ctl_failover_io(io, /*have_lock*/ 1);
13673 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) >
13674 CTL_HA_STATUS_SUCCESS) {
13675 /* XXX KDM what to do if this fails? */
13683 ctl_process_done(union ctl_io *io)
13685 struct ctl_lun *lun;
13686 struct ctl_softc *softc = control_softc;
13687 void (*fe_done)(union ctl_io *io);
13688 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port);
13690 CTL_DEBUG_PRINT(("ctl_process_done\n"));
13692 fe_done = softc->ctl_ports[targ_port]->fe_done;
13695 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
13700 ctl_scsi_path_string(io, path_str, sizeof(path_str));
13701 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13703 sbuf_cat(&sb, path_str);
13704 switch (io->io_hdr.io_type) {
13706 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
13707 sbuf_printf(&sb, "\n");
13708 sbuf_cat(&sb, path_str);
13709 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13710 io->scsiio.tag_num, io->scsiio.tag_type);
13713 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
13714 "Tag Type: %d\n", io->taskio.task_action,
13715 io->taskio.tag_num, io->taskio.tag_type);
13718 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13719 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13722 sbuf_cat(&sb, path_str);
13723 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
13724 (intmax_t)time_uptime - io->io_hdr.start_time);
13726 printf("%s", sbuf_data(&sb));
13728 #endif /* CTL_TIME_IO */
13730 switch (io->io_hdr.io_type) {
13734 if (bootverbose || (ctl_debug & CTL_DEBUG_INFO))
13735 ctl_io_error_print(io, NULL);
13736 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
13740 return (CTL_RETVAL_COMPLETE);
13742 panic("ctl_process_done: invalid io type %d\n",
13743 io->io_hdr.io_type);
13744 break; /* NOTREACHED */
13747 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13749 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13750 io->io_hdr.nexus.targ_mapped_lun));
13754 mtx_lock(&lun->lun_lock);
13757 * Check to see if we have any errors to inject here. We only
13758 * inject errors for commands that don't already have errors set.
13760 if ((STAILQ_FIRST(&lun->error_list) != NULL) &&
13761 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
13762 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
13763 ctl_inject_error(lun, io);
13766 * XXX KDM how do we treat commands that aren't completed
13769 * XXX KDM should we also track I/O latency?
13771 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13772 io->io_hdr.io_type == CTL_IO_SCSI) {
13774 struct bintime cur_bt;
13778 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13780 type = CTL_STATS_READ;
13781 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13783 type = CTL_STATS_WRITE;
13785 type = CTL_STATS_NO_IO;
13787 lun->stats.ports[targ_port].bytes[type] +=
13788 io->scsiio.kern_total_len;
13789 lun->stats.ports[targ_port].operations[type]++;
13791 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
13792 &io->io_hdr.dma_bt);
13793 lun->stats.ports[targ_port].num_dmas[type] +=
13794 io->io_hdr.num_dmas;
13795 getbintime(&cur_bt);
13796 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
13797 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
13802 * Remove this from the OOA queue.
13804 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13806 if (TAILQ_EMPTY(&lun->ooa_queue))
13807 lun->last_busy = getsbinuptime();
13811 * Run through the blocked queue on this LUN and see if anything
13812 * has become unblocked, now that this transaction is done.
13814 ctl_check_blocked(lun);
13817 * If the LUN has been invalidated, free it if there is nothing
13818 * left on its OOA queue.
13820 if ((lun->flags & CTL_LUN_INVALID)
13821 && TAILQ_EMPTY(&lun->ooa_queue)) {
13822 mtx_unlock(&lun->lun_lock);
13823 mtx_lock(&softc->ctl_lock);
13825 mtx_unlock(&softc->ctl_lock);
13827 mtx_unlock(&lun->lun_lock);
13832 * If this command has been aborted, make sure we set the status
13833 * properly. The FETD is responsible for freeing the I/O and doing
13834 * whatever it needs to do to clean up its state.
13836 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13837 ctl_set_task_aborted(&io->scsiio);
13840 * If enabled, print command error status.
13841 * We don't print UAs unless debugging was enabled explicitly.
13844 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
13846 if (!bootverbose && (ctl_debug & CTL_DEBUG_INFO) == 0)
13848 if ((ctl_debug & CTL_DEBUG_INFO) == 0 &&
13849 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) &&
13850 (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) {
13851 int error_code, sense_key, asc, ascq;
13853 scsi_extract_sense_len(&io->scsiio.sense_data,
13854 io->scsiio.sense_len, &error_code, &sense_key,
13855 &asc, &ascq, /*show_errors*/ 0);
13856 if (sense_key == SSD_KEY_UNIT_ATTENTION)
13860 ctl_io_error_print(io, NULL);
13864 * Tell the FETD or the other shelf controller we're done with this
13865 * command. Note that only SCSI commands get to this point. Task
13866 * management commands are completed above.
13868 * We only send status to the other controller if we're in XFER
13869 * mode. In SER_ONLY mode, the I/O is done on the controller that
13870 * received the I/O (from CTL's perspective), and so the status is
13873 * XXX KDM if we hold the lock here, we could cause a deadlock
13874 * if the frontend comes back in in this context to queue
13877 if ((softc->ha_mode == CTL_HA_MODE_XFER)
13878 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
13879 union ctl_ha_msg msg;
13881 memset(&msg, 0, sizeof(msg));
13882 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13883 msg.hdr.original_sc = io->io_hdr.original_sc;
13884 msg.hdr.nexus = io->io_hdr.nexus;
13885 msg.hdr.status = io->io_hdr.status;
13886 msg.scsi.scsi_status = io->scsiio.scsi_status;
13887 msg.scsi.tag_num = io->scsiio.tag_num;
13888 msg.scsi.tag_type = io->scsiio.tag_type;
13889 msg.scsi.sense_len = io->scsiio.sense_len;
13890 msg.scsi.sense_residual = io->scsiio.sense_residual;
13891 msg.scsi.residual = io->scsiio.residual;
13892 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
13893 sizeof(io->scsiio.sense_data));
13895 * We copy this whether or not this is an I/O-related
13896 * command. Otherwise, we'd have to go and check to see
13897 * whether it's a read/write command, and it really isn't
13900 memcpy(&msg.scsi.lbalen,
13901 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
13902 sizeof(msg.scsi.lbalen));
13904 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13905 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
13906 /* XXX do something here */
13913 return (CTL_RETVAL_COMPLETE);
13918 * Front end should call this if it doesn't do autosense. When the request
13919 * sense comes back in from the initiator, we'll dequeue this and send it.
13922 ctl_queue_sense(union ctl_io *io)
13924 struct ctl_lun *lun;
13925 struct ctl_port *port;
13926 struct ctl_softc *softc;
13927 uint32_t initidx, targ_lun;
13929 softc = control_softc;
13931 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13934 * LUN lookup will likely move to the ctl_work_thread() once we
13935 * have our new queueing infrastructure (that doesn't put things on
13936 * a per-LUN queue initially). That is so that we can handle
13937 * things like an INQUIRY to a LUN that we don't have enabled. We
13938 * can't deal with that right now.
13940 mtx_lock(&softc->ctl_lock);
13943 * If we don't have a LUN for this, just toss the sense
13946 port = ctl_io_port(&ctsio->io_hdr);
13947 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13948 if ((targ_lun < CTL_MAX_LUNS)
13949 && (softc->ctl_luns[targ_lun] != NULL))
13950 lun = softc->ctl_luns[targ_lun];
13954 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13956 mtx_lock(&lun->lun_lock);
13958 * Already have CA set for this LUN...toss the sense information.
13960 if (ctl_is_set(lun->have_ca, initidx)) {
13961 mtx_unlock(&lun->lun_lock);
13965 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data,
13966 MIN(sizeof(lun->pending_sense[initidx]),
13967 sizeof(io->scsiio.sense_data)));
13968 ctl_set_mask(lun->have_ca, initidx);
13969 mtx_unlock(&lun->lun_lock);
13972 mtx_unlock(&softc->ctl_lock);
13976 return (CTL_RETVAL_COMPLETE);
13981 * Primary command inlet from frontend ports. All SCSI and task I/O
13982 * requests must go through this function.
13985 ctl_queue(union ctl_io *io)
13987 struct ctl_port *port;
13989 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13992 io->io_hdr.start_time = time_uptime;
13993 getbintime(&io->io_hdr.start_bt);
13994 #endif /* CTL_TIME_IO */
13996 /* Map FE-specific LUN ID into global one. */
13997 port = ctl_io_port(&io->io_hdr);
13998 io->io_hdr.nexus.targ_mapped_lun =
13999 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
14001 switch (io->io_hdr.io_type) {
14004 if (ctl_debug & CTL_DEBUG_CDB)
14006 ctl_enqueue_incoming(io);
14009 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
14013 return (CTL_RETVAL_COMPLETE);
14016 #ifdef CTL_IO_DELAY
14018 ctl_done_timer_wakeup(void *arg)
14022 io = (union ctl_io *)arg;
14025 #endif /* CTL_IO_DELAY */
14028 ctl_done(union ctl_io *io)
14032 * Enable this to catch duplicate completion issues.
14035 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
14036 printf("%s: type %d msg %d cdb %x iptl: "
14037 "%d:%d:%d:%d tag 0x%04x "
14038 "flag %#x status %x\n",
14040 io->io_hdr.io_type,
14041 io->io_hdr.msg_type,
14043 io->io_hdr.nexus.initid.id,
14044 io->io_hdr.nexus.targ_port,
14045 io->io_hdr.nexus.targ_target.id,
14046 io->io_hdr.nexus.targ_lun,
14047 (io->io_hdr.io_type ==
14049 io->taskio.tag_num :
14050 io->scsiio.tag_num,
14052 io->io_hdr.status);
14054 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
14058 * This is an internal copy of an I/O, and should not go through
14059 * the normal done processing logic.
14061 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
14065 * We need to send a msg to the serializing shelf to finish the IO
14066 * as well. We don't send a finish message to the other shelf if
14067 * this is a task management command. Task management commands
14068 * aren't serialized in the OOA queue, but rather just executed on
14069 * both shelf controllers for commands that originated on that
14072 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)
14073 && (io->io_hdr.io_type != CTL_IO_TASK)) {
14074 union ctl_ha_msg msg_io;
14076 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO;
14077 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc;
14078 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io,
14079 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) {
14081 /* continue on to finish IO */
14083 #ifdef CTL_IO_DELAY
14084 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
14085 struct ctl_lun *lun;
14087 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
14089 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
14091 struct ctl_lun *lun;
14093 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
14096 && (lun->delay_info.done_delay > 0)) {
14097 struct callout *callout;
14099 callout = (struct callout *)&io->io_hdr.timer_bytes;
14100 callout_init(callout, /*mpsafe*/ 1);
14101 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
14102 callout_reset(callout,
14103 lun->delay_info.done_delay * hz,
14104 ctl_done_timer_wakeup, io);
14105 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
14106 lun->delay_info.done_delay = 0;
14110 #endif /* CTL_IO_DELAY */
14112 ctl_enqueue_done(io);
14116 ctl_isc(struct ctl_scsiio *ctsio)
14118 struct ctl_lun *lun;
14121 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
14123 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0]));
14125 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n"));
14127 retval = lun->backend->data_submit((union ctl_io *)ctsio);
14134 ctl_work_thread(void *arg)
14136 struct ctl_thread *thr = (struct ctl_thread *)arg;
14137 struct ctl_softc *softc = thr->ctl_softc;
14141 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
14147 * We handle the queues in this order:
14149 * - done queue (to free up resources, unblock other commands)
14153 * If those queues are empty, we break out of the loop and
14156 mtx_lock(&thr->queue_lock);
14157 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
14159 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
14160 mtx_unlock(&thr->queue_lock);
14161 ctl_handle_isc(io);
14164 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
14166 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
14167 /* clear any blocked commands, call fe_done */
14168 mtx_unlock(&thr->queue_lock);
14169 retval = ctl_process_done(io);
14172 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
14174 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
14175 mtx_unlock(&thr->queue_lock);
14176 if (io->io_hdr.io_type == CTL_IO_TASK)
14179 ctl_scsiio_precheck(softc, &io->scsiio);
14182 if (!ctl_pause_rtr) {
14183 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
14185 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
14186 mtx_unlock(&thr->queue_lock);
14187 retval = ctl_scsiio(&io->scsiio);
14188 if (retval != CTL_RETVAL_COMPLETE)
14189 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
14194 /* Sleep until we have something to do. */
14195 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
14200 ctl_lun_thread(void *arg)
14202 struct ctl_softc *softc = (struct ctl_softc *)arg;
14203 struct ctl_be_lun *be_lun;
14206 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
14210 mtx_lock(&softc->ctl_lock);
14211 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
14212 if (be_lun != NULL) {
14213 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
14214 mtx_unlock(&softc->ctl_lock);
14215 ctl_create_lun(be_lun);
14219 /* Sleep until we have something to do. */
14220 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
14221 PDROP | PRIBIO, "-", 0);
14226 ctl_thresh_thread(void *arg)
14228 struct ctl_softc *softc = (struct ctl_softc *)arg;
14229 struct ctl_lun *lun;
14230 struct ctl_be_lun *be_lun;
14231 struct scsi_da_rw_recovery_page *rwpage;
14232 struct ctl_logical_block_provisioning_page *page;
14234 uint64_t thres, val;
14237 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
14240 mtx_lock(&softc->ctl_lock);
14241 STAILQ_FOREACH(lun, &softc->lun_list, links) {
14242 be_lun = lun->be_lun;
14243 if ((lun->flags & CTL_LUN_DISABLED) ||
14244 (lun->flags & CTL_LUN_OFFLINE) ||
14245 lun->backend->lun_attr == NULL)
14247 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT];
14248 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0)
14251 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT];
14252 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
14253 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
14255 thres = scsi_4btoul(page->descr[i].count);
14256 thres <<= CTL_LBP_EXPONENT;
14257 switch (page->descr[i].resource) {
14259 attr = "blocksavail";
14262 attr = "blocksused";
14265 attr = "poolblocksavail";
14268 attr = "poolblocksused";
14273 mtx_unlock(&softc->ctl_lock); // XXX
14274 val = lun->backend->lun_attr(
14275 lun->be_lun->be_lun, attr);
14276 mtx_lock(&softc->ctl_lock);
14277 if (val == UINT64_MAX)
14279 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
14280 == SLBPPD_ARMING_INC)
14281 e |= (val >= thres);
14283 e |= (val <= thres);
14285 mtx_lock(&lun->lun_lock);
14287 if (lun->lasttpt == 0 ||
14288 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
14289 lun->lasttpt = time_uptime;
14290 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
14294 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
14296 mtx_unlock(&lun->lun_lock);
14298 mtx_unlock(&softc->ctl_lock);
14299 pause("-", CTL_LBP_PERIOD * hz);
14304 ctl_enqueue_incoming(union ctl_io *io)
14306 struct ctl_softc *softc = control_softc;
14307 struct ctl_thread *thr;
14310 idx = (io->io_hdr.nexus.targ_port * 127 +
14311 io->io_hdr.nexus.initid.id) % worker_threads;
14312 thr = &softc->threads[idx];
14313 mtx_lock(&thr->queue_lock);
14314 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
14315 mtx_unlock(&thr->queue_lock);
14320 ctl_enqueue_rtr(union ctl_io *io)
14322 struct ctl_softc *softc = control_softc;
14323 struct ctl_thread *thr;
14325 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14326 mtx_lock(&thr->queue_lock);
14327 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
14328 mtx_unlock(&thr->queue_lock);
14333 ctl_enqueue_done(union ctl_io *io)
14335 struct ctl_softc *softc = control_softc;
14336 struct ctl_thread *thr;
14338 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14339 mtx_lock(&thr->queue_lock);
14340 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
14341 mtx_unlock(&thr->queue_lock);
14346 ctl_enqueue_isc(union ctl_io *io)
14348 struct ctl_softc *softc = control_softc;
14349 struct ctl_thread *thr;
14351 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14352 mtx_lock(&thr->queue_lock);
14353 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
14354 mtx_unlock(&thr->queue_lock);
14358 /* Initialization and failover */
14361 ctl_init_isc_msg(void)
14363 printf("CTL: Still calling this thing\n");
14368 * Initializes component into configuration defined by bootMode
14370 * returns hasc_Status:
14372 * ERROR - fatal error
14374 static ctl_ha_comp_status
14375 ctl_isc_init(struct ctl_ha_component *c)
14377 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
14384 * Starts component in state requested. If component starts successfully,
14385 * it must set its own state to the requestrd state
14386 * When requested state is HASC_STATE_HA, the component may refine it
14387 * by adding _SLAVE or _MASTER flags.
14388 * Currently allowed state transitions are:
14389 * UNKNOWN->HA - initial startup
14390 * UNKNOWN->SINGLE - initial startup when no parter detected
14391 * HA->SINGLE - failover
14392 * returns ctl_ha_comp_status:
14393 * OK - component successfully started in requested state
14394 * FAILED - could not start the requested state, failover may
14396 * ERROR - fatal error detected, no future startup possible
14398 static ctl_ha_comp_status
14399 ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state)
14401 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
14403 printf("%s: go\n", __func__);
14405 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap)
14406 if (c->state == CTL_HA_STATE_UNKNOWN ) {
14407 control_softc->is_single = 0;
14408 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
14409 != CTL_HA_STATUS_SUCCESS) {
14410 printf("ctl_isc_start: ctl_ha_msg_create failed.\n");
14411 ret = CTL_HA_COMP_STATUS_ERROR;
14413 } else if (CTL_HA_STATE_IS_HA(c->state)
14414 && CTL_HA_STATE_IS_SINGLE(state)){
14415 // HA->SINGLE transition
14417 control_softc->is_single = 1;
14419 printf("ctl_isc_start:Invalid state transition %X->%X\n",
14421 ret = CTL_HA_COMP_STATUS_ERROR;
14423 if (CTL_HA_STATE_IS_SINGLE(state))
14424 control_softc->is_single = 1;
14432 * Quiesce component
14433 * The component must clear any error conditions (set status to OK) and
14434 * prepare itself to another Start call
14435 * returns ctl_ha_comp_status:
14439 static ctl_ha_comp_status
14440 ctl_isc_quiesce(struct ctl_ha_component *c)
14442 int ret = CTL_HA_COMP_STATUS_OK;
14449 struct ctl_ha_component ctl_ha_component_ctlisc =
14452 .state = CTL_HA_STATE_UNKNOWN,
14453 .init = ctl_isc_init,
14454 .start = ctl_isc_start,
14455 .quiesce = ctl_isc_quiesce