2 * Copyright (c) 2003-2009 Silicon Graphics International Corp.
3 * Copyright (c) 2012 The FreeBSD Foundation
6 * Portions of this software were developed by Edward Tomasz Napierala
7 * under sponsorship from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
16 * substantially similar to the "NO WARRANTY" disclaimer below
17 * ("Disclaimer") and any redistribution must be conditioned upon
18 * including a substantially similar Disclaimer requirement for further
19 * binary redistribution.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGES.
34 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl.c#8 $
37 * CAM Target Layer, a SCSI device emulation subsystem.
39 * Author: Ken Merry <ken@FreeBSD.org>
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/ctype.h>
50 #include <sys/kernel.h>
51 #include <sys/types.h>
52 #include <sys/kthread.h>
54 #include <sys/fcntl.h>
56 #include <sys/module.h>
57 #include <sys/mutex.h>
58 #include <sys/condvar.h>
59 #include <sys/malloc.h>
61 #include <sys/ioccom.h>
62 #include <sys/queue.h>
65 #include <sys/endian.h>
66 #include <sys/sysctl.h>
70 #include <cam/scsi/scsi_all.h>
71 #include <cam/scsi/scsi_da.h>
72 #include <cam/ctl/ctl_io.h>
73 #include <cam/ctl/ctl.h>
74 #include <cam/ctl/ctl_frontend.h>
75 #include <cam/ctl/ctl_frontend_internal.h>
76 #include <cam/ctl/ctl_util.h>
77 #include <cam/ctl/ctl_backend.h>
78 #include <cam/ctl/ctl_ioctl.h>
79 #include <cam/ctl/ctl_ha.h>
80 #include <cam/ctl/ctl_private.h>
81 #include <cam/ctl/ctl_debug.h>
82 #include <cam/ctl/ctl_scsi_all.h>
83 #include <cam/ctl/ctl_error.h>
85 struct ctl_softc *control_softc = NULL;
88 * Size and alignment macros needed for Copan-specific HA hardware. These
89 * can go away when the HA code is re-written, and uses busdma for any
92 #define CTL_ALIGN_8B(target, source, type) \
93 if (((uint32_t)source & 0x7) != 0) \
94 target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\
96 target = (type)source;
98 #define CTL_SIZE_8B(target, size) \
99 if ((size & 0x7) != 0) \
100 target = size + (0x8 - (size & 0x7)); \
104 #define CTL_ALIGN_8B_MARGIN 16
107 * Template mode pages.
111 * Note that these are default values only. The actual values will be
112 * filled in when the user does a mode sense.
114 static struct copan_debugconf_subpage debugconf_page_default = {
115 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
116 DBGCNF_SUBPAGE_CODE, /* subpage */
117 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
118 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
119 DBGCNF_VERSION, /* page_version */
120 {CTL_TIME_IO_DEFAULT_SECS>>8,
121 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */
124 static struct copan_debugconf_subpage debugconf_page_changeable = {
125 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
126 DBGCNF_SUBPAGE_CODE, /* subpage */
127 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
128 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
129 0, /* page_version */
130 {0xff,0xff}, /* ctl_time_io_secs */
133 static struct scsi_da_rw_recovery_page rw_er_page_default = {
134 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
135 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
136 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE,
137 /*read_retry_count*/0,
138 /*correction_span*/0,
139 /*head_offset_count*/0,
140 /*data_strobe_offset_cnt*/0,
141 /*byte8*/SMS_RWER_LBPERE,
142 /*write_retry_count*/0,
144 /*recovery_time_limit*/{0, 0},
147 static struct scsi_da_rw_recovery_page rw_er_page_changeable = {
148 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
149 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
151 /*read_retry_count*/0,
152 /*correction_span*/0,
153 /*head_offset_count*/0,
154 /*data_strobe_offset_cnt*/0,
156 /*write_retry_count*/0,
158 /*recovery_time_limit*/{0, 0},
161 static struct scsi_format_page format_page_default = {
162 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
163 /*page_length*/sizeof(struct scsi_format_page) - 2,
164 /*tracks_per_zone*/ {0, 0},
165 /*alt_sectors_per_zone*/ {0, 0},
166 /*alt_tracks_per_zone*/ {0, 0},
167 /*alt_tracks_per_lun*/ {0, 0},
168 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
169 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
170 /*bytes_per_sector*/ {0, 0},
171 /*interleave*/ {0, 0},
172 /*track_skew*/ {0, 0},
173 /*cylinder_skew*/ {0, 0},
175 /*reserved*/ {0, 0, 0}
178 static struct scsi_format_page format_page_changeable = {
179 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
180 /*page_length*/sizeof(struct scsi_format_page) - 2,
181 /*tracks_per_zone*/ {0, 0},
182 /*alt_sectors_per_zone*/ {0, 0},
183 /*alt_tracks_per_zone*/ {0, 0},
184 /*alt_tracks_per_lun*/ {0, 0},
185 /*sectors_per_track*/ {0, 0},
186 /*bytes_per_sector*/ {0, 0},
187 /*interleave*/ {0, 0},
188 /*track_skew*/ {0, 0},
189 /*cylinder_skew*/ {0, 0},
191 /*reserved*/ {0, 0, 0}
194 static struct scsi_rigid_disk_page rigid_disk_page_default = {
195 /*page_code*/SMS_RIGID_DISK_PAGE,
196 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
197 /*cylinders*/ {0, 0, 0},
198 /*heads*/ CTL_DEFAULT_HEADS,
199 /*start_write_precomp*/ {0, 0, 0},
200 /*start_reduced_current*/ {0, 0, 0},
201 /*step_rate*/ {0, 0},
202 /*landing_zone_cylinder*/ {0, 0, 0},
203 /*rpl*/ SRDP_RPL_DISABLED,
204 /*rotational_offset*/ 0,
206 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
207 CTL_DEFAULT_ROTATION_RATE & 0xff},
211 static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
212 /*page_code*/SMS_RIGID_DISK_PAGE,
213 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
214 /*cylinders*/ {0, 0, 0},
216 /*start_write_precomp*/ {0, 0, 0},
217 /*start_reduced_current*/ {0, 0, 0},
218 /*step_rate*/ {0, 0},
219 /*landing_zone_cylinder*/ {0, 0, 0},
221 /*rotational_offset*/ 0,
223 /*rotation_rate*/ {0, 0},
227 static struct scsi_caching_page caching_page_default = {
228 /*page_code*/SMS_CACHING_PAGE,
229 /*page_length*/sizeof(struct scsi_caching_page) - 2,
230 /*flags1*/ SCP_DISC | SCP_WCE,
232 /*disable_pf_transfer_len*/ {0xff, 0xff},
233 /*min_prefetch*/ {0, 0},
234 /*max_prefetch*/ {0xff, 0xff},
235 /*max_pf_ceiling*/ {0xff, 0xff},
237 /*cache_segments*/ 0,
238 /*cache_seg_size*/ {0, 0},
240 /*non_cache_seg_size*/ {0, 0, 0}
243 static struct scsi_caching_page caching_page_changeable = {
244 /*page_code*/SMS_CACHING_PAGE,
245 /*page_length*/sizeof(struct scsi_caching_page) - 2,
246 /*flags1*/ SCP_WCE | SCP_RCD,
248 /*disable_pf_transfer_len*/ {0, 0},
249 /*min_prefetch*/ {0, 0},
250 /*max_prefetch*/ {0, 0},
251 /*max_pf_ceiling*/ {0, 0},
253 /*cache_segments*/ 0,
254 /*cache_seg_size*/ {0, 0},
256 /*non_cache_seg_size*/ {0, 0, 0}
259 static struct scsi_control_page control_page_default = {
260 /*page_code*/SMS_CONTROL_MODE_PAGE,
261 /*page_length*/sizeof(struct scsi_control_page) - 2,
263 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED,
266 /*aen_holdoff_period*/{0, 0},
267 /*busy_timeout_period*/{0, 0},
268 /*extended_selftest_completion_time*/{0, 0}
271 static struct scsi_control_page control_page_changeable = {
272 /*page_code*/SMS_CONTROL_MODE_PAGE,
273 /*page_length*/sizeof(struct scsi_control_page) - 2,
275 /*queue_flags*/SCP_QUEUE_ALG_MASK,
276 /*eca_and_aen*/SCP_SWP,
278 /*aen_holdoff_period*/{0, 0},
279 /*busy_timeout_period*/{0, 0},
280 /*extended_selftest_completion_time*/{0, 0}
283 static struct scsi_info_exceptions_page ie_page_default = {
284 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
285 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
286 /*info_flags*/SIEP_FLAGS_DEXCPT,
288 /*interval_timer*/{0, 0, 0, 0},
289 /*report_count*/{0, 0, 0, 0}
292 static struct scsi_info_exceptions_page ie_page_changeable = {
293 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
294 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
297 /*interval_timer*/{0, 0, 0, 0},
298 /*report_count*/{0, 0, 0, 0}
301 #define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4)
303 static struct ctl_logical_block_provisioning_page lbp_page_default = {{
304 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
305 /*subpage_code*/0x02,
306 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
308 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
313 /*count*/{0, 0, 0, 0}},
317 /*count*/{0, 0, 0, 0}},
321 /*count*/{0, 0, 0, 0}},
325 /*count*/{0, 0, 0, 0}}
329 static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{
330 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
331 /*subpage_code*/0x02,
332 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
334 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
339 /*count*/{0, 0, 0, 0}},
343 /*count*/{0, 0, 0, 0}},
347 /*count*/{0, 0, 0, 0}},
351 /*count*/{0, 0, 0, 0}}
356 * XXX KDM move these into the softc.
358 static int rcv_sync_msg;
359 static uint8_t ctl_pause_rtr;
361 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
362 static int worker_threads = -1;
363 TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads);
364 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
365 &worker_threads, 1, "Number of worker threads");
366 static int ctl_debug = CTL_DEBUG_NONE;
367 TUNABLE_INT("kern.cam.ctl.debug", &ctl_debug);
368 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN,
369 &ctl_debug, 0, "Enabled debug flags");
372 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
373 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
374 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0),
375 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
377 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 10
379 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
381 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
382 static int ctl_init(void);
383 void ctl_shutdown(void);
384 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
385 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
386 static void ctl_ioctl_online(void *arg);
387 static void ctl_ioctl_offline(void *arg);
388 static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id);
389 static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id);
390 static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio);
391 static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
392 static int ctl_ioctl_submit_wait(union ctl_io *io);
393 static void ctl_ioctl_datamove(union ctl_io *io);
394 static void ctl_ioctl_done(union ctl_io *io);
395 static void ctl_ioctl_hard_startstop_callback(void *arg,
396 struct cfi_metatask *metatask);
397 static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask);
398 static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
399 struct ctl_ooa *ooa_hdr,
400 struct ctl_ooa_entry *kern_entries);
401 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
403 static uint32_t ctl_map_lun(int port_num, uint32_t lun);
404 static uint32_t ctl_map_lun_back(int port_num, uint32_t lun);
405 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
406 struct ctl_be_lun *be_lun, struct ctl_id target_id);
407 static int ctl_free_lun(struct ctl_lun *lun);
408 static void ctl_create_lun(struct ctl_be_lun *be_lun);
410 static void ctl_failover_change_pages(struct ctl_softc *softc,
411 struct ctl_scsiio *ctsio, int master);
414 static int ctl_do_mode_select(union ctl_io *io);
415 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
416 uint64_t res_key, uint64_t sa_res_key,
417 uint8_t type, uint32_t residx,
418 struct ctl_scsiio *ctsio,
419 struct scsi_per_res_out *cdb,
420 struct scsi_per_res_out_parms* param);
421 static void ctl_pro_preempt_other(struct ctl_lun *lun,
422 union ctl_ha_msg *msg);
423 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
424 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
425 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
426 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
427 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
428 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
429 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
431 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
433 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
434 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
435 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
436 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
437 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
438 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2);
439 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
440 union ctl_io *pending_io, union ctl_io *ooa_io);
441 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
442 union ctl_io *starting_io);
443 static int ctl_check_blocked(struct ctl_lun *lun);
444 static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc,
446 const struct ctl_cmd_entry *entry,
447 struct ctl_scsiio *ctsio);
448 //static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc);
449 static void ctl_failover(void);
450 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
451 struct ctl_scsiio *ctsio);
452 static int ctl_scsiio(struct ctl_scsiio *ctsio);
454 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
455 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
456 ctl_ua_type ua_type);
457 static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io,
458 ctl_ua_type ua_type);
459 static int ctl_abort_task(union ctl_io *io);
460 static int ctl_abort_task_set(union ctl_io *io);
461 static int ctl_i_t_nexus_reset(union ctl_io *io);
462 static void ctl_run_task(union ctl_io *io);
464 static void ctl_datamove_timer_wakeup(void *arg);
465 static void ctl_done_timer_wakeup(void *arg);
466 #endif /* CTL_IO_DELAY */
468 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
469 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
470 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
471 static void ctl_datamove_remote_write(union ctl_io *io);
472 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
473 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
474 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
475 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
476 ctl_ha_dt_cb callback);
477 static void ctl_datamove_remote_read(union ctl_io *io);
478 static void ctl_datamove_remote(union ctl_io *io);
479 static int ctl_process_done(union ctl_io *io);
480 static void ctl_lun_thread(void *arg);
481 static void ctl_thresh_thread(void *arg);
482 static void ctl_work_thread(void *arg);
483 static void ctl_enqueue_incoming(union ctl_io *io);
484 static void ctl_enqueue_rtr(union ctl_io *io);
485 static void ctl_enqueue_done(union ctl_io *io);
486 static void ctl_enqueue_isc(union ctl_io *io);
487 static const struct ctl_cmd_entry *
488 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
489 static const struct ctl_cmd_entry *
490 ctl_validate_command(struct ctl_scsiio *ctsio);
491 static int ctl_cmd_applicable(uint8_t lun_type,
492 const struct ctl_cmd_entry *entry);
495 * Load the serialization table. This isn't very pretty, but is probably
496 * the easiest way to do it.
498 #include "ctl_ser_table.c"
501 * We only need to define open, close and ioctl routines for this driver.
503 static struct cdevsw ctl_cdevsw = {
504 .d_version = D_VERSION,
507 .d_close = ctl_close,
508 .d_ioctl = ctl_ioctl,
513 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
514 MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests");
516 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
518 static moduledata_t ctl_moduledata = {
520 ctl_module_event_handler,
524 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
525 MODULE_VERSION(ctl, 1);
527 static struct ctl_frontend ioctl_frontend =
533 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
534 union ctl_ha_msg *msg_info)
536 struct ctl_scsiio *ctsio;
538 if (msg_info->hdr.original_sc == NULL) {
539 printf("%s: original_sc == NULL!\n", __func__);
540 /* XXX KDM now what? */
544 ctsio = &msg_info->hdr.original_sc->scsiio;
545 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
546 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
547 ctsio->io_hdr.status = msg_info->hdr.status;
548 ctsio->scsi_status = msg_info->scsi.scsi_status;
549 ctsio->sense_len = msg_info->scsi.sense_len;
550 ctsio->sense_residual = msg_info->scsi.sense_residual;
551 ctsio->residual = msg_info->scsi.residual;
552 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
553 sizeof(ctsio->sense_data));
554 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
555 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen));
556 ctl_enqueue_isc((union ctl_io *)ctsio);
560 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
561 union ctl_ha_msg *msg_info)
563 struct ctl_scsiio *ctsio;
565 if (msg_info->hdr.serializing_sc == NULL) {
566 printf("%s: serializing_sc == NULL!\n", __func__);
567 /* XXX KDM now what? */
571 ctsio = &msg_info->hdr.serializing_sc->scsiio;
574 * Attempt to catch the situation where an I/O has
575 * been freed, and we're using it again.
577 if (ctsio->io_hdr.io_type == 0xff) {
578 union ctl_io *tmp_io;
579 tmp_io = (union ctl_io *)ctsio;
580 printf("%s: %p use after free!\n", __func__,
582 printf("%s: type %d msg %d cdb %x iptl: "
583 "%d:%d:%d:%d tag 0x%04x "
584 "flag %#x status %x\n",
586 tmp_io->io_hdr.io_type,
587 tmp_io->io_hdr.msg_type,
588 tmp_io->scsiio.cdb[0],
589 tmp_io->io_hdr.nexus.initid.id,
590 tmp_io->io_hdr.nexus.targ_port,
591 tmp_io->io_hdr.nexus.targ_target.id,
592 tmp_io->io_hdr.nexus.targ_lun,
593 (tmp_io->io_hdr.io_type ==
595 tmp_io->taskio.tag_num :
596 tmp_io->scsiio.tag_num,
597 tmp_io->io_hdr.flags,
598 tmp_io->io_hdr.status);
601 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
602 ctl_enqueue_isc((union ctl_io *)ctsio);
606 * ISC (Inter Shelf Communication) event handler. Events from the HA
607 * subsystem come in here.
610 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
612 struct ctl_softc *ctl_softc;
614 struct ctl_prio *presio;
615 ctl_ha_status isc_status;
617 ctl_softc = control_softc;
622 printf("CTL: Isc Msg event %d\n", event);
624 if (event == CTL_HA_EVT_MSG_RECV) {
625 union ctl_ha_msg msg_info;
627 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
628 sizeof(msg_info), /*wait*/ 0);
630 printf("CTL: msg_type %d\n", msg_info.msg_type);
632 if (isc_status != 0) {
633 printf("Error receiving message, status = %d\n",
638 switch (msg_info.hdr.msg_type) {
639 case CTL_MSG_SERIALIZE:
641 printf("Serialize\n");
643 io = ctl_alloc_io_nowait(ctl_softc->othersc_pool);
645 printf("ctl_isc_event_handler: can't allocate "
648 /* Need to set busy and send msg back */
649 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
650 msg_info.hdr.status = CTL_SCSI_ERROR;
651 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY;
652 msg_info.scsi.sense_len = 0;
653 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
654 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){
659 // populate ctsio from msg_info
660 io->io_hdr.io_type = CTL_IO_SCSI;
661 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
662 io->io_hdr.original_sc = msg_info.hdr.original_sc;
664 printf("pOrig %x\n", (int)msg_info.original_sc);
666 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
669 * If we're in serialization-only mode, we don't
670 * want to go through full done processing. Thus
673 * XXX KDM add another flag that is more specific.
675 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)
676 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
677 io->io_hdr.nexus = msg_info.hdr.nexus;
679 printf("targ %d, port %d, iid %d, lun %d\n",
680 io->io_hdr.nexus.targ_target.id,
681 io->io_hdr.nexus.targ_port,
682 io->io_hdr.nexus.initid.id,
683 io->io_hdr.nexus.targ_lun);
685 io->scsiio.tag_num = msg_info.scsi.tag_num;
686 io->scsiio.tag_type = msg_info.scsi.tag_type;
687 memcpy(io->scsiio.cdb, msg_info.scsi.cdb,
689 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
690 const struct ctl_cmd_entry *entry;
692 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
693 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
695 entry->flags & CTL_FLAG_DATA_MASK;
700 /* Performed on the Originating SC, XFER mode only */
701 case CTL_MSG_DATAMOVE: {
702 struct ctl_sg_entry *sgl;
705 io = msg_info.hdr.original_sc;
707 printf("%s: original_sc == NULL!\n", __func__);
708 /* XXX KDM do something here */
711 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
712 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
714 * Keep track of this, we need to send it back over
715 * when the datamove is complete.
717 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
719 if (msg_info.dt.sg_sequence == 0) {
721 * XXX KDM we use the preallocated S/G list
722 * here, but we'll need to change this to
723 * dynamic allocation if we need larger S/G
726 if (msg_info.dt.kern_sg_entries >
727 sizeof(io->io_hdr.remote_sglist) /
728 sizeof(io->io_hdr.remote_sglist[0])) {
729 printf("%s: number of S/G entries "
730 "needed %u > allocated num %zd\n",
732 msg_info.dt.kern_sg_entries,
733 sizeof(io->io_hdr.remote_sglist)/
734 sizeof(io->io_hdr.remote_sglist[0]));
737 * XXX KDM send a message back to
738 * the other side to shut down the
739 * DMA. The error will come back
740 * through via the normal channel.
744 sgl = io->io_hdr.remote_sglist;
746 sizeof(io->io_hdr.remote_sglist));
748 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
750 io->scsiio.kern_sg_entries =
751 msg_info.dt.kern_sg_entries;
752 io->scsiio.rem_sg_entries =
753 msg_info.dt.kern_sg_entries;
754 io->scsiio.kern_data_len =
755 msg_info.dt.kern_data_len;
756 io->scsiio.kern_total_len =
757 msg_info.dt.kern_total_len;
758 io->scsiio.kern_data_resid =
759 msg_info.dt.kern_data_resid;
760 io->scsiio.kern_rel_offset =
761 msg_info.dt.kern_rel_offset;
763 * Clear out per-DMA flags.
765 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK;
767 * Add per-DMA flags that are set for this
768 * particular DMA request.
770 io->io_hdr.flags |= msg_info.dt.flags &
773 sgl = (struct ctl_sg_entry *)
774 io->scsiio.kern_data_ptr;
776 for (i = msg_info.dt.sent_sg_entries, j = 0;
777 i < (msg_info.dt.sent_sg_entries +
778 msg_info.dt.cur_sg_entries); i++, j++) {
779 sgl[i].addr = msg_info.dt.sg_list[j].addr;
780 sgl[i].len = msg_info.dt.sg_list[j].len;
783 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n",
785 msg_info.dt.sg_list[j].addr,
786 msg_info.dt.sg_list[j].len,
787 sgl[i].addr, sgl[i].len, j, i);
791 memcpy(&sgl[msg_info.dt.sent_sg_entries],
793 sizeof(*sgl) * msg_info.dt.cur_sg_entries);
797 * If this is the last piece of the I/O, we've got
798 * the full S/G list. Queue processing in the thread.
799 * Otherwise wait for the next piece.
801 if (msg_info.dt.sg_last != 0)
805 /* Performed on the Serializing (primary) SC, XFER mode only */
806 case CTL_MSG_DATAMOVE_DONE: {
807 if (msg_info.hdr.serializing_sc == NULL) {
808 printf("%s: serializing_sc == NULL!\n",
810 /* XXX KDM now what? */
814 * We grab the sense information here in case
815 * there was a failure, so we can return status
816 * back to the initiator.
818 io = msg_info.hdr.serializing_sc;
819 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
820 io->io_hdr.status = msg_info.hdr.status;
821 io->scsiio.scsi_status = msg_info.scsi.scsi_status;
822 io->scsiio.sense_len = msg_info.scsi.sense_len;
823 io->scsiio.sense_residual =msg_info.scsi.sense_residual;
824 io->io_hdr.port_status = msg_info.scsi.fetd_status;
825 io->scsiio.residual = msg_info.scsi.residual;
826 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data,
827 sizeof(io->scsiio.sense_data));
832 /* Preformed on Originating SC, SER_ONLY mode */
834 io = msg_info.hdr.original_sc;
836 printf("%s: Major Bummer\n", __func__);
840 printf("pOrig %x\n",(int) ctsio);
843 io->io_hdr.msg_type = CTL_MSG_R2R;
844 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
849 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
851 * Performed on the Originating (i.e. secondary) SC in XFER
854 case CTL_MSG_FINISH_IO:
855 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER)
856 ctl_isc_handler_finish_xfer(ctl_softc,
859 ctl_isc_handler_finish_ser_only(ctl_softc,
863 /* Preformed on Originating SC */
864 case CTL_MSG_BAD_JUJU:
865 io = msg_info.hdr.original_sc;
867 printf("%s: Bad JUJU!, original_sc is NULL!\n",
871 ctl_copy_sense_data(&msg_info, io);
873 * IO should have already been cleaned up on other
874 * SC so clear this flag so we won't send a message
875 * back to finish the IO there.
877 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
878 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
880 /* io = msg_info.hdr.serializing_sc; */
881 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
885 /* Handle resets sent from the other side */
886 case CTL_MSG_MANAGE_TASKS: {
887 struct ctl_taskio *taskio;
888 taskio = (struct ctl_taskio *)ctl_alloc_io_nowait(
889 ctl_softc->othersc_pool);
890 if (taskio == NULL) {
891 printf("ctl_isc_event_handler: can't allocate "
894 /* should I just call the proper reset func
898 ctl_zero_io((union ctl_io *)taskio);
899 taskio->io_hdr.io_type = CTL_IO_TASK;
900 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
901 taskio->io_hdr.nexus = msg_info.hdr.nexus;
902 taskio->task_action = msg_info.task.task_action;
903 taskio->tag_num = msg_info.task.tag_num;
904 taskio->tag_type = msg_info.task.tag_type;
906 taskio->io_hdr.start_time = time_uptime;
907 getbintime(&taskio->io_hdr.start_bt);
909 cs_prof_gettime(&taskio->io_hdr.start_ticks);
911 #endif /* CTL_TIME_IO */
912 ctl_run_task((union ctl_io *)taskio);
915 /* Persistent Reserve action which needs attention */
916 case CTL_MSG_PERS_ACTION:
917 presio = (struct ctl_prio *)ctl_alloc_io_nowait(
918 ctl_softc->othersc_pool);
919 if (presio == NULL) {
920 printf("ctl_isc_event_handler: can't allocate "
923 /* Need to set busy and send msg back */
926 ctl_zero_io((union ctl_io *)presio);
927 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
928 presio->pr_msg = msg_info.pr;
929 ctl_enqueue_isc((union ctl_io *)presio);
931 case CTL_MSG_SYNC_FE:
935 printf("How did I get here?\n");
937 } else if (event == CTL_HA_EVT_MSG_SENT) {
938 if (param != CTL_HA_STATUS_SUCCESS) {
939 printf("Bad status from ctl_ha_msg_send status %d\n",
943 } else if (event == CTL_HA_EVT_DISCONNECT) {
944 printf("CTL: Got a disconnect from Isc\n");
947 printf("ctl_isc_event_handler: Unknown event %d\n", event);
956 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
958 struct scsi_sense_data *sense;
960 sense = &dest->scsiio.sense_data;
961 bcopy(&src->scsi.sense_data, sense, sizeof(*sense));
962 dest->scsiio.scsi_status = src->scsi.scsi_status;
963 dest->scsiio.sense_len = src->scsi.sense_len;
964 dest->io_hdr.status = src->hdr.status;
968 ctl_ha_state_sysctl(SYSCTL_HANDLER_ARGS)
970 struct ctl_softc *softc = (struct ctl_softc *)arg1;
974 if (softc->flags & CTL_FLAG_ACTIVE_SHELF)
979 error = sysctl_handle_int(oidp, &value, 0, req);
980 if ((error != 0) || (req->newptr == NULL))
983 mtx_lock(&softc->ctl_lock);
985 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
987 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
988 STAILQ_FOREACH(lun, &softc->lun_list, links) {
989 mtx_lock(&lun->lun_lock);
990 for (i = 0; i < CTL_MAX_INITIATORS; i++)
991 lun->pending_ua[i] |= CTL_UA_ASYM_ACC_CHANGE;
992 mtx_unlock(&lun->lun_lock);
994 mtx_unlock(&softc->ctl_lock);
1001 struct ctl_softc *softc;
1003 struct ctl_port *port;
1004 int i, error, retval;
1011 control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1013 softc = control_softc;
1015 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
1018 softc->dev->si_drv1 = softc;
1021 * By default, return a "bad LUN" peripheral qualifier for unknown
1022 * LUNs. The user can override this default using the tunable or
1023 * sysctl. See the comment in ctl_inquiry_std() for more details.
1025 softc->inquiry_pq_no_lun = 1;
1026 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun",
1027 &softc->inquiry_pq_no_lun);
1028 sysctl_ctx_init(&softc->sysctl_ctx);
1029 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1030 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1031 CTLFLAG_RD, 0, "CAM Target Layer");
1033 if (softc->sysctl_tree == NULL) {
1034 printf("%s: unable to allocate sysctl tree\n", __func__);
1035 destroy_dev(softc->dev);
1036 free(control_softc, M_DEVBUF);
1037 control_softc = NULL;
1041 SYSCTL_ADD_INT(&softc->sysctl_ctx,
1042 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
1043 "inquiry_pq_no_lun", CTLFLAG_RW,
1044 &softc->inquiry_pq_no_lun, 0,
1045 "Report no lun possible for invalid LUNs");
1047 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1048 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
1049 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1050 softc->open_count = 0;
1053 * Default to actually sending a SYNCHRONIZE CACHE command down to
1056 softc->flags = CTL_FLAG_REAL_SYNC;
1059 * In Copan's HA scheme, the "master" and "slave" roles are
1060 * figured out through the slot the controller is in. Although it
1061 * is an active/active system, someone has to be in charge.
1063 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1064 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1065 "HA head ID (0 - no HA)");
1066 if (softc->ha_id == 0) {
1067 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1068 softc->is_single = 1;
1069 softc->port_offset = 0;
1071 softc->port_offset = (softc->ha_id - 1) * CTL_MAX_PORTS;
1072 softc->persis_offset = softc->port_offset * CTL_MAX_INIT_PER_PORT;
1075 * XXX KDM need to figure out where we want to get our target ID
1076 * and WWID. Is it different on each port?
1078 softc->target.id = 0;
1079 softc->target.wwid[0] = 0x12345678;
1080 softc->target.wwid[1] = 0x87654321;
1081 STAILQ_INIT(&softc->lun_list);
1082 STAILQ_INIT(&softc->pending_lun_queue);
1083 STAILQ_INIT(&softc->fe_list);
1084 STAILQ_INIT(&softc->port_list);
1085 STAILQ_INIT(&softc->be_list);
1086 ctl_tpc_init(softc);
1088 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
1091 printf("ctl: can't allocate %d entry other SC pool, "
1092 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1095 softc->othersc_pool = other_pool;
1097 if (worker_threads <= 0)
1098 worker_threads = max(1, mp_ncpus / 4);
1099 if (worker_threads > CTL_MAX_THREADS)
1100 worker_threads = CTL_MAX_THREADS;
1102 for (i = 0; i < worker_threads; i++) {
1103 struct ctl_thread *thr = &softc->threads[i];
1105 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1106 thr->ctl_softc = softc;
1107 STAILQ_INIT(&thr->incoming_queue);
1108 STAILQ_INIT(&thr->rtr_queue);
1109 STAILQ_INIT(&thr->done_queue);
1110 STAILQ_INIT(&thr->isc_queue);
1112 error = kproc_kthread_add(ctl_work_thread, thr,
1113 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1115 printf("error creating CTL work thread!\n");
1116 ctl_pool_free(other_pool);
1120 error = kproc_kthread_add(ctl_lun_thread, softc,
1121 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
1123 printf("error creating CTL lun thread!\n");
1124 ctl_pool_free(other_pool);
1127 error = kproc_kthread_add(ctl_thresh_thread, softc,
1128 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh");
1130 printf("error creating CTL threshold thread!\n");
1131 ctl_pool_free(other_pool);
1135 printf("ctl: CAM Target Layer loaded\n");
1138 * Initialize the ioctl front end.
1140 ctl_frontend_register(&ioctl_frontend);
1141 port = &softc->ioctl_info.port;
1142 port->frontend = &ioctl_frontend;
1143 sprintf(softc->ioctl_info.port_name, "ioctl");
1144 port->port_type = CTL_PORT_IOCTL;
1145 port->num_requested_ctl_io = 100;
1146 port->port_name = softc->ioctl_info.port_name;
1147 port->port_online = ctl_ioctl_online;
1148 port->port_offline = ctl_ioctl_offline;
1149 port->onoff_arg = &softc->ioctl_info;
1150 port->lun_enable = ctl_ioctl_lun_enable;
1151 port->lun_disable = ctl_ioctl_lun_disable;
1152 port->targ_lun_arg = &softc->ioctl_info;
1153 port->fe_datamove = ctl_ioctl_datamove;
1154 port->fe_done = ctl_ioctl_done;
1155 port->max_targets = 15;
1156 port->max_target_id = 15;
1158 if (ctl_port_register(&softc->ioctl_info.port) != 0) {
1159 printf("ctl: ioctl front end registration failed, will "
1160 "continue anyway\n");
1163 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1164 OID_AUTO, "ha_state", CTLTYPE_INT | CTLFLAG_RWTUN,
1165 softc, 0, ctl_ha_state_sysctl, "I", "HA state for this head");
1168 if (sizeof(struct callout) > CTL_TIMER_BYTES) {
1169 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n",
1170 sizeof(struct callout), CTL_TIMER_BYTES);
1173 #endif /* CTL_IO_DELAY */
1181 struct ctl_softc *softc;
1182 struct ctl_lun *lun, *next_lun;
1184 softc = (struct ctl_softc *)control_softc;
1186 if (ctl_port_deregister(&softc->ioctl_info.port) != 0)
1187 printf("ctl: ioctl front end deregistration failed\n");
1189 mtx_lock(&softc->ctl_lock);
1194 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
1195 next_lun = STAILQ_NEXT(lun, links);
1199 mtx_unlock(&softc->ctl_lock);
1201 ctl_frontend_deregister(&ioctl_frontend);
1204 ctl_shutdown_thread(softc->work_thread);
1205 mtx_destroy(&softc->queue_lock);
1208 ctl_tpc_shutdown(softc);
1209 uma_zdestroy(softc->io_zone);
1210 mtx_destroy(&softc->ctl_lock);
1212 destroy_dev(softc->dev);
1214 sysctl_ctx_free(&softc->sysctl_ctx);
1216 free(control_softc, M_DEVBUF);
1217 control_softc = NULL;
1220 printf("ctl: CAM Target Layer unloaded\n");
1224 ctl_module_event_handler(module_t mod, int what, void *arg)
1229 return (ctl_init());
1233 return (EOPNOTSUPP);
1238 * XXX KDM should we do some access checks here? Bump a reference count to
1239 * prevent a CTL module from being unloaded while someone has it open?
1242 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1248 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1254 ctl_port_enable(ctl_port_type port_type)
1256 struct ctl_softc *softc = control_softc;
1257 struct ctl_port *port;
1259 if (softc->is_single == 0) {
1260 union ctl_ha_msg msg_info;
1264 printf("%s: HA mode, synchronizing frontend enable\n",
1267 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE;
1268 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1269 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) {
1270 printf("Sync msg send error retval %d\n", isc_retval);
1272 if (!rcv_sync_msg) {
1273 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
1274 sizeof(msg_info), 1);
1277 printf("CTL:Frontend Enable\n");
1279 printf("%s: single mode, skipping frontend synchronization\n",
1284 STAILQ_FOREACH(port, &softc->port_list, links) {
1285 if (port_type & port->port_type)
1288 printf("port %d\n", port->targ_port);
1290 ctl_port_online(port);
1298 ctl_port_disable(ctl_port_type port_type)
1300 struct ctl_softc *softc;
1301 struct ctl_port *port;
1303 softc = control_softc;
1305 STAILQ_FOREACH(port, &softc->port_list, links) {
1306 if (port_type & port->port_type)
1307 ctl_port_offline(port);
1314 * Returns 0 for success, 1 for failure.
1315 * Currently the only failure mode is if there aren't enough entries
1316 * allocated. So, in case of a failure, look at num_entries_dropped,
1317 * reallocate and try again.
1320 ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced,
1321 int *num_entries_filled, int *num_entries_dropped,
1322 ctl_port_type port_type, int no_virtual)
1324 struct ctl_softc *softc;
1325 struct ctl_port *port;
1326 int entries_dropped, entries_filled;
1330 softc = control_softc;
1334 entries_dropped = 0;
1337 mtx_lock(&softc->ctl_lock);
1338 STAILQ_FOREACH(port, &softc->port_list, links) {
1339 struct ctl_port_entry *entry;
1341 if ((port->port_type & port_type) == 0)
1344 if ((no_virtual != 0)
1345 && (port->virtual_port != 0))
1348 if (entries_filled >= num_entries_alloced) {
1352 entry = &entries[i];
1354 entry->port_type = port->port_type;
1355 strlcpy(entry->port_name, port->port_name,
1356 sizeof(entry->port_name));
1357 entry->physical_port = port->physical_port;
1358 entry->virtual_port = port->virtual_port;
1359 entry->wwnn = port->wwnn;
1360 entry->wwpn = port->wwpn;
1366 mtx_unlock(&softc->ctl_lock);
1368 if (entries_dropped > 0)
1371 *num_entries_dropped = entries_dropped;
1372 *num_entries_filled = entries_filled;
1378 ctl_ioctl_online(void *arg)
1380 struct ctl_ioctl_info *ioctl_info;
1382 ioctl_info = (struct ctl_ioctl_info *)arg;
1384 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED;
1388 ctl_ioctl_offline(void *arg)
1390 struct ctl_ioctl_info *ioctl_info;
1392 ioctl_info = (struct ctl_ioctl_info *)arg;
1394 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED;
1398 * Remove an initiator by port number and initiator ID.
1399 * Returns 0 for success, -1 for failure.
1402 ctl_remove_initiator(struct ctl_port *port, int iid)
1404 struct ctl_softc *softc = control_softc;
1406 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1408 if (iid > CTL_MAX_INIT_PER_PORT) {
1409 printf("%s: initiator ID %u > maximun %u!\n",
1410 __func__, iid, CTL_MAX_INIT_PER_PORT);
1414 mtx_lock(&softc->ctl_lock);
1415 port->wwpn_iid[iid].in_use--;
1416 port->wwpn_iid[iid].last_use = time_uptime;
1417 mtx_unlock(&softc->ctl_lock);
1423 * Add an initiator to the initiator map.
1424 * Returns iid for success, < 0 for failure.
1427 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
1429 struct ctl_softc *softc = control_softc;
1433 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1435 if (iid >= CTL_MAX_INIT_PER_PORT) {
1436 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
1437 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
1442 mtx_lock(&softc->ctl_lock);
1444 if (iid < 0 && (wwpn != 0 || name != NULL)) {
1445 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1446 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
1450 if (name != NULL && port->wwpn_iid[i].name != NULL &&
1451 strcmp(name, port->wwpn_iid[i].name) == 0) {
1459 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1460 if (port->wwpn_iid[i].in_use == 0 &&
1461 port->wwpn_iid[i].wwpn == 0 &&
1462 port->wwpn_iid[i].name == NULL) {
1471 best_time = INT32_MAX;
1472 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1473 if (port->wwpn_iid[i].in_use == 0) {
1474 if (port->wwpn_iid[i].last_use < best_time) {
1476 best_time = port->wwpn_iid[i].last_use;
1484 mtx_unlock(&softc->ctl_lock);
1489 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
1491 * This is not an error yet.
1493 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
1495 printf("%s: port %d iid %u WWPN %#jx arrived"
1496 " again\n", __func__, port->targ_port,
1497 iid, (uintmax_t)wwpn);
1501 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
1502 strcmp(name, port->wwpn_iid[iid].name) == 0) {
1504 printf("%s: port %d iid %u name '%s' arrived"
1505 " again\n", __func__, port->targ_port,
1512 * This is an error, but what do we do about it? The
1513 * driver is telling us we have a new WWPN for this
1514 * initiator ID, so we pretty much need to use it.
1516 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
1517 " but WWPN %#jx '%s' is still at that address\n",
1518 __func__, port->targ_port, iid, wwpn, name,
1519 (uintmax_t)port->wwpn_iid[iid].wwpn,
1520 port->wwpn_iid[iid].name);
1523 * XXX KDM clear have_ca and ua_pending on each LUN for
1528 free(port->wwpn_iid[iid].name, M_CTL);
1529 port->wwpn_iid[iid].name = name;
1530 port->wwpn_iid[iid].wwpn = wwpn;
1531 port->wwpn_iid[iid].in_use++;
1532 mtx_unlock(&softc->ctl_lock);
1538 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
1542 switch (port->port_type) {
1545 struct scsi_transportid_fcp *id =
1546 (struct scsi_transportid_fcp *)buf;
1547 if (port->wwpn_iid[iid].wwpn == 0)
1549 memset(id, 0, sizeof(*id));
1550 id->format_protocol = SCSI_PROTO_FC;
1551 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
1552 return (sizeof(*id));
1554 case CTL_PORT_ISCSI:
1556 struct scsi_transportid_iscsi_port *id =
1557 (struct scsi_transportid_iscsi_port *)buf;
1558 if (port->wwpn_iid[iid].name == NULL)
1561 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
1563 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
1564 len = roundup2(min(len, 252), 4);
1565 scsi_ulto2b(len, id->additional_length);
1566 return (sizeof(*id) + len);
1570 struct scsi_transportid_sas *id =
1571 (struct scsi_transportid_sas *)buf;
1572 if (port->wwpn_iid[iid].wwpn == 0)
1574 memset(id, 0, sizeof(*id));
1575 id->format_protocol = SCSI_PROTO_SAS;
1576 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
1577 return (sizeof(*id));
1581 struct scsi_transportid_spi *id =
1582 (struct scsi_transportid_spi *)buf;
1583 memset(id, 0, sizeof(*id));
1584 id->format_protocol = SCSI_PROTO_SPI;
1585 scsi_ulto2b(iid, id->scsi_addr);
1586 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
1587 return (sizeof(*id));
1593 ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id)
1599 ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id)
1605 * Data movement routine for the CTL ioctl frontend port.
1608 ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio)
1610 struct ctl_sg_entry *ext_sglist, *kern_sglist;
1611 struct ctl_sg_entry ext_entry, kern_entry;
1612 int ext_sglen, ext_sg_entries, kern_sg_entries;
1613 int ext_sg_start, ext_offset;
1614 int len_to_copy, len_copied;
1615 int kern_watermark, ext_watermark;
1616 int ext_sglist_malloced;
1619 ext_sglist_malloced = 0;
1623 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n"));
1626 * If this flag is set, fake the data transfer.
1628 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) {
1629 ctsio->ext_data_filled = ctsio->ext_data_len;
1634 * To simplify things here, if we have a single buffer, stick it in
1635 * a S/G entry and just make it a single entry S/G list.
1637 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) {
1640 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist);
1642 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL,
1644 ext_sglist_malloced = 1;
1645 if (copyin(ctsio->ext_data_ptr, ext_sglist,
1647 ctl_set_internal_failure(ctsio,
1652 ext_sg_entries = ctsio->ext_sg_entries;
1654 for (i = 0; i < ext_sg_entries; i++) {
1655 if ((len_seen + ext_sglist[i].len) >=
1656 ctsio->ext_data_filled) {
1658 ext_offset = ctsio->ext_data_filled - len_seen;
1661 len_seen += ext_sglist[i].len;
1664 ext_sglist = &ext_entry;
1665 ext_sglist->addr = ctsio->ext_data_ptr;
1666 ext_sglist->len = ctsio->ext_data_len;
1669 ext_offset = ctsio->ext_data_filled;
1672 if (ctsio->kern_sg_entries > 0) {
1673 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
1674 kern_sg_entries = ctsio->kern_sg_entries;
1676 kern_sglist = &kern_entry;
1677 kern_sglist->addr = ctsio->kern_data_ptr;
1678 kern_sglist->len = ctsio->kern_data_len;
1679 kern_sg_entries = 1;
1684 ext_watermark = ext_offset;
1686 for (i = ext_sg_start, j = 0;
1687 i < ext_sg_entries && j < kern_sg_entries;) {
1688 uint8_t *ext_ptr, *kern_ptr;
1690 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark,
1691 kern_sglist[j].len - kern_watermark);
1693 ext_ptr = (uint8_t *)ext_sglist[i].addr;
1694 ext_ptr = ext_ptr + ext_watermark;
1695 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
1699 panic("need to implement bus address support");
1701 kern_ptr = bus_to_virt(kern_sglist[j].addr);
1704 kern_ptr = (uint8_t *)kern_sglist[j].addr;
1705 kern_ptr = kern_ptr + kern_watermark;
1707 kern_watermark += len_to_copy;
1708 ext_watermark += len_to_copy;
1710 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
1712 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1713 "bytes to user\n", len_to_copy));
1714 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1715 "to %p\n", kern_ptr, ext_ptr));
1716 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) {
1717 ctl_set_internal_failure(ctsio,
1723 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1724 "bytes from user\n", len_to_copy));
1725 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1726 "to %p\n", ext_ptr, kern_ptr));
1727 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){
1728 ctl_set_internal_failure(ctsio,
1735 len_copied += len_to_copy;
1737 if (ext_sglist[i].len == ext_watermark) {
1742 if (kern_sglist[j].len == kern_watermark) {
1748 ctsio->ext_data_filled += len_copied;
1750 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, "
1751 "kern_sg_entries: %d\n", ext_sg_entries,
1753 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, "
1754 "kern_data_len = %d\n", ctsio->ext_data_len,
1755 ctsio->kern_data_len));
1758 /* XXX KDM set residual?? */
1761 if (ext_sglist_malloced != 0)
1762 free(ext_sglist, M_CTL);
1764 return (CTL_RETVAL_COMPLETE);
1768 * Serialize a command that went down the "wrong" side, and so was sent to
1769 * this controller for execution. The logic is a little different than the
1770 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
1771 * sent back to the other side, but in the success case, we execute the
1772 * command on this side (XFER mode) or tell the other side to execute it
1776 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
1778 struct ctl_softc *ctl_softc;
1779 union ctl_ha_msg msg_info;
1780 struct ctl_lun *lun;
1784 ctl_softc = control_softc;
1786 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
1787 lun = ctl_softc->ctl_luns[targ_lun];
1791 * Why isn't LUN defined? The other side wouldn't
1792 * send a cmd if the LUN is undefined.
1794 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__);
1796 /* "Logical unit not supported" */
1797 ctl_set_sense_data(&msg_info.scsi.sense_data,
1799 /*sense_format*/SSD_TYPE_NONE,
1800 /*current_error*/ 1,
1801 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1806 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1807 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1808 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1809 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1810 msg_info.hdr.serializing_sc = NULL;
1811 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1812 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1813 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1819 mtx_lock(&lun->lun_lock);
1820 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1822 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
1823 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
1825 case CTL_ACTION_BLOCK:
1826 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
1827 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
1830 case CTL_ACTION_PASS:
1831 case CTL_ACTION_SKIP:
1832 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
1833 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
1834 ctl_enqueue_rtr((union ctl_io *)ctsio);
1837 /* send msg back to other side */
1838 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1839 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
1840 msg_info.hdr.msg_type = CTL_MSG_R2R;
1842 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc);
1844 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1845 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1849 case CTL_ACTION_OVERLAP:
1850 /* OVERLAPPED COMMANDS ATTEMPTED */
1851 ctl_set_sense_data(&msg_info.scsi.sense_data,
1853 /*sense_format*/SSD_TYPE_NONE,
1854 /*current_error*/ 1,
1855 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1860 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1861 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1862 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1863 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1864 msg_info.hdr.serializing_sc = NULL;
1865 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1867 printf("BAD JUJU:Major Bummer Overlap\n");
1869 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1871 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1872 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1875 case CTL_ACTION_OVERLAP_TAG:
1876 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */
1877 ctl_set_sense_data(&msg_info.scsi.sense_data,
1879 /*sense_format*/SSD_TYPE_NONE,
1880 /*current_error*/ 1,
1881 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1883 /*ascq*/ ctsio->tag_num & 0xff,
1886 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1887 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1888 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1889 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1890 msg_info.hdr.serializing_sc = NULL;
1891 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1893 printf("BAD JUJU:Major Bummer Overlap Tag\n");
1895 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1897 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1898 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1901 case CTL_ACTION_ERROR:
1903 /* "Internal target failure" */
1904 ctl_set_sense_data(&msg_info.scsi.sense_data,
1906 /*sense_format*/SSD_TYPE_NONE,
1907 /*current_error*/ 1,
1908 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
1913 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1914 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1915 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1916 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1917 msg_info.hdr.serializing_sc = NULL;
1918 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1920 printf("BAD JUJU:Major Bummer HW Error\n");
1922 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1924 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1925 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1929 mtx_unlock(&lun->lun_lock);
1934 ctl_ioctl_submit_wait(union ctl_io *io)
1936 struct ctl_fe_ioctl_params params;
1937 ctl_fe_ioctl_state last_state;
1942 bzero(¶ms, sizeof(params));
1944 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF);
1945 cv_init(¶ms.sem, "ctlioccv");
1946 params.state = CTL_IOCTL_INPROG;
1947 last_state = params.state;
1949 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms;
1951 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n"));
1953 /* This shouldn't happen */
1954 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE)
1960 mtx_lock(¶ms.ioctl_mtx);
1962 * Check the state here, and don't sleep if the state has
1963 * already changed (i.e. wakeup has already occured, but we
1964 * weren't waiting yet).
1966 if (params.state == last_state) {
1967 /* XXX KDM cv_wait_sig instead? */
1968 cv_wait(¶ms.sem, ¶ms.ioctl_mtx);
1970 last_state = params.state;
1972 switch (params.state) {
1973 case CTL_IOCTL_INPROG:
1974 /* Why did we wake up? */
1975 /* XXX KDM error here? */
1976 mtx_unlock(¶ms.ioctl_mtx);
1978 case CTL_IOCTL_DATAMOVE:
1979 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n"));
1982 * change last_state back to INPROG to avoid
1983 * deadlock on subsequent data moves.
1985 params.state = last_state = CTL_IOCTL_INPROG;
1987 mtx_unlock(¶ms.ioctl_mtx);
1988 ctl_ioctl_do_datamove(&io->scsiio);
1990 * Note that in some cases, most notably writes,
1991 * this will queue the I/O and call us back later.
1992 * In other cases, generally reads, this routine
1993 * will immediately call back and wake us up,
1994 * probably using our own context.
1996 io->scsiio.be_move_done(io);
1998 case CTL_IOCTL_DONE:
1999 mtx_unlock(¶ms.ioctl_mtx);
2000 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n"));
2004 mtx_unlock(¶ms.ioctl_mtx);
2005 /* XXX KDM error here? */
2008 } while (done == 0);
2010 mtx_destroy(¶ms.ioctl_mtx);
2011 cv_destroy(¶ms.sem);
2013 return (CTL_RETVAL_COMPLETE);
2017 ctl_ioctl_datamove(union ctl_io *io)
2019 struct ctl_fe_ioctl_params *params;
2021 params = (struct ctl_fe_ioctl_params *)
2022 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2024 mtx_lock(¶ms->ioctl_mtx);
2025 params->state = CTL_IOCTL_DATAMOVE;
2026 cv_broadcast(¶ms->sem);
2027 mtx_unlock(¶ms->ioctl_mtx);
2031 ctl_ioctl_done(union ctl_io *io)
2033 struct ctl_fe_ioctl_params *params;
2035 params = (struct ctl_fe_ioctl_params *)
2036 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2038 mtx_lock(¶ms->ioctl_mtx);
2039 params->state = CTL_IOCTL_DONE;
2040 cv_broadcast(¶ms->sem);
2041 mtx_unlock(¶ms->ioctl_mtx);
2045 ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask)
2047 struct ctl_fe_ioctl_startstop_info *sd_info;
2049 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg;
2051 sd_info->hs_info.status = metatask->status;
2052 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns;
2053 sd_info->hs_info.luns_complete =
2054 metatask->taskinfo.startstop.luns_complete;
2055 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed;
2057 cv_broadcast(&sd_info->sem);
2061 ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask)
2063 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info;
2065 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg;
2067 mtx_lock(fe_bbr_info->lock);
2068 fe_bbr_info->bbr_info->status = metatask->status;
2069 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2070 fe_bbr_info->wakeup_done = 1;
2071 mtx_unlock(fe_bbr_info->lock);
2073 cv_broadcast(&fe_bbr_info->sem);
2077 * Returns 0 for success, errno for failure.
2080 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2081 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2088 mtx_lock(&lun->lun_lock);
2089 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2090 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2092 struct ctl_ooa_entry *entry;
2095 * If we've got more than we can fit, just count the
2096 * remaining entries.
2098 if (*cur_fill_num >= ooa_hdr->alloc_num)
2101 entry = &kern_entries[*cur_fill_num];
2103 entry->tag_num = io->scsiio.tag_num;
2104 entry->lun_num = lun->lun;
2106 entry->start_bt = io->io_hdr.start_bt;
2108 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2109 entry->cdb_len = io->scsiio.cdb_len;
2110 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2111 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2113 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2114 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2116 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2117 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2119 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2120 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2122 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2123 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2125 mtx_unlock(&lun->lun_lock);
2131 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2132 size_t error_str_len)
2136 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2138 if (copyin(user_addr, kptr, len) != 0) {
2139 snprintf(error_str, error_str_len, "Error copying %d bytes "
2140 "from user address %p to kernel address %p", len,
2150 ctl_free_args(int num_args, struct ctl_be_arg *args)
2157 for (i = 0; i < num_args; i++) {
2158 free(args[i].kname, M_CTL);
2159 free(args[i].kvalue, M_CTL);
2165 static struct ctl_be_arg *
2166 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
2167 char *error_str, size_t error_str_len)
2169 struct ctl_be_arg *args;
2172 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
2173 error_str, error_str_len);
2178 for (i = 0; i < num_args; i++) {
2179 args[i].kname = NULL;
2180 args[i].kvalue = NULL;
2183 for (i = 0; i < num_args; i++) {
2186 args[i].kname = ctl_copyin_alloc(args[i].name,
2187 args[i].namelen, error_str, error_str_len);
2188 if (args[i].kname == NULL)
2191 if (args[i].kname[args[i].namelen - 1] != '\0') {
2192 snprintf(error_str, error_str_len, "Argument %d "
2193 "name is not NUL-terminated", i);
2197 if (args[i].flags & CTL_BEARG_RD) {
2198 tmpptr = ctl_copyin_alloc(args[i].value,
2199 args[i].vallen, error_str, error_str_len);
2202 if ((args[i].flags & CTL_BEARG_ASCII)
2203 && (tmpptr[args[i].vallen - 1] != '\0')) {
2204 snprintf(error_str, error_str_len, "Argument "
2205 "%d value is not NUL-terminated", i);
2208 args[i].kvalue = tmpptr;
2210 args[i].kvalue = malloc(args[i].vallen,
2211 M_CTL, M_WAITOK | M_ZERO);
2218 ctl_free_args(num_args, args);
2224 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2228 for (i = 0; i < num_args; i++) {
2229 if (args[i].flags & CTL_BEARG_WR)
2230 copyout(args[i].kvalue, args[i].value, args[i].vallen);
2235 * Escape characters that are illegal or not recommended in XML.
2238 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2240 char *end = str + size;
2245 for (; *str && str < end; str++) {
2248 retval = sbuf_printf(sb, "&");
2251 retval = sbuf_printf(sb, ">");
2254 retval = sbuf_printf(sb, "<");
2257 retval = sbuf_putc(sb, *str);
2270 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2272 struct scsi_vpd_id_descriptor *desc;
2275 if (id == NULL || id->len < 4)
2277 desc = (struct scsi_vpd_id_descriptor *)id->data;
2278 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2279 case SVPD_ID_TYPE_T10:
2280 sbuf_printf(sb, "t10.");
2282 case SVPD_ID_TYPE_EUI64:
2283 sbuf_printf(sb, "eui.");
2285 case SVPD_ID_TYPE_NAA:
2286 sbuf_printf(sb, "naa.");
2288 case SVPD_ID_TYPE_SCSI_NAME:
2291 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2292 case SVPD_ID_CODESET_BINARY:
2293 for (i = 0; i < desc->length; i++)
2294 sbuf_printf(sb, "%02x", desc->identifier[i]);
2296 case SVPD_ID_CODESET_ASCII:
2297 sbuf_printf(sb, "%.*s", (int)desc->length,
2298 (char *)desc->identifier);
2300 case SVPD_ID_CODESET_UTF8:
2301 sbuf_printf(sb, "%s", (char *)desc->identifier);
2307 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2310 struct ctl_softc *softc;
2313 softc = control_softc;
2323 * If we haven't been "enabled", don't allow any SCSI I/O
2326 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) {
2331 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref);
2334 * Need to save the pool reference so it doesn't get
2335 * spammed by the user's ctl_io.
2337 pool_tmp = io->io_hdr.pool;
2338 memcpy(io, (void *)addr, sizeof(*io));
2339 io->io_hdr.pool = pool_tmp;
2342 * No status yet, so make sure the status is set properly.
2344 io->io_hdr.status = CTL_STATUS_NONE;
2347 * The user sets the initiator ID, target and LUN IDs.
2349 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port;
2350 io->io_hdr.flags |= CTL_FLAG_USER_REQ;
2351 if ((io->io_hdr.io_type == CTL_IO_SCSI)
2352 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED))
2353 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++;
2355 retval = ctl_ioctl_submit_wait(io);
2362 memcpy((void *)addr, io, sizeof(*io));
2364 /* return this to our pool */
2369 case CTL_ENABLE_PORT:
2370 case CTL_DISABLE_PORT:
2371 case CTL_SET_PORT_WWNS: {
2372 struct ctl_port *port;
2373 struct ctl_port_entry *entry;
2375 entry = (struct ctl_port_entry *)addr;
2377 mtx_lock(&softc->ctl_lock);
2378 STAILQ_FOREACH(port, &softc->port_list, links) {
2384 if ((entry->port_type == CTL_PORT_NONE)
2385 && (entry->targ_port == port->targ_port)) {
2387 * If the user only wants to enable or
2388 * disable or set WWNs on a specific port,
2389 * do the operation and we're done.
2393 } else if (entry->port_type & port->port_type) {
2395 * Compare the user's type mask with the
2396 * particular frontend type to see if we
2403 * Make sure the user isn't trying to set
2404 * WWNs on multiple ports at the same time.
2406 if (cmd == CTL_SET_PORT_WWNS) {
2407 printf("%s: Can't set WWNs on "
2408 "multiple ports\n", __func__);
2415 * XXX KDM we have to drop the lock here,
2416 * because the online/offline operations
2417 * can potentially block. We need to
2418 * reference count the frontends so they
2421 mtx_unlock(&softc->ctl_lock);
2423 if (cmd == CTL_ENABLE_PORT) {
2424 struct ctl_lun *lun;
2426 STAILQ_FOREACH(lun, &softc->lun_list,
2428 port->lun_enable(port->targ_lun_arg,
2433 ctl_port_online(port);
2434 } else if (cmd == CTL_DISABLE_PORT) {
2435 struct ctl_lun *lun;
2437 ctl_port_offline(port);
2439 STAILQ_FOREACH(lun, &softc->lun_list,
2448 mtx_lock(&softc->ctl_lock);
2450 if (cmd == CTL_SET_PORT_WWNS)
2451 ctl_port_set_wwns(port,
2452 (entry->flags & CTL_PORT_WWNN_VALID) ?
2454 (entry->flags & CTL_PORT_WWPN_VALID) ?
2455 1 : 0, entry->wwpn);
2460 mtx_unlock(&softc->ctl_lock);
2463 case CTL_GET_PORT_LIST: {
2464 struct ctl_port *port;
2465 struct ctl_port_list *list;
2468 list = (struct ctl_port_list *)addr;
2470 if (list->alloc_len != (list->alloc_num *
2471 sizeof(struct ctl_port_entry))) {
2472 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != "
2473 "alloc_num %u * sizeof(struct ctl_port_entry) "
2474 "%zu\n", __func__, list->alloc_len,
2475 list->alloc_num, sizeof(struct ctl_port_entry));
2481 list->dropped_num = 0;
2483 mtx_lock(&softc->ctl_lock);
2484 STAILQ_FOREACH(port, &softc->port_list, links) {
2485 struct ctl_port_entry entry, *list_entry;
2487 if (list->fill_num >= list->alloc_num) {
2488 list->dropped_num++;
2492 entry.port_type = port->port_type;
2493 strlcpy(entry.port_name, port->port_name,
2494 sizeof(entry.port_name));
2495 entry.targ_port = port->targ_port;
2496 entry.physical_port = port->physical_port;
2497 entry.virtual_port = port->virtual_port;
2498 entry.wwnn = port->wwnn;
2499 entry.wwpn = port->wwpn;
2500 if (port->status & CTL_PORT_STATUS_ONLINE)
2505 list_entry = &list->entries[i];
2507 retval = copyout(&entry, list_entry, sizeof(entry));
2509 printf("%s: CTL_GET_PORT_LIST: copyout "
2510 "returned %d\n", __func__, retval);
2515 list->fill_len += sizeof(entry);
2517 mtx_unlock(&softc->ctl_lock);
2520 * If this is non-zero, we had a copyout fault, so there's
2521 * probably no point in attempting to set the status inside
2527 if (list->dropped_num > 0)
2528 list->status = CTL_PORT_LIST_NEED_MORE_SPACE;
2530 list->status = CTL_PORT_LIST_OK;
2533 case CTL_DUMP_OOA: {
2534 struct ctl_lun *lun;
2539 mtx_lock(&softc->ctl_lock);
2540 printf("Dumping OOA queues:\n");
2541 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2542 mtx_lock(&lun->lun_lock);
2543 for (io = (union ctl_io *)TAILQ_FIRST(
2544 &lun->ooa_queue); io != NULL;
2545 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2547 sbuf_new(&sb, printbuf, sizeof(printbuf),
2549 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ",
2553 CTL_FLAG_BLOCKED) ? "" : " BLOCKED",
2555 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
2557 CTL_FLAG_ABORT) ? " ABORT" : "",
2559 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : "");
2560 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
2562 printf("%s\n", sbuf_data(&sb));
2564 mtx_unlock(&lun->lun_lock);
2566 printf("OOA queues dump done\n");
2567 mtx_unlock(&softc->ctl_lock);
2571 struct ctl_lun *lun;
2572 struct ctl_ooa *ooa_hdr;
2573 struct ctl_ooa_entry *entries;
2574 uint32_t cur_fill_num;
2576 ooa_hdr = (struct ctl_ooa *)addr;
2578 if ((ooa_hdr->alloc_len == 0)
2579 || (ooa_hdr->alloc_num == 0)) {
2580 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2581 "must be non-zero\n", __func__,
2582 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2587 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2588 sizeof(struct ctl_ooa_entry))) {
2589 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2590 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2591 __func__, ooa_hdr->alloc_len,
2592 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2597 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2598 if (entries == NULL) {
2599 printf("%s: could not allocate %d bytes for OOA "
2600 "dump\n", __func__, ooa_hdr->alloc_len);
2605 mtx_lock(&softc->ctl_lock);
2606 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2607 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS)
2608 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2609 mtx_unlock(&softc->ctl_lock);
2610 free(entries, M_CTL);
2611 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2612 __func__, (uintmax_t)ooa_hdr->lun_num);
2619 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2620 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2621 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2627 mtx_unlock(&softc->ctl_lock);
2628 free(entries, M_CTL);
2632 lun = softc->ctl_luns[ooa_hdr->lun_num];
2634 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr,
2637 mtx_unlock(&softc->ctl_lock);
2639 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2640 ooa_hdr->fill_len = ooa_hdr->fill_num *
2641 sizeof(struct ctl_ooa_entry);
2642 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2644 printf("%s: error copying out %d bytes for OOA dump\n",
2645 __func__, ooa_hdr->fill_len);
2648 getbintime(&ooa_hdr->cur_bt);
2650 if (cur_fill_num > ooa_hdr->alloc_num) {
2651 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2652 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2654 ooa_hdr->dropped_num = 0;
2655 ooa_hdr->status = CTL_OOA_OK;
2658 free(entries, M_CTL);
2661 case CTL_CHECK_OOA: {
2663 struct ctl_lun *lun;
2664 struct ctl_ooa_info *ooa_info;
2667 ooa_info = (struct ctl_ooa_info *)addr;
2669 if (ooa_info->lun_id >= CTL_MAX_LUNS) {
2670 ooa_info->status = CTL_OOA_INVALID_LUN;
2673 mtx_lock(&softc->ctl_lock);
2674 lun = softc->ctl_luns[ooa_info->lun_id];
2676 mtx_unlock(&softc->ctl_lock);
2677 ooa_info->status = CTL_OOA_INVALID_LUN;
2680 mtx_lock(&lun->lun_lock);
2681 mtx_unlock(&softc->ctl_lock);
2682 ooa_info->num_entries = 0;
2683 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
2684 io != NULL; io = (union ctl_io *)TAILQ_NEXT(
2685 &io->io_hdr, ooa_links)) {
2686 ooa_info->num_entries++;
2688 mtx_unlock(&lun->lun_lock);
2690 ooa_info->status = CTL_OOA_SUCCESS;
2694 case CTL_HARD_START:
2695 case CTL_HARD_STOP: {
2696 struct ctl_fe_ioctl_startstop_info ss_info;
2697 struct cfi_metatask *metatask;
2700 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF);
2702 cv_init(&ss_info.sem, "hard start/stop cv" );
2704 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2705 if (metatask == NULL) {
2707 mtx_destroy(&hs_mtx);
2711 if (cmd == CTL_HARD_START)
2712 metatask->tasktype = CFI_TASK_STARTUP;
2714 metatask->tasktype = CFI_TASK_SHUTDOWN;
2716 metatask->callback = ctl_ioctl_hard_startstop_callback;
2717 metatask->callback_arg = &ss_info;
2719 cfi_action(metatask);
2721 /* Wait for the callback */
2723 cv_wait_sig(&ss_info.sem, &hs_mtx);
2724 mtx_unlock(&hs_mtx);
2727 * All information has been copied from the metatask by the
2728 * time cv_broadcast() is called, so we free the metatask here.
2730 cfi_free_metatask(metatask);
2732 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info));
2734 mtx_destroy(&hs_mtx);
2738 struct ctl_bbrread_info *bbr_info;
2739 struct ctl_fe_ioctl_bbrread_info fe_bbr_info;
2741 struct cfi_metatask *metatask;
2743 bbr_info = (struct ctl_bbrread_info *)addr;
2745 bzero(&fe_bbr_info, sizeof(fe_bbr_info));
2747 bzero(&bbr_mtx, sizeof(bbr_mtx));
2748 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF);
2750 fe_bbr_info.bbr_info = bbr_info;
2751 fe_bbr_info.lock = &bbr_mtx;
2753 cv_init(&fe_bbr_info.sem, "BBR read cv");
2754 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2756 if (metatask == NULL) {
2757 mtx_destroy(&bbr_mtx);
2758 cv_destroy(&fe_bbr_info.sem);
2762 metatask->tasktype = CFI_TASK_BBRREAD;
2763 metatask->callback = ctl_ioctl_bbrread_callback;
2764 metatask->callback_arg = &fe_bbr_info;
2765 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num;
2766 metatask->taskinfo.bbrread.lba = bbr_info->lba;
2767 metatask->taskinfo.bbrread.len = bbr_info->len;
2769 cfi_action(metatask);
2772 while (fe_bbr_info.wakeup_done == 0)
2773 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx);
2774 mtx_unlock(&bbr_mtx);
2776 bbr_info->status = metatask->status;
2777 bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2778 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status;
2779 memcpy(&bbr_info->sense_data,
2780 &metatask->taskinfo.bbrread.sense_data,
2781 ctl_min(sizeof(bbr_info->sense_data),
2782 sizeof(metatask->taskinfo.bbrread.sense_data)));
2784 cfi_free_metatask(metatask);
2786 mtx_destroy(&bbr_mtx);
2787 cv_destroy(&fe_bbr_info.sem);
2791 case CTL_DELAY_IO: {
2792 struct ctl_io_delay_info *delay_info;
2794 struct ctl_lun *lun;
2795 #endif /* CTL_IO_DELAY */
2797 delay_info = (struct ctl_io_delay_info *)addr;
2800 mtx_lock(&softc->ctl_lock);
2802 if ((delay_info->lun_id >= CTL_MAX_LUNS)
2803 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2804 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2806 lun = softc->ctl_luns[delay_info->lun_id];
2807 mtx_lock(&lun->lun_lock);
2809 delay_info->status = CTL_DELAY_STATUS_OK;
2811 switch (delay_info->delay_type) {
2812 case CTL_DELAY_TYPE_CONT:
2814 case CTL_DELAY_TYPE_ONESHOT:
2817 delay_info->status =
2818 CTL_DELAY_STATUS_INVALID_TYPE;
2822 switch (delay_info->delay_loc) {
2823 case CTL_DELAY_LOC_DATAMOVE:
2824 lun->delay_info.datamove_type =
2825 delay_info->delay_type;
2826 lun->delay_info.datamove_delay =
2827 delay_info->delay_secs;
2829 case CTL_DELAY_LOC_DONE:
2830 lun->delay_info.done_type =
2831 delay_info->delay_type;
2832 lun->delay_info.done_delay =
2833 delay_info->delay_secs;
2836 delay_info->status =
2837 CTL_DELAY_STATUS_INVALID_LOC;
2840 mtx_unlock(&lun->lun_lock);
2843 mtx_unlock(&softc->ctl_lock);
2845 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2846 #endif /* CTL_IO_DELAY */
2849 case CTL_REALSYNC_SET: {
2852 syncstate = (int *)addr;
2854 mtx_lock(&softc->ctl_lock);
2855 switch (*syncstate) {
2857 softc->flags &= ~CTL_FLAG_REAL_SYNC;
2860 softc->flags |= CTL_FLAG_REAL_SYNC;
2866 mtx_unlock(&softc->ctl_lock);
2869 case CTL_REALSYNC_GET: {
2872 syncstate = (int*)addr;
2874 mtx_lock(&softc->ctl_lock);
2875 if (softc->flags & CTL_FLAG_REAL_SYNC)
2879 mtx_unlock(&softc->ctl_lock);
2885 struct ctl_sync_info *sync_info;
2886 struct ctl_lun *lun;
2888 sync_info = (struct ctl_sync_info *)addr;
2890 mtx_lock(&softc->ctl_lock);
2891 lun = softc->ctl_luns[sync_info->lun_id];
2893 mtx_unlock(&softc->ctl_lock);
2894 sync_info->status = CTL_GS_SYNC_NO_LUN;
2897 * Get or set the sync interval. We're not bounds checking
2898 * in the set case, hopefully the user won't do something
2901 mtx_lock(&lun->lun_lock);
2902 mtx_unlock(&softc->ctl_lock);
2903 if (cmd == CTL_GETSYNC)
2904 sync_info->sync_interval = lun->sync_interval;
2906 lun->sync_interval = sync_info->sync_interval;
2907 mtx_unlock(&lun->lun_lock);
2909 sync_info->status = CTL_GS_SYNC_OK;
2913 case CTL_GETSTATS: {
2914 struct ctl_stats *stats;
2915 struct ctl_lun *lun;
2918 stats = (struct ctl_stats *)addr;
2920 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2922 stats->status = CTL_SS_NEED_MORE_SPACE;
2923 stats->num_luns = softc->num_luns;
2927 * XXX KDM no locking here. If the LUN list changes,
2928 * things can blow up.
2930 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
2931 i++, lun = STAILQ_NEXT(lun, links)) {
2932 retval = copyout(&lun->stats, &stats->lun_stats[i],
2933 sizeof(lun->stats));
2937 stats->num_luns = softc->num_luns;
2938 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2940 stats->status = CTL_SS_OK;
2942 stats->flags = CTL_STATS_FLAG_TIME_VALID;
2944 stats->flags = CTL_STATS_FLAG_NONE;
2946 getnanouptime(&stats->timestamp);
2949 case CTL_ERROR_INJECT: {
2950 struct ctl_error_desc *err_desc, *new_err_desc;
2951 struct ctl_lun *lun;
2953 err_desc = (struct ctl_error_desc *)addr;
2955 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2957 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2959 mtx_lock(&softc->ctl_lock);
2960 lun = softc->ctl_luns[err_desc->lun_id];
2962 mtx_unlock(&softc->ctl_lock);
2963 free(new_err_desc, M_CTL);
2964 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2965 __func__, (uintmax_t)err_desc->lun_id);
2969 mtx_lock(&lun->lun_lock);
2970 mtx_unlock(&softc->ctl_lock);
2973 * We could do some checking here to verify the validity
2974 * of the request, but given the complexity of error
2975 * injection requests, the checking logic would be fairly
2978 * For now, if the request is invalid, it just won't get
2979 * executed and might get deleted.
2981 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2984 * XXX KDM check to make sure the serial number is unique,
2985 * in case we somehow manage to wrap. That shouldn't
2986 * happen for a very long time, but it's the right thing to
2989 new_err_desc->serial = lun->error_serial;
2990 err_desc->serial = lun->error_serial;
2991 lun->error_serial++;
2993 mtx_unlock(&lun->lun_lock);
2996 case CTL_ERROR_INJECT_DELETE: {
2997 struct ctl_error_desc *delete_desc, *desc, *desc2;
2998 struct ctl_lun *lun;
3001 delete_desc = (struct ctl_error_desc *)addr;
3004 mtx_lock(&softc->ctl_lock);
3005 lun = softc->ctl_luns[delete_desc->lun_id];
3007 mtx_unlock(&softc->ctl_lock);
3008 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
3009 __func__, (uintmax_t)delete_desc->lun_id);
3013 mtx_lock(&lun->lun_lock);
3014 mtx_unlock(&softc->ctl_lock);
3015 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
3016 if (desc->serial != delete_desc->serial)
3019 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
3024 mtx_unlock(&lun->lun_lock);
3025 if (delete_done == 0) {
3026 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
3027 "error serial %ju on LUN %u\n", __func__,
3028 delete_desc->serial, delete_desc->lun_id);
3034 case CTL_DUMP_STRUCTS: {
3036 struct ctl_port *port;
3037 struct ctl_frontend *fe;
3039 mtx_lock(&softc->ctl_lock);
3040 printf("CTL Persistent Reservation information start:\n");
3041 for (i = 0; i < CTL_MAX_LUNS; i++) {
3042 struct ctl_lun *lun;
3044 lun = softc->ctl_luns[i];
3047 || ((lun->flags & CTL_LUN_DISABLED) != 0))
3050 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) {
3051 if (lun->pr_keys[j] == NULL)
3053 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
3054 if (lun->pr_keys[j][k] == 0)
3056 printf(" LUN %d port %d iid %d key "
3058 (uintmax_t)lun->pr_keys[j][k]);
3062 printf("CTL Persistent Reservation information end\n");
3063 printf("CTL Ports:\n");
3064 STAILQ_FOREACH(port, &softc->port_list, links) {
3065 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
3066 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
3067 port->frontend->name, port->port_type,
3068 port->physical_port, port->virtual_port,
3069 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
3070 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3071 if (port->wwpn_iid[j].in_use == 0 &&
3072 port->wwpn_iid[j].wwpn == 0 &&
3073 port->wwpn_iid[j].name == NULL)
3076 printf(" iid %u use %d WWPN %#jx '%s'\n",
3077 j, port->wwpn_iid[j].in_use,
3078 (uintmax_t)port->wwpn_iid[j].wwpn,
3079 port->wwpn_iid[j].name);
3082 printf("CTL Port information end\n");
3083 mtx_unlock(&softc->ctl_lock);
3085 * XXX KDM calling this without a lock. We'd likely want
3086 * to drop the lock before calling the frontend's dump
3089 printf("CTL Frontends:\n");
3090 STAILQ_FOREACH(fe, &softc->fe_list, links) {
3091 printf(" Frontend '%s'\n", fe->name);
3092 if (fe->fe_dump != NULL)
3095 printf("CTL Frontend information end\n");
3099 struct ctl_lun_req *lun_req;
3100 struct ctl_backend_driver *backend;
3102 lun_req = (struct ctl_lun_req *)addr;
3104 backend = ctl_backend_find(lun_req->backend);
3105 if (backend == NULL) {
3106 lun_req->status = CTL_LUN_ERROR;
3107 snprintf(lun_req->error_str,
3108 sizeof(lun_req->error_str),
3109 "Backend \"%s\" not found.",
3113 if (lun_req->num_be_args > 0) {
3114 lun_req->kern_be_args = ctl_copyin_args(
3115 lun_req->num_be_args,
3118 sizeof(lun_req->error_str));
3119 if (lun_req->kern_be_args == NULL) {
3120 lun_req->status = CTL_LUN_ERROR;
3125 retval = backend->ioctl(dev, cmd, addr, flag, td);
3127 if (lun_req->num_be_args > 0) {
3128 ctl_copyout_args(lun_req->num_be_args,
3129 lun_req->kern_be_args);
3130 ctl_free_args(lun_req->num_be_args,
3131 lun_req->kern_be_args);
3135 case CTL_LUN_LIST: {
3137 struct ctl_lun *lun;
3138 struct ctl_lun_list *list;
3139 struct ctl_option *opt;
3141 list = (struct ctl_lun_list *)addr;
3144 * Allocate a fixed length sbuf here, based on the length
3145 * of the user's buffer. We could allocate an auto-extending
3146 * buffer, and then tell the user how much larger our
3147 * amount of data is than his buffer, but that presents
3150 * 1. The sbuf(9) routines use a blocking malloc, and so
3151 * we can't hold a lock while calling them with an
3152 * auto-extending buffer.
3154 * 2. There is not currently a LUN reference counting
3155 * mechanism, outside of outstanding transactions on
3156 * the LUN's OOA queue. So a LUN could go away on us
3157 * while we're getting the LUN number, backend-specific
3158 * information, etc. Thus, given the way things
3159 * currently work, we need to hold the CTL lock while
3160 * grabbing LUN information.
3162 * So, from the user's standpoint, the best thing to do is
3163 * allocate what he thinks is a reasonable buffer length,
3164 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3165 * double the buffer length and try again. (And repeat
3166 * that until he succeeds.)
3168 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3170 list->status = CTL_LUN_LIST_ERROR;
3171 snprintf(list->error_str, sizeof(list->error_str),
3172 "Unable to allocate %d bytes for LUN list",
3177 sbuf_printf(sb, "<ctllunlist>\n");
3179 mtx_lock(&softc->ctl_lock);
3180 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3181 mtx_lock(&lun->lun_lock);
3182 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3183 (uintmax_t)lun->lun);
3186 * Bail out as soon as we see that we've overfilled
3192 retval = sbuf_printf(sb, "\t<backend_type>%s"
3193 "</backend_type>\n",
3194 (lun->backend == NULL) ? "none" :
3195 lun->backend->name);
3200 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3201 lun->be_lun->lun_type);
3206 if (lun->backend == NULL) {
3207 retval = sbuf_printf(sb, "</lun>\n");
3213 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3214 (lun->be_lun->maxlba > 0) ?
3215 lun->be_lun->maxlba + 1 : 0);
3220 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3221 lun->be_lun->blocksize);
3226 retval = sbuf_printf(sb, "\t<serial_number>");
3231 retval = ctl_sbuf_printf_esc(sb,
3232 lun->be_lun->serial_num,
3233 sizeof(lun->be_lun->serial_num));
3238 retval = sbuf_printf(sb, "</serial_number>\n");
3243 retval = sbuf_printf(sb, "\t<device_id>");
3248 retval = ctl_sbuf_printf_esc(sb,
3249 lun->be_lun->device_id,
3250 sizeof(lun->be_lun->device_id));
3255 retval = sbuf_printf(sb, "</device_id>\n");
3260 if (lun->backend->lun_info != NULL) {
3261 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3265 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3266 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3267 opt->name, opt->value, opt->name);
3272 retval = sbuf_printf(sb, "</lun>\n");
3276 mtx_unlock(&lun->lun_lock);
3279 mtx_unlock(&lun->lun_lock);
3280 mtx_unlock(&softc->ctl_lock);
3283 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3286 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3287 snprintf(list->error_str, sizeof(list->error_str),
3288 "Out of space, %d bytes is too small",
3295 retval = copyout(sbuf_data(sb), list->lun_xml,
3298 list->fill_len = sbuf_len(sb) + 1;
3299 list->status = CTL_LUN_LIST_OK;
3304 struct ctl_iscsi *ci;
3305 struct ctl_frontend *fe;
3307 ci = (struct ctl_iscsi *)addr;
3309 fe = ctl_frontend_find("iscsi");
3311 ci->status = CTL_ISCSI_ERROR;
3312 snprintf(ci->error_str, sizeof(ci->error_str),
3313 "Frontend \"iscsi\" not found.");
3317 retval = fe->ioctl(dev, cmd, addr, flag, td);
3320 case CTL_PORT_REQ: {
3321 struct ctl_req *req;
3322 struct ctl_frontend *fe;
3324 req = (struct ctl_req *)addr;
3326 fe = ctl_frontend_find(req->driver);
3328 req->status = CTL_LUN_ERROR;
3329 snprintf(req->error_str, sizeof(req->error_str),
3330 "Frontend \"%s\" not found.", req->driver);
3333 if (req->num_args > 0) {
3334 req->kern_args = ctl_copyin_args(req->num_args,
3335 req->args, req->error_str, sizeof(req->error_str));
3336 if (req->kern_args == NULL) {
3337 req->status = CTL_LUN_ERROR;
3342 retval = fe->ioctl(dev, cmd, addr, flag, td);
3344 if (req->num_args > 0) {
3345 ctl_copyout_args(req->num_args, req->kern_args);
3346 ctl_free_args(req->num_args, req->kern_args);
3350 case CTL_PORT_LIST: {
3352 struct ctl_port *port;
3353 struct ctl_lun_list *list;
3354 struct ctl_option *opt;
3357 list = (struct ctl_lun_list *)addr;
3359 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3361 list->status = CTL_LUN_LIST_ERROR;
3362 snprintf(list->error_str, sizeof(list->error_str),
3363 "Unable to allocate %d bytes for LUN list",
3368 sbuf_printf(sb, "<ctlportlist>\n");
3370 mtx_lock(&softc->ctl_lock);
3371 STAILQ_FOREACH(port, &softc->port_list, links) {
3372 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3373 (uintmax_t)port->targ_port);
3376 * Bail out as soon as we see that we've overfilled
3382 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3383 "</frontend_type>\n", port->frontend->name);
3387 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3392 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3393 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3397 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3402 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3403 port->physical_port);
3407 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3408 port->virtual_port);
3412 if (port->target_devid != NULL) {
3413 sbuf_printf(sb, "\t<target>");
3414 ctl_id_sbuf(port->target_devid, sb);
3415 sbuf_printf(sb, "</target>\n");
3418 if (port->port_devid != NULL) {
3419 sbuf_printf(sb, "\t<port>");
3420 ctl_id_sbuf(port->port_devid, sb);
3421 sbuf_printf(sb, "</port>\n");
3424 if (port->port_info != NULL) {
3425 retval = port->port_info(port->onoff_arg, sb);
3429 STAILQ_FOREACH(opt, &port->options, links) {
3430 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3431 opt->name, opt->value, opt->name);
3436 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3437 if (port->wwpn_iid[j].in_use == 0 ||
3438 (port->wwpn_iid[j].wwpn == 0 &&
3439 port->wwpn_iid[j].name == NULL))
3442 if (port->wwpn_iid[j].name != NULL)
3443 retval = sbuf_printf(sb,
3444 "\t<initiator>%u %s</initiator>\n",
3445 j, port->wwpn_iid[j].name);
3447 retval = sbuf_printf(sb,
3448 "\t<initiator>%u naa.%08jx</initiator>\n",
3449 j, port->wwpn_iid[j].wwpn);
3456 retval = sbuf_printf(sb, "</targ_port>\n");
3460 mtx_unlock(&softc->ctl_lock);
3463 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3466 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3467 snprintf(list->error_str, sizeof(list->error_str),
3468 "Out of space, %d bytes is too small",
3475 retval = copyout(sbuf_data(sb), list->lun_xml,
3478 list->fill_len = sbuf_len(sb) + 1;
3479 list->status = CTL_LUN_LIST_OK;
3484 /* XXX KDM should we fix this? */
3486 struct ctl_backend_driver *backend;
3493 * We encode the backend type as the ioctl type for backend
3494 * ioctls. So parse it out here, and then search for a
3495 * backend of this type.
3497 type = _IOC_TYPE(cmd);
3499 STAILQ_FOREACH(backend, &softc->be_list, links) {
3500 if (backend->type == type) {
3506 printf("ctl: unknown ioctl command %#lx or backend "
3511 retval = backend->ioctl(dev, cmd, addr, flag, td);
3521 ctl_get_initindex(struct ctl_nexus *nexus)
3523 if (nexus->targ_port < CTL_MAX_PORTS)
3524 return (nexus->initid.id +
3525 (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3527 return (nexus->initid.id +
3528 ((nexus->targ_port - CTL_MAX_PORTS) *
3529 CTL_MAX_INIT_PER_PORT));
3533 ctl_get_resindex(struct ctl_nexus *nexus)
3535 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3539 ctl_port_idx(int port_num)
3541 if (port_num < CTL_MAX_PORTS)
3544 return(port_num - CTL_MAX_PORTS);
3548 ctl_map_lun(int port_num, uint32_t lun_id)
3550 struct ctl_port *port;
3552 port = control_softc->ctl_ports[ctl_port_idx(port_num)];
3554 return (UINT32_MAX);
3555 if (port->lun_map == NULL)
3557 return (port->lun_map(port->targ_lun_arg, lun_id));
3561 ctl_map_lun_back(int port_num, uint32_t lun_id)
3563 struct ctl_port *port;
3566 port = control_softc->ctl_ports[ctl_port_idx(port_num)];
3567 if (port->lun_map == NULL)
3569 for (i = 0; i < CTL_MAX_LUNS; i++) {
3570 if (port->lun_map(port->targ_lun_arg, i) == lun_id)
3573 return (UINT32_MAX);
3577 * Note: This only works for bitmask sizes that are at least 32 bits, and
3578 * that are a power of 2.
3581 ctl_ffz(uint32_t *mask, uint32_t size)
3583 uint32_t num_chunks, num_pieces;
3586 num_chunks = (size >> 5);
3587 if (num_chunks == 0)
3589 num_pieces = ctl_min((sizeof(uint32_t) * 8), size);
3591 for (i = 0; i < num_chunks; i++) {
3592 for (j = 0; j < num_pieces; j++) {
3593 if ((mask[i] & (1 << j)) == 0)
3594 return ((i << 5) + j);
3602 ctl_set_mask(uint32_t *mask, uint32_t bit)
3604 uint32_t chunk, piece;
3607 piece = bit % (sizeof(uint32_t) * 8);
3609 if ((mask[chunk] & (1 << piece)) != 0)
3612 mask[chunk] |= (1 << piece);
3618 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3620 uint32_t chunk, piece;
3623 piece = bit % (sizeof(uint32_t) * 8);
3625 if ((mask[chunk] & (1 << piece)) == 0)
3628 mask[chunk] &= ~(1 << piece);
3634 ctl_is_set(uint32_t *mask, uint32_t bit)
3636 uint32_t chunk, piece;
3639 piece = bit % (sizeof(uint32_t) * 8);
3641 if ((mask[chunk] & (1 << piece)) == 0)
3648 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3652 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3655 return (t[residx % CTL_MAX_INIT_PER_PORT]);
3659 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3663 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3666 t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3670 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3675 i = residx/CTL_MAX_INIT_PER_PORT;
3676 if (lun->pr_keys[i] != NULL)
3678 mtx_unlock(&lun->lun_lock);
3679 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3681 mtx_lock(&lun->lun_lock);
3682 if (lun->pr_keys[i] == NULL)
3683 lun->pr_keys[i] = p;
3689 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3693 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3694 KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3695 t[residx % CTL_MAX_INIT_PER_PORT] = key;
3699 * ctl_softc, pool_name, total_ctl_io are passed in.
3700 * npool is passed out.
3703 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3704 uint32_t total_ctl_io, void **npool)
3707 struct ctl_io_pool *pool;
3709 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3714 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3715 pool->ctl_softc = ctl_softc;
3716 pool->zone = uma_zsecond_create(pool->name, NULL,
3717 NULL, NULL, NULL, ctl_softc->io_zone);
3718 /* uma_prealloc(pool->zone, total_ctl_io); */
3722 *npool = ctl_softc->io_zone;
3728 ctl_pool_free(struct ctl_io_pool *pool)
3735 uma_zdestroy(pool->zone);
3741 ctl_alloc_io(void *pool_ref)
3745 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3747 io = uma_zalloc(pool->zone, M_WAITOK);
3749 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK);
3752 io->io_hdr.pool = pool_ref;
3757 ctl_alloc_io_nowait(void *pool_ref)
3761 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3763 io = uma_zalloc(pool->zone, M_NOWAIT);
3765 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT);
3768 io->io_hdr.pool = pool_ref;
3773 ctl_free_io(union ctl_io *io)
3776 struct ctl_io_pool *pool;
3783 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3784 uma_zfree(pool->zone, io);
3786 uma_zfree((uma_zone_t)io->io_hdr.pool, io);
3791 ctl_zero_io(union ctl_io *io)
3799 * May need to preserve linked list pointers at some point too.
3801 pool_ref = io->io_hdr.pool;
3802 memset(io, 0, sizeof(*io));
3803 io->io_hdr.pool = pool_ref;
3807 * This routine is currently used for internal copies of ctl_ios that need
3808 * to persist for some reason after we've already returned status to the
3809 * FETD. (Thus the flag set.)
3812 * Note that this makes a blind copy of all fields in the ctl_io, except
3813 * for the pool reference. This includes any memory that has been
3814 * allocated! That memory will no longer be valid after done has been
3815 * called, so this would be VERY DANGEROUS for command that actually does
3816 * any reads or writes. Right now (11/7/2005), this is only used for immediate
3817 * start and stop commands, which don't transfer any data, so this is not a
3818 * problem. If it is used for anything else, the caller would also need to
3819 * allocate data buffer space and this routine would need to be modified to
3820 * copy the data buffer(s) as well.
3823 ctl_copy_io(union ctl_io *src, union ctl_io *dest)
3832 * May need to preserve linked list pointers at some point too.
3834 pool_ref = dest->io_hdr.pool;
3836 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest)));
3838 dest->io_hdr.pool = pool_ref;
3840 * We need to know that this is an internal copy, and doesn't need
3841 * to get passed back to the FETD that allocated it.
3843 dest->io_hdr.flags |= CTL_FLAG_INT_COPY;
3847 ctl_expand_number(const char *buf, uint64_t *num)
3853 number = strtoq(buf, &endptr, 0);
3855 switch (tolower((unsigned char)*endptr)) {
3875 case '\0': /* No unit. */
3879 /* Unrecognized unit. */
3883 if ((number << shift) >> shift != number) {
3887 *num = number << shift;
3893 * This routine could be used in the future to load default and/or saved
3894 * mode page parameters for a particuar lun.
3897 ctl_init_page_index(struct ctl_lun *lun)
3900 struct ctl_page_index *page_index;
3904 memcpy(&lun->mode_pages.index, page_index_template,
3905 sizeof(page_index_template));
3907 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3909 page_index = &lun->mode_pages.index[i];
3911 * If this is a disk-only mode page, there's no point in
3912 * setting it up. For some pages, we have to have some
3913 * basic information about the disk in order to calculate the
3916 if ((lun->be_lun->lun_type != T_DIRECT)
3917 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
3920 switch (page_index->page_code & SMPH_PC_MASK) {
3921 case SMS_RW_ERROR_RECOVERY_PAGE: {
3922 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3923 panic("subpage is incorrect!");
3924 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
3925 &rw_er_page_default,
3926 sizeof(rw_er_page_default));
3927 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
3928 &rw_er_page_changeable,
3929 sizeof(rw_er_page_changeable));
3930 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
3931 &rw_er_page_default,
3932 sizeof(rw_er_page_default));
3933 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
3934 &rw_er_page_default,
3935 sizeof(rw_er_page_default));
3936 page_index->page_data =
3937 (uint8_t *)lun->mode_pages.rw_er_page;
3940 case SMS_FORMAT_DEVICE_PAGE: {
3941 struct scsi_format_page *format_page;
3943 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3944 panic("subpage is incorrect!");
3947 * Sectors per track are set above. Bytes per
3948 * sector need to be set here on a per-LUN basis.
3950 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
3951 &format_page_default,
3952 sizeof(format_page_default));
3953 memcpy(&lun->mode_pages.format_page[
3954 CTL_PAGE_CHANGEABLE], &format_page_changeable,
3955 sizeof(format_page_changeable));
3956 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
3957 &format_page_default,
3958 sizeof(format_page_default));
3959 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
3960 &format_page_default,
3961 sizeof(format_page_default));
3963 format_page = &lun->mode_pages.format_page[
3965 scsi_ulto2b(lun->be_lun->blocksize,
3966 format_page->bytes_per_sector);
3968 format_page = &lun->mode_pages.format_page[
3970 scsi_ulto2b(lun->be_lun->blocksize,
3971 format_page->bytes_per_sector);
3973 format_page = &lun->mode_pages.format_page[
3975 scsi_ulto2b(lun->be_lun->blocksize,
3976 format_page->bytes_per_sector);
3978 page_index->page_data =
3979 (uint8_t *)lun->mode_pages.format_page;
3982 case SMS_RIGID_DISK_PAGE: {
3983 struct scsi_rigid_disk_page *rigid_disk_page;
3984 uint32_t sectors_per_cylinder;
3988 #endif /* !__XSCALE__ */
3990 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3991 panic("invalid subpage value %d",
3992 page_index->subpage);
3995 * Rotation rate and sectors per track are set
3996 * above. We calculate the cylinders here based on
3997 * capacity. Due to the number of heads and
3998 * sectors per track we're using, smaller arrays
3999 * may turn out to have 0 cylinders. Linux and
4000 * FreeBSD don't pay attention to these mode pages
4001 * to figure out capacity, but Solaris does. It
4002 * seems to deal with 0 cylinders just fine, and
4003 * works out a fake geometry based on the capacity.
4005 memcpy(&lun->mode_pages.rigid_disk_page[
4006 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4007 sizeof(rigid_disk_page_default));
4008 memcpy(&lun->mode_pages.rigid_disk_page[
4009 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4010 sizeof(rigid_disk_page_changeable));
4012 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4016 * The divide method here will be more accurate,
4017 * probably, but results in floating point being
4018 * used in the kernel on i386 (__udivdi3()). On the
4019 * XScale, though, __udivdi3() is implemented in
4022 * The shift method for cylinder calculation is
4023 * accurate if sectors_per_cylinder is a power of
4024 * 2. Otherwise it might be slightly off -- you
4025 * might have a bit of a truncation problem.
4028 cylinders = (lun->be_lun->maxlba + 1) /
4029 sectors_per_cylinder;
4031 for (shift = 31; shift > 0; shift--) {
4032 if (sectors_per_cylinder & (1 << shift))
4035 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4039 * We've basically got 3 bytes, or 24 bits for the
4040 * cylinder size in the mode page. If we're over,
4041 * just round down to 2^24.
4043 if (cylinders > 0xffffff)
4044 cylinders = 0xffffff;
4046 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4048 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4050 if ((value = ctl_get_opt(&lun->be_lun->options,
4052 scsi_ulto2b(strtol(value, NULL, 0),
4053 rigid_disk_page->rotation_rate);
4056 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
4057 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4058 sizeof(rigid_disk_page_default));
4059 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
4060 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4061 sizeof(rigid_disk_page_default));
4063 page_index->page_data =
4064 (uint8_t *)lun->mode_pages.rigid_disk_page;
4067 case SMS_CACHING_PAGE: {
4068 struct scsi_caching_page *caching_page;
4070 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4071 panic("invalid subpage value %d",
4072 page_index->subpage);
4073 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4074 &caching_page_default,
4075 sizeof(caching_page_default));
4076 memcpy(&lun->mode_pages.caching_page[
4077 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4078 sizeof(caching_page_changeable));
4079 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4080 &caching_page_default,
4081 sizeof(caching_page_default));
4082 caching_page = &lun->mode_pages.caching_page[
4084 value = ctl_get_opt(&lun->be_lun->options, "writecache");
4085 if (value != NULL && strcmp(value, "off") == 0)
4086 caching_page->flags1 &= ~SCP_WCE;
4087 value = ctl_get_opt(&lun->be_lun->options, "readcache");
4088 if (value != NULL && strcmp(value, "off") == 0)
4089 caching_page->flags1 |= SCP_RCD;
4090 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4091 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4092 sizeof(caching_page_default));
4093 page_index->page_data =
4094 (uint8_t *)lun->mode_pages.caching_page;
4097 case SMS_CONTROL_MODE_PAGE: {
4098 struct scsi_control_page *control_page;
4100 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4101 panic("invalid subpage value %d",
4102 page_index->subpage);
4104 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT],
4105 &control_page_default,
4106 sizeof(control_page_default));
4107 memcpy(&lun->mode_pages.control_page[
4108 CTL_PAGE_CHANGEABLE], &control_page_changeable,
4109 sizeof(control_page_changeable));
4110 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED],
4111 &control_page_default,
4112 sizeof(control_page_default));
4113 control_page = &lun->mode_pages.control_page[
4115 value = ctl_get_opt(&lun->be_lun->options, "reordering");
4116 if (value != NULL && strcmp(value, "unrestricted") == 0) {
4117 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK;
4118 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED;
4120 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT],
4121 &lun->mode_pages.control_page[CTL_PAGE_SAVED],
4122 sizeof(control_page_default));
4123 page_index->page_data =
4124 (uint8_t *)lun->mode_pages.control_page;
4128 case SMS_INFO_EXCEPTIONS_PAGE: {
4129 switch (page_index->subpage) {
4130 case SMS_SUBPAGE_PAGE_0:
4131 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4133 sizeof(ie_page_default));
4134 memcpy(&lun->mode_pages.ie_page[
4135 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4136 sizeof(ie_page_changeable));
4137 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4139 sizeof(ie_page_default));
4140 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4142 sizeof(ie_page_default));
4143 page_index->page_data =
4144 (uint8_t *)lun->mode_pages.ie_page;
4147 struct ctl_logical_block_provisioning_page *page;
4149 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4151 sizeof(lbp_page_default));
4152 memcpy(&lun->mode_pages.lbp_page[
4153 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4154 sizeof(lbp_page_changeable));
4155 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4157 sizeof(lbp_page_default));
4158 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4159 value = ctl_get_opt(&lun->be_lun->options,
4161 if (value != NULL &&
4162 ctl_expand_number(value, &ival) == 0) {
4163 page->descr[0].flags |= SLBPPD_ENABLED |
4165 if (lun->be_lun->blocksize)
4166 ival /= lun->be_lun->blocksize;
4169 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4170 page->descr[0].count);
4172 value = ctl_get_opt(&lun->be_lun->options,
4174 if (value != NULL &&
4175 ctl_expand_number(value, &ival) == 0) {
4176 page->descr[1].flags |= SLBPPD_ENABLED |
4178 if (lun->be_lun->blocksize)
4179 ival /= lun->be_lun->blocksize;
4182 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4183 page->descr[1].count);
4185 value = ctl_get_opt(&lun->be_lun->options,
4186 "pool-avail-threshold");
4187 if (value != NULL &&
4188 ctl_expand_number(value, &ival) == 0) {
4189 page->descr[2].flags |= SLBPPD_ENABLED |
4191 if (lun->be_lun->blocksize)
4192 ival /= lun->be_lun->blocksize;
4195 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4196 page->descr[2].count);
4198 value = ctl_get_opt(&lun->be_lun->options,
4199 "pool-used-threshold");
4200 if (value != NULL &&
4201 ctl_expand_number(value, &ival) == 0) {
4202 page->descr[3].flags |= SLBPPD_ENABLED |
4204 if (lun->be_lun->blocksize)
4205 ival /= lun->be_lun->blocksize;
4208 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4209 page->descr[3].count);
4211 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4212 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4213 sizeof(lbp_page_default));
4214 page_index->page_data =
4215 (uint8_t *)lun->mode_pages.lbp_page;
4219 case SMS_VENDOR_SPECIFIC_PAGE:{
4220 switch (page_index->subpage) {
4221 case DBGCNF_SUBPAGE_CODE: {
4222 struct copan_debugconf_subpage *current_page,
4225 memcpy(&lun->mode_pages.debugconf_subpage[
4227 &debugconf_page_default,
4228 sizeof(debugconf_page_default));
4229 memcpy(&lun->mode_pages.debugconf_subpage[
4230 CTL_PAGE_CHANGEABLE],
4231 &debugconf_page_changeable,
4232 sizeof(debugconf_page_changeable));
4233 memcpy(&lun->mode_pages.debugconf_subpage[
4235 &debugconf_page_default,
4236 sizeof(debugconf_page_default));
4237 memcpy(&lun->mode_pages.debugconf_subpage[
4239 &debugconf_page_default,
4240 sizeof(debugconf_page_default));
4241 page_index->page_data =
4242 (uint8_t *)lun->mode_pages.debugconf_subpage;
4244 current_page = (struct copan_debugconf_subpage *)
4245 (page_index->page_data +
4246 (page_index->page_len *
4248 saved_page = (struct copan_debugconf_subpage *)
4249 (page_index->page_data +
4250 (page_index->page_len *
4255 panic("invalid subpage value %d",
4256 page_index->subpage);
4262 panic("invalid page value %d",
4263 page_index->page_code & SMPH_PC_MASK);
4268 return (CTL_RETVAL_COMPLETE);
4272 ctl_init_log_page_index(struct ctl_lun *lun)
4274 struct ctl_page_index *page_index;
4277 memcpy(&lun->log_pages.index, log_page_index_template,
4278 sizeof(log_page_index_template));
4281 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4283 page_index = &lun->log_pages.index[i];
4285 * If this is a disk-only mode page, there's no point in
4286 * setting it up. For some pages, we have to have some
4287 * basic information about the disk in order to calculate the
4290 if ((lun->be_lun->lun_type != T_DIRECT)
4291 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
4294 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4295 ((lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) == 0 ||
4296 lun->backend->lun_attr == NULL))
4299 if (page_index->page_code != prev) {
4300 lun->log_pages.pages_page[j] = page_index->page_code;
4301 prev = page_index->page_code;
4304 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4305 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4308 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4309 lun->log_pages.index[0].page_len = j;
4310 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4311 lun->log_pages.index[1].page_len = k * 2;
4312 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4313 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4315 return (CTL_RETVAL_COMPLETE);
4319 hex2bin(const char *str, uint8_t *buf, int buf_size)
4324 memset(buf, 0, buf_size);
4325 while (isspace(str[0]))
4327 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4330 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4334 else if (isalpha(c))
4335 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4341 buf[i / 2] |= (c << 4);
4345 return ((i + 1) / 2);
4352 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4353 * wants us to allocate the LUN and he can block.
4354 * - ctl_softc is always set
4355 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4357 * Returns 0 for success, non-zero (errno) for failure.
4360 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4361 struct ctl_be_lun *const be_lun, struct ctl_id target_id)
4363 struct ctl_lun *nlun, *lun;
4364 struct ctl_port *port;
4365 struct scsi_vpd_id_descriptor *desc;
4366 struct scsi_vpd_id_t10 *t10id;
4367 const char *eui, *naa, *scsiname, *vendor, *value;
4368 int lun_number, i, lun_malloced;
4369 int devidlen, idlen1, idlen2 = 0, len;
4375 * We currently only support Direct Access or Processor LUN types.
4377 switch (be_lun->lun_type) {
4385 be_lun->lun_config_status(be_lun->be_lun,
4386 CTL_LUN_CONFIG_FAILURE);
4389 if (ctl_lun == NULL) {
4390 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4397 memset(lun, 0, sizeof(*lun));
4399 lun->flags = CTL_LUN_MALLOCED;
4401 /* Generate LUN ID. */
4402 devidlen = max(CTL_DEVID_MIN_LEN,
4403 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4404 idlen1 = sizeof(*t10id) + devidlen;
4405 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4406 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4407 if (scsiname != NULL) {
4408 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4409 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4411 eui = ctl_get_opt(&be_lun->options, "eui");
4413 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4415 naa = ctl_get_opt(&be_lun->options, "naa");
4417 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4419 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4420 M_CTL, M_WAITOK | M_ZERO);
4421 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4422 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4423 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4424 desc->length = idlen1;
4425 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4426 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4427 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4428 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4430 strncpy(t10id->vendor, vendor,
4431 min(sizeof(t10id->vendor), strlen(vendor)));
4433 strncpy((char *)t10id->vendor_spec_id,
4434 (char *)be_lun->device_id, devidlen);
4435 if (scsiname != NULL) {
4436 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4438 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4439 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4440 SVPD_ID_TYPE_SCSI_NAME;
4441 desc->length = idlen2;
4442 strlcpy(desc->identifier, scsiname, idlen2);
4445 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4447 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4448 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4450 desc->length = hex2bin(eui, desc->identifier, 16);
4451 desc->length = desc->length > 12 ? 16 :
4452 (desc->length > 8 ? 12 : 8);
4453 len -= 16 - desc->length;
4456 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4458 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4459 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4461 desc->length = hex2bin(naa, desc->identifier, 16);
4462 desc->length = desc->length > 8 ? 16 : 8;
4463 len -= 16 - desc->length;
4465 lun->lun_devid->len = len;
4467 mtx_lock(&ctl_softc->ctl_lock);
4469 * See if the caller requested a particular LUN number. If so, see
4470 * if it is available. Otherwise, allocate the first available LUN.
4472 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4473 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4474 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4475 mtx_unlock(&ctl_softc->ctl_lock);
4476 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4477 printf("ctl: requested LUN ID %d is higher "
4478 "than CTL_MAX_LUNS - 1 (%d)\n",
4479 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4482 * XXX KDM return an error, or just assign
4483 * another LUN ID in this case??
4485 printf("ctl: requested LUN ID %d is already "
4486 "in use\n", be_lun->req_lun_id);
4488 if (lun->flags & CTL_LUN_MALLOCED)
4490 be_lun->lun_config_status(be_lun->be_lun,
4491 CTL_LUN_CONFIG_FAILURE);
4494 lun_number = be_lun->req_lun_id;
4496 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS);
4497 if (lun_number == -1) {
4498 mtx_unlock(&ctl_softc->ctl_lock);
4499 printf("ctl: can't allocate LUN on target %ju, out of "
4500 "LUNs\n", (uintmax_t)target_id.id);
4501 if (lun->flags & CTL_LUN_MALLOCED)
4503 be_lun->lun_config_status(be_lun->be_lun,
4504 CTL_LUN_CONFIG_FAILURE);
4508 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4510 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4511 lun->target = target_id;
4512 lun->lun = lun_number;
4513 lun->be_lun = be_lun;
4515 * The processor LUN is always enabled. Disk LUNs come on line
4516 * disabled, and must be enabled by the backend.
4518 lun->flags |= CTL_LUN_DISABLED;
4519 lun->backend = be_lun->be;
4520 be_lun->ctl_lun = lun;
4521 be_lun->lun_id = lun_number;
4522 atomic_add_int(&be_lun->be->num_luns, 1);
4523 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE)
4524 lun->flags |= CTL_LUN_OFFLINE;
4526 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4527 lun->flags |= CTL_LUN_STOPPED;
4529 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4530 lun->flags |= CTL_LUN_INOPERABLE;
4532 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4533 lun->flags |= CTL_LUN_PRIMARY_SC;
4535 value = ctl_get_opt(&be_lun->options, "readonly");
4536 if (value != NULL && strcmp(value, "on") == 0)
4537 lun->flags |= CTL_LUN_READONLY;
4539 lun->ctl_softc = ctl_softc;
4540 TAILQ_INIT(&lun->ooa_queue);
4541 TAILQ_INIT(&lun->blocked_queue);
4542 STAILQ_INIT(&lun->error_list);
4543 ctl_tpc_lun_init(lun);
4546 * Initialize the mode and log page index.
4548 ctl_init_page_index(lun);
4549 ctl_init_log_page_index(lun);
4552 * Set the poweron UA for all initiators on this LUN only.
4554 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4555 lun->pending_ua[i] = CTL_UA_POWERON;
4558 * Now, before we insert this lun on the lun list, set the lun
4559 * inventory changed UA for all other luns.
4561 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4562 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4563 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE;
4567 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4569 ctl_softc->ctl_luns[lun_number] = lun;
4571 ctl_softc->num_luns++;
4573 /* Setup statistics gathering */
4574 lun->stats.device_type = be_lun->lun_type;
4575 lun->stats.lun_number = lun_number;
4576 if (lun->stats.device_type == T_DIRECT)
4577 lun->stats.blocksize = be_lun->blocksize;
4579 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4580 for (i = 0;i < CTL_MAX_PORTS;i++)
4581 lun->stats.ports[i].targ_port = i;
4583 mtx_unlock(&ctl_softc->ctl_lock);
4585 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4588 * Run through each registered FETD and bring it online if it isn't
4589 * already. Enable the target ID if it hasn't been enabled, and
4590 * enable this particular LUN.
4592 STAILQ_FOREACH(port, &ctl_softc->port_list, links) {
4595 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number);
4597 printf("ctl_alloc_lun: FETD %s port %d returned error "
4598 "%d for lun_enable on target %ju lun %d\n",
4599 port->port_name, port->targ_port, retval,
4600 (uintmax_t)target_id.id, lun_number);
4602 port->status |= CTL_PORT_STATUS_LUN_ONLINE;
4610 * - LUN has already been marked invalid and any pending I/O has been taken
4614 ctl_free_lun(struct ctl_lun *lun)
4616 struct ctl_softc *softc;
4618 struct ctl_port *port;
4620 struct ctl_lun *nlun;
4623 softc = lun->ctl_softc;
4625 mtx_assert(&softc->ctl_lock, MA_OWNED);
4627 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4629 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4631 softc->ctl_luns[lun->lun] = NULL;
4633 if (!TAILQ_EMPTY(&lun->ooa_queue))
4634 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4639 * XXX KDM this scheme only works for a single target/multiple LUN
4640 * setup. It needs to be revamped for a multiple target scheme.
4642 * XXX KDM this results in port->lun_disable() getting called twice,
4643 * once when ctl_disable_lun() is called, and a second time here.
4644 * We really need to re-think the LUN disable semantics. There
4645 * should probably be several steps/levels to LUN removal:
4650 * Right now we only have a disable method when communicating to
4651 * the front end ports, at least for individual LUNs.
4654 STAILQ_FOREACH(port, &softc->port_list, links) {
4657 retval = port->lun_disable(port->targ_lun_arg, lun->target,
4660 printf("ctl_free_lun: FETD %s port %d returned error "
4661 "%d for lun_disable on target %ju lun %jd\n",
4662 port->port_name, port->targ_port, retval,
4663 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4666 if (STAILQ_FIRST(&softc->lun_list) == NULL) {
4667 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE;
4669 retval = port->targ_disable(port->targ_lun_arg,lun->target);
4671 printf("ctl_free_lun: FETD %s port %d "
4672 "returned error %d for targ_disable on "
4673 "target %ju\n", port->port_name,
4674 port->targ_port, retval,
4675 (uintmax_t)lun->target.id);
4677 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE;
4679 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0)
4683 port->port_offline(port->onoff_arg);
4684 port->status &= ~CTL_PORT_STATUS_ONLINE;
4691 * Tell the backend to free resources, if this LUN has a backend.
4693 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4694 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4696 ctl_tpc_lun_shutdown(lun);
4697 mtx_destroy(&lun->lun_lock);
4698 free(lun->lun_devid, M_CTL);
4699 for (i = 0; i < 2 * CTL_MAX_PORTS; i++) {
4700 if (lun->pr_keys[i] != NULL)
4701 free(lun->pr_keys[i], M_CTL);
4703 free(lun->write_buffer, M_CTL);
4704 if (lun->flags & CTL_LUN_MALLOCED)
4707 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4708 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4709 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE;
4717 ctl_create_lun(struct ctl_be_lun *be_lun)
4719 struct ctl_softc *ctl_softc;
4721 ctl_softc = control_softc;
4724 * ctl_alloc_lun() should handle all potential failure cases.
4726 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target);
4730 ctl_add_lun(struct ctl_be_lun *be_lun)
4732 struct ctl_softc *ctl_softc = control_softc;
4734 mtx_lock(&ctl_softc->ctl_lock);
4735 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links);
4736 mtx_unlock(&ctl_softc->ctl_lock);
4737 wakeup(&ctl_softc->pending_lun_queue);
4743 ctl_enable_lun(struct ctl_be_lun *be_lun)
4745 struct ctl_softc *ctl_softc;
4746 struct ctl_port *port, *nport;
4747 struct ctl_lun *lun;
4750 ctl_softc = control_softc;
4752 lun = (struct ctl_lun *)be_lun->ctl_lun;
4754 mtx_lock(&ctl_softc->ctl_lock);
4755 mtx_lock(&lun->lun_lock);
4756 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4758 * eh? Why did we get called if the LUN is already
4761 mtx_unlock(&lun->lun_lock);
4762 mtx_unlock(&ctl_softc->ctl_lock);
4765 lun->flags &= ~CTL_LUN_DISABLED;
4766 mtx_unlock(&lun->lun_lock);
4768 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) {
4769 nport = STAILQ_NEXT(port, links);
4772 * Drop the lock while we call the FETD's enable routine.
4773 * This can lead to a callback into CTL (at least in the
4774 * case of the internal initiator frontend.
4776 mtx_unlock(&ctl_softc->ctl_lock);
4777 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun);
4778 mtx_lock(&ctl_softc->ctl_lock);
4780 printf("%s: FETD %s port %d returned error "
4781 "%d for lun_enable on target %ju lun %jd\n",
4782 __func__, port->port_name, port->targ_port, retval,
4783 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4787 /* NOTE: TODO: why does lun enable affect port status? */
4788 port->status |= CTL_PORT_STATUS_LUN_ONLINE;
4793 mtx_unlock(&ctl_softc->ctl_lock);
4799 ctl_disable_lun(struct ctl_be_lun *be_lun)
4801 struct ctl_softc *ctl_softc;
4802 struct ctl_port *port;
4803 struct ctl_lun *lun;
4806 ctl_softc = control_softc;
4808 lun = (struct ctl_lun *)be_lun->ctl_lun;
4810 mtx_lock(&ctl_softc->ctl_lock);
4811 mtx_lock(&lun->lun_lock);
4812 if (lun->flags & CTL_LUN_DISABLED) {
4813 mtx_unlock(&lun->lun_lock);
4814 mtx_unlock(&ctl_softc->ctl_lock);
4817 lun->flags |= CTL_LUN_DISABLED;
4818 mtx_unlock(&lun->lun_lock);
4820 STAILQ_FOREACH(port, &ctl_softc->port_list, links) {
4821 mtx_unlock(&ctl_softc->ctl_lock);
4823 * Drop the lock before we call the frontend's disable
4824 * routine, to avoid lock order reversals.
4826 * XXX KDM what happens if the frontend list changes while
4827 * we're traversing it? It's unlikely, but should be handled.
4829 retval = port->lun_disable(port->targ_lun_arg, lun->target,
4831 mtx_lock(&ctl_softc->ctl_lock);
4833 printf("ctl_alloc_lun: FETD %s port %d returned error "
4834 "%d for lun_disable on target %ju lun %jd\n",
4835 port->port_name, port->targ_port, retval,
4836 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4840 mtx_unlock(&ctl_softc->ctl_lock);
4846 ctl_start_lun(struct ctl_be_lun *be_lun)
4848 struct ctl_softc *ctl_softc;
4849 struct ctl_lun *lun;
4851 ctl_softc = control_softc;
4853 lun = (struct ctl_lun *)be_lun->ctl_lun;
4855 mtx_lock(&lun->lun_lock);
4856 lun->flags &= ~CTL_LUN_STOPPED;
4857 mtx_unlock(&lun->lun_lock);
4863 ctl_stop_lun(struct ctl_be_lun *be_lun)
4865 struct ctl_softc *ctl_softc;
4866 struct ctl_lun *lun;
4868 ctl_softc = control_softc;
4870 lun = (struct ctl_lun *)be_lun->ctl_lun;
4872 mtx_lock(&lun->lun_lock);
4873 lun->flags |= CTL_LUN_STOPPED;
4874 mtx_unlock(&lun->lun_lock);
4880 ctl_lun_offline(struct ctl_be_lun *be_lun)
4882 struct ctl_softc *ctl_softc;
4883 struct ctl_lun *lun;
4885 ctl_softc = control_softc;
4887 lun = (struct ctl_lun *)be_lun->ctl_lun;
4889 mtx_lock(&lun->lun_lock);
4890 lun->flags |= CTL_LUN_OFFLINE;
4891 mtx_unlock(&lun->lun_lock);
4897 ctl_lun_online(struct ctl_be_lun *be_lun)
4899 struct ctl_softc *ctl_softc;
4900 struct ctl_lun *lun;
4902 ctl_softc = control_softc;
4904 lun = (struct ctl_lun *)be_lun->ctl_lun;
4906 mtx_lock(&lun->lun_lock);
4907 lun->flags &= ~CTL_LUN_OFFLINE;
4908 mtx_unlock(&lun->lun_lock);
4914 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4916 struct ctl_softc *ctl_softc;
4917 struct ctl_lun *lun;
4919 ctl_softc = control_softc;
4921 lun = (struct ctl_lun *)be_lun->ctl_lun;
4923 mtx_lock(&lun->lun_lock);
4926 * The LUN needs to be disabled before it can be marked invalid.
4928 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4929 mtx_unlock(&lun->lun_lock);
4933 * Mark the LUN invalid.
4935 lun->flags |= CTL_LUN_INVALID;
4938 * If there is nothing in the OOA queue, go ahead and free the LUN.
4939 * If we have something in the OOA queue, we'll free it when the
4940 * last I/O completes.
4942 if (TAILQ_EMPTY(&lun->ooa_queue)) {
4943 mtx_unlock(&lun->lun_lock);
4944 mtx_lock(&ctl_softc->ctl_lock);
4946 mtx_unlock(&ctl_softc->ctl_lock);
4948 mtx_unlock(&lun->lun_lock);
4954 ctl_lun_inoperable(struct ctl_be_lun *be_lun)
4956 struct ctl_softc *ctl_softc;
4957 struct ctl_lun *lun;
4959 ctl_softc = control_softc;
4960 lun = (struct ctl_lun *)be_lun->ctl_lun;
4962 mtx_lock(&lun->lun_lock);
4963 lun->flags |= CTL_LUN_INOPERABLE;
4964 mtx_unlock(&lun->lun_lock);
4970 ctl_lun_operable(struct ctl_be_lun *be_lun)
4972 struct ctl_softc *ctl_softc;
4973 struct ctl_lun *lun;
4975 ctl_softc = control_softc;
4976 lun = (struct ctl_lun *)be_lun->ctl_lun;
4978 mtx_lock(&lun->lun_lock);
4979 lun->flags &= ~CTL_LUN_INOPERABLE;
4980 mtx_unlock(&lun->lun_lock);
4986 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
4988 struct ctl_lun *lun;
4989 struct ctl_softc *softc;
4992 softc = control_softc;
4994 lun = (struct ctl_lun *)be_lun->ctl_lun;
4996 mtx_lock(&lun->lun_lock);
4998 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4999 lun->pending_ua[i] |= CTL_UA_CAPACITY_CHANGED;
5001 mtx_unlock(&lun->lun_lock);
5005 * Backend "memory move is complete" callback for requests that never
5006 * make it down to say RAIDCore's configuration code.
5009 ctl_config_move_done(union ctl_io *io)
5013 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
5014 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
5015 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
5017 if ((io->io_hdr.port_status != 0) &&
5018 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5019 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5021 * For hardware error sense keys, the sense key
5022 * specific value is defined to be a retry count,
5023 * but we use it to pass back an internal FETD
5024 * error code. XXX KDM Hopefully the FETD is only
5025 * using 16 bits for an error code, since that's
5026 * all the space we have in the sks field.
5028 ctl_set_internal_failure(&io->scsiio,
5031 io->io_hdr.port_status);
5034 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
5035 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5036 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
5037 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5039 * XXX KDM just assuming a single pointer here, and not a
5040 * S/G list. If we start using S/G lists for config data,
5041 * we'll need to know how to clean them up here as well.
5043 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5044 free(io->scsiio.kern_data_ptr, M_CTL);
5046 retval = CTL_RETVAL_COMPLETE;
5049 * XXX KDM now we need to continue data movement. Some
5051 * - call ctl_scsiio() again? We don't do this for data
5052 * writes, because for those at least we know ahead of
5053 * time where the write will go and how long it is. For
5054 * config writes, though, that information is largely
5055 * contained within the write itself, thus we need to
5056 * parse out the data again.
5058 * - Call some other function once the data is in?
5060 if (ctl_debug & CTL_DEBUG_CDB_DATA)
5064 * XXX KDM call ctl_scsiio() again for now, and check flag
5065 * bits to see whether we're allocated or not.
5067 retval = ctl_scsiio(&io->scsiio);
5073 * This gets called by a backend driver when it is done with a
5074 * data_submit method.
5077 ctl_data_submit_done(union ctl_io *io)
5080 * If the IO_CONT flag is set, we need to call the supplied
5081 * function to continue processing the I/O, instead of completing
5084 * If there is an error, though, we don't want to keep processing.
5085 * Instead, just send status back to the initiator.
5087 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5088 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5089 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5090 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5091 io->scsiio.io_cont(io);
5098 * This gets called by a backend driver when it is done with a
5099 * configuration write.
5102 ctl_config_write_done(union ctl_io *io)
5107 * If the IO_CONT flag is set, we need to call the supplied
5108 * function to continue processing the I/O, instead of completing
5111 * If there is an error, though, we don't want to keep processing.
5112 * Instead, just send status back to the initiator.
5114 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5115 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5116 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5117 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5118 io->scsiio.io_cont(io);
5122 * Since a configuration write can be done for commands that actually
5123 * have data allocated, like write buffer, and commands that have
5124 * no data, like start/stop unit, we need to check here.
5126 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5127 buf = io->scsiio.kern_data_ptr;
5136 * SCSI release command.
5139 ctl_scsi_release(struct ctl_scsiio *ctsio)
5141 int length, longid, thirdparty_id, resv_id;
5142 struct ctl_softc *ctl_softc;
5143 struct ctl_lun *lun;
5149 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5151 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5152 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5153 ctl_softc = control_softc;
5155 switch (ctsio->cdb[0]) {
5157 struct scsi_release_10 *cdb;
5159 cdb = (struct scsi_release_10 *)ctsio->cdb;
5161 if (cdb->byte2 & SR10_LONGID)
5164 thirdparty_id = cdb->thirdparty_id;
5166 resv_id = cdb->resv_id;
5167 length = scsi_2btoul(cdb->length);
5174 * XXX KDM right now, we only support LUN reservation. We don't
5175 * support 3rd party reservations, or extent reservations, which
5176 * might actually need the parameter list. If we've gotten this
5177 * far, we've got a LUN reservation. Anything else got kicked out
5178 * above. So, according to SPC, ignore the length.
5182 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5184 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5185 ctsio->kern_data_len = length;
5186 ctsio->kern_total_len = length;
5187 ctsio->kern_data_resid = 0;
5188 ctsio->kern_rel_offset = 0;
5189 ctsio->kern_sg_entries = 0;
5190 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5191 ctsio->be_move_done = ctl_config_move_done;
5192 ctl_datamove((union ctl_io *)ctsio);
5194 return (CTL_RETVAL_COMPLETE);
5198 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5200 mtx_lock(&lun->lun_lock);
5203 * According to SPC, it is not an error for an intiator to attempt
5204 * to release a reservation on a LUN that isn't reserved, or that
5205 * is reserved by another initiator. The reservation can only be
5206 * released, though, by the initiator who made it or by one of
5207 * several reset type events.
5209 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5210 lun->flags &= ~CTL_LUN_RESERVED;
5212 mtx_unlock(&lun->lun_lock);
5214 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5215 free(ctsio->kern_data_ptr, M_CTL);
5216 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5219 ctl_set_success(ctsio);
5220 ctl_done((union ctl_io *)ctsio);
5221 return (CTL_RETVAL_COMPLETE);
5225 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5227 int extent, thirdparty, longid;
5228 int resv_id, length;
5229 uint64_t thirdparty_id;
5230 struct ctl_softc *ctl_softc;
5231 struct ctl_lun *lun;
5241 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5243 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5244 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5245 ctl_softc = control_softc;
5247 switch (ctsio->cdb[0]) {
5249 struct scsi_reserve_10 *cdb;
5251 cdb = (struct scsi_reserve_10 *)ctsio->cdb;
5253 if (cdb->byte2 & SR10_LONGID)
5256 thirdparty_id = cdb->thirdparty_id;
5258 resv_id = cdb->resv_id;
5259 length = scsi_2btoul(cdb->length);
5265 * XXX KDM right now, we only support LUN reservation. We don't
5266 * support 3rd party reservations, or extent reservations, which
5267 * might actually need the parameter list. If we've gotten this
5268 * far, we've got a LUN reservation. Anything else got kicked out
5269 * above. So, according to SPC, ignore the length.
5273 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5275 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5276 ctsio->kern_data_len = length;
5277 ctsio->kern_total_len = length;
5278 ctsio->kern_data_resid = 0;
5279 ctsio->kern_rel_offset = 0;
5280 ctsio->kern_sg_entries = 0;
5281 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5282 ctsio->be_move_done = ctl_config_move_done;
5283 ctl_datamove((union ctl_io *)ctsio);
5285 return (CTL_RETVAL_COMPLETE);
5289 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5291 mtx_lock(&lun->lun_lock);
5292 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5293 ctl_set_reservation_conflict(ctsio);
5297 lun->flags |= CTL_LUN_RESERVED;
5298 lun->res_idx = residx;
5300 ctl_set_success(ctsio);
5303 mtx_unlock(&lun->lun_lock);
5305 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5306 free(ctsio->kern_data_ptr, M_CTL);
5307 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5310 ctl_done((union ctl_io *)ctsio);
5311 return (CTL_RETVAL_COMPLETE);
5315 ctl_start_stop(struct ctl_scsiio *ctsio)
5317 struct scsi_start_stop_unit *cdb;
5318 struct ctl_lun *lun;
5319 struct ctl_softc *ctl_softc;
5322 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5324 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5325 ctl_softc = control_softc;
5328 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5332 * We don't support the immediate bit on a stop unit. In order to
5333 * do that, we would need to code up a way to know that a stop is
5334 * pending, and hold off any new commands until it completes, one
5335 * way or another. Then we could accept or reject those commands
5336 * depending on its status. We would almost need to do the reverse
5337 * of what we do below for an immediate start -- return the copy of
5338 * the ctl_io to the FETD with status to send to the host (and to
5339 * free the copy!) and then free the original I/O once the stop
5340 * actually completes. That way, the OOA queue mechanism can work
5341 * to block commands that shouldn't proceed. Another alternative
5342 * would be to put the copy in the queue in place of the original,
5343 * and return the original back to the caller. That could be
5346 if ((cdb->byte2 & SSS_IMMED)
5347 && ((cdb->how & SSS_START) == 0)) {
5348 ctl_set_invalid_field(ctsio,
5354 ctl_done((union ctl_io *)ctsio);
5355 return (CTL_RETVAL_COMPLETE);
5358 if ((lun->flags & CTL_LUN_PR_RESERVED)
5359 && ((cdb->how & SSS_START)==0)) {
5362 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5363 if (ctl_get_prkey(lun, residx) == 0
5364 || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5366 ctl_set_reservation_conflict(ctsio);
5367 ctl_done((union ctl_io *)ctsio);
5368 return (CTL_RETVAL_COMPLETE);
5373 * If there is no backend on this device, we can't start or stop
5374 * it. In theory we shouldn't get any start/stop commands in the
5375 * first place at this level if the LUN doesn't have a backend.
5376 * That should get stopped by the command decode code.
5378 if (lun->backend == NULL) {
5379 ctl_set_invalid_opcode(ctsio);
5380 ctl_done((union ctl_io *)ctsio);
5381 return (CTL_RETVAL_COMPLETE);
5385 * XXX KDM Copan-specific offline behavior.
5386 * Figure out a reasonable way to port this?
5389 mtx_lock(&lun->lun_lock);
5391 if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
5392 && (lun->flags & CTL_LUN_OFFLINE)) {
5394 * If the LUN is offline, and the on/offline bit isn't set,
5395 * reject the start or stop. Otherwise, let it through.
5397 mtx_unlock(&lun->lun_lock);
5398 ctl_set_lun_not_ready(ctsio);
5399 ctl_done((union ctl_io *)ctsio);
5401 mtx_unlock(&lun->lun_lock);
5402 #endif /* NEEDTOPORT */
5404 * This could be a start or a stop when we're online,
5405 * or a stop/offline or start/online. A start or stop when
5406 * we're offline is covered in the case above.
5409 * In the non-immediate case, we send the request to
5410 * the backend and return status to the user when
5413 * In the immediate case, we allocate a new ctl_io
5414 * to hold a copy of the request, and send that to
5415 * the backend. We then set good status on the
5416 * user's request and return it immediately.
5418 if (cdb->byte2 & SSS_IMMED) {
5419 union ctl_io *new_io;
5421 new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5422 ctl_copy_io((union ctl_io *)ctsio, new_io);
5423 retval = lun->backend->config_write(new_io);
5424 ctl_set_success(ctsio);
5425 ctl_done((union ctl_io *)ctsio);
5427 retval = lun->backend->config_write(
5428 (union ctl_io *)ctsio);
5437 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5438 * we don't really do anything with the LBA and length fields if the user
5439 * passes them in. Instead we'll just flush out the cache for the entire
5443 ctl_sync_cache(struct ctl_scsiio *ctsio)
5445 struct ctl_lun *lun;
5446 struct ctl_softc *ctl_softc;
5447 uint64_t starting_lba;
5448 uint32_t block_count;
5451 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5453 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5454 ctl_softc = control_softc;
5457 switch (ctsio->cdb[0]) {
5458 case SYNCHRONIZE_CACHE: {
5459 struct scsi_sync_cache *cdb;
5460 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5462 starting_lba = scsi_4btoul(cdb->begin_lba);
5463 block_count = scsi_2btoul(cdb->lb_count);
5466 case SYNCHRONIZE_CACHE_16: {
5467 struct scsi_sync_cache_16 *cdb;
5468 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5470 starting_lba = scsi_8btou64(cdb->begin_lba);
5471 block_count = scsi_4btoul(cdb->lb_count);
5475 ctl_set_invalid_opcode(ctsio);
5476 ctl_done((union ctl_io *)ctsio);
5478 break; /* NOTREACHED */
5482 * We check the LBA and length, but don't do anything with them.
5483 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5484 * get flushed. This check will just help satisfy anyone who wants
5485 * to see an error for an out of range LBA.
5487 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5488 ctl_set_lba_out_of_range(ctsio);
5489 ctl_done((union ctl_io *)ctsio);
5494 * If this LUN has no backend, we can't flush the cache anyway.
5496 if (lun->backend == NULL) {
5497 ctl_set_invalid_opcode(ctsio);
5498 ctl_done((union ctl_io *)ctsio);
5503 * Check to see whether we're configured to send the SYNCHRONIZE
5504 * CACHE command directly to the back end.
5506 mtx_lock(&lun->lun_lock);
5507 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC)
5508 && (++(lun->sync_count) >= lun->sync_interval)) {
5509 lun->sync_count = 0;
5510 mtx_unlock(&lun->lun_lock);
5511 retval = lun->backend->config_write((union ctl_io *)ctsio);
5513 mtx_unlock(&lun->lun_lock);
5514 ctl_set_success(ctsio);
5515 ctl_done((union ctl_io *)ctsio);
5524 ctl_format(struct ctl_scsiio *ctsio)
5526 struct scsi_format *cdb;
5527 struct ctl_lun *lun;
5528 struct ctl_softc *ctl_softc;
5529 int length, defect_list_len;
5531 CTL_DEBUG_PRINT(("ctl_format\n"));
5533 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5534 ctl_softc = control_softc;
5536 cdb = (struct scsi_format *)ctsio->cdb;
5539 if (cdb->byte2 & SF_FMTDATA) {
5540 if (cdb->byte2 & SF_LONGLIST)
5541 length = sizeof(struct scsi_format_header_long);
5543 length = sizeof(struct scsi_format_header_short);
5546 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5548 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5549 ctsio->kern_data_len = length;
5550 ctsio->kern_total_len = length;
5551 ctsio->kern_data_resid = 0;
5552 ctsio->kern_rel_offset = 0;
5553 ctsio->kern_sg_entries = 0;
5554 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5555 ctsio->be_move_done = ctl_config_move_done;
5556 ctl_datamove((union ctl_io *)ctsio);
5558 return (CTL_RETVAL_COMPLETE);
5561 defect_list_len = 0;
5563 if (cdb->byte2 & SF_FMTDATA) {
5564 if (cdb->byte2 & SF_LONGLIST) {
5565 struct scsi_format_header_long *header;
5567 header = (struct scsi_format_header_long *)
5568 ctsio->kern_data_ptr;
5570 defect_list_len = scsi_4btoul(header->defect_list_len);
5571 if (defect_list_len != 0) {
5572 ctl_set_invalid_field(ctsio,
5581 struct scsi_format_header_short *header;
5583 header = (struct scsi_format_header_short *)
5584 ctsio->kern_data_ptr;
5586 defect_list_len = scsi_2btoul(header->defect_list_len);
5587 if (defect_list_len != 0) {
5588 ctl_set_invalid_field(ctsio,
5600 * The format command will clear out the "Medium format corrupted"
5601 * status if set by the configuration code. That status is really
5602 * just a way to notify the host that we have lost the media, and
5603 * get them to issue a command that will basically make them think
5604 * they're blowing away the media.
5606 mtx_lock(&lun->lun_lock);
5607 lun->flags &= ~CTL_LUN_INOPERABLE;
5608 mtx_unlock(&lun->lun_lock);
5610 ctl_set_success(ctsio);
5613 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5614 free(ctsio->kern_data_ptr, M_CTL);
5615 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5618 ctl_done((union ctl_io *)ctsio);
5619 return (CTL_RETVAL_COMPLETE);
5623 ctl_read_buffer(struct ctl_scsiio *ctsio)
5625 struct scsi_read_buffer *cdb;
5626 struct ctl_lun *lun;
5627 int buffer_offset, len;
5628 static uint8_t descr[4];
5629 static uint8_t echo_descr[4] = { 0 };
5631 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5633 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5634 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5636 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA &&
5637 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR &&
5638 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) {
5639 ctl_set_invalid_field(ctsio,
5645 ctl_done((union ctl_io *)ctsio);
5646 return (CTL_RETVAL_COMPLETE);
5649 len = scsi_3btoul(cdb->length);
5650 buffer_offset = scsi_3btoul(cdb->offset);
5652 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5653 ctl_set_invalid_field(ctsio,
5659 ctl_done((union ctl_io *)ctsio);
5660 return (CTL_RETVAL_COMPLETE);
5663 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5665 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5666 ctsio->kern_data_ptr = descr;
5667 len = min(len, sizeof(descr));
5668 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5669 ctsio->kern_data_ptr = echo_descr;
5670 len = min(len, sizeof(echo_descr));
5672 if (lun->write_buffer == NULL) {
5673 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5676 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5678 ctsio->kern_data_len = len;
5679 ctsio->kern_total_len = len;
5680 ctsio->kern_data_resid = 0;
5681 ctsio->kern_rel_offset = 0;
5682 ctsio->kern_sg_entries = 0;
5683 ctl_set_success(ctsio);
5684 ctsio->be_move_done = ctl_config_move_done;
5685 ctl_datamove((union ctl_io *)ctsio);
5686 return (CTL_RETVAL_COMPLETE);
5690 ctl_write_buffer(struct ctl_scsiio *ctsio)
5692 struct scsi_write_buffer *cdb;
5693 struct ctl_lun *lun;
5694 int buffer_offset, len;
5696 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5698 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5699 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5701 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5702 ctl_set_invalid_field(ctsio,
5708 ctl_done((union ctl_io *)ctsio);
5709 return (CTL_RETVAL_COMPLETE);
5712 len = scsi_3btoul(cdb->length);
5713 buffer_offset = scsi_3btoul(cdb->offset);
5715 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5716 ctl_set_invalid_field(ctsio,
5722 ctl_done((union ctl_io *)ctsio);
5723 return (CTL_RETVAL_COMPLETE);
5727 * If we've got a kernel request that hasn't been malloced yet,
5728 * malloc it and tell the caller the data buffer is here.
5730 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5731 if (lun->write_buffer == NULL) {
5732 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5735 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5736 ctsio->kern_data_len = len;
5737 ctsio->kern_total_len = len;
5738 ctsio->kern_data_resid = 0;
5739 ctsio->kern_rel_offset = 0;
5740 ctsio->kern_sg_entries = 0;
5741 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5742 ctsio->be_move_done = ctl_config_move_done;
5743 ctl_datamove((union ctl_io *)ctsio);
5745 return (CTL_RETVAL_COMPLETE);
5748 ctl_set_success(ctsio);
5749 ctl_done((union ctl_io *)ctsio);
5750 return (CTL_RETVAL_COMPLETE);
5754 ctl_write_same(struct ctl_scsiio *ctsio)
5756 struct ctl_lun *lun;
5757 struct ctl_lba_len_flags *lbalen;
5759 uint32_t num_blocks;
5763 retval = CTL_RETVAL_COMPLETE;
5765 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5767 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5769 switch (ctsio->cdb[0]) {
5770 case WRITE_SAME_10: {
5771 struct scsi_write_same_10 *cdb;
5773 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5775 lba = scsi_4btoul(cdb->addr);
5776 num_blocks = scsi_2btoul(cdb->length);
5780 case WRITE_SAME_16: {
5781 struct scsi_write_same_16 *cdb;
5783 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5785 lba = scsi_8btou64(cdb->addr);
5786 num_blocks = scsi_4btoul(cdb->length);
5792 * We got a command we don't support. This shouldn't
5793 * happen, commands should be filtered out above us.
5795 ctl_set_invalid_opcode(ctsio);
5796 ctl_done((union ctl_io *)ctsio);
5798 return (CTL_RETVAL_COMPLETE);
5799 break; /* NOTREACHED */
5802 /* NDOB and ANCHOR flags can be used only together with UNMAP */
5803 if ((byte2 & SWS_UNMAP) == 0 &&
5804 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) {
5805 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5806 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5807 ctl_done((union ctl_io *)ctsio);
5808 return (CTL_RETVAL_COMPLETE);
5812 * The first check is to make sure we're in bounds, the second
5813 * check is to catch wrap-around problems. If the lba + num blocks
5814 * is less than the lba, then we've wrapped around and the block
5815 * range is invalid anyway.
5817 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5818 || ((lba + num_blocks) < lba)) {
5819 ctl_set_lba_out_of_range(ctsio);
5820 ctl_done((union ctl_io *)ctsio);
5821 return (CTL_RETVAL_COMPLETE);
5824 /* Zero number of blocks means "to the last logical block" */
5825 if (num_blocks == 0) {
5826 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5827 ctl_set_invalid_field(ctsio,
5833 ctl_done((union ctl_io *)ctsio);
5834 return (CTL_RETVAL_COMPLETE);
5836 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5839 len = lun->be_lun->blocksize;
5842 * If we've got a kernel request that hasn't been malloced yet,
5843 * malloc it and tell the caller the data buffer is here.
5845 if ((byte2 & SWS_NDOB) == 0 &&
5846 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5847 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5848 ctsio->kern_data_len = len;
5849 ctsio->kern_total_len = len;
5850 ctsio->kern_data_resid = 0;
5851 ctsio->kern_rel_offset = 0;
5852 ctsio->kern_sg_entries = 0;
5853 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5854 ctsio->be_move_done = ctl_config_move_done;
5855 ctl_datamove((union ctl_io *)ctsio);
5857 return (CTL_RETVAL_COMPLETE);
5860 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5862 lbalen->len = num_blocks;
5863 lbalen->flags = byte2;
5864 retval = lun->backend->config_write((union ctl_io *)ctsio);
5870 ctl_unmap(struct ctl_scsiio *ctsio)
5872 struct ctl_lun *lun;
5873 struct scsi_unmap *cdb;
5874 struct ctl_ptr_len_flags *ptrlen;
5875 struct scsi_unmap_header *hdr;
5876 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5878 uint32_t num_blocks;
5882 retval = CTL_RETVAL_COMPLETE;
5884 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5886 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5887 cdb = (struct scsi_unmap *)ctsio->cdb;
5889 len = scsi_2btoul(cdb->length);
5893 * If we've got a kernel request that hasn't been malloced yet,
5894 * malloc it and tell the caller the data buffer is here.
5896 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5897 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5898 ctsio->kern_data_len = len;
5899 ctsio->kern_total_len = len;
5900 ctsio->kern_data_resid = 0;
5901 ctsio->kern_rel_offset = 0;
5902 ctsio->kern_sg_entries = 0;
5903 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5904 ctsio->be_move_done = ctl_config_move_done;
5905 ctl_datamove((union ctl_io *)ctsio);
5907 return (CTL_RETVAL_COMPLETE);
5910 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5911 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5912 if (len < sizeof (*hdr) ||
5913 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5914 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5915 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5916 ctl_set_invalid_field(ctsio,
5924 len = scsi_2btoul(hdr->desc_length);
5925 buf = (struct scsi_unmap_desc *)(hdr + 1);
5926 end = buf + len / sizeof(*buf);
5929 for (range = buf; range < end; range++) {
5930 lba = scsi_8btou64(range->lba);
5931 num_blocks = scsi_4btoul(range->length);
5932 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5933 || ((lba + num_blocks) < lba)) {
5934 ctl_set_lba_out_of_range(ctsio);
5935 ctl_done((union ctl_io *)ctsio);
5936 return (CTL_RETVAL_COMPLETE);
5938 if (num_blocks != 0)
5943 * Block backend can not handle zero last range.
5944 * Filter it out and return if there is nothing left.
5946 len = (uint8_t *)endnz - (uint8_t *)buf;
5948 ctl_set_success(ctsio);
5952 mtx_lock(&lun->lun_lock);
5953 ptrlen = (struct ctl_ptr_len_flags *)
5954 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5955 ptrlen->ptr = (void *)buf;
5957 ptrlen->flags = byte2;
5958 ctl_check_blocked(lun);
5959 mtx_unlock(&lun->lun_lock);
5961 retval = lun->backend->config_write((union ctl_io *)ctsio);
5965 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5966 free(ctsio->kern_data_ptr, M_CTL);
5967 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5969 ctl_done((union ctl_io *)ctsio);
5970 return (CTL_RETVAL_COMPLETE);
5974 * Note that this function currently doesn't actually do anything inside
5975 * CTL to enforce things if the DQue bit is turned on.
5977 * Also note that this function can't be used in the default case, because
5978 * the DQue bit isn't set in the changeable mask for the control mode page
5979 * anyway. This is just here as an example for how to implement a page
5980 * handler, and a placeholder in case we want to allow the user to turn
5981 * tagged queueing on and off.
5983 * The D_SENSE bit handling is functional, however, and will turn
5984 * descriptor sense on and off for a given LUN.
5987 ctl_control_page_handler(struct ctl_scsiio *ctsio,
5988 struct ctl_page_index *page_index, uint8_t *page_ptr)
5990 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
5991 struct ctl_lun *lun;
5992 struct ctl_softc *softc;
5996 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5997 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6000 user_cp = (struct scsi_control_page *)page_ptr;
6001 current_cp = (struct scsi_control_page *)
6002 (page_index->page_data + (page_index->page_len *
6004 saved_cp = (struct scsi_control_page *)
6005 (page_index->page_data + (page_index->page_len *
6008 softc = control_softc;
6010 mtx_lock(&lun->lun_lock);
6011 if (((current_cp->rlec & SCP_DSENSE) == 0)
6012 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
6014 * Descriptor sense is currently turned off and the user
6015 * wants to turn it on.
6017 current_cp->rlec |= SCP_DSENSE;
6018 saved_cp->rlec |= SCP_DSENSE;
6019 lun->flags |= CTL_LUN_SENSE_DESC;
6021 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
6022 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
6024 * Descriptor sense is currently turned on, and the user
6025 * wants to turn it off.
6027 current_cp->rlec &= ~SCP_DSENSE;
6028 saved_cp->rlec &= ~SCP_DSENSE;
6029 lun->flags &= ~CTL_LUN_SENSE_DESC;
6032 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) !=
6033 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) {
6034 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
6035 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
6036 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
6037 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
6040 if ((current_cp->eca_and_aen & SCP_SWP) !=
6041 (user_cp->eca_and_aen & SCP_SWP)) {
6042 current_cp->eca_and_aen &= ~SCP_SWP;
6043 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
6044 saved_cp->eca_and_aen &= ~SCP_SWP;
6045 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
6051 * Let other initiators know that the mode
6052 * parameters for this LUN have changed.
6054 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
6058 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE;
6061 mtx_unlock(&lun->lun_lock);
6067 ctl_caching_sp_handler(struct ctl_scsiio *ctsio,
6068 struct ctl_page_index *page_index, uint8_t *page_ptr)
6070 struct scsi_caching_page *current_cp, *saved_cp, *user_cp;
6071 struct ctl_lun *lun;
6075 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6076 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6079 user_cp = (struct scsi_caching_page *)page_ptr;
6080 current_cp = (struct scsi_caching_page *)
6081 (page_index->page_data + (page_index->page_len *
6083 saved_cp = (struct scsi_caching_page *)
6084 (page_index->page_data + (page_index->page_len *
6087 mtx_lock(&lun->lun_lock);
6088 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) !=
6089 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) {
6090 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
6091 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
6092 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
6093 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
6099 * Let other initiators know that the mode
6100 * parameters for this LUN have changed.
6102 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
6106 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE;
6109 mtx_unlock(&lun->lun_lock);
6115 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
6116 struct ctl_page_index *page_index,
6122 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
6127 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
6128 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
6129 printf("page data:");
6131 printf(" %.2x",page_ptr[i]);
6137 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
6138 struct ctl_page_index *page_index,
6141 struct copan_debugconf_subpage *page;
6143 page = (struct copan_debugconf_subpage *)page_index->page_data +
6144 (page_index->page_len * pc);
6147 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6148 case SMS_PAGE_CTRL_DEFAULT >> 6:
6149 case SMS_PAGE_CTRL_SAVED >> 6:
6151 * We don't update the changable or default bits for this page.
6154 case SMS_PAGE_CTRL_CURRENT >> 6:
6155 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
6156 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
6160 EPRINT(0, "Invalid PC %d!!", pc);
6161 #endif /* NEEDTOPORT */
6169 ctl_do_mode_select(union ctl_io *io)
6171 struct scsi_mode_page_header *page_header;
6172 struct ctl_page_index *page_index;
6173 struct ctl_scsiio *ctsio;
6174 int control_dev, page_len;
6175 int page_len_offset, page_len_size;
6176 union ctl_modepage_info *modepage_info;
6177 struct ctl_lun *lun;
6178 int *len_left, *len_used;
6181 ctsio = &io->scsiio;
6184 retval = CTL_RETVAL_COMPLETE;
6186 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6188 if (lun->be_lun->lun_type != T_DIRECT)
6193 modepage_info = (union ctl_modepage_info *)
6194 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6195 len_left = &modepage_info->header.len_left;
6196 len_used = &modepage_info->header.len_used;
6200 page_header = (struct scsi_mode_page_header *)
6201 (ctsio->kern_data_ptr + *len_used);
6203 if (*len_left == 0) {
6204 free(ctsio->kern_data_ptr, M_CTL);
6205 ctl_set_success(ctsio);
6206 ctl_done((union ctl_io *)ctsio);
6207 return (CTL_RETVAL_COMPLETE);
6208 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6210 free(ctsio->kern_data_ptr, M_CTL);
6211 ctl_set_param_len_error(ctsio);
6212 ctl_done((union ctl_io *)ctsio);
6213 return (CTL_RETVAL_COMPLETE);
6215 } else if ((page_header->page_code & SMPH_SPF)
6216 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6218 free(ctsio->kern_data_ptr, M_CTL);
6219 ctl_set_param_len_error(ctsio);
6220 ctl_done((union ctl_io *)ctsio);
6221 return (CTL_RETVAL_COMPLETE);
6226 * XXX KDM should we do something with the block descriptor?
6228 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6230 if ((control_dev != 0)
6231 && (lun->mode_pages.index[i].page_flags &
6232 CTL_PAGE_FLAG_DISK_ONLY))
6235 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
6236 (page_header->page_code & SMPH_PC_MASK))
6240 * If neither page has a subpage code, then we've got a
6243 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
6244 && ((page_header->page_code & SMPH_SPF) == 0)) {
6245 page_index = &lun->mode_pages.index[i];
6246 page_len = page_header->page_length;
6251 * If both pages have subpages, then the subpage numbers
6254 if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
6255 && (page_header->page_code & SMPH_SPF)) {
6256 struct scsi_mode_page_header_sp *sph;
6258 sph = (struct scsi_mode_page_header_sp *)page_header;
6260 if (lun->mode_pages.index[i].subpage ==
6262 page_index = &lun->mode_pages.index[i];
6263 page_len = scsi_2btoul(sph->page_length);
6270 * If we couldn't find the page, or if we don't have a mode select
6271 * handler for it, send back an error to the user.
6273 if ((page_index == NULL)
6274 || (page_index->select_handler == NULL)) {
6275 ctl_set_invalid_field(ctsio,
6278 /*field*/ *len_used,
6281 free(ctsio->kern_data_ptr, M_CTL);
6282 ctl_done((union ctl_io *)ctsio);
6283 return (CTL_RETVAL_COMPLETE);
6286 if (page_index->page_code & SMPH_SPF) {
6287 page_len_offset = 2;
6291 page_len_offset = 1;
6295 * If the length the initiator gives us isn't the one we specify in
6296 * the mode page header, or if they didn't specify enough data in
6297 * the CDB to avoid truncating this page, kick out the request.
6299 if ((page_len != (page_index->page_len - page_len_offset -
6301 || (*len_left < page_index->page_len)) {
6304 ctl_set_invalid_field(ctsio,
6307 /*field*/ *len_used + page_len_offset,
6310 free(ctsio->kern_data_ptr, M_CTL);
6311 ctl_done((union ctl_io *)ctsio);
6312 return (CTL_RETVAL_COMPLETE);
6316 * Run through the mode page, checking to make sure that the bits
6317 * the user changed are actually legal for him to change.
6319 for (i = 0; i < page_index->page_len; i++) {
6320 uint8_t *user_byte, *change_mask, *current_byte;
6324 user_byte = (uint8_t *)page_header + i;
6325 change_mask = page_index->page_data +
6326 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6327 current_byte = page_index->page_data +
6328 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6331 * Check to see whether the user set any bits in this byte
6332 * that he is not allowed to set.
6334 if ((*user_byte & ~(*change_mask)) ==
6335 (*current_byte & ~(*change_mask)))
6339 * Go through bit by bit to determine which one is illegal.
6342 for (j = 7; j >= 0; j--) {
6343 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6344 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6349 ctl_set_invalid_field(ctsio,
6352 /*field*/ *len_used + i,
6355 free(ctsio->kern_data_ptr, M_CTL);
6356 ctl_done((union ctl_io *)ctsio);
6357 return (CTL_RETVAL_COMPLETE);
6361 * Decrement these before we call the page handler, since we may
6362 * end up getting called back one way or another before the handler
6363 * returns to this context.
6365 *len_left -= page_index->page_len;
6366 *len_used += page_index->page_len;
6368 retval = page_index->select_handler(ctsio, page_index,
6369 (uint8_t *)page_header);
6372 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6373 * wait until this queued command completes to finish processing
6374 * the mode page. If it returns anything other than
6375 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6376 * already set the sense information, freed the data pointer, and
6377 * completed the io for us.
6379 if (retval != CTL_RETVAL_COMPLETE)
6380 goto bailout_no_done;
6383 * If the initiator sent us more than one page, parse the next one.
6388 ctl_set_success(ctsio);
6389 free(ctsio->kern_data_ptr, M_CTL);
6390 ctl_done((union ctl_io *)ctsio);
6394 return (CTL_RETVAL_COMPLETE);
6399 ctl_mode_select(struct ctl_scsiio *ctsio)
6401 int param_len, pf, sp;
6402 int header_size, bd_len;
6403 int len_left, len_used;
6404 struct ctl_page_index *page_index;
6405 struct ctl_lun *lun;
6406 int control_dev, page_len;
6407 union ctl_modepage_info *modepage_info;
6419 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6421 if (lun->be_lun->lun_type != T_DIRECT)
6426 switch (ctsio->cdb[0]) {
6427 case MODE_SELECT_6: {
6428 struct scsi_mode_select_6 *cdb;
6430 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6432 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6433 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6435 param_len = cdb->length;
6436 header_size = sizeof(struct scsi_mode_header_6);
6439 case MODE_SELECT_10: {
6440 struct scsi_mode_select_10 *cdb;
6442 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6444 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6445 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6447 param_len = scsi_2btoul(cdb->length);
6448 header_size = sizeof(struct scsi_mode_header_10);
6452 ctl_set_invalid_opcode(ctsio);
6453 ctl_done((union ctl_io *)ctsio);
6454 return (CTL_RETVAL_COMPLETE);
6455 break; /* NOTREACHED */
6460 * "A parameter list length of zero indicates that the Data-Out Buffer
6461 * shall be empty. This condition shall not be considered as an error."
6463 if (param_len == 0) {
6464 ctl_set_success(ctsio);
6465 ctl_done((union ctl_io *)ctsio);
6466 return (CTL_RETVAL_COMPLETE);
6470 * Since we'll hit this the first time through, prior to
6471 * allocation, we don't need to free a data buffer here.
6473 if (param_len < header_size) {
6474 ctl_set_param_len_error(ctsio);
6475 ctl_done((union ctl_io *)ctsio);
6476 return (CTL_RETVAL_COMPLETE);
6480 * Allocate the data buffer and grab the user's data. In theory,
6481 * we shouldn't have to sanity check the parameter list length here
6482 * because the maximum size is 64K. We should be able to malloc
6483 * that much without too many problems.
6485 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6486 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6487 ctsio->kern_data_len = param_len;
6488 ctsio->kern_total_len = param_len;
6489 ctsio->kern_data_resid = 0;
6490 ctsio->kern_rel_offset = 0;
6491 ctsio->kern_sg_entries = 0;
6492 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6493 ctsio->be_move_done = ctl_config_move_done;
6494 ctl_datamove((union ctl_io *)ctsio);
6496 return (CTL_RETVAL_COMPLETE);
6499 switch (ctsio->cdb[0]) {
6500 case MODE_SELECT_6: {
6501 struct scsi_mode_header_6 *mh6;
6503 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6504 bd_len = mh6->blk_desc_len;
6507 case MODE_SELECT_10: {
6508 struct scsi_mode_header_10 *mh10;
6510 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6511 bd_len = scsi_2btoul(mh10->blk_desc_len);
6515 panic("Invalid CDB type %#x", ctsio->cdb[0]);
6519 if (param_len < (header_size + bd_len)) {
6520 free(ctsio->kern_data_ptr, M_CTL);
6521 ctl_set_param_len_error(ctsio);
6522 ctl_done((union ctl_io *)ctsio);
6523 return (CTL_RETVAL_COMPLETE);
6527 * Set the IO_CONT flag, so that if this I/O gets passed to
6528 * ctl_config_write_done(), it'll get passed back to
6529 * ctl_do_mode_select() for further processing, or completion if
6532 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6533 ctsio->io_cont = ctl_do_mode_select;
6535 modepage_info = (union ctl_modepage_info *)
6536 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6538 memset(modepage_info, 0, sizeof(*modepage_info));
6540 len_left = param_len - header_size - bd_len;
6541 len_used = header_size + bd_len;
6543 modepage_info->header.len_left = len_left;
6544 modepage_info->header.len_used = len_used;
6546 return (ctl_do_mode_select((union ctl_io *)ctsio));
6550 ctl_mode_sense(struct ctl_scsiio *ctsio)
6552 struct ctl_lun *lun;
6553 int pc, page_code, dbd, llba, subpage;
6554 int alloc_len, page_len, header_len, total_len;
6555 struct scsi_mode_block_descr *block_desc;
6556 struct ctl_page_index *page_index;
6564 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6566 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6568 if (lun->be_lun->lun_type != T_DIRECT)
6573 switch (ctsio->cdb[0]) {
6574 case MODE_SENSE_6: {
6575 struct scsi_mode_sense_6 *cdb;
6577 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6579 header_len = sizeof(struct scsi_mode_hdr_6);
6580 if (cdb->byte2 & SMS_DBD)
6583 header_len += sizeof(struct scsi_mode_block_descr);
6585 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6586 page_code = cdb->page & SMS_PAGE_CODE;
6587 subpage = cdb->subpage;
6588 alloc_len = cdb->length;
6591 case MODE_SENSE_10: {
6592 struct scsi_mode_sense_10 *cdb;
6594 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6596 header_len = sizeof(struct scsi_mode_hdr_10);
6598 if (cdb->byte2 & SMS_DBD)
6601 header_len += sizeof(struct scsi_mode_block_descr);
6602 if (cdb->byte2 & SMS10_LLBAA)
6604 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6605 page_code = cdb->page & SMS_PAGE_CODE;
6606 subpage = cdb->subpage;
6607 alloc_len = scsi_2btoul(cdb->length);
6611 ctl_set_invalid_opcode(ctsio);
6612 ctl_done((union ctl_io *)ctsio);
6613 return (CTL_RETVAL_COMPLETE);
6614 break; /* NOTREACHED */
6618 * We have to make a first pass through to calculate the size of
6619 * the pages that match the user's query. Then we allocate enough
6620 * memory to hold it, and actually copy the data into the buffer.
6622 switch (page_code) {
6623 case SMS_ALL_PAGES_PAGE: {
6629 * At the moment, values other than 0 and 0xff here are
6630 * reserved according to SPC-3.
6632 if ((subpage != SMS_SUBPAGE_PAGE_0)
6633 && (subpage != SMS_SUBPAGE_ALL)) {
6634 ctl_set_invalid_field(ctsio,
6640 ctl_done((union ctl_io *)ctsio);
6641 return (CTL_RETVAL_COMPLETE);
6644 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6645 if ((control_dev != 0)
6646 && (lun->mode_pages.index[i].page_flags &
6647 CTL_PAGE_FLAG_DISK_ONLY))
6651 * We don't use this subpage if the user didn't
6652 * request all subpages.
6654 if ((lun->mode_pages.index[i].subpage != 0)
6655 && (subpage == SMS_SUBPAGE_PAGE_0))
6659 printf("found page %#x len %d\n",
6660 lun->mode_pages.index[i].page_code &
6662 lun->mode_pages.index[i].page_len);
6664 page_len += lun->mode_pages.index[i].page_len;
6673 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6674 /* Look for the right page code */
6675 if ((lun->mode_pages.index[i].page_code &
6676 SMPH_PC_MASK) != page_code)
6679 /* Look for the right subpage or the subpage wildcard*/
6680 if ((lun->mode_pages.index[i].subpage != subpage)
6681 && (subpage != SMS_SUBPAGE_ALL))
6684 /* Make sure the page is supported for this dev type */
6685 if ((control_dev != 0)
6686 && (lun->mode_pages.index[i].page_flags &
6687 CTL_PAGE_FLAG_DISK_ONLY))
6691 printf("found page %#x len %d\n",
6692 lun->mode_pages.index[i].page_code &
6694 lun->mode_pages.index[i].page_len);
6697 page_len += lun->mode_pages.index[i].page_len;
6700 if (page_len == 0) {
6701 ctl_set_invalid_field(ctsio,
6707 ctl_done((union ctl_io *)ctsio);
6708 return (CTL_RETVAL_COMPLETE);
6714 total_len = header_len + page_len;
6716 printf("header_len = %d, page_len = %d, total_len = %d\n",
6717 header_len, page_len, total_len);
6720 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6721 ctsio->kern_sg_entries = 0;
6722 ctsio->kern_data_resid = 0;
6723 ctsio->kern_rel_offset = 0;
6724 if (total_len < alloc_len) {
6725 ctsio->residual = alloc_len - total_len;
6726 ctsio->kern_data_len = total_len;
6727 ctsio->kern_total_len = total_len;
6729 ctsio->residual = 0;
6730 ctsio->kern_data_len = alloc_len;
6731 ctsio->kern_total_len = alloc_len;
6734 switch (ctsio->cdb[0]) {
6735 case MODE_SENSE_6: {
6736 struct scsi_mode_hdr_6 *header;
6738 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6740 header->datalen = ctl_min(total_len - 1, 254);
6741 if (control_dev == 0) {
6742 header->dev_specific = 0x10; /* DPOFUA */
6743 if ((lun->flags & CTL_LUN_READONLY) ||
6744 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6745 .eca_and_aen & SCP_SWP) != 0)
6746 header->dev_specific |= 0x80; /* WP */
6749 header->block_descr_len = 0;
6751 header->block_descr_len =
6752 sizeof(struct scsi_mode_block_descr);
6753 block_desc = (struct scsi_mode_block_descr *)&header[1];
6756 case MODE_SENSE_10: {
6757 struct scsi_mode_hdr_10 *header;
6760 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6762 datalen = ctl_min(total_len - 2, 65533);
6763 scsi_ulto2b(datalen, header->datalen);
6764 if (control_dev == 0) {
6765 header->dev_specific = 0x10; /* DPOFUA */
6766 if ((lun->flags & CTL_LUN_READONLY) ||
6767 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6768 .eca_and_aen & SCP_SWP) != 0)
6769 header->dev_specific |= 0x80; /* WP */
6772 scsi_ulto2b(0, header->block_descr_len);
6774 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6775 header->block_descr_len);
6776 block_desc = (struct scsi_mode_block_descr *)&header[1];
6780 panic("invalid CDB type %#x", ctsio->cdb[0]);
6781 break; /* NOTREACHED */
6785 * If we've got a disk, use its blocksize in the block
6786 * descriptor. Otherwise, just set it to 0.
6789 if (control_dev == 0)
6790 scsi_ulto3b(lun->be_lun->blocksize,
6791 block_desc->block_len);
6793 scsi_ulto3b(0, block_desc->block_len);
6796 switch (page_code) {
6797 case SMS_ALL_PAGES_PAGE: {
6800 data_used = header_len;
6801 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6802 struct ctl_page_index *page_index;
6804 page_index = &lun->mode_pages.index[i];
6806 if ((control_dev != 0)
6807 && (page_index->page_flags &
6808 CTL_PAGE_FLAG_DISK_ONLY))
6812 * We don't use this subpage if the user didn't
6813 * request all subpages. We already checked (above)
6814 * to make sure the user only specified a subpage
6815 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6817 if ((page_index->subpage != 0)
6818 && (subpage == SMS_SUBPAGE_PAGE_0))
6822 * Call the handler, if it exists, to update the
6823 * page to the latest values.
6825 if (page_index->sense_handler != NULL)
6826 page_index->sense_handler(ctsio, page_index,pc);
6828 memcpy(ctsio->kern_data_ptr + data_used,
6829 page_index->page_data +
6830 (page_index->page_len * pc),
6831 page_index->page_len);
6832 data_used += page_index->page_len;
6839 data_used = header_len;
6841 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6842 struct ctl_page_index *page_index;
6844 page_index = &lun->mode_pages.index[i];
6846 /* Look for the right page code */
6847 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6850 /* Look for the right subpage or the subpage wildcard*/
6851 if ((page_index->subpage != subpage)
6852 && (subpage != SMS_SUBPAGE_ALL))
6855 /* Make sure the page is supported for this dev type */
6856 if ((control_dev != 0)
6857 && (page_index->page_flags &
6858 CTL_PAGE_FLAG_DISK_ONLY))
6862 * Call the handler, if it exists, to update the
6863 * page to the latest values.
6865 if (page_index->sense_handler != NULL)
6866 page_index->sense_handler(ctsio, page_index,pc);
6868 memcpy(ctsio->kern_data_ptr + data_used,
6869 page_index->page_data +
6870 (page_index->page_len * pc),
6871 page_index->page_len);
6872 data_used += page_index->page_len;
6878 ctl_set_success(ctsio);
6879 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6880 ctsio->be_move_done = ctl_config_move_done;
6881 ctl_datamove((union ctl_io *)ctsio);
6882 return (CTL_RETVAL_COMPLETE);
6886 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6887 struct ctl_page_index *page_index,
6890 struct ctl_lun *lun;
6891 struct scsi_log_param_header *phdr;
6895 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6896 data = page_index->page_data;
6898 if (lun->backend->lun_attr != NULL &&
6899 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6901 phdr = (struct scsi_log_param_header *)data;
6902 scsi_ulto2b(0x0001, phdr->param_code);
6903 phdr->param_control = SLP_LBIN | SLP_LP;
6904 phdr->param_len = 8;
6905 data = (uint8_t *)(phdr + 1);
6906 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6907 data[4] = 0x02; /* per-pool */
6908 data += phdr->param_len;
6911 if (lun->backend->lun_attr != NULL &&
6912 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6914 phdr = (struct scsi_log_param_header *)data;
6915 scsi_ulto2b(0x0002, phdr->param_code);
6916 phdr->param_control = SLP_LBIN | SLP_LP;
6917 phdr->param_len = 8;
6918 data = (uint8_t *)(phdr + 1);
6919 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6920 data[4] = 0x01; /* per-LUN */
6921 data += phdr->param_len;
6924 if (lun->backend->lun_attr != NULL &&
6925 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6927 phdr = (struct scsi_log_param_header *)data;
6928 scsi_ulto2b(0x00f1, phdr->param_code);
6929 phdr->param_control = SLP_LBIN | SLP_LP;
6930 phdr->param_len = 8;
6931 data = (uint8_t *)(phdr + 1);
6932 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6933 data[4] = 0x02; /* per-pool */
6934 data += phdr->param_len;
6937 if (lun->backend->lun_attr != NULL &&
6938 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6940 phdr = (struct scsi_log_param_header *)data;
6941 scsi_ulto2b(0x00f2, phdr->param_code);
6942 phdr->param_control = SLP_LBIN | SLP_LP;
6943 phdr->param_len = 8;
6944 data = (uint8_t *)(phdr + 1);
6945 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6946 data[4] = 0x02; /* per-pool */
6947 data += phdr->param_len;
6950 page_index->page_len = data - page_index->page_data;
6955 ctl_log_sense(struct ctl_scsiio *ctsio)
6957 struct ctl_lun *lun;
6958 int i, pc, page_code, subpage;
6959 int alloc_len, total_len;
6960 struct ctl_page_index *page_index;
6961 struct scsi_log_sense *cdb;
6962 struct scsi_log_header *header;
6964 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6966 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6967 cdb = (struct scsi_log_sense *)ctsio->cdb;
6968 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6969 page_code = cdb->page & SLS_PAGE_CODE;
6970 subpage = cdb->subpage;
6971 alloc_len = scsi_2btoul(cdb->length);
6974 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6975 page_index = &lun->log_pages.index[i];
6977 /* Look for the right page code */
6978 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6981 /* Look for the right subpage or the subpage wildcard*/
6982 if (page_index->subpage != subpage)
6987 if (i >= CTL_NUM_LOG_PAGES) {
6988 ctl_set_invalid_field(ctsio,
6994 ctl_done((union ctl_io *)ctsio);
6995 return (CTL_RETVAL_COMPLETE);
6998 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
7000 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7001 ctsio->kern_sg_entries = 0;
7002 ctsio->kern_data_resid = 0;
7003 ctsio->kern_rel_offset = 0;
7004 if (total_len < alloc_len) {
7005 ctsio->residual = alloc_len - total_len;
7006 ctsio->kern_data_len = total_len;
7007 ctsio->kern_total_len = total_len;
7009 ctsio->residual = 0;
7010 ctsio->kern_data_len = alloc_len;
7011 ctsio->kern_total_len = alloc_len;
7014 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
7015 header->page = page_index->page_code;
7016 if (page_index->subpage) {
7017 header->page |= SL_SPF;
7018 header->subpage = page_index->subpage;
7020 scsi_ulto2b(page_index->page_len, header->datalen);
7023 * Call the handler, if it exists, to update the
7024 * page to the latest values.
7026 if (page_index->sense_handler != NULL)
7027 page_index->sense_handler(ctsio, page_index, pc);
7029 memcpy(header + 1, page_index->page_data, page_index->page_len);
7031 ctl_set_success(ctsio);
7032 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7033 ctsio->be_move_done = ctl_config_move_done;
7034 ctl_datamove((union ctl_io *)ctsio);
7035 return (CTL_RETVAL_COMPLETE);
7039 ctl_read_capacity(struct ctl_scsiio *ctsio)
7041 struct scsi_read_capacity *cdb;
7042 struct scsi_read_capacity_data *data;
7043 struct ctl_lun *lun;
7046 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
7048 cdb = (struct scsi_read_capacity *)ctsio->cdb;
7050 lba = scsi_4btoul(cdb->addr);
7051 if (((cdb->pmi & SRC_PMI) == 0)
7053 ctl_set_invalid_field(/*ctsio*/ ctsio,
7059 ctl_done((union ctl_io *)ctsio);
7060 return (CTL_RETVAL_COMPLETE);
7063 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7065 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7066 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
7067 ctsio->residual = 0;
7068 ctsio->kern_data_len = sizeof(*data);
7069 ctsio->kern_total_len = sizeof(*data);
7070 ctsio->kern_data_resid = 0;
7071 ctsio->kern_rel_offset = 0;
7072 ctsio->kern_sg_entries = 0;
7075 * If the maximum LBA is greater than 0xfffffffe, the user must
7076 * issue a SERVICE ACTION IN (16) command, with the read capacity
7077 * serivce action set.
7079 if (lun->be_lun->maxlba > 0xfffffffe)
7080 scsi_ulto4b(0xffffffff, data->addr);
7082 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
7085 * XXX KDM this may not be 512 bytes...
7087 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7089 ctl_set_success(ctsio);
7090 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7091 ctsio->be_move_done = ctl_config_move_done;
7092 ctl_datamove((union ctl_io *)ctsio);
7093 return (CTL_RETVAL_COMPLETE);
7097 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
7099 struct scsi_read_capacity_16 *cdb;
7100 struct scsi_read_capacity_data_long *data;
7101 struct ctl_lun *lun;
7105 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
7107 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
7109 alloc_len = scsi_4btoul(cdb->alloc_len);
7110 lba = scsi_8btou64(cdb->addr);
7112 if ((cdb->reladr & SRC16_PMI)
7114 ctl_set_invalid_field(/*ctsio*/ ctsio,
7120 ctl_done((union ctl_io *)ctsio);
7121 return (CTL_RETVAL_COMPLETE);
7124 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7126 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7127 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
7129 if (sizeof(*data) < alloc_len) {
7130 ctsio->residual = alloc_len - sizeof(*data);
7131 ctsio->kern_data_len = sizeof(*data);
7132 ctsio->kern_total_len = sizeof(*data);
7134 ctsio->residual = 0;
7135 ctsio->kern_data_len = alloc_len;
7136 ctsio->kern_total_len = alloc_len;
7138 ctsio->kern_data_resid = 0;
7139 ctsio->kern_rel_offset = 0;
7140 ctsio->kern_sg_entries = 0;
7142 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7143 /* XXX KDM this may not be 512 bytes... */
7144 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7145 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7146 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7147 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7148 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
7150 ctl_set_success(ctsio);
7151 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7152 ctsio->be_move_done = ctl_config_move_done;
7153 ctl_datamove((union ctl_io *)ctsio);
7154 return (CTL_RETVAL_COMPLETE);
7158 ctl_read_defect(struct ctl_scsiio *ctsio)
7160 struct scsi_read_defect_data_10 *ccb10;
7161 struct scsi_read_defect_data_12 *ccb12;
7162 struct scsi_read_defect_data_hdr_10 *data10;
7163 struct scsi_read_defect_data_hdr_12 *data12;
7164 uint32_t alloc_len, data_len;
7167 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7169 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7170 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7171 format = ccb10->format;
7172 alloc_len = scsi_2btoul(ccb10->alloc_length);
7173 data_len = sizeof(*data10);
7175 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7176 format = ccb12->format;
7177 alloc_len = scsi_4btoul(ccb12->alloc_length);
7178 data_len = sizeof(*data12);
7180 if (alloc_len == 0) {
7181 ctl_set_success(ctsio);
7182 ctl_done((union ctl_io *)ctsio);
7183 return (CTL_RETVAL_COMPLETE);
7186 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7187 if (data_len < alloc_len) {
7188 ctsio->residual = alloc_len - data_len;
7189 ctsio->kern_data_len = data_len;
7190 ctsio->kern_total_len = data_len;
7192 ctsio->residual = 0;
7193 ctsio->kern_data_len = alloc_len;
7194 ctsio->kern_total_len = alloc_len;
7196 ctsio->kern_data_resid = 0;
7197 ctsio->kern_rel_offset = 0;
7198 ctsio->kern_sg_entries = 0;
7200 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7201 data10 = (struct scsi_read_defect_data_hdr_10 *)
7202 ctsio->kern_data_ptr;
7203 data10->format = format;
7204 scsi_ulto2b(0, data10->length);
7206 data12 = (struct scsi_read_defect_data_hdr_12 *)
7207 ctsio->kern_data_ptr;
7208 data12->format = format;
7209 scsi_ulto2b(0, data12->generation);
7210 scsi_ulto4b(0, data12->length);
7213 ctl_set_success(ctsio);
7214 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7215 ctsio->be_move_done = ctl_config_move_done;
7216 ctl_datamove((union ctl_io *)ctsio);
7217 return (CTL_RETVAL_COMPLETE);
7221 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7223 struct scsi_maintenance_in *cdb;
7225 int alloc_len, ext, total_len = 0, g, p, pc, pg, gs, os;
7226 int num_target_port_groups, num_target_ports;
7227 struct ctl_lun *lun;
7228 struct ctl_softc *softc;
7229 struct ctl_port *port;
7230 struct scsi_target_group_data *rtg_ptr;
7231 struct scsi_target_group_data_extended *rtg_ext_ptr;
7232 struct scsi_target_port_group_descriptor *tpg_desc;
7234 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7236 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7237 softc = control_softc;
7238 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7240 retval = CTL_RETVAL_COMPLETE;
7242 switch (cdb->byte2 & STG_PDF_MASK) {
7243 case STG_PDF_LENGTH:
7246 case STG_PDF_EXTENDED:
7250 ctl_set_invalid_field(/*ctsio*/ ctsio,
7256 ctl_done((union ctl_io *)ctsio);
7260 if (softc->is_single)
7261 num_target_port_groups = 1;
7263 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
7264 num_target_ports = 0;
7265 mtx_lock(&softc->ctl_lock);
7266 STAILQ_FOREACH(port, &softc->port_list, links) {
7267 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7269 if (ctl_map_lun_back(port->targ_port, lun->lun) >= CTL_MAX_LUNS)
7273 mtx_unlock(&softc->ctl_lock);
7276 total_len = sizeof(struct scsi_target_group_data_extended);
7278 total_len = sizeof(struct scsi_target_group_data);
7279 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7280 num_target_port_groups +
7281 sizeof(struct scsi_target_port_descriptor) *
7282 num_target_ports * num_target_port_groups;
7284 alloc_len = scsi_4btoul(cdb->length);
7286 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7288 ctsio->kern_sg_entries = 0;
7290 if (total_len < alloc_len) {
7291 ctsio->residual = alloc_len - total_len;
7292 ctsio->kern_data_len = total_len;
7293 ctsio->kern_total_len = total_len;
7295 ctsio->residual = 0;
7296 ctsio->kern_data_len = alloc_len;
7297 ctsio->kern_total_len = alloc_len;
7299 ctsio->kern_data_resid = 0;
7300 ctsio->kern_rel_offset = 0;
7303 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7304 ctsio->kern_data_ptr;
7305 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7306 rtg_ext_ptr->format_type = 0x10;
7307 rtg_ext_ptr->implicit_transition_time = 0;
7308 tpg_desc = &rtg_ext_ptr->groups[0];
7310 rtg_ptr = (struct scsi_target_group_data *)
7311 ctsio->kern_data_ptr;
7312 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7313 tpg_desc = &rtg_ptr->groups[0];
7316 mtx_lock(&softc->ctl_lock);
7317 pg = softc->port_offset / CTL_MAX_PORTS;
7318 if (softc->flags & CTL_FLAG_ACTIVE_SHELF) {
7319 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) {
7320 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7321 os = TPG_ASYMMETRIC_ACCESS_STANDBY;
7322 } else if (lun->flags & CTL_LUN_PRIMARY_SC) {
7323 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7324 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7326 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7327 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7330 gs = TPG_ASYMMETRIC_ACCESS_STANDBY;
7331 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7333 for (g = 0; g < num_target_port_groups; g++) {
7334 tpg_desc->pref_state = (g == pg) ? gs : os;
7335 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP;
7336 scsi_ulto2b(g + 1, tpg_desc->target_port_group);
7337 tpg_desc->status = TPG_IMPLICIT;
7339 STAILQ_FOREACH(port, &softc->port_list, links) {
7340 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7342 if (ctl_map_lun_back(port->targ_port, lun->lun) >=
7345 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS;
7346 scsi_ulto2b(p, tpg_desc->descriptors[pc].
7347 relative_target_port_identifier);
7350 tpg_desc->target_port_count = pc;
7351 tpg_desc = (struct scsi_target_port_group_descriptor *)
7352 &tpg_desc->descriptors[pc];
7354 mtx_unlock(&softc->ctl_lock);
7356 ctl_set_success(ctsio);
7357 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7358 ctsio->be_move_done = ctl_config_move_done;
7359 ctl_datamove((union ctl_io *)ctsio);
7364 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7366 struct ctl_lun *lun;
7367 struct scsi_report_supported_opcodes *cdb;
7368 const struct ctl_cmd_entry *entry, *sentry;
7369 struct scsi_report_supported_opcodes_all *all;
7370 struct scsi_report_supported_opcodes_descr *descr;
7371 struct scsi_report_supported_opcodes_one *one;
7373 int alloc_len, total_len;
7374 int opcode, service_action, i, j, num;
7376 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7378 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7379 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7381 retval = CTL_RETVAL_COMPLETE;
7383 opcode = cdb->requested_opcode;
7384 service_action = scsi_2btoul(cdb->requested_service_action);
7385 switch (cdb->options & RSO_OPTIONS_MASK) {
7386 case RSO_OPTIONS_ALL:
7388 for (i = 0; i < 256; i++) {
7389 entry = &ctl_cmd_table[i];
7390 if (entry->flags & CTL_CMD_FLAG_SA5) {
7391 for (j = 0; j < 32; j++) {
7392 sentry = &((const struct ctl_cmd_entry *)
7394 if (ctl_cmd_applicable(
7395 lun->be_lun->lun_type, sentry))
7399 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7404 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7405 num * sizeof(struct scsi_report_supported_opcodes_descr);
7407 case RSO_OPTIONS_OC:
7408 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7409 ctl_set_invalid_field(/*ctsio*/ ctsio,
7415 ctl_done((union ctl_io *)ctsio);
7416 return (CTL_RETVAL_COMPLETE);
7418 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7420 case RSO_OPTIONS_OC_SA:
7421 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7422 service_action >= 32) {
7423 ctl_set_invalid_field(/*ctsio*/ ctsio,
7429 ctl_done((union ctl_io *)ctsio);
7430 return (CTL_RETVAL_COMPLETE);
7432 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7435 ctl_set_invalid_field(/*ctsio*/ ctsio,
7441 ctl_done((union ctl_io *)ctsio);
7442 return (CTL_RETVAL_COMPLETE);
7445 alloc_len = scsi_4btoul(cdb->length);
7447 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7449 ctsio->kern_sg_entries = 0;
7451 if (total_len < alloc_len) {
7452 ctsio->residual = alloc_len - total_len;
7453 ctsio->kern_data_len = total_len;
7454 ctsio->kern_total_len = total_len;
7456 ctsio->residual = 0;
7457 ctsio->kern_data_len = alloc_len;
7458 ctsio->kern_total_len = alloc_len;
7460 ctsio->kern_data_resid = 0;
7461 ctsio->kern_rel_offset = 0;
7463 switch (cdb->options & RSO_OPTIONS_MASK) {
7464 case RSO_OPTIONS_ALL:
7465 all = (struct scsi_report_supported_opcodes_all *)
7466 ctsio->kern_data_ptr;
7468 for (i = 0; i < 256; i++) {
7469 entry = &ctl_cmd_table[i];
7470 if (entry->flags & CTL_CMD_FLAG_SA5) {
7471 for (j = 0; j < 32; j++) {
7472 sentry = &((const struct ctl_cmd_entry *)
7474 if (!ctl_cmd_applicable(
7475 lun->be_lun->lun_type, sentry))
7477 descr = &all->descr[num++];
7479 scsi_ulto2b(j, descr->service_action);
7480 descr->flags = RSO_SERVACTV;
7481 scsi_ulto2b(sentry->length,
7485 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7488 descr = &all->descr[num++];
7490 scsi_ulto2b(0, descr->service_action);
7492 scsi_ulto2b(entry->length, descr->cdb_length);
7496 num * sizeof(struct scsi_report_supported_opcodes_descr),
7499 case RSO_OPTIONS_OC:
7500 one = (struct scsi_report_supported_opcodes_one *)
7501 ctsio->kern_data_ptr;
7502 entry = &ctl_cmd_table[opcode];
7504 case RSO_OPTIONS_OC_SA:
7505 one = (struct scsi_report_supported_opcodes_one *)
7506 ctsio->kern_data_ptr;
7507 entry = &ctl_cmd_table[opcode];
7508 entry = &((const struct ctl_cmd_entry *)
7509 entry->execute)[service_action];
7511 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7513 scsi_ulto2b(entry->length, one->cdb_length);
7514 one->cdb_usage[0] = opcode;
7515 memcpy(&one->cdb_usage[1], entry->usage,
7522 ctl_set_success(ctsio);
7523 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7524 ctsio->be_move_done = ctl_config_move_done;
7525 ctl_datamove((union ctl_io *)ctsio);
7530 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7532 struct scsi_report_supported_tmf *cdb;
7533 struct scsi_report_supported_tmf_data *data;
7535 int alloc_len, total_len;
7537 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7539 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7541 retval = CTL_RETVAL_COMPLETE;
7543 total_len = sizeof(struct scsi_report_supported_tmf_data);
7544 alloc_len = scsi_4btoul(cdb->length);
7546 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7548 ctsio->kern_sg_entries = 0;
7550 if (total_len < alloc_len) {
7551 ctsio->residual = alloc_len - total_len;
7552 ctsio->kern_data_len = total_len;
7553 ctsio->kern_total_len = total_len;
7555 ctsio->residual = 0;
7556 ctsio->kern_data_len = alloc_len;
7557 ctsio->kern_total_len = alloc_len;
7559 ctsio->kern_data_resid = 0;
7560 ctsio->kern_rel_offset = 0;
7562 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr;
7563 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS;
7564 data->byte2 |= RST_ITNRS;
7566 ctl_set_success(ctsio);
7567 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7568 ctsio->be_move_done = ctl_config_move_done;
7569 ctl_datamove((union ctl_io *)ctsio);
7574 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7576 struct scsi_report_timestamp *cdb;
7577 struct scsi_report_timestamp_data *data;
7581 int alloc_len, total_len;
7583 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7585 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7587 retval = CTL_RETVAL_COMPLETE;
7589 total_len = sizeof(struct scsi_report_timestamp_data);
7590 alloc_len = scsi_4btoul(cdb->length);
7592 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7594 ctsio->kern_sg_entries = 0;
7596 if (total_len < alloc_len) {
7597 ctsio->residual = alloc_len - total_len;
7598 ctsio->kern_data_len = total_len;
7599 ctsio->kern_total_len = total_len;
7601 ctsio->residual = 0;
7602 ctsio->kern_data_len = alloc_len;
7603 ctsio->kern_total_len = alloc_len;
7605 ctsio->kern_data_resid = 0;
7606 ctsio->kern_rel_offset = 0;
7608 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7609 scsi_ulto2b(sizeof(*data) - 2, data->length);
7610 data->origin = RTS_ORIG_OUTSIDE;
7612 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7613 scsi_ulto4b(timestamp >> 16, data->timestamp);
7614 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7616 ctl_set_success(ctsio);
7617 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7618 ctsio->be_move_done = ctl_config_move_done;
7619 ctl_datamove((union ctl_io *)ctsio);
7624 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7626 struct scsi_per_res_in *cdb;
7627 int alloc_len, total_len = 0;
7628 /* struct scsi_per_res_in_rsrv in_data; */
7629 struct ctl_lun *lun;
7630 struct ctl_softc *softc;
7633 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7635 softc = control_softc;
7637 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7639 alloc_len = scsi_2btoul(cdb->length);
7641 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7644 mtx_lock(&lun->lun_lock);
7645 switch (cdb->action) {
7646 case SPRI_RK: /* read keys */
7647 total_len = sizeof(struct scsi_per_res_in_keys) +
7649 sizeof(struct scsi_per_res_key);
7651 case SPRI_RR: /* read reservation */
7652 if (lun->flags & CTL_LUN_PR_RESERVED)
7653 total_len = sizeof(struct scsi_per_res_in_rsrv);
7655 total_len = sizeof(struct scsi_per_res_in_header);
7657 case SPRI_RC: /* report capabilities */
7658 total_len = sizeof(struct scsi_per_res_cap);
7660 case SPRI_RS: /* read full status */
7661 total_len = sizeof(struct scsi_per_res_in_header) +
7662 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7666 panic("Invalid PR type %x", cdb->action);
7668 mtx_unlock(&lun->lun_lock);
7670 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7672 if (total_len < alloc_len) {
7673 ctsio->residual = alloc_len - total_len;
7674 ctsio->kern_data_len = total_len;
7675 ctsio->kern_total_len = total_len;
7677 ctsio->residual = 0;
7678 ctsio->kern_data_len = alloc_len;
7679 ctsio->kern_total_len = alloc_len;
7682 ctsio->kern_data_resid = 0;
7683 ctsio->kern_rel_offset = 0;
7684 ctsio->kern_sg_entries = 0;
7686 mtx_lock(&lun->lun_lock);
7687 switch (cdb->action) {
7688 case SPRI_RK: { // read keys
7689 struct scsi_per_res_in_keys *res_keys;
7692 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7695 * We had to drop the lock to allocate our buffer, which
7696 * leaves time for someone to come in with another
7697 * persistent reservation. (That is unlikely, though,
7698 * since this should be the only persistent reservation
7699 * command active right now.)
7701 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7702 (lun->pr_key_count *
7703 sizeof(struct scsi_per_res_key)))){
7704 mtx_unlock(&lun->lun_lock);
7705 free(ctsio->kern_data_ptr, M_CTL);
7706 printf("%s: reservation length changed, retrying\n",
7711 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7713 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7714 lun->pr_key_count, res_keys->header.length);
7716 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7717 if ((key = ctl_get_prkey(lun, i)) == 0)
7721 * We used lun->pr_key_count to calculate the
7722 * size to allocate. If it turns out the number of
7723 * initiators with the registered flag set is
7724 * larger than that (i.e. they haven't been kept in
7725 * sync), we've got a problem.
7727 if (key_count >= lun->pr_key_count) {
7729 csevent_log(CSC_CTL | CSC_SHELF_SW |
7731 csevent_LogType_Fault,
7732 csevent_AlertLevel_Yellow,
7733 csevent_FRU_ShelfController,
7734 csevent_FRU_Firmware,
7735 csevent_FRU_Unknown,
7736 "registered keys %d >= key "
7737 "count %d", key_count,
7743 scsi_u64to8b(key, res_keys->keys[key_count].key);
7748 case SPRI_RR: { // read reservation
7749 struct scsi_per_res_in_rsrv *res;
7750 int tmp_len, header_only;
7752 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7754 scsi_ulto4b(lun->PRGeneration, res->header.generation);
7756 if (lun->flags & CTL_LUN_PR_RESERVED)
7758 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7759 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7760 res->header.length);
7763 tmp_len = sizeof(struct scsi_per_res_in_header);
7764 scsi_ulto4b(0, res->header.length);
7769 * We had to drop the lock to allocate our buffer, which
7770 * leaves time for someone to come in with another
7771 * persistent reservation. (That is unlikely, though,
7772 * since this should be the only persistent reservation
7773 * command active right now.)
7775 if (tmp_len != total_len) {
7776 mtx_unlock(&lun->lun_lock);
7777 free(ctsio->kern_data_ptr, M_CTL);
7778 printf("%s: reservation status changed, retrying\n",
7784 * No reservation held, so we're done.
7786 if (header_only != 0)
7790 * If the registration is an All Registrants type, the key
7791 * is 0, since it doesn't really matter.
7793 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7794 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7795 res->data.reservation);
7797 res->data.scopetype = lun->res_type;
7800 case SPRI_RC: //report capabilities
7802 struct scsi_per_res_cap *res_cap;
7805 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7806 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7807 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5;
7808 type_mask = SPRI_TM_WR_EX_AR |
7814 scsi_ulto2b(type_mask, res_cap->type_mask);
7817 case SPRI_RS: { // read full status
7818 struct scsi_per_res_in_full *res_status;
7819 struct scsi_per_res_in_full_desc *res_desc;
7820 struct ctl_port *port;
7823 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7826 * We had to drop the lock to allocate our buffer, which
7827 * leaves time for someone to come in with another
7828 * persistent reservation. (That is unlikely, though,
7829 * since this should be the only persistent reservation
7830 * command active right now.)
7832 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7833 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7834 lun->pr_key_count)){
7835 mtx_unlock(&lun->lun_lock);
7836 free(ctsio->kern_data_ptr, M_CTL);
7837 printf("%s: reservation length changed, retrying\n",
7842 scsi_ulto4b(lun->PRGeneration, res_status->header.generation);
7844 res_desc = &res_status->desc[0];
7845 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7846 if ((key = ctl_get_prkey(lun, i)) == 0)
7849 scsi_u64to8b(key, res_desc->res_key.key);
7850 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7851 (lun->pr_res_idx == i ||
7852 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7853 res_desc->flags = SPRI_FULL_R_HOLDER;
7854 res_desc->scopetype = lun->res_type;
7856 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7857 res_desc->rel_trgt_port_id);
7859 port = softc->ctl_ports[
7860 ctl_port_idx(i / CTL_MAX_INIT_PER_PORT)];
7862 len = ctl_create_iid(port,
7863 i % CTL_MAX_INIT_PER_PORT,
7864 res_desc->transport_id);
7865 scsi_ulto4b(len, res_desc->additional_length);
7866 res_desc = (struct scsi_per_res_in_full_desc *)
7867 &res_desc->transport_id[len];
7869 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7870 res_status->header.length);
7875 * This is a bug, because we just checked for this above,
7876 * and should have returned an error.
7878 panic("Invalid PR type %x", cdb->action);
7879 break; /* NOTREACHED */
7881 mtx_unlock(&lun->lun_lock);
7883 ctl_set_success(ctsio);
7884 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7885 ctsio->be_move_done = ctl_config_move_done;
7886 ctl_datamove((union ctl_io *)ctsio);
7887 return (CTL_RETVAL_COMPLETE);
7891 ctl_set_res_ua(struct ctl_lun *lun, uint32_t residx, ctl_ua_type ua)
7893 int off = lun->ctl_softc->persis_offset;
7895 if (residx >= off && residx < off + CTL_MAX_INITIATORS)
7896 lun->pending_ua[residx - off] |= ua;
7900 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7904 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7905 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7906 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7907 struct scsi_per_res_out_parms* param)
7909 union ctl_ha_msg persis_io;
7915 mtx_lock(&lun->lun_lock);
7916 if (sa_res_key == 0) {
7917 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7918 /* validate scope and type */
7919 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7921 mtx_unlock(&lun->lun_lock);
7922 ctl_set_invalid_field(/*ctsio*/ ctsio,
7928 ctl_done((union ctl_io *)ctsio);
7932 if (type>8 || type==2 || type==4 || type==0) {
7933 mtx_unlock(&lun->lun_lock);
7934 ctl_set_invalid_field(/*ctsio*/ ctsio,
7940 ctl_done((union ctl_io *)ctsio);
7945 * Unregister everybody else and build UA for
7948 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7949 if (i == residx || ctl_get_prkey(lun, i) == 0)
7952 ctl_clr_prkey(lun, i);
7953 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT);
7955 lun->pr_key_count = 1;
7956 lun->res_type = type;
7957 if (lun->res_type != SPR_TYPE_WR_EX_AR
7958 && lun->res_type != SPR_TYPE_EX_AC_AR)
7959 lun->pr_res_idx = residx;
7961 /* send msg to other side */
7962 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7963 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7964 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7965 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7966 persis_io.pr.pr_info.res_type = type;
7967 memcpy(persis_io.pr.pr_info.sa_res_key,
7968 param->serv_act_res_key,
7969 sizeof(param->serv_act_res_key));
7970 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7971 &persis_io, sizeof(persis_io), 0)) >
7972 CTL_HA_STATUS_SUCCESS) {
7973 printf("CTL:Persis Out error returned "
7974 "from ctl_ha_msg_send %d\n",
7978 /* not all registrants */
7979 mtx_unlock(&lun->lun_lock);
7980 free(ctsio->kern_data_ptr, M_CTL);
7981 ctl_set_invalid_field(ctsio,
7987 ctl_done((union ctl_io *)ctsio);
7990 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7991 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7994 if (res_key == sa_res_key) {
7997 * The spec implies this is not good but doesn't
7998 * say what to do. There are two choices either
7999 * generate a res conflict or check condition
8000 * with illegal field in parameter data. Since
8001 * that is what is done when the sa_res_key is
8002 * zero I'll take that approach since this has
8003 * to do with the sa_res_key.
8005 mtx_unlock(&lun->lun_lock);
8006 free(ctsio->kern_data_ptr, M_CTL);
8007 ctl_set_invalid_field(ctsio,
8013 ctl_done((union ctl_io *)ctsio);
8017 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8018 if (ctl_get_prkey(lun, i) != sa_res_key)
8022 ctl_clr_prkey(lun, i);
8023 lun->pr_key_count--;
8024 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8027 mtx_unlock(&lun->lun_lock);
8028 free(ctsio->kern_data_ptr, M_CTL);
8029 ctl_set_reservation_conflict(ctsio);
8030 ctl_done((union ctl_io *)ctsio);
8031 return (CTL_RETVAL_COMPLETE);
8033 /* send msg to other side */
8034 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8035 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8036 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8037 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8038 persis_io.pr.pr_info.res_type = type;
8039 memcpy(persis_io.pr.pr_info.sa_res_key,
8040 param->serv_act_res_key,
8041 sizeof(param->serv_act_res_key));
8042 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8043 &persis_io, sizeof(persis_io), 0)) >
8044 CTL_HA_STATUS_SUCCESS) {
8045 printf("CTL:Persis Out error returned from "
8046 "ctl_ha_msg_send %d\n", isc_retval);
8049 /* Reserved but not all registrants */
8050 /* sa_res_key is res holder */
8051 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
8052 /* validate scope and type */
8053 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
8055 mtx_unlock(&lun->lun_lock);
8056 ctl_set_invalid_field(/*ctsio*/ ctsio,
8062 ctl_done((union ctl_io *)ctsio);
8066 if (type>8 || type==2 || type==4 || type==0) {
8067 mtx_unlock(&lun->lun_lock);
8068 ctl_set_invalid_field(/*ctsio*/ ctsio,
8074 ctl_done((union ctl_io *)ctsio);
8080 * if sa_res_key != res_key remove all
8081 * registrants w/sa_res_key and generate UA
8082 * for these registrants(Registrations
8083 * Preempted) if it wasn't an exclusive
8084 * reservation generate UA(Reservations
8085 * Preempted) for all other registered nexuses
8086 * if the type has changed. Establish the new
8087 * reservation and holder. If res_key and
8088 * sa_res_key are the same do the above
8089 * except don't unregister the res holder.
8092 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8093 if (i == residx || ctl_get_prkey(lun, i) == 0)
8096 if (sa_res_key == ctl_get_prkey(lun, i)) {
8097 ctl_clr_prkey(lun, i);
8098 lun->pr_key_count--;
8099 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8100 } else if (type != lun->res_type
8101 && (lun->res_type == SPR_TYPE_WR_EX_RO
8102 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
8103 ctl_set_res_ua(lun, i, CTL_UA_RES_RELEASE);
8106 lun->res_type = type;
8107 if (lun->res_type != SPR_TYPE_WR_EX_AR
8108 && lun->res_type != SPR_TYPE_EX_AC_AR)
8109 lun->pr_res_idx = residx;
8111 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8113 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8114 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8115 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8116 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8117 persis_io.pr.pr_info.res_type = type;
8118 memcpy(persis_io.pr.pr_info.sa_res_key,
8119 param->serv_act_res_key,
8120 sizeof(param->serv_act_res_key));
8121 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8122 &persis_io, sizeof(persis_io), 0)) >
8123 CTL_HA_STATUS_SUCCESS) {
8124 printf("CTL:Persis Out error returned "
8125 "from ctl_ha_msg_send %d\n",
8130 * sa_res_key is not the res holder just
8131 * remove registrants
8135 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8136 if (sa_res_key != ctl_get_prkey(lun, i))
8140 ctl_clr_prkey(lun, i);
8141 lun->pr_key_count--;
8142 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8146 mtx_unlock(&lun->lun_lock);
8147 free(ctsio->kern_data_ptr, M_CTL);
8148 ctl_set_reservation_conflict(ctsio);
8149 ctl_done((union ctl_io *)ctsio);
8152 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8153 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8154 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8155 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8156 persis_io.pr.pr_info.res_type = type;
8157 memcpy(persis_io.pr.pr_info.sa_res_key,
8158 param->serv_act_res_key,
8159 sizeof(param->serv_act_res_key));
8160 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8161 &persis_io, sizeof(persis_io), 0)) >
8162 CTL_HA_STATUS_SUCCESS) {
8163 printf("CTL:Persis Out error returned "
8164 "from ctl_ha_msg_send %d\n",
8170 lun->PRGeneration++;
8171 mtx_unlock(&lun->lun_lock);
8177 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8179 uint64_t sa_res_key;
8182 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8184 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8185 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8186 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
8187 if (sa_res_key == 0) {
8189 * Unregister everybody else and build UA for
8192 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8193 if (i == msg->pr.pr_info.residx ||
8194 ctl_get_prkey(lun, i) == 0)
8197 ctl_clr_prkey(lun, i);
8198 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8201 lun->pr_key_count = 1;
8202 lun->res_type = msg->pr.pr_info.res_type;
8203 if (lun->res_type != SPR_TYPE_WR_EX_AR
8204 && lun->res_type != SPR_TYPE_EX_AC_AR)
8205 lun->pr_res_idx = msg->pr.pr_info.residx;
8207 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8208 if (sa_res_key == ctl_get_prkey(lun, i))
8211 ctl_clr_prkey(lun, i);
8212 lun->pr_key_count--;
8213 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8217 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8218 if (i == msg->pr.pr_info.residx ||
8219 ctl_get_prkey(lun, i) == 0)
8222 if (sa_res_key == ctl_get_prkey(lun, i)) {
8223 ctl_clr_prkey(lun, i);
8224 lun->pr_key_count--;
8225 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8226 } else if (msg->pr.pr_info.res_type != lun->res_type
8227 && (lun->res_type == SPR_TYPE_WR_EX_RO
8228 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
8229 ctl_set_res_ua(lun, i, CTL_UA_RES_RELEASE);
8232 lun->res_type = msg->pr.pr_info.res_type;
8233 if (lun->res_type != SPR_TYPE_WR_EX_AR
8234 && lun->res_type != SPR_TYPE_EX_AC_AR)
8235 lun->pr_res_idx = msg->pr.pr_info.residx;
8237 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8239 lun->PRGeneration++;
8245 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8249 u_int32_t param_len;
8250 struct scsi_per_res_out *cdb;
8251 struct ctl_lun *lun;
8252 struct scsi_per_res_out_parms* param;
8253 struct ctl_softc *softc;
8255 uint64_t res_key, sa_res_key, key;
8257 union ctl_ha_msg persis_io;
8260 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8262 retval = CTL_RETVAL_COMPLETE;
8264 softc = control_softc;
8266 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8267 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8270 * We only support whole-LUN scope. The scope & type are ignored for
8271 * register, register and ignore existing key and clear.
8272 * We sometimes ignore scope and type on preempts too!!
8273 * Verify reservation type here as well.
8275 type = cdb->scope_type & SPR_TYPE_MASK;
8276 if ((cdb->action == SPRO_RESERVE)
8277 || (cdb->action == SPRO_RELEASE)) {
8278 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8279 ctl_set_invalid_field(/*ctsio*/ ctsio,
8285 ctl_done((union ctl_io *)ctsio);
8286 return (CTL_RETVAL_COMPLETE);
8289 if (type>8 || type==2 || type==4 || type==0) {
8290 ctl_set_invalid_field(/*ctsio*/ ctsio,
8296 ctl_done((union ctl_io *)ctsio);
8297 return (CTL_RETVAL_COMPLETE);
8301 param_len = scsi_4btoul(cdb->length);
8303 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8304 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8305 ctsio->kern_data_len = param_len;
8306 ctsio->kern_total_len = param_len;
8307 ctsio->kern_data_resid = 0;
8308 ctsio->kern_rel_offset = 0;
8309 ctsio->kern_sg_entries = 0;
8310 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8311 ctsio->be_move_done = ctl_config_move_done;
8312 ctl_datamove((union ctl_io *)ctsio);
8314 return (CTL_RETVAL_COMPLETE);
8317 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8319 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8320 res_key = scsi_8btou64(param->res_key.key);
8321 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8324 * Validate the reservation key here except for SPRO_REG_IGNO
8325 * This must be done for all other service actions
8327 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8328 mtx_lock(&lun->lun_lock);
8329 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8330 if (res_key != key) {
8332 * The current key passed in doesn't match
8333 * the one the initiator previously
8336 mtx_unlock(&lun->lun_lock);
8337 free(ctsio->kern_data_ptr, M_CTL);
8338 ctl_set_reservation_conflict(ctsio);
8339 ctl_done((union ctl_io *)ctsio);
8340 return (CTL_RETVAL_COMPLETE);
8342 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8344 * We are not registered
8346 mtx_unlock(&lun->lun_lock);
8347 free(ctsio->kern_data_ptr, M_CTL);
8348 ctl_set_reservation_conflict(ctsio);
8349 ctl_done((union ctl_io *)ctsio);
8350 return (CTL_RETVAL_COMPLETE);
8351 } else if (res_key != 0) {
8353 * We are not registered and trying to register but
8354 * the register key isn't zero.
8356 mtx_unlock(&lun->lun_lock);
8357 free(ctsio->kern_data_ptr, M_CTL);
8358 ctl_set_reservation_conflict(ctsio);
8359 ctl_done((union ctl_io *)ctsio);
8360 return (CTL_RETVAL_COMPLETE);
8362 mtx_unlock(&lun->lun_lock);
8365 switch (cdb->action & SPRO_ACTION_MASK) {
8367 case SPRO_REG_IGNO: {
8370 printf("Registration received\n");
8374 * We don't support any of these options, as we report in
8375 * the read capabilities request (see
8376 * ctl_persistent_reserve_in(), above).
8378 if ((param->flags & SPR_SPEC_I_PT)
8379 || (param->flags & SPR_ALL_TG_PT)
8380 || (param->flags & SPR_APTPL)) {
8383 if (param->flags & SPR_APTPL)
8385 else if (param->flags & SPR_ALL_TG_PT)
8387 else /* SPR_SPEC_I_PT */
8390 free(ctsio->kern_data_ptr, M_CTL);
8391 ctl_set_invalid_field(ctsio,
8397 ctl_done((union ctl_io *)ctsio);
8398 return (CTL_RETVAL_COMPLETE);
8401 mtx_lock(&lun->lun_lock);
8404 * The initiator wants to clear the
8407 if (sa_res_key == 0) {
8409 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8410 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8411 && ctl_get_prkey(lun, residx) == 0)) {
8412 mtx_unlock(&lun->lun_lock);
8416 ctl_clr_prkey(lun, residx);
8417 lun->pr_key_count--;
8419 if (residx == lun->pr_res_idx) {
8420 lun->flags &= ~CTL_LUN_PR_RESERVED;
8421 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8423 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8424 || lun->res_type == SPR_TYPE_EX_AC_RO)
8425 && lun->pr_key_count) {
8427 * If the reservation is a registrants
8428 * only type we need to generate a UA
8429 * for other registered inits. The
8430 * sense code should be RESERVATIONS
8434 for (i = 0; i < CTL_MAX_INITIATORS;i++){
8435 if (ctl_get_prkey(lun, i +
8436 softc->persis_offset) == 0)
8438 lun->pending_ua[i] |=
8443 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8444 if (lun->pr_key_count==0) {
8445 lun->flags &= ~CTL_LUN_PR_RESERVED;
8447 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8450 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8451 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8452 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8453 persis_io.pr.pr_info.residx = residx;
8454 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8455 &persis_io, sizeof(persis_io), 0 )) >
8456 CTL_HA_STATUS_SUCCESS) {
8457 printf("CTL:Persis Out error returned from "
8458 "ctl_ha_msg_send %d\n", isc_retval);
8460 } else /* sa_res_key != 0 */ {
8463 * If we aren't registered currently then increment
8464 * the key count and set the registered flag.
8466 ctl_alloc_prkey(lun, residx);
8467 if (ctl_get_prkey(lun, residx) == 0)
8468 lun->pr_key_count++;
8469 ctl_set_prkey(lun, residx, sa_res_key);
8471 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8472 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8473 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8474 persis_io.pr.pr_info.residx = residx;
8475 memcpy(persis_io.pr.pr_info.sa_res_key,
8476 param->serv_act_res_key,
8477 sizeof(param->serv_act_res_key));
8478 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8479 &persis_io, sizeof(persis_io), 0)) >
8480 CTL_HA_STATUS_SUCCESS) {
8481 printf("CTL:Persis Out error returned from "
8482 "ctl_ha_msg_send %d\n", isc_retval);
8485 lun->PRGeneration++;
8486 mtx_unlock(&lun->lun_lock);
8492 printf("Reserve executed type %d\n", type);
8494 mtx_lock(&lun->lun_lock);
8495 if (lun->flags & CTL_LUN_PR_RESERVED) {
8497 * if this isn't the reservation holder and it's
8498 * not a "all registrants" type or if the type is
8499 * different then we have a conflict
8501 if ((lun->pr_res_idx != residx
8502 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8503 || lun->res_type != type) {
8504 mtx_unlock(&lun->lun_lock);
8505 free(ctsio->kern_data_ptr, M_CTL);
8506 ctl_set_reservation_conflict(ctsio);
8507 ctl_done((union ctl_io *)ctsio);
8508 return (CTL_RETVAL_COMPLETE);
8510 mtx_unlock(&lun->lun_lock);
8511 } else /* create a reservation */ {
8513 * If it's not an "all registrants" type record
8514 * reservation holder
8516 if (type != SPR_TYPE_WR_EX_AR
8517 && type != SPR_TYPE_EX_AC_AR)
8518 lun->pr_res_idx = residx; /* Res holder */
8520 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8522 lun->flags |= CTL_LUN_PR_RESERVED;
8523 lun->res_type = type;
8525 mtx_unlock(&lun->lun_lock);
8527 /* send msg to other side */
8528 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8529 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8530 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8531 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8532 persis_io.pr.pr_info.res_type = type;
8533 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8534 &persis_io, sizeof(persis_io), 0)) >
8535 CTL_HA_STATUS_SUCCESS) {
8536 printf("CTL:Persis Out error returned from "
8537 "ctl_ha_msg_send %d\n", isc_retval);
8543 mtx_lock(&lun->lun_lock);
8544 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8545 /* No reservation exists return good status */
8546 mtx_unlock(&lun->lun_lock);
8550 * Is this nexus a reservation holder?
8552 if (lun->pr_res_idx != residx
8553 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8555 * not a res holder return good status but
8558 mtx_unlock(&lun->lun_lock);
8562 if (lun->res_type != type) {
8563 mtx_unlock(&lun->lun_lock);
8564 free(ctsio->kern_data_ptr, M_CTL);
8565 ctl_set_illegal_pr_release(ctsio);
8566 ctl_done((union ctl_io *)ctsio);
8567 return (CTL_RETVAL_COMPLETE);
8570 /* okay to release */
8571 lun->flags &= ~CTL_LUN_PR_RESERVED;
8572 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8576 * if this isn't an exclusive access
8577 * res generate UA for all other
8580 if (type != SPR_TYPE_EX_AC
8581 && type != SPR_TYPE_WR_EX) {
8582 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8585 i + softc->persis_offset) == 0)
8587 lun->pending_ua[i] |= CTL_UA_RES_RELEASE;
8590 mtx_unlock(&lun->lun_lock);
8591 /* Send msg to other side */
8592 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8593 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8594 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8595 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io,
8596 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8597 printf("CTL:Persis Out error returned from "
8598 "ctl_ha_msg_send %d\n", isc_retval);
8603 /* send msg to other side */
8605 mtx_lock(&lun->lun_lock);
8606 lun->flags &= ~CTL_LUN_PR_RESERVED;
8608 lun->pr_key_count = 0;
8609 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8611 ctl_clr_prkey(lun, residx);
8612 for (i=0; i < 2*CTL_MAX_INITIATORS; i++)
8613 if (ctl_get_prkey(lun, i) != 0) {
8614 ctl_clr_prkey(lun, i);
8615 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8617 lun->PRGeneration++;
8618 mtx_unlock(&lun->lun_lock);
8619 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8620 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8621 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8622 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8623 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8624 printf("CTL:Persis Out error returned from "
8625 "ctl_ha_msg_send %d\n", isc_retval);
8630 case SPRO_PRE_ABO: {
8633 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8634 residx, ctsio, cdb, param);
8636 return (CTL_RETVAL_COMPLETE);
8640 panic("Invalid PR type %x", cdb->action);
8644 free(ctsio->kern_data_ptr, M_CTL);
8645 ctl_set_success(ctsio);
8646 ctl_done((union ctl_io *)ctsio);
8652 * This routine is for handling a message from the other SC pertaining to
8653 * persistent reserve out. All the error checking will have been done
8654 * so only perorming the action need be done here to keep the two
8658 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8660 struct ctl_lun *lun;
8661 struct ctl_softc *softc;
8665 softc = control_softc;
8667 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8668 lun = softc->ctl_luns[targ_lun];
8669 mtx_lock(&lun->lun_lock);
8670 switch(msg->pr.pr_info.action) {
8671 case CTL_PR_REG_KEY:
8672 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8673 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8674 lun->pr_key_count++;
8675 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8676 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8677 lun->PRGeneration++;
8680 case CTL_PR_UNREG_KEY:
8681 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8682 lun->pr_key_count--;
8684 /* XXX Need to see if the reservation has been released */
8685 /* if so do we need to generate UA? */
8686 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8687 lun->flags &= ~CTL_LUN_PR_RESERVED;
8688 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8690 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8691 || lun->res_type == SPR_TYPE_EX_AC_RO)
8692 && lun->pr_key_count) {
8694 * If the reservation is a registrants
8695 * only type we need to generate a UA
8696 * for other registered inits. The
8697 * sense code should be RESERVATIONS
8701 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8702 if (ctl_get_prkey(lun, i +
8703 softc->persis_offset) == 0)
8706 lun->pending_ua[i] |=
8711 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8712 if (lun->pr_key_count==0) {
8713 lun->flags &= ~CTL_LUN_PR_RESERVED;
8715 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8718 lun->PRGeneration++;
8721 case CTL_PR_RESERVE:
8722 lun->flags |= CTL_LUN_PR_RESERVED;
8723 lun->res_type = msg->pr.pr_info.res_type;
8724 lun->pr_res_idx = msg->pr.pr_info.residx;
8728 case CTL_PR_RELEASE:
8730 * if this isn't an exclusive access res generate UA for all
8731 * other registrants.
8733 if (lun->res_type != SPR_TYPE_EX_AC
8734 && lun->res_type != SPR_TYPE_WR_EX) {
8735 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8736 if (ctl_get_prkey(lun, i + softc->persis_offset) != 0)
8737 lun->pending_ua[i] |=
8741 lun->flags &= ~CTL_LUN_PR_RESERVED;
8742 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8746 case CTL_PR_PREEMPT:
8747 ctl_pro_preempt_other(lun, msg);
8750 lun->flags &= ~CTL_LUN_PR_RESERVED;
8752 lun->pr_key_count = 0;
8753 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8755 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8756 if (ctl_get_prkey(lun, i) == 0)
8758 ctl_clr_prkey(lun, i);
8759 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8761 lun->PRGeneration++;
8765 mtx_unlock(&lun->lun_lock);
8769 ctl_read_write(struct ctl_scsiio *ctsio)
8771 struct ctl_lun *lun;
8772 struct ctl_lba_len_flags *lbalen;
8774 uint32_t num_blocks;
8778 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8780 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8783 retval = CTL_RETVAL_COMPLETE;
8785 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8786 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8787 switch (ctsio->cdb[0]) {
8790 struct scsi_rw_6 *cdb;
8792 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8794 lba = scsi_3btoul(cdb->addr);
8795 /* only 5 bits are valid in the most significant address byte */
8797 num_blocks = cdb->length;
8799 * This is correct according to SBC-2.
8801 if (num_blocks == 0)
8807 struct scsi_rw_10 *cdb;
8809 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8810 if (cdb->byte2 & SRW10_FUA)
8811 flags |= CTL_LLF_FUA;
8812 if (cdb->byte2 & SRW10_DPO)
8813 flags |= CTL_LLF_DPO;
8814 lba = scsi_4btoul(cdb->addr);
8815 num_blocks = scsi_2btoul(cdb->length);
8818 case WRITE_VERIFY_10: {
8819 struct scsi_write_verify_10 *cdb;
8821 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8822 flags |= CTL_LLF_FUA;
8823 if (cdb->byte2 & SWV_DPO)
8824 flags |= CTL_LLF_DPO;
8825 lba = scsi_4btoul(cdb->addr);
8826 num_blocks = scsi_2btoul(cdb->length);
8831 struct scsi_rw_12 *cdb;
8833 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8834 if (cdb->byte2 & SRW12_FUA)
8835 flags |= CTL_LLF_FUA;
8836 if (cdb->byte2 & SRW12_DPO)
8837 flags |= CTL_LLF_DPO;
8838 lba = scsi_4btoul(cdb->addr);
8839 num_blocks = scsi_4btoul(cdb->length);
8842 case WRITE_VERIFY_12: {
8843 struct scsi_write_verify_12 *cdb;
8845 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8846 flags |= CTL_LLF_FUA;
8847 if (cdb->byte2 & SWV_DPO)
8848 flags |= CTL_LLF_DPO;
8849 lba = scsi_4btoul(cdb->addr);
8850 num_blocks = scsi_4btoul(cdb->length);
8855 struct scsi_rw_16 *cdb;
8857 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8858 if (cdb->byte2 & SRW12_FUA)
8859 flags |= CTL_LLF_FUA;
8860 if (cdb->byte2 & SRW12_DPO)
8861 flags |= CTL_LLF_DPO;
8862 lba = scsi_8btou64(cdb->addr);
8863 num_blocks = scsi_4btoul(cdb->length);
8866 case WRITE_ATOMIC_16: {
8867 struct scsi_rw_16 *cdb;
8869 if (lun->be_lun->atomicblock == 0) {
8870 ctl_set_invalid_opcode(ctsio);
8871 ctl_done((union ctl_io *)ctsio);
8872 return (CTL_RETVAL_COMPLETE);
8875 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8876 if (cdb->byte2 & SRW12_FUA)
8877 flags |= CTL_LLF_FUA;
8878 if (cdb->byte2 & SRW12_DPO)
8879 flags |= CTL_LLF_DPO;
8880 lba = scsi_8btou64(cdb->addr);
8881 num_blocks = scsi_4btoul(cdb->length);
8882 if (num_blocks > lun->be_lun->atomicblock) {
8883 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8884 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8886 ctl_done((union ctl_io *)ctsio);
8887 return (CTL_RETVAL_COMPLETE);
8891 case WRITE_VERIFY_16: {
8892 struct scsi_write_verify_16 *cdb;
8894 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8895 flags |= CTL_LLF_FUA;
8896 if (cdb->byte2 & SWV_DPO)
8897 flags |= CTL_LLF_DPO;
8898 lba = scsi_8btou64(cdb->addr);
8899 num_blocks = scsi_4btoul(cdb->length);
8904 * We got a command we don't support. This shouldn't
8905 * happen, commands should be filtered out above us.
8907 ctl_set_invalid_opcode(ctsio);
8908 ctl_done((union ctl_io *)ctsio);
8910 return (CTL_RETVAL_COMPLETE);
8911 break; /* NOTREACHED */
8915 * The first check is to make sure we're in bounds, the second
8916 * check is to catch wrap-around problems. If the lba + num blocks
8917 * is less than the lba, then we've wrapped around and the block
8918 * range is invalid anyway.
8920 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8921 || ((lba + num_blocks) < lba)) {
8922 ctl_set_lba_out_of_range(ctsio);
8923 ctl_done((union ctl_io *)ctsio);
8924 return (CTL_RETVAL_COMPLETE);
8928 * According to SBC-3, a transfer length of 0 is not an error.
8929 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8930 * translates to 256 blocks for those commands.
8932 if (num_blocks == 0) {
8933 ctl_set_success(ctsio);
8934 ctl_done((union ctl_io *)ctsio);
8935 return (CTL_RETVAL_COMPLETE);
8938 /* Set FUA and/or DPO if caches are disabled. */
8940 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8942 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8944 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8946 flags |= CTL_LLF_FUA;
8949 lbalen = (struct ctl_lba_len_flags *)
8950 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8952 lbalen->len = num_blocks;
8953 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8955 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8956 ctsio->kern_rel_offset = 0;
8958 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8960 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8966 ctl_cnw_cont(union ctl_io *io)
8968 struct ctl_scsiio *ctsio;
8969 struct ctl_lun *lun;
8970 struct ctl_lba_len_flags *lbalen;
8973 ctsio = &io->scsiio;
8974 ctsio->io_hdr.status = CTL_STATUS_NONE;
8975 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8976 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8977 lbalen = (struct ctl_lba_len_flags *)
8978 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8979 lbalen->flags &= ~CTL_LLF_COMPARE;
8980 lbalen->flags |= CTL_LLF_WRITE;
8982 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8983 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8988 ctl_cnw(struct ctl_scsiio *ctsio)
8990 struct ctl_lun *lun;
8991 struct ctl_lba_len_flags *lbalen;
8993 uint32_t num_blocks;
8996 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8998 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
9001 retval = CTL_RETVAL_COMPLETE;
9003 switch (ctsio->cdb[0]) {
9004 case COMPARE_AND_WRITE: {
9005 struct scsi_compare_and_write *cdb;
9007 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
9008 if (cdb->byte2 & SRW10_FUA)
9009 flags |= CTL_LLF_FUA;
9010 if (cdb->byte2 & SRW10_DPO)
9011 flags |= CTL_LLF_DPO;
9012 lba = scsi_8btou64(cdb->addr);
9013 num_blocks = cdb->length;
9018 * We got a command we don't support. This shouldn't
9019 * happen, commands should be filtered out above us.
9021 ctl_set_invalid_opcode(ctsio);
9022 ctl_done((union ctl_io *)ctsio);
9024 return (CTL_RETVAL_COMPLETE);
9025 break; /* NOTREACHED */
9029 * The first check is to make sure we're in bounds, the second
9030 * check is to catch wrap-around problems. If the lba + num blocks
9031 * is less than the lba, then we've wrapped around and the block
9032 * range is invalid anyway.
9034 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9035 || ((lba + num_blocks) < lba)) {
9036 ctl_set_lba_out_of_range(ctsio);
9037 ctl_done((union ctl_io *)ctsio);
9038 return (CTL_RETVAL_COMPLETE);
9042 * According to SBC-3, a transfer length of 0 is not an error.
9044 if (num_blocks == 0) {
9045 ctl_set_success(ctsio);
9046 ctl_done((union ctl_io *)ctsio);
9047 return (CTL_RETVAL_COMPLETE);
9050 /* Set FUA if write cache is disabled. */
9051 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9053 flags |= CTL_LLF_FUA;
9055 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
9056 ctsio->kern_rel_offset = 0;
9059 * Set the IO_CONT flag, so that if this I/O gets passed to
9060 * ctl_data_submit_done(), it'll get passed back to
9061 * ctl_ctl_cnw_cont() for further processing.
9063 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
9064 ctsio->io_cont = ctl_cnw_cont;
9066 lbalen = (struct ctl_lba_len_flags *)
9067 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9069 lbalen->len = num_blocks;
9070 lbalen->flags = CTL_LLF_COMPARE | flags;
9072 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
9073 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9078 ctl_verify(struct ctl_scsiio *ctsio)
9080 struct ctl_lun *lun;
9081 struct ctl_lba_len_flags *lbalen;
9083 uint32_t num_blocks;
9087 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9089 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
9092 flags = CTL_LLF_FUA;
9093 retval = CTL_RETVAL_COMPLETE;
9095 switch (ctsio->cdb[0]) {
9097 struct scsi_verify_10 *cdb;
9099 cdb = (struct scsi_verify_10 *)ctsio->cdb;
9100 if (cdb->byte2 & SVFY_BYTCHK)
9102 if (cdb->byte2 & SVFY_DPO)
9103 flags |= CTL_LLF_DPO;
9104 lba = scsi_4btoul(cdb->addr);
9105 num_blocks = scsi_2btoul(cdb->length);
9109 struct scsi_verify_12 *cdb;
9111 cdb = (struct scsi_verify_12 *)ctsio->cdb;
9112 if (cdb->byte2 & SVFY_BYTCHK)
9114 if (cdb->byte2 & SVFY_DPO)
9115 flags |= CTL_LLF_DPO;
9116 lba = scsi_4btoul(cdb->addr);
9117 num_blocks = scsi_4btoul(cdb->length);
9121 struct scsi_rw_16 *cdb;
9123 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9124 if (cdb->byte2 & SVFY_BYTCHK)
9126 if (cdb->byte2 & SVFY_DPO)
9127 flags |= CTL_LLF_DPO;
9128 lba = scsi_8btou64(cdb->addr);
9129 num_blocks = scsi_4btoul(cdb->length);
9134 * We got a command we don't support. This shouldn't
9135 * happen, commands should be filtered out above us.
9137 ctl_set_invalid_opcode(ctsio);
9138 ctl_done((union ctl_io *)ctsio);
9139 return (CTL_RETVAL_COMPLETE);
9143 * The first check is to make sure we're in bounds, the second
9144 * check is to catch wrap-around problems. If the lba + num blocks
9145 * is less than the lba, then we've wrapped around and the block
9146 * range is invalid anyway.
9148 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9149 || ((lba + num_blocks) < lba)) {
9150 ctl_set_lba_out_of_range(ctsio);
9151 ctl_done((union ctl_io *)ctsio);
9152 return (CTL_RETVAL_COMPLETE);
9156 * According to SBC-3, a transfer length of 0 is not an error.
9158 if (num_blocks == 0) {
9159 ctl_set_success(ctsio);
9160 ctl_done((union ctl_io *)ctsio);
9161 return (CTL_RETVAL_COMPLETE);
9164 lbalen = (struct ctl_lba_len_flags *)
9165 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9167 lbalen->len = num_blocks;
9169 lbalen->flags = CTL_LLF_COMPARE | flags;
9170 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9172 lbalen->flags = CTL_LLF_VERIFY | flags;
9173 ctsio->kern_total_len = 0;
9175 ctsio->kern_rel_offset = 0;
9177 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9178 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9183 ctl_report_luns(struct ctl_scsiio *ctsio)
9185 struct scsi_report_luns *cdb;
9186 struct scsi_report_luns_data *lun_data;
9187 struct ctl_lun *lun, *request_lun;
9188 int num_luns, retval;
9189 uint32_t alloc_len, lun_datalen;
9190 int num_filled, well_known;
9191 uint32_t initidx, targ_lun_id, lun_id;
9193 retval = CTL_RETVAL_COMPLETE;
9196 cdb = (struct scsi_report_luns *)ctsio->cdb;
9198 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9200 mtx_lock(&control_softc->ctl_lock);
9201 num_luns = control_softc->num_luns;
9202 mtx_unlock(&control_softc->ctl_lock);
9204 switch (cdb->select_report) {
9205 case RPL_REPORT_DEFAULT:
9206 case RPL_REPORT_ALL:
9208 case RPL_REPORT_WELLKNOWN:
9213 ctl_set_invalid_field(ctsio,
9219 ctl_done((union ctl_io *)ctsio);
9221 break; /* NOTREACHED */
9224 alloc_len = scsi_4btoul(cdb->length);
9226 * The initiator has to allocate at least 16 bytes for this request,
9227 * so he can at least get the header and the first LUN. Otherwise
9228 * we reject the request (per SPC-3 rev 14, section 6.21).
9230 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9231 sizeof(struct scsi_report_luns_lundata))) {
9232 ctl_set_invalid_field(ctsio,
9238 ctl_done((union ctl_io *)ctsio);
9242 request_lun = (struct ctl_lun *)
9243 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9245 lun_datalen = sizeof(*lun_data) +
9246 (num_luns * sizeof(struct scsi_report_luns_lundata));
9248 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9249 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9250 ctsio->kern_sg_entries = 0;
9252 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9254 mtx_lock(&control_softc->ctl_lock);
9255 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9256 lun_id = ctl_map_lun(ctsio->io_hdr.nexus.targ_port, targ_lun_id);
9257 if (lun_id >= CTL_MAX_LUNS)
9259 lun = control_softc->ctl_luns[lun_id];
9263 if (targ_lun_id <= 0xff) {
9265 * Peripheral addressing method, bus number 0.
9267 lun_data->luns[num_filled].lundata[0] =
9268 RPL_LUNDATA_ATYP_PERIPH;
9269 lun_data->luns[num_filled].lundata[1] = targ_lun_id;
9271 } else if (targ_lun_id <= 0x3fff) {
9273 * Flat addressing method.
9275 lun_data->luns[num_filled].lundata[0] =
9276 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8);
9277 lun_data->luns[num_filled].lundata[1] =
9278 (targ_lun_id & 0xff);
9280 } else if (targ_lun_id <= 0xffffff) {
9282 * Extended flat addressing method.
9284 lun_data->luns[num_filled].lundata[0] =
9285 RPL_LUNDATA_ATYP_EXTLUN | 0x12;
9286 scsi_ulto3b(targ_lun_id,
9287 &lun_data->luns[num_filled].lundata[1]);
9290 printf("ctl_report_luns: bogus LUN number %jd, "
9291 "skipping\n", (intmax_t)targ_lun_id);
9294 * According to SPC-3, rev 14 section 6.21:
9296 * "The execution of a REPORT LUNS command to any valid and
9297 * installed logical unit shall clear the REPORTED LUNS DATA
9298 * HAS CHANGED unit attention condition for all logical
9299 * units of that target with respect to the requesting
9300 * initiator. A valid and installed logical unit is one
9301 * having a PERIPHERAL QUALIFIER of 000b in the standard
9302 * INQUIRY data (see 6.4.2)."
9304 * If request_lun is NULL, the LUN this report luns command
9305 * was issued to is either disabled or doesn't exist. In that
9306 * case, we shouldn't clear any pending lun change unit
9309 if (request_lun != NULL) {
9310 mtx_lock(&lun->lun_lock);
9311 lun->pending_ua[initidx] &= ~CTL_UA_LUN_CHANGE;
9312 mtx_unlock(&lun->lun_lock);
9315 mtx_unlock(&control_softc->ctl_lock);
9318 * It's quite possible that we've returned fewer LUNs than we allocated
9319 * space for. Trim it.
9321 lun_datalen = sizeof(*lun_data) +
9322 (num_filled * sizeof(struct scsi_report_luns_lundata));
9324 if (lun_datalen < alloc_len) {
9325 ctsio->residual = alloc_len - lun_datalen;
9326 ctsio->kern_data_len = lun_datalen;
9327 ctsio->kern_total_len = lun_datalen;
9329 ctsio->residual = 0;
9330 ctsio->kern_data_len = alloc_len;
9331 ctsio->kern_total_len = alloc_len;
9333 ctsio->kern_data_resid = 0;
9334 ctsio->kern_rel_offset = 0;
9335 ctsio->kern_sg_entries = 0;
9338 * We set this to the actual data length, regardless of how much
9339 * space we actually have to return results. If the user looks at
9340 * this value, he'll know whether or not he allocated enough space
9341 * and reissue the command if necessary. We don't support well
9342 * known logical units, so if the user asks for that, return none.
9344 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9347 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9350 ctl_set_success(ctsio);
9351 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9352 ctsio->be_move_done = ctl_config_move_done;
9353 ctl_datamove((union ctl_io *)ctsio);
9358 ctl_request_sense(struct ctl_scsiio *ctsio)
9360 struct scsi_request_sense *cdb;
9361 struct scsi_sense_data *sense_ptr;
9362 struct ctl_lun *lun;
9365 scsi_sense_data_type sense_format;
9367 cdb = (struct scsi_request_sense *)ctsio->cdb;
9369 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9371 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9374 * Determine which sense format the user wants.
9376 if (cdb->byte2 & SRS_DESC)
9377 sense_format = SSD_TYPE_DESC;
9379 sense_format = SSD_TYPE_FIXED;
9381 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9382 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9383 ctsio->kern_sg_entries = 0;
9386 * struct scsi_sense_data, which is currently set to 256 bytes, is
9387 * larger than the largest allowed value for the length field in the
9388 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9390 ctsio->residual = 0;
9391 ctsio->kern_data_len = cdb->length;
9392 ctsio->kern_total_len = cdb->length;
9394 ctsio->kern_data_resid = 0;
9395 ctsio->kern_rel_offset = 0;
9396 ctsio->kern_sg_entries = 0;
9399 * If we don't have a LUN, we don't have any pending sense.
9405 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9407 * Check for pending sense, and then for pending unit attentions.
9408 * Pending sense gets returned first, then pending unit attentions.
9410 mtx_lock(&lun->lun_lock);
9412 if (ctl_is_set(lun->have_ca, initidx)) {
9413 scsi_sense_data_type stored_format;
9416 * Check to see which sense format was used for the stored
9419 stored_format = scsi_sense_type(&lun->pending_sense[initidx]);
9422 * If the user requested a different sense format than the
9423 * one we stored, then we need to convert it to the other
9424 * format. If we're going from descriptor to fixed format
9425 * sense data, we may lose things in translation, depending
9426 * on what options were used.
9428 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9429 * for some reason we'll just copy it out as-is.
9431 if ((stored_format == SSD_TYPE_FIXED)
9432 && (sense_format == SSD_TYPE_DESC))
9433 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9434 &lun->pending_sense[initidx],
9435 (struct scsi_sense_data_desc *)sense_ptr);
9436 else if ((stored_format == SSD_TYPE_DESC)
9437 && (sense_format == SSD_TYPE_FIXED))
9438 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9439 &lun->pending_sense[initidx],
9440 (struct scsi_sense_data_fixed *)sense_ptr);
9442 memcpy(sense_ptr, &lun->pending_sense[initidx],
9443 ctl_min(sizeof(*sense_ptr),
9444 sizeof(lun->pending_sense[initidx])));
9446 ctl_clear_mask(lun->have_ca, initidx);
9450 if (lun->pending_ua[initidx] != CTL_UA_NONE) {
9451 ctl_ua_type ua_type;
9453 ua_type = ctl_build_ua(&lun->pending_ua[initidx],
9454 sense_ptr, sense_format);
9455 if (ua_type != CTL_UA_NONE)
9458 mtx_unlock(&lun->lun_lock);
9461 * We already have a pending error, return it.
9463 if (have_error != 0) {
9465 * We report the SCSI status as OK, since the status of the
9466 * request sense command itself is OK.
9467 * We report 0 for the sense length, because we aren't doing
9468 * autosense in this case. We're reporting sense as
9471 ctl_set_success(ctsio);
9472 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9473 ctsio->be_move_done = ctl_config_move_done;
9474 ctl_datamove((union ctl_io *)ctsio);
9475 return (CTL_RETVAL_COMPLETE);
9481 * No sense information to report, so we report that everything is
9484 ctl_set_sense_data(sense_ptr,
9487 /*current_error*/ 1,
9488 /*sense_key*/ SSD_KEY_NO_SENSE,
9494 * We report 0 for the sense length, because we aren't doing
9495 * autosense in this case. We're reporting sense as parameter data.
9497 ctl_set_success(ctsio);
9498 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9499 ctsio->be_move_done = ctl_config_move_done;
9500 ctl_datamove((union ctl_io *)ctsio);
9501 return (CTL_RETVAL_COMPLETE);
9505 ctl_tur(struct ctl_scsiio *ctsio)
9508 CTL_DEBUG_PRINT(("ctl_tur\n"));
9510 ctl_set_success(ctsio);
9511 ctl_done((union ctl_io *)ctsio);
9513 return (CTL_RETVAL_COMPLETE);
9518 ctl_cmddt_inquiry(struct ctl_scsiio *ctsio)
9525 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9527 struct scsi_vpd_supported_pages *pages;
9529 struct ctl_lun *lun;
9531 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9533 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9534 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9535 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9536 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9537 ctsio->kern_sg_entries = 0;
9539 if (sup_page_size < alloc_len) {
9540 ctsio->residual = alloc_len - sup_page_size;
9541 ctsio->kern_data_len = sup_page_size;
9542 ctsio->kern_total_len = sup_page_size;
9544 ctsio->residual = 0;
9545 ctsio->kern_data_len = alloc_len;
9546 ctsio->kern_total_len = alloc_len;
9548 ctsio->kern_data_resid = 0;
9549 ctsio->kern_rel_offset = 0;
9550 ctsio->kern_sg_entries = 0;
9553 * The control device is always connected. The disk device, on the
9554 * other hand, may not be online all the time. Need to change this
9555 * to figure out whether the disk device is actually online or not.
9558 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9559 lun->be_lun->lun_type;
9561 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9563 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES;
9564 /* Supported VPD pages */
9565 pages->page_list[0] = SVPD_SUPPORTED_PAGES;
9567 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER;
9568 /* Device Identification */
9569 pages->page_list[2] = SVPD_DEVICE_ID;
9570 /* Extended INQUIRY Data */
9571 pages->page_list[3] = SVPD_EXTENDED_INQUIRY_DATA;
9572 /* Mode Page Policy */
9573 pages->page_list[4] = SVPD_MODE_PAGE_POLICY;
9575 pages->page_list[5] = SVPD_SCSI_PORTS;
9576 /* Third-party Copy */
9577 pages->page_list[6] = SVPD_SCSI_TPC;
9579 pages->page_list[7] = SVPD_BLOCK_LIMITS;
9580 /* Block Device Characteristics */
9581 pages->page_list[8] = SVPD_BDC;
9582 /* Logical Block Provisioning */
9583 pages->page_list[9] = SVPD_LBP;
9585 ctl_set_success(ctsio);
9586 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9587 ctsio->be_move_done = ctl_config_move_done;
9588 ctl_datamove((union ctl_io *)ctsio);
9589 return (CTL_RETVAL_COMPLETE);
9593 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9595 struct scsi_vpd_unit_serial_number *sn_ptr;
9596 struct ctl_lun *lun;
9599 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9601 data_len = 4 + CTL_SN_LEN;
9602 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9603 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9604 if (data_len < alloc_len) {
9605 ctsio->residual = alloc_len - data_len;
9606 ctsio->kern_data_len = data_len;
9607 ctsio->kern_total_len = data_len;
9609 ctsio->residual = 0;
9610 ctsio->kern_data_len = alloc_len;
9611 ctsio->kern_total_len = alloc_len;
9613 ctsio->kern_data_resid = 0;
9614 ctsio->kern_rel_offset = 0;
9615 ctsio->kern_sg_entries = 0;
9618 * The control device is always connected. The disk device, on the
9619 * other hand, may not be online all the time. Need to change this
9620 * to figure out whether the disk device is actually online or not.
9623 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9624 lun->be_lun->lun_type;
9626 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9628 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9629 sn_ptr->length = CTL_SN_LEN;
9631 * If we don't have a LUN, we just leave the serial number as
9635 strncpy((char *)sn_ptr->serial_num,
9636 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9638 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9640 ctl_set_success(ctsio);
9641 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9642 ctsio->be_move_done = ctl_config_move_done;
9643 ctl_datamove((union ctl_io *)ctsio);
9644 return (CTL_RETVAL_COMPLETE);
9649 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9651 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9652 struct ctl_lun *lun;
9655 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9657 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9658 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9659 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9660 ctsio->kern_sg_entries = 0;
9662 if (data_len < alloc_len) {
9663 ctsio->residual = alloc_len - data_len;
9664 ctsio->kern_data_len = data_len;
9665 ctsio->kern_total_len = data_len;
9667 ctsio->residual = 0;
9668 ctsio->kern_data_len = alloc_len;
9669 ctsio->kern_total_len = alloc_len;
9671 ctsio->kern_data_resid = 0;
9672 ctsio->kern_rel_offset = 0;
9673 ctsio->kern_sg_entries = 0;
9676 * The control device is always connected. The disk device, on the
9677 * other hand, may not be online all the time.
9680 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9681 lun->be_lun->lun_type;
9683 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9684 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9685 eid_ptr->page_length = data_len - 4;
9686 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9687 eid_ptr->flags3 = SVPD_EID_V_SUP;
9689 ctl_set_success(ctsio);
9690 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9691 ctsio->be_move_done = ctl_config_move_done;
9692 ctl_datamove((union ctl_io *)ctsio);
9693 return (CTL_RETVAL_COMPLETE);
9697 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9699 struct scsi_vpd_mode_page_policy *mpp_ptr;
9700 struct ctl_lun *lun;
9703 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9705 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9706 sizeof(struct scsi_vpd_mode_page_policy_descr);
9708 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9709 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9710 ctsio->kern_sg_entries = 0;
9712 if (data_len < alloc_len) {
9713 ctsio->residual = alloc_len - data_len;
9714 ctsio->kern_data_len = data_len;
9715 ctsio->kern_total_len = data_len;
9717 ctsio->residual = 0;
9718 ctsio->kern_data_len = alloc_len;
9719 ctsio->kern_total_len = alloc_len;
9721 ctsio->kern_data_resid = 0;
9722 ctsio->kern_rel_offset = 0;
9723 ctsio->kern_sg_entries = 0;
9726 * The control device is always connected. The disk device, on the
9727 * other hand, may not be online all the time.
9730 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9731 lun->be_lun->lun_type;
9733 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9734 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9735 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9736 mpp_ptr->descr[0].page_code = 0x3f;
9737 mpp_ptr->descr[0].subpage_code = 0xff;
9738 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9740 ctl_set_success(ctsio);
9741 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9742 ctsio->be_move_done = ctl_config_move_done;
9743 ctl_datamove((union ctl_io *)ctsio);
9744 return (CTL_RETVAL_COMPLETE);
9748 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9750 struct scsi_vpd_device_id *devid_ptr;
9751 struct scsi_vpd_id_descriptor *desc;
9752 struct ctl_softc *ctl_softc;
9753 struct ctl_lun *lun;
9754 struct ctl_port *port;
9758 ctl_softc = control_softc;
9760 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)];
9761 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9763 data_len = sizeof(struct scsi_vpd_device_id) +
9764 sizeof(struct scsi_vpd_id_descriptor) +
9765 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9766 sizeof(struct scsi_vpd_id_descriptor) +
9767 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9768 if (lun && lun->lun_devid)
9769 data_len += lun->lun_devid->len;
9770 if (port->port_devid)
9771 data_len += port->port_devid->len;
9772 if (port->target_devid)
9773 data_len += port->target_devid->len;
9775 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9776 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9777 ctsio->kern_sg_entries = 0;
9779 if (data_len < alloc_len) {
9780 ctsio->residual = alloc_len - data_len;
9781 ctsio->kern_data_len = data_len;
9782 ctsio->kern_total_len = data_len;
9784 ctsio->residual = 0;
9785 ctsio->kern_data_len = alloc_len;
9786 ctsio->kern_total_len = alloc_len;
9788 ctsio->kern_data_resid = 0;
9789 ctsio->kern_rel_offset = 0;
9790 ctsio->kern_sg_entries = 0;
9793 * The control device is always connected. The disk device, on the
9794 * other hand, may not be online all the time.
9797 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9798 lun->be_lun->lun_type;
9800 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9801 devid_ptr->page_code = SVPD_DEVICE_ID;
9802 scsi_ulto2b(data_len - 4, devid_ptr->length);
9804 if (port->port_type == CTL_PORT_FC)
9805 proto = SCSI_PROTO_FC << 4;
9806 else if (port->port_type == CTL_PORT_ISCSI)
9807 proto = SCSI_PROTO_ISCSI << 4;
9809 proto = SCSI_PROTO_SPI << 4;
9810 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9813 * We're using a LUN association here. i.e., this device ID is a
9814 * per-LUN identifier.
9816 if (lun && lun->lun_devid) {
9817 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9818 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9819 lun->lun_devid->len);
9823 * This is for the WWPN which is a port association.
9825 if (port->port_devid) {
9826 memcpy(desc, port->port_devid->data, port->port_devid->len);
9827 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9828 port->port_devid->len);
9832 * This is for the Relative Target Port(type 4h) identifier
9834 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9835 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9836 SVPD_ID_TYPE_RELTARG;
9838 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9839 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9840 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9843 * This is for the Target Port Group(type 5h) identifier
9845 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9846 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9847 SVPD_ID_TYPE_TPORTGRP;
9849 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1,
9850 &desc->identifier[2]);
9851 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9852 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9855 * This is for the Target identifier
9857 if (port->target_devid) {
9858 memcpy(desc, port->target_devid->data, port->target_devid->len);
9861 ctl_set_success(ctsio);
9862 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9863 ctsio->be_move_done = ctl_config_move_done;
9864 ctl_datamove((union ctl_io *)ctsio);
9865 return (CTL_RETVAL_COMPLETE);
9869 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9871 struct ctl_softc *softc = control_softc;
9872 struct scsi_vpd_scsi_ports *sp;
9873 struct scsi_vpd_port_designation *pd;
9874 struct scsi_vpd_port_designation_cont *pdc;
9875 struct ctl_lun *lun;
9876 struct ctl_port *port;
9877 int data_len, num_target_ports, iid_len, id_len, g, pg, p;
9878 int num_target_port_groups;
9880 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9882 if (softc->is_single)
9883 num_target_port_groups = 1;
9885 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
9886 num_target_ports = 0;
9889 mtx_lock(&softc->ctl_lock);
9890 STAILQ_FOREACH(port, &softc->port_list, links) {
9891 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9894 ctl_map_lun_back(port->targ_port, lun->lun) >=
9898 if (port->init_devid)
9899 iid_len += port->init_devid->len;
9900 if (port->port_devid)
9901 id_len += port->port_devid->len;
9903 mtx_unlock(&softc->ctl_lock);
9905 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups *
9906 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9907 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9908 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9909 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9910 ctsio->kern_sg_entries = 0;
9912 if (data_len < alloc_len) {
9913 ctsio->residual = alloc_len - data_len;
9914 ctsio->kern_data_len = data_len;
9915 ctsio->kern_total_len = data_len;
9917 ctsio->residual = 0;
9918 ctsio->kern_data_len = alloc_len;
9919 ctsio->kern_total_len = alloc_len;
9921 ctsio->kern_data_resid = 0;
9922 ctsio->kern_rel_offset = 0;
9923 ctsio->kern_sg_entries = 0;
9926 * The control device is always connected. The disk device, on the
9927 * other hand, may not be online all the time. Need to change this
9928 * to figure out whether the disk device is actually online or not.
9931 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9932 lun->be_lun->lun_type;
9934 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9936 sp->page_code = SVPD_SCSI_PORTS;
9937 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9939 pd = &sp->design[0];
9941 mtx_lock(&softc->ctl_lock);
9942 pg = softc->port_offset / CTL_MAX_PORTS;
9943 for (g = 0; g < num_target_port_groups; g++) {
9944 STAILQ_FOREACH(port, &softc->port_list, links) {
9945 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9948 ctl_map_lun_back(port->targ_port, lun->lun) >=
9951 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS;
9952 scsi_ulto2b(p, pd->relative_port_id);
9953 if (port->init_devid && g == pg) {
9954 iid_len = port->init_devid->len;
9955 memcpy(pd->initiator_transportid,
9956 port->init_devid->data, port->init_devid->len);
9959 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9960 pdc = (struct scsi_vpd_port_designation_cont *)
9961 (&pd->initiator_transportid[iid_len]);
9962 if (port->port_devid && g == pg) {
9963 id_len = port->port_devid->len;
9964 memcpy(pdc->target_port_descriptors,
9965 port->port_devid->data, port->port_devid->len);
9968 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9969 pd = (struct scsi_vpd_port_designation *)
9970 ((uint8_t *)pdc->target_port_descriptors + id_len);
9973 mtx_unlock(&softc->ctl_lock);
9975 ctl_set_success(ctsio);
9976 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9977 ctsio->be_move_done = ctl_config_move_done;
9978 ctl_datamove((union ctl_io *)ctsio);
9979 return (CTL_RETVAL_COMPLETE);
9983 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9985 struct scsi_vpd_block_limits *bl_ptr;
9986 struct ctl_lun *lun;
9989 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9991 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9992 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9993 ctsio->kern_sg_entries = 0;
9995 if (sizeof(*bl_ptr) < alloc_len) {
9996 ctsio->residual = alloc_len - sizeof(*bl_ptr);
9997 ctsio->kern_data_len = sizeof(*bl_ptr);
9998 ctsio->kern_total_len = sizeof(*bl_ptr);
10000 ctsio->residual = 0;
10001 ctsio->kern_data_len = alloc_len;
10002 ctsio->kern_total_len = alloc_len;
10004 ctsio->kern_data_resid = 0;
10005 ctsio->kern_rel_offset = 0;
10006 ctsio->kern_sg_entries = 0;
10009 * The control device is always connected. The disk device, on the
10010 * other hand, may not be online all the time. Need to change this
10011 * to figure out whether the disk device is actually online or not.
10014 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10015 lun->be_lun->lun_type;
10017 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10019 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
10020 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
10021 bl_ptr->max_cmp_write_len = 0xff;
10022 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
10024 bs = lun->be_lun->blocksize;
10025 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len);
10026 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10027 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
10028 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
10029 if (lun->be_lun->pblockexp != 0) {
10030 scsi_ulto4b((1 << lun->be_lun->pblockexp),
10031 bl_ptr->opt_unmap_grain);
10032 scsi_ulto4b(0x80000000 | lun->be_lun->pblockoff,
10033 bl_ptr->unmap_grain_align);
10036 scsi_ulto4b(lun->be_lun->atomicblock,
10037 bl_ptr->max_atomic_transfer_length);
10038 scsi_ulto4b(0, bl_ptr->atomic_alignment);
10039 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
10041 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
10043 ctl_set_success(ctsio);
10044 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10045 ctsio->be_move_done = ctl_config_move_done;
10046 ctl_datamove((union ctl_io *)ctsio);
10047 return (CTL_RETVAL_COMPLETE);
10051 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
10053 struct scsi_vpd_block_device_characteristics *bdc_ptr;
10054 struct ctl_lun *lun;
10058 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10060 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
10061 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
10062 ctsio->kern_sg_entries = 0;
10064 if (sizeof(*bdc_ptr) < alloc_len) {
10065 ctsio->residual = alloc_len - sizeof(*bdc_ptr);
10066 ctsio->kern_data_len = sizeof(*bdc_ptr);
10067 ctsio->kern_total_len = sizeof(*bdc_ptr);
10069 ctsio->residual = 0;
10070 ctsio->kern_data_len = alloc_len;
10071 ctsio->kern_total_len = alloc_len;
10073 ctsio->kern_data_resid = 0;
10074 ctsio->kern_rel_offset = 0;
10075 ctsio->kern_sg_entries = 0;
10078 * The control device is always connected. The disk device, on the
10079 * other hand, may not be online all the time. Need to change this
10080 * to figure out whether the disk device is actually online or not.
10083 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10084 lun->be_lun->lun_type;
10086 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10087 bdc_ptr->page_code = SVPD_BDC;
10088 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
10090 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL)
10091 i = strtol(value, NULL, 0);
10093 i = CTL_DEFAULT_ROTATION_RATE;
10094 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
10096 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL)
10097 i = strtol(value, NULL, 0);
10100 bdc_ptr->wab_wac_ff = (i & 0x0f);
10101 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
10103 ctl_set_success(ctsio);
10104 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10105 ctsio->be_move_done = ctl_config_move_done;
10106 ctl_datamove((union ctl_io *)ctsio);
10107 return (CTL_RETVAL_COMPLETE);
10111 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
10113 struct scsi_vpd_logical_block_prov *lbp_ptr;
10114 struct ctl_lun *lun;
10116 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10118 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
10119 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
10120 ctsio->kern_sg_entries = 0;
10122 if (sizeof(*lbp_ptr) < alloc_len) {
10123 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
10124 ctsio->kern_data_len = sizeof(*lbp_ptr);
10125 ctsio->kern_total_len = sizeof(*lbp_ptr);
10127 ctsio->residual = 0;
10128 ctsio->kern_data_len = alloc_len;
10129 ctsio->kern_total_len = alloc_len;
10131 ctsio->kern_data_resid = 0;
10132 ctsio->kern_rel_offset = 0;
10133 ctsio->kern_sg_entries = 0;
10136 * The control device is always connected. The disk device, on the
10137 * other hand, may not be online all the time. Need to change this
10138 * to figure out whether the disk device is actually online or not.
10141 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10142 lun->be_lun->lun_type;
10144 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10146 lbp_ptr->page_code = SVPD_LBP;
10147 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
10148 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10149 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
10150 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
10151 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
10152 lbp_ptr->prov_type = SVPD_LBP_THIN;
10155 ctl_set_success(ctsio);
10156 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10157 ctsio->be_move_done = ctl_config_move_done;
10158 ctl_datamove((union ctl_io *)ctsio);
10159 return (CTL_RETVAL_COMPLETE);
10163 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
10165 struct scsi_inquiry *cdb;
10166 int alloc_len, retval;
10168 cdb = (struct scsi_inquiry *)ctsio->cdb;
10170 retval = CTL_RETVAL_COMPLETE;
10172 alloc_len = scsi_2btoul(cdb->length);
10174 switch (cdb->page_code) {
10175 case SVPD_SUPPORTED_PAGES:
10176 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
10178 case SVPD_UNIT_SERIAL_NUMBER:
10179 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
10181 case SVPD_DEVICE_ID:
10182 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
10184 case SVPD_EXTENDED_INQUIRY_DATA:
10185 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
10187 case SVPD_MODE_PAGE_POLICY:
10188 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
10190 case SVPD_SCSI_PORTS:
10191 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10193 case SVPD_SCSI_TPC:
10194 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10196 case SVPD_BLOCK_LIMITS:
10197 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10200 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10203 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10206 ctl_set_invalid_field(ctsio,
10212 ctl_done((union ctl_io *)ctsio);
10213 retval = CTL_RETVAL_COMPLETE;
10221 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10223 struct scsi_inquiry_data *inq_ptr;
10224 struct scsi_inquiry *cdb;
10225 struct ctl_softc *ctl_softc;
10226 struct ctl_lun *lun;
10228 uint32_t alloc_len, data_len;
10229 ctl_port_type port_type;
10231 ctl_softc = control_softc;
10234 * Figure out whether we're talking to a Fibre Channel port or not.
10235 * We treat the ioctl front end, and any SCSI adapters, as packetized
10238 port_type = ctl_softc->ctl_ports[
10239 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type;
10240 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10241 port_type = CTL_PORT_SCSI;
10243 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10244 cdb = (struct scsi_inquiry *)ctsio->cdb;
10245 alloc_len = scsi_2btoul(cdb->length);
10248 * We malloc the full inquiry data size here and fill it
10249 * in. If the user only asks for less, we'll give him
10252 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10253 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10254 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10255 ctsio->kern_sg_entries = 0;
10256 ctsio->kern_data_resid = 0;
10257 ctsio->kern_rel_offset = 0;
10259 if (data_len < alloc_len) {
10260 ctsio->residual = alloc_len - data_len;
10261 ctsio->kern_data_len = data_len;
10262 ctsio->kern_total_len = data_len;
10264 ctsio->residual = 0;
10265 ctsio->kern_data_len = alloc_len;
10266 ctsio->kern_total_len = alloc_len;
10270 * If we have a LUN configured, report it as connected. Otherwise,
10271 * report that it is offline or no device is supported, depending
10272 * on the value of inquiry_pq_no_lun.
10274 * According to the spec (SPC-4 r34), the peripheral qualifier
10275 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario:
10277 * "A peripheral device having the specified peripheral device type
10278 * is not connected to this logical unit. However, the device
10279 * server is capable of supporting the specified peripheral device
10280 * type on this logical unit."
10282 * According to the same spec, the peripheral qualifier
10283 * SID_QUAL_BAD_LU (011b) is used in this scenario:
10285 * "The device server is not capable of supporting a peripheral
10286 * device on this logical unit. For this peripheral qualifier the
10287 * peripheral device type shall be set to 1Fh. All other peripheral
10288 * device type values are reserved for this peripheral qualifier."
10290 * Given the text, it would seem that we probably want to report that
10291 * the LUN is offline here. There is no LUN connected, but we can
10292 * support a LUN at the given LUN number.
10294 * In the real world, though, it sounds like things are a little
10297 * - Linux, when presented with a LUN with the offline peripheral
10298 * qualifier, will create an sg driver instance for it. So when
10299 * you attach it to CTL, you wind up with a ton of sg driver
10300 * instances. (One for every LUN that Linux bothered to probe.)
10301 * Linux does this despite the fact that it issues a REPORT LUNs
10302 * to LUN 0 to get the inventory of supported LUNs.
10304 * - There is other anecdotal evidence (from Emulex folks) about
10305 * arrays that use the offline peripheral qualifier for LUNs that
10306 * are on the "passive" path in an active/passive array.
10308 * So the solution is provide a hopefully reasonable default
10309 * (return bad/no LUN) and allow the user to change the behavior
10310 * with a tunable/sysctl variable.
10313 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10314 lun->be_lun->lun_type;
10315 else if (ctl_softc->inquiry_pq_no_lun == 0)
10316 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10318 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10320 /* RMB in byte 2 is 0 */
10321 inq_ptr->version = SCSI_REV_SPC4;
10324 * According to SAM-3, even if a device only supports a single
10325 * level of LUN addressing, it should still set the HISUP bit:
10327 * 4.9.1 Logical unit numbers overview
10329 * All logical unit number formats described in this standard are
10330 * hierarchical in structure even when only a single level in that
10331 * hierarchy is used. The HISUP bit shall be set to one in the
10332 * standard INQUIRY data (see SPC-2) when any logical unit number
10333 * format described in this standard is used. Non-hierarchical
10334 * formats are outside the scope of this standard.
10336 * Therefore we set the HiSup bit here.
10338 * The reponse format is 2, per SPC-3.
10340 inq_ptr->response_format = SID_HiSup | 2;
10342 inq_ptr->additional_length = data_len -
10343 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10344 CTL_DEBUG_PRINT(("additional_length = %d\n",
10345 inq_ptr->additional_length));
10347 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10348 /* 16 bit addressing */
10349 if (port_type == CTL_PORT_SCSI)
10350 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10351 /* XXX set the SID_MultiP bit here if we're actually going to
10352 respond on multiple ports */
10353 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10355 /* 16 bit data bus, synchronous transfers */
10356 if (port_type == CTL_PORT_SCSI)
10357 inq_ptr->flags = SID_WBus16 | SID_Sync;
10359 * XXX KDM do we want to support tagged queueing on the control
10363 || (lun->be_lun->lun_type != T_PROCESSOR))
10364 inq_ptr->flags |= SID_CmdQue;
10366 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10367 * We have 8 bytes for the vendor name, and 16 bytes for the device
10368 * name and 4 bytes for the revision.
10370 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10371 "vendor")) == NULL) {
10372 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10374 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10375 strncpy(inq_ptr->vendor, val,
10376 min(sizeof(inq_ptr->vendor), strlen(val)));
10379 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10380 sizeof(inq_ptr->product));
10381 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10382 switch (lun->be_lun->lun_type) {
10384 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10385 sizeof(inq_ptr->product));
10388 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10389 sizeof(inq_ptr->product));
10392 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10393 sizeof(inq_ptr->product));
10397 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10398 strncpy(inq_ptr->product, val,
10399 min(sizeof(inq_ptr->product), strlen(val)));
10403 * XXX make this a macro somewhere so it automatically gets
10404 * incremented when we make changes.
10406 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10407 "revision")) == NULL) {
10408 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10410 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10411 strncpy(inq_ptr->revision, val,
10412 min(sizeof(inq_ptr->revision), strlen(val)));
10416 * For parallel SCSI, we support double transition and single
10417 * transition clocking. We also support QAS (Quick Arbitration
10418 * and Selection) and Information Unit transfers on both the
10419 * control and array devices.
10421 if (port_type == CTL_PORT_SCSI)
10422 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10425 /* SAM-5 (no version claimed) */
10426 scsi_ulto2b(0x00A0, inq_ptr->version1);
10427 /* SPC-4 (no version claimed) */
10428 scsi_ulto2b(0x0460, inq_ptr->version2);
10429 if (port_type == CTL_PORT_FC) {
10430 /* FCP-2 ANSI INCITS.350:2003 */
10431 scsi_ulto2b(0x0917, inq_ptr->version3);
10432 } else if (port_type == CTL_PORT_SCSI) {
10433 /* SPI-4 ANSI INCITS.362:200x */
10434 scsi_ulto2b(0x0B56, inq_ptr->version3);
10435 } else if (port_type == CTL_PORT_ISCSI) {
10436 /* iSCSI (no version claimed) */
10437 scsi_ulto2b(0x0960, inq_ptr->version3);
10438 } else if (port_type == CTL_PORT_SAS) {
10439 /* SAS (no version claimed) */
10440 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10444 /* SBC-4 (no version claimed) */
10445 scsi_ulto2b(0x0600, inq_ptr->version4);
10447 switch (lun->be_lun->lun_type) {
10449 /* SBC-4 (no version claimed) */
10450 scsi_ulto2b(0x0600, inq_ptr->version4);
10458 ctl_set_success(ctsio);
10459 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10460 ctsio->be_move_done = ctl_config_move_done;
10461 ctl_datamove((union ctl_io *)ctsio);
10462 return (CTL_RETVAL_COMPLETE);
10466 ctl_inquiry(struct ctl_scsiio *ctsio)
10468 struct scsi_inquiry *cdb;
10471 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10473 cdb = (struct scsi_inquiry *)ctsio->cdb;
10474 if (cdb->byte2 & SI_EVPD)
10475 retval = ctl_inquiry_evpd(ctsio);
10476 else if (cdb->page_code == 0)
10477 retval = ctl_inquiry_std(ctsio);
10479 ctl_set_invalid_field(ctsio,
10485 ctl_done((union ctl_io *)ctsio);
10486 return (CTL_RETVAL_COMPLETE);
10493 * For known CDB types, parse the LBA and length.
10496 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10498 if (io->io_hdr.io_type != CTL_IO_SCSI)
10501 switch (io->scsiio.cdb[0]) {
10502 case COMPARE_AND_WRITE: {
10503 struct scsi_compare_and_write *cdb;
10505 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10507 *lba = scsi_8btou64(cdb->addr);
10508 *len = cdb->length;
10513 struct scsi_rw_6 *cdb;
10515 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10517 *lba = scsi_3btoul(cdb->addr);
10518 /* only 5 bits are valid in the most significant address byte */
10520 *len = cdb->length;
10525 struct scsi_rw_10 *cdb;
10527 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10529 *lba = scsi_4btoul(cdb->addr);
10530 *len = scsi_2btoul(cdb->length);
10533 case WRITE_VERIFY_10: {
10534 struct scsi_write_verify_10 *cdb;
10536 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10538 *lba = scsi_4btoul(cdb->addr);
10539 *len = scsi_2btoul(cdb->length);
10544 struct scsi_rw_12 *cdb;
10546 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10548 *lba = scsi_4btoul(cdb->addr);
10549 *len = scsi_4btoul(cdb->length);
10552 case WRITE_VERIFY_12: {
10553 struct scsi_write_verify_12 *cdb;
10555 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10557 *lba = scsi_4btoul(cdb->addr);
10558 *len = scsi_4btoul(cdb->length);
10563 case WRITE_ATOMIC_16: {
10564 struct scsi_rw_16 *cdb;
10566 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10568 *lba = scsi_8btou64(cdb->addr);
10569 *len = scsi_4btoul(cdb->length);
10572 case WRITE_VERIFY_16: {
10573 struct scsi_write_verify_16 *cdb;
10575 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10577 *lba = scsi_8btou64(cdb->addr);
10578 *len = scsi_4btoul(cdb->length);
10581 case WRITE_SAME_10: {
10582 struct scsi_write_same_10 *cdb;
10584 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10586 *lba = scsi_4btoul(cdb->addr);
10587 *len = scsi_2btoul(cdb->length);
10590 case WRITE_SAME_16: {
10591 struct scsi_write_same_16 *cdb;
10593 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10595 *lba = scsi_8btou64(cdb->addr);
10596 *len = scsi_4btoul(cdb->length);
10600 struct scsi_verify_10 *cdb;
10602 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10604 *lba = scsi_4btoul(cdb->addr);
10605 *len = scsi_2btoul(cdb->length);
10609 struct scsi_verify_12 *cdb;
10611 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10613 *lba = scsi_4btoul(cdb->addr);
10614 *len = scsi_4btoul(cdb->length);
10618 struct scsi_verify_16 *cdb;
10620 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10622 *lba = scsi_8btou64(cdb->addr);
10623 *len = scsi_4btoul(cdb->length);
10633 break; /* NOTREACHED */
10640 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2)
10642 uint64_t endlba1, endlba2;
10644 endlba1 = lba1 + len1 - 1;
10645 endlba2 = lba2 + len2 - 1;
10647 if ((endlba1 < lba2)
10648 || (endlba2 < lba1))
10649 return (CTL_ACTION_PASS);
10651 return (CTL_ACTION_BLOCK);
10655 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10657 struct ctl_ptr_len_flags *ptrlen;
10658 struct scsi_unmap_desc *buf, *end, *range;
10662 /* If not UNMAP -- go other way. */
10663 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10664 io->scsiio.cdb[0] != UNMAP)
10665 return (CTL_ACTION_ERROR);
10667 /* If UNMAP without data -- block and wait for data. */
10668 ptrlen = (struct ctl_ptr_len_flags *)
10669 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10670 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10671 ptrlen->ptr == NULL)
10672 return (CTL_ACTION_BLOCK);
10674 /* UNMAP with data -- check for collision. */
10675 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10676 end = buf + ptrlen->len / sizeof(*buf);
10677 for (range = buf; range < end; range++) {
10678 lba = scsi_8btou64(range->lba);
10679 len = scsi_4btoul(range->length);
10680 if ((lba < lba2 + len2) && (lba + len > lba2))
10681 return (CTL_ACTION_BLOCK);
10683 return (CTL_ACTION_PASS);
10687 ctl_extent_check(union ctl_io *io1, union ctl_io *io2)
10689 uint64_t lba1, lba2;
10690 uint64_t len1, len2;
10693 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10694 return (CTL_ACTION_ERROR);
10696 retval = ctl_extent_check_unmap(io2, lba1, len1);
10697 if (retval != CTL_ACTION_ERROR)
10700 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10701 return (CTL_ACTION_ERROR);
10703 return (ctl_extent_check_lba(lba1, len1, lba2, len2));
10707 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10708 union ctl_io *ooa_io)
10710 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10711 ctl_serialize_action *serialize_row;
10714 * The initiator attempted multiple untagged commands at the same
10715 * time. Can't do that.
10717 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10718 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10719 && ((pending_io->io_hdr.nexus.targ_port ==
10720 ooa_io->io_hdr.nexus.targ_port)
10721 && (pending_io->io_hdr.nexus.initid.id ==
10722 ooa_io->io_hdr.nexus.initid.id))
10723 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10724 return (CTL_ACTION_OVERLAP);
10727 * The initiator attempted to send multiple tagged commands with
10728 * the same ID. (It's fine if different initiators have the same
10731 * Even if all of those conditions are true, we don't kill the I/O
10732 * if the command ahead of us has been aborted. We won't end up
10733 * sending it to the FETD, and it's perfectly legal to resend a
10734 * command with the same tag number as long as the previous
10735 * instance of this tag number has been aborted somehow.
10737 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10738 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10739 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10740 && ((pending_io->io_hdr.nexus.targ_port ==
10741 ooa_io->io_hdr.nexus.targ_port)
10742 && (pending_io->io_hdr.nexus.initid.id ==
10743 ooa_io->io_hdr.nexus.initid.id))
10744 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10745 return (CTL_ACTION_OVERLAP_TAG);
10748 * If we get a head of queue tag, SAM-3 says that we should
10749 * immediately execute it.
10751 * What happens if this command would normally block for some other
10752 * reason? e.g. a request sense with a head of queue tag
10753 * immediately after a write. Normally that would block, but this
10754 * will result in its getting executed immediately...
10756 * We currently return "pass" instead of "skip", so we'll end up
10757 * going through the rest of the queue to check for overlapped tags.
10759 * XXX KDM check for other types of blockage first??
10761 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10762 return (CTL_ACTION_PASS);
10765 * Ordered tags have to block until all items ahead of them
10766 * have completed. If we get called with an ordered tag, we always
10767 * block, if something else is ahead of us in the queue.
10769 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10770 return (CTL_ACTION_BLOCK);
10773 * Simple tags get blocked until all head of queue and ordered tags
10774 * ahead of them have completed. I'm lumping untagged commands in
10775 * with simple tags here. XXX KDM is that the right thing to do?
10777 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10778 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10779 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10780 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10781 return (CTL_ACTION_BLOCK);
10783 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
10784 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
10786 serialize_row = ctl_serialize_table[ooa_entry->seridx];
10788 switch (serialize_row[pending_entry->seridx]) {
10789 case CTL_SER_BLOCK:
10790 return (CTL_ACTION_BLOCK);
10791 case CTL_SER_EXTENT:
10792 return (ctl_extent_check(pending_io, ooa_io));
10793 case CTL_SER_EXTENTOPT:
10794 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
10795 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
10796 return (ctl_extent_check(pending_io, ooa_io));
10799 return (CTL_ACTION_PASS);
10800 case CTL_SER_BLOCKOPT:
10801 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
10802 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
10803 return (CTL_ACTION_BLOCK);
10804 return (CTL_ACTION_PASS);
10806 return (CTL_ACTION_SKIP);
10808 panic("invalid serialization value %d",
10809 serialize_row[pending_entry->seridx]);
10812 return (CTL_ACTION_ERROR);
10816 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
10818 * - pending_io is generally either incoming, or on the blocked queue
10819 * - starting I/O is the I/O we want to start the check with.
10822 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
10823 union ctl_io *starting_io)
10825 union ctl_io *ooa_io;
10828 mtx_assert(&lun->lun_lock, MA_OWNED);
10831 * Run back along the OOA queue, starting with the current
10832 * blocked I/O and going through every I/O before it on the
10833 * queue. If starting_io is NULL, we'll just end up returning
10836 for (ooa_io = starting_io; ooa_io != NULL;
10837 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
10841 * This routine just checks to see whether
10842 * cur_blocked is blocked by ooa_io, which is ahead
10843 * of it in the queue. It doesn't queue/dequeue
10846 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
10848 case CTL_ACTION_BLOCK:
10849 case CTL_ACTION_OVERLAP:
10850 case CTL_ACTION_OVERLAP_TAG:
10851 case CTL_ACTION_SKIP:
10852 case CTL_ACTION_ERROR:
10854 break; /* NOTREACHED */
10855 case CTL_ACTION_PASS:
10858 panic("invalid action %d", action);
10859 break; /* NOTREACHED */
10863 return (CTL_ACTION_PASS);
10868 * - An I/O has just completed, and has been removed from the per-LUN OOA
10869 * queue, so some items on the blocked queue may now be unblocked.
10872 ctl_check_blocked(struct ctl_lun *lun)
10874 union ctl_io *cur_blocked, *next_blocked;
10876 mtx_assert(&lun->lun_lock, MA_OWNED);
10879 * Run forward from the head of the blocked queue, checking each
10880 * entry against the I/Os prior to it on the OOA queue to see if
10881 * there is still any blockage.
10883 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
10884 * with our removing a variable on it while it is traversing the
10887 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
10888 cur_blocked != NULL; cur_blocked = next_blocked) {
10889 union ctl_io *prev_ooa;
10892 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
10895 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
10896 ctl_ooaq, ooa_links);
10899 * If cur_blocked happens to be the first item in the OOA
10900 * queue now, prev_ooa will be NULL, and the action
10901 * returned will just be CTL_ACTION_PASS.
10903 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
10906 case CTL_ACTION_BLOCK:
10907 /* Nothing to do here, still blocked */
10909 case CTL_ACTION_OVERLAP:
10910 case CTL_ACTION_OVERLAP_TAG:
10912 * This shouldn't happen! In theory we've already
10913 * checked this command for overlap...
10916 case CTL_ACTION_PASS:
10917 case CTL_ACTION_SKIP: {
10918 struct ctl_softc *softc;
10919 const struct ctl_cmd_entry *entry;
10923 * The skip case shouldn't happen, this transaction
10924 * should have never made it onto the blocked queue.
10927 * This I/O is no longer blocked, we can remove it
10928 * from the blocked queue. Since this is a TAILQ
10929 * (doubly linked list), we can do O(1) removals
10930 * from any place on the list.
10932 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
10934 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
10936 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){
10938 * Need to send IO back to original side to
10941 union ctl_ha_msg msg_info;
10943 msg_info.hdr.original_sc =
10944 cur_blocked->io_hdr.original_sc;
10945 msg_info.hdr.serializing_sc = cur_blocked;
10946 msg_info.hdr.msg_type = CTL_MSG_R2R;
10947 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
10948 &msg_info, sizeof(msg_info), 0)) >
10949 CTL_HA_STATUS_SUCCESS) {
10950 printf("CTL:Check Blocked error from "
10951 "ctl_ha_msg_send %d\n",
10956 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
10957 softc = control_softc;
10960 * Check this I/O for LUN state changes that may
10961 * have happened while this command was blocked.
10962 * The LUN state may have been changed by a command
10963 * ahead of us in the queue, so we need to re-check
10964 * for any states that can be caused by SCSI
10967 if (ctl_scsiio_lun_check(softc, lun, entry,
10968 &cur_blocked->scsiio) == 0) {
10969 cur_blocked->io_hdr.flags |=
10970 CTL_FLAG_IS_WAS_ON_RTR;
10971 ctl_enqueue_rtr(cur_blocked);
10973 ctl_done(cur_blocked);
10978 * This probably shouldn't happen -- we shouldn't
10979 * get CTL_ACTION_ERROR, or anything else.
10985 return (CTL_RETVAL_COMPLETE);
10989 * This routine (with one exception) checks LUN flags that can be set by
10990 * commands ahead of us in the OOA queue. These flags have to be checked
10991 * when a command initially comes in, and when we pull a command off the
10992 * blocked queue and are preparing to execute it. The reason we have to
10993 * check these flags for commands on the blocked queue is that the LUN
10994 * state may have been changed by a command ahead of us while we're on the
10997 * Ordering is somewhat important with these checks, so please pay
10998 * careful attention to the placement of any new checks.
11001 ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
11002 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11009 mtx_assert(&lun->lun_lock, MA_OWNED);
11012 * If this shelf is a secondary shelf controller, we have to reject
11013 * any media access commands.
11015 if ((ctl_softc->flags & CTL_FLAG_ACTIVE_SHELF) == 0 &&
11016 (entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0) {
11017 ctl_set_lun_standby(ctsio);
11022 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11023 if (lun->flags & CTL_LUN_READONLY) {
11024 ctl_set_sense(ctsio, /*current_error*/ 1,
11025 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11026 /*asc*/ 0x27, /*ascq*/ 0x01, SSD_ELEM_NONE);
11030 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT]
11031 .eca_and_aen & SCP_SWP) != 0) {
11032 ctl_set_sense(ctsio, /*current_error*/ 1,
11033 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11034 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11041 * Check for a reservation conflict. If this command isn't allowed
11042 * even on reserved LUNs, and if this initiator isn't the one who
11043 * reserved us, reject the command with a reservation conflict.
11045 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
11046 if ((lun->flags & CTL_LUN_RESERVED)
11047 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11048 if (lun->res_idx != residx) {
11049 ctl_set_reservation_conflict(ctsio);
11055 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11056 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11057 /* No reservation or command is allowed. */;
11058 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11059 (lun->res_type == SPR_TYPE_WR_EX ||
11060 lun->res_type == SPR_TYPE_WR_EX_RO ||
11061 lun->res_type == SPR_TYPE_WR_EX_AR)) {
11062 /* The command is allowed for Write Exclusive resv. */;
11065 * if we aren't registered or it's a res holder type
11066 * reservation and this isn't the res holder then set a
11069 if (ctl_get_prkey(lun, residx) == 0
11070 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
11071 ctl_set_reservation_conflict(ctsio);
11078 if ((lun->flags & CTL_LUN_OFFLINE)
11079 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) {
11080 ctl_set_lun_not_ready(ctsio);
11086 * If the LUN is stopped, see if this particular command is allowed
11087 * for a stopped lun. Otherwise, reject it with 0x04,0x02.
11089 if ((lun->flags & CTL_LUN_STOPPED)
11090 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
11091 /* "Logical unit not ready, initializing cmd. required" */
11092 ctl_set_lun_stopped(ctsio);
11097 if ((lun->flags & CTL_LUN_INOPERABLE)
11098 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
11099 /* "Medium format corrupted" */
11100 ctl_set_medium_format_corrupted(ctsio);
11111 ctl_failover_io(union ctl_io *io, int have_lock)
11113 ctl_set_busy(&io->scsiio);
11120 struct ctl_lun *lun;
11121 struct ctl_softc *ctl_softc;
11122 union ctl_io *next_io, *pending_io;
11127 ctl_softc = control_softc;
11129 mtx_lock(&ctl_softc->ctl_lock);
11131 * Remove any cmds from the other SC from the rtr queue. These
11132 * will obviously only be for LUNs for which we're the primary.
11133 * We can't send status or get/send data for these commands.
11134 * Since they haven't been executed yet, we can just remove them.
11135 * We'll either abort them or delete them below, depending on
11136 * which HA mode we're in.
11139 mtx_lock(&ctl_softc->queue_lock);
11140 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue);
11141 io != NULL; io = next_io) {
11142 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
11143 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
11144 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr,
11145 ctl_io_hdr, links);
11147 mtx_unlock(&ctl_softc->queue_lock);
11150 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) {
11151 lun = ctl_softc->ctl_luns[lun_idx];
11156 * Processor LUNs are primary on both sides.
11157 * XXX will this always be true?
11159 if (lun->be_lun->lun_type == T_PROCESSOR)
11162 if ((lun->flags & CTL_LUN_PRIMARY_SC)
11163 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
11164 printf("FAILOVER: primary lun %d\n", lun_idx);
11166 * Remove all commands from the other SC. First from the
11167 * blocked queue then from the ooa queue. Once we have
11168 * removed them. Call ctl_check_blocked to see if there
11169 * is anything that can run.
11171 for (io = (union ctl_io *)TAILQ_FIRST(
11172 &lun->blocked_queue); io != NULL; io = next_io) {
11174 next_io = (union ctl_io *)TAILQ_NEXT(
11175 &io->io_hdr, blocked_links);
11177 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
11178 TAILQ_REMOVE(&lun->blocked_queue,
11179 &io->io_hdr,blocked_links);
11180 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11181 TAILQ_REMOVE(&lun->ooa_queue,
11182 &io->io_hdr, ooa_links);
11188 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
11189 io != NULL; io = next_io) {
11191 next_io = (union ctl_io *)TAILQ_NEXT(
11192 &io->io_hdr, ooa_links);
11194 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
11196 TAILQ_REMOVE(&lun->ooa_queue,
11203 ctl_check_blocked(lun);
11204 } else if ((lun->flags & CTL_LUN_PRIMARY_SC)
11205 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
11207 printf("FAILOVER: primary lun %d\n", lun_idx);
11209 * Abort all commands from the other SC. We can't
11210 * send status back for them now. These should get
11211 * cleaned up when they are completed or come out
11212 * for a datamove operation.
11214 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
11215 io != NULL; io = next_io) {
11216 next_io = (union ctl_io *)TAILQ_NEXT(
11217 &io->io_hdr, ooa_links);
11219 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
11220 io->io_hdr.flags |= CTL_FLAG_ABORT;
11222 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
11223 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
11225 printf("FAILOVER: secondary lun %d\n", lun_idx);
11227 lun->flags |= CTL_LUN_PRIMARY_SC;
11230 * We send all I/O that was sent to this controller
11231 * and redirected to the other side back with
11232 * busy status, and have the initiator retry it.
11233 * Figuring out how much data has been transferred,
11234 * etc. and picking up where we left off would be
11237 * XXX KDM need to remove I/O from the blocked
11240 for (pending_io = (union ctl_io *)TAILQ_FIRST(
11241 &lun->ooa_queue); pending_io != NULL;
11242 pending_io = next_io) {
11244 next_io = (union ctl_io *)TAILQ_NEXT(
11245 &pending_io->io_hdr, ooa_links);
11247 pending_io->io_hdr.flags &=
11248 ~CTL_FLAG_SENT_2OTHER_SC;
11250 if (pending_io->io_hdr.flags &
11251 CTL_FLAG_IO_ACTIVE) {
11252 pending_io->io_hdr.flags |=
11255 ctl_set_busy(&pending_io->scsiio);
11256 ctl_done(pending_io);
11261 * Build Unit Attention
11263 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11264 lun->pending_ua[i] |=
11265 CTL_UA_ASYM_ACC_CHANGE;
11267 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
11268 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
11269 printf("FAILOVER: secondary lun %d\n", lun_idx);
11271 * if the first io on the OOA is not on the RtR queue
11274 lun->flags |= CTL_LUN_PRIMARY_SC;
11276 pending_io = (union ctl_io *)TAILQ_FIRST(
11278 if (pending_io==NULL) {
11279 printf("Nothing on OOA queue\n");
11283 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11284 if ((pending_io->io_hdr.flags &
11285 CTL_FLAG_IS_WAS_ON_RTR) == 0) {
11286 pending_io->io_hdr.flags |=
11287 CTL_FLAG_IS_WAS_ON_RTR;
11288 ctl_enqueue_rtr(pending_io);
11293 printf("Tag 0x%04x is running\n",
11294 pending_io->scsiio.tag_num);
11298 next_io = (union ctl_io *)TAILQ_NEXT(
11299 &pending_io->io_hdr, ooa_links);
11300 for (pending_io=next_io; pending_io != NULL;
11301 pending_io = next_io) {
11302 pending_io->io_hdr.flags &=
11303 ~CTL_FLAG_SENT_2OTHER_SC;
11304 next_io = (union ctl_io *)TAILQ_NEXT(
11305 &pending_io->io_hdr, ooa_links);
11306 if (pending_io->io_hdr.flags &
11307 CTL_FLAG_IS_WAS_ON_RTR) {
11309 printf("Tag 0x%04x is running\n",
11310 pending_io->scsiio.tag_num);
11315 switch (ctl_check_ooa(lun, pending_io,
11316 (union ctl_io *)TAILQ_PREV(
11317 &pending_io->io_hdr, ctl_ooaq,
11320 case CTL_ACTION_BLOCK:
11321 TAILQ_INSERT_TAIL(&lun->blocked_queue,
11322 &pending_io->io_hdr,
11324 pending_io->io_hdr.flags |=
11327 case CTL_ACTION_PASS:
11328 case CTL_ACTION_SKIP:
11329 pending_io->io_hdr.flags |=
11330 CTL_FLAG_IS_WAS_ON_RTR;
11331 ctl_enqueue_rtr(pending_io);
11333 case CTL_ACTION_OVERLAP:
11334 ctl_set_overlapped_cmd(
11335 (struct ctl_scsiio *)pending_io);
11336 ctl_done(pending_io);
11338 case CTL_ACTION_OVERLAP_TAG:
11339 ctl_set_overlapped_tag(
11340 (struct ctl_scsiio *)pending_io,
11341 pending_io->scsiio.tag_num & 0xff);
11342 ctl_done(pending_io);
11344 case CTL_ACTION_ERROR:
11346 ctl_set_internal_failure(
11347 (struct ctl_scsiio *)pending_io,
11350 ctl_done(pending_io);
11356 * Build Unit Attention
11358 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11359 lun->pending_ua[i] |=
11360 CTL_UA_ASYM_ACC_CHANGE;
11363 panic("Unhandled HA mode failover, LUN flags = %#x, "
11364 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode);
11368 mtx_unlock(&ctl_softc->ctl_lock);
11372 ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio)
11374 struct ctl_lun *lun;
11375 const struct ctl_cmd_entry *entry;
11376 uint32_t initidx, targ_lun;
11383 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11384 if ((targ_lun < CTL_MAX_LUNS)
11385 && ((lun = ctl_softc->ctl_luns[targ_lun]) != NULL)) {
11387 * If the LUN is invalid, pretend that it doesn't exist.
11388 * It will go away as soon as all pending I/O has been
11391 mtx_lock(&lun->lun_lock);
11392 if (lun->flags & CTL_LUN_DISABLED) {
11393 mtx_unlock(&lun->lun_lock);
11395 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11396 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11398 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
11399 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
11401 if (lun->be_lun->lun_type == T_PROCESSOR) {
11402 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV;
11406 * Every I/O goes into the OOA queue for a
11407 * particular LUN, and stays there until completion.
11409 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr,
11413 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11414 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11417 /* Get command entry and return error if it is unsuppotyed. */
11418 entry = ctl_validate_command(ctsio);
11419 if (entry == NULL) {
11421 mtx_unlock(&lun->lun_lock);
11425 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11426 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11429 * Check to see whether we can send this command to LUNs that don't
11430 * exist. This should pretty much only be the case for inquiry
11431 * and request sense. Further checks, below, really require having
11432 * a LUN, so we can't really check the command anymore. Just put
11433 * it on the rtr queue.
11436 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) {
11437 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11438 ctl_enqueue_rtr((union ctl_io *)ctsio);
11442 ctl_set_unsupported_lun(ctsio);
11443 ctl_done((union ctl_io *)ctsio);
11444 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11448 * Make sure we support this particular command on this LUN.
11449 * e.g., we don't support writes to the control LUN.
11451 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11452 mtx_unlock(&lun->lun_lock);
11453 ctl_set_invalid_opcode(ctsio);
11454 ctl_done((union ctl_io *)ctsio);
11459 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11463 * If we've got a request sense, it'll clear the contingent
11464 * allegiance condition. Otherwise, if we have a CA condition for
11465 * this initiator, clear it, because it sent down a command other
11466 * than request sense.
11468 if ((ctsio->cdb[0] != REQUEST_SENSE)
11469 && (ctl_is_set(lun->have_ca, initidx)))
11470 ctl_clear_mask(lun->have_ca, initidx);
11474 * If the command has this flag set, it handles its own unit
11475 * attention reporting, we shouldn't do anything. Otherwise we
11476 * check for any pending unit attentions, and send them back to the
11477 * initiator. We only do this when a command initially comes in,
11478 * not when we pull it off the blocked queue.
11480 * According to SAM-3, section 5.3.2, the order that things get
11481 * presented back to the host is basically unit attentions caused
11482 * by some sort of reset event, busy status, reservation conflicts
11483 * or task set full, and finally any other status.
11485 * One issue here is that some of the unit attentions we report
11486 * don't fall into the "reset" category (e.g. "reported luns data
11487 * has changed"). So reporting it here, before the reservation
11488 * check, may be technically wrong. I guess the only thing to do
11489 * would be to check for and report the reset events here, and then
11490 * check for the other unit attention types after we check for a
11491 * reservation conflict.
11493 * XXX KDM need to fix this
11495 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11496 ctl_ua_type ua_type;
11498 if (lun->pending_ua[initidx] != CTL_UA_NONE) {
11499 scsi_sense_data_type sense_format;
11502 sense_format = (lun->flags &
11503 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC :
11506 sense_format = SSD_TYPE_FIXED;
11508 ua_type = ctl_build_ua(&lun->pending_ua[initidx],
11509 &ctsio->sense_data, sense_format);
11510 if (ua_type != CTL_UA_NONE) {
11511 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11512 ctsio->io_hdr.status = CTL_SCSI_ERROR |
11514 ctsio->sense_len = SSD_FULL_SIZE;
11515 mtx_unlock(&lun->lun_lock);
11516 ctl_done((union ctl_io *)ctsio);
11523 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) {
11524 mtx_unlock(&lun->lun_lock);
11525 ctl_done((union ctl_io *)ctsio);
11530 * XXX CHD this is where we want to send IO to other side if
11531 * this LUN is secondary on this SC. We will need to make a copy
11532 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11533 * the copy we send as FROM_OTHER.
11534 * We also need to stuff the address of the original IO so we can
11535 * find it easily. Something similar will need be done on the other
11536 * side so when we are done we can find the copy.
11538 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11539 union ctl_ha_msg msg_info;
11542 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11544 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11545 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11547 printf("1. ctsio %p\n", ctsio);
11549 msg_info.hdr.serializing_sc = NULL;
11550 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11551 msg_info.scsi.tag_num = ctsio->tag_num;
11552 msg_info.scsi.tag_type = ctsio->tag_type;
11553 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11555 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11557 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11558 (void *)&msg_info, sizeof(msg_info), 0)) >
11559 CTL_HA_STATUS_SUCCESS) {
11560 printf("CTL:precheck, ctl_ha_msg_send returned %d\n",
11562 printf("CTL:opcode is %x\n", ctsio->cdb[0]);
11565 printf("CTL:Precheck sent msg, opcode is %x\n",opcode);
11570 * XXX KDM this I/O is off the incoming queue, but hasn't
11571 * been inserted on any other queue. We may need to come
11572 * up with a holding queue while we wait for serialization
11573 * so that we have an idea of what we're waiting for from
11576 mtx_unlock(&lun->lun_lock);
11580 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11581 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11582 ctl_ooaq, ooa_links))) {
11583 case CTL_ACTION_BLOCK:
11584 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11585 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11587 mtx_unlock(&lun->lun_lock);
11589 case CTL_ACTION_PASS:
11590 case CTL_ACTION_SKIP:
11591 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11592 mtx_unlock(&lun->lun_lock);
11593 ctl_enqueue_rtr((union ctl_io *)ctsio);
11595 case CTL_ACTION_OVERLAP:
11596 mtx_unlock(&lun->lun_lock);
11597 ctl_set_overlapped_cmd(ctsio);
11598 ctl_done((union ctl_io *)ctsio);
11600 case CTL_ACTION_OVERLAP_TAG:
11601 mtx_unlock(&lun->lun_lock);
11602 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11603 ctl_done((union ctl_io *)ctsio);
11605 case CTL_ACTION_ERROR:
11607 mtx_unlock(&lun->lun_lock);
11608 ctl_set_internal_failure(ctsio,
11610 /*retry_count*/ 0);
11611 ctl_done((union ctl_io *)ctsio);
11617 const struct ctl_cmd_entry *
11618 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11620 const struct ctl_cmd_entry *entry;
11621 int service_action;
11623 entry = &ctl_cmd_table[ctsio->cdb[0]];
11625 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11626 if (entry->flags & CTL_CMD_FLAG_SA5) {
11627 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11628 entry = &((const struct ctl_cmd_entry *)
11629 entry->execute)[service_action];
11634 const struct ctl_cmd_entry *
11635 ctl_validate_command(struct ctl_scsiio *ctsio)
11637 const struct ctl_cmd_entry *entry;
11641 entry = ctl_get_cmd_entry(ctsio, &sa);
11642 if (entry->execute == NULL) {
11644 ctl_set_invalid_field(ctsio,
11651 ctl_set_invalid_opcode(ctsio);
11652 ctl_done((union ctl_io *)ctsio);
11655 KASSERT(entry->length > 0,
11656 ("Not defined length for command 0x%02x/0x%02x",
11657 ctsio->cdb[0], ctsio->cdb[1]));
11658 for (i = 1; i < entry->length; i++) {
11659 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11662 ctl_set_invalid_field(ctsio,
11667 /*bit*/ fls(diff) - 1);
11668 ctl_done((union ctl_io *)ctsio);
11675 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11678 switch (lun_type) {
11680 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) &&
11681 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11685 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) &&
11686 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11696 ctl_scsiio(struct ctl_scsiio *ctsio)
11699 const struct ctl_cmd_entry *entry;
11701 retval = CTL_RETVAL_COMPLETE;
11703 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11705 entry = ctl_get_cmd_entry(ctsio, NULL);
11708 * If this I/O has been aborted, just send it straight to
11709 * ctl_done() without executing it.
11711 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11712 ctl_done((union ctl_io *)ctsio);
11717 * All the checks should have been handled by ctl_scsiio_precheck().
11718 * We should be clear now to just execute the I/O.
11720 retval = entry->execute(ctsio);
11727 * Since we only implement one target right now, a bus reset simply resets
11728 * our single target.
11731 ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io)
11733 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET));
11737 ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
11738 ctl_ua_type ua_type)
11740 struct ctl_lun *lun;
11743 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11744 union ctl_ha_msg msg_info;
11746 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11747 msg_info.hdr.nexus = io->io_hdr.nexus;
11748 if (ua_type==CTL_UA_TARG_RESET)
11749 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11751 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11752 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11753 msg_info.hdr.original_sc = NULL;
11754 msg_info.hdr.serializing_sc = NULL;
11755 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11756 (void *)&msg_info, sizeof(msg_info), 0)) {
11761 mtx_lock(&ctl_softc->ctl_lock);
11762 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links)
11763 retval += ctl_lun_reset(lun, io, ua_type);
11764 mtx_unlock(&ctl_softc->ctl_lock);
11770 * The LUN should always be set. The I/O is optional, and is used to
11771 * distinguish between I/Os sent by this initiator, and by other
11772 * initiators. We set unit attention for initiators other than this one.
11773 * SAM-3 is vague on this point. It does say that a unit attention should
11774 * be established for other initiators when a LUN is reset (see section
11775 * 5.7.3), but it doesn't specifically say that the unit attention should
11776 * be established for this particular initiator when a LUN is reset. Here
11777 * is the relevant text, from SAM-3 rev 8:
11779 * 5.7.2 When a SCSI initiator port aborts its own tasks
11781 * When a SCSI initiator port causes its own task(s) to be aborted, no
11782 * notification that the task(s) have been aborted shall be returned to
11783 * the SCSI initiator port other than the completion response for the
11784 * command or task management function action that caused the task(s) to
11785 * be aborted and notification(s) associated with related effects of the
11786 * action (e.g., a reset unit attention condition).
11788 * XXX KDM for now, we're setting unit attention for all initiators.
11791 ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11799 mtx_lock(&lun->lun_lock);
11801 * Run through the OOA queue and abort each I/O.
11804 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
11806 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11807 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11808 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11812 * This version sets unit attention for every
11815 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11816 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11819 lun->pending_ua[i] |= ua_type;
11824 * A reset (any kind, really) clears reservations established with
11825 * RESERVE/RELEASE. It does not clear reservations established
11826 * with PERSISTENT RESERVE OUT, but we don't support that at the
11827 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
11828 * reservations made with the RESERVE/RELEASE commands, because
11829 * those commands are obsolete in SPC-3.
11831 lun->flags &= ~CTL_LUN_RESERVED;
11833 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11835 ctl_clear_mask(lun->have_ca, i);
11837 lun->pending_ua[i] |= ua_type;
11839 mtx_unlock(&lun->lun_lock);
11845 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11850 mtx_assert(&lun->lun_lock, MA_OWNED);
11853 * Run through the OOA queue and attempt to find the given I/O.
11854 * The target port, initiator ID, tag type and tag number have to
11855 * match the values that we got from the initiator. If we have an
11856 * untagged command to abort, simply abort the first untagged command
11857 * we come to. We only allow one untagged command at a time of course.
11859 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11860 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11862 if ((targ_port == UINT32_MAX ||
11863 targ_port == xio->io_hdr.nexus.targ_port) &&
11864 (init_id == UINT32_MAX ||
11865 init_id == xio->io_hdr.nexus.initid.id)) {
11866 if (targ_port != xio->io_hdr.nexus.targ_port ||
11867 init_id != xio->io_hdr.nexus.initid.id)
11868 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
11869 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11870 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11871 union ctl_ha_msg msg_info;
11873 msg_info.hdr.nexus = xio->io_hdr.nexus;
11874 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11875 msg_info.task.tag_num = xio->scsiio.tag_num;
11876 msg_info.task.tag_type = xio->scsiio.tag_type;
11877 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11878 msg_info.hdr.original_sc = NULL;
11879 msg_info.hdr.serializing_sc = NULL;
11880 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11881 (void *)&msg_info, sizeof(msg_info), 0);
11888 ctl_abort_task_set(union ctl_io *io)
11890 struct ctl_softc *softc = control_softc;
11891 struct ctl_lun *lun;
11897 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11898 mtx_lock(&softc->ctl_lock);
11899 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL))
11900 lun = softc->ctl_luns[targ_lun];
11902 mtx_unlock(&softc->ctl_lock);
11906 mtx_lock(&lun->lun_lock);
11907 mtx_unlock(&softc->ctl_lock);
11908 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
11909 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11910 io->io_hdr.nexus.initid.id,
11911 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11912 } else { /* CTL_TASK_CLEAR_TASK_SET */
11913 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
11914 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11916 mtx_unlock(&lun->lun_lock);
11921 ctl_i_t_nexus_reset(union ctl_io *io)
11923 struct ctl_softc *softc = control_softc;
11924 struct ctl_lun *lun;
11925 uint32_t initidx, residx;
11927 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11928 residx = ctl_get_resindex(&io->io_hdr.nexus);
11929 mtx_lock(&softc->ctl_lock);
11930 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11931 mtx_lock(&lun->lun_lock);
11932 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11933 io->io_hdr.nexus.initid.id,
11934 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11936 ctl_clear_mask(lun->have_ca, initidx);
11938 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
11939 lun->flags &= ~CTL_LUN_RESERVED;
11940 lun->pending_ua[initidx] |= CTL_UA_I_T_NEXUS_LOSS;
11941 mtx_unlock(&lun->lun_lock);
11943 mtx_unlock(&softc->ctl_lock);
11948 ctl_abort_task(union ctl_io *io)
11951 struct ctl_lun *lun;
11952 struct ctl_softc *ctl_softc;
11955 char printbuf[128];
11960 ctl_softc = control_softc;
11966 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11967 mtx_lock(&ctl_softc->ctl_lock);
11968 if ((targ_lun < CTL_MAX_LUNS)
11969 && (ctl_softc->ctl_luns[targ_lun] != NULL))
11970 lun = ctl_softc->ctl_luns[targ_lun];
11972 mtx_unlock(&ctl_softc->ctl_lock);
11977 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
11978 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
11981 mtx_lock(&lun->lun_lock);
11982 mtx_unlock(&ctl_softc->ctl_lock);
11984 * Run through the OOA queue and attempt to find the given I/O.
11985 * The target port, initiator ID, tag type and tag number have to
11986 * match the values that we got from the initiator. If we have an
11987 * untagged command to abort, simply abort the first untagged command
11988 * we come to. We only allow one untagged command at a time of course.
11991 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
11993 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11994 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11996 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
11998 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
11999 lun->lun, xio->scsiio.tag_num,
12000 xio->scsiio.tag_type,
12001 (xio->io_hdr.blocked_links.tqe_prev
12002 == NULL) ? "" : " BLOCKED",
12003 (xio->io_hdr.flags &
12004 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
12005 (xio->io_hdr.flags &
12006 CTL_FLAG_ABORT) ? " ABORT" : "",
12007 (xio->io_hdr.flags &
12008 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
12009 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
12011 printf("%s\n", sbuf_data(&sb));
12014 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port)
12015 && (xio->io_hdr.nexus.initid.id ==
12016 io->io_hdr.nexus.initid.id)) {
12018 * If the abort says that the task is untagged, the
12019 * task in the queue must be untagged. Otherwise,
12020 * we just check to see whether the tag numbers
12021 * match. This is because the QLogic firmware
12022 * doesn't pass back the tag type in an abort
12026 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12027 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12028 || (xio->scsiio.tag_num == io->taskio.tag_num)) {
12031 * XXX KDM we've got problems with FC, because it
12032 * doesn't send down a tag type with aborts. So we
12033 * can only really go by the tag number...
12034 * This may cause problems with parallel SCSI.
12035 * Need to figure that out!!
12037 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12038 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12040 if ((io->io_hdr.flags &
12041 CTL_FLAG_FROM_OTHER_SC) == 0 &&
12042 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12043 union ctl_ha_msg msg_info;
12045 io->io_hdr.flags |=
12046 CTL_FLAG_SENT_2OTHER_SC;
12047 msg_info.hdr.nexus = io->io_hdr.nexus;
12048 msg_info.task.task_action =
12049 CTL_TASK_ABORT_TASK;
12050 msg_info.task.tag_num =
12051 io->taskio.tag_num;
12052 msg_info.task.tag_type =
12053 io->taskio.tag_type;
12054 msg_info.hdr.msg_type =
12055 CTL_MSG_MANAGE_TASKS;
12056 msg_info.hdr.original_sc = NULL;
12057 msg_info.hdr.serializing_sc = NULL;
12059 printf("Sent Abort to other side\n");
12061 if (CTL_HA_STATUS_SUCCESS !=
12062 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12064 sizeof(msg_info), 0)) {
12068 printf("ctl_abort_task: found I/O to abort\n");
12074 mtx_unlock(&lun->lun_lock);
12078 * This isn't really an error. It's entirely possible for
12079 * the abort and command completion to cross on the wire.
12080 * This is more of an informative/diagnostic error.
12083 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
12084 "%d:%d:%d:%d tag %d type %d\n",
12085 io->io_hdr.nexus.initid.id,
12086 io->io_hdr.nexus.targ_port,
12087 io->io_hdr.nexus.targ_target.id,
12088 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
12089 io->taskio.tag_type);
12096 ctl_run_task(union ctl_io *io)
12098 struct ctl_softc *ctl_softc = control_softc;
12100 const char *task_desc;
12102 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12104 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12105 ("ctl_run_task: Unextected io_type %d\n",
12106 io->io_hdr.io_type));
12108 task_desc = ctl_scsi_task_string(&io->taskio);
12109 if (task_desc != NULL) {
12111 csevent_log(CSC_CTL | CSC_SHELF_SW |
12113 csevent_LogType_Trace,
12114 csevent_Severity_Information,
12115 csevent_AlertLevel_Green,
12116 csevent_FRU_Firmware,
12117 csevent_FRU_Unknown,
12118 "CTL: received task: %s",task_desc);
12122 csevent_log(CSC_CTL | CSC_SHELF_SW |
12124 csevent_LogType_Trace,
12125 csevent_Severity_Information,
12126 csevent_AlertLevel_Green,
12127 csevent_FRU_Firmware,
12128 csevent_FRU_Unknown,
12129 "CTL: received unknown task "
12131 io->taskio.task_action,
12132 io->taskio.task_action);
12135 switch (io->taskio.task_action) {
12136 case CTL_TASK_ABORT_TASK:
12137 retval = ctl_abort_task(io);
12139 case CTL_TASK_ABORT_TASK_SET:
12140 case CTL_TASK_CLEAR_TASK_SET:
12141 retval = ctl_abort_task_set(io);
12143 case CTL_TASK_CLEAR_ACA:
12145 case CTL_TASK_I_T_NEXUS_RESET:
12146 retval = ctl_i_t_nexus_reset(io);
12148 case CTL_TASK_LUN_RESET: {
12149 struct ctl_lun *lun;
12152 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12153 mtx_lock(&ctl_softc->ctl_lock);
12154 if ((targ_lun < CTL_MAX_LUNS)
12155 && (ctl_softc->ctl_luns[targ_lun] != NULL))
12156 lun = ctl_softc->ctl_luns[targ_lun];
12158 mtx_unlock(&ctl_softc->ctl_lock);
12163 if (!(io->io_hdr.flags &
12164 CTL_FLAG_FROM_OTHER_SC)) {
12165 union ctl_ha_msg msg_info;
12167 io->io_hdr.flags |=
12168 CTL_FLAG_SENT_2OTHER_SC;
12169 msg_info.hdr.msg_type =
12170 CTL_MSG_MANAGE_TASKS;
12171 msg_info.hdr.nexus = io->io_hdr.nexus;
12172 msg_info.task.task_action =
12173 CTL_TASK_LUN_RESET;
12174 msg_info.hdr.original_sc = NULL;
12175 msg_info.hdr.serializing_sc = NULL;
12176 if (CTL_HA_STATUS_SUCCESS !=
12177 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12179 sizeof(msg_info), 0)) {
12183 retval = ctl_lun_reset(lun, io,
12185 mtx_unlock(&ctl_softc->ctl_lock);
12188 case CTL_TASK_TARGET_RESET:
12189 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET);
12191 case CTL_TASK_BUS_RESET:
12192 retval = ctl_bus_reset(ctl_softc, io);
12194 case CTL_TASK_PORT_LOGIN:
12196 case CTL_TASK_PORT_LOGOUT:
12199 printf("ctl_run_task: got unknown task management event %d\n",
12200 io->taskio.task_action);
12204 io->io_hdr.status = CTL_SUCCESS;
12206 io->io_hdr.status = CTL_ERROR;
12211 * For HA operation. Handle commands that come in from the other
12215 ctl_handle_isc(union ctl_io *io)
12218 struct ctl_lun *lun;
12219 struct ctl_softc *ctl_softc;
12222 ctl_softc = control_softc;
12224 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12225 lun = ctl_softc->ctl_luns[targ_lun];
12227 switch (io->io_hdr.msg_type) {
12228 case CTL_MSG_SERIALIZE:
12229 free_io = ctl_serialize_other_sc_cmd(&io->scsiio);
12231 case CTL_MSG_R2R: {
12232 const struct ctl_cmd_entry *entry;
12235 * This is only used in SER_ONLY mode.
12238 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12239 mtx_lock(&lun->lun_lock);
12240 if (ctl_scsiio_lun_check(ctl_softc, lun,
12241 entry, (struct ctl_scsiio *)io) != 0) {
12242 mtx_unlock(&lun->lun_lock);
12246 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12247 mtx_unlock(&lun->lun_lock);
12248 ctl_enqueue_rtr(io);
12251 case CTL_MSG_FINISH_IO:
12252 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
12257 mtx_lock(&lun->lun_lock);
12258 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
12260 ctl_check_blocked(lun);
12261 mtx_unlock(&lun->lun_lock);
12264 case CTL_MSG_PERS_ACTION:
12265 ctl_hndl_per_res_out_on_other_sc(
12266 (union ctl_ha_msg *)&io->presio.pr_msg);
12269 case CTL_MSG_BAD_JUJU:
12273 case CTL_MSG_DATAMOVE:
12274 /* Only used in XFER mode */
12276 ctl_datamove_remote(io);
12278 case CTL_MSG_DATAMOVE_DONE:
12279 /* Only used in XFER mode */
12281 io->scsiio.be_move_done(io);
12285 printf("%s: Invalid message type %d\n",
12286 __func__, io->io_hdr.msg_type);
12296 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12297 * there is no match.
12299 static ctl_lun_error_pattern
12300 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12302 const struct ctl_cmd_entry *entry;
12303 ctl_lun_error_pattern filtered_pattern, pattern;
12305 pattern = desc->error_pattern;
12308 * XXX KDM we need more data passed into this function to match a
12309 * custom pattern, and we actually need to implement custom pattern
12312 if (pattern & CTL_LUN_PAT_CMD)
12313 return (CTL_LUN_PAT_CMD);
12315 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12316 return (CTL_LUN_PAT_ANY);
12318 entry = ctl_get_cmd_entry(ctsio, NULL);
12320 filtered_pattern = entry->pattern & pattern;
12323 * If the user requested specific flags in the pattern (e.g.
12324 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12327 * If the user did not specify any flags, it doesn't matter whether
12328 * or not the command supports the flags.
12330 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12331 (pattern & ~CTL_LUN_PAT_MASK))
12332 return (CTL_LUN_PAT_NONE);
12335 * If the user asked for a range check, see if the requested LBA
12336 * range overlaps with this command's LBA range.
12338 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12344 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12346 return (CTL_LUN_PAT_NONE);
12348 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12349 desc->lba_range.len);
12351 * A "pass" means that the LBA ranges don't overlap, so
12352 * this doesn't match the user's range criteria.
12354 if (action == CTL_ACTION_PASS)
12355 return (CTL_LUN_PAT_NONE);
12358 return (filtered_pattern);
12362 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12364 struct ctl_error_desc *desc, *desc2;
12366 mtx_assert(&lun->lun_lock, MA_OWNED);
12368 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12369 ctl_lun_error_pattern pattern;
12371 * Check to see whether this particular command matches
12372 * the pattern in the descriptor.
12374 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12375 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12378 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12379 case CTL_LUN_INJ_ABORTED:
12380 ctl_set_aborted(&io->scsiio);
12382 case CTL_LUN_INJ_MEDIUM_ERR:
12383 ctl_set_medium_error(&io->scsiio);
12385 case CTL_LUN_INJ_UA:
12386 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12388 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12390 case CTL_LUN_INJ_CUSTOM:
12392 * We're assuming the user knows what he is doing.
12393 * Just copy the sense information without doing
12396 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12397 ctl_min(sizeof(desc->custom_sense),
12398 sizeof(io->scsiio.sense_data)));
12399 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12400 io->scsiio.sense_len = SSD_FULL_SIZE;
12401 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12403 case CTL_LUN_INJ_NONE:
12406 * If this is an error injection type we don't know
12407 * about, clear the continuous flag (if it is set)
12408 * so it will get deleted below.
12410 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12414 * By default, each error injection action is a one-shot
12416 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12419 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12425 #ifdef CTL_IO_DELAY
12427 ctl_datamove_timer_wakeup(void *arg)
12431 io = (union ctl_io *)arg;
12435 #endif /* CTL_IO_DELAY */
12438 ctl_datamove(union ctl_io *io)
12440 void (*fe_datamove)(union ctl_io *io);
12442 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12444 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12447 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12452 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12453 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12455 sbuf_cat(&sb, path_str);
12456 switch (io->io_hdr.io_type) {
12458 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12459 sbuf_printf(&sb, "\n");
12460 sbuf_cat(&sb, path_str);
12461 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12462 io->scsiio.tag_num, io->scsiio.tag_type);
12465 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12466 "Tag Type: %d\n", io->taskio.task_action,
12467 io->taskio.tag_num, io->taskio.tag_type);
12470 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12471 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12474 sbuf_cat(&sb, path_str);
12475 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12476 (intmax_t)time_uptime - io->io_hdr.start_time);
12478 printf("%s", sbuf_data(&sb));
12480 #endif /* CTL_TIME_IO */
12482 #ifdef CTL_IO_DELAY
12483 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12484 struct ctl_lun *lun;
12486 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12488 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12490 struct ctl_lun *lun;
12492 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12494 && (lun->delay_info.datamove_delay > 0)) {
12495 struct callout *callout;
12497 callout = (struct callout *)&io->io_hdr.timer_bytes;
12498 callout_init(callout, /*mpsafe*/ 1);
12499 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12500 callout_reset(callout,
12501 lun->delay_info.datamove_delay * hz,
12502 ctl_datamove_timer_wakeup, io);
12503 if (lun->delay_info.datamove_type ==
12504 CTL_DELAY_TYPE_ONESHOT)
12505 lun->delay_info.datamove_delay = 0;
12512 * This command has been aborted. Set the port status, so we fail
12515 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12516 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n",
12517 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id,
12518 io->io_hdr.nexus.targ_port,
12519 (uintmax_t)io->io_hdr.nexus.targ_target.id,
12520 io->io_hdr.nexus.targ_lun);
12521 io->io_hdr.port_status = 31337;
12523 * Note that the backend, in this case, will get the
12524 * callback in its context. In other cases it may get
12525 * called in the frontend's interrupt thread context.
12527 io->scsiio.be_move_done(io);
12531 /* Don't confuse frontend with zero length data move. */
12532 if (io->scsiio.kern_data_len == 0) {
12533 io->scsiio.be_move_done(io);
12538 * If we're in XFER mode and this I/O is from the other shelf
12539 * controller, we need to send the DMA to the other side to
12540 * actually transfer the data to/from the host. In serialize only
12541 * mode the transfer happens below CTL and ctl_datamove() is only
12542 * called on the machine that originally received the I/O.
12544 if ((control_softc->ha_mode == CTL_HA_MODE_XFER)
12545 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12546 union ctl_ha_msg msg;
12547 uint32_t sg_entries_sent;
12551 memset(&msg, 0, sizeof(msg));
12552 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
12553 msg.hdr.original_sc = io->io_hdr.original_sc;
12554 msg.hdr.serializing_sc = io;
12555 msg.hdr.nexus = io->io_hdr.nexus;
12556 msg.dt.flags = io->io_hdr.flags;
12558 * We convert everything into a S/G list here. We can't
12559 * pass by reference, only by value between controllers.
12560 * So we can't pass a pointer to the S/G list, only as many
12561 * S/G entries as we can fit in here. If it's possible for
12562 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
12563 * then we need to break this up into multiple transfers.
12565 if (io->scsiio.kern_sg_entries == 0) {
12566 msg.dt.kern_sg_entries = 1;
12568 * If this is in cached memory, flush the cache
12569 * before we send the DMA request to the other
12570 * controller. We want to do this in either the
12571 * read or the write case. The read case is
12572 * straightforward. In the write case, we want to
12573 * make sure nothing is in the local cache that
12574 * could overwrite the DMAed data.
12576 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12578 * XXX KDM use bus_dmamap_sync() here.
12583 * Convert to a physical address if this is a
12586 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
12587 msg.dt.sg_list[0].addr =
12588 io->scsiio.kern_data_ptr;
12591 * XXX KDM use busdma here!
12594 msg.dt.sg_list[0].addr = (void *)
12595 vtophys(io->scsiio.kern_data_ptr);
12599 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
12602 struct ctl_sg_entry *sgl;
12605 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
12606 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
12607 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12609 * XXX KDM use bus_dmamap_sync() here.
12614 msg.dt.kern_data_len = io->scsiio.kern_data_len;
12615 msg.dt.kern_total_len = io->scsiio.kern_total_len;
12616 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
12617 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
12618 msg.dt.sg_sequence = 0;
12621 * Loop until we've sent all of the S/G entries. On the
12622 * other end, we'll recompose these S/G entries into one
12623 * contiguous list before passing it to the
12625 for (sg_entries_sent = 0; sg_entries_sent <
12626 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) {
12627 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/
12628 sizeof(msg.dt.sg_list[0])),
12629 msg.dt.kern_sg_entries - sg_entries_sent);
12631 if (do_sg_copy != 0) {
12632 struct ctl_sg_entry *sgl;
12635 sgl = (struct ctl_sg_entry *)
12636 io->scsiio.kern_data_ptr;
12638 * If this is in cached memory, flush the cache
12639 * before we send the DMA request to the other
12640 * controller. We want to do this in either
12641 * the * read or the write case. The read
12642 * case is straightforward. In the write
12643 * case, we want to make sure nothing is
12644 * in the local cache that could overwrite
12648 for (i = sg_entries_sent, j = 0;
12649 i < msg.dt.cur_sg_entries; i++, j++) {
12650 if ((io->io_hdr.flags &
12651 CTL_FLAG_NO_DATASYNC) == 0) {
12653 * XXX KDM use bus_dmamap_sync()
12656 if ((io->io_hdr.flags &
12657 CTL_FLAG_BUS_ADDR) == 0) {
12659 * XXX KDM use busdma.
12662 msg.dt.sg_list[j].addr =(void *)
12663 vtophys(sgl[i].addr);
12666 msg.dt.sg_list[j].addr =
12669 msg.dt.sg_list[j].len = sgl[i].len;
12673 sg_entries_sent += msg.dt.cur_sg_entries;
12674 if (sg_entries_sent >= msg.dt.kern_sg_entries)
12675 msg.dt.sg_last = 1;
12677 msg.dt.sg_last = 0;
12680 * XXX KDM drop and reacquire the lock here?
12682 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12683 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
12685 * XXX do something here.
12689 msg.dt.sent_sg_entries = sg_entries_sent;
12691 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12692 if (io->io_hdr.flags & CTL_FLAG_FAILOVER)
12693 ctl_failover_io(io, /*have_lock*/ 0);
12698 * Lookup the fe_datamove() function for this particular
12702 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12709 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12711 union ctl_ha_msg msg;
12714 memset(&msg, 0, sizeof(msg));
12716 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12717 msg.hdr.original_sc = io;
12718 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12719 msg.hdr.nexus = io->io_hdr.nexus;
12720 msg.hdr.status = io->io_hdr.status;
12721 msg.scsi.tag_num = io->scsiio.tag_num;
12722 msg.scsi.tag_type = io->scsiio.tag_type;
12723 msg.scsi.scsi_status = io->scsiio.scsi_status;
12724 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12725 sizeof(io->scsiio.sense_data));
12726 msg.scsi.sense_len = io->scsiio.sense_len;
12727 msg.scsi.sense_residual = io->scsiio.sense_residual;
12728 msg.scsi.fetd_status = io->io_hdr.port_status;
12729 msg.scsi.residual = io->scsiio.residual;
12730 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12732 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12733 ctl_failover_io(io, /*have_lock*/ have_lock);
12737 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0);
12738 if (isc_status > CTL_HA_STATUS_SUCCESS) {
12739 /* XXX do something if this fails */
12745 * The DMA to the remote side is done, now we need to tell the other side
12746 * we're done so it can continue with its data movement.
12749 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12755 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12756 printf("%s: ISC DMA write failed with error %d", __func__,
12758 ctl_set_internal_failure(&io->scsiio,
12760 /*retry_count*/ rq->ret);
12763 ctl_dt_req_free(rq);
12766 * In this case, we had to malloc the memory locally. Free it.
12768 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12770 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12771 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12774 * The data is in local and remote memory, so now we need to send
12775 * status (good or back) back to the other side.
12777 ctl_send_datamove_done(io, /*have_lock*/ 0);
12781 * We've moved the data from the host/controller into local memory. Now we
12782 * need to push it over to the remote controller's memory.
12785 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12791 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12792 ctl_datamove_remote_write_cb);
12798 ctl_datamove_remote_write(union ctl_io *io)
12801 void (*fe_datamove)(union ctl_io *io);
12804 * - Get the data from the host/HBA into local memory.
12805 * - DMA memory from the local controller to the remote controller.
12806 * - Send status back to the remote controller.
12809 retval = ctl_datamove_remote_sgl_setup(io);
12813 /* Switch the pointer over so the FETD knows what to do */
12814 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12817 * Use a custom move done callback, since we need to send completion
12818 * back to the other controller, not to the backend on this side.
12820 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12822 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12831 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12840 * In this case, we had to malloc the memory locally. Free it.
12842 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12844 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12845 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12849 scsi_path_string(io, path_str, sizeof(path_str));
12850 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12851 sbuf_cat(&sb, path_str);
12852 scsi_command_string(&io->scsiio, NULL, &sb);
12853 sbuf_printf(&sb, "\n");
12854 sbuf_cat(&sb, path_str);
12855 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12856 io->scsiio.tag_num, io->scsiio.tag_type);
12857 sbuf_cat(&sb, path_str);
12858 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12859 io->io_hdr.flags, io->io_hdr.status);
12861 printk("%s", sbuf_data(&sb));
12866 * The read is done, now we need to send status (good or bad) back
12867 * to the other side.
12869 ctl_send_datamove_done(io, /*have_lock*/ 0);
12875 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12878 void (*fe_datamove)(union ctl_io *io);
12882 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12883 printf("%s: ISC DMA read failed with error %d", __func__,
12885 ctl_set_internal_failure(&io->scsiio,
12887 /*retry_count*/ rq->ret);
12890 ctl_dt_req_free(rq);
12892 /* Switch the pointer over so the FETD knows what to do */
12893 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12896 * Use a custom move done callback, since we need to send completion
12897 * back to the other controller, not to the backend on this side.
12899 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12901 /* XXX KDM add checks like the ones in ctl_datamove? */
12903 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12909 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12911 struct ctl_sg_entry *local_sglist, *remote_sglist;
12912 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist;
12913 struct ctl_softc *softc;
12918 softc = control_softc;
12920 local_sglist = io->io_hdr.local_sglist;
12921 local_dma_sglist = io->io_hdr.local_dma_sglist;
12922 remote_sglist = io->io_hdr.remote_sglist;
12923 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
12925 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) {
12926 for (i = 0; i < io->scsiio.kern_sg_entries; i++) {
12927 local_sglist[i].len = remote_sglist[i].len;
12930 * XXX Detect the situation where the RS-level I/O
12931 * redirector on the other side has already read the
12932 * data off of the AOR RS on this side, and
12933 * transferred it to remote (mirror) memory on the
12934 * other side. Since we already have the data in
12935 * memory here, we just need to use it.
12937 * XXX KDM this can probably be removed once we
12938 * get the cache device code in and take the
12939 * current AOR implementation out.
12942 if ((remote_sglist[i].addr >=
12943 (void *)vtophys(softc->mirr->addr))
12944 && (remote_sglist[i].addr <
12945 ((void *)vtophys(softc->mirr->addr) +
12946 CacheMirrorOffset))) {
12947 local_sglist[i].addr = remote_sglist[i].addr -
12949 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12951 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE;
12953 local_sglist[i].addr = remote_sglist[i].addr +
12958 printf("%s: local %p, remote %p, len %d\n",
12959 __func__, local_sglist[i].addr,
12960 remote_sglist[i].addr, local_sglist[i].len);
12964 uint32_t len_to_go;
12967 * In this case, we don't have automatically allocated
12968 * memory for this I/O on this controller. This typically
12969 * happens with internal CTL I/O -- e.g. inquiry, mode
12970 * sense, etc. Anything coming from RAIDCore will have
12971 * a mirror area available.
12973 len_to_go = io->scsiio.kern_data_len;
12976 * Clear the no datasync flag, we have to use malloced
12979 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC;
12982 * The difficult thing here is that the size of the various
12983 * S/G segments may be different than the size from the
12984 * remote controller. That'll make it harder when DMAing
12985 * the data back to the other side.
12987 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) /
12988 sizeof(io->io_hdr.remote_sglist[0])) &&
12989 (len_to_go > 0); i++) {
12990 local_sglist[i].len = ctl_min(len_to_go, 131072);
12991 CTL_SIZE_8B(local_dma_sglist[i].len,
12992 local_sglist[i].len);
12993 local_sglist[i].addr =
12994 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK);
12996 local_dma_sglist[i].addr = local_sglist[i].addr;
12998 if (local_sglist[i].addr == NULL) {
13001 printf("malloc failed for %zd bytes!",
13002 local_dma_sglist[i].len);
13003 for (j = 0; j < i; j++) {
13004 free(local_sglist[j].addr, M_CTL);
13006 ctl_set_internal_failure(&io->scsiio,
13008 /*retry_count*/ 4857);
13010 goto bailout_error;
13013 /* XXX KDM do we need a sync here? */
13015 len_to_go -= local_sglist[i].len;
13018 * Reset the number of S/G entries accordingly. The
13019 * original number of S/G entries is available in
13022 io->scsiio.kern_sg_entries = i;
13025 printf("%s: kern_sg_entries = %d\n", __func__,
13026 io->scsiio.kern_sg_entries);
13027 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13028 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i,
13029 local_sglist[i].addr, local_sglist[i].len,
13030 local_dma_sglist[i].len);
13039 ctl_send_datamove_done(io, /*have_lock*/ 0);
13045 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
13046 ctl_ha_dt_cb callback)
13048 struct ctl_ha_dt_req *rq;
13049 struct ctl_sg_entry *remote_sglist, *local_sglist;
13050 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist;
13051 uint32_t local_used, remote_used, total_used;
13057 rq = ctl_dt_req_alloc();
13060 * If we failed to allocate the request, and if the DMA didn't fail
13061 * anyway, set busy status. This is just a resource allocation
13065 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE))
13066 ctl_set_busy(&io->scsiio);
13068 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
13071 ctl_dt_req_free(rq);
13074 * The data move failed. We need to return status back
13075 * to the other controller. No point in trying to DMA
13076 * data to the remote controller.
13079 ctl_send_datamove_done(io, /*have_lock*/ 0);
13086 local_sglist = io->io_hdr.local_sglist;
13087 local_dma_sglist = io->io_hdr.local_dma_sglist;
13088 remote_sglist = io->io_hdr.remote_sglist;
13089 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
13094 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) {
13095 rq->ret = CTL_HA_STATUS_SUCCESS;
13102 * Pull/push the data over the wire from/to the other controller.
13103 * This takes into account the possibility that the local and
13104 * remote sglists may not be identical in terms of the size of
13105 * the elements and the number of elements.
13107 * One fundamental assumption here is that the length allocated for
13108 * both the local and remote sglists is identical. Otherwise, we've
13109 * essentially got a coding error of some sort.
13111 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
13113 uint32_t cur_len, dma_length;
13116 rq->id = CTL_HA_DATA_CTL;
13117 rq->command = command;
13121 * Both pointers should be aligned. But it is possible
13122 * that the allocation length is not. They should both
13123 * also have enough slack left over at the end, though,
13124 * to round up to the next 8 byte boundary.
13126 cur_len = ctl_min(local_sglist[i].len - local_used,
13127 remote_sglist[j].len - remote_used);
13130 * In this case, we have a size issue and need to decrease
13131 * the size, except in the case where we actually have less
13132 * than 8 bytes left. In that case, we need to increase
13133 * the DMA length to get the last bit.
13135 if ((cur_len & 0x7) != 0) {
13136 if (cur_len > 0x7) {
13137 cur_len = cur_len - (cur_len & 0x7);
13138 dma_length = cur_len;
13140 CTL_SIZE_8B(dma_length, cur_len);
13144 dma_length = cur_len;
13147 * If we had to allocate memory for this I/O, instead of using
13148 * the non-cached mirror memory, we'll need to flush the cache
13149 * before trying to DMA to the other controller.
13151 * We could end up doing this multiple times for the same
13152 * segment if we have a larger local segment than remote
13153 * segment. That shouldn't be an issue.
13155 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
13157 * XXX KDM use bus_dmamap_sync() here.
13161 rq->size = dma_length;
13163 tmp_ptr = (uint8_t *)local_sglist[i].addr;
13164 tmp_ptr += local_used;
13166 /* Use physical addresses when talking to ISC hardware */
13167 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
13168 /* XXX KDM use busdma */
13170 rq->local = vtophys(tmp_ptr);
13173 rq->local = tmp_ptr;
13175 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
13176 tmp_ptr += remote_used;
13177 rq->remote = tmp_ptr;
13179 rq->callback = NULL;
13181 local_used += cur_len;
13182 if (local_used >= local_sglist[i].len) {
13187 remote_used += cur_len;
13188 if (remote_used >= remote_sglist[j].len) {
13192 total_used += cur_len;
13194 if (total_used >= io->scsiio.kern_data_len)
13195 rq->callback = callback;
13197 if ((rq->size & 0x7) != 0) {
13198 printf("%s: warning: size %d is not on 8b boundary\n",
13199 __func__, rq->size);
13201 if (((uintptr_t)rq->local & 0x7) != 0) {
13202 printf("%s: warning: local %p not on 8b boundary\n",
13203 __func__, rq->local);
13205 if (((uintptr_t)rq->remote & 0x7) != 0) {
13206 printf("%s: warning: remote %p not on 8b boundary\n",
13207 __func__, rq->local);
13210 printf("%s: %s: local %#x remote %#x size %d\n", __func__,
13211 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
13212 rq->local, rq->remote, rq->size);
13215 isc_ret = ctl_dt_single(rq);
13216 if (isc_ret == CTL_HA_STATUS_WAIT)
13219 if (isc_ret == CTL_HA_STATUS_DISCONNECT) {
13220 rq->ret = CTL_HA_STATUS_SUCCESS;
13234 ctl_datamove_remote_read(union ctl_io *io)
13240 * This will send an error to the other controller in the case of a
13243 retval = ctl_datamove_remote_sgl_setup(io);
13247 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
13248 ctl_datamove_remote_read_cb);
13250 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) {
13252 * Make sure we free memory if there was an error.. The
13253 * ctl_datamove_remote_xfer() function will send the
13254 * datamove done message, or call the callback with an
13255 * error if there is a problem.
13257 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13258 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13265 * Process a datamove request from the other controller. This is used for
13266 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
13267 * first. Once that is complete, the data gets DMAed into the remote
13268 * controller's memory. For reads, we DMA from the remote controller's
13269 * memory into our memory first, and then move it out to the FETD.
13272 ctl_datamove_remote(union ctl_io *io)
13274 struct ctl_softc *softc;
13276 softc = control_softc;
13278 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
13281 * Note that we look for an aborted I/O here, but don't do some of
13282 * the other checks that ctl_datamove() normally does.
13283 * We don't need to run the datamove delay code, since that should
13284 * have been done if need be on the other controller.
13286 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
13287 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__,
13288 io->scsiio.tag_num, io->io_hdr.nexus.initid.id,
13289 io->io_hdr.nexus.targ_port,
13290 io->io_hdr.nexus.targ_target.id,
13291 io->io_hdr.nexus.targ_lun);
13292 io->io_hdr.port_status = 31338;
13293 ctl_send_datamove_done(io, /*have_lock*/ 0);
13297 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) {
13298 ctl_datamove_remote_write(io);
13299 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){
13300 ctl_datamove_remote_read(io);
13302 union ctl_ha_msg msg;
13303 struct scsi_sense_data *sense;
13307 memset(&msg, 0, sizeof(msg));
13309 msg.hdr.msg_type = CTL_MSG_BAD_JUJU;
13310 msg.hdr.status = CTL_SCSI_ERROR;
13311 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
13313 retry_count = 4243;
13315 sense = &msg.scsi.sense_data;
13316 sks[0] = SSD_SCS_VALID;
13317 sks[1] = (retry_count >> 8) & 0xff;
13318 sks[2] = retry_count & 0xff;
13320 /* "Internal target failure" */
13321 scsi_set_sense_data(sense,
13322 /*sense_format*/ SSD_TYPE_NONE,
13323 /*current_error*/ 1,
13324 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
13327 /*type*/ SSD_ELEM_SKS,
13328 /*size*/ sizeof(sks),
13332 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
13333 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
13334 ctl_failover_io(io, /*have_lock*/ 1);
13338 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) >
13339 CTL_HA_STATUS_SUCCESS) {
13340 /* XXX KDM what to do if this fails? */
13348 ctl_process_done(union ctl_io *io)
13350 struct ctl_lun *lun;
13351 struct ctl_softc *ctl_softc = control_softc;
13352 void (*fe_done)(union ctl_io *io);
13353 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port);
13355 CTL_DEBUG_PRINT(("ctl_process_done\n"));
13358 control_softc->ctl_ports[targ_port]->fe_done;
13361 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
13366 ctl_scsi_path_string(io, path_str, sizeof(path_str));
13367 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13369 sbuf_cat(&sb, path_str);
13370 switch (io->io_hdr.io_type) {
13372 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
13373 sbuf_printf(&sb, "\n");
13374 sbuf_cat(&sb, path_str);
13375 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13376 io->scsiio.tag_num, io->scsiio.tag_type);
13379 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
13380 "Tag Type: %d\n", io->taskio.task_action,
13381 io->taskio.tag_num, io->taskio.tag_type);
13384 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13385 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13388 sbuf_cat(&sb, path_str);
13389 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
13390 (intmax_t)time_uptime - io->io_hdr.start_time);
13392 printf("%s", sbuf_data(&sb));
13394 #endif /* CTL_TIME_IO */
13396 switch (io->io_hdr.io_type) {
13400 if (bootverbose || (ctl_debug & CTL_DEBUG_INFO))
13401 ctl_io_error_print(io, NULL);
13402 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
13406 return (CTL_RETVAL_COMPLETE);
13408 panic("ctl_process_done: invalid io type %d\n",
13409 io->io_hdr.io_type);
13410 break; /* NOTREACHED */
13413 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13415 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13416 io->io_hdr.nexus.targ_mapped_lun));
13420 mtx_lock(&lun->lun_lock);
13423 * Check to see if we have any errors to inject here. We only
13424 * inject errors for commands that don't already have errors set.
13426 if ((STAILQ_FIRST(&lun->error_list) != NULL) &&
13427 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
13428 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
13429 ctl_inject_error(lun, io);
13432 * XXX KDM how do we treat commands that aren't completed
13435 * XXX KDM should we also track I/O latency?
13437 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13438 io->io_hdr.io_type == CTL_IO_SCSI) {
13440 struct bintime cur_bt;
13444 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13446 type = CTL_STATS_READ;
13447 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13449 type = CTL_STATS_WRITE;
13451 type = CTL_STATS_NO_IO;
13453 lun->stats.ports[targ_port].bytes[type] +=
13454 io->scsiio.kern_total_len;
13455 lun->stats.ports[targ_port].operations[type]++;
13457 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
13458 &io->io_hdr.dma_bt);
13459 lun->stats.ports[targ_port].num_dmas[type] +=
13460 io->io_hdr.num_dmas;
13461 getbintime(&cur_bt);
13462 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
13463 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
13468 * Remove this from the OOA queue.
13470 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13473 * Run through the blocked queue on this LUN and see if anything
13474 * has become unblocked, now that this transaction is done.
13476 ctl_check_blocked(lun);
13479 * If the LUN has been invalidated, free it if there is nothing
13480 * left on its OOA queue.
13482 if ((lun->flags & CTL_LUN_INVALID)
13483 && TAILQ_EMPTY(&lun->ooa_queue)) {
13484 mtx_unlock(&lun->lun_lock);
13485 mtx_lock(&ctl_softc->ctl_lock);
13487 mtx_unlock(&ctl_softc->ctl_lock);
13489 mtx_unlock(&lun->lun_lock);
13494 * If this command has been aborted, make sure we set the status
13495 * properly. The FETD is responsible for freeing the I/O and doing
13496 * whatever it needs to do to clean up its state.
13498 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13499 ctl_set_task_aborted(&io->scsiio);
13502 * If enabled, print command error status.
13503 * We don't print UAs unless debugging was enabled explicitly.
13506 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
13508 if (!bootverbose && (ctl_debug & CTL_DEBUG_INFO) == 0)
13510 if ((ctl_debug & CTL_DEBUG_INFO) == 0 &&
13511 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) &&
13512 (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) {
13513 int error_code, sense_key, asc, ascq;
13515 scsi_extract_sense_len(&io->scsiio.sense_data,
13516 io->scsiio.sense_len, &error_code, &sense_key,
13517 &asc, &ascq, /*show_errors*/ 0);
13518 if (sense_key == SSD_KEY_UNIT_ATTENTION)
13522 ctl_io_error_print(io, NULL);
13526 * Tell the FETD or the other shelf controller we're done with this
13527 * command. Note that only SCSI commands get to this point. Task
13528 * management commands are completed above.
13530 * We only send status to the other controller if we're in XFER
13531 * mode. In SER_ONLY mode, the I/O is done on the controller that
13532 * received the I/O (from CTL's perspective), and so the status is
13535 * XXX KDM if we hold the lock here, we could cause a deadlock
13536 * if the frontend comes back in in this context to queue
13539 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER)
13540 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
13541 union ctl_ha_msg msg;
13543 memset(&msg, 0, sizeof(msg));
13544 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13545 msg.hdr.original_sc = io->io_hdr.original_sc;
13546 msg.hdr.nexus = io->io_hdr.nexus;
13547 msg.hdr.status = io->io_hdr.status;
13548 msg.scsi.scsi_status = io->scsiio.scsi_status;
13549 msg.scsi.tag_num = io->scsiio.tag_num;
13550 msg.scsi.tag_type = io->scsiio.tag_type;
13551 msg.scsi.sense_len = io->scsiio.sense_len;
13552 msg.scsi.sense_residual = io->scsiio.sense_residual;
13553 msg.scsi.residual = io->scsiio.residual;
13554 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
13555 sizeof(io->scsiio.sense_data));
13557 * We copy this whether or not this is an I/O-related
13558 * command. Otherwise, we'd have to go and check to see
13559 * whether it's a read/write command, and it really isn't
13562 memcpy(&msg.scsi.lbalen,
13563 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
13564 sizeof(msg.scsi.lbalen));
13566 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13567 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
13568 /* XXX do something here */
13575 return (CTL_RETVAL_COMPLETE);
13580 * Front end should call this if it doesn't do autosense. When the request
13581 * sense comes back in from the initiator, we'll dequeue this and send it.
13584 ctl_queue_sense(union ctl_io *io)
13586 struct ctl_lun *lun;
13587 struct ctl_softc *ctl_softc;
13588 uint32_t initidx, targ_lun;
13590 ctl_softc = control_softc;
13592 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13595 * LUN lookup will likely move to the ctl_work_thread() once we
13596 * have our new queueing infrastructure (that doesn't put things on
13597 * a per-LUN queue initially). That is so that we can handle
13598 * things like an INQUIRY to a LUN that we don't have enabled. We
13599 * can't deal with that right now.
13601 mtx_lock(&ctl_softc->ctl_lock);
13604 * If we don't have a LUN for this, just toss the sense
13607 targ_lun = io->io_hdr.nexus.targ_lun;
13608 targ_lun = ctl_map_lun(io->io_hdr.nexus.targ_port, targ_lun);
13609 if ((targ_lun < CTL_MAX_LUNS)
13610 && (ctl_softc->ctl_luns[targ_lun] != NULL))
13611 lun = ctl_softc->ctl_luns[targ_lun];
13615 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13617 mtx_lock(&lun->lun_lock);
13619 * Already have CA set for this LUN...toss the sense information.
13621 if (ctl_is_set(lun->have_ca, initidx)) {
13622 mtx_unlock(&lun->lun_lock);
13626 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data,
13627 ctl_min(sizeof(lun->pending_sense[initidx]),
13628 sizeof(io->scsiio.sense_data)));
13629 ctl_set_mask(lun->have_ca, initidx);
13630 mtx_unlock(&lun->lun_lock);
13633 mtx_unlock(&ctl_softc->ctl_lock);
13637 return (CTL_RETVAL_COMPLETE);
13642 * Primary command inlet from frontend ports. All SCSI and task I/O
13643 * requests must go through this function.
13646 ctl_queue(union ctl_io *io)
13648 struct ctl_softc *ctl_softc;
13650 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13652 ctl_softc = control_softc;
13655 io->io_hdr.start_time = time_uptime;
13656 getbintime(&io->io_hdr.start_bt);
13657 #endif /* CTL_TIME_IO */
13659 /* Map FE-specific LUN ID into global one. */
13660 io->io_hdr.nexus.targ_mapped_lun =
13661 ctl_map_lun(io->io_hdr.nexus.targ_port, io->io_hdr.nexus.targ_lun);
13663 switch (io->io_hdr.io_type) {
13666 if (ctl_debug & CTL_DEBUG_CDB)
13668 ctl_enqueue_incoming(io);
13671 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13675 return (CTL_RETVAL_COMPLETE);
13678 #ifdef CTL_IO_DELAY
13680 ctl_done_timer_wakeup(void *arg)
13684 io = (union ctl_io *)arg;
13687 #endif /* CTL_IO_DELAY */
13690 ctl_done(union ctl_io *io)
13692 struct ctl_softc *ctl_softc;
13694 ctl_softc = control_softc;
13697 * Enable this to catch duplicate completion issues.
13700 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13701 printf("%s: type %d msg %d cdb %x iptl: "
13702 "%d:%d:%d:%d tag 0x%04x "
13703 "flag %#x status %x\n",
13705 io->io_hdr.io_type,
13706 io->io_hdr.msg_type,
13708 io->io_hdr.nexus.initid.id,
13709 io->io_hdr.nexus.targ_port,
13710 io->io_hdr.nexus.targ_target.id,
13711 io->io_hdr.nexus.targ_lun,
13712 (io->io_hdr.io_type ==
13714 io->taskio.tag_num :
13715 io->scsiio.tag_num,
13717 io->io_hdr.status);
13719 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13723 * This is an internal copy of an I/O, and should not go through
13724 * the normal done processing logic.
13726 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13730 * We need to send a msg to the serializing shelf to finish the IO
13731 * as well. We don't send a finish message to the other shelf if
13732 * this is a task management command. Task management commands
13733 * aren't serialized in the OOA queue, but rather just executed on
13734 * both shelf controllers for commands that originated on that
13737 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)
13738 && (io->io_hdr.io_type != CTL_IO_TASK)) {
13739 union ctl_ha_msg msg_io;
13741 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO;
13742 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc;
13743 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io,
13744 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) {
13746 /* continue on to finish IO */
13748 #ifdef CTL_IO_DELAY
13749 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13750 struct ctl_lun *lun;
13752 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13754 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13756 struct ctl_lun *lun;
13758 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13761 && (lun->delay_info.done_delay > 0)) {
13762 struct callout *callout;
13764 callout = (struct callout *)&io->io_hdr.timer_bytes;
13765 callout_init(callout, /*mpsafe*/ 1);
13766 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13767 callout_reset(callout,
13768 lun->delay_info.done_delay * hz,
13769 ctl_done_timer_wakeup, io);
13770 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13771 lun->delay_info.done_delay = 0;
13775 #endif /* CTL_IO_DELAY */
13777 ctl_enqueue_done(io);
13781 ctl_isc(struct ctl_scsiio *ctsio)
13783 struct ctl_lun *lun;
13786 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13788 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0]));
13790 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n"));
13792 retval = lun->backend->data_submit((union ctl_io *)ctsio);
13799 ctl_work_thread(void *arg)
13801 struct ctl_thread *thr = (struct ctl_thread *)arg;
13802 struct ctl_softc *softc = thr->ctl_softc;
13806 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13812 * We handle the queues in this order:
13814 * - done queue (to free up resources, unblock other commands)
13818 * If those queues are empty, we break out of the loop and
13821 mtx_lock(&thr->queue_lock);
13822 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13824 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13825 mtx_unlock(&thr->queue_lock);
13826 ctl_handle_isc(io);
13829 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13831 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13832 /* clear any blocked commands, call fe_done */
13833 mtx_unlock(&thr->queue_lock);
13834 retval = ctl_process_done(io);
13837 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13839 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13840 mtx_unlock(&thr->queue_lock);
13841 if (io->io_hdr.io_type == CTL_IO_TASK)
13844 ctl_scsiio_precheck(softc, &io->scsiio);
13847 if (!ctl_pause_rtr) {
13848 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13850 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13851 mtx_unlock(&thr->queue_lock);
13852 retval = ctl_scsiio(&io->scsiio);
13853 if (retval != CTL_RETVAL_COMPLETE)
13854 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13859 /* Sleep until we have something to do. */
13860 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13865 ctl_lun_thread(void *arg)
13867 struct ctl_softc *softc = (struct ctl_softc *)arg;
13868 struct ctl_be_lun *be_lun;
13871 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13875 mtx_lock(&softc->ctl_lock);
13876 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13877 if (be_lun != NULL) {
13878 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13879 mtx_unlock(&softc->ctl_lock);
13880 ctl_create_lun(be_lun);
13884 /* Sleep until we have something to do. */
13885 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13886 PDROP | PRIBIO, "-", 0);
13891 ctl_thresh_thread(void *arg)
13893 struct ctl_softc *softc = (struct ctl_softc *)arg;
13894 struct ctl_lun *lun;
13895 struct ctl_be_lun *be_lun;
13896 struct scsi_da_rw_recovery_page *rwpage;
13897 struct ctl_logical_block_provisioning_page *page;
13899 uint64_t thres, val;
13902 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13905 mtx_lock(&softc->ctl_lock);
13906 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13907 be_lun = lun->be_lun;
13908 if ((lun->flags & CTL_LUN_DISABLED) ||
13909 (lun->flags & CTL_LUN_OFFLINE) ||
13910 (be_lun->flags & CTL_LUN_FLAG_UNMAP) == 0 ||
13911 lun->backend->lun_attr == NULL)
13913 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT];
13914 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0)
13917 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT];
13918 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13919 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13921 thres = scsi_4btoul(page->descr[i].count);
13922 thres <<= CTL_LBP_EXPONENT;
13923 switch (page->descr[i].resource) {
13925 attr = "blocksavail";
13928 attr = "blocksused";
13931 attr = "poolblocksavail";
13934 attr = "poolblocksused";
13939 mtx_unlock(&softc->ctl_lock); // XXX
13940 val = lun->backend->lun_attr(
13941 lun->be_lun->be_lun, attr);
13942 mtx_lock(&softc->ctl_lock);
13943 if (val == UINT64_MAX)
13945 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13946 == SLBPPD_ARMING_INC)
13947 e |= (val >= thres);
13949 e |= (val <= thres);
13951 mtx_lock(&lun->lun_lock);
13953 if (lun->lasttpt == 0 ||
13954 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13955 lun->lasttpt = time_uptime;
13956 for (i = 0; i < CTL_MAX_INITIATORS; i++)
13957 lun->pending_ua[i] |=
13958 CTL_UA_THIN_PROV_THRES;
13962 for (i = 0; i < CTL_MAX_INITIATORS; i++)
13963 lun->pending_ua[i] &= ~CTL_UA_THIN_PROV_THRES;
13965 mtx_unlock(&lun->lun_lock);
13967 mtx_unlock(&softc->ctl_lock);
13968 pause("-", CTL_LBP_PERIOD * hz);
13973 ctl_enqueue_incoming(union ctl_io *io)
13975 struct ctl_softc *softc = control_softc;
13976 struct ctl_thread *thr;
13979 idx = (io->io_hdr.nexus.targ_port * 127 +
13980 io->io_hdr.nexus.initid.id) % worker_threads;
13981 thr = &softc->threads[idx];
13982 mtx_lock(&thr->queue_lock);
13983 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13984 mtx_unlock(&thr->queue_lock);
13989 ctl_enqueue_rtr(union ctl_io *io)
13991 struct ctl_softc *softc = control_softc;
13992 struct ctl_thread *thr;
13994 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13995 mtx_lock(&thr->queue_lock);
13996 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13997 mtx_unlock(&thr->queue_lock);
14002 ctl_enqueue_done(union ctl_io *io)
14004 struct ctl_softc *softc = control_softc;
14005 struct ctl_thread *thr;
14007 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14008 mtx_lock(&thr->queue_lock);
14009 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
14010 mtx_unlock(&thr->queue_lock);
14015 ctl_enqueue_isc(union ctl_io *io)
14017 struct ctl_softc *softc = control_softc;
14018 struct ctl_thread *thr;
14020 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14021 mtx_lock(&thr->queue_lock);
14022 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
14023 mtx_unlock(&thr->queue_lock);
14027 /* Initialization and failover */
14030 ctl_init_isc_msg(void)
14032 printf("CTL: Still calling this thing\n");
14037 * Initializes component into configuration defined by bootMode
14039 * returns hasc_Status:
14041 * ERROR - fatal error
14043 static ctl_ha_comp_status
14044 ctl_isc_init(struct ctl_ha_component *c)
14046 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
14053 * Starts component in state requested. If component starts successfully,
14054 * it must set its own state to the requestrd state
14055 * When requested state is HASC_STATE_HA, the component may refine it
14056 * by adding _SLAVE or _MASTER flags.
14057 * Currently allowed state transitions are:
14058 * UNKNOWN->HA - initial startup
14059 * UNKNOWN->SINGLE - initial startup when no parter detected
14060 * HA->SINGLE - failover
14061 * returns ctl_ha_comp_status:
14062 * OK - component successfully started in requested state
14063 * FAILED - could not start the requested state, failover may
14065 * ERROR - fatal error detected, no future startup possible
14067 static ctl_ha_comp_status
14068 ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state)
14070 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
14072 printf("%s: go\n", __func__);
14074 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap)
14075 if (c->state == CTL_HA_STATE_UNKNOWN ) {
14076 control_softc->is_single = 0;
14077 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
14078 != CTL_HA_STATUS_SUCCESS) {
14079 printf("ctl_isc_start: ctl_ha_msg_create failed.\n");
14080 ret = CTL_HA_COMP_STATUS_ERROR;
14082 } else if (CTL_HA_STATE_IS_HA(c->state)
14083 && CTL_HA_STATE_IS_SINGLE(state)){
14084 // HA->SINGLE transition
14086 control_softc->is_single = 1;
14088 printf("ctl_isc_start:Invalid state transition %X->%X\n",
14090 ret = CTL_HA_COMP_STATUS_ERROR;
14092 if (CTL_HA_STATE_IS_SINGLE(state))
14093 control_softc->is_single = 1;
14101 * Quiesce component
14102 * The component must clear any error conditions (set status to OK) and
14103 * prepare itself to another Start call
14104 * returns ctl_ha_comp_status:
14108 static ctl_ha_comp_status
14109 ctl_isc_quiesce(struct ctl_ha_component *c)
14111 int ret = CTL_HA_COMP_STATUS_OK;
14118 struct ctl_ha_component ctl_ha_component_ctlisc =
14121 .state = CTL_HA_STATE_UNKNOWN,
14122 .init = ctl_isc_init,
14123 .start = ctl_isc_start,
14124 .quiesce = ctl_isc_quiesce