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 copan_debugconf_subpage debugconf_page_default = {
97 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
98 DBGCNF_SUBPAGE_CODE, /* subpage */
99 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
100 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
101 DBGCNF_VERSION, /* page_version */
102 {CTL_TIME_IO_DEFAULT_SECS>>8,
103 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */
106 const static struct copan_debugconf_subpage debugconf_page_changeable = {
107 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
108 DBGCNF_SUBPAGE_CODE, /* subpage */
109 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
110 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
111 0, /* page_version */
112 {0xff,0xff}, /* ctl_time_io_secs */
115 const static struct scsi_da_rw_recovery_page rw_er_page_default = {
116 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
117 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
118 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE,
119 /*read_retry_count*/0,
120 /*correction_span*/0,
121 /*head_offset_count*/0,
122 /*data_strobe_offset_cnt*/0,
123 /*byte8*/SMS_RWER_LBPERE,
124 /*write_retry_count*/0,
126 /*recovery_time_limit*/{0, 0},
129 const static struct scsi_da_rw_recovery_page rw_er_page_changeable = {
130 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
131 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
133 /*read_retry_count*/0,
134 /*correction_span*/0,
135 /*head_offset_count*/0,
136 /*data_strobe_offset_cnt*/0,
138 /*write_retry_count*/0,
140 /*recovery_time_limit*/{0, 0},
143 const static struct scsi_format_page format_page_default = {
144 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
145 /*page_length*/sizeof(struct scsi_format_page) - 2,
146 /*tracks_per_zone*/ {0, 0},
147 /*alt_sectors_per_zone*/ {0, 0},
148 /*alt_tracks_per_zone*/ {0, 0},
149 /*alt_tracks_per_lun*/ {0, 0},
150 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
151 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
152 /*bytes_per_sector*/ {0, 0},
153 /*interleave*/ {0, 0},
154 /*track_skew*/ {0, 0},
155 /*cylinder_skew*/ {0, 0},
157 /*reserved*/ {0, 0, 0}
160 const static struct scsi_format_page format_page_changeable = {
161 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
162 /*page_length*/sizeof(struct scsi_format_page) - 2,
163 /*tracks_per_zone*/ {0, 0},
164 /*alt_sectors_per_zone*/ {0, 0},
165 /*alt_tracks_per_zone*/ {0, 0},
166 /*alt_tracks_per_lun*/ {0, 0},
167 /*sectors_per_track*/ {0, 0},
168 /*bytes_per_sector*/ {0, 0},
169 /*interleave*/ {0, 0},
170 /*track_skew*/ {0, 0},
171 /*cylinder_skew*/ {0, 0},
173 /*reserved*/ {0, 0, 0}
176 const static struct scsi_rigid_disk_page rigid_disk_page_default = {
177 /*page_code*/SMS_RIGID_DISK_PAGE,
178 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
179 /*cylinders*/ {0, 0, 0},
180 /*heads*/ CTL_DEFAULT_HEADS,
181 /*start_write_precomp*/ {0, 0, 0},
182 /*start_reduced_current*/ {0, 0, 0},
183 /*step_rate*/ {0, 0},
184 /*landing_zone_cylinder*/ {0, 0, 0},
185 /*rpl*/ SRDP_RPL_DISABLED,
186 /*rotational_offset*/ 0,
188 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
189 CTL_DEFAULT_ROTATION_RATE & 0xff},
193 const static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
194 /*page_code*/SMS_RIGID_DISK_PAGE,
195 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
196 /*cylinders*/ {0, 0, 0},
198 /*start_write_precomp*/ {0, 0, 0},
199 /*start_reduced_current*/ {0, 0, 0},
200 /*step_rate*/ {0, 0},
201 /*landing_zone_cylinder*/ {0, 0, 0},
203 /*rotational_offset*/ 0,
205 /*rotation_rate*/ {0, 0},
209 const static struct scsi_caching_page caching_page_default = {
210 /*page_code*/SMS_CACHING_PAGE,
211 /*page_length*/sizeof(struct scsi_caching_page) - 2,
212 /*flags1*/ SCP_DISC | SCP_WCE,
214 /*disable_pf_transfer_len*/ {0xff, 0xff},
215 /*min_prefetch*/ {0, 0},
216 /*max_prefetch*/ {0xff, 0xff},
217 /*max_pf_ceiling*/ {0xff, 0xff},
219 /*cache_segments*/ 0,
220 /*cache_seg_size*/ {0, 0},
222 /*non_cache_seg_size*/ {0, 0, 0}
225 const static struct scsi_caching_page caching_page_changeable = {
226 /*page_code*/SMS_CACHING_PAGE,
227 /*page_length*/sizeof(struct scsi_caching_page) - 2,
228 /*flags1*/ SCP_WCE | SCP_RCD,
230 /*disable_pf_transfer_len*/ {0, 0},
231 /*min_prefetch*/ {0, 0},
232 /*max_prefetch*/ {0, 0},
233 /*max_pf_ceiling*/ {0, 0},
235 /*cache_segments*/ 0,
236 /*cache_seg_size*/ {0, 0},
238 /*non_cache_seg_size*/ {0, 0, 0}
241 const static struct scsi_control_page control_page_default = {
242 /*page_code*/SMS_CONTROL_MODE_PAGE,
243 /*page_length*/sizeof(struct scsi_control_page) - 2,
245 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED,
248 /*aen_holdoff_period*/{0, 0},
249 /*busy_timeout_period*/{0, 0},
250 /*extended_selftest_completion_time*/{0, 0}
253 const static struct scsi_control_page control_page_changeable = {
254 /*page_code*/SMS_CONTROL_MODE_PAGE,
255 /*page_length*/sizeof(struct scsi_control_page) - 2,
257 /*queue_flags*/SCP_QUEUE_ALG_MASK,
258 /*eca_and_aen*/SCP_SWP,
260 /*aen_holdoff_period*/{0, 0},
261 /*busy_timeout_period*/{0, 0},
262 /*extended_selftest_completion_time*/{0, 0}
265 #define CTL_CEM_LEN (sizeof(struct scsi_control_ext_page) - 4)
267 const static struct scsi_control_ext_page control_ext_page_default = {
268 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
269 /*subpage_code*/0x01,
270 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
276 const static struct scsi_control_ext_page control_ext_page_changeable = {
277 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
278 /*subpage_code*/0x01,
279 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
285 const static struct scsi_info_exceptions_page ie_page_default = {
286 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
287 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
288 /*info_flags*/SIEP_FLAGS_DEXCPT,
290 /*interval_timer*/{0, 0, 0, 0},
291 /*report_count*/{0, 0, 0, 0}
294 const static struct scsi_info_exceptions_page ie_page_changeable = {
295 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
296 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
299 /*interval_timer*/{0, 0, 0, 0},
300 /*report_count*/{0, 0, 0, 0}
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, 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 args[i].kname = ctl_copyin_alloc(args[i].name,
2429 args[i].namelen, error_str, error_str_len);
2430 if (args[i].kname == NULL)
2433 if (args[i].kname[args[i].namelen - 1] != '\0') {
2434 snprintf(error_str, error_str_len, "Argument %d "
2435 "name is not NUL-terminated", i);
2439 if (args[i].flags & CTL_BEARG_RD) {
2440 tmpptr = ctl_copyin_alloc(args[i].value,
2441 args[i].vallen, error_str, error_str_len);
2444 if ((args[i].flags & CTL_BEARG_ASCII)
2445 && (tmpptr[args[i].vallen - 1] != '\0')) {
2446 snprintf(error_str, error_str_len, "Argument "
2447 "%d value is not NUL-terminated", i);
2448 free(tmpptr, M_CTL);
2451 args[i].kvalue = tmpptr;
2453 args[i].kvalue = malloc(args[i].vallen,
2454 M_CTL, M_WAITOK | M_ZERO);
2461 ctl_free_args(num_args, args);
2467 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2471 for (i = 0; i < num_args; i++) {
2472 if (args[i].flags & CTL_BEARG_WR)
2473 copyout(args[i].kvalue, args[i].value, args[i].vallen);
2478 * Escape characters that are illegal or not recommended in XML.
2481 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2483 char *end = str + size;
2488 for (; *str && str < end; str++) {
2491 retval = sbuf_printf(sb, "&");
2494 retval = sbuf_printf(sb, ">");
2497 retval = sbuf_printf(sb, "<");
2500 retval = sbuf_putc(sb, *str);
2513 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2515 struct scsi_vpd_id_descriptor *desc;
2518 if (id == NULL || id->len < 4)
2520 desc = (struct scsi_vpd_id_descriptor *)id->data;
2521 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2522 case SVPD_ID_TYPE_T10:
2523 sbuf_printf(sb, "t10.");
2525 case SVPD_ID_TYPE_EUI64:
2526 sbuf_printf(sb, "eui.");
2528 case SVPD_ID_TYPE_NAA:
2529 sbuf_printf(sb, "naa.");
2531 case SVPD_ID_TYPE_SCSI_NAME:
2534 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2535 case SVPD_ID_CODESET_BINARY:
2536 for (i = 0; i < desc->length; i++)
2537 sbuf_printf(sb, "%02x", desc->identifier[i]);
2539 case SVPD_ID_CODESET_ASCII:
2540 sbuf_printf(sb, "%.*s", (int)desc->length,
2541 (char *)desc->identifier);
2543 case SVPD_ID_CODESET_UTF8:
2544 sbuf_printf(sb, "%s", (char *)desc->identifier);
2550 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2553 struct ctl_softc *softc = dev->si_drv1;
2554 struct ctl_lun *lun;
2561 retval = ctl_ioctl_io(dev, cmd, addr, flag, td);
2563 case CTL_ENABLE_PORT:
2564 case CTL_DISABLE_PORT:
2565 case CTL_SET_PORT_WWNS: {
2566 struct ctl_port *port;
2567 struct ctl_port_entry *entry;
2569 entry = (struct ctl_port_entry *)addr;
2571 mtx_lock(&softc->ctl_lock);
2572 STAILQ_FOREACH(port, &softc->port_list, links) {
2575 if (port->targ_port < softc->port_min ||
2576 port->targ_port >= softc->port_max)
2581 if ((entry->port_type == CTL_PORT_NONE)
2582 && (entry->targ_port == port->targ_port)) {
2584 * If the user only wants to enable or
2585 * disable or set WWNs on a specific port,
2586 * do the operation and we're done.
2590 } else if (entry->port_type & port->port_type) {
2592 * Compare the user's type mask with the
2593 * particular frontend type to see if we
2600 * Make sure the user isn't trying to set
2601 * WWNs on multiple ports at the same time.
2603 if (cmd == CTL_SET_PORT_WWNS) {
2604 printf("%s: Can't set WWNs on "
2605 "multiple ports\n", __func__);
2614 * XXX KDM we have to drop the lock here, because
2615 * the online/offline operations can potentially
2616 * block. We need to reference count the frontends
2617 * so they can't go away,
2619 if (cmd == CTL_ENABLE_PORT) {
2620 mtx_unlock(&softc->ctl_lock);
2621 ctl_port_online(port);
2622 mtx_lock(&softc->ctl_lock);
2623 } else if (cmd == CTL_DISABLE_PORT) {
2624 mtx_unlock(&softc->ctl_lock);
2625 ctl_port_offline(port);
2626 mtx_lock(&softc->ctl_lock);
2627 } else if (cmd == CTL_SET_PORT_WWNS) {
2628 ctl_port_set_wwns(port,
2629 (entry->flags & CTL_PORT_WWNN_VALID) ?
2631 (entry->flags & CTL_PORT_WWPN_VALID) ?
2632 1 : 0, entry->wwpn);
2637 mtx_unlock(&softc->ctl_lock);
2641 struct ctl_ooa *ooa_hdr;
2642 struct ctl_ooa_entry *entries;
2643 uint32_t cur_fill_num;
2645 ooa_hdr = (struct ctl_ooa *)addr;
2647 if ((ooa_hdr->alloc_len == 0)
2648 || (ooa_hdr->alloc_num == 0)) {
2649 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2650 "must be non-zero\n", __func__,
2651 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2656 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2657 sizeof(struct ctl_ooa_entry))) {
2658 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2659 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2660 __func__, ooa_hdr->alloc_len,
2661 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2666 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2667 if (entries == NULL) {
2668 printf("%s: could not allocate %d bytes for OOA "
2669 "dump\n", __func__, ooa_hdr->alloc_len);
2674 mtx_lock(&softc->ctl_lock);
2675 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2676 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS)
2677 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2678 mtx_unlock(&softc->ctl_lock);
2679 free(entries, M_CTL);
2680 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2681 __func__, (uintmax_t)ooa_hdr->lun_num);
2688 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2689 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2690 ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2694 lun = softc->ctl_luns[ooa_hdr->lun_num];
2695 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr,
2698 mtx_unlock(&softc->ctl_lock);
2700 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2701 ooa_hdr->fill_len = ooa_hdr->fill_num *
2702 sizeof(struct ctl_ooa_entry);
2703 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2705 printf("%s: error copying out %d bytes for OOA dump\n",
2706 __func__, ooa_hdr->fill_len);
2709 getbinuptime(&ooa_hdr->cur_bt);
2711 if (cur_fill_num > ooa_hdr->alloc_num) {
2712 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2713 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2715 ooa_hdr->dropped_num = 0;
2716 ooa_hdr->status = CTL_OOA_OK;
2719 free(entries, M_CTL);
2722 case CTL_DELAY_IO: {
2723 struct ctl_io_delay_info *delay_info;
2725 delay_info = (struct ctl_io_delay_info *)addr;
2728 mtx_lock(&softc->ctl_lock);
2730 if ((delay_info->lun_id >= CTL_MAX_LUNS)
2731 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2732 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2734 lun = softc->ctl_luns[delay_info->lun_id];
2735 mtx_lock(&lun->lun_lock);
2737 delay_info->status = CTL_DELAY_STATUS_OK;
2739 switch (delay_info->delay_type) {
2740 case CTL_DELAY_TYPE_CONT:
2742 case CTL_DELAY_TYPE_ONESHOT:
2745 delay_info->status =
2746 CTL_DELAY_STATUS_INVALID_TYPE;
2750 switch (delay_info->delay_loc) {
2751 case CTL_DELAY_LOC_DATAMOVE:
2752 lun->delay_info.datamove_type =
2753 delay_info->delay_type;
2754 lun->delay_info.datamove_delay =
2755 delay_info->delay_secs;
2757 case CTL_DELAY_LOC_DONE:
2758 lun->delay_info.done_type =
2759 delay_info->delay_type;
2760 lun->delay_info.done_delay =
2761 delay_info->delay_secs;
2764 delay_info->status =
2765 CTL_DELAY_STATUS_INVALID_LOC;
2768 mtx_unlock(&lun->lun_lock);
2771 mtx_unlock(&softc->ctl_lock);
2773 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2774 #endif /* CTL_IO_DELAY */
2777 case CTL_GETSTATS: {
2778 struct ctl_stats *stats;
2781 stats = (struct ctl_stats *)addr;
2783 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2785 stats->status = CTL_SS_NEED_MORE_SPACE;
2786 stats->num_luns = softc->num_luns;
2790 * XXX KDM no locking here. If the LUN list changes,
2791 * things can blow up.
2794 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2795 retval = copyout(&lun->stats, &stats->lun_stats[i++],
2796 sizeof(lun->stats));
2800 stats->num_luns = softc->num_luns;
2801 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2803 stats->status = CTL_SS_OK;
2805 stats->flags = CTL_STATS_FLAG_TIME_VALID;
2807 stats->flags = CTL_STATS_FLAG_NONE;
2809 getnanouptime(&stats->timestamp);
2812 case CTL_ERROR_INJECT: {
2813 struct ctl_error_desc *err_desc, *new_err_desc;
2815 err_desc = (struct ctl_error_desc *)addr;
2817 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2819 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2821 mtx_lock(&softc->ctl_lock);
2822 lun = softc->ctl_luns[err_desc->lun_id];
2824 mtx_unlock(&softc->ctl_lock);
2825 free(new_err_desc, M_CTL);
2826 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2827 __func__, (uintmax_t)err_desc->lun_id);
2831 mtx_lock(&lun->lun_lock);
2832 mtx_unlock(&softc->ctl_lock);
2835 * We could do some checking here to verify the validity
2836 * of the request, but given the complexity of error
2837 * injection requests, the checking logic would be fairly
2840 * For now, if the request is invalid, it just won't get
2841 * executed and might get deleted.
2843 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2846 * XXX KDM check to make sure the serial number is unique,
2847 * in case we somehow manage to wrap. That shouldn't
2848 * happen for a very long time, but it's the right thing to
2851 new_err_desc->serial = lun->error_serial;
2852 err_desc->serial = lun->error_serial;
2853 lun->error_serial++;
2855 mtx_unlock(&lun->lun_lock);
2858 case CTL_ERROR_INJECT_DELETE: {
2859 struct ctl_error_desc *delete_desc, *desc, *desc2;
2862 delete_desc = (struct ctl_error_desc *)addr;
2865 mtx_lock(&softc->ctl_lock);
2866 lun = softc->ctl_luns[delete_desc->lun_id];
2868 mtx_unlock(&softc->ctl_lock);
2869 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2870 __func__, (uintmax_t)delete_desc->lun_id);
2874 mtx_lock(&lun->lun_lock);
2875 mtx_unlock(&softc->ctl_lock);
2876 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2877 if (desc->serial != delete_desc->serial)
2880 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2885 mtx_unlock(&lun->lun_lock);
2886 if (delete_done == 0) {
2887 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2888 "error serial %ju on LUN %u\n", __func__,
2889 delete_desc->serial, delete_desc->lun_id);
2895 case CTL_DUMP_STRUCTS: {
2897 struct ctl_port *port;
2898 struct ctl_frontend *fe;
2900 mtx_lock(&softc->ctl_lock);
2901 printf("CTL Persistent Reservation information start:\n");
2902 for (i = 0; i < CTL_MAX_LUNS; i++) {
2903 lun = softc->ctl_luns[i];
2906 || ((lun->flags & CTL_LUN_DISABLED) != 0))
2909 for (j = 0; j < CTL_MAX_PORTS; j++) {
2910 if (lun->pr_keys[j] == NULL)
2912 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2913 if (lun->pr_keys[j][k] == 0)
2915 printf(" LUN %d port %d iid %d key "
2917 (uintmax_t)lun->pr_keys[j][k]);
2921 printf("CTL Persistent Reservation information end\n");
2922 printf("CTL Ports:\n");
2923 STAILQ_FOREACH(port, &softc->port_list, links) {
2924 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
2925 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
2926 port->frontend->name, port->port_type,
2927 port->physical_port, port->virtual_port,
2928 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
2929 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2930 if (port->wwpn_iid[j].in_use == 0 &&
2931 port->wwpn_iid[j].wwpn == 0 &&
2932 port->wwpn_iid[j].name == NULL)
2935 printf(" iid %u use %d WWPN %#jx '%s'\n",
2936 j, port->wwpn_iid[j].in_use,
2937 (uintmax_t)port->wwpn_iid[j].wwpn,
2938 port->wwpn_iid[j].name);
2941 printf("CTL Port information end\n");
2942 mtx_unlock(&softc->ctl_lock);
2944 * XXX KDM calling this without a lock. We'd likely want
2945 * to drop the lock before calling the frontend's dump
2948 printf("CTL Frontends:\n");
2949 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2950 printf(" Frontend '%s'\n", fe->name);
2951 if (fe->fe_dump != NULL)
2954 printf("CTL Frontend information end\n");
2958 struct ctl_lun_req *lun_req;
2959 struct ctl_backend_driver *backend;
2961 lun_req = (struct ctl_lun_req *)addr;
2963 backend = ctl_backend_find(lun_req->backend);
2964 if (backend == NULL) {
2965 lun_req->status = CTL_LUN_ERROR;
2966 snprintf(lun_req->error_str,
2967 sizeof(lun_req->error_str),
2968 "Backend \"%s\" not found.",
2972 if (lun_req->num_be_args > 0) {
2973 lun_req->kern_be_args = ctl_copyin_args(
2974 lun_req->num_be_args,
2977 sizeof(lun_req->error_str));
2978 if (lun_req->kern_be_args == NULL) {
2979 lun_req->status = CTL_LUN_ERROR;
2984 retval = backend->ioctl(dev, cmd, addr, flag, td);
2986 if (lun_req->num_be_args > 0) {
2987 ctl_copyout_args(lun_req->num_be_args,
2988 lun_req->kern_be_args);
2989 ctl_free_args(lun_req->num_be_args,
2990 lun_req->kern_be_args);
2994 case CTL_LUN_LIST: {
2996 struct ctl_lun_list *list;
2997 struct ctl_option *opt;
2999 list = (struct ctl_lun_list *)addr;
3002 * Allocate a fixed length sbuf here, based on the length
3003 * of the user's buffer. We could allocate an auto-extending
3004 * buffer, and then tell the user how much larger our
3005 * amount of data is than his buffer, but that presents
3008 * 1. The sbuf(9) routines use a blocking malloc, and so
3009 * we can't hold a lock while calling them with an
3010 * auto-extending buffer.
3012 * 2. There is not currently a LUN reference counting
3013 * mechanism, outside of outstanding transactions on
3014 * the LUN's OOA queue. So a LUN could go away on us
3015 * while we're getting the LUN number, backend-specific
3016 * information, etc. Thus, given the way things
3017 * currently work, we need to hold the CTL lock while
3018 * grabbing LUN information.
3020 * So, from the user's standpoint, the best thing to do is
3021 * allocate what he thinks is a reasonable buffer length,
3022 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3023 * double the buffer length and try again. (And repeat
3024 * that until he succeeds.)
3026 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3028 list->status = CTL_LUN_LIST_ERROR;
3029 snprintf(list->error_str, sizeof(list->error_str),
3030 "Unable to allocate %d bytes for LUN list",
3035 sbuf_printf(sb, "<ctllunlist>\n");
3037 mtx_lock(&softc->ctl_lock);
3038 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3039 mtx_lock(&lun->lun_lock);
3040 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3041 (uintmax_t)lun->lun);
3044 * Bail out as soon as we see that we've overfilled
3050 retval = sbuf_printf(sb, "\t<backend_type>%s"
3051 "</backend_type>\n",
3052 (lun->backend == NULL) ? "none" :
3053 lun->backend->name);
3058 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3059 lun->be_lun->lun_type);
3064 if (lun->backend == NULL) {
3065 retval = sbuf_printf(sb, "</lun>\n");
3071 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3072 (lun->be_lun->maxlba > 0) ?
3073 lun->be_lun->maxlba + 1 : 0);
3078 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3079 lun->be_lun->blocksize);
3084 retval = sbuf_printf(sb, "\t<serial_number>");
3089 retval = ctl_sbuf_printf_esc(sb,
3090 lun->be_lun->serial_num,
3091 sizeof(lun->be_lun->serial_num));
3096 retval = sbuf_printf(sb, "</serial_number>\n");
3101 retval = sbuf_printf(sb, "\t<device_id>");
3106 retval = ctl_sbuf_printf_esc(sb,
3107 lun->be_lun->device_id,
3108 sizeof(lun->be_lun->device_id));
3113 retval = sbuf_printf(sb, "</device_id>\n");
3118 if (lun->backend->lun_info != NULL) {
3119 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3123 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3124 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3125 opt->name, opt->value, opt->name);
3130 retval = sbuf_printf(sb, "</lun>\n");
3134 mtx_unlock(&lun->lun_lock);
3137 mtx_unlock(&lun->lun_lock);
3138 mtx_unlock(&softc->ctl_lock);
3141 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3144 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3145 snprintf(list->error_str, sizeof(list->error_str),
3146 "Out of space, %d bytes is too small",
3153 retval = copyout(sbuf_data(sb), list->lun_xml,
3156 list->fill_len = sbuf_len(sb) + 1;
3157 list->status = CTL_LUN_LIST_OK;
3162 struct ctl_iscsi *ci;
3163 struct ctl_frontend *fe;
3165 ci = (struct ctl_iscsi *)addr;
3167 fe = ctl_frontend_find("iscsi");
3169 ci->status = CTL_ISCSI_ERROR;
3170 snprintf(ci->error_str, sizeof(ci->error_str),
3171 "Frontend \"iscsi\" not found.");
3175 retval = fe->ioctl(dev, cmd, addr, flag, td);
3178 case CTL_PORT_REQ: {
3179 struct ctl_req *req;
3180 struct ctl_frontend *fe;
3182 req = (struct ctl_req *)addr;
3184 fe = ctl_frontend_find(req->driver);
3186 req->status = CTL_LUN_ERROR;
3187 snprintf(req->error_str, sizeof(req->error_str),
3188 "Frontend \"%s\" not found.", req->driver);
3191 if (req->num_args > 0) {
3192 req->kern_args = ctl_copyin_args(req->num_args,
3193 req->args, req->error_str, sizeof(req->error_str));
3194 if (req->kern_args == NULL) {
3195 req->status = CTL_LUN_ERROR;
3201 retval = fe->ioctl(dev, cmd, addr, flag, td);
3205 if (req->num_args > 0) {
3206 ctl_copyout_args(req->num_args, req->kern_args);
3207 ctl_free_args(req->num_args, req->kern_args);
3211 case CTL_PORT_LIST: {
3213 struct ctl_port *port;
3214 struct ctl_lun_list *list;
3215 struct ctl_option *opt;
3219 list = (struct ctl_lun_list *)addr;
3221 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3223 list->status = CTL_LUN_LIST_ERROR;
3224 snprintf(list->error_str, sizeof(list->error_str),
3225 "Unable to allocate %d bytes for LUN list",
3230 sbuf_printf(sb, "<ctlportlist>\n");
3232 mtx_lock(&softc->ctl_lock);
3233 STAILQ_FOREACH(port, &softc->port_list, links) {
3234 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3235 (uintmax_t)port->targ_port);
3238 * Bail out as soon as we see that we've overfilled
3244 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3245 "</frontend_type>\n", port->frontend->name);
3249 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3254 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3255 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3259 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3264 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3265 port->physical_port);
3269 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3270 port->virtual_port);
3274 if (port->target_devid != NULL) {
3275 sbuf_printf(sb, "\t<target>");
3276 ctl_id_sbuf(port->target_devid, sb);
3277 sbuf_printf(sb, "</target>\n");
3280 if (port->port_devid != NULL) {
3281 sbuf_printf(sb, "\t<port>");
3282 ctl_id_sbuf(port->port_devid, sb);
3283 sbuf_printf(sb, "</port>\n");
3286 if (port->port_info != NULL) {
3287 retval = port->port_info(port->onoff_arg, sb);
3291 STAILQ_FOREACH(opt, &port->options, links) {
3292 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3293 opt->name, opt->value, opt->name);
3298 if (port->lun_map != NULL) {
3299 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n");
3300 for (j = 0; j < CTL_MAX_LUNS; j++) {
3301 plun = ctl_lun_map_from_port(port, j);
3302 if (plun >= CTL_MAX_LUNS)
3305 "\t<lun id=\"%u\">%u</lun>\n",
3310 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3311 if (port->wwpn_iid[j].in_use == 0 ||
3312 (port->wwpn_iid[j].wwpn == 0 &&
3313 port->wwpn_iid[j].name == NULL))
3316 if (port->wwpn_iid[j].name != NULL)
3317 retval = sbuf_printf(sb,
3318 "\t<initiator id=\"%u\">%s</initiator>\n",
3319 j, port->wwpn_iid[j].name);
3321 retval = sbuf_printf(sb,
3322 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3323 j, port->wwpn_iid[j].wwpn);
3330 retval = sbuf_printf(sb, "</targ_port>\n");
3334 mtx_unlock(&softc->ctl_lock);
3337 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3340 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3341 snprintf(list->error_str, sizeof(list->error_str),
3342 "Out of space, %d bytes is too small",
3349 retval = copyout(sbuf_data(sb), list->lun_xml,
3352 list->fill_len = sbuf_len(sb) + 1;
3353 list->status = CTL_LUN_LIST_OK;
3358 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr;
3359 struct ctl_port *port;
3361 mtx_lock(&softc->ctl_lock);
3362 if (lm->port < softc->port_min ||
3363 lm->port >= softc->port_max ||
3364 (port = softc->ctl_ports[lm->port]) == NULL) {
3365 mtx_unlock(&softc->ctl_lock);
3368 if (port->status & CTL_PORT_STATUS_ONLINE) {
3369 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3370 if (ctl_lun_map_to_port(port, lun->lun) >=
3373 mtx_lock(&lun->lun_lock);
3374 ctl_est_ua_port(lun, lm->port, -1,
3376 mtx_unlock(&lun->lun_lock);
3379 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps
3380 if (lm->plun < CTL_MAX_LUNS) {
3381 if (lm->lun == UINT32_MAX)
3382 retval = ctl_lun_map_unset(port, lm->plun);
3383 else if (lm->lun < CTL_MAX_LUNS &&
3384 softc->ctl_luns[lm->lun] != NULL)
3385 retval = ctl_lun_map_set(port, lm->plun, lm->lun);
3388 } else if (lm->plun == UINT32_MAX) {
3389 if (lm->lun == UINT32_MAX)
3390 retval = ctl_lun_map_deinit(port);
3392 retval = ctl_lun_map_init(port);
3395 if (port->status & CTL_PORT_STATUS_ONLINE)
3396 ctl_isc_announce_port(port);
3400 /* XXX KDM should we fix this? */
3402 struct ctl_backend_driver *backend;
3409 * We encode the backend type as the ioctl type for backend
3410 * ioctls. So parse it out here, and then search for a
3411 * backend of this type.
3413 type = _IOC_TYPE(cmd);
3415 STAILQ_FOREACH(backend, &softc->be_list, links) {
3416 if (backend->type == type) {
3422 printf("ctl: unknown ioctl command %#lx or backend "
3427 retval = backend->ioctl(dev, cmd, addr, flag, td);
3437 ctl_get_initindex(struct ctl_nexus *nexus)
3439 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3443 ctl_lun_map_init(struct ctl_port *port)
3445 struct ctl_softc *softc = port->ctl_softc;
3446 struct ctl_lun *lun;
3449 if (port->lun_map == NULL)
3450 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS,
3452 if (port->lun_map == NULL)
3454 for (i = 0; i < CTL_MAX_LUNS; i++)
3455 port->lun_map[i] = UINT32_MAX;
3456 if (port->status & CTL_PORT_STATUS_ONLINE) {
3457 if (port->lun_disable != NULL) {
3458 STAILQ_FOREACH(lun, &softc->lun_list, links)
3459 port->lun_disable(port->targ_lun_arg, lun->lun);
3461 ctl_isc_announce_port(port);
3467 ctl_lun_map_deinit(struct ctl_port *port)
3469 struct ctl_softc *softc = port->ctl_softc;
3470 struct ctl_lun *lun;
3472 if (port->lun_map == NULL)
3474 free(port->lun_map, M_CTL);
3475 port->lun_map = NULL;
3476 if (port->status & CTL_PORT_STATUS_ONLINE) {
3477 if (port->lun_enable != NULL) {
3478 STAILQ_FOREACH(lun, &softc->lun_list, links)
3479 port->lun_enable(port->targ_lun_arg, lun->lun);
3481 ctl_isc_announce_port(port);
3487 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun)
3492 if (port->lun_map == NULL) {
3493 status = ctl_lun_map_init(port);
3497 old = port->lun_map[plun];
3498 port->lun_map[plun] = glun;
3499 if ((port->status & CTL_PORT_STATUS_ONLINE) && old >= CTL_MAX_LUNS) {
3500 if (port->lun_enable != NULL)
3501 port->lun_enable(port->targ_lun_arg, plun);
3502 ctl_isc_announce_port(port);
3508 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun)
3512 if (port->lun_map == NULL)
3514 old = port->lun_map[plun];
3515 port->lun_map[plun] = UINT32_MAX;
3516 if ((port->status & CTL_PORT_STATUS_ONLINE) && old < CTL_MAX_LUNS) {
3517 if (port->lun_disable != NULL)
3518 port->lun_disable(port->targ_lun_arg, plun);
3519 ctl_isc_announce_port(port);
3525 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
3529 return (UINT32_MAX);
3530 if (port->lun_map == NULL || lun_id >= CTL_MAX_LUNS)
3532 return (port->lun_map[lun_id]);
3536 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
3541 return (UINT32_MAX);
3542 if (port->lun_map == NULL)
3544 for (i = 0; i < CTL_MAX_LUNS; i++) {
3545 if (port->lun_map[i] == lun_id)
3548 return (UINT32_MAX);
3552 ctl_decode_lun(uint64_t encoded)
3555 uint32_t result = 0xffffffff;
3557 be64enc(lun, encoded);
3558 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) {
3559 case RPL_LUNDATA_ATYP_PERIPH:
3560 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 &&
3561 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0)
3564 case RPL_LUNDATA_ATYP_FLAT:
3565 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 &&
3566 lun[6] == 0 && lun[7] == 0)
3567 result = ((lun[0] & 0x3f) << 8) + lun[1];
3569 case RPL_LUNDATA_ATYP_EXTLUN:
3570 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) {
3572 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) {
3577 result = (lun[1] << 16) + (lun[2] << 8) +
3581 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0)
3582 result = (lun[2] << 24) +
3583 (lun[3] << 16) + (lun[4] << 8) +
3588 case RPL_LUNDATA_EXT_EAM_NOT_SPEC:
3589 result = 0xffffffff;
3598 ctl_encode_lun(uint32_t decoded)
3600 uint64_t l = decoded;
3603 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48));
3605 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48));
3607 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) |
3609 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16));
3612 static struct ctl_port *
3613 ctl_io_port(struct ctl_io_hdr *io_hdr)
3616 return (control_softc->ctl_ports[io_hdr->nexus.targ_port]);
3620 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last)
3624 for (i = first; i < last; i++) {
3625 if ((mask[i / 32] & (1 << (i % 32))) == 0)
3632 ctl_set_mask(uint32_t *mask, uint32_t bit)
3634 uint32_t chunk, piece;
3637 piece = bit % (sizeof(uint32_t) * 8);
3639 if ((mask[chunk] & (1 << piece)) != 0)
3642 mask[chunk] |= (1 << piece);
3648 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3650 uint32_t chunk, piece;
3653 piece = bit % (sizeof(uint32_t) * 8);
3655 if ((mask[chunk] & (1 << piece)) == 0)
3658 mask[chunk] &= ~(1 << piece);
3664 ctl_is_set(uint32_t *mask, uint32_t bit)
3666 uint32_t chunk, piece;
3669 piece = bit % (sizeof(uint32_t) * 8);
3671 if ((mask[chunk] & (1 << piece)) == 0)
3678 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3682 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3685 return (t[residx % CTL_MAX_INIT_PER_PORT]);
3689 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3693 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3696 t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3700 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3705 i = residx/CTL_MAX_INIT_PER_PORT;
3706 if (lun->pr_keys[i] != NULL)
3708 mtx_unlock(&lun->lun_lock);
3709 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3711 mtx_lock(&lun->lun_lock);
3712 if (lun->pr_keys[i] == NULL)
3713 lun->pr_keys[i] = p;
3719 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3723 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3724 KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3725 t[residx % CTL_MAX_INIT_PER_PORT] = key;
3729 * ctl_softc, pool_name, total_ctl_io are passed in.
3730 * npool is passed out.
3733 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3734 uint32_t total_ctl_io, void **npool)
3737 struct ctl_io_pool *pool;
3739 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3744 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3745 pool->ctl_softc = ctl_softc;
3746 pool->zone = uma_zsecond_create(pool->name, NULL,
3747 NULL, NULL, NULL, ctl_softc->io_zone);
3748 /* uma_prealloc(pool->zone, total_ctl_io); */
3752 *npool = ctl_softc->io_zone;
3758 ctl_pool_free(struct ctl_io_pool *pool)
3765 uma_zdestroy(pool->zone);
3771 ctl_alloc_io(void *pool_ref)
3775 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3777 io = uma_zalloc(pool->zone, M_WAITOK);
3779 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK);
3782 io->io_hdr.pool = pool_ref;
3787 ctl_alloc_io_nowait(void *pool_ref)
3791 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3793 io = uma_zalloc(pool->zone, M_NOWAIT);
3795 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT);
3798 io->io_hdr.pool = pool_ref;
3803 ctl_free_io(union ctl_io *io)
3806 struct ctl_io_pool *pool;
3813 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3814 uma_zfree(pool->zone, io);
3816 uma_zfree((uma_zone_t)io->io_hdr.pool, io);
3821 ctl_zero_io(union ctl_io *io)
3829 * May need to preserve linked list pointers at some point too.
3831 pool_ref = io->io_hdr.pool;
3832 memset(io, 0, sizeof(*io));
3833 io->io_hdr.pool = pool_ref;
3837 ctl_expand_number(const char *buf, uint64_t *num)
3843 number = strtoq(buf, &endptr, 0);
3845 switch (tolower((unsigned char)*endptr)) {
3865 case '\0': /* No unit. */
3869 /* Unrecognized unit. */
3873 if ((number << shift) >> shift != number) {
3877 *num = number << shift;
3883 * This routine could be used in the future to load default and/or saved
3884 * mode page parameters for a particuar lun.
3887 ctl_init_page_index(struct ctl_lun *lun)
3890 struct ctl_page_index *page_index;
3894 memcpy(&lun->mode_pages.index, page_index_template,
3895 sizeof(page_index_template));
3897 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3899 page_index = &lun->mode_pages.index[i];
3900 if (lun->be_lun->lun_type == T_DIRECT &&
3901 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
3903 if (lun->be_lun->lun_type == T_PROCESSOR &&
3904 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
3906 if (lun->be_lun->lun_type == T_CDROM &&
3907 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
3910 page_code = page_index->page_code & SMPH_PC_MASK;
3911 switch (page_code) {
3912 case SMS_RW_ERROR_RECOVERY_PAGE: {
3913 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
3914 ("subpage %#x for page %#x is incorrect!",
3915 page_index->subpage, page_code));
3916 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
3917 &rw_er_page_default,
3918 sizeof(rw_er_page_default));
3919 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
3920 &rw_er_page_changeable,
3921 sizeof(rw_er_page_changeable));
3922 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
3923 &rw_er_page_default,
3924 sizeof(rw_er_page_default));
3925 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
3926 &rw_er_page_default,
3927 sizeof(rw_er_page_default));
3928 page_index->page_data =
3929 (uint8_t *)lun->mode_pages.rw_er_page;
3932 case SMS_FORMAT_DEVICE_PAGE: {
3933 struct scsi_format_page *format_page;
3935 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
3936 ("subpage %#x for page %#x is incorrect!",
3937 page_index->subpage, page_code));
3940 * Sectors per track are set above. Bytes per
3941 * sector need to be set here on a per-LUN basis.
3943 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
3944 &format_page_default,
3945 sizeof(format_page_default));
3946 memcpy(&lun->mode_pages.format_page[
3947 CTL_PAGE_CHANGEABLE], &format_page_changeable,
3948 sizeof(format_page_changeable));
3949 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
3950 &format_page_default,
3951 sizeof(format_page_default));
3952 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
3953 &format_page_default,
3954 sizeof(format_page_default));
3956 format_page = &lun->mode_pages.format_page[
3958 scsi_ulto2b(lun->be_lun->blocksize,
3959 format_page->bytes_per_sector);
3961 format_page = &lun->mode_pages.format_page[
3963 scsi_ulto2b(lun->be_lun->blocksize,
3964 format_page->bytes_per_sector);
3966 format_page = &lun->mode_pages.format_page[
3968 scsi_ulto2b(lun->be_lun->blocksize,
3969 format_page->bytes_per_sector);
3971 page_index->page_data =
3972 (uint8_t *)lun->mode_pages.format_page;
3975 case SMS_RIGID_DISK_PAGE: {
3976 struct scsi_rigid_disk_page *rigid_disk_page;
3977 uint32_t sectors_per_cylinder;
3981 #endif /* !__XSCALE__ */
3983 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
3984 ("subpage %#x for page %#x is incorrect!",
3985 page_index->subpage, page_code));
3988 * Rotation rate and sectors per track are set
3989 * above. We calculate the cylinders here based on
3990 * capacity. Due to the number of heads and
3991 * sectors per track we're using, smaller arrays
3992 * may turn out to have 0 cylinders. Linux and
3993 * FreeBSD don't pay attention to these mode pages
3994 * to figure out capacity, but Solaris does. It
3995 * seems to deal with 0 cylinders just fine, and
3996 * works out a fake geometry based on the capacity.
3998 memcpy(&lun->mode_pages.rigid_disk_page[
3999 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4000 sizeof(rigid_disk_page_default));
4001 memcpy(&lun->mode_pages.rigid_disk_page[
4002 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4003 sizeof(rigid_disk_page_changeable));
4005 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4009 * The divide method here will be more accurate,
4010 * probably, but results in floating point being
4011 * used in the kernel on i386 (__udivdi3()). On the
4012 * XScale, though, __udivdi3() is implemented in
4015 * The shift method for cylinder calculation is
4016 * accurate if sectors_per_cylinder is a power of
4017 * 2. Otherwise it might be slightly off -- you
4018 * might have a bit of a truncation problem.
4021 cylinders = (lun->be_lun->maxlba + 1) /
4022 sectors_per_cylinder;
4024 for (shift = 31; shift > 0; shift--) {
4025 if (sectors_per_cylinder & (1 << shift))
4028 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4032 * We've basically got 3 bytes, or 24 bits for the
4033 * cylinder size in the mode page. If we're over,
4034 * just round down to 2^24.
4036 if (cylinders > 0xffffff)
4037 cylinders = 0xffffff;
4039 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4041 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4043 if ((value = ctl_get_opt(&lun->be_lun->options,
4045 scsi_ulto2b(strtol(value, NULL, 0),
4046 rigid_disk_page->rotation_rate);
4049 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
4050 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4051 sizeof(rigid_disk_page_default));
4052 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
4053 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4054 sizeof(rigid_disk_page_default));
4056 page_index->page_data =
4057 (uint8_t *)lun->mode_pages.rigid_disk_page;
4060 case SMS_CACHING_PAGE: {
4061 struct scsi_caching_page *caching_page;
4063 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4064 ("subpage %#x for page %#x is incorrect!",
4065 page_index->subpage, page_code));
4066 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4067 &caching_page_default,
4068 sizeof(caching_page_default));
4069 memcpy(&lun->mode_pages.caching_page[
4070 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4071 sizeof(caching_page_changeable));
4072 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4073 &caching_page_default,
4074 sizeof(caching_page_default));
4075 caching_page = &lun->mode_pages.caching_page[
4077 value = ctl_get_opt(&lun->be_lun->options, "writecache");
4078 if (value != NULL && strcmp(value, "off") == 0)
4079 caching_page->flags1 &= ~SCP_WCE;
4080 value = ctl_get_opt(&lun->be_lun->options, "readcache");
4081 if (value != NULL && strcmp(value, "off") == 0)
4082 caching_page->flags1 |= SCP_RCD;
4083 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4084 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4085 sizeof(caching_page_default));
4086 page_index->page_data =
4087 (uint8_t *)lun->mode_pages.caching_page;
4090 case SMS_CONTROL_MODE_PAGE: {
4091 switch (page_index->subpage) {
4092 case SMS_SUBPAGE_PAGE_0: {
4093 struct scsi_control_page *control_page;
4095 memcpy(&lun->mode_pages.control_page[
4097 &control_page_default,
4098 sizeof(control_page_default));
4099 memcpy(&lun->mode_pages.control_page[
4100 CTL_PAGE_CHANGEABLE],
4101 &control_page_changeable,
4102 sizeof(control_page_changeable));
4103 memcpy(&lun->mode_pages.control_page[
4105 &control_page_default,
4106 sizeof(control_page_default));
4107 control_page = &lun->mode_pages.control_page[
4109 value = ctl_get_opt(&lun->be_lun->options,
4111 if (value != NULL &&
4112 strcmp(value, "unrestricted") == 0) {
4113 control_page->queue_flags &=
4114 ~SCP_QUEUE_ALG_MASK;
4115 control_page->queue_flags |=
4116 SCP_QUEUE_ALG_UNRESTRICTED;
4118 memcpy(&lun->mode_pages.control_page[
4120 &lun->mode_pages.control_page[
4122 sizeof(control_page_default));
4123 page_index->page_data =
4124 (uint8_t *)lun->mode_pages.control_page;
4128 memcpy(&lun->mode_pages.control_ext_page[
4130 &control_ext_page_default,
4131 sizeof(control_ext_page_default));
4132 memcpy(&lun->mode_pages.control_ext_page[
4133 CTL_PAGE_CHANGEABLE],
4134 &control_ext_page_changeable,
4135 sizeof(control_ext_page_changeable));
4136 memcpy(&lun->mode_pages.control_ext_page[
4138 &control_ext_page_default,
4139 sizeof(control_ext_page_default));
4140 memcpy(&lun->mode_pages.control_ext_page[
4142 &lun->mode_pages.control_ext_page[
4144 sizeof(control_ext_page_default));
4145 page_index->page_data =
4146 (uint8_t *)lun->mode_pages.control_ext_page;
4149 panic("subpage %#x for page %#x is incorrect!",
4150 page_index->subpage, page_code);
4154 case SMS_INFO_EXCEPTIONS_PAGE: {
4155 switch (page_index->subpage) {
4156 case SMS_SUBPAGE_PAGE_0:
4157 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4159 sizeof(ie_page_default));
4160 memcpy(&lun->mode_pages.ie_page[
4161 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4162 sizeof(ie_page_changeable));
4163 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4165 sizeof(ie_page_default));
4166 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4168 sizeof(ie_page_default));
4169 page_index->page_data =
4170 (uint8_t *)lun->mode_pages.ie_page;
4173 struct ctl_logical_block_provisioning_page *page;
4175 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4177 sizeof(lbp_page_default));
4178 memcpy(&lun->mode_pages.lbp_page[
4179 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4180 sizeof(lbp_page_changeable));
4181 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4183 sizeof(lbp_page_default));
4184 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4185 value = ctl_get_opt(&lun->be_lun->options,
4187 if (value != NULL &&
4188 ctl_expand_number(value, &ival) == 0) {
4189 page->descr[0].flags |= SLBPPD_ENABLED |
4191 if (lun->be_lun->blocksize)
4192 ival /= lun->be_lun->blocksize;
4195 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4196 page->descr[0].count);
4198 value = ctl_get_opt(&lun->be_lun->options,
4200 if (value != NULL &&
4201 ctl_expand_number(value, &ival) == 0) {
4202 page->descr[1].flags |= SLBPPD_ENABLED |
4204 if (lun->be_lun->blocksize)
4205 ival /= lun->be_lun->blocksize;
4208 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4209 page->descr[1].count);
4211 value = ctl_get_opt(&lun->be_lun->options,
4212 "pool-avail-threshold");
4213 if (value != NULL &&
4214 ctl_expand_number(value, &ival) == 0) {
4215 page->descr[2].flags |= SLBPPD_ENABLED |
4217 if (lun->be_lun->blocksize)
4218 ival /= lun->be_lun->blocksize;
4221 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4222 page->descr[2].count);
4224 value = ctl_get_opt(&lun->be_lun->options,
4225 "pool-used-threshold");
4226 if (value != NULL &&
4227 ctl_expand_number(value, &ival) == 0) {
4228 page->descr[3].flags |= SLBPPD_ENABLED |
4230 if (lun->be_lun->blocksize)
4231 ival /= lun->be_lun->blocksize;
4234 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4235 page->descr[3].count);
4237 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4238 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4239 sizeof(lbp_page_default));
4240 page_index->page_data =
4241 (uint8_t *)lun->mode_pages.lbp_page;
4245 panic("subpage %#x for page %#x is incorrect!",
4246 page_index->subpage, page_code);
4250 case SMS_CDDVD_CAPS_PAGE:{
4251 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4252 ("subpage %#x for page %#x is incorrect!",
4253 page_index->subpage, page_code));
4254 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT],
4255 &cddvd_page_default,
4256 sizeof(cddvd_page_default));
4257 memcpy(&lun->mode_pages.cddvd_page[
4258 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable,
4259 sizeof(cddvd_page_changeable));
4260 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4261 &cddvd_page_default,
4262 sizeof(cddvd_page_default));
4263 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT],
4264 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4265 sizeof(cddvd_page_default));
4266 page_index->page_data =
4267 (uint8_t *)lun->mode_pages.cddvd_page;
4270 case SMS_VENDOR_SPECIFIC_PAGE:{
4271 switch (page_index->subpage) {
4272 case DBGCNF_SUBPAGE_CODE: {
4273 memcpy(&lun->mode_pages.debugconf_subpage[
4275 &debugconf_page_default,
4276 sizeof(debugconf_page_default));
4277 memcpy(&lun->mode_pages.debugconf_subpage[
4278 CTL_PAGE_CHANGEABLE],
4279 &debugconf_page_changeable,
4280 sizeof(debugconf_page_changeable));
4281 memcpy(&lun->mode_pages.debugconf_subpage[
4283 &debugconf_page_default,
4284 sizeof(debugconf_page_default));
4285 memcpy(&lun->mode_pages.debugconf_subpage[
4287 &debugconf_page_default,
4288 sizeof(debugconf_page_default));
4289 page_index->page_data =
4290 (uint8_t *)lun->mode_pages.debugconf_subpage;
4294 panic("subpage %#x for page %#x is incorrect!",
4295 page_index->subpage, page_code);
4300 panic("invalid page code value %#x", page_code);
4304 return (CTL_RETVAL_COMPLETE);
4308 ctl_init_log_page_index(struct ctl_lun *lun)
4310 struct ctl_page_index *page_index;
4313 memcpy(&lun->log_pages.index, log_page_index_template,
4314 sizeof(log_page_index_template));
4317 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4319 page_index = &lun->log_pages.index[i];
4320 if (lun->be_lun->lun_type == T_DIRECT &&
4321 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4323 if (lun->be_lun->lun_type == T_PROCESSOR &&
4324 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4326 if (lun->be_lun->lun_type == T_CDROM &&
4327 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4330 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4331 lun->backend->lun_attr == NULL)
4334 if (page_index->page_code != prev) {
4335 lun->log_pages.pages_page[j] = page_index->page_code;
4336 prev = page_index->page_code;
4339 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4340 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4343 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4344 lun->log_pages.index[0].page_len = j;
4345 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4346 lun->log_pages.index[1].page_len = k * 2;
4347 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4348 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4349 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page;
4350 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page);
4352 return (CTL_RETVAL_COMPLETE);
4356 hex2bin(const char *str, uint8_t *buf, int buf_size)
4361 memset(buf, 0, buf_size);
4362 while (isspace(str[0]))
4364 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4367 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4371 else if (isalpha(c))
4372 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4378 buf[i / 2] |= (c << 4);
4382 return ((i + 1) / 2);
4389 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4390 * wants us to allocate the LUN and he can block.
4391 * - ctl_softc is always set
4392 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4394 * Returns 0 for success, non-zero (errno) for failure.
4397 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4398 struct ctl_be_lun *const be_lun)
4400 struct ctl_lun *nlun, *lun;
4401 struct scsi_vpd_id_descriptor *desc;
4402 struct scsi_vpd_id_t10 *t10id;
4403 const char *eui, *naa, *scsiname, *vendor, *value;
4404 int lun_number, i, lun_malloced;
4405 int devidlen, idlen1, idlen2 = 0, len;
4411 * We currently only support Direct Access or Processor LUN types.
4413 switch (be_lun->lun_type) {
4421 be_lun->lun_config_status(be_lun->be_lun,
4422 CTL_LUN_CONFIG_FAILURE);
4425 if (ctl_lun == NULL) {
4426 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4433 memset(lun, 0, sizeof(*lun));
4435 lun->flags = CTL_LUN_MALLOCED;
4437 /* Generate LUN ID. */
4438 devidlen = max(CTL_DEVID_MIN_LEN,
4439 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4440 idlen1 = sizeof(*t10id) + devidlen;
4441 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4442 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4443 if (scsiname != NULL) {
4444 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4445 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4447 eui = ctl_get_opt(&be_lun->options, "eui");
4449 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4451 naa = ctl_get_opt(&be_lun->options, "naa");
4453 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4455 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4456 M_CTL, M_WAITOK | M_ZERO);
4457 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4458 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4459 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4460 desc->length = idlen1;
4461 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4462 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4463 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4464 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4466 strncpy(t10id->vendor, vendor,
4467 min(sizeof(t10id->vendor), strlen(vendor)));
4469 strncpy((char *)t10id->vendor_spec_id,
4470 (char *)be_lun->device_id, devidlen);
4471 if (scsiname != NULL) {
4472 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4474 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4475 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4476 SVPD_ID_TYPE_SCSI_NAME;
4477 desc->length = idlen2;
4478 strlcpy(desc->identifier, scsiname, idlen2);
4481 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4483 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4484 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4486 desc->length = hex2bin(eui, desc->identifier, 16);
4487 desc->length = desc->length > 12 ? 16 :
4488 (desc->length > 8 ? 12 : 8);
4489 len -= 16 - desc->length;
4492 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4494 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4495 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4497 desc->length = hex2bin(naa, desc->identifier, 16);
4498 desc->length = desc->length > 8 ? 16 : 8;
4499 len -= 16 - desc->length;
4501 lun->lun_devid->len = len;
4503 mtx_lock(&ctl_softc->ctl_lock);
4505 * See if the caller requested a particular LUN number. If so, see
4506 * if it is available. Otherwise, allocate the first available LUN.
4508 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4509 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4510 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4511 mtx_unlock(&ctl_softc->ctl_lock);
4512 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4513 printf("ctl: requested LUN ID %d is higher "
4514 "than CTL_MAX_LUNS - 1 (%d)\n",
4515 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4518 * XXX KDM return an error, or just assign
4519 * another LUN ID in this case??
4521 printf("ctl: requested LUN ID %d is already "
4522 "in use\n", be_lun->req_lun_id);
4524 if (lun->flags & CTL_LUN_MALLOCED)
4526 be_lun->lun_config_status(be_lun->be_lun,
4527 CTL_LUN_CONFIG_FAILURE);
4530 lun_number = be_lun->req_lun_id;
4532 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, CTL_MAX_LUNS);
4533 if (lun_number == -1) {
4534 mtx_unlock(&ctl_softc->ctl_lock);
4535 printf("ctl: can't allocate LUN, out of LUNs\n");
4536 if (lun->flags & CTL_LUN_MALLOCED)
4538 be_lun->lun_config_status(be_lun->be_lun,
4539 CTL_LUN_CONFIG_FAILURE);
4543 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4545 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4546 lun->lun = lun_number;
4547 lun->be_lun = be_lun;
4549 * The processor LUN is always enabled. Disk LUNs come on line
4550 * disabled, and must be enabled by the backend.
4552 lun->flags |= CTL_LUN_DISABLED;
4553 lun->backend = be_lun->be;
4554 be_lun->ctl_lun = lun;
4555 be_lun->lun_id = lun_number;
4556 atomic_add_int(&be_lun->be->num_luns, 1);
4557 if (be_lun->flags & CTL_LUN_FLAG_EJECTED)
4558 lun->flags |= CTL_LUN_EJECTED;
4559 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA)
4560 lun->flags |= CTL_LUN_NO_MEDIA;
4561 if (be_lun->flags & CTL_LUN_FLAG_STOPPED)
4562 lun->flags |= CTL_LUN_STOPPED;
4564 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4565 lun->flags |= CTL_LUN_PRIMARY_SC;
4567 value = ctl_get_opt(&be_lun->options, "removable");
4568 if (value != NULL) {
4569 if (strcmp(value, "on") == 0)
4570 lun->flags |= CTL_LUN_REMOVABLE;
4571 } else if (be_lun->lun_type == T_CDROM)
4572 lun->flags |= CTL_LUN_REMOVABLE;
4574 lun->ctl_softc = ctl_softc;
4576 lun->last_busy = getsbinuptime();
4578 TAILQ_INIT(&lun->ooa_queue);
4579 TAILQ_INIT(&lun->blocked_queue);
4580 STAILQ_INIT(&lun->error_list);
4581 ctl_tpc_lun_init(lun);
4584 * Initialize the mode and log page index.
4586 ctl_init_page_index(lun);
4587 ctl_init_log_page_index(lun);
4590 * Now, before we insert this lun on the lun list, set the lun
4591 * inventory changed UA for all other luns.
4593 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4594 mtx_lock(&nlun->lun_lock);
4595 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4596 mtx_unlock(&nlun->lun_lock);
4599 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4601 ctl_softc->ctl_luns[lun_number] = lun;
4603 ctl_softc->num_luns++;
4605 /* Setup statistics gathering */
4606 lun->stats.device_type = be_lun->lun_type;
4607 lun->stats.lun_number = lun_number;
4608 lun->stats.blocksize = be_lun->blocksize;
4609 if (be_lun->blocksize == 0)
4610 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4611 for (i = 0;i < CTL_MAX_PORTS;i++)
4612 lun->stats.ports[i].targ_port = i;
4614 mtx_unlock(&ctl_softc->ctl_lock);
4616 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4623 * - LUN has already been marked invalid and any pending I/O has been taken
4627 ctl_free_lun(struct ctl_lun *lun)
4629 struct ctl_softc *softc;
4630 struct ctl_lun *nlun;
4633 softc = lun->ctl_softc;
4635 mtx_assert(&softc->ctl_lock, MA_OWNED);
4637 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4639 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4641 softc->ctl_luns[lun->lun] = NULL;
4643 if (!TAILQ_EMPTY(&lun->ooa_queue))
4644 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4649 * Tell the backend to free resources, if this LUN has a backend.
4651 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4652 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4654 ctl_tpc_lun_shutdown(lun);
4655 mtx_destroy(&lun->lun_lock);
4656 free(lun->lun_devid, M_CTL);
4657 for (i = 0; i < CTL_MAX_PORTS; i++)
4658 free(lun->pending_ua[i], M_CTL);
4659 for (i = 0; i < CTL_MAX_PORTS; i++)
4660 free(lun->pr_keys[i], M_CTL);
4661 free(lun->write_buffer, M_CTL);
4662 if (lun->flags & CTL_LUN_MALLOCED)
4665 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4666 mtx_lock(&nlun->lun_lock);
4667 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4668 mtx_unlock(&nlun->lun_lock);
4675 ctl_create_lun(struct ctl_be_lun *be_lun)
4679 * ctl_alloc_lun() should handle all potential failure cases.
4681 ctl_alloc_lun(control_softc, NULL, be_lun);
4685 ctl_add_lun(struct ctl_be_lun *be_lun)
4687 struct ctl_softc *softc = control_softc;
4689 mtx_lock(&softc->ctl_lock);
4690 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links);
4691 mtx_unlock(&softc->ctl_lock);
4692 wakeup(&softc->pending_lun_queue);
4698 ctl_enable_lun(struct ctl_be_lun *be_lun)
4700 struct ctl_softc *softc;
4701 struct ctl_port *port, *nport;
4702 struct ctl_lun *lun;
4705 lun = (struct ctl_lun *)be_lun->ctl_lun;
4706 softc = lun->ctl_softc;
4708 mtx_lock(&softc->ctl_lock);
4709 mtx_lock(&lun->lun_lock);
4710 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4712 * eh? Why did we get called if the LUN is already
4715 mtx_unlock(&lun->lun_lock);
4716 mtx_unlock(&softc->ctl_lock);
4719 lun->flags &= ~CTL_LUN_DISABLED;
4720 mtx_unlock(&lun->lun_lock);
4722 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) {
4723 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4724 port->lun_map != NULL || port->lun_enable == NULL)
4728 * Drop the lock while we call the FETD's enable routine.
4729 * This can lead to a callback into CTL (at least in the
4730 * case of the internal initiator frontend.
4732 mtx_unlock(&softc->ctl_lock);
4733 retval = port->lun_enable(port->targ_lun_arg, lun->lun);
4734 mtx_lock(&softc->ctl_lock);
4736 printf("%s: FETD %s port %d returned error "
4737 "%d for lun_enable on lun %jd\n",
4738 __func__, port->port_name, port->targ_port,
4739 retval, (intmax_t)lun->lun);
4743 mtx_unlock(&softc->ctl_lock);
4744 ctl_isc_announce_lun(lun);
4750 ctl_disable_lun(struct ctl_be_lun *be_lun)
4752 struct ctl_softc *softc;
4753 struct ctl_port *port;
4754 struct ctl_lun *lun;
4757 lun = (struct ctl_lun *)be_lun->ctl_lun;
4758 softc = lun->ctl_softc;
4760 mtx_lock(&softc->ctl_lock);
4761 mtx_lock(&lun->lun_lock);
4762 if (lun->flags & CTL_LUN_DISABLED) {
4763 mtx_unlock(&lun->lun_lock);
4764 mtx_unlock(&softc->ctl_lock);
4767 lun->flags |= CTL_LUN_DISABLED;
4768 mtx_unlock(&lun->lun_lock);
4770 STAILQ_FOREACH(port, &softc->port_list, links) {
4771 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4772 port->lun_map != NULL || port->lun_disable == NULL)
4776 * Drop the lock before we call the frontend's disable
4777 * routine, to avoid lock order reversals.
4779 * XXX KDM what happens if the frontend list changes while
4780 * we're traversing it? It's unlikely, but should be handled.
4782 mtx_unlock(&softc->ctl_lock);
4783 retval = port->lun_disable(port->targ_lun_arg, lun->lun);
4784 mtx_lock(&softc->ctl_lock);
4786 printf("%s: FETD %s port %d returned error "
4787 "%d for lun_disable on lun %jd\n",
4788 __func__, port->port_name, port->targ_port,
4789 retval, (intmax_t)lun->lun);
4793 mtx_unlock(&softc->ctl_lock);
4794 ctl_isc_announce_lun(lun);
4800 ctl_start_lun(struct ctl_be_lun *be_lun)
4802 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4804 mtx_lock(&lun->lun_lock);
4805 lun->flags &= ~CTL_LUN_STOPPED;
4806 mtx_unlock(&lun->lun_lock);
4811 ctl_stop_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_lun_no_media(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_NO_MEDIA;
4828 mtx_unlock(&lun->lun_lock);
4833 ctl_lun_has_media(struct ctl_be_lun *be_lun)
4835 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4836 union ctl_ha_msg msg;
4838 mtx_lock(&lun->lun_lock);
4839 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED);
4840 if (lun->flags & CTL_LUN_REMOVABLE)
4841 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE);
4842 mtx_unlock(&lun->lun_lock);
4843 if ((lun->flags & CTL_LUN_REMOVABLE) &&
4844 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4845 bzero(&msg.ua, sizeof(msg.ua));
4846 msg.hdr.msg_type = CTL_MSG_UA;
4847 msg.hdr.nexus.initid = -1;
4848 msg.hdr.nexus.targ_port = -1;
4849 msg.hdr.nexus.targ_lun = lun->lun;
4850 msg.hdr.nexus.targ_mapped_lun = lun->lun;
4853 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE;
4854 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4861 ctl_lun_ejected(struct ctl_be_lun *be_lun)
4863 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4865 mtx_lock(&lun->lun_lock);
4866 lun->flags |= CTL_LUN_EJECTED;
4867 mtx_unlock(&lun->lun_lock);
4872 ctl_lun_primary(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_PRIMARY_SC;
4878 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4879 mtx_unlock(&lun->lun_lock);
4880 ctl_isc_announce_lun(lun);
4885 ctl_lun_secondary(struct ctl_be_lun *be_lun)
4887 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4889 mtx_lock(&lun->lun_lock);
4890 lun->flags &= ~CTL_LUN_PRIMARY_SC;
4891 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4892 mtx_unlock(&lun->lun_lock);
4893 ctl_isc_announce_lun(lun);
4898 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4900 struct ctl_softc *softc;
4901 struct ctl_lun *lun;
4903 lun = (struct ctl_lun *)be_lun->ctl_lun;
4904 softc = lun->ctl_softc;
4906 mtx_lock(&lun->lun_lock);
4909 * The LUN needs to be disabled before it can be marked invalid.
4911 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4912 mtx_unlock(&lun->lun_lock);
4916 * Mark the LUN invalid.
4918 lun->flags |= CTL_LUN_INVALID;
4921 * If there is nothing in the OOA queue, go ahead and free the LUN.
4922 * If we have something in the OOA queue, we'll free it when the
4923 * last I/O completes.
4925 if (TAILQ_EMPTY(&lun->ooa_queue)) {
4926 mtx_unlock(&lun->lun_lock);
4927 mtx_lock(&softc->ctl_lock);
4929 mtx_unlock(&softc->ctl_lock);
4931 mtx_unlock(&lun->lun_lock);
4937 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
4939 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4940 union ctl_ha_msg msg;
4942 mtx_lock(&lun->lun_lock);
4943 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE);
4944 mtx_unlock(&lun->lun_lock);
4945 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4946 /* Send msg to other side. */
4947 bzero(&msg.ua, sizeof(msg.ua));
4948 msg.hdr.msg_type = CTL_MSG_UA;
4949 msg.hdr.nexus.initid = -1;
4950 msg.hdr.nexus.targ_port = -1;
4951 msg.hdr.nexus.targ_lun = lun->lun;
4952 msg.hdr.nexus.targ_mapped_lun = lun->lun;
4955 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE;
4956 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4962 * Backend "memory move is complete" callback for requests that never
4963 * make it down to say RAIDCore's configuration code.
4966 ctl_config_move_done(union ctl_io *io)
4970 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
4971 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
4972 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
4974 if ((io->io_hdr.port_status != 0) &&
4975 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
4976 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
4978 * For hardware error sense keys, the sense key
4979 * specific value is defined to be a retry count,
4980 * but we use it to pass back an internal FETD
4981 * error code. XXX KDM Hopefully the FETD is only
4982 * using 16 bits for an error code, since that's
4983 * all the space we have in the sks field.
4985 ctl_set_internal_failure(&io->scsiio,
4988 io->io_hdr.port_status);
4991 if (ctl_debug & CTL_DEBUG_CDB_DATA)
4993 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
4994 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
4995 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
4996 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
4998 * XXX KDM just assuming a single pointer here, and not a
4999 * S/G list. If we start using S/G lists for config data,
5000 * we'll need to know how to clean them up here as well.
5002 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5003 free(io->scsiio.kern_data_ptr, M_CTL);
5005 retval = CTL_RETVAL_COMPLETE;
5008 * XXX KDM now we need to continue data movement. Some
5010 * - call ctl_scsiio() again? We don't do this for data
5011 * writes, because for those at least we know ahead of
5012 * time where the write will go and how long it is. For
5013 * config writes, though, that information is largely
5014 * contained within the write itself, thus we need to
5015 * parse out the data again.
5017 * - Call some other function once the data is in?
5021 * XXX KDM call ctl_scsiio() again for now, and check flag
5022 * bits to see whether we're allocated or not.
5024 retval = ctl_scsiio(&io->scsiio);
5030 * This gets called by a backend driver when it is done with a
5031 * data_submit method.
5034 ctl_data_submit_done(union ctl_io *io)
5037 * If the IO_CONT flag is set, we need to call the supplied
5038 * function to continue processing the I/O, instead of completing
5041 * If there is an error, though, we don't want to keep processing.
5042 * Instead, just send status back to the initiator.
5044 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5045 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5046 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5047 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5048 io->scsiio.io_cont(io);
5055 * This gets called by a backend driver when it is done with a
5056 * configuration write.
5059 ctl_config_write_done(union ctl_io *io)
5064 * If the IO_CONT flag is set, we need to call the supplied
5065 * function to continue processing the I/O, instead of completing
5068 * If there is an error, though, we don't want to keep processing.
5069 * Instead, just send status back to the initiator.
5071 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5072 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5073 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5074 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5075 io->scsiio.io_cont(io);
5079 * Since a configuration write can be done for commands that actually
5080 * have data allocated, like write buffer, and commands that have
5081 * no data, like start/stop unit, we need to check here.
5083 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5084 buf = io->scsiio.kern_data_ptr;
5093 ctl_config_read_done(union ctl_io *io)
5098 * If there is some error -- we are done, skip data transfer.
5100 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
5101 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5102 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
5103 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5104 buf = io->scsiio.kern_data_ptr;
5114 * If the IO_CONT flag is set, we need to call the supplied
5115 * function to continue processing the I/O, instead of completing
5118 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
5119 io->scsiio.io_cont(io);
5127 * SCSI release command.
5130 ctl_scsi_release(struct ctl_scsiio *ctsio)
5132 struct ctl_lun *lun;
5135 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5137 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5138 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5141 * XXX KDM right now, we only support LUN reservation. We don't
5142 * support 3rd party reservations, or extent reservations, which
5143 * might actually need the parameter list. If we've gotten this
5144 * far, we've got a LUN reservation. Anything else got kicked out
5145 * above. So, according to SPC, ignore the length.
5148 mtx_lock(&lun->lun_lock);
5151 * According to SPC, it is not an error for an intiator to attempt
5152 * to release a reservation on a LUN that isn't reserved, or that
5153 * is reserved by another initiator. The reservation can only be
5154 * released, though, by the initiator who made it or by one of
5155 * several reset type events.
5157 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5158 lun->flags &= ~CTL_LUN_RESERVED;
5160 mtx_unlock(&lun->lun_lock);
5162 ctl_set_success(ctsio);
5163 ctl_done((union ctl_io *)ctsio);
5164 return (CTL_RETVAL_COMPLETE);
5168 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5170 struct ctl_lun *lun;
5173 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5175 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5176 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5179 * XXX KDM right now, we only support LUN reservation. We don't
5180 * support 3rd party reservations, or extent reservations, which
5181 * might actually need the parameter list. If we've gotten this
5182 * far, we've got a LUN reservation. Anything else got kicked out
5183 * above. So, according to SPC, ignore the length.
5186 mtx_lock(&lun->lun_lock);
5187 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5188 ctl_set_reservation_conflict(ctsio);
5192 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */
5193 if (lun->flags & CTL_LUN_PR_RESERVED) {
5194 ctl_set_success(ctsio);
5198 lun->flags |= CTL_LUN_RESERVED;
5199 lun->res_idx = residx;
5200 ctl_set_success(ctsio);
5203 mtx_unlock(&lun->lun_lock);
5204 ctl_done((union ctl_io *)ctsio);
5205 return (CTL_RETVAL_COMPLETE);
5209 ctl_start_stop(struct ctl_scsiio *ctsio)
5211 struct scsi_start_stop_unit *cdb;
5212 struct ctl_lun *lun;
5215 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5217 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5218 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5220 if ((cdb->how & SSS_PC_MASK) == 0) {
5221 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
5222 (cdb->how & SSS_START) == 0) {
5225 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5226 if (ctl_get_prkey(lun, residx) == 0 ||
5227 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) {
5229 ctl_set_reservation_conflict(ctsio);
5230 ctl_done((union ctl_io *)ctsio);
5231 return (CTL_RETVAL_COMPLETE);
5235 if ((cdb->how & SSS_LOEJ) &&
5236 (lun->flags & CTL_LUN_REMOVABLE) == 0) {
5237 ctl_set_invalid_field(ctsio,
5243 ctl_done((union ctl_io *)ctsio);
5244 return (CTL_RETVAL_COMPLETE);
5247 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) &&
5248 lun->prevent_count > 0) {
5249 /* "Medium removal prevented" */
5250 ctl_set_sense(ctsio, /*current_error*/ 1,
5251 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ?
5252 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST,
5253 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE);
5254 ctl_done((union ctl_io *)ctsio);
5255 return (CTL_RETVAL_COMPLETE);
5259 retval = lun->backend->config_write((union ctl_io *)ctsio);
5264 ctl_prevent_allow(struct ctl_scsiio *ctsio)
5266 struct ctl_lun *lun;
5267 struct scsi_prevent *cdb;
5271 CTL_DEBUG_PRINT(("ctl_prevent_allow\n"));
5273 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5274 cdb = (struct scsi_prevent *)ctsio->cdb;
5276 if ((lun->flags & CTL_LUN_REMOVABLE) == 0) {
5277 ctl_set_invalid_opcode(ctsio);
5278 ctl_done((union ctl_io *)ctsio);
5279 return (CTL_RETVAL_COMPLETE);
5282 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5283 mtx_lock(&lun->lun_lock);
5284 if ((cdb->how & PR_PREVENT) &&
5285 ctl_is_set(lun->prevent, initidx) == 0) {
5286 ctl_set_mask(lun->prevent, initidx);
5287 lun->prevent_count++;
5288 } else if ((cdb->how & PR_PREVENT) == 0 &&
5289 ctl_is_set(lun->prevent, initidx)) {
5290 ctl_clear_mask(lun->prevent, initidx);
5291 lun->prevent_count--;
5293 mtx_unlock(&lun->lun_lock);
5294 retval = lun->backend->config_write((union ctl_io *)ctsio);
5299 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5300 * we don't really do anything with the LBA and length fields if the user
5301 * passes them in. Instead we'll just flush out the cache for the entire
5305 ctl_sync_cache(struct ctl_scsiio *ctsio)
5307 struct ctl_lun *lun;
5308 struct ctl_softc *softc;
5309 struct ctl_lba_len_flags *lbalen;
5310 uint64_t starting_lba;
5311 uint32_t block_count;
5315 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5317 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5318 softc = lun->ctl_softc;
5321 switch (ctsio->cdb[0]) {
5322 case SYNCHRONIZE_CACHE: {
5323 struct scsi_sync_cache *cdb;
5324 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5326 starting_lba = scsi_4btoul(cdb->begin_lba);
5327 block_count = scsi_2btoul(cdb->lb_count);
5331 case SYNCHRONIZE_CACHE_16: {
5332 struct scsi_sync_cache_16 *cdb;
5333 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5335 starting_lba = scsi_8btou64(cdb->begin_lba);
5336 block_count = scsi_4btoul(cdb->lb_count);
5341 ctl_set_invalid_opcode(ctsio);
5342 ctl_done((union ctl_io *)ctsio);
5344 break; /* NOTREACHED */
5348 * We check the LBA and length, but don't do anything with them.
5349 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5350 * get flushed. This check will just help satisfy anyone who wants
5351 * to see an error for an out of range LBA.
5353 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5354 ctl_set_lba_out_of_range(ctsio);
5355 ctl_done((union ctl_io *)ctsio);
5359 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5360 lbalen->lba = starting_lba;
5361 lbalen->len = block_count;
5362 lbalen->flags = byte2;
5363 retval = lun->backend->config_write((union ctl_io *)ctsio);
5370 ctl_format(struct ctl_scsiio *ctsio)
5372 struct scsi_format *cdb;
5373 struct ctl_lun *lun;
5374 int length, defect_list_len;
5376 CTL_DEBUG_PRINT(("ctl_format\n"));
5378 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5380 cdb = (struct scsi_format *)ctsio->cdb;
5383 if (cdb->byte2 & SF_FMTDATA) {
5384 if (cdb->byte2 & SF_LONGLIST)
5385 length = sizeof(struct scsi_format_header_long);
5387 length = sizeof(struct scsi_format_header_short);
5390 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5392 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5393 ctsio->kern_data_len = length;
5394 ctsio->kern_total_len = length;
5395 ctsio->kern_data_resid = 0;
5396 ctsio->kern_rel_offset = 0;
5397 ctsio->kern_sg_entries = 0;
5398 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5399 ctsio->be_move_done = ctl_config_move_done;
5400 ctl_datamove((union ctl_io *)ctsio);
5402 return (CTL_RETVAL_COMPLETE);
5405 defect_list_len = 0;
5407 if (cdb->byte2 & SF_FMTDATA) {
5408 if (cdb->byte2 & SF_LONGLIST) {
5409 struct scsi_format_header_long *header;
5411 header = (struct scsi_format_header_long *)
5412 ctsio->kern_data_ptr;
5414 defect_list_len = scsi_4btoul(header->defect_list_len);
5415 if (defect_list_len != 0) {
5416 ctl_set_invalid_field(ctsio,
5425 struct scsi_format_header_short *header;
5427 header = (struct scsi_format_header_short *)
5428 ctsio->kern_data_ptr;
5430 defect_list_len = scsi_2btoul(header->defect_list_len);
5431 if (defect_list_len != 0) {
5432 ctl_set_invalid_field(ctsio,
5443 ctl_set_success(ctsio);
5446 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5447 free(ctsio->kern_data_ptr, M_CTL);
5448 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5451 ctl_done((union ctl_io *)ctsio);
5452 return (CTL_RETVAL_COMPLETE);
5456 ctl_read_buffer(struct ctl_scsiio *ctsio)
5458 struct ctl_lun *lun;
5459 uint64_t buffer_offset;
5462 static uint8_t descr[4];
5463 static uint8_t echo_descr[4] = { 0 };
5465 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5466 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5467 switch (ctsio->cdb[0]) {
5469 struct scsi_read_buffer *cdb;
5471 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5472 buffer_offset = scsi_3btoul(cdb->offset);
5473 len = scsi_3btoul(cdb->length);
5477 case READ_BUFFER_16: {
5478 struct scsi_read_buffer_16 *cdb;
5480 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb;
5481 buffer_offset = scsi_8btou64(cdb->offset);
5482 len = scsi_4btoul(cdb->length);
5486 default: /* This shouldn't happen. */
5487 ctl_set_invalid_opcode(ctsio);
5488 ctl_done((union ctl_io *)ctsio);
5489 return (CTL_RETVAL_COMPLETE);
5492 if ((byte2 & RWB_MODE) != RWB_MODE_DATA &&
5493 (byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR &&
5494 (byte2 & RWB_MODE) != RWB_MODE_DESCR) {
5495 ctl_set_invalid_field(ctsio,
5501 ctl_done((union ctl_io *)ctsio);
5502 return (CTL_RETVAL_COMPLETE);
5505 if (buffer_offset > CTL_WRITE_BUFFER_SIZE ||
5506 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5507 ctl_set_invalid_field(ctsio,
5513 ctl_done((union ctl_io *)ctsio);
5514 return (CTL_RETVAL_COMPLETE);
5517 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5519 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5520 ctsio->kern_data_ptr = descr;
5521 len = min(len, sizeof(descr));
5522 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5523 ctsio->kern_data_ptr = echo_descr;
5524 len = min(len, sizeof(echo_descr));
5526 if (lun->write_buffer == NULL) {
5527 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5530 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5532 ctsio->kern_data_len = len;
5533 ctsio->kern_total_len = len;
5534 ctsio->kern_data_resid = 0;
5535 ctsio->kern_rel_offset = 0;
5536 ctsio->kern_sg_entries = 0;
5537 ctl_set_success(ctsio);
5538 ctsio->be_move_done = ctl_config_move_done;
5539 ctl_datamove((union ctl_io *)ctsio);
5540 return (CTL_RETVAL_COMPLETE);
5544 ctl_write_buffer(struct ctl_scsiio *ctsio)
5546 struct scsi_write_buffer *cdb;
5547 struct ctl_lun *lun;
5548 int buffer_offset, len;
5550 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5552 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5553 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5555 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5556 ctl_set_invalid_field(ctsio,
5562 ctl_done((union ctl_io *)ctsio);
5563 return (CTL_RETVAL_COMPLETE);
5566 len = scsi_3btoul(cdb->length);
5567 buffer_offset = scsi_3btoul(cdb->offset);
5569 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5570 ctl_set_invalid_field(ctsio,
5576 ctl_done((union ctl_io *)ctsio);
5577 return (CTL_RETVAL_COMPLETE);
5581 * If we've got a kernel request that hasn't been malloced yet,
5582 * malloc it and tell the caller the data buffer is here.
5584 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5585 if (lun->write_buffer == NULL) {
5586 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5589 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5590 ctsio->kern_data_len = len;
5591 ctsio->kern_total_len = len;
5592 ctsio->kern_data_resid = 0;
5593 ctsio->kern_rel_offset = 0;
5594 ctsio->kern_sg_entries = 0;
5595 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5596 ctsio->be_move_done = ctl_config_move_done;
5597 ctl_datamove((union ctl_io *)ctsio);
5599 return (CTL_RETVAL_COMPLETE);
5602 ctl_set_success(ctsio);
5603 ctl_done((union ctl_io *)ctsio);
5604 return (CTL_RETVAL_COMPLETE);
5608 ctl_write_same(struct ctl_scsiio *ctsio)
5610 struct ctl_lun *lun;
5611 struct ctl_lba_len_flags *lbalen;
5613 uint32_t num_blocks;
5617 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5619 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5621 switch (ctsio->cdb[0]) {
5622 case WRITE_SAME_10: {
5623 struct scsi_write_same_10 *cdb;
5625 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5627 lba = scsi_4btoul(cdb->addr);
5628 num_blocks = scsi_2btoul(cdb->length);
5632 case WRITE_SAME_16: {
5633 struct scsi_write_same_16 *cdb;
5635 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5637 lba = scsi_8btou64(cdb->addr);
5638 num_blocks = scsi_4btoul(cdb->length);
5644 * We got a command we don't support. This shouldn't
5645 * happen, commands should be filtered out above us.
5647 ctl_set_invalid_opcode(ctsio);
5648 ctl_done((union ctl_io *)ctsio);
5650 return (CTL_RETVAL_COMPLETE);
5651 break; /* NOTREACHED */
5654 /* ANCHOR flag can be used only together with UNMAP */
5655 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) {
5656 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5657 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5658 ctl_done((union ctl_io *)ctsio);
5659 return (CTL_RETVAL_COMPLETE);
5663 * The first check is to make sure we're in bounds, the second
5664 * check is to catch wrap-around problems. If the lba + num blocks
5665 * is less than the lba, then we've wrapped around and the block
5666 * range is invalid anyway.
5668 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5669 || ((lba + num_blocks) < lba)) {
5670 ctl_set_lba_out_of_range(ctsio);
5671 ctl_done((union ctl_io *)ctsio);
5672 return (CTL_RETVAL_COMPLETE);
5675 /* Zero number of blocks means "to the last logical block" */
5676 if (num_blocks == 0) {
5677 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5678 ctl_set_invalid_field(ctsio,
5684 ctl_done((union ctl_io *)ctsio);
5685 return (CTL_RETVAL_COMPLETE);
5687 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5690 len = lun->be_lun->blocksize;
5693 * If we've got a kernel request that hasn't been malloced yet,
5694 * malloc it and tell the caller the data buffer is here.
5696 if ((byte2 & SWS_NDOB) == 0 &&
5697 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5698 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5699 ctsio->kern_data_len = len;
5700 ctsio->kern_total_len = len;
5701 ctsio->kern_data_resid = 0;
5702 ctsio->kern_rel_offset = 0;
5703 ctsio->kern_sg_entries = 0;
5704 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5705 ctsio->be_move_done = ctl_config_move_done;
5706 ctl_datamove((union ctl_io *)ctsio);
5708 return (CTL_RETVAL_COMPLETE);
5711 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5713 lbalen->len = num_blocks;
5714 lbalen->flags = byte2;
5715 retval = lun->backend->config_write((union ctl_io *)ctsio);
5721 ctl_unmap(struct ctl_scsiio *ctsio)
5723 struct ctl_lun *lun;
5724 struct scsi_unmap *cdb;
5725 struct ctl_ptr_len_flags *ptrlen;
5726 struct scsi_unmap_header *hdr;
5727 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5729 uint32_t num_blocks;
5733 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5735 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5736 cdb = (struct scsi_unmap *)ctsio->cdb;
5738 len = scsi_2btoul(cdb->length);
5742 * If we've got a kernel request that hasn't been malloced yet,
5743 * malloc it and tell the caller the data buffer is here.
5745 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5746 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5747 ctsio->kern_data_len = len;
5748 ctsio->kern_total_len = len;
5749 ctsio->kern_data_resid = 0;
5750 ctsio->kern_rel_offset = 0;
5751 ctsio->kern_sg_entries = 0;
5752 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5753 ctsio->be_move_done = ctl_config_move_done;
5754 ctl_datamove((union ctl_io *)ctsio);
5756 return (CTL_RETVAL_COMPLETE);
5759 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5760 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5761 if (len < sizeof (*hdr) ||
5762 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5763 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5764 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5765 ctl_set_invalid_field(ctsio,
5773 len = scsi_2btoul(hdr->desc_length);
5774 buf = (struct scsi_unmap_desc *)(hdr + 1);
5775 end = buf + len / sizeof(*buf);
5778 for (range = buf; range < end; range++) {
5779 lba = scsi_8btou64(range->lba);
5780 num_blocks = scsi_4btoul(range->length);
5781 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5782 || ((lba + num_blocks) < lba)) {
5783 ctl_set_lba_out_of_range(ctsio);
5784 ctl_done((union ctl_io *)ctsio);
5785 return (CTL_RETVAL_COMPLETE);
5787 if (num_blocks != 0)
5792 * Block backend can not handle zero last range.
5793 * Filter it out and return if there is nothing left.
5795 len = (uint8_t *)endnz - (uint8_t *)buf;
5797 ctl_set_success(ctsio);
5801 mtx_lock(&lun->lun_lock);
5802 ptrlen = (struct ctl_ptr_len_flags *)
5803 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5804 ptrlen->ptr = (void *)buf;
5806 ptrlen->flags = byte2;
5807 ctl_check_blocked(lun);
5808 mtx_unlock(&lun->lun_lock);
5810 retval = lun->backend->config_write((union ctl_io *)ctsio);
5814 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5815 free(ctsio->kern_data_ptr, M_CTL);
5816 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5818 ctl_done((union ctl_io *)ctsio);
5819 return (CTL_RETVAL_COMPLETE);
5823 * Note that this function currently doesn't actually do anything inside
5824 * CTL to enforce things if the DQue bit is turned on.
5826 * Also note that this function can't be used in the default case, because
5827 * the DQue bit isn't set in the changeable mask for the control mode page
5828 * anyway. This is just here as an example for how to implement a page
5829 * handler, and a placeholder in case we want to allow the user to turn
5830 * tagged queueing on and off.
5832 * The D_SENSE bit handling is functional, however, and will turn
5833 * descriptor sense on and off for a given LUN.
5836 ctl_control_page_handler(struct ctl_scsiio *ctsio,
5837 struct ctl_page_index *page_index, uint8_t *page_ptr)
5839 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
5840 struct ctl_lun *lun;
5844 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5845 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5848 user_cp = (struct scsi_control_page *)page_ptr;
5849 current_cp = (struct scsi_control_page *)
5850 (page_index->page_data + (page_index->page_len *
5852 saved_cp = (struct scsi_control_page *)
5853 (page_index->page_data + (page_index->page_len *
5856 mtx_lock(&lun->lun_lock);
5857 if (((current_cp->rlec & SCP_DSENSE) == 0)
5858 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
5860 * Descriptor sense is currently turned off and the user
5861 * wants to turn it on.
5863 current_cp->rlec |= SCP_DSENSE;
5864 saved_cp->rlec |= SCP_DSENSE;
5865 lun->flags |= CTL_LUN_SENSE_DESC;
5867 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
5868 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
5870 * Descriptor sense is currently turned on, and the user
5871 * wants to turn it off.
5873 current_cp->rlec &= ~SCP_DSENSE;
5874 saved_cp->rlec &= ~SCP_DSENSE;
5875 lun->flags &= ~CTL_LUN_SENSE_DESC;
5878 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) !=
5879 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) {
5880 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
5881 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
5882 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
5883 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
5886 if ((current_cp->eca_and_aen & SCP_SWP) !=
5887 (user_cp->eca_and_aen & SCP_SWP)) {
5888 current_cp->eca_and_aen &= ~SCP_SWP;
5889 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
5890 saved_cp->eca_and_aen &= ~SCP_SWP;
5891 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
5895 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5896 mtx_unlock(&lun->lun_lock);
5898 ctl_isc_announce_mode(lun,
5899 ctl_get_initindex(&ctsio->io_hdr.nexus),
5900 page_index->page_code, page_index->subpage);
5906 ctl_caching_sp_handler(struct ctl_scsiio *ctsio,
5907 struct ctl_page_index *page_index, uint8_t *page_ptr)
5909 struct scsi_caching_page *current_cp, *saved_cp, *user_cp;
5910 struct ctl_lun *lun;
5914 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5915 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5918 user_cp = (struct scsi_caching_page *)page_ptr;
5919 current_cp = (struct scsi_caching_page *)
5920 (page_index->page_data + (page_index->page_len *
5922 saved_cp = (struct scsi_caching_page *)
5923 (page_index->page_data + (page_index->page_len *
5926 mtx_lock(&lun->lun_lock);
5927 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) !=
5928 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) {
5929 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
5930 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
5931 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
5932 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
5936 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5937 mtx_unlock(&lun->lun_lock);
5939 ctl_isc_announce_mode(lun,
5940 ctl_get_initindex(&ctsio->io_hdr.nexus),
5941 page_index->page_code, page_index->subpage);
5947 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
5948 struct ctl_page_index *page_index,
5954 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
5959 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
5960 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
5961 printf("page data:");
5963 printf(" %.2x",page_ptr[i]);
5969 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
5970 struct ctl_page_index *page_index,
5973 struct copan_debugconf_subpage *page;
5975 page = (struct copan_debugconf_subpage *)page_index->page_data +
5976 (page_index->page_len * pc);
5979 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
5980 case SMS_PAGE_CTRL_DEFAULT >> 6:
5981 case SMS_PAGE_CTRL_SAVED >> 6:
5983 * We don't update the changeable or default bits for this page.
5986 case SMS_PAGE_CTRL_CURRENT >> 6:
5987 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
5988 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
5998 ctl_do_mode_select(union ctl_io *io)
6000 struct scsi_mode_page_header *page_header;
6001 struct ctl_page_index *page_index;
6002 struct ctl_scsiio *ctsio;
6003 int page_len, page_len_offset, page_len_size;
6004 union ctl_modepage_info *modepage_info;
6005 struct ctl_lun *lun;
6006 int *len_left, *len_used;
6009 ctsio = &io->scsiio;
6012 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6014 modepage_info = (union ctl_modepage_info *)
6015 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6016 len_left = &modepage_info->header.len_left;
6017 len_used = &modepage_info->header.len_used;
6021 page_header = (struct scsi_mode_page_header *)
6022 (ctsio->kern_data_ptr + *len_used);
6024 if (*len_left == 0) {
6025 free(ctsio->kern_data_ptr, M_CTL);
6026 ctl_set_success(ctsio);
6027 ctl_done((union ctl_io *)ctsio);
6028 return (CTL_RETVAL_COMPLETE);
6029 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6031 free(ctsio->kern_data_ptr, M_CTL);
6032 ctl_set_param_len_error(ctsio);
6033 ctl_done((union ctl_io *)ctsio);
6034 return (CTL_RETVAL_COMPLETE);
6036 } else if ((page_header->page_code & SMPH_SPF)
6037 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6039 free(ctsio->kern_data_ptr, M_CTL);
6040 ctl_set_param_len_error(ctsio);
6041 ctl_done((union ctl_io *)ctsio);
6042 return (CTL_RETVAL_COMPLETE);
6047 * XXX KDM should we do something with the block descriptor?
6049 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6050 page_index = &lun->mode_pages.index[i];
6051 if (lun->be_lun->lun_type == T_DIRECT &&
6052 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6054 if (lun->be_lun->lun_type == T_PROCESSOR &&
6055 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6057 if (lun->be_lun->lun_type == T_CDROM &&
6058 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6061 if ((page_index->page_code & SMPH_PC_MASK) !=
6062 (page_header->page_code & SMPH_PC_MASK))
6066 * If neither page has a subpage code, then we've got a
6069 if (((page_index->page_code & SMPH_SPF) == 0)
6070 && ((page_header->page_code & SMPH_SPF) == 0)) {
6071 page_len = page_header->page_length;
6076 * If both pages have subpages, then the subpage numbers
6079 if ((page_index->page_code & SMPH_SPF)
6080 && (page_header->page_code & SMPH_SPF)) {
6081 struct scsi_mode_page_header_sp *sph;
6083 sph = (struct scsi_mode_page_header_sp *)page_header;
6084 if (page_index->subpage == sph->subpage) {
6085 page_len = scsi_2btoul(sph->page_length);
6092 * If we couldn't find the page, or if we don't have a mode select
6093 * handler for it, send back an error to the user.
6095 if ((i >= CTL_NUM_MODE_PAGES)
6096 || (page_index->select_handler == NULL)) {
6097 ctl_set_invalid_field(ctsio,
6100 /*field*/ *len_used,
6103 free(ctsio->kern_data_ptr, M_CTL);
6104 ctl_done((union ctl_io *)ctsio);
6105 return (CTL_RETVAL_COMPLETE);
6108 if (page_index->page_code & SMPH_SPF) {
6109 page_len_offset = 2;
6113 page_len_offset = 1;
6117 * If the length the initiator gives us isn't the one we specify in
6118 * the mode page header, or if they didn't specify enough data in
6119 * the CDB to avoid truncating this page, kick out the request.
6121 if ((page_len != (page_index->page_len - page_len_offset -
6123 || (*len_left < page_index->page_len)) {
6126 ctl_set_invalid_field(ctsio,
6129 /*field*/ *len_used + page_len_offset,
6132 free(ctsio->kern_data_ptr, M_CTL);
6133 ctl_done((union ctl_io *)ctsio);
6134 return (CTL_RETVAL_COMPLETE);
6138 * Run through the mode page, checking to make sure that the bits
6139 * the user changed are actually legal for him to change.
6141 for (i = 0; i < page_index->page_len; i++) {
6142 uint8_t *user_byte, *change_mask, *current_byte;
6146 user_byte = (uint8_t *)page_header + i;
6147 change_mask = page_index->page_data +
6148 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6149 current_byte = page_index->page_data +
6150 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6153 * Check to see whether the user set any bits in this byte
6154 * that he is not allowed to set.
6156 if ((*user_byte & ~(*change_mask)) ==
6157 (*current_byte & ~(*change_mask)))
6161 * Go through bit by bit to determine which one is illegal.
6164 for (j = 7; j >= 0; j--) {
6165 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6166 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6171 ctl_set_invalid_field(ctsio,
6174 /*field*/ *len_used + i,
6177 free(ctsio->kern_data_ptr, M_CTL);
6178 ctl_done((union ctl_io *)ctsio);
6179 return (CTL_RETVAL_COMPLETE);
6183 * Decrement these before we call the page handler, since we may
6184 * end up getting called back one way or another before the handler
6185 * returns to this context.
6187 *len_left -= page_index->page_len;
6188 *len_used += page_index->page_len;
6190 retval = page_index->select_handler(ctsio, page_index,
6191 (uint8_t *)page_header);
6194 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6195 * wait until this queued command completes to finish processing
6196 * the mode page. If it returns anything other than
6197 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6198 * already set the sense information, freed the data pointer, and
6199 * completed the io for us.
6201 if (retval != CTL_RETVAL_COMPLETE)
6202 goto bailout_no_done;
6205 * If the initiator sent us more than one page, parse the next one.
6210 ctl_set_success(ctsio);
6211 free(ctsio->kern_data_ptr, M_CTL);
6212 ctl_done((union ctl_io *)ctsio);
6216 return (CTL_RETVAL_COMPLETE);
6221 ctl_mode_select(struct ctl_scsiio *ctsio)
6223 int param_len, pf, sp;
6224 int header_size, bd_len;
6225 union ctl_modepage_info *modepage_info;
6227 switch (ctsio->cdb[0]) {
6228 case MODE_SELECT_6: {
6229 struct scsi_mode_select_6 *cdb;
6231 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6233 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6234 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6235 param_len = cdb->length;
6236 header_size = sizeof(struct scsi_mode_header_6);
6239 case MODE_SELECT_10: {
6240 struct scsi_mode_select_10 *cdb;
6242 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6244 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6245 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6246 param_len = scsi_2btoul(cdb->length);
6247 header_size = sizeof(struct scsi_mode_header_10);
6251 ctl_set_invalid_opcode(ctsio);
6252 ctl_done((union ctl_io *)ctsio);
6253 return (CTL_RETVAL_COMPLETE);
6258 * "A parameter list length of zero indicates that the Data-Out Buffer
6259 * shall be empty. This condition shall not be considered as an error."
6261 if (param_len == 0) {
6262 ctl_set_success(ctsio);
6263 ctl_done((union ctl_io *)ctsio);
6264 return (CTL_RETVAL_COMPLETE);
6268 * Since we'll hit this the first time through, prior to
6269 * allocation, we don't need to free a data buffer here.
6271 if (param_len < header_size) {
6272 ctl_set_param_len_error(ctsio);
6273 ctl_done((union ctl_io *)ctsio);
6274 return (CTL_RETVAL_COMPLETE);
6278 * Allocate the data buffer and grab the user's data. In theory,
6279 * we shouldn't have to sanity check the parameter list length here
6280 * because the maximum size is 64K. We should be able to malloc
6281 * that much without too many problems.
6283 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6284 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6285 ctsio->kern_data_len = param_len;
6286 ctsio->kern_total_len = param_len;
6287 ctsio->kern_data_resid = 0;
6288 ctsio->kern_rel_offset = 0;
6289 ctsio->kern_sg_entries = 0;
6290 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6291 ctsio->be_move_done = ctl_config_move_done;
6292 ctl_datamove((union ctl_io *)ctsio);
6294 return (CTL_RETVAL_COMPLETE);
6297 switch (ctsio->cdb[0]) {
6298 case MODE_SELECT_6: {
6299 struct scsi_mode_header_6 *mh6;
6301 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6302 bd_len = mh6->blk_desc_len;
6305 case MODE_SELECT_10: {
6306 struct scsi_mode_header_10 *mh10;
6308 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6309 bd_len = scsi_2btoul(mh10->blk_desc_len);
6313 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6316 if (param_len < (header_size + bd_len)) {
6317 free(ctsio->kern_data_ptr, M_CTL);
6318 ctl_set_param_len_error(ctsio);
6319 ctl_done((union ctl_io *)ctsio);
6320 return (CTL_RETVAL_COMPLETE);
6324 * Set the IO_CONT flag, so that if this I/O gets passed to
6325 * ctl_config_write_done(), it'll get passed back to
6326 * ctl_do_mode_select() for further processing, or completion if
6329 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6330 ctsio->io_cont = ctl_do_mode_select;
6332 modepage_info = (union ctl_modepage_info *)
6333 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6334 memset(modepage_info, 0, sizeof(*modepage_info));
6335 modepage_info->header.len_left = param_len - header_size - bd_len;
6336 modepage_info->header.len_used = header_size + bd_len;
6338 return (ctl_do_mode_select((union ctl_io *)ctsio));
6342 ctl_mode_sense(struct ctl_scsiio *ctsio)
6344 struct ctl_lun *lun;
6345 int pc, page_code, dbd, llba, subpage;
6346 int alloc_len, page_len, header_len, total_len;
6347 struct scsi_mode_block_descr *block_desc;
6348 struct ctl_page_index *page_index;
6354 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6356 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6357 switch (ctsio->cdb[0]) {
6358 case MODE_SENSE_6: {
6359 struct scsi_mode_sense_6 *cdb;
6361 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6363 header_len = sizeof(struct scsi_mode_hdr_6);
6364 if (cdb->byte2 & SMS_DBD)
6367 header_len += sizeof(struct scsi_mode_block_descr);
6369 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6370 page_code = cdb->page & SMS_PAGE_CODE;
6371 subpage = cdb->subpage;
6372 alloc_len = cdb->length;
6375 case MODE_SENSE_10: {
6376 struct scsi_mode_sense_10 *cdb;
6378 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6380 header_len = sizeof(struct scsi_mode_hdr_10);
6382 if (cdb->byte2 & SMS_DBD)
6385 header_len += sizeof(struct scsi_mode_block_descr);
6386 if (cdb->byte2 & SMS10_LLBAA)
6388 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6389 page_code = cdb->page & SMS_PAGE_CODE;
6390 subpage = cdb->subpage;
6391 alloc_len = scsi_2btoul(cdb->length);
6395 ctl_set_invalid_opcode(ctsio);
6396 ctl_done((union ctl_io *)ctsio);
6397 return (CTL_RETVAL_COMPLETE);
6398 break; /* NOTREACHED */
6402 * We have to make a first pass through to calculate the size of
6403 * the pages that match the user's query. Then we allocate enough
6404 * memory to hold it, and actually copy the data into the buffer.
6406 switch (page_code) {
6407 case SMS_ALL_PAGES_PAGE: {
6413 * At the moment, values other than 0 and 0xff here are
6414 * reserved according to SPC-3.
6416 if ((subpage != SMS_SUBPAGE_PAGE_0)
6417 && (subpage != SMS_SUBPAGE_ALL)) {
6418 ctl_set_invalid_field(ctsio,
6424 ctl_done((union ctl_io *)ctsio);
6425 return (CTL_RETVAL_COMPLETE);
6428 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6429 page_index = &lun->mode_pages.index[i];
6431 /* Make sure the page is supported for this dev type */
6432 if (lun->be_lun->lun_type == T_DIRECT &&
6433 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6435 if (lun->be_lun->lun_type == T_PROCESSOR &&
6436 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6438 if (lun->be_lun->lun_type == T_CDROM &&
6439 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6443 * We don't use this subpage if the user didn't
6444 * request all subpages.
6446 if ((page_index->subpage != 0)
6447 && (subpage == SMS_SUBPAGE_PAGE_0))
6451 printf("found page %#x len %d\n",
6452 page_index->page_code & SMPH_PC_MASK,
6453 page_index->page_len);
6455 page_len += page_index->page_len;
6464 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6465 page_index = &lun->mode_pages.index[i];
6467 /* Make sure the page is supported for this dev type */
6468 if (lun->be_lun->lun_type == T_DIRECT &&
6469 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6471 if (lun->be_lun->lun_type == T_PROCESSOR &&
6472 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6474 if (lun->be_lun->lun_type == T_CDROM &&
6475 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6478 /* Look for the right page code */
6479 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6482 /* Look for the right subpage or the subpage wildcard*/
6483 if ((page_index->subpage != subpage)
6484 && (subpage != SMS_SUBPAGE_ALL))
6488 printf("found page %#x len %d\n",
6489 page_index->page_code & SMPH_PC_MASK,
6490 page_index->page_len);
6493 page_len += page_index->page_len;
6496 if (page_len == 0) {
6497 ctl_set_invalid_field(ctsio,
6503 ctl_done((union ctl_io *)ctsio);
6504 return (CTL_RETVAL_COMPLETE);
6510 total_len = header_len + page_len;
6512 printf("header_len = %d, page_len = %d, total_len = %d\n",
6513 header_len, page_len, total_len);
6516 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6517 ctsio->kern_sg_entries = 0;
6518 ctsio->kern_data_resid = 0;
6519 ctsio->kern_rel_offset = 0;
6520 if (total_len < alloc_len) {
6521 ctsio->residual = alloc_len - total_len;
6522 ctsio->kern_data_len = total_len;
6523 ctsio->kern_total_len = total_len;
6525 ctsio->residual = 0;
6526 ctsio->kern_data_len = alloc_len;
6527 ctsio->kern_total_len = alloc_len;
6530 switch (ctsio->cdb[0]) {
6531 case MODE_SENSE_6: {
6532 struct scsi_mode_hdr_6 *header;
6534 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6536 header->datalen = MIN(total_len - 1, 254);
6537 if (lun->be_lun->lun_type == T_DIRECT) {
6538 header->dev_specific = 0x10; /* DPOFUA */
6539 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6540 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6541 .eca_and_aen & SCP_SWP) != 0)
6542 header->dev_specific |= 0x80; /* WP */
6545 header->block_descr_len = 0;
6547 header->block_descr_len =
6548 sizeof(struct scsi_mode_block_descr);
6549 block_desc = (struct scsi_mode_block_descr *)&header[1];
6552 case MODE_SENSE_10: {
6553 struct scsi_mode_hdr_10 *header;
6556 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6558 datalen = MIN(total_len - 2, 65533);
6559 scsi_ulto2b(datalen, header->datalen);
6560 if (lun->be_lun->lun_type == T_DIRECT) {
6561 header->dev_specific = 0x10; /* DPOFUA */
6562 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6563 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6564 .eca_and_aen & SCP_SWP) != 0)
6565 header->dev_specific |= 0x80; /* WP */
6568 scsi_ulto2b(0, header->block_descr_len);
6570 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6571 header->block_descr_len);
6572 block_desc = (struct scsi_mode_block_descr *)&header[1];
6576 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6580 * If we've got a disk, use its blocksize in the block
6581 * descriptor. Otherwise, just set it to 0.
6584 if (lun->be_lun->lun_type == T_DIRECT)
6585 scsi_ulto3b(lun->be_lun->blocksize,
6586 block_desc->block_len);
6588 scsi_ulto3b(0, block_desc->block_len);
6591 switch (page_code) {
6592 case SMS_ALL_PAGES_PAGE: {
6595 data_used = header_len;
6596 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6597 struct ctl_page_index *page_index;
6599 page_index = &lun->mode_pages.index[i];
6600 if (lun->be_lun->lun_type == T_DIRECT &&
6601 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6603 if (lun->be_lun->lun_type == T_PROCESSOR &&
6604 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6606 if (lun->be_lun->lun_type == T_CDROM &&
6607 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6611 * We don't use this subpage if the user didn't
6612 * request all subpages. We already checked (above)
6613 * to make sure the user only specified a subpage
6614 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6616 if ((page_index->subpage != 0)
6617 && (subpage == SMS_SUBPAGE_PAGE_0))
6621 * Call the handler, if it exists, to update the
6622 * page to the latest values.
6624 if (page_index->sense_handler != NULL)
6625 page_index->sense_handler(ctsio, page_index,pc);
6627 memcpy(ctsio->kern_data_ptr + data_used,
6628 page_index->page_data +
6629 (page_index->page_len * pc),
6630 page_index->page_len);
6631 data_used += page_index->page_len;
6638 data_used = header_len;
6640 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6641 struct ctl_page_index *page_index;
6643 page_index = &lun->mode_pages.index[i];
6645 /* Look for the right page code */
6646 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6649 /* Look for the right subpage or the subpage wildcard*/
6650 if ((page_index->subpage != subpage)
6651 && (subpage != SMS_SUBPAGE_ALL))
6654 /* Make sure the page is supported for this dev type */
6655 if (lun->be_lun->lun_type == T_DIRECT &&
6656 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6658 if (lun->be_lun->lun_type == T_PROCESSOR &&
6659 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6661 if (lun->be_lun->lun_type == T_CDROM &&
6662 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6666 * Call the handler, if it exists, to update the
6667 * page to the latest values.
6669 if (page_index->sense_handler != NULL)
6670 page_index->sense_handler(ctsio, page_index,pc);
6672 memcpy(ctsio->kern_data_ptr + data_used,
6673 page_index->page_data +
6674 (page_index->page_len * pc),
6675 page_index->page_len);
6676 data_used += page_index->page_len;
6682 ctl_set_success(ctsio);
6683 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6684 ctsio->be_move_done = ctl_config_move_done;
6685 ctl_datamove((union ctl_io *)ctsio);
6686 return (CTL_RETVAL_COMPLETE);
6690 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6691 struct ctl_page_index *page_index,
6694 struct ctl_lun *lun;
6695 struct scsi_log_param_header *phdr;
6699 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6700 data = page_index->page_data;
6702 if (lun->backend->lun_attr != NULL &&
6703 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6705 phdr = (struct scsi_log_param_header *)data;
6706 scsi_ulto2b(0x0001, phdr->param_code);
6707 phdr->param_control = SLP_LBIN | SLP_LP;
6708 phdr->param_len = 8;
6709 data = (uint8_t *)(phdr + 1);
6710 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6711 data[4] = 0x02; /* per-pool */
6712 data += phdr->param_len;
6715 if (lun->backend->lun_attr != NULL &&
6716 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6718 phdr = (struct scsi_log_param_header *)data;
6719 scsi_ulto2b(0x0002, phdr->param_code);
6720 phdr->param_control = SLP_LBIN | SLP_LP;
6721 phdr->param_len = 8;
6722 data = (uint8_t *)(phdr + 1);
6723 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6724 data[4] = 0x01; /* per-LUN */
6725 data += phdr->param_len;
6728 if (lun->backend->lun_attr != NULL &&
6729 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6731 phdr = (struct scsi_log_param_header *)data;
6732 scsi_ulto2b(0x00f1, phdr->param_code);
6733 phdr->param_control = SLP_LBIN | SLP_LP;
6734 phdr->param_len = 8;
6735 data = (uint8_t *)(phdr + 1);
6736 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6737 data[4] = 0x02; /* per-pool */
6738 data += phdr->param_len;
6741 if (lun->backend->lun_attr != NULL &&
6742 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6744 phdr = (struct scsi_log_param_header *)data;
6745 scsi_ulto2b(0x00f2, phdr->param_code);
6746 phdr->param_control = SLP_LBIN | SLP_LP;
6747 phdr->param_len = 8;
6748 data = (uint8_t *)(phdr + 1);
6749 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6750 data[4] = 0x02; /* per-pool */
6751 data += phdr->param_len;
6754 page_index->page_len = data - page_index->page_data;
6759 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
6760 struct ctl_page_index *page_index,
6763 struct ctl_lun *lun;
6764 struct stat_page *data;
6765 uint64_t rn, wn, rb, wb;
6766 struct bintime rt, wt;
6769 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6770 data = (struct stat_page *)page_index->page_data;
6772 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
6773 data->sap.hdr.param_control = SLP_LBIN;
6774 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
6775 sizeof(struct scsi_log_param_header);
6776 rn = wn = rb = wb = 0;
6779 for (i = 0; i < CTL_MAX_PORTS; i++) {
6780 rn += lun->stats.ports[i].operations[CTL_STATS_READ];
6781 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE];
6782 rb += lun->stats.ports[i].bytes[CTL_STATS_READ];
6783 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE];
6784 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]);
6785 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]);
6787 scsi_u64to8b(rn, data->sap.read_num);
6788 scsi_u64to8b(wn, data->sap.write_num);
6789 if (lun->stats.blocksize > 0) {
6790 scsi_u64to8b(wb / lun->stats.blocksize,
6791 data->sap.recvieved_lba);
6792 scsi_u64to8b(rb / lun->stats.blocksize,
6793 data->sap.transmitted_lba);
6795 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000),
6796 data->sap.read_int);
6797 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000),
6798 data->sap.write_int);
6799 scsi_u64to8b(0, data->sap.weighted_num);
6800 scsi_u64to8b(0, data->sap.weighted_int);
6801 scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
6802 data->it.hdr.param_control = SLP_LBIN;
6803 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
6804 sizeof(struct scsi_log_param_header);
6806 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
6808 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
6809 data->it.hdr.param_control = SLP_LBIN;
6810 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
6811 sizeof(struct scsi_log_param_header);
6812 scsi_ulto4b(3, data->ti.exponent);
6813 scsi_ulto4b(1, data->ti.integer);
6815 page_index->page_len = sizeof(*data);
6820 ctl_log_sense(struct ctl_scsiio *ctsio)
6822 struct ctl_lun *lun;
6823 int i, pc, page_code, subpage;
6824 int alloc_len, total_len;
6825 struct ctl_page_index *page_index;
6826 struct scsi_log_sense *cdb;
6827 struct scsi_log_header *header;
6829 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6831 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6832 cdb = (struct scsi_log_sense *)ctsio->cdb;
6833 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6834 page_code = cdb->page & SLS_PAGE_CODE;
6835 subpage = cdb->subpage;
6836 alloc_len = scsi_2btoul(cdb->length);
6839 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6840 page_index = &lun->log_pages.index[i];
6842 /* Look for the right page code */
6843 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6846 /* Look for the right subpage or the subpage wildcard*/
6847 if (page_index->subpage != subpage)
6852 if (i >= CTL_NUM_LOG_PAGES) {
6853 ctl_set_invalid_field(ctsio,
6859 ctl_done((union ctl_io *)ctsio);
6860 return (CTL_RETVAL_COMPLETE);
6863 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6865 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6866 ctsio->kern_sg_entries = 0;
6867 ctsio->kern_data_resid = 0;
6868 ctsio->kern_rel_offset = 0;
6869 if (total_len < alloc_len) {
6870 ctsio->residual = alloc_len - total_len;
6871 ctsio->kern_data_len = total_len;
6872 ctsio->kern_total_len = total_len;
6874 ctsio->residual = 0;
6875 ctsio->kern_data_len = alloc_len;
6876 ctsio->kern_total_len = alloc_len;
6879 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
6880 header->page = page_index->page_code;
6881 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING)
6882 header->page |= SL_DS;
6883 if (page_index->subpage) {
6884 header->page |= SL_SPF;
6885 header->subpage = page_index->subpage;
6887 scsi_ulto2b(page_index->page_len, header->datalen);
6890 * Call the handler, if it exists, to update the
6891 * page to the latest values.
6893 if (page_index->sense_handler != NULL)
6894 page_index->sense_handler(ctsio, page_index, pc);
6896 memcpy(header + 1, page_index->page_data, page_index->page_len);
6898 ctl_set_success(ctsio);
6899 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6900 ctsio->be_move_done = ctl_config_move_done;
6901 ctl_datamove((union ctl_io *)ctsio);
6902 return (CTL_RETVAL_COMPLETE);
6906 ctl_read_capacity(struct ctl_scsiio *ctsio)
6908 struct scsi_read_capacity *cdb;
6909 struct scsi_read_capacity_data *data;
6910 struct ctl_lun *lun;
6913 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6915 cdb = (struct scsi_read_capacity *)ctsio->cdb;
6917 lba = scsi_4btoul(cdb->addr);
6918 if (((cdb->pmi & SRC_PMI) == 0)
6920 ctl_set_invalid_field(/*ctsio*/ ctsio,
6926 ctl_done((union ctl_io *)ctsio);
6927 return (CTL_RETVAL_COMPLETE);
6930 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6932 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6933 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6934 ctsio->residual = 0;
6935 ctsio->kern_data_len = sizeof(*data);
6936 ctsio->kern_total_len = sizeof(*data);
6937 ctsio->kern_data_resid = 0;
6938 ctsio->kern_rel_offset = 0;
6939 ctsio->kern_sg_entries = 0;
6942 * If the maximum LBA is greater than 0xfffffffe, the user must
6943 * issue a SERVICE ACTION IN (16) command, with the read capacity
6944 * serivce action set.
6946 if (lun->be_lun->maxlba > 0xfffffffe)
6947 scsi_ulto4b(0xffffffff, data->addr);
6949 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6952 * XXX KDM this may not be 512 bytes...
6954 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6956 ctl_set_success(ctsio);
6957 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6958 ctsio->be_move_done = ctl_config_move_done;
6959 ctl_datamove((union ctl_io *)ctsio);
6960 return (CTL_RETVAL_COMPLETE);
6964 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
6966 struct scsi_read_capacity_16 *cdb;
6967 struct scsi_read_capacity_data_long *data;
6968 struct ctl_lun *lun;
6972 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
6974 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
6976 alloc_len = scsi_4btoul(cdb->alloc_len);
6977 lba = scsi_8btou64(cdb->addr);
6979 if ((cdb->reladr & SRC16_PMI)
6981 ctl_set_invalid_field(/*ctsio*/ ctsio,
6987 ctl_done((union ctl_io *)ctsio);
6988 return (CTL_RETVAL_COMPLETE);
6991 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6993 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6994 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
6996 if (sizeof(*data) < alloc_len) {
6997 ctsio->residual = alloc_len - sizeof(*data);
6998 ctsio->kern_data_len = sizeof(*data);
6999 ctsio->kern_total_len = sizeof(*data);
7001 ctsio->residual = 0;
7002 ctsio->kern_data_len = alloc_len;
7003 ctsio->kern_total_len = alloc_len;
7005 ctsio->kern_data_resid = 0;
7006 ctsio->kern_rel_offset = 0;
7007 ctsio->kern_sg_entries = 0;
7009 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7010 /* XXX KDM this may not be 512 bytes... */
7011 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7012 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7013 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7014 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7015 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
7017 ctl_set_success(ctsio);
7018 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7019 ctsio->be_move_done = ctl_config_move_done;
7020 ctl_datamove((union ctl_io *)ctsio);
7021 return (CTL_RETVAL_COMPLETE);
7025 ctl_get_lba_status(struct ctl_scsiio *ctsio)
7027 struct scsi_get_lba_status *cdb;
7028 struct scsi_get_lba_status_data *data;
7029 struct ctl_lun *lun;
7030 struct ctl_lba_len_flags *lbalen;
7032 uint32_t alloc_len, total_len;
7035 CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
7037 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7038 cdb = (struct scsi_get_lba_status *)ctsio->cdb;
7039 lba = scsi_8btou64(cdb->addr);
7040 alloc_len = scsi_4btoul(cdb->alloc_len);
7042 if (lba > lun->be_lun->maxlba) {
7043 ctl_set_lba_out_of_range(ctsio);
7044 ctl_done((union ctl_io *)ctsio);
7045 return (CTL_RETVAL_COMPLETE);
7048 total_len = sizeof(*data) + sizeof(data->descr[0]);
7049 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7050 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
7052 if (total_len < alloc_len) {
7053 ctsio->residual = alloc_len - total_len;
7054 ctsio->kern_data_len = total_len;
7055 ctsio->kern_total_len = total_len;
7057 ctsio->residual = 0;
7058 ctsio->kern_data_len = alloc_len;
7059 ctsio->kern_total_len = alloc_len;
7061 ctsio->kern_data_resid = 0;
7062 ctsio->kern_rel_offset = 0;
7063 ctsio->kern_sg_entries = 0;
7065 /* Fill dummy data in case backend can't tell anything. */
7066 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
7067 scsi_u64to8b(lba, data->descr[0].addr);
7068 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
7069 data->descr[0].length);
7070 data->descr[0].status = 0; /* Mapped or unknown. */
7072 ctl_set_success(ctsio);
7073 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7074 ctsio->be_move_done = ctl_config_move_done;
7076 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
7078 lbalen->len = total_len;
7080 retval = lun->backend->config_read((union ctl_io *)ctsio);
7081 return (CTL_RETVAL_COMPLETE);
7085 ctl_read_defect(struct ctl_scsiio *ctsio)
7087 struct scsi_read_defect_data_10 *ccb10;
7088 struct scsi_read_defect_data_12 *ccb12;
7089 struct scsi_read_defect_data_hdr_10 *data10;
7090 struct scsi_read_defect_data_hdr_12 *data12;
7091 uint32_t alloc_len, data_len;
7094 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7096 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7097 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7098 format = ccb10->format;
7099 alloc_len = scsi_2btoul(ccb10->alloc_length);
7100 data_len = sizeof(*data10);
7102 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7103 format = ccb12->format;
7104 alloc_len = scsi_4btoul(ccb12->alloc_length);
7105 data_len = sizeof(*data12);
7107 if (alloc_len == 0) {
7108 ctl_set_success(ctsio);
7109 ctl_done((union ctl_io *)ctsio);
7110 return (CTL_RETVAL_COMPLETE);
7113 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7114 if (data_len < alloc_len) {
7115 ctsio->residual = alloc_len - data_len;
7116 ctsio->kern_data_len = data_len;
7117 ctsio->kern_total_len = data_len;
7119 ctsio->residual = 0;
7120 ctsio->kern_data_len = alloc_len;
7121 ctsio->kern_total_len = alloc_len;
7123 ctsio->kern_data_resid = 0;
7124 ctsio->kern_rel_offset = 0;
7125 ctsio->kern_sg_entries = 0;
7127 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7128 data10 = (struct scsi_read_defect_data_hdr_10 *)
7129 ctsio->kern_data_ptr;
7130 data10->format = format;
7131 scsi_ulto2b(0, data10->length);
7133 data12 = (struct scsi_read_defect_data_hdr_12 *)
7134 ctsio->kern_data_ptr;
7135 data12->format = format;
7136 scsi_ulto2b(0, data12->generation);
7137 scsi_ulto4b(0, data12->length);
7140 ctl_set_success(ctsio);
7141 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7142 ctsio->be_move_done = ctl_config_move_done;
7143 ctl_datamove((union ctl_io *)ctsio);
7144 return (CTL_RETVAL_COMPLETE);
7148 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7150 struct scsi_maintenance_in *cdb;
7152 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os;
7153 int num_ha_groups, num_target_ports, shared_group;
7154 struct ctl_lun *lun;
7155 struct ctl_softc *softc;
7156 struct ctl_port *port;
7157 struct scsi_target_group_data *rtg_ptr;
7158 struct scsi_target_group_data_extended *rtg_ext_ptr;
7159 struct scsi_target_port_group_descriptor *tpg_desc;
7161 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7163 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7164 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7165 softc = lun->ctl_softc;
7167 retval = CTL_RETVAL_COMPLETE;
7169 switch (cdb->byte2 & STG_PDF_MASK) {
7170 case STG_PDF_LENGTH:
7173 case STG_PDF_EXTENDED:
7177 ctl_set_invalid_field(/*ctsio*/ ctsio,
7183 ctl_done((union ctl_io *)ctsio);
7187 num_target_ports = 0;
7188 shared_group = (softc->is_single != 0);
7189 mtx_lock(&softc->ctl_lock);
7190 STAILQ_FOREACH(port, &softc->port_list, links) {
7191 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7193 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7196 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7199 mtx_unlock(&softc->ctl_lock);
7200 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES;
7203 total_len = sizeof(struct scsi_target_group_data_extended);
7205 total_len = sizeof(struct scsi_target_group_data);
7206 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7207 (shared_group + num_ha_groups) +
7208 sizeof(struct scsi_target_port_descriptor) * num_target_ports;
7210 alloc_len = scsi_4btoul(cdb->length);
7212 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7214 ctsio->kern_sg_entries = 0;
7216 if (total_len < alloc_len) {
7217 ctsio->residual = alloc_len - total_len;
7218 ctsio->kern_data_len = total_len;
7219 ctsio->kern_total_len = total_len;
7221 ctsio->residual = 0;
7222 ctsio->kern_data_len = alloc_len;
7223 ctsio->kern_total_len = alloc_len;
7225 ctsio->kern_data_resid = 0;
7226 ctsio->kern_rel_offset = 0;
7229 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7230 ctsio->kern_data_ptr;
7231 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7232 rtg_ext_ptr->format_type = 0x10;
7233 rtg_ext_ptr->implicit_transition_time = 0;
7234 tpg_desc = &rtg_ext_ptr->groups[0];
7236 rtg_ptr = (struct scsi_target_group_data *)
7237 ctsio->kern_data_ptr;
7238 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7239 tpg_desc = &rtg_ptr->groups[0];
7242 mtx_lock(&softc->ctl_lock);
7243 pg = softc->port_min / softc->port_cnt;
7244 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) {
7245 /* Some shelf is known to be primary. */
7246 if (softc->ha_link == CTL_HA_LINK_OFFLINE)
7247 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7248 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN)
7249 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7250 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY)
7251 os = TPG_ASYMMETRIC_ACCESS_STANDBY;
7253 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7254 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7255 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7258 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7261 /* No known primary shelf. */
7262 if (softc->ha_link == CTL_HA_LINK_OFFLINE) {
7263 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7264 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7265 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) {
7266 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7267 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7269 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7273 tpg_desc->pref_state = ts;
7274 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7275 TPG_U_SUP | TPG_T_SUP;
7276 scsi_ulto2b(1, tpg_desc->target_port_group);
7277 tpg_desc->status = TPG_IMPLICIT;
7279 STAILQ_FOREACH(port, &softc->port_list, links) {
7280 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7282 if (!softc->is_single &&
7283 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0)
7285 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7287 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7288 relative_target_port_identifier);
7291 tpg_desc->target_port_count = pc;
7292 tpg_desc = (struct scsi_target_port_group_descriptor *)
7293 &tpg_desc->descriptors[pc];
7295 for (g = 0; g < num_ha_groups; g++) {
7296 tpg_desc->pref_state = (g == pg) ? ts : os;
7297 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7298 TPG_U_SUP | TPG_T_SUP;
7299 scsi_ulto2b(2 + g, tpg_desc->target_port_group);
7300 tpg_desc->status = TPG_IMPLICIT;
7302 STAILQ_FOREACH(port, &softc->port_list, links) {
7303 if (port->targ_port < g * softc->port_cnt ||
7304 port->targ_port >= (g + 1) * softc->port_cnt)
7306 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7308 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7310 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7312 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7313 relative_target_port_identifier);
7316 tpg_desc->target_port_count = pc;
7317 tpg_desc = (struct scsi_target_port_group_descriptor *)
7318 &tpg_desc->descriptors[pc];
7320 mtx_unlock(&softc->ctl_lock);
7322 ctl_set_success(ctsio);
7323 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7324 ctsio->be_move_done = ctl_config_move_done;
7325 ctl_datamove((union ctl_io *)ctsio);
7330 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7332 struct ctl_lun *lun;
7333 struct scsi_report_supported_opcodes *cdb;
7334 const struct ctl_cmd_entry *entry, *sentry;
7335 struct scsi_report_supported_opcodes_all *all;
7336 struct scsi_report_supported_opcodes_descr *descr;
7337 struct scsi_report_supported_opcodes_one *one;
7339 int alloc_len, total_len;
7340 int opcode, service_action, i, j, num;
7342 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7344 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7345 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7347 retval = CTL_RETVAL_COMPLETE;
7349 opcode = cdb->requested_opcode;
7350 service_action = scsi_2btoul(cdb->requested_service_action);
7351 switch (cdb->options & RSO_OPTIONS_MASK) {
7352 case RSO_OPTIONS_ALL:
7354 for (i = 0; i < 256; i++) {
7355 entry = &ctl_cmd_table[i];
7356 if (entry->flags & CTL_CMD_FLAG_SA5) {
7357 for (j = 0; j < 32; j++) {
7358 sentry = &((const struct ctl_cmd_entry *)
7360 if (ctl_cmd_applicable(
7361 lun->be_lun->lun_type, sentry))
7365 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7370 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7371 num * sizeof(struct scsi_report_supported_opcodes_descr);
7373 case RSO_OPTIONS_OC:
7374 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7375 ctl_set_invalid_field(/*ctsio*/ ctsio,
7381 ctl_done((union ctl_io *)ctsio);
7382 return (CTL_RETVAL_COMPLETE);
7384 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7386 case RSO_OPTIONS_OC_SA:
7387 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7388 service_action >= 32) {
7389 ctl_set_invalid_field(/*ctsio*/ ctsio,
7395 ctl_done((union ctl_io *)ctsio);
7396 return (CTL_RETVAL_COMPLETE);
7398 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7401 ctl_set_invalid_field(/*ctsio*/ ctsio,
7407 ctl_done((union ctl_io *)ctsio);
7408 return (CTL_RETVAL_COMPLETE);
7411 alloc_len = scsi_4btoul(cdb->length);
7413 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7415 ctsio->kern_sg_entries = 0;
7417 if (total_len < alloc_len) {
7418 ctsio->residual = alloc_len - total_len;
7419 ctsio->kern_data_len = total_len;
7420 ctsio->kern_total_len = total_len;
7422 ctsio->residual = 0;
7423 ctsio->kern_data_len = alloc_len;
7424 ctsio->kern_total_len = alloc_len;
7426 ctsio->kern_data_resid = 0;
7427 ctsio->kern_rel_offset = 0;
7429 switch (cdb->options & RSO_OPTIONS_MASK) {
7430 case RSO_OPTIONS_ALL:
7431 all = (struct scsi_report_supported_opcodes_all *)
7432 ctsio->kern_data_ptr;
7434 for (i = 0; i < 256; i++) {
7435 entry = &ctl_cmd_table[i];
7436 if (entry->flags & CTL_CMD_FLAG_SA5) {
7437 for (j = 0; j < 32; j++) {
7438 sentry = &((const struct ctl_cmd_entry *)
7440 if (!ctl_cmd_applicable(
7441 lun->be_lun->lun_type, sentry))
7443 descr = &all->descr[num++];
7445 scsi_ulto2b(j, descr->service_action);
7446 descr->flags = RSO_SERVACTV;
7447 scsi_ulto2b(sentry->length,
7451 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7454 descr = &all->descr[num++];
7456 scsi_ulto2b(0, descr->service_action);
7458 scsi_ulto2b(entry->length, descr->cdb_length);
7462 num * sizeof(struct scsi_report_supported_opcodes_descr),
7465 case RSO_OPTIONS_OC:
7466 one = (struct scsi_report_supported_opcodes_one *)
7467 ctsio->kern_data_ptr;
7468 entry = &ctl_cmd_table[opcode];
7470 case RSO_OPTIONS_OC_SA:
7471 one = (struct scsi_report_supported_opcodes_one *)
7472 ctsio->kern_data_ptr;
7473 entry = &ctl_cmd_table[opcode];
7474 entry = &((const struct ctl_cmd_entry *)
7475 entry->execute)[service_action];
7477 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7479 scsi_ulto2b(entry->length, one->cdb_length);
7480 one->cdb_usage[0] = opcode;
7481 memcpy(&one->cdb_usage[1], entry->usage,
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_supported_tmf(struct ctl_scsiio *ctsio)
7498 struct scsi_report_supported_tmf *cdb;
7499 struct scsi_report_supported_tmf_data *data;
7501 int alloc_len, total_len;
7503 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7505 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7507 retval = CTL_RETVAL_COMPLETE;
7509 total_len = sizeof(struct scsi_report_supported_tmf_data);
7510 alloc_len = scsi_4btoul(cdb->length);
7512 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7514 ctsio->kern_sg_entries = 0;
7516 if (total_len < alloc_len) {
7517 ctsio->residual = alloc_len - total_len;
7518 ctsio->kern_data_len = total_len;
7519 ctsio->kern_total_len = total_len;
7521 ctsio->residual = 0;
7522 ctsio->kern_data_len = alloc_len;
7523 ctsio->kern_total_len = alloc_len;
7525 ctsio->kern_data_resid = 0;
7526 ctsio->kern_rel_offset = 0;
7528 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr;
7529 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS |
7531 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS;
7533 ctl_set_success(ctsio);
7534 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7535 ctsio->be_move_done = ctl_config_move_done;
7536 ctl_datamove((union ctl_io *)ctsio);
7541 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7543 struct scsi_report_timestamp *cdb;
7544 struct scsi_report_timestamp_data *data;
7548 int alloc_len, total_len;
7550 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7552 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7554 retval = CTL_RETVAL_COMPLETE;
7556 total_len = sizeof(struct scsi_report_timestamp_data);
7557 alloc_len = scsi_4btoul(cdb->length);
7559 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7561 ctsio->kern_sg_entries = 0;
7563 if (total_len < alloc_len) {
7564 ctsio->residual = alloc_len - total_len;
7565 ctsio->kern_data_len = total_len;
7566 ctsio->kern_total_len = total_len;
7568 ctsio->residual = 0;
7569 ctsio->kern_data_len = alloc_len;
7570 ctsio->kern_total_len = alloc_len;
7572 ctsio->kern_data_resid = 0;
7573 ctsio->kern_rel_offset = 0;
7575 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7576 scsi_ulto2b(sizeof(*data) - 2, data->length);
7577 data->origin = RTS_ORIG_OUTSIDE;
7579 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7580 scsi_ulto4b(timestamp >> 16, data->timestamp);
7581 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7583 ctl_set_success(ctsio);
7584 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7585 ctsio->be_move_done = ctl_config_move_done;
7586 ctl_datamove((union ctl_io *)ctsio);
7591 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7593 struct scsi_per_res_in *cdb;
7594 int alloc_len, total_len = 0;
7595 /* struct scsi_per_res_in_rsrv in_data; */
7596 struct ctl_lun *lun;
7597 struct ctl_softc *softc;
7600 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7602 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7604 alloc_len = scsi_2btoul(cdb->length);
7606 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7607 softc = lun->ctl_softc;
7610 mtx_lock(&lun->lun_lock);
7611 switch (cdb->action) {
7612 case SPRI_RK: /* read keys */
7613 total_len = sizeof(struct scsi_per_res_in_keys) +
7615 sizeof(struct scsi_per_res_key);
7617 case SPRI_RR: /* read reservation */
7618 if (lun->flags & CTL_LUN_PR_RESERVED)
7619 total_len = sizeof(struct scsi_per_res_in_rsrv);
7621 total_len = sizeof(struct scsi_per_res_in_header);
7623 case SPRI_RC: /* report capabilities */
7624 total_len = sizeof(struct scsi_per_res_cap);
7626 case SPRI_RS: /* read full status */
7627 total_len = sizeof(struct scsi_per_res_in_header) +
7628 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7632 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7634 mtx_unlock(&lun->lun_lock);
7636 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7638 if (total_len < alloc_len) {
7639 ctsio->residual = alloc_len - total_len;
7640 ctsio->kern_data_len = total_len;
7641 ctsio->kern_total_len = total_len;
7643 ctsio->residual = 0;
7644 ctsio->kern_data_len = alloc_len;
7645 ctsio->kern_total_len = alloc_len;
7648 ctsio->kern_data_resid = 0;
7649 ctsio->kern_rel_offset = 0;
7650 ctsio->kern_sg_entries = 0;
7652 mtx_lock(&lun->lun_lock);
7653 switch (cdb->action) {
7654 case SPRI_RK: { // read keys
7655 struct scsi_per_res_in_keys *res_keys;
7658 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7661 * We had to drop the lock to allocate our buffer, which
7662 * leaves time for someone to come in with another
7663 * persistent reservation. (That is unlikely, though,
7664 * since this should be the only persistent reservation
7665 * command active right now.)
7667 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7668 (lun->pr_key_count *
7669 sizeof(struct scsi_per_res_key)))){
7670 mtx_unlock(&lun->lun_lock);
7671 free(ctsio->kern_data_ptr, M_CTL);
7672 printf("%s: reservation length changed, retrying\n",
7677 scsi_ulto4b(lun->pr_generation, res_keys->header.generation);
7679 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7680 lun->pr_key_count, res_keys->header.length);
7682 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) {
7683 if ((key = ctl_get_prkey(lun, i)) == 0)
7687 * We used lun->pr_key_count to calculate the
7688 * size to allocate. If it turns out the number of
7689 * initiators with the registered flag set is
7690 * larger than that (i.e. they haven't been kept in
7691 * sync), we've got a problem.
7693 if (key_count >= lun->pr_key_count) {
7697 scsi_u64to8b(key, res_keys->keys[key_count].key);
7702 case SPRI_RR: { // read reservation
7703 struct scsi_per_res_in_rsrv *res;
7704 int tmp_len, header_only;
7706 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7708 scsi_ulto4b(lun->pr_generation, res->header.generation);
7710 if (lun->flags & CTL_LUN_PR_RESERVED)
7712 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7713 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7714 res->header.length);
7717 tmp_len = sizeof(struct scsi_per_res_in_header);
7718 scsi_ulto4b(0, res->header.length);
7723 * We had to drop the lock to allocate our buffer, which
7724 * leaves time for someone to come in with another
7725 * persistent reservation. (That is unlikely, though,
7726 * since this should be the only persistent reservation
7727 * command active right now.)
7729 if (tmp_len != total_len) {
7730 mtx_unlock(&lun->lun_lock);
7731 free(ctsio->kern_data_ptr, M_CTL);
7732 printf("%s: reservation status changed, retrying\n",
7738 * No reservation held, so we're done.
7740 if (header_only != 0)
7744 * If the registration is an All Registrants type, the key
7745 * is 0, since it doesn't really matter.
7747 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7748 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7749 res->data.reservation);
7751 res->data.scopetype = lun->pr_res_type;
7754 case SPRI_RC: //report capabilities
7756 struct scsi_per_res_cap *res_cap;
7759 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7760 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7761 res_cap->flags1 = SPRI_CRH;
7762 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5;
7763 type_mask = SPRI_TM_WR_EX_AR |
7769 scsi_ulto2b(type_mask, res_cap->type_mask);
7772 case SPRI_RS: { // read full status
7773 struct scsi_per_res_in_full *res_status;
7774 struct scsi_per_res_in_full_desc *res_desc;
7775 struct ctl_port *port;
7778 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7781 * We had to drop the lock to allocate our buffer, which
7782 * leaves time for someone to come in with another
7783 * persistent reservation. (That is unlikely, though,
7784 * since this should be the only persistent reservation
7785 * command active right now.)
7787 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7788 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7789 lun->pr_key_count)){
7790 mtx_unlock(&lun->lun_lock);
7791 free(ctsio->kern_data_ptr, M_CTL);
7792 printf("%s: reservation length changed, retrying\n",
7797 scsi_ulto4b(lun->pr_generation, res_status->header.generation);
7799 res_desc = &res_status->desc[0];
7800 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7801 if ((key = ctl_get_prkey(lun, i)) == 0)
7804 scsi_u64to8b(key, res_desc->res_key.key);
7805 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7806 (lun->pr_res_idx == i ||
7807 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7808 res_desc->flags = SPRI_FULL_R_HOLDER;
7809 res_desc->scopetype = lun->pr_res_type;
7811 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7812 res_desc->rel_trgt_port_id);
7814 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7816 len = ctl_create_iid(port,
7817 i % CTL_MAX_INIT_PER_PORT,
7818 res_desc->transport_id);
7819 scsi_ulto4b(len, res_desc->additional_length);
7820 res_desc = (struct scsi_per_res_in_full_desc *)
7821 &res_desc->transport_id[len];
7823 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7824 res_status->header.length);
7828 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7830 mtx_unlock(&lun->lun_lock);
7832 ctl_set_success(ctsio);
7833 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7834 ctsio->be_move_done = ctl_config_move_done;
7835 ctl_datamove((union ctl_io *)ctsio);
7836 return (CTL_RETVAL_COMPLETE);
7840 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7844 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7845 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7846 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7847 struct scsi_per_res_out_parms* param)
7849 union ctl_ha_msg persis_io;
7852 mtx_lock(&lun->lun_lock);
7853 if (sa_res_key == 0) {
7854 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7855 /* validate scope and type */
7856 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7858 mtx_unlock(&lun->lun_lock);
7859 ctl_set_invalid_field(/*ctsio*/ ctsio,
7865 ctl_done((union ctl_io *)ctsio);
7869 if (type>8 || type==2 || type==4 || type==0) {
7870 mtx_unlock(&lun->lun_lock);
7871 ctl_set_invalid_field(/*ctsio*/ ctsio,
7877 ctl_done((union ctl_io *)ctsio);
7882 * Unregister everybody else and build UA for
7885 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7886 if (i == residx || ctl_get_prkey(lun, i) == 0)
7889 ctl_clr_prkey(lun, i);
7890 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7892 lun->pr_key_count = 1;
7893 lun->pr_res_type = type;
7894 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7895 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7896 lun->pr_res_idx = residx;
7897 lun->pr_generation++;
7898 mtx_unlock(&lun->lun_lock);
7900 /* send msg to other side */
7901 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7902 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7903 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7904 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7905 persis_io.pr.pr_info.res_type = type;
7906 memcpy(persis_io.pr.pr_info.sa_res_key,
7907 param->serv_act_res_key,
7908 sizeof(param->serv_act_res_key));
7909 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7910 sizeof(persis_io.pr), M_WAITOK);
7912 /* not all registrants */
7913 mtx_unlock(&lun->lun_lock);
7914 free(ctsio->kern_data_ptr, M_CTL);
7915 ctl_set_invalid_field(ctsio,
7921 ctl_done((union ctl_io *)ctsio);
7924 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7925 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7928 if (res_key == sa_res_key) {
7931 * The spec implies this is not good but doesn't
7932 * say what to do. There are two choices either
7933 * generate a res conflict or check condition
7934 * with illegal field in parameter data. Since
7935 * that is what is done when the sa_res_key is
7936 * zero I'll take that approach since this has
7937 * to do with the sa_res_key.
7939 mtx_unlock(&lun->lun_lock);
7940 free(ctsio->kern_data_ptr, M_CTL);
7941 ctl_set_invalid_field(ctsio,
7947 ctl_done((union ctl_io *)ctsio);
7951 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7952 if (ctl_get_prkey(lun, i) != sa_res_key)
7956 ctl_clr_prkey(lun, i);
7957 lun->pr_key_count--;
7958 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7961 mtx_unlock(&lun->lun_lock);
7962 free(ctsio->kern_data_ptr, M_CTL);
7963 ctl_set_reservation_conflict(ctsio);
7964 ctl_done((union ctl_io *)ctsio);
7965 return (CTL_RETVAL_COMPLETE);
7967 lun->pr_generation++;
7968 mtx_unlock(&lun->lun_lock);
7970 /* send msg to other side */
7971 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7972 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7973 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7974 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7975 persis_io.pr.pr_info.res_type = type;
7976 memcpy(persis_io.pr.pr_info.sa_res_key,
7977 param->serv_act_res_key,
7978 sizeof(param->serv_act_res_key));
7979 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7980 sizeof(persis_io.pr), M_WAITOK);
7982 /* Reserved but not all registrants */
7983 /* sa_res_key is res holder */
7984 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
7985 /* validate scope and type */
7986 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7988 mtx_unlock(&lun->lun_lock);
7989 ctl_set_invalid_field(/*ctsio*/ ctsio,
7995 ctl_done((union ctl_io *)ctsio);
7999 if (type>8 || type==2 || type==4 || type==0) {
8000 mtx_unlock(&lun->lun_lock);
8001 ctl_set_invalid_field(/*ctsio*/ ctsio,
8007 ctl_done((union ctl_io *)ctsio);
8013 * if sa_res_key != res_key remove all
8014 * registrants w/sa_res_key and generate UA
8015 * for these registrants(Registrations
8016 * Preempted) if it wasn't an exclusive
8017 * reservation generate UA(Reservations
8018 * Preempted) for all other registered nexuses
8019 * if the type has changed. Establish the new
8020 * reservation and holder. If res_key and
8021 * sa_res_key are the same do the above
8022 * except don't unregister the res holder.
8025 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8026 if (i == residx || ctl_get_prkey(lun, i) == 0)
8029 if (sa_res_key == ctl_get_prkey(lun, i)) {
8030 ctl_clr_prkey(lun, i);
8031 lun->pr_key_count--;
8032 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8033 } else if (type != lun->pr_res_type &&
8034 (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8035 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8036 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8039 lun->pr_res_type = type;
8040 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8041 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8042 lun->pr_res_idx = residx;
8044 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8045 lun->pr_generation++;
8046 mtx_unlock(&lun->lun_lock);
8048 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8049 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8050 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8051 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8052 persis_io.pr.pr_info.res_type = type;
8053 memcpy(persis_io.pr.pr_info.sa_res_key,
8054 param->serv_act_res_key,
8055 sizeof(param->serv_act_res_key));
8056 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8057 sizeof(persis_io.pr), M_WAITOK);
8060 * sa_res_key is not the res holder just
8061 * remove registrants
8065 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8066 if (sa_res_key != ctl_get_prkey(lun, i))
8070 ctl_clr_prkey(lun, i);
8071 lun->pr_key_count--;
8072 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8076 mtx_unlock(&lun->lun_lock);
8077 free(ctsio->kern_data_ptr, M_CTL);
8078 ctl_set_reservation_conflict(ctsio);
8079 ctl_done((union ctl_io *)ctsio);
8082 lun->pr_generation++;
8083 mtx_unlock(&lun->lun_lock);
8085 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8086 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8087 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8088 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8089 persis_io.pr.pr_info.res_type = type;
8090 memcpy(persis_io.pr.pr_info.sa_res_key,
8091 param->serv_act_res_key,
8092 sizeof(param->serv_act_res_key));
8093 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8094 sizeof(persis_io.pr), M_WAITOK);
8101 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8103 uint64_t sa_res_key;
8106 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8108 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8109 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8110 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
8111 if (sa_res_key == 0) {
8113 * Unregister everybody else and build UA for
8116 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8117 if (i == msg->pr.pr_info.residx ||
8118 ctl_get_prkey(lun, i) == 0)
8121 ctl_clr_prkey(lun, i);
8122 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8125 lun->pr_key_count = 1;
8126 lun->pr_res_type = msg->pr.pr_info.res_type;
8127 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8128 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8129 lun->pr_res_idx = msg->pr.pr_info.residx;
8131 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8132 if (sa_res_key == ctl_get_prkey(lun, i))
8135 ctl_clr_prkey(lun, i);
8136 lun->pr_key_count--;
8137 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8141 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8142 if (i == msg->pr.pr_info.residx ||
8143 ctl_get_prkey(lun, i) == 0)
8146 if (sa_res_key == ctl_get_prkey(lun, i)) {
8147 ctl_clr_prkey(lun, i);
8148 lun->pr_key_count--;
8149 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8150 } else if (msg->pr.pr_info.res_type != lun->pr_res_type
8151 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8152 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8153 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8156 lun->pr_res_type = msg->pr.pr_info.res_type;
8157 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8158 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8159 lun->pr_res_idx = msg->pr.pr_info.residx;
8161 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8163 lun->pr_generation++;
8169 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8172 u_int32_t param_len;
8173 struct scsi_per_res_out *cdb;
8174 struct ctl_lun *lun;
8175 struct scsi_per_res_out_parms* param;
8176 struct ctl_softc *softc;
8178 uint64_t res_key, sa_res_key, key;
8180 union ctl_ha_msg persis_io;
8183 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8185 retval = CTL_RETVAL_COMPLETE;
8187 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8188 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8189 softc = lun->ctl_softc;
8192 * We only support whole-LUN scope. The scope & type are ignored for
8193 * register, register and ignore existing key and clear.
8194 * We sometimes ignore scope and type on preempts too!!
8195 * Verify reservation type here as well.
8197 type = cdb->scope_type & SPR_TYPE_MASK;
8198 if ((cdb->action == SPRO_RESERVE)
8199 || (cdb->action == SPRO_RELEASE)) {
8200 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8201 ctl_set_invalid_field(/*ctsio*/ ctsio,
8207 ctl_done((union ctl_io *)ctsio);
8208 return (CTL_RETVAL_COMPLETE);
8211 if (type>8 || type==2 || type==4 || type==0) {
8212 ctl_set_invalid_field(/*ctsio*/ ctsio,
8218 ctl_done((union ctl_io *)ctsio);
8219 return (CTL_RETVAL_COMPLETE);
8223 param_len = scsi_4btoul(cdb->length);
8225 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8226 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8227 ctsio->kern_data_len = param_len;
8228 ctsio->kern_total_len = param_len;
8229 ctsio->kern_data_resid = 0;
8230 ctsio->kern_rel_offset = 0;
8231 ctsio->kern_sg_entries = 0;
8232 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8233 ctsio->be_move_done = ctl_config_move_done;
8234 ctl_datamove((union ctl_io *)ctsio);
8236 return (CTL_RETVAL_COMPLETE);
8239 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8241 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8242 res_key = scsi_8btou64(param->res_key.key);
8243 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8246 * Validate the reservation key here except for SPRO_REG_IGNO
8247 * This must be done for all other service actions
8249 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8250 mtx_lock(&lun->lun_lock);
8251 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8252 if (res_key != key) {
8254 * The current key passed in doesn't match
8255 * the one the initiator previously
8258 mtx_unlock(&lun->lun_lock);
8259 free(ctsio->kern_data_ptr, M_CTL);
8260 ctl_set_reservation_conflict(ctsio);
8261 ctl_done((union ctl_io *)ctsio);
8262 return (CTL_RETVAL_COMPLETE);
8264 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8266 * We are not registered
8268 mtx_unlock(&lun->lun_lock);
8269 free(ctsio->kern_data_ptr, M_CTL);
8270 ctl_set_reservation_conflict(ctsio);
8271 ctl_done((union ctl_io *)ctsio);
8272 return (CTL_RETVAL_COMPLETE);
8273 } else if (res_key != 0) {
8275 * We are not registered and trying to register but
8276 * the register key isn't zero.
8278 mtx_unlock(&lun->lun_lock);
8279 free(ctsio->kern_data_ptr, M_CTL);
8280 ctl_set_reservation_conflict(ctsio);
8281 ctl_done((union ctl_io *)ctsio);
8282 return (CTL_RETVAL_COMPLETE);
8284 mtx_unlock(&lun->lun_lock);
8287 switch (cdb->action & SPRO_ACTION_MASK) {
8289 case SPRO_REG_IGNO: {
8292 printf("Registration received\n");
8296 * We don't support any of these options, as we report in
8297 * the read capabilities request (see
8298 * ctl_persistent_reserve_in(), above).
8300 if ((param->flags & SPR_SPEC_I_PT)
8301 || (param->flags & SPR_ALL_TG_PT)
8302 || (param->flags & SPR_APTPL)) {
8305 if (param->flags & SPR_APTPL)
8307 else if (param->flags & SPR_ALL_TG_PT)
8309 else /* SPR_SPEC_I_PT */
8312 free(ctsio->kern_data_ptr, M_CTL);
8313 ctl_set_invalid_field(ctsio,
8319 ctl_done((union ctl_io *)ctsio);
8320 return (CTL_RETVAL_COMPLETE);
8323 mtx_lock(&lun->lun_lock);
8326 * The initiator wants to clear the
8329 if (sa_res_key == 0) {
8331 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8332 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8333 && ctl_get_prkey(lun, residx) == 0)) {
8334 mtx_unlock(&lun->lun_lock);
8338 ctl_clr_prkey(lun, residx);
8339 lun->pr_key_count--;
8341 if (residx == lun->pr_res_idx) {
8342 lun->flags &= ~CTL_LUN_PR_RESERVED;
8343 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8345 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8346 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8347 lun->pr_key_count) {
8349 * If the reservation is a registrants
8350 * only type we need to generate a UA
8351 * for other registered inits. The
8352 * sense code should be RESERVATIONS
8356 for (i = softc->init_min; i < softc->init_max; i++){
8357 if (ctl_get_prkey(lun, i) == 0)
8360 CTL_UA_RES_RELEASE);
8363 lun->pr_res_type = 0;
8364 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8365 if (lun->pr_key_count==0) {
8366 lun->flags &= ~CTL_LUN_PR_RESERVED;
8367 lun->pr_res_type = 0;
8368 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8371 lun->pr_generation++;
8372 mtx_unlock(&lun->lun_lock);
8374 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8375 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8376 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8377 persis_io.pr.pr_info.residx = residx;
8378 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8379 sizeof(persis_io.pr), M_WAITOK);
8380 } else /* sa_res_key != 0 */ {
8383 * If we aren't registered currently then increment
8384 * the key count and set the registered flag.
8386 ctl_alloc_prkey(lun, residx);
8387 if (ctl_get_prkey(lun, residx) == 0)
8388 lun->pr_key_count++;
8389 ctl_set_prkey(lun, residx, sa_res_key);
8390 lun->pr_generation++;
8391 mtx_unlock(&lun->lun_lock);
8393 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8394 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8395 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8396 persis_io.pr.pr_info.residx = residx;
8397 memcpy(persis_io.pr.pr_info.sa_res_key,
8398 param->serv_act_res_key,
8399 sizeof(param->serv_act_res_key));
8400 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8401 sizeof(persis_io.pr), M_WAITOK);
8408 printf("Reserve executed type %d\n", type);
8410 mtx_lock(&lun->lun_lock);
8411 if (lun->flags & CTL_LUN_PR_RESERVED) {
8413 * if this isn't the reservation holder and it's
8414 * not a "all registrants" type or if the type is
8415 * different then we have a conflict
8417 if ((lun->pr_res_idx != residx
8418 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8419 || lun->pr_res_type != type) {
8420 mtx_unlock(&lun->lun_lock);
8421 free(ctsio->kern_data_ptr, M_CTL);
8422 ctl_set_reservation_conflict(ctsio);
8423 ctl_done((union ctl_io *)ctsio);
8424 return (CTL_RETVAL_COMPLETE);
8426 mtx_unlock(&lun->lun_lock);
8427 } else /* create a reservation */ {
8429 * If it's not an "all registrants" type record
8430 * reservation holder
8432 if (type != SPR_TYPE_WR_EX_AR
8433 && type != SPR_TYPE_EX_AC_AR)
8434 lun->pr_res_idx = residx; /* Res holder */
8436 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8438 lun->flags |= CTL_LUN_PR_RESERVED;
8439 lun->pr_res_type = type;
8441 mtx_unlock(&lun->lun_lock);
8443 /* send msg to other side */
8444 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8445 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8446 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8447 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8448 persis_io.pr.pr_info.res_type = type;
8449 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8450 sizeof(persis_io.pr), M_WAITOK);
8455 mtx_lock(&lun->lun_lock);
8456 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8457 /* No reservation exists return good status */
8458 mtx_unlock(&lun->lun_lock);
8462 * Is this nexus a reservation holder?
8464 if (lun->pr_res_idx != residx
8465 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8467 * not a res holder return good status but
8470 mtx_unlock(&lun->lun_lock);
8474 if (lun->pr_res_type != type) {
8475 mtx_unlock(&lun->lun_lock);
8476 free(ctsio->kern_data_ptr, M_CTL);
8477 ctl_set_illegal_pr_release(ctsio);
8478 ctl_done((union ctl_io *)ctsio);
8479 return (CTL_RETVAL_COMPLETE);
8482 /* okay to release */
8483 lun->flags &= ~CTL_LUN_PR_RESERVED;
8484 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8485 lun->pr_res_type = 0;
8488 * if this isn't an exclusive access
8489 * res generate UA for all other
8492 if (type != SPR_TYPE_EX_AC
8493 && type != SPR_TYPE_WR_EX) {
8494 for (i = softc->init_min; i < softc->init_max; i++) {
8495 if (i == residx || ctl_get_prkey(lun, i) == 0)
8497 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8500 mtx_unlock(&lun->lun_lock);
8502 /* Send msg to other side */
8503 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8504 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8505 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8506 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8507 sizeof(persis_io.pr), M_WAITOK);
8511 /* send msg to other side */
8513 mtx_lock(&lun->lun_lock);
8514 lun->flags &= ~CTL_LUN_PR_RESERVED;
8515 lun->pr_res_type = 0;
8516 lun->pr_key_count = 0;
8517 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8519 ctl_clr_prkey(lun, residx);
8520 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8521 if (ctl_get_prkey(lun, i) != 0) {
8522 ctl_clr_prkey(lun, i);
8523 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8525 lun->pr_generation++;
8526 mtx_unlock(&lun->lun_lock);
8528 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8529 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8530 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8531 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8532 sizeof(persis_io.pr), M_WAITOK);
8536 case SPRO_PRE_ABO: {
8539 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8540 residx, ctsio, cdb, param);
8542 return (CTL_RETVAL_COMPLETE);
8546 panic("%s: Invalid PR type %#x", __func__, cdb->action);
8550 free(ctsio->kern_data_ptr, M_CTL);
8551 ctl_set_success(ctsio);
8552 ctl_done((union ctl_io *)ctsio);
8558 * This routine is for handling a message from the other SC pertaining to
8559 * persistent reserve out. All the error checking will have been done
8560 * so only perorming the action need be done here to keep the two
8564 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8566 struct ctl_softc *softc = control_softc;
8567 struct ctl_lun *lun;
8569 uint32_t residx, targ_lun;
8571 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8572 mtx_lock(&softc->ctl_lock);
8573 if ((targ_lun >= CTL_MAX_LUNS) ||
8574 ((lun = softc->ctl_luns[targ_lun]) == NULL)) {
8575 mtx_unlock(&softc->ctl_lock);
8578 mtx_lock(&lun->lun_lock);
8579 mtx_unlock(&softc->ctl_lock);
8580 if (lun->flags & CTL_LUN_DISABLED) {
8581 mtx_unlock(&lun->lun_lock);
8584 residx = ctl_get_initindex(&msg->hdr.nexus);
8585 switch(msg->pr.pr_info.action) {
8586 case CTL_PR_REG_KEY:
8587 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8588 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8589 lun->pr_key_count++;
8590 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8591 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8592 lun->pr_generation++;
8595 case CTL_PR_UNREG_KEY:
8596 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8597 lun->pr_key_count--;
8599 /* XXX Need to see if the reservation has been released */
8600 /* if so do we need to generate UA? */
8601 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8602 lun->flags &= ~CTL_LUN_PR_RESERVED;
8603 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8605 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8606 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8607 lun->pr_key_count) {
8609 * If the reservation is a registrants
8610 * only type we need to generate a UA
8611 * for other registered inits. The
8612 * sense code should be RESERVATIONS
8616 for (i = softc->init_min; i < softc->init_max; i++) {
8617 if (ctl_get_prkey(lun, i) == 0)
8620 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8623 lun->pr_res_type = 0;
8624 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8625 if (lun->pr_key_count==0) {
8626 lun->flags &= ~CTL_LUN_PR_RESERVED;
8627 lun->pr_res_type = 0;
8628 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8631 lun->pr_generation++;
8634 case CTL_PR_RESERVE:
8635 lun->flags |= CTL_LUN_PR_RESERVED;
8636 lun->pr_res_type = msg->pr.pr_info.res_type;
8637 lun->pr_res_idx = msg->pr.pr_info.residx;
8641 case CTL_PR_RELEASE:
8643 * if this isn't an exclusive access res generate UA for all
8644 * other registrants.
8646 if (lun->pr_res_type != SPR_TYPE_EX_AC &&
8647 lun->pr_res_type != SPR_TYPE_WR_EX) {
8648 for (i = softc->init_min; i < softc->init_max; i++)
8649 if (i == residx || ctl_get_prkey(lun, i) == 0)
8651 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8654 lun->flags &= ~CTL_LUN_PR_RESERVED;
8655 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8656 lun->pr_res_type = 0;
8659 case CTL_PR_PREEMPT:
8660 ctl_pro_preempt_other(lun, msg);
8663 lun->flags &= ~CTL_LUN_PR_RESERVED;
8664 lun->pr_res_type = 0;
8665 lun->pr_key_count = 0;
8666 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8668 for (i=0; i < CTL_MAX_INITIATORS; i++) {
8669 if (ctl_get_prkey(lun, i) == 0)
8671 ctl_clr_prkey(lun, i);
8672 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8674 lun->pr_generation++;
8678 mtx_unlock(&lun->lun_lock);
8682 ctl_read_write(struct ctl_scsiio *ctsio)
8684 struct ctl_lun *lun;
8685 struct ctl_lba_len_flags *lbalen;
8687 uint32_t num_blocks;
8691 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8693 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8696 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8697 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8698 switch (ctsio->cdb[0]) {
8701 struct scsi_rw_6 *cdb;
8703 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8705 lba = scsi_3btoul(cdb->addr);
8706 /* only 5 bits are valid in the most significant address byte */
8708 num_blocks = cdb->length;
8710 * This is correct according to SBC-2.
8712 if (num_blocks == 0)
8718 struct scsi_rw_10 *cdb;
8720 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8721 if (cdb->byte2 & SRW10_FUA)
8722 flags |= CTL_LLF_FUA;
8723 if (cdb->byte2 & SRW10_DPO)
8724 flags |= CTL_LLF_DPO;
8725 lba = scsi_4btoul(cdb->addr);
8726 num_blocks = scsi_2btoul(cdb->length);
8729 case WRITE_VERIFY_10: {
8730 struct scsi_write_verify_10 *cdb;
8732 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8733 flags |= CTL_LLF_FUA;
8734 if (cdb->byte2 & SWV_DPO)
8735 flags |= CTL_LLF_DPO;
8736 lba = scsi_4btoul(cdb->addr);
8737 num_blocks = scsi_2btoul(cdb->length);
8742 struct scsi_rw_12 *cdb;
8744 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8745 if (cdb->byte2 & SRW12_FUA)
8746 flags |= CTL_LLF_FUA;
8747 if (cdb->byte2 & SRW12_DPO)
8748 flags |= CTL_LLF_DPO;
8749 lba = scsi_4btoul(cdb->addr);
8750 num_blocks = scsi_4btoul(cdb->length);
8753 case WRITE_VERIFY_12: {
8754 struct scsi_write_verify_12 *cdb;
8756 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8757 flags |= CTL_LLF_FUA;
8758 if (cdb->byte2 & SWV_DPO)
8759 flags |= CTL_LLF_DPO;
8760 lba = scsi_4btoul(cdb->addr);
8761 num_blocks = scsi_4btoul(cdb->length);
8766 struct scsi_rw_16 *cdb;
8768 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8769 if (cdb->byte2 & SRW12_FUA)
8770 flags |= CTL_LLF_FUA;
8771 if (cdb->byte2 & SRW12_DPO)
8772 flags |= CTL_LLF_DPO;
8773 lba = scsi_8btou64(cdb->addr);
8774 num_blocks = scsi_4btoul(cdb->length);
8777 case WRITE_ATOMIC_16: {
8778 struct scsi_write_atomic_16 *cdb;
8780 if (lun->be_lun->atomicblock == 0) {
8781 ctl_set_invalid_opcode(ctsio);
8782 ctl_done((union ctl_io *)ctsio);
8783 return (CTL_RETVAL_COMPLETE);
8786 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb;
8787 if (cdb->byte2 & SRW12_FUA)
8788 flags |= CTL_LLF_FUA;
8789 if (cdb->byte2 & SRW12_DPO)
8790 flags |= CTL_LLF_DPO;
8791 lba = scsi_8btou64(cdb->addr);
8792 num_blocks = scsi_2btoul(cdb->length);
8793 if (num_blocks > lun->be_lun->atomicblock) {
8794 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8795 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8797 ctl_done((union ctl_io *)ctsio);
8798 return (CTL_RETVAL_COMPLETE);
8802 case WRITE_VERIFY_16: {
8803 struct scsi_write_verify_16 *cdb;
8805 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8806 flags |= CTL_LLF_FUA;
8807 if (cdb->byte2 & SWV_DPO)
8808 flags |= CTL_LLF_DPO;
8809 lba = scsi_8btou64(cdb->addr);
8810 num_blocks = scsi_4btoul(cdb->length);
8815 * We got a command we don't support. This shouldn't
8816 * happen, commands should be filtered out above us.
8818 ctl_set_invalid_opcode(ctsio);
8819 ctl_done((union ctl_io *)ctsio);
8821 return (CTL_RETVAL_COMPLETE);
8822 break; /* NOTREACHED */
8826 * The first check is to make sure we're in bounds, the second
8827 * check is to catch wrap-around problems. If the lba + num blocks
8828 * is less than the lba, then we've wrapped around and the block
8829 * range is invalid anyway.
8831 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8832 || ((lba + num_blocks) < lba)) {
8833 ctl_set_lba_out_of_range(ctsio);
8834 ctl_done((union ctl_io *)ctsio);
8835 return (CTL_RETVAL_COMPLETE);
8839 * According to SBC-3, a transfer length of 0 is not an error.
8840 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8841 * translates to 256 blocks for those commands.
8843 if (num_blocks == 0) {
8844 ctl_set_success(ctsio);
8845 ctl_done((union ctl_io *)ctsio);
8846 return (CTL_RETVAL_COMPLETE);
8849 /* Set FUA and/or DPO if caches are disabled. */
8851 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8853 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8855 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8857 flags |= CTL_LLF_FUA;
8860 lbalen = (struct ctl_lba_len_flags *)
8861 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8863 lbalen->len = num_blocks;
8864 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8866 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8867 ctsio->kern_rel_offset = 0;
8869 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8871 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8876 ctl_cnw_cont(union ctl_io *io)
8878 struct ctl_scsiio *ctsio;
8879 struct ctl_lun *lun;
8880 struct ctl_lba_len_flags *lbalen;
8883 ctsio = &io->scsiio;
8884 ctsio->io_hdr.status = CTL_STATUS_NONE;
8885 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8886 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8887 lbalen = (struct ctl_lba_len_flags *)
8888 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8889 lbalen->flags &= ~CTL_LLF_COMPARE;
8890 lbalen->flags |= CTL_LLF_WRITE;
8892 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8893 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8898 ctl_cnw(struct ctl_scsiio *ctsio)
8900 struct ctl_lun *lun;
8901 struct ctl_lba_len_flags *lbalen;
8903 uint32_t num_blocks;
8906 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8908 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8911 switch (ctsio->cdb[0]) {
8912 case COMPARE_AND_WRITE: {
8913 struct scsi_compare_and_write *cdb;
8915 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8916 if (cdb->byte2 & SRW10_FUA)
8917 flags |= CTL_LLF_FUA;
8918 if (cdb->byte2 & SRW10_DPO)
8919 flags |= CTL_LLF_DPO;
8920 lba = scsi_8btou64(cdb->addr);
8921 num_blocks = cdb->length;
8926 * We got a command we don't support. This shouldn't
8927 * happen, commands should be filtered out above us.
8929 ctl_set_invalid_opcode(ctsio);
8930 ctl_done((union ctl_io *)ctsio);
8932 return (CTL_RETVAL_COMPLETE);
8933 break; /* NOTREACHED */
8937 * The first check is to make sure we're in bounds, the second
8938 * check is to catch wrap-around problems. If the lba + num blocks
8939 * is less than the lba, then we've wrapped around and the block
8940 * range is invalid anyway.
8942 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8943 || ((lba + num_blocks) < lba)) {
8944 ctl_set_lba_out_of_range(ctsio);
8945 ctl_done((union ctl_io *)ctsio);
8946 return (CTL_RETVAL_COMPLETE);
8950 * According to SBC-3, a transfer length of 0 is not an error.
8952 if (num_blocks == 0) {
8953 ctl_set_success(ctsio);
8954 ctl_done((union ctl_io *)ctsio);
8955 return (CTL_RETVAL_COMPLETE);
8958 /* Set FUA if write cache is disabled. */
8959 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8961 flags |= CTL_LLF_FUA;
8963 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
8964 ctsio->kern_rel_offset = 0;
8967 * Set the IO_CONT flag, so that if this I/O gets passed to
8968 * ctl_data_submit_done(), it'll get passed back to
8969 * ctl_ctl_cnw_cont() for further processing.
8971 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
8972 ctsio->io_cont = ctl_cnw_cont;
8974 lbalen = (struct ctl_lba_len_flags *)
8975 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8977 lbalen->len = num_blocks;
8978 lbalen->flags = CTL_LLF_COMPARE | flags;
8980 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
8981 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8986 ctl_verify(struct ctl_scsiio *ctsio)
8988 struct ctl_lun *lun;
8989 struct ctl_lba_len_flags *lbalen;
8991 uint32_t num_blocks;
8995 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8997 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
9000 flags = CTL_LLF_FUA;
9001 switch (ctsio->cdb[0]) {
9003 struct scsi_verify_10 *cdb;
9005 cdb = (struct scsi_verify_10 *)ctsio->cdb;
9006 if (cdb->byte2 & SVFY_BYTCHK)
9008 if (cdb->byte2 & SVFY_DPO)
9009 flags |= CTL_LLF_DPO;
9010 lba = scsi_4btoul(cdb->addr);
9011 num_blocks = scsi_2btoul(cdb->length);
9015 struct scsi_verify_12 *cdb;
9017 cdb = (struct scsi_verify_12 *)ctsio->cdb;
9018 if (cdb->byte2 & SVFY_BYTCHK)
9020 if (cdb->byte2 & SVFY_DPO)
9021 flags |= CTL_LLF_DPO;
9022 lba = scsi_4btoul(cdb->addr);
9023 num_blocks = scsi_4btoul(cdb->length);
9027 struct scsi_rw_16 *cdb;
9029 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9030 if (cdb->byte2 & SVFY_BYTCHK)
9032 if (cdb->byte2 & SVFY_DPO)
9033 flags |= CTL_LLF_DPO;
9034 lba = scsi_8btou64(cdb->addr);
9035 num_blocks = scsi_4btoul(cdb->length);
9040 * We got a command we don't support. This shouldn't
9041 * happen, commands should be filtered out above us.
9043 ctl_set_invalid_opcode(ctsio);
9044 ctl_done((union ctl_io *)ctsio);
9045 return (CTL_RETVAL_COMPLETE);
9049 * The first check is to make sure we're in bounds, the second
9050 * check is to catch wrap-around problems. If the lba + num blocks
9051 * is less than the lba, then we've wrapped around and the block
9052 * range is invalid anyway.
9054 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9055 || ((lba + num_blocks) < lba)) {
9056 ctl_set_lba_out_of_range(ctsio);
9057 ctl_done((union ctl_io *)ctsio);
9058 return (CTL_RETVAL_COMPLETE);
9062 * According to SBC-3, a transfer length of 0 is not an error.
9064 if (num_blocks == 0) {
9065 ctl_set_success(ctsio);
9066 ctl_done((union ctl_io *)ctsio);
9067 return (CTL_RETVAL_COMPLETE);
9070 lbalen = (struct ctl_lba_len_flags *)
9071 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9073 lbalen->len = num_blocks;
9075 lbalen->flags = CTL_LLF_COMPARE | flags;
9076 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9078 lbalen->flags = CTL_LLF_VERIFY | flags;
9079 ctsio->kern_total_len = 0;
9081 ctsio->kern_rel_offset = 0;
9083 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9084 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9089 ctl_report_luns(struct ctl_scsiio *ctsio)
9091 struct ctl_softc *softc;
9092 struct scsi_report_luns *cdb;
9093 struct scsi_report_luns_data *lun_data;
9094 struct ctl_lun *lun, *request_lun;
9095 struct ctl_port *port;
9096 int num_luns, retval;
9097 uint32_t alloc_len, lun_datalen;
9099 uint32_t initidx, targ_lun_id, lun_id;
9101 retval = CTL_RETVAL_COMPLETE;
9102 cdb = (struct scsi_report_luns *)ctsio->cdb;
9103 port = ctl_io_port(&ctsio->io_hdr);
9104 softc = port->ctl_softc;
9106 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9108 mtx_lock(&softc->ctl_lock);
9110 for (targ_lun_id = 0; targ_lun_id < CTL_MAX_LUNS; targ_lun_id++) {
9111 if (ctl_lun_map_from_port(port, targ_lun_id) < CTL_MAX_LUNS)
9114 mtx_unlock(&softc->ctl_lock);
9116 switch (cdb->select_report) {
9117 case RPL_REPORT_DEFAULT:
9118 case RPL_REPORT_ALL:
9119 case RPL_REPORT_NONSUBSID:
9121 case RPL_REPORT_WELLKNOWN:
9122 case RPL_REPORT_ADMIN:
9123 case RPL_REPORT_CONGLOM:
9127 ctl_set_invalid_field(ctsio,
9133 ctl_done((union ctl_io *)ctsio);
9135 break; /* NOTREACHED */
9138 alloc_len = scsi_4btoul(cdb->length);
9140 * The initiator has to allocate at least 16 bytes for this request,
9141 * so he can at least get the header and the first LUN. Otherwise
9142 * we reject the request (per SPC-3 rev 14, section 6.21).
9144 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9145 sizeof(struct scsi_report_luns_lundata))) {
9146 ctl_set_invalid_field(ctsio,
9152 ctl_done((union ctl_io *)ctsio);
9156 request_lun = (struct ctl_lun *)
9157 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9159 lun_datalen = sizeof(*lun_data) +
9160 (num_luns * sizeof(struct scsi_report_luns_lundata));
9162 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9163 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9164 ctsio->kern_sg_entries = 0;
9166 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9168 mtx_lock(&softc->ctl_lock);
9169 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9170 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9171 if (lun_id >= CTL_MAX_LUNS)
9173 lun = softc->ctl_luns[lun_id];
9177 be64enc(lun_data->luns[num_filled++].lundata,
9178 ctl_encode_lun(targ_lun_id));
9181 * According to SPC-3, rev 14 section 6.21:
9183 * "The execution of a REPORT LUNS command to any valid and
9184 * installed logical unit shall clear the REPORTED LUNS DATA
9185 * HAS CHANGED unit attention condition for all logical
9186 * units of that target with respect to the requesting
9187 * initiator. A valid and installed logical unit is one
9188 * having a PERIPHERAL QUALIFIER of 000b in the standard
9189 * INQUIRY data (see 6.4.2)."
9191 * If request_lun is NULL, the LUN this report luns command
9192 * was issued to is either disabled or doesn't exist. In that
9193 * case, we shouldn't clear any pending lun change unit
9196 if (request_lun != NULL) {
9197 mtx_lock(&lun->lun_lock);
9198 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE);
9199 mtx_unlock(&lun->lun_lock);
9202 mtx_unlock(&softc->ctl_lock);
9205 * It's quite possible that we've returned fewer LUNs than we allocated
9206 * space for. Trim it.
9208 lun_datalen = sizeof(*lun_data) +
9209 (num_filled * sizeof(struct scsi_report_luns_lundata));
9211 if (lun_datalen < alloc_len) {
9212 ctsio->residual = alloc_len - lun_datalen;
9213 ctsio->kern_data_len = lun_datalen;
9214 ctsio->kern_total_len = lun_datalen;
9216 ctsio->residual = 0;
9217 ctsio->kern_data_len = alloc_len;
9218 ctsio->kern_total_len = alloc_len;
9220 ctsio->kern_data_resid = 0;
9221 ctsio->kern_rel_offset = 0;
9222 ctsio->kern_sg_entries = 0;
9225 * We set this to the actual data length, regardless of how much
9226 * space we actually have to return results. If the user looks at
9227 * this value, he'll know whether or not he allocated enough space
9228 * and reissue the command if necessary. We don't support well
9229 * known logical units, so if the user asks for that, return none.
9231 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9234 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9237 ctl_set_success(ctsio);
9238 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9239 ctsio->be_move_done = ctl_config_move_done;
9240 ctl_datamove((union ctl_io *)ctsio);
9245 ctl_request_sense(struct ctl_scsiio *ctsio)
9247 struct scsi_request_sense *cdb;
9248 struct scsi_sense_data *sense_ptr;
9249 struct ctl_softc *ctl_softc;
9250 struct ctl_lun *lun;
9253 scsi_sense_data_type sense_format;
9254 ctl_ua_type ua_type;
9256 cdb = (struct scsi_request_sense *)ctsio->cdb;
9258 ctl_softc = control_softc;
9259 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9261 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9264 * Determine which sense format the user wants.
9266 if (cdb->byte2 & SRS_DESC)
9267 sense_format = SSD_TYPE_DESC;
9269 sense_format = SSD_TYPE_FIXED;
9271 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9272 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9273 ctsio->kern_sg_entries = 0;
9276 * struct scsi_sense_data, which is currently set to 256 bytes, is
9277 * larger than the largest allowed value for the length field in the
9278 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9280 ctsio->residual = 0;
9281 ctsio->kern_data_len = cdb->length;
9282 ctsio->kern_total_len = cdb->length;
9284 ctsio->kern_data_resid = 0;
9285 ctsio->kern_rel_offset = 0;
9286 ctsio->kern_sg_entries = 0;
9289 * If we don't have a LUN, we don't have any pending sense.
9295 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9297 * Check for pending sense, and then for pending unit attentions.
9298 * Pending sense gets returned first, then pending unit attentions.
9300 mtx_lock(&lun->lun_lock);
9302 if (ctl_is_set(lun->have_ca, initidx)) {
9303 scsi_sense_data_type stored_format;
9306 * Check to see which sense format was used for the stored
9309 stored_format = scsi_sense_type(&lun->pending_sense[initidx]);
9312 * If the user requested a different sense format than the
9313 * one we stored, then we need to convert it to the other
9314 * format. If we're going from descriptor to fixed format
9315 * sense data, we may lose things in translation, depending
9316 * on what options were used.
9318 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9319 * for some reason we'll just copy it out as-is.
9321 if ((stored_format == SSD_TYPE_FIXED)
9322 && (sense_format == SSD_TYPE_DESC))
9323 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9324 &lun->pending_sense[initidx],
9325 (struct scsi_sense_data_desc *)sense_ptr);
9326 else if ((stored_format == SSD_TYPE_DESC)
9327 && (sense_format == SSD_TYPE_FIXED))
9328 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9329 &lun->pending_sense[initidx],
9330 (struct scsi_sense_data_fixed *)sense_ptr);
9332 memcpy(sense_ptr, &lun->pending_sense[initidx],
9333 MIN(sizeof(*sense_ptr),
9334 sizeof(lun->pending_sense[initidx])));
9336 ctl_clear_mask(lun->have_ca, initidx);
9341 ua_type = ctl_build_ua(lun, initidx, sense_ptr, sense_format);
9342 if (ua_type != CTL_UA_NONE)
9344 if (ua_type == CTL_UA_LUN_CHANGE) {
9345 mtx_unlock(&lun->lun_lock);
9346 mtx_lock(&ctl_softc->ctl_lock);
9347 ctl_clr_ua_allluns(ctl_softc, initidx, ua_type);
9348 mtx_unlock(&ctl_softc->ctl_lock);
9349 mtx_lock(&lun->lun_lock);
9353 mtx_unlock(&lun->lun_lock);
9356 * We already have a pending error, return it.
9358 if (have_error != 0) {
9360 * We report the SCSI status as OK, since the status of the
9361 * request sense command itself is OK.
9362 * We report 0 for the sense length, because we aren't doing
9363 * autosense in this case. We're reporting sense as
9366 ctl_set_success(ctsio);
9367 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9368 ctsio->be_move_done = ctl_config_move_done;
9369 ctl_datamove((union ctl_io *)ctsio);
9370 return (CTL_RETVAL_COMPLETE);
9376 * No sense information to report, so we report that everything is
9379 ctl_set_sense_data(sense_ptr,
9382 /*current_error*/ 1,
9383 /*sense_key*/ SSD_KEY_NO_SENSE,
9389 * We report 0 for the sense length, because we aren't doing
9390 * autosense in this case. We're reporting sense as parameter data.
9392 ctl_set_success(ctsio);
9393 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9394 ctsio->be_move_done = ctl_config_move_done;
9395 ctl_datamove((union ctl_io *)ctsio);
9396 return (CTL_RETVAL_COMPLETE);
9400 ctl_tur(struct ctl_scsiio *ctsio)
9403 CTL_DEBUG_PRINT(("ctl_tur\n"));
9405 ctl_set_success(ctsio);
9406 ctl_done((union ctl_io *)ctsio);
9408 return (CTL_RETVAL_COMPLETE);
9412 * SCSI VPD page 0x00, the Supported VPD Pages page.
9415 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9417 struct scsi_vpd_supported_pages *pages;
9419 struct ctl_lun *lun;
9422 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9424 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9425 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9426 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9427 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9428 ctsio->kern_sg_entries = 0;
9430 if (sup_page_size < alloc_len) {
9431 ctsio->residual = alloc_len - sup_page_size;
9432 ctsio->kern_data_len = sup_page_size;
9433 ctsio->kern_total_len = sup_page_size;
9435 ctsio->residual = 0;
9436 ctsio->kern_data_len = alloc_len;
9437 ctsio->kern_total_len = alloc_len;
9439 ctsio->kern_data_resid = 0;
9440 ctsio->kern_rel_offset = 0;
9441 ctsio->kern_sg_entries = 0;
9444 * The control device is always connected. The disk device, on the
9445 * other hand, may not be online all the time. Need to change this
9446 * to figure out whether the disk device is actually online or not.
9449 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9450 lun->be_lun->lun_type;
9452 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9455 /* Supported VPD pages */
9456 pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9458 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9459 /* Device Identification */
9460 pages->page_list[p++] = SVPD_DEVICE_ID;
9461 /* Extended INQUIRY Data */
9462 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9463 /* Mode Page Policy */
9464 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9466 pages->page_list[p++] = SVPD_SCSI_PORTS;
9467 /* Third-party Copy */
9468 pages->page_list[p++] = SVPD_SCSI_TPC;
9469 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9471 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9472 /* Block Device Characteristics */
9473 pages->page_list[p++] = SVPD_BDC;
9474 /* Logical Block Provisioning */
9475 pages->page_list[p++] = SVPD_LBP;
9479 ctl_set_success(ctsio);
9480 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9481 ctsio->be_move_done = ctl_config_move_done;
9482 ctl_datamove((union ctl_io *)ctsio);
9483 return (CTL_RETVAL_COMPLETE);
9487 * SCSI VPD page 0x80, the Unit Serial Number page.
9490 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9492 struct scsi_vpd_unit_serial_number *sn_ptr;
9493 struct ctl_lun *lun;
9496 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9498 data_len = 4 + CTL_SN_LEN;
9499 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9500 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9501 if (data_len < alloc_len) {
9502 ctsio->residual = alloc_len - data_len;
9503 ctsio->kern_data_len = data_len;
9504 ctsio->kern_total_len = data_len;
9506 ctsio->residual = 0;
9507 ctsio->kern_data_len = alloc_len;
9508 ctsio->kern_total_len = alloc_len;
9510 ctsio->kern_data_resid = 0;
9511 ctsio->kern_rel_offset = 0;
9512 ctsio->kern_sg_entries = 0;
9515 * The control device is always connected. The disk device, on the
9516 * other hand, may not be online all the time. Need to change this
9517 * to figure out whether the disk device is actually online or not.
9520 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9521 lun->be_lun->lun_type;
9523 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9525 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9526 sn_ptr->length = CTL_SN_LEN;
9528 * If we don't have a LUN, we just leave the serial number as
9532 strncpy((char *)sn_ptr->serial_num,
9533 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9535 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9537 ctl_set_success(ctsio);
9538 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9539 ctsio->be_move_done = ctl_config_move_done;
9540 ctl_datamove((union ctl_io *)ctsio);
9541 return (CTL_RETVAL_COMPLETE);
9546 * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9549 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9551 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9552 struct ctl_lun *lun;
9555 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9557 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9558 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9559 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9560 ctsio->kern_sg_entries = 0;
9562 if (data_len < alloc_len) {
9563 ctsio->residual = alloc_len - data_len;
9564 ctsio->kern_data_len = data_len;
9565 ctsio->kern_total_len = data_len;
9567 ctsio->residual = 0;
9568 ctsio->kern_data_len = alloc_len;
9569 ctsio->kern_total_len = alloc_len;
9571 ctsio->kern_data_resid = 0;
9572 ctsio->kern_rel_offset = 0;
9573 ctsio->kern_sg_entries = 0;
9576 * The control device is always connected. The disk device, on the
9577 * other hand, may not be online all the time.
9580 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9581 lun->be_lun->lun_type;
9583 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9584 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9585 scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9587 * We support head of queue, ordered and simple tags.
9589 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9591 * Volatile cache supported.
9593 eid_ptr->flags3 = SVPD_EID_V_SUP;
9596 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9597 * attention for a particular IT nexus on all LUNs once we report
9598 * it to that nexus once. This bit is required as of SPC-4.
9600 eid_ptr->flags4 = SVPD_EID_LUICLT;
9603 * XXX KDM in order to correctly answer this, we would need
9604 * information from the SIM to determine how much sense data it
9605 * can send. So this would really be a path inquiry field, most
9606 * likely. This can be set to a maximum of 252 according to SPC-4,
9607 * but the hardware may or may not be able to support that much.
9608 * 0 just means that the maximum sense data length is not reported.
9610 eid_ptr->max_sense_length = 0;
9612 ctl_set_success(ctsio);
9613 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9614 ctsio->be_move_done = ctl_config_move_done;
9615 ctl_datamove((union ctl_io *)ctsio);
9616 return (CTL_RETVAL_COMPLETE);
9620 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9622 struct scsi_vpd_mode_page_policy *mpp_ptr;
9623 struct ctl_lun *lun;
9626 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9628 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9629 sizeof(struct scsi_vpd_mode_page_policy_descr);
9631 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9632 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9633 ctsio->kern_sg_entries = 0;
9635 if (data_len < alloc_len) {
9636 ctsio->residual = alloc_len - data_len;
9637 ctsio->kern_data_len = data_len;
9638 ctsio->kern_total_len = data_len;
9640 ctsio->residual = 0;
9641 ctsio->kern_data_len = alloc_len;
9642 ctsio->kern_total_len = alloc_len;
9644 ctsio->kern_data_resid = 0;
9645 ctsio->kern_rel_offset = 0;
9646 ctsio->kern_sg_entries = 0;
9649 * The control device is always connected. The disk device, on the
9650 * other hand, may not be online all the time.
9653 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9654 lun->be_lun->lun_type;
9656 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9657 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9658 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9659 mpp_ptr->descr[0].page_code = 0x3f;
9660 mpp_ptr->descr[0].subpage_code = 0xff;
9661 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9663 ctl_set_success(ctsio);
9664 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9665 ctsio->be_move_done = ctl_config_move_done;
9666 ctl_datamove((union ctl_io *)ctsio);
9667 return (CTL_RETVAL_COMPLETE);
9671 * SCSI VPD page 0x83, the Device Identification page.
9674 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9676 struct scsi_vpd_device_id *devid_ptr;
9677 struct scsi_vpd_id_descriptor *desc;
9678 struct ctl_softc *softc;
9679 struct ctl_lun *lun;
9680 struct ctl_port *port;
9684 softc = control_softc;
9686 port = ctl_io_port(&ctsio->io_hdr);
9687 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9689 data_len = sizeof(struct scsi_vpd_device_id) +
9690 sizeof(struct scsi_vpd_id_descriptor) +
9691 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9692 sizeof(struct scsi_vpd_id_descriptor) +
9693 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9694 if (lun && lun->lun_devid)
9695 data_len += lun->lun_devid->len;
9696 if (port && port->port_devid)
9697 data_len += port->port_devid->len;
9698 if (port && port->target_devid)
9699 data_len += port->target_devid->len;
9701 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9702 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9703 ctsio->kern_sg_entries = 0;
9705 if (data_len < alloc_len) {
9706 ctsio->residual = alloc_len - data_len;
9707 ctsio->kern_data_len = data_len;
9708 ctsio->kern_total_len = data_len;
9710 ctsio->residual = 0;
9711 ctsio->kern_data_len = alloc_len;
9712 ctsio->kern_total_len = alloc_len;
9714 ctsio->kern_data_resid = 0;
9715 ctsio->kern_rel_offset = 0;
9716 ctsio->kern_sg_entries = 0;
9719 * The control device is always connected. The disk device, on the
9720 * other hand, may not be online all the time.
9723 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9724 lun->be_lun->lun_type;
9726 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9727 devid_ptr->page_code = SVPD_DEVICE_ID;
9728 scsi_ulto2b(data_len - 4, devid_ptr->length);
9730 if (port && port->port_type == CTL_PORT_FC)
9731 proto = SCSI_PROTO_FC << 4;
9732 else if (port && port->port_type == CTL_PORT_ISCSI)
9733 proto = SCSI_PROTO_ISCSI << 4;
9735 proto = SCSI_PROTO_SPI << 4;
9736 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9739 * We're using a LUN association here. i.e., this device ID is a
9740 * per-LUN identifier.
9742 if (lun && lun->lun_devid) {
9743 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9744 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9745 lun->lun_devid->len);
9749 * This is for the WWPN which is a port association.
9751 if (port && port->port_devid) {
9752 memcpy(desc, port->port_devid->data, port->port_devid->len);
9753 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9754 port->port_devid->len);
9758 * This is for the Relative Target Port(type 4h) identifier
9760 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9761 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9762 SVPD_ID_TYPE_RELTARG;
9764 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9765 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9766 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9769 * This is for the Target Port Group(type 5h) identifier
9771 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9772 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9773 SVPD_ID_TYPE_TPORTGRP;
9775 if (softc->is_single ||
9776 (port && port->status & CTL_PORT_STATUS_HA_SHARED))
9779 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt;
9780 scsi_ulto2b(g, &desc->identifier[2]);
9781 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9782 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9785 * This is for the Target identifier
9787 if (port && port->target_devid) {
9788 memcpy(desc, port->target_devid->data, port->target_devid->len);
9791 ctl_set_success(ctsio);
9792 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9793 ctsio->be_move_done = ctl_config_move_done;
9794 ctl_datamove((union ctl_io *)ctsio);
9795 return (CTL_RETVAL_COMPLETE);
9799 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9801 struct ctl_softc *softc = control_softc;
9802 struct scsi_vpd_scsi_ports *sp;
9803 struct scsi_vpd_port_designation *pd;
9804 struct scsi_vpd_port_designation_cont *pdc;
9805 struct ctl_lun *lun;
9806 struct ctl_port *port;
9807 int data_len, num_target_ports, iid_len, id_len;
9809 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9811 num_target_ports = 0;
9814 mtx_lock(&softc->ctl_lock);
9815 STAILQ_FOREACH(port, &softc->port_list, links) {
9816 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9819 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
9822 if (port->init_devid)
9823 iid_len += port->init_devid->len;
9824 if (port->port_devid)
9825 id_len += port->port_devid->len;
9827 mtx_unlock(&softc->ctl_lock);
9829 data_len = sizeof(struct scsi_vpd_scsi_ports) +
9830 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9831 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9832 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9833 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9834 ctsio->kern_sg_entries = 0;
9836 if (data_len < alloc_len) {
9837 ctsio->residual = alloc_len - data_len;
9838 ctsio->kern_data_len = data_len;
9839 ctsio->kern_total_len = data_len;
9841 ctsio->residual = 0;
9842 ctsio->kern_data_len = alloc_len;
9843 ctsio->kern_total_len = alloc_len;
9845 ctsio->kern_data_resid = 0;
9846 ctsio->kern_rel_offset = 0;
9847 ctsio->kern_sg_entries = 0;
9850 * The control device is always connected. The disk device, on the
9851 * other hand, may not be online all the time. Need to change this
9852 * to figure out whether the disk device is actually online or not.
9855 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9856 lun->be_lun->lun_type;
9858 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9860 sp->page_code = SVPD_SCSI_PORTS;
9861 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9863 pd = &sp->design[0];
9865 mtx_lock(&softc->ctl_lock);
9866 STAILQ_FOREACH(port, &softc->port_list, links) {
9867 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9870 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
9872 scsi_ulto2b(port->targ_port, pd->relative_port_id);
9873 if (port->init_devid) {
9874 iid_len = port->init_devid->len;
9875 memcpy(pd->initiator_transportid,
9876 port->init_devid->data, port->init_devid->len);
9879 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9880 pdc = (struct scsi_vpd_port_designation_cont *)
9881 (&pd->initiator_transportid[iid_len]);
9882 if (port->port_devid) {
9883 id_len = port->port_devid->len;
9884 memcpy(pdc->target_port_descriptors,
9885 port->port_devid->data, port->port_devid->len);
9888 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9889 pd = (struct scsi_vpd_port_designation *)
9890 ((uint8_t *)pdc->target_port_descriptors + id_len);
9892 mtx_unlock(&softc->ctl_lock);
9894 ctl_set_success(ctsio);
9895 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9896 ctsio->be_move_done = ctl_config_move_done;
9897 ctl_datamove((union ctl_io *)ctsio);
9898 return (CTL_RETVAL_COMPLETE);
9902 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9904 struct scsi_vpd_block_limits *bl_ptr;
9905 struct ctl_lun *lun;
9907 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9909 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9910 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9911 ctsio->kern_sg_entries = 0;
9913 if (sizeof(*bl_ptr) < alloc_len) {
9914 ctsio->residual = alloc_len - sizeof(*bl_ptr);
9915 ctsio->kern_data_len = sizeof(*bl_ptr);
9916 ctsio->kern_total_len = sizeof(*bl_ptr);
9918 ctsio->residual = 0;
9919 ctsio->kern_data_len = alloc_len;
9920 ctsio->kern_total_len = alloc_len;
9922 ctsio->kern_data_resid = 0;
9923 ctsio->kern_rel_offset = 0;
9924 ctsio->kern_sg_entries = 0;
9927 * The control device is always connected. The disk device, on the
9928 * other hand, may not be online all the time. Need to change this
9929 * to figure out whether the disk device is actually online or not.
9932 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9933 lun->be_lun->lun_type;
9935 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9937 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9938 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
9939 bl_ptr->max_cmp_write_len = 0xff;
9940 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9942 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
9943 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9944 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
9945 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
9946 if (lun->be_lun->ublockexp != 0) {
9947 scsi_ulto4b((1 << lun->be_lun->ublockexp),
9948 bl_ptr->opt_unmap_grain);
9949 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
9950 bl_ptr->unmap_grain_align);
9953 scsi_ulto4b(lun->be_lun->atomicblock,
9954 bl_ptr->max_atomic_transfer_length);
9955 scsi_ulto4b(0, bl_ptr->atomic_alignment);
9956 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
9957 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary);
9958 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size);
9960 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
9962 ctl_set_success(ctsio);
9963 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9964 ctsio->be_move_done = ctl_config_move_done;
9965 ctl_datamove((union ctl_io *)ctsio);
9966 return (CTL_RETVAL_COMPLETE);
9970 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
9972 struct scsi_vpd_block_device_characteristics *bdc_ptr;
9973 struct ctl_lun *lun;
9977 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9979 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
9980 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
9981 ctsio->kern_sg_entries = 0;
9983 if (sizeof(*bdc_ptr) < alloc_len) {
9984 ctsio->residual = alloc_len - sizeof(*bdc_ptr);
9985 ctsio->kern_data_len = sizeof(*bdc_ptr);
9986 ctsio->kern_total_len = sizeof(*bdc_ptr);
9988 ctsio->residual = 0;
9989 ctsio->kern_data_len = alloc_len;
9990 ctsio->kern_total_len = alloc_len;
9992 ctsio->kern_data_resid = 0;
9993 ctsio->kern_rel_offset = 0;
9994 ctsio->kern_sg_entries = 0;
9997 * The control device is always connected. The disk device, on the
9998 * other hand, may not be online all the time. Need to change this
9999 * to figure out whether the disk device is actually online or not.
10002 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10003 lun->be_lun->lun_type;
10005 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10006 bdc_ptr->page_code = SVPD_BDC;
10007 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
10009 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL)
10010 i = strtol(value, NULL, 0);
10012 i = CTL_DEFAULT_ROTATION_RATE;
10013 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
10015 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL)
10016 i = strtol(value, NULL, 0);
10019 bdc_ptr->wab_wac_ff = (i & 0x0f);
10020 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
10022 ctl_set_success(ctsio);
10023 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10024 ctsio->be_move_done = ctl_config_move_done;
10025 ctl_datamove((union ctl_io *)ctsio);
10026 return (CTL_RETVAL_COMPLETE);
10030 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
10032 struct scsi_vpd_logical_block_prov *lbp_ptr;
10033 struct ctl_lun *lun;
10035 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10037 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
10038 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
10039 ctsio->kern_sg_entries = 0;
10041 if (sizeof(*lbp_ptr) < alloc_len) {
10042 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
10043 ctsio->kern_data_len = sizeof(*lbp_ptr);
10044 ctsio->kern_total_len = sizeof(*lbp_ptr);
10046 ctsio->residual = 0;
10047 ctsio->kern_data_len = alloc_len;
10048 ctsio->kern_total_len = alloc_len;
10050 ctsio->kern_data_resid = 0;
10051 ctsio->kern_rel_offset = 0;
10052 ctsio->kern_sg_entries = 0;
10055 * The control device is always connected. The disk device, on the
10056 * other hand, may not be online all the time. Need to change this
10057 * to figure out whether the disk device is actually online or not.
10060 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10061 lun->be_lun->lun_type;
10063 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10065 lbp_ptr->page_code = SVPD_LBP;
10066 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
10067 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
10068 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10069 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
10070 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
10071 lbp_ptr->prov_type = SVPD_LBP_THIN;
10074 ctl_set_success(ctsio);
10075 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10076 ctsio->be_move_done = ctl_config_move_done;
10077 ctl_datamove((union ctl_io *)ctsio);
10078 return (CTL_RETVAL_COMPLETE);
10082 * INQUIRY with the EVPD bit set.
10085 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
10087 struct ctl_lun *lun;
10088 struct scsi_inquiry *cdb;
10089 int alloc_len, retval;
10091 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10092 cdb = (struct scsi_inquiry *)ctsio->cdb;
10093 alloc_len = scsi_2btoul(cdb->length);
10095 switch (cdb->page_code) {
10096 case SVPD_SUPPORTED_PAGES:
10097 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
10099 case SVPD_UNIT_SERIAL_NUMBER:
10100 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
10102 case SVPD_DEVICE_ID:
10103 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
10105 case SVPD_EXTENDED_INQUIRY_DATA:
10106 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
10108 case SVPD_MODE_PAGE_POLICY:
10109 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
10111 case SVPD_SCSI_PORTS:
10112 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10114 case SVPD_SCSI_TPC:
10115 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10117 case SVPD_BLOCK_LIMITS:
10118 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10120 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10123 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10125 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10128 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10130 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10134 ctl_set_invalid_field(ctsio,
10140 ctl_done((union ctl_io *)ctsio);
10141 retval = CTL_RETVAL_COMPLETE;
10149 * Standard INQUIRY data.
10152 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10154 struct scsi_inquiry_data *inq_ptr;
10155 struct scsi_inquiry *cdb;
10156 struct ctl_softc *softc = control_softc;
10157 struct ctl_port *port;
10158 struct ctl_lun *lun;
10160 uint32_t alloc_len, data_len;
10161 ctl_port_type port_type;
10163 port = ctl_io_port(&ctsio->io_hdr);
10164 port_type = port->port_type;
10165 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10166 port_type = CTL_PORT_SCSI;
10168 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10169 cdb = (struct scsi_inquiry *)ctsio->cdb;
10170 alloc_len = scsi_2btoul(cdb->length);
10173 * We malloc the full inquiry data size here and fill it
10174 * in. If the user only asks for less, we'll give him
10177 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10178 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10179 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10180 ctsio->kern_sg_entries = 0;
10181 ctsio->kern_data_resid = 0;
10182 ctsio->kern_rel_offset = 0;
10184 if (data_len < alloc_len) {
10185 ctsio->residual = alloc_len - data_len;
10186 ctsio->kern_data_len = data_len;
10187 ctsio->kern_total_len = data_len;
10189 ctsio->residual = 0;
10190 ctsio->kern_data_len = alloc_len;
10191 ctsio->kern_total_len = alloc_len;
10195 if ((lun->flags & CTL_LUN_PRIMARY_SC) ||
10196 softc->ha_link >= CTL_HA_LINK_UNKNOWN) {
10197 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10198 lun->be_lun->lun_type;
10200 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) |
10201 lun->be_lun->lun_type;
10203 if (lun->flags & CTL_LUN_REMOVABLE)
10204 inq_ptr->dev_qual2 |= SID_RMB;
10206 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10208 /* RMB in byte 2 is 0 */
10209 inq_ptr->version = SCSI_REV_SPC4;
10212 * According to SAM-3, even if a device only supports a single
10213 * level of LUN addressing, it should still set the HISUP bit:
10215 * 4.9.1 Logical unit numbers overview
10217 * All logical unit number formats described in this standard are
10218 * hierarchical in structure even when only a single level in that
10219 * hierarchy is used. The HISUP bit shall be set to one in the
10220 * standard INQUIRY data (see SPC-2) when any logical unit number
10221 * format described in this standard is used. Non-hierarchical
10222 * formats are outside the scope of this standard.
10224 * Therefore we set the HiSup bit here.
10226 * The response format is 2, per SPC-3.
10228 inq_ptr->response_format = SID_HiSup | 2;
10230 inq_ptr->additional_length = data_len -
10231 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10232 CTL_DEBUG_PRINT(("additional_length = %d\n",
10233 inq_ptr->additional_length));
10235 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10236 if (port_type == CTL_PORT_SCSI)
10237 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10238 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10239 inq_ptr->flags = SID_CmdQue;
10240 if (port_type == CTL_PORT_SCSI)
10241 inq_ptr->flags |= SID_WBus16 | SID_Sync;
10244 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10245 * We have 8 bytes for the vendor name, and 16 bytes for the device
10246 * name and 4 bytes for the revision.
10248 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10249 "vendor")) == NULL) {
10250 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10252 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10253 strncpy(inq_ptr->vendor, val,
10254 min(sizeof(inq_ptr->vendor), strlen(val)));
10257 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10258 sizeof(inq_ptr->product));
10259 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10260 switch (lun->be_lun->lun_type) {
10262 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10263 sizeof(inq_ptr->product));
10266 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10267 sizeof(inq_ptr->product));
10270 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT,
10271 sizeof(inq_ptr->product));
10274 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10275 sizeof(inq_ptr->product));
10279 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10280 strncpy(inq_ptr->product, val,
10281 min(sizeof(inq_ptr->product), strlen(val)));
10285 * XXX make this a macro somewhere so it automatically gets
10286 * incremented when we make changes.
10288 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10289 "revision")) == NULL) {
10290 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10292 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10293 strncpy(inq_ptr->revision, val,
10294 min(sizeof(inq_ptr->revision), strlen(val)));
10298 * For parallel SCSI, we support double transition and single
10299 * transition clocking. We also support QAS (Quick Arbitration
10300 * and Selection) and Information Unit transfers on both the
10301 * control and array devices.
10303 if (port_type == CTL_PORT_SCSI)
10304 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10307 /* SAM-5 (no version claimed) */
10308 scsi_ulto2b(0x00A0, inq_ptr->version1);
10309 /* SPC-4 (no version claimed) */
10310 scsi_ulto2b(0x0460, inq_ptr->version2);
10311 if (port_type == CTL_PORT_FC) {
10312 /* FCP-2 ANSI INCITS.350:2003 */
10313 scsi_ulto2b(0x0917, inq_ptr->version3);
10314 } else if (port_type == CTL_PORT_SCSI) {
10315 /* SPI-4 ANSI INCITS.362:200x */
10316 scsi_ulto2b(0x0B56, inq_ptr->version3);
10317 } else if (port_type == CTL_PORT_ISCSI) {
10318 /* iSCSI (no version claimed) */
10319 scsi_ulto2b(0x0960, inq_ptr->version3);
10320 } else if (port_type == CTL_PORT_SAS) {
10321 /* SAS (no version claimed) */
10322 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10326 /* SBC-4 (no version claimed) */
10327 scsi_ulto2b(0x0600, inq_ptr->version4);
10329 switch (lun->be_lun->lun_type) {
10331 /* SBC-4 (no version claimed) */
10332 scsi_ulto2b(0x0600, inq_ptr->version4);
10337 /* MMC-6 (no version claimed) */
10338 scsi_ulto2b(0x04E0, inq_ptr->version4);
10345 ctl_set_success(ctsio);
10346 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10347 ctsio->be_move_done = ctl_config_move_done;
10348 ctl_datamove((union ctl_io *)ctsio);
10349 return (CTL_RETVAL_COMPLETE);
10353 ctl_inquiry(struct ctl_scsiio *ctsio)
10355 struct scsi_inquiry *cdb;
10358 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10360 cdb = (struct scsi_inquiry *)ctsio->cdb;
10361 if (cdb->byte2 & SI_EVPD)
10362 retval = ctl_inquiry_evpd(ctsio);
10363 else if (cdb->page_code == 0)
10364 retval = ctl_inquiry_std(ctsio);
10366 ctl_set_invalid_field(ctsio,
10372 ctl_done((union ctl_io *)ctsio);
10373 return (CTL_RETVAL_COMPLETE);
10380 ctl_get_config(struct ctl_scsiio *ctsio)
10382 struct scsi_get_config_header *hdr;
10383 struct scsi_get_config_feature *feature;
10384 struct scsi_get_config *cdb;
10385 struct ctl_lun *lun;
10386 uint32_t alloc_len, data_len;
10389 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10390 cdb = (struct scsi_get_config *)ctsio->cdb;
10391 rt = (cdb->rt & SGC_RT_MASK);
10392 starting = scsi_2btoul(cdb->starting_feature);
10393 alloc_len = scsi_2btoul(cdb->length);
10395 data_len = sizeof(struct scsi_get_config_header) +
10396 sizeof(struct scsi_get_config_feature) + 8 +
10397 sizeof(struct scsi_get_config_feature) + 8 +
10398 sizeof(struct scsi_get_config_feature) + 4 +
10399 sizeof(struct scsi_get_config_feature) + 4 +
10400 sizeof(struct scsi_get_config_feature) + 8 +
10401 sizeof(struct scsi_get_config_feature) +
10402 sizeof(struct scsi_get_config_feature) + 4 +
10403 sizeof(struct scsi_get_config_feature) + 4 +
10404 sizeof(struct scsi_get_config_feature) + 4 +
10405 sizeof(struct scsi_get_config_feature) + 4 +
10406 sizeof(struct scsi_get_config_feature) + 4 +
10407 sizeof(struct scsi_get_config_feature) + 4;
10408 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10409 ctsio->kern_sg_entries = 0;
10410 ctsio->kern_data_resid = 0;
10411 ctsio->kern_rel_offset = 0;
10413 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr;
10414 if (lun->flags & CTL_LUN_NO_MEDIA)
10415 scsi_ulto2b(0x0000, hdr->current_profile);
10417 scsi_ulto2b(0x0010, hdr->current_profile);
10418 feature = (struct scsi_get_config_feature *)(hdr + 1);
10420 if (starting > 0x003b)
10422 if (starting > 0x003a)
10424 if (starting > 0x002b)
10426 if (starting > 0x002a)
10428 if (starting > 0x001f)
10430 if (starting > 0x001e)
10432 if (starting > 0x001d)
10434 if (starting > 0x0010)
10436 if (starting > 0x0003)
10438 if (starting > 0x0002)
10440 if (starting > 0x0001)
10442 if (starting > 0x0000)
10446 scsi_ulto2b(0x0000, feature->feature_code);
10447 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT;
10448 feature->add_length = 8;
10449 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */
10450 feature->feature_data[2] = 0x00;
10451 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */
10452 feature->feature_data[6] = 0x01;
10453 feature = (struct scsi_get_config_feature *)
10454 &feature->feature_data[feature->add_length];
10457 scsi_ulto2b(0x0001, feature->feature_code);
10458 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10459 feature->add_length = 8;
10460 scsi_ulto4b(0x00000000, &feature->feature_data[0]);
10461 feature->feature_data[4] = 0x03;
10462 feature = (struct scsi_get_config_feature *)
10463 &feature->feature_data[feature->add_length];
10466 scsi_ulto2b(0x0002, feature->feature_code);
10467 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10468 feature->add_length = 4;
10469 feature->feature_data[0] = 0x02;
10470 feature = (struct scsi_get_config_feature *)
10471 &feature->feature_data[feature->add_length];
10473 f3: /* Removable Medium */
10474 scsi_ulto2b(0x0003, feature->feature_code);
10475 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10476 feature->add_length = 4;
10477 feature->feature_data[0] = 0x39;
10478 feature = (struct scsi_get_config_feature *)
10479 &feature->feature_data[feature->add_length];
10481 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA))
10484 f10: /* Random Read */
10485 scsi_ulto2b(0x0010, feature->feature_code);
10486 feature->flags = 0x00;
10487 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10488 feature->flags |= SGC_F_CURRENT;
10489 feature->add_length = 8;
10490 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]);
10491 scsi_ulto2b(1, &feature->feature_data[4]);
10492 feature->feature_data[6] = 0x00;
10493 feature = (struct scsi_get_config_feature *)
10494 &feature->feature_data[feature->add_length];
10496 f1d: /* Multi-Read */
10497 scsi_ulto2b(0x001D, feature->feature_code);
10498 feature->flags = 0x00;
10499 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10500 feature->flags |= SGC_F_CURRENT;
10501 feature->add_length = 0;
10502 feature = (struct scsi_get_config_feature *)
10503 &feature->feature_data[feature->add_length];
10506 scsi_ulto2b(0x001E, feature->feature_code);
10507 feature->flags = 0x00;
10508 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10509 feature->flags |= SGC_F_CURRENT;
10510 feature->add_length = 4;
10511 feature->feature_data[0] = 0x00;
10512 feature = (struct scsi_get_config_feature *)
10513 &feature->feature_data[feature->add_length];
10515 f1f: /* DVD Read */
10516 scsi_ulto2b(0x001F, feature->feature_code);
10517 feature->flags = 0x08;
10518 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10519 feature->flags |= SGC_F_CURRENT;
10520 feature->add_length = 4;
10521 feature->feature_data[0] = 0x01;
10522 feature->feature_data[2] = 0x03;
10523 feature = (struct scsi_get_config_feature *)
10524 &feature->feature_data[feature->add_length];
10527 scsi_ulto2b(0x002A, feature->feature_code);
10528 feature->flags = 0x04;
10529 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10530 feature->flags |= SGC_F_CURRENT;
10531 feature->add_length = 4;
10532 feature->feature_data[0] = 0x00;
10533 feature->feature_data[1] = 0x00;
10534 feature = (struct scsi_get_config_feature *)
10535 &feature->feature_data[feature->add_length];
10538 scsi_ulto2b(0x002B, feature->feature_code);
10539 feature->flags = 0x00;
10540 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10541 feature->flags |= SGC_F_CURRENT;
10542 feature->add_length = 4;
10543 feature->feature_data[0] = 0x00;
10544 feature = (struct scsi_get_config_feature *)
10545 &feature->feature_data[feature->add_length];
10547 f3a: /* DVD+RW Dual Layer */
10548 scsi_ulto2b(0x003A, feature->feature_code);
10549 feature->flags = 0x00;
10550 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10551 feature->flags |= SGC_F_CURRENT;
10552 feature->add_length = 4;
10553 feature->feature_data[0] = 0x00;
10554 feature->feature_data[1] = 0x00;
10555 feature = (struct scsi_get_config_feature *)
10556 &feature->feature_data[feature->add_length];
10558 f3b: /* DVD+R Dual Layer */
10559 scsi_ulto2b(0x003B, feature->feature_code);
10560 feature->flags = 0x00;
10561 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10562 feature->flags |= SGC_F_CURRENT;
10563 feature->add_length = 4;
10564 feature->feature_data[0] = 0x00;
10565 feature = (struct scsi_get_config_feature *)
10566 &feature->feature_data[feature->add_length];
10569 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10570 if (rt == SGC_RT_SPECIFIC && data_len > 4) {
10571 feature = (struct scsi_get_config_feature *)(hdr + 1);
10572 if (scsi_2btoul(feature->feature_code) == starting)
10573 feature = (struct scsi_get_config_feature *)
10574 &feature->feature_data[feature->add_length];
10575 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10577 scsi_ulto4b(data_len - 4, hdr->data_length);
10578 if (data_len < alloc_len) {
10579 ctsio->residual = alloc_len - data_len;
10580 ctsio->kern_data_len = data_len;
10581 ctsio->kern_total_len = data_len;
10583 ctsio->residual = 0;
10584 ctsio->kern_data_len = alloc_len;
10585 ctsio->kern_total_len = alloc_len;
10588 ctl_set_success(ctsio);
10589 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10590 ctsio->be_move_done = ctl_config_move_done;
10591 ctl_datamove((union ctl_io *)ctsio);
10592 return (CTL_RETVAL_COMPLETE);
10596 ctl_get_event_status(struct ctl_scsiio *ctsio)
10598 struct scsi_get_event_status_header *hdr;
10599 struct scsi_get_event_status *cdb;
10600 struct ctl_lun *lun;
10601 uint32_t alloc_len, data_len;
10604 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10605 cdb = (struct scsi_get_event_status *)ctsio->cdb;
10606 if ((cdb->byte2 & SGESN_POLLED) == 0) {
10607 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
10608 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
10609 ctl_done((union ctl_io *)ctsio);
10610 return (CTL_RETVAL_COMPLETE);
10612 notif_class = cdb->notif_class;
10613 alloc_len = scsi_2btoul(cdb->length);
10615 data_len = sizeof(struct scsi_get_event_status_header);
10616 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10617 ctsio->kern_sg_entries = 0;
10618 ctsio->kern_data_resid = 0;
10619 ctsio->kern_rel_offset = 0;
10621 if (data_len < alloc_len) {
10622 ctsio->residual = alloc_len - data_len;
10623 ctsio->kern_data_len = data_len;
10624 ctsio->kern_total_len = data_len;
10626 ctsio->residual = 0;
10627 ctsio->kern_data_len = alloc_len;
10628 ctsio->kern_total_len = alloc_len;
10631 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr;
10632 scsi_ulto2b(0, hdr->descr_length);
10633 hdr->nea_class = SGESN_NEA;
10634 hdr->supported_class = 0;
10636 ctl_set_success(ctsio);
10637 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10638 ctsio->be_move_done = ctl_config_move_done;
10639 ctl_datamove((union ctl_io *)ctsio);
10640 return (CTL_RETVAL_COMPLETE);
10644 ctl_mechanism_status(struct ctl_scsiio *ctsio)
10646 struct scsi_mechanism_status_header *hdr;
10647 struct scsi_mechanism_status *cdb;
10648 struct ctl_lun *lun;
10649 uint32_t alloc_len, data_len;
10651 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10652 cdb = (struct scsi_mechanism_status *)ctsio->cdb;
10653 alloc_len = scsi_2btoul(cdb->length);
10655 data_len = sizeof(struct scsi_mechanism_status_header);
10656 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10657 ctsio->kern_sg_entries = 0;
10658 ctsio->kern_data_resid = 0;
10659 ctsio->kern_rel_offset = 0;
10661 if (data_len < alloc_len) {
10662 ctsio->residual = alloc_len - data_len;
10663 ctsio->kern_data_len = data_len;
10664 ctsio->kern_total_len = data_len;
10666 ctsio->residual = 0;
10667 ctsio->kern_data_len = alloc_len;
10668 ctsio->kern_total_len = alloc_len;
10671 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr;
10672 hdr->state1 = 0x00;
10673 hdr->state2 = 0xe0;
10674 scsi_ulto3b(0, hdr->lba);
10675 hdr->slots_num = 0;
10676 scsi_ulto2b(0, hdr->slots_length);
10678 ctl_set_success(ctsio);
10679 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10680 ctsio->be_move_done = ctl_config_move_done;
10681 ctl_datamove((union ctl_io *)ctsio);
10682 return (CTL_RETVAL_COMPLETE);
10686 ctl_ultomsf(uint32_t lba, uint8_t *buf)
10691 buf[1] = bin2bcd((lba / 75) / 60);
10692 buf[2] = bin2bcd((lba / 75) % 60);
10693 buf[3] = bin2bcd(lba % 75);
10697 ctl_read_toc(struct ctl_scsiio *ctsio)
10699 struct scsi_read_toc_hdr *hdr;
10700 struct scsi_read_toc_type01_descr *descr;
10701 struct scsi_read_toc *cdb;
10702 struct ctl_lun *lun;
10703 uint32_t alloc_len, data_len;
10706 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10707 cdb = (struct scsi_read_toc *)ctsio->cdb;
10708 msf = (cdb->byte2 & CD_MSF) != 0;
10709 format = cdb->format;
10710 alloc_len = scsi_2btoul(cdb->data_len);
10712 data_len = sizeof(struct scsi_read_toc_hdr);
10714 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr);
10716 data_len += sizeof(struct scsi_read_toc_type01_descr);
10717 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10718 ctsio->kern_sg_entries = 0;
10719 ctsio->kern_data_resid = 0;
10720 ctsio->kern_rel_offset = 0;
10722 if (data_len < alloc_len) {
10723 ctsio->residual = alloc_len - data_len;
10724 ctsio->kern_data_len = data_len;
10725 ctsio->kern_total_len = data_len;
10727 ctsio->residual = 0;
10728 ctsio->kern_data_len = alloc_len;
10729 ctsio->kern_total_len = alloc_len;
10732 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr;
10734 scsi_ulto2b(0x12, hdr->data_length);
10737 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10738 descr->addr_ctl = 0x14;
10739 descr->track_number = 1;
10741 ctl_ultomsf(0, descr->track_start);
10743 scsi_ulto4b(0, descr->track_start);
10745 descr->addr_ctl = 0x14;
10746 descr->track_number = 0xaa;
10748 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start);
10750 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start);
10752 scsi_ulto2b(0x0a, hdr->data_length);
10755 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10756 descr->addr_ctl = 0x14;
10757 descr->track_number = 1;
10759 ctl_ultomsf(0, descr->track_start);
10761 scsi_ulto4b(0, descr->track_start);
10764 ctl_set_success(ctsio);
10765 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10766 ctsio->be_move_done = ctl_config_move_done;
10767 ctl_datamove((union ctl_io *)ctsio);
10768 return (CTL_RETVAL_COMPLETE);
10772 * For known CDB types, parse the LBA and length.
10775 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10777 if (io->io_hdr.io_type != CTL_IO_SCSI)
10780 switch (io->scsiio.cdb[0]) {
10781 case COMPARE_AND_WRITE: {
10782 struct scsi_compare_and_write *cdb;
10784 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10786 *lba = scsi_8btou64(cdb->addr);
10787 *len = cdb->length;
10792 struct scsi_rw_6 *cdb;
10794 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10796 *lba = scsi_3btoul(cdb->addr);
10797 /* only 5 bits are valid in the most significant address byte */
10799 *len = cdb->length;
10804 struct scsi_rw_10 *cdb;
10806 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10808 *lba = scsi_4btoul(cdb->addr);
10809 *len = scsi_2btoul(cdb->length);
10812 case WRITE_VERIFY_10: {
10813 struct scsi_write_verify_10 *cdb;
10815 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10817 *lba = scsi_4btoul(cdb->addr);
10818 *len = scsi_2btoul(cdb->length);
10823 struct scsi_rw_12 *cdb;
10825 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10827 *lba = scsi_4btoul(cdb->addr);
10828 *len = scsi_4btoul(cdb->length);
10831 case WRITE_VERIFY_12: {
10832 struct scsi_write_verify_12 *cdb;
10834 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10836 *lba = scsi_4btoul(cdb->addr);
10837 *len = scsi_4btoul(cdb->length);
10842 struct scsi_rw_16 *cdb;
10844 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10846 *lba = scsi_8btou64(cdb->addr);
10847 *len = scsi_4btoul(cdb->length);
10850 case WRITE_ATOMIC_16: {
10851 struct scsi_write_atomic_16 *cdb;
10853 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb;
10855 *lba = scsi_8btou64(cdb->addr);
10856 *len = scsi_2btoul(cdb->length);
10859 case WRITE_VERIFY_16: {
10860 struct scsi_write_verify_16 *cdb;
10862 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10864 *lba = scsi_8btou64(cdb->addr);
10865 *len = scsi_4btoul(cdb->length);
10868 case WRITE_SAME_10: {
10869 struct scsi_write_same_10 *cdb;
10871 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10873 *lba = scsi_4btoul(cdb->addr);
10874 *len = scsi_2btoul(cdb->length);
10877 case WRITE_SAME_16: {
10878 struct scsi_write_same_16 *cdb;
10880 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10882 *lba = scsi_8btou64(cdb->addr);
10883 *len = scsi_4btoul(cdb->length);
10887 struct scsi_verify_10 *cdb;
10889 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10891 *lba = scsi_4btoul(cdb->addr);
10892 *len = scsi_2btoul(cdb->length);
10896 struct scsi_verify_12 *cdb;
10898 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10900 *lba = scsi_4btoul(cdb->addr);
10901 *len = scsi_4btoul(cdb->length);
10905 struct scsi_verify_16 *cdb;
10907 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10909 *lba = scsi_8btou64(cdb->addr);
10910 *len = scsi_4btoul(cdb->length);
10918 case SERVICE_ACTION_IN: { /* GET LBA STATUS */
10919 struct scsi_get_lba_status *cdb;
10921 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10922 *lba = scsi_8btou64(cdb->addr);
10928 break; /* NOTREACHED */
10935 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10938 uint64_t endlba1, endlba2;
10940 endlba1 = lba1 + len1 - (seq ? 0 : 1);
10941 endlba2 = lba2 + len2 - 1;
10943 if ((endlba1 < lba2) || (endlba2 < lba1))
10944 return (CTL_ACTION_PASS);
10946 return (CTL_ACTION_BLOCK);
10950 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10952 struct ctl_ptr_len_flags *ptrlen;
10953 struct scsi_unmap_desc *buf, *end, *range;
10957 /* If not UNMAP -- go other way. */
10958 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10959 io->scsiio.cdb[0] != UNMAP)
10960 return (CTL_ACTION_ERROR);
10962 /* If UNMAP without data -- block and wait for data. */
10963 ptrlen = (struct ctl_ptr_len_flags *)
10964 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10965 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10966 ptrlen->ptr == NULL)
10967 return (CTL_ACTION_BLOCK);
10969 /* UNMAP with data -- check for collision. */
10970 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10971 end = buf + ptrlen->len / sizeof(*buf);
10972 for (range = buf; range < end; range++) {
10973 lba = scsi_8btou64(range->lba);
10974 len = scsi_4btoul(range->length);
10975 if ((lba < lba2 + len2) && (lba + len > lba2))
10976 return (CTL_ACTION_BLOCK);
10978 return (CTL_ACTION_PASS);
10982 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10984 uint64_t lba1, lba2;
10985 uint64_t len1, len2;
10988 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10989 return (CTL_ACTION_ERROR);
10991 retval = ctl_extent_check_unmap(io1, lba2, len2);
10992 if (retval != CTL_ACTION_ERROR)
10995 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10996 return (CTL_ACTION_ERROR);
10998 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
11000 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
11004 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2)
11006 uint64_t lba1, lba2;
11007 uint64_t len1, len2;
11009 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
11010 return (CTL_ACTION_PASS);
11011 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
11012 return (CTL_ACTION_ERROR);
11013 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
11014 return (CTL_ACTION_ERROR);
11016 if (lba1 + len1 == lba2)
11017 return (CTL_ACTION_BLOCK);
11018 return (CTL_ACTION_PASS);
11022 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
11023 union ctl_io *ooa_io)
11025 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
11026 const ctl_serialize_action *serialize_row;
11029 * The initiator attempted multiple untagged commands at the same
11030 * time. Can't do that.
11032 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11033 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11034 && ((pending_io->io_hdr.nexus.targ_port ==
11035 ooa_io->io_hdr.nexus.targ_port)
11036 && (pending_io->io_hdr.nexus.initid ==
11037 ooa_io->io_hdr.nexus.initid))
11038 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
11039 CTL_FLAG_STATUS_SENT)) == 0))
11040 return (CTL_ACTION_OVERLAP);
11043 * The initiator attempted to send multiple tagged commands with
11044 * the same ID. (It's fine if different initiators have the same
11047 * Even if all of those conditions are true, we don't kill the I/O
11048 * if the command ahead of us has been aborted. We won't end up
11049 * sending it to the FETD, and it's perfectly legal to resend a
11050 * command with the same tag number as long as the previous
11051 * instance of this tag number has been aborted somehow.
11053 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
11054 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
11055 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
11056 && ((pending_io->io_hdr.nexus.targ_port ==
11057 ooa_io->io_hdr.nexus.targ_port)
11058 && (pending_io->io_hdr.nexus.initid ==
11059 ooa_io->io_hdr.nexus.initid))
11060 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
11061 CTL_FLAG_STATUS_SENT)) == 0))
11062 return (CTL_ACTION_OVERLAP_TAG);
11065 * If we get a head of queue tag, SAM-3 says that we should
11066 * immediately execute it.
11068 * What happens if this command would normally block for some other
11069 * reason? e.g. a request sense with a head of queue tag
11070 * immediately after a write. Normally that would block, but this
11071 * will result in its getting executed immediately...
11073 * We currently return "pass" instead of "skip", so we'll end up
11074 * going through the rest of the queue to check for overlapped tags.
11076 * XXX KDM check for other types of blockage first??
11078 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
11079 return (CTL_ACTION_PASS);
11082 * Ordered tags have to block until all items ahead of them
11083 * have completed. If we get called with an ordered tag, we always
11084 * block, if something else is ahead of us in the queue.
11086 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
11087 return (CTL_ACTION_BLOCK);
11090 * Simple tags get blocked until all head of queue and ordered tags
11091 * ahead of them have completed. I'm lumping untagged commands in
11092 * with simple tags here. XXX KDM is that the right thing to do?
11094 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11095 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
11096 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
11097 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
11098 return (CTL_ACTION_BLOCK);
11100 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
11101 KASSERT(pending_entry->seridx < CTL_SERIDX_COUNT,
11102 ("%s: Invalid seridx %d for pending CDB %02x %02x @ %p",
11103 __func__, pending_entry->seridx, pending_io->scsiio.cdb[0],
11104 pending_io->scsiio.cdb[1], pending_io));
11105 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
11106 if (ooa_entry->seridx == CTL_SERIDX_INVLD)
11107 return (CTL_ACTION_PASS); /* Unsupported command in OOA queue */
11108 KASSERT(ooa_entry->seridx < CTL_SERIDX_COUNT,
11109 ("%s: Invalid seridx %d for ooa CDB %02x %02x @ %p",
11110 __func__, ooa_entry->seridx, ooa_io->scsiio.cdb[0],
11111 ooa_io->scsiio.cdb[1], ooa_io));
11113 serialize_row = ctl_serialize_table[ooa_entry->seridx];
11115 switch (serialize_row[pending_entry->seridx]) {
11116 case CTL_SER_BLOCK:
11117 return (CTL_ACTION_BLOCK);
11118 case CTL_SER_EXTENT:
11119 return (ctl_extent_check(ooa_io, pending_io,
11120 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
11121 case CTL_SER_EXTENTOPT:
11122 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
11123 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
11124 return (ctl_extent_check(ooa_io, pending_io,
11126 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
11127 return (CTL_ACTION_PASS);
11128 case CTL_SER_EXTENTSEQ:
11129 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF)
11130 return (ctl_extent_check_seq(ooa_io, pending_io));
11131 return (CTL_ACTION_PASS);
11133 return (CTL_ACTION_PASS);
11134 case CTL_SER_BLOCKOPT:
11135 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
11136 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
11137 return (CTL_ACTION_BLOCK);
11138 return (CTL_ACTION_PASS);
11140 return (CTL_ACTION_SKIP);
11142 panic("%s: Invalid serialization value %d for %d => %d",
11143 __func__, serialize_row[pending_entry->seridx],
11144 pending_entry->seridx, ooa_entry->seridx);
11147 return (CTL_ACTION_ERROR);
11151 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
11153 * - pending_io is generally either incoming, or on the blocked queue
11154 * - starting I/O is the I/O we want to start the check with.
11157 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
11158 union ctl_io *starting_io)
11160 union ctl_io *ooa_io;
11163 mtx_assert(&lun->lun_lock, MA_OWNED);
11166 * Run back along the OOA queue, starting with the current
11167 * blocked I/O and going through every I/O before it on the
11168 * queue. If starting_io is NULL, we'll just end up returning
11171 for (ooa_io = starting_io; ooa_io != NULL;
11172 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
11176 * This routine just checks to see whether
11177 * cur_blocked is blocked by ooa_io, which is ahead
11178 * of it in the queue. It doesn't queue/dequeue
11181 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
11183 case CTL_ACTION_BLOCK:
11184 case CTL_ACTION_OVERLAP:
11185 case CTL_ACTION_OVERLAP_TAG:
11186 case CTL_ACTION_SKIP:
11187 case CTL_ACTION_ERROR:
11189 break; /* NOTREACHED */
11190 case CTL_ACTION_PASS:
11193 panic("%s: Invalid action %d\n", __func__, action);
11197 return (CTL_ACTION_PASS);
11202 * - An I/O has just completed, and has been removed from the per-LUN OOA
11203 * queue, so some items on the blocked queue may now be unblocked.
11206 ctl_check_blocked(struct ctl_lun *lun)
11208 struct ctl_softc *softc = lun->ctl_softc;
11209 union ctl_io *cur_blocked, *next_blocked;
11211 mtx_assert(&lun->lun_lock, MA_OWNED);
11214 * Run forward from the head of the blocked queue, checking each
11215 * entry against the I/Os prior to it on the OOA queue to see if
11216 * there is still any blockage.
11218 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
11219 * with our removing a variable on it while it is traversing the
11222 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
11223 cur_blocked != NULL; cur_blocked = next_blocked) {
11224 union ctl_io *prev_ooa;
11227 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
11230 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
11231 ctl_ooaq, ooa_links);
11234 * If cur_blocked happens to be the first item in the OOA
11235 * queue now, prev_ooa will be NULL, and the action
11236 * returned will just be CTL_ACTION_PASS.
11238 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
11241 case CTL_ACTION_BLOCK:
11242 /* Nothing to do here, still blocked */
11244 case CTL_ACTION_OVERLAP:
11245 case CTL_ACTION_OVERLAP_TAG:
11247 * This shouldn't happen! In theory we've already
11248 * checked this command for overlap...
11251 case CTL_ACTION_PASS:
11252 case CTL_ACTION_SKIP: {
11253 const struct ctl_cmd_entry *entry;
11256 * The skip case shouldn't happen, this transaction
11257 * should have never made it onto the blocked queue.
11260 * This I/O is no longer blocked, we can remove it
11261 * from the blocked queue. Since this is a TAILQ
11262 * (doubly linked list), we can do O(1) removals
11263 * from any place on the list.
11265 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
11267 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11269 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
11270 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){
11272 * Need to send IO back to original side to
11275 union ctl_ha_msg msg_info;
11277 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11278 msg_info.hdr.original_sc =
11279 cur_blocked->io_hdr.original_sc;
11280 msg_info.hdr.serializing_sc = cur_blocked;
11281 msg_info.hdr.msg_type = CTL_MSG_R2R;
11282 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11283 sizeof(msg_info.hdr), M_NOWAIT);
11286 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
11289 * Check this I/O for LUN state changes that may
11290 * have happened while this command was blocked.
11291 * The LUN state may have been changed by a command
11292 * ahead of us in the queue, so we need to re-check
11293 * for any states that can be caused by SCSI
11296 if (ctl_scsiio_lun_check(lun, entry,
11297 &cur_blocked->scsiio) == 0) {
11298 cur_blocked->io_hdr.flags |=
11299 CTL_FLAG_IS_WAS_ON_RTR;
11300 ctl_enqueue_rtr(cur_blocked);
11302 ctl_done(cur_blocked);
11307 * This probably shouldn't happen -- we shouldn't
11308 * get CTL_ACTION_ERROR, or anything else.
11314 return (CTL_RETVAL_COMPLETE);
11318 * This routine (with one exception) checks LUN flags that can be set by
11319 * commands ahead of us in the OOA queue. These flags have to be checked
11320 * when a command initially comes in, and when we pull a command off the
11321 * blocked queue and are preparing to execute it. The reason we have to
11322 * check these flags for commands on the blocked queue is that the LUN
11323 * state may have been changed by a command ahead of us while we're on the
11326 * Ordering is somewhat important with these checks, so please pay
11327 * careful attention to the placement of any new checks.
11330 ctl_scsiio_lun_check(struct ctl_lun *lun,
11331 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11333 struct ctl_softc *softc = lun->ctl_softc;
11339 mtx_assert(&lun->lun_lock, MA_OWNED);
11342 * If this shelf is a secondary shelf controller, we may have to
11343 * reject some commands disallowed by HA mode and link state.
11345 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11346 if (softc->ha_link == CTL_HA_LINK_OFFLINE &&
11347 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11348 ctl_set_lun_unavail(ctsio);
11352 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 &&
11353 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11354 ctl_set_lun_transit(ctsio);
11358 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY &&
11359 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) {
11360 ctl_set_lun_standby(ctsio);
11365 /* The rest of checks are only done on executing side */
11366 if (softc->ha_mode == CTL_HA_MODE_XFER)
11370 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11372 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) {
11373 ctl_set_hw_write_protected(ctsio);
11377 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT]
11378 .eca_and_aen & SCP_SWP) != 0) {
11379 ctl_set_sense(ctsio, /*current_error*/ 1,
11380 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11381 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11388 * Check for a reservation conflict. If this command isn't allowed
11389 * even on reserved LUNs, and if this initiator isn't the one who
11390 * reserved us, reject the command with a reservation conflict.
11392 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11393 if ((lun->flags & CTL_LUN_RESERVED)
11394 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11395 if (lun->res_idx != residx) {
11396 ctl_set_reservation_conflict(ctsio);
11402 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11403 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11404 /* No reservation or command is allowed. */;
11405 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11406 (lun->pr_res_type == SPR_TYPE_WR_EX ||
11407 lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
11408 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) {
11409 /* The command is allowed for Write Exclusive resv. */;
11412 * if we aren't registered or it's a res holder type
11413 * reservation and this isn't the res holder then set a
11416 if (ctl_get_prkey(lun, residx) == 0 ||
11417 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) {
11418 ctl_set_reservation_conflict(ctsio);
11424 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) {
11425 if (lun->flags & CTL_LUN_EJECTED)
11426 ctl_set_lun_ejected(ctsio);
11427 else if (lun->flags & CTL_LUN_NO_MEDIA) {
11428 if (lun->flags & CTL_LUN_REMOVABLE)
11429 ctl_set_lun_no_media(ctsio);
11431 ctl_set_lun_int_reqd(ctsio);
11432 } else if (lun->flags & CTL_LUN_STOPPED)
11433 ctl_set_lun_stopped(ctsio);
11445 ctl_failover_io(union ctl_io *io, int have_lock)
11447 ctl_set_busy(&io->scsiio);
11452 ctl_failover_lun(union ctl_io *rio)
11454 struct ctl_softc *softc = control_softc;
11455 struct ctl_lun *lun;
11456 struct ctl_io_hdr *io, *next_io;
11459 targ_lun = rio->io_hdr.nexus.targ_mapped_lun;
11460 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun));
11462 /* Find and lock the LUN. */
11463 mtx_lock(&softc->ctl_lock);
11464 if ((targ_lun < CTL_MAX_LUNS) &&
11465 ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
11466 mtx_lock(&lun->lun_lock);
11467 mtx_unlock(&softc->ctl_lock);
11468 if (lun->flags & CTL_LUN_DISABLED) {
11469 mtx_unlock(&lun->lun_lock);
11473 mtx_unlock(&softc->ctl_lock);
11477 if (softc->ha_mode == CTL_HA_MODE_XFER) {
11478 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11479 /* We are master */
11480 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11481 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11482 io->flags |= CTL_FLAG_ABORT;
11483 io->flags |= CTL_FLAG_FAILOVER;
11484 } else { /* This can be only due to DATAMOVE */
11485 io->msg_type = CTL_MSG_DATAMOVE_DONE;
11486 io->flags &= ~CTL_FLAG_DMA_INPROG;
11487 io->flags |= CTL_FLAG_IO_ACTIVE;
11488 io->port_status = 31340;
11489 ctl_enqueue_isc((union ctl_io *)io);
11493 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11494 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11495 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11496 io->flags |= CTL_FLAG_FAILOVER;
11498 ctl_set_busy(&((union ctl_io *)io)->
11500 ctl_done((union ctl_io *)io);
11504 } else { /* SERIALIZE modes */
11505 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links,
11507 /* We are master */
11508 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11509 TAILQ_REMOVE(&lun->blocked_queue, io,
11511 io->flags &= ~CTL_FLAG_BLOCKED;
11512 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11513 ctl_free_io((union ctl_io *)io);
11516 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11517 /* We are master */
11518 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11519 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11520 ctl_free_io((union ctl_io *)io);
11523 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11524 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11525 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) {
11526 ctl_set_busy(&((union ctl_io *)io)->
11528 ctl_done((union ctl_io *)io);
11532 ctl_check_blocked(lun);
11534 mtx_unlock(&lun->lun_lock);
11538 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio)
11540 struct ctl_lun *lun;
11541 const struct ctl_cmd_entry *entry;
11542 uint32_t initidx, targ_lun;
11549 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11550 if ((targ_lun < CTL_MAX_LUNS)
11551 && ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
11553 * If the LUN is invalid, pretend that it doesn't exist.
11554 * It will go away as soon as all pending I/O has been
11557 mtx_lock(&lun->lun_lock);
11558 if (lun->flags & CTL_LUN_DISABLED) {
11559 mtx_unlock(&lun->lun_lock);
11561 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11562 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11564 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
11565 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
11569 * Every I/O goes into the OOA queue for a
11570 * particular LUN, and stays there until completion.
11573 if (TAILQ_EMPTY(&lun->ooa_queue)) {
11574 lun->idle_time += getsbinuptime() -
11578 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr,
11582 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11583 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11586 /* Get command entry and return error if it is unsuppotyed. */
11587 entry = ctl_validate_command(ctsio);
11588 if (entry == NULL) {
11590 mtx_unlock(&lun->lun_lock);
11594 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11595 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11598 * Check to see whether we can send this command to LUNs that don't
11599 * exist. This should pretty much only be the case for inquiry
11600 * and request sense. Further checks, below, really require having
11601 * a LUN, so we can't really check the command anymore. Just put
11602 * it on the rtr queue.
11605 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) {
11606 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11607 ctl_enqueue_rtr((union ctl_io *)ctsio);
11611 ctl_set_unsupported_lun(ctsio);
11612 ctl_done((union ctl_io *)ctsio);
11613 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11617 * Make sure we support this particular command on this LUN.
11618 * e.g., we don't support writes to the control LUN.
11620 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11621 mtx_unlock(&lun->lun_lock);
11622 ctl_set_invalid_opcode(ctsio);
11623 ctl_done((union ctl_io *)ctsio);
11628 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11632 * If we've got a request sense, it'll clear the contingent
11633 * allegiance condition. Otherwise, if we have a CA condition for
11634 * this initiator, clear it, because it sent down a command other
11635 * than request sense.
11637 if ((ctsio->cdb[0] != REQUEST_SENSE)
11638 && (ctl_is_set(lun->have_ca, initidx)))
11639 ctl_clear_mask(lun->have_ca, initidx);
11643 * If the command has this flag set, it handles its own unit
11644 * attention reporting, we shouldn't do anything. Otherwise we
11645 * check for any pending unit attentions, and send them back to the
11646 * initiator. We only do this when a command initially comes in,
11647 * not when we pull it off the blocked queue.
11649 * According to SAM-3, section 5.3.2, the order that things get
11650 * presented back to the host is basically unit attentions caused
11651 * by some sort of reset event, busy status, reservation conflicts
11652 * or task set full, and finally any other status.
11654 * One issue here is that some of the unit attentions we report
11655 * don't fall into the "reset" category (e.g. "reported luns data
11656 * has changed"). So reporting it here, before the reservation
11657 * check, may be technically wrong. I guess the only thing to do
11658 * would be to check for and report the reset events here, and then
11659 * check for the other unit attention types after we check for a
11660 * reservation conflict.
11662 * XXX KDM need to fix this
11664 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11665 ctl_ua_type ua_type;
11667 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11669 if (ua_type != CTL_UA_NONE) {
11670 mtx_unlock(&lun->lun_lock);
11671 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11672 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11673 ctsio->sense_len = SSD_FULL_SIZE;
11674 ctl_done((union ctl_io *)ctsio);
11680 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11681 mtx_unlock(&lun->lun_lock);
11682 ctl_done((union ctl_io *)ctsio);
11687 * XXX CHD this is where we want to send IO to other side if
11688 * this LUN is secondary on this SC. We will need to make a copy
11689 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11690 * the copy we send as FROM_OTHER.
11691 * We also need to stuff the address of the original IO so we can
11692 * find it easily. Something similar will need be done on the other
11693 * side so when we are done we can find the copy.
11695 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
11696 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 &&
11697 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) {
11698 union ctl_ha_msg msg_info;
11701 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11702 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11703 mtx_unlock(&lun->lun_lock);
11705 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11706 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11707 msg_info.hdr.serializing_sc = NULL;
11708 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11709 msg_info.scsi.tag_num = ctsio->tag_num;
11710 msg_info.scsi.tag_type = ctsio->tag_type;
11711 msg_info.scsi.cdb_len = ctsio->cdb_len;
11712 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11714 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11715 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data),
11716 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) {
11717 ctl_set_busy(ctsio);
11718 ctl_done((union ctl_io *)ctsio);
11724 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11725 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11726 ctl_ooaq, ooa_links))) {
11727 case CTL_ACTION_BLOCK:
11728 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11729 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11731 mtx_unlock(&lun->lun_lock);
11733 case CTL_ACTION_PASS:
11734 case CTL_ACTION_SKIP:
11735 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11736 mtx_unlock(&lun->lun_lock);
11737 ctl_enqueue_rtr((union ctl_io *)ctsio);
11739 case CTL_ACTION_OVERLAP:
11740 mtx_unlock(&lun->lun_lock);
11741 ctl_set_overlapped_cmd(ctsio);
11742 ctl_done((union ctl_io *)ctsio);
11744 case CTL_ACTION_OVERLAP_TAG:
11745 mtx_unlock(&lun->lun_lock);
11746 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11747 ctl_done((union ctl_io *)ctsio);
11749 case CTL_ACTION_ERROR:
11751 mtx_unlock(&lun->lun_lock);
11752 ctl_set_internal_failure(ctsio,
11754 /*retry_count*/ 0);
11755 ctl_done((union ctl_io *)ctsio);
11761 const struct ctl_cmd_entry *
11762 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11764 const struct ctl_cmd_entry *entry;
11765 int service_action;
11767 entry = &ctl_cmd_table[ctsio->cdb[0]];
11769 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11770 if (entry->flags & CTL_CMD_FLAG_SA5) {
11771 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11772 entry = &((const struct ctl_cmd_entry *)
11773 entry->execute)[service_action];
11778 const struct ctl_cmd_entry *
11779 ctl_validate_command(struct ctl_scsiio *ctsio)
11781 const struct ctl_cmd_entry *entry;
11785 entry = ctl_get_cmd_entry(ctsio, &sa);
11786 if (entry->execute == NULL) {
11788 ctl_set_invalid_field(ctsio,
11795 ctl_set_invalid_opcode(ctsio);
11796 ctl_done((union ctl_io *)ctsio);
11799 KASSERT(entry->length > 0,
11800 ("Not defined length for command 0x%02x/0x%02x",
11801 ctsio->cdb[0], ctsio->cdb[1]));
11802 for (i = 1; i < entry->length; i++) {
11803 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11806 ctl_set_invalid_field(ctsio,
11811 /*bit*/ fls(diff) - 1);
11812 ctl_done((union ctl_io *)ctsio);
11819 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11822 switch (lun_type) {
11824 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0)
11828 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
11832 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0)
11842 ctl_scsiio(struct ctl_scsiio *ctsio)
11845 const struct ctl_cmd_entry *entry;
11847 retval = CTL_RETVAL_COMPLETE;
11849 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11851 entry = ctl_get_cmd_entry(ctsio, NULL);
11854 * If this I/O has been aborted, just send it straight to
11855 * ctl_done() without executing it.
11857 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11858 ctl_done((union ctl_io *)ctsio);
11863 * All the checks should have been handled by ctl_scsiio_precheck().
11864 * We should be clear now to just execute the I/O.
11866 retval = entry->execute(ctsio);
11873 * Since we only implement one target right now, a bus reset simply resets
11874 * our single target.
11877 ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io)
11879 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET));
11883 ctl_target_reset(struct ctl_softc *softc, union ctl_io *io,
11884 ctl_ua_type ua_type)
11886 struct ctl_port *port;
11887 struct ctl_lun *lun;
11890 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11891 union ctl_ha_msg msg_info;
11893 msg_info.hdr.nexus = io->io_hdr.nexus;
11894 if (ua_type==CTL_UA_TARG_RESET)
11895 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11897 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11898 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11899 msg_info.hdr.original_sc = NULL;
11900 msg_info.hdr.serializing_sc = NULL;
11901 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11902 sizeof(msg_info.task), M_WAITOK);
11906 mtx_lock(&softc->ctl_lock);
11907 port = ctl_io_port(&io->io_hdr);
11908 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11909 if (port != NULL &&
11910 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
11912 retval += ctl_do_lun_reset(lun, io, ua_type);
11914 mtx_unlock(&softc->ctl_lock);
11915 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11920 * The LUN should always be set. The I/O is optional, and is used to
11921 * distinguish between I/Os sent by this initiator, and by other
11922 * initiators. We set unit attention for initiators other than this one.
11923 * SAM-3 is vague on this point. It does say that a unit attention should
11924 * be established for other initiators when a LUN is reset (see section
11925 * 5.7.3), but it doesn't specifically say that the unit attention should
11926 * be established for this particular initiator when a LUN is reset. Here
11927 * is the relevant text, from SAM-3 rev 8:
11929 * 5.7.2 When a SCSI initiator port aborts its own tasks
11931 * When a SCSI initiator port causes its own task(s) to be aborted, no
11932 * notification that the task(s) have been aborted shall be returned to
11933 * the SCSI initiator port other than the completion response for the
11934 * command or task management function action that caused the task(s) to
11935 * be aborted and notification(s) associated with related effects of the
11936 * action (e.g., a reset unit attention condition).
11938 * XXX KDM for now, we're setting unit attention for all initiators.
11941 ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11949 mtx_lock(&lun->lun_lock);
11951 * Run through the OOA queue and abort each I/O.
11953 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11954 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11955 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11959 * This version sets unit attention for every
11962 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11963 ctl_est_ua_all(lun, initidx, ua_type);
11965 ctl_est_ua_all(lun, -1, ua_type);
11969 * A reset (any kind, really) clears reservations established with
11970 * RESERVE/RELEASE. It does not clear reservations established
11971 * with PERSISTENT RESERVE OUT, but we don't support that at the
11972 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
11973 * reservations made with the RESERVE/RELEASE commands, because
11974 * those commands are obsolete in SPC-3.
11976 lun->flags &= ~CTL_LUN_RESERVED;
11979 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11980 ctl_clear_mask(lun->have_ca, i);
11982 lun->prevent_count = 0;
11983 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11984 ctl_clear_mask(lun->prevent, i);
11985 mtx_unlock(&lun->lun_lock);
11991 ctl_lun_reset(struct ctl_softc *softc, union ctl_io *io)
11993 struct ctl_lun *lun;
11997 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11998 mtx_lock(&softc->ctl_lock);
11999 if ((targ_lun >= CTL_MAX_LUNS) ||
12000 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12001 mtx_unlock(&softc->ctl_lock);
12002 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12005 retval = ctl_do_lun_reset(lun, io, CTL_UA_LUN_RESET);
12006 mtx_unlock(&softc->ctl_lock);
12007 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12009 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) {
12010 union ctl_ha_msg msg_info;
12012 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12013 msg_info.hdr.nexus = io->io_hdr.nexus;
12014 msg_info.task.task_action = CTL_TASK_LUN_RESET;
12015 msg_info.hdr.original_sc = NULL;
12016 msg_info.hdr.serializing_sc = NULL;
12017 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12018 sizeof(msg_info.task), M_WAITOK);
12024 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
12029 mtx_assert(&lun->lun_lock, MA_OWNED);
12032 * Run through the OOA queue and attempt to find the given I/O.
12033 * The target port, initiator ID, tag type and tag number have to
12034 * match the values that we got from the initiator. If we have an
12035 * untagged command to abort, simply abort the first untagged command
12036 * we come to. We only allow one untagged command at a time of course.
12038 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12039 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12041 if ((targ_port == UINT32_MAX ||
12042 targ_port == xio->io_hdr.nexus.targ_port) &&
12043 (init_id == UINT32_MAX ||
12044 init_id == xio->io_hdr.nexus.initid)) {
12045 if (targ_port != xio->io_hdr.nexus.targ_port ||
12046 init_id != xio->io_hdr.nexus.initid)
12047 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
12048 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12049 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12050 union ctl_ha_msg msg_info;
12052 msg_info.hdr.nexus = xio->io_hdr.nexus;
12053 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12054 msg_info.task.tag_num = xio->scsiio.tag_num;
12055 msg_info.task.tag_type = xio->scsiio.tag_type;
12056 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12057 msg_info.hdr.original_sc = NULL;
12058 msg_info.hdr.serializing_sc = NULL;
12059 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12060 sizeof(msg_info.task), M_NOWAIT);
12067 ctl_abort_task_set(union ctl_io *io)
12069 struct ctl_softc *softc = control_softc;
12070 struct ctl_lun *lun;
12076 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12077 mtx_lock(&softc->ctl_lock);
12078 if ((targ_lun >= CTL_MAX_LUNS) ||
12079 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12080 mtx_unlock(&softc->ctl_lock);
12081 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12085 mtx_lock(&lun->lun_lock);
12086 mtx_unlock(&softc->ctl_lock);
12087 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
12088 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12089 io->io_hdr.nexus.initid,
12090 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12091 } else { /* CTL_TASK_CLEAR_TASK_SET */
12092 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
12093 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12095 mtx_unlock(&lun->lun_lock);
12096 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12101 ctl_i_t_nexus_reset(union ctl_io *io)
12103 struct ctl_softc *softc = control_softc;
12104 struct ctl_lun *lun;
12107 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12108 union ctl_ha_msg msg_info;
12110 msg_info.hdr.nexus = io->io_hdr.nexus;
12111 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET;
12112 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12113 msg_info.hdr.original_sc = NULL;
12114 msg_info.hdr.serializing_sc = NULL;
12115 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12116 sizeof(msg_info.task), M_WAITOK);
12119 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12120 mtx_lock(&softc->ctl_lock);
12121 STAILQ_FOREACH(lun, &softc->lun_list, links) {
12122 mtx_lock(&lun->lun_lock);
12123 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12124 io->io_hdr.nexus.initid, 1);
12126 ctl_clear_mask(lun->have_ca, initidx);
12128 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx))
12129 lun->flags &= ~CTL_LUN_RESERVED;
12130 if (ctl_is_set(lun->prevent, initidx)) {
12131 ctl_clear_mask(lun->prevent, initidx);
12132 lun->prevent_count--;
12134 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS);
12135 mtx_unlock(&lun->lun_lock);
12137 mtx_unlock(&softc->ctl_lock);
12138 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12143 ctl_abort_task(union ctl_io *io)
12146 struct ctl_lun *lun;
12147 struct ctl_softc *softc;
12150 char printbuf[128];
12155 softc = control_softc;
12161 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12162 mtx_lock(&softc->ctl_lock);
12163 if ((targ_lun >= CTL_MAX_LUNS) ||
12164 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12165 mtx_unlock(&softc->ctl_lock);
12166 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12171 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
12172 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
12175 mtx_lock(&lun->lun_lock);
12176 mtx_unlock(&softc->ctl_lock);
12178 * Run through the OOA queue and attempt to find the given I/O.
12179 * The target port, initiator ID, tag type and tag number have to
12180 * match the values that we got from the initiator. If we have an
12181 * untagged command to abort, simply abort the first untagged command
12182 * we come to. We only allow one untagged command at a time of course.
12184 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12185 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12187 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
12189 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
12190 lun->lun, xio->scsiio.tag_num,
12191 xio->scsiio.tag_type,
12192 (xio->io_hdr.blocked_links.tqe_prev
12193 == NULL) ? "" : " BLOCKED",
12194 (xio->io_hdr.flags &
12195 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
12196 (xio->io_hdr.flags &
12197 CTL_FLAG_ABORT) ? " ABORT" : "",
12198 (xio->io_hdr.flags &
12199 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
12200 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
12202 printf("%s\n", sbuf_data(&sb));
12205 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12206 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12207 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12211 * If the abort says that the task is untagged, the
12212 * task in the queue must be untagged. Otherwise,
12213 * we just check to see whether the tag numbers
12214 * match. This is because the QLogic firmware
12215 * doesn't pass back the tag type in an abort
12219 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12220 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12221 || (xio->scsiio.tag_num == io->taskio.tag_num))
12224 * XXX KDM we've got problems with FC, because it
12225 * doesn't send down a tag type with aborts. So we
12226 * can only really go by the tag number...
12227 * This may cause problems with parallel SCSI.
12228 * Need to figure that out!!
12230 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12231 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12233 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
12234 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12235 union ctl_ha_msg msg_info;
12237 msg_info.hdr.nexus = io->io_hdr.nexus;
12238 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12239 msg_info.task.tag_num = io->taskio.tag_num;
12240 msg_info.task.tag_type = io->taskio.tag_type;
12241 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12242 msg_info.hdr.original_sc = NULL;
12243 msg_info.hdr.serializing_sc = NULL;
12245 printf("Sent Abort to other side\n");
12247 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12248 sizeof(msg_info.task), M_NOWAIT);
12251 printf("ctl_abort_task: found I/O to abort\n");
12255 mtx_unlock(&lun->lun_lock);
12259 * This isn't really an error. It's entirely possible for
12260 * the abort and command completion to cross on the wire.
12261 * This is more of an informative/diagnostic error.
12264 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
12265 "%u:%u:%u tag %d type %d\n",
12266 io->io_hdr.nexus.initid,
12267 io->io_hdr.nexus.targ_port,
12268 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
12269 io->taskio.tag_type);
12272 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12277 ctl_query_task(union ctl_io *io, int task_set)
12280 struct ctl_lun *lun;
12281 struct ctl_softc *softc;
12285 softc = control_softc;
12286 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12287 mtx_lock(&softc->ctl_lock);
12288 if ((targ_lun >= CTL_MAX_LUNS) ||
12289 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12290 mtx_unlock(&softc->ctl_lock);
12291 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12294 mtx_lock(&lun->lun_lock);
12295 mtx_unlock(&softc->ctl_lock);
12296 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12297 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12299 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12300 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12301 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12304 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) {
12309 mtx_unlock(&lun->lun_lock);
12311 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12313 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12318 ctl_query_async_event(union ctl_io *io)
12320 struct ctl_lun *lun;
12321 struct ctl_softc *softc;
12323 uint32_t targ_lun, initidx;
12325 softc = control_softc;
12326 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12327 mtx_lock(&softc->ctl_lock);
12328 if ((targ_lun >= CTL_MAX_LUNS) ||
12329 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12330 mtx_unlock(&softc->ctl_lock);
12331 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12334 mtx_lock(&lun->lun_lock);
12335 mtx_unlock(&softc->ctl_lock);
12336 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12337 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp);
12338 mtx_unlock(&lun->lun_lock);
12339 if (ua != CTL_UA_NONE)
12340 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12342 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12347 ctl_run_task(union ctl_io *io)
12349 struct ctl_softc *softc = control_softc;
12352 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12353 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12354 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type));
12355 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED;
12356 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp));
12357 switch (io->taskio.task_action) {
12358 case CTL_TASK_ABORT_TASK:
12359 retval = ctl_abort_task(io);
12361 case CTL_TASK_ABORT_TASK_SET:
12362 case CTL_TASK_CLEAR_TASK_SET:
12363 retval = ctl_abort_task_set(io);
12365 case CTL_TASK_CLEAR_ACA:
12367 case CTL_TASK_I_T_NEXUS_RESET:
12368 retval = ctl_i_t_nexus_reset(io);
12370 case CTL_TASK_LUN_RESET:
12371 retval = ctl_lun_reset(softc, io);
12373 case CTL_TASK_TARGET_RESET:
12374 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET);
12376 case CTL_TASK_BUS_RESET:
12377 retval = ctl_bus_reset(softc, io);
12379 case CTL_TASK_PORT_LOGIN:
12381 case CTL_TASK_PORT_LOGOUT:
12383 case CTL_TASK_QUERY_TASK:
12384 retval = ctl_query_task(io, 0);
12386 case CTL_TASK_QUERY_TASK_SET:
12387 retval = ctl_query_task(io, 1);
12389 case CTL_TASK_QUERY_ASYNC_EVENT:
12390 retval = ctl_query_async_event(io);
12393 printf("%s: got unknown task management event %d\n",
12394 __func__, io->taskio.task_action);
12398 io->io_hdr.status = CTL_SUCCESS;
12400 io->io_hdr.status = CTL_ERROR;
12405 * For HA operation. Handle commands that come in from the other
12409 ctl_handle_isc(union ctl_io *io)
12412 struct ctl_lun *lun;
12413 struct ctl_softc *softc = control_softc;
12416 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12417 lun = softc->ctl_luns[targ_lun];
12419 switch (io->io_hdr.msg_type) {
12420 case CTL_MSG_SERIALIZE:
12421 free_io = ctl_serialize_other_sc_cmd(&io->scsiio);
12423 case CTL_MSG_R2R: {
12424 const struct ctl_cmd_entry *entry;
12427 * This is only used in SER_ONLY mode.
12430 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12431 mtx_lock(&lun->lun_lock);
12432 if (ctl_scsiio_lun_check(lun,
12433 entry, (struct ctl_scsiio *)io) != 0) {
12434 mtx_unlock(&lun->lun_lock);
12438 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12439 mtx_unlock(&lun->lun_lock);
12440 ctl_enqueue_rtr(io);
12443 case CTL_MSG_FINISH_IO:
12444 if (softc->ha_mode == CTL_HA_MODE_XFER) {
12449 mtx_lock(&lun->lun_lock);
12450 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
12452 ctl_check_blocked(lun);
12453 mtx_unlock(&lun->lun_lock);
12456 case CTL_MSG_PERS_ACTION:
12457 ctl_hndl_per_res_out_on_other_sc(
12458 (union ctl_ha_msg *)&io->presio.pr_msg);
12461 case CTL_MSG_BAD_JUJU:
12465 case CTL_MSG_DATAMOVE:
12466 /* Only used in XFER mode */
12468 ctl_datamove_remote(io);
12470 case CTL_MSG_DATAMOVE_DONE:
12471 /* Only used in XFER mode */
12473 io->scsiio.be_move_done(io);
12475 case CTL_MSG_FAILOVER:
12476 ctl_failover_lun(io);
12481 printf("%s: Invalid message type %d\n",
12482 __func__, io->io_hdr.msg_type);
12492 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12493 * there is no match.
12495 static ctl_lun_error_pattern
12496 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12498 const struct ctl_cmd_entry *entry;
12499 ctl_lun_error_pattern filtered_pattern, pattern;
12501 pattern = desc->error_pattern;
12504 * XXX KDM we need more data passed into this function to match a
12505 * custom pattern, and we actually need to implement custom pattern
12508 if (pattern & CTL_LUN_PAT_CMD)
12509 return (CTL_LUN_PAT_CMD);
12511 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12512 return (CTL_LUN_PAT_ANY);
12514 entry = ctl_get_cmd_entry(ctsio, NULL);
12516 filtered_pattern = entry->pattern & pattern;
12519 * If the user requested specific flags in the pattern (e.g.
12520 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12523 * If the user did not specify any flags, it doesn't matter whether
12524 * or not the command supports the flags.
12526 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12527 (pattern & ~CTL_LUN_PAT_MASK))
12528 return (CTL_LUN_PAT_NONE);
12531 * If the user asked for a range check, see if the requested LBA
12532 * range overlaps with this command's LBA range.
12534 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12540 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12542 return (CTL_LUN_PAT_NONE);
12544 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12545 desc->lba_range.len, FALSE);
12547 * A "pass" means that the LBA ranges don't overlap, so
12548 * this doesn't match the user's range criteria.
12550 if (action == CTL_ACTION_PASS)
12551 return (CTL_LUN_PAT_NONE);
12554 return (filtered_pattern);
12558 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12560 struct ctl_error_desc *desc, *desc2;
12562 mtx_assert(&lun->lun_lock, MA_OWNED);
12564 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12565 ctl_lun_error_pattern pattern;
12567 * Check to see whether this particular command matches
12568 * the pattern in the descriptor.
12570 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12571 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12574 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12575 case CTL_LUN_INJ_ABORTED:
12576 ctl_set_aborted(&io->scsiio);
12578 case CTL_LUN_INJ_MEDIUM_ERR:
12579 ctl_set_medium_error(&io->scsiio,
12580 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) !=
12581 CTL_FLAG_DATA_OUT);
12583 case CTL_LUN_INJ_UA:
12584 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12586 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12588 case CTL_LUN_INJ_CUSTOM:
12590 * We're assuming the user knows what he is doing.
12591 * Just copy the sense information without doing
12594 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12595 MIN(sizeof(desc->custom_sense),
12596 sizeof(io->scsiio.sense_data)));
12597 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12598 io->scsiio.sense_len = SSD_FULL_SIZE;
12599 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12601 case CTL_LUN_INJ_NONE:
12604 * If this is an error injection type we don't know
12605 * about, clear the continuous flag (if it is set)
12606 * so it will get deleted below.
12608 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12612 * By default, each error injection action is a one-shot
12614 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12617 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12623 #ifdef CTL_IO_DELAY
12625 ctl_datamove_timer_wakeup(void *arg)
12629 io = (union ctl_io *)arg;
12633 #endif /* CTL_IO_DELAY */
12636 ctl_datamove(union ctl_io *io)
12638 struct ctl_lun *lun;
12639 void (*fe_datamove)(union ctl_io *io);
12641 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12643 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12645 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12647 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12652 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12653 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12655 sbuf_cat(&sb, path_str);
12656 switch (io->io_hdr.io_type) {
12658 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12659 sbuf_printf(&sb, "\n");
12660 sbuf_cat(&sb, path_str);
12661 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12662 io->scsiio.tag_num, io->scsiio.tag_type);
12665 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12666 "Tag Type: %d\n", io->taskio.task_action,
12667 io->taskio.tag_num, io->taskio.tag_type);
12670 panic("%s: Invalid CTL I/O type %d\n",
12671 __func__, io->io_hdr.io_type);
12673 sbuf_cat(&sb, path_str);
12674 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12675 (intmax_t)time_uptime - io->io_hdr.start_time);
12677 printf("%s", sbuf_data(&sb));
12679 #endif /* CTL_TIME_IO */
12681 #ifdef CTL_IO_DELAY
12682 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12683 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12686 && (lun->delay_info.datamove_delay > 0)) {
12688 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
12689 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12690 callout_reset(&io->io_hdr.delay_callout,
12691 lun->delay_info.datamove_delay * hz,
12692 ctl_datamove_timer_wakeup, io);
12693 if (lun->delay_info.datamove_type ==
12694 CTL_DELAY_TYPE_ONESHOT)
12695 lun->delay_info.datamove_delay = 0;
12702 * This command has been aborted. Set the port status, so we fail
12705 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12706 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n",
12707 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12708 io->io_hdr.nexus.targ_port,
12709 io->io_hdr.nexus.targ_lun);
12710 io->io_hdr.port_status = 31337;
12712 * Note that the backend, in this case, will get the
12713 * callback in its context. In other cases it may get
12714 * called in the frontend's interrupt thread context.
12716 io->scsiio.be_move_done(io);
12720 /* Don't confuse frontend with zero length data move. */
12721 if (io->scsiio.kern_data_len == 0) {
12722 io->scsiio.be_move_done(io);
12726 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12731 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12733 union ctl_ha_msg msg;
12735 struct bintime cur_bt;
12738 memset(&msg, 0, sizeof(msg));
12739 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12740 msg.hdr.original_sc = io;
12741 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12742 msg.hdr.nexus = io->io_hdr.nexus;
12743 msg.hdr.status = io->io_hdr.status;
12744 msg.scsi.tag_num = io->scsiio.tag_num;
12745 msg.scsi.tag_type = io->scsiio.tag_type;
12746 msg.scsi.scsi_status = io->scsiio.scsi_status;
12747 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12748 io->scsiio.sense_len);
12749 msg.scsi.sense_len = io->scsiio.sense_len;
12750 msg.scsi.sense_residual = io->scsiio.sense_residual;
12751 msg.scsi.fetd_status = io->io_hdr.port_status;
12752 msg.scsi.residual = io->scsiio.residual;
12753 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12754 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12755 ctl_failover_io(io, /*have_lock*/ have_lock);
12758 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12759 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12760 msg.scsi.sense_len, M_WAITOK);
12763 getbinuptime(&cur_bt);
12764 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
12765 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
12767 io->io_hdr.num_dmas++;
12771 * The DMA to the remote side is done, now we need to tell the other side
12772 * we're done so it can continue with its data movement.
12775 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12782 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12783 printf("%s: ISC DMA write failed with error %d", __func__,
12785 ctl_set_internal_failure(&io->scsiio,
12787 /*retry_count*/ rq->ret);
12790 ctl_dt_req_free(rq);
12792 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12793 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12794 free(io->io_hdr.remote_sglist, M_CTL);
12795 io->io_hdr.remote_sglist = NULL;
12796 io->io_hdr.local_sglist = NULL;
12799 * The data is in local and remote memory, so now we need to send
12800 * status (good or back) back to the other side.
12802 ctl_send_datamove_done(io, /*have_lock*/ 0);
12806 * We've moved the data from the host/controller into local memory. Now we
12807 * need to push it over to the remote controller's memory.
12810 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12814 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12815 ctl_datamove_remote_write_cb);
12820 ctl_datamove_remote_write(union ctl_io *io)
12823 void (*fe_datamove)(union ctl_io *io);
12826 * - Get the data from the host/HBA into local memory.
12827 * - DMA memory from the local controller to the remote controller.
12828 * - Send status back to the remote controller.
12831 retval = ctl_datamove_remote_sgl_setup(io);
12835 /* Switch the pointer over so the FETD knows what to do */
12836 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12839 * Use a custom move done callback, since we need to send completion
12840 * back to the other controller, not to the backend on this side.
12842 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12844 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12849 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12858 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12859 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12860 free(io->io_hdr.remote_sglist, M_CTL);
12861 io->io_hdr.remote_sglist = NULL;
12862 io->io_hdr.local_sglist = NULL;
12865 scsi_path_string(io, path_str, sizeof(path_str));
12866 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12867 sbuf_cat(&sb, path_str);
12868 scsi_command_string(&io->scsiio, NULL, &sb);
12869 sbuf_printf(&sb, "\n");
12870 sbuf_cat(&sb, path_str);
12871 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12872 io->scsiio.tag_num, io->scsiio.tag_type);
12873 sbuf_cat(&sb, path_str);
12874 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12875 io->io_hdr.flags, io->io_hdr.status);
12877 printk("%s", sbuf_data(&sb));
12882 * The read is done, now we need to send status (good or bad) back
12883 * to the other side.
12885 ctl_send_datamove_done(io, /*have_lock*/ 0);
12891 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12894 void (*fe_datamove)(union ctl_io *io);
12898 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12899 printf("%s: ISC DMA read failed with error %d\n", __func__,
12901 ctl_set_internal_failure(&io->scsiio,
12903 /*retry_count*/ rq->ret);
12906 ctl_dt_req_free(rq);
12908 /* Switch the pointer over so the FETD knows what to do */
12909 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12912 * Use a custom move done callback, since we need to send completion
12913 * back to the other controller, not to the backend on this side.
12915 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12917 /* XXX KDM add checks like the ones in ctl_datamove? */
12919 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12924 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12926 struct ctl_sg_entry *local_sglist;
12927 uint32_t len_to_go;
12932 local_sglist = io->io_hdr.local_sglist;
12933 len_to_go = io->scsiio.kern_data_len;
12936 * The difficult thing here is that the size of the various
12937 * S/G segments may be different than the size from the
12938 * remote controller. That'll make it harder when DMAing
12939 * the data back to the other side.
12941 for (i = 0; len_to_go > 0; i++) {
12942 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT);
12943 local_sglist[i].addr =
12944 malloc(local_sglist[i].len, M_CTL, M_WAITOK);
12946 len_to_go -= local_sglist[i].len;
12949 * Reset the number of S/G entries accordingly. The original
12950 * number of S/G entries is available in rem_sg_entries.
12952 io->scsiio.kern_sg_entries = i;
12955 printf("%s: kern_sg_entries = %d\n", __func__,
12956 io->scsiio.kern_sg_entries);
12957 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12958 printf("%s: sg[%d] = %p, %lu\n", __func__, i,
12959 local_sglist[i].addr, local_sglist[i].len);
12966 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12967 ctl_ha_dt_cb callback)
12969 struct ctl_ha_dt_req *rq;
12970 struct ctl_sg_entry *remote_sglist, *local_sglist;
12971 uint32_t local_used, remote_used, total_used;
12974 rq = ctl_dt_req_alloc();
12977 * If we failed to allocate the request, and if the DMA didn't fail
12978 * anyway, set busy status. This is just a resource allocation
12982 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12983 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS))
12984 ctl_set_busy(&io->scsiio);
12986 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12987 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
12990 ctl_dt_req_free(rq);
12993 * The data move failed. We need to return status back
12994 * to the other controller. No point in trying to DMA
12995 * data to the remote controller.
12998 ctl_send_datamove_done(io, /*have_lock*/ 0);
13003 local_sglist = io->io_hdr.local_sglist;
13004 remote_sglist = io->io_hdr.remote_sglist;
13010 * Pull/push the data over the wire from/to the other controller.
13011 * This takes into account the possibility that the local and
13012 * remote sglists may not be identical in terms of the size of
13013 * the elements and the number of elements.
13015 * One fundamental assumption here is that the length allocated for
13016 * both the local and remote sglists is identical. Otherwise, we've
13017 * essentially got a coding error of some sort.
13019 isc_ret = CTL_HA_STATUS_SUCCESS;
13020 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
13024 rq->command = command;
13028 * Both pointers should be aligned. But it is possible
13029 * that the allocation length is not. They should both
13030 * also have enough slack left over at the end, though,
13031 * to round up to the next 8 byte boundary.
13033 cur_len = MIN(local_sglist[i].len - local_used,
13034 remote_sglist[j].len - remote_used);
13035 rq->size = cur_len;
13037 tmp_ptr = (uint8_t *)local_sglist[i].addr;
13038 tmp_ptr += local_used;
13041 /* Use physical addresses when talking to ISC hardware */
13042 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
13043 /* XXX KDM use busdma */
13044 rq->local = vtophys(tmp_ptr);
13046 rq->local = tmp_ptr;
13048 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
13049 ("HA does not support BUS_ADDR"));
13050 rq->local = tmp_ptr;
13053 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
13054 tmp_ptr += remote_used;
13055 rq->remote = tmp_ptr;
13057 rq->callback = NULL;
13059 local_used += cur_len;
13060 if (local_used >= local_sglist[i].len) {
13065 remote_used += cur_len;
13066 if (remote_used >= remote_sglist[j].len) {
13070 total_used += cur_len;
13072 if (total_used >= io->scsiio.kern_data_len)
13073 rq->callback = callback;
13076 printf("%s: %s: local %p remote %p size %d\n", __func__,
13077 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
13078 rq->local, rq->remote, rq->size);
13081 isc_ret = ctl_dt_single(rq);
13082 if (isc_ret > CTL_HA_STATUS_SUCCESS)
13085 if (isc_ret != CTL_HA_STATUS_WAIT) {
13094 ctl_datamove_remote_read(union ctl_io *io)
13100 * This will send an error to the other controller in the case of a
13103 retval = ctl_datamove_remote_sgl_setup(io);
13107 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
13108 ctl_datamove_remote_read_cb);
13111 * Make sure we free memory if there was an error.. The
13112 * ctl_datamove_remote_xfer() function will send the
13113 * datamove done message, or call the callback with an
13114 * error if there is a problem.
13116 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13117 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13118 free(io->io_hdr.remote_sglist, M_CTL);
13119 io->io_hdr.remote_sglist = NULL;
13120 io->io_hdr.local_sglist = NULL;
13125 * Process a datamove request from the other controller. This is used for
13126 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
13127 * first. Once that is complete, the data gets DMAed into the remote
13128 * controller's memory. For reads, we DMA from the remote controller's
13129 * memory into our memory first, and then move it out to the FETD.
13132 ctl_datamove_remote(union ctl_io *io)
13135 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
13137 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
13138 ctl_failover_io(io, /*have_lock*/ 0);
13143 * Note that we look for an aborted I/O here, but don't do some of
13144 * the other checks that ctl_datamove() normally does.
13145 * We don't need to run the datamove delay code, since that should
13146 * have been done if need be on the other controller.
13148 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
13149 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__,
13150 io->scsiio.tag_num, io->io_hdr.nexus.initid,
13151 io->io_hdr.nexus.targ_port,
13152 io->io_hdr.nexus.targ_lun);
13153 io->io_hdr.port_status = 31338;
13154 ctl_send_datamove_done(io, /*have_lock*/ 0);
13158 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
13159 ctl_datamove_remote_write(io);
13160 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
13161 ctl_datamove_remote_read(io);
13163 io->io_hdr.port_status = 31339;
13164 ctl_send_datamove_done(io, /*have_lock*/ 0);
13169 ctl_process_done(union ctl_io *io)
13171 struct ctl_lun *lun;
13172 struct ctl_softc *softc = control_softc;
13173 void (*fe_done)(union ctl_io *io);
13174 union ctl_ha_msg msg;
13175 uint32_t targ_port = io->io_hdr.nexus.targ_port;
13177 CTL_DEBUG_PRINT(("ctl_process_done\n"));
13178 fe_done = softc->ctl_ports[targ_port]->fe_done;
13181 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
13186 ctl_scsi_path_string(io, path_str, sizeof(path_str));
13187 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13189 sbuf_cat(&sb, path_str);
13190 switch (io->io_hdr.io_type) {
13192 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
13193 sbuf_printf(&sb, "\n");
13194 sbuf_cat(&sb, path_str);
13195 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13196 io->scsiio.tag_num, io->scsiio.tag_type);
13199 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
13200 "Tag Type: %d\n", io->taskio.task_action,
13201 io->taskio.tag_num, io->taskio.tag_type);
13204 panic("%s: Invalid CTL I/O type %d\n",
13205 __func__, io->io_hdr.io_type);
13207 sbuf_cat(&sb, path_str);
13208 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
13209 (intmax_t)time_uptime - io->io_hdr.start_time);
13211 printf("%s", sbuf_data(&sb));
13213 #endif /* CTL_TIME_IO */
13215 switch (io->io_hdr.io_type) {
13219 if (ctl_debug & CTL_DEBUG_INFO)
13220 ctl_io_error_print(io, NULL);
13224 panic("%s: Invalid CTL I/O type %d\n",
13225 __func__, io->io_hdr.io_type);
13228 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13230 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13231 io->io_hdr.nexus.targ_mapped_lun));
13235 mtx_lock(&lun->lun_lock);
13238 * Check to see if we have any errors to inject here. We only
13239 * inject errors for commands that don't already have errors set.
13241 if (!STAILQ_EMPTY(&lun->error_list) &&
13242 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
13243 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
13244 ctl_inject_error(lun, io);
13247 * XXX KDM how do we treat commands that aren't completed
13250 * XXX KDM should we also track I/O latency?
13252 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13253 io->io_hdr.io_type == CTL_IO_SCSI) {
13255 struct bintime cur_bt;
13259 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13261 type = CTL_STATS_READ;
13262 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13264 type = CTL_STATS_WRITE;
13266 type = CTL_STATS_NO_IO;
13268 lun->stats.ports[targ_port].bytes[type] +=
13269 io->scsiio.kern_total_len;
13270 lun->stats.ports[targ_port].operations[type]++;
13272 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
13273 &io->io_hdr.dma_bt);
13274 getbinuptime(&cur_bt);
13275 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
13276 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
13278 lun->stats.ports[targ_port].num_dmas[type] +=
13279 io->io_hdr.num_dmas;
13283 * Remove this from the OOA queue.
13285 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13287 if (TAILQ_EMPTY(&lun->ooa_queue))
13288 lun->last_busy = getsbinuptime();
13292 * Run through the blocked queue on this LUN and see if anything
13293 * has become unblocked, now that this transaction is done.
13295 ctl_check_blocked(lun);
13298 * If the LUN has been invalidated, free it if there is nothing
13299 * left on its OOA queue.
13301 if ((lun->flags & CTL_LUN_INVALID)
13302 && TAILQ_EMPTY(&lun->ooa_queue)) {
13303 mtx_unlock(&lun->lun_lock);
13304 mtx_lock(&softc->ctl_lock);
13306 mtx_unlock(&softc->ctl_lock);
13308 mtx_unlock(&lun->lun_lock);
13313 * If this command has been aborted, make sure we set the status
13314 * properly. The FETD is responsible for freeing the I/O and doing
13315 * whatever it needs to do to clean up its state.
13317 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13318 ctl_set_task_aborted(&io->scsiio);
13321 * If enabled, print command error status.
13323 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
13324 (ctl_debug & CTL_DEBUG_INFO) != 0)
13325 ctl_io_error_print(io, NULL);
13328 * Tell the FETD or the other shelf controller we're done with this
13329 * command. Note that only SCSI commands get to this point. Task
13330 * management commands are completed above.
13332 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
13333 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) {
13334 memset(&msg, 0, sizeof(msg));
13335 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13336 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
13337 msg.hdr.nexus = io->io_hdr.nexus;
13338 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13339 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data),
13348 * Front end should call this if it doesn't do autosense. When the request
13349 * sense comes back in from the initiator, we'll dequeue this and send it.
13352 ctl_queue_sense(union ctl_io *io)
13354 struct ctl_lun *lun;
13355 struct ctl_port *port;
13356 struct ctl_softc *softc;
13357 uint32_t initidx, targ_lun;
13359 softc = control_softc;
13361 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13364 * LUN lookup will likely move to the ctl_work_thread() once we
13365 * have our new queueing infrastructure (that doesn't put things on
13366 * a per-LUN queue initially). That is so that we can handle
13367 * things like an INQUIRY to a LUN that we don't have enabled. We
13368 * can't deal with that right now.
13370 mtx_lock(&softc->ctl_lock);
13373 * If we don't have a LUN for this, just toss the sense
13376 port = ctl_io_port(&ctsio->io_hdr);
13377 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13378 if ((targ_lun < CTL_MAX_LUNS)
13379 && (softc->ctl_luns[targ_lun] != NULL))
13380 lun = softc->ctl_luns[targ_lun];
13384 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13386 mtx_lock(&lun->lun_lock);
13388 * Already have CA set for this LUN...toss the sense information.
13390 if (ctl_is_set(lun->have_ca, initidx)) {
13391 mtx_unlock(&lun->lun_lock);
13395 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data,
13396 MIN(sizeof(lun->pending_sense[initidx]),
13397 sizeof(io->scsiio.sense_data)));
13398 ctl_set_mask(lun->have_ca, initidx);
13399 mtx_unlock(&lun->lun_lock);
13402 mtx_unlock(&softc->ctl_lock);
13406 return (CTL_RETVAL_COMPLETE);
13411 * Primary command inlet from frontend ports. All SCSI and task I/O
13412 * requests must go through this function.
13415 ctl_queue(union ctl_io *io)
13417 struct ctl_port *port;
13419 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13422 io->io_hdr.start_time = time_uptime;
13423 getbinuptime(&io->io_hdr.start_bt);
13424 #endif /* CTL_TIME_IO */
13426 /* Map FE-specific LUN ID into global one. */
13427 port = ctl_io_port(&io->io_hdr);
13428 io->io_hdr.nexus.targ_mapped_lun =
13429 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13431 switch (io->io_hdr.io_type) {
13434 if (ctl_debug & CTL_DEBUG_CDB)
13436 ctl_enqueue_incoming(io);
13439 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13443 return (CTL_RETVAL_COMPLETE);
13446 #ifdef CTL_IO_DELAY
13448 ctl_done_timer_wakeup(void *arg)
13452 io = (union ctl_io *)arg;
13455 #endif /* CTL_IO_DELAY */
13458 ctl_serseq_done(union ctl_io *io)
13460 struct ctl_lun *lun;
13462 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13463 if (lun->be_lun == NULL ||
13464 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF)
13466 mtx_lock(&lun->lun_lock);
13467 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE;
13468 ctl_check_blocked(lun);
13469 mtx_unlock(&lun->lun_lock);
13473 ctl_done(union ctl_io *io)
13477 * Enable this to catch duplicate completion issues.
13480 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13481 printf("%s: type %d msg %d cdb %x iptl: "
13482 "%u:%u:%u tag 0x%04x "
13483 "flag %#x status %x\n",
13485 io->io_hdr.io_type,
13486 io->io_hdr.msg_type,
13488 io->io_hdr.nexus.initid,
13489 io->io_hdr.nexus.targ_port,
13490 io->io_hdr.nexus.targ_lun,
13491 (io->io_hdr.io_type ==
13493 io->taskio.tag_num :
13494 io->scsiio.tag_num,
13496 io->io_hdr.status);
13498 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13502 * This is an internal copy of an I/O, and should not go through
13503 * the normal done processing logic.
13505 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13508 #ifdef CTL_IO_DELAY
13509 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13510 struct ctl_lun *lun;
13512 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13514 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13516 struct ctl_lun *lun;
13518 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13521 && (lun->delay_info.done_delay > 0)) {
13523 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
13524 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13525 callout_reset(&io->io_hdr.delay_callout,
13526 lun->delay_info.done_delay * hz,
13527 ctl_done_timer_wakeup, io);
13528 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13529 lun->delay_info.done_delay = 0;
13533 #endif /* CTL_IO_DELAY */
13535 ctl_enqueue_done(io);
13539 ctl_work_thread(void *arg)
13541 struct ctl_thread *thr = (struct ctl_thread *)arg;
13542 struct ctl_softc *softc = thr->ctl_softc;
13546 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13550 * We handle the queues in this order:
13552 * - done queue (to free up resources, unblock other commands)
13556 * If those queues are empty, we break out of the loop and
13559 mtx_lock(&thr->queue_lock);
13560 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13562 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13563 mtx_unlock(&thr->queue_lock);
13564 ctl_handle_isc(io);
13567 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13569 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13570 /* clear any blocked commands, call fe_done */
13571 mtx_unlock(&thr->queue_lock);
13572 ctl_process_done(io);
13575 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13577 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13578 mtx_unlock(&thr->queue_lock);
13579 if (io->io_hdr.io_type == CTL_IO_TASK)
13582 ctl_scsiio_precheck(softc, &io->scsiio);
13585 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13587 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13588 mtx_unlock(&thr->queue_lock);
13589 retval = ctl_scsiio(&io->scsiio);
13590 if (retval != CTL_RETVAL_COMPLETE)
13591 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13595 /* Sleep until we have something to do. */
13596 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13601 ctl_lun_thread(void *arg)
13603 struct ctl_softc *softc = (struct ctl_softc *)arg;
13604 struct ctl_be_lun *be_lun;
13606 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13609 mtx_lock(&softc->ctl_lock);
13610 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13611 if (be_lun != NULL) {
13612 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13613 mtx_unlock(&softc->ctl_lock);
13614 ctl_create_lun(be_lun);
13618 /* Sleep until we have something to do. */
13619 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13620 PDROP | PRIBIO, "-", 0);
13625 ctl_thresh_thread(void *arg)
13627 struct ctl_softc *softc = (struct ctl_softc *)arg;
13628 struct ctl_lun *lun;
13629 struct scsi_da_rw_recovery_page *rwpage;
13630 struct ctl_logical_block_provisioning_page *page;
13632 union ctl_ha_msg msg;
13633 uint64_t thres, val;
13636 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13639 mtx_lock(&softc->ctl_lock);
13640 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13641 if ((lun->flags & CTL_LUN_DISABLED) ||
13642 (lun->flags & CTL_LUN_NO_MEDIA) ||
13643 lun->backend->lun_attr == NULL)
13645 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
13646 softc->ha_mode == CTL_HA_MODE_XFER)
13648 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT];
13649 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0)
13652 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT];
13653 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13654 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13656 thres = scsi_4btoul(page->descr[i].count);
13657 thres <<= CTL_LBP_EXPONENT;
13658 switch (page->descr[i].resource) {
13660 attr = "blocksavail";
13663 attr = "blocksused";
13666 attr = "poolblocksavail";
13669 attr = "poolblocksused";
13674 mtx_unlock(&softc->ctl_lock); // XXX
13675 val = lun->backend->lun_attr(
13676 lun->be_lun->be_lun, attr);
13677 mtx_lock(&softc->ctl_lock);
13678 if (val == UINT64_MAX)
13680 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13681 == SLBPPD_ARMING_INC)
13682 e = (val >= thres);
13684 e = (val <= thres);
13688 mtx_lock(&lun->lun_lock);
13690 scsi_u64to8b((uint8_t *)&page->descr[i] -
13691 (uint8_t *)page, lun->ua_tpt_info);
13692 if (lun->lasttpt == 0 ||
13693 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13694 lun->lasttpt = time_uptime;
13695 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13701 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13704 mtx_unlock(&lun->lun_lock);
13706 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
13707 /* Send msg to other side. */
13708 bzero(&msg.ua, sizeof(msg.ua));
13709 msg.hdr.msg_type = CTL_MSG_UA;
13710 msg.hdr.nexus.initid = -1;
13711 msg.hdr.nexus.targ_port = -1;
13712 msg.hdr.nexus.targ_lun = lun->lun;
13713 msg.hdr.nexus.targ_mapped_lun = lun->lun;
13715 msg.ua.ua_set = (set > 0);
13716 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES;
13717 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8);
13718 mtx_unlock(&softc->ctl_lock); // XXX
13719 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13720 sizeof(msg.ua), M_WAITOK);
13721 mtx_lock(&softc->ctl_lock);
13724 mtx_unlock(&softc->ctl_lock);
13725 pause("-", CTL_LBP_PERIOD * hz);
13730 ctl_enqueue_incoming(union ctl_io *io)
13732 struct ctl_softc *softc = control_softc;
13733 struct ctl_thread *thr;
13736 idx = (io->io_hdr.nexus.targ_port * 127 +
13737 io->io_hdr.nexus.initid) % worker_threads;
13738 thr = &softc->threads[idx];
13739 mtx_lock(&thr->queue_lock);
13740 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13741 mtx_unlock(&thr->queue_lock);
13746 ctl_enqueue_rtr(union ctl_io *io)
13748 struct ctl_softc *softc = control_softc;
13749 struct ctl_thread *thr;
13751 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13752 mtx_lock(&thr->queue_lock);
13753 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13754 mtx_unlock(&thr->queue_lock);
13759 ctl_enqueue_done(union ctl_io *io)
13761 struct ctl_softc *softc = control_softc;
13762 struct ctl_thread *thr;
13764 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13765 mtx_lock(&thr->queue_lock);
13766 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13767 mtx_unlock(&thr->queue_lock);
13772 ctl_enqueue_isc(union ctl_io *io)
13774 struct ctl_softc *softc = control_softc;
13775 struct ctl_thread *thr;
13777 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13778 mtx_lock(&thr->queue_lock);
13779 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13780 mtx_unlock(&thr->queue_lock);