2 * Copyright (c) 2003-2009 Silicon Graphics International Corp.
3 * Copyright (c) 2012 The FreeBSD Foundation
4 * Copyright (c) 2014-2017 Alexander Motin <mav@FreeBSD.org>
7 * Portions of this software were developed by Edward Tomasz Napierala
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions, and the following disclaimer,
15 * without modification.
16 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17 * substantially similar to the "NO WARRANTY" disclaimer below
18 * ("Disclaimer") and any redistribution must be conditioned upon
19 * including a substantially similar Disclaimer requirement for further
20 * binary redistribution.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGES.
38 * CAM Target Layer, a SCSI device emulation subsystem.
40 * Author: Ken Merry <ken@FreeBSD.org>
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/ctype.h>
51 #include <sys/kernel.h>
52 #include <sys/types.h>
53 #include <sys/kthread.h>
55 #include <sys/fcntl.h>
57 #include <sys/module.h>
58 #include <sys/mutex.h>
59 #include <sys/condvar.h>
60 #include <sys/malloc.h>
62 #include <sys/ioccom.h>
63 #include <sys/queue.h>
66 #include <sys/endian.h>
67 #include <sys/sysctl.h>
71 #include <cam/scsi/scsi_all.h>
72 #include <cam/scsi/scsi_cd.h>
73 #include <cam/scsi/scsi_da.h>
74 #include <cam/ctl/ctl_io.h>
75 #include <cam/ctl/ctl.h>
76 #include <cam/ctl/ctl_frontend.h>
77 #include <cam/ctl/ctl_util.h>
78 #include <cam/ctl/ctl_backend.h>
79 #include <cam/ctl/ctl_ioctl.h>
80 #include <cam/ctl/ctl_ha.h>
81 #include <cam/ctl/ctl_private.h>
82 #include <cam/ctl/ctl_debug.h>
83 #include <cam/ctl/ctl_scsi_all.h>
84 #include <cam/ctl/ctl_error.h>
86 struct ctl_softc *control_softc = NULL;
89 * Template mode pages.
93 * Note that these are default values only. The actual values will be
94 * filled in when the user does a mode sense.
96 const static struct scsi_da_rw_recovery_page rw_er_page_default = {
97 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
98 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
99 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE,
100 /*read_retry_count*/0,
101 /*correction_span*/0,
102 /*head_offset_count*/0,
103 /*data_strobe_offset_cnt*/0,
104 /*byte8*/SMS_RWER_LBPERE,
105 /*write_retry_count*/0,
107 /*recovery_time_limit*/{0, 0},
110 const static struct scsi_da_rw_recovery_page rw_er_page_changeable = {
111 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
112 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
113 /*byte3*/SMS_RWER_PER,
114 /*read_retry_count*/0,
115 /*correction_span*/0,
116 /*head_offset_count*/0,
117 /*data_strobe_offset_cnt*/0,
118 /*byte8*/SMS_RWER_LBPERE,
119 /*write_retry_count*/0,
121 /*recovery_time_limit*/{0, 0},
124 const static struct scsi_format_page format_page_default = {
125 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
126 /*page_length*/sizeof(struct scsi_format_page) - 2,
127 /*tracks_per_zone*/ {0, 0},
128 /*alt_sectors_per_zone*/ {0, 0},
129 /*alt_tracks_per_zone*/ {0, 0},
130 /*alt_tracks_per_lun*/ {0, 0},
131 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
132 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
133 /*bytes_per_sector*/ {0, 0},
134 /*interleave*/ {0, 0},
135 /*track_skew*/ {0, 0},
136 /*cylinder_skew*/ {0, 0},
138 /*reserved*/ {0, 0, 0}
141 const static struct scsi_format_page format_page_changeable = {
142 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
143 /*page_length*/sizeof(struct scsi_format_page) - 2,
144 /*tracks_per_zone*/ {0, 0},
145 /*alt_sectors_per_zone*/ {0, 0},
146 /*alt_tracks_per_zone*/ {0, 0},
147 /*alt_tracks_per_lun*/ {0, 0},
148 /*sectors_per_track*/ {0, 0},
149 /*bytes_per_sector*/ {0, 0},
150 /*interleave*/ {0, 0},
151 /*track_skew*/ {0, 0},
152 /*cylinder_skew*/ {0, 0},
154 /*reserved*/ {0, 0, 0}
157 const static struct scsi_rigid_disk_page rigid_disk_page_default = {
158 /*page_code*/SMS_RIGID_DISK_PAGE,
159 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
160 /*cylinders*/ {0, 0, 0},
161 /*heads*/ CTL_DEFAULT_HEADS,
162 /*start_write_precomp*/ {0, 0, 0},
163 /*start_reduced_current*/ {0, 0, 0},
164 /*step_rate*/ {0, 0},
165 /*landing_zone_cylinder*/ {0, 0, 0},
166 /*rpl*/ SRDP_RPL_DISABLED,
167 /*rotational_offset*/ 0,
169 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
170 CTL_DEFAULT_ROTATION_RATE & 0xff},
174 const static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
175 /*page_code*/SMS_RIGID_DISK_PAGE,
176 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
177 /*cylinders*/ {0, 0, 0},
179 /*start_write_precomp*/ {0, 0, 0},
180 /*start_reduced_current*/ {0, 0, 0},
181 /*step_rate*/ {0, 0},
182 /*landing_zone_cylinder*/ {0, 0, 0},
184 /*rotational_offset*/ 0,
186 /*rotation_rate*/ {0, 0},
190 const static struct scsi_da_verify_recovery_page verify_er_page_default = {
191 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE,
192 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2,
194 /*read_retry_count*/0,
195 /*reserved*/{ 0, 0, 0, 0, 0, 0 },
196 /*recovery_time_limit*/{0, 0},
199 const static struct scsi_da_verify_recovery_page verify_er_page_changeable = {
200 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE,
201 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2,
202 /*byte3*/SMS_VER_PER,
203 /*read_retry_count*/0,
204 /*reserved*/{ 0, 0, 0, 0, 0, 0 },
205 /*recovery_time_limit*/{0, 0},
208 const static struct scsi_caching_page caching_page_default = {
209 /*page_code*/SMS_CACHING_PAGE,
210 /*page_length*/sizeof(struct scsi_caching_page) - 2,
211 /*flags1*/ SCP_DISC | SCP_WCE,
213 /*disable_pf_transfer_len*/ {0xff, 0xff},
214 /*min_prefetch*/ {0, 0},
215 /*max_prefetch*/ {0xff, 0xff},
216 /*max_pf_ceiling*/ {0xff, 0xff},
218 /*cache_segments*/ 0,
219 /*cache_seg_size*/ {0, 0},
221 /*non_cache_seg_size*/ {0, 0, 0}
224 const static struct scsi_caching_page caching_page_changeable = {
225 /*page_code*/SMS_CACHING_PAGE,
226 /*page_length*/sizeof(struct scsi_caching_page) - 2,
227 /*flags1*/ SCP_WCE | SCP_RCD,
229 /*disable_pf_transfer_len*/ {0, 0},
230 /*min_prefetch*/ {0, 0},
231 /*max_prefetch*/ {0, 0},
232 /*max_pf_ceiling*/ {0, 0},
234 /*cache_segments*/ 0,
235 /*cache_seg_size*/ {0, 0},
237 /*non_cache_seg_size*/ {0, 0, 0}
240 const static struct scsi_control_page control_page_default = {
241 /*page_code*/SMS_CONTROL_MODE_PAGE,
242 /*page_length*/sizeof(struct scsi_control_page) - 2,
244 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED,
247 /*aen_holdoff_period*/{0, 0},
248 /*busy_timeout_period*/{0, 0},
249 /*extended_selftest_completion_time*/{0, 0}
252 const static struct scsi_control_page control_page_changeable = {
253 /*page_code*/SMS_CONTROL_MODE_PAGE,
254 /*page_length*/sizeof(struct scsi_control_page) - 2,
256 /*queue_flags*/SCP_QUEUE_ALG_MASK | SCP_NUAR,
257 /*eca_and_aen*/SCP_SWP,
259 /*aen_holdoff_period*/{0, 0},
260 /*busy_timeout_period*/{0, 0},
261 /*extended_selftest_completion_time*/{0, 0}
264 #define CTL_CEM_LEN (sizeof(struct scsi_control_ext_page) - 4)
266 const static struct scsi_control_ext_page control_ext_page_default = {
267 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
268 /*subpage_code*/0x01,
269 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
275 const static struct scsi_control_ext_page control_ext_page_changeable = {
276 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
277 /*subpage_code*/0x01,
278 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
284 const static struct scsi_info_exceptions_page ie_page_default = {
285 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
286 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
287 /*info_flags*/SIEP_FLAGS_EWASC,
288 /*mrie*/SIEP_MRIE_NO,
289 /*interval_timer*/{0, 0, 0, 0},
290 /*report_count*/{0, 0, 0, 1}
293 const static struct scsi_info_exceptions_page ie_page_changeable = {
294 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
295 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
296 /*info_flags*/SIEP_FLAGS_EWASC | SIEP_FLAGS_DEXCPT | SIEP_FLAGS_TEST |
299 /*interval_timer*/{0xff, 0xff, 0xff, 0xff},
300 /*report_count*/{0xff, 0xff, 0xff, 0xff}
303 #define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4)
305 const static struct ctl_logical_block_provisioning_page lbp_page_default = {{
306 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
307 /*subpage_code*/0x02,
308 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
310 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
315 /*count*/{0, 0, 0, 0}},
319 /*count*/{0, 0, 0, 0}},
323 /*count*/{0, 0, 0, 0}},
327 /*count*/{0, 0, 0, 0}}
331 const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{
332 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
333 /*subpage_code*/0x02,
334 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
335 /*flags*/SLBPP_SITUA,
336 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
341 /*count*/{0, 0, 0, 0}},
345 /*count*/{0, 0, 0, 0}},
349 /*count*/{0, 0, 0, 0}},
353 /*count*/{0, 0, 0, 0}}
357 const static struct scsi_cddvd_capabilities_page cddvd_page_default = {
358 /*page_code*/SMS_CDDVD_CAPS_PAGE,
359 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2,
367 /*nvol_levels*/{0, 0},
368 /*buffer_size*/{8, 0},
373 /*copy_management*/0,
375 /*rotation_control*/0,
376 /*cur_write_speed*/0,
377 /*num_speed_descr*/0,
380 const static struct scsi_cddvd_capabilities_page cddvd_page_changeable = {
381 /*page_code*/SMS_CDDVD_CAPS_PAGE,
382 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2,
390 /*nvol_levels*/{0, 0},
391 /*buffer_size*/{0, 0},
396 /*copy_management*/0,
398 /*rotation_control*/0,
399 /*cur_write_speed*/0,
400 /*num_speed_descr*/0,
403 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
404 static int worker_threads = -1;
405 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
406 &worker_threads, 1, "Number of worker threads");
407 static int ctl_debug = CTL_DEBUG_NONE;
408 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN,
409 &ctl_debug, 0, "Enabled debug flags");
410 static int ctl_lun_map_size = 1024;
411 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, lun_map_size, CTLFLAG_RWTUN,
412 &ctl_lun_map_size, 0, "Size of per-port LUN map (max LUN + 1)");
415 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
416 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
417 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0),
418 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
420 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 10
422 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
424 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
425 static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest);
426 static int ctl_init(void);
427 static int ctl_shutdown(void);
428 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
429 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
430 static void ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
431 static void ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
432 struct ctl_ooa *ooa_hdr,
433 struct ctl_ooa_entry *kern_entries);
434 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
436 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
437 struct ctl_be_lun *be_lun);
438 static int ctl_free_lun(struct ctl_lun *lun);
439 static void ctl_create_lun(struct ctl_be_lun *be_lun);
441 static int ctl_do_mode_select(union ctl_io *io);
442 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
443 uint64_t res_key, uint64_t sa_res_key,
444 uint8_t type, uint32_t residx,
445 struct ctl_scsiio *ctsio,
446 struct scsi_per_res_out *cdb,
447 struct scsi_per_res_out_parms* param);
448 static void ctl_pro_preempt_other(struct ctl_lun *lun,
449 union ctl_ha_msg *msg);
450 static void ctl_hndl_per_res_out_on_other_sc(union ctl_io *io);
451 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
452 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
453 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
454 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
455 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
456 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
458 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
460 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
461 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
462 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
463 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
464 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
465 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2,
467 static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2);
468 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
469 union ctl_io *pending_io, union ctl_io *ooa_io);
470 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
471 union ctl_io *starting_io);
472 static int ctl_check_blocked(struct ctl_lun *lun);
473 static int ctl_scsiio_lun_check(struct ctl_lun *lun,
474 const struct ctl_cmd_entry *entry,
475 struct ctl_scsiio *ctsio);
476 static void ctl_failover_lun(union ctl_io *io);
477 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
478 struct ctl_scsiio *ctsio);
479 static int ctl_scsiio(struct ctl_scsiio *ctsio);
481 static int ctl_target_reset(union ctl_io *io);
482 static void ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx,
483 ctl_ua_type ua_type);
484 static int ctl_lun_reset(union ctl_io *io);
485 static int ctl_abort_task(union ctl_io *io);
486 static int ctl_abort_task_set(union ctl_io *io);
487 static int ctl_query_task(union ctl_io *io, int task_set);
488 static void ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx,
489 ctl_ua_type ua_type);
490 static int ctl_i_t_nexus_reset(union ctl_io *io);
491 static int ctl_query_async_event(union ctl_io *io);
492 static void ctl_run_task(union ctl_io *io);
494 static void ctl_datamove_timer_wakeup(void *arg);
495 static void ctl_done_timer_wakeup(void *arg);
496 #endif /* CTL_IO_DELAY */
498 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
499 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
500 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
501 static void ctl_datamove_remote_write(union ctl_io *io);
502 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
503 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
504 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
505 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
506 ctl_ha_dt_cb callback);
507 static void ctl_datamove_remote_read(union ctl_io *io);
508 static void ctl_datamove_remote(union ctl_io *io);
509 static void ctl_process_done(union ctl_io *io);
510 static void ctl_lun_thread(void *arg);
511 static void ctl_thresh_thread(void *arg);
512 static void ctl_work_thread(void *arg);
513 static void ctl_enqueue_incoming(union ctl_io *io);
514 static void ctl_enqueue_rtr(union ctl_io *io);
515 static void ctl_enqueue_done(union ctl_io *io);
516 static void ctl_enqueue_isc(union ctl_io *io);
517 static const struct ctl_cmd_entry *
518 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
519 static const struct ctl_cmd_entry *
520 ctl_validate_command(struct ctl_scsiio *ctsio);
521 static int ctl_cmd_applicable(uint8_t lun_type,
522 const struct ctl_cmd_entry *entry);
523 static int ctl_ha_init(void);
524 static int ctl_ha_shutdown(void);
526 static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx);
527 static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx);
528 static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx);
529 static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key);
532 * Load the serialization table. This isn't very pretty, but is probably
533 * the easiest way to do it.
535 #include "ctl_ser_table.c"
538 * We only need to define open, close and ioctl routines for this driver.
540 static struct cdevsw ctl_cdevsw = {
541 .d_version = D_VERSION,
544 .d_close = ctl_close,
545 .d_ioctl = ctl_ioctl,
550 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
552 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
554 static moduledata_t ctl_moduledata = {
556 ctl_module_event_handler,
560 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
561 MODULE_VERSION(ctl, 1);
563 static struct ctl_frontend ha_frontend =
567 .shutdown = ctl_ha_shutdown,
573 struct ctl_softc *softc = control_softc;
575 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
576 &softc->othersc_pool) != 0)
578 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) {
579 ctl_pool_free(softc->othersc_pool);
582 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
583 != CTL_HA_STATUS_SUCCESS) {
584 ctl_ha_msg_destroy(softc);
585 ctl_pool_free(softc->othersc_pool);
592 ctl_ha_shutdown(void)
594 struct ctl_softc *softc = control_softc;
595 struct ctl_port *port;
597 ctl_ha_msg_shutdown(softc);
598 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL) != CTL_HA_STATUS_SUCCESS)
600 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS)
602 ctl_pool_free(softc->othersc_pool);
603 while ((port = STAILQ_FIRST(&ha_frontend.port_list)) != NULL) {
604 ctl_port_deregister(port);
605 free(port->port_name, M_CTL);
612 ctl_ha_datamove(union ctl_io *io)
614 struct ctl_lun *lun = CTL_LUN(io);
615 struct ctl_sg_entry *sgl;
616 union ctl_ha_msg msg;
617 uint32_t sg_entries_sent;
618 int do_sg_copy, i, j;
620 memset(&msg.dt, 0, sizeof(msg.dt));
621 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
622 msg.hdr.original_sc = io->io_hdr.original_sc;
623 msg.hdr.serializing_sc = io;
624 msg.hdr.nexus = io->io_hdr.nexus;
625 msg.hdr.status = io->io_hdr.status;
626 msg.dt.flags = io->io_hdr.flags;
629 * We convert everything into a S/G list here. We can't
630 * pass by reference, only by value between controllers.
631 * So we can't pass a pointer to the S/G list, only as many
632 * S/G entries as we can fit in here. If it's possible for
633 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
634 * then we need to break this up into multiple transfers.
636 if (io->scsiio.kern_sg_entries == 0) {
637 msg.dt.kern_sg_entries = 1;
639 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
640 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
642 /* XXX KDM use busdma here! */
643 msg.dt.sg_list[0].addr =
644 (void *)vtophys(io->scsiio.kern_data_ptr);
647 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
648 ("HA does not support BUS_ADDR"));
649 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
651 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
654 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
658 msg.dt.kern_data_len = io->scsiio.kern_data_len;
659 msg.dt.kern_total_len = io->scsiio.kern_total_len;
660 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
661 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
662 msg.dt.sg_sequence = 0;
665 * Loop until we've sent all of the S/G entries. On the
666 * other end, we'll recompose these S/G entries into one
667 * contiguous list before processing.
669 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries;
670 msg.dt.sg_sequence++) {
671 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) /
672 sizeof(msg.dt.sg_list[0])),
673 msg.dt.kern_sg_entries - sg_entries_sent);
674 if (do_sg_copy != 0) {
675 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
676 for (i = sg_entries_sent, j = 0;
677 i < msg.dt.cur_sg_entries; i++, j++) {
679 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
680 msg.dt.sg_list[j].addr = sgl[i].addr;
682 /* XXX KDM use busdma here! */
683 msg.dt.sg_list[j].addr =
684 (void *)vtophys(sgl[i].addr);
687 KASSERT((io->io_hdr.flags &
688 CTL_FLAG_BUS_ADDR) == 0,
689 ("HA does not support BUS_ADDR"));
690 msg.dt.sg_list[j].addr = sgl[i].addr;
692 msg.dt.sg_list[j].len = sgl[i].len;
696 sg_entries_sent += msg.dt.cur_sg_entries;
697 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries);
698 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
699 sizeof(msg.dt) - sizeof(msg.dt.sg_list) +
700 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries,
701 M_WAITOK) > CTL_HA_STATUS_SUCCESS) {
702 io->io_hdr.port_status = 31341;
703 io->scsiio.be_move_done(io);
706 msg.dt.sent_sg_entries = sg_entries_sent;
710 * Officially handover the request from us to peer.
711 * If failover has just happened, then we must return error.
712 * If failover happen just after, then it is not our problem.
715 mtx_lock(&lun->lun_lock);
716 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
718 mtx_unlock(&lun->lun_lock);
719 io->io_hdr.port_status = 31342;
720 io->scsiio.be_move_done(io);
723 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
724 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
726 mtx_unlock(&lun->lun_lock);
730 ctl_ha_done(union ctl_io *io)
732 union ctl_ha_msg msg;
734 if (io->io_hdr.io_type == CTL_IO_SCSI) {
735 memset(&msg, 0, sizeof(msg));
736 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
737 msg.hdr.original_sc = io->io_hdr.original_sc;
738 msg.hdr.nexus = io->io_hdr.nexus;
739 msg.hdr.status = io->io_hdr.status;
740 msg.scsi.scsi_status = io->scsiio.scsi_status;
741 msg.scsi.tag_num = io->scsiio.tag_num;
742 msg.scsi.tag_type = io->scsiio.tag_type;
743 msg.scsi.sense_len = io->scsiio.sense_len;
744 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
745 io->scsiio.sense_len);
746 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
747 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
748 msg.scsi.sense_len, M_WAITOK);
754 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
755 union ctl_ha_msg *msg_info)
757 struct ctl_scsiio *ctsio;
759 if (msg_info->hdr.original_sc == NULL) {
760 printf("%s: original_sc == NULL!\n", __func__);
761 /* XXX KDM now what? */
765 ctsio = &msg_info->hdr.original_sc->scsiio;
766 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
767 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
768 ctsio->io_hdr.status = msg_info->hdr.status;
769 ctsio->scsi_status = msg_info->scsi.scsi_status;
770 ctsio->sense_len = msg_info->scsi.sense_len;
771 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
772 msg_info->scsi.sense_len);
773 ctl_enqueue_isc((union ctl_io *)ctsio);
777 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
778 union ctl_ha_msg *msg_info)
780 struct ctl_scsiio *ctsio;
782 if (msg_info->hdr.serializing_sc == NULL) {
783 printf("%s: serializing_sc == NULL!\n", __func__);
784 /* XXX KDM now what? */
788 ctsio = &msg_info->hdr.serializing_sc->scsiio;
789 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
790 ctl_enqueue_isc((union ctl_io *)ctsio);
794 ctl_isc_announce_lun(struct ctl_lun *lun)
796 struct ctl_softc *softc = lun->ctl_softc;
797 union ctl_ha_msg *msg;
798 struct ctl_ha_msg_lun_pr_key pr_key;
801 if (softc->ha_link != CTL_HA_LINK_ONLINE)
803 mtx_lock(&lun->lun_lock);
804 i = sizeof(msg->lun);
806 i += lun->lun_devid->len;
807 i += sizeof(pr_key) * lun->pr_key_count;
809 mtx_unlock(&lun->lun_lock);
810 msg = malloc(i, M_CTL, M_WAITOK);
811 mtx_lock(&lun->lun_lock);
812 k = sizeof(msg->lun);
814 k += lun->lun_devid->len;
815 k += sizeof(pr_key) * lun->pr_key_count;
821 bzero(&msg->lun, sizeof(msg->lun));
822 msg->hdr.msg_type = CTL_MSG_LUN_SYNC;
823 msg->hdr.nexus.targ_lun = lun->lun;
824 msg->hdr.nexus.targ_mapped_lun = lun->lun;
825 msg->lun.flags = lun->flags;
826 msg->lun.pr_generation = lun->pr_generation;
827 msg->lun.pr_res_idx = lun->pr_res_idx;
828 msg->lun.pr_res_type = lun->pr_res_type;
829 msg->lun.pr_key_count = lun->pr_key_count;
831 if (lun->lun_devid) {
832 msg->lun.lun_devid_len = lun->lun_devid->len;
833 memcpy(&msg->lun.data[i], lun->lun_devid->data,
834 msg->lun.lun_devid_len);
835 i += msg->lun.lun_devid_len;
837 for (k = 0; k < CTL_MAX_INITIATORS; k++) {
838 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0)
841 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key));
844 mtx_unlock(&lun->lun_lock);
845 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
849 if (lun->flags & CTL_LUN_PRIMARY_SC) {
850 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
851 ctl_isc_announce_mode(lun, -1,
852 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
853 lun->mode_pages.index[i].subpage);
859 ctl_isc_announce_port(struct ctl_port *port)
861 struct ctl_softc *softc = port->ctl_softc;
862 union ctl_ha_msg *msg;
865 if (port->targ_port < softc->port_min ||
866 port->targ_port >= softc->port_max ||
867 softc->ha_link != CTL_HA_LINK_ONLINE)
869 i = sizeof(msg->port) + strlen(port->port_name) + 1;
871 i += port->lun_map_size * sizeof(uint32_t);
872 if (port->port_devid)
873 i += port->port_devid->len;
874 if (port->target_devid)
875 i += port->target_devid->len;
876 if (port->init_devid)
877 i += port->init_devid->len;
878 msg = malloc(i, M_CTL, M_WAITOK);
879 bzero(&msg->port, sizeof(msg->port));
880 msg->hdr.msg_type = CTL_MSG_PORT_SYNC;
881 msg->hdr.nexus.targ_port = port->targ_port;
882 msg->port.port_type = port->port_type;
883 msg->port.physical_port = port->physical_port;
884 msg->port.virtual_port = port->virtual_port;
885 msg->port.status = port->status;
887 msg->port.name_len = sprintf(&msg->port.data[i],
888 "%d:%s", softc->ha_id, port->port_name) + 1;
889 i += msg->port.name_len;
891 msg->port.lun_map_len = port->lun_map_size * sizeof(uint32_t);
892 memcpy(&msg->port.data[i], port->lun_map,
893 msg->port.lun_map_len);
894 i += msg->port.lun_map_len;
896 if (port->port_devid) {
897 msg->port.port_devid_len = port->port_devid->len;
898 memcpy(&msg->port.data[i], port->port_devid->data,
899 msg->port.port_devid_len);
900 i += msg->port.port_devid_len;
902 if (port->target_devid) {
903 msg->port.target_devid_len = port->target_devid->len;
904 memcpy(&msg->port.data[i], port->target_devid->data,
905 msg->port.target_devid_len);
906 i += msg->port.target_devid_len;
908 if (port->init_devid) {
909 msg->port.init_devid_len = port->init_devid->len;
910 memcpy(&msg->port.data[i], port->init_devid->data,
911 msg->port.init_devid_len);
912 i += msg->port.init_devid_len;
914 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
920 ctl_isc_announce_iid(struct ctl_port *port, int iid)
922 struct ctl_softc *softc = port->ctl_softc;
923 union ctl_ha_msg *msg;
926 if (port->targ_port < softc->port_min ||
927 port->targ_port >= softc->port_max ||
928 softc->ha_link != CTL_HA_LINK_ONLINE)
930 mtx_lock(&softc->ctl_lock);
931 i = sizeof(msg->iid);
933 if (port->wwpn_iid[iid].name)
934 l = strlen(port->wwpn_iid[iid].name) + 1;
936 msg = malloc(i, M_CTL, M_NOWAIT);
938 mtx_unlock(&softc->ctl_lock);
941 bzero(&msg->iid, sizeof(msg->iid));
942 msg->hdr.msg_type = CTL_MSG_IID_SYNC;
943 msg->hdr.nexus.targ_port = port->targ_port;
944 msg->hdr.nexus.initid = iid;
945 msg->iid.in_use = port->wwpn_iid[iid].in_use;
946 msg->iid.name_len = l;
947 msg->iid.wwpn = port->wwpn_iid[iid].wwpn;
948 if (port->wwpn_iid[iid].name)
949 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l);
950 mtx_unlock(&softc->ctl_lock);
951 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT);
956 ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx,
957 uint8_t page, uint8_t subpage)
959 struct ctl_softc *softc = lun->ctl_softc;
960 union ctl_ha_msg msg;
963 if (softc->ha_link != CTL_HA_LINK_ONLINE)
965 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
966 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
967 page && lun->mode_pages.index[i].subpage == subpage)
970 if (i == CTL_NUM_MODE_PAGES)
973 /* Don't try to replicate pages not present on this device. */
974 if (lun->mode_pages.index[i].page_data == NULL)
977 bzero(&msg.mode, sizeof(msg.mode));
978 msg.hdr.msg_type = CTL_MSG_MODE_SYNC;
979 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT;
980 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT;
981 msg.hdr.nexus.targ_lun = lun->lun;
982 msg.hdr.nexus.targ_mapped_lun = lun->lun;
983 msg.mode.page_code = page;
984 msg.mode.subpage = subpage;
985 msg.mode.page_len = lun->mode_pages.index[i].page_len;
986 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data,
988 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode),
993 ctl_isc_ha_link_up(struct ctl_softc *softc)
995 struct ctl_port *port;
997 union ctl_ha_msg msg;
1000 /* Announce this node parameters to peer for validation. */
1001 msg.login.msg_type = CTL_MSG_LOGIN;
1002 msg.login.version = CTL_HA_VERSION;
1003 msg.login.ha_mode = softc->ha_mode;
1004 msg.login.ha_id = softc->ha_id;
1005 msg.login.max_luns = CTL_MAX_LUNS;
1006 msg.login.max_ports = CTL_MAX_PORTS;
1007 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT;
1008 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login),
1011 STAILQ_FOREACH(port, &softc->port_list, links) {
1012 ctl_isc_announce_port(port);
1013 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1014 if (port->wwpn_iid[i].in_use)
1015 ctl_isc_announce_iid(port, i);
1018 STAILQ_FOREACH(lun, &softc->lun_list, links)
1019 ctl_isc_announce_lun(lun);
1023 ctl_isc_ha_link_down(struct ctl_softc *softc)
1025 struct ctl_port *port;
1026 struct ctl_lun *lun;
1030 mtx_lock(&softc->ctl_lock);
1031 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1032 mtx_lock(&lun->lun_lock);
1033 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) {
1034 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1035 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1037 mtx_unlock(&lun->lun_lock);
1039 mtx_unlock(&softc->ctl_lock);
1040 io = ctl_alloc_io(softc->othersc_pool);
1041 mtx_lock(&softc->ctl_lock);
1043 io->io_hdr.msg_type = CTL_MSG_FAILOVER;
1044 io->io_hdr.nexus.targ_mapped_lun = lun->lun;
1045 ctl_enqueue_isc(io);
1048 STAILQ_FOREACH(port, &softc->port_list, links) {
1049 if (port->targ_port >= softc->port_min &&
1050 port->targ_port < softc->port_max)
1052 port->status &= ~CTL_PORT_STATUS_ONLINE;
1053 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1054 port->wwpn_iid[i].in_use = 0;
1055 free(port->wwpn_iid[i].name, M_CTL);
1056 port->wwpn_iid[i].name = NULL;
1059 mtx_unlock(&softc->ctl_lock);
1063 ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1065 struct ctl_lun *lun;
1066 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus);
1068 mtx_lock(&softc->ctl_lock);
1069 if (msg->hdr.nexus.targ_mapped_lun >= CTL_MAX_LUNS ||
1070 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) == NULL) {
1071 mtx_unlock(&softc->ctl_lock);
1074 mtx_lock(&lun->lun_lock);
1075 mtx_unlock(&softc->ctl_lock);
1076 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && msg->ua.ua_set)
1077 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8);
1078 if (msg->ua.ua_all) {
1080 ctl_est_ua_all(lun, iid, msg->ua.ua_type);
1082 ctl_clr_ua_all(lun, iid, msg->ua.ua_type);
1085 ctl_est_ua(lun, iid, msg->ua.ua_type);
1087 ctl_clr_ua(lun, iid, msg->ua.ua_type);
1089 mtx_unlock(&lun->lun_lock);
1093 ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1095 struct ctl_lun *lun;
1096 struct ctl_ha_msg_lun_pr_key pr_key;
1098 ctl_lun_flags oflags;
1101 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1102 mtx_lock(&softc->ctl_lock);
1103 if (targ_lun >= CTL_MAX_LUNS ||
1104 (lun = softc->ctl_luns[targ_lun]) == NULL) {
1105 mtx_unlock(&softc->ctl_lock);
1108 mtx_lock(&lun->lun_lock);
1109 mtx_unlock(&softc->ctl_lock);
1110 if (lun->flags & CTL_LUN_DISABLED) {
1111 mtx_unlock(&lun->lun_lock);
1114 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0;
1115 if (msg->lun.lun_devid_len != i || (i > 0 &&
1116 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) {
1117 mtx_unlock(&lun->lun_lock);
1118 printf("%s: Received conflicting HA LUN %d\n",
1119 __func__, targ_lun);
1122 /* Record whether peer is primary. */
1123 oflags = lun->flags;
1124 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) &&
1125 (msg->lun.flags & CTL_LUN_DISABLED) == 0)
1126 lun->flags |= CTL_LUN_PEER_SC_PRIMARY;
1128 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1129 if (oflags != lun->flags)
1130 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1132 /* If peer is primary and we are not -- use data */
1133 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
1134 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) {
1135 lun->pr_generation = msg->lun.pr_generation;
1136 lun->pr_res_idx = msg->lun.pr_res_idx;
1137 lun->pr_res_type = msg->lun.pr_res_type;
1138 lun->pr_key_count = msg->lun.pr_key_count;
1139 for (k = 0; k < CTL_MAX_INITIATORS; k++)
1140 ctl_clr_prkey(lun, k);
1141 for (k = 0; k < msg->lun.pr_key_count; k++) {
1142 memcpy(&pr_key, &msg->lun.data[i],
1144 ctl_alloc_prkey(lun, pr_key.pr_iid);
1145 ctl_set_prkey(lun, pr_key.pr_iid,
1147 i += sizeof(pr_key);
1151 mtx_unlock(&lun->lun_lock);
1152 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n",
1154 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ?
1155 "primary" : "secondary"));
1157 /* If we are primary but peer doesn't know -- notify */
1158 if ((lun->flags & CTL_LUN_PRIMARY_SC) &&
1159 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0)
1160 ctl_isc_announce_lun(lun);
1165 ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1167 struct ctl_port *port;
1168 struct ctl_lun *lun;
1171 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1173 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__,
1174 msg->hdr.nexus.targ_port));
1176 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO);
1177 port->frontend = &ha_frontend;
1178 port->targ_port = msg->hdr.nexus.targ_port;
1179 port->fe_datamove = ctl_ha_datamove;
1180 port->fe_done = ctl_ha_done;
1181 } else if (port->frontend == &ha_frontend) {
1182 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__,
1183 msg->hdr.nexus.targ_port));
1186 printf("%s: Received conflicting HA port %d\n",
1187 __func__, msg->hdr.nexus.targ_port);
1190 port->port_type = msg->port.port_type;
1191 port->physical_port = msg->port.physical_port;
1192 port->virtual_port = msg->port.virtual_port;
1193 port->status = msg->port.status;
1195 free(port->port_name, M_CTL);
1196 port->port_name = strndup(&msg->port.data[i], msg->port.name_len,
1198 i += msg->port.name_len;
1199 if (msg->port.lun_map_len != 0) {
1200 if (port->lun_map == NULL ||
1201 port->lun_map_size * sizeof(uint32_t) <
1202 msg->port.lun_map_len) {
1203 port->lun_map_size = 0;
1204 free(port->lun_map, M_CTL);
1205 port->lun_map = malloc(msg->port.lun_map_len,
1208 memcpy(port->lun_map, &msg->port.data[i], msg->port.lun_map_len);
1209 port->lun_map_size = msg->port.lun_map_len / sizeof(uint32_t);
1210 i += msg->port.lun_map_len;
1212 port->lun_map_size = 0;
1213 free(port->lun_map, M_CTL);
1214 port->lun_map = NULL;
1216 if (msg->port.port_devid_len != 0) {
1217 if (port->port_devid == NULL ||
1218 port->port_devid->len < msg->port.port_devid_len) {
1219 free(port->port_devid, M_CTL);
1220 port->port_devid = malloc(sizeof(struct ctl_devid) +
1221 msg->port.port_devid_len, M_CTL, M_WAITOK);
1223 memcpy(port->port_devid->data, &msg->port.data[i],
1224 msg->port.port_devid_len);
1225 port->port_devid->len = msg->port.port_devid_len;
1226 i += msg->port.port_devid_len;
1228 free(port->port_devid, M_CTL);
1229 port->port_devid = NULL;
1231 if (msg->port.target_devid_len != 0) {
1232 if (port->target_devid == NULL ||
1233 port->target_devid->len < msg->port.target_devid_len) {
1234 free(port->target_devid, M_CTL);
1235 port->target_devid = malloc(sizeof(struct ctl_devid) +
1236 msg->port.target_devid_len, M_CTL, M_WAITOK);
1238 memcpy(port->target_devid->data, &msg->port.data[i],
1239 msg->port.target_devid_len);
1240 port->target_devid->len = msg->port.target_devid_len;
1241 i += msg->port.target_devid_len;
1243 free(port->target_devid, M_CTL);
1244 port->target_devid = NULL;
1246 if (msg->port.init_devid_len != 0) {
1247 if (port->init_devid == NULL ||
1248 port->init_devid->len < msg->port.init_devid_len) {
1249 free(port->init_devid, M_CTL);
1250 port->init_devid = malloc(sizeof(struct ctl_devid) +
1251 msg->port.init_devid_len, M_CTL, M_WAITOK);
1253 memcpy(port->init_devid->data, &msg->port.data[i],
1254 msg->port.init_devid_len);
1255 port->init_devid->len = msg->port.init_devid_len;
1256 i += msg->port.init_devid_len;
1258 free(port->init_devid, M_CTL);
1259 port->init_devid = NULL;
1262 if (ctl_port_register(port) != 0) {
1263 printf("%s: ctl_port_register() failed with error\n",
1267 mtx_lock(&softc->ctl_lock);
1268 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1269 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
1271 mtx_lock(&lun->lun_lock);
1272 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
1273 mtx_unlock(&lun->lun_lock);
1275 mtx_unlock(&softc->ctl_lock);
1279 ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1281 struct ctl_port *port;
1284 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1286 printf("%s: Received IID for unknown port %d\n",
1287 __func__, msg->hdr.nexus.targ_port);
1290 iid = msg->hdr.nexus.initid;
1291 if (port->wwpn_iid[iid].in_use != 0 &&
1292 msg->iid.in_use == 0)
1293 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON);
1294 port->wwpn_iid[iid].in_use = msg->iid.in_use;
1295 port->wwpn_iid[iid].wwpn = msg->iid.wwpn;
1296 free(port->wwpn_iid[iid].name, M_CTL);
1297 if (msg->iid.name_len) {
1298 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0],
1299 msg->iid.name_len, M_CTL);
1301 port->wwpn_iid[iid].name = NULL;
1305 ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1308 if (msg->login.version != CTL_HA_VERSION) {
1309 printf("CTL HA peers have different versions %d != %d\n",
1310 msg->login.version, CTL_HA_VERSION);
1311 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1314 if (msg->login.ha_mode != softc->ha_mode) {
1315 printf("CTL HA peers have different ha_mode %d != %d\n",
1316 msg->login.ha_mode, softc->ha_mode);
1317 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1320 if (msg->login.ha_id == softc->ha_id) {
1321 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id);
1322 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1325 if (msg->login.max_luns != CTL_MAX_LUNS ||
1326 msg->login.max_ports != CTL_MAX_PORTS ||
1327 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) {
1328 printf("CTL HA peers have different limits\n");
1329 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1335 ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1337 struct ctl_lun *lun;
1339 uint32_t initidx, targ_lun;
1341 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1342 mtx_lock(&softc->ctl_lock);
1343 if (targ_lun >= CTL_MAX_LUNS ||
1344 (lun = softc->ctl_luns[targ_lun]) == NULL) {
1345 mtx_unlock(&softc->ctl_lock);
1348 mtx_lock(&lun->lun_lock);
1349 mtx_unlock(&softc->ctl_lock);
1350 if (lun->flags & CTL_LUN_DISABLED) {
1351 mtx_unlock(&lun->lun_lock);
1354 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
1355 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
1356 msg->mode.page_code &&
1357 lun->mode_pages.index[i].subpage == msg->mode.subpage)
1360 if (i == CTL_NUM_MODE_PAGES) {
1361 mtx_unlock(&lun->lun_lock);
1364 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data,
1365 lun->mode_pages.index[i].page_len);
1366 initidx = ctl_get_initindex(&msg->hdr.nexus);
1368 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
1369 mtx_unlock(&lun->lun_lock);
1373 * ISC (Inter Shelf Communication) event handler. Events from the HA
1374 * subsystem come in here.
1377 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
1379 struct ctl_softc *softc = control_softc;
1381 struct ctl_prio *presio;
1382 ctl_ha_status isc_status;
1384 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event));
1385 if (event == CTL_HA_EVT_MSG_RECV) {
1386 union ctl_ha_msg *msg, msgbuf;
1388 if (param > sizeof(msgbuf))
1389 msg = malloc(param, M_CTL, M_WAITOK);
1392 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param,
1394 if (isc_status != CTL_HA_STATUS_SUCCESS) {
1395 printf("%s: Error receiving message: %d\n",
1396 __func__, isc_status);
1402 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type));
1403 switch (msg->hdr.msg_type) {
1404 case CTL_MSG_SERIALIZE:
1405 io = ctl_alloc_io(softc->othersc_pool);
1407 // populate ctsio from msg
1408 io->io_hdr.io_type = CTL_IO_SCSI;
1409 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
1410 io->io_hdr.original_sc = msg->hdr.original_sc;
1411 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
1414 * If we're in serialization-only mode, we don't
1415 * want to go through full done processing. Thus
1418 * XXX KDM add another flag that is more specific.
1420 if (softc->ha_mode != CTL_HA_MODE_XFER)
1421 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
1422 io->io_hdr.nexus = msg->hdr.nexus;
1424 printf("port %u, iid %u, lun %u\n",
1425 io->io_hdr.nexus.targ_port,
1426 io->io_hdr.nexus.initid,
1427 io->io_hdr.nexus.targ_lun);
1429 io->scsiio.tag_num = msg->scsi.tag_num;
1430 io->scsiio.tag_type = msg->scsi.tag_type;
1432 io->io_hdr.start_time = time_uptime;
1433 getbinuptime(&io->io_hdr.start_bt);
1434 #endif /* CTL_TIME_IO */
1435 io->scsiio.cdb_len = msg->scsi.cdb_len;
1436 memcpy(io->scsiio.cdb, msg->scsi.cdb,
1438 if (softc->ha_mode == CTL_HA_MODE_XFER) {
1439 const struct ctl_cmd_entry *entry;
1441 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
1442 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
1444 entry->flags & CTL_FLAG_DATA_MASK;
1446 ctl_enqueue_isc(io);
1449 /* Performed on the Originating SC, XFER mode only */
1450 case CTL_MSG_DATAMOVE: {
1451 struct ctl_sg_entry *sgl;
1454 io = msg->hdr.original_sc;
1456 printf("%s: original_sc == NULL!\n", __func__);
1457 /* XXX KDM do something here */
1460 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
1461 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1463 * Keep track of this, we need to send it back over
1464 * when the datamove is complete.
1466 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1467 if (msg->hdr.status == CTL_SUCCESS)
1468 io->io_hdr.status = msg->hdr.status;
1470 if (msg->dt.sg_sequence == 0) {
1472 getbinuptime(&io->io_hdr.dma_start_bt);
1474 i = msg->dt.kern_sg_entries +
1475 msg->dt.kern_data_len /
1476 CTL_HA_DATAMOVE_SEGMENT + 1;
1477 sgl = malloc(sizeof(*sgl) * i, M_CTL,
1479 io->io_hdr.remote_sglist = sgl;
1480 io->io_hdr.local_sglist =
1481 &sgl[msg->dt.kern_sg_entries];
1483 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
1485 io->scsiio.kern_sg_entries =
1486 msg->dt.kern_sg_entries;
1487 io->scsiio.rem_sg_entries =
1488 msg->dt.kern_sg_entries;
1489 io->scsiio.kern_data_len =
1490 msg->dt.kern_data_len;
1491 io->scsiio.kern_total_len =
1492 msg->dt.kern_total_len;
1493 io->scsiio.kern_data_resid =
1494 msg->dt.kern_data_resid;
1495 io->scsiio.kern_rel_offset =
1496 msg->dt.kern_rel_offset;
1497 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR;
1498 io->io_hdr.flags |= msg->dt.flags &
1501 sgl = (struct ctl_sg_entry *)
1502 io->scsiio.kern_data_ptr;
1504 for (i = msg->dt.sent_sg_entries, j = 0;
1505 i < (msg->dt.sent_sg_entries +
1506 msg->dt.cur_sg_entries); i++, j++) {
1507 sgl[i].addr = msg->dt.sg_list[j].addr;
1508 sgl[i].len = msg->dt.sg_list[j].len;
1511 printf("%s: DATAMOVE: %p,%lu j=%d, i=%d\n",
1512 __func__, sgl[i].addr, sgl[i].len, j, i);
1517 * If this is the last piece of the I/O, we've got
1518 * the full S/G list. Queue processing in the thread.
1519 * Otherwise wait for the next piece.
1521 if (msg->dt.sg_last != 0)
1522 ctl_enqueue_isc(io);
1525 /* Performed on the Serializing (primary) SC, XFER mode only */
1526 case CTL_MSG_DATAMOVE_DONE: {
1527 if (msg->hdr.serializing_sc == NULL) {
1528 printf("%s: serializing_sc == NULL!\n",
1530 /* XXX KDM now what? */
1534 * We grab the sense information here in case
1535 * there was a failure, so we can return status
1536 * back to the initiator.
1538 io = msg->hdr.serializing_sc;
1539 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
1540 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1541 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1542 io->io_hdr.port_status = msg->scsi.port_status;
1543 io->scsiio.kern_data_resid = msg->scsi.kern_data_resid;
1544 if (msg->hdr.status != CTL_STATUS_NONE) {
1545 io->io_hdr.status = msg->hdr.status;
1546 io->scsiio.scsi_status = msg->scsi.scsi_status;
1547 io->scsiio.sense_len = msg->scsi.sense_len;
1548 memcpy(&io->scsiio.sense_data,
1549 &msg->scsi.sense_data,
1550 msg->scsi.sense_len);
1551 if (msg->hdr.status == CTL_SUCCESS)
1552 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1554 ctl_enqueue_isc(io);
1558 /* Preformed on Originating SC, SER_ONLY mode */
1560 io = msg->hdr.original_sc;
1562 printf("%s: original_sc == NULL!\n",
1566 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1567 io->io_hdr.msg_type = CTL_MSG_R2R;
1568 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1569 ctl_enqueue_isc(io);
1573 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
1575 * Performed on the Originating (i.e. secondary) SC in XFER
1578 case CTL_MSG_FINISH_IO:
1579 if (softc->ha_mode == CTL_HA_MODE_XFER)
1580 ctl_isc_handler_finish_xfer(softc, msg);
1582 ctl_isc_handler_finish_ser_only(softc, msg);
1585 /* Preformed on Originating SC */
1586 case CTL_MSG_BAD_JUJU:
1587 io = msg->hdr.original_sc;
1589 printf("%s: Bad JUJU!, original_sc is NULL!\n",
1593 ctl_copy_sense_data(msg, io);
1595 * IO should have already been cleaned up on other
1596 * SC so clear this flag so we won't send a message
1597 * back to finish the IO there.
1599 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
1600 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1602 /* io = msg->hdr.serializing_sc; */
1603 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
1604 ctl_enqueue_isc(io);
1607 /* Handle resets sent from the other side */
1608 case CTL_MSG_MANAGE_TASKS: {
1609 struct ctl_taskio *taskio;
1610 taskio = (struct ctl_taskio *)ctl_alloc_io(
1611 softc->othersc_pool);
1612 ctl_zero_io((union ctl_io *)taskio);
1613 taskio->io_hdr.io_type = CTL_IO_TASK;
1614 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1615 taskio->io_hdr.nexus = msg->hdr.nexus;
1616 taskio->task_action = msg->task.task_action;
1617 taskio->tag_num = msg->task.tag_num;
1618 taskio->tag_type = msg->task.tag_type;
1620 taskio->io_hdr.start_time = time_uptime;
1621 getbinuptime(&taskio->io_hdr.start_bt);
1622 #endif /* CTL_TIME_IO */
1623 ctl_run_task((union ctl_io *)taskio);
1626 /* Persistent Reserve action which needs attention */
1627 case CTL_MSG_PERS_ACTION:
1628 presio = (struct ctl_prio *)ctl_alloc_io(
1629 softc->othersc_pool);
1630 ctl_zero_io((union ctl_io *)presio);
1631 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
1632 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1633 presio->io_hdr.nexus = msg->hdr.nexus;
1634 presio->pr_msg = msg->pr;
1635 ctl_enqueue_isc((union ctl_io *)presio);
1638 ctl_isc_ua(softc, msg, param);
1640 case CTL_MSG_PORT_SYNC:
1641 ctl_isc_port_sync(softc, msg, param);
1643 case CTL_MSG_LUN_SYNC:
1644 ctl_isc_lun_sync(softc, msg, param);
1646 case CTL_MSG_IID_SYNC:
1647 ctl_isc_iid_sync(softc, msg, param);
1650 ctl_isc_login(softc, msg, param);
1652 case CTL_MSG_MODE_SYNC:
1653 ctl_isc_mode_sync(softc, msg, param);
1656 printf("Received HA message of unknown type %d\n",
1658 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1663 } else if (event == CTL_HA_EVT_LINK_CHANGE) {
1664 printf("CTL: HA link status changed from %d to %d\n",
1665 softc->ha_link, param);
1666 if (param == softc->ha_link)
1668 if (softc->ha_link == CTL_HA_LINK_ONLINE) {
1669 softc->ha_link = param;
1670 ctl_isc_ha_link_down(softc);
1672 softc->ha_link = param;
1673 if (softc->ha_link == CTL_HA_LINK_ONLINE)
1674 ctl_isc_ha_link_up(softc);
1678 printf("ctl_isc_event_handler: Unknown event %d\n", event);
1684 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
1687 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data,
1688 src->scsi.sense_len);
1689 dest->scsiio.scsi_status = src->scsi.scsi_status;
1690 dest->scsiio.sense_len = src->scsi.sense_len;
1691 dest->io_hdr.status = src->hdr.status;
1695 ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest)
1698 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data,
1699 src->scsiio.sense_len);
1700 dest->scsi.scsi_status = src->scsiio.scsi_status;
1701 dest->scsi.sense_len = src->scsiio.sense_len;
1702 dest->hdr.status = src->io_hdr.status;
1706 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1708 struct ctl_softc *softc = lun->ctl_softc;
1711 if (initidx < softc->init_min || initidx >= softc->init_max)
1713 mtx_assert(&lun->lun_lock, MA_OWNED);
1714 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1717 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua;
1721 ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua)
1725 mtx_assert(&lun->lun_lock, MA_OWNED);
1726 if (lun->pending_ua[port] == NULL)
1728 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1729 if (port * CTL_MAX_INIT_PER_PORT + i == except)
1731 lun->pending_ua[port][i] |= ua;
1736 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1738 struct ctl_softc *softc = lun->ctl_softc;
1741 mtx_assert(&lun->lun_lock, MA_OWNED);
1742 for (i = softc->port_min; i < softc->port_max; i++)
1743 ctl_est_ua_port(lun, i, except, ua);
1747 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1749 struct ctl_softc *softc = lun->ctl_softc;
1752 if (initidx < softc->init_min || initidx >= softc->init_max)
1754 mtx_assert(&lun->lun_lock, MA_OWNED);
1755 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1758 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua;
1762 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1764 struct ctl_softc *softc = lun->ctl_softc;
1767 mtx_assert(&lun->lun_lock, MA_OWNED);
1768 for (i = softc->port_min; i < softc->port_max; i++) {
1769 if (lun->pending_ua[i] == NULL)
1771 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
1772 if (i * CTL_MAX_INIT_PER_PORT + j == except)
1774 lun->pending_ua[i][j] &= ~ua;
1780 ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx,
1781 ctl_ua_type ua_type)
1783 struct ctl_lun *lun;
1785 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
1786 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) {
1787 mtx_lock(&lun->lun_lock);
1788 ctl_clr_ua(lun, initidx, ua_type);
1789 mtx_unlock(&lun->lun_lock);
1794 ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS)
1796 struct ctl_softc *softc = (struct ctl_softc *)arg1;
1797 struct ctl_lun *lun;
1798 struct ctl_lun_req ireq;
1801 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1;
1802 error = sysctl_handle_int(oidp, &value, 0, req);
1803 if ((error != 0) || (req->newptr == NULL))
1806 mtx_lock(&softc->ctl_lock);
1808 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1810 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
1811 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1812 mtx_unlock(&softc->ctl_lock);
1813 bzero(&ireq, sizeof(ireq));
1814 ireq.reqtype = CTL_LUNREQ_MODIFY;
1815 ireq.reqdata.modify.lun_id = lun->lun;
1816 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0,
1818 if (ireq.status != CTL_LUN_OK) {
1819 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n",
1820 __func__, ireq.status, ireq.error_str);
1822 mtx_lock(&softc->ctl_lock);
1824 mtx_unlock(&softc->ctl_lock);
1831 struct make_dev_args args;
1832 struct ctl_softc *softc;
1835 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1838 make_dev_args_init(&args);
1839 args.mda_devsw = &ctl_cdevsw;
1840 args.mda_uid = UID_ROOT;
1841 args.mda_gid = GID_OPERATOR;
1842 args.mda_mode = 0600;
1843 args.mda_si_drv1 = softc;
1844 error = make_dev_s(&args, &softc->dev, "cam/ctl");
1846 free(softc, M_DEVBUF);
1847 control_softc = NULL;
1851 sysctl_ctx_init(&softc->sysctl_ctx);
1852 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1853 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1854 CTLFLAG_RD, 0, "CAM Target Layer");
1856 if (softc->sysctl_tree == NULL) {
1857 printf("%s: unable to allocate sysctl tree\n", __func__);
1858 destroy_dev(softc->dev);
1859 free(softc, M_DEVBUF);
1860 control_softc = NULL;
1864 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1865 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
1866 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1869 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1870 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0,
1871 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)");
1874 * In Copan's HA scheme, the "master" and "slave" roles are
1875 * figured out through the slot the controller is in. Although it
1876 * is an active/active system, someone has to be in charge.
1878 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1879 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1880 "HA head ID (0 - no HA)");
1881 if (softc->ha_id == 0 || softc->ha_id > NUM_HA_SHELVES) {
1882 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1883 softc->is_single = 1;
1884 softc->port_cnt = CTL_MAX_PORTS;
1885 softc->port_min = 0;
1887 softc->port_cnt = CTL_MAX_PORTS / NUM_HA_SHELVES;
1888 softc->port_min = (softc->ha_id - 1) * softc->port_cnt;
1890 softc->port_max = softc->port_min + softc->port_cnt;
1891 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT;
1892 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT;
1894 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1895 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0,
1896 "HA link state (0 - offline, 1 - unknown, 2 - online)");
1898 STAILQ_INIT(&softc->lun_list);
1899 STAILQ_INIT(&softc->pending_lun_queue);
1900 STAILQ_INIT(&softc->fe_list);
1901 STAILQ_INIT(&softc->port_list);
1902 STAILQ_INIT(&softc->be_list);
1903 ctl_tpc_init(softc);
1905 if (worker_threads <= 0)
1906 worker_threads = max(1, mp_ncpus / 4);
1907 if (worker_threads > CTL_MAX_THREADS)
1908 worker_threads = CTL_MAX_THREADS;
1910 for (i = 0; i < worker_threads; i++) {
1911 struct ctl_thread *thr = &softc->threads[i];
1913 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1914 thr->ctl_softc = softc;
1915 STAILQ_INIT(&thr->incoming_queue);
1916 STAILQ_INIT(&thr->rtr_queue);
1917 STAILQ_INIT(&thr->done_queue);
1918 STAILQ_INIT(&thr->isc_queue);
1920 error = kproc_kthread_add(ctl_work_thread, thr,
1921 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1923 printf("error creating CTL work thread!\n");
1927 error = kproc_kthread_add(ctl_lun_thread, softc,
1928 &softc->ctl_proc, &softc->lun_thread, 0, 0, "ctl", "lun");
1930 printf("error creating CTL lun thread!\n");
1933 error = kproc_kthread_add(ctl_thresh_thread, softc,
1934 &softc->ctl_proc, &softc->thresh_thread, 0, 0, "ctl", "thresh");
1936 printf("error creating CTL threshold thread!\n");
1940 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1941 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN,
1942 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head");
1944 if (softc->is_single == 0) {
1945 if (ctl_frontend_register(&ha_frontend) != 0)
1946 softc->is_single = 1;
1954 struct ctl_softc *softc = control_softc;
1957 if (softc->is_single == 0)
1958 ctl_frontend_deregister(&ha_frontend);
1960 destroy_dev(softc->dev);
1962 /* Shutdown CTL threads. */
1963 softc->shutdown = 1;
1964 for (i = 0; i < worker_threads; i++) {
1965 struct ctl_thread *thr = &softc->threads[i];
1966 while (thr->thread != NULL) {
1968 if (thr->thread != NULL)
1969 pause("CTL thr shutdown", 1);
1971 mtx_destroy(&thr->queue_lock);
1973 while (softc->lun_thread != NULL) {
1974 wakeup(&softc->pending_lun_queue);
1975 if (softc->lun_thread != NULL)
1976 pause("CTL thr shutdown", 1);
1978 while (softc->thresh_thread != NULL) {
1979 wakeup(softc->thresh_thread);
1980 if (softc->thresh_thread != NULL)
1981 pause("CTL thr shutdown", 1);
1984 ctl_tpc_shutdown(softc);
1985 uma_zdestroy(softc->io_zone);
1986 mtx_destroy(&softc->ctl_lock);
1988 sysctl_ctx_free(&softc->sysctl_ctx);
1990 free(softc, M_DEVBUF);
1991 control_softc = NULL;
1996 ctl_module_event_handler(module_t mod, int what, void *arg)
2001 return (ctl_init());
2003 return (ctl_shutdown());
2005 return (EOPNOTSUPP);
2010 * XXX KDM should we do some access checks here? Bump a reference count to
2011 * prevent a CTL module from being unloaded while someone has it open?
2014 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
2020 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
2026 * Remove an initiator by port number and initiator ID.
2027 * Returns 0 for success, -1 for failure.
2030 ctl_remove_initiator(struct ctl_port *port, int iid)
2032 struct ctl_softc *softc = port->ctl_softc;
2035 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2037 if (iid > CTL_MAX_INIT_PER_PORT) {
2038 printf("%s: initiator ID %u > maximun %u!\n",
2039 __func__, iid, CTL_MAX_INIT_PER_PORT);
2043 mtx_lock(&softc->ctl_lock);
2044 last = (--port->wwpn_iid[iid].in_use == 0);
2045 port->wwpn_iid[iid].last_use = time_uptime;
2046 mtx_unlock(&softc->ctl_lock);
2048 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON);
2049 ctl_isc_announce_iid(port, iid);
2055 * Add an initiator to the initiator map.
2056 * Returns iid for success, < 0 for failure.
2059 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
2061 struct ctl_softc *softc = port->ctl_softc;
2065 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2067 if (iid >= CTL_MAX_INIT_PER_PORT) {
2068 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
2069 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
2074 mtx_lock(&softc->ctl_lock);
2076 if (iid < 0 && (wwpn != 0 || name != NULL)) {
2077 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2078 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
2082 if (name != NULL && port->wwpn_iid[i].name != NULL &&
2083 strcmp(name, port->wwpn_iid[i].name) == 0) {
2091 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2092 if (port->wwpn_iid[i].in_use == 0 &&
2093 port->wwpn_iid[i].wwpn == 0 &&
2094 port->wwpn_iid[i].name == NULL) {
2103 best_time = INT32_MAX;
2104 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2105 if (port->wwpn_iid[i].in_use == 0) {
2106 if (port->wwpn_iid[i].last_use < best_time) {
2108 best_time = port->wwpn_iid[i].last_use;
2116 mtx_unlock(&softc->ctl_lock);
2121 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
2123 * This is not an error yet.
2125 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
2127 printf("%s: port %d iid %u WWPN %#jx arrived"
2128 " again\n", __func__, port->targ_port,
2129 iid, (uintmax_t)wwpn);
2133 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
2134 strcmp(name, port->wwpn_iid[iid].name) == 0) {
2136 printf("%s: port %d iid %u name '%s' arrived"
2137 " again\n", __func__, port->targ_port,
2144 * This is an error, but what do we do about it? The
2145 * driver is telling us we have a new WWPN for this
2146 * initiator ID, so we pretty much need to use it.
2148 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
2149 " but WWPN %#jx '%s' is still at that address\n",
2150 __func__, port->targ_port, iid, wwpn, name,
2151 (uintmax_t)port->wwpn_iid[iid].wwpn,
2152 port->wwpn_iid[iid].name);
2155 free(port->wwpn_iid[iid].name, M_CTL);
2156 port->wwpn_iid[iid].name = name;
2157 port->wwpn_iid[iid].wwpn = wwpn;
2158 port->wwpn_iid[iid].in_use++;
2159 mtx_unlock(&softc->ctl_lock);
2160 ctl_isc_announce_iid(port, iid);
2166 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
2170 switch (port->port_type) {
2173 struct scsi_transportid_fcp *id =
2174 (struct scsi_transportid_fcp *)buf;
2175 if (port->wwpn_iid[iid].wwpn == 0)
2177 memset(id, 0, sizeof(*id));
2178 id->format_protocol = SCSI_PROTO_FC;
2179 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
2180 return (sizeof(*id));
2182 case CTL_PORT_ISCSI:
2184 struct scsi_transportid_iscsi_port *id =
2185 (struct scsi_transportid_iscsi_port *)buf;
2186 if (port->wwpn_iid[iid].name == NULL)
2189 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
2191 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
2192 len = roundup2(min(len, 252), 4);
2193 scsi_ulto2b(len, id->additional_length);
2194 return (sizeof(*id) + len);
2198 struct scsi_transportid_sas *id =
2199 (struct scsi_transportid_sas *)buf;
2200 if (port->wwpn_iid[iid].wwpn == 0)
2202 memset(id, 0, sizeof(*id));
2203 id->format_protocol = SCSI_PROTO_SAS;
2204 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
2205 return (sizeof(*id));
2209 struct scsi_transportid_spi *id =
2210 (struct scsi_transportid_spi *)buf;
2211 memset(id, 0, sizeof(*id));
2212 id->format_protocol = SCSI_PROTO_SPI;
2213 scsi_ulto2b(iid, id->scsi_addr);
2214 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
2215 return (sizeof(*id));
2221 * Serialize a command that went down the "wrong" side, and so was sent to
2222 * this controller for execution. The logic is a little different than the
2223 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
2224 * sent back to the other side, but in the success case, we execute the
2225 * command on this side (XFER mode) or tell the other side to execute it
2229 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
2231 struct ctl_softc *softc = CTL_SOFTC(ctsio);
2232 struct ctl_port *port = CTL_PORT(ctsio);
2233 union ctl_ha_msg msg_info;
2234 struct ctl_lun *lun;
2235 const struct ctl_cmd_entry *entry;
2238 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
2240 /* Make sure that we know about this port. */
2241 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) {
2242 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2247 /* Make sure that we know about this LUN. */
2248 mtx_lock(&softc->ctl_lock);
2249 if (targ_lun >= CTL_MAX_LUNS ||
2250 (lun = softc->ctl_luns[targ_lun]) == NULL) {
2251 mtx_unlock(&softc->ctl_lock);
2254 * The other node would not send this request to us unless
2255 * received announce that we are primary node for this LUN.
2256 * If this LUN does not exist now, it is probably result of
2257 * a race, so respond to initiator in the most opaque way.
2259 ctl_set_busy(ctsio);
2262 mtx_lock(&lun->lun_lock);
2263 mtx_unlock(&softc->ctl_lock);
2266 * If the LUN is invalid, pretend that it doesn't exist.
2267 * It will go away as soon as all pending I/Os completed.
2269 if (lun->flags & CTL_LUN_DISABLED) {
2270 mtx_unlock(&lun->lun_lock);
2271 ctl_set_busy(ctsio);
2275 entry = ctl_get_cmd_entry(ctsio, NULL);
2276 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
2277 mtx_unlock(&lun->lun_lock);
2281 CTL_LUN(ctsio) = lun;
2282 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
2285 * Every I/O goes into the OOA queue for a
2286 * particular LUN, and stays there until completion.
2289 if (TAILQ_EMPTY(&lun->ooa_queue))
2290 lun->idle_time += getsbinuptime() - lun->last_busy;
2292 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2294 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
2295 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
2297 case CTL_ACTION_BLOCK:
2298 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
2299 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
2301 mtx_unlock(&lun->lun_lock);
2303 case CTL_ACTION_PASS:
2304 case CTL_ACTION_SKIP:
2305 if (softc->ha_mode == CTL_HA_MODE_XFER) {
2306 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
2307 ctl_enqueue_rtr((union ctl_io *)ctsio);
2308 mtx_unlock(&lun->lun_lock);
2310 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
2311 mtx_unlock(&lun->lun_lock);
2313 /* send msg back to other side */
2314 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2315 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
2316 msg_info.hdr.msg_type = CTL_MSG_R2R;
2317 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2318 sizeof(msg_info.hdr), M_WAITOK);
2321 case CTL_ACTION_OVERLAP:
2322 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2323 mtx_unlock(&lun->lun_lock);
2324 ctl_set_overlapped_cmd(ctsio);
2326 case CTL_ACTION_OVERLAP_TAG:
2327 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2328 mtx_unlock(&lun->lun_lock);
2329 ctl_set_overlapped_tag(ctsio, ctsio->tag_num);
2331 case CTL_ACTION_ERROR:
2333 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2334 mtx_unlock(&lun->lun_lock);
2336 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2339 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
2340 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2341 msg_info.hdr.serializing_sc = NULL;
2342 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
2343 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2344 sizeof(msg_info.scsi), M_WAITOK);
2345 ctl_free_io((union ctl_io *)ctsio);
2351 * Returns 0 for success, errno for failure.
2354 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2355 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2359 mtx_lock(&lun->lun_lock);
2360 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2361 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2363 struct ctl_ooa_entry *entry;
2366 * If we've got more than we can fit, just count the
2367 * remaining entries.
2369 if (*cur_fill_num >= ooa_hdr->alloc_num)
2372 entry = &kern_entries[*cur_fill_num];
2374 entry->tag_num = io->scsiio.tag_num;
2375 entry->lun_num = lun->lun;
2377 entry->start_bt = io->io_hdr.start_bt;
2379 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2380 entry->cdb_len = io->scsiio.cdb_len;
2381 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2382 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2384 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2385 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2387 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2388 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2390 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2391 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2393 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2394 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2396 mtx_unlock(&lun->lun_lock);
2400 ctl_copyin_alloc(void *user_addr, unsigned int len, char *error_str,
2401 size_t error_str_len)
2405 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2407 if (copyin(user_addr, kptr, len) != 0) {
2408 snprintf(error_str, error_str_len, "Error copying %d bytes "
2409 "from user address %p to kernel address %p", len,
2419 ctl_free_args(int num_args, struct ctl_be_arg *args)
2426 for (i = 0; i < num_args; i++) {
2427 free(args[i].kname, M_CTL);
2428 free(args[i].kvalue, M_CTL);
2434 static struct ctl_be_arg *
2435 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
2436 char *error_str, size_t error_str_len)
2438 struct ctl_be_arg *args;
2441 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
2442 error_str, error_str_len);
2447 for (i = 0; i < num_args; i++) {
2448 args[i].kname = NULL;
2449 args[i].kvalue = NULL;
2452 for (i = 0; i < num_args; i++) {
2455 if (args[i].namelen == 0) {
2456 snprintf(error_str, error_str_len, "Argument %d "
2457 "name length is zero", i);
2461 args[i].kname = ctl_copyin_alloc(args[i].name,
2462 args[i].namelen, error_str, error_str_len);
2463 if (args[i].kname == NULL)
2466 if (args[i].kname[args[i].namelen - 1] != '\0') {
2467 snprintf(error_str, error_str_len, "Argument %d "
2468 "name is not NUL-terminated", i);
2472 if (args[i].flags & CTL_BEARG_RD) {
2473 if (args[i].vallen == 0) {
2474 snprintf(error_str, error_str_len, "Argument %d "
2475 "value length is zero", i);
2479 tmpptr = ctl_copyin_alloc(args[i].value,
2480 args[i].vallen, error_str, error_str_len);
2484 if ((args[i].flags & CTL_BEARG_ASCII)
2485 && (tmpptr[args[i].vallen - 1] != '\0')) {
2486 snprintf(error_str, error_str_len, "Argument "
2487 "%d value is not NUL-terminated", i);
2488 free(tmpptr, M_CTL);
2491 args[i].kvalue = tmpptr;
2493 args[i].kvalue = malloc(args[i].vallen,
2494 M_CTL, M_WAITOK | M_ZERO);
2501 ctl_free_args(num_args, args);
2507 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2511 for (i = 0; i < num_args; i++) {
2512 if (args[i].flags & CTL_BEARG_WR)
2513 copyout(args[i].kvalue, args[i].value, args[i].vallen);
2518 * Escape characters that are illegal or not recommended in XML.
2521 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2523 char *end = str + size;
2528 for (; *str && str < end; str++) {
2531 retval = sbuf_printf(sb, "&");
2534 retval = sbuf_printf(sb, ">");
2537 retval = sbuf_printf(sb, "<");
2540 retval = sbuf_putc(sb, *str);
2553 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2555 struct scsi_vpd_id_descriptor *desc;
2558 if (id == NULL || id->len < 4)
2560 desc = (struct scsi_vpd_id_descriptor *)id->data;
2561 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2562 case SVPD_ID_TYPE_T10:
2563 sbuf_printf(sb, "t10.");
2565 case SVPD_ID_TYPE_EUI64:
2566 sbuf_printf(sb, "eui.");
2568 case SVPD_ID_TYPE_NAA:
2569 sbuf_printf(sb, "naa.");
2571 case SVPD_ID_TYPE_SCSI_NAME:
2574 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2575 case SVPD_ID_CODESET_BINARY:
2576 for (i = 0; i < desc->length; i++)
2577 sbuf_printf(sb, "%02x", desc->identifier[i]);
2579 case SVPD_ID_CODESET_ASCII:
2580 sbuf_printf(sb, "%.*s", (int)desc->length,
2581 (char *)desc->identifier);
2583 case SVPD_ID_CODESET_UTF8:
2584 sbuf_printf(sb, "%s", (char *)desc->identifier);
2590 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2593 struct ctl_softc *softc = dev->si_drv1;
2594 struct ctl_port *port;
2595 struct ctl_lun *lun;
2602 retval = ctl_ioctl_io(dev, cmd, addr, flag, td);
2604 case CTL_ENABLE_PORT:
2605 case CTL_DISABLE_PORT:
2606 case CTL_SET_PORT_WWNS: {
2607 struct ctl_port *port;
2608 struct ctl_port_entry *entry;
2610 entry = (struct ctl_port_entry *)addr;
2612 mtx_lock(&softc->ctl_lock);
2613 STAILQ_FOREACH(port, &softc->port_list, links) {
2616 if (port->targ_port < softc->port_min ||
2617 port->targ_port >= softc->port_max)
2622 if ((entry->port_type == CTL_PORT_NONE)
2623 && (entry->targ_port == port->targ_port)) {
2625 * If the user only wants to enable or
2626 * disable or set WWNs on a specific port,
2627 * do the operation and we're done.
2631 } else if (entry->port_type & port->port_type) {
2633 * Compare the user's type mask with the
2634 * particular frontend type to see if we
2641 * Make sure the user isn't trying to set
2642 * WWNs on multiple ports at the same time.
2644 if (cmd == CTL_SET_PORT_WWNS) {
2645 printf("%s: Can't set WWNs on "
2646 "multiple ports\n", __func__);
2655 * XXX KDM we have to drop the lock here, because
2656 * the online/offline operations can potentially
2657 * block. We need to reference count the frontends
2658 * so they can't go away,
2660 if (cmd == CTL_ENABLE_PORT) {
2661 mtx_unlock(&softc->ctl_lock);
2662 ctl_port_online(port);
2663 mtx_lock(&softc->ctl_lock);
2664 } else if (cmd == CTL_DISABLE_PORT) {
2665 mtx_unlock(&softc->ctl_lock);
2666 ctl_port_offline(port);
2667 mtx_lock(&softc->ctl_lock);
2668 } else if (cmd == CTL_SET_PORT_WWNS) {
2669 ctl_port_set_wwns(port,
2670 (entry->flags & CTL_PORT_WWNN_VALID) ?
2672 (entry->flags & CTL_PORT_WWPN_VALID) ?
2673 1 : 0, entry->wwpn);
2678 mtx_unlock(&softc->ctl_lock);
2682 struct ctl_ooa *ooa_hdr;
2683 struct ctl_ooa_entry *entries;
2684 uint32_t cur_fill_num;
2686 ooa_hdr = (struct ctl_ooa *)addr;
2688 if ((ooa_hdr->alloc_len == 0)
2689 || (ooa_hdr->alloc_num == 0)) {
2690 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2691 "must be non-zero\n", __func__,
2692 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2697 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2698 sizeof(struct ctl_ooa_entry))) {
2699 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2700 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2701 __func__, ooa_hdr->alloc_len,
2702 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2707 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2708 if (entries == NULL) {
2709 printf("%s: could not allocate %d bytes for OOA "
2710 "dump\n", __func__, ooa_hdr->alloc_len);
2715 mtx_lock(&softc->ctl_lock);
2716 if ((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0 &&
2717 (ooa_hdr->lun_num >= CTL_MAX_LUNS ||
2718 softc->ctl_luns[ooa_hdr->lun_num] == NULL)) {
2719 mtx_unlock(&softc->ctl_lock);
2720 free(entries, M_CTL);
2721 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2722 __func__, (uintmax_t)ooa_hdr->lun_num);
2729 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2730 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2731 ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2735 lun = softc->ctl_luns[ooa_hdr->lun_num];
2736 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr,
2739 mtx_unlock(&softc->ctl_lock);
2741 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2742 ooa_hdr->fill_len = ooa_hdr->fill_num *
2743 sizeof(struct ctl_ooa_entry);
2744 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2746 printf("%s: error copying out %d bytes for OOA dump\n",
2747 __func__, ooa_hdr->fill_len);
2750 getbinuptime(&ooa_hdr->cur_bt);
2752 if (cur_fill_num > ooa_hdr->alloc_num) {
2753 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2754 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2756 ooa_hdr->dropped_num = 0;
2757 ooa_hdr->status = CTL_OOA_OK;
2760 free(entries, M_CTL);
2763 case CTL_DELAY_IO: {
2764 struct ctl_io_delay_info *delay_info;
2766 delay_info = (struct ctl_io_delay_info *)addr;
2769 mtx_lock(&softc->ctl_lock);
2770 if (delay_info->lun_id >= CTL_MAX_LUNS ||
2771 (lun = softc->ctl_luns[delay_info->lun_id]) == NULL) {
2772 mtx_unlock(&softc->ctl_lock);
2773 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2776 mtx_lock(&lun->lun_lock);
2777 mtx_unlock(&softc->ctl_lock);
2778 delay_info->status = CTL_DELAY_STATUS_OK;
2779 switch (delay_info->delay_type) {
2780 case CTL_DELAY_TYPE_CONT:
2781 case CTL_DELAY_TYPE_ONESHOT:
2784 delay_info->status = CTL_DELAY_STATUS_INVALID_TYPE;
2787 switch (delay_info->delay_loc) {
2788 case CTL_DELAY_LOC_DATAMOVE:
2789 lun->delay_info.datamove_type = delay_info->delay_type;
2790 lun->delay_info.datamove_delay = delay_info->delay_secs;
2792 case CTL_DELAY_LOC_DONE:
2793 lun->delay_info.done_type = delay_info->delay_type;
2794 lun->delay_info.done_delay = delay_info->delay_secs;
2797 delay_info->status = CTL_DELAY_STATUS_INVALID_LOC;
2800 mtx_unlock(&lun->lun_lock);
2802 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2803 #endif /* CTL_IO_DELAY */
2806 #ifdef CTL_LEGACY_STATS
2807 case CTL_GETSTATS: {
2808 struct ctl_stats *stats = (struct ctl_stats *)addr;
2812 * XXX KDM no locking here. If the LUN list changes,
2813 * things can blow up.
2816 stats->status = CTL_SS_OK;
2817 stats->fill_len = 0;
2818 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2819 if (stats->fill_len + sizeof(lun->legacy_stats) >
2821 stats->status = CTL_SS_NEED_MORE_SPACE;
2824 retval = copyout(&lun->legacy_stats, &stats->lun_stats[i++],
2825 sizeof(lun->legacy_stats));
2828 stats->fill_len += sizeof(lun->legacy_stats);
2830 stats->num_luns = softc->num_luns;
2831 stats->flags = CTL_STATS_FLAG_NONE;
2833 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
2835 getnanouptime(&stats->timestamp);
2838 #endif /* CTL_LEGACY_STATS */
2839 case CTL_ERROR_INJECT: {
2840 struct ctl_error_desc *err_desc, *new_err_desc;
2842 err_desc = (struct ctl_error_desc *)addr;
2844 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2846 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2848 mtx_lock(&softc->ctl_lock);
2849 if (err_desc->lun_id >= CTL_MAX_LUNS ||
2850 (lun = softc->ctl_luns[err_desc->lun_id]) == NULL) {
2851 mtx_unlock(&softc->ctl_lock);
2852 free(new_err_desc, M_CTL);
2853 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2854 __func__, (uintmax_t)err_desc->lun_id);
2858 mtx_lock(&lun->lun_lock);
2859 mtx_unlock(&softc->ctl_lock);
2862 * We could do some checking here to verify the validity
2863 * of the request, but given the complexity of error
2864 * injection requests, the checking logic would be fairly
2867 * For now, if the request is invalid, it just won't get
2868 * executed and might get deleted.
2870 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2873 * XXX KDM check to make sure the serial number is unique,
2874 * in case we somehow manage to wrap. That shouldn't
2875 * happen for a very long time, but it's the right thing to
2878 new_err_desc->serial = lun->error_serial;
2879 err_desc->serial = lun->error_serial;
2880 lun->error_serial++;
2882 mtx_unlock(&lun->lun_lock);
2885 case CTL_ERROR_INJECT_DELETE: {
2886 struct ctl_error_desc *delete_desc, *desc, *desc2;
2889 delete_desc = (struct ctl_error_desc *)addr;
2892 mtx_lock(&softc->ctl_lock);
2893 if (delete_desc->lun_id >= CTL_MAX_LUNS ||
2894 (lun = softc->ctl_luns[delete_desc->lun_id]) == NULL) {
2895 mtx_unlock(&softc->ctl_lock);
2896 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2897 __func__, (uintmax_t)delete_desc->lun_id);
2901 mtx_lock(&lun->lun_lock);
2902 mtx_unlock(&softc->ctl_lock);
2903 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2904 if (desc->serial != delete_desc->serial)
2907 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2912 mtx_unlock(&lun->lun_lock);
2913 if (delete_done == 0) {
2914 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2915 "error serial %ju on LUN %u\n", __func__,
2916 delete_desc->serial, delete_desc->lun_id);
2922 case CTL_DUMP_STRUCTS: {
2924 struct ctl_port *port;
2925 struct ctl_frontend *fe;
2927 mtx_lock(&softc->ctl_lock);
2928 printf("CTL Persistent Reservation information start:\n");
2929 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2930 mtx_lock(&lun->lun_lock);
2931 if ((lun->flags & CTL_LUN_DISABLED) != 0) {
2932 mtx_unlock(&lun->lun_lock);
2936 for (j = 0; j < CTL_MAX_PORTS; j++) {
2937 if (lun->pr_keys[j] == NULL)
2939 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2940 if (lun->pr_keys[j][k] == 0)
2942 printf(" LUN %ju port %d iid %d key "
2943 "%#jx\n", lun->lun, j, k,
2944 (uintmax_t)lun->pr_keys[j][k]);
2947 mtx_unlock(&lun->lun_lock);
2949 printf("CTL Persistent Reservation information end\n");
2950 printf("CTL Ports:\n");
2951 STAILQ_FOREACH(port, &softc->port_list, links) {
2952 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
2953 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
2954 port->frontend->name, port->port_type,
2955 port->physical_port, port->virtual_port,
2956 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
2957 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2958 if (port->wwpn_iid[j].in_use == 0 &&
2959 port->wwpn_iid[j].wwpn == 0 &&
2960 port->wwpn_iid[j].name == NULL)
2963 printf(" iid %u use %d WWPN %#jx '%s'\n",
2964 j, port->wwpn_iid[j].in_use,
2965 (uintmax_t)port->wwpn_iid[j].wwpn,
2966 port->wwpn_iid[j].name);
2969 printf("CTL Port information end\n");
2970 mtx_unlock(&softc->ctl_lock);
2972 * XXX KDM calling this without a lock. We'd likely want
2973 * to drop the lock before calling the frontend's dump
2976 printf("CTL Frontends:\n");
2977 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2978 printf(" Frontend '%s'\n", fe->name);
2979 if (fe->fe_dump != NULL)
2982 printf("CTL Frontend information end\n");
2986 struct ctl_lun_req *lun_req;
2987 struct ctl_backend_driver *backend;
2989 lun_req = (struct ctl_lun_req *)addr;
2991 backend = ctl_backend_find(lun_req->backend);
2992 if (backend == NULL) {
2993 lun_req->status = CTL_LUN_ERROR;
2994 snprintf(lun_req->error_str,
2995 sizeof(lun_req->error_str),
2996 "Backend \"%s\" not found.",
3000 if (lun_req->num_be_args > 0) {
3001 lun_req->kern_be_args = ctl_copyin_args(
3002 lun_req->num_be_args,
3005 sizeof(lun_req->error_str));
3006 if (lun_req->kern_be_args == NULL) {
3007 lun_req->status = CTL_LUN_ERROR;
3012 retval = backend->ioctl(dev, cmd, addr, flag, td);
3014 if (lun_req->num_be_args > 0) {
3015 ctl_copyout_args(lun_req->num_be_args,
3016 lun_req->kern_be_args);
3017 ctl_free_args(lun_req->num_be_args,
3018 lun_req->kern_be_args);
3022 case CTL_LUN_LIST: {
3024 struct ctl_lun_list *list;
3025 struct ctl_option *opt;
3027 list = (struct ctl_lun_list *)addr;
3030 * Allocate a fixed length sbuf here, based on the length
3031 * of the user's buffer. We could allocate an auto-extending
3032 * buffer, and then tell the user how much larger our
3033 * amount of data is than his buffer, but that presents
3036 * 1. The sbuf(9) routines use a blocking malloc, and so
3037 * we can't hold a lock while calling them with an
3038 * auto-extending buffer.
3040 * 2. There is not currently a LUN reference counting
3041 * mechanism, outside of outstanding transactions on
3042 * the LUN's OOA queue. So a LUN could go away on us
3043 * while we're getting the LUN number, backend-specific
3044 * information, etc. Thus, given the way things
3045 * currently work, we need to hold the CTL lock while
3046 * grabbing LUN information.
3048 * So, from the user's standpoint, the best thing to do is
3049 * allocate what he thinks is a reasonable buffer length,
3050 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3051 * double the buffer length and try again. (And repeat
3052 * that until he succeeds.)
3054 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3056 list->status = CTL_LUN_LIST_ERROR;
3057 snprintf(list->error_str, sizeof(list->error_str),
3058 "Unable to allocate %d bytes for LUN list",
3063 sbuf_printf(sb, "<ctllunlist>\n");
3065 mtx_lock(&softc->ctl_lock);
3066 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3067 mtx_lock(&lun->lun_lock);
3068 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3069 (uintmax_t)lun->lun);
3072 * Bail out as soon as we see that we've overfilled
3078 retval = sbuf_printf(sb, "\t<backend_type>%s"
3079 "</backend_type>\n",
3080 (lun->backend == NULL) ? "none" :
3081 lun->backend->name);
3086 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3087 lun->be_lun->lun_type);
3092 if (lun->backend == NULL) {
3093 retval = sbuf_printf(sb, "</lun>\n");
3099 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3100 (lun->be_lun->maxlba > 0) ?
3101 lun->be_lun->maxlba + 1 : 0);
3106 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3107 lun->be_lun->blocksize);
3112 retval = sbuf_printf(sb, "\t<serial_number>");
3117 retval = ctl_sbuf_printf_esc(sb,
3118 lun->be_lun->serial_num,
3119 sizeof(lun->be_lun->serial_num));
3124 retval = sbuf_printf(sb, "</serial_number>\n");
3129 retval = sbuf_printf(sb, "\t<device_id>");
3134 retval = ctl_sbuf_printf_esc(sb,
3135 lun->be_lun->device_id,
3136 sizeof(lun->be_lun->device_id));
3141 retval = sbuf_printf(sb, "</device_id>\n");
3146 if (lun->backend->lun_info != NULL) {
3147 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3151 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3152 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3153 opt->name, opt->value, opt->name);
3158 retval = sbuf_printf(sb, "</lun>\n");
3162 mtx_unlock(&lun->lun_lock);
3165 mtx_unlock(&lun->lun_lock);
3166 mtx_unlock(&softc->ctl_lock);
3169 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3172 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3173 snprintf(list->error_str, sizeof(list->error_str),
3174 "Out of space, %d bytes is too small",
3181 retval = copyout(sbuf_data(sb), list->lun_xml,
3184 list->fill_len = sbuf_len(sb) + 1;
3185 list->status = CTL_LUN_LIST_OK;
3190 struct ctl_iscsi *ci;
3191 struct ctl_frontend *fe;
3193 ci = (struct ctl_iscsi *)addr;
3195 fe = ctl_frontend_find("iscsi");
3197 ci->status = CTL_ISCSI_ERROR;
3198 snprintf(ci->error_str, sizeof(ci->error_str),
3199 "Frontend \"iscsi\" not found.");
3203 retval = fe->ioctl(dev, cmd, addr, flag, td);
3206 case CTL_PORT_REQ: {
3207 struct ctl_req *req;
3208 struct ctl_frontend *fe;
3210 req = (struct ctl_req *)addr;
3212 fe = ctl_frontend_find(req->driver);
3214 req->status = CTL_LUN_ERROR;
3215 snprintf(req->error_str, sizeof(req->error_str),
3216 "Frontend \"%s\" not found.", req->driver);
3219 if (req->num_args > 0) {
3220 req->kern_args = ctl_copyin_args(req->num_args,
3221 req->args, req->error_str, sizeof(req->error_str));
3222 if (req->kern_args == NULL) {
3223 req->status = CTL_LUN_ERROR;
3229 retval = fe->ioctl(dev, cmd, addr, flag, td);
3233 if (req->num_args > 0) {
3234 ctl_copyout_args(req->num_args, req->kern_args);
3235 ctl_free_args(req->num_args, req->kern_args);
3239 case CTL_PORT_LIST: {
3241 struct ctl_port *port;
3242 struct ctl_lun_list *list;
3243 struct ctl_option *opt;
3247 list = (struct ctl_lun_list *)addr;
3249 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3251 list->status = CTL_LUN_LIST_ERROR;
3252 snprintf(list->error_str, sizeof(list->error_str),
3253 "Unable to allocate %d bytes for LUN list",
3258 sbuf_printf(sb, "<ctlportlist>\n");
3260 mtx_lock(&softc->ctl_lock);
3261 STAILQ_FOREACH(port, &softc->port_list, links) {
3262 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3263 (uintmax_t)port->targ_port);
3266 * Bail out as soon as we see that we've overfilled
3272 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3273 "</frontend_type>\n", port->frontend->name);
3277 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3282 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3283 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3287 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3292 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3293 port->physical_port);
3297 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3298 port->virtual_port);
3302 if (port->target_devid != NULL) {
3303 sbuf_printf(sb, "\t<target>");
3304 ctl_id_sbuf(port->target_devid, sb);
3305 sbuf_printf(sb, "</target>\n");
3308 if (port->port_devid != NULL) {
3309 sbuf_printf(sb, "\t<port>");
3310 ctl_id_sbuf(port->port_devid, sb);
3311 sbuf_printf(sb, "</port>\n");
3314 if (port->port_info != NULL) {
3315 retval = port->port_info(port->onoff_arg, sb);
3319 STAILQ_FOREACH(opt, &port->options, links) {
3320 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3321 opt->name, opt->value, opt->name);
3326 if (port->lun_map != NULL) {
3327 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n");
3328 for (j = 0; j < port->lun_map_size; j++) {
3329 plun = ctl_lun_map_from_port(port, j);
3330 if (plun == UINT32_MAX)
3333 "\t<lun id=\"%u\">%u</lun>\n",
3338 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3339 if (port->wwpn_iid[j].in_use == 0 ||
3340 (port->wwpn_iid[j].wwpn == 0 &&
3341 port->wwpn_iid[j].name == NULL))
3344 if (port->wwpn_iid[j].name != NULL)
3345 retval = sbuf_printf(sb,
3346 "\t<initiator id=\"%u\">%s</initiator>\n",
3347 j, port->wwpn_iid[j].name);
3349 retval = sbuf_printf(sb,
3350 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3351 j, port->wwpn_iid[j].wwpn);
3358 retval = sbuf_printf(sb, "</targ_port>\n");
3362 mtx_unlock(&softc->ctl_lock);
3365 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3368 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3369 snprintf(list->error_str, sizeof(list->error_str),
3370 "Out of space, %d bytes is too small",
3377 retval = copyout(sbuf_data(sb), list->lun_xml,
3380 list->fill_len = sbuf_len(sb) + 1;
3381 list->status = CTL_LUN_LIST_OK;
3386 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr;
3387 struct ctl_port *port;
3389 mtx_lock(&softc->ctl_lock);
3390 if (lm->port < softc->port_min ||
3391 lm->port >= softc->port_max ||
3392 (port = softc->ctl_ports[lm->port]) == NULL) {
3393 mtx_unlock(&softc->ctl_lock);
3396 if (port->status & CTL_PORT_STATUS_ONLINE) {
3397 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3398 if (ctl_lun_map_to_port(port, lun->lun) ==
3401 mtx_lock(&lun->lun_lock);
3402 ctl_est_ua_port(lun, lm->port, -1,
3404 mtx_unlock(&lun->lun_lock);
3407 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps
3408 if (lm->plun != UINT32_MAX) {
3409 if (lm->lun == UINT32_MAX)
3410 retval = ctl_lun_map_unset(port, lm->plun);
3411 else if (lm->lun < CTL_MAX_LUNS &&
3412 softc->ctl_luns[lm->lun] != NULL)
3413 retval = ctl_lun_map_set(port, lm->plun, lm->lun);
3417 if (lm->lun == UINT32_MAX)
3418 retval = ctl_lun_map_deinit(port);
3420 retval = ctl_lun_map_init(port);
3422 if (port->status & CTL_PORT_STATUS_ONLINE)
3423 ctl_isc_announce_port(port);
3426 case CTL_GET_LUN_STATS: {
3427 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr;
3431 * XXX KDM no locking here. If the LUN list changes,
3432 * things can blow up.
3435 stats->status = CTL_SS_OK;
3436 stats->fill_len = 0;
3437 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3438 if (lun->lun < stats->first_item)
3440 if (stats->fill_len + sizeof(lun->stats) >
3442 stats->status = CTL_SS_NEED_MORE_SPACE;
3445 retval = copyout(&lun->stats, &stats->stats[i++],
3446 sizeof(lun->stats));
3449 stats->fill_len += sizeof(lun->stats);
3451 stats->num_items = softc->num_luns;
3452 stats->flags = CTL_STATS_FLAG_NONE;
3454 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
3456 getnanouptime(&stats->timestamp);
3459 case CTL_GET_PORT_STATS: {
3460 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr;
3464 * XXX KDM no locking here. If the LUN list changes,
3465 * things can blow up.
3468 stats->status = CTL_SS_OK;
3469 stats->fill_len = 0;
3470 STAILQ_FOREACH(port, &softc->port_list, links) {
3471 if (port->targ_port < stats->first_item)
3473 if (stats->fill_len + sizeof(port->stats) >
3475 stats->status = CTL_SS_NEED_MORE_SPACE;
3478 retval = copyout(&port->stats, &stats->stats[i++],
3479 sizeof(port->stats));
3482 stats->fill_len += sizeof(port->stats);
3484 stats->num_items = softc->num_ports;
3485 stats->flags = CTL_STATS_FLAG_NONE;
3487 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
3489 getnanouptime(&stats->timestamp);
3493 /* XXX KDM should we fix this? */
3495 struct ctl_backend_driver *backend;
3502 * We encode the backend type as the ioctl type for backend
3503 * ioctls. So parse it out here, and then search for a
3504 * backend of this type.
3506 type = _IOC_TYPE(cmd);
3508 STAILQ_FOREACH(backend, &softc->be_list, links) {
3509 if (backend->type == type) {
3515 printf("ctl: unknown ioctl command %#lx or backend "
3520 retval = backend->ioctl(dev, cmd, addr, flag, td);
3530 ctl_get_initindex(struct ctl_nexus *nexus)
3532 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3536 ctl_lun_map_init(struct ctl_port *port)
3538 struct ctl_softc *softc = port->ctl_softc;
3539 struct ctl_lun *lun;
3540 int size = ctl_lun_map_size;
3543 if (port->lun_map == NULL || port->lun_map_size < size) {
3544 port->lun_map_size = 0;
3545 free(port->lun_map, M_CTL);
3546 port->lun_map = malloc(size * sizeof(uint32_t),
3549 if (port->lun_map == NULL)
3551 for (i = 0; i < size; i++)
3552 port->lun_map[i] = UINT32_MAX;
3553 port->lun_map_size = size;
3554 if (port->status & CTL_PORT_STATUS_ONLINE) {
3555 if (port->lun_disable != NULL) {
3556 STAILQ_FOREACH(lun, &softc->lun_list, links)
3557 port->lun_disable(port->targ_lun_arg, lun->lun);
3559 ctl_isc_announce_port(port);
3565 ctl_lun_map_deinit(struct ctl_port *port)
3567 struct ctl_softc *softc = port->ctl_softc;
3568 struct ctl_lun *lun;
3570 if (port->lun_map == NULL)
3572 port->lun_map_size = 0;
3573 free(port->lun_map, M_CTL);
3574 port->lun_map = NULL;
3575 if (port->status & CTL_PORT_STATUS_ONLINE) {
3576 if (port->lun_enable != NULL) {
3577 STAILQ_FOREACH(lun, &softc->lun_list, links)
3578 port->lun_enable(port->targ_lun_arg, lun->lun);
3580 ctl_isc_announce_port(port);
3586 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun)
3591 if (port->lun_map == NULL) {
3592 status = ctl_lun_map_init(port);
3596 if (plun >= port->lun_map_size)
3598 old = port->lun_map[plun];
3599 port->lun_map[plun] = glun;
3600 if ((port->status & CTL_PORT_STATUS_ONLINE) && old == UINT32_MAX) {
3601 if (port->lun_enable != NULL)
3602 port->lun_enable(port->targ_lun_arg, plun);
3603 ctl_isc_announce_port(port);
3609 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun)
3613 if (port->lun_map == NULL || plun >= port->lun_map_size)
3615 old = port->lun_map[plun];
3616 port->lun_map[plun] = UINT32_MAX;
3617 if ((port->status & CTL_PORT_STATUS_ONLINE) && old != UINT32_MAX) {
3618 if (port->lun_disable != NULL)
3619 port->lun_disable(port->targ_lun_arg, plun);
3620 ctl_isc_announce_port(port);
3626 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
3630 return (UINT32_MAX);
3631 if (port->lun_map == NULL)
3633 if (lun_id > port->lun_map_size)
3634 return (UINT32_MAX);
3635 return (port->lun_map[lun_id]);
3639 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
3644 return (UINT32_MAX);
3645 if (port->lun_map == NULL)
3647 for (i = 0; i < port->lun_map_size; i++) {
3648 if (port->lun_map[i] == lun_id)
3651 return (UINT32_MAX);
3655 ctl_decode_lun(uint64_t encoded)
3658 uint32_t result = 0xffffffff;
3660 be64enc(lun, encoded);
3661 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) {
3662 case RPL_LUNDATA_ATYP_PERIPH:
3663 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 &&
3664 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0)
3667 case RPL_LUNDATA_ATYP_FLAT:
3668 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 &&
3669 lun[6] == 0 && lun[7] == 0)
3670 result = ((lun[0] & 0x3f) << 8) + lun[1];
3672 case RPL_LUNDATA_ATYP_EXTLUN:
3673 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) {
3675 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) {
3680 result = (lun[1] << 16) + (lun[2] << 8) +
3684 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0)
3685 result = (lun[2] << 24) +
3686 (lun[3] << 16) + (lun[4] << 8) +
3691 case RPL_LUNDATA_EXT_EAM_NOT_SPEC:
3692 result = 0xffffffff;
3701 ctl_encode_lun(uint32_t decoded)
3703 uint64_t l = decoded;
3706 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48));
3708 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48));
3710 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) |
3712 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16));
3716 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last)
3720 for (i = first; i < last; i++) {
3721 if ((mask[i / 32] & (1 << (i % 32))) == 0)
3728 ctl_set_mask(uint32_t *mask, uint32_t bit)
3730 uint32_t chunk, piece;
3733 piece = bit % (sizeof(uint32_t) * 8);
3735 if ((mask[chunk] & (1 << piece)) != 0)
3738 mask[chunk] |= (1 << piece);
3744 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3746 uint32_t chunk, piece;
3749 piece = bit % (sizeof(uint32_t) * 8);
3751 if ((mask[chunk] & (1 << piece)) == 0)
3754 mask[chunk] &= ~(1 << piece);
3760 ctl_is_set(uint32_t *mask, uint32_t bit)
3762 uint32_t chunk, piece;
3765 piece = bit % (sizeof(uint32_t) * 8);
3767 if ((mask[chunk] & (1 << piece)) == 0)
3774 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3778 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3781 return (t[residx % CTL_MAX_INIT_PER_PORT]);
3785 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3789 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3792 t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3796 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3801 i = residx/CTL_MAX_INIT_PER_PORT;
3802 if (lun->pr_keys[i] != NULL)
3804 mtx_unlock(&lun->lun_lock);
3805 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3807 mtx_lock(&lun->lun_lock);
3808 if (lun->pr_keys[i] == NULL)
3809 lun->pr_keys[i] = p;
3815 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3819 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3820 KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3821 t[residx % CTL_MAX_INIT_PER_PORT] = key;
3825 * ctl_softc, pool_name, total_ctl_io are passed in.
3826 * npool is passed out.
3829 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3830 uint32_t total_ctl_io, void **npool)
3832 struct ctl_io_pool *pool;
3834 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3839 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3840 pool->ctl_softc = ctl_softc;
3842 pool->zone = uma_zsecond_create(pool->name, NULL,
3843 NULL, NULL, NULL, ctl_softc->io_zone);
3844 /* uma_prealloc(pool->zone, total_ctl_io); */
3846 pool->zone = ctl_softc->io_zone;
3854 ctl_pool_free(struct ctl_io_pool *pool)
3861 uma_zdestroy(pool->zone);
3867 ctl_alloc_io(void *pool_ref)
3869 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3872 io = uma_zalloc(pool->zone, M_WAITOK);
3874 io->io_hdr.pool = pool_ref;
3875 CTL_SOFTC(io) = pool->ctl_softc;
3881 ctl_alloc_io_nowait(void *pool_ref)
3883 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3886 io = uma_zalloc(pool->zone, M_NOWAIT);
3888 io->io_hdr.pool = pool_ref;
3889 CTL_SOFTC(io) = pool->ctl_softc;
3895 ctl_free_io(union ctl_io *io)
3897 struct ctl_io_pool *pool;
3902 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3903 uma_zfree(pool->zone, io);
3907 ctl_zero_io(union ctl_io *io)
3909 struct ctl_io_pool *pool;
3915 * May need to preserve linked list pointers at some point too.
3917 pool = io->io_hdr.pool;
3918 memset(io, 0, sizeof(*io));
3919 io->io_hdr.pool = pool;
3920 CTL_SOFTC(io) = pool->ctl_softc;
3924 ctl_expand_number(const char *buf, uint64_t *num)
3930 number = strtoq(buf, &endptr, 0);
3932 switch (tolower((unsigned char)*endptr)) {
3952 case '\0': /* No unit. */
3956 /* Unrecognized unit. */
3960 if ((number << shift) >> shift != number) {
3964 *num = number << shift;
3970 * This routine could be used in the future to load default and/or saved
3971 * mode page parameters for a particuar lun.
3974 ctl_init_page_index(struct ctl_lun *lun)
3977 struct ctl_page_index *page_index;
3981 memcpy(&lun->mode_pages.index, page_index_template,
3982 sizeof(page_index_template));
3984 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3986 page_index = &lun->mode_pages.index[i];
3987 if (lun->be_lun->lun_type == T_DIRECT &&
3988 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
3990 if (lun->be_lun->lun_type == T_PROCESSOR &&
3991 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
3993 if (lun->be_lun->lun_type == T_CDROM &&
3994 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
3997 page_code = page_index->page_code & SMPH_PC_MASK;
3998 switch (page_code) {
3999 case SMS_RW_ERROR_RECOVERY_PAGE: {
4000 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4001 ("subpage %#x for page %#x is incorrect!",
4002 page_index->subpage, page_code));
4003 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
4004 &rw_er_page_default,
4005 sizeof(rw_er_page_default));
4006 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
4007 &rw_er_page_changeable,
4008 sizeof(rw_er_page_changeable));
4009 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
4010 &rw_er_page_default,
4011 sizeof(rw_er_page_default));
4012 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
4013 &rw_er_page_default,
4014 sizeof(rw_er_page_default));
4015 page_index->page_data =
4016 (uint8_t *)lun->mode_pages.rw_er_page;
4019 case SMS_FORMAT_DEVICE_PAGE: {
4020 struct scsi_format_page *format_page;
4022 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4023 ("subpage %#x for page %#x is incorrect!",
4024 page_index->subpage, page_code));
4027 * Sectors per track are set above. Bytes per
4028 * sector need to be set here on a per-LUN basis.
4030 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
4031 &format_page_default,
4032 sizeof(format_page_default));
4033 memcpy(&lun->mode_pages.format_page[
4034 CTL_PAGE_CHANGEABLE], &format_page_changeable,
4035 sizeof(format_page_changeable));
4036 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
4037 &format_page_default,
4038 sizeof(format_page_default));
4039 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
4040 &format_page_default,
4041 sizeof(format_page_default));
4043 format_page = &lun->mode_pages.format_page[
4045 scsi_ulto2b(lun->be_lun->blocksize,
4046 format_page->bytes_per_sector);
4048 format_page = &lun->mode_pages.format_page[
4050 scsi_ulto2b(lun->be_lun->blocksize,
4051 format_page->bytes_per_sector);
4053 format_page = &lun->mode_pages.format_page[
4055 scsi_ulto2b(lun->be_lun->blocksize,
4056 format_page->bytes_per_sector);
4058 page_index->page_data =
4059 (uint8_t *)lun->mode_pages.format_page;
4062 case SMS_RIGID_DISK_PAGE: {
4063 struct scsi_rigid_disk_page *rigid_disk_page;
4064 uint32_t sectors_per_cylinder;
4068 #endif /* !__XSCALE__ */
4070 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4071 ("subpage %#x for page %#x is incorrect!",
4072 page_index->subpage, page_code));
4075 * Rotation rate and sectors per track are set
4076 * above. We calculate the cylinders here based on
4077 * capacity. Due to the number of heads and
4078 * sectors per track we're using, smaller arrays
4079 * may turn out to have 0 cylinders. Linux and
4080 * FreeBSD don't pay attention to these mode pages
4081 * to figure out capacity, but Solaris does. It
4082 * seems to deal with 0 cylinders just fine, and
4083 * works out a fake geometry based on the capacity.
4085 memcpy(&lun->mode_pages.rigid_disk_page[
4086 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4087 sizeof(rigid_disk_page_default));
4088 memcpy(&lun->mode_pages.rigid_disk_page[
4089 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4090 sizeof(rigid_disk_page_changeable));
4092 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4096 * The divide method here will be more accurate,
4097 * probably, but results in floating point being
4098 * used in the kernel on i386 (__udivdi3()). On the
4099 * XScale, though, __udivdi3() is implemented in
4102 * The shift method for cylinder calculation is
4103 * accurate if sectors_per_cylinder is a power of
4104 * 2. Otherwise it might be slightly off -- you
4105 * might have a bit of a truncation problem.
4108 cylinders = (lun->be_lun->maxlba + 1) /
4109 sectors_per_cylinder;
4111 for (shift = 31; shift > 0; shift--) {
4112 if (sectors_per_cylinder & (1 << shift))
4115 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4119 * We've basically got 3 bytes, or 24 bits for the
4120 * cylinder size in the mode page. If we're over,
4121 * just round down to 2^24.
4123 if (cylinders > 0xffffff)
4124 cylinders = 0xffffff;
4126 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4128 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4130 if ((value = ctl_get_opt(&lun->be_lun->options,
4132 scsi_ulto2b(strtol(value, NULL, 0),
4133 rigid_disk_page->rotation_rate);
4136 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
4137 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4138 sizeof(rigid_disk_page_default));
4139 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
4140 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4141 sizeof(rigid_disk_page_default));
4143 page_index->page_data =
4144 (uint8_t *)lun->mode_pages.rigid_disk_page;
4147 case SMS_VERIFY_ERROR_RECOVERY_PAGE: {
4148 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4149 ("subpage %#x for page %#x is incorrect!",
4150 page_index->subpage, page_code));
4151 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CURRENT],
4152 &verify_er_page_default,
4153 sizeof(verify_er_page_default));
4154 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CHANGEABLE],
4155 &verify_er_page_changeable,
4156 sizeof(verify_er_page_changeable));
4157 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_DEFAULT],
4158 &verify_er_page_default,
4159 sizeof(verify_er_page_default));
4160 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_SAVED],
4161 &verify_er_page_default,
4162 sizeof(verify_er_page_default));
4163 page_index->page_data =
4164 (uint8_t *)lun->mode_pages.verify_er_page;
4167 case SMS_CACHING_PAGE: {
4168 struct scsi_caching_page *caching_page;
4170 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4171 ("subpage %#x for page %#x is incorrect!",
4172 page_index->subpage, page_code));
4173 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4174 &caching_page_default,
4175 sizeof(caching_page_default));
4176 memcpy(&lun->mode_pages.caching_page[
4177 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4178 sizeof(caching_page_changeable));
4179 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4180 &caching_page_default,
4181 sizeof(caching_page_default));
4182 caching_page = &lun->mode_pages.caching_page[
4184 value = ctl_get_opt(&lun->be_lun->options, "writecache");
4185 if (value != NULL && strcmp(value, "off") == 0)
4186 caching_page->flags1 &= ~SCP_WCE;
4187 value = ctl_get_opt(&lun->be_lun->options, "readcache");
4188 if (value != NULL && strcmp(value, "off") == 0)
4189 caching_page->flags1 |= SCP_RCD;
4190 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4191 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4192 sizeof(caching_page_default));
4193 page_index->page_data =
4194 (uint8_t *)lun->mode_pages.caching_page;
4197 case SMS_CONTROL_MODE_PAGE: {
4198 switch (page_index->subpage) {
4199 case SMS_SUBPAGE_PAGE_0: {
4200 struct scsi_control_page *control_page;
4202 memcpy(&lun->mode_pages.control_page[
4204 &control_page_default,
4205 sizeof(control_page_default));
4206 memcpy(&lun->mode_pages.control_page[
4207 CTL_PAGE_CHANGEABLE],
4208 &control_page_changeable,
4209 sizeof(control_page_changeable));
4210 memcpy(&lun->mode_pages.control_page[
4212 &control_page_default,
4213 sizeof(control_page_default));
4214 control_page = &lun->mode_pages.control_page[
4216 value = ctl_get_opt(&lun->be_lun->options,
4218 if (value != NULL &&
4219 strcmp(value, "unrestricted") == 0) {
4220 control_page->queue_flags &=
4221 ~SCP_QUEUE_ALG_MASK;
4222 control_page->queue_flags |=
4223 SCP_QUEUE_ALG_UNRESTRICTED;
4225 memcpy(&lun->mode_pages.control_page[
4227 &lun->mode_pages.control_page[
4229 sizeof(control_page_default));
4230 page_index->page_data =
4231 (uint8_t *)lun->mode_pages.control_page;
4235 memcpy(&lun->mode_pages.control_ext_page[
4237 &control_ext_page_default,
4238 sizeof(control_ext_page_default));
4239 memcpy(&lun->mode_pages.control_ext_page[
4240 CTL_PAGE_CHANGEABLE],
4241 &control_ext_page_changeable,
4242 sizeof(control_ext_page_changeable));
4243 memcpy(&lun->mode_pages.control_ext_page[
4245 &control_ext_page_default,
4246 sizeof(control_ext_page_default));
4247 memcpy(&lun->mode_pages.control_ext_page[
4249 &lun->mode_pages.control_ext_page[
4251 sizeof(control_ext_page_default));
4252 page_index->page_data =
4253 (uint8_t *)lun->mode_pages.control_ext_page;
4256 panic("subpage %#x for page %#x is incorrect!",
4257 page_index->subpage, page_code);
4261 case SMS_INFO_EXCEPTIONS_PAGE: {
4262 switch (page_index->subpage) {
4263 case SMS_SUBPAGE_PAGE_0:
4264 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4266 sizeof(ie_page_default));
4267 memcpy(&lun->mode_pages.ie_page[
4268 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4269 sizeof(ie_page_changeable));
4270 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4272 sizeof(ie_page_default));
4273 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4275 sizeof(ie_page_default));
4276 page_index->page_data =
4277 (uint8_t *)lun->mode_pages.ie_page;
4280 struct ctl_logical_block_provisioning_page *page;
4282 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4284 sizeof(lbp_page_default));
4285 memcpy(&lun->mode_pages.lbp_page[
4286 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4287 sizeof(lbp_page_changeable));
4288 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4290 sizeof(lbp_page_default));
4291 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4292 value = ctl_get_opt(&lun->be_lun->options,
4294 if (value != NULL &&
4295 ctl_expand_number(value, &ival) == 0) {
4296 page->descr[0].flags |= SLBPPD_ENABLED |
4298 if (lun->be_lun->blocksize)
4299 ival /= lun->be_lun->blocksize;
4302 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4303 page->descr[0].count);
4305 value = ctl_get_opt(&lun->be_lun->options,
4307 if (value != NULL &&
4308 ctl_expand_number(value, &ival) == 0) {
4309 page->descr[1].flags |= SLBPPD_ENABLED |
4311 if (lun->be_lun->blocksize)
4312 ival /= lun->be_lun->blocksize;
4315 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4316 page->descr[1].count);
4318 value = ctl_get_opt(&lun->be_lun->options,
4319 "pool-avail-threshold");
4320 if (value != NULL &&
4321 ctl_expand_number(value, &ival) == 0) {
4322 page->descr[2].flags |= SLBPPD_ENABLED |
4324 if (lun->be_lun->blocksize)
4325 ival /= lun->be_lun->blocksize;
4328 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4329 page->descr[2].count);
4331 value = ctl_get_opt(&lun->be_lun->options,
4332 "pool-used-threshold");
4333 if (value != NULL &&
4334 ctl_expand_number(value, &ival) == 0) {
4335 page->descr[3].flags |= SLBPPD_ENABLED |
4337 if (lun->be_lun->blocksize)
4338 ival /= lun->be_lun->blocksize;
4341 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4342 page->descr[3].count);
4344 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4345 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4346 sizeof(lbp_page_default));
4347 page_index->page_data =
4348 (uint8_t *)lun->mode_pages.lbp_page;
4352 panic("subpage %#x for page %#x is incorrect!",
4353 page_index->subpage, page_code);
4357 case SMS_CDDVD_CAPS_PAGE:{
4358 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4359 ("subpage %#x for page %#x is incorrect!",
4360 page_index->subpage, page_code));
4361 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT],
4362 &cddvd_page_default,
4363 sizeof(cddvd_page_default));
4364 memcpy(&lun->mode_pages.cddvd_page[
4365 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable,
4366 sizeof(cddvd_page_changeable));
4367 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4368 &cddvd_page_default,
4369 sizeof(cddvd_page_default));
4370 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT],
4371 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4372 sizeof(cddvd_page_default));
4373 page_index->page_data =
4374 (uint8_t *)lun->mode_pages.cddvd_page;
4378 panic("invalid page code value %#x", page_code);
4382 return (CTL_RETVAL_COMPLETE);
4386 ctl_init_log_page_index(struct ctl_lun *lun)
4388 struct ctl_page_index *page_index;
4391 memcpy(&lun->log_pages.index, log_page_index_template,
4392 sizeof(log_page_index_template));
4395 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4397 page_index = &lun->log_pages.index[i];
4398 if (lun->be_lun->lun_type == T_DIRECT &&
4399 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4401 if (lun->be_lun->lun_type == T_PROCESSOR &&
4402 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4404 if (lun->be_lun->lun_type == T_CDROM &&
4405 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4408 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4409 lun->backend->lun_attr == NULL)
4412 if (page_index->page_code != prev) {
4413 lun->log_pages.pages_page[j] = page_index->page_code;
4414 prev = page_index->page_code;
4417 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4418 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4421 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4422 lun->log_pages.index[0].page_len = j;
4423 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4424 lun->log_pages.index[1].page_len = k * 2;
4425 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4426 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4427 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page;
4428 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page);
4429 lun->log_pages.index[4].page_data = (uint8_t *)&lun->log_pages.ie_page;
4430 lun->log_pages.index[4].page_len = sizeof(lun->log_pages.ie_page);
4432 return (CTL_RETVAL_COMPLETE);
4436 hex2bin(const char *str, uint8_t *buf, int buf_size)
4441 memset(buf, 0, buf_size);
4442 while (isspace(str[0]))
4444 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4447 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4448 while (str[i] == '-') /* Skip dashes in UUIDs. */
4453 else if (isalpha(c))
4454 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4460 buf[i / 2] |= (c << 4);
4464 return ((i + 1) / 2);
4471 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4472 * wants us to allocate the LUN and he can block.
4473 * - ctl_softc is always set
4474 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4476 * Returns 0 for success, non-zero (errno) for failure.
4479 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4480 struct ctl_be_lun *const be_lun)
4482 struct ctl_lun *nlun, *lun;
4483 struct scsi_vpd_id_descriptor *desc;
4484 struct scsi_vpd_id_t10 *t10id;
4485 const char *eui, *naa, *scsiname, *uuid, *vendor, *value;
4486 int lun_number, lun_malloced;
4487 int devidlen, idlen1, idlen2 = 0, len;
4493 * We currently only support Direct Access or Processor LUN types.
4495 switch (be_lun->lun_type) {
4503 be_lun->lun_config_status(be_lun->be_lun,
4504 CTL_LUN_CONFIG_FAILURE);
4507 if (ctl_lun == NULL) {
4508 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4515 memset(lun, 0, sizeof(*lun));
4517 lun->flags = CTL_LUN_MALLOCED;
4519 /* Generate LUN ID. */
4520 devidlen = max(CTL_DEVID_MIN_LEN,
4521 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4522 idlen1 = sizeof(*t10id) + devidlen;
4523 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4524 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4525 if (scsiname != NULL) {
4526 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4527 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4529 eui = ctl_get_opt(&be_lun->options, "eui");
4531 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4533 naa = ctl_get_opt(&be_lun->options, "naa");
4535 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4537 uuid = ctl_get_opt(&be_lun->options, "uuid");
4539 len += sizeof(struct scsi_vpd_id_descriptor) + 18;
4541 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4542 M_CTL, M_WAITOK | M_ZERO);
4543 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4544 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4545 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4546 desc->length = idlen1;
4547 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4548 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4549 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4550 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4552 strncpy(t10id->vendor, vendor,
4553 min(sizeof(t10id->vendor), strlen(vendor)));
4555 strncpy((char *)t10id->vendor_spec_id,
4556 (char *)be_lun->device_id, devidlen);
4557 if (scsiname != NULL) {
4558 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4560 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4561 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4562 SVPD_ID_TYPE_SCSI_NAME;
4563 desc->length = idlen2;
4564 strlcpy(desc->identifier, scsiname, idlen2);
4567 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4569 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4570 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4572 desc->length = hex2bin(eui, desc->identifier, 16);
4573 desc->length = desc->length > 12 ? 16 :
4574 (desc->length > 8 ? 12 : 8);
4575 len -= 16 - desc->length;
4578 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4580 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4581 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4583 desc->length = hex2bin(naa, desc->identifier, 16);
4584 desc->length = desc->length > 8 ? 16 : 8;
4585 len -= 16 - desc->length;
4588 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4590 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4591 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4593 desc->identifier[0] = 0x10;
4594 hex2bin(uuid, &desc->identifier[2], 16);
4597 lun->lun_devid->len = len;
4599 mtx_lock(&ctl_softc->ctl_lock);
4601 * See if the caller requested a particular LUN number. If so, see
4602 * if it is available. Otherwise, allocate the first available LUN.
4604 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4605 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4606 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4607 mtx_unlock(&ctl_softc->ctl_lock);
4608 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4609 printf("ctl: requested LUN ID %d is higher "
4610 "than CTL_MAX_LUNS - 1 (%d)\n",
4611 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4614 * XXX KDM return an error, or just assign
4615 * another LUN ID in this case??
4617 printf("ctl: requested LUN ID %d is already "
4618 "in use\n", be_lun->req_lun_id);
4621 free(lun->lun_devid, M_CTL);
4622 if (lun->flags & CTL_LUN_MALLOCED)
4624 be_lun->lun_config_status(be_lun->be_lun,
4625 CTL_LUN_CONFIG_FAILURE);
4628 lun_number = be_lun->req_lun_id;
4630 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, CTL_MAX_LUNS);
4631 if (lun_number == -1) {
4632 mtx_unlock(&ctl_softc->ctl_lock);
4633 printf("ctl: can't allocate LUN, out of LUNs\n");
4637 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4638 mtx_unlock(&ctl_softc->ctl_lock);
4640 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4641 lun->lun = lun_number;
4642 lun->be_lun = be_lun;
4644 * The processor LUN is always enabled. Disk LUNs come on line
4645 * disabled, and must be enabled by the backend.
4647 lun->flags |= CTL_LUN_DISABLED;
4648 lun->backend = be_lun->be;
4649 be_lun->ctl_lun = lun;
4650 be_lun->lun_id = lun_number;
4651 atomic_add_int(&be_lun->be->num_luns, 1);
4652 if (be_lun->flags & CTL_LUN_FLAG_EJECTED)
4653 lun->flags |= CTL_LUN_EJECTED;
4654 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA)
4655 lun->flags |= CTL_LUN_NO_MEDIA;
4656 if (be_lun->flags & CTL_LUN_FLAG_STOPPED)
4657 lun->flags |= CTL_LUN_STOPPED;
4659 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4660 lun->flags |= CTL_LUN_PRIMARY_SC;
4662 value = ctl_get_opt(&be_lun->options, "removable");
4663 if (value != NULL) {
4664 if (strcmp(value, "on") == 0)
4665 lun->flags |= CTL_LUN_REMOVABLE;
4666 } else if (be_lun->lun_type == T_CDROM)
4667 lun->flags |= CTL_LUN_REMOVABLE;
4669 lun->ctl_softc = ctl_softc;
4671 lun->last_busy = getsbinuptime();
4673 TAILQ_INIT(&lun->ooa_queue);
4674 TAILQ_INIT(&lun->blocked_queue);
4675 STAILQ_INIT(&lun->error_list);
4676 lun->ie_reported = 1;
4677 callout_init_mtx(&lun->ie_callout, &lun->lun_lock, 0);
4678 ctl_tpc_lun_init(lun);
4679 if (lun->flags & CTL_LUN_REMOVABLE) {
4680 lun->prevent = malloc((CTL_MAX_INITIATORS + 31) / 32 * 4,
4685 * Initialize the mode and log page index.
4687 ctl_init_page_index(lun);
4688 ctl_init_log_page_index(lun);
4690 /* Setup statistics gathering */
4691 #ifdef CTL_LEGACY_STATS
4692 lun->legacy_stats.device_type = be_lun->lun_type;
4693 lun->legacy_stats.lun_number = lun_number;
4694 lun->legacy_stats.blocksize = be_lun->blocksize;
4695 if (be_lun->blocksize == 0)
4696 lun->legacy_stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4697 for (len = 0; len < CTL_MAX_PORTS; len++)
4698 lun->legacy_stats.ports[len].targ_port = len;
4699 #endif /* CTL_LEGACY_STATS */
4700 lun->stats.item = lun_number;
4703 * Now, before we insert this lun on the lun list, set the lun
4704 * inventory changed UA for all other luns.
4706 mtx_lock(&ctl_softc->ctl_lock);
4707 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4708 mtx_lock(&nlun->lun_lock);
4709 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4710 mtx_unlock(&nlun->lun_lock);
4712 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4713 ctl_softc->ctl_luns[lun_number] = lun;
4714 ctl_softc->num_luns++;
4715 mtx_unlock(&ctl_softc->ctl_lock);
4717 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4724 * - LUN has already been marked invalid and any pending I/O has been taken
4728 ctl_free_lun(struct ctl_lun *lun)
4730 struct ctl_softc *softc = lun->ctl_softc;
4731 struct ctl_lun *nlun;
4734 KASSERT(TAILQ_EMPTY(&lun->ooa_queue),
4735 ("Freeing a LUN %p with outstanding I/O!\n", lun));
4737 mtx_lock(&softc->ctl_lock);
4738 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4739 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4740 softc->ctl_luns[lun->lun] = NULL;
4742 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4743 mtx_lock(&nlun->lun_lock);
4744 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4745 mtx_unlock(&nlun->lun_lock);
4747 mtx_unlock(&softc->ctl_lock);
4750 * Tell the backend to free resources, if this LUN has a backend.
4752 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4753 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4755 lun->ie_reportcnt = UINT32_MAX;
4756 callout_drain(&lun->ie_callout);
4757 ctl_tpc_lun_shutdown(lun);
4758 mtx_destroy(&lun->lun_lock);
4759 free(lun->lun_devid, M_CTL);
4760 for (i = 0; i < CTL_MAX_PORTS; i++)
4761 free(lun->pending_ua[i], M_CTL);
4762 for (i = 0; i < CTL_MAX_PORTS; i++)
4763 free(lun->pr_keys[i], M_CTL);
4764 free(lun->write_buffer, M_CTL);
4765 free(lun->prevent, M_CTL);
4766 if (lun->flags & CTL_LUN_MALLOCED)
4773 ctl_create_lun(struct ctl_be_lun *be_lun)
4777 * ctl_alloc_lun() should handle all potential failure cases.
4779 ctl_alloc_lun(control_softc, NULL, be_lun);
4783 ctl_add_lun(struct ctl_be_lun *be_lun)
4785 struct ctl_softc *softc = control_softc;
4787 mtx_lock(&softc->ctl_lock);
4788 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links);
4789 mtx_unlock(&softc->ctl_lock);
4790 wakeup(&softc->pending_lun_queue);
4796 ctl_enable_lun(struct ctl_be_lun *be_lun)
4798 struct ctl_softc *softc;
4799 struct ctl_port *port, *nport;
4800 struct ctl_lun *lun;
4803 lun = (struct ctl_lun *)be_lun->ctl_lun;
4804 softc = lun->ctl_softc;
4806 mtx_lock(&softc->ctl_lock);
4807 mtx_lock(&lun->lun_lock);
4808 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4810 * eh? Why did we get called if the LUN is already
4813 mtx_unlock(&lun->lun_lock);
4814 mtx_unlock(&softc->ctl_lock);
4817 lun->flags &= ~CTL_LUN_DISABLED;
4818 mtx_unlock(&lun->lun_lock);
4820 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) {
4821 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4822 port->lun_map != NULL || port->lun_enable == NULL)
4826 * Drop the lock while we call the FETD's enable routine.
4827 * This can lead to a callback into CTL (at least in the
4828 * case of the internal initiator frontend.
4830 mtx_unlock(&softc->ctl_lock);
4831 retval = port->lun_enable(port->targ_lun_arg, lun->lun);
4832 mtx_lock(&softc->ctl_lock);
4834 printf("%s: FETD %s port %d returned error "
4835 "%d for lun_enable on lun %jd\n",
4836 __func__, port->port_name, port->targ_port,
4837 retval, (intmax_t)lun->lun);
4841 mtx_unlock(&softc->ctl_lock);
4842 ctl_isc_announce_lun(lun);
4848 ctl_disable_lun(struct ctl_be_lun *be_lun)
4850 struct ctl_softc *softc;
4851 struct ctl_port *port;
4852 struct ctl_lun *lun;
4855 lun = (struct ctl_lun *)be_lun->ctl_lun;
4856 softc = lun->ctl_softc;
4858 mtx_lock(&softc->ctl_lock);
4859 mtx_lock(&lun->lun_lock);
4860 if (lun->flags & CTL_LUN_DISABLED) {
4861 mtx_unlock(&lun->lun_lock);
4862 mtx_unlock(&softc->ctl_lock);
4865 lun->flags |= CTL_LUN_DISABLED;
4866 mtx_unlock(&lun->lun_lock);
4868 STAILQ_FOREACH(port, &softc->port_list, links) {
4869 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4870 port->lun_map != NULL || port->lun_disable == NULL)
4874 * Drop the lock before we call the frontend's disable
4875 * routine, to avoid lock order reversals.
4877 * XXX KDM what happens if the frontend list changes while
4878 * we're traversing it? It's unlikely, but should be handled.
4880 mtx_unlock(&softc->ctl_lock);
4881 retval = port->lun_disable(port->targ_lun_arg, lun->lun);
4882 mtx_lock(&softc->ctl_lock);
4884 printf("%s: FETD %s port %d returned error "
4885 "%d for lun_disable on lun %jd\n",
4886 __func__, port->port_name, port->targ_port,
4887 retval, (intmax_t)lun->lun);
4891 mtx_unlock(&softc->ctl_lock);
4892 ctl_isc_announce_lun(lun);
4898 ctl_start_lun(struct ctl_be_lun *be_lun)
4900 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4902 mtx_lock(&lun->lun_lock);
4903 lun->flags &= ~CTL_LUN_STOPPED;
4904 mtx_unlock(&lun->lun_lock);
4909 ctl_stop_lun(struct ctl_be_lun *be_lun)
4911 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4913 mtx_lock(&lun->lun_lock);
4914 lun->flags |= CTL_LUN_STOPPED;
4915 mtx_unlock(&lun->lun_lock);
4920 ctl_lun_no_media(struct ctl_be_lun *be_lun)
4922 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4924 mtx_lock(&lun->lun_lock);
4925 lun->flags |= CTL_LUN_NO_MEDIA;
4926 mtx_unlock(&lun->lun_lock);
4931 ctl_lun_has_media(struct ctl_be_lun *be_lun)
4933 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4934 union ctl_ha_msg msg;
4936 mtx_lock(&lun->lun_lock);
4937 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED);
4938 if (lun->flags & CTL_LUN_REMOVABLE)
4939 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE);
4940 mtx_unlock(&lun->lun_lock);
4941 if ((lun->flags & CTL_LUN_REMOVABLE) &&
4942 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4943 bzero(&msg.ua, sizeof(msg.ua));
4944 msg.hdr.msg_type = CTL_MSG_UA;
4945 msg.hdr.nexus.initid = -1;
4946 msg.hdr.nexus.targ_port = -1;
4947 msg.hdr.nexus.targ_lun = lun->lun;
4948 msg.hdr.nexus.targ_mapped_lun = lun->lun;
4951 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE;
4952 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4959 ctl_lun_ejected(struct ctl_be_lun *be_lun)
4961 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4963 mtx_lock(&lun->lun_lock);
4964 lun->flags |= CTL_LUN_EJECTED;
4965 mtx_unlock(&lun->lun_lock);
4970 ctl_lun_primary(struct ctl_be_lun *be_lun)
4972 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4974 mtx_lock(&lun->lun_lock);
4975 lun->flags |= CTL_LUN_PRIMARY_SC;
4976 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4977 mtx_unlock(&lun->lun_lock);
4978 ctl_isc_announce_lun(lun);
4983 ctl_lun_secondary(struct ctl_be_lun *be_lun)
4985 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4987 mtx_lock(&lun->lun_lock);
4988 lun->flags &= ~CTL_LUN_PRIMARY_SC;
4989 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4990 mtx_unlock(&lun->lun_lock);
4991 ctl_isc_announce_lun(lun);
4996 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4998 struct ctl_softc *softc;
4999 struct ctl_lun *lun;
5001 lun = (struct ctl_lun *)be_lun->ctl_lun;
5002 softc = lun->ctl_softc;
5004 mtx_lock(&lun->lun_lock);
5007 * The LUN needs to be disabled before it can be marked invalid.
5009 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
5010 mtx_unlock(&lun->lun_lock);
5014 * Mark the LUN invalid.
5016 lun->flags |= CTL_LUN_INVALID;
5019 * If there is nothing in the OOA queue, go ahead and free the LUN.
5020 * If we have something in the OOA queue, we'll free it when the
5021 * last I/O completes.
5023 if (TAILQ_EMPTY(&lun->ooa_queue)) {
5024 mtx_unlock(&lun->lun_lock);
5027 mtx_unlock(&lun->lun_lock);
5033 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
5035 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5036 union ctl_ha_msg msg;
5038 mtx_lock(&lun->lun_lock);
5039 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE);
5040 mtx_unlock(&lun->lun_lock);
5041 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
5042 /* Send msg to other side. */
5043 bzero(&msg.ua, sizeof(msg.ua));
5044 msg.hdr.msg_type = CTL_MSG_UA;
5045 msg.hdr.nexus.initid = -1;
5046 msg.hdr.nexus.targ_port = -1;
5047 msg.hdr.nexus.targ_lun = lun->lun;
5048 msg.hdr.nexus.targ_mapped_lun = lun->lun;
5051 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE;
5052 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
5058 * Backend "memory move is complete" callback for requests that never
5059 * make it down to say RAIDCore's configuration code.
5062 ctl_config_move_done(union ctl_io *io)
5066 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
5067 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
5068 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
5070 if ((io->io_hdr.port_status != 0) &&
5071 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5072 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5073 ctl_set_internal_failure(&io->scsiio, /*sks_valid*/ 1,
5074 /*retry_count*/ io->io_hdr.port_status);
5075 } else if (io->scsiio.kern_data_resid != 0 &&
5076 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT &&
5077 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5078 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5079 ctl_set_invalid_field_ciu(&io->scsiio);
5082 if (ctl_debug & CTL_DEBUG_CDB_DATA)
5084 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
5085 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5086 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
5087 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5089 * XXX KDM just assuming a single pointer here, and not a
5090 * S/G list. If we start using S/G lists for config data,
5091 * we'll need to know how to clean them up here as well.
5093 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5094 free(io->scsiio.kern_data_ptr, M_CTL);
5096 retval = CTL_RETVAL_COMPLETE;
5099 * XXX KDM now we need to continue data movement. Some
5101 * - call ctl_scsiio() again? We don't do this for data
5102 * writes, because for those at least we know ahead of
5103 * time where the write will go and how long it is. For
5104 * config writes, though, that information is largely
5105 * contained within the write itself, thus we need to
5106 * parse out the data again.
5108 * - Call some other function once the data is in?
5112 * XXX KDM call ctl_scsiio() again for now, and check flag
5113 * bits to see whether we're allocated or not.
5115 retval = ctl_scsiio(&io->scsiio);
5121 * This gets called by a backend driver when it is done with a
5122 * data_submit method.
5125 ctl_data_submit_done(union ctl_io *io)
5128 * If the IO_CONT flag is set, we need to call the supplied
5129 * function to continue processing the I/O, instead of completing
5132 * If there is an error, though, we don't want to keep processing.
5133 * Instead, just send status back to the initiator.
5135 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5136 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5137 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5138 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5139 io->scsiio.io_cont(io);
5146 * This gets called by a backend driver when it is done with a
5147 * configuration write.
5150 ctl_config_write_done(union ctl_io *io)
5155 * If the IO_CONT flag is set, we need to call the supplied
5156 * function to continue processing the I/O, instead of completing
5159 * If there is an error, though, we don't want to keep processing.
5160 * Instead, just send status back to the initiator.
5162 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5163 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5164 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5165 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5166 io->scsiio.io_cont(io);
5170 * Since a configuration write can be done for commands that actually
5171 * have data allocated, like write buffer, and commands that have
5172 * no data, like start/stop unit, we need to check here.
5174 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5175 buf = io->scsiio.kern_data_ptr;
5184 ctl_config_read_done(union ctl_io *io)
5189 * If there is some error -- we are done, skip data transfer.
5191 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
5192 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5193 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
5194 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5195 buf = io->scsiio.kern_data_ptr;
5205 * If the IO_CONT flag is set, we need to call the supplied
5206 * function to continue processing the I/O, instead of completing
5209 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
5210 io->scsiio.io_cont(io);
5218 * SCSI release command.
5221 ctl_scsi_release(struct ctl_scsiio *ctsio)
5223 struct ctl_lun *lun = CTL_LUN(ctsio);
5226 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5228 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5231 * XXX KDM right now, we only support LUN reservation. We don't
5232 * support 3rd party reservations, or extent reservations, which
5233 * might actually need the parameter list. If we've gotten this
5234 * far, we've got a LUN reservation. Anything else got kicked out
5235 * above. So, according to SPC, ignore the length.
5238 mtx_lock(&lun->lun_lock);
5241 * According to SPC, it is not an error for an intiator to attempt
5242 * to release a reservation on a LUN that isn't reserved, or that
5243 * is reserved by another initiator. The reservation can only be
5244 * released, though, by the initiator who made it or by one of
5245 * several reset type events.
5247 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5248 lun->flags &= ~CTL_LUN_RESERVED;
5250 mtx_unlock(&lun->lun_lock);
5252 ctl_set_success(ctsio);
5253 ctl_done((union ctl_io *)ctsio);
5254 return (CTL_RETVAL_COMPLETE);
5258 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5260 struct ctl_lun *lun = CTL_LUN(ctsio);
5263 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5265 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5268 * XXX KDM right now, we only support LUN reservation. We don't
5269 * support 3rd party reservations, or extent reservations, which
5270 * might actually need the parameter list. If we've gotten this
5271 * far, we've got a LUN reservation. Anything else got kicked out
5272 * above. So, according to SPC, ignore the length.
5275 mtx_lock(&lun->lun_lock);
5276 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5277 ctl_set_reservation_conflict(ctsio);
5281 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */
5282 if (lun->flags & CTL_LUN_PR_RESERVED) {
5283 ctl_set_success(ctsio);
5287 lun->flags |= CTL_LUN_RESERVED;
5288 lun->res_idx = residx;
5289 ctl_set_success(ctsio);
5292 mtx_unlock(&lun->lun_lock);
5293 ctl_done((union ctl_io *)ctsio);
5294 return (CTL_RETVAL_COMPLETE);
5298 ctl_start_stop(struct ctl_scsiio *ctsio)
5300 struct ctl_lun *lun = CTL_LUN(ctsio);
5301 struct scsi_start_stop_unit *cdb;
5304 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5306 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5308 if ((cdb->how & SSS_PC_MASK) == 0) {
5309 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
5310 (cdb->how & SSS_START) == 0) {
5313 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5314 if (ctl_get_prkey(lun, residx) == 0 ||
5315 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) {
5317 ctl_set_reservation_conflict(ctsio);
5318 ctl_done((union ctl_io *)ctsio);
5319 return (CTL_RETVAL_COMPLETE);
5323 if ((cdb->how & SSS_LOEJ) &&
5324 (lun->flags & CTL_LUN_REMOVABLE) == 0) {
5325 ctl_set_invalid_field(ctsio,
5331 ctl_done((union ctl_io *)ctsio);
5332 return (CTL_RETVAL_COMPLETE);
5335 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) &&
5336 lun->prevent_count > 0) {
5337 /* "Medium removal prevented" */
5338 ctl_set_sense(ctsio, /*current_error*/ 1,
5339 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ?
5340 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST,
5341 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE);
5342 ctl_done((union ctl_io *)ctsio);
5343 return (CTL_RETVAL_COMPLETE);
5347 retval = lun->backend->config_write((union ctl_io *)ctsio);
5352 ctl_prevent_allow(struct ctl_scsiio *ctsio)
5354 struct ctl_lun *lun = CTL_LUN(ctsio);
5355 struct scsi_prevent *cdb;
5359 CTL_DEBUG_PRINT(("ctl_prevent_allow\n"));
5361 cdb = (struct scsi_prevent *)ctsio->cdb;
5363 if ((lun->flags & CTL_LUN_REMOVABLE) == 0 || lun->prevent == NULL) {
5364 ctl_set_invalid_opcode(ctsio);
5365 ctl_done((union ctl_io *)ctsio);
5366 return (CTL_RETVAL_COMPLETE);
5369 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5370 mtx_lock(&lun->lun_lock);
5371 if ((cdb->how & PR_PREVENT) &&
5372 ctl_is_set(lun->prevent, initidx) == 0) {
5373 ctl_set_mask(lun->prevent, initidx);
5374 lun->prevent_count++;
5375 } else if ((cdb->how & PR_PREVENT) == 0 &&
5376 ctl_is_set(lun->prevent, initidx)) {
5377 ctl_clear_mask(lun->prevent, initidx);
5378 lun->prevent_count--;
5380 mtx_unlock(&lun->lun_lock);
5381 retval = lun->backend->config_write((union ctl_io *)ctsio);
5386 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5387 * we don't really do anything with the LBA and length fields if the user
5388 * passes them in. Instead we'll just flush out the cache for the entire
5392 ctl_sync_cache(struct ctl_scsiio *ctsio)
5394 struct ctl_lun *lun = CTL_LUN(ctsio);
5395 struct ctl_lba_len_flags *lbalen;
5396 uint64_t starting_lba;
5397 uint32_t block_count;
5401 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5405 switch (ctsio->cdb[0]) {
5406 case SYNCHRONIZE_CACHE: {
5407 struct scsi_sync_cache *cdb;
5408 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5410 starting_lba = scsi_4btoul(cdb->begin_lba);
5411 block_count = scsi_2btoul(cdb->lb_count);
5415 case SYNCHRONIZE_CACHE_16: {
5416 struct scsi_sync_cache_16 *cdb;
5417 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5419 starting_lba = scsi_8btou64(cdb->begin_lba);
5420 block_count = scsi_4btoul(cdb->lb_count);
5425 ctl_set_invalid_opcode(ctsio);
5426 ctl_done((union ctl_io *)ctsio);
5428 break; /* NOTREACHED */
5432 * We check the LBA and length, but don't do anything with them.
5433 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5434 * get flushed. This check will just help satisfy anyone who wants
5435 * to see an error for an out of range LBA.
5437 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5438 ctl_set_lba_out_of_range(ctsio,
5439 MAX(starting_lba, lun->be_lun->maxlba + 1));
5440 ctl_done((union ctl_io *)ctsio);
5444 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5445 lbalen->lba = starting_lba;
5446 lbalen->len = block_count;
5447 lbalen->flags = byte2;
5448 retval = lun->backend->config_write((union ctl_io *)ctsio);
5455 ctl_format(struct ctl_scsiio *ctsio)
5457 struct scsi_format *cdb;
5458 int length, defect_list_len;
5460 CTL_DEBUG_PRINT(("ctl_format\n"));
5462 cdb = (struct scsi_format *)ctsio->cdb;
5465 if (cdb->byte2 & SF_FMTDATA) {
5466 if (cdb->byte2 & SF_LONGLIST)
5467 length = sizeof(struct scsi_format_header_long);
5469 length = sizeof(struct scsi_format_header_short);
5472 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5474 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5475 ctsio->kern_data_len = length;
5476 ctsio->kern_total_len = length;
5477 ctsio->kern_rel_offset = 0;
5478 ctsio->kern_sg_entries = 0;
5479 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5480 ctsio->be_move_done = ctl_config_move_done;
5481 ctl_datamove((union ctl_io *)ctsio);
5483 return (CTL_RETVAL_COMPLETE);
5486 defect_list_len = 0;
5488 if (cdb->byte2 & SF_FMTDATA) {
5489 if (cdb->byte2 & SF_LONGLIST) {
5490 struct scsi_format_header_long *header;
5492 header = (struct scsi_format_header_long *)
5493 ctsio->kern_data_ptr;
5495 defect_list_len = scsi_4btoul(header->defect_list_len);
5496 if (defect_list_len != 0) {
5497 ctl_set_invalid_field(ctsio,
5506 struct scsi_format_header_short *header;
5508 header = (struct scsi_format_header_short *)
5509 ctsio->kern_data_ptr;
5511 defect_list_len = scsi_2btoul(header->defect_list_len);
5512 if (defect_list_len != 0) {
5513 ctl_set_invalid_field(ctsio,
5524 ctl_set_success(ctsio);
5527 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5528 free(ctsio->kern_data_ptr, M_CTL);
5529 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5532 ctl_done((union ctl_io *)ctsio);
5533 return (CTL_RETVAL_COMPLETE);
5537 ctl_read_buffer(struct ctl_scsiio *ctsio)
5539 struct ctl_lun *lun = CTL_LUN(ctsio);
5540 uint64_t buffer_offset;
5543 static uint8_t descr[4];
5544 static uint8_t echo_descr[4] = { 0 };
5546 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5548 switch (ctsio->cdb[0]) {
5550 struct scsi_read_buffer *cdb;
5552 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5553 buffer_offset = scsi_3btoul(cdb->offset);
5554 len = scsi_3btoul(cdb->length);
5558 case READ_BUFFER_16: {
5559 struct scsi_read_buffer_16 *cdb;
5561 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb;
5562 buffer_offset = scsi_8btou64(cdb->offset);
5563 len = scsi_4btoul(cdb->length);
5567 default: /* This shouldn't happen. */
5568 ctl_set_invalid_opcode(ctsio);
5569 ctl_done((union ctl_io *)ctsio);
5570 return (CTL_RETVAL_COMPLETE);
5573 if (buffer_offset > CTL_WRITE_BUFFER_SIZE ||
5574 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5575 ctl_set_invalid_field(ctsio,
5581 ctl_done((union ctl_io *)ctsio);
5582 return (CTL_RETVAL_COMPLETE);
5585 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5587 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5588 ctsio->kern_data_ptr = descr;
5589 len = min(len, sizeof(descr));
5590 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5591 ctsio->kern_data_ptr = echo_descr;
5592 len = min(len, sizeof(echo_descr));
5594 if (lun->write_buffer == NULL) {
5595 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5598 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5600 ctsio->kern_data_len = len;
5601 ctsio->kern_total_len = len;
5602 ctsio->kern_rel_offset = 0;
5603 ctsio->kern_sg_entries = 0;
5604 ctl_set_success(ctsio);
5605 ctsio->be_move_done = ctl_config_move_done;
5606 ctl_datamove((union ctl_io *)ctsio);
5607 return (CTL_RETVAL_COMPLETE);
5611 ctl_write_buffer(struct ctl_scsiio *ctsio)
5613 struct ctl_lun *lun = CTL_LUN(ctsio);
5614 struct scsi_write_buffer *cdb;
5615 int buffer_offset, len;
5617 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5619 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5621 len = scsi_3btoul(cdb->length);
5622 buffer_offset = scsi_3btoul(cdb->offset);
5624 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5625 ctl_set_invalid_field(ctsio,
5631 ctl_done((union ctl_io *)ctsio);
5632 return (CTL_RETVAL_COMPLETE);
5636 * If we've got a kernel request that hasn't been malloced yet,
5637 * malloc it and tell the caller the data buffer is here.
5639 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5640 if (lun->write_buffer == NULL) {
5641 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5644 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5645 ctsio->kern_data_len = len;
5646 ctsio->kern_total_len = len;
5647 ctsio->kern_rel_offset = 0;
5648 ctsio->kern_sg_entries = 0;
5649 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5650 ctsio->be_move_done = ctl_config_move_done;
5651 ctl_datamove((union ctl_io *)ctsio);
5653 return (CTL_RETVAL_COMPLETE);
5656 ctl_set_success(ctsio);
5657 ctl_done((union ctl_io *)ctsio);
5658 return (CTL_RETVAL_COMPLETE);
5662 ctl_write_same(struct ctl_scsiio *ctsio)
5664 struct ctl_lun *lun = CTL_LUN(ctsio);
5665 struct ctl_lba_len_flags *lbalen;
5667 uint32_t num_blocks;
5671 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5673 switch (ctsio->cdb[0]) {
5674 case WRITE_SAME_10: {
5675 struct scsi_write_same_10 *cdb;
5677 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5679 lba = scsi_4btoul(cdb->addr);
5680 num_blocks = scsi_2btoul(cdb->length);
5684 case WRITE_SAME_16: {
5685 struct scsi_write_same_16 *cdb;
5687 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5689 lba = scsi_8btou64(cdb->addr);
5690 num_blocks = scsi_4btoul(cdb->length);
5696 * We got a command we don't support. This shouldn't
5697 * happen, commands should be filtered out above us.
5699 ctl_set_invalid_opcode(ctsio);
5700 ctl_done((union ctl_io *)ctsio);
5702 return (CTL_RETVAL_COMPLETE);
5703 break; /* NOTREACHED */
5706 /* ANCHOR flag can be used only together with UNMAP */
5707 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) {
5708 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5709 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5710 ctl_done((union ctl_io *)ctsio);
5711 return (CTL_RETVAL_COMPLETE);
5715 * The first check is to make sure we're in bounds, the second
5716 * check is to catch wrap-around problems. If the lba + num blocks
5717 * is less than the lba, then we've wrapped around and the block
5718 * range is invalid anyway.
5720 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5721 || ((lba + num_blocks) < lba)) {
5722 ctl_set_lba_out_of_range(ctsio,
5723 MAX(lba, lun->be_lun->maxlba + 1));
5724 ctl_done((union ctl_io *)ctsio);
5725 return (CTL_RETVAL_COMPLETE);
5728 /* Zero number of blocks means "to the last logical block" */
5729 if (num_blocks == 0) {
5730 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5731 ctl_set_invalid_field(ctsio,
5737 ctl_done((union ctl_io *)ctsio);
5738 return (CTL_RETVAL_COMPLETE);
5740 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5743 len = lun->be_lun->blocksize;
5746 * If we've got a kernel request that hasn't been malloced yet,
5747 * malloc it and tell the caller the data buffer is here.
5749 if ((byte2 & SWS_NDOB) == 0 &&
5750 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5751 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5752 ctsio->kern_data_len = len;
5753 ctsio->kern_total_len = len;
5754 ctsio->kern_rel_offset = 0;
5755 ctsio->kern_sg_entries = 0;
5756 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5757 ctsio->be_move_done = ctl_config_move_done;
5758 ctl_datamove((union ctl_io *)ctsio);
5760 return (CTL_RETVAL_COMPLETE);
5763 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5765 lbalen->len = num_blocks;
5766 lbalen->flags = byte2;
5767 retval = lun->backend->config_write((union ctl_io *)ctsio);
5773 ctl_unmap(struct ctl_scsiio *ctsio)
5775 struct ctl_lun *lun = CTL_LUN(ctsio);
5776 struct scsi_unmap *cdb;
5777 struct ctl_ptr_len_flags *ptrlen;
5778 struct scsi_unmap_header *hdr;
5779 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5781 uint32_t num_blocks;
5785 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5787 cdb = (struct scsi_unmap *)ctsio->cdb;
5788 len = scsi_2btoul(cdb->length);
5792 * If we've got a kernel request that hasn't been malloced yet,
5793 * malloc it and tell the caller the data buffer is here.
5795 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5796 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5797 ctsio->kern_data_len = len;
5798 ctsio->kern_total_len = len;
5799 ctsio->kern_rel_offset = 0;
5800 ctsio->kern_sg_entries = 0;
5801 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5802 ctsio->be_move_done = ctl_config_move_done;
5803 ctl_datamove((union ctl_io *)ctsio);
5805 return (CTL_RETVAL_COMPLETE);
5808 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5809 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5810 if (len < sizeof (*hdr) ||
5811 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5812 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5813 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5814 ctl_set_invalid_field(ctsio,
5822 len = scsi_2btoul(hdr->desc_length);
5823 buf = (struct scsi_unmap_desc *)(hdr + 1);
5824 end = buf + len / sizeof(*buf);
5827 for (range = buf; range < end; range++) {
5828 lba = scsi_8btou64(range->lba);
5829 num_blocks = scsi_4btoul(range->length);
5830 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5831 || ((lba + num_blocks) < lba)) {
5832 ctl_set_lba_out_of_range(ctsio,
5833 MAX(lba, lun->be_lun->maxlba + 1));
5834 ctl_done((union ctl_io *)ctsio);
5835 return (CTL_RETVAL_COMPLETE);
5837 if (num_blocks != 0)
5842 * Block backend can not handle zero last range.
5843 * Filter it out and return if there is nothing left.
5845 len = (uint8_t *)endnz - (uint8_t *)buf;
5847 ctl_set_success(ctsio);
5851 mtx_lock(&lun->lun_lock);
5852 ptrlen = (struct ctl_ptr_len_flags *)
5853 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5854 ptrlen->ptr = (void *)buf;
5856 ptrlen->flags = byte2;
5857 ctl_check_blocked(lun);
5858 mtx_unlock(&lun->lun_lock);
5860 retval = lun->backend->config_write((union ctl_io *)ctsio);
5864 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5865 free(ctsio->kern_data_ptr, M_CTL);
5866 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5868 ctl_done((union ctl_io *)ctsio);
5869 return (CTL_RETVAL_COMPLETE);
5873 ctl_default_page_handler(struct ctl_scsiio *ctsio,
5874 struct ctl_page_index *page_index, uint8_t *page_ptr)
5876 struct ctl_lun *lun = CTL_LUN(ctsio);
5877 uint8_t *current_cp;
5881 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5884 current_cp = (page_index->page_data + (page_index->page_len *
5887 mtx_lock(&lun->lun_lock);
5888 if (memcmp(current_cp, page_ptr, page_index->page_len)) {
5889 memcpy(current_cp, page_ptr, page_index->page_len);
5893 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5894 mtx_unlock(&lun->lun_lock);
5896 ctl_isc_announce_mode(lun,
5897 ctl_get_initindex(&ctsio->io_hdr.nexus),
5898 page_index->page_code, page_index->subpage);
5900 return (CTL_RETVAL_COMPLETE);
5904 ctl_ie_timer(void *arg)
5906 struct ctl_lun *lun = arg;
5909 if (lun->ie_asc == 0)
5912 if (lun->MODE_IE.mrie == SIEP_MRIE_UA)
5913 ctl_est_ua_all(lun, -1, CTL_UA_IE);
5915 lun->ie_reported = 0;
5917 if (lun->ie_reportcnt < scsi_4btoul(lun->MODE_IE.report_count)) {
5918 lun->ie_reportcnt++;
5919 t = scsi_4btoul(lun->MODE_IE.interval_timer);
5920 if (t == 0 || t == UINT32_MAX)
5921 t = 3000; /* 5 min */
5922 callout_schedule(&lun->ie_callout, t * hz / 10);
5927 ctl_ie_page_handler(struct ctl_scsiio *ctsio,
5928 struct ctl_page_index *page_index, uint8_t *page_ptr)
5930 struct ctl_lun *lun = CTL_LUN(ctsio);
5931 struct scsi_info_exceptions_page *pg;
5934 (void)ctl_default_page_handler(ctsio, page_index, page_ptr);
5936 pg = (struct scsi_info_exceptions_page *)page_ptr;
5937 mtx_lock(&lun->lun_lock);
5938 if (pg->info_flags & SIEP_FLAGS_TEST) {
5940 lun->ie_ascq = 0xff;
5941 if (pg->mrie == SIEP_MRIE_UA) {
5942 ctl_est_ua_all(lun, -1, CTL_UA_IE);
5943 lun->ie_reported = 1;
5945 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
5946 lun->ie_reported = -1;
5948 lun->ie_reportcnt = 1;
5949 if (lun->ie_reportcnt < scsi_4btoul(pg->report_count)) {
5950 lun->ie_reportcnt++;
5951 t = scsi_4btoul(pg->interval_timer);
5952 if (t == 0 || t == UINT32_MAX)
5953 t = 3000; /* 5 min */
5954 callout_reset(&lun->ie_callout, t * hz / 10,
5960 lun->ie_reported = 1;
5961 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
5962 lun->ie_reportcnt = UINT32_MAX;
5963 callout_stop(&lun->ie_callout);
5965 mtx_unlock(&lun->lun_lock);
5966 return (CTL_RETVAL_COMPLETE);
5970 ctl_do_mode_select(union ctl_io *io)
5972 struct ctl_lun *lun = CTL_LUN(io);
5973 struct scsi_mode_page_header *page_header;
5974 struct ctl_page_index *page_index;
5975 struct ctl_scsiio *ctsio;
5976 int page_len, page_len_offset, page_len_size;
5977 union ctl_modepage_info *modepage_info;
5978 uint16_t *len_left, *len_used;
5981 ctsio = &io->scsiio;
5985 modepage_info = (union ctl_modepage_info *)
5986 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
5987 len_left = &modepage_info->header.len_left;
5988 len_used = &modepage_info->header.len_used;
5992 page_header = (struct scsi_mode_page_header *)
5993 (ctsio->kern_data_ptr + *len_used);
5995 if (*len_left == 0) {
5996 free(ctsio->kern_data_ptr, M_CTL);
5997 ctl_set_success(ctsio);
5998 ctl_done((union ctl_io *)ctsio);
5999 return (CTL_RETVAL_COMPLETE);
6000 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6002 free(ctsio->kern_data_ptr, M_CTL);
6003 ctl_set_param_len_error(ctsio);
6004 ctl_done((union ctl_io *)ctsio);
6005 return (CTL_RETVAL_COMPLETE);
6007 } else if ((page_header->page_code & SMPH_SPF)
6008 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6010 free(ctsio->kern_data_ptr, M_CTL);
6011 ctl_set_param_len_error(ctsio);
6012 ctl_done((union ctl_io *)ctsio);
6013 return (CTL_RETVAL_COMPLETE);
6018 * XXX KDM should we do something with the block descriptor?
6020 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6021 page_index = &lun->mode_pages.index[i];
6022 if (lun->be_lun->lun_type == T_DIRECT &&
6023 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6025 if (lun->be_lun->lun_type == T_PROCESSOR &&
6026 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6028 if (lun->be_lun->lun_type == T_CDROM &&
6029 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6032 if ((page_index->page_code & SMPH_PC_MASK) !=
6033 (page_header->page_code & SMPH_PC_MASK))
6037 * If neither page has a subpage code, then we've got a
6040 if (((page_index->page_code & SMPH_SPF) == 0)
6041 && ((page_header->page_code & SMPH_SPF) == 0)) {
6042 page_len = page_header->page_length;
6047 * If both pages have subpages, then the subpage numbers
6050 if ((page_index->page_code & SMPH_SPF)
6051 && (page_header->page_code & SMPH_SPF)) {
6052 struct scsi_mode_page_header_sp *sph;
6054 sph = (struct scsi_mode_page_header_sp *)page_header;
6055 if (page_index->subpage == sph->subpage) {
6056 page_len = scsi_2btoul(sph->page_length);
6063 * If we couldn't find the page, or if we don't have a mode select
6064 * handler for it, send back an error to the user.
6066 if ((i >= CTL_NUM_MODE_PAGES)
6067 || (page_index->select_handler == NULL)) {
6068 ctl_set_invalid_field(ctsio,
6071 /*field*/ *len_used,
6074 free(ctsio->kern_data_ptr, M_CTL);
6075 ctl_done((union ctl_io *)ctsio);
6076 return (CTL_RETVAL_COMPLETE);
6079 if (page_index->page_code & SMPH_SPF) {
6080 page_len_offset = 2;
6084 page_len_offset = 1;
6088 * If the length the initiator gives us isn't the one we specify in
6089 * the mode page header, or if they didn't specify enough data in
6090 * the CDB to avoid truncating this page, kick out the request.
6092 if (page_len != page_index->page_len - page_len_offset - page_len_size) {
6093 ctl_set_invalid_field(ctsio,
6096 /*field*/ *len_used + page_len_offset,
6099 free(ctsio->kern_data_ptr, M_CTL);
6100 ctl_done((union ctl_io *)ctsio);
6101 return (CTL_RETVAL_COMPLETE);
6103 if (*len_left < page_index->page_len) {
6104 free(ctsio->kern_data_ptr, M_CTL);
6105 ctl_set_param_len_error(ctsio);
6106 ctl_done((union ctl_io *)ctsio);
6107 return (CTL_RETVAL_COMPLETE);
6111 * Run through the mode page, checking to make sure that the bits
6112 * the user changed are actually legal for him to change.
6114 for (i = 0; i < page_index->page_len; i++) {
6115 uint8_t *user_byte, *change_mask, *current_byte;
6119 user_byte = (uint8_t *)page_header + i;
6120 change_mask = page_index->page_data +
6121 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6122 current_byte = page_index->page_data +
6123 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6126 * Check to see whether the user set any bits in this byte
6127 * that he is not allowed to set.
6129 if ((*user_byte & ~(*change_mask)) ==
6130 (*current_byte & ~(*change_mask)))
6134 * Go through bit by bit to determine which one is illegal.
6137 for (j = 7; j >= 0; j--) {
6138 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6139 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6144 ctl_set_invalid_field(ctsio,
6147 /*field*/ *len_used + i,
6150 free(ctsio->kern_data_ptr, M_CTL);
6151 ctl_done((union ctl_io *)ctsio);
6152 return (CTL_RETVAL_COMPLETE);
6156 * Decrement these before we call the page handler, since we may
6157 * end up getting called back one way or another before the handler
6158 * returns to this context.
6160 *len_left -= page_index->page_len;
6161 *len_used += page_index->page_len;
6163 retval = page_index->select_handler(ctsio, page_index,
6164 (uint8_t *)page_header);
6167 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6168 * wait until this queued command completes to finish processing
6169 * the mode page. If it returns anything other than
6170 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6171 * already set the sense information, freed the data pointer, and
6172 * completed the io for us.
6174 if (retval != CTL_RETVAL_COMPLETE)
6175 goto bailout_no_done;
6178 * If the initiator sent us more than one page, parse the next one.
6183 ctl_set_success(ctsio);
6184 free(ctsio->kern_data_ptr, M_CTL);
6185 ctl_done((union ctl_io *)ctsio);
6189 return (CTL_RETVAL_COMPLETE);
6194 ctl_mode_select(struct ctl_scsiio *ctsio)
6196 struct ctl_lun *lun = CTL_LUN(ctsio);
6197 union ctl_modepage_info *modepage_info;
6198 int bd_len, i, header_size, param_len, pf, rtd, sp;
6201 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6202 switch (ctsio->cdb[0]) {
6203 case MODE_SELECT_6: {
6204 struct scsi_mode_select_6 *cdb;
6206 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6208 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6209 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6210 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6211 param_len = cdb->length;
6212 header_size = sizeof(struct scsi_mode_header_6);
6215 case MODE_SELECT_10: {
6216 struct scsi_mode_select_10 *cdb;
6218 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6220 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6221 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6222 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6223 param_len = scsi_2btoul(cdb->length);
6224 header_size = sizeof(struct scsi_mode_header_10);
6228 ctl_set_invalid_opcode(ctsio);
6229 ctl_done((union ctl_io *)ctsio);
6230 return (CTL_RETVAL_COMPLETE);
6234 if (param_len != 0) {
6235 ctl_set_invalid_field(ctsio, /*sks_valid*/ 0,
6236 /*command*/ 1, /*field*/ 0,
6237 /*bit_valid*/ 0, /*bit*/ 0);
6238 ctl_done((union ctl_io *)ctsio);
6239 return (CTL_RETVAL_COMPLETE);
6242 /* Revert to defaults. */
6243 ctl_init_page_index(lun);
6244 mtx_lock(&lun->lun_lock);
6245 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
6246 mtx_unlock(&lun->lun_lock);
6247 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6248 ctl_isc_announce_mode(lun, -1,
6249 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
6250 lun->mode_pages.index[i].subpage);
6252 ctl_set_success(ctsio);
6253 ctl_done((union ctl_io *)ctsio);
6254 return (CTL_RETVAL_COMPLETE);
6259 * "A parameter list length of zero indicates that the Data-Out Buffer
6260 * shall be empty. This condition shall not be considered as an error."
6262 if (param_len == 0) {
6263 ctl_set_success(ctsio);
6264 ctl_done((union ctl_io *)ctsio);
6265 return (CTL_RETVAL_COMPLETE);
6269 * Since we'll hit this the first time through, prior to
6270 * allocation, we don't need to free a data buffer here.
6272 if (param_len < header_size) {
6273 ctl_set_param_len_error(ctsio);
6274 ctl_done((union ctl_io *)ctsio);
6275 return (CTL_RETVAL_COMPLETE);
6279 * Allocate the data buffer and grab the user's data. In theory,
6280 * we shouldn't have to sanity check the parameter list length here
6281 * because the maximum size is 64K. We should be able to malloc
6282 * that much without too many problems.
6284 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6285 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6286 ctsio->kern_data_len = param_len;
6287 ctsio->kern_total_len = param_len;
6288 ctsio->kern_rel_offset = 0;
6289 ctsio->kern_sg_entries = 0;
6290 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6291 ctsio->be_move_done = ctl_config_move_done;
6292 ctl_datamove((union ctl_io *)ctsio);
6294 return (CTL_RETVAL_COMPLETE);
6297 switch (ctsio->cdb[0]) {
6298 case MODE_SELECT_6: {
6299 struct scsi_mode_header_6 *mh6;
6301 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6302 bd_len = mh6->blk_desc_len;
6305 case MODE_SELECT_10: {
6306 struct scsi_mode_header_10 *mh10;
6308 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6309 bd_len = scsi_2btoul(mh10->blk_desc_len);
6313 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6316 if (param_len < (header_size + bd_len)) {
6317 free(ctsio->kern_data_ptr, M_CTL);
6318 ctl_set_param_len_error(ctsio);
6319 ctl_done((union ctl_io *)ctsio);
6320 return (CTL_RETVAL_COMPLETE);
6324 * Set the IO_CONT flag, so that if this I/O gets passed to
6325 * ctl_config_write_done(), it'll get passed back to
6326 * ctl_do_mode_select() for further processing, or completion if
6329 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6330 ctsio->io_cont = ctl_do_mode_select;
6332 modepage_info = (union ctl_modepage_info *)
6333 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6334 memset(modepage_info, 0, sizeof(*modepage_info));
6335 modepage_info->header.len_left = param_len - header_size - bd_len;
6336 modepage_info->header.len_used = header_size + bd_len;
6338 return (ctl_do_mode_select((union ctl_io *)ctsio));
6342 ctl_mode_sense(struct ctl_scsiio *ctsio)
6344 struct ctl_lun *lun = CTL_LUN(ctsio);
6345 int pc, page_code, dbd, llba, subpage;
6346 int alloc_len, page_len, header_len, total_len;
6347 struct scsi_mode_block_descr *block_desc;
6348 struct ctl_page_index *page_index;
6354 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6356 switch (ctsio->cdb[0]) {
6357 case MODE_SENSE_6: {
6358 struct scsi_mode_sense_6 *cdb;
6360 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6362 header_len = sizeof(struct scsi_mode_hdr_6);
6363 if (cdb->byte2 & SMS_DBD)
6366 header_len += sizeof(struct scsi_mode_block_descr);
6368 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6369 page_code = cdb->page & SMS_PAGE_CODE;
6370 subpage = cdb->subpage;
6371 alloc_len = cdb->length;
6374 case MODE_SENSE_10: {
6375 struct scsi_mode_sense_10 *cdb;
6377 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6379 header_len = sizeof(struct scsi_mode_hdr_10);
6381 if (cdb->byte2 & SMS_DBD)
6384 header_len += sizeof(struct scsi_mode_block_descr);
6385 if (cdb->byte2 & SMS10_LLBAA)
6387 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6388 page_code = cdb->page & SMS_PAGE_CODE;
6389 subpage = cdb->subpage;
6390 alloc_len = scsi_2btoul(cdb->length);
6394 ctl_set_invalid_opcode(ctsio);
6395 ctl_done((union ctl_io *)ctsio);
6396 return (CTL_RETVAL_COMPLETE);
6397 break; /* NOTREACHED */
6401 * We have to make a first pass through to calculate the size of
6402 * the pages that match the user's query. Then we allocate enough
6403 * memory to hold it, and actually copy the data into the buffer.
6405 switch (page_code) {
6406 case SMS_ALL_PAGES_PAGE: {
6412 * At the moment, values other than 0 and 0xff here are
6413 * reserved according to SPC-3.
6415 if ((subpage != SMS_SUBPAGE_PAGE_0)
6416 && (subpage != SMS_SUBPAGE_ALL)) {
6417 ctl_set_invalid_field(ctsio,
6423 ctl_done((union ctl_io *)ctsio);
6424 return (CTL_RETVAL_COMPLETE);
6427 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6428 page_index = &lun->mode_pages.index[i];
6430 /* Make sure the page is supported for this dev type */
6431 if (lun->be_lun->lun_type == T_DIRECT &&
6432 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6434 if (lun->be_lun->lun_type == T_PROCESSOR &&
6435 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6437 if (lun->be_lun->lun_type == T_CDROM &&
6438 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6442 * We don't use this subpage if the user didn't
6443 * request all subpages.
6445 if ((page_index->subpage != 0)
6446 && (subpage == SMS_SUBPAGE_PAGE_0))
6450 printf("found page %#x len %d\n",
6451 page_index->page_code & SMPH_PC_MASK,
6452 page_index->page_len);
6454 page_len += page_index->page_len;
6463 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6464 page_index = &lun->mode_pages.index[i];
6466 /* Make sure the page is supported for this dev type */
6467 if (lun->be_lun->lun_type == T_DIRECT &&
6468 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6470 if (lun->be_lun->lun_type == T_PROCESSOR &&
6471 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6473 if (lun->be_lun->lun_type == T_CDROM &&
6474 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6477 /* Look for the right page code */
6478 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6481 /* Look for the right subpage or the subpage wildcard*/
6482 if ((page_index->subpage != subpage)
6483 && (subpage != SMS_SUBPAGE_ALL))
6487 printf("found page %#x len %d\n",
6488 page_index->page_code & SMPH_PC_MASK,
6489 page_index->page_len);
6492 page_len += page_index->page_len;
6495 if (page_len == 0) {
6496 ctl_set_invalid_field(ctsio,
6502 ctl_done((union ctl_io *)ctsio);
6503 return (CTL_RETVAL_COMPLETE);
6509 total_len = header_len + page_len;
6511 printf("header_len = %d, page_len = %d, total_len = %d\n",
6512 header_len, page_len, total_len);
6515 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6516 ctsio->kern_sg_entries = 0;
6517 ctsio->kern_rel_offset = 0;
6518 ctsio->kern_data_len = min(total_len, alloc_len);
6519 ctsio->kern_total_len = ctsio->kern_data_len;
6521 switch (ctsio->cdb[0]) {
6522 case MODE_SENSE_6: {
6523 struct scsi_mode_hdr_6 *header;
6525 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6527 header->datalen = MIN(total_len - 1, 254);
6528 if (lun->be_lun->lun_type == T_DIRECT) {
6529 header->dev_specific = 0x10; /* DPOFUA */
6530 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6531 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6532 header->dev_specific |= 0x80; /* WP */
6535 header->block_descr_len = 0;
6537 header->block_descr_len =
6538 sizeof(struct scsi_mode_block_descr);
6539 block_desc = (struct scsi_mode_block_descr *)&header[1];
6542 case MODE_SENSE_10: {
6543 struct scsi_mode_hdr_10 *header;
6546 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6548 datalen = MIN(total_len - 2, 65533);
6549 scsi_ulto2b(datalen, header->datalen);
6550 if (lun->be_lun->lun_type == T_DIRECT) {
6551 header->dev_specific = 0x10; /* DPOFUA */
6552 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6553 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6554 header->dev_specific |= 0x80; /* WP */
6557 scsi_ulto2b(0, header->block_descr_len);
6559 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6560 header->block_descr_len);
6561 block_desc = (struct scsi_mode_block_descr *)&header[1];
6565 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6569 * If we've got a disk, use its blocksize in the block
6570 * descriptor. Otherwise, just set it to 0.
6573 if (lun->be_lun->lun_type == T_DIRECT)
6574 scsi_ulto3b(lun->be_lun->blocksize,
6575 block_desc->block_len);
6577 scsi_ulto3b(0, block_desc->block_len);
6580 switch (page_code) {
6581 case SMS_ALL_PAGES_PAGE: {
6584 data_used = header_len;
6585 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6586 struct ctl_page_index *page_index;
6588 page_index = &lun->mode_pages.index[i];
6589 if (lun->be_lun->lun_type == T_DIRECT &&
6590 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6592 if (lun->be_lun->lun_type == T_PROCESSOR &&
6593 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6595 if (lun->be_lun->lun_type == T_CDROM &&
6596 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6600 * We don't use this subpage if the user didn't
6601 * request all subpages. We already checked (above)
6602 * to make sure the user only specified a subpage
6603 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6605 if ((page_index->subpage != 0)
6606 && (subpage == SMS_SUBPAGE_PAGE_0))
6610 * Call the handler, if it exists, to update the
6611 * page to the latest values.
6613 if (page_index->sense_handler != NULL)
6614 page_index->sense_handler(ctsio, page_index,pc);
6616 memcpy(ctsio->kern_data_ptr + data_used,
6617 page_index->page_data +
6618 (page_index->page_len * pc),
6619 page_index->page_len);
6620 data_used += page_index->page_len;
6627 data_used = header_len;
6629 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6630 struct ctl_page_index *page_index;
6632 page_index = &lun->mode_pages.index[i];
6634 /* Look for the right page code */
6635 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6638 /* Look for the right subpage or the subpage wildcard*/
6639 if ((page_index->subpage != subpage)
6640 && (subpage != SMS_SUBPAGE_ALL))
6643 /* Make sure the page is supported for this dev type */
6644 if (lun->be_lun->lun_type == T_DIRECT &&
6645 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6647 if (lun->be_lun->lun_type == T_PROCESSOR &&
6648 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6650 if (lun->be_lun->lun_type == T_CDROM &&
6651 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6655 * Call the handler, if it exists, to update the
6656 * page to the latest values.
6658 if (page_index->sense_handler != NULL)
6659 page_index->sense_handler(ctsio, page_index,pc);
6661 memcpy(ctsio->kern_data_ptr + data_used,
6662 page_index->page_data +
6663 (page_index->page_len * pc),
6664 page_index->page_len);
6665 data_used += page_index->page_len;
6671 ctl_set_success(ctsio);
6672 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6673 ctsio->be_move_done = ctl_config_move_done;
6674 ctl_datamove((union ctl_io *)ctsio);
6675 return (CTL_RETVAL_COMPLETE);
6679 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6680 struct ctl_page_index *page_index,
6683 struct ctl_lun *lun = CTL_LUN(ctsio);
6684 struct scsi_log_param_header *phdr;
6688 data = page_index->page_data;
6690 if (lun->backend->lun_attr != NULL &&
6691 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6693 phdr = (struct scsi_log_param_header *)data;
6694 scsi_ulto2b(0x0001, phdr->param_code);
6695 phdr->param_control = SLP_LBIN | SLP_LP;
6696 phdr->param_len = 8;
6697 data = (uint8_t *)(phdr + 1);
6698 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6699 data[4] = 0x02; /* per-pool */
6700 data += phdr->param_len;
6703 if (lun->backend->lun_attr != NULL &&
6704 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6706 phdr = (struct scsi_log_param_header *)data;
6707 scsi_ulto2b(0x0002, phdr->param_code);
6708 phdr->param_control = SLP_LBIN | SLP_LP;
6709 phdr->param_len = 8;
6710 data = (uint8_t *)(phdr + 1);
6711 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6712 data[4] = 0x01; /* per-LUN */
6713 data += phdr->param_len;
6716 if (lun->backend->lun_attr != NULL &&
6717 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6719 phdr = (struct scsi_log_param_header *)data;
6720 scsi_ulto2b(0x00f1, phdr->param_code);
6721 phdr->param_control = SLP_LBIN | SLP_LP;
6722 phdr->param_len = 8;
6723 data = (uint8_t *)(phdr + 1);
6724 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6725 data[4] = 0x02; /* per-pool */
6726 data += phdr->param_len;
6729 if (lun->backend->lun_attr != NULL &&
6730 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6732 phdr = (struct scsi_log_param_header *)data;
6733 scsi_ulto2b(0x00f2, phdr->param_code);
6734 phdr->param_control = SLP_LBIN | SLP_LP;
6735 phdr->param_len = 8;
6736 data = (uint8_t *)(phdr + 1);
6737 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6738 data[4] = 0x02; /* per-pool */
6739 data += phdr->param_len;
6742 page_index->page_len = data - page_index->page_data;
6747 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
6748 struct ctl_page_index *page_index,
6751 struct ctl_lun *lun = CTL_LUN(ctsio);
6752 struct stat_page *data;
6755 data = (struct stat_page *)page_index->page_data;
6757 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
6758 data->sap.hdr.param_control = SLP_LBIN;
6759 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
6760 sizeof(struct scsi_log_param_header);
6761 scsi_u64to8b(lun->stats.operations[CTL_STATS_READ],
6762 data->sap.read_num);
6763 scsi_u64to8b(lun->stats.operations[CTL_STATS_WRITE],
6764 data->sap.write_num);
6765 if (lun->be_lun->blocksize > 0) {
6766 scsi_u64to8b(lun->stats.bytes[CTL_STATS_WRITE] /
6767 lun->be_lun->blocksize, data->sap.recvieved_lba);
6768 scsi_u64to8b(lun->stats.bytes[CTL_STATS_READ] /
6769 lun->be_lun->blocksize, data->sap.transmitted_lba);
6771 t = &lun->stats.time[CTL_STATS_READ];
6772 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000),
6773 data->sap.read_int);
6774 t = &lun->stats.time[CTL_STATS_WRITE];
6775 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000),
6776 data->sap.write_int);
6777 scsi_u64to8b(0, data->sap.weighted_num);
6778 scsi_u64to8b(0, data->sap.weighted_int);
6779 scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
6780 data->it.hdr.param_control = SLP_LBIN;
6781 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
6782 sizeof(struct scsi_log_param_header);
6784 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
6786 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
6787 data->it.hdr.param_control = SLP_LBIN;
6788 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
6789 sizeof(struct scsi_log_param_header);
6790 scsi_ulto4b(3, data->ti.exponent);
6791 scsi_ulto4b(1, data->ti.integer);
6796 ctl_ie_log_sense_handler(struct ctl_scsiio *ctsio,
6797 struct ctl_page_index *page_index,
6800 struct ctl_lun *lun = CTL_LUN(ctsio);
6801 struct scsi_log_informational_exceptions *data;
6803 data = (struct scsi_log_informational_exceptions *)page_index->page_data;
6805 scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code);
6806 data->hdr.param_control = SLP_LBIN;
6807 data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) -
6808 sizeof(struct scsi_log_param_header);
6809 data->ie_asc = lun->ie_asc;
6810 data->ie_ascq = lun->ie_ascq;
6811 data->temperature = 0xff;
6816 ctl_log_sense(struct ctl_scsiio *ctsio)
6818 struct ctl_lun *lun = CTL_LUN(ctsio);
6819 int i, pc, page_code, subpage;
6820 int alloc_len, total_len;
6821 struct ctl_page_index *page_index;
6822 struct scsi_log_sense *cdb;
6823 struct scsi_log_header *header;
6825 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6827 cdb = (struct scsi_log_sense *)ctsio->cdb;
6828 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6829 page_code = cdb->page & SLS_PAGE_CODE;
6830 subpage = cdb->subpage;
6831 alloc_len = scsi_2btoul(cdb->length);
6834 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6835 page_index = &lun->log_pages.index[i];
6837 /* Look for the right page code */
6838 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6841 /* Look for the right subpage or the subpage wildcard*/
6842 if (page_index->subpage != subpage)
6847 if (i >= CTL_NUM_LOG_PAGES) {
6848 ctl_set_invalid_field(ctsio,
6854 ctl_done((union ctl_io *)ctsio);
6855 return (CTL_RETVAL_COMPLETE);
6858 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6860 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6861 ctsio->kern_sg_entries = 0;
6862 ctsio->kern_rel_offset = 0;
6863 ctsio->kern_data_len = min(total_len, alloc_len);
6864 ctsio->kern_total_len = ctsio->kern_data_len;
6866 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
6867 header->page = page_index->page_code;
6868 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING)
6869 header->page |= SL_DS;
6870 if (page_index->subpage) {
6871 header->page |= SL_SPF;
6872 header->subpage = page_index->subpage;
6874 scsi_ulto2b(page_index->page_len, header->datalen);
6877 * Call the handler, if it exists, to update the
6878 * page to the latest values.
6880 if (page_index->sense_handler != NULL)
6881 page_index->sense_handler(ctsio, page_index, pc);
6883 memcpy(header + 1, page_index->page_data, page_index->page_len);
6885 ctl_set_success(ctsio);
6886 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6887 ctsio->be_move_done = ctl_config_move_done;
6888 ctl_datamove((union ctl_io *)ctsio);
6889 return (CTL_RETVAL_COMPLETE);
6893 ctl_read_capacity(struct ctl_scsiio *ctsio)
6895 struct ctl_lun *lun = CTL_LUN(ctsio);
6896 struct scsi_read_capacity *cdb;
6897 struct scsi_read_capacity_data *data;
6900 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6902 cdb = (struct scsi_read_capacity *)ctsio->cdb;
6904 lba = scsi_4btoul(cdb->addr);
6905 if (((cdb->pmi & SRC_PMI) == 0)
6907 ctl_set_invalid_field(/*ctsio*/ ctsio,
6913 ctl_done((union ctl_io *)ctsio);
6914 return (CTL_RETVAL_COMPLETE);
6917 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6918 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6919 ctsio->kern_data_len = sizeof(*data);
6920 ctsio->kern_total_len = sizeof(*data);
6921 ctsio->kern_rel_offset = 0;
6922 ctsio->kern_sg_entries = 0;
6925 * If the maximum LBA is greater than 0xfffffffe, the user must
6926 * issue a SERVICE ACTION IN (16) command, with the read capacity
6927 * serivce action set.
6929 if (lun->be_lun->maxlba > 0xfffffffe)
6930 scsi_ulto4b(0xffffffff, data->addr);
6932 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6935 * XXX KDM this may not be 512 bytes...
6937 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6939 ctl_set_success(ctsio);
6940 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6941 ctsio->be_move_done = ctl_config_move_done;
6942 ctl_datamove((union ctl_io *)ctsio);
6943 return (CTL_RETVAL_COMPLETE);
6947 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
6949 struct ctl_lun *lun = CTL_LUN(ctsio);
6950 struct scsi_read_capacity_16 *cdb;
6951 struct scsi_read_capacity_data_long *data;
6955 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
6957 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
6959 alloc_len = scsi_4btoul(cdb->alloc_len);
6960 lba = scsi_8btou64(cdb->addr);
6962 if ((cdb->reladr & SRC16_PMI)
6964 ctl_set_invalid_field(/*ctsio*/ ctsio,
6970 ctl_done((union ctl_io *)ctsio);
6971 return (CTL_RETVAL_COMPLETE);
6974 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6975 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
6976 ctsio->kern_rel_offset = 0;
6977 ctsio->kern_sg_entries = 0;
6978 ctsio->kern_data_len = min(sizeof(*data), alloc_len);
6979 ctsio->kern_total_len = ctsio->kern_data_len;
6981 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
6982 /* XXX KDM this may not be 512 bytes... */
6983 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6984 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
6985 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
6986 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
6987 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
6989 ctl_set_success(ctsio);
6990 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6991 ctsio->be_move_done = ctl_config_move_done;
6992 ctl_datamove((union ctl_io *)ctsio);
6993 return (CTL_RETVAL_COMPLETE);
6997 ctl_get_lba_status(struct ctl_scsiio *ctsio)
6999 struct ctl_lun *lun = CTL_LUN(ctsio);
7000 struct scsi_get_lba_status *cdb;
7001 struct scsi_get_lba_status_data *data;
7002 struct ctl_lba_len_flags *lbalen;
7004 uint32_t alloc_len, total_len;
7007 CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
7009 cdb = (struct scsi_get_lba_status *)ctsio->cdb;
7010 lba = scsi_8btou64(cdb->addr);
7011 alloc_len = scsi_4btoul(cdb->alloc_len);
7013 if (lba > lun->be_lun->maxlba) {
7014 ctl_set_lba_out_of_range(ctsio, lba);
7015 ctl_done((union ctl_io *)ctsio);
7016 return (CTL_RETVAL_COMPLETE);
7019 total_len = sizeof(*data) + sizeof(data->descr[0]);
7020 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7021 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
7022 ctsio->kern_rel_offset = 0;
7023 ctsio->kern_sg_entries = 0;
7024 ctsio->kern_data_len = min(total_len, alloc_len);
7025 ctsio->kern_total_len = ctsio->kern_data_len;
7027 /* Fill dummy data in case backend can't tell anything. */
7028 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
7029 scsi_u64to8b(lba, data->descr[0].addr);
7030 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
7031 data->descr[0].length);
7032 data->descr[0].status = 0; /* Mapped or unknown. */
7034 ctl_set_success(ctsio);
7035 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7036 ctsio->be_move_done = ctl_config_move_done;
7038 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
7040 lbalen->len = total_len;
7042 retval = lun->backend->config_read((union ctl_io *)ctsio);
7047 ctl_read_defect(struct ctl_scsiio *ctsio)
7049 struct scsi_read_defect_data_10 *ccb10;
7050 struct scsi_read_defect_data_12 *ccb12;
7051 struct scsi_read_defect_data_hdr_10 *data10;
7052 struct scsi_read_defect_data_hdr_12 *data12;
7053 uint32_t alloc_len, data_len;
7056 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7058 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7059 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7060 format = ccb10->format;
7061 alloc_len = scsi_2btoul(ccb10->alloc_length);
7062 data_len = sizeof(*data10);
7064 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7065 format = ccb12->format;
7066 alloc_len = scsi_4btoul(ccb12->alloc_length);
7067 data_len = sizeof(*data12);
7069 if (alloc_len == 0) {
7070 ctl_set_success(ctsio);
7071 ctl_done((union ctl_io *)ctsio);
7072 return (CTL_RETVAL_COMPLETE);
7075 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7076 ctsio->kern_rel_offset = 0;
7077 ctsio->kern_sg_entries = 0;
7078 ctsio->kern_data_len = min(data_len, alloc_len);
7079 ctsio->kern_total_len = ctsio->kern_data_len;
7081 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7082 data10 = (struct scsi_read_defect_data_hdr_10 *)
7083 ctsio->kern_data_ptr;
7084 data10->format = format;
7085 scsi_ulto2b(0, data10->length);
7087 data12 = (struct scsi_read_defect_data_hdr_12 *)
7088 ctsio->kern_data_ptr;
7089 data12->format = format;
7090 scsi_ulto2b(0, data12->generation);
7091 scsi_ulto4b(0, data12->length);
7094 ctl_set_success(ctsio);
7095 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7096 ctsio->be_move_done = ctl_config_move_done;
7097 ctl_datamove((union ctl_io *)ctsio);
7098 return (CTL_RETVAL_COMPLETE);
7102 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7104 struct ctl_softc *softc = CTL_SOFTC(ctsio);
7105 struct ctl_lun *lun = CTL_LUN(ctsio);
7106 struct scsi_maintenance_in *cdb;
7108 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os;
7109 int num_ha_groups, num_target_ports, shared_group;
7110 struct ctl_port *port;
7111 struct scsi_target_group_data *rtg_ptr;
7112 struct scsi_target_group_data_extended *rtg_ext_ptr;
7113 struct scsi_target_port_group_descriptor *tpg_desc;
7115 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7117 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7118 retval = CTL_RETVAL_COMPLETE;
7120 switch (cdb->byte2 & STG_PDF_MASK) {
7121 case STG_PDF_LENGTH:
7124 case STG_PDF_EXTENDED:
7128 ctl_set_invalid_field(/*ctsio*/ ctsio,
7134 ctl_done((union ctl_io *)ctsio);
7138 num_target_ports = 0;
7139 shared_group = (softc->is_single != 0);
7140 mtx_lock(&softc->ctl_lock);
7141 STAILQ_FOREACH(port, &softc->port_list, links) {
7142 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7144 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7147 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7150 mtx_unlock(&softc->ctl_lock);
7151 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES;
7154 total_len = sizeof(struct scsi_target_group_data_extended);
7156 total_len = sizeof(struct scsi_target_group_data);
7157 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7158 (shared_group + num_ha_groups) +
7159 sizeof(struct scsi_target_port_descriptor) * num_target_ports;
7161 alloc_len = scsi_4btoul(cdb->length);
7163 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7164 ctsio->kern_sg_entries = 0;
7165 ctsio->kern_rel_offset = 0;
7166 ctsio->kern_data_len = min(total_len, alloc_len);
7167 ctsio->kern_total_len = ctsio->kern_data_len;
7170 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7171 ctsio->kern_data_ptr;
7172 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7173 rtg_ext_ptr->format_type = 0x10;
7174 rtg_ext_ptr->implicit_transition_time = 0;
7175 tpg_desc = &rtg_ext_ptr->groups[0];
7177 rtg_ptr = (struct scsi_target_group_data *)
7178 ctsio->kern_data_ptr;
7179 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7180 tpg_desc = &rtg_ptr->groups[0];
7183 mtx_lock(&softc->ctl_lock);
7184 pg = softc->port_min / softc->port_cnt;
7185 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) {
7186 /* Some shelf is known to be primary. */
7187 if (softc->ha_link == CTL_HA_LINK_OFFLINE)
7188 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7189 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN)
7190 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7191 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY)
7192 os = TPG_ASYMMETRIC_ACCESS_STANDBY;
7194 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7195 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7196 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7199 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7202 /* No known primary shelf. */
7203 if (softc->ha_link == CTL_HA_LINK_OFFLINE) {
7204 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7205 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7206 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) {
7207 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7208 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7210 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7214 tpg_desc->pref_state = ts;
7215 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7216 TPG_U_SUP | TPG_T_SUP;
7217 scsi_ulto2b(1, tpg_desc->target_port_group);
7218 tpg_desc->status = TPG_IMPLICIT;
7220 STAILQ_FOREACH(port, &softc->port_list, links) {
7221 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7223 if (!softc->is_single &&
7224 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0)
7226 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7228 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7229 relative_target_port_identifier);
7232 tpg_desc->target_port_count = pc;
7233 tpg_desc = (struct scsi_target_port_group_descriptor *)
7234 &tpg_desc->descriptors[pc];
7236 for (g = 0; g < num_ha_groups; g++) {
7237 tpg_desc->pref_state = (g == pg) ? ts : os;
7238 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7239 TPG_U_SUP | TPG_T_SUP;
7240 scsi_ulto2b(2 + g, tpg_desc->target_port_group);
7241 tpg_desc->status = TPG_IMPLICIT;
7243 STAILQ_FOREACH(port, &softc->port_list, links) {
7244 if (port->targ_port < g * softc->port_cnt ||
7245 port->targ_port >= (g + 1) * softc->port_cnt)
7247 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7249 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7251 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7253 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7254 relative_target_port_identifier);
7257 tpg_desc->target_port_count = pc;
7258 tpg_desc = (struct scsi_target_port_group_descriptor *)
7259 &tpg_desc->descriptors[pc];
7261 mtx_unlock(&softc->ctl_lock);
7263 ctl_set_success(ctsio);
7264 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7265 ctsio->be_move_done = ctl_config_move_done;
7266 ctl_datamove((union ctl_io *)ctsio);
7271 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7273 struct ctl_lun *lun = CTL_LUN(ctsio);
7274 struct scsi_report_supported_opcodes *cdb;
7275 const struct ctl_cmd_entry *entry, *sentry;
7276 struct scsi_report_supported_opcodes_all *all;
7277 struct scsi_report_supported_opcodes_descr *descr;
7278 struct scsi_report_supported_opcodes_one *one;
7280 int alloc_len, total_len;
7281 int opcode, service_action, i, j, num;
7283 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7285 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7286 retval = CTL_RETVAL_COMPLETE;
7288 opcode = cdb->requested_opcode;
7289 service_action = scsi_2btoul(cdb->requested_service_action);
7290 switch (cdb->options & RSO_OPTIONS_MASK) {
7291 case RSO_OPTIONS_ALL:
7293 for (i = 0; i < 256; i++) {
7294 entry = &ctl_cmd_table[i];
7295 if (entry->flags & CTL_CMD_FLAG_SA5) {
7296 for (j = 0; j < 32; j++) {
7297 sentry = &((const struct ctl_cmd_entry *)
7299 if (ctl_cmd_applicable(
7300 lun->be_lun->lun_type, sentry))
7304 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7309 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7310 num * sizeof(struct scsi_report_supported_opcodes_descr);
7312 case RSO_OPTIONS_OC:
7313 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7314 ctl_set_invalid_field(/*ctsio*/ ctsio,
7320 ctl_done((union ctl_io *)ctsio);
7321 return (CTL_RETVAL_COMPLETE);
7323 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7325 case RSO_OPTIONS_OC_SA:
7326 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7327 service_action >= 32) {
7328 ctl_set_invalid_field(/*ctsio*/ ctsio,
7334 ctl_done((union ctl_io *)ctsio);
7335 return (CTL_RETVAL_COMPLETE);
7338 case RSO_OPTIONS_OC_ASA:
7339 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7342 ctl_set_invalid_field(/*ctsio*/ ctsio,
7348 ctl_done((union ctl_io *)ctsio);
7349 return (CTL_RETVAL_COMPLETE);
7352 alloc_len = scsi_4btoul(cdb->length);
7354 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7355 ctsio->kern_sg_entries = 0;
7356 ctsio->kern_rel_offset = 0;
7357 ctsio->kern_data_len = min(total_len, alloc_len);
7358 ctsio->kern_total_len = ctsio->kern_data_len;
7360 switch (cdb->options & RSO_OPTIONS_MASK) {
7361 case RSO_OPTIONS_ALL:
7362 all = (struct scsi_report_supported_opcodes_all *)
7363 ctsio->kern_data_ptr;
7365 for (i = 0; i < 256; i++) {
7366 entry = &ctl_cmd_table[i];
7367 if (entry->flags & CTL_CMD_FLAG_SA5) {
7368 for (j = 0; j < 32; j++) {
7369 sentry = &((const struct ctl_cmd_entry *)
7371 if (!ctl_cmd_applicable(
7372 lun->be_lun->lun_type, sentry))
7374 descr = &all->descr[num++];
7376 scsi_ulto2b(j, descr->service_action);
7377 descr->flags = RSO_SERVACTV;
7378 scsi_ulto2b(sentry->length,
7382 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7385 descr = &all->descr[num++];
7387 scsi_ulto2b(0, descr->service_action);
7389 scsi_ulto2b(entry->length, descr->cdb_length);
7393 num * sizeof(struct scsi_report_supported_opcodes_descr),
7396 case RSO_OPTIONS_OC:
7397 one = (struct scsi_report_supported_opcodes_one *)
7398 ctsio->kern_data_ptr;
7399 entry = &ctl_cmd_table[opcode];
7401 case RSO_OPTIONS_OC_SA:
7402 one = (struct scsi_report_supported_opcodes_one *)
7403 ctsio->kern_data_ptr;
7404 entry = &ctl_cmd_table[opcode];
7405 entry = &((const struct ctl_cmd_entry *)
7406 entry->execute)[service_action];
7408 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7410 scsi_ulto2b(entry->length, one->cdb_length);
7411 one->cdb_usage[0] = opcode;
7412 memcpy(&one->cdb_usage[1], entry->usage,
7417 case RSO_OPTIONS_OC_ASA:
7418 one = (struct scsi_report_supported_opcodes_one *)
7419 ctsio->kern_data_ptr;
7420 entry = &ctl_cmd_table[opcode];
7421 if (entry->flags & CTL_CMD_FLAG_SA5) {
7422 entry = &((const struct ctl_cmd_entry *)
7423 entry->execute)[service_action];
7424 } else if (service_action != 0) {
7431 ctl_set_success(ctsio);
7432 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7433 ctsio->be_move_done = ctl_config_move_done;
7434 ctl_datamove((union ctl_io *)ctsio);
7439 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7441 struct scsi_report_supported_tmf *cdb;
7442 struct scsi_report_supported_tmf_ext_data *data;
7444 int alloc_len, total_len;
7446 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7448 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7450 retval = CTL_RETVAL_COMPLETE;
7452 if (cdb->options & RST_REPD)
7453 total_len = sizeof(struct scsi_report_supported_tmf_ext_data);
7455 total_len = sizeof(struct scsi_report_supported_tmf_data);
7456 alloc_len = scsi_4btoul(cdb->length);
7458 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7459 ctsio->kern_sg_entries = 0;
7460 ctsio->kern_rel_offset = 0;
7461 ctsio->kern_data_len = min(total_len, alloc_len);
7462 ctsio->kern_total_len = ctsio->kern_data_len;
7464 data = (struct scsi_report_supported_tmf_ext_data *)ctsio->kern_data_ptr;
7465 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS |
7467 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS;
7468 data->length = total_len - 4;
7470 ctl_set_success(ctsio);
7471 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7472 ctsio->be_move_done = ctl_config_move_done;
7473 ctl_datamove((union ctl_io *)ctsio);
7478 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7480 struct scsi_report_timestamp *cdb;
7481 struct scsi_report_timestamp_data *data;
7485 int alloc_len, total_len;
7487 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7489 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7491 retval = CTL_RETVAL_COMPLETE;
7493 total_len = sizeof(struct scsi_report_timestamp_data);
7494 alloc_len = scsi_4btoul(cdb->length);
7496 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7497 ctsio->kern_sg_entries = 0;
7498 ctsio->kern_rel_offset = 0;
7499 ctsio->kern_data_len = min(total_len, alloc_len);
7500 ctsio->kern_total_len = ctsio->kern_data_len;
7502 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7503 scsi_ulto2b(sizeof(*data) - 2, data->length);
7504 data->origin = RTS_ORIG_OUTSIDE;
7506 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7507 scsi_ulto4b(timestamp >> 16, data->timestamp);
7508 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7510 ctl_set_success(ctsio);
7511 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7512 ctsio->be_move_done = ctl_config_move_done;
7513 ctl_datamove((union ctl_io *)ctsio);
7518 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7520 struct ctl_softc *softc = CTL_SOFTC(ctsio);
7521 struct ctl_lun *lun = CTL_LUN(ctsio);
7522 struct scsi_per_res_in *cdb;
7523 int alloc_len, total_len = 0;
7524 /* struct scsi_per_res_in_rsrv in_data; */
7527 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7529 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7531 alloc_len = scsi_2btoul(cdb->length);
7534 mtx_lock(&lun->lun_lock);
7535 switch (cdb->action) {
7536 case SPRI_RK: /* read keys */
7537 total_len = sizeof(struct scsi_per_res_in_keys) +
7539 sizeof(struct scsi_per_res_key);
7541 case SPRI_RR: /* read reservation */
7542 if (lun->flags & CTL_LUN_PR_RESERVED)
7543 total_len = sizeof(struct scsi_per_res_in_rsrv);
7545 total_len = sizeof(struct scsi_per_res_in_header);
7547 case SPRI_RC: /* report capabilities */
7548 total_len = sizeof(struct scsi_per_res_cap);
7550 case SPRI_RS: /* read full status */
7551 total_len = sizeof(struct scsi_per_res_in_header) +
7552 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7556 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7558 mtx_unlock(&lun->lun_lock);
7560 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7561 ctsio->kern_rel_offset = 0;
7562 ctsio->kern_sg_entries = 0;
7563 ctsio->kern_data_len = min(total_len, alloc_len);
7564 ctsio->kern_total_len = ctsio->kern_data_len;
7566 mtx_lock(&lun->lun_lock);
7567 switch (cdb->action) {
7568 case SPRI_RK: { // read keys
7569 struct scsi_per_res_in_keys *res_keys;
7572 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7575 * We had to drop the lock to allocate our buffer, which
7576 * leaves time for someone to come in with another
7577 * persistent reservation. (That is unlikely, though,
7578 * since this should be the only persistent reservation
7579 * command active right now.)
7581 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7582 (lun->pr_key_count *
7583 sizeof(struct scsi_per_res_key)))){
7584 mtx_unlock(&lun->lun_lock);
7585 free(ctsio->kern_data_ptr, M_CTL);
7586 printf("%s: reservation length changed, retrying\n",
7591 scsi_ulto4b(lun->pr_generation, res_keys->header.generation);
7593 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7594 lun->pr_key_count, res_keys->header.length);
7596 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) {
7597 if ((key = ctl_get_prkey(lun, i)) == 0)
7601 * We used lun->pr_key_count to calculate the
7602 * size to allocate. If it turns out the number of
7603 * initiators with the registered flag set is
7604 * larger than that (i.e. they haven't been kept in
7605 * sync), we've got a problem.
7607 if (key_count >= lun->pr_key_count) {
7611 scsi_u64to8b(key, res_keys->keys[key_count].key);
7616 case SPRI_RR: { // read reservation
7617 struct scsi_per_res_in_rsrv *res;
7618 int tmp_len, header_only;
7620 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7622 scsi_ulto4b(lun->pr_generation, res->header.generation);
7624 if (lun->flags & CTL_LUN_PR_RESERVED)
7626 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7627 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7628 res->header.length);
7631 tmp_len = sizeof(struct scsi_per_res_in_header);
7632 scsi_ulto4b(0, res->header.length);
7637 * We had to drop the lock to allocate our buffer, which
7638 * leaves time for someone to come in with another
7639 * persistent reservation. (That is unlikely, though,
7640 * since this should be the only persistent reservation
7641 * command active right now.)
7643 if (tmp_len != total_len) {
7644 mtx_unlock(&lun->lun_lock);
7645 free(ctsio->kern_data_ptr, M_CTL);
7646 printf("%s: reservation status changed, retrying\n",
7652 * No reservation held, so we're done.
7654 if (header_only != 0)
7658 * If the registration is an All Registrants type, the key
7659 * is 0, since it doesn't really matter.
7661 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7662 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7663 res->data.reservation);
7665 res->data.scopetype = lun->pr_res_type;
7668 case SPRI_RC: //report capabilities
7670 struct scsi_per_res_cap *res_cap;
7673 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7674 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7675 res_cap->flags1 = SPRI_CRH;
7676 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5;
7677 type_mask = SPRI_TM_WR_EX_AR |
7683 scsi_ulto2b(type_mask, res_cap->type_mask);
7686 case SPRI_RS: { // read full status
7687 struct scsi_per_res_in_full *res_status;
7688 struct scsi_per_res_in_full_desc *res_desc;
7689 struct ctl_port *port;
7692 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7695 * We had to drop the lock to allocate our buffer, which
7696 * leaves time for someone to come in with another
7697 * persistent reservation. (That is unlikely, though,
7698 * since this should be the only persistent reservation
7699 * command active right now.)
7701 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7702 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7703 lun->pr_key_count)){
7704 mtx_unlock(&lun->lun_lock);
7705 free(ctsio->kern_data_ptr, M_CTL);
7706 printf("%s: reservation length changed, retrying\n",
7711 scsi_ulto4b(lun->pr_generation, res_status->header.generation);
7713 res_desc = &res_status->desc[0];
7714 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7715 if ((key = ctl_get_prkey(lun, i)) == 0)
7718 scsi_u64to8b(key, res_desc->res_key.key);
7719 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7720 (lun->pr_res_idx == i ||
7721 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7722 res_desc->flags = SPRI_FULL_R_HOLDER;
7723 res_desc->scopetype = lun->pr_res_type;
7725 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7726 res_desc->rel_trgt_port_id);
7728 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7730 len = ctl_create_iid(port,
7731 i % CTL_MAX_INIT_PER_PORT,
7732 res_desc->transport_id);
7733 scsi_ulto4b(len, res_desc->additional_length);
7734 res_desc = (struct scsi_per_res_in_full_desc *)
7735 &res_desc->transport_id[len];
7737 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7738 res_status->header.length);
7742 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7744 mtx_unlock(&lun->lun_lock);
7746 ctl_set_success(ctsio);
7747 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7748 ctsio->be_move_done = ctl_config_move_done;
7749 ctl_datamove((union ctl_io *)ctsio);
7750 return (CTL_RETVAL_COMPLETE);
7754 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7758 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7759 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7760 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7761 struct scsi_per_res_out_parms* param)
7763 union ctl_ha_msg persis_io;
7766 mtx_lock(&lun->lun_lock);
7767 if (sa_res_key == 0) {
7768 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7769 /* validate scope and type */
7770 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7772 mtx_unlock(&lun->lun_lock);
7773 ctl_set_invalid_field(/*ctsio*/ ctsio,
7779 ctl_done((union ctl_io *)ctsio);
7783 if (type>8 || type==2 || type==4 || type==0) {
7784 mtx_unlock(&lun->lun_lock);
7785 ctl_set_invalid_field(/*ctsio*/ ctsio,
7791 ctl_done((union ctl_io *)ctsio);
7796 * Unregister everybody else and build UA for
7799 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7800 if (i == residx || ctl_get_prkey(lun, i) == 0)
7803 ctl_clr_prkey(lun, i);
7804 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7806 lun->pr_key_count = 1;
7807 lun->pr_res_type = type;
7808 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7809 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7810 lun->pr_res_idx = residx;
7811 lun->pr_generation++;
7812 mtx_unlock(&lun->lun_lock);
7814 /* send msg to other side */
7815 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7816 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7817 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7818 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7819 persis_io.pr.pr_info.res_type = type;
7820 memcpy(persis_io.pr.pr_info.sa_res_key,
7821 param->serv_act_res_key,
7822 sizeof(param->serv_act_res_key));
7823 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7824 sizeof(persis_io.pr), M_WAITOK);
7826 /* not all registrants */
7827 mtx_unlock(&lun->lun_lock);
7828 free(ctsio->kern_data_ptr, M_CTL);
7829 ctl_set_invalid_field(ctsio,
7835 ctl_done((union ctl_io *)ctsio);
7838 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7839 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7842 if (res_key == sa_res_key) {
7845 * The spec implies this is not good but doesn't
7846 * say what to do. There are two choices either
7847 * generate a res conflict or check condition
7848 * with illegal field in parameter data. Since
7849 * that is what is done when the sa_res_key is
7850 * zero I'll take that approach since this has
7851 * to do with the sa_res_key.
7853 mtx_unlock(&lun->lun_lock);
7854 free(ctsio->kern_data_ptr, M_CTL);
7855 ctl_set_invalid_field(ctsio,
7861 ctl_done((union ctl_io *)ctsio);
7865 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7866 if (ctl_get_prkey(lun, i) != sa_res_key)
7870 ctl_clr_prkey(lun, i);
7871 lun->pr_key_count--;
7872 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7875 mtx_unlock(&lun->lun_lock);
7876 free(ctsio->kern_data_ptr, M_CTL);
7877 ctl_set_reservation_conflict(ctsio);
7878 ctl_done((union ctl_io *)ctsio);
7879 return (CTL_RETVAL_COMPLETE);
7881 lun->pr_generation++;
7882 mtx_unlock(&lun->lun_lock);
7884 /* send msg to other side */
7885 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7886 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7887 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7888 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7889 persis_io.pr.pr_info.res_type = type;
7890 memcpy(persis_io.pr.pr_info.sa_res_key,
7891 param->serv_act_res_key,
7892 sizeof(param->serv_act_res_key));
7893 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7894 sizeof(persis_io.pr), M_WAITOK);
7896 /* Reserved but not all registrants */
7897 /* sa_res_key is res holder */
7898 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
7899 /* validate scope and type */
7900 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7902 mtx_unlock(&lun->lun_lock);
7903 ctl_set_invalid_field(/*ctsio*/ ctsio,
7909 ctl_done((union ctl_io *)ctsio);
7913 if (type>8 || type==2 || type==4 || type==0) {
7914 mtx_unlock(&lun->lun_lock);
7915 ctl_set_invalid_field(/*ctsio*/ ctsio,
7921 ctl_done((union ctl_io *)ctsio);
7927 * if sa_res_key != res_key remove all
7928 * registrants w/sa_res_key and generate UA
7929 * for these registrants(Registrations
7930 * Preempted) if it wasn't an exclusive
7931 * reservation generate UA(Reservations
7932 * Preempted) for all other registered nexuses
7933 * if the type has changed. Establish the new
7934 * reservation and holder. If res_key and
7935 * sa_res_key are the same do the above
7936 * except don't unregister the res holder.
7939 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7940 if (i == residx || ctl_get_prkey(lun, i) == 0)
7943 if (sa_res_key == ctl_get_prkey(lun, i)) {
7944 ctl_clr_prkey(lun, i);
7945 lun->pr_key_count--;
7946 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7947 } else if (type != lun->pr_res_type &&
7948 (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
7949 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
7950 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
7953 lun->pr_res_type = type;
7954 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7955 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7956 lun->pr_res_idx = residx;
7958 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
7959 lun->pr_generation++;
7960 mtx_unlock(&lun->lun_lock);
7962 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7963 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7964 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7965 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7966 persis_io.pr.pr_info.res_type = type;
7967 memcpy(persis_io.pr.pr_info.sa_res_key,
7968 param->serv_act_res_key,
7969 sizeof(param->serv_act_res_key));
7970 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7971 sizeof(persis_io.pr), M_WAITOK);
7974 * sa_res_key is not the res holder just
7975 * remove registrants
7979 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7980 if (sa_res_key != ctl_get_prkey(lun, i))
7984 ctl_clr_prkey(lun, i);
7985 lun->pr_key_count--;
7986 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7990 mtx_unlock(&lun->lun_lock);
7991 free(ctsio->kern_data_ptr, M_CTL);
7992 ctl_set_reservation_conflict(ctsio);
7993 ctl_done((union ctl_io *)ctsio);
7996 lun->pr_generation++;
7997 mtx_unlock(&lun->lun_lock);
7999 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8000 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8001 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8002 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8003 persis_io.pr.pr_info.res_type = type;
8004 memcpy(persis_io.pr.pr_info.sa_res_key,
8005 param->serv_act_res_key,
8006 sizeof(param->serv_act_res_key));
8007 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8008 sizeof(persis_io.pr), M_WAITOK);
8015 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8017 uint64_t sa_res_key;
8020 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8022 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8023 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8024 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
8025 if (sa_res_key == 0) {
8027 * Unregister everybody else and build UA for
8030 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8031 if (i == msg->pr.pr_info.residx ||
8032 ctl_get_prkey(lun, i) == 0)
8035 ctl_clr_prkey(lun, i);
8036 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8039 lun->pr_key_count = 1;
8040 lun->pr_res_type = msg->pr.pr_info.res_type;
8041 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8042 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8043 lun->pr_res_idx = msg->pr.pr_info.residx;
8045 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8046 if (sa_res_key == ctl_get_prkey(lun, i))
8049 ctl_clr_prkey(lun, i);
8050 lun->pr_key_count--;
8051 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8055 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8056 if (i == msg->pr.pr_info.residx ||
8057 ctl_get_prkey(lun, i) == 0)
8060 if (sa_res_key == ctl_get_prkey(lun, i)) {
8061 ctl_clr_prkey(lun, i);
8062 lun->pr_key_count--;
8063 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8064 } else if (msg->pr.pr_info.res_type != lun->pr_res_type
8065 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8066 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8067 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8070 lun->pr_res_type = msg->pr.pr_info.res_type;
8071 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8072 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8073 lun->pr_res_idx = msg->pr.pr_info.residx;
8075 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8077 lun->pr_generation++;
8083 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8085 struct ctl_softc *softc = CTL_SOFTC(ctsio);
8086 struct ctl_lun *lun = CTL_LUN(ctsio);
8088 u_int32_t param_len;
8089 struct scsi_per_res_out *cdb;
8090 struct scsi_per_res_out_parms* param;
8092 uint64_t res_key, sa_res_key, key;
8094 union ctl_ha_msg persis_io;
8097 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8099 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8100 retval = CTL_RETVAL_COMPLETE;
8103 * We only support whole-LUN scope. The scope & type are ignored for
8104 * register, register and ignore existing key and clear.
8105 * We sometimes ignore scope and type on preempts too!!
8106 * Verify reservation type here as well.
8108 type = cdb->scope_type & SPR_TYPE_MASK;
8109 if ((cdb->action == SPRO_RESERVE)
8110 || (cdb->action == SPRO_RELEASE)) {
8111 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8112 ctl_set_invalid_field(/*ctsio*/ ctsio,
8118 ctl_done((union ctl_io *)ctsio);
8119 return (CTL_RETVAL_COMPLETE);
8122 if (type>8 || type==2 || type==4 || type==0) {
8123 ctl_set_invalid_field(/*ctsio*/ ctsio,
8129 ctl_done((union ctl_io *)ctsio);
8130 return (CTL_RETVAL_COMPLETE);
8134 param_len = scsi_4btoul(cdb->length);
8136 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8137 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8138 ctsio->kern_data_len = param_len;
8139 ctsio->kern_total_len = param_len;
8140 ctsio->kern_rel_offset = 0;
8141 ctsio->kern_sg_entries = 0;
8142 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8143 ctsio->be_move_done = ctl_config_move_done;
8144 ctl_datamove((union ctl_io *)ctsio);
8146 return (CTL_RETVAL_COMPLETE);
8149 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8151 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8152 res_key = scsi_8btou64(param->res_key.key);
8153 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8156 * Validate the reservation key here except for SPRO_REG_IGNO
8157 * This must be done for all other service actions
8159 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8160 mtx_lock(&lun->lun_lock);
8161 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8162 if (res_key != key) {
8164 * The current key passed in doesn't match
8165 * the one the initiator previously
8168 mtx_unlock(&lun->lun_lock);
8169 free(ctsio->kern_data_ptr, M_CTL);
8170 ctl_set_reservation_conflict(ctsio);
8171 ctl_done((union ctl_io *)ctsio);
8172 return (CTL_RETVAL_COMPLETE);
8174 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8176 * We are not registered
8178 mtx_unlock(&lun->lun_lock);
8179 free(ctsio->kern_data_ptr, M_CTL);
8180 ctl_set_reservation_conflict(ctsio);
8181 ctl_done((union ctl_io *)ctsio);
8182 return (CTL_RETVAL_COMPLETE);
8183 } else if (res_key != 0) {
8185 * We are not registered and trying to register but
8186 * the register key isn't zero.
8188 mtx_unlock(&lun->lun_lock);
8189 free(ctsio->kern_data_ptr, M_CTL);
8190 ctl_set_reservation_conflict(ctsio);
8191 ctl_done((union ctl_io *)ctsio);
8192 return (CTL_RETVAL_COMPLETE);
8194 mtx_unlock(&lun->lun_lock);
8197 switch (cdb->action & SPRO_ACTION_MASK) {
8199 case SPRO_REG_IGNO: {
8202 printf("Registration received\n");
8206 * We don't support any of these options, as we report in
8207 * the read capabilities request (see
8208 * ctl_persistent_reserve_in(), above).
8210 if ((param->flags & SPR_SPEC_I_PT)
8211 || (param->flags & SPR_ALL_TG_PT)
8212 || (param->flags & SPR_APTPL)) {
8215 if (param->flags & SPR_APTPL)
8217 else if (param->flags & SPR_ALL_TG_PT)
8219 else /* SPR_SPEC_I_PT */
8222 free(ctsio->kern_data_ptr, M_CTL);
8223 ctl_set_invalid_field(ctsio,
8229 ctl_done((union ctl_io *)ctsio);
8230 return (CTL_RETVAL_COMPLETE);
8233 mtx_lock(&lun->lun_lock);
8236 * The initiator wants to clear the
8239 if (sa_res_key == 0) {
8241 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8242 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8243 && ctl_get_prkey(lun, residx) == 0)) {
8244 mtx_unlock(&lun->lun_lock);
8248 ctl_clr_prkey(lun, residx);
8249 lun->pr_key_count--;
8251 if (residx == lun->pr_res_idx) {
8252 lun->flags &= ~CTL_LUN_PR_RESERVED;
8253 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8255 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8256 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8257 lun->pr_key_count) {
8259 * If the reservation is a registrants
8260 * only type we need to generate a UA
8261 * for other registered inits. The
8262 * sense code should be RESERVATIONS
8266 for (i = softc->init_min; i < softc->init_max; i++){
8267 if (ctl_get_prkey(lun, i) == 0)
8270 CTL_UA_RES_RELEASE);
8273 lun->pr_res_type = 0;
8274 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8275 if (lun->pr_key_count==0) {
8276 lun->flags &= ~CTL_LUN_PR_RESERVED;
8277 lun->pr_res_type = 0;
8278 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8281 lun->pr_generation++;
8282 mtx_unlock(&lun->lun_lock);
8284 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8285 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8286 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8287 persis_io.pr.pr_info.residx = residx;
8288 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8289 sizeof(persis_io.pr), M_WAITOK);
8290 } else /* sa_res_key != 0 */ {
8293 * If we aren't registered currently then increment
8294 * the key count and set the registered flag.
8296 ctl_alloc_prkey(lun, residx);
8297 if (ctl_get_prkey(lun, residx) == 0)
8298 lun->pr_key_count++;
8299 ctl_set_prkey(lun, residx, sa_res_key);
8300 lun->pr_generation++;
8301 mtx_unlock(&lun->lun_lock);
8303 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8304 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8305 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8306 persis_io.pr.pr_info.residx = residx;
8307 memcpy(persis_io.pr.pr_info.sa_res_key,
8308 param->serv_act_res_key,
8309 sizeof(param->serv_act_res_key));
8310 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8311 sizeof(persis_io.pr), M_WAITOK);
8318 printf("Reserve executed type %d\n", type);
8320 mtx_lock(&lun->lun_lock);
8321 if (lun->flags & CTL_LUN_PR_RESERVED) {
8323 * if this isn't the reservation holder and it's
8324 * not a "all registrants" type or if the type is
8325 * different then we have a conflict
8327 if ((lun->pr_res_idx != residx
8328 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8329 || lun->pr_res_type != type) {
8330 mtx_unlock(&lun->lun_lock);
8331 free(ctsio->kern_data_ptr, M_CTL);
8332 ctl_set_reservation_conflict(ctsio);
8333 ctl_done((union ctl_io *)ctsio);
8334 return (CTL_RETVAL_COMPLETE);
8336 mtx_unlock(&lun->lun_lock);
8337 } else /* create a reservation */ {
8339 * If it's not an "all registrants" type record
8340 * reservation holder
8342 if (type != SPR_TYPE_WR_EX_AR
8343 && type != SPR_TYPE_EX_AC_AR)
8344 lun->pr_res_idx = residx; /* Res holder */
8346 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8348 lun->flags |= CTL_LUN_PR_RESERVED;
8349 lun->pr_res_type = type;
8351 mtx_unlock(&lun->lun_lock);
8353 /* send msg to other side */
8354 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8355 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8356 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8357 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8358 persis_io.pr.pr_info.res_type = type;
8359 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8360 sizeof(persis_io.pr), M_WAITOK);
8365 mtx_lock(&lun->lun_lock);
8366 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8367 /* No reservation exists return good status */
8368 mtx_unlock(&lun->lun_lock);
8372 * Is this nexus a reservation holder?
8374 if (lun->pr_res_idx != residx
8375 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8377 * not a res holder return good status but
8380 mtx_unlock(&lun->lun_lock);
8384 if (lun->pr_res_type != type) {
8385 mtx_unlock(&lun->lun_lock);
8386 free(ctsio->kern_data_ptr, M_CTL);
8387 ctl_set_illegal_pr_release(ctsio);
8388 ctl_done((union ctl_io *)ctsio);
8389 return (CTL_RETVAL_COMPLETE);
8392 /* okay to release */
8393 lun->flags &= ~CTL_LUN_PR_RESERVED;
8394 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8395 lun->pr_res_type = 0;
8398 * If this isn't an exclusive access reservation and NUAR
8399 * is not set, generate UA for all other registrants.
8401 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX &&
8402 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8403 for (i = softc->init_min; i < softc->init_max; i++) {
8404 if (i == residx || ctl_get_prkey(lun, i) == 0)
8406 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8409 mtx_unlock(&lun->lun_lock);
8411 /* Send msg to other side */
8412 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8413 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8414 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8415 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8416 sizeof(persis_io.pr), M_WAITOK);
8420 /* send msg to other side */
8422 mtx_lock(&lun->lun_lock);
8423 lun->flags &= ~CTL_LUN_PR_RESERVED;
8424 lun->pr_res_type = 0;
8425 lun->pr_key_count = 0;
8426 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8428 ctl_clr_prkey(lun, residx);
8429 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8430 if (ctl_get_prkey(lun, i) != 0) {
8431 ctl_clr_prkey(lun, i);
8432 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8434 lun->pr_generation++;
8435 mtx_unlock(&lun->lun_lock);
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_CLEAR;
8440 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8441 sizeof(persis_io.pr), M_WAITOK);
8445 case SPRO_PRE_ABO: {
8448 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8449 residx, ctsio, cdb, param);
8451 return (CTL_RETVAL_COMPLETE);
8455 panic("%s: Invalid PR type %#x", __func__, cdb->action);
8459 free(ctsio->kern_data_ptr, M_CTL);
8460 ctl_set_success(ctsio);
8461 ctl_done((union ctl_io *)ctsio);
8467 * This routine is for handling a message from the other SC pertaining to
8468 * persistent reserve out. All the error checking will have been done
8469 * so only perorming the action need be done here to keep the two
8473 ctl_hndl_per_res_out_on_other_sc(union ctl_io *io)
8475 struct ctl_softc *softc = CTL_SOFTC(io);
8476 union ctl_ha_msg *msg = (union ctl_ha_msg *)&io->presio.pr_msg;
8477 struct ctl_lun *lun;
8479 uint32_t residx, targ_lun;
8481 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8482 mtx_lock(&softc->ctl_lock);
8483 if (targ_lun >= CTL_MAX_LUNS ||
8484 (lun = softc->ctl_luns[targ_lun]) == NULL) {
8485 mtx_unlock(&softc->ctl_lock);
8488 mtx_lock(&lun->lun_lock);
8489 mtx_unlock(&softc->ctl_lock);
8490 if (lun->flags & CTL_LUN_DISABLED) {
8491 mtx_unlock(&lun->lun_lock);
8494 residx = ctl_get_initindex(&msg->hdr.nexus);
8495 switch(msg->pr.pr_info.action) {
8496 case CTL_PR_REG_KEY:
8497 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8498 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8499 lun->pr_key_count++;
8500 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8501 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8502 lun->pr_generation++;
8505 case CTL_PR_UNREG_KEY:
8506 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8507 lun->pr_key_count--;
8509 /* XXX Need to see if the reservation has been released */
8510 /* if so do we need to generate UA? */
8511 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8512 lun->flags &= ~CTL_LUN_PR_RESERVED;
8513 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8515 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8516 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8517 lun->pr_key_count) {
8519 * If the reservation is a registrants
8520 * only type we need to generate a UA
8521 * for other registered inits. The
8522 * sense code should be RESERVATIONS
8526 for (i = softc->init_min; i < softc->init_max; i++) {
8527 if (ctl_get_prkey(lun, i) == 0)
8530 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8533 lun->pr_res_type = 0;
8534 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8535 if (lun->pr_key_count==0) {
8536 lun->flags &= ~CTL_LUN_PR_RESERVED;
8537 lun->pr_res_type = 0;
8538 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8541 lun->pr_generation++;
8544 case CTL_PR_RESERVE:
8545 lun->flags |= CTL_LUN_PR_RESERVED;
8546 lun->pr_res_type = msg->pr.pr_info.res_type;
8547 lun->pr_res_idx = msg->pr.pr_info.residx;
8551 case CTL_PR_RELEASE:
8553 * If this isn't an exclusive access reservation and NUAR
8554 * is not set, generate UA for all other registrants.
8556 if (lun->pr_res_type != SPR_TYPE_EX_AC &&
8557 lun->pr_res_type != SPR_TYPE_WR_EX &&
8558 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8559 for (i = softc->init_min; i < softc->init_max; i++)
8560 if (i == residx || ctl_get_prkey(lun, i) == 0)
8562 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8565 lun->flags &= ~CTL_LUN_PR_RESERVED;
8566 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8567 lun->pr_res_type = 0;
8570 case CTL_PR_PREEMPT:
8571 ctl_pro_preempt_other(lun, msg);
8574 lun->flags &= ~CTL_LUN_PR_RESERVED;
8575 lun->pr_res_type = 0;
8576 lun->pr_key_count = 0;
8577 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8579 for (i=0; i < CTL_MAX_INITIATORS; i++) {
8580 if (ctl_get_prkey(lun, i) == 0)
8582 ctl_clr_prkey(lun, i);
8583 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8585 lun->pr_generation++;
8589 mtx_unlock(&lun->lun_lock);
8593 ctl_read_write(struct ctl_scsiio *ctsio)
8595 struct ctl_lun *lun = CTL_LUN(ctsio);
8596 struct ctl_lba_len_flags *lbalen;
8598 uint32_t num_blocks;
8602 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8605 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8606 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8607 switch (ctsio->cdb[0]) {
8610 struct scsi_rw_6 *cdb;
8612 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8614 lba = scsi_3btoul(cdb->addr);
8615 /* only 5 bits are valid in the most significant address byte */
8617 num_blocks = cdb->length;
8619 * This is correct according to SBC-2.
8621 if (num_blocks == 0)
8627 struct scsi_rw_10 *cdb;
8629 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8630 if (cdb->byte2 & SRW10_FUA)
8631 flags |= CTL_LLF_FUA;
8632 if (cdb->byte2 & SRW10_DPO)
8633 flags |= CTL_LLF_DPO;
8634 lba = scsi_4btoul(cdb->addr);
8635 num_blocks = scsi_2btoul(cdb->length);
8638 case WRITE_VERIFY_10: {
8639 struct scsi_write_verify_10 *cdb;
8641 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8642 flags |= CTL_LLF_FUA;
8643 if (cdb->byte2 & SWV_DPO)
8644 flags |= CTL_LLF_DPO;
8645 lba = scsi_4btoul(cdb->addr);
8646 num_blocks = scsi_2btoul(cdb->length);
8651 struct scsi_rw_12 *cdb;
8653 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8654 if (cdb->byte2 & SRW12_FUA)
8655 flags |= CTL_LLF_FUA;
8656 if (cdb->byte2 & SRW12_DPO)
8657 flags |= CTL_LLF_DPO;
8658 lba = scsi_4btoul(cdb->addr);
8659 num_blocks = scsi_4btoul(cdb->length);
8662 case WRITE_VERIFY_12: {
8663 struct scsi_write_verify_12 *cdb;
8665 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8666 flags |= CTL_LLF_FUA;
8667 if (cdb->byte2 & SWV_DPO)
8668 flags |= CTL_LLF_DPO;
8669 lba = scsi_4btoul(cdb->addr);
8670 num_blocks = scsi_4btoul(cdb->length);
8675 struct scsi_rw_16 *cdb;
8677 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8678 if (cdb->byte2 & SRW12_FUA)
8679 flags |= CTL_LLF_FUA;
8680 if (cdb->byte2 & SRW12_DPO)
8681 flags |= CTL_LLF_DPO;
8682 lba = scsi_8btou64(cdb->addr);
8683 num_blocks = scsi_4btoul(cdb->length);
8686 case WRITE_ATOMIC_16: {
8687 struct scsi_write_atomic_16 *cdb;
8689 if (lun->be_lun->atomicblock == 0) {
8690 ctl_set_invalid_opcode(ctsio);
8691 ctl_done((union ctl_io *)ctsio);
8692 return (CTL_RETVAL_COMPLETE);
8695 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb;
8696 if (cdb->byte2 & SRW12_FUA)
8697 flags |= CTL_LLF_FUA;
8698 if (cdb->byte2 & SRW12_DPO)
8699 flags |= CTL_LLF_DPO;
8700 lba = scsi_8btou64(cdb->addr);
8701 num_blocks = scsi_2btoul(cdb->length);
8702 if (num_blocks > lun->be_lun->atomicblock) {
8703 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8704 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8706 ctl_done((union ctl_io *)ctsio);
8707 return (CTL_RETVAL_COMPLETE);
8711 case WRITE_VERIFY_16: {
8712 struct scsi_write_verify_16 *cdb;
8714 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8715 flags |= CTL_LLF_FUA;
8716 if (cdb->byte2 & SWV_DPO)
8717 flags |= CTL_LLF_DPO;
8718 lba = scsi_8btou64(cdb->addr);
8719 num_blocks = scsi_4btoul(cdb->length);
8724 * We got a command we don't support. This shouldn't
8725 * happen, commands should be filtered out above us.
8727 ctl_set_invalid_opcode(ctsio);
8728 ctl_done((union ctl_io *)ctsio);
8730 return (CTL_RETVAL_COMPLETE);
8731 break; /* NOTREACHED */
8735 * The first check is to make sure we're in bounds, the second
8736 * check is to catch wrap-around problems. If the lba + num blocks
8737 * is less than the lba, then we've wrapped around and the block
8738 * range is invalid anyway.
8740 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8741 || ((lba + num_blocks) < lba)) {
8742 ctl_set_lba_out_of_range(ctsio,
8743 MAX(lba, lun->be_lun->maxlba + 1));
8744 ctl_done((union ctl_io *)ctsio);
8745 return (CTL_RETVAL_COMPLETE);
8749 * According to SBC-3, a transfer length of 0 is not an error.
8750 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8751 * translates to 256 blocks for those commands.
8753 if (num_blocks == 0) {
8754 ctl_set_success(ctsio);
8755 ctl_done((union ctl_io *)ctsio);
8756 return (CTL_RETVAL_COMPLETE);
8759 /* Set FUA and/or DPO if caches are disabled. */
8761 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0)
8762 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8764 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8765 flags |= CTL_LLF_FUA;
8768 lbalen = (struct ctl_lba_len_flags *)
8769 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8771 lbalen->len = num_blocks;
8772 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8774 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8775 ctsio->kern_rel_offset = 0;
8777 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8779 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8784 ctl_cnw_cont(union ctl_io *io)
8786 struct ctl_lun *lun = CTL_LUN(io);
8787 struct ctl_scsiio *ctsio;
8788 struct ctl_lba_len_flags *lbalen;
8791 ctsio = &io->scsiio;
8792 ctsio->io_hdr.status = CTL_STATUS_NONE;
8793 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8794 lbalen = (struct ctl_lba_len_flags *)
8795 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8796 lbalen->flags &= ~CTL_LLF_COMPARE;
8797 lbalen->flags |= CTL_LLF_WRITE;
8799 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8800 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8805 ctl_cnw(struct ctl_scsiio *ctsio)
8807 struct ctl_lun *lun = CTL_LUN(ctsio);
8808 struct ctl_lba_len_flags *lbalen;
8810 uint32_t num_blocks;
8813 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8816 switch (ctsio->cdb[0]) {
8817 case COMPARE_AND_WRITE: {
8818 struct scsi_compare_and_write *cdb;
8820 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8821 if (cdb->byte2 & SRW10_FUA)
8822 flags |= CTL_LLF_FUA;
8823 if (cdb->byte2 & SRW10_DPO)
8824 flags |= CTL_LLF_DPO;
8825 lba = scsi_8btou64(cdb->addr);
8826 num_blocks = cdb->length;
8831 * We got a command we don't support. This shouldn't
8832 * happen, commands should be filtered out above us.
8834 ctl_set_invalid_opcode(ctsio);
8835 ctl_done((union ctl_io *)ctsio);
8837 return (CTL_RETVAL_COMPLETE);
8838 break; /* NOTREACHED */
8842 * The first check is to make sure we're in bounds, the second
8843 * check is to catch wrap-around problems. If the lba + num blocks
8844 * is less than the lba, then we've wrapped around and the block
8845 * range is invalid anyway.
8847 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8848 || ((lba + num_blocks) < lba)) {
8849 ctl_set_lba_out_of_range(ctsio,
8850 MAX(lba, lun->be_lun->maxlba + 1));
8851 ctl_done((union ctl_io *)ctsio);
8852 return (CTL_RETVAL_COMPLETE);
8856 * According to SBC-3, a transfer length of 0 is not an error.
8858 if (num_blocks == 0) {
8859 ctl_set_success(ctsio);
8860 ctl_done((union ctl_io *)ctsio);
8861 return (CTL_RETVAL_COMPLETE);
8864 /* Set FUA if write cache is disabled. */
8865 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8866 flags |= CTL_LLF_FUA;
8868 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
8869 ctsio->kern_rel_offset = 0;
8872 * Set the IO_CONT flag, so that if this I/O gets passed to
8873 * ctl_data_submit_done(), it'll get passed back to
8874 * ctl_ctl_cnw_cont() for further processing.
8876 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
8877 ctsio->io_cont = ctl_cnw_cont;
8879 lbalen = (struct ctl_lba_len_flags *)
8880 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8882 lbalen->len = num_blocks;
8883 lbalen->flags = CTL_LLF_COMPARE | flags;
8885 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
8886 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8891 ctl_verify(struct ctl_scsiio *ctsio)
8893 struct ctl_lun *lun = CTL_LUN(ctsio);
8894 struct ctl_lba_len_flags *lbalen;
8896 uint32_t num_blocks;
8900 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
8903 flags = CTL_LLF_FUA;
8904 switch (ctsio->cdb[0]) {
8906 struct scsi_verify_10 *cdb;
8908 cdb = (struct scsi_verify_10 *)ctsio->cdb;
8909 if (cdb->byte2 & SVFY_BYTCHK)
8911 if (cdb->byte2 & SVFY_DPO)
8912 flags |= CTL_LLF_DPO;
8913 lba = scsi_4btoul(cdb->addr);
8914 num_blocks = scsi_2btoul(cdb->length);
8918 struct scsi_verify_12 *cdb;
8920 cdb = (struct scsi_verify_12 *)ctsio->cdb;
8921 if (cdb->byte2 & SVFY_BYTCHK)
8923 if (cdb->byte2 & SVFY_DPO)
8924 flags |= CTL_LLF_DPO;
8925 lba = scsi_4btoul(cdb->addr);
8926 num_blocks = scsi_4btoul(cdb->length);
8930 struct scsi_rw_16 *cdb;
8932 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8933 if (cdb->byte2 & SVFY_BYTCHK)
8935 if (cdb->byte2 & SVFY_DPO)
8936 flags |= CTL_LLF_DPO;
8937 lba = scsi_8btou64(cdb->addr);
8938 num_blocks = scsi_4btoul(cdb->length);
8943 * We got a command we don't support. This shouldn't
8944 * happen, commands should be filtered out above us.
8946 ctl_set_invalid_opcode(ctsio);
8947 ctl_done((union ctl_io *)ctsio);
8948 return (CTL_RETVAL_COMPLETE);
8952 * The first check is to make sure we're in bounds, the second
8953 * check is to catch wrap-around problems. If the lba + num blocks
8954 * is less than the lba, then we've wrapped around and the block
8955 * range is invalid anyway.
8957 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8958 || ((lba + num_blocks) < lba)) {
8959 ctl_set_lba_out_of_range(ctsio,
8960 MAX(lba, lun->be_lun->maxlba + 1));
8961 ctl_done((union ctl_io *)ctsio);
8962 return (CTL_RETVAL_COMPLETE);
8966 * According to SBC-3, a transfer length of 0 is not an error.
8968 if (num_blocks == 0) {
8969 ctl_set_success(ctsio);
8970 ctl_done((union ctl_io *)ctsio);
8971 return (CTL_RETVAL_COMPLETE);
8974 lbalen = (struct ctl_lba_len_flags *)
8975 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8977 lbalen->len = num_blocks;
8979 lbalen->flags = CTL_LLF_COMPARE | flags;
8980 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8982 lbalen->flags = CTL_LLF_VERIFY | flags;
8983 ctsio->kern_total_len = 0;
8985 ctsio->kern_rel_offset = 0;
8987 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
8988 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8993 ctl_report_luns(struct ctl_scsiio *ctsio)
8995 struct ctl_softc *softc = CTL_SOFTC(ctsio);
8996 struct ctl_port *port = CTL_PORT(ctsio);
8997 struct ctl_lun *lun, *request_lun = CTL_LUN(ctsio);
8998 struct scsi_report_luns *cdb;
8999 struct scsi_report_luns_data *lun_data;
9000 int num_filled, num_luns, num_port_luns, retval;
9001 uint32_t alloc_len, lun_datalen;
9002 uint32_t initidx, targ_lun_id, lun_id;
9004 retval = CTL_RETVAL_COMPLETE;
9005 cdb = (struct scsi_report_luns *)ctsio->cdb;
9007 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9010 num_port_luns = port->lun_map ? port->lun_map_size : CTL_MAX_LUNS;
9011 mtx_lock(&softc->ctl_lock);
9012 for (targ_lun_id = 0; targ_lun_id < num_port_luns; targ_lun_id++) {
9013 if (ctl_lun_map_from_port(port, targ_lun_id) != UINT32_MAX)
9016 mtx_unlock(&softc->ctl_lock);
9018 switch (cdb->select_report) {
9019 case RPL_REPORT_DEFAULT:
9020 case RPL_REPORT_ALL:
9021 case RPL_REPORT_NONSUBSID:
9023 case RPL_REPORT_WELLKNOWN:
9024 case RPL_REPORT_ADMIN:
9025 case RPL_REPORT_CONGLOM:
9029 ctl_set_invalid_field(ctsio,
9035 ctl_done((union ctl_io *)ctsio);
9037 break; /* NOTREACHED */
9040 alloc_len = scsi_4btoul(cdb->length);
9042 * The initiator has to allocate at least 16 bytes for this request,
9043 * so he can at least get the header and the first LUN. Otherwise
9044 * we reject the request (per SPC-3 rev 14, section 6.21).
9046 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9047 sizeof(struct scsi_report_luns_lundata))) {
9048 ctl_set_invalid_field(ctsio,
9054 ctl_done((union ctl_io *)ctsio);
9058 lun_datalen = sizeof(*lun_data) +
9059 (num_luns * sizeof(struct scsi_report_luns_lundata));
9061 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9062 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9063 ctsio->kern_sg_entries = 0;
9065 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9067 mtx_lock(&softc->ctl_lock);
9068 for (targ_lun_id = 0, num_filled = 0;
9069 targ_lun_id < num_port_luns && num_filled < num_luns;
9071 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9072 if (lun_id == UINT32_MAX)
9074 lun = softc->ctl_luns[lun_id];
9078 be64enc(lun_data->luns[num_filled++].lundata,
9079 ctl_encode_lun(targ_lun_id));
9082 * According to SPC-3, rev 14 section 6.21:
9084 * "The execution of a REPORT LUNS command to any valid and
9085 * installed logical unit shall clear the REPORTED LUNS DATA
9086 * HAS CHANGED unit attention condition for all logical
9087 * units of that target with respect to the requesting
9088 * initiator. A valid and installed logical unit is one
9089 * having a PERIPHERAL QUALIFIER of 000b in the standard
9090 * INQUIRY data (see 6.4.2)."
9092 * If request_lun is NULL, the LUN this report luns command
9093 * was issued to is either disabled or doesn't exist. In that
9094 * case, we shouldn't clear any pending lun change unit
9097 if (request_lun != NULL) {
9098 mtx_lock(&lun->lun_lock);
9099 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE);
9100 mtx_unlock(&lun->lun_lock);
9103 mtx_unlock(&softc->ctl_lock);
9106 * It's quite possible that we've returned fewer LUNs than we allocated
9107 * space for. Trim it.
9109 lun_datalen = sizeof(*lun_data) +
9110 (num_filled * sizeof(struct scsi_report_luns_lundata));
9111 ctsio->kern_rel_offset = 0;
9112 ctsio->kern_sg_entries = 0;
9113 ctsio->kern_data_len = min(lun_datalen, alloc_len);
9114 ctsio->kern_total_len = ctsio->kern_data_len;
9117 * We set this to the actual data length, regardless of how much
9118 * space we actually have to return results. If the user looks at
9119 * this value, he'll know whether or not he allocated enough space
9120 * and reissue the command if necessary. We don't support well
9121 * known logical units, so if the user asks for that, return none.
9123 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9126 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9129 ctl_set_success(ctsio);
9130 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9131 ctsio->be_move_done = ctl_config_move_done;
9132 ctl_datamove((union ctl_io *)ctsio);
9137 ctl_request_sense(struct ctl_scsiio *ctsio)
9139 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9140 struct ctl_lun *lun = CTL_LUN(ctsio);
9141 struct scsi_request_sense *cdb;
9142 struct scsi_sense_data *sense_ptr, *ps;
9145 u_int sense_len = SSD_FULL_SIZE;
9146 scsi_sense_data_type sense_format;
9147 ctl_ua_type ua_type;
9148 uint8_t asc = 0, ascq = 0;
9150 cdb = (struct scsi_request_sense *)ctsio->cdb;
9152 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9155 * Determine which sense format the user wants.
9157 if (cdb->byte2 & SRS_DESC)
9158 sense_format = SSD_TYPE_DESC;
9160 sense_format = SSD_TYPE_FIXED;
9162 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9163 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9164 ctsio->kern_sg_entries = 0;
9165 ctsio->kern_rel_offset = 0;
9168 * struct scsi_sense_data, which is currently set to 256 bytes, is
9169 * larger than the largest allowed value for the length field in the
9170 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9172 ctsio->kern_data_len = cdb->length;
9173 ctsio->kern_total_len = cdb->length;
9176 * If we don't have a LUN, we don't have any pending sense.
9179 ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
9180 softc->ha_link < CTL_HA_LINK_UNKNOWN)) {
9181 /* "Logical unit not supported" */
9182 ctl_set_sense_data(sense_ptr, &sense_len, NULL, sense_format,
9183 /*current_error*/ 1,
9184 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
9192 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9194 * Check for pending sense, and then for pending unit attentions.
9195 * Pending sense gets returned first, then pending unit attentions.
9197 mtx_lock(&lun->lun_lock);
9198 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT];
9200 ps += initidx % CTL_MAX_INIT_PER_PORT;
9201 if (ps != NULL && ps->error_code != 0) {
9202 scsi_sense_data_type stored_format;
9205 * Check to see which sense format was used for the stored
9208 stored_format = scsi_sense_type(ps);
9211 * If the user requested a different sense format than the
9212 * one we stored, then we need to convert it to the other
9213 * format. If we're going from descriptor to fixed format
9214 * sense data, we may lose things in translation, depending
9215 * on what options were used.
9217 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9218 * for some reason we'll just copy it out as-is.
9220 if ((stored_format == SSD_TYPE_FIXED)
9221 && (sense_format == SSD_TYPE_DESC))
9222 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9223 ps, (struct scsi_sense_data_desc *)sense_ptr);
9224 else if ((stored_format == SSD_TYPE_DESC)
9225 && (sense_format == SSD_TYPE_FIXED))
9226 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9227 ps, (struct scsi_sense_data_fixed *)sense_ptr);
9229 memcpy(sense_ptr, ps, sizeof(*sense_ptr));
9234 ua_type = ctl_build_ua(lun, initidx, sense_ptr, &sense_len,
9236 if (ua_type != CTL_UA_NONE)
9239 if (have_error == 0) {
9241 * Report informational exception if have one and allowed.
9243 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) {
9245 ascq = lun->ie_ascq;
9247 ctl_set_sense_data(sense_ptr, &sense_len, lun, sense_format,
9248 /*current_error*/ 1,
9249 /*sense_key*/ SSD_KEY_NO_SENSE,
9254 mtx_unlock(&lun->lun_lock);
9258 * We report the SCSI status as OK, since the status of the command
9259 * itself is OK. We're reporting sense as parameter data.
9261 ctl_set_success(ctsio);
9262 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9263 ctsio->be_move_done = ctl_config_move_done;
9264 ctl_datamove((union ctl_io *)ctsio);
9265 return (CTL_RETVAL_COMPLETE);
9269 ctl_tur(struct ctl_scsiio *ctsio)
9272 CTL_DEBUG_PRINT(("ctl_tur\n"));
9274 ctl_set_success(ctsio);
9275 ctl_done((union ctl_io *)ctsio);
9277 return (CTL_RETVAL_COMPLETE);
9281 * SCSI VPD page 0x00, the Supported VPD Pages page.
9284 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9286 struct ctl_lun *lun = CTL_LUN(ctsio);
9287 struct scsi_vpd_supported_pages *pages;
9291 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9292 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9293 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9294 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9295 ctsio->kern_rel_offset = 0;
9296 ctsio->kern_sg_entries = 0;
9297 ctsio->kern_data_len = min(sup_page_size, alloc_len);
9298 ctsio->kern_total_len = ctsio->kern_data_len;
9301 * The control device is always connected. The disk device, on the
9302 * other hand, may not be online all the time. Need to change this
9303 * to figure out whether the disk device is actually online or not.
9306 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9307 lun->be_lun->lun_type;
9309 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9312 /* Supported VPD pages */
9313 pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9315 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9316 /* Device Identification */
9317 pages->page_list[p++] = SVPD_DEVICE_ID;
9318 /* Extended INQUIRY Data */
9319 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9320 /* Mode Page Policy */
9321 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9323 pages->page_list[p++] = SVPD_SCSI_PORTS;
9324 /* Third-party Copy */
9325 pages->page_list[p++] = SVPD_SCSI_TPC;
9326 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9328 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9329 /* Block Device Characteristics */
9330 pages->page_list[p++] = SVPD_BDC;
9331 /* Logical Block Provisioning */
9332 pages->page_list[p++] = SVPD_LBP;
9336 ctl_set_success(ctsio);
9337 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9338 ctsio->be_move_done = ctl_config_move_done;
9339 ctl_datamove((union ctl_io *)ctsio);
9340 return (CTL_RETVAL_COMPLETE);
9344 * SCSI VPD page 0x80, the Unit Serial Number page.
9347 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9349 struct ctl_lun *lun = CTL_LUN(ctsio);
9350 struct scsi_vpd_unit_serial_number *sn_ptr;
9353 data_len = 4 + CTL_SN_LEN;
9354 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9355 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9356 ctsio->kern_rel_offset = 0;
9357 ctsio->kern_sg_entries = 0;
9358 ctsio->kern_data_len = min(data_len, alloc_len);
9359 ctsio->kern_total_len = ctsio->kern_data_len;
9362 * The control device is always connected. The disk device, on the
9363 * other hand, may not be online all the time. Need to change this
9364 * to figure out whether the disk device is actually online or not.
9367 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9368 lun->be_lun->lun_type;
9370 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9372 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9373 sn_ptr->length = CTL_SN_LEN;
9375 * If we don't have a LUN, we just leave the serial number as
9379 strncpy((char *)sn_ptr->serial_num,
9380 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9382 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9384 ctl_set_success(ctsio);
9385 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9386 ctsio->be_move_done = ctl_config_move_done;
9387 ctl_datamove((union ctl_io *)ctsio);
9388 return (CTL_RETVAL_COMPLETE);
9393 * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9396 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9398 struct ctl_lun *lun = CTL_LUN(ctsio);
9399 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9402 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9403 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9404 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9405 ctsio->kern_sg_entries = 0;
9406 ctsio->kern_rel_offset = 0;
9407 ctsio->kern_data_len = min(data_len, alloc_len);
9408 ctsio->kern_total_len = ctsio->kern_data_len;
9411 * The control device is always connected. The disk device, on the
9412 * other hand, may not be online all the time.
9415 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9416 lun->be_lun->lun_type;
9418 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9419 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9420 scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9422 * We support head of queue, ordered and simple tags.
9424 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9426 * Volatile cache supported.
9428 eid_ptr->flags3 = SVPD_EID_V_SUP;
9431 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9432 * attention for a particular IT nexus on all LUNs once we report
9433 * it to that nexus once. This bit is required as of SPC-4.
9435 eid_ptr->flags4 = SVPD_EID_LUICLR;
9438 * We support revert to defaults (RTD) bit in MODE SELECT.
9440 eid_ptr->flags5 = SVPD_EID_RTD_SUP;
9443 * XXX KDM in order to correctly answer this, we would need
9444 * information from the SIM to determine how much sense data it
9445 * can send. So this would really be a path inquiry field, most
9446 * likely. This can be set to a maximum of 252 according to SPC-4,
9447 * but the hardware may or may not be able to support that much.
9448 * 0 just means that the maximum sense data length is not reported.
9450 eid_ptr->max_sense_length = 0;
9452 ctl_set_success(ctsio);
9453 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9454 ctsio->be_move_done = ctl_config_move_done;
9455 ctl_datamove((union ctl_io *)ctsio);
9456 return (CTL_RETVAL_COMPLETE);
9460 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9462 struct ctl_lun *lun = CTL_LUN(ctsio);
9463 struct scsi_vpd_mode_page_policy *mpp_ptr;
9466 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9467 sizeof(struct scsi_vpd_mode_page_policy_descr);
9469 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9470 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9471 ctsio->kern_rel_offset = 0;
9472 ctsio->kern_sg_entries = 0;
9473 ctsio->kern_data_len = min(data_len, alloc_len);
9474 ctsio->kern_total_len = ctsio->kern_data_len;
9477 * The control device is always connected. The disk device, on the
9478 * other hand, may not be online all the time.
9481 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9482 lun->be_lun->lun_type;
9484 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9485 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9486 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9487 mpp_ptr->descr[0].page_code = 0x3f;
9488 mpp_ptr->descr[0].subpage_code = 0xff;
9489 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9491 ctl_set_success(ctsio);
9492 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9493 ctsio->be_move_done = ctl_config_move_done;
9494 ctl_datamove((union ctl_io *)ctsio);
9495 return (CTL_RETVAL_COMPLETE);
9499 * SCSI VPD page 0x83, the Device Identification page.
9502 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9504 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9505 struct ctl_port *port = CTL_PORT(ctsio);
9506 struct ctl_lun *lun = CTL_LUN(ctsio);
9507 struct scsi_vpd_device_id *devid_ptr;
9508 struct scsi_vpd_id_descriptor *desc;
9512 data_len = sizeof(struct scsi_vpd_device_id) +
9513 sizeof(struct scsi_vpd_id_descriptor) +
9514 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9515 sizeof(struct scsi_vpd_id_descriptor) +
9516 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9517 if (lun && lun->lun_devid)
9518 data_len += lun->lun_devid->len;
9519 if (port && port->port_devid)
9520 data_len += port->port_devid->len;
9521 if (port && port->target_devid)
9522 data_len += port->target_devid->len;
9524 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9525 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9526 ctsio->kern_sg_entries = 0;
9527 ctsio->kern_rel_offset = 0;
9528 ctsio->kern_sg_entries = 0;
9529 ctsio->kern_data_len = min(data_len, alloc_len);
9530 ctsio->kern_total_len = ctsio->kern_data_len;
9533 * The control device is always connected. The disk device, on the
9534 * other hand, may not be online all the time.
9537 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9538 lun->be_lun->lun_type;
9540 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9541 devid_ptr->page_code = SVPD_DEVICE_ID;
9542 scsi_ulto2b(data_len - 4, devid_ptr->length);
9544 if (port && port->port_type == CTL_PORT_FC)
9545 proto = SCSI_PROTO_FC << 4;
9546 else if (port && port->port_type == CTL_PORT_SAS)
9547 proto = SCSI_PROTO_SAS << 4;
9548 else if (port && port->port_type == CTL_PORT_ISCSI)
9549 proto = SCSI_PROTO_ISCSI << 4;
9551 proto = SCSI_PROTO_SPI << 4;
9552 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9555 * We're using a LUN association here. i.e., this device ID is a
9556 * per-LUN identifier.
9558 if (lun && lun->lun_devid) {
9559 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9560 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9561 lun->lun_devid->len);
9565 * This is for the WWPN which is a port association.
9567 if (port && port->port_devid) {
9568 memcpy(desc, port->port_devid->data, port->port_devid->len);
9569 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9570 port->port_devid->len);
9574 * This is for the Relative Target Port(type 4h) identifier
9576 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9577 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9578 SVPD_ID_TYPE_RELTARG;
9580 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9581 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9582 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9585 * This is for the Target Port Group(type 5h) identifier
9587 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9588 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9589 SVPD_ID_TYPE_TPORTGRP;
9591 if (softc->is_single ||
9592 (port && port->status & CTL_PORT_STATUS_HA_SHARED))
9595 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt;
9596 scsi_ulto2b(g, &desc->identifier[2]);
9597 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9598 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9601 * This is for the Target identifier
9603 if (port && port->target_devid) {
9604 memcpy(desc, port->target_devid->data, port->target_devid->len);
9607 ctl_set_success(ctsio);
9608 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9609 ctsio->be_move_done = ctl_config_move_done;
9610 ctl_datamove((union ctl_io *)ctsio);
9611 return (CTL_RETVAL_COMPLETE);
9615 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9617 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9618 struct ctl_lun *lun = CTL_LUN(ctsio);
9619 struct scsi_vpd_scsi_ports *sp;
9620 struct scsi_vpd_port_designation *pd;
9621 struct scsi_vpd_port_designation_cont *pdc;
9622 struct ctl_port *port;
9623 int data_len, num_target_ports, iid_len, id_len;
9625 num_target_ports = 0;
9628 mtx_lock(&softc->ctl_lock);
9629 STAILQ_FOREACH(port, &softc->port_list, links) {
9630 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9633 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9636 if (port->init_devid)
9637 iid_len += port->init_devid->len;
9638 if (port->port_devid)
9639 id_len += port->port_devid->len;
9641 mtx_unlock(&softc->ctl_lock);
9643 data_len = sizeof(struct scsi_vpd_scsi_ports) +
9644 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9645 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9646 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9647 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9648 ctsio->kern_sg_entries = 0;
9649 ctsio->kern_rel_offset = 0;
9650 ctsio->kern_sg_entries = 0;
9651 ctsio->kern_data_len = min(data_len, alloc_len);
9652 ctsio->kern_total_len = ctsio->kern_data_len;
9655 * The control device is always connected. The disk device, on the
9656 * other hand, may not be online all the time. Need to change this
9657 * to figure out whether the disk device is actually online or not.
9660 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9661 lun->be_lun->lun_type;
9663 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9665 sp->page_code = SVPD_SCSI_PORTS;
9666 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9668 pd = &sp->design[0];
9670 mtx_lock(&softc->ctl_lock);
9671 STAILQ_FOREACH(port, &softc->port_list, links) {
9672 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9675 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9677 scsi_ulto2b(port->targ_port, pd->relative_port_id);
9678 if (port->init_devid) {
9679 iid_len = port->init_devid->len;
9680 memcpy(pd->initiator_transportid,
9681 port->init_devid->data, port->init_devid->len);
9684 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9685 pdc = (struct scsi_vpd_port_designation_cont *)
9686 (&pd->initiator_transportid[iid_len]);
9687 if (port->port_devid) {
9688 id_len = port->port_devid->len;
9689 memcpy(pdc->target_port_descriptors,
9690 port->port_devid->data, port->port_devid->len);
9693 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9694 pd = (struct scsi_vpd_port_designation *)
9695 ((uint8_t *)pdc->target_port_descriptors + id_len);
9697 mtx_unlock(&softc->ctl_lock);
9699 ctl_set_success(ctsio);
9700 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9701 ctsio->be_move_done = ctl_config_move_done;
9702 ctl_datamove((union ctl_io *)ctsio);
9703 return (CTL_RETVAL_COMPLETE);
9707 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9709 struct ctl_lun *lun = CTL_LUN(ctsio);
9710 struct scsi_vpd_block_limits *bl_ptr;
9713 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9714 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9715 ctsio->kern_sg_entries = 0;
9716 ctsio->kern_rel_offset = 0;
9717 ctsio->kern_sg_entries = 0;
9718 ctsio->kern_data_len = min(sizeof(*bl_ptr), alloc_len);
9719 ctsio->kern_total_len = ctsio->kern_data_len;
9722 * The control device is always connected. The disk device, on the
9723 * other hand, may not be online all the time. Need to change this
9724 * to figure out whether the disk device is actually online or not.
9727 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9728 lun->be_lun->lun_type;
9730 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9732 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9733 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
9734 bl_ptr->max_cmp_write_len = 0xff;
9735 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9737 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
9738 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9740 ctl_get_opt_number(&lun->be_lun->options,
9741 "unmap_max_lba", &ival);
9742 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt);
9744 ctl_get_opt_number(&lun->be_lun->options,
9745 "unmap_max_descr", &ival);
9746 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt);
9747 if (lun->be_lun->ublockexp != 0) {
9748 scsi_ulto4b((1 << lun->be_lun->ublockexp),
9749 bl_ptr->opt_unmap_grain);
9750 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
9751 bl_ptr->unmap_grain_align);
9754 scsi_ulto4b(lun->be_lun->atomicblock,
9755 bl_ptr->max_atomic_transfer_length);
9756 scsi_ulto4b(0, bl_ptr->atomic_alignment);
9757 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
9758 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary);
9759 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size);
9761 ctl_get_opt_number(&lun->be_lun->options, "write_same_max_lba", &ival);
9762 scsi_u64to8b(ival, bl_ptr->max_write_same_length);
9765 ctl_set_success(ctsio);
9766 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9767 ctsio->be_move_done = ctl_config_move_done;
9768 ctl_datamove((union ctl_io *)ctsio);
9769 return (CTL_RETVAL_COMPLETE);
9773 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
9775 struct ctl_lun *lun = CTL_LUN(ctsio);
9776 struct scsi_vpd_block_device_characteristics *bdc_ptr;
9780 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
9781 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
9782 ctsio->kern_sg_entries = 0;
9783 ctsio->kern_rel_offset = 0;
9784 ctsio->kern_data_len = min(sizeof(*bdc_ptr), alloc_len);
9785 ctsio->kern_total_len = ctsio->kern_data_len;
9788 * The control device is always connected. The disk device, on the
9789 * other hand, may not be online all the time. Need to change this
9790 * to figure out whether the disk device is actually online or not.
9793 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9794 lun->be_lun->lun_type;
9796 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9797 bdc_ptr->page_code = SVPD_BDC;
9798 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
9800 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL)
9801 i = strtol(value, NULL, 0);
9803 i = CTL_DEFAULT_ROTATION_RATE;
9804 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
9806 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL)
9807 i = strtol(value, NULL, 0);
9810 bdc_ptr->wab_wac_ff = (i & 0x0f);
9811 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
9813 ctl_set_success(ctsio);
9814 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9815 ctsio->be_move_done = ctl_config_move_done;
9816 ctl_datamove((union ctl_io *)ctsio);
9817 return (CTL_RETVAL_COMPLETE);
9821 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
9823 struct ctl_lun *lun = CTL_LUN(ctsio);
9824 struct scsi_vpd_logical_block_prov *lbp_ptr;
9827 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
9828 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
9829 ctsio->kern_sg_entries = 0;
9830 ctsio->kern_rel_offset = 0;
9831 ctsio->kern_data_len = min(sizeof(*lbp_ptr), alloc_len);
9832 ctsio->kern_total_len = ctsio->kern_data_len;
9835 * The control device is always connected. The disk device, on the
9836 * other hand, may not be online all the time. Need to change this
9837 * to figure out whether the disk device is actually online or not.
9840 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9841 lun->be_lun->lun_type;
9843 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9845 lbp_ptr->page_code = SVPD_LBP;
9846 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
9847 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
9848 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9849 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
9850 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
9851 value = ctl_get_opt(&lun->be_lun->options, "provisioning_type");
9852 if (value != NULL) {
9853 if (strcmp(value, "resource") == 0)
9854 lbp_ptr->prov_type = SVPD_LBP_RESOURCE;
9855 else if (strcmp(value, "thin") == 0)
9856 lbp_ptr->prov_type = SVPD_LBP_THIN;
9858 lbp_ptr->prov_type = SVPD_LBP_THIN;
9861 ctl_set_success(ctsio);
9862 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9863 ctsio->be_move_done = ctl_config_move_done;
9864 ctl_datamove((union ctl_io *)ctsio);
9865 return (CTL_RETVAL_COMPLETE);
9869 * INQUIRY with the EVPD bit set.
9872 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
9874 struct ctl_lun *lun = CTL_LUN(ctsio);
9875 struct scsi_inquiry *cdb;
9876 int alloc_len, retval;
9878 cdb = (struct scsi_inquiry *)ctsio->cdb;
9879 alloc_len = scsi_2btoul(cdb->length);
9881 switch (cdb->page_code) {
9882 case SVPD_SUPPORTED_PAGES:
9883 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
9885 case SVPD_UNIT_SERIAL_NUMBER:
9886 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
9888 case SVPD_DEVICE_ID:
9889 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
9891 case SVPD_EXTENDED_INQUIRY_DATA:
9892 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
9894 case SVPD_MODE_PAGE_POLICY:
9895 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
9897 case SVPD_SCSI_PORTS:
9898 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
9901 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
9903 case SVPD_BLOCK_LIMITS:
9904 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
9906 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
9909 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
9911 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
9914 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
9916 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
9920 ctl_set_invalid_field(ctsio,
9926 ctl_done((union ctl_io *)ctsio);
9927 retval = CTL_RETVAL_COMPLETE;
9935 * Standard INQUIRY data.
9938 ctl_inquiry_std(struct ctl_scsiio *ctsio)
9940 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9941 struct ctl_port *port = CTL_PORT(ctsio);
9942 struct ctl_lun *lun = CTL_LUN(ctsio);
9943 struct scsi_inquiry_data *inq_ptr;
9944 struct scsi_inquiry *cdb;
9946 uint32_t alloc_len, data_len;
9947 ctl_port_type port_type;
9949 port_type = port->port_type;
9950 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
9951 port_type = CTL_PORT_SCSI;
9953 cdb = (struct scsi_inquiry *)ctsio->cdb;
9954 alloc_len = scsi_2btoul(cdb->length);
9957 * We malloc the full inquiry data size here and fill it
9958 * in. If the user only asks for less, we'll give him
9961 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
9962 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9963 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
9964 ctsio->kern_sg_entries = 0;
9965 ctsio->kern_rel_offset = 0;
9966 ctsio->kern_data_len = min(data_len, alloc_len);
9967 ctsio->kern_total_len = ctsio->kern_data_len;
9970 if ((lun->flags & CTL_LUN_PRIMARY_SC) ||
9971 softc->ha_link >= CTL_HA_LINK_UNKNOWN) {
9972 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9973 lun->be_lun->lun_type;
9975 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) |
9976 lun->be_lun->lun_type;
9978 if (lun->flags & CTL_LUN_REMOVABLE)
9979 inq_ptr->dev_qual2 |= SID_RMB;
9981 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
9983 /* RMB in byte 2 is 0 */
9984 inq_ptr->version = SCSI_REV_SPC5;
9987 * According to SAM-3, even if a device only supports a single
9988 * level of LUN addressing, it should still set the HISUP bit:
9990 * 4.9.1 Logical unit numbers overview
9992 * All logical unit number formats described in this standard are
9993 * hierarchical in structure even when only a single level in that
9994 * hierarchy is used. The HISUP bit shall be set to one in the
9995 * standard INQUIRY data (see SPC-2) when any logical unit number
9996 * format described in this standard is used. Non-hierarchical
9997 * formats are outside the scope of this standard.
9999 * Therefore we set the HiSup bit here.
10001 * The response format is 2, per SPC-3.
10003 inq_ptr->response_format = SID_HiSup | 2;
10005 inq_ptr->additional_length = data_len -
10006 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10007 CTL_DEBUG_PRINT(("additional_length = %d\n",
10008 inq_ptr->additional_length));
10010 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10011 if (port_type == CTL_PORT_SCSI)
10012 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10013 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10014 inq_ptr->flags = SID_CmdQue;
10015 if (port_type == CTL_PORT_SCSI)
10016 inq_ptr->flags |= SID_WBus16 | SID_Sync;
10019 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10020 * We have 8 bytes for the vendor name, and 16 bytes for the device
10021 * name and 4 bytes for the revision.
10023 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10024 "vendor")) == NULL) {
10025 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10027 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10028 strncpy(inq_ptr->vendor, val,
10029 min(sizeof(inq_ptr->vendor), strlen(val)));
10032 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10033 sizeof(inq_ptr->product));
10034 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10035 switch (lun->be_lun->lun_type) {
10037 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10038 sizeof(inq_ptr->product));
10041 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10042 sizeof(inq_ptr->product));
10045 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT,
10046 sizeof(inq_ptr->product));
10049 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10050 sizeof(inq_ptr->product));
10054 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10055 strncpy(inq_ptr->product, val,
10056 min(sizeof(inq_ptr->product), strlen(val)));
10060 * XXX make this a macro somewhere so it automatically gets
10061 * incremented when we make changes.
10063 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10064 "revision")) == NULL) {
10065 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10067 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10068 strncpy(inq_ptr->revision, val,
10069 min(sizeof(inq_ptr->revision), strlen(val)));
10073 * For parallel SCSI, we support double transition and single
10074 * transition clocking. We also support QAS (Quick Arbitration
10075 * and Selection) and Information Unit transfers on both the
10076 * control and array devices.
10078 if (port_type == CTL_PORT_SCSI)
10079 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10082 /* SAM-6 (no version claimed) */
10083 scsi_ulto2b(0x00C0, inq_ptr->version1);
10084 /* SPC-5 (no version claimed) */
10085 scsi_ulto2b(0x05C0, inq_ptr->version2);
10086 if (port_type == CTL_PORT_FC) {
10087 /* FCP-2 ANSI INCITS.350:2003 */
10088 scsi_ulto2b(0x0917, inq_ptr->version3);
10089 } else if (port_type == CTL_PORT_SCSI) {
10090 /* SPI-4 ANSI INCITS.362:200x */
10091 scsi_ulto2b(0x0B56, inq_ptr->version3);
10092 } else if (port_type == CTL_PORT_ISCSI) {
10093 /* iSCSI (no version claimed) */
10094 scsi_ulto2b(0x0960, inq_ptr->version3);
10095 } else if (port_type == CTL_PORT_SAS) {
10096 /* SAS (no version claimed) */
10097 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10098 } else if (port_type == CTL_PORT_UMASS) {
10099 /* USB Mass Storage Class Bulk-Only Transport, Revision 1.0 */
10100 scsi_ulto2b(0x1730, inq_ptr->version3);
10104 /* SBC-4 (no version claimed) */
10105 scsi_ulto2b(0x0600, inq_ptr->version4);
10107 switch (lun->be_lun->lun_type) {
10109 /* SBC-4 (no version claimed) */
10110 scsi_ulto2b(0x0600, inq_ptr->version4);
10115 /* MMC-6 (no version claimed) */
10116 scsi_ulto2b(0x04E0, inq_ptr->version4);
10123 ctl_set_success(ctsio);
10124 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10125 ctsio->be_move_done = ctl_config_move_done;
10126 ctl_datamove((union ctl_io *)ctsio);
10127 return (CTL_RETVAL_COMPLETE);
10131 ctl_inquiry(struct ctl_scsiio *ctsio)
10133 struct scsi_inquiry *cdb;
10136 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10138 cdb = (struct scsi_inquiry *)ctsio->cdb;
10139 if (cdb->byte2 & SI_EVPD)
10140 retval = ctl_inquiry_evpd(ctsio);
10141 else if (cdb->page_code == 0)
10142 retval = ctl_inquiry_std(ctsio);
10144 ctl_set_invalid_field(ctsio,
10150 ctl_done((union ctl_io *)ctsio);
10151 return (CTL_RETVAL_COMPLETE);
10158 ctl_get_config(struct ctl_scsiio *ctsio)
10160 struct ctl_lun *lun = CTL_LUN(ctsio);
10161 struct scsi_get_config_header *hdr;
10162 struct scsi_get_config_feature *feature;
10163 struct scsi_get_config *cdb;
10164 uint32_t alloc_len, data_len;
10167 cdb = (struct scsi_get_config *)ctsio->cdb;
10168 rt = (cdb->rt & SGC_RT_MASK);
10169 starting = scsi_2btoul(cdb->starting_feature);
10170 alloc_len = scsi_2btoul(cdb->length);
10172 data_len = sizeof(struct scsi_get_config_header) +
10173 sizeof(struct scsi_get_config_feature) + 8 +
10174 sizeof(struct scsi_get_config_feature) + 8 +
10175 sizeof(struct scsi_get_config_feature) + 4 +
10176 sizeof(struct scsi_get_config_feature) + 4 +
10177 sizeof(struct scsi_get_config_feature) + 8 +
10178 sizeof(struct scsi_get_config_feature) +
10179 sizeof(struct scsi_get_config_feature) + 4 +
10180 sizeof(struct scsi_get_config_feature) + 4 +
10181 sizeof(struct scsi_get_config_feature) + 4 +
10182 sizeof(struct scsi_get_config_feature) + 4 +
10183 sizeof(struct scsi_get_config_feature) + 4 +
10184 sizeof(struct scsi_get_config_feature) + 4;
10185 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10186 ctsio->kern_sg_entries = 0;
10187 ctsio->kern_rel_offset = 0;
10189 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr;
10190 if (lun->flags & CTL_LUN_NO_MEDIA)
10191 scsi_ulto2b(0x0000, hdr->current_profile);
10193 scsi_ulto2b(0x0010, hdr->current_profile);
10194 feature = (struct scsi_get_config_feature *)(hdr + 1);
10196 if (starting > 0x003b)
10198 if (starting > 0x003a)
10200 if (starting > 0x002b)
10202 if (starting > 0x002a)
10204 if (starting > 0x001f)
10206 if (starting > 0x001e)
10208 if (starting > 0x001d)
10210 if (starting > 0x0010)
10212 if (starting > 0x0003)
10214 if (starting > 0x0002)
10216 if (starting > 0x0001)
10218 if (starting > 0x0000)
10222 scsi_ulto2b(0x0000, feature->feature_code);
10223 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT;
10224 feature->add_length = 8;
10225 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */
10226 feature->feature_data[2] = 0x00;
10227 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */
10228 feature->feature_data[6] = 0x01;
10229 feature = (struct scsi_get_config_feature *)
10230 &feature->feature_data[feature->add_length];
10233 scsi_ulto2b(0x0001, feature->feature_code);
10234 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10235 feature->add_length = 8;
10236 scsi_ulto4b(0x00000000, &feature->feature_data[0]);
10237 feature->feature_data[4] = 0x03;
10238 feature = (struct scsi_get_config_feature *)
10239 &feature->feature_data[feature->add_length];
10242 scsi_ulto2b(0x0002, feature->feature_code);
10243 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10244 feature->add_length = 4;
10245 feature->feature_data[0] = 0x02;
10246 feature = (struct scsi_get_config_feature *)
10247 &feature->feature_data[feature->add_length];
10249 f3: /* Removable Medium */
10250 scsi_ulto2b(0x0003, feature->feature_code);
10251 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10252 feature->add_length = 4;
10253 feature->feature_data[0] = 0x39;
10254 feature = (struct scsi_get_config_feature *)
10255 &feature->feature_data[feature->add_length];
10257 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA))
10260 f10: /* Random Read */
10261 scsi_ulto2b(0x0010, feature->feature_code);
10262 feature->flags = 0x00;
10263 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10264 feature->flags |= SGC_F_CURRENT;
10265 feature->add_length = 8;
10266 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]);
10267 scsi_ulto2b(1, &feature->feature_data[4]);
10268 feature->feature_data[6] = 0x00;
10269 feature = (struct scsi_get_config_feature *)
10270 &feature->feature_data[feature->add_length];
10272 f1d: /* Multi-Read */
10273 scsi_ulto2b(0x001D, feature->feature_code);
10274 feature->flags = 0x00;
10275 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10276 feature->flags |= SGC_F_CURRENT;
10277 feature->add_length = 0;
10278 feature = (struct scsi_get_config_feature *)
10279 &feature->feature_data[feature->add_length];
10282 scsi_ulto2b(0x001E, feature->feature_code);
10283 feature->flags = 0x00;
10284 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10285 feature->flags |= SGC_F_CURRENT;
10286 feature->add_length = 4;
10287 feature->feature_data[0] = 0x00;
10288 feature = (struct scsi_get_config_feature *)
10289 &feature->feature_data[feature->add_length];
10291 f1f: /* DVD Read */
10292 scsi_ulto2b(0x001F, feature->feature_code);
10293 feature->flags = 0x08;
10294 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10295 feature->flags |= SGC_F_CURRENT;
10296 feature->add_length = 4;
10297 feature->feature_data[0] = 0x01;
10298 feature->feature_data[2] = 0x03;
10299 feature = (struct scsi_get_config_feature *)
10300 &feature->feature_data[feature->add_length];
10303 scsi_ulto2b(0x002A, feature->feature_code);
10304 feature->flags = 0x04;
10305 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10306 feature->flags |= SGC_F_CURRENT;
10307 feature->add_length = 4;
10308 feature->feature_data[0] = 0x00;
10309 feature->feature_data[1] = 0x00;
10310 feature = (struct scsi_get_config_feature *)
10311 &feature->feature_data[feature->add_length];
10314 scsi_ulto2b(0x002B, feature->feature_code);
10315 feature->flags = 0x00;
10316 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10317 feature->flags |= SGC_F_CURRENT;
10318 feature->add_length = 4;
10319 feature->feature_data[0] = 0x00;
10320 feature = (struct scsi_get_config_feature *)
10321 &feature->feature_data[feature->add_length];
10323 f3a: /* DVD+RW Dual Layer */
10324 scsi_ulto2b(0x003A, feature->feature_code);
10325 feature->flags = 0x00;
10326 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10327 feature->flags |= SGC_F_CURRENT;
10328 feature->add_length = 4;
10329 feature->feature_data[0] = 0x00;
10330 feature->feature_data[1] = 0x00;
10331 feature = (struct scsi_get_config_feature *)
10332 &feature->feature_data[feature->add_length];
10334 f3b: /* DVD+R Dual Layer */
10335 scsi_ulto2b(0x003B, feature->feature_code);
10336 feature->flags = 0x00;
10337 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10338 feature->flags |= SGC_F_CURRENT;
10339 feature->add_length = 4;
10340 feature->feature_data[0] = 0x00;
10341 feature = (struct scsi_get_config_feature *)
10342 &feature->feature_data[feature->add_length];
10345 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10346 if (rt == SGC_RT_SPECIFIC && data_len > 4) {
10347 feature = (struct scsi_get_config_feature *)(hdr + 1);
10348 if (scsi_2btoul(feature->feature_code) == starting)
10349 feature = (struct scsi_get_config_feature *)
10350 &feature->feature_data[feature->add_length];
10351 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10353 scsi_ulto4b(data_len - 4, hdr->data_length);
10354 ctsio->kern_data_len = min(data_len, alloc_len);
10355 ctsio->kern_total_len = ctsio->kern_data_len;
10357 ctl_set_success(ctsio);
10358 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10359 ctsio->be_move_done = ctl_config_move_done;
10360 ctl_datamove((union ctl_io *)ctsio);
10361 return (CTL_RETVAL_COMPLETE);
10365 ctl_get_event_status(struct ctl_scsiio *ctsio)
10367 struct scsi_get_event_status_header *hdr;
10368 struct scsi_get_event_status *cdb;
10369 uint32_t alloc_len, data_len;
10372 cdb = (struct scsi_get_event_status *)ctsio->cdb;
10373 if ((cdb->byte2 & SGESN_POLLED) == 0) {
10374 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
10375 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
10376 ctl_done((union ctl_io *)ctsio);
10377 return (CTL_RETVAL_COMPLETE);
10379 notif_class = cdb->notif_class;
10380 alloc_len = scsi_2btoul(cdb->length);
10382 data_len = sizeof(struct scsi_get_event_status_header);
10383 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10384 ctsio->kern_sg_entries = 0;
10385 ctsio->kern_rel_offset = 0;
10386 ctsio->kern_data_len = min(data_len, alloc_len);
10387 ctsio->kern_total_len = ctsio->kern_data_len;
10389 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr;
10390 scsi_ulto2b(0, hdr->descr_length);
10391 hdr->nea_class = SGESN_NEA;
10392 hdr->supported_class = 0;
10394 ctl_set_success(ctsio);
10395 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10396 ctsio->be_move_done = ctl_config_move_done;
10397 ctl_datamove((union ctl_io *)ctsio);
10398 return (CTL_RETVAL_COMPLETE);
10402 ctl_mechanism_status(struct ctl_scsiio *ctsio)
10404 struct scsi_mechanism_status_header *hdr;
10405 struct scsi_mechanism_status *cdb;
10406 uint32_t alloc_len, data_len;
10408 cdb = (struct scsi_mechanism_status *)ctsio->cdb;
10409 alloc_len = scsi_2btoul(cdb->length);
10411 data_len = sizeof(struct scsi_mechanism_status_header);
10412 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10413 ctsio->kern_sg_entries = 0;
10414 ctsio->kern_rel_offset = 0;
10415 ctsio->kern_data_len = min(data_len, alloc_len);
10416 ctsio->kern_total_len = ctsio->kern_data_len;
10418 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr;
10419 hdr->state1 = 0x00;
10420 hdr->state2 = 0xe0;
10421 scsi_ulto3b(0, hdr->lba);
10422 hdr->slots_num = 0;
10423 scsi_ulto2b(0, hdr->slots_length);
10425 ctl_set_success(ctsio);
10426 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10427 ctsio->be_move_done = ctl_config_move_done;
10428 ctl_datamove((union ctl_io *)ctsio);
10429 return (CTL_RETVAL_COMPLETE);
10433 ctl_ultomsf(uint32_t lba, uint8_t *buf)
10438 buf[1] = bin2bcd((lba / 75) / 60);
10439 buf[2] = bin2bcd((lba / 75) % 60);
10440 buf[3] = bin2bcd(lba % 75);
10444 ctl_read_toc(struct ctl_scsiio *ctsio)
10446 struct ctl_lun *lun = CTL_LUN(ctsio);
10447 struct scsi_read_toc_hdr *hdr;
10448 struct scsi_read_toc_type01_descr *descr;
10449 struct scsi_read_toc *cdb;
10450 uint32_t alloc_len, data_len;
10453 cdb = (struct scsi_read_toc *)ctsio->cdb;
10454 msf = (cdb->byte2 & CD_MSF) != 0;
10455 format = cdb->format;
10456 alloc_len = scsi_2btoul(cdb->data_len);
10458 data_len = sizeof(struct scsi_read_toc_hdr);
10460 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr);
10462 data_len += sizeof(struct scsi_read_toc_type01_descr);
10463 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10464 ctsio->kern_sg_entries = 0;
10465 ctsio->kern_rel_offset = 0;
10466 ctsio->kern_data_len = min(data_len, alloc_len);
10467 ctsio->kern_total_len = ctsio->kern_data_len;
10469 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr;
10471 scsi_ulto2b(0x12, hdr->data_length);
10474 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10475 descr->addr_ctl = 0x14;
10476 descr->track_number = 1;
10478 ctl_ultomsf(0, descr->track_start);
10480 scsi_ulto4b(0, descr->track_start);
10482 descr->addr_ctl = 0x14;
10483 descr->track_number = 0xaa;
10485 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start);
10487 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start);
10489 scsi_ulto2b(0x0a, hdr->data_length);
10492 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10493 descr->addr_ctl = 0x14;
10494 descr->track_number = 1;
10496 ctl_ultomsf(0, descr->track_start);
10498 scsi_ulto4b(0, descr->track_start);
10501 ctl_set_success(ctsio);
10502 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10503 ctsio->be_move_done = ctl_config_move_done;
10504 ctl_datamove((union ctl_io *)ctsio);
10505 return (CTL_RETVAL_COMPLETE);
10509 * For known CDB types, parse the LBA and length.
10512 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10514 if (io->io_hdr.io_type != CTL_IO_SCSI)
10517 switch (io->scsiio.cdb[0]) {
10518 case COMPARE_AND_WRITE: {
10519 struct scsi_compare_and_write *cdb;
10521 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10523 *lba = scsi_8btou64(cdb->addr);
10524 *len = cdb->length;
10529 struct scsi_rw_6 *cdb;
10531 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10533 *lba = scsi_3btoul(cdb->addr);
10534 /* only 5 bits are valid in the most significant address byte */
10536 *len = cdb->length;
10541 struct scsi_rw_10 *cdb;
10543 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10545 *lba = scsi_4btoul(cdb->addr);
10546 *len = scsi_2btoul(cdb->length);
10549 case WRITE_VERIFY_10: {
10550 struct scsi_write_verify_10 *cdb;
10552 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10554 *lba = scsi_4btoul(cdb->addr);
10555 *len = scsi_2btoul(cdb->length);
10560 struct scsi_rw_12 *cdb;
10562 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10564 *lba = scsi_4btoul(cdb->addr);
10565 *len = scsi_4btoul(cdb->length);
10568 case WRITE_VERIFY_12: {
10569 struct scsi_write_verify_12 *cdb;
10571 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10573 *lba = scsi_4btoul(cdb->addr);
10574 *len = scsi_4btoul(cdb->length);
10579 struct scsi_rw_16 *cdb;
10581 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10583 *lba = scsi_8btou64(cdb->addr);
10584 *len = scsi_4btoul(cdb->length);
10587 case WRITE_ATOMIC_16: {
10588 struct scsi_write_atomic_16 *cdb;
10590 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb;
10592 *lba = scsi_8btou64(cdb->addr);
10593 *len = scsi_2btoul(cdb->length);
10596 case WRITE_VERIFY_16: {
10597 struct scsi_write_verify_16 *cdb;
10599 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10601 *lba = scsi_8btou64(cdb->addr);
10602 *len = scsi_4btoul(cdb->length);
10605 case WRITE_SAME_10: {
10606 struct scsi_write_same_10 *cdb;
10608 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10610 *lba = scsi_4btoul(cdb->addr);
10611 *len = scsi_2btoul(cdb->length);
10614 case WRITE_SAME_16: {
10615 struct scsi_write_same_16 *cdb;
10617 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10619 *lba = scsi_8btou64(cdb->addr);
10620 *len = scsi_4btoul(cdb->length);
10624 struct scsi_verify_10 *cdb;
10626 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10628 *lba = scsi_4btoul(cdb->addr);
10629 *len = scsi_2btoul(cdb->length);
10633 struct scsi_verify_12 *cdb;
10635 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10637 *lba = scsi_4btoul(cdb->addr);
10638 *len = scsi_4btoul(cdb->length);
10642 struct scsi_verify_16 *cdb;
10644 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10646 *lba = scsi_8btou64(cdb->addr);
10647 *len = scsi_4btoul(cdb->length);
10655 case SERVICE_ACTION_IN: { /* GET LBA STATUS */
10656 struct scsi_get_lba_status *cdb;
10658 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10659 *lba = scsi_8btou64(cdb->addr);
10665 break; /* NOTREACHED */
10672 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10675 uint64_t endlba1, endlba2;
10677 endlba1 = lba1 + len1 - (seq ? 0 : 1);
10678 endlba2 = lba2 + len2 - 1;
10680 if ((endlba1 < lba2) || (endlba2 < lba1))
10681 return (CTL_ACTION_PASS);
10683 return (CTL_ACTION_BLOCK);
10687 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10689 struct ctl_ptr_len_flags *ptrlen;
10690 struct scsi_unmap_desc *buf, *end, *range;
10694 /* If not UNMAP -- go other way. */
10695 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10696 io->scsiio.cdb[0] != UNMAP)
10697 return (CTL_ACTION_ERROR);
10699 /* If UNMAP without data -- block and wait for data. */
10700 ptrlen = (struct ctl_ptr_len_flags *)
10701 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10702 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10703 ptrlen->ptr == NULL)
10704 return (CTL_ACTION_BLOCK);
10706 /* UNMAP with data -- check for collision. */
10707 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10708 end = buf + ptrlen->len / sizeof(*buf);
10709 for (range = buf; range < end; range++) {
10710 lba = scsi_8btou64(range->lba);
10711 len = scsi_4btoul(range->length);
10712 if ((lba < lba2 + len2) && (lba + len > lba2))
10713 return (CTL_ACTION_BLOCK);
10715 return (CTL_ACTION_PASS);
10719 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10721 uint64_t lba1, lba2;
10722 uint64_t len1, len2;
10725 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10726 return (CTL_ACTION_ERROR);
10728 retval = ctl_extent_check_unmap(io1, lba2, len2);
10729 if (retval != CTL_ACTION_ERROR)
10732 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10733 return (CTL_ACTION_ERROR);
10735 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10737 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
10741 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2)
10743 uint64_t lba1, lba2;
10744 uint64_t len1, len2;
10746 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10747 return (CTL_ACTION_PASS);
10748 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10749 return (CTL_ACTION_ERROR);
10750 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10751 return (CTL_ACTION_ERROR);
10753 if (lba1 + len1 == lba2)
10754 return (CTL_ACTION_BLOCK);
10755 return (CTL_ACTION_PASS);
10759 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10760 union ctl_io *ooa_io)
10762 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10763 const ctl_serialize_action *serialize_row;
10766 * The initiator attempted multiple untagged commands at the same
10767 * time. Can't do that.
10769 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10770 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10771 && ((pending_io->io_hdr.nexus.targ_port ==
10772 ooa_io->io_hdr.nexus.targ_port)
10773 && (pending_io->io_hdr.nexus.initid ==
10774 ooa_io->io_hdr.nexus.initid))
10775 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10776 CTL_FLAG_STATUS_SENT)) == 0))
10777 return (CTL_ACTION_OVERLAP);
10780 * The initiator attempted to send multiple tagged commands with
10781 * the same ID. (It's fine if different initiators have the same
10784 * Even if all of those conditions are true, we don't kill the I/O
10785 * if the command ahead of us has been aborted. We won't end up
10786 * sending it to the FETD, and it's perfectly legal to resend a
10787 * command with the same tag number as long as the previous
10788 * instance of this tag number has been aborted somehow.
10790 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10791 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10792 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10793 && ((pending_io->io_hdr.nexus.targ_port ==
10794 ooa_io->io_hdr.nexus.targ_port)
10795 && (pending_io->io_hdr.nexus.initid ==
10796 ooa_io->io_hdr.nexus.initid))
10797 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10798 CTL_FLAG_STATUS_SENT)) == 0))
10799 return (CTL_ACTION_OVERLAP_TAG);
10802 * If we get a head of queue tag, SAM-3 says that we should
10803 * immediately execute it.
10805 * What happens if this command would normally block for some other
10806 * reason? e.g. a request sense with a head of queue tag
10807 * immediately after a write. Normally that would block, but this
10808 * will result in its getting executed immediately...
10810 * We currently return "pass" instead of "skip", so we'll end up
10811 * going through the rest of the queue to check for overlapped tags.
10813 * XXX KDM check for other types of blockage first??
10815 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10816 return (CTL_ACTION_PASS);
10819 * Ordered tags have to block until all items ahead of them
10820 * have completed. If we get called with an ordered tag, we always
10821 * block, if something else is ahead of us in the queue.
10823 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10824 return (CTL_ACTION_BLOCK);
10827 * Simple tags get blocked until all head of queue and ordered tags
10828 * ahead of them have completed. I'm lumping untagged commands in
10829 * with simple tags here. XXX KDM is that the right thing to do?
10831 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10832 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10833 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10834 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10835 return (CTL_ACTION_BLOCK);
10837 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
10838 KASSERT(pending_entry->seridx < CTL_SERIDX_COUNT,
10839 ("%s: Invalid seridx %d for pending CDB %02x %02x @ %p",
10840 __func__, pending_entry->seridx, pending_io->scsiio.cdb[0],
10841 pending_io->scsiio.cdb[1], pending_io));
10842 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
10843 if (ooa_entry->seridx == CTL_SERIDX_INVLD)
10844 return (CTL_ACTION_PASS); /* Unsupported command in OOA queue */
10845 KASSERT(ooa_entry->seridx < CTL_SERIDX_COUNT,
10846 ("%s: Invalid seridx %d for ooa CDB %02x %02x @ %p",
10847 __func__, ooa_entry->seridx, ooa_io->scsiio.cdb[0],
10848 ooa_io->scsiio.cdb[1], ooa_io));
10850 serialize_row = ctl_serialize_table[ooa_entry->seridx];
10852 switch (serialize_row[pending_entry->seridx]) {
10853 case CTL_SER_BLOCK:
10854 return (CTL_ACTION_BLOCK);
10855 case CTL_SER_EXTENT:
10856 return (ctl_extent_check(ooa_io, pending_io,
10857 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10858 case CTL_SER_EXTENTOPT:
10859 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
10860 SCP_QUEUE_ALG_UNRESTRICTED)
10861 return (ctl_extent_check(ooa_io, pending_io,
10863 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10864 return (CTL_ACTION_PASS);
10865 case CTL_SER_EXTENTSEQ:
10866 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF)
10867 return (ctl_extent_check_seq(ooa_io, pending_io));
10868 return (CTL_ACTION_PASS);
10870 return (CTL_ACTION_PASS);
10871 case CTL_SER_BLOCKOPT:
10872 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
10873 SCP_QUEUE_ALG_UNRESTRICTED)
10874 return (CTL_ACTION_BLOCK);
10875 return (CTL_ACTION_PASS);
10877 return (CTL_ACTION_SKIP);
10879 panic("%s: Invalid serialization value %d for %d => %d",
10880 __func__, serialize_row[pending_entry->seridx],
10881 pending_entry->seridx, ooa_entry->seridx);
10884 return (CTL_ACTION_ERROR);
10888 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
10890 * - pending_io is generally either incoming, or on the blocked queue
10891 * - starting I/O is the I/O we want to start the check with.
10894 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
10895 union ctl_io *starting_io)
10897 union ctl_io *ooa_io;
10900 mtx_assert(&lun->lun_lock, MA_OWNED);
10903 * Run back along the OOA queue, starting with the current
10904 * blocked I/O and going through every I/O before it on the
10905 * queue. If starting_io is NULL, we'll just end up returning
10908 for (ooa_io = starting_io; ooa_io != NULL;
10909 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
10913 * This routine just checks to see whether
10914 * cur_blocked is blocked by ooa_io, which is ahead
10915 * of it in the queue. It doesn't queue/dequeue
10918 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
10920 case CTL_ACTION_BLOCK:
10921 case CTL_ACTION_OVERLAP:
10922 case CTL_ACTION_OVERLAP_TAG:
10923 case CTL_ACTION_SKIP:
10924 case CTL_ACTION_ERROR:
10926 break; /* NOTREACHED */
10927 case CTL_ACTION_PASS:
10930 panic("%s: Invalid action %d\n", __func__, action);
10934 return (CTL_ACTION_PASS);
10939 * - An I/O has just completed, and has been removed from the per-LUN OOA
10940 * queue, so some items on the blocked queue may now be unblocked.
10943 ctl_check_blocked(struct ctl_lun *lun)
10945 struct ctl_softc *softc = lun->ctl_softc;
10946 union ctl_io *cur_blocked, *next_blocked;
10948 mtx_assert(&lun->lun_lock, MA_OWNED);
10951 * Run forward from the head of the blocked queue, checking each
10952 * entry against the I/Os prior to it on the OOA queue to see if
10953 * there is still any blockage.
10955 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
10956 * with our removing a variable on it while it is traversing the
10959 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
10960 cur_blocked != NULL; cur_blocked = next_blocked) {
10961 union ctl_io *prev_ooa;
10964 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
10967 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
10968 ctl_ooaq, ooa_links);
10971 * If cur_blocked happens to be the first item in the OOA
10972 * queue now, prev_ooa will be NULL, and the action
10973 * returned will just be CTL_ACTION_PASS.
10975 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
10978 case CTL_ACTION_BLOCK:
10979 /* Nothing to do here, still blocked */
10981 case CTL_ACTION_OVERLAP:
10982 case CTL_ACTION_OVERLAP_TAG:
10984 * This shouldn't happen! In theory we've already
10985 * checked this command for overlap...
10988 case CTL_ACTION_PASS:
10989 case CTL_ACTION_SKIP: {
10990 const struct ctl_cmd_entry *entry;
10993 * The skip case shouldn't happen, this transaction
10994 * should have never made it onto the blocked queue.
10997 * This I/O is no longer blocked, we can remove it
10998 * from the blocked queue. Since this is a TAILQ
10999 * (doubly linked list), we can do O(1) removals
11000 * from any place on the list.
11002 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
11004 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11006 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
11007 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){
11009 * Need to send IO back to original side to
11012 union ctl_ha_msg msg_info;
11014 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11015 msg_info.hdr.original_sc =
11016 cur_blocked->io_hdr.original_sc;
11017 msg_info.hdr.serializing_sc = cur_blocked;
11018 msg_info.hdr.msg_type = CTL_MSG_R2R;
11019 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11020 sizeof(msg_info.hdr), M_NOWAIT);
11023 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
11026 * Check this I/O for LUN state changes that may
11027 * have happened while this command was blocked.
11028 * The LUN state may have been changed by a command
11029 * ahead of us in the queue, so we need to re-check
11030 * for any states that can be caused by SCSI
11033 if (ctl_scsiio_lun_check(lun, entry,
11034 &cur_blocked->scsiio) == 0) {
11035 cur_blocked->io_hdr.flags |=
11036 CTL_FLAG_IS_WAS_ON_RTR;
11037 ctl_enqueue_rtr(cur_blocked);
11039 ctl_done(cur_blocked);
11044 * This probably shouldn't happen -- we shouldn't
11045 * get CTL_ACTION_ERROR, or anything else.
11051 return (CTL_RETVAL_COMPLETE);
11055 * This routine (with one exception) checks LUN flags that can be set by
11056 * commands ahead of us in the OOA queue. These flags have to be checked
11057 * when a command initially comes in, and when we pull a command off the
11058 * blocked queue and are preparing to execute it. The reason we have to
11059 * check these flags for commands on the blocked queue is that the LUN
11060 * state may have been changed by a command ahead of us while we're on the
11063 * Ordering is somewhat important with these checks, so please pay
11064 * careful attention to the placement of any new checks.
11067 ctl_scsiio_lun_check(struct ctl_lun *lun,
11068 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11070 struct ctl_softc *softc = lun->ctl_softc;
11076 mtx_assert(&lun->lun_lock, MA_OWNED);
11079 * If this shelf is a secondary shelf controller, we may have to
11080 * reject some commands disallowed by HA mode and link state.
11082 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11083 if (softc->ha_link == CTL_HA_LINK_OFFLINE &&
11084 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11085 ctl_set_lun_unavail(ctsio);
11089 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 &&
11090 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11091 ctl_set_lun_transit(ctsio);
11095 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY &&
11096 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) {
11097 ctl_set_lun_standby(ctsio);
11102 /* The rest of checks are only done on executing side */
11103 if (softc->ha_mode == CTL_HA_MODE_XFER)
11107 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11109 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) {
11110 ctl_set_hw_write_protected(ctsio);
11114 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) {
11115 ctl_set_sense(ctsio, /*current_error*/ 1,
11116 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11117 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11124 * Check for a reservation conflict. If this command isn't allowed
11125 * even on reserved LUNs, and if this initiator isn't the one who
11126 * reserved us, reject the command with a reservation conflict.
11128 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11129 if ((lun->flags & CTL_LUN_RESERVED)
11130 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11131 if (lun->res_idx != residx) {
11132 ctl_set_reservation_conflict(ctsio);
11138 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11139 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11140 /* No reservation or command is allowed. */;
11141 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11142 (lun->pr_res_type == SPR_TYPE_WR_EX ||
11143 lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
11144 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) {
11145 /* The command is allowed for Write Exclusive resv. */;
11148 * if we aren't registered or it's a res holder type
11149 * reservation and this isn't the res holder then set a
11152 if (ctl_get_prkey(lun, residx) == 0 ||
11153 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) {
11154 ctl_set_reservation_conflict(ctsio);
11160 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) {
11161 if (lun->flags & CTL_LUN_EJECTED)
11162 ctl_set_lun_ejected(ctsio);
11163 else if (lun->flags & CTL_LUN_NO_MEDIA) {
11164 if (lun->flags & CTL_LUN_REMOVABLE)
11165 ctl_set_lun_no_media(ctsio);
11167 ctl_set_lun_int_reqd(ctsio);
11168 } else if (lun->flags & CTL_LUN_STOPPED)
11169 ctl_set_lun_stopped(ctsio);
11181 ctl_failover_io(union ctl_io *io, int have_lock)
11183 ctl_set_busy(&io->scsiio);
11188 ctl_failover_lun(union ctl_io *rio)
11190 struct ctl_softc *softc = CTL_SOFTC(rio);
11191 struct ctl_lun *lun;
11192 struct ctl_io_hdr *io, *next_io;
11195 targ_lun = rio->io_hdr.nexus.targ_mapped_lun;
11196 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun));
11198 /* Find and lock the LUN. */
11199 mtx_lock(&softc->ctl_lock);
11200 if (targ_lun > CTL_MAX_LUNS ||
11201 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11202 mtx_unlock(&softc->ctl_lock);
11205 mtx_lock(&lun->lun_lock);
11206 mtx_unlock(&softc->ctl_lock);
11207 if (lun->flags & CTL_LUN_DISABLED) {
11208 mtx_unlock(&lun->lun_lock);
11212 if (softc->ha_mode == CTL_HA_MODE_XFER) {
11213 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11214 /* We are master */
11215 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11216 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11217 io->flags |= CTL_FLAG_ABORT;
11218 io->flags |= CTL_FLAG_FAILOVER;
11219 } else { /* This can be only due to DATAMOVE */
11220 io->msg_type = CTL_MSG_DATAMOVE_DONE;
11221 io->flags &= ~CTL_FLAG_DMA_INPROG;
11222 io->flags |= CTL_FLAG_IO_ACTIVE;
11223 io->port_status = 31340;
11224 ctl_enqueue_isc((union ctl_io *)io);
11228 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11229 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11230 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11231 io->flags |= CTL_FLAG_FAILOVER;
11233 ctl_set_busy(&((union ctl_io *)io)->
11235 ctl_done((union ctl_io *)io);
11239 } else { /* SERIALIZE modes */
11240 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links,
11242 /* We are master */
11243 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11244 TAILQ_REMOVE(&lun->blocked_queue, io,
11246 io->flags &= ~CTL_FLAG_BLOCKED;
11247 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11248 ctl_free_io((union ctl_io *)io);
11251 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11252 /* We are master */
11253 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11254 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11255 ctl_free_io((union ctl_io *)io);
11258 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11259 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11260 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) {
11261 ctl_set_busy(&((union ctl_io *)io)->
11263 ctl_done((union ctl_io *)io);
11267 ctl_check_blocked(lun);
11269 mtx_unlock(&lun->lun_lock);
11273 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio)
11275 struct ctl_lun *lun;
11276 const struct ctl_cmd_entry *entry;
11277 uint32_t initidx, targ_lun;
11281 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11282 if (targ_lun < CTL_MAX_LUNS)
11283 lun = softc->ctl_luns[targ_lun];
11286 * If the LUN is invalid, pretend that it doesn't exist.
11287 * It will go away as soon as all pending I/O has been
11290 mtx_lock(&lun->lun_lock);
11291 if (lun->flags & CTL_LUN_DISABLED) {
11292 mtx_unlock(&lun->lun_lock);
11296 CTL_LUN(ctsio) = lun;
11298 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
11301 * Every I/O goes into the OOA queue for a particular LUN,
11302 * and stays there until completion.
11305 if (TAILQ_EMPTY(&lun->ooa_queue))
11306 lun->idle_time += getsbinuptime() - lun->last_busy;
11308 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
11311 /* Get command entry and return error if it is unsuppotyed. */
11312 entry = ctl_validate_command(ctsio);
11313 if (entry == NULL) {
11315 mtx_unlock(&lun->lun_lock);
11319 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11320 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11323 * Check to see whether we can send this command to LUNs that don't
11324 * exist. This should pretty much only be the case for inquiry
11325 * and request sense. Further checks, below, really require having
11326 * a LUN, so we can't really check the command anymore. Just put
11327 * it on the rtr queue.
11330 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) {
11331 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11332 ctl_enqueue_rtr((union ctl_io *)ctsio);
11336 ctl_set_unsupported_lun(ctsio);
11337 ctl_done((union ctl_io *)ctsio);
11338 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11342 * Make sure we support this particular command on this LUN.
11343 * e.g., we don't support writes to the control LUN.
11345 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11346 mtx_unlock(&lun->lun_lock);
11347 ctl_set_invalid_opcode(ctsio);
11348 ctl_done((union ctl_io *)ctsio);
11353 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11356 * If we've got a request sense, it'll clear the contingent
11357 * allegiance condition. Otherwise, if we have a CA condition for
11358 * this initiator, clear it, because it sent down a command other
11359 * than request sense.
11361 if (ctsio->cdb[0] != REQUEST_SENSE) {
11362 struct scsi_sense_data *ps;
11364 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT];
11366 ps[initidx % CTL_MAX_INIT_PER_PORT].error_code = 0;
11370 * If the command has this flag set, it handles its own unit
11371 * attention reporting, we shouldn't do anything. Otherwise we
11372 * check for any pending unit attentions, and send them back to the
11373 * initiator. We only do this when a command initially comes in,
11374 * not when we pull it off the blocked queue.
11376 * According to SAM-3, section 5.3.2, the order that things get
11377 * presented back to the host is basically unit attentions caused
11378 * by some sort of reset event, busy status, reservation conflicts
11379 * or task set full, and finally any other status.
11381 * One issue here is that some of the unit attentions we report
11382 * don't fall into the "reset" category (e.g. "reported luns data
11383 * has changed"). So reporting it here, before the reservation
11384 * check, may be technically wrong. I guess the only thing to do
11385 * would be to check for and report the reset events here, and then
11386 * check for the other unit attention types after we check for a
11387 * reservation conflict.
11389 * XXX KDM need to fix this
11391 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11392 ctl_ua_type ua_type;
11393 u_int sense_len = 0;
11395 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11396 &sense_len, SSD_TYPE_NONE);
11397 if (ua_type != CTL_UA_NONE) {
11398 mtx_unlock(&lun->lun_lock);
11399 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11400 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11401 ctsio->sense_len = sense_len;
11402 ctl_done((union ctl_io *)ctsio);
11408 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11409 mtx_unlock(&lun->lun_lock);
11410 ctl_done((union ctl_io *)ctsio);
11415 * XXX CHD this is where we want to send IO to other side if
11416 * this LUN is secondary on this SC. We will need to make a copy
11417 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11418 * the copy we send as FROM_OTHER.
11419 * We also need to stuff the address of the original IO so we can
11420 * find it easily. Something similar will need be done on the other
11421 * side so when we are done we can find the copy.
11423 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
11424 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 &&
11425 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) {
11426 union ctl_ha_msg msg_info;
11429 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11430 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11431 mtx_unlock(&lun->lun_lock);
11433 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11434 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11435 msg_info.hdr.serializing_sc = NULL;
11436 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11437 msg_info.scsi.tag_num = ctsio->tag_num;
11438 msg_info.scsi.tag_type = ctsio->tag_type;
11439 msg_info.scsi.cdb_len = ctsio->cdb_len;
11440 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11442 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11443 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data),
11444 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) {
11445 ctl_set_busy(ctsio);
11446 ctl_done((union ctl_io *)ctsio);
11452 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11453 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11454 ctl_ooaq, ooa_links))) {
11455 case CTL_ACTION_BLOCK:
11456 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11457 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11459 mtx_unlock(&lun->lun_lock);
11461 case CTL_ACTION_PASS:
11462 case CTL_ACTION_SKIP:
11463 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11464 mtx_unlock(&lun->lun_lock);
11465 ctl_enqueue_rtr((union ctl_io *)ctsio);
11467 case CTL_ACTION_OVERLAP:
11468 mtx_unlock(&lun->lun_lock);
11469 ctl_set_overlapped_cmd(ctsio);
11470 ctl_done((union ctl_io *)ctsio);
11472 case CTL_ACTION_OVERLAP_TAG:
11473 mtx_unlock(&lun->lun_lock);
11474 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11475 ctl_done((union ctl_io *)ctsio);
11477 case CTL_ACTION_ERROR:
11479 mtx_unlock(&lun->lun_lock);
11480 ctl_set_internal_failure(ctsio,
11482 /*retry_count*/ 0);
11483 ctl_done((union ctl_io *)ctsio);
11489 const struct ctl_cmd_entry *
11490 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11492 const struct ctl_cmd_entry *entry;
11493 int service_action;
11495 entry = &ctl_cmd_table[ctsio->cdb[0]];
11497 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11498 if (entry->flags & CTL_CMD_FLAG_SA5) {
11499 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11500 entry = &((const struct ctl_cmd_entry *)
11501 entry->execute)[service_action];
11506 const struct ctl_cmd_entry *
11507 ctl_validate_command(struct ctl_scsiio *ctsio)
11509 const struct ctl_cmd_entry *entry;
11513 entry = ctl_get_cmd_entry(ctsio, &sa);
11514 if (entry->execute == NULL) {
11516 ctl_set_invalid_field(ctsio,
11523 ctl_set_invalid_opcode(ctsio);
11524 ctl_done((union ctl_io *)ctsio);
11527 KASSERT(entry->length > 0,
11528 ("Not defined length for command 0x%02x/0x%02x",
11529 ctsio->cdb[0], ctsio->cdb[1]));
11530 for (i = 1; i < entry->length; i++) {
11531 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11534 ctl_set_invalid_field(ctsio,
11539 /*bit*/ fls(diff) - 1);
11540 ctl_done((union ctl_io *)ctsio);
11547 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11550 switch (lun_type) {
11552 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0)
11556 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
11560 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0)
11570 ctl_scsiio(struct ctl_scsiio *ctsio)
11573 const struct ctl_cmd_entry *entry;
11575 retval = CTL_RETVAL_COMPLETE;
11577 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11579 entry = ctl_get_cmd_entry(ctsio, NULL);
11582 * If this I/O has been aborted, just send it straight to
11583 * ctl_done() without executing it.
11585 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11586 ctl_done((union ctl_io *)ctsio);
11591 * All the checks should have been handled by ctl_scsiio_precheck().
11592 * We should be clear now to just execute the I/O.
11594 retval = entry->execute(ctsio);
11601 ctl_target_reset(union ctl_io *io)
11603 struct ctl_softc *softc = CTL_SOFTC(io);
11604 struct ctl_port *port = CTL_PORT(io);
11605 struct ctl_lun *lun;
11607 ctl_ua_type ua_type;
11609 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11610 union ctl_ha_msg msg_info;
11612 msg_info.hdr.nexus = io->io_hdr.nexus;
11613 msg_info.task.task_action = io->taskio.task_action;
11614 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11615 msg_info.hdr.original_sc = NULL;
11616 msg_info.hdr.serializing_sc = NULL;
11617 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11618 sizeof(msg_info.task), M_WAITOK);
11621 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11622 if (io->taskio.task_action == CTL_TASK_TARGET_RESET)
11623 ua_type = CTL_UA_TARG_RESET;
11625 ua_type = CTL_UA_BUS_RESET;
11626 mtx_lock(&softc->ctl_lock);
11627 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11628 if (port != NULL &&
11629 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
11631 ctl_do_lun_reset(lun, initidx, ua_type);
11633 mtx_unlock(&softc->ctl_lock);
11634 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11639 * The LUN should always be set. The I/O is optional, and is used to
11640 * distinguish between I/Os sent by this initiator, and by other
11641 * initiators. We set unit attention for initiators other than this one.
11642 * SAM-3 is vague on this point. It does say that a unit attention should
11643 * be established for other initiators when a LUN is reset (see section
11644 * 5.7.3), but it doesn't specifically say that the unit attention should
11645 * be established for this particular initiator when a LUN is reset. Here
11646 * is the relevant text, from SAM-3 rev 8:
11648 * 5.7.2 When a SCSI initiator port aborts its own tasks
11650 * When a SCSI initiator port causes its own task(s) to be aborted, no
11651 * notification that the task(s) have been aborted shall be returned to
11652 * the SCSI initiator port other than the completion response for the
11653 * command or task management function action that caused the task(s) to
11654 * be aborted and notification(s) associated with related effects of the
11655 * action (e.g., a reset unit attention condition).
11657 * XXX KDM for now, we're setting unit attention for all initiators.
11660 ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua_type)
11665 mtx_lock(&lun->lun_lock);
11667 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11668 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11669 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11672 for (i = 0; i < CTL_MAX_PORTS; i++) {
11673 free(lun->pending_sense[i], M_CTL);
11674 lun->pending_sense[i] = NULL;
11676 /* Clear reservation. */
11677 lun->flags &= ~CTL_LUN_RESERVED;
11678 /* Clear prevent media removal. */
11679 if (lun->prevent) {
11680 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11681 ctl_clear_mask(lun->prevent, i);
11682 lun->prevent_count = 0;
11684 /* Clear TPC status */
11685 ctl_tpc_lun_clear(lun, -1);
11686 /* Establish UA. */
11688 ctl_est_ua_all(lun, initidx, ua_type);
11690 ctl_est_ua_all(lun, -1, ua_type);
11692 mtx_unlock(&lun->lun_lock);
11696 ctl_lun_reset(union ctl_io *io)
11698 struct ctl_softc *softc = CTL_SOFTC(io);
11699 struct ctl_lun *lun;
11700 uint32_t targ_lun, initidx;
11702 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11703 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11704 mtx_lock(&softc->ctl_lock);
11705 if (targ_lun >= CTL_MAX_LUNS ||
11706 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11707 mtx_unlock(&softc->ctl_lock);
11708 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11711 ctl_do_lun_reset(lun, initidx, CTL_UA_LUN_RESET);
11712 mtx_unlock(&softc->ctl_lock);
11713 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11715 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) {
11716 union ctl_ha_msg msg_info;
11718 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11719 msg_info.hdr.nexus = io->io_hdr.nexus;
11720 msg_info.task.task_action = CTL_TASK_LUN_RESET;
11721 msg_info.hdr.original_sc = NULL;
11722 msg_info.hdr.serializing_sc = NULL;
11723 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11724 sizeof(msg_info.task), M_WAITOK);
11730 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11735 mtx_assert(&lun->lun_lock, MA_OWNED);
11738 * Run through the OOA queue and attempt to find the given I/O.
11739 * The target port, initiator ID, tag type and tag number have to
11740 * match the values that we got from the initiator. If we have an
11741 * untagged command to abort, simply abort the first untagged command
11742 * we come to. We only allow one untagged command at a time of course.
11744 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11745 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11747 if ((targ_port == UINT32_MAX ||
11748 targ_port == xio->io_hdr.nexus.targ_port) &&
11749 (init_id == UINT32_MAX ||
11750 init_id == xio->io_hdr.nexus.initid)) {
11751 if (targ_port != xio->io_hdr.nexus.targ_port ||
11752 init_id != xio->io_hdr.nexus.initid)
11753 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
11754 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11755 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11756 union ctl_ha_msg msg_info;
11758 msg_info.hdr.nexus = xio->io_hdr.nexus;
11759 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11760 msg_info.task.tag_num = xio->scsiio.tag_num;
11761 msg_info.task.tag_type = xio->scsiio.tag_type;
11762 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11763 msg_info.hdr.original_sc = NULL;
11764 msg_info.hdr.serializing_sc = NULL;
11765 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11766 sizeof(msg_info.task), M_NOWAIT);
11773 ctl_abort_task_set(union ctl_io *io)
11775 struct ctl_softc *softc = CTL_SOFTC(io);
11776 struct ctl_lun *lun;
11782 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11783 mtx_lock(&softc->ctl_lock);
11784 if (targ_lun >= CTL_MAX_LUNS ||
11785 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11786 mtx_unlock(&softc->ctl_lock);
11787 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11791 mtx_lock(&lun->lun_lock);
11792 mtx_unlock(&softc->ctl_lock);
11793 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
11794 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11795 io->io_hdr.nexus.initid,
11796 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11797 } else { /* CTL_TASK_CLEAR_TASK_SET */
11798 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
11799 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11801 mtx_unlock(&lun->lun_lock);
11802 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11807 ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx,
11808 ctl_ua_type ua_type)
11810 struct ctl_lun *lun;
11811 struct scsi_sense_data *ps;
11814 p = initidx / CTL_MAX_INIT_PER_PORT;
11815 i = initidx % CTL_MAX_INIT_PER_PORT;
11816 mtx_lock(&softc->ctl_lock);
11817 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11818 mtx_lock(&lun->lun_lock);
11820 ctl_abort_tasks_lun(lun, p, i, 1);
11822 ps = lun->pending_sense[p];
11824 ps[i].error_code = 0;
11825 /* Clear reservation. */
11826 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx))
11827 lun->flags &= ~CTL_LUN_RESERVED;
11828 /* Clear prevent media removal. */
11829 if (lun->prevent && ctl_is_set(lun->prevent, initidx)) {
11830 ctl_clear_mask(lun->prevent, initidx);
11831 lun->prevent_count--;
11833 /* Clear TPC status */
11834 ctl_tpc_lun_clear(lun, initidx);
11835 /* Establish UA. */
11836 ctl_est_ua(lun, initidx, ua_type);
11837 mtx_unlock(&lun->lun_lock);
11839 mtx_unlock(&softc->ctl_lock);
11843 ctl_i_t_nexus_reset(union ctl_io *io)
11845 struct ctl_softc *softc = CTL_SOFTC(io);
11848 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11849 union ctl_ha_msg msg_info;
11851 msg_info.hdr.nexus = io->io_hdr.nexus;
11852 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET;
11853 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11854 msg_info.hdr.original_sc = NULL;
11855 msg_info.hdr.serializing_sc = NULL;
11856 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11857 sizeof(msg_info.task), M_WAITOK);
11860 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11861 ctl_i_t_nexus_loss(softc, initidx, CTL_UA_I_T_NEXUS_LOSS);
11862 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11867 ctl_abort_task(union ctl_io *io)
11869 struct ctl_softc *softc = CTL_SOFTC(io);
11871 struct ctl_lun *lun;
11874 char printbuf[128];
11884 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11885 mtx_lock(&softc->ctl_lock);
11886 if (targ_lun >= CTL_MAX_LUNS ||
11887 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11888 mtx_unlock(&softc->ctl_lock);
11889 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11894 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
11895 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
11898 mtx_lock(&lun->lun_lock);
11899 mtx_unlock(&softc->ctl_lock);
11901 * Run through the OOA queue and attempt to find the given I/O.
11902 * The target port, initiator ID, tag type and tag number have to
11903 * match the values that we got from the initiator. If we have an
11904 * untagged command to abort, simply abort the first untagged command
11905 * we come to. We only allow one untagged command at a time of course.
11907 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11908 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11910 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
11912 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
11913 lun->lun, xio->scsiio.tag_num,
11914 xio->scsiio.tag_type,
11915 (xio->io_hdr.blocked_links.tqe_prev
11916 == NULL) ? "" : " BLOCKED",
11917 (xio->io_hdr.flags &
11918 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
11919 (xio->io_hdr.flags &
11920 CTL_FLAG_ABORT) ? " ABORT" : "",
11921 (xio->io_hdr.flags &
11922 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
11923 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
11925 printf("%s\n", sbuf_data(&sb));
11928 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
11929 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
11930 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
11934 * If the abort says that the task is untagged, the
11935 * task in the queue must be untagged. Otherwise,
11936 * we just check to see whether the tag numbers
11937 * match. This is because the QLogic firmware
11938 * doesn't pass back the tag type in an abort
11942 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
11943 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
11944 || (xio->scsiio.tag_num == io->taskio.tag_num))
11947 * XXX KDM we've got problems with FC, because it
11948 * doesn't send down a tag type with aborts. So we
11949 * can only really go by the tag number...
11950 * This may cause problems with parallel SCSI.
11951 * Need to figure that out!!
11953 if (xio->scsiio.tag_num == io->taskio.tag_num) {
11954 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11956 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
11957 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11958 union ctl_ha_msg msg_info;
11960 msg_info.hdr.nexus = io->io_hdr.nexus;
11961 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11962 msg_info.task.tag_num = io->taskio.tag_num;
11963 msg_info.task.tag_type = io->taskio.tag_type;
11964 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11965 msg_info.hdr.original_sc = NULL;
11966 msg_info.hdr.serializing_sc = NULL;
11968 printf("Sent Abort to other side\n");
11970 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11971 sizeof(msg_info.task), M_NOWAIT);
11974 printf("ctl_abort_task: found I/O to abort\n");
11978 mtx_unlock(&lun->lun_lock);
11982 * This isn't really an error. It's entirely possible for
11983 * the abort and command completion to cross on the wire.
11984 * This is more of an informative/diagnostic error.
11987 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
11988 "%u:%u:%u tag %d type %d\n",
11989 io->io_hdr.nexus.initid,
11990 io->io_hdr.nexus.targ_port,
11991 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
11992 io->taskio.tag_type);
11995 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12000 ctl_query_task(union ctl_io *io, int task_set)
12002 struct ctl_softc *softc = CTL_SOFTC(io);
12004 struct ctl_lun *lun;
12008 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12009 mtx_lock(&softc->ctl_lock);
12010 if (targ_lun >= CTL_MAX_LUNS ||
12011 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12012 mtx_unlock(&softc->ctl_lock);
12013 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12016 mtx_lock(&lun->lun_lock);
12017 mtx_unlock(&softc->ctl_lock);
12018 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12019 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12021 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12022 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12023 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12026 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) {
12031 mtx_unlock(&lun->lun_lock);
12033 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12035 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12040 ctl_query_async_event(union ctl_io *io)
12042 struct ctl_softc *softc = CTL_SOFTC(io);
12043 struct ctl_lun *lun;
12045 uint32_t targ_lun, initidx;
12047 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12048 mtx_lock(&softc->ctl_lock);
12049 if (targ_lun >= CTL_MAX_LUNS ||
12050 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12051 mtx_unlock(&softc->ctl_lock);
12052 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12055 mtx_lock(&lun->lun_lock);
12056 mtx_unlock(&softc->ctl_lock);
12057 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12058 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp);
12059 mtx_unlock(&lun->lun_lock);
12060 if (ua != CTL_UA_NONE)
12061 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12063 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12068 ctl_run_task(union ctl_io *io)
12072 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12073 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12074 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type));
12075 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED;
12076 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp));
12077 switch (io->taskio.task_action) {
12078 case CTL_TASK_ABORT_TASK:
12079 retval = ctl_abort_task(io);
12081 case CTL_TASK_ABORT_TASK_SET:
12082 case CTL_TASK_CLEAR_TASK_SET:
12083 retval = ctl_abort_task_set(io);
12085 case CTL_TASK_CLEAR_ACA:
12087 case CTL_TASK_I_T_NEXUS_RESET:
12088 retval = ctl_i_t_nexus_reset(io);
12090 case CTL_TASK_LUN_RESET:
12091 retval = ctl_lun_reset(io);
12093 case CTL_TASK_TARGET_RESET:
12094 case CTL_TASK_BUS_RESET:
12095 retval = ctl_target_reset(io);
12097 case CTL_TASK_PORT_LOGIN:
12099 case CTL_TASK_PORT_LOGOUT:
12101 case CTL_TASK_QUERY_TASK:
12102 retval = ctl_query_task(io, 0);
12104 case CTL_TASK_QUERY_TASK_SET:
12105 retval = ctl_query_task(io, 1);
12107 case CTL_TASK_QUERY_ASYNC_EVENT:
12108 retval = ctl_query_async_event(io);
12111 printf("%s: got unknown task management event %d\n",
12112 __func__, io->taskio.task_action);
12116 io->io_hdr.status = CTL_SUCCESS;
12118 io->io_hdr.status = CTL_ERROR;
12123 * For HA operation. Handle commands that come in from the other
12127 ctl_handle_isc(union ctl_io *io)
12129 struct ctl_softc *softc = CTL_SOFTC(io);
12130 struct ctl_lun *lun;
12131 const struct ctl_cmd_entry *entry;
12134 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12135 switch (io->io_hdr.msg_type) {
12136 case CTL_MSG_SERIALIZE:
12137 ctl_serialize_other_sc_cmd(&io->scsiio);
12139 case CTL_MSG_R2R: /* Only used in SER_ONLY mode. */
12140 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12141 if (targ_lun >= CTL_MAX_LUNS ||
12142 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12146 mtx_lock(&lun->lun_lock);
12147 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) {
12148 mtx_unlock(&lun->lun_lock);
12152 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12153 mtx_unlock(&lun->lun_lock);
12154 ctl_enqueue_rtr(io);
12156 case CTL_MSG_FINISH_IO:
12157 if (softc->ha_mode == CTL_HA_MODE_XFER) {
12161 if (targ_lun >= CTL_MAX_LUNS ||
12162 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12166 mtx_lock(&lun->lun_lock);
12167 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
12168 ctl_check_blocked(lun);
12169 mtx_unlock(&lun->lun_lock);
12172 case CTL_MSG_PERS_ACTION:
12173 ctl_hndl_per_res_out_on_other_sc(io);
12176 case CTL_MSG_BAD_JUJU:
12179 case CTL_MSG_DATAMOVE: /* Only used in XFER mode */
12180 ctl_datamove_remote(io);
12182 case CTL_MSG_DATAMOVE_DONE: /* Only used in XFER mode */
12183 io->scsiio.be_move_done(io);
12185 case CTL_MSG_FAILOVER:
12186 ctl_failover_lun(io);
12190 printf("%s: Invalid message type %d\n",
12191 __func__, io->io_hdr.msg_type);
12200 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12201 * there is no match.
12203 static ctl_lun_error_pattern
12204 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12206 const struct ctl_cmd_entry *entry;
12207 ctl_lun_error_pattern filtered_pattern, pattern;
12209 pattern = desc->error_pattern;
12212 * XXX KDM we need more data passed into this function to match a
12213 * custom pattern, and we actually need to implement custom pattern
12216 if (pattern & CTL_LUN_PAT_CMD)
12217 return (CTL_LUN_PAT_CMD);
12219 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12220 return (CTL_LUN_PAT_ANY);
12222 entry = ctl_get_cmd_entry(ctsio, NULL);
12224 filtered_pattern = entry->pattern & pattern;
12227 * If the user requested specific flags in the pattern (e.g.
12228 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12231 * If the user did not specify any flags, it doesn't matter whether
12232 * or not the command supports the flags.
12234 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12235 (pattern & ~CTL_LUN_PAT_MASK))
12236 return (CTL_LUN_PAT_NONE);
12239 * If the user asked for a range check, see if the requested LBA
12240 * range overlaps with this command's LBA range.
12242 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12248 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12250 return (CTL_LUN_PAT_NONE);
12252 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12253 desc->lba_range.len, FALSE);
12255 * A "pass" means that the LBA ranges don't overlap, so
12256 * this doesn't match the user's range criteria.
12258 if (action == CTL_ACTION_PASS)
12259 return (CTL_LUN_PAT_NONE);
12262 return (filtered_pattern);
12266 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12268 struct ctl_error_desc *desc, *desc2;
12270 mtx_assert(&lun->lun_lock, MA_OWNED);
12272 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12273 ctl_lun_error_pattern pattern;
12275 * Check to see whether this particular command matches
12276 * the pattern in the descriptor.
12278 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12279 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12282 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12283 case CTL_LUN_INJ_ABORTED:
12284 ctl_set_aborted(&io->scsiio);
12286 case CTL_LUN_INJ_MEDIUM_ERR:
12287 ctl_set_medium_error(&io->scsiio,
12288 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) !=
12289 CTL_FLAG_DATA_OUT);
12291 case CTL_LUN_INJ_UA:
12292 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12294 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12296 case CTL_LUN_INJ_CUSTOM:
12298 * We're assuming the user knows what he is doing.
12299 * Just copy the sense information without doing
12302 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12303 MIN(sizeof(desc->custom_sense),
12304 sizeof(io->scsiio.sense_data)));
12305 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12306 io->scsiio.sense_len = SSD_FULL_SIZE;
12307 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12309 case CTL_LUN_INJ_NONE:
12312 * If this is an error injection type we don't know
12313 * about, clear the continuous flag (if it is set)
12314 * so it will get deleted below.
12316 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12320 * By default, each error injection action is a one-shot
12322 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12325 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12331 #ifdef CTL_IO_DELAY
12333 ctl_datamove_timer_wakeup(void *arg)
12337 io = (union ctl_io *)arg;
12341 #endif /* CTL_IO_DELAY */
12344 ctl_datamove(union ctl_io *io)
12346 void (*fe_datamove)(union ctl_io *io);
12348 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
12350 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12352 /* No data transferred yet. Frontend must update this when done. */
12353 io->scsiio.kern_data_resid = io->scsiio.kern_data_len;
12356 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12361 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12362 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12364 sbuf_cat(&sb, path_str);
12365 switch (io->io_hdr.io_type) {
12367 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12368 sbuf_printf(&sb, "\n");
12369 sbuf_cat(&sb, path_str);
12370 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12371 io->scsiio.tag_num, io->scsiio.tag_type);
12374 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12375 "Tag Type: %d\n", io->taskio.task_action,
12376 io->taskio.tag_num, io->taskio.tag_type);
12379 panic("%s: Invalid CTL I/O type %d\n",
12380 __func__, io->io_hdr.io_type);
12382 sbuf_cat(&sb, path_str);
12383 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12384 (intmax_t)time_uptime - io->io_hdr.start_time);
12386 printf("%s", sbuf_data(&sb));
12388 #endif /* CTL_TIME_IO */
12390 #ifdef CTL_IO_DELAY
12391 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12392 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12394 struct ctl_lun *lun;
12398 && (lun->delay_info.datamove_delay > 0)) {
12400 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
12401 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12402 callout_reset(&io->io_hdr.delay_callout,
12403 lun->delay_info.datamove_delay * hz,
12404 ctl_datamove_timer_wakeup, io);
12405 if (lun->delay_info.datamove_type ==
12406 CTL_DELAY_TYPE_ONESHOT)
12407 lun->delay_info.datamove_delay = 0;
12414 * This command has been aborted. Set the port status, so we fail
12417 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12418 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n",
12419 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12420 io->io_hdr.nexus.targ_port,
12421 io->io_hdr.nexus.targ_lun);
12422 io->io_hdr.port_status = 31337;
12424 * Note that the backend, in this case, will get the
12425 * callback in its context. In other cases it may get
12426 * called in the frontend's interrupt thread context.
12428 io->scsiio.be_move_done(io);
12432 /* Don't confuse frontend with zero length data move. */
12433 if (io->scsiio.kern_data_len == 0) {
12434 io->scsiio.be_move_done(io);
12438 fe_datamove = CTL_PORT(io)->fe_datamove;
12443 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12445 union ctl_ha_msg msg;
12447 struct bintime cur_bt;
12450 memset(&msg, 0, sizeof(msg));
12451 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12452 msg.hdr.original_sc = io;
12453 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12454 msg.hdr.nexus = io->io_hdr.nexus;
12455 msg.hdr.status = io->io_hdr.status;
12456 msg.scsi.kern_data_resid = io->scsiio.kern_data_resid;
12457 msg.scsi.tag_num = io->scsiio.tag_num;
12458 msg.scsi.tag_type = io->scsiio.tag_type;
12459 msg.scsi.scsi_status = io->scsiio.scsi_status;
12460 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12461 io->scsiio.sense_len);
12462 msg.scsi.sense_len = io->scsiio.sense_len;
12463 msg.scsi.port_status = io->io_hdr.port_status;
12464 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12465 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12466 ctl_failover_io(io, /*have_lock*/ have_lock);
12469 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12470 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12471 msg.scsi.sense_len, M_WAITOK);
12474 getbinuptime(&cur_bt);
12475 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
12476 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
12478 io->io_hdr.num_dmas++;
12482 * The DMA to the remote side is done, now we need to tell the other side
12483 * we're done so it can continue with its data movement.
12486 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12493 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12494 printf("%s: ISC DMA write failed with error %d", __func__,
12496 ctl_set_internal_failure(&io->scsiio,
12498 /*retry_count*/ rq->ret);
12501 ctl_dt_req_free(rq);
12503 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12504 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12505 free(io->io_hdr.remote_sglist, M_CTL);
12506 io->io_hdr.remote_sglist = NULL;
12507 io->io_hdr.local_sglist = NULL;
12510 * The data is in local and remote memory, so now we need to send
12511 * status (good or back) back to the other side.
12513 ctl_send_datamove_done(io, /*have_lock*/ 0);
12517 * We've moved the data from the host/controller into local memory. Now we
12518 * need to push it over to the remote controller's memory.
12521 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12525 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12526 ctl_datamove_remote_write_cb);
12531 ctl_datamove_remote_write(union ctl_io *io)
12534 void (*fe_datamove)(union ctl_io *io);
12537 * - Get the data from the host/HBA into local memory.
12538 * - DMA memory from the local controller to the remote controller.
12539 * - Send status back to the remote controller.
12542 retval = ctl_datamove_remote_sgl_setup(io);
12546 /* Switch the pointer over so the FETD knows what to do */
12547 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12550 * Use a custom move done callback, since we need to send completion
12551 * back to the other controller, not to the backend on this side.
12553 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12555 fe_datamove = CTL_PORT(io)->fe_datamove;
12560 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12569 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12570 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12571 free(io->io_hdr.remote_sglist, M_CTL);
12572 io->io_hdr.remote_sglist = NULL;
12573 io->io_hdr.local_sglist = NULL;
12576 scsi_path_string(io, path_str, sizeof(path_str));
12577 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12578 sbuf_cat(&sb, path_str);
12579 scsi_command_string(&io->scsiio, NULL, &sb);
12580 sbuf_printf(&sb, "\n");
12581 sbuf_cat(&sb, path_str);
12582 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12583 io->scsiio.tag_num, io->scsiio.tag_type);
12584 sbuf_cat(&sb, path_str);
12585 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12586 io->io_hdr.flags, io->io_hdr.status);
12588 printk("%s", sbuf_data(&sb));
12593 * The read is done, now we need to send status (good or bad) back
12594 * to the other side.
12596 ctl_send_datamove_done(io, /*have_lock*/ 0);
12602 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12605 void (*fe_datamove)(union ctl_io *io);
12609 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12610 printf("%s: ISC DMA read failed with error %d\n", __func__,
12612 ctl_set_internal_failure(&io->scsiio,
12614 /*retry_count*/ rq->ret);
12617 ctl_dt_req_free(rq);
12619 /* Switch the pointer over so the FETD knows what to do */
12620 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12623 * Use a custom move done callback, since we need to send completion
12624 * back to the other controller, not to the backend on this side.
12626 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12628 /* XXX KDM add checks like the ones in ctl_datamove? */
12630 fe_datamove = CTL_PORT(io)->fe_datamove;
12635 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12637 struct ctl_sg_entry *local_sglist;
12638 uint32_t len_to_go;
12643 local_sglist = io->io_hdr.local_sglist;
12644 len_to_go = io->scsiio.kern_data_len;
12647 * The difficult thing here is that the size of the various
12648 * S/G segments may be different than the size from the
12649 * remote controller. That'll make it harder when DMAing
12650 * the data back to the other side.
12652 for (i = 0; len_to_go > 0; i++) {
12653 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT);
12654 local_sglist[i].addr =
12655 malloc(local_sglist[i].len, M_CTL, M_WAITOK);
12657 len_to_go -= local_sglist[i].len;
12660 * Reset the number of S/G entries accordingly. The original
12661 * number of S/G entries is available in rem_sg_entries.
12663 io->scsiio.kern_sg_entries = i;
12666 printf("%s: kern_sg_entries = %d\n", __func__,
12667 io->scsiio.kern_sg_entries);
12668 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12669 printf("%s: sg[%d] = %p, %lu\n", __func__, i,
12670 local_sglist[i].addr, local_sglist[i].len);
12677 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12678 ctl_ha_dt_cb callback)
12680 struct ctl_ha_dt_req *rq;
12681 struct ctl_sg_entry *remote_sglist, *local_sglist;
12682 uint32_t local_used, remote_used, total_used;
12685 rq = ctl_dt_req_alloc();
12688 * If we failed to allocate the request, and if the DMA didn't fail
12689 * anyway, set busy status. This is just a resource allocation
12693 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12694 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS))
12695 ctl_set_busy(&io->scsiio);
12697 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12698 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
12701 ctl_dt_req_free(rq);
12704 * The data move failed. We need to return status back
12705 * to the other controller. No point in trying to DMA
12706 * data to the remote controller.
12709 ctl_send_datamove_done(io, /*have_lock*/ 0);
12714 local_sglist = io->io_hdr.local_sglist;
12715 remote_sglist = io->io_hdr.remote_sglist;
12721 * Pull/push the data over the wire from/to the other controller.
12722 * This takes into account the possibility that the local and
12723 * remote sglists may not be identical in terms of the size of
12724 * the elements and the number of elements.
12726 * One fundamental assumption here is that the length allocated for
12727 * both the local and remote sglists is identical. Otherwise, we've
12728 * essentially got a coding error of some sort.
12730 isc_ret = CTL_HA_STATUS_SUCCESS;
12731 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12735 rq->command = command;
12739 * Both pointers should be aligned. But it is possible
12740 * that the allocation length is not. They should both
12741 * also have enough slack left over at the end, though,
12742 * to round up to the next 8 byte boundary.
12744 cur_len = MIN(local_sglist[i].len - local_used,
12745 remote_sglist[j].len - remote_used);
12746 rq->size = cur_len;
12748 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12749 tmp_ptr += local_used;
12752 /* Use physical addresses when talking to ISC hardware */
12753 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12754 /* XXX KDM use busdma */
12755 rq->local = vtophys(tmp_ptr);
12757 rq->local = tmp_ptr;
12759 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
12760 ("HA does not support BUS_ADDR"));
12761 rq->local = tmp_ptr;
12764 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12765 tmp_ptr += remote_used;
12766 rq->remote = tmp_ptr;
12768 rq->callback = NULL;
12770 local_used += cur_len;
12771 if (local_used >= local_sglist[i].len) {
12776 remote_used += cur_len;
12777 if (remote_used >= remote_sglist[j].len) {
12781 total_used += cur_len;
12783 if (total_used >= io->scsiio.kern_data_len)
12784 rq->callback = callback;
12787 printf("%s: %s: local %p remote %p size %d\n", __func__,
12788 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
12789 rq->local, rq->remote, rq->size);
12792 isc_ret = ctl_dt_single(rq);
12793 if (isc_ret > CTL_HA_STATUS_SUCCESS)
12796 if (isc_ret != CTL_HA_STATUS_WAIT) {
12805 ctl_datamove_remote_read(union ctl_io *io)
12811 * This will send an error to the other controller in the case of a
12814 retval = ctl_datamove_remote_sgl_setup(io);
12818 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12819 ctl_datamove_remote_read_cb);
12822 * Make sure we free memory if there was an error.. The
12823 * ctl_datamove_remote_xfer() function will send the
12824 * datamove done message, or call the callback with an
12825 * error if there is a problem.
12827 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12828 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12829 free(io->io_hdr.remote_sglist, M_CTL);
12830 io->io_hdr.remote_sglist = NULL;
12831 io->io_hdr.local_sglist = NULL;
12836 * Process a datamove request from the other controller. This is used for
12837 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
12838 * first. Once that is complete, the data gets DMAed into the remote
12839 * controller's memory. For reads, we DMA from the remote controller's
12840 * memory into our memory first, and then move it out to the FETD.
12843 ctl_datamove_remote(union ctl_io *io)
12846 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
12848 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12849 ctl_failover_io(io, /*have_lock*/ 0);
12854 * Note that we look for an aborted I/O here, but don't do some of
12855 * the other checks that ctl_datamove() normally does.
12856 * We don't need to run the datamove delay code, since that should
12857 * have been done if need be on the other controller.
12859 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12860 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__,
12861 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12862 io->io_hdr.nexus.targ_port,
12863 io->io_hdr.nexus.targ_lun);
12864 io->io_hdr.port_status = 31338;
12865 ctl_send_datamove_done(io, /*have_lock*/ 0);
12869 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
12870 ctl_datamove_remote_write(io);
12871 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
12872 ctl_datamove_remote_read(io);
12874 io->io_hdr.port_status = 31339;
12875 ctl_send_datamove_done(io, /*have_lock*/ 0);
12880 ctl_process_done(union ctl_io *io)
12882 struct ctl_softc *softc = CTL_SOFTC(io);
12883 struct ctl_port *port = CTL_PORT(io);
12884 struct ctl_lun *lun = CTL_LUN(io);
12885 void (*fe_done)(union ctl_io *io);
12886 union ctl_ha_msg msg;
12888 CTL_DEBUG_PRINT(("ctl_process_done\n"));
12889 fe_done = port->fe_done;
12892 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12897 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12898 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12900 sbuf_cat(&sb, path_str);
12901 switch (io->io_hdr.io_type) {
12903 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12904 sbuf_printf(&sb, "\n");
12905 sbuf_cat(&sb, path_str);
12906 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12907 io->scsiio.tag_num, io->scsiio.tag_type);
12910 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12911 "Tag Type: %d\n", io->taskio.task_action,
12912 io->taskio.tag_num, io->taskio.tag_type);
12915 panic("%s: Invalid CTL I/O type %d\n",
12916 __func__, io->io_hdr.io_type);
12918 sbuf_cat(&sb, path_str);
12919 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
12920 (intmax_t)time_uptime - io->io_hdr.start_time);
12922 printf("%s", sbuf_data(&sb));
12924 #endif /* CTL_TIME_IO */
12926 switch (io->io_hdr.io_type) {
12930 if (ctl_debug & CTL_DEBUG_INFO)
12931 ctl_io_error_print(io, NULL);
12935 panic("%s: Invalid CTL I/O type %d\n",
12936 __func__, io->io_hdr.io_type);
12940 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
12941 io->io_hdr.nexus.targ_mapped_lun));
12945 mtx_lock(&lun->lun_lock);
12948 * Check to see if we have any informational exception and status
12949 * of this command can be modified to report it in form of either
12950 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field.
12952 if (lun->ie_reported == 0 && lun->ie_asc != 0 &&
12953 io->io_hdr.status == CTL_SUCCESS &&
12954 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) {
12955 uint8_t mrie = lun->MODE_IE.mrie;
12956 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) ||
12957 (lun->MODE_VER.byte3 & SMS_VER_PER));
12958 if (((mrie == SIEP_MRIE_REC_COND && per) ||
12959 mrie == SIEP_MRIE_REC_UNCOND ||
12960 mrie == SIEP_MRIE_NO_SENSE) &&
12961 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags &
12962 CTL_CMD_FLAG_NO_SENSE) == 0) {
12963 ctl_set_sense(&io->scsiio,
12964 /*current_error*/ 1,
12965 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ?
12966 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR,
12967 /*asc*/ lun->ie_asc,
12968 /*ascq*/ lun->ie_ascq,
12970 lun->ie_reported = 1;
12972 } else if (lun->ie_reported < 0)
12973 lun->ie_reported = 0;
12976 * Check to see if we have any errors to inject here. We only
12977 * inject errors for commands that don't already have errors set.
12979 if (!STAILQ_EMPTY(&lun->error_list) &&
12980 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
12981 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
12982 ctl_inject_error(lun, io);
12985 * XXX KDM how do we treat commands that aren't completed
12988 * XXX KDM should we also track I/O latency?
12990 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
12991 io->io_hdr.io_type == CTL_IO_SCSI) {
12997 bintime_sub(&bt, &io->io_hdr.start_bt);
12999 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13001 type = CTL_STATS_READ;
13002 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13004 type = CTL_STATS_WRITE;
13006 type = CTL_STATS_NO_IO;
13008 #ifdef CTL_LEGACY_STATS
13009 uint32_t targ_port = port->targ_port;
13010 lun->legacy_stats.ports[targ_port].bytes[type] +=
13011 io->scsiio.kern_total_len;
13012 lun->legacy_stats.ports[targ_port].operations[type] ++;
13013 lun->legacy_stats.ports[targ_port].num_dmas[type] +=
13014 io->io_hdr.num_dmas;
13016 bintime_add(&lun->legacy_stats.ports[targ_port].dma_time[type],
13017 &io->io_hdr.dma_bt);
13018 bintime_add(&lun->legacy_stats.ports[targ_port].time[type],
13021 #endif /* CTL_LEGACY_STATS */
13023 lun->stats.bytes[type] += io->scsiio.kern_total_len;
13024 lun->stats.operations[type] ++;
13025 lun->stats.dmas[type] += io->io_hdr.num_dmas;
13027 bintime_add(&lun->stats.dma_time[type], &io->io_hdr.dma_bt);
13028 bintime_add(&lun->stats.time[type], &bt);
13031 mtx_lock(&port->port_lock);
13032 port->stats.bytes[type] += io->scsiio.kern_total_len;
13033 port->stats.operations[type] ++;
13034 port->stats.dmas[type] += io->io_hdr.num_dmas;
13036 bintime_add(&port->stats.dma_time[type], &io->io_hdr.dma_bt);
13037 bintime_add(&port->stats.time[type], &bt);
13039 mtx_unlock(&port->port_lock);
13043 * Remove this from the OOA queue.
13045 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13047 if (TAILQ_EMPTY(&lun->ooa_queue))
13048 lun->last_busy = getsbinuptime();
13052 * Run through the blocked queue on this LUN and see if anything
13053 * has become unblocked, now that this transaction is done.
13055 ctl_check_blocked(lun);
13058 * If the LUN has been invalidated, free it if there is nothing
13059 * left on its OOA queue.
13061 if ((lun->flags & CTL_LUN_INVALID)
13062 && TAILQ_EMPTY(&lun->ooa_queue)) {
13063 mtx_unlock(&lun->lun_lock);
13066 mtx_unlock(&lun->lun_lock);
13071 * If this command has been aborted, make sure we set the status
13072 * properly. The FETD is responsible for freeing the I/O and doing
13073 * whatever it needs to do to clean up its state.
13075 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13076 ctl_set_task_aborted(&io->scsiio);
13079 * If enabled, print command error status.
13081 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
13082 (ctl_debug & CTL_DEBUG_INFO) != 0)
13083 ctl_io_error_print(io, NULL);
13086 * Tell the FETD or the other shelf controller we're done with this
13087 * command. Note that only SCSI commands get to this point. Task
13088 * management commands are completed above.
13090 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
13091 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) {
13092 memset(&msg, 0, sizeof(msg));
13093 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13094 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
13095 msg.hdr.nexus = io->io_hdr.nexus;
13096 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13097 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data),
13105 * Front end should call this if it doesn't do autosense. When the request
13106 * sense comes back in from the initiator, we'll dequeue this and send it.
13109 ctl_queue_sense(union ctl_io *io)
13111 struct ctl_softc *softc = CTL_SOFTC(io);
13112 struct ctl_port *port = CTL_PORT(io);
13113 struct ctl_lun *lun;
13114 struct scsi_sense_data *ps;
13115 uint32_t initidx, p, targ_lun;
13117 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13119 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13122 * LUN lookup will likely move to the ctl_work_thread() once we
13123 * have our new queueing infrastructure (that doesn't put things on
13124 * a per-LUN queue initially). That is so that we can handle
13125 * things like an INQUIRY to a LUN that we don't have enabled. We
13126 * can't deal with that right now.
13127 * If we don't have a LUN for this, just toss the sense information.
13129 mtx_lock(&softc->ctl_lock);
13130 if (targ_lun >= CTL_MAX_LUNS ||
13131 (lun = softc->ctl_luns[targ_lun]) == NULL) {
13132 mtx_unlock(&softc->ctl_lock);
13135 mtx_lock(&lun->lun_lock);
13136 mtx_unlock(&softc->ctl_lock);
13138 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13139 p = initidx / CTL_MAX_INIT_PER_PORT;
13140 if (lun->pending_sense[p] == NULL) {
13141 lun->pending_sense[p] = malloc(sizeof(*ps) * CTL_MAX_INIT_PER_PORT,
13142 M_CTL, M_NOWAIT | M_ZERO);
13144 if ((ps = lun->pending_sense[p]) != NULL) {
13145 ps += initidx % CTL_MAX_INIT_PER_PORT;
13146 memset(ps, 0, sizeof(*ps));
13147 memcpy(ps, &io->scsiio.sense_data, io->scsiio.sense_len);
13149 mtx_unlock(&lun->lun_lock);
13153 return (CTL_RETVAL_COMPLETE);
13157 * Primary command inlet from frontend ports. All SCSI and task I/O
13158 * requests must go through this function.
13161 ctl_queue(union ctl_io *io)
13163 struct ctl_port *port = CTL_PORT(io);
13165 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13168 io->io_hdr.start_time = time_uptime;
13169 getbinuptime(&io->io_hdr.start_bt);
13170 #endif /* CTL_TIME_IO */
13172 /* Map FE-specific LUN ID into global one. */
13173 io->io_hdr.nexus.targ_mapped_lun =
13174 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13176 switch (io->io_hdr.io_type) {
13179 if (ctl_debug & CTL_DEBUG_CDB)
13181 ctl_enqueue_incoming(io);
13184 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13188 return (CTL_RETVAL_COMPLETE);
13191 #ifdef CTL_IO_DELAY
13193 ctl_done_timer_wakeup(void *arg)
13197 io = (union ctl_io *)arg;
13200 #endif /* CTL_IO_DELAY */
13203 ctl_serseq_done(union ctl_io *io)
13205 struct ctl_lun *lun = CTL_LUN(io);;
13207 if (lun->be_lun == NULL ||
13208 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF)
13210 mtx_lock(&lun->lun_lock);
13211 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE;
13212 ctl_check_blocked(lun);
13213 mtx_unlock(&lun->lun_lock);
13217 ctl_done(union ctl_io *io)
13221 * Enable this to catch duplicate completion issues.
13224 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13225 printf("%s: type %d msg %d cdb %x iptl: "
13226 "%u:%u:%u tag 0x%04x "
13227 "flag %#x status %x\n",
13229 io->io_hdr.io_type,
13230 io->io_hdr.msg_type,
13232 io->io_hdr.nexus.initid,
13233 io->io_hdr.nexus.targ_port,
13234 io->io_hdr.nexus.targ_lun,
13235 (io->io_hdr.io_type ==
13237 io->taskio.tag_num :
13238 io->scsiio.tag_num,
13240 io->io_hdr.status);
13242 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13246 * This is an internal copy of an I/O, and should not go through
13247 * the normal done processing logic.
13249 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13252 #ifdef CTL_IO_DELAY
13253 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13254 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13256 struct ctl_lun *lun = CTL_LUN(io);
13259 && (lun->delay_info.done_delay > 0)) {
13261 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
13262 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13263 callout_reset(&io->io_hdr.delay_callout,
13264 lun->delay_info.done_delay * hz,
13265 ctl_done_timer_wakeup, io);
13266 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13267 lun->delay_info.done_delay = 0;
13271 #endif /* CTL_IO_DELAY */
13273 ctl_enqueue_done(io);
13277 ctl_work_thread(void *arg)
13279 struct ctl_thread *thr = (struct ctl_thread *)arg;
13280 struct ctl_softc *softc = thr->ctl_softc;
13284 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13286 while (!softc->shutdown) {
13288 * We handle the queues in this order:
13290 * - done queue (to free up resources, unblock other commands)
13294 * If those queues are empty, we break out of the loop and
13297 mtx_lock(&thr->queue_lock);
13298 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13300 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13301 mtx_unlock(&thr->queue_lock);
13302 ctl_handle_isc(io);
13305 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13307 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13308 /* clear any blocked commands, call fe_done */
13309 mtx_unlock(&thr->queue_lock);
13310 ctl_process_done(io);
13313 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13315 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13316 mtx_unlock(&thr->queue_lock);
13317 if (io->io_hdr.io_type == CTL_IO_TASK)
13320 ctl_scsiio_precheck(softc, &io->scsiio);
13323 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13325 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13326 mtx_unlock(&thr->queue_lock);
13327 retval = ctl_scsiio(&io->scsiio);
13328 if (retval != CTL_RETVAL_COMPLETE)
13329 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13333 /* Sleep until we have something to do. */
13334 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13336 thr->thread = NULL;
13341 ctl_lun_thread(void *arg)
13343 struct ctl_softc *softc = (struct ctl_softc *)arg;
13344 struct ctl_be_lun *be_lun;
13346 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13348 while (!softc->shutdown) {
13349 mtx_lock(&softc->ctl_lock);
13350 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13351 if (be_lun != NULL) {
13352 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13353 mtx_unlock(&softc->ctl_lock);
13354 ctl_create_lun(be_lun);
13358 /* Sleep until we have something to do. */
13359 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13360 PDROP | PRIBIO, "-", 0);
13362 softc->lun_thread = NULL;
13367 ctl_thresh_thread(void *arg)
13369 struct ctl_softc *softc = (struct ctl_softc *)arg;
13370 struct ctl_lun *lun;
13371 struct ctl_logical_block_provisioning_page *page;
13373 union ctl_ha_msg msg;
13374 uint64_t thres, val;
13377 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13379 while (!softc->shutdown) {
13380 mtx_lock(&softc->ctl_lock);
13381 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13382 if ((lun->flags & CTL_LUN_DISABLED) ||
13383 (lun->flags & CTL_LUN_NO_MEDIA) ||
13384 lun->backend->lun_attr == NULL)
13386 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
13387 softc->ha_mode == CTL_HA_MODE_XFER)
13389 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0)
13392 page = &lun->MODE_LBP;
13393 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13394 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13396 thres = scsi_4btoul(page->descr[i].count);
13397 thres <<= CTL_LBP_EXPONENT;
13398 switch (page->descr[i].resource) {
13400 attr = "blocksavail";
13403 attr = "blocksused";
13406 attr = "poolblocksavail";
13409 attr = "poolblocksused";
13414 mtx_unlock(&softc->ctl_lock); // XXX
13415 val = lun->backend->lun_attr(
13416 lun->be_lun->be_lun, attr);
13417 mtx_lock(&softc->ctl_lock);
13418 if (val == UINT64_MAX)
13420 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13421 == SLBPPD_ARMING_INC)
13422 e = (val >= thres);
13424 e = (val <= thres);
13428 mtx_lock(&lun->lun_lock);
13430 scsi_u64to8b((uint8_t *)&page->descr[i] -
13431 (uint8_t *)page, lun->ua_tpt_info);
13432 if (lun->lasttpt == 0 ||
13433 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13434 lun->lasttpt = time_uptime;
13435 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13441 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13444 mtx_unlock(&lun->lun_lock);
13446 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
13447 /* Send msg to other side. */
13448 bzero(&msg.ua, sizeof(msg.ua));
13449 msg.hdr.msg_type = CTL_MSG_UA;
13450 msg.hdr.nexus.initid = -1;
13451 msg.hdr.nexus.targ_port = -1;
13452 msg.hdr.nexus.targ_lun = lun->lun;
13453 msg.hdr.nexus.targ_mapped_lun = lun->lun;
13455 msg.ua.ua_set = (set > 0);
13456 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES;
13457 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8);
13458 mtx_unlock(&softc->ctl_lock); // XXX
13459 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13460 sizeof(msg.ua), M_WAITOK);
13461 mtx_lock(&softc->ctl_lock);
13464 mtx_sleep(&softc->thresh_thread, &softc->ctl_lock,
13465 PDROP | PRIBIO, "-", CTL_LBP_PERIOD * hz);
13467 softc->thresh_thread = NULL;
13472 ctl_enqueue_incoming(union ctl_io *io)
13474 struct ctl_softc *softc = CTL_SOFTC(io);
13475 struct ctl_thread *thr;
13478 idx = (io->io_hdr.nexus.targ_port * 127 +
13479 io->io_hdr.nexus.initid) % worker_threads;
13480 thr = &softc->threads[idx];
13481 mtx_lock(&thr->queue_lock);
13482 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13483 mtx_unlock(&thr->queue_lock);
13488 ctl_enqueue_rtr(union ctl_io *io)
13490 struct ctl_softc *softc = CTL_SOFTC(io);
13491 struct ctl_thread *thr;
13493 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13494 mtx_lock(&thr->queue_lock);
13495 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13496 mtx_unlock(&thr->queue_lock);
13501 ctl_enqueue_done(union ctl_io *io)
13503 struct ctl_softc *softc = CTL_SOFTC(io);
13504 struct ctl_thread *thr;
13506 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13507 mtx_lock(&thr->queue_lock);
13508 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13509 mtx_unlock(&thr->queue_lock);
13514 ctl_enqueue_isc(union ctl_io *io)
13516 struct ctl_softc *softc = CTL_SOFTC(io);
13517 struct ctl_thread *thr;
13519 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13520 mtx_lock(&thr->queue_lock);
13521 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13522 mtx_unlock(&thr->queue_lock);