2 * Copyright (c) 2003-2009 Silicon Graphics International Corp.
3 * Copyright (c) 2012 The FreeBSD Foundation
6 * Portions of this software were developed by Edward Tomasz Napierala
7 * under sponsorship from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
16 * substantially similar to the "NO WARRANTY" disclaimer below
17 * ("Disclaimer") and any redistribution must be conditioned upon
18 * including a substantially similar Disclaimer requirement for further
19 * binary redistribution.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGES.
34 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl.c#8 $
37 * CAM Target Layer, a SCSI device emulation subsystem.
39 * Author: Ken Merry <ken@FreeBSD.org>
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/ctype.h>
50 #include <sys/kernel.h>
51 #include <sys/types.h>
52 #include <sys/kthread.h>
54 #include <sys/fcntl.h>
56 #include <sys/module.h>
57 #include <sys/mutex.h>
58 #include <sys/condvar.h>
59 #include <sys/malloc.h>
61 #include <sys/ioccom.h>
62 #include <sys/queue.h>
65 #include <sys/endian.h>
66 #include <sys/sysctl.h>
69 #include <cam/scsi/scsi_all.h>
70 #include <cam/scsi/scsi_da.h>
71 #include <cam/ctl/ctl_io.h>
72 #include <cam/ctl/ctl.h>
73 #include <cam/ctl/ctl_frontend.h>
74 #include <cam/ctl/ctl_frontend_internal.h>
75 #include <cam/ctl/ctl_util.h>
76 #include <cam/ctl/ctl_backend.h>
77 #include <cam/ctl/ctl_ioctl.h>
78 #include <cam/ctl/ctl_ha.h>
79 #include <cam/ctl/ctl_private.h>
80 #include <cam/ctl/ctl_debug.h>
81 #include <cam/ctl/ctl_scsi_all.h>
82 #include <cam/ctl/ctl_error.h>
84 struct ctl_softc *control_softc = NULL;
87 * Size and alignment macros needed for Copan-specific HA hardware. These
88 * can go away when the HA code is re-written, and uses busdma for any
91 #define CTL_ALIGN_8B(target, source, type) \
92 if (((uint32_t)source & 0x7) != 0) \
93 target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\
95 target = (type)source;
97 #define CTL_SIZE_8B(target, size) \
98 if ((size & 0x7) != 0) \
99 target = size + (0x8 - (size & 0x7)); \
103 #define CTL_ALIGN_8B_MARGIN 16
106 * Template mode pages.
110 * Note that these are default values only. The actual values will be
111 * filled in when the user does a mode sense.
113 static struct copan_debugconf_subpage debugconf_page_default = {
114 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
115 DBGCNF_SUBPAGE_CODE, /* subpage */
116 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
117 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
118 DBGCNF_VERSION, /* page_version */
119 {CTL_TIME_IO_DEFAULT_SECS>>8,
120 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */
123 static struct copan_debugconf_subpage debugconf_page_changeable = {
124 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
125 DBGCNF_SUBPAGE_CODE, /* subpage */
126 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
127 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
128 0, /* page_version */
129 {0xff,0xff}, /* ctl_time_io_secs */
132 static struct scsi_da_rw_recovery_page rw_er_page_default = {
133 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
134 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
135 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE,
136 /*read_retry_count*/0,
137 /*correction_span*/0,
138 /*head_offset_count*/0,
139 /*data_strobe_offset_cnt*/0,
141 /*write_retry_count*/0,
143 /*recovery_time_limit*/{0, 0},
146 static struct scsi_da_rw_recovery_page rw_er_page_changeable = {
147 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
148 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
150 /*read_retry_count*/0,
151 /*correction_span*/0,
152 /*head_offset_count*/0,
153 /*data_strobe_offset_cnt*/0,
155 /*write_retry_count*/0,
157 /*recovery_time_limit*/{0, 0},
160 static struct scsi_format_page format_page_default = {
161 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
162 /*page_length*/sizeof(struct scsi_format_page) - 2,
163 /*tracks_per_zone*/ {0, 0},
164 /*alt_sectors_per_zone*/ {0, 0},
165 /*alt_tracks_per_zone*/ {0, 0},
166 /*alt_tracks_per_lun*/ {0, 0},
167 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
168 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
169 /*bytes_per_sector*/ {0, 0},
170 /*interleave*/ {0, 0},
171 /*track_skew*/ {0, 0},
172 /*cylinder_skew*/ {0, 0},
174 /*reserved*/ {0, 0, 0}
177 static struct scsi_format_page format_page_changeable = {
178 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
179 /*page_length*/sizeof(struct scsi_format_page) - 2,
180 /*tracks_per_zone*/ {0, 0},
181 /*alt_sectors_per_zone*/ {0, 0},
182 /*alt_tracks_per_zone*/ {0, 0},
183 /*alt_tracks_per_lun*/ {0, 0},
184 /*sectors_per_track*/ {0, 0},
185 /*bytes_per_sector*/ {0, 0},
186 /*interleave*/ {0, 0},
187 /*track_skew*/ {0, 0},
188 /*cylinder_skew*/ {0, 0},
190 /*reserved*/ {0, 0, 0}
193 static struct scsi_rigid_disk_page rigid_disk_page_default = {
194 /*page_code*/SMS_RIGID_DISK_PAGE,
195 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
196 /*cylinders*/ {0, 0, 0},
197 /*heads*/ CTL_DEFAULT_HEADS,
198 /*start_write_precomp*/ {0, 0, 0},
199 /*start_reduced_current*/ {0, 0, 0},
200 /*step_rate*/ {0, 0},
201 /*landing_zone_cylinder*/ {0, 0, 0},
202 /*rpl*/ SRDP_RPL_DISABLED,
203 /*rotational_offset*/ 0,
205 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
206 CTL_DEFAULT_ROTATION_RATE & 0xff},
210 static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
211 /*page_code*/SMS_RIGID_DISK_PAGE,
212 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
213 /*cylinders*/ {0, 0, 0},
215 /*start_write_precomp*/ {0, 0, 0},
216 /*start_reduced_current*/ {0, 0, 0},
217 /*step_rate*/ {0, 0},
218 /*landing_zone_cylinder*/ {0, 0, 0},
220 /*rotational_offset*/ 0,
222 /*rotation_rate*/ {0, 0},
226 static struct scsi_caching_page caching_page_default = {
227 /*page_code*/SMS_CACHING_PAGE,
228 /*page_length*/sizeof(struct scsi_caching_page) - 2,
229 /*flags1*/ SCP_DISC | SCP_WCE,
231 /*disable_pf_transfer_len*/ {0xff, 0xff},
232 /*min_prefetch*/ {0, 0},
233 /*max_prefetch*/ {0xff, 0xff},
234 /*max_pf_ceiling*/ {0xff, 0xff},
236 /*cache_segments*/ 0,
237 /*cache_seg_size*/ {0, 0},
239 /*non_cache_seg_size*/ {0, 0, 0}
242 static struct scsi_caching_page caching_page_changeable = {
243 /*page_code*/SMS_CACHING_PAGE,
244 /*page_length*/sizeof(struct scsi_caching_page) - 2,
245 /*flags1*/ SCP_WCE | SCP_RCD,
247 /*disable_pf_transfer_len*/ {0, 0},
248 /*min_prefetch*/ {0, 0},
249 /*max_prefetch*/ {0, 0},
250 /*max_pf_ceiling*/ {0, 0},
252 /*cache_segments*/ 0,
253 /*cache_seg_size*/ {0, 0},
255 /*non_cache_seg_size*/ {0, 0, 0}
258 static struct scsi_control_page control_page_default = {
259 /*page_code*/SMS_CONTROL_MODE_PAGE,
260 /*page_length*/sizeof(struct scsi_control_page) - 2,
262 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED,
265 /*aen_holdoff_period*/{0, 0},
266 /*busy_timeout_period*/{0, 0},
267 /*extended_selftest_completion_time*/{0, 0}
270 static struct scsi_control_page control_page_changeable = {
271 /*page_code*/SMS_CONTROL_MODE_PAGE,
272 /*page_length*/sizeof(struct scsi_control_page) - 2,
274 /*queue_flags*/SCP_QUEUE_ALG_MASK,
275 /*eca_and_aen*/SCP_SWP,
277 /*aen_holdoff_period*/{0, 0},
278 /*busy_timeout_period*/{0, 0},
279 /*extended_selftest_completion_time*/{0, 0}
282 static struct scsi_info_exceptions_page ie_page_default = {
283 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
284 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
285 /*info_flags*/SIEP_FLAGS_DEXCPT,
287 /*interval_timer*/{0, 0, 0, 0},
288 /*report_count*/{0, 0, 0, 0}
291 static struct scsi_info_exceptions_page ie_page_changeable = {
292 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
293 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
296 /*interval_timer*/{0, 0, 0, 0},
297 /*report_count*/{0, 0, 0, 0}
300 static struct scsi_logical_block_provisioning_page lbp_page_default = {
301 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
302 /*subpage_code*/0x02,
303 /*page_length*/{0, sizeof(struct scsi_logical_block_provisioning_page) - 4},
305 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
309 static struct scsi_logical_block_provisioning_page lbp_page_changeable = {
310 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
311 /*subpage_code*/0x02,
312 /*page_length*/{0, sizeof(struct scsi_logical_block_provisioning_page) - 4},
314 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
319 * XXX KDM move these into the softc.
321 static int rcv_sync_msg;
322 static int persis_offset;
323 static uint8_t ctl_pause_rtr;
324 static int ctl_is_single = 1;
326 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
327 static int worker_threads = -1;
328 TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads);
329 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
330 &worker_threads, 1, "Number of worker threads");
331 static int ctl_debug = CTL_DEBUG_NONE;
332 TUNABLE_INT("kern.cam.ctl.debug", &ctl_debug);
333 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN,
334 &ctl_debug, 0, "Enabled debug flags");
337 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
338 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
339 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0),
340 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
342 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 10
344 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
346 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
347 static int ctl_init(void);
348 void ctl_shutdown(void);
349 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
350 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
351 static void ctl_ioctl_online(void *arg);
352 static void ctl_ioctl_offline(void *arg);
353 static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id);
354 static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id);
355 static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio);
356 static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
357 static int ctl_ioctl_submit_wait(union ctl_io *io);
358 static void ctl_ioctl_datamove(union ctl_io *io);
359 static void ctl_ioctl_done(union ctl_io *io);
360 static void ctl_ioctl_hard_startstop_callback(void *arg,
361 struct cfi_metatask *metatask);
362 static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask);
363 static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
364 struct ctl_ooa *ooa_hdr,
365 struct ctl_ooa_entry *kern_entries);
366 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
368 static uint32_t ctl_map_lun(int port_num, uint32_t lun);
369 static uint32_t ctl_map_lun_back(int port_num, uint32_t lun);
371 static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port,
372 uint32_t targ_target, uint32_t targ_lun,
374 static void ctl_kfree_io(union ctl_io *io);
376 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
377 struct ctl_be_lun *be_lun, struct ctl_id target_id);
378 static int ctl_free_lun(struct ctl_lun *lun);
379 static void ctl_create_lun(struct ctl_be_lun *be_lun);
381 static void ctl_failover_change_pages(struct ctl_softc *softc,
382 struct ctl_scsiio *ctsio, int master);
385 static int ctl_do_mode_select(union ctl_io *io);
386 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
387 uint64_t res_key, uint64_t sa_res_key,
388 uint8_t type, uint32_t residx,
389 struct ctl_scsiio *ctsio,
390 struct scsi_per_res_out *cdb,
391 struct scsi_per_res_out_parms* param);
392 static void ctl_pro_preempt_other(struct ctl_lun *lun,
393 union ctl_ha_msg *msg);
394 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
395 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
396 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
397 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
398 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
399 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
400 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
402 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
404 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
405 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
406 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
407 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
408 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
409 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2);
410 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
411 union ctl_io *pending_io, union ctl_io *ooa_io);
412 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
413 union ctl_io *starting_io);
414 static int ctl_check_blocked(struct ctl_lun *lun);
415 static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc,
417 const struct ctl_cmd_entry *entry,
418 struct ctl_scsiio *ctsio);
419 //static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc);
420 static void ctl_failover(void);
421 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
422 struct ctl_scsiio *ctsio);
423 static int ctl_scsiio(struct ctl_scsiio *ctsio);
425 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
426 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
427 ctl_ua_type ua_type);
428 static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io,
429 ctl_ua_type ua_type);
430 static int ctl_abort_task(union ctl_io *io);
431 static int ctl_abort_task_set(union ctl_io *io);
432 static int ctl_i_t_nexus_reset(union ctl_io *io);
433 static void ctl_run_task(union ctl_io *io);
435 static void ctl_datamove_timer_wakeup(void *arg);
436 static void ctl_done_timer_wakeup(void *arg);
437 #endif /* CTL_IO_DELAY */
439 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
440 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
441 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
442 static void ctl_datamove_remote_write(union ctl_io *io);
443 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
444 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
445 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
446 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
447 ctl_ha_dt_cb callback);
448 static void ctl_datamove_remote_read(union ctl_io *io);
449 static void ctl_datamove_remote(union ctl_io *io);
450 static int ctl_process_done(union ctl_io *io);
451 static void ctl_lun_thread(void *arg);
452 static void ctl_work_thread(void *arg);
453 static void ctl_enqueue_incoming(union ctl_io *io);
454 static void ctl_enqueue_rtr(union ctl_io *io);
455 static void ctl_enqueue_done(union ctl_io *io);
456 static void ctl_enqueue_isc(union ctl_io *io);
457 static const struct ctl_cmd_entry *
458 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
459 static const struct ctl_cmd_entry *
460 ctl_validate_command(struct ctl_scsiio *ctsio);
461 static int ctl_cmd_applicable(uint8_t lun_type,
462 const struct ctl_cmd_entry *entry);
465 * Load the serialization table. This isn't very pretty, but is probably
466 * the easiest way to do it.
468 #include "ctl_ser_table.c"
471 * We only need to define open, close and ioctl routines for this driver.
473 static struct cdevsw ctl_cdevsw = {
474 .d_version = D_VERSION,
477 .d_close = ctl_close,
478 .d_ioctl = ctl_ioctl,
483 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
484 MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests");
486 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
488 static moduledata_t ctl_moduledata = {
490 ctl_module_event_handler,
494 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
495 MODULE_VERSION(ctl, 1);
497 static struct ctl_frontend ioctl_frontend =
503 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
504 union ctl_ha_msg *msg_info)
506 struct ctl_scsiio *ctsio;
508 if (msg_info->hdr.original_sc == NULL) {
509 printf("%s: original_sc == NULL!\n", __func__);
510 /* XXX KDM now what? */
514 ctsio = &msg_info->hdr.original_sc->scsiio;
515 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
516 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
517 ctsio->io_hdr.status = msg_info->hdr.status;
518 ctsio->scsi_status = msg_info->scsi.scsi_status;
519 ctsio->sense_len = msg_info->scsi.sense_len;
520 ctsio->sense_residual = msg_info->scsi.sense_residual;
521 ctsio->residual = msg_info->scsi.residual;
522 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
523 sizeof(ctsio->sense_data));
524 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
525 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen));
526 ctl_enqueue_isc((union ctl_io *)ctsio);
530 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
531 union ctl_ha_msg *msg_info)
533 struct ctl_scsiio *ctsio;
535 if (msg_info->hdr.serializing_sc == NULL) {
536 printf("%s: serializing_sc == NULL!\n", __func__);
537 /* XXX KDM now what? */
541 ctsio = &msg_info->hdr.serializing_sc->scsiio;
544 * Attempt to catch the situation where an I/O has
545 * been freed, and we're using it again.
547 if (ctsio->io_hdr.io_type == 0xff) {
548 union ctl_io *tmp_io;
549 tmp_io = (union ctl_io *)ctsio;
550 printf("%s: %p use after free!\n", __func__,
552 printf("%s: type %d msg %d cdb %x iptl: "
553 "%d:%d:%d:%d tag 0x%04x "
554 "flag %#x status %x\n",
556 tmp_io->io_hdr.io_type,
557 tmp_io->io_hdr.msg_type,
558 tmp_io->scsiio.cdb[0],
559 tmp_io->io_hdr.nexus.initid.id,
560 tmp_io->io_hdr.nexus.targ_port,
561 tmp_io->io_hdr.nexus.targ_target.id,
562 tmp_io->io_hdr.nexus.targ_lun,
563 (tmp_io->io_hdr.io_type ==
565 tmp_io->taskio.tag_num :
566 tmp_io->scsiio.tag_num,
567 tmp_io->io_hdr.flags,
568 tmp_io->io_hdr.status);
571 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
572 ctl_enqueue_isc((union ctl_io *)ctsio);
576 * ISC (Inter Shelf Communication) event handler. Events from the HA
577 * subsystem come in here.
580 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
582 struct ctl_softc *ctl_softc;
584 struct ctl_prio *presio;
585 ctl_ha_status isc_status;
587 ctl_softc = control_softc;
592 printf("CTL: Isc Msg event %d\n", event);
594 if (event == CTL_HA_EVT_MSG_RECV) {
595 union ctl_ha_msg msg_info;
597 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
598 sizeof(msg_info), /*wait*/ 0);
600 printf("CTL: msg_type %d\n", msg_info.msg_type);
602 if (isc_status != 0) {
603 printf("Error receiving message, status = %d\n",
608 switch (msg_info.hdr.msg_type) {
609 case CTL_MSG_SERIALIZE:
611 printf("Serialize\n");
613 io = ctl_alloc_io((void *)ctl_softc->othersc_pool);
615 printf("ctl_isc_event_handler: can't allocate "
618 /* Need to set busy and send msg back */
619 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
620 msg_info.hdr.status = CTL_SCSI_ERROR;
621 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY;
622 msg_info.scsi.sense_len = 0;
623 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
624 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){
629 // populate ctsio from msg_info
630 io->io_hdr.io_type = CTL_IO_SCSI;
631 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
632 io->io_hdr.original_sc = msg_info.hdr.original_sc;
634 printf("pOrig %x\n", (int)msg_info.original_sc);
636 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
639 * If we're in serialization-only mode, we don't
640 * want to go through full done processing. Thus
643 * XXX KDM add another flag that is more specific.
645 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)
646 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
647 io->io_hdr.nexus = msg_info.hdr.nexus;
649 printf("targ %d, port %d, iid %d, lun %d\n",
650 io->io_hdr.nexus.targ_target.id,
651 io->io_hdr.nexus.targ_port,
652 io->io_hdr.nexus.initid.id,
653 io->io_hdr.nexus.targ_lun);
655 io->scsiio.tag_num = msg_info.scsi.tag_num;
656 io->scsiio.tag_type = msg_info.scsi.tag_type;
657 memcpy(io->scsiio.cdb, msg_info.scsi.cdb,
659 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
660 const struct ctl_cmd_entry *entry;
662 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
663 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
665 entry->flags & CTL_FLAG_DATA_MASK;
670 /* Performed on the Originating SC, XFER mode only */
671 case CTL_MSG_DATAMOVE: {
672 struct ctl_sg_entry *sgl;
675 io = msg_info.hdr.original_sc;
677 printf("%s: original_sc == NULL!\n", __func__);
678 /* XXX KDM do something here */
681 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
682 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
684 * Keep track of this, we need to send it back over
685 * when the datamove is complete.
687 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
689 if (msg_info.dt.sg_sequence == 0) {
691 * XXX KDM we use the preallocated S/G list
692 * here, but we'll need to change this to
693 * dynamic allocation if we need larger S/G
696 if (msg_info.dt.kern_sg_entries >
697 sizeof(io->io_hdr.remote_sglist) /
698 sizeof(io->io_hdr.remote_sglist[0])) {
699 printf("%s: number of S/G entries "
700 "needed %u > allocated num %zd\n",
702 msg_info.dt.kern_sg_entries,
703 sizeof(io->io_hdr.remote_sglist)/
704 sizeof(io->io_hdr.remote_sglist[0]));
707 * XXX KDM send a message back to
708 * the other side to shut down the
709 * DMA. The error will come back
710 * through via the normal channel.
714 sgl = io->io_hdr.remote_sglist;
716 sizeof(io->io_hdr.remote_sglist));
718 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
720 io->scsiio.kern_sg_entries =
721 msg_info.dt.kern_sg_entries;
722 io->scsiio.rem_sg_entries =
723 msg_info.dt.kern_sg_entries;
724 io->scsiio.kern_data_len =
725 msg_info.dt.kern_data_len;
726 io->scsiio.kern_total_len =
727 msg_info.dt.kern_total_len;
728 io->scsiio.kern_data_resid =
729 msg_info.dt.kern_data_resid;
730 io->scsiio.kern_rel_offset =
731 msg_info.dt.kern_rel_offset;
733 * Clear out per-DMA flags.
735 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK;
737 * Add per-DMA flags that are set for this
738 * particular DMA request.
740 io->io_hdr.flags |= msg_info.dt.flags &
743 sgl = (struct ctl_sg_entry *)
744 io->scsiio.kern_data_ptr;
746 for (i = msg_info.dt.sent_sg_entries, j = 0;
747 i < (msg_info.dt.sent_sg_entries +
748 msg_info.dt.cur_sg_entries); i++, j++) {
749 sgl[i].addr = msg_info.dt.sg_list[j].addr;
750 sgl[i].len = msg_info.dt.sg_list[j].len;
753 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n",
755 msg_info.dt.sg_list[j].addr,
756 msg_info.dt.sg_list[j].len,
757 sgl[i].addr, sgl[i].len, j, i);
761 memcpy(&sgl[msg_info.dt.sent_sg_entries],
763 sizeof(*sgl) * msg_info.dt.cur_sg_entries);
767 * If this is the last piece of the I/O, we've got
768 * the full S/G list. Queue processing in the thread.
769 * Otherwise wait for the next piece.
771 if (msg_info.dt.sg_last != 0)
775 /* Performed on the Serializing (primary) SC, XFER mode only */
776 case CTL_MSG_DATAMOVE_DONE: {
777 if (msg_info.hdr.serializing_sc == NULL) {
778 printf("%s: serializing_sc == NULL!\n",
780 /* XXX KDM now what? */
784 * We grab the sense information here in case
785 * there was a failure, so we can return status
786 * back to the initiator.
788 io = msg_info.hdr.serializing_sc;
789 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
790 io->io_hdr.status = msg_info.hdr.status;
791 io->scsiio.scsi_status = msg_info.scsi.scsi_status;
792 io->scsiio.sense_len = msg_info.scsi.sense_len;
793 io->scsiio.sense_residual =msg_info.scsi.sense_residual;
794 io->io_hdr.port_status = msg_info.scsi.fetd_status;
795 io->scsiio.residual = msg_info.scsi.residual;
796 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data,
797 sizeof(io->scsiio.sense_data));
802 /* Preformed on Originating SC, SER_ONLY mode */
804 io = msg_info.hdr.original_sc;
806 printf("%s: Major Bummer\n", __func__);
810 printf("pOrig %x\n",(int) ctsio);
813 io->io_hdr.msg_type = CTL_MSG_R2R;
814 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
819 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
821 * Performed on the Originating (i.e. secondary) SC in XFER
824 case CTL_MSG_FINISH_IO:
825 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER)
826 ctl_isc_handler_finish_xfer(ctl_softc,
829 ctl_isc_handler_finish_ser_only(ctl_softc,
833 /* Preformed on Originating SC */
834 case CTL_MSG_BAD_JUJU:
835 io = msg_info.hdr.original_sc;
837 printf("%s: Bad JUJU!, original_sc is NULL!\n",
841 ctl_copy_sense_data(&msg_info, io);
843 * IO should have already been cleaned up on other
844 * SC so clear this flag so we won't send a message
845 * back to finish the IO there.
847 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
848 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
850 /* io = msg_info.hdr.serializing_sc; */
851 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
855 /* Handle resets sent from the other side */
856 case CTL_MSG_MANAGE_TASKS: {
857 struct ctl_taskio *taskio;
858 taskio = (struct ctl_taskio *)ctl_alloc_io(
859 (void *)ctl_softc->othersc_pool);
860 if (taskio == NULL) {
861 printf("ctl_isc_event_handler: can't allocate "
864 /* should I just call the proper reset func
868 ctl_zero_io((union ctl_io *)taskio);
869 taskio->io_hdr.io_type = CTL_IO_TASK;
870 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
871 taskio->io_hdr.nexus = msg_info.hdr.nexus;
872 taskio->task_action = msg_info.task.task_action;
873 taskio->tag_num = msg_info.task.tag_num;
874 taskio->tag_type = msg_info.task.tag_type;
876 taskio->io_hdr.start_time = time_uptime;
877 getbintime(&taskio->io_hdr.start_bt);
879 cs_prof_gettime(&taskio->io_hdr.start_ticks);
881 #endif /* CTL_TIME_IO */
882 ctl_run_task((union ctl_io *)taskio);
885 /* Persistent Reserve action which needs attention */
886 case CTL_MSG_PERS_ACTION:
887 presio = (struct ctl_prio *)ctl_alloc_io(
888 (void *)ctl_softc->othersc_pool);
889 if (presio == NULL) {
890 printf("ctl_isc_event_handler: can't allocate "
893 /* Need to set busy and send msg back */
896 ctl_zero_io((union ctl_io *)presio);
897 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
898 presio->pr_msg = msg_info.pr;
899 ctl_enqueue_isc((union ctl_io *)presio);
901 case CTL_MSG_SYNC_FE:
905 printf("How did I get here?\n");
907 } else if (event == CTL_HA_EVT_MSG_SENT) {
908 if (param != CTL_HA_STATUS_SUCCESS) {
909 printf("Bad status from ctl_ha_msg_send status %d\n",
913 } else if (event == CTL_HA_EVT_DISCONNECT) {
914 printf("CTL: Got a disconnect from Isc\n");
917 printf("ctl_isc_event_handler: Unknown event %d\n", event);
926 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
928 struct scsi_sense_data *sense;
930 sense = &dest->scsiio.sense_data;
931 bcopy(&src->scsi.sense_data, sense, sizeof(*sense));
932 dest->scsiio.scsi_status = src->scsi.scsi_status;
933 dest->scsiio.sense_len = src->scsi.sense_len;
934 dest->io_hdr.status = src->hdr.status;
940 struct ctl_softc *softc;
941 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool;
942 struct ctl_port *port;
944 int i, error, retval;
951 control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
953 softc = control_softc;
955 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
958 softc->dev->si_drv1 = softc;
961 * By default, return a "bad LUN" peripheral qualifier for unknown
962 * LUNs. The user can override this default using the tunable or
963 * sysctl. See the comment in ctl_inquiry_std() for more details.
965 softc->inquiry_pq_no_lun = 1;
966 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun",
967 &softc->inquiry_pq_no_lun);
968 sysctl_ctx_init(&softc->sysctl_ctx);
969 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
970 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
971 CTLFLAG_RD, 0, "CAM Target Layer");
973 if (softc->sysctl_tree == NULL) {
974 printf("%s: unable to allocate sysctl tree\n", __func__);
975 destroy_dev(softc->dev);
976 free(control_softc, M_DEVBUF);
977 control_softc = NULL;
981 SYSCTL_ADD_INT(&softc->sysctl_ctx,
982 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
983 "inquiry_pq_no_lun", CTLFLAG_RW,
984 &softc->inquiry_pq_no_lun, 0,
985 "Report no lun possible for invalid LUNs");
987 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
988 mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF);
989 softc->open_count = 0;
992 * Default to actually sending a SYNCHRONIZE CACHE command down to
995 softc->flags = CTL_FLAG_REAL_SYNC;
998 * In Copan's HA scheme, the "master" and "slave" roles are
999 * figured out through the slot the controller is in. Although it
1000 * is an active/active system, someone has to be in charge.
1003 scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id);
1007 softc->flags |= CTL_FLAG_MASTER_SHELF;
1010 persis_offset = CTL_MAX_INITIATORS;
1013 * XXX KDM need to figure out where we want to get our target ID
1014 * and WWID. Is it different on each port?
1016 softc->target.id = 0;
1017 softc->target.wwid[0] = 0x12345678;
1018 softc->target.wwid[1] = 0x87654321;
1019 STAILQ_INIT(&softc->lun_list);
1020 STAILQ_INIT(&softc->pending_lun_queue);
1021 STAILQ_INIT(&softc->fe_list);
1022 STAILQ_INIT(&softc->port_list);
1023 STAILQ_INIT(&softc->be_list);
1024 STAILQ_INIT(&softc->io_pools);
1025 ctl_tpc_init(softc);
1027 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL,
1028 &internal_pool)!= 0){
1029 printf("ctl: can't allocate %d entry internal pool, "
1030 "exiting\n", CTL_POOL_ENTRIES_INTERNAL);
1034 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY,
1035 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) {
1036 printf("ctl: can't allocate %d entry emergency pool, "
1037 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY);
1038 ctl_pool_free(internal_pool);
1042 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC,
1045 printf("ctl: can't allocate %d entry other SC pool, "
1046 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1047 ctl_pool_free(internal_pool);
1048 ctl_pool_free(emergency_pool);
1052 softc->internal_pool = internal_pool;
1053 softc->emergency_pool = emergency_pool;
1054 softc->othersc_pool = other_pool;
1056 if (worker_threads <= 0)
1057 worker_threads = max(1, mp_ncpus / 4);
1058 if (worker_threads > CTL_MAX_THREADS)
1059 worker_threads = CTL_MAX_THREADS;
1061 for (i = 0; i < worker_threads; i++) {
1062 struct ctl_thread *thr = &softc->threads[i];
1064 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1065 thr->ctl_softc = softc;
1066 STAILQ_INIT(&thr->incoming_queue);
1067 STAILQ_INIT(&thr->rtr_queue);
1068 STAILQ_INIT(&thr->done_queue);
1069 STAILQ_INIT(&thr->isc_queue);
1071 error = kproc_kthread_add(ctl_work_thread, thr,
1072 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1074 printf("error creating CTL work thread!\n");
1075 ctl_pool_free(internal_pool);
1076 ctl_pool_free(emergency_pool);
1077 ctl_pool_free(other_pool);
1081 error = kproc_kthread_add(ctl_lun_thread, softc,
1082 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
1084 printf("error creating CTL lun thread!\n");
1085 ctl_pool_free(internal_pool);
1086 ctl_pool_free(emergency_pool);
1087 ctl_pool_free(other_pool);
1091 printf("ctl: CAM Target Layer loaded\n");
1094 * Initialize the ioctl front end.
1096 ctl_frontend_register(&ioctl_frontend);
1097 port = &softc->ioctl_info.port;
1098 port->frontend = &ioctl_frontend;
1099 sprintf(softc->ioctl_info.port_name, "ioctl");
1100 port->port_type = CTL_PORT_IOCTL;
1101 port->num_requested_ctl_io = 100;
1102 port->port_name = softc->ioctl_info.port_name;
1103 port->port_online = ctl_ioctl_online;
1104 port->port_offline = ctl_ioctl_offline;
1105 port->onoff_arg = &softc->ioctl_info;
1106 port->lun_enable = ctl_ioctl_lun_enable;
1107 port->lun_disable = ctl_ioctl_lun_disable;
1108 port->targ_lun_arg = &softc->ioctl_info;
1109 port->fe_datamove = ctl_ioctl_datamove;
1110 port->fe_done = ctl_ioctl_done;
1111 port->max_targets = 15;
1112 port->max_target_id = 15;
1114 if (ctl_port_register(&softc->ioctl_info.port,
1115 (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) {
1116 printf("ctl: ioctl front end registration failed, will "
1117 "continue anyway\n");
1121 if (sizeof(struct callout) > CTL_TIMER_BYTES) {
1122 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n",
1123 sizeof(struct callout), CTL_TIMER_BYTES);
1126 #endif /* CTL_IO_DELAY */
1134 struct ctl_softc *softc;
1135 struct ctl_lun *lun, *next_lun;
1136 struct ctl_io_pool *pool;
1138 softc = (struct ctl_softc *)control_softc;
1140 if (ctl_port_deregister(&softc->ioctl_info.port) != 0)
1141 printf("ctl: ioctl front end deregistration failed\n");
1143 mtx_lock(&softc->ctl_lock);
1148 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
1149 next_lun = STAILQ_NEXT(lun, links);
1153 mtx_unlock(&softc->ctl_lock);
1155 ctl_frontend_deregister(&ioctl_frontend);
1158 * This will rip the rug out from under any FETDs or anyone else
1159 * that has a pool allocated. Since we increment our module
1160 * refcount any time someone outside the main CTL module allocates
1161 * a pool, we shouldn't have any problems here. The user won't be
1162 * able to unload the CTL module until client modules have
1163 * successfully unloaded.
1165 while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL)
1166 ctl_pool_free(pool);
1169 ctl_shutdown_thread(softc->work_thread);
1170 mtx_destroy(&softc->queue_lock);
1173 ctl_tpc_shutdown(softc);
1174 mtx_destroy(&softc->pool_lock);
1175 mtx_destroy(&softc->ctl_lock);
1177 destroy_dev(softc->dev);
1179 sysctl_ctx_free(&softc->sysctl_ctx);
1181 free(control_softc, M_DEVBUF);
1182 control_softc = NULL;
1185 printf("ctl: CAM Target Layer unloaded\n");
1189 ctl_module_event_handler(module_t mod, int what, void *arg)
1194 return (ctl_init());
1198 return (EOPNOTSUPP);
1203 * XXX KDM should we do some access checks here? Bump a reference count to
1204 * prevent a CTL module from being unloaded while someone has it open?
1207 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1213 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1219 ctl_port_enable(ctl_port_type port_type)
1221 struct ctl_softc *softc;
1222 struct ctl_port *port;
1224 if (ctl_is_single == 0) {
1225 union ctl_ha_msg msg_info;
1229 printf("%s: HA mode, synchronizing frontend enable\n",
1232 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE;
1233 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1234 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) {
1235 printf("Sync msg send error retval %d\n", isc_retval);
1237 if (!rcv_sync_msg) {
1238 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
1239 sizeof(msg_info), 1);
1242 printf("CTL:Frontend Enable\n");
1244 printf("%s: single mode, skipping frontend synchronization\n",
1249 softc = control_softc;
1251 STAILQ_FOREACH(port, &softc->port_list, links) {
1252 if (port_type & port->port_type)
1255 printf("port %d\n", port->targ_port);
1257 ctl_port_online(port);
1265 ctl_port_disable(ctl_port_type port_type)
1267 struct ctl_softc *softc;
1268 struct ctl_port *port;
1270 softc = control_softc;
1272 STAILQ_FOREACH(port, &softc->port_list, links) {
1273 if (port_type & port->port_type)
1274 ctl_port_offline(port);
1281 * Returns 0 for success, 1 for failure.
1282 * Currently the only failure mode is if there aren't enough entries
1283 * allocated. So, in case of a failure, look at num_entries_dropped,
1284 * reallocate and try again.
1287 ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced,
1288 int *num_entries_filled, int *num_entries_dropped,
1289 ctl_port_type port_type, int no_virtual)
1291 struct ctl_softc *softc;
1292 struct ctl_port *port;
1293 int entries_dropped, entries_filled;
1297 softc = control_softc;
1301 entries_dropped = 0;
1304 mtx_lock(&softc->ctl_lock);
1305 STAILQ_FOREACH(port, &softc->port_list, links) {
1306 struct ctl_port_entry *entry;
1308 if ((port->port_type & port_type) == 0)
1311 if ((no_virtual != 0)
1312 && (port->virtual_port != 0))
1315 if (entries_filled >= num_entries_alloced) {
1319 entry = &entries[i];
1321 entry->port_type = port->port_type;
1322 strlcpy(entry->port_name, port->port_name,
1323 sizeof(entry->port_name));
1324 entry->physical_port = port->physical_port;
1325 entry->virtual_port = port->virtual_port;
1326 entry->wwnn = port->wwnn;
1327 entry->wwpn = port->wwpn;
1333 mtx_unlock(&softc->ctl_lock);
1335 if (entries_dropped > 0)
1338 *num_entries_dropped = entries_dropped;
1339 *num_entries_filled = entries_filled;
1345 ctl_ioctl_online(void *arg)
1347 struct ctl_ioctl_info *ioctl_info;
1349 ioctl_info = (struct ctl_ioctl_info *)arg;
1351 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED;
1355 ctl_ioctl_offline(void *arg)
1357 struct ctl_ioctl_info *ioctl_info;
1359 ioctl_info = (struct ctl_ioctl_info *)arg;
1361 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED;
1365 * Remove an initiator by port number and initiator ID.
1366 * Returns 0 for success, -1 for failure.
1369 ctl_remove_initiator(struct ctl_port *port, int iid)
1371 struct ctl_softc *softc = control_softc;
1373 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1375 if (iid > CTL_MAX_INIT_PER_PORT) {
1376 printf("%s: initiator ID %u > maximun %u!\n",
1377 __func__, iid, CTL_MAX_INIT_PER_PORT);
1381 mtx_lock(&softc->ctl_lock);
1382 port->wwpn_iid[iid].in_use--;
1383 port->wwpn_iid[iid].last_use = time_uptime;
1384 mtx_unlock(&softc->ctl_lock);
1390 * Add an initiator to the initiator map.
1391 * Returns iid for success, < 0 for failure.
1394 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
1396 struct ctl_softc *softc = control_softc;
1400 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1402 if (iid >= CTL_MAX_INIT_PER_PORT) {
1403 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
1404 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
1409 mtx_lock(&softc->ctl_lock);
1411 if (iid < 0 && (wwpn != 0 || name != NULL)) {
1412 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1413 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
1417 if (name != NULL && port->wwpn_iid[i].name != NULL &&
1418 strcmp(name, port->wwpn_iid[i].name) == 0) {
1426 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1427 if (port->wwpn_iid[i].in_use == 0 &&
1428 port->wwpn_iid[i].wwpn == 0 &&
1429 port->wwpn_iid[i].name == NULL) {
1438 best_time = INT32_MAX;
1439 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1440 if (port->wwpn_iid[i].in_use == 0) {
1441 if (port->wwpn_iid[i].last_use < best_time) {
1443 best_time = port->wwpn_iid[i].last_use;
1451 mtx_unlock(&softc->ctl_lock);
1456 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
1458 * This is not an error yet.
1460 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
1462 printf("%s: port %d iid %u WWPN %#jx arrived"
1463 " again\n", __func__, port->targ_port,
1464 iid, (uintmax_t)wwpn);
1468 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
1469 strcmp(name, port->wwpn_iid[iid].name) == 0) {
1471 printf("%s: port %d iid %u name '%s' arrived"
1472 " again\n", __func__, port->targ_port,
1479 * This is an error, but what do we do about it? The
1480 * driver is telling us we have a new WWPN for this
1481 * initiator ID, so we pretty much need to use it.
1483 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
1484 " but WWPN %#jx '%s' is still at that address\n",
1485 __func__, port->targ_port, iid, wwpn, name,
1486 (uintmax_t)port->wwpn_iid[iid].wwpn,
1487 port->wwpn_iid[iid].name);
1490 * XXX KDM clear have_ca and ua_pending on each LUN for
1495 free(port->wwpn_iid[iid].name, M_CTL);
1496 port->wwpn_iid[iid].name = name;
1497 port->wwpn_iid[iid].wwpn = wwpn;
1498 port->wwpn_iid[iid].in_use++;
1499 mtx_unlock(&softc->ctl_lock);
1505 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
1509 switch (port->port_type) {
1512 struct scsi_transportid_fcp *id =
1513 (struct scsi_transportid_fcp *)buf;
1514 if (port->wwpn_iid[iid].wwpn == 0)
1516 memset(id, 0, sizeof(*id));
1517 id->format_protocol = SCSI_PROTO_FC;
1518 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
1519 return (sizeof(*id));
1521 case CTL_PORT_ISCSI:
1523 struct scsi_transportid_iscsi_port *id =
1524 (struct scsi_transportid_iscsi_port *)buf;
1525 if (port->wwpn_iid[iid].name == NULL)
1528 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
1530 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
1531 len = roundup2(min(len, 252), 4);
1532 scsi_ulto2b(len, id->additional_length);
1533 return (sizeof(*id) + len);
1537 struct scsi_transportid_sas *id =
1538 (struct scsi_transportid_sas *)buf;
1539 if (port->wwpn_iid[iid].wwpn == 0)
1541 memset(id, 0, sizeof(*id));
1542 id->format_protocol = SCSI_PROTO_SAS;
1543 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
1544 return (sizeof(*id));
1548 struct scsi_transportid_spi *id =
1549 (struct scsi_transportid_spi *)buf;
1550 memset(id, 0, sizeof(*id));
1551 id->format_protocol = SCSI_PROTO_SPI;
1552 scsi_ulto2b(iid, id->scsi_addr);
1553 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
1554 return (sizeof(*id));
1560 ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id)
1566 ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id)
1572 * Data movement routine for the CTL ioctl frontend port.
1575 ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio)
1577 struct ctl_sg_entry *ext_sglist, *kern_sglist;
1578 struct ctl_sg_entry ext_entry, kern_entry;
1579 int ext_sglen, ext_sg_entries, kern_sg_entries;
1580 int ext_sg_start, ext_offset;
1581 int len_to_copy, len_copied;
1582 int kern_watermark, ext_watermark;
1583 int ext_sglist_malloced;
1586 ext_sglist_malloced = 0;
1590 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n"));
1593 * If this flag is set, fake the data transfer.
1595 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) {
1596 ctsio->ext_data_filled = ctsio->ext_data_len;
1601 * To simplify things here, if we have a single buffer, stick it in
1602 * a S/G entry and just make it a single entry S/G list.
1604 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) {
1607 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist);
1609 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL,
1611 ext_sglist_malloced = 1;
1612 if (copyin(ctsio->ext_data_ptr, ext_sglist,
1614 ctl_set_internal_failure(ctsio,
1619 ext_sg_entries = ctsio->ext_sg_entries;
1621 for (i = 0; i < ext_sg_entries; i++) {
1622 if ((len_seen + ext_sglist[i].len) >=
1623 ctsio->ext_data_filled) {
1625 ext_offset = ctsio->ext_data_filled - len_seen;
1628 len_seen += ext_sglist[i].len;
1631 ext_sglist = &ext_entry;
1632 ext_sglist->addr = ctsio->ext_data_ptr;
1633 ext_sglist->len = ctsio->ext_data_len;
1636 ext_offset = ctsio->ext_data_filled;
1639 if (ctsio->kern_sg_entries > 0) {
1640 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
1641 kern_sg_entries = ctsio->kern_sg_entries;
1643 kern_sglist = &kern_entry;
1644 kern_sglist->addr = ctsio->kern_data_ptr;
1645 kern_sglist->len = ctsio->kern_data_len;
1646 kern_sg_entries = 1;
1651 ext_watermark = ext_offset;
1653 for (i = ext_sg_start, j = 0;
1654 i < ext_sg_entries && j < kern_sg_entries;) {
1655 uint8_t *ext_ptr, *kern_ptr;
1657 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark,
1658 kern_sglist[j].len - kern_watermark);
1660 ext_ptr = (uint8_t *)ext_sglist[i].addr;
1661 ext_ptr = ext_ptr + ext_watermark;
1662 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
1666 panic("need to implement bus address support");
1668 kern_ptr = bus_to_virt(kern_sglist[j].addr);
1671 kern_ptr = (uint8_t *)kern_sglist[j].addr;
1672 kern_ptr = kern_ptr + kern_watermark;
1674 kern_watermark += len_to_copy;
1675 ext_watermark += len_to_copy;
1677 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
1679 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1680 "bytes to user\n", len_to_copy));
1681 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1682 "to %p\n", kern_ptr, ext_ptr));
1683 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) {
1684 ctl_set_internal_failure(ctsio,
1690 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1691 "bytes from user\n", len_to_copy));
1692 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1693 "to %p\n", ext_ptr, kern_ptr));
1694 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){
1695 ctl_set_internal_failure(ctsio,
1702 len_copied += len_to_copy;
1704 if (ext_sglist[i].len == ext_watermark) {
1709 if (kern_sglist[j].len == kern_watermark) {
1715 ctsio->ext_data_filled += len_copied;
1717 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, "
1718 "kern_sg_entries: %d\n", ext_sg_entries,
1720 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, "
1721 "kern_data_len = %d\n", ctsio->ext_data_len,
1722 ctsio->kern_data_len));
1725 /* XXX KDM set residual?? */
1728 if (ext_sglist_malloced != 0)
1729 free(ext_sglist, M_CTL);
1731 return (CTL_RETVAL_COMPLETE);
1735 * Serialize a command that went down the "wrong" side, and so was sent to
1736 * this controller for execution. The logic is a little different than the
1737 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
1738 * sent back to the other side, but in the success case, we execute the
1739 * command on this side (XFER mode) or tell the other side to execute it
1743 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
1745 struct ctl_softc *ctl_softc;
1746 union ctl_ha_msg msg_info;
1747 struct ctl_lun *lun;
1751 ctl_softc = control_softc;
1753 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
1754 lun = ctl_softc->ctl_luns[targ_lun];
1758 * Why isn't LUN defined? The other side wouldn't
1759 * send a cmd if the LUN is undefined.
1761 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__);
1763 /* "Logical unit not supported" */
1764 ctl_set_sense_data(&msg_info.scsi.sense_data,
1766 /*sense_format*/SSD_TYPE_NONE,
1767 /*current_error*/ 1,
1768 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1773 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1774 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1775 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1776 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1777 msg_info.hdr.serializing_sc = NULL;
1778 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1779 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1780 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1786 mtx_lock(&lun->lun_lock);
1787 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1789 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
1790 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
1792 case CTL_ACTION_BLOCK:
1793 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
1794 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
1797 case CTL_ACTION_PASS:
1798 case CTL_ACTION_SKIP:
1799 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
1800 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
1801 ctl_enqueue_rtr((union ctl_io *)ctsio);
1804 /* send msg back to other side */
1805 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1806 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
1807 msg_info.hdr.msg_type = CTL_MSG_R2R;
1809 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc);
1811 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1812 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1816 case CTL_ACTION_OVERLAP:
1817 /* OVERLAPPED COMMANDS ATTEMPTED */
1818 ctl_set_sense_data(&msg_info.scsi.sense_data,
1820 /*sense_format*/SSD_TYPE_NONE,
1821 /*current_error*/ 1,
1822 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1827 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1828 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1829 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1830 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1831 msg_info.hdr.serializing_sc = NULL;
1832 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1834 printf("BAD JUJU:Major Bummer Overlap\n");
1836 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1838 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1839 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1842 case CTL_ACTION_OVERLAP_TAG:
1843 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */
1844 ctl_set_sense_data(&msg_info.scsi.sense_data,
1846 /*sense_format*/SSD_TYPE_NONE,
1847 /*current_error*/ 1,
1848 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1850 /*ascq*/ ctsio->tag_num & 0xff,
1853 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1854 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1855 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1856 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1857 msg_info.hdr.serializing_sc = NULL;
1858 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1860 printf("BAD JUJU:Major Bummer Overlap Tag\n");
1862 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1864 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1865 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1868 case CTL_ACTION_ERROR:
1870 /* "Internal target failure" */
1871 ctl_set_sense_data(&msg_info.scsi.sense_data,
1873 /*sense_format*/SSD_TYPE_NONE,
1874 /*current_error*/ 1,
1875 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
1880 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1881 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1882 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1883 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1884 msg_info.hdr.serializing_sc = NULL;
1885 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1887 printf("BAD JUJU:Major Bummer HW Error\n");
1889 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1891 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1892 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1896 mtx_unlock(&lun->lun_lock);
1901 ctl_ioctl_submit_wait(union ctl_io *io)
1903 struct ctl_fe_ioctl_params params;
1904 ctl_fe_ioctl_state last_state;
1909 bzero(¶ms, sizeof(params));
1911 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF);
1912 cv_init(¶ms.sem, "ctlioccv");
1913 params.state = CTL_IOCTL_INPROG;
1914 last_state = params.state;
1916 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms;
1918 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n"));
1920 /* This shouldn't happen */
1921 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE)
1927 mtx_lock(¶ms.ioctl_mtx);
1929 * Check the state here, and don't sleep if the state has
1930 * already changed (i.e. wakeup has already occured, but we
1931 * weren't waiting yet).
1933 if (params.state == last_state) {
1934 /* XXX KDM cv_wait_sig instead? */
1935 cv_wait(¶ms.sem, ¶ms.ioctl_mtx);
1937 last_state = params.state;
1939 switch (params.state) {
1940 case CTL_IOCTL_INPROG:
1941 /* Why did we wake up? */
1942 /* XXX KDM error here? */
1943 mtx_unlock(¶ms.ioctl_mtx);
1945 case CTL_IOCTL_DATAMOVE:
1946 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n"));
1949 * change last_state back to INPROG to avoid
1950 * deadlock on subsequent data moves.
1952 params.state = last_state = CTL_IOCTL_INPROG;
1954 mtx_unlock(¶ms.ioctl_mtx);
1955 ctl_ioctl_do_datamove(&io->scsiio);
1957 * Note that in some cases, most notably writes,
1958 * this will queue the I/O and call us back later.
1959 * In other cases, generally reads, this routine
1960 * will immediately call back and wake us up,
1961 * probably using our own context.
1963 io->scsiio.be_move_done(io);
1965 case CTL_IOCTL_DONE:
1966 mtx_unlock(¶ms.ioctl_mtx);
1967 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n"));
1971 mtx_unlock(¶ms.ioctl_mtx);
1972 /* XXX KDM error here? */
1975 } while (done == 0);
1977 mtx_destroy(¶ms.ioctl_mtx);
1978 cv_destroy(¶ms.sem);
1980 return (CTL_RETVAL_COMPLETE);
1984 ctl_ioctl_datamove(union ctl_io *io)
1986 struct ctl_fe_ioctl_params *params;
1988 params = (struct ctl_fe_ioctl_params *)
1989 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1991 mtx_lock(¶ms->ioctl_mtx);
1992 params->state = CTL_IOCTL_DATAMOVE;
1993 cv_broadcast(¶ms->sem);
1994 mtx_unlock(¶ms->ioctl_mtx);
1998 ctl_ioctl_done(union ctl_io *io)
2000 struct ctl_fe_ioctl_params *params;
2002 params = (struct ctl_fe_ioctl_params *)
2003 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2005 mtx_lock(¶ms->ioctl_mtx);
2006 params->state = CTL_IOCTL_DONE;
2007 cv_broadcast(¶ms->sem);
2008 mtx_unlock(¶ms->ioctl_mtx);
2012 ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask)
2014 struct ctl_fe_ioctl_startstop_info *sd_info;
2016 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg;
2018 sd_info->hs_info.status = metatask->status;
2019 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns;
2020 sd_info->hs_info.luns_complete =
2021 metatask->taskinfo.startstop.luns_complete;
2022 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed;
2024 cv_broadcast(&sd_info->sem);
2028 ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask)
2030 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info;
2032 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg;
2034 mtx_lock(fe_bbr_info->lock);
2035 fe_bbr_info->bbr_info->status = metatask->status;
2036 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2037 fe_bbr_info->wakeup_done = 1;
2038 mtx_unlock(fe_bbr_info->lock);
2040 cv_broadcast(&fe_bbr_info->sem);
2044 * Returns 0 for success, errno for failure.
2047 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2048 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2055 mtx_lock(&lun->lun_lock);
2056 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2057 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2059 struct ctl_ooa_entry *entry;
2062 * If we've got more than we can fit, just count the
2063 * remaining entries.
2065 if (*cur_fill_num >= ooa_hdr->alloc_num)
2068 entry = &kern_entries[*cur_fill_num];
2070 entry->tag_num = io->scsiio.tag_num;
2071 entry->lun_num = lun->lun;
2073 entry->start_bt = io->io_hdr.start_bt;
2075 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2076 entry->cdb_len = io->scsiio.cdb_len;
2077 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2078 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2080 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2081 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2083 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2084 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2086 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2087 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2089 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2090 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2092 mtx_unlock(&lun->lun_lock);
2098 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2099 size_t error_str_len)
2103 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2105 if (copyin(user_addr, kptr, len) != 0) {
2106 snprintf(error_str, error_str_len, "Error copying %d bytes "
2107 "from user address %p to kernel address %p", len,
2117 ctl_free_args(int num_args, struct ctl_be_arg *args)
2124 for (i = 0; i < num_args; i++) {
2125 free(args[i].kname, M_CTL);
2126 free(args[i].kvalue, M_CTL);
2132 static struct ctl_be_arg *
2133 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
2134 char *error_str, size_t error_str_len)
2136 struct ctl_be_arg *args;
2139 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
2140 error_str, error_str_len);
2145 for (i = 0; i < num_args; i++) {
2146 args[i].kname = NULL;
2147 args[i].kvalue = NULL;
2150 for (i = 0; i < num_args; i++) {
2153 args[i].kname = ctl_copyin_alloc(args[i].name,
2154 args[i].namelen, error_str, error_str_len);
2155 if (args[i].kname == NULL)
2158 if (args[i].kname[args[i].namelen - 1] != '\0') {
2159 snprintf(error_str, error_str_len, "Argument %d "
2160 "name is not NUL-terminated", i);
2164 if (args[i].flags & CTL_BEARG_RD) {
2165 tmpptr = ctl_copyin_alloc(args[i].value,
2166 args[i].vallen, error_str, error_str_len);
2169 if ((args[i].flags & CTL_BEARG_ASCII)
2170 && (tmpptr[args[i].vallen - 1] != '\0')) {
2171 snprintf(error_str, error_str_len, "Argument "
2172 "%d value is not NUL-terminated", i);
2175 args[i].kvalue = tmpptr;
2177 args[i].kvalue = malloc(args[i].vallen,
2178 M_CTL, M_WAITOK | M_ZERO);
2185 ctl_free_args(num_args, args);
2191 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2195 for (i = 0; i < num_args; i++) {
2196 if (args[i].flags & CTL_BEARG_WR)
2197 copyout(args[i].kvalue, args[i].value, args[i].vallen);
2202 * Escape characters that are illegal or not recommended in XML.
2205 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2207 char *end = str + size;
2212 for (; *str && str < end; str++) {
2215 retval = sbuf_printf(sb, "&");
2218 retval = sbuf_printf(sb, ">");
2221 retval = sbuf_printf(sb, "<");
2224 retval = sbuf_putc(sb, *str);
2237 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2239 struct scsi_vpd_id_descriptor *desc;
2242 if (id == NULL || id->len < 4)
2244 desc = (struct scsi_vpd_id_descriptor *)id->data;
2245 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2246 case SVPD_ID_TYPE_T10:
2247 sbuf_printf(sb, "t10.");
2249 case SVPD_ID_TYPE_EUI64:
2250 sbuf_printf(sb, "eui.");
2252 case SVPD_ID_TYPE_NAA:
2253 sbuf_printf(sb, "naa.");
2255 case SVPD_ID_TYPE_SCSI_NAME:
2258 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2259 case SVPD_ID_CODESET_BINARY:
2260 for (i = 0; i < desc->length; i++)
2261 sbuf_printf(sb, "%02x", desc->identifier[i]);
2263 case SVPD_ID_CODESET_ASCII:
2264 sbuf_printf(sb, "%.*s", (int)desc->length,
2265 (char *)desc->identifier);
2267 case SVPD_ID_CODESET_UTF8:
2268 sbuf_printf(sb, "%s", (char *)desc->identifier);
2274 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2277 struct ctl_softc *softc;
2280 softc = control_softc;
2290 * If we haven't been "enabled", don't allow any SCSI I/O
2293 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) {
2298 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref);
2300 printf("ctl_ioctl: can't allocate ctl_io!\n");
2306 * Need to save the pool reference so it doesn't get
2307 * spammed by the user's ctl_io.
2309 pool_tmp = io->io_hdr.pool;
2311 memcpy(io, (void *)addr, sizeof(*io));
2313 io->io_hdr.pool = pool_tmp;
2315 * No status yet, so make sure the status is set properly.
2317 io->io_hdr.status = CTL_STATUS_NONE;
2320 * The user sets the initiator ID, target and LUN IDs.
2322 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port;
2323 io->io_hdr.flags |= CTL_FLAG_USER_REQ;
2324 if ((io->io_hdr.io_type == CTL_IO_SCSI)
2325 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED))
2326 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++;
2328 retval = ctl_ioctl_submit_wait(io);
2335 memcpy((void *)addr, io, sizeof(*io));
2337 /* return this to our pool */
2342 case CTL_ENABLE_PORT:
2343 case CTL_DISABLE_PORT:
2344 case CTL_SET_PORT_WWNS: {
2345 struct ctl_port *port;
2346 struct ctl_port_entry *entry;
2348 entry = (struct ctl_port_entry *)addr;
2350 mtx_lock(&softc->ctl_lock);
2351 STAILQ_FOREACH(port, &softc->port_list, links) {
2357 if ((entry->port_type == CTL_PORT_NONE)
2358 && (entry->targ_port == port->targ_port)) {
2360 * If the user only wants to enable or
2361 * disable or set WWNs on a specific port,
2362 * do the operation and we're done.
2366 } else if (entry->port_type & port->port_type) {
2368 * Compare the user's type mask with the
2369 * particular frontend type to see if we
2376 * Make sure the user isn't trying to set
2377 * WWNs on multiple ports at the same time.
2379 if (cmd == CTL_SET_PORT_WWNS) {
2380 printf("%s: Can't set WWNs on "
2381 "multiple ports\n", __func__);
2388 * XXX KDM we have to drop the lock here,
2389 * because the online/offline operations
2390 * can potentially block. We need to
2391 * reference count the frontends so they
2394 mtx_unlock(&softc->ctl_lock);
2396 if (cmd == CTL_ENABLE_PORT) {
2397 struct ctl_lun *lun;
2399 STAILQ_FOREACH(lun, &softc->lun_list,
2401 port->lun_enable(port->targ_lun_arg,
2406 ctl_port_online(port);
2407 } else if (cmd == CTL_DISABLE_PORT) {
2408 struct ctl_lun *lun;
2410 ctl_port_offline(port);
2412 STAILQ_FOREACH(lun, &softc->lun_list,
2421 mtx_lock(&softc->ctl_lock);
2423 if (cmd == CTL_SET_PORT_WWNS)
2424 ctl_port_set_wwns(port,
2425 (entry->flags & CTL_PORT_WWNN_VALID) ?
2427 (entry->flags & CTL_PORT_WWPN_VALID) ?
2428 1 : 0, entry->wwpn);
2433 mtx_unlock(&softc->ctl_lock);
2436 case CTL_GET_PORT_LIST: {
2437 struct ctl_port *port;
2438 struct ctl_port_list *list;
2441 list = (struct ctl_port_list *)addr;
2443 if (list->alloc_len != (list->alloc_num *
2444 sizeof(struct ctl_port_entry))) {
2445 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != "
2446 "alloc_num %u * sizeof(struct ctl_port_entry) "
2447 "%zu\n", __func__, list->alloc_len,
2448 list->alloc_num, sizeof(struct ctl_port_entry));
2454 list->dropped_num = 0;
2456 mtx_lock(&softc->ctl_lock);
2457 STAILQ_FOREACH(port, &softc->port_list, links) {
2458 struct ctl_port_entry entry, *list_entry;
2460 if (list->fill_num >= list->alloc_num) {
2461 list->dropped_num++;
2465 entry.port_type = port->port_type;
2466 strlcpy(entry.port_name, port->port_name,
2467 sizeof(entry.port_name));
2468 entry.targ_port = port->targ_port;
2469 entry.physical_port = port->physical_port;
2470 entry.virtual_port = port->virtual_port;
2471 entry.wwnn = port->wwnn;
2472 entry.wwpn = port->wwpn;
2473 if (port->status & CTL_PORT_STATUS_ONLINE)
2478 list_entry = &list->entries[i];
2480 retval = copyout(&entry, list_entry, sizeof(entry));
2482 printf("%s: CTL_GET_PORT_LIST: copyout "
2483 "returned %d\n", __func__, retval);
2488 list->fill_len += sizeof(entry);
2490 mtx_unlock(&softc->ctl_lock);
2493 * If this is non-zero, we had a copyout fault, so there's
2494 * probably no point in attempting to set the status inside
2500 if (list->dropped_num > 0)
2501 list->status = CTL_PORT_LIST_NEED_MORE_SPACE;
2503 list->status = CTL_PORT_LIST_OK;
2506 case CTL_DUMP_OOA: {
2507 struct ctl_lun *lun;
2512 mtx_lock(&softc->ctl_lock);
2513 printf("Dumping OOA queues:\n");
2514 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2515 mtx_lock(&lun->lun_lock);
2516 for (io = (union ctl_io *)TAILQ_FIRST(
2517 &lun->ooa_queue); io != NULL;
2518 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2520 sbuf_new(&sb, printbuf, sizeof(printbuf),
2522 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ",
2526 CTL_FLAG_BLOCKED) ? "" : " BLOCKED",
2528 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
2530 CTL_FLAG_ABORT) ? " ABORT" : "",
2532 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : "");
2533 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
2535 printf("%s\n", sbuf_data(&sb));
2537 mtx_unlock(&lun->lun_lock);
2539 printf("OOA queues dump done\n");
2540 mtx_unlock(&softc->ctl_lock);
2544 struct ctl_lun *lun;
2545 struct ctl_ooa *ooa_hdr;
2546 struct ctl_ooa_entry *entries;
2547 uint32_t cur_fill_num;
2549 ooa_hdr = (struct ctl_ooa *)addr;
2551 if ((ooa_hdr->alloc_len == 0)
2552 || (ooa_hdr->alloc_num == 0)) {
2553 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2554 "must be non-zero\n", __func__,
2555 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2560 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2561 sizeof(struct ctl_ooa_entry))) {
2562 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2563 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2564 __func__, ooa_hdr->alloc_len,
2565 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2570 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2571 if (entries == NULL) {
2572 printf("%s: could not allocate %d bytes for OOA "
2573 "dump\n", __func__, ooa_hdr->alloc_len);
2578 mtx_lock(&softc->ctl_lock);
2579 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2580 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS)
2581 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2582 mtx_unlock(&softc->ctl_lock);
2583 free(entries, M_CTL);
2584 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2585 __func__, (uintmax_t)ooa_hdr->lun_num);
2592 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2593 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2594 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2600 mtx_unlock(&softc->ctl_lock);
2601 free(entries, M_CTL);
2605 lun = softc->ctl_luns[ooa_hdr->lun_num];
2607 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr,
2610 mtx_unlock(&softc->ctl_lock);
2612 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2613 ooa_hdr->fill_len = ooa_hdr->fill_num *
2614 sizeof(struct ctl_ooa_entry);
2615 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2617 printf("%s: error copying out %d bytes for OOA dump\n",
2618 __func__, ooa_hdr->fill_len);
2621 getbintime(&ooa_hdr->cur_bt);
2623 if (cur_fill_num > ooa_hdr->alloc_num) {
2624 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2625 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2627 ooa_hdr->dropped_num = 0;
2628 ooa_hdr->status = CTL_OOA_OK;
2631 free(entries, M_CTL);
2634 case CTL_CHECK_OOA: {
2636 struct ctl_lun *lun;
2637 struct ctl_ooa_info *ooa_info;
2640 ooa_info = (struct ctl_ooa_info *)addr;
2642 if (ooa_info->lun_id >= CTL_MAX_LUNS) {
2643 ooa_info->status = CTL_OOA_INVALID_LUN;
2646 mtx_lock(&softc->ctl_lock);
2647 lun = softc->ctl_luns[ooa_info->lun_id];
2649 mtx_unlock(&softc->ctl_lock);
2650 ooa_info->status = CTL_OOA_INVALID_LUN;
2653 mtx_lock(&lun->lun_lock);
2654 mtx_unlock(&softc->ctl_lock);
2655 ooa_info->num_entries = 0;
2656 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
2657 io != NULL; io = (union ctl_io *)TAILQ_NEXT(
2658 &io->io_hdr, ooa_links)) {
2659 ooa_info->num_entries++;
2661 mtx_unlock(&lun->lun_lock);
2663 ooa_info->status = CTL_OOA_SUCCESS;
2667 case CTL_HARD_START:
2668 case CTL_HARD_STOP: {
2669 struct ctl_fe_ioctl_startstop_info ss_info;
2670 struct cfi_metatask *metatask;
2673 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF);
2675 cv_init(&ss_info.sem, "hard start/stop cv" );
2677 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2678 if (metatask == NULL) {
2680 mtx_destroy(&hs_mtx);
2684 if (cmd == CTL_HARD_START)
2685 metatask->tasktype = CFI_TASK_STARTUP;
2687 metatask->tasktype = CFI_TASK_SHUTDOWN;
2689 metatask->callback = ctl_ioctl_hard_startstop_callback;
2690 metatask->callback_arg = &ss_info;
2692 cfi_action(metatask);
2694 /* Wait for the callback */
2696 cv_wait_sig(&ss_info.sem, &hs_mtx);
2697 mtx_unlock(&hs_mtx);
2700 * All information has been copied from the metatask by the
2701 * time cv_broadcast() is called, so we free the metatask here.
2703 cfi_free_metatask(metatask);
2705 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info));
2707 mtx_destroy(&hs_mtx);
2711 struct ctl_bbrread_info *bbr_info;
2712 struct ctl_fe_ioctl_bbrread_info fe_bbr_info;
2714 struct cfi_metatask *metatask;
2716 bbr_info = (struct ctl_bbrread_info *)addr;
2718 bzero(&fe_bbr_info, sizeof(fe_bbr_info));
2720 bzero(&bbr_mtx, sizeof(bbr_mtx));
2721 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF);
2723 fe_bbr_info.bbr_info = bbr_info;
2724 fe_bbr_info.lock = &bbr_mtx;
2726 cv_init(&fe_bbr_info.sem, "BBR read cv");
2727 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2729 if (metatask == NULL) {
2730 mtx_destroy(&bbr_mtx);
2731 cv_destroy(&fe_bbr_info.sem);
2735 metatask->tasktype = CFI_TASK_BBRREAD;
2736 metatask->callback = ctl_ioctl_bbrread_callback;
2737 metatask->callback_arg = &fe_bbr_info;
2738 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num;
2739 metatask->taskinfo.bbrread.lba = bbr_info->lba;
2740 metatask->taskinfo.bbrread.len = bbr_info->len;
2742 cfi_action(metatask);
2745 while (fe_bbr_info.wakeup_done == 0)
2746 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx);
2747 mtx_unlock(&bbr_mtx);
2749 bbr_info->status = metatask->status;
2750 bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2751 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status;
2752 memcpy(&bbr_info->sense_data,
2753 &metatask->taskinfo.bbrread.sense_data,
2754 ctl_min(sizeof(bbr_info->sense_data),
2755 sizeof(metatask->taskinfo.bbrread.sense_data)));
2757 cfi_free_metatask(metatask);
2759 mtx_destroy(&bbr_mtx);
2760 cv_destroy(&fe_bbr_info.sem);
2764 case CTL_DELAY_IO: {
2765 struct ctl_io_delay_info *delay_info;
2767 struct ctl_lun *lun;
2768 #endif /* CTL_IO_DELAY */
2770 delay_info = (struct ctl_io_delay_info *)addr;
2773 mtx_lock(&softc->ctl_lock);
2775 if ((delay_info->lun_id >= CTL_MAX_LUNS)
2776 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2777 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2779 lun = softc->ctl_luns[delay_info->lun_id];
2780 mtx_lock(&lun->lun_lock);
2782 delay_info->status = CTL_DELAY_STATUS_OK;
2784 switch (delay_info->delay_type) {
2785 case CTL_DELAY_TYPE_CONT:
2787 case CTL_DELAY_TYPE_ONESHOT:
2790 delay_info->status =
2791 CTL_DELAY_STATUS_INVALID_TYPE;
2795 switch (delay_info->delay_loc) {
2796 case CTL_DELAY_LOC_DATAMOVE:
2797 lun->delay_info.datamove_type =
2798 delay_info->delay_type;
2799 lun->delay_info.datamove_delay =
2800 delay_info->delay_secs;
2802 case CTL_DELAY_LOC_DONE:
2803 lun->delay_info.done_type =
2804 delay_info->delay_type;
2805 lun->delay_info.done_delay =
2806 delay_info->delay_secs;
2809 delay_info->status =
2810 CTL_DELAY_STATUS_INVALID_LOC;
2813 mtx_unlock(&lun->lun_lock);
2816 mtx_unlock(&softc->ctl_lock);
2818 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2819 #endif /* CTL_IO_DELAY */
2822 case CTL_REALSYNC_SET: {
2825 syncstate = (int *)addr;
2827 mtx_lock(&softc->ctl_lock);
2828 switch (*syncstate) {
2830 softc->flags &= ~CTL_FLAG_REAL_SYNC;
2833 softc->flags |= CTL_FLAG_REAL_SYNC;
2839 mtx_unlock(&softc->ctl_lock);
2842 case CTL_REALSYNC_GET: {
2845 syncstate = (int*)addr;
2847 mtx_lock(&softc->ctl_lock);
2848 if (softc->flags & CTL_FLAG_REAL_SYNC)
2852 mtx_unlock(&softc->ctl_lock);
2858 struct ctl_sync_info *sync_info;
2859 struct ctl_lun *lun;
2861 sync_info = (struct ctl_sync_info *)addr;
2863 mtx_lock(&softc->ctl_lock);
2864 lun = softc->ctl_luns[sync_info->lun_id];
2866 mtx_unlock(&softc->ctl_lock);
2867 sync_info->status = CTL_GS_SYNC_NO_LUN;
2870 * Get or set the sync interval. We're not bounds checking
2871 * in the set case, hopefully the user won't do something
2874 mtx_lock(&lun->lun_lock);
2875 mtx_unlock(&softc->ctl_lock);
2876 if (cmd == CTL_GETSYNC)
2877 sync_info->sync_interval = lun->sync_interval;
2879 lun->sync_interval = sync_info->sync_interval;
2880 mtx_unlock(&lun->lun_lock);
2882 sync_info->status = CTL_GS_SYNC_OK;
2886 case CTL_GETSTATS: {
2887 struct ctl_stats *stats;
2888 struct ctl_lun *lun;
2891 stats = (struct ctl_stats *)addr;
2893 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2895 stats->status = CTL_SS_NEED_MORE_SPACE;
2896 stats->num_luns = softc->num_luns;
2900 * XXX KDM no locking here. If the LUN list changes,
2901 * things can blow up.
2903 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
2904 i++, lun = STAILQ_NEXT(lun, links)) {
2905 retval = copyout(&lun->stats, &stats->lun_stats[i],
2906 sizeof(lun->stats));
2910 stats->num_luns = softc->num_luns;
2911 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2913 stats->status = CTL_SS_OK;
2915 stats->flags = CTL_STATS_FLAG_TIME_VALID;
2917 stats->flags = CTL_STATS_FLAG_NONE;
2919 getnanouptime(&stats->timestamp);
2922 case CTL_ERROR_INJECT: {
2923 struct ctl_error_desc *err_desc, *new_err_desc;
2924 struct ctl_lun *lun;
2926 err_desc = (struct ctl_error_desc *)addr;
2928 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2930 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2932 mtx_lock(&softc->ctl_lock);
2933 lun = softc->ctl_luns[err_desc->lun_id];
2935 mtx_unlock(&softc->ctl_lock);
2936 free(new_err_desc, M_CTL);
2937 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2938 __func__, (uintmax_t)err_desc->lun_id);
2942 mtx_lock(&lun->lun_lock);
2943 mtx_unlock(&softc->ctl_lock);
2946 * We could do some checking here to verify the validity
2947 * of the request, but given the complexity of error
2948 * injection requests, the checking logic would be fairly
2951 * For now, if the request is invalid, it just won't get
2952 * executed and might get deleted.
2954 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2957 * XXX KDM check to make sure the serial number is unique,
2958 * in case we somehow manage to wrap. That shouldn't
2959 * happen for a very long time, but it's the right thing to
2962 new_err_desc->serial = lun->error_serial;
2963 err_desc->serial = lun->error_serial;
2964 lun->error_serial++;
2966 mtx_unlock(&lun->lun_lock);
2969 case CTL_ERROR_INJECT_DELETE: {
2970 struct ctl_error_desc *delete_desc, *desc, *desc2;
2971 struct ctl_lun *lun;
2974 delete_desc = (struct ctl_error_desc *)addr;
2977 mtx_lock(&softc->ctl_lock);
2978 lun = softc->ctl_luns[delete_desc->lun_id];
2980 mtx_unlock(&softc->ctl_lock);
2981 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2982 __func__, (uintmax_t)delete_desc->lun_id);
2986 mtx_lock(&lun->lun_lock);
2987 mtx_unlock(&softc->ctl_lock);
2988 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2989 if (desc->serial != delete_desc->serial)
2992 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2997 mtx_unlock(&lun->lun_lock);
2998 if (delete_done == 0) {
2999 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
3000 "error serial %ju on LUN %u\n", __func__,
3001 delete_desc->serial, delete_desc->lun_id);
3007 case CTL_DUMP_STRUCTS: {
3009 struct ctl_port *port;
3010 struct ctl_frontend *fe;
3012 mtx_lock(&softc->ctl_lock);
3013 printf("CTL Persistent Reservation information start:\n");
3014 for (i = 0; i < CTL_MAX_LUNS; i++) {
3015 struct ctl_lun *lun;
3017 lun = softc->ctl_luns[i];
3020 || ((lun->flags & CTL_LUN_DISABLED) != 0))
3023 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) {
3024 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
3025 idx = j * CTL_MAX_INIT_PER_PORT + k;
3026 if (lun->pr_keys[idx] == 0)
3028 printf(" LUN %d port %d iid %d key "
3030 (uintmax_t)lun->pr_keys[idx]);
3034 printf("CTL Persistent Reservation information end\n");
3035 printf("CTL Ports:\n");
3036 STAILQ_FOREACH(port, &softc->port_list, links) {
3037 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
3038 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
3039 port->frontend->name, port->port_type,
3040 port->physical_port, port->virtual_port,
3041 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
3042 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3043 if (port->wwpn_iid[j].in_use == 0 &&
3044 port->wwpn_iid[j].wwpn == 0 &&
3045 port->wwpn_iid[j].name == NULL)
3048 printf(" iid %u use %d WWPN %#jx '%s'\n",
3049 j, port->wwpn_iid[j].in_use,
3050 (uintmax_t)port->wwpn_iid[j].wwpn,
3051 port->wwpn_iid[j].name);
3054 printf("CTL Port information end\n");
3055 mtx_unlock(&softc->ctl_lock);
3057 * XXX KDM calling this without a lock. We'd likely want
3058 * to drop the lock before calling the frontend's dump
3061 printf("CTL Frontends:\n");
3062 STAILQ_FOREACH(fe, &softc->fe_list, links) {
3063 printf(" Frontend '%s'\n", fe->name);
3064 if (fe->fe_dump != NULL)
3067 printf("CTL Frontend information end\n");
3071 struct ctl_lun_req *lun_req;
3072 struct ctl_backend_driver *backend;
3074 lun_req = (struct ctl_lun_req *)addr;
3076 backend = ctl_backend_find(lun_req->backend);
3077 if (backend == NULL) {
3078 lun_req->status = CTL_LUN_ERROR;
3079 snprintf(lun_req->error_str,
3080 sizeof(lun_req->error_str),
3081 "Backend \"%s\" not found.",
3085 if (lun_req->num_be_args > 0) {
3086 lun_req->kern_be_args = ctl_copyin_args(
3087 lun_req->num_be_args,
3090 sizeof(lun_req->error_str));
3091 if (lun_req->kern_be_args == NULL) {
3092 lun_req->status = CTL_LUN_ERROR;
3097 retval = backend->ioctl(dev, cmd, addr, flag, td);
3099 if (lun_req->num_be_args > 0) {
3100 ctl_copyout_args(lun_req->num_be_args,
3101 lun_req->kern_be_args);
3102 ctl_free_args(lun_req->num_be_args,
3103 lun_req->kern_be_args);
3107 case CTL_LUN_LIST: {
3109 struct ctl_lun *lun;
3110 struct ctl_lun_list *list;
3111 struct ctl_option *opt;
3113 list = (struct ctl_lun_list *)addr;
3116 * Allocate a fixed length sbuf here, based on the length
3117 * of the user's buffer. We could allocate an auto-extending
3118 * buffer, and then tell the user how much larger our
3119 * amount of data is than his buffer, but that presents
3122 * 1. The sbuf(9) routines use a blocking malloc, and so
3123 * we can't hold a lock while calling them with an
3124 * auto-extending buffer.
3126 * 2. There is not currently a LUN reference counting
3127 * mechanism, outside of outstanding transactions on
3128 * the LUN's OOA queue. So a LUN could go away on us
3129 * while we're getting the LUN number, backend-specific
3130 * information, etc. Thus, given the way things
3131 * currently work, we need to hold the CTL lock while
3132 * grabbing LUN information.
3134 * So, from the user's standpoint, the best thing to do is
3135 * allocate what he thinks is a reasonable buffer length,
3136 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3137 * double the buffer length and try again. (And repeat
3138 * that until he succeeds.)
3140 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3142 list->status = CTL_LUN_LIST_ERROR;
3143 snprintf(list->error_str, sizeof(list->error_str),
3144 "Unable to allocate %d bytes for LUN list",
3149 sbuf_printf(sb, "<ctllunlist>\n");
3151 mtx_lock(&softc->ctl_lock);
3152 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3153 mtx_lock(&lun->lun_lock);
3154 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3155 (uintmax_t)lun->lun);
3158 * Bail out as soon as we see that we've overfilled
3164 retval = sbuf_printf(sb, "\t<backend_type>%s"
3165 "</backend_type>\n",
3166 (lun->backend == NULL) ? "none" :
3167 lun->backend->name);
3172 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3173 lun->be_lun->lun_type);
3178 if (lun->backend == NULL) {
3179 retval = sbuf_printf(sb, "</lun>\n");
3185 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3186 (lun->be_lun->maxlba > 0) ?
3187 lun->be_lun->maxlba + 1 : 0);
3192 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3193 lun->be_lun->blocksize);
3198 retval = sbuf_printf(sb, "\t<serial_number>");
3203 retval = ctl_sbuf_printf_esc(sb,
3204 lun->be_lun->serial_num,
3205 sizeof(lun->be_lun->serial_num));
3210 retval = sbuf_printf(sb, "</serial_number>\n");
3215 retval = sbuf_printf(sb, "\t<device_id>");
3220 retval = ctl_sbuf_printf_esc(sb,
3221 lun->be_lun->device_id,
3222 sizeof(lun->be_lun->device_id));
3227 retval = sbuf_printf(sb, "</device_id>\n");
3232 if (lun->backend->lun_info != NULL) {
3233 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3237 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3238 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3239 opt->name, opt->value, opt->name);
3244 retval = sbuf_printf(sb, "</lun>\n");
3248 mtx_unlock(&lun->lun_lock);
3251 mtx_unlock(&lun->lun_lock);
3252 mtx_unlock(&softc->ctl_lock);
3255 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3258 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3259 snprintf(list->error_str, sizeof(list->error_str),
3260 "Out of space, %d bytes is too small",
3267 retval = copyout(sbuf_data(sb), list->lun_xml,
3270 list->fill_len = sbuf_len(sb) + 1;
3271 list->status = CTL_LUN_LIST_OK;
3276 struct ctl_iscsi *ci;
3277 struct ctl_frontend *fe;
3279 ci = (struct ctl_iscsi *)addr;
3281 fe = ctl_frontend_find("iscsi");
3283 ci->status = CTL_ISCSI_ERROR;
3284 snprintf(ci->error_str, sizeof(ci->error_str),
3285 "Frontend \"iscsi\" not found.");
3289 retval = fe->ioctl(dev, cmd, addr, flag, td);
3292 case CTL_PORT_REQ: {
3293 struct ctl_req *req;
3294 struct ctl_frontend *fe;
3296 req = (struct ctl_req *)addr;
3298 fe = ctl_frontend_find(req->driver);
3300 req->status = CTL_LUN_ERROR;
3301 snprintf(req->error_str, sizeof(req->error_str),
3302 "Frontend \"%s\" not found.", req->driver);
3305 if (req->num_args > 0) {
3306 req->kern_args = ctl_copyin_args(req->num_args,
3307 req->args, req->error_str, sizeof(req->error_str));
3308 if (req->kern_args == NULL) {
3309 req->status = CTL_LUN_ERROR;
3314 retval = fe->ioctl(dev, cmd, addr, flag, td);
3316 if (req->num_args > 0) {
3317 ctl_copyout_args(req->num_args, req->kern_args);
3318 ctl_free_args(req->num_args, req->kern_args);
3322 case CTL_PORT_LIST: {
3324 struct ctl_port *port;
3325 struct ctl_lun_list *list;
3326 struct ctl_option *opt;
3329 list = (struct ctl_lun_list *)addr;
3331 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3333 list->status = CTL_LUN_LIST_ERROR;
3334 snprintf(list->error_str, sizeof(list->error_str),
3335 "Unable to allocate %d bytes for LUN list",
3340 sbuf_printf(sb, "<ctlportlist>\n");
3342 mtx_lock(&softc->ctl_lock);
3343 STAILQ_FOREACH(port, &softc->port_list, links) {
3344 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3345 (uintmax_t)port->targ_port);
3348 * Bail out as soon as we see that we've overfilled
3354 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3355 "</frontend_type>\n", port->frontend->name);
3359 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3364 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3365 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3369 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3374 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3375 port->physical_port);
3379 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3380 port->virtual_port);
3384 if (port->target_devid != NULL) {
3385 sbuf_printf(sb, "\t<target>");
3386 ctl_id_sbuf(port->target_devid, sb);
3387 sbuf_printf(sb, "</target>\n");
3390 if (port->port_devid != NULL) {
3391 sbuf_printf(sb, "\t<port>");
3392 ctl_id_sbuf(port->port_devid, sb);
3393 sbuf_printf(sb, "</port>\n");
3396 if (port->port_info != NULL) {
3397 retval = port->port_info(port->onoff_arg, sb);
3401 STAILQ_FOREACH(opt, &port->options, links) {
3402 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3403 opt->name, opt->value, opt->name);
3408 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3409 if (port->wwpn_iid[j].in_use == 0 ||
3410 (port->wwpn_iid[j].wwpn == 0 &&
3411 port->wwpn_iid[j].name == NULL))
3414 if (port->wwpn_iid[j].name != NULL)
3415 retval = sbuf_printf(sb,
3416 "\t<initiator>%u %s</initiator>\n",
3417 j, port->wwpn_iid[j].name);
3419 retval = sbuf_printf(sb,
3420 "\t<initiator>%u naa.%08jx</initiator>\n",
3421 j, port->wwpn_iid[j].wwpn);
3428 retval = sbuf_printf(sb, "</targ_port>\n");
3432 mtx_unlock(&softc->ctl_lock);
3435 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3438 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3439 snprintf(list->error_str, sizeof(list->error_str),
3440 "Out of space, %d bytes is too small",
3447 retval = copyout(sbuf_data(sb), list->lun_xml,
3450 list->fill_len = sbuf_len(sb) + 1;
3451 list->status = CTL_LUN_LIST_OK;
3456 /* XXX KDM should we fix this? */
3458 struct ctl_backend_driver *backend;
3465 * We encode the backend type as the ioctl type for backend
3466 * ioctls. So parse it out here, and then search for a
3467 * backend of this type.
3469 type = _IOC_TYPE(cmd);
3471 STAILQ_FOREACH(backend, &softc->be_list, links) {
3472 if (backend->type == type) {
3478 printf("ctl: unknown ioctl command %#lx or backend "
3483 retval = backend->ioctl(dev, cmd, addr, flag, td);
3493 ctl_get_initindex(struct ctl_nexus *nexus)
3495 if (nexus->targ_port < CTL_MAX_PORTS)
3496 return (nexus->initid.id +
3497 (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3499 return (nexus->initid.id +
3500 ((nexus->targ_port - CTL_MAX_PORTS) *
3501 CTL_MAX_INIT_PER_PORT));
3505 ctl_get_resindex(struct ctl_nexus *nexus)
3507 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3511 ctl_port_idx(int port_num)
3513 if (port_num < CTL_MAX_PORTS)
3516 return(port_num - CTL_MAX_PORTS);
3520 ctl_map_lun(int port_num, uint32_t lun_id)
3522 struct ctl_port *port;
3524 port = control_softc->ctl_ports[ctl_port_idx(port_num)];
3526 return (UINT32_MAX);
3527 if (port->lun_map == NULL)
3529 return (port->lun_map(port->targ_lun_arg, lun_id));
3533 ctl_map_lun_back(int port_num, uint32_t lun_id)
3535 struct ctl_port *port;
3538 port = control_softc->ctl_ports[ctl_port_idx(port_num)];
3539 if (port->lun_map == NULL)
3541 for (i = 0; i < CTL_MAX_LUNS; i++) {
3542 if (port->lun_map(port->targ_lun_arg, i) == lun_id)
3545 return (UINT32_MAX);
3549 * Note: This only works for bitmask sizes that are at least 32 bits, and
3550 * that are a power of 2.
3553 ctl_ffz(uint32_t *mask, uint32_t size)
3555 uint32_t num_chunks, num_pieces;
3558 num_chunks = (size >> 5);
3559 if (num_chunks == 0)
3561 num_pieces = ctl_min((sizeof(uint32_t) * 8), size);
3563 for (i = 0; i < num_chunks; i++) {
3564 for (j = 0; j < num_pieces; j++) {
3565 if ((mask[i] & (1 << j)) == 0)
3566 return ((i << 5) + j);
3574 ctl_set_mask(uint32_t *mask, uint32_t bit)
3576 uint32_t chunk, piece;
3579 piece = bit % (sizeof(uint32_t) * 8);
3581 if ((mask[chunk] & (1 << piece)) != 0)
3584 mask[chunk] |= (1 << piece);
3590 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3592 uint32_t chunk, piece;
3595 piece = bit % (sizeof(uint32_t) * 8);
3597 if ((mask[chunk] & (1 << piece)) == 0)
3600 mask[chunk] &= ~(1 << piece);
3606 ctl_is_set(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)
3621 * The bus, target and lun are optional, they can be filled in later.
3622 * can_wait is used to determine whether we can wait on the malloc or not.
3625 ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target,
3626 uint32_t targ_lun, int can_wait)
3631 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK);
3633 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT);
3636 io->io_hdr.io_type = io_type;
3637 io->io_hdr.targ_port = targ_port;
3639 * XXX KDM this needs to change/go away. We need to move
3640 * to a preallocated pool of ctl_scsiio structures.
3642 io->io_hdr.nexus.targ_target.id = targ_target;
3643 io->io_hdr.nexus.targ_lun = targ_lun;
3650 ctl_kfree_io(union ctl_io *io)
3657 * ctl_softc, pool_type, total_ctl_io are passed in.
3658 * npool is passed out.
3661 ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type,
3662 uint32_t total_ctl_io, struct ctl_io_pool **npool)
3665 union ctl_io *cur_io, *next_io;
3666 struct ctl_io_pool *pool;
3671 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3678 pool->type = pool_type;
3679 pool->ctl_softc = ctl_softc;
3681 mtx_lock(&ctl_softc->pool_lock);
3682 pool->id = ctl_softc->cur_pool_id++;
3683 mtx_unlock(&ctl_softc->pool_lock);
3685 pool->flags = CTL_POOL_FLAG_NONE;
3686 pool->refcount = 1; /* Reference for validity. */
3687 STAILQ_INIT(&pool->free_queue);
3690 * XXX KDM other options here:
3691 * - allocate a page at a time
3692 * - allocate one big chunk of memory.
3693 * Page allocation might work well, but would take a little more
3696 for (i = 0; i < total_ctl_io; i++) {
3697 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTLIO,
3699 if (cur_io == NULL) {
3703 cur_io->io_hdr.pool = pool;
3704 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links);
3705 pool->total_ctl_io++;
3706 pool->free_ctl_io++;
3710 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3711 cur_io != NULL; cur_io = next_io) {
3712 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr,
3714 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr,
3716 free(cur_io, M_CTLIO);
3722 mtx_lock(&ctl_softc->pool_lock);
3723 ctl_softc->num_pools++;
3724 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links);
3726 * Increment our usage count if this is an external consumer, so we
3727 * can't get unloaded until the external consumer (most likely a
3728 * FETD) unloads and frees his pool.
3730 * XXX KDM will this increment the caller's module use count, or
3734 if ((pool_type != CTL_POOL_EMERGENCY)
3735 && (pool_type != CTL_POOL_INTERNAL)
3736 && (pool_type != CTL_POOL_4OTHERSC))
3740 mtx_unlock(&ctl_softc->pool_lock);
3750 ctl_pool_acquire(struct ctl_io_pool *pool)
3753 mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED);
3755 if (pool->flags & CTL_POOL_FLAG_INVALID)
3764 ctl_pool_release(struct ctl_io_pool *pool)
3766 struct ctl_softc *ctl_softc = pool->ctl_softc;
3769 mtx_assert(&ctl_softc->pool_lock, MA_OWNED);
3771 if (--pool->refcount != 0)
3774 while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) {
3775 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr,
3780 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links);
3781 ctl_softc->num_pools--;
3784 * XXX KDM will this decrement the caller's usage count or mine?
3787 if ((pool->type != CTL_POOL_EMERGENCY)
3788 && (pool->type != CTL_POOL_INTERNAL)
3789 && (pool->type != CTL_POOL_4OTHERSC))
3797 ctl_pool_free(struct ctl_io_pool *pool)
3799 struct ctl_softc *ctl_softc;
3804 ctl_softc = pool->ctl_softc;
3805 mtx_lock(&ctl_softc->pool_lock);
3806 pool->flags |= CTL_POOL_FLAG_INVALID;
3807 ctl_pool_release(pool);
3808 mtx_unlock(&ctl_softc->pool_lock);
3812 * This routine does not block (except for spinlocks of course).
3813 * It tries to allocate a ctl_io union from the caller's pool as quickly as
3817 ctl_alloc_io(void *pool_ref)
3820 struct ctl_softc *ctl_softc;
3821 struct ctl_io_pool *pool, *npool;
3822 struct ctl_io_pool *emergency_pool;
3824 pool = (struct ctl_io_pool *)pool_ref;
3827 printf("%s: pool is NULL\n", __func__);
3831 emergency_pool = NULL;
3833 ctl_softc = pool->ctl_softc;
3835 mtx_lock(&ctl_softc->pool_lock);
3837 * First, try to get the io structure from the user's pool.
3839 if (ctl_pool_acquire(pool) == 0) {
3840 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3842 STAILQ_REMOVE_HEAD(&pool->free_queue, links);
3843 pool->total_allocated++;
3844 pool->free_ctl_io--;
3845 mtx_unlock(&ctl_softc->pool_lock);
3848 ctl_pool_release(pool);
3851 * If he doesn't have any io structures left, search for an
3852 * emergency pool and grab one from there.
3854 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) {
3855 if (npool->type != CTL_POOL_EMERGENCY)
3858 if (ctl_pool_acquire(npool) != 0)
3861 emergency_pool = npool;
3863 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue);
3865 STAILQ_REMOVE_HEAD(&npool->free_queue, links);
3866 npool->total_allocated++;
3867 npool->free_ctl_io--;
3868 mtx_unlock(&ctl_softc->pool_lock);
3871 ctl_pool_release(npool);
3874 /* Drop the spinlock before we malloc */
3875 mtx_unlock(&ctl_softc->pool_lock);
3878 * The emergency pool (if it exists) didn't have one, so try an
3879 * atomic (i.e. nonblocking) malloc and see if we get lucky.
3881 io = (union ctl_io *)malloc(sizeof(*io), M_CTLIO, M_NOWAIT);
3884 * If the emergency pool exists but is empty, add this
3885 * ctl_io to its list when it gets freed.
3887 if (emergency_pool != NULL) {
3888 mtx_lock(&ctl_softc->pool_lock);
3889 if (ctl_pool_acquire(emergency_pool) == 0) {
3890 io->io_hdr.pool = emergency_pool;
3891 emergency_pool->total_ctl_io++;
3893 * Need to bump this, otherwise
3894 * total_allocated and total_freed won't
3895 * match when we no longer have anything
3898 emergency_pool->total_allocated++;
3900 mtx_unlock(&ctl_softc->pool_lock);
3902 io->io_hdr.pool = NULL;
3909 ctl_free_io(union ctl_io *io)
3915 * If this ctl_io has a pool, return it to that pool.
3917 if (io->io_hdr.pool != NULL) {
3918 struct ctl_io_pool *pool;
3920 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3921 mtx_lock(&pool->ctl_softc->pool_lock);
3922 io->io_hdr.io_type = 0xff;
3923 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links);
3924 pool->total_freed++;
3925 pool->free_ctl_io++;
3926 ctl_pool_release(pool);
3927 mtx_unlock(&pool->ctl_softc->pool_lock);
3930 * Otherwise, just free it. We probably malloced it and
3931 * the emergency pool wasn't available.
3939 ctl_zero_io(union ctl_io *io)
3947 * May need to preserve linked list pointers at some point too.
3949 pool_ref = io->io_hdr.pool;
3951 memset(io, 0, sizeof(*io));
3953 io->io_hdr.pool = pool_ref;
3957 * This routine is currently used for internal copies of ctl_ios that need
3958 * to persist for some reason after we've already returned status to the
3959 * FETD. (Thus the flag set.)
3962 * Note that this makes a blind copy of all fields in the ctl_io, except
3963 * for the pool reference. This includes any memory that has been
3964 * allocated! That memory will no longer be valid after done has been
3965 * called, so this would be VERY DANGEROUS for command that actually does
3966 * any reads or writes. Right now (11/7/2005), this is only used for immediate
3967 * start and stop commands, which don't transfer any data, so this is not a
3968 * problem. If it is used for anything else, the caller would also need to
3969 * allocate data buffer space and this routine would need to be modified to
3970 * copy the data buffer(s) as well.
3973 ctl_copy_io(union ctl_io *src, union ctl_io *dest)
3982 * May need to preserve linked list pointers at some point too.
3984 pool_ref = dest->io_hdr.pool;
3986 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest)));
3988 dest->io_hdr.pool = pool_ref;
3990 * We need to know that this is an internal copy, and doesn't need
3991 * to get passed back to the FETD that allocated it.
3993 dest->io_hdr.flags |= CTL_FLAG_INT_COPY;
3997 * This routine could be used in the future to load default and/or saved
3998 * mode page parameters for a particuar lun.
4001 ctl_init_page_index(struct ctl_lun *lun)
4004 struct ctl_page_index *page_index;
4007 memcpy(&lun->mode_pages.index, page_index_template,
4008 sizeof(page_index_template));
4010 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4012 page_index = &lun->mode_pages.index[i];
4014 * If this is a disk-only mode page, there's no point in
4015 * setting it up. For some pages, we have to have some
4016 * basic information about the disk in order to calculate the
4019 if ((lun->be_lun->lun_type != T_DIRECT)
4020 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
4023 switch (page_index->page_code & SMPH_PC_MASK) {
4024 case SMS_RW_ERROR_RECOVERY_PAGE: {
4025 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4026 panic("subpage is incorrect!");
4027 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
4028 &rw_er_page_default,
4029 sizeof(rw_er_page_default));
4030 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
4031 &rw_er_page_changeable,
4032 sizeof(rw_er_page_changeable));
4033 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
4034 &rw_er_page_default,
4035 sizeof(rw_er_page_default));
4036 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
4037 &rw_er_page_default,
4038 sizeof(rw_er_page_default));
4039 page_index->page_data =
4040 (uint8_t *)lun->mode_pages.rw_er_page;
4043 case SMS_FORMAT_DEVICE_PAGE: {
4044 struct scsi_format_page *format_page;
4046 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4047 panic("subpage is incorrect!");
4050 * Sectors per track are set above. Bytes per
4051 * sector need to be set here on a per-LUN basis.
4053 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
4054 &format_page_default,
4055 sizeof(format_page_default));
4056 memcpy(&lun->mode_pages.format_page[
4057 CTL_PAGE_CHANGEABLE], &format_page_changeable,
4058 sizeof(format_page_changeable));
4059 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
4060 &format_page_default,
4061 sizeof(format_page_default));
4062 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
4063 &format_page_default,
4064 sizeof(format_page_default));
4066 format_page = &lun->mode_pages.format_page[
4068 scsi_ulto2b(lun->be_lun->blocksize,
4069 format_page->bytes_per_sector);
4071 format_page = &lun->mode_pages.format_page[
4073 scsi_ulto2b(lun->be_lun->blocksize,
4074 format_page->bytes_per_sector);
4076 format_page = &lun->mode_pages.format_page[
4078 scsi_ulto2b(lun->be_lun->blocksize,
4079 format_page->bytes_per_sector);
4081 page_index->page_data =
4082 (uint8_t *)lun->mode_pages.format_page;
4085 case SMS_RIGID_DISK_PAGE: {
4086 struct scsi_rigid_disk_page *rigid_disk_page;
4087 uint32_t sectors_per_cylinder;
4091 #endif /* !__XSCALE__ */
4093 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4094 panic("invalid subpage value %d",
4095 page_index->subpage);
4098 * Rotation rate and sectors per track are set
4099 * above. We calculate the cylinders here based on
4100 * capacity. Due to the number of heads and
4101 * sectors per track we're using, smaller arrays
4102 * may turn out to have 0 cylinders. Linux and
4103 * FreeBSD don't pay attention to these mode pages
4104 * to figure out capacity, but Solaris does. It
4105 * seems to deal with 0 cylinders just fine, and
4106 * works out a fake geometry based on the capacity.
4108 memcpy(&lun->mode_pages.rigid_disk_page[
4109 CTL_PAGE_CURRENT], &rigid_disk_page_default,
4110 sizeof(rigid_disk_page_default));
4111 memcpy(&lun->mode_pages.rigid_disk_page[
4112 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4113 sizeof(rigid_disk_page_changeable));
4114 memcpy(&lun->mode_pages.rigid_disk_page[
4115 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4116 sizeof(rigid_disk_page_default));
4117 memcpy(&lun->mode_pages.rigid_disk_page[
4118 CTL_PAGE_SAVED], &rigid_disk_page_default,
4119 sizeof(rigid_disk_page_default));
4121 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4125 * The divide method here will be more accurate,
4126 * probably, but results in floating point being
4127 * used in the kernel on i386 (__udivdi3()). On the
4128 * XScale, though, __udivdi3() is implemented in
4131 * The shift method for cylinder calculation is
4132 * accurate if sectors_per_cylinder is a power of
4133 * 2. Otherwise it might be slightly off -- you
4134 * might have a bit of a truncation problem.
4137 cylinders = (lun->be_lun->maxlba + 1) /
4138 sectors_per_cylinder;
4140 for (shift = 31; shift > 0; shift--) {
4141 if (sectors_per_cylinder & (1 << shift))
4144 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4148 * We've basically got 3 bytes, or 24 bits for the
4149 * cylinder size in the mode page. If we're over,
4150 * just round down to 2^24.
4152 if (cylinders > 0xffffff)
4153 cylinders = 0xffffff;
4155 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4157 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4159 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4161 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4163 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4165 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4167 page_index->page_data =
4168 (uint8_t *)lun->mode_pages.rigid_disk_page;
4171 case SMS_CACHING_PAGE: {
4172 struct scsi_caching_page *caching_page;
4174 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4175 panic("invalid subpage value %d",
4176 page_index->subpage);
4177 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4178 &caching_page_default,
4179 sizeof(caching_page_default));
4180 memcpy(&lun->mode_pages.caching_page[
4181 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4182 sizeof(caching_page_changeable));
4183 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4184 &caching_page_default,
4185 sizeof(caching_page_default));
4186 caching_page = &lun->mode_pages.caching_page[
4188 value = ctl_get_opt(&lun->be_lun->options, "writecache");
4189 if (value != NULL && strcmp(value, "off") == 0)
4190 caching_page->flags1 &= ~SCP_WCE;
4191 value = ctl_get_opt(&lun->be_lun->options, "readcache");
4192 if (value != NULL && strcmp(value, "off") == 0)
4193 caching_page->flags1 |= SCP_RCD;
4194 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4195 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4196 sizeof(caching_page_default));
4197 page_index->page_data =
4198 (uint8_t *)lun->mode_pages.caching_page;
4201 case SMS_CONTROL_MODE_PAGE: {
4202 struct scsi_control_page *control_page;
4204 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4205 panic("invalid subpage value %d",
4206 page_index->subpage);
4208 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT],
4209 &control_page_default,
4210 sizeof(control_page_default));
4211 memcpy(&lun->mode_pages.control_page[
4212 CTL_PAGE_CHANGEABLE], &control_page_changeable,
4213 sizeof(control_page_changeable));
4214 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED],
4215 &control_page_default,
4216 sizeof(control_page_default));
4217 control_page = &lun->mode_pages.control_page[
4219 value = ctl_get_opt(&lun->be_lun->options, "reordering");
4220 if (value != NULL && strcmp(value, "unrestricted") == 0) {
4221 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK;
4222 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED;
4224 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT],
4225 &lun->mode_pages.control_page[CTL_PAGE_SAVED],
4226 sizeof(control_page_default));
4227 page_index->page_data =
4228 (uint8_t *)lun->mode_pages.control_page;
4232 case SMS_INFO_EXCEPTIONS_PAGE: {
4233 switch (page_index->subpage) {
4234 case SMS_SUBPAGE_PAGE_0:
4235 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4237 sizeof(ie_page_default));
4238 memcpy(&lun->mode_pages.ie_page[
4239 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4240 sizeof(ie_page_changeable));
4241 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4243 sizeof(ie_page_default));
4244 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4246 sizeof(ie_page_default));
4247 page_index->page_data =
4248 (uint8_t *)lun->mode_pages.ie_page;
4251 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4253 sizeof(lbp_page_default));
4254 memcpy(&lun->mode_pages.lbp_page[
4255 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4256 sizeof(lbp_page_changeable));
4257 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4259 sizeof(lbp_page_default));
4260 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4262 sizeof(lbp_page_default));
4263 page_index->page_data =
4264 (uint8_t *)lun->mode_pages.lbp_page;
4268 case SMS_VENDOR_SPECIFIC_PAGE:{
4269 switch (page_index->subpage) {
4270 case DBGCNF_SUBPAGE_CODE: {
4271 struct copan_debugconf_subpage *current_page,
4274 memcpy(&lun->mode_pages.debugconf_subpage[
4276 &debugconf_page_default,
4277 sizeof(debugconf_page_default));
4278 memcpy(&lun->mode_pages.debugconf_subpage[
4279 CTL_PAGE_CHANGEABLE],
4280 &debugconf_page_changeable,
4281 sizeof(debugconf_page_changeable));
4282 memcpy(&lun->mode_pages.debugconf_subpage[
4284 &debugconf_page_default,
4285 sizeof(debugconf_page_default));
4286 memcpy(&lun->mode_pages.debugconf_subpage[
4288 &debugconf_page_default,
4289 sizeof(debugconf_page_default));
4290 page_index->page_data =
4291 (uint8_t *)lun->mode_pages.debugconf_subpage;
4293 current_page = (struct copan_debugconf_subpage *)
4294 (page_index->page_data +
4295 (page_index->page_len *
4297 saved_page = (struct copan_debugconf_subpage *)
4298 (page_index->page_data +
4299 (page_index->page_len *
4304 panic("invalid subpage value %d",
4305 page_index->subpage);
4311 panic("invalid page value %d",
4312 page_index->page_code & SMPH_PC_MASK);
4317 return (CTL_RETVAL_COMPLETE);
4321 ctl_init_log_page_index(struct ctl_lun *lun)
4323 struct ctl_page_index *page_index;
4326 memcpy(&lun->log_pages.index, log_page_index_template,
4327 sizeof(log_page_index_template));
4330 for (i = 0, j = 0; i < CTL_NUM_LOG_PAGES; i++) {
4332 page_index = &lun->log_pages.index[i];
4334 * If this is a disk-only mode page, there's no point in
4335 * setting it up. For some pages, we have to have some
4336 * basic information about the disk in order to calculate the
4339 if ((lun->be_lun->lun_type != T_DIRECT)
4340 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
4343 if (page_index->page_code != prev) {
4344 lun->log_pages.pages_page[j] = page_index->page_code;
4345 prev = page_index->page_code;
4348 lun->log_pages.subpages_page[i*2] = page_index->page_code;
4349 lun->log_pages.subpages_page[i*2+1] = page_index->subpage;
4351 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4352 lun->log_pages.index[0].page_len = j;
4353 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4354 lun->log_pages.index[1].page_len = i * 2;
4356 return (CTL_RETVAL_COMPLETE);
4360 hex2bin(const char *str, uint8_t *buf, int buf_size)
4365 memset(buf, 0, buf_size);
4366 while (isspace(str[0]))
4368 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4371 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4375 else if (isalpha(c))
4376 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4382 buf[i / 2] |= (c << 4);
4386 return ((i + 1) / 2);
4393 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4394 * wants us to allocate the LUN and he can block.
4395 * - ctl_softc is always set
4396 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4398 * Returns 0 for success, non-zero (errno) for failure.
4401 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4402 struct ctl_be_lun *const be_lun, struct ctl_id target_id)
4404 struct ctl_lun *nlun, *lun;
4405 struct ctl_port *port;
4406 struct scsi_vpd_id_descriptor *desc;
4407 struct scsi_vpd_id_t10 *t10id;
4408 const char *eui, *naa, *scsiname, *vendor, *value;
4409 int lun_number, i, lun_malloced;
4410 int devidlen, idlen1, idlen2 = 0, len;
4416 * We currently only support Direct Access or Processor LUN types.
4418 switch (be_lun->lun_type) {
4426 be_lun->lun_config_status(be_lun->be_lun,
4427 CTL_LUN_CONFIG_FAILURE);
4430 if (ctl_lun == NULL) {
4431 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4438 memset(lun, 0, sizeof(*lun));
4440 lun->flags = CTL_LUN_MALLOCED;
4442 /* Generate LUN ID. */
4443 devidlen = max(CTL_DEVID_MIN_LEN,
4444 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4445 idlen1 = sizeof(*t10id) + devidlen;
4446 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4447 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4448 if (scsiname != NULL) {
4449 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4450 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4452 eui = ctl_get_opt(&be_lun->options, "eui");
4454 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4456 naa = ctl_get_opt(&be_lun->options, "naa");
4458 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4460 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4461 M_CTL, M_WAITOK | M_ZERO);
4462 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4463 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4464 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4465 desc->length = idlen1;
4466 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4467 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4468 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4469 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4471 strncpy(t10id->vendor, vendor,
4472 min(sizeof(t10id->vendor), strlen(vendor)));
4474 strncpy((char *)t10id->vendor_spec_id,
4475 (char *)be_lun->device_id, devidlen);
4476 if (scsiname != NULL) {
4477 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4479 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4480 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4481 SVPD_ID_TYPE_SCSI_NAME;
4482 desc->length = idlen2;
4483 strlcpy(desc->identifier, scsiname, idlen2);
4486 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4488 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4489 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4491 desc->length = hex2bin(eui, desc->identifier, 16);
4492 desc->length = desc->length > 12 ? 16 :
4493 (desc->length > 8 ? 12 : 8);
4494 len -= 16 - desc->length;
4497 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4499 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4500 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4502 desc->length = hex2bin(naa, desc->identifier, 16);
4503 desc->length = desc->length > 8 ? 16 : 8;
4504 len -= 16 - desc->length;
4506 lun->lun_devid->len = len;
4508 mtx_lock(&ctl_softc->ctl_lock);
4510 * See if the caller requested a particular LUN number. If so, see
4511 * if it is available. Otherwise, allocate the first available LUN.
4513 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4514 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4515 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4516 mtx_unlock(&ctl_softc->ctl_lock);
4517 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4518 printf("ctl: requested LUN ID %d is higher "
4519 "than CTL_MAX_LUNS - 1 (%d)\n",
4520 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4523 * XXX KDM return an error, or just assign
4524 * another LUN ID in this case??
4526 printf("ctl: requested LUN ID %d is already "
4527 "in use\n", be_lun->req_lun_id);
4529 if (lun->flags & CTL_LUN_MALLOCED)
4531 be_lun->lun_config_status(be_lun->be_lun,
4532 CTL_LUN_CONFIG_FAILURE);
4535 lun_number = be_lun->req_lun_id;
4537 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS);
4538 if (lun_number == -1) {
4539 mtx_unlock(&ctl_softc->ctl_lock);
4540 printf("ctl: can't allocate LUN on target %ju, out of "
4541 "LUNs\n", (uintmax_t)target_id.id);
4542 if (lun->flags & CTL_LUN_MALLOCED)
4544 be_lun->lun_config_status(be_lun->be_lun,
4545 CTL_LUN_CONFIG_FAILURE);
4549 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4551 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4552 lun->target = target_id;
4553 lun->lun = lun_number;
4554 lun->be_lun = be_lun;
4556 * The processor LUN is always enabled. Disk LUNs come on line
4557 * disabled, and must be enabled by the backend.
4559 lun->flags |= CTL_LUN_DISABLED;
4560 lun->backend = be_lun->be;
4561 be_lun->ctl_lun = lun;
4562 be_lun->lun_id = lun_number;
4563 atomic_add_int(&be_lun->be->num_luns, 1);
4564 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE)
4565 lun->flags |= CTL_LUN_OFFLINE;
4567 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4568 lun->flags |= CTL_LUN_STOPPED;
4570 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4571 lun->flags |= CTL_LUN_INOPERABLE;
4573 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4574 lun->flags |= CTL_LUN_PRIMARY_SC;
4576 value = ctl_get_opt(&be_lun->options, "readonly");
4577 if (value != NULL && strcmp(value, "on") == 0)
4578 lun->flags |= CTL_LUN_READONLY;
4580 lun->ctl_softc = ctl_softc;
4581 TAILQ_INIT(&lun->ooa_queue);
4582 TAILQ_INIT(&lun->blocked_queue);
4583 STAILQ_INIT(&lun->error_list);
4584 ctl_tpc_lun_init(lun);
4587 * Initialize the mode and log page index.
4589 ctl_init_page_index(lun);
4590 ctl_init_log_page_index(lun);
4593 * Set the poweron UA for all initiators on this LUN only.
4595 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4596 lun->pending_ua[i] = CTL_UA_POWERON;
4599 * Now, before we insert this lun on the lun list, set the lun
4600 * inventory changed UA for all other luns.
4602 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4603 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4604 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE;
4608 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4610 ctl_softc->ctl_luns[lun_number] = lun;
4612 ctl_softc->num_luns++;
4614 /* Setup statistics gathering */
4615 lun->stats.device_type = be_lun->lun_type;
4616 lun->stats.lun_number = lun_number;
4617 if (lun->stats.device_type == T_DIRECT)
4618 lun->stats.blocksize = be_lun->blocksize;
4620 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4621 for (i = 0;i < CTL_MAX_PORTS;i++)
4622 lun->stats.ports[i].targ_port = i;
4624 mtx_unlock(&ctl_softc->ctl_lock);
4626 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4629 * Run through each registered FETD and bring it online if it isn't
4630 * already. Enable the target ID if it hasn't been enabled, and
4631 * enable this particular LUN.
4633 STAILQ_FOREACH(port, &ctl_softc->port_list, links) {
4636 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number);
4638 printf("ctl_alloc_lun: FETD %s port %d returned error "
4639 "%d for lun_enable on target %ju lun %d\n",
4640 port->port_name, port->targ_port, retval,
4641 (uintmax_t)target_id.id, lun_number);
4643 port->status |= CTL_PORT_STATUS_LUN_ONLINE;
4651 * - LUN has already been marked invalid and any pending I/O has been taken
4655 ctl_free_lun(struct ctl_lun *lun)
4657 struct ctl_softc *softc;
4659 struct ctl_port *port;
4661 struct ctl_lun *nlun;
4664 softc = lun->ctl_softc;
4666 mtx_assert(&softc->ctl_lock, MA_OWNED);
4668 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4670 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4672 softc->ctl_luns[lun->lun] = NULL;
4674 if (!TAILQ_EMPTY(&lun->ooa_queue))
4675 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4680 * XXX KDM this scheme only works for a single target/multiple LUN
4681 * setup. It needs to be revamped for a multiple target scheme.
4683 * XXX KDM this results in port->lun_disable() getting called twice,
4684 * once when ctl_disable_lun() is called, and a second time here.
4685 * We really need to re-think the LUN disable semantics. There
4686 * should probably be several steps/levels to LUN removal:
4691 * Right now we only have a disable method when communicating to
4692 * the front end ports, at least for individual LUNs.
4695 STAILQ_FOREACH(port, &softc->port_list, links) {
4698 retval = port->lun_disable(port->targ_lun_arg, lun->target,
4701 printf("ctl_free_lun: FETD %s port %d returned error "
4702 "%d for lun_disable on target %ju lun %jd\n",
4703 port->port_name, port->targ_port, retval,
4704 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4707 if (STAILQ_FIRST(&softc->lun_list) == NULL) {
4708 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE;
4710 retval = port->targ_disable(port->targ_lun_arg,lun->target);
4712 printf("ctl_free_lun: FETD %s port %d "
4713 "returned error %d for targ_disable on "
4714 "target %ju\n", port->port_name,
4715 port->targ_port, retval,
4716 (uintmax_t)lun->target.id);
4718 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE;
4720 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0)
4724 port->port_offline(port->onoff_arg);
4725 port->status &= ~CTL_PORT_STATUS_ONLINE;
4732 * Tell the backend to free resources, if this LUN has a backend.
4734 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4735 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4737 ctl_tpc_lun_shutdown(lun);
4738 mtx_destroy(&lun->lun_lock);
4739 free(lun->lun_devid, M_CTL);
4740 free(lun->write_buffer, M_CTL);
4741 if (lun->flags & CTL_LUN_MALLOCED)
4744 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4745 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4746 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE;
4754 ctl_create_lun(struct ctl_be_lun *be_lun)
4756 struct ctl_softc *ctl_softc;
4758 ctl_softc = control_softc;
4761 * ctl_alloc_lun() should handle all potential failure cases.
4763 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target);
4767 ctl_add_lun(struct ctl_be_lun *be_lun)
4769 struct ctl_softc *ctl_softc = control_softc;
4771 mtx_lock(&ctl_softc->ctl_lock);
4772 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links);
4773 mtx_unlock(&ctl_softc->ctl_lock);
4774 wakeup(&ctl_softc->pending_lun_queue);
4780 ctl_enable_lun(struct ctl_be_lun *be_lun)
4782 struct ctl_softc *ctl_softc;
4783 struct ctl_port *port, *nport;
4784 struct ctl_lun *lun;
4787 ctl_softc = control_softc;
4789 lun = (struct ctl_lun *)be_lun->ctl_lun;
4791 mtx_lock(&ctl_softc->ctl_lock);
4792 mtx_lock(&lun->lun_lock);
4793 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4795 * eh? Why did we get called if the LUN is already
4798 mtx_unlock(&lun->lun_lock);
4799 mtx_unlock(&ctl_softc->ctl_lock);
4802 lun->flags &= ~CTL_LUN_DISABLED;
4803 mtx_unlock(&lun->lun_lock);
4805 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) {
4806 nport = STAILQ_NEXT(port, links);
4809 * Drop the lock while we call the FETD's enable routine.
4810 * This can lead to a callback into CTL (at least in the
4811 * case of the internal initiator frontend.
4813 mtx_unlock(&ctl_softc->ctl_lock);
4814 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun);
4815 mtx_lock(&ctl_softc->ctl_lock);
4817 printf("%s: FETD %s port %d returned error "
4818 "%d for lun_enable on target %ju lun %jd\n",
4819 __func__, port->port_name, port->targ_port, retval,
4820 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4824 /* NOTE: TODO: why does lun enable affect port status? */
4825 port->status |= CTL_PORT_STATUS_LUN_ONLINE;
4830 mtx_unlock(&ctl_softc->ctl_lock);
4836 ctl_disable_lun(struct ctl_be_lun *be_lun)
4838 struct ctl_softc *ctl_softc;
4839 struct ctl_port *port;
4840 struct ctl_lun *lun;
4843 ctl_softc = control_softc;
4845 lun = (struct ctl_lun *)be_lun->ctl_lun;
4847 mtx_lock(&ctl_softc->ctl_lock);
4848 mtx_lock(&lun->lun_lock);
4849 if (lun->flags & CTL_LUN_DISABLED) {
4850 mtx_unlock(&lun->lun_lock);
4851 mtx_unlock(&ctl_softc->ctl_lock);
4854 lun->flags |= CTL_LUN_DISABLED;
4855 mtx_unlock(&lun->lun_lock);
4857 STAILQ_FOREACH(port, &ctl_softc->port_list, links) {
4858 mtx_unlock(&ctl_softc->ctl_lock);
4860 * Drop the lock before we call the frontend's disable
4861 * routine, to avoid lock order reversals.
4863 * XXX KDM what happens if the frontend list changes while
4864 * we're traversing it? It's unlikely, but should be handled.
4866 retval = port->lun_disable(port->targ_lun_arg, lun->target,
4868 mtx_lock(&ctl_softc->ctl_lock);
4870 printf("ctl_alloc_lun: FETD %s port %d returned error "
4871 "%d for lun_disable on target %ju lun %jd\n",
4872 port->port_name, port->targ_port, retval,
4873 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4877 mtx_unlock(&ctl_softc->ctl_lock);
4883 ctl_start_lun(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_STOPPED;
4894 mtx_unlock(&lun->lun_lock);
4900 ctl_stop_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);
4910 lun->flags |= CTL_LUN_STOPPED;
4911 mtx_unlock(&lun->lun_lock);
4917 ctl_lun_offline(struct ctl_be_lun *be_lun)
4919 struct ctl_softc *ctl_softc;
4920 struct ctl_lun *lun;
4922 ctl_softc = control_softc;
4924 lun = (struct ctl_lun *)be_lun->ctl_lun;
4926 mtx_lock(&lun->lun_lock);
4927 lun->flags |= CTL_LUN_OFFLINE;
4928 mtx_unlock(&lun->lun_lock);
4934 ctl_lun_online(struct ctl_be_lun *be_lun)
4936 struct ctl_softc *ctl_softc;
4937 struct ctl_lun *lun;
4939 ctl_softc = control_softc;
4941 lun = (struct ctl_lun *)be_lun->ctl_lun;
4943 mtx_lock(&lun->lun_lock);
4944 lun->flags &= ~CTL_LUN_OFFLINE;
4945 mtx_unlock(&lun->lun_lock);
4951 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4953 struct ctl_softc *ctl_softc;
4954 struct ctl_lun *lun;
4956 ctl_softc = control_softc;
4958 lun = (struct ctl_lun *)be_lun->ctl_lun;
4960 mtx_lock(&lun->lun_lock);
4963 * The LUN needs to be disabled before it can be marked invalid.
4965 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4966 mtx_unlock(&lun->lun_lock);
4970 * Mark the LUN invalid.
4972 lun->flags |= CTL_LUN_INVALID;
4975 * If there is nothing in the OOA queue, go ahead and free the LUN.
4976 * If we have something in the OOA queue, we'll free it when the
4977 * last I/O completes.
4979 if (TAILQ_EMPTY(&lun->ooa_queue)) {
4980 mtx_unlock(&lun->lun_lock);
4981 mtx_lock(&ctl_softc->ctl_lock);
4983 mtx_unlock(&ctl_softc->ctl_lock);
4985 mtx_unlock(&lun->lun_lock);
4991 ctl_lun_inoperable(struct ctl_be_lun *be_lun)
4993 struct ctl_softc *ctl_softc;
4994 struct ctl_lun *lun;
4996 ctl_softc = control_softc;
4997 lun = (struct ctl_lun *)be_lun->ctl_lun;
4999 mtx_lock(&lun->lun_lock);
5000 lun->flags |= CTL_LUN_INOPERABLE;
5001 mtx_unlock(&lun->lun_lock);
5007 ctl_lun_operable(struct ctl_be_lun *be_lun)
5009 struct ctl_softc *ctl_softc;
5010 struct ctl_lun *lun;
5012 ctl_softc = control_softc;
5013 lun = (struct ctl_lun *)be_lun->ctl_lun;
5015 mtx_lock(&lun->lun_lock);
5016 lun->flags &= ~CTL_LUN_INOPERABLE;
5017 mtx_unlock(&lun->lun_lock);
5023 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
5025 struct ctl_lun *lun;
5026 struct ctl_softc *softc;
5029 softc = control_softc;
5031 lun = (struct ctl_lun *)be_lun->ctl_lun;
5033 mtx_lock(&lun->lun_lock);
5035 for (i = 0; i < CTL_MAX_INITIATORS; i++)
5036 lun->pending_ua[i] |= CTL_UA_CAPACITY_CHANGED;
5038 mtx_unlock(&lun->lun_lock);
5042 * Backend "memory move is complete" callback for requests that never
5043 * make it down to say RAIDCore's configuration code.
5046 ctl_config_move_done(union ctl_io *io)
5050 retval = CTL_RETVAL_COMPLETE;
5053 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
5055 * XXX KDM this shouldn't happen, but what if it does?
5057 if (io->io_hdr.io_type != CTL_IO_SCSI)
5058 panic("I/O type isn't CTL_IO_SCSI!");
5060 if ((io->io_hdr.port_status == 0)
5061 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
5062 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE))
5063 io->io_hdr.status = CTL_SUCCESS;
5064 else if ((io->io_hdr.port_status != 0)
5065 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
5066 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){
5068 * For hardware error sense keys, the sense key
5069 * specific value is defined to be a retry count,
5070 * but we use it to pass back an internal FETD
5071 * error code. XXX KDM Hopefully the FETD is only
5072 * using 16 bits for an error code, since that's
5073 * all the space we have in the sks field.
5075 ctl_set_internal_failure(&io->scsiio,
5078 io->io_hdr.port_status);
5079 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5080 free(io->scsiio.kern_data_ptr, M_CTL);
5085 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
5086 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
5087 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5089 * XXX KDM just assuming a single pointer here, and not a
5090 * S/G list. If we start using S/G lists for config data,
5091 * we'll need to know how to clean them up here as well.
5093 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5094 free(io->scsiio.kern_data_ptr, M_CTL);
5095 /* Hopefully the user has already set the status... */
5099 * XXX KDM now we need to continue data movement. Some
5101 * - call ctl_scsiio() again? We don't do this for data
5102 * writes, because for those at least we know ahead of
5103 * time where the write will go and how long it is. For
5104 * config writes, though, that information is largely
5105 * contained within the write itself, thus we need to
5106 * parse out the data again.
5108 * - Call some other function once the data is in?
5110 if (ctl_debug & CTL_DEBUG_CDB_DATA)
5114 * XXX KDM call ctl_scsiio() again for now, and check flag
5115 * bits to see whether we're allocated or not.
5117 retval = ctl_scsiio(&io->scsiio);
5124 * This gets called by a backend driver when it is done with a
5125 * data_submit method.
5128 ctl_data_submit_done(union ctl_io *io)
5131 * If the IO_CONT flag is set, we need to call the supplied
5132 * function to continue processing the I/O, instead of completing
5135 * If there is an error, though, we don't want to keep processing.
5136 * Instead, just send status back to the initiator.
5138 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5139 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5140 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5141 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5142 io->scsiio.io_cont(io);
5149 * This gets called by a backend driver when it is done with a
5150 * configuration write.
5153 ctl_config_write_done(union ctl_io *io)
5158 * If the IO_CONT flag is set, we need to call the supplied
5159 * function to continue processing the I/O, instead of completing
5162 * If there is an error, though, we don't want to keep processing.
5163 * Instead, just send status back to the initiator.
5165 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5166 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5167 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5168 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5169 io->scsiio.io_cont(io);
5173 * Since a configuration write can be done for commands that actually
5174 * have data allocated, like write buffer, and commands that have
5175 * no data, like start/stop unit, we need to check here.
5177 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5178 buf = io->scsiio.kern_data_ptr;
5187 * SCSI release command.
5190 ctl_scsi_release(struct ctl_scsiio *ctsio)
5192 int length, longid, thirdparty_id, resv_id;
5193 struct ctl_softc *ctl_softc;
5194 struct ctl_lun *lun;
5200 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5202 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5203 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5204 ctl_softc = control_softc;
5206 switch (ctsio->cdb[0]) {
5208 struct scsi_release_10 *cdb;
5210 cdb = (struct scsi_release_10 *)ctsio->cdb;
5212 if (cdb->byte2 & SR10_LONGID)
5215 thirdparty_id = cdb->thirdparty_id;
5217 resv_id = cdb->resv_id;
5218 length = scsi_2btoul(cdb->length);
5225 * XXX KDM right now, we only support LUN reservation. We don't
5226 * support 3rd party reservations, or extent reservations, which
5227 * might actually need the parameter list. If we've gotten this
5228 * far, we've got a LUN reservation. Anything else got kicked out
5229 * above. So, according to SPC, ignore the length.
5233 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5235 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5236 ctsio->kern_data_len = length;
5237 ctsio->kern_total_len = length;
5238 ctsio->kern_data_resid = 0;
5239 ctsio->kern_rel_offset = 0;
5240 ctsio->kern_sg_entries = 0;
5241 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5242 ctsio->be_move_done = ctl_config_move_done;
5243 ctl_datamove((union ctl_io *)ctsio);
5245 return (CTL_RETVAL_COMPLETE);
5249 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5251 mtx_lock(&lun->lun_lock);
5254 * According to SPC, it is not an error for an intiator to attempt
5255 * to release a reservation on a LUN that isn't reserved, or that
5256 * is reserved by another initiator. The reservation can only be
5257 * released, though, by the initiator who made it or by one of
5258 * several reset type events.
5260 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5261 lun->flags &= ~CTL_LUN_RESERVED;
5263 mtx_unlock(&lun->lun_lock);
5265 ctsio->scsi_status = SCSI_STATUS_OK;
5266 ctsio->io_hdr.status = CTL_SUCCESS;
5268 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5269 free(ctsio->kern_data_ptr, M_CTL);
5270 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5273 ctl_done((union ctl_io *)ctsio);
5274 return (CTL_RETVAL_COMPLETE);
5278 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5280 int extent, thirdparty, longid;
5281 int resv_id, length;
5282 uint64_t thirdparty_id;
5283 struct ctl_softc *ctl_softc;
5284 struct ctl_lun *lun;
5294 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5296 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5297 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5298 ctl_softc = control_softc;
5300 switch (ctsio->cdb[0]) {
5302 struct scsi_reserve_10 *cdb;
5304 cdb = (struct scsi_reserve_10 *)ctsio->cdb;
5306 if (cdb->byte2 & SR10_LONGID)
5309 thirdparty_id = cdb->thirdparty_id;
5311 resv_id = cdb->resv_id;
5312 length = scsi_2btoul(cdb->length);
5318 * XXX KDM right now, we only support LUN reservation. We don't
5319 * support 3rd party reservations, or extent reservations, which
5320 * might actually need the parameter list. If we've gotten this
5321 * far, we've got a LUN reservation. Anything else got kicked out
5322 * above. So, according to SPC, ignore the length.
5326 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5328 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5329 ctsio->kern_data_len = length;
5330 ctsio->kern_total_len = length;
5331 ctsio->kern_data_resid = 0;
5332 ctsio->kern_rel_offset = 0;
5333 ctsio->kern_sg_entries = 0;
5334 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5335 ctsio->be_move_done = ctl_config_move_done;
5336 ctl_datamove((union ctl_io *)ctsio);
5338 return (CTL_RETVAL_COMPLETE);
5342 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5344 mtx_lock(&lun->lun_lock);
5345 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5346 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
5347 ctsio->io_hdr.status = CTL_SCSI_ERROR;
5351 lun->flags |= CTL_LUN_RESERVED;
5352 lun->res_idx = residx;
5354 ctsio->scsi_status = SCSI_STATUS_OK;
5355 ctsio->io_hdr.status = CTL_SUCCESS;
5358 mtx_unlock(&lun->lun_lock);
5360 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5361 free(ctsio->kern_data_ptr, M_CTL);
5362 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5365 ctl_done((union ctl_io *)ctsio);
5366 return (CTL_RETVAL_COMPLETE);
5370 ctl_start_stop(struct ctl_scsiio *ctsio)
5372 struct scsi_start_stop_unit *cdb;
5373 struct ctl_lun *lun;
5374 struct ctl_softc *ctl_softc;
5377 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5379 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5380 ctl_softc = control_softc;
5383 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5387 * We don't support the immediate bit on a stop unit. In order to
5388 * do that, we would need to code up a way to know that a stop is
5389 * pending, and hold off any new commands until it completes, one
5390 * way or another. Then we could accept or reject those commands
5391 * depending on its status. We would almost need to do the reverse
5392 * of what we do below for an immediate start -- return the copy of
5393 * the ctl_io to the FETD with status to send to the host (and to
5394 * free the copy!) and then free the original I/O once the stop
5395 * actually completes. That way, the OOA queue mechanism can work
5396 * to block commands that shouldn't proceed. Another alternative
5397 * would be to put the copy in the queue in place of the original,
5398 * and return the original back to the caller. That could be
5401 if ((cdb->byte2 & SSS_IMMED)
5402 && ((cdb->how & SSS_START) == 0)) {
5403 ctl_set_invalid_field(ctsio,
5409 ctl_done((union ctl_io *)ctsio);
5410 return (CTL_RETVAL_COMPLETE);
5413 if ((lun->flags & CTL_LUN_PR_RESERVED)
5414 && ((cdb->how & SSS_START)==0)) {
5417 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5418 if (lun->pr_keys[residx] == 0
5419 || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5421 ctl_set_reservation_conflict(ctsio);
5422 ctl_done((union ctl_io *)ctsio);
5423 return (CTL_RETVAL_COMPLETE);
5428 * If there is no backend on this device, we can't start or stop
5429 * it. In theory we shouldn't get any start/stop commands in the
5430 * first place at this level if the LUN doesn't have a backend.
5431 * That should get stopped by the command decode code.
5433 if (lun->backend == NULL) {
5434 ctl_set_invalid_opcode(ctsio);
5435 ctl_done((union ctl_io *)ctsio);
5436 return (CTL_RETVAL_COMPLETE);
5440 * XXX KDM Copan-specific offline behavior.
5441 * Figure out a reasonable way to port this?
5444 mtx_lock(&lun->lun_lock);
5446 if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
5447 && (lun->flags & CTL_LUN_OFFLINE)) {
5449 * If the LUN is offline, and the on/offline bit isn't set,
5450 * reject the start or stop. Otherwise, let it through.
5452 mtx_unlock(&lun->lun_lock);
5453 ctl_set_lun_not_ready(ctsio);
5454 ctl_done((union ctl_io *)ctsio);
5456 mtx_unlock(&lun->lun_lock);
5457 #endif /* NEEDTOPORT */
5459 * This could be a start or a stop when we're online,
5460 * or a stop/offline or start/online. A start or stop when
5461 * we're offline is covered in the case above.
5464 * In the non-immediate case, we send the request to
5465 * the backend and return status to the user when
5468 * In the immediate case, we allocate a new ctl_io
5469 * to hold a copy of the request, and send that to
5470 * the backend. We then set good status on the
5471 * user's request and return it immediately.
5473 if (cdb->byte2 & SSS_IMMED) {
5474 union ctl_io *new_io;
5476 new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5477 if (new_io == NULL) {
5478 ctl_set_busy(ctsio);
5479 ctl_done((union ctl_io *)ctsio);
5481 ctl_copy_io((union ctl_io *)ctsio,
5483 retval = lun->backend->config_write(new_io);
5484 ctl_set_success(ctsio);
5485 ctl_done((union ctl_io *)ctsio);
5488 retval = lun->backend->config_write(
5489 (union ctl_io *)ctsio);
5498 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5499 * we don't really do anything with the LBA and length fields if the user
5500 * passes them in. Instead we'll just flush out the cache for the entire
5504 ctl_sync_cache(struct ctl_scsiio *ctsio)
5506 struct ctl_lun *lun;
5507 struct ctl_softc *ctl_softc;
5508 uint64_t starting_lba;
5509 uint32_t block_count;
5512 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5514 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5515 ctl_softc = control_softc;
5518 switch (ctsio->cdb[0]) {
5519 case SYNCHRONIZE_CACHE: {
5520 struct scsi_sync_cache *cdb;
5521 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5523 starting_lba = scsi_4btoul(cdb->begin_lba);
5524 block_count = scsi_2btoul(cdb->lb_count);
5527 case SYNCHRONIZE_CACHE_16: {
5528 struct scsi_sync_cache_16 *cdb;
5529 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5531 starting_lba = scsi_8btou64(cdb->begin_lba);
5532 block_count = scsi_4btoul(cdb->lb_count);
5536 ctl_set_invalid_opcode(ctsio);
5537 ctl_done((union ctl_io *)ctsio);
5539 break; /* NOTREACHED */
5543 * We check the LBA and length, but don't do anything with them.
5544 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5545 * get flushed. This check will just help satisfy anyone who wants
5546 * to see an error for an out of range LBA.
5548 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5549 ctl_set_lba_out_of_range(ctsio);
5550 ctl_done((union ctl_io *)ctsio);
5555 * If this LUN has no backend, we can't flush the cache anyway.
5557 if (lun->backend == NULL) {
5558 ctl_set_invalid_opcode(ctsio);
5559 ctl_done((union ctl_io *)ctsio);
5564 * Check to see whether we're configured to send the SYNCHRONIZE
5565 * CACHE command directly to the back end.
5567 mtx_lock(&lun->lun_lock);
5568 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC)
5569 && (++(lun->sync_count) >= lun->sync_interval)) {
5570 lun->sync_count = 0;
5571 mtx_unlock(&lun->lun_lock);
5572 retval = lun->backend->config_write((union ctl_io *)ctsio);
5574 mtx_unlock(&lun->lun_lock);
5575 ctl_set_success(ctsio);
5576 ctl_done((union ctl_io *)ctsio);
5585 ctl_format(struct ctl_scsiio *ctsio)
5587 struct scsi_format *cdb;
5588 struct ctl_lun *lun;
5589 struct ctl_softc *ctl_softc;
5590 int length, defect_list_len;
5592 CTL_DEBUG_PRINT(("ctl_format\n"));
5594 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5595 ctl_softc = control_softc;
5597 cdb = (struct scsi_format *)ctsio->cdb;
5600 if (cdb->byte2 & SF_FMTDATA) {
5601 if (cdb->byte2 & SF_LONGLIST)
5602 length = sizeof(struct scsi_format_header_long);
5604 length = sizeof(struct scsi_format_header_short);
5607 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5609 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5610 ctsio->kern_data_len = length;
5611 ctsio->kern_total_len = length;
5612 ctsio->kern_data_resid = 0;
5613 ctsio->kern_rel_offset = 0;
5614 ctsio->kern_sg_entries = 0;
5615 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5616 ctsio->be_move_done = ctl_config_move_done;
5617 ctl_datamove((union ctl_io *)ctsio);
5619 return (CTL_RETVAL_COMPLETE);
5622 defect_list_len = 0;
5624 if (cdb->byte2 & SF_FMTDATA) {
5625 if (cdb->byte2 & SF_LONGLIST) {
5626 struct scsi_format_header_long *header;
5628 header = (struct scsi_format_header_long *)
5629 ctsio->kern_data_ptr;
5631 defect_list_len = scsi_4btoul(header->defect_list_len);
5632 if (defect_list_len != 0) {
5633 ctl_set_invalid_field(ctsio,
5642 struct scsi_format_header_short *header;
5644 header = (struct scsi_format_header_short *)
5645 ctsio->kern_data_ptr;
5647 defect_list_len = scsi_2btoul(header->defect_list_len);
5648 if (defect_list_len != 0) {
5649 ctl_set_invalid_field(ctsio,
5661 * The format command will clear out the "Medium format corrupted"
5662 * status if set by the configuration code. That status is really
5663 * just a way to notify the host that we have lost the media, and
5664 * get them to issue a command that will basically make them think
5665 * they're blowing away the media.
5667 mtx_lock(&lun->lun_lock);
5668 lun->flags &= ~CTL_LUN_INOPERABLE;
5669 mtx_unlock(&lun->lun_lock);
5671 ctsio->scsi_status = SCSI_STATUS_OK;
5672 ctsio->io_hdr.status = CTL_SUCCESS;
5675 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5676 free(ctsio->kern_data_ptr, M_CTL);
5677 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5680 ctl_done((union ctl_io *)ctsio);
5681 return (CTL_RETVAL_COMPLETE);
5685 ctl_read_buffer(struct ctl_scsiio *ctsio)
5687 struct scsi_read_buffer *cdb;
5688 struct ctl_lun *lun;
5689 int buffer_offset, len;
5690 static uint8_t descr[4];
5691 static uint8_t echo_descr[4] = { 0 };
5693 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5695 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5696 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5698 if (lun->flags & CTL_LUN_PR_RESERVED) {
5702 * XXX KDM need a lock here.
5704 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5705 if ((lun->res_type == SPR_TYPE_EX_AC
5706 && residx != lun->pr_res_idx)
5707 || ((lun->res_type == SPR_TYPE_EX_AC_RO
5708 || lun->res_type == SPR_TYPE_EX_AC_AR)
5709 && lun->pr_keys[residx] == 0)) {
5710 ctl_set_reservation_conflict(ctsio);
5711 ctl_done((union ctl_io *)ctsio);
5712 return (CTL_RETVAL_COMPLETE);
5716 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA &&
5717 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR &&
5718 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) {
5719 ctl_set_invalid_field(ctsio,
5725 ctl_done((union ctl_io *)ctsio);
5726 return (CTL_RETVAL_COMPLETE);
5729 len = scsi_3btoul(cdb->length);
5730 buffer_offset = scsi_3btoul(cdb->offset);
5732 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5733 ctl_set_invalid_field(ctsio,
5739 ctl_done((union ctl_io *)ctsio);
5740 return (CTL_RETVAL_COMPLETE);
5743 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5745 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5746 ctsio->kern_data_ptr = descr;
5747 len = min(len, sizeof(descr));
5748 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5749 ctsio->kern_data_ptr = echo_descr;
5750 len = min(len, sizeof(echo_descr));
5752 if (lun->write_buffer == NULL) {
5753 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5756 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5758 ctsio->kern_data_len = len;
5759 ctsio->kern_total_len = len;
5760 ctsio->kern_data_resid = 0;
5761 ctsio->kern_rel_offset = 0;
5762 ctsio->kern_sg_entries = 0;
5763 ctsio->be_move_done = ctl_config_move_done;
5764 ctl_datamove((union ctl_io *)ctsio);
5766 return (CTL_RETVAL_COMPLETE);
5770 ctl_write_buffer(struct ctl_scsiio *ctsio)
5772 struct scsi_write_buffer *cdb;
5773 struct ctl_lun *lun;
5774 int buffer_offset, len;
5776 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5778 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5779 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5781 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5782 ctl_set_invalid_field(ctsio,
5788 ctl_done((union ctl_io *)ctsio);
5789 return (CTL_RETVAL_COMPLETE);
5792 len = scsi_3btoul(cdb->length);
5793 buffer_offset = scsi_3btoul(cdb->offset);
5795 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5796 ctl_set_invalid_field(ctsio,
5802 ctl_done((union ctl_io *)ctsio);
5803 return (CTL_RETVAL_COMPLETE);
5807 * If we've got a kernel request that hasn't been malloced yet,
5808 * malloc it and tell the caller the data buffer is here.
5810 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5811 if (lun->write_buffer == NULL) {
5812 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5815 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5816 ctsio->kern_data_len = len;
5817 ctsio->kern_total_len = len;
5818 ctsio->kern_data_resid = 0;
5819 ctsio->kern_rel_offset = 0;
5820 ctsio->kern_sg_entries = 0;
5821 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5822 ctsio->be_move_done = ctl_config_move_done;
5823 ctl_datamove((union ctl_io *)ctsio);
5825 return (CTL_RETVAL_COMPLETE);
5828 ctl_done((union ctl_io *)ctsio);
5830 return (CTL_RETVAL_COMPLETE);
5834 ctl_write_same(struct ctl_scsiio *ctsio)
5836 struct ctl_lun *lun;
5837 struct ctl_lba_len_flags *lbalen;
5839 uint32_t num_blocks;
5843 retval = CTL_RETVAL_COMPLETE;
5845 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5847 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5849 switch (ctsio->cdb[0]) {
5850 case WRITE_SAME_10: {
5851 struct scsi_write_same_10 *cdb;
5853 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5855 lba = scsi_4btoul(cdb->addr);
5856 num_blocks = scsi_2btoul(cdb->length);
5860 case WRITE_SAME_16: {
5861 struct scsi_write_same_16 *cdb;
5863 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5865 lba = scsi_8btou64(cdb->addr);
5866 num_blocks = scsi_4btoul(cdb->length);
5872 * We got a command we don't support. This shouldn't
5873 * happen, commands should be filtered out above us.
5875 ctl_set_invalid_opcode(ctsio);
5876 ctl_done((union ctl_io *)ctsio);
5878 return (CTL_RETVAL_COMPLETE);
5879 break; /* NOTREACHED */
5882 /* NDOB and ANCHOR flags can be used only together with UNMAP */
5883 if ((byte2 & SWS_UNMAP) == 0 &&
5884 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) {
5885 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5886 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5887 ctl_done((union ctl_io *)ctsio);
5888 return (CTL_RETVAL_COMPLETE);
5892 * The first check is to make sure we're in bounds, the second
5893 * check is to catch wrap-around problems. If the lba + num blocks
5894 * is less than the lba, then we've wrapped around and the block
5895 * range is invalid anyway.
5897 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5898 || ((lba + num_blocks) < lba)) {
5899 ctl_set_lba_out_of_range(ctsio);
5900 ctl_done((union ctl_io *)ctsio);
5901 return (CTL_RETVAL_COMPLETE);
5904 /* Zero number of blocks means "to the last logical block" */
5905 if (num_blocks == 0) {
5906 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5907 ctl_set_invalid_field(ctsio,
5913 ctl_done((union ctl_io *)ctsio);
5914 return (CTL_RETVAL_COMPLETE);
5916 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5919 len = lun->be_lun->blocksize;
5922 * If we've got a kernel request that hasn't been malloced yet,
5923 * malloc it and tell the caller the data buffer is here.
5925 if ((byte2 & SWS_NDOB) == 0 &&
5926 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5927 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5928 ctsio->kern_data_len = len;
5929 ctsio->kern_total_len = len;
5930 ctsio->kern_data_resid = 0;
5931 ctsio->kern_rel_offset = 0;
5932 ctsio->kern_sg_entries = 0;
5933 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5934 ctsio->be_move_done = ctl_config_move_done;
5935 ctl_datamove((union ctl_io *)ctsio);
5937 return (CTL_RETVAL_COMPLETE);
5940 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5942 lbalen->len = num_blocks;
5943 lbalen->flags = byte2;
5944 retval = lun->backend->config_write((union ctl_io *)ctsio);
5950 ctl_unmap(struct ctl_scsiio *ctsio)
5952 struct ctl_lun *lun;
5953 struct scsi_unmap *cdb;
5954 struct ctl_ptr_len_flags *ptrlen;
5955 struct scsi_unmap_header *hdr;
5956 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5958 uint32_t num_blocks;
5962 retval = CTL_RETVAL_COMPLETE;
5964 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5966 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5967 cdb = (struct scsi_unmap *)ctsio->cdb;
5969 len = scsi_2btoul(cdb->length);
5973 * If we've got a kernel request that hasn't been malloced yet,
5974 * malloc it and tell the caller the data buffer is here.
5976 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5977 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5978 ctsio->kern_data_len = len;
5979 ctsio->kern_total_len = len;
5980 ctsio->kern_data_resid = 0;
5981 ctsio->kern_rel_offset = 0;
5982 ctsio->kern_sg_entries = 0;
5983 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5984 ctsio->be_move_done = ctl_config_move_done;
5985 ctl_datamove((union ctl_io *)ctsio);
5987 return (CTL_RETVAL_COMPLETE);
5990 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5991 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5992 if (len < sizeof (*hdr) ||
5993 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5994 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5995 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5996 ctl_set_invalid_field(ctsio,
6002 ctl_done((union ctl_io *)ctsio);
6003 return (CTL_RETVAL_COMPLETE);
6005 len = scsi_2btoul(hdr->desc_length);
6006 buf = (struct scsi_unmap_desc *)(hdr + 1);
6007 end = buf + len / sizeof(*buf);
6010 for (range = buf; range < end; range++) {
6011 lba = scsi_8btou64(range->lba);
6012 num_blocks = scsi_4btoul(range->length);
6013 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
6014 || ((lba + num_blocks) < lba)) {
6015 ctl_set_lba_out_of_range(ctsio);
6016 ctl_done((union ctl_io *)ctsio);
6017 return (CTL_RETVAL_COMPLETE);
6019 if (num_blocks != 0)
6024 * Block backend can not handle zero last range.
6025 * Filter it out and return if there is nothing left.
6027 len = (uint8_t *)endnz - (uint8_t *)buf;
6029 ctl_set_success(ctsio);
6030 ctl_done((union ctl_io *)ctsio);
6031 return (CTL_RETVAL_COMPLETE);
6034 mtx_lock(&lun->lun_lock);
6035 ptrlen = (struct ctl_ptr_len_flags *)
6036 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
6037 ptrlen->ptr = (void *)buf;
6039 ptrlen->flags = byte2;
6040 ctl_check_blocked(lun);
6041 mtx_unlock(&lun->lun_lock);
6043 retval = lun->backend->config_write((union ctl_io *)ctsio);
6048 * Note that this function currently doesn't actually do anything inside
6049 * CTL to enforce things if the DQue bit is turned on.
6051 * Also note that this function can't be used in the default case, because
6052 * the DQue bit isn't set in the changeable mask for the control mode page
6053 * anyway. This is just here as an example for how to implement a page
6054 * handler, and a placeholder in case we want to allow the user to turn
6055 * tagged queueing on and off.
6057 * The D_SENSE bit handling is functional, however, and will turn
6058 * descriptor sense on and off for a given LUN.
6061 ctl_control_page_handler(struct ctl_scsiio *ctsio,
6062 struct ctl_page_index *page_index, uint8_t *page_ptr)
6064 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
6065 struct ctl_lun *lun;
6066 struct ctl_softc *softc;
6070 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6071 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6074 user_cp = (struct scsi_control_page *)page_ptr;
6075 current_cp = (struct scsi_control_page *)
6076 (page_index->page_data + (page_index->page_len *
6078 saved_cp = (struct scsi_control_page *)
6079 (page_index->page_data + (page_index->page_len *
6082 softc = control_softc;
6084 mtx_lock(&lun->lun_lock);
6085 if (((current_cp->rlec & SCP_DSENSE) == 0)
6086 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
6088 * Descriptor sense is currently turned off and the user
6089 * wants to turn it on.
6091 current_cp->rlec |= SCP_DSENSE;
6092 saved_cp->rlec |= SCP_DSENSE;
6093 lun->flags |= CTL_LUN_SENSE_DESC;
6095 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
6096 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
6098 * Descriptor sense is currently turned on, and the user
6099 * wants to turn it off.
6101 current_cp->rlec &= ~SCP_DSENSE;
6102 saved_cp->rlec &= ~SCP_DSENSE;
6103 lun->flags &= ~CTL_LUN_SENSE_DESC;
6106 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) !=
6107 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) {
6108 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
6109 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
6110 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
6111 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
6114 if ((current_cp->eca_and_aen & SCP_SWP) !=
6115 (user_cp->eca_and_aen & SCP_SWP)) {
6116 current_cp->eca_and_aen &= ~SCP_SWP;
6117 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
6118 saved_cp->eca_and_aen &= ~SCP_SWP;
6119 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
6125 * Let other initiators know that the mode
6126 * parameters for this LUN have changed.
6128 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
6132 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE;
6135 mtx_unlock(&lun->lun_lock);
6141 ctl_caching_sp_handler(struct ctl_scsiio *ctsio,
6142 struct ctl_page_index *page_index, uint8_t *page_ptr)
6144 struct scsi_caching_page *current_cp, *saved_cp, *user_cp;
6145 struct ctl_lun *lun;
6149 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6150 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6153 user_cp = (struct scsi_caching_page *)page_ptr;
6154 current_cp = (struct scsi_caching_page *)
6155 (page_index->page_data + (page_index->page_len *
6157 saved_cp = (struct scsi_caching_page *)
6158 (page_index->page_data + (page_index->page_len *
6161 mtx_lock(&lun->lun_lock);
6162 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) !=
6163 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) {
6164 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
6165 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
6166 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
6167 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
6173 * Let other initiators know that the mode
6174 * parameters for this LUN have changed.
6176 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
6180 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE;
6183 mtx_unlock(&lun->lun_lock);
6189 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
6190 struct ctl_page_index *page_index,
6196 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
6201 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
6202 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
6203 printf("page data:");
6205 printf(" %.2x",page_ptr[i]);
6211 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
6212 struct ctl_page_index *page_index,
6215 struct copan_debugconf_subpage *page;
6217 page = (struct copan_debugconf_subpage *)page_index->page_data +
6218 (page_index->page_len * pc);
6221 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6222 case SMS_PAGE_CTRL_DEFAULT >> 6:
6223 case SMS_PAGE_CTRL_SAVED >> 6:
6225 * We don't update the changable or default bits for this page.
6228 case SMS_PAGE_CTRL_CURRENT >> 6:
6229 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
6230 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
6234 EPRINT(0, "Invalid PC %d!!", pc);
6235 #endif /* NEEDTOPORT */
6243 ctl_do_mode_select(union ctl_io *io)
6245 struct scsi_mode_page_header *page_header;
6246 struct ctl_page_index *page_index;
6247 struct ctl_scsiio *ctsio;
6248 int control_dev, page_len;
6249 int page_len_offset, page_len_size;
6250 union ctl_modepage_info *modepage_info;
6251 struct ctl_lun *lun;
6252 int *len_left, *len_used;
6255 ctsio = &io->scsiio;
6258 retval = CTL_RETVAL_COMPLETE;
6260 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6262 if (lun->be_lun->lun_type != T_DIRECT)
6267 modepage_info = (union ctl_modepage_info *)
6268 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6269 len_left = &modepage_info->header.len_left;
6270 len_used = &modepage_info->header.len_used;
6274 page_header = (struct scsi_mode_page_header *)
6275 (ctsio->kern_data_ptr + *len_used);
6277 if (*len_left == 0) {
6278 free(ctsio->kern_data_ptr, M_CTL);
6279 ctl_set_success(ctsio);
6280 ctl_done((union ctl_io *)ctsio);
6281 return (CTL_RETVAL_COMPLETE);
6282 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6284 free(ctsio->kern_data_ptr, M_CTL);
6285 ctl_set_param_len_error(ctsio);
6286 ctl_done((union ctl_io *)ctsio);
6287 return (CTL_RETVAL_COMPLETE);
6289 } else if ((page_header->page_code & SMPH_SPF)
6290 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6292 free(ctsio->kern_data_ptr, M_CTL);
6293 ctl_set_param_len_error(ctsio);
6294 ctl_done((union ctl_io *)ctsio);
6295 return (CTL_RETVAL_COMPLETE);
6300 * XXX KDM should we do something with the block descriptor?
6302 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6304 if ((control_dev != 0)
6305 && (lun->mode_pages.index[i].page_flags &
6306 CTL_PAGE_FLAG_DISK_ONLY))
6309 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
6310 (page_header->page_code & SMPH_PC_MASK))
6314 * If neither page has a subpage code, then we've got a
6317 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
6318 && ((page_header->page_code & SMPH_SPF) == 0)) {
6319 page_index = &lun->mode_pages.index[i];
6320 page_len = page_header->page_length;
6325 * If both pages have subpages, then the subpage numbers
6328 if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
6329 && (page_header->page_code & SMPH_SPF)) {
6330 struct scsi_mode_page_header_sp *sph;
6332 sph = (struct scsi_mode_page_header_sp *)page_header;
6334 if (lun->mode_pages.index[i].subpage ==
6336 page_index = &lun->mode_pages.index[i];
6337 page_len = scsi_2btoul(sph->page_length);
6344 * If we couldn't find the page, or if we don't have a mode select
6345 * handler for it, send back an error to the user.
6347 if ((page_index == NULL)
6348 || (page_index->select_handler == NULL)) {
6349 ctl_set_invalid_field(ctsio,
6352 /*field*/ *len_used,
6355 free(ctsio->kern_data_ptr, M_CTL);
6356 ctl_done((union ctl_io *)ctsio);
6357 return (CTL_RETVAL_COMPLETE);
6360 if (page_index->page_code & SMPH_SPF) {
6361 page_len_offset = 2;
6365 page_len_offset = 1;
6369 * If the length the initiator gives us isn't the one we specify in
6370 * the mode page header, or if they didn't specify enough data in
6371 * the CDB to avoid truncating this page, kick out the request.
6373 if ((page_len != (page_index->page_len - page_len_offset -
6375 || (*len_left < page_index->page_len)) {
6378 ctl_set_invalid_field(ctsio,
6381 /*field*/ *len_used + page_len_offset,
6384 free(ctsio->kern_data_ptr, M_CTL);
6385 ctl_done((union ctl_io *)ctsio);
6386 return (CTL_RETVAL_COMPLETE);
6390 * Run through the mode page, checking to make sure that the bits
6391 * the user changed are actually legal for him to change.
6393 for (i = 0; i < page_index->page_len; i++) {
6394 uint8_t *user_byte, *change_mask, *current_byte;
6398 user_byte = (uint8_t *)page_header + i;
6399 change_mask = page_index->page_data +
6400 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6401 current_byte = page_index->page_data +
6402 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6405 * Check to see whether the user set any bits in this byte
6406 * that he is not allowed to set.
6408 if ((*user_byte & ~(*change_mask)) ==
6409 (*current_byte & ~(*change_mask)))
6413 * Go through bit by bit to determine which one is illegal.
6416 for (j = 7; j >= 0; j--) {
6417 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6418 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6423 ctl_set_invalid_field(ctsio,
6426 /*field*/ *len_used + i,
6429 free(ctsio->kern_data_ptr, M_CTL);
6430 ctl_done((union ctl_io *)ctsio);
6431 return (CTL_RETVAL_COMPLETE);
6435 * Decrement these before we call the page handler, since we may
6436 * end up getting called back one way or another before the handler
6437 * returns to this context.
6439 *len_left -= page_index->page_len;
6440 *len_used += page_index->page_len;
6442 retval = page_index->select_handler(ctsio, page_index,
6443 (uint8_t *)page_header);
6446 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6447 * wait until this queued command completes to finish processing
6448 * the mode page. If it returns anything other than
6449 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6450 * already set the sense information, freed the data pointer, and
6451 * completed the io for us.
6453 if (retval != CTL_RETVAL_COMPLETE)
6454 goto bailout_no_done;
6457 * If the initiator sent us more than one page, parse the next one.
6462 ctl_set_success(ctsio);
6463 free(ctsio->kern_data_ptr, M_CTL);
6464 ctl_done((union ctl_io *)ctsio);
6468 return (CTL_RETVAL_COMPLETE);
6473 ctl_mode_select(struct ctl_scsiio *ctsio)
6475 int param_len, pf, sp;
6476 int header_size, bd_len;
6477 int len_left, len_used;
6478 struct ctl_page_index *page_index;
6479 struct ctl_lun *lun;
6480 int control_dev, page_len;
6481 union ctl_modepage_info *modepage_info;
6493 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6495 if (lun->be_lun->lun_type != T_DIRECT)
6500 switch (ctsio->cdb[0]) {
6501 case MODE_SELECT_6: {
6502 struct scsi_mode_select_6 *cdb;
6504 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6506 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6507 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6509 param_len = cdb->length;
6510 header_size = sizeof(struct scsi_mode_header_6);
6513 case MODE_SELECT_10: {
6514 struct scsi_mode_select_10 *cdb;
6516 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6518 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6519 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6521 param_len = scsi_2btoul(cdb->length);
6522 header_size = sizeof(struct scsi_mode_header_10);
6526 ctl_set_invalid_opcode(ctsio);
6527 ctl_done((union ctl_io *)ctsio);
6528 return (CTL_RETVAL_COMPLETE);
6529 break; /* NOTREACHED */
6534 * "A parameter list length of zero indicates that the Data-Out Buffer
6535 * shall be empty. This condition shall not be considered as an error."
6537 if (param_len == 0) {
6538 ctl_set_success(ctsio);
6539 ctl_done((union ctl_io *)ctsio);
6540 return (CTL_RETVAL_COMPLETE);
6544 * Since we'll hit this the first time through, prior to
6545 * allocation, we don't need to free a data buffer here.
6547 if (param_len < header_size) {
6548 ctl_set_param_len_error(ctsio);
6549 ctl_done((union ctl_io *)ctsio);
6550 return (CTL_RETVAL_COMPLETE);
6554 * Allocate the data buffer and grab the user's data. In theory,
6555 * we shouldn't have to sanity check the parameter list length here
6556 * because the maximum size is 64K. We should be able to malloc
6557 * that much without too many problems.
6559 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6560 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6561 ctsio->kern_data_len = param_len;
6562 ctsio->kern_total_len = param_len;
6563 ctsio->kern_data_resid = 0;
6564 ctsio->kern_rel_offset = 0;
6565 ctsio->kern_sg_entries = 0;
6566 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6567 ctsio->be_move_done = ctl_config_move_done;
6568 ctl_datamove((union ctl_io *)ctsio);
6570 return (CTL_RETVAL_COMPLETE);
6573 switch (ctsio->cdb[0]) {
6574 case MODE_SELECT_6: {
6575 struct scsi_mode_header_6 *mh6;
6577 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6578 bd_len = mh6->blk_desc_len;
6581 case MODE_SELECT_10: {
6582 struct scsi_mode_header_10 *mh10;
6584 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6585 bd_len = scsi_2btoul(mh10->blk_desc_len);
6589 panic("Invalid CDB type %#x", ctsio->cdb[0]);
6593 if (param_len < (header_size + bd_len)) {
6594 free(ctsio->kern_data_ptr, M_CTL);
6595 ctl_set_param_len_error(ctsio);
6596 ctl_done((union ctl_io *)ctsio);
6597 return (CTL_RETVAL_COMPLETE);
6601 * Set the IO_CONT flag, so that if this I/O gets passed to
6602 * ctl_config_write_done(), it'll get passed back to
6603 * ctl_do_mode_select() for further processing, or completion if
6606 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6607 ctsio->io_cont = ctl_do_mode_select;
6609 modepage_info = (union ctl_modepage_info *)
6610 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6612 memset(modepage_info, 0, sizeof(*modepage_info));
6614 len_left = param_len - header_size - bd_len;
6615 len_used = header_size + bd_len;
6617 modepage_info->header.len_left = len_left;
6618 modepage_info->header.len_used = len_used;
6620 return (ctl_do_mode_select((union ctl_io *)ctsio));
6624 ctl_mode_sense(struct ctl_scsiio *ctsio)
6626 struct ctl_lun *lun;
6627 int pc, page_code, dbd, llba, subpage;
6628 int alloc_len, page_len, header_len, total_len;
6629 struct scsi_mode_block_descr *block_desc;
6630 struct ctl_page_index *page_index;
6638 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6640 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6642 if (lun->be_lun->lun_type != T_DIRECT)
6647 if (lun->flags & CTL_LUN_PR_RESERVED) {
6651 * XXX KDM need a lock here.
6653 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
6654 if ((lun->res_type == SPR_TYPE_EX_AC
6655 && residx != lun->pr_res_idx)
6656 || ((lun->res_type == SPR_TYPE_EX_AC_RO
6657 || lun->res_type == SPR_TYPE_EX_AC_AR)
6658 && lun->pr_keys[residx] == 0)) {
6659 ctl_set_reservation_conflict(ctsio);
6660 ctl_done((union ctl_io *)ctsio);
6661 return (CTL_RETVAL_COMPLETE);
6665 switch (ctsio->cdb[0]) {
6666 case MODE_SENSE_6: {
6667 struct scsi_mode_sense_6 *cdb;
6669 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6671 header_len = sizeof(struct scsi_mode_hdr_6);
6672 if (cdb->byte2 & SMS_DBD)
6675 header_len += sizeof(struct scsi_mode_block_descr);
6677 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6678 page_code = cdb->page & SMS_PAGE_CODE;
6679 subpage = cdb->subpage;
6680 alloc_len = cdb->length;
6683 case MODE_SENSE_10: {
6684 struct scsi_mode_sense_10 *cdb;
6686 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6688 header_len = sizeof(struct scsi_mode_hdr_10);
6690 if (cdb->byte2 & SMS_DBD)
6693 header_len += sizeof(struct scsi_mode_block_descr);
6694 if (cdb->byte2 & SMS10_LLBAA)
6696 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6697 page_code = cdb->page & SMS_PAGE_CODE;
6698 subpage = cdb->subpage;
6699 alloc_len = scsi_2btoul(cdb->length);
6703 ctl_set_invalid_opcode(ctsio);
6704 ctl_done((union ctl_io *)ctsio);
6705 return (CTL_RETVAL_COMPLETE);
6706 break; /* NOTREACHED */
6710 * We have to make a first pass through to calculate the size of
6711 * the pages that match the user's query. Then we allocate enough
6712 * memory to hold it, and actually copy the data into the buffer.
6714 switch (page_code) {
6715 case SMS_ALL_PAGES_PAGE: {
6721 * At the moment, values other than 0 and 0xff here are
6722 * reserved according to SPC-3.
6724 if ((subpage != SMS_SUBPAGE_PAGE_0)
6725 && (subpage != SMS_SUBPAGE_ALL)) {
6726 ctl_set_invalid_field(ctsio,
6732 ctl_done((union ctl_io *)ctsio);
6733 return (CTL_RETVAL_COMPLETE);
6736 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6737 if ((control_dev != 0)
6738 && (lun->mode_pages.index[i].page_flags &
6739 CTL_PAGE_FLAG_DISK_ONLY))
6743 * We don't use this subpage if the user didn't
6744 * request all subpages.
6746 if ((lun->mode_pages.index[i].subpage != 0)
6747 && (subpage == SMS_SUBPAGE_PAGE_0))
6751 printf("found page %#x len %d\n",
6752 lun->mode_pages.index[i].page_code &
6754 lun->mode_pages.index[i].page_len);
6756 page_len += lun->mode_pages.index[i].page_len;
6765 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6766 /* Look for the right page code */
6767 if ((lun->mode_pages.index[i].page_code &
6768 SMPH_PC_MASK) != page_code)
6771 /* Look for the right subpage or the subpage wildcard*/
6772 if ((lun->mode_pages.index[i].subpage != subpage)
6773 && (subpage != SMS_SUBPAGE_ALL))
6776 /* Make sure the page is supported for this dev type */
6777 if ((control_dev != 0)
6778 && (lun->mode_pages.index[i].page_flags &
6779 CTL_PAGE_FLAG_DISK_ONLY))
6783 printf("found page %#x len %d\n",
6784 lun->mode_pages.index[i].page_code &
6786 lun->mode_pages.index[i].page_len);
6789 page_len += lun->mode_pages.index[i].page_len;
6792 if (page_len == 0) {
6793 ctl_set_invalid_field(ctsio,
6799 ctl_done((union ctl_io *)ctsio);
6800 return (CTL_RETVAL_COMPLETE);
6806 total_len = header_len + page_len;
6808 printf("header_len = %d, page_len = %d, total_len = %d\n",
6809 header_len, page_len, total_len);
6812 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6813 ctsio->kern_sg_entries = 0;
6814 ctsio->kern_data_resid = 0;
6815 ctsio->kern_rel_offset = 0;
6816 if (total_len < alloc_len) {
6817 ctsio->residual = alloc_len - total_len;
6818 ctsio->kern_data_len = total_len;
6819 ctsio->kern_total_len = total_len;
6821 ctsio->residual = 0;
6822 ctsio->kern_data_len = alloc_len;
6823 ctsio->kern_total_len = alloc_len;
6826 switch (ctsio->cdb[0]) {
6827 case MODE_SENSE_6: {
6828 struct scsi_mode_hdr_6 *header;
6830 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6832 header->datalen = ctl_min(total_len - 1, 254);
6833 if (control_dev == 0) {
6834 header->dev_specific = 0x10; /* DPOFUA */
6835 if ((lun->flags & CTL_LUN_READONLY) ||
6836 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6837 .eca_and_aen & SCP_SWP) != 0)
6838 header->dev_specific |= 0x80; /* WP */
6841 header->block_descr_len = 0;
6843 header->block_descr_len =
6844 sizeof(struct scsi_mode_block_descr);
6845 block_desc = (struct scsi_mode_block_descr *)&header[1];
6848 case MODE_SENSE_10: {
6849 struct scsi_mode_hdr_10 *header;
6852 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6854 datalen = ctl_min(total_len - 2, 65533);
6855 scsi_ulto2b(datalen, header->datalen);
6856 if (control_dev == 0) {
6857 header->dev_specific = 0x10; /* DPOFUA */
6858 if ((lun->flags & CTL_LUN_READONLY) ||
6859 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6860 .eca_and_aen & SCP_SWP) != 0)
6861 header->dev_specific |= 0x80; /* WP */
6864 scsi_ulto2b(0, header->block_descr_len);
6866 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6867 header->block_descr_len);
6868 block_desc = (struct scsi_mode_block_descr *)&header[1];
6872 panic("invalid CDB type %#x", ctsio->cdb[0]);
6873 break; /* NOTREACHED */
6877 * If we've got a disk, use its blocksize in the block
6878 * descriptor. Otherwise, just set it to 0.
6881 if (control_dev == 0)
6882 scsi_ulto3b(lun->be_lun->blocksize,
6883 block_desc->block_len);
6885 scsi_ulto3b(0, block_desc->block_len);
6888 switch (page_code) {
6889 case SMS_ALL_PAGES_PAGE: {
6892 data_used = header_len;
6893 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6894 struct ctl_page_index *page_index;
6896 page_index = &lun->mode_pages.index[i];
6898 if ((control_dev != 0)
6899 && (page_index->page_flags &
6900 CTL_PAGE_FLAG_DISK_ONLY))
6904 * We don't use this subpage if the user didn't
6905 * request all subpages. We already checked (above)
6906 * to make sure the user only specified a subpage
6907 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6909 if ((page_index->subpage != 0)
6910 && (subpage == SMS_SUBPAGE_PAGE_0))
6914 * Call the handler, if it exists, to update the
6915 * page to the latest values.
6917 if (page_index->sense_handler != NULL)
6918 page_index->sense_handler(ctsio, page_index,pc);
6920 memcpy(ctsio->kern_data_ptr + data_used,
6921 page_index->page_data +
6922 (page_index->page_len * pc),
6923 page_index->page_len);
6924 data_used += page_index->page_len;
6931 data_used = header_len;
6933 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6934 struct ctl_page_index *page_index;
6936 page_index = &lun->mode_pages.index[i];
6938 /* Look for the right page code */
6939 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6942 /* Look for the right subpage or the subpage wildcard*/
6943 if ((page_index->subpage != subpage)
6944 && (subpage != SMS_SUBPAGE_ALL))
6947 /* Make sure the page is supported for this dev type */
6948 if ((control_dev != 0)
6949 && (page_index->page_flags &
6950 CTL_PAGE_FLAG_DISK_ONLY))
6954 * Call the handler, if it exists, to update the
6955 * page to the latest values.
6957 if (page_index->sense_handler != NULL)
6958 page_index->sense_handler(ctsio, page_index,pc);
6960 memcpy(ctsio->kern_data_ptr + data_used,
6961 page_index->page_data +
6962 (page_index->page_len * pc),
6963 page_index->page_len);
6964 data_used += page_index->page_len;
6970 ctsio->scsi_status = SCSI_STATUS_OK;
6972 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6973 ctsio->be_move_done = ctl_config_move_done;
6974 ctl_datamove((union ctl_io *)ctsio);
6976 return (CTL_RETVAL_COMPLETE);
6980 ctl_log_sense(struct ctl_scsiio *ctsio)
6982 struct ctl_lun *lun;
6983 int i, pc, page_code, subpage;
6984 int alloc_len, total_len;
6985 struct ctl_page_index *page_index;
6986 struct scsi_log_sense *cdb;
6987 struct scsi_log_header *header;
6989 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6991 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6992 cdb = (struct scsi_log_sense *)ctsio->cdb;
6993 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6994 page_code = cdb->page & SLS_PAGE_CODE;
6995 subpage = cdb->subpage;
6996 alloc_len = scsi_2btoul(cdb->length);
6999 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
7000 page_index = &lun->log_pages.index[i];
7002 /* Look for the right page code */
7003 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
7006 /* Look for the right subpage or the subpage wildcard*/
7007 if (page_index->subpage != subpage)
7012 if (i >= CTL_NUM_LOG_PAGES) {
7013 ctl_set_invalid_field(ctsio,
7019 ctl_done((union ctl_io *)ctsio);
7020 return (CTL_RETVAL_COMPLETE);
7023 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
7025 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7026 ctsio->kern_sg_entries = 0;
7027 ctsio->kern_data_resid = 0;
7028 ctsio->kern_rel_offset = 0;
7029 if (total_len < alloc_len) {
7030 ctsio->residual = alloc_len - total_len;
7031 ctsio->kern_data_len = total_len;
7032 ctsio->kern_total_len = total_len;
7034 ctsio->residual = 0;
7035 ctsio->kern_data_len = alloc_len;
7036 ctsio->kern_total_len = alloc_len;
7039 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
7040 header->page = page_index->page_code;
7041 if (page_index->subpage) {
7042 header->page |= SL_SPF;
7043 header->subpage = page_index->subpage;
7045 scsi_ulto2b(page_index->page_len, header->datalen);
7048 * Call the handler, if it exists, to update the
7049 * page to the latest values.
7051 if (page_index->sense_handler != NULL)
7052 page_index->sense_handler(ctsio, page_index, pc);
7054 memcpy(header + 1, page_index->page_data, page_index->page_len);
7056 ctsio->scsi_status = SCSI_STATUS_OK;
7057 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7058 ctsio->be_move_done = ctl_config_move_done;
7059 ctl_datamove((union ctl_io *)ctsio);
7061 return (CTL_RETVAL_COMPLETE);
7065 ctl_read_capacity(struct ctl_scsiio *ctsio)
7067 struct scsi_read_capacity *cdb;
7068 struct scsi_read_capacity_data *data;
7069 struct ctl_lun *lun;
7072 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
7074 cdb = (struct scsi_read_capacity *)ctsio->cdb;
7076 lba = scsi_4btoul(cdb->addr);
7077 if (((cdb->pmi & SRC_PMI) == 0)
7079 ctl_set_invalid_field(/*ctsio*/ ctsio,
7085 ctl_done((union ctl_io *)ctsio);
7086 return (CTL_RETVAL_COMPLETE);
7089 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7091 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7092 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
7093 ctsio->residual = 0;
7094 ctsio->kern_data_len = sizeof(*data);
7095 ctsio->kern_total_len = sizeof(*data);
7096 ctsio->kern_data_resid = 0;
7097 ctsio->kern_rel_offset = 0;
7098 ctsio->kern_sg_entries = 0;
7101 * If the maximum LBA is greater than 0xfffffffe, the user must
7102 * issue a SERVICE ACTION IN (16) command, with the read capacity
7103 * serivce action set.
7105 if (lun->be_lun->maxlba > 0xfffffffe)
7106 scsi_ulto4b(0xffffffff, data->addr);
7108 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
7111 * XXX KDM this may not be 512 bytes...
7113 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7115 ctsio->scsi_status = SCSI_STATUS_OK;
7117 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7118 ctsio->be_move_done = ctl_config_move_done;
7119 ctl_datamove((union ctl_io *)ctsio);
7121 return (CTL_RETVAL_COMPLETE);
7125 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
7127 struct scsi_read_capacity_16 *cdb;
7128 struct scsi_read_capacity_data_long *data;
7129 struct ctl_lun *lun;
7133 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
7135 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
7137 alloc_len = scsi_4btoul(cdb->alloc_len);
7138 lba = scsi_8btou64(cdb->addr);
7140 if ((cdb->reladr & SRC16_PMI)
7142 ctl_set_invalid_field(/*ctsio*/ ctsio,
7148 ctl_done((union ctl_io *)ctsio);
7149 return (CTL_RETVAL_COMPLETE);
7152 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7154 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7155 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
7157 if (sizeof(*data) < alloc_len) {
7158 ctsio->residual = alloc_len - sizeof(*data);
7159 ctsio->kern_data_len = sizeof(*data);
7160 ctsio->kern_total_len = sizeof(*data);
7162 ctsio->residual = 0;
7163 ctsio->kern_data_len = alloc_len;
7164 ctsio->kern_total_len = alloc_len;
7166 ctsio->kern_data_resid = 0;
7167 ctsio->kern_rel_offset = 0;
7168 ctsio->kern_sg_entries = 0;
7170 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7171 /* XXX KDM this may not be 512 bytes... */
7172 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7173 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7174 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7175 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7176 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
7178 ctsio->scsi_status = SCSI_STATUS_OK;
7180 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7181 ctsio->be_move_done = ctl_config_move_done;
7182 ctl_datamove((union ctl_io *)ctsio);
7184 return (CTL_RETVAL_COMPLETE);
7188 ctl_read_defect(struct ctl_scsiio *ctsio)
7190 struct scsi_read_defect_data_10 *ccb10;
7191 struct scsi_read_defect_data_12 *ccb12;
7192 struct scsi_read_defect_data_hdr_10 *data10;
7193 struct scsi_read_defect_data_hdr_12 *data12;
7194 struct ctl_lun *lun;
7195 uint32_t alloc_len, data_len;
7198 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7200 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7201 if (lun->flags & CTL_LUN_PR_RESERVED) {
7205 * XXX KDM need a lock here.
7207 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
7208 if ((lun->res_type == SPR_TYPE_EX_AC
7209 && residx != lun->pr_res_idx)
7210 || ((lun->res_type == SPR_TYPE_EX_AC_RO
7211 || lun->res_type == SPR_TYPE_EX_AC_AR)
7212 && lun->pr_keys[residx] == 0)) {
7213 ctl_set_reservation_conflict(ctsio);
7214 ctl_done((union ctl_io *)ctsio);
7215 return (CTL_RETVAL_COMPLETE);
7219 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7220 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7221 format = ccb10->format;
7222 alloc_len = scsi_2btoul(ccb10->alloc_length);
7223 data_len = sizeof(*data10);
7225 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7226 format = ccb12->format;
7227 alloc_len = scsi_4btoul(ccb12->alloc_length);
7228 data_len = sizeof(*data12);
7230 if (alloc_len == 0) {
7231 ctl_set_success(ctsio);
7232 ctl_done((union ctl_io *)ctsio);
7233 return (CTL_RETVAL_COMPLETE);
7236 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7237 if (data_len < alloc_len) {
7238 ctsio->residual = alloc_len - data_len;
7239 ctsio->kern_data_len = data_len;
7240 ctsio->kern_total_len = data_len;
7242 ctsio->residual = 0;
7243 ctsio->kern_data_len = alloc_len;
7244 ctsio->kern_total_len = alloc_len;
7246 ctsio->kern_data_resid = 0;
7247 ctsio->kern_rel_offset = 0;
7248 ctsio->kern_sg_entries = 0;
7250 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7251 data10 = (struct scsi_read_defect_data_hdr_10 *)
7252 ctsio->kern_data_ptr;
7253 data10->format = format;
7254 scsi_ulto2b(0, data10->length);
7256 data12 = (struct scsi_read_defect_data_hdr_12 *)
7257 ctsio->kern_data_ptr;
7258 data12->format = format;
7259 scsi_ulto2b(0, data12->generation);
7260 scsi_ulto4b(0, data12->length);
7263 ctsio->scsi_status = SCSI_STATUS_OK;
7264 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7265 ctsio->be_move_done = ctl_config_move_done;
7266 ctl_datamove((union ctl_io *)ctsio);
7267 return (CTL_RETVAL_COMPLETE);
7271 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7273 struct scsi_maintenance_in *cdb;
7275 int alloc_len, ext, total_len = 0, g, p, pc, pg;
7276 int num_target_port_groups, num_target_ports, single;
7277 struct ctl_lun *lun;
7278 struct ctl_softc *softc;
7279 struct ctl_port *port;
7280 struct scsi_target_group_data *rtg_ptr;
7281 struct scsi_target_group_data_extended *rtg_ext_ptr;
7282 struct scsi_target_port_group_descriptor *tpg_desc;
7284 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7286 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7287 softc = control_softc;
7288 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7290 retval = CTL_RETVAL_COMPLETE;
7292 switch (cdb->byte2 & STG_PDF_MASK) {
7293 case STG_PDF_LENGTH:
7296 case STG_PDF_EXTENDED:
7300 ctl_set_invalid_field(/*ctsio*/ ctsio,
7306 ctl_done((union ctl_io *)ctsio);
7310 single = ctl_is_single;
7312 num_target_port_groups = 1;
7314 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
7315 num_target_ports = 0;
7316 mtx_lock(&softc->ctl_lock);
7317 STAILQ_FOREACH(port, &softc->port_list, links) {
7318 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7320 if (ctl_map_lun_back(port->targ_port, lun->lun) >= CTL_MAX_LUNS)
7324 mtx_unlock(&softc->ctl_lock);
7327 total_len = sizeof(struct scsi_target_group_data_extended);
7329 total_len = sizeof(struct scsi_target_group_data);
7330 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7331 num_target_port_groups +
7332 sizeof(struct scsi_target_port_descriptor) *
7333 num_target_ports * num_target_port_groups;
7335 alloc_len = scsi_4btoul(cdb->length);
7337 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7339 ctsio->kern_sg_entries = 0;
7341 if (total_len < alloc_len) {
7342 ctsio->residual = alloc_len - total_len;
7343 ctsio->kern_data_len = total_len;
7344 ctsio->kern_total_len = total_len;
7346 ctsio->residual = 0;
7347 ctsio->kern_data_len = alloc_len;
7348 ctsio->kern_total_len = alloc_len;
7350 ctsio->kern_data_resid = 0;
7351 ctsio->kern_rel_offset = 0;
7354 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7355 ctsio->kern_data_ptr;
7356 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7357 rtg_ext_ptr->format_type = 0x10;
7358 rtg_ext_ptr->implicit_transition_time = 0;
7359 tpg_desc = &rtg_ext_ptr->groups[0];
7361 rtg_ptr = (struct scsi_target_group_data *)
7362 ctsio->kern_data_ptr;
7363 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7364 tpg_desc = &rtg_ptr->groups[0];
7367 pg = ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS;
7368 mtx_lock(&softc->ctl_lock);
7369 for (g = 0; g < num_target_port_groups; g++) {
7371 tpg_desc->pref_state = TPG_PRIMARY |
7372 TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7374 tpg_desc->pref_state =
7375 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7376 tpg_desc->support = TPG_AO_SUP;
7378 tpg_desc->support |= TPG_AN_SUP;
7379 scsi_ulto2b(g + 1, tpg_desc->target_port_group);
7380 tpg_desc->status = TPG_IMPLICIT;
7382 STAILQ_FOREACH(port, &softc->port_list, links) {
7383 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7385 if (ctl_map_lun_back(port->targ_port, lun->lun) >=
7388 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS;
7389 scsi_ulto2b(p, tpg_desc->descriptors[pc].
7390 relative_target_port_identifier);
7393 tpg_desc->target_port_count = pc;
7394 tpg_desc = (struct scsi_target_port_group_descriptor *)
7395 &tpg_desc->descriptors[pc];
7397 mtx_unlock(&softc->ctl_lock);
7399 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7400 ctsio->be_move_done = ctl_config_move_done;
7402 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7403 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7404 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7405 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7406 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7408 ctl_datamove((union ctl_io *)ctsio);
7413 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7415 struct ctl_lun *lun;
7416 struct scsi_report_supported_opcodes *cdb;
7417 const struct ctl_cmd_entry *entry, *sentry;
7418 struct scsi_report_supported_opcodes_all *all;
7419 struct scsi_report_supported_opcodes_descr *descr;
7420 struct scsi_report_supported_opcodes_one *one;
7422 int alloc_len, total_len;
7423 int opcode, service_action, i, j, num;
7425 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7427 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7428 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7430 retval = CTL_RETVAL_COMPLETE;
7432 opcode = cdb->requested_opcode;
7433 service_action = scsi_2btoul(cdb->requested_service_action);
7434 switch (cdb->options & RSO_OPTIONS_MASK) {
7435 case RSO_OPTIONS_ALL:
7437 for (i = 0; i < 256; i++) {
7438 entry = &ctl_cmd_table[i];
7439 if (entry->flags & CTL_CMD_FLAG_SA5) {
7440 for (j = 0; j < 32; j++) {
7441 sentry = &((const struct ctl_cmd_entry *)
7443 if (ctl_cmd_applicable(
7444 lun->be_lun->lun_type, sentry))
7448 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7453 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7454 num * sizeof(struct scsi_report_supported_opcodes_descr);
7456 case RSO_OPTIONS_OC:
7457 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7458 ctl_set_invalid_field(/*ctsio*/ ctsio,
7464 ctl_done((union ctl_io *)ctsio);
7465 return (CTL_RETVAL_COMPLETE);
7467 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7469 case RSO_OPTIONS_OC_SA:
7470 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7471 service_action >= 32) {
7472 ctl_set_invalid_field(/*ctsio*/ ctsio,
7478 ctl_done((union ctl_io *)ctsio);
7479 return (CTL_RETVAL_COMPLETE);
7481 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7484 ctl_set_invalid_field(/*ctsio*/ ctsio,
7490 ctl_done((union ctl_io *)ctsio);
7491 return (CTL_RETVAL_COMPLETE);
7494 alloc_len = scsi_4btoul(cdb->length);
7496 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7498 ctsio->kern_sg_entries = 0;
7500 if (total_len < alloc_len) {
7501 ctsio->residual = alloc_len - total_len;
7502 ctsio->kern_data_len = total_len;
7503 ctsio->kern_total_len = total_len;
7505 ctsio->residual = 0;
7506 ctsio->kern_data_len = alloc_len;
7507 ctsio->kern_total_len = alloc_len;
7509 ctsio->kern_data_resid = 0;
7510 ctsio->kern_rel_offset = 0;
7512 switch (cdb->options & RSO_OPTIONS_MASK) {
7513 case RSO_OPTIONS_ALL:
7514 all = (struct scsi_report_supported_opcodes_all *)
7515 ctsio->kern_data_ptr;
7517 for (i = 0; i < 256; i++) {
7518 entry = &ctl_cmd_table[i];
7519 if (entry->flags & CTL_CMD_FLAG_SA5) {
7520 for (j = 0; j < 32; j++) {
7521 sentry = &((const struct ctl_cmd_entry *)
7523 if (!ctl_cmd_applicable(
7524 lun->be_lun->lun_type, sentry))
7526 descr = &all->descr[num++];
7528 scsi_ulto2b(j, descr->service_action);
7529 descr->flags = RSO_SERVACTV;
7530 scsi_ulto2b(sentry->length,
7534 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7537 descr = &all->descr[num++];
7539 scsi_ulto2b(0, descr->service_action);
7541 scsi_ulto2b(entry->length, descr->cdb_length);
7545 num * sizeof(struct scsi_report_supported_opcodes_descr),
7548 case RSO_OPTIONS_OC:
7549 one = (struct scsi_report_supported_opcodes_one *)
7550 ctsio->kern_data_ptr;
7551 entry = &ctl_cmd_table[opcode];
7553 case RSO_OPTIONS_OC_SA:
7554 one = (struct scsi_report_supported_opcodes_one *)
7555 ctsio->kern_data_ptr;
7556 entry = &ctl_cmd_table[opcode];
7557 entry = &((const struct ctl_cmd_entry *)
7558 entry->execute)[service_action];
7560 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7562 scsi_ulto2b(entry->length, one->cdb_length);
7563 one->cdb_usage[0] = opcode;
7564 memcpy(&one->cdb_usage[1], entry->usage,
7571 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7572 ctsio->be_move_done = ctl_config_move_done;
7574 ctl_datamove((union ctl_io *)ctsio);
7579 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7581 struct ctl_lun *lun;
7582 struct scsi_report_supported_tmf *cdb;
7583 struct scsi_report_supported_tmf_data *data;
7585 int alloc_len, total_len;
7587 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7589 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7590 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7592 retval = CTL_RETVAL_COMPLETE;
7594 total_len = sizeof(struct scsi_report_supported_tmf_data);
7595 alloc_len = scsi_4btoul(cdb->length);
7597 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7599 ctsio->kern_sg_entries = 0;
7601 if (total_len < alloc_len) {
7602 ctsio->residual = alloc_len - total_len;
7603 ctsio->kern_data_len = total_len;
7604 ctsio->kern_total_len = total_len;
7606 ctsio->residual = 0;
7607 ctsio->kern_data_len = alloc_len;
7608 ctsio->kern_total_len = alloc_len;
7610 ctsio->kern_data_resid = 0;
7611 ctsio->kern_rel_offset = 0;
7613 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr;
7614 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS;
7615 data->byte2 |= RST_ITNRS;
7617 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7618 ctsio->be_move_done = ctl_config_move_done;
7620 ctl_datamove((union ctl_io *)ctsio);
7625 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7627 struct ctl_lun *lun;
7628 struct scsi_report_timestamp *cdb;
7629 struct scsi_report_timestamp_data *data;
7633 int alloc_len, total_len;
7635 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7637 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7638 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7640 retval = CTL_RETVAL_COMPLETE;
7642 total_len = sizeof(struct scsi_report_timestamp_data);
7643 alloc_len = scsi_4btoul(cdb->length);
7645 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7647 ctsio->kern_sg_entries = 0;
7649 if (total_len < alloc_len) {
7650 ctsio->residual = alloc_len - total_len;
7651 ctsio->kern_data_len = total_len;
7652 ctsio->kern_total_len = total_len;
7654 ctsio->residual = 0;
7655 ctsio->kern_data_len = alloc_len;
7656 ctsio->kern_total_len = alloc_len;
7658 ctsio->kern_data_resid = 0;
7659 ctsio->kern_rel_offset = 0;
7661 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7662 scsi_ulto2b(sizeof(*data) - 2, data->length);
7663 data->origin = RTS_ORIG_OUTSIDE;
7665 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7666 scsi_ulto4b(timestamp >> 16, data->timestamp);
7667 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7669 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7670 ctsio->be_move_done = ctl_config_move_done;
7672 ctl_datamove((union ctl_io *)ctsio);
7677 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7679 struct scsi_per_res_in *cdb;
7680 int alloc_len, total_len = 0;
7681 /* struct scsi_per_res_in_rsrv in_data; */
7682 struct ctl_lun *lun;
7683 struct ctl_softc *softc;
7685 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7687 softc = control_softc;
7689 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7691 alloc_len = scsi_2btoul(cdb->length);
7693 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7696 mtx_lock(&lun->lun_lock);
7697 switch (cdb->action) {
7698 case SPRI_RK: /* read keys */
7699 total_len = sizeof(struct scsi_per_res_in_keys) +
7701 sizeof(struct scsi_per_res_key);
7703 case SPRI_RR: /* read reservation */
7704 if (lun->flags & CTL_LUN_PR_RESERVED)
7705 total_len = sizeof(struct scsi_per_res_in_rsrv);
7707 total_len = sizeof(struct scsi_per_res_in_header);
7709 case SPRI_RC: /* report capabilities */
7710 total_len = sizeof(struct scsi_per_res_cap);
7712 case SPRI_RS: /* read full status */
7713 total_len = sizeof(struct scsi_per_res_in_header) +
7714 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7718 panic("Invalid PR type %x", cdb->action);
7720 mtx_unlock(&lun->lun_lock);
7722 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7724 if (total_len < alloc_len) {
7725 ctsio->residual = alloc_len - total_len;
7726 ctsio->kern_data_len = total_len;
7727 ctsio->kern_total_len = total_len;
7729 ctsio->residual = 0;
7730 ctsio->kern_data_len = alloc_len;
7731 ctsio->kern_total_len = alloc_len;
7734 ctsio->kern_data_resid = 0;
7735 ctsio->kern_rel_offset = 0;
7736 ctsio->kern_sg_entries = 0;
7738 mtx_lock(&lun->lun_lock);
7739 switch (cdb->action) {
7740 case SPRI_RK: { // read keys
7741 struct scsi_per_res_in_keys *res_keys;
7744 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7747 * We had to drop the lock to allocate our buffer, which
7748 * leaves time for someone to come in with another
7749 * persistent reservation. (That is unlikely, though,
7750 * since this should be the only persistent reservation
7751 * command active right now.)
7753 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7754 (lun->pr_key_count *
7755 sizeof(struct scsi_per_res_key)))){
7756 mtx_unlock(&lun->lun_lock);
7757 free(ctsio->kern_data_ptr, M_CTL);
7758 printf("%s: reservation length changed, retrying\n",
7763 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7765 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7766 lun->pr_key_count, res_keys->header.length);
7768 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7769 if (lun->pr_keys[i] == 0)
7773 * We used lun->pr_key_count to calculate the
7774 * size to allocate. If it turns out the number of
7775 * initiators with the registered flag set is
7776 * larger than that (i.e. they haven't been kept in
7777 * sync), we've got a problem.
7779 if (key_count >= lun->pr_key_count) {
7781 csevent_log(CSC_CTL | CSC_SHELF_SW |
7783 csevent_LogType_Fault,
7784 csevent_AlertLevel_Yellow,
7785 csevent_FRU_ShelfController,
7786 csevent_FRU_Firmware,
7787 csevent_FRU_Unknown,
7788 "registered keys %d >= key "
7789 "count %d", key_count,
7795 scsi_u64to8b(lun->pr_keys[i],
7796 res_keys->keys[key_count].key);
7801 case SPRI_RR: { // read reservation
7802 struct scsi_per_res_in_rsrv *res;
7803 int tmp_len, header_only;
7805 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7807 scsi_ulto4b(lun->PRGeneration, res->header.generation);
7809 if (lun->flags & CTL_LUN_PR_RESERVED)
7811 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7812 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7813 res->header.length);
7816 tmp_len = sizeof(struct scsi_per_res_in_header);
7817 scsi_ulto4b(0, res->header.length);
7822 * We had to drop the lock to allocate our buffer, which
7823 * leaves time for someone to come in with another
7824 * persistent reservation. (That is unlikely, though,
7825 * since this should be the only persistent reservation
7826 * command active right now.)
7828 if (tmp_len != total_len) {
7829 mtx_unlock(&lun->lun_lock);
7830 free(ctsio->kern_data_ptr, M_CTL);
7831 printf("%s: reservation status changed, retrying\n",
7837 * No reservation held, so we're done.
7839 if (header_only != 0)
7843 * If the registration is an All Registrants type, the key
7844 * is 0, since it doesn't really matter.
7846 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7847 scsi_u64to8b(lun->pr_keys[lun->pr_res_idx],
7848 res->data.reservation);
7850 res->data.scopetype = lun->res_type;
7853 case SPRI_RC: //report capabilities
7855 struct scsi_per_res_cap *res_cap;
7858 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7859 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7860 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5;
7861 type_mask = SPRI_TM_WR_EX_AR |
7867 scsi_ulto2b(type_mask, res_cap->type_mask);
7870 case SPRI_RS: { // read full status
7871 struct scsi_per_res_in_full *res_status;
7872 struct scsi_per_res_in_full_desc *res_desc;
7873 struct ctl_port *port;
7876 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7879 * We had to drop the lock to allocate our buffer, which
7880 * leaves time for someone to come in with another
7881 * persistent reservation. (That is unlikely, though,
7882 * since this should be the only persistent reservation
7883 * command active right now.)
7885 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7886 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7887 lun->pr_key_count)){
7888 mtx_unlock(&lun->lun_lock);
7889 free(ctsio->kern_data_ptr, M_CTL);
7890 printf("%s: reservation length changed, retrying\n",
7895 scsi_ulto4b(lun->PRGeneration, res_status->header.generation);
7897 res_desc = &res_status->desc[0];
7898 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7899 if (lun->pr_keys[i] == 0)
7902 scsi_u64to8b(lun->pr_keys[i], res_desc->res_key.key);
7903 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7904 (lun->pr_res_idx == i ||
7905 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7906 res_desc->flags = SPRI_FULL_R_HOLDER;
7907 res_desc->scopetype = lun->res_type;
7909 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7910 res_desc->rel_trgt_port_id);
7912 port = softc->ctl_ports[
7913 ctl_port_idx(i / CTL_MAX_INIT_PER_PORT)];
7915 len = ctl_create_iid(port,
7916 i % CTL_MAX_INIT_PER_PORT,
7917 res_desc->transport_id);
7918 scsi_ulto4b(len, res_desc->additional_length);
7919 res_desc = (struct scsi_per_res_in_full_desc *)
7920 &res_desc->transport_id[len];
7922 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7923 res_status->header.length);
7928 * This is a bug, because we just checked for this above,
7929 * and should have returned an error.
7931 panic("Invalid PR type %x", cdb->action);
7932 break; /* NOTREACHED */
7934 mtx_unlock(&lun->lun_lock);
7936 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7937 ctsio->be_move_done = ctl_config_move_done;
7939 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7940 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7941 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7942 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7943 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7945 ctl_datamove((union ctl_io *)ctsio);
7947 return (CTL_RETVAL_COMPLETE);
7951 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7955 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7956 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7957 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7958 struct scsi_per_res_out_parms* param)
7960 union ctl_ha_msg persis_io;
7966 mtx_lock(&lun->lun_lock);
7967 if (sa_res_key == 0) {
7968 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7969 /* validate scope and type */
7970 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7972 mtx_unlock(&lun->lun_lock);
7973 ctl_set_invalid_field(/*ctsio*/ ctsio,
7979 ctl_done((union ctl_io *)ctsio);
7983 if (type>8 || type==2 || type==4 || type==0) {
7984 mtx_unlock(&lun->lun_lock);
7985 ctl_set_invalid_field(/*ctsio*/ ctsio,
7991 ctl_done((union ctl_io *)ctsio);
7996 * Unregister everybody else and build UA for
7999 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8000 if (i == residx || lun->pr_keys[i] == 0)
8004 && i <CTL_MAX_INITIATORS)
8005 lun->pending_ua[i] |=
8007 else if (persis_offset
8008 && i >= persis_offset)
8009 lun->pending_ua[i-persis_offset] |=
8011 lun->pr_keys[i] = 0;
8013 lun->pr_key_count = 1;
8014 lun->res_type = type;
8015 if (lun->res_type != SPR_TYPE_WR_EX_AR
8016 && lun->res_type != SPR_TYPE_EX_AC_AR)
8017 lun->pr_res_idx = residx;
8019 /* send msg to other side */
8020 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8021 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8022 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8023 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8024 persis_io.pr.pr_info.res_type = type;
8025 memcpy(persis_io.pr.pr_info.sa_res_key,
8026 param->serv_act_res_key,
8027 sizeof(param->serv_act_res_key));
8028 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8029 &persis_io, sizeof(persis_io), 0)) >
8030 CTL_HA_STATUS_SUCCESS) {
8031 printf("CTL:Persis Out error returned "
8032 "from ctl_ha_msg_send %d\n",
8036 /* not all registrants */
8037 mtx_unlock(&lun->lun_lock);
8038 free(ctsio->kern_data_ptr, M_CTL);
8039 ctl_set_invalid_field(ctsio,
8045 ctl_done((union ctl_io *)ctsio);
8048 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8049 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
8052 if (res_key == sa_res_key) {
8055 * The spec implies this is not good but doesn't
8056 * say what to do. There are two choices either
8057 * generate a res conflict or check condition
8058 * with illegal field in parameter data. Since
8059 * that is what is done when the sa_res_key is
8060 * zero I'll take that approach since this has
8061 * to do with the sa_res_key.
8063 mtx_unlock(&lun->lun_lock);
8064 free(ctsio->kern_data_ptr, M_CTL);
8065 ctl_set_invalid_field(ctsio,
8071 ctl_done((union ctl_io *)ctsio);
8075 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8076 if (lun->pr_keys[i] != sa_res_key)
8080 lun->pr_keys[i] = 0;
8081 lun->pr_key_count--;
8083 if (!persis_offset && i < CTL_MAX_INITIATORS)
8084 lun->pending_ua[i] |= CTL_UA_REG_PREEMPT;
8085 else if (persis_offset && i >= persis_offset)
8086 lun->pending_ua[i-persis_offset] |=
8090 mtx_unlock(&lun->lun_lock);
8091 free(ctsio->kern_data_ptr, M_CTL);
8092 ctl_set_reservation_conflict(ctsio);
8093 ctl_done((union ctl_io *)ctsio);
8094 return (CTL_RETVAL_COMPLETE);
8096 /* send msg to other side */
8097 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8098 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8099 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8100 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8101 persis_io.pr.pr_info.res_type = type;
8102 memcpy(persis_io.pr.pr_info.sa_res_key,
8103 param->serv_act_res_key,
8104 sizeof(param->serv_act_res_key));
8105 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8106 &persis_io, sizeof(persis_io), 0)) >
8107 CTL_HA_STATUS_SUCCESS) {
8108 printf("CTL:Persis Out error returned from "
8109 "ctl_ha_msg_send %d\n", isc_retval);
8112 /* Reserved but not all registrants */
8113 /* sa_res_key is res holder */
8114 if (sa_res_key == lun->pr_keys[lun->pr_res_idx]) {
8115 /* validate scope and type */
8116 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
8118 mtx_unlock(&lun->lun_lock);
8119 ctl_set_invalid_field(/*ctsio*/ ctsio,
8125 ctl_done((union ctl_io *)ctsio);
8129 if (type>8 || type==2 || type==4 || type==0) {
8130 mtx_unlock(&lun->lun_lock);
8131 ctl_set_invalid_field(/*ctsio*/ ctsio,
8137 ctl_done((union ctl_io *)ctsio);
8143 * if sa_res_key != res_key remove all
8144 * registrants w/sa_res_key and generate UA
8145 * for these registrants(Registrations
8146 * Preempted) if it wasn't an exclusive
8147 * reservation generate UA(Reservations
8148 * Preempted) for all other registered nexuses
8149 * if the type has changed. Establish the new
8150 * reservation and holder. If res_key and
8151 * sa_res_key are the same do the above
8152 * except don't unregister the res holder.
8155 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8156 if (i == residx || lun->pr_keys[i] == 0)
8159 if (sa_res_key == lun->pr_keys[i]) {
8160 lun->pr_keys[i] = 0;
8161 lun->pr_key_count--;
8164 && i < CTL_MAX_INITIATORS)
8165 lun->pending_ua[i] |=
8167 else if (persis_offset
8168 && i >= persis_offset)
8169 lun->pending_ua[i-persis_offset] |=
8171 } else if (type != lun->res_type
8172 && (lun->res_type == SPR_TYPE_WR_EX_RO
8173 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
8175 && i < CTL_MAX_INITIATORS)
8176 lun->pending_ua[i] |=
8178 else if (persis_offset
8179 && i >= persis_offset)
8185 lun->res_type = type;
8186 if (lun->res_type != SPR_TYPE_WR_EX_AR
8187 && lun->res_type != SPR_TYPE_EX_AC_AR)
8188 lun->pr_res_idx = residx;
8190 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8192 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8193 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8194 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8195 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8196 persis_io.pr.pr_info.res_type = type;
8197 memcpy(persis_io.pr.pr_info.sa_res_key,
8198 param->serv_act_res_key,
8199 sizeof(param->serv_act_res_key));
8200 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8201 &persis_io, sizeof(persis_io), 0)) >
8202 CTL_HA_STATUS_SUCCESS) {
8203 printf("CTL:Persis Out error returned "
8204 "from ctl_ha_msg_send %d\n",
8209 * sa_res_key is not the res holder just
8210 * remove registrants
8214 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8215 if (sa_res_key != lun->pr_keys[i])
8219 lun->pr_keys[i] = 0;
8220 lun->pr_key_count--;
8223 && i < CTL_MAX_INITIATORS)
8224 lun->pending_ua[i] |=
8226 else if (persis_offset
8227 && i >= persis_offset)
8228 lun->pending_ua[i-persis_offset] |=
8233 mtx_unlock(&lun->lun_lock);
8234 free(ctsio->kern_data_ptr, M_CTL);
8235 ctl_set_reservation_conflict(ctsio);
8236 ctl_done((union ctl_io *)ctsio);
8239 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8240 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8241 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8242 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8243 persis_io.pr.pr_info.res_type = type;
8244 memcpy(persis_io.pr.pr_info.sa_res_key,
8245 param->serv_act_res_key,
8246 sizeof(param->serv_act_res_key));
8247 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8248 &persis_io, sizeof(persis_io), 0)) >
8249 CTL_HA_STATUS_SUCCESS) {
8250 printf("CTL:Persis Out error returned "
8251 "from ctl_ha_msg_send %d\n",
8257 lun->PRGeneration++;
8258 mtx_unlock(&lun->lun_lock);
8264 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8266 uint64_t sa_res_key;
8269 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8271 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8272 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8273 || sa_res_key != lun->pr_keys[lun->pr_res_idx]) {
8274 if (sa_res_key == 0) {
8276 * Unregister everybody else and build UA for
8279 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8280 if (i == msg->pr.pr_info.residx ||
8281 lun->pr_keys[i] == 0)
8285 && i < CTL_MAX_INITIATORS)
8286 lun->pending_ua[i] |=
8288 else if (persis_offset && i >= persis_offset)
8289 lun->pending_ua[i - persis_offset] |=
8291 lun->pr_keys[i] = 0;
8294 lun->pr_key_count = 1;
8295 lun->res_type = msg->pr.pr_info.res_type;
8296 if (lun->res_type != SPR_TYPE_WR_EX_AR
8297 && lun->res_type != SPR_TYPE_EX_AC_AR)
8298 lun->pr_res_idx = msg->pr.pr_info.residx;
8300 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8301 if (sa_res_key == lun->pr_keys[i])
8304 lun->pr_keys[i] = 0;
8305 lun->pr_key_count--;
8308 && i < persis_offset)
8309 lun->pending_ua[i] |=
8311 else if (persis_offset
8312 && i >= persis_offset)
8313 lun->pending_ua[i - persis_offset] |=
8318 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8319 if (i == msg->pr.pr_info.residx ||
8320 lun->pr_keys[i] == 0)
8323 if (sa_res_key == lun->pr_keys[i]) {
8324 lun->pr_keys[i] = 0;
8325 lun->pr_key_count--;
8327 && i < CTL_MAX_INITIATORS)
8328 lun->pending_ua[i] |=
8330 else if (persis_offset
8331 && i >= persis_offset)
8332 lun->pending_ua[i - persis_offset] |=
8334 } else if (msg->pr.pr_info.res_type != lun->res_type
8335 && (lun->res_type == SPR_TYPE_WR_EX_RO
8336 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
8338 && i < persis_offset)
8339 lun->pending_ua[i] |=
8341 else if (persis_offset
8342 && i >= persis_offset)
8343 lun->pending_ua[i - persis_offset] |=
8347 lun->res_type = msg->pr.pr_info.res_type;
8348 if (lun->res_type != SPR_TYPE_WR_EX_AR
8349 && lun->res_type != SPR_TYPE_EX_AC_AR)
8350 lun->pr_res_idx = msg->pr.pr_info.residx;
8352 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8354 lun->PRGeneration++;
8360 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8364 u_int32_t param_len;
8365 struct scsi_per_res_out *cdb;
8366 struct ctl_lun *lun;
8367 struct scsi_per_res_out_parms* param;
8368 struct ctl_softc *softc;
8370 uint64_t res_key, sa_res_key;
8372 union ctl_ha_msg persis_io;
8375 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8377 retval = CTL_RETVAL_COMPLETE;
8379 softc = control_softc;
8381 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8382 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8385 * We only support whole-LUN scope. The scope & type are ignored for
8386 * register, register and ignore existing key and clear.
8387 * We sometimes ignore scope and type on preempts too!!
8388 * Verify reservation type here as well.
8390 type = cdb->scope_type & SPR_TYPE_MASK;
8391 if ((cdb->action == SPRO_RESERVE)
8392 || (cdb->action == SPRO_RELEASE)) {
8393 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8394 ctl_set_invalid_field(/*ctsio*/ ctsio,
8400 ctl_done((union ctl_io *)ctsio);
8401 return (CTL_RETVAL_COMPLETE);
8404 if (type>8 || type==2 || type==4 || type==0) {
8405 ctl_set_invalid_field(/*ctsio*/ ctsio,
8411 ctl_done((union ctl_io *)ctsio);
8412 return (CTL_RETVAL_COMPLETE);
8416 param_len = scsi_4btoul(cdb->length);
8418 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8419 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8420 ctsio->kern_data_len = param_len;
8421 ctsio->kern_total_len = param_len;
8422 ctsio->kern_data_resid = 0;
8423 ctsio->kern_rel_offset = 0;
8424 ctsio->kern_sg_entries = 0;
8425 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8426 ctsio->be_move_done = ctl_config_move_done;
8427 ctl_datamove((union ctl_io *)ctsio);
8429 return (CTL_RETVAL_COMPLETE);
8432 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8434 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8435 res_key = scsi_8btou64(param->res_key.key);
8436 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8439 * Validate the reservation key here except for SPRO_REG_IGNO
8440 * This must be done for all other service actions
8442 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8443 mtx_lock(&lun->lun_lock);
8444 if (lun->pr_keys[residx] != 0) {
8445 if (res_key != lun->pr_keys[residx]) {
8447 * The current key passed in doesn't match
8448 * the one the initiator previously
8451 mtx_unlock(&lun->lun_lock);
8452 free(ctsio->kern_data_ptr, M_CTL);
8453 ctl_set_reservation_conflict(ctsio);
8454 ctl_done((union ctl_io *)ctsio);
8455 return (CTL_RETVAL_COMPLETE);
8457 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8459 * We are not registered
8461 mtx_unlock(&lun->lun_lock);
8462 free(ctsio->kern_data_ptr, M_CTL);
8463 ctl_set_reservation_conflict(ctsio);
8464 ctl_done((union ctl_io *)ctsio);
8465 return (CTL_RETVAL_COMPLETE);
8466 } else if (res_key != 0) {
8468 * We are not registered and trying to register but
8469 * the register key isn't zero.
8471 mtx_unlock(&lun->lun_lock);
8472 free(ctsio->kern_data_ptr, M_CTL);
8473 ctl_set_reservation_conflict(ctsio);
8474 ctl_done((union ctl_io *)ctsio);
8475 return (CTL_RETVAL_COMPLETE);
8477 mtx_unlock(&lun->lun_lock);
8480 switch (cdb->action & SPRO_ACTION_MASK) {
8482 case SPRO_REG_IGNO: {
8485 printf("Registration received\n");
8489 * We don't support any of these options, as we report in
8490 * the read capabilities request (see
8491 * ctl_persistent_reserve_in(), above).
8493 if ((param->flags & SPR_SPEC_I_PT)
8494 || (param->flags & SPR_ALL_TG_PT)
8495 || (param->flags & SPR_APTPL)) {
8498 if (param->flags & SPR_APTPL)
8500 else if (param->flags & SPR_ALL_TG_PT)
8502 else /* SPR_SPEC_I_PT */
8505 free(ctsio->kern_data_ptr, M_CTL);
8506 ctl_set_invalid_field(ctsio,
8512 ctl_done((union ctl_io *)ctsio);
8513 return (CTL_RETVAL_COMPLETE);
8516 mtx_lock(&lun->lun_lock);
8519 * The initiator wants to clear the
8522 if (sa_res_key == 0) {
8524 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8525 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8526 && lun->pr_keys[residx] == 0)) {
8527 mtx_unlock(&lun->lun_lock);
8531 lun->pr_keys[residx] = 0;
8532 lun->pr_key_count--;
8534 if (residx == lun->pr_res_idx) {
8535 lun->flags &= ~CTL_LUN_PR_RESERVED;
8536 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8538 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8539 || lun->res_type == SPR_TYPE_EX_AC_RO)
8540 && lun->pr_key_count) {
8542 * If the reservation is a registrants
8543 * only type we need to generate a UA
8544 * for other registered inits. The
8545 * sense code should be RESERVATIONS
8549 for (i = 0; i < CTL_MAX_INITIATORS;i++){
8551 i + persis_offset] == 0)
8553 lun->pending_ua[i] |=
8558 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8559 if (lun->pr_key_count==0) {
8560 lun->flags &= ~CTL_LUN_PR_RESERVED;
8562 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8565 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8566 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8567 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8568 persis_io.pr.pr_info.residx = residx;
8569 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8570 &persis_io, sizeof(persis_io), 0 )) >
8571 CTL_HA_STATUS_SUCCESS) {
8572 printf("CTL:Persis Out error returned from "
8573 "ctl_ha_msg_send %d\n", isc_retval);
8575 } else /* sa_res_key != 0 */ {
8578 * If we aren't registered currently then increment
8579 * the key count and set the registered flag.
8581 if (lun->pr_keys[residx] == 0)
8582 lun->pr_key_count++;
8583 lun->pr_keys[residx] = sa_res_key;
8585 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8586 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8587 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8588 persis_io.pr.pr_info.residx = residx;
8589 memcpy(persis_io.pr.pr_info.sa_res_key,
8590 param->serv_act_res_key,
8591 sizeof(param->serv_act_res_key));
8592 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8593 &persis_io, sizeof(persis_io), 0)) >
8594 CTL_HA_STATUS_SUCCESS) {
8595 printf("CTL:Persis Out error returned from "
8596 "ctl_ha_msg_send %d\n", isc_retval);
8599 lun->PRGeneration++;
8600 mtx_unlock(&lun->lun_lock);
8606 printf("Reserve executed type %d\n", type);
8608 mtx_lock(&lun->lun_lock);
8609 if (lun->flags & CTL_LUN_PR_RESERVED) {
8611 * if this isn't the reservation holder and it's
8612 * not a "all registrants" type or if the type is
8613 * different then we have a conflict
8615 if ((lun->pr_res_idx != residx
8616 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8617 || lun->res_type != type) {
8618 mtx_unlock(&lun->lun_lock);
8619 free(ctsio->kern_data_ptr, M_CTL);
8620 ctl_set_reservation_conflict(ctsio);
8621 ctl_done((union ctl_io *)ctsio);
8622 return (CTL_RETVAL_COMPLETE);
8624 mtx_unlock(&lun->lun_lock);
8625 } else /* create a reservation */ {
8627 * If it's not an "all registrants" type record
8628 * reservation holder
8630 if (type != SPR_TYPE_WR_EX_AR
8631 && type != SPR_TYPE_EX_AC_AR)
8632 lun->pr_res_idx = residx; /* Res holder */
8634 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8636 lun->flags |= CTL_LUN_PR_RESERVED;
8637 lun->res_type = type;
8639 mtx_unlock(&lun->lun_lock);
8641 /* send msg to other side */
8642 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8643 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8644 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8645 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8646 persis_io.pr.pr_info.res_type = type;
8647 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8648 &persis_io, sizeof(persis_io), 0)) >
8649 CTL_HA_STATUS_SUCCESS) {
8650 printf("CTL:Persis Out error returned from "
8651 "ctl_ha_msg_send %d\n", isc_retval);
8657 mtx_lock(&lun->lun_lock);
8658 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8659 /* No reservation exists return good status */
8660 mtx_unlock(&lun->lun_lock);
8664 * Is this nexus a reservation holder?
8666 if (lun->pr_res_idx != residx
8667 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8669 * not a res holder return good status but
8672 mtx_unlock(&lun->lun_lock);
8676 if (lun->res_type != type) {
8677 mtx_unlock(&lun->lun_lock);
8678 free(ctsio->kern_data_ptr, M_CTL);
8679 ctl_set_illegal_pr_release(ctsio);
8680 ctl_done((union ctl_io *)ctsio);
8681 return (CTL_RETVAL_COMPLETE);
8684 /* okay to release */
8685 lun->flags &= ~CTL_LUN_PR_RESERVED;
8686 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8690 * if this isn't an exclusive access
8691 * res generate UA for all other
8694 if (type != SPR_TYPE_EX_AC
8695 && type != SPR_TYPE_WR_EX) {
8696 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8698 lun->pr_keys[i + persis_offset] == 0)
8700 lun->pending_ua[i] |= CTL_UA_RES_RELEASE;
8703 mtx_unlock(&lun->lun_lock);
8704 /* Send msg to other side */
8705 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8706 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8707 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8708 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io,
8709 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8710 printf("CTL:Persis Out error returned from "
8711 "ctl_ha_msg_send %d\n", isc_retval);
8716 /* send msg to other side */
8718 mtx_lock(&lun->lun_lock);
8719 lun->flags &= ~CTL_LUN_PR_RESERVED;
8721 lun->pr_key_count = 0;
8722 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8724 lun->pr_keys[residx] = 0;
8726 for (i=0; i < 2*CTL_MAX_INITIATORS; i++)
8727 if (lun->pr_keys[i] != 0) {
8728 if (!persis_offset && i < CTL_MAX_INITIATORS)
8729 lun->pending_ua[i] |=
8731 else if (persis_offset && i >= persis_offset)
8732 lun->pending_ua[i-persis_offset] |=
8735 lun->pr_keys[i] = 0;
8737 lun->PRGeneration++;
8738 mtx_unlock(&lun->lun_lock);
8739 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8740 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8741 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8742 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8743 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8744 printf("CTL:Persis Out error returned from "
8745 "ctl_ha_msg_send %d\n", isc_retval);
8749 case SPRO_PREEMPT: {
8752 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8753 residx, ctsio, cdb, param);
8755 return (CTL_RETVAL_COMPLETE);
8759 panic("Invalid PR type %x", cdb->action);
8763 free(ctsio->kern_data_ptr, M_CTL);
8764 ctl_set_success(ctsio);
8765 ctl_done((union ctl_io *)ctsio);
8771 * This routine is for handling a message from the other SC pertaining to
8772 * persistent reserve out. All the error checking will have been done
8773 * so only perorming the action need be done here to keep the two
8777 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8779 struct ctl_lun *lun;
8780 struct ctl_softc *softc;
8784 softc = control_softc;
8786 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8787 lun = softc->ctl_luns[targ_lun];
8788 mtx_lock(&lun->lun_lock);
8789 switch(msg->pr.pr_info.action) {
8790 case CTL_PR_REG_KEY:
8791 if (lun->pr_keys[msg->pr.pr_info.residx] == 0)
8792 lun->pr_key_count++;
8793 lun->pr_keys[msg->pr.pr_info.residx] =
8794 scsi_8btou64(msg->pr.pr_info.sa_res_key);
8795 lun->PRGeneration++;
8798 case CTL_PR_UNREG_KEY:
8799 lun->pr_keys[msg->pr.pr_info.residx] = 0;
8800 lun->pr_key_count--;
8802 /* XXX Need to see if the reservation has been released */
8803 /* if so do we need to generate UA? */
8804 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8805 lun->flags &= ~CTL_LUN_PR_RESERVED;
8806 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8808 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8809 || lun->res_type == SPR_TYPE_EX_AC_RO)
8810 && lun->pr_key_count) {
8812 * If the reservation is a registrants
8813 * only type we need to generate a UA
8814 * for other registered inits. The
8815 * sense code should be RESERVATIONS
8819 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8821 persis_offset] == 0)
8824 lun->pending_ua[i] |=
8829 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8830 if (lun->pr_key_count==0) {
8831 lun->flags &= ~CTL_LUN_PR_RESERVED;
8833 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8836 lun->PRGeneration++;
8839 case CTL_PR_RESERVE:
8840 lun->flags |= CTL_LUN_PR_RESERVED;
8841 lun->res_type = msg->pr.pr_info.res_type;
8842 lun->pr_res_idx = msg->pr.pr_info.residx;
8846 case CTL_PR_RELEASE:
8848 * if this isn't an exclusive access res generate UA for all
8849 * other registrants.
8851 if (lun->res_type != SPR_TYPE_EX_AC
8852 && lun->res_type != SPR_TYPE_WR_EX) {
8853 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8854 if (lun->pr_keys[i+persis_offset] != 0)
8855 lun->pending_ua[i] |=
8859 lun->flags &= ~CTL_LUN_PR_RESERVED;
8860 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8864 case CTL_PR_PREEMPT:
8865 ctl_pro_preempt_other(lun, msg);
8868 lun->flags &= ~CTL_LUN_PR_RESERVED;
8870 lun->pr_key_count = 0;
8871 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8873 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8874 if (lun->pr_keys[i] == 0)
8877 && i < CTL_MAX_INITIATORS)
8878 lun->pending_ua[i] |= CTL_UA_RES_PREEMPT;
8879 else if (persis_offset
8880 && i >= persis_offset)
8881 lun->pending_ua[i-persis_offset] |=
8883 lun->pr_keys[i] = 0;
8885 lun->PRGeneration++;
8889 mtx_unlock(&lun->lun_lock);
8893 ctl_read_write(struct ctl_scsiio *ctsio)
8895 struct ctl_lun *lun;
8896 struct ctl_lba_len_flags *lbalen;
8898 uint32_t num_blocks;
8902 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8904 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8907 retval = CTL_RETVAL_COMPLETE;
8909 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8910 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8911 if (lun->flags & CTL_LUN_PR_RESERVED && isread) {
8915 * XXX KDM need a lock here.
8917 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8918 if ((lun->res_type == SPR_TYPE_EX_AC
8919 && residx != lun->pr_res_idx)
8920 || ((lun->res_type == SPR_TYPE_EX_AC_RO
8921 || lun->res_type == SPR_TYPE_EX_AC_AR)
8922 && lun->pr_keys[residx] == 0)) {
8923 ctl_set_reservation_conflict(ctsio);
8924 ctl_done((union ctl_io *)ctsio);
8925 return (CTL_RETVAL_COMPLETE);
8929 switch (ctsio->cdb[0]) {
8932 struct scsi_rw_6 *cdb;
8934 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8936 lba = scsi_3btoul(cdb->addr);
8937 /* only 5 bits are valid in the most significant address byte */
8939 num_blocks = cdb->length;
8941 * This is correct according to SBC-2.
8943 if (num_blocks == 0)
8949 struct scsi_rw_10 *cdb;
8951 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8952 if (cdb->byte2 & SRW10_FUA)
8953 flags |= CTL_LLF_FUA;
8954 if (cdb->byte2 & SRW10_DPO)
8955 flags |= CTL_LLF_DPO;
8956 lba = scsi_4btoul(cdb->addr);
8957 num_blocks = scsi_2btoul(cdb->length);
8960 case WRITE_VERIFY_10: {
8961 struct scsi_write_verify_10 *cdb;
8963 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8964 flags |= CTL_LLF_FUA;
8965 if (cdb->byte2 & SWV_DPO)
8966 flags |= CTL_LLF_DPO;
8967 lba = scsi_4btoul(cdb->addr);
8968 num_blocks = scsi_2btoul(cdb->length);
8973 struct scsi_rw_12 *cdb;
8975 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8976 if (cdb->byte2 & SRW12_FUA)
8977 flags |= CTL_LLF_FUA;
8978 if (cdb->byte2 & SRW12_DPO)
8979 flags |= CTL_LLF_DPO;
8980 lba = scsi_4btoul(cdb->addr);
8981 num_blocks = scsi_4btoul(cdb->length);
8984 case WRITE_VERIFY_12: {
8985 struct scsi_write_verify_12 *cdb;
8987 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8988 flags |= CTL_LLF_FUA;
8989 if (cdb->byte2 & SWV_DPO)
8990 flags |= CTL_LLF_DPO;
8991 lba = scsi_4btoul(cdb->addr);
8992 num_blocks = scsi_4btoul(cdb->length);
8997 struct scsi_rw_16 *cdb;
8999 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9000 if (cdb->byte2 & SRW12_FUA)
9001 flags |= CTL_LLF_FUA;
9002 if (cdb->byte2 & SRW12_DPO)
9003 flags |= CTL_LLF_DPO;
9004 lba = scsi_8btou64(cdb->addr);
9005 num_blocks = scsi_4btoul(cdb->length);
9008 case WRITE_ATOMIC_16: {
9009 struct scsi_rw_16 *cdb;
9011 if (lun->be_lun->atomicblock == 0) {
9012 ctl_set_invalid_opcode(ctsio);
9013 ctl_done((union ctl_io *)ctsio);
9014 return (CTL_RETVAL_COMPLETE);
9017 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9018 if (cdb->byte2 & SRW12_FUA)
9019 flags |= CTL_LLF_FUA;
9020 if (cdb->byte2 & SRW12_DPO)
9021 flags |= CTL_LLF_DPO;
9022 lba = scsi_8btou64(cdb->addr);
9023 num_blocks = scsi_4btoul(cdb->length);
9024 if (num_blocks > lun->be_lun->atomicblock) {
9025 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
9026 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
9028 ctl_done((union ctl_io *)ctsio);
9029 return (CTL_RETVAL_COMPLETE);
9033 case WRITE_VERIFY_16: {
9034 struct scsi_write_verify_16 *cdb;
9036 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
9037 flags |= CTL_LLF_FUA;
9038 if (cdb->byte2 & SWV_DPO)
9039 flags |= CTL_LLF_DPO;
9040 lba = scsi_8btou64(cdb->addr);
9041 num_blocks = scsi_4btoul(cdb->length);
9046 * We got a command we don't support. This shouldn't
9047 * happen, commands should be filtered out above us.
9049 ctl_set_invalid_opcode(ctsio);
9050 ctl_done((union ctl_io *)ctsio);
9052 return (CTL_RETVAL_COMPLETE);
9053 break; /* NOTREACHED */
9057 * The first check is to make sure we're in bounds, the second
9058 * check is to catch wrap-around problems. If the lba + num blocks
9059 * is less than the lba, then we've wrapped around and the block
9060 * range is invalid anyway.
9062 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9063 || ((lba + num_blocks) < lba)) {
9064 ctl_set_lba_out_of_range(ctsio);
9065 ctl_done((union ctl_io *)ctsio);
9066 return (CTL_RETVAL_COMPLETE);
9070 * According to SBC-3, a transfer length of 0 is not an error.
9071 * Note that this cannot happen with WRITE(6) or READ(6), since 0
9072 * translates to 256 blocks for those commands.
9074 if (num_blocks == 0) {
9075 ctl_set_success(ctsio);
9076 ctl_done((union ctl_io *)ctsio);
9077 return (CTL_RETVAL_COMPLETE);
9080 /* Set FUA and/or DPO if caches are disabled. */
9082 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9084 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
9086 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9088 flags |= CTL_LLF_FUA;
9091 lbalen = (struct ctl_lba_len_flags *)
9092 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9094 lbalen->len = num_blocks;
9095 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
9097 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9098 ctsio->kern_rel_offset = 0;
9100 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
9102 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9108 ctl_cnw_cont(union ctl_io *io)
9110 struct ctl_scsiio *ctsio;
9111 struct ctl_lun *lun;
9112 struct ctl_lba_len_flags *lbalen;
9115 ctsio = &io->scsiio;
9116 ctsio->io_hdr.status = CTL_STATUS_NONE;
9117 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
9118 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9119 lbalen = (struct ctl_lba_len_flags *)
9120 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9121 lbalen->flags &= ~CTL_LLF_COMPARE;
9122 lbalen->flags |= CTL_LLF_WRITE;
9124 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
9125 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9130 ctl_cnw(struct ctl_scsiio *ctsio)
9132 struct ctl_lun *lun;
9133 struct ctl_lba_len_flags *lbalen;
9135 uint32_t num_blocks;
9138 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9140 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
9143 retval = CTL_RETVAL_COMPLETE;
9145 switch (ctsio->cdb[0]) {
9146 case COMPARE_AND_WRITE: {
9147 struct scsi_compare_and_write *cdb;
9149 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
9150 if (cdb->byte2 & SRW10_FUA)
9151 flags |= CTL_LLF_FUA;
9152 if (cdb->byte2 & SRW10_DPO)
9153 flags |= CTL_LLF_DPO;
9154 lba = scsi_8btou64(cdb->addr);
9155 num_blocks = cdb->length;
9160 * We got a command we don't support. This shouldn't
9161 * happen, commands should be filtered out above us.
9163 ctl_set_invalid_opcode(ctsio);
9164 ctl_done((union ctl_io *)ctsio);
9166 return (CTL_RETVAL_COMPLETE);
9167 break; /* NOTREACHED */
9171 * The first check is to make sure we're in bounds, the second
9172 * check is to catch wrap-around problems. If the lba + num blocks
9173 * is less than the lba, then we've wrapped around and the block
9174 * range is invalid anyway.
9176 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9177 || ((lba + num_blocks) < lba)) {
9178 ctl_set_lba_out_of_range(ctsio);
9179 ctl_done((union ctl_io *)ctsio);
9180 return (CTL_RETVAL_COMPLETE);
9184 * According to SBC-3, a transfer length of 0 is not an error.
9186 if (num_blocks == 0) {
9187 ctl_set_success(ctsio);
9188 ctl_done((union ctl_io *)ctsio);
9189 return (CTL_RETVAL_COMPLETE);
9192 /* Set FUA if write cache is disabled. */
9193 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9195 flags |= CTL_LLF_FUA;
9197 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
9198 ctsio->kern_rel_offset = 0;
9201 * Set the IO_CONT flag, so that if this I/O gets passed to
9202 * ctl_data_submit_done(), it'll get passed back to
9203 * ctl_ctl_cnw_cont() for further processing.
9205 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
9206 ctsio->io_cont = ctl_cnw_cont;
9208 lbalen = (struct ctl_lba_len_flags *)
9209 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9211 lbalen->len = num_blocks;
9212 lbalen->flags = CTL_LLF_COMPARE | flags;
9214 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
9215 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9220 ctl_verify(struct ctl_scsiio *ctsio)
9222 struct ctl_lun *lun;
9223 struct ctl_lba_len_flags *lbalen;
9225 uint32_t num_blocks;
9229 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9231 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
9234 flags = CTL_LLF_FUA;
9235 retval = CTL_RETVAL_COMPLETE;
9237 switch (ctsio->cdb[0]) {
9239 struct scsi_verify_10 *cdb;
9241 cdb = (struct scsi_verify_10 *)ctsio->cdb;
9242 if (cdb->byte2 & SVFY_BYTCHK)
9244 if (cdb->byte2 & SVFY_DPO)
9245 flags |= CTL_LLF_DPO;
9246 lba = scsi_4btoul(cdb->addr);
9247 num_blocks = scsi_2btoul(cdb->length);
9251 struct scsi_verify_12 *cdb;
9253 cdb = (struct scsi_verify_12 *)ctsio->cdb;
9254 if (cdb->byte2 & SVFY_BYTCHK)
9256 if (cdb->byte2 & SVFY_DPO)
9257 flags |= CTL_LLF_DPO;
9258 lba = scsi_4btoul(cdb->addr);
9259 num_blocks = scsi_4btoul(cdb->length);
9263 struct scsi_rw_16 *cdb;
9265 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9266 if (cdb->byte2 & SVFY_BYTCHK)
9268 if (cdb->byte2 & SVFY_DPO)
9269 flags |= CTL_LLF_DPO;
9270 lba = scsi_8btou64(cdb->addr);
9271 num_blocks = scsi_4btoul(cdb->length);
9276 * We got a command we don't support. This shouldn't
9277 * happen, commands should be filtered out above us.
9279 ctl_set_invalid_opcode(ctsio);
9280 ctl_done((union ctl_io *)ctsio);
9281 return (CTL_RETVAL_COMPLETE);
9285 * The first check is to make sure we're in bounds, the second
9286 * check is to catch wrap-around problems. If the lba + num blocks
9287 * is less than the lba, then we've wrapped around and the block
9288 * range is invalid anyway.
9290 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9291 || ((lba + num_blocks) < lba)) {
9292 ctl_set_lba_out_of_range(ctsio);
9293 ctl_done((union ctl_io *)ctsio);
9294 return (CTL_RETVAL_COMPLETE);
9298 * According to SBC-3, a transfer length of 0 is not an error.
9300 if (num_blocks == 0) {
9301 ctl_set_success(ctsio);
9302 ctl_done((union ctl_io *)ctsio);
9303 return (CTL_RETVAL_COMPLETE);
9306 lbalen = (struct ctl_lba_len_flags *)
9307 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9309 lbalen->len = num_blocks;
9311 lbalen->flags = CTL_LLF_COMPARE | flags;
9312 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9314 lbalen->flags = CTL_LLF_VERIFY | flags;
9315 ctsio->kern_total_len = 0;
9317 ctsio->kern_rel_offset = 0;
9319 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9320 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9325 ctl_report_luns(struct ctl_scsiio *ctsio)
9327 struct scsi_report_luns *cdb;
9328 struct scsi_report_luns_data *lun_data;
9329 struct ctl_lun *lun, *request_lun;
9330 int num_luns, retval;
9331 uint32_t alloc_len, lun_datalen;
9332 int num_filled, well_known;
9333 uint32_t initidx, targ_lun_id, lun_id;
9335 retval = CTL_RETVAL_COMPLETE;
9338 cdb = (struct scsi_report_luns *)ctsio->cdb;
9340 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9342 mtx_lock(&control_softc->ctl_lock);
9343 num_luns = control_softc->num_luns;
9344 mtx_unlock(&control_softc->ctl_lock);
9346 switch (cdb->select_report) {
9347 case RPL_REPORT_DEFAULT:
9348 case RPL_REPORT_ALL:
9350 case RPL_REPORT_WELLKNOWN:
9355 ctl_set_invalid_field(ctsio,
9361 ctl_done((union ctl_io *)ctsio);
9363 break; /* NOTREACHED */
9366 alloc_len = scsi_4btoul(cdb->length);
9368 * The initiator has to allocate at least 16 bytes for this request,
9369 * so he can at least get the header and the first LUN. Otherwise
9370 * we reject the request (per SPC-3 rev 14, section 6.21).
9372 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9373 sizeof(struct scsi_report_luns_lundata))) {
9374 ctl_set_invalid_field(ctsio,
9380 ctl_done((union ctl_io *)ctsio);
9384 request_lun = (struct ctl_lun *)
9385 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9387 lun_datalen = sizeof(*lun_data) +
9388 (num_luns * sizeof(struct scsi_report_luns_lundata));
9390 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9391 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9392 ctsio->kern_sg_entries = 0;
9394 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9396 mtx_lock(&control_softc->ctl_lock);
9397 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9398 lun_id = ctl_map_lun(ctsio->io_hdr.nexus.targ_port, targ_lun_id);
9399 if (lun_id >= CTL_MAX_LUNS)
9401 lun = control_softc->ctl_luns[lun_id];
9405 if (targ_lun_id <= 0xff) {
9407 * Peripheral addressing method, bus number 0.
9409 lun_data->luns[num_filled].lundata[0] =
9410 RPL_LUNDATA_ATYP_PERIPH;
9411 lun_data->luns[num_filled].lundata[1] = targ_lun_id;
9413 } else if (targ_lun_id <= 0x3fff) {
9415 * Flat addressing method.
9417 lun_data->luns[num_filled].lundata[0] =
9418 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8);
9419 lun_data->luns[num_filled].lundata[1] =
9420 (targ_lun_id & 0xff);
9422 } else if (targ_lun_id <= 0xffffff) {
9424 * Extended flat addressing method.
9426 lun_data->luns[num_filled].lundata[0] =
9427 RPL_LUNDATA_ATYP_EXTLUN | 0x12;
9428 scsi_ulto3b(targ_lun_id,
9429 &lun_data->luns[num_filled].lundata[1]);
9432 printf("ctl_report_luns: bogus LUN number %jd, "
9433 "skipping\n", (intmax_t)targ_lun_id);
9436 * According to SPC-3, rev 14 section 6.21:
9438 * "The execution of a REPORT LUNS command to any valid and
9439 * installed logical unit shall clear the REPORTED LUNS DATA
9440 * HAS CHANGED unit attention condition for all logical
9441 * units of that target with respect to the requesting
9442 * initiator. A valid and installed logical unit is one
9443 * having a PERIPHERAL QUALIFIER of 000b in the standard
9444 * INQUIRY data (see 6.4.2)."
9446 * If request_lun is NULL, the LUN this report luns command
9447 * was issued to is either disabled or doesn't exist. In that
9448 * case, we shouldn't clear any pending lun change unit
9451 if (request_lun != NULL) {
9452 mtx_lock(&lun->lun_lock);
9453 lun->pending_ua[initidx] &= ~CTL_UA_LUN_CHANGE;
9454 mtx_unlock(&lun->lun_lock);
9457 mtx_unlock(&control_softc->ctl_lock);
9460 * It's quite possible that we've returned fewer LUNs than we allocated
9461 * space for. Trim it.
9463 lun_datalen = sizeof(*lun_data) +
9464 (num_filled * sizeof(struct scsi_report_luns_lundata));
9466 if (lun_datalen < alloc_len) {
9467 ctsio->residual = alloc_len - lun_datalen;
9468 ctsio->kern_data_len = lun_datalen;
9469 ctsio->kern_total_len = lun_datalen;
9471 ctsio->residual = 0;
9472 ctsio->kern_data_len = alloc_len;
9473 ctsio->kern_total_len = alloc_len;
9475 ctsio->kern_data_resid = 0;
9476 ctsio->kern_rel_offset = 0;
9477 ctsio->kern_sg_entries = 0;
9480 * We set this to the actual data length, regardless of how much
9481 * space we actually have to return results. If the user looks at
9482 * this value, he'll know whether or not he allocated enough space
9483 * and reissue the command if necessary. We don't support well
9484 * known logical units, so if the user asks for that, return none.
9486 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9489 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9492 ctsio->scsi_status = SCSI_STATUS_OK;
9494 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9495 ctsio->be_move_done = ctl_config_move_done;
9496 ctl_datamove((union ctl_io *)ctsio);
9502 ctl_request_sense(struct ctl_scsiio *ctsio)
9504 struct scsi_request_sense *cdb;
9505 struct scsi_sense_data *sense_ptr;
9506 struct ctl_lun *lun;
9509 scsi_sense_data_type sense_format;
9511 cdb = (struct scsi_request_sense *)ctsio->cdb;
9513 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9515 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9518 * Determine which sense format the user wants.
9520 if (cdb->byte2 & SRS_DESC)
9521 sense_format = SSD_TYPE_DESC;
9523 sense_format = SSD_TYPE_FIXED;
9525 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9526 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9527 ctsio->kern_sg_entries = 0;
9530 * struct scsi_sense_data, which is currently set to 256 bytes, is
9531 * larger than the largest allowed value for the length field in the
9532 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9534 ctsio->residual = 0;
9535 ctsio->kern_data_len = cdb->length;
9536 ctsio->kern_total_len = cdb->length;
9538 ctsio->kern_data_resid = 0;
9539 ctsio->kern_rel_offset = 0;
9540 ctsio->kern_sg_entries = 0;
9543 * If we don't have a LUN, we don't have any pending sense.
9549 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9551 * Check for pending sense, and then for pending unit attentions.
9552 * Pending sense gets returned first, then pending unit attentions.
9554 mtx_lock(&lun->lun_lock);
9556 if (ctl_is_set(lun->have_ca, initidx)) {
9557 scsi_sense_data_type stored_format;
9560 * Check to see which sense format was used for the stored
9563 stored_format = scsi_sense_type(&lun->pending_sense[initidx]);
9566 * If the user requested a different sense format than the
9567 * one we stored, then we need to convert it to the other
9568 * format. If we're going from descriptor to fixed format
9569 * sense data, we may lose things in translation, depending
9570 * on what options were used.
9572 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9573 * for some reason we'll just copy it out as-is.
9575 if ((stored_format == SSD_TYPE_FIXED)
9576 && (sense_format == SSD_TYPE_DESC))
9577 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9578 &lun->pending_sense[initidx],
9579 (struct scsi_sense_data_desc *)sense_ptr);
9580 else if ((stored_format == SSD_TYPE_DESC)
9581 && (sense_format == SSD_TYPE_FIXED))
9582 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9583 &lun->pending_sense[initidx],
9584 (struct scsi_sense_data_fixed *)sense_ptr);
9586 memcpy(sense_ptr, &lun->pending_sense[initidx],
9587 ctl_min(sizeof(*sense_ptr),
9588 sizeof(lun->pending_sense[initidx])));
9590 ctl_clear_mask(lun->have_ca, initidx);
9594 if (lun->pending_ua[initidx] != CTL_UA_NONE) {
9595 ctl_ua_type ua_type;
9597 ua_type = ctl_build_ua(&lun->pending_ua[initidx],
9598 sense_ptr, sense_format);
9599 if (ua_type != CTL_UA_NONE)
9602 mtx_unlock(&lun->lun_lock);
9605 * We already have a pending error, return it.
9607 if (have_error != 0) {
9609 * We report the SCSI status as OK, since the status of the
9610 * request sense command itself is OK.
9612 ctsio->scsi_status = SCSI_STATUS_OK;
9615 * We report 0 for the sense length, because we aren't doing
9616 * autosense in this case. We're reporting sense as
9619 ctsio->sense_len = 0;
9620 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9621 ctsio->be_move_done = ctl_config_move_done;
9622 ctl_datamove((union ctl_io *)ctsio);
9624 return (CTL_RETVAL_COMPLETE);
9630 * No sense information to report, so we report that everything is
9633 ctl_set_sense_data(sense_ptr,
9636 /*current_error*/ 1,
9637 /*sense_key*/ SSD_KEY_NO_SENSE,
9642 ctsio->scsi_status = SCSI_STATUS_OK;
9645 * We report 0 for the sense length, because we aren't doing
9646 * autosense in this case. We're reporting sense as parameter data.
9648 ctsio->sense_len = 0;
9649 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9650 ctsio->be_move_done = ctl_config_move_done;
9651 ctl_datamove((union ctl_io *)ctsio);
9653 return (CTL_RETVAL_COMPLETE);
9657 ctl_tur(struct ctl_scsiio *ctsio)
9659 struct ctl_lun *lun;
9661 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9663 CTL_DEBUG_PRINT(("ctl_tur\n"));
9668 ctsio->scsi_status = SCSI_STATUS_OK;
9669 ctsio->io_hdr.status = CTL_SUCCESS;
9671 ctl_done((union ctl_io *)ctsio);
9673 return (CTL_RETVAL_COMPLETE);
9678 ctl_cmddt_inquiry(struct ctl_scsiio *ctsio)
9685 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9687 struct scsi_vpd_supported_pages *pages;
9689 struct ctl_lun *lun;
9691 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9693 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9694 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9695 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9696 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9697 ctsio->kern_sg_entries = 0;
9699 if (sup_page_size < alloc_len) {
9700 ctsio->residual = alloc_len - sup_page_size;
9701 ctsio->kern_data_len = sup_page_size;
9702 ctsio->kern_total_len = sup_page_size;
9704 ctsio->residual = 0;
9705 ctsio->kern_data_len = alloc_len;
9706 ctsio->kern_total_len = alloc_len;
9708 ctsio->kern_data_resid = 0;
9709 ctsio->kern_rel_offset = 0;
9710 ctsio->kern_sg_entries = 0;
9713 * The control device is always connected. The disk device, on the
9714 * other hand, may not be online all the time. Need to change this
9715 * to figure out whether the disk device is actually online or not.
9718 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9719 lun->be_lun->lun_type;
9721 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9723 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES;
9724 /* Supported VPD pages */
9725 pages->page_list[0] = SVPD_SUPPORTED_PAGES;
9727 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER;
9728 /* Device Identification */
9729 pages->page_list[2] = SVPD_DEVICE_ID;
9730 /* Extended INQUIRY Data */
9731 pages->page_list[3] = SVPD_EXTENDED_INQUIRY_DATA;
9732 /* Mode Page Policy */
9733 pages->page_list[4] = SVPD_MODE_PAGE_POLICY;
9735 pages->page_list[5] = SVPD_SCSI_PORTS;
9736 /* Third-party Copy */
9737 pages->page_list[6] = SVPD_SCSI_TPC;
9739 pages->page_list[7] = SVPD_BLOCK_LIMITS;
9740 /* Block Device Characteristics */
9741 pages->page_list[8] = SVPD_BDC;
9742 /* Logical Block Provisioning */
9743 pages->page_list[9] = SVPD_LBP;
9745 ctsio->scsi_status = SCSI_STATUS_OK;
9747 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9748 ctsio->be_move_done = ctl_config_move_done;
9749 ctl_datamove((union ctl_io *)ctsio);
9751 return (CTL_RETVAL_COMPLETE);
9755 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9757 struct scsi_vpd_unit_serial_number *sn_ptr;
9758 struct ctl_lun *lun;
9761 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9763 data_len = 4 + CTL_SN_LEN;
9764 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9765 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9766 if (data_len < alloc_len) {
9767 ctsio->residual = alloc_len - data_len;
9768 ctsio->kern_data_len = data_len;
9769 ctsio->kern_total_len = data_len;
9771 ctsio->residual = 0;
9772 ctsio->kern_data_len = alloc_len;
9773 ctsio->kern_total_len = alloc_len;
9775 ctsio->kern_data_resid = 0;
9776 ctsio->kern_rel_offset = 0;
9777 ctsio->kern_sg_entries = 0;
9780 * The control device is always connected. The disk device, on the
9781 * other hand, may not be online all the time. Need to change this
9782 * to figure out whether the disk device is actually online or not.
9785 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9786 lun->be_lun->lun_type;
9788 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9790 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9791 sn_ptr->length = CTL_SN_LEN;
9793 * If we don't have a LUN, we just leave the serial number as
9797 strncpy((char *)sn_ptr->serial_num,
9798 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9800 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9801 ctsio->scsi_status = SCSI_STATUS_OK;
9803 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9804 ctsio->be_move_done = ctl_config_move_done;
9805 ctl_datamove((union ctl_io *)ctsio);
9807 return (CTL_RETVAL_COMPLETE);
9812 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9814 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9815 struct ctl_lun *lun;
9818 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9820 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9821 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9822 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9823 ctsio->kern_sg_entries = 0;
9825 if (data_len < alloc_len) {
9826 ctsio->residual = alloc_len - data_len;
9827 ctsio->kern_data_len = data_len;
9828 ctsio->kern_total_len = data_len;
9830 ctsio->residual = 0;
9831 ctsio->kern_data_len = alloc_len;
9832 ctsio->kern_total_len = alloc_len;
9834 ctsio->kern_data_resid = 0;
9835 ctsio->kern_rel_offset = 0;
9836 ctsio->kern_sg_entries = 0;
9839 * The control device is always connected. The disk device, on the
9840 * other hand, may not be online all the time.
9843 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9844 lun->be_lun->lun_type;
9846 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9847 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9848 eid_ptr->page_length = data_len - 4;
9849 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9850 eid_ptr->flags3 = SVPD_EID_V_SUP;
9852 ctsio->scsi_status = SCSI_STATUS_OK;
9853 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9854 ctsio->be_move_done = ctl_config_move_done;
9855 ctl_datamove((union ctl_io *)ctsio);
9857 return (CTL_RETVAL_COMPLETE);
9861 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9863 struct scsi_vpd_mode_page_policy *mpp_ptr;
9864 struct ctl_lun *lun;
9867 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9869 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9870 sizeof(struct scsi_vpd_mode_page_policy_descr);
9872 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9873 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9874 ctsio->kern_sg_entries = 0;
9876 if (data_len < alloc_len) {
9877 ctsio->residual = alloc_len - data_len;
9878 ctsio->kern_data_len = data_len;
9879 ctsio->kern_total_len = data_len;
9881 ctsio->residual = 0;
9882 ctsio->kern_data_len = alloc_len;
9883 ctsio->kern_total_len = alloc_len;
9885 ctsio->kern_data_resid = 0;
9886 ctsio->kern_rel_offset = 0;
9887 ctsio->kern_sg_entries = 0;
9890 * The control device is always connected. The disk device, on the
9891 * other hand, may not be online all the time.
9894 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9895 lun->be_lun->lun_type;
9897 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9898 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9899 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9900 mpp_ptr->descr[0].page_code = 0x3f;
9901 mpp_ptr->descr[0].subpage_code = 0xff;
9902 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9904 ctsio->scsi_status = SCSI_STATUS_OK;
9905 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9906 ctsio->be_move_done = ctl_config_move_done;
9907 ctl_datamove((union ctl_io *)ctsio);
9909 return (CTL_RETVAL_COMPLETE);
9913 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9915 struct scsi_vpd_device_id *devid_ptr;
9916 struct scsi_vpd_id_descriptor *desc;
9917 struct ctl_softc *ctl_softc;
9918 struct ctl_lun *lun;
9919 struct ctl_port *port;
9923 ctl_softc = control_softc;
9925 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)];
9926 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9928 data_len = sizeof(struct scsi_vpd_device_id) +
9929 sizeof(struct scsi_vpd_id_descriptor) +
9930 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9931 sizeof(struct scsi_vpd_id_descriptor) +
9932 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9933 if (lun && lun->lun_devid)
9934 data_len += lun->lun_devid->len;
9935 if (port->port_devid)
9936 data_len += port->port_devid->len;
9937 if (port->target_devid)
9938 data_len += port->target_devid->len;
9940 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9941 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9942 ctsio->kern_sg_entries = 0;
9944 if (data_len < alloc_len) {
9945 ctsio->residual = alloc_len - data_len;
9946 ctsio->kern_data_len = data_len;
9947 ctsio->kern_total_len = data_len;
9949 ctsio->residual = 0;
9950 ctsio->kern_data_len = alloc_len;
9951 ctsio->kern_total_len = alloc_len;
9953 ctsio->kern_data_resid = 0;
9954 ctsio->kern_rel_offset = 0;
9955 ctsio->kern_sg_entries = 0;
9958 * The control device is always connected. The disk device, on the
9959 * other hand, may not be online all the time.
9962 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9963 lun->be_lun->lun_type;
9965 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9966 devid_ptr->page_code = SVPD_DEVICE_ID;
9967 scsi_ulto2b(data_len - 4, devid_ptr->length);
9969 if (port->port_type == CTL_PORT_FC)
9970 proto = SCSI_PROTO_FC << 4;
9971 else if (port->port_type == CTL_PORT_ISCSI)
9972 proto = SCSI_PROTO_ISCSI << 4;
9974 proto = SCSI_PROTO_SPI << 4;
9975 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9978 * We're using a LUN association here. i.e., this device ID is a
9979 * per-LUN identifier.
9981 if (lun && lun->lun_devid) {
9982 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9983 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9984 lun->lun_devid->len);
9988 * This is for the WWPN which is a port association.
9990 if (port->port_devid) {
9991 memcpy(desc, port->port_devid->data, port->port_devid->len);
9992 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9993 port->port_devid->len);
9997 * This is for the Relative Target Port(type 4h) identifier
9999 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
10000 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
10001 SVPD_ID_TYPE_RELTARG;
10003 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
10004 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
10005 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
10008 * This is for the Target Port Group(type 5h) identifier
10010 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
10011 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
10012 SVPD_ID_TYPE_TPORTGRP;
10014 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1,
10015 &desc->identifier[2]);
10016 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
10017 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
10020 * This is for the Target identifier
10022 if (port->target_devid) {
10023 memcpy(desc, port->target_devid->data, port->target_devid->len);
10026 ctsio->scsi_status = SCSI_STATUS_OK;
10027 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10028 ctsio->be_move_done = ctl_config_move_done;
10029 ctl_datamove((union ctl_io *)ctsio);
10031 return (CTL_RETVAL_COMPLETE);
10035 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
10037 struct ctl_softc *softc = control_softc;
10038 struct scsi_vpd_scsi_ports *sp;
10039 struct scsi_vpd_port_designation *pd;
10040 struct scsi_vpd_port_designation_cont *pdc;
10041 struct ctl_lun *lun;
10042 struct ctl_port *port;
10043 int data_len, num_target_ports, iid_len, id_len, g, pg, p;
10044 int num_target_port_groups, single;
10046 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10048 single = ctl_is_single;
10050 num_target_port_groups = 1;
10052 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
10053 num_target_ports = 0;
10056 mtx_lock(&softc->ctl_lock);
10057 STAILQ_FOREACH(port, &softc->port_list, links) {
10058 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
10061 ctl_map_lun_back(port->targ_port, lun->lun) >=
10064 num_target_ports++;
10065 if (port->init_devid)
10066 iid_len += port->init_devid->len;
10067 if (port->port_devid)
10068 id_len += port->port_devid->len;
10070 mtx_unlock(&softc->ctl_lock);
10072 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups *
10073 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
10074 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
10075 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10076 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
10077 ctsio->kern_sg_entries = 0;
10079 if (data_len < alloc_len) {
10080 ctsio->residual = alloc_len - data_len;
10081 ctsio->kern_data_len = data_len;
10082 ctsio->kern_total_len = data_len;
10084 ctsio->residual = 0;
10085 ctsio->kern_data_len = alloc_len;
10086 ctsio->kern_total_len = alloc_len;
10088 ctsio->kern_data_resid = 0;
10089 ctsio->kern_rel_offset = 0;
10090 ctsio->kern_sg_entries = 0;
10093 * The control device is always connected. The disk device, on the
10094 * other hand, may not be online all the time. Need to change this
10095 * to figure out whether the disk device is actually online or not.
10098 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
10099 lun->be_lun->lun_type;
10101 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10103 sp->page_code = SVPD_SCSI_PORTS;
10104 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
10106 pd = &sp->design[0];
10108 mtx_lock(&softc->ctl_lock);
10109 if (softc->flags & CTL_FLAG_MASTER_SHELF)
10113 for (g = 0; g < num_target_port_groups; g++) {
10114 STAILQ_FOREACH(port, &softc->port_list, links) {
10115 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
10118 ctl_map_lun_back(port->targ_port, lun->lun) >=
10121 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS;
10122 scsi_ulto2b(p, pd->relative_port_id);
10123 if (port->init_devid && g == pg) {
10124 iid_len = port->init_devid->len;
10125 memcpy(pd->initiator_transportid,
10126 port->init_devid->data, port->init_devid->len);
10129 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
10130 pdc = (struct scsi_vpd_port_designation_cont *)
10131 (&pd->initiator_transportid[iid_len]);
10132 if (port->port_devid && g == pg) {
10133 id_len = port->port_devid->len;
10134 memcpy(pdc->target_port_descriptors,
10135 port->port_devid->data, port->port_devid->len);
10138 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
10139 pd = (struct scsi_vpd_port_designation *)
10140 ((uint8_t *)pdc->target_port_descriptors + id_len);
10143 mtx_unlock(&softc->ctl_lock);
10145 ctsio->scsi_status = SCSI_STATUS_OK;
10146 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10147 ctsio->be_move_done = ctl_config_move_done;
10148 ctl_datamove((union ctl_io *)ctsio);
10150 return (CTL_RETVAL_COMPLETE);
10154 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
10156 struct scsi_vpd_block_limits *bl_ptr;
10157 struct ctl_lun *lun;
10160 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10162 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
10163 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
10164 ctsio->kern_sg_entries = 0;
10166 if (sizeof(*bl_ptr) < alloc_len) {
10167 ctsio->residual = alloc_len - sizeof(*bl_ptr);
10168 ctsio->kern_data_len = sizeof(*bl_ptr);
10169 ctsio->kern_total_len = sizeof(*bl_ptr);
10171 ctsio->residual = 0;
10172 ctsio->kern_data_len = alloc_len;
10173 ctsio->kern_total_len = alloc_len;
10175 ctsio->kern_data_resid = 0;
10176 ctsio->kern_rel_offset = 0;
10177 ctsio->kern_sg_entries = 0;
10180 * The control device is always connected. The disk device, on the
10181 * other hand, may not be online all the time. Need to change this
10182 * to figure out whether the disk device is actually online or not.
10185 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10186 lun->be_lun->lun_type;
10188 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10190 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
10191 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
10192 bl_ptr->max_cmp_write_len = 0xff;
10193 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
10195 bs = lun->be_lun->blocksize;
10196 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len);
10197 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10198 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
10199 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
10200 if (lun->be_lun->pblockexp != 0) {
10201 scsi_ulto4b((1 << lun->be_lun->pblockexp),
10202 bl_ptr->opt_unmap_grain);
10203 scsi_ulto4b(0x80000000 | lun->be_lun->pblockoff,
10204 bl_ptr->unmap_grain_align);
10207 scsi_ulto4b(lun->be_lun->atomicblock,
10208 bl_ptr->max_atomic_transfer_length);
10209 scsi_ulto4b(0, bl_ptr->atomic_alignment);
10210 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
10212 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
10214 ctsio->scsi_status = SCSI_STATUS_OK;
10215 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10216 ctsio->be_move_done = ctl_config_move_done;
10217 ctl_datamove((union ctl_io *)ctsio);
10219 return (CTL_RETVAL_COMPLETE);
10223 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
10225 struct scsi_vpd_block_device_characteristics *bdc_ptr;
10226 struct ctl_lun *lun;
10230 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10232 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
10233 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
10234 ctsio->kern_sg_entries = 0;
10236 if (sizeof(*bdc_ptr) < alloc_len) {
10237 ctsio->residual = alloc_len - sizeof(*bdc_ptr);
10238 ctsio->kern_data_len = sizeof(*bdc_ptr);
10239 ctsio->kern_total_len = sizeof(*bdc_ptr);
10241 ctsio->residual = 0;
10242 ctsio->kern_data_len = alloc_len;
10243 ctsio->kern_total_len = alloc_len;
10245 ctsio->kern_data_resid = 0;
10246 ctsio->kern_rel_offset = 0;
10247 ctsio->kern_sg_entries = 0;
10250 * The control device is always connected. The disk device, on the
10251 * other hand, may not be online all the time. Need to change this
10252 * to figure out whether the disk device is actually online or not.
10255 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10256 lun->be_lun->lun_type;
10258 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10259 bdc_ptr->page_code = SVPD_BDC;
10260 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
10262 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL)
10263 i = strtol(value, NULL, 0);
10265 i = SVPD_NON_ROTATING;
10266 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
10268 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL)
10269 i = strtol(value, NULL, 0);
10272 bdc_ptr->wab_wac_ff = (i & 0x0f);
10273 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
10275 ctsio->scsi_status = SCSI_STATUS_OK;
10276 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10277 ctsio->be_move_done = ctl_config_move_done;
10278 ctl_datamove((union ctl_io *)ctsio);
10280 return (CTL_RETVAL_COMPLETE);
10284 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
10286 struct scsi_vpd_logical_block_prov *lbp_ptr;
10287 struct ctl_lun *lun;
10289 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10291 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
10292 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
10293 ctsio->kern_sg_entries = 0;
10295 if (sizeof(*lbp_ptr) < alloc_len) {
10296 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
10297 ctsio->kern_data_len = sizeof(*lbp_ptr);
10298 ctsio->kern_total_len = sizeof(*lbp_ptr);
10300 ctsio->residual = 0;
10301 ctsio->kern_data_len = alloc_len;
10302 ctsio->kern_total_len = alloc_len;
10304 ctsio->kern_data_resid = 0;
10305 ctsio->kern_rel_offset = 0;
10306 ctsio->kern_sg_entries = 0;
10309 * The control device is always connected. The disk device, on the
10310 * other hand, may not be online all the time. Need to change this
10311 * to figure out whether the disk device is actually online or not.
10314 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10315 lun->be_lun->lun_type;
10317 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10319 lbp_ptr->page_code = SVPD_LBP;
10320 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
10321 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10322 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
10323 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
10324 lbp_ptr->prov_type = SVPD_LBP_RESOURCE;
10327 ctsio->scsi_status = SCSI_STATUS_OK;
10328 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10329 ctsio->be_move_done = ctl_config_move_done;
10330 ctl_datamove((union ctl_io *)ctsio);
10332 return (CTL_RETVAL_COMPLETE);
10336 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
10338 struct scsi_inquiry *cdb;
10339 struct ctl_lun *lun;
10340 int alloc_len, retval;
10342 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10343 cdb = (struct scsi_inquiry *)ctsio->cdb;
10345 retval = CTL_RETVAL_COMPLETE;
10347 alloc_len = scsi_2btoul(cdb->length);
10349 switch (cdb->page_code) {
10350 case SVPD_SUPPORTED_PAGES:
10351 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
10353 case SVPD_UNIT_SERIAL_NUMBER:
10354 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
10356 case SVPD_DEVICE_ID:
10357 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
10359 case SVPD_EXTENDED_INQUIRY_DATA:
10360 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
10362 case SVPD_MODE_PAGE_POLICY:
10363 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
10365 case SVPD_SCSI_PORTS:
10366 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10368 case SVPD_SCSI_TPC:
10369 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10371 case SVPD_BLOCK_LIMITS:
10372 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10375 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10378 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10381 ctl_set_invalid_field(ctsio,
10387 ctl_done((union ctl_io *)ctsio);
10388 retval = CTL_RETVAL_COMPLETE;
10396 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10398 struct scsi_inquiry_data *inq_ptr;
10399 struct scsi_inquiry *cdb;
10400 struct ctl_softc *ctl_softc;
10401 struct ctl_lun *lun;
10403 uint32_t alloc_len, data_len;
10404 ctl_port_type port_type;
10406 ctl_softc = control_softc;
10409 * Figure out whether we're talking to a Fibre Channel port or not.
10410 * We treat the ioctl front end, and any SCSI adapters, as packetized
10413 port_type = ctl_softc->ctl_ports[
10414 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type;
10415 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10416 port_type = CTL_PORT_SCSI;
10418 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10419 cdb = (struct scsi_inquiry *)ctsio->cdb;
10420 alloc_len = scsi_2btoul(cdb->length);
10423 * We malloc the full inquiry data size here and fill it
10424 * in. If the user only asks for less, we'll give him
10427 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10428 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10429 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10430 ctsio->kern_sg_entries = 0;
10431 ctsio->kern_data_resid = 0;
10432 ctsio->kern_rel_offset = 0;
10434 if (data_len < alloc_len) {
10435 ctsio->residual = alloc_len - data_len;
10436 ctsio->kern_data_len = data_len;
10437 ctsio->kern_total_len = data_len;
10439 ctsio->residual = 0;
10440 ctsio->kern_data_len = alloc_len;
10441 ctsio->kern_total_len = alloc_len;
10445 * If we have a LUN configured, report it as connected. Otherwise,
10446 * report that it is offline or no device is supported, depending
10447 * on the value of inquiry_pq_no_lun.
10449 * According to the spec (SPC-4 r34), the peripheral qualifier
10450 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario:
10452 * "A peripheral device having the specified peripheral device type
10453 * is not connected to this logical unit. However, the device
10454 * server is capable of supporting the specified peripheral device
10455 * type on this logical unit."
10457 * According to the same spec, the peripheral qualifier
10458 * SID_QUAL_BAD_LU (011b) is used in this scenario:
10460 * "The device server is not capable of supporting a peripheral
10461 * device on this logical unit. For this peripheral qualifier the
10462 * peripheral device type shall be set to 1Fh. All other peripheral
10463 * device type values are reserved for this peripheral qualifier."
10465 * Given the text, it would seem that we probably want to report that
10466 * the LUN is offline here. There is no LUN connected, but we can
10467 * support a LUN at the given LUN number.
10469 * In the real world, though, it sounds like things are a little
10472 * - Linux, when presented with a LUN with the offline peripheral
10473 * qualifier, will create an sg driver instance for it. So when
10474 * you attach it to CTL, you wind up with a ton of sg driver
10475 * instances. (One for every LUN that Linux bothered to probe.)
10476 * Linux does this despite the fact that it issues a REPORT LUNs
10477 * to LUN 0 to get the inventory of supported LUNs.
10479 * - There is other anecdotal evidence (from Emulex folks) about
10480 * arrays that use the offline peripheral qualifier for LUNs that
10481 * are on the "passive" path in an active/passive array.
10483 * So the solution is provide a hopefully reasonable default
10484 * (return bad/no LUN) and allow the user to change the behavior
10485 * with a tunable/sysctl variable.
10488 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10489 lun->be_lun->lun_type;
10490 else if (ctl_softc->inquiry_pq_no_lun == 0)
10491 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10493 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10495 /* RMB in byte 2 is 0 */
10496 inq_ptr->version = SCSI_REV_SPC4;
10499 * According to SAM-3, even if a device only supports a single
10500 * level of LUN addressing, it should still set the HISUP bit:
10502 * 4.9.1 Logical unit numbers overview
10504 * All logical unit number formats described in this standard are
10505 * hierarchical in structure even when only a single level in that
10506 * hierarchy is used. The HISUP bit shall be set to one in the
10507 * standard INQUIRY data (see SPC-2) when any logical unit number
10508 * format described in this standard is used. Non-hierarchical
10509 * formats are outside the scope of this standard.
10511 * Therefore we set the HiSup bit here.
10513 * The reponse format is 2, per SPC-3.
10515 inq_ptr->response_format = SID_HiSup | 2;
10517 inq_ptr->additional_length = data_len -
10518 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10519 CTL_DEBUG_PRINT(("additional_length = %d\n",
10520 inq_ptr->additional_length));
10522 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10523 /* 16 bit addressing */
10524 if (port_type == CTL_PORT_SCSI)
10525 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10526 /* XXX set the SID_MultiP bit here if we're actually going to
10527 respond on multiple ports */
10528 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10530 /* 16 bit data bus, synchronous transfers */
10531 if (port_type == CTL_PORT_SCSI)
10532 inq_ptr->flags = SID_WBus16 | SID_Sync;
10534 * XXX KDM do we want to support tagged queueing on the control
10538 || (lun->be_lun->lun_type != T_PROCESSOR))
10539 inq_ptr->flags |= SID_CmdQue;
10541 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10542 * We have 8 bytes for the vendor name, and 16 bytes for the device
10543 * name and 4 bytes for the revision.
10545 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10546 "vendor")) == NULL) {
10547 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10549 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10550 strncpy(inq_ptr->vendor, val,
10551 min(sizeof(inq_ptr->vendor), strlen(val)));
10554 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10555 sizeof(inq_ptr->product));
10556 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10557 switch (lun->be_lun->lun_type) {
10559 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10560 sizeof(inq_ptr->product));
10563 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10564 sizeof(inq_ptr->product));
10567 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10568 sizeof(inq_ptr->product));
10572 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10573 strncpy(inq_ptr->product, val,
10574 min(sizeof(inq_ptr->product), strlen(val)));
10578 * XXX make this a macro somewhere so it automatically gets
10579 * incremented when we make changes.
10581 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10582 "revision")) == NULL) {
10583 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10585 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10586 strncpy(inq_ptr->revision, val,
10587 min(sizeof(inq_ptr->revision), strlen(val)));
10591 * For parallel SCSI, we support double transition and single
10592 * transition clocking. We also support QAS (Quick Arbitration
10593 * and Selection) and Information Unit transfers on both the
10594 * control and array devices.
10596 if (port_type == CTL_PORT_SCSI)
10597 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10600 /* SAM-5 (no version claimed) */
10601 scsi_ulto2b(0x00A0, inq_ptr->version1);
10602 /* SPC-4 (no version claimed) */
10603 scsi_ulto2b(0x0460, inq_ptr->version2);
10604 if (port_type == CTL_PORT_FC) {
10605 /* FCP-2 ANSI INCITS.350:2003 */
10606 scsi_ulto2b(0x0917, inq_ptr->version3);
10607 } else if (port_type == CTL_PORT_SCSI) {
10608 /* SPI-4 ANSI INCITS.362:200x */
10609 scsi_ulto2b(0x0B56, inq_ptr->version3);
10610 } else if (port_type == CTL_PORT_ISCSI) {
10611 /* iSCSI (no version claimed) */
10612 scsi_ulto2b(0x0960, inq_ptr->version3);
10613 } else if (port_type == CTL_PORT_SAS) {
10614 /* SAS (no version claimed) */
10615 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10619 /* SBC-4 (no version claimed) */
10620 scsi_ulto2b(0x0600, inq_ptr->version4);
10622 switch (lun->be_lun->lun_type) {
10624 /* SBC-4 (no version claimed) */
10625 scsi_ulto2b(0x0600, inq_ptr->version4);
10633 ctsio->scsi_status = SCSI_STATUS_OK;
10634 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10635 ctsio->be_move_done = ctl_config_move_done;
10636 ctl_datamove((union ctl_io *)ctsio);
10637 return (CTL_RETVAL_COMPLETE);
10641 ctl_inquiry(struct ctl_scsiio *ctsio)
10643 struct scsi_inquiry *cdb;
10646 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10648 cdb = (struct scsi_inquiry *)ctsio->cdb;
10649 if (cdb->byte2 & SI_EVPD)
10650 retval = ctl_inquiry_evpd(ctsio);
10651 else if (cdb->page_code == 0)
10652 retval = ctl_inquiry_std(ctsio);
10654 ctl_set_invalid_field(ctsio,
10660 ctl_done((union ctl_io *)ctsio);
10661 return (CTL_RETVAL_COMPLETE);
10668 * For known CDB types, parse the LBA and length.
10671 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10673 if (io->io_hdr.io_type != CTL_IO_SCSI)
10676 switch (io->scsiio.cdb[0]) {
10677 case COMPARE_AND_WRITE: {
10678 struct scsi_compare_and_write *cdb;
10680 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10682 *lba = scsi_8btou64(cdb->addr);
10683 *len = cdb->length;
10688 struct scsi_rw_6 *cdb;
10690 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10692 *lba = scsi_3btoul(cdb->addr);
10693 /* only 5 bits are valid in the most significant address byte */
10695 *len = cdb->length;
10700 struct scsi_rw_10 *cdb;
10702 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10704 *lba = scsi_4btoul(cdb->addr);
10705 *len = scsi_2btoul(cdb->length);
10708 case WRITE_VERIFY_10: {
10709 struct scsi_write_verify_10 *cdb;
10711 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10713 *lba = scsi_4btoul(cdb->addr);
10714 *len = scsi_2btoul(cdb->length);
10719 struct scsi_rw_12 *cdb;
10721 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10723 *lba = scsi_4btoul(cdb->addr);
10724 *len = scsi_4btoul(cdb->length);
10727 case WRITE_VERIFY_12: {
10728 struct scsi_write_verify_12 *cdb;
10730 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10732 *lba = scsi_4btoul(cdb->addr);
10733 *len = scsi_4btoul(cdb->length);
10738 case WRITE_ATOMIC_16: {
10739 struct scsi_rw_16 *cdb;
10741 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10743 *lba = scsi_8btou64(cdb->addr);
10744 *len = scsi_4btoul(cdb->length);
10747 case WRITE_VERIFY_16: {
10748 struct scsi_write_verify_16 *cdb;
10750 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10752 *lba = scsi_8btou64(cdb->addr);
10753 *len = scsi_4btoul(cdb->length);
10756 case WRITE_SAME_10: {
10757 struct scsi_write_same_10 *cdb;
10759 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10761 *lba = scsi_4btoul(cdb->addr);
10762 *len = scsi_2btoul(cdb->length);
10765 case WRITE_SAME_16: {
10766 struct scsi_write_same_16 *cdb;
10768 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10770 *lba = scsi_8btou64(cdb->addr);
10771 *len = scsi_4btoul(cdb->length);
10775 struct scsi_verify_10 *cdb;
10777 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10779 *lba = scsi_4btoul(cdb->addr);
10780 *len = scsi_2btoul(cdb->length);
10784 struct scsi_verify_12 *cdb;
10786 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10788 *lba = scsi_4btoul(cdb->addr);
10789 *len = scsi_4btoul(cdb->length);
10793 struct scsi_verify_16 *cdb;
10795 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10797 *lba = scsi_8btou64(cdb->addr);
10798 *len = scsi_4btoul(cdb->length);
10808 break; /* NOTREACHED */
10815 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2)
10817 uint64_t endlba1, endlba2;
10819 endlba1 = lba1 + len1 - 1;
10820 endlba2 = lba2 + len2 - 1;
10822 if ((endlba1 < lba2)
10823 || (endlba2 < lba1))
10824 return (CTL_ACTION_PASS);
10826 return (CTL_ACTION_BLOCK);
10830 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10832 struct ctl_ptr_len_flags *ptrlen;
10833 struct scsi_unmap_desc *buf, *end, *range;
10837 /* If not UNMAP -- go other way. */
10838 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10839 io->scsiio.cdb[0] != UNMAP)
10840 return (CTL_ACTION_ERROR);
10842 /* If UNMAP without data -- block and wait for data. */
10843 ptrlen = (struct ctl_ptr_len_flags *)
10844 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10845 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10846 ptrlen->ptr == NULL)
10847 return (CTL_ACTION_BLOCK);
10849 /* UNMAP with data -- check for collision. */
10850 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10851 end = buf + ptrlen->len / sizeof(*buf);
10852 for (range = buf; range < end; range++) {
10853 lba = scsi_8btou64(range->lba);
10854 len = scsi_4btoul(range->length);
10855 if ((lba < lba2 + len2) && (lba + len > lba2))
10856 return (CTL_ACTION_BLOCK);
10858 return (CTL_ACTION_PASS);
10862 ctl_extent_check(union ctl_io *io1, union ctl_io *io2)
10864 uint64_t lba1, lba2;
10865 uint64_t len1, len2;
10868 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10869 return (CTL_ACTION_ERROR);
10871 retval = ctl_extent_check_unmap(io2, lba1, len1);
10872 if (retval != CTL_ACTION_ERROR)
10875 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10876 return (CTL_ACTION_ERROR);
10878 return (ctl_extent_check_lba(lba1, len1, lba2, len2));
10882 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10883 union ctl_io *ooa_io)
10885 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10886 ctl_serialize_action *serialize_row;
10889 * The initiator attempted multiple untagged commands at the same
10890 * time. Can't do that.
10892 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10893 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10894 && ((pending_io->io_hdr.nexus.targ_port ==
10895 ooa_io->io_hdr.nexus.targ_port)
10896 && (pending_io->io_hdr.nexus.initid.id ==
10897 ooa_io->io_hdr.nexus.initid.id))
10898 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10899 return (CTL_ACTION_OVERLAP);
10902 * The initiator attempted to send multiple tagged commands with
10903 * the same ID. (It's fine if different initiators have the same
10906 * Even if all of those conditions are true, we don't kill the I/O
10907 * if the command ahead of us has been aborted. We won't end up
10908 * sending it to the FETD, and it's perfectly legal to resend a
10909 * command with the same tag number as long as the previous
10910 * instance of this tag number has been aborted somehow.
10912 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10913 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10914 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10915 && ((pending_io->io_hdr.nexus.targ_port ==
10916 ooa_io->io_hdr.nexus.targ_port)
10917 && (pending_io->io_hdr.nexus.initid.id ==
10918 ooa_io->io_hdr.nexus.initid.id))
10919 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10920 return (CTL_ACTION_OVERLAP_TAG);
10923 * If we get a head of queue tag, SAM-3 says that we should
10924 * immediately execute it.
10926 * What happens if this command would normally block for some other
10927 * reason? e.g. a request sense with a head of queue tag
10928 * immediately after a write. Normally that would block, but this
10929 * will result in its getting executed immediately...
10931 * We currently return "pass" instead of "skip", so we'll end up
10932 * going through the rest of the queue to check for overlapped tags.
10934 * XXX KDM check for other types of blockage first??
10936 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10937 return (CTL_ACTION_PASS);
10940 * Ordered tags have to block until all items ahead of them
10941 * have completed. If we get called with an ordered tag, we always
10942 * block, if something else is ahead of us in the queue.
10944 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10945 return (CTL_ACTION_BLOCK);
10948 * Simple tags get blocked until all head of queue and ordered tags
10949 * ahead of them have completed. I'm lumping untagged commands in
10950 * with simple tags here. XXX KDM is that the right thing to do?
10952 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10953 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10954 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10955 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10956 return (CTL_ACTION_BLOCK);
10958 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
10959 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
10961 serialize_row = ctl_serialize_table[ooa_entry->seridx];
10963 switch (serialize_row[pending_entry->seridx]) {
10964 case CTL_SER_BLOCK:
10965 return (CTL_ACTION_BLOCK);
10966 case CTL_SER_EXTENT:
10967 return (ctl_extent_check(pending_io, ooa_io));
10968 case CTL_SER_EXTENTOPT:
10969 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
10970 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
10971 return (ctl_extent_check(pending_io, ooa_io));
10974 return (CTL_ACTION_PASS);
10975 case CTL_SER_BLOCKOPT:
10976 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
10977 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
10978 return (CTL_ACTION_BLOCK);
10979 return (CTL_ACTION_PASS);
10981 return (CTL_ACTION_SKIP);
10983 panic("invalid serialization value %d",
10984 serialize_row[pending_entry->seridx]);
10987 return (CTL_ACTION_ERROR);
10991 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
10993 * - pending_io is generally either incoming, or on the blocked queue
10994 * - starting I/O is the I/O we want to start the check with.
10997 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
10998 union ctl_io *starting_io)
11000 union ctl_io *ooa_io;
11003 mtx_assert(&lun->lun_lock, MA_OWNED);
11006 * Run back along the OOA queue, starting with the current
11007 * blocked I/O and going through every I/O before it on the
11008 * queue. If starting_io is NULL, we'll just end up returning
11011 for (ooa_io = starting_io; ooa_io != NULL;
11012 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
11016 * This routine just checks to see whether
11017 * cur_blocked is blocked by ooa_io, which is ahead
11018 * of it in the queue. It doesn't queue/dequeue
11021 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
11023 case CTL_ACTION_BLOCK:
11024 case CTL_ACTION_OVERLAP:
11025 case CTL_ACTION_OVERLAP_TAG:
11026 case CTL_ACTION_SKIP:
11027 case CTL_ACTION_ERROR:
11029 break; /* NOTREACHED */
11030 case CTL_ACTION_PASS:
11033 panic("invalid action %d", action);
11034 break; /* NOTREACHED */
11038 return (CTL_ACTION_PASS);
11043 * - An I/O has just completed, and has been removed from the per-LUN OOA
11044 * queue, so some items on the blocked queue may now be unblocked.
11047 ctl_check_blocked(struct ctl_lun *lun)
11049 union ctl_io *cur_blocked, *next_blocked;
11051 mtx_assert(&lun->lun_lock, MA_OWNED);
11054 * Run forward from the head of the blocked queue, checking each
11055 * entry against the I/Os prior to it on the OOA queue to see if
11056 * there is still any blockage.
11058 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
11059 * with our removing a variable on it while it is traversing the
11062 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
11063 cur_blocked != NULL; cur_blocked = next_blocked) {
11064 union ctl_io *prev_ooa;
11067 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
11070 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
11071 ctl_ooaq, ooa_links);
11074 * If cur_blocked happens to be the first item in the OOA
11075 * queue now, prev_ooa will be NULL, and the action
11076 * returned will just be CTL_ACTION_PASS.
11078 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
11081 case CTL_ACTION_BLOCK:
11082 /* Nothing to do here, still blocked */
11084 case CTL_ACTION_OVERLAP:
11085 case CTL_ACTION_OVERLAP_TAG:
11087 * This shouldn't happen! In theory we've already
11088 * checked this command for overlap...
11091 case CTL_ACTION_PASS:
11092 case CTL_ACTION_SKIP: {
11093 struct ctl_softc *softc;
11094 const struct ctl_cmd_entry *entry;
11099 * The skip case shouldn't happen, this transaction
11100 * should have never made it onto the blocked queue.
11103 * This I/O is no longer blocked, we can remove it
11104 * from the blocked queue. Since this is a TAILQ
11105 * (doubly linked list), we can do O(1) removals
11106 * from any place on the list.
11108 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
11110 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11112 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){
11114 * Need to send IO back to original side to
11117 union ctl_ha_msg msg_info;
11119 msg_info.hdr.original_sc =
11120 cur_blocked->io_hdr.original_sc;
11121 msg_info.hdr.serializing_sc = cur_blocked;
11122 msg_info.hdr.msg_type = CTL_MSG_R2R;
11123 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11124 &msg_info, sizeof(msg_info), 0)) >
11125 CTL_HA_STATUS_SUCCESS) {
11126 printf("CTL:Check Blocked error from "
11127 "ctl_ha_msg_send %d\n",
11132 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
11133 softc = control_softc;
11135 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus);
11138 * Check this I/O for LUN state changes that may
11139 * have happened while this command was blocked.
11140 * The LUN state may have been changed by a command
11141 * ahead of us in the queue, so we need to re-check
11142 * for any states that can be caused by SCSI
11145 if (ctl_scsiio_lun_check(softc, lun, entry,
11146 &cur_blocked->scsiio) == 0) {
11147 cur_blocked->io_hdr.flags |=
11148 CTL_FLAG_IS_WAS_ON_RTR;
11149 ctl_enqueue_rtr(cur_blocked);
11151 ctl_done(cur_blocked);
11156 * This probably shouldn't happen -- we shouldn't
11157 * get CTL_ACTION_ERROR, or anything else.
11163 return (CTL_RETVAL_COMPLETE);
11167 * This routine (with one exception) checks LUN flags that can be set by
11168 * commands ahead of us in the OOA queue. These flags have to be checked
11169 * when a command initially comes in, and when we pull a command off the
11170 * blocked queue and are preparing to execute it. The reason we have to
11171 * check these flags for commands on the blocked queue is that the LUN
11172 * state may have been changed by a command ahead of us while we're on the
11175 * Ordering is somewhat important with these checks, so please pay
11176 * careful attention to the placement of any new checks.
11179 ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
11180 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11187 mtx_assert(&lun->lun_lock, MA_OWNED);
11190 * If this shelf is a secondary shelf controller, we have to reject
11191 * any media access commands.
11194 /* No longer needed for HA */
11195 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0)
11196 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) {
11197 ctl_set_lun_standby(ctsio);
11203 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11204 if (lun->flags & CTL_LUN_READONLY) {
11205 ctl_set_sense(ctsio, /*current_error*/ 1,
11206 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11207 /*asc*/ 0x27, /*ascq*/ 0x01, SSD_ELEM_NONE);
11211 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT]
11212 .eca_and_aen & SCP_SWP) != 0) {
11213 ctl_set_sense(ctsio, /*current_error*/ 1,
11214 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11215 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11222 * Check for a reservation conflict. If this command isn't allowed
11223 * even on reserved LUNs, and if this initiator isn't the one who
11224 * reserved us, reject the command with a reservation conflict.
11226 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
11227 if ((lun->flags & CTL_LUN_RESERVED)
11228 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11229 if (lun->res_idx != residx) {
11230 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
11231 ctsio->io_hdr.status = CTL_SCSI_ERROR;
11237 if ((lun->flags & CTL_LUN_PR_RESERVED)
11238 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) {
11240 * if we aren't registered or it's a res holder type
11241 * reservation and this isn't the res holder then set a
11243 * NOTE: Commands which might be allowed on write exclusive
11244 * type reservations are checked in the particular command
11245 * for a conflict. Read and SSU are the only ones.
11247 if (lun->pr_keys[residx] == 0
11248 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
11249 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
11250 ctsio->io_hdr.status = CTL_SCSI_ERROR;
11257 if ((lun->flags & CTL_LUN_OFFLINE)
11258 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) {
11259 ctl_set_lun_not_ready(ctsio);
11265 * If the LUN is stopped, see if this particular command is allowed
11266 * for a stopped lun. Otherwise, reject it with 0x04,0x02.
11268 if ((lun->flags & CTL_LUN_STOPPED)
11269 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
11270 /* "Logical unit not ready, initializing cmd. required" */
11271 ctl_set_lun_stopped(ctsio);
11276 if ((lun->flags & CTL_LUN_INOPERABLE)
11277 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
11278 /* "Medium format corrupted" */
11279 ctl_set_medium_format_corrupted(ctsio);
11290 ctl_failover_io(union ctl_io *io, int have_lock)
11292 ctl_set_busy(&io->scsiio);
11299 struct ctl_lun *lun;
11300 struct ctl_softc *ctl_softc;
11301 union ctl_io *next_io, *pending_io;
11306 ctl_softc = control_softc;
11308 mtx_lock(&ctl_softc->ctl_lock);
11310 * Remove any cmds from the other SC from the rtr queue. These
11311 * will obviously only be for LUNs for which we're the primary.
11312 * We can't send status or get/send data for these commands.
11313 * Since they haven't been executed yet, we can just remove them.
11314 * We'll either abort them or delete them below, depending on
11315 * which HA mode we're in.
11318 mtx_lock(&ctl_softc->queue_lock);
11319 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue);
11320 io != NULL; io = next_io) {
11321 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
11322 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
11323 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr,
11324 ctl_io_hdr, links);
11326 mtx_unlock(&ctl_softc->queue_lock);
11329 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) {
11330 lun = ctl_softc->ctl_luns[lun_idx];
11335 * Processor LUNs are primary on both sides.
11336 * XXX will this always be true?
11338 if (lun->be_lun->lun_type == T_PROCESSOR)
11341 if ((lun->flags & CTL_LUN_PRIMARY_SC)
11342 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
11343 printf("FAILOVER: primary lun %d\n", lun_idx);
11345 * Remove all commands from the other SC. First from the
11346 * blocked queue then from the ooa queue. Once we have
11347 * removed them. Call ctl_check_blocked to see if there
11348 * is anything that can run.
11350 for (io = (union ctl_io *)TAILQ_FIRST(
11351 &lun->blocked_queue); io != NULL; io = next_io) {
11353 next_io = (union ctl_io *)TAILQ_NEXT(
11354 &io->io_hdr, blocked_links);
11356 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
11357 TAILQ_REMOVE(&lun->blocked_queue,
11358 &io->io_hdr,blocked_links);
11359 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11360 TAILQ_REMOVE(&lun->ooa_queue,
11361 &io->io_hdr, ooa_links);
11367 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
11368 io != NULL; io = next_io) {
11370 next_io = (union ctl_io *)TAILQ_NEXT(
11371 &io->io_hdr, ooa_links);
11373 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
11375 TAILQ_REMOVE(&lun->ooa_queue,
11382 ctl_check_blocked(lun);
11383 } else if ((lun->flags & CTL_LUN_PRIMARY_SC)
11384 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
11386 printf("FAILOVER: primary lun %d\n", lun_idx);
11388 * Abort all commands from the other SC. We can't
11389 * send status back for them now. These should get
11390 * cleaned up when they are completed or come out
11391 * for a datamove operation.
11393 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
11394 io != NULL; io = next_io) {
11395 next_io = (union ctl_io *)TAILQ_NEXT(
11396 &io->io_hdr, ooa_links);
11398 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
11399 io->io_hdr.flags |= CTL_FLAG_ABORT;
11401 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
11402 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
11404 printf("FAILOVER: secondary lun %d\n", lun_idx);
11406 lun->flags |= CTL_LUN_PRIMARY_SC;
11409 * We send all I/O that was sent to this controller
11410 * and redirected to the other side back with
11411 * busy status, and have the initiator retry it.
11412 * Figuring out how much data has been transferred,
11413 * etc. and picking up where we left off would be
11416 * XXX KDM need to remove I/O from the blocked
11419 for (pending_io = (union ctl_io *)TAILQ_FIRST(
11420 &lun->ooa_queue); pending_io != NULL;
11421 pending_io = next_io) {
11423 next_io = (union ctl_io *)TAILQ_NEXT(
11424 &pending_io->io_hdr, ooa_links);
11426 pending_io->io_hdr.flags &=
11427 ~CTL_FLAG_SENT_2OTHER_SC;
11429 if (pending_io->io_hdr.flags &
11430 CTL_FLAG_IO_ACTIVE) {
11431 pending_io->io_hdr.flags |=
11434 ctl_set_busy(&pending_io->scsiio);
11435 ctl_done(pending_io);
11440 * Build Unit Attention
11442 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11443 lun->pending_ua[i] |=
11444 CTL_UA_ASYM_ACC_CHANGE;
11446 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
11447 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
11448 printf("FAILOVER: secondary lun %d\n", lun_idx);
11450 * if the first io on the OOA is not on the RtR queue
11453 lun->flags |= CTL_LUN_PRIMARY_SC;
11455 pending_io = (union ctl_io *)TAILQ_FIRST(
11457 if (pending_io==NULL) {
11458 printf("Nothing on OOA queue\n");
11462 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11463 if ((pending_io->io_hdr.flags &
11464 CTL_FLAG_IS_WAS_ON_RTR) == 0) {
11465 pending_io->io_hdr.flags |=
11466 CTL_FLAG_IS_WAS_ON_RTR;
11467 ctl_enqueue_rtr(pending_io);
11472 printf("Tag 0x%04x is running\n",
11473 pending_io->scsiio.tag_num);
11477 next_io = (union ctl_io *)TAILQ_NEXT(
11478 &pending_io->io_hdr, ooa_links);
11479 for (pending_io=next_io; pending_io != NULL;
11480 pending_io = next_io) {
11481 pending_io->io_hdr.flags &=
11482 ~CTL_FLAG_SENT_2OTHER_SC;
11483 next_io = (union ctl_io *)TAILQ_NEXT(
11484 &pending_io->io_hdr, ooa_links);
11485 if (pending_io->io_hdr.flags &
11486 CTL_FLAG_IS_WAS_ON_RTR) {
11488 printf("Tag 0x%04x is running\n",
11489 pending_io->scsiio.tag_num);
11494 switch (ctl_check_ooa(lun, pending_io,
11495 (union ctl_io *)TAILQ_PREV(
11496 &pending_io->io_hdr, ctl_ooaq,
11499 case CTL_ACTION_BLOCK:
11500 TAILQ_INSERT_TAIL(&lun->blocked_queue,
11501 &pending_io->io_hdr,
11503 pending_io->io_hdr.flags |=
11506 case CTL_ACTION_PASS:
11507 case CTL_ACTION_SKIP:
11508 pending_io->io_hdr.flags |=
11509 CTL_FLAG_IS_WAS_ON_RTR;
11510 ctl_enqueue_rtr(pending_io);
11512 case CTL_ACTION_OVERLAP:
11513 ctl_set_overlapped_cmd(
11514 (struct ctl_scsiio *)pending_io);
11515 ctl_done(pending_io);
11517 case CTL_ACTION_OVERLAP_TAG:
11518 ctl_set_overlapped_tag(
11519 (struct ctl_scsiio *)pending_io,
11520 pending_io->scsiio.tag_num & 0xff);
11521 ctl_done(pending_io);
11523 case CTL_ACTION_ERROR:
11525 ctl_set_internal_failure(
11526 (struct ctl_scsiio *)pending_io,
11529 ctl_done(pending_io);
11535 * Build Unit Attention
11537 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11538 lun->pending_ua[i] |=
11539 CTL_UA_ASYM_ACC_CHANGE;
11542 panic("Unhandled HA mode failover, LUN flags = %#x, "
11543 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode);
11547 mtx_unlock(&ctl_softc->ctl_lock);
11551 ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio)
11553 struct ctl_lun *lun;
11554 const struct ctl_cmd_entry *entry;
11555 uint32_t initidx, targ_lun;
11562 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11563 if ((targ_lun < CTL_MAX_LUNS)
11564 && (ctl_softc->ctl_luns[targ_lun] != NULL)) {
11565 lun = ctl_softc->ctl_luns[targ_lun];
11567 * If the LUN is invalid, pretend that it doesn't exist.
11568 * It will go away as soon as all pending I/O has been
11571 if (lun->flags & CTL_LUN_DISABLED) {
11574 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
11575 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
11577 if (lun->be_lun->lun_type == T_PROCESSOR) {
11578 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV;
11582 * Every I/O goes into the OOA queue for a
11583 * particular LUN, and stays there until completion.
11585 mtx_lock(&lun->lun_lock);
11586 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr,
11590 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11591 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11594 /* Get command entry and return error if it is unsuppotyed. */
11595 entry = ctl_validate_command(ctsio);
11596 if (entry == NULL) {
11598 mtx_unlock(&lun->lun_lock);
11602 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11603 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11606 * Check to see whether we can send this command to LUNs that don't
11607 * exist. This should pretty much only be the case for inquiry
11608 * and request sense. Further checks, below, really require having
11609 * a LUN, so we can't really check the command anymore. Just put
11610 * it on the rtr queue.
11613 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) {
11614 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11615 ctl_enqueue_rtr((union ctl_io *)ctsio);
11619 ctl_set_unsupported_lun(ctsio);
11620 ctl_done((union ctl_io *)ctsio);
11621 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11625 * Make sure we support this particular command on this LUN.
11626 * e.g., we don't support writes to the control LUN.
11628 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11629 mtx_unlock(&lun->lun_lock);
11630 ctl_set_invalid_opcode(ctsio);
11631 ctl_done((union ctl_io *)ctsio);
11636 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11640 * If we've got a request sense, it'll clear the contingent
11641 * allegiance condition. Otherwise, if we have a CA condition for
11642 * this initiator, clear it, because it sent down a command other
11643 * than request sense.
11645 if ((ctsio->cdb[0] != REQUEST_SENSE)
11646 && (ctl_is_set(lun->have_ca, initidx)))
11647 ctl_clear_mask(lun->have_ca, initidx);
11651 * If the command has this flag set, it handles its own unit
11652 * attention reporting, we shouldn't do anything. Otherwise we
11653 * check for any pending unit attentions, and send them back to the
11654 * initiator. We only do this when a command initially comes in,
11655 * not when we pull it off the blocked queue.
11657 * According to SAM-3, section 5.3.2, the order that things get
11658 * presented back to the host is basically unit attentions caused
11659 * by some sort of reset event, busy status, reservation conflicts
11660 * or task set full, and finally any other status.
11662 * One issue here is that some of the unit attentions we report
11663 * don't fall into the "reset" category (e.g. "reported luns data
11664 * has changed"). So reporting it here, before the reservation
11665 * check, may be technically wrong. I guess the only thing to do
11666 * would be to check for and report the reset events here, and then
11667 * check for the other unit attention types after we check for a
11668 * reservation conflict.
11670 * XXX KDM need to fix this
11672 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11673 ctl_ua_type ua_type;
11675 if (lun->pending_ua[initidx] != CTL_UA_NONE) {
11676 scsi_sense_data_type sense_format;
11679 sense_format = (lun->flags &
11680 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC :
11683 sense_format = SSD_TYPE_FIXED;
11685 ua_type = ctl_build_ua(&lun->pending_ua[initidx],
11686 &ctsio->sense_data, sense_format);
11687 if (ua_type != CTL_UA_NONE) {
11688 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11689 ctsio->io_hdr.status = CTL_SCSI_ERROR |
11691 ctsio->sense_len = SSD_FULL_SIZE;
11692 mtx_unlock(&lun->lun_lock);
11693 ctl_done((union ctl_io *)ctsio);
11700 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) {
11701 mtx_unlock(&lun->lun_lock);
11702 ctl_done((union ctl_io *)ctsio);
11707 * XXX CHD this is where we want to send IO to other side if
11708 * this LUN is secondary on this SC. We will need to make a copy
11709 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11710 * the copy we send as FROM_OTHER.
11711 * We also need to stuff the address of the original IO so we can
11712 * find it easily. Something similar will need be done on the other
11713 * side so when we are done we can find the copy.
11715 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11716 union ctl_ha_msg msg_info;
11719 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11721 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11722 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11724 printf("1. ctsio %p\n", ctsio);
11726 msg_info.hdr.serializing_sc = NULL;
11727 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11728 msg_info.scsi.tag_num = ctsio->tag_num;
11729 msg_info.scsi.tag_type = ctsio->tag_type;
11730 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11732 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11734 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11735 (void *)&msg_info, sizeof(msg_info), 0)) >
11736 CTL_HA_STATUS_SUCCESS) {
11737 printf("CTL:precheck, ctl_ha_msg_send returned %d\n",
11739 printf("CTL:opcode is %x\n", ctsio->cdb[0]);
11742 printf("CTL:Precheck sent msg, opcode is %x\n",opcode);
11747 * XXX KDM this I/O is off the incoming queue, but hasn't
11748 * been inserted on any other queue. We may need to come
11749 * up with a holding queue while we wait for serialization
11750 * so that we have an idea of what we're waiting for from
11753 mtx_unlock(&lun->lun_lock);
11757 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11758 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11759 ctl_ooaq, ooa_links))) {
11760 case CTL_ACTION_BLOCK:
11761 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11762 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11764 mtx_unlock(&lun->lun_lock);
11766 case CTL_ACTION_PASS:
11767 case CTL_ACTION_SKIP:
11768 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11769 mtx_unlock(&lun->lun_lock);
11770 ctl_enqueue_rtr((union ctl_io *)ctsio);
11772 case CTL_ACTION_OVERLAP:
11773 mtx_unlock(&lun->lun_lock);
11774 ctl_set_overlapped_cmd(ctsio);
11775 ctl_done((union ctl_io *)ctsio);
11777 case CTL_ACTION_OVERLAP_TAG:
11778 mtx_unlock(&lun->lun_lock);
11779 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11780 ctl_done((union ctl_io *)ctsio);
11782 case CTL_ACTION_ERROR:
11784 mtx_unlock(&lun->lun_lock);
11785 ctl_set_internal_failure(ctsio,
11787 /*retry_count*/ 0);
11788 ctl_done((union ctl_io *)ctsio);
11794 const struct ctl_cmd_entry *
11795 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11797 const struct ctl_cmd_entry *entry;
11798 int service_action;
11800 entry = &ctl_cmd_table[ctsio->cdb[0]];
11802 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11803 if (entry->flags & CTL_CMD_FLAG_SA5) {
11804 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11805 entry = &((const struct ctl_cmd_entry *)
11806 entry->execute)[service_action];
11811 const struct ctl_cmd_entry *
11812 ctl_validate_command(struct ctl_scsiio *ctsio)
11814 const struct ctl_cmd_entry *entry;
11818 entry = ctl_get_cmd_entry(ctsio, &sa);
11819 if (entry->execute == NULL) {
11821 ctl_set_invalid_field(ctsio,
11828 ctl_set_invalid_opcode(ctsio);
11829 ctl_done((union ctl_io *)ctsio);
11832 KASSERT(entry->length > 0,
11833 ("Not defined length for command 0x%02x/0x%02x",
11834 ctsio->cdb[0], ctsio->cdb[1]));
11835 for (i = 1; i < entry->length; i++) {
11836 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11839 ctl_set_invalid_field(ctsio,
11844 /*bit*/ fls(diff) - 1);
11845 ctl_done((union ctl_io *)ctsio);
11852 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11855 switch (lun_type) {
11857 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) &&
11858 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11862 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) &&
11863 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11873 ctl_scsiio(struct ctl_scsiio *ctsio)
11876 const struct ctl_cmd_entry *entry;
11878 retval = CTL_RETVAL_COMPLETE;
11880 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11882 entry = ctl_get_cmd_entry(ctsio, NULL);
11885 * If this I/O has been aborted, just send it straight to
11886 * ctl_done() without executing it.
11888 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11889 ctl_done((union ctl_io *)ctsio);
11894 * All the checks should have been handled by ctl_scsiio_precheck().
11895 * We should be clear now to just execute the I/O.
11897 retval = entry->execute(ctsio);
11904 * Since we only implement one target right now, a bus reset simply resets
11905 * our single target.
11908 ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io)
11910 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET));
11914 ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
11915 ctl_ua_type ua_type)
11917 struct ctl_lun *lun;
11920 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11921 union ctl_ha_msg msg_info;
11923 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11924 msg_info.hdr.nexus = io->io_hdr.nexus;
11925 if (ua_type==CTL_UA_TARG_RESET)
11926 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11928 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11929 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11930 msg_info.hdr.original_sc = NULL;
11931 msg_info.hdr.serializing_sc = NULL;
11932 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11933 (void *)&msg_info, sizeof(msg_info), 0)) {
11938 mtx_lock(&ctl_softc->ctl_lock);
11939 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links)
11940 retval += ctl_lun_reset(lun, io, ua_type);
11941 mtx_unlock(&ctl_softc->ctl_lock);
11947 * The LUN should always be set. The I/O is optional, and is used to
11948 * distinguish between I/Os sent by this initiator, and by other
11949 * initiators. We set unit attention for initiators other than this one.
11950 * SAM-3 is vague on this point. It does say that a unit attention should
11951 * be established for other initiators when a LUN is reset (see section
11952 * 5.7.3), but it doesn't specifically say that the unit attention should
11953 * be established for this particular initiator when a LUN is reset. Here
11954 * is the relevant text, from SAM-3 rev 8:
11956 * 5.7.2 When a SCSI initiator port aborts its own tasks
11958 * When a SCSI initiator port causes its own task(s) to be aborted, no
11959 * notification that the task(s) have been aborted shall be returned to
11960 * the SCSI initiator port other than the completion response for the
11961 * command or task management function action that caused the task(s) to
11962 * be aborted and notification(s) associated with related effects of the
11963 * action (e.g., a reset unit attention condition).
11965 * XXX KDM for now, we're setting unit attention for all initiators.
11968 ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11972 uint32_t initindex;
11976 mtx_lock(&lun->lun_lock);
11978 * Run through the OOA queue and abort each I/O.
11981 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
11983 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11984 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11985 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11989 * This version sets unit attention for every
11992 initindex = ctl_get_initindex(&io->io_hdr.nexus);
11993 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11994 if (initindex == i)
11996 lun->pending_ua[i] |= ua_type;
12001 * A reset (any kind, really) clears reservations established with
12002 * RESERVE/RELEASE. It does not clear reservations established
12003 * with PERSISTENT RESERVE OUT, but we don't support that at the
12004 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
12005 * reservations made with the RESERVE/RELEASE commands, because
12006 * those commands are obsolete in SPC-3.
12008 lun->flags &= ~CTL_LUN_RESERVED;
12010 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
12012 ctl_clear_mask(lun->have_ca, i);
12014 lun->pending_ua[i] |= ua_type;
12016 mtx_unlock(&lun->lun_lock);
12022 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
12027 mtx_assert(&lun->lun_lock, MA_OWNED);
12030 * Run through the OOA queue and attempt to find the given I/O.
12031 * The target port, initiator ID, tag type and tag number have to
12032 * match the values that we got from the initiator. If we have an
12033 * untagged command to abort, simply abort the first untagged command
12034 * we come to. We only allow one untagged command at a time of course.
12036 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12037 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12039 if ((targ_port == UINT32_MAX ||
12040 targ_port == xio->io_hdr.nexus.targ_port) &&
12041 (init_id == UINT32_MAX ||
12042 init_id == xio->io_hdr.nexus.initid.id)) {
12043 if (targ_port != xio->io_hdr.nexus.targ_port ||
12044 init_id != xio->io_hdr.nexus.initid.id)
12045 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
12046 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12047 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12048 union ctl_ha_msg msg_info;
12050 msg_info.hdr.nexus = xio->io_hdr.nexus;
12051 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12052 msg_info.task.tag_num = xio->scsiio.tag_num;
12053 msg_info.task.tag_type = xio->scsiio.tag_type;
12054 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12055 msg_info.hdr.original_sc = NULL;
12056 msg_info.hdr.serializing_sc = NULL;
12057 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12058 (void *)&msg_info, sizeof(msg_info), 0);
12065 ctl_abort_task_set(union ctl_io *io)
12067 struct ctl_softc *softc = control_softc;
12068 struct ctl_lun *lun;
12074 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12075 mtx_lock(&softc->ctl_lock);
12076 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL))
12077 lun = softc->ctl_luns[targ_lun];
12079 mtx_unlock(&softc->ctl_lock);
12083 mtx_lock(&lun->lun_lock);
12084 mtx_unlock(&softc->ctl_lock);
12085 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
12086 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12087 io->io_hdr.nexus.initid.id,
12088 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12089 } else { /* CTL_TASK_CLEAR_TASK_SET */
12090 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
12091 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12093 mtx_unlock(&lun->lun_lock);
12098 ctl_i_t_nexus_reset(union ctl_io *io)
12100 struct ctl_softc *softc = control_softc;
12101 struct ctl_lun *lun;
12102 uint32_t initindex, residx;
12104 initindex = ctl_get_initindex(&io->io_hdr.nexus);
12105 residx = ctl_get_resindex(&io->io_hdr.nexus);
12106 mtx_lock(&softc->ctl_lock);
12107 STAILQ_FOREACH(lun, &softc->lun_list, links) {
12108 mtx_lock(&lun->lun_lock);
12109 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12110 io->io_hdr.nexus.initid.id,
12111 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12113 ctl_clear_mask(lun->have_ca, initindex);
12115 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
12116 lun->flags &= ~CTL_LUN_RESERVED;
12117 lun->pending_ua[initindex] |= CTL_UA_I_T_NEXUS_LOSS;
12118 mtx_unlock(&lun->lun_lock);
12120 mtx_unlock(&softc->ctl_lock);
12125 ctl_abort_task(union ctl_io *io)
12128 struct ctl_lun *lun;
12129 struct ctl_softc *ctl_softc;
12132 char printbuf[128];
12137 ctl_softc = control_softc;
12143 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12144 mtx_lock(&ctl_softc->ctl_lock);
12145 if ((targ_lun < CTL_MAX_LUNS)
12146 && (ctl_softc->ctl_luns[targ_lun] != NULL))
12147 lun = ctl_softc->ctl_luns[targ_lun];
12149 mtx_unlock(&ctl_softc->ctl_lock);
12154 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
12155 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
12158 mtx_lock(&lun->lun_lock);
12159 mtx_unlock(&ctl_softc->ctl_lock);
12161 * Run through the OOA queue and attempt to find the given I/O.
12162 * The target port, initiator ID, tag type and tag number have to
12163 * match the values that we got from the initiator. If we have an
12164 * untagged command to abort, simply abort the first untagged command
12165 * we come to. We only allow one untagged command at a time of course.
12168 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
12170 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12171 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12173 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
12175 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
12176 lun->lun, xio->scsiio.tag_num,
12177 xio->scsiio.tag_type,
12178 (xio->io_hdr.blocked_links.tqe_prev
12179 == NULL) ? "" : " BLOCKED",
12180 (xio->io_hdr.flags &
12181 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
12182 (xio->io_hdr.flags &
12183 CTL_FLAG_ABORT) ? " ABORT" : "",
12184 (xio->io_hdr.flags &
12185 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
12186 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
12188 printf("%s\n", sbuf_data(&sb));
12191 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port)
12192 && (xio->io_hdr.nexus.initid.id ==
12193 io->io_hdr.nexus.initid.id)) {
12195 * If the abort says that the task is untagged, the
12196 * task in the queue must be untagged. Otherwise,
12197 * we just check to see whether the tag numbers
12198 * match. This is because the QLogic firmware
12199 * doesn't pass back the tag type in an abort
12203 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12204 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12205 || (xio->scsiio.tag_num == io->taskio.tag_num)) {
12208 * XXX KDM we've got problems with FC, because it
12209 * doesn't send down a tag type with aborts. So we
12210 * can only really go by the tag number...
12211 * This may cause problems with parallel SCSI.
12212 * Need to figure that out!!
12214 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12215 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12217 if ((io->io_hdr.flags &
12218 CTL_FLAG_FROM_OTHER_SC) == 0 &&
12219 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12220 union ctl_ha_msg msg_info;
12222 io->io_hdr.flags |=
12223 CTL_FLAG_SENT_2OTHER_SC;
12224 msg_info.hdr.nexus = io->io_hdr.nexus;
12225 msg_info.task.task_action =
12226 CTL_TASK_ABORT_TASK;
12227 msg_info.task.tag_num =
12228 io->taskio.tag_num;
12229 msg_info.task.tag_type =
12230 io->taskio.tag_type;
12231 msg_info.hdr.msg_type =
12232 CTL_MSG_MANAGE_TASKS;
12233 msg_info.hdr.original_sc = NULL;
12234 msg_info.hdr.serializing_sc = NULL;
12236 printf("Sent Abort to other side\n");
12238 if (CTL_HA_STATUS_SUCCESS !=
12239 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12241 sizeof(msg_info), 0)) {
12245 printf("ctl_abort_task: found I/O to abort\n");
12251 mtx_unlock(&lun->lun_lock);
12255 * This isn't really an error. It's entirely possible for
12256 * the abort and command completion to cross on the wire.
12257 * This is more of an informative/diagnostic error.
12260 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
12261 "%d:%d:%d:%d tag %d type %d\n",
12262 io->io_hdr.nexus.initid.id,
12263 io->io_hdr.nexus.targ_port,
12264 io->io_hdr.nexus.targ_target.id,
12265 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
12266 io->taskio.tag_type);
12273 ctl_run_task(union ctl_io *io)
12275 struct ctl_softc *ctl_softc = control_softc;
12277 const char *task_desc;
12279 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12281 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12282 ("ctl_run_task: Unextected io_type %d\n",
12283 io->io_hdr.io_type));
12285 task_desc = ctl_scsi_task_string(&io->taskio);
12286 if (task_desc != NULL) {
12288 csevent_log(CSC_CTL | CSC_SHELF_SW |
12290 csevent_LogType_Trace,
12291 csevent_Severity_Information,
12292 csevent_AlertLevel_Green,
12293 csevent_FRU_Firmware,
12294 csevent_FRU_Unknown,
12295 "CTL: received task: %s",task_desc);
12299 csevent_log(CSC_CTL | CSC_SHELF_SW |
12301 csevent_LogType_Trace,
12302 csevent_Severity_Information,
12303 csevent_AlertLevel_Green,
12304 csevent_FRU_Firmware,
12305 csevent_FRU_Unknown,
12306 "CTL: received unknown task "
12308 io->taskio.task_action,
12309 io->taskio.task_action);
12312 switch (io->taskio.task_action) {
12313 case CTL_TASK_ABORT_TASK:
12314 retval = ctl_abort_task(io);
12316 case CTL_TASK_ABORT_TASK_SET:
12317 case CTL_TASK_CLEAR_TASK_SET:
12318 retval = ctl_abort_task_set(io);
12320 case CTL_TASK_CLEAR_ACA:
12322 case CTL_TASK_I_T_NEXUS_RESET:
12323 retval = ctl_i_t_nexus_reset(io);
12325 case CTL_TASK_LUN_RESET: {
12326 struct ctl_lun *lun;
12329 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12330 mtx_lock(&ctl_softc->ctl_lock);
12331 if ((targ_lun < CTL_MAX_LUNS)
12332 && (ctl_softc->ctl_luns[targ_lun] != NULL))
12333 lun = ctl_softc->ctl_luns[targ_lun];
12335 mtx_unlock(&ctl_softc->ctl_lock);
12340 if (!(io->io_hdr.flags &
12341 CTL_FLAG_FROM_OTHER_SC)) {
12342 union ctl_ha_msg msg_info;
12344 io->io_hdr.flags |=
12345 CTL_FLAG_SENT_2OTHER_SC;
12346 msg_info.hdr.msg_type =
12347 CTL_MSG_MANAGE_TASKS;
12348 msg_info.hdr.nexus = io->io_hdr.nexus;
12349 msg_info.task.task_action =
12350 CTL_TASK_LUN_RESET;
12351 msg_info.hdr.original_sc = NULL;
12352 msg_info.hdr.serializing_sc = NULL;
12353 if (CTL_HA_STATUS_SUCCESS !=
12354 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12356 sizeof(msg_info), 0)) {
12360 retval = ctl_lun_reset(lun, io,
12362 mtx_unlock(&ctl_softc->ctl_lock);
12365 case CTL_TASK_TARGET_RESET:
12366 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET);
12368 case CTL_TASK_BUS_RESET:
12369 retval = ctl_bus_reset(ctl_softc, io);
12371 case CTL_TASK_PORT_LOGIN:
12373 case CTL_TASK_PORT_LOGOUT:
12376 printf("ctl_run_task: got unknown task management event %d\n",
12377 io->taskio.task_action);
12381 io->io_hdr.status = CTL_SUCCESS;
12383 io->io_hdr.status = CTL_ERROR;
12388 * For HA operation. Handle commands that come in from the other
12392 ctl_handle_isc(union ctl_io *io)
12395 struct ctl_lun *lun;
12396 struct ctl_softc *ctl_softc;
12399 ctl_softc = control_softc;
12401 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12402 lun = ctl_softc->ctl_luns[targ_lun];
12404 switch (io->io_hdr.msg_type) {
12405 case CTL_MSG_SERIALIZE:
12406 free_io = ctl_serialize_other_sc_cmd(&io->scsiio);
12408 case CTL_MSG_R2R: {
12409 const struct ctl_cmd_entry *entry;
12412 * This is only used in SER_ONLY mode.
12415 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12416 mtx_lock(&lun->lun_lock);
12417 if (ctl_scsiio_lun_check(ctl_softc, lun,
12418 entry, (struct ctl_scsiio *)io) != 0) {
12419 mtx_unlock(&lun->lun_lock);
12423 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12424 mtx_unlock(&lun->lun_lock);
12425 ctl_enqueue_rtr(io);
12428 case CTL_MSG_FINISH_IO:
12429 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
12434 mtx_lock(&lun->lun_lock);
12435 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
12437 ctl_check_blocked(lun);
12438 mtx_unlock(&lun->lun_lock);
12441 case CTL_MSG_PERS_ACTION:
12442 ctl_hndl_per_res_out_on_other_sc(
12443 (union ctl_ha_msg *)&io->presio.pr_msg);
12446 case CTL_MSG_BAD_JUJU:
12450 case CTL_MSG_DATAMOVE:
12451 /* Only used in XFER mode */
12453 ctl_datamove_remote(io);
12455 case CTL_MSG_DATAMOVE_DONE:
12456 /* Only used in XFER mode */
12458 io->scsiio.be_move_done(io);
12462 printf("%s: Invalid message type %d\n",
12463 __func__, io->io_hdr.msg_type);
12473 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12474 * there is no match.
12476 static ctl_lun_error_pattern
12477 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12479 const struct ctl_cmd_entry *entry;
12480 ctl_lun_error_pattern filtered_pattern, pattern;
12482 pattern = desc->error_pattern;
12485 * XXX KDM we need more data passed into this function to match a
12486 * custom pattern, and we actually need to implement custom pattern
12489 if (pattern & CTL_LUN_PAT_CMD)
12490 return (CTL_LUN_PAT_CMD);
12492 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12493 return (CTL_LUN_PAT_ANY);
12495 entry = ctl_get_cmd_entry(ctsio, NULL);
12497 filtered_pattern = entry->pattern & pattern;
12500 * If the user requested specific flags in the pattern (e.g.
12501 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12504 * If the user did not specify any flags, it doesn't matter whether
12505 * or not the command supports the flags.
12507 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12508 (pattern & ~CTL_LUN_PAT_MASK))
12509 return (CTL_LUN_PAT_NONE);
12512 * If the user asked for a range check, see if the requested LBA
12513 * range overlaps with this command's LBA range.
12515 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12521 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12523 return (CTL_LUN_PAT_NONE);
12525 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12526 desc->lba_range.len);
12528 * A "pass" means that the LBA ranges don't overlap, so
12529 * this doesn't match the user's range criteria.
12531 if (action == CTL_ACTION_PASS)
12532 return (CTL_LUN_PAT_NONE);
12535 return (filtered_pattern);
12539 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12541 struct ctl_error_desc *desc, *desc2;
12543 mtx_assert(&lun->lun_lock, MA_OWNED);
12545 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12546 ctl_lun_error_pattern pattern;
12548 * Check to see whether this particular command matches
12549 * the pattern in the descriptor.
12551 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12552 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12555 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12556 case CTL_LUN_INJ_ABORTED:
12557 ctl_set_aborted(&io->scsiio);
12559 case CTL_LUN_INJ_MEDIUM_ERR:
12560 ctl_set_medium_error(&io->scsiio);
12562 case CTL_LUN_INJ_UA:
12563 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12565 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12567 case CTL_LUN_INJ_CUSTOM:
12569 * We're assuming the user knows what he is doing.
12570 * Just copy the sense information without doing
12573 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12574 ctl_min(sizeof(desc->custom_sense),
12575 sizeof(io->scsiio.sense_data)));
12576 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12577 io->scsiio.sense_len = SSD_FULL_SIZE;
12578 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12580 case CTL_LUN_INJ_NONE:
12583 * If this is an error injection type we don't know
12584 * about, clear the continuous flag (if it is set)
12585 * so it will get deleted below.
12587 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12591 * By default, each error injection action is a one-shot
12593 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12596 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12602 #ifdef CTL_IO_DELAY
12604 ctl_datamove_timer_wakeup(void *arg)
12608 io = (union ctl_io *)arg;
12612 #endif /* CTL_IO_DELAY */
12615 ctl_datamove(union ctl_io *io)
12617 void (*fe_datamove)(union ctl_io *io);
12619 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12621 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12624 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12629 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12630 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12632 sbuf_cat(&sb, path_str);
12633 switch (io->io_hdr.io_type) {
12635 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12636 sbuf_printf(&sb, "\n");
12637 sbuf_cat(&sb, path_str);
12638 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12639 io->scsiio.tag_num, io->scsiio.tag_type);
12642 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12643 "Tag Type: %d\n", io->taskio.task_action,
12644 io->taskio.tag_num, io->taskio.tag_type);
12647 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12648 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12651 sbuf_cat(&sb, path_str);
12652 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12653 (intmax_t)time_uptime - io->io_hdr.start_time);
12655 printf("%s", sbuf_data(&sb));
12657 #endif /* CTL_TIME_IO */
12659 #ifdef CTL_IO_DELAY
12660 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12661 struct ctl_lun *lun;
12663 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12665 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12667 struct ctl_lun *lun;
12669 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12671 && (lun->delay_info.datamove_delay > 0)) {
12672 struct callout *callout;
12674 callout = (struct callout *)&io->io_hdr.timer_bytes;
12675 callout_init(callout, /*mpsafe*/ 1);
12676 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12677 callout_reset(callout,
12678 lun->delay_info.datamove_delay * hz,
12679 ctl_datamove_timer_wakeup, io);
12680 if (lun->delay_info.datamove_type ==
12681 CTL_DELAY_TYPE_ONESHOT)
12682 lun->delay_info.datamove_delay = 0;
12689 * This command has been aborted. Set the port status, so we fail
12692 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12693 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n",
12694 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id,
12695 io->io_hdr.nexus.targ_port,
12696 (uintmax_t)io->io_hdr.nexus.targ_target.id,
12697 io->io_hdr.nexus.targ_lun);
12698 io->io_hdr.port_status = 31337;
12700 * Note that the backend, in this case, will get the
12701 * callback in its context. In other cases it may get
12702 * called in the frontend's interrupt thread context.
12704 io->scsiio.be_move_done(io);
12708 /* Don't confuse frontend with zero length data move. */
12709 if (io->scsiio.kern_data_len == 0) {
12710 io->scsiio.be_move_done(io);
12715 * If we're in XFER mode and this I/O is from the other shelf
12716 * controller, we need to send the DMA to the other side to
12717 * actually transfer the data to/from the host. In serialize only
12718 * mode the transfer happens below CTL and ctl_datamove() is only
12719 * called on the machine that originally received the I/O.
12721 if ((control_softc->ha_mode == CTL_HA_MODE_XFER)
12722 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12723 union ctl_ha_msg msg;
12724 uint32_t sg_entries_sent;
12728 memset(&msg, 0, sizeof(msg));
12729 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
12730 msg.hdr.original_sc = io->io_hdr.original_sc;
12731 msg.hdr.serializing_sc = io;
12732 msg.hdr.nexus = io->io_hdr.nexus;
12733 msg.dt.flags = io->io_hdr.flags;
12735 * We convert everything into a S/G list here. We can't
12736 * pass by reference, only by value between controllers.
12737 * So we can't pass a pointer to the S/G list, only as many
12738 * S/G entries as we can fit in here. If it's possible for
12739 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
12740 * then we need to break this up into multiple transfers.
12742 if (io->scsiio.kern_sg_entries == 0) {
12743 msg.dt.kern_sg_entries = 1;
12745 * If this is in cached memory, flush the cache
12746 * before we send the DMA request to the other
12747 * controller. We want to do this in either the
12748 * read or the write case. The read case is
12749 * straightforward. In the write case, we want to
12750 * make sure nothing is in the local cache that
12751 * could overwrite the DMAed data.
12753 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12755 * XXX KDM use bus_dmamap_sync() here.
12760 * Convert to a physical address if this is a
12763 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
12764 msg.dt.sg_list[0].addr =
12765 io->scsiio.kern_data_ptr;
12768 * XXX KDM use busdma here!
12771 msg.dt.sg_list[0].addr = (void *)
12772 vtophys(io->scsiio.kern_data_ptr);
12776 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
12779 struct ctl_sg_entry *sgl;
12782 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
12783 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
12784 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12786 * XXX KDM use bus_dmamap_sync() here.
12791 msg.dt.kern_data_len = io->scsiio.kern_data_len;
12792 msg.dt.kern_total_len = io->scsiio.kern_total_len;
12793 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
12794 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
12795 msg.dt.sg_sequence = 0;
12798 * Loop until we've sent all of the S/G entries. On the
12799 * other end, we'll recompose these S/G entries into one
12800 * contiguous list before passing it to the
12802 for (sg_entries_sent = 0; sg_entries_sent <
12803 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) {
12804 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/
12805 sizeof(msg.dt.sg_list[0])),
12806 msg.dt.kern_sg_entries - sg_entries_sent);
12808 if (do_sg_copy != 0) {
12809 struct ctl_sg_entry *sgl;
12812 sgl = (struct ctl_sg_entry *)
12813 io->scsiio.kern_data_ptr;
12815 * If this is in cached memory, flush the cache
12816 * before we send the DMA request to the other
12817 * controller. We want to do this in either
12818 * the * read or the write case. The read
12819 * case is straightforward. In the write
12820 * case, we want to make sure nothing is
12821 * in the local cache that could overwrite
12825 for (i = sg_entries_sent, j = 0;
12826 i < msg.dt.cur_sg_entries; i++, j++) {
12827 if ((io->io_hdr.flags &
12828 CTL_FLAG_NO_DATASYNC) == 0) {
12830 * XXX KDM use bus_dmamap_sync()
12833 if ((io->io_hdr.flags &
12834 CTL_FLAG_BUS_ADDR) == 0) {
12836 * XXX KDM use busdma.
12839 msg.dt.sg_list[j].addr =(void *)
12840 vtophys(sgl[i].addr);
12843 msg.dt.sg_list[j].addr =
12846 msg.dt.sg_list[j].len = sgl[i].len;
12850 sg_entries_sent += msg.dt.cur_sg_entries;
12851 if (sg_entries_sent >= msg.dt.kern_sg_entries)
12852 msg.dt.sg_last = 1;
12854 msg.dt.sg_last = 0;
12857 * XXX KDM drop and reacquire the lock here?
12859 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12860 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
12862 * XXX do something here.
12866 msg.dt.sent_sg_entries = sg_entries_sent;
12868 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12869 if (io->io_hdr.flags & CTL_FLAG_FAILOVER)
12870 ctl_failover_io(io, /*have_lock*/ 0);
12875 * Lookup the fe_datamove() function for this particular
12879 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12886 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12888 union ctl_ha_msg msg;
12891 memset(&msg, 0, sizeof(msg));
12893 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12894 msg.hdr.original_sc = io;
12895 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12896 msg.hdr.nexus = io->io_hdr.nexus;
12897 msg.hdr.status = io->io_hdr.status;
12898 msg.scsi.tag_num = io->scsiio.tag_num;
12899 msg.scsi.tag_type = io->scsiio.tag_type;
12900 msg.scsi.scsi_status = io->scsiio.scsi_status;
12901 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12902 sizeof(io->scsiio.sense_data));
12903 msg.scsi.sense_len = io->scsiio.sense_len;
12904 msg.scsi.sense_residual = io->scsiio.sense_residual;
12905 msg.scsi.fetd_status = io->io_hdr.port_status;
12906 msg.scsi.residual = io->scsiio.residual;
12907 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12909 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12910 ctl_failover_io(io, /*have_lock*/ have_lock);
12914 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0);
12915 if (isc_status > CTL_HA_STATUS_SUCCESS) {
12916 /* XXX do something if this fails */
12922 * The DMA to the remote side is done, now we need to tell the other side
12923 * we're done so it can continue with its data movement.
12926 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12932 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12933 printf("%s: ISC DMA write failed with error %d", __func__,
12935 ctl_set_internal_failure(&io->scsiio,
12937 /*retry_count*/ rq->ret);
12940 ctl_dt_req_free(rq);
12943 * In this case, we had to malloc the memory locally. Free it.
12945 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12947 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12948 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12951 * The data is in local and remote memory, so now we need to send
12952 * status (good or back) back to the other side.
12954 ctl_send_datamove_done(io, /*have_lock*/ 0);
12958 * We've moved the data from the host/controller into local memory. Now we
12959 * need to push it over to the remote controller's memory.
12962 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12968 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12969 ctl_datamove_remote_write_cb);
12975 ctl_datamove_remote_write(union ctl_io *io)
12978 void (*fe_datamove)(union ctl_io *io);
12981 * - Get the data from the host/HBA into local memory.
12982 * - DMA memory from the local controller to the remote controller.
12983 * - Send status back to the remote controller.
12986 retval = ctl_datamove_remote_sgl_setup(io);
12990 /* Switch the pointer over so the FETD knows what to do */
12991 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12994 * Use a custom move done callback, since we need to send completion
12995 * back to the other controller, not to the backend on this side.
12997 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12999 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
13008 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
13017 * In this case, we had to malloc the memory locally. Free it.
13019 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
13021 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13022 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13026 scsi_path_string(io, path_str, sizeof(path_str));
13027 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13028 sbuf_cat(&sb, path_str);
13029 scsi_command_string(&io->scsiio, NULL, &sb);
13030 sbuf_printf(&sb, "\n");
13031 sbuf_cat(&sb, path_str);
13032 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13033 io->scsiio.tag_num, io->scsiio.tag_type);
13034 sbuf_cat(&sb, path_str);
13035 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
13036 io->io_hdr.flags, io->io_hdr.status);
13038 printk("%s", sbuf_data(&sb));
13043 * The read is done, now we need to send status (good or bad) back
13044 * to the other side.
13046 ctl_send_datamove_done(io, /*have_lock*/ 0);
13052 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
13055 void (*fe_datamove)(union ctl_io *io);
13059 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
13060 printf("%s: ISC DMA read failed with error %d", __func__,
13062 ctl_set_internal_failure(&io->scsiio,
13064 /*retry_count*/ rq->ret);
13067 ctl_dt_req_free(rq);
13069 /* Switch the pointer over so the FETD knows what to do */
13070 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
13073 * Use a custom move done callback, since we need to send completion
13074 * back to the other controller, not to the backend on this side.
13076 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
13078 /* XXX KDM add checks like the ones in ctl_datamove? */
13080 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
13086 ctl_datamove_remote_sgl_setup(union ctl_io *io)
13088 struct ctl_sg_entry *local_sglist, *remote_sglist;
13089 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist;
13090 struct ctl_softc *softc;
13095 softc = control_softc;
13097 local_sglist = io->io_hdr.local_sglist;
13098 local_dma_sglist = io->io_hdr.local_dma_sglist;
13099 remote_sglist = io->io_hdr.remote_sglist;
13100 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
13102 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) {
13103 for (i = 0; i < io->scsiio.kern_sg_entries; i++) {
13104 local_sglist[i].len = remote_sglist[i].len;
13107 * XXX Detect the situation where the RS-level I/O
13108 * redirector on the other side has already read the
13109 * data off of the AOR RS on this side, and
13110 * transferred it to remote (mirror) memory on the
13111 * other side. Since we already have the data in
13112 * memory here, we just need to use it.
13114 * XXX KDM this can probably be removed once we
13115 * get the cache device code in and take the
13116 * current AOR implementation out.
13119 if ((remote_sglist[i].addr >=
13120 (void *)vtophys(softc->mirr->addr))
13121 && (remote_sglist[i].addr <
13122 ((void *)vtophys(softc->mirr->addr) +
13123 CacheMirrorOffset))) {
13124 local_sglist[i].addr = remote_sglist[i].addr -
13126 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13128 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE;
13130 local_sglist[i].addr = remote_sglist[i].addr +
13135 printf("%s: local %p, remote %p, len %d\n",
13136 __func__, local_sglist[i].addr,
13137 remote_sglist[i].addr, local_sglist[i].len);
13141 uint32_t len_to_go;
13144 * In this case, we don't have automatically allocated
13145 * memory for this I/O on this controller. This typically
13146 * happens with internal CTL I/O -- e.g. inquiry, mode
13147 * sense, etc. Anything coming from RAIDCore will have
13148 * a mirror area available.
13150 len_to_go = io->scsiio.kern_data_len;
13153 * Clear the no datasync flag, we have to use malloced
13156 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC;
13159 * The difficult thing here is that the size of the various
13160 * S/G segments may be different than the size from the
13161 * remote controller. That'll make it harder when DMAing
13162 * the data back to the other side.
13164 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) /
13165 sizeof(io->io_hdr.remote_sglist[0])) &&
13166 (len_to_go > 0); i++) {
13167 local_sglist[i].len = ctl_min(len_to_go, 131072);
13168 CTL_SIZE_8B(local_dma_sglist[i].len,
13169 local_sglist[i].len);
13170 local_sglist[i].addr =
13171 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK);
13173 local_dma_sglist[i].addr = local_sglist[i].addr;
13175 if (local_sglist[i].addr == NULL) {
13178 printf("malloc failed for %zd bytes!",
13179 local_dma_sglist[i].len);
13180 for (j = 0; j < i; j++) {
13181 free(local_sglist[j].addr, M_CTL);
13183 ctl_set_internal_failure(&io->scsiio,
13185 /*retry_count*/ 4857);
13187 goto bailout_error;
13190 /* XXX KDM do we need a sync here? */
13192 len_to_go -= local_sglist[i].len;
13195 * Reset the number of S/G entries accordingly. The
13196 * original number of S/G entries is available in
13199 io->scsiio.kern_sg_entries = i;
13202 printf("%s: kern_sg_entries = %d\n", __func__,
13203 io->scsiio.kern_sg_entries);
13204 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13205 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i,
13206 local_sglist[i].addr, local_sglist[i].len,
13207 local_dma_sglist[i].len);
13216 ctl_send_datamove_done(io, /*have_lock*/ 0);
13222 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
13223 ctl_ha_dt_cb callback)
13225 struct ctl_ha_dt_req *rq;
13226 struct ctl_sg_entry *remote_sglist, *local_sglist;
13227 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist;
13228 uint32_t local_used, remote_used, total_used;
13234 rq = ctl_dt_req_alloc();
13237 * If we failed to allocate the request, and if the DMA didn't fail
13238 * anyway, set busy status. This is just a resource allocation
13242 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE))
13243 ctl_set_busy(&io->scsiio);
13245 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
13248 ctl_dt_req_free(rq);
13251 * The data move failed. We need to return status back
13252 * to the other controller. No point in trying to DMA
13253 * data to the remote controller.
13256 ctl_send_datamove_done(io, /*have_lock*/ 0);
13263 local_sglist = io->io_hdr.local_sglist;
13264 local_dma_sglist = io->io_hdr.local_dma_sglist;
13265 remote_sglist = io->io_hdr.remote_sglist;
13266 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
13271 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) {
13272 rq->ret = CTL_HA_STATUS_SUCCESS;
13279 * Pull/push the data over the wire from/to the other controller.
13280 * This takes into account the possibility that the local and
13281 * remote sglists may not be identical in terms of the size of
13282 * the elements and the number of elements.
13284 * One fundamental assumption here is that the length allocated for
13285 * both the local and remote sglists is identical. Otherwise, we've
13286 * essentially got a coding error of some sort.
13288 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
13290 uint32_t cur_len, dma_length;
13293 rq->id = CTL_HA_DATA_CTL;
13294 rq->command = command;
13298 * Both pointers should be aligned. But it is possible
13299 * that the allocation length is not. They should both
13300 * also have enough slack left over at the end, though,
13301 * to round up to the next 8 byte boundary.
13303 cur_len = ctl_min(local_sglist[i].len - local_used,
13304 remote_sglist[j].len - remote_used);
13307 * In this case, we have a size issue and need to decrease
13308 * the size, except in the case where we actually have less
13309 * than 8 bytes left. In that case, we need to increase
13310 * the DMA length to get the last bit.
13312 if ((cur_len & 0x7) != 0) {
13313 if (cur_len > 0x7) {
13314 cur_len = cur_len - (cur_len & 0x7);
13315 dma_length = cur_len;
13317 CTL_SIZE_8B(dma_length, cur_len);
13321 dma_length = cur_len;
13324 * If we had to allocate memory for this I/O, instead of using
13325 * the non-cached mirror memory, we'll need to flush the cache
13326 * before trying to DMA to the other controller.
13328 * We could end up doing this multiple times for the same
13329 * segment if we have a larger local segment than remote
13330 * segment. That shouldn't be an issue.
13332 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
13334 * XXX KDM use bus_dmamap_sync() here.
13338 rq->size = dma_length;
13340 tmp_ptr = (uint8_t *)local_sglist[i].addr;
13341 tmp_ptr += local_used;
13343 /* Use physical addresses when talking to ISC hardware */
13344 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
13345 /* XXX KDM use busdma */
13347 rq->local = vtophys(tmp_ptr);
13350 rq->local = tmp_ptr;
13352 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
13353 tmp_ptr += remote_used;
13354 rq->remote = tmp_ptr;
13356 rq->callback = NULL;
13358 local_used += cur_len;
13359 if (local_used >= local_sglist[i].len) {
13364 remote_used += cur_len;
13365 if (remote_used >= remote_sglist[j].len) {
13369 total_used += cur_len;
13371 if (total_used >= io->scsiio.kern_data_len)
13372 rq->callback = callback;
13374 if ((rq->size & 0x7) != 0) {
13375 printf("%s: warning: size %d is not on 8b boundary\n",
13376 __func__, rq->size);
13378 if (((uintptr_t)rq->local & 0x7) != 0) {
13379 printf("%s: warning: local %p not on 8b boundary\n",
13380 __func__, rq->local);
13382 if (((uintptr_t)rq->remote & 0x7) != 0) {
13383 printf("%s: warning: remote %p not on 8b boundary\n",
13384 __func__, rq->local);
13387 printf("%s: %s: local %#x remote %#x size %d\n", __func__,
13388 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
13389 rq->local, rq->remote, rq->size);
13392 isc_ret = ctl_dt_single(rq);
13393 if (isc_ret == CTL_HA_STATUS_WAIT)
13396 if (isc_ret == CTL_HA_STATUS_DISCONNECT) {
13397 rq->ret = CTL_HA_STATUS_SUCCESS;
13411 ctl_datamove_remote_read(union ctl_io *io)
13417 * This will send an error to the other controller in the case of a
13420 retval = ctl_datamove_remote_sgl_setup(io);
13424 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
13425 ctl_datamove_remote_read_cb);
13427 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) {
13429 * Make sure we free memory if there was an error.. The
13430 * ctl_datamove_remote_xfer() function will send the
13431 * datamove done message, or call the callback with an
13432 * error if there is a problem.
13434 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13435 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13442 * Process a datamove request from the other controller. This is used for
13443 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
13444 * first. Once that is complete, the data gets DMAed into the remote
13445 * controller's memory. For reads, we DMA from the remote controller's
13446 * memory into our memory first, and then move it out to the FETD.
13449 ctl_datamove_remote(union ctl_io *io)
13451 struct ctl_softc *softc;
13453 softc = control_softc;
13455 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
13458 * Note that we look for an aborted I/O here, but don't do some of
13459 * the other checks that ctl_datamove() normally does.
13460 * We don't need to run the datamove delay code, since that should
13461 * have been done if need be on the other controller.
13463 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
13464 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__,
13465 io->scsiio.tag_num, io->io_hdr.nexus.initid.id,
13466 io->io_hdr.nexus.targ_port,
13467 io->io_hdr.nexus.targ_target.id,
13468 io->io_hdr.nexus.targ_lun);
13469 io->io_hdr.port_status = 31338;
13470 ctl_send_datamove_done(io, /*have_lock*/ 0);
13474 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) {
13475 ctl_datamove_remote_write(io);
13476 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){
13477 ctl_datamove_remote_read(io);
13479 union ctl_ha_msg msg;
13480 struct scsi_sense_data *sense;
13484 memset(&msg, 0, sizeof(msg));
13486 msg.hdr.msg_type = CTL_MSG_BAD_JUJU;
13487 msg.hdr.status = CTL_SCSI_ERROR;
13488 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
13490 retry_count = 4243;
13492 sense = &msg.scsi.sense_data;
13493 sks[0] = SSD_SCS_VALID;
13494 sks[1] = (retry_count >> 8) & 0xff;
13495 sks[2] = retry_count & 0xff;
13497 /* "Internal target failure" */
13498 scsi_set_sense_data(sense,
13499 /*sense_format*/ SSD_TYPE_NONE,
13500 /*current_error*/ 1,
13501 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
13504 /*type*/ SSD_ELEM_SKS,
13505 /*size*/ sizeof(sks),
13509 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
13510 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
13511 ctl_failover_io(io, /*have_lock*/ 1);
13515 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) >
13516 CTL_HA_STATUS_SUCCESS) {
13517 /* XXX KDM what to do if this fails? */
13525 ctl_process_done(union ctl_io *io)
13527 struct ctl_lun *lun;
13528 struct ctl_softc *ctl_softc;
13529 void (*fe_done)(union ctl_io *io);
13530 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port);
13532 CTL_DEBUG_PRINT(("ctl_process_done\n"));
13535 control_softc->ctl_ports[targ_port]->fe_done;
13538 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
13543 ctl_scsi_path_string(io, path_str, sizeof(path_str));
13544 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13546 sbuf_cat(&sb, path_str);
13547 switch (io->io_hdr.io_type) {
13549 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
13550 sbuf_printf(&sb, "\n");
13551 sbuf_cat(&sb, path_str);
13552 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13553 io->scsiio.tag_num, io->scsiio.tag_type);
13556 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
13557 "Tag Type: %d\n", io->taskio.task_action,
13558 io->taskio.tag_num, io->taskio.tag_type);
13561 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13562 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13565 sbuf_cat(&sb, path_str);
13566 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
13567 (intmax_t)time_uptime - io->io_hdr.start_time);
13569 printf("%s", sbuf_data(&sb));
13571 #endif /* CTL_TIME_IO */
13573 switch (io->io_hdr.io_type) {
13577 if (bootverbose || (ctl_debug & CTL_DEBUG_INFO))
13578 ctl_io_error_print(io, NULL);
13579 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
13583 return (CTL_RETVAL_COMPLETE);
13585 panic("ctl_process_done: invalid io type %d\n",
13586 io->io_hdr.io_type);
13587 break; /* NOTREACHED */
13590 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13592 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13593 io->io_hdr.nexus.targ_mapped_lun));
13597 ctl_softc = lun->ctl_softc;
13599 mtx_lock(&lun->lun_lock);
13602 * Check to see if we have any errors to inject here. We only
13603 * inject errors for commands that don't already have errors set.
13605 if ((STAILQ_FIRST(&lun->error_list) != NULL)
13606 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))
13607 ctl_inject_error(lun, io);
13610 * XXX KDM how do we treat commands that aren't completed
13613 * XXX KDM should we also track I/O latency?
13615 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13616 io->io_hdr.io_type == CTL_IO_SCSI) {
13618 struct bintime cur_bt;
13622 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13624 type = CTL_STATS_READ;
13625 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13627 type = CTL_STATS_WRITE;
13629 type = CTL_STATS_NO_IO;
13631 lun->stats.ports[targ_port].bytes[type] +=
13632 io->scsiio.kern_total_len;
13633 lun->stats.ports[targ_port].operations[type]++;
13635 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
13636 &io->io_hdr.dma_bt);
13637 lun->stats.ports[targ_port].num_dmas[type] +=
13638 io->io_hdr.num_dmas;
13639 getbintime(&cur_bt);
13640 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
13641 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
13646 * Remove this from the OOA queue.
13648 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13651 * Run through the blocked queue on this LUN and see if anything
13652 * has become unblocked, now that this transaction is done.
13654 ctl_check_blocked(lun);
13657 * If the LUN has been invalidated, free it if there is nothing
13658 * left on its OOA queue.
13660 if ((lun->flags & CTL_LUN_INVALID)
13661 && TAILQ_EMPTY(&lun->ooa_queue)) {
13662 mtx_unlock(&lun->lun_lock);
13663 mtx_lock(&ctl_softc->ctl_lock);
13665 mtx_unlock(&ctl_softc->ctl_lock);
13667 mtx_unlock(&lun->lun_lock);
13670 * If this command has been aborted, make sure we set the status
13671 * properly. The FETD is responsible for freeing the I/O and doing
13672 * whatever it needs to do to clean up its state.
13674 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13675 ctl_set_task_aborted(&io->scsiio);
13678 * If enabled, print command error status.
13679 * We don't print UAs unless debugging was enabled explicitly.
13682 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
13684 if (!bootverbose && (ctl_debug & CTL_DEBUG_INFO) == 0)
13686 if ((ctl_debug & CTL_DEBUG_INFO) == 0 &&
13687 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) &&
13688 (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) {
13689 int error_code, sense_key, asc, ascq;
13691 scsi_extract_sense_len(&io->scsiio.sense_data,
13692 io->scsiio.sense_len, &error_code, &sense_key,
13693 &asc, &ascq, /*show_errors*/ 0);
13694 if (sense_key == SSD_KEY_UNIT_ATTENTION)
13698 ctl_io_error_print(io, NULL);
13702 * Tell the FETD or the other shelf controller we're done with this
13703 * command. Note that only SCSI commands get to this point. Task
13704 * management commands are completed above.
13706 * We only send status to the other controller if we're in XFER
13707 * mode. In SER_ONLY mode, the I/O is done on the controller that
13708 * received the I/O (from CTL's perspective), and so the status is
13711 * XXX KDM if we hold the lock here, we could cause a deadlock
13712 * if the frontend comes back in in this context to queue
13715 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER)
13716 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
13717 union ctl_ha_msg msg;
13719 memset(&msg, 0, sizeof(msg));
13720 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13721 msg.hdr.original_sc = io->io_hdr.original_sc;
13722 msg.hdr.nexus = io->io_hdr.nexus;
13723 msg.hdr.status = io->io_hdr.status;
13724 msg.scsi.scsi_status = io->scsiio.scsi_status;
13725 msg.scsi.tag_num = io->scsiio.tag_num;
13726 msg.scsi.tag_type = io->scsiio.tag_type;
13727 msg.scsi.sense_len = io->scsiio.sense_len;
13728 msg.scsi.sense_residual = io->scsiio.sense_residual;
13729 msg.scsi.residual = io->scsiio.residual;
13730 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
13731 sizeof(io->scsiio.sense_data));
13733 * We copy this whether or not this is an I/O-related
13734 * command. Otherwise, we'd have to go and check to see
13735 * whether it's a read/write command, and it really isn't
13738 memcpy(&msg.scsi.lbalen,
13739 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
13740 sizeof(msg.scsi.lbalen));
13742 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13743 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
13744 /* XXX do something here */
13753 return (CTL_RETVAL_COMPLETE);
13758 * Front end should call this if it doesn't do autosense. When the request
13759 * sense comes back in from the initiator, we'll dequeue this and send it.
13762 ctl_queue_sense(union ctl_io *io)
13764 struct ctl_lun *lun;
13765 struct ctl_softc *ctl_softc;
13766 uint32_t initidx, targ_lun;
13768 ctl_softc = control_softc;
13770 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13773 * LUN lookup will likely move to the ctl_work_thread() once we
13774 * have our new queueing infrastructure (that doesn't put things on
13775 * a per-LUN queue initially). That is so that we can handle
13776 * things like an INQUIRY to a LUN that we don't have enabled. We
13777 * can't deal with that right now.
13779 mtx_lock(&ctl_softc->ctl_lock);
13782 * If we don't have a LUN for this, just toss the sense
13785 targ_lun = io->io_hdr.nexus.targ_lun;
13786 targ_lun = ctl_map_lun(io->io_hdr.nexus.targ_port, targ_lun);
13787 if ((targ_lun < CTL_MAX_LUNS)
13788 && (ctl_softc->ctl_luns[targ_lun] != NULL))
13789 lun = ctl_softc->ctl_luns[targ_lun];
13793 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13795 mtx_lock(&lun->lun_lock);
13797 * Already have CA set for this LUN...toss the sense information.
13799 if (ctl_is_set(lun->have_ca, initidx)) {
13800 mtx_unlock(&lun->lun_lock);
13804 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data,
13805 ctl_min(sizeof(lun->pending_sense[initidx]),
13806 sizeof(io->scsiio.sense_data)));
13807 ctl_set_mask(lun->have_ca, initidx);
13808 mtx_unlock(&lun->lun_lock);
13811 mtx_unlock(&ctl_softc->ctl_lock);
13815 return (CTL_RETVAL_COMPLETE);
13820 * Primary command inlet from frontend ports. All SCSI and task I/O
13821 * requests must go through this function.
13824 ctl_queue(union ctl_io *io)
13826 struct ctl_softc *ctl_softc;
13828 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13830 ctl_softc = control_softc;
13833 io->io_hdr.start_time = time_uptime;
13834 getbintime(&io->io_hdr.start_bt);
13835 #endif /* CTL_TIME_IO */
13837 /* Map FE-specific LUN ID into global one. */
13838 io->io_hdr.nexus.targ_mapped_lun =
13839 ctl_map_lun(io->io_hdr.nexus.targ_port, io->io_hdr.nexus.targ_lun);
13841 switch (io->io_hdr.io_type) {
13844 if (ctl_debug & CTL_DEBUG_CDB)
13846 ctl_enqueue_incoming(io);
13849 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13853 return (CTL_RETVAL_COMPLETE);
13856 #ifdef CTL_IO_DELAY
13858 ctl_done_timer_wakeup(void *arg)
13862 io = (union ctl_io *)arg;
13865 #endif /* CTL_IO_DELAY */
13868 ctl_done(union ctl_io *io)
13870 struct ctl_softc *ctl_softc;
13872 ctl_softc = control_softc;
13875 * Enable this to catch duplicate completion issues.
13878 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13879 printf("%s: type %d msg %d cdb %x iptl: "
13880 "%d:%d:%d:%d tag 0x%04x "
13881 "flag %#x status %x\n",
13883 io->io_hdr.io_type,
13884 io->io_hdr.msg_type,
13886 io->io_hdr.nexus.initid.id,
13887 io->io_hdr.nexus.targ_port,
13888 io->io_hdr.nexus.targ_target.id,
13889 io->io_hdr.nexus.targ_lun,
13890 (io->io_hdr.io_type ==
13892 io->taskio.tag_num :
13893 io->scsiio.tag_num,
13895 io->io_hdr.status);
13897 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13901 * This is an internal copy of an I/O, and should not go through
13902 * the normal done processing logic.
13904 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13908 * We need to send a msg to the serializing shelf to finish the IO
13909 * as well. We don't send a finish message to the other shelf if
13910 * this is a task management command. Task management commands
13911 * aren't serialized in the OOA queue, but rather just executed on
13912 * both shelf controllers for commands that originated on that
13915 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)
13916 && (io->io_hdr.io_type != CTL_IO_TASK)) {
13917 union ctl_ha_msg msg_io;
13919 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO;
13920 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc;
13921 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io,
13922 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) {
13924 /* continue on to finish IO */
13926 #ifdef CTL_IO_DELAY
13927 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13928 struct ctl_lun *lun;
13930 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13932 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13934 struct ctl_lun *lun;
13936 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13939 && (lun->delay_info.done_delay > 0)) {
13940 struct callout *callout;
13942 callout = (struct callout *)&io->io_hdr.timer_bytes;
13943 callout_init(callout, /*mpsafe*/ 1);
13944 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13945 callout_reset(callout,
13946 lun->delay_info.done_delay * hz,
13947 ctl_done_timer_wakeup, io);
13948 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13949 lun->delay_info.done_delay = 0;
13953 #endif /* CTL_IO_DELAY */
13955 ctl_enqueue_done(io);
13959 ctl_isc(struct ctl_scsiio *ctsio)
13961 struct ctl_lun *lun;
13964 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13966 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0]));
13968 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n"));
13970 retval = lun->backend->data_submit((union ctl_io *)ctsio);
13977 ctl_work_thread(void *arg)
13979 struct ctl_thread *thr = (struct ctl_thread *)arg;
13980 struct ctl_softc *softc = thr->ctl_softc;
13984 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13990 * We handle the queues in this order:
13992 * - done queue (to free up resources, unblock other commands)
13996 * If those queues are empty, we break out of the loop and
13999 mtx_lock(&thr->queue_lock);
14000 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
14002 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
14003 mtx_unlock(&thr->queue_lock);
14004 ctl_handle_isc(io);
14007 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
14009 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
14010 /* clear any blocked commands, call fe_done */
14011 mtx_unlock(&thr->queue_lock);
14012 retval = ctl_process_done(io);
14015 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
14017 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
14018 mtx_unlock(&thr->queue_lock);
14019 if (io->io_hdr.io_type == CTL_IO_TASK)
14022 ctl_scsiio_precheck(softc, &io->scsiio);
14025 if (!ctl_pause_rtr) {
14026 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
14028 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
14029 mtx_unlock(&thr->queue_lock);
14030 retval = ctl_scsiio(&io->scsiio);
14031 if (retval != CTL_RETVAL_COMPLETE)
14032 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
14037 /* Sleep until we have something to do. */
14038 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
14043 ctl_lun_thread(void *arg)
14045 struct ctl_softc *softc = (struct ctl_softc *)arg;
14046 struct ctl_be_lun *be_lun;
14049 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
14053 mtx_lock(&softc->ctl_lock);
14054 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
14055 if (be_lun != NULL) {
14056 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
14057 mtx_unlock(&softc->ctl_lock);
14058 ctl_create_lun(be_lun);
14062 /* Sleep until we have something to do. */
14063 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
14064 PDROP | PRIBIO, "-", 0);
14069 ctl_enqueue_incoming(union ctl_io *io)
14071 struct ctl_softc *softc = control_softc;
14072 struct ctl_thread *thr;
14075 idx = (io->io_hdr.nexus.targ_port * 127 +
14076 io->io_hdr.nexus.initid.id) % worker_threads;
14077 thr = &softc->threads[idx];
14078 mtx_lock(&thr->queue_lock);
14079 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
14080 mtx_unlock(&thr->queue_lock);
14085 ctl_enqueue_rtr(union ctl_io *io)
14087 struct ctl_softc *softc = control_softc;
14088 struct ctl_thread *thr;
14090 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14091 mtx_lock(&thr->queue_lock);
14092 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
14093 mtx_unlock(&thr->queue_lock);
14098 ctl_enqueue_done(union ctl_io *io)
14100 struct ctl_softc *softc = control_softc;
14101 struct ctl_thread *thr;
14103 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14104 mtx_lock(&thr->queue_lock);
14105 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
14106 mtx_unlock(&thr->queue_lock);
14111 ctl_enqueue_isc(union ctl_io *io)
14113 struct ctl_softc *softc = control_softc;
14114 struct ctl_thread *thr;
14116 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14117 mtx_lock(&thr->queue_lock);
14118 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
14119 mtx_unlock(&thr->queue_lock);
14123 /* Initialization and failover */
14126 ctl_init_isc_msg(void)
14128 printf("CTL: Still calling this thing\n");
14133 * Initializes component into configuration defined by bootMode
14135 * returns hasc_Status:
14137 * ERROR - fatal error
14139 static ctl_ha_comp_status
14140 ctl_isc_init(struct ctl_ha_component *c)
14142 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
14149 * Starts component in state requested. If component starts successfully,
14150 * it must set its own state to the requestrd state
14151 * When requested state is HASC_STATE_HA, the component may refine it
14152 * by adding _SLAVE or _MASTER flags.
14153 * Currently allowed state transitions are:
14154 * UNKNOWN->HA - initial startup
14155 * UNKNOWN->SINGLE - initial startup when no parter detected
14156 * HA->SINGLE - failover
14157 * returns ctl_ha_comp_status:
14158 * OK - component successfully started in requested state
14159 * FAILED - could not start the requested state, failover may
14161 * ERROR - fatal error detected, no future startup possible
14163 static ctl_ha_comp_status
14164 ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state)
14166 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
14168 printf("%s: go\n", __func__);
14170 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap)
14171 if (c->state == CTL_HA_STATE_UNKNOWN ) {
14173 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
14174 != CTL_HA_STATUS_SUCCESS) {
14175 printf("ctl_isc_start: ctl_ha_msg_create failed.\n");
14176 ret = CTL_HA_COMP_STATUS_ERROR;
14178 } else if (CTL_HA_STATE_IS_HA(c->state)
14179 && CTL_HA_STATE_IS_SINGLE(state)){
14180 // HA->SINGLE transition
14184 printf("ctl_isc_start:Invalid state transition %X->%X\n",
14186 ret = CTL_HA_COMP_STATUS_ERROR;
14188 if (CTL_HA_STATE_IS_SINGLE(state))
14197 * Quiesce component
14198 * The component must clear any error conditions (set status to OK) and
14199 * prepare itself to another Start call
14200 * returns ctl_ha_comp_status:
14204 static ctl_ha_comp_status
14205 ctl_isc_quiesce(struct ctl_ha_component *c)
14207 int ret = CTL_HA_COMP_STATUS_OK;
14214 struct ctl_ha_component ctl_ha_component_ctlisc =
14217 .state = CTL_HA_STATE_UNKNOWN,
14218 .init = ctl_isc_init,
14219 .start = ctl_isc_start,
14220 .quiesce = ctl_isc_quiesce