2 * Copyright (c) 2003-2009 Silicon Graphics International Corp.
3 * Copyright (c) 2012 The FreeBSD Foundation
4 * Copyright (c) 2015 Alexander Motin <mav@FreeBSD.org>
7 * Portions of this software were developed by Edward Tomasz Napierala
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions, and the following disclaimer,
15 * without modification.
16 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17 * substantially similar to the "NO WARRANTY" disclaimer below
18 * ("Disclaimer") and any redistribution must be conditioned upon
19 * including a substantially similar Disclaimer requirement for further
20 * binary redistribution.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGES.
38 * CAM Target Layer, a SCSI device emulation subsystem.
40 * Author: Ken Merry <ken@FreeBSD.org>
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/ctype.h>
51 #include <sys/kernel.h>
52 #include <sys/types.h>
53 #include <sys/kthread.h>
55 #include <sys/fcntl.h>
57 #include <sys/module.h>
58 #include <sys/mutex.h>
59 #include <sys/condvar.h>
60 #include <sys/malloc.h>
62 #include <sys/ioccom.h>
63 #include <sys/queue.h>
66 #include <sys/endian.h>
67 #include <sys/sysctl.h>
71 #include <cam/scsi/scsi_all.h>
72 #include <cam/scsi/scsi_cd.h>
73 #include <cam/scsi/scsi_da.h>
74 #include <cam/ctl/ctl_io.h>
75 #include <cam/ctl/ctl.h>
76 #include <cam/ctl/ctl_frontend.h>
77 #include <cam/ctl/ctl_util.h>
78 #include <cam/ctl/ctl_backend.h>
79 #include <cam/ctl/ctl_ioctl.h>
80 #include <cam/ctl/ctl_ha.h>
81 #include <cam/ctl/ctl_private.h>
82 #include <cam/ctl/ctl_debug.h>
83 #include <cam/ctl/ctl_scsi_all.h>
84 #include <cam/ctl/ctl_error.h>
86 struct ctl_softc *control_softc = NULL;
89 * Template mode pages.
93 * Note that these are default values only. The actual values will be
94 * filled in when the user does a mode sense.
96 const static struct scsi_da_rw_recovery_page rw_er_page_default = {
97 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
98 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
99 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE,
100 /*read_retry_count*/0,
101 /*correction_span*/0,
102 /*head_offset_count*/0,
103 /*data_strobe_offset_cnt*/0,
104 /*byte8*/SMS_RWER_LBPERE,
105 /*write_retry_count*/0,
107 /*recovery_time_limit*/{0, 0},
110 const static struct scsi_da_rw_recovery_page rw_er_page_changeable = {
111 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
112 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
113 /*byte3*/SMS_RWER_PER,
114 /*read_retry_count*/0,
115 /*correction_span*/0,
116 /*head_offset_count*/0,
117 /*data_strobe_offset_cnt*/0,
118 /*byte8*/SMS_RWER_LBPERE,
119 /*write_retry_count*/0,
121 /*recovery_time_limit*/{0, 0},
124 const static struct scsi_format_page format_page_default = {
125 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
126 /*page_length*/sizeof(struct scsi_format_page) - 2,
127 /*tracks_per_zone*/ {0, 0},
128 /*alt_sectors_per_zone*/ {0, 0},
129 /*alt_tracks_per_zone*/ {0, 0},
130 /*alt_tracks_per_lun*/ {0, 0},
131 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
132 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
133 /*bytes_per_sector*/ {0, 0},
134 /*interleave*/ {0, 0},
135 /*track_skew*/ {0, 0},
136 /*cylinder_skew*/ {0, 0},
138 /*reserved*/ {0, 0, 0}
141 const static struct scsi_format_page format_page_changeable = {
142 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
143 /*page_length*/sizeof(struct scsi_format_page) - 2,
144 /*tracks_per_zone*/ {0, 0},
145 /*alt_sectors_per_zone*/ {0, 0},
146 /*alt_tracks_per_zone*/ {0, 0},
147 /*alt_tracks_per_lun*/ {0, 0},
148 /*sectors_per_track*/ {0, 0},
149 /*bytes_per_sector*/ {0, 0},
150 /*interleave*/ {0, 0},
151 /*track_skew*/ {0, 0},
152 /*cylinder_skew*/ {0, 0},
154 /*reserved*/ {0, 0, 0}
157 const static struct scsi_rigid_disk_page rigid_disk_page_default = {
158 /*page_code*/SMS_RIGID_DISK_PAGE,
159 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
160 /*cylinders*/ {0, 0, 0},
161 /*heads*/ CTL_DEFAULT_HEADS,
162 /*start_write_precomp*/ {0, 0, 0},
163 /*start_reduced_current*/ {0, 0, 0},
164 /*step_rate*/ {0, 0},
165 /*landing_zone_cylinder*/ {0, 0, 0},
166 /*rpl*/ SRDP_RPL_DISABLED,
167 /*rotational_offset*/ 0,
169 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
170 CTL_DEFAULT_ROTATION_RATE & 0xff},
174 const static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
175 /*page_code*/SMS_RIGID_DISK_PAGE,
176 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
177 /*cylinders*/ {0, 0, 0},
179 /*start_write_precomp*/ {0, 0, 0},
180 /*start_reduced_current*/ {0, 0, 0},
181 /*step_rate*/ {0, 0},
182 /*landing_zone_cylinder*/ {0, 0, 0},
184 /*rotational_offset*/ 0,
186 /*rotation_rate*/ {0, 0},
190 const static struct scsi_da_verify_recovery_page verify_er_page_default = {
191 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE,
192 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2,
194 /*read_retry_count*/0,
195 /*reserved*/{ 0, 0, 0, 0, 0, 0 },
196 /*recovery_time_limit*/{0, 0},
199 const static struct scsi_da_verify_recovery_page verify_er_page_changeable = {
200 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE,
201 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2,
202 /*byte3*/SMS_VER_PER,
203 /*read_retry_count*/0,
204 /*reserved*/{ 0, 0, 0, 0, 0, 0 },
205 /*recovery_time_limit*/{0, 0},
208 const static struct scsi_caching_page caching_page_default = {
209 /*page_code*/SMS_CACHING_PAGE,
210 /*page_length*/sizeof(struct scsi_caching_page) - 2,
211 /*flags1*/ SCP_DISC | SCP_WCE,
213 /*disable_pf_transfer_len*/ {0xff, 0xff},
214 /*min_prefetch*/ {0, 0},
215 /*max_prefetch*/ {0xff, 0xff},
216 /*max_pf_ceiling*/ {0xff, 0xff},
218 /*cache_segments*/ 0,
219 /*cache_seg_size*/ {0, 0},
221 /*non_cache_seg_size*/ {0, 0, 0}
224 const static struct scsi_caching_page caching_page_changeable = {
225 /*page_code*/SMS_CACHING_PAGE,
226 /*page_length*/sizeof(struct scsi_caching_page) - 2,
227 /*flags1*/ SCP_WCE | SCP_RCD,
229 /*disable_pf_transfer_len*/ {0, 0},
230 /*min_prefetch*/ {0, 0},
231 /*max_prefetch*/ {0, 0},
232 /*max_pf_ceiling*/ {0, 0},
234 /*cache_segments*/ 0,
235 /*cache_seg_size*/ {0, 0},
237 /*non_cache_seg_size*/ {0, 0, 0}
240 const static struct scsi_control_page control_page_default = {
241 /*page_code*/SMS_CONTROL_MODE_PAGE,
242 /*page_length*/sizeof(struct scsi_control_page) - 2,
244 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED,
247 /*aen_holdoff_period*/{0, 0},
248 /*busy_timeout_period*/{0, 0},
249 /*extended_selftest_completion_time*/{0, 0}
252 const static struct scsi_control_page control_page_changeable = {
253 /*page_code*/SMS_CONTROL_MODE_PAGE,
254 /*page_length*/sizeof(struct scsi_control_page) - 2,
256 /*queue_flags*/SCP_QUEUE_ALG_MASK | SCP_NUAR,
257 /*eca_and_aen*/SCP_SWP,
259 /*aen_holdoff_period*/{0, 0},
260 /*busy_timeout_period*/{0, 0},
261 /*extended_selftest_completion_time*/{0, 0}
264 #define CTL_CEM_LEN (sizeof(struct scsi_control_ext_page) - 4)
266 const static struct scsi_control_ext_page control_ext_page_default = {
267 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
268 /*subpage_code*/0x01,
269 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
275 const static struct scsi_control_ext_page control_ext_page_changeable = {
276 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
277 /*subpage_code*/0x01,
278 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
284 const static struct scsi_info_exceptions_page ie_page_default = {
285 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
286 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
287 /*info_flags*/SIEP_FLAGS_EWASC,
288 /*mrie*/SIEP_MRIE_NO,
289 /*interval_timer*/{0, 0, 0, 0},
290 /*report_count*/{0, 0, 0, 1}
293 const static struct scsi_info_exceptions_page ie_page_changeable = {
294 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
295 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
296 /*info_flags*/SIEP_FLAGS_EWASC | SIEP_FLAGS_DEXCPT | SIEP_FLAGS_TEST |
299 /*interval_timer*/{0xff, 0xff, 0xff, 0xff},
300 /*report_count*/{0xff, 0xff, 0xff, 0xff}
303 #define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4)
305 const static struct ctl_logical_block_provisioning_page lbp_page_default = {{
306 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
307 /*subpage_code*/0x02,
308 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
310 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
315 /*count*/{0, 0, 0, 0}},
319 /*count*/{0, 0, 0, 0}},
323 /*count*/{0, 0, 0, 0}},
327 /*count*/{0, 0, 0, 0}}
331 const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{
332 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
333 /*subpage_code*/0x02,
334 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
336 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
341 /*count*/{0, 0, 0, 0}},
345 /*count*/{0, 0, 0, 0}},
349 /*count*/{0, 0, 0, 0}},
353 /*count*/{0, 0, 0, 0}}
357 const static struct scsi_cddvd_capabilities_page cddvd_page_default = {
358 /*page_code*/SMS_CDDVD_CAPS_PAGE,
359 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2,
367 /*nvol_levels*/{0, 0},
368 /*buffer_size*/{8, 0},
373 /*copy_management*/0,
375 /*rotation_control*/0,
376 /*cur_write_speed*/0,
377 /*num_speed_descr*/0,
380 const static struct scsi_cddvd_capabilities_page cddvd_page_changeable = {
381 /*page_code*/SMS_CDDVD_CAPS_PAGE,
382 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2,
390 /*nvol_levels*/{0, 0},
391 /*buffer_size*/{0, 0},
396 /*copy_management*/0,
398 /*rotation_control*/0,
399 /*cur_write_speed*/0,
400 /*num_speed_descr*/0,
403 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
404 static int worker_threads = -1;
405 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
406 &worker_threads, 1, "Number of worker threads");
407 static int ctl_debug = CTL_DEBUG_NONE;
408 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN,
409 &ctl_debug, 0, "Enabled debug flags");
412 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
413 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
414 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0),
415 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
417 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 10
419 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
421 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
422 static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest);
423 static int ctl_init(void);
424 void ctl_shutdown(void);
425 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
426 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
427 static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
428 static void ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
429 struct ctl_ooa *ooa_hdr,
430 struct ctl_ooa_entry *kern_entries);
431 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
433 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
434 struct ctl_be_lun *be_lun);
435 static int ctl_free_lun(struct ctl_lun *lun);
436 static void ctl_create_lun(struct ctl_be_lun *be_lun);
437 static struct ctl_port * ctl_io_port(struct ctl_io_hdr *io_hdr);
439 static int ctl_do_mode_select(union ctl_io *io);
440 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
441 uint64_t res_key, uint64_t sa_res_key,
442 uint8_t type, uint32_t residx,
443 struct ctl_scsiio *ctsio,
444 struct scsi_per_res_out *cdb,
445 struct scsi_per_res_out_parms* param);
446 static void ctl_pro_preempt_other(struct ctl_lun *lun,
447 union ctl_ha_msg *msg);
448 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
449 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
450 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
451 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
452 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
453 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
454 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
456 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
458 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
459 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
460 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
461 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
462 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
463 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2,
465 static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2);
466 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
467 union ctl_io *pending_io, union ctl_io *ooa_io);
468 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
469 union ctl_io *starting_io);
470 static int ctl_check_blocked(struct ctl_lun *lun);
471 static int ctl_scsiio_lun_check(struct ctl_lun *lun,
472 const struct ctl_cmd_entry *entry,
473 struct ctl_scsiio *ctsio);
474 static void ctl_failover_lun(union ctl_io *io);
475 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
476 struct ctl_scsiio *ctsio);
477 static int ctl_scsiio(struct ctl_scsiio *ctsio);
479 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
480 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
481 ctl_ua_type ua_type);
482 static int ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io,
483 ctl_ua_type ua_type);
484 static int ctl_lun_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
485 static int ctl_abort_task(union ctl_io *io);
486 static int ctl_abort_task_set(union ctl_io *io);
487 static int ctl_query_task(union ctl_io *io, int task_set);
488 static int ctl_i_t_nexus_reset(union ctl_io *io);
489 static int ctl_query_async_event(union ctl_io *io);
490 static void ctl_run_task(union ctl_io *io);
492 static void ctl_datamove_timer_wakeup(void *arg);
493 static void ctl_done_timer_wakeup(void *arg);
494 #endif /* CTL_IO_DELAY */
496 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
497 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
498 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
499 static void ctl_datamove_remote_write(union ctl_io *io);
500 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
501 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
502 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
503 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
504 ctl_ha_dt_cb callback);
505 static void ctl_datamove_remote_read(union ctl_io *io);
506 static void ctl_datamove_remote(union ctl_io *io);
507 static void ctl_process_done(union ctl_io *io);
508 static void ctl_lun_thread(void *arg);
509 static void ctl_thresh_thread(void *arg);
510 static void ctl_work_thread(void *arg);
511 static void ctl_enqueue_incoming(union ctl_io *io);
512 static void ctl_enqueue_rtr(union ctl_io *io);
513 static void ctl_enqueue_done(union ctl_io *io);
514 static void ctl_enqueue_isc(union ctl_io *io);
515 static const struct ctl_cmd_entry *
516 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
517 static const struct ctl_cmd_entry *
518 ctl_validate_command(struct ctl_scsiio *ctsio);
519 static int ctl_cmd_applicable(uint8_t lun_type,
520 const struct ctl_cmd_entry *entry);
522 static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx);
523 static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx);
524 static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx);
525 static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key);
528 * Load the serialization table. This isn't very pretty, but is probably
529 * the easiest way to do it.
531 #include "ctl_ser_table.c"
534 * We only need to define open, close and ioctl routines for this driver.
536 static struct cdevsw ctl_cdevsw = {
537 .d_version = D_VERSION,
540 .d_close = ctl_close,
541 .d_ioctl = ctl_ioctl,
546 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
548 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
550 static moduledata_t ctl_moduledata = {
552 ctl_module_event_handler,
556 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
557 MODULE_VERSION(ctl, 1);
559 static struct ctl_frontend ha_frontend =
565 ctl_ha_datamove(union ctl_io *io)
568 struct ctl_sg_entry *sgl;
569 union ctl_ha_msg msg;
570 uint32_t sg_entries_sent;
571 int do_sg_copy, i, j;
573 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
574 memset(&msg.dt, 0, sizeof(msg.dt));
575 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
576 msg.hdr.original_sc = io->io_hdr.original_sc;
577 msg.hdr.serializing_sc = io;
578 msg.hdr.nexus = io->io_hdr.nexus;
579 msg.hdr.status = io->io_hdr.status;
580 msg.dt.flags = io->io_hdr.flags;
583 * We convert everything into a S/G list here. We can't
584 * pass by reference, only by value between controllers.
585 * So we can't pass a pointer to the S/G list, only as many
586 * S/G entries as we can fit in here. If it's possible for
587 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
588 * then we need to break this up into multiple transfers.
590 if (io->scsiio.kern_sg_entries == 0) {
591 msg.dt.kern_sg_entries = 1;
593 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
594 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
596 /* XXX KDM use busdma here! */
597 msg.dt.sg_list[0].addr =
598 (void *)vtophys(io->scsiio.kern_data_ptr);
601 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
602 ("HA does not support BUS_ADDR"));
603 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
605 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
608 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
612 msg.dt.kern_data_len = io->scsiio.kern_data_len;
613 msg.dt.kern_total_len = io->scsiio.kern_total_len;
614 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
615 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
616 msg.dt.sg_sequence = 0;
619 * Loop until we've sent all of the S/G entries. On the
620 * other end, we'll recompose these S/G entries into one
621 * contiguous list before processing.
623 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries;
624 msg.dt.sg_sequence++) {
625 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) /
626 sizeof(msg.dt.sg_list[0])),
627 msg.dt.kern_sg_entries - sg_entries_sent);
628 if (do_sg_copy != 0) {
629 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
630 for (i = sg_entries_sent, j = 0;
631 i < msg.dt.cur_sg_entries; i++, j++) {
633 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
634 msg.dt.sg_list[j].addr = sgl[i].addr;
636 /* XXX KDM use busdma here! */
637 msg.dt.sg_list[j].addr =
638 (void *)vtophys(sgl[i].addr);
641 KASSERT((io->io_hdr.flags &
642 CTL_FLAG_BUS_ADDR) == 0,
643 ("HA does not support BUS_ADDR"));
644 msg.dt.sg_list[j].addr = sgl[i].addr;
646 msg.dt.sg_list[j].len = sgl[i].len;
650 sg_entries_sent += msg.dt.cur_sg_entries;
651 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries);
652 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
653 sizeof(msg.dt) - sizeof(msg.dt.sg_list) +
654 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries,
655 M_WAITOK) > CTL_HA_STATUS_SUCCESS) {
656 io->io_hdr.port_status = 31341;
657 io->scsiio.be_move_done(io);
660 msg.dt.sent_sg_entries = sg_entries_sent;
664 * Officially handover the request from us to peer.
665 * If failover has just happened, then we must return error.
666 * If failover happen just after, then it is not our problem.
669 mtx_lock(&lun->lun_lock);
670 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
672 mtx_unlock(&lun->lun_lock);
673 io->io_hdr.port_status = 31342;
674 io->scsiio.be_move_done(io);
677 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
678 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
680 mtx_unlock(&lun->lun_lock);
684 ctl_ha_done(union ctl_io *io)
686 union ctl_ha_msg msg;
688 if (io->io_hdr.io_type == CTL_IO_SCSI) {
689 memset(&msg, 0, sizeof(msg));
690 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
691 msg.hdr.original_sc = io->io_hdr.original_sc;
692 msg.hdr.nexus = io->io_hdr.nexus;
693 msg.hdr.status = io->io_hdr.status;
694 msg.scsi.scsi_status = io->scsiio.scsi_status;
695 msg.scsi.tag_num = io->scsiio.tag_num;
696 msg.scsi.tag_type = io->scsiio.tag_type;
697 msg.scsi.sense_len = io->scsiio.sense_len;
698 msg.scsi.sense_residual = io->scsiio.sense_residual;
699 msg.scsi.residual = io->scsiio.residual;
700 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
701 io->scsiio.sense_len);
702 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
703 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
704 msg.scsi.sense_len, M_WAITOK);
710 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
711 union ctl_ha_msg *msg_info)
713 struct ctl_scsiio *ctsio;
715 if (msg_info->hdr.original_sc == NULL) {
716 printf("%s: original_sc == NULL!\n", __func__);
717 /* XXX KDM now what? */
721 ctsio = &msg_info->hdr.original_sc->scsiio;
722 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
723 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
724 ctsio->io_hdr.status = msg_info->hdr.status;
725 ctsio->scsi_status = msg_info->scsi.scsi_status;
726 ctsio->sense_len = msg_info->scsi.sense_len;
727 ctsio->sense_residual = msg_info->scsi.sense_residual;
728 ctsio->residual = msg_info->scsi.residual;
729 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
730 msg_info->scsi.sense_len);
731 ctl_enqueue_isc((union ctl_io *)ctsio);
735 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
736 union ctl_ha_msg *msg_info)
738 struct ctl_scsiio *ctsio;
740 if (msg_info->hdr.serializing_sc == NULL) {
741 printf("%s: serializing_sc == NULL!\n", __func__);
742 /* XXX KDM now what? */
746 ctsio = &msg_info->hdr.serializing_sc->scsiio;
747 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
748 ctl_enqueue_isc((union ctl_io *)ctsio);
752 ctl_isc_announce_lun(struct ctl_lun *lun)
754 struct ctl_softc *softc = lun->ctl_softc;
755 union ctl_ha_msg *msg;
756 struct ctl_ha_msg_lun_pr_key pr_key;
759 if (softc->ha_link != CTL_HA_LINK_ONLINE)
761 mtx_lock(&lun->lun_lock);
762 i = sizeof(msg->lun);
764 i += lun->lun_devid->len;
765 i += sizeof(pr_key) * lun->pr_key_count;
767 mtx_unlock(&lun->lun_lock);
768 msg = malloc(i, M_CTL, M_WAITOK);
769 mtx_lock(&lun->lun_lock);
770 k = sizeof(msg->lun);
772 k += lun->lun_devid->len;
773 k += sizeof(pr_key) * lun->pr_key_count;
779 bzero(&msg->lun, sizeof(msg->lun));
780 msg->hdr.msg_type = CTL_MSG_LUN_SYNC;
781 msg->hdr.nexus.targ_lun = lun->lun;
782 msg->hdr.nexus.targ_mapped_lun = lun->lun;
783 msg->lun.flags = lun->flags;
784 msg->lun.pr_generation = lun->pr_generation;
785 msg->lun.pr_res_idx = lun->pr_res_idx;
786 msg->lun.pr_res_type = lun->pr_res_type;
787 msg->lun.pr_key_count = lun->pr_key_count;
789 if (lun->lun_devid) {
790 msg->lun.lun_devid_len = lun->lun_devid->len;
791 memcpy(&msg->lun.data[i], lun->lun_devid->data,
792 msg->lun.lun_devid_len);
793 i += msg->lun.lun_devid_len;
795 for (k = 0; k < CTL_MAX_INITIATORS; k++) {
796 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0)
799 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key));
802 mtx_unlock(&lun->lun_lock);
803 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
807 if (lun->flags & CTL_LUN_PRIMARY_SC) {
808 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
809 ctl_isc_announce_mode(lun, -1,
810 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
811 lun->mode_pages.index[i].subpage);
817 ctl_isc_announce_port(struct ctl_port *port)
819 struct ctl_softc *softc = port->ctl_softc;
820 union ctl_ha_msg *msg;
823 if (port->targ_port < softc->port_min ||
824 port->targ_port >= softc->port_max ||
825 softc->ha_link != CTL_HA_LINK_ONLINE)
827 i = sizeof(msg->port) + strlen(port->port_name) + 1;
829 i += sizeof(uint32_t) * CTL_MAX_LUNS;
830 if (port->port_devid)
831 i += port->port_devid->len;
832 if (port->target_devid)
833 i += port->target_devid->len;
834 if (port->init_devid)
835 i += port->init_devid->len;
836 msg = malloc(i, M_CTL, M_WAITOK);
837 bzero(&msg->port, sizeof(msg->port));
838 msg->hdr.msg_type = CTL_MSG_PORT_SYNC;
839 msg->hdr.nexus.targ_port = port->targ_port;
840 msg->port.port_type = port->port_type;
841 msg->port.physical_port = port->physical_port;
842 msg->port.virtual_port = port->virtual_port;
843 msg->port.status = port->status;
845 msg->port.name_len = sprintf(&msg->port.data[i],
846 "%d:%s", softc->ha_id, port->port_name) + 1;
847 i += msg->port.name_len;
849 msg->port.lun_map_len = sizeof(uint32_t) * CTL_MAX_LUNS;
850 memcpy(&msg->port.data[i], port->lun_map,
851 msg->port.lun_map_len);
852 i += msg->port.lun_map_len;
854 if (port->port_devid) {
855 msg->port.port_devid_len = port->port_devid->len;
856 memcpy(&msg->port.data[i], port->port_devid->data,
857 msg->port.port_devid_len);
858 i += msg->port.port_devid_len;
860 if (port->target_devid) {
861 msg->port.target_devid_len = port->target_devid->len;
862 memcpy(&msg->port.data[i], port->target_devid->data,
863 msg->port.target_devid_len);
864 i += msg->port.target_devid_len;
866 if (port->init_devid) {
867 msg->port.init_devid_len = port->init_devid->len;
868 memcpy(&msg->port.data[i], port->init_devid->data,
869 msg->port.init_devid_len);
870 i += msg->port.init_devid_len;
872 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
878 ctl_isc_announce_iid(struct ctl_port *port, int iid)
880 struct ctl_softc *softc = port->ctl_softc;
881 union ctl_ha_msg *msg;
884 if (port->targ_port < softc->port_min ||
885 port->targ_port >= softc->port_max ||
886 softc->ha_link != CTL_HA_LINK_ONLINE)
888 mtx_lock(&softc->ctl_lock);
889 i = sizeof(msg->iid);
891 if (port->wwpn_iid[iid].name)
892 l = strlen(port->wwpn_iid[iid].name) + 1;
894 msg = malloc(i, M_CTL, M_NOWAIT);
896 mtx_unlock(&softc->ctl_lock);
899 bzero(&msg->iid, sizeof(msg->iid));
900 msg->hdr.msg_type = CTL_MSG_IID_SYNC;
901 msg->hdr.nexus.targ_port = port->targ_port;
902 msg->hdr.nexus.initid = iid;
903 msg->iid.in_use = port->wwpn_iid[iid].in_use;
904 msg->iid.name_len = l;
905 msg->iid.wwpn = port->wwpn_iid[iid].wwpn;
906 if (port->wwpn_iid[iid].name)
907 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l);
908 mtx_unlock(&softc->ctl_lock);
909 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT);
914 ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx,
915 uint8_t page, uint8_t subpage)
917 struct ctl_softc *softc = lun->ctl_softc;
918 union ctl_ha_msg msg;
921 if (softc->ha_link != CTL_HA_LINK_ONLINE)
923 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
924 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
925 page && lun->mode_pages.index[i].subpage == subpage)
928 if (i == CTL_NUM_MODE_PAGES)
931 /* Don't try to replicate pages not present on this device. */
932 if (lun->mode_pages.index[i].page_data == NULL)
935 bzero(&msg.mode, sizeof(msg.mode));
936 msg.hdr.msg_type = CTL_MSG_MODE_SYNC;
937 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT;
938 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT;
939 msg.hdr.nexus.targ_lun = lun->lun;
940 msg.hdr.nexus.targ_mapped_lun = lun->lun;
941 msg.mode.page_code = page;
942 msg.mode.subpage = subpage;
943 msg.mode.page_len = lun->mode_pages.index[i].page_len;
944 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data,
946 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode),
951 ctl_isc_ha_link_up(struct ctl_softc *softc)
953 struct ctl_port *port;
955 union ctl_ha_msg msg;
958 /* Announce this node parameters to peer for validation. */
959 msg.login.msg_type = CTL_MSG_LOGIN;
960 msg.login.version = CTL_HA_VERSION;
961 msg.login.ha_mode = softc->ha_mode;
962 msg.login.ha_id = softc->ha_id;
963 msg.login.max_luns = CTL_MAX_LUNS;
964 msg.login.max_ports = CTL_MAX_PORTS;
965 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT;
966 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login),
969 STAILQ_FOREACH(port, &softc->port_list, links) {
970 ctl_isc_announce_port(port);
971 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
972 if (port->wwpn_iid[i].in_use)
973 ctl_isc_announce_iid(port, i);
976 STAILQ_FOREACH(lun, &softc->lun_list, links)
977 ctl_isc_announce_lun(lun);
981 ctl_isc_ha_link_down(struct ctl_softc *softc)
983 struct ctl_port *port;
988 mtx_lock(&softc->ctl_lock);
989 STAILQ_FOREACH(lun, &softc->lun_list, links) {
990 mtx_lock(&lun->lun_lock);
991 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) {
992 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
993 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
995 mtx_unlock(&lun->lun_lock);
997 mtx_unlock(&softc->ctl_lock);
998 io = ctl_alloc_io(softc->othersc_pool);
999 mtx_lock(&softc->ctl_lock);
1001 io->io_hdr.msg_type = CTL_MSG_FAILOVER;
1002 io->io_hdr.nexus.targ_mapped_lun = lun->lun;
1003 ctl_enqueue_isc(io);
1006 STAILQ_FOREACH(port, &softc->port_list, links) {
1007 if (port->targ_port >= softc->port_min &&
1008 port->targ_port < softc->port_max)
1010 port->status &= ~CTL_PORT_STATUS_ONLINE;
1011 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1012 port->wwpn_iid[i].in_use = 0;
1013 free(port->wwpn_iid[i].name, M_CTL);
1014 port->wwpn_iid[i].name = NULL;
1017 mtx_unlock(&softc->ctl_lock);
1021 ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1023 struct ctl_lun *lun;
1024 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus);
1026 mtx_lock(&softc->ctl_lock);
1027 if (msg->hdr.nexus.targ_lun < CTL_MAX_LUNS &&
1028 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) != NULL) {
1029 mtx_lock(&lun->lun_lock);
1030 mtx_unlock(&softc->ctl_lock);
1031 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES &&
1033 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8);
1034 if (msg->ua.ua_all) {
1036 ctl_est_ua_all(lun, iid, msg->ua.ua_type);
1038 ctl_clr_ua_all(lun, iid, msg->ua.ua_type);
1041 ctl_est_ua(lun, iid, msg->ua.ua_type);
1043 ctl_clr_ua(lun, iid, msg->ua.ua_type);
1045 mtx_unlock(&lun->lun_lock);
1047 mtx_unlock(&softc->ctl_lock);
1051 ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1053 struct ctl_lun *lun;
1054 struct ctl_ha_msg_lun_pr_key pr_key;
1056 ctl_lun_flags oflags;
1059 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1060 mtx_lock(&softc->ctl_lock);
1061 if ((targ_lun >= CTL_MAX_LUNS) ||
1062 ((lun = softc->ctl_luns[targ_lun]) == NULL)) {
1063 mtx_unlock(&softc->ctl_lock);
1066 mtx_lock(&lun->lun_lock);
1067 mtx_unlock(&softc->ctl_lock);
1068 if (lun->flags & CTL_LUN_DISABLED) {
1069 mtx_unlock(&lun->lun_lock);
1072 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0;
1073 if (msg->lun.lun_devid_len != i || (i > 0 &&
1074 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) {
1075 mtx_unlock(&lun->lun_lock);
1076 printf("%s: Received conflicting HA LUN %d\n",
1077 __func__, msg->hdr.nexus.targ_lun);
1080 /* Record whether peer is primary. */
1081 oflags = lun->flags;
1082 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) &&
1083 (msg->lun.flags & CTL_LUN_DISABLED) == 0)
1084 lun->flags |= CTL_LUN_PEER_SC_PRIMARY;
1086 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1087 if (oflags != lun->flags)
1088 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1090 /* If peer is primary and we are not -- use data */
1091 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
1092 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) {
1093 lun->pr_generation = msg->lun.pr_generation;
1094 lun->pr_res_idx = msg->lun.pr_res_idx;
1095 lun->pr_res_type = msg->lun.pr_res_type;
1096 lun->pr_key_count = msg->lun.pr_key_count;
1097 for (k = 0; k < CTL_MAX_INITIATORS; k++)
1098 ctl_clr_prkey(lun, k);
1099 for (k = 0; k < msg->lun.pr_key_count; k++) {
1100 memcpy(&pr_key, &msg->lun.data[i],
1102 ctl_alloc_prkey(lun, pr_key.pr_iid);
1103 ctl_set_prkey(lun, pr_key.pr_iid,
1105 i += sizeof(pr_key);
1109 mtx_unlock(&lun->lun_lock);
1110 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n",
1111 __func__, msg->hdr.nexus.targ_lun,
1112 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ?
1113 "primary" : "secondary"));
1115 /* If we are primary but peer doesn't know -- notify */
1116 if ((lun->flags & CTL_LUN_PRIMARY_SC) &&
1117 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0)
1118 ctl_isc_announce_lun(lun);
1123 ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1125 struct ctl_port *port;
1126 struct ctl_lun *lun;
1129 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1131 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__,
1132 msg->hdr.nexus.targ_port));
1134 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO);
1135 port->frontend = &ha_frontend;
1136 port->targ_port = msg->hdr.nexus.targ_port;
1137 port->fe_datamove = ctl_ha_datamove;
1138 port->fe_done = ctl_ha_done;
1139 } else if (port->frontend == &ha_frontend) {
1140 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__,
1141 msg->hdr.nexus.targ_port));
1144 printf("%s: Received conflicting HA port %d\n",
1145 __func__, msg->hdr.nexus.targ_port);
1148 port->port_type = msg->port.port_type;
1149 port->physical_port = msg->port.physical_port;
1150 port->virtual_port = msg->port.virtual_port;
1151 port->status = msg->port.status;
1153 free(port->port_name, M_CTL);
1154 port->port_name = strndup(&msg->port.data[i], msg->port.name_len,
1156 i += msg->port.name_len;
1157 if (msg->port.lun_map_len != 0) {
1158 if (port->lun_map == NULL)
1159 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS,
1161 memcpy(port->lun_map, &msg->port.data[i],
1162 sizeof(uint32_t) * CTL_MAX_LUNS);
1163 i += msg->port.lun_map_len;
1165 free(port->lun_map, M_CTL);
1166 port->lun_map = NULL;
1168 if (msg->port.port_devid_len != 0) {
1169 if (port->port_devid == NULL ||
1170 port->port_devid->len != msg->port.port_devid_len) {
1171 free(port->port_devid, M_CTL);
1172 port->port_devid = malloc(sizeof(struct ctl_devid) +
1173 msg->port.port_devid_len, M_CTL, M_WAITOK);
1175 memcpy(port->port_devid->data, &msg->port.data[i],
1176 msg->port.port_devid_len);
1177 port->port_devid->len = msg->port.port_devid_len;
1178 i += msg->port.port_devid_len;
1180 free(port->port_devid, M_CTL);
1181 port->port_devid = NULL;
1183 if (msg->port.target_devid_len != 0) {
1184 if (port->target_devid == NULL ||
1185 port->target_devid->len != msg->port.target_devid_len) {
1186 free(port->target_devid, M_CTL);
1187 port->target_devid = malloc(sizeof(struct ctl_devid) +
1188 msg->port.target_devid_len, M_CTL, M_WAITOK);
1190 memcpy(port->target_devid->data, &msg->port.data[i],
1191 msg->port.target_devid_len);
1192 port->target_devid->len = msg->port.target_devid_len;
1193 i += msg->port.target_devid_len;
1195 free(port->target_devid, M_CTL);
1196 port->target_devid = NULL;
1198 if (msg->port.init_devid_len != 0) {
1199 if (port->init_devid == NULL ||
1200 port->init_devid->len != msg->port.init_devid_len) {
1201 free(port->init_devid, M_CTL);
1202 port->init_devid = malloc(sizeof(struct ctl_devid) +
1203 msg->port.init_devid_len, M_CTL, M_WAITOK);
1205 memcpy(port->init_devid->data, &msg->port.data[i],
1206 msg->port.init_devid_len);
1207 port->init_devid->len = msg->port.init_devid_len;
1208 i += msg->port.init_devid_len;
1210 free(port->init_devid, M_CTL);
1211 port->init_devid = NULL;
1214 if (ctl_port_register(port) != 0) {
1215 printf("%s: ctl_port_register() failed with error\n",
1219 mtx_lock(&softc->ctl_lock);
1220 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1221 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
1223 mtx_lock(&lun->lun_lock);
1224 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
1225 mtx_unlock(&lun->lun_lock);
1227 mtx_unlock(&softc->ctl_lock);
1231 ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1233 struct ctl_port *port;
1236 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1238 printf("%s: Received IID for unknown port %d\n",
1239 __func__, msg->hdr.nexus.targ_port);
1242 iid = msg->hdr.nexus.initid;
1243 port->wwpn_iid[iid].in_use = msg->iid.in_use;
1244 port->wwpn_iid[iid].wwpn = msg->iid.wwpn;
1245 free(port->wwpn_iid[iid].name, M_CTL);
1246 if (msg->iid.name_len) {
1247 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0],
1248 msg->iid.name_len, M_CTL);
1250 port->wwpn_iid[iid].name = NULL;
1254 ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1257 if (msg->login.version != CTL_HA_VERSION) {
1258 printf("CTL HA peers have different versions %d != %d\n",
1259 msg->login.version, CTL_HA_VERSION);
1260 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1263 if (msg->login.ha_mode != softc->ha_mode) {
1264 printf("CTL HA peers have different ha_mode %d != %d\n",
1265 msg->login.ha_mode, softc->ha_mode);
1266 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1269 if (msg->login.ha_id == softc->ha_id) {
1270 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id);
1271 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1274 if (msg->login.max_luns != CTL_MAX_LUNS ||
1275 msg->login.max_ports != CTL_MAX_PORTS ||
1276 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) {
1277 printf("CTL HA peers have different limits\n");
1278 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1284 ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1286 struct ctl_lun *lun;
1288 uint32_t initidx, targ_lun;
1290 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1291 mtx_lock(&softc->ctl_lock);
1292 if ((targ_lun >= CTL_MAX_LUNS) ||
1293 ((lun = softc->ctl_luns[targ_lun]) == NULL)) {
1294 mtx_unlock(&softc->ctl_lock);
1297 mtx_lock(&lun->lun_lock);
1298 mtx_unlock(&softc->ctl_lock);
1299 if (lun->flags & CTL_LUN_DISABLED) {
1300 mtx_unlock(&lun->lun_lock);
1303 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
1304 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
1305 msg->mode.page_code &&
1306 lun->mode_pages.index[i].subpage == msg->mode.subpage)
1309 if (i == CTL_NUM_MODE_PAGES) {
1310 mtx_unlock(&lun->lun_lock);
1313 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data,
1314 lun->mode_pages.index[i].page_len);
1315 initidx = ctl_get_initindex(&msg->hdr.nexus);
1317 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
1318 mtx_unlock(&lun->lun_lock);
1322 * ISC (Inter Shelf Communication) event handler. Events from the HA
1323 * subsystem come in here.
1326 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
1328 struct ctl_softc *softc = control_softc;
1330 struct ctl_prio *presio;
1331 ctl_ha_status isc_status;
1333 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event));
1334 if (event == CTL_HA_EVT_MSG_RECV) {
1335 union ctl_ha_msg *msg, msgbuf;
1337 if (param > sizeof(msgbuf))
1338 msg = malloc(param, M_CTL, M_WAITOK);
1341 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param,
1343 if (isc_status != CTL_HA_STATUS_SUCCESS) {
1344 printf("%s: Error receiving message: %d\n",
1345 __func__, isc_status);
1351 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type));
1352 switch (msg->hdr.msg_type) {
1353 case CTL_MSG_SERIALIZE:
1354 io = ctl_alloc_io(softc->othersc_pool);
1356 // populate ctsio from msg
1357 io->io_hdr.io_type = CTL_IO_SCSI;
1358 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
1359 io->io_hdr.original_sc = msg->hdr.original_sc;
1360 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
1363 * If we're in serialization-only mode, we don't
1364 * want to go through full done processing. Thus
1367 * XXX KDM add another flag that is more specific.
1369 if (softc->ha_mode != CTL_HA_MODE_XFER)
1370 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
1371 io->io_hdr.nexus = msg->hdr.nexus;
1373 printf("port %u, iid %u, lun %u\n",
1374 io->io_hdr.nexus.targ_port,
1375 io->io_hdr.nexus.initid,
1376 io->io_hdr.nexus.targ_lun);
1378 io->scsiio.tag_num = msg->scsi.tag_num;
1379 io->scsiio.tag_type = msg->scsi.tag_type;
1381 io->io_hdr.start_time = time_uptime;
1382 getbinuptime(&io->io_hdr.start_bt);
1383 #endif /* CTL_TIME_IO */
1384 io->scsiio.cdb_len = msg->scsi.cdb_len;
1385 memcpy(io->scsiio.cdb, msg->scsi.cdb,
1387 if (softc->ha_mode == CTL_HA_MODE_XFER) {
1388 const struct ctl_cmd_entry *entry;
1390 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
1391 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
1393 entry->flags & CTL_FLAG_DATA_MASK;
1395 ctl_enqueue_isc(io);
1398 /* Performed on the Originating SC, XFER mode only */
1399 case CTL_MSG_DATAMOVE: {
1400 struct ctl_sg_entry *sgl;
1403 io = msg->hdr.original_sc;
1405 printf("%s: original_sc == NULL!\n", __func__);
1406 /* XXX KDM do something here */
1409 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
1410 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1412 * Keep track of this, we need to send it back over
1413 * when the datamove is complete.
1415 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1416 if (msg->hdr.status == CTL_SUCCESS)
1417 io->io_hdr.status = msg->hdr.status;
1419 if (msg->dt.sg_sequence == 0) {
1421 getbinuptime(&io->io_hdr.dma_start_bt);
1423 i = msg->dt.kern_sg_entries +
1424 msg->dt.kern_data_len /
1425 CTL_HA_DATAMOVE_SEGMENT + 1;
1426 sgl = malloc(sizeof(*sgl) * i, M_CTL,
1428 io->io_hdr.remote_sglist = sgl;
1429 io->io_hdr.local_sglist =
1430 &sgl[msg->dt.kern_sg_entries];
1432 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
1434 io->scsiio.kern_sg_entries =
1435 msg->dt.kern_sg_entries;
1436 io->scsiio.rem_sg_entries =
1437 msg->dt.kern_sg_entries;
1438 io->scsiio.kern_data_len =
1439 msg->dt.kern_data_len;
1440 io->scsiio.kern_total_len =
1441 msg->dt.kern_total_len;
1442 io->scsiio.kern_data_resid =
1443 msg->dt.kern_data_resid;
1444 io->scsiio.kern_rel_offset =
1445 msg->dt.kern_rel_offset;
1446 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR;
1447 io->io_hdr.flags |= msg->dt.flags &
1450 sgl = (struct ctl_sg_entry *)
1451 io->scsiio.kern_data_ptr;
1453 for (i = msg->dt.sent_sg_entries, j = 0;
1454 i < (msg->dt.sent_sg_entries +
1455 msg->dt.cur_sg_entries); i++, j++) {
1456 sgl[i].addr = msg->dt.sg_list[j].addr;
1457 sgl[i].len = msg->dt.sg_list[j].len;
1460 printf("%s: DATAMOVE: %p,%lu j=%d, i=%d\n",
1461 __func__, sgl[i].addr, sgl[i].len, j, i);
1466 * If this is the last piece of the I/O, we've got
1467 * the full S/G list. Queue processing in the thread.
1468 * Otherwise wait for the next piece.
1470 if (msg->dt.sg_last != 0)
1471 ctl_enqueue_isc(io);
1474 /* Performed on the Serializing (primary) SC, XFER mode only */
1475 case CTL_MSG_DATAMOVE_DONE: {
1476 if (msg->hdr.serializing_sc == NULL) {
1477 printf("%s: serializing_sc == NULL!\n",
1479 /* XXX KDM now what? */
1483 * We grab the sense information here in case
1484 * there was a failure, so we can return status
1485 * back to the initiator.
1487 io = msg->hdr.serializing_sc;
1488 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
1489 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1490 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1491 io->io_hdr.port_status = msg->scsi.fetd_status;
1492 io->scsiio.residual = msg->scsi.residual;
1493 if (msg->hdr.status != CTL_STATUS_NONE) {
1494 io->io_hdr.status = msg->hdr.status;
1495 io->scsiio.scsi_status = msg->scsi.scsi_status;
1496 io->scsiio.sense_len = msg->scsi.sense_len;
1497 io->scsiio.sense_residual =msg->scsi.sense_residual;
1498 memcpy(&io->scsiio.sense_data,
1499 &msg->scsi.sense_data,
1500 msg->scsi.sense_len);
1501 if (msg->hdr.status == CTL_SUCCESS)
1502 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1504 ctl_enqueue_isc(io);
1508 /* Preformed on Originating SC, SER_ONLY mode */
1510 io = msg->hdr.original_sc;
1512 printf("%s: original_sc == NULL!\n",
1516 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1517 io->io_hdr.msg_type = CTL_MSG_R2R;
1518 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1519 ctl_enqueue_isc(io);
1523 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
1525 * Performed on the Originating (i.e. secondary) SC in XFER
1528 case CTL_MSG_FINISH_IO:
1529 if (softc->ha_mode == CTL_HA_MODE_XFER)
1530 ctl_isc_handler_finish_xfer(softc, msg);
1532 ctl_isc_handler_finish_ser_only(softc, msg);
1535 /* Preformed on Originating SC */
1536 case CTL_MSG_BAD_JUJU:
1537 io = msg->hdr.original_sc;
1539 printf("%s: Bad JUJU!, original_sc is NULL!\n",
1543 ctl_copy_sense_data(msg, io);
1545 * IO should have already been cleaned up on other
1546 * SC so clear this flag so we won't send a message
1547 * back to finish the IO there.
1549 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
1550 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1552 /* io = msg->hdr.serializing_sc; */
1553 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
1554 ctl_enqueue_isc(io);
1557 /* Handle resets sent from the other side */
1558 case CTL_MSG_MANAGE_TASKS: {
1559 struct ctl_taskio *taskio;
1560 taskio = (struct ctl_taskio *)ctl_alloc_io(
1561 softc->othersc_pool);
1562 ctl_zero_io((union ctl_io *)taskio);
1563 taskio->io_hdr.io_type = CTL_IO_TASK;
1564 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1565 taskio->io_hdr.nexus = msg->hdr.nexus;
1566 taskio->task_action = msg->task.task_action;
1567 taskio->tag_num = msg->task.tag_num;
1568 taskio->tag_type = msg->task.tag_type;
1570 taskio->io_hdr.start_time = time_uptime;
1571 getbinuptime(&taskio->io_hdr.start_bt);
1572 #endif /* CTL_TIME_IO */
1573 ctl_run_task((union ctl_io *)taskio);
1576 /* Persistent Reserve action which needs attention */
1577 case CTL_MSG_PERS_ACTION:
1578 presio = (struct ctl_prio *)ctl_alloc_io(
1579 softc->othersc_pool);
1580 ctl_zero_io((union ctl_io *)presio);
1581 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
1582 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1583 presio->io_hdr.nexus = msg->hdr.nexus;
1584 presio->pr_msg = msg->pr;
1585 ctl_enqueue_isc((union ctl_io *)presio);
1588 ctl_isc_ua(softc, msg, param);
1590 case CTL_MSG_PORT_SYNC:
1591 ctl_isc_port_sync(softc, msg, param);
1593 case CTL_MSG_LUN_SYNC:
1594 ctl_isc_lun_sync(softc, msg, param);
1596 case CTL_MSG_IID_SYNC:
1597 ctl_isc_iid_sync(softc, msg, param);
1600 ctl_isc_login(softc, msg, param);
1602 case CTL_MSG_MODE_SYNC:
1603 ctl_isc_mode_sync(softc, msg, param);
1606 printf("Received HA message of unknown type %d\n",
1608 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1613 } else if (event == CTL_HA_EVT_LINK_CHANGE) {
1614 printf("CTL: HA link status changed from %d to %d\n",
1615 softc->ha_link, param);
1616 if (param == softc->ha_link)
1618 if (softc->ha_link == CTL_HA_LINK_ONLINE) {
1619 softc->ha_link = param;
1620 ctl_isc_ha_link_down(softc);
1622 softc->ha_link = param;
1623 if (softc->ha_link == CTL_HA_LINK_ONLINE)
1624 ctl_isc_ha_link_up(softc);
1628 printf("ctl_isc_event_handler: Unknown event %d\n", event);
1634 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
1637 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data,
1638 src->scsi.sense_len);
1639 dest->scsiio.scsi_status = src->scsi.scsi_status;
1640 dest->scsiio.sense_len = src->scsi.sense_len;
1641 dest->io_hdr.status = src->hdr.status;
1645 ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest)
1648 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data,
1649 src->scsiio.sense_len);
1650 dest->scsi.scsi_status = src->scsiio.scsi_status;
1651 dest->scsi.sense_len = src->scsiio.sense_len;
1652 dest->hdr.status = src->io_hdr.status;
1656 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1658 struct ctl_softc *softc = lun->ctl_softc;
1661 if (initidx < softc->init_min || initidx >= softc->init_max)
1663 mtx_assert(&lun->lun_lock, MA_OWNED);
1664 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1667 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua;
1671 ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua)
1675 mtx_assert(&lun->lun_lock, MA_OWNED);
1676 if (lun->pending_ua[port] == NULL)
1678 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1679 if (port * CTL_MAX_INIT_PER_PORT + i == except)
1681 lun->pending_ua[port][i] |= ua;
1686 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1688 struct ctl_softc *softc = lun->ctl_softc;
1691 mtx_assert(&lun->lun_lock, MA_OWNED);
1692 for (i = softc->port_min; i < softc->port_max; i++)
1693 ctl_est_ua_port(lun, i, except, ua);
1697 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1699 struct ctl_softc *softc = lun->ctl_softc;
1702 if (initidx < softc->init_min || initidx >= softc->init_max)
1704 mtx_assert(&lun->lun_lock, MA_OWNED);
1705 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1708 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua;
1712 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1714 struct ctl_softc *softc = lun->ctl_softc;
1717 mtx_assert(&lun->lun_lock, MA_OWNED);
1718 for (i = softc->port_min; i < softc->port_max; i++) {
1719 if (lun->pending_ua[i] == NULL)
1721 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
1722 if (i * CTL_MAX_INIT_PER_PORT + j == except)
1724 lun->pending_ua[i][j] &= ~ua;
1730 ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx,
1731 ctl_ua_type ua_type)
1733 struct ctl_lun *lun;
1735 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
1736 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) {
1737 mtx_lock(&lun->lun_lock);
1738 ctl_clr_ua(lun, initidx, ua_type);
1739 mtx_unlock(&lun->lun_lock);
1744 ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS)
1746 struct ctl_softc *softc = (struct ctl_softc *)arg1;
1747 struct ctl_lun *lun;
1748 struct ctl_lun_req ireq;
1751 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1;
1752 error = sysctl_handle_int(oidp, &value, 0, req);
1753 if ((error != 0) || (req->newptr == NULL))
1756 mtx_lock(&softc->ctl_lock);
1758 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1760 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
1761 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1762 mtx_unlock(&softc->ctl_lock);
1763 bzero(&ireq, sizeof(ireq));
1764 ireq.reqtype = CTL_LUNREQ_MODIFY;
1765 ireq.reqdata.modify.lun_id = lun->lun;
1766 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0,
1768 if (ireq.status != CTL_LUN_OK) {
1769 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n",
1770 __func__, ireq.status, ireq.error_str);
1772 mtx_lock(&softc->ctl_lock);
1774 mtx_unlock(&softc->ctl_lock);
1781 struct make_dev_args args;
1782 struct ctl_softc *softc;
1786 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1789 make_dev_args_init(&args);
1790 args.mda_devsw = &ctl_cdevsw;
1791 args.mda_uid = UID_ROOT;
1792 args.mda_gid = GID_OPERATOR;
1793 args.mda_mode = 0600;
1794 args.mda_si_drv1 = softc;
1795 error = make_dev_s(&args, &softc->dev, "cam/ctl");
1797 free(control_softc, M_DEVBUF);
1801 sysctl_ctx_init(&softc->sysctl_ctx);
1802 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1803 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1804 CTLFLAG_RD, 0, "CAM Target Layer");
1806 if (softc->sysctl_tree == NULL) {
1807 printf("%s: unable to allocate sysctl tree\n", __func__);
1808 destroy_dev(softc->dev);
1809 free(control_softc, M_DEVBUF);
1810 control_softc = NULL;
1814 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1815 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
1816 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1819 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1820 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0,
1821 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)");
1824 * In Copan's HA scheme, the "master" and "slave" roles are
1825 * figured out through the slot the controller is in. Although it
1826 * is an active/active system, someone has to be in charge.
1828 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1829 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1830 "HA head ID (0 - no HA)");
1831 if (softc->ha_id == 0 || softc->ha_id > NUM_HA_SHELVES) {
1832 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1833 softc->is_single = 1;
1834 softc->port_cnt = CTL_MAX_PORTS;
1835 softc->port_min = 0;
1837 softc->port_cnt = CTL_MAX_PORTS / NUM_HA_SHELVES;
1838 softc->port_min = (softc->ha_id - 1) * softc->port_cnt;
1840 softc->port_max = softc->port_min + softc->port_cnt;
1841 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT;
1842 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT;
1844 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1845 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0,
1846 "HA link state (0 - offline, 1 - unknown, 2 - online)");
1848 STAILQ_INIT(&softc->lun_list);
1849 STAILQ_INIT(&softc->pending_lun_queue);
1850 STAILQ_INIT(&softc->fe_list);
1851 STAILQ_INIT(&softc->port_list);
1852 STAILQ_INIT(&softc->be_list);
1853 ctl_tpc_init(softc);
1855 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
1858 printf("ctl: can't allocate %d entry other SC pool, "
1859 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1862 softc->othersc_pool = other_pool;
1864 if (worker_threads <= 0)
1865 worker_threads = max(1, mp_ncpus / 4);
1866 if (worker_threads > CTL_MAX_THREADS)
1867 worker_threads = CTL_MAX_THREADS;
1869 for (i = 0; i < worker_threads; i++) {
1870 struct ctl_thread *thr = &softc->threads[i];
1872 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1873 thr->ctl_softc = softc;
1874 STAILQ_INIT(&thr->incoming_queue);
1875 STAILQ_INIT(&thr->rtr_queue);
1876 STAILQ_INIT(&thr->done_queue);
1877 STAILQ_INIT(&thr->isc_queue);
1879 error = kproc_kthread_add(ctl_work_thread, thr,
1880 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1882 printf("error creating CTL work thread!\n");
1883 ctl_pool_free(other_pool);
1887 error = kproc_kthread_add(ctl_lun_thread, softc,
1888 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
1890 printf("error creating CTL lun thread!\n");
1891 ctl_pool_free(other_pool);
1894 error = kproc_kthread_add(ctl_thresh_thread, softc,
1895 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh");
1897 printf("error creating CTL threshold thread!\n");
1898 ctl_pool_free(other_pool);
1902 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1903 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN,
1904 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head");
1906 if (softc->is_single == 0) {
1907 ctl_frontend_register(&ha_frontend);
1908 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) {
1909 printf("ctl_init: ctl_ha_msg_init failed.\n");
1910 softc->is_single = 1;
1912 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
1913 != CTL_HA_STATUS_SUCCESS) {
1914 printf("ctl_init: ctl_ha_msg_register failed.\n");
1915 softc->is_single = 1;
1924 struct ctl_softc *softc = control_softc;
1925 struct ctl_lun *lun, *next_lun;
1927 if (softc->is_single == 0) {
1928 ctl_ha_msg_shutdown(softc);
1929 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL)
1930 != CTL_HA_STATUS_SUCCESS)
1931 printf("%s: ctl_ha_msg_deregister failed.\n", __func__);
1932 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS)
1933 printf("%s: ctl_ha_msg_destroy failed.\n", __func__);
1934 ctl_frontend_deregister(&ha_frontend);
1937 mtx_lock(&softc->ctl_lock);
1939 STAILQ_FOREACH_SAFE(lun, &softc->lun_list, links, next_lun)
1942 mtx_unlock(&softc->ctl_lock);
1945 ctl_shutdown_thread(softc->work_thread);
1946 mtx_destroy(&softc->queue_lock);
1949 ctl_tpc_shutdown(softc);
1950 uma_zdestroy(softc->io_zone);
1951 mtx_destroy(&softc->ctl_lock);
1953 destroy_dev(softc->dev);
1955 sysctl_ctx_free(&softc->sysctl_ctx);
1957 free(control_softc, M_DEVBUF);
1958 control_softc = NULL;
1962 ctl_module_event_handler(module_t mod, int what, void *arg)
1967 return (ctl_init());
1971 return (EOPNOTSUPP);
1976 * XXX KDM should we do some access checks here? Bump a reference count to
1977 * prevent a CTL module from being unloaded while someone has it open?
1980 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1986 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1992 * Remove an initiator by port number and initiator ID.
1993 * Returns 0 for success, -1 for failure.
1996 ctl_remove_initiator(struct ctl_port *port, int iid)
1998 struct ctl_softc *softc = port->ctl_softc;
2000 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2002 if (iid > CTL_MAX_INIT_PER_PORT) {
2003 printf("%s: initiator ID %u > maximun %u!\n",
2004 __func__, iid, CTL_MAX_INIT_PER_PORT);
2008 mtx_lock(&softc->ctl_lock);
2009 port->wwpn_iid[iid].in_use--;
2010 port->wwpn_iid[iid].last_use = time_uptime;
2011 mtx_unlock(&softc->ctl_lock);
2012 ctl_isc_announce_iid(port, iid);
2018 * Add an initiator to the initiator map.
2019 * Returns iid for success, < 0 for failure.
2022 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
2024 struct ctl_softc *softc = port->ctl_softc;
2028 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2030 if (iid >= CTL_MAX_INIT_PER_PORT) {
2031 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
2032 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
2037 mtx_lock(&softc->ctl_lock);
2039 if (iid < 0 && (wwpn != 0 || name != NULL)) {
2040 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2041 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
2045 if (name != NULL && port->wwpn_iid[i].name != NULL &&
2046 strcmp(name, port->wwpn_iid[i].name) == 0) {
2054 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2055 if (port->wwpn_iid[i].in_use == 0 &&
2056 port->wwpn_iid[i].wwpn == 0 &&
2057 port->wwpn_iid[i].name == NULL) {
2066 best_time = INT32_MAX;
2067 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2068 if (port->wwpn_iid[i].in_use == 0) {
2069 if (port->wwpn_iid[i].last_use < best_time) {
2071 best_time = port->wwpn_iid[i].last_use;
2079 mtx_unlock(&softc->ctl_lock);
2084 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
2086 * This is not an error yet.
2088 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
2090 printf("%s: port %d iid %u WWPN %#jx arrived"
2091 " again\n", __func__, port->targ_port,
2092 iid, (uintmax_t)wwpn);
2096 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
2097 strcmp(name, port->wwpn_iid[iid].name) == 0) {
2099 printf("%s: port %d iid %u name '%s' arrived"
2100 " again\n", __func__, port->targ_port,
2107 * This is an error, but what do we do about it? The
2108 * driver is telling us we have a new WWPN for this
2109 * initiator ID, so we pretty much need to use it.
2111 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
2112 " but WWPN %#jx '%s' is still at that address\n",
2113 __func__, port->targ_port, iid, wwpn, name,
2114 (uintmax_t)port->wwpn_iid[iid].wwpn,
2115 port->wwpn_iid[iid].name);
2118 * XXX KDM clear have_ca and ua_pending on each LUN for
2123 free(port->wwpn_iid[iid].name, M_CTL);
2124 port->wwpn_iid[iid].name = name;
2125 port->wwpn_iid[iid].wwpn = wwpn;
2126 port->wwpn_iid[iid].in_use++;
2127 mtx_unlock(&softc->ctl_lock);
2128 ctl_isc_announce_iid(port, iid);
2134 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
2138 switch (port->port_type) {
2141 struct scsi_transportid_fcp *id =
2142 (struct scsi_transportid_fcp *)buf;
2143 if (port->wwpn_iid[iid].wwpn == 0)
2145 memset(id, 0, sizeof(*id));
2146 id->format_protocol = SCSI_PROTO_FC;
2147 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
2148 return (sizeof(*id));
2150 case CTL_PORT_ISCSI:
2152 struct scsi_transportid_iscsi_port *id =
2153 (struct scsi_transportid_iscsi_port *)buf;
2154 if (port->wwpn_iid[iid].name == NULL)
2157 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
2159 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
2160 len = roundup2(min(len, 252), 4);
2161 scsi_ulto2b(len, id->additional_length);
2162 return (sizeof(*id) + len);
2166 struct scsi_transportid_sas *id =
2167 (struct scsi_transportid_sas *)buf;
2168 if (port->wwpn_iid[iid].wwpn == 0)
2170 memset(id, 0, sizeof(*id));
2171 id->format_protocol = SCSI_PROTO_SAS;
2172 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
2173 return (sizeof(*id));
2177 struct scsi_transportid_spi *id =
2178 (struct scsi_transportid_spi *)buf;
2179 memset(id, 0, sizeof(*id));
2180 id->format_protocol = SCSI_PROTO_SPI;
2181 scsi_ulto2b(iid, id->scsi_addr);
2182 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
2183 return (sizeof(*id));
2189 * Serialize a command that went down the "wrong" side, and so was sent to
2190 * this controller for execution. The logic is a little different than the
2191 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
2192 * sent back to the other side, but in the success case, we execute the
2193 * command on this side (XFER mode) or tell the other side to execute it
2197 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
2199 struct ctl_softc *softc = control_softc;
2200 union ctl_ha_msg msg_info;
2201 struct ctl_port *port;
2202 struct ctl_lun *lun;
2203 const struct ctl_cmd_entry *entry;
2207 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
2208 mtx_lock(&softc->ctl_lock);
2210 /* Make sure that we know about this port. */
2211 port = ctl_io_port(&ctsio->io_hdr);
2212 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) {
2213 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2218 /* Make sure that we know about this LUN. */
2219 if ((targ_lun < CTL_MAX_LUNS) &&
2220 ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
2221 mtx_lock(&lun->lun_lock);
2222 mtx_unlock(&softc->ctl_lock);
2224 * If the LUN is invalid, pretend that it doesn't exist.
2225 * It will go away as soon as all pending I/O has been
2228 if (lun->flags & CTL_LUN_DISABLED) {
2229 mtx_unlock(&lun->lun_lock);
2233 mtx_unlock(&softc->ctl_lock);
2238 * The other node would not send this request to us unless
2239 * received announce that we are primary node for this LUN.
2240 * If this LUN does not exist now, it is probably result of
2241 * a race, so respond to initiator in the most opaque way.
2243 ctl_set_busy(ctsio);
2247 entry = ctl_get_cmd_entry(ctsio, NULL);
2248 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
2249 mtx_unlock(&lun->lun_lock);
2253 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
2254 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = lun->be_lun;
2257 * Every I/O goes into the OOA queue for a
2258 * particular LUN, and stays there until completion.
2261 if (TAILQ_EMPTY(&lun->ooa_queue))
2262 lun->idle_time += getsbinuptime() - lun->last_busy;
2264 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2266 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
2267 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
2269 case CTL_ACTION_BLOCK:
2270 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
2271 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
2273 mtx_unlock(&lun->lun_lock);
2275 case CTL_ACTION_PASS:
2276 case CTL_ACTION_SKIP:
2277 if (softc->ha_mode == CTL_HA_MODE_XFER) {
2278 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
2279 ctl_enqueue_rtr((union ctl_io *)ctsio);
2280 mtx_unlock(&lun->lun_lock);
2282 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
2283 mtx_unlock(&lun->lun_lock);
2285 /* send msg back to other side */
2286 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2287 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
2288 msg_info.hdr.msg_type = CTL_MSG_R2R;
2289 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2290 sizeof(msg_info.hdr), M_WAITOK);
2293 case CTL_ACTION_OVERLAP:
2294 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2295 mtx_unlock(&lun->lun_lock);
2296 ctl_set_overlapped_cmd(ctsio);
2298 case CTL_ACTION_OVERLAP_TAG:
2299 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2300 mtx_unlock(&lun->lun_lock);
2301 ctl_set_overlapped_tag(ctsio, ctsio->tag_num);
2303 case CTL_ACTION_ERROR:
2305 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2306 mtx_unlock(&lun->lun_lock);
2308 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2311 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
2312 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2313 msg_info.hdr.serializing_sc = NULL;
2314 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
2315 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2316 sizeof(msg_info.scsi), M_WAITOK);
2324 * Returns 0 for success, errno for failure.
2327 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2328 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2332 mtx_lock(&lun->lun_lock);
2333 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2334 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2336 struct ctl_ooa_entry *entry;
2339 * If we've got more than we can fit, just count the
2340 * remaining entries.
2342 if (*cur_fill_num >= ooa_hdr->alloc_num)
2345 entry = &kern_entries[*cur_fill_num];
2347 entry->tag_num = io->scsiio.tag_num;
2348 entry->lun_num = lun->lun;
2350 entry->start_bt = io->io_hdr.start_bt;
2352 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2353 entry->cdb_len = io->scsiio.cdb_len;
2354 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2355 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2357 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2358 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2360 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2361 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2363 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2364 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2366 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2367 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2369 mtx_unlock(&lun->lun_lock);
2373 ctl_copyin_alloc(void *user_addr, unsigned int len, char *error_str,
2374 size_t error_str_len)
2378 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2380 if (copyin(user_addr, kptr, len) != 0) {
2381 snprintf(error_str, error_str_len, "Error copying %d bytes "
2382 "from user address %p to kernel address %p", len,
2392 ctl_free_args(int num_args, struct ctl_be_arg *args)
2399 for (i = 0; i < num_args; i++) {
2400 free(args[i].kname, M_CTL);
2401 free(args[i].kvalue, M_CTL);
2407 static struct ctl_be_arg *
2408 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
2409 char *error_str, size_t error_str_len)
2411 struct ctl_be_arg *args;
2414 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
2415 error_str, error_str_len);
2420 for (i = 0; i < num_args; i++) {
2421 args[i].kname = NULL;
2422 args[i].kvalue = NULL;
2425 for (i = 0; i < num_args; i++) {
2428 if (args[i].namelen == 0) {
2429 snprintf(error_str, error_str_len, "Argument %d "
2430 "name length is zero", i);
2434 args[i].kname = ctl_copyin_alloc(args[i].name,
2435 args[i].namelen, error_str, error_str_len);
2436 if (args[i].kname == NULL)
2439 if (args[i].kname[args[i].namelen - 1] != '\0') {
2440 snprintf(error_str, error_str_len, "Argument %d "
2441 "name is not NUL-terminated", i);
2445 if (args[i].flags & CTL_BEARG_RD) {
2446 if (args[i].vallen == 0) {
2447 snprintf(error_str, error_str_len, "Argument %d "
2448 "value length is zero", i);
2452 tmpptr = ctl_copyin_alloc(args[i].value,
2453 args[i].vallen, error_str, error_str_len);
2457 if ((args[i].flags & CTL_BEARG_ASCII)
2458 && (tmpptr[args[i].vallen - 1] != '\0')) {
2459 snprintf(error_str, error_str_len, "Argument "
2460 "%d value is not NUL-terminated", i);
2461 free(tmpptr, M_CTL);
2464 args[i].kvalue = tmpptr;
2466 args[i].kvalue = malloc(args[i].vallen,
2467 M_CTL, M_WAITOK | M_ZERO);
2474 ctl_free_args(num_args, args);
2480 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2484 for (i = 0; i < num_args; i++) {
2485 if (args[i].flags & CTL_BEARG_WR)
2486 copyout(args[i].kvalue, args[i].value, args[i].vallen);
2491 * Escape characters that are illegal or not recommended in XML.
2494 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2496 char *end = str + size;
2501 for (; *str && str < end; str++) {
2504 retval = sbuf_printf(sb, "&");
2507 retval = sbuf_printf(sb, ">");
2510 retval = sbuf_printf(sb, "<");
2513 retval = sbuf_putc(sb, *str);
2526 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2528 struct scsi_vpd_id_descriptor *desc;
2531 if (id == NULL || id->len < 4)
2533 desc = (struct scsi_vpd_id_descriptor *)id->data;
2534 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2535 case SVPD_ID_TYPE_T10:
2536 sbuf_printf(sb, "t10.");
2538 case SVPD_ID_TYPE_EUI64:
2539 sbuf_printf(sb, "eui.");
2541 case SVPD_ID_TYPE_NAA:
2542 sbuf_printf(sb, "naa.");
2544 case SVPD_ID_TYPE_SCSI_NAME:
2547 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2548 case SVPD_ID_CODESET_BINARY:
2549 for (i = 0; i < desc->length; i++)
2550 sbuf_printf(sb, "%02x", desc->identifier[i]);
2552 case SVPD_ID_CODESET_ASCII:
2553 sbuf_printf(sb, "%.*s", (int)desc->length,
2554 (char *)desc->identifier);
2556 case SVPD_ID_CODESET_UTF8:
2557 sbuf_printf(sb, "%s", (char *)desc->identifier);
2563 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2566 struct ctl_softc *softc = dev->si_drv1;
2567 struct ctl_lun *lun;
2574 retval = ctl_ioctl_io(dev, cmd, addr, flag, td);
2576 case CTL_ENABLE_PORT:
2577 case CTL_DISABLE_PORT:
2578 case CTL_SET_PORT_WWNS: {
2579 struct ctl_port *port;
2580 struct ctl_port_entry *entry;
2582 entry = (struct ctl_port_entry *)addr;
2584 mtx_lock(&softc->ctl_lock);
2585 STAILQ_FOREACH(port, &softc->port_list, links) {
2588 if (port->targ_port < softc->port_min ||
2589 port->targ_port >= softc->port_max)
2594 if ((entry->port_type == CTL_PORT_NONE)
2595 && (entry->targ_port == port->targ_port)) {
2597 * If the user only wants to enable or
2598 * disable or set WWNs on a specific port,
2599 * do the operation and we're done.
2603 } else if (entry->port_type & port->port_type) {
2605 * Compare the user's type mask with the
2606 * particular frontend type to see if we
2613 * Make sure the user isn't trying to set
2614 * WWNs on multiple ports at the same time.
2616 if (cmd == CTL_SET_PORT_WWNS) {
2617 printf("%s: Can't set WWNs on "
2618 "multiple ports\n", __func__);
2627 * XXX KDM we have to drop the lock here, because
2628 * the online/offline operations can potentially
2629 * block. We need to reference count the frontends
2630 * so they can't go away,
2632 if (cmd == CTL_ENABLE_PORT) {
2633 mtx_unlock(&softc->ctl_lock);
2634 ctl_port_online(port);
2635 mtx_lock(&softc->ctl_lock);
2636 } else if (cmd == CTL_DISABLE_PORT) {
2637 mtx_unlock(&softc->ctl_lock);
2638 ctl_port_offline(port);
2639 mtx_lock(&softc->ctl_lock);
2640 } else if (cmd == CTL_SET_PORT_WWNS) {
2641 ctl_port_set_wwns(port,
2642 (entry->flags & CTL_PORT_WWNN_VALID) ?
2644 (entry->flags & CTL_PORT_WWPN_VALID) ?
2645 1 : 0, entry->wwpn);
2650 mtx_unlock(&softc->ctl_lock);
2654 struct ctl_ooa *ooa_hdr;
2655 struct ctl_ooa_entry *entries;
2656 uint32_t cur_fill_num;
2658 ooa_hdr = (struct ctl_ooa *)addr;
2660 if ((ooa_hdr->alloc_len == 0)
2661 || (ooa_hdr->alloc_num == 0)) {
2662 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2663 "must be non-zero\n", __func__,
2664 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2669 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2670 sizeof(struct ctl_ooa_entry))) {
2671 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2672 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2673 __func__, ooa_hdr->alloc_len,
2674 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2679 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2680 if (entries == NULL) {
2681 printf("%s: could not allocate %d bytes for OOA "
2682 "dump\n", __func__, ooa_hdr->alloc_len);
2687 mtx_lock(&softc->ctl_lock);
2688 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2689 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS)
2690 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2691 mtx_unlock(&softc->ctl_lock);
2692 free(entries, M_CTL);
2693 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2694 __func__, (uintmax_t)ooa_hdr->lun_num);
2701 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2702 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2703 ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2707 lun = softc->ctl_luns[ooa_hdr->lun_num];
2708 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr,
2711 mtx_unlock(&softc->ctl_lock);
2713 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2714 ooa_hdr->fill_len = ooa_hdr->fill_num *
2715 sizeof(struct ctl_ooa_entry);
2716 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2718 printf("%s: error copying out %d bytes for OOA dump\n",
2719 __func__, ooa_hdr->fill_len);
2722 getbinuptime(&ooa_hdr->cur_bt);
2724 if (cur_fill_num > ooa_hdr->alloc_num) {
2725 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2726 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2728 ooa_hdr->dropped_num = 0;
2729 ooa_hdr->status = CTL_OOA_OK;
2732 free(entries, M_CTL);
2735 case CTL_DELAY_IO: {
2736 struct ctl_io_delay_info *delay_info;
2738 delay_info = (struct ctl_io_delay_info *)addr;
2741 mtx_lock(&softc->ctl_lock);
2743 if ((delay_info->lun_id >= CTL_MAX_LUNS)
2744 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2745 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2747 lun = softc->ctl_luns[delay_info->lun_id];
2748 mtx_lock(&lun->lun_lock);
2750 delay_info->status = CTL_DELAY_STATUS_OK;
2752 switch (delay_info->delay_type) {
2753 case CTL_DELAY_TYPE_CONT:
2755 case CTL_DELAY_TYPE_ONESHOT:
2758 delay_info->status =
2759 CTL_DELAY_STATUS_INVALID_TYPE;
2763 switch (delay_info->delay_loc) {
2764 case CTL_DELAY_LOC_DATAMOVE:
2765 lun->delay_info.datamove_type =
2766 delay_info->delay_type;
2767 lun->delay_info.datamove_delay =
2768 delay_info->delay_secs;
2770 case CTL_DELAY_LOC_DONE:
2771 lun->delay_info.done_type =
2772 delay_info->delay_type;
2773 lun->delay_info.done_delay =
2774 delay_info->delay_secs;
2777 delay_info->status =
2778 CTL_DELAY_STATUS_INVALID_LOC;
2781 mtx_unlock(&lun->lun_lock);
2784 mtx_unlock(&softc->ctl_lock);
2786 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2787 #endif /* CTL_IO_DELAY */
2790 case CTL_GETSTATS: {
2791 struct ctl_stats *stats;
2794 stats = (struct ctl_stats *)addr;
2796 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2798 stats->status = CTL_SS_NEED_MORE_SPACE;
2799 stats->num_luns = softc->num_luns;
2803 * XXX KDM no locking here. If the LUN list changes,
2804 * things can blow up.
2807 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2808 retval = copyout(&lun->stats, &stats->lun_stats[i++],
2809 sizeof(lun->stats));
2813 stats->num_luns = softc->num_luns;
2814 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2816 stats->status = CTL_SS_OK;
2818 stats->flags = CTL_STATS_FLAG_TIME_VALID;
2820 stats->flags = CTL_STATS_FLAG_NONE;
2822 getnanouptime(&stats->timestamp);
2825 case CTL_ERROR_INJECT: {
2826 struct ctl_error_desc *err_desc, *new_err_desc;
2828 err_desc = (struct ctl_error_desc *)addr;
2830 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2832 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2834 mtx_lock(&softc->ctl_lock);
2835 lun = softc->ctl_luns[err_desc->lun_id];
2837 mtx_unlock(&softc->ctl_lock);
2838 free(new_err_desc, M_CTL);
2839 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2840 __func__, (uintmax_t)err_desc->lun_id);
2844 mtx_lock(&lun->lun_lock);
2845 mtx_unlock(&softc->ctl_lock);
2848 * We could do some checking here to verify the validity
2849 * of the request, but given the complexity of error
2850 * injection requests, the checking logic would be fairly
2853 * For now, if the request is invalid, it just won't get
2854 * executed and might get deleted.
2856 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2859 * XXX KDM check to make sure the serial number is unique,
2860 * in case we somehow manage to wrap. That shouldn't
2861 * happen for a very long time, but it's the right thing to
2864 new_err_desc->serial = lun->error_serial;
2865 err_desc->serial = lun->error_serial;
2866 lun->error_serial++;
2868 mtx_unlock(&lun->lun_lock);
2871 case CTL_ERROR_INJECT_DELETE: {
2872 struct ctl_error_desc *delete_desc, *desc, *desc2;
2875 delete_desc = (struct ctl_error_desc *)addr;
2878 mtx_lock(&softc->ctl_lock);
2879 lun = softc->ctl_luns[delete_desc->lun_id];
2881 mtx_unlock(&softc->ctl_lock);
2882 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2883 __func__, (uintmax_t)delete_desc->lun_id);
2887 mtx_lock(&lun->lun_lock);
2888 mtx_unlock(&softc->ctl_lock);
2889 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2890 if (desc->serial != delete_desc->serial)
2893 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2898 mtx_unlock(&lun->lun_lock);
2899 if (delete_done == 0) {
2900 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2901 "error serial %ju on LUN %u\n", __func__,
2902 delete_desc->serial, delete_desc->lun_id);
2908 case CTL_DUMP_STRUCTS: {
2910 struct ctl_port *port;
2911 struct ctl_frontend *fe;
2913 mtx_lock(&softc->ctl_lock);
2914 printf("CTL Persistent Reservation information start:\n");
2915 for (i = 0; i < CTL_MAX_LUNS; i++) {
2916 lun = softc->ctl_luns[i];
2919 || ((lun->flags & CTL_LUN_DISABLED) != 0))
2922 for (j = 0; j < CTL_MAX_PORTS; j++) {
2923 if (lun->pr_keys[j] == NULL)
2925 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2926 if (lun->pr_keys[j][k] == 0)
2928 printf(" LUN %d port %d iid %d key "
2930 (uintmax_t)lun->pr_keys[j][k]);
2934 printf("CTL Persistent Reservation information end\n");
2935 printf("CTL Ports:\n");
2936 STAILQ_FOREACH(port, &softc->port_list, links) {
2937 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
2938 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
2939 port->frontend->name, port->port_type,
2940 port->physical_port, port->virtual_port,
2941 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
2942 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2943 if (port->wwpn_iid[j].in_use == 0 &&
2944 port->wwpn_iid[j].wwpn == 0 &&
2945 port->wwpn_iid[j].name == NULL)
2948 printf(" iid %u use %d WWPN %#jx '%s'\n",
2949 j, port->wwpn_iid[j].in_use,
2950 (uintmax_t)port->wwpn_iid[j].wwpn,
2951 port->wwpn_iid[j].name);
2954 printf("CTL Port information end\n");
2955 mtx_unlock(&softc->ctl_lock);
2957 * XXX KDM calling this without a lock. We'd likely want
2958 * to drop the lock before calling the frontend's dump
2961 printf("CTL Frontends:\n");
2962 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2963 printf(" Frontend '%s'\n", fe->name);
2964 if (fe->fe_dump != NULL)
2967 printf("CTL Frontend information end\n");
2971 struct ctl_lun_req *lun_req;
2972 struct ctl_backend_driver *backend;
2974 lun_req = (struct ctl_lun_req *)addr;
2976 backend = ctl_backend_find(lun_req->backend);
2977 if (backend == NULL) {
2978 lun_req->status = CTL_LUN_ERROR;
2979 snprintf(lun_req->error_str,
2980 sizeof(lun_req->error_str),
2981 "Backend \"%s\" not found.",
2985 if (lun_req->num_be_args > 0) {
2986 lun_req->kern_be_args = ctl_copyin_args(
2987 lun_req->num_be_args,
2990 sizeof(lun_req->error_str));
2991 if (lun_req->kern_be_args == NULL) {
2992 lun_req->status = CTL_LUN_ERROR;
2997 retval = backend->ioctl(dev, cmd, addr, flag, td);
2999 if (lun_req->num_be_args > 0) {
3000 ctl_copyout_args(lun_req->num_be_args,
3001 lun_req->kern_be_args);
3002 ctl_free_args(lun_req->num_be_args,
3003 lun_req->kern_be_args);
3007 case CTL_LUN_LIST: {
3009 struct ctl_lun_list *list;
3010 struct ctl_option *opt;
3012 list = (struct ctl_lun_list *)addr;
3015 * Allocate a fixed length sbuf here, based on the length
3016 * of the user's buffer. We could allocate an auto-extending
3017 * buffer, and then tell the user how much larger our
3018 * amount of data is than his buffer, but that presents
3021 * 1. The sbuf(9) routines use a blocking malloc, and so
3022 * we can't hold a lock while calling them with an
3023 * auto-extending buffer.
3025 * 2. There is not currently a LUN reference counting
3026 * mechanism, outside of outstanding transactions on
3027 * the LUN's OOA queue. So a LUN could go away on us
3028 * while we're getting the LUN number, backend-specific
3029 * information, etc. Thus, given the way things
3030 * currently work, we need to hold the CTL lock while
3031 * grabbing LUN information.
3033 * So, from the user's standpoint, the best thing to do is
3034 * allocate what he thinks is a reasonable buffer length,
3035 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3036 * double the buffer length and try again. (And repeat
3037 * that until he succeeds.)
3039 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3041 list->status = CTL_LUN_LIST_ERROR;
3042 snprintf(list->error_str, sizeof(list->error_str),
3043 "Unable to allocate %d bytes for LUN list",
3048 sbuf_printf(sb, "<ctllunlist>\n");
3050 mtx_lock(&softc->ctl_lock);
3051 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3052 mtx_lock(&lun->lun_lock);
3053 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3054 (uintmax_t)lun->lun);
3057 * Bail out as soon as we see that we've overfilled
3063 retval = sbuf_printf(sb, "\t<backend_type>%s"
3064 "</backend_type>\n",
3065 (lun->backend == NULL) ? "none" :
3066 lun->backend->name);
3071 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3072 lun->be_lun->lun_type);
3077 if (lun->backend == NULL) {
3078 retval = sbuf_printf(sb, "</lun>\n");
3084 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3085 (lun->be_lun->maxlba > 0) ?
3086 lun->be_lun->maxlba + 1 : 0);
3091 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3092 lun->be_lun->blocksize);
3097 retval = sbuf_printf(sb, "\t<serial_number>");
3102 retval = ctl_sbuf_printf_esc(sb,
3103 lun->be_lun->serial_num,
3104 sizeof(lun->be_lun->serial_num));
3109 retval = sbuf_printf(sb, "</serial_number>\n");
3114 retval = sbuf_printf(sb, "\t<device_id>");
3119 retval = ctl_sbuf_printf_esc(sb,
3120 lun->be_lun->device_id,
3121 sizeof(lun->be_lun->device_id));
3126 retval = sbuf_printf(sb, "</device_id>\n");
3131 if (lun->backend->lun_info != NULL) {
3132 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3136 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3137 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3138 opt->name, opt->value, opt->name);
3143 retval = sbuf_printf(sb, "</lun>\n");
3147 mtx_unlock(&lun->lun_lock);
3150 mtx_unlock(&lun->lun_lock);
3151 mtx_unlock(&softc->ctl_lock);
3154 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3157 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3158 snprintf(list->error_str, sizeof(list->error_str),
3159 "Out of space, %d bytes is too small",
3166 retval = copyout(sbuf_data(sb), list->lun_xml,
3169 list->fill_len = sbuf_len(sb) + 1;
3170 list->status = CTL_LUN_LIST_OK;
3175 struct ctl_iscsi *ci;
3176 struct ctl_frontend *fe;
3178 ci = (struct ctl_iscsi *)addr;
3180 fe = ctl_frontend_find("iscsi");
3182 ci->status = CTL_ISCSI_ERROR;
3183 snprintf(ci->error_str, sizeof(ci->error_str),
3184 "Frontend \"iscsi\" not found.");
3188 retval = fe->ioctl(dev, cmd, addr, flag, td);
3191 case CTL_PORT_REQ: {
3192 struct ctl_req *req;
3193 struct ctl_frontend *fe;
3195 req = (struct ctl_req *)addr;
3197 fe = ctl_frontend_find(req->driver);
3199 req->status = CTL_LUN_ERROR;
3200 snprintf(req->error_str, sizeof(req->error_str),
3201 "Frontend \"%s\" not found.", req->driver);
3204 if (req->num_args > 0) {
3205 req->kern_args = ctl_copyin_args(req->num_args,
3206 req->args, req->error_str, sizeof(req->error_str));
3207 if (req->kern_args == NULL) {
3208 req->status = CTL_LUN_ERROR;
3214 retval = fe->ioctl(dev, cmd, addr, flag, td);
3218 if (req->num_args > 0) {
3219 ctl_copyout_args(req->num_args, req->kern_args);
3220 ctl_free_args(req->num_args, req->kern_args);
3224 case CTL_PORT_LIST: {
3226 struct ctl_port *port;
3227 struct ctl_lun_list *list;
3228 struct ctl_option *opt;
3232 list = (struct ctl_lun_list *)addr;
3234 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3236 list->status = CTL_LUN_LIST_ERROR;
3237 snprintf(list->error_str, sizeof(list->error_str),
3238 "Unable to allocate %d bytes for LUN list",
3243 sbuf_printf(sb, "<ctlportlist>\n");
3245 mtx_lock(&softc->ctl_lock);
3246 STAILQ_FOREACH(port, &softc->port_list, links) {
3247 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3248 (uintmax_t)port->targ_port);
3251 * Bail out as soon as we see that we've overfilled
3257 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3258 "</frontend_type>\n", port->frontend->name);
3262 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3267 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3268 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3272 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3277 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3278 port->physical_port);
3282 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3283 port->virtual_port);
3287 if (port->target_devid != NULL) {
3288 sbuf_printf(sb, "\t<target>");
3289 ctl_id_sbuf(port->target_devid, sb);
3290 sbuf_printf(sb, "</target>\n");
3293 if (port->port_devid != NULL) {
3294 sbuf_printf(sb, "\t<port>");
3295 ctl_id_sbuf(port->port_devid, sb);
3296 sbuf_printf(sb, "</port>\n");
3299 if (port->port_info != NULL) {
3300 retval = port->port_info(port->onoff_arg, sb);
3304 STAILQ_FOREACH(opt, &port->options, links) {
3305 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3306 opt->name, opt->value, opt->name);
3311 if (port->lun_map != NULL) {
3312 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n");
3313 for (j = 0; j < CTL_MAX_LUNS; j++) {
3314 plun = ctl_lun_map_from_port(port, j);
3315 if (plun >= CTL_MAX_LUNS)
3318 "\t<lun id=\"%u\">%u</lun>\n",
3323 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3324 if (port->wwpn_iid[j].in_use == 0 ||
3325 (port->wwpn_iid[j].wwpn == 0 &&
3326 port->wwpn_iid[j].name == NULL))
3329 if (port->wwpn_iid[j].name != NULL)
3330 retval = sbuf_printf(sb,
3331 "\t<initiator id=\"%u\">%s</initiator>\n",
3332 j, port->wwpn_iid[j].name);
3334 retval = sbuf_printf(sb,
3335 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3336 j, port->wwpn_iid[j].wwpn);
3343 retval = sbuf_printf(sb, "</targ_port>\n");
3347 mtx_unlock(&softc->ctl_lock);
3350 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3353 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3354 snprintf(list->error_str, sizeof(list->error_str),
3355 "Out of space, %d bytes is too small",
3362 retval = copyout(sbuf_data(sb), list->lun_xml,
3365 list->fill_len = sbuf_len(sb) + 1;
3366 list->status = CTL_LUN_LIST_OK;
3371 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr;
3372 struct ctl_port *port;
3374 mtx_lock(&softc->ctl_lock);
3375 if (lm->port < softc->port_min ||
3376 lm->port >= softc->port_max ||
3377 (port = softc->ctl_ports[lm->port]) == NULL) {
3378 mtx_unlock(&softc->ctl_lock);
3381 if (port->status & CTL_PORT_STATUS_ONLINE) {
3382 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3383 if (ctl_lun_map_to_port(port, lun->lun) >=
3386 mtx_lock(&lun->lun_lock);
3387 ctl_est_ua_port(lun, lm->port, -1,
3389 mtx_unlock(&lun->lun_lock);
3392 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps
3393 if (lm->plun < CTL_MAX_LUNS) {
3394 if (lm->lun == UINT32_MAX)
3395 retval = ctl_lun_map_unset(port, lm->plun);
3396 else if (lm->lun < CTL_MAX_LUNS &&
3397 softc->ctl_luns[lm->lun] != NULL)
3398 retval = ctl_lun_map_set(port, lm->plun, lm->lun);
3401 } else if (lm->plun == UINT32_MAX) {
3402 if (lm->lun == UINT32_MAX)
3403 retval = ctl_lun_map_deinit(port);
3405 retval = ctl_lun_map_init(port);
3408 if (port->status & CTL_PORT_STATUS_ONLINE)
3409 ctl_isc_announce_port(port);
3413 /* XXX KDM should we fix this? */
3415 struct ctl_backend_driver *backend;
3422 * We encode the backend type as the ioctl type for backend
3423 * ioctls. So parse it out here, and then search for a
3424 * backend of this type.
3426 type = _IOC_TYPE(cmd);
3428 STAILQ_FOREACH(backend, &softc->be_list, links) {
3429 if (backend->type == type) {
3435 printf("ctl: unknown ioctl command %#lx or backend "
3440 retval = backend->ioctl(dev, cmd, addr, flag, td);
3450 ctl_get_initindex(struct ctl_nexus *nexus)
3452 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3456 ctl_lun_map_init(struct ctl_port *port)
3458 struct ctl_softc *softc = port->ctl_softc;
3459 struct ctl_lun *lun;
3462 if (port->lun_map == NULL)
3463 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS,
3465 if (port->lun_map == NULL)
3467 for (i = 0; i < CTL_MAX_LUNS; i++)
3468 port->lun_map[i] = UINT32_MAX;
3469 if (port->status & CTL_PORT_STATUS_ONLINE) {
3470 if (port->lun_disable != NULL) {
3471 STAILQ_FOREACH(lun, &softc->lun_list, links)
3472 port->lun_disable(port->targ_lun_arg, lun->lun);
3474 ctl_isc_announce_port(port);
3480 ctl_lun_map_deinit(struct ctl_port *port)
3482 struct ctl_softc *softc = port->ctl_softc;
3483 struct ctl_lun *lun;
3485 if (port->lun_map == NULL)
3487 free(port->lun_map, M_CTL);
3488 port->lun_map = NULL;
3489 if (port->status & CTL_PORT_STATUS_ONLINE) {
3490 if (port->lun_enable != NULL) {
3491 STAILQ_FOREACH(lun, &softc->lun_list, links)
3492 port->lun_enable(port->targ_lun_arg, lun->lun);
3494 ctl_isc_announce_port(port);
3500 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun)
3505 if (port->lun_map == NULL) {
3506 status = ctl_lun_map_init(port);
3510 old = port->lun_map[plun];
3511 port->lun_map[plun] = glun;
3512 if ((port->status & CTL_PORT_STATUS_ONLINE) && old >= CTL_MAX_LUNS) {
3513 if (port->lun_enable != NULL)
3514 port->lun_enable(port->targ_lun_arg, plun);
3515 ctl_isc_announce_port(port);
3521 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun)
3525 if (port->lun_map == NULL)
3527 old = port->lun_map[plun];
3528 port->lun_map[plun] = UINT32_MAX;
3529 if ((port->status & CTL_PORT_STATUS_ONLINE) && old < CTL_MAX_LUNS) {
3530 if (port->lun_disable != NULL)
3531 port->lun_disable(port->targ_lun_arg, plun);
3532 ctl_isc_announce_port(port);
3538 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
3542 return (UINT32_MAX);
3543 if (port->lun_map == NULL || lun_id >= CTL_MAX_LUNS)
3545 return (port->lun_map[lun_id]);
3549 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
3554 return (UINT32_MAX);
3555 if (port->lun_map == NULL)
3557 for (i = 0; i < CTL_MAX_LUNS; i++) {
3558 if (port->lun_map[i] == lun_id)
3561 return (UINT32_MAX);
3565 ctl_decode_lun(uint64_t encoded)
3568 uint32_t result = 0xffffffff;
3570 be64enc(lun, encoded);
3571 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) {
3572 case RPL_LUNDATA_ATYP_PERIPH:
3573 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 &&
3574 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0)
3577 case RPL_LUNDATA_ATYP_FLAT:
3578 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 &&
3579 lun[6] == 0 && lun[7] == 0)
3580 result = ((lun[0] & 0x3f) << 8) + lun[1];
3582 case RPL_LUNDATA_ATYP_EXTLUN:
3583 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) {
3585 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) {
3590 result = (lun[1] << 16) + (lun[2] << 8) +
3594 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0)
3595 result = (lun[2] << 24) +
3596 (lun[3] << 16) + (lun[4] << 8) +
3601 case RPL_LUNDATA_EXT_EAM_NOT_SPEC:
3602 result = 0xffffffff;
3611 ctl_encode_lun(uint32_t decoded)
3613 uint64_t l = decoded;
3616 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48));
3618 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48));
3620 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) |
3622 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16));
3625 static struct ctl_port *
3626 ctl_io_port(struct ctl_io_hdr *io_hdr)
3629 return (control_softc->ctl_ports[io_hdr->nexus.targ_port]);
3633 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last)
3637 for (i = first; i < last; i++) {
3638 if ((mask[i / 32] & (1 << (i % 32))) == 0)
3645 ctl_set_mask(uint32_t *mask, uint32_t bit)
3647 uint32_t chunk, piece;
3650 piece = bit % (sizeof(uint32_t) * 8);
3652 if ((mask[chunk] & (1 << piece)) != 0)
3655 mask[chunk] |= (1 << piece);
3661 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3663 uint32_t chunk, piece;
3666 piece = bit % (sizeof(uint32_t) * 8);
3668 if ((mask[chunk] & (1 << piece)) == 0)
3671 mask[chunk] &= ~(1 << piece);
3677 ctl_is_set(uint32_t *mask, uint32_t bit)
3679 uint32_t chunk, piece;
3682 piece = bit % (sizeof(uint32_t) * 8);
3684 if ((mask[chunk] & (1 << piece)) == 0)
3691 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3695 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3698 return (t[residx % CTL_MAX_INIT_PER_PORT]);
3702 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3706 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3709 t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3713 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3718 i = residx/CTL_MAX_INIT_PER_PORT;
3719 if (lun->pr_keys[i] != NULL)
3721 mtx_unlock(&lun->lun_lock);
3722 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3724 mtx_lock(&lun->lun_lock);
3725 if (lun->pr_keys[i] == NULL)
3726 lun->pr_keys[i] = p;
3732 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3736 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3737 KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3738 t[residx % CTL_MAX_INIT_PER_PORT] = key;
3742 * ctl_softc, pool_name, total_ctl_io are passed in.
3743 * npool is passed out.
3746 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3747 uint32_t total_ctl_io, void **npool)
3750 struct ctl_io_pool *pool;
3752 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3757 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3758 pool->ctl_softc = ctl_softc;
3759 pool->zone = uma_zsecond_create(pool->name, NULL,
3760 NULL, NULL, NULL, ctl_softc->io_zone);
3761 /* uma_prealloc(pool->zone, total_ctl_io); */
3765 *npool = ctl_softc->io_zone;
3771 ctl_pool_free(struct ctl_io_pool *pool)
3778 uma_zdestroy(pool->zone);
3784 ctl_alloc_io(void *pool_ref)
3788 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3790 io = uma_zalloc(pool->zone, M_WAITOK);
3792 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK);
3795 io->io_hdr.pool = pool_ref;
3800 ctl_alloc_io_nowait(void *pool_ref)
3804 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3806 io = uma_zalloc(pool->zone, M_NOWAIT);
3808 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT);
3811 io->io_hdr.pool = pool_ref;
3816 ctl_free_io(union ctl_io *io)
3819 struct ctl_io_pool *pool;
3826 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3827 uma_zfree(pool->zone, io);
3829 uma_zfree((uma_zone_t)io->io_hdr.pool, io);
3834 ctl_zero_io(union ctl_io *io)
3842 * May need to preserve linked list pointers at some point too.
3844 pool_ref = io->io_hdr.pool;
3845 memset(io, 0, sizeof(*io));
3846 io->io_hdr.pool = pool_ref;
3850 ctl_expand_number(const char *buf, uint64_t *num)
3856 number = strtoq(buf, &endptr, 0);
3858 switch (tolower((unsigned char)*endptr)) {
3878 case '\0': /* No unit. */
3882 /* Unrecognized unit. */
3886 if ((number << shift) >> shift != number) {
3890 *num = number << shift;
3896 * This routine could be used in the future to load default and/or saved
3897 * mode page parameters for a particuar lun.
3900 ctl_init_page_index(struct ctl_lun *lun)
3903 struct ctl_page_index *page_index;
3907 memcpy(&lun->mode_pages.index, page_index_template,
3908 sizeof(page_index_template));
3910 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3912 page_index = &lun->mode_pages.index[i];
3913 if (lun->be_lun->lun_type == T_DIRECT &&
3914 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
3916 if (lun->be_lun->lun_type == T_PROCESSOR &&
3917 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
3919 if (lun->be_lun->lun_type == T_CDROM &&
3920 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
3923 page_code = page_index->page_code & SMPH_PC_MASK;
3924 switch (page_code) {
3925 case SMS_RW_ERROR_RECOVERY_PAGE: {
3926 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
3927 ("subpage %#x for page %#x is incorrect!",
3928 page_index->subpage, page_code));
3929 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
3930 &rw_er_page_default,
3931 sizeof(rw_er_page_default));
3932 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
3933 &rw_er_page_changeable,
3934 sizeof(rw_er_page_changeable));
3935 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
3936 &rw_er_page_default,
3937 sizeof(rw_er_page_default));
3938 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
3939 &rw_er_page_default,
3940 sizeof(rw_er_page_default));
3941 page_index->page_data =
3942 (uint8_t *)lun->mode_pages.rw_er_page;
3945 case SMS_FORMAT_DEVICE_PAGE: {
3946 struct scsi_format_page *format_page;
3948 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
3949 ("subpage %#x for page %#x is incorrect!",
3950 page_index->subpage, page_code));
3953 * Sectors per track are set above. Bytes per
3954 * sector need to be set here on a per-LUN basis.
3956 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
3957 &format_page_default,
3958 sizeof(format_page_default));
3959 memcpy(&lun->mode_pages.format_page[
3960 CTL_PAGE_CHANGEABLE], &format_page_changeable,
3961 sizeof(format_page_changeable));
3962 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
3963 &format_page_default,
3964 sizeof(format_page_default));
3965 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
3966 &format_page_default,
3967 sizeof(format_page_default));
3969 format_page = &lun->mode_pages.format_page[
3971 scsi_ulto2b(lun->be_lun->blocksize,
3972 format_page->bytes_per_sector);
3974 format_page = &lun->mode_pages.format_page[
3976 scsi_ulto2b(lun->be_lun->blocksize,
3977 format_page->bytes_per_sector);
3979 format_page = &lun->mode_pages.format_page[
3981 scsi_ulto2b(lun->be_lun->blocksize,
3982 format_page->bytes_per_sector);
3984 page_index->page_data =
3985 (uint8_t *)lun->mode_pages.format_page;
3988 case SMS_RIGID_DISK_PAGE: {
3989 struct scsi_rigid_disk_page *rigid_disk_page;
3990 uint32_t sectors_per_cylinder;
3994 #endif /* !__XSCALE__ */
3996 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
3997 ("subpage %#x for page %#x is incorrect!",
3998 page_index->subpage, page_code));
4001 * Rotation rate and sectors per track are set
4002 * above. We calculate the cylinders here based on
4003 * capacity. Due to the number of heads and
4004 * sectors per track we're using, smaller arrays
4005 * may turn out to have 0 cylinders. Linux and
4006 * FreeBSD don't pay attention to these mode pages
4007 * to figure out capacity, but Solaris does. It
4008 * seems to deal with 0 cylinders just fine, and
4009 * works out a fake geometry based on the capacity.
4011 memcpy(&lun->mode_pages.rigid_disk_page[
4012 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4013 sizeof(rigid_disk_page_default));
4014 memcpy(&lun->mode_pages.rigid_disk_page[
4015 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4016 sizeof(rigid_disk_page_changeable));
4018 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4022 * The divide method here will be more accurate,
4023 * probably, but results in floating point being
4024 * used in the kernel on i386 (__udivdi3()). On the
4025 * XScale, though, __udivdi3() is implemented in
4028 * The shift method for cylinder calculation is
4029 * accurate if sectors_per_cylinder is a power of
4030 * 2. Otherwise it might be slightly off -- you
4031 * might have a bit of a truncation problem.
4034 cylinders = (lun->be_lun->maxlba + 1) /
4035 sectors_per_cylinder;
4037 for (shift = 31; shift > 0; shift--) {
4038 if (sectors_per_cylinder & (1 << shift))
4041 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4045 * We've basically got 3 bytes, or 24 bits for the
4046 * cylinder size in the mode page. If we're over,
4047 * just round down to 2^24.
4049 if (cylinders > 0xffffff)
4050 cylinders = 0xffffff;
4052 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4054 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4056 if ((value = ctl_get_opt(&lun->be_lun->options,
4058 scsi_ulto2b(strtol(value, NULL, 0),
4059 rigid_disk_page->rotation_rate);
4062 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
4063 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4064 sizeof(rigid_disk_page_default));
4065 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
4066 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4067 sizeof(rigid_disk_page_default));
4069 page_index->page_data =
4070 (uint8_t *)lun->mode_pages.rigid_disk_page;
4073 case SMS_VERIFY_ERROR_RECOVERY_PAGE: {
4074 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4075 ("subpage %#x for page %#x is incorrect!",
4076 page_index->subpage, page_code));
4077 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CURRENT],
4078 &verify_er_page_default,
4079 sizeof(verify_er_page_default));
4080 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CHANGEABLE],
4081 &verify_er_page_changeable,
4082 sizeof(verify_er_page_changeable));
4083 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_DEFAULT],
4084 &verify_er_page_default,
4085 sizeof(verify_er_page_default));
4086 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_SAVED],
4087 &verify_er_page_default,
4088 sizeof(verify_er_page_default));
4089 page_index->page_data =
4090 (uint8_t *)lun->mode_pages.verify_er_page;
4093 case SMS_CACHING_PAGE: {
4094 struct scsi_caching_page *caching_page;
4096 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4097 ("subpage %#x for page %#x is incorrect!",
4098 page_index->subpage, page_code));
4099 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4100 &caching_page_default,
4101 sizeof(caching_page_default));
4102 memcpy(&lun->mode_pages.caching_page[
4103 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4104 sizeof(caching_page_changeable));
4105 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4106 &caching_page_default,
4107 sizeof(caching_page_default));
4108 caching_page = &lun->mode_pages.caching_page[
4110 value = ctl_get_opt(&lun->be_lun->options, "writecache");
4111 if (value != NULL && strcmp(value, "off") == 0)
4112 caching_page->flags1 &= ~SCP_WCE;
4113 value = ctl_get_opt(&lun->be_lun->options, "readcache");
4114 if (value != NULL && strcmp(value, "off") == 0)
4115 caching_page->flags1 |= SCP_RCD;
4116 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4117 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4118 sizeof(caching_page_default));
4119 page_index->page_data =
4120 (uint8_t *)lun->mode_pages.caching_page;
4123 case SMS_CONTROL_MODE_PAGE: {
4124 switch (page_index->subpage) {
4125 case SMS_SUBPAGE_PAGE_0: {
4126 struct scsi_control_page *control_page;
4128 memcpy(&lun->mode_pages.control_page[
4130 &control_page_default,
4131 sizeof(control_page_default));
4132 memcpy(&lun->mode_pages.control_page[
4133 CTL_PAGE_CHANGEABLE],
4134 &control_page_changeable,
4135 sizeof(control_page_changeable));
4136 memcpy(&lun->mode_pages.control_page[
4138 &control_page_default,
4139 sizeof(control_page_default));
4140 control_page = &lun->mode_pages.control_page[
4142 value = ctl_get_opt(&lun->be_lun->options,
4144 if (value != NULL &&
4145 strcmp(value, "unrestricted") == 0) {
4146 control_page->queue_flags &=
4147 ~SCP_QUEUE_ALG_MASK;
4148 control_page->queue_flags |=
4149 SCP_QUEUE_ALG_UNRESTRICTED;
4151 memcpy(&lun->mode_pages.control_page[
4153 &lun->mode_pages.control_page[
4155 sizeof(control_page_default));
4156 page_index->page_data =
4157 (uint8_t *)lun->mode_pages.control_page;
4161 memcpy(&lun->mode_pages.control_ext_page[
4163 &control_ext_page_default,
4164 sizeof(control_ext_page_default));
4165 memcpy(&lun->mode_pages.control_ext_page[
4166 CTL_PAGE_CHANGEABLE],
4167 &control_ext_page_changeable,
4168 sizeof(control_ext_page_changeable));
4169 memcpy(&lun->mode_pages.control_ext_page[
4171 &control_ext_page_default,
4172 sizeof(control_ext_page_default));
4173 memcpy(&lun->mode_pages.control_ext_page[
4175 &lun->mode_pages.control_ext_page[
4177 sizeof(control_ext_page_default));
4178 page_index->page_data =
4179 (uint8_t *)lun->mode_pages.control_ext_page;
4182 panic("subpage %#x for page %#x is incorrect!",
4183 page_index->subpage, page_code);
4187 case SMS_INFO_EXCEPTIONS_PAGE: {
4188 switch (page_index->subpage) {
4189 case SMS_SUBPAGE_PAGE_0:
4190 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4192 sizeof(ie_page_default));
4193 memcpy(&lun->mode_pages.ie_page[
4194 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4195 sizeof(ie_page_changeable));
4196 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4198 sizeof(ie_page_default));
4199 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4201 sizeof(ie_page_default));
4202 page_index->page_data =
4203 (uint8_t *)lun->mode_pages.ie_page;
4206 struct ctl_logical_block_provisioning_page *page;
4208 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4210 sizeof(lbp_page_default));
4211 memcpy(&lun->mode_pages.lbp_page[
4212 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4213 sizeof(lbp_page_changeable));
4214 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4216 sizeof(lbp_page_default));
4217 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4218 value = ctl_get_opt(&lun->be_lun->options,
4220 if (value != NULL &&
4221 ctl_expand_number(value, &ival) == 0) {
4222 page->descr[0].flags |= SLBPPD_ENABLED |
4224 if (lun->be_lun->blocksize)
4225 ival /= lun->be_lun->blocksize;
4228 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4229 page->descr[0].count);
4231 value = ctl_get_opt(&lun->be_lun->options,
4233 if (value != NULL &&
4234 ctl_expand_number(value, &ival) == 0) {
4235 page->descr[1].flags |= SLBPPD_ENABLED |
4237 if (lun->be_lun->blocksize)
4238 ival /= lun->be_lun->blocksize;
4241 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4242 page->descr[1].count);
4244 value = ctl_get_opt(&lun->be_lun->options,
4245 "pool-avail-threshold");
4246 if (value != NULL &&
4247 ctl_expand_number(value, &ival) == 0) {
4248 page->descr[2].flags |= SLBPPD_ENABLED |
4250 if (lun->be_lun->blocksize)
4251 ival /= lun->be_lun->blocksize;
4254 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4255 page->descr[2].count);
4257 value = ctl_get_opt(&lun->be_lun->options,
4258 "pool-used-threshold");
4259 if (value != NULL &&
4260 ctl_expand_number(value, &ival) == 0) {
4261 page->descr[3].flags |= SLBPPD_ENABLED |
4263 if (lun->be_lun->blocksize)
4264 ival /= lun->be_lun->blocksize;
4267 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4268 page->descr[3].count);
4270 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4271 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4272 sizeof(lbp_page_default));
4273 page_index->page_data =
4274 (uint8_t *)lun->mode_pages.lbp_page;
4278 panic("subpage %#x for page %#x is incorrect!",
4279 page_index->subpage, page_code);
4283 case SMS_CDDVD_CAPS_PAGE:{
4284 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4285 ("subpage %#x for page %#x is incorrect!",
4286 page_index->subpage, page_code));
4287 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT],
4288 &cddvd_page_default,
4289 sizeof(cddvd_page_default));
4290 memcpy(&lun->mode_pages.cddvd_page[
4291 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable,
4292 sizeof(cddvd_page_changeable));
4293 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4294 &cddvd_page_default,
4295 sizeof(cddvd_page_default));
4296 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT],
4297 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4298 sizeof(cddvd_page_default));
4299 page_index->page_data =
4300 (uint8_t *)lun->mode_pages.cddvd_page;
4304 panic("invalid page code value %#x", page_code);
4308 return (CTL_RETVAL_COMPLETE);
4312 ctl_init_log_page_index(struct ctl_lun *lun)
4314 struct ctl_page_index *page_index;
4317 memcpy(&lun->log_pages.index, log_page_index_template,
4318 sizeof(log_page_index_template));
4321 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4323 page_index = &lun->log_pages.index[i];
4324 if (lun->be_lun->lun_type == T_DIRECT &&
4325 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4327 if (lun->be_lun->lun_type == T_PROCESSOR &&
4328 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4330 if (lun->be_lun->lun_type == T_CDROM &&
4331 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4334 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4335 lun->backend->lun_attr == NULL)
4338 if (page_index->page_code != prev) {
4339 lun->log_pages.pages_page[j] = page_index->page_code;
4340 prev = page_index->page_code;
4343 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4344 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4347 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4348 lun->log_pages.index[0].page_len = j;
4349 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4350 lun->log_pages.index[1].page_len = k * 2;
4351 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4352 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4353 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page;
4354 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page);
4355 lun->log_pages.index[4].page_data = (uint8_t *)&lun->log_pages.ie_page;
4356 lun->log_pages.index[4].page_len = sizeof(lun->log_pages.ie_page);
4358 return (CTL_RETVAL_COMPLETE);
4362 hex2bin(const char *str, uint8_t *buf, int buf_size)
4367 memset(buf, 0, buf_size);
4368 while (isspace(str[0]))
4370 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4373 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4377 else if (isalpha(c))
4378 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4384 buf[i / 2] |= (c << 4);
4388 return ((i + 1) / 2);
4395 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4396 * wants us to allocate the LUN and he can block.
4397 * - ctl_softc is always set
4398 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4400 * Returns 0 for success, non-zero (errno) for failure.
4403 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4404 struct ctl_be_lun *const be_lun)
4406 struct ctl_lun *nlun, *lun;
4407 struct scsi_vpd_id_descriptor *desc;
4408 struct scsi_vpd_id_t10 *t10id;
4409 const char *eui, *naa, *scsiname, *vendor, *value;
4410 int lun_number, i, lun_malloced;
4411 int devidlen, idlen1, idlen2 = 0, len;
4417 * We currently only support Direct Access or Processor LUN types.
4419 switch (be_lun->lun_type) {
4427 be_lun->lun_config_status(be_lun->be_lun,
4428 CTL_LUN_CONFIG_FAILURE);
4431 if (ctl_lun == NULL) {
4432 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4439 memset(lun, 0, sizeof(*lun));
4441 lun->flags = CTL_LUN_MALLOCED;
4443 /* Generate LUN ID. */
4444 devidlen = max(CTL_DEVID_MIN_LEN,
4445 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4446 idlen1 = sizeof(*t10id) + devidlen;
4447 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4448 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4449 if (scsiname != NULL) {
4450 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4451 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4453 eui = ctl_get_opt(&be_lun->options, "eui");
4455 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4457 naa = ctl_get_opt(&be_lun->options, "naa");
4459 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4461 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4462 M_CTL, M_WAITOK | M_ZERO);
4463 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4464 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4465 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4466 desc->length = idlen1;
4467 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4468 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4469 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4470 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4472 strncpy(t10id->vendor, vendor,
4473 min(sizeof(t10id->vendor), strlen(vendor)));
4475 strncpy((char *)t10id->vendor_spec_id,
4476 (char *)be_lun->device_id, devidlen);
4477 if (scsiname != NULL) {
4478 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4480 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4481 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4482 SVPD_ID_TYPE_SCSI_NAME;
4483 desc->length = idlen2;
4484 strlcpy(desc->identifier, scsiname, idlen2);
4487 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4489 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4490 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4492 desc->length = hex2bin(eui, desc->identifier, 16);
4493 desc->length = desc->length > 12 ? 16 :
4494 (desc->length > 8 ? 12 : 8);
4495 len -= 16 - desc->length;
4498 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4500 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4501 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4503 desc->length = hex2bin(naa, desc->identifier, 16);
4504 desc->length = desc->length > 8 ? 16 : 8;
4505 len -= 16 - desc->length;
4507 lun->lun_devid->len = len;
4509 mtx_lock(&ctl_softc->ctl_lock);
4511 * See if the caller requested a particular LUN number. If so, see
4512 * if it is available. Otherwise, allocate the first available LUN.
4514 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4515 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4516 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4517 mtx_unlock(&ctl_softc->ctl_lock);
4518 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4519 printf("ctl: requested LUN ID %d is higher "
4520 "than CTL_MAX_LUNS - 1 (%d)\n",
4521 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4524 * XXX KDM return an error, or just assign
4525 * another LUN ID in this case??
4527 printf("ctl: requested LUN ID %d is already "
4528 "in use\n", be_lun->req_lun_id);
4530 if (lun->flags & CTL_LUN_MALLOCED)
4532 be_lun->lun_config_status(be_lun->be_lun,
4533 CTL_LUN_CONFIG_FAILURE);
4536 lun_number = be_lun->req_lun_id;
4538 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, CTL_MAX_LUNS);
4539 if (lun_number == -1) {
4540 mtx_unlock(&ctl_softc->ctl_lock);
4541 printf("ctl: can't allocate LUN, out of LUNs\n");
4542 if (lun->flags & CTL_LUN_MALLOCED)
4544 be_lun->lun_config_status(be_lun->be_lun,
4545 CTL_LUN_CONFIG_FAILURE);
4549 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4551 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4552 lun->lun = lun_number;
4553 lun->be_lun = be_lun;
4555 * The processor LUN is always enabled. Disk LUNs come on line
4556 * disabled, and must be enabled by the backend.
4558 lun->flags |= CTL_LUN_DISABLED;
4559 lun->backend = be_lun->be;
4560 be_lun->ctl_lun = lun;
4561 be_lun->lun_id = lun_number;
4562 atomic_add_int(&be_lun->be->num_luns, 1);
4563 if (be_lun->flags & CTL_LUN_FLAG_EJECTED)
4564 lun->flags |= CTL_LUN_EJECTED;
4565 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA)
4566 lun->flags |= CTL_LUN_NO_MEDIA;
4567 if (be_lun->flags & CTL_LUN_FLAG_STOPPED)
4568 lun->flags |= CTL_LUN_STOPPED;
4570 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4571 lun->flags |= CTL_LUN_PRIMARY_SC;
4573 value = ctl_get_opt(&be_lun->options, "removable");
4574 if (value != NULL) {
4575 if (strcmp(value, "on") == 0)
4576 lun->flags |= CTL_LUN_REMOVABLE;
4577 } else if (be_lun->lun_type == T_CDROM)
4578 lun->flags |= CTL_LUN_REMOVABLE;
4580 lun->ctl_softc = ctl_softc;
4582 lun->last_busy = getsbinuptime();
4584 TAILQ_INIT(&lun->ooa_queue);
4585 TAILQ_INIT(&lun->blocked_queue);
4586 STAILQ_INIT(&lun->error_list);
4587 lun->ie_reported = 1;
4588 callout_init_mtx(&lun->ie_callout, &lun->lun_lock, 0);
4589 ctl_tpc_lun_init(lun);
4592 * Initialize the mode and log page index.
4594 ctl_init_page_index(lun);
4595 ctl_init_log_page_index(lun);
4598 * Now, before we insert this lun on the lun list, set the lun
4599 * inventory changed UA for all other luns.
4601 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4602 mtx_lock(&nlun->lun_lock);
4603 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4604 mtx_unlock(&nlun->lun_lock);
4607 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4609 ctl_softc->ctl_luns[lun_number] = lun;
4611 ctl_softc->num_luns++;
4613 /* Setup statistics gathering */
4614 lun->stats.device_type = be_lun->lun_type;
4615 lun->stats.lun_number = lun_number;
4616 lun->stats.blocksize = be_lun->blocksize;
4617 if (be_lun->blocksize == 0)
4618 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4619 for (i = 0;i < CTL_MAX_PORTS;i++)
4620 lun->stats.ports[i].targ_port = i;
4622 mtx_unlock(&ctl_softc->ctl_lock);
4624 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4631 * - LUN has already been marked invalid and any pending I/O has been taken
4635 ctl_free_lun(struct ctl_lun *lun)
4637 struct ctl_softc *softc;
4638 struct ctl_lun *nlun;
4641 softc = lun->ctl_softc;
4643 mtx_assert(&softc->ctl_lock, MA_OWNED);
4645 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4647 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4649 softc->ctl_luns[lun->lun] = NULL;
4651 if (!TAILQ_EMPTY(&lun->ooa_queue))
4652 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4657 * Tell the backend to free resources, if this LUN has a backend.
4659 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4660 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4662 lun->ie_reportcnt = UINT32_MAX;
4663 callout_drain(&lun->ie_callout);
4665 ctl_tpc_lun_shutdown(lun);
4666 mtx_destroy(&lun->lun_lock);
4667 free(lun->lun_devid, M_CTL);
4668 for (i = 0; i < CTL_MAX_PORTS; i++)
4669 free(lun->pending_ua[i], M_CTL);
4670 for (i = 0; i < CTL_MAX_PORTS; i++)
4671 free(lun->pr_keys[i], M_CTL);
4672 free(lun->write_buffer, M_CTL);
4673 if (lun->flags & CTL_LUN_MALLOCED)
4676 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4677 mtx_lock(&nlun->lun_lock);
4678 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4679 mtx_unlock(&nlun->lun_lock);
4686 ctl_create_lun(struct ctl_be_lun *be_lun)
4690 * ctl_alloc_lun() should handle all potential failure cases.
4692 ctl_alloc_lun(control_softc, NULL, be_lun);
4696 ctl_add_lun(struct ctl_be_lun *be_lun)
4698 struct ctl_softc *softc = control_softc;
4700 mtx_lock(&softc->ctl_lock);
4701 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links);
4702 mtx_unlock(&softc->ctl_lock);
4703 wakeup(&softc->pending_lun_queue);
4709 ctl_enable_lun(struct ctl_be_lun *be_lun)
4711 struct ctl_softc *softc;
4712 struct ctl_port *port, *nport;
4713 struct ctl_lun *lun;
4716 lun = (struct ctl_lun *)be_lun->ctl_lun;
4717 softc = lun->ctl_softc;
4719 mtx_lock(&softc->ctl_lock);
4720 mtx_lock(&lun->lun_lock);
4721 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4723 * eh? Why did we get called if the LUN is already
4726 mtx_unlock(&lun->lun_lock);
4727 mtx_unlock(&softc->ctl_lock);
4730 lun->flags &= ~CTL_LUN_DISABLED;
4731 mtx_unlock(&lun->lun_lock);
4733 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) {
4734 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4735 port->lun_map != NULL || port->lun_enable == NULL)
4739 * Drop the lock while we call the FETD's enable routine.
4740 * This can lead to a callback into CTL (at least in the
4741 * case of the internal initiator frontend.
4743 mtx_unlock(&softc->ctl_lock);
4744 retval = port->lun_enable(port->targ_lun_arg, lun->lun);
4745 mtx_lock(&softc->ctl_lock);
4747 printf("%s: FETD %s port %d returned error "
4748 "%d for lun_enable on lun %jd\n",
4749 __func__, port->port_name, port->targ_port,
4750 retval, (intmax_t)lun->lun);
4754 mtx_unlock(&softc->ctl_lock);
4755 ctl_isc_announce_lun(lun);
4761 ctl_disable_lun(struct ctl_be_lun *be_lun)
4763 struct ctl_softc *softc;
4764 struct ctl_port *port;
4765 struct ctl_lun *lun;
4768 lun = (struct ctl_lun *)be_lun->ctl_lun;
4769 softc = lun->ctl_softc;
4771 mtx_lock(&softc->ctl_lock);
4772 mtx_lock(&lun->lun_lock);
4773 if (lun->flags & CTL_LUN_DISABLED) {
4774 mtx_unlock(&lun->lun_lock);
4775 mtx_unlock(&softc->ctl_lock);
4778 lun->flags |= CTL_LUN_DISABLED;
4779 mtx_unlock(&lun->lun_lock);
4781 STAILQ_FOREACH(port, &softc->port_list, links) {
4782 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4783 port->lun_map != NULL || port->lun_disable == NULL)
4787 * Drop the lock before we call the frontend's disable
4788 * routine, to avoid lock order reversals.
4790 * XXX KDM what happens if the frontend list changes while
4791 * we're traversing it? It's unlikely, but should be handled.
4793 mtx_unlock(&softc->ctl_lock);
4794 retval = port->lun_disable(port->targ_lun_arg, lun->lun);
4795 mtx_lock(&softc->ctl_lock);
4797 printf("%s: FETD %s port %d returned error "
4798 "%d for lun_disable on lun %jd\n",
4799 __func__, port->port_name, port->targ_port,
4800 retval, (intmax_t)lun->lun);
4804 mtx_unlock(&softc->ctl_lock);
4805 ctl_isc_announce_lun(lun);
4811 ctl_start_lun(struct ctl_be_lun *be_lun)
4813 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4815 mtx_lock(&lun->lun_lock);
4816 lun->flags &= ~CTL_LUN_STOPPED;
4817 mtx_unlock(&lun->lun_lock);
4822 ctl_stop_lun(struct ctl_be_lun *be_lun)
4824 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4826 mtx_lock(&lun->lun_lock);
4827 lun->flags |= CTL_LUN_STOPPED;
4828 mtx_unlock(&lun->lun_lock);
4833 ctl_lun_no_media(struct ctl_be_lun *be_lun)
4835 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4837 mtx_lock(&lun->lun_lock);
4838 lun->flags |= CTL_LUN_NO_MEDIA;
4839 mtx_unlock(&lun->lun_lock);
4844 ctl_lun_has_media(struct ctl_be_lun *be_lun)
4846 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4847 union ctl_ha_msg msg;
4849 mtx_lock(&lun->lun_lock);
4850 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED);
4851 if (lun->flags & CTL_LUN_REMOVABLE)
4852 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE);
4853 mtx_unlock(&lun->lun_lock);
4854 if ((lun->flags & CTL_LUN_REMOVABLE) &&
4855 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4856 bzero(&msg.ua, sizeof(msg.ua));
4857 msg.hdr.msg_type = CTL_MSG_UA;
4858 msg.hdr.nexus.initid = -1;
4859 msg.hdr.nexus.targ_port = -1;
4860 msg.hdr.nexus.targ_lun = lun->lun;
4861 msg.hdr.nexus.targ_mapped_lun = lun->lun;
4864 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE;
4865 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4872 ctl_lun_ejected(struct ctl_be_lun *be_lun)
4874 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4876 mtx_lock(&lun->lun_lock);
4877 lun->flags |= CTL_LUN_EJECTED;
4878 mtx_unlock(&lun->lun_lock);
4883 ctl_lun_primary(struct ctl_be_lun *be_lun)
4885 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4887 mtx_lock(&lun->lun_lock);
4888 lun->flags |= CTL_LUN_PRIMARY_SC;
4889 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4890 mtx_unlock(&lun->lun_lock);
4891 ctl_isc_announce_lun(lun);
4896 ctl_lun_secondary(struct ctl_be_lun *be_lun)
4898 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4900 mtx_lock(&lun->lun_lock);
4901 lun->flags &= ~CTL_LUN_PRIMARY_SC;
4902 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4903 mtx_unlock(&lun->lun_lock);
4904 ctl_isc_announce_lun(lun);
4909 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4911 struct ctl_softc *softc;
4912 struct ctl_lun *lun;
4914 lun = (struct ctl_lun *)be_lun->ctl_lun;
4915 softc = lun->ctl_softc;
4917 mtx_lock(&lun->lun_lock);
4920 * The LUN needs to be disabled before it can be marked invalid.
4922 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4923 mtx_unlock(&lun->lun_lock);
4927 * Mark the LUN invalid.
4929 lun->flags |= CTL_LUN_INVALID;
4932 * If there is nothing in the OOA queue, go ahead and free the LUN.
4933 * If we have something in the OOA queue, we'll free it when the
4934 * last I/O completes.
4936 if (TAILQ_EMPTY(&lun->ooa_queue)) {
4937 mtx_unlock(&lun->lun_lock);
4938 mtx_lock(&softc->ctl_lock);
4940 mtx_unlock(&softc->ctl_lock);
4942 mtx_unlock(&lun->lun_lock);
4948 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
4950 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4951 union ctl_ha_msg msg;
4953 mtx_lock(&lun->lun_lock);
4954 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE);
4955 mtx_unlock(&lun->lun_lock);
4956 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4957 /* Send msg to other side. */
4958 bzero(&msg.ua, sizeof(msg.ua));
4959 msg.hdr.msg_type = CTL_MSG_UA;
4960 msg.hdr.nexus.initid = -1;
4961 msg.hdr.nexus.targ_port = -1;
4962 msg.hdr.nexus.targ_lun = lun->lun;
4963 msg.hdr.nexus.targ_mapped_lun = lun->lun;
4966 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE;
4967 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4973 * Backend "memory move is complete" callback for requests that never
4974 * make it down to say RAIDCore's configuration code.
4977 ctl_config_move_done(union ctl_io *io)
4981 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
4982 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
4983 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
4985 if ((io->io_hdr.port_status != 0) &&
4986 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
4987 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
4989 * For hardware error sense keys, the sense key
4990 * specific value is defined to be a retry count,
4991 * but we use it to pass back an internal FETD
4992 * error code. XXX KDM Hopefully the FETD is only
4993 * using 16 bits for an error code, since that's
4994 * all the space we have in the sks field.
4996 ctl_set_internal_failure(&io->scsiio,
4999 io->io_hdr.port_status);
5002 if (ctl_debug & CTL_DEBUG_CDB_DATA)
5004 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
5005 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5006 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
5007 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5009 * XXX KDM just assuming a single pointer here, and not a
5010 * S/G list. If we start using S/G lists for config data,
5011 * we'll need to know how to clean them up here as well.
5013 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5014 free(io->scsiio.kern_data_ptr, M_CTL);
5016 retval = CTL_RETVAL_COMPLETE;
5019 * XXX KDM now we need to continue data movement. Some
5021 * - call ctl_scsiio() again? We don't do this for data
5022 * writes, because for those at least we know ahead of
5023 * time where the write will go and how long it is. For
5024 * config writes, though, that information is largely
5025 * contained within the write itself, thus we need to
5026 * parse out the data again.
5028 * - Call some other function once the data is in?
5032 * XXX KDM call ctl_scsiio() again for now, and check flag
5033 * bits to see whether we're allocated or not.
5035 retval = ctl_scsiio(&io->scsiio);
5041 * This gets called by a backend driver when it is done with a
5042 * data_submit method.
5045 ctl_data_submit_done(union ctl_io *io)
5048 * If the IO_CONT flag is set, we need to call the supplied
5049 * function to continue processing the I/O, instead of completing
5052 * If there is an error, though, we don't want to keep processing.
5053 * Instead, just send status back to the initiator.
5055 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5056 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5057 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5058 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5059 io->scsiio.io_cont(io);
5066 * This gets called by a backend driver when it is done with a
5067 * configuration write.
5070 ctl_config_write_done(union ctl_io *io)
5075 * If the IO_CONT flag is set, we need to call the supplied
5076 * function to continue processing the I/O, instead of completing
5079 * If there is an error, though, we don't want to keep processing.
5080 * Instead, just send status back to the initiator.
5082 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5083 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5084 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5085 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5086 io->scsiio.io_cont(io);
5090 * Since a configuration write can be done for commands that actually
5091 * have data allocated, like write buffer, and commands that have
5092 * no data, like start/stop unit, we need to check here.
5094 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5095 buf = io->scsiio.kern_data_ptr;
5104 ctl_config_read_done(union ctl_io *io)
5109 * If there is some error -- we are done, skip data transfer.
5111 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
5112 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5113 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
5114 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5115 buf = io->scsiio.kern_data_ptr;
5125 * If the IO_CONT flag is set, we need to call the supplied
5126 * function to continue processing the I/O, instead of completing
5129 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
5130 io->scsiio.io_cont(io);
5138 * SCSI release command.
5141 ctl_scsi_release(struct ctl_scsiio *ctsio)
5143 struct ctl_lun *lun;
5146 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5148 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5149 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5152 * XXX KDM right now, we only support LUN reservation. We don't
5153 * support 3rd party reservations, or extent reservations, which
5154 * might actually need the parameter list. If we've gotten this
5155 * far, we've got a LUN reservation. Anything else got kicked out
5156 * above. So, according to SPC, ignore the length.
5159 mtx_lock(&lun->lun_lock);
5162 * According to SPC, it is not an error for an intiator to attempt
5163 * to release a reservation on a LUN that isn't reserved, or that
5164 * is reserved by another initiator. The reservation can only be
5165 * released, though, by the initiator who made it or by one of
5166 * several reset type events.
5168 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5169 lun->flags &= ~CTL_LUN_RESERVED;
5171 mtx_unlock(&lun->lun_lock);
5173 ctl_set_success(ctsio);
5174 ctl_done((union ctl_io *)ctsio);
5175 return (CTL_RETVAL_COMPLETE);
5179 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5181 struct ctl_lun *lun;
5184 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5186 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5187 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5190 * XXX KDM right now, we only support LUN reservation. We don't
5191 * support 3rd party reservations, or extent reservations, which
5192 * might actually need the parameter list. If we've gotten this
5193 * far, we've got a LUN reservation. Anything else got kicked out
5194 * above. So, according to SPC, ignore the length.
5197 mtx_lock(&lun->lun_lock);
5198 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5199 ctl_set_reservation_conflict(ctsio);
5203 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */
5204 if (lun->flags & CTL_LUN_PR_RESERVED) {
5205 ctl_set_success(ctsio);
5209 lun->flags |= CTL_LUN_RESERVED;
5210 lun->res_idx = residx;
5211 ctl_set_success(ctsio);
5214 mtx_unlock(&lun->lun_lock);
5215 ctl_done((union ctl_io *)ctsio);
5216 return (CTL_RETVAL_COMPLETE);
5220 ctl_start_stop(struct ctl_scsiio *ctsio)
5222 struct scsi_start_stop_unit *cdb;
5223 struct ctl_lun *lun;
5226 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5228 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5229 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5231 if ((cdb->how & SSS_PC_MASK) == 0) {
5232 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
5233 (cdb->how & SSS_START) == 0) {
5236 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5237 if (ctl_get_prkey(lun, residx) == 0 ||
5238 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) {
5240 ctl_set_reservation_conflict(ctsio);
5241 ctl_done((union ctl_io *)ctsio);
5242 return (CTL_RETVAL_COMPLETE);
5246 if ((cdb->how & SSS_LOEJ) &&
5247 (lun->flags & CTL_LUN_REMOVABLE) == 0) {
5248 ctl_set_invalid_field(ctsio,
5254 ctl_done((union ctl_io *)ctsio);
5255 return (CTL_RETVAL_COMPLETE);
5258 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) &&
5259 lun->prevent_count > 0) {
5260 /* "Medium removal prevented" */
5261 ctl_set_sense(ctsio, /*current_error*/ 1,
5262 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ?
5263 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST,
5264 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE);
5265 ctl_done((union ctl_io *)ctsio);
5266 return (CTL_RETVAL_COMPLETE);
5270 retval = lun->backend->config_write((union ctl_io *)ctsio);
5275 ctl_prevent_allow(struct ctl_scsiio *ctsio)
5277 struct ctl_lun *lun;
5278 struct scsi_prevent *cdb;
5282 CTL_DEBUG_PRINT(("ctl_prevent_allow\n"));
5284 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5285 cdb = (struct scsi_prevent *)ctsio->cdb;
5287 if ((lun->flags & CTL_LUN_REMOVABLE) == 0) {
5288 ctl_set_invalid_opcode(ctsio);
5289 ctl_done((union ctl_io *)ctsio);
5290 return (CTL_RETVAL_COMPLETE);
5293 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5294 mtx_lock(&lun->lun_lock);
5295 if ((cdb->how & PR_PREVENT) &&
5296 ctl_is_set(lun->prevent, initidx) == 0) {
5297 ctl_set_mask(lun->prevent, initidx);
5298 lun->prevent_count++;
5299 } else if ((cdb->how & PR_PREVENT) == 0 &&
5300 ctl_is_set(lun->prevent, initidx)) {
5301 ctl_clear_mask(lun->prevent, initidx);
5302 lun->prevent_count--;
5304 mtx_unlock(&lun->lun_lock);
5305 retval = lun->backend->config_write((union ctl_io *)ctsio);
5310 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5311 * we don't really do anything with the LBA and length fields if the user
5312 * passes them in. Instead we'll just flush out the cache for the entire
5316 ctl_sync_cache(struct ctl_scsiio *ctsio)
5318 struct ctl_lun *lun;
5319 struct ctl_softc *softc;
5320 struct ctl_lba_len_flags *lbalen;
5321 uint64_t starting_lba;
5322 uint32_t block_count;
5326 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5328 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5329 softc = lun->ctl_softc;
5332 switch (ctsio->cdb[0]) {
5333 case SYNCHRONIZE_CACHE: {
5334 struct scsi_sync_cache *cdb;
5335 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5337 starting_lba = scsi_4btoul(cdb->begin_lba);
5338 block_count = scsi_2btoul(cdb->lb_count);
5342 case SYNCHRONIZE_CACHE_16: {
5343 struct scsi_sync_cache_16 *cdb;
5344 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5346 starting_lba = scsi_8btou64(cdb->begin_lba);
5347 block_count = scsi_4btoul(cdb->lb_count);
5352 ctl_set_invalid_opcode(ctsio);
5353 ctl_done((union ctl_io *)ctsio);
5355 break; /* NOTREACHED */
5359 * We check the LBA and length, but don't do anything with them.
5360 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5361 * get flushed. This check will just help satisfy anyone who wants
5362 * to see an error for an out of range LBA.
5364 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5365 ctl_set_lba_out_of_range(ctsio);
5366 ctl_done((union ctl_io *)ctsio);
5370 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5371 lbalen->lba = starting_lba;
5372 lbalen->len = block_count;
5373 lbalen->flags = byte2;
5374 retval = lun->backend->config_write((union ctl_io *)ctsio);
5381 ctl_format(struct ctl_scsiio *ctsio)
5383 struct scsi_format *cdb;
5384 struct ctl_lun *lun;
5385 int length, defect_list_len;
5387 CTL_DEBUG_PRINT(("ctl_format\n"));
5389 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5391 cdb = (struct scsi_format *)ctsio->cdb;
5394 if (cdb->byte2 & SF_FMTDATA) {
5395 if (cdb->byte2 & SF_LONGLIST)
5396 length = sizeof(struct scsi_format_header_long);
5398 length = sizeof(struct scsi_format_header_short);
5401 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5403 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5404 ctsio->kern_data_len = length;
5405 ctsio->kern_total_len = length;
5406 ctsio->kern_data_resid = 0;
5407 ctsio->kern_rel_offset = 0;
5408 ctsio->kern_sg_entries = 0;
5409 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5410 ctsio->be_move_done = ctl_config_move_done;
5411 ctl_datamove((union ctl_io *)ctsio);
5413 return (CTL_RETVAL_COMPLETE);
5416 defect_list_len = 0;
5418 if (cdb->byte2 & SF_FMTDATA) {
5419 if (cdb->byte2 & SF_LONGLIST) {
5420 struct scsi_format_header_long *header;
5422 header = (struct scsi_format_header_long *)
5423 ctsio->kern_data_ptr;
5425 defect_list_len = scsi_4btoul(header->defect_list_len);
5426 if (defect_list_len != 0) {
5427 ctl_set_invalid_field(ctsio,
5436 struct scsi_format_header_short *header;
5438 header = (struct scsi_format_header_short *)
5439 ctsio->kern_data_ptr;
5441 defect_list_len = scsi_2btoul(header->defect_list_len);
5442 if (defect_list_len != 0) {
5443 ctl_set_invalid_field(ctsio,
5454 ctl_set_success(ctsio);
5457 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5458 free(ctsio->kern_data_ptr, M_CTL);
5459 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5462 ctl_done((union ctl_io *)ctsio);
5463 return (CTL_RETVAL_COMPLETE);
5467 ctl_read_buffer(struct ctl_scsiio *ctsio)
5469 struct ctl_lun *lun;
5470 uint64_t buffer_offset;
5473 static uint8_t descr[4];
5474 static uint8_t echo_descr[4] = { 0 };
5476 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5477 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5478 switch (ctsio->cdb[0]) {
5480 struct scsi_read_buffer *cdb;
5482 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5483 buffer_offset = scsi_3btoul(cdb->offset);
5484 len = scsi_3btoul(cdb->length);
5488 case READ_BUFFER_16: {
5489 struct scsi_read_buffer_16 *cdb;
5491 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb;
5492 buffer_offset = scsi_8btou64(cdb->offset);
5493 len = scsi_4btoul(cdb->length);
5497 default: /* This shouldn't happen. */
5498 ctl_set_invalid_opcode(ctsio);
5499 ctl_done((union ctl_io *)ctsio);
5500 return (CTL_RETVAL_COMPLETE);
5503 if ((byte2 & RWB_MODE) != RWB_MODE_DATA &&
5504 (byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR &&
5505 (byte2 & RWB_MODE) != RWB_MODE_DESCR) {
5506 ctl_set_invalid_field(ctsio,
5512 ctl_done((union ctl_io *)ctsio);
5513 return (CTL_RETVAL_COMPLETE);
5516 if (buffer_offset > CTL_WRITE_BUFFER_SIZE ||
5517 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5518 ctl_set_invalid_field(ctsio,
5524 ctl_done((union ctl_io *)ctsio);
5525 return (CTL_RETVAL_COMPLETE);
5528 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5530 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5531 ctsio->kern_data_ptr = descr;
5532 len = min(len, sizeof(descr));
5533 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5534 ctsio->kern_data_ptr = echo_descr;
5535 len = min(len, sizeof(echo_descr));
5537 if (lun->write_buffer == NULL) {
5538 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5541 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5543 ctsio->kern_data_len = len;
5544 ctsio->kern_total_len = len;
5545 ctsio->kern_data_resid = 0;
5546 ctsio->kern_rel_offset = 0;
5547 ctsio->kern_sg_entries = 0;
5548 ctl_set_success(ctsio);
5549 ctsio->be_move_done = ctl_config_move_done;
5550 ctl_datamove((union ctl_io *)ctsio);
5551 return (CTL_RETVAL_COMPLETE);
5555 ctl_write_buffer(struct ctl_scsiio *ctsio)
5557 struct scsi_write_buffer *cdb;
5558 struct ctl_lun *lun;
5559 int buffer_offset, len;
5561 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5563 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5564 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5566 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5567 ctl_set_invalid_field(ctsio,
5573 ctl_done((union ctl_io *)ctsio);
5574 return (CTL_RETVAL_COMPLETE);
5577 len = scsi_3btoul(cdb->length);
5578 buffer_offset = scsi_3btoul(cdb->offset);
5580 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5581 ctl_set_invalid_field(ctsio,
5587 ctl_done((union ctl_io *)ctsio);
5588 return (CTL_RETVAL_COMPLETE);
5592 * If we've got a kernel request that hasn't been malloced yet,
5593 * malloc it and tell the caller the data buffer is here.
5595 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5596 if (lun->write_buffer == NULL) {
5597 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5600 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5601 ctsio->kern_data_len = len;
5602 ctsio->kern_total_len = len;
5603 ctsio->kern_data_resid = 0;
5604 ctsio->kern_rel_offset = 0;
5605 ctsio->kern_sg_entries = 0;
5606 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5607 ctsio->be_move_done = ctl_config_move_done;
5608 ctl_datamove((union ctl_io *)ctsio);
5610 return (CTL_RETVAL_COMPLETE);
5613 ctl_set_success(ctsio);
5614 ctl_done((union ctl_io *)ctsio);
5615 return (CTL_RETVAL_COMPLETE);
5619 ctl_write_same(struct ctl_scsiio *ctsio)
5621 struct ctl_lun *lun;
5622 struct ctl_lba_len_flags *lbalen;
5624 uint32_t num_blocks;
5628 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5630 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5632 switch (ctsio->cdb[0]) {
5633 case WRITE_SAME_10: {
5634 struct scsi_write_same_10 *cdb;
5636 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5638 lba = scsi_4btoul(cdb->addr);
5639 num_blocks = scsi_2btoul(cdb->length);
5643 case WRITE_SAME_16: {
5644 struct scsi_write_same_16 *cdb;
5646 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5648 lba = scsi_8btou64(cdb->addr);
5649 num_blocks = scsi_4btoul(cdb->length);
5655 * We got a command we don't support. This shouldn't
5656 * happen, commands should be filtered out above us.
5658 ctl_set_invalid_opcode(ctsio);
5659 ctl_done((union ctl_io *)ctsio);
5661 return (CTL_RETVAL_COMPLETE);
5662 break; /* NOTREACHED */
5665 /* ANCHOR flag can be used only together with UNMAP */
5666 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) {
5667 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5668 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5669 ctl_done((union ctl_io *)ctsio);
5670 return (CTL_RETVAL_COMPLETE);
5674 * The first check is to make sure we're in bounds, the second
5675 * check is to catch wrap-around problems. If the lba + num blocks
5676 * is less than the lba, then we've wrapped around and the block
5677 * range is invalid anyway.
5679 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5680 || ((lba + num_blocks) < lba)) {
5681 ctl_set_lba_out_of_range(ctsio);
5682 ctl_done((union ctl_io *)ctsio);
5683 return (CTL_RETVAL_COMPLETE);
5686 /* Zero number of blocks means "to the last logical block" */
5687 if (num_blocks == 0) {
5688 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5689 ctl_set_invalid_field(ctsio,
5695 ctl_done((union ctl_io *)ctsio);
5696 return (CTL_RETVAL_COMPLETE);
5698 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5701 len = lun->be_lun->blocksize;
5704 * If we've got a kernel request that hasn't been malloced yet,
5705 * malloc it and tell the caller the data buffer is here.
5707 if ((byte2 & SWS_NDOB) == 0 &&
5708 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5709 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5710 ctsio->kern_data_len = len;
5711 ctsio->kern_total_len = len;
5712 ctsio->kern_data_resid = 0;
5713 ctsio->kern_rel_offset = 0;
5714 ctsio->kern_sg_entries = 0;
5715 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5716 ctsio->be_move_done = ctl_config_move_done;
5717 ctl_datamove((union ctl_io *)ctsio);
5719 return (CTL_RETVAL_COMPLETE);
5722 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5724 lbalen->len = num_blocks;
5725 lbalen->flags = byte2;
5726 retval = lun->backend->config_write((union ctl_io *)ctsio);
5732 ctl_unmap(struct ctl_scsiio *ctsio)
5734 struct ctl_lun *lun;
5735 struct scsi_unmap *cdb;
5736 struct ctl_ptr_len_flags *ptrlen;
5737 struct scsi_unmap_header *hdr;
5738 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5740 uint32_t num_blocks;
5744 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5746 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5747 cdb = (struct scsi_unmap *)ctsio->cdb;
5749 len = scsi_2btoul(cdb->length);
5753 * If we've got a kernel request that hasn't been malloced yet,
5754 * malloc it and tell the caller the data buffer is here.
5756 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5757 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5758 ctsio->kern_data_len = len;
5759 ctsio->kern_total_len = len;
5760 ctsio->kern_data_resid = 0;
5761 ctsio->kern_rel_offset = 0;
5762 ctsio->kern_sg_entries = 0;
5763 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5764 ctsio->be_move_done = ctl_config_move_done;
5765 ctl_datamove((union ctl_io *)ctsio);
5767 return (CTL_RETVAL_COMPLETE);
5770 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5771 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5772 if (len < sizeof (*hdr) ||
5773 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5774 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5775 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5776 ctl_set_invalid_field(ctsio,
5784 len = scsi_2btoul(hdr->desc_length);
5785 buf = (struct scsi_unmap_desc *)(hdr + 1);
5786 end = buf + len / sizeof(*buf);
5789 for (range = buf; range < end; range++) {
5790 lba = scsi_8btou64(range->lba);
5791 num_blocks = scsi_4btoul(range->length);
5792 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5793 || ((lba + num_blocks) < lba)) {
5794 ctl_set_lba_out_of_range(ctsio);
5795 ctl_done((union ctl_io *)ctsio);
5796 return (CTL_RETVAL_COMPLETE);
5798 if (num_blocks != 0)
5803 * Block backend can not handle zero last range.
5804 * Filter it out and return if there is nothing left.
5806 len = (uint8_t *)endnz - (uint8_t *)buf;
5808 ctl_set_success(ctsio);
5812 mtx_lock(&lun->lun_lock);
5813 ptrlen = (struct ctl_ptr_len_flags *)
5814 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5815 ptrlen->ptr = (void *)buf;
5817 ptrlen->flags = byte2;
5818 ctl_check_blocked(lun);
5819 mtx_unlock(&lun->lun_lock);
5821 retval = lun->backend->config_write((union ctl_io *)ctsio);
5825 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5826 free(ctsio->kern_data_ptr, M_CTL);
5827 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5829 ctl_done((union ctl_io *)ctsio);
5830 return (CTL_RETVAL_COMPLETE);
5834 ctl_default_page_handler(struct ctl_scsiio *ctsio,
5835 struct ctl_page_index *page_index, uint8_t *page_ptr)
5837 struct ctl_lun *lun;
5838 uint8_t *current_cp, *saved_cp;
5842 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5843 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5846 current_cp = (page_index->page_data + (page_index->page_len *
5848 saved_cp = (page_index->page_data + (page_index->page_len *
5851 mtx_lock(&lun->lun_lock);
5852 if (memcmp(current_cp, page_ptr, page_index->page_len)) {
5853 memcpy(current_cp, page_ptr, page_index->page_len);
5854 memcpy(saved_cp, page_ptr, page_index->page_len);
5858 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5859 mtx_unlock(&lun->lun_lock);
5861 ctl_isc_announce_mode(lun,
5862 ctl_get_initindex(&ctsio->io_hdr.nexus),
5863 page_index->page_code, page_index->subpage);
5865 return (CTL_RETVAL_COMPLETE);
5869 ctl_ie_timer(void *arg)
5871 struct ctl_lun *lun = arg;
5874 if (lun->ie_asc == 0)
5877 if (lun->MODE_IE.mrie == SIEP_MRIE_UA)
5878 ctl_est_ua_all(lun, -1, CTL_UA_IE);
5880 lun->ie_reported = 0;
5882 if (lun->ie_reportcnt < scsi_4btoul(lun->MODE_IE.report_count)) {
5883 lun->ie_reportcnt++;
5884 t = scsi_4btoul(lun->MODE_IE.interval_timer);
5885 if (t == 0 || t == UINT32_MAX)
5886 t = 3000; /* 5 min */
5887 callout_schedule(&lun->ie_callout, t * hz / 10);
5892 ctl_ie_page_handler(struct ctl_scsiio *ctsio,
5893 struct ctl_page_index *page_index, uint8_t *page_ptr)
5895 struct scsi_info_exceptions_page *pg;
5896 struct ctl_lun *lun;
5899 (void)ctl_default_page_handler(ctsio, page_index, page_ptr);
5901 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5902 pg = (struct scsi_info_exceptions_page *)page_ptr;
5903 mtx_lock(&lun->lun_lock);
5904 if (pg->info_flags & SIEP_FLAGS_TEST) {
5906 lun->ie_ascq = 0xff;
5907 if (pg->mrie == SIEP_MRIE_UA) {
5908 ctl_est_ua_all(lun, -1, CTL_UA_IE);
5909 lun->ie_reported = 1;
5911 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
5912 lun->ie_reported = -1;
5914 lun->ie_reportcnt = 1;
5915 if (lun->ie_reportcnt < scsi_4btoul(pg->report_count)) {
5916 lun->ie_reportcnt++;
5917 t = scsi_4btoul(pg->interval_timer);
5918 if (t == 0 || t == UINT32_MAX)
5919 t = 3000; /* 5 min */
5920 callout_reset(&lun->ie_callout, t * hz / 10,
5926 lun->ie_reported = 1;
5927 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
5928 lun->ie_reportcnt = UINT32_MAX;
5929 callout_stop(&lun->ie_callout);
5931 mtx_unlock(&lun->lun_lock);
5932 return (CTL_RETVAL_COMPLETE);
5936 ctl_do_mode_select(union ctl_io *io)
5938 struct scsi_mode_page_header *page_header;
5939 struct ctl_page_index *page_index;
5940 struct ctl_scsiio *ctsio;
5941 int page_len, page_len_offset, page_len_size;
5942 union ctl_modepage_info *modepage_info;
5943 struct ctl_lun *lun;
5944 int *len_left, *len_used;
5947 ctsio = &io->scsiio;
5950 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5952 modepage_info = (union ctl_modepage_info *)
5953 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
5954 len_left = &modepage_info->header.len_left;
5955 len_used = &modepage_info->header.len_used;
5959 page_header = (struct scsi_mode_page_header *)
5960 (ctsio->kern_data_ptr + *len_used);
5962 if (*len_left == 0) {
5963 free(ctsio->kern_data_ptr, M_CTL);
5964 ctl_set_success(ctsio);
5965 ctl_done((union ctl_io *)ctsio);
5966 return (CTL_RETVAL_COMPLETE);
5967 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
5969 free(ctsio->kern_data_ptr, M_CTL);
5970 ctl_set_param_len_error(ctsio);
5971 ctl_done((union ctl_io *)ctsio);
5972 return (CTL_RETVAL_COMPLETE);
5974 } else if ((page_header->page_code & SMPH_SPF)
5975 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
5977 free(ctsio->kern_data_ptr, M_CTL);
5978 ctl_set_param_len_error(ctsio);
5979 ctl_done((union ctl_io *)ctsio);
5980 return (CTL_RETVAL_COMPLETE);
5985 * XXX KDM should we do something with the block descriptor?
5987 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
5988 page_index = &lun->mode_pages.index[i];
5989 if (lun->be_lun->lun_type == T_DIRECT &&
5990 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
5992 if (lun->be_lun->lun_type == T_PROCESSOR &&
5993 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
5995 if (lun->be_lun->lun_type == T_CDROM &&
5996 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
5999 if ((page_index->page_code & SMPH_PC_MASK) !=
6000 (page_header->page_code & SMPH_PC_MASK))
6004 * If neither page has a subpage code, then we've got a
6007 if (((page_index->page_code & SMPH_SPF) == 0)
6008 && ((page_header->page_code & SMPH_SPF) == 0)) {
6009 page_len = page_header->page_length;
6014 * If both pages have subpages, then the subpage numbers
6017 if ((page_index->page_code & SMPH_SPF)
6018 && (page_header->page_code & SMPH_SPF)) {
6019 struct scsi_mode_page_header_sp *sph;
6021 sph = (struct scsi_mode_page_header_sp *)page_header;
6022 if (page_index->subpage == sph->subpage) {
6023 page_len = scsi_2btoul(sph->page_length);
6030 * If we couldn't find the page, or if we don't have a mode select
6031 * handler for it, send back an error to the user.
6033 if ((i >= CTL_NUM_MODE_PAGES)
6034 || (page_index->select_handler == NULL)) {
6035 ctl_set_invalid_field(ctsio,
6038 /*field*/ *len_used,
6041 free(ctsio->kern_data_ptr, M_CTL);
6042 ctl_done((union ctl_io *)ctsio);
6043 return (CTL_RETVAL_COMPLETE);
6046 if (page_index->page_code & SMPH_SPF) {
6047 page_len_offset = 2;
6051 page_len_offset = 1;
6055 * If the length the initiator gives us isn't the one we specify in
6056 * the mode page header, or if they didn't specify enough data in
6057 * the CDB to avoid truncating this page, kick out the request.
6059 if ((page_len != (page_index->page_len - page_len_offset -
6061 || (*len_left < page_index->page_len)) {
6064 ctl_set_invalid_field(ctsio,
6067 /*field*/ *len_used + page_len_offset,
6070 free(ctsio->kern_data_ptr, M_CTL);
6071 ctl_done((union ctl_io *)ctsio);
6072 return (CTL_RETVAL_COMPLETE);
6076 * Run through the mode page, checking to make sure that the bits
6077 * the user changed are actually legal for him to change.
6079 for (i = 0; i < page_index->page_len; i++) {
6080 uint8_t *user_byte, *change_mask, *current_byte;
6084 user_byte = (uint8_t *)page_header + i;
6085 change_mask = page_index->page_data +
6086 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6087 current_byte = page_index->page_data +
6088 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6091 * Check to see whether the user set any bits in this byte
6092 * that he is not allowed to set.
6094 if ((*user_byte & ~(*change_mask)) ==
6095 (*current_byte & ~(*change_mask)))
6099 * Go through bit by bit to determine which one is illegal.
6102 for (j = 7; j >= 0; j--) {
6103 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6104 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6109 ctl_set_invalid_field(ctsio,
6112 /*field*/ *len_used + i,
6115 free(ctsio->kern_data_ptr, M_CTL);
6116 ctl_done((union ctl_io *)ctsio);
6117 return (CTL_RETVAL_COMPLETE);
6121 * Decrement these before we call the page handler, since we may
6122 * end up getting called back one way or another before the handler
6123 * returns to this context.
6125 *len_left -= page_index->page_len;
6126 *len_used += page_index->page_len;
6128 retval = page_index->select_handler(ctsio, page_index,
6129 (uint8_t *)page_header);
6132 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6133 * wait until this queued command completes to finish processing
6134 * the mode page. If it returns anything other than
6135 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6136 * already set the sense information, freed the data pointer, and
6137 * completed the io for us.
6139 if (retval != CTL_RETVAL_COMPLETE)
6140 goto bailout_no_done;
6143 * If the initiator sent us more than one page, parse the next one.
6148 ctl_set_success(ctsio);
6149 free(ctsio->kern_data_ptr, M_CTL);
6150 ctl_done((union ctl_io *)ctsio);
6154 return (CTL_RETVAL_COMPLETE);
6159 ctl_mode_select(struct ctl_scsiio *ctsio)
6161 int param_len, pf, sp;
6162 int header_size, bd_len;
6163 union ctl_modepage_info *modepage_info;
6165 switch (ctsio->cdb[0]) {
6166 case MODE_SELECT_6: {
6167 struct scsi_mode_select_6 *cdb;
6169 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6171 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6172 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6173 param_len = cdb->length;
6174 header_size = sizeof(struct scsi_mode_header_6);
6177 case MODE_SELECT_10: {
6178 struct scsi_mode_select_10 *cdb;
6180 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6182 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6183 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6184 param_len = scsi_2btoul(cdb->length);
6185 header_size = sizeof(struct scsi_mode_header_10);
6189 ctl_set_invalid_opcode(ctsio);
6190 ctl_done((union ctl_io *)ctsio);
6191 return (CTL_RETVAL_COMPLETE);
6196 * "A parameter list length of zero indicates that the Data-Out Buffer
6197 * shall be empty. This condition shall not be considered as an error."
6199 if (param_len == 0) {
6200 ctl_set_success(ctsio);
6201 ctl_done((union ctl_io *)ctsio);
6202 return (CTL_RETVAL_COMPLETE);
6206 * Since we'll hit this the first time through, prior to
6207 * allocation, we don't need to free a data buffer here.
6209 if (param_len < header_size) {
6210 ctl_set_param_len_error(ctsio);
6211 ctl_done((union ctl_io *)ctsio);
6212 return (CTL_RETVAL_COMPLETE);
6216 * Allocate the data buffer and grab the user's data. In theory,
6217 * we shouldn't have to sanity check the parameter list length here
6218 * because the maximum size is 64K. We should be able to malloc
6219 * that much without too many problems.
6221 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6222 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6223 ctsio->kern_data_len = param_len;
6224 ctsio->kern_total_len = param_len;
6225 ctsio->kern_data_resid = 0;
6226 ctsio->kern_rel_offset = 0;
6227 ctsio->kern_sg_entries = 0;
6228 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6229 ctsio->be_move_done = ctl_config_move_done;
6230 ctl_datamove((union ctl_io *)ctsio);
6232 return (CTL_RETVAL_COMPLETE);
6235 switch (ctsio->cdb[0]) {
6236 case MODE_SELECT_6: {
6237 struct scsi_mode_header_6 *mh6;
6239 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6240 bd_len = mh6->blk_desc_len;
6243 case MODE_SELECT_10: {
6244 struct scsi_mode_header_10 *mh10;
6246 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6247 bd_len = scsi_2btoul(mh10->blk_desc_len);
6251 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6254 if (param_len < (header_size + bd_len)) {
6255 free(ctsio->kern_data_ptr, M_CTL);
6256 ctl_set_param_len_error(ctsio);
6257 ctl_done((union ctl_io *)ctsio);
6258 return (CTL_RETVAL_COMPLETE);
6262 * Set the IO_CONT flag, so that if this I/O gets passed to
6263 * ctl_config_write_done(), it'll get passed back to
6264 * ctl_do_mode_select() for further processing, or completion if
6267 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6268 ctsio->io_cont = ctl_do_mode_select;
6270 modepage_info = (union ctl_modepage_info *)
6271 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6272 memset(modepage_info, 0, sizeof(*modepage_info));
6273 modepage_info->header.len_left = param_len - header_size - bd_len;
6274 modepage_info->header.len_used = header_size + bd_len;
6276 return (ctl_do_mode_select((union ctl_io *)ctsio));
6280 ctl_mode_sense(struct ctl_scsiio *ctsio)
6282 struct ctl_lun *lun;
6283 int pc, page_code, dbd, llba, subpage;
6284 int alloc_len, page_len, header_len, total_len;
6285 struct scsi_mode_block_descr *block_desc;
6286 struct ctl_page_index *page_index;
6292 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6294 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6295 switch (ctsio->cdb[0]) {
6296 case MODE_SENSE_6: {
6297 struct scsi_mode_sense_6 *cdb;
6299 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6301 header_len = sizeof(struct scsi_mode_hdr_6);
6302 if (cdb->byte2 & SMS_DBD)
6305 header_len += sizeof(struct scsi_mode_block_descr);
6307 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6308 page_code = cdb->page & SMS_PAGE_CODE;
6309 subpage = cdb->subpage;
6310 alloc_len = cdb->length;
6313 case MODE_SENSE_10: {
6314 struct scsi_mode_sense_10 *cdb;
6316 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6318 header_len = sizeof(struct scsi_mode_hdr_10);
6320 if (cdb->byte2 & SMS_DBD)
6323 header_len += sizeof(struct scsi_mode_block_descr);
6324 if (cdb->byte2 & SMS10_LLBAA)
6326 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6327 page_code = cdb->page & SMS_PAGE_CODE;
6328 subpage = cdb->subpage;
6329 alloc_len = scsi_2btoul(cdb->length);
6333 ctl_set_invalid_opcode(ctsio);
6334 ctl_done((union ctl_io *)ctsio);
6335 return (CTL_RETVAL_COMPLETE);
6336 break; /* NOTREACHED */
6340 * We have to make a first pass through to calculate the size of
6341 * the pages that match the user's query. Then we allocate enough
6342 * memory to hold it, and actually copy the data into the buffer.
6344 switch (page_code) {
6345 case SMS_ALL_PAGES_PAGE: {
6351 * At the moment, values other than 0 and 0xff here are
6352 * reserved according to SPC-3.
6354 if ((subpage != SMS_SUBPAGE_PAGE_0)
6355 && (subpage != SMS_SUBPAGE_ALL)) {
6356 ctl_set_invalid_field(ctsio,
6362 ctl_done((union ctl_io *)ctsio);
6363 return (CTL_RETVAL_COMPLETE);
6366 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6367 page_index = &lun->mode_pages.index[i];
6369 /* Make sure the page is supported for this dev type */
6370 if (lun->be_lun->lun_type == T_DIRECT &&
6371 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6373 if (lun->be_lun->lun_type == T_PROCESSOR &&
6374 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6376 if (lun->be_lun->lun_type == T_CDROM &&
6377 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6381 * We don't use this subpage if the user didn't
6382 * request all subpages.
6384 if ((page_index->subpage != 0)
6385 && (subpage == SMS_SUBPAGE_PAGE_0))
6389 printf("found page %#x len %d\n",
6390 page_index->page_code & SMPH_PC_MASK,
6391 page_index->page_len);
6393 page_len += page_index->page_len;
6402 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6403 page_index = &lun->mode_pages.index[i];
6405 /* Make sure the page is supported for this dev type */
6406 if (lun->be_lun->lun_type == T_DIRECT &&
6407 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6409 if (lun->be_lun->lun_type == T_PROCESSOR &&
6410 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6412 if (lun->be_lun->lun_type == T_CDROM &&
6413 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6416 /* Look for the right page code */
6417 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6420 /* Look for the right subpage or the subpage wildcard*/
6421 if ((page_index->subpage != subpage)
6422 && (subpage != SMS_SUBPAGE_ALL))
6426 printf("found page %#x len %d\n",
6427 page_index->page_code & SMPH_PC_MASK,
6428 page_index->page_len);
6431 page_len += page_index->page_len;
6434 if (page_len == 0) {
6435 ctl_set_invalid_field(ctsio,
6441 ctl_done((union ctl_io *)ctsio);
6442 return (CTL_RETVAL_COMPLETE);
6448 total_len = header_len + page_len;
6450 printf("header_len = %d, page_len = %d, total_len = %d\n",
6451 header_len, page_len, total_len);
6454 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6455 ctsio->kern_sg_entries = 0;
6456 ctsio->kern_data_resid = 0;
6457 ctsio->kern_rel_offset = 0;
6458 if (total_len < alloc_len) {
6459 ctsio->residual = alloc_len - total_len;
6460 ctsio->kern_data_len = total_len;
6461 ctsio->kern_total_len = total_len;
6463 ctsio->residual = 0;
6464 ctsio->kern_data_len = alloc_len;
6465 ctsio->kern_total_len = alloc_len;
6468 switch (ctsio->cdb[0]) {
6469 case MODE_SENSE_6: {
6470 struct scsi_mode_hdr_6 *header;
6472 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6474 header->datalen = MIN(total_len - 1, 254);
6475 if (lun->be_lun->lun_type == T_DIRECT) {
6476 header->dev_specific = 0x10; /* DPOFUA */
6477 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6478 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6479 header->dev_specific |= 0x80; /* WP */
6482 header->block_descr_len = 0;
6484 header->block_descr_len =
6485 sizeof(struct scsi_mode_block_descr);
6486 block_desc = (struct scsi_mode_block_descr *)&header[1];
6489 case MODE_SENSE_10: {
6490 struct scsi_mode_hdr_10 *header;
6493 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6495 datalen = MIN(total_len - 2, 65533);
6496 scsi_ulto2b(datalen, header->datalen);
6497 if (lun->be_lun->lun_type == T_DIRECT) {
6498 header->dev_specific = 0x10; /* DPOFUA */
6499 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6500 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6501 header->dev_specific |= 0x80; /* WP */
6504 scsi_ulto2b(0, header->block_descr_len);
6506 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6507 header->block_descr_len);
6508 block_desc = (struct scsi_mode_block_descr *)&header[1];
6512 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6516 * If we've got a disk, use its blocksize in the block
6517 * descriptor. Otherwise, just set it to 0.
6520 if (lun->be_lun->lun_type == T_DIRECT)
6521 scsi_ulto3b(lun->be_lun->blocksize,
6522 block_desc->block_len);
6524 scsi_ulto3b(0, block_desc->block_len);
6527 switch (page_code) {
6528 case SMS_ALL_PAGES_PAGE: {
6531 data_used = header_len;
6532 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6533 struct ctl_page_index *page_index;
6535 page_index = &lun->mode_pages.index[i];
6536 if (lun->be_lun->lun_type == T_DIRECT &&
6537 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6539 if (lun->be_lun->lun_type == T_PROCESSOR &&
6540 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6542 if (lun->be_lun->lun_type == T_CDROM &&
6543 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6547 * We don't use this subpage if the user didn't
6548 * request all subpages. We already checked (above)
6549 * to make sure the user only specified a subpage
6550 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6552 if ((page_index->subpage != 0)
6553 && (subpage == SMS_SUBPAGE_PAGE_0))
6557 * Call the handler, if it exists, to update the
6558 * page to the latest values.
6560 if (page_index->sense_handler != NULL)
6561 page_index->sense_handler(ctsio, page_index,pc);
6563 memcpy(ctsio->kern_data_ptr + data_used,
6564 page_index->page_data +
6565 (page_index->page_len * pc),
6566 page_index->page_len);
6567 data_used += page_index->page_len;
6574 data_used = header_len;
6576 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6577 struct ctl_page_index *page_index;
6579 page_index = &lun->mode_pages.index[i];
6581 /* Look for the right page code */
6582 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6585 /* Look for the right subpage or the subpage wildcard*/
6586 if ((page_index->subpage != subpage)
6587 && (subpage != SMS_SUBPAGE_ALL))
6590 /* Make sure the page is supported for this dev type */
6591 if (lun->be_lun->lun_type == T_DIRECT &&
6592 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6594 if (lun->be_lun->lun_type == T_PROCESSOR &&
6595 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6597 if (lun->be_lun->lun_type == T_CDROM &&
6598 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6602 * Call the handler, if it exists, to update the
6603 * page to the latest values.
6605 if (page_index->sense_handler != NULL)
6606 page_index->sense_handler(ctsio, page_index,pc);
6608 memcpy(ctsio->kern_data_ptr + data_used,
6609 page_index->page_data +
6610 (page_index->page_len * pc),
6611 page_index->page_len);
6612 data_used += page_index->page_len;
6618 ctl_set_success(ctsio);
6619 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6620 ctsio->be_move_done = ctl_config_move_done;
6621 ctl_datamove((union ctl_io *)ctsio);
6622 return (CTL_RETVAL_COMPLETE);
6626 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6627 struct ctl_page_index *page_index,
6630 struct ctl_lun *lun;
6631 struct scsi_log_param_header *phdr;
6635 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6636 data = page_index->page_data;
6638 if (lun->backend->lun_attr != NULL &&
6639 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6641 phdr = (struct scsi_log_param_header *)data;
6642 scsi_ulto2b(0x0001, phdr->param_code);
6643 phdr->param_control = SLP_LBIN | SLP_LP;
6644 phdr->param_len = 8;
6645 data = (uint8_t *)(phdr + 1);
6646 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6647 data[4] = 0x02; /* per-pool */
6648 data += phdr->param_len;
6651 if (lun->backend->lun_attr != NULL &&
6652 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6654 phdr = (struct scsi_log_param_header *)data;
6655 scsi_ulto2b(0x0002, phdr->param_code);
6656 phdr->param_control = SLP_LBIN | SLP_LP;
6657 phdr->param_len = 8;
6658 data = (uint8_t *)(phdr + 1);
6659 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6660 data[4] = 0x01; /* per-LUN */
6661 data += phdr->param_len;
6664 if (lun->backend->lun_attr != NULL &&
6665 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6667 phdr = (struct scsi_log_param_header *)data;
6668 scsi_ulto2b(0x00f1, phdr->param_code);
6669 phdr->param_control = SLP_LBIN | SLP_LP;
6670 phdr->param_len = 8;
6671 data = (uint8_t *)(phdr + 1);
6672 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6673 data[4] = 0x02; /* per-pool */
6674 data += phdr->param_len;
6677 if (lun->backend->lun_attr != NULL &&
6678 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6680 phdr = (struct scsi_log_param_header *)data;
6681 scsi_ulto2b(0x00f2, phdr->param_code);
6682 phdr->param_control = SLP_LBIN | SLP_LP;
6683 phdr->param_len = 8;
6684 data = (uint8_t *)(phdr + 1);
6685 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6686 data[4] = 0x02; /* per-pool */
6687 data += phdr->param_len;
6690 page_index->page_len = data - page_index->page_data;
6695 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
6696 struct ctl_page_index *page_index,
6699 struct ctl_lun *lun;
6700 struct stat_page *data;
6701 uint64_t rn, wn, rb, wb;
6702 struct bintime rt, wt;
6705 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6706 data = (struct stat_page *)page_index->page_data;
6708 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
6709 data->sap.hdr.param_control = SLP_LBIN;
6710 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
6711 sizeof(struct scsi_log_param_header);
6712 rn = wn = rb = wb = 0;
6715 for (i = 0; i < CTL_MAX_PORTS; i++) {
6716 rn += lun->stats.ports[i].operations[CTL_STATS_READ];
6717 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE];
6718 rb += lun->stats.ports[i].bytes[CTL_STATS_READ];
6719 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE];
6720 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]);
6721 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]);
6723 scsi_u64to8b(rn, data->sap.read_num);
6724 scsi_u64to8b(wn, data->sap.write_num);
6725 if (lun->stats.blocksize > 0) {
6726 scsi_u64to8b(wb / lun->stats.blocksize,
6727 data->sap.recvieved_lba);
6728 scsi_u64to8b(rb / lun->stats.blocksize,
6729 data->sap.transmitted_lba);
6731 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000),
6732 data->sap.read_int);
6733 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000),
6734 data->sap.write_int);
6735 scsi_u64to8b(0, data->sap.weighted_num);
6736 scsi_u64to8b(0, data->sap.weighted_int);
6737 scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
6738 data->it.hdr.param_control = SLP_LBIN;
6739 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
6740 sizeof(struct scsi_log_param_header);
6742 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
6744 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
6745 data->it.hdr.param_control = SLP_LBIN;
6746 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
6747 sizeof(struct scsi_log_param_header);
6748 scsi_ulto4b(3, data->ti.exponent);
6749 scsi_ulto4b(1, data->ti.integer);
6754 ctl_ie_log_sense_handler(struct ctl_scsiio *ctsio,
6755 struct ctl_page_index *page_index,
6758 struct ctl_lun *lun;
6759 struct scsi_log_informational_exceptions *data;
6761 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6762 data = (struct scsi_log_informational_exceptions *)page_index->page_data;
6764 scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code);
6765 data->hdr.param_control = SLP_LBIN;
6766 data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) -
6767 sizeof(struct scsi_log_param_header);
6768 data->ie_asc = lun->ie_asc;
6769 data->ie_ascq = lun->ie_ascq;
6770 data->temperature = 0xff;
6775 ctl_log_sense(struct ctl_scsiio *ctsio)
6777 struct ctl_lun *lun;
6778 int i, pc, page_code, subpage;
6779 int alloc_len, total_len;
6780 struct ctl_page_index *page_index;
6781 struct scsi_log_sense *cdb;
6782 struct scsi_log_header *header;
6784 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6786 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6787 cdb = (struct scsi_log_sense *)ctsio->cdb;
6788 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6789 page_code = cdb->page & SLS_PAGE_CODE;
6790 subpage = cdb->subpage;
6791 alloc_len = scsi_2btoul(cdb->length);
6794 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6795 page_index = &lun->log_pages.index[i];
6797 /* Look for the right page code */
6798 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6801 /* Look for the right subpage or the subpage wildcard*/
6802 if (page_index->subpage != subpage)
6807 if (i >= CTL_NUM_LOG_PAGES) {
6808 ctl_set_invalid_field(ctsio,
6814 ctl_done((union ctl_io *)ctsio);
6815 return (CTL_RETVAL_COMPLETE);
6818 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6820 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6821 ctsio->kern_sg_entries = 0;
6822 ctsio->kern_data_resid = 0;
6823 ctsio->kern_rel_offset = 0;
6824 if (total_len < alloc_len) {
6825 ctsio->residual = alloc_len - total_len;
6826 ctsio->kern_data_len = total_len;
6827 ctsio->kern_total_len = total_len;
6829 ctsio->residual = 0;
6830 ctsio->kern_data_len = alloc_len;
6831 ctsio->kern_total_len = alloc_len;
6834 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
6835 header->page = page_index->page_code;
6836 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING)
6837 header->page |= SL_DS;
6838 if (page_index->subpage) {
6839 header->page |= SL_SPF;
6840 header->subpage = page_index->subpage;
6842 scsi_ulto2b(page_index->page_len, header->datalen);
6845 * Call the handler, if it exists, to update the
6846 * page to the latest values.
6848 if (page_index->sense_handler != NULL)
6849 page_index->sense_handler(ctsio, page_index, pc);
6851 memcpy(header + 1, page_index->page_data, page_index->page_len);
6853 ctl_set_success(ctsio);
6854 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6855 ctsio->be_move_done = ctl_config_move_done;
6856 ctl_datamove((union ctl_io *)ctsio);
6857 return (CTL_RETVAL_COMPLETE);
6861 ctl_read_capacity(struct ctl_scsiio *ctsio)
6863 struct scsi_read_capacity *cdb;
6864 struct scsi_read_capacity_data *data;
6865 struct ctl_lun *lun;
6868 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6870 cdb = (struct scsi_read_capacity *)ctsio->cdb;
6872 lba = scsi_4btoul(cdb->addr);
6873 if (((cdb->pmi & SRC_PMI) == 0)
6875 ctl_set_invalid_field(/*ctsio*/ ctsio,
6881 ctl_done((union ctl_io *)ctsio);
6882 return (CTL_RETVAL_COMPLETE);
6885 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6887 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6888 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6889 ctsio->residual = 0;
6890 ctsio->kern_data_len = sizeof(*data);
6891 ctsio->kern_total_len = sizeof(*data);
6892 ctsio->kern_data_resid = 0;
6893 ctsio->kern_rel_offset = 0;
6894 ctsio->kern_sg_entries = 0;
6897 * If the maximum LBA is greater than 0xfffffffe, the user must
6898 * issue a SERVICE ACTION IN (16) command, with the read capacity
6899 * serivce action set.
6901 if (lun->be_lun->maxlba > 0xfffffffe)
6902 scsi_ulto4b(0xffffffff, data->addr);
6904 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6907 * XXX KDM this may not be 512 bytes...
6909 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6911 ctl_set_success(ctsio);
6912 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6913 ctsio->be_move_done = ctl_config_move_done;
6914 ctl_datamove((union ctl_io *)ctsio);
6915 return (CTL_RETVAL_COMPLETE);
6919 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
6921 struct scsi_read_capacity_16 *cdb;
6922 struct scsi_read_capacity_data_long *data;
6923 struct ctl_lun *lun;
6927 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
6929 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
6931 alloc_len = scsi_4btoul(cdb->alloc_len);
6932 lba = scsi_8btou64(cdb->addr);
6934 if ((cdb->reladr & SRC16_PMI)
6936 ctl_set_invalid_field(/*ctsio*/ ctsio,
6942 ctl_done((union ctl_io *)ctsio);
6943 return (CTL_RETVAL_COMPLETE);
6946 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6948 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6949 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
6951 if (sizeof(*data) < alloc_len) {
6952 ctsio->residual = alloc_len - sizeof(*data);
6953 ctsio->kern_data_len = sizeof(*data);
6954 ctsio->kern_total_len = sizeof(*data);
6956 ctsio->residual = 0;
6957 ctsio->kern_data_len = alloc_len;
6958 ctsio->kern_total_len = alloc_len;
6960 ctsio->kern_data_resid = 0;
6961 ctsio->kern_rel_offset = 0;
6962 ctsio->kern_sg_entries = 0;
6964 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
6965 /* XXX KDM this may not be 512 bytes... */
6966 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6967 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
6968 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
6969 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
6970 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
6972 ctl_set_success(ctsio);
6973 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6974 ctsio->be_move_done = ctl_config_move_done;
6975 ctl_datamove((union ctl_io *)ctsio);
6976 return (CTL_RETVAL_COMPLETE);
6980 ctl_get_lba_status(struct ctl_scsiio *ctsio)
6982 struct scsi_get_lba_status *cdb;
6983 struct scsi_get_lba_status_data *data;
6984 struct ctl_lun *lun;
6985 struct ctl_lba_len_flags *lbalen;
6987 uint32_t alloc_len, total_len;
6990 CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
6992 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6993 cdb = (struct scsi_get_lba_status *)ctsio->cdb;
6994 lba = scsi_8btou64(cdb->addr);
6995 alloc_len = scsi_4btoul(cdb->alloc_len);
6997 if (lba > lun->be_lun->maxlba) {
6998 ctl_set_lba_out_of_range(ctsio);
6999 ctl_done((union ctl_io *)ctsio);
7000 return (CTL_RETVAL_COMPLETE);
7003 total_len = sizeof(*data) + sizeof(data->descr[0]);
7004 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7005 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
7007 if (total_len < alloc_len) {
7008 ctsio->residual = alloc_len - total_len;
7009 ctsio->kern_data_len = total_len;
7010 ctsio->kern_total_len = total_len;
7012 ctsio->residual = 0;
7013 ctsio->kern_data_len = alloc_len;
7014 ctsio->kern_total_len = alloc_len;
7016 ctsio->kern_data_resid = 0;
7017 ctsio->kern_rel_offset = 0;
7018 ctsio->kern_sg_entries = 0;
7020 /* Fill dummy data in case backend can't tell anything. */
7021 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
7022 scsi_u64to8b(lba, data->descr[0].addr);
7023 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
7024 data->descr[0].length);
7025 data->descr[0].status = 0; /* Mapped or unknown. */
7027 ctl_set_success(ctsio);
7028 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7029 ctsio->be_move_done = ctl_config_move_done;
7031 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
7033 lbalen->len = total_len;
7035 retval = lun->backend->config_read((union ctl_io *)ctsio);
7036 return (CTL_RETVAL_COMPLETE);
7040 ctl_read_defect(struct ctl_scsiio *ctsio)
7042 struct scsi_read_defect_data_10 *ccb10;
7043 struct scsi_read_defect_data_12 *ccb12;
7044 struct scsi_read_defect_data_hdr_10 *data10;
7045 struct scsi_read_defect_data_hdr_12 *data12;
7046 uint32_t alloc_len, data_len;
7049 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7051 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7052 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7053 format = ccb10->format;
7054 alloc_len = scsi_2btoul(ccb10->alloc_length);
7055 data_len = sizeof(*data10);
7057 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7058 format = ccb12->format;
7059 alloc_len = scsi_4btoul(ccb12->alloc_length);
7060 data_len = sizeof(*data12);
7062 if (alloc_len == 0) {
7063 ctl_set_success(ctsio);
7064 ctl_done((union ctl_io *)ctsio);
7065 return (CTL_RETVAL_COMPLETE);
7068 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7069 if (data_len < alloc_len) {
7070 ctsio->residual = alloc_len - data_len;
7071 ctsio->kern_data_len = data_len;
7072 ctsio->kern_total_len = data_len;
7074 ctsio->residual = 0;
7075 ctsio->kern_data_len = alloc_len;
7076 ctsio->kern_total_len = alloc_len;
7078 ctsio->kern_data_resid = 0;
7079 ctsio->kern_rel_offset = 0;
7080 ctsio->kern_sg_entries = 0;
7082 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7083 data10 = (struct scsi_read_defect_data_hdr_10 *)
7084 ctsio->kern_data_ptr;
7085 data10->format = format;
7086 scsi_ulto2b(0, data10->length);
7088 data12 = (struct scsi_read_defect_data_hdr_12 *)
7089 ctsio->kern_data_ptr;
7090 data12->format = format;
7091 scsi_ulto2b(0, data12->generation);
7092 scsi_ulto4b(0, data12->length);
7095 ctl_set_success(ctsio);
7096 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7097 ctsio->be_move_done = ctl_config_move_done;
7098 ctl_datamove((union ctl_io *)ctsio);
7099 return (CTL_RETVAL_COMPLETE);
7103 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7105 struct scsi_maintenance_in *cdb;
7107 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os;
7108 int num_ha_groups, num_target_ports, shared_group;
7109 struct ctl_lun *lun;
7110 struct ctl_softc *softc;
7111 struct ctl_port *port;
7112 struct scsi_target_group_data *rtg_ptr;
7113 struct scsi_target_group_data_extended *rtg_ext_ptr;
7114 struct scsi_target_port_group_descriptor *tpg_desc;
7116 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7118 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7119 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7120 softc = lun->ctl_softc;
7122 retval = CTL_RETVAL_COMPLETE;
7124 switch (cdb->byte2 & STG_PDF_MASK) {
7125 case STG_PDF_LENGTH:
7128 case STG_PDF_EXTENDED:
7132 ctl_set_invalid_field(/*ctsio*/ ctsio,
7138 ctl_done((union ctl_io *)ctsio);
7142 num_target_ports = 0;
7143 shared_group = (softc->is_single != 0);
7144 mtx_lock(&softc->ctl_lock);
7145 STAILQ_FOREACH(port, &softc->port_list, links) {
7146 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7148 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7151 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7154 mtx_unlock(&softc->ctl_lock);
7155 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES;
7158 total_len = sizeof(struct scsi_target_group_data_extended);
7160 total_len = sizeof(struct scsi_target_group_data);
7161 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7162 (shared_group + num_ha_groups) +
7163 sizeof(struct scsi_target_port_descriptor) * num_target_ports;
7165 alloc_len = scsi_4btoul(cdb->length);
7167 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7169 ctsio->kern_sg_entries = 0;
7171 if (total_len < alloc_len) {
7172 ctsio->residual = alloc_len - total_len;
7173 ctsio->kern_data_len = total_len;
7174 ctsio->kern_total_len = total_len;
7176 ctsio->residual = 0;
7177 ctsio->kern_data_len = alloc_len;
7178 ctsio->kern_total_len = alloc_len;
7180 ctsio->kern_data_resid = 0;
7181 ctsio->kern_rel_offset = 0;
7184 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7185 ctsio->kern_data_ptr;
7186 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7187 rtg_ext_ptr->format_type = 0x10;
7188 rtg_ext_ptr->implicit_transition_time = 0;
7189 tpg_desc = &rtg_ext_ptr->groups[0];
7191 rtg_ptr = (struct scsi_target_group_data *)
7192 ctsio->kern_data_ptr;
7193 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7194 tpg_desc = &rtg_ptr->groups[0];
7197 mtx_lock(&softc->ctl_lock);
7198 pg = softc->port_min / softc->port_cnt;
7199 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) {
7200 /* Some shelf is known to be primary. */
7201 if (softc->ha_link == CTL_HA_LINK_OFFLINE)
7202 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7203 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN)
7204 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7205 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY)
7206 os = TPG_ASYMMETRIC_ACCESS_STANDBY;
7208 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7209 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7210 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7213 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7216 /* No known primary shelf. */
7217 if (softc->ha_link == CTL_HA_LINK_OFFLINE) {
7218 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7219 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7220 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) {
7221 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7222 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7224 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7228 tpg_desc->pref_state = ts;
7229 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7230 TPG_U_SUP | TPG_T_SUP;
7231 scsi_ulto2b(1, tpg_desc->target_port_group);
7232 tpg_desc->status = TPG_IMPLICIT;
7234 STAILQ_FOREACH(port, &softc->port_list, links) {
7235 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7237 if (!softc->is_single &&
7238 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0)
7240 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7242 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7243 relative_target_port_identifier);
7246 tpg_desc->target_port_count = pc;
7247 tpg_desc = (struct scsi_target_port_group_descriptor *)
7248 &tpg_desc->descriptors[pc];
7250 for (g = 0; g < num_ha_groups; g++) {
7251 tpg_desc->pref_state = (g == pg) ? ts : os;
7252 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7253 TPG_U_SUP | TPG_T_SUP;
7254 scsi_ulto2b(2 + g, tpg_desc->target_port_group);
7255 tpg_desc->status = TPG_IMPLICIT;
7257 STAILQ_FOREACH(port, &softc->port_list, links) {
7258 if (port->targ_port < g * softc->port_cnt ||
7259 port->targ_port >= (g + 1) * softc->port_cnt)
7261 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7263 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7265 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7267 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7268 relative_target_port_identifier);
7271 tpg_desc->target_port_count = pc;
7272 tpg_desc = (struct scsi_target_port_group_descriptor *)
7273 &tpg_desc->descriptors[pc];
7275 mtx_unlock(&softc->ctl_lock);
7277 ctl_set_success(ctsio);
7278 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7279 ctsio->be_move_done = ctl_config_move_done;
7280 ctl_datamove((union ctl_io *)ctsio);
7285 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7287 struct ctl_lun *lun;
7288 struct scsi_report_supported_opcodes *cdb;
7289 const struct ctl_cmd_entry *entry, *sentry;
7290 struct scsi_report_supported_opcodes_all *all;
7291 struct scsi_report_supported_opcodes_descr *descr;
7292 struct scsi_report_supported_opcodes_one *one;
7294 int alloc_len, total_len;
7295 int opcode, service_action, i, j, num;
7297 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7299 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7300 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7302 retval = CTL_RETVAL_COMPLETE;
7304 opcode = cdb->requested_opcode;
7305 service_action = scsi_2btoul(cdb->requested_service_action);
7306 switch (cdb->options & RSO_OPTIONS_MASK) {
7307 case RSO_OPTIONS_ALL:
7309 for (i = 0; i < 256; i++) {
7310 entry = &ctl_cmd_table[i];
7311 if (entry->flags & CTL_CMD_FLAG_SA5) {
7312 for (j = 0; j < 32; j++) {
7313 sentry = &((const struct ctl_cmd_entry *)
7315 if (ctl_cmd_applicable(
7316 lun->be_lun->lun_type, sentry))
7320 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7325 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7326 num * sizeof(struct scsi_report_supported_opcodes_descr);
7328 case RSO_OPTIONS_OC:
7329 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7330 ctl_set_invalid_field(/*ctsio*/ ctsio,
7336 ctl_done((union ctl_io *)ctsio);
7337 return (CTL_RETVAL_COMPLETE);
7339 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7341 case RSO_OPTIONS_OC_SA:
7342 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7343 service_action >= 32) {
7344 ctl_set_invalid_field(/*ctsio*/ ctsio,
7350 ctl_done((union ctl_io *)ctsio);
7351 return (CTL_RETVAL_COMPLETE);
7353 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7356 ctl_set_invalid_field(/*ctsio*/ ctsio,
7362 ctl_done((union ctl_io *)ctsio);
7363 return (CTL_RETVAL_COMPLETE);
7366 alloc_len = scsi_4btoul(cdb->length);
7368 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7370 ctsio->kern_sg_entries = 0;
7372 if (total_len < alloc_len) {
7373 ctsio->residual = alloc_len - total_len;
7374 ctsio->kern_data_len = total_len;
7375 ctsio->kern_total_len = total_len;
7377 ctsio->residual = 0;
7378 ctsio->kern_data_len = alloc_len;
7379 ctsio->kern_total_len = alloc_len;
7381 ctsio->kern_data_resid = 0;
7382 ctsio->kern_rel_offset = 0;
7384 switch (cdb->options & RSO_OPTIONS_MASK) {
7385 case RSO_OPTIONS_ALL:
7386 all = (struct scsi_report_supported_opcodes_all *)
7387 ctsio->kern_data_ptr;
7389 for (i = 0; i < 256; i++) {
7390 entry = &ctl_cmd_table[i];
7391 if (entry->flags & CTL_CMD_FLAG_SA5) {
7392 for (j = 0; j < 32; j++) {
7393 sentry = &((const struct ctl_cmd_entry *)
7395 if (!ctl_cmd_applicable(
7396 lun->be_lun->lun_type, sentry))
7398 descr = &all->descr[num++];
7400 scsi_ulto2b(j, descr->service_action);
7401 descr->flags = RSO_SERVACTV;
7402 scsi_ulto2b(sentry->length,
7406 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7409 descr = &all->descr[num++];
7411 scsi_ulto2b(0, descr->service_action);
7413 scsi_ulto2b(entry->length, descr->cdb_length);
7417 num * sizeof(struct scsi_report_supported_opcodes_descr),
7420 case RSO_OPTIONS_OC:
7421 one = (struct scsi_report_supported_opcodes_one *)
7422 ctsio->kern_data_ptr;
7423 entry = &ctl_cmd_table[opcode];
7425 case RSO_OPTIONS_OC_SA:
7426 one = (struct scsi_report_supported_opcodes_one *)
7427 ctsio->kern_data_ptr;
7428 entry = &ctl_cmd_table[opcode];
7429 entry = &((const struct ctl_cmd_entry *)
7430 entry->execute)[service_action];
7432 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7434 scsi_ulto2b(entry->length, one->cdb_length);
7435 one->cdb_usage[0] = opcode;
7436 memcpy(&one->cdb_usage[1], entry->usage,
7443 ctl_set_success(ctsio);
7444 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7445 ctsio->be_move_done = ctl_config_move_done;
7446 ctl_datamove((union ctl_io *)ctsio);
7451 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7453 struct scsi_report_supported_tmf *cdb;
7454 struct scsi_report_supported_tmf_data *data;
7456 int alloc_len, total_len;
7458 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7460 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7462 retval = CTL_RETVAL_COMPLETE;
7464 total_len = sizeof(struct scsi_report_supported_tmf_data);
7465 alloc_len = scsi_4btoul(cdb->length);
7467 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7469 ctsio->kern_sg_entries = 0;
7471 if (total_len < alloc_len) {
7472 ctsio->residual = alloc_len - total_len;
7473 ctsio->kern_data_len = total_len;
7474 ctsio->kern_total_len = total_len;
7476 ctsio->residual = 0;
7477 ctsio->kern_data_len = alloc_len;
7478 ctsio->kern_total_len = alloc_len;
7480 ctsio->kern_data_resid = 0;
7481 ctsio->kern_rel_offset = 0;
7483 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr;
7484 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS |
7486 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS;
7488 ctl_set_success(ctsio);
7489 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7490 ctsio->be_move_done = ctl_config_move_done;
7491 ctl_datamove((union ctl_io *)ctsio);
7496 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7498 struct scsi_report_timestamp *cdb;
7499 struct scsi_report_timestamp_data *data;
7503 int alloc_len, total_len;
7505 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7507 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7509 retval = CTL_RETVAL_COMPLETE;
7511 total_len = sizeof(struct scsi_report_timestamp_data);
7512 alloc_len = scsi_4btoul(cdb->length);
7514 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7516 ctsio->kern_sg_entries = 0;
7518 if (total_len < alloc_len) {
7519 ctsio->residual = alloc_len - total_len;
7520 ctsio->kern_data_len = total_len;
7521 ctsio->kern_total_len = total_len;
7523 ctsio->residual = 0;
7524 ctsio->kern_data_len = alloc_len;
7525 ctsio->kern_total_len = alloc_len;
7527 ctsio->kern_data_resid = 0;
7528 ctsio->kern_rel_offset = 0;
7530 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7531 scsi_ulto2b(sizeof(*data) - 2, data->length);
7532 data->origin = RTS_ORIG_OUTSIDE;
7534 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7535 scsi_ulto4b(timestamp >> 16, data->timestamp);
7536 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7538 ctl_set_success(ctsio);
7539 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7540 ctsio->be_move_done = ctl_config_move_done;
7541 ctl_datamove((union ctl_io *)ctsio);
7546 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7548 struct scsi_per_res_in *cdb;
7549 int alloc_len, total_len = 0;
7550 /* struct scsi_per_res_in_rsrv in_data; */
7551 struct ctl_lun *lun;
7552 struct ctl_softc *softc;
7555 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7557 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7559 alloc_len = scsi_2btoul(cdb->length);
7561 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7562 softc = lun->ctl_softc;
7565 mtx_lock(&lun->lun_lock);
7566 switch (cdb->action) {
7567 case SPRI_RK: /* read keys */
7568 total_len = sizeof(struct scsi_per_res_in_keys) +
7570 sizeof(struct scsi_per_res_key);
7572 case SPRI_RR: /* read reservation */
7573 if (lun->flags & CTL_LUN_PR_RESERVED)
7574 total_len = sizeof(struct scsi_per_res_in_rsrv);
7576 total_len = sizeof(struct scsi_per_res_in_header);
7578 case SPRI_RC: /* report capabilities */
7579 total_len = sizeof(struct scsi_per_res_cap);
7581 case SPRI_RS: /* read full status */
7582 total_len = sizeof(struct scsi_per_res_in_header) +
7583 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7587 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7589 mtx_unlock(&lun->lun_lock);
7591 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7593 if (total_len < alloc_len) {
7594 ctsio->residual = alloc_len - total_len;
7595 ctsio->kern_data_len = total_len;
7596 ctsio->kern_total_len = total_len;
7598 ctsio->residual = 0;
7599 ctsio->kern_data_len = alloc_len;
7600 ctsio->kern_total_len = alloc_len;
7603 ctsio->kern_data_resid = 0;
7604 ctsio->kern_rel_offset = 0;
7605 ctsio->kern_sg_entries = 0;
7607 mtx_lock(&lun->lun_lock);
7608 switch (cdb->action) {
7609 case SPRI_RK: { // read keys
7610 struct scsi_per_res_in_keys *res_keys;
7613 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7616 * We had to drop the lock to allocate our buffer, which
7617 * leaves time for someone to come in with another
7618 * persistent reservation. (That is unlikely, though,
7619 * since this should be the only persistent reservation
7620 * command active right now.)
7622 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7623 (lun->pr_key_count *
7624 sizeof(struct scsi_per_res_key)))){
7625 mtx_unlock(&lun->lun_lock);
7626 free(ctsio->kern_data_ptr, M_CTL);
7627 printf("%s: reservation length changed, retrying\n",
7632 scsi_ulto4b(lun->pr_generation, res_keys->header.generation);
7634 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7635 lun->pr_key_count, res_keys->header.length);
7637 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) {
7638 if ((key = ctl_get_prkey(lun, i)) == 0)
7642 * We used lun->pr_key_count to calculate the
7643 * size to allocate. If it turns out the number of
7644 * initiators with the registered flag set is
7645 * larger than that (i.e. they haven't been kept in
7646 * sync), we've got a problem.
7648 if (key_count >= lun->pr_key_count) {
7652 scsi_u64to8b(key, res_keys->keys[key_count].key);
7657 case SPRI_RR: { // read reservation
7658 struct scsi_per_res_in_rsrv *res;
7659 int tmp_len, header_only;
7661 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7663 scsi_ulto4b(lun->pr_generation, res->header.generation);
7665 if (lun->flags & CTL_LUN_PR_RESERVED)
7667 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7668 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7669 res->header.length);
7672 tmp_len = sizeof(struct scsi_per_res_in_header);
7673 scsi_ulto4b(0, res->header.length);
7678 * We had to drop the lock to allocate our buffer, which
7679 * leaves time for someone to come in with another
7680 * persistent reservation. (That is unlikely, though,
7681 * since this should be the only persistent reservation
7682 * command active right now.)
7684 if (tmp_len != total_len) {
7685 mtx_unlock(&lun->lun_lock);
7686 free(ctsio->kern_data_ptr, M_CTL);
7687 printf("%s: reservation status changed, retrying\n",
7693 * No reservation held, so we're done.
7695 if (header_only != 0)
7699 * If the registration is an All Registrants type, the key
7700 * is 0, since it doesn't really matter.
7702 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7703 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7704 res->data.reservation);
7706 res->data.scopetype = lun->pr_res_type;
7709 case SPRI_RC: //report capabilities
7711 struct scsi_per_res_cap *res_cap;
7714 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7715 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7716 res_cap->flags1 = SPRI_CRH;
7717 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5;
7718 type_mask = SPRI_TM_WR_EX_AR |
7724 scsi_ulto2b(type_mask, res_cap->type_mask);
7727 case SPRI_RS: { // read full status
7728 struct scsi_per_res_in_full *res_status;
7729 struct scsi_per_res_in_full_desc *res_desc;
7730 struct ctl_port *port;
7733 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7736 * We had to drop the lock to allocate our buffer, which
7737 * leaves time for someone to come in with another
7738 * persistent reservation. (That is unlikely, though,
7739 * since this should be the only persistent reservation
7740 * command active right now.)
7742 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7743 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7744 lun->pr_key_count)){
7745 mtx_unlock(&lun->lun_lock);
7746 free(ctsio->kern_data_ptr, M_CTL);
7747 printf("%s: reservation length changed, retrying\n",
7752 scsi_ulto4b(lun->pr_generation, res_status->header.generation);
7754 res_desc = &res_status->desc[0];
7755 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7756 if ((key = ctl_get_prkey(lun, i)) == 0)
7759 scsi_u64to8b(key, res_desc->res_key.key);
7760 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7761 (lun->pr_res_idx == i ||
7762 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7763 res_desc->flags = SPRI_FULL_R_HOLDER;
7764 res_desc->scopetype = lun->pr_res_type;
7766 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7767 res_desc->rel_trgt_port_id);
7769 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7771 len = ctl_create_iid(port,
7772 i % CTL_MAX_INIT_PER_PORT,
7773 res_desc->transport_id);
7774 scsi_ulto4b(len, res_desc->additional_length);
7775 res_desc = (struct scsi_per_res_in_full_desc *)
7776 &res_desc->transport_id[len];
7778 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7779 res_status->header.length);
7783 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7785 mtx_unlock(&lun->lun_lock);
7787 ctl_set_success(ctsio);
7788 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7789 ctsio->be_move_done = ctl_config_move_done;
7790 ctl_datamove((union ctl_io *)ctsio);
7791 return (CTL_RETVAL_COMPLETE);
7795 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7799 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7800 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7801 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7802 struct scsi_per_res_out_parms* param)
7804 union ctl_ha_msg persis_io;
7807 mtx_lock(&lun->lun_lock);
7808 if (sa_res_key == 0) {
7809 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7810 /* validate scope and type */
7811 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7813 mtx_unlock(&lun->lun_lock);
7814 ctl_set_invalid_field(/*ctsio*/ ctsio,
7820 ctl_done((union ctl_io *)ctsio);
7824 if (type>8 || type==2 || type==4 || type==0) {
7825 mtx_unlock(&lun->lun_lock);
7826 ctl_set_invalid_field(/*ctsio*/ ctsio,
7832 ctl_done((union ctl_io *)ctsio);
7837 * Unregister everybody else and build UA for
7840 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7841 if (i == residx || ctl_get_prkey(lun, i) == 0)
7844 ctl_clr_prkey(lun, i);
7845 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7847 lun->pr_key_count = 1;
7848 lun->pr_res_type = type;
7849 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7850 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7851 lun->pr_res_idx = residx;
7852 lun->pr_generation++;
7853 mtx_unlock(&lun->lun_lock);
7855 /* send msg to other side */
7856 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7857 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7858 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7859 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7860 persis_io.pr.pr_info.res_type = type;
7861 memcpy(persis_io.pr.pr_info.sa_res_key,
7862 param->serv_act_res_key,
7863 sizeof(param->serv_act_res_key));
7864 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7865 sizeof(persis_io.pr), M_WAITOK);
7867 /* not all registrants */
7868 mtx_unlock(&lun->lun_lock);
7869 free(ctsio->kern_data_ptr, M_CTL);
7870 ctl_set_invalid_field(ctsio,
7876 ctl_done((union ctl_io *)ctsio);
7879 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7880 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7883 if (res_key == sa_res_key) {
7886 * The spec implies this is not good but doesn't
7887 * say what to do. There are two choices either
7888 * generate a res conflict or check condition
7889 * with illegal field in parameter data. Since
7890 * that is what is done when the sa_res_key is
7891 * zero I'll take that approach since this has
7892 * to do with the sa_res_key.
7894 mtx_unlock(&lun->lun_lock);
7895 free(ctsio->kern_data_ptr, M_CTL);
7896 ctl_set_invalid_field(ctsio,
7902 ctl_done((union ctl_io *)ctsio);
7906 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7907 if (ctl_get_prkey(lun, i) != sa_res_key)
7911 ctl_clr_prkey(lun, i);
7912 lun->pr_key_count--;
7913 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7916 mtx_unlock(&lun->lun_lock);
7917 free(ctsio->kern_data_ptr, M_CTL);
7918 ctl_set_reservation_conflict(ctsio);
7919 ctl_done((union ctl_io *)ctsio);
7920 return (CTL_RETVAL_COMPLETE);
7922 lun->pr_generation++;
7923 mtx_unlock(&lun->lun_lock);
7925 /* send msg to other side */
7926 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7927 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7928 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7929 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7930 persis_io.pr.pr_info.res_type = type;
7931 memcpy(persis_io.pr.pr_info.sa_res_key,
7932 param->serv_act_res_key,
7933 sizeof(param->serv_act_res_key));
7934 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7935 sizeof(persis_io.pr), M_WAITOK);
7937 /* Reserved but not all registrants */
7938 /* sa_res_key is res holder */
7939 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
7940 /* validate scope and type */
7941 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7943 mtx_unlock(&lun->lun_lock);
7944 ctl_set_invalid_field(/*ctsio*/ ctsio,
7950 ctl_done((union ctl_io *)ctsio);
7954 if (type>8 || type==2 || type==4 || type==0) {
7955 mtx_unlock(&lun->lun_lock);
7956 ctl_set_invalid_field(/*ctsio*/ ctsio,
7962 ctl_done((union ctl_io *)ctsio);
7968 * if sa_res_key != res_key remove all
7969 * registrants w/sa_res_key and generate UA
7970 * for these registrants(Registrations
7971 * Preempted) if it wasn't an exclusive
7972 * reservation generate UA(Reservations
7973 * Preempted) for all other registered nexuses
7974 * if the type has changed. Establish the new
7975 * reservation and holder. If res_key and
7976 * sa_res_key are the same do the above
7977 * except don't unregister the res holder.
7980 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7981 if (i == residx || ctl_get_prkey(lun, i) == 0)
7984 if (sa_res_key == ctl_get_prkey(lun, i)) {
7985 ctl_clr_prkey(lun, i);
7986 lun->pr_key_count--;
7987 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7988 } else if (type != lun->pr_res_type &&
7989 (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
7990 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
7991 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
7994 lun->pr_res_type = type;
7995 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7996 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7997 lun->pr_res_idx = residx;
7999 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8000 lun->pr_generation++;
8001 mtx_unlock(&lun->lun_lock);
8003 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8004 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8005 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8006 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8007 persis_io.pr.pr_info.res_type = type;
8008 memcpy(persis_io.pr.pr_info.sa_res_key,
8009 param->serv_act_res_key,
8010 sizeof(param->serv_act_res_key));
8011 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8012 sizeof(persis_io.pr), M_WAITOK);
8015 * sa_res_key is not the res holder just
8016 * remove registrants
8020 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8021 if (sa_res_key != ctl_get_prkey(lun, i))
8025 ctl_clr_prkey(lun, i);
8026 lun->pr_key_count--;
8027 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8031 mtx_unlock(&lun->lun_lock);
8032 free(ctsio->kern_data_ptr, M_CTL);
8033 ctl_set_reservation_conflict(ctsio);
8034 ctl_done((union ctl_io *)ctsio);
8037 lun->pr_generation++;
8038 mtx_unlock(&lun->lun_lock);
8040 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8041 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8042 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8043 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8044 persis_io.pr.pr_info.res_type = type;
8045 memcpy(persis_io.pr.pr_info.sa_res_key,
8046 param->serv_act_res_key,
8047 sizeof(param->serv_act_res_key));
8048 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8049 sizeof(persis_io.pr), M_WAITOK);
8056 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8058 uint64_t sa_res_key;
8061 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8063 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8064 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8065 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
8066 if (sa_res_key == 0) {
8068 * Unregister everybody else and build UA for
8071 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8072 if (i == msg->pr.pr_info.residx ||
8073 ctl_get_prkey(lun, i) == 0)
8076 ctl_clr_prkey(lun, i);
8077 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8080 lun->pr_key_count = 1;
8081 lun->pr_res_type = msg->pr.pr_info.res_type;
8082 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8083 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8084 lun->pr_res_idx = msg->pr.pr_info.residx;
8086 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8087 if (sa_res_key == ctl_get_prkey(lun, i))
8090 ctl_clr_prkey(lun, i);
8091 lun->pr_key_count--;
8092 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8096 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8097 if (i == msg->pr.pr_info.residx ||
8098 ctl_get_prkey(lun, i) == 0)
8101 if (sa_res_key == ctl_get_prkey(lun, i)) {
8102 ctl_clr_prkey(lun, i);
8103 lun->pr_key_count--;
8104 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8105 } else if (msg->pr.pr_info.res_type != lun->pr_res_type
8106 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8107 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8108 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8111 lun->pr_res_type = msg->pr.pr_info.res_type;
8112 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8113 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8114 lun->pr_res_idx = msg->pr.pr_info.residx;
8116 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8118 lun->pr_generation++;
8124 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8127 u_int32_t param_len;
8128 struct scsi_per_res_out *cdb;
8129 struct ctl_lun *lun;
8130 struct scsi_per_res_out_parms* param;
8131 struct ctl_softc *softc;
8133 uint64_t res_key, sa_res_key, key;
8135 union ctl_ha_msg persis_io;
8138 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8140 retval = CTL_RETVAL_COMPLETE;
8142 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8143 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8144 softc = lun->ctl_softc;
8147 * We only support whole-LUN scope. The scope & type are ignored for
8148 * register, register and ignore existing key and clear.
8149 * We sometimes ignore scope and type on preempts too!!
8150 * Verify reservation type here as well.
8152 type = cdb->scope_type & SPR_TYPE_MASK;
8153 if ((cdb->action == SPRO_RESERVE)
8154 || (cdb->action == SPRO_RELEASE)) {
8155 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8156 ctl_set_invalid_field(/*ctsio*/ ctsio,
8162 ctl_done((union ctl_io *)ctsio);
8163 return (CTL_RETVAL_COMPLETE);
8166 if (type>8 || type==2 || type==4 || type==0) {
8167 ctl_set_invalid_field(/*ctsio*/ ctsio,
8173 ctl_done((union ctl_io *)ctsio);
8174 return (CTL_RETVAL_COMPLETE);
8178 param_len = scsi_4btoul(cdb->length);
8180 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8181 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8182 ctsio->kern_data_len = param_len;
8183 ctsio->kern_total_len = param_len;
8184 ctsio->kern_data_resid = 0;
8185 ctsio->kern_rel_offset = 0;
8186 ctsio->kern_sg_entries = 0;
8187 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8188 ctsio->be_move_done = ctl_config_move_done;
8189 ctl_datamove((union ctl_io *)ctsio);
8191 return (CTL_RETVAL_COMPLETE);
8194 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8196 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8197 res_key = scsi_8btou64(param->res_key.key);
8198 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8201 * Validate the reservation key here except for SPRO_REG_IGNO
8202 * This must be done for all other service actions
8204 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8205 mtx_lock(&lun->lun_lock);
8206 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8207 if (res_key != key) {
8209 * The current key passed in doesn't match
8210 * the one the initiator previously
8213 mtx_unlock(&lun->lun_lock);
8214 free(ctsio->kern_data_ptr, M_CTL);
8215 ctl_set_reservation_conflict(ctsio);
8216 ctl_done((union ctl_io *)ctsio);
8217 return (CTL_RETVAL_COMPLETE);
8219 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8221 * We are not registered
8223 mtx_unlock(&lun->lun_lock);
8224 free(ctsio->kern_data_ptr, M_CTL);
8225 ctl_set_reservation_conflict(ctsio);
8226 ctl_done((union ctl_io *)ctsio);
8227 return (CTL_RETVAL_COMPLETE);
8228 } else if (res_key != 0) {
8230 * We are not registered and trying to register but
8231 * the register key isn't zero.
8233 mtx_unlock(&lun->lun_lock);
8234 free(ctsio->kern_data_ptr, M_CTL);
8235 ctl_set_reservation_conflict(ctsio);
8236 ctl_done((union ctl_io *)ctsio);
8237 return (CTL_RETVAL_COMPLETE);
8239 mtx_unlock(&lun->lun_lock);
8242 switch (cdb->action & SPRO_ACTION_MASK) {
8244 case SPRO_REG_IGNO: {
8247 printf("Registration received\n");
8251 * We don't support any of these options, as we report in
8252 * the read capabilities request (see
8253 * ctl_persistent_reserve_in(), above).
8255 if ((param->flags & SPR_SPEC_I_PT)
8256 || (param->flags & SPR_ALL_TG_PT)
8257 || (param->flags & SPR_APTPL)) {
8260 if (param->flags & SPR_APTPL)
8262 else if (param->flags & SPR_ALL_TG_PT)
8264 else /* SPR_SPEC_I_PT */
8267 free(ctsio->kern_data_ptr, M_CTL);
8268 ctl_set_invalid_field(ctsio,
8274 ctl_done((union ctl_io *)ctsio);
8275 return (CTL_RETVAL_COMPLETE);
8278 mtx_lock(&lun->lun_lock);
8281 * The initiator wants to clear the
8284 if (sa_res_key == 0) {
8286 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8287 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8288 && ctl_get_prkey(lun, residx) == 0)) {
8289 mtx_unlock(&lun->lun_lock);
8293 ctl_clr_prkey(lun, residx);
8294 lun->pr_key_count--;
8296 if (residx == lun->pr_res_idx) {
8297 lun->flags &= ~CTL_LUN_PR_RESERVED;
8298 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8300 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8301 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8302 lun->pr_key_count) {
8304 * If the reservation is a registrants
8305 * only type we need to generate a UA
8306 * for other registered inits. The
8307 * sense code should be RESERVATIONS
8311 for (i = softc->init_min; i < softc->init_max; i++){
8312 if (ctl_get_prkey(lun, i) == 0)
8315 CTL_UA_RES_RELEASE);
8318 lun->pr_res_type = 0;
8319 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8320 if (lun->pr_key_count==0) {
8321 lun->flags &= ~CTL_LUN_PR_RESERVED;
8322 lun->pr_res_type = 0;
8323 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8326 lun->pr_generation++;
8327 mtx_unlock(&lun->lun_lock);
8329 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8330 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8331 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8332 persis_io.pr.pr_info.residx = residx;
8333 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8334 sizeof(persis_io.pr), M_WAITOK);
8335 } else /* sa_res_key != 0 */ {
8338 * If we aren't registered currently then increment
8339 * the key count and set the registered flag.
8341 ctl_alloc_prkey(lun, residx);
8342 if (ctl_get_prkey(lun, residx) == 0)
8343 lun->pr_key_count++;
8344 ctl_set_prkey(lun, residx, sa_res_key);
8345 lun->pr_generation++;
8346 mtx_unlock(&lun->lun_lock);
8348 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8349 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8350 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8351 persis_io.pr.pr_info.residx = residx;
8352 memcpy(persis_io.pr.pr_info.sa_res_key,
8353 param->serv_act_res_key,
8354 sizeof(param->serv_act_res_key));
8355 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8356 sizeof(persis_io.pr), M_WAITOK);
8363 printf("Reserve executed type %d\n", type);
8365 mtx_lock(&lun->lun_lock);
8366 if (lun->flags & CTL_LUN_PR_RESERVED) {
8368 * if this isn't the reservation holder and it's
8369 * not a "all registrants" type or if the type is
8370 * different then we have a conflict
8372 if ((lun->pr_res_idx != residx
8373 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8374 || lun->pr_res_type != type) {
8375 mtx_unlock(&lun->lun_lock);
8376 free(ctsio->kern_data_ptr, M_CTL);
8377 ctl_set_reservation_conflict(ctsio);
8378 ctl_done((union ctl_io *)ctsio);
8379 return (CTL_RETVAL_COMPLETE);
8381 mtx_unlock(&lun->lun_lock);
8382 } else /* create a reservation */ {
8384 * If it's not an "all registrants" type record
8385 * reservation holder
8387 if (type != SPR_TYPE_WR_EX_AR
8388 && type != SPR_TYPE_EX_AC_AR)
8389 lun->pr_res_idx = residx; /* Res holder */
8391 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8393 lun->flags |= CTL_LUN_PR_RESERVED;
8394 lun->pr_res_type = type;
8396 mtx_unlock(&lun->lun_lock);
8398 /* send msg to other side */
8399 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8400 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8401 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8402 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8403 persis_io.pr.pr_info.res_type = type;
8404 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8405 sizeof(persis_io.pr), M_WAITOK);
8410 mtx_lock(&lun->lun_lock);
8411 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8412 /* No reservation exists return good status */
8413 mtx_unlock(&lun->lun_lock);
8417 * Is this nexus a reservation holder?
8419 if (lun->pr_res_idx != residx
8420 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8422 * not a res holder return good status but
8425 mtx_unlock(&lun->lun_lock);
8429 if (lun->pr_res_type != type) {
8430 mtx_unlock(&lun->lun_lock);
8431 free(ctsio->kern_data_ptr, M_CTL);
8432 ctl_set_illegal_pr_release(ctsio);
8433 ctl_done((union ctl_io *)ctsio);
8434 return (CTL_RETVAL_COMPLETE);
8437 /* okay to release */
8438 lun->flags &= ~CTL_LUN_PR_RESERVED;
8439 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8440 lun->pr_res_type = 0;
8443 * If this isn't an exclusive access reservation and NUAR
8444 * is not set, generate UA for all other registrants.
8446 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX &&
8447 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8448 for (i = softc->init_min; i < softc->init_max; i++) {
8449 if (i == residx || ctl_get_prkey(lun, i) == 0)
8451 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8454 mtx_unlock(&lun->lun_lock);
8456 /* Send msg to other side */
8457 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8458 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8459 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8460 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8461 sizeof(persis_io.pr), M_WAITOK);
8465 /* send msg to other side */
8467 mtx_lock(&lun->lun_lock);
8468 lun->flags &= ~CTL_LUN_PR_RESERVED;
8469 lun->pr_res_type = 0;
8470 lun->pr_key_count = 0;
8471 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8473 ctl_clr_prkey(lun, residx);
8474 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8475 if (ctl_get_prkey(lun, i) != 0) {
8476 ctl_clr_prkey(lun, i);
8477 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8479 lun->pr_generation++;
8480 mtx_unlock(&lun->lun_lock);
8482 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8483 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8484 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8485 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8486 sizeof(persis_io.pr), M_WAITOK);
8490 case SPRO_PRE_ABO: {
8493 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8494 residx, ctsio, cdb, param);
8496 return (CTL_RETVAL_COMPLETE);
8500 panic("%s: Invalid PR type %#x", __func__, cdb->action);
8504 free(ctsio->kern_data_ptr, M_CTL);
8505 ctl_set_success(ctsio);
8506 ctl_done((union ctl_io *)ctsio);
8512 * This routine is for handling a message from the other SC pertaining to
8513 * persistent reserve out. All the error checking will have been done
8514 * so only perorming the action need be done here to keep the two
8518 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8520 struct ctl_softc *softc = control_softc;
8521 struct ctl_lun *lun;
8523 uint32_t residx, targ_lun;
8525 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8526 mtx_lock(&softc->ctl_lock);
8527 if ((targ_lun >= CTL_MAX_LUNS) ||
8528 ((lun = softc->ctl_luns[targ_lun]) == NULL)) {
8529 mtx_unlock(&softc->ctl_lock);
8532 mtx_lock(&lun->lun_lock);
8533 mtx_unlock(&softc->ctl_lock);
8534 if (lun->flags & CTL_LUN_DISABLED) {
8535 mtx_unlock(&lun->lun_lock);
8538 residx = ctl_get_initindex(&msg->hdr.nexus);
8539 switch(msg->pr.pr_info.action) {
8540 case CTL_PR_REG_KEY:
8541 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8542 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8543 lun->pr_key_count++;
8544 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8545 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8546 lun->pr_generation++;
8549 case CTL_PR_UNREG_KEY:
8550 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8551 lun->pr_key_count--;
8553 /* XXX Need to see if the reservation has been released */
8554 /* if so do we need to generate UA? */
8555 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8556 lun->flags &= ~CTL_LUN_PR_RESERVED;
8557 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8559 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8560 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8561 lun->pr_key_count) {
8563 * If the reservation is a registrants
8564 * only type we need to generate a UA
8565 * for other registered inits. The
8566 * sense code should be RESERVATIONS
8570 for (i = softc->init_min; i < softc->init_max; i++) {
8571 if (ctl_get_prkey(lun, i) == 0)
8574 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8577 lun->pr_res_type = 0;
8578 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8579 if (lun->pr_key_count==0) {
8580 lun->flags &= ~CTL_LUN_PR_RESERVED;
8581 lun->pr_res_type = 0;
8582 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8585 lun->pr_generation++;
8588 case CTL_PR_RESERVE:
8589 lun->flags |= CTL_LUN_PR_RESERVED;
8590 lun->pr_res_type = msg->pr.pr_info.res_type;
8591 lun->pr_res_idx = msg->pr.pr_info.residx;
8595 case CTL_PR_RELEASE:
8597 * If this isn't an exclusive access reservation and NUAR
8598 * is not set, generate UA for all other registrants.
8600 if (lun->pr_res_type != SPR_TYPE_EX_AC &&
8601 lun->pr_res_type != SPR_TYPE_WR_EX &&
8602 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8603 for (i = softc->init_min; i < softc->init_max; i++)
8604 if (i == residx || ctl_get_prkey(lun, i) == 0)
8606 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8609 lun->flags &= ~CTL_LUN_PR_RESERVED;
8610 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8611 lun->pr_res_type = 0;
8614 case CTL_PR_PREEMPT:
8615 ctl_pro_preempt_other(lun, msg);
8618 lun->flags &= ~CTL_LUN_PR_RESERVED;
8619 lun->pr_res_type = 0;
8620 lun->pr_key_count = 0;
8621 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8623 for (i=0; i < CTL_MAX_INITIATORS; i++) {
8624 if (ctl_get_prkey(lun, i) == 0)
8626 ctl_clr_prkey(lun, i);
8627 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8629 lun->pr_generation++;
8633 mtx_unlock(&lun->lun_lock);
8637 ctl_read_write(struct ctl_scsiio *ctsio)
8639 struct ctl_lun *lun;
8640 struct ctl_lba_len_flags *lbalen;
8642 uint32_t num_blocks;
8646 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8648 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8651 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8652 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8653 switch (ctsio->cdb[0]) {
8656 struct scsi_rw_6 *cdb;
8658 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8660 lba = scsi_3btoul(cdb->addr);
8661 /* only 5 bits are valid in the most significant address byte */
8663 num_blocks = cdb->length;
8665 * This is correct according to SBC-2.
8667 if (num_blocks == 0)
8673 struct scsi_rw_10 *cdb;
8675 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8676 if (cdb->byte2 & SRW10_FUA)
8677 flags |= CTL_LLF_FUA;
8678 if (cdb->byte2 & SRW10_DPO)
8679 flags |= CTL_LLF_DPO;
8680 lba = scsi_4btoul(cdb->addr);
8681 num_blocks = scsi_2btoul(cdb->length);
8684 case WRITE_VERIFY_10: {
8685 struct scsi_write_verify_10 *cdb;
8687 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8688 flags |= CTL_LLF_FUA;
8689 if (cdb->byte2 & SWV_DPO)
8690 flags |= CTL_LLF_DPO;
8691 lba = scsi_4btoul(cdb->addr);
8692 num_blocks = scsi_2btoul(cdb->length);
8697 struct scsi_rw_12 *cdb;
8699 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8700 if (cdb->byte2 & SRW12_FUA)
8701 flags |= CTL_LLF_FUA;
8702 if (cdb->byte2 & SRW12_DPO)
8703 flags |= CTL_LLF_DPO;
8704 lba = scsi_4btoul(cdb->addr);
8705 num_blocks = scsi_4btoul(cdb->length);
8708 case WRITE_VERIFY_12: {
8709 struct scsi_write_verify_12 *cdb;
8711 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8712 flags |= CTL_LLF_FUA;
8713 if (cdb->byte2 & SWV_DPO)
8714 flags |= CTL_LLF_DPO;
8715 lba = scsi_4btoul(cdb->addr);
8716 num_blocks = scsi_4btoul(cdb->length);
8721 struct scsi_rw_16 *cdb;
8723 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8724 if (cdb->byte2 & SRW12_FUA)
8725 flags |= CTL_LLF_FUA;
8726 if (cdb->byte2 & SRW12_DPO)
8727 flags |= CTL_LLF_DPO;
8728 lba = scsi_8btou64(cdb->addr);
8729 num_blocks = scsi_4btoul(cdb->length);
8732 case WRITE_ATOMIC_16: {
8733 struct scsi_write_atomic_16 *cdb;
8735 if (lun->be_lun->atomicblock == 0) {
8736 ctl_set_invalid_opcode(ctsio);
8737 ctl_done((union ctl_io *)ctsio);
8738 return (CTL_RETVAL_COMPLETE);
8741 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb;
8742 if (cdb->byte2 & SRW12_FUA)
8743 flags |= CTL_LLF_FUA;
8744 if (cdb->byte2 & SRW12_DPO)
8745 flags |= CTL_LLF_DPO;
8746 lba = scsi_8btou64(cdb->addr);
8747 num_blocks = scsi_2btoul(cdb->length);
8748 if (num_blocks > lun->be_lun->atomicblock) {
8749 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8750 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8752 ctl_done((union ctl_io *)ctsio);
8753 return (CTL_RETVAL_COMPLETE);
8757 case WRITE_VERIFY_16: {
8758 struct scsi_write_verify_16 *cdb;
8760 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8761 flags |= CTL_LLF_FUA;
8762 if (cdb->byte2 & SWV_DPO)
8763 flags |= CTL_LLF_DPO;
8764 lba = scsi_8btou64(cdb->addr);
8765 num_blocks = scsi_4btoul(cdb->length);
8770 * We got a command we don't support. This shouldn't
8771 * happen, commands should be filtered out above us.
8773 ctl_set_invalid_opcode(ctsio);
8774 ctl_done((union ctl_io *)ctsio);
8776 return (CTL_RETVAL_COMPLETE);
8777 break; /* NOTREACHED */
8781 * The first check is to make sure we're in bounds, the second
8782 * check is to catch wrap-around problems. If the lba + num blocks
8783 * is less than the lba, then we've wrapped around and the block
8784 * range is invalid anyway.
8786 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8787 || ((lba + num_blocks) < lba)) {
8788 ctl_set_lba_out_of_range(ctsio);
8789 ctl_done((union ctl_io *)ctsio);
8790 return (CTL_RETVAL_COMPLETE);
8794 * According to SBC-3, a transfer length of 0 is not an error.
8795 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8796 * translates to 256 blocks for those commands.
8798 if (num_blocks == 0) {
8799 ctl_set_success(ctsio);
8800 ctl_done((union ctl_io *)ctsio);
8801 return (CTL_RETVAL_COMPLETE);
8804 /* Set FUA and/or DPO if caches are disabled. */
8806 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0)
8807 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8809 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8810 flags |= CTL_LLF_FUA;
8813 lbalen = (struct ctl_lba_len_flags *)
8814 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8816 lbalen->len = num_blocks;
8817 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8819 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8820 ctsio->kern_rel_offset = 0;
8822 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8824 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8829 ctl_cnw_cont(union ctl_io *io)
8831 struct ctl_scsiio *ctsio;
8832 struct ctl_lun *lun;
8833 struct ctl_lba_len_flags *lbalen;
8836 ctsio = &io->scsiio;
8837 ctsio->io_hdr.status = CTL_STATUS_NONE;
8838 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8839 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8840 lbalen = (struct ctl_lba_len_flags *)
8841 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8842 lbalen->flags &= ~CTL_LLF_COMPARE;
8843 lbalen->flags |= CTL_LLF_WRITE;
8845 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8846 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8851 ctl_cnw(struct ctl_scsiio *ctsio)
8853 struct ctl_lun *lun;
8854 struct ctl_lba_len_flags *lbalen;
8856 uint32_t num_blocks;
8859 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8861 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8864 switch (ctsio->cdb[0]) {
8865 case COMPARE_AND_WRITE: {
8866 struct scsi_compare_and_write *cdb;
8868 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8869 if (cdb->byte2 & SRW10_FUA)
8870 flags |= CTL_LLF_FUA;
8871 if (cdb->byte2 & SRW10_DPO)
8872 flags |= CTL_LLF_DPO;
8873 lba = scsi_8btou64(cdb->addr);
8874 num_blocks = cdb->length;
8879 * We got a command we don't support. This shouldn't
8880 * happen, commands should be filtered out above us.
8882 ctl_set_invalid_opcode(ctsio);
8883 ctl_done((union ctl_io *)ctsio);
8885 return (CTL_RETVAL_COMPLETE);
8886 break; /* NOTREACHED */
8890 * The first check is to make sure we're in bounds, the second
8891 * check is to catch wrap-around problems. If the lba + num blocks
8892 * is less than the lba, then we've wrapped around and the block
8893 * range is invalid anyway.
8895 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8896 || ((lba + num_blocks) < lba)) {
8897 ctl_set_lba_out_of_range(ctsio);
8898 ctl_done((union ctl_io *)ctsio);
8899 return (CTL_RETVAL_COMPLETE);
8903 * According to SBC-3, a transfer length of 0 is not an error.
8905 if (num_blocks == 0) {
8906 ctl_set_success(ctsio);
8907 ctl_done((union ctl_io *)ctsio);
8908 return (CTL_RETVAL_COMPLETE);
8911 /* Set FUA if write cache is disabled. */
8912 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8913 flags |= CTL_LLF_FUA;
8915 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
8916 ctsio->kern_rel_offset = 0;
8919 * Set the IO_CONT flag, so that if this I/O gets passed to
8920 * ctl_data_submit_done(), it'll get passed back to
8921 * ctl_ctl_cnw_cont() for further processing.
8923 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
8924 ctsio->io_cont = ctl_cnw_cont;
8926 lbalen = (struct ctl_lba_len_flags *)
8927 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8929 lbalen->len = num_blocks;
8930 lbalen->flags = CTL_LLF_COMPARE | flags;
8932 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
8933 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8938 ctl_verify(struct ctl_scsiio *ctsio)
8940 struct ctl_lun *lun;
8941 struct ctl_lba_len_flags *lbalen;
8943 uint32_t num_blocks;
8947 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8949 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
8952 flags = CTL_LLF_FUA;
8953 switch (ctsio->cdb[0]) {
8955 struct scsi_verify_10 *cdb;
8957 cdb = (struct scsi_verify_10 *)ctsio->cdb;
8958 if (cdb->byte2 & SVFY_BYTCHK)
8960 if (cdb->byte2 & SVFY_DPO)
8961 flags |= CTL_LLF_DPO;
8962 lba = scsi_4btoul(cdb->addr);
8963 num_blocks = scsi_2btoul(cdb->length);
8967 struct scsi_verify_12 *cdb;
8969 cdb = (struct scsi_verify_12 *)ctsio->cdb;
8970 if (cdb->byte2 & SVFY_BYTCHK)
8972 if (cdb->byte2 & SVFY_DPO)
8973 flags |= CTL_LLF_DPO;
8974 lba = scsi_4btoul(cdb->addr);
8975 num_blocks = scsi_4btoul(cdb->length);
8979 struct scsi_rw_16 *cdb;
8981 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8982 if (cdb->byte2 & SVFY_BYTCHK)
8984 if (cdb->byte2 & SVFY_DPO)
8985 flags |= CTL_LLF_DPO;
8986 lba = scsi_8btou64(cdb->addr);
8987 num_blocks = scsi_4btoul(cdb->length);
8992 * We got a command we don't support. This shouldn't
8993 * happen, commands should be filtered out above us.
8995 ctl_set_invalid_opcode(ctsio);
8996 ctl_done((union ctl_io *)ctsio);
8997 return (CTL_RETVAL_COMPLETE);
9001 * The first check is to make sure we're in bounds, the second
9002 * check is to catch wrap-around problems. If the lba + num blocks
9003 * is less than the lba, then we've wrapped around and the block
9004 * range is invalid anyway.
9006 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9007 || ((lba + num_blocks) < lba)) {
9008 ctl_set_lba_out_of_range(ctsio);
9009 ctl_done((union ctl_io *)ctsio);
9010 return (CTL_RETVAL_COMPLETE);
9014 * According to SBC-3, a transfer length of 0 is not an error.
9016 if (num_blocks == 0) {
9017 ctl_set_success(ctsio);
9018 ctl_done((union ctl_io *)ctsio);
9019 return (CTL_RETVAL_COMPLETE);
9022 lbalen = (struct ctl_lba_len_flags *)
9023 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9025 lbalen->len = num_blocks;
9027 lbalen->flags = CTL_LLF_COMPARE | flags;
9028 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9030 lbalen->flags = CTL_LLF_VERIFY | flags;
9031 ctsio->kern_total_len = 0;
9033 ctsio->kern_rel_offset = 0;
9035 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9036 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9041 ctl_report_luns(struct ctl_scsiio *ctsio)
9043 struct ctl_softc *softc;
9044 struct scsi_report_luns *cdb;
9045 struct scsi_report_luns_data *lun_data;
9046 struct ctl_lun *lun, *request_lun;
9047 struct ctl_port *port;
9048 int num_luns, retval;
9049 uint32_t alloc_len, lun_datalen;
9051 uint32_t initidx, targ_lun_id, lun_id;
9053 retval = CTL_RETVAL_COMPLETE;
9054 cdb = (struct scsi_report_luns *)ctsio->cdb;
9055 port = ctl_io_port(&ctsio->io_hdr);
9056 softc = port->ctl_softc;
9058 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9060 mtx_lock(&softc->ctl_lock);
9062 for (targ_lun_id = 0; targ_lun_id < CTL_MAX_LUNS; targ_lun_id++) {
9063 if (ctl_lun_map_from_port(port, targ_lun_id) < CTL_MAX_LUNS)
9066 mtx_unlock(&softc->ctl_lock);
9068 switch (cdb->select_report) {
9069 case RPL_REPORT_DEFAULT:
9070 case RPL_REPORT_ALL:
9071 case RPL_REPORT_NONSUBSID:
9073 case RPL_REPORT_WELLKNOWN:
9074 case RPL_REPORT_ADMIN:
9075 case RPL_REPORT_CONGLOM:
9079 ctl_set_invalid_field(ctsio,
9085 ctl_done((union ctl_io *)ctsio);
9087 break; /* NOTREACHED */
9090 alloc_len = scsi_4btoul(cdb->length);
9092 * The initiator has to allocate at least 16 bytes for this request,
9093 * so he can at least get the header and the first LUN. Otherwise
9094 * we reject the request (per SPC-3 rev 14, section 6.21).
9096 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9097 sizeof(struct scsi_report_luns_lundata))) {
9098 ctl_set_invalid_field(ctsio,
9104 ctl_done((union ctl_io *)ctsio);
9108 request_lun = (struct ctl_lun *)
9109 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9111 lun_datalen = sizeof(*lun_data) +
9112 (num_luns * sizeof(struct scsi_report_luns_lundata));
9114 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9115 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9116 ctsio->kern_sg_entries = 0;
9118 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9120 mtx_lock(&softc->ctl_lock);
9121 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9122 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9123 if (lun_id >= CTL_MAX_LUNS)
9125 lun = softc->ctl_luns[lun_id];
9129 be64enc(lun_data->luns[num_filled++].lundata,
9130 ctl_encode_lun(targ_lun_id));
9133 * According to SPC-3, rev 14 section 6.21:
9135 * "The execution of a REPORT LUNS command to any valid and
9136 * installed logical unit shall clear the REPORTED LUNS DATA
9137 * HAS CHANGED unit attention condition for all logical
9138 * units of that target with respect to the requesting
9139 * initiator. A valid and installed logical unit is one
9140 * having a PERIPHERAL QUALIFIER of 000b in the standard
9141 * INQUIRY data (see 6.4.2)."
9143 * If request_lun is NULL, the LUN this report luns command
9144 * was issued to is either disabled or doesn't exist. In that
9145 * case, we shouldn't clear any pending lun change unit
9148 if (request_lun != NULL) {
9149 mtx_lock(&lun->lun_lock);
9150 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE);
9151 mtx_unlock(&lun->lun_lock);
9154 mtx_unlock(&softc->ctl_lock);
9157 * It's quite possible that we've returned fewer LUNs than we allocated
9158 * space for. Trim it.
9160 lun_datalen = sizeof(*lun_data) +
9161 (num_filled * sizeof(struct scsi_report_luns_lundata));
9163 if (lun_datalen < alloc_len) {
9164 ctsio->residual = alloc_len - lun_datalen;
9165 ctsio->kern_data_len = lun_datalen;
9166 ctsio->kern_total_len = lun_datalen;
9168 ctsio->residual = 0;
9169 ctsio->kern_data_len = alloc_len;
9170 ctsio->kern_total_len = alloc_len;
9172 ctsio->kern_data_resid = 0;
9173 ctsio->kern_rel_offset = 0;
9174 ctsio->kern_sg_entries = 0;
9177 * We set this to the actual data length, regardless of how much
9178 * space we actually have to return results. If the user looks at
9179 * this value, he'll know whether or not he allocated enough space
9180 * and reissue the command if necessary. We don't support well
9181 * known logical units, so if the user asks for that, return none.
9183 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9186 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9189 ctl_set_success(ctsio);
9190 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9191 ctsio->be_move_done = ctl_config_move_done;
9192 ctl_datamove((union ctl_io *)ctsio);
9197 ctl_request_sense(struct ctl_scsiio *ctsio)
9199 struct scsi_request_sense *cdb;
9200 struct scsi_sense_data *sense_ptr;
9201 struct ctl_softc *softc;
9202 struct ctl_lun *lun;
9205 scsi_sense_data_type sense_format;
9206 ctl_ua_type ua_type;
9207 uint8_t asc = 0, ascq = 0;
9209 cdb = (struct scsi_request_sense *)ctsio->cdb;
9211 softc = control_softc;
9212 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9214 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9217 * Determine which sense format the user wants.
9219 if (cdb->byte2 & SRS_DESC)
9220 sense_format = SSD_TYPE_DESC;
9222 sense_format = SSD_TYPE_FIXED;
9224 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9225 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9226 ctsio->kern_sg_entries = 0;
9229 * struct scsi_sense_data, which is currently set to 256 bytes, is
9230 * larger than the largest allowed value for the length field in the
9231 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9233 ctsio->residual = 0;
9234 ctsio->kern_data_len = cdb->length;
9235 ctsio->kern_total_len = cdb->length;
9237 ctsio->kern_data_resid = 0;
9238 ctsio->kern_rel_offset = 0;
9239 ctsio->kern_sg_entries = 0;
9242 * If we don't have a LUN, we don't have any pending sense.
9245 ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
9246 softc->ha_link < CTL_HA_LINK_UNKNOWN)) {
9247 /* "Logical unit not supported" */
9248 ctl_set_sense_data(sense_ptr, NULL, sense_format,
9249 /*current_error*/ 1,
9250 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
9258 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9260 * Check for pending sense, and then for pending unit attentions.
9261 * Pending sense gets returned first, then pending unit attentions.
9263 mtx_lock(&lun->lun_lock);
9265 if (ctl_is_set(lun->have_ca, initidx)) {
9266 scsi_sense_data_type stored_format;
9269 * Check to see which sense format was used for the stored
9272 stored_format = scsi_sense_type(&lun->pending_sense[initidx]);
9275 * If the user requested a different sense format than the
9276 * one we stored, then we need to convert it to the other
9277 * format. If we're going from descriptor to fixed format
9278 * sense data, we may lose things in translation, depending
9279 * on what options were used.
9281 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9282 * for some reason we'll just copy it out as-is.
9284 if ((stored_format == SSD_TYPE_FIXED)
9285 && (sense_format == SSD_TYPE_DESC))
9286 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9287 &lun->pending_sense[initidx],
9288 (struct scsi_sense_data_desc *)sense_ptr);
9289 else if ((stored_format == SSD_TYPE_DESC)
9290 && (sense_format == SSD_TYPE_FIXED))
9291 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9292 &lun->pending_sense[initidx],
9293 (struct scsi_sense_data_fixed *)sense_ptr);
9295 memcpy(sense_ptr, &lun->pending_sense[initidx],
9296 MIN(sizeof(*sense_ptr),
9297 sizeof(lun->pending_sense[initidx])));
9299 ctl_clear_mask(lun->have_ca, initidx);
9303 if (have_error == 0) {
9304 ua_type = ctl_build_ua(lun, initidx, sense_ptr, sense_format);
9305 if (ua_type != CTL_UA_NONE)
9307 if (ua_type == CTL_UA_LUN_CHANGE) {
9308 mtx_unlock(&lun->lun_lock);
9309 mtx_lock(&softc->ctl_lock);
9310 ctl_clr_ua_allluns(softc, initidx, ua_type);
9311 mtx_unlock(&softc->ctl_lock);
9312 mtx_lock(&lun->lun_lock);
9315 if (have_error == 0) {
9317 * Report informational exception if have one and allowed.
9319 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) {
9321 ascq = lun->ie_ascq;
9323 ctl_set_sense_data(sense_ptr, lun, sense_format,
9324 /*current_error*/ 1,
9325 /*sense_key*/ SSD_KEY_NO_SENSE,
9330 mtx_unlock(&lun->lun_lock);
9334 * We report the SCSI status as OK, since the status of the command
9335 * itself is OK. We're reporting sense as parameter data.
9337 ctl_set_success(ctsio);
9338 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9339 ctsio->be_move_done = ctl_config_move_done;
9340 ctl_datamove((union ctl_io *)ctsio);
9341 return (CTL_RETVAL_COMPLETE);
9345 ctl_tur(struct ctl_scsiio *ctsio)
9348 CTL_DEBUG_PRINT(("ctl_tur\n"));
9350 ctl_set_success(ctsio);
9351 ctl_done((union ctl_io *)ctsio);
9353 return (CTL_RETVAL_COMPLETE);
9357 * SCSI VPD page 0x00, the Supported VPD Pages page.
9360 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9362 struct scsi_vpd_supported_pages *pages;
9364 struct ctl_lun *lun;
9367 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9369 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9370 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9371 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9372 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9373 ctsio->kern_sg_entries = 0;
9375 if (sup_page_size < alloc_len) {
9376 ctsio->residual = alloc_len - sup_page_size;
9377 ctsio->kern_data_len = sup_page_size;
9378 ctsio->kern_total_len = sup_page_size;
9380 ctsio->residual = 0;
9381 ctsio->kern_data_len = alloc_len;
9382 ctsio->kern_total_len = alloc_len;
9384 ctsio->kern_data_resid = 0;
9385 ctsio->kern_rel_offset = 0;
9386 ctsio->kern_sg_entries = 0;
9389 * The control device is always connected. The disk device, on the
9390 * other hand, may not be online all the time. Need to change this
9391 * to figure out whether the disk device is actually online or not.
9394 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9395 lun->be_lun->lun_type;
9397 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9400 /* Supported VPD pages */
9401 pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9403 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9404 /* Device Identification */
9405 pages->page_list[p++] = SVPD_DEVICE_ID;
9406 /* Extended INQUIRY Data */
9407 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9408 /* Mode Page Policy */
9409 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9411 pages->page_list[p++] = SVPD_SCSI_PORTS;
9412 /* Third-party Copy */
9413 pages->page_list[p++] = SVPD_SCSI_TPC;
9414 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9416 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9417 /* Block Device Characteristics */
9418 pages->page_list[p++] = SVPD_BDC;
9419 /* Logical Block Provisioning */
9420 pages->page_list[p++] = SVPD_LBP;
9424 ctl_set_success(ctsio);
9425 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9426 ctsio->be_move_done = ctl_config_move_done;
9427 ctl_datamove((union ctl_io *)ctsio);
9428 return (CTL_RETVAL_COMPLETE);
9432 * SCSI VPD page 0x80, the Unit Serial Number page.
9435 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9437 struct scsi_vpd_unit_serial_number *sn_ptr;
9438 struct ctl_lun *lun;
9441 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9443 data_len = 4 + CTL_SN_LEN;
9444 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9445 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9446 if (data_len < alloc_len) {
9447 ctsio->residual = alloc_len - data_len;
9448 ctsio->kern_data_len = data_len;
9449 ctsio->kern_total_len = data_len;
9451 ctsio->residual = 0;
9452 ctsio->kern_data_len = alloc_len;
9453 ctsio->kern_total_len = alloc_len;
9455 ctsio->kern_data_resid = 0;
9456 ctsio->kern_rel_offset = 0;
9457 ctsio->kern_sg_entries = 0;
9460 * The control device is always connected. The disk device, on the
9461 * other hand, may not be online all the time. Need to change this
9462 * to figure out whether the disk device is actually online or not.
9465 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9466 lun->be_lun->lun_type;
9468 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9470 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9471 sn_ptr->length = CTL_SN_LEN;
9473 * If we don't have a LUN, we just leave the serial number as
9477 strncpy((char *)sn_ptr->serial_num,
9478 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9480 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9482 ctl_set_success(ctsio);
9483 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9484 ctsio->be_move_done = ctl_config_move_done;
9485 ctl_datamove((union ctl_io *)ctsio);
9486 return (CTL_RETVAL_COMPLETE);
9491 * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9494 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9496 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9497 struct ctl_lun *lun;
9500 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9502 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9503 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9504 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9505 ctsio->kern_sg_entries = 0;
9507 if (data_len < alloc_len) {
9508 ctsio->residual = alloc_len - data_len;
9509 ctsio->kern_data_len = data_len;
9510 ctsio->kern_total_len = data_len;
9512 ctsio->residual = 0;
9513 ctsio->kern_data_len = alloc_len;
9514 ctsio->kern_total_len = alloc_len;
9516 ctsio->kern_data_resid = 0;
9517 ctsio->kern_rel_offset = 0;
9518 ctsio->kern_sg_entries = 0;
9521 * The control device is always connected. The disk device, on the
9522 * other hand, may not be online all the time.
9525 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9526 lun->be_lun->lun_type;
9528 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9529 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9530 scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9532 * We support head of queue, ordered and simple tags.
9534 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9536 * Volatile cache supported.
9538 eid_ptr->flags3 = SVPD_EID_V_SUP;
9541 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9542 * attention for a particular IT nexus on all LUNs once we report
9543 * it to that nexus once. This bit is required as of SPC-4.
9545 eid_ptr->flags4 = SVPD_EID_LUICLT;
9548 * XXX KDM in order to correctly answer this, we would need
9549 * information from the SIM to determine how much sense data it
9550 * can send. So this would really be a path inquiry field, most
9551 * likely. This can be set to a maximum of 252 according to SPC-4,
9552 * but the hardware may or may not be able to support that much.
9553 * 0 just means that the maximum sense data length is not reported.
9555 eid_ptr->max_sense_length = 0;
9557 ctl_set_success(ctsio);
9558 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9559 ctsio->be_move_done = ctl_config_move_done;
9560 ctl_datamove((union ctl_io *)ctsio);
9561 return (CTL_RETVAL_COMPLETE);
9565 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9567 struct scsi_vpd_mode_page_policy *mpp_ptr;
9568 struct ctl_lun *lun;
9571 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9573 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9574 sizeof(struct scsi_vpd_mode_page_policy_descr);
9576 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9577 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9578 ctsio->kern_sg_entries = 0;
9580 if (data_len < alloc_len) {
9581 ctsio->residual = alloc_len - data_len;
9582 ctsio->kern_data_len = data_len;
9583 ctsio->kern_total_len = data_len;
9585 ctsio->residual = 0;
9586 ctsio->kern_data_len = alloc_len;
9587 ctsio->kern_total_len = alloc_len;
9589 ctsio->kern_data_resid = 0;
9590 ctsio->kern_rel_offset = 0;
9591 ctsio->kern_sg_entries = 0;
9594 * The control device is always connected. The disk device, on the
9595 * other hand, may not be online all the time.
9598 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9599 lun->be_lun->lun_type;
9601 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9602 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9603 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9604 mpp_ptr->descr[0].page_code = 0x3f;
9605 mpp_ptr->descr[0].subpage_code = 0xff;
9606 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9608 ctl_set_success(ctsio);
9609 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9610 ctsio->be_move_done = ctl_config_move_done;
9611 ctl_datamove((union ctl_io *)ctsio);
9612 return (CTL_RETVAL_COMPLETE);
9616 * SCSI VPD page 0x83, the Device Identification page.
9619 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9621 struct scsi_vpd_device_id *devid_ptr;
9622 struct scsi_vpd_id_descriptor *desc;
9623 struct ctl_softc *softc;
9624 struct ctl_lun *lun;
9625 struct ctl_port *port;
9629 softc = control_softc;
9631 port = ctl_io_port(&ctsio->io_hdr);
9632 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9634 data_len = sizeof(struct scsi_vpd_device_id) +
9635 sizeof(struct scsi_vpd_id_descriptor) +
9636 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9637 sizeof(struct scsi_vpd_id_descriptor) +
9638 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9639 if (lun && lun->lun_devid)
9640 data_len += lun->lun_devid->len;
9641 if (port && port->port_devid)
9642 data_len += port->port_devid->len;
9643 if (port && port->target_devid)
9644 data_len += port->target_devid->len;
9646 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9647 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9648 ctsio->kern_sg_entries = 0;
9650 if (data_len < alloc_len) {
9651 ctsio->residual = alloc_len - data_len;
9652 ctsio->kern_data_len = data_len;
9653 ctsio->kern_total_len = data_len;
9655 ctsio->residual = 0;
9656 ctsio->kern_data_len = alloc_len;
9657 ctsio->kern_total_len = alloc_len;
9659 ctsio->kern_data_resid = 0;
9660 ctsio->kern_rel_offset = 0;
9661 ctsio->kern_sg_entries = 0;
9664 * The control device is always connected. The disk device, on the
9665 * other hand, may not be online all the time.
9668 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9669 lun->be_lun->lun_type;
9671 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9672 devid_ptr->page_code = SVPD_DEVICE_ID;
9673 scsi_ulto2b(data_len - 4, devid_ptr->length);
9675 if (port && port->port_type == CTL_PORT_FC)
9676 proto = SCSI_PROTO_FC << 4;
9677 else if (port && port->port_type == CTL_PORT_ISCSI)
9678 proto = SCSI_PROTO_ISCSI << 4;
9680 proto = SCSI_PROTO_SPI << 4;
9681 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9684 * We're using a LUN association here. i.e., this device ID is a
9685 * per-LUN identifier.
9687 if (lun && lun->lun_devid) {
9688 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9689 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9690 lun->lun_devid->len);
9694 * This is for the WWPN which is a port association.
9696 if (port && port->port_devid) {
9697 memcpy(desc, port->port_devid->data, port->port_devid->len);
9698 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9699 port->port_devid->len);
9703 * This is for the Relative Target Port(type 4h) identifier
9705 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9706 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9707 SVPD_ID_TYPE_RELTARG;
9709 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9710 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9711 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9714 * This is for the Target Port Group(type 5h) identifier
9716 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9717 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9718 SVPD_ID_TYPE_TPORTGRP;
9720 if (softc->is_single ||
9721 (port && port->status & CTL_PORT_STATUS_HA_SHARED))
9724 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt;
9725 scsi_ulto2b(g, &desc->identifier[2]);
9726 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9727 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9730 * This is for the Target identifier
9732 if (port && port->target_devid) {
9733 memcpy(desc, port->target_devid->data, port->target_devid->len);
9736 ctl_set_success(ctsio);
9737 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9738 ctsio->be_move_done = ctl_config_move_done;
9739 ctl_datamove((union ctl_io *)ctsio);
9740 return (CTL_RETVAL_COMPLETE);
9744 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9746 struct ctl_softc *softc = control_softc;
9747 struct scsi_vpd_scsi_ports *sp;
9748 struct scsi_vpd_port_designation *pd;
9749 struct scsi_vpd_port_designation_cont *pdc;
9750 struct ctl_lun *lun;
9751 struct ctl_port *port;
9752 int data_len, num_target_ports, iid_len, id_len;
9754 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9756 num_target_ports = 0;
9759 mtx_lock(&softc->ctl_lock);
9760 STAILQ_FOREACH(port, &softc->port_list, links) {
9761 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9764 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
9767 if (port->init_devid)
9768 iid_len += port->init_devid->len;
9769 if (port->port_devid)
9770 id_len += port->port_devid->len;
9772 mtx_unlock(&softc->ctl_lock);
9774 data_len = sizeof(struct scsi_vpd_scsi_ports) +
9775 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9776 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9777 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9778 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9779 ctsio->kern_sg_entries = 0;
9781 if (data_len < alloc_len) {
9782 ctsio->residual = alloc_len - data_len;
9783 ctsio->kern_data_len = data_len;
9784 ctsio->kern_total_len = data_len;
9786 ctsio->residual = 0;
9787 ctsio->kern_data_len = alloc_len;
9788 ctsio->kern_total_len = alloc_len;
9790 ctsio->kern_data_resid = 0;
9791 ctsio->kern_rel_offset = 0;
9792 ctsio->kern_sg_entries = 0;
9795 * The control device is always connected. The disk device, on the
9796 * other hand, may not be online all the time. Need to change this
9797 * to figure out whether the disk device is actually online or not.
9800 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9801 lun->be_lun->lun_type;
9803 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9805 sp->page_code = SVPD_SCSI_PORTS;
9806 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9808 pd = &sp->design[0];
9810 mtx_lock(&softc->ctl_lock);
9811 STAILQ_FOREACH(port, &softc->port_list, links) {
9812 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9815 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
9817 scsi_ulto2b(port->targ_port, pd->relative_port_id);
9818 if (port->init_devid) {
9819 iid_len = port->init_devid->len;
9820 memcpy(pd->initiator_transportid,
9821 port->init_devid->data, port->init_devid->len);
9824 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9825 pdc = (struct scsi_vpd_port_designation_cont *)
9826 (&pd->initiator_transportid[iid_len]);
9827 if (port->port_devid) {
9828 id_len = port->port_devid->len;
9829 memcpy(pdc->target_port_descriptors,
9830 port->port_devid->data, port->port_devid->len);
9833 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9834 pd = (struct scsi_vpd_port_designation *)
9835 ((uint8_t *)pdc->target_port_descriptors + id_len);
9837 mtx_unlock(&softc->ctl_lock);
9839 ctl_set_success(ctsio);
9840 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9841 ctsio->be_move_done = ctl_config_move_done;
9842 ctl_datamove((union ctl_io *)ctsio);
9843 return (CTL_RETVAL_COMPLETE);
9847 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9849 struct scsi_vpd_block_limits *bl_ptr;
9850 struct ctl_lun *lun;
9853 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9855 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9856 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9857 ctsio->kern_sg_entries = 0;
9859 if (sizeof(*bl_ptr) < alloc_len) {
9860 ctsio->residual = alloc_len - sizeof(*bl_ptr);
9861 ctsio->kern_data_len = sizeof(*bl_ptr);
9862 ctsio->kern_total_len = sizeof(*bl_ptr);
9864 ctsio->residual = 0;
9865 ctsio->kern_data_len = alloc_len;
9866 ctsio->kern_total_len = alloc_len;
9868 ctsio->kern_data_resid = 0;
9869 ctsio->kern_rel_offset = 0;
9870 ctsio->kern_sg_entries = 0;
9873 * The control device is always connected. The disk device, on the
9874 * other hand, may not be online all the time. Need to change this
9875 * to figure out whether the disk device is actually online or not.
9878 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9879 lun->be_lun->lun_type;
9881 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9883 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9884 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
9885 bl_ptr->max_cmp_write_len = 0xff;
9886 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9888 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
9889 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9891 ctl_get_opt_number(&lun->be_lun->options,
9892 "unmap_max_lba", &ival);
9893 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt);
9895 ctl_get_opt_number(&lun->be_lun->options,
9896 "unmap_max_descr", &ival);
9897 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt);
9898 if (lun->be_lun->ublockexp != 0) {
9899 scsi_ulto4b((1 << lun->be_lun->ublockexp),
9900 bl_ptr->opt_unmap_grain);
9901 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
9902 bl_ptr->unmap_grain_align);
9905 scsi_ulto4b(lun->be_lun->atomicblock,
9906 bl_ptr->max_atomic_transfer_length);
9907 scsi_ulto4b(0, bl_ptr->atomic_alignment);
9908 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
9909 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary);
9910 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size);
9912 ctl_get_opt_number(&lun->be_lun->options, "write_same_max_lba", &ival);
9913 scsi_u64to8b(ival, bl_ptr->max_write_same_length);
9916 ctl_set_success(ctsio);
9917 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9918 ctsio->be_move_done = ctl_config_move_done;
9919 ctl_datamove((union ctl_io *)ctsio);
9920 return (CTL_RETVAL_COMPLETE);
9924 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
9926 struct scsi_vpd_block_device_characteristics *bdc_ptr;
9927 struct ctl_lun *lun;
9931 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9933 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
9934 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
9935 ctsio->kern_sg_entries = 0;
9937 if (sizeof(*bdc_ptr) < alloc_len) {
9938 ctsio->residual = alloc_len - sizeof(*bdc_ptr);
9939 ctsio->kern_data_len = sizeof(*bdc_ptr);
9940 ctsio->kern_total_len = sizeof(*bdc_ptr);
9942 ctsio->residual = 0;
9943 ctsio->kern_data_len = alloc_len;
9944 ctsio->kern_total_len = alloc_len;
9946 ctsio->kern_data_resid = 0;
9947 ctsio->kern_rel_offset = 0;
9948 ctsio->kern_sg_entries = 0;
9951 * The control device is always connected. The disk device, on the
9952 * other hand, may not be online all the time. Need to change this
9953 * to figure out whether the disk device is actually online or not.
9956 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9957 lun->be_lun->lun_type;
9959 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9960 bdc_ptr->page_code = SVPD_BDC;
9961 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
9963 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL)
9964 i = strtol(value, NULL, 0);
9966 i = CTL_DEFAULT_ROTATION_RATE;
9967 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
9969 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL)
9970 i = strtol(value, NULL, 0);
9973 bdc_ptr->wab_wac_ff = (i & 0x0f);
9974 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
9976 ctl_set_success(ctsio);
9977 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9978 ctsio->be_move_done = ctl_config_move_done;
9979 ctl_datamove((union ctl_io *)ctsio);
9980 return (CTL_RETVAL_COMPLETE);
9984 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
9986 struct scsi_vpd_logical_block_prov *lbp_ptr;
9987 struct ctl_lun *lun;
9990 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9992 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
9993 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
9994 ctsio->kern_sg_entries = 0;
9996 if (sizeof(*lbp_ptr) < alloc_len) {
9997 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
9998 ctsio->kern_data_len = sizeof(*lbp_ptr);
9999 ctsio->kern_total_len = sizeof(*lbp_ptr);
10001 ctsio->residual = 0;
10002 ctsio->kern_data_len = alloc_len;
10003 ctsio->kern_total_len = alloc_len;
10005 ctsio->kern_data_resid = 0;
10006 ctsio->kern_rel_offset = 0;
10007 ctsio->kern_sg_entries = 0;
10010 * The control device is always connected. The disk device, on the
10011 * other hand, may not be online all the time. Need to change this
10012 * to figure out whether the disk device is actually online or not.
10015 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10016 lun->be_lun->lun_type;
10018 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10020 lbp_ptr->page_code = SVPD_LBP;
10021 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
10022 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
10023 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10024 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
10025 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
10026 value = ctl_get_opt(&lun->be_lun->options, "provisioning_type");
10027 if (value != NULL) {
10028 if (strcmp(value, "resource") == 0)
10029 lbp_ptr->prov_type = SVPD_LBP_RESOURCE;
10030 else if (strcmp(value, "thin") == 0)
10031 lbp_ptr->prov_type = SVPD_LBP_THIN;
10033 lbp_ptr->prov_type = SVPD_LBP_THIN;
10036 ctl_set_success(ctsio);
10037 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10038 ctsio->be_move_done = ctl_config_move_done;
10039 ctl_datamove((union ctl_io *)ctsio);
10040 return (CTL_RETVAL_COMPLETE);
10044 * INQUIRY with the EVPD bit set.
10047 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
10049 struct ctl_lun *lun;
10050 struct scsi_inquiry *cdb;
10051 int alloc_len, retval;
10053 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10054 cdb = (struct scsi_inquiry *)ctsio->cdb;
10055 alloc_len = scsi_2btoul(cdb->length);
10057 switch (cdb->page_code) {
10058 case SVPD_SUPPORTED_PAGES:
10059 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
10061 case SVPD_UNIT_SERIAL_NUMBER:
10062 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
10064 case SVPD_DEVICE_ID:
10065 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
10067 case SVPD_EXTENDED_INQUIRY_DATA:
10068 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
10070 case SVPD_MODE_PAGE_POLICY:
10071 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
10073 case SVPD_SCSI_PORTS:
10074 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10076 case SVPD_SCSI_TPC:
10077 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10079 case SVPD_BLOCK_LIMITS:
10080 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10082 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10085 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10087 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10090 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10092 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10096 ctl_set_invalid_field(ctsio,
10102 ctl_done((union ctl_io *)ctsio);
10103 retval = CTL_RETVAL_COMPLETE;
10111 * Standard INQUIRY data.
10114 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10116 struct scsi_inquiry_data *inq_ptr;
10117 struct scsi_inquiry *cdb;
10118 struct ctl_softc *softc = control_softc;
10119 struct ctl_port *port;
10120 struct ctl_lun *lun;
10122 uint32_t alloc_len, data_len;
10123 ctl_port_type port_type;
10125 port = ctl_io_port(&ctsio->io_hdr);
10126 port_type = port->port_type;
10127 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10128 port_type = CTL_PORT_SCSI;
10130 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10131 cdb = (struct scsi_inquiry *)ctsio->cdb;
10132 alloc_len = scsi_2btoul(cdb->length);
10135 * We malloc the full inquiry data size here and fill it
10136 * in. If the user only asks for less, we'll give him
10139 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10140 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10141 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10142 ctsio->kern_sg_entries = 0;
10143 ctsio->kern_data_resid = 0;
10144 ctsio->kern_rel_offset = 0;
10146 if (data_len < alloc_len) {
10147 ctsio->residual = alloc_len - data_len;
10148 ctsio->kern_data_len = data_len;
10149 ctsio->kern_total_len = data_len;
10151 ctsio->residual = 0;
10152 ctsio->kern_data_len = alloc_len;
10153 ctsio->kern_total_len = alloc_len;
10157 if ((lun->flags & CTL_LUN_PRIMARY_SC) ||
10158 softc->ha_link >= CTL_HA_LINK_UNKNOWN) {
10159 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10160 lun->be_lun->lun_type;
10162 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) |
10163 lun->be_lun->lun_type;
10165 if (lun->flags & CTL_LUN_REMOVABLE)
10166 inq_ptr->dev_qual2 |= SID_RMB;
10168 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10170 /* RMB in byte 2 is 0 */
10171 inq_ptr->version = SCSI_REV_SPC4;
10174 * According to SAM-3, even if a device only supports a single
10175 * level of LUN addressing, it should still set the HISUP bit:
10177 * 4.9.1 Logical unit numbers overview
10179 * All logical unit number formats described in this standard are
10180 * hierarchical in structure even when only a single level in that
10181 * hierarchy is used. The HISUP bit shall be set to one in the
10182 * standard INQUIRY data (see SPC-2) when any logical unit number
10183 * format described in this standard is used. Non-hierarchical
10184 * formats are outside the scope of this standard.
10186 * Therefore we set the HiSup bit here.
10188 * The response format is 2, per SPC-3.
10190 inq_ptr->response_format = SID_HiSup | 2;
10192 inq_ptr->additional_length = data_len -
10193 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10194 CTL_DEBUG_PRINT(("additional_length = %d\n",
10195 inq_ptr->additional_length));
10197 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10198 if (port_type == CTL_PORT_SCSI)
10199 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10200 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10201 inq_ptr->flags = SID_CmdQue;
10202 if (port_type == CTL_PORT_SCSI)
10203 inq_ptr->flags |= SID_WBus16 | SID_Sync;
10206 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10207 * We have 8 bytes for the vendor name, and 16 bytes for the device
10208 * name and 4 bytes for the revision.
10210 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10211 "vendor")) == NULL) {
10212 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10214 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10215 strncpy(inq_ptr->vendor, val,
10216 min(sizeof(inq_ptr->vendor), strlen(val)));
10219 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10220 sizeof(inq_ptr->product));
10221 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10222 switch (lun->be_lun->lun_type) {
10224 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10225 sizeof(inq_ptr->product));
10228 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10229 sizeof(inq_ptr->product));
10232 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT,
10233 sizeof(inq_ptr->product));
10236 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10237 sizeof(inq_ptr->product));
10241 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10242 strncpy(inq_ptr->product, val,
10243 min(sizeof(inq_ptr->product), strlen(val)));
10247 * XXX make this a macro somewhere so it automatically gets
10248 * incremented when we make changes.
10250 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10251 "revision")) == NULL) {
10252 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10254 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10255 strncpy(inq_ptr->revision, val,
10256 min(sizeof(inq_ptr->revision), strlen(val)));
10260 * For parallel SCSI, we support double transition and single
10261 * transition clocking. We also support QAS (Quick Arbitration
10262 * and Selection) and Information Unit transfers on both the
10263 * control and array devices.
10265 if (port_type == CTL_PORT_SCSI)
10266 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10269 /* SAM-5 (no version claimed) */
10270 scsi_ulto2b(0x00A0, inq_ptr->version1);
10271 /* SPC-4 (no version claimed) */
10272 scsi_ulto2b(0x0460, inq_ptr->version2);
10273 if (port_type == CTL_PORT_FC) {
10274 /* FCP-2 ANSI INCITS.350:2003 */
10275 scsi_ulto2b(0x0917, inq_ptr->version3);
10276 } else if (port_type == CTL_PORT_SCSI) {
10277 /* SPI-4 ANSI INCITS.362:200x */
10278 scsi_ulto2b(0x0B56, inq_ptr->version3);
10279 } else if (port_type == CTL_PORT_ISCSI) {
10280 /* iSCSI (no version claimed) */
10281 scsi_ulto2b(0x0960, inq_ptr->version3);
10282 } else if (port_type == CTL_PORT_SAS) {
10283 /* SAS (no version claimed) */
10284 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10288 /* SBC-4 (no version claimed) */
10289 scsi_ulto2b(0x0600, inq_ptr->version4);
10291 switch (lun->be_lun->lun_type) {
10293 /* SBC-4 (no version claimed) */
10294 scsi_ulto2b(0x0600, inq_ptr->version4);
10299 /* MMC-6 (no version claimed) */
10300 scsi_ulto2b(0x04E0, inq_ptr->version4);
10307 ctl_set_success(ctsio);
10308 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10309 ctsio->be_move_done = ctl_config_move_done;
10310 ctl_datamove((union ctl_io *)ctsio);
10311 return (CTL_RETVAL_COMPLETE);
10315 ctl_inquiry(struct ctl_scsiio *ctsio)
10317 struct scsi_inquiry *cdb;
10320 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10322 cdb = (struct scsi_inquiry *)ctsio->cdb;
10323 if (cdb->byte2 & SI_EVPD)
10324 retval = ctl_inquiry_evpd(ctsio);
10325 else if (cdb->page_code == 0)
10326 retval = ctl_inquiry_std(ctsio);
10328 ctl_set_invalid_field(ctsio,
10334 ctl_done((union ctl_io *)ctsio);
10335 return (CTL_RETVAL_COMPLETE);
10342 ctl_get_config(struct ctl_scsiio *ctsio)
10344 struct scsi_get_config_header *hdr;
10345 struct scsi_get_config_feature *feature;
10346 struct scsi_get_config *cdb;
10347 struct ctl_lun *lun;
10348 uint32_t alloc_len, data_len;
10351 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10352 cdb = (struct scsi_get_config *)ctsio->cdb;
10353 rt = (cdb->rt & SGC_RT_MASK);
10354 starting = scsi_2btoul(cdb->starting_feature);
10355 alloc_len = scsi_2btoul(cdb->length);
10357 data_len = sizeof(struct scsi_get_config_header) +
10358 sizeof(struct scsi_get_config_feature) + 8 +
10359 sizeof(struct scsi_get_config_feature) + 8 +
10360 sizeof(struct scsi_get_config_feature) + 4 +
10361 sizeof(struct scsi_get_config_feature) + 4 +
10362 sizeof(struct scsi_get_config_feature) + 8 +
10363 sizeof(struct scsi_get_config_feature) +
10364 sizeof(struct scsi_get_config_feature) + 4 +
10365 sizeof(struct scsi_get_config_feature) + 4 +
10366 sizeof(struct scsi_get_config_feature) + 4 +
10367 sizeof(struct scsi_get_config_feature) + 4 +
10368 sizeof(struct scsi_get_config_feature) + 4 +
10369 sizeof(struct scsi_get_config_feature) + 4;
10370 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10371 ctsio->kern_sg_entries = 0;
10372 ctsio->kern_data_resid = 0;
10373 ctsio->kern_rel_offset = 0;
10375 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr;
10376 if (lun->flags & CTL_LUN_NO_MEDIA)
10377 scsi_ulto2b(0x0000, hdr->current_profile);
10379 scsi_ulto2b(0x0010, hdr->current_profile);
10380 feature = (struct scsi_get_config_feature *)(hdr + 1);
10382 if (starting > 0x003b)
10384 if (starting > 0x003a)
10386 if (starting > 0x002b)
10388 if (starting > 0x002a)
10390 if (starting > 0x001f)
10392 if (starting > 0x001e)
10394 if (starting > 0x001d)
10396 if (starting > 0x0010)
10398 if (starting > 0x0003)
10400 if (starting > 0x0002)
10402 if (starting > 0x0001)
10404 if (starting > 0x0000)
10408 scsi_ulto2b(0x0000, feature->feature_code);
10409 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT;
10410 feature->add_length = 8;
10411 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */
10412 feature->feature_data[2] = 0x00;
10413 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */
10414 feature->feature_data[6] = 0x01;
10415 feature = (struct scsi_get_config_feature *)
10416 &feature->feature_data[feature->add_length];
10419 scsi_ulto2b(0x0001, feature->feature_code);
10420 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10421 feature->add_length = 8;
10422 scsi_ulto4b(0x00000000, &feature->feature_data[0]);
10423 feature->feature_data[4] = 0x03;
10424 feature = (struct scsi_get_config_feature *)
10425 &feature->feature_data[feature->add_length];
10428 scsi_ulto2b(0x0002, feature->feature_code);
10429 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10430 feature->add_length = 4;
10431 feature->feature_data[0] = 0x02;
10432 feature = (struct scsi_get_config_feature *)
10433 &feature->feature_data[feature->add_length];
10435 f3: /* Removable Medium */
10436 scsi_ulto2b(0x0003, feature->feature_code);
10437 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10438 feature->add_length = 4;
10439 feature->feature_data[0] = 0x39;
10440 feature = (struct scsi_get_config_feature *)
10441 &feature->feature_data[feature->add_length];
10443 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA))
10446 f10: /* Random Read */
10447 scsi_ulto2b(0x0010, feature->feature_code);
10448 feature->flags = 0x00;
10449 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10450 feature->flags |= SGC_F_CURRENT;
10451 feature->add_length = 8;
10452 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]);
10453 scsi_ulto2b(1, &feature->feature_data[4]);
10454 feature->feature_data[6] = 0x00;
10455 feature = (struct scsi_get_config_feature *)
10456 &feature->feature_data[feature->add_length];
10458 f1d: /* Multi-Read */
10459 scsi_ulto2b(0x001D, feature->feature_code);
10460 feature->flags = 0x00;
10461 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10462 feature->flags |= SGC_F_CURRENT;
10463 feature->add_length = 0;
10464 feature = (struct scsi_get_config_feature *)
10465 &feature->feature_data[feature->add_length];
10468 scsi_ulto2b(0x001E, feature->feature_code);
10469 feature->flags = 0x00;
10470 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10471 feature->flags |= SGC_F_CURRENT;
10472 feature->add_length = 4;
10473 feature->feature_data[0] = 0x00;
10474 feature = (struct scsi_get_config_feature *)
10475 &feature->feature_data[feature->add_length];
10477 f1f: /* DVD Read */
10478 scsi_ulto2b(0x001F, feature->feature_code);
10479 feature->flags = 0x08;
10480 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10481 feature->flags |= SGC_F_CURRENT;
10482 feature->add_length = 4;
10483 feature->feature_data[0] = 0x01;
10484 feature->feature_data[2] = 0x03;
10485 feature = (struct scsi_get_config_feature *)
10486 &feature->feature_data[feature->add_length];
10489 scsi_ulto2b(0x002A, feature->feature_code);
10490 feature->flags = 0x04;
10491 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10492 feature->flags |= SGC_F_CURRENT;
10493 feature->add_length = 4;
10494 feature->feature_data[0] = 0x00;
10495 feature->feature_data[1] = 0x00;
10496 feature = (struct scsi_get_config_feature *)
10497 &feature->feature_data[feature->add_length];
10500 scsi_ulto2b(0x002B, feature->feature_code);
10501 feature->flags = 0x00;
10502 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10503 feature->flags |= SGC_F_CURRENT;
10504 feature->add_length = 4;
10505 feature->feature_data[0] = 0x00;
10506 feature = (struct scsi_get_config_feature *)
10507 &feature->feature_data[feature->add_length];
10509 f3a: /* DVD+RW Dual Layer */
10510 scsi_ulto2b(0x003A, feature->feature_code);
10511 feature->flags = 0x00;
10512 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10513 feature->flags |= SGC_F_CURRENT;
10514 feature->add_length = 4;
10515 feature->feature_data[0] = 0x00;
10516 feature->feature_data[1] = 0x00;
10517 feature = (struct scsi_get_config_feature *)
10518 &feature->feature_data[feature->add_length];
10520 f3b: /* DVD+R Dual Layer */
10521 scsi_ulto2b(0x003B, feature->feature_code);
10522 feature->flags = 0x00;
10523 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10524 feature->flags |= SGC_F_CURRENT;
10525 feature->add_length = 4;
10526 feature->feature_data[0] = 0x00;
10527 feature = (struct scsi_get_config_feature *)
10528 &feature->feature_data[feature->add_length];
10531 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10532 if (rt == SGC_RT_SPECIFIC && data_len > 4) {
10533 feature = (struct scsi_get_config_feature *)(hdr + 1);
10534 if (scsi_2btoul(feature->feature_code) == starting)
10535 feature = (struct scsi_get_config_feature *)
10536 &feature->feature_data[feature->add_length];
10537 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10539 scsi_ulto4b(data_len - 4, hdr->data_length);
10540 if (data_len < alloc_len) {
10541 ctsio->residual = alloc_len - data_len;
10542 ctsio->kern_data_len = data_len;
10543 ctsio->kern_total_len = data_len;
10545 ctsio->residual = 0;
10546 ctsio->kern_data_len = alloc_len;
10547 ctsio->kern_total_len = alloc_len;
10550 ctl_set_success(ctsio);
10551 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10552 ctsio->be_move_done = ctl_config_move_done;
10553 ctl_datamove((union ctl_io *)ctsio);
10554 return (CTL_RETVAL_COMPLETE);
10558 ctl_get_event_status(struct ctl_scsiio *ctsio)
10560 struct scsi_get_event_status_header *hdr;
10561 struct scsi_get_event_status *cdb;
10562 struct ctl_lun *lun;
10563 uint32_t alloc_len, data_len;
10566 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10567 cdb = (struct scsi_get_event_status *)ctsio->cdb;
10568 if ((cdb->byte2 & SGESN_POLLED) == 0) {
10569 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
10570 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
10571 ctl_done((union ctl_io *)ctsio);
10572 return (CTL_RETVAL_COMPLETE);
10574 notif_class = cdb->notif_class;
10575 alloc_len = scsi_2btoul(cdb->length);
10577 data_len = sizeof(struct scsi_get_event_status_header);
10578 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10579 ctsio->kern_sg_entries = 0;
10580 ctsio->kern_data_resid = 0;
10581 ctsio->kern_rel_offset = 0;
10583 if (data_len < alloc_len) {
10584 ctsio->residual = alloc_len - data_len;
10585 ctsio->kern_data_len = data_len;
10586 ctsio->kern_total_len = data_len;
10588 ctsio->residual = 0;
10589 ctsio->kern_data_len = alloc_len;
10590 ctsio->kern_total_len = alloc_len;
10593 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr;
10594 scsi_ulto2b(0, hdr->descr_length);
10595 hdr->nea_class = SGESN_NEA;
10596 hdr->supported_class = 0;
10598 ctl_set_success(ctsio);
10599 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10600 ctsio->be_move_done = ctl_config_move_done;
10601 ctl_datamove((union ctl_io *)ctsio);
10602 return (CTL_RETVAL_COMPLETE);
10606 ctl_mechanism_status(struct ctl_scsiio *ctsio)
10608 struct scsi_mechanism_status_header *hdr;
10609 struct scsi_mechanism_status *cdb;
10610 struct ctl_lun *lun;
10611 uint32_t alloc_len, data_len;
10613 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10614 cdb = (struct scsi_mechanism_status *)ctsio->cdb;
10615 alloc_len = scsi_2btoul(cdb->length);
10617 data_len = sizeof(struct scsi_mechanism_status_header);
10618 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10619 ctsio->kern_sg_entries = 0;
10620 ctsio->kern_data_resid = 0;
10621 ctsio->kern_rel_offset = 0;
10623 if (data_len < alloc_len) {
10624 ctsio->residual = alloc_len - data_len;
10625 ctsio->kern_data_len = data_len;
10626 ctsio->kern_total_len = data_len;
10628 ctsio->residual = 0;
10629 ctsio->kern_data_len = alloc_len;
10630 ctsio->kern_total_len = alloc_len;
10633 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr;
10634 hdr->state1 = 0x00;
10635 hdr->state2 = 0xe0;
10636 scsi_ulto3b(0, hdr->lba);
10637 hdr->slots_num = 0;
10638 scsi_ulto2b(0, hdr->slots_length);
10640 ctl_set_success(ctsio);
10641 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10642 ctsio->be_move_done = ctl_config_move_done;
10643 ctl_datamove((union ctl_io *)ctsio);
10644 return (CTL_RETVAL_COMPLETE);
10648 ctl_ultomsf(uint32_t lba, uint8_t *buf)
10653 buf[1] = bin2bcd((lba / 75) / 60);
10654 buf[2] = bin2bcd((lba / 75) % 60);
10655 buf[3] = bin2bcd(lba % 75);
10659 ctl_read_toc(struct ctl_scsiio *ctsio)
10661 struct scsi_read_toc_hdr *hdr;
10662 struct scsi_read_toc_type01_descr *descr;
10663 struct scsi_read_toc *cdb;
10664 struct ctl_lun *lun;
10665 uint32_t alloc_len, data_len;
10668 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10669 cdb = (struct scsi_read_toc *)ctsio->cdb;
10670 msf = (cdb->byte2 & CD_MSF) != 0;
10671 format = cdb->format;
10672 alloc_len = scsi_2btoul(cdb->data_len);
10674 data_len = sizeof(struct scsi_read_toc_hdr);
10676 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr);
10678 data_len += sizeof(struct scsi_read_toc_type01_descr);
10679 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10680 ctsio->kern_sg_entries = 0;
10681 ctsio->kern_data_resid = 0;
10682 ctsio->kern_rel_offset = 0;
10684 if (data_len < alloc_len) {
10685 ctsio->residual = alloc_len - data_len;
10686 ctsio->kern_data_len = data_len;
10687 ctsio->kern_total_len = data_len;
10689 ctsio->residual = 0;
10690 ctsio->kern_data_len = alloc_len;
10691 ctsio->kern_total_len = alloc_len;
10694 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr;
10696 scsi_ulto2b(0x12, hdr->data_length);
10699 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10700 descr->addr_ctl = 0x14;
10701 descr->track_number = 1;
10703 ctl_ultomsf(0, descr->track_start);
10705 scsi_ulto4b(0, descr->track_start);
10707 descr->addr_ctl = 0x14;
10708 descr->track_number = 0xaa;
10710 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start);
10712 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start);
10714 scsi_ulto2b(0x0a, hdr->data_length);
10717 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10718 descr->addr_ctl = 0x14;
10719 descr->track_number = 1;
10721 ctl_ultomsf(0, descr->track_start);
10723 scsi_ulto4b(0, descr->track_start);
10726 ctl_set_success(ctsio);
10727 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10728 ctsio->be_move_done = ctl_config_move_done;
10729 ctl_datamove((union ctl_io *)ctsio);
10730 return (CTL_RETVAL_COMPLETE);
10734 * For known CDB types, parse the LBA and length.
10737 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10739 if (io->io_hdr.io_type != CTL_IO_SCSI)
10742 switch (io->scsiio.cdb[0]) {
10743 case COMPARE_AND_WRITE: {
10744 struct scsi_compare_and_write *cdb;
10746 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10748 *lba = scsi_8btou64(cdb->addr);
10749 *len = cdb->length;
10754 struct scsi_rw_6 *cdb;
10756 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10758 *lba = scsi_3btoul(cdb->addr);
10759 /* only 5 bits are valid in the most significant address byte */
10761 *len = cdb->length;
10766 struct scsi_rw_10 *cdb;
10768 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10770 *lba = scsi_4btoul(cdb->addr);
10771 *len = scsi_2btoul(cdb->length);
10774 case WRITE_VERIFY_10: {
10775 struct scsi_write_verify_10 *cdb;
10777 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10779 *lba = scsi_4btoul(cdb->addr);
10780 *len = scsi_2btoul(cdb->length);
10785 struct scsi_rw_12 *cdb;
10787 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10789 *lba = scsi_4btoul(cdb->addr);
10790 *len = scsi_4btoul(cdb->length);
10793 case WRITE_VERIFY_12: {
10794 struct scsi_write_verify_12 *cdb;
10796 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10798 *lba = scsi_4btoul(cdb->addr);
10799 *len = scsi_4btoul(cdb->length);
10804 struct scsi_rw_16 *cdb;
10806 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10808 *lba = scsi_8btou64(cdb->addr);
10809 *len = scsi_4btoul(cdb->length);
10812 case WRITE_ATOMIC_16: {
10813 struct scsi_write_atomic_16 *cdb;
10815 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb;
10817 *lba = scsi_8btou64(cdb->addr);
10818 *len = scsi_2btoul(cdb->length);
10821 case WRITE_VERIFY_16: {
10822 struct scsi_write_verify_16 *cdb;
10824 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10826 *lba = scsi_8btou64(cdb->addr);
10827 *len = scsi_4btoul(cdb->length);
10830 case WRITE_SAME_10: {
10831 struct scsi_write_same_10 *cdb;
10833 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10835 *lba = scsi_4btoul(cdb->addr);
10836 *len = scsi_2btoul(cdb->length);
10839 case WRITE_SAME_16: {
10840 struct scsi_write_same_16 *cdb;
10842 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10844 *lba = scsi_8btou64(cdb->addr);
10845 *len = scsi_4btoul(cdb->length);
10849 struct scsi_verify_10 *cdb;
10851 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10853 *lba = scsi_4btoul(cdb->addr);
10854 *len = scsi_2btoul(cdb->length);
10858 struct scsi_verify_12 *cdb;
10860 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10862 *lba = scsi_4btoul(cdb->addr);
10863 *len = scsi_4btoul(cdb->length);
10867 struct scsi_verify_16 *cdb;
10869 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10871 *lba = scsi_8btou64(cdb->addr);
10872 *len = scsi_4btoul(cdb->length);
10880 case SERVICE_ACTION_IN: { /* GET LBA STATUS */
10881 struct scsi_get_lba_status *cdb;
10883 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10884 *lba = scsi_8btou64(cdb->addr);
10890 break; /* NOTREACHED */
10897 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10900 uint64_t endlba1, endlba2;
10902 endlba1 = lba1 + len1 - (seq ? 0 : 1);
10903 endlba2 = lba2 + len2 - 1;
10905 if ((endlba1 < lba2) || (endlba2 < lba1))
10906 return (CTL_ACTION_PASS);
10908 return (CTL_ACTION_BLOCK);
10912 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10914 struct ctl_ptr_len_flags *ptrlen;
10915 struct scsi_unmap_desc *buf, *end, *range;
10919 /* If not UNMAP -- go other way. */
10920 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10921 io->scsiio.cdb[0] != UNMAP)
10922 return (CTL_ACTION_ERROR);
10924 /* If UNMAP without data -- block and wait for data. */
10925 ptrlen = (struct ctl_ptr_len_flags *)
10926 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10927 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10928 ptrlen->ptr == NULL)
10929 return (CTL_ACTION_BLOCK);
10931 /* UNMAP with data -- check for collision. */
10932 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10933 end = buf + ptrlen->len / sizeof(*buf);
10934 for (range = buf; range < end; range++) {
10935 lba = scsi_8btou64(range->lba);
10936 len = scsi_4btoul(range->length);
10937 if ((lba < lba2 + len2) && (lba + len > lba2))
10938 return (CTL_ACTION_BLOCK);
10940 return (CTL_ACTION_PASS);
10944 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10946 uint64_t lba1, lba2;
10947 uint64_t len1, len2;
10950 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10951 return (CTL_ACTION_ERROR);
10953 retval = ctl_extent_check_unmap(io1, lba2, len2);
10954 if (retval != CTL_ACTION_ERROR)
10957 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10958 return (CTL_ACTION_ERROR);
10960 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10962 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
10966 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2)
10968 uint64_t lba1, lba2;
10969 uint64_t len1, len2;
10971 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10972 return (CTL_ACTION_PASS);
10973 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10974 return (CTL_ACTION_ERROR);
10975 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10976 return (CTL_ACTION_ERROR);
10978 if (lba1 + len1 == lba2)
10979 return (CTL_ACTION_BLOCK);
10980 return (CTL_ACTION_PASS);
10984 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10985 union ctl_io *ooa_io)
10987 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10988 const ctl_serialize_action *serialize_row;
10991 * The initiator attempted multiple untagged commands at the same
10992 * time. Can't do that.
10994 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10995 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10996 && ((pending_io->io_hdr.nexus.targ_port ==
10997 ooa_io->io_hdr.nexus.targ_port)
10998 && (pending_io->io_hdr.nexus.initid ==
10999 ooa_io->io_hdr.nexus.initid))
11000 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
11001 CTL_FLAG_STATUS_SENT)) == 0))
11002 return (CTL_ACTION_OVERLAP);
11005 * The initiator attempted to send multiple tagged commands with
11006 * the same ID. (It's fine if different initiators have the same
11009 * Even if all of those conditions are true, we don't kill the I/O
11010 * if the command ahead of us has been aborted. We won't end up
11011 * sending it to the FETD, and it's perfectly legal to resend a
11012 * command with the same tag number as long as the previous
11013 * instance of this tag number has been aborted somehow.
11015 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
11016 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
11017 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
11018 && ((pending_io->io_hdr.nexus.targ_port ==
11019 ooa_io->io_hdr.nexus.targ_port)
11020 && (pending_io->io_hdr.nexus.initid ==
11021 ooa_io->io_hdr.nexus.initid))
11022 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
11023 CTL_FLAG_STATUS_SENT)) == 0))
11024 return (CTL_ACTION_OVERLAP_TAG);
11027 * If we get a head of queue tag, SAM-3 says that we should
11028 * immediately execute it.
11030 * What happens if this command would normally block for some other
11031 * reason? e.g. a request sense with a head of queue tag
11032 * immediately after a write. Normally that would block, but this
11033 * will result in its getting executed immediately...
11035 * We currently return "pass" instead of "skip", so we'll end up
11036 * going through the rest of the queue to check for overlapped tags.
11038 * XXX KDM check for other types of blockage first??
11040 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
11041 return (CTL_ACTION_PASS);
11044 * Ordered tags have to block until all items ahead of them
11045 * have completed. If we get called with an ordered tag, we always
11046 * block, if something else is ahead of us in the queue.
11048 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
11049 return (CTL_ACTION_BLOCK);
11052 * Simple tags get blocked until all head of queue and ordered tags
11053 * ahead of them have completed. I'm lumping untagged commands in
11054 * with simple tags here. XXX KDM is that the right thing to do?
11056 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11057 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
11058 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
11059 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
11060 return (CTL_ACTION_BLOCK);
11062 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
11063 KASSERT(pending_entry->seridx < CTL_SERIDX_COUNT,
11064 ("%s: Invalid seridx %d for pending CDB %02x %02x @ %p",
11065 __func__, pending_entry->seridx, pending_io->scsiio.cdb[0],
11066 pending_io->scsiio.cdb[1], pending_io));
11067 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
11068 if (ooa_entry->seridx == CTL_SERIDX_INVLD)
11069 return (CTL_ACTION_PASS); /* Unsupported command in OOA queue */
11070 KASSERT(ooa_entry->seridx < CTL_SERIDX_COUNT,
11071 ("%s: Invalid seridx %d for ooa CDB %02x %02x @ %p",
11072 __func__, ooa_entry->seridx, ooa_io->scsiio.cdb[0],
11073 ooa_io->scsiio.cdb[1], ooa_io));
11075 serialize_row = ctl_serialize_table[ooa_entry->seridx];
11077 switch (serialize_row[pending_entry->seridx]) {
11078 case CTL_SER_BLOCK:
11079 return (CTL_ACTION_BLOCK);
11080 case CTL_SER_EXTENT:
11081 return (ctl_extent_check(ooa_io, pending_io,
11082 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
11083 case CTL_SER_EXTENTOPT:
11084 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
11085 SCP_QUEUE_ALG_UNRESTRICTED)
11086 return (ctl_extent_check(ooa_io, pending_io,
11088 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
11089 return (CTL_ACTION_PASS);
11090 case CTL_SER_EXTENTSEQ:
11091 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF)
11092 return (ctl_extent_check_seq(ooa_io, pending_io));
11093 return (CTL_ACTION_PASS);
11095 return (CTL_ACTION_PASS);
11096 case CTL_SER_BLOCKOPT:
11097 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
11098 SCP_QUEUE_ALG_UNRESTRICTED)
11099 return (CTL_ACTION_BLOCK);
11100 return (CTL_ACTION_PASS);
11102 return (CTL_ACTION_SKIP);
11104 panic("%s: Invalid serialization value %d for %d => %d",
11105 __func__, serialize_row[pending_entry->seridx],
11106 pending_entry->seridx, ooa_entry->seridx);
11109 return (CTL_ACTION_ERROR);
11113 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
11115 * - pending_io is generally either incoming, or on the blocked queue
11116 * - starting I/O is the I/O we want to start the check with.
11119 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
11120 union ctl_io *starting_io)
11122 union ctl_io *ooa_io;
11125 mtx_assert(&lun->lun_lock, MA_OWNED);
11128 * Run back along the OOA queue, starting with the current
11129 * blocked I/O and going through every I/O before it on the
11130 * queue. If starting_io is NULL, we'll just end up returning
11133 for (ooa_io = starting_io; ooa_io != NULL;
11134 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
11138 * This routine just checks to see whether
11139 * cur_blocked is blocked by ooa_io, which is ahead
11140 * of it in the queue. It doesn't queue/dequeue
11143 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
11145 case CTL_ACTION_BLOCK:
11146 case CTL_ACTION_OVERLAP:
11147 case CTL_ACTION_OVERLAP_TAG:
11148 case CTL_ACTION_SKIP:
11149 case CTL_ACTION_ERROR:
11151 break; /* NOTREACHED */
11152 case CTL_ACTION_PASS:
11155 panic("%s: Invalid action %d\n", __func__, action);
11159 return (CTL_ACTION_PASS);
11164 * - An I/O has just completed, and has been removed from the per-LUN OOA
11165 * queue, so some items on the blocked queue may now be unblocked.
11168 ctl_check_blocked(struct ctl_lun *lun)
11170 struct ctl_softc *softc = lun->ctl_softc;
11171 union ctl_io *cur_blocked, *next_blocked;
11173 mtx_assert(&lun->lun_lock, MA_OWNED);
11176 * Run forward from the head of the blocked queue, checking each
11177 * entry against the I/Os prior to it on the OOA queue to see if
11178 * there is still any blockage.
11180 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
11181 * with our removing a variable on it while it is traversing the
11184 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
11185 cur_blocked != NULL; cur_blocked = next_blocked) {
11186 union ctl_io *prev_ooa;
11189 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
11192 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
11193 ctl_ooaq, ooa_links);
11196 * If cur_blocked happens to be the first item in the OOA
11197 * queue now, prev_ooa will be NULL, and the action
11198 * returned will just be CTL_ACTION_PASS.
11200 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
11203 case CTL_ACTION_BLOCK:
11204 /* Nothing to do here, still blocked */
11206 case CTL_ACTION_OVERLAP:
11207 case CTL_ACTION_OVERLAP_TAG:
11209 * This shouldn't happen! In theory we've already
11210 * checked this command for overlap...
11213 case CTL_ACTION_PASS:
11214 case CTL_ACTION_SKIP: {
11215 const struct ctl_cmd_entry *entry;
11218 * The skip case shouldn't happen, this transaction
11219 * should have never made it onto the blocked queue.
11222 * This I/O is no longer blocked, we can remove it
11223 * from the blocked queue. Since this is a TAILQ
11224 * (doubly linked list), we can do O(1) removals
11225 * from any place on the list.
11227 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
11229 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11231 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
11232 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){
11234 * Need to send IO back to original side to
11237 union ctl_ha_msg msg_info;
11239 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11240 msg_info.hdr.original_sc =
11241 cur_blocked->io_hdr.original_sc;
11242 msg_info.hdr.serializing_sc = cur_blocked;
11243 msg_info.hdr.msg_type = CTL_MSG_R2R;
11244 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11245 sizeof(msg_info.hdr), M_NOWAIT);
11248 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
11251 * Check this I/O for LUN state changes that may
11252 * have happened while this command was blocked.
11253 * The LUN state may have been changed by a command
11254 * ahead of us in the queue, so we need to re-check
11255 * for any states that can be caused by SCSI
11258 if (ctl_scsiio_lun_check(lun, entry,
11259 &cur_blocked->scsiio) == 0) {
11260 cur_blocked->io_hdr.flags |=
11261 CTL_FLAG_IS_WAS_ON_RTR;
11262 ctl_enqueue_rtr(cur_blocked);
11264 ctl_done(cur_blocked);
11269 * This probably shouldn't happen -- we shouldn't
11270 * get CTL_ACTION_ERROR, or anything else.
11276 return (CTL_RETVAL_COMPLETE);
11280 * This routine (with one exception) checks LUN flags that can be set by
11281 * commands ahead of us in the OOA queue. These flags have to be checked
11282 * when a command initially comes in, and when we pull a command off the
11283 * blocked queue and are preparing to execute it. The reason we have to
11284 * check these flags for commands on the blocked queue is that the LUN
11285 * state may have been changed by a command ahead of us while we're on the
11288 * Ordering is somewhat important with these checks, so please pay
11289 * careful attention to the placement of any new checks.
11292 ctl_scsiio_lun_check(struct ctl_lun *lun,
11293 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11295 struct ctl_softc *softc = lun->ctl_softc;
11301 mtx_assert(&lun->lun_lock, MA_OWNED);
11304 * If this shelf is a secondary shelf controller, we may have to
11305 * reject some commands disallowed by HA mode and link state.
11307 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11308 if (softc->ha_link == CTL_HA_LINK_OFFLINE &&
11309 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11310 ctl_set_lun_unavail(ctsio);
11314 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 &&
11315 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11316 ctl_set_lun_transit(ctsio);
11320 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY &&
11321 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) {
11322 ctl_set_lun_standby(ctsio);
11327 /* The rest of checks are only done on executing side */
11328 if (softc->ha_mode == CTL_HA_MODE_XFER)
11332 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11334 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) {
11335 ctl_set_hw_write_protected(ctsio);
11339 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) {
11340 ctl_set_sense(ctsio, /*current_error*/ 1,
11341 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11342 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11349 * Check for a reservation conflict. If this command isn't allowed
11350 * even on reserved LUNs, and if this initiator isn't the one who
11351 * reserved us, reject the command with a reservation conflict.
11353 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11354 if ((lun->flags & CTL_LUN_RESERVED)
11355 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11356 if (lun->res_idx != residx) {
11357 ctl_set_reservation_conflict(ctsio);
11363 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11364 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11365 /* No reservation or command is allowed. */;
11366 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11367 (lun->pr_res_type == SPR_TYPE_WR_EX ||
11368 lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
11369 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) {
11370 /* The command is allowed for Write Exclusive resv. */;
11373 * if we aren't registered or it's a res holder type
11374 * reservation and this isn't the res holder then set a
11377 if (ctl_get_prkey(lun, residx) == 0 ||
11378 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) {
11379 ctl_set_reservation_conflict(ctsio);
11385 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) {
11386 if (lun->flags & CTL_LUN_EJECTED)
11387 ctl_set_lun_ejected(ctsio);
11388 else if (lun->flags & CTL_LUN_NO_MEDIA) {
11389 if (lun->flags & CTL_LUN_REMOVABLE)
11390 ctl_set_lun_no_media(ctsio);
11392 ctl_set_lun_int_reqd(ctsio);
11393 } else if (lun->flags & CTL_LUN_STOPPED)
11394 ctl_set_lun_stopped(ctsio);
11406 ctl_failover_io(union ctl_io *io, int have_lock)
11408 ctl_set_busy(&io->scsiio);
11413 ctl_failover_lun(union ctl_io *rio)
11415 struct ctl_softc *softc = control_softc;
11416 struct ctl_lun *lun;
11417 struct ctl_io_hdr *io, *next_io;
11420 targ_lun = rio->io_hdr.nexus.targ_mapped_lun;
11421 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun));
11423 /* Find and lock the LUN. */
11424 mtx_lock(&softc->ctl_lock);
11425 if ((targ_lun < CTL_MAX_LUNS) &&
11426 ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
11427 mtx_lock(&lun->lun_lock);
11428 mtx_unlock(&softc->ctl_lock);
11429 if (lun->flags & CTL_LUN_DISABLED) {
11430 mtx_unlock(&lun->lun_lock);
11434 mtx_unlock(&softc->ctl_lock);
11438 if (softc->ha_mode == CTL_HA_MODE_XFER) {
11439 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11440 /* We are master */
11441 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11442 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11443 io->flags |= CTL_FLAG_ABORT;
11444 io->flags |= CTL_FLAG_FAILOVER;
11445 } else { /* This can be only due to DATAMOVE */
11446 io->msg_type = CTL_MSG_DATAMOVE_DONE;
11447 io->flags &= ~CTL_FLAG_DMA_INPROG;
11448 io->flags |= CTL_FLAG_IO_ACTIVE;
11449 io->port_status = 31340;
11450 ctl_enqueue_isc((union ctl_io *)io);
11454 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11455 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11456 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11457 io->flags |= CTL_FLAG_FAILOVER;
11459 ctl_set_busy(&((union ctl_io *)io)->
11461 ctl_done((union ctl_io *)io);
11465 } else { /* SERIALIZE modes */
11466 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links,
11468 /* We are master */
11469 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11470 TAILQ_REMOVE(&lun->blocked_queue, io,
11472 io->flags &= ~CTL_FLAG_BLOCKED;
11473 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11474 ctl_free_io((union ctl_io *)io);
11477 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11478 /* We are master */
11479 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11480 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11481 ctl_free_io((union ctl_io *)io);
11484 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11485 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11486 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) {
11487 ctl_set_busy(&((union ctl_io *)io)->
11489 ctl_done((union ctl_io *)io);
11493 ctl_check_blocked(lun);
11495 mtx_unlock(&lun->lun_lock);
11499 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio)
11501 struct ctl_lun *lun;
11502 const struct ctl_cmd_entry *entry;
11503 uint32_t initidx, targ_lun;
11510 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11511 if ((targ_lun < CTL_MAX_LUNS)
11512 && ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
11514 * If the LUN is invalid, pretend that it doesn't exist.
11515 * It will go away as soon as all pending I/O has been
11518 mtx_lock(&lun->lun_lock);
11519 if (lun->flags & CTL_LUN_DISABLED) {
11520 mtx_unlock(&lun->lun_lock);
11522 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11523 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11525 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
11526 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
11530 * Every I/O goes into the OOA queue for a
11531 * particular LUN, and stays there until completion.
11534 if (TAILQ_EMPTY(&lun->ooa_queue)) {
11535 lun->idle_time += getsbinuptime() -
11539 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr,
11543 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11544 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11547 /* Get command entry and return error if it is unsuppotyed. */
11548 entry = ctl_validate_command(ctsio);
11549 if (entry == NULL) {
11551 mtx_unlock(&lun->lun_lock);
11555 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11556 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11559 * Check to see whether we can send this command to LUNs that don't
11560 * exist. This should pretty much only be the case for inquiry
11561 * and request sense. Further checks, below, really require having
11562 * a LUN, so we can't really check the command anymore. Just put
11563 * it on the rtr queue.
11566 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) {
11567 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11568 ctl_enqueue_rtr((union ctl_io *)ctsio);
11572 ctl_set_unsupported_lun(ctsio);
11573 ctl_done((union ctl_io *)ctsio);
11574 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11578 * Make sure we support this particular command on this LUN.
11579 * e.g., we don't support writes to the control LUN.
11581 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11582 mtx_unlock(&lun->lun_lock);
11583 ctl_set_invalid_opcode(ctsio);
11584 ctl_done((union ctl_io *)ctsio);
11589 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11593 * If we've got a request sense, it'll clear the contingent
11594 * allegiance condition. Otherwise, if we have a CA condition for
11595 * this initiator, clear it, because it sent down a command other
11596 * than request sense.
11598 if ((ctsio->cdb[0] != REQUEST_SENSE)
11599 && (ctl_is_set(lun->have_ca, initidx)))
11600 ctl_clear_mask(lun->have_ca, initidx);
11604 * If the command has this flag set, it handles its own unit
11605 * attention reporting, we shouldn't do anything. Otherwise we
11606 * check for any pending unit attentions, and send them back to the
11607 * initiator. We only do this when a command initially comes in,
11608 * not when we pull it off the blocked queue.
11610 * According to SAM-3, section 5.3.2, the order that things get
11611 * presented back to the host is basically unit attentions caused
11612 * by some sort of reset event, busy status, reservation conflicts
11613 * or task set full, and finally any other status.
11615 * One issue here is that some of the unit attentions we report
11616 * don't fall into the "reset" category (e.g. "reported luns data
11617 * has changed"). So reporting it here, before the reservation
11618 * check, may be technically wrong. I guess the only thing to do
11619 * would be to check for and report the reset events here, and then
11620 * check for the other unit attention types after we check for a
11621 * reservation conflict.
11623 * XXX KDM need to fix this
11625 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11626 ctl_ua_type ua_type;
11628 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11630 if (ua_type != CTL_UA_NONE) {
11631 mtx_unlock(&lun->lun_lock);
11632 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11633 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11634 ctsio->sense_len = SSD_FULL_SIZE;
11635 ctl_done((union ctl_io *)ctsio);
11641 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11642 mtx_unlock(&lun->lun_lock);
11643 ctl_done((union ctl_io *)ctsio);
11648 * XXX CHD this is where we want to send IO to other side if
11649 * this LUN is secondary on this SC. We will need to make a copy
11650 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11651 * the copy we send as FROM_OTHER.
11652 * We also need to stuff the address of the original IO so we can
11653 * find it easily. Something similar will need be done on the other
11654 * side so when we are done we can find the copy.
11656 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
11657 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 &&
11658 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) {
11659 union ctl_ha_msg msg_info;
11662 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11663 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11664 mtx_unlock(&lun->lun_lock);
11666 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11667 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11668 msg_info.hdr.serializing_sc = NULL;
11669 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11670 msg_info.scsi.tag_num = ctsio->tag_num;
11671 msg_info.scsi.tag_type = ctsio->tag_type;
11672 msg_info.scsi.cdb_len = ctsio->cdb_len;
11673 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11675 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11676 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data),
11677 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) {
11678 ctl_set_busy(ctsio);
11679 ctl_done((union ctl_io *)ctsio);
11685 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11686 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11687 ctl_ooaq, ooa_links))) {
11688 case CTL_ACTION_BLOCK:
11689 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11690 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11692 mtx_unlock(&lun->lun_lock);
11694 case CTL_ACTION_PASS:
11695 case CTL_ACTION_SKIP:
11696 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11697 mtx_unlock(&lun->lun_lock);
11698 ctl_enqueue_rtr((union ctl_io *)ctsio);
11700 case CTL_ACTION_OVERLAP:
11701 mtx_unlock(&lun->lun_lock);
11702 ctl_set_overlapped_cmd(ctsio);
11703 ctl_done((union ctl_io *)ctsio);
11705 case CTL_ACTION_OVERLAP_TAG:
11706 mtx_unlock(&lun->lun_lock);
11707 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11708 ctl_done((union ctl_io *)ctsio);
11710 case CTL_ACTION_ERROR:
11712 mtx_unlock(&lun->lun_lock);
11713 ctl_set_internal_failure(ctsio,
11715 /*retry_count*/ 0);
11716 ctl_done((union ctl_io *)ctsio);
11722 const struct ctl_cmd_entry *
11723 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11725 const struct ctl_cmd_entry *entry;
11726 int service_action;
11728 entry = &ctl_cmd_table[ctsio->cdb[0]];
11730 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11731 if (entry->flags & CTL_CMD_FLAG_SA5) {
11732 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11733 entry = &((const struct ctl_cmd_entry *)
11734 entry->execute)[service_action];
11739 const struct ctl_cmd_entry *
11740 ctl_validate_command(struct ctl_scsiio *ctsio)
11742 const struct ctl_cmd_entry *entry;
11746 entry = ctl_get_cmd_entry(ctsio, &sa);
11747 if (entry->execute == NULL) {
11749 ctl_set_invalid_field(ctsio,
11756 ctl_set_invalid_opcode(ctsio);
11757 ctl_done((union ctl_io *)ctsio);
11760 KASSERT(entry->length > 0,
11761 ("Not defined length for command 0x%02x/0x%02x",
11762 ctsio->cdb[0], ctsio->cdb[1]));
11763 for (i = 1; i < entry->length; i++) {
11764 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11767 ctl_set_invalid_field(ctsio,
11772 /*bit*/ fls(diff) - 1);
11773 ctl_done((union ctl_io *)ctsio);
11780 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11783 switch (lun_type) {
11785 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0)
11789 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
11793 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0)
11803 ctl_scsiio(struct ctl_scsiio *ctsio)
11806 const struct ctl_cmd_entry *entry;
11808 retval = CTL_RETVAL_COMPLETE;
11810 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11812 entry = ctl_get_cmd_entry(ctsio, NULL);
11815 * If this I/O has been aborted, just send it straight to
11816 * ctl_done() without executing it.
11818 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11819 ctl_done((union ctl_io *)ctsio);
11824 * All the checks should have been handled by ctl_scsiio_precheck().
11825 * We should be clear now to just execute the I/O.
11827 retval = entry->execute(ctsio);
11834 * Since we only implement one target right now, a bus reset simply resets
11835 * our single target.
11838 ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io)
11840 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET));
11844 ctl_target_reset(struct ctl_softc *softc, union ctl_io *io,
11845 ctl_ua_type ua_type)
11847 struct ctl_port *port;
11848 struct ctl_lun *lun;
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 if (ua_type==CTL_UA_TARG_RESET)
11856 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11858 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11859 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11860 msg_info.hdr.original_sc = NULL;
11861 msg_info.hdr.serializing_sc = NULL;
11862 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11863 sizeof(msg_info.task), M_WAITOK);
11867 mtx_lock(&softc->ctl_lock);
11868 port = ctl_io_port(&io->io_hdr);
11869 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11870 if (port != NULL &&
11871 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
11873 retval += ctl_do_lun_reset(lun, io, ua_type);
11875 mtx_unlock(&softc->ctl_lock);
11876 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11881 * The LUN should always be set. The I/O is optional, and is used to
11882 * distinguish between I/Os sent by this initiator, and by other
11883 * initiators. We set unit attention for initiators other than this one.
11884 * SAM-3 is vague on this point. It does say that a unit attention should
11885 * be established for other initiators when a LUN is reset (see section
11886 * 5.7.3), but it doesn't specifically say that the unit attention should
11887 * be established for this particular initiator when a LUN is reset. Here
11888 * is the relevant text, from SAM-3 rev 8:
11890 * 5.7.2 When a SCSI initiator port aborts its own tasks
11892 * When a SCSI initiator port causes its own task(s) to be aborted, no
11893 * notification that the task(s) have been aborted shall be returned to
11894 * the SCSI initiator port other than the completion response for the
11895 * command or task management function action that caused the task(s) to
11896 * be aborted and notification(s) associated with related effects of the
11897 * action (e.g., a reset unit attention condition).
11899 * XXX KDM for now, we're setting unit attention for all initiators.
11902 ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11910 mtx_lock(&lun->lun_lock);
11912 * Run through the OOA queue and abort each I/O.
11914 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11915 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11916 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11920 * This version sets unit attention for every
11923 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11924 ctl_est_ua_all(lun, initidx, ua_type);
11926 ctl_est_ua_all(lun, -1, ua_type);
11930 * A reset (any kind, really) clears reservations established with
11931 * RESERVE/RELEASE. It does not clear reservations established
11932 * with PERSISTENT RESERVE OUT, but we don't support that at the
11933 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
11934 * reservations made with the RESERVE/RELEASE commands, because
11935 * those commands are obsolete in SPC-3.
11937 lun->flags &= ~CTL_LUN_RESERVED;
11940 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11941 ctl_clear_mask(lun->have_ca, i);
11943 lun->prevent_count = 0;
11944 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11945 ctl_clear_mask(lun->prevent, i);
11946 mtx_unlock(&lun->lun_lock);
11952 ctl_lun_reset(struct ctl_softc *softc, union ctl_io *io)
11954 struct ctl_lun *lun;
11958 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11959 mtx_lock(&softc->ctl_lock);
11960 if ((targ_lun >= CTL_MAX_LUNS) ||
11961 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11962 mtx_unlock(&softc->ctl_lock);
11963 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11966 retval = ctl_do_lun_reset(lun, io, CTL_UA_LUN_RESET);
11967 mtx_unlock(&softc->ctl_lock);
11968 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11970 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) {
11971 union ctl_ha_msg msg_info;
11973 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11974 msg_info.hdr.nexus = io->io_hdr.nexus;
11975 msg_info.task.task_action = CTL_TASK_LUN_RESET;
11976 msg_info.hdr.original_sc = NULL;
11977 msg_info.hdr.serializing_sc = NULL;
11978 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11979 sizeof(msg_info.task), M_WAITOK);
11985 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11990 mtx_assert(&lun->lun_lock, MA_OWNED);
11993 * Run through the OOA queue and attempt to find the given I/O.
11994 * The target port, initiator ID, tag type and tag number have to
11995 * match the values that we got from the initiator. If we have an
11996 * untagged command to abort, simply abort the first untagged command
11997 * we come to. We only allow one untagged command at a time of course.
11999 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12000 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12002 if ((targ_port == UINT32_MAX ||
12003 targ_port == xio->io_hdr.nexus.targ_port) &&
12004 (init_id == UINT32_MAX ||
12005 init_id == xio->io_hdr.nexus.initid)) {
12006 if (targ_port != xio->io_hdr.nexus.targ_port ||
12007 init_id != xio->io_hdr.nexus.initid)
12008 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
12009 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12010 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12011 union ctl_ha_msg msg_info;
12013 msg_info.hdr.nexus = xio->io_hdr.nexus;
12014 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12015 msg_info.task.tag_num = xio->scsiio.tag_num;
12016 msg_info.task.tag_type = xio->scsiio.tag_type;
12017 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12018 msg_info.hdr.original_sc = NULL;
12019 msg_info.hdr.serializing_sc = NULL;
12020 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12021 sizeof(msg_info.task), M_NOWAIT);
12028 ctl_abort_task_set(union ctl_io *io)
12030 struct ctl_softc *softc = control_softc;
12031 struct ctl_lun *lun;
12037 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12038 mtx_lock(&softc->ctl_lock);
12039 if ((targ_lun >= CTL_MAX_LUNS) ||
12040 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12041 mtx_unlock(&softc->ctl_lock);
12042 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12046 mtx_lock(&lun->lun_lock);
12047 mtx_unlock(&softc->ctl_lock);
12048 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
12049 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12050 io->io_hdr.nexus.initid,
12051 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12052 } else { /* CTL_TASK_CLEAR_TASK_SET */
12053 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
12054 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12056 mtx_unlock(&lun->lun_lock);
12057 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12062 ctl_i_t_nexus_reset(union ctl_io *io)
12064 struct ctl_softc *softc = control_softc;
12065 struct ctl_lun *lun;
12068 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12069 union ctl_ha_msg msg_info;
12071 msg_info.hdr.nexus = io->io_hdr.nexus;
12072 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET;
12073 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12074 msg_info.hdr.original_sc = NULL;
12075 msg_info.hdr.serializing_sc = NULL;
12076 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12077 sizeof(msg_info.task), M_WAITOK);
12080 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12081 mtx_lock(&softc->ctl_lock);
12082 STAILQ_FOREACH(lun, &softc->lun_list, links) {
12083 mtx_lock(&lun->lun_lock);
12084 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12085 io->io_hdr.nexus.initid, 1);
12087 ctl_clear_mask(lun->have_ca, initidx);
12089 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx))
12090 lun->flags &= ~CTL_LUN_RESERVED;
12091 if (ctl_is_set(lun->prevent, initidx)) {
12092 ctl_clear_mask(lun->prevent, initidx);
12093 lun->prevent_count--;
12095 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS);
12096 mtx_unlock(&lun->lun_lock);
12098 mtx_unlock(&softc->ctl_lock);
12099 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12104 ctl_abort_task(union ctl_io *io)
12107 struct ctl_lun *lun;
12108 struct ctl_softc *softc;
12111 char printbuf[128];
12116 softc = control_softc;
12122 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12123 mtx_lock(&softc->ctl_lock);
12124 if ((targ_lun >= CTL_MAX_LUNS) ||
12125 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12126 mtx_unlock(&softc->ctl_lock);
12127 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12132 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
12133 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
12136 mtx_lock(&lun->lun_lock);
12137 mtx_unlock(&softc->ctl_lock);
12139 * Run through the OOA queue and attempt to find the given I/O.
12140 * The target port, initiator ID, tag type and tag number have to
12141 * match the values that we got from the initiator. If we have an
12142 * untagged command to abort, simply abort the first untagged command
12143 * we come to. We only allow one untagged command at a time of course.
12145 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12146 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12148 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
12150 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
12151 lun->lun, xio->scsiio.tag_num,
12152 xio->scsiio.tag_type,
12153 (xio->io_hdr.blocked_links.tqe_prev
12154 == NULL) ? "" : " BLOCKED",
12155 (xio->io_hdr.flags &
12156 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
12157 (xio->io_hdr.flags &
12158 CTL_FLAG_ABORT) ? " ABORT" : "",
12159 (xio->io_hdr.flags &
12160 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
12161 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
12163 printf("%s\n", sbuf_data(&sb));
12166 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12167 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12168 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12172 * If the abort says that the task is untagged, the
12173 * task in the queue must be untagged. Otherwise,
12174 * we just check to see whether the tag numbers
12175 * match. This is because the QLogic firmware
12176 * doesn't pass back the tag type in an abort
12180 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12181 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12182 || (xio->scsiio.tag_num == io->taskio.tag_num))
12185 * XXX KDM we've got problems with FC, because it
12186 * doesn't send down a tag type with aborts. So we
12187 * can only really go by the tag number...
12188 * This may cause problems with parallel SCSI.
12189 * Need to figure that out!!
12191 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12192 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12194 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
12195 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12196 union ctl_ha_msg msg_info;
12198 msg_info.hdr.nexus = io->io_hdr.nexus;
12199 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12200 msg_info.task.tag_num = io->taskio.tag_num;
12201 msg_info.task.tag_type = io->taskio.tag_type;
12202 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12203 msg_info.hdr.original_sc = NULL;
12204 msg_info.hdr.serializing_sc = NULL;
12206 printf("Sent Abort to other side\n");
12208 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12209 sizeof(msg_info.task), M_NOWAIT);
12212 printf("ctl_abort_task: found I/O to abort\n");
12216 mtx_unlock(&lun->lun_lock);
12220 * This isn't really an error. It's entirely possible for
12221 * the abort and command completion to cross on the wire.
12222 * This is more of an informative/diagnostic error.
12225 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
12226 "%u:%u:%u tag %d type %d\n",
12227 io->io_hdr.nexus.initid,
12228 io->io_hdr.nexus.targ_port,
12229 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
12230 io->taskio.tag_type);
12233 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12238 ctl_query_task(union ctl_io *io, int task_set)
12241 struct ctl_lun *lun;
12242 struct ctl_softc *softc;
12246 softc = control_softc;
12247 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12248 mtx_lock(&softc->ctl_lock);
12249 if ((targ_lun >= CTL_MAX_LUNS) ||
12250 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12251 mtx_unlock(&softc->ctl_lock);
12252 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12255 mtx_lock(&lun->lun_lock);
12256 mtx_unlock(&softc->ctl_lock);
12257 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12258 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12260 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12261 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12262 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12265 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) {
12270 mtx_unlock(&lun->lun_lock);
12272 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12274 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12279 ctl_query_async_event(union ctl_io *io)
12281 struct ctl_lun *lun;
12282 struct ctl_softc *softc;
12284 uint32_t targ_lun, initidx;
12286 softc = control_softc;
12287 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12288 mtx_lock(&softc->ctl_lock);
12289 if ((targ_lun >= CTL_MAX_LUNS) ||
12290 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12291 mtx_unlock(&softc->ctl_lock);
12292 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12295 mtx_lock(&lun->lun_lock);
12296 mtx_unlock(&softc->ctl_lock);
12297 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12298 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp);
12299 mtx_unlock(&lun->lun_lock);
12300 if (ua != CTL_UA_NONE)
12301 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12303 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12308 ctl_run_task(union ctl_io *io)
12310 struct ctl_softc *softc = control_softc;
12313 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12314 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12315 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type));
12316 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED;
12317 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp));
12318 switch (io->taskio.task_action) {
12319 case CTL_TASK_ABORT_TASK:
12320 retval = ctl_abort_task(io);
12322 case CTL_TASK_ABORT_TASK_SET:
12323 case CTL_TASK_CLEAR_TASK_SET:
12324 retval = ctl_abort_task_set(io);
12326 case CTL_TASK_CLEAR_ACA:
12328 case CTL_TASK_I_T_NEXUS_RESET:
12329 retval = ctl_i_t_nexus_reset(io);
12331 case CTL_TASK_LUN_RESET:
12332 retval = ctl_lun_reset(softc, io);
12334 case CTL_TASK_TARGET_RESET:
12335 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET);
12337 case CTL_TASK_BUS_RESET:
12338 retval = ctl_bus_reset(softc, io);
12340 case CTL_TASK_PORT_LOGIN:
12342 case CTL_TASK_PORT_LOGOUT:
12344 case CTL_TASK_QUERY_TASK:
12345 retval = ctl_query_task(io, 0);
12347 case CTL_TASK_QUERY_TASK_SET:
12348 retval = ctl_query_task(io, 1);
12350 case CTL_TASK_QUERY_ASYNC_EVENT:
12351 retval = ctl_query_async_event(io);
12354 printf("%s: got unknown task management event %d\n",
12355 __func__, io->taskio.task_action);
12359 io->io_hdr.status = CTL_SUCCESS;
12361 io->io_hdr.status = CTL_ERROR;
12366 * For HA operation. Handle commands that come in from the other
12370 ctl_handle_isc(union ctl_io *io)
12373 struct ctl_lun *lun;
12374 struct ctl_softc *softc = control_softc;
12377 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12378 lun = softc->ctl_luns[targ_lun];
12380 switch (io->io_hdr.msg_type) {
12381 case CTL_MSG_SERIALIZE:
12382 free_io = ctl_serialize_other_sc_cmd(&io->scsiio);
12384 case CTL_MSG_R2R: {
12385 const struct ctl_cmd_entry *entry;
12388 * This is only used in SER_ONLY mode.
12391 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12392 mtx_lock(&lun->lun_lock);
12393 if (ctl_scsiio_lun_check(lun,
12394 entry, (struct ctl_scsiio *)io) != 0) {
12395 mtx_unlock(&lun->lun_lock);
12399 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12400 mtx_unlock(&lun->lun_lock);
12401 ctl_enqueue_rtr(io);
12404 case CTL_MSG_FINISH_IO:
12405 if (softc->ha_mode == CTL_HA_MODE_XFER) {
12410 mtx_lock(&lun->lun_lock);
12411 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
12413 ctl_check_blocked(lun);
12414 mtx_unlock(&lun->lun_lock);
12417 case CTL_MSG_PERS_ACTION:
12418 ctl_hndl_per_res_out_on_other_sc(
12419 (union ctl_ha_msg *)&io->presio.pr_msg);
12422 case CTL_MSG_BAD_JUJU:
12426 case CTL_MSG_DATAMOVE:
12427 /* Only used in XFER mode */
12429 ctl_datamove_remote(io);
12431 case CTL_MSG_DATAMOVE_DONE:
12432 /* Only used in XFER mode */
12434 io->scsiio.be_move_done(io);
12436 case CTL_MSG_FAILOVER:
12437 ctl_failover_lun(io);
12442 printf("%s: Invalid message type %d\n",
12443 __func__, io->io_hdr.msg_type);
12453 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12454 * there is no match.
12456 static ctl_lun_error_pattern
12457 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12459 const struct ctl_cmd_entry *entry;
12460 ctl_lun_error_pattern filtered_pattern, pattern;
12462 pattern = desc->error_pattern;
12465 * XXX KDM we need more data passed into this function to match a
12466 * custom pattern, and we actually need to implement custom pattern
12469 if (pattern & CTL_LUN_PAT_CMD)
12470 return (CTL_LUN_PAT_CMD);
12472 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12473 return (CTL_LUN_PAT_ANY);
12475 entry = ctl_get_cmd_entry(ctsio, NULL);
12477 filtered_pattern = entry->pattern & pattern;
12480 * If the user requested specific flags in the pattern (e.g.
12481 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12484 * If the user did not specify any flags, it doesn't matter whether
12485 * or not the command supports the flags.
12487 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12488 (pattern & ~CTL_LUN_PAT_MASK))
12489 return (CTL_LUN_PAT_NONE);
12492 * If the user asked for a range check, see if the requested LBA
12493 * range overlaps with this command's LBA range.
12495 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12501 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12503 return (CTL_LUN_PAT_NONE);
12505 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12506 desc->lba_range.len, FALSE);
12508 * A "pass" means that the LBA ranges don't overlap, so
12509 * this doesn't match the user's range criteria.
12511 if (action == CTL_ACTION_PASS)
12512 return (CTL_LUN_PAT_NONE);
12515 return (filtered_pattern);
12519 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12521 struct ctl_error_desc *desc, *desc2;
12523 mtx_assert(&lun->lun_lock, MA_OWNED);
12525 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12526 ctl_lun_error_pattern pattern;
12528 * Check to see whether this particular command matches
12529 * the pattern in the descriptor.
12531 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12532 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12535 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12536 case CTL_LUN_INJ_ABORTED:
12537 ctl_set_aborted(&io->scsiio);
12539 case CTL_LUN_INJ_MEDIUM_ERR:
12540 ctl_set_medium_error(&io->scsiio,
12541 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) !=
12542 CTL_FLAG_DATA_OUT);
12544 case CTL_LUN_INJ_UA:
12545 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12547 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12549 case CTL_LUN_INJ_CUSTOM:
12551 * We're assuming the user knows what he is doing.
12552 * Just copy the sense information without doing
12555 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12556 MIN(sizeof(desc->custom_sense),
12557 sizeof(io->scsiio.sense_data)));
12558 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12559 io->scsiio.sense_len = SSD_FULL_SIZE;
12560 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12562 case CTL_LUN_INJ_NONE:
12565 * If this is an error injection type we don't know
12566 * about, clear the continuous flag (if it is set)
12567 * so it will get deleted below.
12569 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12573 * By default, each error injection action is a one-shot
12575 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12578 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12584 #ifdef CTL_IO_DELAY
12586 ctl_datamove_timer_wakeup(void *arg)
12590 io = (union ctl_io *)arg;
12594 #endif /* CTL_IO_DELAY */
12597 ctl_datamove(union ctl_io *io)
12599 struct ctl_lun *lun;
12600 void (*fe_datamove)(union ctl_io *io);
12602 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12604 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12606 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12608 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12613 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12614 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12616 sbuf_cat(&sb, path_str);
12617 switch (io->io_hdr.io_type) {
12619 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12620 sbuf_printf(&sb, "\n");
12621 sbuf_cat(&sb, path_str);
12622 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12623 io->scsiio.tag_num, io->scsiio.tag_type);
12626 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12627 "Tag Type: %d\n", io->taskio.task_action,
12628 io->taskio.tag_num, io->taskio.tag_type);
12631 panic("%s: Invalid CTL I/O type %d\n",
12632 __func__, io->io_hdr.io_type);
12634 sbuf_cat(&sb, path_str);
12635 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12636 (intmax_t)time_uptime - io->io_hdr.start_time);
12638 printf("%s", sbuf_data(&sb));
12640 #endif /* CTL_TIME_IO */
12642 #ifdef CTL_IO_DELAY
12643 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12644 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12647 && (lun->delay_info.datamove_delay > 0)) {
12649 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
12650 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12651 callout_reset(&io->io_hdr.delay_callout,
12652 lun->delay_info.datamove_delay * hz,
12653 ctl_datamove_timer_wakeup, io);
12654 if (lun->delay_info.datamove_type ==
12655 CTL_DELAY_TYPE_ONESHOT)
12656 lun->delay_info.datamove_delay = 0;
12663 * This command has been aborted. Set the port status, so we fail
12666 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12667 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n",
12668 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12669 io->io_hdr.nexus.targ_port,
12670 io->io_hdr.nexus.targ_lun);
12671 io->io_hdr.port_status = 31337;
12673 * Note that the backend, in this case, will get the
12674 * callback in its context. In other cases it may get
12675 * called in the frontend's interrupt thread context.
12677 io->scsiio.be_move_done(io);
12681 /* Don't confuse frontend with zero length data move. */
12682 if (io->scsiio.kern_data_len == 0) {
12683 io->scsiio.be_move_done(io);
12687 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12692 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12694 union ctl_ha_msg msg;
12696 struct bintime cur_bt;
12699 memset(&msg, 0, sizeof(msg));
12700 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12701 msg.hdr.original_sc = io;
12702 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12703 msg.hdr.nexus = io->io_hdr.nexus;
12704 msg.hdr.status = io->io_hdr.status;
12705 msg.scsi.tag_num = io->scsiio.tag_num;
12706 msg.scsi.tag_type = io->scsiio.tag_type;
12707 msg.scsi.scsi_status = io->scsiio.scsi_status;
12708 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12709 io->scsiio.sense_len);
12710 msg.scsi.sense_len = io->scsiio.sense_len;
12711 msg.scsi.sense_residual = io->scsiio.sense_residual;
12712 msg.scsi.fetd_status = io->io_hdr.port_status;
12713 msg.scsi.residual = io->scsiio.residual;
12714 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12715 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12716 ctl_failover_io(io, /*have_lock*/ have_lock);
12719 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12720 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12721 msg.scsi.sense_len, M_WAITOK);
12724 getbinuptime(&cur_bt);
12725 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
12726 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
12728 io->io_hdr.num_dmas++;
12732 * The DMA to the remote side is done, now we need to tell the other side
12733 * we're done so it can continue with its data movement.
12736 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12743 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12744 printf("%s: ISC DMA write failed with error %d", __func__,
12746 ctl_set_internal_failure(&io->scsiio,
12748 /*retry_count*/ rq->ret);
12751 ctl_dt_req_free(rq);
12753 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12754 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12755 free(io->io_hdr.remote_sglist, M_CTL);
12756 io->io_hdr.remote_sglist = NULL;
12757 io->io_hdr.local_sglist = NULL;
12760 * The data is in local and remote memory, so now we need to send
12761 * status (good or back) back to the other side.
12763 ctl_send_datamove_done(io, /*have_lock*/ 0);
12767 * We've moved the data from the host/controller into local memory. Now we
12768 * need to push it over to the remote controller's memory.
12771 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12775 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12776 ctl_datamove_remote_write_cb);
12781 ctl_datamove_remote_write(union ctl_io *io)
12784 void (*fe_datamove)(union ctl_io *io);
12787 * - Get the data from the host/HBA into local memory.
12788 * - DMA memory from the local controller to the remote controller.
12789 * - Send status back to the remote controller.
12792 retval = ctl_datamove_remote_sgl_setup(io);
12796 /* Switch the pointer over so the FETD knows what to do */
12797 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12800 * Use a custom move done callback, since we need to send completion
12801 * back to the other controller, not to the backend on this side.
12803 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12805 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12810 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12819 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12820 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12821 free(io->io_hdr.remote_sglist, M_CTL);
12822 io->io_hdr.remote_sglist = NULL;
12823 io->io_hdr.local_sglist = NULL;
12826 scsi_path_string(io, path_str, sizeof(path_str));
12827 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12828 sbuf_cat(&sb, path_str);
12829 scsi_command_string(&io->scsiio, NULL, &sb);
12830 sbuf_printf(&sb, "\n");
12831 sbuf_cat(&sb, path_str);
12832 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12833 io->scsiio.tag_num, io->scsiio.tag_type);
12834 sbuf_cat(&sb, path_str);
12835 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12836 io->io_hdr.flags, io->io_hdr.status);
12838 printk("%s", sbuf_data(&sb));
12843 * The read is done, now we need to send status (good or bad) back
12844 * to the other side.
12846 ctl_send_datamove_done(io, /*have_lock*/ 0);
12852 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12855 void (*fe_datamove)(union ctl_io *io);
12859 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12860 printf("%s: ISC DMA read failed with error %d\n", __func__,
12862 ctl_set_internal_failure(&io->scsiio,
12864 /*retry_count*/ rq->ret);
12867 ctl_dt_req_free(rq);
12869 /* Switch the pointer over so the FETD knows what to do */
12870 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12873 * Use a custom move done callback, since we need to send completion
12874 * back to the other controller, not to the backend on this side.
12876 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12878 /* XXX KDM add checks like the ones in ctl_datamove? */
12880 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12885 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12887 struct ctl_sg_entry *local_sglist;
12888 uint32_t len_to_go;
12893 local_sglist = io->io_hdr.local_sglist;
12894 len_to_go = io->scsiio.kern_data_len;
12897 * The difficult thing here is that the size of the various
12898 * S/G segments may be different than the size from the
12899 * remote controller. That'll make it harder when DMAing
12900 * the data back to the other side.
12902 for (i = 0; len_to_go > 0; i++) {
12903 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT);
12904 local_sglist[i].addr =
12905 malloc(local_sglist[i].len, M_CTL, M_WAITOK);
12907 len_to_go -= local_sglist[i].len;
12910 * Reset the number of S/G entries accordingly. The original
12911 * number of S/G entries is available in rem_sg_entries.
12913 io->scsiio.kern_sg_entries = i;
12916 printf("%s: kern_sg_entries = %d\n", __func__,
12917 io->scsiio.kern_sg_entries);
12918 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12919 printf("%s: sg[%d] = %p, %lu\n", __func__, i,
12920 local_sglist[i].addr, local_sglist[i].len);
12927 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12928 ctl_ha_dt_cb callback)
12930 struct ctl_ha_dt_req *rq;
12931 struct ctl_sg_entry *remote_sglist, *local_sglist;
12932 uint32_t local_used, remote_used, total_used;
12935 rq = ctl_dt_req_alloc();
12938 * If we failed to allocate the request, and if the DMA didn't fail
12939 * anyway, set busy status. This is just a resource allocation
12943 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12944 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS))
12945 ctl_set_busy(&io->scsiio);
12947 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12948 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
12951 ctl_dt_req_free(rq);
12954 * The data move failed. We need to return status back
12955 * to the other controller. No point in trying to DMA
12956 * data to the remote controller.
12959 ctl_send_datamove_done(io, /*have_lock*/ 0);
12964 local_sglist = io->io_hdr.local_sglist;
12965 remote_sglist = io->io_hdr.remote_sglist;
12971 * Pull/push the data over the wire from/to the other controller.
12972 * This takes into account the possibility that the local and
12973 * remote sglists may not be identical in terms of the size of
12974 * the elements and the number of elements.
12976 * One fundamental assumption here is that the length allocated for
12977 * both the local and remote sglists is identical. Otherwise, we've
12978 * essentially got a coding error of some sort.
12980 isc_ret = CTL_HA_STATUS_SUCCESS;
12981 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12985 rq->command = command;
12989 * Both pointers should be aligned. But it is possible
12990 * that the allocation length is not. They should both
12991 * also have enough slack left over at the end, though,
12992 * to round up to the next 8 byte boundary.
12994 cur_len = MIN(local_sglist[i].len - local_used,
12995 remote_sglist[j].len - remote_used);
12996 rq->size = cur_len;
12998 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12999 tmp_ptr += local_used;
13002 /* Use physical addresses when talking to ISC hardware */
13003 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
13004 /* XXX KDM use busdma */
13005 rq->local = vtophys(tmp_ptr);
13007 rq->local = tmp_ptr;
13009 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
13010 ("HA does not support BUS_ADDR"));
13011 rq->local = tmp_ptr;
13014 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
13015 tmp_ptr += remote_used;
13016 rq->remote = tmp_ptr;
13018 rq->callback = NULL;
13020 local_used += cur_len;
13021 if (local_used >= local_sglist[i].len) {
13026 remote_used += cur_len;
13027 if (remote_used >= remote_sglist[j].len) {
13031 total_used += cur_len;
13033 if (total_used >= io->scsiio.kern_data_len)
13034 rq->callback = callback;
13037 printf("%s: %s: local %p remote %p size %d\n", __func__,
13038 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
13039 rq->local, rq->remote, rq->size);
13042 isc_ret = ctl_dt_single(rq);
13043 if (isc_ret > CTL_HA_STATUS_SUCCESS)
13046 if (isc_ret != CTL_HA_STATUS_WAIT) {
13055 ctl_datamove_remote_read(union ctl_io *io)
13061 * This will send an error to the other controller in the case of a
13064 retval = ctl_datamove_remote_sgl_setup(io);
13068 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
13069 ctl_datamove_remote_read_cb);
13072 * Make sure we free memory if there was an error.. The
13073 * ctl_datamove_remote_xfer() function will send the
13074 * datamove done message, or call the callback with an
13075 * error if there is a problem.
13077 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13078 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13079 free(io->io_hdr.remote_sglist, M_CTL);
13080 io->io_hdr.remote_sglist = NULL;
13081 io->io_hdr.local_sglist = NULL;
13086 * Process a datamove request from the other controller. This is used for
13087 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
13088 * first. Once that is complete, the data gets DMAed into the remote
13089 * controller's memory. For reads, we DMA from the remote controller's
13090 * memory into our memory first, and then move it out to the FETD.
13093 ctl_datamove_remote(union ctl_io *io)
13096 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
13098 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
13099 ctl_failover_io(io, /*have_lock*/ 0);
13104 * Note that we look for an aborted I/O here, but don't do some of
13105 * the other checks that ctl_datamove() normally does.
13106 * We don't need to run the datamove delay code, since that should
13107 * have been done if need be on the other controller.
13109 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
13110 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__,
13111 io->scsiio.tag_num, io->io_hdr.nexus.initid,
13112 io->io_hdr.nexus.targ_port,
13113 io->io_hdr.nexus.targ_lun);
13114 io->io_hdr.port_status = 31338;
13115 ctl_send_datamove_done(io, /*have_lock*/ 0);
13119 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
13120 ctl_datamove_remote_write(io);
13121 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
13122 ctl_datamove_remote_read(io);
13124 io->io_hdr.port_status = 31339;
13125 ctl_send_datamove_done(io, /*have_lock*/ 0);
13130 ctl_process_done(union ctl_io *io)
13132 struct ctl_lun *lun;
13133 struct ctl_softc *softc = control_softc;
13134 void (*fe_done)(union ctl_io *io);
13135 union ctl_ha_msg msg;
13136 uint32_t targ_port = io->io_hdr.nexus.targ_port;
13138 CTL_DEBUG_PRINT(("ctl_process_done\n"));
13139 fe_done = softc->ctl_ports[targ_port]->fe_done;
13142 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
13147 ctl_scsi_path_string(io, path_str, sizeof(path_str));
13148 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13150 sbuf_cat(&sb, path_str);
13151 switch (io->io_hdr.io_type) {
13153 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
13154 sbuf_printf(&sb, "\n");
13155 sbuf_cat(&sb, path_str);
13156 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13157 io->scsiio.tag_num, io->scsiio.tag_type);
13160 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
13161 "Tag Type: %d\n", io->taskio.task_action,
13162 io->taskio.tag_num, io->taskio.tag_type);
13165 panic("%s: Invalid CTL I/O type %d\n",
13166 __func__, io->io_hdr.io_type);
13168 sbuf_cat(&sb, path_str);
13169 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
13170 (intmax_t)time_uptime - io->io_hdr.start_time);
13172 printf("%s", sbuf_data(&sb));
13174 #endif /* CTL_TIME_IO */
13176 switch (io->io_hdr.io_type) {
13180 if (ctl_debug & CTL_DEBUG_INFO)
13181 ctl_io_error_print(io, NULL);
13185 panic("%s: Invalid CTL I/O type %d\n",
13186 __func__, io->io_hdr.io_type);
13189 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13191 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13192 io->io_hdr.nexus.targ_mapped_lun));
13196 mtx_lock(&lun->lun_lock);
13199 * Check to see if we have any informational exception and status
13200 * of this command can be modified to report it in form of either
13201 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field.
13203 if (lun->ie_reported == 0 && lun->ie_asc != 0 &&
13204 io->io_hdr.status == CTL_SUCCESS &&
13205 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) {
13206 uint8_t mrie = lun->MODE_IE.mrie;
13207 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) ||
13208 (lun->MODE_VER.byte3 & SMS_VER_PER));
13209 if (((mrie == SIEP_MRIE_REC_COND && per) ||
13210 mrie == SIEP_MRIE_REC_UNCOND ||
13211 mrie == SIEP_MRIE_NO_SENSE) &&
13212 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags &
13213 CTL_CMD_FLAG_NO_SENSE) == 0) {
13214 ctl_set_sense(&io->scsiio,
13215 /*current_error*/ 1,
13216 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ?
13217 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR,
13218 /*asc*/ lun->ie_asc,
13219 /*ascq*/ lun->ie_ascq,
13221 lun->ie_reported = 1;
13223 } else if (lun->ie_reported < 0)
13224 lun->ie_reported = 0;
13227 * Check to see if we have any errors to inject here. We only
13228 * inject errors for commands that don't already have errors set.
13230 if (!STAILQ_EMPTY(&lun->error_list) &&
13231 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
13232 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
13233 ctl_inject_error(lun, io);
13236 * XXX KDM how do we treat commands that aren't completed
13239 * XXX KDM should we also track I/O latency?
13241 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13242 io->io_hdr.io_type == CTL_IO_SCSI) {
13244 struct bintime cur_bt;
13248 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13250 type = CTL_STATS_READ;
13251 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13253 type = CTL_STATS_WRITE;
13255 type = CTL_STATS_NO_IO;
13257 lun->stats.ports[targ_port].bytes[type] +=
13258 io->scsiio.kern_total_len;
13259 lun->stats.ports[targ_port].operations[type]++;
13261 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
13262 &io->io_hdr.dma_bt);
13263 getbinuptime(&cur_bt);
13264 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
13265 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
13267 lun->stats.ports[targ_port].num_dmas[type] +=
13268 io->io_hdr.num_dmas;
13272 * Remove this from the OOA queue.
13274 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13276 if (TAILQ_EMPTY(&lun->ooa_queue))
13277 lun->last_busy = getsbinuptime();
13281 * Run through the blocked queue on this LUN and see if anything
13282 * has become unblocked, now that this transaction is done.
13284 ctl_check_blocked(lun);
13287 * If the LUN has been invalidated, free it if there is nothing
13288 * left on its OOA queue.
13290 if ((lun->flags & CTL_LUN_INVALID)
13291 && TAILQ_EMPTY(&lun->ooa_queue)) {
13292 mtx_unlock(&lun->lun_lock);
13293 mtx_lock(&softc->ctl_lock);
13295 mtx_unlock(&softc->ctl_lock);
13297 mtx_unlock(&lun->lun_lock);
13302 * If this command has been aborted, make sure we set the status
13303 * properly. The FETD is responsible for freeing the I/O and doing
13304 * whatever it needs to do to clean up its state.
13306 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13307 ctl_set_task_aborted(&io->scsiio);
13310 * If enabled, print command error status.
13312 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
13313 (ctl_debug & CTL_DEBUG_INFO) != 0)
13314 ctl_io_error_print(io, NULL);
13317 * Tell the FETD or the other shelf controller we're done with this
13318 * command. Note that only SCSI commands get to this point. Task
13319 * management commands are completed above.
13321 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
13322 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) {
13323 memset(&msg, 0, sizeof(msg));
13324 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13325 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
13326 msg.hdr.nexus = io->io_hdr.nexus;
13327 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13328 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data),
13337 * Front end should call this if it doesn't do autosense. When the request
13338 * sense comes back in from the initiator, we'll dequeue this and send it.
13341 ctl_queue_sense(union ctl_io *io)
13343 struct ctl_lun *lun;
13344 struct ctl_port *port;
13345 struct ctl_softc *softc;
13346 uint32_t initidx, targ_lun;
13348 softc = control_softc;
13350 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13353 * LUN lookup will likely move to the ctl_work_thread() once we
13354 * have our new queueing infrastructure (that doesn't put things on
13355 * a per-LUN queue initially). That is so that we can handle
13356 * things like an INQUIRY to a LUN that we don't have enabled. We
13357 * can't deal with that right now.
13359 mtx_lock(&softc->ctl_lock);
13362 * If we don't have a LUN for this, just toss the sense
13365 port = ctl_io_port(&ctsio->io_hdr);
13366 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13367 if ((targ_lun < CTL_MAX_LUNS)
13368 && (softc->ctl_luns[targ_lun] != NULL))
13369 lun = softc->ctl_luns[targ_lun];
13373 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13375 mtx_lock(&lun->lun_lock);
13377 * Already have CA set for this LUN...toss the sense information.
13379 if (ctl_is_set(lun->have_ca, initidx)) {
13380 mtx_unlock(&lun->lun_lock);
13384 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data,
13385 MIN(sizeof(lun->pending_sense[initidx]),
13386 sizeof(io->scsiio.sense_data)));
13387 ctl_set_mask(lun->have_ca, initidx);
13388 mtx_unlock(&lun->lun_lock);
13391 mtx_unlock(&softc->ctl_lock);
13395 return (CTL_RETVAL_COMPLETE);
13400 * Primary command inlet from frontend ports. All SCSI and task I/O
13401 * requests must go through this function.
13404 ctl_queue(union ctl_io *io)
13406 struct ctl_port *port;
13408 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13411 io->io_hdr.start_time = time_uptime;
13412 getbinuptime(&io->io_hdr.start_bt);
13413 #endif /* CTL_TIME_IO */
13415 /* Map FE-specific LUN ID into global one. */
13416 port = ctl_io_port(&io->io_hdr);
13417 io->io_hdr.nexus.targ_mapped_lun =
13418 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13420 switch (io->io_hdr.io_type) {
13423 if (ctl_debug & CTL_DEBUG_CDB)
13425 ctl_enqueue_incoming(io);
13428 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13432 return (CTL_RETVAL_COMPLETE);
13435 #ifdef CTL_IO_DELAY
13437 ctl_done_timer_wakeup(void *arg)
13441 io = (union ctl_io *)arg;
13444 #endif /* CTL_IO_DELAY */
13447 ctl_serseq_done(union ctl_io *io)
13449 struct ctl_lun *lun;
13451 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13452 if (lun->be_lun == NULL ||
13453 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF)
13455 mtx_lock(&lun->lun_lock);
13456 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE;
13457 ctl_check_blocked(lun);
13458 mtx_unlock(&lun->lun_lock);
13462 ctl_done(union ctl_io *io)
13466 * Enable this to catch duplicate completion issues.
13469 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13470 printf("%s: type %d msg %d cdb %x iptl: "
13471 "%u:%u:%u tag 0x%04x "
13472 "flag %#x status %x\n",
13474 io->io_hdr.io_type,
13475 io->io_hdr.msg_type,
13477 io->io_hdr.nexus.initid,
13478 io->io_hdr.nexus.targ_port,
13479 io->io_hdr.nexus.targ_lun,
13480 (io->io_hdr.io_type ==
13482 io->taskio.tag_num :
13483 io->scsiio.tag_num,
13485 io->io_hdr.status);
13487 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13491 * This is an internal copy of an I/O, and should not go through
13492 * the normal done processing logic.
13494 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13497 #ifdef CTL_IO_DELAY
13498 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13499 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13501 struct ctl_lun *lun;
13503 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13506 && (lun->delay_info.done_delay > 0)) {
13508 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
13509 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13510 callout_reset(&io->io_hdr.delay_callout,
13511 lun->delay_info.done_delay * hz,
13512 ctl_done_timer_wakeup, io);
13513 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13514 lun->delay_info.done_delay = 0;
13518 #endif /* CTL_IO_DELAY */
13520 ctl_enqueue_done(io);
13524 ctl_work_thread(void *arg)
13526 struct ctl_thread *thr = (struct ctl_thread *)arg;
13527 struct ctl_softc *softc = thr->ctl_softc;
13531 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13535 * We handle the queues in this order:
13537 * - done queue (to free up resources, unblock other commands)
13541 * If those queues are empty, we break out of the loop and
13544 mtx_lock(&thr->queue_lock);
13545 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13547 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13548 mtx_unlock(&thr->queue_lock);
13549 ctl_handle_isc(io);
13552 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13554 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13555 /* clear any blocked commands, call fe_done */
13556 mtx_unlock(&thr->queue_lock);
13557 ctl_process_done(io);
13560 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13562 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13563 mtx_unlock(&thr->queue_lock);
13564 if (io->io_hdr.io_type == CTL_IO_TASK)
13567 ctl_scsiio_precheck(softc, &io->scsiio);
13570 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13572 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13573 mtx_unlock(&thr->queue_lock);
13574 retval = ctl_scsiio(&io->scsiio);
13575 if (retval != CTL_RETVAL_COMPLETE)
13576 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13580 /* Sleep until we have something to do. */
13581 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13586 ctl_lun_thread(void *arg)
13588 struct ctl_softc *softc = (struct ctl_softc *)arg;
13589 struct ctl_be_lun *be_lun;
13591 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13594 mtx_lock(&softc->ctl_lock);
13595 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13596 if (be_lun != NULL) {
13597 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13598 mtx_unlock(&softc->ctl_lock);
13599 ctl_create_lun(be_lun);
13603 /* Sleep until we have something to do. */
13604 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13605 PDROP | PRIBIO, "-", 0);
13610 ctl_thresh_thread(void *arg)
13612 struct ctl_softc *softc = (struct ctl_softc *)arg;
13613 struct ctl_lun *lun;
13614 struct ctl_logical_block_provisioning_page *page;
13616 union ctl_ha_msg msg;
13617 uint64_t thres, val;
13620 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13623 mtx_lock(&softc->ctl_lock);
13624 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13625 if ((lun->flags & CTL_LUN_DISABLED) ||
13626 (lun->flags & CTL_LUN_NO_MEDIA) ||
13627 lun->backend->lun_attr == NULL)
13629 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
13630 softc->ha_mode == CTL_HA_MODE_XFER)
13632 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0)
13635 page = &lun->MODE_LBP;
13636 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13637 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13639 thres = scsi_4btoul(page->descr[i].count);
13640 thres <<= CTL_LBP_EXPONENT;
13641 switch (page->descr[i].resource) {
13643 attr = "blocksavail";
13646 attr = "blocksused";
13649 attr = "poolblocksavail";
13652 attr = "poolblocksused";
13657 mtx_unlock(&softc->ctl_lock); // XXX
13658 val = lun->backend->lun_attr(
13659 lun->be_lun->be_lun, attr);
13660 mtx_lock(&softc->ctl_lock);
13661 if (val == UINT64_MAX)
13663 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13664 == SLBPPD_ARMING_INC)
13665 e = (val >= thres);
13667 e = (val <= thres);
13671 mtx_lock(&lun->lun_lock);
13673 scsi_u64to8b((uint8_t *)&page->descr[i] -
13674 (uint8_t *)page, lun->ua_tpt_info);
13675 if (lun->lasttpt == 0 ||
13676 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13677 lun->lasttpt = time_uptime;
13678 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13684 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13687 mtx_unlock(&lun->lun_lock);
13689 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
13690 /* Send msg to other side. */
13691 bzero(&msg.ua, sizeof(msg.ua));
13692 msg.hdr.msg_type = CTL_MSG_UA;
13693 msg.hdr.nexus.initid = -1;
13694 msg.hdr.nexus.targ_port = -1;
13695 msg.hdr.nexus.targ_lun = lun->lun;
13696 msg.hdr.nexus.targ_mapped_lun = lun->lun;
13698 msg.ua.ua_set = (set > 0);
13699 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES;
13700 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8);
13701 mtx_unlock(&softc->ctl_lock); // XXX
13702 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13703 sizeof(msg.ua), M_WAITOK);
13704 mtx_lock(&softc->ctl_lock);
13707 mtx_unlock(&softc->ctl_lock);
13708 pause("-", CTL_LBP_PERIOD * hz);
13713 ctl_enqueue_incoming(union ctl_io *io)
13715 struct ctl_softc *softc = control_softc;
13716 struct ctl_thread *thr;
13719 idx = (io->io_hdr.nexus.targ_port * 127 +
13720 io->io_hdr.nexus.initid) % worker_threads;
13721 thr = &softc->threads[idx];
13722 mtx_lock(&thr->queue_lock);
13723 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13724 mtx_unlock(&thr->queue_lock);
13729 ctl_enqueue_rtr(union ctl_io *io)
13731 struct ctl_softc *softc = control_softc;
13732 struct ctl_thread *thr;
13734 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13735 mtx_lock(&thr->queue_lock);
13736 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13737 mtx_unlock(&thr->queue_lock);
13742 ctl_enqueue_done(union ctl_io *io)
13744 struct ctl_softc *softc = control_softc;
13745 struct ctl_thread *thr;
13747 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13748 mtx_lock(&thr->queue_lock);
13749 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13750 mtx_unlock(&thr->queue_lock);
13755 ctl_enqueue_isc(union ctl_io *io)
13757 struct ctl_softc *softc = control_softc;
13758 struct ctl_thread *thr;
13760 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13761 mtx_lock(&thr->queue_lock);
13762 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13763 mtx_unlock(&thr->queue_lock);