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 int persis_offset;
360 static uint8_t ctl_pause_rtr;
362 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
363 static int worker_threads = -1;
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 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN,
368 &ctl_debug, 0, "Enabled debug flags");
371 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
372 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
373 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0),
374 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
376 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 10
378 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
380 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
381 static int ctl_init(void);
382 void ctl_shutdown(void);
383 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
384 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
385 static void ctl_ioctl_online(void *arg);
386 static void ctl_ioctl_offline(void *arg);
387 static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id);
388 static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id);
389 static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio);
390 static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
391 static int ctl_ioctl_submit_wait(union ctl_io *io);
392 static void ctl_ioctl_datamove(union ctl_io *io);
393 static void ctl_ioctl_done(union ctl_io *io);
394 static void ctl_ioctl_hard_startstop_callback(void *arg,
395 struct cfi_metatask *metatask);
396 static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask);
397 static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
398 struct ctl_ooa *ooa_hdr,
399 struct ctl_ooa_entry *kern_entries);
400 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
402 static uint32_t ctl_map_lun(int port_num, uint32_t lun);
403 static uint32_t ctl_map_lun_back(int port_num, uint32_t lun);
405 static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port,
406 uint32_t targ_target, uint32_t targ_lun,
408 static void ctl_kfree_io(union ctl_io *io);
410 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
411 struct ctl_be_lun *be_lun, struct ctl_id target_id);
412 static int ctl_free_lun(struct ctl_lun *lun);
413 static void ctl_create_lun(struct ctl_be_lun *be_lun);
415 static void ctl_failover_change_pages(struct ctl_softc *softc,
416 struct ctl_scsiio *ctsio, int master);
419 static int ctl_do_mode_select(union ctl_io *io);
420 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
421 uint64_t res_key, uint64_t sa_res_key,
422 uint8_t type, uint32_t residx,
423 struct ctl_scsiio *ctsio,
424 struct scsi_per_res_out *cdb,
425 struct scsi_per_res_out_parms* param);
426 static void ctl_pro_preempt_other(struct ctl_lun *lun,
427 union ctl_ha_msg *msg);
428 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
429 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
430 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
431 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
432 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
433 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
434 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
436 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
438 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
439 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
440 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
441 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
442 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
443 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2);
444 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
445 union ctl_io *pending_io, union ctl_io *ooa_io);
446 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
447 union ctl_io *starting_io);
448 static int ctl_check_blocked(struct ctl_lun *lun);
449 static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc,
451 const struct ctl_cmd_entry *entry,
452 struct ctl_scsiio *ctsio);
453 //static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc);
454 static void ctl_failover(void);
455 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
456 struct ctl_scsiio *ctsio);
457 static int ctl_scsiio(struct ctl_scsiio *ctsio);
459 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
460 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
461 ctl_ua_type ua_type);
462 static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io,
463 ctl_ua_type ua_type);
464 static int ctl_abort_task(union ctl_io *io);
465 static int ctl_abort_task_set(union ctl_io *io);
466 static int ctl_i_t_nexus_reset(union ctl_io *io);
467 static void ctl_run_task(union ctl_io *io);
469 static void ctl_datamove_timer_wakeup(void *arg);
470 static void ctl_done_timer_wakeup(void *arg);
471 #endif /* CTL_IO_DELAY */
473 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
474 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
475 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
476 static void ctl_datamove_remote_write(union ctl_io *io);
477 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
478 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
479 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
480 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
481 ctl_ha_dt_cb callback);
482 static void ctl_datamove_remote_read(union ctl_io *io);
483 static void ctl_datamove_remote(union ctl_io *io);
484 static int ctl_process_done(union ctl_io *io);
485 static void ctl_lun_thread(void *arg);
486 static void ctl_thresh_thread(void *arg);
487 static void ctl_work_thread(void *arg);
488 static void ctl_enqueue_incoming(union ctl_io *io);
489 static void ctl_enqueue_rtr(union ctl_io *io);
490 static void ctl_enqueue_done(union ctl_io *io);
491 static void ctl_enqueue_isc(union ctl_io *io);
492 static const struct ctl_cmd_entry *
493 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
494 static const struct ctl_cmd_entry *
495 ctl_validate_command(struct ctl_scsiio *ctsio);
496 static int ctl_cmd_applicable(uint8_t lun_type,
497 const struct ctl_cmd_entry *entry);
500 * Load the serialization table. This isn't very pretty, but is probably
501 * the easiest way to do it.
503 #include "ctl_ser_table.c"
506 * We only need to define open, close and ioctl routines for this driver.
508 static struct cdevsw ctl_cdevsw = {
509 .d_version = D_VERSION,
512 .d_close = ctl_close,
513 .d_ioctl = ctl_ioctl,
518 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
519 MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests");
521 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
523 static moduledata_t ctl_moduledata = {
525 ctl_module_event_handler,
529 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
530 MODULE_VERSION(ctl, 1);
532 static struct ctl_frontend ioctl_frontend =
538 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
539 union ctl_ha_msg *msg_info)
541 struct ctl_scsiio *ctsio;
543 if (msg_info->hdr.original_sc == NULL) {
544 printf("%s: original_sc == NULL!\n", __func__);
545 /* XXX KDM now what? */
549 ctsio = &msg_info->hdr.original_sc->scsiio;
550 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
551 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
552 ctsio->io_hdr.status = msg_info->hdr.status;
553 ctsio->scsi_status = msg_info->scsi.scsi_status;
554 ctsio->sense_len = msg_info->scsi.sense_len;
555 ctsio->sense_residual = msg_info->scsi.sense_residual;
556 ctsio->residual = msg_info->scsi.residual;
557 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
558 sizeof(ctsio->sense_data));
559 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
560 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen));
561 ctl_enqueue_isc((union ctl_io *)ctsio);
565 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
566 union ctl_ha_msg *msg_info)
568 struct ctl_scsiio *ctsio;
570 if (msg_info->hdr.serializing_sc == NULL) {
571 printf("%s: serializing_sc == NULL!\n", __func__);
572 /* XXX KDM now what? */
576 ctsio = &msg_info->hdr.serializing_sc->scsiio;
579 * Attempt to catch the situation where an I/O has
580 * been freed, and we're using it again.
582 if (ctsio->io_hdr.io_type == 0xff) {
583 union ctl_io *tmp_io;
584 tmp_io = (union ctl_io *)ctsio;
585 printf("%s: %p use after free!\n", __func__,
587 printf("%s: type %d msg %d cdb %x iptl: "
588 "%d:%d:%d:%d tag 0x%04x "
589 "flag %#x status %x\n",
591 tmp_io->io_hdr.io_type,
592 tmp_io->io_hdr.msg_type,
593 tmp_io->scsiio.cdb[0],
594 tmp_io->io_hdr.nexus.initid.id,
595 tmp_io->io_hdr.nexus.targ_port,
596 tmp_io->io_hdr.nexus.targ_target.id,
597 tmp_io->io_hdr.nexus.targ_lun,
598 (tmp_io->io_hdr.io_type ==
600 tmp_io->taskio.tag_num :
601 tmp_io->scsiio.tag_num,
602 tmp_io->io_hdr.flags,
603 tmp_io->io_hdr.status);
606 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
607 ctl_enqueue_isc((union ctl_io *)ctsio);
611 * ISC (Inter Shelf Communication) event handler. Events from the HA
612 * subsystem come in here.
615 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
617 struct ctl_softc *ctl_softc;
619 struct ctl_prio *presio;
620 ctl_ha_status isc_status;
622 ctl_softc = control_softc;
627 printf("CTL: Isc Msg event %d\n", event);
629 if (event == CTL_HA_EVT_MSG_RECV) {
630 union ctl_ha_msg msg_info;
632 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
633 sizeof(msg_info), /*wait*/ 0);
635 printf("CTL: msg_type %d\n", msg_info.msg_type);
637 if (isc_status != 0) {
638 printf("Error receiving message, status = %d\n",
643 switch (msg_info.hdr.msg_type) {
644 case CTL_MSG_SERIALIZE:
646 printf("Serialize\n");
648 io = ctl_alloc_io_nowait(ctl_softc->othersc_pool);
650 printf("ctl_isc_event_handler: can't allocate "
653 /* Need to set busy and send msg back */
654 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
655 msg_info.hdr.status = CTL_SCSI_ERROR;
656 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY;
657 msg_info.scsi.sense_len = 0;
658 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
659 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){
664 // populate ctsio from msg_info
665 io->io_hdr.io_type = CTL_IO_SCSI;
666 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
667 io->io_hdr.original_sc = msg_info.hdr.original_sc;
669 printf("pOrig %x\n", (int)msg_info.original_sc);
671 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
674 * If we're in serialization-only mode, we don't
675 * want to go through full done processing. Thus
678 * XXX KDM add another flag that is more specific.
680 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)
681 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
682 io->io_hdr.nexus = msg_info.hdr.nexus;
684 printf("targ %d, port %d, iid %d, lun %d\n",
685 io->io_hdr.nexus.targ_target.id,
686 io->io_hdr.nexus.targ_port,
687 io->io_hdr.nexus.initid.id,
688 io->io_hdr.nexus.targ_lun);
690 io->scsiio.tag_num = msg_info.scsi.tag_num;
691 io->scsiio.tag_type = msg_info.scsi.tag_type;
692 memcpy(io->scsiio.cdb, msg_info.scsi.cdb,
694 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
695 const struct ctl_cmd_entry *entry;
697 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
698 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
700 entry->flags & CTL_FLAG_DATA_MASK;
705 /* Performed on the Originating SC, XFER mode only */
706 case CTL_MSG_DATAMOVE: {
707 struct ctl_sg_entry *sgl;
710 io = msg_info.hdr.original_sc;
712 printf("%s: original_sc == NULL!\n", __func__);
713 /* XXX KDM do something here */
716 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
717 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
719 * Keep track of this, we need to send it back over
720 * when the datamove is complete.
722 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
724 if (msg_info.dt.sg_sequence == 0) {
726 * XXX KDM we use the preallocated S/G list
727 * here, but we'll need to change this to
728 * dynamic allocation if we need larger S/G
731 if (msg_info.dt.kern_sg_entries >
732 sizeof(io->io_hdr.remote_sglist) /
733 sizeof(io->io_hdr.remote_sglist[0])) {
734 printf("%s: number of S/G entries "
735 "needed %u > allocated num %zd\n",
737 msg_info.dt.kern_sg_entries,
738 sizeof(io->io_hdr.remote_sglist)/
739 sizeof(io->io_hdr.remote_sglist[0]));
742 * XXX KDM send a message back to
743 * the other side to shut down the
744 * DMA. The error will come back
745 * through via the normal channel.
749 sgl = io->io_hdr.remote_sglist;
751 sizeof(io->io_hdr.remote_sglist));
753 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
755 io->scsiio.kern_sg_entries =
756 msg_info.dt.kern_sg_entries;
757 io->scsiio.rem_sg_entries =
758 msg_info.dt.kern_sg_entries;
759 io->scsiio.kern_data_len =
760 msg_info.dt.kern_data_len;
761 io->scsiio.kern_total_len =
762 msg_info.dt.kern_total_len;
763 io->scsiio.kern_data_resid =
764 msg_info.dt.kern_data_resid;
765 io->scsiio.kern_rel_offset =
766 msg_info.dt.kern_rel_offset;
768 * Clear out per-DMA flags.
770 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK;
772 * Add per-DMA flags that are set for this
773 * particular DMA request.
775 io->io_hdr.flags |= msg_info.dt.flags &
778 sgl = (struct ctl_sg_entry *)
779 io->scsiio.kern_data_ptr;
781 for (i = msg_info.dt.sent_sg_entries, j = 0;
782 i < (msg_info.dt.sent_sg_entries +
783 msg_info.dt.cur_sg_entries); i++, j++) {
784 sgl[i].addr = msg_info.dt.sg_list[j].addr;
785 sgl[i].len = msg_info.dt.sg_list[j].len;
788 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n",
790 msg_info.dt.sg_list[j].addr,
791 msg_info.dt.sg_list[j].len,
792 sgl[i].addr, sgl[i].len, j, i);
796 memcpy(&sgl[msg_info.dt.sent_sg_entries],
798 sizeof(*sgl) * msg_info.dt.cur_sg_entries);
802 * If this is the last piece of the I/O, we've got
803 * the full S/G list. Queue processing in the thread.
804 * Otherwise wait for the next piece.
806 if (msg_info.dt.sg_last != 0)
810 /* Performed on the Serializing (primary) SC, XFER mode only */
811 case CTL_MSG_DATAMOVE_DONE: {
812 if (msg_info.hdr.serializing_sc == NULL) {
813 printf("%s: serializing_sc == NULL!\n",
815 /* XXX KDM now what? */
819 * We grab the sense information here in case
820 * there was a failure, so we can return status
821 * back to the initiator.
823 io = msg_info.hdr.serializing_sc;
824 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
825 io->io_hdr.status = msg_info.hdr.status;
826 io->scsiio.scsi_status = msg_info.scsi.scsi_status;
827 io->scsiio.sense_len = msg_info.scsi.sense_len;
828 io->scsiio.sense_residual =msg_info.scsi.sense_residual;
829 io->io_hdr.port_status = msg_info.scsi.fetd_status;
830 io->scsiio.residual = msg_info.scsi.residual;
831 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data,
832 sizeof(io->scsiio.sense_data));
837 /* Preformed on Originating SC, SER_ONLY mode */
839 io = msg_info.hdr.original_sc;
841 printf("%s: Major Bummer\n", __func__);
845 printf("pOrig %x\n",(int) ctsio);
848 io->io_hdr.msg_type = CTL_MSG_R2R;
849 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
854 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
856 * Performed on the Originating (i.e. secondary) SC in XFER
859 case CTL_MSG_FINISH_IO:
860 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER)
861 ctl_isc_handler_finish_xfer(ctl_softc,
864 ctl_isc_handler_finish_ser_only(ctl_softc,
868 /* Preformed on Originating SC */
869 case CTL_MSG_BAD_JUJU:
870 io = msg_info.hdr.original_sc;
872 printf("%s: Bad JUJU!, original_sc is NULL!\n",
876 ctl_copy_sense_data(&msg_info, io);
878 * IO should have already been cleaned up on other
879 * SC so clear this flag so we won't send a message
880 * back to finish the IO there.
882 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
883 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
885 /* io = msg_info.hdr.serializing_sc; */
886 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
890 /* Handle resets sent from the other side */
891 case CTL_MSG_MANAGE_TASKS: {
892 struct ctl_taskio *taskio;
893 taskio = (struct ctl_taskio *)ctl_alloc_io_nowait(
894 ctl_softc->othersc_pool);
895 if (taskio == NULL) {
896 printf("ctl_isc_event_handler: can't allocate "
899 /* should I just call the proper reset func
903 ctl_zero_io((union ctl_io *)taskio);
904 taskio->io_hdr.io_type = CTL_IO_TASK;
905 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
906 taskio->io_hdr.nexus = msg_info.hdr.nexus;
907 taskio->task_action = msg_info.task.task_action;
908 taskio->tag_num = msg_info.task.tag_num;
909 taskio->tag_type = msg_info.task.tag_type;
911 taskio->io_hdr.start_time = time_uptime;
912 getbintime(&taskio->io_hdr.start_bt);
914 cs_prof_gettime(&taskio->io_hdr.start_ticks);
916 #endif /* CTL_TIME_IO */
917 ctl_run_task((union ctl_io *)taskio);
920 /* Persistent Reserve action which needs attention */
921 case CTL_MSG_PERS_ACTION:
922 presio = (struct ctl_prio *)ctl_alloc_io_nowait(
923 ctl_softc->othersc_pool);
924 if (presio == NULL) {
925 printf("ctl_isc_event_handler: can't allocate "
928 /* Need to set busy and send msg back */
931 ctl_zero_io((union ctl_io *)presio);
932 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
933 presio->pr_msg = msg_info.pr;
934 ctl_enqueue_isc((union ctl_io *)presio);
936 case CTL_MSG_SYNC_FE:
940 printf("How did I get here?\n");
942 } else if (event == CTL_HA_EVT_MSG_SENT) {
943 if (param != CTL_HA_STATUS_SUCCESS) {
944 printf("Bad status from ctl_ha_msg_send status %d\n",
948 } else if (event == CTL_HA_EVT_DISCONNECT) {
949 printf("CTL: Got a disconnect from Isc\n");
952 printf("ctl_isc_event_handler: Unknown event %d\n", event);
961 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
963 struct scsi_sense_data *sense;
965 sense = &dest->scsiio.sense_data;
966 bcopy(&src->scsi.sense_data, sense, sizeof(*sense));
967 dest->scsiio.scsi_status = src->scsi.scsi_status;
968 dest->scsiio.sense_len = src->scsi.sense_len;
969 dest->io_hdr.status = src->hdr.status;
973 ctl_ha_state_sysctl(SYSCTL_HANDLER_ARGS)
975 struct ctl_softc *softc = (struct ctl_softc *)arg1;
979 if (softc->flags & CTL_FLAG_ACTIVE_SHELF)
984 error = sysctl_handle_int(oidp, &value, 0, req);
985 if ((error != 0) || (req->newptr == NULL))
988 mtx_lock(&softc->ctl_lock);
990 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
992 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
993 STAILQ_FOREACH(lun, &softc->lun_list, links) {
994 mtx_lock(&lun->lun_lock);
995 for (i = 0; i < CTL_MAX_INITIATORS; i++)
996 lun->pending_ua[i] |= CTL_UA_ASYM_ACC_CHANGE;
997 mtx_unlock(&lun->lun_lock);
999 mtx_unlock(&softc->ctl_lock);
1006 struct ctl_softc *softc;
1008 struct ctl_port *port;
1009 int i, error, retval;
1016 control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1018 softc = control_softc;
1020 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
1023 softc->dev->si_drv1 = softc;
1026 * By default, return a "bad LUN" peripheral qualifier for unknown
1027 * LUNs. The user can override this default using the tunable or
1028 * sysctl. See the comment in ctl_inquiry_std() for more details.
1030 softc->inquiry_pq_no_lun = 1;
1031 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun",
1032 &softc->inquiry_pq_no_lun);
1033 sysctl_ctx_init(&softc->sysctl_ctx);
1034 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1035 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1036 CTLFLAG_RD, 0, "CAM Target Layer");
1038 if (softc->sysctl_tree == NULL) {
1039 printf("%s: unable to allocate sysctl tree\n", __func__);
1040 destroy_dev(softc->dev);
1041 free(control_softc, M_DEVBUF);
1042 control_softc = NULL;
1046 SYSCTL_ADD_INT(&softc->sysctl_ctx,
1047 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
1048 "inquiry_pq_no_lun", CTLFLAG_RW,
1049 &softc->inquiry_pq_no_lun, 0,
1050 "Report no lun possible for invalid LUNs");
1052 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1053 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
1054 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1055 softc->open_count = 0;
1058 * Default to actually sending a SYNCHRONIZE CACHE command down to
1061 softc->flags = CTL_FLAG_REAL_SYNC;
1064 * In Copan's HA scheme, the "master" and "slave" roles are
1065 * figured out through the slot the controller is in. Although it
1066 * is an active/active system, someone has to be in charge.
1068 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1069 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1070 "HA head ID (0 - no HA)");
1071 if (softc->ha_id == 0) {
1072 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1073 softc->is_single = 1;
1074 softc->port_offset = 0;
1076 softc->port_offset = (softc->ha_id - 1) * CTL_MAX_PORTS;
1077 persis_offset = softc->port_offset * CTL_MAX_INIT_PER_PORT;
1080 * XXX KDM need to figure out where we want to get our target ID
1081 * and WWID. Is it different on each port?
1083 softc->target.id = 0;
1084 softc->target.wwid[0] = 0x12345678;
1085 softc->target.wwid[1] = 0x87654321;
1086 STAILQ_INIT(&softc->lun_list);
1087 STAILQ_INIT(&softc->pending_lun_queue);
1088 STAILQ_INIT(&softc->fe_list);
1089 STAILQ_INIT(&softc->port_list);
1090 STAILQ_INIT(&softc->be_list);
1091 ctl_tpc_init(softc);
1093 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
1096 printf("ctl: can't allocate %d entry other SC pool, "
1097 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1100 softc->othersc_pool = other_pool;
1102 if (worker_threads <= 0)
1103 worker_threads = max(1, mp_ncpus / 4);
1104 if (worker_threads > CTL_MAX_THREADS)
1105 worker_threads = CTL_MAX_THREADS;
1107 for (i = 0; i < worker_threads; i++) {
1108 struct ctl_thread *thr = &softc->threads[i];
1110 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1111 thr->ctl_softc = softc;
1112 STAILQ_INIT(&thr->incoming_queue);
1113 STAILQ_INIT(&thr->rtr_queue);
1114 STAILQ_INIT(&thr->done_queue);
1115 STAILQ_INIT(&thr->isc_queue);
1117 error = kproc_kthread_add(ctl_work_thread, thr,
1118 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1120 printf("error creating CTL work thread!\n");
1121 ctl_pool_free(other_pool);
1125 error = kproc_kthread_add(ctl_lun_thread, softc,
1126 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
1128 printf("error creating CTL lun thread!\n");
1129 ctl_pool_free(other_pool);
1132 error = kproc_kthread_add(ctl_thresh_thread, softc,
1133 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh");
1135 printf("error creating CTL threshold thread!\n");
1136 ctl_pool_free(other_pool);
1140 printf("ctl: CAM Target Layer loaded\n");
1143 * Initialize the ioctl front end.
1145 ctl_frontend_register(&ioctl_frontend);
1146 port = &softc->ioctl_info.port;
1147 port->frontend = &ioctl_frontend;
1148 sprintf(softc->ioctl_info.port_name, "ioctl");
1149 port->port_type = CTL_PORT_IOCTL;
1150 port->num_requested_ctl_io = 100;
1151 port->port_name = softc->ioctl_info.port_name;
1152 port->port_online = ctl_ioctl_online;
1153 port->port_offline = ctl_ioctl_offline;
1154 port->onoff_arg = &softc->ioctl_info;
1155 port->lun_enable = ctl_ioctl_lun_enable;
1156 port->lun_disable = ctl_ioctl_lun_disable;
1157 port->targ_lun_arg = &softc->ioctl_info;
1158 port->fe_datamove = ctl_ioctl_datamove;
1159 port->fe_done = ctl_ioctl_done;
1160 port->max_targets = 15;
1161 port->max_target_id = 15;
1163 if (ctl_port_register(&softc->ioctl_info.port) != 0) {
1164 printf("ctl: ioctl front end registration failed, will "
1165 "continue anyway\n");
1168 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1169 OID_AUTO, "ha_state", CTLTYPE_INT | CTLFLAG_RWTUN,
1170 softc, 0, ctl_ha_state_sysctl, "I", "HA state for this head");
1173 if (sizeof(struct callout) > CTL_TIMER_BYTES) {
1174 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n",
1175 sizeof(struct callout), CTL_TIMER_BYTES);
1178 #endif /* CTL_IO_DELAY */
1186 struct ctl_softc *softc;
1187 struct ctl_lun *lun, *next_lun;
1189 softc = (struct ctl_softc *)control_softc;
1191 if (ctl_port_deregister(&softc->ioctl_info.port) != 0)
1192 printf("ctl: ioctl front end deregistration failed\n");
1194 mtx_lock(&softc->ctl_lock);
1199 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
1200 next_lun = STAILQ_NEXT(lun, links);
1204 mtx_unlock(&softc->ctl_lock);
1206 ctl_frontend_deregister(&ioctl_frontend);
1209 ctl_shutdown_thread(softc->work_thread);
1210 mtx_destroy(&softc->queue_lock);
1213 ctl_tpc_shutdown(softc);
1214 uma_zdestroy(softc->io_zone);
1215 mtx_destroy(&softc->ctl_lock);
1217 destroy_dev(softc->dev);
1219 sysctl_ctx_free(&softc->sysctl_ctx);
1221 free(control_softc, M_DEVBUF);
1222 control_softc = NULL;
1225 printf("ctl: CAM Target Layer unloaded\n");
1229 ctl_module_event_handler(module_t mod, int what, void *arg)
1234 return (ctl_init());
1238 return (EOPNOTSUPP);
1243 * XXX KDM should we do some access checks here? Bump a reference count to
1244 * prevent a CTL module from being unloaded while someone has it open?
1247 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1253 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1259 ctl_port_enable(ctl_port_type port_type)
1261 struct ctl_softc *softc = control_softc;
1262 struct ctl_port *port;
1264 if (softc->is_single == 0) {
1265 union ctl_ha_msg msg_info;
1269 printf("%s: HA mode, synchronizing frontend enable\n",
1272 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE;
1273 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1274 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) {
1275 printf("Sync msg send error retval %d\n", isc_retval);
1277 if (!rcv_sync_msg) {
1278 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
1279 sizeof(msg_info), 1);
1282 printf("CTL:Frontend Enable\n");
1284 printf("%s: single mode, skipping frontend synchronization\n",
1289 STAILQ_FOREACH(port, &softc->port_list, links) {
1290 if (port_type & port->port_type)
1293 printf("port %d\n", port->targ_port);
1295 ctl_port_online(port);
1303 ctl_port_disable(ctl_port_type port_type)
1305 struct ctl_softc *softc;
1306 struct ctl_port *port;
1308 softc = control_softc;
1310 STAILQ_FOREACH(port, &softc->port_list, links) {
1311 if (port_type & port->port_type)
1312 ctl_port_offline(port);
1319 * Returns 0 for success, 1 for failure.
1320 * Currently the only failure mode is if there aren't enough entries
1321 * allocated. So, in case of a failure, look at num_entries_dropped,
1322 * reallocate and try again.
1325 ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced,
1326 int *num_entries_filled, int *num_entries_dropped,
1327 ctl_port_type port_type, int no_virtual)
1329 struct ctl_softc *softc;
1330 struct ctl_port *port;
1331 int entries_dropped, entries_filled;
1335 softc = control_softc;
1339 entries_dropped = 0;
1342 mtx_lock(&softc->ctl_lock);
1343 STAILQ_FOREACH(port, &softc->port_list, links) {
1344 struct ctl_port_entry *entry;
1346 if ((port->port_type & port_type) == 0)
1349 if ((no_virtual != 0)
1350 && (port->virtual_port != 0))
1353 if (entries_filled >= num_entries_alloced) {
1357 entry = &entries[i];
1359 entry->port_type = port->port_type;
1360 strlcpy(entry->port_name, port->port_name,
1361 sizeof(entry->port_name));
1362 entry->physical_port = port->physical_port;
1363 entry->virtual_port = port->virtual_port;
1364 entry->wwnn = port->wwnn;
1365 entry->wwpn = port->wwpn;
1371 mtx_unlock(&softc->ctl_lock);
1373 if (entries_dropped > 0)
1376 *num_entries_dropped = entries_dropped;
1377 *num_entries_filled = entries_filled;
1383 ctl_ioctl_online(void *arg)
1385 struct ctl_ioctl_info *ioctl_info;
1387 ioctl_info = (struct ctl_ioctl_info *)arg;
1389 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED;
1393 ctl_ioctl_offline(void *arg)
1395 struct ctl_ioctl_info *ioctl_info;
1397 ioctl_info = (struct ctl_ioctl_info *)arg;
1399 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED;
1403 * Remove an initiator by port number and initiator ID.
1404 * Returns 0 for success, -1 for failure.
1407 ctl_remove_initiator(struct ctl_port *port, int iid)
1409 struct ctl_softc *softc = control_softc;
1411 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1413 if (iid > CTL_MAX_INIT_PER_PORT) {
1414 printf("%s: initiator ID %u > maximun %u!\n",
1415 __func__, iid, CTL_MAX_INIT_PER_PORT);
1419 mtx_lock(&softc->ctl_lock);
1420 port->wwpn_iid[iid].in_use--;
1421 port->wwpn_iid[iid].last_use = time_uptime;
1422 mtx_unlock(&softc->ctl_lock);
1428 * Add an initiator to the initiator map.
1429 * Returns iid for success, < 0 for failure.
1432 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
1434 struct ctl_softc *softc = control_softc;
1438 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1440 if (iid >= CTL_MAX_INIT_PER_PORT) {
1441 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
1442 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
1447 mtx_lock(&softc->ctl_lock);
1449 if (iid < 0 && (wwpn != 0 || name != NULL)) {
1450 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1451 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
1455 if (name != NULL && port->wwpn_iid[i].name != NULL &&
1456 strcmp(name, port->wwpn_iid[i].name) == 0) {
1464 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1465 if (port->wwpn_iid[i].in_use == 0 &&
1466 port->wwpn_iid[i].wwpn == 0 &&
1467 port->wwpn_iid[i].name == NULL) {
1476 best_time = INT32_MAX;
1477 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1478 if (port->wwpn_iid[i].in_use == 0) {
1479 if (port->wwpn_iid[i].last_use < best_time) {
1481 best_time = port->wwpn_iid[i].last_use;
1489 mtx_unlock(&softc->ctl_lock);
1494 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
1496 * This is not an error yet.
1498 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
1500 printf("%s: port %d iid %u WWPN %#jx arrived"
1501 " again\n", __func__, port->targ_port,
1502 iid, (uintmax_t)wwpn);
1506 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
1507 strcmp(name, port->wwpn_iid[iid].name) == 0) {
1509 printf("%s: port %d iid %u name '%s' arrived"
1510 " again\n", __func__, port->targ_port,
1517 * This is an error, but what do we do about it? The
1518 * driver is telling us we have a new WWPN for this
1519 * initiator ID, so we pretty much need to use it.
1521 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
1522 " but WWPN %#jx '%s' is still at that address\n",
1523 __func__, port->targ_port, iid, wwpn, name,
1524 (uintmax_t)port->wwpn_iid[iid].wwpn,
1525 port->wwpn_iid[iid].name);
1528 * XXX KDM clear have_ca and ua_pending on each LUN for
1533 free(port->wwpn_iid[iid].name, M_CTL);
1534 port->wwpn_iid[iid].name = name;
1535 port->wwpn_iid[iid].wwpn = wwpn;
1536 port->wwpn_iid[iid].in_use++;
1537 mtx_unlock(&softc->ctl_lock);
1543 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
1547 switch (port->port_type) {
1550 struct scsi_transportid_fcp *id =
1551 (struct scsi_transportid_fcp *)buf;
1552 if (port->wwpn_iid[iid].wwpn == 0)
1554 memset(id, 0, sizeof(*id));
1555 id->format_protocol = SCSI_PROTO_FC;
1556 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
1557 return (sizeof(*id));
1559 case CTL_PORT_ISCSI:
1561 struct scsi_transportid_iscsi_port *id =
1562 (struct scsi_transportid_iscsi_port *)buf;
1563 if (port->wwpn_iid[iid].name == NULL)
1566 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
1568 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
1569 len = roundup2(min(len, 252), 4);
1570 scsi_ulto2b(len, id->additional_length);
1571 return (sizeof(*id) + len);
1575 struct scsi_transportid_sas *id =
1576 (struct scsi_transportid_sas *)buf;
1577 if (port->wwpn_iid[iid].wwpn == 0)
1579 memset(id, 0, sizeof(*id));
1580 id->format_protocol = SCSI_PROTO_SAS;
1581 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
1582 return (sizeof(*id));
1586 struct scsi_transportid_spi *id =
1587 (struct scsi_transportid_spi *)buf;
1588 memset(id, 0, sizeof(*id));
1589 id->format_protocol = SCSI_PROTO_SPI;
1590 scsi_ulto2b(iid, id->scsi_addr);
1591 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
1592 return (sizeof(*id));
1598 ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id)
1604 ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id)
1610 * Data movement routine for the CTL ioctl frontend port.
1613 ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio)
1615 struct ctl_sg_entry *ext_sglist, *kern_sglist;
1616 struct ctl_sg_entry ext_entry, kern_entry;
1617 int ext_sglen, ext_sg_entries, kern_sg_entries;
1618 int ext_sg_start, ext_offset;
1619 int len_to_copy, len_copied;
1620 int kern_watermark, ext_watermark;
1621 int ext_sglist_malloced;
1624 ext_sglist_malloced = 0;
1628 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n"));
1631 * If this flag is set, fake the data transfer.
1633 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) {
1634 ctsio->ext_data_filled = ctsio->ext_data_len;
1639 * To simplify things here, if we have a single buffer, stick it in
1640 * a S/G entry and just make it a single entry S/G list.
1642 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) {
1645 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist);
1647 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL,
1649 ext_sglist_malloced = 1;
1650 if (copyin(ctsio->ext_data_ptr, ext_sglist,
1652 ctl_set_internal_failure(ctsio,
1657 ext_sg_entries = ctsio->ext_sg_entries;
1659 for (i = 0; i < ext_sg_entries; i++) {
1660 if ((len_seen + ext_sglist[i].len) >=
1661 ctsio->ext_data_filled) {
1663 ext_offset = ctsio->ext_data_filled - len_seen;
1666 len_seen += ext_sglist[i].len;
1669 ext_sglist = &ext_entry;
1670 ext_sglist->addr = ctsio->ext_data_ptr;
1671 ext_sglist->len = ctsio->ext_data_len;
1674 ext_offset = ctsio->ext_data_filled;
1677 if (ctsio->kern_sg_entries > 0) {
1678 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
1679 kern_sg_entries = ctsio->kern_sg_entries;
1681 kern_sglist = &kern_entry;
1682 kern_sglist->addr = ctsio->kern_data_ptr;
1683 kern_sglist->len = ctsio->kern_data_len;
1684 kern_sg_entries = 1;
1689 ext_watermark = ext_offset;
1691 for (i = ext_sg_start, j = 0;
1692 i < ext_sg_entries && j < kern_sg_entries;) {
1693 uint8_t *ext_ptr, *kern_ptr;
1695 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark,
1696 kern_sglist[j].len - kern_watermark);
1698 ext_ptr = (uint8_t *)ext_sglist[i].addr;
1699 ext_ptr = ext_ptr + ext_watermark;
1700 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
1704 panic("need to implement bus address support");
1706 kern_ptr = bus_to_virt(kern_sglist[j].addr);
1709 kern_ptr = (uint8_t *)kern_sglist[j].addr;
1710 kern_ptr = kern_ptr + kern_watermark;
1712 kern_watermark += len_to_copy;
1713 ext_watermark += len_to_copy;
1715 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
1717 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1718 "bytes to user\n", len_to_copy));
1719 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1720 "to %p\n", kern_ptr, ext_ptr));
1721 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) {
1722 ctl_set_internal_failure(ctsio,
1728 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1729 "bytes from user\n", len_to_copy));
1730 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1731 "to %p\n", ext_ptr, kern_ptr));
1732 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){
1733 ctl_set_internal_failure(ctsio,
1740 len_copied += len_to_copy;
1742 if (ext_sglist[i].len == ext_watermark) {
1747 if (kern_sglist[j].len == kern_watermark) {
1753 ctsio->ext_data_filled += len_copied;
1755 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, "
1756 "kern_sg_entries: %d\n", ext_sg_entries,
1758 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, "
1759 "kern_data_len = %d\n", ctsio->ext_data_len,
1760 ctsio->kern_data_len));
1763 /* XXX KDM set residual?? */
1766 if (ext_sglist_malloced != 0)
1767 free(ext_sglist, M_CTL);
1769 return (CTL_RETVAL_COMPLETE);
1773 * Serialize a command that went down the "wrong" side, and so was sent to
1774 * this controller for execution. The logic is a little different than the
1775 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
1776 * sent back to the other side, but in the success case, we execute the
1777 * command on this side (XFER mode) or tell the other side to execute it
1781 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
1783 struct ctl_softc *ctl_softc;
1784 union ctl_ha_msg msg_info;
1785 struct ctl_lun *lun;
1789 ctl_softc = control_softc;
1791 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
1792 lun = ctl_softc->ctl_luns[targ_lun];
1796 * Why isn't LUN defined? The other side wouldn't
1797 * send a cmd if the LUN is undefined.
1799 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__);
1801 /* "Logical unit not supported" */
1802 ctl_set_sense_data(&msg_info.scsi.sense_data,
1804 /*sense_format*/SSD_TYPE_NONE,
1805 /*current_error*/ 1,
1806 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1811 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1812 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1813 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1814 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1815 msg_info.hdr.serializing_sc = NULL;
1816 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1817 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1818 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1824 mtx_lock(&lun->lun_lock);
1825 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1827 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
1828 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
1830 case CTL_ACTION_BLOCK:
1831 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
1832 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
1835 case CTL_ACTION_PASS:
1836 case CTL_ACTION_SKIP:
1837 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
1838 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
1839 ctl_enqueue_rtr((union ctl_io *)ctsio);
1842 /* send msg back to other side */
1843 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1844 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
1845 msg_info.hdr.msg_type = CTL_MSG_R2R;
1847 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc);
1849 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1850 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1854 case CTL_ACTION_OVERLAP:
1855 /* OVERLAPPED COMMANDS ATTEMPTED */
1856 ctl_set_sense_data(&msg_info.scsi.sense_data,
1858 /*sense_format*/SSD_TYPE_NONE,
1859 /*current_error*/ 1,
1860 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1865 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1866 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1867 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1868 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1869 msg_info.hdr.serializing_sc = NULL;
1870 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1872 printf("BAD JUJU:Major Bummer Overlap\n");
1874 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1876 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1877 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1880 case CTL_ACTION_OVERLAP_TAG:
1881 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */
1882 ctl_set_sense_data(&msg_info.scsi.sense_data,
1884 /*sense_format*/SSD_TYPE_NONE,
1885 /*current_error*/ 1,
1886 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1888 /*ascq*/ ctsio->tag_num & 0xff,
1891 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1892 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1893 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1894 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1895 msg_info.hdr.serializing_sc = NULL;
1896 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1898 printf("BAD JUJU:Major Bummer Overlap Tag\n");
1900 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1902 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1903 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1906 case CTL_ACTION_ERROR:
1908 /* "Internal target failure" */
1909 ctl_set_sense_data(&msg_info.scsi.sense_data,
1911 /*sense_format*/SSD_TYPE_NONE,
1912 /*current_error*/ 1,
1913 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
1918 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1919 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1920 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1921 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1922 msg_info.hdr.serializing_sc = NULL;
1923 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1925 printf("BAD JUJU:Major Bummer HW Error\n");
1927 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1929 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1930 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1934 mtx_unlock(&lun->lun_lock);
1939 ctl_ioctl_submit_wait(union ctl_io *io)
1941 struct ctl_fe_ioctl_params params;
1942 ctl_fe_ioctl_state last_state;
1947 bzero(¶ms, sizeof(params));
1949 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF);
1950 cv_init(¶ms.sem, "ctlioccv");
1951 params.state = CTL_IOCTL_INPROG;
1952 last_state = params.state;
1954 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms;
1956 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n"));
1958 /* This shouldn't happen */
1959 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE)
1965 mtx_lock(¶ms.ioctl_mtx);
1967 * Check the state here, and don't sleep if the state has
1968 * already changed (i.e. wakeup has already occured, but we
1969 * weren't waiting yet).
1971 if (params.state == last_state) {
1972 /* XXX KDM cv_wait_sig instead? */
1973 cv_wait(¶ms.sem, ¶ms.ioctl_mtx);
1975 last_state = params.state;
1977 switch (params.state) {
1978 case CTL_IOCTL_INPROG:
1979 /* Why did we wake up? */
1980 /* XXX KDM error here? */
1981 mtx_unlock(¶ms.ioctl_mtx);
1983 case CTL_IOCTL_DATAMOVE:
1984 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n"));
1987 * change last_state back to INPROG to avoid
1988 * deadlock on subsequent data moves.
1990 params.state = last_state = CTL_IOCTL_INPROG;
1992 mtx_unlock(¶ms.ioctl_mtx);
1993 ctl_ioctl_do_datamove(&io->scsiio);
1995 * Note that in some cases, most notably writes,
1996 * this will queue the I/O and call us back later.
1997 * In other cases, generally reads, this routine
1998 * will immediately call back and wake us up,
1999 * probably using our own context.
2001 io->scsiio.be_move_done(io);
2003 case CTL_IOCTL_DONE:
2004 mtx_unlock(¶ms.ioctl_mtx);
2005 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n"));
2009 mtx_unlock(¶ms.ioctl_mtx);
2010 /* XXX KDM error here? */
2013 } while (done == 0);
2015 mtx_destroy(¶ms.ioctl_mtx);
2016 cv_destroy(¶ms.sem);
2018 return (CTL_RETVAL_COMPLETE);
2022 ctl_ioctl_datamove(union ctl_io *io)
2024 struct ctl_fe_ioctl_params *params;
2026 params = (struct ctl_fe_ioctl_params *)
2027 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2029 mtx_lock(¶ms->ioctl_mtx);
2030 params->state = CTL_IOCTL_DATAMOVE;
2031 cv_broadcast(¶ms->sem);
2032 mtx_unlock(¶ms->ioctl_mtx);
2036 ctl_ioctl_done(union ctl_io *io)
2038 struct ctl_fe_ioctl_params *params;
2040 params = (struct ctl_fe_ioctl_params *)
2041 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2043 mtx_lock(¶ms->ioctl_mtx);
2044 params->state = CTL_IOCTL_DONE;
2045 cv_broadcast(¶ms->sem);
2046 mtx_unlock(¶ms->ioctl_mtx);
2050 ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask)
2052 struct ctl_fe_ioctl_startstop_info *sd_info;
2054 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg;
2056 sd_info->hs_info.status = metatask->status;
2057 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns;
2058 sd_info->hs_info.luns_complete =
2059 metatask->taskinfo.startstop.luns_complete;
2060 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed;
2062 cv_broadcast(&sd_info->sem);
2066 ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask)
2068 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info;
2070 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg;
2072 mtx_lock(fe_bbr_info->lock);
2073 fe_bbr_info->bbr_info->status = metatask->status;
2074 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2075 fe_bbr_info->wakeup_done = 1;
2076 mtx_unlock(fe_bbr_info->lock);
2078 cv_broadcast(&fe_bbr_info->sem);
2082 * Returns 0 for success, errno for failure.
2085 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2086 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2093 mtx_lock(&lun->lun_lock);
2094 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2095 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2097 struct ctl_ooa_entry *entry;
2100 * If we've got more than we can fit, just count the
2101 * remaining entries.
2103 if (*cur_fill_num >= ooa_hdr->alloc_num)
2106 entry = &kern_entries[*cur_fill_num];
2108 entry->tag_num = io->scsiio.tag_num;
2109 entry->lun_num = lun->lun;
2111 entry->start_bt = io->io_hdr.start_bt;
2113 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2114 entry->cdb_len = io->scsiio.cdb_len;
2115 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2116 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2118 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2119 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2121 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2122 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2124 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2125 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2127 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2128 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2130 mtx_unlock(&lun->lun_lock);
2136 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2137 size_t error_str_len)
2141 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2143 if (copyin(user_addr, kptr, len) != 0) {
2144 snprintf(error_str, error_str_len, "Error copying %d bytes "
2145 "from user address %p to kernel address %p", len,
2155 ctl_free_args(int num_args, struct ctl_be_arg *args)
2162 for (i = 0; i < num_args; i++) {
2163 free(args[i].kname, M_CTL);
2164 free(args[i].kvalue, M_CTL);
2170 static struct ctl_be_arg *
2171 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
2172 char *error_str, size_t error_str_len)
2174 struct ctl_be_arg *args;
2177 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
2178 error_str, error_str_len);
2183 for (i = 0; i < num_args; i++) {
2184 args[i].kname = NULL;
2185 args[i].kvalue = NULL;
2188 for (i = 0; i < num_args; i++) {
2191 args[i].kname = ctl_copyin_alloc(args[i].name,
2192 args[i].namelen, error_str, error_str_len);
2193 if (args[i].kname == NULL)
2196 if (args[i].kname[args[i].namelen - 1] != '\0') {
2197 snprintf(error_str, error_str_len, "Argument %d "
2198 "name is not NUL-terminated", i);
2202 if (args[i].flags & CTL_BEARG_RD) {
2203 tmpptr = ctl_copyin_alloc(args[i].value,
2204 args[i].vallen, error_str, error_str_len);
2207 if ((args[i].flags & CTL_BEARG_ASCII)
2208 && (tmpptr[args[i].vallen - 1] != '\0')) {
2209 snprintf(error_str, error_str_len, "Argument "
2210 "%d value is not NUL-terminated", i);
2213 args[i].kvalue = tmpptr;
2215 args[i].kvalue = malloc(args[i].vallen,
2216 M_CTL, M_WAITOK | M_ZERO);
2223 ctl_free_args(num_args, args);
2229 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2233 for (i = 0; i < num_args; i++) {
2234 if (args[i].flags & CTL_BEARG_WR)
2235 copyout(args[i].kvalue, args[i].value, args[i].vallen);
2240 * Escape characters that are illegal or not recommended in XML.
2243 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2245 char *end = str + size;
2250 for (; *str && str < end; str++) {
2253 retval = sbuf_printf(sb, "&");
2256 retval = sbuf_printf(sb, ">");
2259 retval = sbuf_printf(sb, "<");
2262 retval = sbuf_putc(sb, *str);
2275 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2277 struct scsi_vpd_id_descriptor *desc;
2280 if (id == NULL || id->len < 4)
2282 desc = (struct scsi_vpd_id_descriptor *)id->data;
2283 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2284 case SVPD_ID_TYPE_T10:
2285 sbuf_printf(sb, "t10.");
2287 case SVPD_ID_TYPE_EUI64:
2288 sbuf_printf(sb, "eui.");
2290 case SVPD_ID_TYPE_NAA:
2291 sbuf_printf(sb, "naa.");
2293 case SVPD_ID_TYPE_SCSI_NAME:
2296 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2297 case SVPD_ID_CODESET_BINARY:
2298 for (i = 0; i < desc->length; i++)
2299 sbuf_printf(sb, "%02x", desc->identifier[i]);
2301 case SVPD_ID_CODESET_ASCII:
2302 sbuf_printf(sb, "%.*s", (int)desc->length,
2303 (char *)desc->identifier);
2305 case SVPD_ID_CODESET_UTF8:
2306 sbuf_printf(sb, "%s", (char *)desc->identifier);
2312 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2315 struct ctl_softc *softc;
2318 softc = control_softc;
2328 * If we haven't been "enabled", don't allow any SCSI I/O
2331 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) {
2336 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref);
2339 * Need to save the pool reference so it doesn't get
2340 * spammed by the user's ctl_io.
2342 pool_tmp = io->io_hdr.pool;
2343 memcpy(io, (void *)addr, sizeof(*io));
2344 io->io_hdr.pool = pool_tmp;
2347 * No status yet, so make sure the status is set properly.
2349 io->io_hdr.status = CTL_STATUS_NONE;
2352 * The user sets the initiator ID, target and LUN IDs.
2354 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port;
2355 io->io_hdr.flags |= CTL_FLAG_USER_REQ;
2356 if ((io->io_hdr.io_type == CTL_IO_SCSI)
2357 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED))
2358 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++;
2360 retval = ctl_ioctl_submit_wait(io);
2367 memcpy((void *)addr, io, sizeof(*io));
2369 /* return this to our pool */
2374 case CTL_ENABLE_PORT:
2375 case CTL_DISABLE_PORT:
2376 case CTL_SET_PORT_WWNS: {
2377 struct ctl_port *port;
2378 struct ctl_port_entry *entry;
2380 entry = (struct ctl_port_entry *)addr;
2382 mtx_lock(&softc->ctl_lock);
2383 STAILQ_FOREACH(port, &softc->port_list, links) {
2389 if ((entry->port_type == CTL_PORT_NONE)
2390 && (entry->targ_port == port->targ_port)) {
2392 * If the user only wants to enable or
2393 * disable or set WWNs on a specific port,
2394 * do the operation and we're done.
2398 } else if (entry->port_type & port->port_type) {
2400 * Compare the user's type mask with the
2401 * particular frontend type to see if we
2408 * Make sure the user isn't trying to set
2409 * WWNs on multiple ports at the same time.
2411 if (cmd == CTL_SET_PORT_WWNS) {
2412 printf("%s: Can't set WWNs on "
2413 "multiple ports\n", __func__);
2420 * XXX KDM we have to drop the lock here,
2421 * because the online/offline operations
2422 * can potentially block. We need to
2423 * reference count the frontends so they
2426 mtx_unlock(&softc->ctl_lock);
2428 if (cmd == CTL_ENABLE_PORT) {
2429 struct ctl_lun *lun;
2431 STAILQ_FOREACH(lun, &softc->lun_list,
2433 port->lun_enable(port->targ_lun_arg,
2438 ctl_port_online(port);
2439 } else if (cmd == CTL_DISABLE_PORT) {
2440 struct ctl_lun *lun;
2442 ctl_port_offline(port);
2444 STAILQ_FOREACH(lun, &softc->lun_list,
2453 mtx_lock(&softc->ctl_lock);
2455 if (cmd == CTL_SET_PORT_WWNS)
2456 ctl_port_set_wwns(port,
2457 (entry->flags & CTL_PORT_WWNN_VALID) ?
2459 (entry->flags & CTL_PORT_WWPN_VALID) ?
2460 1 : 0, entry->wwpn);
2465 mtx_unlock(&softc->ctl_lock);
2468 case CTL_GET_PORT_LIST: {
2469 struct ctl_port *port;
2470 struct ctl_port_list *list;
2473 list = (struct ctl_port_list *)addr;
2475 if (list->alloc_len != (list->alloc_num *
2476 sizeof(struct ctl_port_entry))) {
2477 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != "
2478 "alloc_num %u * sizeof(struct ctl_port_entry) "
2479 "%zu\n", __func__, list->alloc_len,
2480 list->alloc_num, sizeof(struct ctl_port_entry));
2486 list->dropped_num = 0;
2488 mtx_lock(&softc->ctl_lock);
2489 STAILQ_FOREACH(port, &softc->port_list, links) {
2490 struct ctl_port_entry entry, *list_entry;
2492 if (list->fill_num >= list->alloc_num) {
2493 list->dropped_num++;
2497 entry.port_type = port->port_type;
2498 strlcpy(entry.port_name, port->port_name,
2499 sizeof(entry.port_name));
2500 entry.targ_port = port->targ_port;
2501 entry.physical_port = port->physical_port;
2502 entry.virtual_port = port->virtual_port;
2503 entry.wwnn = port->wwnn;
2504 entry.wwpn = port->wwpn;
2505 if (port->status & CTL_PORT_STATUS_ONLINE)
2510 list_entry = &list->entries[i];
2512 retval = copyout(&entry, list_entry, sizeof(entry));
2514 printf("%s: CTL_GET_PORT_LIST: copyout "
2515 "returned %d\n", __func__, retval);
2520 list->fill_len += sizeof(entry);
2522 mtx_unlock(&softc->ctl_lock);
2525 * If this is non-zero, we had a copyout fault, so there's
2526 * probably no point in attempting to set the status inside
2532 if (list->dropped_num > 0)
2533 list->status = CTL_PORT_LIST_NEED_MORE_SPACE;
2535 list->status = CTL_PORT_LIST_OK;
2538 case CTL_DUMP_OOA: {
2539 struct ctl_lun *lun;
2544 mtx_lock(&softc->ctl_lock);
2545 printf("Dumping OOA queues:\n");
2546 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2547 mtx_lock(&lun->lun_lock);
2548 for (io = (union ctl_io *)TAILQ_FIRST(
2549 &lun->ooa_queue); io != NULL;
2550 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2552 sbuf_new(&sb, printbuf, sizeof(printbuf),
2554 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ",
2558 CTL_FLAG_BLOCKED) ? "" : " BLOCKED",
2560 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
2562 CTL_FLAG_ABORT) ? " ABORT" : "",
2564 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : "");
2565 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
2567 printf("%s\n", sbuf_data(&sb));
2569 mtx_unlock(&lun->lun_lock);
2571 printf("OOA queues dump done\n");
2572 mtx_unlock(&softc->ctl_lock);
2576 struct ctl_lun *lun;
2577 struct ctl_ooa *ooa_hdr;
2578 struct ctl_ooa_entry *entries;
2579 uint32_t cur_fill_num;
2581 ooa_hdr = (struct ctl_ooa *)addr;
2583 if ((ooa_hdr->alloc_len == 0)
2584 || (ooa_hdr->alloc_num == 0)) {
2585 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2586 "must be non-zero\n", __func__,
2587 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2592 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2593 sizeof(struct ctl_ooa_entry))) {
2594 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2595 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2596 __func__, ooa_hdr->alloc_len,
2597 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2602 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2603 if (entries == NULL) {
2604 printf("%s: could not allocate %d bytes for OOA "
2605 "dump\n", __func__, ooa_hdr->alloc_len);
2610 mtx_lock(&softc->ctl_lock);
2611 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2612 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS)
2613 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2614 mtx_unlock(&softc->ctl_lock);
2615 free(entries, M_CTL);
2616 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2617 __func__, (uintmax_t)ooa_hdr->lun_num);
2624 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2625 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2626 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2632 mtx_unlock(&softc->ctl_lock);
2633 free(entries, M_CTL);
2637 lun = softc->ctl_luns[ooa_hdr->lun_num];
2639 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr,
2642 mtx_unlock(&softc->ctl_lock);
2644 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2645 ooa_hdr->fill_len = ooa_hdr->fill_num *
2646 sizeof(struct ctl_ooa_entry);
2647 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2649 printf("%s: error copying out %d bytes for OOA dump\n",
2650 __func__, ooa_hdr->fill_len);
2653 getbintime(&ooa_hdr->cur_bt);
2655 if (cur_fill_num > ooa_hdr->alloc_num) {
2656 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2657 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2659 ooa_hdr->dropped_num = 0;
2660 ooa_hdr->status = CTL_OOA_OK;
2663 free(entries, M_CTL);
2666 case CTL_CHECK_OOA: {
2668 struct ctl_lun *lun;
2669 struct ctl_ooa_info *ooa_info;
2672 ooa_info = (struct ctl_ooa_info *)addr;
2674 if (ooa_info->lun_id >= CTL_MAX_LUNS) {
2675 ooa_info->status = CTL_OOA_INVALID_LUN;
2678 mtx_lock(&softc->ctl_lock);
2679 lun = softc->ctl_luns[ooa_info->lun_id];
2681 mtx_unlock(&softc->ctl_lock);
2682 ooa_info->status = CTL_OOA_INVALID_LUN;
2685 mtx_lock(&lun->lun_lock);
2686 mtx_unlock(&softc->ctl_lock);
2687 ooa_info->num_entries = 0;
2688 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
2689 io != NULL; io = (union ctl_io *)TAILQ_NEXT(
2690 &io->io_hdr, ooa_links)) {
2691 ooa_info->num_entries++;
2693 mtx_unlock(&lun->lun_lock);
2695 ooa_info->status = CTL_OOA_SUCCESS;
2699 case CTL_HARD_START:
2700 case CTL_HARD_STOP: {
2701 struct ctl_fe_ioctl_startstop_info ss_info;
2702 struct cfi_metatask *metatask;
2705 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF);
2707 cv_init(&ss_info.sem, "hard start/stop cv" );
2709 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2710 if (metatask == NULL) {
2712 mtx_destroy(&hs_mtx);
2716 if (cmd == CTL_HARD_START)
2717 metatask->tasktype = CFI_TASK_STARTUP;
2719 metatask->tasktype = CFI_TASK_SHUTDOWN;
2721 metatask->callback = ctl_ioctl_hard_startstop_callback;
2722 metatask->callback_arg = &ss_info;
2724 cfi_action(metatask);
2726 /* Wait for the callback */
2728 cv_wait_sig(&ss_info.sem, &hs_mtx);
2729 mtx_unlock(&hs_mtx);
2732 * All information has been copied from the metatask by the
2733 * time cv_broadcast() is called, so we free the metatask here.
2735 cfi_free_metatask(metatask);
2737 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info));
2739 mtx_destroy(&hs_mtx);
2743 struct ctl_bbrread_info *bbr_info;
2744 struct ctl_fe_ioctl_bbrread_info fe_bbr_info;
2746 struct cfi_metatask *metatask;
2748 bbr_info = (struct ctl_bbrread_info *)addr;
2750 bzero(&fe_bbr_info, sizeof(fe_bbr_info));
2752 bzero(&bbr_mtx, sizeof(bbr_mtx));
2753 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF);
2755 fe_bbr_info.bbr_info = bbr_info;
2756 fe_bbr_info.lock = &bbr_mtx;
2758 cv_init(&fe_bbr_info.sem, "BBR read cv");
2759 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2761 if (metatask == NULL) {
2762 mtx_destroy(&bbr_mtx);
2763 cv_destroy(&fe_bbr_info.sem);
2767 metatask->tasktype = CFI_TASK_BBRREAD;
2768 metatask->callback = ctl_ioctl_bbrread_callback;
2769 metatask->callback_arg = &fe_bbr_info;
2770 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num;
2771 metatask->taskinfo.bbrread.lba = bbr_info->lba;
2772 metatask->taskinfo.bbrread.len = bbr_info->len;
2774 cfi_action(metatask);
2777 while (fe_bbr_info.wakeup_done == 0)
2778 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx);
2779 mtx_unlock(&bbr_mtx);
2781 bbr_info->status = metatask->status;
2782 bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2783 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status;
2784 memcpy(&bbr_info->sense_data,
2785 &metatask->taskinfo.bbrread.sense_data,
2786 ctl_min(sizeof(bbr_info->sense_data),
2787 sizeof(metatask->taskinfo.bbrread.sense_data)));
2789 cfi_free_metatask(metatask);
2791 mtx_destroy(&bbr_mtx);
2792 cv_destroy(&fe_bbr_info.sem);
2796 case CTL_DELAY_IO: {
2797 struct ctl_io_delay_info *delay_info;
2799 struct ctl_lun *lun;
2800 #endif /* CTL_IO_DELAY */
2802 delay_info = (struct ctl_io_delay_info *)addr;
2805 mtx_lock(&softc->ctl_lock);
2807 if ((delay_info->lun_id >= CTL_MAX_LUNS)
2808 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2809 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2811 lun = softc->ctl_luns[delay_info->lun_id];
2812 mtx_lock(&lun->lun_lock);
2814 delay_info->status = CTL_DELAY_STATUS_OK;
2816 switch (delay_info->delay_type) {
2817 case CTL_DELAY_TYPE_CONT:
2819 case CTL_DELAY_TYPE_ONESHOT:
2822 delay_info->status =
2823 CTL_DELAY_STATUS_INVALID_TYPE;
2827 switch (delay_info->delay_loc) {
2828 case CTL_DELAY_LOC_DATAMOVE:
2829 lun->delay_info.datamove_type =
2830 delay_info->delay_type;
2831 lun->delay_info.datamove_delay =
2832 delay_info->delay_secs;
2834 case CTL_DELAY_LOC_DONE:
2835 lun->delay_info.done_type =
2836 delay_info->delay_type;
2837 lun->delay_info.done_delay =
2838 delay_info->delay_secs;
2841 delay_info->status =
2842 CTL_DELAY_STATUS_INVALID_LOC;
2845 mtx_unlock(&lun->lun_lock);
2848 mtx_unlock(&softc->ctl_lock);
2850 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2851 #endif /* CTL_IO_DELAY */
2854 case CTL_REALSYNC_SET: {
2857 syncstate = (int *)addr;
2859 mtx_lock(&softc->ctl_lock);
2860 switch (*syncstate) {
2862 softc->flags &= ~CTL_FLAG_REAL_SYNC;
2865 softc->flags |= CTL_FLAG_REAL_SYNC;
2871 mtx_unlock(&softc->ctl_lock);
2874 case CTL_REALSYNC_GET: {
2877 syncstate = (int*)addr;
2879 mtx_lock(&softc->ctl_lock);
2880 if (softc->flags & CTL_FLAG_REAL_SYNC)
2884 mtx_unlock(&softc->ctl_lock);
2890 struct ctl_sync_info *sync_info;
2891 struct ctl_lun *lun;
2893 sync_info = (struct ctl_sync_info *)addr;
2895 mtx_lock(&softc->ctl_lock);
2896 lun = softc->ctl_luns[sync_info->lun_id];
2898 mtx_unlock(&softc->ctl_lock);
2899 sync_info->status = CTL_GS_SYNC_NO_LUN;
2902 * Get or set the sync interval. We're not bounds checking
2903 * in the set case, hopefully the user won't do something
2906 mtx_lock(&lun->lun_lock);
2907 mtx_unlock(&softc->ctl_lock);
2908 if (cmd == CTL_GETSYNC)
2909 sync_info->sync_interval = lun->sync_interval;
2911 lun->sync_interval = sync_info->sync_interval;
2912 mtx_unlock(&lun->lun_lock);
2914 sync_info->status = CTL_GS_SYNC_OK;
2918 case CTL_GETSTATS: {
2919 struct ctl_stats *stats;
2920 struct ctl_lun *lun;
2923 stats = (struct ctl_stats *)addr;
2925 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2927 stats->status = CTL_SS_NEED_MORE_SPACE;
2928 stats->num_luns = softc->num_luns;
2932 * XXX KDM no locking here. If the LUN list changes,
2933 * things can blow up.
2935 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
2936 i++, lun = STAILQ_NEXT(lun, links)) {
2937 retval = copyout(&lun->stats, &stats->lun_stats[i],
2938 sizeof(lun->stats));
2942 stats->num_luns = softc->num_luns;
2943 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2945 stats->status = CTL_SS_OK;
2947 stats->flags = CTL_STATS_FLAG_TIME_VALID;
2949 stats->flags = CTL_STATS_FLAG_NONE;
2951 getnanouptime(&stats->timestamp);
2954 case CTL_ERROR_INJECT: {
2955 struct ctl_error_desc *err_desc, *new_err_desc;
2956 struct ctl_lun *lun;
2958 err_desc = (struct ctl_error_desc *)addr;
2960 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2962 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2964 mtx_lock(&softc->ctl_lock);
2965 lun = softc->ctl_luns[err_desc->lun_id];
2967 mtx_unlock(&softc->ctl_lock);
2968 free(new_err_desc, M_CTL);
2969 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2970 __func__, (uintmax_t)err_desc->lun_id);
2974 mtx_lock(&lun->lun_lock);
2975 mtx_unlock(&softc->ctl_lock);
2978 * We could do some checking here to verify the validity
2979 * of the request, but given the complexity of error
2980 * injection requests, the checking logic would be fairly
2983 * For now, if the request is invalid, it just won't get
2984 * executed and might get deleted.
2986 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2989 * XXX KDM check to make sure the serial number is unique,
2990 * in case we somehow manage to wrap. That shouldn't
2991 * happen for a very long time, but it's the right thing to
2994 new_err_desc->serial = lun->error_serial;
2995 err_desc->serial = lun->error_serial;
2996 lun->error_serial++;
2998 mtx_unlock(&lun->lun_lock);
3001 case CTL_ERROR_INJECT_DELETE: {
3002 struct ctl_error_desc *delete_desc, *desc, *desc2;
3003 struct ctl_lun *lun;
3006 delete_desc = (struct ctl_error_desc *)addr;
3009 mtx_lock(&softc->ctl_lock);
3010 lun = softc->ctl_luns[delete_desc->lun_id];
3012 mtx_unlock(&softc->ctl_lock);
3013 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
3014 __func__, (uintmax_t)delete_desc->lun_id);
3018 mtx_lock(&lun->lun_lock);
3019 mtx_unlock(&softc->ctl_lock);
3020 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
3021 if (desc->serial != delete_desc->serial)
3024 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
3029 mtx_unlock(&lun->lun_lock);
3030 if (delete_done == 0) {
3031 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
3032 "error serial %ju on LUN %u\n", __func__,
3033 delete_desc->serial, delete_desc->lun_id);
3039 case CTL_DUMP_STRUCTS: {
3041 struct ctl_port *port;
3042 struct ctl_frontend *fe;
3044 mtx_lock(&softc->ctl_lock);
3045 printf("CTL Persistent Reservation information start:\n");
3046 for (i = 0; i < CTL_MAX_LUNS; i++) {
3047 struct ctl_lun *lun;
3049 lun = softc->ctl_luns[i];
3052 || ((lun->flags & CTL_LUN_DISABLED) != 0))
3055 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) {
3056 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
3057 idx = j * CTL_MAX_INIT_PER_PORT + k;
3058 if (lun->pr_keys[idx] == 0)
3060 printf(" LUN %d port %d iid %d key "
3062 (uintmax_t)lun->pr_keys[idx]);
3066 printf("CTL Persistent Reservation information end\n");
3067 printf("CTL Ports:\n");
3068 STAILQ_FOREACH(port, &softc->port_list, links) {
3069 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
3070 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
3071 port->frontend->name, port->port_type,
3072 port->physical_port, port->virtual_port,
3073 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
3074 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3075 if (port->wwpn_iid[j].in_use == 0 &&
3076 port->wwpn_iid[j].wwpn == 0 &&
3077 port->wwpn_iid[j].name == NULL)
3080 printf(" iid %u use %d WWPN %#jx '%s'\n",
3081 j, port->wwpn_iid[j].in_use,
3082 (uintmax_t)port->wwpn_iid[j].wwpn,
3083 port->wwpn_iid[j].name);
3086 printf("CTL Port information end\n");
3087 mtx_unlock(&softc->ctl_lock);
3089 * XXX KDM calling this without a lock. We'd likely want
3090 * to drop the lock before calling the frontend's dump
3093 printf("CTL Frontends:\n");
3094 STAILQ_FOREACH(fe, &softc->fe_list, links) {
3095 printf(" Frontend '%s'\n", fe->name);
3096 if (fe->fe_dump != NULL)
3099 printf("CTL Frontend information end\n");
3103 struct ctl_lun_req *lun_req;
3104 struct ctl_backend_driver *backend;
3106 lun_req = (struct ctl_lun_req *)addr;
3108 backend = ctl_backend_find(lun_req->backend);
3109 if (backend == NULL) {
3110 lun_req->status = CTL_LUN_ERROR;
3111 snprintf(lun_req->error_str,
3112 sizeof(lun_req->error_str),
3113 "Backend \"%s\" not found.",
3117 if (lun_req->num_be_args > 0) {
3118 lun_req->kern_be_args = ctl_copyin_args(
3119 lun_req->num_be_args,
3122 sizeof(lun_req->error_str));
3123 if (lun_req->kern_be_args == NULL) {
3124 lun_req->status = CTL_LUN_ERROR;
3129 retval = backend->ioctl(dev, cmd, addr, flag, td);
3131 if (lun_req->num_be_args > 0) {
3132 ctl_copyout_args(lun_req->num_be_args,
3133 lun_req->kern_be_args);
3134 ctl_free_args(lun_req->num_be_args,
3135 lun_req->kern_be_args);
3139 case CTL_LUN_LIST: {
3141 struct ctl_lun *lun;
3142 struct ctl_lun_list *list;
3143 struct ctl_option *opt;
3145 list = (struct ctl_lun_list *)addr;
3148 * Allocate a fixed length sbuf here, based on the length
3149 * of the user's buffer. We could allocate an auto-extending
3150 * buffer, and then tell the user how much larger our
3151 * amount of data is than his buffer, but that presents
3154 * 1. The sbuf(9) routines use a blocking malloc, and so
3155 * we can't hold a lock while calling them with an
3156 * auto-extending buffer.
3158 * 2. There is not currently a LUN reference counting
3159 * mechanism, outside of outstanding transactions on
3160 * the LUN's OOA queue. So a LUN could go away on us
3161 * while we're getting the LUN number, backend-specific
3162 * information, etc. Thus, given the way things
3163 * currently work, we need to hold the CTL lock while
3164 * grabbing LUN information.
3166 * So, from the user's standpoint, the best thing to do is
3167 * allocate what he thinks is a reasonable buffer length,
3168 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3169 * double the buffer length and try again. (And repeat
3170 * that until he succeeds.)
3172 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3174 list->status = CTL_LUN_LIST_ERROR;
3175 snprintf(list->error_str, sizeof(list->error_str),
3176 "Unable to allocate %d bytes for LUN list",
3181 sbuf_printf(sb, "<ctllunlist>\n");
3183 mtx_lock(&softc->ctl_lock);
3184 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3185 mtx_lock(&lun->lun_lock);
3186 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3187 (uintmax_t)lun->lun);
3190 * Bail out as soon as we see that we've overfilled
3196 retval = sbuf_printf(sb, "\t<backend_type>%s"
3197 "</backend_type>\n",
3198 (lun->backend == NULL) ? "none" :
3199 lun->backend->name);
3204 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3205 lun->be_lun->lun_type);
3210 if (lun->backend == NULL) {
3211 retval = sbuf_printf(sb, "</lun>\n");
3217 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3218 (lun->be_lun->maxlba > 0) ?
3219 lun->be_lun->maxlba + 1 : 0);
3224 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3225 lun->be_lun->blocksize);
3230 retval = sbuf_printf(sb, "\t<serial_number>");
3235 retval = ctl_sbuf_printf_esc(sb,
3236 lun->be_lun->serial_num,
3237 sizeof(lun->be_lun->serial_num));
3242 retval = sbuf_printf(sb, "</serial_number>\n");
3247 retval = sbuf_printf(sb, "\t<device_id>");
3252 retval = ctl_sbuf_printf_esc(sb,
3253 lun->be_lun->device_id,
3254 sizeof(lun->be_lun->device_id));
3259 retval = sbuf_printf(sb, "</device_id>\n");
3264 if (lun->backend->lun_info != NULL) {
3265 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3269 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3270 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3271 opt->name, opt->value, opt->name);
3276 retval = sbuf_printf(sb, "</lun>\n");
3280 mtx_unlock(&lun->lun_lock);
3283 mtx_unlock(&lun->lun_lock);
3284 mtx_unlock(&softc->ctl_lock);
3287 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3290 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3291 snprintf(list->error_str, sizeof(list->error_str),
3292 "Out of space, %d bytes is too small",
3299 retval = copyout(sbuf_data(sb), list->lun_xml,
3302 list->fill_len = sbuf_len(sb) + 1;
3303 list->status = CTL_LUN_LIST_OK;
3308 struct ctl_iscsi *ci;
3309 struct ctl_frontend *fe;
3311 ci = (struct ctl_iscsi *)addr;
3313 fe = ctl_frontend_find("iscsi");
3315 ci->status = CTL_ISCSI_ERROR;
3316 snprintf(ci->error_str, sizeof(ci->error_str),
3317 "Frontend \"iscsi\" not found.");
3321 retval = fe->ioctl(dev, cmd, addr, flag, td);
3324 case CTL_PORT_REQ: {
3325 struct ctl_req *req;
3326 struct ctl_frontend *fe;
3328 req = (struct ctl_req *)addr;
3330 fe = ctl_frontend_find(req->driver);
3332 req->status = CTL_LUN_ERROR;
3333 snprintf(req->error_str, sizeof(req->error_str),
3334 "Frontend \"%s\" not found.", req->driver);
3337 if (req->num_args > 0) {
3338 req->kern_args = ctl_copyin_args(req->num_args,
3339 req->args, req->error_str, sizeof(req->error_str));
3340 if (req->kern_args == NULL) {
3341 req->status = CTL_LUN_ERROR;
3346 retval = fe->ioctl(dev, cmd, addr, flag, td);
3348 if (req->num_args > 0) {
3349 ctl_copyout_args(req->num_args, req->kern_args);
3350 ctl_free_args(req->num_args, req->kern_args);
3354 case CTL_PORT_LIST: {
3356 struct ctl_port *port;
3357 struct ctl_lun_list *list;
3358 struct ctl_option *opt;
3361 list = (struct ctl_lun_list *)addr;
3363 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3365 list->status = CTL_LUN_LIST_ERROR;
3366 snprintf(list->error_str, sizeof(list->error_str),
3367 "Unable to allocate %d bytes for LUN list",
3372 sbuf_printf(sb, "<ctlportlist>\n");
3374 mtx_lock(&softc->ctl_lock);
3375 STAILQ_FOREACH(port, &softc->port_list, links) {
3376 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3377 (uintmax_t)port->targ_port);
3380 * Bail out as soon as we see that we've overfilled
3386 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3387 "</frontend_type>\n", port->frontend->name);
3391 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3396 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3397 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3401 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3406 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3407 port->physical_port);
3411 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3412 port->virtual_port);
3416 if (port->target_devid != NULL) {
3417 sbuf_printf(sb, "\t<target>");
3418 ctl_id_sbuf(port->target_devid, sb);
3419 sbuf_printf(sb, "</target>\n");
3422 if (port->port_devid != NULL) {
3423 sbuf_printf(sb, "\t<port>");
3424 ctl_id_sbuf(port->port_devid, sb);
3425 sbuf_printf(sb, "</port>\n");
3428 if (port->port_info != NULL) {
3429 retval = port->port_info(port->onoff_arg, sb);
3433 STAILQ_FOREACH(opt, &port->options, links) {
3434 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3435 opt->name, opt->value, opt->name);
3440 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3441 if (port->wwpn_iid[j].in_use == 0 ||
3442 (port->wwpn_iid[j].wwpn == 0 &&
3443 port->wwpn_iid[j].name == NULL))
3446 if (port->wwpn_iid[j].name != NULL)
3447 retval = sbuf_printf(sb,
3448 "\t<initiator>%u %s</initiator>\n",
3449 j, port->wwpn_iid[j].name);
3451 retval = sbuf_printf(sb,
3452 "\t<initiator>%u naa.%08jx</initiator>\n",
3453 j, port->wwpn_iid[j].wwpn);
3460 retval = sbuf_printf(sb, "</targ_port>\n");
3464 mtx_unlock(&softc->ctl_lock);
3467 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3470 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3471 snprintf(list->error_str, sizeof(list->error_str),
3472 "Out of space, %d bytes is too small",
3479 retval = copyout(sbuf_data(sb), list->lun_xml,
3482 list->fill_len = sbuf_len(sb) + 1;
3483 list->status = CTL_LUN_LIST_OK;
3488 /* XXX KDM should we fix this? */
3490 struct ctl_backend_driver *backend;
3497 * We encode the backend type as the ioctl type for backend
3498 * ioctls. So parse it out here, and then search for a
3499 * backend of this type.
3501 type = _IOC_TYPE(cmd);
3503 STAILQ_FOREACH(backend, &softc->be_list, links) {
3504 if (backend->type == type) {
3510 printf("ctl: unknown ioctl command %#lx or backend "
3515 retval = backend->ioctl(dev, cmd, addr, flag, td);
3525 ctl_get_initindex(struct ctl_nexus *nexus)
3527 if (nexus->targ_port < CTL_MAX_PORTS)
3528 return (nexus->initid.id +
3529 (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3531 return (nexus->initid.id +
3532 ((nexus->targ_port - CTL_MAX_PORTS) *
3533 CTL_MAX_INIT_PER_PORT));
3537 ctl_get_resindex(struct ctl_nexus *nexus)
3539 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3543 ctl_port_idx(int port_num)
3545 if (port_num < CTL_MAX_PORTS)
3548 return(port_num - CTL_MAX_PORTS);
3552 ctl_map_lun(int port_num, uint32_t lun_id)
3554 struct ctl_port *port;
3556 port = control_softc->ctl_ports[ctl_port_idx(port_num)];
3558 return (UINT32_MAX);
3559 if (port->lun_map == NULL)
3561 return (port->lun_map(port->targ_lun_arg, lun_id));
3565 ctl_map_lun_back(int port_num, uint32_t lun_id)
3567 struct ctl_port *port;
3570 port = control_softc->ctl_ports[ctl_port_idx(port_num)];
3571 if (port->lun_map == NULL)
3573 for (i = 0; i < CTL_MAX_LUNS; i++) {
3574 if (port->lun_map(port->targ_lun_arg, i) == lun_id)
3577 return (UINT32_MAX);
3581 * Note: This only works for bitmask sizes that are at least 32 bits, and
3582 * that are a power of 2.
3585 ctl_ffz(uint32_t *mask, uint32_t size)
3587 uint32_t num_chunks, num_pieces;
3590 num_chunks = (size >> 5);
3591 if (num_chunks == 0)
3593 num_pieces = ctl_min((sizeof(uint32_t) * 8), size);
3595 for (i = 0; i < num_chunks; i++) {
3596 for (j = 0; j < num_pieces; j++) {
3597 if ((mask[i] & (1 << j)) == 0)
3598 return ((i << 5) + j);
3606 ctl_set_mask(uint32_t *mask, uint32_t bit)
3608 uint32_t chunk, piece;
3611 piece = bit % (sizeof(uint32_t) * 8);
3613 if ((mask[chunk] & (1 << piece)) != 0)
3616 mask[chunk] |= (1 << piece);
3622 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3624 uint32_t chunk, piece;
3627 piece = bit % (sizeof(uint32_t) * 8);
3629 if ((mask[chunk] & (1 << piece)) == 0)
3632 mask[chunk] &= ~(1 << piece);
3638 ctl_is_set(uint32_t *mask, uint32_t bit)
3640 uint32_t chunk, piece;
3643 piece = bit % (sizeof(uint32_t) * 8);
3645 if ((mask[chunk] & (1 << piece)) == 0)
3653 * The bus, target and lun are optional, they can be filled in later.
3654 * can_wait is used to determine whether we can wait on the malloc or not.
3657 ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target,
3658 uint32_t targ_lun, int can_wait)
3663 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK);
3665 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT);
3668 io->io_hdr.io_type = io_type;
3669 io->io_hdr.targ_port = targ_port;
3671 * XXX KDM this needs to change/go away. We need to move
3672 * to a preallocated pool of ctl_scsiio structures.
3674 io->io_hdr.nexus.targ_target.id = targ_target;
3675 io->io_hdr.nexus.targ_lun = targ_lun;
3682 ctl_kfree_io(union ctl_io *io)
3689 * ctl_softc, pool_name, total_ctl_io are passed in.
3690 * npool is passed out.
3693 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3694 uint32_t total_ctl_io, void **npool)
3697 struct ctl_io_pool *pool;
3699 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3704 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3705 pool->ctl_softc = ctl_softc;
3706 pool->zone = uma_zsecond_create(pool->name, NULL,
3707 NULL, NULL, NULL, ctl_softc->io_zone);
3708 /* uma_prealloc(pool->zone, total_ctl_io); */
3712 *npool = ctl_softc->io_zone;
3718 ctl_pool_free(struct ctl_io_pool *pool)
3725 uma_zdestroy(pool->zone);
3731 ctl_alloc_io(void *pool_ref)
3735 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3737 io = uma_zalloc(pool->zone, M_WAITOK);
3739 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK);
3742 io->io_hdr.pool = pool_ref;
3747 ctl_alloc_io_nowait(void *pool_ref)
3751 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3753 io = uma_zalloc(pool->zone, M_NOWAIT);
3755 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT);
3758 io->io_hdr.pool = pool_ref;
3763 ctl_free_io(union ctl_io *io)
3766 struct ctl_io_pool *pool;
3773 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3774 uma_zfree(pool->zone, io);
3776 uma_zfree((uma_zone_t)io->io_hdr.pool, io);
3781 ctl_zero_io(union ctl_io *io)
3789 * May need to preserve linked list pointers at some point too.
3791 pool_ref = io->io_hdr.pool;
3792 memset(io, 0, sizeof(*io));
3793 io->io_hdr.pool = pool_ref;
3797 * This routine is currently used for internal copies of ctl_ios that need
3798 * to persist for some reason after we've already returned status to the
3799 * FETD. (Thus the flag set.)
3802 * Note that this makes a blind copy of all fields in the ctl_io, except
3803 * for the pool reference. This includes any memory that has been
3804 * allocated! That memory will no longer be valid after done has been
3805 * called, so this would be VERY DANGEROUS for command that actually does
3806 * any reads or writes. Right now (11/7/2005), this is only used for immediate
3807 * start and stop commands, which don't transfer any data, so this is not a
3808 * problem. If it is used for anything else, the caller would also need to
3809 * allocate data buffer space and this routine would need to be modified to
3810 * copy the data buffer(s) as well.
3813 ctl_copy_io(union ctl_io *src, union ctl_io *dest)
3822 * May need to preserve linked list pointers at some point too.
3824 pool_ref = dest->io_hdr.pool;
3826 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest)));
3828 dest->io_hdr.pool = pool_ref;
3830 * We need to know that this is an internal copy, and doesn't need
3831 * to get passed back to the FETD that allocated it.
3833 dest->io_hdr.flags |= CTL_FLAG_INT_COPY;
3837 ctl_expand_number(const char *buf, uint64_t *num)
3843 number = strtoq(buf, &endptr, 0);
3845 switch (tolower((unsigned char)*endptr)) {
3865 case '\0': /* No unit. */
3869 /* Unrecognized unit. */
3873 if ((number << shift) >> shift != number) {
3877 *num = number << shift;
3883 * This routine could be used in the future to load default and/or saved
3884 * mode page parameters for a particuar lun.
3887 ctl_init_page_index(struct ctl_lun *lun)
3890 struct ctl_page_index *page_index;
3894 memcpy(&lun->mode_pages.index, page_index_template,
3895 sizeof(page_index_template));
3897 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3899 page_index = &lun->mode_pages.index[i];
3901 * If this is a disk-only mode page, there's no point in
3902 * setting it up. For some pages, we have to have some
3903 * basic information about the disk in order to calculate the
3906 if ((lun->be_lun->lun_type != T_DIRECT)
3907 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
3910 switch (page_index->page_code & SMPH_PC_MASK) {
3911 case SMS_RW_ERROR_RECOVERY_PAGE: {
3912 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3913 panic("subpage is incorrect!");
3914 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
3915 &rw_er_page_default,
3916 sizeof(rw_er_page_default));
3917 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
3918 &rw_er_page_changeable,
3919 sizeof(rw_er_page_changeable));
3920 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
3921 &rw_er_page_default,
3922 sizeof(rw_er_page_default));
3923 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
3924 &rw_er_page_default,
3925 sizeof(rw_er_page_default));
3926 page_index->page_data =
3927 (uint8_t *)lun->mode_pages.rw_er_page;
3930 case SMS_FORMAT_DEVICE_PAGE: {
3931 struct scsi_format_page *format_page;
3933 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3934 panic("subpage is incorrect!");
3937 * Sectors per track are set above. Bytes per
3938 * sector need to be set here on a per-LUN basis.
3940 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
3941 &format_page_default,
3942 sizeof(format_page_default));
3943 memcpy(&lun->mode_pages.format_page[
3944 CTL_PAGE_CHANGEABLE], &format_page_changeable,
3945 sizeof(format_page_changeable));
3946 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
3947 &format_page_default,
3948 sizeof(format_page_default));
3949 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
3950 &format_page_default,
3951 sizeof(format_page_default));
3953 format_page = &lun->mode_pages.format_page[
3955 scsi_ulto2b(lun->be_lun->blocksize,
3956 format_page->bytes_per_sector);
3958 format_page = &lun->mode_pages.format_page[
3960 scsi_ulto2b(lun->be_lun->blocksize,
3961 format_page->bytes_per_sector);
3963 format_page = &lun->mode_pages.format_page[
3965 scsi_ulto2b(lun->be_lun->blocksize,
3966 format_page->bytes_per_sector);
3968 page_index->page_data =
3969 (uint8_t *)lun->mode_pages.format_page;
3972 case SMS_RIGID_DISK_PAGE: {
3973 struct scsi_rigid_disk_page *rigid_disk_page;
3974 uint32_t sectors_per_cylinder;
3978 #endif /* !__XSCALE__ */
3980 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3981 panic("invalid subpage value %d",
3982 page_index->subpage);
3985 * Rotation rate and sectors per track are set
3986 * above. We calculate the cylinders here based on
3987 * capacity. Due to the number of heads and
3988 * sectors per track we're using, smaller arrays
3989 * may turn out to have 0 cylinders. Linux and
3990 * FreeBSD don't pay attention to these mode pages
3991 * to figure out capacity, but Solaris does. It
3992 * seems to deal with 0 cylinders just fine, and
3993 * works out a fake geometry based on the capacity.
3995 memcpy(&lun->mode_pages.rigid_disk_page[
3996 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
3997 sizeof(rigid_disk_page_default));
3998 memcpy(&lun->mode_pages.rigid_disk_page[
3999 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4000 sizeof(rigid_disk_page_changeable));
4002 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4006 * The divide method here will be more accurate,
4007 * probably, but results in floating point being
4008 * used in the kernel on i386 (__udivdi3()). On the
4009 * XScale, though, __udivdi3() is implemented in
4012 * The shift method for cylinder calculation is
4013 * accurate if sectors_per_cylinder is a power of
4014 * 2. Otherwise it might be slightly off -- you
4015 * might have a bit of a truncation problem.
4018 cylinders = (lun->be_lun->maxlba + 1) /
4019 sectors_per_cylinder;
4021 for (shift = 31; shift > 0; shift--) {
4022 if (sectors_per_cylinder & (1 << shift))
4025 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4029 * We've basically got 3 bytes, or 24 bits for the
4030 * cylinder size in the mode page. If we're over,
4031 * just round down to 2^24.
4033 if (cylinders > 0xffffff)
4034 cylinders = 0xffffff;
4036 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4038 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4040 if ((value = ctl_get_opt(&lun->be_lun->options,
4042 scsi_ulto2b(strtol(value, NULL, 0),
4043 rigid_disk_page->rotation_rate);
4046 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
4047 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4048 sizeof(rigid_disk_page_default));
4049 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
4050 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4051 sizeof(rigid_disk_page_default));
4053 page_index->page_data =
4054 (uint8_t *)lun->mode_pages.rigid_disk_page;
4057 case SMS_CACHING_PAGE: {
4058 struct scsi_caching_page *caching_page;
4060 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4061 panic("invalid subpage value %d",
4062 page_index->subpage);
4063 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4064 &caching_page_default,
4065 sizeof(caching_page_default));
4066 memcpy(&lun->mode_pages.caching_page[
4067 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4068 sizeof(caching_page_changeable));
4069 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4070 &caching_page_default,
4071 sizeof(caching_page_default));
4072 caching_page = &lun->mode_pages.caching_page[
4074 value = ctl_get_opt(&lun->be_lun->options, "writecache");
4075 if (value != NULL && strcmp(value, "off") == 0)
4076 caching_page->flags1 &= ~SCP_WCE;
4077 value = ctl_get_opt(&lun->be_lun->options, "readcache");
4078 if (value != NULL && strcmp(value, "off") == 0)
4079 caching_page->flags1 |= SCP_RCD;
4080 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4081 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4082 sizeof(caching_page_default));
4083 page_index->page_data =
4084 (uint8_t *)lun->mode_pages.caching_page;
4087 case SMS_CONTROL_MODE_PAGE: {
4088 struct scsi_control_page *control_page;
4090 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4091 panic("invalid subpage value %d",
4092 page_index->subpage);
4094 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT],
4095 &control_page_default,
4096 sizeof(control_page_default));
4097 memcpy(&lun->mode_pages.control_page[
4098 CTL_PAGE_CHANGEABLE], &control_page_changeable,
4099 sizeof(control_page_changeable));
4100 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED],
4101 &control_page_default,
4102 sizeof(control_page_default));
4103 control_page = &lun->mode_pages.control_page[
4105 value = ctl_get_opt(&lun->be_lun->options, "reordering");
4106 if (value != NULL && strcmp(value, "unrestricted") == 0) {
4107 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK;
4108 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED;
4110 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT],
4111 &lun->mode_pages.control_page[CTL_PAGE_SAVED],
4112 sizeof(control_page_default));
4113 page_index->page_data =
4114 (uint8_t *)lun->mode_pages.control_page;
4118 case SMS_INFO_EXCEPTIONS_PAGE: {
4119 switch (page_index->subpage) {
4120 case SMS_SUBPAGE_PAGE_0:
4121 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4123 sizeof(ie_page_default));
4124 memcpy(&lun->mode_pages.ie_page[
4125 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4126 sizeof(ie_page_changeable));
4127 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4129 sizeof(ie_page_default));
4130 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4132 sizeof(ie_page_default));
4133 page_index->page_data =
4134 (uint8_t *)lun->mode_pages.ie_page;
4137 struct ctl_logical_block_provisioning_page *page;
4139 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4141 sizeof(lbp_page_default));
4142 memcpy(&lun->mode_pages.lbp_page[
4143 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4144 sizeof(lbp_page_changeable));
4145 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4147 sizeof(lbp_page_default));
4148 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4149 value = ctl_get_opt(&lun->be_lun->options,
4151 if (value != NULL &&
4152 ctl_expand_number(value, &ival) == 0) {
4153 page->descr[0].flags |= SLBPPD_ENABLED |
4155 if (lun->be_lun->blocksize)
4156 ival /= lun->be_lun->blocksize;
4159 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4160 page->descr[0].count);
4162 value = ctl_get_opt(&lun->be_lun->options,
4164 if (value != NULL &&
4165 ctl_expand_number(value, &ival) == 0) {
4166 page->descr[1].flags |= SLBPPD_ENABLED |
4168 if (lun->be_lun->blocksize)
4169 ival /= lun->be_lun->blocksize;
4172 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4173 page->descr[1].count);
4175 value = ctl_get_opt(&lun->be_lun->options,
4176 "pool-avail-threshold");
4177 if (value != NULL &&
4178 ctl_expand_number(value, &ival) == 0) {
4179 page->descr[2].flags |= SLBPPD_ENABLED |
4181 if (lun->be_lun->blocksize)
4182 ival /= lun->be_lun->blocksize;
4185 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4186 page->descr[2].count);
4188 value = ctl_get_opt(&lun->be_lun->options,
4189 "pool-used-threshold");
4190 if (value != NULL &&
4191 ctl_expand_number(value, &ival) == 0) {
4192 page->descr[3].flags |= SLBPPD_ENABLED |
4194 if (lun->be_lun->blocksize)
4195 ival /= lun->be_lun->blocksize;
4198 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4199 page->descr[3].count);
4201 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4202 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4203 sizeof(lbp_page_default));
4204 page_index->page_data =
4205 (uint8_t *)lun->mode_pages.lbp_page;
4209 case SMS_VENDOR_SPECIFIC_PAGE:{
4210 switch (page_index->subpage) {
4211 case DBGCNF_SUBPAGE_CODE: {
4212 struct copan_debugconf_subpage *current_page,
4215 memcpy(&lun->mode_pages.debugconf_subpage[
4217 &debugconf_page_default,
4218 sizeof(debugconf_page_default));
4219 memcpy(&lun->mode_pages.debugconf_subpage[
4220 CTL_PAGE_CHANGEABLE],
4221 &debugconf_page_changeable,
4222 sizeof(debugconf_page_changeable));
4223 memcpy(&lun->mode_pages.debugconf_subpage[
4225 &debugconf_page_default,
4226 sizeof(debugconf_page_default));
4227 memcpy(&lun->mode_pages.debugconf_subpage[
4229 &debugconf_page_default,
4230 sizeof(debugconf_page_default));
4231 page_index->page_data =
4232 (uint8_t *)lun->mode_pages.debugconf_subpage;
4234 current_page = (struct copan_debugconf_subpage *)
4235 (page_index->page_data +
4236 (page_index->page_len *
4238 saved_page = (struct copan_debugconf_subpage *)
4239 (page_index->page_data +
4240 (page_index->page_len *
4245 panic("invalid subpage value %d",
4246 page_index->subpage);
4252 panic("invalid page value %d",
4253 page_index->page_code & SMPH_PC_MASK);
4258 return (CTL_RETVAL_COMPLETE);
4262 ctl_init_log_page_index(struct ctl_lun *lun)
4264 struct ctl_page_index *page_index;
4267 memcpy(&lun->log_pages.index, log_page_index_template,
4268 sizeof(log_page_index_template));
4271 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4273 page_index = &lun->log_pages.index[i];
4275 * If this is a disk-only mode page, there's no point in
4276 * setting it up. For some pages, we have to have some
4277 * basic information about the disk in order to calculate the
4280 if ((lun->be_lun->lun_type != T_DIRECT)
4281 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
4284 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4285 ((lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) == 0 ||
4286 lun->backend->lun_attr == NULL))
4289 if (page_index->page_code != prev) {
4290 lun->log_pages.pages_page[j] = page_index->page_code;
4291 prev = page_index->page_code;
4294 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4295 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4298 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4299 lun->log_pages.index[0].page_len = j;
4300 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4301 lun->log_pages.index[1].page_len = k * 2;
4302 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4303 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4305 return (CTL_RETVAL_COMPLETE);
4309 hex2bin(const char *str, uint8_t *buf, int buf_size)
4314 memset(buf, 0, buf_size);
4315 while (isspace(str[0]))
4317 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4320 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4324 else if (isalpha(c))
4325 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4331 buf[i / 2] |= (c << 4);
4335 return ((i + 1) / 2);
4342 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4343 * wants us to allocate the LUN and he can block.
4344 * - ctl_softc is always set
4345 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4347 * Returns 0 for success, non-zero (errno) for failure.
4350 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4351 struct ctl_be_lun *const be_lun, struct ctl_id target_id)
4353 struct ctl_lun *nlun, *lun;
4354 struct ctl_port *port;
4355 struct scsi_vpd_id_descriptor *desc;
4356 struct scsi_vpd_id_t10 *t10id;
4357 const char *eui, *naa, *scsiname, *vendor, *value;
4358 int lun_number, i, lun_malloced;
4359 int devidlen, idlen1, idlen2 = 0, len;
4365 * We currently only support Direct Access or Processor LUN types.
4367 switch (be_lun->lun_type) {
4375 be_lun->lun_config_status(be_lun->be_lun,
4376 CTL_LUN_CONFIG_FAILURE);
4379 if (ctl_lun == NULL) {
4380 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4387 memset(lun, 0, sizeof(*lun));
4389 lun->flags = CTL_LUN_MALLOCED;
4391 /* Generate LUN ID. */
4392 devidlen = max(CTL_DEVID_MIN_LEN,
4393 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4394 idlen1 = sizeof(*t10id) + devidlen;
4395 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4396 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4397 if (scsiname != NULL) {
4398 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4399 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4401 eui = ctl_get_opt(&be_lun->options, "eui");
4403 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4405 naa = ctl_get_opt(&be_lun->options, "naa");
4407 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4409 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4410 M_CTL, M_WAITOK | M_ZERO);
4411 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4412 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4413 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4414 desc->length = idlen1;
4415 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4416 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4417 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4418 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4420 strncpy(t10id->vendor, vendor,
4421 min(sizeof(t10id->vendor), strlen(vendor)));
4423 strncpy((char *)t10id->vendor_spec_id,
4424 (char *)be_lun->device_id, devidlen);
4425 if (scsiname != NULL) {
4426 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4428 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4429 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4430 SVPD_ID_TYPE_SCSI_NAME;
4431 desc->length = idlen2;
4432 strlcpy(desc->identifier, scsiname, idlen2);
4435 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4437 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4438 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4440 desc->length = hex2bin(eui, desc->identifier, 16);
4441 desc->length = desc->length > 12 ? 16 :
4442 (desc->length > 8 ? 12 : 8);
4443 len -= 16 - desc->length;
4446 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4448 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4449 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4451 desc->length = hex2bin(naa, desc->identifier, 16);
4452 desc->length = desc->length > 8 ? 16 : 8;
4453 len -= 16 - desc->length;
4455 lun->lun_devid->len = len;
4457 mtx_lock(&ctl_softc->ctl_lock);
4459 * See if the caller requested a particular LUN number. If so, see
4460 * if it is available. Otherwise, allocate the first available LUN.
4462 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4463 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4464 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4465 mtx_unlock(&ctl_softc->ctl_lock);
4466 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4467 printf("ctl: requested LUN ID %d is higher "
4468 "than CTL_MAX_LUNS - 1 (%d)\n",
4469 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4472 * XXX KDM return an error, or just assign
4473 * another LUN ID in this case??
4475 printf("ctl: requested LUN ID %d is already "
4476 "in use\n", be_lun->req_lun_id);
4478 if (lun->flags & CTL_LUN_MALLOCED)
4480 be_lun->lun_config_status(be_lun->be_lun,
4481 CTL_LUN_CONFIG_FAILURE);
4484 lun_number = be_lun->req_lun_id;
4486 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS);
4487 if (lun_number == -1) {
4488 mtx_unlock(&ctl_softc->ctl_lock);
4489 printf("ctl: can't allocate LUN on target %ju, out of "
4490 "LUNs\n", (uintmax_t)target_id.id);
4491 if (lun->flags & CTL_LUN_MALLOCED)
4493 be_lun->lun_config_status(be_lun->be_lun,
4494 CTL_LUN_CONFIG_FAILURE);
4498 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4500 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4501 lun->target = target_id;
4502 lun->lun = lun_number;
4503 lun->be_lun = be_lun;
4505 * The processor LUN is always enabled. Disk LUNs come on line
4506 * disabled, and must be enabled by the backend.
4508 lun->flags |= CTL_LUN_DISABLED;
4509 lun->backend = be_lun->be;
4510 be_lun->ctl_lun = lun;
4511 be_lun->lun_id = lun_number;
4512 atomic_add_int(&be_lun->be->num_luns, 1);
4513 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE)
4514 lun->flags |= CTL_LUN_OFFLINE;
4516 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4517 lun->flags |= CTL_LUN_STOPPED;
4519 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4520 lun->flags |= CTL_LUN_INOPERABLE;
4522 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4523 lun->flags |= CTL_LUN_PRIMARY_SC;
4525 value = ctl_get_opt(&be_lun->options, "readonly");
4526 if (value != NULL && strcmp(value, "on") == 0)
4527 lun->flags |= CTL_LUN_READONLY;
4529 lun->ctl_softc = ctl_softc;
4530 TAILQ_INIT(&lun->ooa_queue);
4531 TAILQ_INIT(&lun->blocked_queue);
4532 STAILQ_INIT(&lun->error_list);
4533 ctl_tpc_lun_init(lun);
4536 * Initialize the mode and log page index.
4538 ctl_init_page_index(lun);
4539 ctl_init_log_page_index(lun);
4542 * Set the poweron UA for all initiators on this LUN only.
4544 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4545 lun->pending_ua[i] = CTL_UA_POWERON;
4548 * Now, before we insert this lun on the lun list, set the lun
4549 * inventory changed UA for all other luns.
4551 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4552 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4553 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE;
4557 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4559 ctl_softc->ctl_luns[lun_number] = lun;
4561 ctl_softc->num_luns++;
4563 /* Setup statistics gathering */
4564 lun->stats.device_type = be_lun->lun_type;
4565 lun->stats.lun_number = lun_number;
4566 if (lun->stats.device_type == T_DIRECT)
4567 lun->stats.blocksize = be_lun->blocksize;
4569 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4570 for (i = 0;i < CTL_MAX_PORTS;i++)
4571 lun->stats.ports[i].targ_port = i;
4573 mtx_unlock(&ctl_softc->ctl_lock);
4575 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4578 * Run through each registered FETD and bring it online if it isn't
4579 * already. Enable the target ID if it hasn't been enabled, and
4580 * enable this particular LUN.
4582 STAILQ_FOREACH(port, &ctl_softc->port_list, links) {
4585 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number);
4587 printf("ctl_alloc_lun: FETD %s port %d returned error "
4588 "%d for lun_enable on target %ju lun %d\n",
4589 port->port_name, port->targ_port, retval,
4590 (uintmax_t)target_id.id, lun_number);
4592 port->status |= CTL_PORT_STATUS_LUN_ONLINE;
4600 * - LUN has already been marked invalid and any pending I/O has been taken
4604 ctl_free_lun(struct ctl_lun *lun)
4606 struct ctl_softc *softc;
4608 struct ctl_port *port;
4610 struct ctl_lun *nlun;
4613 softc = lun->ctl_softc;
4615 mtx_assert(&softc->ctl_lock, MA_OWNED);
4617 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4619 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4621 softc->ctl_luns[lun->lun] = NULL;
4623 if (!TAILQ_EMPTY(&lun->ooa_queue))
4624 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4629 * XXX KDM this scheme only works for a single target/multiple LUN
4630 * setup. It needs to be revamped for a multiple target scheme.
4632 * XXX KDM this results in port->lun_disable() getting called twice,
4633 * once when ctl_disable_lun() is called, and a second time here.
4634 * We really need to re-think the LUN disable semantics. There
4635 * should probably be several steps/levels to LUN removal:
4640 * Right now we only have a disable method when communicating to
4641 * the front end ports, at least for individual LUNs.
4644 STAILQ_FOREACH(port, &softc->port_list, links) {
4647 retval = port->lun_disable(port->targ_lun_arg, lun->target,
4650 printf("ctl_free_lun: FETD %s port %d returned error "
4651 "%d for lun_disable on target %ju lun %jd\n",
4652 port->port_name, port->targ_port, retval,
4653 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4656 if (STAILQ_FIRST(&softc->lun_list) == NULL) {
4657 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE;
4659 retval = port->targ_disable(port->targ_lun_arg,lun->target);
4661 printf("ctl_free_lun: FETD %s port %d "
4662 "returned error %d for targ_disable on "
4663 "target %ju\n", port->port_name,
4664 port->targ_port, retval,
4665 (uintmax_t)lun->target.id);
4667 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE;
4669 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0)
4673 port->port_offline(port->onoff_arg);
4674 port->status &= ~CTL_PORT_STATUS_ONLINE;
4681 * Tell the backend to free resources, if this LUN has a backend.
4683 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4684 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4686 ctl_tpc_lun_shutdown(lun);
4687 mtx_destroy(&lun->lun_lock);
4688 free(lun->lun_devid, M_CTL);
4689 free(lun->write_buffer, M_CTL);
4690 if (lun->flags & CTL_LUN_MALLOCED)
4693 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4694 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4695 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE;
4703 ctl_create_lun(struct ctl_be_lun *be_lun)
4705 struct ctl_softc *ctl_softc;
4707 ctl_softc = control_softc;
4710 * ctl_alloc_lun() should handle all potential failure cases.
4712 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target);
4716 ctl_add_lun(struct ctl_be_lun *be_lun)
4718 struct ctl_softc *ctl_softc = control_softc;
4720 mtx_lock(&ctl_softc->ctl_lock);
4721 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links);
4722 mtx_unlock(&ctl_softc->ctl_lock);
4723 wakeup(&ctl_softc->pending_lun_queue);
4729 ctl_enable_lun(struct ctl_be_lun *be_lun)
4731 struct ctl_softc *ctl_softc;
4732 struct ctl_port *port, *nport;
4733 struct ctl_lun *lun;
4736 ctl_softc = control_softc;
4738 lun = (struct ctl_lun *)be_lun->ctl_lun;
4740 mtx_lock(&ctl_softc->ctl_lock);
4741 mtx_lock(&lun->lun_lock);
4742 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4744 * eh? Why did we get called if the LUN is already
4747 mtx_unlock(&lun->lun_lock);
4748 mtx_unlock(&ctl_softc->ctl_lock);
4751 lun->flags &= ~CTL_LUN_DISABLED;
4752 mtx_unlock(&lun->lun_lock);
4754 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) {
4755 nport = STAILQ_NEXT(port, links);
4758 * Drop the lock while we call the FETD's enable routine.
4759 * This can lead to a callback into CTL (at least in the
4760 * case of the internal initiator frontend.
4762 mtx_unlock(&ctl_softc->ctl_lock);
4763 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun);
4764 mtx_lock(&ctl_softc->ctl_lock);
4766 printf("%s: FETD %s port %d returned error "
4767 "%d for lun_enable on target %ju lun %jd\n",
4768 __func__, port->port_name, port->targ_port, retval,
4769 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4773 /* NOTE: TODO: why does lun enable affect port status? */
4774 port->status |= CTL_PORT_STATUS_LUN_ONLINE;
4779 mtx_unlock(&ctl_softc->ctl_lock);
4785 ctl_disable_lun(struct ctl_be_lun *be_lun)
4787 struct ctl_softc *ctl_softc;
4788 struct ctl_port *port;
4789 struct ctl_lun *lun;
4792 ctl_softc = control_softc;
4794 lun = (struct ctl_lun *)be_lun->ctl_lun;
4796 mtx_lock(&ctl_softc->ctl_lock);
4797 mtx_lock(&lun->lun_lock);
4798 if (lun->flags & CTL_LUN_DISABLED) {
4799 mtx_unlock(&lun->lun_lock);
4800 mtx_unlock(&ctl_softc->ctl_lock);
4803 lun->flags |= CTL_LUN_DISABLED;
4804 mtx_unlock(&lun->lun_lock);
4806 STAILQ_FOREACH(port, &ctl_softc->port_list, links) {
4807 mtx_unlock(&ctl_softc->ctl_lock);
4809 * Drop the lock before we call the frontend's disable
4810 * routine, to avoid lock order reversals.
4812 * XXX KDM what happens if the frontend list changes while
4813 * we're traversing it? It's unlikely, but should be handled.
4815 retval = port->lun_disable(port->targ_lun_arg, lun->target,
4817 mtx_lock(&ctl_softc->ctl_lock);
4819 printf("ctl_alloc_lun: FETD %s port %d returned error "
4820 "%d for lun_disable on target %ju lun %jd\n",
4821 port->port_name, port->targ_port, retval,
4822 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4826 mtx_unlock(&ctl_softc->ctl_lock);
4832 ctl_start_lun(struct ctl_be_lun *be_lun)
4834 struct ctl_softc *ctl_softc;
4835 struct ctl_lun *lun;
4837 ctl_softc = control_softc;
4839 lun = (struct ctl_lun *)be_lun->ctl_lun;
4841 mtx_lock(&lun->lun_lock);
4842 lun->flags &= ~CTL_LUN_STOPPED;
4843 mtx_unlock(&lun->lun_lock);
4849 ctl_stop_lun(struct ctl_be_lun *be_lun)
4851 struct ctl_softc *ctl_softc;
4852 struct ctl_lun *lun;
4854 ctl_softc = control_softc;
4856 lun = (struct ctl_lun *)be_lun->ctl_lun;
4858 mtx_lock(&lun->lun_lock);
4859 lun->flags |= CTL_LUN_STOPPED;
4860 mtx_unlock(&lun->lun_lock);
4866 ctl_lun_offline(struct ctl_be_lun *be_lun)
4868 struct ctl_softc *ctl_softc;
4869 struct ctl_lun *lun;
4871 ctl_softc = control_softc;
4873 lun = (struct ctl_lun *)be_lun->ctl_lun;
4875 mtx_lock(&lun->lun_lock);
4876 lun->flags |= CTL_LUN_OFFLINE;
4877 mtx_unlock(&lun->lun_lock);
4883 ctl_lun_online(struct ctl_be_lun *be_lun)
4885 struct ctl_softc *ctl_softc;
4886 struct ctl_lun *lun;
4888 ctl_softc = control_softc;
4890 lun = (struct ctl_lun *)be_lun->ctl_lun;
4892 mtx_lock(&lun->lun_lock);
4893 lun->flags &= ~CTL_LUN_OFFLINE;
4894 mtx_unlock(&lun->lun_lock);
4900 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4902 struct ctl_softc *ctl_softc;
4903 struct ctl_lun *lun;
4905 ctl_softc = control_softc;
4907 lun = (struct ctl_lun *)be_lun->ctl_lun;
4909 mtx_lock(&lun->lun_lock);
4912 * The LUN needs to be disabled before it can be marked invalid.
4914 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4915 mtx_unlock(&lun->lun_lock);
4919 * Mark the LUN invalid.
4921 lun->flags |= CTL_LUN_INVALID;
4924 * If there is nothing in the OOA queue, go ahead and free the LUN.
4925 * If we have something in the OOA queue, we'll free it when the
4926 * last I/O completes.
4928 if (TAILQ_EMPTY(&lun->ooa_queue)) {
4929 mtx_unlock(&lun->lun_lock);
4930 mtx_lock(&ctl_softc->ctl_lock);
4932 mtx_unlock(&ctl_softc->ctl_lock);
4934 mtx_unlock(&lun->lun_lock);
4940 ctl_lun_inoperable(struct ctl_be_lun *be_lun)
4942 struct ctl_softc *ctl_softc;
4943 struct ctl_lun *lun;
4945 ctl_softc = control_softc;
4946 lun = (struct ctl_lun *)be_lun->ctl_lun;
4948 mtx_lock(&lun->lun_lock);
4949 lun->flags |= CTL_LUN_INOPERABLE;
4950 mtx_unlock(&lun->lun_lock);
4956 ctl_lun_operable(struct ctl_be_lun *be_lun)
4958 struct ctl_softc *ctl_softc;
4959 struct ctl_lun *lun;
4961 ctl_softc = control_softc;
4962 lun = (struct ctl_lun *)be_lun->ctl_lun;
4964 mtx_lock(&lun->lun_lock);
4965 lun->flags &= ~CTL_LUN_INOPERABLE;
4966 mtx_unlock(&lun->lun_lock);
4972 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
4974 struct ctl_lun *lun;
4975 struct ctl_softc *softc;
4978 softc = control_softc;
4980 lun = (struct ctl_lun *)be_lun->ctl_lun;
4982 mtx_lock(&lun->lun_lock);
4984 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4985 lun->pending_ua[i] |= CTL_UA_CAPACITY_CHANGED;
4987 mtx_unlock(&lun->lun_lock);
4991 * Backend "memory move is complete" callback for requests that never
4992 * make it down to say RAIDCore's configuration code.
4995 ctl_config_move_done(union ctl_io *io)
4999 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
5000 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
5001 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
5003 if ((io->io_hdr.port_status != 0) &&
5004 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5005 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5007 * For hardware error sense keys, the sense key
5008 * specific value is defined to be a retry count,
5009 * but we use it to pass back an internal FETD
5010 * error code. XXX KDM Hopefully the FETD is only
5011 * using 16 bits for an error code, since that's
5012 * all the space we have in the sks field.
5014 ctl_set_internal_failure(&io->scsiio,
5017 io->io_hdr.port_status);
5020 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
5021 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5022 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
5023 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5025 * XXX KDM just assuming a single pointer here, and not a
5026 * S/G list. If we start using S/G lists for config data,
5027 * we'll need to know how to clean them up here as well.
5029 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5030 free(io->scsiio.kern_data_ptr, M_CTL);
5032 retval = CTL_RETVAL_COMPLETE;
5035 * XXX KDM now we need to continue data movement. Some
5037 * - call ctl_scsiio() again? We don't do this for data
5038 * writes, because for those at least we know ahead of
5039 * time where the write will go and how long it is. For
5040 * config writes, though, that information is largely
5041 * contained within the write itself, thus we need to
5042 * parse out the data again.
5044 * - Call some other function once the data is in?
5046 if (ctl_debug & CTL_DEBUG_CDB_DATA)
5050 * XXX KDM call ctl_scsiio() again for now, and check flag
5051 * bits to see whether we're allocated or not.
5053 retval = ctl_scsiio(&io->scsiio);
5059 * This gets called by a backend driver when it is done with a
5060 * data_submit method.
5063 ctl_data_submit_done(union ctl_io *io)
5066 * If the IO_CONT flag is set, we need to call the supplied
5067 * function to continue processing the I/O, instead of completing
5070 * If there is an error, though, we don't want to keep processing.
5071 * Instead, just send status back to the initiator.
5073 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5074 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5075 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5076 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5077 io->scsiio.io_cont(io);
5084 * This gets called by a backend driver when it is done with a
5085 * configuration write.
5088 ctl_config_write_done(union ctl_io *io)
5093 * If the IO_CONT flag is set, we need to call the supplied
5094 * function to continue processing the I/O, instead of completing
5097 * If there is an error, though, we don't want to keep processing.
5098 * Instead, just send status back to the initiator.
5100 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5101 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5102 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5103 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5104 io->scsiio.io_cont(io);
5108 * Since a configuration write can be done for commands that actually
5109 * have data allocated, like write buffer, and commands that have
5110 * no data, like start/stop unit, we need to check here.
5112 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5113 buf = io->scsiio.kern_data_ptr;
5122 * SCSI release command.
5125 ctl_scsi_release(struct ctl_scsiio *ctsio)
5127 int length, longid, thirdparty_id, resv_id;
5128 struct ctl_softc *ctl_softc;
5129 struct ctl_lun *lun;
5135 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5137 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5138 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5139 ctl_softc = control_softc;
5141 switch (ctsio->cdb[0]) {
5143 struct scsi_release_10 *cdb;
5145 cdb = (struct scsi_release_10 *)ctsio->cdb;
5147 if (cdb->byte2 & SR10_LONGID)
5150 thirdparty_id = cdb->thirdparty_id;
5152 resv_id = cdb->resv_id;
5153 length = scsi_2btoul(cdb->length);
5160 * XXX KDM right now, we only support LUN reservation. We don't
5161 * support 3rd party reservations, or extent reservations, which
5162 * might actually need the parameter list. If we've gotten this
5163 * far, we've got a LUN reservation. Anything else got kicked out
5164 * above. So, according to SPC, ignore the length.
5168 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5170 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5171 ctsio->kern_data_len = length;
5172 ctsio->kern_total_len = length;
5173 ctsio->kern_data_resid = 0;
5174 ctsio->kern_rel_offset = 0;
5175 ctsio->kern_sg_entries = 0;
5176 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5177 ctsio->be_move_done = ctl_config_move_done;
5178 ctl_datamove((union ctl_io *)ctsio);
5180 return (CTL_RETVAL_COMPLETE);
5184 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5186 mtx_lock(&lun->lun_lock);
5189 * According to SPC, it is not an error for an intiator to attempt
5190 * to release a reservation on a LUN that isn't reserved, or that
5191 * is reserved by another initiator. The reservation can only be
5192 * released, though, by the initiator who made it or by one of
5193 * several reset type events.
5195 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5196 lun->flags &= ~CTL_LUN_RESERVED;
5198 mtx_unlock(&lun->lun_lock);
5200 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5201 free(ctsio->kern_data_ptr, M_CTL);
5202 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5205 ctl_set_success(ctsio);
5206 ctl_done((union ctl_io *)ctsio);
5207 return (CTL_RETVAL_COMPLETE);
5211 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5213 int extent, thirdparty, longid;
5214 int resv_id, length;
5215 uint64_t thirdparty_id;
5216 struct ctl_softc *ctl_softc;
5217 struct ctl_lun *lun;
5227 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5229 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5230 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5231 ctl_softc = control_softc;
5233 switch (ctsio->cdb[0]) {
5235 struct scsi_reserve_10 *cdb;
5237 cdb = (struct scsi_reserve_10 *)ctsio->cdb;
5239 if (cdb->byte2 & SR10_LONGID)
5242 thirdparty_id = cdb->thirdparty_id;
5244 resv_id = cdb->resv_id;
5245 length = scsi_2btoul(cdb->length);
5251 * XXX KDM right now, we only support LUN reservation. We don't
5252 * support 3rd party reservations, or extent reservations, which
5253 * might actually need the parameter list. If we've gotten this
5254 * far, we've got a LUN reservation. Anything else got kicked out
5255 * above. So, according to SPC, ignore the length.
5259 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5261 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5262 ctsio->kern_data_len = length;
5263 ctsio->kern_total_len = length;
5264 ctsio->kern_data_resid = 0;
5265 ctsio->kern_rel_offset = 0;
5266 ctsio->kern_sg_entries = 0;
5267 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5268 ctsio->be_move_done = ctl_config_move_done;
5269 ctl_datamove((union ctl_io *)ctsio);
5271 return (CTL_RETVAL_COMPLETE);
5275 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5277 mtx_lock(&lun->lun_lock);
5278 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5279 ctl_set_reservation_conflict(ctsio);
5283 lun->flags |= CTL_LUN_RESERVED;
5284 lun->res_idx = residx;
5286 ctl_set_success(ctsio);
5289 mtx_unlock(&lun->lun_lock);
5291 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5292 free(ctsio->kern_data_ptr, M_CTL);
5293 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5296 ctl_done((union ctl_io *)ctsio);
5297 return (CTL_RETVAL_COMPLETE);
5301 ctl_start_stop(struct ctl_scsiio *ctsio)
5303 struct scsi_start_stop_unit *cdb;
5304 struct ctl_lun *lun;
5305 struct ctl_softc *ctl_softc;
5308 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5310 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5311 ctl_softc = control_softc;
5314 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5318 * We don't support the immediate bit on a stop unit. In order to
5319 * do that, we would need to code up a way to know that a stop is
5320 * pending, and hold off any new commands until it completes, one
5321 * way or another. Then we could accept or reject those commands
5322 * depending on its status. We would almost need to do the reverse
5323 * of what we do below for an immediate start -- return the copy of
5324 * the ctl_io to the FETD with status to send to the host (and to
5325 * free the copy!) and then free the original I/O once the stop
5326 * actually completes. That way, the OOA queue mechanism can work
5327 * to block commands that shouldn't proceed. Another alternative
5328 * would be to put the copy in the queue in place of the original,
5329 * and return the original back to the caller. That could be
5332 if ((cdb->byte2 & SSS_IMMED)
5333 && ((cdb->how & SSS_START) == 0)) {
5334 ctl_set_invalid_field(ctsio,
5340 ctl_done((union ctl_io *)ctsio);
5341 return (CTL_RETVAL_COMPLETE);
5344 if ((lun->flags & CTL_LUN_PR_RESERVED)
5345 && ((cdb->how & SSS_START)==0)) {
5348 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5349 if (lun->pr_keys[residx] == 0
5350 || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5352 ctl_set_reservation_conflict(ctsio);
5353 ctl_done((union ctl_io *)ctsio);
5354 return (CTL_RETVAL_COMPLETE);
5359 * If there is no backend on this device, we can't start or stop
5360 * it. In theory we shouldn't get any start/stop commands in the
5361 * first place at this level if the LUN doesn't have a backend.
5362 * That should get stopped by the command decode code.
5364 if (lun->backend == NULL) {
5365 ctl_set_invalid_opcode(ctsio);
5366 ctl_done((union ctl_io *)ctsio);
5367 return (CTL_RETVAL_COMPLETE);
5371 * XXX KDM Copan-specific offline behavior.
5372 * Figure out a reasonable way to port this?
5375 mtx_lock(&lun->lun_lock);
5377 if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
5378 && (lun->flags & CTL_LUN_OFFLINE)) {
5380 * If the LUN is offline, and the on/offline bit isn't set,
5381 * reject the start or stop. Otherwise, let it through.
5383 mtx_unlock(&lun->lun_lock);
5384 ctl_set_lun_not_ready(ctsio);
5385 ctl_done((union ctl_io *)ctsio);
5387 mtx_unlock(&lun->lun_lock);
5388 #endif /* NEEDTOPORT */
5390 * This could be a start or a stop when we're online,
5391 * or a stop/offline or start/online. A start or stop when
5392 * we're offline is covered in the case above.
5395 * In the non-immediate case, we send the request to
5396 * the backend and return status to the user when
5399 * In the immediate case, we allocate a new ctl_io
5400 * to hold a copy of the request, and send that to
5401 * the backend. We then set good status on the
5402 * user's request and return it immediately.
5404 if (cdb->byte2 & SSS_IMMED) {
5405 union ctl_io *new_io;
5407 new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5408 ctl_copy_io((union ctl_io *)ctsio, new_io);
5409 retval = lun->backend->config_write(new_io);
5410 ctl_set_success(ctsio);
5411 ctl_done((union ctl_io *)ctsio);
5413 retval = lun->backend->config_write(
5414 (union ctl_io *)ctsio);
5423 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5424 * we don't really do anything with the LBA and length fields if the user
5425 * passes them in. Instead we'll just flush out the cache for the entire
5429 ctl_sync_cache(struct ctl_scsiio *ctsio)
5431 struct ctl_lun *lun;
5432 struct ctl_softc *ctl_softc;
5433 uint64_t starting_lba;
5434 uint32_t block_count;
5437 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5439 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5440 ctl_softc = control_softc;
5443 switch (ctsio->cdb[0]) {
5444 case SYNCHRONIZE_CACHE: {
5445 struct scsi_sync_cache *cdb;
5446 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5448 starting_lba = scsi_4btoul(cdb->begin_lba);
5449 block_count = scsi_2btoul(cdb->lb_count);
5452 case SYNCHRONIZE_CACHE_16: {
5453 struct scsi_sync_cache_16 *cdb;
5454 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5456 starting_lba = scsi_8btou64(cdb->begin_lba);
5457 block_count = scsi_4btoul(cdb->lb_count);
5461 ctl_set_invalid_opcode(ctsio);
5462 ctl_done((union ctl_io *)ctsio);
5464 break; /* NOTREACHED */
5468 * We check the LBA and length, but don't do anything with them.
5469 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5470 * get flushed. This check will just help satisfy anyone who wants
5471 * to see an error for an out of range LBA.
5473 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5474 ctl_set_lba_out_of_range(ctsio);
5475 ctl_done((union ctl_io *)ctsio);
5480 * If this LUN has no backend, we can't flush the cache anyway.
5482 if (lun->backend == NULL) {
5483 ctl_set_invalid_opcode(ctsio);
5484 ctl_done((union ctl_io *)ctsio);
5489 * Check to see whether we're configured to send the SYNCHRONIZE
5490 * CACHE command directly to the back end.
5492 mtx_lock(&lun->lun_lock);
5493 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC)
5494 && (++(lun->sync_count) >= lun->sync_interval)) {
5495 lun->sync_count = 0;
5496 mtx_unlock(&lun->lun_lock);
5497 retval = lun->backend->config_write((union ctl_io *)ctsio);
5499 mtx_unlock(&lun->lun_lock);
5500 ctl_set_success(ctsio);
5501 ctl_done((union ctl_io *)ctsio);
5510 ctl_format(struct ctl_scsiio *ctsio)
5512 struct scsi_format *cdb;
5513 struct ctl_lun *lun;
5514 struct ctl_softc *ctl_softc;
5515 int length, defect_list_len;
5517 CTL_DEBUG_PRINT(("ctl_format\n"));
5519 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5520 ctl_softc = control_softc;
5522 cdb = (struct scsi_format *)ctsio->cdb;
5525 if (cdb->byte2 & SF_FMTDATA) {
5526 if (cdb->byte2 & SF_LONGLIST)
5527 length = sizeof(struct scsi_format_header_long);
5529 length = sizeof(struct scsi_format_header_short);
5532 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5534 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5535 ctsio->kern_data_len = length;
5536 ctsio->kern_total_len = length;
5537 ctsio->kern_data_resid = 0;
5538 ctsio->kern_rel_offset = 0;
5539 ctsio->kern_sg_entries = 0;
5540 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5541 ctsio->be_move_done = ctl_config_move_done;
5542 ctl_datamove((union ctl_io *)ctsio);
5544 return (CTL_RETVAL_COMPLETE);
5547 defect_list_len = 0;
5549 if (cdb->byte2 & SF_FMTDATA) {
5550 if (cdb->byte2 & SF_LONGLIST) {
5551 struct scsi_format_header_long *header;
5553 header = (struct scsi_format_header_long *)
5554 ctsio->kern_data_ptr;
5556 defect_list_len = scsi_4btoul(header->defect_list_len);
5557 if (defect_list_len != 0) {
5558 ctl_set_invalid_field(ctsio,
5567 struct scsi_format_header_short *header;
5569 header = (struct scsi_format_header_short *)
5570 ctsio->kern_data_ptr;
5572 defect_list_len = scsi_2btoul(header->defect_list_len);
5573 if (defect_list_len != 0) {
5574 ctl_set_invalid_field(ctsio,
5586 * The format command will clear out the "Medium format corrupted"
5587 * status if set by the configuration code. That status is really
5588 * just a way to notify the host that we have lost the media, and
5589 * get them to issue a command that will basically make them think
5590 * they're blowing away the media.
5592 mtx_lock(&lun->lun_lock);
5593 lun->flags &= ~CTL_LUN_INOPERABLE;
5594 mtx_unlock(&lun->lun_lock);
5596 ctl_set_success(ctsio);
5599 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5600 free(ctsio->kern_data_ptr, M_CTL);
5601 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5604 ctl_done((union ctl_io *)ctsio);
5605 return (CTL_RETVAL_COMPLETE);
5609 ctl_read_buffer(struct ctl_scsiio *ctsio)
5611 struct scsi_read_buffer *cdb;
5612 struct ctl_lun *lun;
5613 int buffer_offset, len;
5614 static uint8_t descr[4];
5615 static uint8_t echo_descr[4] = { 0 };
5617 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5619 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5620 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5622 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA &&
5623 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR &&
5624 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) {
5625 ctl_set_invalid_field(ctsio,
5631 ctl_done((union ctl_io *)ctsio);
5632 return (CTL_RETVAL_COMPLETE);
5635 len = scsi_3btoul(cdb->length);
5636 buffer_offset = scsi_3btoul(cdb->offset);
5638 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5639 ctl_set_invalid_field(ctsio,
5645 ctl_done((union ctl_io *)ctsio);
5646 return (CTL_RETVAL_COMPLETE);
5649 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5651 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5652 ctsio->kern_data_ptr = descr;
5653 len = min(len, sizeof(descr));
5654 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5655 ctsio->kern_data_ptr = echo_descr;
5656 len = min(len, sizeof(echo_descr));
5658 if (lun->write_buffer == NULL) {
5659 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5662 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5664 ctsio->kern_data_len = len;
5665 ctsio->kern_total_len = len;
5666 ctsio->kern_data_resid = 0;
5667 ctsio->kern_rel_offset = 0;
5668 ctsio->kern_sg_entries = 0;
5669 ctl_set_success(ctsio);
5670 ctsio->be_move_done = ctl_config_move_done;
5671 ctl_datamove((union ctl_io *)ctsio);
5672 return (CTL_RETVAL_COMPLETE);
5676 ctl_write_buffer(struct ctl_scsiio *ctsio)
5678 struct scsi_write_buffer *cdb;
5679 struct ctl_lun *lun;
5680 int buffer_offset, len;
5682 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5684 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5685 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5687 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5688 ctl_set_invalid_field(ctsio,
5694 ctl_done((union ctl_io *)ctsio);
5695 return (CTL_RETVAL_COMPLETE);
5698 len = scsi_3btoul(cdb->length);
5699 buffer_offset = scsi_3btoul(cdb->offset);
5701 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5702 ctl_set_invalid_field(ctsio,
5708 ctl_done((union ctl_io *)ctsio);
5709 return (CTL_RETVAL_COMPLETE);
5713 * If we've got a kernel request that hasn't been malloced yet,
5714 * malloc it and tell the caller the data buffer is here.
5716 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5717 if (lun->write_buffer == NULL) {
5718 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5721 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5722 ctsio->kern_data_len = len;
5723 ctsio->kern_total_len = len;
5724 ctsio->kern_data_resid = 0;
5725 ctsio->kern_rel_offset = 0;
5726 ctsio->kern_sg_entries = 0;
5727 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5728 ctsio->be_move_done = ctl_config_move_done;
5729 ctl_datamove((union ctl_io *)ctsio);
5731 return (CTL_RETVAL_COMPLETE);
5734 ctl_set_success(ctsio);
5735 ctl_done((union ctl_io *)ctsio);
5736 return (CTL_RETVAL_COMPLETE);
5740 ctl_write_same(struct ctl_scsiio *ctsio)
5742 struct ctl_lun *lun;
5743 struct ctl_lba_len_flags *lbalen;
5745 uint32_t num_blocks;
5749 retval = CTL_RETVAL_COMPLETE;
5751 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5753 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5755 switch (ctsio->cdb[0]) {
5756 case WRITE_SAME_10: {
5757 struct scsi_write_same_10 *cdb;
5759 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5761 lba = scsi_4btoul(cdb->addr);
5762 num_blocks = scsi_2btoul(cdb->length);
5766 case WRITE_SAME_16: {
5767 struct scsi_write_same_16 *cdb;
5769 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5771 lba = scsi_8btou64(cdb->addr);
5772 num_blocks = scsi_4btoul(cdb->length);
5778 * We got a command we don't support. This shouldn't
5779 * happen, commands should be filtered out above us.
5781 ctl_set_invalid_opcode(ctsio);
5782 ctl_done((union ctl_io *)ctsio);
5784 return (CTL_RETVAL_COMPLETE);
5785 break; /* NOTREACHED */
5788 /* NDOB and ANCHOR flags can be used only together with UNMAP */
5789 if ((byte2 & SWS_UNMAP) == 0 &&
5790 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) {
5791 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5792 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5793 ctl_done((union ctl_io *)ctsio);
5794 return (CTL_RETVAL_COMPLETE);
5798 * The first check is to make sure we're in bounds, the second
5799 * check is to catch wrap-around problems. If the lba + num blocks
5800 * is less than the lba, then we've wrapped around and the block
5801 * range is invalid anyway.
5803 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5804 || ((lba + num_blocks) < lba)) {
5805 ctl_set_lba_out_of_range(ctsio);
5806 ctl_done((union ctl_io *)ctsio);
5807 return (CTL_RETVAL_COMPLETE);
5810 /* Zero number of blocks means "to the last logical block" */
5811 if (num_blocks == 0) {
5812 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5813 ctl_set_invalid_field(ctsio,
5819 ctl_done((union ctl_io *)ctsio);
5820 return (CTL_RETVAL_COMPLETE);
5822 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5825 len = lun->be_lun->blocksize;
5828 * If we've got a kernel request that hasn't been malloced yet,
5829 * malloc it and tell the caller the data buffer is here.
5831 if ((byte2 & SWS_NDOB) == 0 &&
5832 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5833 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5834 ctsio->kern_data_len = len;
5835 ctsio->kern_total_len = len;
5836 ctsio->kern_data_resid = 0;
5837 ctsio->kern_rel_offset = 0;
5838 ctsio->kern_sg_entries = 0;
5839 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5840 ctsio->be_move_done = ctl_config_move_done;
5841 ctl_datamove((union ctl_io *)ctsio);
5843 return (CTL_RETVAL_COMPLETE);
5846 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5848 lbalen->len = num_blocks;
5849 lbalen->flags = byte2;
5850 retval = lun->backend->config_write((union ctl_io *)ctsio);
5856 ctl_unmap(struct ctl_scsiio *ctsio)
5858 struct ctl_lun *lun;
5859 struct scsi_unmap *cdb;
5860 struct ctl_ptr_len_flags *ptrlen;
5861 struct scsi_unmap_header *hdr;
5862 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5864 uint32_t num_blocks;
5868 retval = CTL_RETVAL_COMPLETE;
5870 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5872 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5873 cdb = (struct scsi_unmap *)ctsio->cdb;
5875 len = scsi_2btoul(cdb->length);
5879 * If we've got a kernel request that hasn't been malloced yet,
5880 * malloc it and tell the caller the data buffer is here.
5882 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5883 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5884 ctsio->kern_data_len = len;
5885 ctsio->kern_total_len = len;
5886 ctsio->kern_data_resid = 0;
5887 ctsio->kern_rel_offset = 0;
5888 ctsio->kern_sg_entries = 0;
5889 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5890 ctsio->be_move_done = ctl_config_move_done;
5891 ctl_datamove((union ctl_io *)ctsio);
5893 return (CTL_RETVAL_COMPLETE);
5896 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5897 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5898 if (len < sizeof (*hdr) ||
5899 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5900 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5901 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5902 ctl_set_invalid_field(ctsio,
5908 ctl_done((union ctl_io *)ctsio);
5909 return (CTL_RETVAL_COMPLETE);
5911 len = scsi_2btoul(hdr->desc_length);
5912 buf = (struct scsi_unmap_desc *)(hdr + 1);
5913 end = buf + len / sizeof(*buf);
5916 for (range = buf; range < end; range++) {
5917 lba = scsi_8btou64(range->lba);
5918 num_blocks = scsi_4btoul(range->length);
5919 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5920 || ((lba + num_blocks) < lba)) {
5921 ctl_set_lba_out_of_range(ctsio);
5922 ctl_done((union ctl_io *)ctsio);
5923 return (CTL_RETVAL_COMPLETE);
5925 if (num_blocks != 0)
5930 * Block backend can not handle zero last range.
5931 * Filter it out and return if there is nothing left.
5933 len = (uint8_t *)endnz - (uint8_t *)buf;
5935 ctl_set_success(ctsio);
5936 ctl_done((union ctl_io *)ctsio);
5937 return (CTL_RETVAL_COMPLETE);
5940 mtx_lock(&lun->lun_lock);
5941 ptrlen = (struct ctl_ptr_len_flags *)
5942 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5943 ptrlen->ptr = (void *)buf;
5945 ptrlen->flags = byte2;
5946 ctl_check_blocked(lun);
5947 mtx_unlock(&lun->lun_lock);
5949 retval = lun->backend->config_write((union ctl_io *)ctsio);
5954 * Note that this function currently doesn't actually do anything inside
5955 * CTL to enforce things if the DQue bit is turned on.
5957 * Also note that this function can't be used in the default case, because
5958 * the DQue bit isn't set in the changeable mask for the control mode page
5959 * anyway. This is just here as an example for how to implement a page
5960 * handler, and a placeholder in case we want to allow the user to turn
5961 * tagged queueing on and off.
5963 * The D_SENSE bit handling is functional, however, and will turn
5964 * descriptor sense on and off for a given LUN.
5967 ctl_control_page_handler(struct ctl_scsiio *ctsio,
5968 struct ctl_page_index *page_index, uint8_t *page_ptr)
5970 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
5971 struct ctl_lun *lun;
5972 struct ctl_softc *softc;
5976 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5977 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5980 user_cp = (struct scsi_control_page *)page_ptr;
5981 current_cp = (struct scsi_control_page *)
5982 (page_index->page_data + (page_index->page_len *
5984 saved_cp = (struct scsi_control_page *)
5985 (page_index->page_data + (page_index->page_len *
5988 softc = control_softc;
5990 mtx_lock(&lun->lun_lock);
5991 if (((current_cp->rlec & SCP_DSENSE) == 0)
5992 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
5994 * Descriptor sense is currently turned off and the user
5995 * wants to turn it on.
5997 current_cp->rlec |= SCP_DSENSE;
5998 saved_cp->rlec |= SCP_DSENSE;
5999 lun->flags |= CTL_LUN_SENSE_DESC;
6001 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
6002 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
6004 * Descriptor sense is currently turned on, and the user
6005 * wants to turn it off.
6007 current_cp->rlec &= ~SCP_DSENSE;
6008 saved_cp->rlec &= ~SCP_DSENSE;
6009 lun->flags &= ~CTL_LUN_SENSE_DESC;
6012 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) !=
6013 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) {
6014 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
6015 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
6016 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
6017 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
6020 if ((current_cp->eca_and_aen & SCP_SWP) !=
6021 (user_cp->eca_and_aen & SCP_SWP)) {
6022 current_cp->eca_and_aen &= ~SCP_SWP;
6023 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
6024 saved_cp->eca_and_aen &= ~SCP_SWP;
6025 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
6031 * Let other initiators know that the mode
6032 * parameters for this LUN have changed.
6034 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
6038 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE;
6041 mtx_unlock(&lun->lun_lock);
6047 ctl_caching_sp_handler(struct ctl_scsiio *ctsio,
6048 struct ctl_page_index *page_index, uint8_t *page_ptr)
6050 struct scsi_caching_page *current_cp, *saved_cp, *user_cp;
6051 struct ctl_lun *lun;
6055 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6056 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6059 user_cp = (struct scsi_caching_page *)page_ptr;
6060 current_cp = (struct scsi_caching_page *)
6061 (page_index->page_data + (page_index->page_len *
6063 saved_cp = (struct scsi_caching_page *)
6064 (page_index->page_data + (page_index->page_len *
6067 mtx_lock(&lun->lun_lock);
6068 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) !=
6069 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) {
6070 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
6071 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
6072 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
6073 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
6079 * Let other initiators know that the mode
6080 * parameters for this LUN have changed.
6082 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
6086 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE;
6089 mtx_unlock(&lun->lun_lock);
6095 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
6096 struct ctl_page_index *page_index,
6102 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
6107 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
6108 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
6109 printf("page data:");
6111 printf(" %.2x",page_ptr[i]);
6117 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
6118 struct ctl_page_index *page_index,
6121 struct copan_debugconf_subpage *page;
6123 page = (struct copan_debugconf_subpage *)page_index->page_data +
6124 (page_index->page_len * pc);
6127 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6128 case SMS_PAGE_CTRL_DEFAULT >> 6:
6129 case SMS_PAGE_CTRL_SAVED >> 6:
6131 * We don't update the changable or default bits for this page.
6134 case SMS_PAGE_CTRL_CURRENT >> 6:
6135 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
6136 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
6140 EPRINT(0, "Invalid PC %d!!", pc);
6141 #endif /* NEEDTOPORT */
6149 ctl_do_mode_select(union ctl_io *io)
6151 struct scsi_mode_page_header *page_header;
6152 struct ctl_page_index *page_index;
6153 struct ctl_scsiio *ctsio;
6154 int control_dev, page_len;
6155 int page_len_offset, page_len_size;
6156 union ctl_modepage_info *modepage_info;
6157 struct ctl_lun *lun;
6158 int *len_left, *len_used;
6161 ctsio = &io->scsiio;
6164 retval = CTL_RETVAL_COMPLETE;
6166 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6168 if (lun->be_lun->lun_type != T_DIRECT)
6173 modepage_info = (union ctl_modepage_info *)
6174 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6175 len_left = &modepage_info->header.len_left;
6176 len_used = &modepage_info->header.len_used;
6180 page_header = (struct scsi_mode_page_header *)
6181 (ctsio->kern_data_ptr + *len_used);
6183 if (*len_left == 0) {
6184 free(ctsio->kern_data_ptr, M_CTL);
6185 ctl_set_success(ctsio);
6186 ctl_done((union ctl_io *)ctsio);
6187 return (CTL_RETVAL_COMPLETE);
6188 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6190 free(ctsio->kern_data_ptr, M_CTL);
6191 ctl_set_param_len_error(ctsio);
6192 ctl_done((union ctl_io *)ctsio);
6193 return (CTL_RETVAL_COMPLETE);
6195 } else if ((page_header->page_code & SMPH_SPF)
6196 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6198 free(ctsio->kern_data_ptr, M_CTL);
6199 ctl_set_param_len_error(ctsio);
6200 ctl_done((union ctl_io *)ctsio);
6201 return (CTL_RETVAL_COMPLETE);
6206 * XXX KDM should we do something with the block descriptor?
6208 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6210 if ((control_dev != 0)
6211 && (lun->mode_pages.index[i].page_flags &
6212 CTL_PAGE_FLAG_DISK_ONLY))
6215 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
6216 (page_header->page_code & SMPH_PC_MASK))
6220 * If neither page has a subpage code, then we've got a
6223 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
6224 && ((page_header->page_code & SMPH_SPF) == 0)) {
6225 page_index = &lun->mode_pages.index[i];
6226 page_len = page_header->page_length;
6231 * If both pages have subpages, then the subpage numbers
6234 if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
6235 && (page_header->page_code & SMPH_SPF)) {
6236 struct scsi_mode_page_header_sp *sph;
6238 sph = (struct scsi_mode_page_header_sp *)page_header;
6240 if (lun->mode_pages.index[i].subpage ==
6242 page_index = &lun->mode_pages.index[i];
6243 page_len = scsi_2btoul(sph->page_length);
6250 * If we couldn't find the page, or if we don't have a mode select
6251 * handler for it, send back an error to the user.
6253 if ((page_index == NULL)
6254 || (page_index->select_handler == NULL)) {
6255 ctl_set_invalid_field(ctsio,
6258 /*field*/ *len_used,
6261 free(ctsio->kern_data_ptr, M_CTL);
6262 ctl_done((union ctl_io *)ctsio);
6263 return (CTL_RETVAL_COMPLETE);
6266 if (page_index->page_code & SMPH_SPF) {
6267 page_len_offset = 2;
6271 page_len_offset = 1;
6275 * If the length the initiator gives us isn't the one we specify in
6276 * the mode page header, or if they didn't specify enough data in
6277 * the CDB to avoid truncating this page, kick out the request.
6279 if ((page_len != (page_index->page_len - page_len_offset -
6281 || (*len_left < page_index->page_len)) {
6284 ctl_set_invalid_field(ctsio,
6287 /*field*/ *len_used + page_len_offset,
6290 free(ctsio->kern_data_ptr, M_CTL);
6291 ctl_done((union ctl_io *)ctsio);
6292 return (CTL_RETVAL_COMPLETE);
6296 * Run through the mode page, checking to make sure that the bits
6297 * the user changed are actually legal for him to change.
6299 for (i = 0; i < page_index->page_len; i++) {
6300 uint8_t *user_byte, *change_mask, *current_byte;
6304 user_byte = (uint8_t *)page_header + i;
6305 change_mask = page_index->page_data +
6306 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6307 current_byte = page_index->page_data +
6308 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6311 * Check to see whether the user set any bits in this byte
6312 * that he is not allowed to set.
6314 if ((*user_byte & ~(*change_mask)) ==
6315 (*current_byte & ~(*change_mask)))
6319 * Go through bit by bit to determine which one is illegal.
6322 for (j = 7; j >= 0; j--) {
6323 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6324 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6329 ctl_set_invalid_field(ctsio,
6332 /*field*/ *len_used + i,
6335 free(ctsio->kern_data_ptr, M_CTL);
6336 ctl_done((union ctl_io *)ctsio);
6337 return (CTL_RETVAL_COMPLETE);
6341 * Decrement these before we call the page handler, since we may
6342 * end up getting called back one way or another before the handler
6343 * returns to this context.
6345 *len_left -= page_index->page_len;
6346 *len_used += page_index->page_len;
6348 retval = page_index->select_handler(ctsio, page_index,
6349 (uint8_t *)page_header);
6352 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6353 * wait until this queued command completes to finish processing
6354 * the mode page. If it returns anything other than
6355 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6356 * already set the sense information, freed the data pointer, and
6357 * completed the io for us.
6359 if (retval != CTL_RETVAL_COMPLETE)
6360 goto bailout_no_done;
6363 * If the initiator sent us more than one page, parse the next one.
6368 ctl_set_success(ctsio);
6369 free(ctsio->kern_data_ptr, M_CTL);
6370 ctl_done((union ctl_io *)ctsio);
6374 return (CTL_RETVAL_COMPLETE);
6379 ctl_mode_select(struct ctl_scsiio *ctsio)
6381 int param_len, pf, sp;
6382 int header_size, bd_len;
6383 int len_left, len_used;
6384 struct ctl_page_index *page_index;
6385 struct ctl_lun *lun;
6386 int control_dev, page_len;
6387 union ctl_modepage_info *modepage_info;
6399 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6401 if (lun->be_lun->lun_type != T_DIRECT)
6406 switch (ctsio->cdb[0]) {
6407 case MODE_SELECT_6: {
6408 struct scsi_mode_select_6 *cdb;
6410 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6412 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6413 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6415 param_len = cdb->length;
6416 header_size = sizeof(struct scsi_mode_header_6);
6419 case MODE_SELECT_10: {
6420 struct scsi_mode_select_10 *cdb;
6422 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6424 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6425 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6427 param_len = scsi_2btoul(cdb->length);
6428 header_size = sizeof(struct scsi_mode_header_10);
6432 ctl_set_invalid_opcode(ctsio);
6433 ctl_done((union ctl_io *)ctsio);
6434 return (CTL_RETVAL_COMPLETE);
6435 break; /* NOTREACHED */
6440 * "A parameter list length of zero indicates that the Data-Out Buffer
6441 * shall be empty. This condition shall not be considered as an error."
6443 if (param_len == 0) {
6444 ctl_set_success(ctsio);
6445 ctl_done((union ctl_io *)ctsio);
6446 return (CTL_RETVAL_COMPLETE);
6450 * Since we'll hit this the first time through, prior to
6451 * allocation, we don't need to free a data buffer here.
6453 if (param_len < header_size) {
6454 ctl_set_param_len_error(ctsio);
6455 ctl_done((union ctl_io *)ctsio);
6456 return (CTL_RETVAL_COMPLETE);
6460 * Allocate the data buffer and grab the user's data. In theory,
6461 * we shouldn't have to sanity check the parameter list length here
6462 * because the maximum size is 64K. We should be able to malloc
6463 * that much without too many problems.
6465 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6466 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6467 ctsio->kern_data_len = param_len;
6468 ctsio->kern_total_len = param_len;
6469 ctsio->kern_data_resid = 0;
6470 ctsio->kern_rel_offset = 0;
6471 ctsio->kern_sg_entries = 0;
6472 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6473 ctsio->be_move_done = ctl_config_move_done;
6474 ctl_datamove((union ctl_io *)ctsio);
6476 return (CTL_RETVAL_COMPLETE);
6479 switch (ctsio->cdb[0]) {
6480 case MODE_SELECT_6: {
6481 struct scsi_mode_header_6 *mh6;
6483 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6484 bd_len = mh6->blk_desc_len;
6487 case MODE_SELECT_10: {
6488 struct scsi_mode_header_10 *mh10;
6490 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6491 bd_len = scsi_2btoul(mh10->blk_desc_len);
6495 panic("Invalid CDB type %#x", ctsio->cdb[0]);
6499 if (param_len < (header_size + bd_len)) {
6500 free(ctsio->kern_data_ptr, M_CTL);
6501 ctl_set_param_len_error(ctsio);
6502 ctl_done((union ctl_io *)ctsio);
6503 return (CTL_RETVAL_COMPLETE);
6507 * Set the IO_CONT flag, so that if this I/O gets passed to
6508 * ctl_config_write_done(), it'll get passed back to
6509 * ctl_do_mode_select() for further processing, or completion if
6512 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6513 ctsio->io_cont = ctl_do_mode_select;
6515 modepage_info = (union ctl_modepage_info *)
6516 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6518 memset(modepage_info, 0, sizeof(*modepage_info));
6520 len_left = param_len - header_size - bd_len;
6521 len_used = header_size + bd_len;
6523 modepage_info->header.len_left = len_left;
6524 modepage_info->header.len_used = len_used;
6526 return (ctl_do_mode_select((union ctl_io *)ctsio));
6530 ctl_mode_sense(struct ctl_scsiio *ctsio)
6532 struct ctl_lun *lun;
6533 int pc, page_code, dbd, llba, subpage;
6534 int alloc_len, page_len, header_len, total_len;
6535 struct scsi_mode_block_descr *block_desc;
6536 struct ctl_page_index *page_index;
6544 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6546 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6548 if (lun->be_lun->lun_type != T_DIRECT)
6553 switch (ctsio->cdb[0]) {
6554 case MODE_SENSE_6: {
6555 struct scsi_mode_sense_6 *cdb;
6557 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6559 header_len = sizeof(struct scsi_mode_hdr_6);
6560 if (cdb->byte2 & SMS_DBD)
6563 header_len += sizeof(struct scsi_mode_block_descr);
6565 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6566 page_code = cdb->page & SMS_PAGE_CODE;
6567 subpage = cdb->subpage;
6568 alloc_len = cdb->length;
6571 case MODE_SENSE_10: {
6572 struct scsi_mode_sense_10 *cdb;
6574 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6576 header_len = sizeof(struct scsi_mode_hdr_10);
6578 if (cdb->byte2 & SMS_DBD)
6581 header_len += sizeof(struct scsi_mode_block_descr);
6582 if (cdb->byte2 & SMS10_LLBAA)
6584 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6585 page_code = cdb->page & SMS_PAGE_CODE;
6586 subpage = cdb->subpage;
6587 alloc_len = scsi_2btoul(cdb->length);
6591 ctl_set_invalid_opcode(ctsio);
6592 ctl_done((union ctl_io *)ctsio);
6593 return (CTL_RETVAL_COMPLETE);
6594 break; /* NOTREACHED */
6598 * We have to make a first pass through to calculate the size of
6599 * the pages that match the user's query. Then we allocate enough
6600 * memory to hold it, and actually copy the data into the buffer.
6602 switch (page_code) {
6603 case SMS_ALL_PAGES_PAGE: {
6609 * At the moment, values other than 0 and 0xff here are
6610 * reserved according to SPC-3.
6612 if ((subpage != SMS_SUBPAGE_PAGE_0)
6613 && (subpage != SMS_SUBPAGE_ALL)) {
6614 ctl_set_invalid_field(ctsio,
6620 ctl_done((union ctl_io *)ctsio);
6621 return (CTL_RETVAL_COMPLETE);
6624 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6625 if ((control_dev != 0)
6626 && (lun->mode_pages.index[i].page_flags &
6627 CTL_PAGE_FLAG_DISK_ONLY))
6631 * We don't use this subpage if the user didn't
6632 * request all subpages.
6634 if ((lun->mode_pages.index[i].subpage != 0)
6635 && (subpage == SMS_SUBPAGE_PAGE_0))
6639 printf("found page %#x len %d\n",
6640 lun->mode_pages.index[i].page_code &
6642 lun->mode_pages.index[i].page_len);
6644 page_len += lun->mode_pages.index[i].page_len;
6653 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6654 /* Look for the right page code */
6655 if ((lun->mode_pages.index[i].page_code &
6656 SMPH_PC_MASK) != page_code)
6659 /* Look for the right subpage or the subpage wildcard*/
6660 if ((lun->mode_pages.index[i].subpage != subpage)
6661 && (subpage != SMS_SUBPAGE_ALL))
6664 /* Make sure the page is supported for this dev type */
6665 if ((control_dev != 0)
6666 && (lun->mode_pages.index[i].page_flags &
6667 CTL_PAGE_FLAG_DISK_ONLY))
6671 printf("found page %#x len %d\n",
6672 lun->mode_pages.index[i].page_code &
6674 lun->mode_pages.index[i].page_len);
6677 page_len += lun->mode_pages.index[i].page_len;
6680 if (page_len == 0) {
6681 ctl_set_invalid_field(ctsio,
6687 ctl_done((union ctl_io *)ctsio);
6688 return (CTL_RETVAL_COMPLETE);
6694 total_len = header_len + page_len;
6696 printf("header_len = %d, page_len = %d, total_len = %d\n",
6697 header_len, page_len, total_len);
6700 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6701 ctsio->kern_sg_entries = 0;
6702 ctsio->kern_data_resid = 0;
6703 ctsio->kern_rel_offset = 0;
6704 if (total_len < alloc_len) {
6705 ctsio->residual = alloc_len - total_len;
6706 ctsio->kern_data_len = total_len;
6707 ctsio->kern_total_len = total_len;
6709 ctsio->residual = 0;
6710 ctsio->kern_data_len = alloc_len;
6711 ctsio->kern_total_len = alloc_len;
6714 switch (ctsio->cdb[0]) {
6715 case MODE_SENSE_6: {
6716 struct scsi_mode_hdr_6 *header;
6718 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6720 header->datalen = ctl_min(total_len - 1, 254);
6721 if (control_dev == 0) {
6722 header->dev_specific = 0x10; /* DPOFUA */
6723 if ((lun->flags & CTL_LUN_READONLY) ||
6724 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6725 .eca_and_aen & SCP_SWP) != 0)
6726 header->dev_specific |= 0x80; /* WP */
6729 header->block_descr_len = 0;
6731 header->block_descr_len =
6732 sizeof(struct scsi_mode_block_descr);
6733 block_desc = (struct scsi_mode_block_descr *)&header[1];
6736 case MODE_SENSE_10: {
6737 struct scsi_mode_hdr_10 *header;
6740 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6742 datalen = ctl_min(total_len - 2, 65533);
6743 scsi_ulto2b(datalen, header->datalen);
6744 if (control_dev == 0) {
6745 header->dev_specific = 0x10; /* DPOFUA */
6746 if ((lun->flags & CTL_LUN_READONLY) ||
6747 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6748 .eca_and_aen & SCP_SWP) != 0)
6749 header->dev_specific |= 0x80; /* WP */
6752 scsi_ulto2b(0, header->block_descr_len);
6754 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6755 header->block_descr_len);
6756 block_desc = (struct scsi_mode_block_descr *)&header[1];
6760 panic("invalid CDB type %#x", ctsio->cdb[0]);
6761 break; /* NOTREACHED */
6765 * If we've got a disk, use its blocksize in the block
6766 * descriptor. Otherwise, just set it to 0.
6769 if (control_dev == 0)
6770 scsi_ulto3b(lun->be_lun->blocksize,
6771 block_desc->block_len);
6773 scsi_ulto3b(0, block_desc->block_len);
6776 switch (page_code) {
6777 case SMS_ALL_PAGES_PAGE: {
6780 data_used = header_len;
6781 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6782 struct ctl_page_index *page_index;
6784 page_index = &lun->mode_pages.index[i];
6786 if ((control_dev != 0)
6787 && (page_index->page_flags &
6788 CTL_PAGE_FLAG_DISK_ONLY))
6792 * We don't use this subpage if the user didn't
6793 * request all subpages. We already checked (above)
6794 * to make sure the user only specified a subpage
6795 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6797 if ((page_index->subpage != 0)
6798 && (subpage == SMS_SUBPAGE_PAGE_0))
6802 * Call the handler, if it exists, to update the
6803 * page to the latest values.
6805 if (page_index->sense_handler != NULL)
6806 page_index->sense_handler(ctsio, page_index,pc);
6808 memcpy(ctsio->kern_data_ptr + data_used,
6809 page_index->page_data +
6810 (page_index->page_len * pc),
6811 page_index->page_len);
6812 data_used += page_index->page_len;
6819 data_used = header_len;
6821 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6822 struct ctl_page_index *page_index;
6824 page_index = &lun->mode_pages.index[i];
6826 /* Look for the right page code */
6827 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6830 /* Look for the right subpage or the subpage wildcard*/
6831 if ((page_index->subpage != subpage)
6832 && (subpage != SMS_SUBPAGE_ALL))
6835 /* Make sure the page is supported for this dev type */
6836 if ((control_dev != 0)
6837 && (page_index->page_flags &
6838 CTL_PAGE_FLAG_DISK_ONLY))
6842 * Call the handler, if it exists, to update the
6843 * page to the latest values.
6845 if (page_index->sense_handler != NULL)
6846 page_index->sense_handler(ctsio, page_index,pc);
6848 memcpy(ctsio->kern_data_ptr + data_used,
6849 page_index->page_data +
6850 (page_index->page_len * pc),
6851 page_index->page_len);
6852 data_used += page_index->page_len;
6858 ctl_set_success(ctsio);
6859 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6860 ctsio->be_move_done = ctl_config_move_done;
6861 ctl_datamove((union ctl_io *)ctsio);
6862 return (CTL_RETVAL_COMPLETE);
6866 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6867 struct ctl_page_index *page_index,
6870 struct ctl_lun *lun;
6871 struct scsi_log_param_header *phdr;
6875 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6876 data = page_index->page_data;
6878 if (lun->backend->lun_attr != NULL &&
6879 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6881 phdr = (struct scsi_log_param_header *)data;
6882 scsi_ulto2b(0x0001, phdr->param_code);
6883 phdr->param_control = SLP_LBIN | SLP_LP;
6884 phdr->param_len = 8;
6885 data = (uint8_t *)(phdr + 1);
6886 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6887 data[4] = 0x01; /* per-LUN */
6888 data += phdr->param_len;
6891 if (lun->backend->lun_attr != NULL &&
6892 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6894 phdr = (struct scsi_log_param_header *)data;
6895 scsi_ulto2b(0x0002, phdr->param_code);
6896 phdr->param_control = SLP_LBIN | SLP_LP;
6897 phdr->param_len = 8;
6898 data = (uint8_t *)(phdr + 1);
6899 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6900 data[4] = 0x02; /* per-pool */
6901 data += phdr->param_len;
6904 if (lun->backend->lun_attr != NULL &&
6905 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6907 phdr = (struct scsi_log_param_header *)data;
6908 scsi_ulto2b(0x00f1, phdr->param_code);
6909 phdr->param_control = SLP_LBIN | SLP_LP;
6910 phdr->param_len = 8;
6911 data = (uint8_t *)(phdr + 1);
6912 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6913 data[4] = 0x02; /* per-pool */
6914 data += phdr->param_len;
6917 if (lun->backend->lun_attr != NULL &&
6918 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6920 phdr = (struct scsi_log_param_header *)data;
6921 scsi_ulto2b(0x00f2, phdr->param_code);
6922 phdr->param_control = SLP_LBIN | SLP_LP;
6923 phdr->param_len = 8;
6924 data = (uint8_t *)(phdr + 1);
6925 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6926 data[4] = 0x02; /* per-pool */
6927 data += phdr->param_len;
6930 page_index->page_len = data - page_index->page_data;
6935 ctl_log_sense(struct ctl_scsiio *ctsio)
6937 struct ctl_lun *lun;
6938 int i, pc, page_code, subpage;
6939 int alloc_len, total_len;
6940 struct ctl_page_index *page_index;
6941 struct scsi_log_sense *cdb;
6942 struct scsi_log_header *header;
6944 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6946 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6947 cdb = (struct scsi_log_sense *)ctsio->cdb;
6948 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6949 page_code = cdb->page & SLS_PAGE_CODE;
6950 subpage = cdb->subpage;
6951 alloc_len = scsi_2btoul(cdb->length);
6954 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6955 page_index = &lun->log_pages.index[i];
6957 /* Look for the right page code */
6958 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6961 /* Look for the right subpage or the subpage wildcard*/
6962 if (page_index->subpage != subpage)
6967 if (i >= CTL_NUM_LOG_PAGES) {
6968 ctl_set_invalid_field(ctsio,
6974 ctl_done((union ctl_io *)ctsio);
6975 return (CTL_RETVAL_COMPLETE);
6978 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6980 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6981 ctsio->kern_sg_entries = 0;
6982 ctsio->kern_data_resid = 0;
6983 ctsio->kern_rel_offset = 0;
6984 if (total_len < alloc_len) {
6985 ctsio->residual = alloc_len - total_len;
6986 ctsio->kern_data_len = total_len;
6987 ctsio->kern_total_len = total_len;
6989 ctsio->residual = 0;
6990 ctsio->kern_data_len = alloc_len;
6991 ctsio->kern_total_len = alloc_len;
6994 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
6995 header->page = page_index->page_code;
6996 if (page_index->subpage) {
6997 header->page |= SL_SPF;
6998 header->subpage = page_index->subpage;
7000 scsi_ulto2b(page_index->page_len, header->datalen);
7003 * Call the handler, if it exists, to update the
7004 * page to the latest values.
7006 if (page_index->sense_handler != NULL)
7007 page_index->sense_handler(ctsio, page_index, pc);
7009 memcpy(header + 1, page_index->page_data, page_index->page_len);
7011 ctl_set_success(ctsio);
7012 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7013 ctsio->be_move_done = ctl_config_move_done;
7014 ctl_datamove((union ctl_io *)ctsio);
7015 return (CTL_RETVAL_COMPLETE);
7019 ctl_read_capacity(struct ctl_scsiio *ctsio)
7021 struct scsi_read_capacity *cdb;
7022 struct scsi_read_capacity_data *data;
7023 struct ctl_lun *lun;
7026 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
7028 cdb = (struct scsi_read_capacity *)ctsio->cdb;
7030 lba = scsi_4btoul(cdb->addr);
7031 if (((cdb->pmi & SRC_PMI) == 0)
7033 ctl_set_invalid_field(/*ctsio*/ ctsio,
7039 ctl_done((union ctl_io *)ctsio);
7040 return (CTL_RETVAL_COMPLETE);
7043 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7045 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7046 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
7047 ctsio->residual = 0;
7048 ctsio->kern_data_len = sizeof(*data);
7049 ctsio->kern_total_len = sizeof(*data);
7050 ctsio->kern_data_resid = 0;
7051 ctsio->kern_rel_offset = 0;
7052 ctsio->kern_sg_entries = 0;
7055 * If the maximum LBA is greater than 0xfffffffe, the user must
7056 * issue a SERVICE ACTION IN (16) command, with the read capacity
7057 * serivce action set.
7059 if (lun->be_lun->maxlba > 0xfffffffe)
7060 scsi_ulto4b(0xffffffff, data->addr);
7062 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
7065 * XXX KDM this may not be 512 bytes...
7067 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7069 ctl_set_success(ctsio);
7070 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7071 ctsio->be_move_done = ctl_config_move_done;
7072 ctl_datamove((union ctl_io *)ctsio);
7073 return (CTL_RETVAL_COMPLETE);
7077 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
7079 struct scsi_read_capacity_16 *cdb;
7080 struct scsi_read_capacity_data_long *data;
7081 struct ctl_lun *lun;
7085 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
7087 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
7089 alloc_len = scsi_4btoul(cdb->alloc_len);
7090 lba = scsi_8btou64(cdb->addr);
7092 if ((cdb->reladr & SRC16_PMI)
7094 ctl_set_invalid_field(/*ctsio*/ ctsio,
7100 ctl_done((union ctl_io *)ctsio);
7101 return (CTL_RETVAL_COMPLETE);
7104 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7106 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7107 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
7109 if (sizeof(*data) < alloc_len) {
7110 ctsio->residual = alloc_len - sizeof(*data);
7111 ctsio->kern_data_len = sizeof(*data);
7112 ctsio->kern_total_len = sizeof(*data);
7114 ctsio->residual = 0;
7115 ctsio->kern_data_len = alloc_len;
7116 ctsio->kern_total_len = alloc_len;
7118 ctsio->kern_data_resid = 0;
7119 ctsio->kern_rel_offset = 0;
7120 ctsio->kern_sg_entries = 0;
7122 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7123 /* XXX KDM this may not be 512 bytes... */
7124 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7125 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7126 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7127 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7128 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
7130 ctl_set_success(ctsio);
7131 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7132 ctsio->be_move_done = ctl_config_move_done;
7133 ctl_datamove((union ctl_io *)ctsio);
7134 return (CTL_RETVAL_COMPLETE);
7138 ctl_read_defect(struct ctl_scsiio *ctsio)
7140 struct scsi_read_defect_data_10 *ccb10;
7141 struct scsi_read_defect_data_12 *ccb12;
7142 struct scsi_read_defect_data_hdr_10 *data10;
7143 struct scsi_read_defect_data_hdr_12 *data12;
7144 uint32_t alloc_len, data_len;
7147 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7149 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7150 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7151 format = ccb10->format;
7152 alloc_len = scsi_2btoul(ccb10->alloc_length);
7153 data_len = sizeof(*data10);
7155 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7156 format = ccb12->format;
7157 alloc_len = scsi_4btoul(ccb12->alloc_length);
7158 data_len = sizeof(*data12);
7160 if (alloc_len == 0) {
7161 ctl_set_success(ctsio);
7162 ctl_done((union ctl_io *)ctsio);
7163 return (CTL_RETVAL_COMPLETE);
7166 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7167 if (data_len < alloc_len) {
7168 ctsio->residual = alloc_len - data_len;
7169 ctsio->kern_data_len = data_len;
7170 ctsio->kern_total_len = data_len;
7172 ctsio->residual = 0;
7173 ctsio->kern_data_len = alloc_len;
7174 ctsio->kern_total_len = alloc_len;
7176 ctsio->kern_data_resid = 0;
7177 ctsio->kern_rel_offset = 0;
7178 ctsio->kern_sg_entries = 0;
7180 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7181 data10 = (struct scsi_read_defect_data_hdr_10 *)
7182 ctsio->kern_data_ptr;
7183 data10->format = format;
7184 scsi_ulto2b(0, data10->length);
7186 data12 = (struct scsi_read_defect_data_hdr_12 *)
7187 ctsio->kern_data_ptr;
7188 data12->format = format;
7189 scsi_ulto2b(0, data12->generation);
7190 scsi_ulto4b(0, data12->length);
7193 ctl_set_success(ctsio);
7194 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7195 ctsio->be_move_done = ctl_config_move_done;
7196 ctl_datamove((union ctl_io *)ctsio);
7197 return (CTL_RETVAL_COMPLETE);
7201 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7203 struct scsi_maintenance_in *cdb;
7205 int alloc_len, ext, total_len = 0, g, p, pc, pg, gs, os;
7206 int num_target_port_groups, num_target_ports;
7207 struct ctl_lun *lun;
7208 struct ctl_softc *softc;
7209 struct ctl_port *port;
7210 struct scsi_target_group_data *rtg_ptr;
7211 struct scsi_target_group_data_extended *rtg_ext_ptr;
7212 struct scsi_target_port_group_descriptor *tpg_desc;
7214 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7216 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7217 softc = control_softc;
7218 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7220 retval = CTL_RETVAL_COMPLETE;
7222 switch (cdb->byte2 & STG_PDF_MASK) {
7223 case STG_PDF_LENGTH:
7226 case STG_PDF_EXTENDED:
7230 ctl_set_invalid_field(/*ctsio*/ ctsio,
7236 ctl_done((union ctl_io *)ctsio);
7240 if (softc->is_single)
7241 num_target_port_groups = 1;
7243 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
7244 num_target_ports = 0;
7245 mtx_lock(&softc->ctl_lock);
7246 STAILQ_FOREACH(port, &softc->port_list, links) {
7247 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7249 if (ctl_map_lun_back(port->targ_port, lun->lun) >= CTL_MAX_LUNS)
7253 mtx_unlock(&softc->ctl_lock);
7256 total_len = sizeof(struct scsi_target_group_data_extended);
7258 total_len = sizeof(struct scsi_target_group_data);
7259 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7260 num_target_port_groups +
7261 sizeof(struct scsi_target_port_descriptor) *
7262 num_target_ports * num_target_port_groups;
7264 alloc_len = scsi_4btoul(cdb->length);
7266 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7268 ctsio->kern_sg_entries = 0;
7270 if (total_len < alloc_len) {
7271 ctsio->residual = alloc_len - total_len;
7272 ctsio->kern_data_len = total_len;
7273 ctsio->kern_total_len = total_len;
7275 ctsio->residual = 0;
7276 ctsio->kern_data_len = alloc_len;
7277 ctsio->kern_total_len = alloc_len;
7279 ctsio->kern_data_resid = 0;
7280 ctsio->kern_rel_offset = 0;
7283 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7284 ctsio->kern_data_ptr;
7285 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7286 rtg_ext_ptr->format_type = 0x10;
7287 rtg_ext_ptr->implicit_transition_time = 0;
7288 tpg_desc = &rtg_ext_ptr->groups[0];
7290 rtg_ptr = (struct scsi_target_group_data *)
7291 ctsio->kern_data_ptr;
7292 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7293 tpg_desc = &rtg_ptr->groups[0];
7296 mtx_lock(&softc->ctl_lock);
7297 pg = softc->port_offset / CTL_MAX_PORTS;
7298 if (softc->flags & CTL_FLAG_ACTIVE_SHELF) {
7299 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) {
7300 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7301 os = TPG_ASYMMETRIC_ACCESS_STANDBY;
7302 } else if (lun->flags & CTL_LUN_PRIMARY_SC) {
7303 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7304 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7306 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7307 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7310 gs = TPG_ASYMMETRIC_ACCESS_STANDBY;
7311 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7313 for (g = 0; g < num_target_port_groups; g++) {
7314 tpg_desc->pref_state = (g == pg) ? gs : os;
7315 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP;
7316 scsi_ulto2b(g + 1, tpg_desc->target_port_group);
7317 tpg_desc->status = TPG_IMPLICIT;
7319 STAILQ_FOREACH(port, &softc->port_list, links) {
7320 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7322 if (ctl_map_lun_back(port->targ_port, lun->lun) >=
7325 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS;
7326 scsi_ulto2b(p, tpg_desc->descriptors[pc].
7327 relative_target_port_identifier);
7330 tpg_desc->target_port_count = pc;
7331 tpg_desc = (struct scsi_target_port_group_descriptor *)
7332 &tpg_desc->descriptors[pc];
7334 mtx_unlock(&softc->ctl_lock);
7336 ctl_set_success(ctsio);
7337 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7338 ctsio->be_move_done = ctl_config_move_done;
7339 ctl_datamove((union ctl_io *)ctsio);
7344 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7346 struct ctl_lun *lun;
7347 struct scsi_report_supported_opcodes *cdb;
7348 const struct ctl_cmd_entry *entry, *sentry;
7349 struct scsi_report_supported_opcodes_all *all;
7350 struct scsi_report_supported_opcodes_descr *descr;
7351 struct scsi_report_supported_opcodes_one *one;
7353 int alloc_len, total_len;
7354 int opcode, service_action, i, j, num;
7356 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7358 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7359 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7361 retval = CTL_RETVAL_COMPLETE;
7363 opcode = cdb->requested_opcode;
7364 service_action = scsi_2btoul(cdb->requested_service_action);
7365 switch (cdb->options & RSO_OPTIONS_MASK) {
7366 case RSO_OPTIONS_ALL:
7368 for (i = 0; i < 256; i++) {
7369 entry = &ctl_cmd_table[i];
7370 if (entry->flags & CTL_CMD_FLAG_SA5) {
7371 for (j = 0; j < 32; j++) {
7372 sentry = &((const struct ctl_cmd_entry *)
7374 if (ctl_cmd_applicable(
7375 lun->be_lun->lun_type, sentry))
7379 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7384 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7385 num * sizeof(struct scsi_report_supported_opcodes_descr);
7387 case RSO_OPTIONS_OC:
7388 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7389 ctl_set_invalid_field(/*ctsio*/ ctsio,
7395 ctl_done((union ctl_io *)ctsio);
7396 return (CTL_RETVAL_COMPLETE);
7398 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7400 case RSO_OPTIONS_OC_SA:
7401 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7402 service_action >= 32) {
7403 ctl_set_invalid_field(/*ctsio*/ ctsio,
7409 ctl_done((union ctl_io *)ctsio);
7410 return (CTL_RETVAL_COMPLETE);
7412 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7415 ctl_set_invalid_field(/*ctsio*/ ctsio,
7421 ctl_done((union ctl_io *)ctsio);
7422 return (CTL_RETVAL_COMPLETE);
7425 alloc_len = scsi_4btoul(cdb->length);
7427 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7429 ctsio->kern_sg_entries = 0;
7431 if (total_len < alloc_len) {
7432 ctsio->residual = alloc_len - total_len;
7433 ctsio->kern_data_len = total_len;
7434 ctsio->kern_total_len = total_len;
7436 ctsio->residual = 0;
7437 ctsio->kern_data_len = alloc_len;
7438 ctsio->kern_total_len = alloc_len;
7440 ctsio->kern_data_resid = 0;
7441 ctsio->kern_rel_offset = 0;
7443 switch (cdb->options & RSO_OPTIONS_MASK) {
7444 case RSO_OPTIONS_ALL:
7445 all = (struct scsi_report_supported_opcodes_all *)
7446 ctsio->kern_data_ptr;
7448 for (i = 0; i < 256; i++) {
7449 entry = &ctl_cmd_table[i];
7450 if (entry->flags & CTL_CMD_FLAG_SA5) {
7451 for (j = 0; j < 32; j++) {
7452 sentry = &((const struct ctl_cmd_entry *)
7454 if (!ctl_cmd_applicable(
7455 lun->be_lun->lun_type, sentry))
7457 descr = &all->descr[num++];
7459 scsi_ulto2b(j, descr->service_action);
7460 descr->flags = RSO_SERVACTV;
7461 scsi_ulto2b(sentry->length,
7465 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7468 descr = &all->descr[num++];
7470 scsi_ulto2b(0, descr->service_action);
7472 scsi_ulto2b(entry->length, descr->cdb_length);
7476 num * sizeof(struct scsi_report_supported_opcodes_descr),
7479 case RSO_OPTIONS_OC:
7480 one = (struct scsi_report_supported_opcodes_one *)
7481 ctsio->kern_data_ptr;
7482 entry = &ctl_cmd_table[opcode];
7484 case RSO_OPTIONS_OC_SA:
7485 one = (struct scsi_report_supported_opcodes_one *)
7486 ctsio->kern_data_ptr;
7487 entry = &ctl_cmd_table[opcode];
7488 entry = &((const struct ctl_cmd_entry *)
7489 entry->execute)[service_action];
7491 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7493 scsi_ulto2b(entry->length, one->cdb_length);
7494 one->cdb_usage[0] = opcode;
7495 memcpy(&one->cdb_usage[1], entry->usage,
7502 ctl_set_success(ctsio);
7503 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7504 ctsio->be_move_done = ctl_config_move_done;
7505 ctl_datamove((union ctl_io *)ctsio);
7510 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7512 struct scsi_report_supported_tmf *cdb;
7513 struct scsi_report_supported_tmf_data *data;
7515 int alloc_len, total_len;
7517 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7519 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7521 retval = CTL_RETVAL_COMPLETE;
7523 total_len = sizeof(struct scsi_report_supported_tmf_data);
7524 alloc_len = scsi_4btoul(cdb->length);
7526 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7528 ctsio->kern_sg_entries = 0;
7530 if (total_len < alloc_len) {
7531 ctsio->residual = alloc_len - total_len;
7532 ctsio->kern_data_len = total_len;
7533 ctsio->kern_total_len = total_len;
7535 ctsio->residual = 0;
7536 ctsio->kern_data_len = alloc_len;
7537 ctsio->kern_total_len = alloc_len;
7539 ctsio->kern_data_resid = 0;
7540 ctsio->kern_rel_offset = 0;
7542 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr;
7543 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS;
7544 data->byte2 |= RST_ITNRS;
7546 ctl_set_success(ctsio);
7547 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7548 ctsio->be_move_done = ctl_config_move_done;
7549 ctl_datamove((union ctl_io *)ctsio);
7554 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7556 struct scsi_report_timestamp *cdb;
7557 struct scsi_report_timestamp_data *data;
7561 int alloc_len, total_len;
7563 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7565 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7567 retval = CTL_RETVAL_COMPLETE;
7569 total_len = sizeof(struct scsi_report_timestamp_data);
7570 alloc_len = scsi_4btoul(cdb->length);
7572 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7574 ctsio->kern_sg_entries = 0;
7576 if (total_len < alloc_len) {
7577 ctsio->residual = alloc_len - total_len;
7578 ctsio->kern_data_len = total_len;
7579 ctsio->kern_total_len = total_len;
7581 ctsio->residual = 0;
7582 ctsio->kern_data_len = alloc_len;
7583 ctsio->kern_total_len = alloc_len;
7585 ctsio->kern_data_resid = 0;
7586 ctsio->kern_rel_offset = 0;
7588 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7589 scsi_ulto2b(sizeof(*data) - 2, data->length);
7590 data->origin = RTS_ORIG_OUTSIDE;
7592 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7593 scsi_ulto4b(timestamp >> 16, data->timestamp);
7594 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7596 ctl_set_success(ctsio);
7597 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7598 ctsio->be_move_done = ctl_config_move_done;
7599 ctl_datamove((union ctl_io *)ctsio);
7604 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7606 struct scsi_per_res_in *cdb;
7607 int alloc_len, total_len = 0;
7608 /* struct scsi_per_res_in_rsrv in_data; */
7609 struct ctl_lun *lun;
7610 struct ctl_softc *softc;
7612 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7614 softc = control_softc;
7616 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7618 alloc_len = scsi_2btoul(cdb->length);
7620 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7623 mtx_lock(&lun->lun_lock);
7624 switch (cdb->action) {
7625 case SPRI_RK: /* read keys */
7626 total_len = sizeof(struct scsi_per_res_in_keys) +
7628 sizeof(struct scsi_per_res_key);
7630 case SPRI_RR: /* read reservation */
7631 if (lun->flags & CTL_LUN_PR_RESERVED)
7632 total_len = sizeof(struct scsi_per_res_in_rsrv);
7634 total_len = sizeof(struct scsi_per_res_in_header);
7636 case SPRI_RC: /* report capabilities */
7637 total_len = sizeof(struct scsi_per_res_cap);
7639 case SPRI_RS: /* read full status */
7640 total_len = sizeof(struct scsi_per_res_in_header) +
7641 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7645 panic("Invalid PR type %x", cdb->action);
7647 mtx_unlock(&lun->lun_lock);
7649 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7651 if (total_len < alloc_len) {
7652 ctsio->residual = alloc_len - total_len;
7653 ctsio->kern_data_len = total_len;
7654 ctsio->kern_total_len = total_len;
7656 ctsio->residual = 0;
7657 ctsio->kern_data_len = alloc_len;
7658 ctsio->kern_total_len = alloc_len;
7661 ctsio->kern_data_resid = 0;
7662 ctsio->kern_rel_offset = 0;
7663 ctsio->kern_sg_entries = 0;
7665 mtx_lock(&lun->lun_lock);
7666 switch (cdb->action) {
7667 case SPRI_RK: { // read keys
7668 struct scsi_per_res_in_keys *res_keys;
7671 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7674 * We had to drop the lock to allocate our buffer, which
7675 * leaves time for someone to come in with another
7676 * persistent reservation. (That is unlikely, though,
7677 * since this should be the only persistent reservation
7678 * command active right now.)
7680 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7681 (lun->pr_key_count *
7682 sizeof(struct scsi_per_res_key)))){
7683 mtx_unlock(&lun->lun_lock);
7684 free(ctsio->kern_data_ptr, M_CTL);
7685 printf("%s: reservation length changed, retrying\n",
7690 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7692 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7693 lun->pr_key_count, res_keys->header.length);
7695 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7696 if (lun->pr_keys[i] == 0)
7700 * We used lun->pr_key_count to calculate the
7701 * size to allocate. If it turns out the number of
7702 * initiators with the registered flag set is
7703 * larger than that (i.e. they haven't been kept in
7704 * sync), we've got a problem.
7706 if (key_count >= lun->pr_key_count) {
7708 csevent_log(CSC_CTL | CSC_SHELF_SW |
7710 csevent_LogType_Fault,
7711 csevent_AlertLevel_Yellow,
7712 csevent_FRU_ShelfController,
7713 csevent_FRU_Firmware,
7714 csevent_FRU_Unknown,
7715 "registered keys %d >= key "
7716 "count %d", key_count,
7722 scsi_u64to8b(lun->pr_keys[i],
7723 res_keys->keys[key_count].key);
7728 case SPRI_RR: { // read reservation
7729 struct scsi_per_res_in_rsrv *res;
7730 int tmp_len, header_only;
7732 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7734 scsi_ulto4b(lun->PRGeneration, res->header.generation);
7736 if (lun->flags & CTL_LUN_PR_RESERVED)
7738 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7739 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7740 res->header.length);
7743 tmp_len = sizeof(struct scsi_per_res_in_header);
7744 scsi_ulto4b(0, res->header.length);
7749 * We had to drop the lock to allocate our buffer, which
7750 * leaves time for someone to come in with another
7751 * persistent reservation. (That is unlikely, though,
7752 * since this should be the only persistent reservation
7753 * command active right now.)
7755 if (tmp_len != total_len) {
7756 mtx_unlock(&lun->lun_lock);
7757 free(ctsio->kern_data_ptr, M_CTL);
7758 printf("%s: reservation status changed, retrying\n",
7764 * No reservation held, so we're done.
7766 if (header_only != 0)
7770 * If the registration is an All Registrants type, the key
7771 * is 0, since it doesn't really matter.
7773 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7774 scsi_u64to8b(lun->pr_keys[lun->pr_res_idx],
7775 res->data.reservation);
7777 res->data.scopetype = lun->res_type;
7780 case SPRI_RC: //report capabilities
7782 struct scsi_per_res_cap *res_cap;
7785 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7786 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7787 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5;
7788 type_mask = SPRI_TM_WR_EX_AR |
7794 scsi_ulto2b(type_mask, res_cap->type_mask);
7797 case SPRI_RS: { // read full status
7798 struct scsi_per_res_in_full *res_status;
7799 struct scsi_per_res_in_full_desc *res_desc;
7800 struct ctl_port *port;
7803 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7806 * We had to drop the lock to allocate our buffer, which
7807 * leaves time for someone to come in with another
7808 * persistent reservation. (That is unlikely, though,
7809 * since this should be the only persistent reservation
7810 * command active right now.)
7812 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7813 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7814 lun->pr_key_count)){
7815 mtx_unlock(&lun->lun_lock);
7816 free(ctsio->kern_data_ptr, M_CTL);
7817 printf("%s: reservation length changed, retrying\n",
7822 scsi_ulto4b(lun->PRGeneration, res_status->header.generation);
7824 res_desc = &res_status->desc[0];
7825 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7826 if (lun->pr_keys[i] == 0)
7829 scsi_u64to8b(lun->pr_keys[i], res_desc->res_key.key);
7830 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7831 (lun->pr_res_idx == i ||
7832 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7833 res_desc->flags = SPRI_FULL_R_HOLDER;
7834 res_desc->scopetype = lun->res_type;
7836 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7837 res_desc->rel_trgt_port_id);
7839 port = softc->ctl_ports[
7840 ctl_port_idx(i / CTL_MAX_INIT_PER_PORT)];
7842 len = ctl_create_iid(port,
7843 i % CTL_MAX_INIT_PER_PORT,
7844 res_desc->transport_id);
7845 scsi_ulto4b(len, res_desc->additional_length);
7846 res_desc = (struct scsi_per_res_in_full_desc *)
7847 &res_desc->transport_id[len];
7849 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7850 res_status->header.length);
7855 * This is a bug, because we just checked for this above,
7856 * and should have returned an error.
7858 panic("Invalid PR type %x", cdb->action);
7859 break; /* NOTREACHED */
7861 mtx_unlock(&lun->lun_lock);
7863 ctl_set_success(ctsio);
7864 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7865 ctsio->be_move_done = ctl_config_move_done;
7866 ctl_datamove((union ctl_io *)ctsio);
7867 return (CTL_RETVAL_COMPLETE);
7871 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7875 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7876 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7877 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7878 struct scsi_per_res_out_parms* param)
7880 union ctl_ha_msg persis_io;
7886 mtx_lock(&lun->lun_lock);
7887 if (sa_res_key == 0) {
7888 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7889 /* validate scope and type */
7890 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7892 mtx_unlock(&lun->lun_lock);
7893 ctl_set_invalid_field(/*ctsio*/ ctsio,
7899 ctl_done((union ctl_io *)ctsio);
7903 if (type>8 || type==2 || type==4 || type==0) {
7904 mtx_unlock(&lun->lun_lock);
7905 ctl_set_invalid_field(/*ctsio*/ ctsio,
7911 ctl_done((union ctl_io *)ctsio);
7916 * Unregister everybody else and build UA for
7919 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7920 if (i == residx || lun->pr_keys[i] == 0)
7924 && i <CTL_MAX_INITIATORS)
7925 lun->pending_ua[i] |=
7927 else if (persis_offset
7928 && i >= persis_offset)
7929 lun->pending_ua[i-persis_offset] |=
7931 lun->pr_keys[i] = 0;
7933 lun->pr_key_count = 1;
7934 lun->res_type = type;
7935 if (lun->res_type != SPR_TYPE_WR_EX_AR
7936 && lun->res_type != SPR_TYPE_EX_AC_AR)
7937 lun->pr_res_idx = residx;
7939 /* send msg to other side */
7940 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7941 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7942 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7943 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7944 persis_io.pr.pr_info.res_type = type;
7945 memcpy(persis_io.pr.pr_info.sa_res_key,
7946 param->serv_act_res_key,
7947 sizeof(param->serv_act_res_key));
7948 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7949 &persis_io, sizeof(persis_io), 0)) >
7950 CTL_HA_STATUS_SUCCESS) {
7951 printf("CTL:Persis Out error returned "
7952 "from ctl_ha_msg_send %d\n",
7956 /* not all registrants */
7957 mtx_unlock(&lun->lun_lock);
7958 free(ctsio->kern_data_ptr, M_CTL);
7959 ctl_set_invalid_field(ctsio,
7965 ctl_done((union ctl_io *)ctsio);
7968 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7969 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7972 if (res_key == sa_res_key) {
7975 * The spec implies this is not good but doesn't
7976 * say what to do. There are two choices either
7977 * generate a res conflict or check condition
7978 * with illegal field in parameter data. Since
7979 * that is what is done when the sa_res_key is
7980 * zero I'll take that approach since this has
7981 * to do with the sa_res_key.
7983 mtx_unlock(&lun->lun_lock);
7984 free(ctsio->kern_data_ptr, M_CTL);
7985 ctl_set_invalid_field(ctsio,
7991 ctl_done((union ctl_io *)ctsio);
7995 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7996 if (lun->pr_keys[i] != sa_res_key)
8000 lun->pr_keys[i] = 0;
8001 lun->pr_key_count--;
8003 if (!persis_offset && i < CTL_MAX_INITIATORS)
8004 lun->pending_ua[i] |= CTL_UA_REG_PREEMPT;
8005 else if (persis_offset && i >= persis_offset)
8006 lun->pending_ua[i-persis_offset] |=
8010 mtx_unlock(&lun->lun_lock);
8011 free(ctsio->kern_data_ptr, M_CTL);
8012 ctl_set_reservation_conflict(ctsio);
8013 ctl_done((union ctl_io *)ctsio);
8014 return (CTL_RETVAL_COMPLETE);
8016 /* send msg to other side */
8017 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8018 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8019 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8020 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8021 persis_io.pr.pr_info.res_type = type;
8022 memcpy(persis_io.pr.pr_info.sa_res_key,
8023 param->serv_act_res_key,
8024 sizeof(param->serv_act_res_key));
8025 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8026 &persis_io, sizeof(persis_io), 0)) >
8027 CTL_HA_STATUS_SUCCESS) {
8028 printf("CTL:Persis Out error returned from "
8029 "ctl_ha_msg_send %d\n", isc_retval);
8032 /* Reserved but not all registrants */
8033 /* sa_res_key is res holder */
8034 if (sa_res_key == lun->pr_keys[lun->pr_res_idx]) {
8035 /* validate scope and type */
8036 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
8038 mtx_unlock(&lun->lun_lock);
8039 ctl_set_invalid_field(/*ctsio*/ ctsio,
8045 ctl_done((union ctl_io *)ctsio);
8049 if (type>8 || type==2 || type==4 || type==0) {
8050 mtx_unlock(&lun->lun_lock);
8051 ctl_set_invalid_field(/*ctsio*/ ctsio,
8057 ctl_done((union ctl_io *)ctsio);
8063 * if sa_res_key != res_key remove all
8064 * registrants w/sa_res_key and generate UA
8065 * for these registrants(Registrations
8066 * Preempted) if it wasn't an exclusive
8067 * reservation generate UA(Reservations
8068 * Preempted) for all other registered nexuses
8069 * if the type has changed. Establish the new
8070 * reservation and holder. If res_key and
8071 * sa_res_key are the same do the above
8072 * except don't unregister the res holder.
8075 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8076 if (i == residx || lun->pr_keys[i] == 0)
8079 if (sa_res_key == lun->pr_keys[i]) {
8080 lun->pr_keys[i] = 0;
8081 lun->pr_key_count--;
8084 && i < CTL_MAX_INITIATORS)
8085 lun->pending_ua[i] |=
8087 else if (persis_offset
8088 && i >= persis_offset)
8089 lun->pending_ua[i-persis_offset] |=
8091 } else if (type != lun->res_type
8092 && (lun->res_type == SPR_TYPE_WR_EX_RO
8093 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
8095 && i < CTL_MAX_INITIATORS)
8096 lun->pending_ua[i] |=
8098 else if (persis_offset
8099 && i >= persis_offset)
8105 lun->res_type = type;
8106 if (lun->res_type != SPR_TYPE_WR_EX_AR
8107 && lun->res_type != SPR_TYPE_EX_AC_AR)
8108 lun->pr_res_idx = residx;
8110 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8112 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8113 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8114 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8115 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8116 persis_io.pr.pr_info.res_type = type;
8117 memcpy(persis_io.pr.pr_info.sa_res_key,
8118 param->serv_act_res_key,
8119 sizeof(param->serv_act_res_key));
8120 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8121 &persis_io, sizeof(persis_io), 0)) >
8122 CTL_HA_STATUS_SUCCESS) {
8123 printf("CTL:Persis Out error returned "
8124 "from ctl_ha_msg_send %d\n",
8129 * sa_res_key is not the res holder just
8130 * remove registrants
8134 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8135 if (sa_res_key != lun->pr_keys[i])
8139 lun->pr_keys[i] = 0;
8140 lun->pr_key_count--;
8143 && i < CTL_MAX_INITIATORS)
8144 lun->pending_ua[i] |=
8146 else if (persis_offset
8147 && i >= persis_offset)
8148 lun->pending_ua[i-persis_offset] |=
8153 mtx_unlock(&lun->lun_lock);
8154 free(ctsio->kern_data_ptr, M_CTL);
8155 ctl_set_reservation_conflict(ctsio);
8156 ctl_done((union ctl_io *)ctsio);
8159 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8160 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8161 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8162 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8163 persis_io.pr.pr_info.res_type = type;
8164 memcpy(persis_io.pr.pr_info.sa_res_key,
8165 param->serv_act_res_key,
8166 sizeof(param->serv_act_res_key));
8167 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8168 &persis_io, sizeof(persis_io), 0)) >
8169 CTL_HA_STATUS_SUCCESS) {
8170 printf("CTL:Persis Out error returned "
8171 "from ctl_ha_msg_send %d\n",
8177 lun->PRGeneration++;
8178 mtx_unlock(&lun->lun_lock);
8184 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8186 uint64_t sa_res_key;
8189 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8191 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8192 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8193 || sa_res_key != lun->pr_keys[lun->pr_res_idx]) {
8194 if (sa_res_key == 0) {
8196 * Unregister everybody else and build UA for
8199 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8200 if (i == msg->pr.pr_info.residx ||
8201 lun->pr_keys[i] == 0)
8205 && i < CTL_MAX_INITIATORS)
8206 lun->pending_ua[i] |=
8208 else if (persis_offset && i >= persis_offset)
8209 lun->pending_ua[i - persis_offset] |=
8211 lun->pr_keys[i] = 0;
8214 lun->pr_key_count = 1;
8215 lun->res_type = msg->pr.pr_info.res_type;
8216 if (lun->res_type != SPR_TYPE_WR_EX_AR
8217 && lun->res_type != SPR_TYPE_EX_AC_AR)
8218 lun->pr_res_idx = msg->pr.pr_info.residx;
8220 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8221 if (sa_res_key == lun->pr_keys[i])
8224 lun->pr_keys[i] = 0;
8225 lun->pr_key_count--;
8228 && i < persis_offset)
8229 lun->pending_ua[i] |=
8231 else if (persis_offset
8232 && i >= persis_offset)
8233 lun->pending_ua[i - persis_offset] |=
8238 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8239 if (i == msg->pr.pr_info.residx ||
8240 lun->pr_keys[i] == 0)
8243 if (sa_res_key == lun->pr_keys[i]) {
8244 lun->pr_keys[i] = 0;
8245 lun->pr_key_count--;
8247 && i < CTL_MAX_INITIATORS)
8248 lun->pending_ua[i] |=
8250 else if (persis_offset
8251 && i >= persis_offset)
8252 lun->pending_ua[i - persis_offset] |=
8254 } else if (msg->pr.pr_info.res_type != lun->res_type
8255 && (lun->res_type == SPR_TYPE_WR_EX_RO
8256 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
8258 && i < persis_offset)
8259 lun->pending_ua[i] |=
8261 else if (persis_offset
8262 && i >= persis_offset)
8263 lun->pending_ua[i - persis_offset] |=
8267 lun->res_type = msg->pr.pr_info.res_type;
8268 if (lun->res_type != SPR_TYPE_WR_EX_AR
8269 && lun->res_type != SPR_TYPE_EX_AC_AR)
8270 lun->pr_res_idx = msg->pr.pr_info.residx;
8272 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8274 lun->PRGeneration++;
8280 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8284 u_int32_t param_len;
8285 struct scsi_per_res_out *cdb;
8286 struct ctl_lun *lun;
8287 struct scsi_per_res_out_parms* param;
8288 struct ctl_softc *softc;
8290 uint64_t res_key, sa_res_key;
8292 union ctl_ha_msg persis_io;
8295 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8297 retval = CTL_RETVAL_COMPLETE;
8299 softc = control_softc;
8301 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8302 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8305 * We only support whole-LUN scope. The scope & type are ignored for
8306 * register, register and ignore existing key and clear.
8307 * We sometimes ignore scope and type on preempts too!!
8308 * Verify reservation type here as well.
8310 type = cdb->scope_type & SPR_TYPE_MASK;
8311 if ((cdb->action == SPRO_RESERVE)
8312 || (cdb->action == SPRO_RELEASE)) {
8313 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8314 ctl_set_invalid_field(/*ctsio*/ ctsio,
8320 ctl_done((union ctl_io *)ctsio);
8321 return (CTL_RETVAL_COMPLETE);
8324 if (type>8 || type==2 || type==4 || type==0) {
8325 ctl_set_invalid_field(/*ctsio*/ ctsio,
8331 ctl_done((union ctl_io *)ctsio);
8332 return (CTL_RETVAL_COMPLETE);
8336 param_len = scsi_4btoul(cdb->length);
8338 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8339 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8340 ctsio->kern_data_len = param_len;
8341 ctsio->kern_total_len = param_len;
8342 ctsio->kern_data_resid = 0;
8343 ctsio->kern_rel_offset = 0;
8344 ctsio->kern_sg_entries = 0;
8345 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8346 ctsio->be_move_done = ctl_config_move_done;
8347 ctl_datamove((union ctl_io *)ctsio);
8349 return (CTL_RETVAL_COMPLETE);
8352 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8354 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8355 res_key = scsi_8btou64(param->res_key.key);
8356 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8359 * Validate the reservation key here except for SPRO_REG_IGNO
8360 * This must be done for all other service actions
8362 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8363 mtx_lock(&lun->lun_lock);
8364 if (lun->pr_keys[residx] != 0) {
8365 if (res_key != lun->pr_keys[residx]) {
8367 * The current key passed in doesn't match
8368 * the one the initiator previously
8371 mtx_unlock(&lun->lun_lock);
8372 free(ctsio->kern_data_ptr, M_CTL);
8373 ctl_set_reservation_conflict(ctsio);
8374 ctl_done((union ctl_io *)ctsio);
8375 return (CTL_RETVAL_COMPLETE);
8377 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8379 * We are not registered
8381 mtx_unlock(&lun->lun_lock);
8382 free(ctsio->kern_data_ptr, M_CTL);
8383 ctl_set_reservation_conflict(ctsio);
8384 ctl_done((union ctl_io *)ctsio);
8385 return (CTL_RETVAL_COMPLETE);
8386 } else if (res_key != 0) {
8388 * We are not registered and trying to register but
8389 * the register key isn't zero.
8391 mtx_unlock(&lun->lun_lock);
8392 free(ctsio->kern_data_ptr, M_CTL);
8393 ctl_set_reservation_conflict(ctsio);
8394 ctl_done((union ctl_io *)ctsio);
8395 return (CTL_RETVAL_COMPLETE);
8397 mtx_unlock(&lun->lun_lock);
8400 switch (cdb->action & SPRO_ACTION_MASK) {
8402 case SPRO_REG_IGNO: {
8405 printf("Registration received\n");
8409 * We don't support any of these options, as we report in
8410 * the read capabilities request (see
8411 * ctl_persistent_reserve_in(), above).
8413 if ((param->flags & SPR_SPEC_I_PT)
8414 || (param->flags & SPR_ALL_TG_PT)
8415 || (param->flags & SPR_APTPL)) {
8418 if (param->flags & SPR_APTPL)
8420 else if (param->flags & SPR_ALL_TG_PT)
8422 else /* SPR_SPEC_I_PT */
8425 free(ctsio->kern_data_ptr, M_CTL);
8426 ctl_set_invalid_field(ctsio,
8432 ctl_done((union ctl_io *)ctsio);
8433 return (CTL_RETVAL_COMPLETE);
8436 mtx_lock(&lun->lun_lock);
8439 * The initiator wants to clear the
8442 if (sa_res_key == 0) {
8444 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8445 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8446 && lun->pr_keys[residx] == 0)) {
8447 mtx_unlock(&lun->lun_lock);
8451 lun->pr_keys[residx] = 0;
8452 lun->pr_key_count--;
8454 if (residx == lun->pr_res_idx) {
8455 lun->flags &= ~CTL_LUN_PR_RESERVED;
8456 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8458 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8459 || lun->res_type == SPR_TYPE_EX_AC_RO)
8460 && lun->pr_key_count) {
8462 * If the reservation is a registrants
8463 * only type we need to generate a UA
8464 * for other registered inits. The
8465 * sense code should be RESERVATIONS
8469 for (i = 0; i < CTL_MAX_INITIATORS;i++){
8471 i + persis_offset] == 0)
8473 lun->pending_ua[i] |=
8478 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8479 if (lun->pr_key_count==0) {
8480 lun->flags &= ~CTL_LUN_PR_RESERVED;
8482 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8485 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8486 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8487 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8488 persis_io.pr.pr_info.residx = residx;
8489 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8490 &persis_io, sizeof(persis_io), 0 )) >
8491 CTL_HA_STATUS_SUCCESS) {
8492 printf("CTL:Persis Out error returned from "
8493 "ctl_ha_msg_send %d\n", isc_retval);
8495 } else /* sa_res_key != 0 */ {
8498 * If we aren't registered currently then increment
8499 * the key count and set the registered flag.
8501 if (lun->pr_keys[residx] == 0)
8502 lun->pr_key_count++;
8503 lun->pr_keys[residx] = sa_res_key;
8505 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8506 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8507 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8508 persis_io.pr.pr_info.residx = residx;
8509 memcpy(persis_io.pr.pr_info.sa_res_key,
8510 param->serv_act_res_key,
8511 sizeof(param->serv_act_res_key));
8512 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8513 &persis_io, sizeof(persis_io), 0)) >
8514 CTL_HA_STATUS_SUCCESS) {
8515 printf("CTL:Persis Out error returned from "
8516 "ctl_ha_msg_send %d\n", isc_retval);
8519 lun->PRGeneration++;
8520 mtx_unlock(&lun->lun_lock);
8526 printf("Reserve executed type %d\n", type);
8528 mtx_lock(&lun->lun_lock);
8529 if (lun->flags & CTL_LUN_PR_RESERVED) {
8531 * if this isn't the reservation holder and it's
8532 * not a "all registrants" type or if the type is
8533 * different then we have a conflict
8535 if ((lun->pr_res_idx != residx
8536 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8537 || lun->res_type != type) {
8538 mtx_unlock(&lun->lun_lock);
8539 free(ctsio->kern_data_ptr, M_CTL);
8540 ctl_set_reservation_conflict(ctsio);
8541 ctl_done((union ctl_io *)ctsio);
8542 return (CTL_RETVAL_COMPLETE);
8544 mtx_unlock(&lun->lun_lock);
8545 } else /* create a reservation */ {
8547 * If it's not an "all registrants" type record
8548 * reservation holder
8550 if (type != SPR_TYPE_WR_EX_AR
8551 && type != SPR_TYPE_EX_AC_AR)
8552 lun->pr_res_idx = residx; /* Res holder */
8554 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8556 lun->flags |= CTL_LUN_PR_RESERVED;
8557 lun->res_type = type;
8559 mtx_unlock(&lun->lun_lock);
8561 /* send msg to other side */
8562 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8563 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8564 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8565 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8566 persis_io.pr.pr_info.res_type = type;
8567 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8568 &persis_io, sizeof(persis_io), 0)) >
8569 CTL_HA_STATUS_SUCCESS) {
8570 printf("CTL:Persis Out error returned from "
8571 "ctl_ha_msg_send %d\n", isc_retval);
8577 mtx_lock(&lun->lun_lock);
8578 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8579 /* No reservation exists return good status */
8580 mtx_unlock(&lun->lun_lock);
8584 * Is this nexus a reservation holder?
8586 if (lun->pr_res_idx != residx
8587 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8589 * not a res holder return good status but
8592 mtx_unlock(&lun->lun_lock);
8596 if (lun->res_type != type) {
8597 mtx_unlock(&lun->lun_lock);
8598 free(ctsio->kern_data_ptr, M_CTL);
8599 ctl_set_illegal_pr_release(ctsio);
8600 ctl_done((union ctl_io *)ctsio);
8601 return (CTL_RETVAL_COMPLETE);
8604 /* okay to release */
8605 lun->flags &= ~CTL_LUN_PR_RESERVED;
8606 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8610 * if this isn't an exclusive access
8611 * res generate UA for all other
8614 if (type != SPR_TYPE_EX_AC
8615 && type != SPR_TYPE_WR_EX) {
8616 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8618 lun->pr_keys[i + persis_offset] == 0)
8620 lun->pending_ua[i] |= CTL_UA_RES_RELEASE;
8623 mtx_unlock(&lun->lun_lock);
8624 /* Send msg to other side */
8625 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8626 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8627 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8628 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io,
8629 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8630 printf("CTL:Persis Out error returned from "
8631 "ctl_ha_msg_send %d\n", isc_retval);
8636 /* send msg to other side */
8638 mtx_lock(&lun->lun_lock);
8639 lun->flags &= ~CTL_LUN_PR_RESERVED;
8641 lun->pr_key_count = 0;
8642 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8644 lun->pr_keys[residx] = 0;
8646 for (i=0; i < 2*CTL_MAX_INITIATORS; i++)
8647 if (lun->pr_keys[i] != 0) {
8648 if (!persis_offset && i < CTL_MAX_INITIATORS)
8649 lun->pending_ua[i] |=
8651 else if (persis_offset && i >= persis_offset)
8652 lun->pending_ua[i-persis_offset] |=
8655 lun->pr_keys[i] = 0;
8657 lun->PRGeneration++;
8658 mtx_unlock(&lun->lun_lock);
8659 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8660 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8661 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8662 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8663 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8664 printf("CTL:Persis Out error returned from "
8665 "ctl_ha_msg_send %d\n", isc_retval);
8670 case SPRO_PRE_ABO: {
8673 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8674 residx, ctsio, cdb, param);
8676 return (CTL_RETVAL_COMPLETE);
8680 panic("Invalid PR type %x", cdb->action);
8684 free(ctsio->kern_data_ptr, M_CTL);
8685 ctl_set_success(ctsio);
8686 ctl_done((union ctl_io *)ctsio);
8692 * This routine is for handling a message from the other SC pertaining to
8693 * persistent reserve out. All the error checking will have been done
8694 * so only perorming the action need be done here to keep the two
8698 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8700 struct ctl_lun *lun;
8701 struct ctl_softc *softc;
8705 softc = control_softc;
8707 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8708 lun = softc->ctl_luns[targ_lun];
8709 mtx_lock(&lun->lun_lock);
8710 switch(msg->pr.pr_info.action) {
8711 case CTL_PR_REG_KEY:
8712 if (lun->pr_keys[msg->pr.pr_info.residx] == 0)
8713 lun->pr_key_count++;
8714 lun->pr_keys[msg->pr.pr_info.residx] =
8715 scsi_8btou64(msg->pr.pr_info.sa_res_key);
8716 lun->PRGeneration++;
8719 case CTL_PR_UNREG_KEY:
8720 lun->pr_keys[msg->pr.pr_info.residx] = 0;
8721 lun->pr_key_count--;
8723 /* XXX Need to see if the reservation has been released */
8724 /* if so do we need to generate UA? */
8725 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8726 lun->flags &= ~CTL_LUN_PR_RESERVED;
8727 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8729 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8730 || lun->res_type == SPR_TYPE_EX_AC_RO)
8731 && lun->pr_key_count) {
8733 * If the reservation is a registrants
8734 * only type we need to generate a UA
8735 * for other registered inits. The
8736 * sense code should be RESERVATIONS
8740 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8742 persis_offset] == 0)
8745 lun->pending_ua[i] |=
8750 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8751 if (lun->pr_key_count==0) {
8752 lun->flags &= ~CTL_LUN_PR_RESERVED;
8754 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8757 lun->PRGeneration++;
8760 case CTL_PR_RESERVE:
8761 lun->flags |= CTL_LUN_PR_RESERVED;
8762 lun->res_type = msg->pr.pr_info.res_type;
8763 lun->pr_res_idx = msg->pr.pr_info.residx;
8767 case CTL_PR_RELEASE:
8769 * if this isn't an exclusive access res generate UA for all
8770 * other registrants.
8772 if (lun->res_type != SPR_TYPE_EX_AC
8773 && lun->res_type != SPR_TYPE_WR_EX) {
8774 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8775 if (lun->pr_keys[i+persis_offset] != 0)
8776 lun->pending_ua[i] |=
8780 lun->flags &= ~CTL_LUN_PR_RESERVED;
8781 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8785 case CTL_PR_PREEMPT:
8786 ctl_pro_preempt_other(lun, msg);
8789 lun->flags &= ~CTL_LUN_PR_RESERVED;
8791 lun->pr_key_count = 0;
8792 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8794 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8795 if (lun->pr_keys[i] == 0)
8798 && i < CTL_MAX_INITIATORS)
8799 lun->pending_ua[i] |= CTL_UA_RES_PREEMPT;
8800 else if (persis_offset
8801 && i >= persis_offset)
8802 lun->pending_ua[i-persis_offset] |=
8804 lun->pr_keys[i] = 0;
8806 lun->PRGeneration++;
8810 mtx_unlock(&lun->lun_lock);
8814 ctl_read_write(struct ctl_scsiio *ctsio)
8816 struct ctl_lun *lun;
8817 struct ctl_lba_len_flags *lbalen;
8819 uint32_t num_blocks;
8823 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8825 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8828 retval = CTL_RETVAL_COMPLETE;
8830 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8831 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8832 switch (ctsio->cdb[0]) {
8835 struct scsi_rw_6 *cdb;
8837 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8839 lba = scsi_3btoul(cdb->addr);
8840 /* only 5 bits are valid in the most significant address byte */
8842 num_blocks = cdb->length;
8844 * This is correct according to SBC-2.
8846 if (num_blocks == 0)
8852 struct scsi_rw_10 *cdb;
8854 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8855 if (cdb->byte2 & SRW10_FUA)
8856 flags |= CTL_LLF_FUA;
8857 if (cdb->byte2 & SRW10_DPO)
8858 flags |= CTL_LLF_DPO;
8859 lba = scsi_4btoul(cdb->addr);
8860 num_blocks = scsi_2btoul(cdb->length);
8863 case WRITE_VERIFY_10: {
8864 struct scsi_write_verify_10 *cdb;
8866 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8867 flags |= CTL_LLF_FUA;
8868 if (cdb->byte2 & SWV_DPO)
8869 flags |= CTL_LLF_DPO;
8870 lba = scsi_4btoul(cdb->addr);
8871 num_blocks = scsi_2btoul(cdb->length);
8876 struct scsi_rw_12 *cdb;
8878 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8879 if (cdb->byte2 & SRW12_FUA)
8880 flags |= CTL_LLF_FUA;
8881 if (cdb->byte2 & SRW12_DPO)
8882 flags |= CTL_LLF_DPO;
8883 lba = scsi_4btoul(cdb->addr);
8884 num_blocks = scsi_4btoul(cdb->length);
8887 case WRITE_VERIFY_12: {
8888 struct scsi_write_verify_12 *cdb;
8890 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8891 flags |= CTL_LLF_FUA;
8892 if (cdb->byte2 & SWV_DPO)
8893 flags |= CTL_LLF_DPO;
8894 lba = scsi_4btoul(cdb->addr);
8895 num_blocks = scsi_4btoul(cdb->length);
8900 struct scsi_rw_16 *cdb;
8902 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8903 if (cdb->byte2 & SRW12_FUA)
8904 flags |= CTL_LLF_FUA;
8905 if (cdb->byte2 & SRW12_DPO)
8906 flags |= CTL_LLF_DPO;
8907 lba = scsi_8btou64(cdb->addr);
8908 num_blocks = scsi_4btoul(cdb->length);
8911 case WRITE_ATOMIC_16: {
8912 struct scsi_rw_16 *cdb;
8914 if (lun->be_lun->atomicblock == 0) {
8915 ctl_set_invalid_opcode(ctsio);
8916 ctl_done((union ctl_io *)ctsio);
8917 return (CTL_RETVAL_COMPLETE);
8920 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8921 if (cdb->byte2 & SRW12_FUA)
8922 flags |= CTL_LLF_FUA;
8923 if (cdb->byte2 & SRW12_DPO)
8924 flags |= CTL_LLF_DPO;
8925 lba = scsi_8btou64(cdb->addr);
8926 num_blocks = scsi_4btoul(cdb->length);
8927 if (num_blocks > lun->be_lun->atomicblock) {
8928 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8929 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8931 ctl_done((union ctl_io *)ctsio);
8932 return (CTL_RETVAL_COMPLETE);
8936 case WRITE_VERIFY_16: {
8937 struct scsi_write_verify_16 *cdb;
8939 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8940 flags |= CTL_LLF_FUA;
8941 if (cdb->byte2 & SWV_DPO)
8942 flags |= CTL_LLF_DPO;
8943 lba = scsi_8btou64(cdb->addr);
8944 num_blocks = scsi_4btoul(cdb->length);
8949 * We got a command we don't support. This shouldn't
8950 * happen, commands should be filtered out above us.
8952 ctl_set_invalid_opcode(ctsio);
8953 ctl_done((union ctl_io *)ctsio);
8955 return (CTL_RETVAL_COMPLETE);
8956 break; /* NOTREACHED */
8960 * The first check is to make sure we're in bounds, the second
8961 * check is to catch wrap-around problems. If the lba + num blocks
8962 * is less than the lba, then we've wrapped around and the block
8963 * range is invalid anyway.
8965 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8966 || ((lba + num_blocks) < lba)) {
8967 ctl_set_lba_out_of_range(ctsio);
8968 ctl_done((union ctl_io *)ctsio);
8969 return (CTL_RETVAL_COMPLETE);
8973 * According to SBC-3, a transfer length of 0 is not an error.
8974 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8975 * translates to 256 blocks for those commands.
8977 if (num_blocks == 0) {
8978 ctl_set_success(ctsio);
8979 ctl_done((union ctl_io *)ctsio);
8980 return (CTL_RETVAL_COMPLETE);
8983 /* Set FUA and/or DPO if caches are disabled. */
8985 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8987 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8989 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8991 flags |= CTL_LLF_FUA;
8994 lbalen = (struct ctl_lba_len_flags *)
8995 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8997 lbalen->len = num_blocks;
8998 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
9000 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9001 ctsio->kern_rel_offset = 0;
9003 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
9005 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9011 ctl_cnw_cont(union ctl_io *io)
9013 struct ctl_scsiio *ctsio;
9014 struct ctl_lun *lun;
9015 struct ctl_lba_len_flags *lbalen;
9018 ctsio = &io->scsiio;
9019 ctsio->io_hdr.status = CTL_STATUS_NONE;
9020 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
9021 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9022 lbalen = (struct ctl_lba_len_flags *)
9023 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9024 lbalen->flags &= ~CTL_LLF_COMPARE;
9025 lbalen->flags |= CTL_LLF_WRITE;
9027 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
9028 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9033 ctl_cnw(struct ctl_scsiio *ctsio)
9035 struct ctl_lun *lun;
9036 struct ctl_lba_len_flags *lbalen;
9038 uint32_t num_blocks;
9041 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9043 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
9046 retval = CTL_RETVAL_COMPLETE;
9048 switch (ctsio->cdb[0]) {
9049 case COMPARE_AND_WRITE: {
9050 struct scsi_compare_and_write *cdb;
9052 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
9053 if (cdb->byte2 & SRW10_FUA)
9054 flags |= CTL_LLF_FUA;
9055 if (cdb->byte2 & SRW10_DPO)
9056 flags |= CTL_LLF_DPO;
9057 lba = scsi_8btou64(cdb->addr);
9058 num_blocks = cdb->length;
9063 * We got a command we don't support. This shouldn't
9064 * happen, commands should be filtered out above us.
9066 ctl_set_invalid_opcode(ctsio);
9067 ctl_done((union ctl_io *)ctsio);
9069 return (CTL_RETVAL_COMPLETE);
9070 break; /* NOTREACHED */
9074 * The first check is to make sure we're in bounds, the second
9075 * check is to catch wrap-around problems. If the lba + num blocks
9076 * is less than the lba, then we've wrapped around and the block
9077 * range is invalid anyway.
9079 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9080 || ((lba + num_blocks) < lba)) {
9081 ctl_set_lba_out_of_range(ctsio);
9082 ctl_done((union ctl_io *)ctsio);
9083 return (CTL_RETVAL_COMPLETE);
9087 * According to SBC-3, a transfer length of 0 is not an error.
9089 if (num_blocks == 0) {
9090 ctl_set_success(ctsio);
9091 ctl_done((union ctl_io *)ctsio);
9092 return (CTL_RETVAL_COMPLETE);
9095 /* Set FUA if write cache is disabled. */
9096 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9098 flags |= CTL_LLF_FUA;
9100 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
9101 ctsio->kern_rel_offset = 0;
9104 * Set the IO_CONT flag, so that if this I/O gets passed to
9105 * ctl_data_submit_done(), it'll get passed back to
9106 * ctl_ctl_cnw_cont() for further processing.
9108 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
9109 ctsio->io_cont = ctl_cnw_cont;
9111 lbalen = (struct ctl_lba_len_flags *)
9112 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9114 lbalen->len = num_blocks;
9115 lbalen->flags = CTL_LLF_COMPARE | flags;
9117 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
9118 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9123 ctl_verify(struct ctl_scsiio *ctsio)
9125 struct ctl_lun *lun;
9126 struct ctl_lba_len_flags *lbalen;
9128 uint32_t num_blocks;
9132 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9134 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
9137 flags = CTL_LLF_FUA;
9138 retval = CTL_RETVAL_COMPLETE;
9140 switch (ctsio->cdb[0]) {
9142 struct scsi_verify_10 *cdb;
9144 cdb = (struct scsi_verify_10 *)ctsio->cdb;
9145 if (cdb->byte2 & SVFY_BYTCHK)
9147 if (cdb->byte2 & SVFY_DPO)
9148 flags |= CTL_LLF_DPO;
9149 lba = scsi_4btoul(cdb->addr);
9150 num_blocks = scsi_2btoul(cdb->length);
9154 struct scsi_verify_12 *cdb;
9156 cdb = (struct scsi_verify_12 *)ctsio->cdb;
9157 if (cdb->byte2 & SVFY_BYTCHK)
9159 if (cdb->byte2 & SVFY_DPO)
9160 flags |= CTL_LLF_DPO;
9161 lba = scsi_4btoul(cdb->addr);
9162 num_blocks = scsi_4btoul(cdb->length);
9166 struct scsi_rw_16 *cdb;
9168 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9169 if (cdb->byte2 & SVFY_BYTCHK)
9171 if (cdb->byte2 & SVFY_DPO)
9172 flags |= CTL_LLF_DPO;
9173 lba = scsi_8btou64(cdb->addr);
9174 num_blocks = scsi_4btoul(cdb->length);
9179 * We got a command we don't support. This shouldn't
9180 * happen, commands should be filtered out above us.
9182 ctl_set_invalid_opcode(ctsio);
9183 ctl_done((union ctl_io *)ctsio);
9184 return (CTL_RETVAL_COMPLETE);
9188 * The first check is to make sure we're in bounds, the second
9189 * check is to catch wrap-around problems. If the lba + num blocks
9190 * is less than the lba, then we've wrapped around and the block
9191 * range is invalid anyway.
9193 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9194 || ((lba + num_blocks) < lba)) {
9195 ctl_set_lba_out_of_range(ctsio);
9196 ctl_done((union ctl_io *)ctsio);
9197 return (CTL_RETVAL_COMPLETE);
9201 * According to SBC-3, a transfer length of 0 is not an error.
9203 if (num_blocks == 0) {
9204 ctl_set_success(ctsio);
9205 ctl_done((union ctl_io *)ctsio);
9206 return (CTL_RETVAL_COMPLETE);
9209 lbalen = (struct ctl_lba_len_flags *)
9210 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9212 lbalen->len = num_blocks;
9214 lbalen->flags = CTL_LLF_COMPARE | flags;
9215 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9217 lbalen->flags = CTL_LLF_VERIFY | flags;
9218 ctsio->kern_total_len = 0;
9220 ctsio->kern_rel_offset = 0;
9222 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9223 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9228 ctl_report_luns(struct ctl_scsiio *ctsio)
9230 struct scsi_report_luns *cdb;
9231 struct scsi_report_luns_data *lun_data;
9232 struct ctl_lun *lun, *request_lun;
9233 int num_luns, retval;
9234 uint32_t alloc_len, lun_datalen;
9235 int num_filled, well_known;
9236 uint32_t initidx, targ_lun_id, lun_id;
9238 retval = CTL_RETVAL_COMPLETE;
9241 cdb = (struct scsi_report_luns *)ctsio->cdb;
9243 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9245 mtx_lock(&control_softc->ctl_lock);
9246 num_luns = control_softc->num_luns;
9247 mtx_unlock(&control_softc->ctl_lock);
9249 switch (cdb->select_report) {
9250 case RPL_REPORT_DEFAULT:
9251 case RPL_REPORT_ALL:
9253 case RPL_REPORT_WELLKNOWN:
9258 ctl_set_invalid_field(ctsio,
9264 ctl_done((union ctl_io *)ctsio);
9266 break; /* NOTREACHED */
9269 alloc_len = scsi_4btoul(cdb->length);
9271 * The initiator has to allocate at least 16 bytes for this request,
9272 * so he can at least get the header and the first LUN. Otherwise
9273 * we reject the request (per SPC-3 rev 14, section 6.21).
9275 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9276 sizeof(struct scsi_report_luns_lundata))) {
9277 ctl_set_invalid_field(ctsio,
9283 ctl_done((union ctl_io *)ctsio);
9287 request_lun = (struct ctl_lun *)
9288 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9290 lun_datalen = sizeof(*lun_data) +
9291 (num_luns * sizeof(struct scsi_report_luns_lundata));
9293 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9294 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9295 ctsio->kern_sg_entries = 0;
9297 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9299 mtx_lock(&control_softc->ctl_lock);
9300 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9301 lun_id = ctl_map_lun(ctsio->io_hdr.nexus.targ_port, targ_lun_id);
9302 if (lun_id >= CTL_MAX_LUNS)
9304 lun = control_softc->ctl_luns[lun_id];
9308 if (targ_lun_id <= 0xff) {
9310 * Peripheral addressing method, bus number 0.
9312 lun_data->luns[num_filled].lundata[0] =
9313 RPL_LUNDATA_ATYP_PERIPH;
9314 lun_data->luns[num_filled].lundata[1] = targ_lun_id;
9316 } else if (targ_lun_id <= 0x3fff) {
9318 * Flat addressing method.
9320 lun_data->luns[num_filled].lundata[0] =
9321 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8);
9322 lun_data->luns[num_filled].lundata[1] =
9323 (targ_lun_id & 0xff);
9325 } else if (targ_lun_id <= 0xffffff) {
9327 * Extended flat addressing method.
9329 lun_data->luns[num_filled].lundata[0] =
9330 RPL_LUNDATA_ATYP_EXTLUN | 0x12;
9331 scsi_ulto3b(targ_lun_id,
9332 &lun_data->luns[num_filled].lundata[1]);
9335 printf("ctl_report_luns: bogus LUN number %jd, "
9336 "skipping\n", (intmax_t)targ_lun_id);
9339 * According to SPC-3, rev 14 section 6.21:
9341 * "The execution of a REPORT LUNS command to any valid and
9342 * installed logical unit shall clear the REPORTED LUNS DATA
9343 * HAS CHANGED unit attention condition for all logical
9344 * units of that target with respect to the requesting
9345 * initiator. A valid and installed logical unit is one
9346 * having a PERIPHERAL QUALIFIER of 000b in the standard
9347 * INQUIRY data (see 6.4.2)."
9349 * If request_lun is NULL, the LUN this report luns command
9350 * was issued to is either disabled or doesn't exist. In that
9351 * case, we shouldn't clear any pending lun change unit
9354 if (request_lun != NULL) {
9355 mtx_lock(&lun->lun_lock);
9356 lun->pending_ua[initidx] &= ~CTL_UA_LUN_CHANGE;
9357 mtx_unlock(&lun->lun_lock);
9360 mtx_unlock(&control_softc->ctl_lock);
9363 * It's quite possible that we've returned fewer LUNs than we allocated
9364 * space for. Trim it.
9366 lun_datalen = sizeof(*lun_data) +
9367 (num_filled * sizeof(struct scsi_report_luns_lundata));
9369 if (lun_datalen < alloc_len) {
9370 ctsio->residual = alloc_len - lun_datalen;
9371 ctsio->kern_data_len = lun_datalen;
9372 ctsio->kern_total_len = lun_datalen;
9374 ctsio->residual = 0;
9375 ctsio->kern_data_len = alloc_len;
9376 ctsio->kern_total_len = alloc_len;
9378 ctsio->kern_data_resid = 0;
9379 ctsio->kern_rel_offset = 0;
9380 ctsio->kern_sg_entries = 0;
9383 * We set this to the actual data length, regardless of how much
9384 * space we actually have to return results. If the user looks at
9385 * this value, he'll know whether or not he allocated enough space
9386 * and reissue the command if necessary. We don't support well
9387 * known logical units, so if the user asks for that, return none.
9389 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9392 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9395 ctl_set_success(ctsio);
9396 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9397 ctsio->be_move_done = ctl_config_move_done;
9398 ctl_datamove((union ctl_io *)ctsio);
9403 ctl_request_sense(struct ctl_scsiio *ctsio)
9405 struct scsi_request_sense *cdb;
9406 struct scsi_sense_data *sense_ptr;
9407 struct ctl_lun *lun;
9410 scsi_sense_data_type sense_format;
9412 cdb = (struct scsi_request_sense *)ctsio->cdb;
9414 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9416 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9419 * Determine which sense format the user wants.
9421 if (cdb->byte2 & SRS_DESC)
9422 sense_format = SSD_TYPE_DESC;
9424 sense_format = SSD_TYPE_FIXED;
9426 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9427 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9428 ctsio->kern_sg_entries = 0;
9431 * struct scsi_sense_data, which is currently set to 256 bytes, is
9432 * larger than the largest allowed value for the length field in the
9433 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9435 ctsio->residual = 0;
9436 ctsio->kern_data_len = cdb->length;
9437 ctsio->kern_total_len = cdb->length;
9439 ctsio->kern_data_resid = 0;
9440 ctsio->kern_rel_offset = 0;
9441 ctsio->kern_sg_entries = 0;
9444 * If we don't have a LUN, we don't have any pending sense.
9450 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9452 * Check for pending sense, and then for pending unit attentions.
9453 * Pending sense gets returned first, then pending unit attentions.
9455 mtx_lock(&lun->lun_lock);
9457 if (ctl_is_set(lun->have_ca, initidx)) {
9458 scsi_sense_data_type stored_format;
9461 * Check to see which sense format was used for the stored
9464 stored_format = scsi_sense_type(&lun->pending_sense[initidx]);
9467 * If the user requested a different sense format than the
9468 * one we stored, then we need to convert it to the other
9469 * format. If we're going from descriptor to fixed format
9470 * sense data, we may lose things in translation, depending
9471 * on what options were used.
9473 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9474 * for some reason we'll just copy it out as-is.
9476 if ((stored_format == SSD_TYPE_FIXED)
9477 && (sense_format == SSD_TYPE_DESC))
9478 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9479 &lun->pending_sense[initidx],
9480 (struct scsi_sense_data_desc *)sense_ptr);
9481 else if ((stored_format == SSD_TYPE_DESC)
9482 && (sense_format == SSD_TYPE_FIXED))
9483 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9484 &lun->pending_sense[initidx],
9485 (struct scsi_sense_data_fixed *)sense_ptr);
9487 memcpy(sense_ptr, &lun->pending_sense[initidx],
9488 ctl_min(sizeof(*sense_ptr),
9489 sizeof(lun->pending_sense[initidx])));
9491 ctl_clear_mask(lun->have_ca, initidx);
9495 if (lun->pending_ua[initidx] != CTL_UA_NONE) {
9496 ctl_ua_type ua_type;
9498 ua_type = ctl_build_ua(&lun->pending_ua[initidx],
9499 sense_ptr, sense_format);
9500 if (ua_type != CTL_UA_NONE)
9503 mtx_unlock(&lun->lun_lock);
9506 * We already have a pending error, return it.
9508 if (have_error != 0) {
9510 * We report the SCSI status as OK, since the status of the
9511 * request sense command itself is OK.
9512 * We report 0 for the sense length, because we aren't doing
9513 * autosense in this case. We're reporting sense as
9516 ctl_set_success(ctsio);
9517 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9518 ctsio->be_move_done = ctl_config_move_done;
9519 ctl_datamove((union ctl_io *)ctsio);
9520 return (CTL_RETVAL_COMPLETE);
9526 * No sense information to report, so we report that everything is
9529 ctl_set_sense_data(sense_ptr,
9532 /*current_error*/ 1,
9533 /*sense_key*/ SSD_KEY_NO_SENSE,
9539 * We report 0 for the sense length, because we aren't doing
9540 * autosense in this case. We're reporting sense as parameter data.
9542 ctl_set_success(ctsio);
9543 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9544 ctsio->be_move_done = ctl_config_move_done;
9545 ctl_datamove((union ctl_io *)ctsio);
9546 return (CTL_RETVAL_COMPLETE);
9550 ctl_tur(struct ctl_scsiio *ctsio)
9553 CTL_DEBUG_PRINT(("ctl_tur\n"));
9555 ctl_set_success(ctsio);
9556 ctl_done((union ctl_io *)ctsio);
9558 return (CTL_RETVAL_COMPLETE);
9563 ctl_cmddt_inquiry(struct ctl_scsiio *ctsio)
9570 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9572 struct scsi_vpd_supported_pages *pages;
9574 struct ctl_lun *lun;
9576 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9578 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9579 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9580 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9581 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9582 ctsio->kern_sg_entries = 0;
9584 if (sup_page_size < alloc_len) {
9585 ctsio->residual = alloc_len - sup_page_size;
9586 ctsio->kern_data_len = sup_page_size;
9587 ctsio->kern_total_len = sup_page_size;
9589 ctsio->residual = 0;
9590 ctsio->kern_data_len = alloc_len;
9591 ctsio->kern_total_len = alloc_len;
9593 ctsio->kern_data_resid = 0;
9594 ctsio->kern_rel_offset = 0;
9595 ctsio->kern_sg_entries = 0;
9598 * The control device is always connected. The disk device, on the
9599 * other hand, may not be online all the time. Need to change this
9600 * to figure out whether the disk device is actually online or not.
9603 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9604 lun->be_lun->lun_type;
9606 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9608 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES;
9609 /* Supported VPD pages */
9610 pages->page_list[0] = SVPD_SUPPORTED_PAGES;
9612 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER;
9613 /* Device Identification */
9614 pages->page_list[2] = SVPD_DEVICE_ID;
9615 /* Extended INQUIRY Data */
9616 pages->page_list[3] = SVPD_EXTENDED_INQUIRY_DATA;
9617 /* Mode Page Policy */
9618 pages->page_list[4] = SVPD_MODE_PAGE_POLICY;
9620 pages->page_list[5] = SVPD_SCSI_PORTS;
9621 /* Third-party Copy */
9622 pages->page_list[6] = SVPD_SCSI_TPC;
9624 pages->page_list[7] = SVPD_BLOCK_LIMITS;
9625 /* Block Device Characteristics */
9626 pages->page_list[8] = SVPD_BDC;
9627 /* Logical Block Provisioning */
9628 pages->page_list[9] = SVPD_LBP;
9630 ctl_set_success(ctsio);
9631 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9632 ctsio->be_move_done = ctl_config_move_done;
9633 ctl_datamove((union ctl_io *)ctsio);
9634 return (CTL_RETVAL_COMPLETE);
9638 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9640 struct scsi_vpd_unit_serial_number *sn_ptr;
9641 struct ctl_lun *lun;
9644 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9646 data_len = 4 + CTL_SN_LEN;
9647 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9648 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9649 if (data_len < alloc_len) {
9650 ctsio->residual = alloc_len - data_len;
9651 ctsio->kern_data_len = data_len;
9652 ctsio->kern_total_len = data_len;
9654 ctsio->residual = 0;
9655 ctsio->kern_data_len = alloc_len;
9656 ctsio->kern_total_len = alloc_len;
9658 ctsio->kern_data_resid = 0;
9659 ctsio->kern_rel_offset = 0;
9660 ctsio->kern_sg_entries = 0;
9663 * The control device is always connected. The disk device, on the
9664 * other hand, may not be online all the time. Need to change this
9665 * to figure out whether the disk device is actually online or not.
9668 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9669 lun->be_lun->lun_type;
9671 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9673 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9674 sn_ptr->length = CTL_SN_LEN;
9676 * If we don't have a LUN, we just leave the serial number as
9680 strncpy((char *)sn_ptr->serial_num,
9681 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9683 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9685 ctl_set_success(ctsio);
9686 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9687 ctsio->be_move_done = ctl_config_move_done;
9688 ctl_datamove((union ctl_io *)ctsio);
9689 return (CTL_RETVAL_COMPLETE);
9694 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9696 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9697 struct ctl_lun *lun;
9700 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9702 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9703 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9704 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9705 ctsio->kern_sg_entries = 0;
9707 if (data_len < alloc_len) {
9708 ctsio->residual = alloc_len - data_len;
9709 ctsio->kern_data_len = data_len;
9710 ctsio->kern_total_len = data_len;
9712 ctsio->residual = 0;
9713 ctsio->kern_data_len = alloc_len;
9714 ctsio->kern_total_len = alloc_len;
9716 ctsio->kern_data_resid = 0;
9717 ctsio->kern_rel_offset = 0;
9718 ctsio->kern_sg_entries = 0;
9721 * The control device is always connected. The disk device, on the
9722 * other hand, may not be online all the time.
9725 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9726 lun->be_lun->lun_type;
9728 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9729 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9730 eid_ptr->page_length = data_len - 4;
9731 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9732 eid_ptr->flags3 = SVPD_EID_V_SUP;
9734 ctl_set_success(ctsio);
9735 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9736 ctsio->be_move_done = ctl_config_move_done;
9737 ctl_datamove((union ctl_io *)ctsio);
9738 return (CTL_RETVAL_COMPLETE);
9742 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9744 struct scsi_vpd_mode_page_policy *mpp_ptr;
9745 struct ctl_lun *lun;
9748 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9750 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9751 sizeof(struct scsi_vpd_mode_page_policy_descr);
9753 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9754 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9755 ctsio->kern_sg_entries = 0;
9757 if (data_len < alloc_len) {
9758 ctsio->residual = alloc_len - data_len;
9759 ctsio->kern_data_len = data_len;
9760 ctsio->kern_total_len = data_len;
9762 ctsio->residual = 0;
9763 ctsio->kern_data_len = alloc_len;
9764 ctsio->kern_total_len = alloc_len;
9766 ctsio->kern_data_resid = 0;
9767 ctsio->kern_rel_offset = 0;
9768 ctsio->kern_sg_entries = 0;
9771 * The control device is always connected. The disk device, on the
9772 * other hand, may not be online all the time.
9775 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9776 lun->be_lun->lun_type;
9778 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9779 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9780 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9781 mpp_ptr->descr[0].page_code = 0x3f;
9782 mpp_ptr->descr[0].subpage_code = 0xff;
9783 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9785 ctl_set_success(ctsio);
9786 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9787 ctsio->be_move_done = ctl_config_move_done;
9788 ctl_datamove((union ctl_io *)ctsio);
9789 return (CTL_RETVAL_COMPLETE);
9793 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9795 struct scsi_vpd_device_id *devid_ptr;
9796 struct scsi_vpd_id_descriptor *desc;
9797 struct ctl_softc *ctl_softc;
9798 struct ctl_lun *lun;
9799 struct ctl_port *port;
9803 ctl_softc = control_softc;
9805 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)];
9806 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9808 data_len = sizeof(struct scsi_vpd_device_id) +
9809 sizeof(struct scsi_vpd_id_descriptor) +
9810 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9811 sizeof(struct scsi_vpd_id_descriptor) +
9812 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9813 if (lun && lun->lun_devid)
9814 data_len += lun->lun_devid->len;
9815 if (port->port_devid)
9816 data_len += port->port_devid->len;
9817 if (port->target_devid)
9818 data_len += port->target_devid->len;
9820 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9821 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9822 ctsio->kern_sg_entries = 0;
9824 if (data_len < alloc_len) {
9825 ctsio->residual = alloc_len - data_len;
9826 ctsio->kern_data_len = data_len;
9827 ctsio->kern_total_len = data_len;
9829 ctsio->residual = 0;
9830 ctsio->kern_data_len = alloc_len;
9831 ctsio->kern_total_len = alloc_len;
9833 ctsio->kern_data_resid = 0;
9834 ctsio->kern_rel_offset = 0;
9835 ctsio->kern_sg_entries = 0;
9838 * The control device is always connected. The disk device, on the
9839 * other hand, may not be online all the time.
9842 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9843 lun->be_lun->lun_type;
9845 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9846 devid_ptr->page_code = SVPD_DEVICE_ID;
9847 scsi_ulto2b(data_len - 4, devid_ptr->length);
9849 if (port->port_type == CTL_PORT_FC)
9850 proto = SCSI_PROTO_FC << 4;
9851 else if (port->port_type == CTL_PORT_ISCSI)
9852 proto = SCSI_PROTO_ISCSI << 4;
9854 proto = SCSI_PROTO_SPI << 4;
9855 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9858 * We're using a LUN association here. i.e., this device ID is a
9859 * per-LUN identifier.
9861 if (lun && lun->lun_devid) {
9862 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9863 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9864 lun->lun_devid->len);
9868 * This is for the WWPN which is a port association.
9870 if (port->port_devid) {
9871 memcpy(desc, port->port_devid->data, port->port_devid->len);
9872 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9873 port->port_devid->len);
9877 * This is for the Relative Target Port(type 4h) identifier
9879 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9880 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9881 SVPD_ID_TYPE_RELTARG;
9883 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9884 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9885 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9888 * This is for the Target Port Group(type 5h) identifier
9890 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9891 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9892 SVPD_ID_TYPE_TPORTGRP;
9894 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1,
9895 &desc->identifier[2]);
9896 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9897 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9900 * This is for the Target identifier
9902 if (port->target_devid) {
9903 memcpy(desc, port->target_devid->data, port->target_devid->len);
9906 ctl_set_success(ctsio);
9907 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9908 ctsio->be_move_done = ctl_config_move_done;
9909 ctl_datamove((union ctl_io *)ctsio);
9910 return (CTL_RETVAL_COMPLETE);
9914 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9916 struct ctl_softc *softc = control_softc;
9917 struct scsi_vpd_scsi_ports *sp;
9918 struct scsi_vpd_port_designation *pd;
9919 struct scsi_vpd_port_designation_cont *pdc;
9920 struct ctl_lun *lun;
9921 struct ctl_port *port;
9922 int data_len, num_target_ports, iid_len, id_len, g, pg, p;
9923 int num_target_port_groups;
9925 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9927 if (softc->is_single)
9928 num_target_port_groups = 1;
9930 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
9931 num_target_ports = 0;
9934 mtx_lock(&softc->ctl_lock);
9935 STAILQ_FOREACH(port, &softc->port_list, links) {
9936 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9939 ctl_map_lun_back(port->targ_port, lun->lun) >=
9943 if (port->init_devid)
9944 iid_len += port->init_devid->len;
9945 if (port->port_devid)
9946 id_len += port->port_devid->len;
9948 mtx_unlock(&softc->ctl_lock);
9950 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups *
9951 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9952 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9953 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9954 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9955 ctsio->kern_sg_entries = 0;
9957 if (data_len < alloc_len) {
9958 ctsio->residual = alloc_len - data_len;
9959 ctsio->kern_data_len = data_len;
9960 ctsio->kern_total_len = data_len;
9962 ctsio->residual = 0;
9963 ctsio->kern_data_len = alloc_len;
9964 ctsio->kern_total_len = alloc_len;
9966 ctsio->kern_data_resid = 0;
9967 ctsio->kern_rel_offset = 0;
9968 ctsio->kern_sg_entries = 0;
9971 * The control device is always connected. The disk device, on the
9972 * other hand, may not be online all the time. Need to change this
9973 * to figure out whether the disk device is actually online or not.
9976 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9977 lun->be_lun->lun_type;
9979 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9981 sp->page_code = SVPD_SCSI_PORTS;
9982 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9984 pd = &sp->design[0];
9986 mtx_lock(&softc->ctl_lock);
9987 pg = softc->port_offset / CTL_MAX_PORTS;
9988 for (g = 0; g < num_target_port_groups; g++) {
9989 STAILQ_FOREACH(port, &softc->port_list, links) {
9990 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9993 ctl_map_lun_back(port->targ_port, lun->lun) >=
9996 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS;
9997 scsi_ulto2b(p, pd->relative_port_id);
9998 if (port->init_devid && g == pg) {
9999 iid_len = port->init_devid->len;
10000 memcpy(pd->initiator_transportid,
10001 port->init_devid->data, port->init_devid->len);
10004 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
10005 pdc = (struct scsi_vpd_port_designation_cont *)
10006 (&pd->initiator_transportid[iid_len]);
10007 if (port->port_devid && g == pg) {
10008 id_len = port->port_devid->len;
10009 memcpy(pdc->target_port_descriptors,
10010 port->port_devid->data, port->port_devid->len);
10013 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
10014 pd = (struct scsi_vpd_port_designation *)
10015 ((uint8_t *)pdc->target_port_descriptors + id_len);
10018 mtx_unlock(&softc->ctl_lock);
10020 ctl_set_success(ctsio);
10021 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10022 ctsio->be_move_done = ctl_config_move_done;
10023 ctl_datamove((union ctl_io *)ctsio);
10024 return (CTL_RETVAL_COMPLETE);
10028 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
10030 struct scsi_vpd_block_limits *bl_ptr;
10031 struct ctl_lun *lun;
10034 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10036 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
10037 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
10038 ctsio->kern_sg_entries = 0;
10040 if (sizeof(*bl_ptr) < alloc_len) {
10041 ctsio->residual = alloc_len - sizeof(*bl_ptr);
10042 ctsio->kern_data_len = sizeof(*bl_ptr);
10043 ctsio->kern_total_len = sizeof(*bl_ptr);
10045 ctsio->residual = 0;
10046 ctsio->kern_data_len = alloc_len;
10047 ctsio->kern_total_len = alloc_len;
10049 ctsio->kern_data_resid = 0;
10050 ctsio->kern_rel_offset = 0;
10051 ctsio->kern_sg_entries = 0;
10054 * The control device is always connected. The disk device, on the
10055 * other hand, may not be online all the time. Need to change this
10056 * to figure out whether the disk device is actually online or not.
10059 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10060 lun->be_lun->lun_type;
10062 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10064 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
10065 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
10066 bl_ptr->max_cmp_write_len = 0xff;
10067 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
10069 bs = lun->be_lun->blocksize;
10070 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len);
10071 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10072 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
10073 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
10074 if (lun->be_lun->pblockexp != 0) {
10075 scsi_ulto4b((1 << lun->be_lun->pblockexp),
10076 bl_ptr->opt_unmap_grain);
10077 scsi_ulto4b(0x80000000 | lun->be_lun->pblockoff,
10078 bl_ptr->unmap_grain_align);
10081 scsi_ulto4b(lun->be_lun->atomicblock,
10082 bl_ptr->max_atomic_transfer_length);
10083 scsi_ulto4b(0, bl_ptr->atomic_alignment);
10084 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
10086 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
10088 ctl_set_success(ctsio);
10089 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10090 ctsio->be_move_done = ctl_config_move_done;
10091 ctl_datamove((union ctl_io *)ctsio);
10092 return (CTL_RETVAL_COMPLETE);
10096 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
10098 struct scsi_vpd_block_device_characteristics *bdc_ptr;
10099 struct ctl_lun *lun;
10103 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10105 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
10106 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
10107 ctsio->kern_sg_entries = 0;
10109 if (sizeof(*bdc_ptr) < alloc_len) {
10110 ctsio->residual = alloc_len - sizeof(*bdc_ptr);
10111 ctsio->kern_data_len = sizeof(*bdc_ptr);
10112 ctsio->kern_total_len = sizeof(*bdc_ptr);
10114 ctsio->residual = 0;
10115 ctsio->kern_data_len = alloc_len;
10116 ctsio->kern_total_len = alloc_len;
10118 ctsio->kern_data_resid = 0;
10119 ctsio->kern_rel_offset = 0;
10120 ctsio->kern_sg_entries = 0;
10123 * The control device is always connected. The disk device, on the
10124 * other hand, may not be online all the time. Need to change this
10125 * to figure out whether the disk device is actually online or not.
10128 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10129 lun->be_lun->lun_type;
10131 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10132 bdc_ptr->page_code = SVPD_BDC;
10133 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
10135 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL)
10136 i = strtol(value, NULL, 0);
10138 i = CTL_DEFAULT_ROTATION_RATE;
10139 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
10141 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL)
10142 i = strtol(value, NULL, 0);
10145 bdc_ptr->wab_wac_ff = (i & 0x0f);
10146 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
10148 ctl_set_success(ctsio);
10149 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10150 ctsio->be_move_done = ctl_config_move_done;
10151 ctl_datamove((union ctl_io *)ctsio);
10152 return (CTL_RETVAL_COMPLETE);
10156 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
10158 struct scsi_vpd_logical_block_prov *lbp_ptr;
10159 struct ctl_lun *lun;
10161 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10163 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
10164 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
10165 ctsio->kern_sg_entries = 0;
10167 if (sizeof(*lbp_ptr) < alloc_len) {
10168 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
10169 ctsio->kern_data_len = sizeof(*lbp_ptr);
10170 ctsio->kern_total_len = sizeof(*lbp_ptr);
10172 ctsio->residual = 0;
10173 ctsio->kern_data_len = alloc_len;
10174 ctsio->kern_total_len = alloc_len;
10176 ctsio->kern_data_resid = 0;
10177 ctsio->kern_rel_offset = 0;
10178 ctsio->kern_sg_entries = 0;
10181 * The control device is always connected. The disk device, on the
10182 * other hand, may not be online all the time. Need to change this
10183 * to figure out whether the disk device is actually online or not.
10186 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10187 lun->be_lun->lun_type;
10189 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10191 lbp_ptr->page_code = SVPD_LBP;
10192 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
10193 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10194 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
10195 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
10196 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
10197 lbp_ptr->prov_type = SVPD_LBP_THIN;
10200 ctl_set_success(ctsio);
10201 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10202 ctsio->be_move_done = ctl_config_move_done;
10203 ctl_datamove((union ctl_io *)ctsio);
10204 return (CTL_RETVAL_COMPLETE);
10208 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
10210 struct scsi_inquiry *cdb;
10211 int alloc_len, retval;
10213 cdb = (struct scsi_inquiry *)ctsio->cdb;
10215 retval = CTL_RETVAL_COMPLETE;
10217 alloc_len = scsi_2btoul(cdb->length);
10219 switch (cdb->page_code) {
10220 case SVPD_SUPPORTED_PAGES:
10221 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
10223 case SVPD_UNIT_SERIAL_NUMBER:
10224 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
10226 case SVPD_DEVICE_ID:
10227 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
10229 case SVPD_EXTENDED_INQUIRY_DATA:
10230 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
10232 case SVPD_MODE_PAGE_POLICY:
10233 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
10235 case SVPD_SCSI_PORTS:
10236 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10238 case SVPD_SCSI_TPC:
10239 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10241 case SVPD_BLOCK_LIMITS:
10242 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10245 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10248 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10251 ctl_set_invalid_field(ctsio,
10257 ctl_done((union ctl_io *)ctsio);
10258 retval = CTL_RETVAL_COMPLETE;
10266 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10268 struct scsi_inquiry_data *inq_ptr;
10269 struct scsi_inquiry *cdb;
10270 struct ctl_softc *ctl_softc;
10271 struct ctl_lun *lun;
10273 uint32_t alloc_len, data_len;
10274 ctl_port_type port_type;
10276 ctl_softc = control_softc;
10279 * Figure out whether we're talking to a Fibre Channel port or not.
10280 * We treat the ioctl front end, and any SCSI adapters, as packetized
10283 port_type = ctl_softc->ctl_ports[
10284 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type;
10285 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10286 port_type = CTL_PORT_SCSI;
10288 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10289 cdb = (struct scsi_inquiry *)ctsio->cdb;
10290 alloc_len = scsi_2btoul(cdb->length);
10293 * We malloc the full inquiry data size here and fill it
10294 * in. If the user only asks for less, we'll give him
10297 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10298 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10299 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10300 ctsio->kern_sg_entries = 0;
10301 ctsio->kern_data_resid = 0;
10302 ctsio->kern_rel_offset = 0;
10304 if (data_len < alloc_len) {
10305 ctsio->residual = alloc_len - data_len;
10306 ctsio->kern_data_len = data_len;
10307 ctsio->kern_total_len = data_len;
10309 ctsio->residual = 0;
10310 ctsio->kern_data_len = alloc_len;
10311 ctsio->kern_total_len = alloc_len;
10315 * If we have a LUN configured, report it as connected. Otherwise,
10316 * report that it is offline or no device is supported, depending
10317 * on the value of inquiry_pq_no_lun.
10319 * According to the spec (SPC-4 r34), the peripheral qualifier
10320 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario:
10322 * "A peripheral device having the specified peripheral device type
10323 * is not connected to this logical unit. However, the device
10324 * server is capable of supporting the specified peripheral device
10325 * type on this logical unit."
10327 * According to the same spec, the peripheral qualifier
10328 * SID_QUAL_BAD_LU (011b) is used in this scenario:
10330 * "The device server is not capable of supporting a peripheral
10331 * device on this logical unit. For this peripheral qualifier the
10332 * peripheral device type shall be set to 1Fh. All other peripheral
10333 * device type values are reserved for this peripheral qualifier."
10335 * Given the text, it would seem that we probably want to report that
10336 * the LUN is offline here. There is no LUN connected, but we can
10337 * support a LUN at the given LUN number.
10339 * In the real world, though, it sounds like things are a little
10342 * - Linux, when presented with a LUN with the offline peripheral
10343 * qualifier, will create an sg driver instance for it. So when
10344 * you attach it to CTL, you wind up with a ton of sg driver
10345 * instances. (One for every LUN that Linux bothered to probe.)
10346 * Linux does this despite the fact that it issues a REPORT LUNs
10347 * to LUN 0 to get the inventory of supported LUNs.
10349 * - There is other anecdotal evidence (from Emulex folks) about
10350 * arrays that use the offline peripheral qualifier for LUNs that
10351 * are on the "passive" path in an active/passive array.
10353 * So the solution is provide a hopefully reasonable default
10354 * (return bad/no LUN) and allow the user to change the behavior
10355 * with a tunable/sysctl variable.
10358 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10359 lun->be_lun->lun_type;
10360 else if (ctl_softc->inquiry_pq_no_lun == 0)
10361 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10363 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10365 /* RMB in byte 2 is 0 */
10366 inq_ptr->version = SCSI_REV_SPC4;
10369 * According to SAM-3, even if a device only supports a single
10370 * level of LUN addressing, it should still set the HISUP bit:
10372 * 4.9.1 Logical unit numbers overview
10374 * All logical unit number formats described in this standard are
10375 * hierarchical in structure even when only a single level in that
10376 * hierarchy is used. The HISUP bit shall be set to one in the
10377 * standard INQUIRY data (see SPC-2) when any logical unit number
10378 * format described in this standard is used. Non-hierarchical
10379 * formats are outside the scope of this standard.
10381 * Therefore we set the HiSup bit here.
10383 * The reponse format is 2, per SPC-3.
10385 inq_ptr->response_format = SID_HiSup | 2;
10387 inq_ptr->additional_length = data_len -
10388 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10389 CTL_DEBUG_PRINT(("additional_length = %d\n",
10390 inq_ptr->additional_length));
10392 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10393 /* 16 bit addressing */
10394 if (port_type == CTL_PORT_SCSI)
10395 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10396 /* XXX set the SID_MultiP bit here if we're actually going to
10397 respond on multiple ports */
10398 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10400 /* 16 bit data bus, synchronous transfers */
10401 if (port_type == CTL_PORT_SCSI)
10402 inq_ptr->flags = SID_WBus16 | SID_Sync;
10404 * XXX KDM do we want to support tagged queueing on the control
10408 || (lun->be_lun->lun_type != T_PROCESSOR))
10409 inq_ptr->flags |= SID_CmdQue;
10411 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10412 * We have 8 bytes for the vendor name, and 16 bytes for the device
10413 * name and 4 bytes for the revision.
10415 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10416 "vendor")) == NULL) {
10417 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10419 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10420 strncpy(inq_ptr->vendor, val,
10421 min(sizeof(inq_ptr->vendor), strlen(val)));
10424 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10425 sizeof(inq_ptr->product));
10426 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10427 switch (lun->be_lun->lun_type) {
10429 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10430 sizeof(inq_ptr->product));
10433 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10434 sizeof(inq_ptr->product));
10437 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10438 sizeof(inq_ptr->product));
10442 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10443 strncpy(inq_ptr->product, val,
10444 min(sizeof(inq_ptr->product), strlen(val)));
10448 * XXX make this a macro somewhere so it automatically gets
10449 * incremented when we make changes.
10451 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10452 "revision")) == NULL) {
10453 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10455 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10456 strncpy(inq_ptr->revision, val,
10457 min(sizeof(inq_ptr->revision), strlen(val)));
10461 * For parallel SCSI, we support double transition and single
10462 * transition clocking. We also support QAS (Quick Arbitration
10463 * and Selection) and Information Unit transfers on both the
10464 * control and array devices.
10466 if (port_type == CTL_PORT_SCSI)
10467 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10470 /* SAM-5 (no version claimed) */
10471 scsi_ulto2b(0x00A0, inq_ptr->version1);
10472 /* SPC-4 (no version claimed) */
10473 scsi_ulto2b(0x0460, inq_ptr->version2);
10474 if (port_type == CTL_PORT_FC) {
10475 /* FCP-2 ANSI INCITS.350:2003 */
10476 scsi_ulto2b(0x0917, inq_ptr->version3);
10477 } else if (port_type == CTL_PORT_SCSI) {
10478 /* SPI-4 ANSI INCITS.362:200x */
10479 scsi_ulto2b(0x0B56, inq_ptr->version3);
10480 } else if (port_type == CTL_PORT_ISCSI) {
10481 /* iSCSI (no version claimed) */
10482 scsi_ulto2b(0x0960, inq_ptr->version3);
10483 } else if (port_type == CTL_PORT_SAS) {
10484 /* SAS (no version claimed) */
10485 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10489 /* SBC-4 (no version claimed) */
10490 scsi_ulto2b(0x0600, inq_ptr->version4);
10492 switch (lun->be_lun->lun_type) {
10494 /* SBC-4 (no version claimed) */
10495 scsi_ulto2b(0x0600, inq_ptr->version4);
10503 ctl_set_success(ctsio);
10504 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10505 ctsio->be_move_done = ctl_config_move_done;
10506 ctl_datamove((union ctl_io *)ctsio);
10507 return (CTL_RETVAL_COMPLETE);
10511 ctl_inquiry(struct ctl_scsiio *ctsio)
10513 struct scsi_inquiry *cdb;
10516 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10518 cdb = (struct scsi_inquiry *)ctsio->cdb;
10519 if (cdb->byte2 & SI_EVPD)
10520 retval = ctl_inquiry_evpd(ctsio);
10521 else if (cdb->page_code == 0)
10522 retval = ctl_inquiry_std(ctsio);
10524 ctl_set_invalid_field(ctsio,
10530 ctl_done((union ctl_io *)ctsio);
10531 return (CTL_RETVAL_COMPLETE);
10538 * For known CDB types, parse the LBA and length.
10541 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10543 if (io->io_hdr.io_type != CTL_IO_SCSI)
10546 switch (io->scsiio.cdb[0]) {
10547 case COMPARE_AND_WRITE: {
10548 struct scsi_compare_and_write *cdb;
10550 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10552 *lba = scsi_8btou64(cdb->addr);
10553 *len = cdb->length;
10558 struct scsi_rw_6 *cdb;
10560 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10562 *lba = scsi_3btoul(cdb->addr);
10563 /* only 5 bits are valid in the most significant address byte */
10565 *len = cdb->length;
10570 struct scsi_rw_10 *cdb;
10572 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10574 *lba = scsi_4btoul(cdb->addr);
10575 *len = scsi_2btoul(cdb->length);
10578 case WRITE_VERIFY_10: {
10579 struct scsi_write_verify_10 *cdb;
10581 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10583 *lba = scsi_4btoul(cdb->addr);
10584 *len = scsi_2btoul(cdb->length);
10589 struct scsi_rw_12 *cdb;
10591 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10593 *lba = scsi_4btoul(cdb->addr);
10594 *len = scsi_4btoul(cdb->length);
10597 case WRITE_VERIFY_12: {
10598 struct scsi_write_verify_12 *cdb;
10600 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10602 *lba = scsi_4btoul(cdb->addr);
10603 *len = scsi_4btoul(cdb->length);
10608 case WRITE_ATOMIC_16: {
10609 struct scsi_rw_16 *cdb;
10611 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10613 *lba = scsi_8btou64(cdb->addr);
10614 *len = scsi_4btoul(cdb->length);
10617 case WRITE_VERIFY_16: {
10618 struct scsi_write_verify_16 *cdb;
10620 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10622 *lba = scsi_8btou64(cdb->addr);
10623 *len = scsi_4btoul(cdb->length);
10626 case WRITE_SAME_10: {
10627 struct scsi_write_same_10 *cdb;
10629 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10631 *lba = scsi_4btoul(cdb->addr);
10632 *len = scsi_2btoul(cdb->length);
10635 case WRITE_SAME_16: {
10636 struct scsi_write_same_16 *cdb;
10638 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10640 *lba = scsi_8btou64(cdb->addr);
10641 *len = scsi_4btoul(cdb->length);
10645 struct scsi_verify_10 *cdb;
10647 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10649 *lba = scsi_4btoul(cdb->addr);
10650 *len = scsi_2btoul(cdb->length);
10654 struct scsi_verify_12 *cdb;
10656 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10658 *lba = scsi_4btoul(cdb->addr);
10659 *len = scsi_4btoul(cdb->length);
10663 struct scsi_verify_16 *cdb;
10665 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10667 *lba = scsi_8btou64(cdb->addr);
10668 *len = scsi_4btoul(cdb->length);
10678 break; /* NOTREACHED */
10685 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2)
10687 uint64_t endlba1, endlba2;
10689 endlba1 = lba1 + len1 - 1;
10690 endlba2 = lba2 + len2 - 1;
10692 if ((endlba1 < lba2)
10693 || (endlba2 < lba1))
10694 return (CTL_ACTION_PASS);
10696 return (CTL_ACTION_BLOCK);
10700 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10702 struct ctl_ptr_len_flags *ptrlen;
10703 struct scsi_unmap_desc *buf, *end, *range;
10707 /* If not UNMAP -- go other way. */
10708 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10709 io->scsiio.cdb[0] != UNMAP)
10710 return (CTL_ACTION_ERROR);
10712 /* If UNMAP without data -- block and wait for data. */
10713 ptrlen = (struct ctl_ptr_len_flags *)
10714 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10715 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10716 ptrlen->ptr == NULL)
10717 return (CTL_ACTION_BLOCK);
10719 /* UNMAP with data -- check for collision. */
10720 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10721 end = buf + ptrlen->len / sizeof(*buf);
10722 for (range = buf; range < end; range++) {
10723 lba = scsi_8btou64(range->lba);
10724 len = scsi_4btoul(range->length);
10725 if ((lba < lba2 + len2) && (lba + len > lba2))
10726 return (CTL_ACTION_BLOCK);
10728 return (CTL_ACTION_PASS);
10732 ctl_extent_check(union ctl_io *io1, union ctl_io *io2)
10734 uint64_t lba1, lba2;
10735 uint64_t len1, len2;
10738 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10739 return (CTL_ACTION_ERROR);
10741 retval = ctl_extent_check_unmap(io2, lba1, len1);
10742 if (retval != CTL_ACTION_ERROR)
10745 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10746 return (CTL_ACTION_ERROR);
10748 return (ctl_extent_check_lba(lba1, len1, lba2, len2));
10752 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10753 union ctl_io *ooa_io)
10755 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10756 ctl_serialize_action *serialize_row;
10759 * The initiator attempted multiple untagged commands at the same
10760 * time. Can't do that.
10762 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10763 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10764 && ((pending_io->io_hdr.nexus.targ_port ==
10765 ooa_io->io_hdr.nexus.targ_port)
10766 && (pending_io->io_hdr.nexus.initid.id ==
10767 ooa_io->io_hdr.nexus.initid.id))
10768 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10769 return (CTL_ACTION_OVERLAP);
10772 * The initiator attempted to send multiple tagged commands with
10773 * the same ID. (It's fine if different initiators have the same
10776 * Even if all of those conditions are true, we don't kill the I/O
10777 * if the command ahead of us has been aborted. We won't end up
10778 * sending it to the FETD, and it's perfectly legal to resend a
10779 * command with the same tag number as long as the previous
10780 * instance of this tag number has been aborted somehow.
10782 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10783 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10784 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10785 && ((pending_io->io_hdr.nexus.targ_port ==
10786 ooa_io->io_hdr.nexus.targ_port)
10787 && (pending_io->io_hdr.nexus.initid.id ==
10788 ooa_io->io_hdr.nexus.initid.id))
10789 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10790 return (CTL_ACTION_OVERLAP_TAG);
10793 * If we get a head of queue tag, SAM-3 says that we should
10794 * immediately execute it.
10796 * What happens if this command would normally block for some other
10797 * reason? e.g. a request sense with a head of queue tag
10798 * immediately after a write. Normally that would block, but this
10799 * will result in its getting executed immediately...
10801 * We currently return "pass" instead of "skip", so we'll end up
10802 * going through the rest of the queue to check for overlapped tags.
10804 * XXX KDM check for other types of blockage first??
10806 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10807 return (CTL_ACTION_PASS);
10810 * Ordered tags have to block until all items ahead of them
10811 * have completed. If we get called with an ordered tag, we always
10812 * block, if something else is ahead of us in the queue.
10814 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10815 return (CTL_ACTION_BLOCK);
10818 * Simple tags get blocked until all head of queue and ordered tags
10819 * ahead of them have completed. I'm lumping untagged commands in
10820 * with simple tags here. XXX KDM is that the right thing to do?
10822 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10823 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10824 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10825 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10826 return (CTL_ACTION_BLOCK);
10828 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
10829 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
10831 serialize_row = ctl_serialize_table[ooa_entry->seridx];
10833 switch (serialize_row[pending_entry->seridx]) {
10834 case CTL_SER_BLOCK:
10835 return (CTL_ACTION_BLOCK);
10836 case CTL_SER_EXTENT:
10837 return (ctl_extent_check(pending_io, ooa_io));
10838 case CTL_SER_EXTENTOPT:
10839 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
10840 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
10841 return (ctl_extent_check(pending_io, ooa_io));
10844 return (CTL_ACTION_PASS);
10845 case CTL_SER_BLOCKOPT:
10846 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
10847 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
10848 return (CTL_ACTION_BLOCK);
10849 return (CTL_ACTION_PASS);
10851 return (CTL_ACTION_SKIP);
10853 panic("invalid serialization value %d",
10854 serialize_row[pending_entry->seridx]);
10857 return (CTL_ACTION_ERROR);
10861 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
10863 * - pending_io is generally either incoming, or on the blocked queue
10864 * - starting I/O is the I/O we want to start the check with.
10867 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
10868 union ctl_io *starting_io)
10870 union ctl_io *ooa_io;
10873 mtx_assert(&lun->lun_lock, MA_OWNED);
10876 * Run back along the OOA queue, starting with the current
10877 * blocked I/O and going through every I/O before it on the
10878 * queue. If starting_io is NULL, we'll just end up returning
10881 for (ooa_io = starting_io; ooa_io != NULL;
10882 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
10886 * This routine just checks to see whether
10887 * cur_blocked is blocked by ooa_io, which is ahead
10888 * of it in the queue. It doesn't queue/dequeue
10891 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
10893 case CTL_ACTION_BLOCK:
10894 case CTL_ACTION_OVERLAP:
10895 case CTL_ACTION_OVERLAP_TAG:
10896 case CTL_ACTION_SKIP:
10897 case CTL_ACTION_ERROR:
10899 break; /* NOTREACHED */
10900 case CTL_ACTION_PASS:
10903 panic("invalid action %d", action);
10904 break; /* NOTREACHED */
10908 return (CTL_ACTION_PASS);
10913 * - An I/O has just completed, and has been removed from the per-LUN OOA
10914 * queue, so some items on the blocked queue may now be unblocked.
10917 ctl_check_blocked(struct ctl_lun *lun)
10919 union ctl_io *cur_blocked, *next_blocked;
10921 mtx_assert(&lun->lun_lock, MA_OWNED);
10924 * Run forward from the head of the blocked queue, checking each
10925 * entry against the I/Os prior to it on the OOA queue to see if
10926 * there is still any blockage.
10928 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
10929 * with our removing a variable on it while it is traversing the
10932 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
10933 cur_blocked != NULL; cur_blocked = next_blocked) {
10934 union ctl_io *prev_ooa;
10937 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
10940 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
10941 ctl_ooaq, ooa_links);
10944 * If cur_blocked happens to be the first item in the OOA
10945 * queue now, prev_ooa will be NULL, and the action
10946 * returned will just be CTL_ACTION_PASS.
10948 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
10951 case CTL_ACTION_BLOCK:
10952 /* Nothing to do here, still blocked */
10954 case CTL_ACTION_OVERLAP:
10955 case CTL_ACTION_OVERLAP_TAG:
10957 * This shouldn't happen! In theory we've already
10958 * checked this command for overlap...
10961 case CTL_ACTION_PASS:
10962 case CTL_ACTION_SKIP: {
10963 struct ctl_softc *softc;
10964 const struct ctl_cmd_entry *entry;
10969 * The skip case shouldn't happen, this transaction
10970 * should have never made it onto the blocked queue.
10973 * This I/O is no longer blocked, we can remove it
10974 * from the blocked queue. Since this is a TAILQ
10975 * (doubly linked list), we can do O(1) removals
10976 * from any place on the list.
10978 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
10980 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
10982 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){
10984 * Need to send IO back to original side to
10987 union ctl_ha_msg msg_info;
10989 msg_info.hdr.original_sc =
10990 cur_blocked->io_hdr.original_sc;
10991 msg_info.hdr.serializing_sc = cur_blocked;
10992 msg_info.hdr.msg_type = CTL_MSG_R2R;
10993 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
10994 &msg_info, sizeof(msg_info), 0)) >
10995 CTL_HA_STATUS_SUCCESS) {
10996 printf("CTL:Check Blocked error from "
10997 "ctl_ha_msg_send %d\n",
11002 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
11003 softc = control_softc;
11005 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus);
11008 * Check this I/O for LUN state changes that may
11009 * have happened while this command was blocked.
11010 * The LUN state may have been changed by a command
11011 * ahead of us in the queue, so we need to re-check
11012 * for any states that can be caused by SCSI
11015 if (ctl_scsiio_lun_check(softc, lun, entry,
11016 &cur_blocked->scsiio) == 0) {
11017 cur_blocked->io_hdr.flags |=
11018 CTL_FLAG_IS_WAS_ON_RTR;
11019 ctl_enqueue_rtr(cur_blocked);
11021 ctl_done(cur_blocked);
11026 * This probably shouldn't happen -- we shouldn't
11027 * get CTL_ACTION_ERROR, or anything else.
11033 return (CTL_RETVAL_COMPLETE);
11037 * This routine (with one exception) checks LUN flags that can be set by
11038 * commands ahead of us in the OOA queue. These flags have to be checked
11039 * when a command initially comes in, and when we pull a command off the
11040 * blocked queue and are preparing to execute it. The reason we have to
11041 * check these flags for commands on the blocked queue is that the LUN
11042 * state may have been changed by a command ahead of us while we're on the
11045 * Ordering is somewhat important with these checks, so please pay
11046 * careful attention to the placement of any new checks.
11049 ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
11050 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11057 mtx_assert(&lun->lun_lock, MA_OWNED);
11060 * If this shelf is a secondary shelf controller, we have to reject
11061 * any media access commands.
11063 if ((ctl_softc->flags & CTL_FLAG_ACTIVE_SHELF) == 0 &&
11064 (entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0) {
11065 ctl_set_lun_standby(ctsio);
11070 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11071 if (lun->flags & CTL_LUN_READONLY) {
11072 ctl_set_sense(ctsio, /*current_error*/ 1,
11073 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11074 /*asc*/ 0x27, /*ascq*/ 0x01, SSD_ELEM_NONE);
11078 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT]
11079 .eca_and_aen & SCP_SWP) != 0) {
11080 ctl_set_sense(ctsio, /*current_error*/ 1,
11081 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11082 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11089 * Check for a reservation conflict. If this command isn't allowed
11090 * even on reserved LUNs, and if this initiator isn't the one who
11091 * reserved us, reject the command with a reservation conflict.
11093 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
11094 if ((lun->flags & CTL_LUN_RESERVED)
11095 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11096 if (lun->res_idx != residx) {
11097 ctl_set_reservation_conflict(ctsio);
11103 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11104 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11105 /* No reservation or command is allowed. */;
11106 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11107 (lun->res_type == SPR_TYPE_WR_EX ||
11108 lun->res_type == SPR_TYPE_WR_EX_RO ||
11109 lun->res_type == SPR_TYPE_WR_EX_AR)) {
11110 /* The command is allowed for Write Exclusive resv. */;
11113 * if we aren't registered or it's a res holder type
11114 * reservation and this isn't the res holder then set a
11117 if (lun->pr_keys[residx] == 0
11118 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
11119 ctl_set_reservation_conflict(ctsio);
11126 if ((lun->flags & CTL_LUN_OFFLINE)
11127 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) {
11128 ctl_set_lun_not_ready(ctsio);
11134 * If the LUN is stopped, see if this particular command is allowed
11135 * for a stopped lun. Otherwise, reject it with 0x04,0x02.
11137 if ((lun->flags & CTL_LUN_STOPPED)
11138 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
11139 /* "Logical unit not ready, initializing cmd. required" */
11140 ctl_set_lun_stopped(ctsio);
11145 if ((lun->flags & CTL_LUN_INOPERABLE)
11146 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
11147 /* "Medium format corrupted" */
11148 ctl_set_medium_format_corrupted(ctsio);
11159 ctl_failover_io(union ctl_io *io, int have_lock)
11161 ctl_set_busy(&io->scsiio);
11168 struct ctl_lun *lun;
11169 struct ctl_softc *ctl_softc;
11170 union ctl_io *next_io, *pending_io;
11175 ctl_softc = control_softc;
11177 mtx_lock(&ctl_softc->ctl_lock);
11179 * Remove any cmds from the other SC from the rtr queue. These
11180 * will obviously only be for LUNs for which we're the primary.
11181 * We can't send status or get/send data for these commands.
11182 * Since they haven't been executed yet, we can just remove them.
11183 * We'll either abort them or delete them below, depending on
11184 * which HA mode we're in.
11187 mtx_lock(&ctl_softc->queue_lock);
11188 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue);
11189 io != NULL; io = next_io) {
11190 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
11191 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
11192 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr,
11193 ctl_io_hdr, links);
11195 mtx_unlock(&ctl_softc->queue_lock);
11198 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) {
11199 lun = ctl_softc->ctl_luns[lun_idx];
11204 * Processor LUNs are primary on both sides.
11205 * XXX will this always be true?
11207 if (lun->be_lun->lun_type == T_PROCESSOR)
11210 if ((lun->flags & CTL_LUN_PRIMARY_SC)
11211 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
11212 printf("FAILOVER: primary lun %d\n", lun_idx);
11214 * Remove all commands from the other SC. First from the
11215 * blocked queue then from the ooa queue. Once we have
11216 * removed them. Call ctl_check_blocked to see if there
11217 * is anything that can run.
11219 for (io = (union ctl_io *)TAILQ_FIRST(
11220 &lun->blocked_queue); io != NULL; io = next_io) {
11222 next_io = (union ctl_io *)TAILQ_NEXT(
11223 &io->io_hdr, blocked_links);
11225 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
11226 TAILQ_REMOVE(&lun->blocked_queue,
11227 &io->io_hdr,blocked_links);
11228 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11229 TAILQ_REMOVE(&lun->ooa_queue,
11230 &io->io_hdr, ooa_links);
11236 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
11237 io != NULL; io = next_io) {
11239 next_io = (union ctl_io *)TAILQ_NEXT(
11240 &io->io_hdr, ooa_links);
11242 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
11244 TAILQ_REMOVE(&lun->ooa_queue,
11251 ctl_check_blocked(lun);
11252 } else if ((lun->flags & CTL_LUN_PRIMARY_SC)
11253 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
11255 printf("FAILOVER: primary lun %d\n", lun_idx);
11257 * Abort all commands from the other SC. We can't
11258 * send status back for them now. These should get
11259 * cleaned up when they are completed or come out
11260 * for a datamove operation.
11262 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
11263 io != NULL; io = next_io) {
11264 next_io = (union ctl_io *)TAILQ_NEXT(
11265 &io->io_hdr, ooa_links);
11267 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
11268 io->io_hdr.flags |= CTL_FLAG_ABORT;
11270 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
11271 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
11273 printf("FAILOVER: secondary lun %d\n", lun_idx);
11275 lun->flags |= CTL_LUN_PRIMARY_SC;
11278 * We send all I/O that was sent to this controller
11279 * and redirected to the other side back with
11280 * busy status, and have the initiator retry it.
11281 * Figuring out how much data has been transferred,
11282 * etc. and picking up where we left off would be
11285 * XXX KDM need to remove I/O from the blocked
11288 for (pending_io = (union ctl_io *)TAILQ_FIRST(
11289 &lun->ooa_queue); pending_io != NULL;
11290 pending_io = next_io) {
11292 next_io = (union ctl_io *)TAILQ_NEXT(
11293 &pending_io->io_hdr, ooa_links);
11295 pending_io->io_hdr.flags &=
11296 ~CTL_FLAG_SENT_2OTHER_SC;
11298 if (pending_io->io_hdr.flags &
11299 CTL_FLAG_IO_ACTIVE) {
11300 pending_io->io_hdr.flags |=
11303 ctl_set_busy(&pending_io->scsiio);
11304 ctl_done(pending_io);
11309 * Build Unit Attention
11311 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11312 lun->pending_ua[i] |=
11313 CTL_UA_ASYM_ACC_CHANGE;
11315 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
11316 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
11317 printf("FAILOVER: secondary lun %d\n", lun_idx);
11319 * if the first io on the OOA is not on the RtR queue
11322 lun->flags |= CTL_LUN_PRIMARY_SC;
11324 pending_io = (union ctl_io *)TAILQ_FIRST(
11326 if (pending_io==NULL) {
11327 printf("Nothing on OOA queue\n");
11331 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11332 if ((pending_io->io_hdr.flags &
11333 CTL_FLAG_IS_WAS_ON_RTR) == 0) {
11334 pending_io->io_hdr.flags |=
11335 CTL_FLAG_IS_WAS_ON_RTR;
11336 ctl_enqueue_rtr(pending_io);
11341 printf("Tag 0x%04x is running\n",
11342 pending_io->scsiio.tag_num);
11346 next_io = (union ctl_io *)TAILQ_NEXT(
11347 &pending_io->io_hdr, ooa_links);
11348 for (pending_io=next_io; pending_io != NULL;
11349 pending_io = next_io) {
11350 pending_io->io_hdr.flags &=
11351 ~CTL_FLAG_SENT_2OTHER_SC;
11352 next_io = (union ctl_io *)TAILQ_NEXT(
11353 &pending_io->io_hdr, ooa_links);
11354 if (pending_io->io_hdr.flags &
11355 CTL_FLAG_IS_WAS_ON_RTR) {
11357 printf("Tag 0x%04x is running\n",
11358 pending_io->scsiio.tag_num);
11363 switch (ctl_check_ooa(lun, pending_io,
11364 (union ctl_io *)TAILQ_PREV(
11365 &pending_io->io_hdr, ctl_ooaq,
11368 case CTL_ACTION_BLOCK:
11369 TAILQ_INSERT_TAIL(&lun->blocked_queue,
11370 &pending_io->io_hdr,
11372 pending_io->io_hdr.flags |=
11375 case CTL_ACTION_PASS:
11376 case CTL_ACTION_SKIP:
11377 pending_io->io_hdr.flags |=
11378 CTL_FLAG_IS_WAS_ON_RTR;
11379 ctl_enqueue_rtr(pending_io);
11381 case CTL_ACTION_OVERLAP:
11382 ctl_set_overlapped_cmd(
11383 (struct ctl_scsiio *)pending_io);
11384 ctl_done(pending_io);
11386 case CTL_ACTION_OVERLAP_TAG:
11387 ctl_set_overlapped_tag(
11388 (struct ctl_scsiio *)pending_io,
11389 pending_io->scsiio.tag_num & 0xff);
11390 ctl_done(pending_io);
11392 case CTL_ACTION_ERROR:
11394 ctl_set_internal_failure(
11395 (struct ctl_scsiio *)pending_io,
11398 ctl_done(pending_io);
11404 * Build Unit Attention
11406 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11407 lun->pending_ua[i] |=
11408 CTL_UA_ASYM_ACC_CHANGE;
11411 panic("Unhandled HA mode failover, LUN flags = %#x, "
11412 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode);
11416 mtx_unlock(&ctl_softc->ctl_lock);
11420 ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio)
11422 struct ctl_lun *lun;
11423 const struct ctl_cmd_entry *entry;
11424 uint32_t initidx, targ_lun;
11431 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11432 if ((targ_lun < CTL_MAX_LUNS)
11433 && ((lun = ctl_softc->ctl_luns[targ_lun]) != NULL)) {
11435 * If the LUN is invalid, pretend that it doesn't exist.
11436 * It will go away as soon as all pending I/O has been
11439 mtx_lock(&lun->lun_lock);
11440 if (lun->flags & CTL_LUN_DISABLED) {
11441 mtx_unlock(&lun->lun_lock);
11443 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11444 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11446 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
11447 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
11449 if (lun->be_lun->lun_type == T_PROCESSOR) {
11450 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV;
11454 * Every I/O goes into the OOA queue for a
11455 * particular LUN, and stays there until completion.
11457 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr,
11461 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11462 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11465 /* Get command entry and return error if it is unsuppotyed. */
11466 entry = ctl_validate_command(ctsio);
11467 if (entry == NULL) {
11469 mtx_unlock(&lun->lun_lock);
11473 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11474 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11477 * Check to see whether we can send this command to LUNs that don't
11478 * exist. This should pretty much only be the case for inquiry
11479 * and request sense. Further checks, below, really require having
11480 * a LUN, so we can't really check the command anymore. Just put
11481 * it on the rtr queue.
11484 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) {
11485 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11486 ctl_enqueue_rtr((union ctl_io *)ctsio);
11490 ctl_set_unsupported_lun(ctsio);
11491 ctl_done((union ctl_io *)ctsio);
11492 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11496 * Make sure we support this particular command on this LUN.
11497 * e.g., we don't support writes to the control LUN.
11499 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11500 mtx_unlock(&lun->lun_lock);
11501 ctl_set_invalid_opcode(ctsio);
11502 ctl_done((union ctl_io *)ctsio);
11507 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11511 * If we've got a request sense, it'll clear the contingent
11512 * allegiance condition. Otherwise, if we have a CA condition for
11513 * this initiator, clear it, because it sent down a command other
11514 * than request sense.
11516 if ((ctsio->cdb[0] != REQUEST_SENSE)
11517 && (ctl_is_set(lun->have_ca, initidx)))
11518 ctl_clear_mask(lun->have_ca, initidx);
11522 * If the command has this flag set, it handles its own unit
11523 * attention reporting, we shouldn't do anything. Otherwise we
11524 * check for any pending unit attentions, and send them back to the
11525 * initiator. We only do this when a command initially comes in,
11526 * not when we pull it off the blocked queue.
11528 * According to SAM-3, section 5.3.2, the order that things get
11529 * presented back to the host is basically unit attentions caused
11530 * by some sort of reset event, busy status, reservation conflicts
11531 * or task set full, and finally any other status.
11533 * One issue here is that some of the unit attentions we report
11534 * don't fall into the "reset" category (e.g. "reported luns data
11535 * has changed"). So reporting it here, before the reservation
11536 * check, may be technically wrong. I guess the only thing to do
11537 * would be to check for and report the reset events here, and then
11538 * check for the other unit attention types after we check for a
11539 * reservation conflict.
11541 * XXX KDM need to fix this
11543 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11544 ctl_ua_type ua_type;
11546 if (lun->pending_ua[initidx] != CTL_UA_NONE) {
11547 scsi_sense_data_type sense_format;
11550 sense_format = (lun->flags &
11551 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC :
11554 sense_format = SSD_TYPE_FIXED;
11556 ua_type = ctl_build_ua(&lun->pending_ua[initidx],
11557 &ctsio->sense_data, sense_format);
11558 if (ua_type != CTL_UA_NONE) {
11559 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11560 ctsio->io_hdr.status = CTL_SCSI_ERROR |
11562 ctsio->sense_len = SSD_FULL_SIZE;
11563 mtx_unlock(&lun->lun_lock);
11564 ctl_done((union ctl_io *)ctsio);
11571 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) {
11572 mtx_unlock(&lun->lun_lock);
11573 ctl_done((union ctl_io *)ctsio);
11578 * XXX CHD this is where we want to send IO to other side if
11579 * this LUN is secondary on this SC. We will need to make a copy
11580 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11581 * the copy we send as FROM_OTHER.
11582 * We also need to stuff the address of the original IO so we can
11583 * find it easily. Something similar will need be done on the other
11584 * side so when we are done we can find the copy.
11586 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11587 union ctl_ha_msg msg_info;
11590 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11592 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11593 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11595 printf("1. ctsio %p\n", ctsio);
11597 msg_info.hdr.serializing_sc = NULL;
11598 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11599 msg_info.scsi.tag_num = ctsio->tag_num;
11600 msg_info.scsi.tag_type = ctsio->tag_type;
11601 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11603 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11605 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11606 (void *)&msg_info, sizeof(msg_info), 0)) >
11607 CTL_HA_STATUS_SUCCESS) {
11608 printf("CTL:precheck, ctl_ha_msg_send returned %d\n",
11610 printf("CTL:opcode is %x\n", ctsio->cdb[0]);
11613 printf("CTL:Precheck sent msg, opcode is %x\n",opcode);
11618 * XXX KDM this I/O is off the incoming queue, but hasn't
11619 * been inserted on any other queue. We may need to come
11620 * up with a holding queue while we wait for serialization
11621 * so that we have an idea of what we're waiting for from
11624 mtx_unlock(&lun->lun_lock);
11628 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11629 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11630 ctl_ooaq, ooa_links))) {
11631 case CTL_ACTION_BLOCK:
11632 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11633 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11635 mtx_unlock(&lun->lun_lock);
11637 case CTL_ACTION_PASS:
11638 case CTL_ACTION_SKIP:
11639 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11640 mtx_unlock(&lun->lun_lock);
11641 ctl_enqueue_rtr((union ctl_io *)ctsio);
11643 case CTL_ACTION_OVERLAP:
11644 mtx_unlock(&lun->lun_lock);
11645 ctl_set_overlapped_cmd(ctsio);
11646 ctl_done((union ctl_io *)ctsio);
11648 case CTL_ACTION_OVERLAP_TAG:
11649 mtx_unlock(&lun->lun_lock);
11650 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11651 ctl_done((union ctl_io *)ctsio);
11653 case CTL_ACTION_ERROR:
11655 mtx_unlock(&lun->lun_lock);
11656 ctl_set_internal_failure(ctsio,
11658 /*retry_count*/ 0);
11659 ctl_done((union ctl_io *)ctsio);
11665 const struct ctl_cmd_entry *
11666 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11668 const struct ctl_cmd_entry *entry;
11669 int service_action;
11671 entry = &ctl_cmd_table[ctsio->cdb[0]];
11673 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11674 if (entry->flags & CTL_CMD_FLAG_SA5) {
11675 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11676 entry = &((const struct ctl_cmd_entry *)
11677 entry->execute)[service_action];
11682 const struct ctl_cmd_entry *
11683 ctl_validate_command(struct ctl_scsiio *ctsio)
11685 const struct ctl_cmd_entry *entry;
11689 entry = ctl_get_cmd_entry(ctsio, &sa);
11690 if (entry->execute == NULL) {
11692 ctl_set_invalid_field(ctsio,
11699 ctl_set_invalid_opcode(ctsio);
11700 ctl_done((union ctl_io *)ctsio);
11703 KASSERT(entry->length > 0,
11704 ("Not defined length for command 0x%02x/0x%02x",
11705 ctsio->cdb[0], ctsio->cdb[1]));
11706 for (i = 1; i < entry->length; i++) {
11707 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11710 ctl_set_invalid_field(ctsio,
11715 /*bit*/ fls(diff) - 1);
11716 ctl_done((union ctl_io *)ctsio);
11723 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11726 switch (lun_type) {
11728 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) &&
11729 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11733 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) &&
11734 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11744 ctl_scsiio(struct ctl_scsiio *ctsio)
11747 const struct ctl_cmd_entry *entry;
11749 retval = CTL_RETVAL_COMPLETE;
11751 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11753 entry = ctl_get_cmd_entry(ctsio, NULL);
11756 * If this I/O has been aborted, just send it straight to
11757 * ctl_done() without executing it.
11759 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11760 ctl_done((union ctl_io *)ctsio);
11765 * All the checks should have been handled by ctl_scsiio_precheck().
11766 * We should be clear now to just execute the I/O.
11768 retval = entry->execute(ctsio);
11775 * Since we only implement one target right now, a bus reset simply resets
11776 * our single target.
11779 ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io)
11781 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET));
11785 ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
11786 ctl_ua_type ua_type)
11788 struct ctl_lun *lun;
11791 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11792 union ctl_ha_msg msg_info;
11794 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11795 msg_info.hdr.nexus = io->io_hdr.nexus;
11796 if (ua_type==CTL_UA_TARG_RESET)
11797 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11799 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11800 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11801 msg_info.hdr.original_sc = NULL;
11802 msg_info.hdr.serializing_sc = NULL;
11803 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11804 (void *)&msg_info, sizeof(msg_info), 0)) {
11809 mtx_lock(&ctl_softc->ctl_lock);
11810 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links)
11811 retval += ctl_lun_reset(lun, io, ua_type);
11812 mtx_unlock(&ctl_softc->ctl_lock);
11818 * The LUN should always be set. The I/O is optional, and is used to
11819 * distinguish between I/Os sent by this initiator, and by other
11820 * initiators. We set unit attention for initiators other than this one.
11821 * SAM-3 is vague on this point. It does say that a unit attention should
11822 * be established for other initiators when a LUN is reset (see section
11823 * 5.7.3), but it doesn't specifically say that the unit attention should
11824 * be established for this particular initiator when a LUN is reset. Here
11825 * is the relevant text, from SAM-3 rev 8:
11827 * 5.7.2 When a SCSI initiator port aborts its own tasks
11829 * When a SCSI initiator port causes its own task(s) to be aborted, no
11830 * notification that the task(s) have been aborted shall be returned to
11831 * the SCSI initiator port other than the completion response for the
11832 * command or task management function action that caused the task(s) to
11833 * be aborted and notification(s) associated with related effects of the
11834 * action (e.g., a reset unit attention condition).
11836 * XXX KDM for now, we're setting unit attention for all initiators.
11839 ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11843 uint32_t initindex;
11847 mtx_lock(&lun->lun_lock);
11849 * Run through the OOA queue and abort each I/O.
11852 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
11854 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11855 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11856 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11860 * This version sets unit attention for every
11863 initindex = ctl_get_initindex(&io->io_hdr.nexus);
11864 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11865 if (initindex == i)
11867 lun->pending_ua[i] |= ua_type;
11872 * A reset (any kind, really) clears reservations established with
11873 * RESERVE/RELEASE. It does not clear reservations established
11874 * with PERSISTENT RESERVE OUT, but we don't support that at the
11875 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
11876 * reservations made with the RESERVE/RELEASE commands, because
11877 * those commands are obsolete in SPC-3.
11879 lun->flags &= ~CTL_LUN_RESERVED;
11881 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11883 ctl_clear_mask(lun->have_ca, i);
11885 lun->pending_ua[i] |= ua_type;
11887 mtx_unlock(&lun->lun_lock);
11893 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11898 mtx_assert(&lun->lun_lock, MA_OWNED);
11901 * Run through the OOA queue and attempt to find the given I/O.
11902 * The target port, initiator ID, tag type and tag number have to
11903 * match the values that we got from the initiator. If we have an
11904 * untagged command to abort, simply abort the first untagged command
11905 * we come to. We only allow one untagged command at a time of course.
11907 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11908 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11910 if ((targ_port == UINT32_MAX ||
11911 targ_port == xio->io_hdr.nexus.targ_port) &&
11912 (init_id == UINT32_MAX ||
11913 init_id == xio->io_hdr.nexus.initid.id)) {
11914 if (targ_port != xio->io_hdr.nexus.targ_port ||
11915 init_id != xio->io_hdr.nexus.initid.id)
11916 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
11917 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11918 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11919 union ctl_ha_msg msg_info;
11921 msg_info.hdr.nexus = xio->io_hdr.nexus;
11922 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11923 msg_info.task.tag_num = xio->scsiio.tag_num;
11924 msg_info.task.tag_type = xio->scsiio.tag_type;
11925 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11926 msg_info.hdr.original_sc = NULL;
11927 msg_info.hdr.serializing_sc = NULL;
11928 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11929 (void *)&msg_info, sizeof(msg_info), 0);
11936 ctl_abort_task_set(union ctl_io *io)
11938 struct ctl_softc *softc = control_softc;
11939 struct ctl_lun *lun;
11945 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11946 mtx_lock(&softc->ctl_lock);
11947 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL))
11948 lun = softc->ctl_luns[targ_lun];
11950 mtx_unlock(&softc->ctl_lock);
11954 mtx_lock(&lun->lun_lock);
11955 mtx_unlock(&softc->ctl_lock);
11956 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
11957 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11958 io->io_hdr.nexus.initid.id,
11959 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11960 } else { /* CTL_TASK_CLEAR_TASK_SET */
11961 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
11962 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11964 mtx_unlock(&lun->lun_lock);
11969 ctl_i_t_nexus_reset(union ctl_io *io)
11971 struct ctl_softc *softc = control_softc;
11972 struct ctl_lun *lun;
11973 uint32_t initindex, residx;
11975 initindex = ctl_get_initindex(&io->io_hdr.nexus);
11976 residx = ctl_get_resindex(&io->io_hdr.nexus);
11977 mtx_lock(&softc->ctl_lock);
11978 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11979 mtx_lock(&lun->lun_lock);
11980 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11981 io->io_hdr.nexus.initid.id,
11982 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11984 ctl_clear_mask(lun->have_ca, initindex);
11986 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
11987 lun->flags &= ~CTL_LUN_RESERVED;
11988 lun->pending_ua[initindex] |= CTL_UA_I_T_NEXUS_LOSS;
11989 mtx_unlock(&lun->lun_lock);
11991 mtx_unlock(&softc->ctl_lock);
11996 ctl_abort_task(union ctl_io *io)
11999 struct ctl_lun *lun;
12000 struct ctl_softc *ctl_softc;
12003 char printbuf[128];
12008 ctl_softc = control_softc;
12014 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12015 mtx_lock(&ctl_softc->ctl_lock);
12016 if ((targ_lun < CTL_MAX_LUNS)
12017 && (ctl_softc->ctl_luns[targ_lun] != NULL))
12018 lun = ctl_softc->ctl_luns[targ_lun];
12020 mtx_unlock(&ctl_softc->ctl_lock);
12025 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
12026 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
12029 mtx_lock(&lun->lun_lock);
12030 mtx_unlock(&ctl_softc->ctl_lock);
12032 * Run through the OOA queue and attempt to find the given I/O.
12033 * The target port, initiator ID, tag type and tag number have to
12034 * match the values that we got from the initiator. If we have an
12035 * untagged command to abort, simply abort the first untagged command
12036 * we come to. We only allow one untagged command at a time of course.
12039 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
12041 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12042 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12044 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
12046 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
12047 lun->lun, xio->scsiio.tag_num,
12048 xio->scsiio.tag_type,
12049 (xio->io_hdr.blocked_links.tqe_prev
12050 == NULL) ? "" : " BLOCKED",
12051 (xio->io_hdr.flags &
12052 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
12053 (xio->io_hdr.flags &
12054 CTL_FLAG_ABORT) ? " ABORT" : "",
12055 (xio->io_hdr.flags &
12056 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
12057 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
12059 printf("%s\n", sbuf_data(&sb));
12062 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port)
12063 && (xio->io_hdr.nexus.initid.id ==
12064 io->io_hdr.nexus.initid.id)) {
12066 * If the abort says that the task is untagged, the
12067 * task in the queue must be untagged. Otherwise,
12068 * we just check to see whether the tag numbers
12069 * match. This is because the QLogic firmware
12070 * doesn't pass back the tag type in an abort
12074 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12075 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12076 || (xio->scsiio.tag_num == io->taskio.tag_num)) {
12079 * XXX KDM we've got problems with FC, because it
12080 * doesn't send down a tag type with aborts. So we
12081 * can only really go by the tag number...
12082 * This may cause problems with parallel SCSI.
12083 * Need to figure that out!!
12085 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12086 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12088 if ((io->io_hdr.flags &
12089 CTL_FLAG_FROM_OTHER_SC) == 0 &&
12090 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12091 union ctl_ha_msg msg_info;
12093 io->io_hdr.flags |=
12094 CTL_FLAG_SENT_2OTHER_SC;
12095 msg_info.hdr.nexus = io->io_hdr.nexus;
12096 msg_info.task.task_action =
12097 CTL_TASK_ABORT_TASK;
12098 msg_info.task.tag_num =
12099 io->taskio.tag_num;
12100 msg_info.task.tag_type =
12101 io->taskio.tag_type;
12102 msg_info.hdr.msg_type =
12103 CTL_MSG_MANAGE_TASKS;
12104 msg_info.hdr.original_sc = NULL;
12105 msg_info.hdr.serializing_sc = NULL;
12107 printf("Sent Abort to other side\n");
12109 if (CTL_HA_STATUS_SUCCESS !=
12110 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12112 sizeof(msg_info), 0)) {
12116 printf("ctl_abort_task: found I/O to abort\n");
12122 mtx_unlock(&lun->lun_lock);
12126 * This isn't really an error. It's entirely possible for
12127 * the abort and command completion to cross on the wire.
12128 * This is more of an informative/diagnostic error.
12131 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
12132 "%d:%d:%d:%d tag %d type %d\n",
12133 io->io_hdr.nexus.initid.id,
12134 io->io_hdr.nexus.targ_port,
12135 io->io_hdr.nexus.targ_target.id,
12136 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
12137 io->taskio.tag_type);
12144 ctl_run_task(union ctl_io *io)
12146 struct ctl_softc *ctl_softc = control_softc;
12148 const char *task_desc;
12150 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12152 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12153 ("ctl_run_task: Unextected io_type %d\n",
12154 io->io_hdr.io_type));
12156 task_desc = ctl_scsi_task_string(&io->taskio);
12157 if (task_desc != NULL) {
12159 csevent_log(CSC_CTL | CSC_SHELF_SW |
12161 csevent_LogType_Trace,
12162 csevent_Severity_Information,
12163 csevent_AlertLevel_Green,
12164 csevent_FRU_Firmware,
12165 csevent_FRU_Unknown,
12166 "CTL: received task: %s",task_desc);
12170 csevent_log(CSC_CTL | CSC_SHELF_SW |
12172 csevent_LogType_Trace,
12173 csevent_Severity_Information,
12174 csevent_AlertLevel_Green,
12175 csevent_FRU_Firmware,
12176 csevent_FRU_Unknown,
12177 "CTL: received unknown task "
12179 io->taskio.task_action,
12180 io->taskio.task_action);
12183 switch (io->taskio.task_action) {
12184 case CTL_TASK_ABORT_TASK:
12185 retval = ctl_abort_task(io);
12187 case CTL_TASK_ABORT_TASK_SET:
12188 case CTL_TASK_CLEAR_TASK_SET:
12189 retval = ctl_abort_task_set(io);
12191 case CTL_TASK_CLEAR_ACA:
12193 case CTL_TASK_I_T_NEXUS_RESET:
12194 retval = ctl_i_t_nexus_reset(io);
12196 case CTL_TASK_LUN_RESET: {
12197 struct ctl_lun *lun;
12200 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12201 mtx_lock(&ctl_softc->ctl_lock);
12202 if ((targ_lun < CTL_MAX_LUNS)
12203 && (ctl_softc->ctl_luns[targ_lun] != NULL))
12204 lun = ctl_softc->ctl_luns[targ_lun];
12206 mtx_unlock(&ctl_softc->ctl_lock);
12211 if (!(io->io_hdr.flags &
12212 CTL_FLAG_FROM_OTHER_SC)) {
12213 union ctl_ha_msg msg_info;
12215 io->io_hdr.flags |=
12216 CTL_FLAG_SENT_2OTHER_SC;
12217 msg_info.hdr.msg_type =
12218 CTL_MSG_MANAGE_TASKS;
12219 msg_info.hdr.nexus = io->io_hdr.nexus;
12220 msg_info.task.task_action =
12221 CTL_TASK_LUN_RESET;
12222 msg_info.hdr.original_sc = NULL;
12223 msg_info.hdr.serializing_sc = NULL;
12224 if (CTL_HA_STATUS_SUCCESS !=
12225 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12227 sizeof(msg_info), 0)) {
12231 retval = ctl_lun_reset(lun, io,
12233 mtx_unlock(&ctl_softc->ctl_lock);
12236 case CTL_TASK_TARGET_RESET:
12237 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET);
12239 case CTL_TASK_BUS_RESET:
12240 retval = ctl_bus_reset(ctl_softc, io);
12242 case CTL_TASK_PORT_LOGIN:
12244 case CTL_TASK_PORT_LOGOUT:
12247 printf("ctl_run_task: got unknown task management event %d\n",
12248 io->taskio.task_action);
12252 io->io_hdr.status = CTL_SUCCESS;
12254 io->io_hdr.status = CTL_ERROR;
12259 * For HA operation. Handle commands that come in from the other
12263 ctl_handle_isc(union ctl_io *io)
12266 struct ctl_lun *lun;
12267 struct ctl_softc *ctl_softc;
12270 ctl_softc = control_softc;
12272 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12273 lun = ctl_softc->ctl_luns[targ_lun];
12275 switch (io->io_hdr.msg_type) {
12276 case CTL_MSG_SERIALIZE:
12277 free_io = ctl_serialize_other_sc_cmd(&io->scsiio);
12279 case CTL_MSG_R2R: {
12280 const struct ctl_cmd_entry *entry;
12283 * This is only used in SER_ONLY mode.
12286 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12287 mtx_lock(&lun->lun_lock);
12288 if (ctl_scsiio_lun_check(ctl_softc, lun,
12289 entry, (struct ctl_scsiio *)io) != 0) {
12290 mtx_unlock(&lun->lun_lock);
12294 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12295 mtx_unlock(&lun->lun_lock);
12296 ctl_enqueue_rtr(io);
12299 case CTL_MSG_FINISH_IO:
12300 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
12305 mtx_lock(&lun->lun_lock);
12306 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
12308 ctl_check_blocked(lun);
12309 mtx_unlock(&lun->lun_lock);
12312 case CTL_MSG_PERS_ACTION:
12313 ctl_hndl_per_res_out_on_other_sc(
12314 (union ctl_ha_msg *)&io->presio.pr_msg);
12317 case CTL_MSG_BAD_JUJU:
12321 case CTL_MSG_DATAMOVE:
12322 /* Only used in XFER mode */
12324 ctl_datamove_remote(io);
12326 case CTL_MSG_DATAMOVE_DONE:
12327 /* Only used in XFER mode */
12329 io->scsiio.be_move_done(io);
12333 printf("%s: Invalid message type %d\n",
12334 __func__, io->io_hdr.msg_type);
12344 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12345 * there is no match.
12347 static ctl_lun_error_pattern
12348 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12350 const struct ctl_cmd_entry *entry;
12351 ctl_lun_error_pattern filtered_pattern, pattern;
12353 pattern = desc->error_pattern;
12356 * XXX KDM we need more data passed into this function to match a
12357 * custom pattern, and we actually need to implement custom pattern
12360 if (pattern & CTL_LUN_PAT_CMD)
12361 return (CTL_LUN_PAT_CMD);
12363 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12364 return (CTL_LUN_PAT_ANY);
12366 entry = ctl_get_cmd_entry(ctsio, NULL);
12368 filtered_pattern = entry->pattern & pattern;
12371 * If the user requested specific flags in the pattern (e.g.
12372 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12375 * If the user did not specify any flags, it doesn't matter whether
12376 * or not the command supports the flags.
12378 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12379 (pattern & ~CTL_LUN_PAT_MASK))
12380 return (CTL_LUN_PAT_NONE);
12383 * If the user asked for a range check, see if the requested LBA
12384 * range overlaps with this command's LBA range.
12386 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12392 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12394 return (CTL_LUN_PAT_NONE);
12396 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12397 desc->lba_range.len);
12399 * A "pass" means that the LBA ranges don't overlap, so
12400 * this doesn't match the user's range criteria.
12402 if (action == CTL_ACTION_PASS)
12403 return (CTL_LUN_PAT_NONE);
12406 return (filtered_pattern);
12410 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12412 struct ctl_error_desc *desc, *desc2;
12414 mtx_assert(&lun->lun_lock, MA_OWNED);
12416 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12417 ctl_lun_error_pattern pattern;
12419 * Check to see whether this particular command matches
12420 * the pattern in the descriptor.
12422 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12423 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12426 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12427 case CTL_LUN_INJ_ABORTED:
12428 ctl_set_aborted(&io->scsiio);
12430 case CTL_LUN_INJ_MEDIUM_ERR:
12431 ctl_set_medium_error(&io->scsiio);
12433 case CTL_LUN_INJ_UA:
12434 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12436 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12438 case CTL_LUN_INJ_CUSTOM:
12440 * We're assuming the user knows what he is doing.
12441 * Just copy the sense information without doing
12444 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12445 ctl_min(sizeof(desc->custom_sense),
12446 sizeof(io->scsiio.sense_data)));
12447 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12448 io->scsiio.sense_len = SSD_FULL_SIZE;
12449 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12451 case CTL_LUN_INJ_NONE:
12454 * If this is an error injection type we don't know
12455 * about, clear the continuous flag (if it is set)
12456 * so it will get deleted below.
12458 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12462 * By default, each error injection action is a one-shot
12464 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12467 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12473 #ifdef CTL_IO_DELAY
12475 ctl_datamove_timer_wakeup(void *arg)
12479 io = (union ctl_io *)arg;
12483 #endif /* CTL_IO_DELAY */
12486 ctl_datamove(union ctl_io *io)
12488 void (*fe_datamove)(union ctl_io *io);
12490 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12492 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12495 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12500 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12501 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12503 sbuf_cat(&sb, path_str);
12504 switch (io->io_hdr.io_type) {
12506 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12507 sbuf_printf(&sb, "\n");
12508 sbuf_cat(&sb, path_str);
12509 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12510 io->scsiio.tag_num, io->scsiio.tag_type);
12513 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12514 "Tag Type: %d\n", io->taskio.task_action,
12515 io->taskio.tag_num, io->taskio.tag_type);
12518 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12519 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12522 sbuf_cat(&sb, path_str);
12523 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12524 (intmax_t)time_uptime - io->io_hdr.start_time);
12526 printf("%s", sbuf_data(&sb));
12528 #endif /* CTL_TIME_IO */
12530 #ifdef CTL_IO_DELAY
12531 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12532 struct ctl_lun *lun;
12534 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12536 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12538 struct ctl_lun *lun;
12540 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12542 && (lun->delay_info.datamove_delay > 0)) {
12543 struct callout *callout;
12545 callout = (struct callout *)&io->io_hdr.timer_bytes;
12546 callout_init(callout, /*mpsafe*/ 1);
12547 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12548 callout_reset(callout,
12549 lun->delay_info.datamove_delay * hz,
12550 ctl_datamove_timer_wakeup, io);
12551 if (lun->delay_info.datamove_type ==
12552 CTL_DELAY_TYPE_ONESHOT)
12553 lun->delay_info.datamove_delay = 0;
12560 * This command has been aborted. Set the port status, so we fail
12563 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12564 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n",
12565 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id,
12566 io->io_hdr.nexus.targ_port,
12567 (uintmax_t)io->io_hdr.nexus.targ_target.id,
12568 io->io_hdr.nexus.targ_lun);
12569 io->io_hdr.port_status = 31337;
12571 * Note that the backend, in this case, will get the
12572 * callback in its context. In other cases it may get
12573 * called in the frontend's interrupt thread context.
12575 io->scsiio.be_move_done(io);
12579 /* Don't confuse frontend with zero length data move. */
12580 if (io->scsiio.kern_data_len == 0) {
12581 io->scsiio.be_move_done(io);
12586 * If we're in XFER mode and this I/O is from the other shelf
12587 * controller, we need to send the DMA to the other side to
12588 * actually transfer the data to/from the host. In serialize only
12589 * mode the transfer happens below CTL and ctl_datamove() is only
12590 * called on the machine that originally received the I/O.
12592 if ((control_softc->ha_mode == CTL_HA_MODE_XFER)
12593 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12594 union ctl_ha_msg msg;
12595 uint32_t sg_entries_sent;
12599 memset(&msg, 0, sizeof(msg));
12600 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
12601 msg.hdr.original_sc = io->io_hdr.original_sc;
12602 msg.hdr.serializing_sc = io;
12603 msg.hdr.nexus = io->io_hdr.nexus;
12604 msg.dt.flags = io->io_hdr.flags;
12606 * We convert everything into a S/G list here. We can't
12607 * pass by reference, only by value between controllers.
12608 * So we can't pass a pointer to the S/G list, only as many
12609 * S/G entries as we can fit in here. If it's possible for
12610 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
12611 * then we need to break this up into multiple transfers.
12613 if (io->scsiio.kern_sg_entries == 0) {
12614 msg.dt.kern_sg_entries = 1;
12616 * If this is in cached memory, flush the cache
12617 * before we send the DMA request to the other
12618 * controller. We want to do this in either the
12619 * read or the write case. The read case is
12620 * straightforward. In the write case, we want to
12621 * make sure nothing is in the local cache that
12622 * could overwrite the DMAed data.
12624 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12626 * XXX KDM use bus_dmamap_sync() here.
12631 * Convert to a physical address if this is a
12634 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
12635 msg.dt.sg_list[0].addr =
12636 io->scsiio.kern_data_ptr;
12639 * XXX KDM use busdma here!
12642 msg.dt.sg_list[0].addr = (void *)
12643 vtophys(io->scsiio.kern_data_ptr);
12647 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
12650 struct ctl_sg_entry *sgl;
12653 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
12654 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
12655 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12657 * XXX KDM use bus_dmamap_sync() here.
12662 msg.dt.kern_data_len = io->scsiio.kern_data_len;
12663 msg.dt.kern_total_len = io->scsiio.kern_total_len;
12664 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
12665 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
12666 msg.dt.sg_sequence = 0;
12669 * Loop until we've sent all of the S/G entries. On the
12670 * other end, we'll recompose these S/G entries into one
12671 * contiguous list before passing it to the
12673 for (sg_entries_sent = 0; sg_entries_sent <
12674 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) {
12675 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/
12676 sizeof(msg.dt.sg_list[0])),
12677 msg.dt.kern_sg_entries - sg_entries_sent);
12679 if (do_sg_copy != 0) {
12680 struct ctl_sg_entry *sgl;
12683 sgl = (struct ctl_sg_entry *)
12684 io->scsiio.kern_data_ptr;
12686 * If this is in cached memory, flush the cache
12687 * before we send the DMA request to the other
12688 * controller. We want to do this in either
12689 * the * read or the write case. The read
12690 * case is straightforward. In the write
12691 * case, we want to make sure nothing is
12692 * in the local cache that could overwrite
12696 for (i = sg_entries_sent, j = 0;
12697 i < msg.dt.cur_sg_entries; i++, j++) {
12698 if ((io->io_hdr.flags &
12699 CTL_FLAG_NO_DATASYNC) == 0) {
12701 * XXX KDM use bus_dmamap_sync()
12704 if ((io->io_hdr.flags &
12705 CTL_FLAG_BUS_ADDR) == 0) {
12707 * XXX KDM use busdma.
12710 msg.dt.sg_list[j].addr =(void *)
12711 vtophys(sgl[i].addr);
12714 msg.dt.sg_list[j].addr =
12717 msg.dt.sg_list[j].len = sgl[i].len;
12721 sg_entries_sent += msg.dt.cur_sg_entries;
12722 if (sg_entries_sent >= msg.dt.kern_sg_entries)
12723 msg.dt.sg_last = 1;
12725 msg.dt.sg_last = 0;
12728 * XXX KDM drop and reacquire the lock here?
12730 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12731 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
12733 * XXX do something here.
12737 msg.dt.sent_sg_entries = sg_entries_sent;
12739 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12740 if (io->io_hdr.flags & CTL_FLAG_FAILOVER)
12741 ctl_failover_io(io, /*have_lock*/ 0);
12746 * Lookup the fe_datamove() function for this particular
12750 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12757 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12759 union ctl_ha_msg msg;
12762 memset(&msg, 0, sizeof(msg));
12764 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12765 msg.hdr.original_sc = io;
12766 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12767 msg.hdr.nexus = io->io_hdr.nexus;
12768 msg.hdr.status = io->io_hdr.status;
12769 msg.scsi.tag_num = io->scsiio.tag_num;
12770 msg.scsi.tag_type = io->scsiio.tag_type;
12771 msg.scsi.scsi_status = io->scsiio.scsi_status;
12772 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12773 sizeof(io->scsiio.sense_data));
12774 msg.scsi.sense_len = io->scsiio.sense_len;
12775 msg.scsi.sense_residual = io->scsiio.sense_residual;
12776 msg.scsi.fetd_status = io->io_hdr.port_status;
12777 msg.scsi.residual = io->scsiio.residual;
12778 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12780 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12781 ctl_failover_io(io, /*have_lock*/ have_lock);
12785 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0);
12786 if (isc_status > CTL_HA_STATUS_SUCCESS) {
12787 /* XXX do something if this fails */
12793 * The DMA to the remote side is done, now we need to tell the other side
12794 * we're done so it can continue with its data movement.
12797 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12803 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12804 printf("%s: ISC DMA write failed with error %d", __func__,
12806 ctl_set_internal_failure(&io->scsiio,
12808 /*retry_count*/ rq->ret);
12811 ctl_dt_req_free(rq);
12814 * In this case, we had to malloc the memory locally. Free it.
12816 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12818 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12819 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12822 * The data is in local and remote memory, so now we need to send
12823 * status (good or back) back to the other side.
12825 ctl_send_datamove_done(io, /*have_lock*/ 0);
12829 * We've moved the data from the host/controller into local memory. Now we
12830 * need to push it over to the remote controller's memory.
12833 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12839 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12840 ctl_datamove_remote_write_cb);
12846 ctl_datamove_remote_write(union ctl_io *io)
12849 void (*fe_datamove)(union ctl_io *io);
12852 * - Get the data from the host/HBA into local memory.
12853 * - DMA memory from the local controller to the remote controller.
12854 * - Send status back to the remote controller.
12857 retval = ctl_datamove_remote_sgl_setup(io);
12861 /* Switch the pointer over so the FETD knows what to do */
12862 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12865 * Use a custom move done callback, since we need to send completion
12866 * back to the other controller, not to the backend on this side.
12868 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12870 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12879 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12888 * In this case, we had to malloc the memory locally. Free it.
12890 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12892 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12893 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12897 scsi_path_string(io, path_str, sizeof(path_str));
12898 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12899 sbuf_cat(&sb, path_str);
12900 scsi_command_string(&io->scsiio, NULL, &sb);
12901 sbuf_printf(&sb, "\n");
12902 sbuf_cat(&sb, path_str);
12903 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12904 io->scsiio.tag_num, io->scsiio.tag_type);
12905 sbuf_cat(&sb, path_str);
12906 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12907 io->io_hdr.flags, io->io_hdr.status);
12909 printk("%s", sbuf_data(&sb));
12914 * The read is done, now we need to send status (good or bad) back
12915 * to the other side.
12917 ctl_send_datamove_done(io, /*have_lock*/ 0);
12923 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12926 void (*fe_datamove)(union ctl_io *io);
12930 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12931 printf("%s: ISC DMA read failed with error %d", __func__,
12933 ctl_set_internal_failure(&io->scsiio,
12935 /*retry_count*/ rq->ret);
12938 ctl_dt_req_free(rq);
12940 /* Switch the pointer over so the FETD knows what to do */
12941 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12944 * Use a custom move done callback, since we need to send completion
12945 * back to the other controller, not to the backend on this side.
12947 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12949 /* XXX KDM add checks like the ones in ctl_datamove? */
12951 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12957 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12959 struct ctl_sg_entry *local_sglist, *remote_sglist;
12960 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist;
12961 struct ctl_softc *softc;
12966 softc = control_softc;
12968 local_sglist = io->io_hdr.local_sglist;
12969 local_dma_sglist = io->io_hdr.local_dma_sglist;
12970 remote_sglist = io->io_hdr.remote_sglist;
12971 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
12973 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) {
12974 for (i = 0; i < io->scsiio.kern_sg_entries; i++) {
12975 local_sglist[i].len = remote_sglist[i].len;
12978 * XXX Detect the situation where the RS-level I/O
12979 * redirector on the other side has already read the
12980 * data off of the AOR RS on this side, and
12981 * transferred it to remote (mirror) memory on the
12982 * other side. Since we already have the data in
12983 * memory here, we just need to use it.
12985 * XXX KDM this can probably be removed once we
12986 * get the cache device code in and take the
12987 * current AOR implementation out.
12990 if ((remote_sglist[i].addr >=
12991 (void *)vtophys(softc->mirr->addr))
12992 && (remote_sglist[i].addr <
12993 ((void *)vtophys(softc->mirr->addr) +
12994 CacheMirrorOffset))) {
12995 local_sglist[i].addr = remote_sglist[i].addr -
12997 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12999 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE;
13001 local_sglist[i].addr = remote_sglist[i].addr +
13006 printf("%s: local %p, remote %p, len %d\n",
13007 __func__, local_sglist[i].addr,
13008 remote_sglist[i].addr, local_sglist[i].len);
13012 uint32_t len_to_go;
13015 * In this case, we don't have automatically allocated
13016 * memory for this I/O on this controller. This typically
13017 * happens with internal CTL I/O -- e.g. inquiry, mode
13018 * sense, etc. Anything coming from RAIDCore will have
13019 * a mirror area available.
13021 len_to_go = io->scsiio.kern_data_len;
13024 * Clear the no datasync flag, we have to use malloced
13027 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC;
13030 * The difficult thing here is that the size of the various
13031 * S/G segments may be different than the size from the
13032 * remote controller. That'll make it harder when DMAing
13033 * the data back to the other side.
13035 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) /
13036 sizeof(io->io_hdr.remote_sglist[0])) &&
13037 (len_to_go > 0); i++) {
13038 local_sglist[i].len = ctl_min(len_to_go, 131072);
13039 CTL_SIZE_8B(local_dma_sglist[i].len,
13040 local_sglist[i].len);
13041 local_sglist[i].addr =
13042 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK);
13044 local_dma_sglist[i].addr = local_sglist[i].addr;
13046 if (local_sglist[i].addr == NULL) {
13049 printf("malloc failed for %zd bytes!",
13050 local_dma_sglist[i].len);
13051 for (j = 0; j < i; j++) {
13052 free(local_sglist[j].addr, M_CTL);
13054 ctl_set_internal_failure(&io->scsiio,
13056 /*retry_count*/ 4857);
13058 goto bailout_error;
13061 /* XXX KDM do we need a sync here? */
13063 len_to_go -= local_sglist[i].len;
13066 * Reset the number of S/G entries accordingly. The
13067 * original number of S/G entries is available in
13070 io->scsiio.kern_sg_entries = i;
13073 printf("%s: kern_sg_entries = %d\n", __func__,
13074 io->scsiio.kern_sg_entries);
13075 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13076 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i,
13077 local_sglist[i].addr, local_sglist[i].len,
13078 local_dma_sglist[i].len);
13087 ctl_send_datamove_done(io, /*have_lock*/ 0);
13093 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
13094 ctl_ha_dt_cb callback)
13096 struct ctl_ha_dt_req *rq;
13097 struct ctl_sg_entry *remote_sglist, *local_sglist;
13098 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist;
13099 uint32_t local_used, remote_used, total_used;
13105 rq = ctl_dt_req_alloc();
13108 * If we failed to allocate the request, and if the DMA didn't fail
13109 * anyway, set busy status. This is just a resource allocation
13113 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE))
13114 ctl_set_busy(&io->scsiio);
13116 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
13119 ctl_dt_req_free(rq);
13122 * The data move failed. We need to return status back
13123 * to the other controller. No point in trying to DMA
13124 * data to the remote controller.
13127 ctl_send_datamove_done(io, /*have_lock*/ 0);
13134 local_sglist = io->io_hdr.local_sglist;
13135 local_dma_sglist = io->io_hdr.local_dma_sglist;
13136 remote_sglist = io->io_hdr.remote_sglist;
13137 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
13142 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) {
13143 rq->ret = CTL_HA_STATUS_SUCCESS;
13150 * Pull/push the data over the wire from/to the other controller.
13151 * This takes into account the possibility that the local and
13152 * remote sglists may not be identical in terms of the size of
13153 * the elements and the number of elements.
13155 * One fundamental assumption here is that the length allocated for
13156 * both the local and remote sglists is identical. Otherwise, we've
13157 * essentially got a coding error of some sort.
13159 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
13161 uint32_t cur_len, dma_length;
13164 rq->id = CTL_HA_DATA_CTL;
13165 rq->command = command;
13169 * Both pointers should be aligned. But it is possible
13170 * that the allocation length is not. They should both
13171 * also have enough slack left over at the end, though,
13172 * to round up to the next 8 byte boundary.
13174 cur_len = ctl_min(local_sglist[i].len - local_used,
13175 remote_sglist[j].len - remote_used);
13178 * In this case, we have a size issue and need to decrease
13179 * the size, except in the case where we actually have less
13180 * than 8 bytes left. In that case, we need to increase
13181 * the DMA length to get the last bit.
13183 if ((cur_len & 0x7) != 0) {
13184 if (cur_len > 0x7) {
13185 cur_len = cur_len - (cur_len & 0x7);
13186 dma_length = cur_len;
13188 CTL_SIZE_8B(dma_length, cur_len);
13192 dma_length = cur_len;
13195 * If we had to allocate memory for this I/O, instead of using
13196 * the non-cached mirror memory, we'll need to flush the cache
13197 * before trying to DMA to the other controller.
13199 * We could end up doing this multiple times for the same
13200 * segment if we have a larger local segment than remote
13201 * segment. That shouldn't be an issue.
13203 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
13205 * XXX KDM use bus_dmamap_sync() here.
13209 rq->size = dma_length;
13211 tmp_ptr = (uint8_t *)local_sglist[i].addr;
13212 tmp_ptr += local_used;
13214 /* Use physical addresses when talking to ISC hardware */
13215 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
13216 /* XXX KDM use busdma */
13218 rq->local = vtophys(tmp_ptr);
13221 rq->local = tmp_ptr;
13223 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
13224 tmp_ptr += remote_used;
13225 rq->remote = tmp_ptr;
13227 rq->callback = NULL;
13229 local_used += cur_len;
13230 if (local_used >= local_sglist[i].len) {
13235 remote_used += cur_len;
13236 if (remote_used >= remote_sglist[j].len) {
13240 total_used += cur_len;
13242 if (total_used >= io->scsiio.kern_data_len)
13243 rq->callback = callback;
13245 if ((rq->size & 0x7) != 0) {
13246 printf("%s: warning: size %d is not on 8b boundary\n",
13247 __func__, rq->size);
13249 if (((uintptr_t)rq->local & 0x7) != 0) {
13250 printf("%s: warning: local %p not on 8b boundary\n",
13251 __func__, rq->local);
13253 if (((uintptr_t)rq->remote & 0x7) != 0) {
13254 printf("%s: warning: remote %p not on 8b boundary\n",
13255 __func__, rq->local);
13258 printf("%s: %s: local %#x remote %#x size %d\n", __func__,
13259 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
13260 rq->local, rq->remote, rq->size);
13263 isc_ret = ctl_dt_single(rq);
13264 if (isc_ret == CTL_HA_STATUS_WAIT)
13267 if (isc_ret == CTL_HA_STATUS_DISCONNECT) {
13268 rq->ret = CTL_HA_STATUS_SUCCESS;
13282 ctl_datamove_remote_read(union ctl_io *io)
13288 * This will send an error to the other controller in the case of a
13291 retval = ctl_datamove_remote_sgl_setup(io);
13295 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
13296 ctl_datamove_remote_read_cb);
13298 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) {
13300 * Make sure we free memory if there was an error.. The
13301 * ctl_datamove_remote_xfer() function will send the
13302 * datamove done message, or call the callback with an
13303 * error if there is a problem.
13305 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13306 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13313 * Process a datamove request from the other controller. This is used for
13314 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
13315 * first. Once that is complete, the data gets DMAed into the remote
13316 * controller's memory. For reads, we DMA from the remote controller's
13317 * memory into our memory first, and then move it out to the FETD.
13320 ctl_datamove_remote(union ctl_io *io)
13322 struct ctl_softc *softc;
13324 softc = control_softc;
13326 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
13329 * Note that we look for an aborted I/O here, but don't do some of
13330 * the other checks that ctl_datamove() normally does.
13331 * We don't need to run the datamove delay code, since that should
13332 * have been done if need be on the other controller.
13334 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
13335 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__,
13336 io->scsiio.tag_num, io->io_hdr.nexus.initid.id,
13337 io->io_hdr.nexus.targ_port,
13338 io->io_hdr.nexus.targ_target.id,
13339 io->io_hdr.nexus.targ_lun);
13340 io->io_hdr.port_status = 31338;
13341 ctl_send_datamove_done(io, /*have_lock*/ 0);
13345 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) {
13346 ctl_datamove_remote_write(io);
13347 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){
13348 ctl_datamove_remote_read(io);
13350 union ctl_ha_msg msg;
13351 struct scsi_sense_data *sense;
13355 memset(&msg, 0, sizeof(msg));
13357 msg.hdr.msg_type = CTL_MSG_BAD_JUJU;
13358 msg.hdr.status = CTL_SCSI_ERROR;
13359 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
13361 retry_count = 4243;
13363 sense = &msg.scsi.sense_data;
13364 sks[0] = SSD_SCS_VALID;
13365 sks[1] = (retry_count >> 8) & 0xff;
13366 sks[2] = retry_count & 0xff;
13368 /* "Internal target failure" */
13369 scsi_set_sense_data(sense,
13370 /*sense_format*/ SSD_TYPE_NONE,
13371 /*current_error*/ 1,
13372 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
13375 /*type*/ SSD_ELEM_SKS,
13376 /*size*/ sizeof(sks),
13380 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
13381 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
13382 ctl_failover_io(io, /*have_lock*/ 1);
13386 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) >
13387 CTL_HA_STATUS_SUCCESS) {
13388 /* XXX KDM what to do if this fails? */
13396 ctl_process_done(union ctl_io *io)
13398 struct ctl_lun *lun;
13399 struct ctl_softc *ctl_softc = control_softc;
13400 void (*fe_done)(union ctl_io *io);
13401 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port);
13403 CTL_DEBUG_PRINT(("ctl_process_done\n"));
13406 control_softc->ctl_ports[targ_port]->fe_done;
13409 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
13414 ctl_scsi_path_string(io, path_str, sizeof(path_str));
13415 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13417 sbuf_cat(&sb, path_str);
13418 switch (io->io_hdr.io_type) {
13420 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
13421 sbuf_printf(&sb, "\n");
13422 sbuf_cat(&sb, path_str);
13423 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13424 io->scsiio.tag_num, io->scsiio.tag_type);
13427 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
13428 "Tag Type: %d\n", io->taskio.task_action,
13429 io->taskio.tag_num, io->taskio.tag_type);
13432 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13433 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13436 sbuf_cat(&sb, path_str);
13437 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
13438 (intmax_t)time_uptime - io->io_hdr.start_time);
13440 printf("%s", sbuf_data(&sb));
13442 #endif /* CTL_TIME_IO */
13444 switch (io->io_hdr.io_type) {
13448 if (bootverbose || (ctl_debug & CTL_DEBUG_INFO))
13449 ctl_io_error_print(io, NULL);
13450 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
13454 return (CTL_RETVAL_COMPLETE);
13456 panic("ctl_process_done: invalid io type %d\n",
13457 io->io_hdr.io_type);
13458 break; /* NOTREACHED */
13461 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13463 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13464 io->io_hdr.nexus.targ_mapped_lun));
13468 mtx_lock(&lun->lun_lock);
13471 * Check to see if we have any errors to inject here. We only
13472 * inject errors for commands that don't already have errors set.
13474 if ((STAILQ_FIRST(&lun->error_list) != NULL) &&
13475 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
13476 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
13477 ctl_inject_error(lun, io);
13480 * XXX KDM how do we treat commands that aren't completed
13483 * XXX KDM should we also track I/O latency?
13485 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13486 io->io_hdr.io_type == CTL_IO_SCSI) {
13488 struct bintime cur_bt;
13492 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13494 type = CTL_STATS_READ;
13495 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13497 type = CTL_STATS_WRITE;
13499 type = CTL_STATS_NO_IO;
13501 lun->stats.ports[targ_port].bytes[type] +=
13502 io->scsiio.kern_total_len;
13503 lun->stats.ports[targ_port].operations[type]++;
13505 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
13506 &io->io_hdr.dma_bt);
13507 lun->stats.ports[targ_port].num_dmas[type] +=
13508 io->io_hdr.num_dmas;
13509 getbintime(&cur_bt);
13510 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
13511 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
13516 * Remove this from the OOA queue.
13518 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13521 * Run through the blocked queue on this LUN and see if anything
13522 * has become unblocked, now that this transaction is done.
13524 ctl_check_blocked(lun);
13527 * If the LUN has been invalidated, free it if there is nothing
13528 * left on its OOA queue.
13530 if ((lun->flags & CTL_LUN_INVALID)
13531 && TAILQ_EMPTY(&lun->ooa_queue)) {
13532 mtx_unlock(&lun->lun_lock);
13533 mtx_lock(&ctl_softc->ctl_lock);
13535 mtx_unlock(&ctl_softc->ctl_lock);
13537 mtx_unlock(&lun->lun_lock);
13542 * If this command has been aborted, make sure we set the status
13543 * properly. The FETD is responsible for freeing the I/O and doing
13544 * whatever it needs to do to clean up its state.
13546 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13547 ctl_set_task_aborted(&io->scsiio);
13550 * If enabled, print command error status.
13551 * We don't print UAs unless debugging was enabled explicitly.
13554 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
13556 if (!bootverbose && (ctl_debug & CTL_DEBUG_INFO) == 0)
13558 if ((ctl_debug & CTL_DEBUG_INFO) == 0 &&
13559 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) &&
13560 (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) {
13561 int error_code, sense_key, asc, ascq;
13563 scsi_extract_sense_len(&io->scsiio.sense_data,
13564 io->scsiio.sense_len, &error_code, &sense_key,
13565 &asc, &ascq, /*show_errors*/ 0);
13566 if (sense_key == SSD_KEY_UNIT_ATTENTION)
13570 ctl_io_error_print(io, NULL);
13574 * Tell the FETD or the other shelf controller we're done with this
13575 * command. Note that only SCSI commands get to this point. Task
13576 * management commands are completed above.
13578 * We only send status to the other controller if we're in XFER
13579 * mode. In SER_ONLY mode, the I/O is done on the controller that
13580 * received the I/O (from CTL's perspective), and so the status is
13583 * XXX KDM if we hold the lock here, we could cause a deadlock
13584 * if the frontend comes back in in this context to queue
13587 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER)
13588 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
13589 union ctl_ha_msg msg;
13591 memset(&msg, 0, sizeof(msg));
13592 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13593 msg.hdr.original_sc = io->io_hdr.original_sc;
13594 msg.hdr.nexus = io->io_hdr.nexus;
13595 msg.hdr.status = io->io_hdr.status;
13596 msg.scsi.scsi_status = io->scsiio.scsi_status;
13597 msg.scsi.tag_num = io->scsiio.tag_num;
13598 msg.scsi.tag_type = io->scsiio.tag_type;
13599 msg.scsi.sense_len = io->scsiio.sense_len;
13600 msg.scsi.sense_residual = io->scsiio.sense_residual;
13601 msg.scsi.residual = io->scsiio.residual;
13602 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
13603 sizeof(io->scsiio.sense_data));
13605 * We copy this whether or not this is an I/O-related
13606 * command. Otherwise, we'd have to go and check to see
13607 * whether it's a read/write command, and it really isn't
13610 memcpy(&msg.scsi.lbalen,
13611 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
13612 sizeof(msg.scsi.lbalen));
13614 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13615 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
13616 /* XXX do something here */
13623 return (CTL_RETVAL_COMPLETE);
13628 * Front end should call this if it doesn't do autosense. When the request
13629 * sense comes back in from the initiator, we'll dequeue this and send it.
13632 ctl_queue_sense(union ctl_io *io)
13634 struct ctl_lun *lun;
13635 struct ctl_softc *ctl_softc;
13636 uint32_t initidx, targ_lun;
13638 ctl_softc = control_softc;
13640 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13643 * LUN lookup will likely move to the ctl_work_thread() once we
13644 * have our new queueing infrastructure (that doesn't put things on
13645 * a per-LUN queue initially). That is so that we can handle
13646 * things like an INQUIRY to a LUN that we don't have enabled. We
13647 * can't deal with that right now.
13649 mtx_lock(&ctl_softc->ctl_lock);
13652 * If we don't have a LUN for this, just toss the sense
13655 targ_lun = io->io_hdr.nexus.targ_lun;
13656 targ_lun = ctl_map_lun(io->io_hdr.nexus.targ_port, targ_lun);
13657 if ((targ_lun < CTL_MAX_LUNS)
13658 && (ctl_softc->ctl_luns[targ_lun] != NULL))
13659 lun = ctl_softc->ctl_luns[targ_lun];
13663 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13665 mtx_lock(&lun->lun_lock);
13667 * Already have CA set for this LUN...toss the sense information.
13669 if (ctl_is_set(lun->have_ca, initidx)) {
13670 mtx_unlock(&lun->lun_lock);
13674 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data,
13675 ctl_min(sizeof(lun->pending_sense[initidx]),
13676 sizeof(io->scsiio.sense_data)));
13677 ctl_set_mask(lun->have_ca, initidx);
13678 mtx_unlock(&lun->lun_lock);
13681 mtx_unlock(&ctl_softc->ctl_lock);
13685 return (CTL_RETVAL_COMPLETE);
13690 * Primary command inlet from frontend ports. All SCSI and task I/O
13691 * requests must go through this function.
13694 ctl_queue(union ctl_io *io)
13696 struct ctl_softc *ctl_softc;
13698 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13700 ctl_softc = control_softc;
13703 io->io_hdr.start_time = time_uptime;
13704 getbintime(&io->io_hdr.start_bt);
13705 #endif /* CTL_TIME_IO */
13707 /* Map FE-specific LUN ID into global one. */
13708 io->io_hdr.nexus.targ_mapped_lun =
13709 ctl_map_lun(io->io_hdr.nexus.targ_port, io->io_hdr.nexus.targ_lun);
13711 switch (io->io_hdr.io_type) {
13714 if (ctl_debug & CTL_DEBUG_CDB)
13716 ctl_enqueue_incoming(io);
13719 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13723 return (CTL_RETVAL_COMPLETE);
13726 #ifdef CTL_IO_DELAY
13728 ctl_done_timer_wakeup(void *arg)
13732 io = (union ctl_io *)arg;
13735 #endif /* CTL_IO_DELAY */
13738 ctl_done(union ctl_io *io)
13740 struct ctl_softc *ctl_softc;
13742 ctl_softc = control_softc;
13745 * Enable this to catch duplicate completion issues.
13748 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13749 printf("%s: type %d msg %d cdb %x iptl: "
13750 "%d:%d:%d:%d tag 0x%04x "
13751 "flag %#x status %x\n",
13753 io->io_hdr.io_type,
13754 io->io_hdr.msg_type,
13756 io->io_hdr.nexus.initid.id,
13757 io->io_hdr.nexus.targ_port,
13758 io->io_hdr.nexus.targ_target.id,
13759 io->io_hdr.nexus.targ_lun,
13760 (io->io_hdr.io_type ==
13762 io->taskio.tag_num :
13763 io->scsiio.tag_num,
13765 io->io_hdr.status);
13767 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13771 * This is an internal copy of an I/O, and should not go through
13772 * the normal done processing logic.
13774 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13778 * We need to send a msg to the serializing shelf to finish the IO
13779 * as well. We don't send a finish message to the other shelf if
13780 * this is a task management command. Task management commands
13781 * aren't serialized in the OOA queue, but rather just executed on
13782 * both shelf controllers for commands that originated on that
13785 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)
13786 && (io->io_hdr.io_type != CTL_IO_TASK)) {
13787 union ctl_ha_msg msg_io;
13789 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO;
13790 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc;
13791 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io,
13792 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) {
13794 /* continue on to finish IO */
13796 #ifdef CTL_IO_DELAY
13797 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13798 struct ctl_lun *lun;
13800 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13802 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13804 struct ctl_lun *lun;
13806 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13809 && (lun->delay_info.done_delay > 0)) {
13810 struct callout *callout;
13812 callout = (struct callout *)&io->io_hdr.timer_bytes;
13813 callout_init(callout, /*mpsafe*/ 1);
13814 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13815 callout_reset(callout,
13816 lun->delay_info.done_delay * hz,
13817 ctl_done_timer_wakeup, io);
13818 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13819 lun->delay_info.done_delay = 0;
13823 #endif /* CTL_IO_DELAY */
13825 ctl_enqueue_done(io);
13829 ctl_isc(struct ctl_scsiio *ctsio)
13831 struct ctl_lun *lun;
13834 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13836 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0]));
13838 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n"));
13840 retval = lun->backend->data_submit((union ctl_io *)ctsio);
13847 ctl_work_thread(void *arg)
13849 struct ctl_thread *thr = (struct ctl_thread *)arg;
13850 struct ctl_softc *softc = thr->ctl_softc;
13854 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13860 * We handle the queues in this order:
13862 * - done queue (to free up resources, unblock other commands)
13866 * If those queues are empty, we break out of the loop and
13869 mtx_lock(&thr->queue_lock);
13870 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13872 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13873 mtx_unlock(&thr->queue_lock);
13874 ctl_handle_isc(io);
13877 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13879 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13880 /* clear any blocked commands, call fe_done */
13881 mtx_unlock(&thr->queue_lock);
13882 retval = ctl_process_done(io);
13885 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13887 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13888 mtx_unlock(&thr->queue_lock);
13889 if (io->io_hdr.io_type == CTL_IO_TASK)
13892 ctl_scsiio_precheck(softc, &io->scsiio);
13895 if (!ctl_pause_rtr) {
13896 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13898 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13899 mtx_unlock(&thr->queue_lock);
13900 retval = ctl_scsiio(&io->scsiio);
13901 if (retval != CTL_RETVAL_COMPLETE)
13902 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13907 /* Sleep until we have something to do. */
13908 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13913 ctl_lun_thread(void *arg)
13915 struct ctl_softc *softc = (struct ctl_softc *)arg;
13916 struct ctl_be_lun *be_lun;
13919 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13923 mtx_lock(&softc->ctl_lock);
13924 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13925 if (be_lun != NULL) {
13926 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13927 mtx_unlock(&softc->ctl_lock);
13928 ctl_create_lun(be_lun);
13932 /* Sleep until we have something to do. */
13933 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13934 PDROP | PRIBIO, "-", 0);
13939 ctl_thresh_thread(void *arg)
13941 struct ctl_softc *softc = (struct ctl_softc *)arg;
13942 struct ctl_lun *lun;
13943 struct ctl_be_lun *be_lun;
13944 struct scsi_da_rw_recovery_page *rwpage;
13945 struct ctl_logical_block_provisioning_page *page;
13947 uint64_t thres, val;
13950 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13953 mtx_lock(&softc->ctl_lock);
13954 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13955 be_lun = lun->be_lun;
13956 if ((lun->flags & CTL_LUN_DISABLED) ||
13957 (lun->flags & CTL_LUN_OFFLINE) ||
13958 (be_lun->flags & CTL_LUN_FLAG_UNMAP) == 0 ||
13959 lun->backend->lun_attr == NULL)
13961 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT];
13962 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0)
13965 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT];
13966 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13967 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13969 thres = scsi_4btoul(page->descr[i].count);
13970 thres <<= CTL_LBP_EXPONENT;
13971 switch (page->descr[i].resource) {
13973 attr = "blocksavail";
13976 attr = "blocksused";
13979 attr = "poolblocksavail";
13982 attr = "poolblocksused";
13987 mtx_unlock(&softc->ctl_lock); // XXX
13988 val = lun->backend->lun_attr(
13989 lun->be_lun->be_lun, attr);
13990 mtx_lock(&softc->ctl_lock);
13991 if (val == UINT64_MAX)
13993 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13994 == SLBPPD_ARMING_INC)
13995 e |= (val >= thres);
13997 e |= (val <= thres);
13999 mtx_lock(&lun->lun_lock);
14001 if (lun->lasttpt == 0 ||
14002 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
14003 lun->lasttpt = time_uptime;
14004 for (i = 0; i < CTL_MAX_INITIATORS; i++)
14005 lun->pending_ua[i] |=
14006 CTL_UA_THIN_PROV_THRES;
14010 for (i = 0; i < CTL_MAX_INITIATORS; i++)
14011 lun->pending_ua[i] &= ~CTL_UA_THIN_PROV_THRES;
14013 mtx_unlock(&lun->lun_lock);
14015 mtx_unlock(&softc->ctl_lock);
14016 pause("-", CTL_LBP_PERIOD * hz);
14021 ctl_enqueue_incoming(union ctl_io *io)
14023 struct ctl_softc *softc = control_softc;
14024 struct ctl_thread *thr;
14027 idx = (io->io_hdr.nexus.targ_port * 127 +
14028 io->io_hdr.nexus.initid.id) % worker_threads;
14029 thr = &softc->threads[idx];
14030 mtx_lock(&thr->queue_lock);
14031 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
14032 mtx_unlock(&thr->queue_lock);
14037 ctl_enqueue_rtr(union ctl_io *io)
14039 struct ctl_softc *softc = control_softc;
14040 struct ctl_thread *thr;
14042 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14043 mtx_lock(&thr->queue_lock);
14044 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
14045 mtx_unlock(&thr->queue_lock);
14050 ctl_enqueue_done(union ctl_io *io)
14052 struct ctl_softc *softc = control_softc;
14053 struct ctl_thread *thr;
14055 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14056 mtx_lock(&thr->queue_lock);
14057 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
14058 mtx_unlock(&thr->queue_lock);
14063 ctl_enqueue_isc(union ctl_io *io)
14065 struct ctl_softc *softc = control_softc;
14066 struct ctl_thread *thr;
14068 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14069 mtx_lock(&thr->queue_lock);
14070 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
14071 mtx_unlock(&thr->queue_lock);
14075 /* Initialization and failover */
14078 ctl_init_isc_msg(void)
14080 printf("CTL: Still calling this thing\n");
14085 * Initializes component into configuration defined by bootMode
14087 * returns hasc_Status:
14089 * ERROR - fatal error
14091 static ctl_ha_comp_status
14092 ctl_isc_init(struct ctl_ha_component *c)
14094 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
14101 * Starts component in state requested. If component starts successfully,
14102 * it must set its own state to the requestrd state
14103 * When requested state is HASC_STATE_HA, the component may refine it
14104 * by adding _SLAVE or _MASTER flags.
14105 * Currently allowed state transitions are:
14106 * UNKNOWN->HA - initial startup
14107 * UNKNOWN->SINGLE - initial startup when no parter detected
14108 * HA->SINGLE - failover
14109 * returns ctl_ha_comp_status:
14110 * OK - component successfully started in requested state
14111 * FAILED - could not start the requested state, failover may
14113 * ERROR - fatal error detected, no future startup possible
14115 static ctl_ha_comp_status
14116 ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state)
14118 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
14120 printf("%s: go\n", __func__);
14122 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap)
14123 if (c->state == CTL_HA_STATE_UNKNOWN ) {
14124 control_softc->is_single = 0;
14125 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
14126 != CTL_HA_STATUS_SUCCESS) {
14127 printf("ctl_isc_start: ctl_ha_msg_create failed.\n");
14128 ret = CTL_HA_COMP_STATUS_ERROR;
14130 } else if (CTL_HA_STATE_IS_HA(c->state)
14131 && CTL_HA_STATE_IS_SINGLE(state)){
14132 // HA->SINGLE transition
14134 control_softc->is_single = 1;
14136 printf("ctl_isc_start:Invalid state transition %X->%X\n",
14138 ret = CTL_HA_COMP_STATUS_ERROR;
14140 if (CTL_HA_STATE_IS_SINGLE(state))
14141 control_softc->is_single = 1;
14149 * Quiesce component
14150 * The component must clear any error conditions (set status to OK) and
14151 * prepare itself to another Start call
14152 * returns ctl_ha_comp_status:
14156 static ctl_ha_comp_status
14157 ctl_isc_quiesce(struct ctl_ha_component *c)
14159 int ret = CTL_HA_COMP_STATUS_OK;
14166 struct ctl_ha_component ctl_ha_component_ctlisc =
14169 .state = CTL_HA_STATE_UNKNOWN,
14170 .init = ctl_isc_init,
14171 .start = ctl_isc_start,
14172 .quiesce = ctl_isc_quiesce