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>
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/ctype.h>
49 #include <sys/kernel.h>
50 #include <sys/types.h>
51 #include <sys/kthread.h>
53 #include <sys/fcntl.h>
55 #include <sys/module.h>
56 #include <sys/mutex.h>
57 #include <sys/condvar.h>
58 #include <sys/malloc.h>
60 #include <sys/ioccom.h>
61 #include <sys/queue.h>
64 #include <sys/endian.h>
65 #include <sys/sysctl.h>
69 #include <cam/scsi/scsi_all.h>
70 #include <cam/scsi/scsi_cd.h>
71 #include <cam/scsi/scsi_da.h>
72 #include <cam/ctl/ctl_io.h>
73 #include <cam/ctl/ctl.h>
74 #include <cam/ctl/ctl_frontend.h>
75 #include <cam/ctl/ctl_util.h>
76 #include <cam/ctl/ctl_backend.h>
77 #include <cam/ctl/ctl_ioctl.h>
78 #include <cam/ctl/ctl_ha.h>
79 #include <cam/ctl/ctl_private.h>
80 #include <cam/ctl/ctl_debug.h>
81 #include <cam/ctl/ctl_scsi_all.h>
82 #include <cam/ctl/ctl_error.h>
84 struct ctl_softc *control_softc = NULL;
87 * Template mode pages.
91 * Note that these are default values only. The actual values will be
92 * filled in when the user does a mode sense.
94 const static struct scsi_da_rw_recovery_page rw_er_page_default = {
95 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
96 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
97 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE,
98 /*read_retry_count*/0,
100 /*head_offset_count*/0,
101 /*data_strobe_offset_cnt*/0,
102 /*byte8*/SMS_RWER_LBPERE,
103 /*write_retry_count*/0,
105 /*recovery_time_limit*/{0, 0},
108 const static struct scsi_da_rw_recovery_page rw_er_page_changeable = {
109 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
110 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
111 /*byte3*/SMS_RWER_PER,
112 /*read_retry_count*/0,
113 /*correction_span*/0,
114 /*head_offset_count*/0,
115 /*data_strobe_offset_cnt*/0,
116 /*byte8*/SMS_RWER_LBPERE,
117 /*write_retry_count*/0,
119 /*recovery_time_limit*/{0, 0},
122 const static struct scsi_format_page format_page_default = {
123 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
124 /*page_length*/sizeof(struct scsi_format_page) - 2,
125 /*tracks_per_zone*/ {0, 0},
126 /*alt_sectors_per_zone*/ {0, 0},
127 /*alt_tracks_per_zone*/ {0, 0},
128 /*alt_tracks_per_lun*/ {0, 0},
129 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
130 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
131 /*bytes_per_sector*/ {0, 0},
132 /*interleave*/ {0, 0},
133 /*track_skew*/ {0, 0},
134 /*cylinder_skew*/ {0, 0},
136 /*reserved*/ {0, 0, 0}
139 const static struct scsi_format_page format_page_changeable = {
140 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
141 /*page_length*/sizeof(struct scsi_format_page) - 2,
142 /*tracks_per_zone*/ {0, 0},
143 /*alt_sectors_per_zone*/ {0, 0},
144 /*alt_tracks_per_zone*/ {0, 0},
145 /*alt_tracks_per_lun*/ {0, 0},
146 /*sectors_per_track*/ {0, 0},
147 /*bytes_per_sector*/ {0, 0},
148 /*interleave*/ {0, 0},
149 /*track_skew*/ {0, 0},
150 /*cylinder_skew*/ {0, 0},
152 /*reserved*/ {0, 0, 0}
155 const static struct scsi_rigid_disk_page rigid_disk_page_default = {
156 /*page_code*/SMS_RIGID_DISK_PAGE,
157 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
158 /*cylinders*/ {0, 0, 0},
159 /*heads*/ CTL_DEFAULT_HEADS,
160 /*start_write_precomp*/ {0, 0, 0},
161 /*start_reduced_current*/ {0, 0, 0},
162 /*step_rate*/ {0, 0},
163 /*landing_zone_cylinder*/ {0, 0, 0},
164 /*rpl*/ SRDP_RPL_DISABLED,
165 /*rotational_offset*/ 0,
167 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
168 CTL_DEFAULT_ROTATION_RATE & 0xff},
172 const static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
173 /*page_code*/SMS_RIGID_DISK_PAGE,
174 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
175 /*cylinders*/ {0, 0, 0},
177 /*start_write_precomp*/ {0, 0, 0},
178 /*start_reduced_current*/ {0, 0, 0},
179 /*step_rate*/ {0, 0},
180 /*landing_zone_cylinder*/ {0, 0, 0},
182 /*rotational_offset*/ 0,
184 /*rotation_rate*/ {0, 0},
188 const static struct scsi_da_verify_recovery_page verify_er_page_default = {
189 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE,
190 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2,
192 /*read_retry_count*/0,
193 /*reserved*/{ 0, 0, 0, 0, 0, 0 },
194 /*recovery_time_limit*/{0, 0},
197 const static struct scsi_da_verify_recovery_page verify_er_page_changeable = {
198 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE,
199 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2,
200 /*byte3*/SMS_VER_PER,
201 /*read_retry_count*/0,
202 /*reserved*/{ 0, 0, 0, 0, 0, 0 },
203 /*recovery_time_limit*/{0, 0},
206 const static struct scsi_caching_page caching_page_default = {
207 /*page_code*/SMS_CACHING_PAGE,
208 /*page_length*/sizeof(struct scsi_caching_page) - 2,
209 /*flags1*/ SCP_DISC | SCP_WCE,
211 /*disable_pf_transfer_len*/ {0xff, 0xff},
212 /*min_prefetch*/ {0, 0},
213 /*max_prefetch*/ {0xff, 0xff},
214 /*max_pf_ceiling*/ {0xff, 0xff},
216 /*cache_segments*/ 0,
217 /*cache_seg_size*/ {0, 0},
219 /*non_cache_seg_size*/ {0, 0, 0}
222 const static struct scsi_caching_page caching_page_changeable = {
223 /*page_code*/SMS_CACHING_PAGE,
224 /*page_length*/sizeof(struct scsi_caching_page) - 2,
225 /*flags1*/ SCP_WCE | SCP_RCD,
227 /*disable_pf_transfer_len*/ {0, 0},
228 /*min_prefetch*/ {0, 0},
229 /*max_prefetch*/ {0, 0},
230 /*max_pf_ceiling*/ {0, 0},
232 /*cache_segments*/ 0,
233 /*cache_seg_size*/ {0, 0},
235 /*non_cache_seg_size*/ {0, 0, 0}
238 const static struct scsi_control_page control_page_default = {
239 /*page_code*/SMS_CONTROL_MODE_PAGE,
240 /*page_length*/sizeof(struct scsi_control_page) - 2,
242 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED,
245 /*aen_holdoff_period*/{0, 0},
246 /*busy_timeout_period*/{0, 0},
247 /*extended_selftest_completion_time*/{0, 0}
250 const static struct scsi_control_page control_page_changeable = {
251 /*page_code*/SMS_CONTROL_MODE_PAGE,
252 /*page_length*/sizeof(struct scsi_control_page) - 2,
254 /*queue_flags*/SCP_QUEUE_ALG_MASK | SCP_NUAR,
255 /*eca_and_aen*/SCP_SWP,
257 /*aen_holdoff_period*/{0, 0},
258 /*busy_timeout_period*/{0, 0},
259 /*extended_selftest_completion_time*/{0, 0}
262 #define CTL_CEM_LEN (sizeof(struct scsi_control_ext_page) - 4)
264 const static struct scsi_control_ext_page control_ext_page_default = {
265 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
266 /*subpage_code*/0x01,
267 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
273 const static struct scsi_control_ext_page control_ext_page_changeable = {
274 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
275 /*subpage_code*/0x01,
276 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
282 const static struct scsi_info_exceptions_page ie_page_default = {
283 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
284 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
285 /*info_flags*/SIEP_FLAGS_EWASC,
286 /*mrie*/SIEP_MRIE_NO,
287 /*interval_timer*/{0, 0, 0, 0},
288 /*report_count*/{0, 0, 0, 1}
291 const static struct scsi_info_exceptions_page ie_page_changeable = {
292 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
293 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
294 /*info_flags*/SIEP_FLAGS_EWASC | SIEP_FLAGS_DEXCPT | SIEP_FLAGS_TEST |
297 /*interval_timer*/{0xff, 0xff, 0xff, 0xff},
298 /*report_count*/{0xff, 0xff, 0xff, 0xff}
301 #define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4)
303 const static struct ctl_logical_block_provisioning_page lbp_page_default = {{
304 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
305 /*subpage_code*/0x02,
306 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
308 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
313 /*count*/{0, 0, 0, 0}},
317 /*count*/{0, 0, 0, 0}},
321 /*count*/{0, 0, 0, 0}},
325 /*count*/{0, 0, 0, 0}}
329 const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{
330 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
331 /*subpage_code*/0x02,
332 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
333 /*flags*/SLBPP_SITUA,
334 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
339 /*count*/{0, 0, 0, 0}},
343 /*count*/{0, 0, 0, 0}},
347 /*count*/{0, 0, 0, 0}},
351 /*count*/{0, 0, 0, 0}}
355 const static struct scsi_cddvd_capabilities_page cddvd_page_default = {
356 /*page_code*/SMS_CDDVD_CAPS_PAGE,
357 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2,
365 /*nvol_levels*/{0, 0},
366 /*buffer_size*/{8, 0},
371 /*copy_management*/0,
373 /*rotation_control*/0,
374 /*cur_write_speed*/0,
375 /*num_speed_descr*/0,
378 const static struct scsi_cddvd_capabilities_page cddvd_page_changeable = {
379 /*page_code*/SMS_CDDVD_CAPS_PAGE,
380 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2,
388 /*nvol_levels*/{0, 0},
389 /*buffer_size*/{0, 0},
394 /*copy_management*/0,
396 /*rotation_control*/0,
397 /*cur_write_speed*/0,
398 /*num_speed_descr*/0,
401 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
402 static int worker_threads = -1;
403 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
404 &worker_threads, 1, "Number of worker threads");
405 static int ctl_debug = CTL_DEBUG_NONE;
406 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN,
407 &ctl_debug, 0, "Enabled debug flags");
408 static int ctl_lun_map_size = 1024;
409 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, lun_map_size, CTLFLAG_RWTUN,
410 &ctl_lun_map_size, 0, "Size of per-port LUN map (max LUN + 1)");
412 static int ctl_time_io_secs = CTL_TIME_IO_DEFAULT_SECS;
413 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, time_io_secs, CTLFLAG_RWTUN,
414 &ctl_time_io_secs, 0, "Log requests taking more seconds");
418 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
419 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
420 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0),
421 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
423 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 10
425 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
427 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
428 static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest);
429 static int ctl_init(void);
430 static int ctl_shutdown(void);
431 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
432 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
433 static void ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
434 static void ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
435 struct ctl_ooa *ooa_hdr,
436 struct ctl_ooa_entry *kern_entries);
437 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
439 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
440 struct ctl_be_lun *be_lun);
441 static int ctl_free_lun(struct ctl_lun *lun);
442 static void ctl_create_lun(struct ctl_be_lun *be_lun);
444 static int ctl_do_mode_select(union ctl_io *io);
445 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
446 uint64_t res_key, uint64_t sa_res_key,
447 uint8_t type, uint32_t residx,
448 struct ctl_scsiio *ctsio,
449 struct scsi_per_res_out *cdb,
450 struct scsi_per_res_out_parms* param);
451 static void ctl_pro_preempt_other(struct ctl_lun *lun,
452 union ctl_ha_msg *msg);
453 static void ctl_hndl_per_res_out_on_other_sc(union ctl_io *io);
454 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
455 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
456 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
457 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
458 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
459 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
461 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
463 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
464 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
465 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
466 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
467 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
468 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2,
470 static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2);
471 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
472 union ctl_io *pending_io, union ctl_io *ooa_io);
473 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
474 union ctl_io *starting_io);
475 static int ctl_check_blocked(struct ctl_lun *lun);
476 static int ctl_scsiio_lun_check(struct ctl_lun *lun,
477 const struct ctl_cmd_entry *entry,
478 struct ctl_scsiio *ctsio);
479 static void ctl_failover_lun(union ctl_io *io);
480 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
481 struct ctl_scsiio *ctsio);
482 static int ctl_scsiio(struct ctl_scsiio *ctsio);
484 static int ctl_target_reset(union ctl_io *io);
485 static void ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx,
486 ctl_ua_type ua_type);
487 static int ctl_lun_reset(union ctl_io *io);
488 static int ctl_abort_task(union ctl_io *io);
489 static int ctl_abort_task_set(union ctl_io *io);
490 static int ctl_query_task(union ctl_io *io, int task_set);
491 static void ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx,
492 ctl_ua_type ua_type);
493 static int ctl_i_t_nexus_reset(union ctl_io *io);
494 static int ctl_query_async_event(union ctl_io *io);
495 static void ctl_run_task(union ctl_io *io);
497 static void ctl_datamove_timer_wakeup(void *arg);
498 static void ctl_done_timer_wakeup(void *arg);
499 #endif /* CTL_IO_DELAY */
501 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
502 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
503 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
504 static void ctl_datamove_remote_write(union ctl_io *io);
505 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
506 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
507 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
508 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
509 ctl_ha_dt_cb callback);
510 static void ctl_datamove_remote_read(union ctl_io *io);
511 static void ctl_datamove_remote(union ctl_io *io);
512 static void ctl_process_done(union ctl_io *io);
513 static void ctl_lun_thread(void *arg);
514 static void ctl_thresh_thread(void *arg);
515 static void ctl_work_thread(void *arg);
516 static void ctl_enqueue_incoming(union ctl_io *io);
517 static void ctl_enqueue_rtr(union ctl_io *io);
518 static void ctl_enqueue_done(union ctl_io *io);
519 static void ctl_enqueue_isc(union ctl_io *io);
520 static const struct ctl_cmd_entry *
521 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
522 static const struct ctl_cmd_entry *
523 ctl_validate_command(struct ctl_scsiio *ctsio);
524 static int ctl_cmd_applicable(uint8_t lun_type,
525 const struct ctl_cmd_entry *entry);
526 static int ctl_ha_init(void);
527 static int ctl_ha_shutdown(void);
529 static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx);
530 static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx);
531 static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx);
532 static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key);
535 * Load the serialization table. This isn't very pretty, but is probably
536 * the easiest way to do it.
538 #include "ctl_ser_table.c"
541 * We only need to define open, close and ioctl routines for this driver.
543 static struct cdevsw ctl_cdevsw = {
544 .d_version = D_VERSION,
547 .d_close = ctl_close,
548 .d_ioctl = ctl_ioctl,
553 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
555 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
557 static moduledata_t ctl_moduledata = {
559 ctl_module_event_handler,
563 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
564 MODULE_VERSION(ctl, 1);
566 static struct ctl_frontend ha_frontend =
570 .shutdown = ctl_ha_shutdown,
576 struct ctl_softc *softc = control_softc;
578 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
579 &softc->othersc_pool) != 0)
581 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) {
582 ctl_pool_free(softc->othersc_pool);
585 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
586 != CTL_HA_STATUS_SUCCESS) {
587 ctl_ha_msg_destroy(softc);
588 ctl_pool_free(softc->othersc_pool);
595 ctl_ha_shutdown(void)
597 struct ctl_softc *softc = control_softc;
598 struct ctl_port *port;
600 ctl_ha_msg_shutdown(softc);
601 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL) != CTL_HA_STATUS_SUCCESS)
603 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS)
605 ctl_pool_free(softc->othersc_pool);
606 while ((port = STAILQ_FIRST(&ha_frontend.port_list)) != NULL) {
607 ctl_port_deregister(port);
608 free(port->port_name, M_CTL);
615 ctl_ha_datamove(union ctl_io *io)
617 struct ctl_lun *lun = CTL_LUN(io);
618 struct ctl_sg_entry *sgl;
619 union ctl_ha_msg msg;
620 uint32_t sg_entries_sent;
621 int do_sg_copy, i, j;
623 memset(&msg.dt, 0, sizeof(msg.dt));
624 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
625 msg.hdr.original_sc = io->io_hdr.original_sc;
626 msg.hdr.serializing_sc = io;
627 msg.hdr.nexus = io->io_hdr.nexus;
628 msg.hdr.status = io->io_hdr.status;
629 msg.dt.flags = io->io_hdr.flags;
632 * We convert everything into a S/G list here. We can't
633 * pass by reference, only by value between controllers.
634 * So we can't pass a pointer to the S/G list, only as many
635 * S/G entries as we can fit in here. If it's possible for
636 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
637 * then we need to break this up into multiple transfers.
639 if (io->scsiio.kern_sg_entries == 0) {
640 msg.dt.kern_sg_entries = 1;
642 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
643 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
645 /* XXX KDM use busdma here! */
646 msg.dt.sg_list[0].addr =
647 (void *)vtophys(io->scsiio.kern_data_ptr);
650 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
651 ("HA does not support BUS_ADDR"));
652 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
654 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
657 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
661 msg.dt.kern_data_len = io->scsiio.kern_data_len;
662 msg.dt.kern_total_len = io->scsiio.kern_total_len;
663 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
664 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
665 msg.dt.sg_sequence = 0;
668 * Loop until we've sent all of the S/G entries. On the
669 * other end, we'll recompose these S/G entries into one
670 * contiguous list before processing.
672 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries;
673 msg.dt.sg_sequence++) {
674 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) /
675 sizeof(msg.dt.sg_list[0])),
676 msg.dt.kern_sg_entries - sg_entries_sent);
677 if (do_sg_copy != 0) {
678 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
679 for (i = sg_entries_sent, j = 0;
680 i < msg.dt.cur_sg_entries; i++, j++) {
682 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
683 msg.dt.sg_list[j].addr = sgl[i].addr;
685 /* XXX KDM use busdma here! */
686 msg.dt.sg_list[j].addr =
687 (void *)vtophys(sgl[i].addr);
690 KASSERT((io->io_hdr.flags &
691 CTL_FLAG_BUS_ADDR) == 0,
692 ("HA does not support BUS_ADDR"));
693 msg.dt.sg_list[j].addr = sgl[i].addr;
695 msg.dt.sg_list[j].len = sgl[i].len;
699 sg_entries_sent += msg.dt.cur_sg_entries;
700 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries);
701 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
702 sizeof(msg.dt) - sizeof(msg.dt.sg_list) +
703 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries,
704 M_WAITOK) > CTL_HA_STATUS_SUCCESS) {
705 io->io_hdr.port_status = 31341;
706 io->scsiio.be_move_done(io);
709 msg.dt.sent_sg_entries = sg_entries_sent;
713 * Officially handover the request from us to peer.
714 * If failover has just happened, then we must return error.
715 * If failover happen just after, then it is not our problem.
718 mtx_lock(&lun->lun_lock);
719 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
721 mtx_unlock(&lun->lun_lock);
722 io->io_hdr.port_status = 31342;
723 io->scsiio.be_move_done(io);
726 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
727 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
729 mtx_unlock(&lun->lun_lock);
733 ctl_ha_done(union ctl_io *io)
735 union ctl_ha_msg msg;
737 if (io->io_hdr.io_type == CTL_IO_SCSI) {
738 memset(&msg, 0, sizeof(msg));
739 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
740 msg.hdr.original_sc = io->io_hdr.original_sc;
741 msg.hdr.nexus = io->io_hdr.nexus;
742 msg.hdr.status = io->io_hdr.status;
743 msg.scsi.scsi_status = io->scsiio.scsi_status;
744 msg.scsi.tag_num = io->scsiio.tag_num;
745 msg.scsi.tag_type = io->scsiio.tag_type;
746 msg.scsi.sense_len = io->scsiio.sense_len;
747 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
748 io->scsiio.sense_len);
749 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
750 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
751 msg.scsi.sense_len, M_WAITOK);
757 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
758 union ctl_ha_msg *msg_info)
760 struct ctl_scsiio *ctsio;
762 if (msg_info->hdr.original_sc == NULL) {
763 printf("%s: original_sc == NULL!\n", __func__);
764 /* XXX KDM now what? */
768 ctsio = &msg_info->hdr.original_sc->scsiio;
769 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
770 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
771 ctsio->io_hdr.status = msg_info->hdr.status;
772 ctsio->scsi_status = msg_info->scsi.scsi_status;
773 ctsio->sense_len = msg_info->scsi.sense_len;
774 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
775 msg_info->scsi.sense_len);
776 ctl_enqueue_isc((union ctl_io *)ctsio);
780 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
781 union ctl_ha_msg *msg_info)
783 struct ctl_scsiio *ctsio;
785 if (msg_info->hdr.serializing_sc == NULL) {
786 printf("%s: serializing_sc == NULL!\n", __func__);
787 /* XXX KDM now what? */
791 ctsio = &msg_info->hdr.serializing_sc->scsiio;
792 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
793 ctl_enqueue_isc((union ctl_io *)ctsio);
797 ctl_isc_announce_lun(struct ctl_lun *lun)
799 struct ctl_softc *softc = lun->ctl_softc;
800 union ctl_ha_msg *msg;
801 struct ctl_ha_msg_lun_pr_key pr_key;
804 if (softc->ha_link != CTL_HA_LINK_ONLINE)
806 mtx_lock(&lun->lun_lock);
807 i = sizeof(msg->lun);
809 i += lun->lun_devid->len;
810 i += sizeof(pr_key) * lun->pr_key_count;
812 mtx_unlock(&lun->lun_lock);
813 msg = malloc(i, M_CTL, M_WAITOK);
814 mtx_lock(&lun->lun_lock);
815 k = sizeof(msg->lun);
817 k += lun->lun_devid->len;
818 k += sizeof(pr_key) * lun->pr_key_count;
824 bzero(&msg->lun, sizeof(msg->lun));
825 msg->hdr.msg_type = CTL_MSG_LUN_SYNC;
826 msg->hdr.nexus.targ_lun = lun->lun;
827 msg->hdr.nexus.targ_mapped_lun = lun->lun;
828 msg->lun.flags = lun->flags;
829 msg->lun.pr_generation = lun->pr_generation;
830 msg->lun.pr_res_idx = lun->pr_res_idx;
831 msg->lun.pr_res_type = lun->pr_res_type;
832 msg->lun.pr_key_count = lun->pr_key_count;
834 if (lun->lun_devid) {
835 msg->lun.lun_devid_len = lun->lun_devid->len;
836 memcpy(&msg->lun.data[i], lun->lun_devid->data,
837 msg->lun.lun_devid_len);
838 i += msg->lun.lun_devid_len;
840 for (k = 0; k < CTL_MAX_INITIATORS; k++) {
841 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0)
844 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key));
847 mtx_unlock(&lun->lun_lock);
848 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
852 if (lun->flags & CTL_LUN_PRIMARY_SC) {
853 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
854 ctl_isc_announce_mode(lun, -1,
855 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
856 lun->mode_pages.index[i].subpage);
862 ctl_isc_announce_port(struct ctl_port *port)
864 struct ctl_softc *softc = port->ctl_softc;
865 union ctl_ha_msg *msg;
868 if (port->targ_port < softc->port_min ||
869 port->targ_port >= softc->port_max ||
870 softc->ha_link != CTL_HA_LINK_ONLINE)
872 i = sizeof(msg->port) + strlen(port->port_name) + 1;
874 i += port->lun_map_size * sizeof(uint32_t);
875 if (port->port_devid)
876 i += port->port_devid->len;
877 if (port->target_devid)
878 i += port->target_devid->len;
879 if (port->init_devid)
880 i += port->init_devid->len;
881 msg = malloc(i, M_CTL, M_WAITOK);
882 bzero(&msg->port, sizeof(msg->port));
883 msg->hdr.msg_type = CTL_MSG_PORT_SYNC;
884 msg->hdr.nexus.targ_port = port->targ_port;
885 msg->port.port_type = port->port_type;
886 msg->port.physical_port = port->physical_port;
887 msg->port.virtual_port = port->virtual_port;
888 msg->port.status = port->status;
890 msg->port.name_len = sprintf(&msg->port.data[i],
891 "%d:%s", softc->ha_id, port->port_name) + 1;
892 i += msg->port.name_len;
894 msg->port.lun_map_len = port->lun_map_size * sizeof(uint32_t);
895 memcpy(&msg->port.data[i], port->lun_map,
896 msg->port.lun_map_len);
897 i += msg->port.lun_map_len;
899 if (port->port_devid) {
900 msg->port.port_devid_len = port->port_devid->len;
901 memcpy(&msg->port.data[i], port->port_devid->data,
902 msg->port.port_devid_len);
903 i += msg->port.port_devid_len;
905 if (port->target_devid) {
906 msg->port.target_devid_len = port->target_devid->len;
907 memcpy(&msg->port.data[i], port->target_devid->data,
908 msg->port.target_devid_len);
909 i += msg->port.target_devid_len;
911 if (port->init_devid) {
912 msg->port.init_devid_len = port->init_devid->len;
913 memcpy(&msg->port.data[i], port->init_devid->data,
914 msg->port.init_devid_len);
915 i += msg->port.init_devid_len;
917 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
923 ctl_isc_announce_iid(struct ctl_port *port, int iid)
925 struct ctl_softc *softc = port->ctl_softc;
926 union ctl_ha_msg *msg;
929 if (port->targ_port < softc->port_min ||
930 port->targ_port >= softc->port_max ||
931 softc->ha_link != CTL_HA_LINK_ONLINE)
933 mtx_lock(&softc->ctl_lock);
934 i = sizeof(msg->iid);
936 if (port->wwpn_iid[iid].name)
937 l = strlen(port->wwpn_iid[iid].name) + 1;
939 msg = malloc(i, M_CTL, M_NOWAIT);
941 mtx_unlock(&softc->ctl_lock);
944 bzero(&msg->iid, sizeof(msg->iid));
945 msg->hdr.msg_type = CTL_MSG_IID_SYNC;
946 msg->hdr.nexus.targ_port = port->targ_port;
947 msg->hdr.nexus.initid = iid;
948 msg->iid.in_use = port->wwpn_iid[iid].in_use;
949 msg->iid.name_len = l;
950 msg->iid.wwpn = port->wwpn_iid[iid].wwpn;
951 if (port->wwpn_iid[iid].name)
952 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l);
953 mtx_unlock(&softc->ctl_lock);
954 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT);
959 ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx,
960 uint8_t page, uint8_t subpage)
962 struct ctl_softc *softc = lun->ctl_softc;
963 union ctl_ha_msg msg;
966 if (softc->ha_link != CTL_HA_LINK_ONLINE)
968 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
969 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
970 page && lun->mode_pages.index[i].subpage == subpage)
973 if (i == CTL_NUM_MODE_PAGES)
976 /* Don't try to replicate pages not present on this device. */
977 if (lun->mode_pages.index[i].page_data == NULL)
980 bzero(&msg.mode, sizeof(msg.mode));
981 msg.hdr.msg_type = CTL_MSG_MODE_SYNC;
982 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT;
983 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT;
984 msg.hdr.nexus.targ_lun = lun->lun;
985 msg.hdr.nexus.targ_mapped_lun = lun->lun;
986 msg.mode.page_code = page;
987 msg.mode.subpage = subpage;
988 msg.mode.page_len = lun->mode_pages.index[i].page_len;
989 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data,
991 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode),
996 ctl_isc_ha_link_up(struct ctl_softc *softc)
998 struct ctl_port *port;
1000 union ctl_ha_msg msg;
1003 /* Announce this node parameters to peer for validation. */
1004 msg.login.msg_type = CTL_MSG_LOGIN;
1005 msg.login.version = CTL_HA_VERSION;
1006 msg.login.ha_mode = softc->ha_mode;
1007 msg.login.ha_id = softc->ha_id;
1008 msg.login.max_luns = CTL_MAX_LUNS;
1009 msg.login.max_ports = CTL_MAX_PORTS;
1010 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT;
1011 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login),
1014 STAILQ_FOREACH(port, &softc->port_list, links) {
1015 ctl_isc_announce_port(port);
1016 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1017 if (port->wwpn_iid[i].in_use)
1018 ctl_isc_announce_iid(port, i);
1021 STAILQ_FOREACH(lun, &softc->lun_list, links)
1022 ctl_isc_announce_lun(lun);
1026 ctl_isc_ha_link_down(struct ctl_softc *softc)
1028 struct ctl_port *port;
1029 struct ctl_lun *lun;
1033 mtx_lock(&softc->ctl_lock);
1034 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1035 mtx_lock(&lun->lun_lock);
1036 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) {
1037 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1038 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1040 mtx_unlock(&lun->lun_lock);
1042 mtx_unlock(&softc->ctl_lock);
1043 io = ctl_alloc_io(softc->othersc_pool);
1044 mtx_lock(&softc->ctl_lock);
1046 io->io_hdr.msg_type = CTL_MSG_FAILOVER;
1047 io->io_hdr.nexus.targ_mapped_lun = lun->lun;
1048 ctl_enqueue_isc(io);
1051 STAILQ_FOREACH(port, &softc->port_list, links) {
1052 if (port->targ_port >= softc->port_min &&
1053 port->targ_port < softc->port_max)
1055 port->status &= ~CTL_PORT_STATUS_ONLINE;
1056 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1057 port->wwpn_iid[i].in_use = 0;
1058 free(port->wwpn_iid[i].name, M_CTL);
1059 port->wwpn_iid[i].name = NULL;
1062 mtx_unlock(&softc->ctl_lock);
1066 ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1068 struct ctl_lun *lun;
1069 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus);
1071 mtx_lock(&softc->ctl_lock);
1072 if (msg->hdr.nexus.targ_mapped_lun >= CTL_MAX_LUNS ||
1073 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) == NULL) {
1074 mtx_unlock(&softc->ctl_lock);
1077 mtx_lock(&lun->lun_lock);
1078 mtx_unlock(&softc->ctl_lock);
1079 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && msg->ua.ua_set)
1080 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8);
1081 if (msg->ua.ua_all) {
1083 ctl_est_ua_all(lun, iid, msg->ua.ua_type);
1085 ctl_clr_ua_all(lun, iid, msg->ua.ua_type);
1088 ctl_est_ua(lun, iid, msg->ua.ua_type);
1090 ctl_clr_ua(lun, iid, msg->ua.ua_type);
1092 mtx_unlock(&lun->lun_lock);
1096 ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1098 struct ctl_lun *lun;
1099 struct ctl_ha_msg_lun_pr_key pr_key;
1101 ctl_lun_flags oflags;
1104 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1105 mtx_lock(&softc->ctl_lock);
1106 if (targ_lun >= CTL_MAX_LUNS ||
1107 (lun = softc->ctl_luns[targ_lun]) == NULL) {
1108 mtx_unlock(&softc->ctl_lock);
1111 mtx_lock(&lun->lun_lock);
1112 mtx_unlock(&softc->ctl_lock);
1113 if (lun->flags & CTL_LUN_DISABLED) {
1114 mtx_unlock(&lun->lun_lock);
1117 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0;
1118 if (msg->lun.lun_devid_len != i || (i > 0 &&
1119 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) {
1120 mtx_unlock(&lun->lun_lock);
1121 printf("%s: Received conflicting HA LUN %d\n",
1122 __func__, targ_lun);
1125 /* Record whether peer is primary. */
1126 oflags = lun->flags;
1127 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) &&
1128 (msg->lun.flags & CTL_LUN_DISABLED) == 0)
1129 lun->flags |= CTL_LUN_PEER_SC_PRIMARY;
1131 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1132 if (oflags != lun->flags)
1133 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1135 /* If peer is primary and we are not -- use data */
1136 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
1137 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) {
1138 lun->pr_generation = msg->lun.pr_generation;
1139 lun->pr_res_idx = msg->lun.pr_res_idx;
1140 lun->pr_res_type = msg->lun.pr_res_type;
1141 lun->pr_key_count = msg->lun.pr_key_count;
1142 for (k = 0; k < CTL_MAX_INITIATORS; k++)
1143 ctl_clr_prkey(lun, k);
1144 for (k = 0; k < msg->lun.pr_key_count; k++) {
1145 memcpy(&pr_key, &msg->lun.data[i],
1147 ctl_alloc_prkey(lun, pr_key.pr_iid);
1148 ctl_set_prkey(lun, pr_key.pr_iid,
1150 i += sizeof(pr_key);
1154 mtx_unlock(&lun->lun_lock);
1155 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n",
1157 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ?
1158 "primary" : "secondary"));
1160 /* If we are primary but peer doesn't know -- notify */
1161 if ((lun->flags & CTL_LUN_PRIMARY_SC) &&
1162 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0)
1163 ctl_isc_announce_lun(lun);
1168 ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1170 struct ctl_port *port;
1171 struct ctl_lun *lun;
1174 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1176 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__,
1177 msg->hdr.nexus.targ_port));
1179 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO);
1180 port->frontend = &ha_frontend;
1181 port->targ_port = msg->hdr.nexus.targ_port;
1182 port->fe_datamove = ctl_ha_datamove;
1183 port->fe_done = ctl_ha_done;
1184 } else if (port->frontend == &ha_frontend) {
1185 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__,
1186 msg->hdr.nexus.targ_port));
1189 printf("%s: Received conflicting HA port %d\n",
1190 __func__, msg->hdr.nexus.targ_port);
1193 port->port_type = msg->port.port_type;
1194 port->physical_port = msg->port.physical_port;
1195 port->virtual_port = msg->port.virtual_port;
1196 port->status = msg->port.status;
1198 free(port->port_name, M_CTL);
1199 port->port_name = strndup(&msg->port.data[i], msg->port.name_len,
1201 i += msg->port.name_len;
1202 if (msg->port.lun_map_len != 0) {
1203 if (port->lun_map == NULL ||
1204 port->lun_map_size * sizeof(uint32_t) <
1205 msg->port.lun_map_len) {
1206 port->lun_map_size = 0;
1207 free(port->lun_map, M_CTL);
1208 port->lun_map = malloc(msg->port.lun_map_len,
1211 memcpy(port->lun_map, &msg->port.data[i], msg->port.lun_map_len);
1212 port->lun_map_size = msg->port.lun_map_len / sizeof(uint32_t);
1213 i += msg->port.lun_map_len;
1215 port->lun_map_size = 0;
1216 free(port->lun_map, M_CTL);
1217 port->lun_map = NULL;
1219 if (msg->port.port_devid_len != 0) {
1220 if (port->port_devid == NULL ||
1221 port->port_devid->len < msg->port.port_devid_len) {
1222 free(port->port_devid, M_CTL);
1223 port->port_devid = malloc(sizeof(struct ctl_devid) +
1224 msg->port.port_devid_len, M_CTL, M_WAITOK);
1226 memcpy(port->port_devid->data, &msg->port.data[i],
1227 msg->port.port_devid_len);
1228 port->port_devid->len = msg->port.port_devid_len;
1229 i += msg->port.port_devid_len;
1231 free(port->port_devid, M_CTL);
1232 port->port_devid = NULL;
1234 if (msg->port.target_devid_len != 0) {
1235 if (port->target_devid == NULL ||
1236 port->target_devid->len < msg->port.target_devid_len) {
1237 free(port->target_devid, M_CTL);
1238 port->target_devid = malloc(sizeof(struct ctl_devid) +
1239 msg->port.target_devid_len, M_CTL, M_WAITOK);
1241 memcpy(port->target_devid->data, &msg->port.data[i],
1242 msg->port.target_devid_len);
1243 port->target_devid->len = msg->port.target_devid_len;
1244 i += msg->port.target_devid_len;
1246 free(port->target_devid, M_CTL);
1247 port->target_devid = NULL;
1249 if (msg->port.init_devid_len != 0) {
1250 if (port->init_devid == NULL ||
1251 port->init_devid->len < msg->port.init_devid_len) {
1252 free(port->init_devid, M_CTL);
1253 port->init_devid = malloc(sizeof(struct ctl_devid) +
1254 msg->port.init_devid_len, M_CTL, M_WAITOK);
1256 memcpy(port->init_devid->data, &msg->port.data[i],
1257 msg->port.init_devid_len);
1258 port->init_devid->len = msg->port.init_devid_len;
1259 i += msg->port.init_devid_len;
1261 free(port->init_devid, M_CTL);
1262 port->init_devid = NULL;
1265 if (ctl_port_register(port) != 0) {
1266 printf("%s: ctl_port_register() failed with error\n",
1270 mtx_lock(&softc->ctl_lock);
1271 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1272 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
1274 mtx_lock(&lun->lun_lock);
1275 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
1276 mtx_unlock(&lun->lun_lock);
1278 mtx_unlock(&softc->ctl_lock);
1282 ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1284 struct ctl_port *port;
1287 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1289 printf("%s: Received IID for unknown port %d\n",
1290 __func__, msg->hdr.nexus.targ_port);
1293 iid = msg->hdr.nexus.initid;
1294 if (port->wwpn_iid[iid].in_use != 0 &&
1295 msg->iid.in_use == 0)
1296 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON);
1297 port->wwpn_iid[iid].in_use = msg->iid.in_use;
1298 port->wwpn_iid[iid].wwpn = msg->iid.wwpn;
1299 free(port->wwpn_iid[iid].name, M_CTL);
1300 if (msg->iid.name_len) {
1301 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0],
1302 msg->iid.name_len, M_CTL);
1304 port->wwpn_iid[iid].name = NULL;
1308 ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1311 if (msg->login.version != CTL_HA_VERSION) {
1312 printf("CTL HA peers have different versions %d != %d\n",
1313 msg->login.version, CTL_HA_VERSION);
1314 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1317 if (msg->login.ha_mode != softc->ha_mode) {
1318 printf("CTL HA peers have different ha_mode %d != %d\n",
1319 msg->login.ha_mode, softc->ha_mode);
1320 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1323 if (msg->login.ha_id == softc->ha_id) {
1324 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id);
1325 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1328 if (msg->login.max_luns != CTL_MAX_LUNS ||
1329 msg->login.max_ports != CTL_MAX_PORTS ||
1330 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) {
1331 printf("CTL HA peers have different limits\n");
1332 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1338 ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1340 struct ctl_lun *lun;
1342 uint32_t initidx, targ_lun;
1344 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1345 mtx_lock(&softc->ctl_lock);
1346 if (targ_lun >= CTL_MAX_LUNS ||
1347 (lun = softc->ctl_luns[targ_lun]) == NULL) {
1348 mtx_unlock(&softc->ctl_lock);
1351 mtx_lock(&lun->lun_lock);
1352 mtx_unlock(&softc->ctl_lock);
1353 if (lun->flags & CTL_LUN_DISABLED) {
1354 mtx_unlock(&lun->lun_lock);
1357 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
1358 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
1359 msg->mode.page_code &&
1360 lun->mode_pages.index[i].subpage == msg->mode.subpage)
1363 if (i == CTL_NUM_MODE_PAGES) {
1364 mtx_unlock(&lun->lun_lock);
1367 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data,
1368 lun->mode_pages.index[i].page_len);
1369 initidx = ctl_get_initindex(&msg->hdr.nexus);
1371 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
1372 mtx_unlock(&lun->lun_lock);
1376 * ISC (Inter Shelf Communication) event handler. Events from the HA
1377 * subsystem come in here.
1380 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
1382 struct ctl_softc *softc = control_softc;
1384 struct ctl_prio *presio;
1385 ctl_ha_status isc_status;
1387 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event));
1388 if (event == CTL_HA_EVT_MSG_RECV) {
1389 union ctl_ha_msg *msg, msgbuf;
1391 if (param > sizeof(msgbuf))
1392 msg = malloc(param, M_CTL, M_WAITOK);
1395 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param,
1397 if (isc_status != CTL_HA_STATUS_SUCCESS) {
1398 printf("%s: Error receiving message: %d\n",
1399 __func__, isc_status);
1405 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type));
1406 switch (msg->hdr.msg_type) {
1407 case CTL_MSG_SERIALIZE:
1408 io = ctl_alloc_io(softc->othersc_pool);
1410 // populate ctsio from msg
1411 io->io_hdr.io_type = CTL_IO_SCSI;
1412 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
1413 io->io_hdr.original_sc = msg->hdr.original_sc;
1414 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
1417 * If we're in serialization-only mode, we don't
1418 * want to go through full done processing. Thus
1421 * XXX KDM add another flag that is more specific.
1423 if (softc->ha_mode != CTL_HA_MODE_XFER)
1424 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
1425 io->io_hdr.nexus = msg->hdr.nexus;
1427 printf("port %u, iid %u, lun %u\n",
1428 io->io_hdr.nexus.targ_port,
1429 io->io_hdr.nexus.initid,
1430 io->io_hdr.nexus.targ_lun);
1432 io->scsiio.tag_num = msg->scsi.tag_num;
1433 io->scsiio.tag_type = msg->scsi.tag_type;
1435 io->io_hdr.start_time = time_uptime;
1436 getbinuptime(&io->io_hdr.start_bt);
1437 #endif /* CTL_TIME_IO */
1438 io->scsiio.cdb_len = msg->scsi.cdb_len;
1439 memcpy(io->scsiio.cdb, msg->scsi.cdb,
1441 if (softc->ha_mode == CTL_HA_MODE_XFER) {
1442 const struct ctl_cmd_entry *entry;
1444 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
1445 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
1447 entry->flags & CTL_FLAG_DATA_MASK;
1449 ctl_enqueue_isc(io);
1452 /* Performed on the Originating SC, XFER mode only */
1453 case CTL_MSG_DATAMOVE: {
1454 struct ctl_sg_entry *sgl;
1457 io = msg->hdr.original_sc;
1459 printf("%s: original_sc == NULL!\n", __func__);
1460 /* XXX KDM do something here */
1463 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
1464 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1466 * Keep track of this, we need to send it back over
1467 * when the datamove is complete.
1469 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1470 if (msg->hdr.status == CTL_SUCCESS)
1471 io->io_hdr.status = msg->hdr.status;
1473 if (msg->dt.sg_sequence == 0) {
1475 getbinuptime(&io->io_hdr.dma_start_bt);
1477 i = msg->dt.kern_sg_entries +
1478 msg->dt.kern_data_len /
1479 CTL_HA_DATAMOVE_SEGMENT + 1;
1480 sgl = malloc(sizeof(*sgl) * i, M_CTL,
1482 io->io_hdr.remote_sglist = sgl;
1483 io->io_hdr.local_sglist =
1484 &sgl[msg->dt.kern_sg_entries];
1486 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
1488 io->scsiio.kern_sg_entries =
1489 msg->dt.kern_sg_entries;
1490 io->scsiio.rem_sg_entries =
1491 msg->dt.kern_sg_entries;
1492 io->scsiio.kern_data_len =
1493 msg->dt.kern_data_len;
1494 io->scsiio.kern_total_len =
1495 msg->dt.kern_total_len;
1496 io->scsiio.kern_data_resid =
1497 msg->dt.kern_data_resid;
1498 io->scsiio.kern_rel_offset =
1499 msg->dt.kern_rel_offset;
1500 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR;
1501 io->io_hdr.flags |= msg->dt.flags &
1504 sgl = (struct ctl_sg_entry *)
1505 io->scsiio.kern_data_ptr;
1507 for (i = msg->dt.sent_sg_entries, j = 0;
1508 i < (msg->dt.sent_sg_entries +
1509 msg->dt.cur_sg_entries); i++, j++) {
1510 sgl[i].addr = msg->dt.sg_list[j].addr;
1511 sgl[i].len = msg->dt.sg_list[j].len;
1514 printf("%s: DATAMOVE: %p,%lu j=%d, i=%d\n",
1515 __func__, sgl[i].addr, sgl[i].len, j, i);
1520 * If this is the last piece of the I/O, we've got
1521 * the full S/G list. Queue processing in the thread.
1522 * Otherwise wait for the next piece.
1524 if (msg->dt.sg_last != 0)
1525 ctl_enqueue_isc(io);
1528 /* Performed on the Serializing (primary) SC, XFER mode only */
1529 case CTL_MSG_DATAMOVE_DONE: {
1530 if (msg->hdr.serializing_sc == NULL) {
1531 printf("%s: serializing_sc == NULL!\n",
1533 /* XXX KDM now what? */
1537 * We grab the sense information here in case
1538 * there was a failure, so we can return status
1539 * back to the initiator.
1541 io = msg->hdr.serializing_sc;
1542 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
1543 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1544 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1545 io->io_hdr.port_status = msg->scsi.port_status;
1546 io->scsiio.kern_data_resid = msg->scsi.kern_data_resid;
1547 if (msg->hdr.status != CTL_STATUS_NONE) {
1548 io->io_hdr.status = msg->hdr.status;
1549 io->scsiio.scsi_status = msg->scsi.scsi_status;
1550 io->scsiio.sense_len = msg->scsi.sense_len;
1551 memcpy(&io->scsiio.sense_data,
1552 &msg->scsi.sense_data,
1553 msg->scsi.sense_len);
1554 if (msg->hdr.status == CTL_SUCCESS)
1555 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1557 ctl_enqueue_isc(io);
1561 /* Preformed on Originating SC, SER_ONLY mode */
1563 io = msg->hdr.original_sc;
1565 printf("%s: original_sc == NULL!\n",
1569 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1570 io->io_hdr.msg_type = CTL_MSG_R2R;
1571 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1572 ctl_enqueue_isc(io);
1576 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
1578 * Performed on the Originating (i.e. secondary) SC in XFER
1581 case CTL_MSG_FINISH_IO:
1582 if (softc->ha_mode == CTL_HA_MODE_XFER)
1583 ctl_isc_handler_finish_xfer(softc, msg);
1585 ctl_isc_handler_finish_ser_only(softc, msg);
1588 /* Preformed on Originating SC */
1589 case CTL_MSG_BAD_JUJU:
1590 io = msg->hdr.original_sc;
1592 printf("%s: Bad JUJU!, original_sc is NULL!\n",
1596 ctl_copy_sense_data(msg, io);
1598 * IO should have already been cleaned up on other
1599 * SC so clear this flag so we won't send a message
1600 * back to finish the IO there.
1602 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
1603 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1605 /* io = msg->hdr.serializing_sc; */
1606 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
1607 ctl_enqueue_isc(io);
1610 /* Handle resets sent from the other side */
1611 case CTL_MSG_MANAGE_TASKS: {
1612 struct ctl_taskio *taskio;
1613 taskio = (struct ctl_taskio *)ctl_alloc_io(
1614 softc->othersc_pool);
1615 ctl_zero_io((union ctl_io *)taskio);
1616 taskio->io_hdr.io_type = CTL_IO_TASK;
1617 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1618 taskio->io_hdr.nexus = msg->hdr.nexus;
1619 taskio->task_action = msg->task.task_action;
1620 taskio->tag_num = msg->task.tag_num;
1621 taskio->tag_type = msg->task.tag_type;
1623 taskio->io_hdr.start_time = time_uptime;
1624 getbinuptime(&taskio->io_hdr.start_bt);
1625 #endif /* CTL_TIME_IO */
1626 ctl_run_task((union ctl_io *)taskio);
1629 /* Persistent Reserve action which needs attention */
1630 case CTL_MSG_PERS_ACTION:
1631 presio = (struct ctl_prio *)ctl_alloc_io(
1632 softc->othersc_pool);
1633 ctl_zero_io((union ctl_io *)presio);
1634 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
1635 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1636 presio->io_hdr.nexus = msg->hdr.nexus;
1637 presio->pr_msg = msg->pr;
1638 ctl_enqueue_isc((union ctl_io *)presio);
1641 ctl_isc_ua(softc, msg, param);
1643 case CTL_MSG_PORT_SYNC:
1644 ctl_isc_port_sync(softc, msg, param);
1646 case CTL_MSG_LUN_SYNC:
1647 ctl_isc_lun_sync(softc, msg, param);
1649 case CTL_MSG_IID_SYNC:
1650 ctl_isc_iid_sync(softc, msg, param);
1653 ctl_isc_login(softc, msg, param);
1655 case CTL_MSG_MODE_SYNC:
1656 ctl_isc_mode_sync(softc, msg, param);
1659 printf("Received HA message of unknown type %d\n",
1661 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1666 } else if (event == CTL_HA_EVT_LINK_CHANGE) {
1667 printf("CTL: HA link status changed from %d to %d\n",
1668 softc->ha_link, param);
1669 if (param == softc->ha_link)
1671 if (softc->ha_link == CTL_HA_LINK_ONLINE) {
1672 softc->ha_link = param;
1673 ctl_isc_ha_link_down(softc);
1675 softc->ha_link = param;
1676 if (softc->ha_link == CTL_HA_LINK_ONLINE)
1677 ctl_isc_ha_link_up(softc);
1681 printf("ctl_isc_event_handler: Unknown event %d\n", event);
1687 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
1690 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data,
1691 src->scsi.sense_len);
1692 dest->scsiio.scsi_status = src->scsi.scsi_status;
1693 dest->scsiio.sense_len = src->scsi.sense_len;
1694 dest->io_hdr.status = src->hdr.status;
1698 ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest)
1701 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data,
1702 src->scsiio.sense_len);
1703 dest->scsi.scsi_status = src->scsiio.scsi_status;
1704 dest->scsi.sense_len = src->scsiio.sense_len;
1705 dest->hdr.status = src->io_hdr.status;
1709 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1711 struct ctl_softc *softc = lun->ctl_softc;
1714 if (initidx < softc->init_min || initidx >= softc->init_max)
1716 mtx_assert(&lun->lun_lock, MA_OWNED);
1717 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1720 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua;
1724 ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua)
1728 mtx_assert(&lun->lun_lock, MA_OWNED);
1729 if (lun->pending_ua[port] == NULL)
1731 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1732 if (port * CTL_MAX_INIT_PER_PORT + i == except)
1734 lun->pending_ua[port][i] |= ua;
1739 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1741 struct ctl_softc *softc = lun->ctl_softc;
1744 mtx_assert(&lun->lun_lock, MA_OWNED);
1745 for (i = softc->port_min; i < softc->port_max; i++)
1746 ctl_est_ua_port(lun, i, except, ua);
1750 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1752 struct ctl_softc *softc = lun->ctl_softc;
1755 if (initidx < softc->init_min || initidx >= softc->init_max)
1757 mtx_assert(&lun->lun_lock, MA_OWNED);
1758 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1761 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua;
1765 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1767 struct ctl_softc *softc = lun->ctl_softc;
1770 mtx_assert(&lun->lun_lock, MA_OWNED);
1771 for (i = softc->port_min; i < softc->port_max; i++) {
1772 if (lun->pending_ua[i] == NULL)
1774 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
1775 if (i * CTL_MAX_INIT_PER_PORT + j == except)
1777 lun->pending_ua[i][j] &= ~ua;
1783 ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx,
1784 ctl_ua_type ua_type)
1786 struct ctl_lun *lun;
1788 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
1789 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) {
1790 mtx_lock(&lun->lun_lock);
1791 ctl_clr_ua(lun, initidx, ua_type);
1792 mtx_unlock(&lun->lun_lock);
1797 ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS)
1799 struct ctl_softc *softc = (struct ctl_softc *)arg1;
1800 struct ctl_lun *lun;
1801 struct ctl_lun_req ireq;
1804 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1;
1805 error = sysctl_handle_int(oidp, &value, 0, req);
1806 if ((error != 0) || (req->newptr == NULL))
1809 mtx_lock(&softc->ctl_lock);
1811 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1813 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
1814 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1815 mtx_unlock(&softc->ctl_lock);
1816 bzero(&ireq, sizeof(ireq));
1817 ireq.reqtype = CTL_LUNREQ_MODIFY;
1818 ireq.reqdata.modify.lun_id = lun->lun;
1819 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0,
1821 if (ireq.status != CTL_LUN_OK) {
1822 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n",
1823 __func__, ireq.status, ireq.error_str);
1825 mtx_lock(&softc->ctl_lock);
1827 mtx_unlock(&softc->ctl_lock);
1834 struct make_dev_args args;
1835 struct ctl_softc *softc;
1838 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1841 make_dev_args_init(&args);
1842 args.mda_devsw = &ctl_cdevsw;
1843 args.mda_uid = UID_ROOT;
1844 args.mda_gid = GID_OPERATOR;
1845 args.mda_mode = 0600;
1846 args.mda_si_drv1 = softc;
1847 error = make_dev_s(&args, &softc->dev, "cam/ctl");
1849 free(softc, M_DEVBUF);
1850 control_softc = NULL;
1854 sysctl_ctx_init(&softc->sysctl_ctx);
1855 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1856 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1857 CTLFLAG_RD, 0, "CAM Target Layer");
1859 if (softc->sysctl_tree == NULL) {
1860 printf("%s: unable to allocate sysctl tree\n", __func__);
1861 destroy_dev(softc->dev);
1862 free(softc, M_DEVBUF);
1863 control_softc = NULL;
1867 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1868 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
1869 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
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 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1882 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1883 "HA head ID (0 - no HA)");
1884 if (softc->ha_id == 0 || softc->ha_id > NUM_HA_SHELVES) {
1885 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1886 softc->is_single = 1;
1887 softc->port_cnt = CTL_MAX_PORTS;
1888 softc->port_min = 0;
1890 softc->port_cnt = CTL_MAX_PORTS / NUM_HA_SHELVES;
1891 softc->port_min = (softc->ha_id - 1) * softc->port_cnt;
1893 softc->port_max = softc->port_min + softc->port_cnt;
1894 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT;
1895 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT;
1897 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1898 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0,
1899 "HA link state (0 - offline, 1 - unknown, 2 - online)");
1901 STAILQ_INIT(&softc->lun_list);
1902 STAILQ_INIT(&softc->pending_lun_queue);
1903 STAILQ_INIT(&softc->fe_list);
1904 STAILQ_INIT(&softc->port_list);
1905 STAILQ_INIT(&softc->be_list);
1906 ctl_tpc_init(softc);
1908 if (worker_threads <= 0)
1909 worker_threads = max(1, mp_ncpus / 4);
1910 if (worker_threads > CTL_MAX_THREADS)
1911 worker_threads = CTL_MAX_THREADS;
1913 for (i = 0; i < worker_threads; i++) {
1914 struct ctl_thread *thr = &softc->threads[i];
1916 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1917 thr->ctl_softc = softc;
1918 STAILQ_INIT(&thr->incoming_queue);
1919 STAILQ_INIT(&thr->rtr_queue);
1920 STAILQ_INIT(&thr->done_queue);
1921 STAILQ_INIT(&thr->isc_queue);
1923 error = kproc_kthread_add(ctl_work_thread, thr,
1924 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1926 printf("error creating CTL work thread!\n");
1930 error = kproc_kthread_add(ctl_lun_thread, softc,
1931 &softc->ctl_proc, &softc->lun_thread, 0, 0, "ctl", "lun");
1933 printf("error creating CTL lun thread!\n");
1936 error = kproc_kthread_add(ctl_thresh_thread, softc,
1937 &softc->ctl_proc, &softc->thresh_thread, 0, 0, "ctl", "thresh");
1939 printf("error creating CTL threshold thread!\n");
1943 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1944 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN,
1945 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head");
1947 if (softc->is_single == 0) {
1948 if (ctl_frontend_register(&ha_frontend) != 0)
1949 softc->is_single = 1;
1957 struct ctl_softc *softc = control_softc;
1960 if (softc->is_single == 0)
1961 ctl_frontend_deregister(&ha_frontend);
1963 destroy_dev(softc->dev);
1965 /* Shutdown CTL threads. */
1966 softc->shutdown = 1;
1967 for (i = 0; i < worker_threads; i++) {
1968 struct ctl_thread *thr = &softc->threads[i];
1969 while (thr->thread != NULL) {
1971 if (thr->thread != NULL)
1972 pause("CTL thr shutdown", 1);
1974 mtx_destroy(&thr->queue_lock);
1976 while (softc->lun_thread != NULL) {
1977 wakeup(&softc->pending_lun_queue);
1978 if (softc->lun_thread != NULL)
1979 pause("CTL thr shutdown", 1);
1981 while (softc->thresh_thread != NULL) {
1982 wakeup(softc->thresh_thread);
1983 if (softc->thresh_thread != NULL)
1984 pause("CTL thr shutdown", 1);
1987 ctl_tpc_shutdown(softc);
1988 uma_zdestroy(softc->io_zone);
1989 mtx_destroy(&softc->ctl_lock);
1991 sysctl_ctx_free(&softc->sysctl_ctx);
1993 free(softc, M_DEVBUF);
1994 control_softc = NULL;
1999 ctl_module_event_handler(module_t mod, int what, void *arg)
2004 return (ctl_init());
2006 return (ctl_shutdown());
2008 return (EOPNOTSUPP);
2013 * XXX KDM should we do some access checks here? Bump a reference count to
2014 * prevent a CTL module from being unloaded while someone has it open?
2017 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
2023 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
2029 * Remove an initiator by port number and initiator ID.
2030 * Returns 0 for success, -1 for failure.
2033 ctl_remove_initiator(struct ctl_port *port, int iid)
2035 struct ctl_softc *softc = port->ctl_softc;
2038 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2040 if (iid > CTL_MAX_INIT_PER_PORT) {
2041 printf("%s: initiator ID %u > maximun %u!\n",
2042 __func__, iid, CTL_MAX_INIT_PER_PORT);
2046 mtx_lock(&softc->ctl_lock);
2047 last = (--port->wwpn_iid[iid].in_use == 0);
2048 port->wwpn_iid[iid].last_use = time_uptime;
2049 mtx_unlock(&softc->ctl_lock);
2051 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON);
2052 ctl_isc_announce_iid(port, iid);
2058 * Add an initiator to the initiator map.
2059 * Returns iid for success, < 0 for failure.
2062 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
2064 struct ctl_softc *softc = port->ctl_softc;
2068 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2070 if (iid >= CTL_MAX_INIT_PER_PORT) {
2071 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
2072 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
2077 mtx_lock(&softc->ctl_lock);
2079 if (iid < 0 && (wwpn != 0 || name != NULL)) {
2080 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2081 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
2085 if (name != NULL && port->wwpn_iid[i].name != NULL &&
2086 strcmp(name, port->wwpn_iid[i].name) == 0) {
2094 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2095 if (port->wwpn_iid[i].in_use == 0 &&
2096 port->wwpn_iid[i].wwpn == 0 &&
2097 port->wwpn_iid[i].name == NULL) {
2106 best_time = INT32_MAX;
2107 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2108 if (port->wwpn_iid[i].in_use == 0) {
2109 if (port->wwpn_iid[i].last_use < best_time) {
2111 best_time = port->wwpn_iid[i].last_use;
2119 mtx_unlock(&softc->ctl_lock);
2124 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
2126 * This is not an error yet.
2128 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
2130 printf("%s: port %d iid %u WWPN %#jx arrived"
2131 " again\n", __func__, port->targ_port,
2132 iid, (uintmax_t)wwpn);
2136 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
2137 strcmp(name, port->wwpn_iid[iid].name) == 0) {
2139 printf("%s: port %d iid %u name '%s' arrived"
2140 " again\n", __func__, port->targ_port,
2147 * This is an error, but what do we do about it? The
2148 * driver is telling us we have a new WWPN for this
2149 * initiator ID, so we pretty much need to use it.
2151 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
2152 " but WWPN %#jx '%s' is still at that address\n",
2153 __func__, port->targ_port, iid, wwpn, name,
2154 (uintmax_t)port->wwpn_iid[iid].wwpn,
2155 port->wwpn_iid[iid].name);
2158 free(port->wwpn_iid[iid].name, M_CTL);
2159 port->wwpn_iid[iid].name = name;
2160 port->wwpn_iid[iid].wwpn = wwpn;
2161 port->wwpn_iid[iid].in_use++;
2162 mtx_unlock(&softc->ctl_lock);
2163 ctl_isc_announce_iid(port, iid);
2169 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
2173 switch (port->port_type) {
2176 struct scsi_transportid_fcp *id =
2177 (struct scsi_transportid_fcp *)buf;
2178 if (port->wwpn_iid[iid].wwpn == 0)
2180 memset(id, 0, sizeof(*id));
2181 id->format_protocol = SCSI_PROTO_FC;
2182 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
2183 return (sizeof(*id));
2185 case CTL_PORT_ISCSI:
2187 struct scsi_transportid_iscsi_port *id =
2188 (struct scsi_transportid_iscsi_port *)buf;
2189 if (port->wwpn_iid[iid].name == NULL)
2192 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
2194 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
2195 len = roundup2(min(len, 252), 4);
2196 scsi_ulto2b(len, id->additional_length);
2197 return (sizeof(*id) + len);
2201 struct scsi_transportid_sas *id =
2202 (struct scsi_transportid_sas *)buf;
2203 if (port->wwpn_iid[iid].wwpn == 0)
2205 memset(id, 0, sizeof(*id));
2206 id->format_protocol = SCSI_PROTO_SAS;
2207 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
2208 return (sizeof(*id));
2212 struct scsi_transportid_spi *id =
2213 (struct scsi_transportid_spi *)buf;
2214 memset(id, 0, sizeof(*id));
2215 id->format_protocol = SCSI_PROTO_SPI;
2216 scsi_ulto2b(iid, id->scsi_addr);
2217 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
2218 return (sizeof(*id));
2224 * Serialize a command that went down the "wrong" side, and so was sent to
2225 * this controller for execution. The logic is a little different than the
2226 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
2227 * sent back to the other side, but in the success case, we execute the
2228 * command on this side (XFER mode) or tell the other side to execute it
2232 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
2234 struct ctl_softc *softc = CTL_SOFTC(ctsio);
2235 struct ctl_port *port = CTL_PORT(ctsio);
2236 union ctl_ha_msg msg_info;
2237 struct ctl_lun *lun;
2238 const struct ctl_cmd_entry *entry;
2241 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
2243 /* Make sure that we know about this port. */
2244 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) {
2245 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2250 /* Make sure that we know about this LUN. */
2251 mtx_lock(&softc->ctl_lock);
2252 if (targ_lun >= CTL_MAX_LUNS ||
2253 (lun = softc->ctl_luns[targ_lun]) == NULL) {
2254 mtx_unlock(&softc->ctl_lock);
2257 * The other node would not send this request to us unless
2258 * received announce that we are primary node for this LUN.
2259 * If this LUN does not exist now, it is probably result of
2260 * a race, so respond to initiator in the most opaque way.
2262 ctl_set_busy(ctsio);
2265 mtx_lock(&lun->lun_lock);
2266 mtx_unlock(&softc->ctl_lock);
2269 * If the LUN is invalid, pretend that it doesn't exist.
2270 * It will go away as soon as all pending I/Os completed.
2272 if (lun->flags & CTL_LUN_DISABLED) {
2273 mtx_unlock(&lun->lun_lock);
2274 ctl_set_busy(ctsio);
2278 entry = ctl_get_cmd_entry(ctsio, NULL);
2279 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
2280 mtx_unlock(&lun->lun_lock);
2284 CTL_LUN(ctsio) = lun;
2285 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
2288 * Every I/O goes into the OOA queue for a
2289 * particular LUN, and stays there until completion.
2292 if (TAILQ_EMPTY(&lun->ooa_queue))
2293 lun->idle_time += getsbinuptime() - lun->last_busy;
2295 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2297 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
2298 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
2300 case CTL_ACTION_BLOCK:
2301 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
2302 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
2304 mtx_unlock(&lun->lun_lock);
2306 case CTL_ACTION_PASS:
2307 case CTL_ACTION_SKIP:
2308 if (softc->ha_mode == CTL_HA_MODE_XFER) {
2309 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
2310 ctl_enqueue_rtr((union ctl_io *)ctsio);
2311 mtx_unlock(&lun->lun_lock);
2313 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
2314 mtx_unlock(&lun->lun_lock);
2316 /* send msg back to other side */
2317 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2318 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
2319 msg_info.hdr.msg_type = CTL_MSG_R2R;
2320 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2321 sizeof(msg_info.hdr), M_WAITOK);
2324 case CTL_ACTION_OVERLAP:
2325 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2326 mtx_unlock(&lun->lun_lock);
2327 ctl_set_overlapped_cmd(ctsio);
2329 case CTL_ACTION_OVERLAP_TAG:
2330 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2331 mtx_unlock(&lun->lun_lock);
2332 ctl_set_overlapped_tag(ctsio, ctsio->tag_num);
2334 case CTL_ACTION_ERROR:
2336 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2337 mtx_unlock(&lun->lun_lock);
2339 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2342 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
2343 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2344 msg_info.hdr.serializing_sc = NULL;
2345 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
2346 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2347 sizeof(msg_info.scsi), M_WAITOK);
2348 ctl_free_io((union ctl_io *)ctsio);
2354 * Returns 0 for success, errno for failure.
2357 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2358 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2362 mtx_lock(&lun->lun_lock);
2363 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2364 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2366 struct ctl_ooa_entry *entry;
2369 * If we've got more than we can fit, just count the
2370 * remaining entries.
2372 if (*cur_fill_num >= ooa_hdr->alloc_num)
2375 entry = &kern_entries[*cur_fill_num];
2377 entry->tag_num = io->scsiio.tag_num;
2378 entry->lun_num = lun->lun;
2380 entry->start_bt = io->io_hdr.start_bt;
2382 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2383 entry->cdb_len = io->scsiio.cdb_len;
2384 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2385 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2387 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2388 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2390 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2391 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2393 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2394 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2396 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2397 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2399 mtx_unlock(&lun->lun_lock);
2403 ctl_copyin_alloc(void *user_addr, unsigned int len, char *error_str,
2404 size_t error_str_len)
2408 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2410 if (copyin(user_addr, kptr, len) != 0) {
2411 snprintf(error_str, error_str_len, "Error copying %d bytes "
2412 "from user address %p to kernel address %p", len,
2422 ctl_free_args(int num_args, struct ctl_be_arg *args)
2429 for (i = 0; i < num_args; i++) {
2430 free(args[i].kname, M_CTL);
2431 free(args[i].kvalue, M_CTL);
2437 static struct ctl_be_arg *
2438 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
2439 char *error_str, size_t error_str_len)
2441 struct ctl_be_arg *args;
2444 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
2445 error_str, error_str_len);
2450 for (i = 0; i < num_args; i++) {
2451 args[i].kname = NULL;
2452 args[i].kvalue = NULL;
2455 for (i = 0; i < num_args; i++) {
2458 if (args[i].namelen == 0) {
2459 snprintf(error_str, error_str_len, "Argument %d "
2460 "name length is zero", i);
2464 args[i].kname = ctl_copyin_alloc(args[i].name,
2465 args[i].namelen, error_str, error_str_len);
2466 if (args[i].kname == NULL)
2469 if (args[i].kname[args[i].namelen - 1] != '\0') {
2470 snprintf(error_str, error_str_len, "Argument %d "
2471 "name is not NUL-terminated", i);
2475 if (args[i].flags & CTL_BEARG_RD) {
2476 if (args[i].vallen == 0) {
2477 snprintf(error_str, error_str_len, "Argument %d "
2478 "value length is zero", i);
2482 tmpptr = ctl_copyin_alloc(args[i].value,
2483 args[i].vallen, error_str, error_str_len);
2487 if ((args[i].flags & CTL_BEARG_ASCII)
2488 && (tmpptr[args[i].vallen - 1] != '\0')) {
2489 snprintf(error_str, error_str_len, "Argument "
2490 "%d value is not NUL-terminated", i);
2491 free(tmpptr, M_CTL);
2494 args[i].kvalue = tmpptr;
2496 args[i].kvalue = malloc(args[i].vallen,
2497 M_CTL, M_WAITOK | M_ZERO);
2504 ctl_free_args(num_args, args);
2510 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2514 for (i = 0; i < num_args; i++) {
2515 if (args[i].flags & CTL_BEARG_WR)
2516 copyout(args[i].kvalue, args[i].value, args[i].vallen);
2521 * Escape characters that are illegal or not recommended in XML.
2524 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2526 char *end = str + size;
2531 for (; *str && str < end; str++) {
2534 retval = sbuf_printf(sb, "&");
2537 retval = sbuf_printf(sb, ">");
2540 retval = sbuf_printf(sb, "<");
2543 retval = sbuf_putc(sb, *str);
2556 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2558 struct scsi_vpd_id_descriptor *desc;
2561 if (id == NULL || id->len < 4)
2563 desc = (struct scsi_vpd_id_descriptor *)id->data;
2564 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2565 case SVPD_ID_TYPE_T10:
2566 sbuf_printf(sb, "t10.");
2568 case SVPD_ID_TYPE_EUI64:
2569 sbuf_printf(sb, "eui.");
2571 case SVPD_ID_TYPE_NAA:
2572 sbuf_printf(sb, "naa.");
2574 case SVPD_ID_TYPE_SCSI_NAME:
2577 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2578 case SVPD_ID_CODESET_BINARY:
2579 for (i = 0; i < desc->length; i++)
2580 sbuf_printf(sb, "%02x", desc->identifier[i]);
2582 case SVPD_ID_CODESET_ASCII:
2583 sbuf_printf(sb, "%.*s", (int)desc->length,
2584 (char *)desc->identifier);
2586 case SVPD_ID_CODESET_UTF8:
2587 sbuf_printf(sb, "%s", (char *)desc->identifier);
2593 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2596 struct ctl_softc *softc = dev->si_drv1;
2597 struct ctl_port *port;
2598 struct ctl_lun *lun;
2605 retval = ctl_ioctl_io(dev, cmd, addr, flag, td);
2607 case CTL_ENABLE_PORT:
2608 case CTL_DISABLE_PORT:
2609 case CTL_SET_PORT_WWNS: {
2610 struct ctl_port *port;
2611 struct ctl_port_entry *entry;
2613 entry = (struct ctl_port_entry *)addr;
2615 mtx_lock(&softc->ctl_lock);
2616 STAILQ_FOREACH(port, &softc->port_list, links) {
2619 if (port->targ_port < softc->port_min ||
2620 port->targ_port >= softc->port_max)
2625 if ((entry->port_type == CTL_PORT_NONE)
2626 && (entry->targ_port == port->targ_port)) {
2628 * If the user only wants to enable or
2629 * disable or set WWNs on a specific port,
2630 * do the operation and we're done.
2634 } else if (entry->port_type & port->port_type) {
2636 * Compare the user's type mask with the
2637 * particular frontend type to see if we
2644 * Make sure the user isn't trying to set
2645 * WWNs on multiple ports at the same time.
2647 if (cmd == CTL_SET_PORT_WWNS) {
2648 printf("%s: Can't set WWNs on "
2649 "multiple ports\n", __func__);
2658 * XXX KDM we have to drop the lock here, because
2659 * the online/offline operations can potentially
2660 * block. We need to reference count the frontends
2661 * so they can't go away,
2663 if (cmd == CTL_ENABLE_PORT) {
2664 mtx_unlock(&softc->ctl_lock);
2665 ctl_port_online(port);
2666 mtx_lock(&softc->ctl_lock);
2667 } else if (cmd == CTL_DISABLE_PORT) {
2668 mtx_unlock(&softc->ctl_lock);
2669 ctl_port_offline(port);
2670 mtx_lock(&softc->ctl_lock);
2671 } else if (cmd == CTL_SET_PORT_WWNS) {
2672 ctl_port_set_wwns(port,
2673 (entry->flags & CTL_PORT_WWNN_VALID) ?
2675 (entry->flags & CTL_PORT_WWPN_VALID) ?
2676 1 : 0, entry->wwpn);
2681 mtx_unlock(&softc->ctl_lock);
2685 struct ctl_ooa *ooa_hdr;
2686 struct ctl_ooa_entry *entries;
2687 uint32_t cur_fill_num;
2689 ooa_hdr = (struct ctl_ooa *)addr;
2691 if ((ooa_hdr->alloc_len == 0)
2692 || (ooa_hdr->alloc_num == 0)) {
2693 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2694 "must be non-zero\n", __func__,
2695 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2700 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2701 sizeof(struct ctl_ooa_entry))) {
2702 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2703 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2704 __func__, ooa_hdr->alloc_len,
2705 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2710 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2711 if (entries == NULL) {
2712 printf("%s: could not allocate %d bytes for OOA "
2713 "dump\n", __func__, ooa_hdr->alloc_len);
2718 mtx_lock(&softc->ctl_lock);
2719 if ((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0 &&
2720 (ooa_hdr->lun_num >= CTL_MAX_LUNS ||
2721 softc->ctl_luns[ooa_hdr->lun_num] == NULL)) {
2722 mtx_unlock(&softc->ctl_lock);
2723 free(entries, M_CTL);
2724 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2725 __func__, (uintmax_t)ooa_hdr->lun_num);
2732 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2733 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2734 ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2738 lun = softc->ctl_luns[ooa_hdr->lun_num];
2739 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr,
2742 mtx_unlock(&softc->ctl_lock);
2744 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2745 ooa_hdr->fill_len = ooa_hdr->fill_num *
2746 sizeof(struct ctl_ooa_entry);
2747 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2749 printf("%s: error copying out %d bytes for OOA dump\n",
2750 __func__, ooa_hdr->fill_len);
2753 getbinuptime(&ooa_hdr->cur_bt);
2755 if (cur_fill_num > ooa_hdr->alloc_num) {
2756 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2757 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2759 ooa_hdr->dropped_num = 0;
2760 ooa_hdr->status = CTL_OOA_OK;
2763 free(entries, M_CTL);
2766 case CTL_DELAY_IO: {
2767 struct ctl_io_delay_info *delay_info;
2769 delay_info = (struct ctl_io_delay_info *)addr;
2772 mtx_lock(&softc->ctl_lock);
2773 if (delay_info->lun_id >= CTL_MAX_LUNS ||
2774 (lun = softc->ctl_luns[delay_info->lun_id]) == NULL) {
2775 mtx_unlock(&softc->ctl_lock);
2776 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2779 mtx_lock(&lun->lun_lock);
2780 mtx_unlock(&softc->ctl_lock);
2781 delay_info->status = CTL_DELAY_STATUS_OK;
2782 switch (delay_info->delay_type) {
2783 case CTL_DELAY_TYPE_CONT:
2784 case CTL_DELAY_TYPE_ONESHOT:
2787 delay_info->status = CTL_DELAY_STATUS_INVALID_TYPE;
2790 switch (delay_info->delay_loc) {
2791 case CTL_DELAY_LOC_DATAMOVE:
2792 lun->delay_info.datamove_type = delay_info->delay_type;
2793 lun->delay_info.datamove_delay = delay_info->delay_secs;
2795 case CTL_DELAY_LOC_DONE:
2796 lun->delay_info.done_type = delay_info->delay_type;
2797 lun->delay_info.done_delay = delay_info->delay_secs;
2800 delay_info->status = CTL_DELAY_STATUS_INVALID_LOC;
2803 mtx_unlock(&lun->lun_lock);
2805 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2806 #endif /* CTL_IO_DELAY */
2809 #ifdef CTL_LEGACY_STATS
2810 case CTL_GETSTATS: {
2811 struct ctl_stats *stats = (struct ctl_stats *)addr;
2815 * XXX KDM no locking here. If the LUN list changes,
2816 * things can blow up.
2819 stats->status = CTL_SS_OK;
2820 stats->fill_len = 0;
2821 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2822 if (stats->fill_len + sizeof(lun->legacy_stats) >
2824 stats->status = CTL_SS_NEED_MORE_SPACE;
2827 retval = copyout(&lun->legacy_stats, &stats->lun_stats[i++],
2828 sizeof(lun->legacy_stats));
2831 stats->fill_len += sizeof(lun->legacy_stats);
2833 stats->num_luns = softc->num_luns;
2834 stats->flags = CTL_STATS_FLAG_NONE;
2836 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
2838 getnanouptime(&stats->timestamp);
2841 #endif /* CTL_LEGACY_STATS */
2842 case CTL_ERROR_INJECT: {
2843 struct ctl_error_desc *err_desc, *new_err_desc;
2845 err_desc = (struct ctl_error_desc *)addr;
2847 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2849 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2851 mtx_lock(&softc->ctl_lock);
2852 if (err_desc->lun_id >= CTL_MAX_LUNS ||
2853 (lun = softc->ctl_luns[err_desc->lun_id]) == NULL) {
2854 mtx_unlock(&softc->ctl_lock);
2855 free(new_err_desc, M_CTL);
2856 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2857 __func__, (uintmax_t)err_desc->lun_id);
2861 mtx_lock(&lun->lun_lock);
2862 mtx_unlock(&softc->ctl_lock);
2865 * We could do some checking here to verify the validity
2866 * of the request, but given the complexity of error
2867 * injection requests, the checking logic would be fairly
2870 * For now, if the request is invalid, it just won't get
2871 * executed and might get deleted.
2873 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2876 * XXX KDM check to make sure the serial number is unique,
2877 * in case we somehow manage to wrap. That shouldn't
2878 * happen for a very long time, but it's the right thing to
2881 new_err_desc->serial = lun->error_serial;
2882 err_desc->serial = lun->error_serial;
2883 lun->error_serial++;
2885 mtx_unlock(&lun->lun_lock);
2888 case CTL_ERROR_INJECT_DELETE: {
2889 struct ctl_error_desc *delete_desc, *desc, *desc2;
2892 delete_desc = (struct ctl_error_desc *)addr;
2895 mtx_lock(&softc->ctl_lock);
2896 if (delete_desc->lun_id >= CTL_MAX_LUNS ||
2897 (lun = softc->ctl_luns[delete_desc->lun_id]) == NULL) {
2898 mtx_unlock(&softc->ctl_lock);
2899 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2900 __func__, (uintmax_t)delete_desc->lun_id);
2904 mtx_lock(&lun->lun_lock);
2905 mtx_unlock(&softc->ctl_lock);
2906 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2907 if (desc->serial != delete_desc->serial)
2910 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2915 mtx_unlock(&lun->lun_lock);
2916 if (delete_done == 0) {
2917 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2918 "error serial %ju on LUN %u\n", __func__,
2919 delete_desc->serial, delete_desc->lun_id);
2925 case CTL_DUMP_STRUCTS: {
2927 struct ctl_port *port;
2928 struct ctl_frontend *fe;
2930 mtx_lock(&softc->ctl_lock);
2931 printf("CTL Persistent Reservation information start:\n");
2932 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2933 mtx_lock(&lun->lun_lock);
2934 if ((lun->flags & CTL_LUN_DISABLED) != 0) {
2935 mtx_unlock(&lun->lun_lock);
2939 for (j = 0; j < CTL_MAX_PORTS; j++) {
2940 if (lun->pr_keys[j] == NULL)
2942 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2943 if (lun->pr_keys[j][k] == 0)
2945 printf(" LUN %ju port %d iid %d key "
2946 "%#jx\n", lun->lun, j, k,
2947 (uintmax_t)lun->pr_keys[j][k]);
2950 mtx_unlock(&lun->lun_lock);
2952 printf("CTL Persistent Reservation information end\n");
2953 printf("CTL Ports:\n");
2954 STAILQ_FOREACH(port, &softc->port_list, links) {
2955 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
2956 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
2957 port->frontend->name, port->port_type,
2958 port->physical_port, port->virtual_port,
2959 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
2960 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2961 if (port->wwpn_iid[j].in_use == 0 &&
2962 port->wwpn_iid[j].wwpn == 0 &&
2963 port->wwpn_iid[j].name == NULL)
2966 printf(" iid %u use %d WWPN %#jx '%s'\n",
2967 j, port->wwpn_iid[j].in_use,
2968 (uintmax_t)port->wwpn_iid[j].wwpn,
2969 port->wwpn_iid[j].name);
2972 printf("CTL Port information end\n");
2973 mtx_unlock(&softc->ctl_lock);
2975 * XXX KDM calling this without a lock. We'd likely want
2976 * to drop the lock before calling the frontend's dump
2979 printf("CTL Frontends:\n");
2980 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2981 printf(" Frontend '%s'\n", fe->name);
2982 if (fe->fe_dump != NULL)
2985 printf("CTL Frontend information end\n");
2989 struct ctl_lun_req *lun_req;
2990 struct ctl_backend_driver *backend;
2992 lun_req = (struct ctl_lun_req *)addr;
2994 backend = ctl_backend_find(lun_req->backend);
2995 if (backend == NULL) {
2996 lun_req->status = CTL_LUN_ERROR;
2997 snprintf(lun_req->error_str,
2998 sizeof(lun_req->error_str),
2999 "Backend \"%s\" not found.",
3003 if (lun_req->num_be_args > 0) {
3004 lun_req->kern_be_args = ctl_copyin_args(
3005 lun_req->num_be_args,
3008 sizeof(lun_req->error_str));
3009 if (lun_req->kern_be_args == NULL) {
3010 lun_req->status = CTL_LUN_ERROR;
3015 retval = backend->ioctl(dev, cmd, addr, flag, td);
3017 if (lun_req->num_be_args > 0) {
3018 ctl_copyout_args(lun_req->num_be_args,
3019 lun_req->kern_be_args);
3020 ctl_free_args(lun_req->num_be_args,
3021 lun_req->kern_be_args);
3025 case CTL_LUN_LIST: {
3027 struct ctl_lun_list *list;
3028 struct ctl_option *opt;
3030 list = (struct ctl_lun_list *)addr;
3033 * Allocate a fixed length sbuf here, based on the length
3034 * of the user's buffer. We could allocate an auto-extending
3035 * buffer, and then tell the user how much larger our
3036 * amount of data is than his buffer, but that presents
3039 * 1. The sbuf(9) routines use a blocking malloc, and so
3040 * we can't hold a lock while calling them with an
3041 * auto-extending buffer.
3043 * 2. There is not currently a LUN reference counting
3044 * mechanism, outside of outstanding transactions on
3045 * the LUN's OOA queue. So a LUN could go away on us
3046 * while we're getting the LUN number, backend-specific
3047 * information, etc. Thus, given the way things
3048 * currently work, we need to hold the CTL lock while
3049 * grabbing LUN information.
3051 * So, from the user's standpoint, the best thing to do is
3052 * allocate what he thinks is a reasonable buffer length,
3053 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3054 * double the buffer length and try again. (And repeat
3055 * that until he succeeds.)
3057 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3059 list->status = CTL_LUN_LIST_ERROR;
3060 snprintf(list->error_str, sizeof(list->error_str),
3061 "Unable to allocate %d bytes for LUN list",
3066 sbuf_printf(sb, "<ctllunlist>\n");
3068 mtx_lock(&softc->ctl_lock);
3069 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3070 mtx_lock(&lun->lun_lock);
3071 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3072 (uintmax_t)lun->lun);
3075 * Bail out as soon as we see that we've overfilled
3081 retval = sbuf_printf(sb, "\t<backend_type>%s"
3082 "</backend_type>\n",
3083 (lun->backend == NULL) ? "none" :
3084 lun->backend->name);
3089 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3090 lun->be_lun->lun_type);
3095 if (lun->backend == NULL) {
3096 retval = sbuf_printf(sb, "</lun>\n");
3102 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3103 (lun->be_lun->maxlba > 0) ?
3104 lun->be_lun->maxlba + 1 : 0);
3109 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3110 lun->be_lun->blocksize);
3115 retval = sbuf_printf(sb, "\t<serial_number>");
3120 retval = ctl_sbuf_printf_esc(sb,
3121 lun->be_lun->serial_num,
3122 sizeof(lun->be_lun->serial_num));
3127 retval = sbuf_printf(sb, "</serial_number>\n");
3132 retval = sbuf_printf(sb, "\t<device_id>");
3137 retval = ctl_sbuf_printf_esc(sb,
3138 lun->be_lun->device_id,
3139 sizeof(lun->be_lun->device_id));
3144 retval = sbuf_printf(sb, "</device_id>\n");
3149 if (lun->backend->lun_info != NULL) {
3150 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3154 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3155 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3156 opt->name, opt->value, opt->name);
3161 retval = sbuf_printf(sb, "</lun>\n");
3165 mtx_unlock(&lun->lun_lock);
3168 mtx_unlock(&lun->lun_lock);
3169 mtx_unlock(&softc->ctl_lock);
3172 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3175 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3176 snprintf(list->error_str, sizeof(list->error_str),
3177 "Out of space, %d bytes is too small",
3184 retval = copyout(sbuf_data(sb), list->lun_xml,
3187 list->fill_len = sbuf_len(sb) + 1;
3188 list->status = CTL_LUN_LIST_OK;
3193 struct ctl_iscsi *ci;
3194 struct ctl_frontend *fe;
3196 ci = (struct ctl_iscsi *)addr;
3198 fe = ctl_frontend_find("iscsi");
3200 ci->status = CTL_ISCSI_ERROR;
3201 snprintf(ci->error_str, sizeof(ci->error_str),
3202 "Frontend \"iscsi\" not found.");
3206 retval = fe->ioctl(dev, cmd, addr, flag, td);
3209 case CTL_PORT_REQ: {
3210 struct ctl_req *req;
3211 struct ctl_frontend *fe;
3213 req = (struct ctl_req *)addr;
3215 fe = ctl_frontend_find(req->driver);
3217 req->status = CTL_LUN_ERROR;
3218 snprintf(req->error_str, sizeof(req->error_str),
3219 "Frontend \"%s\" not found.", req->driver);
3222 if (req->num_args > 0) {
3223 req->kern_args = ctl_copyin_args(req->num_args,
3224 req->args, req->error_str, sizeof(req->error_str));
3225 if (req->kern_args == NULL) {
3226 req->status = CTL_LUN_ERROR;
3232 retval = fe->ioctl(dev, cmd, addr, flag, td);
3236 if (req->num_args > 0) {
3237 ctl_copyout_args(req->num_args, req->kern_args);
3238 ctl_free_args(req->num_args, req->kern_args);
3242 case CTL_PORT_LIST: {
3244 struct ctl_port *port;
3245 struct ctl_lun_list *list;
3246 struct ctl_option *opt;
3250 list = (struct ctl_lun_list *)addr;
3252 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3254 list->status = CTL_LUN_LIST_ERROR;
3255 snprintf(list->error_str, sizeof(list->error_str),
3256 "Unable to allocate %d bytes for LUN list",
3261 sbuf_printf(sb, "<ctlportlist>\n");
3263 mtx_lock(&softc->ctl_lock);
3264 STAILQ_FOREACH(port, &softc->port_list, links) {
3265 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3266 (uintmax_t)port->targ_port);
3269 * Bail out as soon as we see that we've overfilled
3275 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3276 "</frontend_type>\n", port->frontend->name);
3280 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3285 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3286 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3290 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3295 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3296 port->physical_port);
3300 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3301 port->virtual_port);
3305 if (port->target_devid != NULL) {
3306 sbuf_printf(sb, "\t<target>");
3307 ctl_id_sbuf(port->target_devid, sb);
3308 sbuf_printf(sb, "</target>\n");
3311 if (port->port_devid != NULL) {
3312 sbuf_printf(sb, "\t<port>");
3313 ctl_id_sbuf(port->port_devid, sb);
3314 sbuf_printf(sb, "</port>\n");
3317 if (port->port_info != NULL) {
3318 retval = port->port_info(port->onoff_arg, sb);
3322 STAILQ_FOREACH(opt, &port->options, links) {
3323 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3324 opt->name, opt->value, opt->name);
3329 if (port->lun_map != NULL) {
3330 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n");
3331 for (j = 0; j < port->lun_map_size; j++) {
3332 plun = ctl_lun_map_from_port(port, j);
3333 if (plun == UINT32_MAX)
3336 "\t<lun id=\"%u\">%u</lun>\n",
3341 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3342 if (port->wwpn_iid[j].in_use == 0 ||
3343 (port->wwpn_iid[j].wwpn == 0 &&
3344 port->wwpn_iid[j].name == NULL))
3347 if (port->wwpn_iid[j].name != NULL)
3348 retval = sbuf_printf(sb,
3349 "\t<initiator id=\"%u\">%s</initiator>\n",
3350 j, port->wwpn_iid[j].name);
3352 retval = sbuf_printf(sb,
3353 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3354 j, port->wwpn_iid[j].wwpn);
3361 retval = sbuf_printf(sb, "</targ_port>\n");
3365 mtx_unlock(&softc->ctl_lock);
3368 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3371 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3372 snprintf(list->error_str, sizeof(list->error_str),
3373 "Out of space, %d bytes is too small",
3380 retval = copyout(sbuf_data(sb), list->lun_xml,
3383 list->fill_len = sbuf_len(sb) + 1;
3384 list->status = CTL_LUN_LIST_OK;
3389 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr;
3390 struct ctl_port *port;
3392 mtx_lock(&softc->ctl_lock);
3393 if (lm->port < softc->port_min ||
3394 lm->port >= softc->port_max ||
3395 (port = softc->ctl_ports[lm->port]) == NULL) {
3396 mtx_unlock(&softc->ctl_lock);
3399 if (port->status & CTL_PORT_STATUS_ONLINE) {
3400 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3401 if (ctl_lun_map_to_port(port, lun->lun) ==
3404 mtx_lock(&lun->lun_lock);
3405 ctl_est_ua_port(lun, lm->port, -1,
3407 mtx_unlock(&lun->lun_lock);
3410 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps
3411 if (lm->plun != UINT32_MAX) {
3412 if (lm->lun == UINT32_MAX)
3413 retval = ctl_lun_map_unset(port, lm->plun);
3414 else if (lm->lun < CTL_MAX_LUNS &&
3415 softc->ctl_luns[lm->lun] != NULL)
3416 retval = ctl_lun_map_set(port, lm->plun, lm->lun);
3420 if (lm->lun == UINT32_MAX)
3421 retval = ctl_lun_map_deinit(port);
3423 retval = ctl_lun_map_init(port);
3425 if (port->status & CTL_PORT_STATUS_ONLINE)
3426 ctl_isc_announce_port(port);
3429 case CTL_GET_LUN_STATS: {
3430 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr;
3434 * XXX KDM no locking here. If the LUN list changes,
3435 * things can blow up.
3438 stats->status = CTL_SS_OK;
3439 stats->fill_len = 0;
3440 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3441 if (lun->lun < stats->first_item)
3443 if (stats->fill_len + sizeof(lun->stats) >
3445 stats->status = CTL_SS_NEED_MORE_SPACE;
3448 retval = copyout(&lun->stats, &stats->stats[i++],
3449 sizeof(lun->stats));
3452 stats->fill_len += sizeof(lun->stats);
3454 stats->num_items = softc->num_luns;
3455 stats->flags = CTL_STATS_FLAG_NONE;
3457 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
3459 getnanouptime(&stats->timestamp);
3462 case CTL_GET_PORT_STATS: {
3463 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr;
3467 * XXX KDM no locking here. If the LUN list changes,
3468 * things can blow up.
3471 stats->status = CTL_SS_OK;
3472 stats->fill_len = 0;
3473 STAILQ_FOREACH(port, &softc->port_list, links) {
3474 if (port->targ_port < stats->first_item)
3476 if (stats->fill_len + sizeof(port->stats) >
3478 stats->status = CTL_SS_NEED_MORE_SPACE;
3481 retval = copyout(&port->stats, &stats->stats[i++],
3482 sizeof(port->stats));
3485 stats->fill_len += sizeof(port->stats);
3487 stats->num_items = softc->num_ports;
3488 stats->flags = CTL_STATS_FLAG_NONE;
3490 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
3492 getnanouptime(&stats->timestamp);
3496 /* XXX KDM should we fix this? */
3498 struct ctl_backend_driver *backend;
3505 * We encode the backend type as the ioctl type for backend
3506 * ioctls. So parse it out here, and then search for a
3507 * backend of this type.
3509 type = _IOC_TYPE(cmd);
3511 STAILQ_FOREACH(backend, &softc->be_list, links) {
3512 if (backend->type == type) {
3518 printf("ctl: unknown ioctl command %#lx or backend "
3523 retval = backend->ioctl(dev, cmd, addr, flag, td);
3533 ctl_get_initindex(struct ctl_nexus *nexus)
3535 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3539 ctl_lun_map_init(struct ctl_port *port)
3541 struct ctl_softc *softc = port->ctl_softc;
3542 struct ctl_lun *lun;
3543 int size = ctl_lun_map_size;
3546 if (port->lun_map == NULL || port->lun_map_size < size) {
3547 port->lun_map_size = 0;
3548 free(port->lun_map, M_CTL);
3549 port->lun_map = malloc(size * sizeof(uint32_t),
3552 if (port->lun_map == NULL)
3554 for (i = 0; i < size; i++)
3555 port->lun_map[i] = UINT32_MAX;
3556 port->lun_map_size = size;
3557 if (port->status & CTL_PORT_STATUS_ONLINE) {
3558 if (port->lun_disable != NULL) {
3559 STAILQ_FOREACH(lun, &softc->lun_list, links)
3560 port->lun_disable(port->targ_lun_arg, lun->lun);
3562 ctl_isc_announce_port(port);
3568 ctl_lun_map_deinit(struct ctl_port *port)
3570 struct ctl_softc *softc = port->ctl_softc;
3571 struct ctl_lun *lun;
3573 if (port->lun_map == NULL)
3575 port->lun_map_size = 0;
3576 free(port->lun_map, M_CTL);
3577 port->lun_map = NULL;
3578 if (port->status & CTL_PORT_STATUS_ONLINE) {
3579 if (port->lun_enable != NULL) {
3580 STAILQ_FOREACH(lun, &softc->lun_list, links)
3581 port->lun_enable(port->targ_lun_arg, lun->lun);
3583 ctl_isc_announce_port(port);
3589 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun)
3594 if (port->lun_map == NULL) {
3595 status = ctl_lun_map_init(port);
3599 if (plun >= port->lun_map_size)
3601 old = port->lun_map[plun];
3602 port->lun_map[plun] = glun;
3603 if ((port->status & CTL_PORT_STATUS_ONLINE) && old == UINT32_MAX) {
3604 if (port->lun_enable != NULL)
3605 port->lun_enable(port->targ_lun_arg, plun);
3606 ctl_isc_announce_port(port);
3612 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun)
3616 if (port->lun_map == NULL || plun >= port->lun_map_size)
3618 old = port->lun_map[plun];
3619 port->lun_map[plun] = UINT32_MAX;
3620 if ((port->status & CTL_PORT_STATUS_ONLINE) && old != UINT32_MAX) {
3621 if (port->lun_disable != NULL)
3622 port->lun_disable(port->targ_lun_arg, plun);
3623 ctl_isc_announce_port(port);
3629 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
3633 return (UINT32_MAX);
3634 if (port->lun_map == NULL)
3636 if (lun_id > port->lun_map_size)
3637 return (UINT32_MAX);
3638 return (port->lun_map[lun_id]);
3642 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
3647 return (UINT32_MAX);
3648 if (port->lun_map == NULL)
3650 for (i = 0; i < port->lun_map_size; i++) {
3651 if (port->lun_map[i] == lun_id)
3654 return (UINT32_MAX);
3658 ctl_decode_lun(uint64_t encoded)
3661 uint32_t result = 0xffffffff;
3663 be64enc(lun, encoded);
3664 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) {
3665 case RPL_LUNDATA_ATYP_PERIPH:
3666 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 &&
3667 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0)
3670 case RPL_LUNDATA_ATYP_FLAT:
3671 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 &&
3672 lun[6] == 0 && lun[7] == 0)
3673 result = ((lun[0] & 0x3f) << 8) + lun[1];
3675 case RPL_LUNDATA_ATYP_EXTLUN:
3676 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) {
3678 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) {
3683 result = (lun[1] << 16) + (lun[2] << 8) +
3687 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0)
3688 result = (lun[2] << 24) +
3689 (lun[3] << 16) + (lun[4] << 8) +
3694 case RPL_LUNDATA_EXT_EAM_NOT_SPEC:
3695 result = 0xffffffff;
3704 ctl_encode_lun(uint32_t decoded)
3706 uint64_t l = decoded;
3709 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48));
3711 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48));
3713 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) |
3715 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16));
3719 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last)
3723 for (i = first; i < last; i++) {
3724 if ((mask[i / 32] & (1 << (i % 32))) == 0)
3731 ctl_set_mask(uint32_t *mask, uint32_t bit)
3733 uint32_t chunk, piece;
3736 piece = bit % (sizeof(uint32_t) * 8);
3738 if ((mask[chunk] & (1 << piece)) != 0)
3741 mask[chunk] |= (1 << piece);
3747 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3749 uint32_t chunk, piece;
3752 piece = bit % (sizeof(uint32_t) * 8);
3754 if ((mask[chunk] & (1 << piece)) == 0)
3757 mask[chunk] &= ~(1 << piece);
3763 ctl_is_set(uint32_t *mask, uint32_t bit)
3765 uint32_t chunk, piece;
3768 piece = bit % (sizeof(uint32_t) * 8);
3770 if ((mask[chunk] & (1 << piece)) == 0)
3777 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3781 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3784 return (t[residx % CTL_MAX_INIT_PER_PORT]);
3788 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3792 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3795 t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3799 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3804 i = residx/CTL_MAX_INIT_PER_PORT;
3805 if (lun->pr_keys[i] != NULL)
3807 mtx_unlock(&lun->lun_lock);
3808 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3810 mtx_lock(&lun->lun_lock);
3811 if (lun->pr_keys[i] == NULL)
3812 lun->pr_keys[i] = p;
3818 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3822 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3823 KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3824 t[residx % CTL_MAX_INIT_PER_PORT] = key;
3828 * ctl_softc, pool_name, total_ctl_io are passed in.
3829 * npool is passed out.
3832 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3833 uint32_t total_ctl_io, void **npool)
3835 struct ctl_io_pool *pool;
3837 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3842 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3843 pool->ctl_softc = ctl_softc;
3845 pool->zone = uma_zsecond_create(pool->name, NULL,
3846 NULL, NULL, NULL, ctl_softc->io_zone);
3847 /* uma_prealloc(pool->zone, total_ctl_io); */
3849 pool->zone = ctl_softc->io_zone;
3857 ctl_pool_free(struct ctl_io_pool *pool)
3864 uma_zdestroy(pool->zone);
3870 ctl_alloc_io(void *pool_ref)
3872 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3875 io = uma_zalloc(pool->zone, M_WAITOK);
3877 io->io_hdr.pool = pool_ref;
3878 CTL_SOFTC(io) = pool->ctl_softc;
3884 ctl_alloc_io_nowait(void *pool_ref)
3886 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3889 io = uma_zalloc(pool->zone, M_NOWAIT);
3891 io->io_hdr.pool = pool_ref;
3892 CTL_SOFTC(io) = pool->ctl_softc;
3898 ctl_free_io(union ctl_io *io)
3900 struct ctl_io_pool *pool;
3905 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3906 uma_zfree(pool->zone, io);
3910 ctl_zero_io(union ctl_io *io)
3912 struct ctl_io_pool *pool;
3918 * May need to preserve linked list pointers at some point too.
3920 pool = io->io_hdr.pool;
3921 memset(io, 0, sizeof(*io));
3922 io->io_hdr.pool = pool;
3923 CTL_SOFTC(io) = pool->ctl_softc;
3927 ctl_expand_number(const char *buf, uint64_t *num)
3933 number = strtoq(buf, &endptr, 0);
3935 switch (tolower((unsigned char)*endptr)) {
3955 case '\0': /* No unit. */
3959 /* Unrecognized unit. */
3963 if ((number << shift) >> shift != number) {
3967 *num = number << shift;
3973 * This routine could be used in the future to load default and/or saved
3974 * mode page parameters for a particuar lun.
3977 ctl_init_page_index(struct ctl_lun *lun)
3980 struct ctl_page_index *page_index;
3984 memcpy(&lun->mode_pages.index, page_index_template,
3985 sizeof(page_index_template));
3987 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3989 page_index = &lun->mode_pages.index[i];
3990 if (lun->be_lun->lun_type == T_DIRECT &&
3991 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
3993 if (lun->be_lun->lun_type == T_PROCESSOR &&
3994 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
3996 if (lun->be_lun->lun_type == T_CDROM &&
3997 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4000 page_code = page_index->page_code & SMPH_PC_MASK;
4001 switch (page_code) {
4002 case SMS_RW_ERROR_RECOVERY_PAGE: {
4003 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4004 ("subpage %#x for page %#x is incorrect!",
4005 page_index->subpage, page_code));
4006 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
4007 &rw_er_page_default,
4008 sizeof(rw_er_page_default));
4009 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
4010 &rw_er_page_changeable,
4011 sizeof(rw_er_page_changeable));
4012 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
4013 &rw_er_page_default,
4014 sizeof(rw_er_page_default));
4015 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
4016 &rw_er_page_default,
4017 sizeof(rw_er_page_default));
4018 page_index->page_data =
4019 (uint8_t *)lun->mode_pages.rw_er_page;
4022 case SMS_FORMAT_DEVICE_PAGE: {
4023 struct scsi_format_page *format_page;
4025 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4026 ("subpage %#x for page %#x is incorrect!",
4027 page_index->subpage, page_code));
4030 * Sectors per track are set above. Bytes per
4031 * sector need to be set here on a per-LUN basis.
4033 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
4034 &format_page_default,
4035 sizeof(format_page_default));
4036 memcpy(&lun->mode_pages.format_page[
4037 CTL_PAGE_CHANGEABLE], &format_page_changeable,
4038 sizeof(format_page_changeable));
4039 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
4040 &format_page_default,
4041 sizeof(format_page_default));
4042 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
4043 &format_page_default,
4044 sizeof(format_page_default));
4046 format_page = &lun->mode_pages.format_page[
4048 scsi_ulto2b(lun->be_lun->blocksize,
4049 format_page->bytes_per_sector);
4051 format_page = &lun->mode_pages.format_page[
4053 scsi_ulto2b(lun->be_lun->blocksize,
4054 format_page->bytes_per_sector);
4056 format_page = &lun->mode_pages.format_page[
4058 scsi_ulto2b(lun->be_lun->blocksize,
4059 format_page->bytes_per_sector);
4061 page_index->page_data =
4062 (uint8_t *)lun->mode_pages.format_page;
4065 case SMS_RIGID_DISK_PAGE: {
4066 struct scsi_rigid_disk_page *rigid_disk_page;
4067 uint32_t sectors_per_cylinder;
4071 #endif /* !__XSCALE__ */
4073 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4074 ("subpage %#x for page %#x is incorrect!",
4075 page_index->subpage, page_code));
4078 * Rotation rate and sectors per track are set
4079 * above. We calculate the cylinders here based on
4080 * capacity. Due to the number of heads and
4081 * sectors per track we're using, smaller arrays
4082 * may turn out to have 0 cylinders. Linux and
4083 * FreeBSD don't pay attention to these mode pages
4084 * to figure out capacity, but Solaris does. It
4085 * seems to deal with 0 cylinders just fine, and
4086 * works out a fake geometry based on the capacity.
4088 memcpy(&lun->mode_pages.rigid_disk_page[
4089 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4090 sizeof(rigid_disk_page_default));
4091 memcpy(&lun->mode_pages.rigid_disk_page[
4092 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4093 sizeof(rigid_disk_page_changeable));
4095 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4099 * The divide method here will be more accurate,
4100 * probably, but results in floating point being
4101 * used in the kernel on i386 (__udivdi3()). On the
4102 * XScale, though, __udivdi3() is implemented in
4105 * The shift method for cylinder calculation is
4106 * accurate if sectors_per_cylinder is a power of
4107 * 2. Otherwise it might be slightly off -- you
4108 * might have a bit of a truncation problem.
4111 cylinders = (lun->be_lun->maxlba + 1) /
4112 sectors_per_cylinder;
4114 for (shift = 31; shift > 0; shift--) {
4115 if (sectors_per_cylinder & (1 << shift))
4118 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4122 * We've basically got 3 bytes, or 24 bits for the
4123 * cylinder size in the mode page. If we're over,
4124 * just round down to 2^24.
4126 if (cylinders > 0xffffff)
4127 cylinders = 0xffffff;
4129 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4131 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4133 if ((value = ctl_get_opt(&lun->be_lun->options,
4135 scsi_ulto2b(strtol(value, NULL, 0),
4136 rigid_disk_page->rotation_rate);
4139 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
4140 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4141 sizeof(rigid_disk_page_default));
4142 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
4143 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4144 sizeof(rigid_disk_page_default));
4146 page_index->page_data =
4147 (uint8_t *)lun->mode_pages.rigid_disk_page;
4150 case SMS_VERIFY_ERROR_RECOVERY_PAGE: {
4151 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4152 ("subpage %#x for page %#x is incorrect!",
4153 page_index->subpage, page_code));
4154 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CURRENT],
4155 &verify_er_page_default,
4156 sizeof(verify_er_page_default));
4157 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CHANGEABLE],
4158 &verify_er_page_changeable,
4159 sizeof(verify_er_page_changeable));
4160 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_DEFAULT],
4161 &verify_er_page_default,
4162 sizeof(verify_er_page_default));
4163 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_SAVED],
4164 &verify_er_page_default,
4165 sizeof(verify_er_page_default));
4166 page_index->page_data =
4167 (uint8_t *)lun->mode_pages.verify_er_page;
4170 case SMS_CACHING_PAGE: {
4171 struct scsi_caching_page *caching_page;
4173 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4174 ("subpage %#x for page %#x is incorrect!",
4175 page_index->subpage, page_code));
4176 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4177 &caching_page_default,
4178 sizeof(caching_page_default));
4179 memcpy(&lun->mode_pages.caching_page[
4180 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4181 sizeof(caching_page_changeable));
4182 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4183 &caching_page_default,
4184 sizeof(caching_page_default));
4185 caching_page = &lun->mode_pages.caching_page[
4187 value = ctl_get_opt(&lun->be_lun->options, "writecache");
4188 if (value != NULL && strcmp(value, "off") == 0)
4189 caching_page->flags1 &= ~SCP_WCE;
4190 value = ctl_get_opt(&lun->be_lun->options, "readcache");
4191 if (value != NULL && strcmp(value, "off") == 0)
4192 caching_page->flags1 |= SCP_RCD;
4193 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4194 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4195 sizeof(caching_page_default));
4196 page_index->page_data =
4197 (uint8_t *)lun->mode_pages.caching_page;
4200 case SMS_CONTROL_MODE_PAGE: {
4201 switch (page_index->subpage) {
4202 case SMS_SUBPAGE_PAGE_0: {
4203 struct scsi_control_page *control_page;
4205 memcpy(&lun->mode_pages.control_page[
4207 &control_page_default,
4208 sizeof(control_page_default));
4209 memcpy(&lun->mode_pages.control_page[
4210 CTL_PAGE_CHANGEABLE],
4211 &control_page_changeable,
4212 sizeof(control_page_changeable));
4213 memcpy(&lun->mode_pages.control_page[
4215 &control_page_default,
4216 sizeof(control_page_default));
4217 control_page = &lun->mode_pages.control_page[
4219 value = ctl_get_opt(&lun->be_lun->options,
4221 if (value != NULL &&
4222 strcmp(value, "unrestricted") == 0) {
4223 control_page->queue_flags &=
4224 ~SCP_QUEUE_ALG_MASK;
4225 control_page->queue_flags |=
4226 SCP_QUEUE_ALG_UNRESTRICTED;
4228 memcpy(&lun->mode_pages.control_page[
4230 &lun->mode_pages.control_page[
4232 sizeof(control_page_default));
4233 page_index->page_data =
4234 (uint8_t *)lun->mode_pages.control_page;
4238 memcpy(&lun->mode_pages.control_ext_page[
4240 &control_ext_page_default,
4241 sizeof(control_ext_page_default));
4242 memcpy(&lun->mode_pages.control_ext_page[
4243 CTL_PAGE_CHANGEABLE],
4244 &control_ext_page_changeable,
4245 sizeof(control_ext_page_changeable));
4246 memcpy(&lun->mode_pages.control_ext_page[
4248 &control_ext_page_default,
4249 sizeof(control_ext_page_default));
4250 memcpy(&lun->mode_pages.control_ext_page[
4252 &lun->mode_pages.control_ext_page[
4254 sizeof(control_ext_page_default));
4255 page_index->page_data =
4256 (uint8_t *)lun->mode_pages.control_ext_page;
4259 panic("subpage %#x for page %#x is incorrect!",
4260 page_index->subpage, page_code);
4264 case SMS_INFO_EXCEPTIONS_PAGE: {
4265 switch (page_index->subpage) {
4266 case SMS_SUBPAGE_PAGE_0:
4267 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4269 sizeof(ie_page_default));
4270 memcpy(&lun->mode_pages.ie_page[
4271 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4272 sizeof(ie_page_changeable));
4273 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4275 sizeof(ie_page_default));
4276 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4278 sizeof(ie_page_default));
4279 page_index->page_data =
4280 (uint8_t *)lun->mode_pages.ie_page;
4283 struct ctl_logical_block_provisioning_page *page;
4285 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4287 sizeof(lbp_page_default));
4288 memcpy(&lun->mode_pages.lbp_page[
4289 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4290 sizeof(lbp_page_changeable));
4291 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4293 sizeof(lbp_page_default));
4294 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4295 value = ctl_get_opt(&lun->be_lun->options,
4297 if (value != NULL &&
4298 ctl_expand_number(value, &ival) == 0) {
4299 page->descr[0].flags |= SLBPPD_ENABLED |
4301 if (lun->be_lun->blocksize)
4302 ival /= lun->be_lun->blocksize;
4305 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4306 page->descr[0].count);
4308 value = ctl_get_opt(&lun->be_lun->options,
4310 if (value != NULL &&
4311 ctl_expand_number(value, &ival) == 0) {
4312 page->descr[1].flags |= SLBPPD_ENABLED |
4314 if (lun->be_lun->blocksize)
4315 ival /= lun->be_lun->blocksize;
4318 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4319 page->descr[1].count);
4321 value = ctl_get_opt(&lun->be_lun->options,
4322 "pool-avail-threshold");
4323 if (value != NULL &&
4324 ctl_expand_number(value, &ival) == 0) {
4325 page->descr[2].flags |= SLBPPD_ENABLED |
4327 if (lun->be_lun->blocksize)
4328 ival /= lun->be_lun->blocksize;
4331 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4332 page->descr[2].count);
4334 value = ctl_get_opt(&lun->be_lun->options,
4335 "pool-used-threshold");
4336 if (value != NULL &&
4337 ctl_expand_number(value, &ival) == 0) {
4338 page->descr[3].flags |= SLBPPD_ENABLED |
4340 if (lun->be_lun->blocksize)
4341 ival /= lun->be_lun->blocksize;
4344 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4345 page->descr[3].count);
4347 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4348 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4349 sizeof(lbp_page_default));
4350 page_index->page_data =
4351 (uint8_t *)lun->mode_pages.lbp_page;
4355 panic("subpage %#x for page %#x is incorrect!",
4356 page_index->subpage, page_code);
4360 case SMS_CDDVD_CAPS_PAGE:{
4361 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4362 ("subpage %#x for page %#x is incorrect!",
4363 page_index->subpage, page_code));
4364 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT],
4365 &cddvd_page_default,
4366 sizeof(cddvd_page_default));
4367 memcpy(&lun->mode_pages.cddvd_page[
4368 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable,
4369 sizeof(cddvd_page_changeable));
4370 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4371 &cddvd_page_default,
4372 sizeof(cddvd_page_default));
4373 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT],
4374 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4375 sizeof(cddvd_page_default));
4376 page_index->page_data =
4377 (uint8_t *)lun->mode_pages.cddvd_page;
4381 panic("invalid page code value %#x", page_code);
4385 return (CTL_RETVAL_COMPLETE);
4389 ctl_init_log_page_index(struct ctl_lun *lun)
4391 struct ctl_page_index *page_index;
4394 memcpy(&lun->log_pages.index, log_page_index_template,
4395 sizeof(log_page_index_template));
4398 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4400 page_index = &lun->log_pages.index[i];
4401 if (lun->be_lun->lun_type == T_DIRECT &&
4402 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4404 if (lun->be_lun->lun_type == T_PROCESSOR &&
4405 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4407 if (lun->be_lun->lun_type == T_CDROM &&
4408 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4411 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4412 lun->backend->lun_attr == NULL)
4415 if (page_index->page_code != prev) {
4416 lun->log_pages.pages_page[j] = page_index->page_code;
4417 prev = page_index->page_code;
4420 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4421 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4424 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4425 lun->log_pages.index[0].page_len = j;
4426 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4427 lun->log_pages.index[1].page_len = k * 2;
4428 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4429 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4430 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page;
4431 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page);
4432 lun->log_pages.index[4].page_data = (uint8_t *)&lun->log_pages.ie_page;
4433 lun->log_pages.index[4].page_len = sizeof(lun->log_pages.ie_page);
4435 return (CTL_RETVAL_COMPLETE);
4439 hex2bin(const char *str, uint8_t *buf, int buf_size)
4444 memset(buf, 0, buf_size);
4445 while (isspace(str[0]))
4447 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4450 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4451 while (str[i] == '-') /* Skip dashes in UUIDs. */
4456 else if (isalpha(c))
4457 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4463 buf[i / 2] |= (c << 4);
4467 return ((i + 1) / 2);
4474 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4475 * wants us to allocate the LUN and he can block.
4476 * - ctl_softc is always set
4477 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4479 * Returns 0 for success, non-zero (errno) for failure.
4482 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4483 struct ctl_be_lun *const be_lun)
4485 struct ctl_lun *nlun, *lun;
4486 struct scsi_vpd_id_descriptor *desc;
4487 struct scsi_vpd_id_t10 *t10id;
4488 const char *eui, *naa, *scsiname, *uuid, *vendor, *value;
4489 int lun_number, lun_malloced;
4490 int devidlen, idlen1, idlen2 = 0, len;
4496 * We currently only support Direct Access or Processor LUN types.
4498 switch (be_lun->lun_type) {
4506 be_lun->lun_config_status(be_lun->be_lun,
4507 CTL_LUN_CONFIG_FAILURE);
4510 if (ctl_lun == NULL) {
4511 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4518 memset(lun, 0, sizeof(*lun));
4520 lun->flags = CTL_LUN_MALLOCED;
4522 /* Generate LUN ID. */
4523 devidlen = max(CTL_DEVID_MIN_LEN,
4524 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4525 idlen1 = sizeof(*t10id) + devidlen;
4526 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4527 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4528 if (scsiname != NULL) {
4529 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4530 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4532 eui = ctl_get_opt(&be_lun->options, "eui");
4534 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4536 naa = ctl_get_opt(&be_lun->options, "naa");
4538 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4540 uuid = ctl_get_opt(&be_lun->options, "uuid");
4542 len += sizeof(struct scsi_vpd_id_descriptor) + 18;
4544 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4545 M_CTL, M_WAITOK | M_ZERO);
4546 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4547 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4548 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4549 desc->length = idlen1;
4550 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4551 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4552 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4553 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4555 strncpy(t10id->vendor, vendor,
4556 min(sizeof(t10id->vendor), strlen(vendor)));
4558 strncpy((char *)t10id->vendor_spec_id,
4559 (char *)be_lun->device_id, devidlen);
4560 if (scsiname != NULL) {
4561 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4563 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4564 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4565 SVPD_ID_TYPE_SCSI_NAME;
4566 desc->length = idlen2;
4567 strlcpy(desc->identifier, scsiname, idlen2);
4570 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4572 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4573 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4575 desc->length = hex2bin(eui, desc->identifier, 16);
4576 desc->length = desc->length > 12 ? 16 :
4577 (desc->length > 8 ? 12 : 8);
4578 len -= 16 - desc->length;
4581 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4583 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4584 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4586 desc->length = hex2bin(naa, desc->identifier, 16);
4587 desc->length = desc->length > 8 ? 16 : 8;
4588 len -= 16 - desc->length;
4591 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4593 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4594 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4596 desc->identifier[0] = 0x10;
4597 hex2bin(uuid, &desc->identifier[2], 16);
4600 lun->lun_devid->len = len;
4602 mtx_lock(&ctl_softc->ctl_lock);
4604 * See if the caller requested a particular LUN number. If so, see
4605 * if it is available. Otherwise, allocate the first available LUN.
4607 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4608 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4609 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4610 mtx_unlock(&ctl_softc->ctl_lock);
4611 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4612 printf("ctl: requested LUN ID %d is higher "
4613 "than CTL_MAX_LUNS - 1 (%d)\n",
4614 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4617 * XXX KDM return an error, or just assign
4618 * another LUN ID in this case??
4620 printf("ctl: requested LUN ID %d is already "
4621 "in use\n", be_lun->req_lun_id);
4624 free(lun->lun_devid, M_CTL);
4625 if (lun->flags & CTL_LUN_MALLOCED)
4627 be_lun->lun_config_status(be_lun->be_lun,
4628 CTL_LUN_CONFIG_FAILURE);
4631 lun_number = be_lun->req_lun_id;
4633 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, CTL_MAX_LUNS);
4634 if (lun_number == -1) {
4635 mtx_unlock(&ctl_softc->ctl_lock);
4636 printf("ctl: can't allocate LUN, out of LUNs\n");
4640 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4641 mtx_unlock(&ctl_softc->ctl_lock);
4643 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4644 lun->lun = lun_number;
4645 lun->be_lun = be_lun;
4647 * The processor LUN is always enabled. Disk LUNs come on line
4648 * disabled, and must be enabled by the backend.
4650 lun->flags |= CTL_LUN_DISABLED;
4651 lun->backend = be_lun->be;
4652 be_lun->ctl_lun = lun;
4653 be_lun->lun_id = lun_number;
4654 atomic_add_int(&be_lun->be->num_luns, 1);
4655 if (be_lun->flags & CTL_LUN_FLAG_EJECTED)
4656 lun->flags |= CTL_LUN_EJECTED;
4657 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA)
4658 lun->flags |= CTL_LUN_NO_MEDIA;
4659 if (be_lun->flags & CTL_LUN_FLAG_STOPPED)
4660 lun->flags |= CTL_LUN_STOPPED;
4662 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4663 lun->flags |= CTL_LUN_PRIMARY_SC;
4665 value = ctl_get_opt(&be_lun->options, "removable");
4666 if (value != NULL) {
4667 if (strcmp(value, "on") == 0)
4668 lun->flags |= CTL_LUN_REMOVABLE;
4669 } else if (be_lun->lun_type == T_CDROM)
4670 lun->flags |= CTL_LUN_REMOVABLE;
4672 lun->ctl_softc = ctl_softc;
4674 lun->last_busy = getsbinuptime();
4676 TAILQ_INIT(&lun->ooa_queue);
4677 TAILQ_INIT(&lun->blocked_queue);
4678 STAILQ_INIT(&lun->error_list);
4679 lun->ie_reported = 1;
4680 callout_init_mtx(&lun->ie_callout, &lun->lun_lock, 0);
4681 ctl_tpc_lun_init(lun);
4682 if (lun->flags & CTL_LUN_REMOVABLE) {
4683 lun->prevent = malloc((CTL_MAX_INITIATORS + 31) / 32 * 4,
4688 * Initialize the mode and log page index.
4690 ctl_init_page_index(lun);
4691 ctl_init_log_page_index(lun);
4693 /* Setup statistics gathering */
4694 #ifdef CTL_LEGACY_STATS
4695 lun->legacy_stats.device_type = be_lun->lun_type;
4696 lun->legacy_stats.lun_number = lun_number;
4697 lun->legacy_stats.blocksize = be_lun->blocksize;
4698 if (be_lun->blocksize == 0)
4699 lun->legacy_stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4700 for (len = 0; len < CTL_MAX_PORTS; len++)
4701 lun->legacy_stats.ports[len].targ_port = len;
4702 #endif /* CTL_LEGACY_STATS */
4703 lun->stats.item = lun_number;
4706 * Now, before we insert this lun on the lun list, set the lun
4707 * inventory changed UA for all other luns.
4709 mtx_lock(&ctl_softc->ctl_lock);
4710 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4711 mtx_lock(&nlun->lun_lock);
4712 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4713 mtx_unlock(&nlun->lun_lock);
4715 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4716 ctl_softc->ctl_luns[lun_number] = lun;
4717 ctl_softc->num_luns++;
4718 mtx_unlock(&ctl_softc->ctl_lock);
4720 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4727 * - LUN has already been marked invalid and any pending I/O has been taken
4731 ctl_free_lun(struct ctl_lun *lun)
4733 struct ctl_softc *softc = lun->ctl_softc;
4734 struct ctl_lun *nlun;
4737 KASSERT(TAILQ_EMPTY(&lun->ooa_queue),
4738 ("Freeing a LUN %p with outstanding I/O!\n", lun));
4740 mtx_lock(&softc->ctl_lock);
4741 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4742 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4743 softc->ctl_luns[lun->lun] = NULL;
4745 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4746 mtx_lock(&nlun->lun_lock);
4747 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4748 mtx_unlock(&nlun->lun_lock);
4750 mtx_unlock(&softc->ctl_lock);
4753 * Tell the backend to free resources, if this LUN has a backend.
4755 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4756 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4758 lun->ie_reportcnt = UINT32_MAX;
4759 callout_drain(&lun->ie_callout);
4760 ctl_tpc_lun_shutdown(lun);
4761 mtx_destroy(&lun->lun_lock);
4762 free(lun->lun_devid, M_CTL);
4763 for (i = 0; i < CTL_MAX_PORTS; i++)
4764 free(lun->pending_ua[i], M_CTL);
4765 for (i = 0; i < CTL_MAX_PORTS; i++)
4766 free(lun->pr_keys[i], M_CTL);
4767 free(lun->write_buffer, M_CTL);
4768 free(lun->prevent, M_CTL);
4769 if (lun->flags & CTL_LUN_MALLOCED)
4776 ctl_create_lun(struct ctl_be_lun *be_lun)
4780 * ctl_alloc_lun() should handle all potential failure cases.
4782 ctl_alloc_lun(control_softc, NULL, be_lun);
4786 ctl_add_lun(struct ctl_be_lun *be_lun)
4788 struct ctl_softc *softc = control_softc;
4790 mtx_lock(&softc->ctl_lock);
4791 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links);
4792 mtx_unlock(&softc->ctl_lock);
4793 wakeup(&softc->pending_lun_queue);
4799 ctl_enable_lun(struct ctl_be_lun *be_lun)
4801 struct ctl_softc *softc;
4802 struct ctl_port *port, *nport;
4803 struct ctl_lun *lun;
4806 lun = (struct ctl_lun *)be_lun->ctl_lun;
4807 softc = lun->ctl_softc;
4809 mtx_lock(&softc->ctl_lock);
4810 mtx_lock(&lun->lun_lock);
4811 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4813 * eh? Why did we get called if the LUN is already
4816 mtx_unlock(&lun->lun_lock);
4817 mtx_unlock(&softc->ctl_lock);
4820 lun->flags &= ~CTL_LUN_DISABLED;
4821 mtx_unlock(&lun->lun_lock);
4823 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) {
4824 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4825 port->lun_map != NULL || port->lun_enable == NULL)
4829 * Drop the lock while we call the FETD's enable routine.
4830 * This can lead to a callback into CTL (at least in the
4831 * case of the internal initiator frontend.
4833 mtx_unlock(&softc->ctl_lock);
4834 retval = port->lun_enable(port->targ_lun_arg, lun->lun);
4835 mtx_lock(&softc->ctl_lock);
4837 printf("%s: FETD %s port %d returned error "
4838 "%d for lun_enable on lun %jd\n",
4839 __func__, port->port_name, port->targ_port,
4840 retval, (intmax_t)lun->lun);
4844 mtx_unlock(&softc->ctl_lock);
4845 ctl_isc_announce_lun(lun);
4851 ctl_disable_lun(struct ctl_be_lun *be_lun)
4853 struct ctl_softc *softc;
4854 struct ctl_port *port;
4855 struct ctl_lun *lun;
4858 lun = (struct ctl_lun *)be_lun->ctl_lun;
4859 softc = lun->ctl_softc;
4861 mtx_lock(&softc->ctl_lock);
4862 mtx_lock(&lun->lun_lock);
4863 if (lun->flags & CTL_LUN_DISABLED) {
4864 mtx_unlock(&lun->lun_lock);
4865 mtx_unlock(&softc->ctl_lock);
4868 lun->flags |= CTL_LUN_DISABLED;
4869 mtx_unlock(&lun->lun_lock);
4871 STAILQ_FOREACH(port, &softc->port_list, links) {
4872 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4873 port->lun_map != NULL || port->lun_disable == NULL)
4877 * Drop the lock before we call the frontend's disable
4878 * routine, to avoid lock order reversals.
4880 * XXX KDM what happens if the frontend list changes while
4881 * we're traversing it? It's unlikely, but should be handled.
4883 mtx_unlock(&softc->ctl_lock);
4884 retval = port->lun_disable(port->targ_lun_arg, lun->lun);
4885 mtx_lock(&softc->ctl_lock);
4887 printf("%s: FETD %s port %d returned error "
4888 "%d for lun_disable on lun %jd\n",
4889 __func__, port->port_name, port->targ_port,
4890 retval, (intmax_t)lun->lun);
4894 mtx_unlock(&softc->ctl_lock);
4895 ctl_isc_announce_lun(lun);
4901 ctl_start_lun(struct ctl_be_lun *be_lun)
4903 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4905 mtx_lock(&lun->lun_lock);
4906 lun->flags &= ~CTL_LUN_STOPPED;
4907 mtx_unlock(&lun->lun_lock);
4912 ctl_stop_lun(struct ctl_be_lun *be_lun)
4914 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4916 mtx_lock(&lun->lun_lock);
4917 lun->flags |= CTL_LUN_STOPPED;
4918 mtx_unlock(&lun->lun_lock);
4923 ctl_lun_no_media(struct ctl_be_lun *be_lun)
4925 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4927 mtx_lock(&lun->lun_lock);
4928 lun->flags |= CTL_LUN_NO_MEDIA;
4929 mtx_unlock(&lun->lun_lock);
4934 ctl_lun_has_media(struct ctl_be_lun *be_lun)
4936 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4937 union ctl_ha_msg msg;
4939 mtx_lock(&lun->lun_lock);
4940 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED);
4941 if (lun->flags & CTL_LUN_REMOVABLE)
4942 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE);
4943 mtx_unlock(&lun->lun_lock);
4944 if ((lun->flags & CTL_LUN_REMOVABLE) &&
4945 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4946 bzero(&msg.ua, sizeof(msg.ua));
4947 msg.hdr.msg_type = CTL_MSG_UA;
4948 msg.hdr.nexus.initid = -1;
4949 msg.hdr.nexus.targ_port = -1;
4950 msg.hdr.nexus.targ_lun = lun->lun;
4951 msg.hdr.nexus.targ_mapped_lun = lun->lun;
4954 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE;
4955 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4962 ctl_lun_ejected(struct ctl_be_lun *be_lun)
4964 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4966 mtx_lock(&lun->lun_lock);
4967 lun->flags |= CTL_LUN_EJECTED;
4968 mtx_unlock(&lun->lun_lock);
4973 ctl_lun_primary(struct ctl_be_lun *be_lun)
4975 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4977 mtx_lock(&lun->lun_lock);
4978 lun->flags |= CTL_LUN_PRIMARY_SC;
4979 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4980 mtx_unlock(&lun->lun_lock);
4981 ctl_isc_announce_lun(lun);
4986 ctl_lun_secondary(struct ctl_be_lun *be_lun)
4988 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4990 mtx_lock(&lun->lun_lock);
4991 lun->flags &= ~CTL_LUN_PRIMARY_SC;
4992 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4993 mtx_unlock(&lun->lun_lock);
4994 ctl_isc_announce_lun(lun);
4999 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
5001 struct ctl_softc *softc;
5002 struct ctl_lun *lun;
5004 lun = (struct ctl_lun *)be_lun->ctl_lun;
5005 softc = lun->ctl_softc;
5007 mtx_lock(&lun->lun_lock);
5010 * The LUN needs to be disabled before it can be marked invalid.
5012 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
5013 mtx_unlock(&lun->lun_lock);
5017 * Mark the LUN invalid.
5019 lun->flags |= CTL_LUN_INVALID;
5022 * If there is nothing in the OOA queue, go ahead and free the LUN.
5023 * If we have something in the OOA queue, we'll free it when the
5024 * last I/O completes.
5026 if (TAILQ_EMPTY(&lun->ooa_queue)) {
5027 mtx_unlock(&lun->lun_lock);
5030 mtx_unlock(&lun->lun_lock);
5036 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
5038 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5039 union ctl_ha_msg msg;
5041 mtx_lock(&lun->lun_lock);
5042 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE);
5043 mtx_unlock(&lun->lun_lock);
5044 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
5045 /* Send msg to other side. */
5046 bzero(&msg.ua, sizeof(msg.ua));
5047 msg.hdr.msg_type = CTL_MSG_UA;
5048 msg.hdr.nexus.initid = -1;
5049 msg.hdr.nexus.targ_port = -1;
5050 msg.hdr.nexus.targ_lun = lun->lun;
5051 msg.hdr.nexus.targ_mapped_lun = lun->lun;
5054 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE;
5055 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
5061 * Backend "memory move is complete" callback for requests that never
5062 * make it down to say RAIDCore's configuration code.
5065 ctl_config_move_done(union ctl_io *io)
5069 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
5070 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
5071 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
5073 if ((io->io_hdr.port_status != 0) &&
5074 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5075 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5076 ctl_set_internal_failure(&io->scsiio, /*sks_valid*/ 1,
5077 /*retry_count*/ io->io_hdr.port_status);
5078 } else if (io->scsiio.kern_data_resid != 0 &&
5079 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT &&
5080 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5081 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5082 ctl_set_invalid_field_ciu(&io->scsiio);
5085 if (ctl_debug & CTL_DEBUG_CDB_DATA)
5087 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
5088 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5089 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
5090 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5092 * XXX KDM just assuming a single pointer here, and not a
5093 * S/G list. If we start using S/G lists for config data,
5094 * we'll need to know how to clean them up here as well.
5096 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5097 free(io->scsiio.kern_data_ptr, M_CTL);
5099 retval = CTL_RETVAL_COMPLETE;
5102 * XXX KDM now we need to continue data movement. Some
5104 * - call ctl_scsiio() again? We don't do this for data
5105 * writes, because for those at least we know ahead of
5106 * time where the write will go and how long it is. For
5107 * config writes, though, that information is largely
5108 * contained within the write itself, thus we need to
5109 * parse out the data again.
5111 * - Call some other function once the data is in?
5115 * XXX KDM call ctl_scsiio() again for now, and check flag
5116 * bits to see whether we're allocated or not.
5118 retval = ctl_scsiio(&io->scsiio);
5124 * This gets called by a backend driver when it is done with a
5125 * data_submit method.
5128 ctl_data_submit_done(union ctl_io *io)
5131 * If the IO_CONT flag is set, we need to call the supplied
5132 * function to continue processing the I/O, instead of completing
5135 * If there is an error, though, we don't want to keep processing.
5136 * Instead, just send status back to the initiator.
5138 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5139 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5140 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5141 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5142 io->scsiio.io_cont(io);
5149 * This gets called by a backend driver when it is done with a
5150 * configuration write.
5153 ctl_config_write_done(union ctl_io *io)
5158 * If the IO_CONT flag is set, we need to call the supplied
5159 * function to continue processing the I/O, instead of completing
5162 * If there is an error, though, we don't want to keep processing.
5163 * Instead, just send status back to the initiator.
5165 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5166 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5167 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5168 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5169 io->scsiio.io_cont(io);
5173 * Since a configuration write can be done for commands that actually
5174 * have data allocated, like write buffer, and commands that have
5175 * no data, like start/stop unit, we need to check here.
5177 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5178 buf = io->scsiio.kern_data_ptr;
5187 ctl_config_read_done(union ctl_io *io)
5192 * If there is some error -- we are done, skip data transfer.
5194 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
5195 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5196 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
5197 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5198 buf = io->scsiio.kern_data_ptr;
5208 * If the IO_CONT flag is set, we need to call the supplied
5209 * function to continue processing the I/O, instead of completing
5212 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
5213 io->scsiio.io_cont(io);
5221 * SCSI release command.
5224 ctl_scsi_release(struct ctl_scsiio *ctsio)
5226 struct ctl_lun *lun = CTL_LUN(ctsio);
5229 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5231 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5234 * XXX KDM right now, we only support LUN reservation. We don't
5235 * support 3rd party reservations, or extent reservations, which
5236 * might actually need the parameter list. If we've gotten this
5237 * far, we've got a LUN reservation. Anything else got kicked out
5238 * above. So, according to SPC, ignore the length.
5241 mtx_lock(&lun->lun_lock);
5244 * According to SPC, it is not an error for an intiator to attempt
5245 * to release a reservation on a LUN that isn't reserved, or that
5246 * is reserved by another initiator. The reservation can only be
5247 * released, though, by the initiator who made it or by one of
5248 * several reset type events.
5250 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5251 lun->flags &= ~CTL_LUN_RESERVED;
5253 mtx_unlock(&lun->lun_lock);
5255 ctl_set_success(ctsio);
5256 ctl_done((union ctl_io *)ctsio);
5257 return (CTL_RETVAL_COMPLETE);
5261 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5263 struct ctl_lun *lun = CTL_LUN(ctsio);
5266 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5268 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5271 * XXX KDM right now, we only support LUN reservation. We don't
5272 * support 3rd party reservations, or extent reservations, which
5273 * might actually need the parameter list. If we've gotten this
5274 * far, we've got a LUN reservation. Anything else got kicked out
5275 * above. So, according to SPC, ignore the length.
5278 mtx_lock(&lun->lun_lock);
5279 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5280 ctl_set_reservation_conflict(ctsio);
5284 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */
5285 if (lun->flags & CTL_LUN_PR_RESERVED) {
5286 ctl_set_success(ctsio);
5290 lun->flags |= CTL_LUN_RESERVED;
5291 lun->res_idx = residx;
5292 ctl_set_success(ctsio);
5295 mtx_unlock(&lun->lun_lock);
5296 ctl_done((union ctl_io *)ctsio);
5297 return (CTL_RETVAL_COMPLETE);
5301 ctl_start_stop(struct ctl_scsiio *ctsio)
5303 struct ctl_lun *lun = CTL_LUN(ctsio);
5304 struct scsi_start_stop_unit *cdb;
5307 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5309 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5311 if ((cdb->how & SSS_PC_MASK) == 0) {
5312 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
5313 (cdb->how & SSS_START) == 0) {
5316 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5317 if (ctl_get_prkey(lun, residx) == 0 ||
5318 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) {
5320 ctl_set_reservation_conflict(ctsio);
5321 ctl_done((union ctl_io *)ctsio);
5322 return (CTL_RETVAL_COMPLETE);
5326 if ((cdb->how & SSS_LOEJ) &&
5327 (lun->flags & CTL_LUN_REMOVABLE) == 0) {
5328 ctl_set_invalid_field(ctsio,
5334 ctl_done((union ctl_io *)ctsio);
5335 return (CTL_RETVAL_COMPLETE);
5338 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) &&
5339 lun->prevent_count > 0) {
5340 /* "Medium removal prevented" */
5341 ctl_set_sense(ctsio, /*current_error*/ 1,
5342 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ?
5343 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST,
5344 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE);
5345 ctl_done((union ctl_io *)ctsio);
5346 return (CTL_RETVAL_COMPLETE);
5350 retval = lun->backend->config_write((union ctl_io *)ctsio);
5355 ctl_prevent_allow(struct ctl_scsiio *ctsio)
5357 struct ctl_lun *lun = CTL_LUN(ctsio);
5358 struct scsi_prevent *cdb;
5362 CTL_DEBUG_PRINT(("ctl_prevent_allow\n"));
5364 cdb = (struct scsi_prevent *)ctsio->cdb;
5366 if ((lun->flags & CTL_LUN_REMOVABLE) == 0 || lun->prevent == NULL) {
5367 ctl_set_invalid_opcode(ctsio);
5368 ctl_done((union ctl_io *)ctsio);
5369 return (CTL_RETVAL_COMPLETE);
5372 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5373 mtx_lock(&lun->lun_lock);
5374 if ((cdb->how & PR_PREVENT) &&
5375 ctl_is_set(lun->prevent, initidx) == 0) {
5376 ctl_set_mask(lun->prevent, initidx);
5377 lun->prevent_count++;
5378 } else if ((cdb->how & PR_PREVENT) == 0 &&
5379 ctl_is_set(lun->prevent, initidx)) {
5380 ctl_clear_mask(lun->prevent, initidx);
5381 lun->prevent_count--;
5383 mtx_unlock(&lun->lun_lock);
5384 retval = lun->backend->config_write((union ctl_io *)ctsio);
5389 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5390 * we don't really do anything with the LBA and length fields if the user
5391 * passes them in. Instead we'll just flush out the cache for the entire
5395 ctl_sync_cache(struct ctl_scsiio *ctsio)
5397 struct ctl_lun *lun = CTL_LUN(ctsio);
5398 struct ctl_lba_len_flags *lbalen;
5399 uint64_t starting_lba;
5400 uint32_t block_count;
5404 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5408 switch (ctsio->cdb[0]) {
5409 case SYNCHRONIZE_CACHE: {
5410 struct scsi_sync_cache *cdb;
5411 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5413 starting_lba = scsi_4btoul(cdb->begin_lba);
5414 block_count = scsi_2btoul(cdb->lb_count);
5418 case SYNCHRONIZE_CACHE_16: {
5419 struct scsi_sync_cache_16 *cdb;
5420 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5422 starting_lba = scsi_8btou64(cdb->begin_lba);
5423 block_count = scsi_4btoul(cdb->lb_count);
5428 ctl_set_invalid_opcode(ctsio);
5429 ctl_done((union ctl_io *)ctsio);
5431 break; /* NOTREACHED */
5435 * We check the LBA and length, but don't do anything with them.
5436 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5437 * get flushed. This check will just help satisfy anyone who wants
5438 * to see an error for an out of range LBA.
5440 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5441 ctl_set_lba_out_of_range(ctsio,
5442 MAX(starting_lba, lun->be_lun->maxlba + 1));
5443 ctl_done((union ctl_io *)ctsio);
5447 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5448 lbalen->lba = starting_lba;
5449 lbalen->len = block_count;
5450 lbalen->flags = byte2;
5451 retval = lun->backend->config_write((union ctl_io *)ctsio);
5458 ctl_format(struct ctl_scsiio *ctsio)
5460 struct scsi_format *cdb;
5461 int length, defect_list_len;
5463 CTL_DEBUG_PRINT(("ctl_format\n"));
5465 cdb = (struct scsi_format *)ctsio->cdb;
5468 if (cdb->byte2 & SF_FMTDATA) {
5469 if (cdb->byte2 & SF_LONGLIST)
5470 length = sizeof(struct scsi_format_header_long);
5472 length = sizeof(struct scsi_format_header_short);
5475 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5477 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5478 ctsio->kern_data_len = length;
5479 ctsio->kern_total_len = length;
5480 ctsio->kern_rel_offset = 0;
5481 ctsio->kern_sg_entries = 0;
5482 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5483 ctsio->be_move_done = ctl_config_move_done;
5484 ctl_datamove((union ctl_io *)ctsio);
5486 return (CTL_RETVAL_COMPLETE);
5489 defect_list_len = 0;
5491 if (cdb->byte2 & SF_FMTDATA) {
5492 if (cdb->byte2 & SF_LONGLIST) {
5493 struct scsi_format_header_long *header;
5495 header = (struct scsi_format_header_long *)
5496 ctsio->kern_data_ptr;
5498 defect_list_len = scsi_4btoul(header->defect_list_len);
5499 if (defect_list_len != 0) {
5500 ctl_set_invalid_field(ctsio,
5509 struct scsi_format_header_short *header;
5511 header = (struct scsi_format_header_short *)
5512 ctsio->kern_data_ptr;
5514 defect_list_len = scsi_2btoul(header->defect_list_len);
5515 if (defect_list_len != 0) {
5516 ctl_set_invalid_field(ctsio,
5527 ctl_set_success(ctsio);
5530 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5531 free(ctsio->kern_data_ptr, M_CTL);
5532 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5535 ctl_done((union ctl_io *)ctsio);
5536 return (CTL_RETVAL_COMPLETE);
5540 ctl_read_buffer(struct ctl_scsiio *ctsio)
5542 struct ctl_lun *lun = CTL_LUN(ctsio);
5543 uint64_t buffer_offset;
5546 static uint8_t descr[4];
5547 static uint8_t echo_descr[4] = { 0 };
5549 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5551 switch (ctsio->cdb[0]) {
5553 struct scsi_read_buffer *cdb;
5555 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5556 buffer_offset = scsi_3btoul(cdb->offset);
5557 len = scsi_3btoul(cdb->length);
5561 case READ_BUFFER_16: {
5562 struct scsi_read_buffer_16 *cdb;
5564 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb;
5565 buffer_offset = scsi_8btou64(cdb->offset);
5566 len = scsi_4btoul(cdb->length);
5570 default: /* This shouldn't happen. */
5571 ctl_set_invalid_opcode(ctsio);
5572 ctl_done((union ctl_io *)ctsio);
5573 return (CTL_RETVAL_COMPLETE);
5576 if (buffer_offset > CTL_WRITE_BUFFER_SIZE ||
5577 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5578 ctl_set_invalid_field(ctsio,
5584 ctl_done((union ctl_io *)ctsio);
5585 return (CTL_RETVAL_COMPLETE);
5588 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5590 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5591 ctsio->kern_data_ptr = descr;
5592 len = min(len, sizeof(descr));
5593 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5594 ctsio->kern_data_ptr = echo_descr;
5595 len = min(len, sizeof(echo_descr));
5597 if (lun->write_buffer == NULL) {
5598 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5601 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5603 ctsio->kern_data_len = len;
5604 ctsio->kern_total_len = len;
5605 ctsio->kern_rel_offset = 0;
5606 ctsio->kern_sg_entries = 0;
5607 ctl_set_success(ctsio);
5608 ctsio->be_move_done = ctl_config_move_done;
5609 ctl_datamove((union ctl_io *)ctsio);
5610 return (CTL_RETVAL_COMPLETE);
5614 ctl_write_buffer(struct ctl_scsiio *ctsio)
5616 struct ctl_lun *lun = CTL_LUN(ctsio);
5617 struct scsi_write_buffer *cdb;
5618 int buffer_offset, len;
5620 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5622 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5624 len = scsi_3btoul(cdb->length);
5625 buffer_offset = scsi_3btoul(cdb->offset);
5627 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5628 ctl_set_invalid_field(ctsio,
5634 ctl_done((union ctl_io *)ctsio);
5635 return (CTL_RETVAL_COMPLETE);
5639 * If we've got a kernel request that hasn't been malloced yet,
5640 * malloc it and tell the caller the data buffer is here.
5642 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5643 if (lun->write_buffer == NULL) {
5644 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5647 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5648 ctsio->kern_data_len = len;
5649 ctsio->kern_total_len = len;
5650 ctsio->kern_rel_offset = 0;
5651 ctsio->kern_sg_entries = 0;
5652 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5653 ctsio->be_move_done = ctl_config_move_done;
5654 ctl_datamove((union ctl_io *)ctsio);
5656 return (CTL_RETVAL_COMPLETE);
5659 ctl_set_success(ctsio);
5660 ctl_done((union ctl_io *)ctsio);
5661 return (CTL_RETVAL_COMPLETE);
5665 ctl_write_same(struct ctl_scsiio *ctsio)
5667 struct ctl_lun *lun = CTL_LUN(ctsio);
5668 struct ctl_lba_len_flags *lbalen;
5670 uint32_t num_blocks;
5674 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5676 switch (ctsio->cdb[0]) {
5677 case WRITE_SAME_10: {
5678 struct scsi_write_same_10 *cdb;
5680 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5682 lba = scsi_4btoul(cdb->addr);
5683 num_blocks = scsi_2btoul(cdb->length);
5687 case WRITE_SAME_16: {
5688 struct scsi_write_same_16 *cdb;
5690 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5692 lba = scsi_8btou64(cdb->addr);
5693 num_blocks = scsi_4btoul(cdb->length);
5699 * We got a command we don't support. This shouldn't
5700 * happen, commands should be filtered out above us.
5702 ctl_set_invalid_opcode(ctsio);
5703 ctl_done((union ctl_io *)ctsio);
5705 return (CTL_RETVAL_COMPLETE);
5706 break; /* NOTREACHED */
5709 /* ANCHOR flag can be used only together with UNMAP */
5710 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) {
5711 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5712 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5713 ctl_done((union ctl_io *)ctsio);
5714 return (CTL_RETVAL_COMPLETE);
5718 * The first check is to make sure we're in bounds, the second
5719 * check is to catch wrap-around problems. If the lba + num blocks
5720 * is less than the lba, then we've wrapped around and the block
5721 * range is invalid anyway.
5723 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5724 || ((lba + num_blocks) < lba)) {
5725 ctl_set_lba_out_of_range(ctsio,
5726 MAX(lba, lun->be_lun->maxlba + 1));
5727 ctl_done((union ctl_io *)ctsio);
5728 return (CTL_RETVAL_COMPLETE);
5731 /* Zero number of blocks means "to the last logical block" */
5732 if (num_blocks == 0) {
5733 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5734 ctl_set_invalid_field(ctsio,
5740 ctl_done((union ctl_io *)ctsio);
5741 return (CTL_RETVAL_COMPLETE);
5743 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5746 len = lun->be_lun->blocksize;
5749 * If we've got a kernel request that hasn't been malloced yet,
5750 * malloc it and tell the caller the data buffer is here.
5752 if ((byte2 & SWS_NDOB) == 0 &&
5753 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5754 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5755 ctsio->kern_data_len = len;
5756 ctsio->kern_total_len = len;
5757 ctsio->kern_rel_offset = 0;
5758 ctsio->kern_sg_entries = 0;
5759 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5760 ctsio->be_move_done = ctl_config_move_done;
5761 ctl_datamove((union ctl_io *)ctsio);
5763 return (CTL_RETVAL_COMPLETE);
5766 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5768 lbalen->len = num_blocks;
5769 lbalen->flags = byte2;
5770 retval = lun->backend->config_write((union ctl_io *)ctsio);
5776 ctl_unmap(struct ctl_scsiio *ctsio)
5778 struct ctl_lun *lun = CTL_LUN(ctsio);
5779 struct scsi_unmap *cdb;
5780 struct ctl_ptr_len_flags *ptrlen;
5781 struct scsi_unmap_header *hdr;
5782 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5784 uint32_t num_blocks;
5788 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5790 cdb = (struct scsi_unmap *)ctsio->cdb;
5791 len = scsi_2btoul(cdb->length);
5795 * If we've got a kernel request that hasn't been malloced yet,
5796 * malloc it and tell the caller the data buffer is here.
5798 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5799 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5800 ctsio->kern_data_len = len;
5801 ctsio->kern_total_len = len;
5802 ctsio->kern_rel_offset = 0;
5803 ctsio->kern_sg_entries = 0;
5804 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5805 ctsio->be_move_done = ctl_config_move_done;
5806 ctl_datamove((union ctl_io *)ctsio);
5808 return (CTL_RETVAL_COMPLETE);
5811 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5812 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5813 if (len < sizeof (*hdr) ||
5814 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5815 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5816 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5817 ctl_set_invalid_field(ctsio,
5825 len = scsi_2btoul(hdr->desc_length);
5826 buf = (struct scsi_unmap_desc *)(hdr + 1);
5827 end = buf + len / sizeof(*buf);
5830 for (range = buf; range < end; range++) {
5831 lba = scsi_8btou64(range->lba);
5832 num_blocks = scsi_4btoul(range->length);
5833 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5834 || ((lba + num_blocks) < lba)) {
5835 ctl_set_lba_out_of_range(ctsio,
5836 MAX(lba, lun->be_lun->maxlba + 1));
5837 ctl_done((union ctl_io *)ctsio);
5838 return (CTL_RETVAL_COMPLETE);
5840 if (num_blocks != 0)
5845 * Block backend can not handle zero last range.
5846 * Filter it out and return if there is nothing left.
5848 len = (uint8_t *)endnz - (uint8_t *)buf;
5850 ctl_set_success(ctsio);
5854 mtx_lock(&lun->lun_lock);
5855 ptrlen = (struct ctl_ptr_len_flags *)
5856 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5857 ptrlen->ptr = (void *)buf;
5859 ptrlen->flags = byte2;
5860 ctl_check_blocked(lun);
5861 mtx_unlock(&lun->lun_lock);
5863 retval = lun->backend->config_write((union ctl_io *)ctsio);
5867 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5868 free(ctsio->kern_data_ptr, M_CTL);
5869 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5871 ctl_done((union ctl_io *)ctsio);
5872 return (CTL_RETVAL_COMPLETE);
5876 ctl_default_page_handler(struct ctl_scsiio *ctsio,
5877 struct ctl_page_index *page_index, uint8_t *page_ptr)
5879 struct ctl_lun *lun = CTL_LUN(ctsio);
5880 uint8_t *current_cp;
5884 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5887 current_cp = (page_index->page_data + (page_index->page_len *
5890 mtx_lock(&lun->lun_lock);
5891 if (memcmp(current_cp, page_ptr, page_index->page_len)) {
5892 memcpy(current_cp, page_ptr, page_index->page_len);
5896 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5897 mtx_unlock(&lun->lun_lock);
5899 ctl_isc_announce_mode(lun,
5900 ctl_get_initindex(&ctsio->io_hdr.nexus),
5901 page_index->page_code, page_index->subpage);
5903 return (CTL_RETVAL_COMPLETE);
5907 ctl_ie_timer(void *arg)
5909 struct ctl_lun *lun = arg;
5912 if (lun->ie_asc == 0)
5915 if (lun->MODE_IE.mrie == SIEP_MRIE_UA)
5916 ctl_est_ua_all(lun, -1, CTL_UA_IE);
5918 lun->ie_reported = 0;
5920 if (lun->ie_reportcnt < scsi_4btoul(lun->MODE_IE.report_count)) {
5921 lun->ie_reportcnt++;
5922 t = scsi_4btoul(lun->MODE_IE.interval_timer);
5923 if (t == 0 || t == UINT32_MAX)
5924 t = 3000; /* 5 min */
5925 callout_schedule(&lun->ie_callout, t * hz / 10);
5930 ctl_ie_page_handler(struct ctl_scsiio *ctsio,
5931 struct ctl_page_index *page_index, uint8_t *page_ptr)
5933 struct ctl_lun *lun = CTL_LUN(ctsio);
5934 struct scsi_info_exceptions_page *pg;
5937 (void)ctl_default_page_handler(ctsio, page_index, page_ptr);
5939 pg = (struct scsi_info_exceptions_page *)page_ptr;
5940 mtx_lock(&lun->lun_lock);
5941 if (pg->info_flags & SIEP_FLAGS_TEST) {
5943 lun->ie_ascq = 0xff;
5944 if (pg->mrie == SIEP_MRIE_UA) {
5945 ctl_est_ua_all(lun, -1, CTL_UA_IE);
5946 lun->ie_reported = 1;
5948 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
5949 lun->ie_reported = -1;
5951 lun->ie_reportcnt = 1;
5952 if (lun->ie_reportcnt < scsi_4btoul(pg->report_count)) {
5953 lun->ie_reportcnt++;
5954 t = scsi_4btoul(pg->interval_timer);
5955 if (t == 0 || t == UINT32_MAX)
5956 t = 3000; /* 5 min */
5957 callout_reset(&lun->ie_callout, t * hz / 10,
5963 lun->ie_reported = 1;
5964 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
5965 lun->ie_reportcnt = UINT32_MAX;
5966 callout_stop(&lun->ie_callout);
5968 mtx_unlock(&lun->lun_lock);
5969 return (CTL_RETVAL_COMPLETE);
5973 ctl_do_mode_select(union ctl_io *io)
5975 struct ctl_lun *lun = CTL_LUN(io);
5976 struct scsi_mode_page_header *page_header;
5977 struct ctl_page_index *page_index;
5978 struct ctl_scsiio *ctsio;
5979 int page_len, page_len_offset, page_len_size;
5980 union ctl_modepage_info *modepage_info;
5981 uint16_t *len_left, *len_used;
5984 ctsio = &io->scsiio;
5988 modepage_info = (union ctl_modepage_info *)
5989 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
5990 len_left = &modepage_info->header.len_left;
5991 len_used = &modepage_info->header.len_used;
5995 page_header = (struct scsi_mode_page_header *)
5996 (ctsio->kern_data_ptr + *len_used);
5998 if (*len_left == 0) {
5999 free(ctsio->kern_data_ptr, M_CTL);
6000 ctl_set_success(ctsio);
6001 ctl_done((union ctl_io *)ctsio);
6002 return (CTL_RETVAL_COMPLETE);
6003 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6005 free(ctsio->kern_data_ptr, M_CTL);
6006 ctl_set_param_len_error(ctsio);
6007 ctl_done((union ctl_io *)ctsio);
6008 return (CTL_RETVAL_COMPLETE);
6010 } else if ((page_header->page_code & SMPH_SPF)
6011 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6013 free(ctsio->kern_data_ptr, M_CTL);
6014 ctl_set_param_len_error(ctsio);
6015 ctl_done((union ctl_io *)ctsio);
6016 return (CTL_RETVAL_COMPLETE);
6021 * XXX KDM should we do something with the block descriptor?
6023 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6024 page_index = &lun->mode_pages.index[i];
6025 if (lun->be_lun->lun_type == T_DIRECT &&
6026 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6028 if (lun->be_lun->lun_type == T_PROCESSOR &&
6029 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6031 if (lun->be_lun->lun_type == T_CDROM &&
6032 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6035 if ((page_index->page_code & SMPH_PC_MASK) !=
6036 (page_header->page_code & SMPH_PC_MASK))
6040 * If neither page has a subpage code, then we've got a
6043 if (((page_index->page_code & SMPH_SPF) == 0)
6044 && ((page_header->page_code & SMPH_SPF) == 0)) {
6045 page_len = page_header->page_length;
6050 * If both pages have subpages, then the subpage numbers
6053 if ((page_index->page_code & SMPH_SPF)
6054 && (page_header->page_code & SMPH_SPF)) {
6055 struct scsi_mode_page_header_sp *sph;
6057 sph = (struct scsi_mode_page_header_sp *)page_header;
6058 if (page_index->subpage == sph->subpage) {
6059 page_len = scsi_2btoul(sph->page_length);
6066 * If we couldn't find the page, or if we don't have a mode select
6067 * handler for it, send back an error to the user.
6069 if ((i >= CTL_NUM_MODE_PAGES)
6070 || (page_index->select_handler == NULL)) {
6071 ctl_set_invalid_field(ctsio,
6074 /*field*/ *len_used,
6077 free(ctsio->kern_data_ptr, M_CTL);
6078 ctl_done((union ctl_io *)ctsio);
6079 return (CTL_RETVAL_COMPLETE);
6082 if (page_index->page_code & SMPH_SPF) {
6083 page_len_offset = 2;
6087 page_len_offset = 1;
6091 * If the length the initiator gives us isn't the one we specify in
6092 * the mode page header, or if they didn't specify enough data in
6093 * the CDB to avoid truncating this page, kick out the request.
6095 if (page_len != page_index->page_len - page_len_offset - page_len_size) {
6096 ctl_set_invalid_field(ctsio,
6099 /*field*/ *len_used + page_len_offset,
6102 free(ctsio->kern_data_ptr, M_CTL);
6103 ctl_done((union ctl_io *)ctsio);
6104 return (CTL_RETVAL_COMPLETE);
6106 if (*len_left < page_index->page_len) {
6107 free(ctsio->kern_data_ptr, M_CTL);
6108 ctl_set_param_len_error(ctsio);
6109 ctl_done((union ctl_io *)ctsio);
6110 return (CTL_RETVAL_COMPLETE);
6114 * Run through the mode page, checking to make sure that the bits
6115 * the user changed are actually legal for him to change.
6117 for (i = 0; i < page_index->page_len; i++) {
6118 uint8_t *user_byte, *change_mask, *current_byte;
6122 user_byte = (uint8_t *)page_header + i;
6123 change_mask = page_index->page_data +
6124 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6125 current_byte = page_index->page_data +
6126 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6129 * Check to see whether the user set any bits in this byte
6130 * that he is not allowed to set.
6132 if ((*user_byte & ~(*change_mask)) ==
6133 (*current_byte & ~(*change_mask)))
6137 * Go through bit by bit to determine which one is illegal.
6140 for (j = 7; j >= 0; j--) {
6141 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6142 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6147 ctl_set_invalid_field(ctsio,
6150 /*field*/ *len_used + i,
6153 free(ctsio->kern_data_ptr, M_CTL);
6154 ctl_done((union ctl_io *)ctsio);
6155 return (CTL_RETVAL_COMPLETE);
6159 * Decrement these before we call the page handler, since we may
6160 * end up getting called back one way or another before the handler
6161 * returns to this context.
6163 *len_left -= page_index->page_len;
6164 *len_used += page_index->page_len;
6166 retval = page_index->select_handler(ctsio, page_index,
6167 (uint8_t *)page_header);
6170 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6171 * wait until this queued command completes to finish processing
6172 * the mode page. If it returns anything other than
6173 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6174 * already set the sense information, freed the data pointer, and
6175 * completed the io for us.
6177 if (retval != CTL_RETVAL_COMPLETE)
6178 goto bailout_no_done;
6181 * If the initiator sent us more than one page, parse the next one.
6186 ctl_set_success(ctsio);
6187 free(ctsio->kern_data_ptr, M_CTL);
6188 ctl_done((union ctl_io *)ctsio);
6192 return (CTL_RETVAL_COMPLETE);
6197 ctl_mode_select(struct ctl_scsiio *ctsio)
6199 struct ctl_lun *lun = CTL_LUN(ctsio);
6200 union ctl_modepage_info *modepage_info;
6201 int bd_len, i, header_size, param_len, pf, rtd, sp;
6204 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6205 switch (ctsio->cdb[0]) {
6206 case MODE_SELECT_6: {
6207 struct scsi_mode_select_6 *cdb;
6209 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6211 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6212 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6213 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6214 param_len = cdb->length;
6215 header_size = sizeof(struct scsi_mode_header_6);
6218 case MODE_SELECT_10: {
6219 struct scsi_mode_select_10 *cdb;
6221 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6223 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6224 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6225 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6226 param_len = scsi_2btoul(cdb->length);
6227 header_size = sizeof(struct scsi_mode_header_10);
6231 ctl_set_invalid_opcode(ctsio);
6232 ctl_done((union ctl_io *)ctsio);
6233 return (CTL_RETVAL_COMPLETE);
6237 if (param_len != 0) {
6238 ctl_set_invalid_field(ctsio, /*sks_valid*/ 0,
6239 /*command*/ 1, /*field*/ 0,
6240 /*bit_valid*/ 0, /*bit*/ 0);
6241 ctl_done((union ctl_io *)ctsio);
6242 return (CTL_RETVAL_COMPLETE);
6245 /* Revert to defaults. */
6246 ctl_init_page_index(lun);
6247 mtx_lock(&lun->lun_lock);
6248 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
6249 mtx_unlock(&lun->lun_lock);
6250 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6251 ctl_isc_announce_mode(lun, -1,
6252 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
6253 lun->mode_pages.index[i].subpage);
6255 ctl_set_success(ctsio);
6256 ctl_done((union ctl_io *)ctsio);
6257 return (CTL_RETVAL_COMPLETE);
6262 * "A parameter list length of zero indicates that the Data-Out Buffer
6263 * shall be empty. This condition shall not be considered as an error."
6265 if (param_len == 0) {
6266 ctl_set_success(ctsio);
6267 ctl_done((union ctl_io *)ctsio);
6268 return (CTL_RETVAL_COMPLETE);
6272 * Since we'll hit this the first time through, prior to
6273 * allocation, we don't need to free a data buffer here.
6275 if (param_len < header_size) {
6276 ctl_set_param_len_error(ctsio);
6277 ctl_done((union ctl_io *)ctsio);
6278 return (CTL_RETVAL_COMPLETE);
6282 * Allocate the data buffer and grab the user's data. In theory,
6283 * we shouldn't have to sanity check the parameter list length here
6284 * because the maximum size is 64K. We should be able to malloc
6285 * that much without too many problems.
6287 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6288 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6289 ctsio->kern_data_len = param_len;
6290 ctsio->kern_total_len = param_len;
6291 ctsio->kern_rel_offset = 0;
6292 ctsio->kern_sg_entries = 0;
6293 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6294 ctsio->be_move_done = ctl_config_move_done;
6295 ctl_datamove((union ctl_io *)ctsio);
6297 return (CTL_RETVAL_COMPLETE);
6300 switch (ctsio->cdb[0]) {
6301 case MODE_SELECT_6: {
6302 struct scsi_mode_header_6 *mh6;
6304 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6305 bd_len = mh6->blk_desc_len;
6308 case MODE_SELECT_10: {
6309 struct scsi_mode_header_10 *mh10;
6311 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6312 bd_len = scsi_2btoul(mh10->blk_desc_len);
6316 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6319 if (param_len < (header_size + bd_len)) {
6320 free(ctsio->kern_data_ptr, M_CTL);
6321 ctl_set_param_len_error(ctsio);
6322 ctl_done((union ctl_io *)ctsio);
6323 return (CTL_RETVAL_COMPLETE);
6327 * Set the IO_CONT flag, so that if this I/O gets passed to
6328 * ctl_config_write_done(), it'll get passed back to
6329 * ctl_do_mode_select() for further processing, or completion if
6332 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6333 ctsio->io_cont = ctl_do_mode_select;
6335 modepage_info = (union ctl_modepage_info *)
6336 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6337 memset(modepage_info, 0, sizeof(*modepage_info));
6338 modepage_info->header.len_left = param_len - header_size - bd_len;
6339 modepage_info->header.len_used = header_size + bd_len;
6341 return (ctl_do_mode_select((union ctl_io *)ctsio));
6345 ctl_mode_sense(struct ctl_scsiio *ctsio)
6347 struct ctl_lun *lun = CTL_LUN(ctsio);
6348 int pc, page_code, dbd, llba, subpage;
6349 int alloc_len, page_len, header_len, total_len;
6350 struct scsi_mode_block_descr *block_desc;
6351 struct ctl_page_index *page_index;
6357 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6359 switch (ctsio->cdb[0]) {
6360 case MODE_SENSE_6: {
6361 struct scsi_mode_sense_6 *cdb;
6363 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6365 header_len = sizeof(struct scsi_mode_hdr_6);
6366 if (cdb->byte2 & SMS_DBD)
6369 header_len += sizeof(struct scsi_mode_block_descr);
6371 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6372 page_code = cdb->page & SMS_PAGE_CODE;
6373 subpage = cdb->subpage;
6374 alloc_len = cdb->length;
6377 case MODE_SENSE_10: {
6378 struct scsi_mode_sense_10 *cdb;
6380 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6382 header_len = sizeof(struct scsi_mode_hdr_10);
6384 if (cdb->byte2 & SMS_DBD)
6387 header_len += sizeof(struct scsi_mode_block_descr);
6388 if (cdb->byte2 & SMS10_LLBAA)
6390 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6391 page_code = cdb->page & SMS_PAGE_CODE;
6392 subpage = cdb->subpage;
6393 alloc_len = scsi_2btoul(cdb->length);
6397 ctl_set_invalid_opcode(ctsio);
6398 ctl_done((union ctl_io *)ctsio);
6399 return (CTL_RETVAL_COMPLETE);
6400 break; /* NOTREACHED */
6404 * We have to make a first pass through to calculate the size of
6405 * the pages that match the user's query. Then we allocate enough
6406 * memory to hold it, and actually copy the data into the buffer.
6408 switch (page_code) {
6409 case SMS_ALL_PAGES_PAGE: {
6415 * At the moment, values other than 0 and 0xff here are
6416 * reserved according to SPC-3.
6418 if ((subpage != SMS_SUBPAGE_PAGE_0)
6419 && (subpage != SMS_SUBPAGE_ALL)) {
6420 ctl_set_invalid_field(ctsio,
6426 ctl_done((union ctl_io *)ctsio);
6427 return (CTL_RETVAL_COMPLETE);
6430 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6431 page_index = &lun->mode_pages.index[i];
6433 /* Make sure the page is supported for this dev type */
6434 if (lun->be_lun->lun_type == T_DIRECT &&
6435 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6437 if (lun->be_lun->lun_type == T_PROCESSOR &&
6438 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6440 if (lun->be_lun->lun_type == T_CDROM &&
6441 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6445 * We don't use this subpage if the user didn't
6446 * request all subpages.
6448 if ((page_index->subpage != 0)
6449 && (subpage == SMS_SUBPAGE_PAGE_0))
6453 printf("found page %#x len %d\n",
6454 page_index->page_code & SMPH_PC_MASK,
6455 page_index->page_len);
6457 page_len += page_index->page_len;
6466 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6467 page_index = &lun->mode_pages.index[i];
6469 /* Make sure the page is supported for this dev type */
6470 if (lun->be_lun->lun_type == T_DIRECT &&
6471 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6473 if (lun->be_lun->lun_type == T_PROCESSOR &&
6474 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6476 if (lun->be_lun->lun_type == T_CDROM &&
6477 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6480 /* Look for the right page code */
6481 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6484 /* Look for the right subpage or the subpage wildcard*/
6485 if ((page_index->subpage != subpage)
6486 && (subpage != SMS_SUBPAGE_ALL))
6490 printf("found page %#x len %d\n",
6491 page_index->page_code & SMPH_PC_MASK,
6492 page_index->page_len);
6495 page_len += page_index->page_len;
6498 if (page_len == 0) {
6499 ctl_set_invalid_field(ctsio,
6505 ctl_done((union ctl_io *)ctsio);
6506 return (CTL_RETVAL_COMPLETE);
6512 total_len = header_len + page_len;
6514 printf("header_len = %d, page_len = %d, total_len = %d\n",
6515 header_len, page_len, total_len);
6518 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6519 ctsio->kern_sg_entries = 0;
6520 ctsio->kern_rel_offset = 0;
6521 ctsio->kern_data_len = min(total_len, alloc_len);
6522 ctsio->kern_total_len = ctsio->kern_data_len;
6524 switch (ctsio->cdb[0]) {
6525 case MODE_SENSE_6: {
6526 struct scsi_mode_hdr_6 *header;
6528 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6530 header->datalen = MIN(total_len - 1, 254);
6531 if (lun->be_lun->lun_type == T_DIRECT) {
6532 header->dev_specific = 0x10; /* DPOFUA */
6533 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6534 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6535 header->dev_specific |= 0x80; /* WP */
6538 header->block_descr_len = 0;
6540 header->block_descr_len =
6541 sizeof(struct scsi_mode_block_descr);
6542 block_desc = (struct scsi_mode_block_descr *)&header[1];
6545 case MODE_SENSE_10: {
6546 struct scsi_mode_hdr_10 *header;
6549 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6551 datalen = MIN(total_len - 2, 65533);
6552 scsi_ulto2b(datalen, header->datalen);
6553 if (lun->be_lun->lun_type == T_DIRECT) {
6554 header->dev_specific = 0x10; /* DPOFUA */
6555 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6556 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6557 header->dev_specific |= 0x80; /* WP */
6560 scsi_ulto2b(0, header->block_descr_len);
6562 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6563 header->block_descr_len);
6564 block_desc = (struct scsi_mode_block_descr *)&header[1];
6568 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6572 * If we've got a disk, use its blocksize in the block
6573 * descriptor. Otherwise, just set it to 0.
6576 if (lun->be_lun->lun_type == T_DIRECT)
6577 scsi_ulto3b(lun->be_lun->blocksize,
6578 block_desc->block_len);
6580 scsi_ulto3b(0, block_desc->block_len);
6583 switch (page_code) {
6584 case SMS_ALL_PAGES_PAGE: {
6587 data_used = header_len;
6588 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6589 struct ctl_page_index *page_index;
6591 page_index = &lun->mode_pages.index[i];
6592 if (lun->be_lun->lun_type == T_DIRECT &&
6593 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6595 if (lun->be_lun->lun_type == T_PROCESSOR &&
6596 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6598 if (lun->be_lun->lun_type == T_CDROM &&
6599 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6603 * We don't use this subpage if the user didn't
6604 * request all subpages. We already checked (above)
6605 * to make sure the user only specified a subpage
6606 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6608 if ((page_index->subpage != 0)
6609 && (subpage == SMS_SUBPAGE_PAGE_0))
6613 * Call the handler, if it exists, to update the
6614 * page to the latest values.
6616 if (page_index->sense_handler != NULL)
6617 page_index->sense_handler(ctsio, page_index,pc);
6619 memcpy(ctsio->kern_data_ptr + data_used,
6620 page_index->page_data +
6621 (page_index->page_len * pc),
6622 page_index->page_len);
6623 data_used += page_index->page_len;
6630 data_used = header_len;
6632 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6633 struct ctl_page_index *page_index;
6635 page_index = &lun->mode_pages.index[i];
6637 /* Look for the right page code */
6638 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6641 /* Look for the right subpage or the subpage wildcard*/
6642 if ((page_index->subpage != subpage)
6643 && (subpage != SMS_SUBPAGE_ALL))
6646 /* Make sure the page is supported for this dev type */
6647 if (lun->be_lun->lun_type == T_DIRECT &&
6648 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6650 if (lun->be_lun->lun_type == T_PROCESSOR &&
6651 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6653 if (lun->be_lun->lun_type == T_CDROM &&
6654 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6658 * Call the handler, if it exists, to update the
6659 * page to the latest values.
6661 if (page_index->sense_handler != NULL)
6662 page_index->sense_handler(ctsio, page_index,pc);
6664 memcpy(ctsio->kern_data_ptr + data_used,
6665 page_index->page_data +
6666 (page_index->page_len * pc),
6667 page_index->page_len);
6668 data_used += page_index->page_len;
6674 ctl_set_success(ctsio);
6675 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6676 ctsio->be_move_done = ctl_config_move_done;
6677 ctl_datamove((union ctl_io *)ctsio);
6678 return (CTL_RETVAL_COMPLETE);
6682 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6683 struct ctl_page_index *page_index,
6686 struct ctl_lun *lun = CTL_LUN(ctsio);
6687 struct scsi_log_param_header *phdr;
6691 data = page_index->page_data;
6693 if (lun->backend->lun_attr != NULL &&
6694 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6696 phdr = (struct scsi_log_param_header *)data;
6697 scsi_ulto2b(0x0001, phdr->param_code);
6698 phdr->param_control = SLP_LBIN | SLP_LP;
6699 phdr->param_len = 8;
6700 data = (uint8_t *)(phdr + 1);
6701 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6702 data[4] = 0x02; /* per-pool */
6703 data += phdr->param_len;
6706 if (lun->backend->lun_attr != NULL &&
6707 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6709 phdr = (struct scsi_log_param_header *)data;
6710 scsi_ulto2b(0x0002, phdr->param_code);
6711 phdr->param_control = SLP_LBIN | SLP_LP;
6712 phdr->param_len = 8;
6713 data = (uint8_t *)(phdr + 1);
6714 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6715 data[4] = 0x01; /* per-LUN */
6716 data += phdr->param_len;
6719 if (lun->backend->lun_attr != NULL &&
6720 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6722 phdr = (struct scsi_log_param_header *)data;
6723 scsi_ulto2b(0x00f1, phdr->param_code);
6724 phdr->param_control = SLP_LBIN | SLP_LP;
6725 phdr->param_len = 8;
6726 data = (uint8_t *)(phdr + 1);
6727 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6728 data[4] = 0x02; /* per-pool */
6729 data += phdr->param_len;
6732 if (lun->backend->lun_attr != NULL &&
6733 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6735 phdr = (struct scsi_log_param_header *)data;
6736 scsi_ulto2b(0x00f2, phdr->param_code);
6737 phdr->param_control = SLP_LBIN | SLP_LP;
6738 phdr->param_len = 8;
6739 data = (uint8_t *)(phdr + 1);
6740 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6741 data[4] = 0x02; /* per-pool */
6742 data += phdr->param_len;
6745 page_index->page_len = data - page_index->page_data;
6750 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
6751 struct ctl_page_index *page_index,
6754 struct ctl_lun *lun = CTL_LUN(ctsio);
6755 struct stat_page *data;
6758 data = (struct stat_page *)page_index->page_data;
6760 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
6761 data->sap.hdr.param_control = SLP_LBIN;
6762 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
6763 sizeof(struct scsi_log_param_header);
6764 scsi_u64to8b(lun->stats.operations[CTL_STATS_READ],
6765 data->sap.read_num);
6766 scsi_u64to8b(lun->stats.operations[CTL_STATS_WRITE],
6767 data->sap.write_num);
6768 if (lun->be_lun->blocksize > 0) {
6769 scsi_u64to8b(lun->stats.bytes[CTL_STATS_WRITE] /
6770 lun->be_lun->blocksize, data->sap.recvieved_lba);
6771 scsi_u64to8b(lun->stats.bytes[CTL_STATS_READ] /
6772 lun->be_lun->blocksize, data->sap.transmitted_lba);
6774 t = &lun->stats.time[CTL_STATS_READ];
6775 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000),
6776 data->sap.read_int);
6777 t = &lun->stats.time[CTL_STATS_WRITE];
6778 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000),
6779 data->sap.write_int);
6780 scsi_u64to8b(0, data->sap.weighted_num);
6781 scsi_u64to8b(0, data->sap.weighted_int);
6782 scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
6783 data->it.hdr.param_control = SLP_LBIN;
6784 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
6785 sizeof(struct scsi_log_param_header);
6787 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
6789 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
6790 data->it.hdr.param_control = SLP_LBIN;
6791 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
6792 sizeof(struct scsi_log_param_header);
6793 scsi_ulto4b(3, data->ti.exponent);
6794 scsi_ulto4b(1, data->ti.integer);
6799 ctl_ie_log_sense_handler(struct ctl_scsiio *ctsio,
6800 struct ctl_page_index *page_index,
6803 struct ctl_lun *lun = CTL_LUN(ctsio);
6804 struct scsi_log_informational_exceptions *data;
6806 data = (struct scsi_log_informational_exceptions *)page_index->page_data;
6808 scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code);
6809 data->hdr.param_control = SLP_LBIN;
6810 data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) -
6811 sizeof(struct scsi_log_param_header);
6812 data->ie_asc = lun->ie_asc;
6813 data->ie_ascq = lun->ie_ascq;
6814 data->temperature = 0xff;
6819 ctl_log_sense(struct ctl_scsiio *ctsio)
6821 struct ctl_lun *lun = CTL_LUN(ctsio);
6822 int i, pc, page_code, subpage;
6823 int alloc_len, total_len;
6824 struct ctl_page_index *page_index;
6825 struct scsi_log_sense *cdb;
6826 struct scsi_log_header *header;
6828 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6830 cdb = (struct scsi_log_sense *)ctsio->cdb;
6831 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6832 page_code = cdb->page & SLS_PAGE_CODE;
6833 subpage = cdb->subpage;
6834 alloc_len = scsi_2btoul(cdb->length);
6837 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6838 page_index = &lun->log_pages.index[i];
6840 /* Look for the right page code */
6841 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6844 /* Look for the right subpage or the subpage wildcard*/
6845 if (page_index->subpage != subpage)
6850 if (i >= CTL_NUM_LOG_PAGES) {
6851 ctl_set_invalid_field(ctsio,
6857 ctl_done((union ctl_io *)ctsio);
6858 return (CTL_RETVAL_COMPLETE);
6861 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6863 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6864 ctsio->kern_sg_entries = 0;
6865 ctsio->kern_rel_offset = 0;
6866 ctsio->kern_data_len = min(total_len, alloc_len);
6867 ctsio->kern_total_len = ctsio->kern_data_len;
6869 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
6870 header->page = page_index->page_code;
6871 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING)
6872 header->page |= SL_DS;
6873 if (page_index->subpage) {
6874 header->page |= SL_SPF;
6875 header->subpage = page_index->subpage;
6877 scsi_ulto2b(page_index->page_len, header->datalen);
6880 * Call the handler, if it exists, to update the
6881 * page to the latest values.
6883 if (page_index->sense_handler != NULL)
6884 page_index->sense_handler(ctsio, page_index, pc);
6886 memcpy(header + 1, page_index->page_data, page_index->page_len);
6888 ctl_set_success(ctsio);
6889 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6890 ctsio->be_move_done = ctl_config_move_done;
6891 ctl_datamove((union ctl_io *)ctsio);
6892 return (CTL_RETVAL_COMPLETE);
6896 ctl_read_capacity(struct ctl_scsiio *ctsio)
6898 struct ctl_lun *lun = CTL_LUN(ctsio);
6899 struct scsi_read_capacity *cdb;
6900 struct scsi_read_capacity_data *data;
6903 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6905 cdb = (struct scsi_read_capacity *)ctsio->cdb;
6907 lba = scsi_4btoul(cdb->addr);
6908 if (((cdb->pmi & SRC_PMI) == 0)
6910 ctl_set_invalid_field(/*ctsio*/ ctsio,
6916 ctl_done((union ctl_io *)ctsio);
6917 return (CTL_RETVAL_COMPLETE);
6920 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6921 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6922 ctsio->kern_data_len = sizeof(*data);
6923 ctsio->kern_total_len = sizeof(*data);
6924 ctsio->kern_rel_offset = 0;
6925 ctsio->kern_sg_entries = 0;
6928 * If the maximum LBA is greater than 0xfffffffe, the user must
6929 * issue a SERVICE ACTION IN (16) command, with the read capacity
6930 * serivce action set.
6932 if (lun->be_lun->maxlba > 0xfffffffe)
6933 scsi_ulto4b(0xffffffff, data->addr);
6935 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6938 * XXX KDM this may not be 512 bytes...
6940 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6942 ctl_set_success(ctsio);
6943 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6944 ctsio->be_move_done = ctl_config_move_done;
6945 ctl_datamove((union ctl_io *)ctsio);
6946 return (CTL_RETVAL_COMPLETE);
6950 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
6952 struct ctl_lun *lun = CTL_LUN(ctsio);
6953 struct scsi_read_capacity_16 *cdb;
6954 struct scsi_read_capacity_data_long *data;
6958 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
6960 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
6962 alloc_len = scsi_4btoul(cdb->alloc_len);
6963 lba = scsi_8btou64(cdb->addr);
6965 if ((cdb->reladr & SRC16_PMI)
6967 ctl_set_invalid_field(/*ctsio*/ ctsio,
6973 ctl_done((union ctl_io *)ctsio);
6974 return (CTL_RETVAL_COMPLETE);
6977 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6978 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
6979 ctsio->kern_rel_offset = 0;
6980 ctsio->kern_sg_entries = 0;
6981 ctsio->kern_data_len = min(sizeof(*data), alloc_len);
6982 ctsio->kern_total_len = ctsio->kern_data_len;
6984 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
6985 /* XXX KDM this may not be 512 bytes... */
6986 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6987 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
6988 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
6989 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
6990 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
6992 ctl_set_success(ctsio);
6993 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6994 ctsio->be_move_done = ctl_config_move_done;
6995 ctl_datamove((union ctl_io *)ctsio);
6996 return (CTL_RETVAL_COMPLETE);
7000 ctl_get_lba_status(struct ctl_scsiio *ctsio)
7002 struct ctl_lun *lun = CTL_LUN(ctsio);
7003 struct scsi_get_lba_status *cdb;
7004 struct scsi_get_lba_status_data *data;
7005 struct ctl_lba_len_flags *lbalen;
7007 uint32_t alloc_len, total_len;
7010 CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
7012 cdb = (struct scsi_get_lba_status *)ctsio->cdb;
7013 lba = scsi_8btou64(cdb->addr);
7014 alloc_len = scsi_4btoul(cdb->alloc_len);
7016 if (lba > lun->be_lun->maxlba) {
7017 ctl_set_lba_out_of_range(ctsio, lba);
7018 ctl_done((union ctl_io *)ctsio);
7019 return (CTL_RETVAL_COMPLETE);
7022 total_len = sizeof(*data) + sizeof(data->descr[0]);
7023 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7024 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
7025 ctsio->kern_rel_offset = 0;
7026 ctsio->kern_sg_entries = 0;
7027 ctsio->kern_data_len = min(total_len, alloc_len);
7028 ctsio->kern_total_len = ctsio->kern_data_len;
7030 /* Fill dummy data in case backend can't tell anything. */
7031 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
7032 scsi_u64to8b(lba, data->descr[0].addr);
7033 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
7034 data->descr[0].length);
7035 data->descr[0].status = 0; /* Mapped or unknown. */
7037 ctl_set_success(ctsio);
7038 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7039 ctsio->be_move_done = ctl_config_move_done;
7041 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
7043 lbalen->len = total_len;
7045 retval = lun->backend->config_read((union ctl_io *)ctsio);
7050 ctl_read_defect(struct ctl_scsiio *ctsio)
7052 struct scsi_read_defect_data_10 *ccb10;
7053 struct scsi_read_defect_data_12 *ccb12;
7054 struct scsi_read_defect_data_hdr_10 *data10;
7055 struct scsi_read_defect_data_hdr_12 *data12;
7056 uint32_t alloc_len, data_len;
7059 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7061 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7062 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7063 format = ccb10->format;
7064 alloc_len = scsi_2btoul(ccb10->alloc_length);
7065 data_len = sizeof(*data10);
7067 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7068 format = ccb12->format;
7069 alloc_len = scsi_4btoul(ccb12->alloc_length);
7070 data_len = sizeof(*data12);
7072 if (alloc_len == 0) {
7073 ctl_set_success(ctsio);
7074 ctl_done((union ctl_io *)ctsio);
7075 return (CTL_RETVAL_COMPLETE);
7078 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7079 ctsio->kern_rel_offset = 0;
7080 ctsio->kern_sg_entries = 0;
7081 ctsio->kern_data_len = min(data_len, alloc_len);
7082 ctsio->kern_total_len = ctsio->kern_data_len;
7084 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7085 data10 = (struct scsi_read_defect_data_hdr_10 *)
7086 ctsio->kern_data_ptr;
7087 data10->format = format;
7088 scsi_ulto2b(0, data10->length);
7090 data12 = (struct scsi_read_defect_data_hdr_12 *)
7091 ctsio->kern_data_ptr;
7092 data12->format = format;
7093 scsi_ulto2b(0, data12->generation);
7094 scsi_ulto4b(0, data12->length);
7097 ctl_set_success(ctsio);
7098 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7099 ctsio->be_move_done = ctl_config_move_done;
7100 ctl_datamove((union ctl_io *)ctsio);
7101 return (CTL_RETVAL_COMPLETE);
7105 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7107 struct ctl_softc *softc = CTL_SOFTC(ctsio);
7108 struct ctl_lun *lun = CTL_LUN(ctsio);
7109 struct scsi_maintenance_in *cdb;
7111 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os;
7112 int num_ha_groups, num_target_ports, shared_group;
7113 struct ctl_port *port;
7114 struct scsi_target_group_data *rtg_ptr;
7115 struct scsi_target_group_data_extended *rtg_ext_ptr;
7116 struct scsi_target_port_group_descriptor *tpg_desc;
7118 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7120 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7121 retval = CTL_RETVAL_COMPLETE;
7123 switch (cdb->byte2 & STG_PDF_MASK) {
7124 case STG_PDF_LENGTH:
7127 case STG_PDF_EXTENDED:
7131 ctl_set_invalid_field(/*ctsio*/ ctsio,
7137 ctl_done((union ctl_io *)ctsio);
7141 num_target_ports = 0;
7142 shared_group = (softc->is_single != 0);
7143 mtx_lock(&softc->ctl_lock);
7144 STAILQ_FOREACH(port, &softc->port_list, links) {
7145 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7147 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7150 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7153 mtx_unlock(&softc->ctl_lock);
7154 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES;
7157 total_len = sizeof(struct scsi_target_group_data_extended);
7159 total_len = sizeof(struct scsi_target_group_data);
7160 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7161 (shared_group + num_ha_groups) +
7162 sizeof(struct scsi_target_port_descriptor) * num_target_ports;
7164 alloc_len = scsi_4btoul(cdb->length);
7166 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7167 ctsio->kern_sg_entries = 0;
7168 ctsio->kern_rel_offset = 0;
7169 ctsio->kern_data_len = min(total_len, alloc_len);
7170 ctsio->kern_total_len = ctsio->kern_data_len;
7173 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7174 ctsio->kern_data_ptr;
7175 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7176 rtg_ext_ptr->format_type = 0x10;
7177 rtg_ext_ptr->implicit_transition_time = 0;
7178 tpg_desc = &rtg_ext_ptr->groups[0];
7180 rtg_ptr = (struct scsi_target_group_data *)
7181 ctsio->kern_data_ptr;
7182 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7183 tpg_desc = &rtg_ptr->groups[0];
7186 mtx_lock(&softc->ctl_lock);
7187 pg = softc->port_min / softc->port_cnt;
7188 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) {
7189 /* Some shelf is known to be primary. */
7190 if (softc->ha_link == CTL_HA_LINK_OFFLINE)
7191 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7192 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN)
7193 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7194 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY)
7195 os = TPG_ASYMMETRIC_ACCESS_STANDBY;
7197 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7198 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7199 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7202 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7205 /* No known primary shelf. */
7206 if (softc->ha_link == CTL_HA_LINK_OFFLINE) {
7207 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7208 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7209 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) {
7210 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7211 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7213 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7217 tpg_desc->pref_state = ts;
7218 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7219 TPG_U_SUP | TPG_T_SUP;
7220 scsi_ulto2b(1, tpg_desc->target_port_group);
7221 tpg_desc->status = TPG_IMPLICIT;
7223 STAILQ_FOREACH(port, &softc->port_list, links) {
7224 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7226 if (!softc->is_single &&
7227 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0)
7229 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7231 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7232 relative_target_port_identifier);
7235 tpg_desc->target_port_count = pc;
7236 tpg_desc = (struct scsi_target_port_group_descriptor *)
7237 &tpg_desc->descriptors[pc];
7239 for (g = 0; g < num_ha_groups; g++) {
7240 tpg_desc->pref_state = (g == pg) ? ts : os;
7241 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7242 TPG_U_SUP | TPG_T_SUP;
7243 scsi_ulto2b(2 + g, tpg_desc->target_port_group);
7244 tpg_desc->status = TPG_IMPLICIT;
7246 STAILQ_FOREACH(port, &softc->port_list, links) {
7247 if (port->targ_port < g * softc->port_cnt ||
7248 port->targ_port >= (g + 1) * softc->port_cnt)
7250 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7252 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7254 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7256 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7257 relative_target_port_identifier);
7260 tpg_desc->target_port_count = pc;
7261 tpg_desc = (struct scsi_target_port_group_descriptor *)
7262 &tpg_desc->descriptors[pc];
7264 mtx_unlock(&softc->ctl_lock);
7266 ctl_set_success(ctsio);
7267 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7268 ctsio->be_move_done = ctl_config_move_done;
7269 ctl_datamove((union ctl_io *)ctsio);
7274 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7276 struct ctl_lun *lun = CTL_LUN(ctsio);
7277 struct scsi_report_supported_opcodes *cdb;
7278 const struct ctl_cmd_entry *entry, *sentry;
7279 struct scsi_report_supported_opcodes_all *all;
7280 struct scsi_report_supported_opcodes_descr *descr;
7281 struct scsi_report_supported_opcodes_one *one;
7283 int alloc_len, total_len;
7284 int opcode, service_action, i, j, num;
7286 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7288 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7289 retval = CTL_RETVAL_COMPLETE;
7291 opcode = cdb->requested_opcode;
7292 service_action = scsi_2btoul(cdb->requested_service_action);
7293 switch (cdb->options & RSO_OPTIONS_MASK) {
7294 case RSO_OPTIONS_ALL:
7296 for (i = 0; i < 256; i++) {
7297 entry = &ctl_cmd_table[i];
7298 if (entry->flags & CTL_CMD_FLAG_SA5) {
7299 for (j = 0; j < 32; j++) {
7300 sentry = &((const struct ctl_cmd_entry *)
7302 if (ctl_cmd_applicable(
7303 lun->be_lun->lun_type, sentry))
7307 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7312 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7313 num * sizeof(struct scsi_report_supported_opcodes_descr);
7315 case RSO_OPTIONS_OC:
7316 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7317 ctl_set_invalid_field(/*ctsio*/ ctsio,
7323 ctl_done((union ctl_io *)ctsio);
7324 return (CTL_RETVAL_COMPLETE);
7326 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7328 case RSO_OPTIONS_OC_SA:
7329 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7330 service_action >= 32) {
7331 ctl_set_invalid_field(/*ctsio*/ ctsio,
7337 ctl_done((union ctl_io *)ctsio);
7338 return (CTL_RETVAL_COMPLETE);
7341 case RSO_OPTIONS_OC_ASA:
7342 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7345 ctl_set_invalid_field(/*ctsio*/ ctsio,
7351 ctl_done((union ctl_io *)ctsio);
7352 return (CTL_RETVAL_COMPLETE);
7355 alloc_len = scsi_4btoul(cdb->length);
7357 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7358 ctsio->kern_sg_entries = 0;
7359 ctsio->kern_rel_offset = 0;
7360 ctsio->kern_data_len = min(total_len, alloc_len);
7361 ctsio->kern_total_len = ctsio->kern_data_len;
7363 switch (cdb->options & RSO_OPTIONS_MASK) {
7364 case RSO_OPTIONS_ALL:
7365 all = (struct scsi_report_supported_opcodes_all *)
7366 ctsio->kern_data_ptr;
7368 for (i = 0; i < 256; i++) {
7369 entry = &ctl_cmd_table[i];
7370 if (entry->flags & CTL_CMD_FLAG_SA5) {
7371 for (j = 0; j < 32; j++) {
7372 sentry = &((const struct ctl_cmd_entry *)
7374 if (!ctl_cmd_applicable(
7375 lun->be_lun->lun_type, sentry))
7377 descr = &all->descr[num++];
7379 scsi_ulto2b(j, descr->service_action);
7380 descr->flags = RSO_SERVACTV;
7381 scsi_ulto2b(sentry->length,
7385 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7388 descr = &all->descr[num++];
7390 scsi_ulto2b(0, descr->service_action);
7392 scsi_ulto2b(entry->length, descr->cdb_length);
7396 num * sizeof(struct scsi_report_supported_opcodes_descr),
7399 case RSO_OPTIONS_OC:
7400 one = (struct scsi_report_supported_opcodes_one *)
7401 ctsio->kern_data_ptr;
7402 entry = &ctl_cmd_table[opcode];
7404 case RSO_OPTIONS_OC_SA:
7405 one = (struct scsi_report_supported_opcodes_one *)
7406 ctsio->kern_data_ptr;
7407 entry = &ctl_cmd_table[opcode];
7408 entry = &((const struct ctl_cmd_entry *)
7409 entry->execute)[service_action];
7411 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7413 scsi_ulto2b(entry->length, one->cdb_length);
7414 one->cdb_usage[0] = opcode;
7415 memcpy(&one->cdb_usage[1], entry->usage,
7420 case RSO_OPTIONS_OC_ASA:
7421 one = (struct scsi_report_supported_opcodes_one *)
7422 ctsio->kern_data_ptr;
7423 entry = &ctl_cmd_table[opcode];
7424 if (entry->flags & CTL_CMD_FLAG_SA5) {
7425 entry = &((const struct ctl_cmd_entry *)
7426 entry->execute)[service_action];
7427 } else if (service_action != 0) {
7434 ctl_set_success(ctsio);
7435 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7436 ctsio->be_move_done = ctl_config_move_done;
7437 ctl_datamove((union ctl_io *)ctsio);
7442 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7444 struct scsi_report_supported_tmf *cdb;
7445 struct scsi_report_supported_tmf_ext_data *data;
7447 int alloc_len, total_len;
7449 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7451 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7453 retval = CTL_RETVAL_COMPLETE;
7455 if (cdb->options & RST_REPD)
7456 total_len = sizeof(struct scsi_report_supported_tmf_ext_data);
7458 total_len = sizeof(struct scsi_report_supported_tmf_data);
7459 alloc_len = scsi_4btoul(cdb->length);
7461 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7462 ctsio->kern_sg_entries = 0;
7463 ctsio->kern_rel_offset = 0;
7464 ctsio->kern_data_len = min(total_len, alloc_len);
7465 ctsio->kern_total_len = ctsio->kern_data_len;
7467 data = (struct scsi_report_supported_tmf_ext_data *)ctsio->kern_data_ptr;
7468 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS |
7470 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS;
7471 data->length = total_len - 4;
7473 ctl_set_success(ctsio);
7474 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7475 ctsio->be_move_done = ctl_config_move_done;
7476 ctl_datamove((union ctl_io *)ctsio);
7481 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7483 struct scsi_report_timestamp *cdb;
7484 struct scsi_report_timestamp_data *data;
7488 int alloc_len, total_len;
7490 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7492 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7494 retval = CTL_RETVAL_COMPLETE;
7496 total_len = sizeof(struct scsi_report_timestamp_data);
7497 alloc_len = scsi_4btoul(cdb->length);
7499 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7500 ctsio->kern_sg_entries = 0;
7501 ctsio->kern_rel_offset = 0;
7502 ctsio->kern_data_len = min(total_len, alloc_len);
7503 ctsio->kern_total_len = ctsio->kern_data_len;
7505 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7506 scsi_ulto2b(sizeof(*data) - 2, data->length);
7507 data->origin = RTS_ORIG_OUTSIDE;
7509 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7510 scsi_ulto4b(timestamp >> 16, data->timestamp);
7511 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7513 ctl_set_success(ctsio);
7514 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7515 ctsio->be_move_done = ctl_config_move_done;
7516 ctl_datamove((union ctl_io *)ctsio);
7521 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7523 struct ctl_softc *softc = CTL_SOFTC(ctsio);
7524 struct ctl_lun *lun = CTL_LUN(ctsio);
7525 struct scsi_per_res_in *cdb;
7526 int alloc_len, total_len = 0;
7527 /* struct scsi_per_res_in_rsrv in_data; */
7530 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7532 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7534 alloc_len = scsi_2btoul(cdb->length);
7537 mtx_lock(&lun->lun_lock);
7538 switch (cdb->action) {
7539 case SPRI_RK: /* read keys */
7540 total_len = sizeof(struct scsi_per_res_in_keys) +
7542 sizeof(struct scsi_per_res_key);
7544 case SPRI_RR: /* read reservation */
7545 if (lun->flags & CTL_LUN_PR_RESERVED)
7546 total_len = sizeof(struct scsi_per_res_in_rsrv);
7548 total_len = sizeof(struct scsi_per_res_in_header);
7550 case SPRI_RC: /* report capabilities */
7551 total_len = sizeof(struct scsi_per_res_cap);
7553 case SPRI_RS: /* read full status */
7554 total_len = sizeof(struct scsi_per_res_in_header) +
7555 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7559 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7561 mtx_unlock(&lun->lun_lock);
7563 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7564 ctsio->kern_rel_offset = 0;
7565 ctsio->kern_sg_entries = 0;
7566 ctsio->kern_data_len = min(total_len, alloc_len);
7567 ctsio->kern_total_len = ctsio->kern_data_len;
7569 mtx_lock(&lun->lun_lock);
7570 switch (cdb->action) {
7571 case SPRI_RK: { // read keys
7572 struct scsi_per_res_in_keys *res_keys;
7575 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7578 * We had to drop the lock to allocate our buffer, which
7579 * leaves time for someone to come in with another
7580 * persistent reservation. (That is unlikely, though,
7581 * since this should be the only persistent reservation
7582 * command active right now.)
7584 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7585 (lun->pr_key_count *
7586 sizeof(struct scsi_per_res_key)))){
7587 mtx_unlock(&lun->lun_lock);
7588 free(ctsio->kern_data_ptr, M_CTL);
7589 printf("%s: reservation length changed, retrying\n",
7594 scsi_ulto4b(lun->pr_generation, res_keys->header.generation);
7596 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7597 lun->pr_key_count, res_keys->header.length);
7599 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) {
7600 if ((key = ctl_get_prkey(lun, i)) == 0)
7604 * We used lun->pr_key_count to calculate the
7605 * size to allocate. If it turns out the number of
7606 * initiators with the registered flag set is
7607 * larger than that (i.e. they haven't been kept in
7608 * sync), we've got a problem.
7610 if (key_count >= lun->pr_key_count) {
7614 scsi_u64to8b(key, res_keys->keys[key_count].key);
7619 case SPRI_RR: { // read reservation
7620 struct scsi_per_res_in_rsrv *res;
7621 int tmp_len, header_only;
7623 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7625 scsi_ulto4b(lun->pr_generation, res->header.generation);
7627 if (lun->flags & CTL_LUN_PR_RESERVED)
7629 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7630 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7631 res->header.length);
7634 tmp_len = sizeof(struct scsi_per_res_in_header);
7635 scsi_ulto4b(0, res->header.length);
7640 * We had to drop the lock to allocate our buffer, which
7641 * leaves time for someone to come in with another
7642 * persistent reservation. (That is unlikely, though,
7643 * since this should be the only persistent reservation
7644 * command active right now.)
7646 if (tmp_len != total_len) {
7647 mtx_unlock(&lun->lun_lock);
7648 free(ctsio->kern_data_ptr, M_CTL);
7649 printf("%s: reservation status changed, retrying\n",
7655 * No reservation held, so we're done.
7657 if (header_only != 0)
7661 * If the registration is an All Registrants type, the key
7662 * is 0, since it doesn't really matter.
7664 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7665 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7666 res->data.reservation);
7668 res->data.scopetype = lun->pr_res_type;
7671 case SPRI_RC: //report capabilities
7673 struct scsi_per_res_cap *res_cap;
7676 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7677 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7678 res_cap->flags1 = SPRI_CRH;
7679 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5;
7680 type_mask = SPRI_TM_WR_EX_AR |
7686 scsi_ulto2b(type_mask, res_cap->type_mask);
7689 case SPRI_RS: { // read full status
7690 struct scsi_per_res_in_full *res_status;
7691 struct scsi_per_res_in_full_desc *res_desc;
7692 struct ctl_port *port;
7695 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7698 * We had to drop the lock to allocate our buffer, which
7699 * leaves time for someone to come in with another
7700 * persistent reservation. (That is unlikely, though,
7701 * since this should be the only persistent reservation
7702 * command active right now.)
7704 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7705 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7706 lun->pr_key_count)){
7707 mtx_unlock(&lun->lun_lock);
7708 free(ctsio->kern_data_ptr, M_CTL);
7709 printf("%s: reservation length changed, retrying\n",
7714 scsi_ulto4b(lun->pr_generation, res_status->header.generation);
7716 res_desc = &res_status->desc[0];
7717 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7718 if ((key = ctl_get_prkey(lun, i)) == 0)
7721 scsi_u64to8b(key, res_desc->res_key.key);
7722 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7723 (lun->pr_res_idx == i ||
7724 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7725 res_desc->flags = SPRI_FULL_R_HOLDER;
7726 res_desc->scopetype = lun->pr_res_type;
7728 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7729 res_desc->rel_trgt_port_id);
7731 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7733 len = ctl_create_iid(port,
7734 i % CTL_MAX_INIT_PER_PORT,
7735 res_desc->transport_id);
7736 scsi_ulto4b(len, res_desc->additional_length);
7737 res_desc = (struct scsi_per_res_in_full_desc *)
7738 &res_desc->transport_id[len];
7740 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7741 res_status->header.length);
7745 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7747 mtx_unlock(&lun->lun_lock);
7749 ctl_set_success(ctsio);
7750 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7751 ctsio->be_move_done = ctl_config_move_done;
7752 ctl_datamove((union ctl_io *)ctsio);
7753 return (CTL_RETVAL_COMPLETE);
7757 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7761 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7762 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7763 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7764 struct scsi_per_res_out_parms* param)
7766 union ctl_ha_msg persis_io;
7769 mtx_lock(&lun->lun_lock);
7770 if (sa_res_key == 0) {
7771 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7772 /* validate scope and type */
7773 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7775 mtx_unlock(&lun->lun_lock);
7776 ctl_set_invalid_field(/*ctsio*/ ctsio,
7782 ctl_done((union ctl_io *)ctsio);
7786 if (type>8 || type==2 || type==4 || type==0) {
7787 mtx_unlock(&lun->lun_lock);
7788 ctl_set_invalid_field(/*ctsio*/ ctsio,
7794 ctl_done((union ctl_io *)ctsio);
7799 * Unregister everybody else and build UA for
7802 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7803 if (i == residx || ctl_get_prkey(lun, i) == 0)
7806 ctl_clr_prkey(lun, i);
7807 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7809 lun->pr_key_count = 1;
7810 lun->pr_res_type = type;
7811 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7812 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7813 lun->pr_res_idx = residx;
7814 lun->pr_generation++;
7815 mtx_unlock(&lun->lun_lock);
7817 /* send msg to other side */
7818 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7819 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7820 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7821 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7822 persis_io.pr.pr_info.res_type = type;
7823 memcpy(persis_io.pr.pr_info.sa_res_key,
7824 param->serv_act_res_key,
7825 sizeof(param->serv_act_res_key));
7826 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7827 sizeof(persis_io.pr), M_WAITOK);
7829 /* not all registrants */
7830 mtx_unlock(&lun->lun_lock);
7831 free(ctsio->kern_data_ptr, M_CTL);
7832 ctl_set_invalid_field(ctsio,
7838 ctl_done((union ctl_io *)ctsio);
7841 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7842 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7845 if (res_key == sa_res_key) {
7848 * The spec implies this is not good but doesn't
7849 * say what to do. There are two choices either
7850 * generate a res conflict or check condition
7851 * with illegal field in parameter data. Since
7852 * that is what is done when the sa_res_key is
7853 * zero I'll take that approach since this has
7854 * to do with the sa_res_key.
7856 mtx_unlock(&lun->lun_lock);
7857 free(ctsio->kern_data_ptr, M_CTL);
7858 ctl_set_invalid_field(ctsio,
7864 ctl_done((union ctl_io *)ctsio);
7868 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7869 if (ctl_get_prkey(lun, i) != sa_res_key)
7873 ctl_clr_prkey(lun, i);
7874 lun->pr_key_count--;
7875 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7878 mtx_unlock(&lun->lun_lock);
7879 free(ctsio->kern_data_ptr, M_CTL);
7880 ctl_set_reservation_conflict(ctsio);
7881 ctl_done((union ctl_io *)ctsio);
7882 return (CTL_RETVAL_COMPLETE);
7884 lun->pr_generation++;
7885 mtx_unlock(&lun->lun_lock);
7887 /* send msg to other side */
7888 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7889 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7890 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7891 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7892 persis_io.pr.pr_info.res_type = type;
7893 memcpy(persis_io.pr.pr_info.sa_res_key,
7894 param->serv_act_res_key,
7895 sizeof(param->serv_act_res_key));
7896 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7897 sizeof(persis_io.pr), M_WAITOK);
7899 /* Reserved but not all registrants */
7900 /* sa_res_key is res holder */
7901 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
7902 /* validate scope and type */
7903 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7905 mtx_unlock(&lun->lun_lock);
7906 ctl_set_invalid_field(/*ctsio*/ ctsio,
7912 ctl_done((union ctl_io *)ctsio);
7916 if (type>8 || type==2 || type==4 || type==0) {
7917 mtx_unlock(&lun->lun_lock);
7918 ctl_set_invalid_field(/*ctsio*/ ctsio,
7924 ctl_done((union ctl_io *)ctsio);
7930 * if sa_res_key != res_key remove all
7931 * registrants w/sa_res_key and generate UA
7932 * for these registrants(Registrations
7933 * Preempted) if it wasn't an exclusive
7934 * reservation generate UA(Reservations
7935 * Preempted) for all other registered nexuses
7936 * if the type has changed. Establish the new
7937 * reservation and holder. If res_key and
7938 * sa_res_key are the same do the above
7939 * except don't unregister the res holder.
7942 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7943 if (i == residx || ctl_get_prkey(lun, i) == 0)
7946 if (sa_res_key == ctl_get_prkey(lun, i)) {
7947 ctl_clr_prkey(lun, i);
7948 lun->pr_key_count--;
7949 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7950 } else if (type != lun->pr_res_type &&
7951 (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
7952 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
7953 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
7956 lun->pr_res_type = type;
7957 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7958 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7959 lun->pr_res_idx = residx;
7961 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
7962 lun->pr_generation++;
7963 mtx_unlock(&lun->lun_lock);
7965 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7966 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7967 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7968 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7969 persis_io.pr.pr_info.res_type = type;
7970 memcpy(persis_io.pr.pr_info.sa_res_key,
7971 param->serv_act_res_key,
7972 sizeof(param->serv_act_res_key));
7973 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7974 sizeof(persis_io.pr), M_WAITOK);
7977 * sa_res_key is not the res holder just
7978 * remove registrants
7982 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7983 if (sa_res_key != ctl_get_prkey(lun, i))
7987 ctl_clr_prkey(lun, i);
7988 lun->pr_key_count--;
7989 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7993 mtx_unlock(&lun->lun_lock);
7994 free(ctsio->kern_data_ptr, M_CTL);
7995 ctl_set_reservation_conflict(ctsio);
7996 ctl_done((union ctl_io *)ctsio);
7999 lun->pr_generation++;
8000 mtx_unlock(&lun->lun_lock);
8002 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8003 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8004 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8005 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8006 persis_io.pr.pr_info.res_type = type;
8007 memcpy(persis_io.pr.pr_info.sa_res_key,
8008 param->serv_act_res_key,
8009 sizeof(param->serv_act_res_key));
8010 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8011 sizeof(persis_io.pr), M_WAITOK);
8018 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8020 uint64_t sa_res_key;
8023 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8025 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8026 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8027 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
8028 if (sa_res_key == 0) {
8030 * Unregister everybody else and build UA for
8033 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8034 if (i == msg->pr.pr_info.residx ||
8035 ctl_get_prkey(lun, i) == 0)
8038 ctl_clr_prkey(lun, i);
8039 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8042 lun->pr_key_count = 1;
8043 lun->pr_res_type = msg->pr.pr_info.res_type;
8044 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8045 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8046 lun->pr_res_idx = msg->pr.pr_info.residx;
8048 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8049 if (sa_res_key == ctl_get_prkey(lun, i))
8052 ctl_clr_prkey(lun, i);
8053 lun->pr_key_count--;
8054 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8058 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8059 if (i == msg->pr.pr_info.residx ||
8060 ctl_get_prkey(lun, i) == 0)
8063 if (sa_res_key == ctl_get_prkey(lun, i)) {
8064 ctl_clr_prkey(lun, i);
8065 lun->pr_key_count--;
8066 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8067 } else if (msg->pr.pr_info.res_type != lun->pr_res_type
8068 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8069 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8070 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8073 lun->pr_res_type = msg->pr.pr_info.res_type;
8074 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8075 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8076 lun->pr_res_idx = msg->pr.pr_info.residx;
8078 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8080 lun->pr_generation++;
8086 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8088 struct ctl_softc *softc = CTL_SOFTC(ctsio);
8089 struct ctl_lun *lun = CTL_LUN(ctsio);
8091 u_int32_t param_len;
8092 struct scsi_per_res_out *cdb;
8093 struct scsi_per_res_out_parms* param;
8095 uint64_t res_key, sa_res_key, key;
8097 union ctl_ha_msg persis_io;
8100 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8102 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8103 retval = CTL_RETVAL_COMPLETE;
8106 * We only support whole-LUN scope. The scope & type are ignored for
8107 * register, register and ignore existing key and clear.
8108 * We sometimes ignore scope and type on preempts too!!
8109 * Verify reservation type here as well.
8111 type = cdb->scope_type & SPR_TYPE_MASK;
8112 if ((cdb->action == SPRO_RESERVE)
8113 || (cdb->action == SPRO_RELEASE)) {
8114 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8115 ctl_set_invalid_field(/*ctsio*/ ctsio,
8121 ctl_done((union ctl_io *)ctsio);
8122 return (CTL_RETVAL_COMPLETE);
8125 if (type>8 || type==2 || type==4 || type==0) {
8126 ctl_set_invalid_field(/*ctsio*/ ctsio,
8132 ctl_done((union ctl_io *)ctsio);
8133 return (CTL_RETVAL_COMPLETE);
8137 param_len = scsi_4btoul(cdb->length);
8139 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8140 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8141 ctsio->kern_data_len = param_len;
8142 ctsio->kern_total_len = param_len;
8143 ctsio->kern_rel_offset = 0;
8144 ctsio->kern_sg_entries = 0;
8145 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8146 ctsio->be_move_done = ctl_config_move_done;
8147 ctl_datamove((union ctl_io *)ctsio);
8149 return (CTL_RETVAL_COMPLETE);
8152 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8154 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8155 res_key = scsi_8btou64(param->res_key.key);
8156 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8159 * Validate the reservation key here except for SPRO_REG_IGNO
8160 * This must be done for all other service actions
8162 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8163 mtx_lock(&lun->lun_lock);
8164 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8165 if (res_key != key) {
8167 * The current key passed in doesn't match
8168 * the one the initiator previously
8171 mtx_unlock(&lun->lun_lock);
8172 free(ctsio->kern_data_ptr, M_CTL);
8173 ctl_set_reservation_conflict(ctsio);
8174 ctl_done((union ctl_io *)ctsio);
8175 return (CTL_RETVAL_COMPLETE);
8177 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8179 * We are not registered
8181 mtx_unlock(&lun->lun_lock);
8182 free(ctsio->kern_data_ptr, M_CTL);
8183 ctl_set_reservation_conflict(ctsio);
8184 ctl_done((union ctl_io *)ctsio);
8185 return (CTL_RETVAL_COMPLETE);
8186 } else if (res_key != 0) {
8188 * We are not registered and trying to register but
8189 * the register key isn't zero.
8191 mtx_unlock(&lun->lun_lock);
8192 free(ctsio->kern_data_ptr, M_CTL);
8193 ctl_set_reservation_conflict(ctsio);
8194 ctl_done((union ctl_io *)ctsio);
8195 return (CTL_RETVAL_COMPLETE);
8197 mtx_unlock(&lun->lun_lock);
8200 switch (cdb->action & SPRO_ACTION_MASK) {
8202 case SPRO_REG_IGNO: {
8205 printf("Registration received\n");
8209 * We don't support any of these options, as we report in
8210 * the read capabilities request (see
8211 * ctl_persistent_reserve_in(), above).
8213 if ((param->flags & SPR_SPEC_I_PT)
8214 || (param->flags & SPR_ALL_TG_PT)
8215 || (param->flags & SPR_APTPL)) {
8218 if (param->flags & SPR_APTPL)
8220 else if (param->flags & SPR_ALL_TG_PT)
8222 else /* SPR_SPEC_I_PT */
8225 free(ctsio->kern_data_ptr, M_CTL);
8226 ctl_set_invalid_field(ctsio,
8232 ctl_done((union ctl_io *)ctsio);
8233 return (CTL_RETVAL_COMPLETE);
8236 mtx_lock(&lun->lun_lock);
8239 * The initiator wants to clear the
8242 if (sa_res_key == 0) {
8244 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8245 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8246 && ctl_get_prkey(lun, residx) == 0)) {
8247 mtx_unlock(&lun->lun_lock);
8251 ctl_clr_prkey(lun, residx);
8252 lun->pr_key_count--;
8254 if (residx == lun->pr_res_idx) {
8255 lun->flags &= ~CTL_LUN_PR_RESERVED;
8256 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8258 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8259 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8260 lun->pr_key_count) {
8262 * If the reservation is a registrants
8263 * only type we need to generate a UA
8264 * for other registered inits. The
8265 * sense code should be RESERVATIONS
8269 for (i = softc->init_min; i < softc->init_max; i++){
8270 if (ctl_get_prkey(lun, i) == 0)
8273 CTL_UA_RES_RELEASE);
8276 lun->pr_res_type = 0;
8277 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8278 if (lun->pr_key_count==0) {
8279 lun->flags &= ~CTL_LUN_PR_RESERVED;
8280 lun->pr_res_type = 0;
8281 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8284 lun->pr_generation++;
8285 mtx_unlock(&lun->lun_lock);
8287 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8288 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8289 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8290 persis_io.pr.pr_info.residx = residx;
8291 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8292 sizeof(persis_io.pr), M_WAITOK);
8293 } else /* sa_res_key != 0 */ {
8296 * If we aren't registered currently then increment
8297 * the key count and set the registered flag.
8299 ctl_alloc_prkey(lun, residx);
8300 if (ctl_get_prkey(lun, residx) == 0)
8301 lun->pr_key_count++;
8302 ctl_set_prkey(lun, residx, sa_res_key);
8303 lun->pr_generation++;
8304 mtx_unlock(&lun->lun_lock);
8306 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8307 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8308 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8309 persis_io.pr.pr_info.residx = residx;
8310 memcpy(persis_io.pr.pr_info.sa_res_key,
8311 param->serv_act_res_key,
8312 sizeof(param->serv_act_res_key));
8313 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8314 sizeof(persis_io.pr), M_WAITOK);
8321 printf("Reserve executed type %d\n", type);
8323 mtx_lock(&lun->lun_lock);
8324 if (lun->flags & CTL_LUN_PR_RESERVED) {
8326 * if this isn't the reservation holder and it's
8327 * not a "all registrants" type or if the type is
8328 * different then we have a conflict
8330 if ((lun->pr_res_idx != residx
8331 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8332 || lun->pr_res_type != type) {
8333 mtx_unlock(&lun->lun_lock);
8334 free(ctsio->kern_data_ptr, M_CTL);
8335 ctl_set_reservation_conflict(ctsio);
8336 ctl_done((union ctl_io *)ctsio);
8337 return (CTL_RETVAL_COMPLETE);
8339 mtx_unlock(&lun->lun_lock);
8340 } else /* create a reservation */ {
8342 * If it's not an "all registrants" type record
8343 * reservation holder
8345 if (type != SPR_TYPE_WR_EX_AR
8346 && type != SPR_TYPE_EX_AC_AR)
8347 lun->pr_res_idx = residx; /* Res holder */
8349 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8351 lun->flags |= CTL_LUN_PR_RESERVED;
8352 lun->pr_res_type = type;
8354 mtx_unlock(&lun->lun_lock);
8356 /* send msg to other side */
8357 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8358 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8359 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8360 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8361 persis_io.pr.pr_info.res_type = type;
8362 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8363 sizeof(persis_io.pr), M_WAITOK);
8368 mtx_lock(&lun->lun_lock);
8369 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8370 /* No reservation exists return good status */
8371 mtx_unlock(&lun->lun_lock);
8375 * Is this nexus a reservation holder?
8377 if (lun->pr_res_idx != residx
8378 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8380 * not a res holder return good status but
8383 mtx_unlock(&lun->lun_lock);
8387 if (lun->pr_res_type != type) {
8388 mtx_unlock(&lun->lun_lock);
8389 free(ctsio->kern_data_ptr, M_CTL);
8390 ctl_set_illegal_pr_release(ctsio);
8391 ctl_done((union ctl_io *)ctsio);
8392 return (CTL_RETVAL_COMPLETE);
8395 /* okay to release */
8396 lun->flags &= ~CTL_LUN_PR_RESERVED;
8397 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8398 lun->pr_res_type = 0;
8401 * If this isn't an exclusive access reservation and NUAR
8402 * is not set, generate UA for all other registrants.
8404 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX &&
8405 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8406 for (i = softc->init_min; i < softc->init_max; i++) {
8407 if (i == residx || ctl_get_prkey(lun, i) == 0)
8409 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8412 mtx_unlock(&lun->lun_lock);
8414 /* Send msg to other side */
8415 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8416 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8417 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8418 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8419 sizeof(persis_io.pr), M_WAITOK);
8423 /* send msg to other side */
8425 mtx_lock(&lun->lun_lock);
8426 lun->flags &= ~CTL_LUN_PR_RESERVED;
8427 lun->pr_res_type = 0;
8428 lun->pr_key_count = 0;
8429 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8431 ctl_clr_prkey(lun, residx);
8432 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8433 if (ctl_get_prkey(lun, i) != 0) {
8434 ctl_clr_prkey(lun, i);
8435 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8437 lun->pr_generation++;
8438 mtx_unlock(&lun->lun_lock);
8440 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8441 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8442 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8443 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8444 sizeof(persis_io.pr), M_WAITOK);
8448 case SPRO_PRE_ABO: {
8451 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8452 residx, ctsio, cdb, param);
8454 return (CTL_RETVAL_COMPLETE);
8458 panic("%s: Invalid PR type %#x", __func__, cdb->action);
8462 free(ctsio->kern_data_ptr, M_CTL);
8463 ctl_set_success(ctsio);
8464 ctl_done((union ctl_io *)ctsio);
8470 * This routine is for handling a message from the other SC pertaining to
8471 * persistent reserve out. All the error checking will have been done
8472 * so only perorming the action need be done here to keep the two
8476 ctl_hndl_per_res_out_on_other_sc(union ctl_io *io)
8478 struct ctl_softc *softc = CTL_SOFTC(io);
8479 union ctl_ha_msg *msg = (union ctl_ha_msg *)&io->presio.pr_msg;
8480 struct ctl_lun *lun;
8482 uint32_t residx, targ_lun;
8484 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8485 mtx_lock(&softc->ctl_lock);
8486 if (targ_lun >= CTL_MAX_LUNS ||
8487 (lun = softc->ctl_luns[targ_lun]) == NULL) {
8488 mtx_unlock(&softc->ctl_lock);
8491 mtx_lock(&lun->lun_lock);
8492 mtx_unlock(&softc->ctl_lock);
8493 if (lun->flags & CTL_LUN_DISABLED) {
8494 mtx_unlock(&lun->lun_lock);
8497 residx = ctl_get_initindex(&msg->hdr.nexus);
8498 switch(msg->pr.pr_info.action) {
8499 case CTL_PR_REG_KEY:
8500 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8501 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8502 lun->pr_key_count++;
8503 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8504 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8505 lun->pr_generation++;
8508 case CTL_PR_UNREG_KEY:
8509 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8510 lun->pr_key_count--;
8512 /* XXX Need to see if the reservation has been released */
8513 /* if so do we need to generate UA? */
8514 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8515 lun->flags &= ~CTL_LUN_PR_RESERVED;
8516 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8518 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8519 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8520 lun->pr_key_count) {
8522 * If the reservation is a registrants
8523 * only type we need to generate a UA
8524 * for other registered inits. The
8525 * sense code should be RESERVATIONS
8529 for (i = softc->init_min; i < softc->init_max; i++) {
8530 if (ctl_get_prkey(lun, i) == 0)
8533 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8536 lun->pr_res_type = 0;
8537 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8538 if (lun->pr_key_count==0) {
8539 lun->flags &= ~CTL_LUN_PR_RESERVED;
8540 lun->pr_res_type = 0;
8541 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8544 lun->pr_generation++;
8547 case CTL_PR_RESERVE:
8548 lun->flags |= CTL_LUN_PR_RESERVED;
8549 lun->pr_res_type = msg->pr.pr_info.res_type;
8550 lun->pr_res_idx = msg->pr.pr_info.residx;
8554 case CTL_PR_RELEASE:
8556 * If this isn't an exclusive access reservation and NUAR
8557 * is not set, generate UA for all other registrants.
8559 if (lun->pr_res_type != SPR_TYPE_EX_AC &&
8560 lun->pr_res_type != SPR_TYPE_WR_EX &&
8561 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8562 for (i = softc->init_min; i < softc->init_max; i++)
8563 if (i == residx || ctl_get_prkey(lun, i) == 0)
8565 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8568 lun->flags &= ~CTL_LUN_PR_RESERVED;
8569 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8570 lun->pr_res_type = 0;
8573 case CTL_PR_PREEMPT:
8574 ctl_pro_preempt_other(lun, msg);
8577 lun->flags &= ~CTL_LUN_PR_RESERVED;
8578 lun->pr_res_type = 0;
8579 lun->pr_key_count = 0;
8580 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8582 for (i=0; i < CTL_MAX_INITIATORS; i++) {
8583 if (ctl_get_prkey(lun, i) == 0)
8585 ctl_clr_prkey(lun, i);
8586 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8588 lun->pr_generation++;
8592 mtx_unlock(&lun->lun_lock);
8596 ctl_read_write(struct ctl_scsiio *ctsio)
8598 struct ctl_lun *lun = CTL_LUN(ctsio);
8599 struct ctl_lba_len_flags *lbalen;
8601 uint32_t num_blocks;
8605 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8608 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8609 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8610 switch (ctsio->cdb[0]) {
8613 struct scsi_rw_6 *cdb;
8615 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8617 lba = scsi_3btoul(cdb->addr);
8618 /* only 5 bits are valid in the most significant address byte */
8620 num_blocks = cdb->length;
8622 * This is correct according to SBC-2.
8624 if (num_blocks == 0)
8630 struct scsi_rw_10 *cdb;
8632 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8633 if (cdb->byte2 & SRW10_FUA)
8634 flags |= CTL_LLF_FUA;
8635 if (cdb->byte2 & SRW10_DPO)
8636 flags |= CTL_LLF_DPO;
8637 lba = scsi_4btoul(cdb->addr);
8638 num_blocks = scsi_2btoul(cdb->length);
8641 case WRITE_VERIFY_10: {
8642 struct scsi_write_verify_10 *cdb;
8644 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8645 flags |= CTL_LLF_FUA;
8646 if (cdb->byte2 & SWV_DPO)
8647 flags |= CTL_LLF_DPO;
8648 lba = scsi_4btoul(cdb->addr);
8649 num_blocks = scsi_2btoul(cdb->length);
8654 struct scsi_rw_12 *cdb;
8656 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8657 if (cdb->byte2 & SRW12_FUA)
8658 flags |= CTL_LLF_FUA;
8659 if (cdb->byte2 & SRW12_DPO)
8660 flags |= CTL_LLF_DPO;
8661 lba = scsi_4btoul(cdb->addr);
8662 num_blocks = scsi_4btoul(cdb->length);
8665 case WRITE_VERIFY_12: {
8666 struct scsi_write_verify_12 *cdb;
8668 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8669 flags |= CTL_LLF_FUA;
8670 if (cdb->byte2 & SWV_DPO)
8671 flags |= CTL_LLF_DPO;
8672 lba = scsi_4btoul(cdb->addr);
8673 num_blocks = scsi_4btoul(cdb->length);
8678 struct scsi_rw_16 *cdb;
8680 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8681 if (cdb->byte2 & SRW12_FUA)
8682 flags |= CTL_LLF_FUA;
8683 if (cdb->byte2 & SRW12_DPO)
8684 flags |= CTL_LLF_DPO;
8685 lba = scsi_8btou64(cdb->addr);
8686 num_blocks = scsi_4btoul(cdb->length);
8689 case WRITE_ATOMIC_16: {
8690 struct scsi_write_atomic_16 *cdb;
8692 if (lun->be_lun->atomicblock == 0) {
8693 ctl_set_invalid_opcode(ctsio);
8694 ctl_done((union ctl_io *)ctsio);
8695 return (CTL_RETVAL_COMPLETE);
8698 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb;
8699 if (cdb->byte2 & SRW12_FUA)
8700 flags |= CTL_LLF_FUA;
8701 if (cdb->byte2 & SRW12_DPO)
8702 flags |= CTL_LLF_DPO;
8703 lba = scsi_8btou64(cdb->addr);
8704 num_blocks = scsi_2btoul(cdb->length);
8705 if (num_blocks > lun->be_lun->atomicblock) {
8706 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8707 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8709 ctl_done((union ctl_io *)ctsio);
8710 return (CTL_RETVAL_COMPLETE);
8714 case WRITE_VERIFY_16: {
8715 struct scsi_write_verify_16 *cdb;
8717 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8718 flags |= CTL_LLF_FUA;
8719 if (cdb->byte2 & SWV_DPO)
8720 flags |= CTL_LLF_DPO;
8721 lba = scsi_8btou64(cdb->addr);
8722 num_blocks = scsi_4btoul(cdb->length);
8727 * We got a command we don't support. This shouldn't
8728 * happen, commands should be filtered out above us.
8730 ctl_set_invalid_opcode(ctsio);
8731 ctl_done((union ctl_io *)ctsio);
8733 return (CTL_RETVAL_COMPLETE);
8734 break; /* NOTREACHED */
8738 * The first check is to make sure we're in bounds, the second
8739 * check is to catch wrap-around problems. If the lba + num blocks
8740 * is less than the lba, then we've wrapped around and the block
8741 * range is invalid anyway.
8743 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8744 || ((lba + num_blocks) < lba)) {
8745 ctl_set_lba_out_of_range(ctsio,
8746 MAX(lba, lun->be_lun->maxlba + 1));
8747 ctl_done((union ctl_io *)ctsio);
8748 return (CTL_RETVAL_COMPLETE);
8752 * According to SBC-3, a transfer length of 0 is not an error.
8753 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8754 * translates to 256 blocks for those commands.
8756 if (num_blocks == 0) {
8757 ctl_set_success(ctsio);
8758 ctl_done((union ctl_io *)ctsio);
8759 return (CTL_RETVAL_COMPLETE);
8762 /* Set FUA and/or DPO if caches are disabled. */
8764 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0)
8765 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8767 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8768 flags |= CTL_LLF_FUA;
8771 lbalen = (struct ctl_lba_len_flags *)
8772 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8774 lbalen->len = num_blocks;
8775 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8777 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8778 ctsio->kern_rel_offset = 0;
8780 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8782 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8787 ctl_cnw_cont(union ctl_io *io)
8789 struct ctl_lun *lun = CTL_LUN(io);
8790 struct ctl_scsiio *ctsio;
8791 struct ctl_lba_len_flags *lbalen;
8794 ctsio = &io->scsiio;
8795 ctsio->io_hdr.status = CTL_STATUS_NONE;
8796 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8797 lbalen = (struct ctl_lba_len_flags *)
8798 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8799 lbalen->flags &= ~CTL_LLF_COMPARE;
8800 lbalen->flags |= CTL_LLF_WRITE;
8802 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8803 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8808 ctl_cnw(struct ctl_scsiio *ctsio)
8810 struct ctl_lun *lun = CTL_LUN(ctsio);
8811 struct ctl_lba_len_flags *lbalen;
8813 uint32_t num_blocks;
8816 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8819 switch (ctsio->cdb[0]) {
8820 case COMPARE_AND_WRITE: {
8821 struct scsi_compare_and_write *cdb;
8823 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8824 if (cdb->byte2 & SRW10_FUA)
8825 flags |= CTL_LLF_FUA;
8826 if (cdb->byte2 & SRW10_DPO)
8827 flags |= CTL_LLF_DPO;
8828 lba = scsi_8btou64(cdb->addr);
8829 num_blocks = cdb->length;
8834 * We got a command we don't support. This shouldn't
8835 * happen, commands should be filtered out above us.
8837 ctl_set_invalid_opcode(ctsio);
8838 ctl_done((union ctl_io *)ctsio);
8840 return (CTL_RETVAL_COMPLETE);
8841 break; /* NOTREACHED */
8845 * The first check is to make sure we're in bounds, the second
8846 * check is to catch wrap-around problems. If the lba + num blocks
8847 * is less than the lba, then we've wrapped around and the block
8848 * range is invalid anyway.
8850 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8851 || ((lba + num_blocks) < lba)) {
8852 ctl_set_lba_out_of_range(ctsio,
8853 MAX(lba, lun->be_lun->maxlba + 1));
8854 ctl_done((union ctl_io *)ctsio);
8855 return (CTL_RETVAL_COMPLETE);
8859 * According to SBC-3, a transfer length of 0 is not an error.
8861 if (num_blocks == 0) {
8862 ctl_set_success(ctsio);
8863 ctl_done((union ctl_io *)ctsio);
8864 return (CTL_RETVAL_COMPLETE);
8867 /* Set FUA if write cache is disabled. */
8868 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8869 flags |= CTL_LLF_FUA;
8871 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
8872 ctsio->kern_rel_offset = 0;
8875 * Set the IO_CONT flag, so that if this I/O gets passed to
8876 * ctl_data_submit_done(), it'll get passed back to
8877 * ctl_ctl_cnw_cont() for further processing.
8879 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
8880 ctsio->io_cont = ctl_cnw_cont;
8882 lbalen = (struct ctl_lba_len_flags *)
8883 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8885 lbalen->len = num_blocks;
8886 lbalen->flags = CTL_LLF_COMPARE | flags;
8888 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
8889 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8894 ctl_verify(struct ctl_scsiio *ctsio)
8896 struct ctl_lun *lun = CTL_LUN(ctsio);
8897 struct ctl_lba_len_flags *lbalen;
8899 uint32_t num_blocks;
8903 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
8906 flags = CTL_LLF_FUA;
8907 switch (ctsio->cdb[0]) {
8909 struct scsi_verify_10 *cdb;
8911 cdb = (struct scsi_verify_10 *)ctsio->cdb;
8912 if (cdb->byte2 & SVFY_BYTCHK)
8914 if (cdb->byte2 & SVFY_DPO)
8915 flags |= CTL_LLF_DPO;
8916 lba = scsi_4btoul(cdb->addr);
8917 num_blocks = scsi_2btoul(cdb->length);
8921 struct scsi_verify_12 *cdb;
8923 cdb = (struct scsi_verify_12 *)ctsio->cdb;
8924 if (cdb->byte2 & SVFY_BYTCHK)
8926 if (cdb->byte2 & SVFY_DPO)
8927 flags |= CTL_LLF_DPO;
8928 lba = scsi_4btoul(cdb->addr);
8929 num_blocks = scsi_4btoul(cdb->length);
8933 struct scsi_rw_16 *cdb;
8935 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8936 if (cdb->byte2 & SVFY_BYTCHK)
8938 if (cdb->byte2 & SVFY_DPO)
8939 flags |= CTL_LLF_DPO;
8940 lba = scsi_8btou64(cdb->addr);
8941 num_blocks = scsi_4btoul(cdb->length);
8946 * We got a command we don't support. This shouldn't
8947 * happen, commands should be filtered out above us.
8949 ctl_set_invalid_opcode(ctsio);
8950 ctl_done((union ctl_io *)ctsio);
8951 return (CTL_RETVAL_COMPLETE);
8955 * The first check is to make sure we're in bounds, the second
8956 * check is to catch wrap-around problems. If the lba + num blocks
8957 * is less than the lba, then we've wrapped around and the block
8958 * range is invalid anyway.
8960 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8961 || ((lba + num_blocks) < lba)) {
8962 ctl_set_lba_out_of_range(ctsio,
8963 MAX(lba, lun->be_lun->maxlba + 1));
8964 ctl_done((union ctl_io *)ctsio);
8965 return (CTL_RETVAL_COMPLETE);
8969 * According to SBC-3, a transfer length of 0 is not an error.
8971 if (num_blocks == 0) {
8972 ctl_set_success(ctsio);
8973 ctl_done((union ctl_io *)ctsio);
8974 return (CTL_RETVAL_COMPLETE);
8977 lbalen = (struct ctl_lba_len_flags *)
8978 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8980 lbalen->len = num_blocks;
8982 lbalen->flags = CTL_LLF_COMPARE | flags;
8983 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8985 lbalen->flags = CTL_LLF_VERIFY | flags;
8986 ctsio->kern_total_len = 0;
8988 ctsio->kern_rel_offset = 0;
8990 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
8991 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8996 ctl_report_luns(struct ctl_scsiio *ctsio)
8998 struct ctl_softc *softc = CTL_SOFTC(ctsio);
8999 struct ctl_port *port = CTL_PORT(ctsio);
9000 struct ctl_lun *lun, *request_lun = CTL_LUN(ctsio);
9001 struct scsi_report_luns *cdb;
9002 struct scsi_report_luns_data *lun_data;
9003 int num_filled, num_luns, num_port_luns, retval;
9004 uint32_t alloc_len, lun_datalen;
9005 uint32_t initidx, targ_lun_id, lun_id;
9007 retval = CTL_RETVAL_COMPLETE;
9008 cdb = (struct scsi_report_luns *)ctsio->cdb;
9010 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9013 num_port_luns = port->lun_map ? port->lun_map_size : CTL_MAX_LUNS;
9014 mtx_lock(&softc->ctl_lock);
9015 for (targ_lun_id = 0; targ_lun_id < num_port_luns; targ_lun_id++) {
9016 if (ctl_lun_map_from_port(port, targ_lun_id) != UINT32_MAX)
9019 mtx_unlock(&softc->ctl_lock);
9021 switch (cdb->select_report) {
9022 case RPL_REPORT_DEFAULT:
9023 case RPL_REPORT_ALL:
9024 case RPL_REPORT_NONSUBSID:
9026 case RPL_REPORT_WELLKNOWN:
9027 case RPL_REPORT_ADMIN:
9028 case RPL_REPORT_CONGLOM:
9032 ctl_set_invalid_field(ctsio,
9038 ctl_done((union ctl_io *)ctsio);
9040 break; /* NOTREACHED */
9043 alloc_len = scsi_4btoul(cdb->length);
9045 * The initiator has to allocate at least 16 bytes for this request,
9046 * so he can at least get the header and the first LUN. Otherwise
9047 * we reject the request (per SPC-3 rev 14, section 6.21).
9049 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9050 sizeof(struct scsi_report_luns_lundata))) {
9051 ctl_set_invalid_field(ctsio,
9057 ctl_done((union ctl_io *)ctsio);
9061 lun_datalen = sizeof(*lun_data) +
9062 (num_luns * sizeof(struct scsi_report_luns_lundata));
9064 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9065 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9066 ctsio->kern_sg_entries = 0;
9068 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9070 mtx_lock(&softc->ctl_lock);
9071 for (targ_lun_id = 0, num_filled = 0;
9072 targ_lun_id < num_port_luns && num_filled < num_luns;
9074 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9075 if (lun_id == UINT32_MAX)
9077 lun = softc->ctl_luns[lun_id];
9081 be64enc(lun_data->luns[num_filled++].lundata,
9082 ctl_encode_lun(targ_lun_id));
9085 * According to SPC-3, rev 14 section 6.21:
9087 * "The execution of a REPORT LUNS command to any valid and
9088 * installed logical unit shall clear the REPORTED LUNS DATA
9089 * HAS CHANGED unit attention condition for all logical
9090 * units of that target with respect to the requesting
9091 * initiator. A valid and installed logical unit is one
9092 * having a PERIPHERAL QUALIFIER of 000b in the standard
9093 * INQUIRY data (see 6.4.2)."
9095 * If request_lun is NULL, the LUN this report luns command
9096 * was issued to is either disabled or doesn't exist. In that
9097 * case, we shouldn't clear any pending lun change unit
9100 if (request_lun != NULL) {
9101 mtx_lock(&lun->lun_lock);
9102 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE);
9103 mtx_unlock(&lun->lun_lock);
9106 mtx_unlock(&softc->ctl_lock);
9109 * It's quite possible that we've returned fewer LUNs than we allocated
9110 * space for. Trim it.
9112 lun_datalen = sizeof(*lun_data) +
9113 (num_filled * sizeof(struct scsi_report_luns_lundata));
9114 ctsio->kern_rel_offset = 0;
9115 ctsio->kern_sg_entries = 0;
9116 ctsio->kern_data_len = min(lun_datalen, alloc_len);
9117 ctsio->kern_total_len = ctsio->kern_data_len;
9120 * We set this to the actual data length, regardless of how much
9121 * space we actually have to return results. If the user looks at
9122 * this value, he'll know whether or not he allocated enough space
9123 * and reissue the command if necessary. We don't support well
9124 * known logical units, so if the user asks for that, return none.
9126 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9129 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9132 ctl_set_success(ctsio);
9133 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9134 ctsio->be_move_done = ctl_config_move_done;
9135 ctl_datamove((union ctl_io *)ctsio);
9140 ctl_request_sense(struct ctl_scsiio *ctsio)
9142 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9143 struct ctl_lun *lun = CTL_LUN(ctsio);
9144 struct scsi_request_sense *cdb;
9145 struct scsi_sense_data *sense_ptr, *ps;
9148 u_int sense_len = SSD_FULL_SIZE;
9149 scsi_sense_data_type sense_format;
9150 ctl_ua_type ua_type;
9151 uint8_t asc = 0, ascq = 0;
9153 cdb = (struct scsi_request_sense *)ctsio->cdb;
9155 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9158 * Determine which sense format the user wants.
9160 if (cdb->byte2 & SRS_DESC)
9161 sense_format = SSD_TYPE_DESC;
9163 sense_format = SSD_TYPE_FIXED;
9165 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9166 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9167 ctsio->kern_sg_entries = 0;
9168 ctsio->kern_rel_offset = 0;
9171 * struct scsi_sense_data, which is currently set to 256 bytes, is
9172 * larger than the largest allowed value for the length field in the
9173 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9175 ctsio->kern_data_len = cdb->length;
9176 ctsio->kern_total_len = cdb->length;
9179 * If we don't have a LUN, we don't have any pending sense.
9182 ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
9183 softc->ha_link < CTL_HA_LINK_UNKNOWN)) {
9184 /* "Logical unit not supported" */
9185 ctl_set_sense_data(sense_ptr, &sense_len, NULL, sense_format,
9186 /*current_error*/ 1,
9187 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
9195 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9197 * Check for pending sense, and then for pending unit attentions.
9198 * Pending sense gets returned first, then pending unit attentions.
9200 mtx_lock(&lun->lun_lock);
9201 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT];
9203 ps += initidx % CTL_MAX_INIT_PER_PORT;
9204 if (ps != NULL && ps->error_code != 0) {
9205 scsi_sense_data_type stored_format;
9208 * Check to see which sense format was used for the stored
9211 stored_format = scsi_sense_type(ps);
9214 * If the user requested a different sense format than the
9215 * one we stored, then we need to convert it to the other
9216 * format. If we're going from descriptor to fixed format
9217 * sense data, we may lose things in translation, depending
9218 * on what options were used.
9220 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9221 * for some reason we'll just copy it out as-is.
9223 if ((stored_format == SSD_TYPE_FIXED)
9224 && (sense_format == SSD_TYPE_DESC))
9225 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9226 ps, (struct scsi_sense_data_desc *)sense_ptr);
9227 else if ((stored_format == SSD_TYPE_DESC)
9228 && (sense_format == SSD_TYPE_FIXED))
9229 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9230 ps, (struct scsi_sense_data_fixed *)sense_ptr);
9232 memcpy(sense_ptr, ps, sizeof(*sense_ptr));
9237 ua_type = ctl_build_ua(lun, initidx, sense_ptr, &sense_len,
9239 if (ua_type != CTL_UA_NONE)
9242 if (have_error == 0) {
9244 * Report informational exception if have one and allowed.
9246 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) {
9248 ascq = lun->ie_ascq;
9250 ctl_set_sense_data(sense_ptr, &sense_len, lun, sense_format,
9251 /*current_error*/ 1,
9252 /*sense_key*/ SSD_KEY_NO_SENSE,
9257 mtx_unlock(&lun->lun_lock);
9261 * We report the SCSI status as OK, since the status of the command
9262 * itself is OK. We're reporting sense as parameter data.
9264 ctl_set_success(ctsio);
9265 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9266 ctsio->be_move_done = ctl_config_move_done;
9267 ctl_datamove((union ctl_io *)ctsio);
9268 return (CTL_RETVAL_COMPLETE);
9272 ctl_tur(struct ctl_scsiio *ctsio)
9275 CTL_DEBUG_PRINT(("ctl_tur\n"));
9277 ctl_set_success(ctsio);
9278 ctl_done((union ctl_io *)ctsio);
9280 return (CTL_RETVAL_COMPLETE);
9284 * SCSI VPD page 0x00, the Supported VPD Pages page.
9287 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9289 struct ctl_lun *lun = CTL_LUN(ctsio);
9290 struct scsi_vpd_supported_pages *pages;
9294 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9295 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9296 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9297 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9298 ctsio->kern_rel_offset = 0;
9299 ctsio->kern_sg_entries = 0;
9300 ctsio->kern_data_len = min(sup_page_size, alloc_len);
9301 ctsio->kern_total_len = ctsio->kern_data_len;
9304 * The control device is always connected. The disk device, on the
9305 * other hand, may not be online all the time. Need to change this
9306 * to figure out whether the disk device is actually online or not.
9309 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9310 lun->be_lun->lun_type;
9312 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9315 /* Supported VPD pages */
9316 pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9318 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9319 /* Device Identification */
9320 pages->page_list[p++] = SVPD_DEVICE_ID;
9321 /* Extended INQUIRY Data */
9322 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9323 /* Mode Page Policy */
9324 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9326 pages->page_list[p++] = SVPD_SCSI_PORTS;
9327 /* Third-party Copy */
9328 pages->page_list[p++] = SVPD_SCSI_TPC;
9329 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9331 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9332 /* Block Device Characteristics */
9333 pages->page_list[p++] = SVPD_BDC;
9334 /* Logical Block Provisioning */
9335 pages->page_list[p++] = SVPD_LBP;
9339 ctl_set_success(ctsio);
9340 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9341 ctsio->be_move_done = ctl_config_move_done;
9342 ctl_datamove((union ctl_io *)ctsio);
9343 return (CTL_RETVAL_COMPLETE);
9347 * SCSI VPD page 0x80, the Unit Serial Number page.
9350 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9352 struct ctl_lun *lun = CTL_LUN(ctsio);
9353 struct scsi_vpd_unit_serial_number *sn_ptr;
9356 data_len = 4 + CTL_SN_LEN;
9357 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9358 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9359 ctsio->kern_rel_offset = 0;
9360 ctsio->kern_sg_entries = 0;
9361 ctsio->kern_data_len = min(data_len, alloc_len);
9362 ctsio->kern_total_len = ctsio->kern_data_len;
9365 * The control device is always connected. The disk device, on the
9366 * other hand, may not be online all the time. Need to change this
9367 * to figure out whether the disk device is actually online or not.
9370 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9371 lun->be_lun->lun_type;
9373 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9375 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9376 sn_ptr->length = CTL_SN_LEN;
9378 * If we don't have a LUN, we just leave the serial number as
9382 strncpy((char *)sn_ptr->serial_num,
9383 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9385 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9387 ctl_set_success(ctsio);
9388 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9389 ctsio->be_move_done = ctl_config_move_done;
9390 ctl_datamove((union ctl_io *)ctsio);
9391 return (CTL_RETVAL_COMPLETE);
9396 * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9399 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9401 struct ctl_lun *lun = CTL_LUN(ctsio);
9402 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9405 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9406 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9407 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9408 ctsio->kern_sg_entries = 0;
9409 ctsio->kern_rel_offset = 0;
9410 ctsio->kern_data_len = min(data_len, alloc_len);
9411 ctsio->kern_total_len = ctsio->kern_data_len;
9414 * The control device is always connected. The disk device, on the
9415 * other hand, may not be online all the time.
9418 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9419 lun->be_lun->lun_type;
9421 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9422 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9423 scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9425 * We support head of queue, ordered and simple tags.
9427 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9429 * Volatile cache supported.
9431 eid_ptr->flags3 = SVPD_EID_V_SUP;
9434 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9435 * attention for a particular IT nexus on all LUNs once we report
9436 * it to that nexus once. This bit is required as of SPC-4.
9438 eid_ptr->flags4 = SVPD_EID_LUICLR;
9441 * We support revert to defaults (RTD) bit in MODE SELECT.
9443 eid_ptr->flags5 = SVPD_EID_RTD_SUP;
9446 * XXX KDM in order to correctly answer this, we would need
9447 * information from the SIM to determine how much sense data it
9448 * can send. So this would really be a path inquiry field, most
9449 * likely. This can be set to a maximum of 252 according to SPC-4,
9450 * but the hardware may or may not be able to support that much.
9451 * 0 just means that the maximum sense data length is not reported.
9453 eid_ptr->max_sense_length = 0;
9455 ctl_set_success(ctsio);
9456 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9457 ctsio->be_move_done = ctl_config_move_done;
9458 ctl_datamove((union ctl_io *)ctsio);
9459 return (CTL_RETVAL_COMPLETE);
9463 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9465 struct ctl_lun *lun = CTL_LUN(ctsio);
9466 struct scsi_vpd_mode_page_policy *mpp_ptr;
9469 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9470 sizeof(struct scsi_vpd_mode_page_policy_descr);
9472 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9473 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9474 ctsio->kern_rel_offset = 0;
9475 ctsio->kern_sg_entries = 0;
9476 ctsio->kern_data_len = min(data_len, alloc_len);
9477 ctsio->kern_total_len = ctsio->kern_data_len;
9480 * The control device is always connected. The disk device, on the
9481 * other hand, may not be online all the time.
9484 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9485 lun->be_lun->lun_type;
9487 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9488 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9489 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9490 mpp_ptr->descr[0].page_code = 0x3f;
9491 mpp_ptr->descr[0].subpage_code = 0xff;
9492 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9494 ctl_set_success(ctsio);
9495 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9496 ctsio->be_move_done = ctl_config_move_done;
9497 ctl_datamove((union ctl_io *)ctsio);
9498 return (CTL_RETVAL_COMPLETE);
9502 * SCSI VPD page 0x83, the Device Identification page.
9505 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9507 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9508 struct ctl_port *port = CTL_PORT(ctsio);
9509 struct ctl_lun *lun = CTL_LUN(ctsio);
9510 struct scsi_vpd_device_id *devid_ptr;
9511 struct scsi_vpd_id_descriptor *desc;
9515 data_len = sizeof(struct scsi_vpd_device_id) +
9516 sizeof(struct scsi_vpd_id_descriptor) +
9517 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9518 sizeof(struct scsi_vpd_id_descriptor) +
9519 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9520 if (lun && lun->lun_devid)
9521 data_len += lun->lun_devid->len;
9522 if (port && port->port_devid)
9523 data_len += port->port_devid->len;
9524 if (port && port->target_devid)
9525 data_len += port->target_devid->len;
9527 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9528 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9529 ctsio->kern_sg_entries = 0;
9530 ctsio->kern_rel_offset = 0;
9531 ctsio->kern_sg_entries = 0;
9532 ctsio->kern_data_len = min(data_len, alloc_len);
9533 ctsio->kern_total_len = ctsio->kern_data_len;
9536 * The control device is always connected. The disk device, on the
9537 * other hand, may not be online all the time.
9540 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9541 lun->be_lun->lun_type;
9543 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9544 devid_ptr->page_code = SVPD_DEVICE_ID;
9545 scsi_ulto2b(data_len - 4, devid_ptr->length);
9547 if (port && port->port_type == CTL_PORT_FC)
9548 proto = SCSI_PROTO_FC << 4;
9549 else if (port && port->port_type == CTL_PORT_SAS)
9550 proto = SCSI_PROTO_SAS << 4;
9551 else if (port && port->port_type == CTL_PORT_ISCSI)
9552 proto = SCSI_PROTO_ISCSI << 4;
9554 proto = SCSI_PROTO_SPI << 4;
9555 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9558 * We're using a LUN association here. i.e., this device ID is a
9559 * per-LUN identifier.
9561 if (lun && lun->lun_devid) {
9562 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9563 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9564 lun->lun_devid->len);
9568 * This is for the WWPN which is a port association.
9570 if (port && port->port_devid) {
9571 memcpy(desc, port->port_devid->data, port->port_devid->len);
9572 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9573 port->port_devid->len);
9577 * This is for the Relative Target Port(type 4h) identifier
9579 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9580 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9581 SVPD_ID_TYPE_RELTARG;
9583 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9584 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9585 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9588 * This is for the Target Port Group(type 5h) identifier
9590 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9591 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9592 SVPD_ID_TYPE_TPORTGRP;
9594 if (softc->is_single ||
9595 (port && port->status & CTL_PORT_STATUS_HA_SHARED))
9598 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt;
9599 scsi_ulto2b(g, &desc->identifier[2]);
9600 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9601 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9604 * This is for the Target identifier
9606 if (port && port->target_devid) {
9607 memcpy(desc, port->target_devid->data, port->target_devid->len);
9610 ctl_set_success(ctsio);
9611 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9612 ctsio->be_move_done = ctl_config_move_done;
9613 ctl_datamove((union ctl_io *)ctsio);
9614 return (CTL_RETVAL_COMPLETE);
9618 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9620 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9621 struct ctl_lun *lun = CTL_LUN(ctsio);
9622 struct scsi_vpd_scsi_ports *sp;
9623 struct scsi_vpd_port_designation *pd;
9624 struct scsi_vpd_port_designation_cont *pdc;
9625 struct ctl_port *port;
9626 int data_len, num_target_ports, iid_len, id_len;
9628 num_target_ports = 0;
9631 mtx_lock(&softc->ctl_lock);
9632 STAILQ_FOREACH(port, &softc->port_list, links) {
9633 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9636 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9639 if (port->init_devid)
9640 iid_len += port->init_devid->len;
9641 if (port->port_devid)
9642 id_len += port->port_devid->len;
9644 mtx_unlock(&softc->ctl_lock);
9646 data_len = sizeof(struct scsi_vpd_scsi_ports) +
9647 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9648 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9649 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9650 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9651 ctsio->kern_sg_entries = 0;
9652 ctsio->kern_rel_offset = 0;
9653 ctsio->kern_sg_entries = 0;
9654 ctsio->kern_data_len = min(data_len, alloc_len);
9655 ctsio->kern_total_len = ctsio->kern_data_len;
9658 * The control device is always connected. The disk device, on the
9659 * other hand, may not be online all the time. Need to change this
9660 * to figure out whether the disk device is actually online or not.
9663 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9664 lun->be_lun->lun_type;
9666 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9668 sp->page_code = SVPD_SCSI_PORTS;
9669 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9671 pd = &sp->design[0];
9673 mtx_lock(&softc->ctl_lock);
9674 STAILQ_FOREACH(port, &softc->port_list, links) {
9675 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9678 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9680 scsi_ulto2b(port->targ_port, pd->relative_port_id);
9681 if (port->init_devid) {
9682 iid_len = port->init_devid->len;
9683 memcpy(pd->initiator_transportid,
9684 port->init_devid->data, port->init_devid->len);
9687 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9688 pdc = (struct scsi_vpd_port_designation_cont *)
9689 (&pd->initiator_transportid[iid_len]);
9690 if (port->port_devid) {
9691 id_len = port->port_devid->len;
9692 memcpy(pdc->target_port_descriptors,
9693 port->port_devid->data, port->port_devid->len);
9696 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9697 pd = (struct scsi_vpd_port_designation *)
9698 ((uint8_t *)pdc->target_port_descriptors + id_len);
9700 mtx_unlock(&softc->ctl_lock);
9702 ctl_set_success(ctsio);
9703 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9704 ctsio->be_move_done = ctl_config_move_done;
9705 ctl_datamove((union ctl_io *)ctsio);
9706 return (CTL_RETVAL_COMPLETE);
9710 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9712 struct ctl_lun *lun = CTL_LUN(ctsio);
9713 struct scsi_vpd_block_limits *bl_ptr;
9716 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9717 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9718 ctsio->kern_sg_entries = 0;
9719 ctsio->kern_rel_offset = 0;
9720 ctsio->kern_sg_entries = 0;
9721 ctsio->kern_data_len = min(sizeof(*bl_ptr), alloc_len);
9722 ctsio->kern_total_len = ctsio->kern_data_len;
9725 * The control device is always connected. The disk device, on the
9726 * other hand, may not be online all the time. Need to change this
9727 * to figure out whether the disk device is actually online or not.
9730 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9731 lun->be_lun->lun_type;
9733 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9735 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9736 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
9737 bl_ptr->max_cmp_write_len = 0xff;
9738 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9740 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
9741 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9743 ctl_get_opt_number(&lun->be_lun->options,
9744 "unmap_max_lba", &ival);
9745 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt);
9747 ctl_get_opt_number(&lun->be_lun->options,
9748 "unmap_max_descr", &ival);
9749 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt);
9750 if (lun->be_lun->ublockexp != 0) {
9751 scsi_ulto4b((1 << lun->be_lun->ublockexp),
9752 bl_ptr->opt_unmap_grain);
9753 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
9754 bl_ptr->unmap_grain_align);
9757 scsi_ulto4b(lun->be_lun->atomicblock,
9758 bl_ptr->max_atomic_transfer_length);
9759 scsi_ulto4b(0, bl_ptr->atomic_alignment);
9760 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
9761 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary);
9762 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size);
9764 ctl_get_opt_number(&lun->be_lun->options, "write_same_max_lba", &ival);
9765 scsi_u64to8b(ival, bl_ptr->max_write_same_length);
9768 ctl_set_success(ctsio);
9769 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9770 ctsio->be_move_done = ctl_config_move_done;
9771 ctl_datamove((union ctl_io *)ctsio);
9772 return (CTL_RETVAL_COMPLETE);
9776 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
9778 struct ctl_lun *lun = CTL_LUN(ctsio);
9779 struct scsi_vpd_block_device_characteristics *bdc_ptr;
9783 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
9784 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
9785 ctsio->kern_sg_entries = 0;
9786 ctsio->kern_rel_offset = 0;
9787 ctsio->kern_data_len = min(sizeof(*bdc_ptr), alloc_len);
9788 ctsio->kern_total_len = ctsio->kern_data_len;
9791 * The control device is always connected. The disk device, on the
9792 * other hand, may not be online all the time. Need to change this
9793 * to figure out whether the disk device is actually online or not.
9796 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9797 lun->be_lun->lun_type;
9799 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9800 bdc_ptr->page_code = SVPD_BDC;
9801 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
9803 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL)
9804 i = strtol(value, NULL, 0);
9806 i = CTL_DEFAULT_ROTATION_RATE;
9807 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
9809 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL)
9810 i = strtol(value, NULL, 0);
9813 bdc_ptr->wab_wac_ff = (i & 0x0f);
9814 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
9816 ctl_set_success(ctsio);
9817 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9818 ctsio->be_move_done = ctl_config_move_done;
9819 ctl_datamove((union ctl_io *)ctsio);
9820 return (CTL_RETVAL_COMPLETE);
9824 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
9826 struct ctl_lun *lun = CTL_LUN(ctsio);
9827 struct scsi_vpd_logical_block_prov *lbp_ptr;
9830 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
9831 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
9832 ctsio->kern_sg_entries = 0;
9833 ctsio->kern_rel_offset = 0;
9834 ctsio->kern_data_len = min(sizeof(*lbp_ptr), alloc_len);
9835 ctsio->kern_total_len = ctsio->kern_data_len;
9838 * The control device is always connected. The disk device, on the
9839 * other hand, may not be online all the time. Need to change this
9840 * to figure out whether the disk device is actually online or not.
9843 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9844 lun->be_lun->lun_type;
9846 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9848 lbp_ptr->page_code = SVPD_LBP;
9849 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
9850 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
9851 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9852 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
9853 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
9854 value = ctl_get_opt(&lun->be_lun->options, "provisioning_type");
9855 if (value != NULL) {
9856 if (strcmp(value, "resource") == 0)
9857 lbp_ptr->prov_type = SVPD_LBP_RESOURCE;
9858 else if (strcmp(value, "thin") == 0)
9859 lbp_ptr->prov_type = SVPD_LBP_THIN;
9861 lbp_ptr->prov_type = SVPD_LBP_THIN;
9864 ctl_set_success(ctsio);
9865 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9866 ctsio->be_move_done = ctl_config_move_done;
9867 ctl_datamove((union ctl_io *)ctsio);
9868 return (CTL_RETVAL_COMPLETE);
9872 * INQUIRY with the EVPD bit set.
9875 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
9877 struct ctl_lun *lun = CTL_LUN(ctsio);
9878 struct scsi_inquiry *cdb;
9879 int alloc_len, retval;
9881 cdb = (struct scsi_inquiry *)ctsio->cdb;
9882 alloc_len = scsi_2btoul(cdb->length);
9884 switch (cdb->page_code) {
9885 case SVPD_SUPPORTED_PAGES:
9886 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
9888 case SVPD_UNIT_SERIAL_NUMBER:
9889 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
9891 case SVPD_DEVICE_ID:
9892 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
9894 case SVPD_EXTENDED_INQUIRY_DATA:
9895 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
9897 case SVPD_MODE_PAGE_POLICY:
9898 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
9900 case SVPD_SCSI_PORTS:
9901 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
9904 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
9906 case SVPD_BLOCK_LIMITS:
9907 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
9909 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
9912 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
9914 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
9917 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
9919 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
9923 ctl_set_invalid_field(ctsio,
9929 ctl_done((union ctl_io *)ctsio);
9930 retval = CTL_RETVAL_COMPLETE;
9938 * Standard INQUIRY data.
9941 ctl_inquiry_std(struct ctl_scsiio *ctsio)
9943 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9944 struct ctl_port *port = CTL_PORT(ctsio);
9945 struct ctl_lun *lun = CTL_LUN(ctsio);
9946 struct scsi_inquiry_data *inq_ptr;
9947 struct scsi_inquiry *cdb;
9949 uint32_t alloc_len, data_len;
9950 ctl_port_type port_type;
9952 port_type = port->port_type;
9953 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
9954 port_type = CTL_PORT_SCSI;
9956 cdb = (struct scsi_inquiry *)ctsio->cdb;
9957 alloc_len = scsi_2btoul(cdb->length);
9960 * We malloc the full inquiry data size here and fill it
9961 * in. If the user only asks for less, we'll give him
9964 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
9965 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9966 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
9967 ctsio->kern_sg_entries = 0;
9968 ctsio->kern_rel_offset = 0;
9969 ctsio->kern_data_len = min(data_len, alloc_len);
9970 ctsio->kern_total_len = ctsio->kern_data_len;
9973 if ((lun->flags & CTL_LUN_PRIMARY_SC) ||
9974 softc->ha_link >= CTL_HA_LINK_UNKNOWN) {
9975 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9976 lun->be_lun->lun_type;
9978 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) |
9979 lun->be_lun->lun_type;
9981 if (lun->flags & CTL_LUN_REMOVABLE)
9982 inq_ptr->dev_qual2 |= SID_RMB;
9984 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
9986 /* RMB in byte 2 is 0 */
9987 inq_ptr->version = SCSI_REV_SPC5;
9990 * According to SAM-3, even if a device only supports a single
9991 * level of LUN addressing, it should still set the HISUP bit:
9993 * 4.9.1 Logical unit numbers overview
9995 * All logical unit number formats described in this standard are
9996 * hierarchical in structure even when only a single level in that
9997 * hierarchy is used. The HISUP bit shall be set to one in the
9998 * standard INQUIRY data (see SPC-2) when any logical unit number
9999 * format described in this standard is used. Non-hierarchical
10000 * formats are outside the scope of this standard.
10002 * Therefore we set the HiSup bit here.
10004 * The response format is 2, per SPC-3.
10006 inq_ptr->response_format = SID_HiSup | 2;
10008 inq_ptr->additional_length = data_len -
10009 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10010 CTL_DEBUG_PRINT(("additional_length = %d\n",
10011 inq_ptr->additional_length));
10013 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10014 if (port_type == CTL_PORT_SCSI)
10015 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10016 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10017 inq_ptr->flags = SID_CmdQue;
10018 if (port_type == CTL_PORT_SCSI)
10019 inq_ptr->flags |= SID_WBus16 | SID_Sync;
10022 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10023 * We have 8 bytes for the vendor name, and 16 bytes for the device
10024 * name and 4 bytes for the revision.
10026 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10027 "vendor")) == NULL) {
10028 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10030 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10031 strncpy(inq_ptr->vendor, val,
10032 min(sizeof(inq_ptr->vendor), strlen(val)));
10035 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10036 sizeof(inq_ptr->product));
10037 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10038 switch (lun->be_lun->lun_type) {
10040 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10041 sizeof(inq_ptr->product));
10044 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10045 sizeof(inq_ptr->product));
10048 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT,
10049 sizeof(inq_ptr->product));
10052 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10053 sizeof(inq_ptr->product));
10057 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10058 strncpy(inq_ptr->product, val,
10059 min(sizeof(inq_ptr->product), strlen(val)));
10063 * XXX make this a macro somewhere so it automatically gets
10064 * incremented when we make changes.
10066 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10067 "revision")) == NULL) {
10068 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10070 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10071 strncpy(inq_ptr->revision, val,
10072 min(sizeof(inq_ptr->revision), strlen(val)));
10076 * For parallel SCSI, we support double transition and single
10077 * transition clocking. We also support QAS (Quick Arbitration
10078 * and Selection) and Information Unit transfers on both the
10079 * control and array devices.
10081 if (port_type == CTL_PORT_SCSI)
10082 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10085 /* SAM-6 (no version claimed) */
10086 scsi_ulto2b(0x00C0, inq_ptr->version1);
10087 /* SPC-5 (no version claimed) */
10088 scsi_ulto2b(0x05C0, inq_ptr->version2);
10089 if (port_type == CTL_PORT_FC) {
10090 /* FCP-2 ANSI INCITS.350:2003 */
10091 scsi_ulto2b(0x0917, inq_ptr->version3);
10092 } else if (port_type == CTL_PORT_SCSI) {
10093 /* SPI-4 ANSI INCITS.362:200x */
10094 scsi_ulto2b(0x0B56, inq_ptr->version3);
10095 } else if (port_type == CTL_PORT_ISCSI) {
10096 /* iSCSI (no version claimed) */
10097 scsi_ulto2b(0x0960, inq_ptr->version3);
10098 } else if (port_type == CTL_PORT_SAS) {
10099 /* SAS (no version claimed) */
10100 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10101 } else if (port_type == CTL_PORT_UMASS) {
10102 /* USB Mass Storage Class Bulk-Only Transport, Revision 1.0 */
10103 scsi_ulto2b(0x1730, inq_ptr->version3);
10107 /* SBC-4 (no version claimed) */
10108 scsi_ulto2b(0x0600, inq_ptr->version4);
10110 switch (lun->be_lun->lun_type) {
10112 /* SBC-4 (no version claimed) */
10113 scsi_ulto2b(0x0600, inq_ptr->version4);
10118 /* MMC-6 (no version claimed) */
10119 scsi_ulto2b(0x04E0, inq_ptr->version4);
10126 ctl_set_success(ctsio);
10127 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10128 ctsio->be_move_done = ctl_config_move_done;
10129 ctl_datamove((union ctl_io *)ctsio);
10130 return (CTL_RETVAL_COMPLETE);
10134 ctl_inquiry(struct ctl_scsiio *ctsio)
10136 struct scsi_inquiry *cdb;
10139 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10141 cdb = (struct scsi_inquiry *)ctsio->cdb;
10142 if (cdb->byte2 & SI_EVPD)
10143 retval = ctl_inquiry_evpd(ctsio);
10144 else if (cdb->page_code == 0)
10145 retval = ctl_inquiry_std(ctsio);
10147 ctl_set_invalid_field(ctsio,
10153 ctl_done((union ctl_io *)ctsio);
10154 return (CTL_RETVAL_COMPLETE);
10161 ctl_get_config(struct ctl_scsiio *ctsio)
10163 struct ctl_lun *lun = CTL_LUN(ctsio);
10164 struct scsi_get_config_header *hdr;
10165 struct scsi_get_config_feature *feature;
10166 struct scsi_get_config *cdb;
10167 uint32_t alloc_len, data_len;
10170 cdb = (struct scsi_get_config *)ctsio->cdb;
10171 rt = (cdb->rt & SGC_RT_MASK);
10172 starting = scsi_2btoul(cdb->starting_feature);
10173 alloc_len = scsi_2btoul(cdb->length);
10175 data_len = sizeof(struct scsi_get_config_header) +
10176 sizeof(struct scsi_get_config_feature) + 8 +
10177 sizeof(struct scsi_get_config_feature) + 8 +
10178 sizeof(struct scsi_get_config_feature) + 4 +
10179 sizeof(struct scsi_get_config_feature) + 4 +
10180 sizeof(struct scsi_get_config_feature) + 8 +
10181 sizeof(struct scsi_get_config_feature) +
10182 sizeof(struct scsi_get_config_feature) + 4 +
10183 sizeof(struct scsi_get_config_feature) + 4 +
10184 sizeof(struct scsi_get_config_feature) + 4 +
10185 sizeof(struct scsi_get_config_feature) + 4 +
10186 sizeof(struct scsi_get_config_feature) + 4 +
10187 sizeof(struct scsi_get_config_feature) + 4;
10188 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10189 ctsio->kern_sg_entries = 0;
10190 ctsio->kern_rel_offset = 0;
10192 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr;
10193 if (lun->flags & CTL_LUN_NO_MEDIA)
10194 scsi_ulto2b(0x0000, hdr->current_profile);
10196 scsi_ulto2b(0x0010, hdr->current_profile);
10197 feature = (struct scsi_get_config_feature *)(hdr + 1);
10199 if (starting > 0x003b)
10201 if (starting > 0x003a)
10203 if (starting > 0x002b)
10205 if (starting > 0x002a)
10207 if (starting > 0x001f)
10209 if (starting > 0x001e)
10211 if (starting > 0x001d)
10213 if (starting > 0x0010)
10215 if (starting > 0x0003)
10217 if (starting > 0x0002)
10219 if (starting > 0x0001)
10221 if (starting > 0x0000)
10225 scsi_ulto2b(0x0000, feature->feature_code);
10226 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT;
10227 feature->add_length = 8;
10228 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */
10229 feature->feature_data[2] = 0x00;
10230 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */
10231 feature->feature_data[6] = 0x01;
10232 feature = (struct scsi_get_config_feature *)
10233 &feature->feature_data[feature->add_length];
10236 scsi_ulto2b(0x0001, feature->feature_code);
10237 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10238 feature->add_length = 8;
10239 scsi_ulto4b(0x00000000, &feature->feature_data[0]);
10240 feature->feature_data[4] = 0x03;
10241 feature = (struct scsi_get_config_feature *)
10242 &feature->feature_data[feature->add_length];
10245 scsi_ulto2b(0x0002, feature->feature_code);
10246 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10247 feature->add_length = 4;
10248 feature->feature_data[0] = 0x02;
10249 feature = (struct scsi_get_config_feature *)
10250 &feature->feature_data[feature->add_length];
10252 f3: /* Removable Medium */
10253 scsi_ulto2b(0x0003, feature->feature_code);
10254 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10255 feature->add_length = 4;
10256 feature->feature_data[0] = 0x39;
10257 feature = (struct scsi_get_config_feature *)
10258 &feature->feature_data[feature->add_length];
10260 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA))
10263 f10: /* Random Read */
10264 scsi_ulto2b(0x0010, feature->feature_code);
10265 feature->flags = 0x00;
10266 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10267 feature->flags |= SGC_F_CURRENT;
10268 feature->add_length = 8;
10269 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]);
10270 scsi_ulto2b(1, &feature->feature_data[4]);
10271 feature->feature_data[6] = 0x00;
10272 feature = (struct scsi_get_config_feature *)
10273 &feature->feature_data[feature->add_length];
10275 f1d: /* Multi-Read */
10276 scsi_ulto2b(0x001D, feature->feature_code);
10277 feature->flags = 0x00;
10278 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10279 feature->flags |= SGC_F_CURRENT;
10280 feature->add_length = 0;
10281 feature = (struct scsi_get_config_feature *)
10282 &feature->feature_data[feature->add_length];
10285 scsi_ulto2b(0x001E, feature->feature_code);
10286 feature->flags = 0x00;
10287 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10288 feature->flags |= SGC_F_CURRENT;
10289 feature->add_length = 4;
10290 feature->feature_data[0] = 0x00;
10291 feature = (struct scsi_get_config_feature *)
10292 &feature->feature_data[feature->add_length];
10294 f1f: /* DVD Read */
10295 scsi_ulto2b(0x001F, feature->feature_code);
10296 feature->flags = 0x08;
10297 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10298 feature->flags |= SGC_F_CURRENT;
10299 feature->add_length = 4;
10300 feature->feature_data[0] = 0x01;
10301 feature->feature_data[2] = 0x03;
10302 feature = (struct scsi_get_config_feature *)
10303 &feature->feature_data[feature->add_length];
10306 scsi_ulto2b(0x002A, feature->feature_code);
10307 feature->flags = 0x04;
10308 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10309 feature->flags |= SGC_F_CURRENT;
10310 feature->add_length = 4;
10311 feature->feature_data[0] = 0x00;
10312 feature->feature_data[1] = 0x00;
10313 feature = (struct scsi_get_config_feature *)
10314 &feature->feature_data[feature->add_length];
10317 scsi_ulto2b(0x002B, feature->feature_code);
10318 feature->flags = 0x00;
10319 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10320 feature->flags |= SGC_F_CURRENT;
10321 feature->add_length = 4;
10322 feature->feature_data[0] = 0x00;
10323 feature = (struct scsi_get_config_feature *)
10324 &feature->feature_data[feature->add_length];
10326 f3a: /* DVD+RW Dual Layer */
10327 scsi_ulto2b(0x003A, feature->feature_code);
10328 feature->flags = 0x00;
10329 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10330 feature->flags |= SGC_F_CURRENT;
10331 feature->add_length = 4;
10332 feature->feature_data[0] = 0x00;
10333 feature->feature_data[1] = 0x00;
10334 feature = (struct scsi_get_config_feature *)
10335 &feature->feature_data[feature->add_length];
10337 f3b: /* DVD+R Dual Layer */
10338 scsi_ulto2b(0x003B, feature->feature_code);
10339 feature->flags = 0x00;
10340 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10341 feature->flags |= SGC_F_CURRENT;
10342 feature->add_length = 4;
10343 feature->feature_data[0] = 0x00;
10344 feature = (struct scsi_get_config_feature *)
10345 &feature->feature_data[feature->add_length];
10348 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10349 if (rt == SGC_RT_SPECIFIC && data_len > 4) {
10350 feature = (struct scsi_get_config_feature *)(hdr + 1);
10351 if (scsi_2btoul(feature->feature_code) == starting)
10352 feature = (struct scsi_get_config_feature *)
10353 &feature->feature_data[feature->add_length];
10354 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10356 scsi_ulto4b(data_len - 4, hdr->data_length);
10357 ctsio->kern_data_len = min(data_len, alloc_len);
10358 ctsio->kern_total_len = ctsio->kern_data_len;
10360 ctl_set_success(ctsio);
10361 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10362 ctsio->be_move_done = ctl_config_move_done;
10363 ctl_datamove((union ctl_io *)ctsio);
10364 return (CTL_RETVAL_COMPLETE);
10368 ctl_get_event_status(struct ctl_scsiio *ctsio)
10370 struct scsi_get_event_status_header *hdr;
10371 struct scsi_get_event_status *cdb;
10372 uint32_t alloc_len, data_len;
10375 cdb = (struct scsi_get_event_status *)ctsio->cdb;
10376 if ((cdb->byte2 & SGESN_POLLED) == 0) {
10377 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
10378 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
10379 ctl_done((union ctl_io *)ctsio);
10380 return (CTL_RETVAL_COMPLETE);
10382 notif_class = cdb->notif_class;
10383 alloc_len = scsi_2btoul(cdb->length);
10385 data_len = sizeof(struct scsi_get_event_status_header);
10386 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10387 ctsio->kern_sg_entries = 0;
10388 ctsio->kern_rel_offset = 0;
10389 ctsio->kern_data_len = min(data_len, alloc_len);
10390 ctsio->kern_total_len = ctsio->kern_data_len;
10392 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr;
10393 scsi_ulto2b(0, hdr->descr_length);
10394 hdr->nea_class = SGESN_NEA;
10395 hdr->supported_class = 0;
10397 ctl_set_success(ctsio);
10398 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10399 ctsio->be_move_done = ctl_config_move_done;
10400 ctl_datamove((union ctl_io *)ctsio);
10401 return (CTL_RETVAL_COMPLETE);
10405 ctl_mechanism_status(struct ctl_scsiio *ctsio)
10407 struct scsi_mechanism_status_header *hdr;
10408 struct scsi_mechanism_status *cdb;
10409 uint32_t alloc_len, data_len;
10411 cdb = (struct scsi_mechanism_status *)ctsio->cdb;
10412 alloc_len = scsi_2btoul(cdb->length);
10414 data_len = sizeof(struct scsi_mechanism_status_header);
10415 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10416 ctsio->kern_sg_entries = 0;
10417 ctsio->kern_rel_offset = 0;
10418 ctsio->kern_data_len = min(data_len, alloc_len);
10419 ctsio->kern_total_len = ctsio->kern_data_len;
10421 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr;
10422 hdr->state1 = 0x00;
10423 hdr->state2 = 0xe0;
10424 scsi_ulto3b(0, hdr->lba);
10425 hdr->slots_num = 0;
10426 scsi_ulto2b(0, hdr->slots_length);
10428 ctl_set_success(ctsio);
10429 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10430 ctsio->be_move_done = ctl_config_move_done;
10431 ctl_datamove((union ctl_io *)ctsio);
10432 return (CTL_RETVAL_COMPLETE);
10436 ctl_ultomsf(uint32_t lba, uint8_t *buf)
10441 buf[1] = bin2bcd((lba / 75) / 60);
10442 buf[2] = bin2bcd((lba / 75) % 60);
10443 buf[3] = bin2bcd(lba % 75);
10447 ctl_read_toc(struct ctl_scsiio *ctsio)
10449 struct ctl_lun *lun = CTL_LUN(ctsio);
10450 struct scsi_read_toc_hdr *hdr;
10451 struct scsi_read_toc_type01_descr *descr;
10452 struct scsi_read_toc *cdb;
10453 uint32_t alloc_len, data_len;
10456 cdb = (struct scsi_read_toc *)ctsio->cdb;
10457 msf = (cdb->byte2 & CD_MSF) != 0;
10458 format = cdb->format;
10459 alloc_len = scsi_2btoul(cdb->data_len);
10461 data_len = sizeof(struct scsi_read_toc_hdr);
10463 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr);
10465 data_len += sizeof(struct scsi_read_toc_type01_descr);
10466 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10467 ctsio->kern_sg_entries = 0;
10468 ctsio->kern_rel_offset = 0;
10469 ctsio->kern_data_len = min(data_len, alloc_len);
10470 ctsio->kern_total_len = ctsio->kern_data_len;
10472 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr;
10474 scsi_ulto2b(0x12, hdr->data_length);
10477 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10478 descr->addr_ctl = 0x14;
10479 descr->track_number = 1;
10481 ctl_ultomsf(0, descr->track_start);
10483 scsi_ulto4b(0, descr->track_start);
10485 descr->addr_ctl = 0x14;
10486 descr->track_number = 0xaa;
10488 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start);
10490 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start);
10492 scsi_ulto2b(0x0a, hdr->data_length);
10495 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10496 descr->addr_ctl = 0x14;
10497 descr->track_number = 1;
10499 ctl_ultomsf(0, descr->track_start);
10501 scsi_ulto4b(0, descr->track_start);
10504 ctl_set_success(ctsio);
10505 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10506 ctsio->be_move_done = ctl_config_move_done;
10507 ctl_datamove((union ctl_io *)ctsio);
10508 return (CTL_RETVAL_COMPLETE);
10512 * For known CDB types, parse the LBA and length.
10515 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10517 if (io->io_hdr.io_type != CTL_IO_SCSI)
10520 switch (io->scsiio.cdb[0]) {
10521 case COMPARE_AND_WRITE: {
10522 struct scsi_compare_and_write *cdb;
10524 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10526 *lba = scsi_8btou64(cdb->addr);
10527 *len = cdb->length;
10532 struct scsi_rw_6 *cdb;
10534 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10536 *lba = scsi_3btoul(cdb->addr);
10537 /* only 5 bits are valid in the most significant address byte */
10539 *len = cdb->length;
10544 struct scsi_rw_10 *cdb;
10546 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10548 *lba = scsi_4btoul(cdb->addr);
10549 *len = scsi_2btoul(cdb->length);
10552 case WRITE_VERIFY_10: {
10553 struct scsi_write_verify_10 *cdb;
10555 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10557 *lba = scsi_4btoul(cdb->addr);
10558 *len = scsi_2btoul(cdb->length);
10563 struct scsi_rw_12 *cdb;
10565 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10567 *lba = scsi_4btoul(cdb->addr);
10568 *len = scsi_4btoul(cdb->length);
10571 case WRITE_VERIFY_12: {
10572 struct scsi_write_verify_12 *cdb;
10574 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10576 *lba = scsi_4btoul(cdb->addr);
10577 *len = scsi_4btoul(cdb->length);
10582 struct scsi_rw_16 *cdb;
10584 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10586 *lba = scsi_8btou64(cdb->addr);
10587 *len = scsi_4btoul(cdb->length);
10590 case WRITE_ATOMIC_16: {
10591 struct scsi_write_atomic_16 *cdb;
10593 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb;
10595 *lba = scsi_8btou64(cdb->addr);
10596 *len = scsi_2btoul(cdb->length);
10599 case WRITE_VERIFY_16: {
10600 struct scsi_write_verify_16 *cdb;
10602 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10604 *lba = scsi_8btou64(cdb->addr);
10605 *len = scsi_4btoul(cdb->length);
10608 case WRITE_SAME_10: {
10609 struct scsi_write_same_10 *cdb;
10611 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10613 *lba = scsi_4btoul(cdb->addr);
10614 *len = scsi_2btoul(cdb->length);
10617 case WRITE_SAME_16: {
10618 struct scsi_write_same_16 *cdb;
10620 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10622 *lba = scsi_8btou64(cdb->addr);
10623 *len = scsi_4btoul(cdb->length);
10627 struct scsi_verify_10 *cdb;
10629 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10631 *lba = scsi_4btoul(cdb->addr);
10632 *len = scsi_2btoul(cdb->length);
10636 struct scsi_verify_12 *cdb;
10638 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10640 *lba = scsi_4btoul(cdb->addr);
10641 *len = scsi_4btoul(cdb->length);
10645 struct scsi_verify_16 *cdb;
10647 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10649 *lba = scsi_8btou64(cdb->addr);
10650 *len = scsi_4btoul(cdb->length);
10658 case SERVICE_ACTION_IN: { /* GET LBA STATUS */
10659 struct scsi_get_lba_status *cdb;
10661 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10662 *lba = scsi_8btou64(cdb->addr);
10668 break; /* NOTREACHED */
10675 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10678 uint64_t endlba1, endlba2;
10680 endlba1 = lba1 + len1 - (seq ? 0 : 1);
10681 endlba2 = lba2 + len2 - 1;
10683 if ((endlba1 < lba2) || (endlba2 < lba1))
10684 return (CTL_ACTION_PASS);
10686 return (CTL_ACTION_BLOCK);
10690 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10692 struct ctl_ptr_len_flags *ptrlen;
10693 struct scsi_unmap_desc *buf, *end, *range;
10697 /* If not UNMAP -- go other way. */
10698 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10699 io->scsiio.cdb[0] != UNMAP)
10700 return (CTL_ACTION_ERROR);
10702 /* If UNMAP without data -- block and wait for data. */
10703 ptrlen = (struct ctl_ptr_len_flags *)
10704 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10705 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10706 ptrlen->ptr == NULL)
10707 return (CTL_ACTION_BLOCK);
10709 /* UNMAP with data -- check for collision. */
10710 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10711 end = buf + ptrlen->len / sizeof(*buf);
10712 for (range = buf; range < end; range++) {
10713 lba = scsi_8btou64(range->lba);
10714 len = scsi_4btoul(range->length);
10715 if ((lba < lba2 + len2) && (lba + len > lba2))
10716 return (CTL_ACTION_BLOCK);
10718 return (CTL_ACTION_PASS);
10722 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10724 uint64_t lba1, lba2;
10725 uint64_t len1, len2;
10728 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10729 return (CTL_ACTION_ERROR);
10731 retval = ctl_extent_check_unmap(io1, lba2, len2);
10732 if (retval != CTL_ACTION_ERROR)
10735 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10736 return (CTL_ACTION_ERROR);
10738 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10740 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
10744 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2)
10746 uint64_t lba1, lba2;
10747 uint64_t len1, len2;
10749 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10750 return (CTL_ACTION_PASS);
10751 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10752 return (CTL_ACTION_ERROR);
10753 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10754 return (CTL_ACTION_ERROR);
10756 if (lba1 + len1 == lba2)
10757 return (CTL_ACTION_BLOCK);
10758 return (CTL_ACTION_PASS);
10762 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10763 union ctl_io *ooa_io)
10765 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10766 const ctl_serialize_action *serialize_row;
10769 * The initiator attempted multiple untagged commands at the same
10770 * time. Can't do that.
10772 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10773 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10774 && ((pending_io->io_hdr.nexus.targ_port ==
10775 ooa_io->io_hdr.nexus.targ_port)
10776 && (pending_io->io_hdr.nexus.initid ==
10777 ooa_io->io_hdr.nexus.initid))
10778 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10779 CTL_FLAG_STATUS_SENT)) == 0))
10780 return (CTL_ACTION_OVERLAP);
10783 * The initiator attempted to send multiple tagged commands with
10784 * the same ID. (It's fine if different initiators have the same
10787 * Even if all of those conditions are true, we don't kill the I/O
10788 * if the command ahead of us has been aborted. We won't end up
10789 * sending it to the FETD, and it's perfectly legal to resend a
10790 * command with the same tag number as long as the previous
10791 * instance of this tag number has been aborted somehow.
10793 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10794 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10795 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10796 && ((pending_io->io_hdr.nexus.targ_port ==
10797 ooa_io->io_hdr.nexus.targ_port)
10798 && (pending_io->io_hdr.nexus.initid ==
10799 ooa_io->io_hdr.nexus.initid))
10800 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10801 CTL_FLAG_STATUS_SENT)) == 0))
10802 return (CTL_ACTION_OVERLAP_TAG);
10805 * If we get a head of queue tag, SAM-3 says that we should
10806 * immediately execute it.
10808 * What happens if this command would normally block for some other
10809 * reason? e.g. a request sense with a head of queue tag
10810 * immediately after a write. Normally that would block, but this
10811 * will result in its getting executed immediately...
10813 * We currently return "pass" instead of "skip", so we'll end up
10814 * going through the rest of the queue to check for overlapped tags.
10816 * XXX KDM check for other types of blockage first??
10818 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10819 return (CTL_ACTION_PASS);
10822 * Ordered tags have to block until all items ahead of them
10823 * have completed. If we get called with an ordered tag, we always
10824 * block, if something else is ahead of us in the queue.
10826 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10827 return (CTL_ACTION_BLOCK);
10830 * Simple tags get blocked until all head of queue and ordered tags
10831 * ahead of them have completed. I'm lumping untagged commands in
10832 * with simple tags here. XXX KDM is that the right thing to do?
10834 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10835 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10836 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10837 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10838 return (CTL_ACTION_BLOCK);
10840 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
10841 KASSERT(pending_entry->seridx < CTL_SERIDX_COUNT,
10842 ("%s: Invalid seridx %d for pending CDB %02x %02x @ %p",
10843 __func__, pending_entry->seridx, pending_io->scsiio.cdb[0],
10844 pending_io->scsiio.cdb[1], pending_io));
10845 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
10846 if (ooa_entry->seridx == CTL_SERIDX_INVLD)
10847 return (CTL_ACTION_PASS); /* Unsupported command in OOA queue */
10848 KASSERT(ooa_entry->seridx < CTL_SERIDX_COUNT,
10849 ("%s: Invalid seridx %d for ooa CDB %02x %02x @ %p",
10850 __func__, ooa_entry->seridx, ooa_io->scsiio.cdb[0],
10851 ooa_io->scsiio.cdb[1], ooa_io));
10853 serialize_row = ctl_serialize_table[ooa_entry->seridx];
10855 switch (serialize_row[pending_entry->seridx]) {
10856 case CTL_SER_BLOCK:
10857 return (CTL_ACTION_BLOCK);
10858 case CTL_SER_EXTENT:
10859 return (ctl_extent_check(ooa_io, pending_io,
10860 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10861 case CTL_SER_EXTENTOPT:
10862 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
10863 SCP_QUEUE_ALG_UNRESTRICTED)
10864 return (ctl_extent_check(ooa_io, pending_io,
10866 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10867 return (CTL_ACTION_PASS);
10868 case CTL_SER_EXTENTSEQ:
10869 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF)
10870 return (ctl_extent_check_seq(ooa_io, pending_io));
10871 return (CTL_ACTION_PASS);
10873 return (CTL_ACTION_PASS);
10874 case CTL_SER_BLOCKOPT:
10875 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
10876 SCP_QUEUE_ALG_UNRESTRICTED)
10877 return (CTL_ACTION_BLOCK);
10878 return (CTL_ACTION_PASS);
10880 return (CTL_ACTION_SKIP);
10882 panic("%s: Invalid serialization value %d for %d => %d",
10883 __func__, serialize_row[pending_entry->seridx],
10884 pending_entry->seridx, ooa_entry->seridx);
10887 return (CTL_ACTION_ERROR);
10891 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
10893 * - pending_io is generally either incoming, or on the blocked queue
10894 * - starting I/O is the I/O we want to start the check with.
10897 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
10898 union ctl_io *starting_io)
10900 union ctl_io *ooa_io;
10903 mtx_assert(&lun->lun_lock, MA_OWNED);
10906 * Run back along the OOA queue, starting with the current
10907 * blocked I/O and going through every I/O before it on the
10908 * queue. If starting_io is NULL, we'll just end up returning
10911 for (ooa_io = starting_io; ooa_io != NULL;
10912 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
10916 * This routine just checks to see whether
10917 * cur_blocked is blocked by ooa_io, which is ahead
10918 * of it in the queue. It doesn't queue/dequeue
10921 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
10923 case CTL_ACTION_BLOCK:
10924 case CTL_ACTION_OVERLAP:
10925 case CTL_ACTION_OVERLAP_TAG:
10926 case CTL_ACTION_SKIP:
10927 case CTL_ACTION_ERROR:
10929 break; /* NOTREACHED */
10930 case CTL_ACTION_PASS:
10933 panic("%s: Invalid action %d\n", __func__, action);
10937 return (CTL_ACTION_PASS);
10942 * - An I/O has just completed, and has been removed from the per-LUN OOA
10943 * queue, so some items on the blocked queue may now be unblocked.
10946 ctl_check_blocked(struct ctl_lun *lun)
10948 struct ctl_softc *softc = lun->ctl_softc;
10949 union ctl_io *cur_blocked, *next_blocked;
10951 mtx_assert(&lun->lun_lock, MA_OWNED);
10954 * Run forward from the head of the blocked queue, checking each
10955 * entry against the I/Os prior to it on the OOA queue to see if
10956 * there is still any blockage.
10958 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
10959 * with our removing a variable on it while it is traversing the
10962 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
10963 cur_blocked != NULL; cur_blocked = next_blocked) {
10964 union ctl_io *prev_ooa;
10967 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
10970 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
10971 ctl_ooaq, ooa_links);
10974 * If cur_blocked happens to be the first item in the OOA
10975 * queue now, prev_ooa will be NULL, and the action
10976 * returned will just be CTL_ACTION_PASS.
10978 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
10981 case CTL_ACTION_BLOCK:
10982 /* Nothing to do here, still blocked */
10984 case CTL_ACTION_OVERLAP:
10985 case CTL_ACTION_OVERLAP_TAG:
10987 * This shouldn't happen! In theory we've already
10988 * checked this command for overlap...
10991 case CTL_ACTION_PASS:
10992 case CTL_ACTION_SKIP: {
10993 const struct ctl_cmd_entry *entry;
10996 * The skip case shouldn't happen, this transaction
10997 * should have never made it onto the blocked queue.
11000 * This I/O is no longer blocked, we can remove it
11001 * from the blocked queue. Since this is a TAILQ
11002 * (doubly linked list), we can do O(1) removals
11003 * from any place on the list.
11005 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
11007 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11009 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
11010 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){
11012 * Need to send IO back to original side to
11015 union ctl_ha_msg msg_info;
11017 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11018 msg_info.hdr.original_sc =
11019 cur_blocked->io_hdr.original_sc;
11020 msg_info.hdr.serializing_sc = cur_blocked;
11021 msg_info.hdr.msg_type = CTL_MSG_R2R;
11022 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11023 sizeof(msg_info.hdr), M_NOWAIT);
11026 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
11029 * Check this I/O for LUN state changes that may
11030 * have happened while this command was blocked.
11031 * The LUN state may have been changed by a command
11032 * ahead of us in the queue, so we need to re-check
11033 * for any states that can be caused by SCSI
11036 if (ctl_scsiio_lun_check(lun, entry,
11037 &cur_blocked->scsiio) == 0) {
11038 cur_blocked->io_hdr.flags |=
11039 CTL_FLAG_IS_WAS_ON_RTR;
11040 ctl_enqueue_rtr(cur_blocked);
11042 ctl_done(cur_blocked);
11047 * This probably shouldn't happen -- we shouldn't
11048 * get CTL_ACTION_ERROR, or anything else.
11054 return (CTL_RETVAL_COMPLETE);
11058 * This routine (with one exception) checks LUN flags that can be set by
11059 * commands ahead of us in the OOA queue. These flags have to be checked
11060 * when a command initially comes in, and when we pull a command off the
11061 * blocked queue and are preparing to execute it. The reason we have to
11062 * check these flags for commands on the blocked queue is that the LUN
11063 * state may have been changed by a command ahead of us while we're on the
11066 * Ordering is somewhat important with these checks, so please pay
11067 * careful attention to the placement of any new checks.
11070 ctl_scsiio_lun_check(struct ctl_lun *lun,
11071 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11073 struct ctl_softc *softc = lun->ctl_softc;
11079 mtx_assert(&lun->lun_lock, MA_OWNED);
11082 * If this shelf is a secondary shelf controller, we may have to
11083 * reject some commands disallowed by HA mode and link state.
11085 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11086 if (softc->ha_link == CTL_HA_LINK_OFFLINE &&
11087 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11088 ctl_set_lun_unavail(ctsio);
11092 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 &&
11093 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11094 ctl_set_lun_transit(ctsio);
11098 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY &&
11099 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) {
11100 ctl_set_lun_standby(ctsio);
11105 /* The rest of checks are only done on executing side */
11106 if (softc->ha_mode == CTL_HA_MODE_XFER)
11110 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11112 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) {
11113 ctl_set_hw_write_protected(ctsio);
11117 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) {
11118 ctl_set_sense(ctsio, /*current_error*/ 1,
11119 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11120 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11127 * Check for a reservation conflict. If this command isn't allowed
11128 * even on reserved LUNs, and if this initiator isn't the one who
11129 * reserved us, reject the command with a reservation conflict.
11131 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11132 if ((lun->flags & CTL_LUN_RESERVED)
11133 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11134 if (lun->res_idx != residx) {
11135 ctl_set_reservation_conflict(ctsio);
11141 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11142 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11143 /* No reservation or command is allowed. */;
11144 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11145 (lun->pr_res_type == SPR_TYPE_WR_EX ||
11146 lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
11147 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) {
11148 /* The command is allowed for Write Exclusive resv. */;
11151 * if we aren't registered or it's a res holder type
11152 * reservation and this isn't the res holder then set a
11155 if (ctl_get_prkey(lun, residx) == 0 ||
11156 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) {
11157 ctl_set_reservation_conflict(ctsio);
11163 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) {
11164 if (lun->flags & CTL_LUN_EJECTED)
11165 ctl_set_lun_ejected(ctsio);
11166 else if (lun->flags & CTL_LUN_NO_MEDIA) {
11167 if (lun->flags & CTL_LUN_REMOVABLE)
11168 ctl_set_lun_no_media(ctsio);
11170 ctl_set_lun_int_reqd(ctsio);
11171 } else if (lun->flags & CTL_LUN_STOPPED)
11172 ctl_set_lun_stopped(ctsio);
11184 ctl_failover_io(union ctl_io *io, int have_lock)
11186 ctl_set_busy(&io->scsiio);
11191 ctl_failover_lun(union ctl_io *rio)
11193 struct ctl_softc *softc = CTL_SOFTC(rio);
11194 struct ctl_lun *lun;
11195 struct ctl_io_hdr *io, *next_io;
11198 targ_lun = rio->io_hdr.nexus.targ_mapped_lun;
11199 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun));
11201 /* Find and lock the LUN. */
11202 mtx_lock(&softc->ctl_lock);
11203 if (targ_lun > CTL_MAX_LUNS ||
11204 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11205 mtx_unlock(&softc->ctl_lock);
11208 mtx_lock(&lun->lun_lock);
11209 mtx_unlock(&softc->ctl_lock);
11210 if (lun->flags & CTL_LUN_DISABLED) {
11211 mtx_unlock(&lun->lun_lock);
11215 if (softc->ha_mode == CTL_HA_MODE_XFER) {
11216 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11217 /* We are master */
11218 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11219 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11220 io->flags |= CTL_FLAG_ABORT;
11221 io->flags |= CTL_FLAG_FAILOVER;
11222 } else { /* This can be only due to DATAMOVE */
11223 io->msg_type = CTL_MSG_DATAMOVE_DONE;
11224 io->flags &= ~CTL_FLAG_DMA_INPROG;
11225 io->flags |= CTL_FLAG_IO_ACTIVE;
11226 io->port_status = 31340;
11227 ctl_enqueue_isc((union ctl_io *)io);
11231 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11232 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11233 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11234 io->flags |= CTL_FLAG_FAILOVER;
11236 ctl_set_busy(&((union ctl_io *)io)->
11238 ctl_done((union ctl_io *)io);
11242 } else { /* SERIALIZE modes */
11243 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links,
11245 /* We are master */
11246 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11247 TAILQ_REMOVE(&lun->blocked_queue, io,
11249 io->flags &= ~CTL_FLAG_BLOCKED;
11250 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11251 ctl_free_io((union ctl_io *)io);
11254 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11255 /* We are master */
11256 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11257 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11258 ctl_free_io((union ctl_io *)io);
11261 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11262 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11263 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) {
11264 ctl_set_busy(&((union ctl_io *)io)->
11266 ctl_done((union ctl_io *)io);
11270 ctl_check_blocked(lun);
11272 mtx_unlock(&lun->lun_lock);
11276 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio)
11278 struct ctl_lun *lun;
11279 const struct ctl_cmd_entry *entry;
11280 uint32_t initidx, targ_lun;
11284 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11285 if (targ_lun < CTL_MAX_LUNS)
11286 lun = softc->ctl_luns[targ_lun];
11289 * If the LUN is invalid, pretend that it doesn't exist.
11290 * It will go away as soon as all pending I/O has been
11293 mtx_lock(&lun->lun_lock);
11294 if (lun->flags & CTL_LUN_DISABLED) {
11295 mtx_unlock(&lun->lun_lock);
11299 CTL_LUN(ctsio) = lun;
11301 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
11304 * Every I/O goes into the OOA queue for a particular LUN,
11305 * and stays there until completion.
11308 if (TAILQ_EMPTY(&lun->ooa_queue))
11309 lun->idle_time += getsbinuptime() - lun->last_busy;
11311 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
11314 /* Get command entry and return error if it is unsuppotyed. */
11315 entry = ctl_validate_command(ctsio);
11316 if (entry == NULL) {
11318 mtx_unlock(&lun->lun_lock);
11322 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11323 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11326 * Check to see whether we can send this command to LUNs that don't
11327 * exist. This should pretty much only be the case for inquiry
11328 * and request sense. Further checks, below, really require having
11329 * a LUN, so we can't really check the command anymore. Just put
11330 * it on the rtr queue.
11333 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) {
11334 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11335 ctl_enqueue_rtr((union ctl_io *)ctsio);
11339 ctl_set_unsupported_lun(ctsio);
11340 ctl_done((union ctl_io *)ctsio);
11341 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11345 * Make sure we support this particular command on this LUN.
11346 * e.g., we don't support writes to the control LUN.
11348 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11349 mtx_unlock(&lun->lun_lock);
11350 ctl_set_invalid_opcode(ctsio);
11351 ctl_done((union ctl_io *)ctsio);
11356 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11359 * If we've got a request sense, it'll clear the contingent
11360 * allegiance condition. Otherwise, if we have a CA condition for
11361 * this initiator, clear it, because it sent down a command other
11362 * than request sense.
11364 if (ctsio->cdb[0] != REQUEST_SENSE) {
11365 struct scsi_sense_data *ps;
11367 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT];
11369 ps[initidx % CTL_MAX_INIT_PER_PORT].error_code = 0;
11373 * If the command has this flag set, it handles its own unit
11374 * attention reporting, we shouldn't do anything. Otherwise we
11375 * check for any pending unit attentions, and send them back to the
11376 * initiator. We only do this when a command initially comes in,
11377 * not when we pull it off the blocked queue.
11379 * According to SAM-3, section 5.3.2, the order that things get
11380 * presented back to the host is basically unit attentions caused
11381 * by some sort of reset event, busy status, reservation conflicts
11382 * or task set full, and finally any other status.
11384 * One issue here is that some of the unit attentions we report
11385 * don't fall into the "reset" category (e.g. "reported luns data
11386 * has changed"). So reporting it here, before the reservation
11387 * check, may be technically wrong. I guess the only thing to do
11388 * would be to check for and report the reset events here, and then
11389 * check for the other unit attention types after we check for a
11390 * reservation conflict.
11392 * XXX KDM need to fix this
11394 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11395 ctl_ua_type ua_type;
11396 u_int sense_len = 0;
11398 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11399 &sense_len, SSD_TYPE_NONE);
11400 if (ua_type != CTL_UA_NONE) {
11401 mtx_unlock(&lun->lun_lock);
11402 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11403 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11404 ctsio->sense_len = sense_len;
11405 ctl_done((union ctl_io *)ctsio);
11411 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11412 mtx_unlock(&lun->lun_lock);
11413 ctl_done((union ctl_io *)ctsio);
11418 * XXX CHD this is where we want to send IO to other side if
11419 * this LUN is secondary on this SC. We will need to make a copy
11420 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11421 * the copy we send as FROM_OTHER.
11422 * We also need to stuff the address of the original IO so we can
11423 * find it easily. Something similar will need be done on the other
11424 * side so when we are done we can find the copy.
11426 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
11427 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 &&
11428 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) {
11429 union ctl_ha_msg msg_info;
11432 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11433 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11434 mtx_unlock(&lun->lun_lock);
11436 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11437 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11438 msg_info.hdr.serializing_sc = NULL;
11439 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11440 msg_info.scsi.tag_num = ctsio->tag_num;
11441 msg_info.scsi.tag_type = ctsio->tag_type;
11442 msg_info.scsi.cdb_len = ctsio->cdb_len;
11443 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11445 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11446 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data),
11447 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) {
11448 ctl_set_busy(ctsio);
11449 ctl_done((union ctl_io *)ctsio);
11455 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11456 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11457 ctl_ooaq, ooa_links))) {
11458 case CTL_ACTION_BLOCK:
11459 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11460 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11462 mtx_unlock(&lun->lun_lock);
11464 case CTL_ACTION_PASS:
11465 case CTL_ACTION_SKIP:
11466 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11467 mtx_unlock(&lun->lun_lock);
11468 ctl_enqueue_rtr((union ctl_io *)ctsio);
11470 case CTL_ACTION_OVERLAP:
11471 mtx_unlock(&lun->lun_lock);
11472 ctl_set_overlapped_cmd(ctsio);
11473 ctl_done((union ctl_io *)ctsio);
11475 case CTL_ACTION_OVERLAP_TAG:
11476 mtx_unlock(&lun->lun_lock);
11477 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11478 ctl_done((union ctl_io *)ctsio);
11480 case CTL_ACTION_ERROR:
11482 mtx_unlock(&lun->lun_lock);
11483 ctl_set_internal_failure(ctsio,
11485 /*retry_count*/ 0);
11486 ctl_done((union ctl_io *)ctsio);
11492 const struct ctl_cmd_entry *
11493 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11495 const struct ctl_cmd_entry *entry;
11496 int service_action;
11498 entry = &ctl_cmd_table[ctsio->cdb[0]];
11500 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11501 if (entry->flags & CTL_CMD_FLAG_SA5) {
11502 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11503 entry = &((const struct ctl_cmd_entry *)
11504 entry->execute)[service_action];
11509 const struct ctl_cmd_entry *
11510 ctl_validate_command(struct ctl_scsiio *ctsio)
11512 const struct ctl_cmd_entry *entry;
11516 entry = ctl_get_cmd_entry(ctsio, &sa);
11517 if (entry->execute == NULL) {
11519 ctl_set_invalid_field(ctsio,
11526 ctl_set_invalid_opcode(ctsio);
11527 ctl_done((union ctl_io *)ctsio);
11530 KASSERT(entry->length > 0,
11531 ("Not defined length for command 0x%02x/0x%02x",
11532 ctsio->cdb[0], ctsio->cdb[1]));
11533 for (i = 1; i < entry->length; i++) {
11534 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11537 ctl_set_invalid_field(ctsio,
11542 /*bit*/ fls(diff) - 1);
11543 ctl_done((union ctl_io *)ctsio);
11550 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11553 switch (lun_type) {
11555 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0)
11559 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
11563 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0)
11573 ctl_scsiio(struct ctl_scsiio *ctsio)
11576 const struct ctl_cmd_entry *entry;
11578 retval = CTL_RETVAL_COMPLETE;
11580 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11582 entry = ctl_get_cmd_entry(ctsio, NULL);
11585 * If this I/O has been aborted, just send it straight to
11586 * ctl_done() without executing it.
11588 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11589 ctl_done((union ctl_io *)ctsio);
11594 * All the checks should have been handled by ctl_scsiio_precheck().
11595 * We should be clear now to just execute the I/O.
11597 retval = entry->execute(ctsio);
11604 ctl_target_reset(union ctl_io *io)
11606 struct ctl_softc *softc = CTL_SOFTC(io);
11607 struct ctl_port *port = CTL_PORT(io);
11608 struct ctl_lun *lun;
11610 ctl_ua_type ua_type;
11612 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11613 union ctl_ha_msg msg_info;
11615 msg_info.hdr.nexus = io->io_hdr.nexus;
11616 msg_info.task.task_action = io->taskio.task_action;
11617 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11618 msg_info.hdr.original_sc = NULL;
11619 msg_info.hdr.serializing_sc = NULL;
11620 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11621 sizeof(msg_info.task), M_WAITOK);
11624 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11625 if (io->taskio.task_action == CTL_TASK_TARGET_RESET)
11626 ua_type = CTL_UA_TARG_RESET;
11628 ua_type = CTL_UA_BUS_RESET;
11629 mtx_lock(&softc->ctl_lock);
11630 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11631 if (port != NULL &&
11632 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
11634 ctl_do_lun_reset(lun, initidx, ua_type);
11636 mtx_unlock(&softc->ctl_lock);
11637 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11642 * The LUN should always be set. The I/O is optional, and is used to
11643 * distinguish between I/Os sent by this initiator, and by other
11644 * initiators. We set unit attention for initiators other than this one.
11645 * SAM-3 is vague on this point. It does say that a unit attention should
11646 * be established for other initiators when a LUN is reset (see section
11647 * 5.7.3), but it doesn't specifically say that the unit attention should
11648 * be established for this particular initiator when a LUN is reset. Here
11649 * is the relevant text, from SAM-3 rev 8:
11651 * 5.7.2 When a SCSI initiator port aborts its own tasks
11653 * When a SCSI initiator port causes its own task(s) to be aborted, no
11654 * notification that the task(s) have been aborted shall be returned to
11655 * the SCSI initiator port other than the completion response for the
11656 * command or task management function action that caused the task(s) to
11657 * be aborted and notification(s) associated with related effects of the
11658 * action (e.g., a reset unit attention condition).
11660 * XXX KDM for now, we're setting unit attention for all initiators.
11663 ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua_type)
11668 mtx_lock(&lun->lun_lock);
11670 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11671 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11672 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11675 for (i = 0; i < CTL_MAX_PORTS; i++) {
11676 free(lun->pending_sense[i], M_CTL);
11677 lun->pending_sense[i] = NULL;
11679 /* Clear reservation. */
11680 lun->flags &= ~CTL_LUN_RESERVED;
11681 /* Clear prevent media removal. */
11682 if (lun->prevent) {
11683 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11684 ctl_clear_mask(lun->prevent, i);
11685 lun->prevent_count = 0;
11687 /* Clear TPC status */
11688 ctl_tpc_lun_clear(lun, -1);
11689 /* Establish UA. */
11691 ctl_est_ua_all(lun, initidx, ua_type);
11693 ctl_est_ua_all(lun, -1, ua_type);
11695 mtx_unlock(&lun->lun_lock);
11699 ctl_lun_reset(union ctl_io *io)
11701 struct ctl_softc *softc = CTL_SOFTC(io);
11702 struct ctl_lun *lun;
11703 uint32_t targ_lun, initidx;
11705 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11706 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11707 mtx_lock(&softc->ctl_lock);
11708 if (targ_lun >= CTL_MAX_LUNS ||
11709 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11710 mtx_unlock(&softc->ctl_lock);
11711 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11714 ctl_do_lun_reset(lun, initidx, CTL_UA_LUN_RESET);
11715 mtx_unlock(&softc->ctl_lock);
11716 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11718 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) {
11719 union ctl_ha_msg msg_info;
11721 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11722 msg_info.hdr.nexus = io->io_hdr.nexus;
11723 msg_info.task.task_action = CTL_TASK_LUN_RESET;
11724 msg_info.hdr.original_sc = NULL;
11725 msg_info.hdr.serializing_sc = NULL;
11726 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11727 sizeof(msg_info.task), M_WAITOK);
11733 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11738 mtx_assert(&lun->lun_lock, MA_OWNED);
11741 * Run through the OOA queue and attempt to find the given I/O.
11742 * The target port, initiator ID, tag type and tag number have to
11743 * match the values that we got from the initiator. If we have an
11744 * untagged command to abort, simply abort the first untagged command
11745 * we come to. We only allow one untagged command at a time of course.
11747 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11748 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11750 if ((targ_port == UINT32_MAX ||
11751 targ_port == xio->io_hdr.nexus.targ_port) &&
11752 (init_id == UINT32_MAX ||
11753 init_id == xio->io_hdr.nexus.initid)) {
11754 if (targ_port != xio->io_hdr.nexus.targ_port ||
11755 init_id != xio->io_hdr.nexus.initid)
11756 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
11757 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11758 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11759 union ctl_ha_msg msg_info;
11761 msg_info.hdr.nexus = xio->io_hdr.nexus;
11762 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11763 msg_info.task.tag_num = xio->scsiio.tag_num;
11764 msg_info.task.tag_type = xio->scsiio.tag_type;
11765 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11766 msg_info.hdr.original_sc = NULL;
11767 msg_info.hdr.serializing_sc = NULL;
11768 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11769 sizeof(msg_info.task), M_NOWAIT);
11776 ctl_abort_task_set(union ctl_io *io)
11778 struct ctl_softc *softc = CTL_SOFTC(io);
11779 struct ctl_lun *lun;
11785 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11786 mtx_lock(&softc->ctl_lock);
11787 if (targ_lun >= CTL_MAX_LUNS ||
11788 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11789 mtx_unlock(&softc->ctl_lock);
11790 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11794 mtx_lock(&lun->lun_lock);
11795 mtx_unlock(&softc->ctl_lock);
11796 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
11797 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11798 io->io_hdr.nexus.initid,
11799 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11800 } else { /* CTL_TASK_CLEAR_TASK_SET */
11801 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
11802 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11804 mtx_unlock(&lun->lun_lock);
11805 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11810 ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx,
11811 ctl_ua_type ua_type)
11813 struct ctl_lun *lun;
11814 struct scsi_sense_data *ps;
11817 p = initidx / CTL_MAX_INIT_PER_PORT;
11818 i = initidx % CTL_MAX_INIT_PER_PORT;
11819 mtx_lock(&softc->ctl_lock);
11820 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11821 mtx_lock(&lun->lun_lock);
11823 ctl_abort_tasks_lun(lun, p, i, 1);
11825 ps = lun->pending_sense[p];
11827 ps[i].error_code = 0;
11828 /* Clear reservation. */
11829 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx))
11830 lun->flags &= ~CTL_LUN_RESERVED;
11831 /* Clear prevent media removal. */
11832 if (lun->prevent && ctl_is_set(lun->prevent, initidx)) {
11833 ctl_clear_mask(lun->prevent, initidx);
11834 lun->prevent_count--;
11836 /* Clear TPC status */
11837 ctl_tpc_lun_clear(lun, initidx);
11838 /* Establish UA. */
11839 ctl_est_ua(lun, initidx, ua_type);
11840 mtx_unlock(&lun->lun_lock);
11842 mtx_unlock(&softc->ctl_lock);
11846 ctl_i_t_nexus_reset(union ctl_io *io)
11848 struct ctl_softc *softc = CTL_SOFTC(io);
11851 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11852 union ctl_ha_msg msg_info;
11854 msg_info.hdr.nexus = io->io_hdr.nexus;
11855 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET;
11856 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11857 msg_info.hdr.original_sc = NULL;
11858 msg_info.hdr.serializing_sc = NULL;
11859 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11860 sizeof(msg_info.task), M_WAITOK);
11863 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11864 ctl_i_t_nexus_loss(softc, initidx, CTL_UA_I_T_NEXUS_LOSS);
11865 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11870 ctl_abort_task(union ctl_io *io)
11872 struct ctl_softc *softc = CTL_SOFTC(io);
11874 struct ctl_lun *lun;
11877 char printbuf[128];
11887 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11888 mtx_lock(&softc->ctl_lock);
11889 if (targ_lun >= CTL_MAX_LUNS ||
11890 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11891 mtx_unlock(&softc->ctl_lock);
11892 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11897 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
11898 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
11901 mtx_lock(&lun->lun_lock);
11902 mtx_unlock(&softc->ctl_lock);
11904 * Run through the OOA queue and attempt to find the given I/O.
11905 * The target port, initiator ID, tag type and tag number have to
11906 * match the values that we got from the initiator. If we have an
11907 * untagged command to abort, simply abort the first untagged command
11908 * we come to. We only allow one untagged command at a time of course.
11910 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11911 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11913 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
11915 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
11916 lun->lun, xio->scsiio.tag_num,
11917 xio->scsiio.tag_type,
11918 (xio->io_hdr.blocked_links.tqe_prev
11919 == NULL) ? "" : " BLOCKED",
11920 (xio->io_hdr.flags &
11921 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
11922 (xio->io_hdr.flags &
11923 CTL_FLAG_ABORT) ? " ABORT" : "",
11924 (xio->io_hdr.flags &
11925 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
11926 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
11928 printf("%s\n", sbuf_data(&sb));
11931 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
11932 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
11933 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
11937 * If the abort says that the task is untagged, the
11938 * task in the queue must be untagged. Otherwise,
11939 * we just check to see whether the tag numbers
11940 * match. This is because the QLogic firmware
11941 * doesn't pass back the tag type in an abort
11945 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
11946 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
11947 || (xio->scsiio.tag_num == io->taskio.tag_num))
11950 * XXX KDM we've got problems with FC, because it
11951 * doesn't send down a tag type with aborts. So we
11952 * can only really go by the tag number...
11953 * This may cause problems with parallel SCSI.
11954 * Need to figure that out!!
11956 if (xio->scsiio.tag_num == io->taskio.tag_num) {
11957 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11959 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
11960 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11961 union ctl_ha_msg msg_info;
11963 msg_info.hdr.nexus = io->io_hdr.nexus;
11964 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11965 msg_info.task.tag_num = io->taskio.tag_num;
11966 msg_info.task.tag_type = io->taskio.tag_type;
11967 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11968 msg_info.hdr.original_sc = NULL;
11969 msg_info.hdr.serializing_sc = NULL;
11971 printf("Sent Abort to other side\n");
11973 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11974 sizeof(msg_info.task), M_NOWAIT);
11977 printf("ctl_abort_task: found I/O to abort\n");
11981 mtx_unlock(&lun->lun_lock);
11985 * This isn't really an error. It's entirely possible for
11986 * the abort and command completion to cross on the wire.
11987 * This is more of an informative/diagnostic error.
11990 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
11991 "%u:%u:%u tag %d type %d\n",
11992 io->io_hdr.nexus.initid,
11993 io->io_hdr.nexus.targ_port,
11994 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
11995 io->taskio.tag_type);
11998 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12003 ctl_query_task(union ctl_io *io, int task_set)
12005 struct ctl_softc *softc = CTL_SOFTC(io);
12007 struct ctl_lun *lun;
12011 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12012 mtx_lock(&softc->ctl_lock);
12013 if (targ_lun >= CTL_MAX_LUNS ||
12014 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12015 mtx_unlock(&softc->ctl_lock);
12016 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12019 mtx_lock(&lun->lun_lock);
12020 mtx_unlock(&softc->ctl_lock);
12021 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12022 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12024 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12025 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12026 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12029 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) {
12034 mtx_unlock(&lun->lun_lock);
12036 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12038 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12043 ctl_query_async_event(union ctl_io *io)
12045 struct ctl_softc *softc = CTL_SOFTC(io);
12046 struct ctl_lun *lun;
12048 uint32_t targ_lun, initidx;
12050 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12051 mtx_lock(&softc->ctl_lock);
12052 if (targ_lun >= CTL_MAX_LUNS ||
12053 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12054 mtx_unlock(&softc->ctl_lock);
12055 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12058 mtx_lock(&lun->lun_lock);
12059 mtx_unlock(&softc->ctl_lock);
12060 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12061 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp);
12062 mtx_unlock(&lun->lun_lock);
12063 if (ua != CTL_UA_NONE)
12064 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12066 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12071 ctl_run_task(union ctl_io *io)
12075 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12076 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12077 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type));
12078 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED;
12079 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp));
12080 switch (io->taskio.task_action) {
12081 case CTL_TASK_ABORT_TASK:
12082 retval = ctl_abort_task(io);
12084 case CTL_TASK_ABORT_TASK_SET:
12085 case CTL_TASK_CLEAR_TASK_SET:
12086 retval = ctl_abort_task_set(io);
12088 case CTL_TASK_CLEAR_ACA:
12090 case CTL_TASK_I_T_NEXUS_RESET:
12091 retval = ctl_i_t_nexus_reset(io);
12093 case CTL_TASK_LUN_RESET:
12094 retval = ctl_lun_reset(io);
12096 case CTL_TASK_TARGET_RESET:
12097 case CTL_TASK_BUS_RESET:
12098 retval = ctl_target_reset(io);
12100 case CTL_TASK_PORT_LOGIN:
12102 case CTL_TASK_PORT_LOGOUT:
12104 case CTL_TASK_QUERY_TASK:
12105 retval = ctl_query_task(io, 0);
12107 case CTL_TASK_QUERY_TASK_SET:
12108 retval = ctl_query_task(io, 1);
12110 case CTL_TASK_QUERY_ASYNC_EVENT:
12111 retval = ctl_query_async_event(io);
12114 printf("%s: got unknown task management event %d\n",
12115 __func__, io->taskio.task_action);
12119 io->io_hdr.status = CTL_SUCCESS;
12121 io->io_hdr.status = CTL_ERROR;
12126 * For HA operation. Handle commands that come in from the other
12130 ctl_handle_isc(union ctl_io *io)
12132 struct ctl_softc *softc = CTL_SOFTC(io);
12133 struct ctl_lun *lun;
12134 const struct ctl_cmd_entry *entry;
12137 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12138 switch (io->io_hdr.msg_type) {
12139 case CTL_MSG_SERIALIZE:
12140 ctl_serialize_other_sc_cmd(&io->scsiio);
12142 case CTL_MSG_R2R: /* Only used in SER_ONLY mode. */
12143 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12144 if (targ_lun >= CTL_MAX_LUNS ||
12145 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12149 mtx_lock(&lun->lun_lock);
12150 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) {
12151 mtx_unlock(&lun->lun_lock);
12155 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12156 mtx_unlock(&lun->lun_lock);
12157 ctl_enqueue_rtr(io);
12159 case CTL_MSG_FINISH_IO:
12160 if (softc->ha_mode == CTL_HA_MODE_XFER) {
12164 if (targ_lun >= CTL_MAX_LUNS ||
12165 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12169 mtx_lock(&lun->lun_lock);
12170 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
12171 ctl_check_blocked(lun);
12172 mtx_unlock(&lun->lun_lock);
12175 case CTL_MSG_PERS_ACTION:
12176 ctl_hndl_per_res_out_on_other_sc(io);
12179 case CTL_MSG_BAD_JUJU:
12182 case CTL_MSG_DATAMOVE: /* Only used in XFER mode */
12183 ctl_datamove_remote(io);
12185 case CTL_MSG_DATAMOVE_DONE: /* Only used in XFER mode */
12186 io->scsiio.be_move_done(io);
12188 case CTL_MSG_FAILOVER:
12189 ctl_failover_lun(io);
12193 printf("%s: Invalid message type %d\n",
12194 __func__, io->io_hdr.msg_type);
12203 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12204 * there is no match.
12206 static ctl_lun_error_pattern
12207 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12209 const struct ctl_cmd_entry *entry;
12210 ctl_lun_error_pattern filtered_pattern, pattern;
12212 pattern = desc->error_pattern;
12215 * XXX KDM we need more data passed into this function to match a
12216 * custom pattern, and we actually need to implement custom pattern
12219 if (pattern & CTL_LUN_PAT_CMD)
12220 return (CTL_LUN_PAT_CMD);
12222 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12223 return (CTL_LUN_PAT_ANY);
12225 entry = ctl_get_cmd_entry(ctsio, NULL);
12227 filtered_pattern = entry->pattern & pattern;
12230 * If the user requested specific flags in the pattern (e.g.
12231 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12234 * If the user did not specify any flags, it doesn't matter whether
12235 * or not the command supports the flags.
12237 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12238 (pattern & ~CTL_LUN_PAT_MASK))
12239 return (CTL_LUN_PAT_NONE);
12242 * If the user asked for a range check, see if the requested LBA
12243 * range overlaps with this command's LBA range.
12245 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12251 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12253 return (CTL_LUN_PAT_NONE);
12255 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12256 desc->lba_range.len, FALSE);
12258 * A "pass" means that the LBA ranges don't overlap, so
12259 * this doesn't match the user's range criteria.
12261 if (action == CTL_ACTION_PASS)
12262 return (CTL_LUN_PAT_NONE);
12265 return (filtered_pattern);
12269 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12271 struct ctl_error_desc *desc, *desc2;
12273 mtx_assert(&lun->lun_lock, MA_OWNED);
12275 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12276 ctl_lun_error_pattern pattern;
12278 * Check to see whether this particular command matches
12279 * the pattern in the descriptor.
12281 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12282 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12285 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12286 case CTL_LUN_INJ_ABORTED:
12287 ctl_set_aborted(&io->scsiio);
12289 case CTL_LUN_INJ_MEDIUM_ERR:
12290 ctl_set_medium_error(&io->scsiio,
12291 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) !=
12292 CTL_FLAG_DATA_OUT);
12294 case CTL_LUN_INJ_UA:
12295 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12297 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12299 case CTL_LUN_INJ_CUSTOM:
12301 * We're assuming the user knows what he is doing.
12302 * Just copy the sense information without doing
12305 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12306 MIN(sizeof(desc->custom_sense),
12307 sizeof(io->scsiio.sense_data)));
12308 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12309 io->scsiio.sense_len = SSD_FULL_SIZE;
12310 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12312 case CTL_LUN_INJ_NONE:
12315 * If this is an error injection type we don't know
12316 * about, clear the continuous flag (if it is set)
12317 * so it will get deleted below.
12319 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12323 * By default, each error injection action is a one-shot
12325 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12328 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12334 #ifdef CTL_IO_DELAY
12336 ctl_datamove_timer_wakeup(void *arg)
12340 io = (union ctl_io *)arg;
12344 #endif /* CTL_IO_DELAY */
12347 ctl_datamove(union ctl_io *io)
12349 void (*fe_datamove)(union ctl_io *io);
12351 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
12353 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12355 /* No data transferred yet. Frontend must update this when done. */
12356 io->scsiio.kern_data_resid = io->scsiio.kern_data_len;
12359 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12364 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12365 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12367 sbuf_cat(&sb, path_str);
12368 switch (io->io_hdr.io_type) {
12370 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12371 sbuf_printf(&sb, "\n");
12372 sbuf_cat(&sb, path_str);
12373 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12374 io->scsiio.tag_num, io->scsiio.tag_type);
12377 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12378 "Tag Type: %d\n", io->taskio.task_action,
12379 io->taskio.tag_num, io->taskio.tag_type);
12382 panic("%s: Invalid CTL I/O type %d\n",
12383 __func__, io->io_hdr.io_type);
12385 sbuf_cat(&sb, path_str);
12386 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12387 (intmax_t)time_uptime - io->io_hdr.start_time);
12389 printf("%s", sbuf_data(&sb));
12391 #endif /* CTL_TIME_IO */
12393 #ifdef CTL_IO_DELAY
12394 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12395 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12397 struct ctl_lun *lun;
12401 && (lun->delay_info.datamove_delay > 0)) {
12403 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
12404 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12405 callout_reset(&io->io_hdr.delay_callout,
12406 lun->delay_info.datamove_delay * hz,
12407 ctl_datamove_timer_wakeup, io);
12408 if (lun->delay_info.datamove_type ==
12409 CTL_DELAY_TYPE_ONESHOT)
12410 lun->delay_info.datamove_delay = 0;
12417 * This command has been aborted. Set the port status, so we fail
12420 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12421 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n",
12422 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12423 io->io_hdr.nexus.targ_port,
12424 io->io_hdr.nexus.targ_lun);
12425 io->io_hdr.port_status = 31337;
12427 * Note that the backend, in this case, will get the
12428 * callback in its context. In other cases it may get
12429 * called in the frontend's interrupt thread context.
12431 io->scsiio.be_move_done(io);
12435 /* Don't confuse frontend with zero length data move. */
12436 if (io->scsiio.kern_data_len == 0) {
12437 io->scsiio.be_move_done(io);
12441 fe_datamove = CTL_PORT(io)->fe_datamove;
12446 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12448 union ctl_ha_msg msg;
12450 struct bintime cur_bt;
12453 memset(&msg, 0, sizeof(msg));
12454 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12455 msg.hdr.original_sc = io;
12456 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12457 msg.hdr.nexus = io->io_hdr.nexus;
12458 msg.hdr.status = io->io_hdr.status;
12459 msg.scsi.kern_data_resid = io->scsiio.kern_data_resid;
12460 msg.scsi.tag_num = io->scsiio.tag_num;
12461 msg.scsi.tag_type = io->scsiio.tag_type;
12462 msg.scsi.scsi_status = io->scsiio.scsi_status;
12463 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12464 io->scsiio.sense_len);
12465 msg.scsi.sense_len = io->scsiio.sense_len;
12466 msg.scsi.port_status = io->io_hdr.port_status;
12467 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12468 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12469 ctl_failover_io(io, /*have_lock*/ have_lock);
12472 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12473 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12474 msg.scsi.sense_len, M_WAITOK);
12477 getbinuptime(&cur_bt);
12478 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
12479 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
12481 io->io_hdr.num_dmas++;
12485 * The DMA to the remote side is done, now we need to tell the other side
12486 * we're done so it can continue with its data movement.
12489 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12496 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12497 printf("%s: ISC DMA write failed with error %d", __func__,
12499 ctl_set_internal_failure(&io->scsiio,
12501 /*retry_count*/ rq->ret);
12504 ctl_dt_req_free(rq);
12506 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12507 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12508 free(io->io_hdr.remote_sglist, M_CTL);
12509 io->io_hdr.remote_sglist = NULL;
12510 io->io_hdr.local_sglist = NULL;
12513 * The data is in local and remote memory, so now we need to send
12514 * status (good or back) back to the other side.
12516 ctl_send_datamove_done(io, /*have_lock*/ 0);
12520 * We've moved the data from the host/controller into local memory. Now we
12521 * need to push it over to the remote controller's memory.
12524 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12528 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12529 ctl_datamove_remote_write_cb);
12534 ctl_datamove_remote_write(union ctl_io *io)
12537 void (*fe_datamove)(union ctl_io *io);
12540 * - Get the data from the host/HBA into local memory.
12541 * - DMA memory from the local controller to the remote controller.
12542 * - Send status back to the remote controller.
12545 retval = ctl_datamove_remote_sgl_setup(io);
12549 /* Switch the pointer over so the FETD knows what to do */
12550 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12553 * Use a custom move done callback, since we need to send completion
12554 * back to the other controller, not to the backend on this side.
12556 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12558 fe_datamove = CTL_PORT(io)->fe_datamove;
12563 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12572 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12573 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12574 free(io->io_hdr.remote_sglist, M_CTL);
12575 io->io_hdr.remote_sglist = NULL;
12576 io->io_hdr.local_sglist = NULL;
12579 scsi_path_string(io, path_str, sizeof(path_str));
12580 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12581 sbuf_cat(&sb, path_str);
12582 scsi_command_string(&io->scsiio, NULL, &sb);
12583 sbuf_printf(&sb, "\n");
12584 sbuf_cat(&sb, path_str);
12585 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12586 io->scsiio.tag_num, io->scsiio.tag_type);
12587 sbuf_cat(&sb, path_str);
12588 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12589 io->io_hdr.flags, io->io_hdr.status);
12591 printk("%s", sbuf_data(&sb));
12596 * The read is done, now we need to send status (good or bad) back
12597 * to the other side.
12599 ctl_send_datamove_done(io, /*have_lock*/ 0);
12605 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12608 void (*fe_datamove)(union ctl_io *io);
12612 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12613 printf("%s: ISC DMA read failed with error %d\n", __func__,
12615 ctl_set_internal_failure(&io->scsiio,
12617 /*retry_count*/ rq->ret);
12620 ctl_dt_req_free(rq);
12622 /* Switch the pointer over so the FETD knows what to do */
12623 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12626 * Use a custom move done callback, since we need to send completion
12627 * back to the other controller, not to the backend on this side.
12629 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12631 /* XXX KDM add checks like the ones in ctl_datamove? */
12633 fe_datamove = CTL_PORT(io)->fe_datamove;
12638 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12640 struct ctl_sg_entry *local_sglist;
12641 uint32_t len_to_go;
12646 local_sglist = io->io_hdr.local_sglist;
12647 len_to_go = io->scsiio.kern_data_len;
12650 * The difficult thing here is that the size of the various
12651 * S/G segments may be different than the size from the
12652 * remote controller. That'll make it harder when DMAing
12653 * the data back to the other side.
12655 for (i = 0; len_to_go > 0; i++) {
12656 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT);
12657 local_sglist[i].addr =
12658 malloc(local_sglist[i].len, M_CTL, M_WAITOK);
12660 len_to_go -= local_sglist[i].len;
12663 * Reset the number of S/G entries accordingly. The original
12664 * number of S/G entries is available in rem_sg_entries.
12666 io->scsiio.kern_sg_entries = i;
12669 printf("%s: kern_sg_entries = %d\n", __func__,
12670 io->scsiio.kern_sg_entries);
12671 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12672 printf("%s: sg[%d] = %p, %lu\n", __func__, i,
12673 local_sglist[i].addr, local_sglist[i].len);
12680 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12681 ctl_ha_dt_cb callback)
12683 struct ctl_ha_dt_req *rq;
12684 struct ctl_sg_entry *remote_sglist, *local_sglist;
12685 uint32_t local_used, remote_used, total_used;
12688 rq = ctl_dt_req_alloc();
12691 * If we failed to allocate the request, and if the DMA didn't fail
12692 * anyway, set busy status. This is just a resource allocation
12696 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12697 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS))
12698 ctl_set_busy(&io->scsiio);
12700 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12701 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
12704 ctl_dt_req_free(rq);
12707 * The data move failed. We need to return status back
12708 * to the other controller. No point in trying to DMA
12709 * data to the remote controller.
12712 ctl_send_datamove_done(io, /*have_lock*/ 0);
12717 local_sglist = io->io_hdr.local_sglist;
12718 remote_sglist = io->io_hdr.remote_sglist;
12724 * Pull/push the data over the wire from/to the other controller.
12725 * This takes into account the possibility that the local and
12726 * remote sglists may not be identical in terms of the size of
12727 * the elements and the number of elements.
12729 * One fundamental assumption here is that the length allocated for
12730 * both the local and remote sglists is identical. Otherwise, we've
12731 * essentially got a coding error of some sort.
12733 isc_ret = CTL_HA_STATUS_SUCCESS;
12734 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12738 rq->command = command;
12742 * Both pointers should be aligned. But it is possible
12743 * that the allocation length is not. They should both
12744 * also have enough slack left over at the end, though,
12745 * to round up to the next 8 byte boundary.
12747 cur_len = MIN(local_sglist[i].len - local_used,
12748 remote_sglist[j].len - remote_used);
12749 rq->size = cur_len;
12751 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12752 tmp_ptr += local_used;
12755 /* Use physical addresses when talking to ISC hardware */
12756 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12757 /* XXX KDM use busdma */
12758 rq->local = vtophys(tmp_ptr);
12760 rq->local = tmp_ptr;
12762 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
12763 ("HA does not support BUS_ADDR"));
12764 rq->local = tmp_ptr;
12767 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12768 tmp_ptr += remote_used;
12769 rq->remote = tmp_ptr;
12771 rq->callback = NULL;
12773 local_used += cur_len;
12774 if (local_used >= local_sglist[i].len) {
12779 remote_used += cur_len;
12780 if (remote_used >= remote_sglist[j].len) {
12784 total_used += cur_len;
12786 if (total_used >= io->scsiio.kern_data_len)
12787 rq->callback = callback;
12790 printf("%s: %s: local %p remote %p size %d\n", __func__,
12791 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
12792 rq->local, rq->remote, rq->size);
12795 isc_ret = ctl_dt_single(rq);
12796 if (isc_ret > CTL_HA_STATUS_SUCCESS)
12799 if (isc_ret != CTL_HA_STATUS_WAIT) {
12808 ctl_datamove_remote_read(union ctl_io *io)
12814 * This will send an error to the other controller in the case of a
12817 retval = ctl_datamove_remote_sgl_setup(io);
12821 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12822 ctl_datamove_remote_read_cb);
12825 * Make sure we free memory if there was an error.. The
12826 * ctl_datamove_remote_xfer() function will send the
12827 * datamove done message, or call the callback with an
12828 * error if there is a problem.
12830 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12831 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12832 free(io->io_hdr.remote_sglist, M_CTL);
12833 io->io_hdr.remote_sglist = NULL;
12834 io->io_hdr.local_sglist = NULL;
12839 * Process a datamove request from the other controller. This is used for
12840 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
12841 * first. Once that is complete, the data gets DMAed into the remote
12842 * controller's memory. For reads, we DMA from the remote controller's
12843 * memory into our memory first, and then move it out to the FETD.
12846 ctl_datamove_remote(union ctl_io *io)
12849 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
12851 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12852 ctl_failover_io(io, /*have_lock*/ 0);
12857 * Note that we look for an aborted I/O here, but don't do some of
12858 * the other checks that ctl_datamove() normally does.
12859 * We don't need to run the datamove delay code, since that should
12860 * have been done if need be on the other controller.
12862 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12863 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__,
12864 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12865 io->io_hdr.nexus.targ_port,
12866 io->io_hdr.nexus.targ_lun);
12867 io->io_hdr.port_status = 31338;
12868 ctl_send_datamove_done(io, /*have_lock*/ 0);
12872 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
12873 ctl_datamove_remote_write(io);
12874 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
12875 ctl_datamove_remote_read(io);
12877 io->io_hdr.port_status = 31339;
12878 ctl_send_datamove_done(io, /*have_lock*/ 0);
12883 ctl_process_done(union ctl_io *io)
12885 struct ctl_softc *softc = CTL_SOFTC(io);
12886 struct ctl_port *port = CTL_PORT(io);
12887 struct ctl_lun *lun = CTL_LUN(io);
12888 void (*fe_done)(union ctl_io *io);
12889 union ctl_ha_msg msg;
12891 CTL_DEBUG_PRINT(("ctl_process_done\n"));
12892 fe_done = port->fe_done;
12895 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12900 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12901 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12903 sbuf_cat(&sb, path_str);
12904 switch (io->io_hdr.io_type) {
12906 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12907 sbuf_printf(&sb, "\n");
12908 sbuf_cat(&sb, path_str);
12909 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12910 io->scsiio.tag_num, io->scsiio.tag_type);
12913 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12914 "Tag Type: %d\n", io->taskio.task_action,
12915 io->taskio.tag_num, io->taskio.tag_type);
12918 panic("%s: Invalid CTL I/O type %d\n",
12919 __func__, io->io_hdr.io_type);
12921 sbuf_cat(&sb, path_str);
12922 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
12923 (intmax_t)time_uptime - io->io_hdr.start_time);
12925 printf("%s", sbuf_data(&sb));
12927 #endif /* CTL_TIME_IO */
12929 switch (io->io_hdr.io_type) {
12933 if (ctl_debug & CTL_DEBUG_INFO)
12934 ctl_io_error_print(io, NULL);
12938 panic("%s: Invalid CTL I/O type %d\n",
12939 __func__, io->io_hdr.io_type);
12943 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
12944 io->io_hdr.nexus.targ_mapped_lun));
12948 mtx_lock(&lun->lun_lock);
12951 * Check to see if we have any informational exception and status
12952 * of this command can be modified to report it in form of either
12953 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field.
12955 if (lun->ie_reported == 0 && lun->ie_asc != 0 &&
12956 io->io_hdr.status == CTL_SUCCESS &&
12957 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) {
12958 uint8_t mrie = lun->MODE_IE.mrie;
12959 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) ||
12960 (lun->MODE_VER.byte3 & SMS_VER_PER));
12961 if (((mrie == SIEP_MRIE_REC_COND && per) ||
12962 mrie == SIEP_MRIE_REC_UNCOND ||
12963 mrie == SIEP_MRIE_NO_SENSE) &&
12964 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags &
12965 CTL_CMD_FLAG_NO_SENSE) == 0) {
12966 ctl_set_sense(&io->scsiio,
12967 /*current_error*/ 1,
12968 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ?
12969 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR,
12970 /*asc*/ lun->ie_asc,
12971 /*ascq*/ lun->ie_ascq,
12973 lun->ie_reported = 1;
12975 } else if (lun->ie_reported < 0)
12976 lun->ie_reported = 0;
12979 * Check to see if we have any errors to inject here. We only
12980 * inject errors for commands that don't already have errors set.
12982 if (!STAILQ_EMPTY(&lun->error_list) &&
12983 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
12984 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
12985 ctl_inject_error(lun, io);
12988 * XXX KDM how do we treat commands that aren't completed
12991 * XXX KDM should we also track I/O latency?
12993 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
12994 io->io_hdr.io_type == CTL_IO_SCSI) {
13000 bintime_sub(&bt, &io->io_hdr.start_bt);
13002 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13004 type = CTL_STATS_READ;
13005 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13007 type = CTL_STATS_WRITE;
13009 type = CTL_STATS_NO_IO;
13011 #ifdef CTL_LEGACY_STATS
13012 uint32_t targ_port = port->targ_port;
13013 lun->legacy_stats.ports[targ_port].bytes[type] +=
13014 io->scsiio.kern_total_len;
13015 lun->legacy_stats.ports[targ_port].operations[type] ++;
13016 lun->legacy_stats.ports[targ_port].num_dmas[type] +=
13017 io->io_hdr.num_dmas;
13019 bintime_add(&lun->legacy_stats.ports[targ_port].dma_time[type],
13020 &io->io_hdr.dma_bt);
13021 bintime_add(&lun->legacy_stats.ports[targ_port].time[type],
13024 #endif /* CTL_LEGACY_STATS */
13026 lun->stats.bytes[type] += io->scsiio.kern_total_len;
13027 lun->stats.operations[type] ++;
13028 lun->stats.dmas[type] += io->io_hdr.num_dmas;
13030 bintime_add(&lun->stats.dma_time[type], &io->io_hdr.dma_bt);
13031 bintime_add(&lun->stats.time[type], &bt);
13034 mtx_lock(&port->port_lock);
13035 port->stats.bytes[type] += io->scsiio.kern_total_len;
13036 port->stats.operations[type] ++;
13037 port->stats.dmas[type] += io->io_hdr.num_dmas;
13039 bintime_add(&port->stats.dma_time[type], &io->io_hdr.dma_bt);
13040 bintime_add(&port->stats.time[type], &bt);
13042 mtx_unlock(&port->port_lock);
13046 * Remove this from the OOA queue.
13048 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13050 if (TAILQ_EMPTY(&lun->ooa_queue))
13051 lun->last_busy = getsbinuptime();
13055 * Run through the blocked queue on this LUN and see if anything
13056 * has become unblocked, now that this transaction is done.
13058 ctl_check_blocked(lun);
13061 * If the LUN has been invalidated, free it if there is nothing
13062 * left on its OOA queue.
13064 if ((lun->flags & CTL_LUN_INVALID)
13065 && TAILQ_EMPTY(&lun->ooa_queue)) {
13066 mtx_unlock(&lun->lun_lock);
13069 mtx_unlock(&lun->lun_lock);
13074 * If this command has been aborted, make sure we set the status
13075 * properly. The FETD is responsible for freeing the I/O and doing
13076 * whatever it needs to do to clean up its state.
13078 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13079 ctl_set_task_aborted(&io->scsiio);
13082 * If enabled, print command error status.
13084 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
13085 (ctl_debug & CTL_DEBUG_INFO) != 0)
13086 ctl_io_error_print(io, NULL);
13089 * Tell the FETD or the other shelf controller we're done with this
13090 * command. Note that only SCSI commands get to this point. Task
13091 * management commands are completed above.
13093 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
13094 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) {
13095 memset(&msg, 0, sizeof(msg));
13096 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13097 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
13098 msg.hdr.nexus = io->io_hdr.nexus;
13099 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13100 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data),
13108 * Front end should call this if it doesn't do autosense. When the request
13109 * sense comes back in from the initiator, we'll dequeue this and send it.
13112 ctl_queue_sense(union ctl_io *io)
13114 struct ctl_softc *softc = CTL_SOFTC(io);
13115 struct ctl_port *port = CTL_PORT(io);
13116 struct ctl_lun *lun;
13117 struct scsi_sense_data *ps;
13118 uint32_t initidx, p, targ_lun;
13120 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13122 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13125 * LUN lookup will likely move to the ctl_work_thread() once we
13126 * have our new queueing infrastructure (that doesn't put things on
13127 * a per-LUN queue initially). That is so that we can handle
13128 * things like an INQUIRY to a LUN that we don't have enabled. We
13129 * can't deal with that right now.
13130 * If we don't have a LUN for this, just toss the sense information.
13132 mtx_lock(&softc->ctl_lock);
13133 if (targ_lun >= CTL_MAX_LUNS ||
13134 (lun = softc->ctl_luns[targ_lun]) == NULL) {
13135 mtx_unlock(&softc->ctl_lock);
13138 mtx_lock(&lun->lun_lock);
13139 mtx_unlock(&softc->ctl_lock);
13141 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13142 p = initidx / CTL_MAX_INIT_PER_PORT;
13143 if (lun->pending_sense[p] == NULL) {
13144 lun->pending_sense[p] = malloc(sizeof(*ps) * CTL_MAX_INIT_PER_PORT,
13145 M_CTL, M_NOWAIT | M_ZERO);
13147 if ((ps = lun->pending_sense[p]) != NULL) {
13148 ps += initidx % CTL_MAX_INIT_PER_PORT;
13149 memset(ps, 0, sizeof(*ps));
13150 memcpy(ps, &io->scsiio.sense_data, io->scsiio.sense_len);
13152 mtx_unlock(&lun->lun_lock);
13156 return (CTL_RETVAL_COMPLETE);
13160 * Primary command inlet from frontend ports. All SCSI and task I/O
13161 * requests must go through this function.
13164 ctl_queue(union ctl_io *io)
13166 struct ctl_port *port = CTL_PORT(io);
13168 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13171 io->io_hdr.start_time = time_uptime;
13172 getbinuptime(&io->io_hdr.start_bt);
13173 #endif /* CTL_TIME_IO */
13175 /* Map FE-specific LUN ID into global one. */
13176 io->io_hdr.nexus.targ_mapped_lun =
13177 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13179 switch (io->io_hdr.io_type) {
13182 if (ctl_debug & CTL_DEBUG_CDB)
13184 ctl_enqueue_incoming(io);
13187 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13191 return (CTL_RETVAL_COMPLETE);
13194 #ifdef CTL_IO_DELAY
13196 ctl_done_timer_wakeup(void *arg)
13200 io = (union ctl_io *)arg;
13203 #endif /* CTL_IO_DELAY */
13206 ctl_serseq_done(union ctl_io *io)
13208 struct ctl_lun *lun = CTL_LUN(io);;
13210 if (lun->be_lun == NULL ||
13211 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF)
13213 mtx_lock(&lun->lun_lock);
13214 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE;
13215 ctl_check_blocked(lun);
13216 mtx_unlock(&lun->lun_lock);
13220 ctl_done(union ctl_io *io)
13224 * Enable this to catch duplicate completion issues.
13227 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13228 printf("%s: type %d msg %d cdb %x iptl: "
13229 "%u:%u:%u tag 0x%04x "
13230 "flag %#x status %x\n",
13232 io->io_hdr.io_type,
13233 io->io_hdr.msg_type,
13235 io->io_hdr.nexus.initid,
13236 io->io_hdr.nexus.targ_port,
13237 io->io_hdr.nexus.targ_lun,
13238 (io->io_hdr.io_type ==
13240 io->taskio.tag_num :
13241 io->scsiio.tag_num,
13243 io->io_hdr.status);
13245 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13249 * This is an internal copy of an I/O, and should not go through
13250 * the normal done processing logic.
13252 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13255 #ifdef CTL_IO_DELAY
13256 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13257 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13259 struct ctl_lun *lun = CTL_LUN(io);
13262 && (lun->delay_info.done_delay > 0)) {
13264 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
13265 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13266 callout_reset(&io->io_hdr.delay_callout,
13267 lun->delay_info.done_delay * hz,
13268 ctl_done_timer_wakeup, io);
13269 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13270 lun->delay_info.done_delay = 0;
13274 #endif /* CTL_IO_DELAY */
13276 ctl_enqueue_done(io);
13280 ctl_work_thread(void *arg)
13282 struct ctl_thread *thr = (struct ctl_thread *)arg;
13283 struct ctl_softc *softc = thr->ctl_softc;
13287 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13289 while (!softc->shutdown) {
13291 * We handle the queues in this order:
13293 * - done queue (to free up resources, unblock other commands)
13297 * If those queues are empty, we break out of the loop and
13300 mtx_lock(&thr->queue_lock);
13301 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13303 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13304 mtx_unlock(&thr->queue_lock);
13305 ctl_handle_isc(io);
13308 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13310 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13311 /* clear any blocked commands, call fe_done */
13312 mtx_unlock(&thr->queue_lock);
13313 ctl_process_done(io);
13316 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13318 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13319 mtx_unlock(&thr->queue_lock);
13320 if (io->io_hdr.io_type == CTL_IO_TASK)
13323 ctl_scsiio_precheck(softc, &io->scsiio);
13326 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13328 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13329 mtx_unlock(&thr->queue_lock);
13330 retval = ctl_scsiio(&io->scsiio);
13331 if (retval != CTL_RETVAL_COMPLETE)
13332 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13336 /* Sleep until we have something to do. */
13337 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13339 thr->thread = NULL;
13344 ctl_lun_thread(void *arg)
13346 struct ctl_softc *softc = (struct ctl_softc *)arg;
13347 struct ctl_be_lun *be_lun;
13349 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13351 while (!softc->shutdown) {
13352 mtx_lock(&softc->ctl_lock);
13353 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13354 if (be_lun != NULL) {
13355 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13356 mtx_unlock(&softc->ctl_lock);
13357 ctl_create_lun(be_lun);
13361 /* Sleep until we have something to do. */
13362 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13363 PDROP | PRIBIO, "-", 0);
13365 softc->lun_thread = NULL;
13370 ctl_thresh_thread(void *arg)
13372 struct ctl_softc *softc = (struct ctl_softc *)arg;
13373 struct ctl_lun *lun;
13374 struct ctl_logical_block_provisioning_page *page;
13376 union ctl_ha_msg msg;
13377 uint64_t thres, val;
13380 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13382 while (!softc->shutdown) {
13383 mtx_lock(&softc->ctl_lock);
13384 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13385 if ((lun->flags & CTL_LUN_DISABLED) ||
13386 (lun->flags & CTL_LUN_NO_MEDIA) ||
13387 lun->backend->lun_attr == NULL)
13389 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
13390 softc->ha_mode == CTL_HA_MODE_XFER)
13392 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0)
13395 page = &lun->MODE_LBP;
13396 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13397 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13399 thres = scsi_4btoul(page->descr[i].count);
13400 thres <<= CTL_LBP_EXPONENT;
13401 switch (page->descr[i].resource) {
13403 attr = "blocksavail";
13406 attr = "blocksused";
13409 attr = "poolblocksavail";
13412 attr = "poolblocksused";
13417 mtx_unlock(&softc->ctl_lock); // XXX
13418 val = lun->backend->lun_attr(
13419 lun->be_lun->be_lun, attr);
13420 mtx_lock(&softc->ctl_lock);
13421 if (val == UINT64_MAX)
13423 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13424 == SLBPPD_ARMING_INC)
13425 e = (val >= thres);
13427 e = (val <= thres);
13431 mtx_lock(&lun->lun_lock);
13433 scsi_u64to8b((uint8_t *)&page->descr[i] -
13434 (uint8_t *)page, lun->ua_tpt_info);
13435 if (lun->lasttpt == 0 ||
13436 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13437 lun->lasttpt = time_uptime;
13438 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13444 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13447 mtx_unlock(&lun->lun_lock);
13449 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
13450 /* Send msg to other side. */
13451 bzero(&msg.ua, sizeof(msg.ua));
13452 msg.hdr.msg_type = CTL_MSG_UA;
13453 msg.hdr.nexus.initid = -1;
13454 msg.hdr.nexus.targ_port = -1;
13455 msg.hdr.nexus.targ_lun = lun->lun;
13456 msg.hdr.nexus.targ_mapped_lun = lun->lun;
13458 msg.ua.ua_set = (set > 0);
13459 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES;
13460 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8);
13461 mtx_unlock(&softc->ctl_lock); // XXX
13462 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13463 sizeof(msg.ua), M_WAITOK);
13464 mtx_lock(&softc->ctl_lock);
13467 mtx_sleep(&softc->thresh_thread, &softc->ctl_lock,
13468 PDROP | PRIBIO, "-", CTL_LBP_PERIOD * hz);
13470 softc->thresh_thread = NULL;
13475 ctl_enqueue_incoming(union ctl_io *io)
13477 struct ctl_softc *softc = CTL_SOFTC(io);
13478 struct ctl_thread *thr;
13481 idx = (io->io_hdr.nexus.targ_port * 127 +
13482 io->io_hdr.nexus.initid) % worker_threads;
13483 thr = &softc->threads[idx];
13484 mtx_lock(&thr->queue_lock);
13485 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13486 mtx_unlock(&thr->queue_lock);
13491 ctl_enqueue_rtr(union ctl_io *io)
13493 struct ctl_softc *softc = CTL_SOFTC(io);
13494 struct ctl_thread *thr;
13496 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13497 mtx_lock(&thr->queue_lock);
13498 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13499 mtx_unlock(&thr->queue_lock);
13504 ctl_enqueue_done(union ctl_io *io)
13506 struct ctl_softc *softc = CTL_SOFTC(io);
13507 struct ctl_thread *thr;
13509 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13510 mtx_lock(&thr->queue_lock);
13511 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13512 mtx_unlock(&thr->queue_lock);
13517 ctl_enqueue_isc(union ctl_io *io)
13519 struct ctl_softc *softc = CTL_SOFTC(io);
13520 struct ctl_thread *thr;
13522 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13523 mtx_lock(&thr->queue_lock);
13524 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13525 mtx_unlock(&thr->queue_lock);