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/kernel.h>
50 #include <sys/types.h>
51 #include <sys/kthread.h>
53 #include <sys/fcntl.h>
55 #include <sys/module.h>
56 #include <sys/mutex.h>
57 #include <sys/condvar.h>
58 #include <sys/malloc.h>
60 #include <sys/ioccom.h>
61 #include <sys/queue.h>
64 #include <sys/endian.h>
65 #include <sys/sysctl.h>
68 #include <cam/scsi/scsi_all.h>
69 #include <cam/scsi/scsi_da.h>
70 #include <cam/ctl/ctl_io.h>
71 #include <cam/ctl/ctl.h>
72 #include <cam/ctl/ctl_frontend.h>
73 #include <cam/ctl/ctl_frontend_internal.h>
74 #include <cam/ctl/ctl_util.h>
75 #include <cam/ctl/ctl_backend.h>
76 #include <cam/ctl/ctl_ioctl.h>
77 #include <cam/ctl/ctl_ha.h>
78 #include <cam/ctl/ctl_private.h>
79 #include <cam/ctl/ctl_debug.h>
80 #include <cam/ctl/ctl_scsi_all.h>
81 #include <cam/ctl/ctl_error.h>
83 struct ctl_softc *control_softc = NULL;
86 * Size and alignment macros needed for Copan-specific HA hardware. These
87 * can go away when the HA code is re-written, and uses busdma for any
90 #define CTL_ALIGN_8B(target, source, type) \
91 if (((uint32_t)source & 0x7) != 0) \
92 target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\
94 target = (type)source;
96 #define CTL_SIZE_8B(target, size) \
97 if ((size & 0x7) != 0) \
98 target = size + (0x8 - (size & 0x7)); \
102 #define CTL_ALIGN_8B_MARGIN 16
105 * Template mode pages.
109 * Note that these are default values only. The actual values will be
110 * filled in when the user does a mode sense.
112 static struct copan_debugconf_subpage debugconf_page_default = {
113 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
114 DBGCNF_SUBPAGE_CODE, /* subpage */
115 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
116 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
117 DBGCNF_VERSION, /* page_version */
118 {CTL_TIME_IO_DEFAULT_SECS>>8,
119 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */
122 static struct copan_debugconf_subpage debugconf_page_changeable = {
123 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
124 DBGCNF_SUBPAGE_CODE, /* subpage */
125 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
126 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
127 0, /* page_version */
128 {0xff,0xff}, /* ctl_time_io_secs */
131 static struct scsi_da_rw_recovery_page rw_er_page_default = {
132 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
133 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
134 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE,
135 /*read_retry_count*/0,
136 /*correction_span*/0,
137 /*head_offset_count*/0,
138 /*data_strobe_offset_cnt*/0,
140 /*write_retry_count*/0,
142 /*recovery_time_limit*/{0, 0},
145 static struct scsi_da_rw_recovery_page rw_er_page_changeable = {
146 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
147 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
149 /*read_retry_count*/0,
150 /*correction_span*/0,
151 /*head_offset_count*/0,
152 /*data_strobe_offset_cnt*/0,
154 /*write_retry_count*/0,
156 /*recovery_time_limit*/{0, 0},
159 static struct scsi_format_page format_page_default = {
160 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
161 /*page_length*/sizeof(struct scsi_format_page) - 2,
162 /*tracks_per_zone*/ {0, 0},
163 /*alt_sectors_per_zone*/ {0, 0},
164 /*alt_tracks_per_zone*/ {0, 0},
165 /*alt_tracks_per_lun*/ {0, 0},
166 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
167 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
168 /*bytes_per_sector*/ {0, 0},
169 /*interleave*/ {0, 0},
170 /*track_skew*/ {0, 0},
171 /*cylinder_skew*/ {0, 0},
173 /*reserved*/ {0, 0, 0}
176 static struct scsi_format_page format_page_changeable = {
177 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
178 /*page_length*/sizeof(struct scsi_format_page) - 2,
179 /*tracks_per_zone*/ {0, 0},
180 /*alt_sectors_per_zone*/ {0, 0},
181 /*alt_tracks_per_zone*/ {0, 0},
182 /*alt_tracks_per_lun*/ {0, 0},
183 /*sectors_per_track*/ {0, 0},
184 /*bytes_per_sector*/ {0, 0},
185 /*interleave*/ {0, 0},
186 /*track_skew*/ {0, 0},
187 /*cylinder_skew*/ {0, 0},
189 /*reserved*/ {0, 0, 0}
192 static struct scsi_rigid_disk_page rigid_disk_page_default = {
193 /*page_code*/SMS_RIGID_DISK_PAGE,
194 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
195 /*cylinders*/ {0, 0, 0},
196 /*heads*/ CTL_DEFAULT_HEADS,
197 /*start_write_precomp*/ {0, 0, 0},
198 /*start_reduced_current*/ {0, 0, 0},
199 /*step_rate*/ {0, 0},
200 /*landing_zone_cylinder*/ {0, 0, 0},
201 /*rpl*/ SRDP_RPL_DISABLED,
202 /*rotational_offset*/ 0,
204 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
205 CTL_DEFAULT_ROTATION_RATE & 0xff},
209 static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
210 /*page_code*/SMS_RIGID_DISK_PAGE,
211 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
212 /*cylinders*/ {0, 0, 0},
214 /*start_write_precomp*/ {0, 0, 0},
215 /*start_reduced_current*/ {0, 0, 0},
216 /*step_rate*/ {0, 0},
217 /*landing_zone_cylinder*/ {0, 0, 0},
219 /*rotational_offset*/ 0,
221 /*rotation_rate*/ {0, 0},
225 static struct scsi_caching_page caching_page_default = {
226 /*page_code*/SMS_CACHING_PAGE,
227 /*page_length*/sizeof(struct scsi_caching_page) - 2,
228 /*flags1*/ SCP_DISC | SCP_WCE,
230 /*disable_pf_transfer_len*/ {0xff, 0xff},
231 /*min_prefetch*/ {0, 0},
232 /*max_prefetch*/ {0xff, 0xff},
233 /*max_pf_ceiling*/ {0xff, 0xff},
235 /*cache_segments*/ 0,
236 /*cache_seg_size*/ {0, 0},
238 /*non_cache_seg_size*/ {0, 0, 0}
241 static struct scsi_caching_page caching_page_changeable = {
242 /*page_code*/SMS_CACHING_PAGE,
243 /*page_length*/sizeof(struct scsi_caching_page) - 2,
244 /*flags1*/ SCP_WCE | SCP_RCD,
246 /*disable_pf_transfer_len*/ {0, 0},
247 /*min_prefetch*/ {0, 0},
248 /*max_prefetch*/ {0, 0},
249 /*max_pf_ceiling*/ {0, 0},
251 /*cache_segments*/ 0,
252 /*cache_seg_size*/ {0, 0},
254 /*non_cache_seg_size*/ {0, 0, 0}
257 static struct scsi_control_page control_page_default = {
258 /*page_code*/SMS_CONTROL_MODE_PAGE,
259 /*page_length*/sizeof(struct scsi_control_page) - 2,
261 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED,
264 /*aen_holdoff_period*/{0, 0},
265 /*busy_timeout_period*/{0, 0},
266 /*extended_selftest_completion_time*/{0, 0}
269 static struct scsi_control_page control_page_changeable = {
270 /*page_code*/SMS_CONTROL_MODE_PAGE,
271 /*page_length*/sizeof(struct scsi_control_page) - 2,
273 /*queue_flags*/SCP_QUEUE_ALG_MASK,
274 /*eca_and_aen*/SCP_SWP,
276 /*aen_holdoff_period*/{0, 0},
277 /*busy_timeout_period*/{0, 0},
278 /*extended_selftest_completion_time*/{0, 0}
281 static struct scsi_info_exceptions_page ie_page_default = {
282 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
283 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
284 /*info_flags*/SIEP_FLAGS_DEXCPT,
286 /*interval_timer*/{0, 0, 0, 0},
287 /*report_count*/{0, 0, 0, 0}
290 static struct scsi_info_exceptions_page ie_page_changeable = {
291 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
292 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
295 /*interval_timer*/{0, 0, 0, 0},
296 /*report_count*/{0, 0, 0, 0}
299 static struct scsi_logical_block_provisioning_page lbp_page_default = {
300 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
301 /*subpage_code*/0x02,
302 /*page_length*/{0, sizeof(struct scsi_logical_block_provisioning_page) - 4},
304 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
308 static struct scsi_logical_block_provisioning_page lbp_page_changeable = {
309 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
310 /*subpage_code*/0x02,
311 /*page_length*/{0, sizeof(struct scsi_logical_block_provisioning_page) - 4},
313 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
318 * XXX KDM move these into the softc.
320 static int rcv_sync_msg;
321 static int persis_offset;
322 static uint8_t ctl_pause_rtr;
323 static int ctl_is_single = 1;
325 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
326 static int worker_threads = -1;
327 TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads);
328 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
329 &worker_threads, 1, "Number of worker threads");
330 static int ctl_debug = CTL_DEBUG_NONE;
331 TUNABLE_INT("kern.cam.ctl.debug", &ctl_debug);
332 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN,
333 &ctl_debug, 0, "Enabled debug flags");
336 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
337 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
338 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0),
339 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
341 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 10
343 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
345 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
346 static int ctl_init(void);
347 void ctl_shutdown(void);
348 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
349 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
350 static void ctl_ioctl_online(void *arg);
351 static void ctl_ioctl_offline(void *arg);
352 static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id);
353 static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id);
354 static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio);
355 static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
356 static int ctl_ioctl_submit_wait(union ctl_io *io);
357 static void ctl_ioctl_datamove(union ctl_io *io);
358 static void ctl_ioctl_done(union ctl_io *io);
359 static void ctl_ioctl_hard_startstop_callback(void *arg,
360 struct cfi_metatask *metatask);
361 static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask);
362 static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
363 struct ctl_ooa *ooa_hdr,
364 struct ctl_ooa_entry *kern_entries);
365 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
367 static uint32_t ctl_map_lun(int port_num, uint32_t lun);
368 static uint32_t ctl_map_lun_back(int port_num, uint32_t lun);
370 static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port,
371 uint32_t targ_target, uint32_t targ_lun,
373 static void ctl_kfree_io(union ctl_io *io);
375 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
376 struct ctl_be_lun *be_lun, struct ctl_id target_id);
377 static int ctl_free_lun(struct ctl_lun *lun);
378 static void ctl_create_lun(struct ctl_be_lun *be_lun);
380 static void ctl_failover_change_pages(struct ctl_softc *softc,
381 struct ctl_scsiio *ctsio, int master);
384 static int ctl_do_mode_select(union ctl_io *io);
385 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
386 uint64_t res_key, uint64_t sa_res_key,
387 uint8_t type, uint32_t residx,
388 struct ctl_scsiio *ctsio,
389 struct scsi_per_res_out *cdb,
390 struct scsi_per_res_out_parms* param);
391 static void ctl_pro_preempt_other(struct ctl_lun *lun,
392 union ctl_ha_msg *msg);
393 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
394 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
395 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
396 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
397 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
398 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
399 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
401 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
403 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
404 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
405 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
406 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
407 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
408 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2);
409 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
410 union ctl_io *pending_io, union ctl_io *ooa_io);
411 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
412 union ctl_io *starting_io);
413 static int ctl_check_blocked(struct ctl_lun *lun);
414 static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc,
416 const struct ctl_cmd_entry *entry,
417 struct ctl_scsiio *ctsio);
418 //static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc);
419 static void ctl_failover(void);
420 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
421 struct ctl_scsiio *ctsio);
422 static int ctl_scsiio(struct ctl_scsiio *ctsio);
424 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
425 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
426 ctl_ua_type ua_type);
427 static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io,
428 ctl_ua_type ua_type);
429 static int ctl_abort_task(union ctl_io *io);
430 static int ctl_abort_task_set(union ctl_io *io);
431 static int ctl_i_t_nexus_reset(union ctl_io *io);
432 static void ctl_run_task(union ctl_io *io);
434 static void ctl_datamove_timer_wakeup(void *arg);
435 static void ctl_done_timer_wakeup(void *arg);
436 #endif /* CTL_IO_DELAY */
438 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
439 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
440 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
441 static void ctl_datamove_remote_write(union ctl_io *io);
442 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
443 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
444 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
445 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
446 ctl_ha_dt_cb callback);
447 static void ctl_datamove_remote_read(union ctl_io *io);
448 static void ctl_datamove_remote(union ctl_io *io);
449 static int ctl_process_done(union ctl_io *io);
450 static void ctl_lun_thread(void *arg);
451 static void ctl_work_thread(void *arg);
452 static void ctl_enqueue_incoming(union ctl_io *io);
453 static void ctl_enqueue_rtr(union ctl_io *io);
454 static void ctl_enqueue_done(union ctl_io *io);
455 static void ctl_enqueue_isc(union ctl_io *io);
456 static const struct ctl_cmd_entry *
457 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
458 static const struct ctl_cmd_entry *
459 ctl_validate_command(struct ctl_scsiio *ctsio);
460 static int ctl_cmd_applicable(uint8_t lun_type,
461 const struct ctl_cmd_entry *entry);
464 * Load the serialization table. This isn't very pretty, but is probably
465 * the easiest way to do it.
467 #include "ctl_ser_table.c"
470 * We only need to define open, close and ioctl routines for this driver.
472 static struct cdevsw ctl_cdevsw = {
473 .d_version = D_VERSION,
476 .d_close = ctl_close,
477 .d_ioctl = ctl_ioctl,
482 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
483 MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests");
485 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
487 static moduledata_t ctl_moduledata = {
489 ctl_module_event_handler,
493 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
494 MODULE_VERSION(ctl, 1);
496 static struct ctl_frontend ioctl_frontend =
502 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
503 union ctl_ha_msg *msg_info)
505 struct ctl_scsiio *ctsio;
507 if (msg_info->hdr.original_sc == NULL) {
508 printf("%s: original_sc == NULL!\n", __func__);
509 /* XXX KDM now what? */
513 ctsio = &msg_info->hdr.original_sc->scsiio;
514 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
515 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
516 ctsio->io_hdr.status = msg_info->hdr.status;
517 ctsio->scsi_status = msg_info->scsi.scsi_status;
518 ctsio->sense_len = msg_info->scsi.sense_len;
519 ctsio->sense_residual = msg_info->scsi.sense_residual;
520 ctsio->residual = msg_info->scsi.residual;
521 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
522 sizeof(ctsio->sense_data));
523 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
524 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen));
525 ctl_enqueue_isc((union ctl_io *)ctsio);
529 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
530 union ctl_ha_msg *msg_info)
532 struct ctl_scsiio *ctsio;
534 if (msg_info->hdr.serializing_sc == NULL) {
535 printf("%s: serializing_sc == NULL!\n", __func__);
536 /* XXX KDM now what? */
540 ctsio = &msg_info->hdr.serializing_sc->scsiio;
543 * Attempt to catch the situation where an I/O has
544 * been freed, and we're using it again.
546 if (ctsio->io_hdr.io_type == 0xff) {
547 union ctl_io *tmp_io;
548 tmp_io = (union ctl_io *)ctsio;
549 printf("%s: %p use after free!\n", __func__,
551 printf("%s: type %d msg %d cdb %x iptl: "
552 "%d:%d:%d:%d tag 0x%04x "
553 "flag %#x status %x\n",
555 tmp_io->io_hdr.io_type,
556 tmp_io->io_hdr.msg_type,
557 tmp_io->scsiio.cdb[0],
558 tmp_io->io_hdr.nexus.initid.id,
559 tmp_io->io_hdr.nexus.targ_port,
560 tmp_io->io_hdr.nexus.targ_target.id,
561 tmp_io->io_hdr.nexus.targ_lun,
562 (tmp_io->io_hdr.io_type ==
564 tmp_io->taskio.tag_num :
565 tmp_io->scsiio.tag_num,
566 tmp_io->io_hdr.flags,
567 tmp_io->io_hdr.status);
570 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
571 ctl_enqueue_isc((union ctl_io *)ctsio);
575 * ISC (Inter Shelf Communication) event handler. Events from the HA
576 * subsystem come in here.
579 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
581 struct ctl_softc *ctl_softc;
583 struct ctl_prio *presio;
584 ctl_ha_status isc_status;
586 ctl_softc = control_softc;
591 printf("CTL: Isc Msg event %d\n", event);
593 if (event == CTL_HA_EVT_MSG_RECV) {
594 union ctl_ha_msg msg_info;
596 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
597 sizeof(msg_info), /*wait*/ 0);
599 printf("CTL: msg_type %d\n", msg_info.msg_type);
601 if (isc_status != 0) {
602 printf("Error receiving message, status = %d\n",
607 switch (msg_info.hdr.msg_type) {
608 case CTL_MSG_SERIALIZE:
610 printf("Serialize\n");
612 io = ctl_alloc_io((void *)ctl_softc->othersc_pool);
614 printf("ctl_isc_event_handler: can't allocate "
617 /* Need to set busy and send msg back */
618 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
619 msg_info.hdr.status = CTL_SCSI_ERROR;
620 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY;
621 msg_info.scsi.sense_len = 0;
622 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
623 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){
628 // populate ctsio from msg_info
629 io->io_hdr.io_type = CTL_IO_SCSI;
630 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
631 io->io_hdr.original_sc = msg_info.hdr.original_sc;
633 printf("pOrig %x\n", (int)msg_info.original_sc);
635 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
638 * If we're in serialization-only mode, we don't
639 * want to go through full done processing. Thus
642 * XXX KDM add another flag that is more specific.
644 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)
645 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
646 io->io_hdr.nexus = msg_info.hdr.nexus;
648 printf("targ %d, port %d, iid %d, lun %d\n",
649 io->io_hdr.nexus.targ_target.id,
650 io->io_hdr.nexus.targ_port,
651 io->io_hdr.nexus.initid.id,
652 io->io_hdr.nexus.targ_lun);
654 io->scsiio.tag_num = msg_info.scsi.tag_num;
655 io->scsiio.tag_type = msg_info.scsi.tag_type;
656 memcpy(io->scsiio.cdb, msg_info.scsi.cdb,
658 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
659 const struct ctl_cmd_entry *entry;
661 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
662 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
664 entry->flags & CTL_FLAG_DATA_MASK;
669 /* Performed on the Originating SC, XFER mode only */
670 case CTL_MSG_DATAMOVE: {
671 struct ctl_sg_entry *sgl;
674 io = msg_info.hdr.original_sc;
676 printf("%s: original_sc == NULL!\n", __func__);
677 /* XXX KDM do something here */
680 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
681 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
683 * Keep track of this, we need to send it back over
684 * when the datamove is complete.
686 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
688 if (msg_info.dt.sg_sequence == 0) {
690 * XXX KDM we use the preallocated S/G list
691 * here, but we'll need to change this to
692 * dynamic allocation if we need larger S/G
695 if (msg_info.dt.kern_sg_entries >
696 sizeof(io->io_hdr.remote_sglist) /
697 sizeof(io->io_hdr.remote_sglist[0])) {
698 printf("%s: number of S/G entries "
699 "needed %u > allocated num %zd\n",
701 msg_info.dt.kern_sg_entries,
702 sizeof(io->io_hdr.remote_sglist)/
703 sizeof(io->io_hdr.remote_sglist[0]));
706 * XXX KDM send a message back to
707 * the other side to shut down the
708 * DMA. The error will come back
709 * through via the normal channel.
713 sgl = io->io_hdr.remote_sglist;
715 sizeof(io->io_hdr.remote_sglist));
717 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
719 io->scsiio.kern_sg_entries =
720 msg_info.dt.kern_sg_entries;
721 io->scsiio.rem_sg_entries =
722 msg_info.dt.kern_sg_entries;
723 io->scsiio.kern_data_len =
724 msg_info.dt.kern_data_len;
725 io->scsiio.kern_total_len =
726 msg_info.dt.kern_total_len;
727 io->scsiio.kern_data_resid =
728 msg_info.dt.kern_data_resid;
729 io->scsiio.kern_rel_offset =
730 msg_info.dt.kern_rel_offset;
732 * Clear out per-DMA flags.
734 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK;
736 * Add per-DMA flags that are set for this
737 * particular DMA request.
739 io->io_hdr.flags |= msg_info.dt.flags &
742 sgl = (struct ctl_sg_entry *)
743 io->scsiio.kern_data_ptr;
745 for (i = msg_info.dt.sent_sg_entries, j = 0;
746 i < (msg_info.dt.sent_sg_entries +
747 msg_info.dt.cur_sg_entries); i++, j++) {
748 sgl[i].addr = msg_info.dt.sg_list[j].addr;
749 sgl[i].len = msg_info.dt.sg_list[j].len;
752 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n",
754 msg_info.dt.sg_list[j].addr,
755 msg_info.dt.sg_list[j].len,
756 sgl[i].addr, sgl[i].len, j, i);
760 memcpy(&sgl[msg_info.dt.sent_sg_entries],
762 sizeof(*sgl) * msg_info.dt.cur_sg_entries);
766 * If this is the last piece of the I/O, we've got
767 * the full S/G list. Queue processing in the thread.
768 * Otherwise wait for the next piece.
770 if (msg_info.dt.sg_last != 0)
774 /* Performed on the Serializing (primary) SC, XFER mode only */
775 case CTL_MSG_DATAMOVE_DONE: {
776 if (msg_info.hdr.serializing_sc == NULL) {
777 printf("%s: serializing_sc == NULL!\n",
779 /* XXX KDM now what? */
783 * We grab the sense information here in case
784 * there was a failure, so we can return status
785 * back to the initiator.
787 io = msg_info.hdr.serializing_sc;
788 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
789 io->io_hdr.status = msg_info.hdr.status;
790 io->scsiio.scsi_status = msg_info.scsi.scsi_status;
791 io->scsiio.sense_len = msg_info.scsi.sense_len;
792 io->scsiio.sense_residual =msg_info.scsi.sense_residual;
793 io->io_hdr.port_status = msg_info.scsi.fetd_status;
794 io->scsiio.residual = msg_info.scsi.residual;
795 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data,
796 sizeof(io->scsiio.sense_data));
801 /* Preformed on Originating SC, SER_ONLY mode */
803 io = msg_info.hdr.original_sc;
805 printf("%s: Major Bummer\n", __func__);
809 printf("pOrig %x\n",(int) ctsio);
812 io->io_hdr.msg_type = CTL_MSG_R2R;
813 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
818 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
820 * Performed on the Originating (i.e. secondary) SC in XFER
823 case CTL_MSG_FINISH_IO:
824 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER)
825 ctl_isc_handler_finish_xfer(ctl_softc,
828 ctl_isc_handler_finish_ser_only(ctl_softc,
832 /* Preformed on Originating SC */
833 case CTL_MSG_BAD_JUJU:
834 io = msg_info.hdr.original_sc;
836 printf("%s: Bad JUJU!, original_sc is NULL!\n",
840 ctl_copy_sense_data(&msg_info, io);
842 * IO should have already been cleaned up on other
843 * SC so clear this flag so we won't send a message
844 * back to finish the IO there.
846 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
847 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
849 /* io = msg_info.hdr.serializing_sc; */
850 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
854 /* Handle resets sent from the other side */
855 case CTL_MSG_MANAGE_TASKS: {
856 struct ctl_taskio *taskio;
857 taskio = (struct ctl_taskio *)ctl_alloc_io(
858 (void *)ctl_softc->othersc_pool);
859 if (taskio == NULL) {
860 printf("ctl_isc_event_handler: can't allocate "
863 /* should I just call the proper reset func
867 ctl_zero_io((union ctl_io *)taskio);
868 taskio->io_hdr.io_type = CTL_IO_TASK;
869 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
870 taskio->io_hdr.nexus = msg_info.hdr.nexus;
871 taskio->task_action = msg_info.task.task_action;
872 taskio->tag_num = msg_info.task.tag_num;
873 taskio->tag_type = msg_info.task.tag_type;
875 taskio->io_hdr.start_time = time_uptime;
876 getbintime(&taskio->io_hdr.start_bt);
878 cs_prof_gettime(&taskio->io_hdr.start_ticks);
880 #endif /* CTL_TIME_IO */
881 ctl_run_task((union ctl_io *)taskio);
884 /* Persistent Reserve action which needs attention */
885 case CTL_MSG_PERS_ACTION:
886 presio = (struct ctl_prio *)ctl_alloc_io(
887 (void *)ctl_softc->othersc_pool);
888 if (presio == NULL) {
889 printf("ctl_isc_event_handler: can't allocate "
892 /* Need to set busy and send msg back */
895 ctl_zero_io((union ctl_io *)presio);
896 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
897 presio->pr_msg = msg_info.pr;
898 ctl_enqueue_isc((union ctl_io *)presio);
900 case CTL_MSG_SYNC_FE:
904 printf("How did I get here?\n");
906 } else if (event == CTL_HA_EVT_MSG_SENT) {
907 if (param != CTL_HA_STATUS_SUCCESS) {
908 printf("Bad status from ctl_ha_msg_send status %d\n",
912 } else if (event == CTL_HA_EVT_DISCONNECT) {
913 printf("CTL: Got a disconnect from Isc\n");
916 printf("ctl_isc_event_handler: Unknown event %d\n", event);
925 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
927 struct scsi_sense_data *sense;
929 sense = &dest->scsiio.sense_data;
930 bcopy(&src->scsi.sense_data, sense, sizeof(*sense));
931 dest->scsiio.scsi_status = src->scsi.scsi_status;
932 dest->scsiio.sense_len = src->scsi.sense_len;
933 dest->io_hdr.status = src->hdr.status;
939 struct ctl_softc *softc;
940 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool;
941 struct ctl_port *port;
943 int i, error, retval;
950 control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
952 softc = control_softc;
954 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
957 softc->dev->si_drv1 = softc;
960 * By default, return a "bad LUN" peripheral qualifier for unknown
961 * LUNs. The user can override this default using the tunable or
962 * sysctl. See the comment in ctl_inquiry_std() for more details.
964 softc->inquiry_pq_no_lun = 1;
965 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun",
966 &softc->inquiry_pq_no_lun);
967 sysctl_ctx_init(&softc->sysctl_ctx);
968 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
969 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
970 CTLFLAG_RD, 0, "CAM Target Layer");
972 if (softc->sysctl_tree == NULL) {
973 printf("%s: unable to allocate sysctl tree\n", __func__);
974 destroy_dev(softc->dev);
975 free(control_softc, M_DEVBUF);
976 control_softc = NULL;
980 SYSCTL_ADD_INT(&softc->sysctl_ctx,
981 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
982 "inquiry_pq_no_lun", CTLFLAG_RW,
983 &softc->inquiry_pq_no_lun, 0,
984 "Report no lun possible for invalid LUNs");
986 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
987 mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF);
988 softc->open_count = 0;
991 * Default to actually sending a SYNCHRONIZE CACHE command down to
994 softc->flags = CTL_FLAG_REAL_SYNC;
997 * In Copan's HA scheme, the "master" and "slave" roles are
998 * figured out through the slot the controller is in. Although it
999 * is an active/active system, someone has to be in charge.
1002 scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id);
1006 softc->flags |= CTL_FLAG_MASTER_SHELF;
1009 persis_offset = CTL_MAX_INITIATORS;
1012 * XXX KDM need to figure out where we want to get our target ID
1013 * and WWID. Is it different on each port?
1015 softc->target.id = 0;
1016 softc->target.wwid[0] = 0x12345678;
1017 softc->target.wwid[1] = 0x87654321;
1018 STAILQ_INIT(&softc->lun_list);
1019 STAILQ_INIT(&softc->pending_lun_queue);
1020 STAILQ_INIT(&softc->fe_list);
1021 STAILQ_INIT(&softc->port_list);
1022 STAILQ_INIT(&softc->be_list);
1023 STAILQ_INIT(&softc->io_pools);
1024 ctl_tpc_init(softc);
1026 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL,
1027 &internal_pool)!= 0){
1028 printf("ctl: can't allocate %d entry internal pool, "
1029 "exiting\n", CTL_POOL_ENTRIES_INTERNAL);
1033 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY,
1034 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) {
1035 printf("ctl: can't allocate %d entry emergency pool, "
1036 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY);
1037 ctl_pool_free(internal_pool);
1041 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC,
1044 printf("ctl: can't allocate %d entry other SC pool, "
1045 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1046 ctl_pool_free(internal_pool);
1047 ctl_pool_free(emergency_pool);
1051 softc->internal_pool = internal_pool;
1052 softc->emergency_pool = emergency_pool;
1053 softc->othersc_pool = other_pool;
1055 if (worker_threads <= 0)
1056 worker_threads = max(1, mp_ncpus / 4);
1057 if (worker_threads > CTL_MAX_THREADS)
1058 worker_threads = CTL_MAX_THREADS;
1060 for (i = 0; i < worker_threads; i++) {
1061 struct ctl_thread *thr = &softc->threads[i];
1063 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1064 thr->ctl_softc = softc;
1065 STAILQ_INIT(&thr->incoming_queue);
1066 STAILQ_INIT(&thr->rtr_queue);
1067 STAILQ_INIT(&thr->done_queue);
1068 STAILQ_INIT(&thr->isc_queue);
1070 error = kproc_kthread_add(ctl_work_thread, thr,
1071 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1073 printf("error creating CTL work thread!\n");
1074 ctl_pool_free(internal_pool);
1075 ctl_pool_free(emergency_pool);
1076 ctl_pool_free(other_pool);
1080 error = kproc_kthread_add(ctl_lun_thread, softc,
1081 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
1083 printf("error creating CTL lun thread!\n");
1084 ctl_pool_free(internal_pool);
1085 ctl_pool_free(emergency_pool);
1086 ctl_pool_free(other_pool);
1090 printf("ctl: CAM Target Layer loaded\n");
1093 * Initialize the ioctl front end.
1095 ctl_frontend_register(&ioctl_frontend);
1096 port = &softc->ioctl_info.port;
1097 port->frontend = &ioctl_frontend;
1098 sprintf(softc->ioctl_info.port_name, "ioctl");
1099 port->port_type = CTL_PORT_IOCTL;
1100 port->num_requested_ctl_io = 100;
1101 port->port_name = softc->ioctl_info.port_name;
1102 port->port_online = ctl_ioctl_online;
1103 port->port_offline = ctl_ioctl_offline;
1104 port->onoff_arg = &softc->ioctl_info;
1105 port->lun_enable = ctl_ioctl_lun_enable;
1106 port->lun_disable = ctl_ioctl_lun_disable;
1107 port->targ_lun_arg = &softc->ioctl_info;
1108 port->fe_datamove = ctl_ioctl_datamove;
1109 port->fe_done = ctl_ioctl_done;
1110 port->max_targets = 15;
1111 port->max_target_id = 15;
1113 if (ctl_port_register(&softc->ioctl_info.port,
1114 (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) {
1115 printf("ctl: ioctl front end registration failed, will "
1116 "continue anyway\n");
1120 if (sizeof(struct callout) > CTL_TIMER_BYTES) {
1121 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n",
1122 sizeof(struct callout), CTL_TIMER_BYTES);
1125 #endif /* CTL_IO_DELAY */
1133 struct ctl_softc *softc;
1134 struct ctl_lun *lun, *next_lun;
1135 struct ctl_io_pool *pool;
1137 softc = (struct ctl_softc *)control_softc;
1139 if (ctl_port_deregister(&softc->ioctl_info.port) != 0)
1140 printf("ctl: ioctl front end deregistration failed\n");
1142 mtx_lock(&softc->ctl_lock);
1147 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
1148 next_lun = STAILQ_NEXT(lun, links);
1152 mtx_unlock(&softc->ctl_lock);
1154 ctl_frontend_deregister(&ioctl_frontend);
1157 * This will rip the rug out from under any FETDs or anyone else
1158 * that has a pool allocated. Since we increment our module
1159 * refcount any time someone outside the main CTL module allocates
1160 * a pool, we shouldn't have any problems here. The user won't be
1161 * able to unload the CTL module until client modules have
1162 * successfully unloaded.
1164 while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL)
1165 ctl_pool_free(pool);
1168 ctl_shutdown_thread(softc->work_thread);
1169 mtx_destroy(&softc->queue_lock);
1172 ctl_tpc_shutdown(softc);
1173 mtx_destroy(&softc->pool_lock);
1174 mtx_destroy(&softc->ctl_lock);
1176 destroy_dev(softc->dev);
1178 sysctl_ctx_free(&softc->sysctl_ctx);
1180 free(control_softc, M_DEVBUF);
1181 control_softc = NULL;
1184 printf("ctl: CAM Target Layer unloaded\n");
1188 ctl_module_event_handler(module_t mod, int what, void *arg)
1193 return (ctl_init());
1197 return (EOPNOTSUPP);
1202 * XXX KDM should we do some access checks here? Bump a reference count to
1203 * prevent a CTL module from being unloaded while someone has it open?
1206 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1212 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1218 ctl_port_enable(ctl_port_type port_type)
1220 struct ctl_softc *softc;
1221 struct ctl_port *port;
1223 if (ctl_is_single == 0) {
1224 union ctl_ha_msg msg_info;
1228 printf("%s: HA mode, synchronizing frontend enable\n",
1231 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE;
1232 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1233 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) {
1234 printf("Sync msg send error retval %d\n", isc_retval);
1236 if (!rcv_sync_msg) {
1237 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
1238 sizeof(msg_info), 1);
1241 printf("CTL:Frontend Enable\n");
1243 printf("%s: single mode, skipping frontend synchronization\n",
1248 softc = control_softc;
1250 STAILQ_FOREACH(port, &softc->port_list, links) {
1251 if (port_type & port->port_type)
1254 printf("port %d\n", port->targ_port);
1256 ctl_port_online(port);
1264 ctl_port_disable(ctl_port_type port_type)
1266 struct ctl_softc *softc;
1267 struct ctl_port *port;
1269 softc = control_softc;
1271 STAILQ_FOREACH(port, &softc->port_list, links) {
1272 if (port_type & port->port_type)
1273 ctl_port_offline(port);
1280 * Returns 0 for success, 1 for failure.
1281 * Currently the only failure mode is if there aren't enough entries
1282 * allocated. So, in case of a failure, look at num_entries_dropped,
1283 * reallocate and try again.
1286 ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced,
1287 int *num_entries_filled, int *num_entries_dropped,
1288 ctl_port_type port_type, int no_virtual)
1290 struct ctl_softc *softc;
1291 struct ctl_port *port;
1292 int entries_dropped, entries_filled;
1296 softc = control_softc;
1300 entries_dropped = 0;
1303 mtx_lock(&softc->ctl_lock);
1304 STAILQ_FOREACH(port, &softc->port_list, links) {
1305 struct ctl_port_entry *entry;
1307 if ((port->port_type & port_type) == 0)
1310 if ((no_virtual != 0)
1311 && (port->virtual_port != 0))
1314 if (entries_filled >= num_entries_alloced) {
1318 entry = &entries[i];
1320 entry->port_type = port->port_type;
1321 strlcpy(entry->port_name, port->port_name,
1322 sizeof(entry->port_name));
1323 entry->physical_port = port->physical_port;
1324 entry->virtual_port = port->virtual_port;
1325 entry->wwnn = port->wwnn;
1326 entry->wwpn = port->wwpn;
1332 mtx_unlock(&softc->ctl_lock);
1334 if (entries_dropped > 0)
1337 *num_entries_dropped = entries_dropped;
1338 *num_entries_filled = entries_filled;
1344 ctl_ioctl_online(void *arg)
1346 struct ctl_ioctl_info *ioctl_info;
1348 ioctl_info = (struct ctl_ioctl_info *)arg;
1350 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED;
1354 ctl_ioctl_offline(void *arg)
1356 struct ctl_ioctl_info *ioctl_info;
1358 ioctl_info = (struct ctl_ioctl_info *)arg;
1360 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED;
1364 * Remove an initiator by port number and initiator ID.
1365 * Returns 0 for success, -1 for failure.
1368 ctl_remove_initiator(struct ctl_port *port, int iid)
1370 struct ctl_softc *softc = control_softc;
1372 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1374 if (iid > CTL_MAX_INIT_PER_PORT) {
1375 printf("%s: initiator ID %u > maximun %u!\n",
1376 __func__, iid, CTL_MAX_INIT_PER_PORT);
1380 mtx_lock(&softc->ctl_lock);
1381 port->wwpn_iid[iid].in_use--;
1382 port->wwpn_iid[iid].last_use = time_uptime;
1383 mtx_unlock(&softc->ctl_lock);
1389 * Add an initiator to the initiator map.
1390 * Returns iid for success, < 0 for failure.
1393 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
1395 struct ctl_softc *softc = control_softc;
1399 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1401 if (iid >= CTL_MAX_INIT_PER_PORT) {
1402 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
1403 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
1408 mtx_lock(&softc->ctl_lock);
1410 if (iid < 0 && (wwpn != 0 || name != NULL)) {
1411 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1412 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
1416 if (name != NULL && port->wwpn_iid[i].name != NULL &&
1417 strcmp(name, port->wwpn_iid[i].name) == 0) {
1425 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1426 if (port->wwpn_iid[i].in_use == 0 &&
1427 port->wwpn_iid[i].wwpn == 0 &&
1428 port->wwpn_iid[i].name == NULL) {
1437 best_time = INT32_MAX;
1438 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1439 if (port->wwpn_iid[i].in_use == 0) {
1440 if (port->wwpn_iid[i].last_use < best_time) {
1442 best_time = port->wwpn_iid[i].last_use;
1450 mtx_unlock(&softc->ctl_lock);
1455 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
1457 * This is not an error yet.
1459 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
1461 printf("%s: port %d iid %u WWPN %#jx arrived"
1462 " again\n", __func__, port->targ_port,
1463 iid, (uintmax_t)wwpn);
1467 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
1468 strcmp(name, port->wwpn_iid[iid].name) == 0) {
1470 printf("%s: port %d iid %u name '%s' arrived"
1471 " again\n", __func__, port->targ_port,
1478 * This is an error, but what do we do about it? The
1479 * driver is telling us we have a new WWPN for this
1480 * initiator ID, so we pretty much need to use it.
1482 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
1483 " but WWPN %#jx '%s' is still at that address\n",
1484 __func__, port->targ_port, iid, wwpn, name,
1485 (uintmax_t)port->wwpn_iid[iid].wwpn,
1486 port->wwpn_iid[iid].name);
1489 * XXX KDM clear have_ca and ua_pending on each LUN for
1494 free(port->wwpn_iid[iid].name, M_CTL);
1495 port->wwpn_iid[iid].name = name;
1496 port->wwpn_iid[iid].wwpn = wwpn;
1497 port->wwpn_iid[iid].in_use++;
1498 mtx_unlock(&softc->ctl_lock);
1504 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
1508 switch (port->port_type) {
1511 struct scsi_transportid_fcp *id =
1512 (struct scsi_transportid_fcp *)buf;
1513 if (port->wwpn_iid[iid].wwpn == 0)
1515 memset(id, 0, sizeof(*id));
1516 id->format_protocol = SCSI_PROTO_FC;
1517 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
1518 return (sizeof(*id));
1520 case CTL_PORT_ISCSI:
1522 struct scsi_transportid_iscsi_port *id =
1523 (struct scsi_transportid_iscsi_port *)buf;
1524 if (port->wwpn_iid[iid].name == NULL)
1527 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
1529 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
1530 len = roundup2(min(len, 252), 4);
1531 scsi_ulto2b(len, id->additional_length);
1532 return (sizeof(*id) + len);
1536 struct scsi_transportid_sas *id =
1537 (struct scsi_transportid_sas *)buf;
1538 if (port->wwpn_iid[iid].wwpn == 0)
1540 memset(id, 0, sizeof(*id));
1541 id->format_protocol = SCSI_PROTO_SAS;
1542 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
1543 return (sizeof(*id));
1547 struct scsi_transportid_spi *id =
1548 (struct scsi_transportid_spi *)buf;
1549 memset(id, 0, sizeof(*id));
1550 id->format_protocol = SCSI_PROTO_SPI;
1551 scsi_ulto2b(iid, id->scsi_addr);
1552 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
1553 return (sizeof(*id));
1559 ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id)
1565 ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id)
1571 * Data movement routine for the CTL ioctl frontend port.
1574 ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio)
1576 struct ctl_sg_entry *ext_sglist, *kern_sglist;
1577 struct ctl_sg_entry ext_entry, kern_entry;
1578 int ext_sglen, ext_sg_entries, kern_sg_entries;
1579 int ext_sg_start, ext_offset;
1580 int len_to_copy, len_copied;
1581 int kern_watermark, ext_watermark;
1582 int ext_sglist_malloced;
1585 ext_sglist_malloced = 0;
1589 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n"));
1592 * If this flag is set, fake the data transfer.
1594 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) {
1595 ctsio->ext_data_filled = ctsio->ext_data_len;
1600 * To simplify things here, if we have a single buffer, stick it in
1601 * a S/G entry and just make it a single entry S/G list.
1603 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) {
1606 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist);
1608 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL,
1610 ext_sglist_malloced = 1;
1611 if (copyin(ctsio->ext_data_ptr, ext_sglist,
1613 ctl_set_internal_failure(ctsio,
1618 ext_sg_entries = ctsio->ext_sg_entries;
1620 for (i = 0; i < ext_sg_entries; i++) {
1621 if ((len_seen + ext_sglist[i].len) >=
1622 ctsio->ext_data_filled) {
1624 ext_offset = ctsio->ext_data_filled - len_seen;
1627 len_seen += ext_sglist[i].len;
1630 ext_sglist = &ext_entry;
1631 ext_sglist->addr = ctsio->ext_data_ptr;
1632 ext_sglist->len = ctsio->ext_data_len;
1635 ext_offset = ctsio->ext_data_filled;
1638 if (ctsio->kern_sg_entries > 0) {
1639 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
1640 kern_sg_entries = ctsio->kern_sg_entries;
1642 kern_sglist = &kern_entry;
1643 kern_sglist->addr = ctsio->kern_data_ptr;
1644 kern_sglist->len = ctsio->kern_data_len;
1645 kern_sg_entries = 1;
1650 ext_watermark = ext_offset;
1652 for (i = ext_sg_start, j = 0;
1653 i < ext_sg_entries && j < kern_sg_entries;) {
1654 uint8_t *ext_ptr, *kern_ptr;
1656 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark,
1657 kern_sglist[j].len - kern_watermark);
1659 ext_ptr = (uint8_t *)ext_sglist[i].addr;
1660 ext_ptr = ext_ptr + ext_watermark;
1661 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
1665 panic("need to implement bus address support");
1667 kern_ptr = bus_to_virt(kern_sglist[j].addr);
1670 kern_ptr = (uint8_t *)kern_sglist[j].addr;
1671 kern_ptr = kern_ptr + kern_watermark;
1673 kern_watermark += len_to_copy;
1674 ext_watermark += len_to_copy;
1676 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
1678 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1679 "bytes to user\n", len_to_copy));
1680 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1681 "to %p\n", kern_ptr, ext_ptr));
1682 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) {
1683 ctl_set_internal_failure(ctsio,
1689 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1690 "bytes from user\n", len_to_copy));
1691 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1692 "to %p\n", ext_ptr, kern_ptr));
1693 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){
1694 ctl_set_internal_failure(ctsio,
1701 len_copied += len_to_copy;
1703 if (ext_sglist[i].len == ext_watermark) {
1708 if (kern_sglist[j].len == kern_watermark) {
1714 ctsio->ext_data_filled += len_copied;
1716 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, "
1717 "kern_sg_entries: %d\n", ext_sg_entries,
1719 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, "
1720 "kern_data_len = %d\n", ctsio->ext_data_len,
1721 ctsio->kern_data_len));
1724 /* XXX KDM set residual?? */
1727 if (ext_sglist_malloced != 0)
1728 free(ext_sglist, M_CTL);
1730 return (CTL_RETVAL_COMPLETE);
1734 * Serialize a command that went down the "wrong" side, and so was sent to
1735 * this controller for execution. The logic is a little different than the
1736 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
1737 * sent back to the other side, but in the success case, we execute the
1738 * command on this side (XFER mode) or tell the other side to execute it
1742 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
1744 struct ctl_softc *ctl_softc;
1745 union ctl_ha_msg msg_info;
1746 struct ctl_lun *lun;
1750 ctl_softc = control_softc;
1752 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
1753 lun = ctl_softc->ctl_luns[targ_lun];
1757 * Why isn't LUN defined? The other side wouldn't
1758 * send a cmd if the LUN is undefined.
1760 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__);
1762 /* "Logical unit not supported" */
1763 ctl_set_sense_data(&msg_info.scsi.sense_data,
1765 /*sense_format*/SSD_TYPE_NONE,
1766 /*current_error*/ 1,
1767 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1772 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1773 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1774 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1775 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1776 msg_info.hdr.serializing_sc = NULL;
1777 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1778 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1779 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1785 mtx_lock(&lun->lun_lock);
1786 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1788 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
1789 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
1791 case CTL_ACTION_BLOCK:
1792 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
1793 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
1796 case CTL_ACTION_PASS:
1797 case CTL_ACTION_SKIP:
1798 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
1799 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
1800 ctl_enqueue_rtr((union ctl_io *)ctsio);
1803 /* send msg back to other side */
1804 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1805 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
1806 msg_info.hdr.msg_type = CTL_MSG_R2R;
1808 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc);
1810 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1811 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1815 case CTL_ACTION_OVERLAP:
1816 /* OVERLAPPED COMMANDS ATTEMPTED */
1817 ctl_set_sense_data(&msg_info.scsi.sense_data,
1819 /*sense_format*/SSD_TYPE_NONE,
1820 /*current_error*/ 1,
1821 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1826 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1827 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1828 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1829 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1830 msg_info.hdr.serializing_sc = NULL;
1831 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1833 printf("BAD JUJU:Major Bummer Overlap\n");
1835 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1837 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1838 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1841 case CTL_ACTION_OVERLAP_TAG:
1842 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */
1843 ctl_set_sense_data(&msg_info.scsi.sense_data,
1845 /*sense_format*/SSD_TYPE_NONE,
1846 /*current_error*/ 1,
1847 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1849 /*ascq*/ ctsio->tag_num & 0xff,
1852 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1853 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1854 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1855 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1856 msg_info.hdr.serializing_sc = NULL;
1857 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1859 printf("BAD JUJU:Major Bummer Overlap Tag\n");
1861 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1863 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1864 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1867 case CTL_ACTION_ERROR:
1869 /* "Internal target failure" */
1870 ctl_set_sense_data(&msg_info.scsi.sense_data,
1872 /*sense_format*/SSD_TYPE_NONE,
1873 /*current_error*/ 1,
1874 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
1879 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1880 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1881 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1882 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1883 msg_info.hdr.serializing_sc = NULL;
1884 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1886 printf("BAD JUJU:Major Bummer HW Error\n");
1888 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1890 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1891 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1895 mtx_unlock(&lun->lun_lock);
1900 ctl_ioctl_submit_wait(union ctl_io *io)
1902 struct ctl_fe_ioctl_params params;
1903 ctl_fe_ioctl_state last_state;
1908 bzero(¶ms, sizeof(params));
1910 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF);
1911 cv_init(¶ms.sem, "ctlioccv");
1912 params.state = CTL_IOCTL_INPROG;
1913 last_state = params.state;
1915 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms;
1917 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n"));
1919 /* This shouldn't happen */
1920 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE)
1926 mtx_lock(¶ms.ioctl_mtx);
1928 * Check the state here, and don't sleep if the state has
1929 * already changed (i.e. wakeup has already occured, but we
1930 * weren't waiting yet).
1932 if (params.state == last_state) {
1933 /* XXX KDM cv_wait_sig instead? */
1934 cv_wait(¶ms.sem, ¶ms.ioctl_mtx);
1936 last_state = params.state;
1938 switch (params.state) {
1939 case CTL_IOCTL_INPROG:
1940 /* Why did we wake up? */
1941 /* XXX KDM error here? */
1942 mtx_unlock(¶ms.ioctl_mtx);
1944 case CTL_IOCTL_DATAMOVE:
1945 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n"));
1948 * change last_state back to INPROG to avoid
1949 * deadlock on subsequent data moves.
1951 params.state = last_state = CTL_IOCTL_INPROG;
1953 mtx_unlock(¶ms.ioctl_mtx);
1954 ctl_ioctl_do_datamove(&io->scsiio);
1956 * Note that in some cases, most notably writes,
1957 * this will queue the I/O and call us back later.
1958 * In other cases, generally reads, this routine
1959 * will immediately call back and wake us up,
1960 * probably using our own context.
1962 io->scsiio.be_move_done(io);
1964 case CTL_IOCTL_DONE:
1965 mtx_unlock(¶ms.ioctl_mtx);
1966 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n"));
1970 mtx_unlock(¶ms.ioctl_mtx);
1971 /* XXX KDM error here? */
1974 } while (done == 0);
1976 mtx_destroy(¶ms.ioctl_mtx);
1977 cv_destroy(¶ms.sem);
1979 return (CTL_RETVAL_COMPLETE);
1983 ctl_ioctl_datamove(union ctl_io *io)
1985 struct ctl_fe_ioctl_params *params;
1987 params = (struct ctl_fe_ioctl_params *)
1988 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1990 mtx_lock(¶ms->ioctl_mtx);
1991 params->state = CTL_IOCTL_DATAMOVE;
1992 cv_broadcast(¶ms->sem);
1993 mtx_unlock(¶ms->ioctl_mtx);
1997 ctl_ioctl_done(union ctl_io *io)
1999 struct ctl_fe_ioctl_params *params;
2001 params = (struct ctl_fe_ioctl_params *)
2002 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2004 mtx_lock(¶ms->ioctl_mtx);
2005 params->state = CTL_IOCTL_DONE;
2006 cv_broadcast(¶ms->sem);
2007 mtx_unlock(¶ms->ioctl_mtx);
2011 ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask)
2013 struct ctl_fe_ioctl_startstop_info *sd_info;
2015 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg;
2017 sd_info->hs_info.status = metatask->status;
2018 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns;
2019 sd_info->hs_info.luns_complete =
2020 metatask->taskinfo.startstop.luns_complete;
2021 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed;
2023 cv_broadcast(&sd_info->sem);
2027 ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask)
2029 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info;
2031 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg;
2033 mtx_lock(fe_bbr_info->lock);
2034 fe_bbr_info->bbr_info->status = metatask->status;
2035 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2036 fe_bbr_info->wakeup_done = 1;
2037 mtx_unlock(fe_bbr_info->lock);
2039 cv_broadcast(&fe_bbr_info->sem);
2043 * Returns 0 for success, errno for failure.
2046 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2047 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2054 mtx_lock(&lun->lun_lock);
2055 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2056 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2058 struct ctl_ooa_entry *entry;
2061 * If we've got more than we can fit, just count the
2062 * remaining entries.
2064 if (*cur_fill_num >= ooa_hdr->alloc_num)
2067 entry = &kern_entries[*cur_fill_num];
2069 entry->tag_num = io->scsiio.tag_num;
2070 entry->lun_num = lun->lun;
2072 entry->start_bt = io->io_hdr.start_bt;
2074 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2075 entry->cdb_len = io->scsiio.cdb_len;
2076 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2077 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2079 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2080 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2082 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2083 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2085 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2086 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2088 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2089 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2091 mtx_unlock(&lun->lun_lock);
2097 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2098 size_t error_str_len)
2102 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2104 if (copyin(user_addr, kptr, len) != 0) {
2105 snprintf(error_str, error_str_len, "Error copying %d bytes "
2106 "from user address %p to kernel address %p", len,
2116 ctl_free_args(int num_args, struct ctl_be_arg *args)
2123 for (i = 0; i < num_args; i++) {
2124 free(args[i].kname, M_CTL);
2125 free(args[i].kvalue, M_CTL);
2131 static struct ctl_be_arg *
2132 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
2133 char *error_str, size_t error_str_len)
2135 struct ctl_be_arg *args;
2138 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
2139 error_str, error_str_len);
2144 for (i = 0; i < num_args; i++) {
2145 args[i].kname = NULL;
2146 args[i].kvalue = NULL;
2149 for (i = 0; i < num_args; i++) {
2152 args[i].kname = ctl_copyin_alloc(args[i].name,
2153 args[i].namelen, error_str, error_str_len);
2154 if (args[i].kname == NULL)
2157 if (args[i].kname[args[i].namelen - 1] != '\0') {
2158 snprintf(error_str, error_str_len, "Argument %d "
2159 "name is not NUL-terminated", i);
2163 if (args[i].flags & CTL_BEARG_RD) {
2164 tmpptr = ctl_copyin_alloc(args[i].value,
2165 args[i].vallen, error_str, error_str_len);
2168 if ((args[i].flags & CTL_BEARG_ASCII)
2169 && (tmpptr[args[i].vallen - 1] != '\0')) {
2170 snprintf(error_str, error_str_len, "Argument "
2171 "%d value is not NUL-terminated", i);
2174 args[i].kvalue = tmpptr;
2176 args[i].kvalue = malloc(args[i].vallen,
2177 M_CTL, M_WAITOK | M_ZERO);
2184 ctl_free_args(num_args, args);
2190 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2194 for (i = 0; i < num_args; i++) {
2195 if (args[i].flags & CTL_BEARG_WR)
2196 copyout(args[i].kvalue, args[i].value, args[i].vallen);
2201 * Escape characters that are illegal or not recommended in XML.
2204 ctl_sbuf_printf_esc(struct sbuf *sb, char *str)
2210 for (; *str; str++) {
2213 retval = sbuf_printf(sb, "&");
2216 retval = sbuf_printf(sb, ">");
2219 retval = sbuf_printf(sb, "<");
2222 retval = sbuf_putc(sb, *str);
2235 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2237 struct scsi_vpd_id_descriptor *desc;
2240 if (id == NULL || id->len < 4)
2242 desc = (struct scsi_vpd_id_descriptor *)id->data;
2243 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2244 case SVPD_ID_TYPE_T10:
2245 sbuf_printf(sb, "t10.");
2247 case SVPD_ID_TYPE_EUI64:
2248 sbuf_printf(sb, "eui.");
2250 case SVPD_ID_TYPE_NAA:
2251 sbuf_printf(sb, "naa.");
2253 case SVPD_ID_TYPE_SCSI_NAME:
2256 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2257 case SVPD_ID_CODESET_BINARY:
2258 for (i = 0; i < desc->length; i++)
2259 sbuf_printf(sb, "%02x", desc->identifier[i]);
2261 case SVPD_ID_CODESET_ASCII:
2262 sbuf_printf(sb, "%.*s", (int)desc->length,
2263 (char *)desc->identifier);
2265 case SVPD_ID_CODESET_UTF8:
2266 sbuf_printf(sb, "%s", (char *)desc->identifier);
2272 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2275 struct ctl_softc *softc;
2278 softc = control_softc;
2288 * If we haven't been "enabled", don't allow any SCSI I/O
2291 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) {
2296 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref);
2298 printf("ctl_ioctl: can't allocate ctl_io!\n");
2304 * Need to save the pool reference so it doesn't get
2305 * spammed by the user's ctl_io.
2307 pool_tmp = io->io_hdr.pool;
2309 memcpy(io, (void *)addr, sizeof(*io));
2311 io->io_hdr.pool = pool_tmp;
2313 * No status yet, so make sure the status is set properly.
2315 io->io_hdr.status = CTL_STATUS_NONE;
2318 * The user sets the initiator ID, target and LUN IDs.
2320 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port;
2321 io->io_hdr.flags |= CTL_FLAG_USER_REQ;
2322 if ((io->io_hdr.io_type == CTL_IO_SCSI)
2323 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED))
2324 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++;
2326 retval = ctl_ioctl_submit_wait(io);
2333 memcpy((void *)addr, io, sizeof(*io));
2335 /* return this to our pool */
2340 case CTL_ENABLE_PORT:
2341 case CTL_DISABLE_PORT:
2342 case CTL_SET_PORT_WWNS: {
2343 struct ctl_port *port;
2344 struct ctl_port_entry *entry;
2346 entry = (struct ctl_port_entry *)addr;
2348 mtx_lock(&softc->ctl_lock);
2349 STAILQ_FOREACH(port, &softc->port_list, links) {
2355 if ((entry->port_type == CTL_PORT_NONE)
2356 && (entry->targ_port == port->targ_port)) {
2358 * If the user only wants to enable or
2359 * disable or set WWNs on a specific port,
2360 * do the operation and we're done.
2364 } else if (entry->port_type & port->port_type) {
2366 * Compare the user's type mask with the
2367 * particular frontend type to see if we
2374 * Make sure the user isn't trying to set
2375 * WWNs on multiple ports at the same time.
2377 if (cmd == CTL_SET_PORT_WWNS) {
2378 printf("%s: Can't set WWNs on "
2379 "multiple ports\n", __func__);
2386 * XXX KDM we have to drop the lock here,
2387 * because the online/offline operations
2388 * can potentially block. We need to
2389 * reference count the frontends so they
2392 mtx_unlock(&softc->ctl_lock);
2394 if (cmd == CTL_ENABLE_PORT) {
2395 struct ctl_lun *lun;
2397 STAILQ_FOREACH(lun, &softc->lun_list,
2399 port->lun_enable(port->targ_lun_arg,
2404 ctl_port_online(port);
2405 } else if (cmd == CTL_DISABLE_PORT) {
2406 struct ctl_lun *lun;
2408 ctl_port_offline(port);
2410 STAILQ_FOREACH(lun, &softc->lun_list,
2419 mtx_lock(&softc->ctl_lock);
2421 if (cmd == CTL_SET_PORT_WWNS)
2422 ctl_port_set_wwns(port,
2423 (entry->flags & CTL_PORT_WWNN_VALID) ?
2425 (entry->flags & CTL_PORT_WWPN_VALID) ?
2426 1 : 0, entry->wwpn);
2431 mtx_unlock(&softc->ctl_lock);
2434 case CTL_GET_PORT_LIST: {
2435 struct ctl_port *port;
2436 struct ctl_port_list *list;
2439 list = (struct ctl_port_list *)addr;
2441 if (list->alloc_len != (list->alloc_num *
2442 sizeof(struct ctl_port_entry))) {
2443 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != "
2444 "alloc_num %u * sizeof(struct ctl_port_entry) "
2445 "%zu\n", __func__, list->alloc_len,
2446 list->alloc_num, sizeof(struct ctl_port_entry));
2452 list->dropped_num = 0;
2454 mtx_lock(&softc->ctl_lock);
2455 STAILQ_FOREACH(port, &softc->port_list, links) {
2456 struct ctl_port_entry entry, *list_entry;
2458 if (list->fill_num >= list->alloc_num) {
2459 list->dropped_num++;
2463 entry.port_type = port->port_type;
2464 strlcpy(entry.port_name, port->port_name,
2465 sizeof(entry.port_name));
2466 entry.targ_port = port->targ_port;
2467 entry.physical_port = port->physical_port;
2468 entry.virtual_port = port->virtual_port;
2469 entry.wwnn = port->wwnn;
2470 entry.wwpn = port->wwpn;
2471 if (port->status & CTL_PORT_STATUS_ONLINE)
2476 list_entry = &list->entries[i];
2478 retval = copyout(&entry, list_entry, sizeof(entry));
2480 printf("%s: CTL_GET_PORT_LIST: copyout "
2481 "returned %d\n", __func__, retval);
2486 list->fill_len += sizeof(entry);
2488 mtx_unlock(&softc->ctl_lock);
2491 * If this is non-zero, we had a copyout fault, so there's
2492 * probably no point in attempting to set the status inside
2498 if (list->dropped_num > 0)
2499 list->status = CTL_PORT_LIST_NEED_MORE_SPACE;
2501 list->status = CTL_PORT_LIST_OK;
2504 case CTL_DUMP_OOA: {
2505 struct ctl_lun *lun;
2510 mtx_lock(&softc->ctl_lock);
2511 printf("Dumping OOA queues:\n");
2512 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2513 mtx_lock(&lun->lun_lock);
2514 for (io = (union ctl_io *)TAILQ_FIRST(
2515 &lun->ooa_queue); io != NULL;
2516 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2518 sbuf_new(&sb, printbuf, sizeof(printbuf),
2520 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ",
2524 CTL_FLAG_BLOCKED) ? "" : " BLOCKED",
2526 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
2528 CTL_FLAG_ABORT) ? " ABORT" : "",
2530 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : "");
2531 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
2533 printf("%s\n", sbuf_data(&sb));
2535 mtx_unlock(&lun->lun_lock);
2537 printf("OOA queues dump done\n");
2538 mtx_unlock(&softc->ctl_lock);
2542 struct ctl_lun *lun;
2543 struct ctl_ooa *ooa_hdr;
2544 struct ctl_ooa_entry *entries;
2545 uint32_t cur_fill_num;
2547 ooa_hdr = (struct ctl_ooa *)addr;
2549 if ((ooa_hdr->alloc_len == 0)
2550 || (ooa_hdr->alloc_num == 0)) {
2551 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2552 "must be non-zero\n", __func__,
2553 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2558 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2559 sizeof(struct ctl_ooa_entry))) {
2560 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2561 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2562 __func__, ooa_hdr->alloc_len,
2563 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2568 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2569 if (entries == NULL) {
2570 printf("%s: could not allocate %d bytes for OOA "
2571 "dump\n", __func__, ooa_hdr->alloc_len);
2576 mtx_lock(&softc->ctl_lock);
2577 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2578 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS)
2579 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2580 mtx_unlock(&softc->ctl_lock);
2581 free(entries, M_CTL);
2582 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2583 __func__, (uintmax_t)ooa_hdr->lun_num);
2590 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2591 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2592 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2598 mtx_unlock(&softc->ctl_lock);
2599 free(entries, M_CTL);
2603 lun = softc->ctl_luns[ooa_hdr->lun_num];
2605 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr,
2608 mtx_unlock(&softc->ctl_lock);
2610 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2611 ooa_hdr->fill_len = ooa_hdr->fill_num *
2612 sizeof(struct ctl_ooa_entry);
2613 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2615 printf("%s: error copying out %d bytes for OOA dump\n",
2616 __func__, ooa_hdr->fill_len);
2619 getbintime(&ooa_hdr->cur_bt);
2621 if (cur_fill_num > ooa_hdr->alloc_num) {
2622 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2623 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2625 ooa_hdr->dropped_num = 0;
2626 ooa_hdr->status = CTL_OOA_OK;
2629 free(entries, M_CTL);
2632 case CTL_CHECK_OOA: {
2634 struct ctl_lun *lun;
2635 struct ctl_ooa_info *ooa_info;
2638 ooa_info = (struct ctl_ooa_info *)addr;
2640 if (ooa_info->lun_id >= CTL_MAX_LUNS) {
2641 ooa_info->status = CTL_OOA_INVALID_LUN;
2644 mtx_lock(&softc->ctl_lock);
2645 lun = softc->ctl_luns[ooa_info->lun_id];
2647 mtx_unlock(&softc->ctl_lock);
2648 ooa_info->status = CTL_OOA_INVALID_LUN;
2651 mtx_lock(&lun->lun_lock);
2652 mtx_unlock(&softc->ctl_lock);
2653 ooa_info->num_entries = 0;
2654 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
2655 io != NULL; io = (union ctl_io *)TAILQ_NEXT(
2656 &io->io_hdr, ooa_links)) {
2657 ooa_info->num_entries++;
2659 mtx_unlock(&lun->lun_lock);
2661 ooa_info->status = CTL_OOA_SUCCESS;
2665 case CTL_HARD_START:
2666 case CTL_HARD_STOP: {
2667 struct ctl_fe_ioctl_startstop_info ss_info;
2668 struct cfi_metatask *metatask;
2671 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF);
2673 cv_init(&ss_info.sem, "hard start/stop cv" );
2675 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2676 if (metatask == NULL) {
2678 mtx_destroy(&hs_mtx);
2682 if (cmd == CTL_HARD_START)
2683 metatask->tasktype = CFI_TASK_STARTUP;
2685 metatask->tasktype = CFI_TASK_SHUTDOWN;
2687 metatask->callback = ctl_ioctl_hard_startstop_callback;
2688 metatask->callback_arg = &ss_info;
2690 cfi_action(metatask);
2692 /* Wait for the callback */
2694 cv_wait_sig(&ss_info.sem, &hs_mtx);
2695 mtx_unlock(&hs_mtx);
2698 * All information has been copied from the metatask by the
2699 * time cv_broadcast() is called, so we free the metatask here.
2701 cfi_free_metatask(metatask);
2703 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info));
2705 mtx_destroy(&hs_mtx);
2709 struct ctl_bbrread_info *bbr_info;
2710 struct ctl_fe_ioctl_bbrread_info fe_bbr_info;
2712 struct cfi_metatask *metatask;
2714 bbr_info = (struct ctl_bbrread_info *)addr;
2716 bzero(&fe_bbr_info, sizeof(fe_bbr_info));
2718 bzero(&bbr_mtx, sizeof(bbr_mtx));
2719 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF);
2721 fe_bbr_info.bbr_info = bbr_info;
2722 fe_bbr_info.lock = &bbr_mtx;
2724 cv_init(&fe_bbr_info.sem, "BBR read cv");
2725 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2727 if (metatask == NULL) {
2728 mtx_destroy(&bbr_mtx);
2729 cv_destroy(&fe_bbr_info.sem);
2733 metatask->tasktype = CFI_TASK_BBRREAD;
2734 metatask->callback = ctl_ioctl_bbrread_callback;
2735 metatask->callback_arg = &fe_bbr_info;
2736 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num;
2737 metatask->taskinfo.bbrread.lba = bbr_info->lba;
2738 metatask->taskinfo.bbrread.len = bbr_info->len;
2740 cfi_action(metatask);
2743 while (fe_bbr_info.wakeup_done == 0)
2744 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx);
2745 mtx_unlock(&bbr_mtx);
2747 bbr_info->status = metatask->status;
2748 bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2749 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status;
2750 memcpy(&bbr_info->sense_data,
2751 &metatask->taskinfo.bbrread.sense_data,
2752 ctl_min(sizeof(bbr_info->sense_data),
2753 sizeof(metatask->taskinfo.bbrread.sense_data)));
2755 cfi_free_metatask(metatask);
2757 mtx_destroy(&bbr_mtx);
2758 cv_destroy(&fe_bbr_info.sem);
2762 case CTL_DELAY_IO: {
2763 struct ctl_io_delay_info *delay_info;
2765 struct ctl_lun *lun;
2766 #endif /* CTL_IO_DELAY */
2768 delay_info = (struct ctl_io_delay_info *)addr;
2771 mtx_lock(&softc->ctl_lock);
2773 if ((delay_info->lun_id >= CTL_MAX_LUNS)
2774 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2775 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2777 lun = softc->ctl_luns[delay_info->lun_id];
2778 mtx_lock(&lun->lun_lock);
2780 delay_info->status = CTL_DELAY_STATUS_OK;
2782 switch (delay_info->delay_type) {
2783 case CTL_DELAY_TYPE_CONT:
2785 case CTL_DELAY_TYPE_ONESHOT:
2788 delay_info->status =
2789 CTL_DELAY_STATUS_INVALID_TYPE;
2793 switch (delay_info->delay_loc) {
2794 case CTL_DELAY_LOC_DATAMOVE:
2795 lun->delay_info.datamove_type =
2796 delay_info->delay_type;
2797 lun->delay_info.datamove_delay =
2798 delay_info->delay_secs;
2800 case CTL_DELAY_LOC_DONE:
2801 lun->delay_info.done_type =
2802 delay_info->delay_type;
2803 lun->delay_info.done_delay =
2804 delay_info->delay_secs;
2807 delay_info->status =
2808 CTL_DELAY_STATUS_INVALID_LOC;
2811 mtx_unlock(&lun->lun_lock);
2814 mtx_unlock(&softc->ctl_lock);
2816 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2817 #endif /* CTL_IO_DELAY */
2820 case CTL_REALSYNC_SET: {
2823 syncstate = (int *)addr;
2825 mtx_lock(&softc->ctl_lock);
2826 switch (*syncstate) {
2828 softc->flags &= ~CTL_FLAG_REAL_SYNC;
2831 softc->flags |= CTL_FLAG_REAL_SYNC;
2837 mtx_unlock(&softc->ctl_lock);
2840 case CTL_REALSYNC_GET: {
2843 syncstate = (int*)addr;
2845 mtx_lock(&softc->ctl_lock);
2846 if (softc->flags & CTL_FLAG_REAL_SYNC)
2850 mtx_unlock(&softc->ctl_lock);
2856 struct ctl_sync_info *sync_info;
2857 struct ctl_lun *lun;
2859 sync_info = (struct ctl_sync_info *)addr;
2861 mtx_lock(&softc->ctl_lock);
2862 lun = softc->ctl_luns[sync_info->lun_id];
2864 mtx_unlock(&softc->ctl_lock);
2865 sync_info->status = CTL_GS_SYNC_NO_LUN;
2868 * Get or set the sync interval. We're not bounds checking
2869 * in the set case, hopefully the user won't do something
2872 mtx_lock(&lun->lun_lock);
2873 mtx_unlock(&softc->ctl_lock);
2874 if (cmd == CTL_GETSYNC)
2875 sync_info->sync_interval = lun->sync_interval;
2877 lun->sync_interval = sync_info->sync_interval;
2878 mtx_unlock(&lun->lun_lock);
2880 sync_info->status = CTL_GS_SYNC_OK;
2884 case CTL_GETSTATS: {
2885 struct ctl_stats *stats;
2886 struct ctl_lun *lun;
2889 stats = (struct ctl_stats *)addr;
2891 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2893 stats->status = CTL_SS_NEED_MORE_SPACE;
2894 stats->num_luns = softc->num_luns;
2898 * XXX KDM no locking here. If the LUN list changes,
2899 * things can blow up.
2901 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
2902 i++, lun = STAILQ_NEXT(lun, links)) {
2903 retval = copyout(&lun->stats, &stats->lun_stats[i],
2904 sizeof(lun->stats));
2908 stats->num_luns = softc->num_luns;
2909 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2911 stats->status = CTL_SS_OK;
2913 stats->flags = CTL_STATS_FLAG_TIME_VALID;
2915 stats->flags = CTL_STATS_FLAG_NONE;
2917 getnanouptime(&stats->timestamp);
2920 case CTL_ERROR_INJECT: {
2921 struct ctl_error_desc *err_desc, *new_err_desc;
2922 struct ctl_lun *lun;
2924 err_desc = (struct ctl_error_desc *)addr;
2926 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2928 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2930 mtx_lock(&softc->ctl_lock);
2931 lun = softc->ctl_luns[err_desc->lun_id];
2933 mtx_unlock(&softc->ctl_lock);
2934 free(new_err_desc, M_CTL);
2935 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2936 __func__, (uintmax_t)err_desc->lun_id);
2940 mtx_lock(&lun->lun_lock);
2941 mtx_unlock(&softc->ctl_lock);
2944 * We could do some checking here to verify the validity
2945 * of the request, but given the complexity of error
2946 * injection requests, the checking logic would be fairly
2949 * For now, if the request is invalid, it just won't get
2950 * executed and might get deleted.
2952 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2955 * XXX KDM check to make sure the serial number is unique,
2956 * in case we somehow manage to wrap. That shouldn't
2957 * happen for a very long time, but it's the right thing to
2960 new_err_desc->serial = lun->error_serial;
2961 err_desc->serial = lun->error_serial;
2962 lun->error_serial++;
2964 mtx_unlock(&lun->lun_lock);
2967 case CTL_ERROR_INJECT_DELETE: {
2968 struct ctl_error_desc *delete_desc, *desc, *desc2;
2969 struct ctl_lun *lun;
2972 delete_desc = (struct ctl_error_desc *)addr;
2975 mtx_lock(&softc->ctl_lock);
2976 lun = softc->ctl_luns[delete_desc->lun_id];
2978 mtx_unlock(&softc->ctl_lock);
2979 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2980 __func__, (uintmax_t)delete_desc->lun_id);
2984 mtx_lock(&lun->lun_lock);
2985 mtx_unlock(&softc->ctl_lock);
2986 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2987 if (desc->serial != delete_desc->serial)
2990 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2995 mtx_unlock(&lun->lun_lock);
2996 if (delete_done == 0) {
2997 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2998 "error serial %ju on LUN %u\n", __func__,
2999 delete_desc->serial, delete_desc->lun_id);
3005 case CTL_DUMP_STRUCTS: {
3007 struct ctl_port *port;
3008 struct ctl_frontend *fe;
3010 mtx_lock(&softc->ctl_lock);
3011 printf("CTL Persistent Reservation information start:\n");
3012 for (i = 0; i < CTL_MAX_LUNS; i++) {
3013 struct ctl_lun *lun;
3015 lun = softc->ctl_luns[i];
3018 || ((lun->flags & CTL_LUN_DISABLED) != 0))
3021 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) {
3022 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
3023 idx = j * CTL_MAX_INIT_PER_PORT + k;
3024 if (lun->pr_keys[idx] == 0)
3026 printf(" LUN %d port %d iid %d key "
3028 (uintmax_t)lun->pr_keys[idx]);
3032 printf("CTL Persistent Reservation information end\n");
3033 printf("CTL Ports:\n");
3034 STAILQ_FOREACH(port, &softc->port_list, links) {
3035 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
3036 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
3037 port->frontend->name, port->port_type,
3038 port->physical_port, port->virtual_port,
3039 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
3040 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3041 if (port->wwpn_iid[j].in_use == 0 &&
3042 port->wwpn_iid[j].wwpn == 0 &&
3043 port->wwpn_iid[j].name == NULL)
3046 printf(" iid %u use %d WWPN %#jx '%s'\n",
3047 j, port->wwpn_iid[j].in_use,
3048 (uintmax_t)port->wwpn_iid[j].wwpn,
3049 port->wwpn_iid[j].name);
3052 printf("CTL Port information end\n");
3053 mtx_unlock(&softc->ctl_lock);
3055 * XXX KDM calling this without a lock. We'd likely want
3056 * to drop the lock before calling the frontend's dump
3059 printf("CTL Frontends:\n");
3060 STAILQ_FOREACH(fe, &softc->fe_list, links) {
3061 printf(" Frontend '%s'\n", fe->name);
3062 if (fe->fe_dump != NULL)
3065 printf("CTL Frontend information end\n");
3069 struct ctl_lun_req *lun_req;
3070 struct ctl_backend_driver *backend;
3072 lun_req = (struct ctl_lun_req *)addr;
3074 backend = ctl_backend_find(lun_req->backend);
3075 if (backend == NULL) {
3076 lun_req->status = CTL_LUN_ERROR;
3077 snprintf(lun_req->error_str,
3078 sizeof(lun_req->error_str),
3079 "Backend \"%s\" not found.",
3083 if (lun_req->num_be_args > 0) {
3084 lun_req->kern_be_args = ctl_copyin_args(
3085 lun_req->num_be_args,
3088 sizeof(lun_req->error_str));
3089 if (lun_req->kern_be_args == NULL) {
3090 lun_req->status = CTL_LUN_ERROR;
3095 retval = backend->ioctl(dev, cmd, addr, flag, td);
3097 if (lun_req->num_be_args > 0) {
3098 ctl_copyout_args(lun_req->num_be_args,
3099 lun_req->kern_be_args);
3100 ctl_free_args(lun_req->num_be_args,
3101 lun_req->kern_be_args);
3105 case CTL_LUN_LIST: {
3107 struct ctl_lun *lun;
3108 struct ctl_lun_list *list;
3109 struct ctl_option *opt;
3111 list = (struct ctl_lun_list *)addr;
3114 * Allocate a fixed length sbuf here, based on the length
3115 * of the user's buffer. We could allocate an auto-extending
3116 * buffer, and then tell the user how much larger our
3117 * amount of data is than his buffer, but that presents
3120 * 1. The sbuf(9) routines use a blocking malloc, and so
3121 * we can't hold a lock while calling them with an
3122 * auto-extending buffer.
3124 * 2. There is not currently a LUN reference counting
3125 * mechanism, outside of outstanding transactions on
3126 * the LUN's OOA queue. So a LUN could go away on us
3127 * while we're getting the LUN number, backend-specific
3128 * information, etc. Thus, given the way things
3129 * currently work, we need to hold the CTL lock while
3130 * grabbing LUN information.
3132 * So, from the user's standpoint, the best thing to do is
3133 * allocate what he thinks is a reasonable buffer length,
3134 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3135 * double the buffer length and try again. (And repeat
3136 * that until he succeeds.)
3138 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3140 list->status = CTL_LUN_LIST_ERROR;
3141 snprintf(list->error_str, sizeof(list->error_str),
3142 "Unable to allocate %d bytes for LUN list",
3147 sbuf_printf(sb, "<ctllunlist>\n");
3149 mtx_lock(&softc->ctl_lock);
3150 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3151 mtx_lock(&lun->lun_lock);
3152 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3153 (uintmax_t)lun->lun);
3156 * Bail out as soon as we see that we've overfilled
3162 retval = sbuf_printf(sb, "\t<backend_type>%s"
3163 "</backend_type>\n",
3164 (lun->backend == NULL) ? "none" :
3165 lun->backend->name);
3170 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3171 lun->be_lun->lun_type);
3176 if (lun->backend == NULL) {
3177 retval = sbuf_printf(sb, "</lun>\n");
3183 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3184 (lun->be_lun->maxlba > 0) ?
3185 lun->be_lun->maxlba + 1 : 0);
3190 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3191 lun->be_lun->blocksize);
3196 retval = sbuf_printf(sb, "\t<serial_number>");
3201 retval = ctl_sbuf_printf_esc(sb,
3202 lun->be_lun->serial_num);
3207 retval = sbuf_printf(sb, "</serial_number>\n");
3212 retval = sbuf_printf(sb, "\t<device_id>");
3217 retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id);
3222 retval = sbuf_printf(sb, "</device_id>\n");
3227 if (lun->backend->lun_info != NULL) {
3228 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3232 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3233 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3234 opt->name, opt->value, opt->name);
3239 retval = sbuf_printf(sb, "</lun>\n");
3243 mtx_unlock(&lun->lun_lock);
3246 mtx_unlock(&lun->lun_lock);
3247 mtx_unlock(&softc->ctl_lock);
3250 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3253 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3254 snprintf(list->error_str, sizeof(list->error_str),
3255 "Out of space, %d bytes is too small",
3262 retval = copyout(sbuf_data(sb), list->lun_xml,
3265 list->fill_len = sbuf_len(sb) + 1;
3266 list->status = CTL_LUN_LIST_OK;
3271 struct ctl_iscsi *ci;
3272 struct ctl_frontend *fe;
3274 ci = (struct ctl_iscsi *)addr;
3276 fe = ctl_frontend_find("iscsi");
3278 ci->status = CTL_ISCSI_ERROR;
3279 snprintf(ci->error_str, sizeof(ci->error_str),
3280 "Frontend \"iscsi\" not found.");
3284 retval = fe->ioctl(dev, cmd, addr, flag, td);
3287 case CTL_PORT_REQ: {
3288 struct ctl_req *req;
3289 struct ctl_frontend *fe;
3291 req = (struct ctl_req *)addr;
3293 fe = ctl_frontend_find(req->driver);
3295 req->status = CTL_LUN_ERROR;
3296 snprintf(req->error_str, sizeof(req->error_str),
3297 "Frontend \"%s\" not found.", req->driver);
3300 if (req->num_args > 0) {
3301 req->kern_args = ctl_copyin_args(req->num_args,
3302 req->args, req->error_str, sizeof(req->error_str));
3303 if (req->kern_args == NULL) {
3304 req->status = CTL_LUN_ERROR;
3309 retval = fe->ioctl(dev, cmd, addr, flag, td);
3311 if (req->num_args > 0) {
3312 ctl_copyout_args(req->num_args, req->kern_args);
3313 ctl_free_args(req->num_args, req->kern_args);
3317 case CTL_PORT_LIST: {
3319 struct ctl_port *port;
3320 struct ctl_lun_list *list;
3321 struct ctl_option *opt;
3324 list = (struct ctl_lun_list *)addr;
3326 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3328 list->status = CTL_LUN_LIST_ERROR;
3329 snprintf(list->error_str, sizeof(list->error_str),
3330 "Unable to allocate %d bytes for LUN list",
3335 sbuf_printf(sb, "<ctlportlist>\n");
3337 mtx_lock(&softc->ctl_lock);
3338 STAILQ_FOREACH(port, &softc->port_list, links) {
3339 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3340 (uintmax_t)port->targ_port);
3343 * Bail out as soon as we see that we've overfilled
3349 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3350 "</frontend_type>\n", port->frontend->name);
3354 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3359 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3360 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3364 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3369 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3370 port->physical_port);
3374 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3375 port->virtual_port);
3379 if (port->target_devid != NULL) {
3380 sbuf_printf(sb, "\t<target>");
3381 ctl_id_sbuf(port->target_devid, sb);
3382 sbuf_printf(sb, "</target>\n");
3385 if (port->port_devid != NULL) {
3386 sbuf_printf(sb, "\t<port>");
3387 ctl_id_sbuf(port->port_devid, sb);
3388 sbuf_printf(sb, "</port>\n");
3391 if (port->port_info != NULL) {
3392 retval = port->port_info(port->onoff_arg, sb);
3396 STAILQ_FOREACH(opt, &port->options, links) {
3397 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3398 opt->name, opt->value, opt->name);
3403 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3404 if (port->wwpn_iid[j].in_use == 0 ||
3405 (port->wwpn_iid[j].wwpn == 0 &&
3406 port->wwpn_iid[j].name == NULL))
3409 if (port->wwpn_iid[j].name != NULL)
3410 retval = sbuf_printf(sb,
3411 "\t<initiator>%u %s</initiator>\n",
3412 j, port->wwpn_iid[j].name);
3414 retval = sbuf_printf(sb,
3415 "\t<initiator>%u naa.%08jx</initiator>\n",
3416 j, port->wwpn_iid[j].wwpn);
3423 retval = sbuf_printf(sb, "</targ_port>\n");
3427 mtx_unlock(&softc->ctl_lock);
3430 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3433 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3434 snprintf(list->error_str, sizeof(list->error_str),
3435 "Out of space, %d bytes is too small",
3442 retval = copyout(sbuf_data(sb), list->lun_xml,
3445 list->fill_len = sbuf_len(sb) + 1;
3446 list->status = CTL_LUN_LIST_OK;
3451 /* XXX KDM should we fix this? */
3453 struct ctl_backend_driver *backend;
3460 * We encode the backend type as the ioctl type for backend
3461 * ioctls. So parse it out here, and then search for a
3462 * backend of this type.
3464 type = _IOC_TYPE(cmd);
3466 STAILQ_FOREACH(backend, &softc->be_list, links) {
3467 if (backend->type == type) {
3473 printf("ctl: unknown ioctl command %#lx or backend "
3478 retval = backend->ioctl(dev, cmd, addr, flag, td);
3488 ctl_get_initindex(struct ctl_nexus *nexus)
3490 if (nexus->targ_port < CTL_MAX_PORTS)
3491 return (nexus->initid.id +
3492 (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3494 return (nexus->initid.id +
3495 ((nexus->targ_port - CTL_MAX_PORTS) *
3496 CTL_MAX_INIT_PER_PORT));
3500 ctl_get_resindex(struct ctl_nexus *nexus)
3502 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3506 ctl_port_idx(int port_num)
3508 if (port_num < CTL_MAX_PORTS)
3511 return(port_num - CTL_MAX_PORTS);
3515 ctl_map_lun(int port_num, uint32_t lun_id)
3517 struct ctl_port *port;
3519 port = control_softc->ctl_ports[ctl_port_idx(port_num)];
3521 return (UINT32_MAX);
3522 if (port->lun_map == NULL)
3524 return (port->lun_map(port->targ_lun_arg, lun_id));
3528 ctl_map_lun_back(int port_num, uint32_t lun_id)
3530 struct ctl_port *port;
3533 port = control_softc->ctl_ports[ctl_port_idx(port_num)];
3534 if (port->lun_map == NULL)
3536 for (i = 0; i < CTL_MAX_LUNS; i++) {
3537 if (port->lun_map(port->targ_lun_arg, i) == lun_id)
3540 return (UINT32_MAX);
3544 * Note: This only works for bitmask sizes that are at least 32 bits, and
3545 * that are a power of 2.
3548 ctl_ffz(uint32_t *mask, uint32_t size)
3550 uint32_t num_chunks, num_pieces;
3553 num_chunks = (size >> 5);
3554 if (num_chunks == 0)
3556 num_pieces = ctl_min((sizeof(uint32_t) * 8), size);
3558 for (i = 0; i < num_chunks; i++) {
3559 for (j = 0; j < num_pieces; j++) {
3560 if ((mask[i] & (1 << j)) == 0)
3561 return ((i << 5) + j);
3569 ctl_set_mask(uint32_t *mask, uint32_t bit)
3571 uint32_t chunk, piece;
3574 piece = bit % (sizeof(uint32_t) * 8);
3576 if ((mask[chunk] & (1 << piece)) != 0)
3579 mask[chunk] |= (1 << piece);
3585 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3587 uint32_t chunk, piece;
3590 piece = bit % (sizeof(uint32_t) * 8);
3592 if ((mask[chunk] & (1 << piece)) == 0)
3595 mask[chunk] &= ~(1 << piece);
3601 ctl_is_set(uint32_t *mask, uint32_t bit)
3603 uint32_t chunk, piece;
3606 piece = bit % (sizeof(uint32_t) * 8);
3608 if ((mask[chunk] & (1 << piece)) == 0)
3616 * The bus, target and lun are optional, they can be filled in later.
3617 * can_wait is used to determine whether we can wait on the malloc or not.
3620 ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target,
3621 uint32_t targ_lun, int can_wait)
3626 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK);
3628 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT);
3631 io->io_hdr.io_type = io_type;
3632 io->io_hdr.targ_port = targ_port;
3634 * XXX KDM this needs to change/go away. We need to move
3635 * to a preallocated pool of ctl_scsiio structures.
3637 io->io_hdr.nexus.targ_target.id = targ_target;
3638 io->io_hdr.nexus.targ_lun = targ_lun;
3645 ctl_kfree_io(union ctl_io *io)
3652 * ctl_softc, pool_type, total_ctl_io are passed in.
3653 * npool is passed out.
3656 ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type,
3657 uint32_t total_ctl_io, struct ctl_io_pool **npool)
3660 union ctl_io *cur_io, *next_io;
3661 struct ctl_io_pool *pool;
3666 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3673 pool->type = pool_type;
3674 pool->ctl_softc = ctl_softc;
3676 mtx_lock(&ctl_softc->pool_lock);
3677 pool->id = ctl_softc->cur_pool_id++;
3678 mtx_unlock(&ctl_softc->pool_lock);
3680 pool->flags = CTL_POOL_FLAG_NONE;
3681 pool->refcount = 1; /* Reference for validity. */
3682 STAILQ_INIT(&pool->free_queue);
3685 * XXX KDM other options here:
3686 * - allocate a page at a time
3687 * - allocate one big chunk of memory.
3688 * Page allocation might work well, but would take a little more
3691 for (i = 0; i < total_ctl_io; i++) {
3692 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTLIO,
3694 if (cur_io == NULL) {
3698 cur_io->io_hdr.pool = pool;
3699 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links);
3700 pool->total_ctl_io++;
3701 pool->free_ctl_io++;
3705 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3706 cur_io != NULL; cur_io = next_io) {
3707 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr,
3709 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr,
3711 free(cur_io, M_CTLIO);
3717 mtx_lock(&ctl_softc->pool_lock);
3718 ctl_softc->num_pools++;
3719 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links);
3721 * Increment our usage count if this is an external consumer, so we
3722 * can't get unloaded until the external consumer (most likely a
3723 * FETD) unloads and frees his pool.
3725 * XXX KDM will this increment the caller's module use count, or
3729 if ((pool_type != CTL_POOL_EMERGENCY)
3730 && (pool_type != CTL_POOL_INTERNAL)
3731 && (pool_type != CTL_POOL_4OTHERSC))
3735 mtx_unlock(&ctl_softc->pool_lock);
3745 ctl_pool_acquire(struct ctl_io_pool *pool)
3748 mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED);
3750 if (pool->flags & CTL_POOL_FLAG_INVALID)
3759 ctl_pool_release(struct ctl_io_pool *pool)
3761 struct ctl_softc *ctl_softc = pool->ctl_softc;
3764 mtx_assert(&ctl_softc->pool_lock, MA_OWNED);
3766 if (--pool->refcount != 0)
3769 while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) {
3770 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr,
3775 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links);
3776 ctl_softc->num_pools--;
3779 * XXX KDM will this decrement the caller's usage count or mine?
3782 if ((pool->type != CTL_POOL_EMERGENCY)
3783 && (pool->type != CTL_POOL_INTERNAL)
3784 && (pool->type != CTL_POOL_4OTHERSC))
3792 ctl_pool_free(struct ctl_io_pool *pool)
3794 struct ctl_softc *ctl_softc;
3799 ctl_softc = pool->ctl_softc;
3800 mtx_lock(&ctl_softc->pool_lock);
3801 pool->flags |= CTL_POOL_FLAG_INVALID;
3802 ctl_pool_release(pool);
3803 mtx_unlock(&ctl_softc->pool_lock);
3807 * This routine does not block (except for spinlocks of course).
3808 * It tries to allocate a ctl_io union from the caller's pool as quickly as
3812 ctl_alloc_io(void *pool_ref)
3815 struct ctl_softc *ctl_softc;
3816 struct ctl_io_pool *pool, *npool;
3817 struct ctl_io_pool *emergency_pool;
3819 pool = (struct ctl_io_pool *)pool_ref;
3822 printf("%s: pool is NULL\n", __func__);
3826 emergency_pool = NULL;
3828 ctl_softc = pool->ctl_softc;
3830 mtx_lock(&ctl_softc->pool_lock);
3832 * First, try to get the io structure from the user's pool.
3834 if (ctl_pool_acquire(pool) == 0) {
3835 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3837 STAILQ_REMOVE_HEAD(&pool->free_queue, links);
3838 pool->total_allocated++;
3839 pool->free_ctl_io--;
3840 mtx_unlock(&ctl_softc->pool_lock);
3843 ctl_pool_release(pool);
3846 * If he doesn't have any io structures left, search for an
3847 * emergency pool and grab one from there.
3849 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) {
3850 if (npool->type != CTL_POOL_EMERGENCY)
3853 if (ctl_pool_acquire(npool) != 0)
3856 emergency_pool = npool;
3858 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue);
3860 STAILQ_REMOVE_HEAD(&npool->free_queue, links);
3861 npool->total_allocated++;
3862 npool->free_ctl_io--;
3863 mtx_unlock(&ctl_softc->pool_lock);
3866 ctl_pool_release(npool);
3869 /* Drop the spinlock before we malloc */
3870 mtx_unlock(&ctl_softc->pool_lock);
3873 * The emergency pool (if it exists) didn't have one, so try an
3874 * atomic (i.e. nonblocking) malloc and see if we get lucky.
3876 io = (union ctl_io *)malloc(sizeof(*io), M_CTLIO, M_NOWAIT);
3879 * If the emergency pool exists but is empty, add this
3880 * ctl_io to its list when it gets freed.
3882 if (emergency_pool != NULL) {
3883 mtx_lock(&ctl_softc->pool_lock);
3884 if (ctl_pool_acquire(emergency_pool) == 0) {
3885 io->io_hdr.pool = emergency_pool;
3886 emergency_pool->total_ctl_io++;
3888 * Need to bump this, otherwise
3889 * total_allocated and total_freed won't
3890 * match when we no longer have anything
3893 emergency_pool->total_allocated++;
3895 mtx_unlock(&ctl_softc->pool_lock);
3897 io->io_hdr.pool = NULL;
3904 ctl_free_io(union ctl_io *io)
3910 * If this ctl_io has a pool, return it to that pool.
3912 if (io->io_hdr.pool != NULL) {
3913 struct ctl_io_pool *pool;
3915 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3916 mtx_lock(&pool->ctl_softc->pool_lock);
3917 io->io_hdr.io_type = 0xff;
3918 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links);
3919 pool->total_freed++;
3920 pool->free_ctl_io++;
3921 ctl_pool_release(pool);
3922 mtx_unlock(&pool->ctl_softc->pool_lock);
3925 * Otherwise, just free it. We probably malloced it and
3926 * the emergency pool wasn't available.
3934 ctl_zero_io(union ctl_io *io)
3942 * May need to preserve linked list pointers at some point too.
3944 pool_ref = io->io_hdr.pool;
3946 memset(io, 0, sizeof(*io));
3948 io->io_hdr.pool = pool_ref;
3952 * This routine is currently used for internal copies of ctl_ios that need
3953 * to persist for some reason after we've already returned status to the
3954 * FETD. (Thus the flag set.)
3957 * Note that this makes a blind copy of all fields in the ctl_io, except
3958 * for the pool reference. This includes any memory that has been
3959 * allocated! That memory will no longer be valid after done has been
3960 * called, so this would be VERY DANGEROUS for command that actually does
3961 * any reads or writes. Right now (11/7/2005), this is only used for immediate
3962 * start and stop commands, which don't transfer any data, so this is not a
3963 * problem. If it is used for anything else, the caller would also need to
3964 * allocate data buffer space and this routine would need to be modified to
3965 * copy the data buffer(s) as well.
3968 ctl_copy_io(union ctl_io *src, union ctl_io *dest)
3977 * May need to preserve linked list pointers at some point too.
3979 pool_ref = dest->io_hdr.pool;
3981 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest)));
3983 dest->io_hdr.pool = pool_ref;
3985 * We need to know that this is an internal copy, and doesn't need
3986 * to get passed back to the FETD that allocated it.
3988 dest->io_hdr.flags |= CTL_FLAG_INT_COPY;
3992 * This routine could be used in the future to load default and/or saved
3993 * mode page parameters for a particuar lun.
3996 ctl_init_page_index(struct ctl_lun *lun)
3999 struct ctl_page_index *page_index;
4002 memcpy(&lun->mode_pages.index, page_index_template,
4003 sizeof(page_index_template));
4005 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4007 page_index = &lun->mode_pages.index[i];
4009 * If this is a disk-only mode page, there's no point in
4010 * setting it up. For some pages, we have to have some
4011 * basic information about the disk in order to calculate the
4014 if ((lun->be_lun->lun_type != T_DIRECT)
4015 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
4018 switch (page_index->page_code & SMPH_PC_MASK) {
4019 case SMS_RW_ERROR_RECOVERY_PAGE: {
4020 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4021 panic("subpage is incorrect!");
4022 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
4023 &rw_er_page_default,
4024 sizeof(rw_er_page_default));
4025 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
4026 &rw_er_page_changeable,
4027 sizeof(rw_er_page_changeable));
4028 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
4029 &rw_er_page_default,
4030 sizeof(rw_er_page_default));
4031 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
4032 &rw_er_page_default,
4033 sizeof(rw_er_page_default));
4034 page_index->page_data =
4035 (uint8_t *)lun->mode_pages.rw_er_page;
4038 case SMS_FORMAT_DEVICE_PAGE: {
4039 struct scsi_format_page *format_page;
4041 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4042 panic("subpage is incorrect!");
4045 * Sectors per track are set above. Bytes per
4046 * sector need to be set here on a per-LUN basis.
4048 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
4049 &format_page_default,
4050 sizeof(format_page_default));
4051 memcpy(&lun->mode_pages.format_page[
4052 CTL_PAGE_CHANGEABLE], &format_page_changeable,
4053 sizeof(format_page_changeable));
4054 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
4055 &format_page_default,
4056 sizeof(format_page_default));
4057 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
4058 &format_page_default,
4059 sizeof(format_page_default));
4061 format_page = &lun->mode_pages.format_page[
4063 scsi_ulto2b(lun->be_lun->blocksize,
4064 format_page->bytes_per_sector);
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 page_index->page_data =
4077 (uint8_t *)lun->mode_pages.format_page;
4080 case SMS_RIGID_DISK_PAGE: {
4081 struct scsi_rigid_disk_page *rigid_disk_page;
4082 uint32_t sectors_per_cylinder;
4086 #endif /* !__XSCALE__ */
4088 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4089 panic("invalid subpage value %d",
4090 page_index->subpage);
4093 * Rotation rate and sectors per track are set
4094 * above. We calculate the cylinders here based on
4095 * capacity. Due to the number of heads and
4096 * sectors per track we're using, smaller arrays
4097 * may turn out to have 0 cylinders. Linux and
4098 * FreeBSD don't pay attention to these mode pages
4099 * to figure out capacity, but Solaris does. It
4100 * seems to deal with 0 cylinders just fine, and
4101 * works out a fake geometry based on the capacity.
4103 memcpy(&lun->mode_pages.rigid_disk_page[
4104 CTL_PAGE_CURRENT], &rigid_disk_page_default,
4105 sizeof(rigid_disk_page_default));
4106 memcpy(&lun->mode_pages.rigid_disk_page[
4107 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4108 sizeof(rigid_disk_page_changeable));
4109 memcpy(&lun->mode_pages.rigid_disk_page[
4110 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4111 sizeof(rigid_disk_page_default));
4112 memcpy(&lun->mode_pages.rigid_disk_page[
4113 CTL_PAGE_SAVED], &rigid_disk_page_default,
4114 sizeof(rigid_disk_page_default));
4116 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4120 * The divide method here will be more accurate,
4121 * probably, but results in floating point being
4122 * used in the kernel on i386 (__udivdi3()). On the
4123 * XScale, though, __udivdi3() is implemented in
4126 * The shift method for cylinder calculation is
4127 * accurate if sectors_per_cylinder is a power of
4128 * 2. Otherwise it might be slightly off -- you
4129 * might have a bit of a truncation problem.
4132 cylinders = (lun->be_lun->maxlba + 1) /
4133 sectors_per_cylinder;
4135 for (shift = 31; shift > 0; shift--) {
4136 if (sectors_per_cylinder & (1 << shift))
4139 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4143 * We've basically got 3 bytes, or 24 bits for the
4144 * cylinder size in the mode page. If we're over,
4145 * just round down to 2^24.
4147 if (cylinders > 0xffffff)
4148 cylinders = 0xffffff;
4150 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4152 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4154 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4156 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4158 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4160 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4162 page_index->page_data =
4163 (uint8_t *)lun->mode_pages.rigid_disk_page;
4166 case SMS_CACHING_PAGE: {
4167 struct scsi_caching_page *caching_page;
4169 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4170 panic("invalid subpage value %d",
4171 page_index->subpage);
4172 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4173 &caching_page_default,
4174 sizeof(caching_page_default));
4175 memcpy(&lun->mode_pages.caching_page[
4176 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4177 sizeof(caching_page_changeable));
4178 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4179 &caching_page_default,
4180 sizeof(caching_page_default));
4181 caching_page = &lun->mode_pages.caching_page[
4183 value = ctl_get_opt(&lun->be_lun->options, "writecache");
4184 if (value != NULL && strcmp(value, "off") == 0)
4185 caching_page->flags1 &= ~SCP_WCE;
4186 value = ctl_get_opt(&lun->be_lun->options, "readcache");
4187 if (value != NULL && strcmp(value, "off") == 0)
4188 caching_page->flags1 |= SCP_RCD;
4189 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4190 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4191 sizeof(caching_page_default));
4192 page_index->page_data =
4193 (uint8_t *)lun->mode_pages.caching_page;
4196 case SMS_CONTROL_MODE_PAGE: {
4197 struct scsi_control_page *control_page;
4199 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4200 panic("invalid subpage value %d",
4201 page_index->subpage);
4203 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT],
4204 &control_page_default,
4205 sizeof(control_page_default));
4206 memcpy(&lun->mode_pages.control_page[
4207 CTL_PAGE_CHANGEABLE], &control_page_changeable,
4208 sizeof(control_page_changeable));
4209 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED],
4210 &control_page_default,
4211 sizeof(control_page_default));
4212 control_page = &lun->mode_pages.control_page[
4214 value = ctl_get_opt(&lun->be_lun->options, "reordering");
4215 if (value != NULL && strcmp(value, "unrestricted") == 0) {
4216 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK;
4217 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED;
4219 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT],
4220 &lun->mode_pages.control_page[CTL_PAGE_SAVED],
4221 sizeof(control_page_default));
4222 page_index->page_data =
4223 (uint8_t *)lun->mode_pages.control_page;
4227 case SMS_INFO_EXCEPTIONS_PAGE: {
4228 switch (page_index->subpage) {
4229 case SMS_SUBPAGE_PAGE_0:
4230 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4232 sizeof(ie_page_default));
4233 memcpy(&lun->mode_pages.ie_page[
4234 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4235 sizeof(ie_page_changeable));
4236 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4238 sizeof(ie_page_default));
4239 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4241 sizeof(ie_page_default));
4242 page_index->page_data =
4243 (uint8_t *)lun->mode_pages.ie_page;
4246 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4248 sizeof(lbp_page_default));
4249 memcpy(&lun->mode_pages.lbp_page[
4250 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4251 sizeof(lbp_page_changeable));
4252 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4254 sizeof(lbp_page_default));
4255 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4257 sizeof(lbp_page_default));
4258 page_index->page_data =
4259 (uint8_t *)lun->mode_pages.lbp_page;
4263 case SMS_VENDOR_SPECIFIC_PAGE:{
4264 switch (page_index->subpage) {
4265 case DBGCNF_SUBPAGE_CODE: {
4266 struct copan_debugconf_subpage *current_page,
4269 memcpy(&lun->mode_pages.debugconf_subpage[
4271 &debugconf_page_default,
4272 sizeof(debugconf_page_default));
4273 memcpy(&lun->mode_pages.debugconf_subpage[
4274 CTL_PAGE_CHANGEABLE],
4275 &debugconf_page_changeable,
4276 sizeof(debugconf_page_changeable));
4277 memcpy(&lun->mode_pages.debugconf_subpage[
4279 &debugconf_page_default,
4280 sizeof(debugconf_page_default));
4281 memcpy(&lun->mode_pages.debugconf_subpage[
4283 &debugconf_page_default,
4284 sizeof(debugconf_page_default));
4285 page_index->page_data =
4286 (uint8_t *)lun->mode_pages.debugconf_subpage;
4288 current_page = (struct copan_debugconf_subpage *)
4289 (page_index->page_data +
4290 (page_index->page_len *
4292 saved_page = (struct copan_debugconf_subpage *)
4293 (page_index->page_data +
4294 (page_index->page_len *
4299 panic("invalid subpage value %d",
4300 page_index->subpage);
4306 panic("invalid page value %d",
4307 page_index->page_code & SMPH_PC_MASK);
4312 return (CTL_RETVAL_COMPLETE);
4316 ctl_init_log_page_index(struct ctl_lun *lun)
4318 struct ctl_page_index *page_index;
4321 memcpy(&lun->log_pages.index, log_page_index_template,
4322 sizeof(log_page_index_template));
4325 for (i = 0, j = 0; i < CTL_NUM_LOG_PAGES; i++) {
4327 page_index = &lun->log_pages.index[i];
4329 * If this is a disk-only mode page, there's no point in
4330 * setting it up. For some pages, we have to have some
4331 * basic information about the disk in order to calculate the
4334 if ((lun->be_lun->lun_type != T_DIRECT)
4335 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
4338 if (page_index->page_code != prev) {
4339 lun->log_pages.pages_page[j] = page_index->page_code;
4340 prev = page_index->page_code;
4343 lun->log_pages.subpages_page[i*2] = page_index->page_code;
4344 lun->log_pages.subpages_page[i*2+1] = page_index->subpage;
4346 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4347 lun->log_pages.index[0].page_len = j;
4348 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4349 lun->log_pages.index[1].page_len = i * 2;
4351 return (CTL_RETVAL_COMPLETE);
4358 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4359 * wants us to allocate the LUN and he can block.
4360 * - ctl_softc is always set
4361 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4363 * Returns 0 for success, non-zero (errno) for failure.
4366 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4367 struct ctl_be_lun *const be_lun, struct ctl_id target_id)
4369 struct ctl_lun *nlun, *lun;
4370 struct ctl_port *port;
4371 struct scsi_vpd_id_descriptor *desc;
4372 struct scsi_vpd_id_t10 *t10id;
4373 const char *eui, *naa, *scsiname, *vendor, *value;
4374 int lun_number, i, lun_malloced;
4375 int devidlen, idlen1, idlen2 = 0, len;
4381 * We currently only support Direct Access or Processor LUN types.
4383 switch (be_lun->lun_type) {
4391 be_lun->lun_config_status(be_lun->be_lun,
4392 CTL_LUN_CONFIG_FAILURE);
4395 if (ctl_lun == NULL) {
4396 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4403 memset(lun, 0, sizeof(*lun));
4405 lun->flags = CTL_LUN_MALLOCED;
4407 /* Generate LUN ID. */
4408 devidlen = max(CTL_DEVID_MIN_LEN,
4409 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4410 idlen1 = sizeof(*t10id) + devidlen;
4411 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4412 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4413 if (scsiname != NULL) {
4414 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4415 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4417 eui = ctl_get_opt(&be_lun->options, "eui");
4419 len += sizeof(struct scsi_vpd_id_descriptor) + 8;
4421 naa = ctl_get_opt(&be_lun->options, "naa");
4423 len += sizeof(struct scsi_vpd_id_descriptor) + 8;
4425 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4426 M_CTL, M_WAITOK | M_ZERO);
4427 lun->lun_devid->len = len;
4428 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4429 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4430 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4431 desc->length = idlen1;
4432 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4433 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4434 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4435 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4437 strncpy(t10id->vendor, vendor,
4438 min(sizeof(t10id->vendor), strlen(vendor)));
4440 strncpy((char *)t10id->vendor_spec_id,
4441 (char *)be_lun->device_id, devidlen);
4442 if (scsiname != NULL) {
4443 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4445 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4446 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4447 SVPD_ID_TYPE_SCSI_NAME;
4448 desc->length = idlen2;
4449 strlcpy(desc->identifier, scsiname, idlen2);
4452 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4454 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4455 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4458 scsi_u64to8b(strtouq(eui, NULL, 0), desc->identifier);
4461 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4463 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4464 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4467 scsi_u64to8b(strtouq(naa, NULL, 0), desc->identifier);
4470 mtx_lock(&ctl_softc->ctl_lock);
4472 * See if the caller requested a particular LUN number. If so, see
4473 * if it is available. Otherwise, allocate the first available LUN.
4475 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4476 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4477 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4478 mtx_unlock(&ctl_softc->ctl_lock);
4479 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4480 printf("ctl: requested LUN ID %d is higher "
4481 "than CTL_MAX_LUNS - 1 (%d)\n",
4482 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4485 * XXX KDM return an error, or just assign
4486 * another LUN ID in this case??
4488 printf("ctl: requested LUN ID %d is already "
4489 "in use\n", be_lun->req_lun_id);
4491 if (lun->flags & CTL_LUN_MALLOCED)
4493 be_lun->lun_config_status(be_lun->be_lun,
4494 CTL_LUN_CONFIG_FAILURE);
4497 lun_number = be_lun->req_lun_id;
4499 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS);
4500 if (lun_number == -1) {
4501 mtx_unlock(&ctl_softc->ctl_lock);
4502 printf("ctl: can't allocate LUN on target %ju, out of "
4503 "LUNs\n", (uintmax_t)target_id.id);
4504 if (lun->flags & CTL_LUN_MALLOCED)
4506 be_lun->lun_config_status(be_lun->be_lun,
4507 CTL_LUN_CONFIG_FAILURE);
4511 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4513 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4514 lun->target = target_id;
4515 lun->lun = lun_number;
4516 lun->be_lun = be_lun;
4518 * The processor LUN is always enabled. Disk LUNs come on line
4519 * disabled, and must be enabled by the backend.
4521 lun->flags |= CTL_LUN_DISABLED;
4522 lun->backend = be_lun->be;
4523 be_lun->ctl_lun = lun;
4524 be_lun->lun_id = lun_number;
4525 atomic_add_int(&be_lun->be->num_luns, 1);
4526 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE)
4527 lun->flags |= CTL_LUN_OFFLINE;
4529 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4530 lun->flags |= CTL_LUN_STOPPED;
4532 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4533 lun->flags |= CTL_LUN_INOPERABLE;
4535 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4536 lun->flags |= CTL_LUN_PRIMARY_SC;
4538 value = ctl_get_opt(&be_lun->options, "readonly");
4539 if (value != NULL && strcmp(value, "on") == 0)
4540 lun->flags |= CTL_LUN_READONLY;
4542 lun->ctl_softc = ctl_softc;
4543 TAILQ_INIT(&lun->ooa_queue);
4544 TAILQ_INIT(&lun->blocked_queue);
4545 STAILQ_INIT(&lun->error_list);
4546 ctl_tpc_lun_init(lun);
4549 * Initialize the mode and log page index.
4551 ctl_init_page_index(lun);
4552 ctl_init_log_page_index(lun);
4555 * Set the poweron UA for all initiators on this LUN only.
4557 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4558 lun->pending_ua[i] = CTL_UA_POWERON;
4561 * Now, before we insert this lun on the lun list, set the lun
4562 * inventory changed UA for all other luns.
4564 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4565 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4566 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE;
4570 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4572 ctl_softc->ctl_luns[lun_number] = lun;
4574 ctl_softc->num_luns++;
4576 /* Setup statistics gathering */
4577 lun->stats.device_type = be_lun->lun_type;
4578 lun->stats.lun_number = lun_number;
4579 if (lun->stats.device_type == T_DIRECT)
4580 lun->stats.blocksize = be_lun->blocksize;
4582 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4583 for (i = 0;i < CTL_MAX_PORTS;i++)
4584 lun->stats.ports[i].targ_port = i;
4586 mtx_unlock(&ctl_softc->ctl_lock);
4588 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4591 * Run through each registered FETD and bring it online if it isn't
4592 * already. Enable the target ID if it hasn't been enabled, and
4593 * enable this particular LUN.
4595 STAILQ_FOREACH(port, &ctl_softc->port_list, links) {
4598 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number);
4600 printf("ctl_alloc_lun: FETD %s port %d returned error "
4601 "%d for lun_enable on target %ju lun %d\n",
4602 port->port_name, port->targ_port, retval,
4603 (uintmax_t)target_id.id, lun_number);
4605 port->status |= CTL_PORT_STATUS_LUN_ONLINE;
4613 * - LUN has already been marked invalid and any pending I/O has been taken
4617 ctl_free_lun(struct ctl_lun *lun)
4619 struct ctl_softc *softc;
4621 struct ctl_port *port;
4623 struct ctl_lun *nlun;
4626 softc = lun->ctl_softc;
4628 mtx_assert(&softc->ctl_lock, MA_OWNED);
4630 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4632 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4634 softc->ctl_luns[lun->lun] = NULL;
4636 if (!TAILQ_EMPTY(&lun->ooa_queue))
4637 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4642 * XXX KDM this scheme only works for a single target/multiple LUN
4643 * setup. It needs to be revamped for a multiple target scheme.
4645 * XXX KDM this results in port->lun_disable() getting called twice,
4646 * once when ctl_disable_lun() is called, and a second time here.
4647 * We really need to re-think the LUN disable semantics. There
4648 * should probably be several steps/levels to LUN removal:
4653 * Right now we only have a disable method when communicating to
4654 * the front end ports, at least for individual LUNs.
4657 STAILQ_FOREACH(port, &softc->port_list, links) {
4660 retval = port->lun_disable(port->targ_lun_arg, lun->target,
4663 printf("ctl_free_lun: FETD %s port %d returned error "
4664 "%d for lun_disable on target %ju lun %jd\n",
4665 port->port_name, port->targ_port, retval,
4666 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4669 if (STAILQ_FIRST(&softc->lun_list) == NULL) {
4670 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE;
4672 retval = port->targ_disable(port->targ_lun_arg,lun->target);
4674 printf("ctl_free_lun: FETD %s port %d "
4675 "returned error %d for targ_disable on "
4676 "target %ju\n", port->port_name,
4677 port->targ_port, retval,
4678 (uintmax_t)lun->target.id);
4680 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE;
4682 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0)
4686 port->port_offline(port->onoff_arg);
4687 port->status &= ~CTL_PORT_STATUS_ONLINE;
4694 * Tell the backend to free resources, if this LUN has a backend.
4696 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4697 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4699 ctl_tpc_lun_shutdown(lun);
4700 mtx_destroy(&lun->lun_lock);
4701 free(lun->lun_devid, M_CTL);
4702 if (lun->flags & CTL_LUN_MALLOCED)
4705 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4706 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4707 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE;
4715 ctl_create_lun(struct ctl_be_lun *be_lun)
4717 struct ctl_softc *ctl_softc;
4719 ctl_softc = control_softc;
4722 * ctl_alloc_lun() should handle all potential failure cases.
4724 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target);
4728 ctl_add_lun(struct ctl_be_lun *be_lun)
4730 struct ctl_softc *ctl_softc = control_softc;
4732 mtx_lock(&ctl_softc->ctl_lock);
4733 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links);
4734 mtx_unlock(&ctl_softc->ctl_lock);
4735 wakeup(&ctl_softc->pending_lun_queue);
4741 ctl_enable_lun(struct ctl_be_lun *be_lun)
4743 struct ctl_softc *ctl_softc;
4744 struct ctl_port *port, *nport;
4745 struct ctl_lun *lun;
4748 ctl_softc = control_softc;
4750 lun = (struct ctl_lun *)be_lun->ctl_lun;
4752 mtx_lock(&ctl_softc->ctl_lock);
4753 mtx_lock(&lun->lun_lock);
4754 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4756 * eh? Why did we get called if the LUN is already
4759 mtx_unlock(&lun->lun_lock);
4760 mtx_unlock(&ctl_softc->ctl_lock);
4763 lun->flags &= ~CTL_LUN_DISABLED;
4764 mtx_unlock(&lun->lun_lock);
4766 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) {
4767 nport = STAILQ_NEXT(port, links);
4770 * Drop the lock while we call the FETD's enable routine.
4771 * This can lead to a callback into CTL (at least in the
4772 * case of the internal initiator frontend.
4774 mtx_unlock(&ctl_softc->ctl_lock);
4775 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun);
4776 mtx_lock(&ctl_softc->ctl_lock);
4778 printf("%s: FETD %s port %d returned error "
4779 "%d for lun_enable on target %ju lun %jd\n",
4780 __func__, port->port_name, port->targ_port, retval,
4781 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4785 /* NOTE: TODO: why does lun enable affect port status? */
4786 port->status |= CTL_PORT_STATUS_LUN_ONLINE;
4791 mtx_unlock(&ctl_softc->ctl_lock);
4797 ctl_disable_lun(struct ctl_be_lun *be_lun)
4799 struct ctl_softc *ctl_softc;
4800 struct ctl_port *port;
4801 struct ctl_lun *lun;
4804 ctl_softc = control_softc;
4806 lun = (struct ctl_lun *)be_lun->ctl_lun;
4808 mtx_lock(&ctl_softc->ctl_lock);
4809 mtx_lock(&lun->lun_lock);
4810 if (lun->flags & CTL_LUN_DISABLED) {
4811 mtx_unlock(&lun->lun_lock);
4812 mtx_unlock(&ctl_softc->ctl_lock);
4815 lun->flags |= CTL_LUN_DISABLED;
4816 mtx_unlock(&lun->lun_lock);
4818 STAILQ_FOREACH(port, &ctl_softc->port_list, links) {
4819 mtx_unlock(&ctl_softc->ctl_lock);
4821 * Drop the lock before we call the frontend's disable
4822 * routine, to avoid lock order reversals.
4824 * XXX KDM what happens if the frontend list changes while
4825 * we're traversing it? It's unlikely, but should be handled.
4827 retval = port->lun_disable(port->targ_lun_arg, lun->target,
4829 mtx_lock(&ctl_softc->ctl_lock);
4831 printf("ctl_alloc_lun: FETD %s port %d returned error "
4832 "%d for lun_disable on target %ju lun %jd\n",
4833 port->port_name, port->targ_port, retval,
4834 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4838 mtx_unlock(&ctl_softc->ctl_lock);
4844 ctl_start_lun(struct ctl_be_lun *be_lun)
4846 struct ctl_softc *ctl_softc;
4847 struct ctl_lun *lun;
4849 ctl_softc = control_softc;
4851 lun = (struct ctl_lun *)be_lun->ctl_lun;
4853 mtx_lock(&lun->lun_lock);
4854 lun->flags &= ~CTL_LUN_STOPPED;
4855 mtx_unlock(&lun->lun_lock);
4861 ctl_stop_lun(struct ctl_be_lun *be_lun)
4863 struct ctl_softc *ctl_softc;
4864 struct ctl_lun *lun;
4866 ctl_softc = control_softc;
4868 lun = (struct ctl_lun *)be_lun->ctl_lun;
4870 mtx_lock(&lun->lun_lock);
4871 lun->flags |= CTL_LUN_STOPPED;
4872 mtx_unlock(&lun->lun_lock);
4878 ctl_lun_offline(struct ctl_be_lun *be_lun)
4880 struct ctl_softc *ctl_softc;
4881 struct ctl_lun *lun;
4883 ctl_softc = control_softc;
4885 lun = (struct ctl_lun *)be_lun->ctl_lun;
4887 mtx_lock(&lun->lun_lock);
4888 lun->flags |= CTL_LUN_OFFLINE;
4889 mtx_unlock(&lun->lun_lock);
4895 ctl_lun_online(struct ctl_be_lun *be_lun)
4897 struct ctl_softc *ctl_softc;
4898 struct ctl_lun *lun;
4900 ctl_softc = control_softc;
4902 lun = (struct ctl_lun *)be_lun->ctl_lun;
4904 mtx_lock(&lun->lun_lock);
4905 lun->flags &= ~CTL_LUN_OFFLINE;
4906 mtx_unlock(&lun->lun_lock);
4912 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4914 struct ctl_softc *ctl_softc;
4915 struct ctl_lun *lun;
4917 ctl_softc = control_softc;
4919 lun = (struct ctl_lun *)be_lun->ctl_lun;
4921 mtx_lock(&lun->lun_lock);
4924 * The LUN needs to be disabled before it can be marked invalid.
4926 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4927 mtx_unlock(&lun->lun_lock);
4931 * Mark the LUN invalid.
4933 lun->flags |= CTL_LUN_INVALID;
4936 * If there is nothing in the OOA queue, go ahead and free the LUN.
4937 * If we have something in the OOA queue, we'll free it when the
4938 * last I/O completes.
4940 if (TAILQ_EMPTY(&lun->ooa_queue)) {
4941 mtx_unlock(&lun->lun_lock);
4942 mtx_lock(&ctl_softc->ctl_lock);
4944 mtx_unlock(&ctl_softc->ctl_lock);
4946 mtx_unlock(&lun->lun_lock);
4952 ctl_lun_inoperable(struct ctl_be_lun *be_lun)
4954 struct ctl_softc *ctl_softc;
4955 struct ctl_lun *lun;
4957 ctl_softc = control_softc;
4958 lun = (struct ctl_lun *)be_lun->ctl_lun;
4960 mtx_lock(&lun->lun_lock);
4961 lun->flags |= CTL_LUN_INOPERABLE;
4962 mtx_unlock(&lun->lun_lock);
4968 ctl_lun_operable(struct ctl_be_lun *be_lun)
4970 struct ctl_softc *ctl_softc;
4971 struct ctl_lun *lun;
4973 ctl_softc = control_softc;
4974 lun = (struct ctl_lun *)be_lun->ctl_lun;
4976 mtx_lock(&lun->lun_lock);
4977 lun->flags &= ~CTL_LUN_INOPERABLE;
4978 mtx_unlock(&lun->lun_lock);
4984 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
4986 struct ctl_lun *lun;
4987 struct ctl_softc *softc;
4990 softc = control_softc;
4992 lun = (struct ctl_lun *)be_lun->ctl_lun;
4994 mtx_lock(&lun->lun_lock);
4996 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4997 lun->pending_ua[i] |= CTL_UA_CAPACITY_CHANGED;
4999 mtx_unlock(&lun->lun_lock);
5003 * Backend "memory move is complete" callback for requests that never
5004 * make it down to say RAIDCore's configuration code.
5007 ctl_config_move_done(union ctl_io *io)
5011 retval = CTL_RETVAL_COMPLETE;
5014 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
5016 * XXX KDM this shouldn't happen, but what if it does?
5018 if (io->io_hdr.io_type != CTL_IO_SCSI)
5019 panic("I/O type isn't CTL_IO_SCSI!");
5021 if ((io->io_hdr.port_status == 0)
5022 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
5023 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE))
5024 io->io_hdr.status = CTL_SUCCESS;
5025 else if ((io->io_hdr.port_status != 0)
5026 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
5027 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){
5029 * For hardware error sense keys, the sense key
5030 * specific value is defined to be a retry count,
5031 * but we use it to pass back an internal FETD
5032 * error code. XXX KDM Hopefully the FETD is only
5033 * using 16 bits for an error code, since that's
5034 * all the space we have in the sks field.
5036 ctl_set_internal_failure(&io->scsiio,
5039 io->io_hdr.port_status);
5040 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5041 free(io->scsiio.kern_data_ptr, M_CTL);
5046 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
5047 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
5048 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5050 * XXX KDM just assuming a single pointer here, and not a
5051 * S/G list. If we start using S/G lists for config data,
5052 * we'll need to know how to clean them up here as well.
5054 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5055 free(io->scsiio.kern_data_ptr, M_CTL);
5056 /* Hopefully the user has already set the status... */
5060 * XXX KDM now we need to continue data movement. Some
5062 * - call ctl_scsiio() again? We don't do this for data
5063 * writes, because for those at least we know ahead of
5064 * time where the write will go and how long it is. For
5065 * config writes, though, that information is largely
5066 * contained within the write itself, thus we need to
5067 * parse out the data again.
5069 * - Call some other function once the data is in?
5071 if (ctl_debug & CTL_DEBUG_CDB_DATA)
5075 * XXX KDM call ctl_scsiio() again for now, and check flag
5076 * bits to see whether we're allocated or not.
5078 retval = ctl_scsiio(&io->scsiio);
5085 * This gets called by a backend driver when it is done with a
5086 * data_submit method.
5089 ctl_data_submit_done(union ctl_io *io)
5092 * If the IO_CONT flag is set, we need to call the supplied
5093 * function to continue processing the I/O, instead of completing
5096 * If there is an error, though, we don't want to keep processing.
5097 * Instead, just send status back to the initiator.
5099 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5100 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5101 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5102 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5103 io->scsiio.io_cont(io);
5110 * This gets called by a backend driver when it is done with a
5111 * configuration write.
5114 ctl_config_write_done(union ctl_io *io)
5119 * If the IO_CONT flag is set, we need to call the supplied
5120 * function to continue processing the I/O, instead of completing
5123 * If there is an error, though, we don't want to keep processing.
5124 * Instead, just send status back to the initiator.
5126 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5127 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5128 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5129 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5130 io->scsiio.io_cont(io);
5134 * Since a configuration write can be done for commands that actually
5135 * have data allocated, like write buffer, and commands that have
5136 * no data, like start/stop unit, we need to check here.
5138 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5139 buf = io->scsiio.kern_data_ptr;
5148 * SCSI release command.
5151 ctl_scsi_release(struct ctl_scsiio *ctsio)
5153 int length, longid, thirdparty_id, resv_id;
5154 struct ctl_softc *ctl_softc;
5155 struct ctl_lun *lun;
5161 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5163 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5164 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5165 ctl_softc = control_softc;
5167 switch (ctsio->cdb[0]) {
5169 struct scsi_release_10 *cdb;
5171 cdb = (struct scsi_release_10 *)ctsio->cdb;
5173 if (cdb->byte2 & SR10_LONGID)
5176 thirdparty_id = cdb->thirdparty_id;
5178 resv_id = cdb->resv_id;
5179 length = scsi_2btoul(cdb->length);
5186 * XXX KDM right now, we only support LUN reservation. We don't
5187 * support 3rd party reservations, or extent reservations, which
5188 * might actually need the parameter list. If we've gotten this
5189 * far, we've got a LUN reservation. Anything else got kicked out
5190 * above. So, according to SPC, ignore the length.
5194 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5196 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5197 ctsio->kern_data_len = length;
5198 ctsio->kern_total_len = length;
5199 ctsio->kern_data_resid = 0;
5200 ctsio->kern_rel_offset = 0;
5201 ctsio->kern_sg_entries = 0;
5202 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5203 ctsio->be_move_done = ctl_config_move_done;
5204 ctl_datamove((union ctl_io *)ctsio);
5206 return (CTL_RETVAL_COMPLETE);
5210 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5212 mtx_lock(&lun->lun_lock);
5215 * According to SPC, it is not an error for an intiator to attempt
5216 * to release a reservation on a LUN that isn't reserved, or that
5217 * is reserved by another initiator. The reservation can only be
5218 * released, though, by the initiator who made it or by one of
5219 * several reset type events.
5221 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5222 lun->flags &= ~CTL_LUN_RESERVED;
5224 mtx_unlock(&lun->lun_lock);
5226 ctsio->scsi_status = SCSI_STATUS_OK;
5227 ctsio->io_hdr.status = CTL_SUCCESS;
5229 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5230 free(ctsio->kern_data_ptr, M_CTL);
5231 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5234 ctl_done((union ctl_io *)ctsio);
5235 return (CTL_RETVAL_COMPLETE);
5239 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5241 int extent, thirdparty, longid;
5242 int resv_id, length;
5243 uint64_t thirdparty_id;
5244 struct ctl_softc *ctl_softc;
5245 struct ctl_lun *lun;
5255 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5257 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5258 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5259 ctl_softc = control_softc;
5261 switch (ctsio->cdb[0]) {
5263 struct scsi_reserve_10 *cdb;
5265 cdb = (struct scsi_reserve_10 *)ctsio->cdb;
5267 if (cdb->byte2 & SR10_LONGID)
5270 thirdparty_id = cdb->thirdparty_id;
5272 resv_id = cdb->resv_id;
5273 length = scsi_2btoul(cdb->length);
5279 * XXX KDM right now, we only support LUN reservation. We don't
5280 * support 3rd party reservations, or extent reservations, which
5281 * might actually need the parameter list. If we've gotten this
5282 * far, we've got a LUN reservation. Anything else got kicked out
5283 * above. So, according to SPC, ignore the length.
5287 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5289 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5290 ctsio->kern_data_len = length;
5291 ctsio->kern_total_len = length;
5292 ctsio->kern_data_resid = 0;
5293 ctsio->kern_rel_offset = 0;
5294 ctsio->kern_sg_entries = 0;
5295 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5296 ctsio->be_move_done = ctl_config_move_done;
5297 ctl_datamove((union ctl_io *)ctsio);
5299 return (CTL_RETVAL_COMPLETE);
5303 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5305 mtx_lock(&lun->lun_lock);
5306 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5307 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
5308 ctsio->io_hdr.status = CTL_SCSI_ERROR;
5312 lun->flags |= CTL_LUN_RESERVED;
5313 lun->res_idx = residx;
5315 ctsio->scsi_status = SCSI_STATUS_OK;
5316 ctsio->io_hdr.status = CTL_SUCCESS;
5319 mtx_unlock(&lun->lun_lock);
5321 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5322 free(ctsio->kern_data_ptr, M_CTL);
5323 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5326 ctl_done((union ctl_io *)ctsio);
5327 return (CTL_RETVAL_COMPLETE);
5331 ctl_start_stop(struct ctl_scsiio *ctsio)
5333 struct scsi_start_stop_unit *cdb;
5334 struct ctl_lun *lun;
5335 struct ctl_softc *ctl_softc;
5338 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5340 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5341 ctl_softc = control_softc;
5344 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5348 * We don't support the immediate bit on a stop unit. In order to
5349 * do that, we would need to code up a way to know that a stop is
5350 * pending, and hold off any new commands until it completes, one
5351 * way or another. Then we could accept or reject those commands
5352 * depending on its status. We would almost need to do the reverse
5353 * of what we do below for an immediate start -- return the copy of
5354 * the ctl_io to the FETD with status to send to the host (and to
5355 * free the copy!) and then free the original I/O once the stop
5356 * actually completes. That way, the OOA queue mechanism can work
5357 * to block commands that shouldn't proceed. Another alternative
5358 * would be to put the copy in the queue in place of the original,
5359 * and return the original back to the caller. That could be
5362 if ((cdb->byte2 & SSS_IMMED)
5363 && ((cdb->how & SSS_START) == 0)) {
5364 ctl_set_invalid_field(ctsio,
5370 ctl_done((union ctl_io *)ctsio);
5371 return (CTL_RETVAL_COMPLETE);
5374 if ((lun->flags & CTL_LUN_PR_RESERVED)
5375 && ((cdb->how & SSS_START)==0)) {
5378 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5379 if (lun->pr_keys[residx] == 0
5380 || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5382 ctl_set_reservation_conflict(ctsio);
5383 ctl_done((union ctl_io *)ctsio);
5384 return (CTL_RETVAL_COMPLETE);
5389 * If there is no backend on this device, we can't start or stop
5390 * it. In theory we shouldn't get any start/stop commands in the
5391 * first place at this level if the LUN doesn't have a backend.
5392 * That should get stopped by the command decode code.
5394 if (lun->backend == NULL) {
5395 ctl_set_invalid_opcode(ctsio);
5396 ctl_done((union ctl_io *)ctsio);
5397 return (CTL_RETVAL_COMPLETE);
5401 * XXX KDM Copan-specific offline behavior.
5402 * Figure out a reasonable way to port this?
5405 mtx_lock(&lun->lun_lock);
5407 if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
5408 && (lun->flags & CTL_LUN_OFFLINE)) {
5410 * If the LUN is offline, and the on/offline bit isn't set,
5411 * reject the start or stop. Otherwise, let it through.
5413 mtx_unlock(&lun->lun_lock);
5414 ctl_set_lun_not_ready(ctsio);
5415 ctl_done((union ctl_io *)ctsio);
5417 mtx_unlock(&lun->lun_lock);
5418 #endif /* NEEDTOPORT */
5420 * This could be a start or a stop when we're online,
5421 * or a stop/offline or start/online. A start or stop when
5422 * we're offline is covered in the case above.
5425 * In the non-immediate case, we send the request to
5426 * the backend and return status to the user when
5429 * In the immediate case, we allocate a new ctl_io
5430 * to hold a copy of the request, and send that to
5431 * the backend. We then set good status on the
5432 * user's request and return it immediately.
5434 if (cdb->byte2 & SSS_IMMED) {
5435 union ctl_io *new_io;
5437 new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5438 if (new_io == NULL) {
5439 ctl_set_busy(ctsio);
5440 ctl_done((union ctl_io *)ctsio);
5442 ctl_copy_io((union ctl_io *)ctsio,
5444 retval = lun->backend->config_write(new_io);
5445 ctl_set_success(ctsio);
5446 ctl_done((union ctl_io *)ctsio);
5449 retval = lun->backend->config_write(
5450 (union ctl_io *)ctsio);
5459 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5460 * we don't really do anything with the LBA and length fields if the user
5461 * passes them in. Instead we'll just flush out the cache for the entire
5465 ctl_sync_cache(struct ctl_scsiio *ctsio)
5467 struct ctl_lun *lun;
5468 struct ctl_softc *ctl_softc;
5469 uint64_t starting_lba;
5470 uint32_t block_count;
5473 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5475 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5476 ctl_softc = control_softc;
5479 switch (ctsio->cdb[0]) {
5480 case SYNCHRONIZE_CACHE: {
5481 struct scsi_sync_cache *cdb;
5482 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5484 starting_lba = scsi_4btoul(cdb->begin_lba);
5485 block_count = scsi_2btoul(cdb->lb_count);
5488 case SYNCHRONIZE_CACHE_16: {
5489 struct scsi_sync_cache_16 *cdb;
5490 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5492 starting_lba = scsi_8btou64(cdb->begin_lba);
5493 block_count = scsi_4btoul(cdb->lb_count);
5497 ctl_set_invalid_opcode(ctsio);
5498 ctl_done((union ctl_io *)ctsio);
5500 break; /* NOTREACHED */
5504 * We check the LBA and length, but don't do anything with them.
5505 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5506 * get flushed. This check will just help satisfy anyone who wants
5507 * to see an error for an out of range LBA.
5509 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5510 ctl_set_lba_out_of_range(ctsio);
5511 ctl_done((union ctl_io *)ctsio);
5516 * If this LUN has no backend, we can't flush the cache anyway.
5518 if (lun->backend == NULL) {
5519 ctl_set_invalid_opcode(ctsio);
5520 ctl_done((union ctl_io *)ctsio);
5525 * Check to see whether we're configured to send the SYNCHRONIZE
5526 * CACHE command directly to the back end.
5528 mtx_lock(&lun->lun_lock);
5529 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC)
5530 && (++(lun->sync_count) >= lun->sync_interval)) {
5531 lun->sync_count = 0;
5532 mtx_unlock(&lun->lun_lock);
5533 retval = lun->backend->config_write((union ctl_io *)ctsio);
5535 mtx_unlock(&lun->lun_lock);
5536 ctl_set_success(ctsio);
5537 ctl_done((union ctl_io *)ctsio);
5546 ctl_format(struct ctl_scsiio *ctsio)
5548 struct scsi_format *cdb;
5549 struct ctl_lun *lun;
5550 struct ctl_softc *ctl_softc;
5551 int length, defect_list_len;
5553 CTL_DEBUG_PRINT(("ctl_format\n"));
5555 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5556 ctl_softc = control_softc;
5558 cdb = (struct scsi_format *)ctsio->cdb;
5561 if (cdb->byte2 & SF_FMTDATA) {
5562 if (cdb->byte2 & SF_LONGLIST)
5563 length = sizeof(struct scsi_format_header_long);
5565 length = sizeof(struct scsi_format_header_short);
5568 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5570 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5571 ctsio->kern_data_len = length;
5572 ctsio->kern_total_len = length;
5573 ctsio->kern_data_resid = 0;
5574 ctsio->kern_rel_offset = 0;
5575 ctsio->kern_sg_entries = 0;
5576 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5577 ctsio->be_move_done = ctl_config_move_done;
5578 ctl_datamove((union ctl_io *)ctsio);
5580 return (CTL_RETVAL_COMPLETE);
5583 defect_list_len = 0;
5585 if (cdb->byte2 & SF_FMTDATA) {
5586 if (cdb->byte2 & SF_LONGLIST) {
5587 struct scsi_format_header_long *header;
5589 header = (struct scsi_format_header_long *)
5590 ctsio->kern_data_ptr;
5592 defect_list_len = scsi_4btoul(header->defect_list_len);
5593 if (defect_list_len != 0) {
5594 ctl_set_invalid_field(ctsio,
5603 struct scsi_format_header_short *header;
5605 header = (struct scsi_format_header_short *)
5606 ctsio->kern_data_ptr;
5608 defect_list_len = scsi_2btoul(header->defect_list_len);
5609 if (defect_list_len != 0) {
5610 ctl_set_invalid_field(ctsio,
5622 * The format command will clear out the "Medium format corrupted"
5623 * status if set by the configuration code. That status is really
5624 * just a way to notify the host that we have lost the media, and
5625 * get them to issue a command that will basically make them think
5626 * they're blowing away the media.
5628 mtx_lock(&lun->lun_lock);
5629 lun->flags &= ~CTL_LUN_INOPERABLE;
5630 mtx_unlock(&lun->lun_lock);
5632 ctsio->scsi_status = SCSI_STATUS_OK;
5633 ctsio->io_hdr.status = CTL_SUCCESS;
5636 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5637 free(ctsio->kern_data_ptr, M_CTL);
5638 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5641 ctl_done((union ctl_io *)ctsio);
5642 return (CTL_RETVAL_COMPLETE);
5646 ctl_read_buffer(struct ctl_scsiio *ctsio)
5648 struct scsi_read_buffer *cdb;
5649 struct ctl_lun *lun;
5650 int buffer_offset, len;
5651 static uint8_t descr[4];
5652 static uint8_t echo_descr[4] = { 0 };
5654 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5656 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5657 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5659 if (lun->flags & CTL_LUN_PR_RESERVED) {
5663 * XXX KDM need a lock here.
5665 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5666 if ((lun->res_type == SPR_TYPE_EX_AC
5667 && residx != lun->pr_res_idx)
5668 || ((lun->res_type == SPR_TYPE_EX_AC_RO
5669 || lun->res_type == SPR_TYPE_EX_AC_AR)
5670 && lun->pr_keys[residx] == 0)) {
5671 ctl_set_reservation_conflict(ctsio);
5672 ctl_done((union ctl_io *)ctsio);
5673 return (CTL_RETVAL_COMPLETE);
5677 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA &&
5678 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR &&
5679 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) {
5680 ctl_set_invalid_field(ctsio,
5686 ctl_done((union ctl_io *)ctsio);
5687 return (CTL_RETVAL_COMPLETE);
5690 len = scsi_3btoul(cdb->length);
5691 buffer_offset = scsi_3btoul(cdb->offset);
5693 if (buffer_offset + len > sizeof(lun->write_buffer)) {
5694 ctl_set_invalid_field(ctsio,
5700 ctl_done((union ctl_io *)ctsio);
5701 return (CTL_RETVAL_COMPLETE);
5704 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5706 scsi_ulto3b(sizeof(lun->write_buffer), &descr[1]);
5707 ctsio->kern_data_ptr = descr;
5708 len = min(len, sizeof(descr));
5709 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5710 ctsio->kern_data_ptr = echo_descr;
5711 len = min(len, sizeof(echo_descr));
5713 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5714 ctsio->kern_data_len = len;
5715 ctsio->kern_total_len = len;
5716 ctsio->kern_data_resid = 0;
5717 ctsio->kern_rel_offset = 0;
5718 ctsio->kern_sg_entries = 0;
5719 ctsio->be_move_done = ctl_config_move_done;
5720 ctl_datamove((union ctl_io *)ctsio);
5722 return (CTL_RETVAL_COMPLETE);
5726 ctl_write_buffer(struct ctl_scsiio *ctsio)
5728 struct scsi_write_buffer *cdb;
5729 struct ctl_lun *lun;
5730 int buffer_offset, len;
5732 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5734 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5735 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5737 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5738 ctl_set_invalid_field(ctsio,
5744 ctl_done((union ctl_io *)ctsio);
5745 return (CTL_RETVAL_COMPLETE);
5748 len = scsi_3btoul(cdb->length);
5749 buffer_offset = scsi_3btoul(cdb->offset);
5751 if (buffer_offset + len > sizeof(lun->write_buffer)) {
5752 ctl_set_invalid_field(ctsio,
5758 ctl_done((union ctl_io *)ctsio);
5759 return (CTL_RETVAL_COMPLETE);
5763 * If we've got a kernel request that hasn't been malloced yet,
5764 * malloc it and tell the caller the data buffer is here.
5766 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5767 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5768 ctsio->kern_data_len = len;
5769 ctsio->kern_total_len = len;
5770 ctsio->kern_data_resid = 0;
5771 ctsio->kern_rel_offset = 0;
5772 ctsio->kern_sg_entries = 0;
5773 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5774 ctsio->be_move_done = ctl_config_move_done;
5775 ctl_datamove((union ctl_io *)ctsio);
5777 return (CTL_RETVAL_COMPLETE);
5780 ctl_done((union ctl_io *)ctsio);
5782 return (CTL_RETVAL_COMPLETE);
5786 ctl_write_same(struct ctl_scsiio *ctsio)
5788 struct ctl_lun *lun;
5789 struct ctl_lba_len_flags *lbalen;
5791 uint32_t num_blocks;
5795 retval = CTL_RETVAL_COMPLETE;
5797 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5799 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5801 switch (ctsio->cdb[0]) {
5802 case WRITE_SAME_10: {
5803 struct scsi_write_same_10 *cdb;
5805 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5807 lba = scsi_4btoul(cdb->addr);
5808 num_blocks = scsi_2btoul(cdb->length);
5812 case WRITE_SAME_16: {
5813 struct scsi_write_same_16 *cdb;
5815 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5817 lba = scsi_8btou64(cdb->addr);
5818 num_blocks = scsi_4btoul(cdb->length);
5824 * We got a command we don't support. This shouldn't
5825 * happen, commands should be filtered out above us.
5827 ctl_set_invalid_opcode(ctsio);
5828 ctl_done((union ctl_io *)ctsio);
5830 return (CTL_RETVAL_COMPLETE);
5831 break; /* NOTREACHED */
5834 /* NDOB and ANCHOR flags can be used only together with UNMAP */
5835 if ((byte2 & SWS_UNMAP) == 0 &&
5836 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) {
5837 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5838 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5839 ctl_done((union ctl_io *)ctsio);
5840 return (CTL_RETVAL_COMPLETE);
5844 * The first check is to make sure we're in bounds, the second
5845 * check is to catch wrap-around problems. If the lba + num blocks
5846 * is less than the lba, then we've wrapped around and the block
5847 * range is invalid anyway.
5849 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5850 || ((lba + num_blocks) < lba)) {
5851 ctl_set_lba_out_of_range(ctsio);
5852 ctl_done((union ctl_io *)ctsio);
5853 return (CTL_RETVAL_COMPLETE);
5856 /* Zero number of blocks means "to the last logical block" */
5857 if (num_blocks == 0) {
5858 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5859 ctl_set_invalid_field(ctsio,
5865 ctl_done((union ctl_io *)ctsio);
5866 return (CTL_RETVAL_COMPLETE);
5868 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5871 len = lun->be_lun->blocksize;
5874 * If we've got a kernel request that hasn't been malloced yet,
5875 * malloc it and tell the caller the data buffer is here.
5877 if ((byte2 & SWS_NDOB) == 0 &&
5878 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5879 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5880 ctsio->kern_data_len = len;
5881 ctsio->kern_total_len = len;
5882 ctsio->kern_data_resid = 0;
5883 ctsio->kern_rel_offset = 0;
5884 ctsio->kern_sg_entries = 0;
5885 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5886 ctsio->be_move_done = ctl_config_move_done;
5887 ctl_datamove((union ctl_io *)ctsio);
5889 return (CTL_RETVAL_COMPLETE);
5892 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5894 lbalen->len = num_blocks;
5895 lbalen->flags = byte2;
5896 retval = lun->backend->config_write((union ctl_io *)ctsio);
5902 ctl_unmap(struct ctl_scsiio *ctsio)
5904 struct ctl_lun *lun;
5905 struct scsi_unmap *cdb;
5906 struct ctl_ptr_len_flags *ptrlen;
5907 struct scsi_unmap_header *hdr;
5908 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5910 uint32_t num_blocks;
5914 retval = CTL_RETVAL_COMPLETE;
5916 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5918 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5919 cdb = (struct scsi_unmap *)ctsio->cdb;
5921 len = scsi_2btoul(cdb->length);
5925 * If we've got a kernel request that hasn't been malloced yet,
5926 * malloc it and tell the caller the data buffer is here.
5928 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5929 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5930 ctsio->kern_data_len = len;
5931 ctsio->kern_total_len = len;
5932 ctsio->kern_data_resid = 0;
5933 ctsio->kern_rel_offset = 0;
5934 ctsio->kern_sg_entries = 0;
5935 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5936 ctsio->be_move_done = ctl_config_move_done;
5937 ctl_datamove((union ctl_io *)ctsio);
5939 return (CTL_RETVAL_COMPLETE);
5942 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5943 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5944 if (len < sizeof (*hdr) ||
5945 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5946 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5947 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5948 ctl_set_invalid_field(ctsio,
5954 ctl_done((union ctl_io *)ctsio);
5955 return (CTL_RETVAL_COMPLETE);
5957 len = scsi_2btoul(hdr->desc_length);
5958 buf = (struct scsi_unmap_desc *)(hdr + 1);
5959 end = buf + len / sizeof(*buf);
5962 for (range = buf; range < end; range++) {
5963 lba = scsi_8btou64(range->lba);
5964 num_blocks = scsi_4btoul(range->length);
5965 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5966 || ((lba + num_blocks) < lba)) {
5967 ctl_set_lba_out_of_range(ctsio);
5968 ctl_done((union ctl_io *)ctsio);
5969 return (CTL_RETVAL_COMPLETE);
5971 if (num_blocks != 0)
5976 * Block backend can not handle zero last range.
5977 * Filter it out and return if there is nothing left.
5979 len = (uint8_t *)endnz - (uint8_t *)buf;
5981 ctl_set_success(ctsio);
5982 ctl_done((union ctl_io *)ctsio);
5983 return (CTL_RETVAL_COMPLETE);
5986 mtx_lock(&lun->lun_lock);
5987 ptrlen = (struct ctl_ptr_len_flags *)
5988 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5989 ptrlen->ptr = (void *)buf;
5991 ptrlen->flags = byte2;
5992 ctl_check_blocked(lun);
5993 mtx_unlock(&lun->lun_lock);
5995 retval = lun->backend->config_write((union ctl_io *)ctsio);
6000 * Note that this function currently doesn't actually do anything inside
6001 * CTL to enforce things if the DQue bit is turned on.
6003 * Also note that this function can't be used in the default case, because
6004 * the DQue bit isn't set in the changeable mask for the control mode page
6005 * anyway. This is just here as an example for how to implement a page
6006 * handler, and a placeholder in case we want to allow the user to turn
6007 * tagged queueing on and off.
6009 * The D_SENSE bit handling is functional, however, and will turn
6010 * descriptor sense on and off for a given LUN.
6013 ctl_control_page_handler(struct ctl_scsiio *ctsio,
6014 struct ctl_page_index *page_index, uint8_t *page_ptr)
6016 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
6017 struct ctl_lun *lun;
6018 struct ctl_softc *softc;
6022 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6023 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6026 user_cp = (struct scsi_control_page *)page_ptr;
6027 current_cp = (struct scsi_control_page *)
6028 (page_index->page_data + (page_index->page_len *
6030 saved_cp = (struct scsi_control_page *)
6031 (page_index->page_data + (page_index->page_len *
6034 softc = control_softc;
6036 mtx_lock(&lun->lun_lock);
6037 if (((current_cp->rlec & SCP_DSENSE) == 0)
6038 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
6040 * Descriptor sense is currently turned off and the user
6041 * wants to turn it on.
6043 current_cp->rlec |= SCP_DSENSE;
6044 saved_cp->rlec |= SCP_DSENSE;
6045 lun->flags |= CTL_LUN_SENSE_DESC;
6047 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
6048 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
6050 * Descriptor sense is currently turned on, and the user
6051 * wants to turn it off.
6053 current_cp->rlec &= ~SCP_DSENSE;
6054 saved_cp->rlec &= ~SCP_DSENSE;
6055 lun->flags &= ~CTL_LUN_SENSE_DESC;
6058 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) !=
6059 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) {
6060 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
6061 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
6062 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
6063 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
6066 if ((current_cp->eca_and_aen & SCP_SWP) !=
6067 (user_cp->eca_and_aen & SCP_SWP)) {
6068 current_cp->eca_and_aen &= ~SCP_SWP;
6069 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
6070 saved_cp->eca_and_aen &= ~SCP_SWP;
6071 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
6077 * Let other initiators know that the mode
6078 * parameters for this LUN have changed.
6080 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
6084 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE;
6087 mtx_unlock(&lun->lun_lock);
6093 ctl_caching_sp_handler(struct ctl_scsiio *ctsio,
6094 struct ctl_page_index *page_index, uint8_t *page_ptr)
6096 struct scsi_caching_page *current_cp, *saved_cp, *user_cp;
6097 struct ctl_lun *lun;
6101 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6102 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6105 user_cp = (struct scsi_caching_page *)page_ptr;
6106 current_cp = (struct scsi_caching_page *)
6107 (page_index->page_data + (page_index->page_len *
6109 saved_cp = (struct scsi_caching_page *)
6110 (page_index->page_data + (page_index->page_len *
6113 mtx_lock(&lun->lun_lock);
6114 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) !=
6115 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) {
6116 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
6117 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
6118 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
6119 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
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_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
6142 struct ctl_page_index *page_index,
6148 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
6153 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
6154 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
6155 printf("page data:");
6157 printf(" %.2x",page_ptr[i]);
6163 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
6164 struct ctl_page_index *page_index,
6167 struct copan_debugconf_subpage *page;
6169 page = (struct copan_debugconf_subpage *)page_index->page_data +
6170 (page_index->page_len * pc);
6173 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6174 case SMS_PAGE_CTRL_DEFAULT >> 6:
6175 case SMS_PAGE_CTRL_SAVED >> 6:
6177 * We don't update the changable or default bits for this page.
6180 case SMS_PAGE_CTRL_CURRENT >> 6:
6181 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
6182 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
6186 EPRINT(0, "Invalid PC %d!!", pc);
6187 #endif /* NEEDTOPORT */
6195 ctl_do_mode_select(union ctl_io *io)
6197 struct scsi_mode_page_header *page_header;
6198 struct ctl_page_index *page_index;
6199 struct ctl_scsiio *ctsio;
6200 int control_dev, page_len;
6201 int page_len_offset, page_len_size;
6202 union ctl_modepage_info *modepage_info;
6203 struct ctl_lun *lun;
6204 int *len_left, *len_used;
6207 ctsio = &io->scsiio;
6210 retval = CTL_RETVAL_COMPLETE;
6212 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6214 if (lun->be_lun->lun_type != T_DIRECT)
6219 modepage_info = (union ctl_modepage_info *)
6220 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6221 len_left = &modepage_info->header.len_left;
6222 len_used = &modepage_info->header.len_used;
6226 page_header = (struct scsi_mode_page_header *)
6227 (ctsio->kern_data_ptr + *len_used);
6229 if (*len_left == 0) {
6230 free(ctsio->kern_data_ptr, M_CTL);
6231 ctl_set_success(ctsio);
6232 ctl_done((union ctl_io *)ctsio);
6233 return (CTL_RETVAL_COMPLETE);
6234 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6236 free(ctsio->kern_data_ptr, M_CTL);
6237 ctl_set_param_len_error(ctsio);
6238 ctl_done((union ctl_io *)ctsio);
6239 return (CTL_RETVAL_COMPLETE);
6241 } else if ((page_header->page_code & SMPH_SPF)
6242 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6244 free(ctsio->kern_data_ptr, M_CTL);
6245 ctl_set_param_len_error(ctsio);
6246 ctl_done((union ctl_io *)ctsio);
6247 return (CTL_RETVAL_COMPLETE);
6252 * XXX KDM should we do something with the block descriptor?
6254 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6256 if ((control_dev != 0)
6257 && (lun->mode_pages.index[i].page_flags &
6258 CTL_PAGE_FLAG_DISK_ONLY))
6261 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
6262 (page_header->page_code & SMPH_PC_MASK))
6266 * If neither page has a subpage code, then we've got a
6269 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
6270 && ((page_header->page_code & SMPH_SPF) == 0)) {
6271 page_index = &lun->mode_pages.index[i];
6272 page_len = page_header->page_length;
6277 * If both pages have subpages, then the subpage numbers
6280 if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
6281 && (page_header->page_code & SMPH_SPF)) {
6282 struct scsi_mode_page_header_sp *sph;
6284 sph = (struct scsi_mode_page_header_sp *)page_header;
6286 if (lun->mode_pages.index[i].subpage ==
6288 page_index = &lun->mode_pages.index[i];
6289 page_len = scsi_2btoul(sph->page_length);
6296 * If we couldn't find the page, or if we don't have a mode select
6297 * handler for it, send back an error to the user.
6299 if ((page_index == NULL)
6300 || (page_index->select_handler == NULL)) {
6301 ctl_set_invalid_field(ctsio,
6304 /*field*/ *len_used,
6307 free(ctsio->kern_data_ptr, M_CTL);
6308 ctl_done((union ctl_io *)ctsio);
6309 return (CTL_RETVAL_COMPLETE);
6312 if (page_index->page_code & SMPH_SPF) {
6313 page_len_offset = 2;
6317 page_len_offset = 1;
6321 * If the length the initiator gives us isn't the one we specify in
6322 * the mode page header, or if they didn't specify enough data in
6323 * the CDB to avoid truncating this page, kick out the request.
6325 if ((page_len != (page_index->page_len - page_len_offset -
6327 || (*len_left < page_index->page_len)) {
6330 ctl_set_invalid_field(ctsio,
6333 /*field*/ *len_used + page_len_offset,
6336 free(ctsio->kern_data_ptr, M_CTL);
6337 ctl_done((union ctl_io *)ctsio);
6338 return (CTL_RETVAL_COMPLETE);
6342 * Run through the mode page, checking to make sure that the bits
6343 * the user changed are actually legal for him to change.
6345 for (i = 0; i < page_index->page_len; i++) {
6346 uint8_t *user_byte, *change_mask, *current_byte;
6350 user_byte = (uint8_t *)page_header + i;
6351 change_mask = page_index->page_data +
6352 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6353 current_byte = page_index->page_data +
6354 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6357 * Check to see whether the user set any bits in this byte
6358 * that he is not allowed to set.
6360 if ((*user_byte & ~(*change_mask)) ==
6361 (*current_byte & ~(*change_mask)))
6365 * Go through bit by bit to determine which one is illegal.
6368 for (j = 7; j >= 0; j--) {
6369 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6370 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6375 ctl_set_invalid_field(ctsio,
6378 /*field*/ *len_used + i,
6381 free(ctsio->kern_data_ptr, M_CTL);
6382 ctl_done((union ctl_io *)ctsio);
6383 return (CTL_RETVAL_COMPLETE);
6387 * Decrement these before we call the page handler, since we may
6388 * end up getting called back one way or another before the handler
6389 * returns to this context.
6391 *len_left -= page_index->page_len;
6392 *len_used += page_index->page_len;
6394 retval = page_index->select_handler(ctsio, page_index,
6395 (uint8_t *)page_header);
6398 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6399 * wait until this queued command completes to finish processing
6400 * the mode page. If it returns anything other than
6401 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6402 * already set the sense information, freed the data pointer, and
6403 * completed the io for us.
6405 if (retval != CTL_RETVAL_COMPLETE)
6406 goto bailout_no_done;
6409 * If the initiator sent us more than one page, parse the next one.
6414 ctl_set_success(ctsio);
6415 free(ctsio->kern_data_ptr, M_CTL);
6416 ctl_done((union ctl_io *)ctsio);
6420 return (CTL_RETVAL_COMPLETE);
6425 ctl_mode_select(struct ctl_scsiio *ctsio)
6427 int param_len, pf, sp;
6428 int header_size, bd_len;
6429 int len_left, len_used;
6430 struct ctl_page_index *page_index;
6431 struct ctl_lun *lun;
6432 int control_dev, page_len;
6433 union ctl_modepage_info *modepage_info;
6445 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6447 if (lun->be_lun->lun_type != T_DIRECT)
6452 switch (ctsio->cdb[0]) {
6453 case MODE_SELECT_6: {
6454 struct scsi_mode_select_6 *cdb;
6456 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6458 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6459 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6461 param_len = cdb->length;
6462 header_size = sizeof(struct scsi_mode_header_6);
6465 case MODE_SELECT_10: {
6466 struct scsi_mode_select_10 *cdb;
6468 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6470 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6471 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6473 param_len = scsi_2btoul(cdb->length);
6474 header_size = sizeof(struct scsi_mode_header_10);
6478 ctl_set_invalid_opcode(ctsio);
6479 ctl_done((union ctl_io *)ctsio);
6480 return (CTL_RETVAL_COMPLETE);
6481 break; /* NOTREACHED */
6486 * "A parameter list length of zero indicates that the Data-Out Buffer
6487 * shall be empty. This condition shall not be considered as an error."
6489 if (param_len == 0) {
6490 ctl_set_success(ctsio);
6491 ctl_done((union ctl_io *)ctsio);
6492 return (CTL_RETVAL_COMPLETE);
6496 * Since we'll hit this the first time through, prior to
6497 * allocation, we don't need to free a data buffer here.
6499 if (param_len < header_size) {
6500 ctl_set_param_len_error(ctsio);
6501 ctl_done((union ctl_io *)ctsio);
6502 return (CTL_RETVAL_COMPLETE);
6506 * Allocate the data buffer and grab the user's data. In theory,
6507 * we shouldn't have to sanity check the parameter list length here
6508 * because the maximum size is 64K. We should be able to malloc
6509 * that much without too many problems.
6511 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6512 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6513 ctsio->kern_data_len = param_len;
6514 ctsio->kern_total_len = param_len;
6515 ctsio->kern_data_resid = 0;
6516 ctsio->kern_rel_offset = 0;
6517 ctsio->kern_sg_entries = 0;
6518 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6519 ctsio->be_move_done = ctl_config_move_done;
6520 ctl_datamove((union ctl_io *)ctsio);
6522 return (CTL_RETVAL_COMPLETE);
6525 switch (ctsio->cdb[0]) {
6526 case MODE_SELECT_6: {
6527 struct scsi_mode_header_6 *mh6;
6529 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6530 bd_len = mh6->blk_desc_len;
6533 case MODE_SELECT_10: {
6534 struct scsi_mode_header_10 *mh10;
6536 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6537 bd_len = scsi_2btoul(mh10->blk_desc_len);
6541 panic("Invalid CDB type %#x", ctsio->cdb[0]);
6545 if (param_len < (header_size + bd_len)) {
6546 free(ctsio->kern_data_ptr, M_CTL);
6547 ctl_set_param_len_error(ctsio);
6548 ctl_done((union ctl_io *)ctsio);
6549 return (CTL_RETVAL_COMPLETE);
6553 * Set the IO_CONT flag, so that if this I/O gets passed to
6554 * ctl_config_write_done(), it'll get passed back to
6555 * ctl_do_mode_select() for further processing, or completion if
6558 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6559 ctsio->io_cont = ctl_do_mode_select;
6561 modepage_info = (union ctl_modepage_info *)
6562 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6564 memset(modepage_info, 0, sizeof(*modepage_info));
6566 len_left = param_len - header_size - bd_len;
6567 len_used = header_size + bd_len;
6569 modepage_info->header.len_left = len_left;
6570 modepage_info->header.len_used = len_used;
6572 return (ctl_do_mode_select((union ctl_io *)ctsio));
6576 ctl_mode_sense(struct ctl_scsiio *ctsio)
6578 struct ctl_lun *lun;
6579 int pc, page_code, dbd, llba, subpage;
6580 int alloc_len, page_len, header_len, total_len;
6581 struct scsi_mode_block_descr *block_desc;
6582 struct ctl_page_index *page_index;
6590 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6592 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6594 if (lun->be_lun->lun_type != T_DIRECT)
6599 if (lun->flags & CTL_LUN_PR_RESERVED) {
6603 * XXX KDM need a lock here.
6605 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
6606 if ((lun->res_type == SPR_TYPE_EX_AC
6607 && residx != lun->pr_res_idx)
6608 || ((lun->res_type == SPR_TYPE_EX_AC_RO
6609 || lun->res_type == SPR_TYPE_EX_AC_AR)
6610 && lun->pr_keys[residx] == 0)) {
6611 ctl_set_reservation_conflict(ctsio);
6612 ctl_done((union ctl_io *)ctsio);
6613 return (CTL_RETVAL_COMPLETE);
6617 switch (ctsio->cdb[0]) {
6618 case MODE_SENSE_6: {
6619 struct scsi_mode_sense_6 *cdb;
6621 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6623 header_len = sizeof(struct scsi_mode_hdr_6);
6624 if (cdb->byte2 & SMS_DBD)
6627 header_len += sizeof(struct scsi_mode_block_descr);
6629 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6630 page_code = cdb->page & SMS_PAGE_CODE;
6631 subpage = cdb->subpage;
6632 alloc_len = cdb->length;
6635 case MODE_SENSE_10: {
6636 struct scsi_mode_sense_10 *cdb;
6638 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6640 header_len = sizeof(struct scsi_mode_hdr_10);
6642 if (cdb->byte2 & SMS_DBD)
6645 header_len += sizeof(struct scsi_mode_block_descr);
6646 if (cdb->byte2 & SMS10_LLBAA)
6648 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6649 page_code = cdb->page & SMS_PAGE_CODE;
6650 subpage = cdb->subpage;
6651 alloc_len = scsi_2btoul(cdb->length);
6655 ctl_set_invalid_opcode(ctsio);
6656 ctl_done((union ctl_io *)ctsio);
6657 return (CTL_RETVAL_COMPLETE);
6658 break; /* NOTREACHED */
6662 * We have to make a first pass through to calculate the size of
6663 * the pages that match the user's query. Then we allocate enough
6664 * memory to hold it, and actually copy the data into the buffer.
6666 switch (page_code) {
6667 case SMS_ALL_PAGES_PAGE: {
6673 * At the moment, values other than 0 and 0xff here are
6674 * reserved according to SPC-3.
6676 if ((subpage != SMS_SUBPAGE_PAGE_0)
6677 && (subpage != SMS_SUBPAGE_ALL)) {
6678 ctl_set_invalid_field(ctsio,
6684 ctl_done((union ctl_io *)ctsio);
6685 return (CTL_RETVAL_COMPLETE);
6688 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6689 if ((control_dev != 0)
6690 && (lun->mode_pages.index[i].page_flags &
6691 CTL_PAGE_FLAG_DISK_ONLY))
6695 * We don't use this subpage if the user didn't
6696 * request all subpages.
6698 if ((lun->mode_pages.index[i].subpage != 0)
6699 && (subpage == SMS_SUBPAGE_PAGE_0))
6703 printf("found page %#x len %d\n",
6704 lun->mode_pages.index[i].page_code &
6706 lun->mode_pages.index[i].page_len);
6708 page_len += lun->mode_pages.index[i].page_len;
6717 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6718 /* Look for the right page code */
6719 if ((lun->mode_pages.index[i].page_code &
6720 SMPH_PC_MASK) != page_code)
6723 /* Look for the right subpage or the subpage wildcard*/
6724 if ((lun->mode_pages.index[i].subpage != subpage)
6725 && (subpage != SMS_SUBPAGE_ALL))
6728 /* Make sure the page is supported for this dev type */
6729 if ((control_dev != 0)
6730 && (lun->mode_pages.index[i].page_flags &
6731 CTL_PAGE_FLAG_DISK_ONLY))
6735 printf("found page %#x len %d\n",
6736 lun->mode_pages.index[i].page_code &
6738 lun->mode_pages.index[i].page_len);
6741 page_len += lun->mode_pages.index[i].page_len;
6744 if (page_len == 0) {
6745 ctl_set_invalid_field(ctsio,
6751 ctl_done((union ctl_io *)ctsio);
6752 return (CTL_RETVAL_COMPLETE);
6758 total_len = header_len + page_len;
6760 printf("header_len = %d, page_len = %d, total_len = %d\n",
6761 header_len, page_len, total_len);
6764 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6765 ctsio->kern_sg_entries = 0;
6766 ctsio->kern_data_resid = 0;
6767 ctsio->kern_rel_offset = 0;
6768 if (total_len < alloc_len) {
6769 ctsio->residual = alloc_len - total_len;
6770 ctsio->kern_data_len = total_len;
6771 ctsio->kern_total_len = total_len;
6773 ctsio->residual = 0;
6774 ctsio->kern_data_len = alloc_len;
6775 ctsio->kern_total_len = alloc_len;
6778 switch (ctsio->cdb[0]) {
6779 case MODE_SENSE_6: {
6780 struct scsi_mode_hdr_6 *header;
6782 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6784 header->datalen = ctl_min(total_len - 1, 254);
6785 if (control_dev == 0) {
6786 header->dev_specific = 0x10; /* DPOFUA */
6787 if ((lun->flags & CTL_LUN_READONLY) ||
6788 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6789 .eca_and_aen & SCP_SWP) != 0)
6790 header->dev_specific |= 0x80; /* WP */
6793 header->block_descr_len = 0;
6795 header->block_descr_len =
6796 sizeof(struct scsi_mode_block_descr);
6797 block_desc = (struct scsi_mode_block_descr *)&header[1];
6800 case MODE_SENSE_10: {
6801 struct scsi_mode_hdr_10 *header;
6804 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6806 datalen = ctl_min(total_len - 2, 65533);
6807 scsi_ulto2b(datalen, header->datalen);
6808 if (control_dev == 0) {
6809 header->dev_specific = 0x10; /* DPOFUA */
6810 if ((lun->flags & CTL_LUN_READONLY) ||
6811 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6812 .eca_and_aen & SCP_SWP) != 0)
6813 header->dev_specific |= 0x80; /* WP */
6816 scsi_ulto2b(0, header->block_descr_len);
6818 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6819 header->block_descr_len);
6820 block_desc = (struct scsi_mode_block_descr *)&header[1];
6824 panic("invalid CDB type %#x", ctsio->cdb[0]);
6825 break; /* NOTREACHED */
6829 * If we've got a disk, use its blocksize in the block
6830 * descriptor. Otherwise, just set it to 0.
6833 if (control_dev == 0)
6834 scsi_ulto3b(lun->be_lun->blocksize,
6835 block_desc->block_len);
6837 scsi_ulto3b(0, block_desc->block_len);
6840 switch (page_code) {
6841 case SMS_ALL_PAGES_PAGE: {
6844 data_used = header_len;
6845 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6846 struct ctl_page_index *page_index;
6848 page_index = &lun->mode_pages.index[i];
6850 if ((control_dev != 0)
6851 && (page_index->page_flags &
6852 CTL_PAGE_FLAG_DISK_ONLY))
6856 * We don't use this subpage if the user didn't
6857 * request all subpages. We already checked (above)
6858 * to make sure the user only specified a subpage
6859 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6861 if ((page_index->subpage != 0)
6862 && (subpage == SMS_SUBPAGE_PAGE_0))
6866 * Call the handler, if it exists, to update the
6867 * page to the latest values.
6869 if (page_index->sense_handler != NULL)
6870 page_index->sense_handler(ctsio, page_index,pc);
6872 memcpy(ctsio->kern_data_ptr + data_used,
6873 page_index->page_data +
6874 (page_index->page_len * pc),
6875 page_index->page_len);
6876 data_used += page_index->page_len;
6883 data_used = header_len;
6885 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6886 struct ctl_page_index *page_index;
6888 page_index = &lun->mode_pages.index[i];
6890 /* Look for the right page code */
6891 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6894 /* Look for the right subpage or the subpage wildcard*/
6895 if ((page_index->subpage != subpage)
6896 && (subpage != SMS_SUBPAGE_ALL))
6899 /* Make sure the page is supported for this dev type */
6900 if ((control_dev != 0)
6901 && (page_index->page_flags &
6902 CTL_PAGE_FLAG_DISK_ONLY))
6906 * Call the handler, if it exists, to update the
6907 * page to the latest values.
6909 if (page_index->sense_handler != NULL)
6910 page_index->sense_handler(ctsio, page_index,pc);
6912 memcpy(ctsio->kern_data_ptr + data_used,
6913 page_index->page_data +
6914 (page_index->page_len * pc),
6915 page_index->page_len);
6916 data_used += page_index->page_len;
6922 ctsio->scsi_status = SCSI_STATUS_OK;
6924 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6925 ctsio->be_move_done = ctl_config_move_done;
6926 ctl_datamove((union ctl_io *)ctsio);
6928 return (CTL_RETVAL_COMPLETE);
6932 ctl_log_sense(struct ctl_scsiio *ctsio)
6934 struct ctl_lun *lun;
6935 int i, pc, page_code, subpage;
6936 int alloc_len, total_len;
6937 struct ctl_page_index *page_index;
6938 struct scsi_log_sense *cdb;
6939 struct scsi_log_header *header;
6941 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6943 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6944 cdb = (struct scsi_log_sense *)ctsio->cdb;
6945 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6946 page_code = cdb->page & SLS_PAGE_CODE;
6947 subpage = cdb->subpage;
6948 alloc_len = scsi_2btoul(cdb->length);
6951 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6952 page_index = &lun->log_pages.index[i];
6954 /* Look for the right page code */
6955 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6958 /* Look for the right subpage or the subpage wildcard*/
6959 if (page_index->subpage != subpage)
6964 if (i >= CTL_NUM_LOG_PAGES) {
6965 ctl_set_invalid_field(ctsio,
6971 ctl_done((union ctl_io *)ctsio);
6972 return (CTL_RETVAL_COMPLETE);
6975 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6977 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6978 ctsio->kern_sg_entries = 0;
6979 ctsio->kern_data_resid = 0;
6980 ctsio->kern_rel_offset = 0;
6981 if (total_len < alloc_len) {
6982 ctsio->residual = alloc_len - total_len;
6983 ctsio->kern_data_len = total_len;
6984 ctsio->kern_total_len = total_len;
6986 ctsio->residual = 0;
6987 ctsio->kern_data_len = alloc_len;
6988 ctsio->kern_total_len = alloc_len;
6991 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
6992 header->page = page_index->page_code;
6993 if (page_index->subpage) {
6994 header->page |= SL_SPF;
6995 header->subpage = page_index->subpage;
6997 scsi_ulto2b(page_index->page_len, header->datalen);
7000 * Call the handler, if it exists, to update the
7001 * page to the latest values.
7003 if (page_index->sense_handler != NULL)
7004 page_index->sense_handler(ctsio, page_index, pc);
7006 memcpy(header + 1, page_index->page_data, page_index->page_len);
7008 ctsio->scsi_status = SCSI_STATUS_OK;
7009 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7010 ctsio->be_move_done = ctl_config_move_done;
7011 ctl_datamove((union ctl_io *)ctsio);
7013 return (CTL_RETVAL_COMPLETE);
7017 ctl_read_capacity(struct ctl_scsiio *ctsio)
7019 struct scsi_read_capacity *cdb;
7020 struct scsi_read_capacity_data *data;
7021 struct ctl_lun *lun;
7024 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
7026 cdb = (struct scsi_read_capacity *)ctsio->cdb;
7028 lba = scsi_4btoul(cdb->addr);
7029 if (((cdb->pmi & SRC_PMI) == 0)
7031 ctl_set_invalid_field(/*ctsio*/ ctsio,
7037 ctl_done((union ctl_io *)ctsio);
7038 return (CTL_RETVAL_COMPLETE);
7041 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7043 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7044 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
7045 ctsio->residual = 0;
7046 ctsio->kern_data_len = sizeof(*data);
7047 ctsio->kern_total_len = sizeof(*data);
7048 ctsio->kern_data_resid = 0;
7049 ctsio->kern_rel_offset = 0;
7050 ctsio->kern_sg_entries = 0;
7053 * If the maximum LBA is greater than 0xfffffffe, the user must
7054 * issue a SERVICE ACTION IN (16) command, with the read capacity
7055 * serivce action set.
7057 if (lun->be_lun->maxlba > 0xfffffffe)
7058 scsi_ulto4b(0xffffffff, data->addr);
7060 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
7063 * XXX KDM this may not be 512 bytes...
7065 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7067 ctsio->scsi_status = SCSI_STATUS_OK;
7069 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7070 ctsio->be_move_done = ctl_config_move_done;
7071 ctl_datamove((union ctl_io *)ctsio);
7073 return (CTL_RETVAL_COMPLETE);
7077 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
7079 struct scsi_read_capacity_16 *cdb;
7080 struct scsi_read_capacity_data_long *data;
7081 struct ctl_lun *lun;
7085 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
7087 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
7089 alloc_len = scsi_4btoul(cdb->alloc_len);
7090 lba = scsi_8btou64(cdb->addr);
7092 if ((cdb->reladr & SRC16_PMI)
7094 ctl_set_invalid_field(/*ctsio*/ ctsio,
7100 ctl_done((union ctl_io *)ctsio);
7101 return (CTL_RETVAL_COMPLETE);
7104 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7106 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7107 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
7109 if (sizeof(*data) < alloc_len) {
7110 ctsio->residual = alloc_len - sizeof(*data);
7111 ctsio->kern_data_len = sizeof(*data);
7112 ctsio->kern_total_len = sizeof(*data);
7114 ctsio->residual = 0;
7115 ctsio->kern_data_len = alloc_len;
7116 ctsio->kern_total_len = alloc_len;
7118 ctsio->kern_data_resid = 0;
7119 ctsio->kern_rel_offset = 0;
7120 ctsio->kern_sg_entries = 0;
7122 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7123 /* XXX KDM this may not be 512 bytes... */
7124 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7125 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7126 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7127 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7128 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
7130 ctsio->scsi_status = SCSI_STATUS_OK;
7132 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7133 ctsio->be_move_done = ctl_config_move_done;
7134 ctl_datamove((union ctl_io *)ctsio);
7136 return (CTL_RETVAL_COMPLETE);
7140 ctl_read_defect(struct ctl_scsiio *ctsio)
7142 struct scsi_read_defect_data_10 *ccb10;
7143 struct scsi_read_defect_data_12 *ccb12;
7144 struct scsi_read_defect_data_hdr_10 *data10;
7145 struct scsi_read_defect_data_hdr_12 *data12;
7146 struct ctl_lun *lun;
7147 uint32_t alloc_len, data_len;
7150 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7152 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7153 if (lun->flags & CTL_LUN_PR_RESERVED) {
7157 * XXX KDM need a lock here.
7159 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
7160 if ((lun->res_type == SPR_TYPE_EX_AC
7161 && residx != lun->pr_res_idx)
7162 || ((lun->res_type == SPR_TYPE_EX_AC_RO
7163 || lun->res_type == SPR_TYPE_EX_AC_AR)
7164 && lun->pr_keys[residx] == 0)) {
7165 ctl_set_reservation_conflict(ctsio);
7166 ctl_done((union ctl_io *)ctsio);
7167 return (CTL_RETVAL_COMPLETE);
7171 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7172 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7173 format = ccb10->format;
7174 alloc_len = scsi_2btoul(ccb10->alloc_length);
7175 data_len = sizeof(*data10);
7177 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7178 format = ccb12->format;
7179 alloc_len = scsi_4btoul(ccb12->alloc_length);
7180 data_len = sizeof(*data12);
7182 if (alloc_len == 0) {
7183 ctl_set_success(ctsio);
7184 ctl_done((union ctl_io *)ctsio);
7185 return (CTL_RETVAL_COMPLETE);
7188 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7189 if (data_len < alloc_len) {
7190 ctsio->residual = alloc_len - data_len;
7191 ctsio->kern_data_len = data_len;
7192 ctsio->kern_total_len = data_len;
7194 ctsio->residual = 0;
7195 ctsio->kern_data_len = alloc_len;
7196 ctsio->kern_total_len = alloc_len;
7198 ctsio->kern_data_resid = 0;
7199 ctsio->kern_rel_offset = 0;
7200 ctsio->kern_sg_entries = 0;
7202 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7203 data10 = (struct scsi_read_defect_data_hdr_10 *)
7204 ctsio->kern_data_ptr;
7205 data10->format = format;
7206 scsi_ulto2b(0, data10->length);
7208 data12 = (struct scsi_read_defect_data_hdr_12 *)
7209 ctsio->kern_data_ptr;
7210 data12->format = format;
7211 scsi_ulto2b(0, data12->generation);
7212 scsi_ulto4b(0, data12->length);
7215 ctsio->scsi_status = SCSI_STATUS_OK;
7216 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7217 ctsio->be_move_done = ctl_config_move_done;
7218 ctl_datamove((union ctl_io *)ctsio);
7219 return (CTL_RETVAL_COMPLETE);
7223 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7225 struct scsi_maintenance_in *cdb;
7227 int alloc_len, ext, total_len = 0, g, p, pc, pg;
7228 int num_target_port_groups, num_target_ports, single;
7229 struct ctl_lun *lun;
7230 struct ctl_softc *softc;
7231 struct ctl_port *port;
7232 struct scsi_target_group_data *rtg_ptr;
7233 struct scsi_target_group_data_extended *rtg_ext_ptr;
7234 struct scsi_target_port_group_descriptor *tpg_desc;
7236 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7238 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7239 softc = control_softc;
7240 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7242 retval = CTL_RETVAL_COMPLETE;
7244 switch (cdb->byte2 & STG_PDF_MASK) {
7245 case STG_PDF_LENGTH:
7248 case STG_PDF_EXTENDED:
7252 ctl_set_invalid_field(/*ctsio*/ ctsio,
7258 ctl_done((union ctl_io *)ctsio);
7262 single = ctl_is_single;
7264 num_target_port_groups = 1;
7266 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
7267 num_target_ports = 0;
7268 mtx_lock(&softc->ctl_lock);
7269 STAILQ_FOREACH(port, &softc->port_list, links) {
7270 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7272 if (ctl_map_lun_back(port->targ_port, lun->lun) >= CTL_MAX_LUNS)
7276 mtx_unlock(&softc->ctl_lock);
7279 total_len = sizeof(struct scsi_target_group_data_extended);
7281 total_len = sizeof(struct scsi_target_group_data);
7282 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7283 num_target_port_groups +
7284 sizeof(struct scsi_target_port_descriptor) *
7285 num_target_ports * num_target_port_groups;
7287 alloc_len = scsi_4btoul(cdb->length);
7289 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7291 ctsio->kern_sg_entries = 0;
7293 if (total_len < alloc_len) {
7294 ctsio->residual = alloc_len - total_len;
7295 ctsio->kern_data_len = total_len;
7296 ctsio->kern_total_len = total_len;
7298 ctsio->residual = 0;
7299 ctsio->kern_data_len = alloc_len;
7300 ctsio->kern_total_len = alloc_len;
7302 ctsio->kern_data_resid = 0;
7303 ctsio->kern_rel_offset = 0;
7306 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7307 ctsio->kern_data_ptr;
7308 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7309 rtg_ext_ptr->format_type = 0x10;
7310 rtg_ext_ptr->implicit_transition_time = 0;
7311 tpg_desc = &rtg_ext_ptr->groups[0];
7313 rtg_ptr = (struct scsi_target_group_data *)
7314 ctsio->kern_data_ptr;
7315 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7316 tpg_desc = &rtg_ptr->groups[0];
7319 pg = ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS;
7320 mtx_lock(&softc->ctl_lock);
7321 for (g = 0; g < num_target_port_groups; g++) {
7323 tpg_desc->pref_state = TPG_PRIMARY |
7324 TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7326 tpg_desc->pref_state =
7327 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7328 tpg_desc->support = TPG_AO_SUP;
7330 tpg_desc->support |= TPG_AN_SUP;
7331 scsi_ulto2b(g + 1, tpg_desc->target_port_group);
7332 tpg_desc->status = TPG_IMPLICIT;
7334 STAILQ_FOREACH(port, &softc->port_list, links) {
7335 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7337 if (ctl_map_lun_back(port->targ_port, lun->lun) >=
7340 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS;
7341 scsi_ulto2b(p, tpg_desc->descriptors[pc].
7342 relative_target_port_identifier);
7345 tpg_desc->target_port_count = pc;
7346 tpg_desc = (struct scsi_target_port_group_descriptor *)
7347 &tpg_desc->descriptors[pc];
7349 mtx_unlock(&softc->ctl_lock);
7351 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7352 ctsio->be_move_done = ctl_config_move_done;
7354 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7355 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7356 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7357 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7358 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7360 ctl_datamove((union ctl_io *)ctsio);
7365 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7367 struct ctl_lun *lun;
7368 struct scsi_report_supported_opcodes *cdb;
7369 const struct ctl_cmd_entry *entry, *sentry;
7370 struct scsi_report_supported_opcodes_all *all;
7371 struct scsi_report_supported_opcodes_descr *descr;
7372 struct scsi_report_supported_opcodes_one *one;
7374 int alloc_len, total_len;
7375 int opcode, service_action, i, j, num;
7377 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7379 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7380 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7382 retval = CTL_RETVAL_COMPLETE;
7384 opcode = cdb->requested_opcode;
7385 service_action = scsi_2btoul(cdb->requested_service_action);
7386 switch (cdb->options & RSO_OPTIONS_MASK) {
7387 case RSO_OPTIONS_ALL:
7389 for (i = 0; i < 256; i++) {
7390 entry = &ctl_cmd_table[i];
7391 if (entry->flags & CTL_CMD_FLAG_SA5) {
7392 for (j = 0; j < 32; j++) {
7393 sentry = &((const struct ctl_cmd_entry *)
7395 if (ctl_cmd_applicable(
7396 lun->be_lun->lun_type, sentry))
7400 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7405 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7406 num * sizeof(struct scsi_report_supported_opcodes_descr);
7408 case RSO_OPTIONS_OC:
7409 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7410 ctl_set_invalid_field(/*ctsio*/ ctsio,
7416 ctl_done((union ctl_io *)ctsio);
7417 return (CTL_RETVAL_COMPLETE);
7419 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7421 case RSO_OPTIONS_OC_SA:
7422 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7423 service_action >= 32) {
7424 ctl_set_invalid_field(/*ctsio*/ ctsio,
7430 ctl_done((union ctl_io *)ctsio);
7431 return (CTL_RETVAL_COMPLETE);
7433 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7436 ctl_set_invalid_field(/*ctsio*/ ctsio,
7442 ctl_done((union ctl_io *)ctsio);
7443 return (CTL_RETVAL_COMPLETE);
7446 alloc_len = scsi_4btoul(cdb->length);
7448 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7450 ctsio->kern_sg_entries = 0;
7452 if (total_len < alloc_len) {
7453 ctsio->residual = alloc_len - total_len;
7454 ctsio->kern_data_len = total_len;
7455 ctsio->kern_total_len = total_len;
7457 ctsio->residual = 0;
7458 ctsio->kern_data_len = alloc_len;
7459 ctsio->kern_total_len = alloc_len;
7461 ctsio->kern_data_resid = 0;
7462 ctsio->kern_rel_offset = 0;
7464 switch (cdb->options & RSO_OPTIONS_MASK) {
7465 case RSO_OPTIONS_ALL:
7466 all = (struct scsi_report_supported_opcodes_all *)
7467 ctsio->kern_data_ptr;
7469 for (i = 0; i < 256; i++) {
7470 entry = &ctl_cmd_table[i];
7471 if (entry->flags & CTL_CMD_FLAG_SA5) {
7472 for (j = 0; j < 32; j++) {
7473 sentry = &((const struct ctl_cmd_entry *)
7475 if (!ctl_cmd_applicable(
7476 lun->be_lun->lun_type, sentry))
7478 descr = &all->descr[num++];
7480 scsi_ulto2b(j, descr->service_action);
7481 descr->flags = RSO_SERVACTV;
7482 scsi_ulto2b(sentry->length,
7486 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7489 descr = &all->descr[num++];
7491 scsi_ulto2b(0, descr->service_action);
7493 scsi_ulto2b(entry->length, descr->cdb_length);
7497 num * sizeof(struct scsi_report_supported_opcodes_descr),
7500 case RSO_OPTIONS_OC:
7501 one = (struct scsi_report_supported_opcodes_one *)
7502 ctsio->kern_data_ptr;
7503 entry = &ctl_cmd_table[opcode];
7505 case RSO_OPTIONS_OC_SA:
7506 one = (struct scsi_report_supported_opcodes_one *)
7507 ctsio->kern_data_ptr;
7508 entry = &ctl_cmd_table[opcode];
7509 entry = &((const struct ctl_cmd_entry *)
7510 entry->execute)[service_action];
7512 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7514 scsi_ulto2b(entry->length, one->cdb_length);
7515 one->cdb_usage[0] = opcode;
7516 memcpy(&one->cdb_usage[1], entry->usage,
7523 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7524 ctsio->be_move_done = ctl_config_move_done;
7526 ctl_datamove((union ctl_io *)ctsio);
7531 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7533 struct ctl_lun *lun;
7534 struct scsi_report_supported_tmf *cdb;
7535 struct scsi_report_supported_tmf_data *data;
7537 int alloc_len, total_len;
7539 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7541 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7542 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7544 retval = CTL_RETVAL_COMPLETE;
7546 total_len = sizeof(struct scsi_report_supported_tmf_data);
7547 alloc_len = scsi_4btoul(cdb->length);
7549 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7551 ctsio->kern_sg_entries = 0;
7553 if (total_len < alloc_len) {
7554 ctsio->residual = alloc_len - total_len;
7555 ctsio->kern_data_len = total_len;
7556 ctsio->kern_total_len = total_len;
7558 ctsio->residual = 0;
7559 ctsio->kern_data_len = alloc_len;
7560 ctsio->kern_total_len = alloc_len;
7562 ctsio->kern_data_resid = 0;
7563 ctsio->kern_rel_offset = 0;
7565 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr;
7566 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS;
7567 data->byte2 |= RST_ITNRS;
7569 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7570 ctsio->be_move_done = ctl_config_move_done;
7572 ctl_datamove((union ctl_io *)ctsio);
7577 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7579 struct ctl_lun *lun;
7580 struct scsi_report_timestamp *cdb;
7581 struct scsi_report_timestamp_data *data;
7585 int alloc_len, total_len;
7587 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7589 cdb = (struct scsi_report_timestamp *)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_timestamp_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_timestamp_data *)ctsio->kern_data_ptr;
7614 scsi_ulto2b(sizeof(*data) - 2, data->length);
7615 data->origin = RTS_ORIG_OUTSIDE;
7617 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7618 scsi_ulto4b(timestamp >> 16, data->timestamp);
7619 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7621 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7622 ctsio->be_move_done = ctl_config_move_done;
7624 ctl_datamove((union ctl_io *)ctsio);
7629 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7631 struct scsi_per_res_in *cdb;
7632 int alloc_len, total_len = 0;
7633 /* struct scsi_per_res_in_rsrv in_data; */
7634 struct ctl_lun *lun;
7635 struct ctl_softc *softc;
7637 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7639 softc = control_softc;
7641 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7643 alloc_len = scsi_2btoul(cdb->length);
7645 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7648 mtx_lock(&lun->lun_lock);
7649 switch (cdb->action) {
7650 case SPRI_RK: /* read keys */
7651 total_len = sizeof(struct scsi_per_res_in_keys) +
7653 sizeof(struct scsi_per_res_key);
7655 case SPRI_RR: /* read reservation */
7656 if (lun->flags & CTL_LUN_PR_RESERVED)
7657 total_len = sizeof(struct scsi_per_res_in_rsrv);
7659 total_len = sizeof(struct scsi_per_res_in_header);
7661 case SPRI_RC: /* report capabilities */
7662 total_len = sizeof(struct scsi_per_res_cap);
7664 case SPRI_RS: /* read full status */
7665 total_len = sizeof(struct scsi_per_res_in_header) +
7666 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7670 panic("Invalid PR type %x", cdb->action);
7672 mtx_unlock(&lun->lun_lock);
7674 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7676 if (total_len < alloc_len) {
7677 ctsio->residual = alloc_len - total_len;
7678 ctsio->kern_data_len = total_len;
7679 ctsio->kern_total_len = total_len;
7681 ctsio->residual = 0;
7682 ctsio->kern_data_len = alloc_len;
7683 ctsio->kern_total_len = alloc_len;
7686 ctsio->kern_data_resid = 0;
7687 ctsio->kern_rel_offset = 0;
7688 ctsio->kern_sg_entries = 0;
7690 mtx_lock(&lun->lun_lock);
7691 switch (cdb->action) {
7692 case SPRI_RK: { // read keys
7693 struct scsi_per_res_in_keys *res_keys;
7696 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7699 * We had to drop the lock to allocate our buffer, which
7700 * leaves time for someone to come in with another
7701 * persistent reservation. (That is unlikely, though,
7702 * since this should be the only persistent reservation
7703 * command active right now.)
7705 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7706 (lun->pr_key_count *
7707 sizeof(struct scsi_per_res_key)))){
7708 mtx_unlock(&lun->lun_lock);
7709 free(ctsio->kern_data_ptr, M_CTL);
7710 printf("%s: reservation length changed, retrying\n",
7715 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7717 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7718 lun->pr_key_count, res_keys->header.length);
7720 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7721 if (lun->pr_keys[i] == 0)
7725 * We used lun->pr_key_count to calculate the
7726 * size to allocate. If it turns out the number of
7727 * initiators with the registered flag set is
7728 * larger than that (i.e. they haven't been kept in
7729 * sync), we've got a problem.
7731 if (key_count >= lun->pr_key_count) {
7733 csevent_log(CSC_CTL | CSC_SHELF_SW |
7735 csevent_LogType_Fault,
7736 csevent_AlertLevel_Yellow,
7737 csevent_FRU_ShelfController,
7738 csevent_FRU_Firmware,
7739 csevent_FRU_Unknown,
7740 "registered keys %d >= key "
7741 "count %d", key_count,
7747 scsi_u64to8b(lun->pr_keys[i],
7748 res_keys->keys[key_count].key);
7753 case SPRI_RR: { // read reservation
7754 struct scsi_per_res_in_rsrv *res;
7755 int tmp_len, header_only;
7757 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7759 scsi_ulto4b(lun->PRGeneration, res->header.generation);
7761 if (lun->flags & CTL_LUN_PR_RESERVED)
7763 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7764 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7765 res->header.length);
7768 tmp_len = sizeof(struct scsi_per_res_in_header);
7769 scsi_ulto4b(0, res->header.length);
7774 * We had to drop the lock to allocate our buffer, which
7775 * leaves time for someone to come in with another
7776 * persistent reservation. (That is unlikely, though,
7777 * since this should be the only persistent reservation
7778 * command active right now.)
7780 if (tmp_len != total_len) {
7781 mtx_unlock(&lun->lun_lock);
7782 free(ctsio->kern_data_ptr, M_CTL);
7783 printf("%s: reservation status changed, retrying\n",
7789 * No reservation held, so we're done.
7791 if (header_only != 0)
7795 * If the registration is an All Registrants type, the key
7796 * is 0, since it doesn't really matter.
7798 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7799 scsi_u64to8b(lun->pr_keys[lun->pr_res_idx],
7800 res->data.reservation);
7802 res->data.scopetype = lun->res_type;
7805 case SPRI_RC: //report capabilities
7807 struct scsi_per_res_cap *res_cap;
7810 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7811 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7812 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5;
7813 type_mask = SPRI_TM_WR_EX_AR |
7819 scsi_ulto2b(type_mask, res_cap->type_mask);
7822 case SPRI_RS: { // read full status
7823 struct scsi_per_res_in_full *res_status;
7824 struct scsi_per_res_in_full_desc *res_desc;
7825 struct ctl_port *port;
7828 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7831 * We had to drop the lock to allocate our buffer, which
7832 * leaves time for someone to come in with another
7833 * persistent reservation. (That is unlikely, though,
7834 * since this should be the only persistent reservation
7835 * command active right now.)
7837 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7838 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7839 lun->pr_key_count)){
7840 mtx_unlock(&lun->lun_lock);
7841 free(ctsio->kern_data_ptr, M_CTL);
7842 printf("%s: reservation length changed, retrying\n",
7847 scsi_ulto4b(lun->PRGeneration, res_status->header.generation);
7849 res_desc = &res_status->desc[0];
7850 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7851 if (lun->pr_keys[i] == 0)
7854 scsi_u64to8b(lun->pr_keys[i], res_desc->res_key.key);
7855 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7856 (lun->pr_res_idx == i ||
7857 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7858 res_desc->flags = SPRI_FULL_R_HOLDER;
7859 res_desc->scopetype = lun->res_type;
7861 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7862 res_desc->rel_trgt_port_id);
7864 port = softc->ctl_ports[
7865 ctl_port_idx(i / CTL_MAX_INIT_PER_PORT)];
7867 len = ctl_create_iid(port,
7868 i % CTL_MAX_INIT_PER_PORT,
7869 res_desc->transport_id);
7870 scsi_ulto4b(len, res_desc->additional_length);
7871 res_desc = (struct scsi_per_res_in_full_desc *)
7872 &res_desc->transport_id[len];
7874 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7875 res_status->header.length);
7880 * This is a bug, because we just checked for this above,
7881 * and should have returned an error.
7883 panic("Invalid PR type %x", cdb->action);
7884 break; /* NOTREACHED */
7886 mtx_unlock(&lun->lun_lock);
7888 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7889 ctsio->be_move_done = ctl_config_move_done;
7891 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7892 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7893 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7894 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7895 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7897 ctl_datamove((union ctl_io *)ctsio);
7899 return (CTL_RETVAL_COMPLETE);
7903 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7907 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7908 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7909 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7910 struct scsi_per_res_out_parms* param)
7912 union ctl_ha_msg persis_io;
7918 mtx_lock(&lun->lun_lock);
7919 if (sa_res_key == 0) {
7920 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7921 /* validate scope and type */
7922 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7924 mtx_unlock(&lun->lun_lock);
7925 ctl_set_invalid_field(/*ctsio*/ ctsio,
7931 ctl_done((union ctl_io *)ctsio);
7935 if (type>8 || type==2 || type==4 || type==0) {
7936 mtx_unlock(&lun->lun_lock);
7937 ctl_set_invalid_field(/*ctsio*/ ctsio,
7943 ctl_done((union ctl_io *)ctsio);
7948 * Unregister everybody else and build UA for
7951 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7952 if (i == residx || lun->pr_keys[i] == 0)
7956 && i <CTL_MAX_INITIATORS)
7957 lun->pending_ua[i] |=
7959 else if (persis_offset
7960 && i >= persis_offset)
7961 lun->pending_ua[i-persis_offset] |=
7963 lun->pr_keys[i] = 0;
7965 lun->pr_key_count = 1;
7966 lun->res_type = type;
7967 if (lun->res_type != SPR_TYPE_WR_EX_AR
7968 && lun->res_type != SPR_TYPE_EX_AC_AR)
7969 lun->pr_res_idx = residx;
7971 /* send msg to other side */
7972 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7973 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7974 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7975 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7976 persis_io.pr.pr_info.res_type = type;
7977 memcpy(persis_io.pr.pr_info.sa_res_key,
7978 param->serv_act_res_key,
7979 sizeof(param->serv_act_res_key));
7980 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7981 &persis_io, sizeof(persis_io), 0)) >
7982 CTL_HA_STATUS_SUCCESS) {
7983 printf("CTL:Persis Out error returned "
7984 "from ctl_ha_msg_send %d\n",
7988 /* not all registrants */
7989 mtx_unlock(&lun->lun_lock);
7990 free(ctsio->kern_data_ptr, M_CTL);
7991 ctl_set_invalid_field(ctsio,
7997 ctl_done((union ctl_io *)ctsio);
8000 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8001 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
8004 if (res_key == sa_res_key) {
8007 * The spec implies this is not good but doesn't
8008 * say what to do. There are two choices either
8009 * generate a res conflict or check condition
8010 * with illegal field in parameter data. Since
8011 * that is what is done when the sa_res_key is
8012 * zero I'll take that approach since this has
8013 * to do with the sa_res_key.
8015 mtx_unlock(&lun->lun_lock);
8016 free(ctsio->kern_data_ptr, M_CTL);
8017 ctl_set_invalid_field(ctsio,
8023 ctl_done((union ctl_io *)ctsio);
8027 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8028 if (lun->pr_keys[i] != sa_res_key)
8032 lun->pr_keys[i] = 0;
8033 lun->pr_key_count--;
8035 if (!persis_offset && i < CTL_MAX_INITIATORS)
8036 lun->pending_ua[i] |= CTL_UA_REG_PREEMPT;
8037 else if (persis_offset && i >= persis_offset)
8038 lun->pending_ua[i-persis_offset] |=
8042 mtx_unlock(&lun->lun_lock);
8043 free(ctsio->kern_data_ptr, M_CTL);
8044 ctl_set_reservation_conflict(ctsio);
8045 ctl_done((union ctl_io *)ctsio);
8046 return (CTL_RETVAL_COMPLETE);
8048 /* send msg to other side */
8049 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8050 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8051 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8052 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8053 persis_io.pr.pr_info.res_type = type;
8054 memcpy(persis_io.pr.pr_info.sa_res_key,
8055 param->serv_act_res_key,
8056 sizeof(param->serv_act_res_key));
8057 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8058 &persis_io, sizeof(persis_io), 0)) >
8059 CTL_HA_STATUS_SUCCESS) {
8060 printf("CTL:Persis Out error returned from "
8061 "ctl_ha_msg_send %d\n", isc_retval);
8064 /* Reserved but not all registrants */
8065 /* sa_res_key is res holder */
8066 if (sa_res_key == lun->pr_keys[lun->pr_res_idx]) {
8067 /* validate scope and type */
8068 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
8070 mtx_unlock(&lun->lun_lock);
8071 ctl_set_invalid_field(/*ctsio*/ ctsio,
8077 ctl_done((union ctl_io *)ctsio);
8081 if (type>8 || type==2 || type==4 || type==0) {
8082 mtx_unlock(&lun->lun_lock);
8083 ctl_set_invalid_field(/*ctsio*/ ctsio,
8089 ctl_done((union ctl_io *)ctsio);
8095 * if sa_res_key != res_key remove all
8096 * registrants w/sa_res_key and generate UA
8097 * for these registrants(Registrations
8098 * Preempted) if it wasn't an exclusive
8099 * reservation generate UA(Reservations
8100 * Preempted) for all other registered nexuses
8101 * if the type has changed. Establish the new
8102 * reservation and holder. If res_key and
8103 * sa_res_key are the same do the above
8104 * except don't unregister the res holder.
8107 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8108 if (i == residx || lun->pr_keys[i] == 0)
8111 if (sa_res_key == lun->pr_keys[i]) {
8112 lun->pr_keys[i] = 0;
8113 lun->pr_key_count--;
8116 && i < CTL_MAX_INITIATORS)
8117 lun->pending_ua[i] |=
8119 else if (persis_offset
8120 && i >= persis_offset)
8121 lun->pending_ua[i-persis_offset] |=
8123 } else if (type != lun->res_type
8124 && (lun->res_type == SPR_TYPE_WR_EX_RO
8125 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
8127 && i < CTL_MAX_INITIATORS)
8128 lun->pending_ua[i] |=
8130 else if (persis_offset
8131 && i >= persis_offset)
8137 lun->res_type = type;
8138 if (lun->res_type != SPR_TYPE_WR_EX_AR
8139 && lun->res_type != SPR_TYPE_EX_AC_AR)
8140 lun->pr_res_idx = residx;
8142 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8144 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8145 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8146 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8147 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8148 persis_io.pr.pr_info.res_type = type;
8149 memcpy(persis_io.pr.pr_info.sa_res_key,
8150 param->serv_act_res_key,
8151 sizeof(param->serv_act_res_key));
8152 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8153 &persis_io, sizeof(persis_io), 0)) >
8154 CTL_HA_STATUS_SUCCESS) {
8155 printf("CTL:Persis Out error returned "
8156 "from ctl_ha_msg_send %d\n",
8161 * sa_res_key is not the res holder just
8162 * remove registrants
8166 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8167 if (sa_res_key != lun->pr_keys[i])
8171 lun->pr_keys[i] = 0;
8172 lun->pr_key_count--;
8175 && i < CTL_MAX_INITIATORS)
8176 lun->pending_ua[i] |=
8178 else if (persis_offset
8179 && i >= persis_offset)
8180 lun->pending_ua[i-persis_offset] |=
8185 mtx_unlock(&lun->lun_lock);
8186 free(ctsio->kern_data_ptr, M_CTL);
8187 ctl_set_reservation_conflict(ctsio);
8188 ctl_done((union ctl_io *)ctsio);
8191 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8192 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8193 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8194 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8195 persis_io.pr.pr_info.res_type = type;
8196 memcpy(persis_io.pr.pr_info.sa_res_key,
8197 param->serv_act_res_key,
8198 sizeof(param->serv_act_res_key));
8199 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8200 &persis_io, sizeof(persis_io), 0)) >
8201 CTL_HA_STATUS_SUCCESS) {
8202 printf("CTL:Persis Out error returned "
8203 "from ctl_ha_msg_send %d\n",
8209 lun->PRGeneration++;
8210 mtx_unlock(&lun->lun_lock);
8216 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8218 uint64_t sa_res_key;
8221 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8223 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8224 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8225 || sa_res_key != lun->pr_keys[lun->pr_res_idx]) {
8226 if (sa_res_key == 0) {
8228 * Unregister everybody else and build UA for
8231 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8232 if (i == msg->pr.pr_info.residx ||
8233 lun->pr_keys[i] == 0)
8237 && i < CTL_MAX_INITIATORS)
8238 lun->pending_ua[i] |=
8240 else if (persis_offset && i >= persis_offset)
8241 lun->pending_ua[i - persis_offset] |=
8243 lun->pr_keys[i] = 0;
8246 lun->pr_key_count = 1;
8247 lun->res_type = msg->pr.pr_info.res_type;
8248 if (lun->res_type != SPR_TYPE_WR_EX_AR
8249 && lun->res_type != SPR_TYPE_EX_AC_AR)
8250 lun->pr_res_idx = msg->pr.pr_info.residx;
8252 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8253 if (sa_res_key == lun->pr_keys[i])
8256 lun->pr_keys[i] = 0;
8257 lun->pr_key_count--;
8260 && i < persis_offset)
8261 lun->pending_ua[i] |=
8263 else if (persis_offset
8264 && i >= persis_offset)
8265 lun->pending_ua[i - persis_offset] |=
8270 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8271 if (i == msg->pr.pr_info.residx ||
8272 lun->pr_keys[i] == 0)
8275 if (sa_res_key == lun->pr_keys[i]) {
8276 lun->pr_keys[i] = 0;
8277 lun->pr_key_count--;
8279 && i < CTL_MAX_INITIATORS)
8280 lun->pending_ua[i] |=
8282 else if (persis_offset
8283 && i >= persis_offset)
8284 lun->pending_ua[i - persis_offset] |=
8286 } else if (msg->pr.pr_info.res_type != lun->res_type
8287 && (lun->res_type == SPR_TYPE_WR_EX_RO
8288 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
8290 && i < persis_offset)
8291 lun->pending_ua[i] |=
8293 else if (persis_offset
8294 && i >= persis_offset)
8295 lun->pending_ua[i - persis_offset] |=
8299 lun->res_type = msg->pr.pr_info.res_type;
8300 if (lun->res_type != SPR_TYPE_WR_EX_AR
8301 && lun->res_type != SPR_TYPE_EX_AC_AR)
8302 lun->pr_res_idx = msg->pr.pr_info.residx;
8304 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8306 lun->PRGeneration++;
8312 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8316 u_int32_t param_len;
8317 struct scsi_per_res_out *cdb;
8318 struct ctl_lun *lun;
8319 struct scsi_per_res_out_parms* param;
8320 struct ctl_softc *softc;
8322 uint64_t res_key, sa_res_key;
8324 union ctl_ha_msg persis_io;
8327 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8329 retval = CTL_RETVAL_COMPLETE;
8331 softc = control_softc;
8333 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8334 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8337 * We only support whole-LUN scope. The scope & type are ignored for
8338 * register, register and ignore existing key and clear.
8339 * We sometimes ignore scope and type on preempts too!!
8340 * Verify reservation type here as well.
8342 type = cdb->scope_type & SPR_TYPE_MASK;
8343 if ((cdb->action == SPRO_RESERVE)
8344 || (cdb->action == SPRO_RELEASE)) {
8345 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8346 ctl_set_invalid_field(/*ctsio*/ ctsio,
8352 ctl_done((union ctl_io *)ctsio);
8353 return (CTL_RETVAL_COMPLETE);
8356 if (type>8 || type==2 || type==4 || type==0) {
8357 ctl_set_invalid_field(/*ctsio*/ ctsio,
8363 ctl_done((union ctl_io *)ctsio);
8364 return (CTL_RETVAL_COMPLETE);
8368 param_len = scsi_4btoul(cdb->length);
8370 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8371 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8372 ctsio->kern_data_len = param_len;
8373 ctsio->kern_total_len = param_len;
8374 ctsio->kern_data_resid = 0;
8375 ctsio->kern_rel_offset = 0;
8376 ctsio->kern_sg_entries = 0;
8377 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8378 ctsio->be_move_done = ctl_config_move_done;
8379 ctl_datamove((union ctl_io *)ctsio);
8381 return (CTL_RETVAL_COMPLETE);
8384 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8386 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8387 res_key = scsi_8btou64(param->res_key.key);
8388 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8391 * Validate the reservation key here except for SPRO_REG_IGNO
8392 * This must be done for all other service actions
8394 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8395 mtx_lock(&lun->lun_lock);
8396 if (lun->pr_keys[residx] != 0) {
8397 if (res_key != lun->pr_keys[residx]) {
8399 * The current key passed in doesn't match
8400 * the one the initiator previously
8403 mtx_unlock(&lun->lun_lock);
8404 free(ctsio->kern_data_ptr, M_CTL);
8405 ctl_set_reservation_conflict(ctsio);
8406 ctl_done((union ctl_io *)ctsio);
8407 return (CTL_RETVAL_COMPLETE);
8409 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8411 * We are not registered
8413 mtx_unlock(&lun->lun_lock);
8414 free(ctsio->kern_data_ptr, M_CTL);
8415 ctl_set_reservation_conflict(ctsio);
8416 ctl_done((union ctl_io *)ctsio);
8417 return (CTL_RETVAL_COMPLETE);
8418 } else if (res_key != 0) {
8420 * We are not registered and trying to register but
8421 * the register key isn't zero.
8423 mtx_unlock(&lun->lun_lock);
8424 free(ctsio->kern_data_ptr, M_CTL);
8425 ctl_set_reservation_conflict(ctsio);
8426 ctl_done((union ctl_io *)ctsio);
8427 return (CTL_RETVAL_COMPLETE);
8429 mtx_unlock(&lun->lun_lock);
8432 switch (cdb->action & SPRO_ACTION_MASK) {
8434 case SPRO_REG_IGNO: {
8437 printf("Registration received\n");
8441 * We don't support any of these options, as we report in
8442 * the read capabilities request (see
8443 * ctl_persistent_reserve_in(), above).
8445 if ((param->flags & SPR_SPEC_I_PT)
8446 || (param->flags & SPR_ALL_TG_PT)
8447 || (param->flags & SPR_APTPL)) {
8450 if (param->flags & SPR_APTPL)
8452 else if (param->flags & SPR_ALL_TG_PT)
8454 else /* SPR_SPEC_I_PT */
8457 free(ctsio->kern_data_ptr, M_CTL);
8458 ctl_set_invalid_field(ctsio,
8464 ctl_done((union ctl_io *)ctsio);
8465 return (CTL_RETVAL_COMPLETE);
8468 mtx_lock(&lun->lun_lock);
8471 * The initiator wants to clear the
8474 if (sa_res_key == 0) {
8476 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8477 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8478 && lun->pr_keys[residx] == 0)) {
8479 mtx_unlock(&lun->lun_lock);
8483 lun->pr_keys[residx] = 0;
8484 lun->pr_key_count--;
8486 if (residx == lun->pr_res_idx) {
8487 lun->flags &= ~CTL_LUN_PR_RESERVED;
8488 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8490 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8491 || lun->res_type == SPR_TYPE_EX_AC_RO)
8492 && lun->pr_key_count) {
8494 * If the reservation is a registrants
8495 * only type we need to generate a UA
8496 * for other registered inits. The
8497 * sense code should be RESERVATIONS
8501 for (i = 0; i < CTL_MAX_INITIATORS;i++){
8503 i + persis_offset] == 0)
8505 lun->pending_ua[i] |=
8510 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8511 if (lun->pr_key_count==0) {
8512 lun->flags &= ~CTL_LUN_PR_RESERVED;
8514 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8517 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8518 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8519 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8520 persis_io.pr.pr_info.residx = residx;
8521 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8522 &persis_io, sizeof(persis_io), 0 )) >
8523 CTL_HA_STATUS_SUCCESS) {
8524 printf("CTL:Persis Out error returned from "
8525 "ctl_ha_msg_send %d\n", isc_retval);
8527 } else /* sa_res_key != 0 */ {
8530 * If we aren't registered currently then increment
8531 * the key count and set the registered flag.
8533 if (lun->pr_keys[residx] == 0)
8534 lun->pr_key_count++;
8535 lun->pr_keys[residx] = sa_res_key;
8537 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8538 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8539 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8540 persis_io.pr.pr_info.residx = residx;
8541 memcpy(persis_io.pr.pr_info.sa_res_key,
8542 param->serv_act_res_key,
8543 sizeof(param->serv_act_res_key));
8544 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8545 &persis_io, sizeof(persis_io), 0)) >
8546 CTL_HA_STATUS_SUCCESS) {
8547 printf("CTL:Persis Out error returned from "
8548 "ctl_ha_msg_send %d\n", isc_retval);
8551 lun->PRGeneration++;
8552 mtx_unlock(&lun->lun_lock);
8558 printf("Reserve executed type %d\n", type);
8560 mtx_lock(&lun->lun_lock);
8561 if (lun->flags & CTL_LUN_PR_RESERVED) {
8563 * if this isn't the reservation holder and it's
8564 * not a "all registrants" type or if the type is
8565 * different then we have a conflict
8567 if ((lun->pr_res_idx != residx
8568 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8569 || lun->res_type != type) {
8570 mtx_unlock(&lun->lun_lock);
8571 free(ctsio->kern_data_ptr, M_CTL);
8572 ctl_set_reservation_conflict(ctsio);
8573 ctl_done((union ctl_io *)ctsio);
8574 return (CTL_RETVAL_COMPLETE);
8576 mtx_unlock(&lun->lun_lock);
8577 } else /* create a reservation */ {
8579 * If it's not an "all registrants" type record
8580 * reservation holder
8582 if (type != SPR_TYPE_WR_EX_AR
8583 && type != SPR_TYPE_EX_AC_AR)
8584 lun->pr_res_idx = residx; /* Res holder */
8586 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8588 lun->flags |= CTL_LUN_PR_RESERVED;
8589 lun->res_type = type;
8591 mtx_unlock(&lun->lun_lock);
8593 /* send msg to other side */
8594 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8595 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8596 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8597 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8598 persis_io.pr.pr_info.res_type = type;
8599 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8600 &persis_io, sizeof(persis_io), 0)) >
8601 CTL_HA_STATUS_SUCCESS) {
8602 printf("CTL:Persis Out error returned from "
8603 "ctl_ha_msg_send %d\n", isc_retval);
8609 mtx_lock(&lun->lun_lock);
8610 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8611 /* No reservation exists return good status */
8612 mtx_unlock(&lun->lun_lock);
8616 * Is this nexus a reservation holder?
8618 if (lun->pr_res_idx != residx
8619 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8621 * not a res holder return good status but
8624 mtx_unlock(&lun->lun_lock);
8628 if (lun->res_type != type) {
8629 mtx_unlock(&lun->lun_lock);
8630 free(ctsio->kern_data_ptr, M_CTL);
8631 ctl_set_illegal_pr_release(ctsio);
8632 ctl_done((union ctl_io *)ctsio);
8633 return (CTL_RETVAL_COMPLETE);
8636 /* okay to release */
8637 lun->flags &= ~CTL_LUN_PR_RESERVED;
8638 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8642 * if this isn't an exclusive access
8643 * res generate UA for all other
8646 if (type != SPR_TYPE_EX_AC
8647 && type != SPR_TYPE_WR_EX) {
8648 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8650 lun->pr_keys[i + persis_offset] == 0)
8652 lun->pending_ua[i] |= CTL_UA_RES_RELEASE;
8655 mtx_unlock(&lun->lun_lock);
8656 /* Send msg to other side */
8657 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8658 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8659 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8660 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io,
8661 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8662 printf("CTL:Persis Out error returned from "
8663 "ctl_ha_msg_send %d\n", isc_retval);
8668 /* send msg to other side */
8670 mtx_lock(&lun->lun_lock);
8671 lun->flags &= ~CTL_LUN_PR_RESERVED;
8673 lun->pr_key_count = 0;
8674 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8676 lun->pr_keys[residx] = 0;
8678 for (i=0; i < 2*CTL_MAX_INITIATORS; i++)
8679 if (lun->pr_keys[i] != 0) {
8680 if (!persis_offset && i < CTL_MAX_INITIATORS)
8681 lun->pending_ua[i] |=
8683 else if (persis_offset && i >= persis_offset)
8684 lun->pending_ua[i-persis_offset] |=
8687 lun->pr_keys[i] = 0;
8689 lun->PRGeneration++;
8690 mtx_unlock(&lun->lun_lock);
8691 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8692 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8693 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8694 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8695 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8696 printf("CTL:Persis Out error returned from "
8697 "ctl_ha_msg_send %d\n", isc_retval);
8701 case SPRO_PREEMPT: {
8704 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8705 residx, ctsio, cdb, param);
8707 return (CTL_RETVAL_COMPLETE);
8711 panic("Invalid PR type %x", cdb->action);
8715 free(ctsio->kern_data_ptr, M_CTL);
8716 ctl_set_success(ctsio);
8717 ctl_done((union ctl_io *)ctsio);
8723 * This routine is for handling a message from the other SC pertaining to
8724 * persistent reserve out. All the error checking will have been done
8725 * so only perorming the action need be done here to keep the two
8729 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8731 struct ctl_lun *lun;
8732 struct ctl_softc *softc;
8736 softc = control_softc;
8738 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8739 lun = softc->ctl_luns[targ_lun];
8740 mtx_lock(&lun->lun_lock);
8741 switch(msg->pr.pr_info.action) {
8742 case CTL_PR_REG_KEY:
8743 if (lun->pr_keys[msg->pr.pr_info.residx] == 0)
8744 lun->pr_key_count++;
8745 lun->pr_keys[msg->pr.pr_info.residx] =
8746 scsi_8btou64(msg->pr.pr_info.sa_res_key);
8747 lun->PRGeneration++;
8750 case CTL_PR_UNREG_KEY:
8751 lun->pr_keys[msg->pr.pr_info.residx] = 0;
8752 lun->pr_key_count--;
8754 /* XXX Need to see if the reservation has been released */
8755 /* if so do we need to generate UA? */
8756 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8757 lun->flags &= ~CTL_LUN_PR_RESERVED;
8758 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8760 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8761 || lun->res_type == SPR_TYPE_EX_AC_RO)
8762 && lun->pr_key_count) {
8764 * If the reservation is a registrants
8765 * only type we need to generate a UA
8766 * for other registered inits. The
8767 * sense code should be RESERVATIONS
8771 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8773 persis_offset] == 0)
8776 lun->pending_ua[i] |=
8781 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8782 if (lun->pr_key_count==0) {
8783 lun->flags &= ~CTL_LUN_PR_RESERVED;
8785 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8788 lun->PRGeneration++;
8791 case CTL_PR_RESERVE:
8792 lun->flags |= CTL_LUN_PR_RESERVED;
8793 lun->res_type = msg->pr.pr_info.res_type;
8794 lun->pr_res_idx = msg->pr.pr_info.residx;
8798 case CTL_PR_RELEASE:
8800 * if this isn't an exclusive access res generate UA for all
8801 * other registrants.
8803 if (lun->res_type != SPR_TYPE_EX_AC
8804 && lun->res_type != SPR_TYPE_WR_EX) {
8805 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8806 if (lun->pr_keys[i+persis_offset] != 0)
8807 lun->pending_ua[i] |=
8811 lun->flags &= ~CTL_LUN_PR_RESERVED;
8812 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8816 case CTL_PR_PREEMPT:
8817 ctl_pro_preempt_other(lun, msg);
8820 lun->flags &= ~CTL_LUN_PR_RESERVED;
8822 lun->pr_key_count = 0;
8823 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8825 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8826 if (lun->pr_keys[i] == 0)
8829 && i < CTL_MAX_INITIATORS)
8830 lun->pending_ua[i] |= CTL_UA_RES_PREEMPT;
8831 else if (persis_offset
8832 && i >= persis_offset)
8833 lun->pending_ua[i-persis_offset] |=
8835 lun->pr_keys[i] = 0;
8837 lun->PRGeneration++;
8841 mtx_unlock(&lun->lun_lock);
8845 ctl_read_write(struct ctl_scsiio *ctsio)
8847 struct ctl_lun *lun;
8848 struct ctl_lba_len_flags *lbalen;
8850 uint32_t num_blocks;
8854 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8856 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8859 retval = CTL_RETVAL_COMPLETE;
8861 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8862 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8863 if (lun->flags & CTL_LUN_PR_RESERVED && isread) {
8867 * XXX KDM need a lock here.
8869 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8870 if ((lun->res_type == SPR_TYPE_EX_AC
8871 && residx != lun->pr_res_idx)
8872 || ((lun->res_type == SPR_TYPE_EX_AC_RO
8873 || lun->res_type == SPR_TYPE_EX_AC_AR)
8874 && lun->pr_keys[residx] == 0)) {
8875 ctl_set_reservation_conflict(ctsio);
8876 ctl_done((union ctl_io *)ctsio);
8877 return (CTL_RETVAL_COMPLETE);
8881 switch (ctsio->cdb[0]) {
8884 struct scsi_rw_6 *cdb;
8886 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8888 lba = scsi_3btoul(cdb->addr);
8889 /* only 5 bits are valid in the most significant address byte */
8891 num_blocks = cdb->length;
8893 * This is correct according to SBC-2.
8895 if (num_blocks == 0)
8901 struct scsi_rw_10 *cdb;
8903 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8904 if (cdb->byte2 & SRW10_FUA)
8905 flags |= CTL_LLF_FUA;
8906 if (cdb->byte2 & SRW10_DPO)
8907 flags |= CTL_LLF_DPO;
8908 lba = scsi_4btoul(cdb->addr);
8909 num_blocks = scsi_2btoul(cdb->length);
8912 case WRITE_VERIFY_10: {
8913 struct scsi_write_verify_10 *cdb;
8915 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8916 flags |= CTL_LLF_FUA;
8917 if (cdb->byte2 & SWV_DPO)
8918 flags |= CTL_LLF_DPO;
8919 lba = scsi_4btoul(cdb->addr);
8920 num_blocks = scsi_2btoul(cdb->length);
8925 struct scsi_rw_12 *cdb;
8927 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8928 if (cdb->byte2 & SRW12_FUA)
8929 flags |= CTL_LLF_FUA;
8930 if (cdb->byte2 & SRW12_DPO)
8931 flags |= CTL_LLF_DPO;
8932 lba = scsi_4btoul(cdb->addr);
8933 num_blocks = scsi_4btoul(cdb->length);
8936 case WRITE_VERIFY_12: {
8937 struct scsi_write_verify_12 *cdb;
8939 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8940 flags |= CTL_LLF_FUA;
8941 if (cdb->byte2 & SWV_DPO)
8942 flags |= CTL_LLF_DPO;
8943 lba = scsi_4btoul(cdb->addr);
8944 num_blocks = scsi_4btoul(cdb->length);
8949 struct scsi_rw_16 *cdb;
8951 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8952 if (cdb->byte2 & SRW12_FUA)
8953 flags |= CTL_LLF_FUA;
8954 if (cdb->byte2 & SRW12_DPO)
8955 flags |= CTL_LLF_DPO;
8956 lba = scsi_8btou64(cdb->addr);
8957 num_blocks = scsi_4btoul(cdb->length);
8960 case WRITE_ATOMIC_16: {
8961 struct scsi_rw_16 *cdb;
8963 if (lun->be_lun->atomicblock == 0) {
8964 ctl_set_invalid_opcode(ctsio);
8965 ctl_done((union ctl_io *)ctsio);
8966 return (CTL_RETVAL_COMPLETE);
8969 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8970 if (cdb->byte2 & SRW12_FUA)
8971 flags |= CTL_LLF_FUA;
8972 if (cdb->byte2 & SRW12_DPO)
8973 flags |= CTL_LLF_DPO;
8974 lba = scsi_8btou64(cdb->addr);
8975 num_blocks = scsi_4btoul(cdb->length);
8976 if (num_blocks > lun->be_lun->atomicblock) {
8977 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8978 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8980 ctl_done((union ctl_io *)ctsio);
8981 return (CTL_RETVAL_COMPLETE);
8985 case WRITE_VERIFY_16: {
8986 struct scsi_write_verify_16 *cdb;
8988 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8989 flags |= CTL_LLF_FUA;
8990 if (cdb->byte2 & SWV_DPO)
8991 flags |= CTL_LLF_DPO;
8992 lba = scsi_8btou64(cdb->addr);
8993 num_blocks = scsi_4btoul(cdb->length);
8998 * We got a command we don't support. This shouldn't
8999 * happen, commands should be filtered out above us.
9001 ctl_set_invalid_opcode(ctsio);
9002 ctl_done((union ctl_io *)ctsio);
9004 return (CTL_RETVAL_COMPLETE);
9005 break; /* NOTREACHED */
9009 * The first check is to make sure we're in bounds, the second
9010 * check is to catch wrap-around problems. If the lba + num blocks
9011 * is less than the lba, then we've wrapped around and the block
9012 * range is invalid anyway.
9014 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9015 || ((lba + num_blocks) < lba)) {
9016 ctl_set_lba_out_of_range(ctsio);
9017 ctl_done((union ctl_io *)ctsio);
9018 return (CTL_RETVAL_COMPLETE);
9022 * According to SBC-3, a transfer length of 0 is not an error.
9023 * Note that this cannot happen with WRITE(6) or READ(6), since 0
9024 * translates to 256 blocks for those commands.
9026 if (num_blocks == 0) {
9027 ctl_set_success(ctsio);
9028 ctl_done((union ctl_io *)ctsio);
9029 return (CTL_RETVAL_COMPLETE);
9032 /* Set FUA and/or DPO if caches are disabled. */
9034 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9036 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
9038 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9040 flags |= CTL_LLF_FUA;
9043 lbalen = (struct ctl_lba_len_flags *)
9044 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9046 lbalen->len = num_blocks;
9047 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
9049 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9050 ctsio->kern_rel_offset = 0;
9052 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
9054 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9060 ctl_cnw_cont(union ctl_io *io)
9062 struct ctl_scsiio *ctsio;
9063 struct ctl_lun *lun;
9064 struct ctl_lba_len_flags *lbalen;
9067 ctsio = &io->scsiio;
9068 ctsio->io_hdr.status = CTL_STATUS_NONE;
9069 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
9070 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9071 lbalen = (struct ctl_lba_len_flags *)
9072 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9073 lbalen->flags &= ~CTL_LLF_COMPARE;
9074 lbalen->flags |= CTL_LLF_WRITE;
9076 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
9077 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9082 ctl_cnw(struct ctl_scsiio *ctsio)
9084 struct ctl_lun *lun;
9085 struct ctl_lba_len_flags *lbalen;
9087 uint32_t num_blocks;
9090 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9092 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
9095 retval = CTL_RETVAL_COMPLETE;
9097 switch (ctsio->cdb[0]) {
9098 case COMPARE_AND_WRITE: {
9099 struct scsi_compare_and_write *cdb;
9101 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
9102 if (cdb->byte2 & SRW10_FUA)
9103 flags |= CTL_LLF_FUA;
9104 if (cdb->byte2 & SRW10_DPO)
9105 flags |= CTL_LLF_DPO;
9106 lba = scsi_8btou64(cdb->addr);
9107 num_blocks = cdb->length;
9112 * We got a command we don't support. This shouldn't
9113 * happen, commands should be filtered out above us.
9115 ctl_set_invalid_opcode(ctsio);
9116 ctl_done((union ctl_io *)ctsio);
9118 return (CTL_RETVAL_COMPLETE);
9119 break; /* NOTREACHED */
9123 * The first check is to make sure we're in bounds, the second
9124 * check is to catch wrap-around problems. If the lba + num blocks
9125 * is less than the lba, then we've wrapped around and the block
9126 * range is invalid anyway.
9128 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9129 || ((lba + num_blocks) < lba)) {
9130 ctl_set_lba_out_of_range(ctsio);
9131 ctl_done((union ctl_io *)ctsio);
9132 return (CTL_RETVAL_COMPLETE);
9136 * According to SBC-3, a transfer length of 0 is not an error.
9138 if (num_blocks == 0) {
9139 ctl_set_success(ctsio);
9140 ctl_done((union ctl_io *)ctsio);
9141 return (CTL_RETVAL_COMPLETE);
9144 /* Set FUA if write cache is disabled. */
9145 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9147 flags |= CTL_LLF_FUA;
9149 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
9150 ctsio->kern_rel_offset = 0;
9153 * Set the IO_CONT flag, so that if this I/O gets passed to
9154 * ctl_data_submit_done(), it'll get passed back to
9155 * ctl_ctl_cnw_cont() for further processing.
9157 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
9158 ctsio->io_cont = ctl_cnw_cont;
9160 lbalen = (struct ctl_lba_len_flags *)
9161 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9163 lbalen->len = num_blocks;
9164 lbalen->flags = CTL_LLF_COMPARE | flags;
9166 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
9167 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9172 ctl_verify(struct ctl_scsiio *ctsio)
9174 struct ctl_lun *lun;
9175 struct ctl_lba_len_flags *lbalen;
9177 uint32_t num_blocks;
9181 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9183 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
9186 flags = CTL_LLF_FUA;
9187 retval = CTL_RETVAL_COMPLETE;
9189 switch (ctsio->cdb[0]) {
9191 struct scsi_verify_10 *cdb;
9193 cdb = (struct scsi_verify_10 *)ctsio->cdb;
9194 if (cdb->byte2 & SVFY_BYTCHK)
9196 if (cdb->byte2 & SVFY_DPO)
9197 flags |= CTL_LLF_DPO;
9198 lba = scsi_4btoul(cdb->addr);
9199 num_blocks = scsi_2btoul(cdb->length);
9203 struct scsi_verify_12 *cdb;
9205 cdb = (struct scsi_verify_12 *)ctsio->cdb;
9206 if (cdb->byte2 & SVFY_BYTCHK)
9208 if (cdb->byte2 & SVFY_DPO)
9209 flags |= CTL_LLF_DPO;
9210 lba = scsi_4btoul(cdb->addr);
9211 num_blocks = scsi_4btoul(cdb->length);
9215 struct scsi_rw_16 *cdb;
9217 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9218 if (cdb->byte2 & SVFY_BYTCHK)
9220 if (cdb->byte2 & SVFY_DPO)
9221 flags |= CTL_LLF_DPO;
9222 lba = scsi_8btou64(cdb->addr);
9223 num_blocks = scsi_4btoul(cdb->length);
9228 * We got a command we don't support. This shouldn't
9229 * happen, commands should be filtered out above us.
9231 ctl_set_invalid_opcode(ctsio);
9232 ctl_done((union ctl_io *)ctsio);
9233 return (CTL_RETVAL_COMPLETE);
9237 * The first check is to make sure we're in bounds, the second
9238 * check is to catch wrap-around problems. If the lba + num blocks
9239 * is less than the lba, then we've wrapped around and the block
9240 * range is invalid anyway.
9242 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9243 || ((lba + num_blocks) < lba)) {
9244 ctl_set_lba_out_of_range(ctsio);
9245 ctl_done((union ctl_io *)ctsio);
9246 return (CTL_RETVAL_COMPLETE);
9250 * According to SBC-3, a transfer length of 0 is not an error.
9252 if (num_blocks == 0) {
9253 ctl_set_success(ctsio);
9254 ctl_done((union ctl_io *)ctsio);
9255 return (CTL_RETVAL_COMPLETE);
9258 lbalen = (struct ctl_lba_len_flags *)
9259 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9261 lbalen->len = num_blocks;
9263 lbalen->flags = CTL_LLF_COMPARE | flags;
9264 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9266 lbalen->flags = CTL_LLF_VERIFY | flags;
9267 ctsio->kern_total_len = 0;
9269 ctsio->kern_rel_offset = 0;
9271 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9272 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9277 ctl_report_luns(struct ctl_scsiio *ctsio)
9279 struct scsi_report_luns *cdb;
9280 struct scsi_report_luns_data *lun_data;
9281 struct ctl_lun *lun, *request_lun;
9282 int num_luns, retval;
9283 uint32_t alloc_len, lun_datalen;
9284 int num_filled, well_known;
9285 uint32_t initidx, targ_lun_id, lun_id;
9287 retval = CTL_RETVAL_COMPLETE;
9290 cdb = (struct scsi_report_luns *)ctsio->cdb;
9292 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9294 mtx_lock(&control_softc->ctl_lock);
9295 num_luns = control_softc->num_luns;
9296 mtx_unlock(&control_softc->ctl_lock);
9298 switch (cdb->select_report) {
9299 case RPL_REPORT_DEFAULT:
9300 case RPL_REPORT_ALL:
9302 case RPL_REPORT_WELLKNOWN:
9307 ctl_set_invalid_field(ctsio,
9313 ctl_done((union ctl_io *)ctsio);
9315 break; /* NOTREACHED */
9318 alloc_len = scsi_4btoul(cdb->length);
9320 * The initiator has to allocate at least 16 bytes for this request,
9321 * so he can at least get the header and the first LUN. Otherwise
9322 * we reject the request (per SPC-3 rev 14, section 6.21).
9324 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9325 sizeof(struct scsi_report_luns_lundata))) {
9326 ctl_set_invalid_field(ctsio,
9332 ctl_done((union ctl_io *)ctsio);
9336 request_lun = (struct ctl_lun *)
9337 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9339 lun_datalen = sizeof(*lun_data) +
9340 (num_luns * sizeof(struct scsi_report_luns_lundata));
9342 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9343 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9344 ctsio->kern_sg_entries = 0;
9346 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9348 mtx_lock(&control_softc->ctl_lock);
9349 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9350 lun_id = ctl_map_lun(ctsio->io_hdr.nexus.targ_port, targ_lun_id);
9351 if (lun_id >= CTL_MAX_LUNS)
9353 lun = control_softc->ctl_luns[lun_id];
9357 if (targ_lun_id <= 0xff) {
9359 * Peripheral addressing method, bus number 0.
9361 lun_data->luns[num_filled].lundata[0] =
9362 RPL_LUNDATA_ATYP_PERIPH;
9363 lun_data->luns[num_filled].lundata[1] = targ_lun_id;
9365 } else if (targ_lun_id <= 0x3fff) {
9367 * Flat addressing method.
9369 lun_data->luns[num_filled].lundata[0] =
9370 RPL_LUNDATA_ATYP_FLAT |
9371 (targ_lun_id & RPL_LUNDATA_FLAT_LUN_MASK);
9372 #ifdef OLDCTLHEADERS
9373 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) |
9374 (targ_lun_id & SRLD_BUS_LUN_MASK);
9376 lun_data->luns[num_filled].lundata[1] =
9377 #ifdef OLDCTLHEADERS
9378 targ_lun_id >> SRLD_BUS_LUN_BITS;
9380 targ_lun_id >> RPL_LUNDATA_FLAT_LUN_BITS;
9383 printf("ctl_report_luns: bogus LUN number %jd, "
9384 "skipping\n", (intmax_t)targ_lun_id);
9387 * According to SPC-3, rev 14 section 6.21:
9389 * "The execution of a REPORT LUNS command to any valid and
9390 * installed logical unit shall clear the REPORTED LUNS DATA
9391 * HAS CHANGED unit attention condition for all logical
9392 * units of that target with respect to the requesting
9393 * initiator. A valid and installed logical unit is one
9394 * having a PERIPHERAL QUALIFIER of 000b in the standard
9395 * INQUIRY data (see 6.4.2)."
9397 * If request_lun is NULL, the LUN this report luns command
9398 * was issued to is either disabled or doesn't exist. In that
9399 * case, we shouldn't clear any pending lun change unit
9402 if (request_lun != NULL) {
9403 mtx_lock(&lun->lun_lock);
9404 lun->pending_ua[initidx] &= ~CTL_UA_LUN_CHANGE;
9405 mtx_unlock(&lun->lun_lock);
9408 mtx_unlock(&control_softc->ctl_lock);
9411 * It's quite possible that we've returned fewer LUNs than we allocated
9412 * space for. Trim it.
9414 lun_datalen = sizeof(*lun_data) +
9415 (num_filled * sizeof(struct scsi_report_luns_lundata));
9417 if (lun_datalen < alloc_len) {
9418 ctsio->residual = alloc_len - lun_datalen;
9419 ctsio->kern_data_len = lun_datalen;
9420 ctsio->kern_total_len = lun_datalen;
9422 ctsio->residual = 0;
9423 ctsio->kern_data_len = alloc_len;
9424 ctsio->kern_total_len = alloc_len;
9426 ctsio->kern_data_resid = 0;
9427 ctsio->kern_rel_offset = 0;
9428 ctsio->kern_sg_entries = 0;
9431 * We set this to the actual data length, regardless of how much
9432 * space we actually have to return results. If the user looks at
9433 * this value, he'll know whether or not he allocated enough space
9434 * and reissue the command if necessary. We don't support well
9435 * known logical units, so if the user asks for that, return none.
9437 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9440 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9443 ctsio->scsi_status = SCSI_STATUS_OK;
9445 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9446 ctsio->be_move_done = ctl_config_move_done;
9447 ctl_datamove((union ctl_io *)ctsio);
9453 ctl_request_sense(struct ctl_scsiio *ctsio)
9455 struct scsi_request_sense *cdb;
9456 struct scsi_sense_data *sense_ptr;
9457 struct ctl_lun *lun;
9460 scsi_sense_data_type sense_format;
9462 cdb = (struct scsi_request_sense *)ctsio->cdb;
9464 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9466 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9469 * Determine which sense format the user wants.
9471 if (cdb->byte2 & SRS_DESC)
9472 sense_format = SSD_TYPE_DESC;
9474 sense_format = SSD_TYPE_FIXED;
9476 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9477 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9478 ctsio->kern_sg_entries = 0;
9481 * struct scsi_sense_data, which is currently set to 256 bytes, is
9482 * larger than the largest allowed value for the length field in the
9483 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9485 ctsio->residual = 0;
9486 ctsio->kern_data_len = cdb->length;
9487 ctsio->kern_total_len = cdb->length;
9489 ctsio->kern_data_resid = 0;
9490 ctsio->kern_rel_offset = 0;
9491 ctsio->kern_sg_entries = 0;
9494 * If we don't have a LUN, we don't have any pending sense.
9500 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9502 * Check for pending sense, and then for pending unit attentions.
9503 * Pending sense gets returned first, then pending unit attentions.
9505 mtx_lock(&lun->lun_lock);
9507 if (ctl_is_set(lun->have_ca, initidx)) {
9508 scsi_sense_data_type stored_format;
9511 * Check to see which sense format was used for the stored
9514 stored_format = scsi_sense_type(&lun->pending_sense[initidx]);
9517 * If the user requested a different sense format than the
9518 * one we stored, then we need to convert it to the other
9519 * format. If we're going from descriptor to fixed format
9520 * sense data, we may lose things in translation, depending
9521 * on what options were used.
9523 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9524 * for some reason we'll just copy it out as-is.
9526 if ((stored_format == SSD_TYPE_FIXED)
9527 && (sense_format == SSD_TYPE_DESC))
9528 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9529 &lun->pending_sense[initidx],
9530 (struct scsi_sense_data_desc *)sense_ptr);
9531 else if ((stored_format == SSD_TYPE_DESC)
9532 && (sense_format == SSD_TYPE_FIXED))
9533 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9534 &lun->pending_sense[initidx],
9535 (struct scsi_sense_data_fixed *)sense_ptr);
9537 memcpy(sense_ptr, &lun->pending_sense[initidx],
9538 ctl_min(sizeof(*sense_ptr),
9539 sizeof(lun->pending_sense[initidx])));
9541 ctl_clear_mask(lun->have_ca, initidx);
9545 if (lun->pending_ua[initidx] != CTL_UA_NONE) {
9546 ctl_ua_type ua_type;
9548 ua_type = ctl_build_ua(&lun->pending_ua[initidx],
9549 sense_ptr, sense_format);
9550 if (ua_type != CTL_UA_NONE)
9553 mtx_unlock(&lun->lun_lock);
9556 * We already have a pending error, return it.
9558 if (have_error != 0) {
9560 * We report the SCSI status as OK, since the status of the
9561 * request sense command itself is OK.
9563 ctsio->scsi_status = SCSI_STATUS_OK;
9566 * We report 0 for the sense length, because we aren't doing
9567 * autosense in this case. We're reporting sense as
9570 ctsio->sense_len = 0;
9571 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9572 ctsio->be_move_done = ctl_config_move_done;
9573 ctl_datamove((union ctl_io *)ctsio);
9575 return (CTL_RETVAL_COMPLETE);
9581 * No sense information to report, so we report that everything is
9584 ctl_set_sense_data(sense_ptr,
9587 /*current_error*/ 1,
9588 /*sense_key*/ SSD_KEY_NO_SENSE,
9593 ctsio->scsi_status = SCSI_STATUS_OK;
9596 * We report 0 for the sense length, because we aren't doing
9597 * autosense in this case. We're reporting sense as parameter data.
9599 ctsio->sense_len = 0;
9600 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9601 ctsio->be_move_done = ctl_config_move_done;
9602 ctl_datamove((union ctl_io *)ctsio);
9604 return (CTL_RETVAL_COMPLETE);
9608 ctl_tur(struct ctl_scsiio *ctsio)
9610 struct ctl_lun *lun;
9612 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9614 CTL_DEBUG_PRINT(("ctl_tur\n"));
9619 ctsio->scsi_status = SCSI_STATUS_OK;
9620 ctsio->io_hdr.status = CTL_SUCCESS;
9622 ctl_done((union ctl_io *)ctsio);
9624 return (CTL_RETVAL_COMPLETE);
9629 ctl_cmddt_inquiry(struct ctl_scsiio *ctsio)
9636 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9638 struct scsi_vpd_supported_pages *pages;
9640 struct ctl_lun *lun;
9642 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9644 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9645 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9646 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9647 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9648 ctsio->kern_sg_entries = 0;
9650 if (sup_page_size < alloc_len) {
9651 ctsio->residual = alloc_len - sup_page_size;
9652 ctsio->kern_data_len = sup_page_size;
9653 ctsio->kern_total_len = sup_page_size;
9655 ctsio->residual = 0;
9656 ctsio->kern_data_len = alloc_len;
9657 ctsio->kern_total_len = alloc_len;
9659 ctsio->kern_data_resid = 0;
9660 ctsio->kern_rel_offset = 0;
9661 ctsio->kern_sg_entries = 0;
9664 * The control device is always connected. The disk device, on the
9665 * other hand, may not be online all the time. Need to change this
9666 * to figure out whether the disk device is actually online or not.
9669 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9670 lun->be_lun->lun_type;
9672 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9674 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES;
9675 /* Supported VPD pages */
9676 pages->page_list[0] = SVPD_SUPPORTED_PAGES;
9678 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER;
9679 /* Device Identification */
9680 pages->page_list[2] = SVPD_DEVICE_ID;
9681 /* Extended INQUIRY Data */
9682 pages->page_list[3] = SVPD_EXTENDED_INQUIRY_DATA;
9683 /* Mode Page Policy */
9684 pages->page_list[4] = SVPD_MODE_PAGE_POLICY;
9686 pages->page_list[5] = SVPD_SCSI_PORTS;
9687 /* Third-party Copy */
9688 pages->page_list[6] = SVPD_SCSI_TPC;
9690 pages->page_list[7] = SVPD_BLOCK_LIMITS;
9691 /* Block Device Characteristics */
9692 pages->page_list[8] = SVPD_BDC;
9693 /* Logical Block Provisioning */
9694 pages->page_list[9] = SVPD_LBP;
9696 ctsio->scsi_status = SCSI_STATUS_OK;
9698 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9699 ctsio->be_move_done = ctl_config_move_done;
9700 ctl_datamove((union ctl_io *)ctsio);
9702 return (CTL_RETVAL_COMPLETE);
9706 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9708 struct scsi_vpd_unit_serial_number *sn_ptr;
9709 struct ctl_lun *lun;
9712 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9714 data_len = 4 + CTL_SN_LEN;
9715 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9716 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9717 if (data_len < alloc_len) {
9718 ctsio->residual = alloc_len - data_len;
9719 ctsio->kern_data_len = data_len;
9720 ctsio->kern_total_len = data_len;
9722 ctsio->residual = 0;
9723 ctsio->kern_data_len = alloc_len;
9724 ctsio->kern_total_len = alloc_len;
9726 ctsio->kern_data_resid = 0;
9727 ctsio->kern_rel_offset = 0;
9728 ctsio->kern_sg_entries = 0;
9731 * The control device is always connected. The disk device, on the
9732 * other hand, may not be online all the time. Need to change this
9733 * to figure out whether the disk device is actually online or not.
9736 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9737 lun->be_lun->lun_type;
9739 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9741 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9742 sn_ptr->length = CTL_SN_LEN;
9744 * If we don't have a LUN, we just leave the serial number as
9748 strncpy((char *)sn_ptr->serial_num,
9749 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9751 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9752 ctsio->scsi_status = SCSI_STATUS_OK;
9754 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9755 ctsio->be_move_done = ctl_config_move_done;
9756 ctl_datamove((union ctl_io *)ctsio);
9758 return (CTL_RETVAL_COMPLETE);
9763 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9765 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9766 struct ctl_lun *lun;
9769 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9771 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9772 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9773 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9774 ctsio->kern_sg_entries = 0;
9776 if (data_len < alloc_len) {
9777 ctsio->residual = alloc_len - data_len;
9778 ctsio->kern_data_len = data_len;
9779 ctsio->kern_total_len = data_len;
9781 ctsio->residual = 0;
9782 ctsio->kern_data_len = alloc_len;
9783 ctsio->kern_total_len = alloc_len;
9785 ctsio->kern_data_resid = 0;
9786 ctsio->kern_rel_offset = 0;
9787 ctsio->kern_sg_entries = 0;
9790 * The control device is always connected. The disk device, on the
9791 * other hand, may not be online all the time.
9794 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9795 lun->be_lun->lun_type;
9797 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9798 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9799 eid_ptr->page_length = data_len - 4;
9800 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9801 eid_ptr->flags3 = SVPD_EID_V_SUP;
9803 ctsio->scsi_status = SCSI_STATUS_OK;
9804 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9805 ctsio->be_move_done = ctl_config_move_done;
9806 ctl_datamove((union ctl_io *)ctsio);
9808 return (CTL_RETVAL_COMPLETE);
9812 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9814 struct scsi_vpd_mode_page_policy *mpp_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_mode_page_policy) +
9821 sizeof(struct scsi_vpd_mode_page_policy_descr);
9823 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9824 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9825 ctsio->kern_sg_entries = 0;
9827 if (data_len < alloc_len) {
9828 ctsio->residual = alloc_len - data_len;
9829 ctsio->kern_data_len = data_len;
9830 ctsio->kern_total_len = data_len;
9832 ctsio->residual = 0;
9833 ctsio->kern_data_len = alloc_len;
9834 ctsio->kern_total_len = alloc_len;
9836 ctsio->kern_data_resid = 0;
9837 ctsio->kern_rel_offset = 0;
9838 ctsio->kern_sg_entries = 0;
9841 * The control device is always connected. The disk device, on the
9842 * other hand, may not be online all the time.
9845 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9846 lun->be_lun->lun_type;
9848 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9849 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9850 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9851 mpp_ptr->descr[0].page_code = 0x3f;
9852 mpp_ptr->descr[0].subpage_code = 0xff;
9853 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9855 ctsio->scsi_status = SCSI_STATUS_OK;
9856 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9857 ctsio->be_move_done = ctl_config_move_done;
9858 ctl_datamove((union ctl_io *)ctsio);
9860 return (CTL_RETVAL_COMPLETE);
9864 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9866 struct scsi_vpd_device_id *devid_ptr;
9867 struct scsi_vpd_id_descriptor *desc;
9868 struct ctl_softc *ctl_softc;
9869 struct ctl_lun *lun;
9870 struct ctl_port *port;
9874 ctl_softc = control_softc;
9876 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)];
9877 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9879 data_len = sizeof(struct scsi_vpd_device_id) +
9880 sizeof(struct scsi_vpd_id_descriptor) +
9881 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9882 sizeof(struct scsi_vpd_id_descriptor) +
9883 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9884 if (lun && lun->lun_devid)
9885 data_len += lun->lun_devid->len;
9886 if (port->port_devid)
9887 data_len += port->port_devid->len;
9888 if (port->target_devid)
9889 data_len += port->target_devid->len;
9891 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9892 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9893 ctsio->kern_sg_entries = 0;
9895 if (data_len < alloc_len) {
9896 ctsio->residual = alloc_len - data_len;
9897 ctsio->kern_data_len = data_len;
9898 ctsio->kern_total_len = data_len;
9900 ctsio->residual = 0;
9901 ctsio->kern_data_len = alloc_len;
9902 ctsio->kern_total_len = alloc_len;
9904 ctsio->kern_data_resid = 0;
9905 ctsio->kern_rel_offset = 0;
9906 ctsio->kern_sg_entries = 0;
9909 * The control device is always connected. The disk device, on the
9910 * other hand, may not be online all the time.
9913 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9914 lun->be_lun->lun_type;
9916 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9917 devid_ptr->page_code = SVPD_DEVICE_ID;
9918 scsi_ulto2b(data_len - 4, devid_ptr->length);
9920 if (port->port_type == CTL_PORT_FC)
9921 proto = SCSI_PROTO_FC << 4;
9922 else if (port->port_type == CTL_PORT_ISCSI)
9923 proto = SCSI_PROTO_ISCSI << 4;
9925 proto = SCSI_PROTO_SPI << 4;
9926 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9929 * We're using a LUN association here. i.e., this device ID is a
9930 * per-LUN identifier.
9932 if (lun && lun->lun_devid) {
9933 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9934 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9935 lun->lun_devid->len);
9939 * This is for the WWPN which is a port association.
9941 if (port->port_devid) {
9942 memcpy(desc, port->port_devid->data, port->port_devid->len);
9943 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9944 port->port_devid->len);
9948 * This is for the Relative Target Port(type 4h) identifier
9950 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9951 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9952 SVPD_ID_TYPE_RELTARG;
9954 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9955 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9956 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9959 * This is for the Target Port Group(type 5h) identifier
9961 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9962 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9963 SVPD_ID_TYPE_TPORTGRP;
9965 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1,
9966 &desc->identifier[2]);
9967 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9968 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9971 * This is for the Target identifier
9973 if (port->target_devid) {
9974 memcpy(desc, port->target_devid->data, port->target_devid->len);
9977 ctsio->scsi_status = SCSI_STATUS_OK;
9978 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9979 ctsio->be_move_done = ctl_config_move_done;
9980 ctl_datamove((union ctl_io *)ctsio);
9982 return (CTL_RETVAL_COMPLETE);
9986 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9988 struct ctl_softc *softc = control_softc;
9989 struct scsi_vpd_scsi_ports *sp;
9990 struct scsi_vpd_port_designation *pd;
9991 struct scsi_vpd_port_designation_cont *pdc;
9992 struct ctl_lun *lun;
9993 struct ctl_port *port;
9994 int data_len, num_target_ports, iid_len, id_len, g, pg, p;
9995 int num_target_port_groups, single;
9997 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9999 single = ctl_is_single;
10001 num_target_port_groups = 1;
10003 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
10004 num_target_ports = 0;
10007 mtx_lock(&softc->ctl_lock);
10008 STAILQ_FOREACH(port, &softc->port_list, links) {
10009 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
10012 ctl_map_lun_back(port->targ_port, lun->lun) >=
10015 num_target_ports++;
10016 if (port->init_devid)
10017 iid_len += port->init_devid->len;
10018 if (port->port_devid)
10019 id_len += port->port_devid->len;
10021 mtx_unlock(&softc->ctl_lock);
10023 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups *
10024 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
10025 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
10026 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10027 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
10028 ctsio->kern_sg_entries = 0;
10030 if (data_len < alloc_len) {
10031 ctsio->residual = alloc_len - data_len;
10032 ctsio->kern_data_len = data_len;
10033 ctsio->kern_total_len = data_len;
10035 ctsio->residual = 0;
10036 ctsio->kern_data_len = alloc_len;
10037 ctsio->kern_total_len = alloc_len;
10039 ctsio->kern_data_resid = 0;
10040 ctsio->kern_rel_offset = 0;
10041 ctsio->kern_sg_entries = 0;
10044 * The control device is always connected. The disk device, on the
10045 * other hand, may not be online all the time. Need to change this
10046 * to figure out whether the disk device is actually online or not.
10049 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
10050 lun->be_lun->lun_type;
10052 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10054 sp->page_code = SVPD_SCSI_PORTS;
10055 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
10057 pd = &sp->design[0];
10059 mtx_lock(&softc->ctl_lock);
10060 if (softc->flags & CTL_FLAG_MASTER_SHELF)
10064 for (g = 0; g < num_target_port_groups; g++) {
10065 STAILQ_FOREACH(port, &softc->port_list, links) {
10066 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
10069 ctl_map_lun_back(port->targ_port, lun->lun) >=
10072 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS;
10073 scsi_ulto2b(p, pd->relative_port_id);
10074 if (port->init_devid && g == pg) {
10075 iid_len = port->init_devid->len;
10076 memcpy(pd->initiator_transportid,
10077 port->init_devid->data, port->init_devid->len);
10080 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
10081 pdc = (struct scsi_vpd_port_designation_cont *)
10082 (&pd->initiator_transportid[iid_len]);
10083 if (port->port_devid && g == pg) {
10084 id_len = port->port_devid->len;
10085 memcpy(pdc->target_port_descriptors,
10086 port->port_devid->data, port->port_devid->len);
10089 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
10090 pd = (struct scsi_vpd_port_designation *)
10091 ((uint8_t *)pdc->target_port_descriptors + id_len);
10094 mtx_unlock(&softc->ctl_lock);
10096 ctsio->scsi_status = SCSI_STATUS_OK;
10097 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10098 ctsio->be_move_done = ctl_config_move_done;
10099 ctl_datamove((union ctl_io *)ctsio);
10101 return (CTL_RETVAL_COMPLETE);
10105 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
10107 struct scsi_vpd_block_limits *bl_ptr;
10108 struct ctl_lun *lun;
10111 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10113 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
10114 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
10115 ctsio->kern_sg_entries = 0;
10117 if (sizeof(*bl_ptr) < alloc_len) {
10118 ctsio->residual = alloc_len - sizeof(*bl_ptr);
10119 ctsio->kern_data_len = sizeof(*bl_ptr);
10120 ctsio->kern_total_len = sizeof(*bl_ptr);
10122 ctsio->residual = 0;
10123 ctsio->kern_data_len = alloc_len;
10124 ctsio->kern_total_len = alloc_len;
10126 ctsio->kern_data_resid = 0;
10127 ctsio->kern_rel_offset = 0;
10128 ctsio->kern_sg_entries = 0;
10131 * The control device is always connected. The disk device, on the
10132 * other hand, may not be online all the time. Need to change this
10133 * to figure out whether the disk device is actually online or not.
10136 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10137 lun->be_lun->lun_type;
10139 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10141 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
10142 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
10143 bl_ptr->max_cmp_write_len = 0xff;
10144 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
10146 bs = lun->be_lun->blocksize;
10147 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len);
10148 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10149 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
10150 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
10151 if (lun->be_lun->pblockexp != 0) {
10152 scsi_ulto4b((1 << lun->be_lun->pblockexp),
10153 bl_ptr->opt_unmap_grain);
10154 scsi_ulto4b(0x80000000 | lun->be_lun->pblockoff,
10155 bl_ptr->unmap_grain_align);
10158 scsi_ulto4b(lun->be_lun->atomicblock,
10159 bl_ptr->max_atomic_transfer_length);
10160 scsi_ulto4b(0, bl_ptr->atomic_alignment);
10161 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
10163 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
10165 ctsio->scsi_status = SCSI_STATUS_OK;
10166 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10167 ctsio->be_move_done = ctl_config_move_done;
10168 ctl_datamove((union ctl_io *)ctsio);
10170 return (CTL_RETVAL_COMPLETE);
10174 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
10176 struct scsi_vpd_block_device_characteristics *bdc_ptr;
10177 struct ctl_lun *lun;
10179 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10181 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
10182 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
10183 ctsio->kern_sg_entries = 0;
10185 if (sizeof(*bdc_ptr) < alloc_len) {
10186 ctsio->residual = alloc_len - sizeof(*bdc_ptr);
10187 ctsio->kern_data_len = sizeof(*bdc_ptr);
10188 ctsio->kern_total_len = sizeof(*bdc_ptr);
10190 ctsio->residual = 0;
10191 ctsio->kern_data_len = alloc_len;
10192 ctsio->kern_total_len = alloc_len;
10194 ctsio->kern_data_resid = 0;
10195 ctsio->kern_rel_offset = 0;
10196 ctsio->kern_sg_entries = 0;
10199 * The control device is always connected. The disk device, on the
10200 * other hand, may not be online all the time. Need to change this
10201 * to figure out whether the disk device is actually online or not.
10204 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10205 lun->be_lun->lun_type;
10207 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10208 bdc_ptr->page_code = SVPD_BDC;
10209 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
10210 scsi_ulto2b(SVPD_NON_ROTATING, bdc_ptr->medium_rotation_rate);
10211 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
10213 ctsio->scsi_status = SCSI_STATUS_OK;
10214 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10215 ctsio->be_move_done = ctl_config_move_done;
10216 ctl_datamove((union ctl_io *)ctsio);
10218 return (CTL_RETVAL_COMPLETE);
10222 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
10224 struct scsi_vpd_logical_block_prov *lbp_ptr;
10225 struct ctl_lun *lun;
10227 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10229 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
10230 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
10231 ctsio->kern_sg_entries = 0;
10233 if (sizeof(*lbp_ptr) < alloc_len) {
10234 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
10235 ctsio->kern_data_len = sizeof(*lbp_ptr);
10236 ctsio->kern_total_len = sizeof(*lbp_ptr);
10238 ctsio->residual = 0;
10239 ctsio->kern_data_len = alloc_len;
10240 ctsio->kern_total_len = alloc_len;
10242 ctsio->kern_data_resid = 0;
10243 ctsio->kern_rel_offset = 0;
10244 ctsio->kern_sg_entries = 0;
10247 * The control device is always connected. The disk device, on the
10248 * other hand, may not be online all the time. Need to change this
10249 * to figure out whether the disk device is actually online or not.
10252 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10253 lun->be_lun->lun_type;
10255 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10257 lbp_ptr->page_code = SVPD_LBP;
10258 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
10259 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10260 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
10261 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
10262 lbp_ptr->prov_type = SVPD_LBP_RESOURCE;
10265 ctsio->scsi_status = SCSI_STATUS_OK;
10266 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10267 ctsio->be_move_done = ctl_config_move_done;
10268 ctl_datamove((union ctl_io *)ctsio);
10270 return (CTL_RETVAL_COMPLETE);
10274 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
10276 struct scsi_inquiry *cdb;
10277 struct ctl_lun *lun;
10278 int alloc_len, retval;
10280 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10281 cdb = (struct scsi_inquiry *)ctsio->cdb;
10283 retval = CTL_RETVAL_COMPLETE;
10285 alloc_len = scsi_2btoul(cdb->length);
10287 switch (cdb->page_code) {
10288 case SVPD_SUPPORTED_PAGES:
10289 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
10291 case SVPD_UNIT_SERIAL_NUMBER:
10292 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
10294 case SVPD_DEVICE_ID:
10295 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
10297 case SVPD_EXTENDED_INQUIRY_DATA:
10298 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
10300 case SVPD_MODE_PAGE_POLICY:
10301 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
10303 case SVPD_SCSI_PORTS:
10304 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10306 case SVPD_SCSI_TPC:
10307 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10309 case SVPD_BLOCK_LIMITS:
10310 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10313 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10316 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10319 ctl_set_invalid_field(ctsio,
10325 ctl_done((union ctl_io *)ctsio);
10326 retval = CTL_RETVAL_COMPLETE;
10334 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10336 struct scsi_inquiry_data *inq_ptr;
10337 struct scsi_inquiry *cdb;
10338 struct ctl_softc *ctl_softc;
10339 struct ctl_lun *lun;
10341 uint32_t alloc_len, data_len;
10342 ctl_port_type port_type;
10344 ctl_softc = control_softc;
10347 * Figure out whether we're talking to a Fibre Channel port or not.
10348 * We treat the ioctl front end, and any SCSI adapters, as packetized
10351 port_type = ctl_softc->ctl_ports[
10352 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type;
10353 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10354 port_type = CTL_PORT_SCSI;
10356 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10357 cdb = (struct scsi_inquiry *)ctsio->cdb;
10358 alloc_len = scsi_2btoul(cdb->length);
10361 * We malloc the full inquiry data size here and fill it
10362 * in. If the user only asks for less, we'll give him
10365 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10366 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10367 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10368 ctsio->kern_sg_entries = 0;
10369 ctsio->kern_data_resid = 0;
10370 ctsio->kern_rel_offset = 0;
10372 if (data_len < alloc_len) {
10373 ctsio->residual = alloc_len - data_len;
10374 ctsio->kern_data_len = data_len;
10375 ctsio->kern_total_len = data_len;
10377 ctsio->residual = 0;
10378 ctsio->kern_data_len = alloc_len;
10379 ctsio->kern_total_len = alloc_len;
10383 * If we have a LUN configured, report it as connected. Otherwise,
10384 * report that it is offline or no device is supported, depending
10385 * on the value of inquiry_pq_no_lun.
10387 * According to the spec (SPC-4 r34), the peripheral qualifier
10388 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario:
10390 * "A peripheral device having the specified peripheral device type
10391 * is not connected to this logical unit. However, the device
10392 * server is capable of supporting the specified peripheral device
10393 * type on this logical unit."
10395 * According to the same spec, the peripheral qualifier
10396 * SID_QUAL_BAD_LU (011b) is used in this scenario:
10398 * "The device server is not capable of supporting a peripheral
10399 * device on this logical unit. For this peripheral qualifier the
10400 * peripheral device type shall be set to 1Fh. All other peripheral
10401 * device type values are reserved for this peripheral qualifier."
10403 * Given the text, it would seem that we probably want to report that
10404 * the LUN is offline here. There is no LUN connected, but we can
10405 * support a LUN at the given LUN number.
10407 * In the real world, though, it sounds like things are a little
10410 * - Linux, when presented with a LUN with the offline peripheral
10411 * qualifier, will create an sg driver instance for it. So when
10412 * you attach it to CTL, you wind up with a ton of sg driver
10413 * instances. (One for every LUN that Linux bothered to probe.)
10414 * Linux does this despite the fact that it issues a REPORT LUNs
10415 * to LUN 0 to get the inventory of supported LUNs.
10417 * - There is other anecdotal evidence (from Emulex folks) about
10418 * arrays that use the offline peripheral qualifier for LUNs that
10419 * are on the "passive" path in an active/passive array.
10421 * So the solution is provide a hopefully reasonable default
10422 * (return bad/no LUN) and allow the user to change the behavior
10423 * with a tunable/sysctl variable.
10426 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10427 lun->be_lun->lun_type;
10428 else if (ctl_softc->inquiry_pq_no_lun == 0)
10429 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10431 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10433 /* RMB in byte 2 is 0 */
10434 inq_ptr->version = SCSI_REV_SPC4;
10437 * According to SAM-3, even if a device only supports a single
10438 * level of LUN addressing, it should still set the HISUP bit:
10440 * 4.9.1 Logical unit numbers overview
10442 * All logical unit number formats described in this standard are
10443 * hierarchical in structure even when only a single level in that
10444 * hierarchy is used. The HISUP bit shall be set to one in the
10445 * standard INQUIRY data (see SPC-2) when any logical unit number
10446 * format described in this standard is used. Non-hierarchical
10447 * formats are outside the scope of this standard.
10449 * Therefore we set the HiSup bit here.
10451 * The reponse format is 2, per SPC-3.
10453 inq_ptr->response_format = SID_HiSup | 2;
10455 inq_ptr->additional_length = data_len -
10456 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10457 CTL_DEBUG_PRINT(("additional_length = %d\n",
10458 inq_ptr->additional_length));
10460 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10461 /* 16 bit addressing */
10462 if (port_type == CTL_PORT_SCSI)
10463 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10464 /* XXX set the SID_MultiP bit here if we're actually going to
10465 respond on multiple ports */
10466 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10468 /* 16 bit data bus, synchronous transfers */
10469 if (port_type == CTL_PORT_SCSI)
10470 inq_ptr->flags = SID_WBus16 | SID_Sync;
10472 * XXX KDM do we want to support tagged queueing on the control
10476 || (lun->be_lun->lun_type != T_PROCESSOR))
10477 inq_ptr->flags |= SID_CmdQue;
10479 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10480 * We have 8 bytes for the vendor name, and 16 bytes for the device
10481 * name and 4 bytes for the revision.
10483 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10484 "vendor")) == NULL) {
10485 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10487 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10488 strncpy(inq_ptr->vendor, val,
10489 min(sizeof(inq_ptr->vendor), strlen(val)));
10492 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10493 sizeof(inq_ptr->product));
10494 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10495 switch (lun->be_lun->lun_type) {
10497 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10498 sizeof(inq_ptr->product));
10501 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10502 sizeof(inq_ptr->product));
10505 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10506 sizeof(inq_ptr->product));
10510 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10511 strncpy(inq_ptr->product, val,
10512 min(sizeof(inq_ptr->product), strlen(val)));
10516 * XXX make this a macro somewhere so it automatically gets
10517 * incremented when we make changes.
10519 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10520 "revision")) == NULL) {
10521 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10523 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10524 strncpy(inq_ptr->revision, val,
10525 min(sizeof(inq_ptr->revision), strlen(val)));
10529 * For parallel SCSI, we support double transition and single
10530 * transition clocking. We also support QAS (Quick Arbitration
10531 * and Selection) and Information Unit transfers on both the
10532 * control and array devices.
10534 if (port_type == CTL_PORT_SCSI)
10535 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10538 /* SAM-5 (no version claimed) */
10539 scsi_ulto2b(0x00A0, inq_ptr->version1);
10540 /* SPC-4 (no version claimed) */
10541 scsi_ulto2b(0x0460, inq_ptr->version2);
10542 if (port_type == CTL_PORT_FC) {
10543 /* FCP-2 ANSI INCITS.350:2003 */
10544 scsi_ulto2b(0x0917, inq_ptr->version3);
10545 } else if (port_type == CTL_PORT_SCSI) {
10546 /* SPI-4 ANSI INCITS.362:200x */
10547 scsi_ulto2b(0x0B56, inq_ptr->version3);
10548 } else if (port_type == CTL_PORT_ISCSI) {
10549 /* iSCSI (no version claimed) */
10550 scsi_ulto2b(0x0960, inq_ptr->version3);
10551 } else if (port_type == CTL_PORT_SAS) {
10552 /* SAS (no version claimed) */
10553 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10557 /* SBC-4 (no version claimed) */
10558 scsi_ulto2b(0x0600, inq_ptr->version4);
10560 switch (lun->be_lun->lun_type) {
10562 /* SBC-4 (no version claimed) */
10563 scsi_ulto2b(0x0600, inq_ptr->version4);
10571 ctsio->scsi_status = SCSI_STATUS_OK;
10572 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10573 ctsio->be_move_done = ctl_config_move_done;
10574 ctl_datamove((union ctl_io *)ctsio);
10575 return (CTL_RETVAL_COMPLETE);
10579 ctl_inquiry(struct ctl_scsiio *ctsio)
10581 struct scsi_inquiry *cdb;
10584 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10586 cdb = (struct scsi_inquiry *)ctsio->cdb;
10587 if (cdb->byte2 & SI_EVPD)
10588 retval = ctl_inquiry_evpd(ctsio);
10589 else if (cdb->page_code == 0)
10590 retval = ctl_inquiry_std(ctsio);
10592 ctl_set_invalid_field(ctsio,
10598 ctl_done((union ctl_io *)ctsio);
10599 return (CTL_RETVAL_COMPLETE);
10606 * For known CDB types, parse the LBA and length.
10609 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10611 if (io->io_hdr.io_type != CTL_IO_SCSI)
10614 switch (io->scsiio.cdb[0]) {
10615 case COMPARE_AND_WRITE: {
10616 struct scsi_compare_and_write *cdb;
10618 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10620 *lba = scsi_8btou64(cdb->addr);
10621 *len = cdb->length;
10626 struct scsi_rw_6 *cdb;
10628 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10630 *lba = scsi_3btoul(cdb->addr);
10631 /* only 5 bits are valid in the most significant address byte */
10633 *len = cdb->length;
10638 struct scsi_rw_10 *cdb;
10640 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10642 *lba = scsi_4btoul(cdb->addr);
10643 *len = scsi_2btoul(cdb->length);
10646 case WRITE_VERIFY_10: {
10647 struct scsi_write_verify_10 *cdb;
10649 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10651 *lba = scsi_4btoul(cdb->addr);
10652 *len = scsi_2btoul(cdb->length);
10657 struct scsi_rw_12 *cdb;
10659 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10661 *lba = scsi_4btoul(cdb->addr);
10662 *len = scsi_4btoul(cdb->length);
10665 case WRITE_VERIFY_12: {
10666 struct scsi_write_verify_12 *cdb;
10668 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10670 *lba = scsi_4btoul(cdb->addr);
10671 *len = scsi_4btoul(cdb->length);
10676 case WRITE_ATOMIC_16: {
10677 struct scsi_rw_16 *cdb;
10679 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10681 *lba = scsi_8btou64(cdb->addr);
10682 *len = scsi_4btoul(cdb->length);
10685 case WRITE_VERIFY_16: {
10686 struct scsi_write_verify_16 *cdb;
10688 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10690 *lba = scsi_8btou64(cdb->addr);
10691 *len = scsi_4btoul(cdb->length);
10694 case WRITE_SAME_10: {
10695 struct scsi_write_same_10 *cdb;
10697 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10699 *lba = scsi_4btoul(cdb->addr);
10700 *len = scsi_2btoul(cdb->length);
10703 case WRITE_SAME_16: {
10704 struct scsi_write_same_16 *cdb;
10706 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10708 *lba = scsi_8btou64(cdb->addr);
10709 *len = scsi_4btoul(cdb->length);
10713 struct scsi_verify_10 *cdb;
10715 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10717 *lba = scsi_4btoul(cdb->addr);
10718 *len = scsi_2btoul(cdb->length);
10722 struct scsi_verify_12 *cdb;
10724 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10726 *lba = scsi_4btoul(cdb->addr);
10727 *len = scsi_4btoul(cdb->length);
10731 struct scsi_verify_16 *cdb;
10733 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10735 *lba = scsi_8btou64(cdb->addr);
10736 *len = scsi_4btoul(cdb->length);
10746 break; /* NOTREACHED */
10753 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2)
10755 uint64_t endlba1, endlba2;
10757 endlba1 = lba1 + len1 - 1;
10758 endlba2 = lba2 + len2 - 1;
10760 if ((endlba1 < lba2)
10761 || (endlba2 < lba1))
10762 return (CTL_ACTION_PASS);
10764 return (CTL_ACTION_BLOCK);
10768 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10770 struct ctl_ptr_len_flags *ptrlen;
10771 struct scsi_unmap_desc *buf, *end, *range;
10775 /* If not UNMAP -- go other way. */
10776 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10777 io->scsiio.cdb[0] != UNMAP)
10778 return (CTL_ACTION_ERROR);
10780 /* If UNMAP without data -- block and wait for data. */
10781 ptrlen = (struct ctl_ptr_len_flags *)
10782 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10783 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10784 ptrlen->ptr == NULL)
10785 return (CTL_ACTION_BLOCK);
10787 /* UNMAP with data -- check for collision. */
10788 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10789 end = buf + ptrlen->len / sizeof(*buf);
10790 for (range = buf; range < end; range++) {
10791 lba = scsi_8btou64(range->lba);
10792 len = scsi_4btoul(range->length);
10793 if ((lba < lba2 + len2) && (lba + len > lba2))
10794 return (CTL_ACTION_BLOCK);
10796 return (CTL_ACTION_PASS);
10800 ctl_extent_check(union ctl_io *io1, union ctl_io *io2)
10802 uint64_t lba1, lba2;
10803 uint64_t len1, len2;
10806 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10807 return (CTL_ACTION_ERROR);
10809 retval = ctl_extent_check_unmap(io2, lba1, len1);
10810 if (retval != CTL_ACTION_ERROR)
10813 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10814 return (CTL_ACTION_ERROR);
10816 return (ctl_extent_check_lba(lba1, len1, lba2, len2));
10820 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10821 union ctl_io *ooa_io)
10823 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10824 ctl_serialize_action *serialize_row;
10827 * The initiator attempted multiple untagged commands at the same
10828 * time. Can't do that.
10830 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10831 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10832 && ((pending_io->io_hdr.nexus.targ_port ==
10833 ooa_io->io_hdr.nexus.targ_port)
10834 && (pending_io->io_hdr.nexus.initid.id ==
10835 ooa_io->io_hdr.nexus.initid.id))
10836 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10837 return (CTL_ACTION_OVERLAP);
10840 * The initiator attempted to send multiple tagged commands with
10841 * the same ID. (It's fine if different initiators have the same
10844 * Even if all of those conditions are true, we don't kill the I/O
10845 * if the command ahead of us has been aborted. We won't end up
10846 * sending it to the FETD, and it's perfectly legal to resend a
10847 * command with the same tag number as long as the previous
10848 * instance of this tag number has been aborted somehow.
10850 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10851 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10852 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10853 && ((pending_io->io_hdr.nexus.targ_port ==
10854 ooa_io->io_hdr.nexus.targ_port)
10855 && (pending_io->io_hdr.nexus.initid.id ==
10856 ooa_io->io_hdr.nexus.initid.id))
10857 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10858 return (CTL_ACTION_OVERLAP_TAG);
10861 * If we get a head of queue tag, SAM-3 says that we should
10862 * immediately execute it.
10864 * What happens if this command would normally block for some other
10865 * reason? e.g. a request sense with a head of queue tag
10866 * immediately after a write. Normally that would block, but this
10867 * will result in its getting executed immediately...
10869 * We currently return "pass" instead of "skip", so we'll end up
10870 * going through the rest of the queue to check for overlapped tags.
10872 * XXX KDM check for other types of blockage first??
10874 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10875 return (CTL_ACTION_PASS);
10878 * Ordered tags have to block until all items ahead of them
10879 * have completed. If we get called with an ordered tag, we always
10880 * block, if something else is ahead of us in the queue.
10882 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10883 return (CTL_ACTION_BLOCK);
10886 * Simple tags get blocked until all head of queue and ordered tags
10887 * ahead of them have completed. I'm lumping untagged commands in
10888 * with simple tags here. XXX KDM is that the right thing to do?
10890 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10891 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10892 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10893 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10894 return (CTL_ACTION_BLOCK);
10896 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
10897 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
10899 serialize_row = ctl_serialize_table[ooa_entry->seridx];
10901 switch (serialize_row[pending_entry->seridx]) {
10902 case CTL_SER_BLOCK:
10903 return (CTL_ACTION_BLOCK);
10904 case CTL_SER_EXTENT:
10905 return (ctl_extent_check(pending_io, ooa_io));
10906 case CTL_SER_EXTENTOPT:
10907 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
10908 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
10909 return (ctl_extent_check(pending_io, ooa_io));
10912 return (CTL_ACTION_PASS);
10913 case CTL_SER_BLOCKOPT:
10914 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
10915 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
10916 return (CTL_ACTION_BLOCK);
10917 return (CTL_ACTION_PASS);
10919 return (CTL_ACTION_SKIP);
10921 panic("invalid serialization value %d",
10922 serialize_row[pending_entry->seridx]);
10925 return (CTL_ACTION_ERROR);
10929 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
10931 * - pending_io is generally either incoming, or on the blocked queue
10932 * - starting I/O is the I/O we want to start the check with.
10935 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
10936 union ctl_io *starting_io)
10938 union ctl_io *ooa_io;
10941 mtx_assert(&lun->lun_lock, MA_OWNED);
10944 * Run back along the OOA queue, starting with the current
10945 * blocked I/O and going through every I/O before it on the
10946 * queue. If starting_io is NULL, we'll just end up returning
10949 for (ooa_io = starting_io; ooa_io != NULL;
10950 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
10954 * This routine just checks to see whether
10955 * cur_blocked is blocked by ooa_io, which is ahead
10956 * of it in the queue. It doesn't queue/dequeue
10959 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
10961 case CTL_ACTION_BLOCK:
10962 case CTL_ACTION_OVERLAP:
10963 case CTL_ACTION_OVERLAP_TAG:
10964 case CTL_ACTION_SKIP:
10965 case CTL_ACTION_ERROR:
10967 break; /* NOTREACHED */
10968 case CTL_ACTION_PASS:
10971 panic("invalid action %d", action);
10972 break; /* NOTREACHED */
10976 return (CTL_ACTION_PASS);
10981 * - An I/O has just completed, and has been removed from the per-LUN OOA
10982 * queue, so some items on the blocked queue may now be unblocked.
10985 ctl_check_blocked(struct ctl_lun *lun)
10987 union ctl_io *cur_blocked, *next_blocked;
10989 mtx_assert(&lun->lun_lock, MA_OWNED);
10992 * Run forward from the head of the blocked queue, checking each
10993 * entry against the I/Os prior to it on the OOA queue to see if
10994 * there is still any blockage.
10996 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
10997 * with our removing a variable on it while it is traversing the
11000 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
11001 cur_blocked != NULL; cur_blocked = next_blocked) {
11002 union ctl_io *prev_ooa;
11005 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
11008 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
11009 ctl_ooaq, ooa_links);
11012 * If cur_blocked happens to be the first item in the OOA
11013 * queue now, prev_ooa will be NULL, and the action
11014 * returned will just be CTL_ACTION_PASS.
11016 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
11019 case CTL_ACTION_BLOCK:
11020 /* Nothing to do here, still blocked */
11022 case CTL_ACTION_OVERLAP:
11023 case CTL_ACTION_OVERLAP_TAG:
11025 * This shouldn't happen! In theory we've already
11026 * checked this command for overlap...
11029 case CTL_ACTION_PASS:
11030 case CTL_ACTION_SKIP: {
11031 struct ctl_softc *softc;
11032 const struct ctl_cmd_entry *entry;
11037 * The skip case shouldn't happen, this transaction
11038 * should have never made it onto the blocked queue.
11041 * This I/O is no longer blocked, we can remove it
11042 * from the blocked queue. Since this is a TAILQ
11043 * (doubly linked list), we can do O(1) removals
11044 * from any place on the list.
11046 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
11048 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11050 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){
11052 * Need to send IO back to original side to
11055 union ctl_ha_msg msg_info;
11057 msg_info.hdr.original_sc =
11058 cur_blocked->io_hdr.original_sc;
11059 msg_info.hdr.serializing_sc = cur_blocked;
11060 msg_info.hdr.msg_type = CTL_MSG_R2R;
11061 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11062 &msg_info, sizeof(msg_info), 0)) >
11063 CTL_HA_STATUS_SUCCESS) {
11064 printf("CTL:Check Blocked error from "
11065 "ctl_ha_msg_send %d\n",
11070 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
11071 softc = control_softc;
11073 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus);
11076 * Check this I/O for LUN state changes that may
11077 * have happened while this command was blocked.
11078 * The LUN state may have been changed by a command
11079 * ahead of us in the queue, so we need to re-check
11080 * for any states that can be caused by SCSI
11083 if (ctl_scsiio_lun_check(softc, lun, entry,
11084 &cur_blocked->scsiio) == 0) {
11085 cur_blocked->io_hdr.flags |=
11086 CTL_FLAG_IS_WAS_ON_RTR;
11087 ctl_enqueue_rtr(cur_blocked);
11089 ctl_done(cur_blocked);
11094 * This probably shouldn't happen -- we shouldn't
11095 * get CTL_ACTION_ERROR, or anything else.
11101 return (CTL_RETVAL_COMPLETE);
11105 * This routine (with one exception) checks LUN flags that can be set by
11106 * commands ahead of us in the OOA queue. These flags have to be checked
11107 * when a command initially comes in, and when we pull a command off the
11108 * blocked queue and are preparing to execute it. The reason we have to
11109 * check these flags for commands on the blocked queue is that the LUN
11110 * state may have been changed by a command ahead of us while we're on the
11113 * Ordering is somewhat important with these checks, so please pay
11114 * careful attention to the placement of any new checks.
11117 ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
11118 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11125 mtx_assert(&lun->lun_lock, MA_OWNED);
11128 * If this shelf is a secondary shelf controller, we have to reject
11129 * any media access commands.
11132 /* No longer needed for HA */
11133 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0)
11134 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) {
11135 ctl_set_lun_standby(ctsio);
11141 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11142 if (lun->flags & CTL_LUN_READONLY) {
11143 ctl_set_sense(ctsio, /*current_error*/ 1,
11144 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11145 /*asc*/ 0x27, /*ascq*/ 0x01, SSD_ELEM_NONE);
11149 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT]
11150 .eca_and_aen & SCP_SWP) != 0) {
11151 ctl_set_sense(ctsio, /*current_error*/ 1,
11152 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11153 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11160 * Check for a reservation conflict. If this command isn't allowed
11161 * even on reserved LUNs, and if this initiator isn't the one who
11162 * reserved us, reject the command with a reservation conflict.
11164 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
11165 if ((lun->flags & CTL_LUN_RESERVED)
11166 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11167 if (lun->res_idx != residx) {
11168 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
11169 ctsio->io_hdr.status = CTL_SCSI_ERROR;
11175 if ((lun->flags & CTL_LUN_PR_RESERVED)
11176 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) {
11178 * if we aren't registered or it's a res holder type
11179 * reservation and this isn't the res holder then set a
11181 * NOTE: Commands which might be allowed on write exclusive
11182 * type reservations are checked in the particular command
11183 * for a conflict. Read and SSU are the only ones.
11185 if (lun->pr_keys[residx] == 0
11186 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
11187 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
11188 ctsio->io_hdr.status = CTL_SCSI_ERROR;
11195 if ((lun->flags & CTL_LUN_OFFLINE)
11196 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) {
11197 ctl_set_lun_not_ready(ctsio);
11203 * If the LUN is stopped, see if this particular command is allowed
11204 * for a stopped lun. Otherwise, reject it with 0x04,0x02.
11206 if ((lun->flags & CTL_LUN_STOPPED)
11207 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
11208 /* "Logical unit not ready, initializing cmd. required" */
11209 ctl_set_lun_stopped(ctsio);
11214 if ((lun->flags & CTL_LUN_INOPERABLE)
11215 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
11216 /* "Medium format corrupted" */
11217 ctl_set_medium_format_corrupted(ctsio);
11228 ctl_failover_io(union ctl_io *io, int have_lock)
11230 ctl_set_busy(&io->scsiio);
11237 struct ctl_lun *lun;
11238 struct ctl_softc *ctl_softc;
11239 union ctl_io *next_io, *pending_io;
11244 ctl_softc = control_softc;
11246 mtx_lock(&ctl_softc->ctl_lock);
11248 * Remove any cmds from the other SC from the rtr queue. These
11249 * will obviously only be for LUNs for which we're the primary.
11250 * We can't send status or get/send data for these commands.
11251 * Since they haven't been executed yet, we can just remove them.
11252 * We'll either abort them or delete them below, depending on
11253 * which HA mode we're in.
11256 mtx_lock(&ctl_softc->queue_lock);
11257 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue);
11258 io != NULL; io = next_io) {
11259 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
11260 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
11261 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr,
11262 ctl_io_hdr, links);
11264 mtx_unlock(&ctl_softc->queue_lock);
11267 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) {
11268 lun = ctl_softc->ctl_luns[lun_idx];
11273 * Processor LUNs are primary on both sides.
11274 * XXX will this always be true?
11276 if (lun->be_lun->lun_type == T_PROCESSOR)
11279 if ((lun->flags & CTL_LUN_PRIMARY_SC)
11280 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
11281 printf("FAILOVER: primary lun %d\n", lun_idx);
11283 * Remove all commands from the other SC. First from the
11284 * blocked queue then from the ooa queue. Once we have
11285 * removed them. Call ctl_check_blocked to see if there
11286 * is anything that can run.
11288 for (io = (union ctl_io *)TAILQ_FIRST(
11289 &lun->blocked_queue); io != NULL; io = next_io) {
11291 next_io = (union ctl_io *)TAILQ_NEXT(
11292 &io->io_hdr, blocked_links);
11294 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
11295 TAILQ_REMOVE(&lun->blocked_queue,
11296 &io->io_hdr,blocked_links);
11297 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11298 TAILQ_REMOVE(&lun->ooa_queue,
11299 &io->io_hdr, ooa_links);
11305 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
11306 io != NULL; io = next_io) {
11308 next_io = (union ctl_io *)TAILQ_NEXT(
11309 &io->io_hdr, ooa_links);
11311 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
11313 TAILQ_REMOVE(&lun->ooa_queue,
11320 ctl_check_blocked(lun);
11321 } else if ((lun->flags & CTL_LUN_PRIMARY_SC)
11322 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
11324 printf("FAILOVER: primary lun %d\n", lun_idx);
11326 * Abort all commands from the other SC. We can't
11327 * send status back for them now. These should get
11328 * cleaned up when they are completed or come out
11329 * for a datamove operation.
11331 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
11332 io != NULL; io = next_io) {
11333 next_io = (union ctl_io *)TAILQ_NEXT(
11334 &io->io_hdr, ooa_links);
11336 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
11337 io->io_hdr.flags |= CTL_FLAG_ABORT;
11339 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
11340 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
11342 printf("FAILOVER: secondary lun %d\n", lun_idx);
11344 lun->flags |= CTL_LUN_PRIMARY_SC;
11347 * We send all I/O that was sent to this controller
11348 * and redirected to the other side back with
11349 * busy status, and have the initiator retry it.
11350 * Figuring out how much data has been transferred,
11351 * etc. and picking up where we left off would be
11354 * XXX KDM need to remove I/O from the blocked
11357 for (pending_io = (union ctl_io *)TAILQ_FIRST(
11358 &lun->ooa_queue); pending_io != NULL;
11359 pending_io = next_io) {
11361 next_io = (union ctl_io *)TAILQ_NEXT(
11362 &pending_io->io_hdr, ooa_links);
11364 pending_io->io_hdr.flags &=
11365 ~CTL_FLAG_SENT_2OTHER_SC;
11367 if (pending_io->io_hdr.flags &
11368 CTL_FLAG_IO_ACTIVE) {
11369 pending_io->io_hdr.flags |=
11372 ctl_set_busy(&pending_io->scsiio);
11373 ctl_done(pending_io);
11378 * Build Unit Attention
11380 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11381 lun->pending_ua[i] |=
11382 CTL_UA_ASYM_ACC_CHANGE;
11384 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
11385 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
11386 printf("FAILOVER: secondary lun %d\n", lun_idx);
11388 * if the first io on the OOA is not on the RtR queue
11391 lun->flags |= CTL_LUN_PRIMARY_SC;
11393 pending_io = (union ctl_io *)TAILQ_FIRST(
11395 if (pending_io==NULL) {
11396 printf("Nothing on OOA queue\n");
11400 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11401 if ((pending_io->io_hdr.flags &
11402 CTL_FLAG_IS_WAS_ON_RTR) == 0) {
11403 pending_io->io_hdr.flags |=
11404 CTL_FLAG_IS_WAS_ON_RTR;
11405 ctl_enqueue_rtr(pending_io);
11410 printf("Tag 0x%04x is running\n",
11411 pending_io->scsiio.tag_num);
11415 next_io = (union ctl_io *)TAILQ_NEXT(
11416 &pending_io->io_hdr, ooa_links);
11417 for (pending_io=next_io; pending_io != NULL;
11418 pending_io = next_io) {
11419 pending_io->io_hdr.flags &=
11420 ~CTL_FLAG_SENT_2OTHER_SC;
11421 next_io = (union ctl_io *)TAILQ_NEXT(
11422 &pending_io->io_hdr, ooa_links);
11423 if (pending_io->io_hdr.flags &
11424 CTL_FLAG_IS_WAS_ON_RTR) {
11426 printf("Tag 0x%04x is running\n",
11427 pending_io->scsiio.tag_num);
11432 switch (ctl_check_ooa(lun, pending_io,
11433 (union ctl_io *)TAILQ_PREV(
11434 &pending_io->io_hdr, ctl_ooaq,
11437 case CTL_ACTION_BLOCK:
11438 TAILQ_INSERT_TAIL(&lun->blocked_queue,
11439 &pending_io->io_hdr,
11441 pending_io->io_hdr.flags |=
11444 case CTL_ACTION_PASS:
11445 case CTL_ACTION_SKIP:
11446 pending_io->io_hdr.flags |=
11447 CTL_FLAG_IS_WAS_ON_RTR;
11448 ctl_enqueue_rtr(pending_io);
11450 case CTL_ACTION_OVERLAP:
11451 ctl_set_overlapped_cmd(
11452 (struct ctl_scsiio *)pending_io);
11453 ctl_done(pending_io);
11455 case CTL_ACTION_OVERLAP_TAG:
11456 ctl_set_overlapped_tag(
11457 (struct ctl_scsiio *)pending_io,
11458 pending_io->scsiio.tag_num & 0xff);
11459 ctl_done(pending_io);
11461 case CTL_ACTION_ERROR:
11463 ctl_set_internal_failure(
11464 (struct ctl_scsiio *)pending_io,
11467 ctl_done(pending_io);
11473 * Build Unit Attention
11475 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11476 lun->pending_ua[i] |=
11477 CTL_UA_ASYM_ACC_CHANGE;
11480 panic("Unhandled HA mode failover, LUN flags = %#x, "
11481 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode);
11485 mtx_unlock(&ctl_softc->ctl_lock);
11489 ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio)
11491 struct ctl_lun *lun;
11492 const struct ctl_cmd_entry *entry;
11493 uint32_t initidx, targ_lun;
11500 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11501 if ((targ_lun < CTL_MAX_LUNS)
11502 && (ctl_softc->ctl_luns[targ_lun] != NULL)) {
11503 lun = ctl_softc->ctl_luns[targ_lun];
11505 * If the LUN is invalid, pretend that it doesn't exist.
11506 * It will go away as soon as all pending I/O has been
11509 if (lun->flags & CTL_LUN_DISABLED) {
11512 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
11513 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
11515 if (lun->be_lun->lun_type == T_PROCESSOR) {
11516 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV;
11520 * Every I/O goes into the OOA queue for a
11521 * particular LUN, and stays there until completion.
11523 mtx_lock(&lun->lun_lock);
11524 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr,
11528 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11529 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11532 /* Get command entry and return error if it is unsuppotyed. */
11533 entry = ctl_validate_command(ctsio);
11534 if (entry == NULL) {
11536 mtx_unlock(&lun->lun_lock);
11540 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11541 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11544 * Check to see whether we can send this command to LUNs that don't
11545 * exist. This should pretty much only be the case for inquiry
11546 * and request sense. Further checks, below, really require having
11547 * a LUN, so we can't really check the command anymore. Just put
11548 * it on the rtr queue.
11551 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) {
11552 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11553 ctl_enqueue_rtr((union ctl_io *)ctsio);
11557 ctl_set_unsupported_lun(ctsio);
11558 ctl_done((union ctl_io *)ctsio);
11559 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11563 * Make sure we support this particular command on this LUN.
11564 * e.g., we don't support writes to the control LUN.
11566 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11567 mtx_unlock(&lun->lun_lock);
11568 ctl_set_invalid_opcode(ctsio);
11569 ctl_done((union ctl_io *)ctsio);
11574 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11578 * If we've got a request sense, it'll clear the contingent
11579 * allegiance condition. Otherwise, if we have a CA condition for
11580 * this initiator, clear it, because it sent down a command other
11581 * than request sense.
11583 if ((ctsio->cdb[0] != REQUEST_SENSE)
11584 && (ctl_is_set(lun->have_ca, initidx)))
11585 ctl_clear_mask(lun->have_ca, initidx);
11589 * If the command has this flag set, it handles its own unit
11590 * attention reporting, we shouldn't do anything. Otherwise we
11591 * check for any pending unit attentions, and send them back to the
11592 * initiator. We only do this when a command initially comes in,
11593 * not when we pull it off the blocked queue.
11595 * According to SAM-3, section 5.3.2, the order that things get
11596 * presented back to the host is basically unit attentions caused
11597 * by some sort of reset event, busy status, reservation conflicts
11598 * or task set full, and finally any other status.
11600 * One issue here is that some of the unit attentions we report
11601 * don't fall into the "reset" category (e.g. "reported luns data
11602 * has changed"). So reporting it here, before the reservation
11603 * check, may be technically wrong. I guess the only thing to do
11604 * would be to check for and report the reset events here, and then
11605 * check for the other unit attention types after we check for a
11606 * reservation conflict.
11608 * XXX KDM need to fix this
11610 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11611 ctl_ua_type ua_type;
11613 if (lun->pending_ua[initidx] != CTL_UA_NONE) {
11614 scsi_sense_data_type sense_format;
11617 sense_format = (lun->flags &
11618 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC :
11621 sense_format = SSD_TYPE_FIXED;
11623 ua_type = ctl_build_ua(&lun->pending_ua[initidx],
11624 &ctsio->sense_data, sense_format);
11625 if (ua_type != CTL_UA_NONE) {
11626 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11627 ctsio->io_hdr.status = CTL_SCSI_ERROR |
11629 ctsio->sense_len = SSD_FULL_SIZE;
11630 mtx_unlock(&lun->lun_lock);
11631 ctl_done((union ctl_io *)ctsio);
11638 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) {
11639 mtx_unlock(&lun->lun_lock);
11640 ctl_done((union ctl_io *)ctsio);
11645 * XXX CHD this is where we want to send IO to other side if
11646 * this LUN is secondary on this SC. We will need to make a copy
11647 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11648 * the copy we send as FROM_OTHER.
11649 * We also need to stuff the address of the original IO so we can
11650 * find it easily. Something similar will need be done on the other
11651 * side so when we are done we can find the copy.
11653 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11654 union ctl_ha_msg msg_info;
11657 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11659 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11660 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11662 printf("1. ctsio %p\n", ctsio);
11664 msg_info.hdr.serializing_sc = NULL;
11665 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11666 msg_info.scsi.tag_num = ctsio->tag_num;
11667 msg_info.scsi.tag_type = ctsio->tag_type;
11668 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11670 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11672 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11673 (void *)&msg_info, sizeof(msg_info), 0)) >
11674 CTL_HA_STATUS_SUCCESS) {
11675 printf("CTL:precheck, ctl_ha_msg_send returned %d\n",
11677 printf("CTL:opcode is %x\n", ctsio->cdb[0]);
11680 printf("CTL:Precheck sent msg, opcode is %x\n",opcode);
11685 * XXX KDM this I/O is off the incoming queue, but hasn't
11686 * been inserted on any other queue. We may need to come
11687 * up with a holding queue while we wait for serialization
11688 * so that we have an idea of what we're waiting for from
11691 mtx_unlock(&lun->lun_lock);
11695 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11696 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11697 ctl_ooaq, ooa_links))) {
11698 case CTL_ACTION_BLOCK:
11699 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11700 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11702 mtx_unlock(&lun->lun_lock);
11704 case CTL_ACTION_PASS:
11705 case CTL_ACTION_SKIP:
11706 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11707 mtx_unlock(&lun->lun_lock);
11708 ctl_enqueue_rtr((union ctl_io *)ctsio);
11710 case CTL_ACTION_OVERLAP:
11711 mtx_unlock(&lun->lun_lock);
11712 ctl_set_overlapped_cmd(ctsio);
11713 ctl_done((union ctl_io *)ctsio);
11715 case CTL_ACTION_OVERLAP_TAG:
11716 mtx_unlock(&lun->lun_lock);
11717 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11718 ctl_done((union ctl_io *)ctsio);
11720 case CTL_ACTION_ERROR:
11722 mtx_unlock(&lun->lun_lock);
11723 ctl_set_internal_failure(ctsio,
11725 /*retry_count*/ 0);
11726 ctl_done((union ctl_io *)ctsio);
11732 const struct ctl_cmd_entry *
11733 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11735 const struct ctl_cmd_entry *entry;
11736 int service_action;
11738 entry = &ctl_cmd_table[ctsio->cdb[0]];
11740 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11741 if (entry->flags & CTL_CMD_FLAG_SA5) {
11742 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11743 entry = &((const struct ctl_cmd_entry *)
11744 entry->execute)[service_action];
11749 const struct ctl_cmd_entry *
11750 ctl_validate_command(struct ctl_scsiio *ctsio)
11752 const struct ctl_cmd_entry *entry;
11756 entry = ctl_get_cmd_entry(ctsio, &sa);
11757 if (entry->execute == NULL) {
11759 ctl_set_invalid_field(ctsio,
11766 ctl_set_invalid_opcode(ctsio);
11767 ctl_done((union ctl_io *)ctsio);
11770 KASSERT(entry->length > 0,
11771 ("Not defined length for command 0x%02x/0x%02x",
11772 ctsio->cdb[0], ctsio->cdb[1]));
11773 for (i = 1; i < entry->length; i++) {
11774 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11777 ctl_set_invalid_field(ctsio,
11782 /*bit*/ fls(diff) - 1);
11783 ctl_done((union ctl_io *)ctsio);
11790 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11793 switch (lun_type) {
11795 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) &&
11796 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11800 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) &&
11801 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11811 ctl_scsiio(struct ctl_scsiio *ctsio)
11814 const struct ctl_cmd_entry *entry;
11816 retval = CTL_RETVAL_COMPLETE;
11818 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11820 entry = ctl_get_cmd_entry(ctsio, NULL);
11823 * If this I/O has been aborted, just send it straight to
11824 * ctl_done() without executing it.
11826 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11827 ctl_done((union ctl_io *)ctsio);
11832 * All the checks should have been handled by ctl_scsiio_precheck().
11833 * We should be clear now to just execute the I/O.
11835 retval = entry->execute(ctsio);
11842 * Since we only implement one target right now, a bus reset simply resets
11843 * our single target.
11846 ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io)
11848 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET));
11852 ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
11853 ctl_ua_type ua_type)
11855 struct ctl_lun *lun;
11858 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11859 union ctl_ha_msg msg_info;
11861 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11862 msg_info.hdr.nexus = io->io_hdr.nexus;
11863 if (ua_type==CTL_UA_TARG_RESET)
11864 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11866 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11867 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11868 msg_info.hdr.original_sc = NULL;
11869 msg_info.hdr.serializing_sc = NULL;
11870 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11871 (void *)&msg_info, sizeof(msg_info), 0)) {
11876 mtx_lock(&ctl_softc->ctl_lock);
11877 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links)
11878 retval += ctl_lun_reset(lun, io, ua_type);
11879 mtx_unlock(&ctl_softc->ctl_lock);
11885 * The LUN should always be set. The I/O is optional, and is used to
11886 * distinguish between I/Os sent by this initiator, and by other
11887 * initiators. We set unit attention for initiators other than this one.
11888 * SAM-3 is vague on this point. It does say that a unit attention should
11889 * be established for other initiators when a LUN is reset (see section
11890 * 5.7.3), but it doesn't specifically say that the unit attention should
11891 * be established for this particular initiator when a LUN is reset. Here
11892 * is the relevant text, from SAM-3 rev 8:
11894 * 5.7.2 When a SCSI initiator port aborts its own tasks
11896 * When a SCSI initiator port causes its own task(s) to be aborted, no
11897 * notification that the task(s) have been aborted shall be returned to
11898 * the SCSI initiator port other than the completion response for the
11899 * command or task management function action that caused the task(s) to
11900 * be aborted and notification(s) associated with related effects of the
11901 * action (e.g., a reset unit attention condition).
11903 * XXX KDM for now, we're setting unit attention for all initiators.
11906 ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11910 uint32_t initindex;
11914 mtx_lock(&lun->lun_lock);
11916 * Run through the OOA queue and abort each I/O.
11919 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
11921 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11922 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11923 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11927 * This version sets unit attention for every
11930 initindex = ctl_get_initindex(&io->io_hdr.nexus);
11931 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11932 if (initindex == i)
11934 lun->pending_ua[i] |= ua_type;
11939 * A reset (any kind, really) clears reservations established with
11940 * RESERVE/RELEASE. It does not clear reservations established
11941 * with PERSISTENT RESERVE OUT, but we don't support that at the
11942 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
11943 * reservations made with the RESERVE/RELEASE commands, because
11944 * those commands are obsolete in SPC-3.
11946 lun->flags &= ~CTL_LUN_RESERVED;
11948 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11950 ctl_clear_mask(lun->have_ca, i);
11952 lun->pending_ua[i] |= ua_type;
11954 mtx_unlock(&lun->lun_lock);
11960 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11965 mtx_assert(&lun->lun_lock, MA_OWNED);
11968 * Run through the OOA queue and attempt to find the given I/O.
11969 * The target port, initiator ID, tag type and tag number have to
11970 * match the values that we got from the initiator. If we have an
11971 * untagged command to abort, simply abort the first untagged command
11972 * we come to. We only allow one untagged command at a time of course.
11974 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11975 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11977 if ((targ_port == UINT32_MAX ||
11978 targ_port == xio->io_hdr.nexus.targ_port) &&
11979 (init_id == UINT32_MAX ||
11980 init_id == xio->io_hdr.nexus.initid.id)) {
11981 if (targ_port != xio->io_hdr.nexus.targ_port ||
11982 init_id != xio->io_hdr.nexus.initid.id)
11983 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
11984 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11985 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11986 union ctl_ha_msg msg_info;
11988 msg_info.hdr.nexus = xio->io_hdr.nexus;
11989 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11990 msg_info.task.tag_num = xio->scsiio.tag_num;
11991 msg_info.task.tag_type = xio->scsiio.tag_type;
11992 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11993 msg_info.hdr.original_sc = NULL;
11994 msg_info.hdr.serializing_sc = NULL;
11995 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11996 (void *)&msg_info, sizeof(msg_info), 0);
12003 ctl_abort_task_set(union ctl_io *io)
12005 struct ctl_softc *softc = control_softc;
12006 struct ctl_lun *lun;
12012 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12013 mtx_lock(&softc->ctl_lock);
12014 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL))
12015 lun = softc->ctl_luns[targ_lun];
12017 mtx_unlock(&softc->ctl_lock);
12021 mtx_lock(&lun->lun_lock);
12022 mtx_unlock(&softc->ctl_lock);
12023 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
12024 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12025 io->io_hdr.nexus.initid.id,
12026 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12027 } else { /* CTL_TASK_CLEAR_TASK_SET */
12028 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
12029 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12031 mtx_unlock(&lun->lun_lock);
12036 ctl_i_t_nexus_reset(union ctl_io *io)
12038 struct ctl_softc *softc = control_softc;
12039 struct ctl_lun *lun;
12040 uint32_t initindex, residx;
12042 initindex = ctl_get_initindex(&io->io_hdr.nexus);
12043 residx = ctl_get_resindex(&io->io_hdr.nexus);
12044 mtx_lock(&softc->ctl_lock);
12045 STAILQ_FOREACH(lun, &softc->lun_list, links) {
12046 mtx_lock(&lun->lun_lock);
12047 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12048 io->io_hdr.nexus.initid.id,
12049 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12051 ctl_clear_mask(lun->have_ca, initindex);
12053 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
12054 lun->flags &= ~CTL_LUN_RESERVED;
12055 lun->pending_ua[initindex] |= CTL_UA_I_T_NEXUS_LOSS;
12056 mtx_unlock(&lun->lun_lock);
12058 mtx_unlock(&softc->ctl_lock);
12063 ctl_abort_task(union ctl_io *io)
12066 struct ctl_lun *lun;
12067 struct ctl_softc *ctl_softc;
12070 char printbuf[128];
12075 ctl_softc = control_softc;
12081 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12082 mtx_lock(&ctl_softc->ctl_lock);
12083 if ((targ_lun < CTL_MAX_LUNS)
12084 && (ctl_softc->ctl_luns[targ_lun] != NULL))
12085 lun = ctl_softc->ctl_luns[targ_lun];
12087 mtx_unlock(&ctl_softc->ctl_lock);
12092 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
12093 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
12096 mtx_lock(&lun->lun_lock);
12097 mtx_unlock(&ctl_softc->ctl_lock);
12099 * Run through the OOA queue and attempt to find the given I/O.
12100 * The target port, initiator ID, tag type and tag number have to
12101 * match the values that we got from the initiator. If we have an
12102 * untagged command to abort, simply abort the first untagged command
12103 * we come to. We only allow one untagged command at a time of course.
12106 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
12108 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12109 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12111 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
12113 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
12114 lun->lun, xio->scsiio.tag_num,
12115 xio->scsiio.tag_type,
12116 (xio->io_hdr.blocked_links.tqe_prev
12117 == NULL) ? "" : " BLOCKED",
12118 (xio->io_hdr.flags &
12119 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
12120 (xio->io_hdr.flags &
12121 CTL_FLAG_ABORT) ? " ABORT" : "",
12122 (xio->io_hdr.flags &
12123 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
12124 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
12126 printf("%s\n", sbuf_data(&sb));
12129 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port)
12130 && (xio->io_hdr.nexus.initid.id ==
12131 io->io_hdr.nexus.initid.id)) {
12133 * If the abort says that the task is untagged, the
12134 * task in the queue must be untagged. Otherwise,
12135 * we just check to see whether the tag numbers
12136 * match. This is because the QLogic firmware
12137 * doesn't pass back the tag type in an abort
12141 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12142 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12143 || (xio->scsiio.tag_num == io->taskio.tag_num)) {
12146 * XXX KDM we've got problems with FC, because it
12147 * doesn't send down a tag type with aborts. So we
12148 * can only really go by the tag number...
12149 * This may cause problems with parallel SCSI.
12150 * Need to figure that out!!
12152 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12153 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12155 if ((io->io_hdr.flags &
12156 CTL_FLAG_FROM_OTHER_SC) == 0 &&
12157 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12158 union ctl_ha_msg msg_info;
12160 io->io_hdr.flags |=
12161 CTL_FLAG_SENT_2OTHER_SC;
12162 msg_info.hdr.nexus = io->io_hdr.nexus;
12163 msg_info.task.task_action =
12164 CTL_TASK_ABORT_TASK;
12165 msg_info.task.tag_num =
12166 io->taskio.tag_num;
12167 msg_info.task.tag_type =
12168 io->taskio.tag_type;
12169 msg_info.hdr.msg_type =
12170 CTL_MSG_MANAGE_TASKS;
12171 msg_info.hdr.original_sc = NULL;
12172 msg_info.hdr.serializing_sc = NULL;
12174 printf("Sent Abort to other side\n");
12176 if (CTL_HA_STATUS_SUCCESS !=
12177 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12179 sizeof(msg_info), 0)) {
12183 printf("ctl_abort_task: found I/O to abort\n");
12189 mtx_unlock(&lun->lun_lock);
12193 * This isn't really an error. It's entirely possible for
12194 * the abort and command completion to cross on the wire.
12195 * This is more of an informative/diagnostic error.
12198 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
12199 "%d:%d:%d:%d tag %d type %d\n",
12200 io->io_hdr.nexus.initid.id,
12201 io->io_hdr.nexus.targ_port,
12202 io->io_hdr.nexus.targ_target.id,
12203 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
12204 io->taskio.tag_type);
12211 ctl_run_task(union ctl_io *io)
12213 struct ctl_softc *ctl_softc = control_softc;
12215 const char *task_desc;
12217 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12219 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12220 ("ctl_run_task: Unextected io_type %d\n",
12221 io->io_hdr.io_type));
12223 task_desc = ctl_scsi_task_string(&io->taskio);
12224 if (task_desc != NULL) {
12226 csevent_log(CSC_CTL | CSC_SHELF_SW |
12228 csevent_LogType_Trace,
12229 csevent_Severity_Information,
12230 csevent_AlertLevel_Green,
12231 csevent_FRU_Firmware,
12232 csevent_FRU_Unknown,
12233 "CTL: received task: %s",task_desc);
12237 csevent_log(CSC_CTL | CSC_SHELF_SW |
12239 csevent_LogType_Trace,
12240 csevent_Severity_Information,
12241 csevent_AlertLevel_Green,
12242 csevent_FRU_Firmware,
12243 csevent_FRU_Unknown,
12244 "CTL: received unknown task "
12246 io->taskio.task_action,
12247 io->taskio.task_action);
12250 switch (io->taskio.task_action) {
12251 case CTL_TASK_ABORT_TASK:
12252 retval = ctl_abort_task(io);
12254 case CTL_TASK_ABORT_TASK_SET:
12255 case CTL_TASK_CLEAR_TASK_SET:
12256 retval = ctl_abort_task_set(io);
12258 case CTL_TASK_CLEAR_ACA:
12260 case CTL_TASK_I_T_NEXUS_RESET:
12261 retval = ctl_i_t_nexus_reset(io);
12263 case CTL_TASK_LUN_RESET: {
12264 struct ctl_lun *lun;
12267 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12268 mtx_lock(&ctl_softc->ctl_lock);
12269 if ((targ_lun < CTL_MAX_LUNS)
12270 && (ctl_softc->ctl_luns[targ_lun] != NULL))
12271 lun = ctl_softc->ctl_luns[targ_lun];
12273 mtx_unlock(&ctl_softc->ctl_lock);
12278 if (!(io->io_hdr.flags &
12279 CTL_FLAG_FROM_OTHER_SC)) {
12280 union ctl_ha_msg msg_info;
12282 io->io_hdr.flags |=
12283 CTL_FLAG_SENT_2OTHER_SC;
12284 msg_info.hdr.msg_type =
12285 CTL_MSG_MANAGE_TASKS;
12286 msg_info.hdr.nexus = io->io_hdr.nexus;
12287 msg_info.task.task_action =
12288 CTL_TASK_LUN_RESET;
12289 msg_info.hdr.original_sc = NULL;
12290 msg_info.hdr.serializing_sc = NULL;
12291 if (CTL_HA_STATUS_SUCCESS !=
12292 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12294 sizeof(msg_info), 0)) {
12298 retval = ctl_lun_reset(lun, io,
12300 mtx_unlock(&ctl_softc->ctl_lock);
12303 case CTL_TASK_TARGET_RESET:
12304 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET);
12306 case CTL_TASK_BUS_RESET:
12307 retval = ctl_bus_reset(ctl_softc, io);
12309 case CTL_TASK_PORT_LOGIN:
12311 case CTL_TASK_PORT_LOGOUT:
12314 printf("ctl_run_task: got unknown task management event %d\n",
12315 io->taskio.task_action);
12319 io->io_hdr.status = CTL_SUCCESS;
12321 io->io_hdr.status = CTL_ERROR;
12326 * For HA operation. Handle commands that come in from the other
12330 ctl_handle_isc(union ctl_io *io)
12333 struct ctl_lun *lun;
12334 struct ctl_softc *ctl_softc;
12337 ctl_softc = control_softc;
12339 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12340 lun = ctl_softc->ctl_luns[targ_lun];
12342 switch (io->io_hdr.msg_type) {
12343 case CTL_MSG_SERIALIZE:
12344 free_io = ctl_serialize_other_sc_cmd(&io->scsiio);
12346 case CTL_MSG_R2R: {
12347 const struct ctl_cmd_entry *entry;
12350 * This is only used in SER_ONLY mode.
12353 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12354 mtx_lock(&lun->lun_lock);
12355 if (ctl_scsiio_lun_check(ctl_softc, lun,
12356 entry, (struct ctl_scsiio *)io) != 0) {
12357 mtx_unlock(&lun->lun_lock);
12361 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12362 mtx_unlock(&lun->lun_lock);
12363 ctl_enqueue_rtr(io);
12366 case CTL_MSG_FINISH_IO:
12367 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
12372 mtx_lock(&lun->lun_lock);
12373 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
12375 ctl_check_blocked(lun);
12376 mtx_unlock(&lun->lun_lock);
12379 case CTL_MSG_PERS_ACTION:
12380 ctl_hndl_per_res_out_on_other_sc(
12381 (union ctl_ha_msg *)&io->presio.pr_msg);
12384 case CTL_MSG_BAD_JUJU:
12388 case CTL_MSG_DATAMOVE:
12389 /* Only used in XFER mode */
12391 ctl_datamove_remote(io);
12393 case CTL_MSG_DATAMOVE_DONE:
12394 /* Only used in XFER mode */
12396 io->scsiio.be_move_done(io);
12400 printf("%s: Invalid message type %d\n",
12401 __func__, io->io_hdr.msg_type);
12411 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12412 * there is no match.
12414 static ctl_lun_error_pattern
12415 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12417 const struct ctl_cmd_entry *entry;
12418 ctl_lun_error_pattern filtered_pattern, pattern;
12420 pattern = desc->error_pattern;
12423 * XXX KDM we need more data passed into this function to match a
12424 * custom pattern, and we actually need to implement custom pattern
12427 if (pattern & CTL_LUN_PAT_CMD)
12428 return (CTL_LUN_PAT_CMD);
12430 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12431 return (CTL_LUN_PAT_ANY);
12433 entry = ctl_get_cmd_entry(ctsio, NULL);
12435 filtered_pattern = entry->pattern & pattern;
12438 * If the user requested specific flags in the pattern (e.g.
12439 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12442 * If the user did not specify any flags, it doesn't matter whether
12443 * or not the command supports the flags.
12445 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12446 (pattern & ~CTL_LUN_PAT_MASK))
12447 return (CTL_LUN_PAT_NONE);
12450 * If the user asked for a range check, see if the requested LBA
12451 * range overlaps with this command's LBA range.
12453 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12459 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12461 return (CTL_LUN_PAT_NONE);
12463 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12464 desc->lba_range.len);
12466 * A "pass" means that the LBA ranges don't overlap, so
12467 * this doesn't match the user's range criteria.
12469 if (action == CTL_ACTION_PASS)
12470 return (CTL_LUN_PAT_NONE);
12473 return (filtered_pattern);
12477 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12479 struct ctl_error_desc *desc, *desc2;
12481 mtx_assert(&lun->lun_lock, MA_OWNED);
12483 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12484 ctl_lun_error_pattern pattern;
12486 * Check to see whether this particular command matches
12487 * the pattern in the descriptor.
12489 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12490 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12493 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12494 case CTL_LUN_INJ_ABORTED:
12495 ctl_set_aborted(&io->scsiio);
12497 case CTL_LUN_INJ_MEDIUM_ERR:
12498 ctl_set_medium_error(&io->scsiio);
12500 case CTL_LUN_INJ_UA:
12501 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12503 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12505 case CTL_LUN_INJ_CUSTOM:
12507 * We're assuming the user knows what he is doing.
12508 * Just copy the sense information without doing
12511 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12512 ctl_min(sizeof(desc->custom_sense),
12513 sizeof(io->scsiio.sense_data)));
12514 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12515 io->scsiio.sense_len = SSD_FULL_SIZE;
12516 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12518 case CTL_LUN_INJ_NONE:
12521 * If this is an error injection type we don't know
12522 * about, clear the continuous flag (if it is set)
12523 * so it will get deleted below.
12525 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12529 * By default, each error injection action is a one-shot
12531 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12534 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12540 #ifdef CTL_IO_DELAY
12542 ctl_datamove_timer_wakeup(void *arg)
12546 io = (union ctl_io *)arg;
12550 #endif /* CTL_IO_DELAY */
12553 ctl_datamove(union ctl_io *io)
12555 void (*fe_datamove)(union ctl_io *io);
12557 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12559 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12562 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12567 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12568 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12570 sbuf_cat(&sb, path_str);
12571 switch (io->io_hdr.io_type) {
12573 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12574 sbuf_printf(&sb, "\n");
12575 sbuf_cat(&sb, path_str);
12576 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12577 io->scsiio.tag_num, io->scsiio.tag_type);
12580 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12581 "Tag Type: %d\n", io->taskio.task_action,
12582 io->taskio.tag_num, io->taskio.tag_type);
12585 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12586 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12589 sbuf_cat(&sb, path_str);
12590 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12591 (intmax_t)time_uptime - io->io_hdr.start_time);
12593 printf("%s", sbuf_data(&sb));
12595 #endif /* CTL_TIME_IO */
12597 #ifdef CTL_IO_DELAY
12598 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12599 struct ctl_lun *lun;
12601 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12603 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12605 struct ctl_lun *lun;
12607 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12609 && (lun->delay_info.datamove_delay > 0)) {
12610 struct callout *callout;
12612 callout = (struct callout *)&io->io_hdr.timer_bytes;
12613 callout_init(callout, /*mpsafe*/ 1);
12614 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12615 callout_reset(callout,
12616 lun->delay_info.datamove_delay * hz,
12617 ctl_datamove_timer_wakeup, io);
12618 if (lun->delay_info.datamove_type ==
12619 CTL_DELAY_TYPE_ONESHOT)
12620 lun->delay_info.datamove_delay = 0;
12627 * This command has been aborted. Set the port status, so we fail
12630 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12631 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n",
12632 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id,
12633 io->io_hdr.nexus.targ_port,
12634 (uintmax_t)io->io_hdr.nexus.targ_target.id,
12635 io->io_hdr.nexus.targ_lun);
12636 io->io_hdr.port_status = 31337;
12638 * Note that the backend, in this case, will get the
12639 * callback in its context. In other cases it may get
12640 * called in the frontend's interrupt thread context.
12642 io->scsiio.be_move_done(io);
12646 /* Don't confuse frontend with zero length data move. */
12647 if (io->scsiio.kern_data_len == 0) {
12648 io->scsiio.be_move_done(io);
12653 * If we're in XFER mode and this I/O is from the other shelf
12654 * controller, we need to send the DMA to the other side to
12655 * actually transfer the data to/from the host. In serialize only
12656 * mode the transfer happens below CTL and ctl_datamove() is only
12657 * called on the machine that originally received the I/O.
12659 if ((control_softc->ha_mode == CTL_HA_MODE_XFER)
12660 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12661 union ctl_ha_msg msg;
12662 uint32_t sg_entries_sent;
12666 memset(&msg, 0, sizeof(msg));
12667 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
12668 msg.hdr.original_sc = io->io_hdr.original_sc;
12669 msg.hdr.serializing_sc = io;
12670 msg.hdr.nexus = io->io_hdr.nexus;
12671 msg.dt.flags = io->io_hdr.flags;
12673 * We convert everything into a S/G list here. We can't
12674 * pass by reference, only by value between controllers.
12675 * So we can't pass a pointer to the S/G list, only as many
12676 * S/G entries as we can fit in here. If it's possible for
12677 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
12678 * then we need to break this up into multiple transfers.
12680 if (io->scsiio.kern_sg_entries == 0) {
12681 msg.dt.kern_sg_entries = 1;
12683 * If this is in cached memory, flush the cache
12684 * before we send the DMA request to the other
12685 * controller. We want to do this in either the
12686 * read or the write case. The read case is
12687 * straightforward. In the write case, we want to
12688 * make sure nothing is in the local cache that
12689 * could overwrite the DMAed data.
12691 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12693 * XXX KDM use bus_dmamap_sync() here.
12698 * Convert to a physical address if this is a
12701 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
12702 msg.dt.sg_list[0].addr =
12703 io->scsiio.kern_data_ptr;
12706 * XXX KDM use busdma here!
12709 msg.dt.sg_list[0].addr = (void *)
12710 vtophys(io->scsiio.kern_data_ptr);
12714 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
12717 struct ctl_sg_entry *sgl;
12720 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
12721 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
12722 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12724 * XXX KDM use bus_dmamap_sync() here.
12729 msg.dt.kern_data_len = io->scsiio.kern_data_len;
12730 msg.dt.kern_total_len = io->scsiio.kern_total_len;
12731 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
12732 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
12733 msg.dt.sg_sequence = 0;
12736 * Loop until we've sent all of the S/G entries. On the
12737 * other end, we'll recompose these S/G entries into one
12738 * contiguous list before passing it to the
12740 for (sg_entries_sent = 0; sg_entries_sent <
12741 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) {
12742 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/
12743 sizeof(msg.dt.sg_list[0])),
12744 msg.dt.kern_sg_entries - sg_entries_sent);
12746 if (do_sg_copy != 0) {
12747 struct ctl_sg_entry *sgl;
12750 sgl = (struct ctl_sg_entry *)
12751 io->scsiio.kern_data_ptr;
12753 * If this is in cached memory, flush the cache
12754 * before we send the DMA request to the other
12755 * controller. We want to do this in either
12756 * the * read or the write case. The read
12757 * case is straightforward. In the write
12758 * case, we want to make sure nothing is
12759 * in the local cache that could overwrite
12763 for (i = sg_entries_sent, j = 0;
12764 i < msg.dt.cur_sg_entries; i++, j++) {
12765 if ((io->io_hdr.flags &
12766 CTL_FLAG_NO_DATASYNC) == 0) {
12768 * XXX KDM use bus_dmamap_sync()
12771 if ((io->io_hdr.flags &
12772 CTL_FLAG_BUS_ADDR) == 0) {
12774 * XXX KDM use busdma.
12777 msg.dt.sg_list[j].addr =(void *)
12778 vtophys(sgl[i].addr);
12781 msg.dt.sg_list[j].addr =
12784 msg.dt.sg_list[j].len = sgl[i].len;
12788 sg_entries_sent += msg.dt.cur_sg_entries;
12789 if (sg_entries_sent >= msg.dt.kern_sg_entries)
12790 msg.dt.sg_last = 1;
12792 msg.dt.sg_last = 0;
12795 * XXX KDM drop and reacquire the lock here?
12797 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12798 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
12800 * XXX do something here.
12804 msg.dt.sent_sg_entries = sg_entries_sent;
12806 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12807 if (io->io_hdr.flags & CTL_FLAG_FAILOVER)
12808 ctl_failover_io(io, /*have_lock*/ 0);
12813 * Lookup the fe_datamove() function for this particular
12817 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12824 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12826 union ctl_ha_msg msg;
12829 memset(&msg, 0, sizeof(msg));
12831 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12832 msg.hdr.original_sc = io;
12833 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12834 msg.hdr.nexus = io->io_hdr.nexus;
12835 msg.hdr.status = io->io_hdr.status;
12836 msg.scsi.tag_num = io->scsiio.tag_num;
12837 msg.scsi.tag_type = io->scsiio.tag_type;
12838 msg.scsi.scsi_status = io->scsiio.scsi_status;
12839 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12840 sizeof(io->scsiio.sense_data));
12841 msg.scsi.sense_len = io->scsiio.sense_len;
12842 msg.scsi.sense_residual = io->scsiio.sense_residual;
12843 msg.scsi.fetd_status = io->io_hdr.port_status;
12844 msg.scsi.residual = io->scsiio.residual;
12845 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12847 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12848 ctl_failover_io(io, /*have_lock*/ have_lock);
12852 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0);
12853 if (isc_status > CTL_HA_STATUS_SUCCESS) {
12854 /* XXX do something if this fails */
12860 * The DMA to the remote side is done, now we need to tell the other side
12861 * we're done so it can continue with its data movement.
12864 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12870 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12871 printf("%s: ISC DMA write failed with error %d", __func__,
12873 ctl_set_internal_failure(&io->scsiio,
12875 /*retry_count*/ rq->ret);
12878 ctl_dt_req_free(rq);
12881 * In this case, we had to malloc the memory locally. Free it.
12883 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12885 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12886 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12889 * The data is in local and remote memory, so now we need to send
12890 * status (good or back) back to the other side.
12892 ctl_send_datamove_done(io, /*have_lock*/ 0);
12896 * We've moved the data from the host/controller into local memory. Now we
12897 * need to push it over to the remote controller's memory.
12900 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12906 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12907 ctl_datamove_remote_write_cb);
12913 ctl_datamove_remote_write(union ctl_io *io)
12916 void (*fe_datamove)(union ctl_io *io);
12919 * - Get the data from the host/HBA into local memory.
12920 * - DMA memory from the local controller to the remote controller.
12921 * - Send status back to the remote controller.
12924 retval = ctl_datamove_remote_sgl_setup(io);
12928 /* Switch the pointer over so the FETD knows what to do */
12929 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12932 * Use a custom move done callback, since we need to send completion
12933 * back to the other controller, not to the backend on this side.
12935 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12937 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12946 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12955 * In this case, we had to malloc the memory locally. Free it.
12957 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12959 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12960 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12964 scsi_path_string(io, path_str, sizeof(path_str));
12965 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12966 sbuf_cat(&sb, path_str);
12967 scsi_command_string(&io->scsiio, NULL, &sb);
12968 sbuf_printf(&sb, "\n");
12969 sbuf_cat(&sb, path_str);
12970 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12971 io->scsiio.tag_num, io->scsiio.tag_type);
12972 sbuf_cat(&sb, path_str);
12973 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12974 io->io_hdr.flags, io->io_hdr.status);
12976 printk("%s", sbuf_data(&sb));
12981 * The read is done, now we need to send status (good or bad) back
12982 * to the other side.
12984 ctl_send_datamove_done(io, /*have_lock*/ 0);
12990 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12993 void (*fe_datamove)(union ctl_io *io);
12997 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12998 printf("%s: ISC DMA read failed with error %d", __func__,
13000 ctl_set_internal_failure(&io->scsiio,
13002 /*retry_count*/ rq->ret);
13005 ctl_dt_req_free(rq);
13007 /* Switch the pointer over so the FETD knows what to do */
13008 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
13011 * Use a custom move done callback, since we need to send completion
13012 * back to the other controller, not to the backend on this side.
13014 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
13016 /* XXX KDM add checks like the ones in ctl_datamove? */
13018 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
13024 ctl_datamove_remote_sgl_setup(union ctl_io *io)
13026 struct ctl_sg_entry *local_sglist, *remote_sglist;
13027 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist;
13028 struct ctl_softc *softc;
13033 softc = control_softc;
13035 local_sglist = io->io_hdr.local_sglist;
13036 local_dma_sglist = io->io_hdr.local_dma_sglist;
13037 remote_sglist = io->io_hdr.remote_sglist;
13038 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
13040 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) {
13041 for (i = 0; i < io->scsiio.kern_sg_entries; i++) {
13042 local_sglist[i].len = remote_sglist[i].len;
13045 * XXX Detect the situation where the RS-level I/O
13046 * redirector on the other side has already read the
13047 * data off of the AOR RS on this side, and
13048 * transferred it to remote (mirror) memory on the
13049 * other side. Since we already have the data in
13050 * memory here, we just need to use it.
13052 * XXX KDM this can probably be removed once we
13053 * get the cache device code in and take the
13054 * current AOR implementation out.
13057 if ((remote_sglist[i].addr >=
13058 (void *)vtophys(softc->mirr->addr))
13059 && (remote_sglist[i].addr <
13060 ((void *)vtophys(softc->mirr->addr) +
13061 CacheMirrorOffset))) {
13062 local_sglist[i].addr = remote_sglist[i].addr -
13064 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13066 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE;
13068 local_sglist[i].addr = remote_sglist[i].addr +
13073 printf("%s: local %p, remote %p, len %d\n",
13074 __func__, local_sglist[i].addr,
13075 remote_sglist[i].addr, local_sglist[i].len);
13079 uint32_t len_to_go;
13082 * In this case, we don't have automatically allocated
13083 * memory for this I/O on this controller. This typically
13084 * happens with internal CTL I/O -- e.g. inquiry, mode
13085 * sense, etc. Anything coming from RAIDCore will have
13086 * a mirror area available.
13088 len_to_go = io->scsiio.kern_data_len;
13091 * Clear the no datasync flag, we have to use malloced
13094 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC;
13097 * The difficult thing here is that the size of the various
13098 * S/G segments may be different than the size from the
13099 * remote controller. That'll make it harder when DMAing
13100 * the data back to the other side.
13102 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) /
13103 sizeof(io->io_hdr.remote_sglist[0])) &&
13104 (len_to_go > 0); i++) {
13105 local_sglist[i].len = ctl_min(len_to_go, 131072);
13106 CTL_SIZE_8B(local_dma_sglist[i].len,
13107 local_sglist[i].len);
13108 local_sglist[i].addr =
13109 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK);
13111 local_dma_sglist[i].addr = local_sglist[i].addr;
13113 if (local_sglist[i].addr == NULL) {
13116 printf("malloc failed for %zd bytes!",
13117 local_dma_sglist[i].len);
13118 for (j = 0; j < i; j++) {
13119 free(local_sglist[j].addr, M_CTL);
13121 ctl_set_internal_failure(&io->scsiio,
13123 /*retry_count*/ 4857);
13125 goto bailout_error;
13128 /* XXX KDM do we need a sync here? */
13130 len_to_go -= local_sglist[i].len;
13133 * Reset the number of S/G entries accordingly. The
13134 * original number of S/G entries is available in
13137 io->scsiio.kern_sg_entries = i;
13140 printf("%s: kern_sg_entries = %d\n", __func__,
13141 io->scsiio.kern_sg_entries);
13142 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13143 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i,
13144 local_sglist[i].addr, local_sglist[i].len,
13145 local_dma_sglist[i].len);
13154 ctl_send_datamove_done(io, /*have_lock*/ 0);
13160 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
13161 ctl_ha_dt_cb callback)
13163 struct ctl_ha_dt_req *rq;
13164 struct ctl_sg_entry *remote_sglist, *local_sglist;
13165 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist;
13166 uint32_t local_used, remote_used, total_used;
13172 rq = ctl_dt_req_alloc();
13175 * If we failed to allocate the request, and if the DMA didn't fail
13176 * anyway, set busy status. This is just a resource allocation
13180 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE))
13181 ctl_set_busy(&io->scsiio);
13183 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
13186 ctl_dt_req_free(rq);
13189 * The data move failed. We need to return status back
13190 * to the other controller. No point in trying to DMA
13191 * data to the remote controller.
13194 ctl_send_datamove_done(io, /*have_lock*/ 0);
13201 local_sglist = io->io_hdr.local_sglist;
13202 local_dma_sglist = io->io_hdr.local_dma_sglist;
13203 remote_sglist = io->io_hdr.remote_sglist;
13204 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
13209 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) {
13210 rq->ret = CTL_HA_STATUS_SUCCESS;
13217 * Pull/push the data over the wire from/to the other controller.
13218 * This takes into account the possibility that the local and
13219 * remote sglists may not be identical in terms of the size of
13220 * the elements and the number of elements.
13222 * One fundamental assumption here is that the length allocated for
13223 * both the local and remote sglists is identical. Otherwise, we've
13224 * essentially got a coding error of some sort.
13226 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
13228 uint32_t cur_len, dma_length;
13231 rq->id = CTL_HA_DATA_CTL;
13232 rq->command = command;
13236 * Both pointers should be aligned. But it is possible
13237 * that the allocation length is not. They should both
13238 * also have enough slack left over at the end, though,
13239 * to round up to the next 8 byte boundary.
13241 cur_len = ctl_min(local_sglist[i].len - local_used,
13242 remote_sglist[j].len - remote_used);
13245 * In this case, we have a size issue and need to decrease
13246 * the size, except in the case where we actually have less
13247 * than 8 bytes left. In that case, we need to increase
13248 * the DMA length to get the last bit.
13250 if ((cur_len & 0x7) != 0) {
13251 if (cur_len > 0x7) {
13252 cur_len = cur_len - (cur_len & 0x7);
13253 dma_length = cur_len;
13255 CTL_SIZE_8B(dma_length, cur_len);
13259 dma_length = cur_len;
13262 * If we had to allocate memory for this I/O, instead of using
13263 * the non-cached mirror memory, we'll need to flush the cache
13264 * before trying to DMA to the other controller.
13266 * We could end up doing this multiple times for the same
13267 * segment if we have a larger local segment than remote
13268 * segment. That shouldn't be an issue.
13270 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
13272 * XXX KDM use bus_dmamap_sync() here.
13276 rq->size = dma_length;
13278 tmp_ptr = (uint8_t *)local_sglist[i].addr;
13279 tmp_ptr += local_used;
13281 /* Use physical addresses when talking to ISC hardware */
13282 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
13283 /* XXX KDM use busdma */
13285 rq->local = vtophys(tmp_ptr);
13288 rq->local = tmp_ptr;
13290 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
13291 tmp_ptr += remote_used;
13292 rq->remote = tmp_ptr;
13294 rq->callback = NULL;
13296 local_used += cur_len;
13297 if (local_used >= local_sglist[i].len) {
13302 remote_used += cur_len;
13303 if (remote_used >= remote_sglist[j].len) {
13307 total_used += cur_len;
13309 if (total_used >= io->scsiio.kern_data_len)
13310 rq->callback = callback;
13312 if ((rq->size & 0x7) != 0) {
13313 printf("%s: warning: size %d is not on 8b boundary\n",
13314 __func__, rq->size);
13316 if (((uintptr_t)rq->local & 0x7) != 0) {
13317 printf("%s: warning: local %p not on 8b boundary\n",
13318 __func__, rq->local);
13320 if (((uintptr_t)rq->remote & 0x7) != 0) {
13321 printf("%s: warning: remote %p not on 8b boundary\n",
13322 __func__, rq->local);
13325 printf("%s: %s: local %#x remote %#x size %d\n", __func__,
13326 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
13327 rq->local, rq->remote, rq->size);
13330 isc_ret = ctl_dt_single(rq);
13331 if (isc_ret == CTL_HA_STATUS_WAIT)
13334 if (isc_ret == CTL_HA_STATUS_DISCONNECT) {
13335 rq->ret = CTL_HA_STATUS_SUCCESS;
13349 ctl_datamove_remote_read(union ctl_io *io)
13355 * This will send an error to the other controller in the case of a
13358 retval = ctl_datamove_remote_sgl_setup(io);
13362 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
13363 ctl_datamove_remote_read_cb);
13365 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) {
13367 * Make sure we free memory if there was an error.. The
13368 * ctl_datamove_remote_xfer() function will send the
13369 * datamove done message, or call the callback with an
13370 * error if there is a problem.
13372 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13373 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13380 * Process a datamove request from the other controller. This is used for
13381 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
13382 * first. Once that is complete, the data gets DMAed into the remote
13383 * controller's memory. For reads, we DMA from the remote controller's
13384 * memory into our memory first, and then move it out to the FETD.
13387 ctl_datamove_remote(union ctl_io *io)
13389 struct ctl_softc *softc;
13391 softc = control_softc;
13393 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
13396 * Note that we look for an aborted I/O here, but don't do some of
13397 * the other checks that ctl_datamove() normally does.
13398 * We don't need to run the datamove delay code, since that should
13399 * have been done if need be on the other controller.
13401 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
13402 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__,
13403 io->scsiio.tag_num, io->io_hdr.nexus.initid.id,
13404 io->io_hdr.nexus.targ_port,
13405 io->io_hdr.nexus.targ_target.id,
13406 io->io_hdr.nexus.targ_lun);
13407 io->io_hdr.port_status = 31338;
13408 ctl_send_datamove_done(io, /*have_lock*/ 0);
13412 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) {
13413 ctl_datamove_remote_write(io);
13414 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){
13415 ctl_datamove_remote_read(io);
13417 union ctl_ha_msg msg;
13418 struct scsi_sense_data *sense;
13422 memset(&msg, 0, sizeof(msg));
13424 msg.hdr.msg_type = CTL_MSG_BAD_JUJU;
13425 msg.hdr.status = CTL_SCSI_ERROR;
13426 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
13428 retry_count = 4243;
13430 sense = &msg.scsi.sense_data;
13431 sks[0] = SSD_SCS_VALID;
13432 sks[1] = (retry_count >> 8) & 0xff;
13433 sks[2] = retry_count & 0xff;
13435 /* "Internal target failure" */
13436 scsi_set_sense_data(sense,
13437 /*sense_format*/ SSD_TYPE_NONE,
13438 /*current_error*/ 1,
13439 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
13442 /*type*/ SSD_ELEM_SKS,
13443 /*size*/ sizeof(sks),
13447 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
13448 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
13449 ctl_failover_io(io, /*have_lock*/ 1);
13453 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) >
13454 CTL_HA_STATUS_SUCCESS) {
13455 /* XXX KDM what to do if this fails? */
13463 ctl_process_done(union ctl_io *io)
13465 struct ctl_lun *lun;
13466 struct ctl_softc *ctl_softc;
13467 void (*fe_done)(union ctl_io *io);
13468 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port);
13470 CTL_DEBUG_PRINT(("ctl_process_done\n"));
13473 control_softc->ctl_ports[targ_port]->fe_done;
13476 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
13481 ctl_scsi_path_string(io, path_str, sizeof(path_str));
13482 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13484 sbuf_cat(&sb, path_str);
13485 switch (io->io_hdr.io_type) {
13487 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
13488 sbuf_printf(&sb, "\n");
13489 sbuf_cat(&sb, path_str);
13490 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13491 io->scsiio.tag_num, io->scsiio.tag_type);
13494 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
13495 "Tag Type: %d\n", io->taskio.task_action,
13496 io->taskio.tag_num, io->taskio.tag_type);
13499 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13500 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13503 sbuf_cat(&sb, path_str);
13504 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
13505 (intmax_t)time_uptime - io->io_hdr.start_time);
13507 printf("%s", sbuf_data(&sb));
13509 #endif /* CTL_TIME_IO */
13511 switch (io->io_hdr.io_type) {
13515 if (bootverbose || (ctl_debug & CTL_DEBUG_INFO))
13516 ctl_io_error_print(io, NULL);
13517 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
13521 return (CTL_RETVAL_COMPLETE);
13523 panic("ctl_process_done: invalid io type %d\n",
13524 io->io_hdr.io_type);
13525 break; /* NOTREACHED */
13528 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13530 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13531 io->io_hdr.nexus.targ_mapped_lun));
13535 ctl_softc = lun->ctl_softc;
13537 mtx_lock(&lun->lun_lock);
13540 * Check to see if we have any errors to inject here. We only
13541 * inject errors for commands that don't already have errors set.
13543 if ((STAILQ_FIRST(&lun->error_list) != NULL)
13544 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))
13545 ctl_inject_error(lun, io);
13548 * XXX KDM how do we treat commands that aren't completed
13551 * XXX KDM should we also track I/O latency?
13553 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13554 io->io_hdr.io_type == CTL_IO_SCSI) {
13556 struct bintime cur_bt;
13560 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13562 type = CTL_STATS_READ;
13563 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13565 type = CTL_STATS_WRITE;
13567 type = CTL_STATS_NO_IO;
13569 lun->stats.ports[targ_port].bytes[type] +=
13570 io->scsiio.kern_total_len;
13571 lun->stats.ports[targ_port].operations[type]++;
13573 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
13574 &io->io_hdr.dma_bt);
13575 lun->stats.ports[targ_port].num_dmas[type] +=
13576 io->io_hdr.num_dmas;
13577 getbintime(&cur_bt);
13578 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
13579 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
13584 * Remove this from the OOA queue.
13586 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13589 * Run through the blocked queue on this LUN and see if anything
13590 * has become unblocked, now that this transaction is done.
13592 ctl_check_blocked(lun);
13595 * If the LUN has been invalidated, free it if there is nothing
13596 * left on its OOA queue.
13598 if ((lun->flags & CTL_LUN_INVALID)
13599 && TAILQ_EMPTY(&lun->ooa_queue)) {
13600 mtx_unlock(&lun->lun_lock);
13601 mtx_lock(&ctl_softc->ctl_lock);
13603 mtx_unlock(&ctl_softc->ctl_lock);
13605 mtx_unlock(&lun->lun_lock);
13608 * If this command has been aborted, make sure we set the status
13609 * properly. The FETD is responsible for freeing the I/O and doing
13610 * whatever it needs to do to clean up its state.
13612 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13613 ctl_set_task_aborted(&io->scsiio);
13616 * If enabled, print command error status.
13617 * We don't print UAs unless debugging was enabled explicitly.
13620 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
13622 if (!bootverbose && (ctl_debug & CTL_DEBUG_INFO) == 0)
13624 if ((ctl_debug & CTL_DEBUG_INFO) == 0 &&
13625 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) &&
13626 (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) {
13627 int error_code, sense_key, asc, ascq;
13629 scsi_extract_sense_len(&io->scsiio.sense_data,
13630 io->scsiio.sense_len, &error_code, &sense_key,
13631 &asc, &ascq, /*show_errors*/ 0);
13632 if (sense_key == SSD_KEY_UNIT_ATTENTION)
13636 ctl_io_error_print(io, NULL);
13640 * Tell the FETD or the other shelf controller we're done with this
13641 * command. Note that only SCSI commands get to this point. Task
13642 * management commands are completed above.
13644 * We only send status to the other controller if we're in XFER
13645 * mode. In SER_ONLY mode, the I/O is done on the controller that
13646 * received the I/O (from CTL's perspective), and so the status is
13649 * XXX KDM if we hold the lock here, we could cause a deadlock
13650 * if the frontend comes back in in this context to queue
13653 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER)
13654 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
13655 union ctl_ha_msg msg;
13657 memset(&msg, 0, sizeof(msg));
13658 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13659 msg.hdr.original_sc = io->io_hdr.original_sc;
13660 msg.hdr.nexus = io->io_hdr.nexus;
13661 msg.hdr.status = io->io_hdr.status;
13662 msg.scsi.scsi_status = io->scsiio.scsi_status;
13663 msg.scsi.tag_num = io->scsiio.tag_num;
13664 msg.scsi.tag_type = io->scsiio.tag_type;
13665 msg.scsi.sense_len = io->scsiio.sense_len;
13666 msg.scsi.sense_residual = io->scsiio.sense_residual;
13667 msg.scsi.residual = io->scsiio.residual;
13668 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
13669 sizeof(io->scsiio.sense_data));
13671 * We copy this whether or not this is an I/O-related
13672 * command. Otherwise, we'd have to go and check to see
13673 * whether it's a read/write command, and it really isn't
13676 memcpy(&msg.scsi.lbalen,
13677 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
13678 sizeof(msg.scsi.lbalen));
13680 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13681 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
13682 /* XXX do something here */
13691 return (CTL_RETVAL_COMPLETE);
13696 * Front end should call this if it doesn't do autosense. When the request
13697 * sense comes back in from the initiator, we'll dequeue this and send it.
13700 ctl_queue_sense(union ctl_io *io)
13702 struct ctl_lun *lun;
13703 struct ctl_softc *ctl_softc;
13704 uint32_t initidx, targ_lun;
13706 ctl_softc = control_softc;
13708 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13711 * LUN lookup will likely move to the ctl_work_thread() once we
13712 * have our new queueing infrastructure (that doesn't put things on
13713 * a per-LUN queue initially). That is so that we can handle
13714 * things like an INQUIRY to a LUN that we don't have enabled. We
13715 * can't deal with that right now.
13717 mtx_lock(&ctl_softc->ctl_lock);
13720 * If we don't have a LUN for this, just toss the sense
13723 targ_lun = io->io_hdr.nexus.targ_lun;
13724 targ_lun = ctl_map_lun(io->io_hdr.nexus.targ_port, targ_lun);
13725 if ((targ_lun < CTL_MAX_LUNS)
13726 && (ctl_softc->ctl_luns[targ_lun] != NULL))
13727 lun = ctl_softc->ctl_luns[targ_lun];
13731 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13733 mtx_lock(&lun->lun_lock);
13735 * Already have CA set for this LUN...toss the sense information.
13737 if (ctl_is_set(lun->have_ca, initidx)) {
13738 mtx_unlock(&lun->lun_lock);
13742 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data,
13743 ctl_min(sizeof(lun->pending_sense[initidx]),
13744 sizeof(io->scsiio.sense_data)));
13745 ctl_set_mask(lun->have_ca, initidx);
13746 mtx_unlock(&lun->lun_lock);
13749 mtx_unlock(&ctl_softc->ctl_lock);
13753 return (CTL_RETVAL_COMPLETE);
13758 * Primary command inlet from frontend ports. All SCSI and task I/O
13759 * requests must go through this function.
13762 ctl_queue(union ctl_io *io)
13764 struct ctl_softc *ctl_softc;
13766 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13768 ctl_softc = control_softc;
13771 io->io_hdr.start_time = time_uptime;
13772 getbintime(&io->io_hdr.start_bt);
13773 #endif /* CTL_TIME_IO */
13775 /* Map FE-specific LUN ID into global one. */
13776 io->io_hdr.nexus.targ_mapped_lun =
13777 ctl_map_lun(io->io_hdr.nexus.targ_port, io->io_hdr.nexus.targ_lun);
13779 switch (io->io_hdr.io_type) {
13782 if (ctl_debug & CTL_DEBUG_CDB)
13784 ctl_enqueue_incoming(io);
13787 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13791 return (CTL_RETVAL_COMPLETE);
13794 #ifdef CTL_IO_DELAY
13796 ctl_done_timer_wakeup(void *arg)
13800 io = (union ctl_io *)arg;
13803 #endif /* CTL_IO_DELAY */
13806 ctl_done(union ctl_io *io)
13808 struct ctl_softc *ctl_softc;
13810 ctl_softc = control_softc;
13813 * Enable this to catch duplicate completion issues.
13816 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13817 printf("%s: type %d msg %d cdb %x iptl: "
13818 "%d:%d:%d:%d tag 0x%04x "
13819 "flag %#x status %x\n",
13821 io->io_hdr.io_type,
13822 io->io_hdr.msg_type,
13824 io->io_hdr.nexus.initid.id,
13825 io->io_hdr.nexus.targ_port,
13826 io->io_hdr.nexus.targ_target.id,
13827 io->io_hdr.nexus.targ_lun,
13828 (io->io_hdr.io_type ==
13830 io->taskio.tag_num :
13831 io->scsiio.tag_num,
13833 io->io_hdr.status);
13835 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13839 * This is an internal copy of an I/O, and should not go through
13840 * the normal done processing logic.
13842 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13846 * We need to send a msg to the serializing shelf to finish the IO
13847 * as well. We don't send a finish message to the other shelf if
13848 * this is a task management command. Task management commands
13849 * aren't serialized in the OOA queue, but rather just executed on
13850 * both shelf controllers for commands that originated on that
13853 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)
13854 && (io->io_hdr.io_type != CTL_IO_TASK)) {
13855 union ctl_ha_msg msg_io;
13857 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO;
13858 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc;
13859 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io,
13860 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) {
13862 /* continue on to finish IO */
13864 #ifdef CTL_IO_DELAY
13865 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13866 struct ctl_lun *lun;
13868 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13870 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13872 struct ctl_lun *lun;
13874 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13877 && (lun->delay_info.done_delay > 0)) {
13878 struct callout *callout;
13880 callout = (struct callout *)&io->io_hdr.timer_bytes;
13881 callout_init(callout, /*mpsafe*/ 1);
13882 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13883 callout_reset(callout,
13884 lun->delay_info.done_delay * hz,
13885 ctl_done_timer_wakeup, io);
13886 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13887 lun->delay_info.done_delay = 0;
13891 #endif /* CTL_IO_DELAY */
13893 ctl_enqueue_done(io);
13897 ctl_isc(struct ctl_scsiio *ctsio)
13899 struct ctl_lun *lun;
13902 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13904 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0]));
13906 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n"));
13908 retval = lun->backend->data_submit((union ctl_io *)ctsio);
13915 ctl_work_thread(void *arg)
13917 struct ctl_thread *thr = (struct ctl_thread *)arg;
13918 struct ctl_softc *softc = thr->ctl_softc;
13922 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13928 * We handle the queues in this order:
13930 * - done queue (to free up resources, unblock other commands)
13934 * If those queues are empty, we break out of the loop and
13937 mtx_lock(&thr->queue_lock);
13938 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13940 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13941 mtx_unlock(&thr->queue_lock);
13942 ctl_handle_isc(io);
13945 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13947 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13948 /* clear any blocked commands, call fe_done */
13949 mtx_unlock(&thr->queue_lock);
13950 retval = ctl_process_done(io);
13953 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13955 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13956 mtx_unlock(&thr->queue_lock);
13957 if (io->io_hdr.io_type == CTL_IO_TASK)
13960 ctl_scsiio_precheck(softc, &io->scsiio);
13963 if (!ctl_pause_rtr) {
13964 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13966 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13967 mtx_unlock(&thr->queue_lock);
13968 retval = ctl_scsiio(&io->scsiio);
13969 if (retval != CTL_RETVAL_COMPLETE)
13970 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13975 /* Sleep until we have something to do. */
13976 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13981 ctl_lun_thread(void *arg)
13983 struct ctl_softc *softc = (struct ctl_softc *)arg;
13984 struct ctl_be_lun *be_lun;
13987 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13991 mtx_lock(&softc->ctl_lock);
13992 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13993 if (be_lun != NULL) {
13994 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13995 mtx_unlock(&softc->ctl_lock);
13996 ctl_create_lun(be_lun);
14000 /* Sleep until we have something to do. */
14001 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
14002 PDROP | PRIBIO, "-", 0);
14007 ctl_enqueue_incoming(union ctl_io *io)
14009 struct ctl_softc *softc = control_softc;
14010 struct ctl_thread *thr;
14013 idx = (io->io_hdr.nexus.targ_port * 127 +
14014 io->io_hdr.nexus.initid.id) % worker_threads;
14015 thr = &softc->threads[idx];
14016 mtx_lock(&thr->queue_lock);
14017 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
14018 mtx_unlock(&thr->queue_lock);
14023 ctl_enqueue_rtr(union ctl_io *io)
14025 struct ctl_softc *softc = control_softc;
14026 struct ctl_thread *thr;
14028 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14029 mtx_lock(&thr->queue_lock);
14030 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
14031 mtx_unlock(&thr->queue_lock);
14036 ctl_enqueue_done(union ctl_io *io)
14038 struct ctl_softc *softc = control_softc;
14039 struct ctl_thread *thr;
14041 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14042 mtx_lock(&thr->queue_lock);
14043 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
14044 mtx_unlock(&thr->queue_lock);
14049 ctl_enqueue_isc(union ctl_io *io)
14051 struct ctl_softc *softc = control_softc;
14052 struct ctl_thread *thr;
14054 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14055 mtx_lock(&thr->queue_lock);
14056 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
14057 mtx_unlock(&thr->queue_lock);
14061 /* Initialization and failover */
14064 ctl_init_isc_msg(void)
14066 printf("CTL: Still calling this thing\n");
14071 * Initializes component into configuration defined by bootMode
14073 * returns hasc_Status:
14075 * ERROR - fatal error
14077 static ctl_ha_comp_status
14078 ctl_isc_init(struct ctl_ha_component *c)
14080 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
14087 * Starts component in state requested. If component starts successfully,
14088 * it must set its own state to the requestrd state
14089 * When requested state is HASC_STATE_HA, the component may refine it
14090 * by adding _SLAVE or _MASTER flags.
14091 * Currently allowed state transitions are:
14092 * UNKNOWN->HA - initial startup
14093 * UNKNOWN->SINGLE - initial startup when no parter detected
14094 * HA->SINGLE - failover
14095 * returns ctl_ha_comp_status:
14096 * OK - component successfully started in requested state
14097 * FAILED - could not start the requested state, failover may
14099 * ERROR - fatal error detected, no future startup possible
14101 static ctl_ha_comp_status
14102 ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state)
14104 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
14106 printf("%s: go\n", __func__);
14108 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap)
14109 if (c->state == CTL_HA_STATE_UNKNOWN ) {
14111 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
14112 != CTL_HA_STATUS_SUCCESS) {
14113 printf("ctl_isc_start: ctl_ha_msg_create failed.\n");
14114 ret = CTL_HA_COMP_STATUS_ERROR;
14116 } else if (CTL_HA_STATE_IS_HA(c->state)
14117 && CTL_HA_STATE_IS_SINGLE(state)){
14118 // HA->SINGLE transition
14122 printf("ctl_isc_start:Invalid state transition %X->%X\n",
14124 ret = CTL_HA_COMP_STATUS_ERROR;
14126 if (CTL_HA_STATE_IS_SINGLE(state))
14135 * Quiesce component
14136 * The component must clear any error conditions (set status to OK) and
14137 * prepare itself to another Start call
14138 * returns ctl_ha_comp_status:
14142 static ctl_ha_comp_status
14143 ctl_isc_quiesce(struct ctl_ha_component *c)
14145 int ret = CTL_HA_COMP_STATUS_OK;
14152 struct ctl_ha_component ctl_ha_component_ctlisc =
14155 .state = CTL_HA_STATE_UNKNOWN,
14156 .init = ctl_isc_init,
14157 .start = ctl_isc_start,
14158 .quiesce = ctl_isc_quiesce