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 TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads);
406 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
407 &worker_threads, 1, "Number of worker threads");
408 static int ctl_debug = CTL_DEBUG_NONE;
409 TUNABLE_INT("kern.cam.ctl.debug", &ctl_debug);
410 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN,
411 &ctl_debug, 0, "Enabled debug flags");
412 static int ctl_lun_map_size = 1024;
413 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, lun_map_size, CTLFLAG_RWTUN,
414 &ctl_lun_map_size, 0, "Size of per-port LUN map (max LUN + 1)");
417 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
418 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
419 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0),
420 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
422 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 10
424 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
426 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
427 static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest);
428 static int ctl_init(void);
429 static int ctl_shutdown(void);
430 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
431 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
432 static void ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
433 static void ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
434 struct ctl_ooa *ooa_hdr,
435 struct ctl_ooa_entry *kern_entries);
436 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
438 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
439 struct ctl_be_lun *be_lun);
440 static int ctl_free_lun(struct ctl_lun *lun);
441 static void ctl_create_lun(struct ctl_be_lun *be_lun);
443 static int ctl_do_mode_select(union ctl_io *io);
444 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
445 uint64_t res_key, uint64_t sa_res_key,
446 uint8_t type, uint32_t residx,
447 struct ctl_scsiio *ctsio,
448 struct scsi_per_res_out *cdb,
449 struct scsi_per_res_out_parms* param);
450 static void ctl_pro_preempt_other(struct ctl_lun *lun,
451 union ctl_ha_msg *msg);
452 static void ctl_hndl_per_res_out_on_other_sc(union ctl_io *io);
453 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
454 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
455 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
456 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
457 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
458 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
460 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
462 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
463 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
464 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
465 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
466 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
467 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2,
469 static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2);
470 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
471 union ctl_io *pending_io, union ctl_io *ooa_io);
472 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
473 union ctl_io *starting_io);
474 static int ctl_check_blocked(struct ctl_lun *lun);
475 static int ctl_scsiio_lun_check(struct ctl_lun *lun,
476 const struct ctl_cmd_entry *entry,
477 struct ctl_scsiio *ctsio);
478 static void ctl_failover_lun(union ctl_io *io);
479 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
480 struct ctl_scsiio *ctsio);
481 static int ctl_scsiio(struct ctl_scsiio *ctsio);
483 static int ctl_target_reset(union ctl_io *io);
484 static void ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx,
485 ctl_ua_type ua_type);
486 static int ctl_lun_reset(union ctl_io *io);
487 static int ctl_abort_task(union ctl_io *io);
488 static int ctl_abort_task_set(union ctl_io *io);
489 static int ctl_query_task(union ctl_io *io, int task_set);
490 static void ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx,
491 ctl_ua_type ua_type);
492 static int ctl_i_t_nexus_reset(union ctl_io *io);
493 static int ctl_query_async_event(union ctl_io *io);
494 static void ctl_run_task(union ctl_io *io);
496 static void ctl_datamove_timer_wakeup(void *arg);
497 static void ctl_done_timer_wakeup(void *arg);
498 #endif /* CTL_IO_DELAY */
500 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
501 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
502 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
503 static void ctl_datamove_remote_write(union ctl_io *io);
504 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
505 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
506 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
507 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
508 ctl_ha_dt_cb callback);
509 static void ctl_datamove_remote_read(union ctl_io *io);
510 static void ctl_datamove_remote(union ctl_io *io);
511 static void ctl_process_done(union ctl_io *io);
512 static void ctl_lun_thread(void *arg);
513 static void ctl_thresh_thread(void *arg);
514 static void ctl_work_thread(void *arg);
515 static void ctl_enqueue_incoming(union ctl_io *io);
516 static void ctl_enqueue_rtr(union ctl_io *io);
517 static void ctl_enqueue_done(union ctl_io *io);
518 static void ctl_enqueue_isc(union ctl_io *io);
519 static const struct ctl_cmd_entry *
520 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
521 static const struct ctl_cmd_entry *
522 ctl_validate_command(struct ctl_scsiio *ctsio);
523 static int ctl_cmd_applicable(uint8_t lun_type,
524 const struct ctl_cmd_entry *entry);
525 static int ctl_ha_init(void);
526 static int ctl_ha_shutdown(void);
528 static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx);
529 static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx);
530 static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx);
531 static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key);
534 * Load the serialization table. This isn't very pretty, but is probably
535 * the easiest way to do it.
537 #include "ctl_ser_table.c"
540 * We only need to define open, close and ioctl routines for this driver.
542 static struct cdevsw ctl_cdevsw = {
543 .d_version = D_VERSION,
546 .d_close = ctl_close,
547 .d_ioctl = ctl_ioctl,
552 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
554 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
556 static moduledata_t ctl_moduledata = {
558 ctl_module_event_handler,
562 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
563 MODULE_VERSION(ctl, 1);
565 static struct ctl_frontend ha_frontend =
569 .shutdown = ctl_ha_shutdown,
575 struct ctl_softc *softc = control_softc;
577 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
578 &softc->othersc_pool) != 0)
580 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) {
581 ctl_pool_free(softc->othersc_pool);
584 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
585 != CTL_HA_STATUS_SUCCESS) {
586 ctl_ha_msg_destroy(softc);
587 ctl_pool_free(softc->othersc_pool);
594 ctl_ha_shutdown(void)
596 struct ctl_softc *softc = control_softc;
597 struct ctl_port *port;
599 ctl_ha_msg_shutdown(softc);
600 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL) != CTL_HA_STATUS_SUCCESS)
602 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS)
604 ctl_pool_free(softc->othersc_pool);
605 while ((port = STAILQ_FIRST(&ha_frontend.port_list)) != NULL) {
606 ctl_port_deregister(port);
607 free(port->port_name, M_CTL);
614 ctl_ha_datamove(union ctl_io *io)
616 struct ctl_lun *lun = CTL_LUN(io);
617 struct ctl_sg_entry *sgl;
618 union ctl_ha_msg msg;
619 uint32_t sg_entries_sent;
620 int do_sg_copy, i, j;
622 memset(&msg.dt, 0, sizeof(msg.dt));
623 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
624 msg.hdr.original_sc = io->io_hdr.original_sc;
625 msg.hdr.serializing_sc = io;
626 msg.hdr.nexus = io->io_hdr.nexus;
627 msg.hdr.status = io->io_hdr.status;
628 msg.dt.flags = io->io_hdr.flags;
631 * We convert everything into a S/G list here. We can't
632 * pass by reference, only by value between controllers.
633 * So we can't pass a pointer to the S/G list, only as many
634 * S/G entries as we can fit in here. If it's possible for
635 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
636 * then we need to break this up into multiple transfers.
638 if (io->scsiio.kern_sg_entries == 0) {
639 msg.dt.kern_sg_entries = 1;
641 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
642 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
644 /* XXX KDM use busdma here! */
645 msg.dt.sg_list[0].addr =
646 (void *)vtophys(io->scsiio.kern_data_ptr);
649 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
650 ("HA does not support BUS_ADDR"));
651 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
653 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
656 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
660 msg.dt.kern_data_len = io->scsiio.kern_data_len;
661 msg.dt.kern_total_len = io->scsiio.kern_total_len;
662 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
663 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
664 msg.dt.sg_sequence = 0;
667 * Loop until we've sent all of the S/G entries. On the
668 * other end, we'll recompose these S/G entries into one
669 * contiguous list before processing.
671 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries;
672 msg.dt.sg_sequence++) {
673 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) /
674 sizeof(msg.dt.sg_list[0])),
675 msg.dt.kern_sg_entries - sg_entries_sent);
676 if (do_sg_copy != 0) {
677 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
678 for (i = sg_entries_sent, j = 0;
679 i < msg.dt.cur_sg_entries; i++, j++) {
681 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
682 msg.dt.sg_list[j].addr = sgl[i].addr;
684 /* XXX KDM use busdma here! */
685 msg.dt.sg_list[j].addr =
686 (void *)vtophys(sgl[i].addr);
689 KASSERT((io->io_hdr.flags &
690 CTL_FLAG_BUS_ADDR) == 0,
691 ("HA does not support BUS_ADDR"));
692 msg.dt.sg_list[j].addr = sgl[i].addr;
694 msg.dt.sg_list[j].len = sgl[i].len;
698 sg_entries_sent += msg.dt.cur_sg_entries;
699 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries);
700 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
701 sizeof(msg.dt) - sizeof(msg.dt.sg_list) +
702 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries,
703 M_WAITOK) > CTL_HA_STATUS_SUCCESS) {
704 io->io_hdr.port_status = 31341;
705 io->scsiio.be_move_done(io);
708 msg.dt.sent_sg_entries = sg_entries_sent;
712 * Officially handover the request from us to peer.
713 * If failover has just happened, then we must return error.
714 * If failover happen just after, then it is not our problem.
717 mtx_lock(&lun->lun_lock);
718 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
720 mtx_unlock(&lun->lun_lock);
721 io->io_hdr.port_status = 31342;
722 io->scsiio.be_move_done(io);
725 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
726 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
728 mtx_unlock(&lun->lun_lock);
732 ctl_ha_done(union ctl_io *io)
734 union ctl_ha_msg msg;
736 if (io->io_hdr.io_type == CTL_IO_SCSI) {
737 memset(&msg, 0, sizeof(msg));
738 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
739 msg.hdr.original_sc = io->io_hdr.original_sc;
740 msg.hdr.nexus = io->io_hdr.nexus;
741 msg.hdr.status = io->io_hdr.status;
742 msg.scsi.scsi_status = io->scsiio.scsi_status;
743 msg.scsi.tag_num = io->scsiio.tag_num;
744 msg.scsi.tag_type = io->scsiio.tag_type;
745 msg.scsi.sense_len = io->scsiio.sense_len;
746 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
747 io->scsiio.sense_len);
748 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
749 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
750 msg.scsi.sense_len, M_WAITOK);
756 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
757 union ctl_ha_msg *msg_info)
759 struct ctl_scsiio *ctsio;
761 if (msg_info->hdr.original_sc == NULL) {
762 printf("%s: original_sc == NULL!\n", __func__);
763 /* XXX KDM now what? */
767 ctsio = &msg_info->hdr.original_sc->scsiio;
768 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
769 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
770 ctsio->io_hdr.status = msg_info->hdr.status;
771 ctsio->scsi_status = msg_info->scsi.scsi_status;
772 ctsio->sense_len = msg_info->scsi.sense_len;
773 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
774 msg_info->scsi.sense_len);
775 ctl_enqueue_isc((union ctl_io *)ctsio);
779 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
780 union ctl_ha_msg *msg_info)
782 struct ctl_scsiio *ctsio;
784 if (msg_info->hdr.serializing_sc == NULL) {
785 printf("%s: serializing_sc == NULL!\n", __func__);
786 /* XXX KDM now what? */
790 ctsio = &msg_info->hdr.serializing_sc->scsiio;
791 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
792 ctl_enqueue_isc((union ctl_io *)ctsio);
796 ctl_isc_announce_lun(struct ctl_lun *lun)
798 struct ctl_softc *softc = lun->ctl_softc;
799 union ctl_ha_msg *msg;
800 struct ctl_ha_msg_lun_pr_key pr_key;
803 if (softc->ha_link != CTL_HA_LINK_ONLINE)
805 mtx_lock(&lun->lun_lock);
806 i = sizeof(msg->lun);
808 i += lun->lun_devid->len;
809 i += sizeof(pr_key) * lun->pr_key_count;
811 mtx_unlock(&lun->lun_lock);
812 msg = malloc(i, M_CTL, M_WAITOK);
813 mtx_lock(&lun->lun_lock);
814 k = sizeof(msg->lun);
816 k += lun->lun_devid->len;
817 k += sizeof(pr_key) * lun->pr_key_count;
823 bzero(&msg->lun, sizeof(msg->lun));
824 msg->hdr.msg_type = CTL_MSG_LUN_SYNC;
825 msg->hdr.nexus.targ_lun = lun->lun;
826 msg->hdr.nexus.targ_mapped_lun = lun->lun;
827 msg->lun.flags = lun->flags;
828 msg->lun.pr_generation = lun->pr_generation;
829 msg->lun.pr_res_idx = lun->pr_res_idx;
830 msg->lun.pr_res_type = lun->pr_res_type;
831 msg->lun.pr_key_count = lun->pr_key_count;
833 if (lun->lun_devid) {
834 msg->lun.lun_devid_len = lun->lun_devid->len;
835 memcpy(&msg->lun.data[i], lun->lun_devid->data,
836 msg->lun.lun_devid_len);
837 i += msg->lun.lun_devid_len;
839 for (k = 0; k < CTL_MAX_INITIATORS; k++) {
840 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0)
843 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key));
846 mtx_unlock(&lun->lun_lock);
847 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
851 if (lun->flags & CTL_LUN_PRIMARY_SC) {
852 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
853 ctl_isc_announce_mode(lun, -1,
854 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
855 lun->mode_pages.index[i].subpage);
861 ctl_isc_announce_port(struct ctl_port *port)
863 struct ctl_softc *softc = port->ctl_softc;
864 union ctl_ha_msg *msg;
867 if (port->targ_port < softc->port_min ||
868 port->targ_port >= softc->port_max ||
869 softc->ha_link != CTL_HA_LINK_ONLINE)
871 i = sizeof(msg->port) + strlen(port->port_name) + 1;
873 i += port->lun_map_size * sizeof(uint32_t);
874 if (port->port_devid)
875 i += port->port_devid->len;
876 if (port->target_devid)
877 i += port->target_devid->len;
878 if (port->init_devid)
879 i += port->init_devid->len;
880 msg = malloc(i, M_CTL, M_WAITOK);
881 bzero(&msg->port, sizeof(msg->port));
882 msg->hdr.msg_type = CTL_MSG_PORT_SYNC;
883 msg->hdr.nexus.targ_port = port->targ_port;
884 msg->port.port_type = port->port_type;
885 msg->port.physical_port = port->physical_port;
886 msg->port.virtual_port = port->virtual_port;
887 msg->port.status = port->status;
889 msg->port.name_len = sprintf(&msg->port.data[i],
890 "%d:%s", softc->ha_id, port->port_name) + 1;
891 i += msg->port.name_len;
893 msg->port.lun_map_len = port->lun_map_size * sizeof(uint32_t);
894 memcpy(&msg->port.data[i], port->lun_map,
895 msg->port.lun_map_len);
896 i += msg->port.lun_map_len;
898 if (port->port_devid) {
899 msg->port.port_devid_len = port->port_devid->len;
900 memcpy(&msg->port.data[i], port->port_devid->data,
901 msg->port.port_devid_len);
902 i += msg->port.port_devid_len;
904 if (port->target_devid) {
905 msg->port.target_devid_len = port->target_devid->len;
906 memcpy(&msg->port.data[i], port->target_devid->data,
907 msg->port.target_devid_len);
908 i += msg->port.target_devid_len;
910 if (port->init_devid) {
911 msg->port.init_devid_len = port->init_devid->len;
912 memcpy(&msg->port.data[i], port->init_devid->data,
913 msg->port.init_devid_len);
914 i += msg->port.init_devid_len;
916 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
922 ctl_isc_announce_iid(struct ctl_port *port, int iid)
924 struct ctl_softc *softc = port->ctl_softc;
925 union ctl_ha_msg *msg;
928 if (port->targ_port < softc->port_min ||
929 port->targ_port >= softc->port_max ||
930 softc->ha_link != CTL_HA_LINK_ONLINE)
932 mtx_lock(&softc->ctl_lock);
933 i = sizeof(msg->iid);
935 if (port->wwpn_iid[iid].name)
936 l = strlen(port->wwpn_iid[iid].name) + 1;
938 msg = malloc(i, M_CTL, M_NOWAIT);
940 mtx_unlock(&softc->ctl_lock);
943 bzero(&msg->iid, sizeof(msg->iid));
944 msg->hdr.msg_type = CTL_MSG_IID_SYNC;
945 msg->hdr.nexus.targ_port = port->targ_port;
946 msg->hdr.nexus.initid = iid;
947 msg->iid.in_use = port->wwpn_iid[iid].in_use;
948 msg->iid.name_len = l;
949 msg->iid.wwpn = port->wwpn_iid[iid].wwpn;
950 if (port->wwpn_iid[iid].name)
951 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l);
952 mtx_unlock(&softc->ctl_lock);
953 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT);
958 ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx,
959 uint8_t page, uint8_t subpage)
961 struct ctl_softc *softc = lun->ctl_softc;
962 union ctl_ha_msg msg;
965 if (softc->ha_link != CTL_HA_LINK_ONLINE)
967 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
968 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
969 page && lun->mode_pages.index[i].subpage == subpage)
972 if (i == CTL_NUM_MODE_PAGES)
975 /* Don't try to replicate pages not present on this device. */
976 if (lun->mode_pages.index[i].page_data == NULL)
979 bzero(&msg.mode, sizeof(msg.mode));
980 msg.hdr.msg_type = CTL_MSG_MODE_SYNC;
981 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT;
982 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT;
983 msg.hdr.nexus.targ_lun = lun->lun;
984 msg.hdr.nexus.targ_mapped_lun = lun->lun;
985 msg.mode.page_code = page;
986 msg.mode.subpage = subpage;
987 msg.mode.page_len = lun->mode_pages.index[i].page_len;
988 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data,
990 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode),
995 ctl_isc_ha_link_up(struct ctl_softc *softc)
997 struct ctl_port *port;
999 union ctl_ha_msg msg;
1002 /* Announce this node parameters to peer for validation. */
1003 msg.login.msg_type = CTL_MSG_LOGIN;
1004 msg.login.version = CTL_HA_VERSION;
1005 msg.login.ha_mode = softc->ha_mode;
1006 msg.login.ha_id = softc->ha_id;
1007 msg.login.max_luns = CTL_MAX_LUNS;
1008 msg.login.max_ports = CTL_MAX_PORTS;
1009 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT;
1010 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login),
1013 STAILQ_FOREACH(port, &softc->port_list, links) {
1014 ctl_isc_announce_port(port);
1015 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1016 if (port->wwpn_iid[i].in_use)
1017 ctl_isc_announce_iid(port, i);
1020 STAILQ_FOREACH(lun, &softc->lun_list, links)
1021 ctl_isc_announce_lun(lun);
1025 ctl_isc_ha_link_down(struct ctl_softc *softc)
1027 struct ctl_port *port;
1028 struct ctl_lun *lun;
1032 mtx_lock(&softc->ctl_lock);
1033 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1034 mtx_lock(&lun->lun_lock);
1035 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) {
1036 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1037 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1039 mtx_unlock(&lun->lun_lock);
1041 mtx_unlock(&softc->ctl_lock);
1042 io = ctl_alloc_io(softc->othersc_pool);
1043 mtx_lock(&softc->ctl_lock);
1045 io->io_hdr.msg_type = CTL_MSG_FAILOVER;
1046 io->io_hdr.nexus.targ_mapped_lun = lun->lun;
1047 ctl_enqueue_isc(io);
1050 STAILQ_FOREACH(port, &softc->port_list, links) {
1051 if (port->targ_port >= softc->port_min &&
1052 port->targ_port < softc->port_max)
1054 port->status &= ~CTL_PORT_STATUS_ONLINE;
1055 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1056 port->wwpn_iid[i].in_use = 0;
1057 free(port->wwpn_iid[i].name, M_CTL);
1058 port->wwpn_iid[i].name = NULL;
1061 mtx_unlock(&softc->ctl_lock);
1065 ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1067 struct ctl_lun *lun;
1068 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus);
1070 mtx_lock(&softc->ctl_lock);
1071 if (msg->hdr.nexus.targ_mapped_lun >= CTL_MAX_LUNS ||
1072 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) == NULL) {
1073 mtx_unlock(&softc->ctl_lock);
1076 mtx_lock(&lun->lun_lock);
1077 mtx_unlock(&softc->ctl_lock);
1078 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && msg->ua.ua_set)
1079 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8);
1080 if (msg->ua.ua_all) {
1082 ctl_est_ua_all(lun, iid, msg->ua.ua_type);
1084 ctl_clr_ua_all(lun, iid, msg->ua.ua_type);
1087 ctl_est_ua(lun, iid, msg->ua.ua_type);
1089 ctl_clr_ua(lun, iid, msg->ua.ua_type);
1091 mtx_unlock(&lun->lun_lock);
1095 ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1097 struct ctl_lun *lun;
1098 struct ctl_ha_msg_lun_pr_key pr_key;
1100 ctl_lun_flags oflags;
1103 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1104 mtx_lock(&softc->ctl_lock);
1105 if (targ_lun >= CTL_MAX_LUNS ||
1106 (lun = softc->ctl_luns[targ_lun]) == NULL) {
1107 mtx_unlock(&softc->ctl_lock);
1110 mtx_lock(&lun->lun_lock);
1111 mtx_unlock(&softc->ctl_lock);
1112 if (lun->flags & CTL_LUN_DISABLED) {
1113 mtx_unlock(&lun->lun_lock);
1116 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0;
1117 if (msg->lun.lun_devid_len != i || (i > 0 &&
1118 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) {
1119 mtx_unlock(&lun->lun_lock);
1120 printf("%s: Received conflicting HA LUN %d\n",
1121 __func__, targ_lun);
1124 /* Record whether peer is primary. */
1125 oflags = lun->flags;
1126 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) &&
1127 (msg->lun.flags & CTL_LUN_DISABLED) == 0)
1128 lun->flags |= CTL_LUN_PEER_SC_PRIMARY;
1130 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1131 if (oflags != lun->flags)
1132 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1134 /* If peer is primary and we are not -- use data */
1135 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
1136 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) {
1137 lun->pr_generation = msg->lun.pr_generation;
1138 lun->pr_res_idx = msg->lun.pr_res_idx;
1139 lun->pr_res_type = msg->lun.pr_res_type;
1140 lun->pr_key_count = msg->lun.pr_key_count;
1141 for (k = 0; k < CTL_MAX_INITIATORS; k++)
1142 ctl_clr_prkey(lun, k);
1143 for (k = 0; k < msg->lun.pr_key_count; k++) {
1144 memcpy(&pr_key, &msg->lun.data[i],
1146 ctl_alloc_prkey(lun, pr_key.pr_iid);
1147 ctl_set_prkey(lun, pr_key.pr_iid,
1149 i += sizeof(pr_key);
1153 mtx_unlock(&lun->lun_lock);
1154 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n",
1156 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ?
1157 "primary" : "secondary"));
1159 /* If we are primary but peer doesn't know -- notify */
1160 if ((lun->flags & CTL_LUN_PRIMARY_SC) &&
1161 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0)
1162 ctl_isc_announce_lun(lun);
1167 ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1169 struct ctl_port *port;
1170 struct ctl_lun *lun;
1173 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1175 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__,
1176 msg->hdr.nexus.targ_port));
1178 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO);
1179 port->frontend = &ha_frontend;
1180 port->targ_port = msg->hdr.nexus.targ_port;
1181 port->fe_datamove = ctl_ha_datamove;
1182 port->fe_done = ctl_ha_done;
1183 } else if (port->frontend == &ha_frontend) {
1184 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__,
1185 msg->hdr.nexus.targ_port));
1188 printf("%s: Received conflicting HA port %d\n",
1189 __func__, msg->hdr.nexus.targ_port);
1192 port->port_type = msg->port.port_type;
1193 port->physical_port = msg->port.physical_port;
1194 port->virtual_port = msg->port.virtual_port;
1195 port->status = msg->port.status;
1197 free(port->port_name, M_CTL);
1198 port->port_name = strndup(&msg->port.data[i], msg->port.name_len,
1200 i += msg->port.name_len;
1201 if (msg->port.lun_map_len != 0) {
1202 if (port->lun_map == NULL ||
1203 port->lun_map_size * sizeof(uint32_t) <
1204 msg->port.lun_map_len) {
1205 port->lun_map_size = 0;
1206 free(port->lun_map, M_CTL);
1207 port->lun_map = malloc(msg->port.lun_map_len,
1210 memcpy(port->lun_map, &msg->port.data[i], msg->port.lun_map_len);
1211 port->lun_map_size = msg->port.lun_map_len / sizeof(uint32_t);
1212 i += msg->port.lun_map_len;
1214 port->lun_map_size = 0;
1215 free(port->lun_map, M_CTL);
1216 port->lun_map = NULL;
1218 if (msg->port.port_devid_len != 0) {
1219 if (port->port_devid == NULL ||
1220 port->port_devid->len < msg->port.port_devid_len) {
1221 free(port->port_devid, M_CTL);
1222 port->port_devid = malloc(sizeof(struct ctl_devid) +
1223 msg->port.port_devid_len, M_CTL, M_WAITOK);
1225 memcpy(port->port_devid->data, &msg->port.data[i],
1226 msg->port.port_devid_len);
1227 port->port_devid->len = msg->port.port_devid_len;
1228 i += msg->port.port_devid_len;
1230 free(port->port_devid, M_CTL);
1231 port->port_devid = NULL;
1233 if (msg->port.target_devid_len != 0) {
1234 if (port->target_devid == NULL ||
1235 port->target_devid->len < msg->port.target_devid_len) {
1236 free(port->target_devid, M_CTL);
1237 port->target_devid = malloc(sizeof(struct ctl_devid) +
1238 msg->port.target_devid_len, M_CTL, M_WAITOK);
1240 memcpy(port->target_devid->data, &msg->port.data[i],
1241 msg->port.target_devid_len);
1242 port->target_devid->len = msg->port.target_devid_len;
1243 i += msg->port.target_devid_len;
1245 free(port->target_devid, M_CTL);
1246 port->target_devid = NULL;
1248 if (msg->port.init_devid_len != 0) {
1249 if (port->init_devid == NULL ||
1250 port->init_devid->len < msg->port.init_devid_len) {
1251 free(port->init_devid, M_CTL);
1252 port->init_devid = malloc(sizeof(struct ctl_devid) +
1253 msg->port.init_devid_len, M_CTL, M_WAITOK);
1255 memcpy(port->init_devid->data, &msg->port.data[i],
1256 msg->port.init_devid_len);
1257 port->init_devid->len = msg->port.init_devid_len;
1258 i += msg->port.init_devid_len;
1260 free(port->init_devid, M_CTL);
1261 port->init_devid = NULL;
1264 if (ctl_port_register(port) != 0) {
1265 printf("%s: ctl_port_register() failed with error\n",
1269 mtx_lock(&softc->ctl_lock);
1270 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1271 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
1273 mtx_lock(&lun->lun_lock);
1274 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
1275 mtx_unlock(&lun->lun_lock);
1277 mtx_unlock(&softc->ctl_lock);
1281 ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1283 struct ctl_port *port;
1286 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1288 printf("%s: Received IID for unknown port %d\n",
1289 __func__, msg->hdr.nexus.targ_port);
1292 iid = msg->hdr.nexus.initid;
1293 if (port->wwpn_iid[iid].in_use != 0 &&
1294 msg->iid.in_use == 0)
1295 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON);
1296 port->wwpn_iid[iid].in_use = msg->iid.in_use;
1297 port->wwpn_iid[iid].wwpn = msg->iid.wwpn;
1298 free(port->wwpn_iid[iid].name, M_CTL);
1299 if (msg->iid.name_len) {
1300 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0],
1301 msg->iid.name_len, M_CTL);
1303 port->wwpn_iid[iid].name = NULL;
1307 ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1310 if (msg->login.version != CTL_HA_VERSION) {
1311 printf("CTL HA peers have different versions %d != %d\n",
1312 msg->login.version, CTL_HA_VERSION);
1313 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1316 if (msg->login.ha_mode != softc->ha_mode) {
1317 printf("CTL HA peers have different ha_mode %d != %d\n",
1318 msg->login.ha_mode, softc->ha_mode);
1319 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1322 if (msg->login.ha_id == softc->ha_id) {
1323 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id);
1324 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1327 if (msg->login.max_luns != CTL_MAX_LUNS ||
1328 msg->login.max_ports != CTL_MAX_PORTS ||
1329 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) {
1330 printf("CTL HA peers have different limits\n");
1331 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1337 ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1339 struct ctl_lun *lun;
1341 uint32_t initidx, targ_lun;
1343 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1344 mtx_lock(&softc->ctl_lock);
1345 if (targ_lun >= CTL_MAX_LUNS ||
1346 (lun = softc->ctl_luns[targ_lun]) == NULL) {
1347 mtx_unlock(&softc->ctl_lock);
1350 mtx_lock(&lun->lun_lock);
1351 mtx_unlock(&softc->ctl_lock);
1352 if (lun->flags & CTL_LUN_DISABLED) {
1353 mtx_unlock(&lun->lun_lock);
1356 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
1357 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
1358 msg->mode.page_code &&
1359 lun->mode_pages.index[i].subpage == msg->mode.subpage)
1362 if (i == CTL_NUM_MODE_PAGES) {
1363 mtx_unlock(&lun->lun_lock);
1366 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data,
1367 lun->mode_pages.index[i].page_len);
1368 initidx = ctl_get_initindex(&msg->hdr.nexus);
1370 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
1371 mtx_unlock(&lun->lun_lock);
1375 * ISC (Inter Shelf Communication) event handler. Events from the HA
1376 * subsystem come in here.
1379 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
1381 struct ctl_softc *softc = control_softc;
1383 struct ctl_prio *presio;
1384 ctl_ha_status isc_status;
1386 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event));
1387 if (event == CTL_HA_EVT_MSG_RECV) {
1388 union ctl_ha_msg *msg, msgbuf;
1390 if (param > sizeof(msgbuf))
1391 msg = malloc(param, M_CTL, M_WAITOK);
1394 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param,
1396 if (isc_status != CTL_HA_STATUS_SUCCESS) {
1397 printf("%s: Error receiving message: %d\n",
1398 __func__, isc_status);
1404 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type));
1405 switch (msg->hdr.msg_type) {
1406 case CTL_MSG_SERIALIZE:
1407 io = ctl_alloc_io(softc->othersc_pool);
1409 // populate ctsio from msg
1410 io->io_hdr.io_type = CTL_IO_SCSI;
1411 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
1412 io->io_hdr.original_sc = msg->hdr.original_sc;
1413 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
1416 * If we're in serialization-only mode, we don't
1417 * want to go through full done processing. Thus
1420 * XXX KDM add another flag that is more specific.
1422 if (softc->ha_mode != CTL_HA_MODE_XFER)
1423 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
1424 io->io_hdr.nexus = msg->hdr.nexus;
1426 printf("port %u, iid %u, lun %u\n",
1427 io->io_hdr.nexus.targ_port,
1428 io->io_hdr.nexus.initid,
1429 io->io_hdr.nexus.targ_lun);
1431 io->scsiio.tag_num = msg->scsi.tag_num;
1432 io->scsiio.tag_type = msg->scsi.tag_type;
1434 io->io_hdr.start_time = time_uptime;
1435 getbinuptime(&io->io_hdr.start_bt);
1436 #endif /* CTL_TIME_IO */
1437 io->scsiio.cdb_len = msg->scsi.cdb_len;
1438 memcpy(io->scsiio.cdb, msg->scsi.cdb,
1440 if (softc->ha_mode == CTL_HA_MODE_XFER) {
1441 const struct ctl_cmd_entry *entry;
1443 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
1444 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
1446 entry->flags & CTL_FLAG_DATA_MASK;
1448 ctl_enqueue_isc(io);
1451 /* Performed on the Originating SC, XFER mode only */
1452 case CTL_MSG_DATAMOVE: {
1453 struct ctl_sg_entry *sgl;
1456 io = msg->hdr.original_sc;
1458 printf("%s: original_sc == NULL!\n", __func__);
1459 /* XXX KDM do something here */
1462 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
1463 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1465 * Keep track of this, we need to send it back over
1466 * when the datamove is complete.
1468 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1469 if (msg->hdr.status == CTL_SUCCESS)
1470 io->io_hdr.status = msg->hdr.status;
1472 if (msg->dt.sg_sequence == 0) {
1474 getbinuptime(&io->io_hdr.dma_start_bt);
1476 i = msg->dt.kern_sg_entries +
1477 msg->dt.kern_data_len /
1478 CTL_HA_DATAMOVE_SEGMENT + 1;
1479 sgl = malloc(sizeof(*sgl) * i, M_CTL,
1481 io->io_hdr.remote_sglist = sgl;
1482 io->io_hdr.local_sglist =
1483 &sgl[msg->dt.kern_sg_entries];
1485 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
1487 io->scsiio.kern_sg_entries =
1488 msg->dt.kern_sg_entries;
1489 io->scsiio.rem_sg_entries =
1490 msg->dt.kern_sg_entries;
1491 io->scsiio.kern_data_len =
1492 msg->dt.kern_data_len;
1493 io->scsiio.kern_total_len =
1494 msg->dt.kern_total_len;
1495 io->scsiio.kern_data_resid =
1496 msg->dt.kern_data_resid;
1497 io->scsiio.kern_rel_offset =
1498 msg->dt.kern_rel_offset;
1499 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR;
1500 io->io_hdr.flags |= msg->dt.flags &
1503 sgl = (struct ctl_sg_entry *)
1504 io->scsiio.kern_data_ptr;
1506 for (i = msg->dt.sent_sg_entries, j = 0;
1507 i < (msg->dt.sent_sg_entries +
1508 msg->dt.cur_sg_entries); i++, j++) {
1509 sgl[i].addr = msg->dt.sg_list[j].addr;
1510 sgl[i].len = msg->dt.sg_list[j].len;
1513 printf("%s: DATAMOVE: %p,%lu j=%d, i=%d\n",
1514 __func__, sgl[i].addr, sgl[i].len, j, i);
1519 * If this is the last piece of the I/O, we've got
1520 * the full S/G list. Queue processing in the thread.
1521 * Otherwise wait for the next piece.
1523 if (msg->dt.sg_last != 0)
1524 ctl_enqueue_isc(io);
1527 /* Performed on the Serializing (primary) SC, XFER mode only */
1528 case CTL_MSG_DATAMOVE_DONE: {
1529 if (msg->hdr.serializing_sc == NULL) {
1530 printf("%s: serializing_sc == NULL!\n",
1532 /* XXX KDM now what? */
1536 * We grab the sense information here in case
1537 * there was a failure, so we can return status
1538 * back to the initiator.
1540 io = msg->hdr.serializing_sc;
1541 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
1542 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1543 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1544 io->io_hdr.port_status = msg->scsi.port_status;
1545 io->scsiio.kern_data_resid = msg->scsi.kern_data_resid;
1546 if (msg->hdr.status != CTL_STATUS_NONE) {
1547 io->io_hdr.status = msg->hdr.status;
1548 io->scsiio.scsi_status = msg->scsi.scsi_status;
1549 io->scsiio.sense_len = msg->scsi.sense_len;
1550 memcpy(&io->scsiio.sense_data,
1551 &msg->scsi.sense_data,
1552 msg->scsi.sense_len);
1553 if (msg->hdr.status == CTL_SUCCESS)
1554 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1556 ctl_enqueue_isc(io);
1560 /* Preformed on Originating SC, SER_ONLY mode */
1562 io = msg->hdr.original_sc;
1564 printf("%s: original_sc == NULL!\n",
1568 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1569 io->io_hdr.msg_type = CTL_MSG_R2R;
1570 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1571 ctl_enqueue_isc(io);
1575 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
1577 * Performed on the Originating (i.e. secondary) SC in XFER
1580 case CTL_MSG_FINISH_IO:
1581 if (softc->ha_mode == CTL_HA_MODE_XFER)
1582 ctl_isc_handler_finish_xfer(softc, msg);
1584 ctl_isc_handler_finish_ser_only(softc, msg);
1587 /* Preformed on Originating SC */
1588 case CTL_MSG_BAD_JUJU:
1589 io = msg->hdr.original_sc;
1591 printf("%s: Bad JUJU!, original_sc is NULL!\n",
1595 ctl_copy_sense_data(msg, io);
1597 * IO should have already been cleaned up on other
1598 * SC so clear this flag so we won't send a message
1599 * back to finish the IO there.
1601 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
1602 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1604 /* io = msg->hdr.serializing_sc; */
1605 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
1606 ctl_enqueue_isc(io);
1609 /* Handle resets sent from the other side */
1610 case CTL_MSG_MANAGE_TASKS: {
1611 struct ctl_taskio *taskio;
1612 taskio = (struct ctl_taskio *)ctl_alloc_io(
1613 softc->othersc_pool);
1614 ctl_zero_io((union ctl_io *)taskio);
1615 taskio->io_hdr.io_type = CTL_IO_TASK;
1616 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1617 taskio->io_hdr.nexus = msg->hdr.nexus;
1618 taskio->task_action = msg->task.task_action;
1619 taskio->tag_num = msg->task.tag_num;
1620 taskio->tag_type = msg->task.tag_type;
1622 taskio->io_hdr.start_time = time_uptime;
1623 getbinuptime(&taskio->io_hdr.start_bt);
1624 #endif /* CTL_TIME_IO */
1625 ctl_run_task((union ctl_io *)taskio);
1628 /* Persistent Reserve action which needs attention */
1629 case CTL_MSG_PERS_ACTION:
1630 presio = (struct ctl_prio *)ctl_alloc_io(
1631 softc->othersc_pool);
1632 ctl_zero_io((union ctl_io *)presio);
1633 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
1634 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1635 presio->io_hdr.nexus = msg->hdr.nexus;
1636 presio->pr_msg = msg->pr;
1637 ctl_enqueue_isc((union ctl_io *)presio);
1640 ctl_isc_ua(softc, msg, param);
1642 case CTL_MSG_PORT_SYNC:
1643 ctl_isc_port_sync(softc, msg, param);
1645 case CTL_MSG_LUN_SYNC:
1646 ctl_isc_lun_sync(softc, msg, param);
1648 case CTL_MSG_IID_SYNC:
1649 ctl_isc_iid_sync(softc, msg, param);
1652 ctl_isc_login(softc, msg, param);
1654 case CTL_MSG_MODE_SYNC:
1655 ctl_isc_mode_sync(softc, msg, param);
1658 printf("Received HA message of unknown type %d\n",
1660 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1665 } else if (event == CTL_HA_EVT_LINK_CHANGE) {
1666 printf("CTL: HA link status changed from %d to %d\n",
1667 softc->ha_link, param);
1668 if (param == softc->ha_link)
1670 if (softc->ha_link == CTL_HA_LINK_ONLINE) {
1671 softc->ha_link = param;
1672 ctl_isc_ha_link_down(softc);
1674 softc->ha_link = param;
1675 if (softc->ha_link == CTL_HA_LINK_ONLINE)
1676 ctl_isc_ha_link_up(softc);
1680 printf("ctl_isc_event_handler: Unknown event %d\n", event);
1686 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
1689 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data,
1690 src->scsi.sense_len);
1691 dest->scsiio.scsi_status = src->scsi.scsi_status;
1692 dest->scsiio.sense_len = src->scsi.sense_len;
1693 dest->io_hdr.status = src->hdr.status;
1697 ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest)
1700 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data,
1701 src->scsiio.sense_len);
1702 dest->scsi.scsi_status = src->scsiio.scsi_status;
1703 dest->scsi.sense_len = src->scsiio.sense_len;
1704 dest->hdr.status = src->io_hdr.status;
1708 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1710 struct ctl_softc *softc = lun->ctl_softc;
1713 if (initidx < softc->init_min || initidx >= softc->init_max)
1715 mtx_assert(&lun->lun_lock, MA_OWNED);
1716 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1719 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua;
1723 ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua)
1727 mtx_assert(&lun->lun_lock, MA_OWNED);
1728 if (lun->pending_ua[port] == NULL)
1730 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1731 if (port * CTL_MAX_INIT_PER_PORT + i == except)
1733 lun->pending_ua[port][i] |= ua;
1738 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1740 struct ctl_softc *softc = lun->ctl_softc;
1743 mtx_assert(&lun->lun_lock, MA_OWNED);
1744 for (i = softc->port_min; i < softc->port_max; i++)
1745 ctl_est_ua_port(lun, i, except, ua);
1749 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1751 struct ctl_softc *softc = lun->ctl_softc;
1754 if (initidx < softc->init_min || initidx >= softc->init_max)
1756 mtx_assert(&lun->lun_lock, MA_OWNED);
1757 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1760 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua;
1764 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1766 struct ctl_softc *softc = lun->ctl_softc;
1769 mtx_assert(&lun->lun_lock, MA_OWNED);
1770 for (i = softc->port_min; i < softc->port_max; i++) {
1771 if (lun->pending_ua[i] == NULL)
1773 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
1774 if (i * CTL_MAX_INIT_PER_PORT + j == except)
1776 lun->pending_ua[i][j] &= ~ua;
1782 ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx,
1783 ctl_ua_type ua_type)
1785 struct ctl_lun *lun;
1787 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
1788 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) {
1789 mtx_lock(&lun->lun_lock);
1790 ctl_clr_ua(lun, initidx, ua_type);
1791 mtx_unlock(&lun->lun_lock);
1796 ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS)
1798 struct ctl_softc *softc = (struct ctl_softc *)arg1;
1799 struct ctl_lun *lun;
1800 struct ctl_lun_req ireq;
1803 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1;
1804 error = sysctl_handle_int(oidp, &value, 0, req);
1805 if ((error != 0) || (req->newptr == NULL))
1808 mtx_lock(&softc->ctl_lock);
1810 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1812 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
1813 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1814 mtx_unlock(&softc->ctl_lock);
1815 bzero(&ireq, sizeof(ireq));
1816 ireq.reqtype = CTL_LUNREQ_MODIFY;
1817 ireq.reqdata.modify.lun_id = lun->lun;
1818 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0,
1820 if (ireq.status != CTL_LUN_OK) {
1821 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n",
1822 __func__, ireq.status, ireq.error_str);
1824 mtx_lock(&softc->ctl_lock);
1826 mtx_unlock(&softc->ctl_lock);
1833 struct make_dev_args args;
1834 struct ctl_softc *softc;
1837 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1840 make_dev_args_init(&args);
1841 args.mda_devsw = &ctl_cdevsw;
1842 args.mda_uid = UID_ROOT;
1843 args.mda_gid = GID_OPERATOR;
1844 args.mda_mode = 0600;
1845 args.mda_si_drv1 = softc;
1846 error = make_dev_s(&args, &softc->dev, "cam/ctl");
1848 free(softc, M_DEVBUF);
1849 control_softc = NULL;
1853 sysctl_ctx_init(&softc->sysctl_ctx);
1854 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1855 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1856 CTLFLAG_RD, 0, "CAM Target Layer");
1858 if (softc->sysctl_tree == NULL) {
1859 printf("%s: unable to allocate sysctl tree\n", __func__);
1860 destroy_dev(softc->dev);
1861 free(softc, M_DEVBUF);
1862 control_softc = NULL;
1866 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1867 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
1868 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1871 TUNABLE_INT_FETCH("kern.cam.ctl.ha_mode", (int *)&softc->ha_mode);
1872 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1873 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0,
1874 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)");
1877 * In Copan's HA scheme, the "master" and "slave" roles are
1878 * figured out through the slot the controller is in. Although it
1879 * is an active/active system, someone has to be in charge.
1881 TUNABLE_INT_FETCH("kern.cam.ctl.ha_id", &softc->ha_id);
1882 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1883 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1884 "HA head ID (0 - no HA)");
1885 if (softc->ha_id == 0 || softc->ha_id > NUM_HA_SHELVES) {
1886 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1887 softc->is_single = 1;
1888 softc->port_cnt = CTL_MAX_PORTS;
1889 softc->port_min = 0;
1891 softc->port_cnt = CTL_MAX_PORTS / NUM_HA_SHELVES;
1892 softc->port_min = (softc->ha_id - 1) * softc->port_cnt;
1894 softc->port_max = softc->port_min + softc->port_cnt;
1895 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT;
1896 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT;
1898 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1899 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0,
1900 "HA link state (0 - offline, 1 - unknown, 2 - online)");
1902 STAILQ_INIT(&softc->lun_list);
1903 STAILQ_INIT(&softc->pending_lun_queue);
1904 STAILQ_INIT(&softc->fe_list);
1905 STAILQ_INIT(&softc->port_list);
1906 STAILQ_INIT(&softc->be_list);
1907 ctl_tpc_init(softc);
1909 if (worker_threads <= 0)
1910 worker_threads = max(1, mp_ncpus / 4);
1911 if (worker_threads > CTL_MAX_THREADS)
1912 worker_threads = CTL_MAX_THREADS;
1914 for (i = 0; i < worker_threads; i++) {
1915 struct ctl_thread *thr = &softc->threads[i];
1917 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1918 thr->ctl_softc = softc;
1919 STAILQ_INIT(&thr->incoming_queue);
1920 STAILQ_INIT(&thr->rtr_queue);
1921 STAILQ_INIT(&thr->done_queue);
1922 STAILQ_INIT(&thr->isc_queue);
1924 error = kproc_kthread_add(ctl_work_thread, thr,
1925 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1927 printf("error creating CTL work thread!\n");
1931 error = kproc_kthread_add(ctl_lun_thread, softc,
1932 &softc->ctl_proc, &softc->lun_thread, 0, 0, "ctl", "lun");
1934 printf("error creating CTL lun thread!\n");
1937 error = kproc_kthread_add(ctl_thresh_thread, softc,
1938 &softc->ctl_proc, &softc->thresh_thread, 0, 0, "ctl", "thresh");
1940 printf("error creating CTL threshold thread!\n");
1944 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1945 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN,
1946 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head");
1948 if (softc->is_single == 0) {
1949 if (ctl_frontend_register(&ha_frontend) != 0)
1950 softc->is_single = 1;
1958 struct ctl_softc *softc = control_softc;
1961 if (softc->is_single == 0)
1962 ctl_frontend_deregister(&ha_frontend);
1964 destroy_dev(softc->dev);
1966 /* Shutdown CTL threads. */
1967 softc->shutdown = 1;
1968 for (i = 0; i < worker_threads; i++) {
1969 struct ctl_thread *thr = &softc->threads[i];
1970 while (thr->thread != NULL) {
1972 if (thr->thread != NULL)
1973 pause("CTL thr shutdown", 1);
1975 mtx_destroy(&thr->queue_lock);
1977 while (softc->lun_thread != NULL) {
1978 wakeup(&softc->pending_lun_queue);
1979 if (softc->lun_thread != NULL)
1980 pause("CTL thr shutdown", 1);
1982 while (softc->thresh_thread != NULL) {
1983 wakeup(softc->thresh_thread);
1984 if (softc->thresh_thread != NULL)
1985 pause("CTL thr shutdown", 1);
1988 ctl_tpc_shutdown(softc);
1989 uma_zdestroy(softc->io_zone);
1990 mtx_destroy(&softc->ctl_lock);
1992 sysctl_ctx_free(&softc->sysctl_ctx);
1994 free(softc, M_DEVBUF);
1995 control_softc = NULL;
2000 ctl_module_event_handler(module_t mod, int what, void *arg)
2005 return (ctl_init());
2007 return (ctl_shutdown());
2009 return (EOPNOTSUPP);
2014 * XXX KDM should we do some access checks here? Bump a reference count to
2015 * prevent a CTL module from being unloaded while someone has it open?
2018 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
2024 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
2030 * Remove an initiator by port number and initiator ID.
2031 * Returns 0 for success, -1 for failure.
2034 ctl_remove_initiator(struct ctl_port *port, int iid)
2036 struct ctl_softc *softc = port->ctl_softc;
2039 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2041 if (iid > CTL_MAX_INIT_PER_PORT) {
2042 printf("%s: initiator ID %u > maximun %u!\n",
2043 __func__, iid, CTL_MAX_INIT_PER_PORT);
2047 mtx_lock(&softc->ctl_lock);
2048 last = (--port->wwpn_iid[iid].in_use == 0);
2049 port->wwpn_iid[iid].last_use = time_uptime;
2050 mtx_unlock(&softc->ctl_lock);
2052 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON);
2053 ctl_isc_announce_iid(port, iid);
2059 * Add an initiator to the initiator map.
2060 * Returns iid for success, < 0 for failure.
2063 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
2065 struct ctl_softc *softc = port->ctl_softc;
2069 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2071 if (iid >= CTL_MAX_INIT_PER_PORT) {
2072 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
2073 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
2078 mtx_lock(&softc->ctl_lock);
2080 if (iid < 0 && (wwpn != 0 || name != NULL)) {
2081 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2082 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
2086 if (name != NULL && port->wwpn_iid[i].name != NULL &&
2087 strcmp(name, port->wwpn_iid[i].name) == 0) {
2095 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2096 if (port->wwpn_iid[i].in_use == 0 &&
2097 port->wwpn_iid[i].wwpn == 0 &&
2098 port->wwpn_iid[i].name == NULL) {
2107 best_time = INT32_MAX;
2108 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2109 if (port->wwpn_iid[i].in_use == 0) {
2110 if (port->wwpn_iid[i].last_use < best_time) {
2112 best_time = port->wwpn_iid[i].last_use;
2120 mtx_unlock(&softc->ctl_lock);
2125 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
2127 * This is not an error yet.
2129 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
2131 printf("%s: port %d iid %u WWPN %#jx arrived"
2132 " again\n", __func__, port->targ_port,
2133 iid, (uintmax_t)wwpn);
2137 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
2138 strcmp(name, port->wwpn_iid[iid].name) == 0) {
2140 printf("%s: port %d iid %u name '%s' arrived"
2141 " again\n", __func__, port->targ_port,
2148 * This is an error, but what do we do about it? The
2149 * driver is telling us we have a new WWPN for this
2150 * initiator ID, so we pretty much need to use it.
2152 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
2153 " but WWPN %#jx '%s' is still at that address\n",
2154 __func__, port->targ_port, iid, wwpn, name,
2155 (uintmax_t)port->wwpn_iid[iid].wwpn,
2156 port->wwpn_iid[iid].name);
2159 free(port->wwpn_iid[iid].name, M_CTL);
2160 port->wwpn_iid[iid].name = name;
2161 port->wwpn_iid[iid].wwpn = wwpn;
2162 port->wwpn_iid[iid].in_use++;
2163 mtx_unlock(&softc->ctl_lock);
2164 ctl_isc_announce_iid(port, iid);
2170 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
2174 switch (port->port_type) {
2177 struct scsi_transportid_fcp *id =
2178 (struct scsi_transportid_fcp *)buf;
2179 if (port->wwpn_iid[iid].wwpn == 0)
2181 memset(id, 0, sizeof(*id));
2182 id->format_protocol = SCSI_PROTO_FC;
2183 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
2184 return (sizeof(*id));
2186 case CTL_PORT_ISCSI:
2188 struct scsi_transportid_iscsi_port *id =
2189 (struct scsi_transportid_iscsi_port *)buf;
2190 if (port->wwpn_iid[iid].name == NULL)
2193 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
2195 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
2196 len = roundup2(min(len, 252), 4);
2197 scsi_ulto2b(len, id->additional_length);
2198 return (sizeof(*id) + len);
2202 struct scsi_transportid_sas *id =
2203 (struct scsi_transportid_sas *)buf;
2204 if (port->wwpn_iid[iid].wwpn == 0)
2206 memset(id, 0, sizeof(*id));
2207 id->format_protocol = SCSI_PROTO_SAS;
2208 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
2209 return (sizeof(*id));
2213 struct scsi_transportid_spi *id =
2214 (struct scsi_transportid_spi *)buf;
2215 memset(id, 0, sizeof(*id));
2216 id->format_protocol = SCSI_PROTO_SPI;
2217 scsi_ulto2b(iid, id->scsi_addr);
2218 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
2219 return (sizeof(*id));
2225 * Serialize a command that went down the "wrong" side, and so was sent to
2226 * this controller for execution. The logic is a little different than the
2227 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
2228 * sent back to the other side, but in the success case, we execute the
2229 * command on this side (XFER mode) or tell the other side to execute it
2233 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
2235 struct ctl_softc *softc = CTL_SOFTC(ctsio);
2236 struct ctl_port *port = CTL_PORT(ctsio);
2237 union ctl_ha_msg msg_info;
2238 struct ctl_lun *lun;
2239 const struct ctl_cmd_entry *entry;
2242 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
2244 /* Make sure that we know about this port. */
2245 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) {
2246 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2251 /* Make sure that we know about this LUN. */
2252 mtx_lock(&softc->ctl_lock);
2253 if (targ_lun >= CTL_MAX_LUNS ||
2254 (lun = softc->ctl_luns[targ_lun]) == NULL) {
2255 mtx_unlock(&softc->ctl_lock);
2258 * The other node would not send this request to us unless
2259 * received announce that we are primary node for this LUN.
2260 * If this LUN does not exist now, it is probably result of
2261 * a race, so respond to initiator in the most opaque way.
2263 ctl_set_busy(ctsio);
2266 mtx_lock(&lun->lun_lock);
2267 mtx_unlock(&softc->ctl_lock);
2270 * If the LUN is invalid, pretend that it doesn't exist.
2271 * It will go away as soon as all pending I/Os completed.
2273 if (lun->flags & CTL_LUN_DISABLED) {
2274 mtx_unlock(&lun->lun_lock);
2275 ctl_set_busy(ctsio);
2279 entry = ctl_get_cmd_entry(ctsio, NULL);
2280 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
2281 mtx_unlock(&lun->lun_lock);
2285 CTL_LUN(ctsio) = lun;
2286 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
2289 * Every I/O goes into the OOA queue for a
2290 * particular LUN, and stays there until completion.
2293 if (TAILQ_EMPTY(&lun->ooa_queue))
2294 lun->idle_time += getsbinuptime() - lun->last_busy;
2296 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2298 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
2299 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
2301 case CTL_ACTION_BLOCK:
2302 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
2303 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
2305 mtx_unlock(&lun->lun_lock);
2307 case CTL_ACTION_PASS:
2308 case CTL_ACTION_SKIP:
2309 if (softc->ha_mode == CTL_HA_MODE_XFER) {
2310 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
2311 ctl_enqueue_rtr((union ctl_io *)ctsio);
2312 mtx_unlock(&lun->lun_lock);
2314 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
2315 mtx_unlock(&lun->lun_lock);
2317 /* send msg back to other side */
2318 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2319 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
2320 msg_info.hdr.msg_type = CTL_MSG_R2R;
2321 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2322 sizeof(msg_info.hdr), M_WAITOK);
2325 case CTL_ACTION_OVERLAP:
2326 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2327 mtx_unlock(&lun->lun_lock);
2328 ctl_set_overlapped_cmd(ctsio);
2330 case CTL_ACTION_OVERLAP_TAG:
2331 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2332 mtx_unlock(&lun->lun_lock);
2333 ctl_set_overlapped_tag(ctsio, ctsio->tag_num);
2335 case CTL_ACTION_ERROR:
2337 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2338 mtx_unlock(&lun->lun_lock);
2340 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2343 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
2344 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2345 msg_info.hdr.serializing_sc = NULL;
2346 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
2347 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2348 sizeof(msg_info.scsi), M_WAITOK);
2349 ctl_free_io((union ctl_io *)ctsio);
2355 * Returns 0 for success, errno for failure.
2358 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2359 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2363 mtx_lock(&lun->lun_lock);
2364 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2365 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2367 struct ctl_ooa_entry *entry;
2370 * If we've got more than we can fit, just count the
2371 * remaining entries.
2373 if (*cur_fill_num >= ooa_hdr->alloc_num)
2376 entry = &kern_entries[*cur_fill_num];
2378 entry->tag_num = io->scsiio.tag_num;
2379 entry->lun_num = lun->lun;
2381 entry->start_bt = io->io_hdr.start_bt;
2383 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2384 entry->cdb_len = io->scsiio.cdb_len;
2385 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2386 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2388 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2389 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2391 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2392 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2394 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2395 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2397 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2398 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2400 mtx_unlock(&lun->lun_lock);
2404 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2405 size_t error_str_len)
2409 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2411 if (copyin(user_addr, kptr, len) != 0) {
2412 snprintf(error_str, error_str_len, "Error copying %d bytes "
2413 "from user address %p to kernel address %p", len,
2423 ctl_free_args(int num_args, struct ctl_be_arg *args)
2430 for (i = 0; i < num_args; i++) {
2431 free(args[i].kname, M_CTL);
2432 free(args[i].kvalue, M_CTL);
2438 static struct ctl_be_arg *
2439 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
2440 char *error_str, size_t error_str_len)
2442 struct ctl_be_arg *args;
2445 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
2446 error_str, error_str_len);
2451 for (i = 0; i < num_args; i++) {
2452 args[i].kname = NULL;
2453 args[i].kvalue = NULL;
2456 for (i = 0; i < num_args; i++) {
2459 args[i].kname = ctl_copyin_alloc(args[i].name,
2460 args[i].namelen, error_str, error_str_len);
2461 if (args[i].kname == NULL)
2464 if (args[i].kname[args[i].namelen - 1] != '\0') {
2465 snprintf(error_str, error_str_len, "Argument %d "
2466 "name is not NUL-terminated", i);
2470 if (args[i].flags & CTL_BEARG_RD) {
2471 tmpptr = ctl_copyin_alloc(args[i].value,
2472 args[i].vallen, error_str, error_str_len);
2475 if ((args[i].flags & CTL_BEARG_ASCII)
2476 && (tmpptr[args[i].vallen - 1] != '\0')) {
2477 snprintf(error_str, error_str_len, "Argument "
2478 "%d value is not NUL-terminated", i);
2479 free(tmpptr, M_CTL);
2482 args[i].kvalue = tmpptr;
2484 args[i].kvalue = malloc(args[i].vallen,
2485 M_CTL, M_WAITOK | M_ZERO);
2492 ctl_free_args(num_args, args);
2498 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2502 for (i = 0; i < num_args; i++) {
2503 if (args[i].flags & CTL_BEARG_WR)
2504 copyout(args[i].kvalue, args[i].value, args[i].vallen);
2509 * Escape characters that are illegal or not recommended in XML.
2512 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2514 char *end = str + size;
2519 for (; *str && str < end; str++) {
2522 retval = sbuf_printf(sb, "&");
2525 retval = sbuf_printf(sb, ">");
2528 retval = sbuf_printf(sb, "<");
2531 retval = sbuf_putc(sb, *str);
2544 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2546 struct scsi_vpd_id_descriptor *desc;
2549 if (id == NULL || id->len < 4)
2551 desc = (struct scsi_vpd_id_descriptor *)id->data;
2552 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2553 case SVPD_ID_TYPE_T10:
2554 sbuf_printf(sb, "t10.");
2556 case SVPD_ID_TYPE_EUI64:
2557 sbuf_printf(sb, "eui.");
2559 case SVPD_ID_TYPE_NAA:
2560 sbuf_printf(sb, "naa.");
2562 case SVPD_ID_TYPE_SCSI_NAME:
2565 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2566 case SVPD_ID_CODESET_BINARY:
2567 for (i = 0; i < desc->length; i++)
2568 sbuf_printf(sb, "%02x", desc->identifier[i]);
2570 case SVPD_ID_CODESET_ASCII:
2571 sbuf_printf(sb, "%.*s", (int)desc->length,
2572 (char *)desc->identifier);
2574 case SVPD_ID_CODESET_UTF8:
2575 sbuf_printf(sb, "%s", (char *)desc->identifier);
2581 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2584 struct ctl_softc *softc = dev->si_drv1;
2585 struct ctl_port *port;
2586 struct ctl_lun *lun;
2593 retval = ctl_ioctl_io(dev, cmd, addr, flag, td);
2595 case CTL_ENABLE_PORT:
2596 case CTL_DISABLE_PORT:
2597 case CTL_SET_PORT_WWNS: {
2598 struct ctl_port *port;
2599 struct ctl_port_entry *entry;
2601 entry = (struct ctl_port_entry *)addr;
2603 mtx_lock(&softc->ctl_lock);
2604 STAILQ_FOREACH(port, &softc->port_list, links) {
2607 if (port->targ_port < softc->port_min ||
2608 port->targ_port >= softc->port_max)
2613 if ((entry->port_type == CTL_PORT_NONE)
2614 && (entry->targ_port == port->targ_port)) {
2616 * If the user only wants to enable or
2617 * disable or set WWNs on a specific port,
2618 * do the operation and we're done.
2622 } else if (entry->port_type & port->port_type) {
2624 * Compare the user's type mask with the
2625 * particular frontend type to see if we
2632 * Make sure the user isn't trying to set
2633 * WWNs on multiple ports at the same time.
2635 if (cmd == CTL_SET_PORT_WWNS) {
2636 printf("%s: Can't set WWNs on "
2637 "multiple ports\n", __func__);
2646 * XXX KDM we have to drop the lock here, because
2647 * the online/offline operations can potentially
2648 * block. We need to reference count the frontends
2649 * so they can't go away,
2651 if (cmd == CTL_ENABLE_PORT) {
2652 mtx_unlock(&softc->ctl_lock);
2653 ctl_port_online(port);
2654 mtx_lock(&softc->ctl_lock);
2655 } else if (cmd == CTL_DISABLE_PORT) {
2656 mtx_unlock(&softc->ctl_lock);
2657 ctl_port_offline(port);
2658 mtx_lock(&softc->ctl_lock);
2659 } else if (cmd == CTL_SET_PORT_WWNS) {
2660 ctl_port_set_wwns(port,
2661 (entry->flags & CTL_PORT_WWNN_VALID) ?
2663 (entry->flags & CTL_PORT_WWPN_VALID) ?
2664 1 : 0, entry->wwpn);
2669 mtx_unlock(&softc->ctl_lock);
2673 struct ctl_ooa *ooa_hdr;
2674 struct ctl_ooa_entry *entries;
2675 uint32_t cur_fill_num;
2677 ooa_hdr = (struct ctl_ooa *)addr;
2679 if ((ooa_hdr->alloc_len == 0)
2680 || (ooa_hdr->alloc_num == 0)) {
2681 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2682 "must be non-zero\n", __func__,
2683 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2688 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2689 sizeof(struct ctl_ooa_entry))) {
2690 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2691 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2692 __func__, ooa_hdr->alloc_len,
2693 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2698 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2699 if (entries == NULL) {
2700 printf("%s: could not allocate %d bytes for OOA "
2701 "dump\n", __func__, ooa_hdr->alloc_len);
2706 mtx_lock(&softc->ctl_lock);
2707 if ((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0 &&
2708 (ooa_hdr->lun_num >= CTL_MAX_LUNS ||
2709 softc->ctl_luns[ooa_hdr->lun_num] == NULL)) {
2710 mtx_unlock(&softc->ctl_lock);
2711 free(entries, M_CTL);
2712 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2713 __func__, (uintmax_t)ooa_hdr->lun_num);
2720 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2721 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2722 ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2726 lun = softc->ctl_luns[ooa_hdr->lun_num];
2727 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr,
2730 mtx_unlock(&softc->ctl_lock);
2732 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2733 ooa_hdr->fill_len = ooa_hdr->fill_num *
2734 sizeof(struct ctl_ooa_entry);
2735 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2737 printf("%s: error copying out %d bytes for OOA dump\n",
2738 __func__, ooa_hdr->fill_len);
2741 getbinuptime(&ooa_hdr->cur_bt);
2743 if (cur_fill_num > ooa_hdr->alloc_num) {
2744 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2745 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2747 ooa_hdr->dropped_num = 0;
2748 ooa_hdr->status = CTL_OOA_OK;
2751 free(entries, M_CTL);
2754 case CTL_DELAY_IO: {
2755 struct ctl_io_delay_info *delay_info;
2757 delay_info = (struct ctl_io_delay_info *)addr;
2760 mtx_lock(&softc->ctl_lock);
2761 if (delay_info->lun_id >= CTL_MAX_LUNS ||
2762 (lun = softc->ctl_luns[delay_info->lun_id]) == NULL) {
2763 mtx_unlock(&softc->ctl_lock);
2764 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2767 mtx_lock(&lun->lun_lock);
2768 mtx_unlock(&softc->ctl_lock);
2769 delay_info->status = CTL_DELAY_STATUS_OK;
2770 switch (delay_info->delay_type) {
2771 case CTL_DELAY_TYPE_CONT:
2772 case CTL_DELAY_TYPE_ONESHOT:
2775 delay_info->status = CTL_DELAY_STATUS_INVALID_TYPE;
2778 switch (delay_info->delay_loc) {
2779 case CTL_DELAY_LOC_DATAMOVE:
2780 lun->delay_info.datamove_type = delay_info->delay_type;
2781 lun->delay_info.datamove_delay = delay_info->delay_secs;
2783 case CTL_DELAY_LOC_DONE:
2784 lun->delay_info.done_type = delay_info->delay_type;
2785 lun->delay_info.done_delay = delay_info->delay_secs;
2788 delay_info->status = CTL_DELAY_STATUS_INVALID_LOC;
2791 mtx_unlock(&lun->lun_lock);
2793 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2794 #endif /* CTL_IO_DELAY */
2797 #ifdef CTL_LEGACY_STATS
2798 case CTL_GETSTATS: {
2799 struct ctl_stats *stats = (struct ctl_stats *)addr;
2803 * XXX KDM no locking here. If the LUN list changes,
2804 * things can blow up.
2807 stats->status = CTL_SS_OK;
2808 stats->fill_len = 0;
2809 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2810 if (stats->fill_len + sizeof(lun->legacy_stats) >
2812 stats->status = CTL_SS_NEED_MORE_SPACE;
2815 retval = copyout(&lun->legacy_stats, &stats->lun_stats[i++],
2816 sizeof(lun->legacy_stats));
2819 stats->fill_len += sizeof(lun->legacy_stats);
2821 stats->num_luns = softc->num_luns;
2822 stats->flags = CTL_STATS_FLAG_NONE;
2824 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
2826 getnanouptime(&stats->timestamp);
2829 #endif /* CTL_LEGACY_STATS */
2830 case CTL_ERROR_INJECT: {
2831 struct ctl_error_desc *err_desc, *new_err_desc;
2833 err_desc = (struct ctl_error_desc *)addr;
2835 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2837 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2839 mtx_lock(&softc->ctl_lock);
2840 if (err_desc->lun_id >= CTL_MAX_LUNS ||
2841 (lun = softc->ctl_luns[err_desc->lun_id]) == NULL) {
2842 mtx_unlock(&softc->ctl_lock);
2843 free(new_err_desc, M_CTL);
2844 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2845 __func__, (uintmax_t)err_desc->lun_id);
2849 mtx_lock(&lun->lun_lock);
2850 mtx_unlock(&softc->ctl_lock);
2853 * We could do some checking here to verify the validity
2854 * of the request, but given the complexity of error
2855 * injection requests, the checking logic would be fairly
2858 * For now, if the request is invalid, it just won't get
2859 * executed and might get deleted.
2861 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2864 * XXX KDM check to make sure the serial number is unique,
2865 * in case we somehow manage to wrap. That shouldn't
2866 * happen for a very long time, but it's the right thing to
2869 new_err_desc->serial = lun->error_serial;
2870 err_desc->serial = lun->error_serial;
2871 lun->error_serial++;
2873 mtx_unlock(&lun->lun_lock);
2876 case CTL_ERROR_INJECT_DELETE: {
2877 struct ctl_error_desc *delete_desc, *desc, *desc2;
2880 delete_desc = (struct ctl_error_desc *)addr;
2883 mtx_lock(&softc->ctl_lock);
2884 if (delete_desc->lun_id >= CTL_MAX_LUNS ||
2885 (lun = softc->ctl_luns[delete_desc->lun_id]) == NULL) {
2886 mtx_unlock(&softc->ctl_lock);
2887 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2888 __func__, (uintmax_t)delete_desc->lun_id);
2892 mtx_lock(&lun->lun_lock);
2893 mtx_unlock(&softc->ctl_lock);
2894 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2895 if (desc->serial != delete_desc->serial)
2898 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2903 mtx_unlock(&lun->lun_lock);
2904 if (delete_done == 0) {
2905 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2906 "error serial %ju on LUN %u\n", __func__,
2907 delete_desc->serial, delete_desc->lun_id);
2913 case CTL_DUMP_STRUCTS: {
2915 struct ctl_port *port;
2916 struct ctl_frontend *fe;
2918 mtx_lock(&softc->ctl_lock);
2919 printf("CTL Persistent Reservation information start:\n");
2920 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2921 mtx_lock(&lun->lun_lock);
2922 if ((lun->flags & CTL_LUN_DISABLED) != 0) {
2923 mtx_unlock(&lun->lun_lock);
2927 for (j = 0; j < CTL_MAX_PORTS; j++) {
2928 if (lun->pr_keys[j] == NULL)
2930 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2931 if (lun->pr_keys[j][k] == 0)
2933 printf(" LUN %ju port %d iid %d key "
2934 "%#jx\n", lun->lun, j, k,
2935 (uintmax_t)lun->pr_keys[j][k]);
2938 mtx_unlock(&lun->lun_lock);
2940 printf("CTL Persistent Reservation information end\n");
2941 printf("CTL Ports:\n");
2942 STAILQ_FOREACH(port, &softc->port_list, links) {
2943 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
2944 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
2945 port->frontend->name, port->port_type,
2946 port->physical_port, port->virtual_port,
2947 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
2948 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2949 if (port->wwpn_iid[j].in_use == 0 &&
2950 port->wwpn_iid[j].wwpn == 0 &&
2951 port->wwpn_iid[j].name == NULL)
2954 printf(" iid %u use %d WWPN %#jx '%s'\n",
2955 j, port->wwpn_iid[j].in_use,
2956 (uintmax_t)port->wwpn_iid[j].wwpn,
2957 port->wwpn_iid[j].name);
2960 printf("CTL Port information end\n");
2961 mtx_unlock(&softc->ctl_lock);
2963 * XXX KDM calling this without a lock. We'd likely want
2964 * to drop the lock before calling the frontend's dump
2967 printf("CTL Frontends:\n");
2968 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2969 printf(" Frontend '%s'\n", fe->name);
2970 if (fe->fe_dump != NULL)
2973 printf("CTL Frontend information end\n");
2977 struct ctl_lun_req *lun_req;
2978 struct ctl_backend_driver *backend;
2980 lun_req = (struct ctl_lun_req *)addr;
2982 backend = ctl_backend_find(lun_req->backend);
2983 if (backend == NULL) {
2984 lun_req->status = CTL_LUN_ERROR;
2985 snprintf(lun_req->error_str,
2986 sizeof(lun_req->error_str),
2987 "Backend \"%s\" not found.",
2991 if (lun_req->num_be_args > 0) {
2992 lun_req->kern_be_args = ctl_copyin_args(
2993 lun_req->num_be_args,
2996 sizeof(lun_req->error_str));
2997 if (lun_req->kern_be_args == NULL) {
2998 lun_req->status = CTL_LUN_ERROR;
3003 retval = backend->ioctl(dev, cmd, addr, flag, td);
3005 if (lun_req->num_be_args > 0) {
3006 ctl_copyout_args(lun_req->num_be_args,
3007 lun_req->kern_be_args);
3008 ctl_free_args(lun_req->num_be_args,
3009 lun_req->kern_be_args);
3013 case CTL_LUN_LIST: {
3015 struct ctl_lun_list *list;
3016 struct ctl_option *opt;
3018 list = (struct ctl_lun_list *)addr;
3021 * Allocate a fixed length sbuf here, based on the length
3022 * of the user's buffer. We could allocate an auto-extending
3023 * buffer, and then tell the user how much larger our
3024 * amount of data is than his buffer, but that presents
3027 * 1. The sbuf(9) routines use a blocking malloc, and so
3028 * we can't hold a lock while calling them with an
3029 * auto-extending buffer.
3031 * 2. There is not currently a LUN reference counting
3032 * mechanism, outside of outstanding transactions on
3033 * the LUN's OOA queue. So a LUN could go away on us
3034 * while we're getting the LUN number, backend-specific
3035 * information, etc. Thus, given the way things
3036 * currently work, we need to hold the CTL lock while
3037 * grabbing LUN information.
3039 * So, from the user's standpoint, the best thing to do is
3040 * allocate what he thinks is a reasonable buffer length,
3041 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3042 * double the buffer length and try again. (And repeat
3043 * that until he succeeds.)
3045 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3047 list->status = CTL_LUN_LIST_ERROR;
3048 snprintf(list->error_str, sizeof(list->error_str),
3049 "Unable to allocate %d bytes for LUN list",
3054 sbuf_printf(sb, "<ctllunlist>\n");
3056 mtx_lock(&softc->ctl_lock);
3057 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3058 mtx_lock(&lun->lun_lock);
3059 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3060 (uintmax_t)lun->lun);
3063 * Bail out as soon as we see that we've overfilled
3069 retval = sbuf_printf(sb, "\t<backend_type>%s"
3070 "</backend_type>\n",
3071 (lun->backend == NULL) ? "none" :
3072 lun->backend->name);
3077 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3078 lun->be_lun->lun_type);
3083 if (lun->backend == NULL) {
3084 retval = sbuf_printf(sb, "</lun>\n");
3090 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3091 (lun->be_lun->maxlba > 0) ?
3092 lun->be_lun->maxlba + 1 : 0);
3097 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3098 lun->be_lun->blocksize);
3103 retval = sbuf_printf(sb, "\t<serial_number>");
3108 retval = ctl_sbuf_printf_esc(sb,
3109 lun->be_lun->serial_num,
3110 sizeof(lun->be_lun->serial_num));
3115 retval = sbuf_printf(sb, "</serial_number>\n");
3120 retval = sbuf_printf(sb, "\t<device_id>");
3125 retval = ctl_sbuf_printf_esc(sb,
3126 lun->be_lun->device_id,
3127 sizeof(lun->be_lun->device_id));
3132 retval = sbuf_printf(sb, "</device_id>\n");
3137 if (lun->backend->lun_info != NULL) {
3138 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3142 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3143 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3144 opt->name, opt->value, opt->name);
3149 retval = sbuf_printf(sb, "</lun>\n");
3153 mtx_unlock(&lun->lun_lock);
3156 mtx_unlock(&lun->lun_lock);
3157 mtx_unlock(&softc->ctl_lock);
3160 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3163 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3164 snprintf(list->error_str, sizeof(list->error_str),
3165 "Out of space, %d bytes is too small",
3172 retval = copyout(sbuf_data(sb), list->lun_xml,
3175 list->fill_len = sbuf_len(sb) + 1;
3176 list->status = CTL_LUN_LIST_OK;
3181 struct ctl_iscsi *ci;
3182 struct ctl_frontend *fe;
3184 ci = (struct ctl_iscsi *)addr;
3186 fe = ctl_frontend_find("iscsi");
3188 ci->status = CTL_ISCSI_ERROR;
3189 snprintf(ci->error_str, sizeof(ci->error_str),
3190 "Frontend \"iscsi\" not found.");
3194 retval = fe->ioctl(dev, cmd, addr, flag, td);
3197 case CTL_PORT_REQ: {
3198 struct ctl_req *req;
3199 struct ctl_frontend *fe;
3201 req = (struct ctl_req *)addr;
3203 fe = ctl_frontend_find(req->driver);
3205 req->status = CTL_LUN_ERROR;
3206 snprintf(req->error_str, sizeof(req->error_str),
3207 "Frontend \"%s\" not found.", req->driver);
3210 if (req->num_args > 0) {
3211 req->kern_args = ctl_copyin_args(req->num_args,
3212 req->args, req->error_str, sizeof(req->error_str));
3213 if (req->kern_args == NULL) {
3214 req->status = CTL_LUN_ERROR;
3220 retval = fe->ioctl(dev, cmd, addr, flag, td);
3224 if (req->num_args > 0) {
3225 ctl_copyout_args(req->num_args, req->kern_args);
3226 ctl_free_args(req->num_args, req->kern_args);
3230 case CTL_PORT_LIST: {
3232 struct ctl_port *port;
3233 struct ctl_lun_list *list;
3234 struct ctl_option *opt;
3238 list = (struct ctl_lun_list *)addr;
3240 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3242 list->status = CTL_LUN_LIST_ERROR;
3243 snprintf(list->error_str, sizeof(list->error_str),
3244 "Unable to allocate %d bytes for LUN list",
3249 sbuf_printf(sb, "<ctlportlist>\n");
3251 mtx_lock(&softc->ctl_lock);
3252 STAILQ_FOREACH(port, &softc->port_list, links) {
3253 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3254 (uintmax_t)port->targ_port);
3257 * Bail out as soon as we see that we've overfilled
3263 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3264 "</frontend_type>\n", port->frontend->name);
3268 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3273 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3274 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3278 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3283 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3284 port->physical_port);
3288 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3289 port->virtual_port);
3293 if (port->target_devid != NULL) {
3294 sbuf_printf(sb, "\t<target>");
3295 ctl_id_sbuf(port->target_devid, sb);
3296 sbuf_printf(sb, "</target>\n");
3299 if (port->port_devid != NULL) {
3300 sbuf_printf(sb, "\t<port>");
3301 ctl_id_sbuf(port->port_devid, sb);
3302 sbuf_printf(sb, "</port>\n");
3305 if (port->port_info != NULL) {
3306 retval = port->port_info(port->onoff_arg, sb);
3310 STAILQ_FOREACH(opt, &port->options, links) {
3311 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3312 opt->name, opt->value, opt->name);
3317 if (port->lun_map != NULL) {
3318 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n");
3319 for (j = 0; j < port->lun_map_size; j++) {
3320 plun = ctl_lun_map_from_port(port, j);
3321 if (plun == UINT32_MAX)
3324 "\t<lun id=\"%u\">%u</lun>\n",
3329 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3330 if (port->wwpn_iid[j].in_use == 0 ||
3331 (port->wwpn_iid[j].wwpn == 0 &&
3332 port->wwpn_iid[j].name == NULL))
3335 if (port->wwpn_iid[j].name != NULL)
3336 retval = sbuf_printf(sb,
3337 "\t<initiator id=\"%u\">%s</initiator>\n",
3338 j, port->wwpn_iid[j].name);
3340 retval = sbuf_printf(sb,
3341 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3342 j, port->wwpn_iid[j].wwpn);
3349 retval = sbuf_printf(sb, "</targ_port>\n");
3353 mtx_unlock(&softc->ctl_lock);
3356 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3359 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3360 snprintf(list->error_str, sizeof(list->error_str),
3361 "Out of space, %d bytes is too small",
3368 retval = copyout(sbuf_data(sb), list->lun_xml,
3371 list->fill_len = sbuf_len(sb) + 1;
3372 list->status = CTL_LUN_LIST_OK;
3377 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr;
3378 struct ctl_port *port;
3380 mtx_lock(&softc->ctl_lock);
3381 if (lm->port < softc->port_min ||
3382 lm->port >= softc->port_max ||
3383 (port = softc->ctl_ports[lm->port]) == NULL) {
3384 mtx_unlock(&softc->ctl_lock);
3387 if (port->status & CTL_PORT_STATUS_ONLINE) {
3388 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3389 if (ctl_lun_map_to_port(port, lun->lun) ==
3392 mtx_lock(&lun->lun_lock);
3393 ctl_est_ua_port(lun, lm->port, -1,
3395 mtx_unlock(&lun->lun_lock);
3398 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps
3399 if (lm->plun != UINT32_MAX) {
3400 if (lm->lun == UINT32_MAX)
3401 retval = ctl_lun_map_unset(port, lm->plun);
3402 else if (lm->lun < CTL_MAX_LUNS &&
3403 softc->ctl_luns[lm->lun] != NULL)
3404 retval = ctl_lun_map_set(port, lm->plun, lm->lun);
3408 if (lm->lun == UINT32_MAX)
3409 retval = ctl_lun_map_deinit(port);
3411 retval = ctl_lun_map_init(port);
3413 if (port->status & CTL_PORT_STATUS_ONLINE)
3414 ctl_isc_announce_port(port);
3417 case CTL_GET_LUN_STATS: {
3418 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr;
3422 * XXX KDM no locking here. If the LUN list changes,
3423 * things can blow up.
3426 stats->status = CTL_SS_OK;
3427 stats->fill_len = 0;
3428 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3429 if (lun->lun < stats->first_item)
3431 if (stats->fill_len + sizeof(lun->stats) >
3433 stats->status = CTL_SS_NEED_MORE_SPACE;
3436 retval = copyout(&lun->stats, &stats->stats[i++],
3437 sizeof(lun->stats));
3440 stats->fill_len += sizeof(lun->stats);
3442 stats->num_items = softc->num_luns;
3443 stats->flags = CTL_STATS_FLAG_NONE;
3445 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
3447 getnanouptime(&stats->timestamp);
3450 case CTL_GET_PORT_STATS: {
3451 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr;
3455 * XXX KDM no locking here. If the LUN list changes,
3456 * things can blow up.
3459 stats->status = CTL_SS_OK;
3460 stats->fill_len = 0;
3461 STAILQ_FOREACH(port, &softc->port_list, links) {
3462 if (port->targ_port < stats->first_item)
3464 if (stats->fill_len + sizeof(port->stats) >
3466 stats->status = CTL_SS_NEED_MORE_SPACE;
3469 retval = copyout(&port->stats, &stats->stats[i++],
3470 sizeof(port->stats));
3473 stats->fill_len += sizeof(port->stats);
3475 stats->num_items = softc->num_ports;
3476 stats->flags = CTL_STATS_FLAG_NONE;
3478 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
3480 getnanouptime(&stats->timestamp);
3484 /* XXX KDM should we fix this? */
3486 struct ctl_backend_driver *backend;
3493 * We encode the backend type as the ioctl type for backend
3494 * ioctls. So parse it out here, and then search for a
3495 * backend of this type.
3497 type = _IOC_TYPE(cmd);
3499 STAILQ_FOREACH(backend, &softc->be_list, links) {
3500 if (backend->type == type) {
3506 printf("ctl: unknown ioctl command %#lx or backend "
3511 retval = backend->ioctl(dev, cmd, addr, flag, td);
3521 ctl_get_initindex(struct ctl_nexus *nexus)
3523 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3527 ctl_lun_map_init(struct ctl_port *port)
3529 struct ctl_softc *softc = port->ctl_softc;
3530 struct ctl_lun *lun;
3531 int size = ctl_lun_map_size;
3534 if (port->lun_map == NULL || port->lun_map_size < size) {
3535 port->lun_map_size = 0;
3536 free(port->lun_map, M_CTL);
3537 port->lun_map = malloc(size * sizeof(uint32_t),
3540 if (port->lun_map == NULL)
3542 for (i = 0; i < size; i++)
3543 port->lun_map[i] = UINT32_MAX;
3544 port->lun_map_size = size;
3545 if (port->status & CTL_PORT_STATUS_ONLINE) {
3546 if (port->lun_disable != NULL) {
3547 STAILQ_FOREACH(lun, &softc->lun_list, links)
3548 port->lun_disable(port->targ_lun_arg, lun->lun);
3550 ctl_isc_announce_port(port);
3556 ctl_lun_map_deinit(struct ctl_port *port)
3558 struct ctl_softc *softc = port->ctl_softc;
3559 struct ctl_lun *lun;
3561 if (port->lun_map == NULL)
3563 port->lun_map_size = 0;
3564 free(port->lun_map, M_CTL);
3565 port->lun_map = NULL;
3566 if (port->status & CTL_PORT_STATUS_ONLINE) {
3567 if (port->lun_enable != NULL) {
3568 STAILQ_FOREACH(lun, &softc->lun_list, links)
3569 port->lun_enable(port->targ_lun_arg, lun->lun);
3571 ctl_isc_announce_port(port);
3577 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun)
3582 if (port->lun_map == NULL) {
3583 status = ctl_lun_map_init(port);
3587 if (plun >= port->lun_map_size)
3589 old = port->lun_map[plun];
3590 port->lun_map[plun] = glun;
3591 if ((port->status & CTL_PORT_STATUS_ONLINE) && old == UINT32_MAX) {
3592 if (port->lun_enable != NULL)
3593 port->lun_enable(port->targ_lun_arg, plun);
3594 ctl_isc_announce_port(port);
3600 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun)
3604 if (port->lun_map == NULL || plun >= port->lun_map_size)
3606 old = port->lun_map[plun];
3607 port->lun_map[plun] = UINT32_MAX;
3608 if ((port->status & CTL_PORT_STATUS_ONLINE) && old != UINT32_MAX) {
3609 if (port->lun_disable != NULL)
3610 port->lun_disable(port->targ_lun_arg, plun);
3611 ctl_isc_announce_port(port);
3617 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
3621 return (UINT32_MAX);
3622 if (port->lun_map == NULL)
3624 if (lun_id > port->lun_map_size)
3625 return (UINT32_MAX);
3626 return (port->lun_map[lun_id]);
3630 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
3635 return (UINT32_MAX);
3636 if (port->lun_map == NULL)
3638 for (i = 0; i < port->lun_map_size; i++) {
3639 if (port->lun_map[i] == lun_id)
3642 return (UINT32_MAX);
3646 ctl_decode_lun(uint64_t encoded)
3649 uint32_t result = 0xffffffff;
3651 be64enc(lun, encoded);
3652 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) {
3653 case RPL_LUNDATA_ATYP_PERIPH:
3654 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 &&
3655 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0)
3658 case RPL_LUNDATA_ATYP_FLAT:
3659 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 &&
3660 lun[6] == 0 && lun[7] == 0)
3661 result = ((lun[0] & 0x3f) << 8) + lun[1];
3663 case RPL_LUNDATA_ATYP_EXTLUN:
3664 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) {
3666 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) {
3671 result = (lun[1] << 16) + (lun[2] << 8) +
3675 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0)
3676 result = (lun[2] << 24) +
3677 (lun[3] << 16) + (lun[4] << 8) +
3682 case RPL_LUNDATA_EXT_EAM_NOT_SPEC:
3683 result = 0xffffffff;
3692 ctl_encode_lun(uint32_t decoded)
3694 uint64_t l = decoded;
3697 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48));
3699 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48));
3701 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) |
3703 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16));
3707 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last)
3711 for (i = first; i < last; i++) {
3712 if ((mask[i / 32] & (1 << (i % 32))) == 0)
3719 ctl_set_mask(uint32_t *mask, uint32_t bit)
3721 uint32_t chunk, piece;
3724 piece = bit % (sizeof(uint32_t) * 8);
3726 if ((mask[chunk] & (1 << piece)) != 0)
3729 mask[chunk] |= (1 << piece);
3735 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3737 uint32_t chunk, piece;
3740 piece = bit % (sizeof(uint32_t) * 8);
3742 if ((mask[chunk] & (1 << piece)) == 0)
3745 mask[chunk] &= ~(1 << piece);
3751 ctl_is_set(uint32_t *mask, uint32_t bit)
3753 uint32_t chunk, piece;
3756 piece = bit % (sizeof(uint32_t) * 8);
3758 if ((mask[chunk] & (1 << piece)) == 0)
3765 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3769 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3772 return (t[residx % CTL_MAX_INIT_PER_PORT]);
3776 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3780 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3783 t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3787 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3792 i = residx/CTL_MAX_INIT_PER_PORT;
3793 if (lun->pr_keys[i] != NULL)
3795 mtx_unlock(&lun->lun_lock);
3796 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3798 mtx_lock(&lun->lun_lock);
3799 if (lun->pr_keys[i] == NULL)
3800 lun->pr_keys[i] = p;
3806 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3810 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3811 KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3812 t[residx % CTL_MAX_INIT_PER_PORT] = key;
3816 * ctl_softc, pool_name, total_ctl_io are passed in.
3817 * npool is passed out.
3820 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3821 uint32_t total_ctl_io, void **npool)
3823 struct ctl_io_pool *pool;
3825 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3830 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3831 pool->ctl_softc = ctl_softc;
3833 pool->zone = uma_zsecond_create(pool->name, NULL,
3834 NULL, NULL, NULL, ctl_softc->io_zone);
3835 /* uma_prealloc(pool->zone, total_ctl_io); */
3837 pool->zone = ctl_softc->io_zone;
3845 ctl_pool_free(struct ctl_io_pool *pool)
3852 uma_zdestroy(pool->zone);
3858 ctl_alloc_io(void *pool_ref)
3860 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3863 io = uma_zalloc(pool->zone, M_WAITOK);
3865 io->io_hdr.pool = pool_ref;
3866 CTL_SOFTC(io) = pool->ctl_softc;
3872 ctl_alloc_io_nowait(void *pool_ref)
3874 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3877 io = uma_zalloc(pool->zone, M_NOWAIT);
3879 io->io_hdr.pool = pool_ref;
3880 CTL_SOFTC(io) = pool->ctl_softc;
3886 ctl_free_io(union ctl_io *io)
3888 struct ctl_io_pool *pool;
3893 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3894 uma_zfree(pool->zone, io);
3898 ctl_zero_io(union ctl_io *io)
3900 struct ctl_io_pool *pool;
3906 * May need to preserve linked list pointers at some point too.
3908 pool = io->io_hdr.pool;
3909 memset(io, 0, sizeof(*io));
3910 io->io_hdr.pool = pool;
3911 CTL_SOFTC(io) = pool->ctl_softc;
3915 ctl_expand_number(const char *buf, uint64_t *num)
3921 number = strtoq(buf, &endptr, 0);
3923 switch (tolower((unsigned char)*endptr)) {
3943 case '\0': /* No unit. */
3947 /* Unrecognized unit. */
3951 if ((number << shift) >> shift != number) {
3955 *num = number << shift;
3961 * This routine could be used in the future to load default and/or saved
3962 * mode page parameters for a particuar lun.
3965 ctl_init_page_index(struct ctl_lun *lun)
3968 struct ctl_page_index *page_index;
3972 memcpy(&lun->mode_pages.index, page_index_template,
3973 sizeof(page_index_template));
3975 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3977 page_index = &lun->mode_pages.index[i];
3978 if (lun->be_lun->lun_type == T_DIRECT &&
3979 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
3981 if (lun->be_lun->lun_type == T_PROCESSOR &&
3982 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
3984 if (lun->be_lun->lun_type == T_CDROM &&
3985 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
3988 page_code = page_index->page_code & SMPH_PC_MASK;
3989 switch (page_code) {
3990 case SMS_RW_ERROR_RECOVERY_PAGE: {
3991 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
3992 ("subpage %#x for page %#x is incorrect!",
3993 page_index->subpage, page_code));
3994 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
3995 &rw_er_page_default,
3996 sizeof(rw_er_page_default));
3997 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
3998 &rw_er_page_changeable,
3999 sizeof(rw_er_page_changeable));
4000 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
4001 &rw_er_page_default,
4002 sizeof(rw_er_page_default));
4003 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
4004 &rw_er_page_default,
4005 sizeof(rw_er_page_default));
4006 page_index->page_data =
4007 (uint8_t *)lun->mode_pages.rw_er_page;
4010 case SMS_FORMAT_DEVICE_PAGE: {
4011 struct scsi_format_page *format_page;
4013 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4014 ("subpage %#x for page %#x is incorrect!",
4015 page_index->subpage, page_code));
4018 * Sectors per track are set above. Bytes per
4019 * sector need to be set here on a per-LUN basis.
4021 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
4022 &format_page_default,
4023 sizeof(format_page_default));
4024 memcpy(&lun->mode_pages.format_page[
4025 CTL_PAGE_CHANGEABLE], &format_page_changeable,
4026 sizeof(format_page_changeable));
4027 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
4028 &format_page_default,
4029 sizeof(format_page_default));
4030 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
4031 &format_page_default,
4032 sizeof(format_page_default));
4034 format_page = &lun->mode_pages.format_page[
4036 scsi_ulto2b(lun->be_lun->blocksize,
4037 format_page->bytes_per_sector);
4039 format_page = &lun->mode_pages.format_page[
4041 scsi_ulto2b(lun->be_lun->blocksize,
4042 format_page->bytes_per_sector);
4044 format_page = &lun->mode_pages.format_page[
4046 scsi_ulto2b(lun->be_lun->blocksize,
4047 format_page->bytes_per_sector);
4049 page_index->page_data =
4050 (uint8_t *)lun->mode_pages.format_page;
4053 case SMS_RIGID_DISK_PAGE: {
4054 struct scsi_rigid_disk_page *rigid_disk_page;
4055 uint32_t sectors_per_cylinder;
4059 #endif /* !__XSCALE__ */
4061 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4062 ("subpage %#x for page %#x is incorrect!",
4063 page_index->subpage, page_code));
4066 * Rotation rate and sectors per track are set
4067 * above. We calculate the cylinders here based on
4068 * capacity. Due to the number of heads and
4069 * sectors per track we're using, smaller arrays
4070 * may turn out to have 0 cylinders. Linux and
4071 * FreeBSD don't pay attention to these mode pages
4072 * to figure out capacity, but Solaris does. It
4073 * seems to deal with 0 cylinders just fine, and
4074 * works out a fake geometry based on the capacity.
4076 memcpy(&lun->mode_pages.rigid_disk_page[
4077 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4078 sizeof(rigid_disk_page_default));
4079 memcpy(&lun->mode_pages.rigid_disk_page[
4080 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4081 sizeof(rigid_disk_page_changeable));
4083 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4087 * The divide method here will be more accurate,
4088 * probably, but results in floating point being
4089 * used in the kernel on i386 (__udivdi3()). On the
4090 * XScale, though, __udivdi3() is implemented in
4093 * The shift method for cylinder calculation is
4094 * accurate if sectors_per_cylinder is a power of
4095 * 2. Otherwise it might be slightly off -- you
4096 * might have a bit of a truncation problem.
4099 cylinders = (lun->be_lun->maxlba + 1) /
4100 sectors_per_cylinder;
4102 for (shift = 31; shift > 0; shift--) {
4103 if (sectors_per_cylinder & (1 << shift))
4106 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4110 * We've basically got 3 bytes, or 24 bits for the
4111 * cylinder size in the mode page. If we're over,
4112 * just round down to 2^24.
4114 if (cylinders > 0xffffff)
4115 cylinders = 0xffffff;
4117 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4119 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4121 if ((value = ctl_get_opt(&lun->be_lun->options,
4123 scsi_ulto2b(strtol(value, NULL, 0),
4124 rigid_disk_page->rotation_rate);
4127 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
4128 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4129 sizeof(rigid_disk_page_default));
4130 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
4131 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4132 sizeof(rigid_disk_page_default));
4134 page_index->page_data =
4135 (uint8_t *)lun->mode_pages.rigid_disk_page;
4138 case SMS_VERIFY_ERROR_RECOVERY_PAGE: {
4139 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4140 ("subpage %#x for page %#x is incorrect!",
4141 page_index->subpage, page_code));
4142 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CURRENT],
4143 &verify_er_page_default,
4144 sizeof(verify_er_page_default));
4145 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CHANGEABLE],
4146 &verify_er_page_changeable,
4147 sizeof(verify_er_page_changeable));
4148 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_DEFAULT],
4149 &verify_er_page_default,
4150 sizeof(verify_er_page_default));
4151 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_SAVED],
4152 &verify_er_page_default,
4153 sizeof(verify_er_page_default));
4154 page_index->page_data =
4155 (uint8_t *)lun->mode_pages.verify_er_page;
4158 case SMS_CACHING_PAGE: {
4159 struct scsi_caching_page *caching_page;
4161 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4162 ("subpage %#x for page %#x is incorrect!",
4163 page_index->subpage, page_code));
4164 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4165 &caching_page_default,
4166 sizeof(caching_page_default));
4167 memcpy(&lun->mode_pages.caching_page[
4168 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4169 sizeof(caching_page_changeable));
4170 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4171 &caching_page_default,
4172 sizeof(caching_page_default));
4173 caching_page = &lun->mode_pages.caching_page[
4175 value = ctl_get_opt(&lun->be_lun->options, "writecache");
4176 if (value != NULL && strcmp(value, "off") == 0)
4177 caching_page->flags1 &= ~SCP_WCE;
4178 value = ctl_get_opt(&lun->be_lun->options, "readcache");
4179 if (value != NULL && strcmp(value, "off") == 0)
4180 caching_page->flags1 |= SCP_RCD;
4181 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4182 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4183 sizeof(caching_page_default));
4184 page_index->page_data =
4185 (uint8_t *)lun->mode_pages.caching_page;
4188 case SMS_CONTROL_MODE_PAGE: {
4189 switch (page_index->subpage) {
4190 case SMS_SUBPAGE_PAGE_0: {
4191 struct scsi_control_page *control_page;
4193 memcpy(&lun->mode_pages.control_page[
4195 &control_page_default,
4196 sizeof(control_page_default));
4197 memcpy(&lun->mode_pages.control_page[
4198 CTL_PAGE_CHANGEABLE],
4199 &control_page_changeable,
4200 sizeof(control_page_changeable));
4201 memcpy(&lun->mode_pages.control_page[
4203 &control_page_default,
4204 sizeof(control_page_default));
4205 control_page = &lun->mode_pages.control_page[
4207 value = ctl_get_opt(&lun->be_lun->options,
4209 if (value != NULL &&
4210 strcmp(value, "unrestricted") == 0) {
4211 control_page->queue_flags &=
4212 ~SCP_QUEUE_ALG_MASK;
4213 control_page->queue_flags |=
4214 SCP_QUEUE_ALG_UNRESTRICTED;
4216 memcpy(&lun->mode_pages.control_page[
4218 &lun->mode_pages.control_page[
4220 sizeof(control_page_default));
4221 page_index->page_data =
4222 (uint8_t *)lun->mode_pages.control_page;
4226 memcpy(&lun->mode_pages.control_ext_page[
4228 &control_ext_page_default,
4229 sizeof(control_ext_page_default));
4230 memcpy(&lun->mode_pages.control_ext_page[
4231 CTL_PAGE_CHANGEABLE],
4232 &control_ext_page_changeable,
4233 sizeof(control_ext_page_changeable));
4234 memcpy(&lun->mode_pages.control_ext_page[
4236 &control_ext_page_default,
4237 sizeof(control_ext_page_default));
4238 memcpy(&lun->mode_pages.control_ext_page[
4240 &lun->mode_pages.control_ext_page[
4242 sizeof(control_ext_page_default));
4243 page_index->page_data =
4244 (uint8_t *)lun->mode_pages.control_ext_page;
4247 panic("subpage %#x for page %#x is incorrect!",
4248 page_index->subpage, page_code);
4252 case SMS_INFO_EXCEPTIONS_PAGE: {
4253 switch (page_index->subpage) {
4254 case SMS_SUBPAGE_PAGE_0:
4255 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4257 sizeof(ie_page_default));
4258 memcpy(&lun->mode_pages.ie_page[
4259 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4260 sizeof(ie_page_changeable));
4261 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4263 sizeof(ie_page_default));
4264 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4266 sizeof(ie_page_default));
4267 page_index->page_data =
4268 (uint8_t *)lun->mode_pages.ie_page;
4271 struct ctl_logical_block_provisioning_page *page;
4273 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4275 sizeof(lbp_page_default));
4276 memcpy(&lun->mode_pages.lbp_page[
4277 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4278 sizeof(lbp_page_changeable));
4279 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4281 sizeof(lbp_page_default));
4282 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4283 value = ctl_get_opt(&lun->be_lun->options,
4285 if (value != NULL &&
4286 ctl_expand_number(value, &ival) == 0) {
4287 page->descr[0].flags |= SLBPPD_ENABLED |
4289 if (lun->be_lun->blocksize)
4290 ival /= lun->be_lun->blocksize;
4293 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4294 page->descr[0].count);
4296 value = ctl_get_opt(&lun->be_lun->options,
4298 if (value != NULL &&
4299 ctl_expand_number(value, &ival) == 0) {
4300 page->descr[1].flags |= SLBPPD_ENABLED |
4302 if (lun->be_lun->blocksize)
4303 ival /= lun->be_lun->blocksize;
4306 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4307 page->descr[1].count);
4309 value = ctl_get_opt(&lun->be_lun->options,
4310 "pool-avail-threshold");
4311 if (value != NULL &&
4312 ctl_expand_number(value, &ival) == 0) {
4313 page->descr[2].flags |= SLBPPD_ENABLED |
4315 if (lun->be_lun->blocksize)
4316 ival /= lun->be_lun->blocksize;
4319 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4320 page->descr[2].count);
4322 value = ctl_get_opt(&lun->be_lun->options,
4323 "pool-used-threshold");
4324 if (value != NULL &&
4325 ctl_expand_number(value, &ival) == 0) {
4326 page->descr[3].flags |= SLBPPD_ENABLED |
4328 if (lun->be_lun->blocksize)
4329 ival /= lun->be_lun->blocksize;
4332 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4333 page->descr[3].count);
4335 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4336 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4337 sizeof(lbp_page_default));
4338 page_index->page_data =
4339 (uint8_t *)lun->mode_pages.lbp_page;
4343 panic("subpage %#x for page %#x is incorrect!",
4344 page_index->subpage, page_code);
4348 case SMS_CDDVD_CAPS_PAGE:{
4349 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4350 ("subpage %#x for page %#x is incorrect!",
4351 page_index->subpage, page_code));
4352 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT],
4353 &cddvd_page_default,
4354 sizeof(cddvd_page_default));
4355 memcpy(&lun->mode_pages.cddvd_page[
4356 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable,
4357 sizeof(cddvd_page_changeable));
4358 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4359 &cddvd_page_default,
4360 sizeof(cddvd_page_default));
4361 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT],
4362 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4363 sizeof(cddvd_page_default));
4364 page_index->page_data =
4365 (uint8_t *)lun->mode_pages.cddvd_page;
4369 panic("invalid page code value %#x", page_code);
4373 return (CTL_RETVAL_COMPLETE);
4377 ctl_init_log_page_index(struct ctl_lun *lun)
4379 struct ctl_page_index *page_index;
4382 memcpy(&lun->log_pages.index, log_page_index_template,
4383 sizeof(log_page_index_template));
4386 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4388 page_index = &lun->log_pages.index[i];
4389 if (lun->be_lun->lun_type == T_DIRECT &&
4390 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4392 if (lun->be_lun->lun_type == T_PROCESSOR &&
4393 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4395 if (lun->be_lun->lun_type == T_CDROM &&
4396 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4399 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4400 lun->backend->lun_attr == NULL)
4403 if (page_index->page_code != prev) {
4404 lun->log_pages.pages_page[j] = page_index->page_code;
4405 prev = page_index->page_code;
4408 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4409 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4412 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4413 lun->log_pages.index[0].page_len = j;
4414 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4415 lun->log_pages.index[1].page_len = k * 2;
4416 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4417 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4418 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page;
4419 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page);
4420 lun->log_pages.index[4].page_data = (uint8_t *)&lun->log_pages.ie_page;
4421 lun->log_pages.index[4].page_len = sizeof(lun->log_pages.ie_page);
4423 return (CTL_RETVAL_COMPLETE);
4427 hex2bin(const char *str, uint8_t *buf, int buf_size)
4432 memset(buf, 0, buf_size);
4433 while (isspace(str[0]))
4435 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4438 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4439 while (str[i] == '-') /* Skip dashes in UUIDs. */
4444 else if (isalpha(c))
4445 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4451 buf[i / 2] |= (c << 4);
4455 return ((i + 1) / 2);
4462 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4463 * wants us to allocate the LUN and he can block.
4464 * - ctl_softc is always set
4465 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4467 * Returns 0 for success, non-zero (errno) for failure.
4470 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4471 struct ctl_be_lun *const be_lun)
4473 struct ctl_lun *nlun, *lun;
4474 struct scsi_vpd_id_descriptor *desc;
4475 struct scsi_vpd_id_t10 *t10id;
4476 const char *eui, *naa, *scsiname, *uuid, *vendor, *value;
4477 int lun_number, lun_malloced;
4478 int devidlen, idlen1, idlen2 = 0, len;
4484 * We currently only support Direct Access or Processor LUN types.
4486 switch (be_lun->lun_type) {
4494 be_lun->lun_config_status(be_lun->be_lun,
4495 CTL_LUN_CONFIG_FAILURE);
4498 if (ctl_lun == NULL) {
4499 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4506 memset(lun, 0, sizeof(*lun));
4508 lun->flags = CTL_LUN_MALLOCED;
4510 /* Generate LUN ID. */
4511 devidlen = max(CTL_DEVID_MIN_LEN,
4512 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4513 idlen1 = sizeof(*t10id) + devidlen;
4514 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4515 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4516 if (scsiname != NULL) {
4517 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4518 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4520 eui = ctl_get_opt(&be_lun->options, "eui");
4522 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4524 naa = ctl_get_opt(&be_lun->options, "naa");
4526 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4528 uuid = ctl_get_opt(&be_lun->options, "uuid");
4530 len += sizeof(struct scsi_vpd_id_descriptor) + 18;
4532 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4533 M_CTL, M_WAITOK | M_ZERO);
4534 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4535 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4536 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4537 desc->length = idlen1;
4538 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4539 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4540 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4541 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4543 strncpy(t10id->vendor, vendor,
4544 min(sizeof(t10id->vendor), strlen(vendor)));
4546 strncpy((char *)t10id->vendor_spec_id,
4547 (char *)be_lun->device_id, devidlen);
4548 if (scsiname != NULL) {
4549 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4551 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4552 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4553 SVPD_ID_TYPE_SCSI_NAME;
4554 desc->length = idlen2;
4555 strlcpy(desc->identifier, scsiname, idlen2);
4558 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4560 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4561 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4563 desc->length = hex2bin(eui, desc->identifier, 16);
4564 desc->length = desc->length > 12 ? 16 :
4565 (desc->length > 8 ? 12 : 8);
4566 len -= 16 - desc->length;
4569 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4571 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4572 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4574 desc->length = hex2bin(naa, desc->identifier, 16);
4575 desc->length = desc->length > 8 ? 16 : 8;
4576 len -= 16 - desc->length;
4579 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4581 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4582 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4584 desc->identifier[0] = 0x10;
4585 hex2bin(uuid, &desc->identifier[2], 16);
4588 lun->lun_devid->len = len;
4590 mtx_lock(&ctl_softc->ctl_lock);
4592 * See if the caller requested a particular LUN number. If so, see
4593 * if it is available. Otherwise, allocate the first available LUN.
4595 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4596 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4597 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4598 mtx_unlock(&ctl_softc->ctl_lock);
4599 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4600 printf("ctl: requested LUN ID %d is higher "
4601 "than CTL_MAX_LUNS - 1 (%d)\n",
4602 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4605 * XXX KDM return an error, or just assign
4606 * another LUN ID in this case??
4608 printf("ctl: requested LUN ID %d is already "
4609 "in use\n", be_lun->req_lun_id);
4612 free(lun->lun_devid, M_CTL);
4613 if (lun->flags & CTL_LUN_MALLOCED)
4615 be_lun->lun_config_status(be_lun->be_lun,
4616 CTL_LUN_CONFIG_FAILURE);
4619 lun_number = be_lun->req_lun_id;
4621 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, CTL_MAX_LUNS);
4622 if (lun_number == -1) {
4623 mtx_unlock(&ctl_softc->ctl_lock);
4624 printf("ctl: can't allocate LUN, out of LUNs\n");
4628 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4629 mtx_unlock(&ctl_softc->ctl_lock);
4631 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4632 lun->lun = lun_number;
4633 lun->be_lun = be_lun;
4635 * The processor LUN is always enabled. Disk LUNs come on line
4636 * disabled, and must be enabled by the backend.
4638 lun->flags |= CTL_LUN_DISABLED;
4639 lun->backend = be_lun->be;
4640 be_lun->ctl_lun = lun;
4641 be_lun->lun_id = lun_number;
4642 atomic_add_int(&be_lun->be->num_luns, 1);
4643 if (be_lun->flags & CTL_LUN_FLAG_EJECTED)
4644 lun->flags |= CTL_LUN_EJECTED;
4645 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA)
4646 lun->flags |= CTL_LUN_NO_MEDIA;
4647 if (be_lun->flags & CTL_LUN_FLAG_STOPPED)
4648 lun->flags |= CTL_LUN_STOPPED;
4650 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4651 lun->flags |= CTL_LUN_PRIMARY_SC;
4653 value = ctl_get_opt(&be_lun->options, "removable");
4654 if (value != NULL) {
4655 if (strcmp(value, "on") == 0)
4656 lun->flags |= CTL_LUN_REMOVABLE;
4657 } else if (be_lun->lun_type == T_CDROM)
4658 lun->flags |= CTL_LUN_REMOVABLE;
4660 lun->ctl_softc = ctl_softc;
4662 lun->last_busy = getsbinuptime();
4664 TAILQ_INIT(&lun->ooa_queue);
4665 TAILQ_INIT(&lun->blocked_queue);
4666 STAILQ_INIT(&lun->error_list);
4667 lun->ie_reported = 1;
4668 callout_init_mtx(&lun->ie_callout, &lun->lun_lock, 0);
4669 ctl_tpc_lun_init(lun);
4670 if (lun->flags & CTL_LUN_REMOVABLE) {
4671 lun->prevent = malloc((CTL_MAX_INITIATORS + 31) / 32 * 4,
4676 * Initialize the mode and log page index.
4678 ctl_init_page_index(lun);
4679 ctl_init_log_page_index(lun);
4681 /* Setup statistics gathering */
4682 #ifdef CTL_LEGACY_STATS
4683 lun->legacy_stats.device_type = be_lun->lun_type;
4684 lun->legacy_stats.lun_number = lun_number;
4685 lun->legacy_stats.blocksize = be_lun->blocksize;
4686 if (be_lun->blocksize == 0)
4687 lun->legacy_stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4688 for (len = 0; len < CTL_MAX_PORTS; len++)
4689 lun->legacy_stats.ports[len].targ_port = len;
4690 #endif /* CTL_LEGACY_STATS */
4691 lun->stats.item = lun_number;
4694 * Now, before we insert this lun on the lun list, set the lun
4695 * inventory changed UA for all other luns.
4697 mtx_lock(&ctl_softc->ctl_lock);
4698 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4699 mtx_lock(&nlun->lun_lock);
4700 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4701 mtx_unlock(&nlun->lun_lock);
4703 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4704 ctl_softc->ctl_luns[lun_number] = lun;
4705 ctl_softc->num_luns++;
4706 mtx_unlock(&ctl_softc->ctl_lock);
4708 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4715 * - LUN has already been marked invalid and any pending I/O has been taken
4719 ctl_free_lun(struct ctl_lun *lun)
4721 struct ctl_softc *softc = lun->ctl_softc;
4722 struct ctl_lun *nlun;
4725 mtx_assert(&softc->ctl_lock, MA_OWNED);
4727 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4729 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4731 softc->ctl_luns[lun->lun] = NULL;
4733 if (!TAILQ_EMPTY(&lun->ooa_queue))
4734 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4739 * Tell the backend to free resources, if this LUN has a backend.
4741 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4742 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4744 lun->ie_reportcnt = UINT32_MAX;
4745 callout_drain(&lun->ie_callout);
4747 ctl_tpc_lun_shutdown(lun);
4748 mtx_destroy(&lun->lun_lock);
4749 free(lun->lun_devid, M_CTL);
4750 for (i = 0; i < CTL_MAX_PORTS; i++)
4751 free(lun->pending_ua[i], M_CTL);
4752 for (i = 0; i < CTL_MAX_PORTS; i++)
4753 free(lun->pr_keys[i], M_CTL);
4754 free(lun->write_buffer, M_CTL);
4755 free(lun->prevent, M_CTL);
4756 if (lun->flags & CTL_LUN_MALLOCED)
4759 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4760 mtx_lock(&nlun->lun_lock);
4761 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4762 mtx_unlock(&nlun->lun_lock);
4769 ctl_create_lun(struct ctl_be_lun *be_lun)
4773 * ctl_alloc_lun() should handle all potential failure cases.
4775 ctl_alloc_lun(control_softc, NULL, be_lun);
4779 ctl_add_lun(struct ctl_be_lun *be_lun)
4781 struct ctl_softc *softc = control_softc;
4783 mtx_lock(&softc->ctl_lock);
4784 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links);
4785 mtx_unlock(&softc->ctl_lock);
4786 wakeup(&softc->pending_lun_queue);
4792 ctl_enable_lun(struct ctl_be_lun *be_lun)
4794 struct ctl_softc *softc;
4795 struct ctl_port *port, *nport;
4796 struct ctl_lun *lun;
4799 lun = (struct ctl_lun *)be_lun->ctl_lun;
4800 softc = lun->ctl_softc;
4802 mtx_lock(&softc->ctl_lock);
4803 mtx_lock(&lun->lun_lock);
4804 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4806 * eh? Why did we get called if the LUN is already
4809 mtx_unlock(&lun->lun_lock);
4810 mtx_unlock(&softc->ctl_lock);
4813 lun->flags &= ~CTL_LUN_DISABLED;
4814 mtx_unlock(&lun->lun_lock);
4816 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) {
4817 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4818 port->lun_map != NULL || port->lun_enable == NULL)
4822 * Drop the lock while we call the FETD's enable routine.
4823 * This can lead to a callback into CTL (at least in the
4824 * case of the internal initiator frontend.
4826 mtx_unlock(&softc->ctl_lock);
4827 retval = port->lun_enable(port->targ_lun_arg, lun->lun);
4828 mtx_lock(&softc->ctl_lock);
4830 printf("%s: FETD %s port %d returned error "
4831 "%d for lun_enable on lun %jd\n",
4832 __func__, port->port_name, port->targ_port,
4833 retval, (intmax_t)lun->lun);
4837 mtx_unlock(&softc->ctl_lock);
4838 ctl_isc_announce_lun(lun);
4844 ctl_disable_lun(struct ctl_be_lun *be_lun)
4846 struct ctl_softc *softc;
4847 struct ctl_port *port;
4848 struct ctl_lun *lun;
4851 lun = (struct ctl_lun *)be_lun->ctl_lun;
4852 softc = lun->ctl_softc;
4854 mtx_lock(&softc->ctl_lock);
4855 mtx_lock(&lun->lun_lock);
4856 if (lun->flags & CTL_LUN_DISABLED) {
4857 mtx_unlock(&lun->lun_lock);
4858 mtx_unlock(&softc->ctl_lock);
4861 lun->flags |= CTL_LUN_DISABLED;
4862 mtx_unlock(&lun->lun_lock);
4864 STAILQ_FOREACH(port, &softc->port_list, links) {
4865 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4866 port->lun_map != NULL || port->lun_disable == NULL)
4870 * Drop the lock before we call the frontend's disable
4871 * routine, to avoid lock order reversals.
4873 * XXX KDM what happens if the frontend list changes while
4874 * we're traversing it? It's unlikely, but should be handled.
4876 mtx_unlock(&softc->ctl_lock);
4877 retval = port->lun_disable(port->targ_lun_arg, lun->lun);
4878 mtx_lock(&softc->ctl_lock);
4880 printf("%s: FETD %s port %d returned error "
4881 "%d for lun_disable on lun %jd\n",
4882 __func__, port->port_name, port->targ_port,
4883 retval, (intmax_t)lun->lun);
4887 mtx_unlock(&softc->ctl_lock);
4888 ctl_isc_announce_lun(lun);
4894 ctl_start_lun(struct ctl_be_lun *be_lun)
4896 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4898 mtx_lock(&lun->lun_lock);
4899 lun->flags &= ~CTL_LUN_STOPPED;
4900 mtx_unlock(&lun->lun_lock);
4905 ctl_stop_lun(struct ctl_be_lun *be_lun)
4907 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4909 mtx_lock(&lun->lun_lock);
4910 lun->flags |= CTL_LUN_STOPPED;
4911 mtx_unlock(&lun->lun_lock);
4916 ctl_lun_no_media(struct ctl_be_lun *be_lun)
4918 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4920 mtx_lock(&lun->lun_lock);
4921 lun->flags |= CTL_LUN_NO_MEDIA;
4922 mtx_unlock(&lun->lun_lock);
4927 ctl_lun_has_media(struct ctl_be_lun *be_lun)
4929 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4930 union ctl_ha_msg msg;
4932 mtx_lock(&lun->lun_lock);
4933 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED);
4934 if (lun->flags & CTL_LUN_REMOVABLE)
4935 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE);
4936 mtx_unlock(&lun->lun_lock);
4937 if ((lun->flags & CTL_LUN_REMOVABLE) &&
4938 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4939 bzero(&msg.ua, sizeof(msg.ua));
4940 msg.hdr.msg_type = CTL_MSG_UA;
4941 msg.hdr.nexus.initid = -1;
4942 msg.hdr.nexus.targ_port = -1;
4943 msg.hdr.nexus.targ_lun = lun->lun;
4944 msg.hdr.nexus.targ_mapped_lun = lun->lun;
4947 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE;
4948 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4955 ctl_lun_ejected(struct ctl_be_lun *be_lun)
4957 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4959 mtx_lock(&lun->lun_lock);
4960 lun->flags |= CTL_LUN_EJECTED;
4961 mtx_unlock(&lun->lun_lock);
4966 ctl_lun_primary(struct ctl_be_lun *be_lun)
4968 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4970 mtx_lock(&lun->lun_lock);
4971 lun->flags |= CTL_LUN_PRIMARY_SC;
4972 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4973 mtx_unlock(&lun->lun_lock);
4974 ctl_isc_announce_lun(lun);
4979 ctl_lun_secondary(struct ctl_be_lun *be_lun)
4981 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4983 mtx_lock(&lun->lun_lock);
4984 lun->flags &= ~CTL_LUN_PRIMARY_SC;
4985 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4986 mtx_unlock(&lun->lun_lock);
4987 ctl_isc_announce_lun(lun);
4992 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4994 struct ctl_softc *softc;
4995 struct ctl_lun *lun;
4997 lun = (struct ctl_lun *)be_lun->ctl_lun;
4998 softc = lun->ctl_softc;
5000 mtx_lock(&lun->lun_lock);
5003 * The LUN needs to be disabled before it can be marked invalid.
5005 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
5006 mtx_unlock(&lun->lun_lock);
5010 * Mark the LUN invalid.
5012 lun->flags |= CTL_LUN_INVALID;
5015 * If there is nothing in the OOA queue, go ahead and free the LUN.
5016 * If we have something in the OOA queue, we'll free it when the
5017 * last I/O completes.
5019 if (TAILQ_EMPTY(&lun->ooa_queue)) {
5020 mtx_unlock(&lun->lun_lock);
5021 mtx_lock(&softc->ctl_lock);
5023 mtx_unlock(&softc->ctl_lock);
5025 mtx_unlock(&lun->lun_lock);
5031 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
5033 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5034 union ctl_ha_msg msg;
5036 mtx_lock(&lun->lun_lock);
5037 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE);
5038 mtx_unlock(&lun->lun_lock);
5039 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
5040 /* Send msg to other side. */
5041 bzero(&msg.ua, sizeof(msg.ua));
5042 msg.hdr.msg_type = CTL_MSG_UA;
5043 msg.hdr.nexus.initid = -1;
5044 msg.hdr.nexus.targ_port = -1;
5045 msg.hdr.nexus.targ_lun = lun->lun;
5046 msg.hdr.nexus.targ_mapped_lun = lun->lun;
5049 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE;
5050 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
5056 * Backend "memory move is complete" callback for requests that never
5057 * make it down to say RAIDCore's configuration code.
5060 ctl_config_move_done(union ctl_io *io)
5064 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
5065 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
5066 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
5068 if ((io->io_hdr.port_status != 0) &&
5069 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5070 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5071 ctl_set_internal_failure(&io->scsiio, /*sks_valid*/ 1,
5072 /*retry_count*/ io->io_hdr.port_status);
5073 } else if (io->scsiio.kern_data_resid != 0 &&
5074 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT &&
5075 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5076 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5077 ctl_set_invalid_field_ciu(&io->scsiio);
5080 if (ctl_debug & CTL_DEBUG_CDB_DATA)
5082 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
5083 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5084 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
5085 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5087 * XXX KDM just assuming a single pointer here, and not a
5088 * S/G list. If we start using S/G lists for config data,
5089 * we'll need to know how to clean them up here as well.
5091 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5092 free(io->scsiio.kern_data_ptr, M_CTL);
5094 retval = CTL_RETVAL_COMPLETE;
5097 * XXX KDM now we need to continue data movement. Some
5099 * - call ctl_scsiio() again? We don't do this for data
5100 * writes, because for those at least we know ahead of
5101 * time where the write will go and how long it is. For
5102 * config writes, though, that information is largely
5103 * contained within the write itself, thus we need to
5104 * parse out the data again.
5106 * - Call some other function once the data is in?
5110 * XXX KDM call ctl_scsiio() again for now, and check flag
5111 * bits to see whether we're allocated or not.
5113 retval = ctl_scsiio(&io->scsiio);
5119 * This gets called by a backend driver when it is done with a
5120 * data_submit method.
5123 ctl_data_submit_done(union ctl_io *io)
5126 * If the IO_CONT flag is set, we need to call the supplied
5127 * function to continue processing the I/O, instead of completing
5130 * If there is an error, though, we don't want to keep processing.
5131 * Instead, just send status back to the initiator.
5133 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5134 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5135 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5136 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5137 io->scsiio.io_cont(io);
5144 * This gets called by a backend driver when it is done with a
5145 * configuration write.
5148 ctl_config_write_done(union ctl_io *io)
5153 * If the IO_CONT flag is set, we need to call the supplied
5154 * function to continue processing the I/O, instead of completing
5157 * If there is an error, though, we don't want to keep processing.
5158 * Instead, just send status back to the initiator.
5160 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5161 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5162 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5163 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5164 io->scsiio.io_cont(io);
5168 * Since a configuration write can be done for commands that actually
5169 * have data allocated, like write buffer, and commands that have
5170 * no data, like start/stop unit, we need to check here.
5172 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5173 buf = io->scsiio.kern_data_ptr;
5182 ctl_config_read_done(union ctl_io *io)
5187 * If there is some error -- we are done, skip data transfer.
5189 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
5190 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5191 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
5192 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5193 buf = io->scsiio.kern_data_ptr;
5203 * If the IO_CONT flag is set, we need to call the supplied
5204 * function to continue processing the I/O, instead of completing
5207 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
5208 io->scsiio.io_cont(io);
5216 * SCSI release command.
5219 ctl_scsi_release(struct ctl_scsiio *ctsio)
5221 struct ctl_lun *lun = CTL_LUN(ctsio);
5224 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5226 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5229 * XXX KDM right now, we only support LUN reservation. We don't
5230 * support 3rd party reservations, or extent reservations, which
5231 * might actually need the parameter list. If we've gotten this
5232 * far, we've got a LUN reservation. Anything else got kicked out
5233 * above. So, according to SPC, ignore the length.
5236 mtx_lock(&lun->lun_lock);
5239 * According to SPC, it is not an error for an intiator to attempt
5240 * to release a reservation on a LUN that isn't reserved, or that
5241 * is reserved by another initiator. The reservation can only be
5242 * released, though, by the initiator who made it or by one of
5243 * several reset type events.
5245 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5246 lun->flags &= ~CTL_LUN_RESERVED;
5248 mtx_unlock(&lun->lun_lock);
5250 ctl_set_success(ctsio);
5251 ctl_done((union ctl_io *)ctsio);
5252 return (CTL_RETVAL_COMPLETE);
5256 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5258 struct ctl_lun *lun = CTL_LUN(ctsio);
5261 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5263 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5266 * XXX KDM right now, we only support LUN reservation. We don't
5267 * support 3rd party reservations, or extent reservations, which
5268 * might actually need the parameter list. If we've gotten this
5269 * far, we've got a LUN reservation. Anything else got kicked out
5270 * above. So, according to SPC, ignore the length.
5273 mtx_lock(&lun->lun_lock);
5274 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5275 ctl_set_reservation_conflict(ctsio);
5279 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */
5280 if (lun->flags & CTL_LUN_PR_RESERVED) {
5281 ctl_set_success(ctsio);
5285 lun->flags |= CTL_LUN_RESERVED;
5286 lun->res_idx = residx;
5287 ctl_set_success(ctsio);
5290 mtx_unlock(&lun->lun_lock);
5291 ctl_done((union ctl_io *)ctsio);
5292 return (CTL_RETVAL_COMPLETE);
5296 ctl_start_stop(struct ctl_scsiio *ctsio)
5298 struct ctl_lun *lun = CTL_LUN(ctsio);
5299 struct scsi_start_stop_unit *cdb;
5302 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5304 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5306 if ((cdb->how & SSS_PC_MASK) == 0) {
5307 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
5308 (cdb->how & SSS_START) == 0) {
5311 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5312 if (ctl_get_prkey(lun, residx) == 0 ||
5313 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) {
5315 ctl_set_reservation_conflict(ctsio);
5316 ctl_done((union ctl_io *)ctsio);
5317 return (CTL_RETVAL_COMPLETE);
5321 if ((cdb->how & SSS_LOEJ) &&
5322 (lun->flags & CTL_LUN_REMOVABLE) == 0) {
5323 ctl_set_invalid_field(ctsio,
5329 ctl_done((union ctl_io *)ctsio);
5330 return (CTL_RETVAL_COMPLETE);
5333 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) &&
5334 lun->prevent_count > 0) {
5335 /* "Medium removal prevented" */
5336 ctl_set_sense(ctsio, /*current_error*/ 1,
5337 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ?
5338 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST,
5339 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE);
5340 ctl_done((union ctl_io *)ctsio);
5341 return (CTL_RETVAL_COMPLETE);
5345 retval = lun->backend->config_write((union ctl_io *)ctsio);
5350 ctl_prevent_allow(struct ctl_scsiio *ctsio)
5352 struct ctl_lun *lun = CTL_LUN(ctsio);
5353 struct scsi_prevent *cdb;
5357 CTL_DEBUG_PRINT(("ctl_prevent_allow\n"));
5359 cdb = (struct scsi_prevent *)ctsio->cdb;
5361 if ((lun->flags & CTL_LUN_REMOVABLE) == 0 || lun->prevent == NULL) {
5362 ctl_set_invalid_opcode(ctsio);
5363 ctl_done((union ctl_io *)ctsio);
5364 return (CTL_RETVAL_COMPLETE);
5367 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5368 mtx_lock(&lun->lun_lock);
5369 if ((cdb->how & PR_PREVENT) &&
5370 ctl_is_set(lun->prevent, initidx) == 0) {
5371 ctl_set_mask(lun->prevent, initidx);
5372 lun->prevent_count++;
5373 } else if ((cdb->how & PR_PREVENT) == 0 &&
5374 ctl_is_set(lun->prevent, initidx)) {
5375 ctl_clear_mask(lun->prevent, initidx);
5376 lun->prevent_count--;
5378 mtx_unlock(&lun->lun_lock);
5379 retval = lun->backend->config_write((union ctl_io *)ctsio);
5384 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5385 * we don't really do anything with the LBA and length fields if the user
5386 * passes them in. Instead we'll just flush out the cache for the entire
5390 ctl_sync_cache(struct ctl_scsiio *ctsio)
5392 struct ctl_lun *lun = CTL_LUN(ctsio);
5393 struct ctl_lba_len_flags *lbalen;
5394 uint64_t starting_lba;
5395 uint32_t block_count;
5399 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5403 switch (ctsio->cdb[0]) {
5404 case SYNCHRONIZE_CACHE: {
5405 struct scsi_sync_cache *cdb;
5406 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5408 starting_lba = scsi_4btoul(cdb->begin_lba);
5409 block_count = scsi_2btoul(cdb->lb_count);
5413 case SYNCHRONIZE_CACHE_16: {
5414 struct scsi_sync_cache_16 *cdb;
5415 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5417 starting_lba = scsi_8btou64(cdb->begin_lba);
5418 block_count = scsi_4btoul(cdb->lb_count);
5423 ctl_set_invalid_opcode(ctsio);
5424 ctl_done((union ctl_io *)ctsio);
5426 break; /* NOTREACHED */
5430 * We check the LBA and length, but don't do anything with them.
5431 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5432 * get flushed. This check will just help satisfy anyone who wants
5433 * to see an error for an out of range LBA.
5435 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5436 ctl_set_lba_out_of_range(ctsio,
5437 MAX(starting_lba, lun->be_lun->maxlba + 1));
5438 ctl_done((union ctl_io *)ctsio);
5442 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5443 lbalen->lba = starting_lba;
5444 lbalen->len = block_count;
5445 lbalen->flags = byte2;
5446 retval = lun->backend->config_write((union ctl_io *)ctsio);
5453 ctl_format(struct ctl_scsiio *ctsio)
5455 struct scsi_format *cdb;
5456 int length, defect_list_len;
5458 CTL_DEBUG_PRINT(("ctl_format\n"));
5460 cdb = (struct scsi_format *)ctsio->cdb;
5463 if (cdb->byte2 & SF_FMTDATA) {
5464 if (cdb->byte2 & SF_LONGLIST)
5465 length = sizeof(struct scsi_format_header_long);
5467 length = sizeof(struct scsi_format_header_short);
5470 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5472 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5473 ctsio->kern_data_len = length;
5474 ctsio->kern_total_len = length;
5475 ctsio->kern_rel_offset = 0;
5476 ctsio->kern_sg_entries = 0;
5477 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5478 ctsio->be_move_done = ctl_config_move_done;
5479 ctl_datamove((union ctl_io *)ctsio);
5481 return (CTL_RETVAL_COMPLETE);
5484 defect_list_len = 0;
5486 if (cdb->byte2 & SF_FMTDATA) {
5487 if (cdb->byte2 & SF_LONGLIST) {
5488 struct scsi_format_header_long *header;
5490 header = (struct scsi_format_header_long *)
5491 ctsio->kern_data_ptr;
5493 defect_list_len = scsi_4btoul(header->defect_list_len);
5494 if (defect_list_len != 0) {
5495 ctl_set_invalid_field(ctsio,
5504 struct scsi_format_header_short *header;
5506 header = (struct scsi_format_header_short *)
5507 ctsio->kern_data_ptr;
5509 defect_list_len = scsi_2btoul(header->defect_list_len);
5510 if (defect_list_len != 0) {
5511 ctl_set_invalid_field(ctsio,
5522 ctl_set_success(ctsio);
5525 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5526 free(ctsio->kern_data_ptr, M_CTL);
5527 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5530 ctl_done((union ctl_io *)ctsio);
5531 return (CTL_RETVAL_COMPLETE);
5535 ctl_read_buffer(struct ctl_scsiio *ctsio)
5537 struct ctl_lun *lun = CTL_LUN(ctsio);
5538 uint64_t buffer_offset;
5541 static uint8_t descr[4];
5542 static uint8_t echo_descr[4] = { 0 };
5544 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5546 switch (ctsio->cdb[0]) {
5548 struct scsi_read_buffer *cdb;
5550 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5551 buffer_offset = scsi_3btoul(cdb->offset);
5552 len = scsi_3btoul(cdb->length);
5556 case READ_BUFFER_16: {
5557 struct scsi_read_buffer_16 *cdb;
5559 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb;
5560 buffer_offset = scsi_8btou64(cdb->offset);
5561 len = scsi_4btoul(cdb->length);
5565 default: /* This shouldn't happen. */
5566 ctl_set_invalid_opcode(ctsio);
5567 ctl_done((union ctl_io *)ctsio);
5568 return (CTL_RETVAL_COMPLETE);
5571 if (buffer_offset > CTL_WRITE_BUFFER_SIZE ||
5572 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5573 ctl_set_invalid_field(ctsio,
5579 ctl_done((union ctl_io *)ctsio);
5580 return (CTL_RETVAL_COMPLETE);
5583 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5585 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5586 ctsio->kern_data_ptr = descr;
5587 len = min(len, sizeof(descr));
5588 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5589 ctsio->kern_data_ptr = echo_descr;
5590 len = min(len, sizeof(echo_descr));
5592 if (lun->write_buffer == NULL) {
5593 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5596 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5598 ctsio->kern_data_len = len;
5599 ctsio->kern_total_len = len;
5600 ctsio->kern_rel_offset = 0;
5601 ctsio->kern_sg_entries = 0;
5602 ctl_set_success(ctsio);
5603 ctsio->be_move_done = ctl_config_move_done;
5604 ctl_datamove((union ctl_io *)ctsio);
5605 return (CTL_RETVAL_COMPLETE);
5609 ctl_write_buffer(struct ctl_scsiio *ctsio)
5611 struct ctl_lun *lun = CTL_LUN(ctsio);
5612 struct scsi_write_buffer *cdb;
5613 int buffer_offset, len;
5615 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5617 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5619 len = scsi_3btoul(cdb->length);
5620 buffer_offset = scsi_3btoul(cdb->offset);
5622 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5623 ctl_set_invalid_field(ctsio,
5629 ctl_done((union ctl_io *)ctsio);
5630 return (CTL_RETVAL_COMPLETE);
5634 * If we've got a kernel request that hasn't been malloced yet,
5635 * malloc it and tell the caller the data buffer is here.
5637 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5638 if (lun->write_buffer == NULL) {
5639 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5642 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5643 ctsio->kern_data_len = len;
5644 ctsio->kern_total_len = len;
5645 ctsio->kern_rel_offset = 0;
5646 ctsio->kern_sg_entries = 0;
5647 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5648 ctsio->be_move_done = ctl_config_move_done;
5649 ctl_datamove((union ctl_io *)ctsio);
5651 return (CTL_RETVAL_COMPLETE);
5654 ctl_set_success(ctsio);
5655 ctl_done((union ctl_io *)ctsio);
5656 return (CTL_RETVAL_COMPLETE);
5660 ctl_write_same(struct ctl_scsiio *ctsio)
5662 struct ctl_lun *lun = CTL_LUN(ctsio);
5663 struct ctl_lba_len_flags *lbalen;
5665 uint32_t num_blocks;
5669 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5671 switch (ctsio->cdb[0]) {
5672 case WRITE_SAME_10: {
5673 struct scsi_write_same_10 *cdb;
5675 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5677 lba = scsi_4btoul(cdb->addr);
5678 num_blocks = scsi_2btoul(cdb->length);
5682 case WRITE_SAME_16: {
5683 struct scsi_write_same_16 *cdb;
5685 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5687 lba = scsi_8btou64(cdb->addr);
5688 num_blocks = scsi_4btoul(cdb->length);
5694 * We got a command we don't support. This shouldn't
5695 * happen, commands should be filtered out above us.
5697 ctl_set_invalid_opcode(ctsio);
5698 ctl_done((union ctl_io *)ctsio);
5700 return (CTL_RETVAL_COMPLETE);
5701 break; /* NOTREACHED */
5704 /* ANCHOR flag can be used only together with UNMAP */
5705 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) {
5706 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5707 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5708 ctl_done((union ctl_io *)ctsio);
5709 return (CTL_RETVAL_COMPLETE);
5713 * The first check is to make sure we're in bounds, the second
5714 * check is to catch wrap-around problems. If the lba + num blocks
5715 * is less than the lba, then we've wrapped around and the block
5716 * range is invalid anyway.
5718 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5719 || ((lba + num_blocks) < lba)) {
5720 ctl_set_lba_out_of_range(ctsio,
5721 MAX(lba, lun->be_lun->maxlba + 1));
5722 ctl_done((union ctl_io *)ctsio);
5723 return (CTL_RETVAL_COMPLETE);
5726 /* Zero number of blocks means "to the last logical block" */
5727 if (num_blocks == 0) {
5728 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5729 ctl_set_invalid_field(ctsio,
5735 ctl_done((union ctl_io *)ctsio);
5736 return (CTL_RETVAL_COMPLETE);
5738 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5741 len = lun->be_lun->blocksize;
5744 * If we've got a kernel request that hasn't been malloced yet,
5745 * malloc it and tell the caller the data buffer is here.
5747 if ((byte2 & SWS_NDOB) == 0 &&
5748 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5749 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5750 ctsio->kern_data_len = len;
5751 ctsio->kern_total_len = len;
5752 ctsio->kern_rel_offset = 0;
5753 ctsio->kern_sg_entries = 0;
5754 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5755 ctsio->be_move_done = ctl_config_move_done;
5756 ctl_datamove((union ctl_io *)ctsio);
5758 return (CTL_RETVAL_COMPLETE);
5761 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5763 lbalen->len = num_blocks;
5764 lbalen->flags = byte2;
5765 retval = lun->backend->config_write((union ctl_io *)ctsio);
5771 ctl_unmap(struct ctl_scsiio *ctsio)
5773 struct ctl_lun *lun = CTL_LUN(ctsio);
5774 struct scsi_unmap *cdb;
5775 struct ctl_ptr_len_flags *ptrlen;
5776 struct scsi_unmap_header *hdr;
5777 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5779 uint32_t num_blocks;
5783 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5785 cdb = (struct scsi_unmap *)ctsio->cdb;
5786 len = scsi_2btoul(cdb->length);
5790 * If we've got a kernel request that hasn't been malloced yet,
5791 * malloc it and tell the caller the data buffer is here.
5793 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5794 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5795 ctsio->kern_data_len = len;
5796 ctsio->kern_total_len = len;
5797 ctsio->kern_rel_offset = 0;
5798 ctsio->kern_sg_entries = 0;
5799 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5800 ctsio->be_move_done = ctl_config_move_done;
5801 ctl_datamove((union ctl_io *)ctsio);
5803 return (CTL_RETVAL_COMPLETE);
5806 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5807 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5808 if (len < sizeof (*hdr) ||
5809 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5810 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5811 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5812 ctl_set_invalid_field(ctsio,
5820 len = scsi_2btoul(hdr->desc_length);
5821 buf = (struct scsi_unmap_desc *)(hdr + 1);
5822 end = buf + len / sizeof(*buf);
5825 for (range = buf; range < end; range++) {
5826 lba = scsi_8btou64(range->lba);
5827 num_blocks = scsi_4btoul(range->length);
5828 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5829 || ((lba + num_blocks) < lba)) {
5830 ctl_set_lba_out_of_range(ctsio,
5831 MAX(lba, lun->be_lun->maxlba + 1));
5832 ctl_done((union ctl_io *)ctsio);
5833 return (CTL_RETVAL_COMPLETE);
5835 if (num_blocks != 0)
5840 * Block backend can not handle zero last range.
5841 * Filter it out and return if there is nothing left.
5843 len = (uint8_t *)endnz - (uint8_t *)buf;
5845 ctl_set_success(ctsio);
5849 mtx_lock(&lun->lun_lock);
5850 ptrlen = (struct ctl_ptr_len_flags *)
5851 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5852 ptrlen->ptr = (void *)buf;
5854 ptrlen->flags = byte2;
5855 ctl_check_blocked(lun);
5856 mtx_unlock(&lun->lun_lock);
5858 retval = lun->backend->config_write((union ctl_io *)ctsio);
5862 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5863 free(ctsio->kern_data_ptr, M_CTL);
5864 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5866 ctl_done((union ctl_io *)ctsio);
5867 return (CTL_RETVAL_COMPLETE);
5871 ctl_default_page_handler(struct ctl_scsiio *ctsio,
5872 struct ctl_page_index *page_index, uint8_t *page_ptr)
5874 struct ctl_lun *lun = CTL_LUN(ctsio);
5875 uint8_t *current_cp;
5879 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5882 current_cp = (page_index->page_data + (page_index->page_len *
5885 mtx_lock(&lun->lun_lock);
5886 if (memcmp(current_cp, page_ptr, page_index->page_len)) {
5887 memcpy(current_cp, page_ptr, page_index->page_len);
5891 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5892 mtx_unlock(&lun->lun_lock);
5894 ctl_isc_announce_mode(lun,
5895 ctl_get_initindex(&ctsio->io_hdr.nexus),
5896 page_index->page_code, page_index->subpage);
5898 return (CTL_RETVAL_COMPLETE);
5902 ctl_ie_timer(void *arg)
5904 struct ctl_lun *lun = arg;
5907 if (lun->ie_asc == 0)
5910 if (lun->MODE_IE.mrie == SIEP_MRIE_UA)
5911 ctl_est_ua_all(lun, -1, CTL_UA_IE);
5913 lun->ie_reported = 0;
5915 if (lun->ie_reportcnt < scsi_4btoul(lun->MODE_IE.report_count)) {
5916 lun->ie_reportcnt++;
5917 t = scsi_4btoul(lun->MODE_IE.interval_timer);
5918 if (t == 0 || t == UINT32_MAX)
5919 t = 3000; /* 5 min */
5920 callout_schedule(&lun->ie_callout, t * hz / 10);
5925 ctl_ie_page_handler(struct ctl_scsiio *ctsio,
5926 struct ctl_page_index *page_index, uint8_t *page_ptr)
5928 struct ctl_lun *lun = CTL_LUN(ctsio);
5929 struct scsi_info_exceptions_page *pg;
5932 (void)ctl_default_page_handler(ctsio, page_index, page_ptr);
5934 pg = (struct scsi_info_exceptions_page *)page_ptr;
5935 mtx_lock(&lun->lun_lock);
5936 if (pg->info_flags & SIEP_FLAGS_TEST) {
5938 lun->ie_ascq = 0xff;
5939 if (pg->mrie == SIEP_MRIE_UA) {
5940 ctl_est_ua_all(lun, -1, CTL_UA_IE);
5941 lun->ie_reported = 1;
5943 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
5944 lun->ie_reported = -1;
5946 lun->ie_reportcnt = 1;
5947 if (lun->ie_reportcnt < scsi_4btoul(pg->report_count)) {
5948 lun->ie_reportcnt++;
5949 t = scsi_4btoul(pg->interval_timer);
5950 if (t == 0 || t == UINT32_MAX)
5951 t = 3000; /* 5 min */
5952 callout_reset(&lun->ie_callout, t * hz / 10,
5958 lun->ie_reported = 1;
5959 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
5960 lun->ie_reportcnt = UINT32_MAX;
5961 callout_stop(&lun->ie_callout);
5963 mtx_unlock(&lun->lun_lock);
5964 return (CTL_RETVAL_COMPLETE);
5968 ctl_do_mode_select(union ctl_io *io)
5970 struct ctl_lun *lun = CTL_LUN(io);
5971 struct scsi_mode_page_header *page_header;
5972 struct ctl_page_index *page_index;
5973 struct ctl_scsiio *ctsio;
5974 int page_len, page_len_offset, page_len_size;
5975 union ctl_modepage_info *modepage_info;
5976 uint16_t *len_left, *len_used;
5979 ctsio = &io->scsiio;
5983 modepage_info = (union ctl_modepage_info *)
5984 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
5985 len_left = &modepage_info->header.len_left;
5986 len_used = &modepage_info->header.len_used;
5990 page_header = (struct scsi_mode_page_header *)
5991 (ctsio->kern_data_ptr + *len_used);
5993 if (*len_left == 0) {
5994 free(ctsio->kern_data_ptr, M_CTL);
5995 ctl_set_success(ctsio);
5996 ctl_done((union ctl_io *)ctsio);
5997 return (CTL_RETVAL_COMPLETE);
5998 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6000 free(ctsio->kern_data_ptr, M_CTL);
6001 ctl_set_param_len_error(ctsio);
6002 ctl_done((union ctl_io *)ctsio);
6003 return (CTL_RETVAL_COMPLETE);
6005 } else if ((page_header->page_code & SMPH_SPF)
6006 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6008 free(ctsio->kern_data_ptr, M_CTL);
6009 ctl_set_param_len_error(ctsio);
6010 ctl_done((union ctl_io *)ctsio);
6011 return (CTL_RETVAL_COMPLETE);
6016 * XXX KDM should we do something with the block descriptor?
6018 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6019 page_index = &lun->mode_pages.index[i];
6020 if (lun->be_lun->lun_type == T_DIRECT &&
6021 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6023 if (lun->be_lun->lun_type == T_PROCESSOR &&
6024 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6026 if (lun->be_lun->lun_type == T_CDROM &&
6027 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6030 if ((page_index->page_code & SMPH_PC_MASK) !=
6031 (page_header->page_code & SMPH_PC_MASK))
6035 * If neither page has a subpage code, then we've got a
6038 if (((page_index->page_code & SMPH_SPF) == 0)
6039 && ((page_header->page_code & SMPH_SPF) == 0)) {
6040 page_len = page_header->page_length;
6045 * If both pages have subpages, then the subpage numbers
6048 if ((page_index->page_code & SMPH_SPF)
6049 && (page_header->page_code & SMPH_SPF)) {
6050 struct scsi_mode_page_header_sp *sph;
6052 sph = (struct scsi_mode_page_header_sp *)page_header;
6053 if (page_index->subpage == sph->subpage) {
6054 page_len = scsi_2btoul(sph->page_length);
6061 * If we couldn't find the page, or if we don't have a mode select
6062 * handler for it, send back an error to the user.
6064 if ((i >= CTL_NUM_MODE_PAGES)
6065 || (page_index->select_handler == NULL)) {
6066 ctl_set_invalid_field(ctsio,
6069 /*field*/ *len_used,
6072 free(ctsio->kern_data_ptr, M_CTL);
6073 ctl_done((union ctl_io *)ctsio);
6074 return (CTL_RETVAL_COMPLETE);
6077 if (page_index->page_code & SMPH_SPF) {
6078 page_len_offset = 2;
6082 page_len_offset = 1;
6086 * If the length the initiator gives us isn't the one we specify in
6087 * the mode page header, or if they didn't specify enough data in
6088 * the CDB to avoid truncating this page, kick out the request.
6090 if (page_len != page_index->page_len - page_len_offset - page_len_size) {
6091 ctl_set_invalid_field(ctsio,
6094 /*field*/ *len_used + page_len_offset,
6097 free(ctsio->kern_data_ptr, M_CTL);
6098 ctl_done((union ctl_io *)ctsio);
6099 return (CTL_RETVAL_COMPLETE);
6101 if (*len_left < page_index->page_len) {
6102 free(ctsio->kern_data_ptr, M_CTL);
6103 ctl_set_param_len_error(ctsio);
6104 ctl_done((union ctl_io *)ctsio);
6105 return (CTL_RETVAL_COMPLETE);
6109 * Run through the mode page, checking to make sure that the bits
6110 * the user changed are actually legal for him to change.
6112 for (i = 0; i < page_index->page_len; i++) {
6113 uint8_t *user_byte, *change_mask, *current_byte;
6117 user_byte = (uint8_t *)page_header + i;
6118 change_mask = page_index->page_data +
6119 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6120 current_byte = page_index->page_data +
6121 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6124 * Check to see whether the user set any bits in this byte
6125 * that he is not allowed to set.
6127 if ((*user_byte & ~(*change_mask)) ==
6128 (*current_byte & ~(*change_mask)))
6132 * Go through bit by bit to determine which one is illegal.
6135 for (j = 7; j >= 0; j--) {
6136 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6137 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6142 ctl_set_invalid_field(ctsio,
6145 /*field*/ *len_used + i,
6148 free(ctsio->kern_data_ptr, M_CTL);
6149 ctl_done((union ctl_io *)ctsio);
6150 return (CTL_RETVAL_COMPLETE);
6154 * Decrement these before we call the page handler, since we may
6155 * end up getting called back one way or another before the handler
6156 * returns to this context.
6158 *len_left -= page_index->page_len;
6159 *len_used += page_index->page_len;
6161 retval = page_index->select_handler(ctsio, page_index,
6162 (uint8_t *)page_header);
6165 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6166 * wait until this queued command completes to finish processing
6167 * the mode page. If it returns anything other than
6168 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6169 * already set the sense information, freed the data pointer, and
6170 * completed the io for us.
6172 if (retval != CTL_RETVAL_COMPLETE)
6173 goto bailout_no_done;
6176 * If the initiator sent us more than one page, parse the next one.
6181 ctl_set_success(ctsio);
6182 free(ctsio->kern_data_ptr, M_CTL);
6183 ctl_done((union ctl_io *)ctsio);
6187 return (CTL_RETVAL_COMPLETE);
6192 ctl_mode_select(struct ctl_scsiio *ctsio)
6194 struct ctl_lun *lun = CTL_LUN(ctsio);
6195 union ctl_modepage_info *modepage_info;
6196 int bd_len, i, header_size, param_len, pf, rtd, sp;
6199 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6200 switch (ctsio->cdb[0]) {
6201 case MODE_SELECT_6: {
6202 struct scsi_mode_select_6 *cdb;
6204 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6206 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6207 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6208 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6209 param_len = cdb->length;
6210 header_size = sizeof(struct scsi_mode_header_6);
6213 case MODE_SELECT_10: {
6214 struct scsi_mode_select_10 *cdb;
6216 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6218 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6219 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6220 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6221 param_len = scsi_2btoul(cdb->length);
6222 header_size = sizeof(struct scsi_mode_header_10);
6226 ctl_set_invalid_opcode(ctsio);
6227 ctl_done((union ctl_io *)ctsio);
6228 return (CTL_RETVAL_COMPLETE);
6232 if (param_len != 0) {
6233 ctl_set_invalid_field(ctsio, /*sks_valid*/ 0,
6234 /*command*/ 1, /*field*/ 0,
6235 /*bit_valid*/ 0, /*bit*/ 0);
6236 ctl_done((union ctl_io *)ctsio);
6237 return (CTL_RETVAL_COMPLETE);
6240 /* Revert to defaults. */
6241 ctl_init_page_index(lun);
6242 mtx_lock(&lun->lun_lock);
6243 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
6244 mtx_unlock(&lun->lun_lock);
6245 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6246 ctl_isc_announce_mode(lun, -1,
6247 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
6248 lun->mode_pages.index[i].subpage);
6250 ctl_set_success(ctsio);
6251 ctl_done((union ctl_io *)ctsio);
6252 return (CTL_RETVAL_COMPLETE);
6257 * "A parameter list length of zero indicates that the Data-Out Buffer
6258 * shall be empty. This condition shall not be considered as an error."
6260 if (param_len == 0) {
6261 ctl_set_success(ctsio);
6262 ctl_done((union ctl_io *)ctsio);
6263 return (CTL_RETVAL_COMPLETE);
6267 * Since we'll hit this the first time through, prior to
6268 * allocation, we don't need to free a data buffer here.
6270 if (param_len < header_size) {
6271 ctl_set_param_len_error(ctsio);
6272 ctl_done((union ctl_io *)ctsio);
6273 return (CTL_RETVAL_COMPLETE);
6277 * Allocate the data buffer and grab the user's data. In theory,
6278 * we shouldn't have to sanity check the parameter list length here
6279 * because the maximum size is 64K. We should be able to malloc
6280 * that much without too many problems.
6282 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6283 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6284 ctsio->kern_data_len = param_len;
6285 ctsio->kern_total_len = param_len;
6286 ctsio->kern_rel_offset = 0;
6287 ctsio->kern_sg_entries = 0;
6288 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6289 ctsio->be_move_done = ctl_config_move_done;
6290 ctl_datamove((union ctl_io *)ctsio);
6292 return (CTL_RETVAL_COMPLETE);
6295 switch (ctsio->cdb[0]) {
6296 case MODE_SELECT_6: {
6297 struct scsi_mode_header_6 *mh6;
6299 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6300 bd_len = mh6->blk_desc_len;
6303 case MODE_SELECT_10: {
6304 struct scsi_mode_header_10 *mh10;
6306 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6307 bd_len = scsi_2btoul(mh10->blk_desc_len);
6311 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6314 if (param_len < (header_size + bd_len)) {
6315 free(ctsio->kern_data_ptr, M_CTL);
6316 ctl_set_param_len_error(ctsio);
6317 ctl_done((union ctl_io *)ctsio);
6318 return (CTL_RETVAL_COMPLETE);
6322 * Set the IO_CONT flag, so that if this I/O gets passed to
6323 * ctl_config_write_done(), it'll get passed back to
6324 * ctl_do_mode_select() for further processing, or completion if
6327 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6328 ctsio->io_cont = ctl_do_mode_select;
6330 modepage_info = (union ctl_modepage_info *)
6331 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6332 memset(modepage_info, 0, sizeof(*modepage_info));
6333 modepage_info->header.len_left = param_len - header_size - bd_len;
6334 modepage_info->header.len_used = header_size + bd_len;
6336 return (ctl_do_mode_select((union ctl_io *)ctsio));
6340 ctl_mode_sense(struct ctl_scsiio *ctsio)
6342 struct ctl_lun *lun = CTL_LUN(ctsio);
6343 int pc, page_code, dbd, llba, subpage;
6344 int alloc_len, page_len, header_len, total_len;
6345 struct scsi_mode_block_descr *block_desc;
6346 struct ctl_page_index *page_index;
6352 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6354 switch (ctsio->cdb[0]) {
6355 case MODE_SENSE_6: {
6356 struct scsi_mode_sense_6 *cdb;
6358 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6360 header_len = sizeof(struct scsi_mode_hdr_6);
6361 if (cdb->byte2 & SMS_DBD)
6364 header_len += sizeof(struct scsi_mode_block_descr);
6366 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6367 page_code = cdb->page & SMS_PAGE_CODE;
6368 subpage = cdb->subpage;
6369 alloc_len = cdb->length;
6372 case MODE_SENSE_10: {
6373 struct scsi_mode_sense_10 *cdb;
6375 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6377 header_len = sizeof(struct scsi_mode_hdr_10);
6379 if (cdb->byte2 & SMS_DBD)
6382 header_len += sizeof(struct scsi_mode_block_descr);
6383 if (cdb->byte2 & SMS10_LLBAA)
6385 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6386 page_code = cdb->page & SMS_PAGE_CODE;
6387 subpage = cdb->subpage;
6388 alloc_len = scsi_2btoul(cdb->length);
6392 ctl_set_invalid_opcode(ctsio);
6393 ctl_done((union ctl_io *)ctsio);
6394 return (CTL_RETVAL_COMPLETE);
6395 break; /* NOTREACHED */
6399 * We have to make a first pass through to calculate the size of
6400 * the pages that match the user's query. Then we allocate enough
6401 * memory to hold it, and actually copy the data into the buffer.
6403 switch (page_code) {
6404 case SMS_ALL_PAGES_PAGE: {
6410 * At the moment, values other than 0 and 0xff here are
6411 * reserved according to SPC-3.
6413 if ((subpage != SMS_SUBPAGE_PAGE_0)
6414 && (subpage != SMS_SUBPAGE_ALL)) {
6415 ctl_set_invalid_field(ctsio,
6421 ctl_done((union ctl_io *)ctsio);
6422 return (CTL_RETVAL_COMPLETE);
6425 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6426 page_index = &lun->mode_pages.index[i];
6428 /* Make sure the page is supported for this dev type */
6429 if (lun->be_lun->lun_type == T_DIRECT &&
6430 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6432 if (lun->be_lun->lun_type == T_PROCESSOR &&
6433 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6435 if (lun->be_lun->lun_type == T_CDROM &&
6436 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6440 * We don't use this subpage if the user didn't
6441 * request all subpages.
6443 if ((page_index->subpage != 0)
6444 && (subpage == SMS_SUBPAGE_PAGE_0))
6448 printf("found page %#x len %d\n",
6449 page_index->page_code & SMPH_PC_MASK,
6450 page_index->page_len);
6452 page_len += page_index->page_len;
6461 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6462 page_index = &lun->mode_pages.index[i];
6464 /* Make sure the page is supported for this dev type */
6465 if (lun->be_lun->lun_type == T_DIRECT &&
6466 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6468 if (lun->be_lun->lun_type == T_PROCESSOR &&
6469 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6471 if (lun->be_lun->lun_type == T_CDROM &&
6472 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6475 /* Look for the right page code */
6476 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6479 /* Look for the right subpage or the subpage wildcard*/
6480 if ((page_index->subpage != subpage)
6481 && (subpage != SMS_SUBPAGE_ALL))
6485 printf("found page %#x len %d\n",
6486 page_index->page_code & SMPH_PC_MASK,
6487 page_index->page_len);
6490 page_len += page_index->page_len;
6493 if (page_len == 0) {
6494 ctl_set_invalid_field(ctsio,
6500 ctl_done((union ctl_io *)ctsio);
6501 return (CTL_RETVAL_COMPLETE);
6507 total_len = header_len + page_len;
6509 printf("header_len = %d, page_len = %d, total_len = %d\n",
6510 header_len, page_len, total_len);
6513 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6514 ctsio->kern_sg_entries = 0;
6515 ctsio->kern_rel_offset = 0;
6516 ctsio->kern_data_len = min(total_len, alloc_len);
6517 ctsio->kern_total_len = ctsio->kern_data_len;
6519 switch (ctsio->cdb[0]) {
6520 case MODE_SENSE_6: {
6521 struct scsi_mode_hdr_6 *header;
6523 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6525 header->datalen = MIN(total_len - 1, 254);
6526 if (lun->be_lun->lun_type == T_DIRECT) {
6527 header->dev_specific = 0x10; /* DPOFUA */
6528 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6529 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6530 header->dev_specific |= 0x80; /* WP */
6533 header->block_descr_len = 0;
6535 header->block_descr_len =
6536 sizeof(struct scsi_mode_block_descr);
6537 block_desc = (struct scsi_mode_block_descr *)&header[1];
6540 case MODE_SENSE_10: {
6541 struct scsi_mode_hdr_10 *header;
6544 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6546 datalen = MIN(total_len - 2, 65533);
6547 scsi_ulto2b(datalen, header->datalen);
6548 if (lun->be_lun->lun_type == T_DIRECT) {
6549 header->dev_specific = 0x10; /* DPOFUA */
6550 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6551 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6552 header->dev_specific |= 0x80; /* WP */
6555 scsi_ulto2b(0, header->block_descr_len);
6557 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6558 header->block_descr_len);
6559 block_desc = (struct scsi_mode_block_descr *)&header[1];
6563 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6567 * If we've got a disk, use its blocksize in the block
6568 * descriptor. Otherwise, just set it to 0.
6571 if (lun->be_lun->lun_type == T_DIRECT)
6572 scsi_ulto3b(lun->be_lun->blocksize,
6573 block_desc->block_len);
6575 scsi_ulto3b(0, block_desc->block_len);
6578 switch (page_code) {
6579 case SMS_ALL_PAGES_PAGE: {
6582 data_used = header_len;
6583 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6584 struct ctl_page_index *page_index;
6586 page_index = &lun->mode_pages.index[i];
6587 if (lun->be_lun->lun_type == T_DIRECT &&
6588 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6590 if (lun->be_lun->lun_type == T_PROCESSOR &&
6591 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6593 if (lun->be_lun->lun_type == T_CDROM &&
6594 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6598 * We don't use this subpage if the user didn't
6599 * request all subpages. We already checked (above)
6600 * to make sure the user only specified a subpage
6601 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6603 if ((page_index->subpage != 0)
6604 && (subpage == SMS_SUBPAGE_PAGE_0))
6608 * Call the handler, if it exists, to update the
6609 * page to the latest values.
6611 if (page_index->sense_handler != NULL)
6612 page_index->sense_handler(ctsio, page_index,pc);
6614 memcpy(ctsio->kern_data_ptr + data_used,
6615 page_index->page_data +
6616 (page_index->page_len * pc),
6617 page_index->page_len);
6618 data_used += page_index->page_len;
6625 data_used = header_len;
6627 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6628 struct ctl_page_index *page_index;
6630 page_index = &lun->mode_pages.index[i];
6632 /* Look for the right page code */
6633 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6636 /* Look for the right subpage or the subpage wildcard*/
6637 if ((page_index->subpage != subpage)
6638 && (subpage != SMS_SUBPAGE_ALL))
6641 /* Make sure the page is supported for this dev type */
6642 if (lun->be_lun->lun_type == T_DIRECT &&
6643 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6645 if (lun->be_lun->lun_type == T_PROCESSOR &&
6646 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6648 if (lun->be_lun->lun_type == T_CDROM &&
6649 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6653 * Call the handler, if it exists, to update the
6654 * page to the latest values.
6656 if (page_index->sense_handler != NULL)
6657 page_index->sense_handler(ctsio, page_index,pc);
6659 memcpy(ctsio->kern_data_ptr + data_used,
6660 page_index->page_data +
6661 (page_index->page_len * pc),
6662 page_index->page_len);
6663 data_used += page_index->page_len;
6669 ctl_set_success(ctsio);
6670 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6671 ctsio->be_move_done = ctl_config_move_done;
6672 ctl_datamove((union ctl_io *)ctsio);
6673 return (CTL_RETVAL_COMPLETE);
6677 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6678 struct ctl_page_index *page_index,
6681 struct ctl_lun *lun = CTL_LUN(ctsio);
6682 struct scsi_log_param_header *phdr;
6686 data = page_index->page_data;
6688 if (lun->backend->lun_attr != NULL &&
6689 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6691 phdr = (struct scsi_log_param_header *)data;
6692 scsi_ulto2b(0x0001, phdr->param_code);
6693 phdr->param_control = SLP_LBIN | SLP_LP;
6694 phdr->param_len = 8;
6695 data = (uint8_t *)(phdr + 1);
6696 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6697 data[4] = 0x02; /* per-pool */
6698 data += phdr->param_len;
6701 if (lun->backend->lun_attr != NULL &&
6702 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6704 phdr = (struct scsi_log_param_header *)data;
6705 scsi_ulto2b(0x0002, phdr->param_code);
6706 phdr->param_control = SLP_LBIN | SLP_LP;
6707 phdr->param_len = 8;
6708 data = (uint8_t *)(phdr + 1);
6709 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6710 data[4] = 0x01; /* per-LUN */
6711 data += phdr->param_len;
6714 if (lun->backend->lun_attr != NULL &&
6715 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6717 phdr = (struct scsi_log_param_header *)data;
6718 scsi_ulto2b(0x00f1, phdr->param_code);
6719 phdr->param_control = SLP_LBIN | SLP_LP;
6720 phdr->param_len = 8;
6721 data = (uint8_t *)(phdr + 1);
6722 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6723 data[4] = 0x02; /* per-pool */
6724 data += phdr->param_len;
6727 if (lun->backend->lun_attr != NULL &&
6728 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6730 phdr = (struct scsi_log_param_header *)data;
6731 scsi_ulto2b(0x00f2, phdr->param_code);
6732 phdr->param_control = SLP_LBIN | SLP_LP;
6733 phdr->param_len = 8;
6734 data = (uint8_t *)(phdr + 1);
6735 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6736 data[4] = 0x02; /* per-pool */
6737 data += phdr->param_len;
6740 page_index->page_len = data - page_index->page_data;
6745 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
6746 struct ctl_page_index *page_index,
6749 struct ctl_lun *lun = CTL_LUN(ctsio);
6750 struct stat_page *data;
6753 data = (struct stat_page *)page_index->page_data;
6755 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
6756 data->sap.hdr.param_control = SLP_LBIN;
6757 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
6758 sizeof(struct scsi_log_param_header);
6759 scsi_u64to8b(lun->stats.operations[CTL_STATS_READ],
6760 data->sap.read_num);
6761 scsi_u64to8b(lun->stats.operations[CTL_STATS_WRITE],
6762 data->sap.write_num);
6763 if (lun->be_lun->blocksize > 0) {
6764 scsi_u64to8b(lun->stats.bytes[CTL_STATS_WRITE] /
6765 lun->be_lun->blocksize, data->sap.recvieved_lba);
6766 scsi_u64to8b(lun->stats.bytes[CTL_STATS_READ] /
6767 lun->be_lun->blocksize, data->sap.transmitted_lba);
6769 t = &lun->stats.time[CTL_STATS_READ];
6770 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000),
6771 data->sap.read_int);
6772 t = &lun->stats.time[CTL_STATS_WRITE];
6773 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000),
6774 data->sap.write_int);
6775 scsi_u64to8b(0, data->sap.weighted_num);
6776 scsi_u64to8b(0, data->sap.weighted_int);
6777 scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
6778 data->it.hdr.param_control = SLP_LBIN;
6779 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
6780 sizeof(struct scsi_log_param_header);
6782 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
6784 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
6785 data->it.hdr.param_control = SLP_LBIN;
6786 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
6787 sizeof(struct scsi_log_param_header);
6788 scsi_ulto4b(3, data->ti.exponent);
6789 scsi_ulto4b(1, data->ti.integer);
6794 ctl_ie_log_sense_handler(struct ctl_scsiio *ctsio,
6795 struct ctl_page_index *page_index,
6798 struct ctl_lun *lun = CTL_LUN(ctsio);
6799 struct scsi_log_informational_exceptions *data;
6801 data = (struct scsi_log_informational_exceptions *)page_index->page_data;
6803 scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code);
6804 data->hdr.param_control = SLP_LBIN;
6805 data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) -
6806 sizeof(struct scsi_log_param_header);
6807 data->ie_asc = lun->ie_asc;
6808 data->ie_ascq = lun->ie_ascq;
6809 data->temperature = 0xff;
6814 ctl_log_sense(struct ctl_scsiio *ctsio)
6816 struct ctl_lun *lun = CTL_LUN(ctsio);
6817 int i, pc, page_code, subpage;
6818 int alloc_len, total_len;
6819 struct ctl_page_index *page_index;
6820 struct scsi_log_sense *cdb;
6821 struct scsi_log_header *header;
6823 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6825 cdb = (struct scsi_log_sense *)ctsio->cdb;
6826 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6827 page_code = cdb->page & SLS_PAGE_CODE;
6828 subpage = cdb->subpage;
6829 alloc_len = scsi_2btoul(cdb->length);
6832 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6833 page_index = &lun->log_pages.index[i];
6835 /* Look for the right page code */
6836 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6839 /* Look for the right subpage or the subpage wildcard*/
6840 if (page_index->subpage != subpage)
6845 if (i >= CTL_NUM_LOG_PAGES) {
6846 ctl_set_invalid_field(ctsio,
6852 ctl_done((union ctl_io *)ctsio);
6853 return (CTL_RETVAL_COMPLETE);
6856 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6858 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6859 ctsio->kern_sg_entries = 0;
6860 ctsio->kern_rel_offset = 0;
6861 ctsio->kern_data_len = min(total_len, alloc_len);
6862 ctsio->kern_total_len = ctsio->kern_data_len;
6864 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
6865 header->page = page_index->page_code;
6866 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING)
6867 header->page |= SL_DS;
6868 if (page_index->subpage) {
6869 header->page |= SL_SPF;
6870 header->subpage = page_index->subpage;
6872 scsi_ulto2b(page_index->page_len, header->datalen);
6875 * Call the handler, if it exists, to update the
6876 * page to the latest values.
6878 if (page_index->sense_handler != NULL)
6879 page_index->sense_handler(ctsio, page_index, pc);
6881 memcpy(header + 1, page_index->page_data, page_index->page_len);
6883 ctl_set_success(ctsio);
6884 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6885 ctsio->be_move_done = ctl_config_move_done;
6886 ctl_datamove((union ctl_io *)ctsio);
6887 return (CTL_RETVAL_COMPLETE);
6891 ctl_read_capacity(struct ctl_scsiio *ctsio)
6893 struct ctl_lun *lun = CTL_LUN(ctsio);
6894 struct scsi_read_capacity *cdb;
6895 struct scsi_read_capacity_data *data;
6898 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6900 cdb = (struct scsi_read_capacity *)ctsio->cdb;
6902 lba = scsi_4btoul(cdb->addr);
6903 if (((cdb->pmi & SRC_PMI) == 0)
6905 ctl_set_invalid_field(/*ctsio*/ ctsio,
6911 ctl_done((union ctl_io *)ctsio);
6912 return (CTL_RETVAL_COMPLETE);
6915 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6916 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6917 ctsio->kern_data_len = sizeof(*data);
6918 ctsio->kern_total_len = sizeof(*data);
6919 ctsio->kern_rel_offset = 0;
6920 ctsio->kern_sg_entries = 0;
6923 * If the maximum LBA is greater than 0xfffffffe, the user must
6924 * issue a SERVICE ACTION IN (16) command, with the read capacity
6925 * serivce action set.
6927 if (lun->be_lun->maxlba > 0xfffffffe)
6928 scsi_ulto4b(0xffffffff, data->addr);
6930 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6933 * XXX KDM this may not be 512 bytes...
6935 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6937 ctl_set_success(ctsio);
6938 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6939 ctsio->be_move_done = ctl_config_move_done;
6940 ctl_datamove((union ctl_io *)ctsio);
6941 return (CTL_RETVAL_COMPLETE);
6945 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
6947 struct ctl_lun *lun = CTL_LUN(ctsio);
6948 struct scsi_read_capacity_16 *cdb;
6949 struct scsi_read_capacity_data_long *data;
6953 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
6955 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
6957 alloc_len = scsi_4btoul(cdb->alloc_len);
6958 lba = scsi_8btou64(cdb->addr);
6960 if ((cdb->reladr & SRC16_PMI)
6962 ctl_set_invalid_field(/*ctsio*/ ctsio,
6968 ctl_done((union ctl_io *)ctsio);
6969 return (CTL_RETVAL_COMPLETE);
6972 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6973 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
6974 ctsio->kern_rel_offset = 0;
6975 ctsio->kern_sg_entries = 0;
6976 ctsio->kern_data_len = min(sizeof(*data), alloc_len);
6977 ctsio->kern_total_len = ctsio->kern_data_len;
6979 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
6980 /* XXX KDM this may not be 512 bytes... */
6981 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6982 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
6983 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
6984 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
6985 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
6987 ctl_set_success(ctsio);
6988 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6989 ctsio->be_move_done = ctl_config_move_done;
6990 ctl_datamove((union ctl_io *)ctsio);
6991 return (CTL_RETVAL_COMPLETE);
6995 ctl_get_lba_status(struct ctl_scsiio *ctsio)
6997 struct ctl_lun *lun = CTL_LUN(ctsio);
6998 struct scsi_get_lba_status *cdb;
6999 struct scsi_get_lba_status_data *data;
7000 struct ctl_lba_len_flags *lbalen;
7002 uint32_t alloc_len, total_len;
7005 CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
7007 cdb = (struct scsi_get_lba_status *)ctsio->cdb;
7008 lba = scsi_8btou64(cdb->addr);
7009 alloc_len = scsi_4btoul(cdb->alloc_len);
7011 if (lba > lun->be_lun->maxlba) {
7012 ctl_set_lba_out_of_range(ctsio, lba);
7013 ctl_done((union ctl_io *)ctsio);
7014 return (CTL_RETVAL_COMPLETE);
7017 total_len = sizeof(*data) + sizeof(data->descr[0]);
7018 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7019 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
7020 ctsio->kern_rel_offset = 0;
7021 ctsio->kern_sg_entries = 0;
7022 ctsio->kern_data_len = min(total_len, alloc_len);
7023 ctsio->kern_total_len = ctsio->kern_data_len;
7025 /* Fill dummy data in case backend can't tell anything. */
7026 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
7027 scsi_u64to8b(lba, data->descr[0].addr);
7028 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
7029 data->descr[0].length);
7030 data->descr[0].status = 0; /* Mapped or unknown. */
7032 ctl_set_success(ctsio);
7033 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7034 ctsio->be_move_done = ctl_config_move_done;
7036 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
7038 lbalen->len = total_len;
7040 retval = lun->backend->config_read((union ctl_io *)ctsio);
7045 ctl_read_defect(struct ctl_scsiio *ctsio)
7047 struct scsi_read_defect_data_10 *ccb10;
7048 struct scsi_read_defect_data_12 *ccb12;
7049 struct scsi_read_defect_data_hdr_10 *data10;
7050 struct scsi_read_defect_data_hdr_12 *data12;
7051 uint32_t alloc_len, data_len;
7054 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7056 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7057 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7058 format = ccb10->format;
7059 alloc_len = scsi_2btoul(ccb10->alloc_length);
7060 data_len = sizeof(*data10);
7062 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7063 format = ccb12->format;
7064 alloc_len = scsi_4btoul(ccb12->alloc_length);
7065 data_len = sizeof(*data12);
7067 if (alloc_len == 0) {
7068 ctl_set_success(ctsio);
7069 ctl_done((union ctl_io *)ctsio);
7070 return (CTL_RETVAL_COMPLETE);
7073 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7074 ctsio->kern_rel_offset = 0;
7075 ctsio->kern_sg_entries = 0;
7076 ctsio->kern_data_len = min(data_len, alloc_len);
7077 ctsio->kern_total_len = ctsio->kern_data_len;
7079 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7080 data10 = (struct scsi_read_defect_data_hdr_10 *)
7081 ctsio->kern_data_ptr;
7082 data10->format = format;
7083 scsi_ulto2b(0, data10->length);
7085 data12 = (struct scsi_read_defect_data_hdr_12 *)
7086 ctsio->kern_data_ptr;
7087 data12->format = format;
7088 scsi_ulto2b(0, data12->generation);
7089 scsi_ulto4b(0, data12->length);
7092 ctl_set_success(ctsio);
7093 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7094 ctsio->be_move_done = ctl_config_move_done;
7095 ctl_datamove((union ctl_io *)ctsio);
7096 return (CTL_RETVAL_COMPLETE);
7100 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7102 struct ctl_softc *softc = CTL_SOFTC(ctsio);
7103 struct ctl_lun *lun = CTL_LUN(ctsio);
7104 struct scsi_maintenance_in *cdb;
7106 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os;
7107 int num_ha_groups, num_target_ports, shared_group;
7108 struct ctl_port *port;
7109 struct scsi_target_group_data *rtg_ptr;
7110 struct scsi_target_group_data_extended *rtg_ext_ptr;
7111 struct scsi_target_port_group_descriptor *tpg_desc;
7113 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7115 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7116 retval = CTL_RETVAL_COMPLETE;
7118 switch (cdb->byte2 & STG_PDF_MASK) {
7119 case STG_PDF_LENGTH:
7122 case STG_PDF_EXTENDED:
7126 ctl_set_invalid_field(/*ctsio*/ ctsio,
7132 ctl_done((union ctl_io *)ctsio);
7136 num_target_ports = 0;
7137 shared_group = (softc->is_single != 0);
7138 mtx_lock(&softc->ctl_lock);
7139 STAILQ_FOREACH(port, &softc->port_list, links) {
7140 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7142 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7145 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7148 mtx_unlock(&softc->ctl_lock);
7149 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES;
7152 total_len = sizeof(struct scsi_target_group_data_extended);
7154 total_len = sizeof(struct scsi_target_group_data);
7155 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7156 (shared_group + num_ha_groups) +
7157 sizeof(struct scsi_target_port_descriptor) * num_target_ports;
7159 alloc_len = scsi_4btoul(cdb->length);
7161 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7162 ctsio->kern_sg_entries = 0;
7163 ctsio->kern_rel_offset = 0;
7164 ctsio->kern_data_len = min(total_len, alloc_len);
7165 ctsio->kern_total_len = ctsio->kern_data_len;
7168 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7169 ctsio->kern_data_ptr;
7170 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7171 rtg_ext_ptr->format_type = 0x10;
7172 rtg_ext_ptr->implicit_transition_time = 0;
7173 tpg_desc = &rtg_ext_ptr->groups[0];
7175 rtg_ptr = (struct scsi_target_group_data *)
7176 ctsio->kern_data_ptr;
7177 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7178 tpg_desc = &rtg_ptr->groups[0];
7181 mtx_lock(&softc->ctl_lock);
7182 pg = softc->port_min / softc->port_cnt;
7183 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) {
7184 /* Some shelf is known to be primary. */
7185 if (softc->ha_link == CTL_HA_LINK_OFFLINE)
7186 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7187 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN)
7188 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7189 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY)
7190 os = TPG_ASYMMETRIC_ACCESS_STANDBY;
7192 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7193 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7194 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7197 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7200 /* No known primary shelf. */
7201 if (softc->ha_link == CTL_HA_LINK_OFFLINE) {
7202 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7203 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7204 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) {
7205 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7206 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7208 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7212 tpg_desc->pref_state = ts;
7213 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7214 TPG_U_SUP | TPG_T_SUP;
7215 scsi_ulto2b(1, tpg_desc->target_port_group);
7216 tpg_desc->status = TPG_IMPLICIT;
7218 STAILQ_FOREACH(port, &softc->port_list, links) {
7219 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7221 if (!softc->is_single &&
7222 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0)
7224 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7226 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7227 relative_target_port_identifier);
7230 tpg_desc->target_port_count = pc;
7231 tpg_desc = (struct scsi_target_port_group_descriptor *)
7232 &tpg_desc->descriptors[pc];
7234 for (g = 0; g < num_ha_groups; g++) {
7235 tpg_desc->pref_state = (g == pg) ? ts : os;
7236 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7237 TPG_U_SUP | TPG_T_SUP;
7238 scsi_ulto2b(2 + g, tpg_desc->target_port_group);
7239 tpg_desc->status = TPG_IMPLICIT;
7241 STAILQ_FOREACH(port, &softc->port_list, links) {
7242 if (port->targ_port < g * softc->port_cnt ||
7243 port->targ_port >= (g + 1) * softc->port_cnt)
7245 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7247 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7249 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7251 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7252 relative_target_port_identifier);
7255 tpg_desc->target_port_count = pc;
7256 tpg_desc = (struct scsi_target_port_group_descriptor *)
7257 &tpg_desc->descriptors[pc];
7259 mtx_unlock(&softc->ctl_lock);
7261 ctl_set_success(ctsio);
7262 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7263 ctsio->be_move_done = ctl_config_move_done;
7264 ctl_datamove((union ctl_io *)ctsio);
7269 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7271 struct ctl_lun *lun = CTL_LUN(ctsio);
7272 struct scsi_report_supported_opcodes *cdb;
7273 const struct ctl_cmd_entry *entry, *sentry;
7274 struct scsi_report_supported_opcodes_all *all;
7275 struct scsi_report_supported_opcodes_descr *descr;
7276 struct scsi_report_supported_opcodes_one *one;
7278 int alloc_len, total_len;
7279 int opcode, service_action, i, j, num;
7281 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7283 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7284 retval = CTL_RETVAL_COMPLETE;
7286 opcode = cdb->requested_opcode;
7287 service_action = scsi_2btoul(cdb->requested_service_action);
7288 switch (cdb->options & RSO_OPTIONS_MASK) {
7289 case RSO_OPTIONS_ALL:
7291 for (i = 0; i < 256; i++) {
7292 entry = &ctl_cmd_table[i];
7293 if (entry->flags & CTL_CMD_FLAG_SA5) {
7294 for (j = 0; j < 32; j++) {
7295 sentry = &((const struct ctl_cmd_entry *)
7297 if (ctl_cmd_applicable(
7298 lun->be_lun->lun_type, sentry))
7302 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7307 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7308 num * sizeof(struct scsi_report_supported_opcodes_descr);
7310 case RSO_OPTIONS_OC:
7311 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7312 ctl_set_invalid_field(/*ctsio*/ ctsio,
7318 ctl_done((union ctl_io *)ctsio);
7319 return (CTL_RETVAL_COMPLETE);
7321 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7323 case RSO_OPTIONS_OC_SA:
7324 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7325 service_action >= 32) {
7326 ctl_set_invalid_field(/*ctsio*/ ctsio,
7332 ctl_done((union ctl_io *)ctsio);
7333 return (CTL_RETVAL_COMPLETE);
7336 case RSO_OPTIONS_OC_ASA:
7337 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7340 ctl_set_invalid_field(/*ctsio*/ ctsio,
7346 ctl_done((union ctl_io *)ctsio);
7347 return (CTL_RETVAL_COMPLETE);
7350 alloc_len = scsi_4btoul(cdb->length);
7352 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7353 ctsio->kern_sg_entries = 0;
7354 ctsio->kern_rel_offset = 0;
7355 ctsio->kern_data_len = min(total_len, alloc_len);
7356 ctsio->kern_total_len = ctsio->kern_data_len;
7358 switch (cdb->options & RSO_OPTIONS_MASK) {
7359 case RSO_OPTIONS_ALL:
7360 all = (struct scsi_report_supported_opcodes_all *)
7361 ctsio->kern_data_ptr;
7363 for (i = 0; i < 256; i++) {
7364 entry = &ctl_cmd_table[i];
7365 if (entry->flags & CTL_CMD_FLAG_SA5) {
7366 for (j = 0; j < 32; j++) {
7367 sentry = &((const struct ctl_cmd_entry *)
7369 if (!ctl_cmd_applicable(
7370 lun->be_lun->lun_type, sentry))
7372 descr = &all->descr[num++];
7374 scsi_ulto2b(j, descr->service_action);
7375 descr->flags = RSO_SERVACTV;
7376 scsi_ulto2b(sentry->length,
7380 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7383 descr = &all->descr[num++];
7385 scsi_ulto2b(0, descr->service_action);
7387 scsi_ulto2b(entry->length, descr->cdb_length);
7391 num * sizeof(struct scsi_report_supported_opcodes_descr),
7394 case RSO_OPTIONS_OC:
7395 one = (struct scsi_report_supported_opcodes_one *)
7396 ctsio->kern_data_ptr;
7397 entry = &ctl_cmd_table[opcode];
7399 case RSO_OPTIONS_OC_SA:
7400 one = (struct scsi_report_supported_opcodes_one *)
7401 ctsio->kern_data_ptr;
7402 entry = &ctl_cmd_table[opcode];
7403 entry = &((const struct ctl_cmd_entry *)
7404 entry->execute)[service_action];
7406 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7408 scsi_ulto2b(entry->length, one->cdb_length);
7409 one->cdb_usage[0] = opcode;
7410 memcpy(&one->cdb_usage[1], entry->usage,
7415 case RSO_OPTIONS_OC_ASA:
7416 one = (struct scsi_report_supported_opcodes_one *)
7417 ctsio->kern_data_ptr;
7418 entry = &ctl_cmd_table[opcode];
7419 if (entry->flags & CTL_CMD_FLAG_SA5) {
7420 entry = &((const struct ctl_cmd_entry *)
7421 entry->execute)[service_action];
7422 } else if (service_action != 0) {
7429 ctl_set_success(ctsio);
7430 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7431 ctsio->be_move_done = ctl_config_move_done;
7432 ctl_datamove((union ctl_io *)ctsio);
7437 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7439 struct scsi_report_supported_tmf *cdb;
7440 struct scsi_report_supported_tmf_ext_data *data;
7442 int alloc_len, total_len;
7444 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7446 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7448 retval = CTL_RETVAL_COMPLETE;
7450 if (cdb->options & RST_REPD)
7451 total_len = sizeof(struct scsi_report_supported_tmf_ext_data);
7453 total_len = sizeof(struct scsi_report_supported_tmf_data);
7454 alloc_len = scsi_4btoul(cdb->length);
7456 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7457 ctsio->kern_sg_entries = 0;
7458 ctsio->kern_rel_offset = 0;
7459 ctsio->kern_data_len = min(total_len, alloc_len);
7460 ctsio->kern_total_len = ctsio->kern_data_len;
7462 data = (struct scsi_report_supported_tmf_ext_data *)ctsio->kern_data_ptr;
7463 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS |
7465 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS;
7466 data->length = total_len - 4;
7468 ctl_set_success(ctsio);
7469 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7470 ctsio->be_move_done = ctl_config_move_done;
7471 ctl_datamove((union ctl_io *)ctsio);
7476 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7478 struct scsi_report_timestamp *cdb;
7479 struct scsi_report_timestamp_data *data;
7483 int alloc_len, total_len;
7485 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7487 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7489 retval = CTL_RETVAL_COMPLETE;
7491 total_len = sizeof(struct scsi_report_timestamp_data);
7492 alloc_len = scsi_4btoul(cdb->length);
7494 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7495 ctsio->kern_sg_entries = 0;
7496 ctsio->kern_rel_offset = 0;
7497 ctsio->kern_data_len = min(total_len, alloc_len);
7498 ctsio->kern_total_len = ctsio->kern_data_len;
7500 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7501 scsi_ulto2b(sizeof(*data) - 2, data->length);
7502 data->origin = RTS_ORIG_OUTSIDE;
7504 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7505 scsi_ulto4b(timestamp >> 16, data->timestamp);
7506 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7508 ctl_set_success(ctsio);
7509 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7510 ctsio->be_move_done = ctl_config_move_done;
7511 ctl_datamove((union ctl_io *)ctsio);
7516 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7518 struct ctl_softc *softc = CTL_SOFTC(ctsio);
7519 struct ctl_lun *lun = CTL_LUN(ctsio);
7520 struct scsi_per_res_in *cdb;
7521 int alloc_len, total_len = 0;
7522 /* struct scsi_per_res_in_rsrv in_data; */
7525 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7527 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7529 alloc_len = scsi_2btoul(cdb->length);
7532 mtx_lock(&lun->lun_lock);
7533 switch (cdb->action) {
7534 case SPRI_RK: /* read keys */
7535 total_len = sizeof(struct scsi_per_res_in_keys) +
7537 sizeof(struct scsi_per_res_key);
7539 case SPRI_RR: /* read reservation */
7540 if (lun->flags & CTL_LUN_PR_RESERVED)
7541 total_len = sizeof(struct scsi_per_res_in_rsrv);
7543 total_len = sizeof(struct scsi_per_res_in_header);
7545 case SPRI_RC: /* report capabilities */
7546 total_len = sizeof(struct scsi_per_res_cap);
7548 case SPRI_RS: /* read full status */
7549 total_len = sizeof(struct scsi_per_res_in_header) +
7550 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7554 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7556 mtx_unlock(&lun->lun_lock);
7558 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7559 ctsio->kern_rel_offset = 0;
7560 ctsio->kern_sg_entries = 0;
7561 ctsio->kern_data_len = min(total_len, alloc_len);
7562 ctsio->kern_total_len = ctsio->kern_data_len;
7564 mtx_lock(&lun->lun_lock);
7565 switch (cdb->action) {
7566 case SPRI_RK: { // read keys
7567 struct scsi_per_res_in_keys *res_keys;
7570 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7573 * We had to drop the lock to allocate our buffer, which
7574 * leaves time for someone to come in with another
7575 * persistent reservation. (That is unlikely, though,
7576 * since this should be the only persistent reservation
7577 * command active right now.)
7579 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7580 (lun->pr_key_count *
7581 sizeof(struct scsi_per_res_key)))){
7582 mtx_unlock(&lun->lun_lock);
7583 free(ctsio->kern_data_ptr, M_CTL);
7584 printf("%s: reservation length changed, retrying\n",
7589 scsi_ulto4b(lun->pr_generation, res_keys->header.generation);
7591 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7592 lun->pr_key_count, res_keys->header.length);
7594 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) {
7595 if ((key = ctl_get_prkey(lun, i)) == 0)
7599 * We used lun->pr_key_count to calculate the
7600 * size to allocate. If it turns out the number of
7601 * initiators with the registered flag set is
7602 * larger than that (i.e. they haven't been kept in
7603 * sync), we've got a problem.
7605 if (key_count >= lun->pr_key_count) {
7609 scsi_u64to8b(key, res_keys->keys[key_count].key);
7614 case SPRI_RR: { // read reservation
7615 struct scsi_per_res_in_rsrv *res;
7616 int tmp_len, header_only;
7618 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7620 scsi_ulto4b(lun->pr_generation, res->header.generation);
7622 if (lun->flags & CTL_LUN_PR_RESERVED)
7624 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7625 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7626 res->header.length);
7629 tmp_len = sizeof(struct scsi_per_res_in_header);
7630 scsi_ulto4b(0, res->header.length);
7635 * We had to drop the lock to allocate our buffer, which
7636 * leaves time for someone to come in with another
7637 * persistent reservation. (That is unlikely, though,
7638 * since this should be the only persistent reservation
7639 * command active right now.)
7641 if (tmp_len != total_len) {
7642 mtx_unlock(&lun->lun_lock);
7643 free(ctsio->kern_data_ptr, M_CTL);
7644 printf("%s: reservation status changed, retrying\n",
7650 * No reservation held, so we're done.
7652 if (header_only != 0)
7656 * If the registration is an All Registrants type, the key
7657 * is 0, since it doesn't really matter.
7659 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7660 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7661 res->data.reservation);
7663 res->data.scopetype = lun->pr_res_type;
7666 case SPRI_RC: //report capabilities
7668 struct scsi_per_res_cap *res_cap;
7671 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7672 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7673 res_cap->flags1 = SPRI_CRH;
7674 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5;
7675 type_mask = SPRI_TM_WR_EX_AR |
7681 scsi_ulto2b(type_mask, res_cap->type_mask);
7684 case SPRI_RS: { // read full status
7685 struct scsi_per_res_in_full *res_status;
7686 struct scsi_per_res_in_full_desc *res_desc;
7687 struct ctl_port *port;
7690 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7693 * We had to drop the lock to allocate our buffer, which
7694 * leaves time for someone to come in with another
7695 * persistent reservation. (That is unlikely, though,
7696 * since this should be the only persistent reservation
7697 * command active right now.)
7699 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7700 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7701 lun->pr_key_count)){
7702 mtx_unlock(&lun->lun_lock);
7703 free(ctsio->kern_data_ptr, M_CTL);
7704 printf("%s: reservation length changed, retrying\n",
7709 scsi_ulto4b(lun->pr_generation, res_status->header.generation);
7711 res_desc = &res_status->desc[0];
7712 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7713 if ((key = ctl_get_prkey(lun, i)) == 0)
7716 scsi_u64to8b(key, res_desc->res_key.key);
7717 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7718 (lun->pr_res_idx == i ||
7719 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7720 res_desc->flags = SPRI_FULL_R_HOLDER;
7721 res_desc->scopetype = lun->pr_res_type;
7723 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7724 res_desc->rel_trgt_port_id);
7726 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7728 len = ctl_create_iid(port,
7729 i % CTL_MAX_INIT_PER_PORT,
7730 res_desc->transport_id);
7731 scsi_ulto4b(len, res_desc->additional_length);
7732 res_desc = (struct scsi_per_res_in_full_desc *)
7733 &res_desc->transport_id[len];
7735 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7736 res_status->header.length);
7740 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7742 mtx_unlock(&lun->lun_lock);
7744 ctl_set_success(ctsio);
7745 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7746 ctsio->be_move_done = ctl_config_move_done;
7747 ctl_datamove((union ctl_io *)ctsio);
7748 return (CTL_RETVAL_COMPLETE);
7752 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7756 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7757 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7758 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7759 struct scsi_per_res_out_parms* param)
7761 union ctl_ha_msg persis_io;
7764 mtx_lock(&lun->lun_lock);
7765 if (sa_res_key == 0) {
7766 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7767 /* validate scope and type */
7768 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7770 mtx_unlock(&lun->lun_lock);
7771 ctl_set_invalid_field(/*ctsio*/ ctsio,
7777 ctl_done((union ctl_io *)ctsio);
7781 if (type>8 || type==2 || type==4 || type==0) {
7782 mtx_unlock(&lun->lun_lock);
7783 ctl_set_invalid_field(/*ctsio*/ ctsio,
7789 ctl_done((union ctl_io *)ctsio);
7794 * Unregister everybody else and build UA for
7797 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7798 if (i == residx || ctl_get_prkey(lun, i) == 0)
7801 ctl_clr_prkey(lun, i);
7802 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7804 lun->pr_key_count = 1;
7805 lun->pr_res_type = type;
7806 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7807 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7808 lun->pr_res_idx = residx;
7809 lun->pr_generation++;
7810 mtx_unlock(&lun->lun_lock);
7812 /* send msg to other side */
7813 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7814 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7815 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7816 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7817 persis_io.pr.pr_info.res_type = type;
7818 memcpy(persis_io.pr.pr_info.sa_res_key,
7819 param->serv_act_res_key,
7820 sizeof(param->serv_act_res_key));
7821 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7822 sizeof(persis_io.pr), M_WAITOK);
7824 /* not all registrants */
7825 mtx_unlock(&lun->lun_lock);
7826 free(ctsio->kern_data_ptr, M_CTL);
7827 ctl_set_invalid_field(ctsio,
7833 ctl_done((union ctl_io *)ctsio);
7836 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7837 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7840 if (res_key == sa_res_key) {
7843 * The spec implies this is not good but doesn't
7844 * say what to do. There are two choices either
7845 * generate a res conflict or check condition
7846 * with illegal field in parameter data. Since
7847 * that is what is done when the sa_res_key is
7848 * zero I'll take that approach since this has
7849 * to do with the sa_res_key.
7851 mtx_unlock(&lun->lun_lock);
7852 free(ctsio->kern_data_ptr, M_CTL);
7853 ctl_set_invalid_field(ctsio,
7859 ctl_done((union ctl_io *)ctsio);
7863 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7864 if (ctl_get_prkey(lun, i) != sa_res_key)
7868 ctl_clr_prkey(lun, i);
7869 lun->pr_key_count--;
7870 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7873 mtx_unlock(&lun->lun_lock);
7874 free(ctsio->kern_data_ptr, M_CTL);
7875 ctl_set_reservation_conflict(ctsio);
7876 ctl_done((union ctl_io *)ctsio);
7877 return (CTL_RETVAL_COMPLETE);
7879 lun->pr_generation++;
7880 mtx_unlock(&lun->lun_lock);
7882 /* send msg to other side */
7883 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7884 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7885 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7886 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7887 persis_io.pr.pr_info.res_type = type;
7888 memcpy(persis_io.pr.pr_info.sa_res_key,
7889 param->serv_act_res_key,
7890 sizeof(param->serv_act_res_key));
7891 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7892 sizeof(persis_io.pr), M_WAITOK);
7894 /* Reserved but not all registrants */
7895 /* sa_res_key is res holder */
7896 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
7897 /* validate scope and type */
7898 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7900 mtx_unlock(&lun->lun_lock);
7901 ctl_set_invalid_field(/*ctsio*/ ctsio,
7907 ctl_done((union ctl_io *)ctsio);
7911 if (type>8 || type==2 || type==4 || type==0) {
7912 mtx_unlock(&lun->lun_lock);
7913 ctl_set_invalid_field(/*ctsio*/ ctsio,
7919 ctl_done((union ctl_io *)ctsio);
7925 * if sa_res_key != res_key remove all
7926 * registrants w/sa_res_key and generate UA
7927 * for these registrants(Registrations
7928 * Preempted) if it wasn't an exclusive
7929 * reservation generate UA(Reservations
7930 * Preempted) for all other registered nexuses
7931 * if the type has changed. Establish the new
7932 * reservation and holder. If res_key and
7933 * sa_res_key are the same do the above
7934 * except don't unregister the res holder.
7937 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7938 if (i == residx || ctl_get_prkey(lun, i) == 0)
7941 if (sa_res_key == ctl_get_prkey(lun, i)) {
7942 ctl_clr_prkey(lun, i);
7943 lun->pr_key_count--;
7944 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7945 } else if (type != lun->pr_res_type &&
7946 (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
7947 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
7948 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
7951 lun->pr_res_type = type;
7952 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7953 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7954 lun->pr_res_idx = residx;
7956 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
7957 lun->pr_generation++;
7958 mtx_unlock(&lun->lun_lock);
7960 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7961 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7962 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7963 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7964 persis_io.pr.pr_info.res_type = type;
7965 memcpy(persis_io.pr.pr_info.sa_res_key,
7966 param->serv_act_res_key,
7967 sizeof(param->serv_act_res_key));
7968 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7969 sizeof(persis_io.pr), M_WAITOK);
7972 * sa_res_key is not the res holder just
7973 * remove registrants
7977 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7978 if (sa_res_key != ctl_get_prkey(lun, i))
7982 ctl_clr_prkey(lun, i);
7983 lun->pr_key_count--;
7984 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7988 mtx_unlock(&lun->lun_lock);
7989 free(ctsio->kern_data_ptr, M_CTL);
7990 ctl_set_reservation_conflict(ctsio);
7991 ctl_done((union ctl_io *)ctsio);
7994 lun->pr_generation++;
7995 mtx_unlock(&lun->lun_lock);
7997 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7998 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7999 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8000 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8001 persis_io.pr.pr_info.res_type = type;
8002 memcpy(persis_io.pr.pr_info.sa_res_key,
8003 param->serv_act_res_key,
8004 sizeof(param->serv_act_res_key));
8005 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8006 sizeof(persis_io.pr), M_WAITOK);
8013 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8015 uint64_t sa_res_key;
8018 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8020 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8021 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8022 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
8023 if (sa_res_key == 0) {
8025 * Unregister everybody else and build UA for
8028 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8029 if (i == msg->pr.pr_info.residx ||
8030 ctl_get_prkey(lun, i) == 0)
8033 ctl_clr_prkey(lun, i);
8034 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8037 lun->pr_key_count = 1;
8038 lun->pr_res_type = msg->pr.pr_info.res_type;
8039 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8040 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8041 lun->pr_res_idx = msg->pr.pr_info.residx;
8043 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8044 if (sa_res_key == ctl_get_prkey(lun, i))
8047 ctl_clr_prkey(lun, i);
8048 lun->pr_key_count--;
8049 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8053 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8054 if (i == msg->pr.pr_info.residx ||
8055 ctl_get_prkey(lun, i) == 0)
8058 if (sa_res_key == ctl_get_prkey(lun, i)) {
8059 ctl_clr_prkey(lun, i);
8060 lun->pr_key_count--;
8061 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8062 } else if (msg->pr.pr_info.res_type != lun->pr_res_type
8063 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8064 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8065 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8068 lun->pr_res_type = msg->pr.pr_info.res_type;
8069 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8070 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8071 lun->pr_res_idx = msg->pr.pr_info.residx;
8073 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8075 lun->pr_generation++;
8081 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8083 struct ctl_softc *softc = CTL_SOFTC(ctsio);
8084 struct ctl_lun *lun = CTL_LUN(ctsio);
8086 u_int32_t param_len;
8087 struct scsi_per_res_out *cdb;
8088 struct scsi_per_res_out_parms* param;
8090 uint64_t res_key, sa_res_key, key;
8092 union ctl_ha_msg persis_io;
8095 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8097 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8098 retval = CTL_RETVAL_COMPLETE;
8101 * We only support whole-LUN scope. The scope & type are ignored for
8102 * register, register and ignore existing key and clear.
8103 * We sometimes ignore scope and type on preempts too!!
8104 * Verify reservation type here as well.
8106 type = cdb->scope_type & SPR_TYPE_MASK;
8107 if ((cdb->action == SPRO_RESERVE)
8108 || (cdb->action == SPRO_RELEASE)) {
8109 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8110 ctl_set_invalid_field(/*ctsio*/ ctsio,
8116 ctl_done((union ctl_io *)ctsio);
8117 return (CTL_RETVAL_COMPLETE);
8120 if (type>8 || type==2 || type==4 || type==0) {
8121 ctl_set_invalid_field(/*ctsio*/ ctsio,
8127 ctl_done((union ctl_io *)ctsio);
8128 return (CTL_RETVAL_COMPLETE);
8132 param_len = scsi_4btoul(cdb->length);
8134 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8135 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8136 ctsio->kern_data_len = param_len;
8137 ctsio->kern_total_len = param_len;
8138 ctsio->kern_rel_offset = 0;
8139 ctsio->kern_sg_entries = 0;
8140 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8141 ctsio->be_move_done = ctl_config_move_done;
8142 ctl_datamove((union ctl_io *)ctsio);
8144 return (CTL_RETVAL_COMPLETE);
8147 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8149 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8150 res_key = scsi_8btou64(param->res_key.key);
8151 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8154 * Validate the reservation key here except for SPRO_REG_IGNO
8155 * This must be done for all other service actions
8157 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8158 mtx_lock(&lun->lun_lock);
8159 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8160 if (res_key != key) {
8162 * The current key passed in doesn't match
8163 * the one the initiator previously
8166 mtx_unlock(&lun->lun_lock);
8167 free(ctsio->kern_data_ptr, M_CTL);
8168 ctl_set_reservation_conflict(ctsio);
8169 ctl_done((union ctl_io *)ctsio);
8170 return (CTL_RETVAL_COMPLETE);
8172 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8174 * We are not registered
8176 mtx_unlock(&lun->lun_lock);
8177 free(ctsio->kern_data_ptr, M_CTL);
8178 ctl_set_reservation_conflict(ctsio);
8179 ctl_done((union ctl_io *)ctsio);
8180 return (CTL_RETVAL_COMPLETE);
8181 } else if (res_key != 0) {
8183 * We are not registered and trying to register but
8184 * the register key isn't zero.
8186 mtx_unlock(&lun->lun_lock);
8187 free(ctsio->kern_data_ptr, M_CTL);
8188 ctl_set_reservation_conflict(ctsio);
8189 ctl_done((union ctl_io *)ctsio);
8190 return (CTL_RETVAL_COMPLETE);
8192 mtx_unlock(&lun->lun_lock);
8195 switch (cdb->action & SPRO_ACTION_MASK) {
8197 case SPRO_REG_IGNO: {
8200 printf("Registration received\n");
8204 * We don't support any of these options, as we report in
8205 * the read capabilities request (see
8206 * ctl_persistent_reserve_in(), above).
8208 if ((param->flags & SPR_SPEC_I_PT)
8209 || (param->flags & SPR_ALL_TG_PT)
8210 || (param->flags & SPR_APTPL)) {
8213 if (param->flags & SPR_APTPL)
8215 else if (param->flags & SPR_ALL_TG_PT)
8217 else /* SPR_SPEC_I_PT */
8220 free(ctsio->kern_data_ptr, M_CTL);
8221 ctl_set_invalid_field(ctsio,
8227 ctl_done((union ctl_io *)ctsio);
8228 return (CTL_RETVAL_COMPLETE);
8231 mtx_lock(&lun->lun_lock);
8234 * The initiator wants to clear the
8237 if (sa_res_key == 0) {
8239 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8240 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8241 && ctl_get_prkey(lun, residx) == 0)) {
8242 mtx_unlock(&lun->lun_lock);
8246 ctl_clr_prkey(lun, residx);
8247 lun->pr_key_count--;
8249 if (residx == lun->pr_res_idx) {
8250 lun->flags &= ~CTL_LUN_PR_RESERVED;
8251 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8253 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8254 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8255 lun->pr_key_count) {
8257 * If the reservation is a registrants
8258 * only type we need to generate a UA
8259 * for other registered inits. The
8260 * sense code should be RESERVATIONS
8264 for (i = softc->init_min; i < softc->init_max; i++){
8265 if (ctl_get_prkey(lun, i) == 0)
8268 CTL_UA_RES_RELEASE);
8271 lun->pr_res_type = 0;
8272 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8273 if (lun->pr_key_count==0) {
8274 lun->flags &= ~CTL_LUN_PR_RESERVED;
8275 lun->pr_res_type = 0;
8276 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8279 lun->pr_generation++;
8280 mtx_unlock(&lun->lun_lock);
8282 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8283 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8284 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8285 persis_io.pr.pr_info.residx = residx;
8286 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8287 sizeof(persis_io.pr), M_WAITOK);
8288 } else /* sa_res_key != 0 */ {
8291 * If we aren't registered currently then increment
8292 * the key count and set the registered flag.
8294 ctl_alloc_prkey(lun, residx);
8295 if (ctl_get_prkey(lun, residx) == 0)
8296 lun->pr_key_count++;
8297 ctl_set_prkey(lun, residx, sa_res_key);
8298 lun->pr_generation++;
8299 mtx_unlock(&lun->lun_lock);
8301 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8302 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8303 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8304 persis_io.pr.pr_info.residx = residx;
8305 memcpy(persis_io.pr.pr_info.sa_res_key,
8306 param->serv_act_res_key,
8307 sizeof(param->serv_act_res_key));
8308 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8309 sizeof(persis_io.pr), M_WAITOK);
8316 printf("Reserve executed type %d\n", type);
8318 mtx_lock(&lun->lun_lock);
8319 if (lun->flags & CTL_LUN_PR_RESERVED) {
8321 * if this isn't the reservation holder and it's
8322 * not a "all registrants" type or if the type is
8323 * different then we have a conflict
8325 if ((lun->pr_res_idx != residx
8326 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8327 || lun->pr_res_type != type) {
8328 mtx_unlock(&lun->lun_lock);
8329 free(ctsio->kern_data_ptr, M_CTL);
8330 ctl_set_reservation_conflict(ctsio);
8331 ctl_done((union ctl_io *)ctsio);
8332 return (CTL_RETVAL_COMPLETE);
8334 mtx_unlock(&lun->lun_lock);
8335 } else /* create a reservation */ {
8337 * If it's not an "all registrants" type record
8338 * reservation holder
8340 if (type != SPR_TYPE_WR_EX_AR
8341 && type != SPR_TYPE_EX_AC_AR)
8342 lun->pr_res_idx = residx; /* Res holder */
8344 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8346 lun->flags |= CTL_LUN_PR_RESERVED;
8347 lun->pr_res_type = type;
8349 mtx_unlock(&lun->lun_lock);
8351 /* send msg to other side */
8352 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8353 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8354 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8355 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8356 persis_io.pr.pr_info.res_type = type;
8357 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8358 sizeof(persis_io.pr), M_WAITOK);
8363 mtx_lock(&lun->lun_lock);
8364 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8365 /* No reservation exists return good status */
8366 mtx_unlock(&lun->lun_lock);
8370 * Is this nexus a reservation holder?
8372 if (lun->pr_res_idx != residx
8373 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8375 * not a res holder return good status but
8378 mtx_unlock(&lun->lun_lock);
8382 if (lun->pr_res_type != type) {
8383 mtx_unlock(&lun->lun_lock);
8384 free(ctsio->kern_data_ptr, M_CTL);
8385 ctl_set_illegal_pr_release(ctsio);
8386 ctl_done((union ctl_io *)ctsio);
8387 return (CTL_RETVAL_COMPLETE);
8390 /* okay to release */
8391 lun->flags &= ~CTL_LUN_PR_RESERVED;
8392 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8393 lun->pr_res_type = 0;
8396 * If this isn't an exclusive access reservation and NUAR
8397 * is not set, generate UA for all other registrants.
8399 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX &&
8400 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8401 for (i = softc->init_min; i < softc->init_max; i++) {
8402 if (i == residx || ctl_get_prkey(lun, i) == 0)
8404 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8407 mtx_unlock(&lun->lun_lock);
8409 /* Send msg to other side */
8410 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8411 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8412 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8413 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8414 sizeof(persis_io.pr), M_WAITOK);
8418 /* send msg to other side */
8420 mtx_lock(&lun->lun_lock);
8421 lun->flags &= ~CTL_LUN_PR_RESERVED;
8422 lun->pr_res_type = 0;
8423 lun->pr_key_count = 0;
8424 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8426 ctl_clr_prkey(lun, residx);
8427 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8428 if (ctl_get_prkey(lun, i) != 0) {
8429 ctl_clr_prkey(lun, i);
8430 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8432 lun->pr_generation++;
8433 mtx_unlock(&lun->lun_lock);
8435 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8436 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8437 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8438 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8439 sizeof(persis_io.pr), M_WAITOK);
8443 case SPRO_PRE_ABO: {
8446 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8447 residx, ctsio, cdb, param);
8449 return (CTL_RETVAL_COMPLETE);
8453 panic("%s: Invalid PR type %#x", __func__, cdb->action);
8457 free(ctsio->kern_data_ptr, M_CTL);
8458 ctl_set_success(ctsio);
8459 ctl_done((union ctl_io *)ctsio);
8465 * This routine is for handling a message from the other SC pertaining to
8466 * persistent reserve out. All the error checking will have been done
8467 * so only perorming the action need be done here to keep the two
8471 ctl_hndl_per_res_out_on_other_sc(union ctl_io *io)
8473 struct ctl_softc *softc = CTL_SOFTC(io);
8474 union ctl_ha_msg *msg = (union ctl_ha_msg *)&io->presio.pr_msg;
8475 struct ctl_lun *lun;
8477 uint32_t residx, targ_lun;
8479 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8480 mtx_lock(&softc->ctl_lock);
8481 if (targ_lun >= CTL_MAX_LUNS ||
8482 (lun = softc->ctl_luns[targ_lun]) == NULL) {
8483 mtx_unlock(&softc->ctl_lock);
8486 mtx_lock(&lun->lun_lock);
8487 mtx_unlock(&softc->ctl_lock);
8488 if (lun->flags & CTL_LUN_DISABLED) {
8489 mtx_unlock(&lun->lun_lock);
8492 residx = ctl_get_initindex(&msg->hdr.nexus);
8493 switch(msg->pr.pr_info.action) {
8494 case CTL_PR_REG_KEY:
8495 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8496 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8497 lun->pr_key_count++;
8498 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8499 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8500 lun->pr_generation++;
8503 case CTL_PR_UNREG_KEY:
8504 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8505 lun->pr_key_count--;
8507 /* XXX Need to see if the reservation has been released */
8508 /* if so do we need to generate UA? */
8509 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8510 lun->flags &= ~CTL_LUN_PR_RESERVED;
8511 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8513 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8514 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8515 lun->pr_key_count) {
8517 * If the reservation is a registrants
8518 * only type we need to generate a UA
8519 * for other registered inits. The
8520 * sense code should be RESERVATIONS
8524 for (i = softc->init_min; i < softc->init_max; i++) {
8525 if (ctl_get_prkey(lun, i) == 0)
8528 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8531 lun->pr_res_type = 0;
8532 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8533 if (lun->pr_key_count==0) {
8534 lun->flags &= ~CTL_LUN_PR_RESERVED;
8535 lun->pr_res_type = 0;
8536 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8539 lun->pr_generation++;
8542 case CTL_PR_RESERVE:
8543 lun->flags |= CTL_LUN_PR_RESERVED;
8544 lun->pr_res_type = msg->pr.pr_info.res_type;
8545 lun->pr_res_idx = msg->pr.pr_info.residx;
8549 case CTL_PR_RELEASE:
8551 * If this isn't an exclusive access reservation and NUAR
8552 * is not set, generate UA for all other registrants.
8554 if (lun->pr_res_type != SPR_TYPE_EX_AC &&
8555 lun->pr_res_type != SPR_TYPE_WR_EX &&
8556 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8557 for (i = softc->init_min; i < softc->init_max; i++)
8558 if (i == residx || ctl_get_prkey(lun, i) == 0)
8560 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8563 lun->flags &= ~CTL_LUN_PR_RESERVED;
8564 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8565 lun->pr_res_type = 0;
8568 case CTL_PR_PREEMPT:
8569 ctl_pro_preempt_other(lun, msg);
8572 lun->flags &= ~CTL_LUN_PR_RESERVED;
8573 lun->pr_res_type = 0;
8574 lun->pr_key_count = 0;
8575 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8577 for (i=0; i < CTL_MAX_INITIATORS; i++) {
8578 if (ctl_get_prkey(lun, i) == 0)
8580 ctl_clr_prkey(lun, i);
8581 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8583 lun->pr_generation++;
8587 mtx_unlock(&lun->lun_lock);
8591 ctl_read_write(struct ctl_scsiio *ctsio)
8593 struct ctl_lun *lun = CTL_LUN(ctsio);
8594 struct ctl_lba_len_flags *lbalen;
8596 uint32_t num_blocks;
8600 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8603 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8604 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8605 switch (ctsio->cdb[0]) {
8608 struct scsi_rw_6 *cdb;
8610 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8612 lba = scsi_3btoul(cdb->addr);
8613 /* only 5 bits are valid in the most significant address byte */
8615 num_blocks = cdb->length;
8617 * This is correct according to SBC-2.
8619 if (num_blocks == 0)
8625 struct scsi_rw_10 *cdb;
8627 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8628 if (cdb->byte2 & SRW10_FUA)
8629 flags |= CTL_LLF_FUA;
8630 if (cdb->byte2 & SRW10_DPO)
8631 flags |= CTL_LLF_DPO;
8632 lba = scsi_4btoul(cdb->addr);
8633 num_blocks = scsi_2btoul(cdb->length);
8636 case WRITE_VERIFY_10: {
8637 struct scsi_write_verify_10 *cdb;
8639 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8640 flags |= CTL_LLF_FUA;
8641 if (cdb->byte2 & SWV_DPO)
8642 flags |= CTL_LLF_DPO;
8643 lba = scsi_4btoul(cdb->addr);
8644 num_blocks = scsi_2btoul(cdb->length);
8649 struct scsi_rw_12 *cdb;
8651 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8652 if (cdb->byte2 & SRW12_FUA)
8653 flags |= CTL_LLF_FUA;
8654 if (cdb->byte2 & SRW12_DPO)
8655 flags |= CTL_LLF_DPO;
8656 lba = scsi_4btoul(cdb->addr);
8657 num_blocks = scsi_4btoul(cdb->length);
8660 case WRITE_VERIFY_12: {
8661 struct scsi_write_verify_12 *cdb;
8663 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8664 flags |= CTL_LLF_FUA;
8665 if (cdb->byte2 & SWV_DPO)
8666 flags |= CTL_LLF_DPO;
8667 lba = scsi_4btoul(cdb->addr);
8668 num_blocks = scsi_4btoul(cdb->length);
8673 struct scsi_rw_16 *cdb;
8675 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8676 if (cdb->byte2 & SRW12_FUA)
8677 flags |= CTL_LLF_FUA;
8678 if (cdb->byte2 & SRW12_DPO)
8679 flags |= CTL_LLF_DPO;
8680 lba = scsi_8btou64(cdb->addr);
8681 num_blocks = scsi_4btoul(cdb->length);
8684 case WRITE_ATOMIC_16: {
8685 struct scsi_write_atomic_16 *cdb;
8687 if (lun->be_lun->atomicblock == 0) {
8688 ctl_set_invalid_opcode(ctsio);
8689 ctl_done((union ctl_io *)ctsio);
8690 return (CTL_RETVAL_COMPLETE);
8693 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb;
8694 if (cdb->byte2 & SRW12_FUA)
8695 flags |= CTL_LLF_FUA;
8696 if (cdb->byte2 & SRW12_DPO)
8697 flags |= CTL_LLF_DPO;
8698 lba = scsi_8btou64(cdb->addr);
8699 num_blocks = scsi_2btoul(cdb->length);
8700 if (num_blocks > lun->be_lun->atomicblock) {
8701 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8702 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8704 ctl_done((union ctl_io *)ctsio);
8705 return (CTL_RETVAL_COMPLETE);
8709 case WRITE_VERIFY_16: {
8710 struct scsi_write_verify_16 *cdb;
8712 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8713 flags |= CTL_LLF_FUA;
8714 if (cdb->byte2 & SWV_DPO)
8715 flags |= CTL_LLF_DPO;
8716 lba = scsi_8btou64(cdb->addr);
8717 num_blocks = scsi_4btoul(cdb->length);
8722 * We got a command we don't support. This shouldn't
8723 * happen, commands should be filtered out above us.
8725 ctl_set_invalid_opcode(ctsio);
8726 ctl_done((union ctl_io *)ctsio);
8728 return (CTL_RETVAL_COMPLETE);
8729 break; /* NOTREACHED */
8733 * The first check is to make sure we're in bounds, the second
8734 * check is to catch wrap-around problems. If the lba + num blocks
8735 * is less than the lba, then we've wrapped around and the block
8736 * range is invalid anyway.
8738 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8739 || ((lba + num_blocks) < lba)) {
8740 ctl_set_lba_out_of_range(ctsio,
8741 MAX(lba, lun->be_lun->maxlba + 1));
8742 ctl_done((union ctl_io *)ctsio);
8743 return (CTL_RETVAL_COMPLETE);
8747 * According to SBC-3, a transfer length of 0 is not an error.
8748 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8749 * translates to 256 blocks for those commands.
8751 if (num_blocks == 0) {
8752 ctl_set_success(ctsio);
8753 ctl_done((union ctl_io *)ctsio);
8754 return (CTL_RETVAL_COMPLETE);
8757 /* Set FUA and/or DPO if caches are disabled. */
8759 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0)
8760 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8762 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8763 flags |= CTL_LLF_FUA;
8766 lbalen = (struct ctl_lba_len_flags *)
8767 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8769 lbalen->len = num_blocks;
8770 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8772 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8773 ctsio->kern_rel_offset = 0;
8775 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8777 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8782 ctl_cnw_cont(union ctl_io *io)
8784 struct ctl_lun *lun = CTL_LUN(io);
8785 struct ctl_scsiio *ctsio;
8786 struct ctl_lba_len_flags *lbalen;
8789 ctsio = &io->scsiio;
8790 ctsio->io_hdr.status = CTL_STATUS_NONE;
8791 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8792 lbalen = (struct ctl_lba_len_flags *)
8793 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8794 lbalen->flags &= ~CTL_LLF_COMPARE;
8795 lbalen->flags |= CTL_LLF_WRITE;
8797 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8798 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8803 ctl_cnw(struct ctl_scsiio *ctsio)
8805 struct ctl_lun *lun = CTL_LUN(ctsio);
8806 struct ctl_lba_len_flags *lbalen;
8808 uint32_t num_blocks;
8811 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8814 switch (ctsio->cdb[0]) {
8815 case COMPARE_AND_WRITE: {
8816 struct scsi_compare_and_write *cdb;
8818 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8819 if (cdb->byte2 & SRW10_FUA)
8820 flags |= CTL_LLF_FUA;
8821 if (cdb->byte2 & SRW10_DPO)
8822 flags |= CTL_LLF_DPO;
8823 lba = scsi_8btou64(cdb->addr);
8824 num_blocks = cdb->length;
8829 * We got a command we don't support. This shouldn't
8830 * happen, commands should be filtered out above us.
8832 ctl_set_invalid_opcode(ctsio);
8833 ctl_done((union ctl_io *)ctsio);
8835 return (CTL_RETVAL_COMPLETE);
8836 break; /* NOTREACHED */
8840 * The first check is to make sure we're in bounds, the second
8841 * check is to catch wrap-around problems. If the lba + num blocks
8842 * is less than the lba, then we've wrapped around and the block
8843 * range is invalid anyway.
8845 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8846 || ((lba + num_blocks) < lba)) {
8847 ctl_set_lba_out_of_range(ctsio,
8848 MAX(lba, lun->be_lun->maxlba + 1));
8849 ctl_done((union ctl_io *)ctsio);
8850 return (CTL_RETVAL_COMPLETE);
8854 * According to SBC-3, a transfer length of 0 is not an error.
8856 if (num_blocks == 0) {
8857 ctl_set_success(ctsio);
8858 ctl_done((union ctl_io *)ctsio);
8859 return (CTL_RETVAL_COMPLETE);
8862 /* Set FUA if write cache is disabled. */
8863 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8864 flags |= CTL_LLF_FUA;
8866 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
8867 ctsio->kern_rel_offset = 0;
8870 * Set the IO_CONT flag, so that if this I/O gets passed to
8871 * ctl_data_submit_done(), it'll get passed back to
8872 * ctl_ctl_cnw_cont() for further processing.
8874 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
8875 ctsio->io_cont = ctl_cnw_cont;
8877 lbalen = (struct ctl_lba_len_flags *)
8878 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8880 lbalen->len = num_blocks;
8881 lbalen->flags = CTL_LLF_COMPARE | flags;
8883 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
8884 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8889 ctl_verify(struct ctl_scsiio *ctsio)
8891 struct ctl_lun *lun = CTL_LUN(ctsio);
8892 struct ctl_lba_len_flags *lbalen;
8894 uint32_t num_blocks;
8898 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
8901 flags = CTL_LLF_FUA;
8902 switch (ctsio->cdb[0]) {
8904 struct scsi_verify_10 *cdb;
8906 cdb = (struct scsi_verify_10 *)ctsio->cdb;
8907 if (cdb->byte2 & SVFY_BYTCHK)
8909 if (cdb->byte2 & SVFY_DPO)
8910 flags |= CTL_LLF_DPO;
8911 lba = scsi_4btoul(cdb->addr);
8912 num_blocks = scsi_2btoul(cdb->length);
8916 struct scsi_verify_12 *cdb;
8918 cdb = (struct scsi_verify_12 *)ctsio->cdb;
8919 if (cdb->byte2 & SVFY_BYTCHK)
8921 if (cdb->byte2 & SVFY_DPO)
8922 flags |= CTL_LLF_DPO;
8923 lba = scsi_4btoul(cdb->addr);
8924 num_blocks = scsi_4btoul(cdb->length);
8928 struct scsi_rw_16 *cdb;
8930 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8931 if (cdb->byte2 & SVFY_BYTCHK)
8933 if (cdb->byte2 & SVFY_DPO)
8934 flags |= CTL_LLF_DPO;
8935 lba = scsi_8btou64(cdb->addr);
8936 num_blocks = scsi_4btoul(cdb->length);
8941 * We got a command we don't support. This shouldn't
8942 * happen, commands should be filtered out above us.
8944 ctl_set_invalid_opcode(ctsio);
8945 ctl_done((union ctl_io *)ctsio);
8946 return (CTL_RETVAL_COMPLETE);
8950 * The first check is to make sure we're in bounds, the second
8951 * check is to catch wrap-around problems. If the lba + num blocks
8952 * is less than the lba, then we've wrapped around and the block
8953 * range is invalid anyway.
8955 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8956 || ((lba + num_blocks) < lba)) {
8957 ctl_set_lba_out_of_range(ctsio,
8958 MAX(lba, lun->be_lun->maxlba + 1));
8959 ctl_done((union ctl_io *)ctsio);
8960 return (CTL_RETVAL_COMPLETE);
8964 * According to SBC-3, a transfer length of 0 is not an error.
8966 if (num_blocks == 0) {
8967 ctl_set_success(ctsio);
8968 ctl_done((union ctl_io *)ctsio);
8969 return (CTL_RETVAL_COMPLETE);
8972 lbalen = (struct ctl_lba_len_flags *)
8973 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8975 lbalen->len = num_blocks;
8977 lbalen->flags = CTL_LLF_COMPARE | flags;
8978 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8980 lbalen->flags = CTL_LLF_VERIFY | flags;
8981 ctsio->kern_total_len = 0;
8983 ctsio->kern_rel_offset = 0;
8985 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
8986 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8991 ctl_report_luns(struct ctl_scsiio *ctsio)
8993 struct ctl_softc *softc = CTL_SOFTC(ctsio);
8994 struct ctl_port *port = CTL_PORT(ctsio);
8995 struct ctl_lun *lun, *request_lun = CTL_LUN(ctsio);
8996 struct scsi_report_luns *cdb;
8997 struct scsi_report_luns_data *lun_data;
8998 int num_filled, num_luns, num_port_luns, retval;
8999 uint32_t alloc_len, lun_datalen;
9000 uint32_t initidx, targ_lun_id, lun_id;
9002 retval = CTL_RETVAL_COMPLETE;
9003 cdb = (struct scsi_report_luns *)ctsio->cdb;
9005 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9008 num_port_luns = port->lun_map ? port->lun_map_size : CTL_MAX_LUNS;
9009 mtx_lock(&softc->ctl_lock);
9010 for (targ_lun_id = 0; targ_lun_id < num_port_luns; targ_lun_id++) {
9011 if (ctl_lun_map_from_port(port, targ_lun_id) != UINT32_MAX)
9014 mtx_unlock(&softc->ctl_lock);
9016 switch (cdb->select_report) {
9017 case RPL_REPORT_DEFAULT:
9018 case RPL_REPORT_ALL:
9019 case RPL_REPORT_NONSUBSID:
9021 case RPL_REPORT_WELLKNOWN:
9022 case RPL_REPORT_ADMIN:
9023 case RPL_REPORT_CONGLOM:
9027 ctl_set_invalid_field(ctsio,
9033 ctl_done((union ctl_io *)ctsio);
9035 break; /* NOTREACHED */
9038 alloc_len = scsi_4btoul(cdb->length);
9040 * The initiator has to allocate at least 16 bytes for this request,
9041 * so he can at least get the header and the first LUN. Otherwise
9042 * we reject the request (per SPC-3 rev 14, section 6.21).
9044 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9045 sizeof(struct scsi_report_luns_lundata))) {
9046 ctl_set_invalid_field(ctsio,
9052 ctl_done((union ctl_io *)ctsio);
9056 lun_datalen = sizeof(*lun_data) +
9057 (num_luns * sizeof(struct scsi_report_luns_lundata));
9059 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9060 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9061 ctsio->kern_sg_entries = 0;
9063 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9065 mtx_lock(&softc->ctl_lock);
9066 for (targ_lun_id = 0, num_filled = 0;
9067 targ_lun_id < num_port_luns && num_filled < num_luns;
9069 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9070 if (lun_id == UINT32_MAX)
9072 lun = softc->ctl_luns[lun_id];
9076 be64enc(lun_data->luns[num_filled++].lundata,
9077 ctl_encode_lun(targ_lun_id));
9080 * According to SPC-3, rev 14 section 6.21:
9082 * "The execution of a REPORT LUNS command to any valid and
9083 * installed logical unit shall clear the REPORTED LUNS DATA
9084 * HAS CHANGED unit attention condition for all logical
9085 * units of that target with respect to the requesting
9086 * initiator. A valid and installed logical unit is one
9087 * having a PERIPHERAL QUALIFIER of 000b in the standard
9088 * INQUIRY data (see 6.4.2)."
9090 * If request_lun is NULL, the LUN this report luns command
9091 * was issued to is either disabled or doesn't exist. In that
9092 * case, we shouldn't clear any pending lun change unit
9095 if (request_lun != NULL) {
9096 mtx_lock(&lun->lun_lock);
9097 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE);
9098 mtx_unlock(&lun->lun_lock);
9101 mtx_unlock(&softc->ctl_lock);
9104 * It's quite possible that we've returned fewer LUNs than we allocated
9105 * space for. Trim it.
9107 lun_datalen = sizeof(*lun_data) +
9108 (num_filled * sizeof(struct scsi_report_luns_lundata));
9109 ctsio->kern_rel_offset = 0;
9110 ctsio->kern_sg_entries = 0;
9111 ctsio->kern_data_len = min(lun_datalen, alloc_len);
9112 ctsio->kern_total_len = ctsio->kern_data_len;
9115 * We set this to the actual data length, regardless of how much
9116 * space we actually have to return results. If the user looks at
9117 * this value, he'll know whether or not he allocated enough space
9118 * and reissue the command if necessary. We don't support well
9119 * known logical units, so if the user asks for that, return none.
9121 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9124 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9127 ctl_set_success(ctsio);
9128 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9129 ctsio->be_move_done = ctl_config_move_done;
9130 ctl_datamove((union ctl_io *)ctsio);
9135 ctl_request_sense(struct ctl_scsiio *ctsio)
9137 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9138 struct ctl_lun *lun = CTL_LUN(ctsio);
9139 struct scsi_request_sense *cdb;
9140 struct scsi_sense_data *sense_ptr, *ps;
9143 u_int sense_len = SSD_FULL_SIZE;
9144 scsi_sense_data_type sense_format;
9145 ctl_ua_type ua_type;
9146 uint8_t asc = 0, ascq = 0;
9148 cdb = (struct scsi_request_sense *)ctsio->cdb;
9150 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9153 * Determine which sense format the user wants.
9155 if (cdb->byte2 & SRS_DESC)
9156 sense_format = SSD_TYPE_DESC;
9158 sense_format = SSD_TYPE_FIXED;
9160 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9161 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9162 ctsio->kern_sg_entries = 0;
9163 ctsio->kern_rel_offset = 0;
9166 * struct scsi_sense_data, which is currently set to 256 bytes, is
9167 * larger than the largest allowed value for the length field in the
9168 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9170 ctsio->kern_data_len = cdb->length;
9171 ctsio->kern_total_len = cdb->length;
9174 * If we don't have a LUN, we don't have any pending sense.
9177 ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
9178 softc->ha_link < CTL_HA_LINK_UNKNOWN)) {
9179 /* "Logical unit not supported" */
9180 ctl_set_sense_data(sense_ptr, &sense_len, NULL, sense_format,
9181 /*current_error*/ 1,
9182 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
9190 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9192 * Check for pending sense, and then for pending unit attentions.
9193 * Pending sense gets returned first, then pending unit attentions.
9195 mtx_lock(&lun->lun_lock);
9196 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT];
9198 ps += initidx % CTL_MAX_INIT_PER_PORT;
9199 if (ps != NULL && ps->error_code != 0) {
9200 scsi_sense_data_type stored_format;
9203 * Check to see which sense format was used for the stored
9206 stored_format = scsi_sense_type(ps);
9209 * If the user requested a different sense format than the
9210 * one we stored, then we need to convert it to the other
9211 * format. If we're going from descriptor to fixed format
9212 * sense data, we may lose things in translation, depending
9213 * on what options were used.
9215 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9216 * for some reason we'll just copy it out as-is.
9218 if ((stored_format == SSD_TYPE_FIXED)
9219 && (sense_format == SSD_TYPE_DESC))
9220 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9221 ps, (struct scsi_sense_data_desc *)sense_ptr);
9222 else if ((stored_format == SSD_TYPE_DESC)
9223 && (sense_format == SSD_TYPE_FIXED))
9224 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9225 ps, (struct scsi_sense_data_fixed *)sense_ptr);
9227 memcpy(sense_ptr, ps, sizeof(*sense_ptr));
9232 ua_type = ctl_build_ua(lun, initidx, sense_ptr, &sense_len,
9234 if (ua_type != CTL_UA_NONE)
9237 if (have_error == 0) {
9239 * Report informational exception if have one and allowed.
9241 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) {
9243 ascq = lun->ie_ascq;
9245 ctl_set_sense_data(sense_ptr, &sense_len, lun, sense_format,
9246 /*current_error*/ 1,
9247 /*sense_key*/ SSD_KEY_NO_SENSE,
9252 mtx_unlock(&lun->lun_lock);
9256 * We report the SCSI status as OK, since the status of the command
9257 * itself is OK. We're reporting sense as parameter data.
9259 ctl_set_success(ctsio);
9260 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9261 ctsio->be_move_done = ctl_config_move_done;
9262 ctl_datamove((union ctl_io *)ctsio);
9263 return (CTL_RETVAL_COMPLETE);
9267 ctl_tur(struct ctl_scsiio *ctsio)
9270 CTL_DEBUG_PRINT(("ctl_tur\n"));
9272 ctl_set_success(ctsio);
9273 ctl_done((union ctl_io *)ctsio);
9275 return (CTL_RETVAL_COMPLETE);
9279 * SCSI VPD page 0x00, the Supported VPD Pages page.
9282 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9284 struct ctl_lun *lun = CTL_LUN(ctsio);
9285 struct scsi_vpd_supported_pages *pages;
9289 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9290 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9291 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9292 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9293 ctsio->kern_rel_offset = 0;
9294 ctsio->kern_sg_entries = 0;
9295 ctsio->kern_data_len = min(sup_page_size, alloc_len);
9296 ctsio->kern_total_len = ctsio->kern_data_len;
9299 * The control device is always connected. The disk device, on the
9300 * other hand, may not be online all the time. Need to change this
9301 * to figure out whether the disk device is actually online or not.
9304 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9305 lun->be_lun->lun_type;
9307 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9310 /* Supported VPD pages */
9311 pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9313 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9314 /* Device Identification */
9315 pages->page_list[p++] = SVPD_DEVICE_ID;
9316 /* Extended INQUIRY Data */
9317 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9318 /* Mode Page Policy */
9319 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9321 pages->page_list[p++] = SVPD_SCSI_PORTS;
9322 /* Third-party Copy */
9323 pages->page_list[p++] = SVPD_SCSI_TPC;
9324 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9326 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9327 /* Block Device Characteristics */
9328 pages->page_list[p++] = SVPD_BDC;
9329 /* Logical Block Provisioning */
9330 pages->page_list[p++] = SVPD_LBP;
9334 ctl_set_success(ctsio);
9335 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9336 ctsio->be_move_done = ctl_config_move_done;
9337 ctl_datamove((union ctl_io *)ctsio);
9338 return (CTL_RETVAL_COMPLETE);
9342 * SCSI VPD page 0x80, the Unit Serial Number page.
9345 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9347 struct ctl_lun *lun = CTL_LUN(ctsio);
9348 struct scsi_vpd_unit_serial_number *sn_ptr;
9351 data_len = 4 + CTL_SN_LEN;
9352 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9353 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9354 ctsio->kern_rel_offset = 0;
9355 ctsio->kern_sg_entries = 0;
9356 ctsio->kern_data_len = min(data_len, alloc_len);
9357 ctsio->kern_total_len = ctsio->kern_data_len;
9360 * The control device is always connected. The disk device, on the
9361 * other hand, may not be online all the time. Need to change this
9362 * to figure out whether the disk device is actually online or not.
9365 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9366 lun->be_lun->lun_type;
9368 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9370 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9371 sn_ptr->length = CTL_SN_LEN;
9373 * If we don't have a LUN, we just leave the serial number as
9377 strncpy((char *)sn_ptr->serial_num,
9378 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9380 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9382 ctl_set_success(ctsio);
9383 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9384 ctsio->be_move_done = ctl_config_move_done;
9385 ctl_datamove((union ctl_io *)ctsio);
9386 return (CTL_RETVAL_COMPLETE);
9391 * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9394 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9396 struct ctl_lun *lun = CTL_LUN(ctsio);
9397 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9400 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9401 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9402 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9403 ctsio->kern_sg_entries = 0;
9404 ctsio->kern_rel_offset = 0;
9405 ctsio->kern_data_len = min(data_len, alloc_len);
9406 ctsio->kern_total_len = ctsio->kern_data_len;
9409 * The control device is always connected. The disk device, on the
9410 * other hand, may not be online all the time.
9413 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9414 lun->be_lun->lun_type;
9416 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9417 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9418 scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9420 * We support head of queue, ordered and simple tags.
9422 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9424 * Volatile cache supported.
9426 eid_ptr->flags3 = SVPD_EID_V_SUP;
9429 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9430 * attention for a particular IT nexus on all LUNs once we report
9431 * it to that nexus once. This bit is required as of SPC-4.
9433 eid_ptr->flags4 = SVPD_EID_LUICLR;
9436 * We support revert to defaults (RTD) bit in MODE SELECT.
9438 eid_ptr->flags5 = SVPD_EID_RTD_SUP;
9441 * XXX KDM in order to correctly answer this, we would need
9442 * information from the SIM to determine how much sense data it
9443 * can send. So this would really be a path inquiry field, most
9444 * likely. This can be set to a maximum of 252 according to SPC-4,
9445 * but the hardware may or may not be able to support that much.
9446 * 0 just means that the maximum sense data length is not reported.
9448 eid_ptr->max_sense_length = 0;
9450 ctl_set_success(ctsio);
9451 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9452 ctsio->be_move_done = ctl_config_move_done;
9453 ctl_datamove((union ctl_io *)ctsio);
9454 return (CTL_RETVAL_COMPLETE);
9458 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9460 struct ctl_lun *lun = CTL_LUN(ctsio);
9461 struct scsi_vpd_mode_page_policy *mpp_ptr;
9464 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9465 sizeof(struct scsi_vpd_mode_page_policy_descr);
9467 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9468 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9469 ctsio->kern_rel_offset = 0;
9470 ctsio->kern_sg_entries = 0;
9471 ctsio->kern_data_len = min(data_len, alloc_len);
9472 ctsio->kern_total_len = ctsio->kern_data_len;
9475 * The control device is always connected. The disk device, on the
9476 * other hand, may not be online all the time.
9479 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9480 lun->be_lun->lun_type;
9482 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9483 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9484 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9485 mpp_ptr->descr[0].page_code = 0x3f;
9486 mpp_ptr->descr[0].subpage_code = 0xff;
9487 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9489 ctl_set_success(ctsio);
9490 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9491 ctsio->be_move_done = ctl_config_move_done;
9492 ctl_datamove((union ctl_io *)ctsio);
9493 return (CTL_RETVAL_COMPLETE);
9497 * SCSI VPD page 0x83, the Device Identification page.
9500 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9502 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9503 struct ctl_port *port = CTL_PORT(ctsio);
9504 struct ctl_lun *lun = CTL_LUN(ctsio);
9505 struct scsi_vpd_device_id *devid_ptr;
9506 struct scsi_vpd_id_descriptor *desc;
9510 data_len = sizeof(struct scsi_vpd_device_id) +
9511 sizeof(struct scsi_vpd_id_descriptor) +
9512 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9513 sizeof(struct scsi_vpd_id_descriptor) +
9514 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9515 if (lun && lun->lun_devid)
9516 data_len += lun->lun_devid->len;
9517 if (port && port->port_devid)
9518 data_len += port->port_devid->len;
9519 if (port && port->target_devid)
9520 data_len += port->target_devid->len;
9522 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9523 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9524 ctsio->kern_sg_entries = 0;
9525 ctsio->kern_rel_offset = 0;
9526 ctsio->kern_sg_entries = 0;
9527 ctsio->kern_data_len = min(data_len, alloc_len);
9528 ctsio->kern_total_len = ctsio->kern_data_len;
9531 * The control device is always connected. The disk device, on the
9532 * other hand, may not be online all the time.
9535 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9536 lun->be_lun->lun_type;
9538 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9539 devid_ptr->page_code = SVPD_DEVICE_ID;
9540 scsi_ulto2b(data_len - 4, devid_ptr->length);
9542 if (port && port->port_type == CTL_PORT_FC)
9543 proto = SCSI_PROTO_FC << 4;
9544 else if (port && port->port_type == CTL_PORT_SAS)
9545 proto = SCSI_PROTO_SAS << 4;
9546 else if (port && port->port_type == CTL_PORT_ISCSI)
9547 proto = SCSI_PROTO_ISCSI << 4;
9549 proto = SCSI_PROTO_SPI << 4;
9550 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9553 * We're using a LUN association here. i.e., this device ID is a
9554 * per-LUN identifier.
9556 if (lun && lun->lun_devid) {
9557 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9558 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9559 lun->lun_devid->len);
9563 * This is for the WWPN which is a port association.
9565 if (port && port->port_devid) {
9566 memcpy(desc, port->port_devid->data, port->port_devid->len);
9567 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9568 port->port_devid->len);
9572 * This is for the Relative Target Port(type 4h) identifier
9574 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9575 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9576 SVPD_ID_TYPE_RELTARG;
9578 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9579 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9580 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9583 * This is for the Target Port Group(type 5h) identifier
9585 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9586 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9587 SVPD_ID_TYPE_TPORTGRP;
9589 if (softc->is_single ||
9590 (port && port->status & CTL_PORT_STATUS_HA_SHARED))
9593 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt;
9594 scsi_ulto2b(g, &desc->identifier[2]);
9595 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9596 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9599 * This is for the Target identifier
9601 if (port && port->target_devid) {
9602 memcpy(desc, port->target_devid->data, port->target_devid->len);
9605 ctl_set_success(ctsio);
9606 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9607 ctsio->be_move_done = ctl_config_move_done;
9608 ctl_datamove((union ctl_io *)ctsio);
9609 return (CTL_RETVAL_COMPLETE);
9613 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9615 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9616 struct ctl_lun *lun = CTL_LUN(ctsio);
9617 struct scsi_vpd_scsi_ports *sp;
9618 struct scsi_vpd_port_designation *pd;
9619 struct scsi_vpd_port_designation_cont *pdc;
9620 struct ctl_port *port;
9621 int data_len, num_target_ports, iid_len, id_len;
9623 num_target_ports = 0;
9626 mtx_lock(&softc->ctl_lock);
9627 STAILQ_FOREACH(port, &softc->port_list, links) {
9628 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9631 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9634 if (port->init_devid)
9635 iid_len += port->init_devid->len;
9636 if (port->port_devid)
9637 id_len += port->port_devid->len;
9639 mtx_unlock(&softc->ctl_lock);
9641 data_len = sizeof(struct scsi_vpd_scsi_ports) +
9642 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9643 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9644 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9645 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9646 ctsio->kern_sg_entries = 0;
9647 ctsio->kern_rel_offset = 0;
9648 ctsio->kern_sg_entries = 0;
9649 ctsio->kern_data_len = min(data_len, alloc_len);
9650 ctsio->kern_total_len = ctsio->kern_data_len;
9653 * The control device is always connected. The disk device, on the
9654 * other hand, may not be online all the time. Need to change this
9655 * to figure out whether the disk device is actually online or not.
9658 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9659 lun->be_lun->lun_type;
9661 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9663 sp->page_code = SVPD_SCSI_PORTS;
9664 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9666 pd = &sp->design[0];
9668 mtx_lock(&softc->ctl_lock);
9669 STAILQ_FOREACH(port, &softc->port_list, links) {
9670 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9673 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9675 scsi_ulto2b(port->targ_port, pd->relative_port_id);
9676 if (port->init_devid) {
9677 iid_len = port->init_devid->len;
9678 memcpy(pd->initiator_transportid,
9679 port->init_devid->data, port->init_devid->len);
9682 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9683 pdc = (struct scsi_vpd_port_designation_cont *)
9684 (&pd->initiator_transportid[iid_len]);
9685 if (port->port_devid) {
9686 id_len = port->port_devid->len;
9687 memcpy(pdc->target_port_descriptors,
9688 port->port_devid->data, port->port_devid->len);
9691 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9692 pd = (struct scsi_vpd_port_designation *)
9693 ((uint8_t *)pdc->target_port_descriptors + id_len);
9695 mtx_unlock(&softc->ctl_lock);
9697 ctl_set_success(ctsio);
9698 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9699 ctsio->be_move_done = ctl_config_move_done;
9700 ctl_datamove((union ctl_io *)ctsio);
9701 return (CTL_RETVAL_COMPLETE);
9705 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9707 struct ctl_lun *lun = CTL_LUN(ctsio);
9708 struct scsi_vpd_block_limits *bl_ptr;
9711 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9712 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9713 ctsio->kern_sg_entries = 0;
9714 ctsio->kern_rel_offset = 0;
9715 ctsio->kern_sg_entries = 0;
9716 ctsio->kern_data_len = min(sizeof(*bl_ptr), alloc_len);
9717 ctsio->kern_total_len = ctsio->kern_data_len;
9720 * The control device is always connected. The disk device, on the
9721 * other hand, may not be online all the time. Need to change this
9722 * to figure out whether the disk device is actually online or not.
9725 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9726 lun->be_lun->lun_type;
9728 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9730 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9731 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
9732 bl_ptr->max_cmp_write_len = 0xff;
9733 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9735 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
9736 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9738 ctl_get_opt_number(&lun->be_lun->options,
9739 "unmap_max_lba", &ival);
9740 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt);
9742 ctl_get_opt_number(&lun->be_lun->options,
9743 "unmap_max_descr", &ival);
9744 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt);
9745 if (lun->be_lun->ublockexp != 0) {
9746 scsi_ulto4b((1 << lun->be_lun->ublockexp),
9747 bl_ptr->opt_unmap_grain);
9748 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
9749 bl_ptr->unmap_grain_align);
9752 scsi_ulto4b(lun->be_lun->atomicblock,
9753 bl_ptr->max_atomic_transfer_length);
9754 scsi_ulto4b(0, bl_ptr->atomic_alignment);
9755 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
9756 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary);
9757 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size);
9759 ctl_get_opt_number(&lun->be_lun->options, "write_same_max_lba", &ival);
9760 scsi_u64to8b(ival, bl_ptr->max_write_same_length);
9763 ctl_set_success(ctsio);
9764 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9765 ctsio->be_move_done = ctl_config_move_done;
9766 ctl_datamove((union ctl_io *)ctsio);
9767 return (CTL_RETVAL_COMPLETE);
9771 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
9773 struct ctl_lun *lun = CTL_LUN(ctsio);
9774 struct scsi_vpd_block_device_characteristics *bdc_ptr;
9778 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
9779 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
9780 ctsio->kern_sg_entries = 0;
9781 ctsio->kern_rel_offset = 0;
9782 ctsio->kern_data_len = min(sizeof(*bdc_ptr), alloc_len);
9783 ctsio->kern_total_len = ctsio->kern_data_len;
9786 * The control device is always connected. The disk device, on the
9787 * other hand, may not be online all the time. Need to change this
9788 * to figure out whether the disk device is actually online or not.
9791 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9792 lun->be_lun->lun_type;
9794 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9795 bdc_ptr->page_code = SVPD_BDC;
9796 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
9798 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL)
9799 i = strtol(value, NULL, 0);
9801 i = CTL_DEFAULT_ROTATION_RATE;
9802 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
9804 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL)
9805 i = strtol(value, NULL, 0);
9808 bdc_ptr->wab_wac_ff = (i & 0x0f);
9809 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
9811 ctl_set_success(ctsio);
9812 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9813 ctsio->be_move_done = ctl_config_move_done;
9814 ctl_datamove((union ctl_io *)ctsio);
9815 return (CTL_RETVAL_COMPLETE);
9819 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
9821 struct ctl_lun *lun = CTL_LUN(ctsio);
9822 struct scsi_vpd_logical_block_prov *lbp_ptr;
9825 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
9826 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
9827 ctsio->kern_sg_entries = 0;
9828 ctsio->kern_rel_offset = 0;
9829 ctsio->kern_data_len = min(sizeof(*lbp_ptr), alloc_len);
9830 ctsio->kern_total_len = ctsio->kern_data_len;
9833 * The control device is always connected. The disk device, on the
9834 * other hand, may not be online all the time. Need to change this
9835 * to figure out whether the disk device is actually online or not.
9838 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9839 lun->be_lun->lun_type;
9841 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9843 lbp_ptr->page_code = SVPD_LBP;
9844 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
9845 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
9846 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9847 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
9848 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
9849 value = ctl_get_opt(&lun->be_lun->options, "provisioning_type");
9850 if (value != NULL) {
9851 if (strcmp(value, "resource") == 0)
9852 lbp_ptr->prov_type = SVPD_LBP_RESOURCE;
9853 else if (strcmp(value, "thin") == 0)
9854 lbp_ptr->prov_type = SVPD_LBP_THIN;
9856 lbp_ptr->prov_type = SVPD_LBP_THIN;
9859 ctl_set_success(ctsio);
9860 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9861 ctsio->be_move_done = ctl_config_move_done;
9862 ctl_datamove((union ctl_io *)ctsio);
9863 return (CTL_RETVAL_COMPLETE);
9867 * INQUIRY with the EVPD bit set.
9870 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
9872 struct ctl_lun *lun = CTL_LUN(ctsio);
9873 struct scsi_inquiry *cdb;
9874 int alloc_len, retval;
9876 cdb = (struct scsi_inquiry *)ctsio->cdb;
9877 alloc_len = scsi_2btoul(cdb->length);
9879 switch (cdb->page_code) {
9880 case SVPD_SUPPORTED_PAGES:
9881 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
9883 case SVPD_UNIT_SERIAL_NUMBER:
9884 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
9886 case SVPD_DEVICE_ID:
9887 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
9889 case SVPD_EXTENDED_INQUIRY_DATA:
9890 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
9892 case SVPD_MODE_PAGE_POLICY:
9893 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
9895 case SVPD_SCSI_PORTS:
9896 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
9899 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
9901 case SVPD_BLOCK_LIMITS:
9902 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
9904 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
9907 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
9909 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
9912 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
9914 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
9918 ctl_set_invalid_field(ctsio,
9924 ctl_done((union ctl_io *)ctsio);
9925 retval = CTL_RETVAL_COMPLETE;
9933 * Standard INQUIRY data.
9936 ctl_inquiry_std(struct ctl_scsiio *ctsio)
9938 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9939 struct ctl_port *port = CTL_PORT(ctsio);
9940 struct ctl_lun *lun = CTL_LUN(ctsio);
9941 struct scsi_inquiry_data *inq_ptr;
9942 struct scsi_inquiry *cdb;
9944 uint32_t alloc_len, data_len;
9945 ctl_port_type port_type;
9947 port_type = port->port_type;
9948 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
9949 port_type = CTL_PORT_SCSI;
9951 cdb = (struct scsi_inquiry *)ctsio->cdb;
9952 alloc_len = scsi_2btoul(cdb->length);
9955 * We malloc the full inquiry data size here and fill it
9956 * in. If the user only asks for less, we'll give him
9959 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
9960 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9961 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
9962 ctsio->kern_sg_entries = 0;
9963 ctsio->kern_rel_offset = 0;
9964 ctsio->kern_data_len = min(data_len, alloc_len);
9965 ctsio->kern_total_len = ctsio->kern_data_len;
9968 if ((lun->flags & CTL_LUN_PRIMARY_SC) ||
9969 softc->ha_link >= CTL_HA_LINK_UNKNOWN) {
9970 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9971 lun->be_lun->lun_type;
9973 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) |
9974 lun->be_lun->lun_type;
9976 if (lun->flags & CTL_LUN_REMOVABLE)
9977 inq_ptr->dev_qual2 |= SID_RMB;
9979 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
9981 /* RMB in byte 2 is 0 */
9982 inq_ptr->version = SCSI_REV_SPC5;
9985 * According to SAM-3, even if a device only supports a single
9986 * level of LUN addressing, it should still set the HISUP bit:
9988 * 4.9.1 Logical unit numbers overview
9990 * All logical unit number formats described in this standard are
9991 * hierarchical in structure even when only a single level in that
9992 * hierarchy is used. The HISUP bit shall be set to one in the
9993 * standard INQUIRY data (see SPC-2) when any logical unit number
9994 * format described in this standard is used. Non-hierarchical
9995 * formats are outside the scope of this standard.
9997 * Therefore we set the HiSup bit here.
9999 * The response format is 2, per SPC-3.
10001 inq_ptr->response_format = SID_HiSup | 2;
10003 inq_ptr->additional_length = data_len -
10004 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10005 CTL_DEBUG_PRINT(("additional_length = %d\n",
10006 inq_ptr->additional_length));
10008 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10009 if (port_type == CTL_PORT_SCSI)
10010 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10011 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10012 inq_ptr->flags = SID_CmdQue;
10013 if (port_type == CTL_PORT_SCSI)
10014 inq_ptr->flags |= SID_WBus16 | SID_Sync;
10017 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10018 * We have 8 bytes for the vendor name, and 16 bytes for the device
10019 * name and 4 bytes for the revision.
10021 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10022 "vendor")) == NULL) {
10023 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10025 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10026 strncpy(inq_ptr->vendor, val,
10027 min(sizeof(inq_ptr->vendor), strlen(val)));
10030 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10031 sizeof(inq_ptr->product));
10032 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10033 switch (lun->be_lun->lun_type) {
10035 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10036 sizeof(inq_ptr->product));
10039 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10040 sizeof(inq_ptr->product));
10043 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT,
10044 sizeof(inq_ptr->product));
10047 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10048 sizeof(inq_ptr->product));
10052 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10053 strncpy(inq_ptr->product, val,
10054 min(sizeof(inq_ptr->product), strlen(val)));
10058 * XXX make this a macro somewhere so it automatically gets
10059 * incremented when we make changes.
10061 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10062 "revision")) == NULL) {
10063 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10065 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10066 strncpy(inq_ptr->revision, val,
10067 min(sizeof(inq_ptr->revision), strlen(val)));
10071 * For parallel SCSI, we support double transition and single
10072 * transition clocking. We also support QAS (Quick Arbitration
10073 * and Selection) and Information Unit transfers on both the
10074 * control and array devices.
10076 if (port_type == CTL_PORT_SCSI)
10077 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10080 /* SAM-6 (no version claimed) */
10081 scsi_ulto2b(0x00C0, inq_ptr->version1);
10082 /* SPC-5 (no version claimed) */
10083 scsi_ulto2b(0x05C0, inq_ptr->version2);
10084 if (port_type == CTL_PORT_FC) {
10085 /* FCP-2 ANSI INCITS.350:2003 */
10086 scsi_ulto2b(0x0917, inq_ptr->version3);
10087 } else if (port_type == CTL_PORT_SCSI) {
10088 /* SPI-4 ANSI INCITS.362:200x */
10089 scsi_ulto2b(0x0B56, inq_ptr->version3);
10090 } else if (port_type == CTL_PORT_ISCSI) {
10091 /* iSCSI (no version claimed) */
10092 scsi_ulto2b(0x0960, inq_ptr->version3);
10093 } else if (port_type == CTL_PORT_SAS) {
10094 /* SAS (no version claimed) */
10095 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10099 /* SBC-4 (no version claimed) */
10100 scsi_ulto2b(0x0600, inq_ptr->version4);
10102 switch (lun->be_lun->lun_type) {
10104 /* SBC-4 (no version claimed) */
10105 scsi_ulto2b(0x0600, inq_ptr->version4);
10110 /* MMC-6 (no version claimed) */
10111 scsi_ulto2b(0x04E0, inq_ptr->version4);
10118 ctl_set_success(ctsio);
10119 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10120 ctsio->be_move_done = ctl_config_move_done;
10121 ctl_datamove((union ctl_io *)ctsio);
10122 return (CTL_RETVAL_COMPLETE);
10126 ctl_inquiry(struct ctl_scsiio *ctsio)
10128 struct scsi_inquiry *cdb;
10131 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10133 cdb = (struct scsi_inquiry *)ctsio->cdb;
10134 if (cdb->byte2 & SI_EVPD)
10135 retval = ctl_inquiry_evpd(ctsio);
10136 else if (cdb->page_code == 0)
10137 retval = ctl_inquiry_std(ctsio);
10139 ctl_set_invalid_field(ctsio,
10145 ctl_done((union ctl_io *)ctsio);
10146 return (CTL_RETVAL_COMPLETE);
10153 ctl_get_config(struct ctl_scsiio *ctsio)
10155 struct ctl_lun *lun = CTL_LUN(ctsio);
10156 struct scsi_get_config_header *hdr;
10157 struct scsi_get_config_feature *feature;
10158 struct scsi_get_config *cdb;
10159 uint32_t alloc_len, data_len;
10162 cdb = (struct scsi_get_config *)ctsio->cdb;
10163 rt = (cdb->rt & SGC_RT_MASK);
10164 starting = scsi_2btoul(cdb->starting_feature);
10165 alloc_len = scsi_2btoul(cdb->length);
10167 data_len = sizeof(struct scsi_get_config_header) +
10168 sizeof(struct scsi_get_config_feature) + 8 +
10169 sizeof(struct scsi_get_config_feature) + 8 +
10170 sizeof(struct scsi_get_config_feature) + 4 +
10171 sizeof(struct scsi_get_config_feature) + 4 +
10172 sizeof(struct scsi_get_config_feature) + 8 +
10173 sizeof(struct scsi_get_config_feature) +
10174 sizeof(struct scsi_get_config_feature) + 4 +
10175 sizeof(struct scsi_get_config_feature) + 4 +
10176 sizeof(struct scsi_get_config_feature) + 4 +
10177 sizeof(struct scsi_get_config_feature) + 4 +
10178 sizeof(struct scsi_get_config_feature) + 4 +
10179 sizeof(struct scsi_get_config_feature) + 4;
10180 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10181 ctsio->kern_sg_entries = 0;
10182 ctsio->kern_rel_offset = 0;
10184 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr;
10185 if (lun->flags & CTL_LUN_NO_MEDIA)
10186 scsi_ulto2b(0x0000, hdr->current_profile);
10188 scsi_ulto2b(0x0010, hdr->current_profile);
10189 feature = (struct scsi_get_config_feature *)(hdr + 1);
10191 if (starting > 0x003b)
10193 if (starting > 0x003a)
10195 if (starting > 0x002b)
10197 if (starting > 0x002a)
10199 if (starting > 0x001f)
10201 if (starting > 0x001e)
10203 if (starting > 0x001d)
10205 if (starting > 0x0010)
10207 if (starting > 0x0003)
10209 if (starting > 0x0002)
10211 if (starting > 0x0001)
10213 if (starting > 0x0000)
10217 scsi_ulto2b(0x0000, feature->feature_code);
10218 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT;
10219 feature->add_length = 8;
10220 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */
10221 feature->feature_data[2] = 0x00;
10222 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */
10223 feature->feature_data[6] = 0x01;
10224 feature = (struct scsi_get_config_feature *)
10225 &feature->feature_data[feature->add_length];
10228 scsi_ulto2b(0x0001, feature->feature_code);
10229 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10230 feature->add_length = 8;
10231 scsi_ulto4b(0x00000000, &feature->feature_data[0]);
10232 feature->feature_data[4] = 0x03;
10233 feature = (struct scsi_get_config_feature *)
10234 &feature->feature_data[feature->add_length];
10237 scsi_ulto2b(0x0002, feature->feature_code);
10238 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10239 feature->add_length = 4;
10240 feature->feature_data[0] = 0x02;
10241 feature = (struct scsi_get_config_feature *)
10242 &feature->feature_data[feature->add_length];
10244 f3: /* Removable Medium */
10245 scsi_ulto2b(0x0003, feature->feature_code);
10246 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10247 feature->add_length = 4;
10248 feature->feature_data[0] = 0x39;
10249 feature = (struct scsi_get_config_feature *)
10250 &feature->feature_data[feature->add_length];
10252 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA))
10255 f10: /* Random Read */
10256 scsi_ulto2b(0x0010, feature->feature_code);
10257 feature->flags = 0x00;
10258 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10259 feature->flags |= SGC_F_CURRENT;
10260 feature->add_length = 8;
10261 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]);
10262 scsi_ulto2b(1, &feature->feature_data[4]);
10263 feature->feature_data[6] = 0x00;
10264 feature = (struct scsi_get_config_feature *)
10265 &feature->feature_data[feature->add_length];
10267 f1d: /* Multi-Read */
10268 scsi_ulto2b(0x001D, feature->feature_code);
10269 feature->flags = 0x00;
10270 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10271 feature->flags |= SGC_F_CURRENT;
10272 feature->add_length = 0;
10273 feature = (struct scsi_get_config_feature *)
10274 &feature->feature_data[feature->add_length];
10277 scsi_ulto2b(0x001E, feature->feature_code);
10278 feature->flags = 0x00;
10279 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10280 feature->flags |= SGC_F_CURRENT;
10281 feature->add_length = 4;
10282 feature->feature_data[0] = 0x00;
10283 feature = (struct scsi_get_config_feature *)
10284 &feature->feature_data[feature->add_length];
10286 f1f: /* DVD Read */
10287 scsi_ulto2b(0x001F, feature->feature_code);
10288 feature->flags = 0x08;
10289 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10290 feature->flags |= SGC_F_CURRENT;
10291 feature->add_length = 4;
10292 feature->feature_data[0] = 0x01;
10293 feature->feature_data[2] = 0x03;
10294 feature = (struct scsi_get_config_feature *)
10295 &feature->feature_data[feature->add_length];
10298 scsi_ulto2b(0x002A, feature->feature_code);
10299 feature->flags = 0x04;
10300 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10301 feature->flags |= SGC_F_CURRENT;
10302 feature->add_length = 4;
10303 feature->feature_data[0] = 0x00;
10304 feature->feature_data[1] = 0x00;
10305 feature = (struct scsi_get_config_feature *)
10306 &feature->feature_data[feature->add_length];
10309 scsi_ulto2b(0x002B, feature->feature_code);
10310 feature->flags = 0x00;
10311 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10312 feature->flags |= SGC_F_CURRENT;
10313 feature->add_length = 4;
10314 feature->feature_data[0] = 0x00;
10315 feature = (struct scsi_get_config_feature *)
10316 &feature->feature_data[feature->add_length];
10318 f3a: /* DVD+RW Dual Layer */
10319 scsi_ulto2b(0x003A, feature->feature_code);
10320 feature->flags = 0x00;
10321 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10322 feature->flags |= SGC_F_CURRENT;
10323 feature->add_length = 4;
10324 feature->feature_data[0] = 0x00;
10325 feature->feature_data[1] = 0x00;
10326 feature = (struct scsi_get_config_feature *)
10327 &feature->feature_data[feature->add_length];
10329 f3b: /* DVD+R Dual Layer */
10330 scsi_ulto2b(0x003B, feature->feature_code);
10331 feature->flags = 0x00;
10332 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10333 feature->flags |= SGC_F_CURRENT;
10334 feature->add_length = 4;
10335 feature->feature_data[0] = 0x00;
10336 feature = (struct scsi_get_config_feature *)
10337 &feature->feature_data[feature->add_length];
10340 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10341 if (rt == SGC_RT_SPECIFIC && data_len > 4) {
10342 feature = (struct scsi_get_config_feature *)(hdr + 1);
10343 if (scsi_2btoul(feature->feature_code) == starting)
10344 feature = (struct scsi_get_config_feature *)
10345 &feature->feature_data[feature->add_length];
10346 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10348 scsi_ulto4b(data_len - 4, hdr->data_length);
10349 ctsio->kern_data_len = min(data_len, alloc_len);
10350 ctsio->kern_total_len = ctsio->kern_data_len;
10352 ctl_set_success(ctsio);
10353 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10354 ctsio->be_move_done = ctl_config_move_done;
10355 ctl_datamove((union ctl_io *)ctsio);
10356 return (CTL_RETVAL_COMPLETE);
10360 ctl_get_event_status(struct ctl_scsiio *ctsio)
10362 struct scsi_get_event_status_header *hdr;
10363 struct scsi_get_event_status *cdb;
10364 uint32_t alloc_len, data_len;
10367 cdb = (struct scsi_get_event_status *)ctsio->cdb;
10368 if ((cdb->byte2 & SGESN_POLLED) == 0) {
10369 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
10370 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
10371 ctl_done((union ctl_io *)ctsio);
10372 return (CTL_RETVAL_COMPLETE);
10374 notif_class = cdb->notif_class;
10375 alloc_len = scsi_2btoul(cdb->length);
10377 data_len = sizeof(struct scsi_get_event_status_header);
10378 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10379 ctsio->kern_sg_entries = 0;
10380 ctsio->kern_rel_offset = 0;
10381 ctsio->kern_data_len = min(data_len, alloc_len);
10382 ctsio->kern_total_len = ctsio->kern_data_len;
10384 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr;
10385 scsi_ulto2b(0, hdr->descr_length);
10386 hdr->nea_class = SGESN_NEA;
10387 hdr->supported_class = 0;
10389 ctl_set_success(ctsio);
10390 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10391 ctsio->be_move_done = ctl_config_move_done;
10392 ctl_datamove((union ctl_io *)ctsio);
10393 return (CTL_RETVAL_COMPLETE);
10397 ctl_mechanism_status(struct ctl_scsiio *ctsio)
10399 struct scsi_mechanism_status_header *hdr;
10400 struct scsi_mechanism_status *cdb;
10401 uint32_t alloc_len, data_len;
10403 cdb = (struct scsi_mechanism_status *)ctsio->cdb;
10404 alloc_len = scsi_2btoul(cdb->length);
10406 data_len = sizeof(struct scsi_mechanism_status_header);
10407 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10408 ctsio->kern_sg_entries = 0;
10409 ctsio->kern_rel_offset = 0;
10410 ctsio->kern_data_len = min(data_len, alloc_len);
10411 ctsio->kern_total_len = ctsio->kern_data_len;
10413 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr;
10414 hdr->state1 = 0x00;
10415 hdr->state2 = 0xe0;
10416 scsi_ulto3b(0, hdr->lba);
10417 hdr->slots_num = 0;
10418 scsi_ulto2b(0, hdr->slots_length);
10420 ctl_set_success(ctsio);
10421 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10422 ctsio->be_move_done = ctl_config_move_done;
10423 ctl_datamove((union ctl_io *)ctsio);
10424 return (CTL_RETVAL_COMPLETE);
10428 ctl_ultomsf(uint32_t lba, uint8_t *buf)
10433 buf[1] = bin2bcd((lba / 75) / 60);
10434 buf[2] = bin2bcd((lba / 75) % 60);
10435 buf[3] = bin2bcd(lba % 75);
10439 ctl_read_toc(struct ctl_scsiio *ctsio)
10441 struct ctl_lun *lun = CTL_LUN(ctsio);
10442 struct scsi_read_toc_hdr *hdr;
10443 struct scsi_read_toc_type01_descr *descr;
10444 struct scsi_read_toc *cdb;
10445 uint32_t alloc_len, data_len;
10448 cdb = (struct scsi_read_toc *)ctsio->cdb;
10449 msf = (cdb->byte2 & CD_MSF) != 0;
10450 format = cdb->format;
10451 alloc_len = scsi_2btoul(cdb->data_len);
10453 data_len = sizeof(struct scsi_read_toc_hdr);
10455 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr);
10457 data_len += sizeof(struct scsi_read_toc_type01_descr);
10458 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10459 ctsio->kern_sg_entries = 0;
10460 ctsio->kern_rel_offset = 0;
10461 ctsio->kern_data_len = min(data_len, alloc_len);
10462 ctsio->kern_total_len = ctsio->kern_data_len;
10464 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr;
10466 scsi_ulto2b(0x12, hdr->data_length);
10469 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10470 descr->addr_ctl = 0x14;
10471 descr->track_number = 1;
10473 ctl_ultomsf(0, descr->track_start);
10475 scsi_ulto4b(0, descr->track_start);
10477 descr->addr_ctl = 0x14;
10478 descr->track_number = 0xaa;
10480 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start);
10482 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start);
10484 scsi_ulto2b(0x0a, hdr->data_length);
10487 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10488 descr->addr_ctl = 0x14;
10489 descr->track_number = 1;
10491 ctl_ultomsf(0, descr->track_start);
10493 scsi_ulto4b(0, descr->track_start);
10496 ctl_set_success(ctsio);
10497 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10498 ctsio->be_move_done = ctl_config_move_done;
10499 ctl_datamove((union ctl_io *)ctsio);
10500 return (CTL_RETVAL_COMPLETE);
10504 * For known CDB types, parse the LBA and length.
10507 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10509 if (io->io_hdr.io_type != CTL_IO_SCSI)
10512 switch (io->scsiio.cdb[0]) {
10513 case COMPARE_AND_WRITE: {
10514 struct scsi_compare_and_write *cdb;
10516 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10518 *lba = scsi_8btou64(cdb->addr);
10519 *len = cdb->length;
10524 struct scsi_rw_6 *cdb;
10526 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10528 *lba = scsi_3btoul(cdb->addr);
10529 /* only 5 bits are valid in the most significant address byte */
10531 *len = cdb->length;
10536 struct scsi_rw_10 *cdb;
10538 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10540 *lba = scsi_4btoul(cdb->addr);
10541 *len = scsi_2btoul(cdb->length);
10544 case WRITE_VERIFY_10: {
10545 struct scsi_write_verify_10 *cdb;
10547 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10549 *lba = scsi_4btoul(cdb->addr);
10550 *len = scsi_2btoul(cdb->length);
10555 struct scsi_rw_12 *cdb;
10557 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10559 *lba = scsi_4btoul(cdb->addr);
10560 *len = scsi_4btoul(cdb->length);
10563 case WRITE_VERIFY_12: {
10564 struct scsi_write_verify_12 *cdb;
10566 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10568 *lba = scsi_4btoul(cdb->addr);
10569 *len = scsi_4btoul(cdb->length);
10574 struct scsi_rw_16 *cdb;
10576 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10578 *lba = scsi_8btou64(cdb->addr);
10579 *len = scsi_4btoul(cdb->length);
10582 case WRITE_ATOMIC_16: {
10583 struct scsi_write_atomic_16 *cdb;
10585 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb;
10587 *lba = scsi_8btou64(cdb->addr);
10588 *len = scsi_2btoul(cdb->length);
10591 case WRITE_VERIFY_16: {
10592 struct scsi_write_verify_16 *cdb;
10594 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10596 *lba = scsi_8btou64(cdb->addr);
10597 *len = scsi_4btoul(cdb->length);
10600 case WRITE_SAME_10: {
10601 struct scsi_write_same_10 *cdb;
10603 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10605 *lba = scsi_4btoul(cdb->addr);
10606 *len = scsi_2btoul(cdb->length);
10609 case WRITE_SAME_16: {
10610 struct scsi_write_same_16 *cdb;
10612 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10614 *lba = scsi_8btou64(cdb->addr);
10615 *len = scsi_4btoul(cdb->length);
10619 struct scsi_verify_10 *cdb;
10621 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10623 *lba = scsi_4btoul(cdb->addr);
10624 *len = scsi_2btoul(cdb->length);
10628 struct scsi_verify_12 *cdb;
10630 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10632 *lba = scsi_4btoul(cdb->addr);
10633 *len = scsi_4btoul(cdb->length);
10637 struct scsi_verify_16 *cdb;
10639 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10641 *lba = scsi_8btou64(cdb->addr);
10642 *len = scsi_4btoul(cdb->length);
10650 case SERVICE_ACTION_IN: { /* GET LBA STATUS */
10651 struct scsi_get_lba_status *cdb;
10653 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10654 *lba = scsi_8btou64(cdb->addr);
10660 break; /* NOTREACHED */
10667 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10670 uint64_t endlba1, endlba2;
10672 endlba1 = lba1 + len1 - (seq ? 0 : 1);
10673 endlba2 = lba2 + len2 - 1;
10675 if ((endlba1 < lba2) || (endlba2 < lba1))
10676 return (CTL_ACTION_PASS);
10678 return (CTL_ACTION_BLOCK);
10682 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10684 struct ctl_ptr_len_flags *ptrlen;
10685 struct scsi_unmap_desc *buf, *end, *range;
10689 /* If not UNMAP -- go other way. */
10690 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10691 io->scsiio.cdb[0] != UNMAP)
10692 return (CTL_ACTION_ERROR);
10694 /* If UNMAP without data -- block and wait for data. */
10695 ptrlen = (struct ctl_ptr_len_flags *)
10696 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10697 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10698 ptrlen->ptr == NULL)
10699 return (CTL_ACTION_BLOCK);
10701 /* UNMAP with data -- check for collision. */
10702 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10703 end = buf + ptrlen->len / sizeof(*buf);
10704 for (range = buf; range < end; range++) {
10705 lba = scsi_8btou64(range->lba);
10706 len = scsi_4btoul(range->length);
10707 if ((lba < lba2 + len2) && (lba + len > lba2))
10708 return (CTL_ACTION_BLOCK);
10710 return (CTL_ACTION_PASS);
10714 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10716 uint64_t lba1, lba2;
10717 uint64_t len1, len2;
10720 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10721 return (CTL_ACTION_ERROR);
10723 retval = ctl_extent_check_unmap(io1, lba2, len2);
10724 if (retval != CTL_ACTION_ERROR)
10727 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10728 return (CTL_ACTION_ERROR);
10730 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10732 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
10736 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2)
10738 uint64_t lba1, lba2;
10739 uint64_t len1, len2;
10741 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10742 return (CTL_ACTION_PASS);
10743 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10744 return (CTL_ACTION_ERROR);
10745 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10746 return (CTL_ACTION_ERROR);
10748 if (lba1 + len1 == lba2)
10749 return (CTL_ACTION_BLOCK);
10750 return (CTL_ACTION_PASS);
10754 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10755 union ctl_io *ooa_io)
10757 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10758 const ctl_serialize_action *serialize_row;
10761 * The initiator attempted multiple untagged commands at the same
10762 * time. Can't do that.
10764 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10765 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10766 && ((pending_io->io_hdr.nexus.targ_port ==
10767 ooa_io->io_hdr.nexus.targ_port)
10768 && (pending_io->io_hdr.nexus.initid ==
10769 ooa_io->io_hdr.nexus.initid))
10770 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10771 CTL_FLAG_STATUS_SENT)) == 0))
10772 return (CTL_ACTION_OVERLAP);
10775 * The initiator attempted to send multiple tagged commands with
10776 * the same ID. (It's fine if different initiators have the same
10779 * Even if all of those conditions are true, we don't kill the I/O
10780 * if the command ahead of us has been aborted. We won't end up
10781 * sending it to the FETD, and it's perfectly legal to resend a
10782 * command with the same tag number as long as the previous
10783 * instance of this tag number has been aborted somehow.
10785 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10786 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10787 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10788 && ((pending_io->io_hdr.nexus.targ_port ==
10789 ooa_io->io_hdr.nexus.targ_port)
10790 && (pending_io->io_hdr.nexus.initid ==
10791 ooa_io->io_hdr.nexus.initid))
10792 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10793 CTL_FLAG_STATUS_SENT)) == 0))
10794 return (CTL_ACTION_OVERLAP_TAG);
10797 * If we get a head of queue tag, SAM-3 says that we should
10798 * immediately execute it.
10800 * What happens if this command would normally block for some other
10801 * reason? e.g. a request sense with a head of queue tag
10802 * immediately after a write. Normally that would block, but this
10803 * will result in its getting executed immediately...
10805 * We currently return "pass" instead of "skip", so we'll end up
10806 * going through the rest of the queue to check for overlapped tags.
10808 * XXX KDM check for other types of blockage first??
10810 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10811 return (CTL_ACTION_PASS);
10814 * Ordered tags have to block until all items ahead of them
10815 * have completed. If we get called with an ordered tag, we always
10816 * block, if something else is ahead of us in the queue.
10818 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10819 return (CTL_ACTION_BLOCK);
10822 * Simple tags get blocked until all head of queue and ordered tags
10823 * ahead of them have completed. I'm lumping untagged commands in
10824 * with simple tags here. XXX KDM is that the right thing to do?
10826 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10827 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10828 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10829 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10830 return (CTL_ACTION_BLOCK);
10832 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
10833 KASSERT(pending_entry->seridx < CTL_SERIDX_COUNT,
10834 ("%s: Invalid seridx %d for pending CDB %02x %02x @ %p",
10835 __func__, pending_entry->seridx, pending_io->scsiio.cdb[0],
10836 pending_io->scsiio.cdb[1], pending_io));
10837 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
10838 if (ooa_entry->seridx == CTL_SERIDX_INVLD)
10839 return (CTL_ACTION_PASS); /* Unsupported command in OOA queue */
10840 KASSERT(ooa_entry->seridx < CTL_SERIDX_COUNT,
10841 ("%s: Invalid seridx %d for ooa CDB %02x %02x @ %p",
10842 __func__, ooa_entry->seridx, ooa_io->scsiio.cdb[0],
10843 ooa_io->scsiio.cdb[1], ooa_io));
10845 serialize_row = ctl_serialize_table[ooa_entry->seridx];
10847 switch (serialize_row[pending_entry->seridx]) {
10848 case CTL_SER_BLOCK:
10849 return (CTL_ACTION_BLOCK);
10850 case CTL_SER_EXTENT:
10851 return (ctl_extent_check(ooa_io, pending_io,
10852 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10853 case CTL_SER_EXTENTOPT:
10854 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
10855 SCP_QUEUE_ALG_UNRESTRICTED)
10856 return (ctl_extent_check(ooa_io, pending_io,
10858 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10859 return (CTL_ACTION_PASS);
10860 case CTL_SER_EXTENTSEQ:
10861 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF)
10862 return (ctl_extent_check_seq(ooa_io, pending_io));
10863 return (CTL_ACTION_PASS);
10865 return (CTL_ACTION_PASS);
10866 case CTL_SER_BLOCKOPT:
10867 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
10868 SCP_QUEUE_ALG_UNRESTRICTED)
10869 return (CTL_ACTION_BLOCK);
10870 return (CTL_ACTION_PASS);
10872 return (CTL_ACTION_SKIP);
10874 panic("%s: Invalid serialization value %d for %d => %d",
10875 __func__, serialize_row[pending_entry->seridx],
10876 pending_entry->seridx, ooa_entry->seridx);
10879 return (CTL_ACTION_ERROR);
10883 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
10885 * - pending_io is generally either incoming, or on the blocked queue
10886 * - starting I/O is the I/O we want to start the check with.
10889 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
10890 union ctl_io *starting_io)
10892 union ctl_io *ooa_io;
10895 mtx_assert(&lun->lun_lock, MA_OWNED);
10898 * Run back along the OOA queue, starting with the current
10899 * blocked I/O and going through every I/O before it on the
10900 * queue. If starting_io is NULL, we'll just end up returning
10903 for (ooa_io = starting_io; ooa_io != NULL;
10904 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
10908 * This routine just checks to see whether
10909 * cur_blocked is blocked by ooa_io, which is ahead
10910 * of it in the queue. It doesn't queue/dequeue
10913 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
10915 case CTL_ACTION_BLOCK:
10916 case CTL_ACTION_OVERLAP:
10917 case CTL_ACTION_OVERLAP_TAG:
10918 case CTL_ACTION_SKIP:
10919 case CTL_ACTION_ERROR:
10921 break; /* NOTREACHED */
10922 case CTL_ACTION_PASS:
10925 panic("%s: Invalid action %d\n", __func__, action);
10929 return (CTL_ACTION_PASS);
10934 * - An I/O has just completed, and has been removed from the per-LUN OOA
10935 * queue, so some items on the blocked queue may now be unblocked.
10938 ctl_check_blocked(struct ctl_lun *lun)
10940 struct ctl_softc *softc = lun->ctl_softc;
10941 union ctl_io *cur_blocked, *next_blocked;
10943 mtx_assert(&lun->lun_lock, MA_OWNED);
10946 * Run forward from the head of the blocked queue, checking each
10947 * entry against the I/Os prior to it on the OOA queue to see if
10948 * there is still any blockage.
10950 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
10951 * with our removing a variable on it while it is traversing the
10954 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
10955 cur_blocked != NULL; cur_blocked = next_blocked) {
10956 union ctl_io *prev_ooa;
10959 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
10962 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
10963 ctl_ooaq, ooa_links);
10966 * If cur_blocked happens to be the first item in the OOA
10967 * queue now, prev_ooa will be NULL, and the action
10968 * returned will just be CTL_ACTION_PASS.
10970 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
10973 case CTL_ACTION_BLOCK:
10974 /* Nothing to do here, still blocked */
10976 case CTL_ACTION_OVERLAP:
10977 case CTL_ACTION_OVERLAP_TAG:
10979 * This shouldn't happen! In theory we've already
10980 * checked this command for overlap...
10983 case CTL_ACTION_PASS:
10984 case CTL_ACTION_SKIP: {
10985 const struct ctl_cmd_entry *entry;
10988 * The skip case shouldn't happen, this transaction
10989 * should have never made it onto the blocked queue.
10992 * This I/O is no longer blocked, we can remove it
10993 * from the blocked queue. Since this is a TAILQ
10994 * (doubly linked list), we can do O(1) removals
10995 * from any place on the list.
10997 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
10999 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11001 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
11002 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){
11004 * Need to send IO back to original side to
11007 union ctl_ha_msg msg_info;
11009 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11010 msg_info.hdr.original_sc =
11011 cur_blocked->io_hdr.original_sc;
11012 msg_info.hdr.serializing_sc = cur_blocked;
11013 msg_info.hdr.msg_type = CTL_MSG_R2R;
11014 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11015 sizeof(msg_info.hdr), M_NOWAIT);
11018 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
11021 * Check this I/O for LUN state changes that may
11022 * have happened while this command was blocked.
11023 * The LUN state may have been changed by a command
11024 * ahead of us in the queue, so we need to re-check
11025 * for any states that can be caused by SCSI
11028 if (ctl_scsiio_lun_check(lun, entry,
11029 &cur_blocked->scsiio) == 0) {
11030 cur_blocked->io_hdr.flags |=
11031 CTL_FLAG_IS_WAS_ON_RTR;
11032 ctl_enqueue_rtr(cur_blocked);
11034 ctl_done(cur_blocked);
11039 * This probably shouldn't happen -- we shouldn't
11040 * get CTL_ACTION_ERROR, or anything else.
11046 return (CTL_RETVAL_COMPLETE);
11050 * This routine (with one exception) checks LUN flags that can be set by
11051 * commands ahead of us in the OOA queue. These flags have to be checked
11052 * when a command initially comes in, and when we pull a command off the
11053 * blocked queue and are preparing to execute it. The reason we have to
11054 * check these flags for commands on the blocked queue is that the LUN
11055 * state may have been changed by a command ahead of us while we're on the
11058 * Ordering is somewhat important with these checks, so please pay
11059 * careful attention to the placement of any new checks.
11062 ctl_scsiio_lun_check(struct ctl_lun *lun,
11063 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11065 struct ctl_softc *softc = lun->ctl_softc;
11071 mtx_assert(&lun->lun_lock, MA_OWNED);
11074 * If this shelf is a secondary shelf controller, we may have to
11075 * reject some commands disallowed by HA mode and link state.
11077 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11078 if (softc->ha_link == CTL_HA_LINK_OFFLINE &&
11079 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11080 ctl_set_lun_unavail(ctsio);
11084 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 &&
11085 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11086 ctl_set_lun_transit(ctsio);
11090 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY &&
11091 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) {
11092 ctl_set_lun_standby(ctsio);
11097 /* The rest of checks are only done on executing side */
11098 if (softc->ha_mode == CTL_HA_MODE_XFER)
11102 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11104 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) {
11105 ctl_set_hw_write_protected(ctsio);
11109 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) {
11110 ctl_set_sense(ctsio, /*current_error*/ 1,
11111 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11112 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11119 * Check for a reservation conflict. If this command isn't allowed
11120 * even on reserved LUNs, and if this initiator isn't the one who
11121 * reserved us, reject the command with a reservation conflict.
11123 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11124 if ((lun->flags & CTL_LUN_RESERVED)
11125 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11126 if (lun->res_idx != residx) {
11127 ctl_set_reservation_conflict(ctsio);
11133 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11134 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11135 /* No reservation or command is allowed. */;
11136 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11137 (lun->pr_res_type == SPR_TYPE_WR_EX ||
11138 lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
11139 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) {
11140 /* The command is allowed for Write Exclusive resv. */;
11143 * if we aren't registered or it's a res holder type
11144 * reservation and this isn't the res holder then set a
11147 if (ctl_get_prkey(lun, residx) == 0 ||
11148 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) {
11149 ctl_set_reservation_conflict(ctsio);
11155 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) {
11156 if (lun->flags & CTL_LUN_EJECTED)
11157 ctl_set_lun_ejected(ctsio);
11158 else if (lun->flags & CTL_LUN_NO_MEDIA) {
11159 if (lun->flags & CTL_LUN_REMOVABLE)
11160 ctl_set_lun_no_media(ctsio);
11162 ctl_set_lun_int_reqd(ctsio);
11163 } else if (lun->flags & CTL_LUN_STOPPED)
11164 ctl_set_lun_stopped(ctsio);
11176 ctl_failover_io(union ctl_io *io, int have_lock)
11178 ctl_set_busy(&io->scsiio);
11183 ctl_failover_lun(union ctl_io *rio)
11185 struct ctl_softc *softc = CTL_SOFTC(rio);
11186 struct ctl_lun *lun;
11187 struct ctl_io_hdr *io, *next_io;
11190 targ_lun = rio->io_hdr.nexus.targ_mapped_lun;
11191 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun));
11193 /* Find and lock the LUN. */
11194 mtx_lock(&softc->ctl_lock);
11195 if (targ_lun > CTL_MAX_LUNS ||
11196 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11197 mtx_unlock(&softc->ctl_lock);
11200 mtx_lock(&lun->lun_lock);
11201 mtx_unlock(&softc->ctl_lock);
11202 if (lun->flags & CTL_LUN_DISABLED) {
11203 mtx_unlock(&lun->lun_lock);
11207 if (softc->ha_mode == CTL_HA_MODE_XFER) {
11208 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11209 /* We are master */
11210 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11211 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11212 io->flags |= CTL_FLAG_ABORT;
11213 io->flags |= CTL_FLAG_FAILOVER;
11214 } else { /* This can be only due to DATAMOVE */
11215 io->msg_type = CTL_MSG_DATAMOVE_DONE;
11216 io->flags &= ~CTL_FLAG_DMA_INPROG;
11217 io->flags |= CTL_FLAG_IO_ACTIVE;
11218 io->port_status = 31340;
11219 ctl_enqueue_isc((union ctl_io *)io);
11223 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11224 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11225 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11226 io->flags |= CTL_FLAG_FAILOVER;
11228 ctl_set_busy(&((union ctl_io *)io)->
11230 ctl_done((union ctl_io *)io);
11234 } else { /* SERIALIZE modes */
11235 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links,
11237 /* We are master */
11238 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11239 TAILQ_REMOVE(&lun->blocked_queue, io,
11241 io->flags &= ~CTL_FLAG_BLOCKED;
11242 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11243 ctl_free_io((union ctl_io *)io);
11246 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11247 /* We are master */
11248 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11249 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11250 ctl_free_io((union ctl_io *)io);
11253 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11254 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11255 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) {
11256 ctl_set_busy(&((union ctl_io *)io)->
11258 ctl_done((union ctl_io *)io);
11262 ctl_check_blocked(lun);
11264 mtx_unlock(&lun->lun_lock);
11268 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio)
11270 struct ctl_lun *lun;
11271 const struct ctl_cmd_entry *entry;
11272 uint32_t initidx, targ_lun;
11276 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11277 if (targ_lun < CTL_MAX_LUNS)
11278 lun = softc->ctl_luns[targ_lun];
11281 * If the LUN is invalid, pretend that it doesn't exist.
11282 * It will go away as soon as all pending I/O has been
11285 mtx_lock(&lun->lun_lock);
11286 if (lun->flags & CTL_LUN_DISABLED) {
11287 mtx_unlock(&lun->lun_lock);
11291 CTL_LUN(ctsio) = lun;
11293 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
11296 * Every I/O goes into the OOA queue for a particular LUN,
11297 * and stays there until completion.
11300 if (TAILQ_EMPTY(&lun->ooa_queue))
11301 lun->idle_time += getsbinuptime() - lun->last_busy;
11303 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
11306 /* Get command entry and return error if it is unsuppotyed. */
11307 entry = ctl_validate_command(ctsio);
11308 if (entry == NULL) {
11310 mtx_unlock(&lun->lun_lock);
11314 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11315 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11318 * Check to see whether we can send this command to LUNs that don't
11319 * exist. This should pretty much only be the case for inquiry
11320 * and request sense. Further checks, below, really require having
11321 * a LUN, so we can't really check the command anymore. Just put
11322 * it on the rtr queue.
11325 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) {
11326 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11327 ctl_enqueue_rtr((union ctl_io *)ctsio);
11331 ctl_set_unsupported_lun(ctsio);
11332 ctl_done((union ctl_io *)ctsio);
11333 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11337 * Make sure we support this particular command on this LUN.
11338 * e.g., we don't support writes to the control LUN.
11340 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11341 mtx_unlock(&lun->lun_lock);
11342 ctl_set_invalid_opcode(ctsio);
11343 ctl_done((union ctl_io *)ctsio);
11348 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11351 * If we've got a request sense, it'll clear the contingent
11352 * allegiance condition. Otherwise, if we have a CA condition for
11353 * this initiator, clear it, because it sent down a command other
11354 * than request sense.
11356 if (ctsio->cdb[0] != REQUEST_SENSE) {
11357 struct scsi_sense_data *ps;
11359 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT];
11361 ps[initidx % CTL_MAX_INIT_PER_PORT].error_code = 0;
11365 * If the command has this flag set, it handles its own unit
11366 * attention reporting, we shouldn't do anything. Otherwise we
11367 * check for any pending unit attentions, and send them back to the
11368 * initiator. We only do this when a command initially comes in,
11369 * not when we pull it off the blocked queue.
11371 * According to SAM-3, section 5.3.2, the order that things get
11372 * presented back to the host is basically unit attentions caused
11373 * by some sort of reset event, busy status, reservation conflicts
11374 * or task set full, and finally any other status.
11376 * One issue here is that some of the unit attentions we report
11377 * don't fall into the "reset" category (e.g. "reported luns data
11378 * has changed"). So reporting it here, before the reservation
11379 * check, may be technically wrong. I guess the only thing to do
11380 * would be to check for and report the reset events here, and then
11381 * check for the other unit attention types after we check for a
11382 * reservation conflict.
11384 * XXX KDM need to fix this
11386 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11387 ctl_ua_type ua_type;
11388 u_int sense_len = 0;
11390 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11391 &sense_len, SSD_TYPE_NONE);
11392 if (ua_type != CTL_UA_NONE) {
11393 mtx_unlock(&lun->lun_lock);
11394 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11395 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11396 ctsio->sense_len = sense_len;
11397 ctl_done((union ctl_io *)ctsio);
11403 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11404 mtx_unlock(&lun->lun_lock);
11405 ctl_done((union ctl_io *)ctsio);
11410 * XXX CHD this is where we want to send IO to other side if
11411 * this LUN is secondary on this SC. We will need to make a copy
11412 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11413 * the copy we send as FROM_OTHER.
11414 * We also need to stuff the address of the original IO so we can
11415 * find it easily. Something similar will need be done on the other
11416 * side so when we are done we can find the copy.
11418 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
11419 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 &&
11420 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) {
11421 union ctl_ha_msg msg_info;
11424 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11425 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11426 mtx_unlock(&lun->lun_lock);
11428 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11429 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11430 msg_info.hdr.serializing_sc = NULL;
11431 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11432 msg_info.scsi.tag_num = ctsio->tag_num;
11433 msg_info.scsi.tag_type = ctsio->tag_type;
11434 msg_info.scsi.cdb_len = ctsio->cdb_len;
11435 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11437 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11438 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data),
11439 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) {
11440 ctl_set_busy(ctsio);
11441 ctl_done((union ctl_io *)ctsio);
11447 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11448 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11449 ctl_ooaq, ooa_links))) {
11450 case CTL_ACTION_BLOCK:
11451 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11452 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11454 mtx_unlock(&lun->lun_lock);
11456 case CTL_ACTION_PASS:
11457 case CTL_ACTION_SKIP:
11458 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11459 mtx_unlock(&lun->lun_lock);
11460 ctl_enqueue_rtr((union ctl_io *)ctsio);
11462 case CTL_ACTION_OVERLAP:
11463 mtx_unlock(&lun->lun_lock);
11464 ctl_set_overlapped_cmd(ctsio);
11465 ctl_done((union ctl_io *)ctsio);
11467 case CTL_ACTION_OVERLAP_TAG:
11468 mtx_unlock(&lun->lun_lock);
11469 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11470 ctl_done((union ctl_io *)ctsio);
11472 case CTL_ACTION_ERROR:
11474 mtx_unlock(&lun->lun_lock);
11475 ctl_set_internal_failure(ctsio,
11477 /*retry_count*/ 0);
11478 ctl_done((union ctl_io *)ctsio);
11484 const struct ctl_cmd_entry *
11485 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11487 const struct ctl_cmd_entry *entry;
11488 int service_action;
11490 entry = &ctl_cmd_table[ctsio->cdb[0]];
11492 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11493 if (entry->flags & CTL_CMD_FLAG_SA5) {
11494 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11495 entry = &((const struct ctl_cmd_entry *)
11496 entry->execute)[service_action];
11501 const struct ctl_cmd_entry *
11502 ctl_validate_command(struct ctl_scsiio *ctsio)
11504 const struct ctl_cmd_entry *entry;
11508 entry = ctl_get_cmd_entry(ctsio, &sa);
11509 if (entry->execute == NULL) {
11511 ctl_set_invalid_field(ctsio,
11518 ctl_set_invalid_opcode(ctsio);
11519 ctl_done((union ctl_io *)ctsio);
11522 KASSERT(entry->length > 0,
11523 ("Not defined length for command 0x%02x/0x%02x",
11524 ctsio->cdb[0], ctsio->cdb[1]));
11525 for (i = 1; i < entry->length; i++) {
11526 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11529 ctl_set_invalid_field(ctsio,
11534 /*bit*/ fls(diff) - 1);
11535 ctl_done((union ctl_io *)ctsio);
11542 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11545 switch (lun_type) {
11547 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0)
11551 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
11555 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0)
11565 ctl_scsiio(struct ctl_scsiio *ctsio)
11568 const struct ctl_cmd_entry *entry;
11570 retval = CTL_RETVAL_COMPLETE;
11572 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11574 entry = ctl_get_cmd_entry(ctsio, NULL);
11577 * If this I/O has been aborted, just send it straight to
11578 * ctl_done() without executing it.
11580 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11581 ctl_done((union ctl_io *)ctsio);
11586 * All the checks should have been handled by ctl_scsiio_precheck().
11587 * We should be clear now to just execute the I/O.
11589 retval = entry->execute(ctsio);
11596 ctl_target_reset(union ctl_io *io)
11598 struct ctl_softc *softc = CTL_SOFTC(io);
11599 struct ctl_port *port = CTL_PORT(io);
11600 struct ctl_lun *lun;
11602 ctl_ua_type ua_type;
11604 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11605 union ctl_ha_msg msg_info;
11607 msg_info.hdr.nexus = io->io_hdr.nexus;
11608 msg_info.task.task_action = io->taskio.task_action;
11609 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11610 msg_info.hdr.original_sc = NULL;
11611 msg_info.hdr.serializing_sc = NULL;
11612 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11613 sizeof(msg_info.task), M_WAITOK);
11616 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11617 if (io->taskio.task_action == CTL_TASK_TARGET_RESET)
11618 ua_type = CTL_UA_TARG_RESET;
11620 ua_type = CTL_UA_BUS_RESET;
11621 mtx_lock(&softc->ctl_lock);
11622 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11623 if (port != NULL &&
11624 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
11626 ctl_do_lun_reset(lun, initidx, ua_type);
11628 mtx_unlock(&softc->ctl_lock);
11629 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11634 * The LUN should always be set. The I/O is optional, and is used to
11635 * distinguish between I/Os sent by this initiator, and by other
11636 * initiators. We set unit attention for initiators other than this one.
11637 * SAM-3 is vague on this point. It does say that a unit attention should
11638 * be established for other initiators when a LUN is reset (see section
11639 * 5.7.3), but it doesn't specifically say that the unit attention should
11640 * be established for this particular initiator when a LUN is reset. Here
11641 * is the relevant text, from SAM-3 rev 8:
11643 * 5.7.2 When a SCSI initiator port aborts its own tasks
11645 * When a SCSI initiator port causes its own task(s) to be aborted, no
11646 * notification that the task(s) have been aborted shall be returned to
11647 * the SCSI initiator port other than the completion response for the
11648 * command or task management function action that caused the task(s) to
11649 * be aborted and notification(s) associated with related effects of the
11650 * action (e.g., a reset unit attention condition).
11652 * XXX KDM for now, we're setting unit attention for all initiators.
11655 ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua_type)
11660 mtx_lock(&lun->lun_lock);
11662 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11663 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11664 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11667 for (i = 0; i < CTL_MAX_PORTS; i++) {
11668 free(lun->pending_sense[i], M_CTL);
11669 lun->pending_sense[i] = NULL;
11671 /* Clear reservation. */
11672 lun->flags &= ~CTL_LUN_RESERVED;
11673 /* Clear prevent media removal. */
11674 if (lun->prevent) {
11675 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11676 ctl_clear_mask(lun->prevent, i);
11677 lun->prevent_count = 0;
11679 /* Clear TPC status */
11680 ctl_tpc_lun_clear(lun, -1);
11681 /* Establish UA. */
11683 ctl_est_ua_all(lun, initidx, ua_type);
11685 ctl_est_ua_all(lun, -1, ua_type);
11687 mtx_unlock(&lun->lun_lock);
11691 ctl_lun_reset(union ctl_io *io)
11693 struct ctl_softc *softc = CTL_SOFTC(io);
11694 struct ctl_lun *lun;
11695 uint32_t targ_lun, initidx;
11697 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11698 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11699 mtx_lock(&softc->ctl_lock);
11700 if (targ_lun >= CTL_MAX_LUNS ||
11701 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11702 mtx_unlock(&softc->ctl_lock);
11703 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11706 ctl_do_lun_reset(lun, initidx, CTL_UA_LUN_RESET);
11707 mtx_unlock(&softc->ctl_lock);
11708 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11710 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) {
11711 union ctl_ha_msg msg_info;
11713 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11714 msg_info.hdr.nexus = io->io_hdr.nexus;
11715 msg_info.task.task_action = CTL_TASK_LUN_RESET;
11716 msg_info.hdr.original_sc = NULL;
11717 msg_info.hdr.serializing_sc = NULL;
11718 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11719 sizeof(msg_info.task), M_WAITOK);
11725 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11730 mtx_assert(&lun->lun_lock, MA_OWNED);
11733 * Run through the OOA queue and attempt to find the given I/O.
11734 * The target port, initiator ID, tag type and tag number have to
11735 * match the values that we got from the initiator. If we have an
11736 * untagged command to abort, simply abort the first untagged command
11737 * we come to. We only allow one untagged command at a time of course.
11739 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11740 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11742 if ((targ_port == UINT32_MAX ||
11743 targ_port == xio->io_hdr.nexus.targ_port) &&
11744 (init_id == UINT32_MAX ||
11745 init_id == xio->io_hdr.nexus.initid)) {
11746 if (targ_port != xio->io_hdr.nexus.targ_port ||
11747 init_id != xio->io_hdr.nexus.initid)
11748 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
11749 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11750 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11751 union ctl_ha_msg msg_info;
11753 msg_info.hdr.nexus = xio->io_hdr.nexus;
11754 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11755 msg_info.task.tag_num = xio->scsiio.tag_num;
11756 msg_info.task.tag_type = xio->scsiio.tag_type;
11757 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11758 msg_info.hdr.original_sc = NULL;
11759 msg_info.hdr.serializing_sc = NULL;
11760 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11761 sizeof(msg_info.task), M_NOWAIT);
11768 ctl_abort_task_set(union ctl_io *io)
11770 struct ctl_softc *softc = CTL_SOFTC(io);
11771 struct ctl_lun *lun;
11777 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11778 mtx_lock(&softc->ctl_lock);
11779 if (targ_lun >= CTL_MAX_LUNS ||
11780 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11781 mtx_unlock(&softc->ctl_lock);
11782 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11786 mtx_lock(&lun->lun_lock);
11787 mtx_unlock(&softc->ctl_lock);
11788 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
11789 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11790 io->io_hdr.nexus.initid,
11791 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11792 } else { /* CTL_TASK_CLEAR_TASK_SET */
11793 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
11794 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11796 mtx_unlock(&lun->lun_lock);
11797 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11802 ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx,
11803 ctl_ua_type ua_type)
11805 struct ctl_lun *lun;
11806 struct scsi_sense_data *ps;
11809 p = initidx / CTL_MAX_INIT_PER_PORT;
11810 i = initidx % CTL_MAX_INIT_PER_PORT;
11811 mtx_lock(&softc->ctl_lock);
11812 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11813 mtx_lock(&lun->lun_lock);
11815 ctl_abort_tasks_lun(lun, p, i, 1);
11817 ps = lun->pending_sense[p];
11819 ps[i].error_code = 0;
11820 /* Clear reservation. */
11821 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx))
11822 lun->flags &= ~CTL_LUN_RESERVED;
11823 /* Clear prevent media removal. */
11824 if (lun->prevent && ctl_is_set(lun->prevent, initidx)) {
11825 ctl_clear_mask(lun->prevent, initidx);
11826 lun->prevent_count--;
11828 /* Clear TPC status */
11829 ctl_tpc_lun_clear(lun, initidx);
11830 /* Establish UA. */
11831 ctl_est_ua(lun, initidx, ua_type);
11832 mtx_unlock(&lun->lun_lock);
11834 mtx_unlock(&softc->ctl_lock);
11838 ctl_i_t_nexus_reset(union ctl_io *io)
11840 struct ctl_softc *softc = CTL_SOFTC(io);
11843 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11844 union ctl_ha_msg msg_info;
11846 msg_info.hdr.nexus = io->io_hdr.nexus;
11847 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET;
11848 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11849 msg_info.hdr.original_sc = NULL;
11850 msg_info.hdr.serializing_sc = NULL;
11851 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11852 sizeof(msg_info.task), M_WAITOK);
11855 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11856 ctl_i_t_nexus_loss(softc, initidx, CTL_UA_I_T_NEXUS_LOSS);
11857 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11862 ctl_abort_task(union ctl_io *io)
11864 struct ctl_softc *softc = CTL_SOFTC(io);
11866 struct ctl_lun *lun;
11869 char printbuf[128];
11879 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11880 mtx_lock(&softc->ctl_lock);
11881 if (targ_lun >= CTL_MAX_LUNS ||
11882 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11883 mtx_unlock(&softc->ctl_lock);
11884 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11889 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
11890 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
11893 mtx_lock(&lun->lun_lock);
11894 mtx_unlock(&softc->ctl_lock);
11896 * Run through the OOA queue and attempt to find the given I/O.
11897 * The target port, initiator ID, tag type and tag number have to
11898 * match the values that we got from the initiator. If we have an
11899 * untagged command to abort, simply abort the first untagged command
11900 * we come to. We only allow one untagged command at a time of course.
11902 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11903 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11905 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
11907 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
11908 lun->lun, xio->scsiio.tag_num,
11909 xio->scsiio.tag_type,
11910 (xio->io_hdr.blocked_links.tqe_prev
11911 == NULL) ? "" : " BLOCKED",
11912 (xio->io_hdr.flags &
11913 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
11914 (xio->io_hdr.flags &
11915 CTL_FLAG_ABORT) ? " ABORT" : "",
11916 (xio->io_hdr.flags &
11917 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
11918 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
11920 printf("%s\n", sbuf_data(&sb));
11923 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
11924 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
11925 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
11929 * If the abort says that the task is untagged, the
11930 * task in the queue must be untagged. Otherwise,
11931 * we just check to see whether the tag numbers
11932 * match. This is because the QLogic firmware
11933 * doesn't pass back the tag type in an abort
11937 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
11938 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
11939 || (xio->scsiio.tag_num == io->taskio.tag_num))
11942 * XXX KDM we've got problems with FC, because it
11943 * doesn't send down a tag type with aborts. So we
11944 * can only really go by the tag number...
11945 * This may cause problems with parallel SCSI.
11946 * Need to figure that out!!
11948 if (xio->scsiio.tag_num == io->taskio.tag_num) {
11949 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11951 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
11952 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11953 union ctl_ha_msg msg_info;
11955 msg_info.hdr.nexus = io->io_hdr.nexus;
11956 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11957 msg_info.task.tag_num = io->taskio.tag_num;
11958 msg_info.task.tag_type = io->taskio.tag_type;
11959 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11960 msg_info.hdr.original_sc = NULL;
11961 msg_info.hdr.serializing_sc = NULL;
11963 printf("Sent Abort to other side\n");
11965 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11966 sizeof(msg_info.task), M_NOWAIT);
11969 printf("ctl_abort_task: found I/O to abort\n");
11973 mtx_unlock(&lun->lun_lock);
11977 * This isn't really an error. It's entirely possible for
11978 * the abort and command completion to cross on the wire.
11979 * This is more of an informative/diagnostic error.
11982 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
11983 "%u:%u:%u tag %d type %d\n",
11984 io->io_hdr.nexus.initid,
11985 io->io_hdr.nexus.targ_port,
11986 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
11987 io->taskio.tag_type);
11990 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11995 ctl_query_task(union ctl_io *io, int task_set)
11997 struct ctl_softc *softc = CTL_SOFTC(io);
11999 struct ctl_lun *lun;
12003 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12004 mtx_lock(&softc->ctl_lock);
12005 if (targ_lun >= CTL_MAX_LUNS ||
12006 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12007 mtx_unlock(&softc->ctl_lock);
12008 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12011 mtx_lock(&lun->lun_lock);
12012 mtx_unlock(&softc->ctl_lock);
12013 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12014 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12016 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12017 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12018 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12021 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) {
12026 mtx_unlock(&lun->lun_lock);
12028 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12030 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12035 ctl_query_async_event(union ctl_io *io)
12037 struct ctl_softc *softc = CTL_SOFTC(io);
12038 struct ctl_lun *lun;
12040 uint32_t targ_lun, initidx;
12042 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12043 mtx_lock(&softc->ctl_lock);
12044 if (targ_lun >= CTL_MAX_LUNS ||
12045 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12046 mtx_unlock(&softc->ctl_lock);
12047 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12050 mtx_lock(&lun->lun_lock);
12051 mtx_unlock(&softc->ctl_lock);
12052 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12053 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp);
12054 mtx_unlock(&lun->lun_lock);
12055 if (ua != CTL_UA_NONE)
12056 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12058 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12063 ctl_run_task(union ctl_io *io)
12067 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12068 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12069 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type));
12070 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED;
12071 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp));
12072 switch (io->taskio.task_action) {
12073 case CTL_TASK_ABORT_TASK:
12074 retval = ctl_abort_task(io);
12076 case CTL_TASK_ABORT_TASK_SET:
12077 case CTL_TASK_CLEAR_TASK_SET:
12078 retval = ctl_abort_task_set(io);
12080 case CTL_TASK_CLEAR_ACA:
12082 case CTL_TASK_I_T_NEXUS_RESET:
12083 retval = ctl_i_t_nexus_reset(io);
12085 case CTL_TASK_LUN_RESET:
12086 retval = ctl_lun_reset(io);
12088 case CTL_TASK_TARGET_RESET:
12089 case CTL_TASK_BUS_RESET:
12090 retval = ctl_target_reset(io);
12092 case CTL_TASK_PORT_LOGIN:
12094 case CTL_TASK_PORT_LOGOUT:
12096 case CTL_TASK_QUERY_TASK:
12097 retval = ctl_query_task(io, 0);
12099 case CTL_TASK_QUERY_TASK_SET:
12100 retval = ctl_query_task(io, 1);
12102 case CTL_TASK_QUERY_ASYNC_EVENT:
12103 retval = ctl_query_async_event(io);
12106 printf("%s: got unknown task management event %d\n",
12107 __func__, io->taskio.task_action);
12111 io->io_hdr.status = CTL_SUCCESS;
12113 io->io_hdr.status = CTL_ERROR;
12118 * For HA operation. Handle commands that come in from the other
12122 ctl_handle_isc(union ctl_io *io)
12124 struct ctl_softc *softc = CTL_SOFTC(io);
12125 struct ctl_lun *lun;
12126 const struct ctl_cmd_entry *entry;
12129 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12130 switch (io->io_hdr.msg_type) {
12131 case CTL_MSG_SERIALIZE:
12132 ctl_serialize_other_sc_cmd(&io->scsiio);
12134 case CTL_MSG_R2R: /* Only used in SER_ONLY mode. */
12135 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12136 if (targ_lun >= CTL_MAX_LUNS ||
12137 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12141 mtx_lock(&lun->lun_lock);
12142 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) {
12143 mtx_unlock(&lun->lun_lock);
12147 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12148 mtx_unlock(&lun->lun_lock);
12149 ctl_enqueue_rtr(io);
12151 case CTL_MSG_FINISH_IO:
12152 if (softc->ha_mode == CTL_HA_MODE_XFER) {
12156 if (targ_lun >= CTL_MAX_LUNS ||
12157 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12161 mtx_lock(&lun->lun_lock);
12162 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
12163 ctl_check_blocked(lun);
12164 mtx_unlock(&lun->lun_lock);
12167 case CTL_MSG_PERS_ACTION:
12168 ctl_hndl_per_res_out_on_other_sc(io);
12171 case CTL_MSG_BAD_JUJU:
12174 case CTL_MSG_DATAMOVE: /* Only used in XFER mode */
12175 ctl_datamove_remote(io);
12177 case CTL_MSG_DATAMOVE_DONE: /* Only used in XFER mode */
12178 io->scsiio.be_move_done(io);
12180 case CTL_MSG_FAILOVER:
12181 ctl_failover_lun(io);
12185 printf("%s: Invalid message type %d\n",
12186 __func__, io->io_hdr.msg_type);
12195 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12196 * there is no match.
12198 static ctl_lun_error_pattern
12199 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12201 const struct ctl_cmd_entry *entry;
12202 ctl_lun_error_pattern filtered_pattern, pattern;
12204 pattern = desc->error_pattern;
12207 * XXX KDM we need more data passed into this function to match a
12208 * custom pattern, and we actually need to implement custom pattern
12211 if (pattern & CTL_LUN_PAT_CMD)
12212 return (CTL_LUN_PAT_CMD);
12214 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12215 return (CTL_LUN_PAT_ANY);
12217 entry = ctl_get_cmd_entry(ctsio, NULL);
12219 filtered_pattern = entry->pattern & pattern;
12222 * If the user requested specific flags in the pattern (e.g.
12223 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12226 * If the user did not specify any flags, it doesn't matter whether
12227 * or not the command supports the flags.
12229 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12230 (pattern & ~CTL_LUN_PAT_MASK))
12231 return (CTL_LUN_PAT_NONE);
12234 * If the user asked for a range check, see if the requested LBA
12235 * range overlaps with this command's LBA range.
12237 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12243 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12245 return (CTL_LUN_PAT_NONE);
12247 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12248 desc->lba_range.len, FALSE);
12250 * A "pass" means that the LBA ranges don't overlap, so
12251 * this doesn't match the user's range criteria.
12253 if (action == CTL_ACTION_PASS)
12254 return (CTL_LUN_PAT_NONE);
12257 return (filtered_pattern);
12261 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12263 struct ctl_error_desc *desc, *desc2;
12265 mtx_assert(&lun->lun_lock, MA_OWNED);
12267 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12268 ctl_lun_error_pattern pattern;
12270 * Check to see whether this particular command matches
12271 * the pattern in the descriptor.
12273 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12274 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12277 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12278 case CTL_LUN_INJ_ABORTED:
12279 ctl_set_aborted(&io->scsiio);
12281 case CTL_LUN_INJ_MEDIUM_ERR:
12282 ctl_set_medium_error(&io->scsiio,
12283 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) !=
12284 CTL_FLAG_DATA_OUT);
12286 case CTL_LUN_INJ_UA:
12287 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12289 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12291 case CTL_LUN_INJ_CUSTOM:
12293 * We're assuming the user knows what he is doing.
12294 * Just copy the sense information without doing
12297 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12298 MIN(sizeof(desc->custom_sense),
12299 sizeof(io->scsiio.sense_data)));
12300 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12301 io->scsiio.sense_len = SSD_FULL_SIZE;
12302 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12304 case CTL_LUN_INJ_NONE:
12307 * If this is an error injection type we don't know
12308 * about, clear the continuous flag (if it is set)
12309 * so it will get deleted below.
12311 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12315 * By default, each error injection action is a one-shot
12317 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12320 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12326 #ifdef CTL_IO_DELAY
12328 ctl_datamove_timer_wakeup(void *arg)
12332 io = (union ctl_io *)arg;
12336 #endif /* CTL_IO_DELAY */
12339 ctl_datamove(union ctl_io *io)
12341 void (*fe_datamove)(union ctl_io *io);
12343 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
12345 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12347 /* No data transferred yet. Frontend must update this when done. */
12348 io->scsiio.kern_data_resid = io->scsiio.kern_data_len;
12351 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12356 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12357 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12359 sbuf_cat(&sb, path_str);
12360 switch (io->io_hdr.io_type) {
12362 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12363 sbuf_printf(&sb, "\n");
12364 sbuf_cat(&sb, path_str);
12365 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12366 io->scsiio.tag_num, io->scsiio.tag_type);
12369 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12370 "Tag Type: %d\n", io->taskio.task_action,
12371 io->taskio.tag_num, io->taskio.tag_type);
12374 panic("%s: Invalid CTL I/O type %d\n",
12375 __func__, io->io_hdr.io_type);
12377 sbuf_cat(&sb, path_str);
12378 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12379 (intmax_t)time_uptime - io->io_hdr.start_time);
12381 printf("%s", sbuf_data(&sb));
12383 #endif /* CTL_TIME_IO */
12385 #ifdef CTL_IO_DELAY
12386 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12387 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12389 struct ctl_lun *lun;
12393 && (lun->delay_info.datamove_delay > 0)) {
12395 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
12396 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12397 callout_reset(&io->io_hdr.delay_callout,
12398 lun->delay_info.datamove_delay * hz,
12399 ctl_datamove_timer_wakeup, io);
12400 if (lun->delay_info.datamove_type ==
12401 CTL_DELAY_TYPE_ONESHOT)
12402 lun->delay_info.datamove_delay = 0;
12409 * This command has been aborted. Set the port status, so we fail
12412 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12413 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n",
12414 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12415 io->io_hdr.nexus.targ_port,
12416 io->io_hdr.nexus.targ_lun);
12417 io->io_hdr.port_status = 31337;
12419 * Note that the backend, in this case, will get the
12420 * callback in its context. In other cases it may get
12421 * called in the frontend's interrupt thread context.
12423 io->scsiio.be_move_done(io);
12427 /* Don't confuse frontend with zero length data move. */
12428 if (io->scsiio.kern_data_len == 0) {
12429 io->scsiio.be_move_done(io);
12433 fe_datamove = CTL_PORT(io)->fe_datamove;
12438 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12440 union ctl_ha_msg msg;
12442 struct bintime cur_bt;
12445 memset(&msg, 0, sizeof(msg));
12446 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12447 msg.hdr.original_sc = io;
12448 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12449 msg.hdr.nexus = io->io_hdr.nexus;
12450 msg.hdr.status = io->io_hdr.status;
12451 msg.scsi.kern_data_resid = io->scsiio.kern_data_resid;
12452 msg.scsi.tag_num = io->scsiio.tag_num;
12453 msg.scsi.tag_type = io->scsiio.tag_type;
12454 msg.scsi.scsi_status = io->scsiio.scsi_status;
12455 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12456 io->scsiio.sense_len);
12457 msg.scsi.sense_len = io->scsiio.sense_len;
12458 msg.scsi.port_status = io->io_hdr.port_status;
12459 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12460 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12461 ctl_failover_io(io, /*have_lock*/ have_lock);
12464 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12465 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12466 msg.scsi.sense_len, M_WAITOK);
12469 getbinuptime(&cur_bt);
12470 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
12471 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
12473 io->io_hdr.num_dmas++;
12477 * The DMA to the remote side is done, now we need to tell the other side
12478 * we're done so it can continue with its data movement.
12481 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12488 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12489 printf("%s: ISC DMA write failed with error %d", __func__,
12491 ctl_set_internal_failure(&io->scsiio,
12493 /*retry_count*/ rq->ret);
12496 ctl_dt_req_free(rq);
12498 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12499 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12500 free(io->io_hdr.remote_sglist, M_CTL);
12501 io->io_hdr.remote_sglist = NULL;
12502 io->io_hdr.local_sglist = NULL;
12505 * The data is in local and remote memory, so now we need to send
12506 * status (good or back) back to the other side.
12508 ctl_send_datamove_done(io, /*have_lock*/ 0);
12512 * We've moved the data from the host/controller into local memory. Now we
12513 * need to push it over to the remote controller's memory.
12516 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12520 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12521 ctl_datamove_remote_write_cb);
12526 ctl_datamove_remote_write(union ctl_io *io)
12529 void (*fe_datamove)(union ctl_io *io);
12532 * - Get the data from the host/HBA into local memory.
12533 * - DMA memory from the local controller to the remote controller.
12534 * - Send status back to the remote controller.
12537 retval = ctl_datamove_remote_sgl_setup(io);
12541 /* Switch the pointer over so the FETD knows what to do */
12542 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12545 * Use a custom move done callback, since we need to send completion
12546 * back to the other controller, not to the backend on this side.
12548 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12550 fe_datamove = CTL_PORT(io)->fe_datamove;
12555 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12564 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12565 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12566 free(io->io_hdr.remote_sglist, M_CTL);
12567 io->io_hdr.remote_sglist = NULL;
12568 io->io_hdr.local_sglist = NULL;
12571 scsi_path_string(io, path_str, sizeof(path_str));
12572 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12573 sbuf_cat(&sb, path_str);
12574 scsi_command_string(&io->scsiio, NULL, &sb);
12575 sbuf_printf(&sb, "\n");
12576 sbuf_cat(&sb, path_str);
12577 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12578 io->scsiio.tag_num, io->scsiio.tag_type);
12579 sbuf_cat(&sb, path_str);
12580 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12581 io->io_hdr.flags, io->io_hdr.status);
12583 printk("%s", sbuf_data(&sb));
12588 * The read is done, now we need to send status (good or bad) back
12589 * to the other side.
12591 ctl_send_datamove_done(io, /*have_lock*/ 0);
12597 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12600 void (*fe_datamove)(union ctl_io *io);
12604 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12605 printf("%s: ISC DMA read failed with error %d\n", __func__,
12607 ctl_set_internal_failure(&io->scsiio,
12609 /*retry_count*/ rq->ret);
12612 ctl_dt_req_free(rq);
12614 /* Switch the pointer over so the FETD knows what to do */
12615 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12618 * Use a custom move done callback, since we need to send completion
12619 * back to the other controller, not to the backend on this side.
12621 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12623 /* XXX KDM add checks like the ones in ctl_datamove? */
12625 fe_datamove = CTL_PORT(io)->fe_datamove;
12630 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12632 struct ctl_sg_entry *local_sglist;
12633 uint32_t len_to_go;
12638 local_sglist = io->io_hdr.local_sglist;
12639 len_to_go = io->scsiio.kern_data_len;
12642 * The difficult thing here is that the size of the various
12643 * S/G segments may be different than the size from the
12644 * remote controller. That'll make it harder when DMAing
12645 * the data back to the other side.
12647 for (i = 0; len_to_go > 0; i++) {
12648 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT);
12649 local_sglist[i].addr =
12650 malloc(local_sglist[i].len, M_CTL, M_WAITOK);
12652 len_to_go -= local_sglist[i].len;
12655 * Reset the number of S/G entries accordingly. The original
12656 * number of S/G entries is available in rem_sg_entries.
12658 io->scsiio.kern_sg_entries = i;
12661 printf("%s: kern_sg_entries = %d\n", __func__,
12662 io->scsiio.kern_sg_entries);
12663 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12664 printf("%s: sg[%d] = %p, %lu\n", __func__, i,
12665 local_sglist[i].addr, local_sglist[i].len);
12672 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12673 ctl_ha_dt_cb callback)
12675 struct ctl_ha_dt_req *rq;
12676 struct ctl_sg_entry *remote_sglist, *local_sglist;
12677 uint32_t local_used, remote_used, total_used;
12680 rq = ctl_dt_req_alloc();
12683 * If we failed to allocate the request, and if the DMA didn't fail
12684 * anyway, set busy status. This is just a resource allocation
12688 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12689 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS))
12690 ctl_set_busy(&io->scsiio);
12692 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12693 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
12696 ctl_dt_req_free(rq);
12699 * The data move failed. We need to return status back
12700 * to the other controller. No point in trying to DMA
12701 * data to the remote controller.
12704 ctl_send_datamove_done(io, /*have_lock*/ 0);
12709 local_sglist = io->io_hdr.local_sglist;
12710 remote_sglist = io->io_hdr.remote_sglist;
12716 * Pull/push the data over the wire from/to the other controller.
12717 * This takes into account the possibility that the local and
12718 * remote sglists may not be identical in terms of the size of
12719 * the elements and the number of elements.
12721 * One fundamental assumption here is that the length allocated for
12722 * both the local and remote sglists is identical. Otherwise, we've
12723 * essentially got a coding error of some sort.
12725 isc_ret = CTL_HA_STATUS_SUCCESS;
12726 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12730 rq->command = command;
12734 * Both pointers should be aligned. But it is possible
12735 * that the allocation length is not. They should both
12736 * also have enough slack left over at the end, though,
12737 * to round up to the next 8 byte boundary.
12739 cur_len = MIN(local_sglist[i].len - local_used,
12740 remote_sglist[j].len - remote_used);
12741 rq->size = cur_len;
12743 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12744 tmp_ptr += local_used;
12747 /* Use physical addresses when talking to ISC hardware */
12748 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12749 /* XXX KDM use busdma */
12750 rq->local = vtophys(tmp_ptr);
12752 rq->local = tmp_ptr;
12754 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
12755 ("HA does not support BUS_ADDR"));
12756 rq->local = tmp_ptr;
12759 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12760 tmp_ptr += remote_used;
12761 rq->remote = tmp_ptr;
12763 rq->callback = NULL;
12765 local_used += cur_len;
12766 if (local_used >= local_sglist[i].len) {
12771 remote_used += cur_len;
12772 if (remote_used >= remote_sglist[j].len) {
12776 total_used += cur_len;
12778 if (total_used >= io->scsiio.kern_data_len)
12779 rq->callback = callback;
12782 printf("%s: %s: local %p remote %p size %d\n", __func__,
12783 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
12784 rq->local, rq->remote, rq->size);
12787 isc_ret = ctl_dt_single(rq);
12788 if (isc_ret > CTL_HA_STATUS_SUCCESS)
12791 if (isc_ret != CTL_HA_STATUS_WAIT) {
12800 ctl_datamove_remote_read(union ctl_io *io)
12806 * This will send an error to the other controller in the case of a
12809 retval = ctl_datamove_remote_sgl_setup(io);
12813 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12814 ctl_datamove_remote_read_cb);
12817 * Make sure we free memory if there was an error.. The
12818 * ctl_datamove_remote_xfer() function will send the
12819 * datamove done message, or call the callback with an
12820 * error if there is a problem.
12822 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12823 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12824 free(io->io_hdr.remote_sglist, M_CTL);
12825 io->io_hdr.remote_sglist = NULL;
12826 io->io_hdr.local_sglist = NULL;
12831 * Process a datamove request from the other controller. This is used for
12832 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
12833 * first. Once that is complete, the data gets DMAed into the remote
12834 * controller's memory. For reads, we DMA from the remote controller's
12835 * memory into our memory first, and then move it out to the FETD.
12838 ctl_datamove_remote(union ctl_io *io)
12841 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
12843 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12844 ctl_failover_io(io, /*have_lock*/ 0);
12849 * Note that we look for an aborted I/O here, but don't do some of
12850 * the other checks that ctl_datamove() normally does.
12851 * We don't need to run the datamove delay code, since that should
12852 * have been done if need be on the other controller.
12854 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12855 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__,
12856 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12857 io->io_hdr.nexus.targ_port,
12858 io->io_hdr.nexus.targ_lun);
12859 io->io_hdr.port_status = 31338;
12860 ctl_send_datamove_done(io, /*have_lock*/ 0);
12864 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
12865 ctl_datamove_remote_write(io);
12866 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
12867 ctl_datamove_remote_read(io);
12869 io->io_hdr.port_status = 31339;
12870 ctl_send_datamove_done(io, /*have_lock*/ 0);
12875 ctl_process_done(union ctl_io *io)
12877 struct ctl_softc *softc = CTL_SOFTC(io);
12878 struct ctl_port *port = CTL_PORT(io);
12879 struct ctl_lun *lun = CTL_LUN(io);
12880 void (*fe_done)(union ctl_io *io);
12881 union ctl_ha_msg msg;
12883 CTL_DEBUG_PRINT(("ctl_process_done\n"));
12884 fe_done = port->fe_done;
12887 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12892 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12893 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12895 sbuf_cat(&sb, path_str);
12896 switch (io->io_hdr.io_type) {
12898 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12899 sbuf_printf(&sb, "\n");
12900 sbuf_cat(&sb, path_str);
12901 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12902 io->scsiio.tag_num, io->scsiio.tag_type);
12905 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12906 "Tag Type: %d\n", io->taskio.task_action,
12907 io->taskio.tag_num, io->taskio.tag_type);
12910 panic("%s: Invalid CTL I/O type %d\n",
12911 __func__, io->io_hdr.io_type);
12913 sbuf_cat(&sb, path_str);
12914 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
12915 (intmax_t)time_uptime - io->io_hdr.start_time);
12917 printf("%s", sbuf_data(&sb));
12919 #endif /* CTL_TIME_IO */
12921 switch (io->io_hdr.io_type) {
12925 if (ctl_debug & CTL_DEBUG_INFO)
12926 ctl_io_error_print(io, NULL);
12930 panic("%s: Invalid CTL I/O type %d\n",
12931 __func__, io->io_hdr.io_type);
12935 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
12936 io->io_hdr.nexus.targ_mapped_lun));
12940 mtx_lock(&lun->lun_lock);
12943 * Check to see if we have any informational exception and status
12944 * of this command can be modified to report it in form of either
12945 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field.
12947 if (lun->ie_reported == 0 && lun->ie_asc != 0 &&
12948 io->io_hdr.status == CTL_SUCCESS &&
12949 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) {
12950 uint8_t mrie = lun->MODE_IE.mrie;
12951 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) ||
12952 (lun->MODE_VER.byte3 & SMS_VER_PER));
12953 if (((mrie == SIEP_MRIE_REC_COND && per) ||
12954 mrie == SIEP_MRIE_REC_UNCOND ||
12955 mrie == SIEP_MRIE_NO_SENSE) &&
12956 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags &
12957 CTL_CMD_FLAG_NO_SENSE) == 0) {
12958 ctl_set_sense(&io->scsiio,
12959 /*current_error*/ 1,
12960 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ?
12961 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR,
12962 /*asc*/ lun->ie_asc,
12963 /*ascq*/ lun->ie_ascq,
12965 lun->ie_reported = 1;
12967 } else if (lun->ie_reported < 0)
12968 lun->ie_reported = 0;
12971 * Check to see if we have any errors to inject here. We only
12972 * inject errors for commands that don't already have errors set.
12974 if (!STAILQ_EMPTY(&lun->error_list) &&
12975 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
12976 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
12977 ctl_inject_error(lun, io);
12980 * XXX KDM how do we treat commands that aren't completed
12983 * XXX KDM should we also track I/O latency?
12985 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
12986 io->io_hdr.io_type == CTL_IO_SCSI) {
12992 bintime_sub(&bt, &io->io_hdr.start_bt);
12994 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12996 type = CTL_STATS_READ;
12997 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12999 type = CTL_STATS_WRITE;
13001 type = CTL_STATS_NO_IO;
13003 #ifdef CTL_LEGACY_STATS
13004 uint32_t targ_port = port->targ_port;
13005 lun->legacy_stats.ports[targ_port].bytes[type] +=
13006 io->scsiio.kern_total_len;
13007 lun->legacy_stats.ports[targ_port].operations[type] ++;
13008 lun->legacy_stats.ports[targ_port].num_dmas[type] +=
13009 io->io_hdr.num_dmas;
13011 bintime_add(&lun->legacy_stats.ports[targ_port].dma_time[type],
13012 &io->io_hdr.dma_bt);
13013 bintime_add(&lun->legacy_stats.ports[targ_port].time[type],
13016 #endif /* CTL_LEGACY_STATS */
13018 lun->stats.bytes[type] += io->scsiio.kern_total_len;
13019 lun->stats.operations[type] ++;
13020 lun->stats.dmas[type] += io->io_hdr.num_dmas;
13022 bintime_add(&lun->stats.dma_time[type], &io->io_hdr.dma_bt);
13023 bintime_add(&lun->stats.time[type], &bt);
13026 mtx_lock(&port->port_lock);
13027 port->stats.bytes[type] += io->scsiio.kern_total_len;
13028 port->stats.operations[type] ++;
13029 port->stats.dmas[type] += io->io_hdr.num_dmas;
13031 bintime_add(&port->stats.dma_time[type], &io->io_hdr.dma_bt);
13032 bintime_add(&port->stats.time[type], &bt);
13034 mtx_unlock(&port->port_lock);
13038 * Remove this from the OOA queue.
13040 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13042 if (TAILQ_EMPTY(&lun->ooa_queue))
13043 lun->last_busy = getsbinuptime();
13047 * Run through the blocked queue on this LUN and see if anything
13048 * has become unblocked, now that this transaction is done.
13050 ctl_check_blocked(lun);
13053 * If the LUN has been invalidated, free it if there is nothing
13054 * left on its OOA queue.
13056 if ((lun->flags & CTL_LUN_INVALID)
13057 && TAILQ_EMPTY(&lun->ooa_queue)) {
13058 mtx_unlock(&lun->lun_lock);
13059 mtx_lock(&softc->ctl_lock);
13061 mtx_unlock(&softc->ctl_lock);
13063 mtx_unlock(&lun->lun_lock);
13068 * If this command has been aborted, make sure we set the status
13069 * properly. The FETD is responsible for freeing the I/O and doing
13070 * whatever it needs to do to clean up its state.
13072 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13073 ctl_set_task_aborted(&io->scsiio);
13076 * If enabled, print command error status.
13078 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
13079 (ctl_debug & CTL_DEBUG_INFO) != 0)
13080 ctl_io_error_print(io, NULL);
13083 * Tell the FETD or the other shelf controller we're done with this
13084 * command. Note that only SCSI commands get to this point. Task
13085 * management commands are completed above.
13087 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
13088 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) {
13089 memset(&msg, 0, sizeof(msg));
13090 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13091 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
13092 msg.hdr.nexus = io->io_hdr.nexus;
13093 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13094 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data),
13102 * Front end should call this if it doesn't do autosense. When the request
13103 * sense comes back in from the initiator, we'll dequeue this and send it.
13106 ctl_queue_sense(union ctl_io *io)
13108 struct ctl_softc *softc = CTL_SOFTC(io);
13109 struct ctl_port *port = CTL_PORT(io);
13110 struct ctl_lun *lun;
13111 struct scsi_sense_data *ps;
13112 uint32_t initidx, p, targ_lun;
13114 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13116 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13119 * LUN lookup will likely move to the ctl_work_thread() once we
13120 * have our new queueing infrastructure (that doesn't put things on
13121 * a per-LUN queue initially). That is so that we can handle
13122 * things like an INQUIRY to a LUN that we don't have enabled. We
13123 * can't deal with that right now.
13124 * If we don't have a LUN for this, just toss the sense information.
13126 mtx_lock(&softc->ctl_lock);
13127 if (targ_lun >= CTL_MAX_LUNS ||
13128 (lun = softc->ctl_luns[targ_lun]) == NULL) {
13129 mtx_unlock(&softc->ctl_lock);
13132 mtx_lock(&lun->lun_lock);
13133 mtx_unlock(&softc->ctl_lock);
13135 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13136 p = initidx / CTL_MAX_INIT_PER_PORT;
13137 if (lun->pending_sense[p] == NULL) {
13138 lun->pending_sense[p] = malloc(sizeof(*ps) * CTL_MAX_INIT_PER_PORT,
13139 M_CTL, M_NOWAIT | M_ZERO);
13141 if ((ps = lun->pending_sense[p]) != NULL) {
13142 ps += initidx % CTL_MAX_INIT_PER_PORT;
13143 memset(ps, 0, sizeof(*ps));
13144 memcpy(ps, &io->scsiio.sense_data, io->scsiio.sense_len);
13146 mtx_unlock(&lun->lun_lock);
13150 return (CTL_RETVAL_COMPLETE);
13154 * Primary command inlet from frontend ports. All SCSI and task I/O
13155 * requests must go through this function.
13158 ctl_queue(union ctl_io *io)
13160 struct ctl_port *port = CTL_PORT(io);
13162 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13165 io->io_hdr.start_time = time_uptime;
13166 getbinuptime(&io->io_hdr.start_bt);
13167 #endif /* CTL_TIME_IO */
13169 /* Map FE-specific LUN ID into global one. */
13170 io->io_hdr.nexus.targ_mapped_lun =
13171 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13173 switch (io->io_hdr.io_type) {
13176 if (ctl_debug & CTL_DEBUG_CDB)
13178 ctl_enqueue_incoming(io);
13181 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13185 return (CTL_RETVAL_COMPLETE);
13188 #ifdef CTL_IO_DELAY
13190 ctl_done_timer_wakeup(void *arg)
13194 io = (union ctl_io *)arg;
13197 #endif /* CTL_IO_DELAY */
13200 ctl_serseq_done(union ctl_io *io)
13202 struct ctl_lun *lun = CTL_LUN(io);;
13204 if (lun->be_lun == NULL ||
13205 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF)
13207 mtx_lock(&lun->lun_lock);
13208 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE;
13209 ctl_check_blocked(lun);
13210 mtx_unlock(&lun->lun_lock);
13214 ctl_done(union ctl_io *io)
13218 * Enable this to catch duplicate completion issues.
13221 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13222 printf("%s: type %d msg %d cdb %x iptl: "
13223 "%u:%u:%u tag 0x%04x "
13224 "flag %#x status %x\n",
13226 io->io_hdr.io_type,
13227 io->io_hdr.msg_type,
13229 io->io_hdr.nexus.initid,
13230 io->io_hdr.nexus.targ_port,
13231 io->io_hdr.nexus.targ_lun,
13232 (io->io_hdr.io_type ==
13234 io->taskio.tag_num :
13235 io->scsiio.tag_num,
13237 io->io_hdr.status);
13239 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13243 * This is an internal copy of an I/O, and should not go through
13244 * the normal done processing logic.
13246 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13249 #ifdef CTL_IO_DELAY
13250 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13251 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13253 struct ctl_lun *lun = CTL_LUN(io);
13256 && (lun->delay_info.done_delay > 0)) {
13258 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
13259 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13260 callout_reset(&io->io_hdr.delay_callout,
13261 lun->delay_info.done_delay * hz,
13262 ctl_done_timer_wakeup, io);
13263 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13264 lun->delay_info.done_delay = 0;
13268 #endif /* CTL_IO_DELAY */
13270 ctl_enqueue_done(io);
13274 ctl_work_thread(void *arg)
13276 struct ctl_thread *thr = (struct ctl_thread *)arg;
13277 struct ctl_softc *softc = thr->ctl_softc;
13281 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13283 while (!softc->shutdown) {
13285 * We handle the queues in this order:
13287 * - done queue (to free up resources, unblock other commands)
13291 * If those queues are empty, we break out of the loop and
13294 mtx_lock(&thr->queue_lock);
13295 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13297 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13298 mtx_unlock(&thr->queue_lock);
13299 ctl_handle_isc(io);
13302 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13304 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13305 /* clear any blocked commands, call fe_done */
13306 mtx_unlock(&thr->queue_lock);
13307 ctl_process_done(io);
13310 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13312 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13313 mtx_unlock(&thr->queue_lock);
13314 if (io->io_hdr.io_type == CTL_IO_TASK)
13317 ctl_scsiio_precheck(softc, &io->scsiio);
13320 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13322 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13323 mtx_unlock(&thr->queue_lock);
13324 retval = ctl_scsiio(&io->scsiio);
13325 if (retval != CTL_RETVAL_COMPLETE)
13326 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13330 /* Sleep until we have something to do. */
13331 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13333 thr->thread = NULL;
13338 ctl_lun_thread(void *arg)
13340 struct ctl_softc *softc = (struct ctl_softc *)arg;
13341 struct ctl_be_lun *be_lun;
13343 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13345 while (!softc->shutdown) {
13346 mtx_lock(&softc->ctl_lock);
13347 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13348 if (be_lun != NULL) {
13349 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13350 mtx_unlock(&softc->ctl_lock);
13351 ctl_create_lun(be_lun);
13355 /* Sleep until we have something to do. */
13356 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13357 PDROP | PRIBIO, "-", 0);
13359 softc->lun_thread = NULL;
13364 ctl_thresh_thread(void *arg)
13366 struct ctl_softc *softc = (struct ctl_softc *)arg;
13367 struct ctl_lun *lun;
13368 struct ctl_logical_block_provisioning_page *page;
13370 union ctl_ha_msg msg;
13371 uint64_t thres, val;
13374 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13376 while (!softc->shutdown) {
13377 mtx_lock(&softc->ctl_lock);
13378 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13379 if ((lun->flags & CTL_LUN_DISABLED) ||
13380 (lun->flags & CTL_LUN_NO_MEDIA) ||
13381 lun->backend->lun_attr == NULL)
13383 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
13384 softc->ha_mode == CTL_HA_MODE_XFER)
13386 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0)
13389 page = &lun->MODE_LBP;
13390 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13391 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13393 thres = scsi_4btoul(page->descr[i].count);
13394 thres <<= CTL_LBP_EXPONENT;
13395 switch (page->descr[i].resource) {
13397 attr = "blocksavail";
13400 attr = "blocksused";
13403 attr = "poolblocksavail";
13406 attr = "poolblocksused";
13411 mtx_unlock(&softc->ctl_lock); // XXX
13412 val = lun->backend->lun_attr(
13413 lun->be_lun->be_lun, attr);
13414 mtx_lock(&softc->ctl_lock);
13415 if (val == UINT64_MAX)
13417 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13418 == SLBPPD_ARMING_INC)
13419 e = (val >= thres);
13421 e = (val <= thres);
13425 mtx_lock(&lun->lun_lock);
13427 scsi_u64to8b((uint8_t *)&page->descr[i] -
13428 (uint8_t *)page, lun->ua_tpt_info);
13429 if (lun->lasttpt == 0 ||
13430 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13431 lun->lasttpt = time_uptime;
13432 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13438 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13441 mtx_unlock(&lun->lun_lock);
13443 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
13444 /* Send msg to other side. */
13445 bzero(&msg.ua, sizeof(msg.ua));
13446 msg.hdr.msg_type = CTL_MSG_UA;
13447 msg.hdr.nexus.initid = -1;
13448 msg.hdr.nexus.targ_port = -1;
13449 msg.hdr.nexus.targ_lun = lun->lun;
13450 msg.hdr.nexus.targ_mapped_lun = lun->lun;
13452 msg.ua.ua_set = (set > 0);
13453 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES;
13454 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8);
13455 mtx_unlock(&softc->ctl_lock); // XXX
13456 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13457 sizeof(msg.ua), M_WAITOK);
13458 mtx_lock(&softc->ctl_lock);
13461 mtx_sleep(&softc->thresh_thread, &softc->ctl_lock,
13462 PDROP | PRIBIO, "-", CTL_LBP_PERIOD * hz);
13464 softc->thresh_thread = NULL;
13469 ctl_enqueue_incoming(union ctl_io *io)
13471 struct ctl_softc *softc = CTL_SOFTC(io);
13472 struct ctl_thread *thr;
13475 idx = (io->io_hdr.nexus.targ_port * 127 +
13476 io->io_hdr.nexus.initid) % worker_threads;
13477 thr = &softc->threads[idx];
13478 mtx_lock(&thr->queue_lock);
13479 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13480 mtx_unlock(&thr->queue_lock);
13485 ctl_enqueue_rtr(union ctl_io *io)
13487 struct ctl_softc *softc = CTL_SOFTC(io);
13488 struct ctl_thread *thr;
13490 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13491 mtx_lock(&thr->queue_lock);
13492 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13493 mtx_unlock(&thr->queue_lock);
13498 ctl_enqueue_done(union ctl_io *io)
13500 struct ctl_softc *softc = CTL_SOFTC(io);
13501 struct ctl_thread *thr;
13503 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13504 mtx_lock(&thr->queue_lock);
13505 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13506 mtx_unlock(&thr->queue_lock);
13511 ctl_enqueue_isc(union ctl_io *io)
13513 struct ctl_softc *softc = CTL_SOFTC(io);
13514 struct ctl_thread *thr;
13516 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13517 mtx_lock(&thr->queue_lock);
13518 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13519 mtx_unlock(&thr->queue_lock);