2 * Copyright (c) 2003-2009 Silicon Graphics International Corp.
3 * Copyright (c) 2012 The FreeBSD Foundation
6 * Portions of this software were developed by Edward Tomasz Napierala
7 * under sponsorship from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
16 * substantially similar to the "NO WARRANTY" disclaimer below
17 * ("Disclaimer") and any redistribution must be conditioned upon
18 * including a substantially similar Disclaimer requirement for further
19 * binary redistribution.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGES.
34 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl.c#8 $
37 * CAM Target Layer, a SCSI device emulation subsystem.
39 * Author: Ken Merry <ken@FreeBSD.org>
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/ctype.h>
50 #include <sys/kernel.h>
51 #include <sys/types.h>
52 #include <sys/kthread.h>
54 #include <sys/fcntl.h>
56 #include <sys/module.h>
57 #include <sys/mutex.h>
58 #include <sys/condvar.h>
59 #include <sys/malloc.h>
61 #include <sys/ioccom.h>
62 #include <sys/queue.h>
65 #include <sys/endian.h>
66 #include <sys/sysctl.h>
70 #include <cam/scsi/scsi_all.h>
71 #include <cam/scsi/scsi_da.h>
72 #include <cam/ctl/ctl_io.h>
73 #include <cam/ctl/ctl.h>
74 #include <cam/ctl/ctl_frontend.h>
75 #include <cam/ctl/ctl_frontend_internal.h>
76 #include <cam/ctl/ctl_util.h>
77 #include <cam/ctl/ctl_backend.h>
78 #include <cam/ctl/ctl_ioctl.h>
79 #include <cam/ctl/ctl_ha.h>
80 #include <cam/ctl/ctl_private.h>
81 #include <cam/ctl/ctl_debug.h>
82 #include <cam/ctl/ctl_scsi_all.h>
83 #include <cam/ctl/ctl_error.h>
85 struct ctl_softc *control_softc = NULL;
88 * Size and alignment macros needed for Copan-specific HA hardware. These
89 * can go away when the HA code is re-written, and uses busdma for any
92 #define CTL_ALIGN_8B(target, source, type) \
93 if (((uint32_t)source & 0x7) != 0) \
94 target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\
96 target = (type)source;
98 #define CTL_SIZE_8B(target, size) \
99 if ((size & 0x7) != 0) \
100 target = size + (0x8 - (size & 0x7)); \
104 #define CTL_ALIGN_8B_MARGIN 16
107 * Template mode pages.
111 * Note that these are default values only. The actual values will be
112 * filled in when the user does a mode sense.
114 static struct copan_debugconf_subpage debugconf_page_default = {
115 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
116 DBGCNF_SUBPAGE_CODE, /* subpage */
117 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
118 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
119 DBGCNF_VERSION, /* page_version */
120 {CTL_TIME_IO_DEFAULT_SECS>>8,
121 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */
124 static struct copan_debugconf_subpage debugconf_page_changeable = {
125 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
126 DBGCNF_SUBPAGE_CODE, /* subpage */
127 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
128 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
129 0, /* page_version */
130 {0xff,0xff}, /* ctl_time_io_secs */
133 static struct scsi_da_rw_recovery_page rw_er_page_default = {
134 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
135 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
136 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE,
137 /*read_retry_count*/0,
138 /*correction_span*/0,
139 /*head_offset_count*/0,
140 /*data_strobe_offset_cnt*/0,
141 /*byte8*/SMS_RWER_LBPERE,
142 /*write_retry_count*/0,
144 /*recovery_time_limit*/{0, 0},
147 static struct scsi_da_rw_recovery_page rw_er_page_changeable = {
148 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
149 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
151 /*read_retry_count*/0,
152 /*correction_span*/0,
153 /*head_offset_count*/0,
154 /*data_strobe_offset_cnt*/0,
156 /*write_retry_count*/0,
158 /*recovery_time_limit*/{0, 0},
161 static struct scsi_format_page format_page_default = {
162 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
163 /*page_length*/sizeof(struct scsi_format_page) - 2,
164 /*tracks_per_zone*/ {0, 0},
165 /*alt_sectors_per_zone*/ {0, 0},
166 /*alt_tracks_per_zone*/ {0, 0},
167 /*alt_tracks_per_lun*/ {0, 0},
168 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
169 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
170 /*bytes_per_sector*/ {0, 0},
171 /*interleave*/ {0, 0},
172 /*track_skew*/ {0, 0},
173 /*cylinder_skew*/ {0, 0},
175 /*reserved*/ {0, 0, 0}
178 static struct scsi_format_page format_page_changeable = {
179 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
180 /*page_length*/sizeof(struct scsi_format_page) - 2,
181 /*tracks_per_zone*/ {0, 0},
182 /*alt_sectors_per_zone*/ {0, 0},
183 /*alt_tracks_per_zone*/ {0, 0},
184 /*alt_tracks_per_lun*/ {0, 0},
185 /*sectors_per_track*/ {0, 0},
186 /*bytes_per_sector*/ {0, 0},
187 /*interleave*/ {0, 0},
188 /*track_skew*/ {0, 0},
189 /*cylinder_skew*/ {0, 0},
191 /*reserved*/ {0, 0, 0}
194 static struct scsi_rigid_disk_page rigid_disk_page_default = {
195 /*page_code*/SMS_RIGID_DISK_PAGE,
196 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
197 /*cylinders*/ {0, 0, 0},
198 /*heads*/ CTL_DEFAULT_HEADS,
199 /*start_write_precomp*/ {0, 0, 0},
200 /*start_reduced_current*/ {0, 0, 0},
201 /*step_rate*/ {0, 0},
202 /*landing_zone_cylinder*/ {0, 0, 0},
203 /*rpl*/ SRDP_RPL_DISABLED,
204 /*rotational_offset*/ 0,
206 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
207 CTL_DEFAULT_ROTATION_RATE & 0xff},
211 static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
212 /*page_code*/SMS_RIGID_DISK_PAGE,
213 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
214 /*cylinders*/ {0, 0, 0},
216 /*start_write_precomp*/ {0, 0, 0},
217 /*start_reduced_current*/ {0, 0, 0},
218 /*step_rate*/ {0, 0},
219 /*landing_zone_cylinder*/ {0, 0, 0},
221 /*rotational_offset*/ 0,
223 /*rotation_rate*/ {0, 0},
227 static struct scsi_caching_page caching_page_default = {
228 /*page_code*/SMS_CACHING_PAGE,
229 /*page_length*/sizeof(struct scsi_caching_page) - 2,
230 /*flags1*/ SCP_DISC | SCP_WCE,
232 /*disable_pf_transfer_len*/ {0xff, 0xff},
233 /*min_prefetch*/ {0, 0},
234 /*max_prefetch*/ {0xff, 0xff},
235 /*max_pf_ceiling*/ {0xff, 0xff},
237 /*cache_segments*/ 0,
238 /*cache_seg_size*/ {0, 0},
240 /*non_cache_seg_size*/ {0, 0, 0}
243 static struct scsi_caching_page caching_page_changeable = {
244 /*page_code*/SMS_CACHING_PAGE,
245 /*page_length*/sizeof(struct scsi_caching_page) - 2,
246 /*flags1*/ SCP_WCE | SCP_RCD,
248 /*disable_pf_transfer_len*/ {0, 0},
249 /*min_prefetch*/ {0, 0},
250 /*max_prefetch*/ {0, 0},
251 /*max_pf_ceiling*/ {0, 0},
253 /*cache_segments*/ 0,
254 /*cache_seg_size*/ {0, 0},
256 /*non_cache_seg_size*/ {0, 0, 0}
259 static struct scsi_control_page control_page_default = {
260 /*page_code*/SMS_CONTROL_MODE_PAGE,
261 /*page_length*/sizeof(struct scsi_control_page) - 2,
263 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED,
266 /*aen_holdoff_period*/{0, 0},
267 /*busy_timeout_period*/{0, 0},
268 /*extended_selftest_completion_time*/{0, 0}
271 static struct scsi_control_page control_page_changeable = {
272 /*page_code*/SMS_CONTROL_MODE_PAGE,
273 /*page_length*/sizeof(struct scsi_control_page) - 2,
275 /*queue_flags*/SCP_QUEUE_ALG_MASK,
276 /*eca_and_aen*/SCP_SWP,
278 /*aen_holdoff_period*/{0, 0},
279 /*busy_timeout_period*/{0, 0},
280 /*extended_selftest_completion_time*/{0, 0}
283 static struct scsi_info_exceptions_page ie_page_default = {
284 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
285 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
286 /*info_flags*/SIEP_FLAGS_DEXCPT,
288 /*interval_timer*/{0, 0, 0, 0},
289 /*report_count*/{0, 0, 0, 0}
292 static struct scsi_info_exceptions_page ie_page_changeable = {
293 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
294 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
297 /*interval_timer*/{0, 0, 0, 0},
298 /*report_count*/{0, 0, 0, 0}
301 #define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4)
303 static struct ctl_logical_block_provisioning_page lbp_page_default = {{
304 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
305 /*subpage_code*/0x02,
306 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
308 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
313 /*count*/{0, 0, 0, 0}},
317 /*count*/{0, 0, 0, 0}},
321 /*count*/{0, 0, 0, 0}},
325 /*count*/{0, 0, 0, 0}}
329 static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{
330 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
331 /*subpage_code*/0x02,
332 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
334 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
339 /*count*/{0, 0, 0, 0}},
343 /*count*/{0, 0, 0, 0}},
347 /*count*/{0, 0, 0, 0}},
351 /*count*/{0, 0, 0, 0}}
356 * XXX KDM move these into the softc.
358 static int rcv_sync_msg;
359 static uint8_t ctl_pause_rtr;
361 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
362 static int worker_threads = -1;
363 TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads);
364 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
365 &worker_threads, 1, "Number of worker threads");
366 static int ctl_debug = CTL_DEBUG_NONE;
367 TUNABLE_INT("kern.cam.ctl.debug", &ctl_debug);
368 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN,
369 &ctl_debug, 0, "Enabled debug flags");
372 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
373 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
374 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0),
375 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
377 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 10
379 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
381 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
382 static int ctl_init(void);
383 void ctl_shutdown(void);
384 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
385 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
386 static void ctl_ioctl_online(void *arg);
387 static void ctl_ioctl_offline(void *arg);
388 static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id);
389 static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id);
390 static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio);
391 static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
392 static int ctl_ioctl_submit_wait(union ctl_io *io);
393 static void ctl_ioctl_datamove(union ctl_io *io);
394 static void ctl_ioctl_done(union ctl_io *io);
395 static void ctl_ioctl_hard_startstop_callback(void *arg,
396 struct cfi_metatask *metatask);
397 static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask);
398 static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
399 struct ctl_ooa *ooa_hdr,
400 struct ctl_ooa_entry *kern_entries);
401 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
403 static uint32_t ctl_map_lun(int port_num, uint32_t lun);
404 static uint32_t ctl_map_lun_back(int port_num, uint32_t lun);
406 static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port,
407 uint32_t targ_target, uint32_t targ_lun,
409 static void ctl_kfree_io(union ctl_io *io);
411 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
412 struct ctl_be_lun *be_lun, struct ctl_id target_id);
413 static int ctl_free_lun(struct ctl_lun *lun);
414 static void ctl_create_lun(struct ctl_be_lun *be_lun);
416 static void ctl_failover_change_pages(struct ctl_softc *softc,
417 struct ctl_scsiio *ctsio, int master);
420 static int ctl_do_mode_select(union ctl_io *io);
421 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
422 uint64_t res_key, uint64_t sa_res_key,
423 uint8_t type, uint32_t residx,
424 struct ctl_scsiio *ctsio,
425 struct scsi_per_res_out *cdb,
426 struct scsi_per_res_out_parms* param);
427 static void ctl_pro_preempt_other(struct ctl_lun *lun,
428 union ctl_ha_msg *msg);
429 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
430 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
431 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
432 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
433 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
434 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
435 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
437 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
439 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
440 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
441 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
442 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
443 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
444 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2);
445 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
446 union ctl_io *pending_io, union ctl_io *ooa_io);
447 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
448 union ctl_io *starting_io);
449 static int ctl_check_blocked(struct ctl_lun *lun);
450 static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc,
452 const struct ctl_cmd_entry *entry,
453 struct ctl_scsiio *ctsio);
454 //static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc);
455 static void ctl_failover(void);
456 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
457 struct ctl_scsiio *ctsio);
458 static int ctl_scsiio(struct ctl_scsiio *ctsio);
460 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
461 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
462 ctl_ua_type ua_type);
463 static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io,
464 ctl_ua_type ua_type);
465 static int ctl_abort_task(union ctl_io *io);
466 static int ctl_abort_task_set(union ctl_io *io);
467 static int ctl_i_t_nexus_reset(union ctl_io *io);
468 static void ctl_run_task(union ctl_io *io);
470 static void ctl_datamove_timer_wakeup(void *arg);
471 static void ctl_done_timer_wakeup(void *arg);
472 #endif /* CTL_IO_DELAY */
474 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
475 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
476 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
477 static void ctl_datamove_remote_write(union ctl_io *io);
478 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
479 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
480 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
481 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
482 ctl_ha_dt_cb callback);
483 static void ctl_datamove_remote_read(union ctl_io *io);
484 static void ctl_datamove_remote(union ctl_io *io);
485 static int ctl_process_done(union ctl_io *io);
486 static void ctl_lun_thread(void *arg);
487 static void ctl_thresh_thread(void *arg);
488 static void ctl_work_thread(void *arg);
489 static void ctl_enqueue_incoming(union ctl_io *io);
490 static void ctl_enqueue_rtr(union ctl_io *io);
491 static void ctl_enqueue_done(union ctl_io *io);
492 static void ctl_enqueue_isc(union ctl_io *io);
493 static const struct ctl_cmd_entry *
494 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
495 static const struct ctl_cmd_entry *
496 ctl_validate_command(struct ctl_scsiio *ctsio);
497 static int ctl_cmd_applicable(uint8_t lun_type,
498 const struct ctl_cmd_entry *entry);
501 * Load the serialization table. This isn't very pretty, but is probably
502 * the easiest way to do it.
504 #include "ctl_ser_table.c"
507 * We only need to define open, close and ioctl routines for this driver.
509 static struct cdevsw ctl_cdevsw = {
510 .d_version = D_VERSION,
513 .d_close = ctl_close,
514 .d_ioctl = ctl_ioctl,
519 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
520 MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests");
522 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
524 static moduledata_t ctl_moduledata = {
526 ctl_module_event_handler,
530 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
531 MODULE_VERSION(ctl, 1);
533 static struct ctl_frontend ioctl_frontend =
539 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
540 union ctl_ha_msg *msg_info)
542 struct ctl_scsiio *ctsio;
544 if (msg_info->hdr.original_sc == NULL) {
545 printf("%s: original_sc == NULL!\n", __func__);
546 /* XXX KDM now what? */
550 ctsio = &msg_info->hdr.original_sc->scsiio;
551 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
552 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
553 ctsio->io_hdr.status = msg_info->hdr.status;
554 ctsio->scsi_status = msg_info->scsi.scsi_status;
555 ctsio->sense_len = msg_info->scsi.sense_len;
556 ctsio->sense_residual = msg_info->scsi.sense_residual;
557 ctsio->residual = msg_info->scsi.residual;
558 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
559 sizeof(ctsio->sense_data));
560 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
561 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen));
562 ctl_enqueue_isc((union ctl_io *)ctsio);
566 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
567 union ctl_ha_msg *msg_info)
569 struct ctl_scsiio *ctsio;
571 if (msg_info->hdr.serializing_sc == NULL) {
572 printf("%s: serializing_sc == NULL!\n", __func__);
573 /* XXX KDM now what? */
577 ctsio = &msg_info->hdr.serializing_sc->scsiio;
580 * Attempt to catch the situation where an I/O has
581 * been freed, and we're using it again.
583 if (ctsio->io_hdr.io_type == 0xff) {
584 union ctl_io *tmp_io;
585 tmp_io = (union ctl_io *)ctsio;
586 printf("%s: %p use after free!\n", __func__,
588 printf("%s: type %d msg %d cdb %x iptl: "
589 "%d:%d:%d:%d tag 0x%04x "
590 "flag %#x status %x\n",
592 tmp_io->io_hdr.io_type,
593 tmp_io->io_hdr.msg_type,
594 tmp_io->scsiio.cdb[0],
595 tmp_io->io_hdr.nexus.initid.id,
596 tmp_io->io_hdr.nexus.targ_port,
597 tmp_io->io_hdr.nexus.targ_target.id,
598 tmp_io->io_hdr.nexus.targ_lun,
599 (tmp_io->io_hdr.io_type ==
601 tmp_io->taskio.tag_num :
602 tmp_io->scsiio.tag_num,
603 tmp_io->io_hdr.flags,
604 tmp_io->io_hdr.status);
607 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
608 ctl_enqueue_isc((union ctl_io *)ctsio);
612 * ISC (Inter Shelf Communication) event handler. Events from the HA
613 * subsystem come in here.
616 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
618 struct ctl_softc *ctl_softc;
620 struct ctl_prio *presio;
621 ctl_ha_status isc_status;
623 ctl_softc = control_softc;
628 printf("CTL: Isc Msg event %d\n", event);
630 if (event == CTL_HA_EVT_MSG_RECV) {
631 union ctl_ha_msg msg_info;
633 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
634 sizeof(msg_info), /*wait*/ 0);
636 printf("CTL: msg_type %d\n", msg_info.msg_type);
638 if (isc_status != 0) {
639 printf("Error receiving message, status = %d\n",
644 switch (msg_info.hdr.msg_type) {
645 case CTL_MSG_SERIALIZE:
647 printf("Serialize\n");
649 io = ctl_alloc_io_nowait(ctl_softc->othersc_pool);
651 printf("ctl_isc_event_handler: can't allocate "
654 /* Need to set busy and send msg back */
655 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
656 msg_info.hdr.status = CTL_SCSI_ERROR;
657 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY;
658 msg_info.scsi.sense_len = 0;
659 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
660 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){
665 // populate ctsio from msg_info
666 io->io_hdr.io_type = CTL_IO_SCSI;
667 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
668 io->io_hdr.original_sc = msg_info.hdr.original_sc;
670 printf("pOrig %x\n", (int)msg_info.original_sc);
672 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
675 * If we're in serialization-only mode, we don't
676 * want to go through full done processing. Thus
679 * XXX KDM add another flag that is more specific.
681 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)
682 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
683 io->io_hdr.nexus = msg_info.hdr.nexus;
685 printf("targ %d, port %d, iid %d, lun %d\n",
686 io->io_hdr.nexus.targ_target.id,
687 io->io_hdr.nexus.targ_port,
688 io->io_hdr.nexus.initid.id,
689 io->io_hdr.nexus.targ_lun);
691 io->scsiio.tag_num = msg_info.scsi.tag_num;
692 io->scsiio.tag_type = msg_info.scsi.tag_type;
693 memcpy(io->scsiio.cdb, msg_info.scsi.cdb,
695 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
696 const struct ctl_cmd_entry *entry;
698 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
699 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
701 entry->flags & CTL_FLAG_DATA_MASK;
706 /* Performed on the Originating SC, XFER mode only */
707 case CTL_MSG_DATAMOVE: {
708 struct ctl_sg_entry *sgl;
711 io = msg_info.hdr.original_sc;
713 printf("%s: original_sc == NULL!\n", __func__);
714 /* XXX KDM do something here */
717 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
718 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
720 * Keep track of this, we need to send it back over
721 * when the datamove is complete.
723 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
725 if (msg_info.dt.sg_sequence == 0) {
727 * XXX KDM we use the preallocated S/G list
728 * here, but we'll need to change this to
729 * dynamic allocation if we need larger S/G
732 if (msg_info.dt.kern_sg_entries >
733 sizeof(io->io_hdr.remote_sglist) /
734 sizeof(io->io_hdr.remote_sglist[0])) {
735 printf("%s: number of S/G entries "
736 "needed %u > allocated num %zd\n",
738 msg_info.dt.kern_sg_entries,
739 sizeof(io->io_hdr.remote_sglist)/
740 sizeof(io->io_hdr.remote_sglist[0]));
743 * XXX KDM send a message back to
744 * the other side to shut down the
745 * DMA. The error will come back
746 * through via the normal channel.
750 sgl = io->io_hdr.remote_sglist;
752 sizeof(io->io_hdr.remote_sglist));
754 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
756 io->scsiio.kern_sg_entries =
757 msg_info.dt.kern_sg_entries;
758 io->scsiio.rem_sg_entries =
759 msg_info.dt.kern_sg_entries;
760 io->scsiio.kern_data_len =
761 msg_info.dt.kern_data_len;
762 io->scsiio.kern_total_len =
763 msg_info.dt.kern_total_len;
764 io->scsiio.kern_data_resid =
765 msg_info.dt.kern_data_resid;
766 io->scsiio.kern_rel_offset =
767 msg_info.dt.kern_rel_offset;
769 * Clear out per-DMA flags.
771 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK;
773 * Add per-DMA flags that are set for this
774 * particular DMA request.
776 io->io_hdr.flags |= msg_info.dt.flags &
779 sgl = (struct ctl_sg_entry *)
780 io->scsiio.kern_data_ptr;
782 for (i = msg_info.dt.sent_sg_entries, j = 0;
783 i < (msg_info.dt.sent_sg_entries +
784 msg_info.dt.cur_sg_entries); i++, j++) {
785 sgl[i].addr = msg_info.dt.sg_list[j].addr;
786 sgl[i].len = msg_info.dt.sg_list[j].len;
789 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n",
791 msg_info.dt.sg_list[j].addr,
792 msg_info.dt.sg_list[j].len,
793 sgl[i].addr, sgl[i].len, j, i);
797 memcpy(&sgl[msg_info.dt.sent_sg_entries],
799 sizeof(*sgl) * msg_info.dt.cur_sg_entries);
803 * If this is the last piece of the I/O, we've got
804 * the full S/G list. Queue processing in the thread.
805 * Otherwise wait for the next piece.
807 if (msg_info.dt.sg_last != 0)
811 /* Performed on the Serializing (primary) SC, XFER mode only */
812 case CTL_MSG_DATAMOVE_DONE: {
813 if (msg_info.hdr.serializing_sc == NULL) {
814 printf("%s: serializing_sc == NULL!\n",
816 /* XXX KDM now what? */
820 * We grab the sense information here in case
821 * there was a failure, so we can return status
822 * back to the initiator.
824 io = msg_info.hdr.serializing_sc;
825 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
826 io->io_hdr.status = msg_info.hdr.status;
827 io->scsiio.scsi_status = msg_info.scsi.scsi_status;
828 io->scsiio.sense_len = msg_info.scsi.sense_len;
829 io->scsiio.sense_residual =msg_info.scsi.sense_residual;
830 io->io_hdr.port_status = msg_info.scsi.fetd_status;
831 io->scsiio.residual = msg_info.scsi.residual;
832 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data,
833 sizeof(io->scsiio.sense_data));
838 /* Preformed on Originating SC, SER_ONLY mode */
840 io = msg_info.hdr.original_sc;
842 printf("%s: Major Bummer\n", __func__);
846 printf("pOrig %x\n",(int) ctsio);
849 io->io_hdr.msg_type = CTL_MSG_R2R;
850 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
855 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
857 * Performed on the Originating (i.e. secondary) SC in XFER
860 case CTL_MSG_FINISH_IO:
861 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER)
862 ctl_isc_handler_finish_xfer(ctl_softc,
865 ctl_isc_handler_finish_ser_only(ctl_softc,
869 /* Preformed on Originating SC */
870 case CTL_MSG_BAD_JUJU:
871 io = msg_info.hdr.original_sc;
873 printf("%s: Bad JUJU!, original_sc is NULL!\n",
877 ctl_copy_sense_data(&msg_info, io);
879 * IO should have already been cleaned up on other
880 * SC so clear this flag so we won't send a message
881 * back to finish the IO there.
883 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
884 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
886 /* io = msg_info.hdr.serializing_sc; */
887 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
891 /* Handle resets sent from the other side */
892 case CTL_MSG_MANAGE_TASKS: {
893 struct ctl_taskio *taskio;
894 taskio = (struct ctl_taskio *)ctl_alloc_io_nowait(
895 ctl_softc->othersc_pool);
896 if (taskio == NULL) {
897 printf("ctl_isc_event_handler: can't allocate "
900 /* should I just call the proper reset func
904 ctl_zero_io((union ctl_io *)taskio);
905 taskio->io_hdr.io_type = CTL_IO_TASK;
906 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
907 taskio->io_hdr.nexus = msg_info.hdr.nexus;
908 taskio->task_action = msg_info.task.task_action;
909 taskio->tag_num = msg_info.task.tag_num;
910 taskio->tag_type = msg_info.task.tag_type;
912 taskio->io_hdr.start_time = time_uptime;
913 getbintime(&taskio->io_hdr.start_bt);
915 cs_prof_gettime(&taskio->io_hdr.start_ticks);
917 #endif /* CTL_TIME_IO */
918 ctl_run_task((union ctl_io *)taskio);
921 /* Persistent Reserve action which needs attention */
922 case CTL_MSG_PERS_ACTION:
923 presio = (struct ctl_prio *)ctl_alloc_io_nowait(
924 ctl_softc->othersc_pool);
925 if (presio == NULL) {
926 printf("ctl_isc_event_handler: can't allocate "
929 /* Need to set busy and send msg back */
932 ctl_zero_io((union ctl_io *)presio);
933 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
934 presio->pr_msg = msg_info.pr;
935 ctl_enqueue_isc((union ctl_io *)presio);
937 case CTL_MSG_SYNC_FE:
941 printf("How did I get here?\n");
943 } else if (event == CTL_HA_EVT_MSG_SENT) {
944 if (param != CTL_HA_STATUS_SUCCESS) {
945 printf("Bad status from ctl_ha_msg_send status %d\n",
949 } else if (event == CTL_HA_EVT_DISCONNECT) {
950 printf("CTL: Got a disconnect from Isc\n");
953 printf("ctl_isc_event_handler: Unknown event %d\n", event);
962 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
964 struct scsi_sense_data *sense;
966 sense = &dest->scsiio.sense_data;
967 bcopy(&src->scsi.sense_data, sense, sizeof(*sense));
968 dest->scsiio.scsi_status = src->scsi.scsi_status;
969 dest->scsiio.sense_len = src->scsi.sense_len;
970 dest->io_hdr.status = src->hdr.status;
974 ctl_ha_state_sysctl(SYSCTL_HANDLER_ARGS)
976 struct ctl_softc *softc = (struct ctl_softc *)arg1;
980 if (softc->flags & CTL_FLAG_ACTIVE_SHELF)
985 error = sysctl_handle_int(oidp, &value, 0, req);
986 if ((error != 0) || (req->newptr == NULL))
989 mtx_lock(&softc->ctl_lock);
991 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
993 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
994 STAILQ_FOREACH(lun, &softc->lun_list, links) {
995 mtx_lock(&lun->lun_lock);
996 for (i = 0; i < CTL_MAX_INITIATORS; i++)
997 lun->pending_ua[i] |= CTL_UA_ASYM_ACC_CHANGE;
998 mtx_unlock(&lun->lun_lock);
1000 mtx_unlock(&softc->ctl_lock);
1007 struct ctl_softc *softc;
1009 struct ctl_port *port;
1010 int i, error, retval;
1017 control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1019 softc = control_softc;
1021 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
1024 softc->dev->si_drv1 = softc;
1027 * By default, return a "bad LUN" peripheral qualifier for unknown
1028 * LUNs. The user can override this default using the tunable or
1029 * sysctl. See the comment in ctl_inquiry_std() for more details.
1031 softc->inquiry_pq_no_lun = 1;
1032 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun",
1033 &softc->inquiry_pq_no_lun);
1034 sysctl_ctx_init(&softc->sysctl_ctx);
1035 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1036 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1037 CTLFLAG_RD, 0, "CAM Target Layer");
1039 if (softc->sysctl_tree == NULL) {
1040 printf("%s: unable to allocate sysctl tree\n", __func__);
1041 destroy_dev(softc->dev);
1042 free(control_softc, M_DEVBUF);
1043 control_softc = NULL;
1047 SYSCTL_ADD_INT(&softc->sysctl_ctx,
1048 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
1049 "inquiry_pq_no_lun", CTLFLAG_RW,
1050 &softc->inquiry_pq_no_lun, 0,
1051 "Report no lun possible for invalid LUNs");
1053 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1054 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
1055 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1056 softc->open_count = 0;
1059 * Default to actually sending a SYNCHRONIZE CACHE command down to
1062 softc->flags = CTL_FLAG_REAL_SYNC;
1065 * In Copan's HA scheme, the "master" and "slave" roles are
1066 * figured out through the slot the controller is in. Although it
1067 * is an active/active system, someone has to be in charge.
1069 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1070 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1071 "HA head ID (0 - no HA)");
1072 if (softc->ha_id == 0) {
1073 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1074 softc->is_single = 1;
1075 softc->port_offset = 0;
1077 softc->port_offset = (softc->ha_id - 1) * CTL_MAX_PORTS;
1078 softc->persis_offset = softc->port_offset * CTL_MAX_INIT_PER_PORT;
1081 * XXX KDM need to figure out where we want to get our target ID
1082 * and WWID. Is it different on each port?
1084 softc->target.id = 0;
1085 softc->target.wwid[0] = 0x12345678;
1086 softc->target.wwid[1] = 0x87654321;
1087 STAILQ_INIT(&softc->lun_list);
1088 STAILQ_INIT(&softc->pending_lun_queue);
1089 STAILQ_INIT(&softc->fe_list);
1090 STAILQ_INIT(&softc->port_list);
1091 STAILQ_INIT(&softc->be_list);
1092 ctl_tpc_init(softc);
1094 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
1097 printf("ctl: can't allocate %d entry other SC pool, "
1098 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1101 softc->othersc_pool = other_pool;
1103 if (worker_threads <= 0)
1104 worker_threads = max(1, mp_ncpus / 4);
1105 if (worker_threads > CTL_MAX_THREADS)
1106 worker_threads = CTL_MAX_THREADS;
1108 for (i = 0; i < worker_threads; i++) {
1109 struct ctl_thread *thr = &softc->threads[i];
1111 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1112 thr->ctl_softc = softc;
1113 STAILQ_INIT(&thr->incoming_queue);
1114 STAILQ_INIT(&thr->rtr_queue);
1115 STAILQ_INIT(&thr->done_queue);
1116 STAILQ_INIT(&thr->isc_queue);
1118 error = kproc_kthread_add(ctl_work_thread, thr,
1119 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1121 printf("error creating CTL work thread!\n");
1122 ctl_pool_free(other_pool);
1126 error = kproc_kthread_add(ctl_lun_thread, softc,
1127 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
1129 printf("error creating CTL lun thread!\n");
1130 ctl_pool_free(other_pool);
1133 error = kproc_kthread_add(ctl_thresh_thread, softc,
1134 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh");
1136 printf("error creating CTL threshold thread!\n");
1137 ctl_pool_free(other_pool);
1141 printf("ctl: CAM Target Layer loaded\n");
1144 * Initialize the ioctl front end.
1146 ctl_frontend_register(&ioctl_frontend);
1147 port = &softc->ioctl_info.port;
1148 port->frontend = &ioctl_frontend;
1149 sprintf(softc->ioctl_info.port_name, "ioctl");
1150 port->port_type = CTL_PORT_IOCTL;
1151 port->num_requested_ctl_io = 100;
1152 port->port_name = softc->ioctl_info.port_name;
1153 port->port_online = ctl_ioctl_online;
1154 port->port_offline = ctl_ioctl_offline;
1155 port->onoff_arg = &softc->ioctl_info;
1156 port->lun_enable = ctl_ioctl_lun_enable;
1157 port->lun_disable = ctl_ioctl_lun_disable;
1158 port->targ_lun_arg = &softc->ioctl_info;
1159 port->fe_datamove = ctl_ioctl_datamove;
1160 port->fe_done = ctl_ioctl_done;
1161 port->max_targets = 15;
1162 port->max_target_id = 15;
1164 if (ctl_port_register(&softc->ioctl_info.port) != 0) {
1165 printf("ctl: ioctl front end registration failed, will "
1166 "continue anyway\n");
1169 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1170 OID_AUTO, "ha_state", CTLTYPE_INT | CTLFLAG_RWTUN,
1171 softc, 0, ctl_ha_state_sysctl, "I", "HA state for this head");
1174 if (sizeof(struct callout) > CTL_TIMER_BYTES) {
1175 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n",
1176 sizeof(struct callout), CTL_TIMER_BYTES);
1179 #endif /* CTL_IO_DELAY */
1187 struct ctl_softc *softc;
1188 struct ctl_lun *lun, *next_lun;
1190 softc = (struct ctl_softc *)control_softc;
1192 if (ctl_port_deregister(&softc->ioctl_info.port) != 0)
1193 printf("ctl: ioctl front end deregistration failed\n");
1195 mtx_lock(&softc->ctl_lock);
1200 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
1201 next_lun = STAILQ_NEXT(lun, links);
1205 mtx_unlock(&softc->ctl_lock);
1207 ctl_frontend_deregister(&ioctl_frontend);
1210 ctl_shutdown_thread(softc->work_thread);
1211 mtx_destroy(&softc->queue_lock);
1214 ctl_tpc_shutdown(softc);
1215 uma_zdestroy(softc->io_zone);
1216 mtx_destroy(&softc->ctl_lock);
1218 destroy_dev(softc->dev);
1220 sysctl_ctx_free(&softc->sysctl_ctx);
1222 free(control_softc, M_DEVBUF);
1223 control_softc = NULL;
1226 printf("ctl: CAM Target Layer unloaded\n");
1230 ctl_module_event_handler(module_t mod, int what, void *arg)
1235 return (ctl_init());
1239 return (EOPNOTSUPP);
1244 * XXX KDM should we do some access checks here? Bump a reference count to
1245 * prevent a CTL module from being unloaded while someone has it open?
1248 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1254 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1260 ctl_port_enable(ctl_port_type port_type)
1262 struct ctl_softc *softc = control_softc;
1263 struct ctl_port *port;
1265 if (softc->is_single == 0) {
1266 union ctl_ha_msg msg_info;
1270 printf("%s: HA mode, synchronizing frontend enable\n",
1273 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE;
1274 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1275 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) {
1276 printf("Sync msg send error retval %d\n", isc_retval);
1278 if (!rcv_sync_msg) {
1279 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
1280 sizeof(msg_info), 1);
1283 printf("CTL:Frontend Enable\n");
1285 printf("%s: single mode, skipping frontend synchronization\n",
1290 STAILQ_FOREACH(port, &softc->port_list, links) {
1291 if (port_type & port->port_type)
1294 printf("port %d\n", port->targ_port);
1296 ctl_port_online(port);
1304 ctl_port_disable(ctl_port_type port_type)
1306 struct ctl_softc *softc;
1307 struct ctl_port *port;
1309 softc = control_softc;
1311 STAILQ_FOREACH(port, &softc->port_list, links) {
1312 if (port_type & port->port_type)
1313 ctl_port_offline(port);
1320 * Returns 0 for success, 1 for failure.
1321 * Currently the only failure mode is if there aren't enough entries
1322 * allocated. So, in case of a failure, look at num_entries_dropped,
1323 * reallocate and try again.
1326 ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced,
1327 int *num_entries_filled, int *num_entries_dropped,
1328 ctl_port_type port_type, int no_virtual)
1330 struct ctl_softc *softc;
1331 struct ctl_port *port;
1332 int entries_dropped, entries_filled;
1336 softc = control_softc;
1340 entries_dropped = 0;
1343 mtx_lock(&softc->ctl_lock);
1344 STAILQ_FOREACH(port, &softc->port_list, links) {
1345 struct ctl_port_entry *entry;
1347 if ((port->port_type & port_type) == 0)
1350 if ((no_virtual != 0)
1351 && (port->virtual_port != 0))
1354 if (entries_filled >= num_entries_alloced) {
1358 entry = &entries[i];
1360 entry->port_type = port->port_type;
1361 strlcpy(entry->port_name, port->port_name,
1362 sizeof(entry->port_name));
1363 entry->physical_port = port->physical_port;
1364 entry->virtual_port = port->virtual_port;
1365 entry->wwnn = port->wwnn;
1366 entry->wwpn = port->wwpn;
1372 mtx_unlock(&softc->ctl_lock);
1374 if (entries_dropped > 0)
1377 *num_entries_dropped = entries_dropped;
1378 *num_entries_filled = entries_filled;
1384 ctl_ioctl_online(void *arg)
1386 struct ctl_ioctl_info *ioctl_info;
1388 ioctl_info = (struct ctl_ioctl_info *)arg;
1390 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED;
1394 ctl_ioctl_offline(void *arg)
1396 struct ctl_ioctl_info *ioctl_info;
1398 ioctl_info = (struct ctl_ioctl_info *)arg;
1400 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED;
1404 * Remove an initiator by port number and initiator ID.
1405 * Returns 0 for success, -1 for failure.
1408 ctl_remove_initiator(struct ctl_port *port, int iid)
1410 struct ctl_softc *softc = control_softc;
1412 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1414 if (iid > CTL_MAX_INIT_PER_PORT) {
1415 printf("%s: initiator ID %u > maximun %u!\n",
1416 __func__, iid, CTL_MAX_INIT_PER_PORT);
1420 mtx_lock(&softc->ctl_lock);
1421 port->wwpn_iid[iid].in_use--;
1422 port->wwpn_iid[iid].last_use = time_uptime;
1423 mtx_unlock(&softc->ctl_lock);
1429 * Add an initiator to the initiator map.
1430 * Returns iid for success, < 0 for failure.
1433 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
1435 struct ctl_softc *softc = control_softc;
1439 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1441 if (iid >= CTL_MAX_INIT_PER_PORT) {
1442 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
1443 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
1448 mtx_lock(&softc->ctl_lock);
1450 if (iid < 0 && (wwpn != 0 || name != NULL)) {
1451 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1452 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
1456 if (name != NULL && port->wwpn_iid[i].name != NULL &&
1457 strcmp(name, port->wwpn_iid[i].name) == 0) {
1465 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1466 if (port->wwpn_iid[i].in_use == 0 &&
1467 port->wwpn_iid[i].wwpn == 0 &&
1468 port->wwpn_iid[i].name == NULL) {
1477 best_time = INT32_MAX;
1478 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1479 if (port->wwpn_iid[i].in_use == 0) {
1480 if (port->wwpn_iid[i].last_use < best_time) {
1482 best_time = port->wwpn_iid[i].last_use;
1490 mtx_unlock(&softc->ctl_lock);
1495 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
1497 * This is not an error yet.
1499 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
1501 printf("%s: port %d iid %u WWPN %#jx arrived"
1502 " again\n", __func__, port->targ_port,
1503 iid, (uintmax_t)wwpn);
1507 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
1508 strcmp(name, port->wwpn_iid[iid].name) == 0) {
1510 printf("%s: port %d iid %u name '%s' arrived"
1511 " again\n", __func__, port->targ_port,
1518 * This is an error, but what do we do about it? The
1519 * driver is telling us we have a new WWPN for this
1520 * initiator ID, so we pretty much need to use it.
1522 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
1523 " but WWPN %#jx '%s' is still at that address\n",
1524 __func__, port->targ_port, iid, wwpn, name,
1525 (uintmax_t)port->wwpn_iid[iid].wwpn,
1526 port->wwpn_iid[iid].name);
1529 * XXX KDM clear have_ca and ua_pending on each LUN for
1534 free(port->wwpn_iid[iid].name, M_CTL);
1535 port->wwpn_iid[iid].name = name;
1536 port->wwpn_iid[iid].wwpn = wwpn;
1537 port->wwpn_iid[iid].in_use++;
1538 mtx_unlock(&softc->ctl_lock);
1544 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
1548 switch (port->port_type) {
1551 struct scsi_transportid_fcp *id =
1552 (struct scsi_transportid_fcp *)buf;
1553 if (port->wwpn_iid[iid].wwpn == 0)
1555 memset(id, 0, sizeof(*id));
1556 id->format_protocol = SCSI_PROTO_FC;
1557 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
1558 return (sizeof(*id));
1560 case CTL_PORT_ISCSI:
1562 struct scsi_transportid_iscsi_port *id =
1563 (struct scsi_transportid_iscsi_port *)buf;
1564 if (port->wwpn_iid[iid].name == NULL)
1567 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
1569 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
1570 len = roundup2(min(len, 252), 4);
1571 scsi_ulto2b(len, id->additional_length);
1572 return (sizeof(*id) + len);
1576 struct scsi_transportid_sas *id =
1577 (struct scsi_transportid_sas *)buf;
1578 if (port->wwpn_iid[iid].wwpn == 0)
1580 memset(id, 0, sizeof(*id));
1581 id->format_protocol = SCSI_PROTO_SAS;
1582 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
1583 return (sizeof(*id));
1587 struct scsi_transportid_spi *id =
1588 (struct scsi_transportid_spi *)buf;
1589 memset(id, 0, sizeof(*id));
1590 id->format_protocol = SCSI_PROTO_SPI;
1591 scsi_ulto2b(iid, id->scsi_addr);
1592 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
1593 return (sizeof(*id));
1599 ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id)
1605 ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id)
1611 * Data movement routine for the CTL ioctl frontend port.
1614 ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio)
1616 struct ctl_sg_entry *ext_sglist, *kern_sglist;
1617 struct ctl_sg_entry ext_entry, kern_entry;
1618 int ext_sglen, ext_sg_entries, kern_sg_entries;
1619 int ext_sg_start, ext_offset;
1620 int len_to_copy, len_copied;
1621 int kern_watermark, ext_watermark;
1622 int ext_sglist_malloced;
1625 ext_sglist_malloced = 0;
1629 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n"));
1632 * If this flag is set, fake the data transfer.
1634 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) {
1635 ctsio->ext_data_filled = ctsio->ext_data_len;
1640 * To simplify things here, if we have a single buffer, stick it in
1641 * a S/G entry and just make it a single entry S/G list.
1643 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) {
1646 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist);
1648 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL,
1650 ext_sglist_malloced = 1;
1651 if (copyin(ctsio->ext_data_ptr, ext_sglist,
1653 ctl_set_internal_failure(ctsio,
1658 ext_sg_entries = ctsio->ext_sg_entries;
1660 for (i = 0; i < ext_sg_entries; i++) {
1661 if ((len_seen + ext_sglist[i].len) >=
1662 ctsio->ext_data_filled) {
1664 ext_offset = ctsio->ext_data_filled - len_seen;
1667 len_seen += ext_sglist[i].len;
1670 ext_sglist = &ext_entry;
1671 ext_sglist->addr = ctsio->ext_data_ptr;
1672 ext_sglist->len = ctsio->ext_data_len;
1675 ext_offset = ctsio->ext_data_filled;
1678 if (ctsio->kern_sg_entries > 0) {
1679 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
1680 kern_sg_entries = ctsio->kern_sg_entries;
1682 kern_sglist = &kern_entry;
1683 kern_sglist->addr = ctsio->kern_data_ptr;
1684 kern_sglist->len = ctsio->kern_data_len;
1685 kern_sg_entries = 1;
1690 ext_watermark = ext_offset;
1692 for (i = ext_sg_start, j = 0;
1693 i < ext_sg_entries && j < kern_sg_entries;) {
1694 uint8_t *ext_ptr, *kern_ptr;
1696 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark,
1697 kern_sglist[j].len - kern_watermark);
1699 ext_ptr = (uint8_t *)ext_sglist[i].addr;
1700 ext_ptr = ext_ptr + ext_watermark;
1701 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
1705 panic("need to implement bus address support");
1707 kern_ptr = bus_to_virt(kern_sglist[j].addr);
1710 kern_ptr = (uint8_t *)kern_sglist[j].addr;
1711 kern_ptr = kern_ptr + kern_watermark;
1713 kern_watermark += len_to_copy;
1714 ext_watermark += len_to_copy;
1716 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
1718 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1719 "bytes to user\n", len_to_copy));
1720 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1721 "to %p\n", kern_ptr, ext_ptr));
1722 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) {
1723 ctl_set_internal_failure(ctsio,
1729 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1730 "bytes from user\n", len_to_copy));
1731 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1732 "to %p\n", ext_ptr, kern_ptr));
1733 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){
1734 ctl_set_internal_failure(ctsio,
1741 len_copied += len_to_copy;
1743 if (ext_sglist[i].len == ext_watermark) {
1748 if (kern_sglist[j].len == kern_watermark) {
1754 ctsio->ext_data_filled += len_copied;
1756 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, "
1757 "kern_sg_entries: %d\n", ext_sg_entries,
1759 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, "
1760 "kern_data_len = %d\n", ctsio->ext_data_len,
1761 ctsio->kern_data_len));
1764 /* XXX KDM set residual?? */
1767 if (ext_sglist_malloced != 0)
1768 free(ext_sglist, M_CTL);
1770 return (CTL_RETVAL_COMPLETE);
1774 * Serialize a command that went down the "wrong" side, and so was sent to
1775 * this controller for execution. The logic is a little different than the
1776 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
1777 * sent back to the other side, but in the success case, we execute the
1778 * command on this side (XFER mode) or tell the other side to execute it
1782 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
1784 struct ctl_softc *ctl_softc;
1785 union ctl_ha_msg msg_info;
1786 struct ctl_lun *lun;
1790 ctl_softc = control_softc;
1792 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
1793 lun = ctl_softc->ctl_luns[targ_lun];
1797 * Why isn't LUN defined? The other side wouldn't
1798 * send a cmd if the LUN is undefined.
1800 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__);
1802 /* "Logical unit not supported" */
1803 ctl_set_sense_data(&msg_info.scsi.sense_data,
1805 /*sense_format*/SSD_TYPE_NONE,
1806 /*current_error*/ 1,
1807 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1812 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1813 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1814 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1815 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1816 msg_info.hdr.serializing_sc = NULL;
1817 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1818 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1819 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1825 mtx_lock(&lun->lun_lock);
1826 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1828 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
1829 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
1831 case CTL_ACTION_BLOCK:
1832 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
1833 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
1836 case CTL_ACTION_PASS:
1837 case CTL_ACTION_SKIP:
1838 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
1839 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
1840 ctl_enqueue_rtr((union ctl_io *)ctsio);
1843 /* send msg back to other side */
1844 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1845 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
1846 msg_info.hdr.msg_type = CTL_MSG_R2R;
1848 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc);
1850 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1851 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1855 case CTL_ACTION_OVERLAP:
1856 /* OVERLAPPED COMMANDS ATTEMPTED */
1857 ctl_set_sense_data(&msg_info.scsi.sense_data,
1859 /*sense_format*/SSD_TYPE_NONE,
1860 /*current_error*/ 1,
1861 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1866 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1867 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1868 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1869 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1870 msg_info.hdr.serializing_sc = NULL;
1871 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1873 printf("BAD JUJU:Major Bummer Overlap\n");
1875 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1877 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1878 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1881 case CTL_ACTION_OVERLAP_TAG:
1882 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */
1883 ctl_set_sense_data(&msg_info.scsi.sense_data,
1885 /*sense_format*/SSD_TYPE_NONE,
1886 /*current_error*/ 1,
1887 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1889 /*ascq*/ ctsio->tag_num & 0xff,
1892 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1893 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1894 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1895 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1896 msg_info.hdr.serializing_sc = NULL;
1897 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1899 printf("BAD JUJU:Major Bummer Overlap Tag\n");
1901 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1903 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1904 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1907 case CTL_ACTION_ERROR:
1909 /* "Internal target failure" */
1910 ctl_set_sense_data(&msg_info.scsi.sense_data,
1912 /*sense_format*/SSD_TYPE_NONE,
1913 /*current_error*/ 1,
1914 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
1919 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1920 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1921 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1922 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1923 msg_info.hdr.serializing_sc = NULL;
1924 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1926 printf("BAD JUJU:Major Bummer HW Error\n");
1928 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1930 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1931 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1935 mtx_unlock(&lun->lun_lock);
1940 ctl_ioctl_submit_wait(union ctl_io *io)
1942 struct ctl_fe_ioctl_params params;
1943 ctl_fe_ioctl_state last_state;
1948 bzero(¶ms, sizeof(params));
1950 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF);
1951 cv_init(¶ms.sem, "ctlioccv");
1952 params.state = CTL_IOCTL_INPROG;
1953 last_state = params.state;
1955 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms;
1957 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n"));
1959 /* This shouldn't happen */
1960 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE)
1966 mtx_lock(¶ms.ioctl_mtx);
1968 * Check the state here, and don't sleep if the state has
1969 * already changed (i.e. wakeup has already occured, but we
1970 * weren't waiting yet).
1972 if (params.state == last_state) {
1973 /* XXX KDM cv_wait_sig instead? */
1974 cv_wait(¶ms.sem, ¶ms.ioctl_mtx);
1976 last_state = params.state;
1978 switch (params.state) {
1979 case CTL_IOCTL_INPROG:
1980 /* Why did we wake up? */
1981 /* XXX KDM error here? */
1982 mtx_unlock(¶ms.ioctl_mtx);
1984 case CTL_IOCTL_DATAMOVE:
1985 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n"));
1988 * change last_state back to INPROG to avoid
1989 * deadlock on subsequent data moves.
1991 params.state = last_state = CTL_IOCTL_INPROG;
1993 mtx_unlock(¶ms.ioctl_mtx);
1994 ctl_ioctl_do_datamove(&io->scsiio);
1996 * Note that in some cases, most notably writes,
1997 * this will queue the I/O and call us back later.
1998 * In other cases, generally reads, this routine
1999 * will immediately call back and wake us up,
2000 * probably using our own context.
2002 io->scsiio.be_move_done(io);
2004 case CTL_IOCTL_DONE:
2005 mtx_unlock(¶ms.ioctl_mtx);
2006 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n"));
2010 mtx_unlock(¶ms.ioctl_mtx);
2011 /* XXX KDM error here? */
2014 } while (done == 0);
2016 mtx_destroy(¶ms.ioctl_mtx);
2017 cv_destroy(¶ms.sem);
2019 return (CTL_RETVAL_COMPLETE);
2023 ctl_ioctl_datamove(union ctl_io *io)
2025 struct ctl_fe_ioctl_params *params;
2027 params = (struct ctl_fe_ioctl_params *)
2028 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2030 mtx_lock(¶ms->ioctl_mtx);
2031 params->state = CTL_IOCTL_DATAMOVE;
2032 cv_broadcast(¶ms->sem);
2033 mtx_unlock(¶ms->ioctl_mtx);
2037 ctl_ioctl_done(union ctl_io *io)
2039 struct ctl_fe_ioctl_params *params;
2041 params = (struct ctl_fe_ioctl_params *)
2042 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2044 mtx_lock(¶ms->ioctl_mtx);
2045 params->state = CTL_IOCTL_DONE;
2046 cv_broadcast(¶ms->sem);
2047 mtx_unlock(¶ms->ioctl_mtx);
2051 ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask)
2053 struct ctl_fe_ioctl_startstop_info *sd_info;
2055 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg;
2057 sd_info->hs_info.status = metatask->status;
2058 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns;
2059 sd_info->hs_info.luns_complete =
2060 metatask->taskinfo.startstop.luns_complete;
2061 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed;
2063 cv_broadcast(&sd_info->sem);
2067 ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask)
2069 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info;
2071 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg;
2073 mtx_lock(fe_bbr_info->lock);
2074 fe_bbr_info->bbr_info->status = metatask->status;
2075 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2076 fe_bbr_info->wakeup_done = 1;
2077 mtx_unlock(fe_bbr_info->lock);
2079 cv_broadcast(&fe_bbr_info->sem);
2083 * Returns 0 for success, errno for failure.
2086 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2087 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2094 mtx_lock(&lun->lun_lock);
2095 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2096 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2098 struct ctl_ooa_entry *entry;
2101 * If we've got more than we can fit, just count the
2102 * remaining entries.
2104 if (*cur_fill_num >= ooa_hdr->alloc_num)
2107 entry = &kern_entries[*cur_fill_num];
2109 entry->tag_num = io->scsiio.tag_num;
2110 entry->lun_num = lun->lun;
2112 entry->start_bt = io->io_hdr.start_bt;
2114 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2115 entry->cdb_len = io->scsiio.cdb_len;
2116 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2117 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2119 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2120 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2122 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2123 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2125 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2126 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2128 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2129 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2131 mtx_unlock(&lun->lun_lock);
2137 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2138 size_t error_str_len)
2142 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2144 if (copyin(user_addr, kptr, len) != 0) {
2145 snprintf(error_str, error_str_len, "Error copying %d bytes "
2146 "from user address %p to kernel address %p", len,
2156 ctl_free_args(int num_args, struct ctl_be_arg *args)
2163 for (i = 0; i < num_args; i++) {
2164 free(args[i].kname, M_CTL);
2165 free(args[i].kvalue, M_CTL);
2171 static struct ctl_be_arg *
2172 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
2173 char *error_str, size_t error_str_len)
2175 struct ctl_be_arg *args;
2178 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
2179 error_str, error_str_len);
2184 for (i = 0; i < num_args; i++) {
2185 args[i].kname = NULL;
2186 args[i].kvalue = NULL;
2189 for (i = 0; i < num_args; i++) {
2192 args[i].kname = ctl_copyin_alloc(args[i].name,
2193 args[i].namelen, error_str, error_str_len);
2194 if (args[i].kname == NULL)
2197 if (args[i].kname[args[i].namelen - 1] != '\0') {
2198 snprintf(error_str, error_str_len, "Argument %d "
2199 "name is not NUL-terminated", i);
2203 if (args[i].flags & CTL_BEARG_RD) {
2204 tmpptr = ctl_copyin_alloc(args[i].value,
2205 args[i].vallen, error_str, error_str_len);
2208 if ((args[i].flags & CTL_BEARG_ASCII)
2209 && (tmpptr[args[i].vallen - 1] != '\0')) {
2210 snprintf(error_str, error_str_len, "Argument "
2211 "%d value is not NUL-terminated", i);
2214 args[i].kvalue = tmpptr;
2216 args[i].kvalue = malloc(args[i].vallen,
2217 M_CTL, M_WAITOK | M_ZERO);
2224 ctl_free_args(num_args, args);
2230 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2234 for (i = 0; i < num_args; i++) {
2235 if (args[i].flags & CTL_BEARG_WR)
2236 copyout(args[i].kvalue, args[i].value, args[i].vallen);
2241 * Escape characters that are illegal or not recommended in XML.
2244 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2246 char *end = str + size;
2251 for (; *str && str < end; str++) {
2254 retval = sbuf_printf(sb, "&");
2257 retval = sbuf_printf(sb, ">");
2260 retval = sbuf_printf(sb, "<");
2263 retval = sbuf_putc(sb, *str);
2276 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2278 struct scsi_vpd_id_descriptor *desc;
2281 if (id == NULL || id->len < 4)
2283 desc = (struct scsi_vpd_id_descriptor *)id->data;
2284 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2285 case SVPD_ID_TYPE_T10:
2286 sbuf_printf(sb, "t10.");
2288 case SVPD_ID_TYPE_EUI64:
2289 sbuf_printf(sb, "eui.");
2291 case SVPD_ID_TYPE_NAA:
2292 sbuf_printf(sb, "naa.");
2294 case SVPD_ID_TYPE_SCSI_NAME:
2297 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2298 case SVPD_ID_CODESET_BINARY:
2299 for (i = 0; i < desc->length; i++)
2300 sbuf_printf(sb, "%02x", desc->identifier[i]);
2302 case SVPD_ID_CODESET_ASCII:
2303 sbuf_printf(sb, "%.*s", (int)desc->length,
2304 (char *)desc->identifier);
2306 case SVPD_ID_CODESET_UTF8:
2307 sbuf_printf(sb, "%s", (char *)desc->identifier);
2313 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2316 struct ctl_softc *softc;
2319 softc = control_softc;
2329 * If we haven't been "enabled", don't allow any SCSI I/O
2332 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) {
2337 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref);
2340 * Need to save the pool reference so it doesn't get
2341 * spammed by the user's ctl_io.
2343 pool_tmp = io->io_hdr.pool;
2344 memcpy(io, (void *)addr, sizeof(*io));
2345 io->io_hdr.pool = pool_tmp;
2348 * No status yet, so make sure the status is set properly.
2350 io->io_hdr.status = CTL_STATUS_NONE;
2353 * The user sets the initiator ID, target and LUN IDs.
2355 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port;
2356 io->io_hdr.flags |= CTL_FLAG_USER_REQ;
2357 if ((io->io_hdr.io_type == CTL_IO_SCSI)
2358 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED))
2359 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++;
2361 retval = ctl_ioctl_submit_wait(io);
2368 memcpy((void *)addr, io, sizeof(*io));
2370 /* return this to our pool */
2375 case CTL_ENABLE_PORT:
2376 case CTL_DISABLE_PORT:
2377 case CTL_SET_PORT_WWNS: {
2378 struct ctl_port *port;
2379 struct ctl_port_entry *entry;
2381 entry = (struct ctl_port_entry *)addr;
2383 mtx_lock(&softc->ctl_lock);
2384 STAILQ_FOREACH(port, &softc->port_list, links) {
2390 if ((entry->port_type == CTL_PORT_NONE)
2391 && (entry->targ_port == port->targ_port)) {
2393 * If the user only wants to enable or
2394 * disable or set WWNs on a specific port,
2395 * do the operation and we're done.
2399 } else if (entry->port_type & port->port_type) {
2401 * Compare the user's type mask with the
2402 * particular frontend type to see if we
2409 * Make sure the user isn't trying to set
2410 * WWNs on multiple ports at the same time.
2412 if (cmd == CTL_SET_PORT_WWNS) {
2413 printf("%s: Can't set WWNs on "
2414 "multiple ports\n", __func__);
2421 * XXX KDM we have to drop the lock here,
2422 * because the online/offline operations
2423 * can potentially block. We need to
2424 * reference count the frontends so they
2427 mtx_unlock(&softc->ctl_lock);
2429 if (cmd == CTL_ENABLE_PORT) {
2430 struct ctl_lun *lun;
2432 STAILQ_FOREACH(lun, &softc->lun_list,
2434 port->lun_enable(port->targ_lun_arg,
2439 ctl_port_online(port);
2440 } else if (cmd == CTL_DISABLE_PORT) {
2441 struct ctl_lun *lun;
2443 ctl_port_offline(port);
2445 STAILQ_FOREACH(lun, &softc->lun_list,
2454 mtx_lock(&softc->ctl_lock);
2456 if (cmd == CTL_SET_PORT_WWNS)
2457 ctl_port_set_wwns(port,
2458 (entry->flags & CTL_PORT_WWNN_VALID) ?
2460 (entry->flags & CTL_PORT_WWPN_VALID) ?
2461 1 : 0, entry->wwpn);
2466 mtx_unlock(&softc->ctl_lock);
2469 case CTL_GET_PORT_LIST: {
2470 struct ctl_port *port;
2471 struct ctl_port_list *list;
2474 list = (struct ctl_port_list *)addr;
2476 if (list->alloc_len != (list->alloc_num *
2477 sizeof(struct ctl_port_entry))) {
2478 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != "
2479 "alloc_num %u * sizeof(struct ctl_port_entry) "
2480 "%zu\n", __func__, list->alloc_len,
2481 list->alloc_num, sizeof(struct ctl_port_entry));
2487 list->dropped_num = 0;
2489 mtx_lock(&softc->ctl_lock);
2490 STAILQ_FOREACH(port, &softc->port_list, links) {
2491 struct ctl_port_entry entry, *list_entry;
2493 if (list->fill_num >= list->alloc_num) {
2494 list->dropped_num++;
2498 entry.port_type = port->port_type;
2499 strlcpy(entry.port_name, port->port_name,
2500 sizeof(entry.port_name));
2501 entry.targ_port = port->targ_port;
2502 entry.physical_port = port->physical_port;
2503 entry.virtual_port = port->virtual_port;
2504 entry.wwnn = port->wwnn;
2505 entry.wwpn = port->wwpn;
2506 if (port->status & CTL_PORT_STATUS_ONLINE)
2511 list_entry = &list->entries[i];
2513 retval = copyout(&entry, list_entry, sizeof(entry));
2515 printf("%s: CTL_GET_PORT_LIST: copyout "
2516 "returned %d\n", __func__, retval);
2521 list->fill_len += sizeof(entry);
2523 mtx_unlock(&softc->ctl_lock);
2526 * If this is non-zero, we had a copyout fault, so there's
2527 * probably no point in attempting to set the status inside
2533 if (list->dropped_num > 0)
2534 list->status = CTL_PORT_LIST_NEED_MORE_SPACE;
2536 list->status = CTL_PORT_LIST_OK;
2539 case CTL_DUMP_OOA: {
2540 struct ctl_lun *lun;
2545 mtx_lock(&softc->ctl_lock);
2546 printf("Dumping OOA queues:\n");
2547 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2548 mtx_lock(&lun->lun_lock);
2549 for (io = (union ctl_io *)TAILQ_FIRST(
2550 &lun->ooa_queue); io != NULL;
2551 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2553 sbuf_new(&sb, printbuf, sizeof(printbuf),
2555 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ",
2559 CTL_FLAG_BLOCKED) ? "" : " BLOCKED",
2561 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
2563 CTL_FLAG_ABORT) ? " ABORT" : "",
2565 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : "");
2566 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
2568 printf("%s\n", sbuf_data(&sb));
2570 mtx_unlock(&lun->lun_lock);
2572 printf("OOA queues dump done\n");
2573 mtx_unlock(&softc->ctl_lock);
2577 struct ctl_lun *lun;
2578 struct ctl_ooa *ooa_hdr;
2579 struct ctl_ooa_entry *entries;
2580 uint32_t cur_fill_num;
2582 ooa_hdr = (struct ctl_ooa *)addr;
2584 if ((ooa_hdr->alloc_len == 0)
2585 || (ooa_hdr->alloc_num == 0)) {
2586 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2587 "must be non-zero\n", __func__,
2588 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2593 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2594 sizeof(struct ctl_ooa_entry))) {
2595 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2596 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2597 __func__, ooa_hdr->alloc_len,
2598 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2603 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2604 if (entries == NULL) {
2605 printf("%s: could not allocate %d bytes for OOA "
2606 "dump\n", __func__, ooa_hdr->alloc_len);
2611 mtx_lock(&softc->ctl_lock);
2612 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2613 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS)
2614 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2615 mtx_unlock(&softc->ctl_lock);
2616 free(entries, M_CTL);
2617 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2618 __func__, (uintmax_t)ooa_hdr->lun_num);
2625 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2626 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2627 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2633 mtx_unlock(&softc->ctl_lock);
2634 free(entries, M_CTL);
2638 lun = softc->ctl_luns[ooa_hdr->lun_num];
2640 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr,
2643 mtx_unlock(&softc->ctl_lock);
2645 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2646 ooa_hdr->fill_len = ooa_hdr->fill_num *
2647 sizeof(struct ctl_ooa_entry);
2648 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2650 printf("%s: error copying out %d bytes for OOA dump\n",
2651 __func__, ooa_hdr->fill_len);
2654 getbintime(&ooa_hdr->cur_bt);
2656 if (cur_fill_num > ooa_hdr->alloc_num) {
2657 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2658 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2660 ooa_hdr->dropped_num = 0;
2661 ooa_hdr->status = CTL_OOA_OK;
2664 free(entries, M_CTL);
2667 case CTL_CHECK_OOA: {
2669 struct ctl_lun *lun;
2670 struct ctl_ooa_info *ooa_info;
2673 ooa_info = (struct ctl_ooa_info *)addr;
2675 if (ooa_info->lun_id >= CTL_MAX_LUNS) {
2676 ooa_info->status = CTL_OOA_INVALID_LUN;
2679 mtx_lock(&softc->ctl_lock);
2680 lun = softc->ctl_luns[ooa_info->lun_id];
2682 mtx_unlock(&softc->ctl_lock);
2683 ooa_info->status = CTL_OOA_INVALID_LUN;
2686 mtx_lock(&lun->lun_lock);
2687 mtx_unlock(&softc->ctl_lock);
2688 ooa_info->num_entries = 0;
2689 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
2690 io != NULL; io = (union ctl_io *)TAILQ_NEXT(
2691 &io->io_hdr, ooa_links)) {
2692 ooa_info->num_entries++;
2694 mtx_unlock(&lun->lun_lock);
2696 ooa_info->status = CTL_OOA_SUCCESS;
2700 case CTL_HARD_START:
2701 case CTL_HARD_STOP: {
2702 struct ctl_fe_ioctl_startstop_info ss_info;
2703 struct cfi_metatask *metatask;
2706 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF);
2708 cv_init(&ss_info.sem, "hard start/stop cv" );
2710 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2711 if (metatask == NULL) {
2713 mtx_destroy(&hs_mtx);
2717 if (cmd == CTL_HARD_START)
2718 metatask->tasktype = CFI_TASK_STARTUP;
2720 metatask->tasktype = CFI_TASK_SHUTDOWN;
2722 metatask->callback = ctl_ioctl_hard_startstop_callback;
2723 metatask->callback_arg = &ss_info;
2725 cfi_action(metatask);
2727 /* Wait for the callback */
2729 cv_wait_sig(&ss_info.sem, &hs_mtx);
2730 mtx_unlock(&hs_mtx);
2733 * All information has been copied from the metatask by the
2734 * time cv_broadcast() is called, so we free the metatask here.
2736 cfi_free_metatask(metatask);
2738 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info));
2740 mtx_destroy(&hs_mtx);
2744 struct ctl_bbrread_info *bbr_info;
2745 struct ctl_fe_ioctl_bbrread_info fe_bbr_info;
2747 struct cfi_metatask *metatask;
2749 bbr_info = (struct ctl_bbrread_info *)addr;
2751 bzero(&fe_bbr_info, sizeof(fe_bbr_info));
2753 bzero(&bbr_mtx, sizeof(bbr_mtx));
2754 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF);
2756 fe_bbr_info.bbr_info = bbr_info;
2757 fe_bbr_info.lock = &bbr_mtx;
2759 cv_init(&fe_bbr_info.sem, "BBR read cv");
2760 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2762 if (metatask == NULL) {
2763 mtx_destroy(&bbr_mtx);
2764 cv_destroy(&fe_bbr_info.sem);
2768 metatask->tasktype = CFI_TASK_BBRREAD;
2769 metatask->callback = ctl_ioctl_bbrread_callback;
2770 metatask->callback_arg = &fe_bbr_info;
2771 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num;
2772 metatask->taskinfo.bbrread.lba = bbr_info->lba;
2773 metatask->taskinfo.bbrread.len = bbr_info->len;
2775 cfi_action(metatask);
2778 while (fe_bbr_info.wakeup_done == 0)
2779 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx);
2780 mtx_unlock(&bbr_mtx);
2782 bbr_info->status = metatask->status;
2783 bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2784 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status;
2785 memcpy(&bbr_info->sense_data,
2786 &metatask->taskinfo.bbrread.sense_data,
2787 ctl_min(sizeof(bbr_info->sense_data),
2788 sizeof(metatask->taskinfo.bbrread.sense_data)));
2790 cfi_free_metatask(metatask);
2792 mtx_destroy(&bbr_mtx);
2793 cv_destroy(&fe_bbr_info.sem);
2797 case CTL_DELAY_IO: {
2798 struct ctl_io_delay_info *delay_info;
2800 struct ctl_lun *lun;
2801 #endif /* CTL_IO_DELAY */
2803 delay_info = (struct ctl_io_delay_info *)addr;
2806 mtx_lock(&softc->ctl_lock);
2808 if ((delay_info->lun_id >= CTL_MAX_LUNS)
2809 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2810 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2812 lun = softc->ctl_luns[delay_info->lun_id];
2813 mtx_lock(&lun->lun_lock);
2815 delay_info->status = CTL_DELAY_STATUS_OK;
2817 switch (delay_info->delay_type) {
2818 case CTL_DELAY_TYPE_CONT:
2820 case CTL_DELAY_TYPE_ONESHOT:
2823 delay_info->status =
2824 CTL_DELAY_STATUS_INVALID_TYPE;
2828 switch (delay_info->delay_loc) {
2829 case CTL_DELAY_LOC_DATAMOVE:
2830 lun->delay_info.datamove_type =
2831 delay_info->delay_type;
2832 lun->delay_info.datamove_delay =
2833 delay_info->delay_secs;
2835 case CTL_DELAY_LOC_DONE:
2836 lun->delay_info.done_type =
2837 delay_info->delay_type;
2838 lun->delay_info.done_delay =
2839 delay_info->delay_secs;
2842 delay_info->status =
2843 CTL_DELAY_STATUS_INVALID_LOC;
2846 mtx_unlock(&lun->lun_lock);
2849 mtx_unlock(&softc->ctl_lock);
2851 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2852 #endif /* CTL_IO_DELAY */
2855 case CTL_REALSYNC_SET: {
2858 syncstate = (int *)addr;
2860 mtx_lock(&softc->ctl_lock);
2861 switch (*syncstate) {
2863 softc->flags &= ~CTL_FLAG_REAL_SYNC;
2866 softc->flags |= CTL_FLAG_REAL_SYNC;
2872 mtx_unlock(&softc->ctl_lock);
2875 case CTL_REALSYNC_GET: {
2878 syncstate = (int*)addr;
2880 mtx_lock(&softc->ctl_lock);
2881 if (softc->flags & CTL_FLAG_REAL_SYNC)
2885 mtx_unlock(&softc->ctl_lock);
2891 struct ctl_sync_info *sync_info;
2892 struct ctl_lun *lun;
2894 sync_info = (struct ctl_sync_info *)addr;
2896 mtx_lock(&softc->ctl_lock);
2897 lun = softc->ctl_luns[sync_info->lun_id];
2899 mtx_unlock(&softc->ctl_lock);
2900 sync_info->status = CTL_GS_SYNC_NO_LUN;
2903 * Get or set the sync interval. We're not bounds checking
2904 * in the set case, hopefully the user won't do something
2907 mtx_lock(&lun->lun_lock);
2908 mtx_unlock(&softc->ctl_lock);
2909 if (cmd == CTL_GETSYNC)
2910 sync_info->sync_interval = lun->sync_interval;
2912 lun->sync_interval = sync_info->sync_interval;
2913 mtx_unlock(&lun->lun_lock);
2915 sync_info->status = CTL_GS_SYNC_OK;
2919 case CTL_GETSTATS: {
2920 struct ctl_stats *stats;
2921 struct ctl_lun *lun;
2924 stats = (struct ctl_stats *)addr;
2926 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2928 stats->status = CTL_SS_NEED_MORE_SPACE;
2929 stats->num_luns = softc->num_luns;
2933 * XXX KDM no locking here. If the LUN list changes,
2934 * things can blow up.
2936 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
2937 i++, lun = STAILQ_NEXT(lun, links)) {
2938 retval = copyout(&lun->stats, &stats->lun_stats[i],
2939 sizeof(lun->stats));
2943 stats->num_luns = softc->num_luns;
2944 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2946 stats->status = CTL_SS_OK;
2948 stats->flags = CTL_STATS_FLAG_TIME_VALID;
2950 stats->flags = CTL_STATS_FLAG_NONE;
2952 getnanouptime(&stats->timestamp);
2955 case CTL_ERROR_INJECT: {
2956 struct ctl_error_desc *err_desc, *new_err_desc;
2957 struct ctl_lun *lun;
2959 err_desc = (struct ctl_error_desc *)addr;
2961 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2963 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2965 mtx_lock(&softc->ctl_lock);
2966 lun = softc->ctl_luns[err_desc->lun_id];
2968 mtx_unlock(&softc->ctl_lock);
2969 free(new_err_desc, M_CTL);
2970 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2971 __func__, (uintmax_t)err_desc->lun_id);
2975 mtx_lock(&lun->lun_lock);
2976 mtx_unlock(&softc->ctl_lock);
2979 * We could do some checking here to verify the validity
2980 * of the request, but given the complexity of error
2981 * injection requests, the checking logic would be fairly
2984 * For now, if the request is invalid, it just won't get
2985 * executed and might get deleted.
2987 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2990 * XXX KDM check to make sure the serial number is unique,
2991 * in case we somehow manage to wrap. That shouldn't
2992 * happen for a very long time, but it's the right thing to
2995 new_err_desc->serial = lun->error_serial;
2996 err_desc->serial = lun->error_serial;
2997 lun->error_serial++;
2999 mtx_unlock(&lun->lun_lock);
3002 case CTL_ERROR_INJECT_DELETE: {
3003 struct ctl_error_desc *delete_desc, *desc, *desc2;
3004 struct ctl_lun *lun;
3007 delete_desc = (struct ctl_error_desc *)addr;
3010 mtx_lock(&softc->ctl_lock);
3011 lun = softc->ctl_luns[delete_desc->lun_id];
3013 mtx_unlock(&softc->ctl_lock);
3014 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
3015 __func__, (uintmax_t)delete_desc->lun_id);
3019 mtx_lock(&lun->lun_lock);
3020 mtx_unlock(&softc->ctl_lock);
3021 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
3022 if (desc->serial != delete_desc->serial)
3025 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
3030 mtx_unlock(&lun->lun_lock);
3031 if (delete_done == 0) {
3032 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
3033 "error serial %ju on LUN %u\n", __func__,
3034 delete_desc->serial, delete_desc->lun_id);
3040 case CTL_DUMP_STRUCTS: {
3042 struct ctl_port *port;
3043 struct ctl_frontend *fe;
3045 mtx_lock(&softc->ctl_lock);
3046 printf("CTL Persistent Reservation information start:\n");
3047 for (i = 0; i < CTL_MAX_LUNS; i++) {
3048 struct ctl_lun *lun;
3050 lun = softc->ctl_luns[i];
3053 || ((lun->flags & CTL_LUN_DISABLED) != 0))
3056 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) {
3057 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
3058 idx = j * CTL_MAX_INIT_PER_PORT + k;
3059 if (lun->pr_keys[idx] == 0)
3061 printf(" LUN %d port %d iid %d key "
3063 (uintmax_t)lun->pr_keys[idx]);
3067 printf("CTL Persistent Reservation information end\n");
3068 printf("CTL Ports:\n");
3069 STAILQ_FOREACH(port, &softc->port_list, links) {
3070 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
3071 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
3072 port->frontend->name, port->port_type,
3073 port->physical_port, port->virtual_port,
3074 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
3075 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3076 if (port->wwpn_iid[j].in_use == 0 &&
3077 port->wwpn_iid[j].wwpn == 0 &&
3078 port->wwpn_iid[j].name == NULL)
3081 printf(" iid %u use %d WWPN %#jx '%s'\n",
3082 j, port->wwpn_iid[j].in_use,
3083 (uintmax_t)port->wwpn_iid[j].wwpn,
3084 port->wwpn_iid[j].name);
3087 printf("CTL Port information end\n");
3088 mtx_unlock(&softc->ctl_lock);
3090 * XXX KDM calling this without a lock. We'd likely want
3091 * to drop the lock before calling the frontend's dump
3094 printf("CTL Frontends:\n");
3095 STAILQ_FOREACH(fe, &softc->fe_list, links) {
3096 printf(" Frontend '%s'\n", fe->name);
3097 if (fe->fe_dump != NULL)
3100 printf("CTL Frontend information end\n");
3104 struct ctl_lun_req *lun_req;
3105 struct ctl_backend_driver *backend;
3107 lun_req = (struct ctl_lun_req *)addr;
3109 backend = ctl_backend_find(lun_req->backend);
3110 if (backend == NULL) {
3111 lun_req->status = CTL_LUN_ERROR;
3112 snprintf(lun_req->error_str,
3113 sizeof(lun_req->error_str),
3114 "Backend \"%s\" not found.",
3118 if (lun_req->num_be_args > 0) {
3119 lun_req->kern_be_args = ctl_copyin_args(
3120 lun_req->num_be_args,
3123 sizeof(lun_req->error_str));
3124 if (lun_req->kern_be_args == NULL) {
3125 lun_req->status = CTL_LUN_ERROR;
3130 retval = backend->ioctl(dev, cmd, addr, flag, td);
3132 if (lun_req->num_be_args > 0) {
3133 ctl_copyout_args(lun_req->num_be_args,
3134 lun_req->kern_be_args);
3135 ctl_free_args(lun_req->num_be_args,
3136 lun_req->kern_be_args);
3140 case CTL_LUN_LIST: {
3142 struct ctl_lun *lun;
3143 struct ctl_lun_list *list;
3144 struct ctl_option *opt;
3146 list = (struct ctl_lun_list *)addr;
3149 * Allocate a fixed length sbuf here, based on the length
3150 * of the user's buffer. We could allocate an auto-extending
3151 * buffer, and then tell the user how much larger our
3152 * amount of data is than his buffer, but that presents
3155 * 1. The sbuf(9) routines use a blocking malloc, and so
3156 * we can't hold a lock while calling them with an
3157 * auto-extending buffer.
3159 * 2. There is not currently a LUN reference counting
3160 * mechanism, outside of outstanding transactions on
3161 * the LUN's OOA queue. So a LUN could go away on us
3162 * while we're getting the LUN number, backend-specific
3163 * information, etc. Thus, given the way things
3164 * currently work, we need to hold the CTL lock while
3165 * grabbing LUN information.
3167 * So, from the user's standpoint, the best thing to do is
3168 * allocate what he thinks is a reasonable buffer length,
3169 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3170 * double the buffer length and try again. (And repeat
3171 * that until he succeeds.)
3173 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3175 list->status = CTL_LUN_LIST_ERROR;
3176 snprintf(list->error_str, sizeof(list->error_str),
3177 "Unable to allocate %d bytes for LUN list",
3182 sbuf_printf(sb, "<ctllunlist>\n");
3184 mtx_lock(&softc->ctl_lock);
3185 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3186 mtx_lock(&lun->lun_lock);
3187 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3188 (uintmax_t)lun->lun);
3191 * Bail out as soon as we see that we've overfilled
3197 retval = sbuf_printf(sb, "\t<backend_type>%s"
3198 "</backend_type>\n",
3199 (lun->backend == NULL) ? "none" :
3200 lun->backend->name);
3205 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3206 lun->be_lun->lun_type);
3211 if (lun->backend == NULL) {
3212 retval = sbuf_printf(sb, "</lun>\n");
3218 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3219 (lun->be_lun->maxlba > 0) ?
3220 lun->be_lun->maxlba + 1 : 0);
3225 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3226 lun->be_lun->blocksize);
3231 retval = sbuf_printf(sb, "\t<serial_number>");
3236 retval = ctl_sbuf_printf_esc(sb,
3237 lun->be_lun->serial_num,
3238 sizeof(lun->be_lun->serial_num));
3243 retval = sbuf_printf(sb, "</serial_number>\n");
3248 retval = sbuf_printf(sb, "\t<device_id>");
3253 retval = ctl_sbuf_printf_esc(sb,
3254 lun->be_lun->device_id,
3255 sizeof(lun->be_lun->device_id));
3260 retval = sbuf_printf(sb, "</device_id>\n");
3265 if (lun->backend->lun_info != NULL) {
3266 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3270 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3271 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3272 opt->name, opt->value, opt->name);
3277 retval = sbuf_printf(sb, "</lun>\n");
3281 mtx_unlock(&lun->lun_lock);
3284 mtx_unlock(&lun->lun_lock);
3285 mtx_unlock(&softc->ctl_lock);
3288 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3291 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3292 snprintf(list->error_str, sizeof(list->error_str),
3293 "Out of space, %d bytes is too small",
3300 retval = copyout(sbuf_data(sb), list->lun_xml,
3303 list->fill_len = sbuf_len(sb) + 1;
3304 list->status = CTL_LUN_LIST_OK;
3309 struct ctl_iscsi *ci;
3310 struct ctl_frontend *fe;
3312 ci = (struct ctl_iscsi *)addr;
3314 fe = ctl_frontend_find("iscsi");
3316 ci->status = CTL_ISCSI_ERROR;
3317 snprintf(ci->error_str, sizeof(ci->error_str),
3318 "Frontend \"iscsi\" not found.");
3322 retval = fe->ioctl(dev, cmd, addr, flag, td);
3325 case CTL_PORT_REQ: {
3326 struct ctl_req *req;
3327 struct ctl_frontend *fe;
3329 req = (struct ctl_req *)addr;
3331 fe = ctl_frontend_find(req->driver);
3333 req->status = CTL_LUN_ERROR;
3334 snprintf(req->error_str, sizeof(req->error_str),
3335 "Frontend \"%s\" not found.", req->driver);
3338 if (req->num_args > 0) {
3339 req->kern_args = ctl_copyin_args(req->num_args,
3340 req->args, req->error_str, sizeof(req->error_str));
3341 if (req->kern_args == NULL) {
3342 req->status = CTL_LUN_ERROR;
3347 retval = fe->ioctl(dev, cmd, addr, flag, td);
3349 if (req->num_args > 0) {
3350 ctl_copyout_args(req->num_args, req->kern_args);
3351 ctl_free_args(req->num_args, req->kern_args);
3355 case CTL_PORT_LIST: {
3357 struct ctl_port *port;
3358 struct ctl_lun_list *list;
3359 struct ctl_option *opt;
3362 list = (struct ctl_lun_list *)addr;
3364 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3366 list->status = CTL_LUN_LIST_ERROR;
3367 snprintf(list->error_str, sizeof(list->error_str),
3368 "Unable to allocate %d bytes for LUN list",
3373 sbuf_printf(sb, "<ctlportlist>\n");
3375 mtx_lock(&softc->ctl_lock);
3376 STAILQ_FOREACH(port, &softc->port_list, links) {
3377 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3378 (uintmax_t)port->targ_port);
3381 * Bail out as soon as we see that we've overfilled
3387 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3388 "</frontend_type>\n", port->frontend->name);
3392 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3397 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3398 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3402 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3407 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3408 port->physical_port);
3412 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3413 port->virtual_port);
3417 if (port->target_devid != NULL) {
3418 sbuf_printf(sb, "\t<target>");
3419 ctl_id_sbuf(port->target_devid, sb);
3420 sbuf_printf(sb, "</target>\n");
3423 if (port->port_devid != NULL) {
3424 sbuf_printf(sb, "\t<port>");
3425 ctl_id_sbuf(port->port_devid, sb);
3426 sbuf_printf(sb, "</port>\n");
3429 if (port->port_info != NULL) {
3430 retval = port->port_info(port->onoff_arg, sb);
3434 STAILQ_FOREACH(opt, &port->options, links) {
3435 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3436 opt->name, opt->value, opt->name);
3441 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3442 if (port->wwpn_iid[j].in_use == 0 ||
3443 (port->wwpn_iid[j].wwpn == 0 &&
3444 port->wwpn_iid[j].name == NULL))
3447 if (port->wwpn_iid[j].name != NULL)
3448 retval = sbuf_printf(sb,
3449 "\t<initiator>%u %s</initiator>\n",
3450 j, port->wwpn_iid[j].name);
3452 retval = sbuf_printf(sb,
3453 "\t<initiator>%u naa.%08jx</initiator>\n",
3454 j, port->wwpn_iid[j].wwpn);
3461 retval = sbuf_printf(sb, "</targ_port>\n");
3465 mtx_unlock(&softc->ctl_lock);
3468 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3471 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3472 snprintf(list->error_str, sizeof(list->error_str),
3473 "Out of space, %d bytes is too small",
3480 retval = copyout(sbuf_data(sb), list->lun_xml,
3483 list->fill_len = sbuf_len(sb) + 1;
3484 list->status = CTL_LUN_LIST_OK;
3489 /* XXX KDM should we fix this? */
3491 struct ctl_backend_driver *backend;
3498 * We encode the backend type as the ioctl type for backend
3499 * ioctls. So parse it out here, and then search for a
3500 * backend of this type.
3502 type = _IOC_TYPE(cmd);
3504 STAILQ_FOREACH(backend, &softc->be_list, links) {
3505 if (backend->type == type) {
3511 printf("ctl: unknown ioctl command %#lx or backend "
3516 retval = backend->ioctl(dev, cmd, addr, flag, td);
3526 ctl_get_initindex(struct ctl_nexus *nexus)
3528 if (nexus->targ_port < CTL_MAX_PORTS)
3529 return (nexus->initid.id +
3530 (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3532 return (nexus->initid.id +
3533 ((nexus->targ_port - CTL_MAX_PORTS) *
3534 CTL_MAX_INIT_PER_PORT));
3538 ctl_get_resindex(struct ctl_nexus *nexus)
3540 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3544 ctl_port_idx(int port_num)
3546 if (port_num < CTL_MAX_PORTS)
3549 return(port_num - CTL_MAX_PORTS);
3553 ctl_map_lun(int port_num, uint32_t lun_id)
3555 struct ctl_port *port;
3557 port = control_softc->ctl_ports[ctl_port_idx(port_num)];
3559 return (UINT32_MAX);
3560 if (port->lun_map == NULL)
3562 return (port->lun_map(port->targ_lun_arg, lun_id));
3566 ctl_map_lun_back(int port_num, uint32_t lun_id)
3568 struct ctl_port *port;
3571 port = control_softc->ctl_ports[ctl_port_idx(port_num)];
3572 if (port->lun_map == NULL)
3574 for (i = 0; i < CTL_MAX_LUNS; i++) {
3575 if (port->lun_map(port->targ_lun_arg, i) == lun_id)
3578 return (UINT32_MAX);
3582 * Note: This only works for bitmask sizes that are at least 32 bits, and
3583 * that are a power of 2.
3586 ctl_ffz(uint32_t *mask, uint32_t size)
3588 uint32_t num_chunks, num_pieces;
3591 num_chunks = (size >> 5);
3592 if (num_chunks == 0)
3594 num_pieces = ctl_min((sizeof(uint32_t) * 8), size);
3596 for (i = 0; i < num_chunks; i++) {
3597 for (j = 0; j < num_pieces; j++) {
3598 if ((mask[i] & (1 << j)) == 0)
3599 return ((i << 5) + j);
3607 ctl_set_mask(uint32_t *mask, uint32_t bit)
3609 uint32_t chunk, piece;
3612 piece = bit % (sizeof(uint32_t) * 8);
3614 if ((mask[chunk] & (1 << piece)) != 0)
3617 mask[chunk] |= (1 << piece);
3623 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3625 uint32_t chunk, piece;
3628 piece = bit % (sizeof(uint32_t) * 8);
3630 if ((mask[chunk] & (1 << piece)) == 0)
3633 mask[chunk] &= ~(1 << piece);
3639 ctl_is_set(uint32_t *mask, uint32_t bit)
3641 uint32_t chunk, piece;
3644 piece = bit % (sizeof(uint32_t) * 8);
3646 if ((mask[chunk] & (1 << piece)) == 0)
3654 * The bus, target and lun are optional, they can be filled in later.
3655 * can_wait is used to determine whether we can wait on the malloc or not.
3658 ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target,
3659 uint32_t targ_lun, int can_wait)
3664 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK);
3666 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT);
3669 io->io_hdr.io_type = io_type;
3670 io->io_hdr.targ_port = targ_port;
3672 * XXX KDM this needs to change/go away. We need to move
3673 * to a preallocated pool of ctl_scsiio structures.
3675 io->io_hdr.nexus.targ_target.id = targ_target;
3676 io->io_hdr.nexus.targ_lun = targ_lun;
3683 ctl_kfree_io(union ctl_io *io)
3690 * ctl_softc, pool_name, total_ctl_io are passed in.
3691 * npool is passed out.
3694 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3695 uint32_t total_ctl_io, void **npool)
3698 struct ctl_io_pool *pool;
3700 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3705 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3706 pool->ctl_softc = ctl_softc;
3707 pool->zone = uma_zsecond_create(pool->name, NULL,
3708 NULL, NULL, NULL, ctl_softc->io_zone);
3709 /* uma_prealloc(pool->zone, total_ctl_io); */
3713 *npool = ctl_softc->io_zone;
3719 ctl_pool_free(struct ctl_io_pool *pool)
3726 uma_zdestroy(pool->zone);
3732 ctl_alloc_io(void *pool_ref)
3736 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3738 io = uma_zalloc(pool->zone, M_WAITOK);
3740 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK);
3743 io->io_hdr.pool = pool_ref;
3748 ctl_alloc_io_nowait(void *pool_ref)
3752 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3754 io = uma_zalloc(pool->zone, M_NOWAIT);
3756 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT);
3759 io->io_hdr.pool = pool_ref;
3764 ctl_free_io(union ctl_io *io)
3767 struct ctl_io_pool *pool;
3774 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3775 uma_zfree(pool->zone, io);
3777 uma_zfree((uma_zone_t)io->io_hdr.pool, io);
3782 ctl_zero_io(union ctl_io *io)
3790 * May need to preserve linked list pointers at some point too.
3792 pool_ref = io->io_hdr.pool;
3793 memset(io, 0, sizeof(*io));
3794 io->io_hdr.pool = pool_ref;
3798 * This routine is currently used for internal copies of ctl_ios that need
3799 * to persist for some reason after we've already returned status to the
3800 * FETD. (Thus the flag set.)
3803 * Note that this makes a blind copy of all fields in the ctl_io, except
3804 * for the pool reference. This includes any memory that has been
3805 * allocated! That memory will no longer be valid after done has been
3806 * called, so this would be VERY DANGEROUS for command that actually does
3807 * any reads or writes. Right now (11/7/2005), this is only used for immediate
3808 * start and stop commands, which don't transfer any data, so this is not a
3809 * problem. If it is used for anything else, the caller would also need to
3810 * allocate data buffer space and this routine would need to be modified to
3811 * copy the data buffer(s) as well.
3814 ctl_copy_io(union ctl_io *src, union ctl_io *dest)
3823 * May need to preserve linked list pointers at some point too.
3825 pool_ref = dest->io_hdr.pool;
3827 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest)));
3829 dest->io_hdr.pool = pool_ref;
3831 * We need to know that this is an internal copy, and doesn't need
3832 * to get passed back to the FETD that allocated it.
3834 dest->io_hdr.flags |= CTL_FLAG_INT_COPY;
3838 ctl_expand_number(const char *buf, uint64_t *num)
3844 number = strtoq(buf, &endptr, 0);
3846 switch (tolower((unsigned char)*endptr)) {
3866 case '\0': /* No unit. */
3870 /* Unrecognized unit. */
3874 if ((number << shift) >> shift != number) {
3878 *num = number << shift;
3884 * This routine could be used in the future to load default and/or saved
3885 * mode page parameters for a particuar lun.
3888 ctl_init_page_index(struct ctl_lun *lun)
3891 struct ctl_page_index *page_index;
3895 memcpy(&lun->mode_pages.index, page_index_template,
3896 sizeof(page_index_template));
3898 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3900 page_index = &lun->mode_pages.index[i];
3902 * If this is a disk-only mode page, there's no point in
3903 * setting it up. For some pages, we have to have some
3904 * basic information about the disk in order to calculate the
3907 if ((lun->be_lun->lun_type != T_DIRECT)
3908 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
3911 switch (page_index->page_code & SMPH_PC_MASK) {
3912 case SMS_RW_ERROR_RECOVERY_PAGE: {
3913 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3914 panic("subpage is incorrect!");
3915 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
3916 &rw_er_page_default,
3917 sizeof(rw_er_page_default));
3918 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
3919 &rw_er_page_changeable,
3920 sizeof(rw_er_page_changeable));
3921 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
3922 &rw_er_page_default,
3923 sizeof(rw_er_page_default));
3924 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
3925 &rw_er_page_default,
3926 sizeof(rw_er_page_default));
3927 page_index->page_data =
3928 (uint8_t *)lun->mode_pages.rw_er_page;
3931 case SMS_FORMAT_DEVICE_PAGE: {
3932 struct scsi_format_page *format_page;
3934 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3935 panic("subpage is incorrect!");
3938 * Sectors per track are set above. Bytes per
3939 * sector need to be set here on a per-LUN basis.
3941 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
3942 &format_page_default,
3943 sizeof(format_page_default));
3944 memcpy(&lun->mode_pages.format_page[
3945 CTL_PAGE_CHANGEABLE], &format_page_changeable,
3946 sizeof(format_page_changeable));
3947 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
3948 &format_page_default,
3949 sizeof(format_page_default));
3950 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
3951 &format_page_default,
3952 sizeof(format_page_default));
3954 format_page = &lun->mode_pages.format_page[
3956 scsi_ulto2b(lun->be_lun->blocksize,
3957 format_page->bytes_per_sector);
3959 format_page = &lun->mode_pages.format_page[
3961 scsi_ulto2b(lun->be_lun->blocksize,
3962 format_page->bytes_per_sector);
3964 format_page = &lun->mode_pages.format_page[
3966 scsi_ulto2b(lun->be_lun->blocksize,
3967 format_page->bytes_per_sector);
3969 page_index->page_data =
3970 (uint8_t *)lun->mode_pages.format_page;
3973 case SMS_RIGID_DISK_PAGE: {
3974 struct scsi_rigid_disk_page *rigid_disk_page;
3975 uint32_t sectors_per_cylinder;
3979 #endif /* !__XSCALE__ */
3981 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3982 panic("invalid subpage value %d",
3983 page_index->subpage);
3986 * Rotation rate and sectors per track are set
3987 * above. We calculate the cylinders here based on
3988 * capacity. Due to the number of heads and
3989 * sectors per track we're using, smaller arrays
3990 * may turn out to have 0 cylinders. Linux and
3991 * FreeBSD don't pay attention to these mode pages
3992 * to figure out capacity, but Solaris does. It
3993 * seems to deal with 0 cylinders just fine, and
3994 * works out a fake geometry based on the capacity.
3996 memcpy(&lun->mode_pages.rigid_disk_page[
3997 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
3998 sizeof(rigid_disk_page_default));
3999 memcpy(&lun->mode_pages.rigid_disk_page[
4000 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4001 sizeof(rigid_disk_page_changeable));
4003 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4007 * The divide method here will be more accurate,
4008 * probably, but results in floating point being
4009 * used in the kernel on i386 (__udivdi3()). On the
4010 * XScale, though, __udivdi3() is implemented in
4013 * The shift method for cylinder calculation is
4014 * accurate if sectors_per_cylinder is a power of
4015 * 2. Otherwise it might be slightly off -- you
4016 * might have a bit of a truncation problem.
4019 cylinders = (lun->be_lun->maxlba + 1) /
4020 sectors_per_cylinder;
4022 for (shift = 31; shift > 0; shift--) {
4023 if (sectors_per_cylinder & (1 << shift))
4026 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4030 * We've basically got 3 bytes, or 24 bits for the
4031 * cylinder size in the mode page. If we're over,
4032 * just round down to 2^24.
4034 if (cylinders > 0xffffff)
4035 cylinders = 0xffffff;
4037 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4039 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4041 if ((value = ctl_get_opt(&lun->be_lun->options,
4043 scsi_ulto2b(strtol(value, NULL, 0),
4044 rigid_disk_page->rotation_rate);
4047 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
4048 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4049 sizeof(rigid_disk_page_default));
4050 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
4051 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4052 sizeof(rigid_disk_page_default));
4054 page_index->page_data =
4055 (uint8_t *)lun->mode_pages.rigid_disk_page;
4058 case SMS_CACHING_PAGE: {
4059 struct scsi_caching_page *caching_page;
4061 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4062 panic("invalid subpage value %d",
4063 page_index->subpage);
4064 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4065 &caching_page_default,
4066 sizeof(caching_page_default));
4067 memcpy(&lun->mode_pages.caching_page[
4068 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4069 sizeof(caching_page_changeable));
4070 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4071 &caching_page_default,
4072 sizeof(caching_page_default));
4073 caching_page = &lun->mode_pages.caching_page[
4075 value = ctl_get_opt(&lun->be_lun->options, "writecache");
4076 if (value != NULL && strcmp(value, "off") == 0)
4077 caching_page->flags1 &= ~SCP_WCE;
4078 value = ctl_get_opt(&lun->be_lun->options, "readcache");
4079 if (value != NULL && strcmp(value, "off") == 0)
4080 caching_page->flags1 |= SCP_RCD;
4081 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4082 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4083 sizeof(caching_page_default));
4084 page_index->page_data =
4085 (uint8_t *)lun->mode_pages.caching_page;
4088 case SMS_CONTROL_MODE_PAGE: {
4089 struct scsi_control_page *control_page;
4091 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4092 panic("invalid subpage value %d",
4093 page_index->subpage);
4095 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT],
4096 &control_page_default,
4097 sizeof(control_page_default));
4098 memcpy(&lun->mode_pages.control_page[
4099 CTL_PAGE_CHANGEABLE], &control_page_changeable,
4100 sizeof(control_page_changeable));
4101 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED],
4102 &control_page_default,
4103 sizeof(control_page_default));
4104 control_page = &lun->mode_pages.control_page[
4106 value = ctl_get_opt(&lun->be_lun->options, "reordering");
4107 if (value != NULL && strcmp(value, "unrestricted") == 0) {
4108 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK;
4109 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED;
4111 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT],
4112 &lun->mode_pages.control_page[CTL_PAGE_SAVED],
4113 sizeof(control_page_default));
4114 page_index->page_data =
4115 (uint8_t *)lun->mode_pages.control_page;
4119 case SMS_INFO_EXCEPTIONS_PAGE: {
4120 switch (page_index->subpage) {
4121 case SMS_SUBPAGE_PAGE_0:
4122 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4124 sizeof(ie_page_default));
4125 memcpy(&lun->mode_pages.ie_page[
4126 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4127 sizeof(ie_page_changeable));
4128 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4130 sizeof(ie_page_default));
4131 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4133 sizeof(ie_page_default));
4134 page_index->page_data =
4135 (uint8_t *)lun->mode_pages.ie_page;
4138 struct ctl_logical_block_provisioning_page *page;
4140 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4142 sizeof(lbp_page_default));
4143 memcpy(&lun->mode_pages.lbp_page[
4144 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4145 sizeof(lbp_page_changeable));
4146 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4148 sizeof(lbp_page_default));
4149 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4150 value = ctl_get_opt(&lun->be_lun->options,
4152 if (value != NULL &&
4153 ctl_expand_number(value, &ival) == 0) {
4154 page->descr[0].flags |= SLBPPD_ENABLED |
4156 if (lun->be_lun->blocksize)
4157 ival /= lun->be_lun->blocksize;
4160 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4161 page->descr[0].count);
4163 value = ctl_get_opt(&lun->be_lun->options,
4165 if (value != NULL &&
4166 ctl_expand_number(value, &ival) == 0) {
4167 page->descr[1].flags |= SLBPPD_ENABLED |
4169 if (lun->be_lun->blocksize)
4170 ival /= lun->be_lun->blocksize;
4173 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4174 page->descr[1].count);
4176 value = ctl_get_opt(&lun->be_lun->options,
4177 "pool-avail-threshold");
4178 if (value != NULL &&
4179 ctl_expand_number(value, &ival) == 0) {
4180 page->descr[2].flags |= SLBPPD_ENABLED |
4182 if (lun->be_lun->blocksize)
4183 ival /= lun->be_lun->blocksize;
4186 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4187 page->descr[2].count);
4189 value = ctl_get_opt(&lun->be_lun->options,
4190 "pool-used-threshold");
4191 if (value != NULL &&
4192 ctl_expand_number(value, &ival) == 0) {
4193 page->descr[3].flags |= SLBPPD_ENABLED |
4195 if (lun->be_lun->blocksize)
4196 ival /= lun->be_lun->blocksize;
4199 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4200 page->descr[3].count);
4202 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4203 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4204 sizeof(lbp_page_default));
4205 page_index->page_data =
4206 (uint8_t *)lun->mode_pages.lbp_page;
4210 case SMS_VENDOR_SPECIFIC_PAGE:{
4211 switch (page_index->subpage) {
4212 case DBGCNF_SUBPAGE_CODE: {
4213 struct copan_debugconf_subpage *current_page,
4216 memcpy(&lun->mode_pages.debugconf_subpage[
4218 &debugconf_page_default,
4219 sizeof(debugconf_page_default));
4220 memcpy(&lun->mode_pages.debugconf_subpage[
4221 CTL_PAGE_CHANGEABLE],
4222 &debugconf_page_changeable,
4223 sizeof(debugconf_page_changeable));
4224 memcpy(&lun->mode_pages.debugconf_subpage[
4226 &debugconf_page_default,
4227 sizeof(debugconf_page_default));
4228 memcpy(&lun->mode_pages.debugconf_subpage[
4230 &debugconf_page_default,
4231 sizeof(debugconf_page_default));
4232 page_index->page_data =
4233 (uint8_t *)lun->mode_pages.debugconf_subpage;
4235 current_page = (struct copan_debugconf_subpage *)
4236 (page_index->page_data +
4237 (page_index->page_len *
4239 saved_page = (struct copan_debugconf_subpage *)
4240 (page_index->page_data +
4241 (page_index->page_len *
4246 panic("invalid subpage value %d",
4247 page_index->subpage);
4253 panic("invalid page value %d",
4254 page_index->page_code & SMPH_PC_MASK);
4259 return (CTL_RETVAL_COMPLETE);
4263 ctl_init_log_page_index(struct ctl_lun *lun)
4265 struct ctl_page_index *page_index;
4268 memcpy(&lun->log_pages.index, log_page_index_template,
4269 sizeof(log_page_index_template));
4272 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4274 page_index = &lun->log_pages.index[i];
4276 * If this is a disk-only mode page, there's no point in
4277 * setting it up. For some pages, we have to have some
4278 * basic information about the disk in order to calculate the
4281 if ((lun->be_lun->lun_type != T_DIRECT)
4282 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
4285 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4286 ((lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) == 0 ||
4287 lun->backend->lun_attr == NULL))
4290 if (page_index->page_code != prev) {
4291 lun->log_pages.pages_page[j] = page_index->page_code;
4292 prev = page_index->page_code;
4295 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4296 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4299 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4300 lun->log_pages.index[0].page_len = j;
4301 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4302 lun->log_pages.index[1].page_len = k * 2;
4303 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4304 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4306 return (CTL_RETVAL_COMPLETE);
4310 hex2bin(const char *str, uint8_t *buf, int buf_size)
4315 memset(buf, 0, buf_size);
4316 while (isspace(str[0]))
4318 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4321 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4325 else if (isalpha(c))
4326 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4332 buf[i / 2] |= (c << 4);
4336 return ((i + 1) / 2);
4343 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4344 * wants us to allocate the LUN and he can block.
4345 * - ctl_softc is always set
4346 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4348 * Returns 0 for success, non-zero (errno) for failure.
4351 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4352 struct ctl_be_lun *const be_lun, struct ctl_id target_id)
4354 struct ctl_lun *nlun, *lun;
4355 struct ctl_port *port;
4356 struct scsi_vpd_id_descriptor *desc;
4357 struct scsi_vpd_id_t10 *t10id;
4358 const char *eui, *naa, *scsiname, *vendor, *value;
4359 int lun_number, i, lun_malloced;
4360 int devidlen, idlen1, idlen2 = 0, len;
4366 * We currently only support Direct Access or Processor LUN types.
4368 switch (be_lun->lun_type) {
4376 be_lun->lun_config_status(be_lun->be_lun,
4377 CTL_LUN_CONFIG_FAILURE);
4380 if (ctl_lun == NULL) {
4381 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4388 memset(lun, 0, sizeof(*lun));
4390 lun->flags = CTL_LUN_MALLOCED;
4392 /* Generate LUN ID. */
4393 devidlen = max(CTL_DEVID_MIN_LEN,
4394 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4395 idlen1 = sizeof(*t10id) + devidlen;
4396 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4397 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4398 if (scsiname != NULL) {
4399 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4400 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4402 eui = ctl_get_opt(&be_lun->options, "eui");
4404 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4406 naa = ctl_get_opt(&be_lun->options, "naa");
4408 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4410 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4411 M_CTL, M_WAITOK | M_ZERO);
4412 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4413 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4414 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4415 desc->length = idlen1;
4416 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4417 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4418 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4419 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4421 strncpy(t10id->vendor, vendor,
4422 min(sizeof(t10id->vendor), strlen(vendor)));
4424 strncpy((char *)t10id->vendor_spec_id,
4425 (char *)be_lun->device_id, devidlen);
4426 if (scsiname != NULL) {
4427 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4429 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4430 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4431 SVPD_ID_TYPE_SCSI_NAME;
4432 desc->length = idlen2;
4433 strlcpy(desc->identifier, scsiname, idlen2);
4436 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4438 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4439 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4441 desc->length = hex2bin(eui, desc->identifier, 16);
4442 desc->length = desc->length > 12 ? 16 :
4443 (desc->length > 8 ? 12 : 8);
4444 len -= 16 - desc->length;
4447 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4449 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4450 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4452 desc->length = hex2bin(naa, desc->identifier, 16);
4453 desc->length = desc->length > 8 ? 16 : 8;
4454 len -= 16 - desc->length;
4456 lun->lun_devid->len = len;
4458 mtx_lock(&ctl_softc->ctl_lock);
4460 * See if the caller requested a particular LUN number. If so, see
4461 * if it is available. Otherwise, allocate the first available LUN.
4463 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4464 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4465 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4466 mtx_unlock(&ctl_softc->ctl_lock);
4467 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4468 printf("ctl: requested LUN ID %d is higher "
4469 "than CTL_MAX_LUNS - 1 (%d)\n",
4470 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4473 * XXX KDM return an error, or just assign
4474 * another LUN ID in this case??
4476 printf("ctl: requested LUN ID %d is already "
4477 "in use\n", be_lun->req_lun_id);
4479 if (lun->flags & CTL_LUN_MALLOCED)
4481 be_lun->lun_config_status(be_lun->be_lun,
4482 CTL_LUN_CONFIG_FAILURE);
4485 lun_number = be_lun->req_lun_id;
4487 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS);
4488 if (lun_number == -1) {
4489 mtx_unlock(&ctl_softc->ctl_lock);
4490 printf("ctl: can't allocate LUN on target %ju, out of "
4491 "LUNs\n", (uintmax_t)target_id.id);
4492 if (lun->flags & CTL_LUN_MALLOCED)
4494 be_lun->lun_config_status(be_lun->be_lun,
4495 CTL_LUN_CONFIG_FAILURE);
4499 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4501 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4502 lun->target = target_id;
4503 lun->lun = lun_number;
4504 lun->be_lun = be_lun;
4506 * The processor LUN is always enabled. Disk LUNs come on line
4507 * disabled, and must be enabled by the backend.
4509 lun->flags |= CTL_LUN_DISABLED;
4510 lun->backend = be_lun->be;
4511 be_lun->ctl_lun = lun;
4512 be_lun->lun_id = lun_number;
4513 atomic_add_int(&be_lun->be->num_luns, 1);
4514 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE)
4515 lun->flags |= CTL_LUN_OFFLINE;
4517 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4518 lun->flags |= CTL_LUN_STOPPED;
4520 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4521 lun->flags |= CTL_LUN_INOPERABLE;
4523 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4524 lun->flags |= CTL_LUN_PRIMARY_SC;
4526 value = ctl_get_opt(&be_lun->options, "readonly");
4527 if (value != NULL && strcmp(value, "on") == 0)
4528 lun->flags |= CTL_LUN_READONLY;
4530 lun->ctl_softc = ctl_softc;
4531 TAILQ_INIT(&lun->ooa_queue);
4532 TAILQ_INIT(&lun->blocked_queue);
4533 STAILQ_INIT(&lun->error_list);
4534 ctl_tpc_lun_init(lun);
4537 * Initialize the mode and log page index.
4539 ctl_init_page_index(lun);
4540 ctl_init_log_page_index(lun);
4543 * Set the poweron UA for all initiators on this LUN only.
4545 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4546 lun->pending_ua[i] = CTL_UA_POWERON;
4549 * Now, before we insert this lun on the lun list, set the lun
4550 * inventory changed UA for all other luns.
4552 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4553 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4554 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE;
4558 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4560 ctl_softc->ctl_luns[lun_number] = lun;
4562 ctl_softc->num_luns++;
4564 /* Setup statistics gathering */
4565 lun->stats.device_type = be_lun->lun_type;
4566 lun->stats.lun_number = lun_number;
4567 if (lun->stats.device_type == T_DIRECT)
4568 lun->stats.blocksize = be_lun->blocksize;
4570 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4571 for (i = 0;i < CTL_MAX_PORTS;i++)
4572 lun->stats.ports[i].targ_port = i;
4574 mtx_unlock(&ctl_softc->ctl_lock);
4576 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4579 * Run through each registered FETD and bring it online if it isn't
4580 * already. Enable the target ID if it hasn't been enabled, and
4581 * enable this particular LUN.
4583 STAILQ_FOREACH(port, &ctl_softc->port_list, links) {
4586 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number);
4588 printf("ctl_alloc_lun: FETD %s port %d returned error "
4589 "%d for lun_enable on target %ju lun %d\n",
4590 port->port_name, port->targ_port, retval,
4591 (uintmax_t)target_id.id, lun_number);
4593 port->status |= CTL_PORT_STATUS_LUN_ONLINE;
4601 * - LUN has already been marked invalid and any pending I/O has been taken
4605 ctl_free_lun(struct ctl_lun *lun)
4607 struct ctl_softc *softc;
4609 struct ctl_port *port;
4611 struct ctl_lun *nlun;
4614 softc = lun->ctl_softc;
4616 mtx_assert(&softc->ctl_lock, MA_OWNED);
4618 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4620 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4622 softc->ctl_luns[lun->lun] = NULL;
4624 if (!TAILQ_EMPTY(&lun->ooa_queue))
4625 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4630 * XXX KDM this scheme only works for a single target/multiple LUN
4631 * setup. It needs to be revamped for a multiple target scheme.
4633 * XXX KDM this results in port->lun_disable() getting called twice,
4634 * once when ctl_disable_lun() is called, and a second time here.
4635 * We really need to re-think the LUN disable semantics. There
4636 * should probably be several steps/levels to LUN removal:
4641 * Right now we only have a disable method when communicating to
4642 * the front end ports, at least for individual LUNs.
4645 STAILQ_FOREACH(port, &softc->port_list, links) {
4648 retval = port->lun_disable(port->targ_lun_arg, lun->target,
4651 printf("ctl_free_lun: FETD %s port %d returned error "
4652 "%d for lun_disable on target %ju lun %jd\n",
4653 port->port_name, port->targ_port, retval,
4654 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4657 if (STAILQ_FIRST(&softc->lun_list) == NULL) {
4658 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE;
4660 retval = port->targ_disable(port->targ_lun_arg,lun->target);
4662 printf("ctl_free_lun: FETD %s port %d "
4663 "returned error %d for targ_disable on "
4664 "target %ju\n", port->port_name,
4665 port->targ_port, retval,
4666 (uintmax_t)lun->target.id);
4668 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE;
4670 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0)
4674 port->port_offline(port->onoff_arg);
4675 port->status &= ~CTL_PORT_STATUS_ONLINE;
4682 * Tell the backend to free resources, if this LUN has a backend.
4684 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4685 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4687 ctl_tpc_lun_shutdown(lun);
4688 mtx_destroy(&lun->lun_lock);
4689 free(lun->lun_devid, M_CTL);
4690 free(lun->write_buffer, M_CTL);
4691 if (lun->flags & CTL_LUN_MALLOCED)
4694 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4695 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4696 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE;
4704 ctl_create_lun(struct ctl_be_lun *be_lun)
4706 struct ctl_softc *ctl_softc;
4708 ctl_softc = control_softc;
4711 * ctl_alloc_lun() should handle all potential failure cases.
4713 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target);
4717 ctl_add_lun(struct ctl_be_lun *be_lun)
4719 struct ctl_softc *ctl_softc = control_softc;
4721 mtx_lock(&ctl_softc->ctl_lock);
4722 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links);
4723 mtx_unlock(&ctl_softc->ctl_lock);
4724 wakeup(&ctl_softc->pending_lun_queue);
4730 ctl_enable_lun(struct ctl_be_lun *be_lun)
4732 struct ctl_softc *ctl_softc;
4733 struct ctl_port *port, *nport;
4734 struct ctl_lun *lun;
4737 ctl_softc = control_softc;
4739 lun = (struct ctl_lun *)be_lun->ctl_lun;
4741 mtx_lock(&ctl_softc->ctl_lock);
4742 mtx_lock(&lun->lun_lock);
4743 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4745 * eh? Why did we get called if the LUN is already
4748 mtx_unlock(&lun->lun_lock);
4749 mtx_unlock(&ctl_softc->ctl_lock);
4752 lun->flags &= ~CTL_LUN_DISABLED;
4753 mtx_unlock(&lun->lun_lock);
4755 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) {
4756 nport = STAILQ_NEXT(port, links);
4759 * Drop the lock while we call the FETD's enable routine.
4760 * This can lead to a callback into CTL (at least in the
4761 * case of the internal initiator frontend.
4763 mtx_unlock(&ctl_softc->ctl_lock);
4764 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun);
4765 mtx_lock(&ctl_softc->ctl_lock);
4767 printf("%s: FETD %s port %d returned error "
4768 "%d for lun_enable on target %ju lun %jd\n",
4769 __func__, port->port_name, port->targ_port, retval,
4770 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4774 /* NOTE: TODO: why does lun enable affect port status? */
4775 port->status |= CTL_PORT_STATUS_LUN_ONLINE;
4780 mtx_unlock(&ctl_softc->ctl_lock);
4786 ctl_disable_lun(struct ctl_be_lun *be_lun)
4788 struct ctl_softc *ctl_softc;
4789 struct ctl_port *port;
4790 struct ctl_lun *lun;
4793 ctl_softc = control_softc;
4795 lun = (struct ctl_lun *)be_lun->ctl_lun;
4797 mtx_lock(&ctl_softc->ctl_lock);
4798 mtx_lock(&lun->lun_lock);
4799 if (lun->flags & CTL_LUN_DISABLED) {
4800 mtx_unlock(&lun->lun_lock);
4801 mtx_unlock(&ctl_softc->ctl_lock);
4804 lun->flags |= CTL_LUN_DISABLED;
4805 mtx_unlock(&lun->lun_lock);
4807 STAILQ_FOREACH(port, &ctl_softc->port_list, links) {
4808 mtx_unlock(&ctl_softc->ctl_lock);
4810 * Drop the lock before we call the frontend's disable
4811 * routine, to avoid lock order reversals.
4813 * XXX KDM what happens if the frontend list changes while
4814 * we're traversing it? It's unlikely, but should be handled.
4816 retval = port->lun_disable(port->targ_lun_arg, lun->target,
4818 mtx_lock(&ctl_softc->ctl_lock);
4820 printf("ctl_alloc_lun: FETD %s port %d returned error "
4821 "%d for lun_disable on target %ju lun %jd\n",
4822 port->port_name, port->targ_port, retval,
4823 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4827 mtx_unlock(&ctl_softc->ctl_lock);
4833 ctl_start_lun(struct ctl_be_lun *be_lun)
4835 struct ctl_softc *ctl_softc;
4836 struct ctl_lun *lun;
4838 ctl_softc = control_softc;
4840 lun = (struct ctl_lun *)be_lun->ctl_lun;
4842 mtx_lock(&lun->lun_lock);
4843 lun->flags &= ~CTL_LUN_STOPPED;
4844 mtx_unlock(&lun->lun_lock);
4850 ctl_stop_lun(struct ctl_be_lun *be_lun)
4852 struct ctl_softc *ctl_softc;
4853 struct ctl_lun *lun;
4855 ctl_softc = control_softc;
4857 lun = (struct ctl_lun *)be_lun->ctl_lun;
4859 mtx_lock(&lun->lun_lock);
4860 lun->flags |= CTL_LUN_STOPPED;
4861 mtx_unlock(&lun->lun_lock);
4867 ctl_lun_offline(struct ctl_be_lun *be_lun)
4869 struct ctl_softc *ctl_softc;
4870 struct ctl_lun *lun;
4872 ctl_softc = control_softc;
4874 lun = (struct ctl_lun *)be_lun->ctl_lun;
4876 mtx_lock(&lun->lun_lock);
4877 lun->flags |= CTL_LUN_OFFLINE;
4878 mtx_unlock(&lun->lun_lock);
4884 ctl_lun_online(struct ctl_be_lun *be_lun)
4886 struct ctl_softc *ctl_softc;
4887 struct ctl_lun *lun;
4889 ctl_softc = control_softc;
4891 lun = (struct ctl_lun *)be_lun->ctl_lun;
4893 mtx_lock(&lun->lun_lock);
4894 lun->flags &= ~CTL_LUN_OFFLINE;
4895 mtx_unlock(&lun->lun_lock);
4901 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4903 struct ctl_softc *ctl_softc;
4904 struct ctl_lun *lun;
4906 ctl_softc = control_softc;
4908 lun = (struct ctl_lun *)be_lun->ctl_lun;
4910 mtx_lock(&lun->lun_lock);
4913 * The LUN needs to be disabled before it can be marked invalid.
4915 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4916 mtx_unlock(&lun->lun_lock);
4920 * Mark the LUN invalid.
4922 lun->flags |= CTL_LUN_INVALID;
4925 * If there is nothing in the OOA queue, go ahead and free the LUN.
4926 * If we have something in the OOA queue, we'll free it when the
4927 * last I/O completes.
4929 if (TAILQ_EMPTY(&lun->ooa_queue)) {
4930 mtx_unlock(&lun->lun_lock);
4931 mtx_lock(&ctl_softc->ctl_lock);
4933 mtx_unlock(&ctl_softc->ctl_lock);
4935 mtx_unlock(&lun->lun_lock);
4941 ctl_lun_inoperable(struct ctl_be_lun *be_lun)
4943 struct ctl_softc *ctl_softc;
4944 struct ctl_lun *lun;
4946 ctl_softc = control_softc;
4947 lun = (struct ctl_lun *)be_lun->ctl_lun;
4949 mtx_lock(&lun->lun_lock);
4950 lun->flags |= CTL_LUN_INOPERABLE;
4951 mtx_unlock(&lun->lun_lock);
4957 ctl_lun_operable(struct ctl_be_lun *be_lun)
4959 struct ctl_softc *ctl_softc;
4960 struct ctl_lun *lun;
4962 ctl_softc = control_softc;
4963 lun = (struct ctl_lun *)be_lun->ctl_lun;
4965 mtx_lock(&lun->lun_lock);
4966 lun->flags &= ~CTL_LUN_INOPERABLE;
4967 mtx_unlock(&lun->lun_lock);
4973 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
4975 struct ctl_lun *lun;
4976 struct ctl_softc *softc;
4979 softc = control_softc;
4981 lun = (struct ctl_lun *)be_lun->ctl_lun;
4983 mtx_lock(&lun->lun_lock);
4985 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4986 lun->pending_ua[i] |= CTL_UA_CAPACITY_CHANGED;
4988 mtx_unlock(&lun->lun_lock);
4992 * Backend "memory move is complete" callback for requests that never
4993 * make it down to say RAIDCore's configuration code.
4996 ctl_config_move_done(union ctl_io *io)
5000 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
5001 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
5002 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
5004 if ((io->io_hdr.port_status != 0) &&
5005 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5006 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5008 * For hardware error sense keys, the sense key
5009 * specific value is defined to be a retry count,
5010 * but we use it to pass back an internal FETD
5011 * error code. XXX KDM Hopefully the FETD is only
5012 * using 16 bits for an error code, since that's
5013 * all the space we have in the sks field.
5015 ctl_set_internal_failure(&io->scsiio,
5018 io->io_hdr.port_status);
5021 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
5022 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5023 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
5024 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5026 * XXX KDM just assuming a single pointer here, and not a
5027 * S/G list. If we start using S/G lists for config data,
5028 * we'll need to know how to clean them up here as well.
5030 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5031 free(io->scsiio.kern_data_ptr, M_CTL);
5033 retval = CTL_RETVAL_COMPLETE;
5036 * XXX KDM now we need to continue data movement. Some
5038 * - call ctl_scsiio() again? We don't do this for data
5039 * writes, because for those at least we know ahead of
5040 * time where the write will go and how long it is. For
5041 * config writes, though, that information is largely
5042 * contained within the write itself, thus we need to
5043 * parse out the data again.
5045 * - Call some other function once the data is in?
5047 if (ctl_debug & CTL_DEBUG_CDB_DATA)
5051 * XXX KDM call ctl_scsiio() again for now, and check flag
5052 * bits to see whether we're allocated or not.
5054 retval = ctl_scsiio(&io->scsiio);
5060 * This gets called by a backend driver when it is done with a
5061 * data_submit method.
5064 ctl_data_submit_done(union ctl_io *io)
5067 * If the IO_CONT flag is set, we need to call the supplied
5068 * function to continue processing the I/O, instead of completing
5071 * If there is an error, though, we don't want to keep processing.
5072 * Instead, just send status back to the initiator.
5074 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5075 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5076 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5077 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5078 io->scsiio.io_cont(io);
5085 * This gets called by a backend driver when it is done with a
5086 * configuration write.
5089 ctl_config_write_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);
5109 * Since a configuration write can be done for commands that actually
5110 * have data allocated, like write buffer, and commands that have
5111 * no data, like start/stop unit, we need to check here.
5113 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5114 buf = io->scsiio.kern_data_ptr;
5123 * SCSI release command.
5126 ctl_scsi_release(struct ctl_scsiio *ctsio)
5128 int length, longid, thirdparty_id, resv_id;
5129 struct ctl_softc *ctl_softc;
5130 struct ctl_lun *lun;
5136 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5138 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5139 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5140 ctl_softc = control_softc;
5142 switch (ctsio->cdb[0]) {
5144 struct scsi_release_10 *cdb;
5146 cdb = (struct scsi_release_10 *)ctsio->cdb;
5148 if (cdb->byte2 & SR10_LONGID)
5151 thirdparty_id = cdb->thirdparty_id;
5153 resv_id = cdb->resv_id;
5154 length = scsi_2btoul(cdb->length);
5161 * XXX KDM right now, we only support LUN reservation. We don't
5162 * support 3rd party reservations, or extent reservations, which
5163 * might actually need the parameter list. If we've gotten this
5164 * far, we've got a LUN reservation. Anything else got kicked out
5165 * above. So, according to SPC, ignore the length.
5169 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5171 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5172 ctsio->kern_data_len = length;
5173 ctsio->kern_total_len = length;
5174 ctsio->kern_data_resid = 0;
5175 ctsio->kern_rel_offset = 0;
5176 ctsio->kern_sg_entries = 0;
5177 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5178 ctsio->be_move_done = ctl_config_move_done;
5179 ctl_datamove((union ctl_io *)ctsio);
5181 return (CTL_RETVAL_COMPLETE);
5185 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5187 mtx_lock(&lun->lun_lock);
5190 * According to SPC, it is not an error for an intiator to attempt
5191 * to release a reservation on a LUN that isn't reserved, or that
5192 * is reserved by another initiator. The reservation can only be
5193 * released, though, by the initiator who made it or by one of
5194 * several reset type events.
5196 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5197 lun->flags &= ~CTL_LUN_RESERVED;
5199 mtx_unlock(&lun->lun_lock);
5201 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5202 free(ctsio->kern_data_ptr, M_CTL);
5203 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5206 ctl_set_success(ctsio);
5207 ctl_done((union ctl_io *)ctsio);
5208 return (CTL_RETVAL_COMPLETE);
5212 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5214 int extent, thirdparty, longid;
5215 int resv_id, length;
5216 uint64_t thirdparty_id;
5217 struct ctl_softc *ctl_softc;
5218 struct ctl_lun *lun;
5228 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5230 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5231 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5232 ctl_softc = control_softc;
5234 switch (ctsio->cdb[0]) {
5236 struct scsi_reserve_10 *cdb;
5238 cdb = (struct scsi_reserve_10 *)ctsio->cdb;
5240 if (cdb->byte2 & SR10_LONGID)
5243 thirdparty_id = cdb->thirdparty_id;
5245 resv_id = cdb->resv_id;
5246 length = scsi_2btoul(cdb->length);
5252 * XXX KDM right now, we only support LUN reservation. We don't
5253 * support 3rd party reservations, or extent reservations, which
5254 * might actually need the parameter list. If we've gotten this
5255 * far, we've got a LUN reservation. Anything else got kicked out
5256 * above. So, according to SPC, ignore the length.
5260 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5262 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5263 ctsio->kern_data_len = length;
5264 ctsio->kern_total_len = length;
5265 ctsio->kern_data_resid = 0;
5266 ctsio->kern_rel_offset = 0;
5267 ctsio->kern_sg_entries = 0;
5268 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5269 ctsio->be_move_done = ctl_config_move_done;
5270 ctl_datamove((union ctl_io *)ctsio);
5272 return (CTL_RETVAL_COMPLETE);
5276 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5278 mtx_lock(&lun->lun_lock);
5279 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5280 ctl_set_reservation_conflict(ctsio);
5284 lun->flags |= CTL_LUN_RESERVED;
5285 lun->res_idx = residx;
5287 ctl_set_success(ctsio);
5290 mtx_unlock(&lun->lun_lock);
5292 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5293 free(ctsio->kern_data_ptr, M_CTL);
5294 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5297 ctl_done((union ctl_io *)ctsio);
5298 return (CTL_RETVAL_COMPLETE);
5302 ctl_start_stop(struct ctl_scsiio *ctsio)
5304 struct scsi_start_stop_unit *cdb;
5305 struct ctl_lun *lun;
5306 struct ctl_softc *ctl_softc;
5309 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5311 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5312 ctl_softc = control_softc;
5315 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5319 * We don't support the immediate bit on a stop unit. In order to
5320 * do that, we would need to code up a way to know that a stop is
5321 * pending, and hold off any new commands until it completes, one
5322 * way or another. Then we could accept or reject those commands
5323 * depending on its status. We would almost need to do the reverse
5324 * of what we do below for an immediate start -- return the copy of
5325 * the ctl_io to the FETD with status to send to the host (and to
5326 * free the copy!) and then free the original I/O once the stop
5327 * actually completes. That way, the OOA queue mechanism can work
5328 * to block commands that shouldn't proceed. Another alternative
5329 * would be to put the copy in the queue in place of the original,
5330 * and return the original back to the caller. That could be
5333 if ((cdb->byte2 & SSS_IMMED)
5334 && ((cdb->how & SSS_START) == 0)) {
5335 ctl_set_invalid_field(ctsio,
5341 ctl_done((union ctl_io *)ctsio);
5342 return (CTL_RETVAL_COMPLETE);
5345 if ((lun->flags & CTL_LUN_PR_RESERVED)
5346 && ((cdb->how & SSS_START)==0)) {
5349 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5350 if (lun->pr_keys[residx] == 0
5351 || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5353 ctl_set_reservation_conflict(ctsio);
5354 ctl_done((union ctl_io *)ctsio);
5355 return (CTL_RETVAL_COMPLETE);
5360 * If there is no backend on this device, we can't start or stop
5361 * it. In theory we shouldn't get any start/stop commands in the
5362 * first place at this level if the LUN doesn't have a backend.
5363 * That should get stopped by the command decode code.
5365 if (lun->backend == NULL) {
5366 ctl_set_invalid_opcode(ctsio);
5367 ctl_done((union ctl_io *)ctsio);
5368 return (CTL_RETVAL_COMPLETE);
5372 * XXX KDM Copan-specific offline behavior.
5373 * Figure out a reasonable way to port this?
5376 mtx_lock(&lun->lun_lock);
5378 if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
5379 && (lun->flags & CTL_LUN_OFFLINE)) {
5381 * If the LUN is offline, and the on/offline bit isn't set,
5382 * reject the start or stop. Otherwise, let it through.
5384 mtx_unlock(&lun->lun_lock);
5385 ctl_set_lun_not_ready(ctsio);
5386 ctl_done((union ctl_io *)ctsio);
5388 mtx_unlock(&lun->lun_lock);
5389 #endif /* NEEDTOPORT */
5391 * This could be a start or a stop when we're online,
5392 * or a stop/offline or start/online. A start or stop when
5393 * we're offline is covered in the case above.
5396 * In the non-immediate case, we send the request to
5397 * the backend and return status to the user when
5400 * In the immediate case, we allocate a new ctl_io
5401 * to hold a copy of the request, and send that to
5402 * the backend. We then set good status on the
5403 * user's request and return it immediately.
5405 if (cdb->byte2 & SSS_IMMED) {
5406 union ctl_io *new_io;
5408 new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5409 ctl_copy_io((union ctl_io *)ctsio, new_io);
5410 retval = lun->backend->config_write(new_io);
5411 ctl_set_success(ctsio);
5412 ctl_done((union ctl_io *)ctsio);
5414 retval = lun->backend->config_write(
5415 (union ctl_io *)ctsio);
5424 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5425 * we don't really do anything with the LBA and length fields if the user
5426 * passes them in. Instead we'll just flush out the cache for the entire
5430 ctl_sync_cache(struct ctl_scsiio *ctsio)
5432 struct ctl_lun *lun;
5433 struct ctl_softc *ctl_softc;
5434 uint64_t starting_lba;
5435 uint32_t block_count;
5438 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5440 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5441 ctl_softc = control_softc;
5444 switch (ctsio->cdb[0]) {
5445 case SYNCHRONIZE_CACHE: {
5446 struct scsi_sync_cache *cdb;
5447 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5449 starting_lba = scsi_4btoul(cdb->begin_lba);
5450 block_count = scsi_2btoul(cdb->lb_count);
5453 case SYNCHRONIZE_CACHE_16: {
5454 struct scsi_sync_cache_16 *cdb;
5455 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5457 starting_lba = scsi_8btou64(cdb->begin_lba);
5458 block_count = scsi_4btoul(cdb->lb_count);
5462 ctl_set_invalid_opcode(ctsio);
5463 ctl_done((union ctl_io *)ctsio);
5465 break; /* NOTREACHED */
5469 * We check the LBA and length, but don't do anything with them.
5470 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5471 * get flushed. This check will just help satisfy anyone who wants
5472 * to see an error for an out of range LBA.
5474 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5475 ctl_set_lba_out_of_range(ctsio);
5476 ctl_done((union ctl_io *)ctsio);
5481 * If this LUN has no backend, we can't flush the cache anyway.
5483 if (lun->backend == NULL) {
5484 ctl_set_invalid_opcode(ctsio);
5485 ctl_done((union ctl_io *)ctsio);
5490 * Check to see whether we're configured to send the SYNCHRONIZE
5491 * CACHE command directly to the back end.
5493 mtx_lock(&lun->lun_lock);
5494 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC)
5495 && (++(lun->sync_count) >= lun->sync_interval)) {
5496 lun->sync_count = 0;
5497 mtx_unlock(&lun->lun_lock);
5498 retval = lun->backend->config_write((union ctl_io *)ctsio);
5500 mtx_unlock(&lun->lun_lock);
5501 ctl_set_success(ctsio);
5502 ctl_done((union ctl_io *)ctsio);
5511 ctl_format(struct ctl_scsiio *ctsio)
5513 struct scsi_format *cdb;
5514 struct ctl_lun *lun;
5515 struct ctl_softc *ctl_softc;
5516 int length, defect_list_len;
5518 CTL_DEBUG_PRINT(("ctl_format\n"));
5520 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5521 ctl_softc = control_softc;
5523 cdb = (struct scsi_format *)ctsio->cdb;
5526 if (cdb->byte2 & SF_FMTDATA) {
5527 if (cdb->byte2 & SF_LONGLIST)
5528 length = sizeof(struct scsi_format_header_long);
5530 length = sizeof(struct scsi_format_header_short);
5533 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5535 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5536 ctsio->kern_data_len = length;
5537 ctsio->kern_total_len = length;
5538 ctsio->kern_data_resid = 0;
5539 ctsio->kern_rel_offset = 0;
5540 ctsio->kern_sg_entries = 0;
5541 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5542 ctsio->be_move_done = ctl_config_move_done;
5543 ctl_datamove((union ctl_io *)ctsio);
5545 return (CTL_RETVAL_COMPLETE);
5548 defect_list_len = 0;
5550 if (cdb->byte2 & SF_FMTDATA) {
5551 if (cdb->byte2 & SF_LONGLIST) {
5552 struct scsi_format_header_long *header;
5554 header = (struct scsi_format_header_long *)
5555 ctsio->kern_data_ptr;
5557 defect_list_len = scsi_4btoul(header->defect_list_len);
5558 if (defect_list_len != 0) {
5559 ctl_set_invalid_field(ctsio,
5568 struct scsi_format_header_short *header;
5570 header = (struct scsi_format_header_short *)
5571 ctsio->kern_data_ptr;
5573 defect_list_len = scsi_2btoul(header->defect_list_len);
5574 if (defect_list_len != 0) {
5575 ctl_set_invalid_field(ctsio,
5587 * The format command will clear out the "Medium format corrupted"
5588 * status if set by the configuration code. That status is really
5589 * just a way to notify the host that we have lost the media, and
5590 * get them to issue a command that will basically make them think
5591 * they're blowing away the media.
5593 mtx_lock(&lun->lun_lock);
5594 lun->flags &= ~CTL_LUN_INOPERABLE;
5595 mtx_unlock(&lun->lun_lock);
5597 ctl_set_success(ctsio);
5600 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5601 free(ctsio->kern_data_ptr, M_CTL);
5602 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5605 ctl_done((union ctl_io *)ctsio);
5606 return (CTL_RETVAL_COMPLETE);
5610 ctl_read_buffer(struct ctl_scsiio *ctsio)
5612 struct scsi_read_buffer *cdb;
5613 struct ctl_lun *lun;
5614 int buffer_offset, len;
5615 static uint8_t descr[4];
5616 static uint8_t echo_descr[4] = { 0 };
5618 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5620 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5621 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5623 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA &&
5624 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR &&
5625 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) {
5626 ctl_set_invalid_field(ctsio,
5632 ctl_done((union ctl_io *)ctsio);
5633 return (CTL_RETVAL_COMPLETE);
5636 len = scsi_3btoul(cdb->length);
5637 buffer_offset = scsi_3btoul(cdb->offset);
5639 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5640 ctl_set_invalid_field(ctsio,
5646 ctl_done((union ctl_io *)ctsio);
5647 return (CTL_RETVAL_COMPLETE);
5650 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5652 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5653 ctsio->kern_data_ptr = descr;
5654 len = min(len, sizeof(descr));
5655 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5656 ctsio->kern_data_ptr = echo_descr;
5657 len = min(len, sizeof(echo_descr));
5659 if (lun->write_buffer == NULL) {
5660 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5663 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5665 ctsio->kern_data_len = len;
5666 ctsio->kern_total_len = len;
5667 ctsio->kern_data_resid = 0;
5668 ctsio->kern_rel_offset = 0;
5669 ctsio->kern_sg_entries = 0;
5670 ctl_set_success(ctsio);
5671 ctsio->be_move_done = ctl_config_move_done;
5672 ctl_datamove((union ctl_io *)ctsio);
5673 return (CTL_RETVAL_COMPLETE);
5677 ctl_write_buffer(struct ctl_scsiio *ctsio)
5679 struct scsi_write_buffer *cdb;
5680 struct ctl_lun *lun;
5681 int buffer_offset, len;
5683 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5685 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5686 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5688 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5689 ctl_set_invalid_field(ctsio,
5695 ctl_done((union ctl_io *)ctsio);
5696 return (CTL_RETVAL_COMPLETE);
5699 len = scsi_3btoul(cdb->length);
5700 buffer_offset = scsi_3btoul(cdb->offset);
5702 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5703 ctl_set_invalid_field(ctsio,
5709 ctl_done((union ctl_io *)ctsio);
5710 return (CTL_RETVAL_COMPLETE);
5714 * If we've got a kernel request that hasn't been malloced yet,
5715 * malloc it and tell the caller the data buffer is here.
5717 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5718 if (lun->write_buffer == NULL) {
5719 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5722 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5723 ctsio->kern_data_len = len;
5724 ctsio->kern_total_len = len;
5725 ctsio->kern_data_resid = 0;
5726 ctsio->kern_rel_offset = 0;
5727 ctsio->kern_sg_entries = 0;
5728 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5729 ctsio->be_move_done = ctl_config_move_done;
5730 ctl_datamove((union ctl_io *)ctsio);
5732 return (CTL_RETVAL_COMPLETE);
5735 ctl_set_success(ctsio);
5736 ctl_done((union ctl_io *)ctsio);
5737 return (CTL_RETVAL_COMPLETE);
5741 ctl_write_same(struct ctl_scsiio *ctsio)
5743 struct ctl_lun *lun;
5744 struct ctl_lba_len_flags *lbalen;
5746 uint32_t num_blocks;
5750 retval = CTL_RETVAL_COMPLETE;
5752 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5754 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5756 switch (ctsio->cdb[0]) {
5757 case WRITE_SAME_10: {
5758 struct scsi_write_same_10 *cdb;
5760 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5762 lba = scsi_4btoul(cdb->addr);
5763 num_blocks = scsi_2btoul(cdb->length);
5767 case WRITE_SAME_16: {
5768 struct scsi_write_same_16 *cdb;
5770 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5772 lba = scsi_8btou64(cdb->addr);
5773 num_blocks = scsi_4btoul(cdb->length);
5779 * We got a command we don't support. This shouldn't
5780 * happen, commands should be filtered out above us.
5782 ctl_set_invalid_opcode(ctsio);
5783 ctl_done((union ctl_io *)ctsio);
5785 return (CTL_RETVAL_COMPLETE);
5786 break; /* NOTREACHED */
5789 /* NDOB and ANCHOR flags can be used only together with UNMAP */
5790 if ((byte2 & SWS_UNMAP) == 0 &&
5791 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) {
5792 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5793 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5794 ctl_done((union ctl_io *)ctsio);
5795 return (CTL_RETVAL_COMPLETE);
5799 * The first check is to make sure we're in bounds, the second
5800 * check is to catch wrap-around problems. If the lba + num blocks
5801 * is less than the lba, then we've wrapped around and the block
5802 * range is invalid anyway.
5804 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5805 || ((lba + num_blocks) < lba)) {
5806 ctl_set_lba_out_of_range(ctsio);
5807 ctl_done((union ctl_io *)ctsio);
5808 return (CTL_RETVAL_COMPLETE);
5811 /* Zero number of blocks means "to the last logical block" */
5812 if (num_blocks == 0) {
5813 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5814 ctl_set_invalid_field(ctsio,
5820 ctl_done((union ctl_io *)ctsio);
5821 return (CTL_RETVAL_COMPLETE);
5823 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5826 len = lun->be_lun->blocksize;
5829 * If we've got a kernel request that hasn't been malloced yet,
5830 * malloc it and tell the caller the data buffer is here.
5832 if ((byte2 & SWS_NDOB) == 0 &&
5833 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5834 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5835 ctsio->kern_data_len = len;
5836 ctsio->kern_total_len = len;
5837 ctsio->kern_data_resid = 0;
5838 ctsio->kern_rel_offset = 0;
5839 ctsio->kern_sg_entries = 0;
5840 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5841 ctsio->be_move_done = ctl_config_move_done;
5842 ctl_datamove((union ctl_io *)ctsio);
5844 return (CTL_RETVAL_COMPLETE);
5847 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5849 lbalen->len = num_blocks;
5850 lbalen->flags = byte2;
5851 retval = lun->backend->config_write((union ctl_io *)ctsio);
5857 ctl_unmap(struct ctl_scsiio *ctsio)
5859 struct ctl_lun *lun;
5860 struct scsi_unmap *cdb;
5861 struct ctl_ptr_len_flags *ptrlen;
5862 struct scsi_unmap_header *hdr;
5863 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5865 uint32_t num_blocks;
5869 retval = CTL_RETVAL_COMPLETE;
5871 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5873 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5874 cdb = (struct scsi_unmap *)ctsio->cdb;
5876 len = scsi_2btoul(cdb->length);
5880 * If we've got a kernel request that hasn't been malloced yet,
5881 * malloc it and tell the caller the data buffer is here.
5883 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5884 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5885 ctsio->kern_data_len = len;
5886 ctsio->kern_total_len = len;
5887 ctsio->kern_data_resid = 0;
5888 ctsio->kern_rel_offset = 0;
5889 ctsio->kern_sg_entries = 0;
5890 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5891 ctsio->be_move_done = ctl_config_move_done;
5892 ctl_datamove((union ctl_io *)ctsio);
5894 return (CTL_RETVAL_COMPLETE);
5897 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5898 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5899 if (len < sizeof (*hdr) ||
5900 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5901 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5902 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5903 ctl_set_invalid_field(ctsio,
5911 len = scsi_2btoul(hdr->desc_length);
5912 buf = (struct scsi_unmap_desc *)(hdr + 1);
5913 end = buf + len / sizeof(*buf);
5916 for (range = buf; range < end; range++) {
5917 lba = scsi_8btou64(range->lba);
5918 num_blocks = scsi_4btoul(range->length);
5919 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5920 || ((lba + num_blocks) < lba)) {
5921 ctl_set_lba_out_of_range(ctsio);
5922 ctl_done((union ctl_io *)ctsio);
5923 return (CTL_RETVAL_COMPLETE);
5925 if (num_blocks != 0)
5930 * Block backend can not handle zero last range.
5931 * Filter it out and return if there is nothing left.
5933 len = (uint8_t *)endnz - (uint8_t *)buf;
5935 ctl_set_success(ctsio);
5939 mtx_lock(&lun->lun_lock);
5940 ptrlen = (struct ctl_ptr_len_flags *)
5941 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5942 ptrlen->ptr = (void *)buf;
5944 ptrlen->flags = byte2;
5945 ctl_check_blocked(lun);
5946 mtx_unlock(&lun->lun_lock);
5948 retval = lun->backend->config_write((union ctl_io *)ctsio);
5952 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5953 free(ctsio->kern_data_ptr, M_CTL);
5954 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5956 ctl_done((union ctl_io *)ctsio);
5957 return (CTL_RETVAL_COMPLETE);
5961 * Note that this function currently doesn't actually do anything inside
5962 * CTL to enforce things if the DQue bit is turned on.
5964 * Also note that this function can't be used in the default case, because
5965 * the DQue bit isn't set in the changeable mask for the control mode page
5966 * anyway. This is just here as an example for how to implement a page
5967 * handler, and a placeholder in case we want to allow the user to turn
5968 * tagged queueing on and off.
5970 * The D_SENSE bit handling is functional, however, and will turn
5971 * descriptor sense on and off for a given LUN.
5974 ctl_control_page_handler(struct ctl_scsiio *ctsio,
5975 struct ctl_page_index *page_index, uint8_t *page_ptr)
5977 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
5978 struct ctl_lun *lun;
5979 struct ctl_softc *softc;
5983 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5984 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5987 user_cp = (struct scsi_control_page *)page_ptr;
5988 current_cp = (struct scsi_control_page *)
5989 (page_index->page_data + (page_index->page_len *
5991 saved_cp = (struct scsi_control_page *)
5992 (page_index->page_data + (page_index->page_len *
5995 softc = control_softc;
5997 mtx_lock(&lun->lun_lock);
5998 if (((current_cp->rlec & SCP_DSENSE) == 0)
5999 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
6001 * Descriptor sense is currently turned off and the user
6002 * wants to turn it on.
6004 current_cp->rlec |= SCP_DSENSE;
6005 saved_cp->rlec |= SCP_DSENSE;
6006 lun->flags |= CTL_LUN_SENSE_DESC;
6008 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
6009 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
6011 * Descriptor sense is currently turned on, and the user
6012 * wants to turn it off.
6014 current_cp->rlec &= ~SCP_DSENSE;
6015 saved_cp->rlec &= ~SCP_DSENSE;
6016 lun->flags &= ~CTL_LUN_SENSE_DESC;
6019 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) !=
6020 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) {
6021 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
6022 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
6023 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
6024 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
6027 if ((current_cp->eca_and_aen & SCP_SWP) !=
6028 (user_cp->eca_and_aen & SCP_SWP)) {
6029 current_cp->eca_and_aen &= ~SCP_SWP;
6030 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
6031 saved_cp->eca_and_aen &= ~SCP_SWP;
6032 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
6038 * Let other initiators know that the mode
6039 * parameters for this LUN have changed.
6041 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
6045 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE;
6048 mtx_unlock(&lun->lun_lock);
6054 ctl_caching_sp_handler(struct ctl_scsiio *ctsio,
6055 struct ctl_page_index *page_index, uint8_t *page_ptr)
6057 struct scsi_caching_page *current_cp, *saved_cp, *user_cp;
6058 struct ctl_lun *lun;
6062 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6063 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6066 user_cp = (struct scsi_caching_page *)page_ptr;
6067 current_cp = (struct scsi_caching_page *)
6068 (page_index->page_data + (page_index->page_len *
6070 saved_cp = (struct scsi_caching_page *)
6071 (page_index->page_data + (page_index->page_len *
6074 mtx_lock(&lun->lun_lock);
6075 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) !=
6076 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) {
6077 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
6078 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
6079 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
6080 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
6086 * Let other initiators know that the mode
6087 * parameters for this LUN have changed.
6089 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
6093 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE;
6096 mtx_unlock(&lun->lun_lock);
6102 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
6103 struct ctl_page_index *page_index,
6109 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
6114 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
6115 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
6116 printf("page data:");
6118 printf(" %.2x",page_ptr[i]);
6124 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
6125 struct ctl_page_index *page_index,
6128 struct copan_debugconf_subpage *page;
6130 page = (struct copan_debugconf_subpage *)page_index->page_data +
6131 (page_index->page_len * pc);
6134 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6135 case SMS_PAGE_CTRL_DEFAULT >> 6:
6136 case SMS_PAGE_CTRL_SAVED >> 6:
6138 * We don't update the changable or default bits for this page.
6141 case SMS_PAGE_CTRL_CURRENT >> 6:
6142 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
6143 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
6147 EPRINT(0, "Invalid PC %d!!", pc);
6148 #endif /* NEEDTOPORT */
6156 ctl_do_mode_select(union ctl_io *io)
6158 struct scsi_mode_page_header *page_header;
6159 struct ctl_page_index *page_index;
6160 struct ctl_scsiio *ctsio;
6161 int control_dev, page_len;
6162 int page_len_offset, page_len_size;
6163 union ctl_modepage_info *modepage_info;
6164 struct ctl_lun *lun;
6165 int *len_left, *len_used;
6168 ctsio = &io->scsiio;
6171 retval = CTL_RETVAL_COMPLETE;
6173 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6175 if (lun->be_lun->lun_type != T_DIRECT)
6180 modepage_info = (union ctl_modepage_info *)
6181 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6182 len_left = &modepage_info->header.len_left;
6183 len_used = &modepage_info->header.len_used;
6187 page_header = (struct scsi_mode_page_header *)
6188 (ctsio->kern_data_ptr + *len_used);
6190 if (*len_left == 0) {
6191 free(ctsio->kern_data_ptr, M_CTL);
6192 ctl_set_success(ctsio);
6193 ctl_done((union ctl_io *)ctsio);
6194 return (CTL_RETVAL_COMPLETE);
6195 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6197 free(ctsio->kern_data_ptr, M_CTL);
6198 ctl_set_param_len_error(ctsio);
6199 ctl_done((union ctl_io *)ctsio);
6200 return (CTL_RETVAL_COMPLETE);
6202 } else if ((page_header->page_code & SMPH_SPF)
6203 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6205 free(ctsio->kern_data_ptr, M_CTL);
6206 ctl_set_param_len_error(ctsio);
6207 ctl_done((union ctl_io *)ctsio);
6208 return (CTL_RETVAL_COMPLETE);
6213 * XXX KDM should we do something with the block descriptor?
6215 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6217 if ((control_dev != 0)
6218 && (lun->mode_pages.index[i].page_flags &
6219 CTL_PAGE_FLAG_DISK_ONLY))
6222 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
6223 (page_header->page_code & SMPH_PC_MASK))
6227 * If neither page has a subpage code, then we've got a
6230 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
6231 && ((page_header->page_code & SMPH_SPF) == 0)) {
6232 page_index = &lun->mode_pages.index[i];
6233 page_len = page_header->page_length;
6238 * If both pages have subpages, then the subpage numbers
6241 if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
6242 && (page_header->page_code & SMPH_SPF)) {
6243 struct scsi_mode_page_header_sp *sph;
6245 sph = (struct scsi_mode_page_header_sp *)page_header;
6247 if (lun->mode_pages.index[i].subpage ==
6249 page_index = &lun->mode_pages.index[i];
6250 page_len = scsi_2btoul(sph->page_length);
6257 * If we couldn't find the page, or if we don't have a mode select
6258 * handler for it, send back an error to the user.
6260 if ((page_index == NULL)
6261 || (page_index->select_handler == NULL)) {
6262 ctl_set_invalid_field(ctsio,
6265 /*field*/ *len_used,
6268 free(ctsio->kern_data_ptr, M_CTL);
6269 ctl_done((union ctl_io *)ctsio);
6270 return (CTL_RETVAL_COMPLETE);
6273 if (page_index->page_code & SMPH_SPF) {
6274 page_len_offset = 2;
6278 page_len_offset = 1;
6282 * If the length the initiator gives us isn't the one we specify in
6283 * the mode page header, or if they didn't specify enough data in
6284 * the CDB to avoid truncating this page, kick out the request.
6286 if ((page_len != (page_index->page_len - page_len_offset -
6288 || (*len_left < page_index->page_len)) {
6291 ctl_set_invalid_field(ctsio,
6294 /*field*/ *len_used + page_len_offset,
6297 free(ctsio->kern_data_ptr, M_CTL);
6298 ctl_done((union ctl_io *)ctsio);
6299 return (CTL_RETVAL_COMPLETE);
6303 * Run through the mode page, checking to make sure that the bits
6304 * the user changed are actually legal for him to change.
6306 for (i = 0; i < page_index->page_len; i++) {
6307 uint8_t *user_byte, *change_mask, *current_byte;
6311 user_byte = (uint8_t *)page_header + i;
6312 change_mask = page_index->page_data +
6313 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6314 current_byte = page_index->page_data +
6315 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6318 * Check to see whether the user set any bits in this byte
6319 * that he is not allowed to set.
6321 if ((*user_byte & ~(*change_mask)) ==
6322 (*current_byte & ~(*change_mask)))
6326 * Go through bit by bit to determine which one is illegal.
6329 for (j = 7; j >= 0; j--) {
6330 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6331 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6336 ctl_set_invalid_field(ctsio,
6339 /*field*/ *len_used + i,
6342 free(ctsio->kern_data_ptr, M_CTL);
6343 ctl_done((union ctl_io *)ctsio);
6344 return (CTL_RETVAL_COMPLETE);
6348 * Decrement these before we call the page handler, since we may
6349 * end up getting called back one way or another before the handler
6350 * returns to this context.
6352 *len_left -= page_index->page_len;
6353 *len_used += page_index->page_len;
6355 retval = page_index->select_handler(ctsio, page_index,
6356 (uint8_t *)page_header);
6359 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6360 * wait until this queued command completes to finish processing
6361 * the mode page. If it returns anything other than
6362 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6363 * already set the sense information, freed the data pointer, and
6364 * completed the io for us.
6366 if (retval != CTL_RETVAL_COMPLETE)
6367 goto bailout_no_done;
6370 * If the initiator sent us more than one page, parse the next one.
6375 ctl_set_success(ctsio);
6376 free(ctsio->kern_data_ptr, M_CTL);
6377 ctl_done((union ctl_io *)ctsio);
6381 return (CTL_RETVAL_COMPLETE);
6386 ctl_mode_select(struct ctl_scsiio *ctsio)
6388 int param_len, pf, sp;
6389 int header_size, bd_len;
6390 int len_left, len_used;
6391 struct ctl_page_index *page_index;
6392 struct ctl_lun *lun;
6393 int control_dev, page_len;
6394 union ctl_modepage_info *modepage_info;
6406 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6408 if (lun->be_lun->lun_type != T_DIRECT)
6413 switch (ctsio->cdb[0]) {
6414 case MODE_SELECT_6: {
6415 struct scsi_mode_select_6 *cdb;
6417 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6419 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6420 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6422 param_len = cdb->length;
6423 header_size = sizeof(struct scsi_mode_header_6);
6426 case MODE_SELECT_10: {
6427 struct scsi_mode_select_10 *cdb;
6429 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6431 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6432 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6434 param_len = scsi_2btoul(cdb->length);
6435 header_size = sizeof(struct scsi_mode_header_10);
6439 ctl_set_invalid_opcode(ctsio);
6440 ctl_done((union ctl_io *)ctsio);
6441 return (CTL_RETVAL_COMPLETE);
6442 break; /* NOTREACHED */
6447 * "A parameter list length of zero indicates that the Data-Out Buffer
6448 * shall be empty. This condition shall not be considered as an error."
6450 if (param_len == 0) {
6451 ctl_set_success(ctsio);
6452 ctl_done((union ctl_io *)ctsio);
6453 return (CTL_RETVAL_COMPLETE);
6457 * Since we'll hit this the first time through, prior to
6458 * allocation, we don't need to free a data buffer here.
6460 if (param_len < header_size) {
6461 ctl_set_param_len_error(ctsio);
6462 ctl_done((union ctl_io *)ctsio);
6463 return (CTL_RETVAL_COMPLETE);
6467 * Allocate the data buffer and grab the user's data. In theory,
6468 * we shouldn't have to sanity check the parameter list length here
6469 * because the maximum size is 64K. We should be able to malloc
6470 * that much without too many problems.
6472 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6473 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6474 ctsio->kern_data_len = param_len;
6475 ctsio->kern_total_len = param_len;
6476 ctsio->kern_data_resid = 0;
6477 ctsio->kern_rel_offset = 0;
6478 ctsio->kern_sg_entries = 0;
6479 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6480 ctsio->be_move_done = ctl_config_move_done;
6481 ctl_datamove((union ctl_io *)ctsio);
6483 return (CTL_RETVAL_COMPLETE);
6486 switch (ctsio->cdb[0]) {
6487 case MODE_SELECT_6: {
6488 struct scsi_mode_header_6 *mh6;
6490 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6491 bd_len = mh6->blk_desc_len;
6494 case MODE_SELECT_10: {
6495 struct scsi_mode_header_10 *mh10;
6497 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6498 bd_len = scsi_2btoul(mh10->blk_desc_len);
6502 panic("Invalid CDB type %#x", ctsio->cdb[0]);
6506 if (param_len < (header_size + bd_len)) {
6507 free(ctsio->kern_data_ptr, M_CTL);
6508 ctl_set_param_len_error(ctsio);
6509 ctl_done((union ctl_io *)ctsio);
6510 return (CTL_RETVAL_COMPLETE);
6514 * Set the IO_CONT flag, so that if this I/O gets passed to
6515 * ctl_config_write_done(), it'll get passed back to
6516 * ctl_do_mode_select() for further processing, or completion if
6519 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6520 ctsio->io_cont = ctl_do_mode_select;
6522 modepage_info = (union ctl_modepage_info *)
6523 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6525 memset(modepage_info, 0, sizeof(*modepage_info));
6527 len_left = param_len - header_size - bd_len;
6528 len_used = header_size + bd_len;
6530 modepage_info->header.len_left = len_left;
6531 modepage_info->header.len_used = len_used;
6533 return (ctl_do_mode_select((union ctl_io *)ctsio));
6537 ctl_mode_sense(struct ctl_scsiio *ctsio)
6539 struct ctl_lun *lun;
6540 int pc, page_code, dbd, llba, subpage;
6541 int alloc_len, page_len, header_len, total_len;
6542 struct scsi_mode_block_descr *block_desc;
6543 struct ctl_page_index *page_index;
6551 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6553 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6555 if (lun->be_lun->lun_type != T_DIRECT)
6560 switch (ctsio->cdb[0]) {
6561 case MODE_SENSE_6: {
6562 struct scsi_mode_sense_6 *cdb;
6564 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6566 header_len = sizeof(struct scsi_mode_hdr_6);
6567 if (cdb->byte2 & SMS_DBD)
6570 header_len += sizeof(struct scsi_mode_block_descr);
6572 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6573 page_code = cdb->page & SMS_PAGE_CODE;
6574 subpage = cdb->subpage;
6575 alloc_len = cdb->length;
6578 case MODE_SENSE_10: {
6579 struct scsi_mode_sense_10 *cdb;
6581 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6583 header_len = sizeof(struct scsi_mode_hdr_10);
6585 if (cdb->byte2 & SMS_DBD)
6588 header_len += sizeof(struct scsi_mode_block_descr);
6589 if (cdb->byte2 & SMS10_LLBAA)
6591 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6592 page_code = cdb->page & SMS_PAGE_CODE;
6593 subpage = cdb->subpage;
6594 alloc_len = scsi_2btoul(cdb->length);
6598 ctl_set_invalid_opcode(ctsio);
6599 ctl_done((union ctl_io *)ctsio);
6600 return (CTL_RETVAL_COMPLETE);
6601 break; /* NOTREACHED */
6605 * We have to make a first pass through to calculate the size of
6606 * the pages that match the user's query. Then we allocate enough
6607 * memory to hold it, and actually copy the data into the buffer.
6609 switch (page_code) {
6610 case SMS_ALL_PAGES_PAGE: {
6616 * At the moment, values other than 0 and 0xff here are
6617 * reserved according to SPC-3.
6619 if ((subpage != SMS_SUBPAGE_PAGE_0)
6620 && (subpage != SMS_SUBPAGE_ALL)) {
6621 ctl_set_invalid_field(ctsio,
6627 ctl_done((union ctl_io *)ctsio);
6628 return (CTL_RETVAL_COMPLETE);
6631 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6632 if ((control_dev != 0)
6633 && (lun->mode_pages.index[i].page_flags &
6634 CTL_PAGE_FLAG_DISK_ONLY))
6638 * We don't use this subpage if the user didn't
6639 * request all subpages.
6641 if ((lun->mode_pages.index[i].subpage != 0)
6642 && (subpage == SMS_SUBPAGE_PAGE_0))
6646 printf("found page %#x len %d\n",
6647 lun->mode_pages.index[i].page_code &
6649 lun->mode_pages.index[i].page_len);
6651 page_len += lun->mode_pages.index[i].page_len;
6660 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6661 /* Look for the right page code */
6662 if ((lun->mode_pages.index[i].page_code &
6663 SMPH_PC_MASK) != page_code)
6666 /* Look for the right subpage or the subpage wildcard*/
6667 if ((lun->mode_pages.index[i].subpage != subpage)
6668 && (subpage != SMS_SUBPAGE_ALL))
6671 /* Make sure the page is supported for this dev type */
6672 if ((control_dev != 0)
6673 && (lun->mode_pages.index[i].page_flags &
6674 CTL_PAGE_FLAG_DISK_ONLY))
6678 printf("found page %#x len %d\n",
6679 lun->mode_pages.index[i].page_code &
6681 lun->mode_pages.index[i].page_len);
6684 page_len += lun->mode_pages.index[i].page_len;
6687 if (page_len == 0) {
6688 ctl_set_invalid_field(ctsio,
6694 ctl_done((union ctl_io *)ctsio);
6695 return (CTL_RETVAL_COMPLETE);
6701 total_len = header_len + page_len;
6703 printf("header_len = %d, page_len = %d, total_len = %d\n",
6704 header_len, page_len, total_len);
6707 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6708 ctsio->kern_sg_entries = 0;
6709 ctsio->kern_data_resid = 0;
6710 ctsio->kern_rel_offset = 0;
6711 if (total_len < alloc_len) {
6712 ctsio->residual = alloc_len - total_len;
6713 ctsio->kern_data_len = total_len;
6714 ctsio->kern_total_len = total_len;
6716 ctsio->residual = 0;
6717 ctsio->kern_data_len = alloc_len;
6718 ctsio->kern_total_len = alloc_len;
6721 switch (ctsio->cdb[0]) {
6722 case MODE_SENSE_6: {
6723 struct scsi_mode_hdr_6 *header;
6725 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6727 header->datalen = ctl_min(total_len - 1, 254);
6728 if (control_dev == 0) {
6729 header->dev_specific = 0x10; /* DPOFUA */
6730 if ((lun->flags & CTL_LUN_READONLY) ||
6731 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6732 .eca_and_aen & SCP_SWP) != 0)
6733 header->dev_specific |= 0x80; /* WP */
6736 header->block_descr_len = 0;
6738 header->block_descr_len =
6739 sizeof(struct scsi_mode_block_descr);
6740 block_desc = (struct scsi_mode_block_descr *)&header[1];
6743 case MODE_SENSE_10: {
6744 struct scsi_mode_hdr_10 *header;
6747 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6749 datalen = ctl_min(total_len - 2, 65533);
6750 scsi_ulto2b(datalen, header->datalen);
6751 if (control_dev == 0) {
6752 header->dev_specific = 0x10; /* DPOFUA */
6753 if ((lun->flags & CTL_LUN_READONLY) ||
6754 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6755 .eca_and_aen & SCP_SWP) != 0)
6756 header->dev_specific |= 0x80; /* WP */
6759 scsi_ulto2b(0, header->block_descr_len);
6761 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6762 header->block_descr_len);
6763 block_desc = (struct scsi_mode_block_descr *)&header[1];
6767 panic("invalid CDB type %#x", ctsio->cdb[0]);
6768 break; /* NOTREACHED */
6772 * If we've got a disk, use its blocksize in the block
6773 * descriptor. Otherwise, just set it to 0.
6776 if (control_dev == 0)
6777 scsi_ulto3b(lun->be_lun->blocksize,
6778 block_desc->block_len);
6780 scsi_ulto3b(0, block_desc->block_len);
6783 switch (page_code) {
6784 case SMS_ALL_PAGES_PAGE: {
6787 data_used = header_len;
6788 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6789 struct ctl_page_index *page_index;
6791 page_index = &lun->mode_pages.index[i];
6793 if ((control_dev != 0)
6794 && (page_index->page_flags &
6795 CTL_PAGE_FLAG_DISK_ONLY))
6799 * We don't use this subpage if the user didn't
6800 * request all subpages. We already checked (above)
6801 * to make sure the user only specified a subpage
6802 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6804 if ((page_index->subpage != 0)
6805 && (subpage == SMS_SUBPAGE_PAGE_0))
6809 * Call the handler, if it exists, to update the
6810 * page to the latest values.
6812 if (page_index->sense_handler != NULL)
6813 page_index->sense_handler(ctsio, page_index,pc);
6815 memcpy(ctsio->kern_data_ptr + data_used,
6816 page_index->page_data +
6817 (page_index->page_len * pc),
6818 page_index->page_len);
6819 data_used += page_index->page_len;
6826 data_used = header_len;
6828 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6829 struct ctl_page_index *page_index;
6831 page_index = &lun->mode_pages.index[i];
6833 /* Look for the right page code */
6834 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6837 /* Look for the right subpage or the subpage wildcard*/
6838 if ((page_index->subpage != subpage)
6839 && (subpage != SMS_SUBPAGE_ALL))
6842 /* Make sure the page is supported for this dev type */
6843 if ((control_dev != 0)
6844 && (page_index->page_flags &
6845 CTL_PAGE_FLAG_DISK_ONLY))
6849 * Call the handler, if it exists, to update the
6850 * page to the latest values.
6852 if (page_index->sense_handler != NULL)
6853 page_index->sense_handler(ctsio, page_index,pc);
6855 memcpy(ctsio->kern_data_ptr + data_used,
6856 page_index->page_data +
6857 (page_index->page_len * pc),
6858 page_index->page_len);
6859 data_used += page_index->page_len;
6865 ctl_set_success(ctsio);
6866 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6867 ctsio->be_move_done = ctl_config_move_done;
6868 ctl_datamove((union ctl_io *)ctsio);
6869 return (CTL_RETVAL_COMPLETE);
6873 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6874 struct ctl_page_index *page_index,
6877 struct ctl_lun *lun;
6878 struct scsi_log_param_header *phdr;
6882 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6883 data = page_index->page_data;
6885 if (lun->backend->lun_attr != NULL &&
6886 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6888 phdr = (struct scsi_log_param_header *)data;
6889 scsi_ulto2b(0x0001, phdr->param_code);
6890 phdr->param_control = SLP_LBIN | SLP_LP;
6891 phdr->param_len = 8;
6892 data = (uint8_t *)(phdr + 1);
6893 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6894 data[4] = 0x02; /* per-pool */
6895 data += phdr->param_len;
6898 if (lun->backend->lun_attr != NULL &&
6899 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6901 phdr = (struct scsi_log_param_header *)data;
6902 scsi_ulto2b(0x0002, phdr->param_code);
6903 phdr->param_control = SLP_LBIN | SLP_LP;
6904 phdr->param_len = 8;
6905 data = (uint8_t *)(phdr + 1);
6906 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6907 data[4] = 0x01; /* per-LUN */
6908 data += phdr->param_len;
6911 if (lun->backend->lun_attr != NULL &&
6912 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6914 phdr = (struct scsi_log_param_header *)data;
6915 scsi_ulto2b(0x00f1, phdr->param_code);
6916 phdr->param_control = SLP_LBIN | SLP_LP;
6917 phdr->param_len = 8;
6918 data = (uint8_t *)(phdr + 1);
6919 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6920 data[4] = 0x02; /* per-pool */
6921 data += phdr->param_len;
6924 if (lun->backend->lun_attr != NULL &&
6925 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6927 phdr = (struct scsi_log_param_header *)data;
6928 scsi_ulto2b(0x00f2, phdr->param_code);
6929 phdr->param_control = SLP_LBIN | SLP_LP;
6930 phdr->param_len = 8;
6931 data = (uint8_t *)(phdr + 1);
6932 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6933 data[4] = 0x02; /* per-pool */
6934 data += phdr->param_len;
6937 page_index->page_len = data - page_index->page_data;
6942 ctl_log_sense(struct ctl_scsiio *ctsio)
6944 struct ctl_lun *lun;
6945 int i, pc, page_code, subpage;
6946 int alloc_len, total_len;
6947 struct ctl_page_index *page_index;
6948 struct scsi_log_sense *cdb;
6949 struct scsi_log_header *header;
6951 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6953 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6954 cdb = (struct scsi_log_sense *)ctsio->cdb;
6955 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6956 page_code = cdb->page & SLS_PAGE_CODE;
6957 subpage = cdb->subpage;
6958 alloc_len = scsi_2btoul(cdb->length);
6961 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6962 page_index = &lun->log_pages.index[i];
6964 /* Look for the right page code */
6965 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6968 /* Look for the right subpage or the subpage wildcard*/
6969 if (page_index->subpage != subpage)
6974 if (i >= CTL_NUM_LOG_PAGES) {
6975 ctl_set_invalid_field(ctsio,
6981 ctl_done((union ctl_io *)ctsio);
6982 return (CTL_RETVAL_COMPLETE);
6985 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6987 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6988 ctsio->kern_sg_entries = 0;
6989 ctsio->kern_data_resid = 0;
6990 ctsio->kern_rel_offset = 0;
6991 if (total_len < alloc_len) {
6992 ctsio->residual = alloc_len - total_len;
6993 ctsio->kern_data_len = total_len;
6994 ctsio->kern_total_len = total_len;
6996 ctsio->residual = 0;
6997 ctsio->kern_data_len = alloc_len;
6998 ctsio->kern_total_len = alloc_len;
7001 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
7002 header->page = page_index->page_code;
7003 if (page_index->subpage) {
7004 header->page |= SL_SPF;
7005 header->subpage = page_index->subpage;
7007 scsi_ulto2b(page_index->page_len, header->datalen);
7010 * Call the handler, if it exists, to update the
7011 * page to the latest values.
7013 if (page_index->sense_handler != NULL)
7014 page_index->sense_handler(ctsio, page_index, pc);
7016 memcpy(header + 1, page_index->page_data, page_index->page_len);
7018 ctl_set_success(ctsio);
7019 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7020 ctsio->be_move_done = ctl_config_move_done;
7021 ctl_datamove((union ctl_io *)ctsio);
7022 return (CTL_RETVAL_COMPLETE);
7026 ctl_read_capacity(struct ctl_scsiio *ctsio)
7028 struct scsi_read_capacity *cdb;
7029 struct scsi_read_capacity_data *data;
7030 struct ctl_lun *lun;
7033 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
7035 cdb = (struct scsi_read_capacity *)ctsio->cdb;
7037 lba = scsi_4btoul(cdb->addr);
7038 if (((cdb->pmi & SRC_PMI) == 0)
7040 ctl_set_invalid_field(/*ctsio*/ ctsio,
7046 ctl_done((union ctl_io *)ctsio);
7047 return (CTL_RETVAL_COMPLETE);
7050 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7052 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7053 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
7054 ctsio->residual = 0;
7055 ctsio->kern_data_len = sizeof(*data);
7056 ctsio->kern_total_len = sizeof(*data);
7057 ctsio->kern_data_resid = 0;
7058 ctsio->kern_rel_offset = 0;
7059 ctsio->kern_sg_entries = 0;
7062 * If the maximum LBA is greater than 0xfffffffe, the user must
7063 * issue a SERVICE ACTION IN (16) command, with the read capacity
7064 * serivce action set.
7066 if (lun->be_lun->maxlba > 0xfffffffe)
7067 scsi_ulto4b(0xffffffff, data->addr);
7069 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
7072 * XXX KDM this may not be 512 bytes...
7074 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7076 ctl_set_success(ctsio);
7077 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7078 ctsio->be_move_done = ctl_config_move_done;
7079 ctl_datamove((union ctl_io *)ctsio);
7080 return (CTL_RETVAL_COMPLETE);
7084 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
7086 struct scsi_read_capacity_16 *cdb;
7087 struct scsi_read_capacity_data_long *data;
7088 struct ctl_lun *lun;
7092 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
7094 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
7096 alloc_len = scsi_4btoul(cdb->alloc_len);
7097 lba = scsi_8btou64(cdb->addr);
7099 if ((cdb->reladr & SRC16_PMI)
7101 ctl_set_invalid_field(/*ctsio*/ ctsio,
7107 ctl_done((union ctl_io *)ctsio);
7108 return (CTL_RETVAL_COMPLETE);
7111 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7113 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7114 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
7116 if (sizeof(*data) < alloc_len) {
7117 ctsio->residual = alloc_len - sizeof(*data);
7118 ctsio->kern_data_len = sizeof(*data);
7119 ctsio->kern_total_len = sizeof(*data);
7121 ctsio->residual = 0;
7122 ctsio->kern_data_len = alloc_len;
7123 ctsio->kern_total_len = alloc_len;
7125 ctsio->kern_data_resid = 0;
7126 ctsio->kern_rel_offset = 0;
7127 ctsio->kern_sg_entries = 0;
7129 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7130 /* XXX KDM this may not be 512 bytes... */
7131 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7132 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7133 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7134 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7135 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
7137 ctl_set_success(ctsio);
7138 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7139 ctsio->be_move_done = ctl_config_move_done;
7140 ctl_datamove((union ctl_io *)ctsio);
7141 return (CTL_RETVAL_COMPLETE);
7145 ctl_read_defect(struct ctl_scsiio *ctsio)
7147 struct scsi_read_defect_data_10 *ccb10;
7148 struct scsi_read_defect_data_12 *ccb12;
7149 struct scsi_read_defect_data_hdr_10 *data10;
7150 struct scsi_read_defect_data_hdr_12 *data12;
7151 uint32_t alloc_len, data_len;
7154 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7156 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7157 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7158 format = ccb10->format;
7159 alloc_len = scsi_2btoul(ccb10->alloc_length);
7160 data_len = sizeof(*data10);
7162 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7163 format = ccb12->format;
7164 alloc_len = scsi_4btoul(ccb12->alloc_length);
7165 data_len = sizeof(*data12);
7167 if (alloc_len == 0) {
7168 ctl_set_success(ctsio);
7169 ctl_done((union ctl_io *)ctsio);
7170 return (CTL_RETVAL_COMPLETE);
7173 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7174 if (data_len < alloc_len) {
7175 ctsio->residual = alloc_len - data_len;
7176 ctsio->kern_data_len = data_len;
7177 ctsio->kern_total_len = data_len;
7179 ctsio->residual = 0;
7180 ctsio->kern_data_len = alloc_len;
7181 ctsio->kern_total_len = alloc_len;
7183 ctsio->kern_data_resid = 0;
7184 ctsio->kern_rel_offset = 0;
7185 ctsio->kern_sg_entries = 0;
7187 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7188 data10 = (struct scsi_read_defect_data_hdr_10 *)
7189 ctsio->kern_data_ptr;
7190 data10->format = format;
7191 scsi_ulto2b(0, data10->length);
7193 data12 = (struct scsi_read_defect_data_hdr_12 *)
7194 ctsio->kern_data_ptr;
7195 data12->format = format;
7196 scsi_ulto2b(0, data12->generation);
7197 scsi_ulto4b(0, data12->length);
7200 ctl_set_success(ctsio);
7201 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7202 ctsio->be_move_done = ctl_config_move_done;
7203 ctl_datamove((union ctl_io *)ctsio);
7204 return (CTL_RETVAL_COMPLETE);
7208 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7210 struct scsi_maintenance_in *cdb;
7212 int alloc_len, ext, total_len = 0, g, p, pc, pg, gs, os;
7213 int num_target_port_groups, num_target_ports;
7214 struct ctl_lun *lun;
7215 struct ctl_softc *softc;
7216 struct ctl_port *port;
7217 struct scsi_target_group_data *rtg_ptr;
7218 struct scsi_target_group_data_extended *rtg_ext_ptr;
7219 struct scsi_target_port_group_descriptor *tpg_desc;
7221 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7223 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7224 softc = control_softc;
7225 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7227 retval = CTL_RETVAL_COMPLETE;
7229 switch (cdb->byte2 & STG_PDF_MASK) {
7230 case STG_PDF_LENGTH:
7233 case STG_PDF_EXTENDED:
7237 ctl_set_invalid_field(/*ctsio*/ ctsio,
7243 ctl_done((union ctl_io *)ctsio);
7247 if (softc->is_single)
7248 num_target_port_groups = 1;
7250 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
7251 num_target_ports = 0;
7252 mtx_lock(&softc->ctl_lock);
7253 STAILQ_FOREACH(port, &softc->port_list, links) {
7254 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7256 if (ctl_map_lun_back(port->targ_port, lun->lun) >= CTL_MAX_LUNS)
7260 mtx_unlock(&softc->ctl_lock);
7263 total_len = sizeof(struct scsi_target_group_data_extended);
7265 total_len = sizeof(struct scsi_target_group_data);
7266 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7267 num_target_port_groups +
7268 sizeof(struct scsi_target_port_descriptor) *
7269 num_target_ports * num_target_port_groups;
7271 alloc_len = scsi_4btoul(cdb->length);
7273 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7275 ctsio->kern_sg_entries = 0;
7277 if (total_len < alloc_len) {
7278 ctsio->residual = alloc_len - total_len;
7279 ctsio->kern_data_len = total_len;
7280 ctsio->kern_total_len = total_len;
7282 ctsio->residual = 0;
7283 ctsio->kern_data_len = alloc_len;
7284 ctsio->kern_total_len = alloc_len;
7286 ctsio->kern_data_resid = 0;
7287 ctsio->kern_rel_offset = 0;
7290 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7291 ctsio->kern_data_ptr;
7292 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7293 rtg_ext_ptr->format_type = 0x10;
7294 rtg_ext_ptr->implicit_transition_time = 0;
7295 tpg_desc = &rtg_ext_ptr->groups[0];
7297 rtg_ptr = (struct scsi_target_group_data *)
7298 ctsio->kern_data_ptr;
7299 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7300 tpg_desc = &rtg_ptr->groups[0];
7303 mtx_lock(&softc->ctl_lock);
7304 pg = softc->port_offset / CTL_MAX_PORTS;
7305 if (softc->flags & CTL_FLAG_ACTIVE_SHELF) {
7306 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) {
7307 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7308 os = TPG_ASYMMETRIC_ACCESS_STANDBY;
7309 } else if (lun->flags & CTL_LUN_PRIMARY_SC) {
7310 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7311 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7313 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7314 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7317 gs = TPG_ASYMMETRIC_ACCESS_STANDBY;
7318 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7320 for (g = 0; g < num_target_port_groups; g++) {
7321 tpg_desc->pref_state = (g == pg) ? gs : os;
7322 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP;
7323 scsi_ulto2b(g + 1, tpg_desc->target_port_group);
7324 tpg_desc->status = TPG_IMPLICIT;
7326 STAILQ_FOREACH(port, &softc->port_list, links) {
7327 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7329 if (ctl_map_lun_back(port->targ_port, lun->lun) >=
7332 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS;
7333 scsi_ulto2b(p, tpg_desc->descriptors[pc].
7334 relative_target_port_identifier);
7337 tpg_desc->target_port_count = pc;
7338 tpg_desc = (struct scsi_target_port_group_descriptor *)
7339 &tpg_desc->descriptors[pc];
7341 mtx_unlock(&softc->ctl_lock);
7343 ctl_set_success(ctsio);
7344 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7345 ctsio->be_move_done = ctl_config_move_done;
7346 ctl_datamove((union ctl_io *)ctsio);
7351 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7353 struct ctl_lun *lun;
7354 struct scsi_report_supported_opcodes *cdb;
7355 const struct ctl_cmd_entry *entry, *sentry;
7356 struct scsi_report_supported_opcodes_all *all;
7357 struct scsi_report_supported_opcodes_descr *descr;
7358 struct scsi_report_supported_opcodes_one *one;
7360 int alloc_len, total_len;
7361 int opcode, service_action, i, j, num;
7363 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7365 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7366 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7368 retval = CTL_RETVAL_COMPLETE;
7370 opcode = cdb->requested_opcode;
7371 service_action = scsi_2btoul(cdb->requested_service_action);
7372 switch (cdb->options & RSO_OPTIONS_MASK) {
7373 case RSO_OPTIONS_ALL:
7375 for (i = 0; i < 256; i++) {
7376 entry = &ctl_cmd_table[i];
7377 if (entry->flags & CTL_CMD_FLAG_SA5) {
7378 for (j = 0; j < 32; j++) {
7379 sentry = &((const struct ctl_cmd_entry *)
7381 if (ctl_cmd_applicable(
7382 lun->be_lun->lun_type, sentry))
7386 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7391 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7392 num * sizeof(struct scsi_report_supported_opcodes_descr);
7394 case RSO_OPTIONS_OC:
7395 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7396 ctl_set_invalid_field(/*ctsio*/ ctsio,
7402 ctl_done((union ctl_io *)ctsio);
7403 return (CTL_RETVAL_COMPLETE);
7405 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7407 case RSO_OPTIONS_OC_SA:
7408 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7409 service_action >= 32) {
7410 ctl_set_invalid_field(/*ctsio*/ ctsio,
7416 ctl_done((union ctl_io *)ctsio);
7417 return (CTL_RETVAL_COMPLETE);
7419 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7422 ctl_set_invalid_field(/*ctsio*/ ctsio,
7428 ctl_done((union ctl_io *)ctsio);
7429 return (CTL_RETVAL_COMPLETE);
7432 alloc_len = scsi_4btoul(cdb->length);
7434 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7436 ctsio->kern_sg_entries = 0;
7438 if (total_len < alloc_len) {
7439 ctsio->residual = alloc_len - total_len;
7440 ctsio->kern_data_len = total_len;
7441 ctsio->kern_total_len = total_len;
7443 ctsio->residual = 0;
7444 ctsio->kern_data_len = alloc_len;
7445 ctsio->kern_total_len = alloc_len;
7447 ctsio->kern_data_resid = 0;
7448 ctsio->kern_rel_offset = 0;
7450 switch (cdb->options & RSO_OPTIONS_MASK) {
7451 case RSO_OPTIONS_ALL:
7452 all = (struct scsi_report_supported_opcodes_all *)
7453 ctsio->kern_data_ptr;
7455 for (i = 0; i < 256; i++) {
7456 entry = &ctl_cmd_table[i];
7457 if (entry->flags & CTL_CMD_FLAG_SA5) {
7458 for (j = 0; j < 32; j++) {
7459 sentry = &((const struct ctl_cmd_entry *)
7461 if (!ctl_cmd_applicable(
7462 lun->be_lun->lun_type, sentry))
7464 descr = &all->descr[num++];
7466 scsi_ulto2b(j, descr->service_action);
7467 descr->flags = RSO_SERVACTV;
7468 scsi_ulto2b(sentry->length,
7472 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7475 descr = &all->descr[num++];
7477 scsi_ulto2b(0, descr->service_action);
7479 scsi_ulto2b(entry->length, descr->cdb_length);
7483 num * sizeof(struct scsi_report_supported_opcodes_descr),
7486 case RSO_OPTIONS_OC:
7487 one = (struct scsi_report_supported_opcodes_one *)
7488 ctsio->kern_data_ptr;
7489 entry = &ctl_cmd_table[opcode];
7491 case RSO_OPTIONS_OC_SA:
7492 one = (struct scsi_report_supported_opcodes_one *)
7493 ctsio->kern_data_ptr;
7494 entry = &ctl_cmd_table[opcode];
7495 entry = &((const struct ctl_cmd_entry *)
7496 entry->execute)[service_action];
7498 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7500 scsi_ulto2b(entry->length, one->cdb_length);
7501 one->cdb_usage[0] = opcode;
7502 memcpy(&one->cdb_usage[1], entry->usage,
7509 ctl_set_success(ctsio);
7510 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7511 ctsio->be_move_done = ctl_config_move_done;
7512 ctl_datamove((union ctl_io *)ctsio);
7517 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7519 struct scsi_report_supported_tmf *cdb;
7520 struct scsi_report_supported_tmf_data *data;
7522 int alloc_len, total_len;
7524 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7526 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7528 retval = CTL_RETVAL_COMPLETE;
7530 total_len = sizeof(struct scsi_report_supported_tmf_data);
7531 alloc_len = scsi_4btoul(cdb->length);
7533 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7535 ctsio->kern_sg_entries = 0;
7537 if (total_len < alloc_len) {
7538 ctsio->residual = alloc_len - total_len;
7539 ctsio->kern_data_len = total_len;
7540 ctsio->kern_total_len = total_len;
7542 ctsio->residual = 0;
7543 ctsio->kern_data_len = alloc_len;
7544 ctsio->kern_total_len = alloc_len;
7546 ctsio->kern_data_resid = 0;
7547 ctsio->kern_rel_offset = 0;
7549 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr;
7550 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS;
7551 data->byte2 |= RST_ITNRS;
7553 ctl_set_success(ctsio);
7554 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7555 ctsio->be_move_done = ctl_config_move_done;
7556 ctl_datamove((union ctl_io *)ctsio);
7561 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7563 struct scsi_report_timestamp *cdb;
7564 struct scsi_report_timestamp_data *data;
7568 int alloc_len, total_len;
7570 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7572 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7574 retval = CTL_RETVAL_COMPLETE;
7576 total_len = sizeof(struct scsi_report_timestamp_data);
7577 alloc_len = scsi_4btoul(cdb->length);
7579 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7581 ctsio->kern_sg_entries = 0;
7583 if (total_len < alloc_len) {
7584 ctsio->residual = alloc_len - total_len;
7585 ctsio->kern_data_len = total_len;
7586 ctsio->kern_total_len = total_len;
7588 ctsio->residual = 0;
7589 ctsio->kern_data_len = alloc_len;
7590 ctsio->kern_total_len = alloc_len;
7592 ctsio->kern_data_resid = 0;
7593 ctsio->kern_rel_offset = 0;
7595 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7596 scsi_ulto2b(sizeof(*data) - 2, data->length);
7597 data->origin = RTS_ORIG_OUTSIDE;
7599 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7600 scsi_ulto4b(timestamp >> 16, data->timestamp);
7601 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7603 ctl_set_success(ctsio);
7604 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7605 ctsio->be_move_done = ctl_config_move_done;
7606 ctl_datamove((union ctl_io *)ctsio);
7611 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7613 struct scsi_per_res_in *cdb;
7614 int alloc_len, total_len = 0;
7615 /* struct scsi_per_res_in_rsrv in_data; */
7616 struct ctl_lun *lun;
7617 struct ctl_softc *softc;
7619 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7621 softc = control_softc;
7623 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7625 alloc_len = scsi_2btoul(cdb->length);
7627 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7630 mtx_lock(&lun->lun_lock);
7631 switch (cdb->action) {
7632 case SPRI_RK: /* read keys */
7633 total_len = sizeof(struct scsi_per_res_in_keys) +
7635 sizeof(struct scsi_per_res_key);
7637 case SPRI_RR: /* read reservation */
7638 if (lun->flags & CTL_LUN_PR_RESERVED)
7639 total_len = sizeof(struct scsi_per_res_in_rsrv);
7641 total_len = sizeof(struct scsi_per_res_in_header);
7643 case SPRI_RC: /* report capabilities */
7644 total_len = sizeof(struct scsi_per_res_cap);
7646 case SPRI_RS: /* read full status */
7647 total_len = sizeof(struct scsi_per_res_in_header) +
7648 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7652 panic("Invalid PR type %x", cdb->action);
7654 mtx_unlock(&lun->lun_lock);
7656 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7658 if (total_len < alloc_len) {
7659 ctsio->residual = alloc_len - total_len;
7660 ctsio->kern_data_len = total_len;
7661 ctsio->kern_total_len = total_len;
7663 ctsio->residual = 0;
7664 ctsio->kern_data_len = alloc_len;
7665 ctsio->kern_total_len = alloc_len;
7668 ctsio->kern_data_resid = 0;
7669 ctsio->kern_rel_offset = 0;
7670 ctsio->kern_sg_entries = 0;
7672 mtx_lock(&lun->lun_lock);
7673 switch (cdb->action) {
7674 case SPRI_RK: { // read keys
7675 struct scsi_per_res_in_keys *res_keys;
7678 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7681 * We had to drop the lock to allocate our buffer, which
7682 * leaves time for someone to come in with another
7683 * persistent reservation. (That is unlikely, though,
7684 * since this should be the only persistent reservation
7685 * command active right now.)
7687 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7688 (lun->pr_key_count *
7689 sizeof(struct scsi_per_res_key)))){
7690 mtx_unlock(&lun->lun_lock);
7691 free(ctsio->kern_data_ptr, M_CTL);
7692 printf("%s: reservation length changed, retrying\n",
7697 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7699 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7700 lun->pr_key_count, res_keys->header.length);
7702 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7703 if (lun->pr_keys[i] == 0)
7707 * We used lun->pr_key_count to calculate the
7708 * size to allocate. If it turns out the number of
7709 * initiators with the registered flag set is
7710 * larger than that (i.e. they haven't been kept in
7711 * sync), we've got a problem.
7713 if (key_count >= lun->pr_key_count) {
7715 csevent_log(CSC_CTL | CSC_SHELF_SW |
7717 csevent_LogType_Fault,
7718 csevent_AlertLevel_Yellow,
7719 csevent_FRU_ShelfController,
7720 csevent_FRU_Firmware,
7721 csevent_FRU_Unknown,
7722 "registered keys %d >= key "
7723 "count %d", key_count,
7729 scsi_u64to8b(lun->pr_keys[i],
7730 res_keys->keys[key_count].key);
7735 case SPRI_RR: { // read reservation
7736 struct scsi_per_res_in_rsrv *res;
7737 int tmp_len, header_only;
7739 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7741 scsi_ulto4b(lun->PRGeneration, res->header.generation);
7743 if (lun->flags & CTL_LUN_PR_RESERVED)
7745 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7746 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7747 res->header.length);
7750 tmp_len = sizeof(struct scsi_per_res_in_header);
7751 scsi_ulto4b(0, res->header.length);
7756 * We had to drop the lock to allocate our buffer, which
7757 * leaves time for someone to come in with another
7758 * persistent reservation. (That is unlikely, though,
7759 * since this should be the only persistent reservation
7760 * command active right now.)
7762 if (tmp_len != total_len) {
7763 mtx_unlock(&lun->lun_lock);
7764 free(ctsio->kern_data_ptr, M_CTL);
7765 printf("%s: reservation status changed, retrying\n",
7771 * No reservation held, so we're done.
7773 if (header_only != 0)
7777 * If the registration is an All Registrants type, the key
7778 * is 0, since it doesn't really matter.
7780 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7781 scsi_u64to8b(lun->pr_keys[lun->pr_res_idx],
7782 res->data.reservation);
7784 res->data.scopetype = lun->res_type;
7787 case SPRI_RC: //report capabilities
7789 struct scsi_per_res_cap *res_cap;
7792 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7793 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7794 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5;
7795 type_mask = SPRI_TM_WR_EX_AR |
7801 scsi_ulto2b(type_mask, res_cap->type_mask);
7804 case SPRI_RS: { // read full status
7805 struct scsi_per_res_in_full *res_status;
7806 struct scsi_per_res_in_full_desc *res_desc;
7807 struct ctl_port *port;
7810 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7813 * We had to drop the lock to allocate our buffer, which
7814 * leaves time for someone to come in with another
7815 * persistent reservation. (That is unlikely, though,
7816 * since this should be the only persistent reservation
7817 * command active right now.)
7819 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7820 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7821 lun->pr_key_count)){
7822 mtx_unlock(&lun->lun_lock);
7823 free(ctsio->kern_data_ptr, M_CTL);
7824 printf("%s: reservation length changed, retrying\n",
7829 scsi_ulto4b(lun->PRGeneration, res_status->header.generation);
7831 res_desc = &res_status->desc[0];
7832 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7833 if (lun->pr_keys[i] == 0)
7836 scsi_u64to8b(lun->pr_keys[i], res_desc->res_key.key);
7837 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7838 (lun->pr_res_idx == i ||
7839 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7840 res_desc->flags = SPRI_FULL_R_HOLDER;
7841 res_desc->scopetype = lun->res_type;
7843 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7844 res_desc->rel_trgt_port_id);
7846 port = softc->ctl_ports[
7847 ctl_port_idx(i / CTL_MAX_INIT_PER_PORT)];
7849 len = ctl_create_iid(port,
7850 i % CTL_MAX_INIT_PER_PORT,
7851 res_desc->transport_id);
7852 scsi_ulto4b(len, res_desc->additional_length);
7853 res_desc = (struct scsi_per_res_in_full_desc *)
7854 &res_desc->transport_id[len];
7856 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7857 res_status->header.length);
7862 * This is a bug, because we just checked for this above,
7863 * and should have returned an error.
7865 panic("Invalid PR type %x", cdb->action);
7866 break; /* NOTREACHED */
7868 mtx_unlock(&lun->lun_lock);
7870 ctl_set_success(ctsio);
7871 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7872 ctsio->be_move_done = ctl_config_move_done;
7873 ctl_datamove((union ctl_io *)ctsio);
7874 return (CTL_RETVAL_COMPLETE);
7878 ctl_set_res_ua(struct ctl_lun *lun, uint32_t residx, ctl_ua_type ua)
7880 int off = lun->ctl_softc->persis_offset;
7882 if (residx >= off && residx < off + CTL_MAX_INITIATORS)
7883 lun->pending_ua[residx - off] |= ua;
7887 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7891 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7892 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7893 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7894 struct scsi_per_res_out_parms* param)
7896 union ctl_ha_msg persis_io;
7902 mtx_lock(&lun->lun_lock);
7903 if (sa_res_key == 0) {
7904 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7905 /* validate scope and type */
7906 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7908 mtx_unlock(&lun->lun_lock);
7909 ctl_set_invalid_field(/*ctsio*/ ctsio,
7915 ctl_done((union ctl_io *)ctsio);
7919 if (type>8 || type==2 || type==4 || type==0) {
7920 mtx_unlock(&lun->lun_lock);
7921 ctl_set_invalid_field(/*ctsio*/ ctsio,
7927 ctl_done((union ctl_io *)ctsio);
7932 * Unregister everybody else and build UA for
7935 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7936 if (i == residx || lun->pr_keys[i] == 0)
7939 lun->pr_keys[i] = 0;
7940 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT);
7942 lun->pr_key_count = 1;
7943 lun->res_type = type;
7944 if (lun->res_type != SPR_TYPE_WR_EX_AR
7945 && lun->res_type != SPR_TYPE_EX_AC_AR)
7946 lun->pr_res_idx = residx;
7948 /* send msg to other side */
7949 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7950 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7951 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7952 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7953 persis_io.pr.pr_info.res_type = type;
7954 memcpy(persis_io.pr.pr_info.sa_res_key,
7955 param->serv_act_res_key,
7956 sizeof(param->serv_act_res_key));
7957 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7958 &persis_io, sizeof(persis_io), 0)) >
7959 CTL_HA_STATUS_SUCCESS) {
7960 printf("CTL:Persis Out error returned "
7961 "from ctl_ha_msg_send %d\n",
7965 /* not all registrants */
7966 mtx_unlock(&lun->lun_lock);
7967 free(ctsio->kern_data_ptr, M_CTL);
7968 ctl_set_invalid_field(ctsio,
7974 ctl_done((union ctl_io *)ctsio);
7977 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7978 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7981 if (res_key == sa_res_key) {
7984 * The spec implies this is not good but doesn't
7985 * say what to do. There are two choices either
7986 * generate a res conflict or check condition
7987 * with illegal field in parameter data. Since
7988 * that is what is done when the sa_res_key is
7989 * zero I'll take that approach since this has
7990 * to do with the sa_res_key.
7992 mtx_unlock(&lun->lun_lock);
7993 free(ctsio->kern_data_ptr, M_CTL);
7994 ctl_set_invalid_field(ctsio,
8000 ctl_done((union ctl_io *)ctsio);
8004 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8005 if (lun->pr_keys[i] != sa_res_key)
8009 lun->pr_keys[i] = 0;
8010 lun->pr_key_count--;
8011 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8014 mtx_unlock(&lun->lun_lock);
8015 free(ctsio->kern_data_ptr, M_CTL);
8016 ctl_set_reservation_conflict(ctsio);
8017 ctl_done((union ctl_io *)ctsio);
8018 return (CTL_RETVAL_COMPLETE);
8020 /* send msg to other side */
8021 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8022 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8023 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8024 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8025 persis_io.pr.pr_info.res_type = type;
8026 memcpy(persis_io.pr.pr_info.sa_res_key,
8027 param->serv_act_res_key,
8028 sizeof(param->serv_act_res_key));
8029 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8030 &persis_io, sizeof(persis_io), 0)) >
8031 CTL_HA_STATUS_SUCCESS) {
8032 printf("CTL:Persis Out error returned from "
8033 "ctl_ha_msg_send %d\n", isc_retval);
8036 /* Reserved but not all registrants */
8037 /* sa_res_key is res holder */
8038 if (sa_res_key == lun->pr_keys[lun->pr_res_idx]) {
8039 /* validate scope and type */
8040 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
8042 mtx_unlock(&lun->lun_lock);
8043 ctl_set_invalid_field(/*ctsio*/ ctsio,
8049 ctl_done((union ctl_io *)ctsio);
8053 if (type>8 || type==2 || type==4 || type==0) {
8054 mtx_unlock(&lun->lun_lock);
8055 ctl_set_invalid_field(/*ctsio*/ ctsio,
8061 ctl_done((union ctl_io *)ctsio);
8067 * if sa_res_key != res_key remove all
8068 * registrants w/sa_res_key and generate UA
8069 * for these registrants(Registrations
8070 * Preempted) if it wasn't an exclusive
8071 * reservation generate UA(Reservations
8072 * Preempted) for all other registered nexuses
8073 * if the type has changed. Establish the new
8074 * reservation and holder. If res_key and
8075 * sa_res_key are the same do the above
8076 * except don't unregister the res holder.
8079 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8080 if (i == residx || lun->pr_keys[i] == 0)
8083 if (sa_res_key == lun->pr_keys[i]) {
8084 lun->pr_keys[i] = 0;
8085 lun->pr_key_count--;
8086 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8087 } else if (type != lun->res_type
8088 && (lun->res_type == SPR_TYPE_WR_EX_RO
8089 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
8090 ctl_set_res_ua(lun, i, CTL_UA_RES_RELEASE);
8093 lun->res_type = type;
8094 if (lun->res_type != SPR_TYPE_WR_EX_AR
8095 && lun->res_type != SPR_TYPE_EX_AC_AR)
8096 lun->pr_res_idx = residx;
8098 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8100 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8101 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8102 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8103 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8104 persis_io.pr.pr_info.res_type = type;
8105 memcpy(persis_io.pr.pr_info.sa_res_key,
8106 param->serv_act_res_key,
8107 sizeof(param->serv_act_res_key));
8108 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8109 &persis_io, sizeof(persis_io), 0)) >
8110 CTL_HA_STATUS_SUCCESS) {
8111 printf("CTL:Persis Out error returned "
8112 "from ctl_ha_msg_send %d\n",
8117 * sa_res_key is not the res holder just
8118 * remove registrants
8122 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8123 if (sa_res_key != lun->pr_keys[i])
8127 lun->pr_keys[i] = 0;
8128 lun->pr_key_count--;
8129 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8133 mtx_unlock(&lun->lun_lock);
8134 free(ctsio->kern_data_ptr, M_CTL);
8135 ctl_set_reservation_conflict(ctsio);
8136 ctl_done((union ctl_io *)ctsio);
8139 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8140 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8141 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8142 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8143 persis_io.pr.pr_info.res_type = type;
8144 memcpy(persis_io.pr.pr_info.sa_res_key,
8145 param->serv_act_res_key,
8146 sizeof(param->serv_act_res_key));
8147 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8148 &persis_io, sizeof(persis_io), 0)) >
8149 CTL_HA_STATUS_SUCCESS) {
8150 printf("CTL:Persis Out error returned "
8151 "from ctl_ha_msg_send %d\n",
8157 lun->PRGeneration++;
8158 mtx_unlock(&lun->lun_lock);
8164 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8166 uint64_t sa_res_key;
8169 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8171 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8172 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8173 || sa_res_key != lun->pr_keys[lun->pr_res_idx]) {
8174 if (sa_res_key == 0) {
8176 * Unregister everybody else and build UA for
8179 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8180 if (i == msg->pr.pr_info.residx ||
8181 lun->pr_keys[i] == 0)
8184 lun->pr_keys[i] = 0;
8185 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8188 lun->pr_key_count = 1;
8189 lun->res_type = msg->pr.pr_info.res_type;
8190 if (lun->res_type != SPR_TYPE_WR_EX_AR
8191 && lun->res_type != SPR_TYPE_EX_AC_AR)
8192 lun->pr_res_idx = msg->pr.pr_info.residx;
8194 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8195 if (sa_res_key == lun->pr_keys[i])
8198 lun->pr_keys[i] = 0;
8199 lun->pr_key_count--;
8200 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8204 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8205 if (i == msg->pr.pr_info.residx ||
8206 lun->pr_keys[i] == 0)
8209 if (sa_res_key == lun->pr_keys[i]) {
8210 lun->pr_keys[i] = 0;
8211 lun->pr_key_count--;
8212 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8213 } else if (msg->pr.pr_info.res_type != lun->res_type
8214 && (lun->res_type == SPR_TYPE_WR_EX_RO
8215 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
8216 ctl_set_res_ua(lun, i, CTL_UA_RES_RELEASE);
8219 lun->res_type = msg->pr.pr_info.res_type;
8220 if (lun->res_type != SPR_TYPE_WR_EX_AR
8221 && lun->res_type != SPR_TYPE_EX_AC_AR)
8222 lun->pr_res_idx = msg->pr.pr_info.residx;
8224 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8226 lun->PRGeneration++;
8232 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8236 u_int32_t param_len;
8237 struct scsi_per_res_out *cdb;
8238 struct ctl_lun *lun;
8239 struct scsi_per_res_out_parms* param;
8240 struct ctl_softc *softc;
8242 uint64_t res_key, sa_res_key;
8244 union ctl_ha_msg persis_io;
8247 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8249 retval = CTL_RETVAL_COMPLETE;
8251 softc = control_softc;
8253 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8254 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8257 * We only support whole-LUN scope. The scope & type are ignored for
8258 * register, register and ignore existing key and clear.
8259 * We sometimes ignore scope and type on preempts too!!
8260 * Verify reservation type here as well.
8262 type = cdb->scope_type & SPR_TYPE_MASK;
8263 if ((cdb->action == SPRO_RESERVE)
8264 || (cdb->action == SPRO_RELEASE)) {
8265 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8266 ctl_set_invalid_field(/*ctsio*/ ctsio,
8272 ctl_done((union ctl_io *)ctsio);
8273 return (CTL_RETVAL_COMPLETE);
8276 if (type>8 || type==2 || type==4 || type==0) {
8277 ctl_set_invalid_field(/*ctsio*/ ctsio,
8283 ctl_done((union ctl_io *)ctsio);
8284 return (CTL_RETVAL_COMPLETE);
8288 param_len = scsi_4btoul(cdb->length);
8290 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8291 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8292 ctsio->kern_data_len = param_len;
8293 ctsio->kern_total_len = param_len;
8294 ctsio->kern_data_resid = 0;
8295 ctsio->kern_rel_offset = 0;
8296 ctsio->kern_sg_entries = 0;
8297 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8298 ctsio->be_move_done = ctl_config_move_done;
8299 ctl_datamove((union ctl_io *)ctsio);
8301 return (CTL_RETVAL_COMPLETE);
8304 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8306 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8307 res_key = scsi_8btou64(param->res_key.key);
8308 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8311 * Validate the reservation key here except for SPRO_REG_IGNO
8312 * This must be done for all other service actions
8314 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8315 mtx_lock(&lun->lun_lock);
8316 if (lun->pr_keys[residx] != 0) {
8317 if (res_key != lun->pr_keys[residx]) {
8319 * The current key passed in doesn't match
8320 * the one the initiator previously
8323 mtx_unlock(&lun->lun_lock);
8324 free(ctsio->kern_data_ptr, M_CTL);
8325 ctl_set_reservation_conflict(ctsio);
8326 ctl_done((union ctl_io *)ctsio);
8327 return (CTL_RETVAL_COMPLETE);
8329 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8331 * We are not registered
8333 mtx_unlock(&lun->lun_lock);
8334 free(ctsio->kern_data_ptr, M_CTL);
8335 ctl_set_reservation_conflict(ctsio);
8336 ctl_done((union ctl_io *)ctsio);
8337 return (CTL_RETVAL_COMPLETE);
8338 } else if (res_key != 0) {
8340 * We are not registered and trying to register but
8341 * the register key isn't zero.
8343 mtx_unlock(&lun->lun_lock);
8344 free(ctsio->kern_data_ptr, M_CTL);
8345 ctl_set_reservation_conflict(ctsio);
8346 ctl_done((union ctl_io *)ctsio);
8347 return (CTL_RETVAL_COMPLETE);
8349 mtx_unlock(&lun->lun_lock);
8352 switch (cdb->action & SPRO_ACTION_MASK) {
8354 case SPRO_REG_IGNO: {
8357 printf("Registration received\n");
8361 * We don't support any of these options, as we report in
8362 * the read capabilities request (see
8363 * ctl_persistent_reserve_in(), above).
8365 if ((param->flags & SPR_SPEC_I_PT)
8366 || (param->flags & SPR_ALL_TG_PT)
8367 || (param->flags & SPR_APTPL)) {
8370 if (param->flags & SPR_APTPL)
8372 else if (param->flags & SPR_ALL_TG_PT)
8374 else /* SPR_SPEC_I_PT */
8377 free(ctsio->kern_data_ptr, M_CTL);
8378 ctl_set_invalid_field(ctsio,
8384 ctl_done((union ctl_io *)ctsio);
8385 return (CTL_RETVAL_COMPLETE);
8388 mtx_lock(&lun->lun_lock);
8391 * The initiator wants to clear the
8394 if (sa_res_key == 0) {
8396 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8397 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8398 && lun->pr_keys[residx] == 0)) {
8399 mtx_unlock(&lun->lun_lock);
8403 lun->pr_keys[residx] = 0;
8404 lun->pr_key_count--;
8406 if (residx == lun->pr_res_idx) {
8407 lun->flags &= ~CTL_LUN_PR_RESERVED;
8408 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8410 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8411 || lun->res_type == SPR_TYPE_EX_AC_RO)
8412 && lun->pr_key_count) {
8414 * If the reservation is a registrants
8415 * only type we need to generate a UA
8416 * for other registered inits. The
8417 * sense code should be RESERVATIONS
8421 for (i = 0; i < CTL_MAX_INITIATORS;i++){
8422 if (lun->pr_keys[i +
8423 softc->persis_offset] == 0)
8425 lun->pending_ua[i] |=
8430 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8431 if (lun->pr_key_count==0) {
8432 lun->flags &= ~CTL_LUN_PR_RESERVED;
8434 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8437 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8438 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8439 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8440 persis_io.pr.pr_info.residx = residx;
8441 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8442 &persis_io, sizeof(persis_io), 0 )) >
8443 CTL_HA_STATUS_SUCCESS) {
8444 printf("CTL:Persis Out error returned from "
8445 "ctl_ha_msg_send %d\n", isc_retval);
8447 } else /* sa_res_key != 0 */ {
8450 * If we aren't registered currently then increment
8451 * the key count and set the registered flag.
8453 if (lun->pr_keys[residx] == 0)
8454 lun->pr_key_count++;
8455 lun->pr_keys[residx] = sa_res_key;
8457 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8458 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8459 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8460 persis_io.pr.pr_info.residx = residx;
8461 memcpy(persis_io.pr.pr_info.sa_res_key,
8462 param->serv_act_res_key,
8463 sizeof(param->serv_act_res_key));
8464 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8465 &persis_io, sizeof(persis_io), 0)) >
8466 CTL_HA_STATUS_SUCCESS) {
8467 printf("CTL:Persis Out error returned from "
8468 "ctl_ha_msg_send %d\n", isc_retval);
8471 lun->PRGeneration++;
8472 mtx_unlock(&lun->lun_lock);
8478 printf("Reserve executed type %d\n", type);
8480 mtx_lock(&lun->lun_lock);
8481 if (lun->flags & CTL_LUN_PR_RESERVED) {
8483 * if this isn't the reservation holder and it's
8484 * not a "all registrants" type or if the type is
8485 * different then we have a conflict
8487 if ((lun->pr_res_idx != residx
8488 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8489 || lun->res_type != type) {
8490 mtx_unlock(&lun->lun_lock);
8491 free(ctsio->kern_data_ptr, M_CTL);
8492 ctl_set_reservation_conflict(ctsio);
8493 ctl_done((union ctl_io *)ctsio);
8494 return (CTL_RETVAL_COMPLETE);
8496 mtx_unlock(&lun->lun_lock);
8497 } else /* create a reservation */ {
8499 * If it's not an "all registrants" type record
8500 * reservation holder
8502 if (type != SPR_TYPE_WR_EX_AR
8503 && type != SPR_TYPE_EX_AC_AR)
8504 lun->pr_res_idx = residx; /* Res holder */
8506 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8508 lun->flags |= CTL_LUN_PR_RESERVED;
8509 lun->res_type = type;
8511 mtx_unlock(&lun->lun_lock);
8513 /* send msg to other side */
8514 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8515 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8516 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8517 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8518 persis_io.pr.pr_info.res_type = type;
8519 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8520 &persis_io, sizeof(persis_io), 0)) >
8521 CTL_HA_STATUS_SUCCESS) {
8522 printf("CTL:Persis Out error returned from "
8523 "ctl_ha_msg_send %d\n", isc_retval);
8529 mtx_lock(&lun->lun_lock);
8530 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8531 /* No reservation exists return good status */
8532 mtx_unlock(&lun->lun_lock);
8536 * Is this nexus a reservation holder?
8538 if (lun->pr_res_idx != residx
8539 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8541 * not a res holder return good status but
8544 mtx_unlock(&lun->lun_lock);
8548 if (lun->res_type != type) {
8549 mtx_unlock(&lun->lun_lock);
8550 free(ctsio->kern_data_ptr, M_CTL);
8551 ctl_set_illegal_pr_release(ctsio);
8552 ctl_done((union ctl_io *)ctsio);
8553 return (CTL_RETVAL_COMPLETE);
8556 /* okay to release */
8557 lun->flags &= ~CTL_LUN_PR_RESERVED;
8558 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8562 * if this isn't an exclusive access
8563 * res generate UA for all other
8566 if (type != SPR_TYPE_EX_AC
8567 && type != SPR_TYPE_WR_EX) {
8568 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8570 lun->pr_keys[i + softc->persis_offset] == 0)
8572 lun->pending_ua[i] |= CTL_UA_RES_RELEASE;
8575 mtx_unlock(&lun->lun_lock);
8576 /* Send msg to other side */
8577 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8578 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8579 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8580 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io,
8581 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8582 printf("CTL:Persis Out error returned from "
8583 "ctl_ha_msg_send %d\n", isc_retval);
8588 /* send msg to other side */
8590 mtx_lock(&lun->lun_lock);
8591 lun->flags &= ~CTL_LUN_PR_RESERVED;
8593 lun->pr_key_count = 0;
8594 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8596 lun->pr_keys[residx] = 0;
8598 for (i=0; i < 2*CTL_MAX_INITIATORS; i++)
8599 if (lun->pr_keys[i] != 0) {
8600 lun->pr_keys[i] = 0;
8601 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8603 lun->PRGeneration++;
8604 mtx_unlock(&lun->lun_lock);
8605 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8606 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8607 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8608 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8609 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8610 printf("CTL:Persis Out error returned from "
8611 "ctl_ha_msg_send %d\n", isc_retval);
8616 case SPRO_PRE_ABO: {
8619 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8620 residx, ctsio, cdb, param);
8622 return (CTL_RETVAL_COMPLETE);
8626 panic("Invalid PR type %x", cdb->action);
8630 free(ctsio->kern_data_ptr, M_CTL);
8631 ctl_set_success(ctsio);
8632 ctl_done((union ctl_io *)ctsio);
8638 * This routine is for handling a message from the other SC pertaining to
8639 * persistent reserve out. All the error checking will have been done
8640 * so only perorming the action need be done here to keep the two
8644 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8646 struct ctl_lun *lun;
8647 struct ctl_softc *softc;
8651 softc = control_softc;
8653 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8654 lun = softc->ctl_luns[targ_lun];
8655 mtx_lock(&lun->lun_lock);
8656 switch(msg->pr.pr_info.action) {
8657 case CTL_PR_REG_KEY:
8658 if (lun->pr_keys[msg->pr.pr_info.residx] == 0)
8659 lun->pr_key_count++;
8660 lun->pr_keys[msg->pr.pr_info.residx] =
8661 scsi_8btou64(msg->pr.pr_info.sa_res_key);
8662 lun->PRGeneration++;
8665 case CTL_PR_UNREG_KEY:
8666 lun->pr_keys[msg->pr.pr_info.residx] = 0;
8667 lun->pr_key_count--;
8669 /* XXX Need to see if the reservation has been released */
8670 /* if so do we need to generate UA? */
8671 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8672 lun->flags &= ~CTL_LUN_PR_RESERVED;
8673 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8675 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8676 || lun->res_type == SPR_TYPE_EX_AC_RO)
8677 && lun->pr_key_count) {
8679 * If the reservation is a registrants
8680 * only type we need to generate a UA
8681 * for other registered inits. The
8682 * sense code should be RESERVATIONS
8686 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8687 if (lun->pr_keys[i +
8688 softc->persis_offset] == 0)
8691 lun->pending_ua[i] |=
8696 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8697 if (lun->pr_key_count==0) {
8698 lun->flags &= ~CTL_LUN_PR_RESERVED;
8700 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8703 lun->PRGeneration++;
8706 case CTL_PR_RESERVE:
8707 lun->flags |= CTL_LUN_PR_RESERVED;
8708 lun->res_type = msg->pr.pr_info.res_type;
8709 lun->pr_res_idx = msg->pr.pr_info.residx;
8713 case CTL_PR_RELEASE:
8715 * if this isn't an exclusive access res generate UA for all
8716 * other registrants.
8718 if (lun->res_type != SPR_TYPE_EX_AC
8719 && lun->res_type != SPR_TYPE_WR_EX) {
8720 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8721 if (lun->pr_keys[i + softc->persis_offset] != 0)
8722 lun->pending_ua[i] |=
8726 lun->flags &= ~CTL_LUN_PR_RESERVED;
8727 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8731 case CTL_PR_PREEMPT:
8732 ctl_pro_preempt_other(lun, msg);
8735 lun->flags &= ~CTL_LUN_PR_RESERVED;
8737 lun->pr_key_count = 0;
8738 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8740 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8741 if (lun->pr_keys[i] == 0)
8743 lun->pr_keys[i] = 0;
8744 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8746 lun->PRGeneration++;
8750 mtx_unlock(&lun->lun_lock);
8754 ctl_read_write(struct ctl_scsiio *ctsio)
8756 struct ctl_lun *lun;
8757 struct ctl_lba_len_flags *lbalen;
8759 uint32_t num_blocks;
8763 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8765 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8768 retval = CTL_RETVAL_COMPLETE;
8770 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8771 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8772 switch (ctsio->cdb[0]) {
8775 struct scsi_rw_6 *cdb;
8777 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8779 lba = scsi_3btoul(cdb->addr);
8780 /* only 5 bits are valid in the most significant address byte */
8782 num_blocks = cdb->length;
8784 * This is correct according to SBC-2.
8786 if (num_blocks == 0)
8792 struct scsi_rw_10 *cdb;
8794 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8795 if (cdb->byte2 & SRW10_FUA)
8796 flags |= CTL_LLF_FUA;
8797 if (cdb->byte2 & SRW10_DPO)
8798 flags |= CTL_LLF_DPO;
8799 lba = scsi_4btoul(cdb->addr);
8800 num_blocks = scsi_2btoul(cdb->length);
8803 case WRITE_VERIFY_10: {
8804 struct scsi_write_verify_10 *cdb;
8806 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8807 flags |= CTL_LLF_FUA;
8808 if (cdb->byte2 & SWV_DPO)
8809 flags |= CTL_LLF_DPO;
8810 lba = scsi_4btoul(cdb->addr);
8811 num_blocks = scsi_2btoul(cdb->length);
8816 struct scsi_rw_12 *cdb;
8818 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8819 if (cdb->byte2 & SRW12_FUA)
8820 flags |= CTL_LLF_FUA;
8821 if (cdb->byte2 & SRW12_DPO)
8822 flags |= CTL_LLF_DPO;
8823 lba = scsi_4btoul(cdb->addr);
8824 num_blocks = scsi_4btoul(cdb->length);
8827 case WRITE_VERIFY_12: {
8828 struct scsi_write_verify_12 *cdb;
8830 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8831 flags |= CTL_LLF_FUA;
8832 if (cdb->byte2 & SWV_DPO)
8833 flags |= CTL_LLF_DPO;
8834 lba = scsi_4btoul(cdb->addr);
8835 num_blocks = scsi_4btoul(cdb->length);
8840 struct scsi_rw_16 *cdb;
8842 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8843 if (cdb->byte2 & SRW12_FUA)
8844 flags |= CTL_LLF_FUA;
8845 if (cdb->byte2 & SRW12_DPO)
8846 flags |= CTL_LLF_DPO;
8847 lba = scsi_8btou64(cdb->addr);
8848 num_blocks = scsi_4btoul(cdb->length);
8851 case WRITE_ATOMIC_16: {
8852 struct scsi_rw_16 *cdb;
8854 if (lun->be_lun->atomicblock == 0) {
8855 ctl_set_invalid_opcode(ctsio);
8856 ctl_done((union ctl_io *)ctsio);
8857 return (CTL_RETVAL_COMPLETE);
8860 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8861 if (cdb->byte2 & SRW12_FUA)
8862 flags |= CTL_LLF_FUA;
8863 if (cdb->byte2 & SRW12_DPO)
8864 flags |= CTL_LLF_DPO;
8865 lba = scsi_8btou64(cdb->addr);
8866 num_blocks = scsi_4btoul(cdb->length);
8867 if (num_blocks > lun->be_lun->atomicblock) {
8868 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8869 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8871 ctl_done((union ctl_io *)ctsio);
8872 return (CTL_RETVAL_COMPLETE);
8876 case WRITE_VERIFY_16: {
8877 struct scsi_write_verify_16 *cdb;
8879 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8880 flags |= CTL_LLF_FUA;
8881 if (cdb->byte2 & SWV_DPO)
8882 flags |= CTL_LLF_DPO;
8883 lba = scsi_8btou64(cdb->addr);
8884 num_blocks = scsi_4btoul(cdb->length);
8889 * We got a command we don't support. This shouldn't
8890 * happen, commands should be filtered out above us.
8892 ctl_set_invalid_opcode(ctsio);
8893 ctl_done((union ctl_io *)ctsio);
8895 return (CTL_RETVAL_COMPLETE);
8896 break; /* NOTREACHED */
8900 * The first check is to make sure we're in bounds, the second
8901 * check is to catch wrap-around problems. If the lba + num blocks
8902 * is less than the lba, then we've wrapped around and the block
8903 * range is invalid anyway.
8905 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8906 || ((lba + num_blocks) < lba)) {
8907 ctl_set_lba_out_of_range(ctsio);
8908 ctl_done((union ctl_io *)ctsio);
8909 return (CTL_RETVAL_COMPLETE);
8913 * According to SBC-3, a transfer length of 0 is not an error.
8914 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8915 * translates to 256 blocks for those commands.
8917 if (num_blocks == 0) {
8918 ctl_set_success(ctsio);
8919 ctl_done((union ctl_io *)ctsio);
8920 return (CTL_RETVAL_COMPLETE);
8923 /* Set FUA and/or DPO if caches are disabled. */
8925 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8927 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8929 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8931 flags |= CTL_LLF_FUA;
8934 lbalen = (struct ctl_lba_len_flags *)
8935 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8937 lbalen->len = num_blocks;
8938 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8940 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8941 ctsio->kern_rel_offset = 0;
8943 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8945 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8951 ctl_cnw_cont(union ctl_io *io)
8953 struct ctl_scsiio *ctsio;
8954 struct ctl_lun *lun;
8955 struct ctl_lba_len_flags *lbalen;
8958 ctsio = &io->scsiio;
8959 ctsio->io_hdr.status = CTL_STATUS_NONE;
8960 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8961 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8962 lbalen = (struct ctl_lba_len_flags *)
8963 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8964 lbalen->flags &= ~CTL_LLF_COMPARE;
8965 lbalen->flags |= CTL_LLF_WRITE;
8967 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8968 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8973 ctl_cnw(struct ctl_scsiio *ctsio)
8975 struct ctl_lun *lun;
8976 struct ctl_lba_len_flags *lbalen;
8978 uint32_t num_blocks;
8981 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8983 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8986 retval = CTL_RETVAL_COMPLETE;
8988 switch (ctsio->cdb[0]) {
8989 case COMPARE_AND_WRITE: {
8990 struct scsi_compare_and_write *cdb;
8992 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8993 if (cdb->byte2 & SRW10_FUA)
8994 flags |= CTL_LLF_FUA;
8995 if (cdb->byte2 & SRW10_DPO)
8996 flags |= CTL_LLF_DPO;
8997 lba = scsi_8btou64(cdb->addr);
8998 num_blocks = cdb->length;
9003 * We got a command we don't support. This shouldn't
9004 * happen, commands should be filtered out above us.
9006 ctl_set_invalid_opcode(ctsio);
9007 ctl_done((union ctl_io *)ctsio);
9009 return (CTL_RETVAL_COMPLETE);
9010 break; /* NOTREACHED */
9014 * The first check is to make sure we're in bounds, the second
9015 * check is to catch wrap-around problems. If the lba + num blocks
9016 * is less than the lba, then we've wrapped around and the block
9017 * range is invalid anyway.
9019 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9020 || ((lba + num_blocks) < lba)) {
9021 ctl_set_lba_out_of_range(ctsio);
9022 ctl_done((union ctl_io *)ctsio);
9023 return (CTL_RETVAL_COMPLETE);
9027 * According to SBC-3, a transfer length of 0 is not an error.
9029 if (num_blocks == 0) {
9030 ctl_set_success(ctsio);
9031 ctl_done((union ctl_io *)ctsio);
9032 return (CTL_RETVAL_COMPLETE);
9035 /* Set FUA if write cache is disabled. */
9036 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9038 flags |= CTL_LLF_FUA;
9040 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
9041 ctsio->kern_rel_offset = 0;
9044 * Set the IO_CONT flag, so that if this I/O gets passed to
9045 * ctl_data_submit_done(), it'll get passed back to
9046 * ctl_ctl_cnw_cont() for further processing.
9048 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
9049 ctsio->io_cont = ctl_cnw_cont;
9051 lbalen = (struct ctl_lba_len_flags *)
9052 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9054 lbalen->len = num_blocks;
9055 lbalen->flags = CTL_LLF_COMPARE | flags;
9057 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
9058 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9063 ctl_verify(struct ctl_scsiio *ctsio)
9065 struct ctl_lun *lun;
9066 struct ctl_lba_len_flags *lbalen;
9068 uint32_t num_blocks;
9072 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9074 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
9077 flags = CTL_LLF_FUA;
9078 retval = CTL_RETVAL_COMPLETE;
9080 switch (ctsio->cdb[0]) {
9082 struct scsi_verify_10 *cdb;
9084 cdb = (struct scsi_verify_10 *)ctsio->cdb;
9085 if (cdb->byte2 & SVFY_BYTCHK)
9087 if (cdb->byte2 & SVFY_DPO)
9088 flags |= CTL_LLF_DPO;
9089 lba = scsi_4btoul(cdb->addr);
9090 num_blocks = scsi_2btoul(cdb->length);
9094 struct scsi_verify_12 *cdb;
9096 cdb = (struct scsi_verify_12 *)ctsio->cdb;
9097 if (cdb->byte2 & SVFY_BYTCHK)
9099 if (cdb->byte2 & SVFY_DPO)
9100 flags |= CTL_LLF_DPO;
9101 lba = scsi_4btoul(cdb->addr);
9102 num_blocks = scsi_4btoul(cdb->length);
9106 struct scsi_rw_16 *cdb;
9108 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9109 if (cdb->byte2 & SVFY_BYTCHK)
9111 if (cdb->byte2 & SVFY_DPO)
9112 flags |= CTL_LLF_DPO;
9113 lba = scsi_8btou64(cdb->addr);
9114 num_blocks = scsi_4btoul(cdb->length);
9119 * We got a command we don't support. This shouldn't
9120 * happen, commands should be filtered out above us.
9122 ctl_set_invalid_opcode(ctsio);
9123 ctl_done((union ctl_io *)ctsio);
9124 return (CTL_RETVAL_COMPLETE);
9128 * The first check is to make sure we're in bounds, the second
9129 * check is to catch wrap-around problems. If the lba + num blocks
9130 * is less than the lba, then we've wrapped around and the block
9131 * range is invalid anyway.
9133 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9134 || ((lba + num_blocks) < lba)) {
9135 ctl_set_lba_out_of_range(ctsio);
9136 ctl_done((union ctl_io *)ctsio);
9137 return (CTL_RETVAL_COMPLETE);
9141 * According to SBC-3, a transfer length of 0 is not an error.
9143 if (num_blocks == 0) {
9144 ctl_set_success(ctsio);
9145 ctl_done((union ctl_io *)ctsio);
9146 return (CTL_RETVAL_COMPLETE);
9149 lbalen = (struct ctl_lba_len_flags *)
9150 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9152 lbalen->len = num_blocks;
9154 lbalen->flags = CTL_LLF_COMPARE | flags;
9155 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9157 lbalen->flags = CTL_LLF_VERIFY | flags;
9158 ctsio->kern_total_len = 0;
9160 ctsio->kern_rel_offset = 0;
9162 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9163 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9168 ctl_report_luns(struct ctl_scsiio *ctsio)
9170 struct scsi_report_luns *cdb;
9171 struct scsi_report_luns_data *lun_data;
9172 struct ctl_lun *lun, *request_lun;
9173 int num_luns, retval;
9174 uint32_t alloc_len, lun_datalen;
9175 int num_filled, well_known;
9176 uint32_t initidx, targ_lun_id, lun_id;
9178 retval = CTL_RETVAL_COMPLETE;
9181 cdb = (struct scsi_report_luns *)ctsio->cdb;
9183 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9185 mtx_lock(&control_softc->ctl_lock);
9186 num_luns = control_softc->num_luns;
9187 mtx_unlock(&control_softc->ctl_lock);
9189 switch (cdb->select_report) {
9190 case RPL_REPORT_DEFAULT:
9191 case RPL_REPORT_ALL:
9193 case RPL_REPORT_WELLKNOWN:
9198 ctl_set_invalid_field(ctsio,
9204 ctl_done((union ctl_io *)ctsio);
9206 break; /* NOTREACHED */
9209 alloc_len = scsi_4btoul(cdb->length);
9211 * The initiator has to allocate at least 16 bytes for this request,
9212 * so he can at least get the header and the first LUN. Otherwise
9213 * we reject the request (per SPC-3 rev 14, section 6.21).
9215 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9216 sizeof(struct scsi_report_luns_lundata))) {
9217 ctl_set_invalid_field(ctsio,
9223 ctl_done((union ctl_io *)ctsio);
9227 request_lun = (struct ctl_lun *)
9228 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9230 lun_datalen = sizeof(*lun_data) +
9231 (num_luns * sizeof(struct scsi_report_luns_lundata));
9233 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9234 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9235 ctsio->kern_sg_entries = 0;
9237 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9239 mtx_lock(&control_softc->ctl_lock);
9240 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9241 lun_id = ctl_map_lun(ctsio->io_hdr.nexus.targ_port, targ_lun_id);
9242 if (lun_id >= CTL_MAX_LUNS)
9244 lun = control_softc->ctl_luns[lun_id];
9248 if (targ_lun_id <= 0xff) {
9250 * Peripheral addressing method, bus number 0.
9252 lun_data->luns[num_filled].lundata[0] =
9253 RPL_LUNDATA_ATYP_PERIPH;
9254 lun_data->luns[num_filled].lundata[1] = targ_lun_id;
9256 } else if (targ_lun_id <= 0x3fff) {
9258 * Flat addressing method.
9260 lun_data->luns[num_filled].lundata[0] =
9261 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8);
9262 lun_data->luns[num_filled].lundata[1] =
9263 (targ_lun_id & 0xff);
9265 } else if (targ_lun_id <= 0xffffff) {
9267 * Extended flat addressing method.
9269 lun_data->luns[num_filled].lundata[0] =
9270 RPL_LUNDATA_ATYP_EXTLUN | 0x12;
9271 scsi_ulto3b(targ_lun_id,
9272 &lun_data->luns[num_filled].lundata[1]);
9275 printf("ctl_report_luns: bogus LUN number %jd, "
9276 "skipping\n", (intmax_t)targ_lun_id);
9279 * According to SPC-3, rev 14 section 6.21:
9281 * "The execution of a REPORT LUNS command to any valid and
9282 * installed logical unit shall clear the REPORTED LUNS DATA
9283 * HAS CHANGED unit attention condition for all logical
9284 * units of that target with respect to the requesting
9285 * initiator. A valid and installed logical unit is one
9286 * having a PERIPHERAL QUALIFIER of 000b in the standard
9287 * INQUIRY data (see 6.4.2)."
9289 * If request_lun is NULL, the LUN this report luns command
9290 * was issued to is either disabled or doesn't exist. In that
9291 * case, we shouldn't clear any pending lun change unit
9294 if (request_lun != NULL) {
9295 mtx_lock(&lun->lun_lock);
9296 lun->pending_ua[initidx] &= ~CTL_UA_LUN_CHANGE;
9297 mtx_unlock(&lun->lun_lock);
9300 mtx_unlock(&control_softc->ctl_lock);
9303 * It's quite possible that we've returned fewer LUNs than we allocated
9304 * space for. Trim it.
9306 lun_datalen = sizeof(*lun_data) +
9307 (num_filled * sizeof(struct scsi_report_luns_lundata));
9309 if (lun_datalen < alloc_len) {
9310 ctsio->residual = alloc_len - lun_datalen;
9311 ctsio->kern_data_len = lun_datalen;
9312 ctsio->kern_total_len = lun_datalen;
9314 ctsio->residual = 0;
9315 ctsio->kern_data_len = alloc_len;
9316 ctsio->kern_total_len = alloc_len;
9318 ctsio->kern_data_resid = 0;
9319 ctsio->kern_rel_offset = 0;
9320 ctsio->kern_sg_entries = 0;
9323 * We set this to the actual data length, regardless of how much
9324 * space we actually have to return results. If the user looks at
9325 * this value, he'll know whether or not he allocated enough space
9326 * and reissue the command if necessary. We don't support well
9327 * known logical units, so if the user asks for that, return none.
9329 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9332 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9335 ctl_set_success(ctsio);
9336 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9337 ctsio->be_move_done = ctl_config_move_done;
9338 ctl_datamove((union ctl_io *)ctsio);
9343 ctl_request_sense(struct ctl_scsiio *ctsio)
9345 struct scsi_request_sense *cdb;
9346 struct scsi_sense_data *sense_ptr;
9347 struct ctl_lun *lun;
9350 scsi_sense_data_type sense_format;
9352 cdb = (struct scsi_request_sense *)ctsio->cdb;
9354 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9356 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9359 * Determine which sense format the user wants.
9361 if (cdb->byte2 & SRS_DESC)
9362 sense_format = SSD_TYPE_DESC;
9364 sense_format = SSD_TYPE_FIXED;
9366 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9367 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9368 ctsio->kern_sg_entries = 0;
9371 * struct scsi_sense_data, which is currently set to 256 bytes, is
9372 * larger than the largest allowed value for the length field in the
9373 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9375 ctsio->residual = 0;
9376 ctsio->kern_data_len = cdb->length;
9377 ctsio->kern_total_len = cdb->length;
9379 ctsio->kern_data_resid = 0;
9380 ctsio->kern_rel_offset = 0;
9381 ctsio->kern_sg_entries = 0;
9384 * If we don't have a LUN, we don't have any pending sense.
9390 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9392 * Check for pending sense, and then for pending unit attentions.
9393 * Pending sense gets returned first, then pending unit attentions.
9395 mtx_lock(&lun->lun_lock);
9397 if (ctl_is_set(lun->have_ca, initidx)) {
9398 scsi_sense_data_type stored_format;
9401 * Check to see which sense format was used for the stored
9404 stored_format = scsi_sense_type(&lun->pending_sense[initidx]);
9407 * If the user requested a different sense format than the
9408 * one we stored, then we need to convert it to the other
9409 * format. If we're going from descriptor to fixed format
9410 * sense data, we may lose things in translation, depending
9411 * on what options were used.
9413 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9414 * for some reason we'll just copy it out as-is.
9416 if ((stored_format == SSD_TYPE_FIXED)
9417 && (sense_format == SSD_TYPE_DESC))
9418 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9419 &lun->pending_sense[initidx],
9420 (struct scsi_sense_data_desc *)sense_ptr);
9421 else if ((stored_format == SSD_TYPE_DESC)
9422 && (sense_format == SSD_TYPE_FIXED))
9423 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9424 &lun->pending_sense[initidx],
9425 (struct scsi_sense_data_fixed *)sense_ptr);
9427 memcpy(sense_ptr, &lun->pending_sense[initidx],
9428 ctl_min(sizeof(*sense_ptr),
9429 sizeof(lun->pending_sense[initidx])));
9431 ctl_clear_mask(lun->have_ca, initidx);
9435 if (lun->pending_ua[initidx] != CTL_UA_NONE) {
9436 ctl_ua_type ua_type;
9438 ua_type = ctl_build_ua(&lun->pending_ua[initidx],
9439 sense_ptr, sense_format);
9440 if (ua_type != CTL_UA_NONE)
9443 mtx_unlock(&lun->lun_lock);
9446 * We already have a pending error, return it.
9448 if (have_error != 0) {
9450 * We report the SCSI status as OK, since the status of the
9451 * request sense command itself is OK.
9452 * We report 0 for the sense length, because we aren't doing
9453 * autosense in this case. We're reporting sense as
9456 ctl_set_success(ctsio);
9457 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9458 ctsio->be_move_done = ctl_config_move_done;
9459 ctl_datamove((union ctl_io *)ctsio);
9460 return (CTL_RETVAL_COMPLETE);
9466 * No sense information to report, so we report that everything is
9469 ctl_set_sense_data(sense_ptr,
9472 /*current_error*/ 1,
9473 /*sense_key*/ SSD_KEY_NO_SENSE,
9479 * We report 0 for the sense length, because we aren't doing
9480 * autosense in this case. We're reporting sense as parameter data.
9482 ctl_set_success(ctsio);
9483 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9484 ctsio->be_move_done = ctl_config_move_done;
9485 ctl_datamove((union ctl_io *)ctsio);
9486 return (CTL_RETVAL_COMPLETE);
9490 ctl_tur(struct ctl_scsiio *ctsio)
9493 CTL_DEBUG_PRINT(("ctl_tur\n"));
9495 ctl_set_success(ctsio);
9496 ctl_done((union ctl_io *)ctsio);
9498 return (CTL_RETVAL_COMPLETE);
9503 ctl_cmddt_inquiry(struct ctl_scsiio *ctsio)
9510 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9512 struct scsi_vpd_supported_pages *pages;
9514 struct ctl_lun *lun;
9516 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9518 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9519 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9520 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9521 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9522 ctsio->kern_sg_entries = 0;
9524 if (sup_page_size < alloc_len) {
9525 ctsio->residual = alloc_len - sup_page_size;
9526 ctsio->kern_data_len = sup_page_size;
9527 ctsio->kern_total_len = sup_page_size;
9529 ctsio->residual = 0;
9530 ctsio->kern_data_len = alloc_len;
9531 ctsio->kern_total_len = alloc_len;
9533 ctsio->kern_data_resid = 0;
9534 ctsio->kern_rel_offset = 0;
9535 ctsio->kern_sg_entries = 0;
9538 * The control device is always connected. The disk device, on the
9539 * other hand, may not be online all the time. Need to change this
9540 * to figure out whether the disk device is actually online or not.
9543 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9544 lun->be_lun->lun_type;
9546 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9548 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES;
9549 /* Supported VPD pages */
9550 pages->page_list[0] = SVPD_SUPPORTED_PAGES;
9552 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER;
9553 /* Device Identification */
9554 pages->page_list[2] = SVPD_DEVICE_ID;
9555 /* Extended INQUIRY Data */
9556 pages->page_list[3] = SVPD_EXTENDED_INQUIRY_DATA;
9557 /* Mode Page Policy */
9558 pages->page_list[4] = SVPD_MODE_PAGE_POLICY;
9560 pages->page_list[5] = SVPD_SCSI_PORTS;
9561 /* Third-party Copy */
9562 pages->page_list[6] = SVPD_SCSI_TPC;
9564 pages->page_list[7] = SVPD_BLOCK_LIMITS;
9565 /* Block Device Characteristics */
9566 pages->page_list[8] = SVPD_BDC;
9567 /* Logical Block Provisioning */
9568 pages->page_list[9] = SVPD_LBP;
9570 ctl_set_success(ctsio);
9571 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9572 ctsio->be_move_done = ctl_config_move_done;
9573 ctl_datamove((union ctl_io *)ctsio);
9574 return (CTL_RETVAL_COMPLETE);
9578 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9580 struct scsi_vpd_unit_serial_number *sn_ptr;
9581 struct ctl_lun *lun;
9584 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9586 data_len = 4 + CTL_SN_LEN;
9587 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9588 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9589 if (data_len < alloc_len) {
9590 ctsio->residual = alloc_len - data_len;
9591 ctsio->kern_data_len = data_len;
9592 ctsio->kern_total_len = data_len;
9594 ctsio->residual = 0;
9595 ctsio->kern_data_len = alloc_len;
9596 ctsio->kern_total_len = alloc_len;
9598 ctsio->kern_data_resid = 0;
9599 ctsio->kern_rel_offset = 0;
9600 ctsio->kern_sg_entries = 0;
9603 * The control device is always connected. The disk device, on the
9604 * other hand, may not be online all the time. Need to change this
9605 * to figure out whether the disk device is actually online or not.
9608 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9609 lun->be_lun->lun_type;
9611 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9613 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9614 sn_ptr->length = CTL_SN_LEN;
9616 * If we don't have a LUN, we just leave the serial number as
9620 strncpy((char *)sn_ptr->serial_num,
9621 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9623 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9625 ctl_set_success(ctsio);
9626 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9627 ctsio->be_move_done = ctl_config_move_done;
9628 ctl_datamove((union ctl_io *)ctsio);
9629 return (CTL_RETVAL_COMPLETE);
9634 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9636 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9637 struct ctl_lun *lun;
9640 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9642 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9643 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9644 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9645 ctsio->kern_sg_entries = 0;
9647 if (data_len < alloc_len) {
9648 ctsio->residual = alloc_len - data_len;
9649 ctsio->kern_data_len = data_len;
9650 ctsio->kern_total_len = data_len;
9652 ctsio->residual = 0;
9653 ctsio->kern_data_len = alloc_len;
9654 ctsio->kern_total_len = alloc_len;
9656 ctsio->kern_data_resid = 0;
9657 ctsio->kern_rel_offset = 0;
9658 ctsio->kern_sg_entries = 0;
9661 * The control device is always connected. The disk device, on the
9662 * other hand, may not be online all the time.
9665 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9666 lun->be_lun->lun_type;
9668 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9669 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9670 eid_ptr->page_length = data_len - 4;
9671 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9672 eid_ptr->flags3 = SVPD_EID_V_SUP;
9674 ctl_set_success(ctsio);
9675 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9676 ctsio->be_move_done = ctl_config_move_done;
9677 ctl_datamove((union ctl_io *)ctsio);
9678 return (CTL_RETVAL_COMPLETE);
9682 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9684 struct scsi_vpd_mode_page_policy *mpp_ptr;
9685 struct ctl_lun *lun;
9688 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9690 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9691 sizeof(struct scsi_vpd_mode_page_policy_descr);
9693 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9694 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9695 ctsio->kern_sg_entries = 0;
9697 if (data_len < alloc_len) {
9698 ctsio->residual = alloc_len - data_len;
9699 ctsio->kern_data_len = data_len;
9700 ctsio->kern_total_len = data_len;
9702 ctsio->residual = 0;
9703 ctsio->kern_data_len = alloc_len;
9704 ctsio->kern_total_len = alloc_len;
9706 ctsio->kern_data_resid = 0;
9707 ctsio->kern_rel_offset = 0;
9708 ctsio->kern_sg_entries = 0;
9711 * The control device is always connected. The disk device, on the
9712 * other hand, may not be online all the time.
9715 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9716 lun->be_lun->lun_type;
9718 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9719 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9720 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9721 mpp_ptr->descr[0].page_code = 0x3f;
9722 mpp_ptr->descr[0].subpage_code = 0xff;
9723 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9725 ctl_set_success(ctsio);
9726 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9727 ctsio->be_move_done = ctl_config_move_done;
9728 ctl_datamove((union ctl_io *)ctsio);
9729 return (CTL_RETVAL_COMPLETE);
9733 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9735 struct scsi_vpd_device_id *devid_ptr;
9736 struct scsi_vpd_id_descriptor *desc;
9737 struct ctl_softc *ctl_softc;
9738 struct ctl_lun *lun;
9739 struct ctl_port *port;
9743 ctl_softc = control_softc;
9745 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)];
9746 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9748 data_len = sizeof(struct scsi_vpd_device_id) +
9749 sizeof(struct scsi_vpd_id_descriptor) +
9750 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9751 sizeof(struct scsi_vpd_id_descriptor) +
9752 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9753 if (lun && lun->lun_devid)
9754 data_len += lun->lun_devid->len;
9755 if (port->port_devid)
9756 data_len += port->port_devid->len;
9757 if (port->target_devid)
9758 data_len += port->target_devid->len;
9760 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9761 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9762 ctsio->kern_sg_entries = 0;
9764 if (data_len < alloc_len) {
9765 ctsio->residual = alloc_len - data_len;
9766 ctsio->kern_data_len = data_len;
9767 ctsio->kern_total_len = data_len;
9769 ctsio->residual = 0;
9770 ctsio->kern_data_len = alloc_len;
9771 ctsio->kern_total_len = alloc_len;
9773 ctsio->kern_data_resid = 0;
9774 ctsio->kern_rel_offset = 0;
9775 ctsio->kern_sg_entries = 0;
9778 * The control device is always connected. The disk device, on the
9779 * other hand, may not be online all the time.
9782 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9783 lun->be_lun->lun_type;
9785 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9786 devid_ptr->page_code = SVPD_DEVICE_ID;
9787 scsi_ulto2b(data_len - 4, devid_ptr->length);
9789 if (port->port_type == CTL_PORT_FC)
9790 proto = SCSI_PROTO_FC << 4;
9791 else if (port->port_type == CTL_PORT_ISCSI)
9792 proto = SCSI_PROTO_ISCSI << 4;
9794 proto = SCSI_PROTO_SPI << 4;
9795 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9798 * We're using a LUN association here. i.e., this device ID is a
9799 * per-LUN identifier.
9801 if (lun && lun->lun_devid) {
9802 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9803 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9804 lun->lun_devid->len);
9808 * This is for the WWPN which is a port association.
9810 if (port->port_devid) {
9811 memcpy(desc, port->port_devid->data, port->port_devid->len);
9812 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9813 port->port_devid->len);
9817 * This is for the Relative Target Port(type 4h) identifier
9819 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9820 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9821 SVPD_ID_TYPE_RELTARG;
9823 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9824 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9825 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9828 * This is for the Target Port Group(type 5h) identifier
9830 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9831 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9832 SVPD_ID_TYPE_TPORTGRP;
9834 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1,
9835 &desc->identifier[2]);
9836 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9837 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9840 * This is for the Target identifier
9842 if (port->target_devid) {
9843 memcpy(desc, port->target_devid->data, port->target_devid->len);
9846 ctl_set_success(ctsio);
9847 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9848 ctsio->be_move_done = ctl_config_move_done;
9849 ctl_datamove((union ctl_io *)ctsio);
9850 return (CTL_RETVAL_COMPLETE);
9854 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9856 struct ctl_softc *softc = control_softc;
9857 struct scsi_vpd_scsi_ports *sp;
9858 struct scsi_vpd_port_designation *pd;
9859 struct scsi_vpd_port_designation_cont *pdc;
9860 struct ctl_lun *lun;
9861 struct ctl_port *port;
9862 int data_len, num_target_ports, iid_len, id_len, g, pg, p;
9863 int num_target_port_groups;
9865 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9867 if (softc->is_single)
9868 num_target_port_groups = 1;
9870 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
9871 num_target_ports = 0;
9874 mtx_lock(&softc->ctl_lock);
9875 STAILQ_FOREACH(port, &softc->port_list, links) {
9876 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9879 ctl_map_lun_back(port->targ_port, lun->lun) >=
9883 if (port->init_devid)
9884 iid_len += port->init_devid->len;
9885 if (port->port_devid)
9886 id_len += port->port_devid->len;
9888 mtx_unlock(&softc->ctl_lock);
9890 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups *
9891 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9892 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9893 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9894 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9895 ctsio->kern_sg_entries = 0;
9897 if (data_len < alloc_len) {
9898 ctsio->residual = alloc_len - data_len;
9899 ctsio->kern_data_len = data_len;
9900 ctsio->kern_total_len = data_len;
9902 ctsio->residual = 0;
9903 ctsio->kern_data_len = alloc_len;
9904 ctsio->kern_total_len = alloc_len;
9906 ctsio->kern_data_resid = 0;
9907 ctsio->kern_rel_offset = 0;
9908 ctsio->kern_sg_entries = 0;
9911 * The control device is always connected. The disk device, on the
9912 * other hand, may not be online all the time. Need to change this
9913 * to figure out whether the disk device is actually online or not.
9916 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9917 lun->be_lun->lun_type;
9919 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9921 sp->page_code = SVPD_SCSI_PORTS;
9922 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9924 pd = &sp->design[0];
9926 mtx_lock(&softc->ctl_lock);
9927 pg = softc->port_offset / CTL_MAX_PORTS;
9928 for (g = 0; g < num_target_port_groups; g++) {
9929 STAILQ_FOREACH(port, &softc->port_list, links) {
9930 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9933 ctl_map_lun_back(port->targ_port, lun->lun) >=
9936 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS;
9937 scsi_ulto2b(p, pd->relative_port_id);
9938 if (port->init_devid && g == pg) {
9939 iid_len = port->init_devid->len;
9940 memcpy(pd->initiator_transportid,
9941 port->init_devid->data, port->init_devid->len);
9944 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9945 pdc = (struct scsi_vpd_port_designation_cont *)
9946 (&pd->initiator_transportid[iid_len]);
9947 if (port->port_devid && g == pg) {
9948 id_len = port->port_devid->len;
9949 memcpy(pdc->target_port_descriptors,
9950 port->port_devid->data, port->port_devid->len);
9953 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9954 pd = (struct scsi_vpd_port_designation *)
9955 ((uint8_t *)pdc->target_port_descriptors + id_len);
9958 mtx_unlock(&softc->ctl_lock);
9960 ctl_set_success(ctsio);
9961 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9962 ctsio->be_move_done = ctl_config_move_done;
9963 ctl_datamove((union ctl_io *)ctsio);
9964 return (CTL_RETVAL_COMPLETE);
9968 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9970 struct scsi_vpd_block_limits *bl_ptr;
9971 struct ctl_lun *lun;
9974 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9976 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9977 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9978 ctsio->kern_sg_entries = 0;
9980 if (sizeof(*bl_ptr) < alloc_len) {
9981 ctsio->residual = alloc_len - sizeof(*bl_ptr);
9982 ctsio->kern_data_len = sizeof(*bl_ptr);
9983 ctsio->kern_total_len = sizeof(*bl_ptr);
9985 ctsio->residual = 0;
9986 ctsio->kern_data_len = alloc_len;
9987 ctsio->kern_total_len = alloc_len;
9989 ctsio->kern_data_resid = 0;
9990 ctsio->kern_rel_offset = 0;
9991 ctsio->kern_sg_entries = 0;
9994 * The control device is always connected. The disk device, on the
9995 * other hand, may not be online all the time. Need to change this
9996 * to figure out whether the disk device is actually online or not.
9999 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10000 lun->be_lun->lun_type;
10002 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10004 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
10005 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
10006 bl_ptr->max_cmp_write_len = 0xff;
10007 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
10009 bs = lun->be_lun->blocksize;
10010 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len);
10011 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10012 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
10013 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
10014 if (lun->be_lun->pblockexp != 0) {
10015 scsi_ulto4b((1 << lun->be_lun->pblockexp),
10016 bl_ptr->opt_unmap_grain);
10017 scsi_ulto4b(0x80000000 | lun->be_lun->pblockoff,
10018 bl_ptr->unmap_grain_align);
10021 scsi_ulto4b(lun->be_lun->atomicblock,
10022 bl_ptr->max_atomic_transfer_length);
10023 scsi_ulto4b(0, bl_ptr->atomic_alignment);
10024 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
10026 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
10028 ctl_set_success(ctsio);
10029 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10030 ctsio->be_move_done = ctl_config_move_done;
10031 ctl_datamove((union ctl_io *)ctsio);
10032 return (CTL_RETVAL_COMPLETE);
10036 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
10038 struct scsi_vpd_block_device_characteristics *bdc_ptr;
10039 struct ctl_lun *lun;
10043 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10045 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
10046 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
10047 ctsio->kern_sg_entries = 0;
10049 if (sizeof(*bdc_ptr) < alloc_len) {
10050 ctsio->residual = alloc_len - sizeof(*bdc_ptr);
10051 ctsio->kern_data_len = sizeof(*bdc_ptr);
10052 ctsio->kern_total_len = sizeof(*bdc_ptr);
10054 ctsio->residual = 0;
10055 ctsio->kern_data_len = alloc_len;
10056 ctsio->kern_total_len = alloc_len;
10058 ctsio->kern_data_resid = 0;
10059 ctsio->kern_rel_offset = 0;
10060 ctsio->kern_sg_entries = 0;
10063 * The control device is always connected. The disk device, on the
10064 * other hand, may not be online all the time. Need to change this
10065 * to figure out whether the disk device is actually online or not.
10068 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10069 lun->be_lun->lun_type;
10071 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10072 bdc_ptr->page_code = SVPD_BDC;
10073 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
10075 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL)
10076 i = strtol(value, NULL, 0);
10078 i = CTL_DEFAULT_ROTATION_RATE;
10079 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
10081 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL)
10082 i = strtol(value, NULL, 0);
10085 bdc_ptr->wab_wac_ff = (i & 0x0f);
10086 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
10088 ctl_set_success(ctsio);
10089 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10090 ctsio->be_move_done = ctl_config_move_done;
10091 ctl_datamove((union ctl_io *)ctsio);
10092 return (CTL_RETVAL_COMPLETE);
10096 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
10098 struct scsi_vpd_logical_block_prov *lbp_ptr;
10099 struct ctl_lun *lun;
10101 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10103 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
10104 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
10105 ctsio->kern_sg_entries = 0;
10107 if (sizeof(*lbp_ptr) < alloc_len) {
10108 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
10109 ctsio->kern_data_len = sizeof(*lbp_ptr);
10110 ctsio->kern_total_len = sizeof(*lbp_ptr);
10112 ctsio->residual = 0;
10113 ctsio->kern_data_len = alloc_len;
10114 ctsio->kern_total_len = alloc_len;
10116 ctsio->kern_data_resid = 0;
10117 ctsio->kern_rel_offset = 0;
10118 ctsio->kern_sg_entries = 0;
10121 * The control device is always connected. The disk device, on the
10122 * other hand, may not be online all the time. Need to change this
10123 * to figure out whether the disk device is actually online or not.
10126 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10127 lun->be_lun->lun_type;
10129 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10131 lbp_ptr->page_code = SVPD_LBP;
10132 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
10133 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10134 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
10135 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
10136 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
10137 lbp_ptr->prov_type = SVPD_LBP_THIN;
10140 ctl_set_success(ctsio);
10141 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10142 ctsio->be_move_done = ctl_config_move_done;
10143 ctl_datamove((union ctl_io *)ctsio);
10144 return (CTL_RETVAL_COMPLETE);
10148 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
10150 struct scsi_inquiry *cdb;
10151 int alloc_len, retval;
10153 cdb = (struct scsi_inquiry *)ctsio->cdb;
10155 retval = CTL_RETVAL_COMPLETE;
10157 alloc_len = scsi_2btoul(cdb->length);
10159 switch (cdb->page_code) {
10160 case SVPD_SUPPORTED_PAGES:
10161 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
10163 case SVPD_UNIT_SERIAL_NUMBER:
10164 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
10166 case SVPD_DEVICE_ID:
10167 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
10169 case SVPD_EXTENDED_INQUIRY_DATA:
10170 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
10172 case SVPD_MODE_PAGE_POLICY:
10173 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
10175 case SVPD_SCSI_PORTS:
10176 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10178 case SVPD_SCSI_TPC:
10179 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10181 case SVPD_BLOCK_LIMITS:
10182 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10185 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10188 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10191 ctl_set_invalid_field(ctsio,
10197 ctl_done((union ctl_io *)ctsio);
10198 retval = CTL_RETVAL_COMPLETE;
10206 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10208 struct scsi_inquiry_data *inq_ptr;
10209 struct scsi_inquiry *cdb;
10210 struct ctl_softc *ctl_softc;
10211 struct ctl_lun *lun;
10213 uint32_t alloc_len, data_len;
10214 ctl_port_type port_type;
10216 ctl_softc = control_softc;
10219 * Figure out whether we're talking to a Fibre Channel port or not.
10220 * We treat the ioctl front end, and any SCSI adapters, as packetized
10223 port_type = ctl_softc->ctl_ports[
10224 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type;
10225 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10226 port_type = CTL_PORT_SCSI;
10228 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10229 cdb = (struct scsi_inquiry *)ctsio->cdb;
10230 alloc_len = scsi_2btoul(cdb->length);
10233 * We malloc the full inquiry data size here and fill it
10234 * in. If the user only asks for less, we'll give him
10237 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10238 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10239 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10240 ctsio->kern_sg_entries = 0;
10241 ctsio->kern_data_resid = 0;
10242 ctsio->kern_rel_offset = 0;
10244 if (data_len < alloc_len) {
10245 ctsio->residual = alloc_len - data_len;
10246 ctsio->kern_data_len = data_len;
10247 ctsio->kern_total_len = data_len;
10249 ctsio->residual = 0;
10250 ctsio->kern_data_len = alloc_len;
10251 ctsio->kern_total_len = alloc_len;
10255 * If we have a LUN configured, report it as connected. Otherwise,
10256 * report that it is offline or no device is supported, depending
10257 * on the value of inquiry_pq_no_lun.
10259 * According to the spec (SPC-4 r34), the peripheral qualifier
10260 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario:
10262 * "A peripheral device having the specified peripheral device type
10263 * is not connected to this logical unit. However, the device
10264 * server is capable of supporting the specified peripheral device
10265 * type on this logical unit."
10267 * According to the same spec, the peripheral qualifier
10268 * SID_QUAL_BAD_LU (011b) is used in this scenario:
10270 * "The device server is not capable of supporting a peripheral
10271 * device on this logical unit. For this peripheral qualifier the
10272 * peripheral device type shall be set to 1Fh. All other peripheral
10273 * device type values are reserved for this peripheral qualifier."
10275 * Given the text, it would seem that we probably want to report that
10276 * the LUN is offline here. There is no LUN connected, but we can
10277 * support a LUN at the given LUN number.
10279 * In the real world, though, it sounds like things are a little
10282 * - Linux, when presented with a LUN with the offline peripheral
10283 * qualifier, will create an sg driver instance for it. So when
10284 * you attach it to CTL, you wind up with a ton of sg driver
10285 * instances. (One for every LUN that Linux bothered to probe.)
10286 * Linux does this despite the fact that it issues a REPORT LUNs
10287 * to LUN 0 to get the inventory of supported LUNs.
10289 * - There is other anecdotal evidence (from Emulex folks) about
10290 * arrays that use the offline peripheral qualifier for LUNs that
10291 * are on the "passive" path in an active/passive array.
10293 * So the solution is provide a hopefully reasonable default
10294 * (return bad/no LUN) and allow the user to change the behavior
10295 * with a tunable/sysctl variable.
10298 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10299 lun->be_lun->lun_type;
10300 else if (ctl_softc->inquiry_pq_no_lun == 0)
10301 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10303 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10305 /* RMB in byte 2 is 0 */
10306 inq_ptr->version = SCSI_REV_SPC4;
10309 * According to SAM-3, even if a device only supports a single
10310 * level of LUN addressing, it should still set the HISUP bit:
10312 * 4.9.1 Logical unit numbers overview
10314 * All logical unit number formats described in this standard are
10315 * hierarchical in structure even when only a single level in that
10316 * hierarchy is used. The HISUP bit shall be set to one in the
10317 * standard INQUIRY data (see SPC-2) when any logical unit number
10318 * format described in this standard is used. Non-hierarchical
10319 * formats are outside the scope of this standard.
10321 * Therefore we set the HiSup bit here.
10323 * The reponse format is 2, per SPC-3.
10325 inq_ptr->response_format = SID_HiSup | 2;
10327 inq_ptr->additional_length = data_len -
10328 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10329 CTL_DEBUG_PRINT(("additional_length = %d\n",
10330 inq_ptr->additional_length));
10332 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10333 /* 16 bit addressing */
10334 if (port_type == CTL_PORT_SCSI)
10335 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10336 /* XXX set the SID_MultiP bit here if we're actually going to
10337 respond on multiple ports */
10338 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10340 /* 16 bit data bus, synchronous transfers */
10341 if (port_type == CTL_PORT_SCSI)
10342 inq_ptr->flags = SID_WBus16 | SID_Sync;
10344 * XXX KDM do we want to support tagged queueing on the control
10348 || (lun->be_lun->lun_type != T_PROCESSOR))
10349 inq_ptr->flags |= SID_CmdQue;
10351 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10352 * We have 8 bytes for the vendor name, and 16 bytes for the device
10353 * name and 4 bytes for the revision.
10355 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10356 "vendor")) == NULL) {
10357 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10359 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10360 strncpy(inq_ptr->vendor, val,
10361 min(sizeof(inq_ptr->vendor), strlen(val)));
10364 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10365 sizeof(inq_ptr->product));
10366 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10367 switch (lun->be_lun->lun_type) {
10369 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10370 sizeof(inq_ptr->product));
10373 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10374 sizeof(inq_ptr->product));
10377 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10378 sizeof(inq_ptr->product));
10382 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10383 strncpy(inq_ptr->product, val,
10384 min(sizeof(inq_ptr->product), strlen(val)));
10388 * XXX make this a macro somewhere so it automatically gets
10389 * incremented when we make changes.
10391 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10392 "revision")) == NULL) {
10393 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10395 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10396 strncpy(inq_ptr->revision, val,
10397 min(sizeof(inq_ptr->revision), strlen(val)));
10401 * For parallel SCSI, we support double transition and single
10402 * transition clocking. We also support QAS (Quick Arbitration
10403 * and Selection) and Information Unit transfers on both the
10404 * control and array devices.
10406 if (port_type == CTL_PORT_SCSI)
10407 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10410 /* SAM-5 (no version claimed) */
10411 scsi_ulto2b(0x00A0, inq_ptr->version1);
10412 /* SPC-4 (no version claimed) */
10413 scsi_ulto2b(0x0460, inq_ptr->version2);
10414 if (port_type == CTL_PORT_FC) {
10415 /* FCP-2 ANSI INCITS.350:2003 */
10416 scsi_ulto2b(0x0917, inq_ptr->version3);
10417 } else if (port_type == CTL_PORT_SCSI) {
10418 /* SPI-4 ANSI INCITS.362:200x */
10419 scsi_ulto2b(0x0B56, inq_ptr->version3);
10420 } else if (port_type == CTL_PORT_ISCSI) {
10421 /* iSCSI (no version claimed) */
10422 scsi_ulto2b(0x0960, inq_ptr->version3);
10423 } else if (port_type == CTL_PORT_SAS) {
10424 /* SAS (no version claimed) */
10425 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10429 /* SBC-4 (no version claimed) */
10430 scsi_ulto2b(0x0600, inq_ptr->version4);
10432 switch (lun->be_lun->lun_type) {
10434 /* SBC-4 (no version claimed) */
10435 scsi_ulto2b(0x0600, inq_ptr->version4);
10443 ctl_set_success(ctsio);
10444 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10445 ctsio->be_move_done = ctl_config_move_done;
10446 ctl_datamove((union ctl_io *)ctsio);
10447 return (CTL_RETVAL_COMPLETE);
10451 ctl_inquiry(struct ctl_scsiio *ctsio)
10453 struct scsi_inquiry *cdb;
10456 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10458 cdb = (struct scsi_inquiry *)ctsio->cdb;
10459 if (cdb->byte2 & SI_EVPD)
10460 retval = ctl_inquiry_evpd(ctsio);
10461 else if (cdb->page_code == 0)
10462 retval = ctl_inquiry_std(ctsio);
10464 ctl_set_invalid_field(ctsio,
10470 ctl_done((union ctl_io *)ctsio);
10471 return (CTL_RETVAL_COMPLETE);
10478 * For known CDB types, parse the LBA and length.
10481 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10483 if (io->io_hdr.io_type != CTL_IO_SCSI)
10486 switch (io->scsiio.cdb[0]) {
10487 case COMPARE_AND_WRITE: {
10488 struct scsi_compare_and_write *cdb;
10490 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10492 *lba = scsi_8btou64(cdb->addr);
10493 *len = cdb->length;
10498 struct scsi_rw_6 *cdb;
10500 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10502 *lba = scsi_3btoul(cdb->addr);
10503 /* only 5 bits are valid in the most significant address byte */
10505 *len = cdb->length;
10510 struct scsi_rw_10 *cdb;
10512 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10514 *lba = scsi_4btoul(cdb->addr);
10515 *len = scsi_2btoul(cdb->length);
10518 case WRITE_VERIFY_10: {
10519 struct scsi_write_verify_10 *cdb;
10521 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10523 *lba = scsi_4btoul(cdb->addr);
10524 *len = scsi_2btoul(cdb->length);
10529 struct scsi_rw_12 *cdb;
10531 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10533 *lba = scsi_4btoul(cdb->addr);
10534 *len = scsi_4btoul(cdb->length);
10537 case WRITE_VERIFY_12: {
10538 struct scsi_write_verify_12 *cdb;
10540 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10542 *lba = scsi_4btoul(cdb->addr);
10543 *len = scsi_4btoul(cdb->length);
10548 case WRITE_ATOMIC_16: {
10549 struct scsi_rw_16 *cdb;
10551 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10553 *lba = scsi_8btou64(cdb->addr);
10554 *len = scsi_4btoul(cdb->length);
10557 case WRITE_VERIFY_16: {
10558 struct scsi_write_verify_16 *cdb;
10560 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10562 *lba = scsi_8btou64(cdb->addr);
10563 *len = scsi_4btoul(cdb->length);
10566 case WRITE_SAME_10: {
10567 struct scsi_write_same_10 *cdb;
10569 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10571 *lba = scsi_4btoul(cdb->addr);
10572 *len = scsi_2btoul(cdb->length);
10575 case WRITE_SAME_16: {
10576 struct scsi_write_same_16 *cdb;
10578 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10580 *lba = scsi_8btou64(cdb->addr);
10581 *len = scsi_4btoul(cdb->length);
10585 struct scsi_verify_10 *cdb;
10587 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10589 *lba = scsi_4btoul(cdb->addr);
10590 *len = scsi_2btoul(cdb->length);
10594 struct scsi_verify_12 *cdb;
10596 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10598 *lba = scsi_4btoul(cdb->addr);
10599 *len = scsi_4btoul(cdb->length);
10603 struct scsi_verify_16 *cdb;
10605 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10607 *lba = scsi_8btou64(cdb->addr);
10608 *len = scsi_4btoul(cdb->length);
10618 break; /* NOTREACHED */
10625 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2)
10627 uint64_t endlba1, endlba2;
10629 endlba1 = lba1 + len1 - 1;
10630 endlba2 = lba2 + len2 - 1;
10632 if ((endlba1 < lba2)
10633 || (endlba2 < lba1))
10634 return (CTL_ACTION_PASS);
10636 return (CTL_ACTION_BLOCK);
10640 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10642 struct ctl_ptr_len_flags *ptrlen;
10643 struct scsi_unmap_desc *buf, *end, *range;
10647 /* If not UNMAP -- go other way. */
10648 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10649 io->scsiio.cdb[0] != UNMAP)
10650 return (CTL_ACTION_ERROR);
10652 /* If UNMAP without data -- block and wait for data. */
10653 ptrlen = (struct ctl_ptr_len_flags *)
10654 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10655 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10656 ptrlen->ptr == NULL)
10657 return (CTL_ACTION_BLOCK);
10659 /* UNMAP with data -- check for collision. */
10660 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10661 end = buf + ptrlen->len / sizeof(*buf);
10662 for (range = buf; range < end; range++) {
10663 lba = scsi_8btou64(range->lba);
10664 len = scsi_4btoul(range->length);
10665 if ((lba < lba2 + len2) && (lba + len > lba2))
10666 return (CTL_ACTION_BLOCK);
10668 return (CTL_ACTION_PASS);
10672 ctl_extent_check(union ctl_io *io1, union ctl_io *io2)
10674 uint64_t lba1, lba2;
10675 uint64_t len1, len2;
10678 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10679 return (CTL_ACTION_ERROR);
10681 retval = ctl_extent_check_unmap(io2, lba1, len1);
10682 if (retval != CTL_ACTION_ERROR)
10685 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10686 return (CTL_ACTION_ERROR);
10688 return (ctl_extent_check_lba(lba1, len1, lba2, len2));
10692 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10693 union ctl_io *ooa_io)
10695 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10696 ctl_serialize_action *serialize_row;
10699 * The initiator attempted multiple untagged commands at the same
10700 * time. Can't do that.
10702 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10703 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10704 && ((pending_io->io_hdr.nexus.targ_port ==
10705 ooa_io->io_hdr.nexus.targ_port)
10706 && (pending_io->io_hdr.nexus.initid.id ==
10707 ooa_io->io_hdr.nexus.initid.id))
10708 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10709 return (CTL_ACTION_OVERLAP);
10712 * The initiator attempted to send multiple tagged commands with
10713 * the same ID. (It's fine if different initiators have the same
10716 * Even if all of those conditions are true, we don't kill the I/O
10717 * if the command ahead of us has been aborted. We won't end up
10718 * sending it to the FETD, and it's perfectly legal to resend a
10719 * command with the same tag number as long as the previous
10720 * instance of this tag number has been aborted somehow.
10722 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10723 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10724 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10725 && ((pending_io->io_hdr.nexus.targ_port ==
10726 ooa_io->io_hdr.nexus.targ_port)
10727 && (pending_io->io_hdr.nexus.initid.id ==
10728 ooa_io->io_hdr.nexus.initid.id))
10729 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10730 return (CTL_ACTION_OVERLAP_TAG);
10733 * If we get a head of queue tag, SAM-3 says that we should
10734 * immediately execute it.
10736 * What happens if this command would normally block for some other
10737 * reason? e.g. a request sense with a head of queue tag
10738 * immediately after a write. Normally that would block, but this
10739 * will result in its getting executed immediately...
10741 * We currently return "pass" instead of "skip", so we'll end up
10742 * going through the rest of the queue to check for overlapped tags.
10744 * XXX KDM check for other types of blockage first??
10746 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10747 return (CTL_ACTION_PASS);
10750 * Ordered tags have to block until all items ahead of them
10751 * have completed. If we get called with an ordered tag, we always
10752 * block, if something else is ahead of us in the queue.
10754 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10755 return (CTL_ACTION_BLOCK);
10758 * Simple tags get blocked until all head of queue and ordered tags
10759 * ahead of them have completed. I'm lumping untagged commands in
10760 * with simple tags here. XXX KDM is that the right thing to do?
10762 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10763 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10764 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10765 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10766 return (CTL_ACTION_BLOCK);
10768 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
10769 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
10771 serialize_row = ctl_serialize_table[ooa_entry->seridx];
10773 switch (serialize_row[pending_entry->seridx]) {
10774 case CTL_SER_BLOCK:
10775 return (CTL_ACTION_BLOCK);
10776 case CTL_SER_EXTENT:
10777 return (ctl_extent_check(pending_io, ooa_io));
10778 case CTL_SER_EXTENTOPT:
10779 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
10780 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
10781 return (ctl_extent_check(pending_io, ooa_io));
10784 return (CTL_ACTION_PASS);
10785 case CTL_SER_BLOCKOPT:
10786 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
10787 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
10788 return (CTL_ACTION_BLOCK);
10789 return (CTL_ACTION_PASS);
10791 return (CTL_ACTION_SKIP);
10793 panic("invalid serialization value %d",
10794 serialize_row[pending_entry->seridx]);
10797 return (CTL_ACTION_ERROR);
10801 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
10803 * - pending_io is generally either incoming, or on the blocked queue
10804 * - starting I/O is the I/O we want to start the check with.
10807 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
10808 union ctl_io *starting_io)
10810 union ctl_io *ooa_io;
10813 mtx_assert(&lun->lun_lock, MA_OWNED);
10816 * Run back along the OOA queue, starting with the current
10817 * blocked I/O and going through every I/O before it on the
10818 * queue. If starting_io is NULL, we'll just end up returning
10821 for (ooa_io = starting_io; ooa_io != NULL;
10822 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
10826 * This routine just checks to see whether
10827 * cur_blocked is blocked by ooa_io, which is ahead
10828 * of it in the queue. It doesn't queue/dequeue
10831 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
10833 case CTL_ACTION_BLOCK:
10834 case CTL_ACTION_OVERLAP:
10835 case CTL_ACTION_OVERLAP_TAG:
10836 case CTL_ACTION_SKIP:
10837 case CTL_ACTION_ERROR:
10839 break; /* NOTREACHED */
10840 case CTL_ACTION_PASS:
10843 panic("invalid action %d", action);
10844 break; /* NOTREACHED */
10848 return (CTL_ACTION_PASS);
10853 * - An I/O has just completed, and has been removed from the per-LUN OOA
10854 * queue, so some items on the blocked queue may now be unblocked.
10857 ctl_check_blocked(struct ctl_lun *lun)
10859 union ctl_io *cur_blocked, *next_blocked;
10861 mtx_assert(&lun->lun_lock, MA_OWNED);
10864 * Run forward from the head of the blocked queue, checking each
10865 * entry against the I/Os prior to it on the OOA queue to see if
10866 * there is still any blockage.
10868 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
10869 * with our removing a variable on it while it is traversing the
10872 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
10873 cur_blocked != NULL; cur_blocked = next_blocked) {
10874 union ctl_io *prev_ooa;
10877 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
10880 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
10881 ctl_ooaq, ooa_links);
10884 * If cur_blocked happens to be the first item in the OOA
10885 * queue now, prev_ooa will be NULL, and the action
10886 * returned will just be CTL_ACTION_PASS.
10888 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
10891 case CTL_ACTION_BLOCK:
10892 /* Nothing to do here, still blocked */
10894 case CTL_ACTION_OVERLAP:
10895 case CTL_ACTION_OVERLAP_TAG:
10897 * This shouldn't happen! In theory we've already
10898 * checked this command for overlap...
10901 case CTL_ACTION_PASS:
10902 case CTL_ACTION_SKIP: {
10903 struct ctl_softc *softc;
10904 const struct ctl_cmd_entry *entry;
10908 * The skip case shouldn't happen, this transaction
10909 * should have never made it onto the blocked queue.
10912 * This I/O is no longer blocked, we can remove it
10913 * from the blocked queue. Since this is a TAILQ
10914 * (doubly linked list), we can do O(1) removals
10915 * from any place on the list.
10917 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
10919 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
10921 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){
10923 * Need to send IO back to original side to
10926 union ctl_ha_msg msg_info;
10928 msg_info.hdr.original_sc =
10929 cur_blocked->io_hdr.original_sc;
10930 msg_info.hdr.serializing_sc = cur_blocked;
10931 msg_info.hdr.msg_type = CTL_MSG_R2R;
10932 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
10933 &msg_info, sizeof(msg_info), 0)) >
10934 CTL_HA_STATUS_SUCCESS) {
10935 printf("CTL:Check Blocked error from "
10936 "ctl_ha_msg_send %d\n",
10941 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
10942 softc = control_softc;
10945 * Check this I/O for LUN state changes that may
10946 * have happened while this command was blocked.
10947 * The LUN state may have been changed by a command
10948 * ahead of us in the queue, so we need to re-check
10949 * for any states that can be caused by SCSI
10952 if (ctl_scsiio_lun_check(softc, lun, entry,
10953 &cur_blocked->scsiio) == 0) {
10954 cur_blocked->io_hdr.flags |=
10955 CTL_FLAG_IS_WAS_ON_RTR;
10956 ctl_enqueue_rtr(cur_blocked);
10958 ctl_done(cur_blocked);
10963 * This probably shouldn't happen -- we shouldn't
10964 * get CTL_ACTION_ERROR, or anything else.
10970 return (CTL_RETVAL_COMPLETE);
10974 * This routine (with one exception) checks LUN flags that can be set by
10975 * commands ahead of us in the OOA queue. These flags have to be checked
10976 * when a command initially comes in, and when we pull a command off the
10977 * blocked queue and are preparing to execute it. The reason we have to
10978 * check these flags for commands on the blocked queue is that the LUN
10979 * state may have been changed by a command ahead of us while we're on the
10982 * Ordering is somewhat important with these checks, so please pay
10983 * careful attention to the placement of any new checks.
10986 ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
10987 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
10994 mtx_assert(&lun->lun_lock, MA_OWNED);
10997 * If this shelf is a secondary shelf controller, we have to reject
10998 * any media access commands.
11000 if ((ctl_softc->flags & CTL_FLAG_ACTIVE_SHELF) == 0 &&
11001 (entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0) {
11002 ctl_set_lun_standby(ctsio);
11007 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11008 if (lun->flags & CTL_LUN_READONLY) {
11009 ctl_set_sense(ctsio, /*current_error*/ 1,
11010 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11011 /*asc*/ 0x27, /*ascq*/ 0x01, SSD_ELEM_NONE);
11015 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT]
11016 .eca_and_aen & SCP_SWP) != 0) {
11017 ctl_set_sense(ctsio, /*current_error*/ 1,
11018 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11019 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11026 * Check for a reservation conflict. If this command isn't allowed
11027 * even on reserved LUNs, and if this initiator isn't the one who
11028 * reserved us, reject the command with a reservation conflict.
11030 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
11031 if ((lun->flags & CTL_LUN_RESERVED)
11032 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11033 if (lun->res_idx != residx) {
11034 ctl_set_reservation_conflict(ctsio);
11040 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11041 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11042 /* No reservation or command is allowed. */;
11043 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11044 (lun->res_type == SPR_TYPE_WR_EX ||
11045 lun->res_type == SPR_TYPE_WR_EX_RO ||
11046 lun->res_type == SPR_TYPE_WR_EX_AR)) {
11047 /* The command is allowed for Write Exclusive resv. */;
11050 * if we aren't registered or it's a res holder type
11051 * reservation and this isn't the res holder then set a
11054 if (lun->pr_keys[residx] == 0
11055 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
11056 ctl_set_reservation_conflict(ctsio);
11063 if ((lun->flags & CTL_LUN_OFFLINE)
11064 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) {
11065 ctl_set_lun_not_ready(ctsio);
11071 * If the LUN is stopped, see if this particular command is allowed
11072 * for a stopped lun. Otherwise, reject it with 0x04,0x02.
11074 if ((lun->flags & CTL_LUN_STOPPED)
11075 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
11076 /* "Logical unit not ready, initializing cmd. required" */
11077 ctl_set_lun_stopped(ctsio);
11082 if ((lun->flags & CTL_LUN_INOPERABLE)
11083 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
11084 /* "Medium format corrupted" */
11085 ctl_set_medium_format_corrupted(ctsio);
11096 ctl_failover_io(union ctl_io *io, int have_lock)
11098 ctl_set_busy(&io->scsiio);
11105 struct ctl_lun *lun;
11106 struct ctl_softc *ctl_softc;
11107 union ctl_io *next_io, *pending_io;
11112 ctl_softc = control_softc;
11114 mtx_lock(&ctl_softc->ctl_lock);
11116 * Remove any cmds from the other SC from the rtr queue. These
11117 * will obviously only be for LUNs for which we're the primary.
11118 * We can't send status or get/send data for these commands.
11119 * Since they haven't been executed yet, we can just remove them.
11120 * We'll either abort them or delete them below, depending on
11121 * which HA mode we're in.
11124 mtx_lock(&ctl_softc->queue_lock);
11125 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue);
11126 io != NULL; io = next_io) {
11127 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
11128 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
11129 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr,
11130 ctl_io_hdr, links);
11132 mtx_unlock(&ctl_softc->queue_lock);
11135 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) {
11136 lun = ctl_softc->ctl_luns[lun_idx];
11141 * Processor LUNs are primary on both sides.
11142 * XXX will this always be true?
11144 if (lun->be_lun->lun_type == T_PROCESSOR)
11147 if ((lun->flags & CTL_LUN_PRIMARY_SC)
11148 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
11149 printf("FAILOVER: primary lun %d\n", lun_idx);
11151 * Remove all commands from the other SC. First from the
11152 * blocked queue then from the ooa queue. Once we have
11153 * removed them. Call ctl_check_blocked to see if there
11154 * is anything that can run.
11156 for (io = (union ctl_io *)TAILQ_FIRST(
11157 &lun->blocked_queue); io != NULL; io = next_io) {
11159 next_io = (union ctl_io *)TAILQ_NEXT(
11160 &io->io_hdr, blocked_links);
11162 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
11163 TAILQ_REMOVE(&lun->blocked_queue,
11164 &io->io_hdr,blocked_links);
11165 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11166 TAILQ_REMOVE(&lun->ooa_queue,
11167 &io->io_hdr, ooa_links);
11173 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
11174 io != NULL; io = next_io) {
11176 next_io = (union ctl_io *)TAILQ_NEXT(
11177 &io->io_hdr, ooa_links);
11179 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
11181 TAILQ_REMOVE(&lun->ooa_queue,
11188 ctl_check_blocked(lun);
11189 } else if ((lun->flags & CTL_LUN_PRIMARY_SC)
11190 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
11192 printf("FAILOVER: primary lun %d\n", lun_idx);
11194 * Abort all commands from the other SC. We can't
11195 * send status back for them now. These should get
11196 * cleaned up when they are completed or come out
11197 * for a datamove operation.
11199 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
11200 io != NULL; io = next_io) {
11201 next_io = (union ctl_io *)TAILQ_NEXT(
11202 &io->io_hdr, ooa_links);
11204 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
11205 io->io_hdr.flags |= CTL_FLAG_ABORT;
11207 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
11208 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
11210 printf("FAILOVER: secondary lun %d\n", lun_idx);
11212 lun->flags |= CTL_LUN_PRIMARY_SC;
11215 * We send all I/O that was sent to this controller
11216 * and redirected to the other side back with
11217 * busy status, and have the initiator retry it.
11218 * Figuring out how much data has been transferred,
11219 * etc. and picking up where we left off would be
11222 * XXX KDM need to remove I/O from the blocked
11225 for (pending_io = (union ctl_io *)TAILQ_FIRST(
11226 &lun->ooa_queue); pending_io != NULL;
11227 pending_io = next_io) {
11229 next_io = (union ctl_io *)TAILQ_NEXT(
11230 &pending_io->io_hdr, ooa_links);
11232 pending_io->io_hdr.flags &=
11233 ~CTL_FLAG_SENT_2OTHER_SC;
11235 if (pending_io->io_hdr.flags &
11236 CTL_FLAG_IO_ACTIVE) {
11237 pending_io->io_hdr.flags |=
11240 ctl_set_busy(&pending_io->scsiio);
11241 ctl_done(pending_io);
11246 * Build Unit Attention
11248 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11249 lun->pending_ua[i] |=
11250 CTL_UA_ASYM_ACC_CHANGE;
11252 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
11253 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
11254 printf("FAILOVER: secondary lun %d\n", lun_idx);
11256 * if the first io on the OOA is not on the RtR queue
11259 lun->flags |= CTL_LUN_PRIMARY_SC;
11261 pending_io = (union ctl_io *)TAILQ_FIRST(
11263 if (pending_io==NULL) {
11264 printf("Nothing on OOA queue\n");
11268 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11269 if ((pending_io->io_hdr.flags &
11270 CTL_FLAG_IS_WAS_ON_RTR) == 0) {
11271 pending_io->io_hdr.flags |=
11272 CTL_FLAG_IS_WAS_ON_RTR;
11273 ctl_enqueue_rtr(pending_io);
11278 printf("Tag 0x%04x is running\n",
11279 pending_io->scsiio.tag_num);
11283 next_io = (union ctl_io *)TAILQ_NEXT(
11284 &pending_io->io_hdr, ooa_links);
11285 for (pending_io=next_io; pending_io != NULL;
11286 pending_io = next_io) {
11287 pending_io->io_hdr.flags &=
11288 ~CTL_FLAG_SENT_2OTHER_SC;
11289 next_io = (union ctl_io *)TAILQ_NEXT(
11290 &pending_io->io_hdr, ooa_links);
11291 if (pending_io->io_hdr.flags &
11292 CTL_FLAG_IS_WAS_ON_RTR) {
11294 printf("Tag 0x%04x is running\n",
11295 pending_io->scsiio.tag_num);
11300 switch (ctl_check_ooa(lun, pending_io,
11301 (union ctl_io *)TAILQ_PREV(
11302 &pending_io->io_hdr, ctl_ooaq,
11305 case CTL_ACTION_BLOCK:
11306 TAILQ_INSERT_TAIL(&lun->blocked_queue,
11307 &pending_io->io_hdr,
11309 pending_io->io_hdr.flags |=
11312 case CTL_ACTION_PASS:
11313 case CTL_ACTION_SKIP:
11314 pending_io->io_hdr.flags |=
11315 CTL_FLAG_IS_WAS_ON_RTR;
11316 ctl_enqueue_rtr(pending_io);
11318 case CTL_ACTION_OVERLAP:
11319 ctl_set_overlapped_cmd(
11320 (struct ctl_scsiio *)pending_io);
11321 ctl_done(pending_io);
11323 case CTL_ACTION_OVERLAP_TAG:
11324 ctl_set_overlapped_tag(
11325 (struct ctl_scsiio *)pending_io,
11326 pending_io->scsiio.tag_num & 0xff);
11327 ctl_done(pending_io);
11329 case CTL_ACTION_ERROR:
11331 ctl_set_internal_failure(
11332 (struct ctl_scsiio *)pending_io,
11335 ctl_done(pending_io);
11341 * Build Unit Attention
11343 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11344 lun->pending_ua[i] |=
11345 CTL_UA_ASYM_ACC_CHANGE;
11348 panic("Unhandled HA mode failover, LUN flags = %#x, "
11349 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode);
11353 mtx_unlock(&ctl_softc->ctl_lock);
11357 ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio)
11359 struct ctl_lun *lun;
11360 const struct ctl_cmd_entry *entry;
11361 uint32_t initidx, targ_lun;
11368 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11369 if ((targ_lun < CTL_MAX_LUNS)
11370 && ((lun = ctl_softc->ctl_luns[targ_lun]) != NULL)) {
11372 * If the LUN is invalid, pretend that it doesn't exist.
11373 * It will go away as soon as all pending I/O has been
11376 mtx_lock(&lun->lun_lock);
11377 if (lun->flags & CTL_LUN_DISABLED) {
11378 mtx_unlock(&lun->lun_lock);
11380 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11381 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11383 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
11384 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
11386 if (lun->be_lun->lun_type == T_PROCESSOR) {
11387 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV;
11391 * Every I/O goes into the OOA queue for a
11392 * particular LUN, and stays there until completion.
11394 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr,
11398 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11399 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11402 /* Get command entry and return error if it is unsuppotyed. */
11403 entry = ctl_validate_command(ctsio);
11404 if (entry == NULL) {
11406 mtx_unlock(&lun->lun_lock);
11410 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11411 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11414 * Check to see whether we can send this command to LUNs that don't
11415 * exist. This should pretty much only be the case for inquiry
11416 * and request sense. Further checks, below, really require having
11417 * a LUN, so we can't really check the command anymore. Just put
11418 * it on the rtr queue.
11421 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) {
11422 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11423 ctl_enqueue_rtr((union ctl_io *)ctsio);
11427 ctl_set_unsupported_lun(ctsio);
11428 ctl_done((union ctl_io *)ctsio);
11429 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11433 * Make sure we support this particular command on this LUN.
11434 * e.g., we don't support writes to the control LUN.
11436 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11437 mtx_unlock(&lun->lun_lock);
11438 ctl_set_invalid_opcode(ctsio);
11439 ctl_done((union ctl_io *)ctsio);
11444 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11448 * If we've got a request sense, it'll clear the contingent
11449 * allegiance condition. Otherwise, if we have a CA condition for
11450 * this initiator, clear it, because it sent down a command other
11451 * than request sense.
11453 if ((ctsio->cdb[0] != REQUEST_SENSE)
11454 && (ctl_is_set(lun->have_ca, initidx)))
11455 ctl_clear_mask(lun->have_ca, initidx);
11459 * If the command has this flag set, it handles its own unit
11460 * attention reporting, we shouldn't do anything. Otherwise we
11461 * check for any pending unit attentions, and send them back to the
11462 * initiator. We only do this when a command initially comes in,
11463 * not when we pull it off the blocked queue.
11465 * According to SAM-3, section 5.3.2, the order that things get
11466 * presented back to the host is basically unit attentions caused
11467 * by some sort of reset event, busy status, reservation conflicts
11468 * or task set full, and finally any other status.
11470 * One issue here is that some of the unit attentions we report
11471 * don't fall into the "reset" category (e.g. "reported luns data
11472 * has changed"). So reporting it here, before the reservation
11473 * check, may be technically wrong. I guess the only thing to do
11474 * would be to check for and report the reset events here, and then
11475 * check for the other unit attention types after we check for a
11476 * reservation conflict.
11478 * XXX KDM need to fix this
11480 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11481 ctl_ua_type ua_type;
11483 if (lun->pending_ua[initidx] != CTL_UA_NONE) {
11484 scsi_sense_data_type sense_format;
11487 sense_format = (lun->flags &
11488 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC :
11491 sense_format = SSD_TYPE_FIXED;
11493 ua_type = ctl_build_ua(&lun->pending_ua[initidx],
11494 &ctsio->sense_data, sense_format);
11495 if (ua_type != CTL_UA_NONE) {
11496 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11497 ctsio->io_hdr.status = CTL_SCSI_ERROR |
11499 ctsio->sense_len = SSD_FULL_SIZE;
11500 mtx_unlock(&lun->lun_lock);
11501 ctl_done((union ctl_io *)ctsio);
11508 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) {
11509 mtx_unlock(&lun->lun_lock);
11510 ctl_done((union ctl_io *)ctsio);
11515 * XXX CHD this is where we want to send IO to other side if
11516 * this LUN is secondary on this SC. We will need to make a copy
11517 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11518 * the copy we send as FROM_OTHER.
11519 * We also need to stuff the address of the original IO so we can
11520 * find it easily. Something similar will need be done on the other
11521 * side so when we are done we can find the copy.
11523 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11524 union ctl_ha_msg msg_info;
11527 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11529 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11530 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11532 printf("1. ctsio %p\n", ctsio);
11534 msg_info.hdr.serializing_sc = NULL;
11535 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11536 msg_info.scsi.tag_num = ctsio->tag_num;
11537 msg_info.scsi.tag_type = ctsio->tag_type;
11538 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11540 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11542 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11543 (void *)&msg_info, sizeof(msg_info), 0)) >
11544 CTL_HA_STATUS_SUCCESS) {
11545 printf("CTL:precheck, ctl_ha_msg_send returned %d\n",
11547 printf("CTL:opcode is %x\n", ctsio->cdb[0]);
11550 printf("CTL:Precheck sent msg, opcode is %x\n",opcode);
11555 * XXX KDM this I/O is off the incoming queue, but hasn't
11556 * been inserted on any other queue. We may need to come
11557 * up with a holding queue while we wait for serialization
11558 * so that we have an idea of what we're waiting for from
11561 mtx_unlock(&lun->lun_lock);
11565 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11566 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11567 ctl_ooaq, ooa_links))) {
11568 case CTL_ACTION_BLOCK:
11569 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11570 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11572 mtx_unlock(&lun->lun_lock);
11574 case CTL_ACTION_PASS:
11575 case CTL_ACTION_SKIP:
11576 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11577 mtx_unlock(&lun->lun_lock);
11578 ctl_enqueue_rtr((union ctl_io *)ctsio);
11580 case CTL_ACTION_OVERLAP:
11581 mtx_unlock(&lun->lun_lock);
11582 ctl_set_overlapped_cmd(ctsio);
11583 ctl_done((union ctl_io *)ctsio);
11585 case CTL_ACTION_OVERLAP_TAG:
11586 mtx_unlock(&lun->lun_lock);
11587 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11588 ctl_done((union ctl_io *)ctsio);
11590 case CTL_ACTION_ERROR:
11592 mtx_unlock(&lun->lun_lock);
11593 ctl_set_internal_failure(ctsio,
11595 /*retry_count*/ 0);
11596 ctl_done((union ctl_io *)ctsio);
11602 const struct ctl_cmd_entry *
11603 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11605 const struct ctl_cmd_entry *entry;
11606 int service_action;
11608 entry = &ctl_cmd_table[ctsio->cdb[0]];
11610 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11611 if (entry->flags & CTL_CMD_FLAG_SA5) {
11612 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11613 entry = &((const struct ctl_cmd_entry *)
11614 entry->execute)[service_action];
11619 const struct ctl_cmd_entry *
11620 ctl_validate_command(struct ctl_scsiio *ctsio)
11622 const struct ctl_cmd_entry *entry;
11626 entry = ctl_get_cmd_entry(ctsio, &sa);
11627 if (entry->execute == NULL) {
11629 ctl_set_invalid_field(ctsio,
11636 ctl_set_invalid_opcode(ctsio);
11637 ctl_done((union ctl_io *)ctsio);
11640 KASSERT(entry->length > 0,
11641 ("Not defined length for command 0x%02x/0x%02x",
11642 ctsio->cdb[0], ctsio->cdb[1]));
11643 for (i = 1; i < entry->length; i++) {
11644 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11647 ctl_set_invalid_field(ctsio,
11652 /*bit*/ fls(diff) - 1);
11653 ctl_done((union ctl_io *)ctsio);
11660 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11663 switch (lun_type) {
11665 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) &&
11666 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11670 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) &&
11671 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11681 ctl_scsiio(struct ctl_scsiio *ctsio)
11684 const struct ctl_cmd_entry *entry;
11686 retval = CTL_RETVAL_COMPLETE;
11688 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11690 entry = ctl_get_cmd_entry(ctsio, NULL);
11693 * If this I/O has been aborted, just send it straight to
11694 * ctl_done() without executing it.
11696 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11697 ctl_done((union ctl_io *)ctsio);
11702 * All the checks should have been handled by ctl_scsiio_precheck().
11703 * We should be clear now to just execute the I/O.
11705 retval = entry->execute(ctsio);
11712 * Since we only implement one target right now, a bus reset simply resets
11713 * our single target.
11716 ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io)
11718 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET));
11722 ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
11723 ctl_ua_type ua_type)
11725 struct ctl_lun *lun;
11728 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11729 union ctl_ha_msg msg_info;
11731 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11732 msg_info.hdr.nexus = io->io_hdr.nexus;
11733 if (ua_type==CTL_UA_TARG_RESET)
11734 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11736 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11737 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11738 msg_info.hdr.original_sc = NULL;
11739 msg_info.hdr.serializing_sc = NULL;
11740 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11741 (void *)&msg_info, sizeof(msg_info), 0)) {
11746 mtx_lock(&ctl_softc->ctl_lock);
11747 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links)
11748 retval += ctl_lun_reset(lun, io, ua_type);
11749 mtx_unlock(&ctl_softc->ctl_lock);
11755 * The LUN should always be set. The I/O is optional, and is used to
11756 * distinguish between I/Os sent by this initiator, and by other
11757 * initiators. We set unit attention for initiators other than this one.
11758 * SAM-3 is vague on this point. It does say that a unit attention should
11759 * be established for other initiators when a LUN is reset (see section
11760 * 5.7.3), but it doesn't specifically say that the unit attention should
11761 * be established for this particular initiator when a LUN is reset. Here
11762 * is the relevant text, from SAM-3 rev 8:
11764 * 5.7.2 When a SCSI initiator port aborts its own tasks
11766 * When a SCSI initiator port causes its own task(s) to be aborted, no
11767 * notification that the task(s) have been aborted shall be returned to
11768 * the SCSI initiator port other than the completion response for the
11769 * command or task management function action that caused the task(s) to
11770 * be aborted and notification(s) associated with related effects of the
11771 * action (e.g., a reset unit attention condition).
11773 * XXX KDM for now, we're setting unit attention for all initiators.
11776 ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11784 mtx_lock(&lun->lun_lock);
11786 * Run through the OOA queue and abort each I/O.
11789 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
11791 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11792 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11793 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11797 * This version sets unit attention for every
11800 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11801 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11804 lun->pending_ua[i] |= ua_type;
11809 * A reset (any kind, really) clears reservations established with
11810 * RESERVE/RELEASE. It does not clear reservations established
11811 * with PERSISTENT RESERVE OUT, but we don't support that at the
11812 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
11813 * reservations made with the RESERVE/RELEASE commands, because
11814 * those commands are obsolete in SPC-3.
11816 lun->flags &= ~CTL_LUN_RESERVED;
11818 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11820 ctl_clear_mask(lun->have_ca, i);
11822 lun->pending_ua[i] |= ua_type;
11824 mtx_unlock(&lun->lun_lock);
11830 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11835 mtx_assert(&lun->lun_lock, MA_OWNED);
11838 * Run through the OOA queue and attempt to find the given I/O.
11839 * The target port, initiator ID, tag type and tag number have to
11840 * match the values that we got from the initiator. If we have an
11841 * untagged command to abort, simply abort the first untagged command
11842 * we come to. We only allow one untagged command at a time of course.
11844 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11845 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11847 if ((targ_port == UINT32_MAX ||
11848 targ_port == xio->io_hdr.nexus.targ_port) &&
11849 (init_id == UINT32_MAX ||
11850 init_id == xio->io_hdr.nexus.initid.id)) {
11851 if (targ_port != xio->io_hdr.nexus.targ_port ||
11852 init_id != xio->io_hdr.nexus.initid.id)
11853 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
11854 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11855 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11856 union ctl_ha_msg msg_info;
11858 msg_info.hdr.nexus = xio->io_hdr.nexus;
11859 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11860 msg_info.task.tag_num = xio->scsiio.tag_num;
11861 msg_info.task.tag_type = xio->scsiio.tag_type;
11862 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11863 msg_info.hdr.original_sc = NULL;
11864 msg_info.hdr.serializing_sc = NULL;
11865 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11866 (void *)&msg_info, sizeof(msg_info), 0);
11873 ctl_abort_task_set(union ctl_io *io)
11875 struct ctl_softc *softc = control_softc;
11876 struct ctl_lun *lun;
11882 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11883 mtx_lock(&softc->ctl_lock);
11884 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL))
11885 lun = softc->ctl_luns[targ_lun];
11887 mtx_unlock(&softc->ctl_lock);
11891 mtx_lock(&lun->lun_lock);
11892 mtx_unlock(&softc->ctl_lock);
11893 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
11894 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11895 io->io_hdr.nexus.initid.id,
11896 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11897 } else { /* CTL_TASK_CLEAR_TASK_SET */
11898 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
11899 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11901 mtx_unlock(&lun->lun_lock);
11906 ctl_i_t_nexus_reset(union ctl_io *io)
11908 struct ctl_softc *softc = control_softc;
11909 struct ctl_lun *lun;
11910 uint32_t initidx, residx;
11912 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11913 residx = ctl_get_resindex(&io->io_hdr.nexus);
11914 mtx_lock(&softc->ctl_lock);
11915 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11916 mtx_lock(&lun->lun_lock);
11917 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11918 io->io_hdr.nexus.initid.id,
11919 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11921 ctl_clear_mask(lun->have_ca, initidx);
11923 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
11924 lun->flags &= ~CTL_LUN_RESERVED;
11925 lun->pending_ua[initidx] |= CTL_UA_I_T_NEXUS_LOSS;
11926 mtx_unlock(&lun->lun_lock);
11928 mtx_unlock(&softc->ctl_lock);
11933 ctl_abort_task(union ctl_io *io)
11936 struct ctl_lun *lun;
11937 struct ctl_softc *ctl_softc;
11940 char printbuf[128];
11945 ctl_softc = control_softc;
11951 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11952 mtx_lock(&ctl_softc->ctl_lock);
11953 if ((targ_lun < CTL_MAX_LUNS)
11954 && (ctl_softc->ctl_luns[targ_lun] != NULL))
11955 lun = ctl_softc->ctl_luns[targ_lun];
11957 mtx_unlock(&ctl_softc->ctl_lock);
11962 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
11963 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
11966 mtx_lock(&lun->lun_lock);
11967 mtx_unlock(&ctl_softc->ctl_lock);
11969 * Run through the OOA queue and attempt to find the given I/O.
11970 * The target port, initiator ID, tag type and tag number have to
11971 * match the values that we got from the initiator. If we have an
11972 * untagged command to abort, simply abort the first untagged command
11973 * we come to. We only allow one untagged command at a time of course.
11976 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
11978 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11979 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11981 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
11983 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
11984 lun->lun, xio->scsiio.tag_num,
11985 xio->scsiio.tag_type,
11986 (xio->io_hdr.blocked_links.tqe_prev
11987 == NULL) ? "" : " BLOCKED",
11988 (xio->io_hdr.flags &
11989 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
11990 (xio->io_hdr.flags &
11991 CTL_FLAG_ABORT) ? " ABORT" : "",
11992 (xio->io_hdr.flags &
11993 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
11994 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
11996 printf("%s\n", sbuf_data(&sb));
11999 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port)
12000 && (xio->io_hdr.nexus.initid.id ==
12001 io->io_hdr.nexus.initid.id)) {
12003 * If the abort says that the task is untagged, the
12004 * task in the queue must be untagged. Otherwise,
12005 * we just check to see whether the tag numbers
12006 * match. This is because the QLogic firmware
12007 * doesn't pass back the tag type in an abort
12011 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12012 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12013 || (xio->scsiio.tag_num == io->taskio.tag_num)) {
12016 * XXX KDM we've got problems with FC, because it
12017 * doesn't send down a tag type with aborts. So we
12018 * can only really go by the tag number...
12019 * This may cause problems with parallel SCSI.
12020 * Need to figure that out!!
12022 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12023 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12025 if ((io->io_hdr.flags &
12026 CTL_FLAG_FROM_OTHER_SC) == 0 &&
12027 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12028 union ctl_ha_msg msg_info;
12030 io->io_hdr.flags |=
12031 CTL_FLAG_SENT_2OTHER_SC;
12032 msg_info.hdr.nexus = io->io_hdr.nexus;
12033 msg_info.task.task_action =
12034 CTL_TASK_ABORT_TASK;
12035 msg_info.task.tag_num =
12036 io->taskio.tag_num;
12037 msg_info.task.tag_type =
12038 io->taskio.tag_type;
12039 msg_info.hdr.msg_type =
12040 CTL_MSG_MANAGE_TASKS;
12041 msg_info.hdr.original_sc = NULL;
12042 msg_info.hdr.serializing_sc = NULL;
12044 printf("Sent Abort to other side\n");
12046 if (CTL_HA_STATUS_SUCCESS !=
12047 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12049 sizeof(msg_info), 0)) {
12053 printf("ctl_abort_task: found I/O to abort\n");
12059 mtx_unlock(&lun->lun_lock);
12063 * This isn't really an error. It's entirely possible for
12064 * the abort and command completion to cross on the wire.
12065 * This is more of an informative/diagnostic error.
12068 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
12069 "%d:%d:%d:%d tag %d type %d\n",
12070 io->io_hdr.nexus.initid.id,
12071 io->io_hdr.nexus.targ_port,
12072 io->io_hdr.nexus.targ_target.id,
12073 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
12074 io->taskio.tag_type);
12081 ctl_run_task(union ctl_io *io)
12083 struct ctl_softc *ctl_softc = control_softc;
12085 const char *task_desc;
12087 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12089 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12090 ("ctl_run_task: Unextected io_type %d\n",
12091 io->io_hdr.io_type));
12093 task_desc = ctl_scsi_task_string(&io->taskio);
12094 if (task_desc != NULL) {
12096 csevent_log(CSC_CTL | CSC_SHELF_SW |
12098 csevent_LogType_Trace,
12099 csevent_Severity_Information,
12100 csevent_AlertLevel_Green,
12101 csevent_FRU_Firmware,
12102 csevent_FRU_Unknown,
12103 "CTL: received task: %s",task_desc);
12107 csevent_log(CSC_CTL | CSC_SHELF_SW |
12109 csevent_LogType_Trace,
12110 csevent_Severity_Information,
12111 csevent_AlertLevel_Green,
12112 csevent_FRU_Firmware,
12113 csevent_FRU_Unknown,
12114 "CTL: received unknown task "
12116 io->taskio.task_action,
12117 io->taskio.task_action);
12120 switch (io->taskio.task_action) {
12121 case CTL_TASK_ABORT_TASK:
12122 retval = ctl_abort_task(io);
12124 case CTL_TASK_ABORT_TASK_SET:
12125 case CTL_TASK_CLEAR_TASK_SET:
12126 retval = ctl_abort_task_set(io);
12128 case CTL_TASK_CLEAR_ACA:
12130 case CTL_TASK_I_T_NEXUS_RESET:
12131 retval = ctl_i_t_nexus_reset(io);
12133 case CTL_TASK_LUN_RESET: {
12134 struct ctl_lun *lun;
12137 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12138 mtx_lock(&ctl_softc->ctl_lock);
12139 if ((targ_lun < CTL_MAX_LUNS)
12140 && (ctl_softc->ctl_luns[targ_lun] != NULL))
12141 lun = ctl_softc->ctl_luns[targ_lun];
12143 mtx_unlock(&ctl_softc->ctl_lock);
12148 if (!(io->io_hdr.flags &
12149 CTL_FLAG_FROM_OTHER_SC)) {
12150 union ctl_ha_msg msg_info;
12152 io->io_hdr.flags |=
12153 CTL_FLAG_SENT_2OTHER_SC;
12154 msg_info.hdr.msg_type =
12155 CTL_MSG_MANAGE_TASKS;
12156 msg_info.hdr.nexus = io->io_hdr.nexus;
12157 msg_info.task.task_action =
12158 CTL_TASK_LUN_RESET;
12159 msg_info.hdr.original_sc = NULL;
12160 msg_info.hdr.serializing_sc = NULL;
12161 if (CTL_HA_STATUS_SUCCESS !=
12162 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12164 sizeof(msg_info), 0)) {
12168 retval = ctl_lun_reset(lun, io,
12170 mtx_unlock(&ctl_softc->ctl_lock);
12173 case CTL_TASK_TARGET_RESET:
12174 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET);
12176 case CTL_TASK_BUS_RESET:
12177 retval = ctl_bus_reset(ctl_softc, io);
12179 case CTL_TASK_PORT_LOGIN:
12181 case CTL_TASK_PORT_LOGOUT:
12184 printf("ctl_run_task: got unknown task management event %d\n",
12185 io->taskio.task_action);
12189 io->io_hdr.status = CTL_SUCCESS;
12191 io->io_hdr.status = CTL_ERROR;
12196 * For HA operation. Handle commands that come in from the other
12200 ctl_handle_isc(union ctl_io *io)
12203 struct ctl_lun *lun;
12204 struct ctl_softc *ctl_softc;
12207 ctl_softc = control_softc;
12209 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12210 lun = ctl_softc->ctl_luns[targ_lun];
12212 switch (io->io_hdr.msg_type) {
12213 case CTL_MSG_SERIALIZE:
12214 free_io = ctl_serialize_other_sc_cmd(&io->scsiio);
12216 case CTL_MSG_R2R: {
12217 const struct ctl_cmd_entry *entry;
12220 * This is only used in SER_ONLY mode.
12223 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12224 mtx_lock(&lun->lun_lock);
12225 if (ctl_scsiio_lun_check(ctl_softc, lun,
12226 entry, (struct ctl_scsiio *)io) != 0) {
12227 mtx_unlock(&lun->lun_lock);
12231 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12232 mtx_unlock(&lun->lun_lock);
12233 ctl_enqueue_rtr(io);
12236 case CTL_MSG_FINISH_IO:
12237 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
12242 mtx_lock(&lun->lun_lock);
12243 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
12245 ctl_check_blocked(lun);
12246 mtx_unlock(&lun->lun_lock);
12249 case CTL_MSG_PERS_ACTION:
12250 ctl_hndl_per_res_out_on_other_sc(
12251 (union ctl_ha_msg *)&io->presio.pr_msg);
12254 case CTL_MSG_BAD_JUJU:
12258 case CTL_MSG_DATAMOVE:
12259 /* Only used in XFER mode */
12261 ctl_datamove_remote(io);
12263 case CTL_MSG_DATAMOVE_DONE:
12264 /* Only used in XFER mode */
12266 io->scsiio.be_move_done(io);
12270 printf("%s: Invalid message type %d\n",
12271 __func__, io->io_hdr.msg_type);
12281 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12282 * there is no match.
12284 static ctl_lun_error_pattern
12285 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12287 const struct ctl_cmd_entry *entry;
12288 ctl_lun_error_pattern filtered_pattern, pattern;
12290 pattern = desc->error_pattern;
12293 * XXX KDM we need more data passed into this function to match a
12294 * custom pattern, and we actually need to implement custom pattern
12297 if (pattern & CTL_LUN_PAT_CMD)
12298 return (CTL_LUN_PAT_CMD);
12300 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12301 return (CTL_LUN_PAT_ANY);
12303 entry = ctl_get_cmd_entry(ctsio, NULL);
12305 filtered_pattern = entry->pattern & pattern;
12308 * If the user requested specific flags in the pattern (e.g.
12309 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12312 * If the user did not specify any flags, it doesn't matter whether
12313 * or not the command supports the flags.
12315 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12316 (pattern & ~CTL_LUN_PAT_MASK))
12317 return (CTL_LUN_PAT_NONE);
12320 * If the user asked for a range check, see if the requested LBA
12321 * range overlaps with this command's LBA range.
12323 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12329 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12331 return (CTL_LUN_PAT_NONE);
12333 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12334 desc->lba_range.len);
12336 * A "pass" means that the LBA ranges don't overlap, so
12337 * this doesn't match the user's range criteria.
12339 if (action == CTL_ACTION_PASS)
12340 return (CTL_LUN_PAT_NONE);
12343 return (filtered_pattern);
12347 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12349 struct ctl_error_desc *desc, *desc2;
12351 mtx_assert(&lun->lun_lock, MA_OWNED);
12353 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12354 ctl_lun_error_pattern pattern;
12356 * Check to see whether this particular command matches
12357 * the pattern in the descriptor.
12359 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12360 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12363 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12364 case CTL_LUN_INJ_ABORTED:
12365 ctl_set_aborted(&io->scsiio);
12367 case CTL_LUN_INJ_MEDIUM_ERR:
12368 ctl_set_medium_error(&io->scsiio);
12370 case CTL_LUN_INJ_UA:
12371 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12373 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12375 case CTL_LUN_INJ_CUSTOM:
12377 * We're assuming the user knows what he is doing.
12378 * Just copy the sense information without doing
12381 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12382 ctl_min(sizeof(desc->custom_sense),
12383 sizeof(io->scsiio.sense_data)));
12384 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12385 io->scsiio.sense_len = SSD_FULL_SIZE;
12386 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12388 case CTL_LUN_INJ_NONE:
12391 * If this is an error injection type we don't know
12392 * about, clear the continuous flag (if it is set)
12393 * so it will get deleted below.
12395 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12399 * By default, each error injection action is a one-shot
12401 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12404 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12410 #ifdef CTL_IO_DELAY
12412 ctl_datamove_timer_wakeup(void *arg)
12416 io = (union ctl_io *)arg;
12420 #endif /* CTL_IO_DELAY */
12423 ctl_datamove(union ctl_io *io)
12425 void (*fe_datamove)(union ctl_io *io);
12427 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12429 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12432 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12437 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12438 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12440 sbuf_cat(&sb, path_str);
12441 switch (io->io_hdr.io_type) {
12443 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12444 sbuf_printf(&sb, "\n");
12445 sbuf_cat(&sb, path_str);
12446 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12447 io->scsiio.tag_num, io->scsiio.tag_type);
12450 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12451 "Tag Type: %d\n", io->taskio.task_action,
12452 io->taskio.tag_num, io->taskio.tag_type);
12455 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12456 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12459 sbuf_cat(&sb, path_str);
12460 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12461 (intmax_t)time_uptime - io->io_hdr.start_time);
12463 printf("%s", sbuf_data(&sb));
12465 #endif /* CTL_TIME_IO */
12467 #ifdef CTL_IO_DELAY
12468 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12469 struct ctl_lun *lun;
12471 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12473 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12475 struct ctl_lun *lun;
12477 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12479 && (lun->delay_info.datamove_delay > 0)) {
12480 struct callout *callout;
12482 callout = (struct callout *)&io->io_hdr.timer_bytes;
12483 callout_init(callout, /*mpsafe*/ 1);
12484 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12485 callout_reset(callout,
12486 lun->delay_info.datamove_delay * hz,
12487 ctl_datamove_timer_wakeup, io);
12488 if (lun->delay_info.datamove_type ==
12489 CTL_DELAY_TYPE_ONESHOT)
12490 lun->delay_info.datamove_delay = 0;
12497 * This command has been aborted. Set the port status, so we fail
12500 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12501 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n",
12502 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id,
12503 io->io_hdr.nexus.targ_port,
12504 (uintmax_t)io->io_hdr.nexus.targ_target.id,
12505 io->io_hdr.nexus.targ_lun);
12506 io->io_hdr.port_status = 31337;
12508 * Note that the backend, in this case, will get the
12509 * callback in its context. In other cases it may get
12510 * called in the frontend's interrupt thread context.
12512 io->scsiio.be_move_done(io);
12516 /* Don't confuse frontend with zero length data move. */
12517 if (io->scsiio.kern_data_len == 0) {
12518 io->scsiio.be_move_done(io);
12523 * If we're in XFER mode and this I/O is from the other shelf
12524 * controller, we need to send the DMA to the other side to
12525 * actually transfer the data to/from the host. In serialize only
12526 * mode the transfer happens below CTL and ctl_datamove() is only
12527 * called on the machine that originally received the I/O.
12529 if ((control_softc->ha_mode == CTL_HA_MODE_XFER)
12530 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12531 union ctl_ha_msg msg;
12532 uint32_t sg_entries_sent;
12536 memset(&msg, 0, sizeof(msg));
12537 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
12538 msg.hdr.original_sc = io->io_hdr.original_sc;
12539 msg.hdr.serializing_sc = io;
12540 msg.hdr.nexus = io->io_hdr.nexus;
12541 msg.dt.flags = io->io_hdr.flags;
12543 * We convert everything into a S/G list here. We can't
12544 * pass by reference, only by value between controllers.
12545 * So we can't pass a pointer to the S/G list, only as many
12546 * S/G entries as we can fit in here. If it's possible for
12547 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
12548 * then we need to break this up into multiple transfers.
12550 if (io->scsiio.kern_sg_entries == 0) {
12551 msg.dt.kern_sg_entries = 1;
12553 * If this is in cached memory, flush the cache
12554 * before we send the DMA request to the other
12555 * controller. We want to do this in either the
12556 * read or the write case. The read case is
12557 * straightforward. In the write case, we want to
12558 * make sure nothing is in the local cache that
12559 * could overwrite the DMAed data.
12561 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12563 * XXX KDM use bus_dmamap_sync() here.
12568 * Convert to a physical address if this is a
12571 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
12572 msg.dt.sg_list[0].addr =
12573 io->scsiio.kern_data_ptr;
12576 * XXX KDM use busdma here!
12579 msg.dt.sg_list[0].addr = (void *)
12580 vtophys(io->scsiio.kern_data_ptr);
12584 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
12587 struct ctl_sg_entry *sgl;
12590 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
12591 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
12592 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12594 * XXX KDM use bus_dmamap_sync() here.
12599 msg.dt.kern_data_len = io->scsiio.kern_data_len;
12600 msg.dt.kern_total_len = io->scsiio.kern_total_len;
12601 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
12602 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
12603 msg.dt.sg_sequence = 0;
12606 * Loop until we've sent all of the S/G entries. On the
12607 * other end, we'll recompose these S/G entries into one
12608 * contiguous list before passing it to the
12610 for (sg_entries_sent = 0; sg_entries_sent <
12611 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) {
12612 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/
12613 sizeof(msg.dt.sg_list[0])),
12614 msg.dt.kern_sg_entries - sg_entries_sent);
12616 if (do_sg_copy != 0) {
12617 struct ctl_sg_entry *sgl;
12620 sgl = (struct ctl_sg_entry *)
12621 io->scsiio.kern_data_ptr;
12623 * If this is in cached memory, flush the cache
12624 * before we send the DMA request to the other
12625 * controller. We want to do this in either
12626 * the * read or the write case. The read
12627 * case is straightforward. In the write
12628 * case, we want to make sure nothing is
12629 * in the local cache that could overwrite
12633 for (i = sg_entries_sent, j = 0;
12634 i < msg.dt.cur_sg_entries; i++, j++) {
12635 if ((io->io_hdr.flags &
12636 CTL_FLAG_NO_DATASYNC) == 0) {
12638 * XXX KDM use bus_dmamap_sync()
12641 if ((io->io_hdr.flags &
12642 CTL_FLAG_BUS_ADDR) == 0) {
12644 * XXX KDM use busdma.
12647 msg.dt.sg_list[j].addr =(void *)
12648 vtophys(sgl[i].addr);
12651 msg.dt.sg_list[j].addr =
12654 msg.dt.sg_list[j].len = sgl[i].len;
12658 sg_entries_sent += msg.dt.cur_sg_entries;
12659 if (sg_entries_sent >= msg.dt.kern_sg_entries)
12660 msg.dt.sg_last = 1;
12662 msg.dt.sg_last = 0;
12665 * XXX KDM drop and reacquire the lock here?
12667 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12668 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
12670 * XXX do something here.
12674 msg.dt.sent_sg_entries = sg_entries_sent;
12676 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12677 if (io->io_hdr.flags & CTL_FLAG_FAILOVER)
12678 ctl_failover_io(io, /*have_lock*/ 0);
12683 * Lookup the fe_datamove() function for this particular
12687 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12694 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12696 union ctl_ha_msg msg;
12699 memset(&msg, 0, sizeof(msg));
12701 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12702 msg.hdr.original_sc = io;
12703 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12704 msg.hdr.nexus = io->io_hdr.nexus;
12705 msg.hdr.status = io->io_hdr.status;
12706 msg.scsi.tag_num = io->scsiio.tag_num;
12707 msg.scsi.tag_type = io->scsiio.tag_type;
12708 msg.scsi.scsi_status = io->scsiio.scsi_status;
12709 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12710 sizeof(io->scsiio.sense_data));
12711 msg.scsi.sense_len = io->scsiio.sense_len;
12712 msg.scsi.sense_residual = io->scsiio.sense_residual;
12713 msg.scsi.fetd_status = io->io_hdr.port_status;
12714 msg.scsi.residual = io->scsiio.residual;
12715 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12717 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12718 ctl_failover_io(io, /*have_lock*/ have_lock);
12722 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0);
12723 if (isc_status > CTL_HA_STATUS_SUCCESS) {
12724 /* XXX do something if this fails */
12730 * The DMA to the remote side is done, now we need to tell the other side
12731 * we're done so it can continue with its data movement.
12734 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12740 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12741 printf("%s: ISC DMA write failed with error %d", __func__,
12743 ctl_set_internal_failure(&io->scsiio,
12745 /*retry_count*/ rq->ret);
12748 ctl_dt_req_free(rq);
12751 * In this case, we had to malloc the memory locally. Free it.
12753 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12755 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12756 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12759 * The data is in local and remote memory, so now we need to send
12760 * status (good or back) back to the other side.
12762 ctl_send_datamove_done(io, /*have_lock*/ 0);
12766 * We've moved the data from the host/controller into local memory. Now we
12767 * need to push it over to the remote controller's memory.
12770 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12776 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12777 ctl_datamove_remote_write_cb);
12783 ctl_datamove_remote_write(union ctl_io *io)
12786 void (*fe_datamove)(union ctl_io *io);
12789 * - Get the data from the host/HBA into local memory.
12790 * - DMA memory from the local controller to the remote controller.
12791 * - Send status back to the remote controller.
12794 retval = ctl_datamove_remote_sgl_setup(io);
12798 /* Switch the pointer over so the FETD knows what to do */
12799 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12802 * Use a custom move done callback, since we need to send completion
12803 * back to the other controller, not to the backend on this side.
12805 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12807 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12816 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12825 * In this case, we had to malloc the memory locally. Free it.
12827 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12829 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12830 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12834 scsi_path_string(io, path_str, sizeof(path_str));
12835 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12836 sbuf_cat(&sb, path_str);
12837 scsi_command_string(&io->scsiio, NULL, &sb);
12838 sbuf_printf(&sb, "\n");
12839 sbuf_cat(&sb, path_str);
12840 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12841 io->scsiio.tag_num, io->scsiio.tag_type);
12842 sbuf_cat(&sb, path_str);
12843 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12844 io->io_hdr.flags, io->io_hdr.status);
12846 printk("%s", sbuf_data(&sb));
12851 * The read is done, now we need to send status (good or bad) back
12852 * to the other side.
12854 ctl_send_datamove_done(io, /*have_lock*/ 0);
12860 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12863 void (*fe_datamove)(union ctl_io *io);
12867 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12868 printf("%s: ISC DMA read failed with error %d", __func__,
12870 ctl_set_internal_failure(&io->scsiio,
12872 /*retry_count*/ rq->ret);
12875 ctl_dt_req_free(rq);
12877 /* Switch the pointer over so the FETD knows what to do */
12878 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12881 * Use a custom move done callback, since we need to send completion
12882 * back to the other controller, not to the backend on this side.
12884 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12886 /* XXX KDM add checks like the ones in ctl_datamove? */
12888 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12894 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12896 struct ctl_sg_entry *local_sglist, *remote_sglist;
12897 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist;
12898 struct ctl_softc *softc;
12903 softc = control_softc;
12905 local_sglist = io->io_hdr.local_sglist;
12906 local_dma_sglist = io->io_hdr.local_dma_sglist;
12907 remote_sglist = io->io_hdr.remote_sglist;
12908 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
12910 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) {
12911 for (i = 0; i < io->scsiio.kern_sg_entries; i++) {
12912 local_sglist[i].len = remote_sglist[i].len;
12915 * XXX Detect the situation where the RS-level I/O
12916 * redirector on the other side has already read the
12917 * data off of the AOR RS on this side, and
12918 * transferred it to remote (mirror) memory on the
12919 * other side. Since we already have the data in
12920 * memory here, we just need to use it.
12922 * XXX KDM this can probably be removed once we
12923 * get the cache device code in and take the
12924 * current AOR implementation out.
12927 if ((remote_sglist[i].addr >=
12928 (void *)vtophys(softc->mirr->addr))
12929 && (remote_sglist[i].addr <
12930 ((void *)vtophys(softc->mirr->addr) +
12931 CacheMirrorOffset))) {
12932 local_sglist[i].addr = remote_sglist[i].addr -
12934 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12936 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE;
12938 local_sglist[i].addr = remote_sglist[i].addr +
12943 printf("%s: local %p, remote %p, len %d\n",
12944 __func__, local_sglist[i].addr,
12945 remote_sglist[i].addr, local_sglist[i].len);
12949 uint32_t len_to_go;
12952 * In this case, we don't have automatically allocated
12953 * memory for this I/O on this controller. This typically
12954 * happens with internal CTL I/O -- e.g. inquiry, mode
12955 * sense, etc. Anything coming from RAIDCore will have
12956 * a mirror area available.
12958 len_to_go = io->scsiio.kern_data_len;
12961 * Clear the no datasync flag, we have to use malloced
12964 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC;
12967 * The difficult thing here is that the size of the various
12968 * S/G segments may be different than the size from the
12969 * remote controller. That'll make it harder when DMAing
12970 * the data back to the other side.
12972 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) /
12973 sizeof(io->io_hdr.remote_sglist[0])) &&
12974 (len_to_go > 0); i++) {
12975 local_sglist[i].len = ctl_min(len_to_go, 131072);
12976 CTL_SIZE_8B(local_dma_sglist[i].len,
12977 local_sglist[i].len);
12978 local_sglist[i].addr =
12979 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK);
12981 local_dma_sglist[i].addr = local_sglist[i].addr;
12983 if (local_sglist[i].addr == NULL) {
12986 printf("malloc failed for %zd bytes!",
12987 local_dma_sglist[i].len);
12988 for (j = 0; j < i; j++) {
12989 free(local_sglist[j].addr, M_CTL);
12991 ctl_set_internal_failure(&io->scsiio,
12993 /*retry_count*/ 4857);
12995 goto bailout_error;
12998 /* XXX KDM do we need a sync here? */
13000 len_to_go -= local_sglist[i].len;
13003 * Reset the number of S/G entries accordingly. The
13004 * original number of S/G entries is available in
13007 io->scsiio.kern_sg_entries = i;
13010 printf("%s: kern_sg_entries = %d\n", __func__,
13011 io->scsiio.kern_sg_entries);
13012 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13013 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i,
13014 local_sglist[i].addr, local_sglist[i].len,
13015 local_dma_sglist[i].len);
13024 ctl_send_datamove_done(io, /*have_lock*/ 0);
13030 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
13031 ctl_ha_dt_cb callback)
13033 struct ctl_ha_dt_req *rq;
13034 struct ctl_sg_entry *remote_sglist, *local_sglist;
13035 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist;
13036 uint32_t local_used, remote_used, total_used;
13042 rq = ctl_dt_req_alloc();
13045 * If we failed to allocate the request, and if the DMA didn't fail
13046 * anyway, set busy status. This is just a resource allocation
13050 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE))
13051 ctl_set_busy(&io->scsiio);
13053 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
13056 ctl_dt_req_free(rq);
13059 * The data move failed. We need to return status back
13060 * to the other controller. No point in trying to DMA
13061 * data to the remote controller.
13064 ctl_send_datamove_done(io, /*have_lock*/ 0);
13071 local_sglist = io->io_hdr.local_sglist;
13072 local_dma_sglist = io->io_hdr.local_dma_sglist;
13073 remote_sglist = io->io_hdr.remote_sglist;
13074 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
13079 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) {
13080 rq->ret = CTL_HA_STATUS_SUCCESS;
13087 * Pull/push the data over the wire from/to the other controller.
13088 * This takes into account the possibility that the local and
13089 * remote sglists may not be identical in terms of the size of
13090 * the elements and the number of elements.
13092 * One fundamental assumption here is that the length allocated for
13093 * both the local and remote sglists is identical. Otherwise, we've
13094 * essentially got a coding error of some sort.
13096 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
13098 uint32_t cur_len, dma_length;
13101 rq->id = CTL_HA_DATA_CTL;
13102 rq->command = command;
13106 * Both pointers should be aligned. But it is possible
13107 * that the allocation length is not. They should both
13108 * also have enough slack left over at the end, though,
13109 * to round up to the next 8 byte boundary.
13111 cur_len = ctl_min(local_sglist[i].len - local_used,
13112 remote_sglist[j].len - remote_used);
13115 * In this case, we have a size issue and need to decrease
13116 * the size, except in the case where we actually have less
13117 * than 8 bytes left. In that case, we need to increase
13118 * the DMA length to get the last bit.
13120 if ((cur_len & 0x7) != 0) {
13121 if (cur_len > 0x7) {
13122 cur_len = cur_len - (cur_len & 0x7);
13123 dma_length = cur_len;
13125 CTL_SIZE_8B(dma_length, cur_len);
13129 dma_length = cur_len;
13132 * If we had to allocate memory for this I/O, instead of using
13133 * the non-cached mirror memory, we'll need to flush the cache
13134 * before trying to DMA to the other controller.
13136 * We could end up doing this multiple times for the same
13137 * segment if we have a larger local segment than remote
13138 * segment. That shouldn't be an issue.
13140 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
13142 * XXX KDM use bus_dmamap_sync() here.
13146 rq->size = dma_length;
13148 tmp_ptr = (uint8_t *)local_sglist[i].addr;
13149 tmp_ptr += local_used;
13151 /* Use physical addresses when talking to ISC hardware */
13152 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
13153 /* XXX KDM use busdma */
13155 rq->local = vtophys(tmp_ptr);
13158 rq->local = tmp_ptr;
13160 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
13161 tmp_ptr += remote_used;
13162 rq->remote = tmp_ptr;
13164 rq->callback = NULL;
13166 local_used += cur_len;
13167 if (local_used >= local_sglist[i].len) {
13172 remote_used += cur_len;
13173 if (remote_used >= remote_sglist[j].len) {
13177 total_used += cur_len;
13179 if (total_used >= io->scsiio.kern_data_len)
13180 rq->callback = callback;
13182 if ((rq->size & 0x7) != 0) {
13183 printf("%s: warning: size %d is not on 8b boundary\n",
13184 __func__, rq->size);
13186 if (((uintptr_t)rq->local & 0x7) != 0) {
13187 printf("%s: warning: local %p not on 8b boundary\n",
13188 __func__, rq->local);
13190 if (((uintptr_t)rq->remote & 0x7) != 0) {
13191 printf("%s: warning: remote %p not on 8b boundary\n",
13192 __func__, rq->local);
13195 printf("%s: %s: local %#x remote %#x size %d\n", __func__,
13196 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
13197 rq->local, rq->remote, rq->size);
13200 isc_ret = ctl_dt_single(rq);
13201 if (isc_ret == CTL_HA_STATUS_WAIT)
13204 if (isc_ret == CTL_HA_STATUS_DISCONNECT) {
13205 rq->ret = CTL_HA_STATUS_SUCCESS;
13219 ctl_datamove_remote_read(union ctl_io *io)
13225 * This will send an error to the other controller in the case of a
13228 retval = ctl_datamove_remote_sgl_setup(io);
13232 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
13233 ctl_datamove_remote_read_cb);
13235 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) {
13237 * Make sure we free memory if there was an error.. The
13238 * ctl_datamove_remote_xfer() function will send the
13239 * datamove done message, or call the callback with an
13240 * error if there is a problem.
13242 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13243 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13250 * Process a datamove request from the other controller. This is used for
13251 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
13252 * first. Once that is complete, the data gets DMAed into the remote
13253 * controller's memory. For reads, we DMA from the remote controller's
13254 * memory into our memory first, and then move it out to the FETD.
13257 ctl_datamove_remote(union ctl_io *io)
13259 struct ctl_softc *softc;
13261 softc = control_softc;
13263 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
13266 * Note that we look for an aborted I/O here, but don't do some of
13267 * the other checks that ctl_datamove() normally does.
13268 * We don't need to run the datamove delay code, since that should
13269 * have been done if need be on the other controller.
13271 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
13272 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__,
13273 io->scsiio.tag_num, io->io_hdr.nexus.initid.id,
13274 io->io_hdr.nexus.targ_port,
13275 io->io_hdr.nexus.targ_target.id,
13276 io->io_hdr.nexus.targ_lun);
13277 io->io_hdr.port_status = 31338;
13278 ctl_send_datamove_done(io, /*have_lock*/ 0);
13282 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) {
13283 ctl_datamove_remote_write(io);
13284 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){
13285 ctl_datamove_remote_read(io);
13287 union ctl_ha_msg msg;
13288 struct scsi_sense_data *sense;
13292 memset(&msg, 0, sizeof(msg));
13294 msg.hdr.msg_type = CTL_MSG_BAD_JUJU;
13295 msg.hdr.status = CTL_SCSI_ERROR;
13296 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
13298 retry_count = 4243;
13300 sense = &msg.scsi.sense_data;
13301 sks[0] = SSD_SCS_VALID;
13302 sks[1] = (retry_count >> 8) & 0xff;
13303 sks[2] = retry_count & 0xff;
13305 /* "Internal target failure" */
13306 scsi_set_sense_data(sense,
13307 /*sense_format*/ SSD_TYPE_NONE,
13308 /*current_error*/ 1,
13309 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
13312 /*type*/ SSD_ELEM_SKS,
13313 /*size*/ sizeof(sks),
13317 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
13318 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
13319 ctl_failover_io(io, /*have_lock*/ 1);
13323 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) >
13324 CTL_HA_STATUS_SUCCESS) {
13325 /* XXX KDM what to do if this fails? */
13333 ctl_process_done(union ctl_io *io)
13335 struct ctl_lun *lun;
13336 struct ctl_softc *ctl_softc = control_softc;
13337 void (*fe_done)(union ctl_io *io);
13338 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port);
13340 CTL_DEBUG_PRINT(("ctl_process_done\n"));
13343 control_softc->ctl_ports[targ_port]->fe_done;
13346 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
13351 ctl_scsi_path_string(io, path_str, sizeof(path_str));
13352 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13354 sbuf_cat(&sb, path_str);
13355 switch (io->io_hdr.io_type) {
13357 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
13358 sbuf_printf(&sb, "\n");
13359 sbuf_cat(&sb, path_str);
13360 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13361 io->scsiio.tag_num, io->scsiio.tag_type);
13364 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
13365 "Tag Type: %d\n", io->taskio.task_action,
13366 io->taskio.tag_num, io->taskio.tag_type);
13369 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13370 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13373 sbuf_cat(&sb, path_str);
13374 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
13375 (intmax_t)time_uptime - io->io_hdr.start_time);
13377 printf("%s", sbuf_data(&sb));
13379 #endif /* CTL_TIME_IO */
13381 switch (io->io_hdr.io_type) {
13385 if (bootverbose || (ctl_debug & CTL_DEBUG_INFO))
13386 ctl_io_error_print(io, NULL);
13387 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
13391 return (CTL_RETVAL_COMPLETE);
13393 panic("ctl_process_done: invalid io type %d\n",
13394 io->io_hdr.io_type);
13395 break; /* NOTREACHED */
13398 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13400 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13401 io->io_hdr.nexus.targ_mapped_lun));
13405 mtx_lock(&lun->lun_lock);
13408 * Check to see if we have any errors to inject here. We only
13409 * inject errors for commands that don't already have errors set.
13411 if ((STAILQ_FIRST(&lun->error_list) != NULL) &&
13412 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
13413 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
13414 ctl_inject_error(lun, io);
13417 * XXX KDM how do we treat commands that aren't completed
13420 * XXX KDM should we also track I/O latency?
13422 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13423 io->io_hdr.io_type == CTL_IO_SCSI) {
13425 struct bintime cur_bt;
13429 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13431 type = CTL_STATS_READ;
13432 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13434 type = CTL_STATS_WRITE;
13436 type = CTL_STATS_NO_IO;
13438 lun->stats.ports[targ_port].bytes[type] +=
13439 io->scsiio.kern_total_len;
13440 lun->stats.ports[targ_port].operations[type]++;
13442 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
13443 &io->io_hdr.dma_bt);
13444 lun->stats.ports[targ_port].num_dmas[type] +=
13445 io->io_hdr.num_dmas;
13446 getbintime(&cur_bt);
13447 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
13448 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
13453 * Remove this from the OOA queue.
13455 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13458 * Run through the blocked queue on this LUN and see if anything
13459 * has become unblocked, now that this transaction is done.
13461 ctl_check_blocked(lun);
13464 * If the LUN has been invalidated, free it if there is nothing
13465 * left on its OOA queue.
13467 if ((lun->flags & CTL_LUN_INVALID)
13468 && TAILQ_EMPTY(&lun->ooa_queue)) {
13469 mtx_unlock(&lun->lun_lock);
13470 mtx_lock(&ctl_softc->ctl_lock);
13472 mtx_unlock(&ctl_softc->ctl_lock);
13474 mtx_unlock(&lun->lun_lock);
13479 * If this command has been aborted, make sure we set the status
13480 * properly. The FETD is responsible for freeing the I/O and doing
13481 * whatever it needs to do to clean up its state.
13483 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13484 ctl_set_task_aborted(&io->scsiio);
13487 * If enabled, print command error status.
13488 * We don't print UAs unless debugging was enabled explicitly.
13491 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
13493 if (!bootverbose && (ctl_debug & CTL_DEBUG_INFO) == 0)
13495 if ((ctl_debug & CTL_DEBUG_INFO) == 0 &&
13496 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) &&
13497 (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) {
13498 int error_code, sense_key, asc, ascq;
13500 scsi_extract_sense_len(&io->scsiio.sense_data,
13501 io->scsiio.sense_len, &error_code, &sense_key,
13502 &asc, &ascq, /*show_errors*/ 0);
13503 if (sense_key == SSD_KEY_UNIT_ATTENTION)
13507 ctl_io_error_print(io, NULL);
13511 * Tell the FETD or the other shelf controller we're done with this
13512 * command. Note that only SCSI commands get to this point. Task
13513 * management commands are completed above.
13515 * We only send status to the other controller if we're in XFER
13516 * mode. In SER_ONLY mode, the I/O is done on the controller that
13517 * received the I/O (from CTL's perspective), and so the status is
13520 * XXX KDM if we hold the lock here, we could cause a deadlock
13521 * if the frontend comes back in in this context to queue
13524 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER)
13525 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
13526 union ctl_ha_msg msg;
13528 memset(&msg, 0, sizeof(msg));
13529 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13530 msg.hdr.original_sc = io->io_hdr.original_sc;
13531 msg.hdr.nexus = io->io_hdr.nexus;
13532 msg.hdr.status = io->io_hdr.status;
13533 msg.scsi.scsi_status = io->scsiio.scsi_status;
13534 msg.scsi.tag_num = io->scsiio.tag_num;
13535 msg.scsi.tag_type = io->scsiio.tag_type;
13536 msg.scsi.sense_len = io->scsiio.sense_len;
13537 msg.scsi.sense_residual = io->scsiio.sense_residual;
13538 msg.scsi.residual = io->scsiio.residual;
13539 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
13540 sizeof(io->scsiio.sense_data));
13542 * We copy this whether or not this is an I/O-related
13543 * command. Otherwise, we'd have to go and check to see
13544 * whether it's a read/write command, and it really isn't
13547 memcpy(&msg.scsi.lbalen,
13548 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
13549 sizeof(msg.scsi.lbalen));
13551 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13552 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
13553 /* XXX do something here */
13560 return (CTL_RETVAL_COMPLETE);
13565 * Front end should call this if it doesn't do autosense. When the request
13566 * sense comes back in from the initiator, we'll dequeue this and send it.
13569 ctl_queue_sense(union ctl_io *io)
13571 struct ctl_lun *lun;
13572 struct ctl_softc *ctl_softc;
13573 uint32_t initidx, targ_lun;
13575 ctl_softc = control_softc;
13577 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13580 * LUN lookup will likely move to the ctl_work_thread() once we
13581 * have our new queueing infrastructure (that doesn't put things on
13582 * a per-LUN queue initially). That is so that we can handle
13583 * things like an INQUIRY to a LUN that we don't have enabled. We
13584 * can't deal with that right now.
13586 mtx_lock(&ctl_softc->ctl_lock);
13589 * If we don't have a LUN for this, just toss the sense
13592 targ_lun = io->io_hdr.nexus.targ_lun;
13593 targ_lun = ctl_map_lun(io->io_hdr.nexus.targ_port, targ_lun);
13594 if ((targ_lun < CTL_MAX_LUNS)
13595 && (ctl_softc->ctl_luns[targ_lun] != NULL))
13596 lun = ctl_softc->ctl_luns[targ_lun];
13600 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13602 mtx_lock(&lun->lun_lock);
13604 * Already have CA set for this LUN...toss the sense information.
13606 if (ctl_is_set(lun->have_ca, initidx)) {
13607 mtx_unlock(&lun->lun_lock);
13611 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data,
13612 ctl_min(sizeof(lun->pending_sense[initidx]),
13613 sizeof(io->scsiio.sense_data)));
13614 ctl_set_mask(lun->have_ca, initidx);
13615 mtx_unlock(&lun->lun_lock);
13618 mtx_unlock(&ctl_softc->ctl_lock);
13622 return (CTL_RETVAL_COMPLETE);
13627 * Primary command inlet from frontend ports. All SCSI and task I/O
13628 * requests must go through this function.
13631 ctl_queue(union ctl_io *io)
13633 struct ctl_softc *ctl_softc;
13635 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13637 ctl_softc = control_softc;
13640 io->io_hdr.start_time = time_uptime;
13641 getbintime(&io->io_hdr.start_bt);
13642 #endif /* CTL_TIME_IO */
13644 /* Map FE-specific LUN ID into global one. */
13645 io->io_hdr.nexus.targ_mapped_lun =
13646 ctl_map_lun(io->io_hdr.nexus.targ_port, io->io_hdr.nexus.targ_lun);
13648 switch (io->io_hdr.io_type) {
13651 if (ctl_debug & CTL_DEBUG_CDB)
13653 ctl_enqueue_incoming(io);
13656 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13660 return (CTL_RETVAL_COMPLETE);
13663 #ifdef CTL_IO_DELAY
13665 ctl_done_timer_wakeup(void *arg)
13669 io = (union ctl_io *)arg;
13672 #endif /* CTL_IO_DELAY */
13675 ctl_done(union ctl_io *io)
13677 struct ctl_softc *ctl_softc;
13679 ctl_softc = control_softc;
13682 * Enable this to catch duplicate completion issues.
13685 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13686 printf("%s: type %d msg %d cdb %x iptl: "
13687 "%d:%d:%d:%d tag 0x%04x "
13688 "flag %#x status %x\n",
13690 io->io_hdr.io_type,
13691 io->io_hdr.msg_type,
13693 io->io_hdr.nexus.initid.id,
13694 io->io_hdr.nexus.targ_port,
13695 io->io_hdr.nexus.targ_target.id,
13696 io->io_hdr.nexus.targ_lun,
13697 (io->io_hdr.io_type ==
13699 io->taskio.tag_num :
13700 io->scsiio.tag_num,
13702 io->io_hdr.status);
13704 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13708 * This is an internal copy of an I/O, and should not go through
13709 * the normal done processing logic.
13711 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13715 * We need to send a msg to the serializing shelf to finish the IO
13716 * as well. We don't send a finish message to the other shelf if
13717 * this is a task management command. Task management commands
13718 * aren't serialized in the OOA queue, but rather just executed on
13719 * both shelf controllers for commands that originated on that
13722 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)
13723 && (io->io_hdr.io_type != CTL_IO_TASK)) {
13724 union ctl_ha_msg msg_io;
13726 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO;
13727 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc;
13728 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io,
13729 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) {
13731 /* continue on to finish IO */
13733 #ifdef CTL_IO_DELAY
13734 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13735 struct ctl_lun *lun;
13737 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13739 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13741 struct ctl_lun *lun;
13743 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13746 && (lun->delay_info.done_delay > 0)) {
13747 struct callout *callout;
13749 callout = (struct callout *)&io->io_hdr.timer_bytes;
13750 callout_init(callout, /*mpsafe*/ 1);
13751 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13752 callout_reset(callout,
13753 lun->delay_info.done_delay * hz,
13754 ctl_done_timer_wakeup, io);
13755 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13756 lun->delay_info.done_delay = 0;
13760 #endif /* CTL_IO_DELAY */
13762 ctl_enqueue_done(io);
13766 ctl_isc(struct ctl_scsiio *ctsio)
13768 struct ctl_lun *lun;
13771 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13773 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0]));
13775 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n"));
13777 retval = lun->backend->data_submit((union ctl_io *)ctsio);
13784 ctl_work_thread(void *arg)
13786 struct ctl_thread *thr = (struct ctl_thread *)arg;
13787 struct ctl_softc *softc = thr->ctl_softc;
13791 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13797 * We handle the queues in this order:
13799 * - done queue (to free up resources, unblock other commands)
13803 * If those queues are empty, we break out of the loop and
13806 mtx_lock(&thr->queue_lock);
13807 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13809 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13810 mtx_unlock(&thr->queue_lock);
13811 ctl_handle_isc(io);
13814 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13816 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13817 /* clear any blocked commands, call fe_done */
13818 mtx_unlock(&thr->queue_lock);
13819 retval = ctl_process_done(io);
13822 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13824 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13825 mtx_unlock(&thr->queue_lock);
13826 if (io->io_hdr.io_type == CTL_IO_TASK)
13829 ctl_scsiio_precheck(softc, &io->scsiio);
13832 if (!ctl_pause_rtr) {
13833 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13835 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13836 mtx_unlock(&thr->queue_lock);
13837 retval = ctl_scsiio(&io->scsiio);
13838 if (retval != CTL_RETVAL_COMPLETE)
13839 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13844 /* Sleep until we have something to do. */
13845 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13850 ctl_lun_thread(void *arg)
13852 struct ctl_softc *softc = (struct ctl_softc *)arg;
13853 struct ctl_be_lun *be_lun;
13856 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13860 mtx_lock(&softc->ctl_lock);
13861 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13862 if (be_lun != NULL) {
13863 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13864 mtx_unlock(&softc->ctl_lock);
13865 ctl_create_lun(be_lun);
13869 /* Sleep until we have something to do. */
13870 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13871 PDROP | PRIBIO, "-", 0);
13876 ctl_thresh_thread(void *arg)
13878 struct ctl_softc *softc = (struct ctl_softc *)arg;
13879 struct ctl_lun *lun;
13880 struct ctl_be_lun *be_lun;
13881 struct scsi_da_rw_recovery_page *rwpage;
13882 struct ctl_logical_block_provisioning_page *page;
13884 uint64_t thres, val;
13887 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13890 mtx_lock(&softc->ctl_lock);
13891 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13892 be_lun = lun->be_lun;
13893 if ((lun->flags & CTL_LUN_DISABLED) ||
13894 (lun->flags & CTL_LUN_OFFLINE) ||
13895 (be_lun->flags & CTL_LUN_FLAG_UNMAP) == 0 ||
13896 lun->backend->lun_attr == NULL)
13898 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT];
13899 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0)
13902 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT];
13903 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13904 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13906 thres = scsi_4btoul(page->descr[i].count);
13907 thres <<= CTL_LBP_EXPONENT;
13908 switch (page->descr[i].resource) {
13910 attr = "blocksavail";
13913 attr = "blocksused";
13916 attr = "poolblocksavail";
13919 attr = "poolblocksused";
13924 mtx_unlock(&softc->ctl_lock); // XXX
13925 val = lun->backend->lun_attr(
13926 lun->be_lun->be_lun, attr);
13927 mtx_lock(&softc->ctl_lock);
13928 if (val == UINT64_MAX)
13930 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13931 == SLBPPD_ARMING_INC)
13932 e |= (val >= thres);
13934 e |= (val <= thres);
13936 mtx_lock(&lun->lun_lock);
13938 if (lun->lasttpt == 0 ||
13939 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13940 lun->lasttpt = time_uptime;
13941 for (i = 0; i < CTL_MAX_INITIATORS; i++)
13942 lun->pending_ua[i] |=
13943 CTL_UA_THIN_PROV_THRES;
13947 for (i = 0; i < CTL_MAX_INITIATORS; i++)
13948 lun->pending_ua[i] &= ~CTL_UA_THIN_PROV_THRES;
13950 mtx_unlock(&lun->lun_lock);
13952 mtx_unlock(&softc->ctl_lock);
13953 pause("-", CTL_LBP_PERIOD * hz);
13958 ctl_enqueue_incoming(union ctl_io *io)
13960 struct ctl_softc *softc = control_softc;
13961 struct ctl_thread *thr;
13964 idx = (io->io_hdr.nexus.targ_port * 127 +
13965 io->io_hdr.nexus.initid.id) % worker_threads;
13966 thr = &softc->threads[idx];
13967 mtx_lock(&thr->queue_lock);
13968 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13969 mtx_unlock(&thr->queue_lock);
13974 ctl_enqueue_rtr(union ctl_io *io)
13976 struct ctl_softc *softc = control_softc;
13977 struct ctl_thread *thr;
13979 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13980 mtx_lock(&thr->queue_lock);
13981 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13982 mtx_unlock(&thr->queue_lock);
13987 ctl_enqueue_done(union ctl_io *io)
13989 struct ctl_softc *softc = control_softc;
13990 struct ctl_thread *thr;
13992 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13993 mtx_lock(&thr->queue_lock);
13994 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13995 mtx_unlock(&thr->queue_lock);
14000 ctl_enqueue_isc(union ctl_io *io)
14002 struct ctl_softc *softc = control_softc;
14003 struct ctl_thread *thr;
14005 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14006 mtx_lock(&thr->queue_lock);
14007 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
14008 mtx_unlock(&thr->queue_lock);
14012 /* Initialization and failover */
14015 ctl_init_isc_msg(void)
14017 printf("CTL: Still calling this thing\n");
14022 * Initializes component into configuration defined by bootMode
14024 * returns hasc_Status:
14026 * ERROR - fatal error
14028 static ctl_ha_comp_status
14029 ctl_isc_init(struct ctl_ha_component *c)
14031 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
14038 * Starts component in state requested. If component starts successfully,
14039 * it must set its own state to the requestrd state
14040 * When requested state is HASC_STATE_HA, the component may refine it
14041 * by adding _SLAVE or _MASTER flags.
14042 * Currently allowed state transitions are:
14043 * UNKNOWN->HA - initial startup
14044 * UNKNOWN->SINGLE - initial startup when no parter detected
14045 * HA->SINGLE - failover
14046 * returns ctl_ha_comp_status:
14047 * OK - component successfully started in requested state
14048 * FAILED - could not start the requested state, failover may
14050 * ERROR - fatal error detected, no future startup possible
14052 static ctl_ha_comp_status
14053 ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state)
14055 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
14057 printf("%s: go\n", __func__);
14059 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap)
14060 if (c->state == CTL_HA_STATE_UNKNOWN ) {
14061 control_softc->is_single = 0;
14062 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
14063 != CTL_HA_STATUS_SUCCESS) {
14064 printf("ctl_isc_start: ctl_ha_msg_create failed.\n");
14065 ret = CTL_HA_COMP_STATUS_ERROR;
14067 } else if (CTL_HA_STATE_IS_HA(c->state)
14068 && CTL_HA_STATE_IS_SINGLE(state)){
14069 // HA->SINGLE transition
14071 control_softc->is_single = 1;
14073 printf("ctl_isc_start:Invalid state transition %X->%X\n",
14075 ret = CTL_HA_COMP_STATUS_ERROR;
14077 if (CTL_HA_STATE_IS_SINGLE(state))
14078 control_softc->is_single = 1;
14086 * Quiesce component
14087 * The component must clear any error conditions (set status to OK) and
14088 * prepare itself to another Start call
14089 * returns ctl_ha_comp_status:
14093 static ctl_ha_comp_status
14094 ctl_isc_quiesce(struct ctl_ha_component *c)
14096 int ret = CTL_HA_COMP_STATUS_OK;
14103 struct ctl_ha_component ctl_ha_component_ctlisc =
14106 .state = CTL_HA_STATE_UNKNOWN,
14107 .init = ctl_isc_init,
14108 .start = ctl_isc_start,
14109 .quiesce = ctl_isc_quiesce