2 * Copyright (c) 2003-2009 Silicon Graphics International Corp.
3 * Copyright (c) 2012 The FreeBSD Foundation
6 * Portions of this software were developed by Edward Tomasz Napierala
7 * under sponsorship from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
16 * substantially similar to the "NO WARRANTY" disclaimer below
17 * ("Disclaimer") and any redistribution must be conditioned upon
18 * including a substantially similar Disclaimer requirement for further
19 * binary redistribution.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGES.
34 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl.c#8 $
37 * CAM Target Layer, a SCSI device emulation subsystem.
39 * Author: Ken Merry <ken@FreeBSD.org>
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/ctype.h>
50 #include <sys/kernel.h>
51 #include <sys/types.h>
52 #include <sys/kthread.h>
54 #include <sys/fcntl.h>
56 #include <sys/module.h>
57 #include <sys/mutex.h>
58 #include <sys/condvar.h>
59 #include <sys/malloc.h>
61 #include <sys/ioccom.h>
62 #include <sys/queue.h>
65 #include <sys/endian.h>
66 #include <sys/sysctl.h>
70 #include <cam/scsi/scsi_all.h>
71 #include <cam/scsi/scsi_da.h>
72 #include <cam/ctl/ctl_io.h>
73 #include <cam/ctl/ctl.h>
74 #include <cam/ctl/ctl_frontend.h>
75 #include <cam/ctl/ctl_frontend_internal.h>
76 #include <cam/ctl/ctl_util.h>
77 #include <cam/ctl/ctl_backend.h>
78 #include <cam/ctl/ctl_ioctl.h>
79 #include <cam/ctl/ctl_ha.h>
80 #include <cam/ctl/ctl_private.h>
81 #include <cam/ctl/ctl_debug.h>
82 #include <cam/ctl/ctl_scsi_all.h>
83 #include <cam/ctl/ctl_error.h>
85 struct ctl_softc *control_softc = NULL;
88 * Size and alignment macros needed for Copan-specific HA hardware. These
89 * can go away when the HA code is re-written, and uses busdma for any
92 #define CTL_ALIGN_8B(target, source, type) \
93 if (((uint32_t)source & 0x7) != 0) \
94 target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\
96 target = (type)source;
98 #define CTL_SIZE_8B(target, size) \
99 if ((size & 0x7) != 0) \
100 target = size + (0x8 - (size & 0x7)); \
104 #define CTL_ALIGN_8B_MARGIN 16
107 * Template mode pages.
111 * Note that these are default values only. The actual values will be
112 * filled in when the user does a mode sense.
114 const static struct copan_debugconf_subpage debugconf_page_default = {
115 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
116 DBGCNF_SUBPAGE_CODE, /* subpage */
117 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
118 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
119 DBGCNF_VERSION, /* page_version */
120 {CTL_TIME_IO_DEFAULT_SECS>>8,
121 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */
124 const static struct copan_debugconf_subpage debugconf_page_changeable = {
125 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
126 DBGCNF_SUBPAGE_CODE, /* subpage */
127 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
128 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
129 0, /* page_version */
130 {0xff,0xff}, /* ctl_time_io_secs */
133 const static struct scsi_da_rw_recovery_page rw_er_page_default = {
134 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
135 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
136 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE,
137 /*read_retry_count*/0,
138 /*correction_span*/0,
139 /*head_offset_count*/0,
140 /*data_strobe_offset_cnt*/0,
141 /*byte8*/SMS_RWER_LBPERE,
142 /*write_retry_count*/0,
144 /*recovery_time_limit*/{0, 0},
147 const static struct scsi_da_rw_recovery_page rw_er_page_changeable = {
148 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
149 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
151 /*read_retry_count*/0,
152 /*correction_span*/0,
153 /*head_offset_count*/0,
154 /*data_strobe_offset_cnt*/0,
156 /*write_retry_count*/0,
158 /*recovery_time_limit*/{0, 0},
161 const static struct scsi_format_page format_page_default = {
162 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
163 /*page_length*/sizeof(struct scsi_format_page) - 2,
164 /*tracks_per_zone*/ {0, 0},
165 /*alt_sectors_per_zone*/ {0, 0},
166 /*alt_tracks_per_zone*/ {0, 0},
167 /*alt_tracks_per_lun*/ {0, 0},
168 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
169 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
170 /*bytes_per_sector*/ {0, 0},
171 /*interleave*/ {0, 0},
172 /*track_skew*/ {0, 0},
173 /*cylinder_skew*/ {0, 0},
175 /*reserved*/ {0, 0, 0}
178 const static struct scsi_format_page format_page_changeable = {
179 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
180 /*page_length*/sizeof(struct scsi_format_page) - 2,
181 /*tracks_per_zone*/ {0, 0},
182 /*alt_sectors_per_zone*/ {0, 0},
183 /*alt_tracks_per_zone*/ {0, 0},
184 /*alt_tracks_per_lun*/ {0, 0},
185 /*sectors_per_track*/ {0, 0},
186 /*bytes_per_sector*/ {0, 0},
187 /*interleave*/ {0, 0},
188 /*track_skew*/ {0, 0},
189 /*cylinder_skew*/ {0, 0},
191 /*reserved*/ {0, 0, 0}
194 const static struct scsi_rigid_disk_page rigid_disk_page_default = {
195 /*page_code*/SMS_RIGID_DISK_PAGE,
196 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
197 /*cylinders*/ {0, 0, 0},
198 /*heads*/ CTL_DEFAULT_HEADS,
199 /*start_write_precomp*/ {0, 0, 0},
200 /*start_reduced_current*/ {0, 0, 0},
201 /*step_rate*/ {0, 0},
202 /*landing_zone_cylinder*/ {0, 0, 0},
203 /*rpl*/ SRDP_RPL_DISABLED,
204 /*rotational_offset*/ 0,
206 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
207 CTL_DEFAULT_ROTATION_RATE & 0xff},
211 const static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
212 /*page_code*/SMS_RIGID_DISK_PAGE,
213 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
214 /*cylinders*/ {0, 0, 0},
216 /*start_write_precomp*/ {0, 0, 0},
217 /*start_reduced_current*/ {0, 0, 0},
218 /*step_rate*/ {0, 0},
219 /*landing_zone_cylinder*/ {0, 0, 0},
221 /*rotational_offset*/ 0,
223 /*rotation_rate*/ {0, 0},
227 const static struct scsi_caching_page caching_page_default = {
228 /*page_code*/SMS_CACHING_PAGE,
229 /*page_length*/sizeof(struct scsi_caching_page) - 2,
230 /*flags1*/ SCP_DISC | SCP_WCE,
232 /*disable_pf_transfer_len*/ {0xff, 0xff},
233 /*min_prefetch*/ {0, 0},
234 /*max_prefetch*/ {0xff, 0xff},
235 /*max_pf_ceiling*/ {0xff, 0xff},
237 /*cache_segments*/ 0,
238 /*cache_seg_size*/ {0, 0},
240 /*non_cache_seg_size*/ {0, 0, 0}
243 const static struct scsi_caching_page caching_page_changeable = {
244 /*page_code*/SMS_CACHING_PAGE,
245 /*page_length*/sizeof(struct scsi_caching_page) - 2,
246 /*flags1*/ SCP_WCE | SCP_RCD,
248 /*disable_pf_transfer_len*/ {0, 0},
249 /*min_prefetch*/ {0, 0},
250 /*max_prefetch*/ {0, 0},
251 /*max_pf_ceiling*/ {0, 0},
253 /*cache_segments*/ 0,
254 /*cache_seg_size*/ {0, 0},
256 /*non_cache_seg_size*/ {0, 0, 0}
259 const static struct scsi_control_page control_page_default = {
260 /*page_code*/SMS_CONTROL_MODE_PAGE,
261 /*page_length*/sizeof(struct scsi_control_page) - 2,
263 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED,
266 /*aen_holdoff_period*/{0, 0},
267 /*busy_timeout_period*/{0, 0},
268 /*extended_selftest_completion_time*/{0, 0}
271 const static struct scsi_control_page control_page_changeable = {
272 /*page_code*/SMS_CONTROL_MODE_PAGE,
273 /*page_length*/sizeof(struct scsi_control_page) - 2,
275 /*queue_flags*/SCP_QUEUE_ALG_MASK,
276 /*eca_and_aen*/SCP_SWP,
278 /*aen_holdoff_period*/{0, 0},
279 /*busy_timeout_period*/{0, 0},
280 /*extended_selftest_completion_time*/{0, 0}
283 const static struct scsi_info_exceptions_page ie_page_default = {
284 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
285 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
286 /*info_flags*/SIEP_FLAGS_DEXCPT,
288 /*interval_timer*/{0, 0, 0, 0},
289 /*report_count*/{0, 0, 0, 0}
292 const static struct scsi_info_exceptions_page ie_page_changeable = {
293 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
294 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
297 /*interval_timer*/{0, 0, 0, 0},
298 /*report_count*/{0, 0, 0, 0}
301 #define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4)
303 const static struct ctl_logical_block_provisioning_page lbp_page_default = {{
304 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
305 /*subpage_code*/0x02,
306 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
308 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
313 /*count*/{0, 0, 0, 0}},
317 /*count*/{0, 0, 0, 0}},
321 /*count*/{0, 0, 0, 0}},
325 /*count*/{0, 0, 0, 0}}
329 const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{
330 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
331 /*subpage_code*/0x02,
332 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
334 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
339 /*count*/{0, 0, 0, 0}},
343 /*count*/{0, 0, 0, 0}},
347 /*count*/{0, 0, 0, 0}},
351 /*count*/{0, 0, 0, 0}}
356 * XXX KDM move these into the softc.
358 static int rcv_sync_msg;
359 static uint8_t ctl_pause_rtr;
361 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
362 static int worker_threads = -1;
363 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
364 &worker_threads, 1, "Number of worker threads");
365 static int ctl_debug = CTL_DEBUG_NONE;
366 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN,
367 &ctl_debug, 0, "Enabled debug flags");
370 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
371 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
372 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0),
373 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
375 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 10
377 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
379 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
380 static int ctl_init(void);
381 void ctl_shutdown(void);
382 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
383 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
384 static void ctl_ioctl_online(void *arg);
385 static void ctl_ioctl_offline(void *arg);
386 static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id);
387 static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id);
388 static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio);
389 static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
390 static int ctl_ioctl_submit_wait(union ctl_io *io);
391 static void ctl_ioctl_datamove(union ctl_io *io);
392 static void ctl_ioctl_done(union ctl_io *io);
393 static void ctl_ioctl_hard_startstop_callback(void *arg,
394 struct cfi_metatask *metatask);
395 static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask);
396 static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
397 struct ctl_ooa *ooa_hdr,
398 struct ctl_ooa_entry *kern_entries);
399 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
401 static uint32_t ctl_map_lun(struct ctl_softc *softc, int port_num, uint32_t lun);
402 static uint32_t ctl_map_lun_back(struct ctl_softc *softc, int port_num, uint32_t lun);
403 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
404 struct ctl_be_lun *be_lun, struct ctl_id target_id);
405 static int ctl_free_lun(struct ctl_lun *lun);
406 static void ctl_create_lun(struct ctl_be_lun *be_lun);
408 static void ctl_failover_change_pages(struct ctl_softc *softc,
409 struct ctl_scsiio *ctsio, int master);
412 static int ctl_do_mode_select(union ctl_io *io);
413 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
414 uint64_t res_key, uint64_t sa_res_key,
415 uint8_t type, uint32_t residx,
416 struct ctl_scsiio *ctsio,
417 struct scsi_per_res_out *cdb,
418 struct scsi_per_res_out_parms* param);
419 static void ctl_pro_preempt_other(struct ctl_lun *lun,
420 union ctl_ha_msg *msg);
421 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
422 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
423 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
424 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
425 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
426 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
427 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
429 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
431 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
432 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
433 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
434 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
435 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
436 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2,
438 static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2);
439 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
440 union ctl_io *pending_io, union ctl_io *ooa_io);
441 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
442 union ctl_io *starting_io);
443 static int ctl_check_blocked(struct ctl_lun *lun);
444 static int ctl_scsiio_lun_check(struct ctl_lun *lun,
445 const struct ctl_cmd_entry *entry,
446 struct ctl_scsiio *ctsio);
447 //static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc);
448 static void ctl_failover(void);
449 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
450 struct ctl_scsiio *ctsio);
451 static int ctl_scsiio(struct ctl_scsiio *ctsio);
453 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
454 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
455 ctl_ua_type ua_type);
456 static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io,
457 ctl_ua_type ua_type);
458 static int ctl_abort_task(union ctl_io *io);
459 static int ctl_abort_task_set(union ctl_io *io);
460 static int ctl_i_t_nexus_reset(union ctl_io *io);
461 static void ctl_run_task(union ctl_io *io);
463 static void ctl_datamove_timer_wakeup(void *arg);
464 static void ctl_done_timer_wakeup(void *arg);
465 #endif /* CTL_IO_DELAY */
467 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
468 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
469 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
470 static void ctl_datamove_remote_write(union ctl_io *io);
471 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
472 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
473 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
474 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
475 ctl_ha_dt_cb callback);
476 static void ctl_datamove_remote_read(union ctl_io *io);
477 static void ctl_datamove_remote(union ctl_io *io);
478 static int ctl_process_done(union ctl_io *io);
479 static void ctl_lun_thread(void *arg);
480 static void ctl_thresh_thread(void *arg);
481 static void ctl_work_thread(void *arg);
482 static void ctl_enqueue_incoming(union ctl_io *io);
483 static void ctl_enqueue_rtr(union ctl_io *io);
484 static void ctl_enqueue_done(union ctl_io *io);
485 static void ctl_enqueue_isc(union ctl_io *io);
486 static const struct ctl_cmd_entry *
487 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
488 static const struct ctl_cmd_entry *
489 ctl_validate_command(struct ctl_scsiio *ctsio);
490 static int ctl_cmd_applicable(uint8_t lun_type,
491 const struct ctl_cmd_entry *entry);
494 * Load the serialization table. This isn't very pretty, but is probably
495 * the easiest way to do it.
497 #include "ctl_ser_table.c"
500 * We only need to define open, close and ioctl routines for this driver.
502 static struct cdevsw ctl_cdevsw = {
503 .d_version = D_VERSION,
506 .d_close = ctl_close,
507 .d_ioctl = ctl_ioctl,
512 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
513 MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests");
515 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
517 static moduledata_t ctl_moduledata = {
519 ctl_module_event_handler,
523 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
524 MODULE_VERSION(ctl, 1);
526 static struct ctl_frontend ioctl_frontend =
532 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
533 union ctl_ha_msg *msg_info)
535 struct ctl_scsiio *ctsio;
537 if (msg_info->hdr.original_sc == NULL) {
538 printf("%s: original_sc == NULL!\n", __func__);
539 /* XXX KDM now what? */
543 ctsio = &msg_info->hdr.original_sc->scsiio;
544 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
545 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
546 ctsio->io_hdr.status = msg_info->hdr.status;
547 ctsio->scsi_status = msg_info->scsi.scsi_status;
548 ctsio->sense_len = msg_info->scsi.sense_len;
549 ctsio->sense_residual = msg_info->scsi.sense_residual;
550 ctsio->residual = msg_info->scsi.residual;
551 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
552 sizeof(ctsio->sense_data));
553 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
554 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen));
555 ctl_enqueue_isc((union ctl_io *)ctsio);
559 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
560 union ctl_ha_msg *msg_info)
562 struct ctl_scsiio *ctsio;
564 if (msg_info->hdr.serializing_sc == NULL) {
565 printf("%s: serializing_sc == NULL!\n", __func__);
566 /* XXX KDM now what? */
570 ctsio = &msg_info->hdr.serializing_sc->scsiio;
573 * Attempt to catch the situation where an I/O has
574 * been freed, and we're using it again.
576 if (ctsio->io_hdr.io_type == 0xff) {
577 union ctl_io *tmp_io;
578 tmp_io = (union ctl_io *)ctsio;
579 printf("%s: %p use after free!\n", __func__,
581 printf("%s: type %d msg %d cdb %x iptl: "
582 "%d:%d:%d:%d tag 0x%04x "
583 "flag %#x status %x\n",
585 tmp_io->io_hdr.io_type,
586 tmp_io->io_hdr.msg_type,
587 tmp_io->scsiio.cdb[0],
588 tmp_io->io_hdr.nexus.initid.id,
589 tmp_io->io_hdr.nexus.targ_port,
590 tmp_io->io_hdr.nexus.targ_target.id,
591 tmp_io->io_hdr.nexus.targ_lun,
592 (tmp_io->io_hdr.io_type ==
594 tmp_io->taskio.tag_num :
595 tmp_io->scsiio.tag_num,
596 tmp_io->io_hdr.flags,
597 tmp_io->io_hdr.status);
600 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
601 ctl_enqueue_isc((union ctl_io *)ctsio);
605 * ISC (Inter Shelf Communication) event handler. Events from the HA
606 * subsystem come in here.
609 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
611 struct ctl_softc *softc;
613 struct ctl_prio *presio;
614 ctl_ha_status isc_status;
616 softc = control_softc;
621 printf("CTL: Isc Msg event %d\n", event);
623 if (event == CTL_HA_EVT_MSG_RECV) {
624 union ctl_ha_msg msg_info;
626 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
627 sizeof(msg_info), /*wait*/ 0);
629 printf("CTL: msg_type %d\n", msg_info.msg_type);
631 if (isc_status != 0) {
632 printf("Error receiving message, status = %d\n",
637 switch (msg_info.hdr.msg_type) {
638 case CTL_MSG_SERIALIZE:
640 printf("Serialize\n");
642 io = ctl_alloc_io_nowait(softc->othersc_pool);
644 printf("ctl_isc_event_handler: can't allocate "
647 /* Need to set busy and send msg back */
648 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
649 msg_info.hdr.status = CTL_SCSI_ERROR;
650 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY;
651 msg_info.scsi.sense_len = 0;
652 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
653 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){
658 // populate ctsio from msg_info
659 io->io_hdr.io_type = CTL_IO_SCSI;
660 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
661 io->io_hdr.original_sc = msg_info.hdr.original_sc;
663 printf("pOrig %x\n", (int)msg_info.original_sc);
665 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
668 * If we're in serialization-only mode, we don't
669 * want to go through full done processing. Thus
672 * XXX KDM add another flag that is more specific.
674 if (softc->ha_mode == CTL_HA_MODE_SER_ONLY)
675 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
676 io->io_hdr.nexus = msg_info.hdr.nexus;
678 printf("targ %d, port %d, iid %d, lun %d\n",
679 io->io_hdr.nexus.targ_target.id,
680 io->io_hdr.nexus.targ_port,
681 io->io_hdr.nexus.initid.id,
682 io->io_hdr.nexus.targ_lun);
684 io->scsiio.tag_num = msg_info.scsi.tag_num;
685 io->scsiio.tag_type = msg_info.scsi.tag_type;
686 memcpy(io->scsiio.cdb, msg_info.scsi.cdb,
688 if (softc->ha_mode == CTL_HA_MODE_XFER) {
689 const struct ctl_cmd_entry *entry;
691 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
692 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
694 entry->flags & CTL_FLAG_DATA_MASK;
699 /* Performed on the Originating SC, XFER mode only */
700 case CTL_MSG_DATAMOVE: {
701 struct ctl_sg_entry *sgl;
704 io = msg_info.hdr.original_sc;
706 printf("%s: original_sc == NULL!\n", __func__);
707 /* XXX KDM do something here */
710 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
711 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
713 * Keep track of this, we need to send it back over
714 * when the datamove is complete.
716 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
718 if (msg_info.dt.sg_sequence == 0) {
720 * XXX KDM we use the preallocated S/G list
721 * here, but we'll need to change this to
722 * dynamic allocation if we need larger S/G
725 if (msg_info.dt.kern_sg_entries >
726 sizeof(io->io_hdr.remote_sglist) /
727 sizeof(io->io_hdr.remote_sglist[0])) {
728 printf("%s: number of S/G entries "
729 "needed %u > allocated num %zd\n",
731 msg_info.dt.kern_sg_entries,
732 sizeof(io->io_hdr.remote_sglist)/
733 sizeof(io->io_hdr.remote_sglist[0]));
736 * XXX KDM send a message back to
737 * the other side to shut down the
738 * DMA. The error will come back
739 * through via the normal channel.
743 sgl = io->io_hdr.remote_sglist;
745 sizeof(io->io_hdr.remote_sglist));
747 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
749 io->scsiio.kern_sg_entries =
750 msg_info.dt.kern_sg_entries;
751 io->scsiio.rem_sg_entries =
752 msg_info.dt.kern_sg_entries;
753 io->scsiio.kern_data_len =
754 msg_info.dt.kern_data_len;
755 io->scsiio.kern_total_len =
756 msg_info.dt.kern_total_len;
757 io->scsiio.kern_data_resid =
758 msg_info.dt.kern_data_resid;
759 io->scsiio.kern_rel_offset =
760 msg_info.dt.kern_rel_offset;
762 * Clear out per-DMA flags.
764 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK;
766 * Add per-DMA flags that are set for this
767 * particular DMA request.
769 io->io_hdr.flags |= msg_info.dt.flags &
772 sgl = (struct ctl_sg_entry *)
773 io->scsiio.kern_data_ptr;
775 for (i = msg_info.dt.sent_sg_entries, j = 0;
776 i < (msg_info.dt.sent_sg_entries +
777 msg_info.dt.cur_sg_entries); i++, j++) {
778 sgl[i].addr = msg_info.dt.sg_list[j].addr;
779 sgl[i].len = msg_info.dt.sg_list[j].len;
782 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n",
784 msg_info.dt.sg_list[j].addr,
785 msg_info.dt.sg_list[j].len,
786 sgl[i].addr, sgl[i].len, j, i);
790 memcpy(&sgl[msg_info.dt.sent_sg_entries],
792 sizeof(*sgl) * msg_info.dt.cur_sg_entries);
796 * If this is the last piece of the I/O, we've got
797 * the full S/G list. Queue processing in the thread.
798 * Otherwise wait for the next piece.
800 if (msg_info.dt.sg_last != 0)
804 /* Performed on the Serializing (primary) SC, XFER mode only */
805 case CTL_MSG_DATAMOVE_DONE: {
806 if (msg_info.hdr.serializing_sc == NULL) {
807 printf("%s: serializing_sc == NULL!\n",
809 /* XXX KDM now what? */
813 * We grab the sense information here in case
814 * there was a failure, so we can return status
815 * back to the initiator.
817 io = msg_info.hdr.serializing_sc;
818 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
819 io->io_hdr.status = msg_info.hdr.status;
820 io->scsiio.scsi_status = msg_info.scsi.scsi_status;
821 io->scsiio.sense_len = msg_info.scsi.sense_len;
822 io->scsiio.sense_residual =msg_info.scsi.sense_residual;
823 io->io_hdr.port_status = msg_info.scsi.fetd_status;
824 io->scsiio.residual = msg_info.scsi.residual;
825 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data,
826 sizeof(io->scsiio.sense_data));
831 /* Preformed on Originating SC, SER_ONLY mode */
833 io = msg_info.hdr.original_sc;
835 printf("%s: Major Bummer\n", __func__);
839 printf("pOrig %x\n",(int) ctsio);
842 io->io_hdr.msg_type = CTL_MSG_R2R;
843 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
848 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
850 * Performed on the Originating (i.e. secondary) SC in XFER
853 case CTL_MSG_FINISH_IO:
854 if (softc->ha_mode == CTL_HA_MODE_XFER)
855 ctl_isc_handler_finish_xfer(softc,
858 ctl_isc_handler_finish_ser_only(softc,
862 /* Preformed on Originating SC */
863 case CTL_MSG_BAD_JUJU:
864 io = msg_info.hdr.original_sc;
866 printf("%s: Bad JUJU!, original_sc is NULL!\n",
870 ctl_copy_sense_data(&msg_info, io);
872 * IO should have already been cleaned up on other
873 * SC so clear this flag so we won't send a message
874 * back to finish the IO there.
876 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
877 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
879 /* io = msg_info.hdr.serializing_sc; */
880 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
884 /* Handle resets sent from the other side */
885 case CTL_MSG_MANAGE_TASKS: {
886 struct ctl_taskio *taskio;
887 taskio = (struct ctl_taskio *)ctl_alloc_io_nowait(
888 softc->othersc_pool);
889 if (taskio == NULL) {
890 printf("ctl_isc_event_handler: can't allocate "
893 /* should I just call the proper reset func
897 ctl_zero_io((union ctl_io *)taskio);
898 taskio->io_hdr.io_type = CTL_IO_TASK;
899 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
900 taskio->io_hdr.nexus = msg_info.hdr.nexus;
901 taskio->task_action = msg_info.task.task_action;
902 taskio->tag_num = msg_info.task.tag_num;
903 taskio->tag_type = msg_info.task.tag_type;
905 taskio->io_hdr.start_time = time_uptime;
906 getbintime(&taskio->io_hdr.start_bt);
908 cs_prof_gettime(&taskio->io_hdr.start_ticks);
910 #endif /* CTL_TIME_IO */
911 ctl_run_task((union ctl_io *)taskio);
914 /* Persistent Reserve action which needs attention */
915 case CTL_MSG_PERS_ACTION:
916 presio = (struct ctl_prio *)ctl_alloc_io_nowait(
917 softc->othersc_pool);
918 if (presio == NULL) {
919 printf("ctl_isc_event_handler: can't allocate "
922 /* Need to set busy and send msg back */
925 ctl_zero_io((union ctl_io *)presio);
926 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
927 presio->pr_msg = msg_info.pr;
928 ctl_enqueue_isc((union ctl_io *)presio);
930 case CTL_MSG_SYNC_FE:
934 printf("How did I get here?\n");
936 } else if (event == CTL_HA_EVT_MSG_SENT) {
937 if (param != CTL_HA_STATUS_SUCCESS) {
938 printf("Bad status from ctl_ha_msg_send status %d\n",
942 } else if (event == CTL_HA_EVT_DISCONNECT) {
943 printf("CTL: Got a disconnect from Isc\n");
946 printf("ctl_isc_event_handler: Unknown event %d\n", event);
955 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
957 struct scsi_sense_data *sense;
959 sense = &dest->scsiio.sense_data;
960 bcopy(&src->scsi.sense_data, sense, sizeof(*sense));
961 dest->scsiio.scsi_status = src->scsi.scsi_status;
962 dest->scsiio.sense_len = src->scsi.sense_len;
963 dest->io_hdr.status = src->hdr.status;
967 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
971 mtx_assert(&lun->lun_lock, MA_OWNED);
972 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
975 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua;
979 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
983 mtx_assert(&lun->lun_lock, MA_OWNED);
984 for (i = 0; i < CTL_MAX_PORTS; i++) {
985 if (lun->pending_ua[i] == NULL)
987 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
988 if (i * CTL_MAX_INIT_PER_PORT + j == except)
990 lun->pending_ua[i][j] |= ua;
996 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1000 mtx_assert(&lun->lun_lock, MA_OWNED);
1001 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1004 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua;
1008 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1012 mtx_assert(&lun->lun_lock, MA_OWNED);
1013 for (i = 0; i < CTL_MAX_PORTS; i++) {
1014 if (lun->pending_ua[i] == NULL)
1016 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
1017 if (i * CTL_MAX_INIT_PER_PORT + j == except)
1019 lun->pending_ua[i][j] &= ~ua;
1025 ctl_ha_state_sysctl(SYSCTL_HANDLER_ARGS)
1027 struct ctl_softc *softc = (struct ctl_softc *)arg1;
1028 struct ctl_lun *lun;
1031 if (softc->flags & CTL_FLAG_ACTIVE_SHELF)
1036 error = sysctl_handle_int(oidp, &value, 0, req);
1037 if ((error != 0) || (req->newptr == NULL))
1040 mtx_lock(&softc->ctl_lock);
1042 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1044 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
1045 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1046 mtx_lock(&lun->lun_lock);
1047 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1048 mtx_unlock(&lun->lun_lock);
1050 mtx_unlock(&softc->ctl_lock);
1057 struct ctl_softc *softc;
1059 struct ctl_port *port;
1060 int i, error, retval;
1067 control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1069 softc = control_softc;
1071 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
1074 softc->dev->si_drv1 = softc;
1077 * By default, return a "bad LUN" peripheral qualifier for unknown
1078 * LUNs. The user can override this default using the tunable or
1079 * sysctl. See the comment in ctl_inquiry_std() for more details.
1081 softc->inquiry_pq_no_lun = 1;
1082 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun",
1083 &softc->inquiry_pq_no_lun);
1084 sysctl_ctx_init(&softc->sysctl_ctx);
1085 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1086 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1087 CTLFLAG_RD, 0, "CAM Target Layer");
1089 if (softc->sysctl_tree == NULL) {
1090 printf("%s: unable to allocate sysctl tree\n", __func__);
1091 destroy_dev(softc->dev);
1092 free(control_softc, M_DEVBUF);
1093 control_softc = NULL;
1097 SYSCTL_ADD_INT(&softc->sysctl_ctx,
1098 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
1099 "inquiry_pq_no_lun", CTLFLAG_RW,
1100 &softc->inquiry_pq_no_lun, 0,
1101 "Report no lun possible for invalid LUNs");
1103 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1104 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
1105 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1106 softc->open_count = 0;
1109 * Default to actually sending a SYNCHRONIZE CACHE command down to
1112 softc->flags = CTL_FLAG_REAL_SYNC;
1115 * In Copan's HA scheme, the "master" and "slave" roles are
1116 * figured out through the slot the controller is in. Although it
1117 * is an active/active system, someone has to be in charge.
1119 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1120 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1121 "HA head ID (0 - no HA)");
1122 if (softc->ha_id == 0) {
1123 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1124 softc->is_single = 1;
1125 softc->port_offset = 0;
1127 softc->port_offset = (softc->ha_id - 1) * CTL_MAX_PORTS;
1128 softc->persis_offset = softc->port_offset * CTL_MAX_INIT_PER_PORT;
1131 * XXX KDM need to figure out where we want to get our target ID
1132 * and WWID. Is it different on each port?
1134 softc->target.id = 0;
1135 softc->target.wwid[0] = 0x12345678;
1136 softc->target.wwid[1] = 0x87654321;
1137 STAILQ_INIT(&softc->lun_list);
1138 STAILQ_INIT(&softc->pending_lun_queue);
1139 STAILQ_INIT(&softc->fe_list);
1140 STAILQ_INIT(&softc->port_list);
1141 STAILQ_INIT(&softc->be_list);
1142 ctl_tpc_init(softc);
1144 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
1147 printf("ctl: can't allocate %d entry other SC pool, "
1148 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1151 softc->othersc_pool = other_pool;
1153 if (worker_threads <= 0)
1154 worker_threads = max(1, mp_ncpus / 4);
1155 if (worker_threads > CTL_MAX_THREADS)
1156 worker_threads = CTL_MAX_THREADS;
1158 for (i = 0; i < worker_threads; i++) {
1159 struct ctl_thread *thr = &softc->threads[i];
1161 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1162 thr->ctl_softc = softc;
1163 STAILQ_INIT(&thr->incoming_queue);
1164 STAILQ_INIT(&thr->rtr_queue);
1165 STAILQ_INIT(&thr->done_queue);
1166 STAILQ_INIT(&thr->isc_queue);
1168 error = kproc_kthread_add(ctl_work_thread, thr,
1169 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1171 printf("error creating CTL work thread!\n");
1172 ctl_pool_free(other_pool);
1176 error = kproc_kthread_add(ctl_lun_thread, softc,
1177 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
1179 printf("error creating CTL lun thread!\n");
1180 ctl_pool_free(other_pool);
1183 error = kproc_kthread_add(ctl_thresh_thread, softc,
1184 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh");
1186 printf("error creating CTL threshold thread!\n");
1187 ctl_pool_free(other_pool);
1191 printf("ctl: CAM Target Layer loaded\n");
1194 * Initialize the ioctl front end.
1196 ctl_frontend_register(&ioctl_frontend);
1197 port = &softc->ioctl_info.port;
1198 port->frontend = &ioctl_frontend;
1199 sprintf(softc->ioctl_info.port_name, "ioctl");
1200 port->port_type = CTL_PORT_IOCTL;
1201 port->num_requested_ctl_io = 100;
1202 port->port_name = softc->ioctl_info.port_name;
1203 port->port_online = ctl_ioctl_online;
1204 port->port_offline = ctl_ioctl_offline;
1205 port->onoff_arg = &softc->ioctl_info;
1206 port->lun_enable = ctl_ioctl_lun_enable;
1207 port->lun_disable = ctl_ioctl_lun_disable;
1208 port->targ_lun_arg = &softc->ioctl_info;
1209 port->fe_datamove = ctl_ioctl_datamove;
1210 port->fe_done = ctl_ioctl_done;
1211 port->max_targets = 15;
1212 port->max_target_id = 15;
1214 if (ctl_port_register(&softc->ioctl_info.port) != 0) {
1215 printf("ctl: ioctl front end registration failed, will "
1216 "continue anyway\n");
1219 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1220 OID_AUTO, "ha_state", CTLTYPE_INT | CTLFLAG_RWTUN,
1221 softc, 0, ctl_ha_state_sysctl, "I", "HA state for this head");
1224 if (sizeof(struct callout) > CTL_TIMER_BYTES) {
1225 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n",
1226 sizeof(struct callout), CTL_TIMER_BYTES);
1229 #endif /* CTL_IO_DELAY */
1237 struct ctl_softc *softc;
1238 struct ctl_lun *lun, *next_lun;
1240 softc = (struct ctl_softc *)control_softc;
1242 if (ctl_port_deregister(&softc->ioctl_info.port) != 0)
1243 printf("ctl: ioctl front end deregistration failed\n");
1245 mtx_lock(&softc->ctl_lock);
1250 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
1251 next_lun = STAILQ_NEXT(lun, links);
1255 mtx_unlock(&softc->ctl_lock);
1257 ctl_frontend_deregister(&ioctl_frontend);
1260 ctl_shutdown_thread(softc->work_thread);
1261 mtx_destroy(&softc->queue_lock);
1264 ctl_tpc_shutdown(softc);
1265 uma_zdestroy(softc->io_zone);
1266 mtx_destroy(&softc->ctl_lock);
1268 destroy_dev(softc->dev);
1270 sysctl_ctx_free(&softc->sysctl_ctx);
1272 free(control_softc, M_DEVBUF);
1273 control_softc = NULL;
1276 printf("ctl: CAM Target Layer unloaded\n");
1280 ctl_module_event_handler(module_t mod, int what, void *arg)
1285 return (ctl_init());
1289 return (EOPNOTSUPP);
1294 * XXX KDM should we do some access checks here? Bump a reference count to
1295 * prevent a CTL module from being unloaded while someone has it open?
1298 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1304 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1310 ctl_port_enable(ctl_port_type port_type)
1312 struct ctl_softc *softc = control_softc;
1313 struct ctl_port *port;
1315 if (softc->is_single == 0) {
1316 union ctl_ha_msg msg_info;
1320 printf("%s: HA mode, synchronizing frontend enable\n",
1323 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE;
1324 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1325 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) {
1326 printf("Sync msg send error retval %d\n", isc_retval);
1328 if (!rcv_sync_msg) {
1329 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
1330 sizeof(msg_info), 1);
1333 printf("CTL:Frontend Enable\n");
1335 printf("%s: single mode, skipping frontend synchronization\n",
1340 STAILQ_FOREACH(port, &softc->port_list, links) {
1341 if (port_type & port->port_type)
1344 printf("port %d\n", port->targ_port);
1346 ctl_port_online(port);
1354 ctl_port_disable(ctl_port_type port_type)
1356 struct ctl_softc *softc;
1357 struct ctl_port *port;
1359 softc = control_softc;
1361 STAILQ_FOREACH(port, &softc->port_list, links) {
1362 if (port_type & port->port_type)
1363 ctl_port_offline(port);
1370 * Returns 0 for success, 1 for failure.
1371 * Currently the only failure mode is if there aren't enough entries
1372 * allocated. So, in case of a failure, look at num_entries_dropped,
1373 * reallocate and try again.
1376 ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced,
1377 int *num_entries_filled, int *num_entries_dropped,
1378 ctl_port_type port_type, int no_virtual)
1380 struct ctl_softc *softc;
1381 struct ctl_port *port;
1382 int entries_dropped, entries_filled;
1386 softc = control_softc;
1390 entries_dropped = 0;
1393 mtx_lock(&softc->ctl_lock);
1394 STAILQ_FOREACH(port, &softc->port_list, links) {
1395 struct ctl_port_entry *entry;
1397 if ((port->port_type & port_type) == 0)
1400 if ((no_virtual != 0)
1401 && (port->virtual_port != 0))
1404 if (entries_filled >= num_entries_alloced) {
1408 entry = &entries[i];
1410 entry->port_type = port->port_type;
1411 strlcpy(entry->port_name, port->port_name,
1412 sizeof(entry->port_name));
1413 entry->physical_port = port->physical_port;
1414 entry->virtual_port = port->virtual_port;
1415 entry->wwnn = port->wwnn;
1416 entry->wwpn = port->wwpn;
1422 mtx_unlock(&softc->ctl_lock);
1424 if (entries_dropped > 0)
1427 *num_entries_dropped = entries_dropped;
1428 *num_entries_filled = entries_filled;
1434 ctl_ioctl_online(void *arg)
1436 struct ctl_ioctl_info *ioctl_info;
1438 ioctl_info = (struct ctl_ioctl_info *)arg;
1440 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED;
1444 ctl_ioctl_offline(void *arg)
1446 struct ctl_ioctl_info *ioctl_info;
1448 ioctl_info = (struct ctl_ioctl_info *)arg;
1450 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED;
1454 * Remove an initiator by port number and initiator ID.
1455 * Returns 0 for success, -1 for failure.
1458 ctl_remove_initiator(struct ctl_port *port, int iid)
1460 struct ctl_softc *softc = control_softc;
1462 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1464 if (iid > CTL_MAX_INIT_PER_PORT) {
1465 printf("%s: initiator ID %u > maximun %u!\n",
1466 __func__, iid, CTL_MAX_INIT_PER_PORT);
1470 mtx_lock(&softc->ctl_lock);
1471 port->wwpn_iid[iid].in_use--;
1472 port->wwpn_iid[iid].last_use = time_uptime;
1473 mtx_unlock(&softc->ctl_lock);
1479 * Add an initiator to the initiator map.
1480 * Returns iid for success, < 0 for failure.
1483 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
1485 struct ctl_softc *softc = control_softc;
1489 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1491 if (iid >= CTL_MAX_INIT_PER_PORT) {
1492 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
1493 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
1498 mtx_lock(&softc->ctl_lock);
1500 if (iid < 0 && (wwpn != 0 || name != NULL)) {
1501 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1502 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
1506 if (name != NULL && port->wwpn_iid[i].name != NULL &&
1507 strcmp(name, port->wwpn_iid[i].name) == 0) {
1515 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1516 if (port->wwpn_iid[i].in_use == 0 &&
1517 port->wwpn_iid[i].wwpn == 0 &&
1518 port->wwpn_iid[i].name == NULL) {
1527 best_time = INT32_MAX;
1528 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1529 if (port->wwpn_iid[i].in_use == 0) {
1530 if (port->wwpn_iid[i].last_use < best_time) {
1532 best_time = port->wwpn_iid[i].last_use;
1540 mtx_unlock(&softc->ctl_lock);
1545 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
1547 * This is not an error yet.
1549 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
1551 printf("%s: port %d iid %u WWPN %#jx arrived"
1552 " again\n", __func__, port->targ_port,
1553 iid, (uintmax_t)wwpn);
1557 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
1558 strcmp(name, port->wwpn_iid[iid].name) == 0) {
1560 printf("%s: port %d iid %u name '%s' arrived"
1561 " again\n", __func__, port->targ_port,
1568 * This is an error, but what do we do about it? The
1569 * driver is telling us we have a new WWPN for this
1570 * initiator ID, so we pretty much need to use it.
1572 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
1573 " but WWPN %#jx '%s' is still at that address\n",
1574 __func__, port->targ_port, iid, wwpn, name,
1575 (uintmax_t)port->wwpn_iid[iid].wwpn,
1576 port->wwpn_iid[iid].name);
1579 * XXX KDM clear have_ca and ua_pending on each LUN for
1584 free(port->wwpn_iid[iid].name, M_CTL);
1585 port->wwpn_iid[iid].name = name;
1586 port->wwpn_iid[iid].wwpn = wwpn;
1587 port->wwpn_iid[iid].in_use++;
1588 mtx_unlock(&softc->ctl_lock);
1594 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
1598 switch (port->port_type) {
1601 struct scsi_transportid_fcp *id =
1602 (struct scsi_transportid_fcp *)buf;
1603 if (port->wwpn_iid[iid].wwpn == 0)
1605 memset(id, 0, sizeof(*id));
1606 id->format_protocol = SCSI_PROTO_FC;
1607 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
1608 return (sizeof(*id));
1610 case CTL_PORT_ISCSI:
1612 struct scsi_transportid_iscsi_port *id =
1613 (struct scsi_transportid_iscsi_port *)buf;
1614 if (port->wwpn_iid[iid].name == NULL)
1617 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
1619 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
1620 len = roundup2(min(len, 252), 4);
1621 scsi_ulto2b(len, id->additional_length);
1622 return (sizeof(*id) + len);
1626 struct scsi_transportid_sas *id =
1627 (struct scsi_transportid_sas *)buf;
1628 if (port->wwpn_iid[iid].wwpn == 0)
1630 memset(id, 0, sizeof(*id));
1631 id->format_protocol = SCSI_PROTO_SAS;
1632 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
1633 return (sizeof(*id));
1637 struct scsi_transportid_spi *id =
1638 (struct scsi_transportid_spi *)buf;
1639 memset(id, 0, sizeof(*id));
1640 id->format_protocol = SCSI_PROTO_SPI;
1641 scsi_ulto2b(iid, id->scsi_addr);
1642 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
1643 return (sizeof(*id));
1649 ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id)
1655 ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id)
1661 * Data movement routine for the CTL ioctl frontend port.
1664 ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio)
1666 struct ctl_sg_entry *ext_sglist, *kern_sglist;
1667 struct ctl_sg_entry ext_entry, kern_entry;
1668 int ext_sglen, ext_sg_entries, kern_sg_entries;
1669 int ext_sg_start, ext_offset;
1670 int len_to_copy, len_copied;
1671 int kern_watermark, ext_watermark;
1672 int ext_sglist_malloced;
1675 ext_sglist_malloced = 0;
1679 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n"));
1682 * If this flag is set, fake the data transfer.
1684 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) {
1685 ctsio->ext_data_filled = ctsio->ext_data_len;
1690 * To simplify things here, if we have a single buffer, stick it in
1691 * a S/G entry and just make it a single entry S/G list.
1693 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) {
1696 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist);
1698 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL,
1700 ext_sglist_malloced = 1;
1701 if (copyin(ctsio->ext_data_ptr, ext_sglist,
1703 ctl_set_internal_failure(ctsio,
1708 ext_sg_entries = ctsio->ext_sg_entries;
1710 for (i = 0; i < ext_sg_entries; i++) {
1711 if ((len_seen + ext_sglist[i].len) >=
1712 ctsio->ext_data_filled) {
1714 ext_offset = ctsio->ext_data_filled - len_seen;
1717 len_seen += ext_sglist[i].len;
1720 ext_sglist = &ext_entry;
1721 ext_sglist->addr = ctsio->ext_data_ptr;
1722 ext_sglist->len = ctsio->ext_data_len;
1725 ext_offset = ctsio->ext_data_filled;
1728 if (ctsio->kern_sg_entries > 0) {
1729 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
1730 kern_sg_entries = ctsio->kern_sg_entries;
1732 kern_sglist = &kern_entry;
1733 kern_sglist->addr = ctsio->kern_data_ptr;
1734 kern_sglist->len = ctsio->kern_data_len;
1735 kern_sg_entries = 1;
1740 ext_watermark = ext_offset;
1742 for (i = ext_sg_start, j = 0;
1743 i < ext_sg_entries && j < kern_sg_entries;) {
1744 uint8_t *ext_ptr, *kern_ptr;
1746 len_to_copy = MIN(ext_sglist[i].len - ext_watermark,
1747 kern_sglist[j].len - kern_watermark);
1749 ext_ptr = (uint8_t *)ext_sglist[i].addr;
1750 ext_ptr = ext_ptr + ext_watermark;
1751 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
1755 panic("need to implement bus address support");
1757 kern_ptr = bus_to_virt(kern_sglist[j].addr);
1760 kern_ptr = (uint8_t *)kern_sglist[j].addr;
1761 kern_ptr = kern_ptr + kern_watermark;
1763 kern_watermark += len_to_copy;
1764 ext_watermark += len_to_copy;
1766 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
1768 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1769 "bytes to user\n", len_to_copy));
1770 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1771 "to %p\n", kern_ptr, ext_ptr));
1772 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) {
1773 ctl_set_internal_failure(ctsio,
1779 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1780 "bytes from user\n", len_to_copy));
1781 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1782 "to %p\n", ext_ptr, kern_ptr));
1783 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){
1784 ctl_set_internal_failure(ctsio,
1791 len_copied += len_to_copy;
1793 if (ext_sglist[i].len == ext_watermark) {
1798 if (kern_sglist[j].len == kern_watermark) {
1804 ctsio->ext_data_filled += len_copied;
1806 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, "
1807 "kern_sg_entries: %d\n", ext_sg_entries,
1809 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, "
1810 "kern_data_len = %d\n", ctsio->ext_data_len,
1811 ctsio->kern_data_len));
1814 /* XXX KDM set residual?? */
1817 if (ext_sglist_malloced != 0)
1818 free(ext_sglist, M_CTL);
1820 return (CTL_RETVAL_COMPLETE);
1824 * Serialize a command that went down the "wrong" side, and so was sent to
1825 * this controller for execution. The logic is a little different than the
1826 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
1827 * sent back to the other side, but in the success case, we execute the
1828 * command on this side (XFER mode) or tell the other side to execute it
1832 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
1834 struct ctl_softc *softc;
1835 union ctl_ha_msg msg_info;
1836 struct ctl_lun *lun;
1840 softc = control_softc;
1842 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
1843 lun = softc->ctl_luns[targ_lun];
1847 * Why isn't LUN defined? The other side wouldn't
1848 * send a cmd if the LUN is undefined.
1850 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__);
1852 /* "Logical unit not supported" */
1853 ctl_set_sense_data(&msg_info.scsi.sense_data,
1855 /*sense_format*/SSD_TYPE_NONE,
1856 /*current_error*/ 1,
1857 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1862 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1863 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1864 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1865 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1866 msg_info.hdr.serializing_sc = NULL;
1867 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1868 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1869 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1875 mtx_lock(&lun->lun_lock);
1876 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1878 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
1879 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
1881 case CTL_ACTION_BLOCK:
1882 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
1883 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
1886 case CTL_ACTION_PASS:
1887 case CTL_ACTION_SKIP:
1888 if (softc->ha_mode == CTL_HA_MODE_XFER) {
1889 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
1890 ctl_enqueue_rtr((union ctl_io *)ctsio);
1893 /* send msg back to other side */
1894 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1895 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
1896 msg_info.hdr.msg_type = CTL_MSG_R2R;
1898 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc);
1900 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1901 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1905 case CTL_ACTION_OVERLAP:
1906 /* OVERLAPPED COMMANDS ATTEMPTED */
1907 ctl_set_sense_data(&msg_info.scsi.sense_data,
1909 /*sense_format*/SSD_TYPE_NONE,
1910 /*current_error*/ 1,
1911 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1916 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1917 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1918 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1919 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1920 msg_info.hdr.serializing_sc = NULL;
1921 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1923 printf("BAD JUJU:Major Bummer Overlap\n");
1925 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1927 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1928 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1931 case CTL_ACTION_OVERLAP_TAG:
1932 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */
1933 ctl_set_sense_data(&msg_info.scsi.sense_data,
1935 /*sense_format*/SSD_TYPE_NONE,
1936 /*current_error*/ 1,
1937 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1939 /*ascq*/ ctsio->tag_num & 0xff,
1942 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1943 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1944 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1945 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1946 msg_info.hdr.serializing_sc = NULL;
1947 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1949 printf("BAD JUJU:Major Bummer Overlap Tag\n");
1951 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1953 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1954 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1957 case CTL_ACTION_ERROR:
1959 /* "Internal target failure" */
1960 ctl_set_sense_data(&msg_info.scsi.sense_data,
1962 /*sense_format*/SSD_TYPE_NONE,
1963 /*current_error*/ 1,
1964 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
1969 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1970 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1971 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1972 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1973 msg_info.hdr.serializing_sc = NULL;
1974 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1976 printf("BAD JUJU:Major Bummer HW Error\n");
1978 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1980 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1981 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1985 mtx_unlock(&lun->lun_lock);
1990 ctl_ioctl_submit_wait(union ctl_io *io)
1992 struct ctl_fe_ioctl_params params;
1993 ctl_fe_ioctl_state last_state;
1998 bzero(¶ms, sizeof(params));
2000 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF);
2001 cv_init(¶ms.sem, "ctlioccv");
2002 params.state = CTL_IOCTL_INPROG;
2003 last_state = params.state;
2005 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms;
2007 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n"));
2009 /* This shouldn't happen */
2010 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE)
2016 mtx_lock(¶ms.ioctl_mtx);
2018 * Check the state here, and don't sleep if the state has
2019 * already changed (i.e. wakeup has already occured, but we
2020 * weren't waiting yet).
2022 if (params.state == last_state) {
2023 /* XXX KDM cv_wait_sig instead? */
2024 cv_wait(¶ms.sem, ¶ms.ioctl_mtx);
2026 last_state = params.state;
2028 switch (params.state) {
2029 case CTL_IOCTL_INPROG:
2030 /* Why did we wake up? */
2031 /* XXX KDM error here? */
2032 mtx_unlock(¶ms.ioctl_mtx);
2034 case CTL_IOCTL_DATAMOVE:
2035 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n"));
2038 * change last_state back to INPROG to avoid
2039 * deadlock on subsequent data moves.
2041 params.state = last_state = CTL_IOCTL_INPROG;
2043 mtx_unlock(¶ms.ioctl_mtx);
2044 ctl_ioctl_do_datamove(&io->scsiio);
2046 * Note that in some cases, most notably writes,
2047 * this will queue the I/O and call us back later.
2048 * In other cases, generally reads, this routine
2049 * will immediately call back and wake us up,
2050 * probably using our own context.
2052 io->scsiio.be_move_done(io);
2054 case CTL_IOCTL_DONE:
2055 mtx_unlock(¶ms.ioctl_mtx);
2056 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n"));
2060 mtx_unlock(¶ms.ioctl_mtx);
2061 /* XXX KDM error here? */
2064 } while (done == 0);
2066 mtx_destroy(¶ms.ioctl_mtx);
2067 cv_destroy(¶ms.sem);
2069 return (CTL_RETVAL_COMPLETE);
2073 ctl_ioctl_datamove(union ctl_io *io)
2075 struct ctl_fe_ioctl_params *params;
2077 params = (struct ctl_fe_ioctl_params *)
2078 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2080 mtx_lock(¶ms->ioctl_mtx);
2081 params->state = CTL_IOCTL_DATAMOVE;
2082 cv_broadcast(¶ms->sem);
2083 mtx_unlock(¶ms->ioctl_mtx);
2087 ctl_ioctl_done(union ctl_io *io)
2089 struct ctl_fe_ioctl_params *params;
2091 params = (struct ctl_fe_ioctl_params *)
2092 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2094 mtx_lock(¶ms->ioctl_mtx);
2095 params->state = CTL_IOCTL_DONE;
2096 cv_broadcast(¶ms->sem);
2097 mtx_unlock(¶ms->ioctl_mtx);
2101 ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask)
2103 struct ctl_fe_ioctl_startstop_info *sd_info;
2105 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg;
2107 sd_info->hs_info.status = metatask->status;
2108 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns;
2109 sd_info->hs_info.luns_complete =
2110 metatask->taskinfo.startstop.luns_complete;
2111 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed;
2113 cv_broadcast(&sd_info->sem);
2117 ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask)
2119 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info;
2121 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg;
2123 mtx_lock(fe_bbr_info->lock);
2124 fe_bbr_info->bbr_info->status = metatask->status;
2125 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2126 fe_bbr_info->wakeup_done = 1;
2127 mtx_unlock(fe_bbr_info->lock);
2129 cv_broadcast(&fe_bbr_info->sem);
2133 * Returns 0 for success, errno for failure.
2136 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2137 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2144 mtx_lock(&lun->lun_lock);
2145 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2146 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2148 struct ctl_ooa_entry *entry;
2151 * If we've got more than we can fit, just count the
2152 * remaining entries.
2154 if (*cur_fill_num >= ooa_hdr->alloc_num)
2157 entry = &kern_entries[*cur_fill_num];
2159 entry->tag_num = io->scsiio.tag_num;
2160 entry->lun_num = lun->lun;
2162 entry->start_bt = io->io_hdr.start_bt;
2164 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2165 entry->cdb_len = io->scsiio.cdb_len;
2166 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2167 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2169 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2170 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2172 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2173 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2175 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2176 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2178 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2179 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2181 mtx_unlock(&lun->lun_lock);
2187 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2188 size_t error_str_len)
2192 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2194 if (copyin(user_addr, kptr, len) != 0) {
2195 snprintf(error_str, error_str_len, "Error copying %d bytes "
2196 "from user address %p to kernel address %p", len,
2206 ctl_free_args(int num_args, struct ctl_be_arg *args)
2213 for (i = 0; i < num_args; i++) {
2214 free(args[i].kname, M_CTL);
2215 free(args[i].kvalue, M_CTL);
2221 static struct ctl_be_arg *
2222 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
2223 char *error_str, size_t error_str_len)
2225 struct ctl_be_arg *args;
2228 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
2229 error_str, error_str_len);
2234 for (i = 0; i < num_args; i++) {
2235 args[i].kname = NULL;
2236 args[i].kvalue = NULL;
2239 for (i = 0; i < num_args; i++) {
2242 args[i].kname = ctl_copyin_alloc(args[i].name,
2243 args[i].namelen, error_str, error_str_len);
2244 if (args[i].kname == NULL)
2247 if (args[i].kname[args[i].namelen - 1] != '\0') {
2248 snprintf(error_str, error_str_len, "Argument %d "
2249 "name is not NUL-terminated", i);
2253 if (args[i].flags & CTL_BEARG_RD) {
2254 tmpptr = ctl_copyin_alloc(args[i].value,
2255 args[i].vallen, error_str, error_str_len);
2258 if ((args[i].flags & CTL_BEARG_ASCII)
2259 && (tmpptr[args[i].vallen - 1] != '\0')) {
2260 snprintf(error_str, error_str_len, "Argument "
2261 "%d value is not NUL-terminated", i);
2264 args[i].kvalue = tmpptr;
2266 args[i].kvalue = malloc(args[i].vallen,
2267 M_CTL, M_WAITOK | M_ZERO);
2274 ctl_free_args(num_args, args);
2280 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2284 for (i = 0; i < num_args; i++) {
2285 if (args[i].flags & CTL_BEARG_WR)
2286 copyout(args[i].kvalue, args[i].value, args[i].vallen);
2291 * Escape characters that are illegal or not recommended in XML.
2294 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2296 char *end = str + size;
2301 for (; *str && str < end; str++) {
2304 retval = sbuf_printf(sb, "&");
2307 retval = sbuf_printf(sb, ">");
2310 retval = sbuf_printf(sb, "<");
2313 retval = sbuf_putc(sb, *str);
2326 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2328 struct scsi_vpd_id_descriptor *desc;
2331 if (id == NULL || id->len < 4)
2333 desc = (struct scsi_vpd_id_descriptor *)id->data;
2334 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2335 case SVPD_ID_TYPE_T10:
2336 sbuf_printf(sb, "t10.");
2338 case SVPD_ID_TYPE_EUI64:
2339 sbuf_printf(sb, "eui.");
2341 case SVPD_ID_TYPE_NAA:
2342 sbuf_printf(sb, "naa.");
2344 case SVPD_ID_TYPE_SCSI_NAME:
2347 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2348 case SVPD_ID_CODESET_BINARY:
2349 for (i = 0; i < desc->length; i++)
2350 sbuf_printf(sb, "%02x", desc->identifier[i]);
2352 case SVPD_ID_CODESET_ASCII:
2353 sbuf_printf(sb, "%.*s", (int)desc->length,
2354 (char *)desc->identifier);
2356 case SVPD_ID_CODESET_UTF8:
2357 sbuf_printf(sb, "%s", (char *)desc->identifier);
2363 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2366 struct ctl_softc *softc;
2369 softc = control_softc;
2379 * If we haven't been "enabled", don't allow any SCSI I/O
2382 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) {
2387 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref);
2390 * Need to save the pool reference so it doesn't get
2391 * spammed by the user's ctl_io.
2393 pool_tmp = io->io_hdr.pool;
2394 memcpy(io, (void *)addr, sizeof(*io));
2395 io->io_hdr.pool = pool_tmp;
2398 * No status yet, so make sure the status is set properly.
2400 io->io_hdr.status = CTL_STATUS_NONE;
2403 * The user sets the initiator ID, target and LUN IDs.
2405 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port;
2406 io->io_hdr.flags |= CTL_FLAG_USER_REQ;
2407 if ((io->io_hdr.io_type == CTL_IO_SCSI)
2408 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED))
2409 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++;
2411 retval = ctl_ioctl_submit_wait(io);
2418 memcpy((void *)addr, io, sizeof(*io));
2420 /* return this to our pool */
2425 case CTL_ENABLE_PORT:
2426 case CTL_DISABLE_PORT:
2427 case CTL_SET_PORT_WWNS: {
2428 struct ctl_port *port;
2429 struct ctl_port_entry *entry;
2431 entry = (struct ctl_port_entry *)addr;
2433 mtx_lock(&softc->ctl_lock);
2434 STAILQ_FOREACH(port, &softc->port_list, links) {
2440 if ((entry->port_type == CTL_PORT_NONE)
2441 && (entry->targ_port == port->targ_port)) {
2443 * If the user only wants to enable or
2444 * disable or set WWNs on a specific port,
2445 * do the operation and we're done.
2449 } else if (entry->port_type & port->port_type) {
2451 * Compare the user's type mask with the
2452 * particular frontend type to see if we
2459 * Make sure the user isn't trying to set
2460 * WWNs on multiple ports at the same time.
2462 if (cmd == CTL_SET_PORT_WWNS) {
2463 printf("%s: Can't set WWNs on "
2464 "multiple ports\n", __func__);
2471 * XXX KDM we have to drop the lock here,
2472 * because the online/offline operations
2473 * can potentially block. We need to
2474 * reference count the frontends so they
2477 mtx_unlock(&softc->ctl_lock);
2479 if (cmd == CTL_ENABLE_PORT) {
2480 struct ctl_lun *lun;
2482 STAILQ_FOREACH(lun, &softc->lun_list,
2484 port->lun_enable(port->targ_lun_arg,
2489 ctl_port_online(port);
2490 } else if (cmd == CTL_DISABLE_PORT) {
2491 struct ctl_lun *lun;
2493 ctl_port_offline(port);
2495 STAILQ_FOREACH(lun, &softc->lun_list,
2504 mtx_lock(&softc->ctl_lock);
2506 if (cmd == CTL_SET_PORT_WWNS)
2507 ctl_port_set_wwns(port,
2508 (entry->flags & CTL_PORT_WWNN_VALID) ?
2510 (entry->flags & CTL_PORT_WWPN_VALID) ?
2511 1 : 0, entry->wwpn);
2516 mtx_unlock(&softc->ctl_lock);
2519 case CTL_GET_PORT_LIST: {
2520 struct ctl_port *port;
2521 struct ctl_port_list *list;
2524 list = (struct ctl_port_list *)addr;
2526 if (list->alloc_len != (list->alloc_num *
2527 sizeof(struct ctl_port_entry))) {
2528 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != "
2529 "alloc_num %u * sizeof(struct ctl_port_entry) "
2530 "%zu\n", __func__, list->alloc_len,
2531 list->alloc_num, sizeof(struct ctl_port_entry));
2537 list->dropped_num = 0;
2539 mtx_lock(&softc->ctl_lock);
2540 STAILQ_FOREACH(port, &softc->port_list, links) {
2541 struct ctl_port_entry entry, *list_entry;
2543 if (list->fill_num >= list->alloc_num) {
2544 list->dropped_num++;
2548 entry.port_type = port->port_type;
2549 strlcpy(entry.port_name, port->port_name,
2550 sizeof(entry.port_name));
2551 entry.targ_port = port->targ_port;
2552 entry.physical_port = port->physical_port;
2553 entry.virtual_port = port->virtual_port;
2554 entry.wwnn = port->wwnn;
2555 entry.wwpn = port->wwpn;
2556 if (port->status & CTL_PORT_STATUS_ONLINE)
2561 list_entry = &list->entries[i];
2563 retval = copyout(&entry, list_entry, sizeof(entry));
2565 printf("%s: CTL_GET_PORT_LIST: copyout "
2566 "returned %d\n", __func__, retval);
2571 list->fill_len += sizeof(entry);
2573 mtx_unlock(&softc->ctl_lock);
2576 * If this is non-zero, we had a copyout fault, so there's
2577 * probably no point in attempting to set the status inside
2583 if (list->dropped_num > 0)
2584 list->status = CTL_PORT_LIST_NEED_MORE_SPACE;
2586 list->status = CTL_PORT_LIST_OK;
2589 case CTL_DUMP_OOA: {
2590 struct ctl_lun *lun;
2595 mtx_lock(&softc->ctl_lock);
2596 printf("Dumping OOA queues:\n");
2597 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2598 mtx_lock(&lun->lun_lock);
2599 for (io = (union ctl_io *)TAILQ_FIRST(
2600 &lun->ooa_queue); io != NULL;
2601 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2603 sbuf_new(&sb, printbuf, sizeof(printbuf),
2605 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ",
2609 CTL_FLAG_BLOCKED) ? "" : " BLOCKED",
2611 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
2613 CTL_FLAG_ABORT) ? " ABORT" : "",
2615 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : "");
2616 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
2618 printf("%s\n", sbuf_data(&sb));
2620 mtx_unlock(&lun->lun_lock);
2622 printf("OOA queues dump done\n");
2623 mtx_unlock(&softc->ctl_lock);
2627 struct ctl_lun *lun;
2628 struct ctl_ooa *ooa_hdr;
2629 struct ctl_ooa_entry *entries;
2630 uint32_t cur_fill_num;
2632 ooa_hdr = (struct ctl_ooa *)addr;
2634 if ((ooa_hdr->alloc_len == 0)
2635 || (ooa_hdr->alloc_num == 0)) {
2636 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2637 "must be non-zero\n", __func__,
2638 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2643 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2644 sizeof(struct ctl_ooa_entry))) {
2645 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2646 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2647 __func__, ooa_hdr->alloc_len,
2648 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2653 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2654 if (entries == NULL) {
2655 printf("%s: could not allocate %d bytes for OOA "
2656 "dump\n", __func__, ooa_hdr->alloc_len);
2661 mtx_lock(&softc->ctl_lock);
2662 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2663 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS)
2664 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2665 mtx_unlock(&softc->ctl_lock);
2666 free(entries, M_CTL);
2667 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2668 __func__, (uintmax_t)ooa_hdr->lun_num);
2675 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2676 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2677 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2683 mtx_unlock(&softc->ctl_lock);
2684 free(entries, M_CTL);
2688 lun = softc->ctl_luns[ooa_hdr->lun_num];
2690 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr,
2693 mtx_unlock(&softc->ctl_lock);
2695 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2696 ooa_hdr->fill_len = ooa_hdr->fill_num *
2697 sizeof(struct ctl_ooa_entry);
2698 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2700 printf("%s: error copying out %d bytes for OOA dump\n",
2701 __func__, ooa_hdr->fill_len);
2704 getbintime(&ooa_hdr->cur_bt);
2706 if (cur_fill_num > ooa_hdr->alloc_num) {
2707 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2708 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2710 ooa_hdr->dropped_num = 0;
2711 ooa_hdr->status = CTL_OOA_OK;
2714 free(entries, M_CTL);
2717 case CTL_CHECK_OOA: {
2719 struct ctl_lun *lun;
2720 struct ctl_ooa_info *ooa_info;
2723 ooa_info = (struct ctl_ooa_info *)addr;
2725 if (ooa_info->lun_id >= CTL_MAX_LUNS) {
2726 ooa_info->status = CTL_OOA_INVALID_LUN;
2729 mtx_lock(&softc->ctl_lock);
2730 lun = softc->ctl_luns[ooa_info->lun_id];
2732 mtx_unlock(&softc->ctl_lock);
2733 ooa_info->status = CTL_OOA_INVALID_LUN;
2736 mtx_lock(&lun->lun_lock);
2737 mtx_unlock(&softc->ctl_lock);
2738 ooa_info->num_entries = 0;
2739 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
2740 io != NULL; io = (union ctl_io *)TAILQ_NEXT(
2741 &io->io_hdr, ooa_links)) {
2742 ooa_info->num_entries++;
2744 mtx_unlock(&lun->lun_lock);
2746 ooa_info->status = CTL_OOA_SUCCESS;
2750 case CTL_HARD_START:
2751 case CTL_HARD_STOP: {
2752 struct ctl_fe_ioctl_startstop_info ss_info;
2753 struct cfi_metatask *metatask;
2756 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF);
2758 cv_init(&ss_info.sem, "hard start/stop cv" );
2760 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2761 if (metatask == NULL) {
2763 mtx_destroy(&hs_mtx);
2767 if (cmd == CTL_HARD_START)
2768 metatask->tasktype = CFI_TASK_STARTUP;
2770 metatask->tasktype = CFI_TASK_SHUTDOWN;
2772 metatask->callback = ctl_ioctl_hard_startstop_callback;
2773 metatask->callback_arg = &ss_info;
2775 cfi_action(metatask);
2777 /* Wait for the callback */
2779 cv_wait_sig(&ss_info.sem, &hs_mtx);
2780 mtx_unlock(&hs_mtx);
2783 * All information has been copied from the metatask by the
2784 * time cv_broadcast() is called, so we free the metatask here.
2786 cfi_free_metatask(metatask);
2788 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info));
2790 mtx_destroy(&hs_mtx);
2794 struct ctl_bbrread_info *bbr_info;
2795 struct ctl_fe_ioctl_bbrread_info fe_bbr_info;
2797 struct cfi_metatask *metatask;
2799 bbr_info = (struct ctl_bbrread_info *)addr;
2801 bzero(&fe_bbr_info, sizeof(fe_bbr_info));
2803 bzero(&bbr_mtx, sizeof(bbr_mtx));
2804 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF);
2806 fe_bbr_info.bbr_info = bbr_info;
2807 fe_bbr_info.lock = &bbr_mtx;
2809 cv_init(&fe_bbr_info.sem, "BBR read cv");
2810 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2812 if (metatask == NULL) {
2813 mtx_destroy(&bbr_mtx);
2814 cv_destroy(&fe_bbr_info.sem);
2818 metatask->tasktype = CFI_TASK_BBRREAD;
2819 metatask->callback = ctl_ioctl_bbrread_callback;
2820 metatask->callback_arg = &fe_bbr_info;
2821 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num;
2822 metatask->taskinfo.bbrread.lba = bbr_info->lba;
2823 metatask->taskinfo.bbrread.len = bbr_info->len;
2825 cfi_action(metatask);
2828 while (fe_bbr_info.wakeup_done == 0)
2829 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx);
2830 mtx_unlock(&bbr_mtx);
2832 bbr_info->status = metatask->status;
2833 bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2834 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status;
2835 memcpy(&bbr_info->sense_data,
2836 &metatask->taskinfo.bbrread.sense_data,
2837 MIN(sizeof(bbr_info->sense_data),
2838 sizeof(metatask->taskinfo.bbrread.sense_data)));
2840 cfi_free_metatask(metatask);
2842 mtx_destroy(&bbr_mtx);
2843 cv_destroy(&fe_bbr_info.sem);
2847 case CTL_DELAY_IO: {
2848 struct ctl_io_delay_info *delay_info;
2850 struct ctl_lun *lun;
2851 #endif /* CTL_IO_DELAY */
2853 delay_info = (struct ctl_io_delay_info *)addr;
2856 mtx_lock(&softc->ctl_lock);
2858 if ((delay_info->lun_id >= CTL_MAX_LUNS)
2859 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2860 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2862 lun = softc->ctl_luns[delay_info->lun_id];
2863 mtx_lock(&lun->lun_lock);
2865 delay_info->status = CTL_DELAY_STATUS_OK;
2867 switch (delay_info->delay_type) {
2868 case CTL_DELAY_TYPE_CONT:
2870 case CTL_DELAY_TYPE_ONESHOT:
2873 delay_info->status =
2874 CTL_DELAY_STATUS_INVALID_TYPE;
2878 switch (delay_info->delay_loc) {
2879 case CTL_DELAY_LOC_DATAMOVE:
2880 lun->delay_info.datamove_type =
2881 delay_info->delay_type;
2882 lun->delay_info.datamove_delay =
2883 delay_info->delay_secs;
2885 case CTL_DELAY_LOC_DONE:
2886 lun->delay_info.done_type =
2887 delay_info->delay_type;
2888 lun->delay_info.done_delay =
2889 delay_info->delay_secs;
2892 delay_info->status =
2893 CTL_DELAY_STATUS_INVALID_LOC;
2896 mtx_unlock(&lun->lun_lock);
2899 mtx_unlock(&softc->ctl_lock);
2901 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2902 #endif /* CTL_IO_DELAY */
2905 case CTL_REALSYNC_SET: {
2908 syncstate = (int *)addr;
2910 mtx_lock(&softc->ctl_lock);
2911 switch (*syncstate) {
2913 softc->flags &= ~CTL_FLAG_REAL_SYNC;
2916 softc->flags |= CTL_FLAG_REAL_SYNC;
2922 mtx_unlock(&softc->ctl_lock);
2925 case CTL_REALSYNC_GET: {
2928 syncstate = (int*)addr;
2930 mtx_lock(&softc->ctl_lock);
2931 if (softc->flags & CTL_FLAG_REAL_SYNC)
2935 mtx_unlock(&softc->ctl_lock);
2941 struct ctl_sync_info *sync_info;
2942 struct ctl_lun *lun;
2944 sync_info = (struct ctl_sync_info *)addr;
2946 mtx_lock(&softc->ctl_lock);
2947 lun = softc->ctl_luns[sync_info->lun_id];
2949 mtx_unlock(&softc->ctl_lock);
2950 sync_info->status = CTL_GS_SYNC_NO_LUN;
2953 * Get or set the sync interval. We're not bounds checking
2954 * in the set case, hopefully the user won't do something
2957 mtx_lock(&lun->lun_lock);
2958 mtx_unlock(&softc->ctl_lock);
2959 if (cmd == CTL_GETSYNC)
2960 sync_info->sync_interval = lun->sync_interval;
2962 lun->sync_interval = sync_info->sync_interval;
2963 mtx_unlock(&lun->lun_lock);
2965 sync_info->status = CTL_GS_SYNC_OK;
2969 case CTL_GETSTATS: {
2970 struct ctl_stats *stats;
2971 struct ctl_lun *lun;
2974 stats = (struct ctl_stats *)addr;
2976 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2978 stats->status = CTL_SS_NEED_MORE_SPACE;
2979 stats->num_luns = softc->num_luns;
2983 * XXX KDM no locking here. If the LUN list changes,
2984 * things can blow up.
2986 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
2987 i++, lun = STAILQ_NEXT(lun, links)) {
2988 retval = copyout(&lun->stats, &stats->lun_stats[i],
2989 sizeof(lun->stats));
2993 stats->num_luns = softc->num_luns;
2994 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2996 stats->status = CTL_SS_OK;
2998 stats->flags = CTL_STATS_FLAG_TIME_VALID;
3000 stats->flags = CTL_STATS_FLAG_NONE;
3002 getnanouptime(&stats->timestamp);
3005 case CTL_ERROR_INJECT: {
3006 struct ctl_error_desc *err_desc, *new_err_desc;
3007 struct ctl_lun *lun;
3009 err_desc = (struct ctl_error_desc *)addr;
3011 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
3013 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
3015 mtx_lock(&softc->ctl_lock);
3016 lun = softc->ctl_luns[err_desc->lun_id];
3018 mtx_unlock(&softc->ctl_lock);
3019 free(new_err_desc, M_CTL);
3020 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
3021 __func__, (uintmax_t)err_desc->lun_id);
3025 mtx_lock(&lun->lun_lock);
3026 mtx_unlock(&softc->ctl_lock);
3029 * We could do some checking here to verify the validity
3030 * of the request, but given the complexity of error
3031 * injection requests, the checking logic would be fairly
3034 * For now, if the request is invalid, it just won't get
3035 * executed and might get deleted.
3037 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
3040 * XXX KDM check to make sure the serial number is unique,
3041 * in case we somehow manage to wrap. That shouldn't
3042 * happen for a very long time, but it's the right thing to
3045 new_err_desc->serial = lun->error_serial;
3046 err_desc->serial = lun->error_serial;
3047 lun->error_serial++;
3049 mtx_unlock(&lun->lun_lock);
3052 case CTL_ERROR_INJECT_DELETE: {
3053 struct ctl_error_desc *delete_desc, *desc, *desc2;
3054 struct ctl_lun *lun;
3057 delete_desc = (struct ctl_error_desc *)addr;
3060 mtx_lock(&softc->ctl_lock);
3061 lun = softc->ctl_luns[delete_desc->lun_id];
3063 mtx_unlock(&softc->ctl_lock);
3064 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
3065 __func__, (uintmax_t)delete_desc->lun_id);
3069 mtx_lock(&lun->lun_lock);
3070 mtx_unlock(&softc->ctl_lock);
3071 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
3072 if (desc->serial != delete_desc->serial)
3075 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
3080 mtx_unlock(&lun->lun_lock);
3081 if (delete_done == 0) {
3082 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
3083 "error serial %ju on LUN %u\n", __func__,
3084 delete_desc->serial, delete_desc->lun_id);
3090 case CTL_DUMP_STRUCTS: {
3092 struct ctl_port *port;
3093 struct ctl_frontend *fe;
3095 mtx_lock(&softc->ctl_lock);
3096 printf("CTL Persistent Reservation information start:\n");
3097 for (i = 0; i < CTL_MAX_LUNS; i++) {
3098 struct ctl_lun *lun;
3100 lun = softc->ctl_luns[i];
3103 || ((lun->flags & CTL_LUN_DISABLED) != 0))
3106 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) {
3107 if (lun->pr_keys[j] == NULL)
3109 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
3110 if (lun->pr_keys[j][k] == 0)
3112 printf(" LUN %d port %d iid %d key "
3114 (uintmax_t)lun->pr_keys[j][k]);
3118 printf("CTL Persistent Reservation information end\n");
3119 printf("CTL Ports:\n");
3120 STAILQ_FOREACH(port, &softc->port_list, links) {
3121 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
3122 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
3123 port->frontend->name, port->port_type,
3124 port->physical_port, port->virtual_port,
3125 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
3126 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3127 if (port->wwpn_iid[j].in_use == 0 &&
3128 port->wwpn_iid[j].wwpn == 0 &&
3129 port->wwpn_iid[j].name == NULL)
3132 printf(" iid %u use %d WWPN %#jx '%s'\n",
3133 j, port->wwpn_iid[j].in_use,
3134 (uintmax_t)port->wwpn_iid[j].wwpn,
3135 port->wwpn_iid[j].name);
3138 printf("CTL Port information end\n");
3139 mtx_unlock(&softc->ctl_lock);
3141 * XXX KDM calling this without a lock. We'd likely want
3142 * to drop the lock before calling the frontend's dump
3145 printf("CTL Frontends:\n");
3146 STAILQ_FOREACH(fe, &softc->fe_list, links) {
3147 printf(" Frontend '%s'\n", fe->name);
3148 if (fe->fe_dump != NULL)
3151 printf("CTL Frontend information end\n");
3155 struct ctl_lun_req *lun_req;
3156 struct ctl_backend_driver *backend;
3158 lun_req = (struct ctl_lun_req *)addr;
3160 backend = ctl_backend_find(lun_req->backend);
3161 if (backend == NULL) {
3162 lun_req->status = CTL_LUN_ERROR;
3163 snprintf(lun_req->error_str,
3164 sizeof(lun_req->error_str),
3165 "Backend \"%s\" not found.",
3169 if (lun_req->num_be_args > 0) {
3170 lun_req->kern_be_args = ctl_copyin_args(
3171 lun_req->num_be_args,
3174 sizeof(lun_req->error_str));
3175 if (lun_req->kern_be_args == NULL) {
3176 lun_req->status = CTL_LUN_ERROR;
3181 retval = backend->ioctl(dev, cmd, addr, flag, td);
3183 if (lun_req->num_be_args > 0) {
3184 ctl_copyout_args(lun_req->num_be_args,
3185 lun_req->kern_be_args);
3186 ctl_free_args(lun_req->num_be_args,
3187 lun_req->kern_be_args);
3191 case CTL_LUN_LIST: {
3193 struct ctl_lun *lun;
3194 struct ctl_lun_list *list;
3195 struct ctl_option *opt;
3197 list = (struct ctl_lun_list *)addr;
3200 * Allocate a fixed length sbuf here, based on the length
3201 * of the user's buffer. We could allocate an auto-extending
3202 * buffer, and then tell the user how much larger our
3203 * amount of data is than his buffer, but that presents
3206 * 1. The sbuf(9) routines use a blocking malloc, and so
3207 * we can't hold a lock while calling them with an
3208 * auto-extending buffer.
3210 * 2. There is not currently a LUN reference counting
3211 * mechanism, outside of outstanding transactions on
3212 * the LUN's OOA queue. So a LUN could go away on us
3213 * while we're getting the LUN number, backend-specific
3214 * information, etc. Thus, given the way things
3215 * currently work, we need to hold the CTL lock while
3216 * grabbing LUN information.
3218 * So, from the user's standpoint, the best thing to do is
3219 * allocate what he thinks is a reasonable buffer length,
3220 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3221 * double the buffer length and try again. (And repeat
3222 * that until he succeeds.)
3224 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3226 list->status = CTL_LUN_LIST_ERROR;
3227 snprintf(list->error_str, sizeof(list->error_str),
3228 "Unable to allocate %d bytes for LUN list",
3233 sbuf_printf(sb, "<ctllunlist>\n");
3235 mtx_lock(&softc->ctl_lock);
3236 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3237 mtx_lock(&lun->lun_lock);
3238 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3239 (uintmax_t)lun->lun);
3242 * Bail out as soon as we see that we've overfilled
3248 retval = sbuf_printf(sb, "\t<backend_type>%s"
3249 "</backend_type>\n",
3250 (lun->backend == NULL) ? "none" :
3251 lun->backend->name);
3256 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3257 lun->be_lun->lun_type);
3262 if (lun->backend == NULL) {
3263 retval = sbuf_printf(sb, "</lun>\n");
3269 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3270 (lun->be_lun->maxlba > 0) ?
3271 lun->be_lun->maxlba + 1 : 0);
3276 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3277 lun->be_lun->blocksize);
3282 retval = sbuf_printf(sb, "\t<serial_number>");
3287 retval = ctl_sbuf_printf_esc(sb,
3288 lun->be_lun->serial_num,
3289 sizeof(lun->be_lun->serial_num));
3294 retval = sbuf_printf(sb, "</serial_number>\n");
3299 retval = sbuf_printf(sb, "\t<device_id>");
3304 retval = ctl_sbuf_printf_esc(sb,
3305 lun->be_lun->device_id,
3306 sizeof(lun->be_lun->device_id));
3311 retval = sbuf_printf(sb, "</device_id>\n");
3316 if (lun->backend->lun_info != NULL) {
3317 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3321 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3322 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3323 opt->name, opt->value, opt->name);
3328 retval = sbuf_printf(sb, "</lun>\n");
3332 mtx_unlock(&lun->lun_lock);
3335 mtx_unlock(&lun->lun_lock);
3336 mtx_unlock(&softc->ctl_lock);
3339 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3342 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3343 snprintf(list->error_str, sizeof(list->error_str),
3344 "Out of space, %d bytes is too small",
3351 retval = copyout(sbuf_data(sb), list->lun_xml,
3354 list->fill_len = sbuf_len(sb) + 1;
3355 list->status = CTL_LUN_LIST_OK;
3360 struct ctl_iscsi *ci;
3361 struct ctl_frontend *fe;
3363 ci = (struct ctl_iscsi *)addr;
3365 fe = ctl_frontend_find("iscsi");
3367 ci->status = CTL_ISCSI_ERROR;
3368 snprintf(ci->error_str, sizeof(ci->error_str),
3369 "Frontend \"iscsi\" not found.");
3373 retval = fe->ioctl(dev, cmd, addr, flag, td);
3376 case CTL_PORT_REQ: {
3377 struct ctl_req *req;
3378 struct ctl_frontend *fe;
3380 req = (struct ctl_req *)addr;
3382 fe = ctl_frontend_find(req->driver);
3384 req->status = CTL_LUN_ERROR;
3385 snprintf(req->error_str, sizeof(req->error_str),
3386 "Frontend \"%s\" not found.", req->driver);
3389 if (req->num_args > 0) {
3390 req->kern_args = ctl_copyin_args(req->num_args,
3391 req->args, req->error_str, sizeof(req->error_str));
3392 if (req->kern_args == NULL) {
3393 req->status = CTL_LUN_ERROR;
3398 retval = fe->ioctl(dev, cmd, addr, flag, td);
3400 if (req->num_args > 0) {
3401 ctl_copyout_args(req->num_args, req->kern_args);
3402 ctl_free_args(req->num_args, req->kern_args);
3406 case CTL_PORT_LIST: {
3408 struct ctl_port *port;
3409 struct ctl_lun_list *list;
3410 struct ctl_option *opt;
3413 list = (struct ctl_lun_list *)addr;
3415 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3417 list->status = CTL_LUN_LIST_ERROR;
3418 snprintf(list->error_str, sizeof(list->error_str),
3419 "Unable to allocate %d bytes for LUN list",
3424 sbuf_printf(sb, "<ctlportlist>\n");
3426 mtx_lock(&softc->ctl_lock);
3427 STAILQ_FOREACH(port, &softc->port_list, links) {
3428 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3429 (uintmax_t)port->targ_port);
3432 * Bail out as soon as we see that we've overfilled
3438 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3439 "</frontend_type>\n", port->frontend->name);
3443 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3448 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3449 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3453 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3458 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3459 port->physical_port);
3463 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3464 port->virtual_port);
3468 if (port->target_devid != NULL) {
3469 sbuf_printf(sb, "\t<target>");
3470 ctl_id_sbuf(port->target_devid, sb);
3471 sbuf_printf(sb, "</target>\n");
3474 if (port->port_devid != NULL) {
3475 sbuf_printf(sb, "\t<port>");
3476 ctl_id_sbuf(port->port_devid, sb);
3477 sbuf_printf(sb, "</port>\n");
3480 if (port->port_info != NULL) {
3481 retval = port->port_info(port->onoff_arg, sb);
3485 STAILQ_FOREACH(opt, &port->options, links) {
3486 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3487 opt->name, opt->value, opt->name);
3492 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3493 if (port->wwpn_iid[j].in_use == 0 ||
3494 (port->wwpn_iid[j].wwpn == 0 &&
3495 port->wwpn_iid[j].name == NULL))
3498 if (port->wwpn_iid[j].name != NULL)
3499 retval = sbuf_printf(sb,
3500 "\t<initiator id=\"%u\">%s</initiator>\n",
3501 j, port->wwpn_iid[j].name);
3503 retval = sbuf_printf(sb,
3504 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3505 j, port->wwpn_iid[j].wwpn);
3512 retval = sbuf_printf(sb, "</targ_port>\n");
3516 mtx_unlock(&softc->ctl_lock);
3519 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3522 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3523 snprintf(list->error_str, sizeof(list->error_str),
3524 "Out of space, %d bytes is too small",
3531 retval = copyout(sbuf_data(sb), list->lun_xml,
3534 list->fill_len = sbuf_len(sb) + 1;
3535 list->status = CTL_LUN_LIST_OK;
3540 /* XXX KDM should we fix this? */
3542 struct ctl_backend_driver *backend;
3549 * We encode the backend type as the ioctl type for backend
3550 * ioctls. So parse it out here, and then search for a
3551 * backend of this type.
3553 type = _IOC_TYPE(cmd);
3555 STAILQ_FOREACH(backend, &softc->be_list, links) {
3556 if (backend->type == type) {
3562 printf("ctl: unknown ioctl command %#lx or backend "
3567 retval = backend->ioctl(dev, cmd, addr, flag, td);
3577 ctl_get_initindex(struct ctl_nexus *nexus)
3579 if (nexus->targ_port < CTL_MAX_PORTS)
3580 return (nexus->initid.id +
3581 (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3583 return (nexus->initid.id +
3584 ((nexus->targ_port - CTL_MAX_PORTS) *
3585 CTL_MAX_INIT_PER_PORT));
3589 ctl_get_resindex(struct ctl_nexus *nexus)
3591 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3595 ctl_port_idx(int port_num)
3597 if (port_num < CTL_MAX_PORTS)
3600 return(port_num - CTL_MAX_PORTS);
3604 ctl_map_lun(struct ctl_softc *softc, int port_num, uint32_t lun_id)
3606 struct ctl_port *port;
3608 port = softc->ctl_ports[ctl_port_idx(port_num)];
3610 return (UINT32_MAX);
3611 if (port->lun_map == NULL)
3613 return (port->lun_map(port->targ_lun_arg, lun_id));
3617 ctl_map_lun_back(struct ctl_softc *softc, int port_num, uint32_t lun_id)
3619 struct ctl_port *port;
3622 port = softc->ctl_ports[ctl_port_idx(port_num)];
3623 if (port->lun_map == NULL)
3625 for (i = 0; i < CTL_MAX_LUNS; i++) {
3626 if (port->lun_map(port->targ_lun_arg, i) == lun_id)
3629 return (UINT32_MAX);
3633 * Note: This only works for bitmask sizes that are at least 32 bits, and
3634 * that are a power of 2.
3637 ctl_ffz(uint32_t *mask, uint32_t size)
3639 uint32_t num_chunks, num_pieces;
3642 num_chunks = (size >> 5);
3643 if (num_chunks == 0)
3645 num_pieces = MIN((sizeof(uint32_t) * 8), size);
3647 for (i = 0; i < num_chunks; i++) {
3648 for (j = 0; j < num_pieces; j++) {
3649 if ((mask[i] & (1 << j)) == 0)
3650 return ((i << 5) + j);
3658 ctl_set_mask(uint32_t *mask, uint32_t bit)
3660 uint32_t chunk, piece;
3663 piece = bit % (sizeof(uint32_t) * 8);
3665 if ((mask[chunk] & (1 << piece)) != 0)
3668 mask[chunk] |= (1 << piece);
3674 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3676 uint32_t chunk, piece;
3679 piece = bit % (sizeof(uint32_t) * 8);
3681 if ((mask[chunk] & (1 << piece)) == 0)
3684 mask[chunk] &= ~(1 << piece);
3690 ctl_is_set(uint32_t *mask, uint32_t bit)
3692 uint32_t chunk, piece;
3695 piece = bit % (sizeof(uint32_t) * 8);
3697 if ((mask[chunk] & (1 << piece)) == 0)
3704 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3708 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3711 return (t[residx % CTL_MAX_INIT_PER_PORT]);
3715 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3719 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3722 t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3726 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3731 i = residx/CTL_MAX_INIT_PER_PORT;
3732 if (lun->pr_keys[i] != NULL)
3734 mtx_unlock(&lun->lun_lock);
3735 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3737 mtx_lock(&lun->lun_lock);
3738 if (lun->pr_keys[i] == NULL)
3739 lun->pr_keys[i] = p;
3745 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3749 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3750 KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3751 t[residx % CTL_MAX_INIT_PER_PORT] = key;
3755 * ctl_softc, pool_name, total_ctl_io are passed in.
3756 * npool is passed out.
3759 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3760 uint32_t total_ctl_io, void **npool)
3763 struct ctl_io_pool *pool;
3765 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3770 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3771 pool->ctl_softc = ctl_softc;
3772 pool->zone = uma_zsecond_create(pool->name, NULL,
3773 NULL, NULL, NULL, ctl_softc->io_zone);
3774 /* uma_prealloc(pool->zone, total_ctl_io); */
3778 *npool = ctl_softc->io_zone;
3784 ctl_pool_free(struct ctl_io_pool *pool)
3791 uma_zdestroy(pool->zone);
3797 ctl_alloc_io(void *pool_ref)
3801 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3803 io = uma_zalloc(pool->zone, M_WAITOK);
3805 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK);
3808 io->io_hdr.pool = pool_ref;
3813 ctl_alloc_io_nowait(void *pool_ref)
3817 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3819 io = uma_zalloc(pool->zone, M_NOWAIT);
3821 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT);
3824 io->io_hdr.pool = pool_ref;
3829 ctl_free_io(union ctl_io *io)
3832 struct ctl_io_pool *pool;
3839 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3840 uma_zfree(pool->zone, io);
3842 uma_zfree((uma_zone_t)io->io_hdr.pool, io);
3847 ctl_zero_io(union ctl_io *io)
3855 * May need to preserve linked list pointers at some point too.
3857 pool_ref = io->io_hdr.pool;
3858 memset(io, 0, sizeof(*io));
3859 io->io_hdr.pool = pool_ref;
3863 * This routine is currently used for internal copies of ctl_ios that need
3864 * to persist for some reason after we've already returned status to the
3865 * FETD. (Thus the flag set.)
3868 * Note that this makes a blind copy of all fields in the ctl_io, except
3869 * for the pool reference. This includes any memory that has been
3870 * allocated! That memory will no longer be valid after done has been
3871 * called, so this would be VERY DANGEROUS for command that actually does
3872 * any reads or writes. Right now (11/7/2005), this is only used for immediate
3873 * start and stop commands, which don't transfer any data, so this is not a
3874 * problem. If it is used for anything else, the caller would also need to
3875 * allocate data buffer space and this routine would need to be modified to
3876 * copy the data buffer(s) as well.
3879 ctl_copy_io(union ctl_io *src, union ctl_io *dest)
3888 * May need to preserve linked list pointers at some point too.
3890 pool_ref = dest->io_hdr.pool;
3892 memcpy(dest, src, MIN(sizeof(*src), sizeof(*dest)));
3894 dest->io_hdr.pool = pool_ref;
3896 * We need to know that this is an internal copy, and doesn't need
3897 * to get passed back to the FETD that allocated it.
3899 dest->io_hdr.flags |= CTL_FLAG_INT_COPY;
3903 ctl_expand_number(const char *buf, uint64_t *num)
3909 number = strtoq(buf, &endptr, 0);
3911 switch (tolower((unsigned char)*endptr)) {
3931 case '\0': /* No unit. */
3935 /* Unrecognized unit. */
3939 if ((number << shift) >> shift != number) {
3943 *num = number << shift;
3949 * This routine could be used in the future to load default and/or saved
3950 * mode page parameters for a particuar lun.
3953 ctl_init_page_index(struct ctl_lun *lun)
3956 struct ctl_page_index *page_index;
3960 memcpy(&lun->mode_pages.index, page_index_template,
3961 sizeof(page_index_template));
3963 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3965 page_index = &lun->mode_pages.index[i];
3967 * If this is a disk-only mode page, there's no point in
3968 * setting it up. For some pages, we have to have some
3969 * basic information about the disk in order to calculate the
3972 if ((lun->be_lun->lun_type != T_DIRECT)
3973 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
3976 switch (page_index->page_code & SMPH_PC_MASK) {
3977 case SMS_RW_ERROR_RECOVERY_PAGE: {
3978 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3979 panic("subpage is incorrect!");
3980 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
3981 &rw_er_page_default,
3982 sizeof(rw_er_page_default));
3983 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
3984 &rw_er_page_changeable,
3985 sizeof(rw_er_page_changeable));
3986 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
3987 &rw_er_page_default,
3988 sizeof(rw_er_page_default));
3989 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
3990 &rw_er_page_default,
3991 sizeof(rw_er_page_default));
3992 page_index->page_data =
3993 (uint8_t *)lun->mode_pages.rw_er_page;
3996 case SMS_FORMAT_DEVICE_PAGE: {
3997 struct scsi_format_page *format_page;
3999 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4000 panic("subpage is incorrect!");
4003 * Sectors per track are set above. Bytes per
4004 * sector need to be set here on a per-LUN basis.
4006 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
4007 &format_page_default,
4008 sizeof(format_page_default));
4009 memcpy(&lun->mode_pages.format_page[
4010 CTL_PAGE_CHANGEABLE], &format_page_changeable,
4011 sizeof(format_page_changeable));
4012 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
4013 &format_page_default,
4014 sizeof(format_page_default));
4015 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
4016 &format_page_default,
4017 sizeof(format_page_default));
4019 format_page = &lun->mode_pages.format_page[
4021 scsi_ulto2b(lun->be_lun->blocksize,
4022 format_page->bytes_per_sector);
4024 format_page = &lun->mode_pages.format_page[
4026 scsi_ulto2b(lun->be_lun->blocksize,
4027 format_page->bytes_per_sector);
4029 format_page = &lun->mode_pages.format_page[
4031 scsi_ulto2b(lun->be_lun->blocksize,
4032 format_page->bytes_per_sector);
4034 page_index->page_data =
4035 (uint8_t *)lun->mode_pages.format_page;
4038 case SMS_RIGID_DISK_PAGE: {
4039 struct scsi_rigid_disk_page *rigid_disk_page;
4040 uint32_t sectors_per_cylinder;
4044 #endif /* !__XSCALE__ */
4046 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4047 panic("invalid subpage value %d",
4048 page_index->subpage);
4051 * Rotation rate and sectors per track are set
4052 * above. We calculate the cylinders here based on
4053 * capacity. Due to the number of heads and
4054 * sectors per track we're using, smaller arrays
4055 * may turn out to have 0 cylinders. Linux and
4056 * FreeBSD don't pay attention to these mode pages
4057 * to figure out capacity, but Solaris does. It
4058 * seems to deal with 0 cylinders just fine, and
4059 * works out a fake geometry based on the capacity.
4061 memcpy(&lun->mode_pages.rigid_disk_page[
4062 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4063 sizeof(rigid_disk_page_default));
4064 memcpy(&lun->mode_pages.rigid_disk_page[
4065 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4066 sizeof(rigid_disk_page_changeable));
4068 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4072 * The divide method here will be more accurate,
4073 * probably, but results in floating point being
4074 * used in the kernel on i386 (__udivdi3()). On the
4075 * XScale, though, __udivdi3() is implemented in
4078 * The shift method for cylinder calculation is
4079 * accurate if sectors_per_cylinder is a power of
4080 * 2. Otherwise it might be slightly off -- you
4081 * might have a bit of a truncation problem.
4084 cylinders = (lun->be_lun->maxlba + 1) /
4085 sectors_per_cylinder;
4087 for (shift = 31; shift > 0; shift--) {
4088 if (sectors_per_cylinder & (1 << shift))
4091 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4095 * We've basically got 3 bytes, or 24 bits for the
4096 * cylinder size in the mode page. If we're over,
4097 * just round down to 2^24.
4099 if (cylinders > 0xffffff)
4100 cylinders = 0xffffff;
4102 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4104 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4106 if ((value = ctl_get_opt(&lun->be_lun->options,
4108 scsi_ulto2b(strtol(value, NULL, 0),
4109 rigid_disk_page->rotation_rate);
4112 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
4113 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4114 sizeof(rigid_disk_page_default));
4115 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
4116 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4117 sizeof(rigid_disk_page_default));
4119 page_index->page_data =
4120 (uint8_t *)lun->mode_pages.rigid_disk_page;
4123 case SMS_CACHING_PAGE: {
4124 struct scsi_caching_page *caching_page;
4126 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4127 panic("invalid subpage value %d",
4128 page_index->subpage);
4129 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4130 &caching_page_default,
4131 sizeof(caching_page_default));
4132 memcpy(&lun->mode_pages.caching_page[
4133 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4134 sizeof(caching_page_changeable));
4135 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4136 &caching_page_default,
4137 sizeof(caching_page_default));
4138 caching_page = &lun->mode_pages.caching_page[
4140 value = ctl_get_opt(&lun->be_lun->options, "writecache");
4141 if (value != NULL && strcmp(value, "off") == 0)
4142 caching_page->flags1 &= ~SCP_WCE;
4143 value = ctl_get_opt(&lun->be_lun->options, "readcache");
4144 if (value != NULL && strcmp(value, "off") == 0)
4145 caching_page->flags1 |= SCP_RCD;
4146 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4147 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4148 sizeof(caching_page_default));
4149 page_index->page_data =
4150 (uint8_t *)lun->mode_pages.caching_page;
4153 case SMS_CONTROL_MODE_PAGE: {
4154 struct scsi_control_page *control_page;
4156 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4157 panic("invalid subpage value %d",
4158 page_index->subpage);
4160 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT],
4161 &control_page_default,
4162 sizeof(control_page_default));
4163 memcpy(&lun->mode_pages.control_page[
4164 CTL_PAGE_CHANGEABLE], &control_page_changeable,
4165 sizeof(control_page_changeable));
4166 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED],
4167 &control_page_default,
4168 sizeof(control_page_default));
4169 control_page = &lun->mode_pages.control_page[
4171 value = ctl_get_opt(&lun->be_lun->options, "reordering");
4172 if (value != NULL && strcmp(value, "unrestricted") == 0) {
4173 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK;
4174 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED;
4176 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT],
4177 &lun->mode_pages.control_page[CTL_PAGE_SAVED],
4178 sizeof(control_page_default));
4179 page_index->page_data =
4180 (uint8_t *)lun->mode_pages.control_page;
4184 case SMS_INFO_EXCEPTIONS_PAGE: {
4185 switch (page_index->subpage) {
4186 case SMS_SUBPAGE_PAGE_0:
4187 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4189 sizeof(ie_page_default));
4190 memcpy(&lun->mode_pages.ie_page[
4191 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4192 sizeof(ie_page_changeable));
4193 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4195 sizeof(ie_page_default));
4196 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4198 sizeof(ie_page_default));
4199 page_index->page_data =
4200 (uint8_t *)lun->mode_pages.ie_page;
4203 struct ctl_logical_block_provisioning_page *page;
4205 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4207 sizeof(lbp_page_default));
4208 memcpy(&lun->mode_pages.lbp_page[
4209 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4210 sizeof(lbp_page_changeable));
4211 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4213 sizeof(lbp_page_default));
4214 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4215 value = ctl_get_opt(&lun->be_lun->options,
4217 if (value != NULL &&
4218 ctl_expand_number(value, &ival) == 0) {
4219 page->descr[0].flags |= SLBPPD_ENABLED |
4221 if (lun->be_lun->blocksize)
4222 ival /= lun->be_lun->blocksize;
4225 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4226 page->descr[0].count);
4228 value = ctl_get_opt(&lun->be_lun->options,
4230 if (value != NULL &&
4231 ctl_expand_number(value, &ival) == 0) {
4232 page->descr[1].flags |= SLBPPD_ENABLED |
4234 if (lun->be_lun->blocksize)
4235 ival /= lun->be_lun->blocksize;
4238 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4239 page->descr[1].count);
4241 value = ctl_get_opt(&lun->be_lun->options,
4242 "pool-avail-threshold");
4243 if (value != NULL &&
4244 ctl_expand_number(value, &ival) == 0) {
4245 page->descr[2].flags |= SLBPPD_ENABLED |
4247 if (lun->be_lun->blocksize)
4248 ival /= lun->be_lun->blocksize;
4251 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4252 page->descr[2].count);
4254 value = ctl_get_opt(&lun->be_lun->options,
4255 "pool-used-threshold");
4256 if (value != NULL &&
4257 ctl_expand_number(value, &ival) == 0) {
4258 page->descr[3].flags |= SLBPPD_ENABLED |
4260 if (lun->be_lun->blocksize)
4261 ival /= lun->be_lun->blocksize;
4264 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4265 page->descr[3].count);
4267 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4268 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4269 sizeof(lbp_page_default));
4270 page_index->page_data =
4271 (uint8_t *)lun->mode_pages.lbp_page;
4275 case SMS_VENDOR_SPECIFIC_PAGE:{
4276 switch (page_index->subpage) {
4277 case DBGCNF_SUBPAGE_CODE: {
4278 struct copan_debugconf_subpage *current_page,
4281 memcpy(&lun->mode_pages.debugconf_subpage[
4283 &debugconf_page_default,
4284 sizeof(debugconf_page_default));
4285 memcpy(&lun->mode_pages.debugconf_subpage[
4286 CTL_PAGE_CHANGEABLE],
4287 &debugconf_page_changeable,
4288 sizeof(debugconf_page_changeable));
4289 memcpy(&lun->mode_pages.debugconf_subpage[
4291 &debugconf_page_default,
4292 sizeof(debugconf_page_default));
4293 memcpy(&lun->mode_pages.debugconf_subpage[
4295 &debugconf_page_default,
4296 sizeof(debugconf_page_default));
4297 page_index->page_data =
4298 (uint8_t *)lun->mode_pages.debugconf_subpage;
4300 current_page = (struct copan_debugconf_subpage *)
4301 (page_index->page_data +
4302 (page_index->page_len *
4304 saved_page = (struct copan_debugconf_subpage *)
4305 (page_index->page_data +
4306 (page_index->page_len *
4311 panic("invalid subpage value %d",
4312 page_index->subpage);
4318 panic("invalid page value %d",
4319 page_index->page_code & SMPH_PC_MASK);
4324 return (CTL_RETVAL_COMPLETE);
4328 ctl_init_log_page_index(struct ctl_lun *lun)
4330 struct ctl_page_index *page_index;
4333 memcpy(&lun->log_pages.index, log_page_index_template,
4334 sizeof(log_page_index_template));
4337 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4339 page_index = &lun->log_pages.index[i];
4341 * If this is a disk-only mode page, there's no point in
4342 * setting it up. For some pages, we have to have some
4343 * basic information about the disk in order to calculate the
4346 if ((lun->be_lun->lun_type != T_DIRECT)
4347 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
4350 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4351 lun->backend->lun_attr == NULL)
4354 if (page_index->page_code != prev) {
4355 lun->log_pages.pages_page[j] = page_index->page_code;
4356 prev = page_index->page_code;
4359 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4360 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4363 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4364 lun->log_pages.index[0].page_len = j;
4365 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4366 lun->log_pages.index[1].page_len = k * 2;
4367 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4368 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4370 return (CTL_RETVAL_COMPLETE);
4374 hex2bin(const char *str, uint8_t *buf, int buf_size)
4379 memset(buf, 0, buf_size);
4380 while (isspace(str[0]))
4382 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4385 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4389 else if (isalpha(c))
4390 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4396 buf[i / 2] |= (c << 4);
4400 return ((i + 1) / 2);
4407 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4408 * wants us to allocate the LUN and he can block.
4409 * - ctl_softc is always set
4410 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4412 * Returns 0 for success, non-zero (errno) for failure.
4415 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4416 struct ctl_be_lun *const be_lun, struct ctl_id target_id)
4418 struct ctl_lun *nlun, *lun;
4419 struct ctl_port *port;
4420 struct scsi_vpd_id_descriptor *desc;
4421 struct scsi_vpd_id_t10 *t10id;
4422 const char *eui, *naa, *scsiname, *vendor, *value;
4423 int lun_number, i, lun_malloced;
4424 int devidlen, idlen1, idlen2 = 0, len;
4430 * We currently only support Direct Access or Processor LUN types.
4432 switch (be_lun->lun_type) {
4440 be_lun->lun_config_status(be_lun->be_lun,
4441 CTL_LUN_CONFIG_FAILURE);
4444 if (ctl_lun == NULL) {
4445 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4452 memset(lun, 0, sizeof(*lun));
4454 lun->flags = CTL_LUN_MALLOCED;
4456 /* Generate LUN ID. */
4457 devidlen = max(CTL_DEVID_MIN_LEN,
4458 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4459 idlen1 = sizeof(*t10id) + devidlen;
4460 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4461 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4462 if (scsiname != NULL) {
4463 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4464 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4466 eui = ctl_get_opt(&be_lun->options, "eui");
4468 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4470 naa = ctl_get_opt(&be_lun->options, "naa");
4472 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4474 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4475 M_CTL, M_WAITOK | M_ZERO);
4476 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4477 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4478 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4479 desc->length = idlen1;
4480 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4481 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4482 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4483 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4485 strncpy(t10id->vendor, vendor,
4486 min(sizeof(t10id->vendor), strlen(vendor)));
4488 strncpy((char *)t10id->vendor_spec_id,
4489 (char *)be_lun->device_id, devidlen);
4490 if (scsiname != NULL) {
4491 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4493 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4494 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4495 SVPD_ID_TYPE_SCSI_NAME;
4496 desc->length = idlen2;
4497 strlcpy(desc->identifier, scsiname, idlen2);
4500 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4502 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4503 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4505 desc->length = hex2bin(eui, desc->identifier, 16);
4506 desc->length = desc->length > 12 ? 16 :
4507 (desc->length > 8 ? 12 : 8);
4508 len -= 16 - desc->length;
4511 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4513 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4514 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4516 desc->length = hex2bin(naa, desc->identifier, 16);
4517 desc->length = desc->length > 8 ? 16 : 8;
4518 len -= 16 - desc->length;
4520 lun->lun_devid->len = len;
4522 mtx_lock(&ctl_softc->ctl_lock);
4524 * See if the caller requested a particular LUN number. If so, see
4525 * if it is available. Otherwise, allocate the first available LUN.
4527 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4528 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4529 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4530 mtx_unlock(&ctl_softc->ctl_lock);
4531 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4532 printf("ctl: requested LUN ID %d is higher "
4533 "than CTL_MAX_LUNS - 1 (%d)\n",
4534 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4537 * XXX KDM return an error, or just assign
4538 * another LUN ID in this case??
4540 printf("ctl: requested LUN ID %d is already "
4541 "in use\n", be_lun->req_lun_id);
4543 if (lun->flags & CTL_LUN_MALLOCED)
4545 be_lun->lun_config_status(be_lun->be_lun,
4546 CTL_LUN_CONFIG_FAILURE);
4549 lun_number = be_lun->req_lun_id;
4551 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS);
4552 if (lun_number == -1) {
4553 mtx_unlock(&ctl_softc->ctl_lock);
4554 printf("ctl: can't allocate LUN on target %ju, out of "
4555 "LUNs\n", (uintmax_t)target_id.id);
4556 if (lun->flags & CTL_LUN_MALLOCED)
4558 be_lun->lun_config_status(be_lun->be_lun,
4559 CTL_LUN_CONFIG_FAILURE);
4563 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4565 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4566 lun->target = target_id;
4567 lun->lun = lun_number;
4568 lun->be_lun = be_lun;
4570 * The processor LUN is always enabled. Disk LUNs come on line
4571 * disabled, and must be enabled by the backend.
4573 lun->flags |= CTL_LUN_DISABLED;
4574 lun->backend = be_lun->be;
4575 be_lun->ctl_lun = lun;
4576 be_lun->lun_id = lun_number;
4577 atomic_add_int(&be_lun->be->num_luns, 1);
4578 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE)
4579 lun->flags |= CTL_LUN_OFFLINE;
4581 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4582 lun->flags |= CTL_LUN_STOPPED;
4584 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4585 lun->flags |= CTL_LUN_INOPERABLE;
4587 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4588 lun->flags |= CTL_LUN_PRIMARY_SC;
4590 value = ctl_get_opt(&be_lun->options, "readonly");
4591 if (value != NULL && strcmp(value, "on") == 0)
4592 lun->flags |= CTL_LUN_READONLY;
4594 lun->serseq = CTL_LUN_SERSEQ_OFF;
4595 if (be_lun->flags & CTL_LUN_FLAG_SERSEQ_READ)
4596 lun->serseq = CTL_LUN_SERSEQ_READ;
4597 value = ctl_get_opt(&be_lun->options, "serseq");
4598 if (value != NULL && strcmp(value, "on") == 0)
4599 lun->serseq = CTL_LUN_SERSEQ_ON;
4600 else if (value != NULL && strcmp(value, "read") == 0)
4601 lun->serseq = CTL_LUN_SERSEQ_READ;
4602 else if (value != NULL && strcmp(value, "off") == 0)
4603 lun->serseq = CTL_LUN_SERSEQ_OFF;
4605 lun->ctl_softc = ctl_softc;
4606 TAILQ_INIT(&lun->ooa_queue);
4607 TAILQ_INIT(&lun->blocked_queue);
4608 STAILQ_INIT(&lun->error_list);
4609 ctl_tpc_lun_init(lun);
4612 * Initialize the mode and log page index.
4614 ctl_init_page_index(lun);
4615 ctl_init_log_page_index(lun);
4618 * Now, before we insert this lun on the lun list, set the lun
4619 * inventory changed UA for all other luns.
4621 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4622 mtx_lock(&nlun->lun_lock);
4623 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4624 mtx_unlock(&nlun->lun_lock);
4627 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4629 ctl_softc->ctl_luns[lun_number] = lun;
4631 ctl_softc->num_luns++;
4633 /* Setup statistics gathering */
4634 lun->stats.device_type = be_lun->lun_type;
4635 lun->stats.lun_number = lun_number;
4636 if (lun->stats.device_type == T_DIRECT)
4637 lun->stats.blocksize = be_lun->blocksize;
4639 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4640 for (i = 0;i < CTL_MAX_PORTS;i++)
4641 lun->stats.ports[i].targ_port = i;
4643 mtx_unlock(&ctl_softc->ctl_lock);
4645 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4648 * Run through each registered FETD and bring it online if it isn't
4649 * already. Enable the target ID if it hasn't been enabled, and
4650 * enable this particular LUN.
4652 STAILQ_FOREACH(port, &ctl_softc->port_list, links) {
4655 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number);
4657 printf("ctl_alloc_lun: FETD %s port %d returned error "
4658 "%d for lun_enable on target %ju lun %d\n",
4659 port->port_name, port->targ_port, retval,
4660 (uintmax_t)target_id.id, lun_number);
4662 port->status |= CTL_PORT_STATUS_LUN_ONLINE;
4670 * - LUN has already been marked invalid and any pending I/O has been taken
4674 ctl_free_lun(struct ctl_lun *lun)
4676 struct ctl_softc *softc;
4678 struct ctl_port *port;
4680 struct ctl_lun *nlun;
4683 softc = lun->ctl_softc;
4685 mtx_assert(&softc->ctl_lock, MA_OWNED);
4687 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4689 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4691 softc->ctl_luns[lun->lun] = NULL;
4693 if (!TAILQ_EMPTY(&lun->ooa_queue))
4694 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4699 * XXX KDM this scheme only works for a single target/multiple LUN
4700 * setup. It needs to be revamped for a multiple target scheme.
4702 * XXX KDM this results in port->lun_disable() getting called twice,
4703 * once when ctl_disable_lun() is called, and a second time here.
4704 * We really need to re-think the LUN disable semantics. There
4705 * should probably be several steps/levels to LUN removal:
4710 * Right now we only have a disable method when communicating to
4711 * the front end ports, at least for individual LUNs.
4714 STAILQ_FOREACH(port, &softc->port_list, links) {
4717 retval = port->lun_disable(port->targ_lun_arg, lun->target,
4720 printf("ctl_free_lun: FETD %s port %d returned error "
4721 "%d for lun_disable on target %ju lun %jd\n",
4722 port->port_name, port->targ_port, retval,
4723 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4726 if (STAILQ_FIRST(&softc->lun_list) == NULL) {
4727 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE;
4729 retval = port->targ_disable(port->targ_lun_arg,lun->target);
4731 printf("ctl_free_lun: FETD %s port %d "
4732 "returned error %d for targ_disable on "
4733 "target %ju\n", port->port_name,
4734 port->targ_port, retval,
4735 (uintmax_t)lun->target.id);
4737 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE;
4739 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0)
4743 port->port_offline(port->onoff_arg);
4744 port->status &= ~CTL_PORT_STATUS_ONLINE;
4751 * Tell the backend to free resources, if this LUN has a backend.
4753 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4754 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4756 ctl_tpc_lun_shutdown(lun);
4757 mtx_destroy(&lun->lun_lock);
4758 free(lun->lun_devid, M_CTL);
4759 for (i = 0; i < CTL_MAX_PORTS; i++)
4760 free(lun->pending_ua[i], M_CTL);
4761 for (i = 0; i < 2 * CTL_MAX_PORTS; i++)
4762 free(lun->pr_keys[i], M_CTL);
4763 free(lun->write_buffer, M_CTL);
4764 if (lun->flags & CTL_LUN_MALLOCED)
4767 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4768 mtx_lock(&nlun->lun_lock);
4769 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4770 mtx_unlock(&nlun->lun_lock);
4777 ctl_create_lun(struct ctl_be_lun *be_lun)
4779 struct ctl_softc *softc;
4781 softc = control_softc;
4784 * ctl_alloc_lun() should handle all potential failure cases.
4786 ctl_alloc_lun(softc, NULL, be_lun, softc->target);
4790 ctl_add_lun(struct ctl_be_lun *be_lun)
4792 struct ctl_softc *softc = control_softc;
4794 mtx_lock(&softc->ctl_lock);
4795 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links);
4796 mtx_unlock(&softc->ctl_lock);
4797 wakeup(&softc->pending_lun_queue);
4803 ctl_enable_lun(struct ctl_be_lun *be_lun)
4805 struct ctl_softc *softc;
4806 struct ctl_port *port, *nport;
4807 struct ctl_lun *lun;
4810 lun = (struct ctl_lun *)be_lun->ctl_lun;
4811 softc = lun->ctl_softc;
4813 mtx_lock(&softc->ctl_lock);
4814 mtx_lock(&lun->lun_lock);
4815 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4817 * eh? Why did we get called if the LUN is already
4820 mtx_unlock(&lun->lun_lock);
4821 mtx_unlock(&softc->ctl_lock);
4824 lun->flags &= ~CTL_LUN_DISABLED;
4825 mtx_unlock(&lun->lun_lock);
4827 for (port = STAILQ_FIRST(&softc->port_list); port != NULL; port = nport) {
4828 nport = STAILQ_NEXT(port, links);
4831 * Drop the lock while we call the FETD's enable routine.
4832 * This can lead to a callback into CTL (at least in the
4833 * case of the internal initiator frontend.
4835 mtx_unlock(&softc->ctl_lock);
4836 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun);
4837 mtx_lock(&softc->ctl_lock);
4839 printf("%s: FETD %s port %d returned error "
4840 "%d for lun_enable on target %ju lun %jd\n",
4841 __func__, port->port_name, port->targ_port, retval,
4842 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4846 /* NOTE: TODO: why does lun enable affect port status? */
4847 port->status |= CTL_PORT_STATUS_LUN_ONLINE;
4852 mtx_unlock(&softc->ctl_lock);
4858 ctl_disable_lun(struct ctl_be_lun *be_lun)
4860 struct ctl_softc *softc;
4861 struct ctl_port *port;
4862 struct ctl_lun *lun;
4865 lun = (struct ctl_lun *)be_lun->ctl_lun;
4866 softc = lun->ctl_softc;
4868 mtx_lock(&softc->ctl_lock);
4869 mtx_lock(&lun->lun_lock);
4870 if (lun->flags & CTL_LUN_DISABLED) {
4871 mtx_unlock(&lun->lun_lock);
4872 mtx_unlock(&softc->ctl_lock);
4875 lun->flags |= CTL_LUN_DISABLED;
4876 mtx_unlock(&lun->lun_lock);
4878 STAILQ_FOREACH(port, &softc->port_list, links) {
4879 mtx_unlock(&softc->ctl_lock);
4881 * Drop the lock before we call the frontend's disable
4882 * routine, to avoid lock order reversals.
4884 * XXX KDM what happens if the frontend list changes while
4885 * we're traversing it? It's unlikely, but should be handled.
4887 retval = port->lun_disable(port->targ_lun_arg, lun->target,
4889 mtx_lock(&softc->ctl_lock);
4891 printf("ctl_alloc_lun: FETD %s port %d returned error "
4892 "%d for lun_disable on target %ju lun %jd\n",
4893 port->port_name, port->targ_port, retval,
4894 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4898 mtx_unlock(&softc->ctl_lock);
4904 ctl_start_lun(struct ctl_be_lun *be_lun)
4906 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4908 mtx_lock(&lun->lun_lock);
4909 lun->flags &= ~CTL_LUN_STOPPED;
4910 mtx_unlock(&lun->lun_lock);
4915 ctl_stop_lun(struct ctl_be_lun *be_lun)
4917 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4919 mtx_lock(&lun->lun_lock);
4920 lun->flags |= CTL_LUN_STOPPED;
4921 mtx_unlock(&lun->lun_lock);
4926 ctl_lun_offline(struct ctl_be_lun *be_lun)
4928 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4930 mtx_lock(&lun->lun_lock);
4931 lun->flags |= CTL_LUN_OFFLINE;
4932 mtx_unlock(&lun->lun_lock);
4937 ctl_lun_online(struct ctl_be_lun *be_lun)
4939 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4941 mtx_lock(&lun->lun_lock);
4942 lun->flags &= ~CTL_LUN_OFFLINE;
4943 mtx_unlock(&lun->lun_lock);
4948 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4950 struct ctl_softc *softc;
4951 struct ctl_lun *lun;
4953 lun = (struct ctl_lun *)be_lun->ctl_lun;
4954 softc = lun->ctl_softc;
4956 mtx_lock(&lun->lun_lock);
4959 * The LUN needs to be disabled before it can be marked invalid.
4961 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4962 mtx_unlock(&lun->lun_lock);
4966 * Mark the LUN invalid.
4968 lun->flags |= CTL_LUN_INVALID;
4971 * If there is nothing in the OOA queue, go ahead and free the LUN.
4972 * If we have something in the OOA queue, we'll free it when the
4973 * last I/O completes.
4975 if (TAILQ_EMPTY(&lun->ooa_queue)) {
4976 mtx_unlock(&lun->lun_lock);
4977 mtx_lock(&softc->ctl_lock);
4979 mtx_unlock(&softc->ctl_lock);
4981 mtx_unlock(&lun->lun_lock);
4987 ctl_lun_inoperable(struct ctl_be_lun *be_lun)
4989 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4991 mtx_lock(&lun->lun_lock);
4992 lun->flags |= CTL_LUN_INOPERABLE;
4993 mtx_unlock(&lun->lun_lock);
4998 ctl_lun_operable(struct ctl_be_lun *be_lun)
5000 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5002 mtx_lock(&lun->lun_lock);
5003 lun->flags &= ~CTL_LUN_INOPERABLE;
5004 mtx_unlock(&lun->lun_lock);
5009 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
5011 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5013 mtx_lock(&lun->lun_lock);
5014 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGED);
5015 mtx_unlock(&lun->lun_lock);
5019 * Backend "memory move is complete" callback for requests that never
5020 * make it down to say RAIDCore's configuration code.
5023 ctl_config_move_done(union ctl_io *io)
5027 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
5028 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
5029 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
5031 if ((io->io_hdr.port_status != 0) &&
5032 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5033 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5035 * For hardware error sense keys, the sense key
5036 * specific value is defined to be a retry count,
5037 * but we use it to pass back an internal FETD
5038 * error code. XXX KDM Hopefully the FETD is only
5039 * using 16 bits for an error code, since that's
5040 * all the space we have in the sks field.
5042 ctl_set_internal_failure(&io->scsiio,
5045 io->io_hdr.port_status);
5048 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
5049 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5050 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
5051 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5053 * XXX KDM just assuming a single pointer here, and not a
5054 * S/G list. If we start using S/G lists for config data,
5055 * we'll need to know how to clean them up here as well.
5057 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5058 free(io->scsiio.kern_data_ptr, M_CTL);
5060 retval = CTL_RETVAL_COMPLETE;
5063 * XXX KDM now we need to continue data movement. Some
5065 * - call ctl_scsiio() again? We don't do this for data
5066 * writes, because for those at least we know ahead of
5067 * time where the write will go and how long it is. For
5068 * config writes, though, that information is largely
5069 * contained within the write itself, thus we need to
5070 * parse out the data again.
5072 * - Call some other function once the data is in?
5074 if (ctl_debug & CTL_DEBUG_CDB_DATA)
5078 * XXX KDM call ctl_scsiio() again for now, and check flag
5079 * bits to see whether we're allocated or not.
5081 retval = ctl_scsiio(&io->scsiio);
5087 * This gets called by a backend driver when it is done with a
5088 * data_submit method.
5091 ctl_data_submit_done(union ctl_io *io)
5094 * If the IO_CONT flag is set, we need to call the supplied
5095 * function to continue processing the I/O, instead of completing
5098 * If there is an error, though, we don't want to keep processing.
5099 * Instead, just send status back to the initiator.
5101 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5102 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5103 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5104 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5105 io->scsiio.io_cont(io);
5112 * This gets called by a backend driver when it is done with a
5113 * configuration write.
5116 ctl_config_write_done(union ctl_io *io)
5121 * If the IO_CONT flag is set, we need to call the supplied
5122 * function to continue processing the I/O, instead of completing
5125 * If there is an error, though, we don't want to keep processing.
5126 * Instead, just send status back to the initiator.
5128 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5129 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5130 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5131 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5132 io->scsiio.io_cont(io);
5136 * Since a configuration write can be done for commands that actually
5137 * have data allocated, like write buffer, and commands that have
5138 * no data, like start/stop unit, we need to check here.
5140 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5141 buf = io->scsiio.kern_data_ptr;
5150 ctl_config_read_done(union ctl_io *io)
5155 * If there is some error -- we are done, skip data transfer.
5157 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
5158 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5159 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
5160 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5161 buf = io->scsiio.kern_data_ptr;
5171 * If the IO_CONT flag is set, we need to call the supplied
5172 * function to continue processing the I/O, instead of completing
5175 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
5176 io->scsiio.io_cont(io);
5184 * SCSI release command.
5187 ctl_scsi_release(struct ctl_scsiio *ctsio)
5189 int length, longid, thirdparty_id, resv_id;
5190 struct ctl_lun *lun;
5196 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5198 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5199 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5201 switch (ctsio->cdb[0]) {
5203 struct scsi_release_10 *cdb;
5205 cdb = (struct scsi_release_10 *)ctsio->cdb;
5207 if (cdb->byte2 & SR10_LONGID)
5210 thirdparty_id = cdb->thirdparty_id;
5212 resv_id = cdb->resv_id;
5213 length = scsi_2btoul(cdb->length);
5220 * XXX KDM right now, we only support LUN reservation. We don't
5221 * support 3rd party reservations, or extent reservations, which
5222 * might actually need the parameter list. If we've gotten this
5223 * far, we've got a LUN reservation. Anything else got kicked out
5224 * above. So, according to SPC, ignore the length.
5228 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5230 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5231 ctsio->kern_data_len = length;
5232 ctsio->kern_total_len = length;
5233 ctsio->kern_data_resid = 0;
5234 ctsio->kern_rel_offset = 0;
5235 ctsio->kern_sg_entries = 0;
5236 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5237 ctsio->be_move_done = ctl_config_move_done;
5238 ctl_datamove((union ctl_io *)ctsio);
5240 return (CTL_RETVAL_COMPLETE);
5244 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5246 mtx_lock(&lun->lun_lock);
5249 * According to SPC, it is not an error for an intiator to attempt
5250 * to release a reservation on a LUN that isn't reserved, or that
5251 * is reserved by another initiator. The reservation can only be
5252 * released, though, by the initiator who made it or by one of
5253 * several reset type events.
5255 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5256 lun->flags &= ~CTL_LUN_RESERVED;
5258 mtx_unlock(&lun->lun_lock);
5260 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5261 free(ctsio->kern_data_ptr, M_CTL);
5262 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5265 ctl_set_success(ctsio);
5266 ctl_done((union ctl_io *)ctsio);
5267 return (CTL_RETVAL_COMPLETE);
5271 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5273 int extent, thirdparty, longid;
5274 int resv_id, length;
5275 uint64_t thirdparty_id;
5276 struct ctl_lun *lun;
5286 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5288 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5289 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5291 switch (ctsio->cdb[0]) {
5293 struct scsi_reserve_10 *cdb;
5295 cdb = (struct scsi_reserve_10 *)ctsio->cdb;
5297 if (cdb->byte2 & SR10_LONGID)
5300 thirdparty_id = cdb->thirdparty_id;
5302 resv_id = cdb->resv_id;
5303 length = scsi_2btoul(cdb->length);
5309 * XXX KDM right now, we only support LUN reservation. We don't
5310 * support 3rd party reservations, or extent reservations, which
5311 * might actually need the parameter list. If we've gotten this
5312 * far, we've got a LUN reservation. Anything else got kicked out
5313 * above. So, according to SPC, ignore the length.
5317 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5319 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5320 ctsio->kern_data_len = length;
5321 ctsio->kern_total_len = length;
5322 ctsio->kern_data_resid = 0;
5323 ctsio->kern_rel_offset = 0;
5324 ctsio->kern_sg_entries = 0;
5325 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5326 ctsio->be_move_done = ctl_config_move_done;
5327 ctl_datamove((union ctl_io *)ctsio);
5329 return (CTL_RETVAL_COMPLETE);
5333 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5335 mtx_lock(&lun->lun_lock);
5336 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5337 ctl_set_reservation_conflict(ctsio);
5341 lun->flags |= CTL_LUN_RESERVED;
5342 lun->res_idx = residx;
5344 ctl_set_success(ctsio);
5347 mtx_unlock(&lun->lun_lock);
5349 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5350 free(ctsio->kern_data_ptr, M_CTL);
5351 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5354 ctl_done((union ctl_io *)ctsio);
5355 return (CTL_RETVAL_COMPLETE);
5359 ctl_start_stop(struct ctl_scsiio *ctsio)
5361 struct scsi_start_stop_unit *cdb;
5362 struct ctl_lun *lun;
5365 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5367 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5370 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5374 * We don't support the immediate bit on a stop unit. In order to
5375 * do that, we would need to code up a way to know that a stop is
5376 * pending, and hold off any new commands until it completes, one
5377 * way or another. Then we could accept or reject those commands
5378 * depending on its status. We would almost need to do the reverse
5379 * of what we do below for an immediate start -- return the copy of
5380 * the ctl_io to the FETD with status to send to the host (and to
5381 * free the copy!) and then free the original I/O once the stop
5382 * actually completes. That way, the OOA queue mechanism can work
5383 * to block commands that shouldn't proceed. Another alternative
5384 * would be to put the copy in the queue in place of the original,
5385 * and return the original back to the caller. That could be
5388 if ((cdb->byte2 & SSS_IMMED)
5389 && ((cdb->how & SSS_START) == 0)) {
5390 ctl_set_invalid_field(ctsio,
5396 ctl_done((union ctl_io *)ctsio);
5397 return (CTL_RETVAL_COMPLETE);
5400 if ((lun->flags & CTL_LUN_PR_RESERVED)
5401 && ((cdb->how & SSS_START)==0)) {
5404 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5405 if (ctl_get_prkey(lun, residx) == 0
5406 || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5408 ctl_set_reservation_conflict(ctsio);
5409 ctl_done((union ctl_io *)ctsio);
5410 return (CTL_RETVAL_COMPLETE);
5415 * If there is no backend on this device, we can't start or stop
5416 * it. In theory we shouldn't get any start/stop commands in the
5417 * first place at this level if the LUN doesn't have a backend.
5418 * That should get stopped by the command decode code.
5420 if (lun->backend == NULL) {
5421 ctl_set_invalid_opcode(ctsio);
5422 ctl_done((union ctl_io *)ctsio);
5423 return (CTL_RETVAL_COMPLETE);
5427 * XXX KDM Copan-specific offline behavior.
5428 * Figure out a reasonable way to port this?
5431 mtx_lock(&lun->lun_lock);
5433 if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
5434 && (lun->flags & CTL_LUN_OFFLINE)) {
5436 * If the LUN is offline, and the on/offline bit isn't set,
5437 * reject the start or stop. Otherwise, let it through.
5439 mtx_unlock(&lun->lun_lock);
5440 ctl_set_lun_not_ready(ctsio);
5441 ctl_done((union ctl_io *)ctsio);
5443 mtx_unlock(&lun->lun_lock);
5444 #endif /* NEEDTOPORT */
5446 * This could be a start or a stop when we're online,
5447 * or a stop/offline or start/online. A start or stop when
5448 * we're offline is covered in the case above.
5451 * In the non-immediate case, we send the request to
5452 * the backend and return status to the user when
5455 * In the immediate case, we allocate a new ctl_io
5456 * to hold a copy of the request, and send that to
5457 * the backend. We then set good status on the
5458 * user's request and return it immediately.
5460 if (cdb->byte2 & SSS_IMMED) {
5461 union ctl_io *new_io;
5463 new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5464 ctl_copy_io((union ctl_io *)ctsio, new_io);
5465 retval = lun->backend->config_write(new_io);
5466 ctl_set_success(ctsio);
5467 ctl_done((union ctl_io *)ctsio);
5469 retval = lun->backend->config_write(
5470 (union ctl_io *)ctsio);
5479 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5480 * we don't really do anything with the LBA and length fields if the user
5481 * passes them in. Instead we'll just flush out the cache for the entire
5485 ctl_sync_cache(struct ctl_scsiio *ctsio)
5487 struct ctl_lun *lun;
5488 struct ctl_softc *softc;
5489 uint64_t starting_lba;
5490 uint32_t block_count;
5493 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5495 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5496 softc = lun->ctl_softc;
5499 switch (ctsio->cdb[0]) {
5500 case SYNCHRONIZE_CACHE: {
5501 struct scsi_sync_cache *cdb;
5502 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5504 starting_lba = scsi_4btoul(cdb->begin_lba);
5505 block_count = scsi_2btoul(cdb->lb_count);
5508 case SYNCHRONIZE_CACHE_16: {
5509 struct scsi_sync_cache_16 *cdb;
5510 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5512 starting_lba = scsi_8btou64(cdb->begin_lba);
5513 block_count = scsi_4btoul(cdb->lb_count);
5517 ctl_set_invalid_opcode(ctsio);
5518 ctl_done((union ctl_io *)ctsio);
5520 break; /* NOTREACHED */
5524 * We check the LBA and length, but don't do anything with them.
5525 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5526 * get flushed. This check will just help satisfy anyone who wants
5527 * to see an error for an out of range LBA.
5529 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5530 ctl_set_lba_out_of_range(ctsio);
5531 ctl_done((union ctl_io *)ctsio);
5536 * If this LUN has no backend, we can't flush the cache anyway.
5538 if (lun->backend == NULL) {
5539 ctl_set_invalid_opcode(ctsio);
5540 ctl_done((union ctl_io *)ctsio);
5545 * Check to see whether we're configured to send the SYNCHRONIZE
5546 * CACHE command directly to the back end.
5548 mtx_lock(&lun->lun_lock);
5549 if ((softc->flags & CTL_FLAG_REAL_SYNC)
5550 && (++(lun->sync_count) >= lun->sync_interval)) {
5551 lun->sync_count = 0;
5552 mtx_unlock(&lun->lun_lock);
5553 retval = lun->backend->config_write((union ctl_io *)ctsio);
5555 mtx_unlock(&lun->lun_lock);
5556 ctl_set_success(ctsio);
5557 ctl_done((union ctl_io *)ctsio);
5566 ctl_format(struct ctl_scsiio *ctsio)
5568 struct scsi_format *cdb;
5569 struct ctl_lun *lun;
5570 int length, defect_list_len;
5572 CTL_DEBUG_PRINT(("ctl_format\n"));
5574 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5576 cdb = (struct scsi_format *)ctsio->cdb;
5579 if (cdb->byte2 & SF_FMTDATA) {
5580 if (cdb->byte2 & SF_LONGLIST)
5581 length = sizeof(struct scsi_format_header_long);
5583 length = sizeof(struct scsi_format_header_short);
5586 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5588 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5589 ctsio->kern_data_len = length;
5590 ctsio->kern_total_len = length;
5591 ctsio->kern_data_resid = 0;
5592 ctsio->kern_rel_offset = 0;
5593 ctsio->kern_sg_entries = 0;
5594 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5595 ctsio->be_move_done = ctl_config_move_done;
5596 ctl_datamove((union ctl_io *)ctsio);
5598 return (CTL_RETVAL_COMPLETE);
5601 defect_list_len = 0;
5603 if (cdb->byte2 & SF_FMTDATA) {
5604 if (cdb->byte2 & SF_LONGLIST) {
5605 struct scsi_format_header_long *header;
5607 header = (struct scsi_format_header_long *)
5608 ctsio->kern_data_ptr;
5610 defect_list_len = scsi_4btoul(header->defect_list_len);
5611 if (defect_list_len != 0) {
5612 ctl_set_invalid_field(ctsio,
5621 struct scsi_format_header_short *header;
5623 header = (struct scsi_format_header_short *)
5624 ctsio->kern_data_ptr;
5626 defect_list_len = scsi_2btoul(header->defect_list_len);
5627 if (defect_list_len != 0) {
5628 ctl_set_invalid_field(ctsio,
5640 * The format command will clear out the "Medium format corrupted"
5641 * status if set by the configuration code. That status is really
5642 * just a way to notify the host that we have lost the media, and
5643 * get them to issue a command that will basically make them think
5644 * they're blowing away the media.
5646 mtx_lock(&lun->lun_lock);
5647 lun->flags &= ~CTL_LUN_INOPERABLE;
5648 mtx_unlock(&lun->lun_lock);
5650 ctl_set_success(ctsio);
5653 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5654 free(ctsio->kern_data_ptr, M_CTL);
5655 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5658 ctl_done((union ctl_io *)ctsio);
5659 return (CTL_RETVAL_COMPLETE);
5663 ctl_read_buffer(struct ctl_scsiio *ctsio)
5665 struct scsi_read_buffer *cdb;
5666 struct ctl_lun *lun;
5667 int buffer_offset, len;
5668 static uint8_t descr[4];
5669 static uint8_t echo_descr[4] = { 0 };
5671 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5673 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5674 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5676 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA &&
5677 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR &&
5678 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) {
5679 ctl_set_invalid_field(ctsio,
5685 ctl_done((union ctl_io *)ctsio);
5686 return (CTL_RETVAL_COMPLETE);
5689 len = scsi_3btoul(cdb->length);
5690 buffer_offset = scsi_3btoul(cdb->offset);
5692 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5693 ctl_set_invalid_field(ctsio,
5699 ctl_done((union ctl_io *)ctsio);
5700 return (CTL_RETVAL_COMPLETE);
5703 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5705 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5706 ctsio->kern_data_ptr = descr;
5707 len = min(len, sizeof(descr));
5708 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5709 ctsio->kern_data_ptr = echo_descr;
5710 len = min(len, sizeof(echo_descr));
5712 if (lun->write_buffer == NULL) {
5713 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5716 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5718 ctsio->kern_data_len = len;
5719 ctsio->kern_total_len = len;
5720 ctsio->kern_data_resid = 0;
5721 ctsio->kern_rel_offset = 0;
5722 ctsio->kern_sg_entries = 0;
5723 ctl_set_success(ctsio);
5724 ctsio->be_move_done = ctl_config_move_done;
5725 ctl_datamove((union ctl_io *)ctsio);
5726 return (CTL_RETVAL_COMPLETE);
5730 ctl_write_buffer(struct ctl_scsiio *ctsio)
5732 struct scsi_write_buffer *cdb;
5733 struct ctl_lun *lun;
5734 int buffer_offset, len;
5736 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5738 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5739 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5741 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5742 ctl_set_invalid_field(ctsio,
5748 ctl_done((union ctl_io *)ctsio);
5749 return (CTL_RETVAL_COMPLETE);
5752 len = scsi_3btoul(cdb->length);
5753 buffer_offset = scsi_3btoul(cdb->offset);
5755 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5756 ctl_set_invalid_field(ctsio,
5762 ctl_done((union ctl_io *)ctsio);
5763 return (CTL_RETVAL_COMPLETE);
5767 * If we've got a kernel request that hasn't been malloced yet,
5768 * malloc it and tell the caller the data buffer is here.
5770 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5771 if (lun->write_buffer == NULL) {
5772 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5775 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5776 ctsio->kern_data_len = len;
5777 ctsio->kern_total_len = len;
5778 ctsio->kern_data_resid = 0;
5779 ctsio->kern_rel_offset = 0;
5780 ctsio->kern_sg_entries = 0;
5781 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5782 ctsio->be_move_done = ctl_config_move_done;
5783 ctl_datamove((union ctl_io *)ctsio);
5785 return (CTL_RETVAL_COMPLETE);
5788 ctl_set_success(ctsio);
5789 ctl_done((union ctl_io *)ctsio);
5790 return (CTL_RETVAL_COMPLETE);
5794 ctl_write_same(struct ctl_scsiio *ctsio)
5796 struct ctl_lun *lun;
5797 struct ctl_lba_len_flags *lbalen;
5799 uint32_t num_blocks;
5803 retval = CTL_RETVAL_COMPLETE;
5805 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5807 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5809 switch (ctsio->cdb[0]) {
5810 case WRITE_SAME_10: {
5811 struct scsi_write_same_10 *cdb;
5813 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5815 lba = scsi_4btoul(cdb->addr);
5816 num_blocks = scsi_2btoul(cdb->length);
5820 case WRITE_SAME_16: {
5821 struct scsi_write_same_16 *cdb;
5823 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5825 lba = scsi_8btou64(cdb->addr);
5826 num_blocks = scsi_4btoul(cdb->length);
5832 * We got a command we don't support. This shouldn't
5833 * happen, commands should be filtered out above us.
5835 ctl_set_invalid_opcode(ctsio);
5836 ctl_done((union ctl_io *)ctsio);
5838 return (CTL_RETVAL_COMPLETE);
5839 break; /* NOTREACHED */
5842 /* NDOB and ANCHOR flags can be used only together with UNMAP */
5843 if ((byte2 & SWS_UNMAP) == 0 &&
5844 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) {
5845 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5846 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5847 ctl_done((union ctl_io *)ctsio);
5848 return (CTL_RETVAL_COMPLETE);
5852 * The first check is to make sure we're in bounds, the second
5853 * check is to catch wrap-around problems. If the lba + num blocks
5854 * is less than the lba, then we've wrapped around and the block
5855 * range is invalid anyway.
5857 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5858 || ((lba + num_blocks) < lba)) {
5859 ctl_set_lba_out_of_range(ctsio);
5860 ctl_done((union ctl_io *)ctsio);
5861 return (CTL_RETVAL_COMPLETE);
5864 /* Zero number of blocks means "to the last logical block" */
5865 if (num_blocks == 0) {
5866 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5867 ctl_set_invalid_field(ctsio,
5873 ctl_done((union ctl_io *)ctsio);
5874 return (CTL_RETVAL_COMPLETE);
5876 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5879 len = lun->be_lun->blocksize;
5882 * If we've got a kernel request that hasn't been malloced yet,
5883 * malloc it and tell the caller the data buffer is here.
5885 if ((byte2 & SWS_NDOB) == 0 &&
5886 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5887 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5888 ctsio->kern_data_len = len;
5889 ctsio->kern_total_len = len;
5890 ctsio->kern_data_resid = 0;
5891 ctsio->kern_rel_offset = 0;
5892 ctsio->kern_sg_entries = 0;
5893 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5894 ctsio->be_move_done = ctl_config_move_done;
5895 ctl_datamove((union ctl_io *)ctsio);
5897 return (CTL_RETVAL_COMPLETE);
5900 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5902 lbalen->len = num_blocks;
5903 lbalen->flags = byte2;
5904 retval = lun->backend->config_write((union ctl_io *)ctsio);
5910 ctl_unmap(struct ctl_scsiio *ctsio)
5912 struct ctl_lun *lun;
5913 struct scsi_unmap *cdb;
5914 struct ctl_ptr_len_flags *ptrlen;
5915 struct scsi_unmap_header *hdr;
5916 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5918 uint32_t num_blocks;
5922 retval = CTL_RETVAL_COMPLETE;
5924 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5926 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5927 cdb = (struct scsi_unmap *)ctsio->cdb;
5929 len = scsi_2btoul(cdb->length);
5933 * If we've got a kernel request that hasn't been malloced yet,
5934 * malloc it and tell the caller the data buffer is here.
5936 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5937 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5938 ctsio->kern_data_len = len;
5939 ctsio->kern_total_len = len;
5940 ctsio->kern_data_resid = 0;
5941 ctsio->kern_rel_offset = 0;
5942 ctsio->kern_sg_entries = 0;
5943 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5944 ctsio->be_move_done = ctl_config_move_done;
5945 ctl_datamove((union ctl_io *)ctsio);
5947 return (CTL_RETVAL_COMPLETE);
5950 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5951 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5952 if (len < sizeof (*hdr) ||
5953 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5954 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5955 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5956 ctl_set_invalid_field(ctsio,
5964 len = scsi_2btoul(hdr->desc_length);
5965 buf = (struct scsi_unmap_desc *)(hdr + 1);
5966 end = buf + len / sizeof(*buf);
5969 for (range = buf; range < end; range++) {
5970 lba = scsi_8btou64(range->lba);
5971 num_blocks = scsi_4btoul(range->length);
5972 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5973 || ((lba + num_blocks) < lba)) {
5974 ctl_set_lba_out_of_range(ctsio);
5975 ctl_done((union ctl_io *)ctsio);
5976 return (CTL_RETVAL_COMPLETE);
5978 if (num_blocks != 0)
5983 * Block backend can not handle zero last range.
5984 * Filter it out and return if there is nothing left.
5986 len = (uint8_t *)endnz - (uint8_t *)buf;
5988 ctl_set_success(ctsio);
5992 mtx_lock(&lun->lun_lock);
5993 ptrlen = (struct ctl_ptr_len_flags *)
5994 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5995 ptrlen->ptr = (void *)buf;
5997 ptrlen->flags = byte2;
5998 ctl_check_blocked(lun);
5999 mtx_unlock(&lun->lun_lock);
6001 retval = lun->backend->config_write((union ctl_io *)ctsio);
6005 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
6006 free(ctsio->kern_data_ptr, M_CTL);
6007 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
6009 ctl_done((union ctl_io *)ctsio);
6010 return (CTL_RETVAL_COMPLETE);
6014 * Note that this function currently doesn't actually do anything inside
6015 * CTL to enforce things if the DQue bit is turned on.
6017 * Also note that this function can't be used in the default case, because
6018 * the DQue bit isn't set in the changeable mask for the control mode page
6019 * anyway. This is just here as an example for how to implement a page
6020 * handler, and a placeholder in case we want to allow the user to turn
6021 * tagged queueing on and off.
6023 * The D_SENSE bit handling is functional, however, and will turn
6024 * descriptor sense on and off for a given LUN.
6027 ctl_control_page_handler(struct ctl_scsiio *ctsio,
6028 struct ctl_page_index *page_index, uint8_t *page_ptr)
6030 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
6031 struct ctl_lun *lun;
6035 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6036 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6039 user_cp = (struct scsi_control_page *)page_ptr;
6040 current_cp = (struct scsi_control_page *)
6041 (page_index->page_data + (page_index->page_len *
6043 saved_cp = (struct scsi_control_page *)
6044 (page_index->page_data + (page_index->page_len *
6047 mtx_lock(&lun->lun_lock);
6048 if (((current_cp->rlec & SCP_DSENSE) == 0)
6049 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
6051 * Descriptor sense is currently turned off and the user
6052 * wants to turn it on.
6054 current_cp->rlec |= SCP_DSENSE;
6055 saved_cp->rlec |= SCP_DSENSE;
6056 lun->flags |= CTL_LUN_SENSE_DESC;
6058 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
6059 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
6061 * Descriptor sense is currently turned on, and the user
6062 * wants to turn it off.
6064 current_cp->rlec &= ~SCP_DSENSE;
6065 saved_cp->rlec &= ~SCP_DSENSE;
6066 lun->flags &= ~CTL_LUN_SENSE_DESC;
6069 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) !=
6070 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) {
6071 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
6072 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
6073 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
6074 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
6077 if ((current_cp->eca_and_aen & SCP_SWP) !=
6078 (user_cp->eca_and_aen & SCP_SWP)) {
6079 current_cp->eca_and_aen &= ~SCP_SWP;
6080 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
6081 saved_cp->eca_and_aen &= ~SCP_SWP;
6082 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
6086 ctl_est_ua_all(lun, initidx, 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);
6123 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
6124 mtx_unlock(&lun->lun_lock);
6130 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
6131 struct ctl_page_index *page_index,
6137 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
6142 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
6143 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
6144 printf("page data:");
6146 printf(" %.2x",page_ptr[i]);
6152 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
6153 struct ctl_page_index *page_index,
6156 struct copan_debugconf_subpage *page;
6158 page = (struct copan_debugconf_subpage *)page_index->page_data +
6159 (page_index->page_len * pc);
6162 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6163 case SMS_PAGE_CTRL_DEFAULT >> 6:
6164 case SMS_PAGE_CTRL_SAVED >> 6:
6166 * We don't update the changable or default bits for this page.
6169 case SMS_PAGE_CTRL_CURRENT >> 6:
6170 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
6171 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
6175 EPRINT(0, "Invalid PC %d!!", pc);
6176 #endif /* NEEDTOPORT */
6184 ctl_do_mode_select(union ctl_io *io)
6186 struct scsi_mode_page_header *page_header;
6187 struct ctl_page_index *page_index;
6188 struct ctl_scsiio *ctsio;
6189 int control_dev, page_len;
6190 int page_len_offset, page_len_size;
6191 union ctl_modepage_info *modepage_info;
6192 struct ctl_lun *lun;
6193 int *len_left, *len_used;
6196 ctsio = &io->scsiio;
6199 retval = CTL_RETVAL_COMPLETE;
6201 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6203 if (lun->be_lun->lun_type != T_DIRECT)
6208 modepage_info = (union ctl_modepage_info *)
6209 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6210 len_left = &modepage_info->header.len_left;
6211 len_used = &modepage_info->header.len_used;
6215 page_header = (struct scsi_mode_page_header *)
6216 (ctsio->kern_data_ptr + *len_used);
6218 if (*len_left == 0) {
6219 free(ctsio->kern_data_ptr, M_CTL);
6220 ctl_set_success(ctsio);
6221 ctl_done((union ctl_io *)ctsio);
6222 return (CTL_RETVAL_COMPLETE);
6223 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6225 free(ctsio->kern_data_ptr, M_CTL);
6226 ctl_set_param_len_error(ctsio);
6227 ctl_done((union ctl_io *)ctsio);
6228 return (CTL_RETVAL_COMPLETE);
6230 } else if ((page_header->page_code & SMPH_SPF)
6231 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6233 free(ctsio->kern_data_ptr, M_CTL);
6234 ctl_set_param_len_error(ctsio);
6235 ctl_done((union ctl_io *)ctsio);
6236 return (CTL_RETVAL_COMPLETE);
6241 * XXX KDM should we do something with the block descriptor?
6243 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6245 if ((control_dev != 0)
6246 && (lun->mode_pages.index[i].page_flags &
6247 CTL_PAGE_FLAG_DISK_ONLY))
6250 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
6251 (page_header->page_code & SMPH_PC_MASK))
6255 * If neither page has a subpage code, then we've got a
6258 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
6259 && ((page_header->page_code & SMPH_SPF) == 0)) {
6260 page_index = &lun->mode_pages.index[i];
6261 page_len = page_header->page_length;
6266 * If both pages have subpages, then the subpage numbers
6269 if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
6270 && (page_header->page_code & SMPH_SPF)) {
6271 struct scsi_mode_page_header_sp *sph;
6273 sph = (struct scsi_mode_page_header_sp *)page_header;
6275 if (lun->mode_pages.index[i].subpage ==
6277 page_index = &lun->mode_pages.index[i];
6278 page_len = scsi_2btoul(sph->page_length);
6285 * If we couldn't find the page, or if we don't have a mode select
6286 * handler for it, send back an error to the user.
6288 if ((page_index == NULL)
6289 || (page_index->select_handler == NULL)) {
6290 ctl_set_invalid_field(ctsio,
6293 /*field*/ *len_used,
6296 free(ctsio->kern_data_ptr, M_CTL);
6297 ctl_done((union ctl_io *)ctsio);
6298 return (CTL_RETVAL_COMPLETE);
6301 if (page_index->page_code & SMPH_SPF) {
6302 page_len_offset = 2;
6306 page_len_offset = 1;
6310 * If the length the initiator gives us isn't the one we specify in
6311 * the mode page header, or if they didn't specify enough data in
6312 * the CDB to avoid truncating this page, kick out the request.
6314 if ((page_len != (page_index->page_len - page_len_offset -
6316 || (*len_left < page_index->page_len)) {
6319 ctl_set_invalid_field(ctsio,
6322 /*field*/ *len_used + page_len_offset,
6325 free(ctsio->kern_data_ptr, M_CTL);
6326 ctl_done((union ctl_io *)ctsio);
6327 return (CTL_RETVAL_COMPLETE);
6331 * Run through the mode page, checking to make sure that the bits
6332 * the user changed are actually legal for him to change.
6334 for (i = 0; i < page_index->page_len; i++) {
6335 uint8_t *user_byte, *change_mask, *current_byte;
6339 user_byte = (uint8_t *)page_header + i;
6340 change_mask = page_index->page_data +
6341 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6342 current_byte = page_index->page_data +
6343 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6346 * Check to see whether the user set any bits in this byte
6347 * that he is not allowed to set.
6349 if ((*user_byte & ~(*change_mask)) ==
6350 (*current_byte & ~(*change_mask)))
6354 * Go through bit by bit to determine which one is illegal.
6357 for (j = 7; j >= 0; j--) {
6358 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6359 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6364 ctl_set_invalid_field(ctsio,
6367 /*field*/ *len_used + i,
6370 free(ctsio->kern_data_ptr, M_CTL);
6371 ctl_done((union ctl_io *)ctsio);
6372 return (CTL_RETVAL_COMPLETE);
6376 * Decrement these before we call the page handler, since we may
6377 * end up getting called back one way or another before the handler
6378 * returns to this context.
6380 *len_left -= page_index->page_len;
6381 *len_used += page_index->page_len;
6383 retval = page_index->select_handler(ctsio, page_index,
6384 (uint8_t *)page_header);
6387 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6388 * wait until this queued command completes to finish processing
6389 * the mode page. If it returns anything other than
6390 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6391 * already set the sense information, freed the data pointer, and
6392 * completed the io for us.
6394 if (retval != CTL_RETVAL_COMPLETE)
6395 goto bailout_no_done;
6398 * If the initiator sent us more than one page, parse the next one.
6403 ctl_set_success(ctsio);
6404 free(ctsio->kern_data_ptr, M_CTL);
6405 ctl_done((union ctl_io *)ctsio);
6409 return (CTL_RETVAL_COMPLETE);
6414 ctl_mode_select(struct ctl_scsiio *ctsio)
6416 int param_len, pf, sp;
6417 int header_size, bd_len;
6418 int len_left, len_used;
6419 struct ctl_page_index *page_index;
6420 struct ctl_lun *lun;
6421 int control_dev, page_len;
6422 union ctl_modepage_info *modepage_info;
6434 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6436 if (lun->be_lun->lun_type != T_DIRECT)
6441 switch (ctsio->cdb[0]) {
6442 case MODE_SELECT_6: {
6443 struct scsi_mode_select_6 *cdb;
6445 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6447 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6448 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6450 param_len = cdb->length;
6451 header_size = sizeof(struct scsi_mode_header_6);
6454 case MODE_SELECT_10: {
6455 struct scsi_mode_select_10 *cdb;
6457 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6459 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6460 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6462 param_len = scsi_2btoul(cdb->length);
6463 header_size = sizeof(struct scsi_mode_header_10);
6467 ctl_set_invalid_opcode(ctsio);
6468 ctl_done((union ctl_io *)ctsio);
6469 return (CTL_RETVAL_COMPLETE);
6470 break; /* NOTREACHED */
6475 * "A parameter list length of zero indicates that the Data-Out Buffer
6476 * shall be empty. This condition shall not be considered as an error."
6478 if (param_len == 0) {
6479 ctl_set_success(ctsio);
6480 ctl_done((union ctl_io *)ctsio);
6481 return (CTL_RETVAL_COMPLETE);
6485 * Since we'll hit this the first time through, prior to
6486 * allocation, we don't need to free a data buffer here.
6488 if (param_len < header_size) {
6489 ctl_set_param_len_error(ctsio);
6490 ctl_done((union ctl_io *)ctsio);
6491 return (CTL_RETVAL_COMPLETE);
6495 * Allocate the data buffer and grab the user's data. In theory,
6496 * we shouldn't have to sanity check the parameter list length here
6497 * because the maximum size is 64K. We should be able to malloc
6498 * that much without too many problems.
6500 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6501 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6502 ctsio->kern_data_len = param_len;
6503 ctsio->kern_total_len = param_len;
6504 ctsio->kern_data_resid = 0;
6505 ctsio->kern_rel_offset = 0;
6506 ctsio->kern_sg_entries = 0;
6507 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6508 ctsio->be_move_done = ctl_config_move_done;
6509 ctl_datamove((union ctl_io *)ctsio);
6511 return (CTL_RETVAL_COMPLETE);
6514 switch (ctsio->cdb[0]) {
6515 case MODE_SELECT_6: {
6516 struct scsi_mode_header_6 *mh6;
6518 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6519 bd_len = mh6->blk_desc_len;
6522 case MODE_SELECT_10: {
6523 struct scsi_mode_header_10 *mh10;
6525 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6526 bd_len = scsi_2btoul(mh10->blk_desc_len);
6530 panic("Invalid CDB type %#x", ctsio->cdb[0]);
6534 if (param_len < (header_size + bd_len)) {
6535 free(ctsio->kern_data_ptr, M_CTL);
6536 ctl_set_param_len_error(ctsio);
6537 ctl_done((union ctl_io *)ctsio);
6538 return (CTL_RETVAL_COMPLETE);
6542 * Set the IO_CONT flag, so that if this I/O gets passed to
6543 * ctl_config_write_done(), it'll get passed back to
6544 * ctl_do_mode_select() for further processing, or completion if
6547 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6548 ctsio->io_cont = ctl_do_mode_select;
6550 modepage_info = (union ctl_modepage_info *)
6551 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6553 memset(modepage_info, 0, sizeof(*modepage_info));
6555 len_left = param_len - header_size - bd_len;
6556 len_used = header_size + bd_len;
6558 modepage_info->header.len_left = len_left;
6559 modepage_info->header.len_used = len_used;
6561 return (ctl_do_mode_select((union ctl_io *)ctsio));
6565 ctl_mode_sense(struct ctl_scsiio *ctsio)
6567 struct ctl_lun *lun;
6568 int pc, page_code, dbd, llba, subpage;
6569 int alloc_len, page_len, header_len, total_len;
6570 struct scsi_mode_block_descr *block_desc;
6571 struct ctl_page_index *page_index;
6579 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6581 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6583 if (lun->be_lun->lun_type != T_DIRECT)
6588 switch (ctsio->cdb[0]) {
6589 case MODE_SENSE_6: {
6590 struct scsi_mode_sense_6 *cdb;
6592 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6594 header_len = sizeof(struct scsi_mode_hdr_6);
6595 if (cdb->byte2 & SMS_DBD)
6598 header_len += sizeof(struct scsi_mode_block_descr);
6600 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6601 page_code = cdb->page & SMS_PAGE_CODE;
6602 subpage = cdb->subpage;
6603 alloc_len = cdb->length;
6606 case MODE_SENSE_10: {
6607 struct scsi_mode_sense_10 *cdb;
6609 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6611 header_len = sizeof(struct scsi_mode_hdr_10);
6613 if (cdb->byte2 & SMS_DBD)
6616 header_len += sizeof(struct scsi_mode_block_descr);
6617 if (cdb->byte2 & SMS10_LLBAA)
6619 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6620 page_code = cdb->page & SMS_PAGE_CODE;
6621 subpage = cdb->subpage;
6622 alloc_len = scsi_2btoul(cdb->length);
6626 ctl_set_invalid_opcode(ctsio);
6627 ctl_done((union ctl_io *)ctsio);
6628 return (CTL_RETVAL_COMPLETE);
6629 break; /* NOTREACHED */
6633 * We have to make a first pass through to calculate the size of
6634 * the pages that match the user's query. Then we allocate enough
6635 * memory to hold it, and actually copy the data into the buffer.
6637 switch (page_code) {
6638 case SMS_ALL_PAGES_PAGE: {
6644 * At the moment, values other than 0 and 0xff here are
6645 * reserved according to SPC-3.
6647 if ((subpage != SMS_SUBPAGE_PAGE_0)
6648 && (subpage != SMS_SUBPAGE_ALL)) {
6649 ctl_set_invalid_field(ctsio,
6655 ctl_done((union ctl_io *)ctsio);
6656 return (CTL_RETVAL_COMPLETE);
6659 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6660 if ((control_dev != 0)
6661 && (lun->mode_pages.index[i].page_flags &
6662 CTL_PAGE_FLAG_DISK_ONLY))
6666 * We don't use this subpage if the user didn't
6667 * request all subpages.
6669 if ((lun->mode_pages.index[i].subpage != 0)
6670 && (subpage == SMS_SUBPAGE_PAGE_0))
6674 printf("found page %#x len %d\n",
6675 lun->mode_pages.index[i].page_code &
6677 lun->mode_pages.index[i].page_len);
6679 page_len += lun->mode_pages.index[i].page_len;
6688 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6689 /* Look for the right page code */
6690 if ((lun->mode_pages.index[i].page_code &
6691 SMPH_PC_MASK) != page_code)
6694 /* Look for the right subpage or the subpage wildcard*/
6695 if ((lun->mode_pages.index[i].subpage != subpage)
6696 && (subpage != SMS_SUBPAGE_ALL))
6699 /* Make sure the page is supported for this dev type */
6700 if ((control_dev != 0)
6701 && (lun->mode_pages.index[i].page_flags &
6702 CTL_PAGE_FLAG_DISK_ONLY))
6706 printf("found page %#x len %d\n",
6707 lun->mode_pages.index[i].page_code &
6709 lun->mode_pages.index[i].page_len);
6712 page_len += lun->mode_pages.index[i].page_len;
6715 if (page_len == 0) {
6716 ctl_set_invalid_field(ctsio,
6722 ctl_done((union ctl_io *)ctsio);
6723 return (CTL_RETVAL_COMPLETE);
6729 total_len = header_len + page_len;
6731 printf("header_len = %d, page_len = %d, total_len = %d\n",
6732 header_len, page_len, total_len);
6735 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6736 ctsio->kern_sg_entries = 0;
6737 ctsio->kern_data_resid = 0;
6738 ctsio->kern_rel_offset = 0;
6739 if (total_len < alloc_len) {
6740 ctsio->residual = alloc_len - total_len;
6741 ctsio->kern_data_len = total_len;
6742 ctsio->kern_total_len = total_len;
6744 ctsio->residual = 0;
6745 ctsio->kern_data_len = alloc_len;
6746 ctsio->kern_total_len = alloc_len;
6749 switch (ctsio->cdb[0]) {
6750 case MODE_SENSE_6: {
6751 struct scsi_mode_hdr_6 *header;
6753 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6755 header->datalen = MIN(total_len - 1, 254);
6756 if (control_dev == 0) {
6757 header->dev_specific = 0x10; /* DPOFUA */
6758 if ((lun->flags & CTL_LUN_READONLY) ||
6759 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6760 .eca_and_aen & SCP_SWP) != 0)
6761 header->dev_specific |= 0x80; /* WP */
6764 header->block_descr_len = 0;
6766 header->block_descr_len =
6767 sizeof(struct scsi_mode_block_descr);
6768 block_desc = (struct scsi_mode_block_descr *)&header[1];
6771 case MODE_SENSE_10: {
6772 struct scsi_mode_hdr_10 *header;
6775 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6777 datalen = MIN(total_len - 2, 65533);
6778 scsi_ulto2b(datalen, header->datalen);
6779 if (control_dev == 0) {
6780 header->dev_specific = 0x10; /* DPOFUA */
6781 if ((lun->flags & CTL_LUN_READONLY) ||
6782 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6783 .eca_and_aen & SCP_SWP) != 0)
6784 header->dev_specific |= 0x80; /* WP */
6787 scsi_ulto2b(0, header->block_descr_len);
6789 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6790 header->block_descr_len);
6791 block_desc = (struct scsi_mode_block_descr *)&header[1];
6795 panic("invalid CDB type %#x", ctsio->cdb[0]);
6796 break; /* NOTREACHED */
6800 * If we've got a disk, use its blocksize in the block
6801 * descriptor. Otherwise, just set it to 0.
6804 if (control_dev == 0)
6805 scsi_ulto3b(lun->be_lun->blocksize,
6806 block_desc->block_len);
6808 scsi_ulto3b(0, block_desc->block_len);
6811 switch (page_code) {
6812 case SMS_ALL_PAGES_PAGE: {
6815 data_used = header_len;
6816 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6817 struct ctl_page_index *page_index;
6819 page_index = &lun->mode_pages.index[i];
6821 if ((control_dev != 0)
6822 && (page_index->page_flags &
6823 CTL_PAGE_FLAG_DISK_ONLY))
6827 * We don't use this subpage if the user didn't
6828 * request all subpages. We already checked (above)
6829 * to make sure the user only specified a subpage
6830 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6832 if ((page_index->subpage != 0)
6833 && (subpage == SMS_SUBPAGE_PAGE_0))
6837 * Call the handler, if it exists, to update the
6838 * page to the latest values.
6840 if (page_index->sense_handler != NULL)
6841 page_index->sense_handler(ctsio, page_index,pc);
6843 memcpy(ctsio->kern_data_ptr + data_used,
6844 page_index->page_data +
6845 (page_index->page_len * pc),
6846 page_index->page_len);
6847 data_used += page_index->page_len;
6854 data_used = header_len;
6856 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6857 struct ctl_page_index *page_index;
6859 page_index = &lun->mode_pages.index[i];
6861 /* Look for the right page code */
6862 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6865 /* Look for the right subpage or the subpage wildcard*/
6866 if ((page_index->subpage != subpage)
6867 && (subpage != SMS_SUBPAGE_ALL))
6870 /* Make sure the page is supported for this dev type */
6871 if ((control_dev != 0)
6872 && (page_index->page_flags &
6873 CTL_PAGE_FLAG_DISK_ONLY))
6877 * Call the handler, if it exists, to update the
6878 * page to the latest values.
6880 if (page_index->sense_handler != NULL)
6881 page_index->sense_handler(ctsio, page_index,pc);
6883 memcpy(ctsio->kern_data_ptr + data_used,
6884 page_index->page_data +
6885 (page_index->page_len * pc),
6886 page_index->page_len);
6887 data_used += page_index->page_len;
6893 ctl_set_success(ctsio);
6894 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6895 ctsio->be_move_done = ctl_config_move_done;
6896 ctl_datamove((union ctl_io *)ctsio);
6897 return (CTL_RETVAL_COMPLETE);
6901 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6902 struct ctl_page_index *page_index,
6905 struct ctl_lun *lun;
6906 struct scsi_log_param_header *phdr;
6910 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6911 data = page_index->page_data;
6913 if (lun->backend->lun_attr != NULL &&
6914 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6916 phdr = (struct scsi_log_param_header *)data;
6917 scsi_ulto2b(0x0001, phdr->param_code);
6918 phdr->param_control = SLP_LBIN | SLP_LP;
6919 phdr->param_len = 8;
6920 data = (uint8_t *)(phdr + 1);
6921 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6922 data[4] = 0x02; /* per-pool */
6923 data += phdr->param_len;
6926 if (lun->backend->lun_attr != NULL &&
6927 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6929 phdr = (struct scsi_log_param_header *)data;
6930 scsi_ulto2b(0x0002, phdr->param_code);
6931 phdr->param_control = SLP_LBIN | SLP_LP;
6932 phdr->param_len = 8;
6933 data = (uint8_t *)(phdr + 1);
6934 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6935 data[4] = 0x01; /* per-LUN */
6936 data += phdr->param_len;
6939 if (lun->backend->lun_attr != NULL &&
6940 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6942 phdr = (struct scsi_log_param_header *)data;
6943 scsi_ulto2b(0x00f1, phdr->param_code);
6944 phdr->param_control = SLP_LBIN | SLP_LP;
6945 phdr->param_len = 8;
6946 data = (uint8_t *)(phdr + 1);
6947 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6948 data[4] = 0x02; /* per-pool */
6949 data += phdr->param_len;
6952 if (lun->backend->lun_attr != NULL &&
6953 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6955 phdr = (struct scsi_log_param_header *)data;
6956 scsi_ulto2b(0x00f2, phdr->param_code);
6957 phdr->param_control = SLP_LBIN | SLP_LP;
6958 phdr->param_len = 8;
6959 data = (uint8_t *)(phdr + 1);
6960 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6961 data[4] = 0x02; /* per-pool */
6962 data += phdr->param_len;
6965 page_index->page_len = data - page_index->page_data;
6970 ctl_log_sense(struct ctl_scsiio *ctsio)
6972 struct ctl_lun *lun;
6973 int i, pc, page_code, subpage;
6974 int alloc_len, total_len;
6975 struct ctl_page_index *page_index;
6976 struct scsi_log_sense *cdb;
6977 struct scsi_log_header *header;
6979 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6981 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6982 cdb = (struct scsi_log_sense *)ctsio->cdb;
6983 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6984 page_code = cdb->page & SLS_PAGE_CODE;
6985 subpage = cdb->subpage;
6986 alloc_len = scsi_2btoul(cdb->length);
6989 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6990 page_index = &lun->log_pages.index[i];
6992 /* Look for the right page code */
6993 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6996 /* Look for the right subpage or the subpage wildcard*/
6997 if (page_index->subpage != subpage)
7002 if (i >= CTL_NUM_LOG_PAGES) {
7003 ctl_set_invalid_field(ctsio,
7009 ctl_done((union ctl_io *)ctsio);
7010 return (CTL_RETVAL_COMPLETE);
7013 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
7015 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7016 ctsio->kern_sg_entries = 0;
7017 ctsio->kern_data_resid = 0;
7018 ctsio->kern_rel_offset = 0;
7019 if (total_len < alloc_len) {
7020 ctsio->residual = alloc_len - total_len;
7021 ctsio->kern_data_len = total_len;
7022 ctsio->kern_total_len = total_len;
7024 ctsio->residual = 0;
7025 ctsio->kern_data_len = alloc_len;
7026 ctsio->kern_total_len = alloc_len;
7029 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
7030 header->page = page_index->page_code;
7031 if (page_index->subpage) {
7032 header->page |= SL_SPF;
7033 header->subpage = page_index->subpage;
7035 scsi_ulto2b(page_index->page_len, header->datalen);
7038 * Call the handler, if it exists, to update the
7039 * page to the latest values.
7041 if (page_index->sense_handler != NULL)
7042 page_index->sense_handler(ctsio, page_index, pc);
7044 memcpy(header + 1, page_index->page_data, page_index->page_len);
7046 ctl_set_success(ctsio);
7047 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7048 ctsio->be_move_done = ctl_config_move_done;
7049 ctl_datamove((union ctl_io *)ctsio);
7050 return (CTL_RETVAL_COMPLETE);
7054 ctl_read_capacity(struct ctl_scsiio *ctsio)
7056 struct scsi_read_capacity *cdb;
7057 struct scsi_read_capacity_data *data;
7058 struct ctl_lun *lun;
7061 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
7063 cdb = (struct scsi_read_capacity *)ctsio->cdb;
7065 lba = scsi_4btoul(cdb->addr);
7066 if (((cdb->pmi & SRC_PMI) == 0)
7068 ctl_set_invalid_field(/*ctsio*/ ctsio,
7074 ctl_done((union ctl_io *)ctsio);
7075 return (CTL_RETVAL_COMPLETE);
7078 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7080 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7081 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
7082 ctsio->residual = 0;
7083 ctsio->kern_data_len = sizeof(*data);
7084 ctsio->kern_total_len = sizeof(*data);
7085 ctsio->kern_data_resid = 0;
7086 ctsio->kern_rel_offset = 0;
7087 ctsio->kern_sg_entries = 0;
7090 * If the maximum LBA is greater than 0xfffffffe, the user must
7091 * issue a SERVICE ACTION IN (16) command, with the read capacity
7092 * serivce action set.
7094 if (lun->be_lun->maxlba > 0xfffffffe)
7095 scsi_ulto4b(0xffffffff, data->addr);
7097 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
7100 * XXX KDM this may not be 512 bytes...
7102 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7104 ctl_set_success(ctsio);
7105 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7106 ctsio->be_move_done = ctl_config_move_done;
7107 ctl_datamove((union ctl_io *)ctsio);
7108 return (CTL_RETVAL_COMPLETE);
7112 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
7114 struct scsi_read_capacity_16 *cdb;
7115 struct scsi_read_capacity_data_long *data;
7116 struct ctl_lun *lun;
7120 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
7122 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
7124 alloc_len = scsi_4btoul(cdb->alloc_len);
7125 lba = scsi_8btou64(cdb->addr);
7127 if ((cdb->reladr & SRC16_PMI)
7129 ctl_set_invalid_field(/*ctsio*/ ctsio,
7135 ctl_done((union ctl_io *)ctsio);
7136 return (CTL_RETVAL_COMPLETE);
7139 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7141 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7142 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
7144 if (sizeof(*data) < alloc_len) {
7145 ctsio->residual = alloc_len - sizeof(*data);
7146 ctsio->kern_data_len = sizeof(*data);
7147 ctsio->kern_total_len = sizeof(*data);
7149 ctsio->residual = 0;
7150 ctsio->kern_data_len = alloc_len;
7151 ctsio->kern_total_len = alloc_len;
7153 ctsio->kern_data_resid = 0;
7154 ctsio->kern_rel_offset = 0;
7155 ctsio->kern_sg_entries = 0;
7157 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7158 /* XXX KDM this may not be 512 bytes... */
7159 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7160 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7161 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7162 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7163 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
7165 ctl_set_success(ctsio);
7166 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7167 ctsio->be_move_done = ctl_config_move_done;
7168 ctl_datamove((union ctl_io *)ctsio);
7169 return (CTL_RETVAL_COMPLETE);
7173 ctl_get_lba_status(struct ctl_scsiio *ctsio)
7175 struct scsi_get_lba_status *cdb;
7176 struct scsi_get_lba_status_data *data;
7177 struct ctl_lun *lun;
7178 struct ctl_lba_len_flags *lbalen;
7180 uint32_t alloc_len, total_len;
7183 CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
7185 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7186 cdb = (struct scsi_get_lba_status *)ctsio->cdb;
7187 lba = scsi_8btou64(cdb->addr);
7188 alloc_len = scsi_4btoul(cdb->alloc_len);
7190 if (lba > lun->be_lun->maxlba) {
7191 ctl_set_lba_out_of_range(ctsio);
7192 ctl_done((union ctl_io *)ctsio);
7193 return (CTL_RETVAL_COMPLETE);
7196 total_len = sizeof(*data) + sizeof(data->descr[0]);
7197 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7198 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
7200 if (total_len < alloc_len) {
7201 ctsio->residual = alloc_len - total_len;
7202 ctsio->kern_data_len = total_len;
7203 ctsio->kern_total_len = total_len;
7205 ctsio->residual = 0;
7206 ctsio->kern_data_len = alloc_len;
7207 ctsio->kern_total_len = alloc_len;
7209 ctsio->kern_data_resid = 0;
7210 ctsio->kern_rel_offset = 0;
7211 ctsio->kern_sg_entries = 0;
7213 /* Fill dummy data in case backend can't tell anything. */
7214 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
7215 scsi_u64to8b(lba, data->descr[0].addr);
7216 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
7217 data->descr[0].length);
7218 data->descr[0].status = 0; /* Mapped or unknown. */
7220 ctl_set_success(ctsio);
7221 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7222 ctsio->be_move_done = ctl_config_move_done;
7224 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
7226 lbalen->len = total_len;
7228 retval = lun->backend->config_read((union ctl_io *)ctsio);
7229 return (CTL_RETVAL_COMPLETE);
7233 ctl_read_defect(struct ctl_scsiio *ctsio)
7235 struct scsi_read_defect_data_10 *ccb10;
7236 struct scsi_read_defect_data_12 *ccb12;
7237 struct scsi_read_defect_data_hdr_10 *data10;
7238 struct scsi_read_defect_data_hdr_12 *data12;
7239 uint32_t alloc_len, data_len;
7242 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7244 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7245 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7246 format = ccb10->format;
7247 alloc_len = scsi_2btoul(ccb10->alloc_length);
7248 data_len = sizeof(*data10);
7250 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7251 format = ccb12->format;
7252 alloc_len = scsi_4btoul(ccb12->alloc_length);
7253 data_len = sizeof(*data12);
7255 if (alloc_len == 0) {
7256 ctl_set_success(ctsio);
7257 ctl_done((union ctl_io *)ctsio);
7258 return (CTL_RETVAL_COMPLETE);
7261 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7262 if (data_len < alloc_len) {
7263 ctsio->residual = alloc_len - data_len;
7264 ctsio->kern_data_len = data_len;
7265 ctsio->kern_total_len = data_len;
7267 ctsio->residual = 0;
7268 ctsio->kern_data_len = alloc_len;
7269 ctsio->kern_total_len = alloc_len;
7271 ctsio->kern_data_resid = 0;
7272 ctsio->kern_rel_offset = 0;
7273 ctsio->kern_sg_entries = 0;
7275 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7276 data10 = (struct scsi_read_defect_data_hdr_10 *)
7277 ctsio->kern_data_ptr;
7278 data10->format = format;
7279 scsi_ulto2b(0, data10->length);
7281 data12 = (struct scsi_read_defect_data_hdr_12 *)
7282 ctsio->kern_data_ptr;
7283 data12->format = format;
7284 scsi_ulto2b(0, data12->generation);
7285 scsi_ulto4b(0, data12->length);
7288 ctl_set_success(ctsio);
7289 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7290 ctsio->be_move_done = ctl_config_move_done;
7291 ctl_datamove((union ctl_io *)ctsio);
7292 return (CTL_RETVAL_COMPLETE);
7296 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7298 struct scsi_maintenance_in *cdb;
7300 int alloc_len, ext, total_len = 0, g, p, pc, pg, gs, os;
7301 int num_target_port_groups, num_target_ports;
7302 struct ctl_lun *lun;
7303 struct ctl_softc *softc;
7304 struct ctl_port *port;
7305 struct scsi_target_group_data *rtg_ptr;
7306 struct scsi_target_group_data_extended *rtg_ext_ptr;
7307 struct scsi_target_port_group_descriptor *tpg_desc;
7309 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7311 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7312 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7313 softc = lun->ctl_softc;
7315 retval = CTL_RETVAL_COMPLETE;
7317 switch (cdb->byte2 & STG_PDF_MASK) {
7318 case STG_PDF_LENGTH:
7321 case STG_PDF_EXTENDED:
7325 ctl_set_invalid_field(/*ctsio*/ ctsio,
7331 ctl_done((union ctl_io *)ctsio);
7335 if (softc->is_single)
7336 num_target_port_groups = 1;
7338 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
7339 num_target_ports = 0;
7340 mtx_lock(&softc->ctl_lock);
7341 STAILQ_FOREACH(port, &softc->port_list, links) {
7342 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7344 if (ctl_map_lun_back(softc, port->targ_port, lun->lun) >=
7349 mtx_unlock(&softc->ctl_lock);
7352 total_len = sizeof(struct scsi_target_group_data_extended);
7354 total_len = sizeof(struct scsi_target_group_data);
7355 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7356 num_target_port_groups +
7357 sizeof(struct scsi_target_port_descriptor) *
7358 num_target_ports * num_target_port_groups;
7360 alloc_len = scsi_4btoul(cdb->length);
7362 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7364 ctsio->kern_sg_entries = 0;
7366 if (total_len < alloc_len) {
7367 ctsio->residual = alloc_len - total_len;
7368 ctsio->kern_data_len = total_len;
7369 ctsio->kern_total_len = total_len;
7371 ctsio->residual = 0;
7372 ctsio->kern_data_len = alloc_len;
7373 ctsio->kern_total_len = alloc_len;
7375 ctsio->kern_data_resid = 0;
7376 ctsio->kern_rel_offset = 0;
7379 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7380 ctsio->kern_data_ptr;
7381 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7382 rtg_ext_ptr->format_type = 0x10;
7383 rtg_ext_ptr->implicit_transition_time = 0;
7384 tpg_desc = &rtg_ext_ptr->groups[0];
7386 rtg_ptr = (struct scsi_target_group_data *)
7387 ctsio->kern_data_ptr;
7388 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7389 tpg_desc = &rtg_ptr->groups[0];
7392 mtx_lock(&softc->ctl_lock);
7393 pg = softc->port_offset / CTL_MAX_PORTS;
7394 if (softc->flags & CTL_FLAG_ACTIVE_SHELF) {
7395 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) {
7396 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7397 os = TPG_ASYMMETRIC_ACCESS_STANDBY;
7398 } else if (lun->flags & CTL_LUN_PRIMARY_SC) {
7399 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7400 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7402 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7403 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7406 gs = TPG_ASYMMETRIC_ACCESS_STANDBY;
7407 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7409 for (g = 0; g < num_target_port_groups; g++) {
7410 tpg_desc->pref_state = (g == pg) ? gs : os;
7411 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP;
7412 scsi_ulto2b(g + 1, tpg_desc->target_port_group);
7413 tpg_desc->status = TPG_IMPLICIT;
7415 STAILQ_FOREACH(port, &softc->port_list, links) {
7416 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7418 if (ctl_map_lun_back(softc, port->targ_port, lun->lun)
7421 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS;
7422 scsi_ulto2b(p, tpg_desc->descriptors[pc].
7423 relative_target_port_identifier);
7426 tpg_desc->target_port_count = pc;
7427 tpg_desc = (struct scsi_target_port_group_descriptor *)
7428 &tpg_desc->descriptors[pc];
7430 mtx_unlock(&softc->ctl_lock);
7432 ctl_set_success(ctsio);
7433 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7434 ctsio->be_move_done = ctl_config_move_done;
7435 ctl_datamove((union ctl_io *)ctsio);
7440 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7442 struct ctl_lun *lun;
7443 struct scsi_report_supported_opcodes *cdb;
7444 const struct ctl_cmd_entry *entry, *sentry;
7445 struct scsi_report_supported_opcodes_all *all;
7446 struct scsi_report_supported_opcodes_descr *descr;
7447 struct scsi_report_supported_opcodes_one *one;
7449 int alloc_len, total_len;
7450 int opcode, service_action, i, j, num;
7452 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7454 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7455 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7457 retval = CTL_RETVAL_COMPLETE;
7459 opcode = cdb->requested_opcode;
7460 service_action = scsi_2btoul(cdb->requested_service_action);
7461 switch (cdb->options & RSO_OPTIONS_MASK) {
7462 case RSO_OPTIONS_ALL:
7464 for (i = 0; i < 256; i++) {
7465 entry = &ctl_cmd_table[i];
7466 if (entry->flags & CTL_CMD_FLAG_SA5) {
7467 for (j = 0; j < 32; j++) {
7468 sentry = &((const struct ctl_cmd_entry *)
7470 if (ctl_cmd_applicable(
7471 lun->be_lun->lun_type, sentry))
7475 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7480 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7481 num * sizeof(struct scsi_report_supported_opcodes_descr);
7483 case RSO_OPTIONS_OC:
7484 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7485 ctl_set_invalid_field(/*ctsio*/ ctsio,
7491 ctl_done((union ctl_io *)ctsio);
7492 return (CTL_RETVAL_COMPLETE);
7494 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7496 case RSO_OPTIONS_OC_SA:
7497 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7498 service_action >= 32) {
7499 ctl_set_invalid_field(/*ctsio*/ ctsio,
7505 ctl_done((union ctl_io *)ctsio);
7506 return (CTL_RETVAL_COMPLETE);
7508 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7511 ctl_set_invalid_field(/*ctsio*/ ctsio,
7517 ctl_done((union ctl_io *)ctsio);
7518 return (CTL_RETVAL_COMPLETE);
7521 alloc_len = scsi_4btoul(cdb->length);
7523 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7525 ctsio->kern_sg_entries = 0;
7527 if (total_len < alloc_len) {
7528 ctsio->residual = alloc_len - total_len;
7529 ctsio->kern_data_len = total_len;
7530 ctsio->kern_total_len = total_len;
7532 ctsio->residual = 0;
7533 ctsio->kern_data_len = alloc_len;
7534 ctsio->kern_total_len = alloc_len;
7536 ctsio->kern_data_resid = 0;
7537 ctsio->kern_rel_offset = 0;
7539 switch (cdb->options & RSO_OPTIONS_MASK) {
7540 case RSO_OPTIONS_ALL:
7541 all = (struct scsi_report_supported_opcodes_all *)
7542 ctsio->kern_data_ptr;
7544 for (i = 0; i < 256; i++) {
7545 entry = &ctl_cmd_table[i];
7546 if (entry->flags & CTL_CMD_FLAG_SA5) {
7547 for (j = 0; j < 32; j++) {
7548 sentry = &((const struct ctl_cmd_entry *)
7550 if (!ctl_cmd_applicable(
7551 lun->be_lun->lun_type, sentry))
7553 descr = &all->descr[num++];
7555 scsi_ulto2b(j, descr->service_action);
7556 descr->flags = RSO_SERVACTV;
7557 scsi_ulto2b(sentry->length,
7561 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7564 descr = &all->descr[num++];
7566 scsi_ulto2b(0, descr->service_action);
7568 scsi_ulto2b(entry->length, descr->cdb_length);
7572 num * sizeof(struct scsi_report_supported_opcodes_descr),
7575 case RSO_OPTIONS_OC:
7576 one = (struct scsi_report_supported_opcodes_one *)
7577 ctsio->kern_data_ptr;
7578 entry = &ctl_cmd_table[opcode];
7580 case RSO_OPTIONS_OC_SA:
7581 one = (struct scsi_report_supported_opcodes_one *)
7582 ctsio->kern_data_ptr;
7583 entry = &ctl_cmd_table[opcode];
7584 entry = &((const struct ctl_cmd_entry *)
7585 entry->execute)[service_action];
7587 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7589 scsi_ulto2b(entry->length, one->cdb_length);
7590 one->cdb_usage[0] = opcode;
7591 memcpy(&one->cdb_usage[1], entry->usage,
7598 ctl_set_success(ctsio);
7599 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7600 ctsio->be_move_done = ctl_config_move_done;
7601 ctl_datamove((union ctl_io *)ctsio);
7606 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7608 struct scsi_report_supported_tmf *cdb;
7609 struct scsi_report_supported_tmf_data *data;
7611 int alloc_len, total_len;
7613 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7615 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7617 retval = CTL_RETVAL_COMPLETE;
7619 total_len = sizeof(struct scsi_report_supported_tmf_data);
7620 alloc_len = scsi_4btoul(cdb->length);
7622 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7624 ctsio->kern_sg_entries = 0;
7626 if (total_len < alloc_len) {
7627 ctsio->residual = alloc_len - total_len;
7628 ctsio->kern_data_len = total_len;
7629 ctsio->kern_total_len = total_len;
7631 ctsio->residual = 0;
7632 ctsio->kern_data_len = alloc_len;
7633 ctsio->kern_total_len = alloc_len;
7635 ctsio->kern_data_resid = 0;
7636 ctsio->kern_rel_offset = 0;
7638 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr;
7639 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS;
7640 data->byte2 |= RST_ITNRS;
7642 ctl_set_success(ctsio);
7643 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7644 ctsio->be_move_done = ctl_config_move_done;
7645 ctl_datamove((union ctl_io *)ctsio);
7650 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7652 struct scsi_report_timestamp *cdb;
7653 struct scsi_report_timestamp_data *data;
7657 int alloc_len, total_len;
7659 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7661 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7663 retval = CTL_RETVAL_COMPLETE;
7665 total_len = sizeof(struct scsi_report_timestamp_data);
7666 alloc_len = scsi_4btoul(cdb->length);
7668 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7670 ctsio->kern_sg_entries = 0;
7672 if (total_len < alloc_len) {
7673 ctsio->residual = alloc_len - total_len;
7674 ctsio->kern_data_len = total_len;
7675 ctsio->kern_total_len = total_len;
7677 ctsio->residual = 0;
7678 ctsio->kern_data_len = alloc_len;
7679 ctsio->kern_total_len = alloc_len;
7681 ctsio->kern_data_resid = 0;
7682 ctsio->kern_rel_offset = 0;
7684 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7685 scsi_ulto2b(sizeof(*data) - 2, data->length);
7686 data->origin = RTS_ORIG_OUTSIDE;
7688 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7689 scsi_ulto4b(timestamp >> 16, data->timestamp);
7690 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7692 ctl_set_success(ctsio);
7693 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7694 ctsio->be_move_done = ctl_config_move_done;
7695 ctl_datamove((union ctl_io *)ctsio);
7700 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7702 struct scsi_per_res_in *cdb;
7703 int alloc_len, total_len = 0;
7704 /* struct scsi_per_res_in_rsrv in_data; */
7705 struct ctl_lun *lun;
7706 struct ctl_softc *softc;
7709 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7711 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7713 alloc_len = scsi_2btoul(cdb->length);
7715 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7716 softc = lun->ctl_softc;
7719 mtx_lock(&lun->lun_lock);
7720 switch (cdb->action) {
7721 case SPRI_RK: /* read keys */
7722 total_len = sizeof(struct scsi_per_res_in_keys) +
7724 sizeof(struct scsi_per_res_key);
7726 case SPRI_RR: /* read reservation */
7727 if (lun->flags & CTL_LUN_PR_RESERVED)
7728 total_len = sizeof(struct scsi_per_res_in_rsrv);
7730 total_len = sizeof(struct scsi_per_res_in_header);
7732 case SPRI_RC: /* report capabilities */
7733 total_len = sizeof(struct scsi_per_res_cap);
7735 case SPRI_RS: /* read full status */
7736 total_len = sizeof(struct scsi_per_res_in_header) +
7737 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7741 panic("Invalid PR type %x", cdb->action);
7743 mtx_unlock(&lun->lun_lock);
7745 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7747 if (total_len < alloc_len) {
7748 ctsio->residual = alloc_len - total_len;
7749 ctsio->kern_data_len = total_len;
7750 ctsio->kern_total_len = total_len;
7752 ctsio->residual = 0;
7753 ctsio->kern_data_len = alloc_len;
7754 ctsio->kern_total_len = alloc_len;
7757 ctsio->kern_data_resid = 0;
7758 ctsio->kern_rel_offset = 0;
7759 ctsio->kern_sg_entries = 0;
7761 mtx_lock(&lun->lun_lock);
7762 switch (cdb->action) {
7763 case SPRI_RK: { // read keys
7764 struct scsi_per_res_in_keys *res_keys;
7767 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7770 * We had to drop the lock to allocate our buffer, which
7771 * leaves time for someone to come in with another
7772 * persistent reservation. (That is unlikely, though,
7773 * since this should be the only persistent reservation
7774 * command active right now.)
7776 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7777 (lun->pr_key_count *
7778 sizeof(struct scsi_per_res_key)))){
7779 mtx_unlock(&lun->lun_lock);
7780 free(ctsio->kern_data_ptr, M_CTL);
7781 printf("%s: reservation length changed, retrying\n",
7786 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7788 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7789 lun->pr_key_count, res_keys->header.length);
7791 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7792 if ((key = ctl_get_prkey(lun, i)) == 0)
7796 * We used lun->pr_key_count to calculate the
7797 * size to allocate. If it turns out the number of
7798 * initiators with the registered flag set is
7799 * larger than that (i.e. they haven't been kept in
7800 * sync), we've got a problem.
7802 if (key_count >= lun->pr_key_count) {
7804 csevent_log(CSC_CTL | CSC_SHELF_SW |
7806 csevent_LogType_Fault,
7807 csevent_AlertLevel_Yellow,
7808 csevent_FRU_ShelfController,
7809 csevent_FRU_Firmware,
7810 csevent_FRU_Unknown,
7811 "registered keys %d >= key "
7812 "count %d", key_count,
7818 scsi_u64to8b(key, res_keys->keys[key_count].key);
7823 case SPRI_RR: { // read reservation
7824 struct scsi_per_res_in_rsrv *res;
7825 int tmp_len, header_only;
7827 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7829 scsi_ulto4b(lun->PRGeneration, res->header.generation);
7831 if (lun->flags & CTL_LUN_PR_RESERVED)
7833 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7834 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7835 res->header.length);
7838 tmp_len = sizeof(struct scsi_per_res_in_header);
7839 scsi_ulto4b(0, res->header.length);
7844 * We had to drop the lock to allocate our buffer, which
7845 * leaves time for someone to come in with another
7846 * persistent reservation. (That is unlikely, though,
7847 * since this should be the only persistent reservation
7848 * command active right now.)
7850 if (tmp_len != total_len) {
7851 mtx_unlock(&lun->lun_lock);
7852 free(ctsio->kern_data_ptr, M_CTL);
7853 printf("%s: reservation status changed, retrying\n",
7859 * No reservation held, so we're done.
7861 if (header_only != 0)
7865 * If the registration is an All Registrants type, the key
7866 * is 0, since it doesn't really matter.
7868 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7869 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7870 res->data.reservation);
7872 res->data.scopetype = lun->res_type;
7875 case SPRI_RC: //report capabilities
7877 struct scsi_per_res_cap *res_cap;
7880 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7881 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7882 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5;
7883 type_mask = SPRI_TM_WR_EX_AR |
7889 scsi_ulto2b(type_mask, res_cap->type_mask);
7892 case SPRI_RS: { // read full status
7893 struct scsi_per_res_in_full *res_status;
7894 struct scsi_per_res_in_full_desc *res_desc;
7895 struct ctl_port *port;
7898 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7901 * We had to drop the lock to allocate our buffer, which
7902 * leaves time for someone to come in with another
7903 * persistent reservation. (That is unlikely, though,
7904 * since this should be the only persistent reservation
7905 * command active right now.)
7907 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7908 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7909 lun->pr_key_count)){
7910 mtx_unlock(&lun->lun_lock);
7911 free(ctsio->kern_data_ptr, M_CTL);
7912 printf("%s: reservation length changed, retrying\n",
7917 scsi_ulto4b(lun->PRGeneration, res_status->header.generation);
7919 res_desc = &res_status->desc[0];
7920 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7921 if ((key = ctl_get_prkey(lun, i)) == 0)
7924 scsi_u64to8b(key, res_desc->res_key.key);
7925 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7926 (lun->pr_res_idx == i ||
7927 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7928 res_desc->flags = SPRI_FULL_R_HOLDER;
7929 res_desc->scopetype = lun->res_type;
7931 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7932 res_desc->rel_trgt_port_id);
7934 port = softc->ctl_ports[
7935 ctl_port_idx(i / CTL_MAX_INIT_PER_PORT)];
7937 len = ctl_create_iid(port,
7938 i % CTL_MAX_INIT_PER_PORT,
7939 res_desc->transport_id);
7940 scsi_ulto4b(len, res_desc->additional_length);
7941 res_desc = (struct scsi_per_res_in_full_desc *)
7942 &res_desc->transport_id[len];
7944 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7945 res_status->header.length);
7950 * This is a bug, because we just checked for this above,
7951 * and should have returned an error.
7953 panic("Invalid PR type %x", cdb->action);
7954 break; /* NOTREACHED */
7956 mtx_unlock(&lun->lun_lock);
7958 ctl_set_success(ctsio);
7959 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7960 ctsio->be_move_done = ctl_config_move_done;
7961 ctl_datamove((union ctl_io *)ctsio);
7962 return (CTL_RETVAL_COMPLETE);
7966 ctl_est_res_ua(struct ctl_lun *lun, uint32_t residx, ctl_ua_type ua)
7968 int off = lun->ctl_softc->persis_offset;
7970 if (residx >= off && residx < off + CTL_MAX_INITIATORS)
7971 ctl_est_ua(lun, residx - off, ua);
7975 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7979 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7980 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7981 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7982 struct scsi_per_res_out_parms* param)
7984 union ctl_ha_msg persis_io;
7990 mtx_lock(&lun->lun_lock);
7991 if (sa_res_key == 0) {
7992 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7993 /* validate scope and type */
7994 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7996 mtx_unlock(&lun->lun_lock);
7997 ctl_set_invalid_field(/*ctsio*/ ctsio,
8003 ctl_done((union ctl_io *)ctsio);
8007 if (type>8 || type==2 || type==4 || type==0) {
8008 mtx_unlock(&lun->lun_lock);
8009 ctl_set_invalid_field(/*ctsio*/ ctsio,
8015 ctl_done((union ctl_io *)ctsio);
8020 * Unregister everybody else and build UA for
8023 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8024 if (i == residx || ctl_get_prkey(lun, i) == 0)
8027 ctl_clr_prkey(lun, i);
8028 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8030 lun->pr_key_count = 1;
8031 lun->res_type = type;
8032 if (lun->res_type != SPR_TYPE_WR_EX_AR
8033 && lun->res_type != SPR_TYPE_EX_AC_AR)
8034 lun->pr_res_idx = residx;
8036 /* send msg to other side */
8037 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8038 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8039 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8040 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8041 persis_io.pr.pr_info.res_type = type;
8042 memcpy(persis_io.pr.pr_info.sa_res_key,
8043 param->serv_act_res_key,
8044 sizeof(param->serv_act_res_key));
8045 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8046 &persis_io, sizeof(persis_io), 0)) >
8047 CTL_HA_STATUS_SUCCESS) {
8048 printf("CTL:Persis Out error returned "
8049 "from ctl_ha_msg_send %d\n",
8053 /* not all registrants */
8054 mtx_unlock(&lun->lun_lock);
8055 free(ctsio->kern_data_ptr, M_CTL);
8056 ctl_set_invalid_field(ctsio,
8062 ctl_done((union ctl_io *)ctsio);
8065 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8066 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
8069 if (res_key == sa_res_key) {
8072 * The spec implies this is not good but doesn't
8073 * say what to do. There are two choices either
8074 * generate a res conflict or check condition
8075 * with illegal field in parameter data. Since
8076 * that is what is done when the sa_res_key is
8077 * zero I'll take that approach since this has
8078 * to do with the sa_res_key.
8080 mtx_unlock(&lun->lun_lock);
8081 free(ctsio->kern_data_ptr, M_CTL);
8082 ctl_set_invalid_field(ctsio,
8088 ctl_done((union ctl_io *)ctsio);
8092 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8093 if (ctl_get_prkey(lun, i) != sa_res_key)
8097 ctl_clr_prkey(lun, i);
8098 lun->pr_key_count--;
8099 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8102 mtx_unlock(&lun->lun_lock);
8103 free(ctsio->kern_data_ptr, M_CTL);
8104 ctl_set_reservation_conflict(ctsio);
8105 ctl_done((union ctl_io *)ctsio);
8106 return (CTL_RETVAL_COMPLETE);
8108 /* send msg to other side */
8109 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8110 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8111 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8112 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8113 persis_io.pr.pr_info.res_type = type;
8114 memcpy(persis_io.pr.pr_info.sa_res_key,
8115 param->serv_act_res_key,
8116 sizeof(param->serv_act_res_key));
8117 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8118 &persis_io, sizeof(persis_io), 0)) >
8119 CTL_HA_STATUS_SUCCESS) {
8120 printf("CTL:Persis Out error returned from "
8121 "ctl_ha_msg_send %d\n", isc_retval);
8124 /* Reserved but not all registrants */
8125 /* sa_res_key is res holder */
8126 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
8127 /* validate scope and type */
8128 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
8130 mtx_unlock(&lun->lun_lock);
8131 ctl_set_invalid_field(/*ctsio*/ ctsio,
8137 ctl_done((union ctl_io *)ctsio);
8141 if (type>8 || type==2 || type==4 || type==0) {
8142 mtx_unlock(&lun->lun_lock);
8143 ctl_set_invalid_field(/*ctsio*/ ctsio,
8149 ctl_done((union ctl_io *)ctsio);
8155 * if sa_res_key != res_key remove all
8156 * registrants w/sa_res_key and generate UA
8157 * for these registrants(Registrations
8158 * Preempted) if it wasn't an exclusive
8159 * reservation generate UA(Reservations
8160 * Preempted) for all other registered nexuses
8161 * if the type has changed. Establish the new
8162 * reservation and holder. If res_key and
8163 * sa_res_key are the same do the above
8164 * except don't unregister the res holder.
8167 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8168 if (i == residx || ctl_get_prkey(lun, i) == 0)
8171 if (sa_res_key == ctl_get_prkey(lun, i)) {
8172 ctl_clr_prkey(lun, i);
8173 lun->pr_key_count--;
8174 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8175 } else if (type != lun->res_type
8176 && (lun->res_type == SPR_TYPE_WR_EX_RO
8177 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
8178 ctl_est_res_ua(lun, i, CTL_UA_RES_RELEASE);
8181 lun->res_type = type;
8182 if (lun->res_type != SPR_TYPE_WR_EX_AR
8183 && lun->res_type != SPR_TYPE_EX_AC_AR)
8184 lun->pr_res_idx = residx;
8186 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8188 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8189 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8190 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8191 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8192 persis_io.pr.pr_info.res_type = type;
8193 memcpy(persis_io.pr.pr_info.sa_res_key,
8194 param->serv_act_res_key,
8195 sizeof(param->serv_act_res_key));
8196 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8197 &persis_io, sizeof(persis_io), 0)) >
8198 CTL_HA_STATUS_SUCCESS) {
8199 printf("CTL:Persis Out error returned "
8200 "from ctl_ha_msg_send %d\n",
8205 * sa_res_key is not the res holder just
8206 * remove registrants
8210 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8211 if (sa_res_key != ctl_get_prkey(lun, i))
8215 ctl_clr_prkey(lun, i);
8216 lun->pr_key_count--;
8217 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8221 mtx_unlock(&lun->lun_lock);
8222 free(ctsio->kern_data_ptr, M_CTL);
8223 ctl_set_reservation_conflict(ctsio);
8224 ctl_done((union ctl_io *)ctsio);
8227 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8228 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8229 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8230 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8231 persis_io.pr.pr_info.res_type = type;
8232 memcpy(persis_io.pr.pr_info.sa_res_key,
8233 param->serv_act_res_key,
8234 sizeof(param->serv_act_res_key));
8235 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8236 &persis_io, sizeof(persis_io), 0)) >
8237 CTL_HA_STATUS_SUCCESS) {
8238 printf("CTL:Persis Out error returned "
8239 "from ctl_ha_msg_send %d\n",
8245 lun->PRGeneration++;
8246 mtx_unlock(&lun->lun_lock);
8252 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8254 uint64_t sa_res_key;
8257 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8259 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8260 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8261 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
8262 if (sa_res_key == 0) {
8264 * Unregister everybody else and build UA for
8267 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8268 if (i == msg->pr.pr_info.residx ||
8269 ctl_get_prkey(lun, i) == 0)
8272 ctl_clr_prkey(lun, i);
8273 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8276 lun->pr_key_count = 1;
8277 lun->res_type = msg->pr.pr_info.res_type;
8278 if (lun->res_type != SPR_TYPE_WR_EX_AR
8279 && lun->res_type != SPR_TYPE_EX_AC_AR)
8280 lun->pr_res_idx = msg->pr.pr_info.residx;
8282 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8283 if (sa_res_key == ctl_get_prkey(lun, i))
8286 ctl_clr_prkey(lun, i);
8287 lun->pr_key_count--;
8288 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8292 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8293 if (i == msg->pr.pr_info.residx ||
8294 ctl_get_prkey(lun, i) == 0)
8297 if (sa_res_key == ctl_get_prkey(lun, i)) {
8298 ctl_clr_prkey(lun, i);
8299 lun->pr_key_count--;
8300 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8301 } else if (msg->pr.pr_info.res_type != lun->res_type
8302 && (lun->res_type == SPR_TYPE_WR_EX_RO
8303 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
8304 ctl_est_res_ua(lun, i, CTL_UA_RES_RELEASE);
8307 lun->res_type = msg->pr.pr_info.res_type;
8308 if (lun->res_type != SPR_TYPE_WR_EX_AR
8309 && lun->res_type != SPR_TYPE_EX_AC_AR)
8310 lun->pr_res_idx = msg->pr.pr_info.residx;
8312 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8314 lun->PRGeneration++;
8320 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8324 u_int32_t param_len;
8325 struct scsi_per_res_out *cdb;
8326 struct ctl_lun *lun;
8327 struct scsi_per_res_out_parms* param;
8328 struct ctl_softc *softc;
8330 uint64_t res_key, sa_res_key, key;
8332 union ctl_ha_msg persis_io;
8335 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8337 retval = CTL_RETVAL_COMPLETE;
8339 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8340 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8341 softc = lun->ctl_softc;
8344 * We only support whole-LUN scope. The scope & type are ignored for
8345 * register, register and ignore existing key and clear.
8346 * We sometimes ignore scope and type on preempts too!!
8347 * Verify reservation type here as well.
8349 type = cdb->scope_type & SPR_TYPE_MASK;
8350 if ((cdb->action == SPRO_RESERVE)
8351 || (cdb->action == SPRO_RELEASE)) {
8352 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8353 ctl_set_invalid_field(/*ctsio*/ ctsio,
8359 ctl_done((union ctl_io *)ctsio);
8360 return (CTL_RETVAL_COMPLETE);
8363 if (type>8 || type==2 || type==4 || type==0) {
8364 ctl_set_invalid_field(/*ctsio*/ ctsio,
8370 ctl_done((union ctl_io *)ctsio);
8371 return (CTL_RETVAL_COMPLETE);
8375 param_len = scsi_4btoul(cdb->length);
8377 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8378 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8379 ctsio->kern_data_len = param_len;
8380 ctsio->kern_total_len = param_len;
8381 ctsio->kern_data_resid = 0;
8382 ctsio->kern_rel_offset = 0;
8383 ctsio->kern_sg_entries = 0;
8384 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8385 ctsio->be_move_done = ctl_config_move_done;
8386 ctl_datamove((union ctl_io *)ctsio);
8388 return (CTL_RETVAL_COMPLETE);
8391 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8393 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8394 res_key = scsi_8btou64(param->res_key.key);
8395 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8398 * Validate the reservation key here except for SPRO_REG_IGNO
8399 * This must be done for all other service actions
8401 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8402 mtx_lock(&lun->lun_lock);
8403 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8404 if (res_key != key) {
8406 * The current key passed in doesn't match
8407 * the one the initiator previously
8410 mtx_unlock(&lun->lun_lock);
8411 free(ctsio->kern_data_ptr, M_CTL);
8412 ctl_set_reservation_conflict(ctsio);
8413 ctl_done((union ctl_io *)ctsio);
8414 return (CTL_RETVAL_COMPLETE);
8416 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8418 * We are not registered
8420 mtx_unlock(&lun->lun_lock);
8421 free(ctsio->kern_data_ptr, M_CTL);
8422 ctl_set_reservation_conflict(ctsio);
8423 ctl_done((union ctl_io *)ctsio);
8424 return (CTL_RETVAL_COMPLETE);
8425 } else if (res_key != 0) {
8427 * We are not registered and trying to register but
8428 * the register key isn't zero.
8430 mtx_unlock(&lun->lun_lock);
8431 free(ctsio->kern_data_ptr, M_CTL);
8432 ctl_set_reservation_conflict(ctsio);
8433 ctl_done((union ctl_io *)ctsio);
8434 return (CTL_RETVAL_COMPLETE);
8436 mtx_unlock(&lun->lun_lock);
8439 switch (cdb->action & SPRO_ACTION_MASK) {
8441 case SPRO_REG_IGNO: {
8444 printf("Registration received\n");
8448 * We don't support any of these options, as we report in
8449 * the read capabilities request (see
8450 * ctl_persistent_reserve_in(), above).
8452 if ((param->flags & SPR_SPEC_I_PT)
8453 || (param->flags & SPR_ALL_TG_PT)
8454 || (param->flags & SPR_APTPL)) {
8457 if (param->flags & SPR_APTPL)
8459 else if (param->flags & SPR_ALL_TG_PT)
8461 else /* SPR_SPEC_I_PT */
8464 free(ctsio->kern_data_ptr, M_CTL);
8465 ctl_set_invalid_field(ctsio,
8471 ctl_done((union ctl_io *)ctsio);
8472 return (CTL_RETVAL_COMPLETE);
8475 mtx_lock(&lun->lun_lock);
8478 * The initiator wants to clear the
8481 if (sa_res_key == 0) {
8483 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8484 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8485 && ctl_get_prkey(lun, residx) == 0)) {
8486 mtx_unlock(&lun->lun_lock);
8490 ctl_clr_prkey(lun, residx);
8491 lun->pr_key_count--;
8493 if (residx == lun->pr_res_idx) {
8494 lun->flags &= ~CTL_LUN_PR_RESERVED;
8495 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8497 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8498 || lun->res_type == SPR_TYPE_EX_AC_RO)
8499 && lun->pr_key_count) {
8501 * If the reservation is a registrants
8502 * only type we need to generate a UA
8503 * for other registered inits. The
8504 * sense code should be RESERVATIONS
8508 for (i = 0; i < CTL_MAX_INITIATORS;i++){
8509 if (ctl_get_prkey(lun, i +
8510 softc->persis_offset) == 0)
8513 CTL_UA_RES_RELEASE);
8517 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8518 if (lun->pr_key_count==0) {
8519 lun->flags &= ~CTL_LUN_PR_RESERVED;
8521 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8524 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8525 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8526 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8527 persis_io.pr.pr_info.residx = residx;
8528 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8529 &persis_io, sizeof(persis_io), 0 )) >
8530 CTL_HA_STATUS_SUCCESS) {
8531 printf("CTL:Persis Out error returned from "
8532 "ctl_ha_msg_send %d\n", isc_retval);
8534 } else /* sa_res_key != 0 */ {
8537 * If we aren't registered currently then increment
8538 * the key count and set the registered flag.
8540 ctl_alloc_prkey(lun, residx);
8541 if (ctl_get_prkey(lun, residx) == 0)
8542 lun->pr_key_count++;
8543 ctl_set_prkey(lun, residx, sa_res_key);
8545 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8546 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8547 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8548 persis_io.pr.pr_info.residx = residx;
8549 memcpy(persis_io.pr.pr_info.sa_res_key,
8550 param->serv_act_res_key,
8551 sizeof(param->serv_act_res_key));
8552 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8553 &persis_io, sizeof(persis_io), 0)) >
8554 CTL_HA_STATUS_SUCCESS) {
8555 printf("CTL:Persis Out error returned from "
8556 "ctl_ha_msg_send %d\n", isc_retval);
8559 lun->PRGeneration++;
8560 mtx_unlock(&lun->lun_lock);
8566 printf("Reserve executed type %d\n", type);
8568 mtx_lock(&lun->lun_lock);
8569 if (lun->flags & CTL_LUN_PR_RESERVED) {
8571 * if this isn't the reservation holder and it's
8572 * not a "all registrants" type or if the type is
8573 * different then we have a conflict
8575 if ((lun->pr_res_idx != residx
8576 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8577 || lun->res_type != type) {
8578 mtx_unlock(&lun->lun_lock);
8579 free(ctsio->kern_data_ptr, M_CTL);
8580 ctl_set_reservation_conflict(ctsio);
8581 ctl_done((union ctl_io *)ctsio);
8582 return (CTL_RETVAL_COMPLETE);
8584 mtx_unlock(&lun->lun_lock);
8585 } else /* create a reservation */ {
8587 * If it's not an "all registrants" type record
8588 * reservation holder
8590 if (type != SPR_TYPE_WR_EX_AR
8591 && type != SPR_TYPE_EX_AC_AR)
8592 lun->pr_res_idx = residx; /* Res holder */
8594 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8596 lun->flags |= CTL_LUN_PR_RESERVED;
8597 lun->res_type = type;
8599 mtx_unlock(&lun->lun_lock);
8601 /* send msg to other side */
8602 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8603 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8604 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8605 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8606 persis_io.pr.pr_info.res_type = type;
8607 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8608 &persis_io, sizeof(persis_io), 0)) >
8609 CTL_HA_STATUS_SUCCESS) {
8610 printf("CTL:Persis Out error returned from "
8611 "ctl_ha_msg_send %d\n", isc_retval);
8617 mtx_lock(&lun->lun_lock);
8618 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8619 /* No reservation exists return good status */
8620 mtx_unlock(&lun->lun_lock);
8624 * Is this nexus a reservation holder?
8626 if (lun->pr_res_idx != residx
8627 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8629 * not a res holder return good status but
8632 mtx_unlock(&lun->lun_lock);
8636 if (lun->res_type != type) {
8637 mtx_unlock(&lun->lun_lock);
8638 free(ctsio->kern_data_ptr, M_CTL);
8639 ctl_set_illegal_pr_release(ctsio);
8640 ctl_done((union ctl_io *)ctsio);
8641 return (CTL_RETVAL_COMPLETE);
8644 /* okay to release */
8645 lun->flags &= ~CTL_LUN_PR_RESERVED;
8646 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8650 * if this isn't an exclusive access
8651 * res generate UA for all other
8654 if (type != SPR_TYPE_EX_AC
8655 && type != SPR_TYPE_WR_EX) {
8656 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8659 i + softc->persis_offset) == 0)
8661 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8664 mtx_unlock(&lun->lun_lock);
8665 /* Send msg to other side */
8666 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8667 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8668 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8669 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io,
8670 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8671 printf("CTL:Persis Out error returned from "
8672 "ctl_ha_msg_send %d\n", isc_retval);
8677 /* send msg to other side */
8679 mtx_lock(&lun->lun_lock);
8680 lun->flags &= ~CTL_LUN_PR_RESERVED;
8682 lun->pr_key_count = 0;
8683 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8685 ctl_clr_prkey(lun, residx);
8686 for (i=0; i < 2*CTL_MAX_INITIATORS; i++)
8687 if (ctl_get_prkey(lun, i) != 0) {
8688 ctl_clr_prkey(lun, i);
8689 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8691 lun->PRGeneration++;
8692 mtx_unlock(&lun->lun_lock);
8693 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8694 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8695 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8696 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8697 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8698 printf("CTL:Persis Out error returned from "
8699 "ctl_ha_msg_send %d\n", isc_retval);
8704 case SPRO_PRE_ABO: {
8707 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8708 residx, ctsio, cdb, param);
8710 return (CTL_RETVAL_COMPLETE);
8714 panic("Invalid PR type %x", cdb->action);
8718 free(ctsio->kern_data_ptr, M_CTL);
8719 ctl_set_success(ctsio);
8720 ctl_done((union ctl_io *)ctsio);
8726 * This routine is for handling a message from the other SC pertaining to
8727 * persistent reserve out. All the error checking will have been done
8728 * so only perorming the action need be done here to keep the two
8732 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8734 struct ctl_lun *lun;
8735 struct ctl_softc *softc;
8739 softc = control_softc;
8741 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8742 lun = softc->ctl_luns[targ_lun];
8743 mtx_lock(&lun->lun_lock);
8744 switch(msg->pr.pr_info.action) {
8745 case CTL_PR_REG_KEY:
8746 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8747 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8748 lun->pr_key_count++;
8749 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8750 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8751 lun->PRGeneration++;
8754 case CTL_PR_UNREG_KEY:
8755 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8756 lun->pr_key_count--;
8758 /* XXX Need to see if the reservation has been released */
8759 /* if so do we need to generate UA? */
8760 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8761 lun->flags &= ~CTL_LUN_PR_RESERVED;
8762 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8764 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8765 || lun->res_type == SPR_TYPE_EX_AC_RO)
8766 && lun->pr_key_count) {
8768 * If the reservation is a registrants
8769 * only type we need to generate a UA
8770 * for other registered inits. The
8771 * sense code should be RESERVATIONS
8775 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8776 if (ctl_get_prkey(lun, i +
8777 softc->persis_offset) == 0)
8780 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8784 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8785 if (lun->pr_key_count==0) {
8786 lun->flags &= ~CTL_LUN_PR_RESERVED;
8788 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8791 lun->PRGeneration++;
8794 case CTL_PR_RESERVE:
8795 lun->flags |= CTL_LUN_PR_RESERVED;
8796 lun->res_type = msg->pr.pr_info.res_type;
8797 lun->pr_res_idx = msg->pr.pr_info.residx;
8801 case CTL_PR_RELEASE:
8803 * if this isn't an exclusive access res generate UA for all
8804 * other registrants.
8806 if (lun->res_type != SPR_TYPE_EX_AC
8807 && lun->res_type != SPR_TYPE_WR_EX) {
8808 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8809 if (ctl_get_prkey(lun, i + softc->persis_offset) != 0)
8810 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8813 lun->flags &= ~CTL_LUN_PR_RESERVED;
8814 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8818 case CTL_PR_PREEMPT:
8819 ctl_pro_preempt_other(lun, msg);
8822 lun->flags &= ~CTL_LUN_PR_RESERVED;
8824 lun->pr_key_count = 0;
8825 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8827 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8828 if (ctl_get_prkey(lun, i) == 0)
8830 ctl_clr_prkey(lun, i);
8831 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8833 lun->PRGeneration++;
8837 mtx_unlock(&lun->lun_lock);
8841 ctl_read_write(struct ctl_scsiio *ctsio)
8843 struct ctl_lun *lun;
8844 struct ctl_lba_len_flags *lbalen;
8846 uint32_t num_blocks;
8850 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8852 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8855 retval = CTL_RETVAL_COMPLETE;
8857 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8858 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8859 switch (ctsio->cdb[0]) {
8862 struct scsi_rw_6 *cdb;
8864 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8866 lba = scsi_3btoul(cdb->addr);
8867 /* only 5 bits are valid in the most significant address byte */
8869 num_blocks = cdb->length;
8871 * This is correct according to SBC-2.
8873 if (num_blocks == 0)
8879 struct scsi_rw_10 *cdb;
8881 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8882 if (cdb->byte2 & SRW10_FUA)
8883 flags |= CTL_LLF_FUA;
8884 if (cdb->byte2 & SRW10_DPO)
8885 flags |= CTL_LLF_DPO;
8886 lba = scsi_4btoul(cdb->addr);
8887 num_blocks = scsi_2btoul(cdb->length);
8890 case WRITE_VERIFY_10: {
8891 struct scsi_write_verify_10 *cdb;
8893 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8894 flags |= CTL_LLF_FUA;
8895 if (cdb->byte2 & SWV_DPO)
8896 flags |= CTL_LLF_DPO;
8897 lba = scsi_4btoul(cdb->addr);
8898 num_blocks = scsi_2btoul(cdb->length);
8903 struct scsi_rw_12 *cdb;
8905 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8906 if (cdb->byte2 & SRW12_FUA)
8907 flags |= CTL_LLF_FUA;
8908 if (cdb->byte2 & SRW12_DPO)
8909 flags |= CTL_LLF_DPO;
8910 lba = scsi_4btoul(cdb->addr);
8911 num_blocks = scsi_4btoul(cdb->length);
8914 case WRITE_VERIFY_12: {
8915 struct scsi_write_verify_12 *cdb;
8917 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8918 flags |= CTL_LLF_FUA;
8919 if (cdb->byte2 & SWV_DPO)
8920 flags |= CTL_LLF_DPO;
8921 lba = scsi_4btoul(cdb->addr);
8922 num_blocks = scsi_4btoul(cdb->length);
8927 struct scsi_rw_16 *cdb;
8929 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8930 if (cdb->byte2 & SRW12_FUA)
8931 flags |= CTL_LLF_FUA;
8932 if (cdb->byte2 & SRW12_DPO)
8933 flags |= CTL_LLF_DPO;
8934 lba = scsi_8btou64(cdb->addr);
8935 num_blocks = scsi_4btoul(cdb->length);
8938 case WRITE_ATOMIC_16: {
8939 struct scsi_rw_16 *cdb;
8941 if (lun->be_lun->atomicblock == 0) {
8942 ctl_set_invalid_opcode(ctsio);
8943 ctl_done((union ctl_io *)ctsio);
8944 return (CTL_RETVAL_COMPLETE);
8947 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8948 if (cdb->byte2 & SRW12_FUA)
8949 flags |= CTL_LLF_FUA;
8950 if (cdb->byte2 & SRW12_DPO)
8951 flags |= CTL_LLF_DPO;
8952 lba = scsi_8btou64(cdb->addr);
8953 num_blocks = scsi_4btoul(cdb->length);
8954 if (num_blocks > lun->be_lun->atomicblock) {
8955 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8956 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8958 ctl_done((union ctl_io *)ctsio);
8959 return (CTL_RETVAL_COMPLETE);
8963 case WRITE_VERIFY_16: {
8964 struct scsi_write_verify_16 *cdb;
8966 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8967 flags |= CTL_LLF_FUA;
8968 if (cdb->byte2 & SWV_DPO)
8969 flags |= CTL_LLF_DPO;
8970 lba = scsi_8btou64(cdb->addr);
8971 num_blocks = scsi_4btoul(cdb->length);
8976 * We got a command we don't support. This shouldn't
8977 * happen, commands should be filtered out above us.
8979 ctl_set_invalid_opcode(ctsio);
8980 ctl_done((union ctl_io *)ctsio);
8982 return (CTL_RETVAL_COMPLETE);
8983 break; /* NOTREACHED */
8987 * The first check is to make sure we're in bounds, the second
8988 * check is to catch wrap-around problems. If the lba + num blocks
8989 * is less than the lba, then we've wrapped around and the block
8990 * range is invalid anyway.
8992 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8993 || ((lba + num_blocks) < lba)) {
8994 ctl_set_lba_out_of_range(ctsio);
8995 ctl_done((union ctl_io *)ctsio);
8996 return (CTL_RETVAL_COMPLETE);
9000 * According to SBC-3, a transfer length of 0 is not an error.
9001 * Note that this cannot happen with WRITE(6) or READ(6), since 0
9002 * translates to 256 blocks for those commands.
9004 if (num_blocks == 0) {
9005 ctl_set_success(ctsio);
9006 ctl_done((union ctl_io *)ctsio);
9007 return (CTL_RETVAL_COMPLETE);
9010 /* Set FUA and/or DPO if caches are disabled. */
9012 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9014 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
9016 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9018 flags |= CTL_LLF_FUA;
9021 lbalen = (struct ctl_lba_len_flags *)
9022 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9024 lbalen->len = num_blocks;
9025 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
9027 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9028 ctsio->kern_rel_offset = 0;
9030 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
9032 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9038 ctl_cnw_cont(union ctl_io *io)
9040 struct ctl_scsiio *ctsio;
9041 struct ctl_lun *lun;
9042 struct ctl_lba_len_flags *lbalen;
9045 ctsio = &io->scsiio;
9046 ctsio->io_hdr.status = CTL_STATUS_NONE;
9047 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
9048 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9049 lbalen = (struct ctl_lba_len_flags *)
9050 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9051 lbalen->flags &= ~CTL_LLF_COMPARE;
9052 lbalen->flags |= CTL_LLF_WRITE;
9054 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
9055 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9060 ctl_cnw(struct ctl_scsiio *ctsio)
9062 struct ctl_lun *lun;
9063 struct ctl_lba_len_flags *lbalen;
9065 uint32_t num_blocks;
9068 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9070 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
9073 retval = CTL_RETVAL_COMPLETE;
9075 switch (ctsio->cdb[0]) {
9076 case COMPARE_AND_WRITE: {
9077 struct scsi_compare_and_write *cdb;
9079 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
9080 if (cdb->byte2 & SRW10_FUA)
9081 flags |= CTL_LLF_FUA;
9082 if (cdb->byte2 & SRW10_DPO)
9083 flags |= CTL_LLF_DPO;
9084 lba = scsi_8btou64(cdb->addr);
9085 num_blocks = cdb->length;
9090 * We got a command we don't support. This shouldn't
9091 * happen, commands should be filtered out above us.
9093 ctl_set_invalid_opcode(ctsio);
9094 ctl_done((union ctl_io *)ctsio);
9096 return (CTL_RETVAL_COMPLETE);
9097 break; /* NOTREACHED */
9101 * The first check is to make sure we're in bounds, the second
9102 * check is to catch wrap-around problems. If the lba + num blocks
9103 * is less than the lba, then we've wrapped around and the block
9104 * range is invalid anyway.
9106 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9107 || ((lba + num_blocks) < lba)) {
9108 ctl_set_lba_out_of_range(ctsio);
9109 ctl_done((union ctl_io *)ctsio);
9110 return (CTL_RETVAL_COMPLETE);
9114 * According to SBC-3, a transfer length of 0 is not an error.
9116 if (num_blocks == 0) {
9117 ctl_set_success(ctsio);
9118 ctl_done((union ctl_io *)ctsio);
9119 return (CTL_RETVAL_COMPLETE);
9122 /* Set FUA if write cache is disabled. */
9123 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9125 flags |= CTL_LLF_FUA;
9127 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
9128 ctsio->kern_rel_offset = 0;
9131 * Set the IO_CONT flag, so that if this I/O gets passed to
9132 * ctl_data_submit_done(), it'll get passed back to
9133 * ctl_ctl_cnw_cont() for further processing.
9135 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
9136 ctsio->io_cont = ctl_cnw_cont;
9138 lbalen = (struct ctl_lba_len_flags *)
9139 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9141 lbalen->len = num_blocks;
9142 lbalen->flags = CTL_LLF_COMPARE | flags;
9144 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
9145 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9150 ctl_verify(struct ctl_scsiio *ctsio)
9152 struct ctl_lun *lun;
9153 struct ctl_lba_len_flags *lbalen;
9155 uint32_t num_blocks;
9159 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9161 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
9164 flags = CTL_LLF_FUA;
9165 retval = CTL_RETVAL_COMPLETE;
9167 switch (ctsio->cdb[0]) {
9169 struct scsi_verify_10 *cdb;
9171 cdb = (struct scsi_verify_10 *)ctsio->cdb;
9172 if (cdb->byte2 & SVFY_BYTCHK)
9174 if (cdb->byte2 & SVFY_DPO)
9175 flags |= CTL_LLF_DPO;
9176 lba = scsi_4btoul(cdb->addr);
9177 num_blocks = scsi_2btoul(cdb->length);
9181 struct scsi_verify_12 *cdb;
9183 cdb = (struct scsi_verify_12 *)ctsio->cdb;
9184 if (cdb->byte2 & SVFY_BYTCHK)
9186 if (cdb->byte2 & SVFY_DPO)
9187 flags |= CTL_LLF_DPO;
9188 lba = scsi_4btoul(cdb->addr);
9189 num_blocks = scsi_4btoul(cdb->length);
9193 struct scsi_rw_16 *cdb;
9195 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9196 if (cdb->byte2 & SVFY_BYTCHK)
9198 if (cdb->byte2 & SVFY_DPO)
9199 flags |= CTL_LLF_DPO;
9200 lba = scsi_8btou64(cdb->addr);
9201 num_blocks = scsi_4btoul(cdb->length);
9206 * We got a command we don't support. This shouldn't
9207 * happen, commands should be filtered out above us.
9209 ctl_set_invalid_opcode(ctsio);
9210 ctl_done((union ctl_io *)ctsio);
9211 return (CTL_RETVAL_COMPLETE);
9215 * The first check is to make sure we're in bounds, the second
9216 * check is to catch wrap-around problems. If the lba + num blocks
9217 * is less than the lba, then we've wrapped around and the block
9218 * range is invalid anyway.
9220 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9221 || ((lba + num_blocks) < lba)) {
9222 ctl_set_lba_out_of_range(ctsio);
9223 ctl_done((union ctl_io *)ctsio);
9224 return (CTL_RETVAL_COMPLETE);
9228 * According to SBC-3, a transfer length of 0 is not an error.
9230 if (num_blocks == 0) {
9231 ctl_set_success(ctsio);
9232 ctl_done((union ctl_io *)ctsio);
9233 return (CTL_RETVAL_COMPLETE);
9236 lbalen = (struct ctl_lba_len_flags *)
9237 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9239 lbalen->len = num_blocks;
9241 lbalen->flags = CTL_LLF_COMPARE | flags;
9242 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9244 lbalen->flags = CTL_LLF_VERIFY | flags;
9245 ctsio->kern_total_len = 0;
9247 ctsio->kern_rel_offset = 0;
9249 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9250 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9255 ctl_report_luns(struct ctl_scsiio *ctsio)
9257 struct ctl_softc *softc = control_softc;
9258 struct scsi_report_luns *cdb;
9259 struct scsi_report_luns_data *lun_data;
9260 struct ctl_lun *lun, *request_lun;
9261 int num_luns, retval;
9262 uint32_t alloc_len, lun_datalen;
9263 int num_filled, well_known;
9264 uint32_t initidx, targ_lun_id, lun_id;
9266 retval = CTL_RETVAL_COMPLETE;
9269 cdb = (struct scsi_report_luns *)ctsio->cdb;
9271 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9273 mtx_lock(&softc->ctl_lock);
9274 num_luns = softc->num_luns;
9275 mtx_unlock(&softc->ctl_lock);
9277 switch (cdb->select_report) {
9278 case RPL_REPORT_DEFAULT:
9279 case RPL_REPORT_ALL:
9281 case RPL_REPORT_WELLKNOWN:
9286 ctl_set_invalid_field(ctsio,
9292 ctl_done((union ctl_io *)ctsio);
9294 break; /* NOTREACHED */
9297 alloc_len = scsi_4btoul(cdb->length);
9299 * The initiator has to allocate at least 16 bytes for this request,
9300 * so he can at least get the header and the first LUN. Otherwise
9301 * we reject the request (per SPC-3 rev 14, section 6.21).
9303 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9304 sizeof(struct scsi_report_luns_lundata))) {
9305 ctl_set_invalid_field(ctsio,
9311 ctl_done((union ctl_io *)ctsio);
9315 request_lun = (struct ctl_lun *)
9316 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9318 lun_datalen = sizeof(*lun_data) +
9319 (num_luns * sizeof(struct scsi_report_luns_lundata));
9321 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9322 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9323 ctsio->kern_sg_entries = 0;
9325 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9327 mtx_lock(&softc->ctl_lock);
9328 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9329 lun_id = ctl_map_lun(softc, ctsio->io_hdr.nexus.targ_port,
9331 if (lun_id >= CTL_MAX_LUNS)
9333 lun = softc->ctl_luns[lun_id];
9337 if (targ_lun_id <= 0xff) {
9339 * Peripheral addressing method, bus number 0.
9341 lun_data->luns[num_filled].lundata[0] =
9342 RPL_LUNDATA_ATYP_PERIPH;
9343 lun_data->luns[num_filled].lundata[1] = targ_lun_id;
9345 } else if (targ_lun_id <= 0x3fff) {
9347 * Flat addressing method.
9349 lun_data->luns[num_filled].lundata[0] =
9350 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8);
9351 lun_data->luns[num_filled].lundata[1] =
9352 (targ_lun_id & 0xff);
9354 } else if (targ_lun_id <= 0xffffff) {
9356 * Extended flat addressing method.
9358 lun_data->luns[num_filled].lundata[0] =
9359 RPL_LUNDATA_ATYP_EXTLUN | 0x12;
9360 scsi_ulto3b(targ_lun_id,
9361 &lun_data->luns[num_filled].lundata[1]);
9364 printf("ctl_report_luns: bogus LUN number %jd, "
9365 "skipping\n", (intmax_t)targ_lun_id);
9368 * According to SPC-3, rev 14 section 6.21:
9370 * "The execution of a REPORT LUNS command to any valid and
9371 * installed logical unit shall clear the REPORTED LUNS DATA
9372 * HAS CHANGED unit attention condition for all logical
9373 * units of that target with respect to the requesting
9374 * initiator. A valid and installed logical unit is one
9375 * having a PERIPHERAL QUALIFIER of 000b in the standard
9376 * INQUIRY data (see 6.4.2)."
9378 * If request_lun is NULL, the LUN this report luns command
9379 * was issued to is either disabled or doesn't exist. In that
9380 * case, we shouldn't clear any pending lun change unit
9383 if (request_lun != NULL) {
9384 mtx_lock(&lun->lun_lock);
9385 ctl_clr_ua(lun, initidx, CTL_UA_RES_RELEASE);
9386 mtx_unlock(&lun->lun_lock);
9389 mtx_unlock(&softc->ctl_lock);
9392 * It's quite possible that we've returned fewer LUNs than we allocated
9393 * space for. Trim it.
9395 lun_datalen = sizeof(*lun_data) +
9396 (num_filled * sizeof(struct scsi_report_luns_lundata));
9398 if (lun_datalen < alloc_len) {
9399 ctsio->residual = alloc_len - lun_datalen;
9400 ctsio->kern_data_len = lun_datalen;
9401 ctsio->kern_total_len = lun_datalen;
9403 ctsio->residual = 0;
9404 ctsio->kern_data_len = alloc_len;
9405 ctsio->kern_total_len = alloc_len;
9407 ctsio->kern_data_resid = 0;
9408 ctsio->kern_rel_offset = 0;
9409 ctsio->kern_sg_entries = 0;
9412 * We set this to the actual data length, regardless of how much
9413 * space we actually have to return results. If the user looks at
9414 * this value, he'll know whether or not he allocated enough space
9415 * and reissue the command if necessary. We don't support well
9416 * known logical units, so if the user asks for that, return none.
9418 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9421 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9424 ctl_set_success(ctsio);
9425 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9426 ctsio->be_move_done = ctl_config_move_done;
9427 ctl_datamove((union ctl_io *)ctsio);
9432 ctl_request_sense(struct ctl_scsiio *ctsio)
9434 struct scsi_request_sense *cdb;
9435 struct scsi_sense_data *sense_ptr;
9436 struct ctl_lun *lun;
9439 scsi_sense_data_type sense_format;
9440 ctl_ua_type ua_type;
9442 cdb = (struct scsi_request_sense *)ctsio->cdb;
9444 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9446 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9449 * Determine which sense format the user wants.
9451 if (cdb->byte2 & SRS_DESC)
9452 sense_format = SSD_TYPE_DESC;
9454 sense_format = SSD_TYPE_FIXED;
9456 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9457 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9458 ctsio->kern_sg_entries = 0;
9461 * struct scsi_sense_data, which is currently set to 256 bytes, is
9462 * larger than the largest allowed value for the length field in the
9463 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9465 ctsio->residual = 0;
9466 ctsio->kern_data_len = cdb->length;
9467 ctsio->kern_total_len = cdb->length;
9469 ctsio->kern_data_resid = 0;
9470 ctsio->kern_rel_offset = 0;
9471 ctsio->kern_sg_entries = 0;
9474 * If we don't have a LUN, we don't have any pending sense.
9480 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9482 * Check for pending sense, and then for pending unit attentions.
9483 * Pending sense gets returned first, then pending unit attentions.
9485 mtx_lock(&lun->lun_lock);
9487 if (ctl_is_set(lun->have_ca, initidx)) {
9488 scsi_sense_data_type stored_format;
9491 * Check to see which sense format was used for the stored
9494 stored_format = scsi_sense_type(&lun->pending_sense[initidx]);
9497 * If the user requested a different sense format than the
9498 * one we stored, then we need to convert it to the other
9499 * format. If we're going from descriptor to fixed format
9500 * sense data, we may lose things in translation, depending
9501 * on what options were used.
9503 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9504 * for some reason we'll just copy it out as-is.
9506 if ((stored_format == SSD_TYPE_FIXED)
9507 && (sense_format == SSD_TYPE_DESC))
9508 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9509 &lun->pending_sense[initidx],
9510 (struct scsi_sense_data_desc *)sense_ptr);
9511 else if ((stored_format == SSD_TYPE_DESC)
9512 && (sense_format == SSD_TYPE_FIXED))
9513 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9514 &lun->pending_sense[initidx],
9515 (struct scsi_sense_data_fixed *)sense_ptr);
9517 memcpy(sense_ptr, &lun->pending_sense[initidx],
9518 MIN(sizeof(*sense_ptr),
9519 sizeof(lun->pending_sense[initidx])));
9521 ctl_clear_mask(lun->have_ca, initidx);
9526 ua_type = ctl_build_ua(lun, initidx, sense_ptr, sense_format);
9527 if (ua_type != CTL_UA_NONE)
9530 mtx_unlock(&lun->lun_lock);
9533 * We already have a pending error, return it.
9535 if (have_error != 0) {
9537 * We report the SCSI status as OK, since the status of the
9538 * request sense command itself is OK.
9539 * We report 0 for the sense length, because we aren't doing
9540 * autosense in this case. We're reporting sense as
9543 ctl_set_success(ctsio);
9544 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9545 ctsio->be_move_done = ctl_config_move_done;
9546 ctl_datamove((union ctl_io *)ctsio);
9547 return (CTL_RETVAL_COMPLETE);
9553 * No sense information to report, so we report that everything is
9556 ctl_set_sense_data(sense_ptr,
9559 /*current_error*/ 1,
9560 /*sense_key*/ SSD_KEY_NO_SENSE,
9566 * We report 0 for the sense length, because we aren't doing
9567 * autosense in this case. We're reporting sense as parameter data.
9569 ctl_set_success(ctsio);
9570 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9571 ctsio->be_move_done = ctl_config_move_done;
9572 ctl_datamove((union ctl_io *)ctsio);
9573 return (CTL_RETVAL_COMPLETE);
9577 ctl_tur(struct ctl_scsiio *ctsio)
9580 CTL_DEBUG_PRINT(("ctl_tur\n"));
9582 ctl_set_success(ctsio);
9583 ctl_done((union ctl_io *)ctsio);
9585 return (CTL_RETVAL_COMPLETE);
9590 ctl_cmddt_inquiry(struct ctl_scsiio *ctsio)
9597 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9599 struct scsi_vpd_supported_pages *pages;
9601 struct ctl_lun *lun;
9604 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9606 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9607 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9608 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9609 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9610 ctsio->kern_sg_entries = 0;
9612 if (sup_page_size < alloc_len) {
9613 ctsio->residual = alloc_len - sup_page_size;
9614 ctsio->kern_data_len = sup_page_size;
9615 ctsio->kern_total_len = sup_page_size;
9617 ctsio->residual = 0;
9618 ctsio->kern_data_len = alloc_len;
9619 ctsio->kern_total_len = alloc_len;
9621 ctsio->kern_data_resid = 0;
9622 ctsio->kern_rel_offset = 0;
9623 ctsio->kern_sg_entries = 0;
9626 * The control device is always connected. The disk device, on the
9627 * other hand, may not be online all the time. Need to change this
9628 * to figure out whether the disk device is actually online or not.
9631 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9632 lun->be_lun->lun_type;
9634 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9637 /* Supported VPD pages */
9638 pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9640 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9641 /* Device Identification */
9642 pages->page_list[p++] = SVPD_DEVICE_ID;
9643 /* Extended INQUIRY Data */
9644 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9645 /* Mode Page Policy */
9646 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9648 pages->page_list[p++] = SVPD_SCSI_PORTS;
9649 /* Third-party Copy */
9650 pages->page_list[p++] = SVPD_SCSI_TPC;
9651 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9653 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9654 /* Block Device Characteristics */
9655 pages->page_list[p++] = SVPD_BDC;
9656 /* Logical Block Provisioning */
9657 pages->page_list[p++] = SVPD_LBP;
9661 ctl_set_success(ctsio);
9662 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9663 ctsio->be_move_done = ctl_config_move_done;
9664 ctl_datamove((union ctl_io *)ctsio);
9665 return (CTL_RETVAL_COMPLETE);
9669 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9671 struct scsi_vpd_unit_serial_number *sn_ptr;
9672 struct ctl_lun *lun;
9675 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9677 data_len = 4 + CTL_SN_LEN;
9678 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9679 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9680 if (data_len < alloc_len) {
9681 ctsio->residual = alloc_len - data_len;
9682 ctsio->kern_data_len = data_len;
9683 ctsio->kern_total_len = data_len;
9685 ctsio->residual = 0;
9686 ctsio->kern_data_len = alloc_len;
9687 ctsio->kern_total_len = alloc_len;
9689 ctsio->kern_data_resid = 0;
9690 ctsio->kern_rel_offset = 0;
9691 ctsio->kern_sg_entries = 0;
9694 * The control device is always connected. The disk device, on the
9695 * other hand, may not be online all the time. Need to change this
9696 * to figure out whether the disk device is actually online or not.
9699 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9700 lun->be_lun->lun_type;
9702 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9704 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9705 sn_ptr->length = CTL_SN_LEN;
9707 * If we don't have a LUN, we just leave the serial number as
9711 strncpy((char *)sn_ptr->serial_num,
9712 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9714 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9716 ctl_set_success(ctsio);
9717 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9718 ctsio->be_move_done = ctl_config_move_done;
9719 ctl_datamove((union ctl_io *)ctsio);
9720 return (CTL_RETVAL_COMPLETE);
9725 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9727 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9728 struct ctl_lun *lun;
9731 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9733 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9734 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9735 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9736 ctsio->kern_sg_entries = 0;
9738 if (data_len < alloc_len) {
9739 ctsio->residual = alloc_len - data_len;
9740 ctsio->kern_data_len = data_len;
9741 ctsio->kern_total_len = data_len;
9743 ctsio->residual = 0;
9744 ctsio->kern_data_len = alloc_len;
9745 ctsio->kern_total_len = alloc_len;
9747 ctsio->kern_data_resid = 0;
9748 ctsio->kern_rel_offset = 0;
9749 ctsio->kern_sg_entries = 0;
9752 * The control device is always connected. The disk device, on the
9753 * other hand, may not be online all the time.
9756 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9757 lun->be_lun->lun_type;
9759 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9760 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9761 eid_ptr->page_length = data_len - 4;
9762 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9763 eid_ptr->flags3 = SVPD_EID_V_SUP;
9765 ctl_set_success(ctsio);
9766 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9767 ctsio->be_move_done = ctl_config_move_done;
9768 ctl_datamove((union ctl_io *)ctsio);
9769 return (CTL_RETVAL_COMPLETE);
9773 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9775 struct scsi_vpd_mode_page_policy *mpp_ptr;
9776 struct ctl_lun *lun;
9779 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9781 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9782 sizeof(struct scsi_vpd_mode_page_policy_descr);
9784 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9785 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9786 ctsio->kern_sg_entries = 0;
9788 if (data_len < alloc_len) {
9789 ctsio->residual = alloc_len - data_len;
9790 ctsio->kern_data_len = data_len;
9791 ctsio->kern_total_len = data_len;
9793 ctsio->residual = 0;
9794 ctsio->kern_data_len = alloc_len;
9795 ctsio->kern_total_len = alloc_len;
9797 ctsio->kern_data_resid = 0;
9798 ctsio->kern_rel_offset = 0;
9799 ctsio->kern_sg_entries = 0;
9802 * The control device is always connected. The disk device, on the
9803 * other hand, may not be online all the time.
9806 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9807 lun->be_lun->lun_type;
9809 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9810 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9811 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9812 mpp_ptr->descr[0].page_code = 0x3f;
9813 mpp_ptr->descr[0].subpage_code = 0xff;
9814 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9816 ctl_set_success(ctsio);
9817 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9818 ctsio->be_move_done = ctl_config_move_done;
9819 ctl_datamove((union ctl_io *)ctsio);
9820 return (CTL_RETVAL_COMPLETE);
9824 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9826 struct scsi_vpd_device_id *devid_ptr;
9827 struct scsi_vpd_id_descriptor *desc;
9828 struct ctl_softc *softc;
9829 struct ctl_lun *lun;
9830 struct ctl_port *port;
9834 softc = control_softc;
9836 port = softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)];
9837 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9839 data_len = sizeof(struct scsi_vpd_device_id) +
9840 sizeof(struct scsi_vpd_id_descriptor) +
9841 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9842 sizeof(struct scsi_vpd_id_descriptor) +
9843 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9844 if (lun && lun->lun_devid)
9845 data_len += lun->lun_devid->len;
9846 if (port->port_devid)
9847 data_len += port->port_devid->len;
9848 if (port->target_devid)
9849 data_len += port->target_devid->len;
9851 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9852 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9853 ctsio->kern_sg_entries = 0;
9855 if (data_len < alloc_len) {
9856 ctsio->residual = alloc_len - data_len;
9857 ctsio->kern_data_len = data_len;
9858 ctsio->kern_total_len = data_len;
9860 ctsio->residual = 0;
9861 ctsio->kern_data_len = alloc_len;
9862 ctsio->kern_total_len = alloc_len;
9864 ctsio->kern_data_resid = 0;
9865 ctsio->kern_rel_offset = 0;
9866 ctsio->kern_sg_entries = 0;
9869 * The control device is always connected. The disk device, on the
9870 * other hand, may not be online all the time.
9873 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9874 lun->be_lun->lun_type;
9876 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9877 devid_ptr->page_code = SVPD_DEVICE_ID;
9878 scsi_ulto2b(data_len - 4, devid_ptr->length);
9880 if (port->port_type == CTL_PORT_FC)
9881 proto = SCSI_PROTO_FC << 4;
9882 else if (port->port_type == CTL_PORT_ISCSI)
9883 proto = SCSI_PROTO_ISCSI << 4;
9885 proto = SCSI_PROTO_SPI << 4;
9886 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9889 * We're using a LUN association here. i.e., this device ID is a
9890 * per-LUN identifier.
9892 if (lun && lun->lun_devid) {
9893 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9894 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9895 lun->lun_devid->len);
9899 * This is for the WWPN which is a port association.
9901 if (port->port_devid) {
9902 memcpy(desc, port->port_devid->data, port->port_devid->len);
9903 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9904 port->port_devid->len);
9908 * This is for the Relative Target Port(type 4h) identifier
9910 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9911 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9912 SVPD_ID_TYPE_RELTARG;
9914 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9915 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9916 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9919 * This is for the Target Port Group(type 5h) identifier
9921 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9922 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9923 SVPD_ID_TYPE_TPORTGRP;
9925 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1,
9926 &desc->identifier[2]);
9927 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9928 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9931 * This is for the Target identifier
9933 if (port->target_devid) {
9934 memcpy(desc, port->target_devid->data, port->target_devid->len);
9937 ctl_set_success(ctsio);
9938 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9939 ctsio->be_move_done = ctl_config_move_done;
9940 ctl_datamove((union ctl_io *)ctsio);
9941 return (CTL_RETVAL_COMPLETE);
9945 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9947 struct ctl_softc *softc = control_softc;
9948 struct scsi_vpd_scsi_ports *sp;
9949 struct scsi_vpd_port_designation *pd;
9950 struct scsi_vpd_port_designation_cont *pdc;
9951 struct ctl_lun *lun;
9952 struct ctl_port *port;
9953 int data_len, num_target_ports, iid_len, id_len, g, pg, p;
9954 int num_target_port_groups;
9956 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9958 if (softc->is_single)
9959 num_target_port_groups = 1;
9961 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
9962 num_target_ports = 0;
9965 mtx_lock(&softc->ctl_lock);
9966 STAILQ_FOREACH(port, &softc->port_list, links) {
9967 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9970 ctl_map_lun_back(softc, port->targ_port, lun->lun) >=
9974 if (port->init_devid)
9975 iid_len += port->init_devid->len;
9976 if (port->port_devid)
9977 id_len += port->port_devid->len;
9979 mtx_unlock(&softc->ctl_lock);
9981 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups *
9982 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9983 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9984 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9985 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9986 ctsio->kern_sg_entries = 0;
9988 if (data_len < alloc_len) {
9989 ctsio->residual = alloc_len - data_len;
9990 ctsio->kern_data_len = data_len;
9991 ctsio->kern_total_len = data_len;
9993 ctsio->residual = 0;
9994 ctsio->kern_data_len = alloc_len;
9995 ctsio->kern_total_len = alloc_len;
9997 ctsio->kern_data_resid = 0;
9998 ctsio->kern_rel_offset = 0;
9999 ctsio->kern_sg_entries = 0;
10002 * The control device is always connected. The disk device, on the
10003 * other hand, may not be online all the time. Need to change this
10004 * to figure out whether the disk device is actually online or not.
10007 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
10008 lun->be_lun->lun_type;
10010 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10012 sp->page_code = SVPD_SCSI_PORTS;
10013 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
10015 pd = &sp->design[0];
10017 mtx_lock(&softc->ctl_lock);
10018 pg = softc->port_offset / CTL_MAX_PORTS;
10019 for (g = 0; g < num_target_port_groups; g++) {
10020 STAILQ_FOREACH(port, &softc->port_list, links) {
10021 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
10024 ctl_map_lun_back(softc, port->targ_port, lun->lun)
10027 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS;
10028 scsi_ulto2b(p, pd->relative_port_id);
10029 if (port->init_devid && g == pg) {
10030 iid_len = port->init_devid->len;
10031 memcpy(pd->initiator_transportid,
10032 port->init_devid->data, port->init_devid->len);
10035 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
10036 pdc = (struct scsi_vpd_port_designation_cont *)
10037 (&pd->initiator_transportid[iid_len]);
10038 if (port->port_devid && g == pg) {
10039 id_len = port->port_devid->len;
10040 memcpy(pdc->target_port_descriptors,
10041 port->port_devid->data, port->port_devid->len);
10044 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
10045 pd = (struct scsi_vpd_port_designation *)
10046 ((uint8_t *)pdc->target_port_descriptors + id_len);
10049 mtx_unlock(&softc->ctl_lock);
10051 ctl_set_success(ctsio);
10052 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10053 ctsio->be_move_done = ctl_config_move_done;
10054 ctl_datamove((union ctl_io *)ctsio);
10055 return (CTL_RETVAL_COMPLETE);
10059 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
10061 struct scsi_vpd_block_limits *bl_ptr;
10062 struct ctl_lun *lun;
10065 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10067 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
10068 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
10069 ctsio->kern_sg_entries = 0;
10071 if (sizeof(*bl_ptr) < alloc_len) {
10072 ctsio->residual = alloc_len - sizeof(*bl_ptr);
10073 ctsio->kern_data_len = sizeof(*bl_ptr);
10074 ctsio->kern_total_len = sizeof(*bl_ptr);
10076 ctsio->residual = 0;
10077 ctsio->kern_data_len = alloc_len;
10078 ctsio->kern_total_len = alloc_len;
10080 ctsio->kern_data_resid = 0;
10081 ctsio->kern_rel_offset = 0;
10082 ctsio->kern_sg_entries = 0;
10085 * The control device is always connected. The disk device, on the
10086 * other hand, may not be online all the time. Need to change this
10087 * to figure out whether the disk device is actually online or not.
10090 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10091 lun->be_lun->lun_type;
10093 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10095 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
10096 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
10097 bl_ptr->max_cmp_write_len = 0xff;
10098 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
10100 bs = lun->be_lun->blocksize;
10101 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
10102 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10103 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
10104 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
10105 if (lun->be_lun->ublockexp != 0) {
10106 scsi_ulto4b((1 << lun->be_lun->ublockexp),
10107 bl_ptr->opt_unmap_grain);
10108 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
10109 bl_ptr->unmap_grain_align);
10112 scsi_ulto4b(lun->be_lun->atomicblock,
10113 bl_ptr->max_atomic_transfer_length);
10114 scsi_ulto4b(0, bl_ptr->atomic_alignment);
10115 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
10117 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
10119 ctl_set_success(ctsio);
10120 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10121 ctsio->be_move_done = ctl_config_move_done;
10122 ctl_datamove((union ctl_io *)ctsio);
10123 return (CTL_RETVAL_COMPLETE);
10127 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
10129 struct scsi_vpd_block_device_characteristics *bdc_ptr;
10130 struct ctl_lun *lun;
10134 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10136 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
10137 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
10138 ctsio->kern_sg_entries = 0;
10140 if (sizeof(*bdc_ptr) < alloc_len) {
10141 ctsio->residual = alloc_len - sizeof(*bdc_ptr);
10142 ctsio->kern_data_len = sizeof(*bdc_ptr);
10143 ctsio->kern_total_len = sizeof(*bdc_ptr);
10145 ctsio->residual = 0;
10146 ctsio->kern_data_len = alloc_len;
10147 ctsio->kern_total_len = alloc_len;
10149 ctsio->kern_data_resid = 0;
10150 ctsio->kern_rel_offset = 0;
10151 ctsio->kern_sg_entries = 0;
10154 * The control device is always connected. The disk device, on the
10155 * other hand, may not be online all the time. Need to change this
10156 * to figure out whether the disk device is actually online or not.
10159 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10160 lun->be_lun->lun_type;
10162 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10163 bdc_ptr->page_code = SVPD_BDC;
10164 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
10166 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL)
10167 i = strtol(value, NULL, 0);
10169 i = CTL_DEFAULT_ROTATION_RATE;
10170 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
10172 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL)
10173 i = strtol(value, NULL, 0);
10176 bdc_ptr->wab_wac_ff = (i & 0x0f);
10177 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
10179 ctl_set_success(ctsio);
10180 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10181 ctsio->be_move_done = ctl_config_move_done;
10182 ctl_datamove((union ctl_io *)ctsio);
10183 return (CTL_RETVAL_COMPLETE);
10187 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
10189 struct scsi_vpd_logical_block_prov *lbp_ptr;
10190 struct ctl_lun *lun;
10192 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10194 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
10195 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
10196 ctsio->kern_sg_entries = 0;
10198 if (sizeof(*lbp_ptr) < alloc_len) {
10199 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
10200 ctsio->kern_data_len = sizeof(*lbp_ptr);
10201 ctsio->kern_total_len = sizeof(*lbp_ptr);
10203 ctsio->residual = 0;
10204 ctsio->kern_data_len = alloc_len;
10205 ctsio->kern_total_len = alloc_len;
10207 ctsio->kern_data_resid = 0;
10208 ctsio->kern_rel_offset = 0;
10209 ctsio->kern_sg_entries = 0;
10212 * The control device is always connected. The disk device, on the
10213 * other hand, may not be online all the time. Need to change this
10214 * to figure out whether the disk device is actually online or not.
10217 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10218 lun->be_lun->lun_type;
10220 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10222 lbp_ptr->page_code = SVPD_LBP;
10223 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
10224 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
10225 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10226 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
10227 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
10228 lbp_ptr->prov_type = SVPD_LBP_THIN;
10231 ctl_set_success(ctsio);
10232 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10233 ctsio->be_move_done = ctl_config_move_done;
10234 ctl_datamove((union ctl_io *)ctsio);
10235 return (CTL_RETVAL_COMPLETE);
10239 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
10241 struct ctl_lun *lun;
10242 struct scsi_inquiry *cdb;
10243 int alloc_len, retval;
10245 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10246 cdb = (struct scsi_inquiry *)ctsio->cdb;
10247 alloc_len = scsi_2btoul(cdb->length);
10249 switch (cdb->page_code) {
10250 case SVPD_SUPPORTED_PAGES:
10251 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
10253 case SVPD_UNIT_SERIAL_NUMBER:
10254 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
10256 case SVPD_DEVICE_ID:
10257 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
10259 case SVPD_EXTENDED_INQUIRY_DATA:
10260 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
10262 case SVPD_MODE_PAGE_POLICY:
10263 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
10265 case SVPD_SCSI_PORTS:
10266 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10268 case SVPD_SCSI_TPC:
10269 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10271 case SVPD_BLOCK_LIMITS:
10272 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10274 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10277 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10279 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10282 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10284 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10288 ctl_set_invalid_field(ctsio,
10294 ctl_done((union ctl_io *)ctsio);
10295 retval = CTL_RETVAL_COMPLETE;
10303 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10305 struct scsi_inquiry_data *inq_ptr;
10306 struct scsi_inquiry *cdb;
10307 struct ctl_softc *softc;
10308 struct ctl_lun *lun;
10310 uint32_t alloc_len, data_len;
10311 ctl_port_type port_type;
10313 softc = control_softc;
10316 * Figure out whether we're talking to a Fibre Channel port or not.
10317 * We treat the ioctl front end, and any SCSI adapters, as packetized
10320 port_type = softc->ctl_ports[
10321 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type;
10322 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10323 port_type = CTL_PORT_SCSI;
10325 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10326 cdb = (struct scsi_inquiry *)ctsio->cdb;
10327 alloc_len = scsi_2btoul(cdb->length);
10330 * We malloc the full inquiry data size here and fill it
10331 * in. If the user only asks for less, we'll give him
10334 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10335 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10336 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10337 ctsio->kern_sg_entries = 0;
10338 ctsio->kern_data_resid = 0;
10339 ctsio->kern_rel_offset = 0;
10341 if (data_len < alloc_len) {
10342 ctsio->residual = alloc_len - data_len;
10343 ctsio->kern_data_len = data_len;
10344 ctsio->kern_total_len = data_len;
10346 ctsio->residual = 0;
10347 ctsio->kern_data_len = alloc_len;
10348 ctsio->kern_total_len = alloc_len;
10352 * If we have a LUN configured, report it as connected. Otherwise,
10353 * report that it is offline or no device is supported, depending
10354 * on the value of inquiry_pq_no_lun.
10356 * According to the spec (SPC-4 r34), the peripheral qualifier
10357 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario:
10359 * "A peripheral device having the specified peripheral device type
10360 * is not connected to this logical unit. However, the device
10361 * server is capable of supporting the specified peripheral device
10362 * type on this logical unit."
10364 * According to the same spec, the peripheral qualifier
10365 * SID_QUAL_BAD_LU (011b) is used in this scenario:
10367 * "The device server is not capable of supporting a peripheral
10368 * device on this logical unit. For this peripheral qualifier the
10369 * peripheral device type shall be set to 1Fh. All other peripheral
10370 * device type values are reserved for this peripheral qualifier."
10372 * Given the text, it would seem that we probably want to report that
10373 * the LUN is offline here. There is no LUN connected, but we can
10374 * support a LUN at the given LUN number.
10376 * In the real world, though, it sounds like things are a little
10379 * - Linux, when presented with a LUN with the offline peripheral
10380 * qualifier, will create an sg driver instance for it. So when
10381 * you attach it to CTL, you wind up with a ton of sg driver
10382 * instances. (One for every LUN that Linux bothered to probe.)
10383 * Linux does this despite the fact that it issues a REPORT LUNs
10384 * to LUN 0 to get the inventory of supported LUNs.
10386 * - There is other anecdotal evidence (from Emulex folks) about
10387 * arrays that use the offline peripheral qualifier for LUNs that
10388 * are on the "passive" path in an active/passive array.
10390 * So the solution is provide a hopefully reasonable default
10391 * (return bad/no LUN) and allow the user to change the behavior
10392 * with a tunable/sysctl variable.
10395 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10396 lun->be_lun->lun_type;
10397 else if (softc->inquiry_pq_no_lun == 0)
10398 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10400 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10402 /* RMB in byte 2 is 0 */
10403 inq_ptr->version = SCSI_REV_SPC4;
10406 * According to SAM-3, even if a device only supports a single
10407 * level of LUN addressing, it should still set the HISUP bit:
10409 * 4.9.1 Logical unit numbers overview
10411 * All logical unit number formats described in this standard are
10412 * hierarchical in structure even when only a single level in that
10413 * hierarchy is used. The HISUP bit shall be set to one in the
10414 * standard INQUIRY data (see SPC-2) when any logical unit number
10415 * format described in this standard is used. Non-hierarchical
10416 * formats are outside the scope of this standard.
10418 * Therefore we set the HiSup bit here.
10420 * The reponse format is 2, per SPC-3.
10422 inq_ptr->response_format = SID_HiSup | 2;
10424 inq_ptr->additional_length = data_len -
10425 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10426 CTL_DEBUG_PRINT(("additional_length = %d\n",
10427 inq_ptr->additional_length));
10429 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10430 /* 16 bit addressing */
10431 if (port_type == CTL_PORT_SCSI)
10432 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10433 /* XXX set the SID_MultiP bit here if we're actually going to
10434 respond on multiple ports */
10435 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10437 /* 16 bit data bus, synchronous transfers */
10438 if (port_type == CTL_PORT_SCSI)
10439 inq_ptr->flags = SID_WBus16 | SID_Sync;
10441 * XXX KDM do we want to support tagged queueing on the control
10445 || (lun->be_lun->lun_type != T_PROCESSOR))
10446 inq_ptr->flags |= SID_CmdQue;
10448 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10449 * We have 8 bytes for the vendor name, and 16 bytes for the device
10450 * name and 4 bytes for the revision.
10452 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10453 "vendor")) == NULL) {
10454 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10456 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10457 strncpy(inq_ptr->vendor, val,
10458 min(sizeof(inq_ptr->vendor), strlen(val)));
10461 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10462 sizeof(inq_ptr->product));
10463 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10464 switch (lun->be_lun->lun_type) {
10466 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10467 sizeof(inq_ptr->product));
10470 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10471 sizeof(inq_ptr->product));
10474 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10475 sizeof(inq_ptr->product));
10479 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10480 strncpy(inq_ptr->product, val,
10481 min(sizeof(inq_ptr->product), strlen(val)));
10485 * XXX make this a macro somewhere so it automatically gets
10486 * incremented when we make changes.
10488 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10489 "revision")) == NULL) {
10490 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10492 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10493 strncpy(inq_ptr->revision, val,
10494 min(sizeof(inq_ptr->revision), strlen(val)));
10498 * For parallel SCSI, we support double transition and single
10499 * transition clocking. We also support QAS (Quick Arbitration
10500 * and Selection) and Information Unit transfers on both the
10501 * control and array devices.
10503 if (port_type == CTL_PORT_SCSI)
10504 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10507 /* SAM-5 (no version claimed) */
10508 scsi_ulto2b(0x00A0, inq_ptr->version1);
10509 /* SPC-4 (no version claimed) */
10510 scsi_ulto2b(0x0460, inq_ptr->version2);
10511 if (port_type == CTL_PORT_FC) {
10512 /* FCP-2 ANSI INCITS.350:2003 */
10513 scsi_ulto2b(0x0917, inq_ptr->version3);
10514 } else if (port_type == CTL_PORT_SCSI) {
10515 /* SPI-4 ANSI INCITS.362:200x */
10516 scsi_ulto2b(0x0B56, inq_ptr->version3);
10517 } else if (port_type == CTL_PORT_ISCSI) {
10518 /* iSCSI (no version claimed) */
10519 scsi_ulto2b(0x0960, inq_ptr->version3);
10520 } else if (port_type == CTL_PORT_SAS) {
10521 /* SAS (no version claimed) */
10522 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10526 /* SBC-4 (no version claimed) */
10527 scsi_ulto2b(0x0600, inq_ptr->version4);
10529 switch (lun->be_lun->lun_type) {
10531 /* SBC-4 (no version claimed) */
10532 scsi_ulto2b(0x0600, inq_ptr->version4);
10540 ctl_set_success(ctsio);
10541 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10542 ctsio->be_move_done = ctl_config_move_done;
10543 ctl_datamove((union ctl_io *)ctsio);
10544 return (CTL_RETVAL_COMPLETE);
10548 ctl_inquiry(struct ctl_scsiio *ctsio)
10550 struct scsi_inquiry *cdb;
10553 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10555 cdb = (struct scsi_inquiry *)ctsio->cdb;
10556 if (cdb->byte2 & SI_EVPD)
10557 retval = ctl_inquiry_evpd(ctsio);
10558 else if (cdb->page_code == 0)
10559 retval = ctl_inquiry_std(ctsio);
10561 ctl_set_invalid_field(ctsio,
10567 ctl_done((union ctl_io *)ctsio);
10568 return (CTL_RETVAL_COMPLETE);
10575 * For known CDB types, parse the LBA and length.
10578 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10580 if (io->io_hdr.io_type != CTL_IO_SCSI)
10583 switch (io->scsiio.cdb[0]) {
10584 case COMPARE_AND_WRITE: {
10585 struct scsi_compare_and_write *cdb;
10587 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10589 *lba = scsi_8btou64(cdb->addr);
10590 *len = cdb->length;
10595 struct scsi_rw_6 *cdb;
10597 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10599 *lba = scsi_3btoul(cdb->addr);
10600 /* only 5 bits are valid in the most significant address byte */
10602 *len = cdb->length;
10607 struct scsi_rw_10 *cdb;
10609 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10611 *lba = scsi_4btoul(cdb->addr);
10612 *len = scsi_2btoul(cdb->length);
10615 case WRITE_VERIFY_10: {
10616 struct scsi_write_verify_10 *cdb;
10618 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10620 *lba = scsi_4btoul(cdb->addr);
10621 *len = scsi_2btoul(cdb->length);
10626 struct scsi_rw_12 *cdb;
10628 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10630 *lba = scsi_4btoul(cdb->addr);
10631 *len = scsi_4btoul(cdb->length);
10634 case WRITE_VERIFY_12: {
10635 struct scsi_write_verify_12 *cdb;
10637 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10639 *lba = scsi_4btoul(cdb->addr);
10640 *len = scsi_4btoul(cdb->length);
10645 case WRITE_ATOMIC_16: {
10646 struct scsi_rw_16 *cdb;
10648 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10650 *lba = scsi_8btou64(cdb->addr);
10651 *len = scsi_4btoul(cdb->length);
10654 case WRITE_VERIFY_16: {
10655 struct scsi_write_verify_16 *cdb;
10657 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10659 *lba = scsi_8btou64(cdb->addr);
10660 *len = scsi_4btoul(cdb->length);
10663 case WRITE_SAME_10: {
10664 struct scsi_write_same_10 *cdb;
10666 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10668 *lba = scsi_4btoul(cdb->addr);
10669 *len = scsi_2btoul(cdb->length);
10672 case WRITE_SAME_16: {
10673 struct scsi_write_same_16 *cdb;
10675 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10677 *lba = scsi_8btou64(cdb->addr);
10678 *len = scsi_4btoul(cdb->length);
10682 struct scsi_verify_10 *cdb;
10684 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10686 *lba = scsi_4btoul(cdb->addr);
10687 *len = scsi_2btoul(cdb->length);
10691 struct scsi_verify_12 *cdb;
10693 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10695 *lba = scsi_4btoul(cdb->addr);
10696 *len = scsi_4btoul(cdb->length);
10700 struct scsi_verify_16 *cdb;
10702 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10704 *lba = scsi_8btou64(cdb->addr);
10705 *len = scsi_4btoul(cdb->length);
10713 case SERVICE_ACTION_IN: { /* GET LBA STATUS */
10714 struct scsi_get_lba_status *cdb;
10716 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10717 *lba = scsi_8btou64(cdb->addr);
10723 break; /* NOTREACHED */
10730 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10733 uint64_t endlba1, endlba2;
10735 endlba1 = lba1 + len1 - (seq ? 0 : 1);
10736 endlba2 = lba2 + len2 - 1;
10738 if ((endlba1 < lba2) || (endlba2 < lba1))
10739 return (CTL_ACTION_PASS);
10741 return (CTL_ACTION_BLOCK);
10745 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10747 struct ctl_ptr_len_flags *ptrlen;
10748 struct scsi_unmap_desc *buf, *end, *range;
10752 /* If not UNMAP -- go other way. */
10753 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10754 io->scsiio.cdb[0] != UNMAP)
10755 return (CTL_ACTION_ERROR);
10757 /* If UNMAP without data -- block and wait for data. */
10758 ptrlen = (struct ctl_ptr_len_flags *)
10759 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10760 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10761 ptrlen->ptr == NULL)
10762 return (CTL_ACTION_BLOCK);
10764 /* UNMAP with data -- check for collision. */
10765 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10766 end = buf + ptrlen->len / sizeof(*buf);
10767 for (range = buf; range < end; range++) {
10768 lba = scsi_8btou64(range->lba);
10769 len = scsi_4btoul(range->length);
10770 if ((lba < lba2 + len2) && (lba + len > lba2))
10771 return (CTL_ACTION_BLOCK);
10773 return (CTL_ACTION_PASS);
10777 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10779 uint64_t lba1, lba2;
10780 uint64_t len1, len2;
10783 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10784 return (CTL_ACTION_ERROR);
10786 retval = ctl_extent_check_unmap(io1, lba2, len2);
10787 if (retval != CTL_ACTION_ERROR)
10790 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10791 return (CTL_ACTION_ERROR);
10793 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
10797 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2)
10799 uint64_t lba1, lba2;
10800 uint64_t len1, len2;
10802 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10803 return (CTL_ACTION_ERROR);
10804 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10805 return (CTL_ACTION_ERROR);
10807 if (lba1 + len1 == lba2)
10808 return (CTL_ACTION_BLOCK);
10809 return (CTL_ACTION_PASS);
10813 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10814 union ctl_io *ooa_io)
10816 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10817 ctl_serialize_action *serialize_row;
10820 * The initiator attempted multiple untagged commands at the same
10821 * time. Can't do that.
10823 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10824 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10825 && ((pending_io->io_hdr.nexus.targ_port ==
10826 ooa_io->io_hdr.nexus.targ_port)
10827 && (pending_io->io_hdr.nexus.initid.id ==
10828 ooa_io->io_hdr.nexus.initid.id))
10829 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10830 return (CTL_ACTION_OVERLAP);
10833 * The initiator attempted to send multiple tagged commands with
10834 * the same ID. (It's fine if different initiators have the same
10837 * Even if all of those conditions are true, we don't kill the I/O
10838 * if the command ahead of us has been aborted. We won't end up
10839 * sending it to the FETD, and it's perfectly legal to resend a
10840 * command with the same tag number as long as the previous
10841 * instance of this tag number has been aborted somehow.
10843 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10844 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10845 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10846 && ((pending_io->io_hdr.nexus.targ_port ==
10847 ooa_io->io_hdr.nexus.targ_port)
10848 && (pending_io->io_hdr.nexus.initid.id ==
10849 ooa_io->io_hdr.nexus.initid.id))
10850 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10851 return (CTL_ACTION_OVERLAP_TAG);
10854 * If we get a head of queue tag, SAM-3 says that we should
10855 * immediately execute it.
10857 * What happens if this command would normally block for some other
10858 * reason? e.g. a request sense with a head of queue tag
10859 * immediately after a write. Normally that would block, but this
10860 * will result in its getting executed immediately...
10862 * We currently return "pass" instead of "skip", so we'll end up
10863 * going through the rest of the queue to check for overlapped tags.
10865 * XXX KDM check for other types of blockage first??
10867 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10868 return (CTL_ACTION_PASS);
10871 * Ordered tags have to block until all items ahead of them
10872 * have completed. If we get called with an ordered tag, we always
10873 * block, if something else is ahead of us in the queue.
10875 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10876 return (CTL_ACTION_BLOCK);
10879 * Simple tags get blocked until all head of queue and ordered tags
10880 * ahead of them have completed. I'm lumping untagged commands in
10881 * with simple tags here. XXX KDM is that the right thing to do?
10883 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10884 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10885 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10886 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10887 return (CTL_ACTION_BLOCK);
10889 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
10890 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
10892 serialize_row = ctl_serialize_table[ooa_entry->seridx];
10894 switch (serialize_row[pending_entry->seridx]) {
10895 case CTL_SER_BLOCK:
10896 return (CTL_ACTION_BLOCK);
10897 case CTL_SER_EXTENT:
10898 return (ctl_extent_check(ooa_io, pending_io,
10899 (lun->serseq == CTL_LUN_SERSEQ_ON)));
10900 case CTL_SER_EXTENTOPT:
10901 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
10902 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
10903 return (ctl_extent_check(ooa_io, pending_io,
10904 (lun->serseq == CTL_LUN_SERSEQ_ON)));
10905 return (CTL_ACTION_PASS);
10906 case CTL_SER_EXTENTSEQ:
10907 if (lun->serseq != CTL_LUN_SERSEQ_OFF)
10908 return (ctl_extent_check_seq(ooa_io, pending_io));
10909 return (CTL_ACTION_PASS);
10911 return (CTL_ACTION_PASS);
10912 case CTL_SER_BLOCKOPT:
10913 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
10914 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
10915 return (CTL_ACTION_BLOCK);
10916 return (CTL_ACTION_PASS);
10918 return (CTL_ACTION_SKIP);
10920 panic("invalid serialization value %d",
10921 serialize_row[pending_entry->seridx]);
10924 return (CTL_ACTION_ERROR);
10928 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
10930 * - pending_io is generally either incoming, or on the blocked queue
10931 * - starting I/O is the I/O we want to start the check with.
10934 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
10935 union ctl_io *starting_io)
10937 union ctl_io *ooa_io;
10940 mtx_assert(&lun->lun_lock, MA_OWNED);
10943 * Run back along the OOA queue, starting with the current
10944 * blocked I/O and going through every I/O before it on the
10945 * queue. If starting_io is NULL, we'll just end up returning
10948 for (ooa_io = starting_io; ooa_io != NULL;
10949 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
10953 * This routine just checks to see whether
10954 * cur_blocked is blocked by ooa_io, which is ahead
10955 * of it in the queue. It doesn't queue/dequeue
10958 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
10960 case CTL_ACTION_BLOCK:
10961 case CTL_ACTION_OVERLAP:
10962 case CTL_ACTION_OVERLAP_TAG:
10963 case CTL_ACTION_SKIP:
10964 case CTL_ACTION_ERROR:
10966 break; /* NOTREACHED */
10967 case CTL_ACTION_PASS:
10970 panic("invalid action %d", action);
10971 break; /* NOTREACHED */
10975 return (CTL_ACTION_PASS);
10980 * - An I/O has just completed, and has been removed from the per-LUN OOA
10981 * queue, so some items on the blocked queue may now be unblocked.
10984 ctl_check_blocked(struct ctl_lun *lun)
10986 union ctl_io *cur_blocked, *next_blocked;
10988 mtx_assert(&lun->lun_lock, MA_OWNED);
10991 * Run forward from the head of the blocked queue, checking each
10992 * entry against the I/Os prior to it on the OOA queue to see if
10993 * there is still any blockage.
10995 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
10996 * with our removing a variable on it while it is traversing the
10999 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
11000 cur_blocked != NULL; cur_blocked = next_blocked) {
11001 union ctl_io *prev_ooa;
11004 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
11007 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
11008 ctl_ooaq, ooa_links);
11011 * If cur_blocked happens to be the first item in the OOA
11012 * queue now, prev_ooa will be NULL, and the action
11013 * returned will just be CTL_ACTION_PASS.
11015 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
11018 case CTL_ACTION_BLOCK:
11019 /* Nothing to do here, still blocked */
11021 case CTL_ACTION_OVERLAP:
11022 case CTL_ACTION_OVERLAP_TAG:
11024 * This shouldn't happen! In theory we've already
11025 * checked this command for overlap...
11028 case CTL_ACTION_PASS:
11029 case CTL_ACTION_SKIP: {
11030 const struct ctl_cmd_entry *entry;
11034 * The skip case shouldn't happen, this transaction
11035 * should have never made it onto the blocked queue.
11038 * This I/O is no longer blocked, we can remove it
11039 * from the blocked queue. Since this is a TAILQ
11040 * (doubly linked list), we can do O(1) removals
11041 * from any place on the list.
11043 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
11045 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11047 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){
11049 * Need to send IO back to original side to
11052 union ctl_ha_msg msg_info;
11054 msg_info.hdr.original_sc =
11055 cur_blocked->io_hdr.original_sc;
11056 msg_info.hdr.serializing_sc = cur_blocked;
11057 msg_info.hdr.msg_type = CTL_MSG_R2R;
11058 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11059 &msg_info, sizeof(msg_info), 0)) >
11060 CTL_HA_STATUS_SUCCESS) {
11061 printf("CTL:Check Blocked error from "
11062 "ctl_ha_msg_send %d\n",
11067 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
11070 * Check this I/O for LUN state changes that may
11071 * have happened while this command was blocked.
11072 * The LUN state may have been changed by a command
11073 * ahead of us in the queue, so we need to re-check
11074 * for any states that can be caused by SCSI
11077 if (ctl_scsiio_lun_check(lun, entry,
11078 &cur_blocked->scsiio) == 0) {
11079 cur_blocked->io_hdr.flags |=
11080 CTL_FLAG_IS_WAS_ON_RTR;
11081 ctl_enqueue_rtr(cur_blocked);
11083 ctl_done(cur_blocked);
11088 * This probably shouldn't happen -- we shouldn't
11089 * get CTL_ACTION_ERROR, or anything else.
11095 return (CTL_RETVAL_COMPLETE);
11099 * This routine (with one exception) checks LUN flags that can be set by
11100 * commands ahead of us in the OOA queue. These flags have to be checked
11101 * when a command initially comes in, and when we pull a command off the
11102 * blocked queue and are preparing to execute it. The reason we have to
11103 * check these flags for commands on the blocked queue is that the LUN
11104 * state may have been changed by a command ahead of us while we're on the
11107 * Ordering is somewhat important with these checks, so please pay
11108 * careful attention to the placement of any new checks.
11111 ctl_scsiio_lun_check(struct ctl_lun *lun,
11112 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11114 struct ctl_softc *softc = lun->ctl_softc;
11120 mtx_assert(&lun->lun_lock, MA_OWNED);
11123 * If this shelf is a secondary shelf controller, we have to reject
11124 * any media access commands.
11126 if ((softc->flags & CTL_FLAG_ACTIVE_SHELF) == 0 &&
11127 (entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0) {
11128 ctl_set_lun_standby(ctsio);
11133 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11134 if (lun->flags & CTL_LUN_READONLY) {
11135 ctl_set_sense(ctsio, /*current_error*/ 1,
11136 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11137 /*asc*/ 0x27, /*ascq*/ 0x01, SSD_ELEM_NONE);
11141 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT]
11142 .eca_and_aen & SCP_SWP) != 0) {
11143 ctl_set_sense(ctsio, /*current_error*/ 1,
11144 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11145 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11152 * Check for a reservation conflict. If this command isn't allowed
11153 * even on reserved LUNs, and if this initiator isn't the one who
11154 * reserved us, reject the command with a reservation conflict.
11156 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
11157 if ((lun->flags & CTL_LUN_RESERVED)
11158 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11159 if (lun->res_idx != residx) {
11160 ctl_set_reservation_conflict(ctsio);
11166 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11167 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11168 /* No reservation or command is allowed. */;
11169 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11170 (lun->res_type == SPR_TYPE_WR_EX ||
11171 lun->res_type == SPR_TYPE_WR_EX_RO ||
11172 lun->res_type == SPR_TYPE_WR_EX_AR)) {
11173 /* The command is allowed for Write Exclusive resv. */;
11176 * if we aren't registered or it's a res holder type
11177 * reservation and this isn't the res holder then set a
11180 if (ctl_get_prkey(lun, residx) == 0
11181 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
11182 ctl_set_reservation_conflict(ctsio);
11189 if ((lun->flags & CTL_LUN_OFFLINE)
11190 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) {
11191 ctl_set_lun_not_ready(ctsio);
11197 * If the LUN is stopped, see if this particular command is allowed
11198 * for a stopped lun. Otherwise, reject it with 0x04,0x02.
11200 if ((lun->flags & CTL_LUN_STOPPED)
11201 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
11202 /* "Logical unit not ready, initializing cmd. required" */
11203 ctl_set_lun_stopped(ctsio);
11208 if ((lun->flags & CTL_LUN_INOPERABLE)
11209 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
11210 /* "Medium format corrupted" */
11211 ctl_set_medium_format_corrupted(ctsio);
11222 ctl_failover_io(union ctl_io *io, int have_lock)
11224 ctl_set_busy(&io->scsiio);
11231 struct ctl_lun *lun;
11232 struct ctl_softc *softc;
11233 union ctl_io *next_io, *pending_io;
11237 softc = control_softc;
11239 mtx_lock(&softc->ctl_lock);
11241 * Remove any cmds from the other SC from the rtr queue. These
11242 * will obviously only be for LUNs for which we're the primary.
11243 * We can't send status or get/send data for these commands.
11244 * Since they haven't been executed yet, we can just remove them.
11245 * We'll either abort them or delete them below, depending on
11246 * which HA mode we're in.
11249 mtx_lock(&softc->queue_lock);
11250 for (io = (union ctl_io *)STAILQ_FIRST(&softc->rtr_queue);
11251 io != NULL; io = next_io) {
11252 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
11253 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
11254 STAILQ_REMOVE(&softc->rtr_queue, &io->io_hdr,
11255 ctl_io_hdr, links);
11257 mtx_unlock(&softc->queue_lock);
11260 for (lun_idx=0; lun_idx < softc->num_luns; lun_idx++) {
11261 lun = softc->ctl_luns[lun_idx];
11266 * Processor LUNs are primary on both sides.
11267 * XXX will this always be true?
11269 if (lun->be_lun->lun_type == T_PROCESSOR)
11272 if ((lun->flags & CTL_LUN_PRIMARY_SC)
11273 && (softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
11274 printf("FAILOVER: primary lun %d\n", lun_idx);
11276 * Remove all commands from the other SC. First from the
11277 * blocked queue then from the ooa queue. Once we have
11278 * removed them. Call ctl_check_blocked to see if there
11279 * is anything that can run.
11281 for (io = (union ctl_io *)TAILQ_FIRST(
11282 &lun->blocked_queue); io != NULL; io = next_io) {
11284 next_io = (union ctl_io *)TAILQ_NEXT(
11285 &io->io_hdr, blocked_links);
11287 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
11288 TAILQ_REMOVE(&lun->blocked_queue,
11289 &io->io_hdr,blocked_links);
11290 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11291 TAILQ_REMOVE(&lun->ooa_queue,
11292 &io->io_hdr, ooa_links);
11298 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
11299 io != NULL; io = next_io) {
11301 next_io = (union ctl_io *)TAILQ_NEXT(
11302 &io->io_hdr, ooa_links);
11304 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
11306 TAILQ_REMOVE(&lun->ooa_queue,
11313 ctl_check_blocked(lun);
11314 } else if ((lun->flags & CTL_LUN_PRIMARY_SC)
11315 && (softc->ha_mode == CTL_HA_MODE_XFER)) {
11317 printf("FAILOVER: primary lun %d\n", lun_idx);
11319 * Abort all commands from the other SC. We can't
11320 * send status back for them now. These should get
11321 * cleaned up when they are completed or come out
11322 * for a datamove operation.
11324 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
11325 io != NULL; io = next_io) {
11326 next_io = (union ctl_io *)TAILQ_NEXT(
11327 &io->io_hdr, ooa_links);
11329 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
11330 io->io_hdr.flags |= CTL_FLAG_ABORT;
11332 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
11333 && (softc->ha_mode == CTL_HA_MODE_XFER)) {
11335 printf("FAILOVER: secondary lun %d\n", lun_idx);
11337 lun->flags |= CTL_LUN_PRIMARY_SC;
11340 * We send all I/O that was sent to this controller
11341 * and redirected to the other side back with
11342 * busy status, and have the initiator retry it.
11343 * Figuring out how much data has been transferred,
11344 * etc. and picking up where we left off would be
11347 * XXX KDM need to remove I/O from the blocked
11350 for (pending_io = (union ctl_io *)TAILQ_FIRST(
11351 &lun->ooa_queue); pending_io != NULL;
11352 pending_io = next_io) {
11354 next_io = (union ctl_io *)TAILQ_NEXT(
11355 &pending_io->io_hdr, ooa_links);
11357 pending_io->io_hdr.flags &=
11358 ~CTL_FLAG_SENT_2OTHER_SC;
11360 if (pending_io->io_hdr.flags &
11361 CTL_FLAG_IO_ACTIVE) {
11362 pending_io->io_hdr.flags |=
11365 ctl_set_busy(&pending_io->scsiio);
11366 ctl_done(pending_io);
11370 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
11371 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
11372 && (softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
11373 printf("FAILOVER: secondary lun %d\n", lun_idx);
11375 * if the first io on the OOA is not on the RtR queue
11378 lun->flags |= CTL_LUN_PRIMARY_SC;
11380 pending_io = (union ctl_io *)TAILQ_FIRST(
11382 if (pending_io==NULL) {
11383 printf("Nothing on OOA queue\n");
11387 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11388 if ((pending_io->io_hdr.flags &
11389 CTL_FLAG_IS_WAS_ON_RTR) == 0) {
11390 pending_io->io_hdr.flags |=
11391 CTL_FLAG_IS_WAS_ON_RTR;
11392 ctl_enqueue_rtr(pending_io);
11397 printf("Tag 0x%04x is running\n",
11398 pending_io->scsiio.tag_num);
11402 next_io = (union ctl_io *)TAILQ_NEXT(
11403 &pending_io->io_hdr, ooa_links);
11404 for (pending_io=next_io; pending_io != NULL;
11405 pending_io = next_io) {
11406 pending_io->io_hdr.flags &=
11407 ~CTL_FLAG_SENT_2OTHER_SC;
11408 next_io = (union ctl_io *)TAILQ_NEXT(
11409 &pending_io->io_hdr, ooa_links);
11410 if (pending_io->io_hdr.flags &
11411 CTL_FLAG_IS_WAS_ON_RTR) {
11413 printf("Tag 0x%04x is running\n",
11414 pending_io->scsiio.tag_num);
11419 switch (ctl_check_ooa(lun, pending_io,
11420 (union ctl_io *)TAILQ_PREV(
11421 &pending_io->io_hdr, ctl_ooaq,
11424 case CTL_ACTION_BLOCK:
11425 TAILQ_INSERT_TAIL(&lun->blocked_queue,
11426 &pending_io->io_hdr,
11428 pending_io->io_hdr.flags |=
11431 case CTL_ACTION_PASS:
11432 case CTL_ACTION_SKIP:
11433 pending_io->io_hdr.flags |=
11434 CTL_FLAG_IS_WAS_ON_RTR;
11435 ctl_enqueue_rtr(pending_io);
11437 case CTL_ACTION_OVERLAP:
11438 ctl_set_overlapped_cmd(
11439 (struct ctl_scsiio *)pending_io);
11440 ctl_done(pending_io);
11442 case CTL_ACTION_OVERLAP_TAG:
11443 ctl_set_overlapped_tag(
11444 (struct ctl_scsiio *)pending_io,
11445 pending_io->scsiio.tag_num & 0xff);
11446 ctl_done(pending_io);
11448 case CTL_ACTION_ERROR:
11450 ctl_set_internal_failure(
11451 (struct ctl_scsiio *)pending_io,
11454 ctl_done(pending_io);
11459 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
11461 panic("Unhandled HA mode failover, LUN flags = %#x, "
11462 "ha_mode = #%x", lun->flags, softc->ha_mode);
11466 mtx_unlock(&softc->ctl_lock);
11470 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio)
11472 struct ctl_lun *lun;
11473 const struct ctl_cmd_entry *entry;
11474 uint32_t initidx, targ_lun;
11481 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11482 if ((targ_lun < CTL_MAX_LUNS)
11483 && ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
11485 * If the LUN is invalid, pretend that it doesn't exist.
11486 * It will go away as soon as all pending I/O has been
11489 mtx_lock(&lun->lun_lock);
11490 if (lun->flags & CTL_LUN_DISABLED) {
11491 mtx_unlock(&lun->lun_lock);
11493 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11494 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11496 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
11497 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
11499 if (lun->be_lun->lun_type == T_PROCESSOR) {
11500 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV;
11504 * Every I/O goes into the OOA queue for a
11505 * particular LUN, and stays there until completion.
11507 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr,
11511 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11512 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11515 /* Get command entry and return error if it is unsuppotyed. */
11516 entry = ctl_validate_command(ctsio);
11517 if (entry == NULL) {
11519 mtx_unlock(&lun->lun_lock);
11523 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11524 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11527 * Check to see whether we can send this command to LUNs that don't
11528 * exist. This should pretty much only be the case for inquiry
11529 * and request sense. Further checks, below, really require having
11530 * a LUN, so we can't really check the command anymore. Just put
11531 * it on the rtr queue.
11534 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) {
11535 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11536 ctl_enqueue_rtr((union ctl_io *)ctsio);
11540 ctl_set_unsupported_lun(ctsio);
11541 ctl_done((union ctl_io *)ctsio);
11542 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11546 * Make sure we support this particular command on this LUN.
11547 * e.g., we don't support writes to the control LUN.
11549 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11550 mtx_unlock(&lun->lun_lock);
11551 ctl_set_invalid_opcode(ctsio);
11552 ctl_done((union ctl_io *)ctsio);
11557 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11561 * If we've got a request sense, it'll clear the contingent
11562 * allegiance condition. Otherwise, if we have a CA condition for
11563 * this initiator, clear it, because it sent down a command other
11564 * than request sense.
11566 if ((ctsio->cdb[0] != REQUEST_SENSE)
11567 && (ctl_is_set(lun->have_ca, initidx)))
11568 ctl_clear_mask(lun->have_ca, initidx);
11572 * If the command has this flag set, it handles its own unit
11573 * attention reporting, we shouldn't do anything. Otherwise we
11574 * check for any pending unit attentions, and send them back to the
11575 * initiator. We only do this when a command initially comes in,
11576 * not when we pull it off the blocked queue.
11578 * According to SAM-3, section 5.3.2, the order that things get
11579 * presented back to the host is basically unit attentions caused
11580 * by some sort of reset event, busy status, reservation conflicts
11581 * or task set full, and finally any other status.
11583 * One issue here is that some of the unit attentions we report
11584 * don't fall into the "reset" category (e.g. "reported luns data
11585 * has changed"). So reporting it here, before the reservation
11586 * check, may be technically wrong. I guess the only thing to do
11587 * would be to check for and report the reset events here, and then
11588 * check for the other unit attention types after we check for a
11589 * reservation conflict.
11591 * XXX KDM need to fix this
11593 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11594 ctl_ua_type ua_type;
11595 scsi_sense_data_type sense_format;
11597 if (lun->flags & CTL_LUN_SENSE_DESC)
11598 sense_format = SSD_TYPE_DESC;
11600 sense_format = SSD_TYPE_FIXED;
11602 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11604 if (ua_type != CTL_UA_NONE) {
11605 mtx_unlock(&lun->lun_lock);
11606 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11607 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11608 ctsio->sense_len = SSD_FULL_SIZE;
11609 ctl_done((union ctl_io *)ctsio);
11615 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11616 mtx_unlock(&lun->lun_lock);
11617 ctl_done((union ctl_io *)ctsio);
11622 * XXX CHD this is where we want to send IO to other side if
11623 * this LUN is secondary on this SC. We will need to make a copy
11624 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11625 * the copy we send as FROM_OTHER.
11626 * We also need to stuff the address of the original IO so we can
11627 * find it easily. Something similar will need be done on the other
11628 * side so when we are done we can find the copy.
11630 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11631 union ctl_ha_msg msg_info;
11634 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11636 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11637 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11639 printf("1. ctsio %p\n", ctsio);
11641 msg_info.hdr.serializing_sc = NULL;
11642 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11643 msg_info.scsi.tag_num = ctsio->tag_num;
11644 msg_info.scsi.tag_type = ctsio->tag_type;
11645 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11647 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11649 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11650 (void *)&msg_info, sizeof(msg_info), 0)) >
11651 CTL_HA_STATUS_SUCCESS) {
11652 printf("CTL:precheck, ctl_ha_msg_send returned %d\n",
11654 printf("CTL:opcode is %x\n", ctsio->cdb[0]);
11657 printf("CTL:Precheck sent msg, opcode is %x\n",opcode);
11662 * XXX KDM this I/O is off the incoming queue, but hasn't
11663 * been inserted on any other queue. We may need to come
11664 * up with a holding queue while we wait for serialization
11665 * so that we have an idea of what we're waiting for from
11668 mtx_unlock(&lun->lun_lock);
11672 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11673 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11674 ctl_ooaq, ooa_links))) {
11675 case CTL_ACTION_BLOCK:
11676 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11677 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11679 mtx_unlock(&lun->lun_lock);
11681 case CTL_ACTION_PASS:
11682 case CTL_ACTION_SKIP:
11683 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11684 mtx_unlock(&lun->lun_lock);
11685 ctl_enqueue_rtr((union ctl_io *)ctsio);
11687 case CTL_ACTION_OVERLAP:
11688 mtx_unlock(&lun->lun_lock);
11689 ctl_set_overlapped_cmd(ctsio);
11690 ctl_done((union ctl_io *)ctsio);
11692 case CTL_ACTION_OVERLAP_TAG:
11693 mtx_unlock(&lun->lun_lock);
11694 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11695 ctl_done((union ctl_io *)ctsio);
11697 case CTL_ACTION_ERROR:
11699 mtx_unlock(&lun->lun_lock);
11700 ctl_set_internal_failure(ctsio,
11702 /*retry_count*/ 0);
11703 ctl_done((union ctl_io *)ctsio);
11709 const struct ctl_cmd_entry *
11710 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11712 const struct ctl_cmd_entry *entry;
11713 int service_action;
11715 entry = &ctl_cmd_table[ctsio->cdb[0]];
11717 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11718 if (entry->flags & CTL_CMD_FLAG_SA5) {
11719 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11720 entry = &((const struct ctl_cmd_entry *)
11721 entry->execute)[service_action];
11726 const struct ctl_cmd_entry *
11727 ctl_validate_command(struct ctl_scsiio *ctsio)
11729 const struct ctl_cmd_entry *entry;
11733 entry = ctl_get_cmd_entry(ctsio, &sa);
11734 if (entry->execute == NULL) {
11736 ctl_set_invalid_field(ctsio,
11743 ctl_set_invalid_opcode(ctsio);
11744 ctl_done((union ctl_io *)ctsio);
11747 KASSERT(entry->length > 0,
11748 ("Not defined length for command 0x%02x/0x%02x",
11749 ctsio->cdb[0], ctsio->cdb[1]));
11750 for (i = 1; i < entry->length; i++) {
11751 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11754 ctl_set_invalid_field(ctsio,
11759 /*bit*/ fls(diff) - 1);
11760 ctl_done((union ctl_io *)ctsio);
11767 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11770 switch (lun_type) {
11772 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) &&
11773 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11777 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) &&
11778 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11788 ctl_scsiio(struct ctl_scsiio *ctsio)
11791 const struct ctl_cmd_entry *entry;
11793 retval = CTL_RETVAL_COMPLETE;
11795 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11797 entry = ctl_get_cmd_entry(ctsio, NULL);
11800 * If this I/O has been aborted, just send it straight to
11801 * ctl_done() without executing it.
11803 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11804 ctl_done((union ctl_io *)ctsio);
11809 * All the checks should have been handled by ctl_scsiio_precheck().
11810 * We should be clear now to just execute the I/O.
11812 retval = entry->execute(ctsio);
11819 * Since we only implement one target right now, a bus reset simply resets
11820 * our single target.
11823 ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io)
11825 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET));
11829 ctl_target_reset(struct ctl_softc *softc, union ctl_io *io,
11830 ctl_ua_type ua_type)
11832 struct ctl_lun *lun;
11835 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11836 union ctl_ha_msg msg_info;
11838 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11839 msg_info.hdr.nexus = io->io_hdr.nexus;
11840 if (ua_type==CTL_UA_TARG_RESET)
11841 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11843 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11844 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11845 msg_info.hdr.original_sc = NULL;
11846 msg_info.hdr.serializing_sc = NULL;
11847 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11848 (void *)&msg_info, sizeof(msg_info), 0)) {
11853 mtx_lock(&softc->ctl_lock);
11854 STAILQ_FOREACH(lun, &softc->lun_list, links)
11855 retval += ctl_lun_reset(lun, io, ua_type);
11856 mtx_unlock(&softc->ctl_lock);
11862 * The LUN should always be set. The I/O is optional, and is used to
11863 * distinguish between I/Os sent by this initiator, and by other
11864 * initiators. We set unit attention for initiators other than this one.
11865 * SAM-3 is vague on this point. It does say that a unit attention should
11866 * be established for other initiators when a LUN is reset (see section
11867 * 5.7.3), but it doesn't specifically say that the unit attention should
11868 * be established for this particular initiator when a LUN is reset. Here
11869 * is the relevant text, from SAM-3 rev 8:
11871 * 5.7.2 When a SCSI initiator port aborts its own tasks
11873 * When a SCSI initiator port causes its own task(s) to be aborted, no
11874 * notification that the task(s) have been aborted shall be returned to
11875 * the SCSI initiator port other than the completion response for the
11876 * command or task management function action that caused the task(s) to
11877 * be aborted and notification(s) associated with related effects of the
11878 * action (e.g., a reset unit attention condition).
11880 * XXX KDM for now, we're setting unit attention for all initiators.
11883 ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11893 mtx_lock(&lun->lun_lock);
11895 * Run through the OOA queue and abort each I/O.
11898 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
11900 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11901 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11902 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11906 * This version sets unit attention for every
11909 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11910 ctl_est_ua_all(lun, initidx, ua_type);
11912 ctl_est_ua_all(lun, -1, ua_type);
11916 * A reset (any kind, really) clears reservations established with
11917 * RESERVE/RELEASE. It does not clear reservations established
11918 * with PERSISTENT RESERVE OUT, but we don't support that at the
11919 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
11920 * reservations made with the RESERVE/RELEASE commands, because
11921 * those commands are obsolete in SPC-3.
11923 lun->flags &= ~CTL_LUN_RESERVED;
11926 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11927 ctl_clear_mask(lun->have_ca, i);
11929 mtx_unlock(&lun->lun_lock);
11935 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11940 mtx_assert(&lun->lun_lock, MA_OWNED);
11943 * Run through the OOA queue and attempt to find the given I/O.
11944 * The target port, initiator ID, tag type and tag number have to
11945 * match the values that we got from the initiator. If we have an
11946 * untagged command to abort, simply abort the first untagged command
11947 * we come to. We only allow one untagged command at a time of course.
11949 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11950 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11952 if ((targ_port == UINT32_MAX ||
11953 targ_port == xio->io_hdr.nexus.targ_port) &&
11954 (init_id == UINT32_MAX ||
11955 init_id == xio->io_hdr.nexus.initid.id)) {
11956 if (targ_port != xio->io_hdr.nexus.targ_port ||
11957 init_id != xio->io_hdr.nexus.initid.id)
11958 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
11959 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11960 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11961 union ctl_ha_msg msg_info;
11963 msg_info.hdr.nexus = xio->io_hdr.nexus;
11964 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11965 msg_info.task.tag_num = xio->scsiio.tag_num;
11966 msg_info.task.tag_type = xio->scsiio.tag_type;
11967 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11968 msg_info.hdr.original_sc = NULL;
11969 msg_info.hdr.serializing_sc = NULL;
11970 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11971 (void *)&msg_info, sizeof(msg_info), 0);
11978 ctl_abort_task_set(union ctl_io *io)
11980 struct ctl_softc *softc = control_softc;
11981 struct ctl_lun *lun;
11987 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11988 mtx_lock(&softc->ctl_lock);
11989 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL))
11990 lun = softc->ctl_luns[targ_lun];
11992 mtx_unlock(&softc->ctl_lock);
11996 mtx_lock(&lun->lun_lock);
11997 mtx_unlock(&softc->ctl_lock);
11998 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
11999 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12000 io->io_hdr.nexus.initid.id,
12001 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12002 } else { /* CTL_TASK_CLEAR_TASK_SET */
12003 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
12004 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12006 mtx_unlock(&lun->lun_lock);
12011 ctl_i_t_nexus_reset(union ctl_io *io)
12013 struct ctl_softc *softc = control_softc;
12014 struct ctl_lun *lun;
12015 uint32_t initidx, residx;
12017 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12018 residx = ctl_get_resindex(&io->io_hdr.nexus);
12019 mtx_lock(&softc->ctl_lock);
12020 STAILQ_FOREACH(lun, &softc->lun_list, links) {
12021 mtx_lock(&lun->lun_lock);
12022 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12023 io->io_hdr.nexus.initid.id,
12024 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12026 ctl_clear_mask(lun->have_ca, initidx);
12028 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
12029 lun->flags &= ~CTL_LUN_RESERVED;
12030 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS);
12031 mtx_unlock(&lun->lun_lock);
12033 mtx_unlock(&softc->ctl_lock);
12038 ctl_abort_task(union ctl_io *io)
12041 struct ctl_lun *lun;
12042 struct ctl_softc *softc;
12045 char printbuf[128];
12050 softc = control_softc;
12056 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12057 mtx_lock(&softc->ctl_lock);
12058 if ((targ_lun < CTL_MAX_LUNS)
12059 && (softc->ctl_luns[targ_lun] != NULL))
12060 lun = softc->ctl_luns[targ_lun];
12062 mtx_unlock(&softc->ctl_lock);
12067 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
12068 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
12071 mtx_lock(&lun->lun_lock);
12072 mtx_unlock(&softc->ctl_lock);
12074 * Run through the OOA queue and attempt to find the given I/O.
12075 * The target port, initiator ID, tag type and tag number have to
12076 * match the values that we got from the initiator. If we have an
12077 * untagged command to abort, simply abort the first untagged command
12078 * we come to. We only allow one untagged command at a time of course.
12081 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
12083 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12084 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12086 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
12088 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
12089 lun->lun, xio->scsiio.tag_num,
12090 xio->scsiio.tag_type,
12091 (xio->io_hdr.blocked_links.tqe_prev
12092 == NULL) ? "" : " BLOCKED",
12093 (xio->io_hdr.flags &
12094 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
12095 (xio->io_hdr.flags &
12096 CTL_FLAG_ABORT) ? " ABORT" : "",
12097 (xio->io_hdr.flags &
12098 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
12099 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
12101 printf("%s\n", sbuf_data(&sb));
12104 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port)
12105 && (xio->io_hdr.nexus.initid.id ==
12106 io->io_hdr.nexus.initid.id)) {
12108 * If the abort says that the task is untagged, the
12109 * task in the queue must be untagged. Otherwise,
12110 * we just check to see whether the tag numbers
12111 * match. This is because the QLogic firmware
12112 * doesn't pass back the tag type in an abort
12116 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12117 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12118 || (xio->scsiio.tag_num == io->taskio.tag_num)) {
12121 * XXX KDM we've got problems with FC, because it
12122 * doesn't send down a tag type with aborts. So we
12123 * can only really go by the tag number...
12124 * This may cause problems with parallel SCSI.
12125 * Need to figure that out!!
12127 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12128 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12130 if ((io->io_hdr.flags &
12131 CTL_FLAG_FROM_OTHER_SC) == 0 &&
12132 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12133 union ctl_ha_msg msg_info;
12135 io->io_hdr.flags |=
12136 CTL_FLAG_SENT_2OTHER_SC;
12137 msg_info.hdr.nexus = io->io_hdr.nexus;
12138 msg_info.task.task_action =
12139 CTL_TASK_ABORT_TASK;
12140 msg_info.task.tag_num =
12141 io->taskio.tag_num;
12142 msg_info.task.tag_type =
12143 io->taskio.tag_type;
12144 msg_info.hdr.msg_type =
12145 CTL_MSG_MANAGE_TASKS;
12146 msg_info.hdr.original_sc = NULL;
12147 msg_info.hdr.serializing_sc = NULL;
12149 printf("Sent Abort to other side\n");
12151 if (CTL_HA_STATUS_SUCCESS !=
12152 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12154 sizeof(msg_info), 0)) {
12158 printf("ctl_abort_task: found I/O to abort\n");
12164 mtx_unlock(&lun->lun_lock);
12168 * This isn't really an error. It's entirely possible for
12169 * the abort and command completion to cross on the wire.
12170 * This is more of an informative/diagnostic error.
12173 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
12174 "%d:%d:%d:%d tag %d type %d\n",
12175 io->io_hdr.nexus.initid.id,
12176 io->io_hdr.nexus.targ_port,
12177 io->io_hdr.nexus.targ_target.id,
12178 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
12179 io->taskio.tag_type);
12186 ctl_run_task(union ctl_io *io)
12188 struct ctl_softc *softc = control_softc;
12190 const char *task_desc;
12192 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12194 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12195 ("ctl_run_task: Unextected io_type %d\n",
12196 io->io_hdr.io_type));
12198 task_desc = ctl_scsi_task_string(&io->taskio);
12199 if (task_desc != NULL) {
12201 csevent_log(CSC_CTL | CSC_SHELF_SW |
12203 csevent_LogType_Trace,
12204 csevent_Severity_Information,
12205 csevent_AlertLevel_Green,
12206 csevent_FRU_Firmware,
12207 csevent_FRU_Unknown,
12208 "CTL: received task: %s",task_desc);
12212 csevent_log(CSC_CTL | CSC_SHELF_SW |
12214 csevent_LogType_Trace,
12215 csevent_Severity_Information,
12216 csevent_AlertLevel_Green,
12217 csevent_FRU_Firmware,
12218 csevent_FRU_Unknown,
12219 "CTL: received unknown task "
12221 io->taskio.task_action,
12222 io->taskio.task_action);
12225 switch (io->taskio.task_action) {
12226 case CTL_TASK_ABORT_TASK:
12227 retval = ctl_abort_task(io);
12229 case CTL_TASK_ABORT_TASK_SET:
12230 case CTL_TASK_CLEAR_TASK_SET:
12231 retval = ctl_abort_task_set(io);
12233 case CTL_TASK_CLEAR_ACA:
12235 case CTL_TASK_I_T_NEXUS_RESET:
12236 retval = ctl_i_t_nexus_reset(io);
12238 case CTL_TASK_LUN_RESET: {
12239 struct ctl_lun *lun;
12242 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12243 mtx_lock(&softc->ctl_lock);
12244 if ((targ_lun < CTL_MAX_LUNS)
12245 && (softc->ctl_luns[targ_lun] != NULL))
12246 lun = softc->ctl_luns[targ_lun];
12248 mtx_unlock(&softc->ctl_lock);
12253 if (!(io->io_hdr.flags &
12254 CTL_FLAG_FROM_OTHER_SC)) {
12255 union ctl_ha_msg msg_info;
12257 io->io_hdr.flags |=
12258 CTL_FLAG_SENT_2OTHER_SC;
12259 msg_info.hdr.msg_type =
12260 CTL_MSG_MANAGE_TASKS;
12261 msg_info.hdr.nexus = io->io_hdr.nexus;
12262 msg_info.task.task_action =
12263 CTL_TASK_LUN_RESET;
12264 msg_info.hdr.original_sc = NULL;
12265 msg_info.hdr.serializing_sc = NULL;
12266 if (CTL_HA_STATUS_SUCCESS !=
12267 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12269 sizeof(msg_info), 0)) {
12273 retval = ctl_lun_reset(lun, io,
12275 mtx_unlock(&softc->ctl_lock);
12278 case CTL_TASK_TARGET_RESET:
12279 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET);
12281 case CTL_TASK_BUS_RESET:
12282 retval = ctl_bus_reset(softc, io);
12284 case CTL_TASK_PORT_LOGIN:
12286 case CTL_TASK_PORT_LOGOUT:
12289 printf("ctl_run_task: got unknown task management event %d\n",
12290 io->taskio.task_action);
12294 io->io_hdr.status = CTL_SUCCESS;
12296 io->io_hdr.status = CTL_ERROR;
12301 * For HA operation. Handle commands that come in from the other
12305 ctl_handle_isc(union ctl_io *io)
12308 struct ctl_lun *lun;
12309 struct ctl_softc *softc;
12312 softc = control_softc;
12314 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12315 lun = softc->ctl_luns[targ_lun];
12317 switch (io->io_hdr.msg_type) {
12318 case CTL_MSG_SERIALIZE:
12319 free_io = ctl_serialize_other_sc_cmd(&io->scsiio);
12321 case CTL_MSG_R2R: {
12322 const struct ctl_cmd_entry *entry;
12325 * This is only used in SER_ONLY mode.
12328 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12329 mtx_lock(&lun->lun_lock);
12330 if (ctl_scsiio_lun_check(lun,
12331 entry, (struct ctl_scsiio *)io) != 0) {
12332 mtx_unlock(&lun->lun_lock);
12336 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12337 mtx_unlock(&lun->lun_lock);
12338 ctl_enqueue_rtr(io);
12341 case CTL_MSG_FINISH_IO:
12342 if (softc->ha_mode == CTL_HA_MODE_XFER) {
12347 mtx_lock(&lun->lun_lock);
12348 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
12350 ctl_check_blocked(lun);
12351 mtx_unlock(&lun->lun_lock);
12354 case CTL_MSG_PERS_ACTION:
12355 ctl_hndl_per_res_out_on_other_sc(
12356 (union ctl_ha_msg *)&io->presio.pr_msg);
12359 case CTL_MSG_BAD_JUJU:
12363 case CTL_MSG_DATAMOVE:
12364 /* Only used in XFER mode */
12366 ctl_datamove_remote(io);
12368 case CTL_MSG_DATAMOVE_DONE:
12369 /* Only used in XFER mode */
12371 io->scsiio.be_move_done(io);
12375 printf("%s: Invalid message type %d\n",
12376 __func__, io->io_hdr.msg_type);
12386 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12387 * there is no match.
12389 static ctl_lun_error_pattern
12390 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12392 const struct ctl_cmd_entry *entry;
12393 ctl_lun_error_pattern filtered_pattern, pattern;
12395 pattern = desc->error_pattern;
12398 * XXX KDM we need more data passed into this function to match a
12399 * custom pattern, and we actually need to implement custom pattern
12402 if (pattern & CTL_LUN_PAT_CMD)
12403 return (CTL_LUN_PAT_CMD);
12405 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12406 return (CTL_LUN_PAT_ANY);
12408 entry = ctl_get_cmd_entry(ctsio, NULL);
12410 filtered_pattern = entry->pattern & pattern;
12413 * If the user requested specific flags in the pattern (e.g.
12414 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12417 * If the user did not specify any flags, it doesn't matter whether
12418 * or not the command supports the flags.
12420 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12421 (pattern & ~CTL_LUN_PAT_MASK))
12422 return (CTL_LUN_PAT_NONE);
12425 * If the user asked for a range check, see if the requested LBA
12426 * range overlaps with this command's LBA range.
12428 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12434 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12436 return (CTL_LUN_PAT_NONE);
12438 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12439 desc->lba_range.len, FALSE);
12441 * A "pass" means that the LBA ranges don't overlap, so
12442 * this doesn't match the user's range criteria.
12444 if (action == CTL_ACTION_PASS)
12445 return (CTL_LUN_PAT_NONE);
12448 return (filtered_pattern);
12452 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12454 struct ctl_error_desc *desc, *desc2;
12456 mtx_assert(&lun->lun_lock, MA_OWNED);
12458 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12459 ctl_lun_error_pattern pattern;
12461 * Check to see whether this particular command matches
12462 * the pattern in the descriptor.
12464 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12465 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12468 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12469 case CTL_LUN_INJ_ABORTED:
12470 ctl_set_aborted(&io->scsiio);
12472 case CTL_LUN_INJ_MEDIUM_ERR:
12473 ctl_set_medium_error(&io->scsiio);
12475 case CTL_LUN_INJ_UA:
12476 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12478 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12480 case CTL_LUN_INJ_CUSTOM:
12482 * We're assuming the user knows what he is doing.
12483 * Just copy the sense information without doing
12486 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12487 MIN(sizeof(desc->custom_sense),
12488 sizeof(io->scsiio.sense_data)));
12489 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12490 io->scsiio.sense_len = SSD_FULL_SIZE;
12491 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12493 case CTL_LUN_INJ_NONE:
12496 * If this is an error injection type we don't know
12497 * about, clear the continuous flag (if it is set)
12498 * so it will get deleted below.
12500 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12504 * By default, each error injection action is a one-shot
12506 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12509 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12515 #ifdef CTL_IO_DELAY
12517 ctl_datamove_timer_wakeup(void *arg)
12521 io = (union ctl_io *)arg;
12525 #endif /* CTL_IO_DELAY */
12528 ctl_datamove(union ctl_io *io)
12530 void (*fe_datamove)(union ctl_io *io);
12532 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12534 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12537 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12542 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12543 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12545 sbuf_cat(&sb, path_str);
12546 switch (io->io_hdr.io_type) {
12548 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12549 sbuf_printf(&sb, "\n");
12550 sbuf_cat(&sb, path_str);
12551 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12552 io->scsiio.tag_num, io->scsiio.tag_type);
12555 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12556 "Tag Type: %d\n", io->taskio.task_action,
12557 io->taskio.tag_num, io->taskio.tag_type);
12560 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12561 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12564 sbuf_cat(&sb, path_str);
12565 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12566 (intmax_t)time_uptime - io->io_hdr.start_time);
12568 printf("%s", sbuf_data(&sb));
12570 #endif /* CTL_TIME_IO */
12572 #ifdef CTL_IO_DELAY
12573 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12574 struct ctl_lun *lun;
12576 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12578 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12580 struct ctl_lun *lun;
12582 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12584 && (lun->delay_info.datamove_delay > 0)) {
12585 struct callout *callout;
12587 callout = (struct callout *)&io->io_hdr.timer_bytes;
12588 callout_init(callout, /*mpsafe*/ 1);
12589 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12590 callout_reset(callout,
12591 lun->delay_info.datamove_delay * hz,
12592 ctl_datamove_timer_wakeup, io);
12593 if (lun->delay_info.datamove_type ==
12594 CTL_DELAY_TYPE_ONESHOT)
12595 lun->delay_info.datamove_delay = 0;
12602 * This command has been aborted. Set the port status, so we fail
12605 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12606 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n",
12607 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id,
12608 io->io_hdr.nexus.targ_port,
12609 (uintmax_t)io->io_hdr.nexus.targ_target.id,
12610 io->io_hdr.nexus.targ_lun);
12611 io->io_hdr.port_status = 31337;
12613 * Note that the backend, in this case, will get the
12614 * callback in its context. In other cases it may get
12615 * called in the frontend's interrupt thread context.
12617 io->scsiio.be_move_done(io);
12621 /* Don't confuse frontend with zero length data move. */
12622 if (io->scsiio.kern_data_len == 0) {
12623 io->scsiio.be_move_done(io);
12628 * If we're in XFER mode and this I/O is from the other shelf
12629 * controller, we need to send the DMA to the other side to
12630 * actually transfer the data to/from the host. In serialize only
12631 * mode the transfer happens below CTL and ctl_datamove() is only
12632 * called on the machine that originally received the I/O.
12634 if ((control_softc->ha_mode == CTL_HA_MODE_XFER)
12635 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12636 union ctl_ha_msg msg;
12637 uint32_t sg_entries_sent;
12641 memset(&msg, 0, sizeof(msg));
12642 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
12643 msg.hdr.original_sc = io->io_hdr.original_sc;
12644 msg.hdr.serializing_sc = io;
12645 msg.hdr.nexus = io->io_hdr.nexus;
12646 msg.dt.flags = io->io_hdr.flags;
12648 * We convert everything into a S/G list here. We can't
12649 * pass by reference, only by value between controllers.
12650 * So we can't pass a pointer to the S/G list, only as many
12651 * S/G entries as we can fit in here. If it's possible for
12652 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
12653 * then we need to break this up into multiple transfers.
12655 if (io->scsiio.kern_sg_entries == 0) {
12656 msg.dt.kern_sg_entries = 1;
12658 * If this is in cached memory, flush the cache
12659 * before we send the DMA request to the other
12660 * controller. We want to do this in either the
12661 * read or the write case. The read case is
12662 * straightforward. In the write case, we want to
12663 * make sure nothing is in the local cache that
12664 * could overwrite the DMAed data.
12666 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12668 * XXX KDM use bus_dmamap_sync() here.
12673 * Convert to a physical address if this is a
12676 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
12677 msg.dt.sg_list[0].addr =
12678 io->scsiio.kern_data_ptr;
12681 * XXX KDM use busdma here!
12684 msg.dt.sg_list[0].addr = (void *)
12685 vtophys(io->scsiio.kern_data_ptr);
12689 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
12692 struct ctl_sg_entry *sgl;
12695 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
12696 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
12697 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12699 * XXX KDM use bus_dmamap_sync() here.
12704 msg.dt.kern_data_len = io->scsiio.kern_data_len;
12705 msg.dt.kern_total_len = io->scsiio.kern_total_len;
12706 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
12707 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
12708 msg.dt.sg_sequence = 0;
12711 * Loop until we've sent all of the S/G entries. On the
12712 * other end, we'll recompose these S/G entries into one
12713 * contiguous list before passing it to the
12715 for (sg_entries_sent = 0; sg_entries_sent <
12716 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) {
12717 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list)/
12718 sizeof(msg.dt.sg_list[0])),
12719 msg.dt.kern_sg_entries - sg_entries_sent);
12721 if (do_sg_copy != 0) {
12722 struct ctl_sg_entry *sgl;
12725 sgl = (struct ctl_sg_entry *)
12726 io->scsiio.kern_data_ptr;
12728 * If this is in cached memory, flush the cache
12729 * before we send the DMA request to the other
12730 * controller. We want to do this in either
12731 * the * read or the write case. The read
12732 * case is straightforward. In the write
12733 * case, we want to make sure nothing is
12734 * in the local cache that could overwrite
12738 for (i = sg_entries_sent, j = 0;
12739 i < msg.dt.cur_sg_entries; i++, j++) {
12740 if ((io->io_hdr.flags &
12741 CTL_FLAG_NO_DATASYNC) == 0) {
12743 * XXX KDM use bus_dmamap_sync()
12746 if ((io->io_hdr.flags &
12747 CTL_FLAG_BUS_ADDR) == 0) {
12749 * XXX KDM use busdma.
12752 msg.dt.sg_list[j].addr =(void *)
12753 vtophys(sgl[i].addr);
12756 msg.dt.sg_list[j].addr =
12759 msg.dt.sg_list[j].len = sgl[i].len;
12763 sg_entries_sent += msg.dt.cur_sg_entries;
12764 if (sg_entries_sent >= msg.dt.kern_sg_entries)
12765 msg.dt.sg_last = 1;
12767 msg.dt.sg_last = 0;
12770 * XXX KDM drop and reacquire the lock here?
12772 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12773 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
12775 * XXX do something here.
12779 msg.dt.sent_sg_entries = sg_entries_sent;
12781 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12782 if (io->io_hdr.flags & CTL_FLAG_FAILOVER)
12783 ctl_failover_io(io, /*have_lock*/ 0);
12788 * Lookup the fe_datamove() function for this particular
12792 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12799 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12801 union ctl_ha_msg msg;
12804 memset(&msg, 0, sizeof(msg));
12806 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12807 msg.hdr.original_sc = io;
12808 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12809 msg.hdr.nexus = io->io_hdr.nexus;
12810 msg.hdr.status = io->io_hdr.status;
12811 msg.scsi.tag_num = io->scsiio.tag_num;
12812 msg.scsi.tag_type = io->scsiio.tag_type;
12813 msg.scsi.scsi_status = io->scsiio.scsi_status;
12814 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12815 sizeof(io->scsiio.sense_data));
12816 msg.scsi.sense_len = io->scsiio.sense_len;
12817 msg.scsi.sense_residual = io->scsiio.sense_residual;
12818 msg.scsi.fetd_status = io->io_hdr.port_status;
12819 msg.scsi.residual = io->scsiio.residual;
12820 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12822 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12823 ctl_failover_io(io, /*have_lock*/ have_lock);
12827 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0);
12828 if (isc_status > CTL_HA_STATUS_SUCCESS) {
12829 /* XXX do something if this fails */
12835 * The DMA to the remote side is done, now we need to tell the other side
12836 * we're done so it can continue with its data movement.
12839 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12845 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12846 printf("%s: ISC DMA write failed with error %d", __func__,
12848 ctl_set_internal_failure(&io->scsiio,
12850 /*retry_count*/ rq->ret);
12853 ctl_dt_req_free(rq);
12856 * In this case, we had to malloc the memory locally. Free it.
12858 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12860 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12861 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12864 * The data is in local and remote memory, so now we need to send
12865 * status (good or back) back to the other side.
12867 ctl_send_datamove_done(io, /*have_lock*/ 0);
12871 * We've moved the data from the host/controller into local memory. Now we
12872 * need to push it over to the remote controller's memory.
12875 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12881 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12882 ctl_datamove_remote_write_cb);
12888 ctl_datamove_remote_write(union ctl_io *io)
12891 void (*fe_datamove)(union ctl_io *io);
12894 * - Get the data from the host/HBA into local memory.
12895 * - DMA memory from the local controller to the remote controller.
12896 * - Send status back to the remote controller.
12899 retval = ctl_datamove_remote_sgl_setup(io);
12903 /* Switch the pointer over so the FETD knows what to do */
12904 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12907 * Use a custom move done callback, since we need to send completion
12908 * back to the other controller, not to the backend on this side.
12910 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12912 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12921 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12930 * In this case, we had to malloc the memory locally. Free it.
12932 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12934 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12935 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12939 scsi_path_string(io, path_str, sizeof(path_str));
12940 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12941 sbuf_cat(&sb, path_str);
12942 scsi_command_string(&io->scsiio, NULL, &sb);
12943 sbuf_printf(&sb, "\n");
12944 sbuf_cat(&sb, path_str);
12945 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12946 io->scsiio.tag_num, io->scsiio.tag_type);
12947 sbuf_cat(&sb, path_str);
12948 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12949 io->io_hdr.flags, io->io_hdr.status);
12951 printk("%s", sbuf_data(&sb));
12956 * The read is done, now we need to send status (good or bad) back
12957 * to the other side.
12959 ctl_send_datamove_done(io, /*have_lock*/ 0);
12965 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12968 void (*fe_datamove)(union ctl_io *io);
12972 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12973 printf("%s: ISC DMA read failed with error %d", __func__,
12975 ctl_set_internal_failure(&io->scsiio,
12977 /*retry_count*/ rq->ret);
12980 ctl_dt_req_free(rq);
12982 /* Switch the pointer over so the FETD knows what to do */
12983 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12986 * Use a custom move done callback, since we need to send completion
12987 * back to the other controller, not to the backend on this side.
12989 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12991 /* XXX KDM add checks like the ones in ctl_datamove? */
12993 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12999 ctl_datamove_remote_sgl_setup(union ctl_io *io)
13001 struct ctl_sg_entry *local_sglist, *remote_sglist;
13002 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist;
13003 struct ctl_softc *softc;
13008 softc = control_softc;
13010 local_sglist = io->io_hdr.local_sglist;
13011 local_dma_sglist = io->io_hdr.local_dma_sglist;
13012 remote_sglist = io->io_hdr.remote_sglist;
13013 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
13015 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) {
13016 for (i = 0; i < io->scsiio.kern_sg_entries; i++) {
13017 local_sglist[i].len = remote_sglist[i].len;
13020 * XXX Detect the situation where the RS-level I/O
13021 * redirector on the other side has already read the
13022 * data off of the AOR RS on this side, and
13023 * transferred it to remote (mirror) memory on the
13024 * other side. Since we already have the data in
13025 * memory here, we just need to use it.
13027 * XXX KDM this can probably be removed once we
13028 * get the cache device code in and take the
13029 * current AOR implementation out.
13032 if ((remote_sglist[i].addr >=
13033 (void *)vtophys(softc->mirr->addr))
13034 && (remote_sglist[i].addr <
13035 ((void *)vtophys(softc->mirr->addr) +
13036 CacheMirrorOffset))) {
13037 local_sglist[i].addr = remote_sglist[i].addr -
13039 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13041 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE;
13043 local_sglist[i].addr = remote_sglist[i].addr +
13048 printf("%s: local %p, remote %p, len %d\n",
13049 __func__, local_sglist[i].addr,
13050 remote_sglist[i].addr, local_sglist[i].len);
13054 uint32_t len_to_go;
13057 * In this case, we don't have automatically allocated
13058 * memory for this I/O on this controller. This typically
13059 * happens with internal CTL I/O -- e.g. inquiry, mode
13060 * sense, etc. Anything coming from RAIDCore will have
13061 * a mirror area available.
13063 len_to_go = io->scsiio.kern_data_len;
13066 * Clear the no datasync flag, we have to use malloced
13069 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC;
13072 * The difficult thing here is that the size of the various
13073 * S/G segments may be different than the size from the
13074 * remote controller. That'll make it harder when DMAing
13075 * the data back to the other side.
13077 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) /
13078 sizeof(io->io_hdr.remote_sglist[0])) &&
13079 (len_to_go > 0); i++) {
13080 local_sglist[i].len = MIN(len_to_go, 131072);
13081 CTL_SIZE_8B(local_dma_sglist[i].len,
13082 local_sglist[i].len);
13083 local_sglist[i].addr =
13084 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK);
13086 local_dma_sglist[i].addr = local_sglist[i].addr;
13088 if (local_sglist[i].addr == NULL) {
13091 printf("malloc failed for %zd bytes!",
13092 local_dma_sglist[i].len);
13093 for (j = 0; j < i; j++) {
13094 free(local_sglist[j].addr, M_CTL);
13096 ctl_set_internal_failure(&io->scsiio,
13098 /*retry_count*/ 4857);
13100 goto bailout_error;
13103 /* XXX KDM do we need a sync here? */
13105 len_to_go -= local_sglist[i].len;
13108 * Reset the number of S/G entries accordingly. The
13109 * original number of S/G entries is available in
13112 io->scsiio.kern_sg_entries = i;
13115 printf("%s: kern_sg_entries = %d\n", __func__,
13116 io->scsiio.kern_sg_entries);
13117 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13118 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i,
13119 local_sglist[i].addr, local_sglist[i].len,
13120 local_dma_sglist[i].len);
13129 ctl_send_datamove_done(io, /*have_lock*/ 0);
13135 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
13136 ctl_ha_dt_cb callback)
13138 struct ctl_ha_dt_req *rq;
13139 struct ctl_sg_entry *remote_sglist, *local_sglist;
13140 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist;
13141 uint32_t local_used, remote_used, total_used;
13147 rq = ctl_dt_req_alloc();
13150 * If we failed to allocate the request, and if the DMA didn't fail
13151 * anyway, set busy status. This is just a resource allocation
13155 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE))
13156 ctl_set_busy(&io->scsiio);
13158 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
13161 ctl_dt_req_free(rq);
13164 * The data move failed. We need to return status back
13165 * to the other controller. No point in trying to DMA
13166 * data to the remote controller.
13169 ctl_send_datamove_done(io, /*have_lock*/ 0);
13176 local_sglist = io->io_hdr.local_sglist;
13177 local_dma_sglist = io->io_hdr.local_dma_sglist;
13178 remote_sglist = io->io_hdr.remote_sglist;
13179 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
13184 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) {
13185 rq->ret = CTL_HA_STATUS_SUCCESS;
13192 * Pull/push the data over the wire from/to the other controller.
13193 * This takes into account the possibility that the local and
13194 * remote sglists may not be identical in terms of the size of
13195 * the elements and the number of elements.
13197 * One fundamental assumption here is that the length allocated for
13198 * both the local and remote sglists is identical. Otherwise, we've
13199 * essentially got a coding error of some sort.
13201 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
13203 uint32_t cur_len, dma_length;
13206 rq->id = CTL_HA_DATA_CTL;
13207 rq->command = command;
13211 * Both pointers should be aligned. But it is possible
13212 * that the allocation length is not. They should both
13213 * also have enough slack left over at the end, though,
13214 * to round up to the next 8 byte boundary.
13216 cur_len = MIN(local_sglist[i].len - local_used,
13217 remote_sglist[j].len - remote_used);
13220 * In this case, we have a size issue and need to decrease
13221 * the size, except in the case where we actually have less
13222 * than 8 bytes left. In that case, we need to increase
13223 * the DMA length to get the last bit.
13225 if ((cur_len & 0x7) != 0) {
13226 if (cur_len > 0x7) {
13227 cur_len = cur_len - (cur_len & 0x7);
13228 dma_length = cur_len;
13230 CTL_SIZE_8B(dma_length, cur_len);
13234 dma_length = cur_len;
13237 * If we had to allocate memory for this I/O, instead of using
13238 * the non-cached mirror memory, we'll need to flush the cache
13239 * before trying to DMA to the other controller.
13241 * We could end up doing this multiple times for the same
13242 * segment if we have a larger local segment than remote
13243 * segment. That shouldn't be an issue.
13245 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
13247 * XXX KDM use bus_dmamap_sync() here.
13251 rq->size = dma_length;
13253 tmp_ptr = (uint8_t *)local_sglist[i].addr;
13254 tmp_ptr += local_used;
13256 /* Use physical addresses when talking to ISC hardware */
13257 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
13258 /* XXX KDM use busdma */
13260 rq->local = vtophys(tmp_ptr);
13263 rq->local = tmp_ptr;
13265 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
13266 tmp_ptr += remote_used;
13267 rq->remote = tmp_ptr;
13269 rq->callback = NULL;
13271 local_used += cur_len;
13272 if (local_used >= local_sglist[i].len) {
13277 remote_used += cur_len;
13278 if (remote_used >= remote_sglist[j].len) {
13282 total_used += cur_len;
13284 if (total_used >= io->scsiio.kern_data_len)
13285 rq->callback = callback;
13287 if ((rq->size & 0x7) != 0) {
13288 printf("%s: warning: size %d is not on 8b boundary\n",
13289 __func__, rq->size);
13291 if (((uintptr_t)rq->local & 0x7) != 0) {
13292 printf("%s: warning: local %p not on 8b boundary\n",
13293 __func__, rq->local);
13295 if (((uintptr_t)rq->remote & 0x7) != 0) {
13296 printf("%s: warning: remote %p not on 8b boundary\n",
13297 __func__, rq->local);
13300 printf("%s: %s: local %#x remote %#x size %d\n", __func__,
13301 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
13302 rq->local, rq->remote, rq->size);
13305 isc_ret = ctl_dt_single(rq);
13306 if (isc_ret == CTL_HA_STATUS_WAIT)
13309 if (isc_ret == CTL_HA_STATUS_DISCONNECT) {
13310 rq->ret = CTL_HA_STATUS_SUCCESS;
13324 ctl_datamove_remote_read(union ctl_io *io)
13330 * This will send an error to the other controller in the case of a
13333 retval = ctl_datamove_remote_sgl_setup(io);
13337 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
13338 ctl_datamove_remote_read_cb);
13340 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) {
13342 * Make sure we free memory if there was an error.. The
13343 * ctl_datamove_remote_xfer() function will send the
13344 * datamove done message, or call the callback with an
13345 * error if there is a problem.
13347 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13348 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13355 * Process a datamove request from the other controller. This is used for
13356 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
13357 * first. Once that is complete, the data gets DMAed into the remote
13358 * controller's memory. For reads, we DMA from the remote controller's
13359 * memory into our memory first, and then move it out to the FETD.
13362 ctl_datamove_remote(union ctl_io *io)
13364 struct ctl_softc *softc;
13366 softc = control_softc;
13368 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
13371 * Note that we look for an aborted I/O here, but don't do some of
13372 * the other checks that ctl_datamove() normally does.
13373 * We don't need to run the datamove delay code, since that should
13374 * have been done if need be on the other controller.
13376 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
13377 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__,
13378 io->scsiio.tag_num, io->io_hdr.nexus.initid.id,
13379 io->io_hdr.nexus.targ_port,
13380 io->io_hdr.nexus.targ_target.id,
13381 io->io_hdr.nexus.targ_lun);
13382 io->io_hdr.port_status = 31338;
13383 ctl_send_datamove_done(io, /*have_lock*/ 0);
13387 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) {
13388 ctl_datamove_remote_write(io);
13389 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){
13390 ctl_datamove_remote_read(io);
13392 union ctl_ha_msg msg;
13393 struct scsi_sense_data *sense;
13397 memset(&msg, 0, sizeof(msg));
13399 msg.hdr.msg_type = CTL_MSG_BAD_JUJU;
13400 msg.hdr.status = CTL_SCSI_ERROR;
13401 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
13403 retry_count = 4243;
13405 sense = &msg.scsi.sense_data;
13406 sks[0] = SSD_SCS_VALID;
13407 sks[1] = (retry_count >> 8) & 0xff;
13408 sks[2] = retry_count & 0xff;
13410 /* "Internal target failure" */
13411 scsi_set_sense_data(sense,
13412 /*sense_format*/ SSD_TYPE_NONE,
13413 /*current_error*/ 1,
13414 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
13417 /*type*/ SSD_ELEM_SKS,
13418 /*size*/ sizeof(sks),
13422 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
13423 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
13424 ctl_failover_io(io, /*have_lock*/ 1);
13428 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) >
13429 CTL_HA_STATUS_SUCCESS) {
13430 /* XXX KDM what to do if this fails? */
13438 ctl_process_done(union ctl_io *io)
13440 struct ctl_lun *lun;
13441 struct ctl_softc *softc = control_softc;
13442 void (*fe_done)(union ctl_io *io);
13443 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port);
13445 CTL_DEBUG_PRINT(("ctl_process_done\n"));
13447 fe_done = softc->ctl_ports[targ_port]->fe_done;
13450 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
13455 ctl_scsi_path_string(io, path_str, sizeof(path_str));
13456 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13458 sbuf_cat(&sb, path_str);
13459 switch (io->io_hdr.io_type) {
13461 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
13462 sbuf_printf(&sb, "\n");
13463 sbuf_cat(&sb, path_str);
13464 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13465 io->scsiio.tag_num, io->scsiio.tag_type);
13468 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
13469 "Tag Type: %d\n", io->taskio.task_action,
13470 io->taskio.tag_num, io->taskio.tag_type);
13473 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13474 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13477 sbuf_cat(&sb, path_str);
13478 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
13479 (intmax_t)time_uptime - io->io_hdr.start_time);
13481 printf("%s", sbuf_data(&sb));
13483 #endif /* CTL_TIME_IO */
13485 switch (io->io_hdr.io_type) {
13489 if (bootverbose || (ctl_debug & CTL_DEBUG_INFO))
13490 ctl_io_error_print(io, NULL);
13491 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
13495 return (CTL_RETVAL_COMPLETE);
13497 panic("ctl_process_done: invalid io type %d\n",
13498 io->io_hdr.io_type);
13499 break; /* NOTREACHED */
13502 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13504 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13505 io->io_hdr.nexus.targ_mapped_lun));
13509 mtx_lock(&lun->lun_lock);
13512 * Check to see if we have any errors to inject here. We only
13513 * inject errors for commands that don't already have errors set.
13515 if ((STAILQ_FIRST(&lun->error_list) != NULL) &&
13516 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
13517 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
13518 ctl_inject_error(lun, io);
13521 * XXX KDM how do we treat commands that aren't completed
13524 * XXX KDM should we also track I/O latency?
13526 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13527 io->io_hdr.io_type == CTL_IO_SCSI) {
13529 struct bintime cur_bt;
13533 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13535 type = CTL_STATS_READ;
13536 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13538 type = CTL_STATS_WRITE;
13540 type = CTL_STATS_NO_IO;
13542 lun->stats.ports[targ_port].bytes[type] +=
13543 io->scsiio.kern_total_len;
13544 lun->stats.ports[targ_port].operations[type]++;
13546 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
13547 &io->io_hdr.dma_bt);
13548 lun->stats.ports[targ_port].num_dmas[type] +=
13549 io->io_hdr.num_dmas;
13550 getbintime(&cur_bt);
13551 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
13552 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
13557 * Remove this from the OOA queue.
13559 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13562 * Run through the blocked queue on this LUN and see if anything
13563 * has become unblocked, now that this transaction is done.
13565 ctl_check_blocked(lun);
13568 * If the LUN has been invalidated, free it if there is nothing
13569 * left on its OOA queue.
13571 if ((lun->flags & CTL_LUN_INVALID)
13572 && TAILQ_EMPTY(&lun->ooa_queue)) {
13573 mtx_unlock(&lun->lun_lock);
13574 mtx_lock(&softc->ctl_lock);
13576 mtx_unlock(&softc->ctl_lock);
13578 mtx_unlock(&lun->lun_lock);
13583 * If this command has been aborted, make sure we set the status
13584 * properly. The FETD is responsible for freeing the I/O and doing
13585 * whatever it needs to do to clean up its state.
13587 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13588 ctl_set_task_aborted(&io->scsiio);
13591 * If enabled, print command error status.
13592 * We don't print UAs unless debugging was enabled explicitly.
13595 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
13597 if (!bootverbose && (ctl_debug & CTL_DEBUG_INFO) == 0)
13599 if ((ctl_debug & CTL_DEBUG_INFO) == 0 &&
13600 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) &&
13601 (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) {
13602 int error_code, sense_key, asc, ascq;
13604 scsi_extract_sense_len(&io->scsiio.sense_data,
13605 io->scsiio.sense_len, &error_code, &sense_key,
13606 &asc, &ascq, /*show_errors*/ 0);
13607 if (sense_key == SSD_KEY_UNIT_ATTENTION)
13611 ctl_io_error_print(io, NULL);
13615 * Tell the FETD or the other shelf controller we're done with this
13616 * command. Note that only SCSI commands get to this point. Task
13617 * management commands are completed above.
13619 * We only send status to the other controller if we're in XFER
13620 * mode. In SER_ONLY mode, the I/O is done on the controller that
13621 * received the I/O (from CTL's perspective), and so the status is
13624 * XXX KDM if we hold the lock here, we could cause a deadlock
13625 * if the frontend comes back in in this context to queue
13628 if ((softc->ha_mode == CTL_HA_MODE_XFER)
13629 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
13630 union ctl_ha_msg msg;
13632 memset(&msg, 0, sizeof(msg));
13633 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13634 msg.hdr.original_sc = io->io_hdr.original_sc;
13635 msg.hdr.nexus = io->io_hdr.nexus;
13636 msg.hdr.status = io->io_hdr.status;
13637 msg.scsi.scsi_status = io->scsiio.scsi_status;
13638 msg.scsi.tag_num = io->scsiio.tag_num;
13639 msg.scsi.tag_type = io->scsiio.tag_type;
13640 msg.scsi.sense_len = io->scsiio.sense_len;
13641 msg.scsi.sense_residual = io->scsiio.sense_residual;
13642 msg.scsi.residual = io->scsiio.residual;
13643 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
13644 sizeof(io->scsiio.sense_data));
13646 * We copy this whether or not this is an I/O-related
13647 * command. Otherwise, we'd have to go and check to see
13648 * whether it's a read/write command, and it really isn't
13651 memcpy(&msg.scsi.lbalen,
13652 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
13653 sizeof(msg.scsi.lbalen));
13655 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13656 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
13657 /* XXX do something here */
13664 return (CTL_RETVAL_COMPLETE);
13669 * Front end should call this if it doesn't do autosense. When the request
13670 * sense comes back in from the initiator, we'll dequeue this and send it.
13673 ctl_queue_sense(union ctl_io *io)
13675 struct ctl_lun *lun;
13676 struct ctl_softc *softc;
13677 uint32_t initidx, targ_lun;
13679 softc = control_softc;
13681 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13684 * LUN lookup will likely move to the ctl_work_thread() once we
13685 * have our new queueing infrastructure (that doesn't put things on
13686 * a per-LUN queue initially). That is so that we can handle
13687 * things like an INQUIRY to a LUN that we don't have enabled. We
13688 * can't deal with that right now.
13690 mtx_lock(&softc->ctl_lock);
13693 * If we don't have a LUN for this, just toss the sense
13696 targ_lun = io->io_hdr.nexus.targ_lun;
13697 targ_lun = ctl_map_lun(softc, io->io_hdr.nexus.targ_port, targ_lun);
13698 if ((targ_lun < CTL_MAX_LUNS)
13699 && (softc->ctl_luns[targ_lun] != NULL))
13700 lun = softc->ctl_luns[targ_lun];
13704 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13706 mtx_lock(&lun->lun_lock);
13708 * Already have CA set for this LUN...toss the sense information.
13710 if (ctl_is_set(lun->have_ca, initidx)) {
13711 mtx_unlock(&lun->lun_lock);
13715 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data,
13716 MIN(sizeof(lun->pending_sense[initidx]),
13717 sizeof(io->scsiio.sense_data)));
13718 ctl_set_mask(lun->have_ca, initidx);
13719 mtx_unlock(&lun->lun_lock);
13722 mtx_unlock(&softc->ctl_lock);
13726 return (CTL_RETVAL_COMPLETE);
13731 * Primary command inlet from frontend ports. All SCSI and task I/O
13732 * requests must go through this function.
13735 ctl_queue(union ctl_io *io)
13738 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13741 io->io_hdr.start_time = time_uptime;
13742 getbintime(&io->io_hdr.start_bt);
13743 #endif /* CTL_TIME_IO */
13745 /* Map FE-specific LUN ID into global one. */
13746 io->io_hdr.nexus.targ_mapped_lun =
13747 ctl_map_lun(control_softc, io->io_hdr.nexus.targ_port,
13748 io->io_hdr.nexus.targ_lun);
13750 switch (io->io_hdr.io_type) {
13753 if (ctl_debug & CTL_DEBUG_CDB)
13755 ctl_enqueue_incoming(io);
13758 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13762 return (CTL_RETVAL_COMPLETE);
13765 #ifdef CTL_IO_DELAY
13767 ctl_done_timer_wakeup(void *arg)
13771 io = (union ctl_io *)arg;
13774 #endif /* CTL_IO_DELAY */
13777 ctl_done(union ctl_io *io)
13781 * Enable this to catch duplicate completion issues.
13784 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13785 printf("%s: type %d msg %d cdb %x iptl: "
13786 "%d:%d:%d:%d tag 0x%04x "
13787 "flag %#x status %x\n",
13789 io->io_hdr.io_type,
13790 io->io_hdr.msg_type,
13792 io->io_hdr.nexus.initid.id,
13793 io->io_hdr.nexus.targ_port,
13794 io->io_hdr.nexus.targ_target.id,
13795 io->io_hdr.nexus.targ_lun,
13796 (io->io_hdr.io_type ==
13798 io->taskio.tag_num :
13799 io->scsiio.tag_num,
13801 io->io_hdr.status);
13803 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13807 * This is an internal copy of an I/O, and should not go through
13808 * the normal done processing logic.
13810 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13814 * We need to send a msg to the serializing shelf to finish the IO
13815 * as well. We don't send a finish message to the other shelf if
13816 * this is a task management command. Task management commands
13817 * aren't serialized in the OOA queue, but rather just executed on
13818 * both shelf controllers for commands that originated on that
13821 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)
13822 && (io->io_hdr.io_type != CTL_IO_TASK)) {
13823 union ctl_ha_msg msg_io;
13825 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO;
13826 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc;
13827 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io,
13828 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) {
13830 /* continue on to finish IO */
13832 #ifdef CTL_IO_DELAY
13833 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13834 struct ctl_lun *lun;
13836 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13838 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13840 struct ctl_lun *lun;
13842 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13845 && (lun->delay_info.done_delay > 0)) {
13846 struct callout *callout;
13848 callout = (struct callout *)&io->io_hdr.timer_bytes;
13849 callout_init(callout, /*mpsafe*/ 1);
13850 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13851 callout_reset(callout,
13852 lun->delay_info.done_delay * hz,
13853 ctl_done_timer_wakeup, io);
13854 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13855 lun->delay_info.done_delay = 0;
13859 #endif /* CTL_IO_DELAY */
13861 ctl_enqueue_done(io);
13865 ctl_isc(struct ctl_scsiio *ctsio)
13867 struct ctl_lun *lun;
13870 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13872 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0]));
13874 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n"));
13876 retval = lun->backend->data_submit((union ctl_io *)ctsio);
13883 ctl_work_thread(void *arg)
13885 struct ctl_thread *thr = (struct ctl_thread *)arg;
13886 struct ctl_softc *softc = thr->ctl_softc;
13890 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13896 * We handle the queues in this order:
13898 * - done queue (to free up resources, unblock other commands)
13902 * If those queues are empty, we break out of the loop and
13905 mtx_lock(&thr->queue_lock);
13906 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13908 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13909 mtx_unlock(&thr->queue_lock);
13910 ctl_handle_isc(io);
13913 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13915 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13916 /* clear any blocked commands, call fe_done */
13917 mtx_unlock(&thr->queue_lock);
13918 retval = ctl_process_done(io);
13921 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13923 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13924 mtx_unlock(&thr->queue_lock);
13925 if (io->io_hdr.io_type == CTL_IO_TASK)
13928 ctl_scsiio_precheck(softc, &io->scsiio);
13931 if (!ctl_pause_rtr) {
13932 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13934 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13935 mtx_unlock(&thr->queue_lock);
13936 retval = ctl_scsiio(&io->scsiio);
13937 if (retval != CTL_RETVAL_COMPLETE)
13938 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13943 /* Sleep until we have something to do. */
13944 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13949 ctl_lun_thread(void *arg)
13951 struct ctl_softc *softc = (struct ctl_softc *)arg;
13952 struct ctl_be_lun *be_lun;
13955 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13959 mtx_lock(&softc->ctl_lock);
13960 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13961 if (be_lun != NULL) {
13962 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13963 mtx_unlock(&softc->ctl_lock);
13964 ctl_create_lun(be_lun);
13968 /* Sleep until we have something to do. */
13969 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13970 PDROP | PRIBIO, "-", 0);
13975 ctl_thresh_thread(void *arg)
13977 struct ctl_softc *softc = (struct ctl_softc *)arg;
13978 struct ctl_lun *lun;
13979 struct ctl_be_lun *be_lun;
13980 struct scsi_da_rw_recovery_page *rwpage;
13981 struct ctl_logical_block_provisioning_page *page;
13983 uint64_t thres, val;
13986 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13989 mtx_lock(&softc->ctl_lock);
13990 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13991 be_lun = lun->be_lun;
13992 if ((lun->flags & CTL_LUN_DISABLED) ||
13993 (lun->flags & CTL_LUN_OFFLINE) ||
13994 lun->backend->lun_attr == NULL)
13996 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT];
13997 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0)
14000 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT];
14001 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
14002 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
14004 thres = scsi_4btoul(page->descr[i].count);
14005 thres <<= CTL_LBP_EXPONENT;
14006 switch (page->descr[i].resource) {
14008 attr = "blocksavail";
14011 attr = "blocksused";
14014 attr = "poolblocksavail";
14017 attr = "poolblocksused";
14022 mtx_unlock(&softc->ctl_lock); // XXX
14023 val = lun->backend->lun_attr(
14024 lun->be_lun->be_lun, attr);
14025 mtx_lock(&softc->ctl_lock);
14026 if (val == UINT64_MAX)
14028 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
14029 == SLBPPD_ARMING_INC)
14030 e |= (val >= thres);
14032 e |= (val <= thres);
14034 mtx_lock(&lun->lun_lock);
14036 if (lun->lasttpt == 0 ||
14037 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
14038 lun->lasttpt = time_uptime;
14039 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
14043 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
14045 mtx_unlock(&lun->lun_lock);
14047 mtx_unlock(&softc->ctl_lock);
14048 pause("-", CTL_LBP_PERIOD * hz);
14053 ctl_enqueue_incoming(union ctl_io *io)
14055 struct ctl_softc *softc = control_softc;
14056 struct ctl_thread *thr;
14059 idx = (io->io_hdr.nexus.targ_port * 127 +
14060 io->io_hdr.nexus.initid.id) % worker_threads;
14061 thr = &softc->threads[idx];
14062 mtx_lock(&thr->queue_lock);
14063 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
14064 mtx_unlock(&thr->queue_lock);
14069 ctl_enqueue_rtr(union ctl_io *io)
14071 struct ctl_softc *softc = control_softc;
14072 struct ctl_thread *thr;
14074 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14075 mtx_lock(&thr->queue_lock);
14076 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
14077 mtx_unlock(&thr->queue_lock);
14082 ctl_enqueue_done(union ctl_io *io)
14084 struct ctl_softc *softc = control_softc;
14085 struct ctl_thread *thr;
14087 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14088 mtx_lock(&thr->queue_lock);
14089 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
14090 mtx_unlock(&thr->queue_lock);
14095 ctl_enqueue_isc(union ctl_io *io)
14097 struct ctl_softc *softc = control_softc;
14098 struct ctl_thread *thr;
14100 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14101 mtx_lock(&thr->queue_lock);
14102 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
14103 mtx_unlock(&thr->queue_lock);
14107 /* Initialization and failover */
14110 ctl_init_isc_msg(void)
14112 printf("CTL: Still calling this thing\n");
14117 * Initializes component into configuration defined by bootMode
14119 * returns hasc_Status:
14121 * ERROR - fatal error
14123 static ctl_ha_comp_status
14124 ctl_isc_init(struct ctl_ha_component *c)
14126 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
14133 * Starts component in state requested. If component starts successfully,
14134 * it must set its own state to the requestrd state
14135 * When requested state is HASC_STATE_HA, the component may refine it
14136 * by adding _SLAVE or _MASTER flags.
14137 * Currently allowed state transitions are:
14138 * UNKNOWN->HA - initial startup
14139 * UNKNOWN->SINGLE - initial startup when no parter detected
14140 * HA->SINGLE - failover
14141 * returns ctl_ha_comp_status:
14142 * OK - component successfully started in requested state
14143 * FAILED - could not start the requested state, failover may
14145 * ERROR - fatal error detected, no future startup possible
14147 static ctl_ha_comp_status
14148 ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state)
14150 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
14152 printf("%s: go\n", __func__);
14154 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap)
14155 if (c->state == CTL_HA_STATE_UNKNOWN ) {
14156 control_softc->is_single = 0;
14157 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
14158 != CTL_HA_STATUS_SUCCESS) {
14159 printf("ctl_isc_start: ctl_ha_msg_create failed.\n");
14160 ret = CTL_HA_COMP_STATUS_ERROR;
14162 } else if (CTL_HA_STATE_IS_HA(c->state)
14163 && CTL_HA_STATE_IS_SINGLE(state)){
14164 // HA->SINGLE transition
14166 control_softc->is_single = 1;
14168 printf("ctl_isc_start:Invalid state transition %X->%X\n",
14170 ret = CTL_HA_COMP_STATUS_ERROR;
14172 if (CTL_HA_STATE_IS_SINGLE(state))
14173 control_softc->is_single = 1;
14181 * Quiesce component
14182 * The component must clear any error conditions (set status to OK) and
14183 * prepare itself to another Start call
14184 * returns ctl_ha_comp_status:
14188 static ctl_ha_comp_status
14189 ctl_isc_quiesce(struct ctl_ha_component *c)
14191 int ret = CTL_HA_COMP_STATUS_OK;
14198 struct ctl_ha_component ctl_ha_component_ctlisc =
14201 .state = CTL_HA_STATE_UNKNOWN,
14202 .init = ctl_isc_init,
14203 .start = ctl_isc_start,
14204 .quiesce = ctl_isc_quiesce