2 * Copyright (c) 2003-2009 Silicon Graphics International Corp.
3 * Copyright (c) 2012 The FreeBSD Foundation
4 * Copyright (c) 2015 Alexander Motin <mav@FreeBSD.org>
7 * Portions of this software were developed by Edward Tomasz Napierala
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions, and the following disclaimer,
15 * without modification.
16 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17 * substantially similar to the "NO WARRANTY" disclaimer below
18 * ("Disclaimer") and any redistribution must be conditioned upon
19 * including a substantially similar Disclaimer requirement for further
20 * binary redistribution.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGES.
38 * CAM Target Layer, a SCSI device emulation subsystem.
40 * Author: Ken Merry <ken@FreeBSD.org>
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/ctype.h>
51 #include <sys/kernel.h>
52 #include <sys/types.h>
53 #include <sys/kthread.h>
55 #include <sys/fcntl.h>
57 #include <sys/module.h>
58 #include <sys/mutex.h>
59 #include <sys/condvar.h>
60 #include <sys/malloc.h>
62 #include <sys/ioccom.h>
63 #include <sys/queue.h>
66 #include <sys/endian.h>
67 #include <sys/sysctl.h>
71 #include <cam/scsi/scsi_all.h>
72 #include <cam/scsi/scsi_cd.h>
73 #include <cam/scsi/scsi_da.h>
74 #include <cam/ctl/ctl_io.h>
75 #include <cam/ctl/ctl.h>
76 #include <cam/ctl/ctl_frontend.h>
77 #include <cam/ctl/ctl_util.h>
78 #include <cam/ctl/ctl_backend.h>
79 #include <cam/ctl/ctl_ioctl.h>
80 #include <cam/ctl/ctl_ha.h>
81 #include <cam/ctl/ctl_private.h>
82 #include <cam/ctl/ctl_debug.h>
83 #include <cam/ctl/ctl_scsi_all.h>
84 #include <cam/ctl/ctl_error.h>
86 struct ctl_softc *control_softc = NULL;
89 * Template mode pages.
93 * Note that these are default values only. The actual values will be
94 * filled in when the user does a mode sense.
96 const static struct scsi_da_rw_recovery_page rw_er_page_default = {
97 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
98 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
99 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE,
100 /*read_retry_count*/0,
101 /*correction_span*/0,
102 /*head_offset_count*/0,
103 /*data_strobe_offset_cnt*/0,
104 /*byte8*/SMS_RWER_LBPERE,
105 /*write_retry_count*/0,
107 /*recovery_time_limit*/{0, 0},
110 const static struct scsi_da_rw_recovery_page rw_er_page_changeable = {
111 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
112 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
113 /*byte3*/SMS_RWER_PER,
114 /*read_retry_count*/0,
115 /*correction_span*/0,
116 /*head_offset_count*/0,
117 /*data_strobe_offset_cnt*/0,
118 /*byte8*/SMS_RWER_LBPERE,
119 /*write_retry_count*/0,
121 /*recovery_time_limit*/{0, 0},
124 const static struct scsi_format_page format_page_default = {
125 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
126 /*page_length*/sizeof(struct scsi_format_page) - 2,
127 /*tracks_per_zone*/ {0, 0},
128 /*alt_sectors_per_zone*/ {0, 0},
129 /*alt_tracks_per_zone*/ {0, 0},
130 /*alt_tracks_per_lun*/ {0, 0},
131 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
132 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
133 /*bytes_per_sector*/ {0, 0},
134 /*interleave*/ {0, 0},
135 /*track_skew*/ {0, 0},
136 /*cylinder_skew*/ {0, 0},
138 /*reserved*/ {0, 0, 0}
141 const static struct scsi_format_page format_page_changeable = {
142 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
143 /*page_length*/sizeof(struct scsi_format_page) - 2,
144 /*tracks_per_zone*/ {0, 0},
145 /*alt_sectors_per_zone*/ {0, 0},
146 /*alt_tracks_per_zone*/ {0, 0},
147 /*alt_tracks_per_lun*/ {0, 0},
148 /*sectors_per_track*/ {0, 0},
149 /*bytes_per_sector*/ {0, 0},
150 /*interleave*/ {0, 0},
151 /*track_skew*/ {0, 0},
152 /*cylinder_skew*/ {0, 0},
154 /*reserved*/ {0, 0, 0}
157 const static struct scsi_rigid_disk_page rigid_disk_page_default = {
158 /*page_code*/SMS_RIGID_DISK_PAGE,
159 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
160 /*cylinders*/ {0, 0, 0},
161 /*heads*/ CTL_DEFAULT_HEADS,
162 /*start_write_precomp*/ {0, 0, 0},
163 /*start_reduced_current*/ {0, 0, 0},
164 /*step_rate*/ {0, 0},
165 /*landing_zone_cylinder*/ {0, 0, 0},
166 /*rpl*/ SRDP_RPL_DISABLED,
167 /*rotational_offset*/ 0,
169 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
170 CTL_DEFAULT_ROTATION_RATE & 0xff},
174 const static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
175 /*page_code*/SMS_RIGID_DISK_PAGE,
176 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
177 /*cylinders*/ {0, 0, 0},
179 /*start_write_precomp*/ {0, 0, 0},
180 /*start_reduced_current*/ {0, 0, 0},
181 /*step_rate*/ {0, 0},
182 /*landing_zone_cylinder*/ {0, 0, 0},
184 /*rotational_offset*/ 0,
186 /*rotation_rate*/ {0, 0},
190 const static struct scsi_da_verify_recovery_page verify_er_page_default = {
191 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE,
192 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2,
194 /*read_retry_count*/0,
195 /*reserved*/{ 0, 0, 0, 0, 0, 0 },
196 /*recovery_time_limit*/{0, 0},
199 const static struct scsi_da_verify_recovery_page verify_er_page_changeable = {
200 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE,
201 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2,
202 /*byte3*/SMS_VER_PER,
203 /*read_retry_count*/0,
204 /*reserved*/{ 0, 0, 0, 0, 0, 0 },
205 /*recovery_time_limit*/{0, 0},
208 const static struct scsi_caching_page caching_page_default = {
209 /*page_code*/SMS_CACHING_PAGE,
210 /*page_length*/sizeof(struct scsi_caching_page) - 2,
211 /*flags1*/ SCP_DISC | SCP_WCE,
213 /*disable_pf_transfer_len*/ {0xff, 0xff},
214 /*min_prefetch*/ {0, 0},
215 /*max_prefetch*/ {0xff, 0xff},
216 /*max_pf_ceiling*/ {0xff, 0xff},
218 /*cache_segments*/ 0,
219 /*cache_seg_size*/ {0, 0},
221 /*non_cache_seg_size*/ {0, 0, 0}
224 const static struct scsi_caching_page caching_page_changeable = {
225 /*page_code*/SMS_CACHING_PAGE,
226 /*page_length*/sizeof(struct scsi_caching_page) - 2,
227 /*flags1*/ SCP_WCE | SCP_RCD,
229 /*disable_pf_transfer_len*/ {0, 0},
230 /*min_prefetch*/ {0, 0},
231 /*max_prefetch*/ {0, 0},
232 /*max_pf_ceiling*/ {0, 0},
234 /*cache_segments*/ 0,
235 /*cache_seg_size*/ {0, 0},
237 /*non_cache_seg_size*/ {0, 0, 0}
240 const static struct scsi_control_page control_page_default = {
241 /*page_code*/SMS_CONTROL_MODE_PAGE,
242 /*page_length*/sizeof(struct scsi_control_page) - 2,
244 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED,
247 /*aen_holdoff_period*/{0, 0},
248 /*busy_timeout_period*/{0, 0},
249 /*extended_selftest_completion_time*/{0, 0}
252 const static struct scsi_control_page control_page_changeable = {
253 /*page_code*/SMS_CONTROL_MODE_PAGE,
254 /*page_length*/sizeof(struct scsi_control_page) - 2,
256 /*queue_flags*/SCP_QUEUE_ALG_MASK | SCP_NUAR,
257 /*eca_and_aen*/SCP_SWP,
259 /*aen_holdoff_period*/{0, 0},
260 /*busy_timeout_period*/{0, 0},
261 /*extended_selftest_completion_time*/{0, 0}
264 #define CTL_CEM_LEN (sizeof(struct scsi_control_ext_page) - 4)
266 const static struct scsi_control_ext_page control_ext_page_default = {
267 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
268 /*subpage_code*/0x01,
269 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
275 const static struct scsi_control_ext_page control_ext_page_changeable = {
276 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
277 /*subpage_code*/0x01,
278 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
284 const static struct scsi_info_exceptions_page ie_page_default = {
285 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
286 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
287 /*info_flags*/SIEP_FLAGS_EWASC,
288 /*mrie*/SIEP_MRIE_NO,
289 /*interval_timer*/{0, 0, 0, 0},
290 /*report_count*/{0, 0, 0, 1}
293 const static struct scsi_info_exceptions_page ie_page_changeable = {
294 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
295 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
296 /*info_flags*/SIEP_FLAGS_EWASC | SIEP_FLAGS_DEXCPT | SIEP_FLAGS_TEST |
299 /*interval_timer*/{0xff, 0xff, 0xff, 0xff},
300 /*report_count*/{0xff, 0xff, 0xff, 0xff}
303 #define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4)
305 const static struct ctl_logical_block_provisioning_page lbp_page_default = {{
306 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
307 /*subpage_code*/0x02,
308 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
310 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
315 /*count*/{0, 0, 0, 0}},
319 /*count*/{0, 0, 0, 0}},
323 /*count*/{0, 0, 0, 0}},
327 /*count*/{0, 0, 0, 0}}
331 const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{
332 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
333 /*subpage_code*/0x02,
334 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
335 /*flags*/SLBPP_SITUA,
336 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
341 /*count*/{0, 0, 0, 0}},
345 /*count*/{0, 0, 0, 0}},
349 /*count*/{0, 0, 0, 0}},
353 /*count*/{0, 0, 0, 0}}
357 const static struct scsi_cddvd_capabilities_page cddvd_page_default = {
358 /*page_code*/SMS_CDDVD_CAPS_PAGE,
359 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2,
367 /*nvol_levels*/{0, 0},
368 /*buffer_size*/{8, 0},
373 /*copy_management*/0,
375 /*rotation_control*/0,
376 /*cur_write_speed*/0,
377 /*num_speed_descr*/0,
380 const static struct scsi_cddvd_capabilities_page cddvd_page_changeable = {
381 /*page_code*/SMS_CDDVD_CAPS_PAGE,
382 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2,
390 /*nvol_levels*/{0, 0},
391 /*buffer_size*/{0, 0},
396 /*copy_management*/0,
398 /*rotation_control*/0,
399 /*cur_write_speed*/0,
400 /*num_speed_descr*/0,
403 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
404 static int worker_threads = -1;
405 TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads);
406 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
407 &worker_threads, 1, "Number of worker threads");
408 static int ctl_debug = CTL_DEBUG_NONE;
409 TUNABLE_INT("kern.cam.ctl.debug", &ctl_debug);
410 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN,
411 &ctl_debug, 0, "Enabled debug flags");
412 static int ctl_lun_map_size = 1024;
413 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, lun_map_size, CTLFLAG_RWTUN,
414 &ctl_lun_map_size, 0, "Size of per-port LUN map (max LUN + 1)");
417 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
418 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
419 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0),
420 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
422 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 10
424 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
426 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
427 static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest);
428 static int ctl_init(void);
429 void ctl_shutdown(void);
430 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
431 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
432 static void ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
433 static void ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
434 struct ctl_ooa *ooa_hdr,
435 struct ctl_ooa_entry *kern_entries);
436 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
438 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
439 struct ctl_be_lun *be_lun);
440 static int ctl_free_lun(struct ctl_lun *lun);
441 static void ctl_create_lun(struct ctl_be_lun *be_lun);
443 static int ctl_do_mode_select(union ctl_io *io);
444 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
445 uint64_t res_key, uint64_t sa_res_key,
446 uint8_t type, uint32_t residx,
447 struct ctl_scsiio *ctsio,
448 struct scsi_per_res_out *cdb,
449 struct scsi_per_res_out_parms* param);
450 static void ctl_pro_preempt_other(struct ctl_lun *lun,
451 union ctl_ha_msg *msg);
452 static void ctl_hndl_per_res_out_on_other_sc(union ctl_io *io);
453 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
454 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
455 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
456 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
457 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
458 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
460 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
462 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
463 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
464 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
465 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
466 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
467 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2,
469 static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2);
470 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
471 union ctl_io *pending_io, union ctl_io *ooa_io);
472 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
473 union ctl_io *starting_io);
474 static int ctl_check_blocked(struct ctl_lun *lun);
475 static int ctl_scsiio_lun_check(struct ctl_lun *lun,
476 const struct ctl_cmd_entry *entry,
477 struct ctl_scsiio *ctsio);
478 static void ctl_failover_lun(union ctl_io *io);
479 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
480 struct ctl_scsiio *ctsio);
481 static int ctl_scsiio(struct ctl_scsiio *ctsio);
483 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
484 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
485 ctl_ua_type ua_type);
486 static int ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io,
487 ctl_ua_type ua_type);
488 static int ctl_lun_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
489 static int ctl_abort_task(union ctl_io *io);
490 static int ctl_abort_task_set(union ctl_io *io);
491 static int ctl_query_task(union ctl_io *io, int task_set);
492 static int ctl_i_t_nexus_reset(union ctl_io *io);
493 static int ctl_query_async_event(union ctl_io *io);
494 static void ctl_run_task(union ctl_io *io);
496 static void ctl_datamove_timer_wakeup(void *arg);
497 static void ctl_done_timer_wakeup(void *arg);
498 #endif /* CTL_IO_DELAY */
500 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
501 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
502 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
503 static void ctl_datamove_remote_write(union ctl_io *io);
504 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
505 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
506 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
507 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
508 ctl_ha_dt_cb callback);
509 static void ctl_datamove_remote_read(union ctl_io *io);
510 static void ctl_datamove_remote(union ctl_io *io);
511 static void ctl_process_done(union ctl_io *io);
512 static void ctl_lun_thread(void *arg);
513 static void ctl_thresh_thread(void *arg);
514 static void ctl_work_thread(void *arg);
515 static void ctl_enqueue_incoming(union ctl_io *io);
516 static void ctl_enqueue_rtr(union ctl_io *io);
517 static void ctl_enqueue_done(union ctl_io *io);
518 static void ctl_enqueue_isc(union ctl_io *io);
519 static const struct ctl_cmd_entry *
520 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
521 static const struct ctl_cmd_entry *
522 ctl_validate_command(struct ctl_scsiio *ctsio);
523 static int ctl_cmd_applicable(uint8_t lun_type,
524 const struct ctl_cmd_entry *entry);
526 static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx);
527 static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx);
528 static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx);
529 static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key);
532 * Load the serialization table. This isn't very pretty, but is probably
533 * the easiest way to do it.
535 #include "ctl_ser_table.c"
538 * We only need to define open, close and ioctl routines for this driver.
540 static struct cdevsw ctl_cdevsw = {
541 .d_version = D_VERSION,
544 .d_close = ctl_close,
545 .d_ioctl = ctl_ioctl,
550 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
552 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
554 static moduledata_t ctl_moduledata = {
556 ctl_module_event_handler,
560 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
561 MODULE_VERSION(ctl, 1);
563 static struct ctl_frontend ha_frontend =
569 ctl_ha_datamove(union ctl_io *io)
571 struct ctl_lun *lun = CTL_LUN(io);
572 struct ctl_sg_entry *sgl;
573 union ctl_ha_msg msg;
574 uint32_t sg_entries_sent;
575 int do_sg_copy, i, j;
577 memset(&msg.dt, 0, sizeof(msg.dt));
578 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
579 msg.hdr.original_sc = io->io_hdr.original_sc;
580 msg.hdr.serializing_sc = io;
581 msg.hdr.nexus = io->io_hdr.nexus;
582 msg.hdr.status = io->io_hdr.status;
583 msg.dt.flags = io->io_hdr.flags;
586 * We convert everything into a S/G list here. We can't
587 * pass by reference, only by value between controllers.
588 * So we can't pass a pointer to the S/G list, only as many
589 * S/G entries as we can fit in here. If it's possible for
590 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
591 * then we need to break this up into multiple transfers.
593 if (io->scsiio.kern_sg_entries == 0) {
594 msg.dt.kern_sg_entries = 1;
596 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
597 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
599 /* XXX KDM use busdma here! */
600 msg.dt.sg_list[0].addr =
601 (void *)vtophys(io->scsiio.kern_data_ptr);
604 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
605 ("HA does not support BUS_ADDR"));
606 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
608 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
611 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
615 msg.dt.kern_data_len = io->scsiio.kern_data_len;
616 msg.dt.kern_total_len = io->scsiio.kern_total_len;
617 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
618 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
619 msg.dt.sg_sequence = 0;
622 * Loop until we've sent all of the S/G entries. On the
623 * other end, we'll recompose these S/G entries into one
624 * contiguous list before processing.
626 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries;
627 msg.dt.sg_sequence++) {
628 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) /
629 sizeof(msg.dt.sg_list[0])),
630 msg.dt.kern_sg_entries - sg_entries_sent);
631 if (do_sg_copy != 0) {
632 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
633 for (i = sg_entries_sent, j = 0;
634 i < msg.dt.cur_sg_entries; i++, j++) {
636 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
637 msg.dt.sg_list[j].addr = sgl[i].addr;
639 /* XXX KDM use busdma here! */
640 msg.dt.sg_list[j].addr =
641 (void *)vtophys(sgl[i].addr);
644 KASSERT((io->io_hdr.flags &
645 CTL_FLAG_BUS_ADDR) == 0,
646 ("HA does not support BUS_ADDR"));
647 msg.dt.sg_list[j].addr = sgl[i].addr;
649 msg.dt.sg_list[j].len = sgl[i].len;
653 sg_entries_sent += msg.dt.cur_sg_entries;
654 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries);
655 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
656 sizeof(msg.dt) - sizeof(msg.dt.sg_list) +
657 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries,
658 M_WAITOK) > CTL_HA_STATUS_SUCCESS) {
659 io->io_hdr.port_status = 31341;
660 io->scsiio.be_move_done(io);
663 msg.dt.sent_sg_entries = sg_entries_sent;
667 * Officially handover the request from us to peer.
668 * If failover has just happened, then we must return error.
669 * If failover happen just after, then it is not our problem.
672 mtx_lock(&lun->lun_lock);
673 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
675 mtx_unlock(&lun->lun_lock);
676 io->io_hdr.port_status = 31342;
677 io->scsiio.be_move_done(io);
680 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
681 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
683 mtx_unlock(&lun->lun_lock);
687 ctl_ha_done(union ctl_io *io)
689 union ctl_ha_msg msg;
691 if (io->io_hdr.io_type == CTL_IO_SCSI) {
692 memset(&msg, 0, sizeof(msg));
693 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
694 msg.hdr.original_sc = io->io_hdr.original_sc;
695 msg.hdr.nexus = io->io_hdr.nexus;
696 msg.hdr.status = io->io_hdr.status;
697 msg.scsi.scsi_status = io->scsiio.scsi_status;
698 msg.scsi.tag_num = io->scsiio.tag_num;
699 msg.scsi.tag_type = io->scsiio.tag_type;
700 msg.scsi.sense_len = io->scsiio.sense_len;
701 msg.scsi.sense_residual = io->scsiio.sense_residual;
702 msg.scsi.residual = io->scsiio.residual;
703 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
704 io->scsiio.sense_len);
705 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
706 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
707 msg.scsi.sense_len, M_WAITOK);
713 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
714 union ctl_ha_msg *msg_info)
716 struct ctl_scsiio *ctsio;
718 if (msg_info->hdr.original_sc == NULL) {
719 printf("%s: original_sc == NULL!\n", __func__);
720 /* XXX KDM now what? */
724 ctsio = &msg_info->hdr.original_sc->scsiio;
725 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
726 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
727 ctsio->io_hdr.status = msg_info->hdr.status;
728 ctsio->scsi_status = msg_info->scsi.scsi_status;
729 ctsio->sense_len = msg_info->scsi.sense_len;
730 ctsio->sense_residual = msg_info->scsi.sense_residual;
731 ctsio->residual = msg_info->scsi.residual;
732 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
733 msg_info->scsi.sense_len);
734 ctl_enqueue_isc((union ctl_io *)ctsio);
738 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
739 union ctl_ha_msg *msg_info)
741 struct ctl_scsiio *ctsio;
743 if (msg_info->hdr.serializing_sc == NULL) {
744 printf("%s: serializing_sc == NULL!\n", __func__);
745 /* XXX KDM now what? */
749 ctsio = &msg_info->hdr.serializing_sc->scsiio;
750 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
751 ctl_enqueue_isc((union ctl_io *)ctsio);
755 ctl_isc_announce_lun(struct ctl_lun *lun)
757 struct ctl_softc *softc = lun->ctl_softc;
758 union ctl_ha_msg *msg;
759 struct ctl_ha_msg_lun_pr_key pr_key;
762 if (softc->ha_link != CTL_HA_LINK_ONLINE)
764 mtx_lock(&lun->lun_lock);
765 i = sizeof(msg->lun);
767 i += lun->lun_devid->len;
768 i += sizeof(pr_key) * lun->pr_key_count;
770 mtx_unlock(&lun->lun_lock);
771 msg = malloc(i, M_CTL, M_WAITOK);
772 mtx_lock(&lun->lun_lock);
773 k = sizeof(msg->lun);
775 k += lun->lun_devid->len;
776 k += sizeof(pr_key) * lun->pr_key_count;
782 bzero(&msg->lun, sizeof(msg->lun));
783 msg->hdr.msg_type = CTL_MSG_LUN_SYNC;
784 msg->hdr.nexus.targ_lun = lun->lun;
785 msg->hdr.nexus.targ_mapped_lun = lun->lun;
786 msg->lun.flags = lun->flags;
787 msg->lun.pr_generation = lun->pr_generation;
788 msg->lun.pr_res_idx = lun->pr_res_idx;
789 msg->lun.pr_res_type = lun->pr_res_type;
790 msg->lun.pr_key_count = lun->pr_key_count;
792 if (lun->lun_devid) {
793 msg->lun.lun_devid_len = lun->lun_devid->len;
794 memcpy(&msg->lun.data[i], lun->lun_devid->data,
795 msg->lun.lun_devid_len);
796 i += msg->lun.lun_devid_len;
798 for (k = 0; k < CTL_MAX_INITIATORS; k++) {
799 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0)
802 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key));
805 mtx_unlock(&lun->lun_lock);
806 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
810 if (lun->flags & CTL_LUN_PRIMARY_SC) {
811 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
812 ctl_isc_announce_mode(lun, -1,
813 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
814 lun->mode_pages.index[i].subpage);
820 ctl_isc_announce_port(struct ctl_port *port)
822 struct ctl_softc *softc = port->ctl_softc;
823 union ctl_ha_msg *msg;
826 if (port->targ_port < softc->port_min ||
827 port->targ_port >= softc->port_max ||
828 softc->ha_link != CTL_HA_LINK_ONLINE)
830 i = sizeof(msg->port) + strlen(port->port_name) + 1;
832 i += port->lun_map_size * sizeof(uint32_t);
833 if (port->port_devid)
834 i += port->port_devid->len;
835 if (port->target_devid)
836 i += port->target_devid->len;
837 if (port->init_devid)
838 i += port->init_devid->len;
839 msg = malloc(i, M_CTL, M_WAITOK);
840 bzero(&msg->port, sizeof(msg->port));
841 msg->hdr.msg_type = CTL_MSG_PORT_SYNC;
842 msg->hdr.nexus.targ_port = port->targ_port;
843 msg->port.port_type = port->port_type;
844 msg->port.physical_port = port->physical_port;
845 msg->port.virtual_port = port->virtual_port;
846 msg->port.status = port->status;
848 msg->port.name_len = sprintf(&msg->port.data[i],
849 "%d:%s", softc->ha_id, port->port_name) + 1;
850 i += msg->port.name_len;
852 msg->port.lun_map_len = port->lun_map_size * sizeof(uint32_t);
853 memcpy(&msg->port.data[i], port->lun_map,
854 msg->port.lun_map_len);
855 i += msg->port.lun_map_len;
857 if (port->port_devid) {
858 msg->port.port_devid_len = port->port_devid->len;
859 memcpy(&msg->port.data[i], port->port_devid->data,
860 msg->port.port_devid_len);
861 i += msg->port.port_devid_len;
863 if (port->target_devid) {
864 msg->port.target_devid_len = port->target_devid->len;
865 memcpy(&msg->port.data[i], port->target_devid->data,
866 msg->port.target_devid_len);
867 i += msg->port.target_devid_len;
869 if (port->init_devid) {
870 msg->port.init_devid_len = port->init_devid->len;
871 memcpy(&msg->port.data[i], port->init_devid->data,
872 msg->port.init_devid_len);
873 i += msg->port.init_devid_len;
875 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
881 ctl_isc_announce_iid(struct ctl_port *port, int iid)
883 struct ctl_softc *softc = port->ctl_softc;
884 union ctl_ha_msg *msg;
887 if (port->targ_port < softc->port_min ||
888 port->targ_port >= softc->port_max ||
889 softc->ha_link != CTL_HA_LINK_ONLINE)
891 mtx_lock(&softc->ctl_lock);
892 i = sizeof(msg->iid);
894 if (port->wwpn_iid[iid].name)
895 l = strlen(port->wwpn_iid[iid].name) + 1;
897 msg = malloc(i, M_CTL, M_NOWAIT);
899 mtx_unlock(&softc->ctl_lock);
902 bzero(&msg->iid, sizeof(msg->iid));
903 msg->hdr.msg_type = CTL_MSG_IID_SYNC;
904 msg->hdr.nexus.targ_port = port->targ_port;
905 msg->hdr.nexus.initid = iid;
906 msg->iid.in_use = port->wwpn_iid[iid].in_use;
907 msg->iid.name_len = l;
908 msg->iid.wwpn = port->wwpn_iid[iid].wwpn;
909 if (port->wwpn_iid[iid].name)
910 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l);
911 mtx_unlock(&softc->ctl_lock);
912 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT);
917 ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx,
918 uint8_t page, uint8_t subpage)
920 struct ctl_softc *softc = lun->ctl_softc;
921 union ctl_ha_msg msg;
924 if (softc->ha_link != CTL_HA_LINK_ONLINE)
926 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
927 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
928 page && lun->mode_pages.index[i].subpage == subpage)
931 if (i == CTL_NUM_MODE_PAGES)
934 /* Don't try to replicate pages not present on this device. */
935 if (lun->mode_pages.index[i].page_data == NULL)
938 bzero(&msg.mode, sizeof(msg.mode));
939 msg.hdr.msg_type = CTL_MSG_MODE_SYNC;
940 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT;
941 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT;
942 msg.hdr.nexus.targ_lun = lun->lun;
943 msg.hdr.nexus.targ_mapped_lun = lun->lun;
944 msg.mode.page_code = page;
945 msg.mode.subpage = subpage;
946 msg.mode.page_len = lun->mode_pages.index[i].page_len;
947 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data,
949 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode),
954 ctl_isc_ha_link_up(struct ctl_softc *softc)
956 struct ctl_port *port;
958 union ctl_ha_msg msg;
961 /* Announce this node parameters to peer for validation. */
962 msg.login.msg_type = CTL_MSG_LOGIN;
963 msg.login.version = CTL_HA_VERSION;
964 msg.login.ha_mode = softc->ha_mode;
965 msg.login.ha_id = softc->ha_id;
966 msg.login.max_luns = CTL_MAX_LUNS;
967 msg.login.max_ports = CTL_MAX_PORTS;
968 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT;
969 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login),
972 STAILQ_FOREACH(port, &softc->port_list, links) {
973 ctl_isc_announce_port(port);
974 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
975 if (port->wwpn_iid[i].in_use)
976 ctl_isc_announce_iid(port, i);
979 STAILQ_FOREACH(lun, &softc->lun_list, links)
980 ctl_isc_announce_lun(lun);
984 ctl_isc_ha_link_down(struct ctl_softc *softc)
986 struct ctl_port *port;
991 mtx_lock(&softc->ctl_lock);
992 STAILQ_FOREACH(lun, &softc->lun_list, links) {
993 mtx_lock(&lun->lun_lock);
994 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) {
995 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
996 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
998 mtx_unlock(&lun->lun_lock);
1000 mtx_unlock(&softc->ctl_lock);
1001 io = ctl_alloc_io(softc->othersc_pool);
1002 mtx_lock(&softc->ctl_lock);
1004 io->io_hdr.msg_type = CTL_MSG_FAILOVER;
1005 io->io_hdr.nexus.targ_mapped_lun = lun->lun;
1006 ctl_enqueue_isc(io);
1009 STAILQ_FOREACH(port, &softc->port_list, links) {
1010 if (port->targ_port >= softc->port_min &&
1011 port->targ_port < softc->port_max)
1013 port->status &= ~CTL_PORT_STATUS_ONLINE;
1014 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1015 port->wwpn_iid[i].in_use = 0;
1016 free(port->wwpn_iid[i].name, M_CTL);
1017 port->wwpn_iid[i].name = NULL;
1020 mtx_unlock(&softc->ctl_lock);
1024 ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1026 struct ctl_lun *lun;
1027 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus);
1029 mtx_lock(&softc->ctl_lock);
1030 if (msg->hdr.nexus.targ_mapped_lun >= CTL_MAX_LUNS ||
1031 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) == NULL) {
1032 mtx_unlock(&softc->ctl_lock);
1035 mtx_lock(&lun->lun_lock);
1036 mtx_unlock(&softc->ctl_lock);
1037 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && msg->ua.ua_set)
1038 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8);
1039 if (msg->ua.ua_all) {
1041 ctl_est_ua_all(lun, iid, msg->ua.ua_type);
1043 ctl_clr_ua_all(lun, iid, msg->ua.ua_type);
1046 ctl_est_ua(lun, iid, msg->ua.ua_type);
1048 ctl_clr_ua(lun, iid, msg->ua.ua_type);
1050 mtx_unlock(&lun->lun_lock);
1054 ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1056 struct ctl_lun *lun;
1057 struct ctl_ha_msg_lun_pr_key pr_key;
1059 ctl_lun_flags oflags;
1062 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1063 mtx_lock(&softc->ctl_lock);
1064 if (targ_lun >= CTL_MAX_LUNS ||
1065 (lun = softc->ctl_luns[targ_lun]) == NULL) {
1066 mtx_unlock(&softc->ctl_lock);
1069 mtx_lock(&lun->lun_lock);
1070 mtx_unlock(&softc->ctl_lock);
1071 if (lun->flags & CTL_LUN_DISABLED) {
1072 mtx_unlock(&lun->lun_lock);
1075 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0;
1076 if (msg->lun.lun_devid_len != i || (i > 0 &&
1077 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) {
1078 mtx_unlock(&lun->lun_lock);
1079 printf("%s: Received conflicting HA LUN %d\n",
1080 __func__, targ_lun);
1083 /* Record whether peer is primary. */
1084 oflags = lun->flags;
1085 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) &&
1086 (msg->lun.flags & CTL_LUN_DISABLED) == 0)
1087 lun->flags |= CTL_LUN_PEER_SC_PRIMARY;
1089 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1090 if (oflags != lun->flags)
1091 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1093 /* If peer is primary and we are not -- use data */
1094 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
1095 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) {
1096 lun->pr_generation = msg->lun.pr_generation;
1097 lun->pr_res_idx = msg->lun.pr_res_idx;
1098 lun->pr_res_type = msg->lun.pr_res_type;
1099 lun->pr_key_count = msg->lun.pr_key_count;
1100 for (k = 0; k < CTL_MAX_INITIATORS; k++)
1101 ctl_clr_prkey(lun, k);
1102 for (k = 0; k < msg->lun.pr_key_count; k++) {
1103 memcpy(&pr_key, &msg->lun.data[i],
1105 ctl_alloc_prkey(lun, pr_key.pr_iid);
1106 ctl_set_prkey(lun, pr_key.pr_iid,
1108 i += sizeof(pr_key);
1112 mtx_unlock(&lun->lun_lock);
1113 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n",
1115 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ?
1116 "primary" : "secondary"));
1118 /* If we are primary but peer doesn't know -- notify */
1119 if ((lun->flags & CTL_LUN_PRIMARY_SC) &&
1120 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0)
1121 ctl_isc_announce_lun(lun);
1126 ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1128 struct ctl_port *port;
1129 struct ctl_lun *lun;
1132 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1134 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__,
1135 msg->hdr.nexus.targ_port));
1137 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO);
1138 port->frontend = &ha_frontend;
1139 port->targ_port = msg->hdr.nexus.targ_port;
1140 port->fe_datamove = ctl_ha_datamove;
1141 port->fe_done = ctl_ha_done;
1142 } else if (port->frontend == &ha_frontend) {
1143 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__,
1144 msg->hdr.nexus.targ_port));
1147 printf("%s: Received conflicting HA port %d\n",
1148 __func__, msg->hdr.nexus.targ_port);
1151 port->port_type = msg->port.port_type;
1152 port->physical_port = msg->port.physical_port;
1153 port->virtual_port = msg->port.virtual_port;
1154 port->status = msg->port.status;
1156 free(port->port_name, M_CTL);
1157 port->port_name = strndup(&msg->port.data[i], msg->port.name_len,
1159 i += msg->port.name_len;
1160 if (msg->port.lun_map_len != 0) {
1161 if (port->lun_map == NULL ||
1162 port->lun_map_size * sizeof(uint32_t) <
1163 msg->port.lun_map_len) {
1164 port->lun_map_size = 0;
1165 free(port->lun_map, M_CTL);
1166 port->lun_map = malloc(msg->port.lun_map_len,
1169 memcpy(port->lun_map, &msg->port.data[i], msg->port.lun_map_len);
1170 port->lun_map_size = msg->port.lun_map_len / sizeof(uint32_t);
1171 i += msg->port.lun_map_len;
1173 port->lun_map_size = 0;
1174 free(port->lun_map, M_CTL);
1175 port->lun_map = NULL;
1177 if (msg->port.port_devid_len != 0) {
1178 if (port->port_devid == NULL ||
1179 port->port_devid->len < msg->port.port_devid_len) {
1180 free(port->port_devid, M_CTL);
1181 port->port_devid = malloc(sizeof(struct ctl_devid) +
1182 msg->port.port_devid_len, M_CTL, M_WAITOK);
1184 memcpy(port->port_devid->data, &msg->port.data[i],
1185 msg->port.port_devid_len);
1186 port->port_devid->len = msg->port.port_devid_len;
1187 i += msg->port.port_devid_len;
1189 free(port->port_devid, M_CTL);
1190 port->port_devid = NULL;
1192 if (msg->port.target_devid_len != 0) {
1193 if (port->target_devid == NULL ||
1194 port->target_devid->len < msg->port.target_devid_len) {
1195 free(port->target_devid, M_CTL);
1196 port->target_devid = malloc(sizeof(struct ctl_devid) +
1197 msg->port.target_devid_len, M_CTL, M_WAITOK);
1199 memcpy(port->target_devid->data, &msg->port.data[i],
1200 msg->port.target_devid_len);
1201 port->target_devid->len = msg->port.target_devid_len;
1202 i += msg->port.target_devid_len;
1204 free(port->target_devid, M_CTL);
1205 port->target_devid = NULL;
1207 if (msg->port.init_devid_len != 0) {
1208 if (port->init_devid == NULL ||
1209 port->init_devid->len < msg->port.init_devid_len) {
1210 free(port->init_devid, M_CTL);
1211 port->init_devid = malloc(sizeof(struct ctl_devid) +
1212 msg->port.init_devid_len, M_CTL, M_WAITOK);
1214 memcpy(port->init_devid->data, &msg->port.data[i],
1215 msg->port.init_devid_len);
1216 port->init_devid->len = msg->port.init_devid_len;
1217 i += msg->port.init_devid_len;
1219 free(port->init_devid, M_CTL);
1220 port->init_devid = NULL;
1223 if (ctl_port_register(port) != 0) {
1224 printf("%s: ctl_port_register() failed with error\n",
1228 mtx_lock(&softc->ctl_lock);
1229 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1230 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
1232 mtx_lock(&lun->lun_lock);
1233 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
1234 mtx_unlock(&lun->lun_lock);
1236 mtx_unlock(&softc->ctl_lock);
1240 ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1242 struct ctl_port *port;
1245 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1247 printf("%s: Received IID for unknown port %d\n",
1248 __func__, msg->hdr.nexus.targ_port);
1251 iid = msg->hdr.nexus.initid;
1252 port->wwpn_iid[iid].in_use = msg->iid.in_use;
1253 port->wwpn_iid[iid].wwpn = msg->iid.wwpn;
1254 free(port->wwpn_iid[iid].name, M_CTL);
1255 if (msg->iid.name_len) {
1256 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0],
1257 msg->iid.name_len, M_CTL);
1259 port->wwpn_iid[iid].name = NULL;
1263 ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1266 if (msg->login.version != CTL_HA_VERSION) {
1267 printf("CTL HA peers have different versions %d != %d\n",
1268 msg->login.version, CTL_HA_VERSION);
1269 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1272 if (msg->login.ha_mode != softc->ha_mode) {
1273 printf("CTL HA peers have different ha_mode %d != %d\n",
1274 msg->login.ha_mode, softc->ha_mode);
1275 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1278 if (msg->login.ha_id == softc->ha_id) {
1279 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id);
1280 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1283 if (msg->login.max_luns != CTL_MAX_LUNS ||
1284 msg->login.max_ports != CTL_MAX_PORTS ||
1285 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) {
1286 printf("CTL HA peers have different limits\n");
1287 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1293 ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1295 struct ctl_lun *lun;
1297 uint32_t initidx, targ_lun;
1299 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1300 mtx_lock(&softc->ctl_lock);
1301 if (targ_lun >= CTL_MAX_LUNS ||
1302 (lun = softc->ctl_luns[targ_lun]) == NULL) {
1303 mtx_unlock(&softc->ctl_lock);
1306 mtx_lock(&lun->lun_lock);
1307 mtx_unlock(&softc->ctl_lock);
1308 if (lun->flags & CTL_LUN_DISABLED) {
1309 mtx_unlock(&lun->lun_lock);
1312 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
1313 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
1314 msg->mode.page_code &&
1315 lun->mode_pages.index[i].subpage == msg->mode.subpage)
1318 if (i == CTL_NUM_MODE_PAGES) {
1319 mtx_unlock(&lun->lun_lock);
1322 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data,
1323 lun->mode_pages.index[i].page_len);
1324 initidx = ctl_get_initindex(&msg->hdr.nexus);
1326 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
1327 mtx_unlock(&lun->lun_lock);
1331 * ISC (Inter Shelf Communication) event handler. Events from the HA
1332 * subsystem come in here.
1335 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
1337 struct ctl_softc *softc = control_softc;
1339 struct ctl_prio *presio;
1340 ctl_ha_status isc_status;
1342 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event));
1343 if (event == CTL_HA_EVT_MSG_RECV) {
1344 union ctl_ha_msg *msg, msgbuf;
1346 if (param > sizeof(msgbuf))
1347 msg = malloc(param, M_CTL, M_WAITOK);
1350 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param,
1352 if (isc_status != CTL_HA_STATUS_SUCCESS) {
1353 printf("%s: Error receiving message: %d\n",
1354 __func__, isc_status);
1360 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type));
1361 switch (msg->hdr.msg_type) {
1362 case CTL_MSG_SERIALIZE:
1363 io = ctl_alloc_io(softc->othersc_pool);
1365 // populate ctsio from msg
1366 io->io_hdr.io_type = CTL_IO_SCSI;
1367 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
1368 io->io_hdr.original_sc = msg->hdr.original_sc;
1369 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
1372 * If we're in serialization-only mode, we don't
1373 * want to go through full done processing. Thus
1376 * XXX KDM add another flag that is more specific.
1378 if (softc->ha_mode != CTL_HA_MODE_XFER)
1379 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
1380 io->io_hdr.nexus = msg->hdr.nexus;
1382 printf("port %u, iid %u, lun %u\n",
1383 io->io_hdr.nexus.targ_port,
1384 io->io_hdr.nexus.initid,
1385 io->io_hdr.nexus.targ_lun);
1387 io->scsiio.tag_num = msg->scsi.tag_num;
1388 io->scsiio.tag_type = msg->scsi.tag_type;
1390 io->io_hdr.start_time = time_uptime;
1391 getbinuptime(&io->io_hdr.start_bt);
1392 #endif /* CTL_TIME_IO */
1393 io->scsiio.cdb_len = msg->scsi.cdb_len;
1394 memcpy(io->scsiio.cdb, msg->scsi.cdb,
1396 if (softc->ha_mode == CTL_HA_MODE_XFER) {
1397 const struct ctl_cmd_entry *entry;
1399 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
1400 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
1402 entry->flags & CTL_FLAG_DATA_MASK;
1404 ctl_enqueue_isc(io);
1407 /* Performed on the Originating SC, XFER mode only */
1408 case CTL_MSG_DATAMOVE: {
1409 struct ctl_sg_entry *sgl;
1412 io = msg->hdr.original_sc;
1414 printf("%s: original_sc == NULL!\n", __func__);
1415 /* XXX KDM do something here */
1418 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
1419 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1421 * Keep track of this, we need to send it back over
1422 * when the datamove is complete.
1424 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1425 if (msg->hdr.status == CTL_SUCCESS)
1426 io->io_hdr.status = msg->hdr.status;
1428 if (msg->dt.sg_sequence == 0) {
1430 getbinuptime(&io->io_hdr.dma_start_bt);
1432 i = msg->dt.kern_sg_entries +
1433 msg->dt.kern_data_len /
1434 CTL_HA_DATAMOVE_SEGMENT + 1;
1435 sgl = malloc(sizeof(*sgl) * i, M_CTL,
1437 io->io_hdr.remote_sglist = sgl;
1438 io->io_hdr.local_sglist =
1439 &sgl[msg->dt.kern_sg_entries];
1441 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
1443 io->scsiio.kern_sg_entries =
1444 msg->dt.kern_sg_entries;
1445 io->scsiio.rem_sg_entries =
1446 msg->dt.kern_sg_entries;
1447 io->scsiio.kern_data_len =
1448 msg->dt.kern_data_len;
1449 io->scsiio.kern_total_len =
1450 msg->dt.kern_total_len;
1451 io->scsiio.kern_data_resid =
1452 msg->dt.kern_data_resid;
1453 io->scsiio.kern_rel_offset =
1454 msg->dt.kern_rel_offset;
1455 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR;
1456 io->io_hdr.flags |= msg->dt.flags &
1459 sgl = (struct ctl_sg_entry *)
1460 io->scsiio.kern_data_ptr;
1462 for (i = msg->dt.sent_sg_entries, j = 0;
1463 i < (msg->dt.sent_sg_entries +
1464 msg->dt.cur_sg_entries); i++, j++) {
1465 sgl[i].addr = msg->dt.sg_list[j].addr;
1466 sgl[i].len = msg->dt.sg_list[j].len;
1469 printf("%s: DATAMOVE: %p,%lu j=%d, i=%d\n",
1470 __func__, sgl[i].addr, sgl[i].len, j, i);
1475 * If this is the last piece of the I/O, we've got
1476 * the full S/G list. Queue processing in the thread.
1477 * Otherwise wait for the next piece.
1479 if (msg->dt.sg_last != 0)
1480 ctl_enqueue_isc(io);
1483 /* Performed on the Serializing (primary) SC, XFER mode only */
1484 case CTL_MSG_DATAMOVE_DONE: {
1485 if (msg->hdr.serializing_sc == NULL) {
1486 printf("%s: serializing_sc == NULL!\n",
1488 /* XXX KDM now what? */
1492 * We grab the sense information here in case
1493 * there was a failure, so we can return status
1494 * back to the initiator.
1496 io = msg->hdr.serializing_sc;
1497 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
1498 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1499 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1500 io->io_hdr.port_status = msg->scsi.fetd_status;
1501 io->scsiio.residual = msg->scsi.residual;
1502 if (msg->hdr.status != CTL_STATUS_NONE) {
1503 io->io_hdr.status = msg->hdr.status;
1504 io->scsiio.scsi_status = msg->scsi.scsi_status;
1505 io->scsiio.sense_len = msg->scsi.sense_len;
1506 io->scsiio.sense_residual =msg->scsi.sense_residual;
1507 memcpy(&io->scsiio.sense_data,
1508 &msg->scsi.sense_data,
1509 msg->scsi.sense_len);
1510 if (msg->hdr.status == CTL_SUCCESS)
1511 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1513 ctl_enqueue_isc(io);
1517 /* Preformed on Originating SC, SER_ONLY mode */
1519 io = msg->hdr.original_sc;
1521 printf("%s: original_sc == NULL!\n",
1525 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1526 io->io_hdr.msg_type = CTL_MSG_R2R;
1527 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1528 ctl_enqueue_isc(io);
1532 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
1534 * Performed on the Originating (i.e. secondary) SC in XFER
1537 case CTL_MSG_FINISH_IO:
1538 if (softc->ha_mode == CTL_HA_MODE_XFER)
1539 ctl_isc_handler_finish_xfer(softc, msg);
1541 ctl_isc_handler_finish_ser_only(softc, msg);
1544 /* Preformed on Originating SC */
1545 case CTL_MSG_BAD_JUJU:
1546 io = msg->hdr.original_sc;
1548 printf("%s: Bad JUJU!, original_sc is NULL!\n",
1552 ctl_copy_sense_data(msg, io);
1554 * IO should have already been cleaned up on other
1555 * SC so clear this flag so we won't send a message
1556 * back to finish the IO there.
1558 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
1559 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1561 /* io = msg->hdr.serializing_sc; */
1562 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
1563 ctl_enqueue_isc(io);
1566 /* Handle resets sent from the other side */
1567 case CTL_MSG_MANAGE_TASKS: {
1568 struct ctl_taskio *taskio;
1569 taskio = (struct ctl_taskio *)ctl_alloc_io(
1570 softc->othersc_pool);
1571 ctl_zero_io((union ctl_io *)taskio);
1572 taskio->io_hdr.io_type = CTL_IO_TASK;
1573 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1574 taskio->io_hdr.nexus = msg->hdr.nexus;
1575 taskio->task_action = msg->task.task_action;
1576 taskio->tag_num = msg->task.tag_num;
1577 taskio->tag_type = msg->task.tag_type;
1579 taskio->io_hdr.start_time = time_uptime;
1580 getbinuptime(&taskio->io_hdr.start_bt);
1581 #endif /* CTL_TIME_IO */
1582 ctl_run_task((union ctl_io *)taskio);
1585 /* Persistent Reserve action which needs attention */
1586 case CTL_MSG_PERS_ACTION:
1587 presio = (struct ctl_prio *)ctl_alloc_io(
1588 softc->othersc_pool);
1589 ctl_zero_io((union ctl_io *)presio);
1590 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
1591 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1592 presio->io_hdr.nexus = msg->hdr.nexus;
1593 presio->pr_msg = msg->pr;
1594 ctl_enqueue_isc((union ctl_io *)presio);
1597 ctl_isc_ua(softc, msg, param);
1599 case CTL_MSG_PORT_SYNC:
1600 ctl_isc_port_sync(softc, msg, param);
1602 case CTL_MSG_LUN_SYNC:
1603 ctl_isc_lun_sync(softc, msg, param);
1605 case CTL_MSG_IID_SYNC:
1606 ctl_isc_iid_sync(softc, msg, param);
1609 ctl_isc_login(softc, msg, param);
1611 case CTL_MSG_MODE_SYNC:
1612 ctl_isc_mode_sync(softc, msg, param);
1615 printf("Received HA message of unknown type %d\n",
1617 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1622 } else if (event == CTL_HA_EVT_LINK_CHANGE) {
1623 printf("CTL: HA link status changed from %d to %d\n",
1624 softc->ha_link, param);
1625 if (param == softc->ha_link)
1627 if (softc->ha_link == CTL_HA_LINK_ONLINE) {
1628 softc->ha_link = param;
1629 ctl_isc_ha_link_down(softc);
1631 softc->ha_link = param;
1632 if (softc->ha_link == CTL_HA_LINK_ONLINE)
1633 ctl_isc_ha_link_up(softc);
1637 printf("ctl_isc_event_handler: Unknown event %d\n", event);
1643 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
1646 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data,
1647 src->scsi.sense_len);
1648 dest->scsiio.scsi_status = src->scsi.scsi_status;
1649 dest->scsiio.sense_len = src->scsi.sense_len;
1650 dest->io_hdr.status = src->hdr.status;
1654 ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest)
1657 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data,
1658 src->scsiio.sense_len);
1659 dest->scsi.scsi_status = src->scsiio.scsi_status;
1660 dest->scsi.sense_len = src->scsiio.sense_len;
1661 dest->hdr.status = src->io_hdr.status;
1665 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1667 struct ctl_softc *softc = lun->ctl_softc;
1670 if (initidx < softc->init_min || initidx >= softc->init_max)
1672 mtx_assert(&lun->lun_lock, MA_OWNED);
1673 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1676 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua;
1680 ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua)
1684 mtx_assert(&lun->lun_lock, MA_OWNED);
1685 if (lun->pending_ua[port] == NULL)
1687 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1688 if (port * CTL_MAX_INIT_PER_PORT + i == except)
1690 lun->pending_ua[port][i] |= ua;
1695 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1697 struct ctl_softc *softc = lun->ctl_softc;
1700 mtx_assert(&lun->lun_lock, MA_OWNED);
1701 for (i = softc->port_min; i < softc->port_max; i++)
1702 ctl_est_ua_port(lun, i, except, ua);
1706 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1708 struct ctl_softc *softc = lun->ctl_softc;
1711 if (initidx < softc->init_min || initidx >= softc->init_max)
1713 mtx_assert(&lun->lun_lock, MA_OWNED);
1714 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1717 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua;
1721 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1723 struct ctl_softc *softc = lun->ctl_softc;
1726 mtx_assert(&lun->lun_lock, MA_OWNED);
1727 for (i = softc->port_min; i < softc->port_max; i++) {
1728 if (lun->pending_ua[i] == NULL)
1730 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
1731 if (i * CTL_MAX_INIT_PER_PORT + j == except)
1733 lun->pending_ua[i][j] &= ~ua;
1739 ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx,
1740 ctl_ua_type ua_type)
1742 struct ctl_lun *lun;
1744 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
1745 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) {
1746 mtx_lock(&lun->lun_lock);
1747 ctl_clr_ua(lun, initidx, ua_type);
1748 mtx_unlock(&lun->lun_lock);
1753 ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS)
1755 struct ctl_softc *softc = (struct ctl_softc *)arg1;
1756 struct ctl_lun *lun;
1757 struct ctl_lun_req ireq;
1760 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1;
1761 error = sysctl_handle_int(oidp, &value, 0, req);
1762 if ((error != 0) || (req->newptr == NULL))
1765 mtx_lock(&softc->ctl_lock);
1767 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1769 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
1770 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1771 mtx_unlock(&softc->ctl_lock);
1772 bzero(&ireq, sizeof(ireq));
1773 ireq.reqtype = CTL_LUNREQ_MODIFY;
1774 ireq.reqdata.modify.lun_id = lun->lun;
1775 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0,
1777 if (ireq.status != CTL_LUN_OK) {
1778 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n",
1779 __func__, ireq.status, ireq.error_str);
1781 mtx_lock(&softc->ctl_lock);
1783 mtx_unlock(&softc->ctl_lock);
1790 struct make_dev_args args;
1791 struct ctl_softc *softc;
1795 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1798 make_dev_args_init(&args);
1799 args.mda_devsw = &ctl_cdevsw;
1800 args.mda_uid = UID_ROOT;
1801 args.mda_gid = GID_OPERATOR;
1802 args.mda_mode = 0600;
1803 args.mda_si_drv1 = softc;
1804 error = make_dev_s(&args, &softc->dev, "cam/ctl");
1806 free(softc, M_DEVBUF);
1807 control_softc = NULL;
1811 sysctl_ctx_init(&softc->sysctl_ctx);
1812 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1813 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1814 CTLFLAG_RD, 0, "CAM Target Layer");
1816 if (softc->sysctl_tree == NULL) {
1817 printf("%s: unable to allocate sysctl tree\n", __func__);
1818 destroy_dev(softc->dev);
1819 free(softc, M_DEVBUF);
1820 control_softc = NULL;
1824 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1825 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
1826 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1829 TUNABLE_INT_FETCH("kern.cam.ctl.ha_mode", (int *)&softc->ha_mode);
1830 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1831 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0,
1832 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)");
1835 * In Copan's HA scheme, the "master" and "slave" roles are
1836 * figured out through the slot the controller is in. Although it
1837 * is an active/active system, someone has to be in charge.
1839 TUNABLE_INT_FETCH("kern.cam.ctl.ha_id", &softc->ha_id);
1840 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1841 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1842 "HA head ID (0 - no HA)");
1843 if (softc->ha_id == 0 || softc->ha_id > NUM_HA_SHELVES) {
1844 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1845 softc->is_single = 1;
1846 softc->port_cnt = CTL_MAX_PORTS;
1847 softc->port_min = 0;
1849 softc->port_cnt = CTL_MAX_PORTS / NUM_HA_SHELVES;
1850 softc->port_min = (softc->ha_id - 1) * softc->port_cnt;
1852 softc->port_max = softc->port_min + softc->port_cnt;
1853 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT;
1854 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT;
1856 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1857 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0,
1858 "HA link state (0 - offline, 1 - unknown, 2 - online)");
1860 STAILQ_INIT(&softc->lun_list);
1861 STAILQ_INIT(&softc->pending_lun_queue);
1862 STAILQ_INIT(&softc->fe_list);
1863 STAILQ_INIT(&softc->port_list);
1864 STAILQ_INIT(&softc->be_list);
1865 ctl_tpc_init(softc);
1867 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
1870 printf("ctl: can't allocate %d entry other SC pool, "
1871 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1874 softc->othersc_pool = other_pool;
1876 if (worker_threads <= 0)
1877 worker_threads = max(1, mp_ncpus / 4);
1878 if (worker_threads > CTL_MAX_THREADS)
1879 worker_threads = CTL_MAX_THREADS;
1881 for (i = 0; i < worker_threads; i++) {
1882 struct ctl_thread *thr = &softc->threads[i];
1884 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1885 thr->ctl_softc = softc;
1886 STAILQ_INIT(&thr->incoming_queue);
1887 STAILQ_INIT(&thr->rtr_queue);
1888 STAILQ_INIT(&thr->done_queue);
1889 STAILQ_INIT(&thr->isc_queue);
1891 error = kproc_kthread_add(ctl_work_thread, thr,
1892 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1894 printf("error creating CTL work thread!\n");
1895 ctl_pool_free(other_pool);
1899 error = kproc_kthread_add(ctl_lun_thread, softc,
1900 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
1902 printf("error creating CTL lun thread!\n");
1903 ctl_pool_free(other_pool);
1906 error = kproc_kthread_add(ctl_thresh_thread, softc,
1907 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh");
1909 printf("error creating CTL threshold thread!\n");
1910 ctl_pool_free(other_pool);
1914 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1915 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN,
1916 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head");
1918 if (softc->is_single == 0) {
1919 ctl_frontend_register(&ha_frontend);
1920 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) {
1921 printf("ctl_init: ctl_ha_msg_init failed.\n");
1922 softc->is_single = 1;
1924 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
1925 != CTL_HA_STATUS_SUCCESS) {
1926 printf("ctl_init: ctl_ha_msg_register failed.\n");
1927 softc->is_single = 1;
1936 struct ctl_softc *softc = control_softc;
1937 struct ctl_lun *lun, *next_lun;
1939 if (softc->is_single == 0) {
1940 ctl_ha_msg_shutdown(softc);
1941 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL)
1942 != CTL_HA_STATUS_SUCCESS)
1943 printf("%s: ctl_ha_msg_deregister failed.\n", __func__);
1944 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS)
1945 printf("%s: ctl_ha_msg_destroy failed.\n", __func__);
1946 ctl_frontend_deregister(&ha_frontend);
1949 mtx_lock(&softc->ctl_lock);
1951 STAILQ_FOREACH_SAFE(lun, &softc->lun_list, links, next_lun)
1954 mtx_unlock(&softc->ctl_lock);
1957 ctl_shutdown_thread(softc->work_thread);
1958 mtx_destroy(&softc->queue_lock);
1961 ctl_tpc_shutdown(softc);
1962 uma_zdestroy(softc->io_zone);
1963 mtx_destroy(&softc->ctl_lock);
1965 destroy_dev(softc->dev);
1967 sysctl_ctx_free(&softc->sysctl_ctx);
1969 free(softc, M_DEVBUF);
1970 control_softc = NULL;
1974 ctl_module_event_handler(module_t mod, int what, void *arg)
1979 return (ctl_init());
1983 return (EOPNOTSUPP);
1988 * XXX KDM should we do some access checks here? Bump a reference count to
1989 * prevent a CTL module from being unloaded while someone has it open?
1992 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1998 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
2004 * Remove an initiator by port number and initiator ID.
2005 * Returns 0 for success, -1 for failure.
2008 ctl_remove_initiator(struct ctl_port *port, int iid)
2010 struct ctl_softc *softc = port->ctl_softc;
2012 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2014 if (iid > CTL_MAX_INIT_PER_PORT) {
2015 printf("%s: initiator ID %u > maximun %u!\n",
2016 __func__, iid, CTL_MAX_INIT_PER_PORT);
2020 mtx_lock(&softc->ctl_lock);
2021 port->wwpn_iid[iid].in_use--;
2022 port->wwpn_iid[iid].last_use = time_uptime;
2023 mtx_unlock(&softc->ctl_lock);
2024 ctl_isc_announce_iid(port, iid);
2030 * Add an initiator to the initiator map.
2031 * Returns iid for success, < 0 for failure.
2034 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
2036 struct ctl_softc *softc = port->ctl_softc;
2040 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2042 if (iid >= CTL_MAX_INIT_PER_PORT) {
2043 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
2044 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
2049 mtx_lock(&softc->ctl_lock);
2051 if (iid < 0 && (wwpn != 0 || name != NULL)) {
2052 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2053 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
2057 if (name != NULL && port->wwpn_iid[i].name != NULL &&
2058 strcmp(name, port->wwpn_iid[i].name) == 0) {
2066 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2067 if (port->wwpn_iid[i].in_use == 0 &&
2068 port->wwpn_iid[i].wwpn == 0 &&
2069 port->wwpn_iid[i].name == NULL) {
2078 best_time = INT32_MAX;
2079 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2080 if (port->wwpn_iid[i].in_use == 0) {
2081 if (port->wwpn_iid[i].last_use < best_time) {
2083 best_time = port->wwpn_iid[i].last_use;
2091 mtx_unlock(&softc->ctl_lock);
2096 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
2098 * This is not an error yet.
2100 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
2102 printf("%s: port %d iid %u WWPN %#jx arrived"
2103 " again\n", __func__, port->targ_port,
2104 iid, (uintmax_t)wwpn);
2108 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
2109 strcmp(name, port->wwpn_iid[iid].name) == 0) {
2111 printf("%s: port %d iid %u name '%s' arrived"
2112 " again\n", __func__, port->targ_port,
2119 * This is an error, but what do we do about it? The
2120 * driver is telling us we have a new WWPN for this
2121 * initiator ID, so we pretty much need to use it.
2123 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
2124 " but WWPN %#jx '%s' is still at that address\n",
2125 __func__, port->targ_port, iid, wwpn, name,
2126 (uintmax_t)port->wwpn_iid[iid].wwpn,
2127 port->wwpn_iid[iid].name);
2130 * XXX KDM clear have_ca and ua_pending on each LUN for
2135 free(port->wwpn_iid[iid].name, M_CTL);
2136 port->wwpn_iid[iid].name = name;
2137 port->wwpn_iid[iid].wwpn = wwpn;
2138 port->wwpn_iid[iid].in_use++;
2139 mtx_unlock(&softc->ctl_lock);
2140 ctl_isc_announce_iid(port, iid);
2146 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
2150 switch (port->port_type) {
2153 struct scsi_transportid_fcp *id =
2154 (struct scsi_transportid_fcp *)buf;
2155 if (port->wwpn_iid[iid].wwpn == 0)
2157 memset(id, 0, sizeof(*id));
2158 id->format_protocol = SCSI_PROTO_FC;
2159 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
2160 return (sizeof(*id));
2162 case CTL_PORT_ISCSI:
2164 struct scsi_transportid_iscsi_port *id =
2165 (struct scsi_transportid_iscsi_port *)buf;
2166 if (port->wwpn_iid[iid].name == NULL)
2169 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
2171 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
2172 len = roundup2(min(len, 252), 4);
2173 scsi_ulto2b(len, id->additional_length);
2174 return (sizeof(*id) + len);
2178 struct scsi_transportid_sas *id =
2179 (struct scsi_transportid_sas *)buf;
2180 if (port->wwpn_iid[iid].wwpn == 0)
2182 memset(id, 0, sizeof(*id));
2183 id->format_protocol = SCSI_PROTO_SAS;
2184 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
2185 return (sizeof(*id));
2189 struct scsi_transportid_spi *id =
2190 (struct scsi_transportid_spi *)buf;
2191 memset(id, 0, sizeof(*id));
2192 id->format_protocol = SCSI_PROTO_SPI;
2193 scsi_ulto2b(iid, id->scsi_addr);
2194 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
2195 return (sizeof(*id));
2201 * Serialize a command that went down the "wrong" side, and so was sent to
2202 * this controller for execution. The logic is a little different than the
2203 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
2204 * sent back to the other side, but in the success case, we execute the
2205 * command on this side (XFER mode) or tell the other side to execute it
2209 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
2211 struct ctl_softc *softc = CTL_SOFTC(ctsio);
2212 struct ctl_port *port = CTL_PORT(ctsio);
2213 union ctl_ha_msg msg_info;
2214 struct ctl_lun *lun;
2215 const struct ctl_cmd_entry *entry;
2218 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
2220 /* Make sure that we know about this port. */
2221 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) {
2222 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2227 /* Make sure that we know about this LUN. */
2228 mtx_lock(&softc->ctl_lock);
2229 if (targ_lun >= CTL_MAX_LUNS ||
2230 (lun = softc->ctl_luns[targ_lun]) == NULL) {
2231 mtx_unlock(&softc->ctl_lock);
2234 * The other node would not send this request to us unless
2235 * received announce that we are primary node for this LUN.
2236 * If this LUN does not exist now, it is probably result of
2237 * a race, so respond to initiator in the most opaque way.
2239 ctl_set_busy(ctsio);
2242 mtx_lock(&lun->lun_lock);
2243 mtx_unlock(&softc->ctl_lock);
2246 * If the LUN is invalid, pretend that it doesn't exist.
2247 * It will go away as soon as all pending I/Os completed.
2249 if (lun->flags & CTL_LUN_DISABLED) {
2250 mtx_unlock(&lun->lun_lock);
2251 ctl_set_busy(ctsio);
2255 entry = ctl_get_cmd_entry(ctsio, NULL);
2256 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
2257 mtx_unlock(&lun->lun_lock);
2261 CTL_LUN(ctsio) = lun;
2262 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
2265 * Every I/O goes into the OOA queue for a
2266 * particular LUN, and stays there until completion.
2269 if (TAILQ_EMPTY(&lun->ooa_queue))
2270 lun->idle_time += getsbinuptime() - lun->last_busy;
2272 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2274 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
2275 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
2277 case CTL_ACTION_BLOCK:
2278 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
2279 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
2281 mtx_unlock(&lun->lun_lock);
2283 case CTL_ACTION_PASS:
2284 case CTL_ACTION_SKIP:
2285 if (softc->ha_mode == CTL_HA_MODE_XFER) {
2286 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
2287 ctl_enqueue_rtr((union ctl_io *)ctsio);
2288 mtx_unlock(&lun->lun_lock);
2290 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
2291 mtx_unlock(&lun->lun_lock);
2293 /* send msg back to other side */
2294 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2295 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
2296 msg_info.hdr.msg_type = CTL_MSG_R2R;
2297 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2298 sizeof(msg_info.hdr), M_WAITOK);
2301 case CTL_ACTION_OVERLAP:
2302 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2303 mtx_unlock(&lun->lun_lock);
2304 ctl_set_overlapped_cmd(ctsio);
2306 case CTL_ACTION_OVERLAP_TAG:
2307 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2308 mtx_unlock(&lun->lun_lock);
2309 ctl_set_overlapped_tag(ctsio, ctsio->tag_num);
2311 case CTL_ACTION_ERROR:
2313 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2314 mtx_unlock(&lun->lun_lock);
2316 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2319 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
2320 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2321 msg_info.hdr.serializing_sc = NULL;
2322 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
2323 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2324 sizeof(msg_info.scsi), M_WAITOK);
2325 ctl_free_io((union ctl_io *)ctsio);
2331 * Returns 0 for success, errno for failure.
2334 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2335 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2339 mtx_lock(&lun->lun_lock);
2340 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2341 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2343 struct ctl_ooa_entry *entry;
2346 * If we've got more than we can fit, just count the
2347 * remaining entries.
2349 if (*cur_fill_num >= ooa_hdr->alloc_num)
2352 entry = &kern_entries[*cur_fill_num];
2354 entry->tag_num = io->scsiio.tag_num;
2355 entry->lun_num = lun->lun;
2357 entry->start_bt = io->io_hdr.start_bt;
2359 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2360 entry->cdb_len = io->scsiio.cdb_len;
2361 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2362 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2364 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2365 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2367 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2368 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2370 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2371 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2373 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2374 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2376 mtx_unlock(&lun->lun_lock);
2380 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2381 size_t error_str_len)
2385 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2387 if (copyin(user_addr, kptr, len) != 0) {
2388 snprintf(error_str, error_str_len, "Error copying %d bytes "
2389 "from user address %p to kernel address %p", len,
2399 ctl_free_args(int num_args, struct ctl_be_arg *args)
2406 for (i = 0; i < num_args; i++) {
2407 free(args[i].kname, M_CTL);
2408 free(args[i].kvalue, M_CTL);
2414 static struct ctl_be_arg *
2415 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
2416 char *error_str, size_t error_str_len)
2418 struct ctl_be_arg *args;
2421 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
2422 error_str, error_str_len);
2427 for (i = 0; i < num_args; i++) {
2428 args[i].kname = NULL;
2429 args[i].kvalue = NULL;
2432 for (i = 0; i < num_args; i++) {
2435 args[i].kname = ctl_copyin_alloc(args[i].name,
2436 args[i].namelen, error_str, error_str_len);
2437 if (args[i].kname == NULL)
2440 if (args[i].kname[args[i].namelen - 1] != '\0') {
2441 snprintf(error_str, error_str_len, "Argument %d "
2442 "name is not NUL-terminated", i);
2446 if (args[i].flags & CTL_BEARG_RD) {
2447 tmpptr = ctl_copyin_alloc(args[i].value,
2448 args[i].vallen, error_str, error_str_len);
2451 if ((args[i].flags & CTL_BEARG_ASCII)
2452 && (tmpptr[args[i].vallen - 1] != '\0')) {
2453 snprintf(error_str, error_str_len, "Argument "
2454 "%d value is not NUL-terminated", i);
2455 free(tmpptr, M_CTL);
2458 args[i].kvalue = tmpptr;
2460 args[i].kvalue = malloc(args[i].vallen,
2461 M_CTL, M_WAITOK | M_ZERO);
2468 ctl_free_args(num_args, args);
2474 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2478 for (i = 0; i < num_args; i++) {
2479 if (args[i].flags & CTL_BEARG_WR)
2480 copyout(args[i].kvalue, args[i].value, args[i].vallen);
2485 * Escape characters that are illegal or not recommended in XML.
2488 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2490 char *end = str + size;
2495 for (; *str && str < end; str++) {
2498 retval = sbuf_printf(sb, "&");
2501 retval = sbuf_printf(sb, ">");
2504 retval = sbuf_printf(sb, "<");
2507 retval = sbuf_putc(sb, *str);
2520 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2522 struct scsi_vpd_id_descriptor *desc;
2525 if (id == NULL || id->len < 4)
2527 desc = (struct scsi_vpd_id_descriptor *)id->data;
2528 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2529 case SVPD_ID_TYPE_T10:
2530 sbuf_printf(sb, "t10.");
2532 case SVPD_ID_TYPE_EUI64:
2533 sbuf_printf(sb, "eui.");
2535 case SVPD_ID_TYPE_NAA:
2536 sbuf_printf(sb, "naa.");
2538 case SVPD_ID_TYPE_SCSI_NAME:
2541 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2542 case SVPD_ID_CODESET_BINARY:
2543 for (i = 0; i < desc->length; i++)
2544 sbuf_printf(sb, "%02x", desc->identifier[i]);
2546 case SVPD_ID_CODESET_ASCII:
2547 sbuf_printf(sb, "%.*s", (int)desc->length,
2548 (char *)desc->identifier);
2550 case SVPD_ID_CODESET_UTF8:
2551 sbuf_printf(sb, "%s", (char *)desc->identifier);
2557 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2560 struct ctl_softc *softc = dev->si_drv1;
2561 struct ctl_lun *lun;
2568 retval = ctl_ioctl_io(dev, cmd, addr, flag, td);
2570 case CTL_ENABLE_PORT:
2571 case CTL_DISABLE_PORT:
2572 case CTL_SET_PORT_WWNS: {
2573 struct ctl_port *port;
2574 struct ctl_port_entry *entry;
2576 entry = (struct ctl_port_entry *)addr;
2578 mtx_lock(&softc->ctl_lock);
2579 STAILQ_FOREACH(port, &softc->port_list, links) {
2582 if (port->targ_port < softc->port_min ||
2583 port->targ_port >= softc->port_max)
2588 if ((entry->port_type == CTL_PORT_NONE)
2589 && (entry->targ_port == port->targ_port)) {
2591 * If the user only wants to enable or
2592 * disable or set WWNs on a specific port,
2593 * do the operation and we're done.
2597 } else if (entry->port_type & port->port_type) {
2599 * Compare the user's type mask with the
2600 * particular frontend type to see if we
2607 * Make sure the user isn't trying to set
2608 * WWNs on multiple ports at the same time.
2610 if (cmd == CTL_SET_PORT_WWNS) {
2611 printf("%s: Can't set WWNs on "
2612 "multiple ports\n", __func__);
2621 * XXX KDM we have to drop the lock here, because
2622 * the online/offline operations can potentially
2623 * block. We need to reference count the frontends
2624 * so they can't go away,
2626 if (cmd == CTL_ENABLE_PORT) {
2627 mtx_unlock(&softc->ctl_lock);
2628 ctl_port_online(port);
2629 mtx_lock(&softc->ctl_lock);
2630 } else if (cmd == CTL_DISABLE_PORT) {
2631 mtx_unlock(&softc->ctl_lock);
2632 ctl_port_offline(port);
2633 mtx_lock(&softc->ctl_lock);
2634 } else if (cmd == CTL_SET_PORT_WWNS) {
2635 ctl_port_set_wwns(port,
2636 (entry->flags & CTL_PORT_WWNN_VALID) ?
2638 (entry->flags & CTL_PORT_WWPN_VALID) ?
2639 1 : 0, entry->wwpn);
2644 mtx_unlock(&softc->ctl_lock);
2648 struct ctl_ooa *ooa_hdr;
2649 struct ctl_ooa_entry *entries;
2650 uint32_t cur_fill_num;
2652 ooa_hdr = (struct ctl_ooa *)addr;
2654 if ((ooa_hdr->alloc_len == 0)
2655 || (ooa_hdr->alloc_num == 0)) {
2656 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2657 "must be non-zero\n", __func__,
2658 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2663 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2664 sizeof(struct ctl_ooa_entry))) {
2665 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2666 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2667 __func__, ooa_hdr->alloc_len,
2668 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2673 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2674 if (entries == NULL) {
2675 printf("%s: could not allocate %d bytes for OOA "
2676 "dump\n", __func__, ooa_hdr->alloc_len);
2681 mtx_lock(&softc->ctl_lock);
2682 if ((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0 &&
2683 (ooa_hdr->lun_num >= CTL_MAX_LUNS ||
2684 softc->ctl_luns[ooa_hdr->lun_num] == NULL)) {
2685 mtx_unlock(&softc->ctl_lock);
2686 free(entries, M_CTL);
2687 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2688 __func__, (uintmax_t)ooa_hdr->lun_num);
2695 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2696 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2697 ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2701 lun = softc->ctl_luns[ooa_hdr->lun_num];
2702 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr,
2705 mtx_unlock(&softc->ctl_lock);
2707 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2708 ooa_hdr->fill_len = ooa_hdr->fill_num *
2709 sizeof(struct ctl_ooa_entry);
2710 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2712 printf("%s: error copying out %d bytes for OOA dump\n",
2713 __func__, ooa_hdr->fill_len);
2716 getbinuptime(&ooa_hdr->cur_bt);
2718 if (cur_fill_num > ooa_hdr->alloc_num) {
2719 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2720 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2722 ooa_hdr->dropped_num = 0;
2723 ooa_hdr->status = CTL_OOA_OK;
2726 free(entries, M_CTL);
2729 case CTL_DELAY_IO: {
2730 struct ctl_io_delay_info *delay_info;
2732 delay_info = (struct ctl_io_delay_info *)addr;
2735 mtx_lock(&softc->ctl_lock);
2736 if (delay_info->lun_id >= CTL_MAX_LUNS ||
2737 (lun = softc->ctl_luns[delay_info->lun_id]) == NULL) {
2738 mtx_unlock(&softc->ctl_lock);
2739 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2742 mtx_lock(&lun->lun_lock);
2743 mtx_unlock(&softc->ctl_lock);
2744 delay_info->status = CTL_DELAY_STATUS_OK;
2745 switch (delay_info->delay_type) {
2746 case CTL_DELAY_TYPE_CONT:
2747 case CTL_DELAY_TYPE_ONESHOT:
2750 delay_info->status = CTL_DELAY_STATUS_INVALID_TYPE;
2753 switch (delay_info->delay_loc) {
2754 case CTL_DELAY_LOC_DATAMOVE:
2755 lun->delay_info.datamove_type = delay_info->delay_type;
2756 lun->delay_info.datamove_delay = delay_info->delay_secs;
2758 case CTL_DELAY_LOC_DONE:
2759 lun->delay_info.done_type = delay_info->delay_type;
2760 lun->delay_info.done_delay = delay_info->delay_secs;
2763 delay_info->status = CTL_DELAY_STATUS_INVALID_LOC;
2766 mtx_unlock(&lun->lun_lock);
2768 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2769 #endif /* CTL_IO_DELAY */
2772 case CTL_GETSTATS: {
2773 struct ctl_stats *stats;
2776 stats = (struct ctl_stats *)addr;
2778 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2780 stats->status = CTL_SS_NEED_MORE_SPACE;
2781 stats->num_luns = softc->num_luns;
2785 * XXX KDM no locking here. If the LUN list changes,
2786 * things can blow up.
2789 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2790 retval = copyout(&lun->stats, &stats->lun_stats[i++],
2791 sizeof(lun->stats));
2795 stats->num_luns = softc->num_luns;
2796 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2798 stats->status = CTL_SS_OK;
2800 stats->flags = CTL_STATS_FLAG_TIME_VALID;
2802 stats->flags = CTL_STATS_FLAG_NONE;
2804 getnanouptime(&stats->timestamp);
2807 case CTL_ERROR_INJECT: {
2808 struct ctl_error_desc *err_desc, *new_err_desc;
2810 err_desc = (struct ctl_error_desc *)addr;
2812 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2814 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2816 mtx_lock(&softc->ctl_lock);
2817 if (err_desc->lun_id >= CTL_MAX_LUNS ||
2818 (lun = softc->ctl_luns[err_desc->lun_id]) == NULL) {
2819 mtx_unlock(&softc->ctl_lock);
2820 free(new_err_desc, M_CTL);
2821 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2822 __func__, (uintmax_t)err_desc->lun_id);
2826 mtx_lock(&lun->lun_lock);
2827 mtx_unlock(&softc->ctl_lock);
2830 * We could do some checking here to verify the validity
2831 * of the request, but given the complexity of error
2832 * injection requests, the checking logic would be fairly
2835 * For now, if the request is invalid, it just won't get
2836 * executed and might get deleted.
2838 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2841 * XXX KDM check to make sure the serial number is unique,
2842 * in case we somehow manage to wrap. That shouldn't
2843 * happen for a very long time, but it's the right thing to
2846 new_err_desc->serial = lun->error_serial;
2847 err_desc->serial = lun->error_serial;
2848 lun->error_serial++;
2850 mtx_unlock(&lun->lun_lock);
2853 case CTL_ERROR_INJECT_DELETE: {
2854 struct ctl_error_desc *delete_desc, *desc, *desc2;
2857 delete_desc = (struct ctl_error_desc *)addr;
2860 mtx_lock(&softc->ctl_lock);
2861 if (delete_desc->lun_id >= CTL_MAX_LUNS ||
2862 (lun = softc->ctl_luns[delete_desc->lun_id]) == NULL) {
2863 mtx_unlock(&softc->ctl_lock);
2864 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2865 __func__, (uintmax_t)delete_desc->lun_id);
2869 mtx_lock(&lun->lun_lock);
2870 mtx_unlock(&softc->ctl_lock);
2871 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2872 if (desc->serial != delete_desc->serial)
2875 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2880 mtx_unlock(&lun->lun_lock);
2881 if (delete_done == 0) {
2882 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2883 "error serial %ju on LUN %u\n", __func__,
2884 delete_desc->serial, delete_desc->lun_id);
2890 case CTL_DUMP_STRUCTS: {
2892 struct ctl_port *port;
2893 struct ctl_frontend *fe;
2895 mtx_lock(&softc->ctl_lock);
2896 printf("CTL Persistent Reservation information start:\n");
2897 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2898 mtx_lock(&lun->lun_lock);
2899 if ((lun->flags & CTL_LUN_DISABLED) != 0) {
2900 mtx_unlock(&lun->lun_lock);
2904 for (j = 0; j < CTL_MAX_PORTS; j++) {
2905 if (lun->pr_keys[j] == NULL)
2907 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2908 if (lun->pr_keys[j][k] == 0)
2910 printf(" LUN %ju port %d iid %d key "
2911 "%#jx\n", lun->lun, j, k,
2912 (uintmax_t)lun->pr_keys[j][k]);
2915 mtx_unlock(&lun->lun_lock);
2917 printf("CTL Persistent Reservation information end\n");
2918 printf("CTL Ports:\n");
2919 STAILQ_FOREACH(port, &softc->port_list, links) {
2920 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
2921 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
2922 port->frontend->name, port->port_type,
2923 port->physical_port, port->virtual_port,
2924 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
2925 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2926 if (port->wwpn_iid[j].in_use == 0 &&
2927 port->wwpn_iid[j].wwpn == 0 &&
2928 port->wwpn_iid[j].name == NULL)
2931 printf(" iid %u use %d WWPN %#jx '%s'\n",
2932 j, port->wwpn_iid[j].in_use,
2933 (uintmax_t)port->wwpn_iid[j].wwpn,
2934 port->wwpn_iid[j].name);
2937 printf("CTL Port information end\n");
2938 mtx_unlock(&softc->ctl_lock);
2940 * XXX KDM calling this without a lock. We'd likely want
2941 * to drop the lock before calling the frontend's dump
2944 printf("CTL Frontends:\n");
2945 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2946 printf(" Frontend '%s'\n", fe->name);
2947 if (fe->fe_dump != NULL)
2950 printf("CTL Frontend information end\n");
2954 struct ctl_lun_req *lun_req;
2955 struct ctl_backend_driver *backend;
2957 lun_req = (struct ctl_lun_req *)addr;
2959 backend = ctl_backend_find(lun_req->backend);
2960 if (backend == NULL) {
2961 lun_req->status = CTL_LUN_ERROR;
2962 snprintf(lun_req->error_str,
2963 sizeof(lun_req->error_str),
2964 "Backend \"%s\" not found.",
2968 if (lun_req->num_be_args > 0) {
2969 lun_req->kern_be_args = ctl_copyin_args(
2970 lun_req->num_be_args,
2973 sizeof(lun_req->error_str));
2974 if (lun_req->kern_be_args == NULL) {
2975 lun_req->status = CTL_LUN_ERROR;
2980 retval = backend->ioctl(dev, cmd, addr, flag, td);
2982 if (lun_req->num_be_args > 0) {
2983 ctl_copyout_args(lun_req->num_be_args,
2984 lun_req->kern_be_args);
2985 ctl_free_args(lun_req->num_be_args,
2986 lun_req->kern_be_args);
2990 case CTL_LUN_LIST: {
2992 struct ctl_lun_list *list;
2993 struct ctl_option *opt;
2995 list = (struct ctl_lun_list *)addr;
2998 * Allocate a fixed length sbuf here, based on the length
2999 * of the user's buffer. We could allocate an auto-extending
3000 * buffer, and then tell the user how much larger our
3001 * amount of data is than his buffer, but that presents
3004 * 1. The sbuf(9) routines use a blocking malloc, and so
3005 * we can't hold a lock while calling them with an
3006 * auto-extending buffer.
3008 * 2. There is not currently a LUN reference counting
3009 * mechanism, outside of outstanding transactions on
3010 * the LUN's OOA queue. So a LUN could go away on us
3011 * while we're getting the LUN number, backend-specific
3012 * information, etc. Thus, given the way things
3013 * currently work, we need to hold the CTL lock while
3014 * grabbing LUN information.
3016 * So, from the user's standpoint, the best thing to do is
3017 * allocate what he thinks is a reasonable buffer length,
3018 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3019 * double the buffer length and try again. (And repeat
3020 * that until he succeeds.)
3022 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3024 list->status = CTL_LUN_LIST_ERROR;
3025 snprintf(list->error_str, sizeof(list->error_str),
3026 "Unable to allocate %d bytes for LUN list",
3031 sbuf_printf(sb, "<ctllunlist>\n");
3033 mtx_lock(&softc->ctl_lock);
3034 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3035 mtx_lock(&lun->lun_lock);
3036 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3037 (uintmax_t)lun->lun);
3040 * Bail out as soon as we see that we've overfilled
3046 retval = sbuf_printf(sb, "\t<backend_type>%s"
3047 "</backend_type>\n",
3048 (lun->backend == NULL) ? "none" :
3049 lun->backend->name);
3054 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3055 lun->be_lun->lun_type);
3060 if (lun->backend == NULL) {
3061 retval = sbuf_printf(sb, "</lun>\n");
3067 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3068 (lun->be_lun->maxlba > 0) ?
3069 lun->be_lun->maxlba + 1 : 0);
3074 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3075 lun->be_lun->blocksize);
3080 retval = sbuf_printf(sb, "\t<serial_number>");
3085 retval = ctl_sbuf_printf_esc(sb,
3086 lun->be_lun->serial_num,
3087 sizeof(lun->be_lun->serial_num));
3092 retval = sbuf_printf(sb, "</serial_number>\n");
3097 retval = sbuf_printf(sb, "\t<device_id>");
3102 retval = ctl_sbuf_printf_esc(sb,
3103 lun->be_lun->device_id,
3104 sizeof(lun->be_lun->device_id));
3109 retval = sbuf_printf(sb, "</device_id>\n");
3114 if (lun->backend->lun_info != NULL) {
3115 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3119 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3120 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3121 opt->name, opt->value, opt->name);
3126 retval = sbuf_printf(sb, "</lun>\n");
3130 mtx_unlock(&lun->lun_lock);
3133 mtx_unlock(&lun->lun_lock);
3134 mtx_unlock(&softc->ctl_lock);
3137 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3140 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3141 snprintf(list->error_str, sizeof(list->error_str),
3142 "Out of space, %d bytes is too small",
3149 retval = copyout(sbuf_data(sb), list->lun_xml,
3152 list->fill_len = sbuf_len(sb) + 1;
3153 list->status = CTL_LUN_LIST_OK;
3158 struct ctl_iscsi *ci;
3159 struct ctl_frontend *fe;
3161 ci = (struct ctl_iscsi *)addr;
3163 fe = ctl_frontend_find("iscsi");
3165 ci->status = CTL_ISCSI_ERROR;
3166 snprintf(ci->error_str, sizeof(ci->error_str),
3167 "Frontend \"iscsi\" not found.");
3171 retval = fe->ioctl(dev, cmd, addr, flag, td);
3174 case CTL_PORT_REQ: {
3175 struct ctl_req *req;
3176 struct ctl_frontend *fe;
3178 req = (struct ctl_req *)addr;
3180 fe = ctl_frontend_find(req->driver);
3182 req->status = CTL_LUN_ERROR;
3183 snprintf(req->error_str, sizeof(req->error_str),
3184 "Frontend \"%s\" not found.", req->driver);
3187 if (req->num_args > 0) {
3188 req->kern_args = ctl_copyin_args(req->num_args,
3189 req->args, req->error_str, sizeof(req->error_str));
3190 if (req->kern_args == NULL) {
3191 req->status = CTL_LUN_ERROR;
3197 retval = fe->ioctl(dev, cmd, addr, flag, td);
3201 if (req->num_args > 0) {
3202 ctl_copyout_args(req->num_args, req->kern_args);
3203 ctl_free_args(req->num_args, req->kern_args);
3207 case CTL_PORT_LIST: {
3209 struct ctl_port *port;
3210 struct ctl_lun_list *list;
3211 struct ctl_option *opt;
3215 list = (struct ctl_lun_list *)addr;
3217 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3219 list->status = CTL_LUN_LIST_ERROR;
3220 snprintf(list->error_str, sizeof(list->error_str),
3221 "Unable to allocate %d bytes for LUN list",
3226 sbuf_printf(sb, "<ctlportlist>\n");
3228 mtx_lock(&softc->ctl_lock);
3229 STAILQ_FOREACH(port, &softc->port_list, links) {
3230 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3231 (uintmax_t)port->targ_port);
3234 * Bail out as soon as we see that we've overfilled
3240 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3241 "</frontend_type>\n", port->frontend->name);
3245 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3250 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3251 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3255 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3260 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3261 port->physical_port);
3265 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3266 port->virtual_port);
3270 if (port->target_devid != NULL) {
3271 sbuf_printf(sb, "\t<target>");
3272 ctl_id_sbuf(port->target_devid, sb);
3273 sbuf_printf(sb, "</target>\n");
3276 if (port->port_devid != NULL) {
3277 sbuf_printf(sb, "\t<port>");
3278 ctl_id_sbuf(port->port_devid, sb);
3279 sbuf_printf(sb, "</port>\n");
3282 if (port->port_info != NULL) {
3283 retval = port->port_info(port->onoff_arg, sb);
3287 STAILQ_FOREACH(opt, &port->options, links) {
3288 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3289 opt->name, opt->value, opt->name);
3294 if (port->lun_map != NULL) {
3295 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n");
3296 for (j = 0; j < port->lun_map_size; j++) {
3297 plun = ctl_lun_map_from_port(port, j);
3298 if (plun == UINT32_MAX)
3301 "\t<lun id=\"%u\">%u</lun>\n",
3306 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3307 if (port->wwpn_iid[j].in_use == 0 ||
3308 (port->wwpn_iid[j].wwpn == 0 &&
3309 port->wwpn_iid[j].name == NULL))
3312 if (port->wwpn_iid[j].name != NULL)
3313 retval = sbuf_printf(sb,
3314 "\t<initiator id=\"%u\">%s</initiator>\n",
3315 j, port->wwpn_iid[j].name);
3317 retval = sbuf_printf(sb,
3318 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3319 j, port->wwpn_iid[j].wwpn);
3326 retval = sbuf_printf(sb, "</targ_port>\n");
3330 mtx_unlock(&softc->ctl_lock);
3333 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3336 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3337 snprintf(list->error_str, sizeof(list->error_str),
3338 "Out of space, %d bytes is too small",
3345 retval = copyout(sbuf_data(sb), list->lun_xml,
3348 list->fill_len = sbuf_len(sb) + 1;
3349 list->status = CTL_LUN_LIST_OK;
3354 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr;
3355 struct ctl_port *port;
3357 mtx_lock(&softc->ctl_lock);
3358 if (lm->port < softc->port_min ||
3359 lm->port >= softc->port_max ||
3360 (port = softc->ctl_ports[lm->port]) == NULL) {
3361 mtx_unlock(&softc->ctl_lock);
3364 if (port->status & CTL_PORT_STATUS_ONLINE) {
3365 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3366 if (ctl_lun_map_to_port(port, lun->lun) ==
3369 mtx_lock(&lun->lun_lock);
3370 ctl_est_ua_port(lun, lm->port, -1,
3372 mtx_unlock(&lun->lun_lock);
3375 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps
3376 if (lm->plun != UINT32_MAX) {
3377 if (lm->lun == UINT32_MAX)
3378 retval = ctl_lun_map_unset(port, lm->plun);
3379 else if (lm->lun < CTL_MAX_LUNS &&
3380 softc->ctl_luns[lm->lun] != NULL)
3381 retval = ctl_lun_map_set(port, lm->plun, lm->lun);
3385 if (lm->lun == UINT32_MAX)
3386 retval = ctl_lun_map_deinit(port);
3388 retval = ctl_lun_map_init(port);
3390 if (port->status & CTL_PORT_STATUS_ONLINE)
3391 ctl_isc_announce_port(port);
3395 /* XXX KDM should we fix this? */
3397 struct ctl_backend_driver *backend;
3404 * We encode the backend type as the ioctl type for backend
3405 * ioctls. So parse it out here, and then search for a
3406 * backend of this type.
3408 type = _IOC_TYPE(cmd);
3410 STAILQ_FOREACH(backend, &softc->be_list, links) {
3411 if (backend->type == type) {
3417 printf("ctl: unknown ioctl command %#lx or backend "
3422 retval = backend->ioctl(dev, cmd, addr, flag, td);
3432 ctl_get_initindex(struct ctl_nexus *nexus)
3434 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3438 ctl_lun_map_init(struct ctl_port *port)
3440 struct ctl_softc *softc = port->ctl_softc;
3441 struct ctl_lun *lun;
3442 int size = ctl_lun_map_size;
3445 if (port->lun_map == NULL || port->lun_map_size < size) {
3446 port->lun_map_size = 0;
3447 free(port->lun_map, M_CTL);
3448 port->lun_map = malloc(size * sizeof(uint32_t),
3451 if (port->lun_map == NULL)
3453 for (i = 0; i < size; i++)
3454 port->lun_map[i] = UINT32_MAX;
3455 port->lun_map_size = size;
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 port->lun_map_size = 0;
3475 free(port->lun_map, M_CTL);
3476 port->lun_map = NULL;
3477 if (port->status & CTL_PORT_STATUS_ONLINE) {
3478 if (port->lun_enable != NULL) {
3479 STAILQ_FOREACH(lun, &softc->lun_list, links)
3480 port->lun_enable(port->targ_lun_arg, lun->lun);
3482 ctl_isc_announce_port(port);
3488 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun)
3493 if (port->lun_map == NULL) {
3494 status = ctl_lun_map_init(port);
3498 if (plun >= port->lun_map_size)
3500 old = port->lun_map[plun];
3501 port->lun_map[plun] = glun;
3502 if ((port->status & CTL_PORT_STATUS_ONLINE) && old == UINT32_MAX) {
3503 if (port->lun_enable != NULL)
3504 port->lun_enable(port->targ_lun_arg, plun);
3505 ctl_isc_announce_port(port);
3511 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun)
3515 if (port->lun_map == NULL || plun >= port->lun_map_size)
3517 old = port->lun_map[plun];
3518 port->lun_map[plun] = UINT32_MAX;
3519 if ((port->status & CTL_PORT_STATUS_ONLINE) && old != UINT32_MAX) {
3520 if (port->lun_disable != NULL)
3521 port->lun_disable(port->targ_lun_arg, plun);
3522 ctl_isc_announce_port(port);
3528 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
3532 return (UINT32_MAX);
3533 if (port->lun_map == NULL)
3535 if (lun_id > port->lun_map_size)
3536 return (UINT32_MAX);
3537 return (port->lun_map[lun_id]);
3541 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
3546 return (UINT32_MAX);
3547 if (port->lun_map == NULL)
3549 for (i = 0; i < port->lun_map_size; i++) {
3550 if (port->lun_map[i] == lun_id)
3553 return (UINT32_MAX);
3557 ctl_decode_lun(uint64_t encoded)
3560 uint32_t result = 0xffffffff;
3562 be64enc(lun, encoded);
3563 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) {
3564 case RPL_LUNDATA_ATYP_PERIPH:
3565 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 &&
3566 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0)
3569 case RPL_LUNDATA_ATYP_FLAT:
3570 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 &&
3571 lun[6] == 0 && lun[7] == 0)
3572 result = ((lun[0] & 0x3f) << 8) + lun[1];
3574 case RPL_LUNDATA_ATYP_EXTLUN:
3575 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) {
3577 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) {
3582 result = (lun[1] << 16) + (lun[2] << 8) +
3586 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0)
3587 result = (lun[2] << 24) +
3588 (lun[3] << 16) + (lun[4] << 8) +
3593 case RPL_LUNDATA_EXT_EAM_NOT_SPEC:
3594 result = 0xffffffff;
3603 ctl_encode_lun(uint32_t decoded)
3605 uint64_t l = decoded;
3608 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48));
3610 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48));
3612 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) |
3614 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16));
3618 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last)
3622 for (i = first; i < last; i++) {
3623 if ((mask[i / 32] & (1 << (i % 32))) == 0)
3630 ctl_set_mask(uint32_t *mask, uint32_t bit)
3632 uint32_t chunk, piece;
3635 piece = bit % (sizeof(uint32_t) * 8);
3637 if ((mask[chunk] & (1 << piece)) != 0)
3640 mask[chunk] |= (1 << piece);
3646 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3648 uint32_t chunk, piece;
3651 piece = bit % (sizeof(uint32_t) * 8);
3653 if ((mask[chunk] & (1 << piece)) == 0)
3656 mask[chunk] &= ~(1 << piece);
3662 ctl_is_set(uint32_t *mask, uint32_t bit)
3664 uint32_t chunk, piece;
3667 piece = bit % (sizeof(uint32_t) * 8);
3669 if ((mask[chunk] & (1 << piece)) == 0)
3676 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3680 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3683 return (t[residx % CTL_MAX_INIT_PER_PORT]);
3687 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3691 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3694 t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3698 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3703 i = residx/CTL_MAX_INIT_PER_PORT;
3704 if (lun->pr_keys[i] != NULL)
3706 mtx_unlock(&lun->lun_lock);
3707 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3709 mtx_lock(&lun->lun_lock);
3710 if (lun->pr_keys[i] == NULL)
3711 lun->pr_keys[i] = p;
3717 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3721 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3722 KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3723 t[residx % CTL_MAX_INIT_PER_PORT] = key;
3727 * ctl_softc, pool_name, total_ctl_io are passed in.
3728 * npool is passed out.
3731 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3732 uint32_t total_ctl_io, void **npool)
3734 struct ctl_io_pool *pool;
3736 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3741 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3742 pool->ctl_softc = ctl_softc;
3744 pool->zone = uma_zsecond_create(pool->name, NULL,
3745 NULL, NULL, NULL, ctl_softc->io_zone);
3746 /* uma_prealloc(pool->zone, total_ctl_io); */
3748 pool->zone = ctl_softc->io_zone;
3756 ctl_pool_free(struct ctl_io_pool *pool)
3763 uma_zdestroy(pool->zone);
3769 ctl_alloc_io(void *pool_ref)
3771 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3774 io = uma_zalloc(pool->zone, M_WAITOK);
3776 io->io_hdr.pool = pool_ref;
3777 CTL_SOFTC(io) = pool->ctl_softc;
3783 ctl_alloc_io_nowait(void *pool_ref)
3785 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3788 io = uma_zalloc(pool->zone, M_NOWAIT);
3790 io->io_hdr.pool = pool_ref;
3791 CTL_SOFTC(io) = pool->ctl_softc;
3797 ctl_free_io(union ctl_io *io)
3799 struct ctl_io_pool *pool;
3804 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3805 uma_zfree(pool->zone, io);
3809 ctl_zero_io(union ctl_io *io)
3811 struct ctl_io_pool *pool;
3817 * May need to preserve linked list pointers at some point too.
3819 pool = io->io_hdr.pool;
3820 memset(io, 0, sizeof(*io));
3821 io->io_hdr.pool = pool;
3822 CTL_SOFTC(io) = pool->ctl_softc;
3826 ctl_expand_number(const char *buf, uint64_t *num)
3832 number = strtoq(buf, &endptr, 0);
3834 switch (tolower((unsigned char)*endptr)) {
3854 case '\0': /* No unit. */
3858 /* Unrecognized unit. */
3862 if ((number << shift) >> shift != number) {
3866 *num = number << shift;
3872 * This routine could be used in the future to load default and/or saved
3873 * mode page parameters for a particuar lun.
3876 ctl_init_page_index(struct ctl_lun *lun)
3879 struct ctl_page_index *page_index;
3883 memcpy(&lun->mode_pages.index, page_index_template,
3884 sizeof(page_index_template));
3886 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3888 page_index = &lun->mode_pages.index[i];
3889 if (lun->be_lun->lun_type == T_DIRECT &&
3890 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
3892 if (lun->be_lun->lun_type == T_PROCESSOR &&
3893 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
3895 if (lun->be_lun->lun_type == T_CDROM &&
3896 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
3899 page_code = page_index->page_code & SMPH_PC_MASK;
3900 switch (page_code) {
3901 case SMS_RW_ERROR_RECOVERY_PAGE: {
3902 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
3903 ("subpage %#x for page %#x is incorrect!",
3904 page_index->subpage, page_code));
3905 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
3906 &rw_er_page_default,
3907 sizeof(rw_er_page_default));
3908 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
3909 &rw_er_page_changeable,
3910 sizeof(rw_er_page_changeable));
3911 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
3912 &rw_er_page_default,
3913 sizeof(rw_er_page_default));
3914 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
3915 &rw_er_page_default,
3916 sizeof(rw_er_page_default));
3917 page_index->page_data =
3918 (uint8_t *)lun->mode_pages.rw_er_page;
3921 case SMS_FORMAT_DEVICE_PAGE: {
3922 struct scsi_format_page *format_page;
3924 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
3925 ("subpage %#x for page %#x is incorrect!",
3926 page_index->subpage, page_code));
3929 * Sectors per track are set above. Bytes per
3930 * sector need to be set here on a per-LUN basis.
3932 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
3933 &format_page_default,
3934 sizeof(format_page_default));
3935 memcpy(&lun->mode_pages.format_page[
3936 CTL_PAGE_CHANGEABLE], &format_page_changeable,
3937 sizeof(format_page_changeable));
3938 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
3939 &format_page_default,
3940 sizeof(format_page_default));
3941 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
3942 &format_page_default,
3943 sizeof(format_page_default));
3945 format_page = &lun->mode_pages.format_page[
3947 scsi_ulto2b(lun->be_lun->blocksize,
3948 format_page->bytes_per_sector);
3950 format_page = &lun->mode_pages.format_page[
3952 scsi_ulto2b(lun->be_lun->blocksize,
3953 format_page->bytes_per_sector);
3955 format_page = &lun->mode_pages.format_page[
3957 scsi_ulto2b(lun->be_lun->blocksize,
3958 format_page->bytes_per_sector);
3960 page_index->page_data =
3961 (uint8_t *)lun->mode_pages.format_page;
3964 case SMS_RIGID_DISK_PAGE: {
3965 struct scsi_rigid_disk_page *rigid_disk_page;
3966 uint32_t sectors_per_cylinder;
3970 #endif /* !__XSCALE__ */
3972 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
3973 ("subpage %#x for page %#x is incorrect!",
3974 page_index->subpage, page_code));
3977 * Rotation rate and sectors per track are set
3978 * above. We calculate the cylinders here based on
3979 * capacity. Due to the number of heads and
3980 * sectors per track we're using, smaller arrays
3981 * may turn out to have 0 cylinders. Linux and
3982 * FreeBSD don't pay attention to these mode pages
3983 * to figure out capacity, but Solaris does. It
3984 * seems to deal with 0 cylinders just fine, and
3985 * works out a fake geometry based on the capacity.
3987 memcpy(&lun->mode_pages.rigid_disk_page[
3988 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
3989 sizeof(rigid_disk_page_default));
3990 memcpy(&lun->mode_pages.rigid_disk_page[
3991 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
3992 sizeof(rigid_disk_page_changeable));
3994 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
3998 * The divide method here will be more accurate,
3999 * probably, but results in floating point being
4000 * used in the kernel on i386 (__udivdi3()). On the
4001 * XScale, though, __udivdi3() is implemented in
4004 * The shift method for cylinder calculation is
4005 * accurate if sectors_per_cylinder is a power of
4006 * 2. Otherwise it might be slightly off -- you
4007 * might have a bit of a truncation problem.
4010 cylinders = (lun->be_lun->maxlba + 1) /
4011 sectors_per_cylinder;
4013 for (shift = 31; shift > 0; shift--) {
4014 if (sectors_per_cylinder & (1 << shift))
4017 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4021 * We've basically got 3 bytes, or 24 bits for the
4022 * cylinder size in the mode page. If we're over,
4023 * just round down to 2^24.
4025 if (cylinders > 0xffffff)
4026 cylinders = 0xffffff;
4028 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4030 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4032 if ((value = ctl_get_opt(&lun->be_lun->options,
4034 scsi_ulto2b(strtol(value, NULL, 0),
4035 rigid_disk_page->rotation_rate);
4038 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
4039 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4040 sizeof(rigid_disk_page_default));
4041 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
4042 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4043 sizeof(rigid_disk_page_default));
4045 page_index->page_data =
4046 (uint8_t *)lun->mode_pages.rigid_disk_page;
4049 case SMS_VERIFY_ERROR_RECOVERY_PAGE: {
4050 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4051 ("subpage %#x for page %#x is incorrect!",
4052 page_index->subpage, page_code));
4053 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CURRENT],
4054 &verify_er_page_default,
4055 sizeof(verify_er_page_default));
4056 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CHANGEABLE],
4057 &verify_er_page_changeable,
4058 sizeof(verify_er_page_changeable));
4059 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_DEFAULT],
4060 &verify_er_page_default,
4061 sizeof(verify_er_page_default));
4062 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_SAVED],
4063 &verify_er_page_default,
4064 sizeof(verify_er_page_default));
4065 page_index->page_data =
4066 (uint8_t *)lun->mode_pages.verify_er_page;
4069 case SMS_CACHING_PAGE: {
4070 struct scsi_caching_page *caching_page;
4072 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4073 ("subpage %#x for page %#x is incorrect!",
4074 page_index->subpage, page_code));
4075 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4076 &caching_page_default,
4077 sizeof(caching_page_default));
4078 memcpy(&lun->mode_pages.caching_page[
4079 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4080 sizeof(caching_page_changeable));
4081 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4082 &caching_page_default,
4083 sizeof(caching_page_default));
4084 caching_page = &lun->mode_pages.caching_page[
4086 value = ctl_get_opt(&lun->be_lun->options, "writecache");
4087 if (value != NULL && strcmp(value, "off") == 0)
4088 caching_page->flags1 &= ~SCP_WCE;
4089 value = ctl_get_opt(&lun->be_lun->options, "readcache");
4090 if (value != NULL && strcmp(value, "off") == 0)
4091 caching_page->flags1 |= SCP_RCD;
4092 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4093 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4094 sizeof(caching_page_default));
4095 page_index->page_data =
4096 (uint8_t *)lun->mode_pages.caching_page;
4099 case SMS_CONTROL_MODE_PAGE: {
4100 switch (page_index->subpage) {
4101 case SMS_SUBPAGE_PAGE_0: {
4102 struct scsi_control_page *control_page;
4104 memcpy(&lun->mode_pages.control_page[
4106 &control_page_default,
4107 sizeof(control_page_default));
4108 memcpy(&lun->mode_pages.control_page[
4109 CTL_PAGE_CHANGEABLE],
4110 &control_page_changeable,
4111 sizeof(control_page_changeable));
4112 memcpy(&lun->mode_pages.control_page[
4114 &control_page_default,
4115 sizeof(control_page_default));
4116 control_page = &lun->mode_pages.control_page[
4118 value = ctl_get_opt(&lun->be_lun->options,
4120 if (value != NULL &&
4121 strcmp(value, "unrestricted") == 0) {
4122 control_page->queue_flags &=
4123 ~SCP_QUEUE_ALG_MASK;
4124 control_page->queue_flags |=
4125 SCP_QUEUE_ALG_UNRESTRICTED;
4127 memcpy(&lun->mode_pages.control_page[
4129 &lun->mode_pages.control_page[
4131 sizeof(control_page_default));
4132 page_index->page_data =
4133 (uint8_t *)lun->mode_pages.control_page;
4137 memcpy(&lun->mode_pages.control_ext_page[
4139 &control_ext_page_default,
4140 sizeof(control_ext_page_default));
4141 memcpy(&lun->mode_pages.control_ext_page[
4142 CTL_PAGE_CHANGEABLE],
4143 &control_ext_page_changeable,
4144 sizeof(control_ext_page_changeable));
4145 memcpy(&lun->mode_pages.control_ext_page[
4147 &control_ext_page_default,
4148 sizeof(control_ext_page_default));
4149 memcpy(&lun->mode_pages.control_ext_page[
4151 &lun->mode_pages.control_ext_page[
4153 sizeof(control_ext_page_default));
4154 page_index->page_data =
4155 (uint8_t *)lun->mode_pages.control_ext_page;
4158 panic("subpage %#x for page %#x is incorrect!",
4159 page_index->subpage, page_code);
4163 case SMS_INFO_EXCEPTIONS_PAGE: {
4164 switch (page_index->subpage) {
4165 case SMS_SUBPAGE_PAGE_0:
4166 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4168 sizeof(ie_page_default));
4169 memcpy(&lun->mode_pages.ie_page[
4170 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4171 sizeof(ie_page_changeable));
4172 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4174 sizeof(ie_page_default));
4175 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4177 sizeof(ie_page_default));
4178 page_index->page_data =
4179 (uint8_t *)lun->mode_pages.ie_page;
4182 struct ctl_logical_block_provisioning_page *page;
4184 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4186 sizeof(lbp_page_default));
4187 memcpy(&lun->mode_pages.lbp_page[
4188 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4189 sizeof(lbp_page_changeable));
4190 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4192 sizeof(lbp_page_default));
4193 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4194 value = ctl_get_opt(&lun->be_lun->options,
4196 if (value != NULL &&
4197 ctl_expand_number(value, &ival) == 0) {
4198 page->descr[0].flags |= SLBPPD_ENABLED |
4200 if (lun->be_lun->blocksize)
4201 ival /= lun->be_lun->blocksize;
4204 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4205 page->descr[0].count);
4207 value = ctl_get_opt(&lun->be_lun->options,
4209 if (value != NULL &&
4210 ctl_expand_number(value, &ival) == 0) {
4211 page->descr[1].flags |= SLBPPD_ENABLED |
4213 if (lun->be_lun->blocksize)
4214 ival /= lun->be_lun->blocksize;
4217 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4218 page->descr[1].count);
4220 value = ctl_get_opt(&lun->be_lun->options,
4221 "pool-avail-threshold");
4222 if (value != NULL &&
4223 ctl_expand_number(value, &ival) == 0) {
4224 page->descr[2].flags |= SLBPPD_ENABLED |
4226 if (lun->be_lun->blocksize)
4227 ival /= lun->be_lun->blocksize;
4230 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4231 page->descr[2].count);
4233 value = ctl_get_opt(&lun->be_lun->options,
4234 "pool-used-threshold");
4235 if (value != NULL &&
4236 ctl_expand_number(value, &ival) == 0) {
4237 page->descr[3].flags |= SLBPPD_ENABLED |
4239 if (lun->be_lun->blocksize)
4240 ival /= lun->be_lun->blocksize;
4243 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4244 page->descr[3].count);
4246 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4247 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4248 sizeof(lbp_page_default));
4249 page_index->page_data =
4250 (uint8_t *)lun->mode_pages.lbp_page;
4254 panic("subpage %#x for page %#x is incorrect!",
4255 page_index->subpage, page_code);
4259 case SMS_CDDVD_CAPS_PAGE:{
4260 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4261 ("subpage %#x for page %#x is incorrect!",
4262 page_index->subpage, page_code));
4263 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT],
4264 &cddvd_page_default,
4265 sizeof(cddvd_page_default));
4266 memcpy(&lun->mode_pages.cddvd_page[
4267 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable,
4268 sizeof(cddvd_page_changeable));
4269 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4270 &cddvd_page_default,
4271 sizeof(cddvd_page_default));
4272 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT],
4273 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4274 sizeof(cddvd_page_default));
4275 page_index->page_data =
4276 (uint8_t *)lun->mode_pages.cddvd_page;
4280 panic("invalid page code value %#x", page_code);
4284 return (CTL_RETVAL_COMPLETE);
4288 ctl_init_log_page_index(struct ctl_lun *lun)
4290 struct ctl_page_index *page_index;
4293 memcpy(&lun->log_pages.index, log_page_index_template,
4294 sizeof(log_page_index_template));
4297 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4299 page_index = &lun->log_pages.index[i];
4300 if (lun->be_lun->lun_type == T_DIRECT &&
4301 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4303 if (lun->be_lun->lun_type == T_PROCESSOR &&
4304 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4306 if (lun->be_lun->lun_type == T_CDROM &&
4307 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4310 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4311 lun->backend->lun_attr == NULL)
4314 if (page_index->page_code != prev) {
4315 lun->log_pages.pages_page[j] = page_index->page_code;
4316 prev = page_index->page_code;
4319 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4320 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4323 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4324 lun->log_pages.index[0].page_len = j;
4325 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4326 lun->log_pages.index[1].page_len = k * 2;
4327 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4328 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4329 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page;
4330 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page);
4331 lun->log_pages.index[4].page_data = (uint8_t *)&lun->log_pages.ie_page;
4332 lun->log_pages.index[4].page_len = sizeof(lun->log_pages.ie_page);
4334 return (CTL_RETVAL_COMPLETE);
4338 hex2bin(const char *str, uint8_t *buf, int buf_size)
4343 memset(buf, 0, buf_size);
4344 while (isspace(str[0]))
4346 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4349 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4350 while (str[i] == '-') /* Skip dashes in UUIDs. */
4355 else if (isalpha(c))
4356 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4362 buf[i / 2] |= (c << 4);
4366 return ((i + 1) / 2);
4373 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4374 * wants us to allocate the LUN and he can block.
4375 * - ctl_softc is always set
4376 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4378 * Returns 0 for success, non-zero (errno) for failure.
4381 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4382 struct ctl_be_lun *const be_lun)
4384 struct ctl_lun *nlun, *lun;
4385 struct scsi_vpd_id_descriptor *desc;
4386 struct scsi_vpd_id_t10 *t10id;
4387 const char *eui, *naa, *scsiname, *uuid, *vendor, *value;
4388 int lun_number, i, lun_malloced;
4389 int devidlen, idlen1, idlen2 = 0, len;
4395 * We currently only support Direct Access or Processor LUN types.
4397 switch (be_lun->lun_type) {
4405 be_lun->lun_config_status(be_lun->be_lun,
4406 CTL_LUN_CONFIG_FAILURE);
4409 if (ctl_lun == NULL) {
4410 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4417 memset(lun, 0, sizeof(*lun));
4419 lun->flags = CTL_LUN_MALLOCED;
4421 /* Generate LUN ID. */
4422 devidlen = max(CTL_DEVID_MIN_LEN,
4423 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4424 idlen1 = sizeof(*t10id) + devidlen;
4425 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4426 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4427 if (scsiname != NULL) {
4428 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4429 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4431 eui = ctl_get_opt(&be_lun->options, "eui");
4433 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4435 naa = ctl_get_opt(&be_lun->options, "naa");
4437 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4439 uuid = ctl_get_opt(&be_lun->options, "uuid");
4441 len += sizeof(struct scsi_vpd_id_descriptor) + 18;
4443 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4444 M_CTL, M_WAITOK | M_ZERO);
4445 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4446 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4447 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4448 desc->length = idlen1;
4449 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4450 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4451 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4452 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4454 strncpy(t10id->vendor, vendor,
4455 min(sizeof(t10id->vendor), strlen(vendor)));
4457 strncpy((char *)t10id->vendor_spec_id,
4458 (char *)be_lun->device_id, devidlen);
4459 if (scsiname != NULL) {
4460 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4462 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4463 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4464 SVPD_ID_TYPE_SCSI_NAME;
4465 desc->length = idlen2;
4466 strlcpy(desc->identifier, scsiname, idlen2);
4469 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4471 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4472 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4474 desc->length = hex2bin(eui, desc->identifier, 16);
4475 desc->length = desc->length > 12 ? 16 :
4476 (desc->length > 8 ? 12 : 8);
4477 len -= 16 - desc->length;
4480 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4482 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4483 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4485 desc->length = hex2bin(naa, desc->identifier, 16);
4486 desc->length = desc->length > 8 ? 16 : 8;
4487 len -= 16 - desc->length;
4490 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4492 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4493 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4495 desc->identifier[0] = 0x10;
4496 hex2bin(uuid, &desc->identifier[2], 16);
4499 lun->lun_devid->len = len;
4501 mtx_lock(&ctl_softc->ctl_lock);
4503 * See if the caller requested a particular LUN number. If so, see
4504 * if it is available. Otherwise, allocate the first available LUN.
4506 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4507 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4508 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4509 mtx_unlock(&ctl_softc->ctl_lock);
4510 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4511 printf("ctl: requested LUN ID %d is higher "
4512 "than CTL_MAX_LUNS - 1 (%d)\n",
4513 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4516 * XXX KDM return an error, or just assign
4517 * another LUN ID in this case??
4519 printf("ctl: requested LUN ID %d is already "
4520 "in use\n", be_lun->req_lun_id);
4522 if (lun->flags & CTL_LUN_MALLOCED)
4524 be_lun->lun_config_status(be_lun->be_lun,
4525 CTL_LUN_CONFIG_FAILURE);
4528 lun_number = be_lun->req_lun_id;
4530 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, CTL_MAX_LUNS);
4531 if (lun_number == -1) {
4532 mtx_unlock(&ctl_softc->ctl_lock);
4533 printf("ctl: can't allocate LUN, out of LUNs\n");
4534 if (lun->flags & CTL_LUN_MALLOCED)
4536 be_lun->lun_config_status(be_lun->be_lun,
4537 CTL_LUN_CONFIG_FAILURE);
4541 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4543 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4544 lun->lun = lun_number;
4545 lun->be_lun = be_lun;
4547 * The processor LUN is always enabled. Disk LUNs come on line
4548 * disabled, and must be enabled by the backend.
4550 lun->flags |= CTL_LUN_DISABLED;
4551 lun->backend = be_lun->be;
4552 be_lun->ctl_lun = lun;
4553 be_lun->lun_id = lun_number;
4554 atomic_add_int(&be_lun->be->num_luns, 1);
4555 if (be_lun->flags & CTL_LUN_FLAG_EJECTED)
4556 lun->flags |= CTL_LUN_EJECTED;
4557 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA)
4558 lun->flags |= CTL_LUN_NO_MEDIA;
4559 if (be_lun->flags & CTL_LUN_FLAG_STOPPED)
4560 lun->flags |= CTL_LUN_STOPPED;
4562 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4563 lun->flags |= CTL_LUN_PRIMARY_SC;
4565 value = ctl_get_opt(&be_lun->options, "removable");
4566 if (value != NULL) {
4567 if (strcmp(value, "on") == 0)
4568 lun->flags |= CTL_LUN_REMOVABLE;
4569 } else if (be_lun->lun_type == T_CDROM)
4570 lun->flags |= CTL_LUN_REMOVABLE;
4572 lun->ctl_softc = ctl_softc;
4574 lun->last_busy = getsbinuptime();
4576 TAILQ_INIT(&lun->ooa_queue);
4577 TAILQ_INIT(&lun->blocked_queue);
4578 STAILQ_INIT(&lun->error_list);
4579 lun->ie_reported = 1;
4580 callout_init_mtx(&lun->ie_callout, &lun->lun_lock, 0);
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 = lun->ctl_softc;
4630 struct ctl_lun *nlun;
4633 mtx_assert(&softc->ctl_lock, MA_OWNED);
4635 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4637 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4639 softc->ctl_luns[lun->lun] = NULL;
4641 if (!TAILQ_EMPTY(&lun->ooa_queue))
4642 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4647 * Tell the backend to free resources, if this LUN has a backend.
4649 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4650 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4652 lun->ie_reportcnt = UINT32_MAX;
4653 callout_drain(&lun->ie_callout);
4655 ctl_tpc_lun_shutdown(lun);
4656 mtx_destroy(&lun->lun_lock);
4657 free(lun->lun_devid, M_CTL);
4658 for (i = 0; i < CTL_MAX_PORTS; i++)
4659 free(lun->pending_ua[i], M_CTL);
4660 for (i = 0; i < CTL_MAX_PORTS; i++)
4661 free(lun->pr_keys[i], M_CTL);
4662 free(lun->write_buffer, M_CTL);
4663 if (lun->flags & CTL_LUN_MALLOCED)
4666 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4667 mtx_lock(&nlun->lun_lock);
4668 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4669 mtx_unlock(&nlun->lun_lock);
4676 ctl_create_lun(struct ctl_be_lun *be_lun)
4680 * ctl_alloc_lun() should handle all potential failure cases.
4682 ctl_alloc_lun(control_softc, NULL, be_lun);
4686 ctl_add_lun(struct ctl_be_lun *be_lun)
4688 struct ctl_softc *softc = control_softc;
4690 mtx_lock(&softc->ctl_lock);
4691 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links);
4692 mtx_unlock(&softc->ctl_lock);
4693 wakeup(&softc->pending_lun_queue);
4699 ctl_enable_lun(struct ctl_be_lun *be_lun)
4701 struct ctl_softc *softc;
4702 struct ctl_port *port, *nport;
4703 struct ctl_lun *lun;
4706 lun = (struct ctl_lun *)be_lun->ctl_lun;
4707 softc = lun->ctl_softc;
4709 mtx_lock(&softc->ctl_lock);
4710 mtx_lock(&lun->lun_lock);
4711 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4713 * eh? Why did we get called if the LUN is already
4716 mtx_unlock(&lun->lun_lock);
4717 mtx_unlock(&softc->ctl_lock);
4720 lun->flags &= ~CTL_LUN_DISABLED;
4721 mtx_unlock(&lun->lun_lock);
4723 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) {
4724 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4725 port->lun_map != NULL || port->lun_enable == NULL)
4729 * Drop the lock while we call the FETD's enable routine.
4730 * This can lead to a callback into CTL (at least in the
4731 * case of the internal initiator frontend.
4733 mtx_unlock(&softc->ctl_lock);
4734 retval = port->lun_enable(port->targ_lun_arg, lun->lun);
4735 mtx_lock(&softc->ctl_lock);
4737 printf("%s: FETD %s port %d returned error "
4738 "%d for lun_enable on lun %jd\n",
4739 __func__, port->port_name, port->targ_port,
4740 retval, (intmax_t)lun->lun);
4744 mtx_unlock(&softc->ctl_lock);
4745 ctl_isc_announce_lun(lun);
4751 ctl_disable_lun(struct ctl_be_lun *be_lun)
4753 struct ctl_softc *softc;
4754 struct ctl_port *port;
4755 struct ctl_lun *lun;
4758 lun = (struct ctl_lun *)be_lun->ctl_lun;
4759 softc = lun->ctl_softc;
4761 mtx_lock(&softc->ctl_lock);
4762 mtx_lock(&lun->lun_lock);
4763 if (lun->flags & CTL_LUN_DISABLED) {
4764 mtx_unlock(&lun->lun_lock);
4765 mtx_unlock(&softc->ctl_lock);
4768 lun->flags |= CTL_LUN_DISABLED;
4769 mtx_unlock(&lun->lun_lock);
4771 STAILQ_FOREACH(port, &softc->port_list, links) {
4772 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4773 port->lun_map != NULL || port->lun_disable == NULL)
4777 * Drop the lock before we call the frontend's disable
4778 * routine, to avoid lock order reversals.
4780 * XXX KDM what happens if the frontend list changes while
4781 * we're traversing it? It's unlikely, but should be handled.
4783 mtx_unlock(&softc->ctl_lock);
4784 retval = port->lun_disable(port->targ_lun_arg, lun->lun);
4785 mtx_lock(&softc->ctl_lock);
4787 printf("%s: FETD %s port %d returned error "
4788 "%d for lun_disable on lun %jd\n",
4789 __func__, port->port_name, port->targ_port,
4790 retval, (intmax_t)lun->lun);
4794 mtx_unlock(&softc->ctl_lock);
4795 ctl_isc_announce_lun(lun);
4801 ctl_start_lun(struct ctl_be_lun *be_lun)
4803 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4805 mtx_lock(&lun->lun_lock);
4806 lun->flags &= ~CTL_LUN_STOPPED;
4807 mtx_unlock(&lun->lun_lock);
4812 ctl_stop_lun(struct ctl_be_lun *be_lun)
4814 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4816 mtx_lock(&lun->lun_lock);
4817 lun->flags |= CTL_LUN_STOPPED;
4818 mtx_unlock(&lun->lun_lock);
4823 ctl_lun_no_media(struct ctl_be_lun *be_lun)
4825 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4827 mtx_lock(&lun->lun_lock);
4828 lun->flags |= CTL_LUN_NO_MEDIA;
4829 mtx_unlock(&lun->lun_lock);
4834 ctl_lun_has_media(struct ctl_be_lun *be_lun)
4836 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4837 union ctl_ha_msg msg;
4839 mtx_lock(&lun->lun_lock);
4840 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED);
4841 if (lun->flags & CTL_LUN_REMOVABLE)
4842 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE);
4843 mtx_unlock(&lun->lun_lock);
4844 if ((lun->flags & CTL_LUN_REMOVABLE) &&
4845 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4846 bzero(&msg.ua, sizeof(msg.ua));
4847 msg.hdr.msg_type = CTL_MSG_UA;
4848 msg.hdr.nexus.initid = -1;
4849 msg.hdr.nexus.targ_port = -1;
4850 msg.hdr.nexus.targ_lun = lun->lun;
4851 msg.hdr.nexus.targ_mapped_lun = lun->lun;
4854 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE;
4855 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4862 ctl_lun_ejected(struct ctl_be_lun *be_lun)
4864 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4866 mtx_lock(&lun->lun_lock);
4867 lun->flags |= CTL_LUN_EJECTED;
4868 mtx_unlock(&lun->lun_lock);
4873 ctl_lun_primary(struct ctl_be_lun *be_lun)
4875 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4877 mtx_lock(&lun->lun_lock);
4878 lun->flags |= CTL_LUN_PRIMARY_SC;
4879 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4880 mtx_unlock(&lun->lun_lock);
4881 ctl_isc_announce_lun(lun);
4886 ctl_lun_secondary(struct ctl_be_lun *be_lun)
4888 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4890 mtx_lock(&lun->lun_lock);
4891 lun->flags &= ~CTL_LUN_PRIMARY_SC;
4892 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4893 mtx_unlock(&lun->lun_lock);
4894 ctl_isc_announce_lun(lun);
4899 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4901 struct ctl_softc *softc;
4902 struct ctl_lun *lun;
4904 lun = (struct ctl_lun *)be_lun->ctl_lun;
4905 softc = lun->ctl_softc;
4907 mtx_lock(&lun->lun_lock);
4910 * The LUN needs to be disabled before it can be marked invalid.
4912 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4913 mtx_unlock(&lun->lun_lock);
4917 * Mark the LUN invalid.
4919 lun->flags |= CTL_LUN_INVALID;
4922 * If there is nothing in the OOA queue, go ahead and free the LUN.
4923 * If we have something in the OOA queue, we'll free it when the
4924 * last I/O completes.
4926 if (TAILQ_EMPTY(&lun->ooa_queue)) {
4927 mtx_unlock(&lun->lun_lock);
4928 mtx_lock(&softc->ctl_lock);
4930 mtx_unlock(&softc->ctl_lock);
4932 mtx_unlock(&lun->lun_lock);
4938 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
4940 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4941 union ctl_ha_msg msg;
4943 mtx_lock(&lun->lun_lock);
4944 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE);
4945 mtx_unlock(&lun->lun_lock);
4946 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4947 /* Send msg to other side. */
4948 bzero(&msg.ua, sizeof(msg.ua));
4949 msg.hdr.msg_type = CTL_MSG_UA;
4950 msg.hdr.nexus.initid = -1;
4951 msg.hdr.nexus.targ_port = -1;
4952 msg.hdr.nexus.targ_lun = lun->lun;
4953 msg.hdr.nexus.targ_mapped_lun = lun->lun;
4956 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE;
4957 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4963 * Backend "memory move is complete" callback for requests that never
4964 * make it down to say RAIDCore's configuration code.
4967 ctl_config_move_done(union ctl_io *io)
4971 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
4972 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
4973 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
4975 if ((io->io_hdr.port_status != 0) &&
4976 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
4977 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
4979 * For hardware error sense keys, the sense key
4980 * specific value is defined to be a retry count,
4981 * but we use it to pass back an internal FETD
4982 * error code. XXX KDM Hopefully the FETD is only
4983 * using 16 bits for an error code, since that's
4984 * all the space we have in the sks field.
4986 ctl_set_internal_failure(&io->scsiio,
4989 io->io_hdr.port_status);
4992 if (ctl_debug & CTL_DEBUG_CDB_DATA)
4994 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
4995 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
4996 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
4997 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
4999 * XXX KDM just assuming a single pointer here, and not a
5000 * S/G list. If we start using S/G lists for config data,
5001 * we'll need to know how to clean them up here as well.
5003 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5004 free(io->scsiio.kern_data_ptr, M_CTL);
5006 retval = CTL_RETVAL_COMPLETE;
5009 * XXX KDM now we need to continue data movement. Some
5011 * - call ctl_scsiio() again? We don't do this for data
5012 * writes, because for those at least we know ahead of
5013 * time where the write will go and how long it is. For
5014 * config writes, though, that information is largely
5015 * contained within the write itself, thus we need to
5016 * parse out the data again.
5018 * - Call some other function once the data is in?
5022 * XXX KDM call ctl_scsiio() again for now, and check flag
5023 * bits to see whether we're allocated or not.
5025 retval = ctl_scsiio(&io->scsiio);
5031 * This gets called by a backend driver when it is done with a
5032 * data_submit method.
5035 ctl_data_submit_done(union ctl_io *io)
5038 * If the IO_CONT flag is set, we need to call the supplied
5039 * function to continue processing the I/O, instead of completing
5042 * If there is an error, though, we don't want to keep processing.
5043 * Instead, just send status back to the initiator.
5045 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5046 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5047 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5048 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5049 io->scsiio.io_cont(io);
5056 * This gets called by a backend driver when it is done with a
5057 * configuration write.
5060 ctl_config_write_done(union ctl_io *io)
5065 * If the IO_CONT flag is set, we need to call the supplied
5066 * function to continue processing the I/O, instead of completing
5069 * If there is an error, though, we don't want to keep processing.
5070 * Instead, just send status back to the initiator.
5072 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5073 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5074 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5075 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5076 io->scsiio.io_cont(io);
5080 * Since a configuration write can be done for commands that actually
5081 * have data allocated, like write buffer, and commands that have
5082 * no data, like start/stop unit, we need to check here.
5084 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5085 buf = io->scsiio.kern_data_ptr;
5094 ctl_config_read_done(union ctl_io *io)
5099 * If there is some error -- we are done, skip data transfer.
5101 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
5102 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5103 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
5104 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5105 buf = io->scsiio.kern_data_ptr;
5115 * If the IO_CONT flag is set, we need to call the supplied
5116 * function to continue processing the I/O, instead of completing
5119 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
5120 io->scsiio.io_cont(io);
5128 * SCSI release command.
5131 ctl_scsi_release(struct ctl_scsiio *ctsio)
5133 struct ctl_lun *lun = CTL_LUN(ctsio);
5136 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5138 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
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 = CTL_LUN(ctsio);
5173 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5175 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5178 * XXX KDM right now, we only support LUN reservation. We don't
5179 * support 3rd party reservations, or extent reservations, which
5180 * might actually need the parameter list. If we've gotten this
5181 * far, we've got a LUN reservation. Anything else got kicked out
5182 * above. So, according to SPC, ignore the length.
5185 mtx_lock(&lun->lun_lock);
5186 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5187 ctl_set_reservation_conflict(ctsio);
5191 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */
5192 if (lun->flags & CTL_LUN_PR_RESERVED) {
5193 ctl_set_success(ctsio);
5197 lun->flags |= CTL_LUN_RESERVED;
5198 lun->res_idx = residx;
5199 ctl_set_success(ctsio);
5202 mtx_unlock(&lun->lun_lock);
5203 ctl_done((union ctl_io *)ctsio);
5204 return (CTL_RETVAL_COMPLETE);
5208 ctl_start_stop(struct ctl_scsiio *ctsio)
5210 struct ctl_lun *lun = CTL_LUN(ctsio);
5211 struct scsi_start_stop_unit *cdb;
5214 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5216 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5218 if ((cdb->how & SSS_PC_MASK) == 0) {
5219 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
5220 (cdb->how & SSS_START) == 0) {
5223 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5224 if (ctl_get_prkey(lun, residx) == 0 ||
5225 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) {
5227 ctl_set_reservation_conflict(ctsio);
5228 ctl_done((union ctl_io *)ctsio);
5229 return (CTL_RETVAL_COMPLETE);
5233 if ((cdb->how & SSS_LOEJ) &&
5234 (lun->flags & CTL_LUN_REMOVABLE) == 0) {
5235 ctl_set_invalid_field(ctsio,
5241 ctl_done((union ctl_io *)ctsio);
5242 return (CTL_RETVAL_COMPLETE);
5245 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) &&
5246 lun->prevent_count > 0) {
5247 /* "Medium removal prevented" */
5248 ctl_set_sense(ctsio, /*current_error*/ 1,
5249 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ?
5250 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST,
5251 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE);
5252 ctl_done((union ctl_io *)ctsio);
5253 return (CTL_RETVAL_COMPLETE);
5257 retval = lun->backend->config_write((union ctl_io *)ctsio);
5262 ctl_prevent_allow(struct ctl_scsiio *ctsio)
5264 struct ctl_lun *lun = CTL_LUN(ctsio);
5265 struct scsi_prevent *cdb;
5269 CTL_DEBUG_PRINT(("ctl_prevent_allow\n"));
5271 cdb = (struct scsi_prevent *)ctsio->cdb;
5273 if ((lun->flags & CTL_LUN_REMOVABLE) == 0) {
5274 ctl_set_invalid_opcode(ctsio);
5275 ctl_done((union ctl_io *)ctsio);
5276 return (CTL_RETVAL_COMPLETE);
5279 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5280 mtx_lock(&lun->lun_lock);
5281 if ((cdb->how & PR_PREVENT) &&
5282 ctl_is_set(lun->prevent, initidx) == 0) {
5283 ctl_set_mask(lun->prevent, initidx);
5284 lun->prevent_count++;
5285 } else if ((cdb->how & PR_PREVENT) == 0 &&
5286 ctl_is_set(lun->prevent, initidx)) {
5287 ctl_clear_mask(lun->prevent, initidx);
5288 lun->prevent_count--;
5290 mtx_unlock(&lun->lun_lock);
5291 retval = lun->backend->config_write((union ctl_io *)ctsio);
5296 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5297 * we don't really do anything with the LBA and length fields if the user
5298 * passes them in. Instead we'll just flush out the cache for the entire
5302 ctl_sync_cache(struct ctl_scsiio *ctsio)
5304 struct ctl_lun *lun = CTL_LUN(ctsio);
5305 struct ctl_lba_len_flags *lbalen;
5306 uint64_t starting_lba;
5307 uint32_t block_count;
5311 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5315 switch (ctsio->cdb[0]) {
5316 case SYNCHRONIZE_CACHE: {
5317 struct scsi_sync_cache *cdb;
5318 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5320 starting_lba = scsi_4btoul(cdb->begin_lba);
5321 block_count = scsi_2btoul(cdb->lb_count);
5325 case SYNCHRONIZE_CACHE_16: {
5326 struct scsi_sync_cache_16 *cdb;
5327 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5329 starting_lba = scsi_8btou64(cdb->begin_lba);
5330 block_count = scsi_4btoul(cdb->lb_count);
5335 ctl_set_invalid_opcode(ctsio);
5336 ctl_done((union ctl_io *)ctsio);
5338 break; /* NOTREACHED */
5342 * We check the LBA and length, but don't do anything with them.
5343 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5344 * get flushed. This check will just help satisfy anyone who wants
5345 * to see an error for an out of range LBA.
5347 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5348 ctl_set_lba_out_of_range(ctsio,
5349 MAX(starting_lba, lun->be_lun->maxlba + 1));
5350 ctl_done((union ctl_io *)ctsio);
5354 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5355 lbalen->lba = starting_lba;
5356 lbalen->len = block_count;
5357 lbalen->flags = byte2;
5358 retval = lun->backend->config_write((union ctl_io *)ctsio);
5365 ctl_format(struct ctl_scsiio *ctsio)
5367 struct scsi_format *cdb;
5368 int length, defect_list_len;
5370 CTL_DEBUG_PRINT(("ctl_format\n"));
5372 cdb = (struct scsi_format *)ctsio->cdb;
5375 if (cdb->byte2 & SF_FMTDATA) {
5376 if (cdb->byte2 & SF_LONGLIST)
5377 length = sizeof(struct scsi_format_header_long);
5379 length = sizeof(struct scsi_format_header_short);
5382 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5384 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5385 ctsio->kern_data_len = length;
5386 ctsio->kern_total_len = length;
5387 ctsio->kern_data_resid = 0;
5388 ctsio->kern_rel_offset = 0;
5389 ctsio->kern_sg_entries = 0;
5390 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5391 ctsio->be_move_done = ctl_config_move_done;
5392 ctl_datamove((union ctl_io *)ctsio);
5394 return (CTL_RETVAL_COMPLETE);
5397 defect_list_len = 0;
5399 if (cdb->byte2 & SF_FMTDATA) {
5400 if (cdb->byte2 & SF_LONGLIST) {
5401 struct scsi_format_header_long *header;
5403 header = (struct scsi_format_header_long *)
5404 ctsio->kern_data_ptr;
5406 defect_list_len = scsi_4btoul(header->defect_list_len);
5407 if (defect_list_len != 0) {
5408 ctl_set_invalid_field(ctsio,
5417 struct scsi_format_header_short *header;
5419 header = (struct scsi_format_header_short *)
5420 ctsio->kern_data_ptr;
5422 defect_list_len = scsi_2btoul(header->defect_list_len);
5423 if (defect_list_len != 0) {
5424 ctl_set_invalid_field(ctsio,
5435 ctl_set_success(ctsio);
5438 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5439 free(ctsio->kern_data_ptr, M_CTL);
5440 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5443 ctl_done((union ctl_io *)ctsio);
5444 return (CTL_RETVAL_COMPLETE);
5448 ctl_read_buffer(struct ctl_scsiio *ctsio)
5450 struct ctl_lun *lun = CTL_LUN(ctsio);
5451 uint64_t buffer_offset;
5454 static uint8_t descr[4];
5455 static uint8_t echo_descr[4] = { 0 };
5457 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5459 switch (ctsio->cdb[0]) {
5461 struct scsi_read_buffer *cdb;
5463 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5464 buffer_offset = scsi_3btoul(cdb->offset);
5465 len = scsi_3btoul(cdb->length);
5469 case READ_BUFFER_16: {
5470 struct scsi_read_buffer_16 *cdb;
5472 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb;
5473 buffer_offset = scsi_8btou64(cdb->offset);
5474 len = scsi_4btoul(cdb->length);
5478 default: /* This shouldn't happen. */
5479 ctl_set_invalid_opcode(ctsio);
5480 ctl_done((union ctl_io *)ctsio);
5481 return (CTL_RETVAL_COMPLETE);
5484 if (buffer_offset > CTL_WRITE_BUFFER_SIZE ||
5485 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5486 ctl_set_invalid_field(ctsio,
5492 ctl_done((union ctl_io *)ctsio);
5493 return (CTL_RETVAL_COMPLETE);
5496 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5498 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5499 ctsio->kern_data_ptr = descr;
5500 len = min(len, sizeof(descr));
5501 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5502 ctsio->kern_data_ptr = echo_descr;
5503 len = min(len, sizeof(echo_descr));
5505 if (lun->write_buffer == NULL) {
5506 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5509 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5511 ctsio->kern_data_len = len;
5512 ctsio->kern_total_len = len;
5513 ctsio->kern_data_resid = 0;
5514 ctsio->kern_rel_offset = 0;
5515 ctsio->kern_sg_entries = 0;
5516 ctl_set_success(ctsio);
5517 ctsio->be_move_done = ctl_config_move_done;
5518 ctl_datamove((union ctl_io *)ctsio);
5519 return (CTL_RETVAL_COMPLETE);
5523 ctl_write_buffer(struct ctl_scsiio *ctsio)
5525 struct ctl_lun *lun = CTL_LUN(ctsio);
5526 struct scsi_write_buffer *cdb;
5527 int buffer_offset, len;
5529 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5531 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5533 len = scsi_3btoul(cdb->length);
5534 buffer_offset = scsi_3btoul(cdb->offset);
5536 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5537 ctl_set_invalid_field(ctsio,
5543 ctl_done((union ctl_io *)ctsio);
5544 return (CTL_RETVAL_COMPLETE);
5548 * If we've got a kernel request that hasn't been malloced yet,
5549 * malloc it and tell the caller the data buffer is here.
5551 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5552 if (lun->write_buffer == NULL) {
5553 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5556 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5557 ctsio->kern_data_len = len;
5558 ctsio->kern_total_len = len;
5559 ctsio->kern_data_resid = 0;
5560 ctsio->kern_rel_offset = 0;
5561 ctsio->kern_sg_entries = 0;
5562 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5563 ctsio->be_move_done = ctl_config_move_done;
5564 ctl_datamove((union ctl_io *)ctsio);
5566 return (CTL_RETVAL_COMPLETE);
5569 ctl_set_success(ctsio);
5570 ctl_done((union ctl_io *)ctsio);
5571 return (CTL_RETVAL_COMPLETE);
5575 ctl_write_same(struct ctl_scsiio *ctsio)
5577 struct ctl_lun *lun = CTL_LUN(ctsio);
5578 struct ctl_lba_len_flags *lbalen;
5580 uint32_t num_blocks;
5584 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5586 switch (ctsio->cdb[0]) {
5587 case WRITE_SAME_10: {
5588 struct scsi_write_same_10 *cdb;
5590 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5592 lba = scsi_4btoul(cdb->addr);
5593 num_blocks = scsi_2btoul(cdb->length);
5597 case WRITE_SAME_16: {
5598 struct scsi_write_same_16 *cdb;
5600 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5602 lba = scsi_8btou64(cdb->addr);
5603 num_blocks = scsi_4btoul(cdb->length);
5609 * We got a command we don't support. This shouldn't
5610 * happen, commands should be filtered out above us.
5612 ctl_set_invalid_opcode(ctsio);
5613 ctl_done((union ctl_io *)ctsio);
5615 return (CTL_RETVAL_COMPLETE);
5616 break; /* NOTREACHED */
5619 /* ANCHOR flag can be used only together with UNMAP */
5620 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) {
5621 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5622 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5623 ctl_done((union ctl_io *)ctsio);
5624 return (CTL_RETVAL_COMPLETE);
5628 * The first check is to make sure we're in bounds, the second
5629 * check is to catch wrap-around problems. If the lba + num blocks
5630 * is less than the lba, then we've wrapped around and the block
5631 * range is invalid anyway.
5633 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5634 || ((lba + num_blocks) < lba)) {
5635 ctl_set_lba_out_of_range(ctsio,
5636 MAX(lba, lun->be_lun->maxlba + 1));
5637 ctl_done((union ctl_io *)ctsio);
5638 return (CTL_RETVAL_COMPLETE);
5641 /* Zero number of blocks means "to the last logical block" */
5642 if (num_blocks == 0) {
5643 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5644 ctl_set_invalid_field(ctsio,
5650 ctl_done((union ctl_io *)ctsio);
5651 return (CTL_RETVAL_COMPLETE);
5653 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5656 len = lun->be_lun->blocksize;
5659 * If we've got a kernel request that hasn't been malloced yet,
5660 * malloc it and tell the caller the data buffer is here.
5662 if ((byte2 & SWS_NDOB) == 0 &&
5663 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5664 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5665 ctsio->kern_data_len = len;
5666 ctsio->kern_total_len = len;
5667 ctsio->kern_data_resid = 0;
5668 ctsio->kern_rel_offset = 0;
5669 ctsio->kern_sg_entries = 0;
5670 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5671 ctsio->be_move_done = ctl_config_move_done;
5672 ctl_datamove((union ctl_io *)ctsio);
5674 return (CTL_RETVAL_COMPLETE);
5677 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5679 lbalen->len = num_blocks;
5680 lbalen->flags = byte2;
5681 retval = lun->backend->config_write((union ctl_io *)ctsio);
5687 ctl_unmap(struct ctl_scsiio *ctsio)
5689 struct ctl_lun *lun = CTL_LUN(ctsio);
5690 struct scsi_unmap *cdb;
5691 struct ctl_ptr_len_flags *ptrlen;
5692 struct scsi_unmap_header *hdr;
5693 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5695 uint32_t num_blocks;
5699 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5701 cdb = (struct scsi_unmap *)ctsio->cdb;
5702 len = scsi_2btoul(cdb->length);
5706 * If we've got a kernel request that hasn't been malloced yet,
5707 * malloc it and tell the caller the data buffer is here.
5709 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5710 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5711 ctsio->kern_data_len = len;
5712 ctsio->kern_total_len = len;
5713 ctsio->kern_data_resid = 0;
5714 ctsio->kern_rel_offset = 0;
5715 ctsio->kern_sg_entries = 0;
5716 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5717 ctsio->be_move_done = ctl_config_move_done;
5718 ctl_datamove((union ctl_io *)ctsio);
5720 return (CTL_RETVAL_COMPLETE);
5723 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5724 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5725 if (len < sizeof (*hdr) ||
5726 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5727 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5728 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5729 ctl_set_invalid_field(ctsio,
5737 len = scsi_2btoul(hdr->desc_length);
5738 buf = (struct scsi_unmap_desc *)(hdr + 1);
5739 end = buf + len / sizeof(*buf);
5742 for (range = buf; range < end; range++) {
5743 lba = scsi_8btou64(range->lba);
5744 num_blocks = scsi_4btoul(range->length);
5745 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5746 || ((lba + num_blocks) < lba)) {
5747 ctl_set_lba_out_of_range(ctsio,
5748 MAX(lba, lun->be_lun->maxlba + 1));
5749 ctl_done((union ctl_io *)ctsio);
5750 return (CTL_RETVAL_COMPLETE);
5752 if (num_blocks != 0)
5757 * Block backend can not handle zero last range.
5758 * Filter it out and return if there is nothing left.
5760 len = (uint8_t *)endnz - (uint8_t *)buf;
5762 ctl_set_success(ctsio);
5766 mtx_lock(&lun->lun_lock);
5767 ptrlen = (struct ctl_ptr_len_flags *)
5768 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5769 ptrlen->ptr = (void *)buf;
5771 ptrlen->flags = byte2;
5772 ctl_check_blocked(lun);
5773 mtx_unlock(&lun->lun_lock);
5775 retval = lun->backend->config_write((union ctl_io *)ctsio);
5779 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5780 free(ctsio->kern_data_ptr, M_CTL);
5781 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5783 ctl_done((union ctl_io *)ctsio);
5784 return (CTL_RETVAL_COMPLETE);
5788 ctl_default_page_handler(struct ctl_scsiio *ctsio,
5789 struct ctl_page_index *page_index, uint8_t *page_ptr)
5791 struct ctl_lun *lun = CTL_LUN(ctsio);
5792 uint8_t *current_cp;
5796 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5799 current_cp = (page_index->page_data + (page_index->page_len *
5802 mtx_lock(&lun->lun_lock);
5803 if (memcmp(current_cp, page_ptr, page_index->page_len)) {
5804 memcpy(current_cp, page_ptr, page_index->page_len);
5808 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5809 mtx_unlock(&lun->lun_lock);
5811 ctl_isc_announce_mode(lun,
5812 ctl_get_initindex(&ctsio->io_hdr.nexus),
5813 page_index->page_code, page_index->subpage);
5815 return (CTL_RETVAL_COMPLETE);
5819 ctl_ie_timer(void *arg)
5821 struct ctl_lun *lun = arg;
5824 if (lun->ie_asc == 0)
5827 if (lun->MODE_IE.mrie == SIEP_MRIE_UA)
5828 ctl_est_ua_all(lun, -1, CTL_UA_IE);
5830 lun->ie_reported = 0;
5832 if (lun->ie_reportcnt < scsi_4btoul(lun->MODE_IE.report_count)) {
5833 lun->ie_reportcnt++;
5834 t = scsi_4btoul(lun->MODE_IE.interval_timer);
5835 if (t == 0 || t == UINT32_MAX)
5836 t = 3000; /* 5 min */
5837 callout_schedule(&lun->ie_callout, t * hz / 10);
5842 ctl_ie_page_handler(struct ctl_scsiio *ctsio,
5843 struct ctl_page_index *page_index, uint8_t *page_ptr)
5845 struct ctl_lun *lun = CTL_LUN(ctsio);
5846 struct scsi_info_exceptions_page *pg;
5849 (void)ctl_default_page_handler(ctsio, page_index, page_ptr);
5851 pg = (struct scsi_info_exceptions_page *)page_ptr;
5852 mtx_lock(&lun->lun_lock);
5853 if (pg->info_flags & SIEP_FLAGS_TEST) {
5855 lun->ie_ascq = 0xff;
5856 if (pg->mrie == SIEP_MRIE_UA) {
5857 ctl_est_ua_all(lun, -1, CTL_UA_IE);
5858 lun->ie_reported = 1;
5860 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
5861 lun->ie_reported = -1;
5863 lun->ie_reportcnt = 1;
5864 if (lun->ie_reportcnt < scsi_4btoul(pg->report_count)) {
5865 lun->ie_reportcnt++;
5866 t = scsi_4btoul(pg->interval_timer);
5867 if (t == 0 || t == UINT32_MAX)
5868 t = 3000; /* 5 min */
5869 callout_reset(&lun->ie_callout, t * hz / 10,
5875 lun->ie_reported = 1;
5876 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
5877 lun->ie_reportcnt = UINT32_MAX;
5878 callout_stop(&lun->ie_callout);
5880 mtx_unlock(&lun->lun_lock);
5881 return (CTL_RETVAL_COMPLETE);
5885 ctl_do_mode_select(union ctl_io *io)
5887 struct ctl_lun *lun = CTL_LUN(io);
5888 struct scsi_mode_page_header *page_header;
5889 struct ctl_page_index *page_index;
5890 struct ctl_scsiio *ctsio;
5891 int page_len, page_len_offset, page_len_size;
5892 union ctl_modepage_info *modepage_info;
5893 uint16_t *len_left, *len_used;
5896 ctsio = &io->scsiio;
5900 modepage_info = (union ctl_modepage_info *)
5901 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
5902 len_left = &modepage_info->header.len_left;
5903 len_used = &modepage_info->header.len_used;
5907 page_header = (struct scsi_mode_page_header *)
5908 (ctsio->kern_data_ptr + *len_used);
5910 if (*len_left == 0) {
5911 free(ctsio->kern_data_ptr, M_CTL);
5912 ctl_set_success(ctsio);
5913 ctl_done((union ctl_io *)ctsio);
5914 return (CTL_RETVAL_COMPLETE);
5915 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
5917 free(ctsio->kern_data_ptr, M_CTL);
5918 ctl_set_param_len_error(ctsio);
5919 ctl_done((union ctl_io *)ctsio);
5920 return (CTL_RETVAL_COMPLETE);
5922 } else if ((page_header->page_code & SMPH_SPF)
5923 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
5925 free(ctsio->kern_data_ptr, M_CTL);
5926 ctl_set_param_len_error(ctsio);
5927 ctl_done((union ctl_io *)ctsio);
5928 return (CTL_RETVAL_COMPLETE);
5933 * XXX KDM should we do something with the block descriptor?
5935 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
5936 page_index = &lun->mode_pages.index[i];
5937 if (lun->be_lun->lun_type == T_DIRECT &&
5938 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
5940 if (lun->be_lun->lun_type == T_PROCESSOR &&
5941 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
5943 if (lun->be_lun->lun_type == T_CDROM &&
5944 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
5947 if ((page_index->page_code & SMPH_PC_MASK) !=
5948 (page_header->page_code & SMPH_PC_MASK))
5952 * If neither page has a subpage code, then we've got a
5955 if (((page_index->page_code & SMPH_SPF) == 0)
5956 && ((page_header->page_code & SMPH_SPF) == 0)) {
5957 page_len = page_header->page_length;
5962 * If both pages have subpages, then the subpage numbers
5965 if ((page_index->page_code & SMPH_SPF)
5966 && (page_header->page_code & SMPH_SPF)) {
5967 struct scsi_mode_page_header_sp *sph;
5969 sph = (struct scsi_mode_page_header_sp *)page_header;
5970 if (page_index->subpage == sph->subpage) {
5971 page_len = scsi_2btoul(sph->page_length);
5978 * If we couldn't find the page, or if we don't have a mode select
5979 * handler for it, send back an error to the user.
5981 if ((i >= CTL_NUM_MODE_PAGES)
5982 || (page_index->select_handler == NULL)) {
5983 ctl_set_invalid_field(ctsio,
5986 /*field*/ *len_used,
5989 free(ctsio->kern_data_ptr, M_CTL);
5990 ctl_done((union ctl_io *)ctsio);
5991 return (CTL_RETVAL_COMPLETE);
5994 if (page_index->page_code & SMPH_SPF) {
5995 page_len_offset = 2;
5999 page_len_offset = 1;
6003 * If the length the initiator gives us isn't the one we specify in
6004 * the mode page header, or if they didn't specify enough data in
6005 * the CDB to avoid truncating this page, kick out the request.
6007 if (page_len != page_index->page_len - page_len_offset - page_len_size) {
6008 ctl_set_invalid_field(ctsio,
6011 /*field*/ *len_used + page_len_offset,
6014 free(ctsio->kern_data_ptr, M_CTL);
6015 ctl_done((union ctl_io *)ctsio);
6016 return (CTL_RETVAL_COMPLETE);
6018 if (*len_left < page_index->page_len) {
6019 free(ctsio->kern_data_ptr, M_CTL);
6020 ctl_set_param_len_error(ctsio);
6021 ctl_done((union ctl_io *)ctsio);
6022 return (CTL_RETVAL_COMPLETE);
6026 * Run through the mode page, checking to make sure that the bits
6027 * the user changed are actually legal for him to change.
6029 for (i = 0; i < page_index->page_len; i++) {
6030 uint8_t *user_byte, *change_mask, *current_byte;
6034 user_byte = (uint8_t *)page_header + i;
6035 change_mask = page_index->page_data +
6036 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6037 current_byte = page_index->page_data +
6038 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6041 * Check to see whether the user set any bits in this byte
6042 * that he is not allowed to set.
6044 if ((*user_byte & ~(*change_mask)) ==
6045 (*current_byte & ~(*change_mask)))
6049 * Go through bit by bit to determine which one is illegal.
6052 for (j = 7; j >= 0; j--) {
6053 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6054 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6059 ctl_set_invalid_field(ctsio,
6062 /*field*/ *len_used + i,
6065 free(ctsio->kern_data_ptr, M_CTL);
6066 ctl_done((union ctl_io *)ctsio);
6067 return (CTL_RETVAL_COMPLETE);
6071 * Decrement these before we call the page handler, since we may
6072 * end up getting called back one way or another before the handler
6073 * returns to this context.
6075 *len_left -= page_index->page_len;
6076 *len_used += page_index->page_len;
6078 retval = page_index->select_handler(ctsio, page_index,
6079 (uint8_t *)page_header);
6082 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6083 * wait until this queued command completes to finish processing
6084 * the mode page. If it returns anything other than
6085 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6086 * already set the sense information, freed the data pointer, and
6087 * completed the io for us.
6089 if (retval != CTL_RETVAL_COMPLETE)
6090 goto bailout_no_done;
6093 * If the initiator sent us more than one page, parse the next one.
6098 ctl_set_success(ctsio);
6099 free(ctsio->kern_data_ptr, M_CTL);
6100 ctl_done((union ctl_io *)ctsio);
6104 return (CTL_RETVAL_COMPLETE);
6109 ctl_mode_select(struct ctl_scsiio *ctsio)
6111 struct ctl_lun *lun = CTL_LUN(ctsio);
6112 union ctl_modepage_info *modepage_info;
6113 int bd_len, i, header_size, param_len, pf, rtd, sp;
6116 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6117 switch (ctsio->cdb[0]) {
6118 case MODE_SELECT_6: {
6119 struct scsi_mode_select_6 *cdb;
6121 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6123 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6124 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6125 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6126 param_len = cdb->length;
6127 header_size = sizeof(struct scsi_mode_header_6);
6130 case MODE_SELECT_10: {
6131 struct scsi_mode_select_10 *cdb;
6133 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6135 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6136 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6137 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6138 param_len = scsi_2btoul(cdb->length);
6139 header_size = sizeof(struct scsi_mode_header_10);
6143 ctl_set_invalid_opcode(ctsio);
6144 ctl_done((union ctl_io *)ctsio);
6145 return (CTL_RETVAL_COMPLETE);
6149 if (param_len != 0) {
6150 ctl_set_invalid_field(ctsio, /*sks_valid*/ 0,
6151 /*command*/ 1, /*field*/ 0,
6152 /*bit_valid*/ 0, /*bit*/ 0);
6153 ctl_done((union ctl_io *)ctsio);
6154 return (CTL_RETVAL_COMPLETE);
6157 /* Revert to defaults. */
6158 ctl_init_page_index(lun);
6159 mtx_lock(&lun->lun_lock);
6160 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
6161 mtx_unlock(&lun->lun_lock);
6162 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6163 ctl_isc_announce_mode(lun, -1,
6164 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
6165 lun->mode_pages.index[i].subpage);
6167 ctl_set_success(ctsio);
6168 ctl_done((union ctl_io *)ctsio);
6169 return (CTL_RETVAL_COMPLETE);
6174 * "A parameter list length of zero indicates that the Data-Out Buffer
6175 * shall be empty. This condition shall not be considered as an error."
6177 if (param_len == 0) {
6178 ctl_set_success(ctsio);
6179 ctl_done((union ctl_io *)ctsio);
6180 return (CTL_RETVAL_COMPLETE);
6184 * Since we'll hit this the first time through, prior to
6185 * allocation, we don't need to free a data buffer here.
6187 if (param_len < header_size) {
6188 ctl_set_param_len_error(ctsio);
6189 ctl_done((union ctl_io *)ctsio);
6190 return (CTL_RETVAL_COMPLETE);
6194 * Allocate the data buffer and grab the user's data. In theory,
6195 * we shouldn't have to sanity check the parameter list length here
6196 * because the maximum size is 64K. We should be able to malloc
6197 * that much without too many problems.
6199 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6200 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6201 ctsio->kern_data_len = param_len;
6202 ctsio->kern_total_len = param_len;
6203 ctsio->kern_data_resid = 0;
6204 ctsio->kern_rel_offset = 0;
6205 ctsio->kern_sg_entries = 0;
6206 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6207 ctsio->be_move_done = ctl_config_move_done;
6208 ctl_datamove((union ctl_io *)ctsio);
6210 return (CTL_RETVAL_COMPLETE);
6213 switch (ctsio->cdb[0]) {
6214 case MODE_SELECT_6: {
6215 struct scsi_mode_header_6 *mh6;
6217 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6218 bd_len = mh6->blk_desc_len;
6221 case MODE_SELECT_10: {
6222 struct scsi_mode_header_10 *mh10;
6224 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6225 bd_len = scsi_2btoul(mh10->blk_desc_len);
6229 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6232 if (param_len < (header_size + bd_len)) {
6233 free(ctsio->kern_data_ptr, M_CTL);
6234 ctl_set_param_len_error(ctsio);
6235 ctl_done((union ctl_io *)ctsio);
6236 return (CTL_RETVAL_COMPLETE);
6240 * Set the IO_CONT flag, so that if this I/O gets passed to
6241 * ctl_config_write_done(), it'll get passed back to
6242 * ctl_do_mode_select() for further processing, or completion if
6245 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6246 ctsio->io_cont = ctl_do_mode_select;
6248 modepage_info = (union ctl_modepage_info *)
6249 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6250 memset(modepage_info, 0, sizeof(*modepage_info));
6251 modepage_info->header.len_left = param_len - header_size - bd_len;
6252 modepage_info->header.len_used = header_size + bd_len;
6254 return (ctl_do_mode_select((union ctl_io *)ctsio));
6258 ctl_mode_sense(struct ctl_scsiio *ctsio)
6260 struct ctl_lun *lun = CTL_LUN(ctsio);
6261 int pc, page_code, dbd, llba, subpage;
6262 int alloc_len, page_len, header_len, total_len;
6263 struct scsi_mode_block_descr *block_desc;
6264 struct ctl_page_index *page_index;
6270 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6272 switch (ctsio->cdb[0]) {
6273 case MODE_SENSE_6: {
6274 struct scsi_mode_sense_6 *cdb;
6276 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6278 header_len = sizeof(struct scsi_mode_hdr_6);
6279 if (cdb->byte2 & SMS_DBD)
6282 header_len += sizeof(struct scsi_mode_block_descr);
6284 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6285 page_code = cdb->page & SMS_PAGE_CODE;
6286 subpage = cdb->subpage;
6287 alloc_len = cdb->length;
6290 case MODE_SENSE_10: {
6291 struct scsi_mode_sense_10 *cdb;
6293 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6295 header_len = sizeof(struct scsi_mode_hdr_10);
6297 if (cdb->byte2 & SMS_DBD)
6300 header_len += sizeof(struct scsi_mode_block_descr);
6301 if (cdb->byte2 & SMS10_LLBAA)
6303 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6304 page_code = cdb->page & SMS_PAGE_CODE;
6305 subpage = cdb->subpage;
6306 alloc_len = scsi_2btoul(cdb->length);
6310 ctl_set_invalid_opcode(ctsio);
6311 ctl_done((union ctl_io *)ctsio);
6312 return (CTL_RETVAL_COMPLETE);
6313 break; /* NOTREACHED */
6317 * We have to make a first pass through to calculate the size of
6318 * the pages that match the user's query. Then we allocate enough
6319 * memory to hold it, and actually copy the data into the buffer.
6321 switch (page_code) {
6322 case SMS_ALL_PAGES_PAGE: {
6328 * At the moment, values other than 0 and 0xff here are
6329 * reserved according to SPC-3.
6331 if ((subpage != SMS_SUBPAGE_PAGE_0)
6332 && (subpage != SMS_SUBPAGE_ALL)) {
6333 ctl_set_invalid_field(ctsio,
6339 ctl_done((union ctl_io *)ctsio);
6340 return (CTL_RETVAL_COMPLETE);
6343 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6344 page_index = &lun->mode_pages.index[i];
6346 /* Make sure the page is supported for this dev type */
6347 if (lun->be_lun->lun_type == T_DIRECT &&
6348 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6350 if (lun->be_lun->lun_type == T_PROCESSOR &&
6351 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6353 if (lun->be_lun->lun_type == T_CDROM &&
6354 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6358 * We don't use this subpage if the user didn't
6359 * request all subpages.
6361 if ((page_index->subpage != 0)
6362 && (subpage == SMS_SUBPAGE_PAGE_0))
6366 printf("found page %#x len %d\n",
6367 page_index->page_code & SMPH_PC_MASK,
6368 page_index->page_len);
6370 page_len += page_index->page_len;
6379 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6380 page_index = &lun->mode_pages.index[i];
6382 /* Make sure the page is supported for this dev type */
6383 if (lun->be_lun->lun_type == T_DIRECT &&
6384 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6386 if (lun->be_lun->lun_type == T_PROCESSOR &&
6387 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6389 if (lun->be_lun->lun_type == T_CDROM &&
6390 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6393 /* Look for the right page code */
6394 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6397 /* Look for the right subpage or the subpage wildcard*/
6398 if ((page_index->subpage != subpage)
6399 && (subpage != SMS_SUBPAGE_ALL))
6403 printf("found page %#x len %d\n",
6404 page_index->page_code & SMPH_PC_MASK,
6405 page_index->page_len);
6408 page_len += page_index->page_len;
6411 if (page_len == 0) {
6412 ctl_set_invalid_field(ctsio,
6418 ctl_done((union ctl_io *)ctsio);
6419 return (CTL_RETVAL_COMPLETE);
6425 total_len = header_len + page_len;
6427 printf("header_len = %d, page_len = %d, total_len = %d\n",
6428 header_len, page_len, total_len);
6431 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6432 ctsio->kern_sg_entries = 0;
6433 ctsio->kern_data_resid = 0;
6434 ctsio->kern_rel_offset = 0;
6435 if (total_len < alloc_len) {
6436 ctsio->residual = alloc_len - total_len;
6437 ctsio->kern_data_len = total_len;
6438 ctsio->kern_total_len = total_len;
6440 ctsio->residual = 0;
6441 ctsio->kern_data_len = alloc_len;
6442 ctsio->kern_total_len = alloc_len;
6445 switch (ctsio->cdb[0]) {
6446 case MODE_SENSE_6: {
6447 struct scsi_mode_hdr_6 *header;
6449 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6451 header->datalen = MIN(total_len - 1, 254);
6452 if (lun->be_lun->lun_type == T_DIRECT) {
6453 header->dev_specific = 0x10; /* DPOFUA */
6454 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6455 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6456 header->dev_specific |= 0x80; /* WP */
6459 header->block_descr_len = 0;
6461 header->block_descr_len =
6462 sizeof(struct scsi_mode_block_descr);
6463 block_desc = (struct scsi_mode_block_descr *)&header[1];
6466 case MODE_SENSE_10: {
6467 struct scsi_mode_hdr_10 *header;
6470 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6472 datalen = MIN(total_len - 2, 65533);
6473 scsi_ulto2b(datalen, header->datalen);
6474 if (lun->be_lun->lun_type == T_DIRECT) {
6475 header->dev_specific = 0x10; /* DPOFUA */
6476 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6477 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6478 header->dev_specific |= 0x80; /* WP */
6481 scsi_ulto2b(0, header->block_descr_len);
6483 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6484 header->block_descr_len);
6485 block_desc = (struct scsi_mode_block_descr *)&header[1];
6489 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6493 * If we've got a disk, use its blocksize in the block
6494 * descriptor. Otherwise, just set it to 0.
6497 if (lun->be_lun->lun_type == T_DIRECT)
6498 scsi_ulto3b(lun->be_lun->blocksize,
6499 block_desc->block_len);
6501 scsi_ulto3b(0, block_desc->block_len);
6504 switch (page_code) {
6505 case SMS_ALL_PAGES_PAGE: {
6508 data_used = header_len;
6509 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6510 struct ctl_page_index *page_index;
6512 page_index = &lun->mode_pages.index[i];
6513 if (lun->be_lun->lun_type == T_DIRECT &&
6514 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6516 if (lun->be_lun->lun_type == T_PROCESSOR &&
6517 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6519 if (lun->be_lun->lun_type == T_CDROM &&
6520 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6524 * We don't use this subpage if the user didn't
6525 * request all subpages. We already checked (above)
6526 * to make sure the user only specified a subpage
6527 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6529 if ((page_index->subpage != 0)
6530 && (subpage == SMS_SUBPAGE_PAGE_0))
6534 * Call the handler, if it exists, to update the
6535 * page to the latest values.
6537 if (page_index->sense_handler != NULL)
6538 page_index->sense_handler(ctsio, page_index,pc);
6540 memcpy(ctsio->kern_data_ptr + data_used,
6541 page_index->page_data +
6542 (page_index->page_len * pc),
6543 page_index->page_len);
6544 data_used += page_index->page_len;
6551 data_used = header_len;
6553 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6554 struct ctl_page_index *page_index;
6556 page_index = &lun->mode_pages.index[i];
6558 /* Look for the right page code */
6559 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6562 /* Look for the right subpage or the subpage wildcard*/
6563 if ((page_index->subpage != subpage)
6564 && (subpage != SMS_SUBPAGE_ALL))
6567 /* Make sure the page is supported for this dev type */
6568 if (lun->be_lun->lun_type == T_DIRECT &&
6569 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6571 if (lun->be_lun->lun_type == T_PROCESSOR &&
6572 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6574 if (lun->be_lun->lun_type == T_CDROM &&
6575 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6579 * Call the handler, if it exists, to update the
6580 * page to the latest values.
6582 if (page_index->sense_handler != NULL)
6583 page_index->sense_handler(ctsio, page_index,pc);
6585 memcpy(ctsio->kern_data_ptr + data_used,
6586 page_index->page_data +
6587 (page_index->page_len * pc),
6588 page_index->page_len);
6589 data_used += page_index->page_len;
6595 ctl_set_success(ctsio);
6596 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6597 ctsio->be_move_done = ctl_config_move_done;
6598 ctl_datamove((union ctl_io *)ctsio);
6599 return (CTL_RETVAL_COMPLETE);
6603 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6604 struct ctl_page_index *page_index,
6607 struct ctl_lun *lun = CTL_LUN(ctsio);
6608 struct scsi_log_param_header *phdr;
6612 data = page_index->page_data;
6614 if (lun->backend->lun_attr != NULL &&
6615 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6617 phdr = (struct scsi_log_param_header *)data;
6618 scsi_ulto2b(0x0001, phdr->param_code);
6619 phdr->param_control = SLP_LBIN | SLP_LP;
6620 phdr->param_len = 8;
6621 data = (uint8_t *)(phdr + 1);
6622 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6623 data[4] = 0x02; /* per-pool */
6624 data += phdr->param_len;
6627 if (lun->backend->lun_attr != NULL &&
6628 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6630 phdr = (struct scsi_log_param_header *)data;
6631 scsi_ulto2b(0x0002, phdr->param_code);
6632 phdr->param_control = SLP_LBIN | SLP_LP;
6633 phdr->param_len = 8;
6634 data = (uint8_t *)(phdr + 1);
6635 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6636 data[4] = 0x01; /* per-LUN */
6637 data += phdr->param_len;
6640 if (lun->backend->lun_attr != NULL &&
6641 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6643 phdr = (struct scsi_log_param_header *)data;
6644 scsi_ulto2b(0x00f1, phdr->param_code);
6645 phdr->param_control = SLP_LBIN | SLP_LP;
6646 phdr->param_len = 8;
6647 data = (uint8_t *)(phdr + 1);
6648 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6649 data[4] = 0x02; /* per-pool */
6650 data += phdr->param_len;
6653 if (lun->backend->lun_attr != NULL &&
6654 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6656 phdr = (struct scsi_log_param_header *)data;
6657 scsi_ulto2b(0x00f2, phdr->param_code);
6658 phdr->param_control = SLP_LBIN | SLP_LP;
6659 phdr->param_len = 8;
6660 data = (uint8_t *)(phdr + 1);
6661 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6662 data[4] = 0x02; /* per-pool */
6663 data += phdr->param_len;
6666 page_index->page_len = data - page_index->page_data;
6671 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
6672 struct ctl_page_index *page_index,
6675 struct ctl_lun *lun = CTL_LUN(ctsio);
6676 struct stat_page *data;
6677 uint64_t rn, wn, rb, wb;
6678 struct bintime rt, wt;
6681 data = (struct stat_page *)page_index->page_data;
6683 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
6684 data->sap.hdr.param_control = SLP_LBIN;
6685 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
6686 sizeof(struct scsi_log_param_header);
6687 rn = wn = rb = wb = 0;
6690 for (i = 0; i < CTL_MAX_PORTS; i++) {
6691 rn += lun->stats.ports[i].operations[CTL_STATS_READ];
6692 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE];
6693 rb += lun->stats.ports[i].bytes[CTL_STATS_READ];
6694 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE];
6695 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]);
6696 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]);
6698 scsi_u64to8b(rn, data->sap.read_num);
6699 scsi_u64to8b(wn, data->sap.write_num);
6700 if (lun->stats.blocksize > 0) {
6701 scsi_u64to8b(wb / lun->stats.blocksize,
6702 data->sap.recvieved_lba);
6703 scsi_u64to8b(rb / lun->stats.blocksize,
6704 data->sap.transmitted_lba);
6706 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000),
6707 data->sap.read_int);
6708 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000),
6709 data->sap.write_int);
6710 scsi_u64to8b(0, data->sap.weighted_num);
6711 scsi_u64to8b(0, data->sap.weighted_int);
6712 scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
6713 data->it.hdr.param_control = SLP_LBIN;
6714 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
6715 sizeof(struct scsi_log_param_header);
6717 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
6719 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
6720 data->it.hdr.param_control = SLP_LBIN;
6721 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
6722 sizeof(struct scsi_log_param_header);
6723 scsi_ulto4b(3, data->ti.exponent);
6724 scsi_ulto4b(1, data->ti.integer);
6729 ctl_ie_log_sense_handler(struct ctl_scsiio *ctsio,
6730 struct ctl_page_index *page_index,
6733 struct ctl_lun *lun = CTL_LUN(ctsio);
6734 struct scsi_log_informational_exceptions *data;
6736 data = (struct scsi_log_informational_exceptions *)page_index->page_data;
6738 scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code);
6739 data->hdr.param_control = SLP_LBIN;
6740 data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) -
6741 sizeof(struct scsi_log_param_header);
6742 data->ie_asc = lun->ie_asc;
6743 data->ie_ascq = lun->ie_ascq;
6744 data->temperature = 0xff;
6749 ctl_log_sense(struct ctl_scsiio *ctsio)
6751 struct ctl_lun *lun = CTL_LUN(ctsio);
6752 int i, pc, page_code, subpage;
6753 int alloc_len, total_len;
6754 struct ctl_page_index *page_index;
6755 struct scsi_log_sense *cdb;
6756 struct scsi_log_header *header;
6758 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6760 cdb = (struct scsi_log_sense *)ctsio->cdb;
6761 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6762 page_code = cdb->page & SLS_PAGE_CODE;
6763 subpage = cdb->subpage;
6764 alloc_len = scsi_2btoul(cdb->length);
6767 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6768 page_index = &lun->log_pages.index[i];
6770 /* Look for the right page code */
6771 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6774 /* Look for the right subpage or the subpage wildcard*/
6775 if (page_index->subpage != subpage)
6780 if (i >= CTL_NUM_LOG_PAGES) {
6781 ctl_set_invalid_field(ctsio,
6787 ctl_done((union ctl_io *)ctsio);
6788 return (CTL_RETVAL_COMPLETE);
6791 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6793 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6794 ctsio->kern_sg_entries = 0;
6795 ctsio->kern_data_resid = 0;
6796 ctsio->kern_rel_offset = 0;
6797 if (total_len < alloc_len) {
6798 ctsio->residual = alloc_len - total_len;
6799 ctsio->kern_data_len = total_len;
6800 ctsio->kern_total_len = total_len;
6802 ctsio->residual = 0;
6803 ctsio->kern_data_len = alloc_len;
6804 ctsio->kern_total_len = alloc_len;
6807 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
6808 header->page = page_index->page_code;
6809 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING)
6810 header->page |= SL_DS;
6811 if (page_index->subpage) {
6812 header->page |= SL_SPF;
6813 header->subpage = page_index->subpage;
6815 scsi_ulto2b(page_index->page_len, header->datalen);
6818 * Call the handler, if it exists, to update the
6819 * page to the latest values.
6821 if (page_index->sense_handler != NULL)
6822 page_index->sense_handler(ctsio, page_index, pc);
6824 memcpy(header + 1, page_index->page_data, page_index->page_len);
6826 ctl_set_success(ctsio);
6827 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6828 ctsio->be_move_done = ctl_config_move_done;
6829 ctl_datamove((union ctl_io *)ctsio);
6830 return (CTL_RETVAL_COMPLETE);
6834 ctl_read_capacity(struct ctl_scsiio *ctsio)
6836 struct ctl_lun *lun = CTL_LUN(ctsio);
6837 struct scsi_read_capacity *cdb;
6838 struct scsi_read_capacity_data *data;
6841 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6843 cdb = (struct scsi_read_capacity *)ctsio->cdb;
6845 lba = scsi_4btoul(cdb->addr);
6846 if (((cdb->pmi & SRC_PMI) == 0)
6848 ctl_set_invalid_field(/*ctsio*/ ctsio,
6854 ctl_done((union ctl_io *)ctsio);
6855 return (CTL_RETVAL_COMPLETE);
6858 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6859 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6860 ctsio->residual = 0;
6861 ctsio->kern_data_len = sizeof(*data);
6862 ctsio->kern_total_len = sizeof(*data);
6863 ctsio->kern_data_resid = 0;
6864 ctsio->kern_rel_offset = 0;
6865 ctsio->kern_sg_entries = 0;
6868 * If the maximum LBA is greater than 0xfffffffe, the user must
6869 * issue a SERVICE ACTION IN (16) command, with the read capacity
6870 * serivce action set.
6872 if (lun->be_lun->maxlba > 0xfffffffe)
6873 scsi_ulto4b(0xffffffff, data->addr);
6875 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6878 * XXX KDM this may not be 512 bytes...
6880 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6882 ctl_set_success(ctsio);
6883 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6884 ctsio->be_move_done = ctl_config_move_done;
6885 ctl_datamove((union ctl_io *)ctsio);
6886 return (CTL_RETVAL_COMPLETE);
6890 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
6892 struct ctl_lun *lun = CTL_LUN(ctsio);
6893 struct scsi_read_capacity_16 *cdb;
6894 struct scsi_read_capacity_data_long *data;
6898 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
6900 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
6902 alloc_len = scsi_4btoul(cdb->alloc_len);
6903 lba = scsi_8btou64(cdb->addr);
6905 if ((cdb->reladr & SRC16_PMI)
6907 ctl_set_invalid_field(/*ctsio*/ ctsio,
6913 ctl_done((union ctl_io *)ctsio);
6914 return (CTL_RETVAL_COMPLETE);
6917 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6918 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
6920 if (sizeof(*data) < alloc_len) {
6921 ctsio->residual = alloc_len - sizeof(*data);
6922 ctsio->kern_data_len = sizeof(*data);
6923 ctsio->kern_total_len = sizeof(*data);
6925 ctsio->residual = 0;
6926 ctsio->kern_data_len = alloc_len;
6927 ctsio->kern_total_len = alloc_len;
6929 ctsio->kern_data_resid = 0;
6930 ctsio->kern_rel_offset = 0;
6931 ctsio->kern_sg_entries = 0;
6933 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
6934 /* XXX KDM this may not be 512 bytes... */
6935 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6936 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
6937 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
6938 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
6939 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
6941 ctl_set_success(ctsio);
6942 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6943 ctsio->be_move_done = ctl_config_move_done;
6944 ctl_datamove((union ctl_io *)ctsio);
6945 return (CTL_RETVAL_COMPLETE);
6949 ctl_get_lba_status(struct ctl_scsiio *ctsio)
6951 struct ctl_lun *lun = CTL_LUN(ctsio);
6952 struct scsi_get_lba_status *cdb;
6953 struct scsi_get_lba_status_data *data;
6954 struct ctl_lba_len_flags *lbalen;
6956 uint32_t alloc_len, total_len;
6959 CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
6961 cdb = (struct scsi_get_lba_status *)ctsio->cdb;
6962 lba = scsi_8btou64(cdb->addr);
6963 alloc_len = scsi_4btoul(cdb->alloc_len);
6965 if (lba > lun->be_lun->maxlba) {
6966 ctl_set_lba_out_of_range(ctsio, lba);
6967 ctl_done((union ctl_io *)ctsio);
6968 return (CTL_RETVAL_COMPLETE);
6971 total_len = sizeof(*data) + sizeof(data->descr[0]);
6972 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6973 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
6975 if (total_len < alloc_len) {
6976 ctsio->residual = alloc_len - total_len;
6977 ctsio->kern_data_len = total_len;
6978 ctsio->kern_total_len = total_len;
6980 ctsio->residual = 0;
6981 ctsio->kern_data_len = alloc_len;
6982 ctsio->kern_total_len = alloc_len;
6984 ctsio->kern_data_resid = 0;
6985 ctsio->kern_rel_offset = 0;
6986 ctsio->kern_sg_entries = 0;
6988 /* Fill dummy data in case backend can't tell anything. */
6989 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
6990 scsi_u64to8b(lba, data->descr[0].addr);
6991 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
6992 data->descr[0].length);
6993 data->descr[0].status = 0; /* Mapped or unknown. */
6995 ctl_set_success(ctsio);
6996 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6997 ctsio->be_move_done = ctl_config_move_done;
6999 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
7001 lbalen->len = total_len;
7003 retval = lun->backend->config_read((union ctl_io *)ctsio);
7004 return (CTL_RETVAL_COMPLETE);
7008 ctl_read_defect(struct ctl_scsiio *ctsio)
7010 struct scsi_read_defect_data_10 *ccb10;
7011 struct scsi_read_defect_data_12 *ccb12;
7012 struct scsi_read_defect_data_hdr_10 *data10;
7013 struct scsi_read_defect_data_hdr_12 *data12;
7014 uint32_t alloc_len, data_len;
7017 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7019 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7020 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7021 format = ccb10->format;
7022 alloc_len = scsi_2btoul(ccb10->alloc_length);
7023 data_len = sizeof(*data10);
7025 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7026 format = ccb12->format;
7027 alloc_len = scsi_4btoul(ccb12->alloc_length);
7028 data_len = sizeof(*data12);
7030 if (alloc_len == 0) {
7031 ctl_set_success(ctsio);
7032 ctl_done((union ctl_io *)ctsio);
7033 return (CTL_RETVAL_COMPLETE);
7036 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7037 if (data_len < alloc_len) {
7038 ctsio->residual = alloc_len - data_len;
7039 ctsio->kern_data_len = data_len;
7040 ctsio->kern_total_len = data_len;
7042 ctsio->residual = 0;
7043 ctsio->kern_data_len = alloc_len;
7044 ctsio->kern_total_len = alloc_len;
7046 ctsio->kern_data_resid = 0;
7047 ctsio->kern_rel_offset = 0;
7048 ctsio->kern_sg_entries = 0;
7050 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7051 data10 = (struct scsi_read_defect_data_hdr_10 *)
7052 ctsio->kern_data_ptr;
7053 data10->format = format;
7054 scsi_ulto2b(0, data10->length);
7056 data12 = (struct scsi_read_defect_data_hdr_12 *)
7057 ctsio->kern_data_ptr;
7058 data12->format = format;
7059 scsi_ulto2b(0, data12->generation);
7060 scsi_ulto4b(0, data12->length);
7063 ctl_set_success(ctsio);
7064 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7065 ctsio->be_move_done = ctl_config_move_done;
7066 ctl_datamove((union ctl_io *)ctsio);
7067 return (CTL_RETVAL_COMPLETE);
7071 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7073 struct ctl_softc *softc = CTL_SOFTC(ctsio);
7074 struct ctl_lun *lun = CTL_LUN(ctsio);
7075 struct scsi_maintenance_in *cdb;
7077 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os;
7078 int num_ha_groups, num_target_ports, shared_group;
7079 struct ctl_port *port;
7080 struct scsi_target_group_data *rtg_ptr;
7081 struct scsi_target_group_data_extended *rtg_ext_ptr;
7082 struct scsi_target_port_group_descriptor *tpg_desc;
7084 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7086 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7087 retval = CTL_RETVAL_COMPLETE;
7089 switch (cdb->byte2 & STG_PDF_MASK) {
7090 case STG_PDF_LENGTH:
7093 case STG_PDF_EXTENDED:
7097 ctl_set_invalid_field(/*ctsio*/ ctsio,
7103 ctl_done((union ctl_io *)ctsio);
7107 num_target_ports = 0;
7108 shared_group = (softc->is_single != 0);
7109 mtx_lock(&softc->ctl_lock);
7110 STAILQ_FOREACH(port, &softc->port_list, links) {
7111 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7113 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7116 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7119 mtx_unlock(&softc->ctl_lock);
7120 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES;
7123 total_len = sizeof(struct scsi_target_group_data_extended);
7125 total_len = sizeof(struct scsi_target_group_data);
7126 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7127 (shared_group + num_ha_groups) +
7128 sizeof(struct scsi_target_port_descriptor) * num_target_ports;
7130 alloc_len = scsi_4btoul(cdb->length);
7132 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7134 ctsio->kern_sg_entries = 0;
7136 if (total_len < alloc_len) {
7137 ctsio->residual = alloc_len - total_len;
7138 ctsio->kern_data_len = total_len;
7139 ctsio->kern_total_len = total_len;
7141 ctsio->residual = 0;
7142 ctsio->kern_data_len = alloc_len;
7143 ctsio->kern_total_len = alloc_len;
7145 ctsio->kern_data_resid = 0;
7146 ctsio->kern_rel_offset = 0;
7149 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7150 ctsio->kern_data_ptr;
7151 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7152 rtg_ext_ptr->format_type = 0x10;
7153 rtg_ext_ptr->implicit_transition_time = 0;
7154 tpg_desc = &rtg_ext_ptr->groups[0];
7156 rtg_ptr = (struct scsi_target_group_data *)
7157 ctsio->kern_data_ptr;
7158 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7159 tpg_desc = &rtg_ptr->groups[0];
7162 mtx_lock(&softc->ctl_lock);
7163 pg = softc->port_min / softc->port_cnt;
7164 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) {
7165 /* Some shelf is known to be primary. */
7166 if (softc->ha_link == CTL_HA_LINK_OFFLINE)
7167 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7168 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN)
7169 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7170 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY)
7171 os = TPG_ASYMMETRIC_ACCESS_STANDBY;
7173 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7174 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7175 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7178 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7181 /* No known primary shelf. */
7182 if (softc->ha_link == CTL_HA_LINK_OFFLINE) {
7183 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7184 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7185 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) {
7186 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7187 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7189 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7193 tpg_desc->pref_state = ts;
7194 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7195 TPG_U_SUP | TPG_T_SUP;
7196 scsi_ulto2b(1, tpg_desc->target_port_group);
7197 tpg_desc->status = TPG_IMPLICIT;
7199 STAILQ_FOREACH(port, &softc->port_list, links) {
7200 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7202 if (!softc->is_single &&
7203 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0)
7205 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7207 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7208 relative_target_port_identifier);
7211 tpg_desc->target_port_count = pc;
7212 tpg_desc = (struct scsi_target_port_group_descriptor *)
7213 &tpg_desc->descriptors[pc];
7215 for (g = 0; g < num_ha_groups; g++) {
7216 tpg_desc->pref_state = (g == pg) ? ts : os;
7217 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7218 TPG_U_SUP | TPG_T_SUP;
7219 scsi_ulto2b(2 + g, tpg_desc->target_port_group);
7220 tpg_desc->status = TPG_IMPLICIT;
7222 STAILQ_FOREACH(port, &softc->port_list, links) {
7223 if (port->targ_port < g * softc->port_cnt ||
7224 port->targ_port >= (g + 1) * softc->port_cnt)
7226 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7228 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7230 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7232 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7233 relative_target_port_identifier);
7236 tpg_desc->target_port_count = pc;
7237 tpg_desc = (struct scsi_target_port_group_descriptor *)
7238 &tpg_desc->descriptors[pc];
7240 mtx_unlock(&softc->ctl_lock);
7242 ctl_set_success(ctsio);
7243 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7244 ctsio->be_move_done = ctl_config_move_done;
7245 ctl_datamove((union ctl_io *)ctsio);
7250 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7252 struct ctl_lun *lun = CTL_LUN(ctsio);
7253 struct scsi_report_supported_opcodes *cdb;
7254 const struct ctl_cmd_entry *entry, *sentry;
7255 struct scsi_report_supported_opcodes_all *all;
7256 struct scsi_report_supported_opcodes_descr *descr;
7257 struct scsi_report_supported_opcodes_one *one;
7259 int alloc_len, total_len;
7260 int opcode, service_action, i, j, num;
7262 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7264 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7265 retval = CTL_RETVAL_COMPLETE;
7267 opcode = cdb->requested_opcode;
7268 service_action = scsi_2btoul(cdb->requested_service_action);
7269 switch (cdb->options & RSO_OPTIONS_MASK) {
7270 case RSO_OPTIONS_ALL:
7272 for (i = 0; i < 256; i++) {
7273 entry = &ctl_cmd_table[i];
7274 if (entry->flags & CTL_CMD_FLAG_SA5) {
7275 for (j = 0; j < 32; j++) {
7276 sentry = &((const struct ctl_cmd_entry *)
7278 if (ctl_cmd_applicable(
7279 lun->be_lun->lun_type, sentry))
7283 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7288 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7289 num * sizeof(struct scsi_report_supported_opcodes_descr);
7291 case RSO_OPTIONS_OC:
7292 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7293 ctl_set_invalid_field(/*ctsio*/ ctsio,
7299 ctl_done((union ctl_io *)ctsio);
7300 return (CTL_RETVAL_COMPLETE);
7302 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7304 case RSO_OPTIONS_OC_SA:
7305 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7306 service_action >= 32) {
7307 ctl_set_invalid_field(/*ctsio*/ ctsio,
7313 ctl_done((union ctl_io *)ctsio);
7314 return (CTL_RETVAL_COMPLETE);
7317 case RSO_OPTIONS_OC_ASA:
7318 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7321 ctl_set_invalid_field(/*ctsio*/ ctsio,
7327 ctl_done((union ctl_io *)ctsio);
7328 return (CTL_RETVAL_COMPLETE);
7331 alloc_len = scsi_4btoul(cdb->length);
7333 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7335 ctsio->kern_sg_entries = 0;
7337 if (total_len < alloc_len) {
7338 ctsio->residual = alloc_len - total_len;
7339 ctsio->kern_data_len = total_len;
7340 ctsio->kern_total_len = total_len;
7342 ctsio->residual = 0;
7343 ctsio->kern_data_len = alloc_len;
7344 ctsio->kern_total_len = alloc_len;
7346 ctsio->kern_data_resid = 0;
7347 ctsio->kern_rel_offset = 0;
7349 switch (cdb->options & RSO_OPTIONS_MASK) {
7350 case RSO_OPTIONS_ALL:
7351 all = (struct scsi_report_supported_opcodes_all *)
7352 ctsio->kern_data_ptr;
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))
7363 descr = &all->descr[num++];
7365 scsi_ulto2b(j, descr->service_action);
7366 descr->flags = RSO_SERVACTV;
7367 scsi_ulto2b(sentry->length,
7371 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7374 descr = &all->descr[num++];
7376 scsi_ulto2b(0, descr->service_action);
7378 scsi_ulto2b(entry->length, descr->cdb_length);
7382 num * sizeof(struct scsi_report_supported_opcodes_descr),
7385 case RSO_OPTIONS_OC:
7386 one = (struct scsi_report_supported_opcodes_one *)
7387 ctsio->kern_data_ptr;
7388 entry = &ctl_cmd_table[opcode];
7390 case RSO_OPTIONS_OC_SA:
7391 one = (struct scsi_report_supported_opcodes_one *)
7392 ctsio->kern_data_ptr;
7393 entry = &ctl_cmd_table[opcode];
7394 entry = &((const struct ctl_cmd_entry *)
7395 entry->execute)[service_action];
7397 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7399 scsi_ulto2b(entry->length, one->cdb_length);
7400 one->cdb_usage[0] = opcode;
7401 memcpy(&one->cdb_usage[1], entry->usage,
7406 case RSO_OPTIONS_OC_ASA:
7407 one = (struct scsi_report_supported_opcodes_one *)
7408 ctsio->kern_data_ptr;
7409 entry = &ctl_cmd_table[opcode];
7410 if (entry->flags & CTL_CMD_FLAG_SA5) {
7411 entry = &((const struct ctl_cmd_entry *)
7412 entry->execute)[service_action];
7413 } else if (service_action != 0) {
7420 ctl_set_success(ctsio);
7421 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7422 ctsio->be_move_done = ctl_config_move_done;
7423 ctl_datamove((union ctl_io *)ctsio);
7428 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7430 struct scsi_report_supported_tmf *cdb;
7431 struct scsi_report_supported_tmf_ext_data *data;
7433 int alloc_len, total_len;
7435 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7437 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7439 retval = CTL_RETVAL_COMPLETE;
7441 if (cdb->options & RST_REPD)
7442 total_len = sizeof(struct scsi_report_supported_tmf_ext_data);
7444 total_len = sizeof(struct scsi_report_supported_tmf_data);
7445 alloc_len = scsi_4btoul(cdb->length);
7447 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7449 ctsio->kern_sg_entries = 0;
7451 if (total_len < alloc_len) {
7452 ctsio->residual = alloc_len - total_len;
7453 ctsio->kern_data_len = total_len;
7454 ctsio->kern_total_len = total_len;
7456 ctsio->residual = 0;
7457 ctsio->kern_data_len = alloc_len;
7458 ctsio->kern_total_len = alloc_len;
7460 ctsio->kern_data_resid = 0;
7461 ctsio->kern_rel_offset = 0;
7463 data = (struct scsi_report_supported_tmf_ext_data *)ctsio->kern_data_ptr;
7464 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS |
7466 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS;
7467 data->length = total_len - 4;
7469 ctl_set_success(ctsio);
7470 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7471 ctsio->be_move_done = ctl_config_move_done;
7472 ctl_datamove((union ctl_io *)ctsio);
7477 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7479 struct scsi_report_timestamp *cdb;
7480 struct scsi_report_timestamp_data *data;
7484 int alloc_len, total_len;
7486 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7488 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7490 retval = CTL_RETVAL_COMPLETE;
7492 total_len = sizeof(struct scsi_report_timestamp_data);
7493 alloc_len = scsi_4btoul(cdb->length);
7495 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7497 ctsio->kern_sg_entries = 0;
7499 if (total_len < alloc_len) {
7500 ctsio->residual = alloc_len - total_len;
7501 ctsio->kern_data_len = total_len;
7502 ctsio->kern_total_len = total_len;
7504 ctsio->residual = 0;
7505 ctsio->kern_data_len = alloc_len;
7506 ctsio->kern_total_len = alloc_len;
7508 ctsio->kern_data_resid = 0;
7509 ctsio->kern_rel_offset = 0;
7511 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7512 scsi_ulto2b(sizeof(*data) - 2, data->length);
7513 data->origin = RTS_ORIG_OUTSIDE;
7515 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7516 scsi_ulto4b(timestamp >> 16, data->timestamp);
7517 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7519 ctl_set_success(ctsio);
7520 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7521 ctsio->be_move_done = ctl_config_move_done;
7522 ctl_datamove((union ctl_io *)ctsio);
7527 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7529 struct ctl_softc *softc = CTL_SOFTC(ctsio);
7530 struct ctl_lun *lun = CTL_LUN(ctsio);
7531 struct scsi_per_res_in *cdb;
7532 int alloc_len, total_len = 0;
7533 /* struct scsi_per_res_in_rsrv in_data; */
7536 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7538 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7540 alloc_len = scsi_2btoul(cdb->length);
7543 mtx_lock(&lun->lun_lock);
7544 switch (cdb->action) {
7545 case SPRI_RK: /* read keys */
7546 total_len = sizeof(struct scsi_per_res_in_keys) +
7548 sizeof(struct scsi_per_res_key);
7550 case SPRI_RR: /* read reservation */
7551 if (lun->flags & CTL_LUN_PR_RESERVED)
7552 total_len = sizeof(struct scsi_per_res_in_rsrv);
7554 total_len = sizeof(struct scsi_per_res_in_header);
7556 case SPRI_RC: /* report capabilities */
7557 total_len = sizeof(struct scsi_per_res_cap);
7559 case SPRI_RS: /* read full status */
7560 total_len = sizeof(struct scsi_per_res_in_header) +
7561 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7565 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7567 mtx_unlock(&lun->lun_lock);
7569 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7571 if (total_len < alloc_len) {
7572 ctsio->residual = alloc_len - total_len;
7573 ctsio->kern_data_len = total_len;
7574 ctsio->kern_total_len = total_len;
7576 ctsio->residual = 0;
7577 ctsio->kern_data_len = alloc_len;
7578 ctsio->kern_total_len = alloc_len;
7581 ctsio->kern_data_resid = 0;
7582 ctsio->kern_rel_offset = 0;
7583 ctsio->kern_sg_entries = 0;
7585 mtx_lock(&lun->lun_lock);
7586 switch (cdb->action) {
7587 case SPRI_RK: { // read keys
7588 struct scsi_per_res_in_keys *res_keys;
7591 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7594 * We had to drop the lock to allocate our buffer, which
7595 * leaves time for someone to come in with another
7596 * persistent reservation. (That is unlikely, though,
7597 * since this should be the only persistent reservation
7598 * command active right now.)
7600 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7601 (lun->pr_key_count *
7602 sizeof(struct scsi_per_res_key)))){
7603 mtx_unlock(&lun->lun_lock);
7604 free(ctsio->kern_data_ptr, M_CTL);
7605 printf("%s: reservation length changed, retrying\n",
7610 scsi_ulto4b(lun->pr_generation, res_keys->header.generation);
7612 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7613 lun->pr_key_count, res_keys->header.length);
7615 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) {
7616 if ((key = ctl_get_prkey(lun, i)) == 0)
7620 * We used lun->pr_key_count to calculate the
7621 * size to allocate. If it turns out the number of
7622 * initiators with the registered flag set is
7623 * larger than that (i.e. they haven't been kept in
7624 * sync), we've got a problem.
7626 if (key_count >= lun->pr_key_count) {
7630 scsi_u64to8b(key, res_keys->keys[key_count].key);
7635 case SPRI_RR: { // read reservation
7636 struct scsi_per_res_in_rsrv *res;
7637 int tmp_len, header_only;
7639 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7641 scsi_ulto4b(lun->pr_generation, res->header.generation);
7643 if (lun->flags & CTL_LUN_PR_RESERVED)
7645 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7646 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7647 res->header.length);
7650 tmp_len = sizeof(struct scsi_per_res_in_header);
7651 scsi_ulto4b(0, res->header.length);
7656 * We had to drop the lock to allocate our buffer, which
7657 * leaves time for someone to come in with another
7658 * persistent reservation. (That is unlikely, though,
7659 * since this should be the only persistent reservation
7660 * command active right now.)
7662 if (tmp_len != total_len) {
7663 mtx_unlock(&lun->lun_lock);
7664 free(ctsio->kern_data_ptr, M_CTL);
7665 printf("%s: reservation status changed, retrying\n",
7671 * No reservation held, so we're done.
7673 if (header_only != 0)
7677 * If the registration is an All Registrants type, the key
7678 * is 0, since it doesn't really matter.
7680 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7681 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7682 res->data.reservation);
7684 res->data.scopetype = lun->pr_res_type;
7687 case SPRI_RC: //report capabilities
7689 struct scsi_per_res_cap *res_cap;
7692 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7693 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7694 res_cap->flags1 = SPRI_CRH;
7695 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5;
7696 type_mask = SPRI_TM_WR_EX_AR |
7702 scsi_ulto2b(type_mask, res_cap->type_mask);
7705 case SPRI_RS: { // read full status
7706 struct scsi_per_res_in_full *res_status;
7707 struct scsi_per_res_in_full_desc *res_desc;
7708 struct ctl_port *port;
7711 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7714 * We had to drop the lock to allocate our buffer, which
7715 * leaves time for someone to come in with another
7716 * persistent reservation. (That is unlikely, though,
7717 * since this should be the only persistent reservation
7718 * command active right now.)
7720 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7721 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7722 lun->pr_key_count)){
7723 mtx_unlock(&lun->lun_lock);
7724 free(ctsio->kern_data_ptr, M_CTL);
7725 printf("%s: reservation length changed, retrying\n",
7730 scsi_ulto4b(lun->pr_generation, res_status->header.generation);
7732 res_desc = &res_status->desc[0];
7733 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7734 if ((key = ctl_get_prkey(lun, i)) == 0)
7737 scsi_u64to8b(key, res_desc->res_key.key);
7738 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7739 (lun->pr_res_idx == i ||
7740 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7741 res_desc->flags = SPRI_FULL_R_HOLDER;
7742 res_desc->scopetype = lun->pr_res_type;
7744 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7745 res_desc->rel_trgt_port_id);
7747 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7749 len = ctl_create_iid(port,
7750 i % CTL_MAX_INIT_PER_PORT,
7751 res_desc->transport_id);
7752 scsi_ulto4b(len, res_desc->additional_length);
7753 res_desc = (struct scsi_per_res_in_full_desc *)
7754 &res_desc->transport_id[len];
7756 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7757 res_status->header.length);
7761 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7763 mtx_unlock(&lun->lun_lock);
7765 ctl_set_success(ctsio);
7766 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7767 ctsio->be_move_done = ctl_config_move_done;
7768 ctl_datamove((union ctl_io *)ctsio);
7769 return (CTL_RETVAL_COMPLETE);
7773 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7777 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7778 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7779 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7780 struct scsi_per_res_out_parms* param)
7782 union ctl_ha_msg persis_io;
7785 mtx_lock(&lun->lun_lock);
7786 if (sa_res_key == 0) {
7787 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7788 /* validate scope and type */
7789 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7791 mtx_unlock(&lun->lun_lock);
7792 ctl_set_invalid_field(/*ctsio*/ ctsio,
7798 ctl_done((union ctl_io *)ctsio);
7802 if (type>8 || type==2 || type==4 || type==0) {
7803 mtx_unlock(&lun->lun_lock);
7804 ctl_set_invalid_field(/*ctsio*/ ctsio,
7810 ctl_done((union ctl_io *)ctsio);
7815 * Unregister everybody else and build UA for
7818 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7819 if (i == residx || ctl_get_prkey(lun, i) == 0)
7822 ctl_clr_prkey(lun, i);
7823 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7825 lun->pr_key_count = 1;
7826 lun->pr_res_type = type;
7827 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7828 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7829 lun->pr_res_idx = residx;
7830 lun->pr_generation++;
7831 mtx_unlock(&lun->lun_lock);
7833 /* send msg to other side */
7834 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7835 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7836 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7837 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7838 persis_io.pr.pr_info.res_type = type;
7839 memcpy(persis_io.pr.pr_info.sa_res_key,
7840 param->serv_act_res_key,
7841 sizeof(param->serv_act_res_key));
7842 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7843 sizeof(persis_io.pr), M_WAITOK);
7845 /* not all registrants */
7846 mtx_unlock(&lun->lun_lock);
7847 free(ctsio->kern_data_ptr, M_CTL);
7848 ctl_set_invalid_field(ctsio,
7854 ctl_done((union ctl_io *)ctsio);
7857 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7858 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7861 if (res_key == sa_res_key) {
7864 * The spec implies this is not good but doesn't
7865 * say what to do. There are two choices either
7866 * generate a res conflict or check condition
7867 * with illegal field in parameter data. Since
7868 * that is what is done when the sa_res_key is
7869 * zero I'll take that approach since this has
7870 * to do with the sa_res_key.
7872 mtx_unlock(&lun->lun_lock);
7873 free(ctsio->kern_data_ptr, M_CTL);
7874 ctl_set_invalid_field(ctsio,
7880 ctl_done((union ctl_io *)ctsio);
7884 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7885 if (ctl_get_prkey(lun, i) != sa_res_key)
7889 ctl_clr_prkey(lun, i);
7890 lun->pr_key_count--;
7891 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7894 mtx_unlock(&lun->lun_lock);
7895 free(ctsio->kern_data_ptr, M_CTL);
7896 ctl_set_reservation_conflict(ctsio);
7897 ctl_done((union ctl_io *)ctsio);
7898 return (CTL_RETVAL_COMPLETE);
7900 lun->pr_generation++;
7901 mtx_unlock(&lun->lun_lock);
7903 /* send msg to other side */
7904 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7905 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7906 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7907 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7908 persis_io.pr.pr_info.res_type = type;
7909 memcpy(persis_io.pr.pr_info.sa_res_key,
7910 param->serv_act_res_key,
7911 sizeof(param->serv_act_res_key));
7912 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7913 sizeof(persis_io.pr), M_WAITOK);
7915 /* Reserved but not all registrants */
7916 /* sa_res_key is res holder */
7917 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
7918 /* validate scope and type */
7919 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7921 mtx_unlock(&lun->lun_lock);
7922 ctl_set_invalid_field(/*ctsio*/ ctsio,
7928 ctl_done((union ctl_io *)ctsio);
7932 if (type>8 || type==2 || type==4 || type==0) {
7933 mtx_unlock(&lun->lun_lock);
7934 ctl_set_invalid_field(/*ctsio*/ ctsio,
7940 ctl_done((union ctl_io *)ctsio);
7946 * if sa_res_key != res_key remove all
7947 * registrants w/sa_res_key and generate UA
7948 * for these registrants(Registrations
7949 * Preempted) if it wasn't an exclusive
7950 * reservation generate UA(Reservations
7951 * Preempted) for all other registered nexuses
7952 * if the type has changed. Establish the new
7953 * reservation and holder. If res_key and
7954 * sa_res_key are the same do the above
7955 * except don't unregister the res holder.
7958 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7959 if (i == residx || ctl_get_prkey(lun, i) == 0)
7962 if (sa_res_key == ctl_get_prkey(lun, i)) {
7963 ctl_clr_prkey(lun, i);
7964 lun->pr_key_count--;
7965 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7966 } else if (type != lun->pr_res_type &&
7967 (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
7968 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
7969 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
7972 lun->pr_res_type = type;
7973 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7974 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7975 lun->pr_res_idx = residx;
7977 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
7978 lun->pr_generation++;
7979 mtx_unlock(&lun->lun_lock);
7981 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7982 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7983 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7984 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7985 persis_io.pr.pr_info.res_type = type;
7986 memcpy(persis_io.pr.pr_info.sa_res_key,
7987 param->serv_act_res_key,
7988 sizeof(param->serv_act_res_key));
7989 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7990 sizeof(persis_io.pr), M_WAITOK);
7993 * sa_res_key is not the res holder just
7994 * remove registrants
7998 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7999 if (sa_res_key != ctl_get_prkey(lun, i))
8003 ctl_clr_prkey(lun, i);
8004 lun->pr_key_count--;
8005 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8009 mtx_unlock(&lun->lun_lock);
8010 free(ctsio->kern_data_ptr, M_CTL);
8011 ctl_set_reservation_conflict(ctsio);
8012 ctl_done((union ctl_io *)ctsio);
8015 lun->pr_generation++;
8016 mtx_unlock(&lun->lun_lock);
8018 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8019 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8020 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8021 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8022 persis_io.pr.pr_info.res_type = type;
8023 memcpy(persis_io.pr.pr_info.sa_res_key,
8024 param->serv_act_res_key,
8025 sizeof(param->serv_act_res_key));
8026 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8027 sizeof(persis_io.pr), M_WAITOK);
8034 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8036 uint64_t sa_res_key;
8039 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8041 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8042 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8043 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
8044 if (sa_res_key == 0) {
8046 * Unregister everybody else and build UA for
8049 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8050 if (i == msg->pr.pr_info.residx ||
8051 ctl_get_prkey(lun, i) == 0)
8054 ctl_clr_prkey(lun, i);
8055 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8058 lun->pr_key_count = 1;
8059 lun->pr_res_type = msg->pr.pr_info.res_type;
8060 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8061 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8062 lun->pr_res_idx = msg->pr.pr_info.residx;
8064 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8065 if (sa_res_key == ctl_get_prkey(lun, i))
8068 ctl_clr_prkey(lun, i);
8069 lun->pr_key_count--;
8070 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8074 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8075 if (i == msg->pr.pr_info.residx ||
8076 ctl_get_prkey(lun, i) == 0)
8079 if (sa_res_key == ctl_get_prkey(lun, i)) {
8080 ctl_clr_prkey(lun, i);
8081 lun->pr_key_count--;
8082 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8083 } else if (msg->pr.pr_info.res_type != lun->pr_res_type
8084 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8085 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8086 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8089 lun->pr_res_type = msg->pr.pr_info.res_type;
8090 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8091 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8092 lun->pr_res_idx = msg->pr.pr_info.residx;
8094 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8096 lun->pr_generation++;
8102 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8104 struct ctl_softc *softc = CTL_SOFTC(ctsio);
8105 struct ctl_lun *lun = CTL_LUN(ctsio);
8107 u_int32_t param_len;
8108 struct scsi_per_res_out *cdb;
8109 struct scsi_per_res_out_parms* param;
8111 uint64_t res_key, sa_res_key, key;
8113 union ctl_ha_msg persis_io;
8116 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8118 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8119 retval = CTL_RETVAL_COMPLETE;
8122 * We only support whole-LUN scope. The scope & type are ignored for
8123 * register, register and ignore existing key and clear.
8124 * We sometimes ignore scope and type on preempts too!!
8125 * Verify reservation type here as well.
8127 type = cdb->scope_type & SPR_TYPE_MASK;
8128 if ((cdb->action == SPRO_RESERVE)
8129 || (cdb->action == SPRO_RELEASE)) {
8130 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8131 ctl_set_invalid_field(/*ctsio*/ ctsio,
8137 ctl_done((union ctl_io *)ctsio);
8138 return (CTL_RETVAL_COMPLETE);
8141 if (type>8 || type==2 || type==4 || type==0) {
8142 ctl_set_invalid_field(/*ctsio*/ ctsio,
8148 ctl_done((union ctl_io *)ctsio);
8149 return (CTL_RETVAL_COMPLETE);
8153 param_len = scsi_4btoul(cdb->length);
8155 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8156 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8157 ctsio->kern_data_len = param_len;
8158 ctsio->kern_total_len = param_len;
8159 ctsio->kern_data_resid = 0;
8160 ctsio->kern_rel_offset = 0;
8161 ctsio->kern_sg_entries = 0;
8162 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8163 ctsio->be_move_done = ctl_config_move_done;
8164 ctl_datamove((union ctl_io *)ctsio);
8166 return (CTL_RETVAL_COMPLETE);
8169 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8171 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8172 res_key = scsi_8btou64(param->res_key.key);
8173 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8176 * Validate the reservation key here except for SPRO_REG_IGNO
8177 * This must be done for all other service actions
8179 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8180 mtx_lock(&lun->lun_lock);
8181 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8182 if (res_key != key) {
8184 * The current key passed in doesn't match
8185 * the one the initiator previously
8188 mtx_unlock(&lun->lun_lock);
8189 free(ctsio->kern_data_ptr, M_CTL);
8190 ctl_set_reservation_conflict(ctsio);
8191 ctl_done((union ctl_io *)ctsio);
8192 return (CTL_RETVAL_COMPLETE);
8194 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8196 * We are not registered
8198 mtx_unlock(&lun->lun_lock);
8199 free(ctsio->kern_data_ptr, M_CTL);
8200 ctl_set_reservation_conflict(ctsio);
8201 ctl_done((union ctl_io *)ctsio);
8202 return (CTL_RETVAL_COMPLETE);
8203 } else if (res_key != 0) {
8205 * We are not registered and trying to register but
8206 * the register key isn't zero.
8208 mtx_unlock(&lun->lun_lock);
8209 free(ctsio->kern_data_ptr, M_CTL);
8210 ctl_set_reservation_conflict(ctsio);
8211 ctl_done((union ctl_io *)ctsio);
8212 return (CTL_RETVAL_COMPLETE);
8214 mtx_unlock(&lun->lun_lock);
8217 switch (cdb->action & SPRO_ACTION_MASK) {
8219 case SPRO_REG_IGNO: {
8222 printf("Registration received\n");
8226 * We don't support any of these options, as we report in
8227 * the read capabilities request (see
8228 * ctl_persistent_reserve_in(), above).
8230 if ((param->flags & SPR_SPEC_I_PT)
8231 || (param->flags & SPR_ALL_TG_PT)
8232 || (param->flags & SPR_APTPL)) {
8235 if (param->flags & SPR_APTPL)
8237 else if (param->flags & SPR_ALL_TG_PT)
8239 else /* SPR_SPEC_I_PT */
8242 free(ctsio->kern_data_ptr, M_CTL);
8243 ctl_set_invalid_field(ctsio,
8249 ctl_done((union ctl_io *)ctsio);
8250 return (CTL_RETVAL_COMPLETE);
8253 mtx_lock(&lun->lun_lock);
8256 * The initiator wants to clear the
8259 if (sa_res_key == 0) {
8261 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8262 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8263 && ctl_get_prkey(lun, residx) == 0)) {
8264 mtx_unlock(&lun->lun_lock);
8268 ctl_clr_prkey(lun, residx);
8269 lun->pr_key_count--;
8271 if (residx == lun->pr_res_idx) {
8272 lun->flags &= ~CTL_LUN_PR_RESERVED;
8273 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8275 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8276 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8277 lun->pr_key_count) {
8279 * If the reservation is a registrants
8280 * only type we need to generate a UA
8281 * for other registered inits. The
8282 * sense code should be RESERVATIONS
8286 for (i = softc->init_min; i < softc->init_max; i++){
8287 if (ctl_get_prkey(lun, i) == 0)
8290 CTL_UA_RES_RELEASE);
8293 lun->pr_res_type = 0;
8294 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8295 if (lun->pr_key_count==0) {
8296 lun->flags &= ~CTL_LUN_PR_RESERVED;
8297 lun->pr_res_type = 0;
8298 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8301 lun->pr_generation++;
8302 mtx_unlock(&lun->lun_lock);
8304 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8305 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8306 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8307 persis_io.pr.pr_info.residx = residx;
8308 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8309 sizeof(persis_io.pr), M_WAITOK);
8310 } else /* sa_res_key != 0 */ {
8313 * If we aren't registered currently then increment
8314 * the key count and set the registered flag.
8316 ctl_alloc_prkey(lun, residx);
8317 if (ctl_get_prkey(lun, residx) == 0)
8318 lun->pr_key_count++;
8319 ctl_set_prkey(lun, residx, sa_res_key);
8320 lun->pr_generation++;
8321 mtx_unlock(&lun->lun_lock);
8323 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8324 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8325 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8326 persis_io.pr.pr_info.residx = residx;
8327 memcpy(persis_io.pr.pr_info.sa_res_key,
8328 param->serv_act_res_key,
8329 sizeof(param->serv_act_res_key));
8330 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8331 sizeof(persis_io.pr), M_WAITOK);
8338 printf("Reserve executed type %d\n", type);
8340 mtx_lock(&lun->lun_lock);
8341 if (lun->flags & CTL_LUN_PR_RESERVED) {
8343 * if this isn't the reservation holder and it's
8344 * not a "all registrants" type or if the type is
8345 * different then we have a conflict
8347 if ((lun->pr_res_idx != residx
8348 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8349 || lun->pr_res_type != type) {
8350 mtx_unlock(&lun->lun_lock);
8351 free(ctsio->kern_data_ptr, M_CTL);
8352 ctl_set_reservation_conflict(ctsio);
8353 ctl_done((union ctl_io *)ctsio);
8354 return (CTL_RETVAL_COMPLETE);
8356 mtx_unlock(&lun->lun_lock);
8357 } else /* create a reservation */ {
8359 * If it's not an "all registrants" type record
8360 * reservation holder
8362 if (type != SPR_TYPE_WR_EX_AR
8363 && type != SPR_TYPE_EX_AC_AR)
8364 lun->pr_res_idx = residx; /* Res holder */
8366 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8368 lun->flags |= CTL_LUN_PR_RESERVED;
8369 lun->pr_res_type = type;
8371 mtx_unlock(&lun->lun_lock);
8373 /* send msg to other side */
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_RESERVE;
8377 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8378 persis_io.pr.pr_info.res_type = type;
8379 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8380 sizeof(persis_io.pr), M_WAITOK);
8385 mtx_lock(&lun->lun_lock);
8386 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8387 /* No reservation exists return good status */
8388 mtx_unlock(&lun->lun_lock);
8392 * Is this nexus a reservation holder?
8394 if (lun->pr_res_idx != residx
8395 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8397 * not a res holder return good status but
8400 mtx_unlock(&lun->lun_lock);
8404 if (lun->pr_res_type != type) {
8405 mtx_unlock(&lun->lun_lock);
8406 free(ctsio->kern_data_ptr, M_CTL);
8407 ctl_set_illegal_pr_release(ctsio);
8408 ctl_done((union ctl_io *)ctsio);
8409 return (CTL_RETVAL_COMPLETE);
8412 /* okay to release */
8413 lun->flags &= ~CTL_LUN_PR_RESERVED;
8414 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8415 lun->pr_res_type = 0;
8418 * If this isn't an exclusive access reservation and NUAR
8419 * is not set, generate UA for all other registrants.
8421 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX &&
8422 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8423 for (i = softc->init_min; i < softc->init_max; i++) {
8424 if (i == residx || ctl_get_prkey(lun, i) == 0)
8426 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8429 mtx_unlock(&lun->lun_lock);
8431 /* Send msg to other side */
8432 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8433 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8434 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8435 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8436 sizeof(persis_io.pr), M_WAITOK);
8440 /* send msg to other side */
8442 mtx_lock(&lun->lun_lock);
8443 lun->flags &= ~CTL_LUN_PR_RESERVED;
8444 lun->pr_res_type = 0;
8445 lun->pr_key_count = 0;
8446 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8448 ctl_clr_prkey(lun, residx);
8449 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8450 if (ctl_get_prkey(lun, i) != 0) {
8451 ctl_clr_prkey(lun, i);
8452 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8454 lun->pr_generation++;
8455 mtx_unlock(&lun->lun_lock);
8457 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8458 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8459 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8460 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8461 sizeof(persis_io.pr), M_WAITOK);
8465 case SPRO_PRE_ABO: {
8468 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8469 residx, ctsio, cdb, param);
8471 return (CTL_RETVAL_COMPLETE);
8475 panic("%s: Invalid PR type %#x", __func__, cdb->action);
8479 free(ctsio->kern_data_ptr, M_CTL);
8480 ctl_set_success(ctsio);
8481 ctl_done((union ctl_io *)ctsio);
8487 * This routine is for handling a message from the other SC pertaining to
8488 * persistent reserve out. All the error checking will have been done
8489 * so only perorming the action need be done here to keep the two
8493 ctl_hndl_per_res_out_on_other_sc(union ctl_io *io)
8495 struct ctl_softc *softc = CTL_SOFTC(io);
8496 union ctl_ha_msg *msg = (union ctl_ha_msg *)&io->presio.pr_msg;
8497 struct ctl_lun *lun;
8499 uint32_t residx, targ_lun;
8501 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8502 mtx_lock(&softc->ctl_lock);
8503 if (targ_lun >= CTL_MAX_LUNS ||
8504 (lun = softc->ctl_luns[targ_lun]) == NULL) {
8505 mtx_unlock(&softc->ctl_lock);
8508 mtx_lock(&lun->lun_lock);
8509 mtx_unlock(&softc->ctl_lock);
8510 if (lun->flags & CTL_LUN_DISABLED) {
8511 mtx_unlock(&lun->lun_lock);
8514 residx = ctl_get_initindex(&msg->hdr.nexus);
8515 switch(msg->pr.pr_info.action) {
8516 case CTL_PR_REG_KEY:
8517 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8518 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8519 lun->pr_key_count++;
8520 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8521 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8522 lun->pr_generation++;
8525 case CTL_PR_UNREG_KEY:
8526 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8527 lun->pr_key_count--;
8529 /* XXX Need to see if the reservation has been released */
8530 /* if so do we need to generate UA? */
8531 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8532 lun->flags &= ~CTL_LUN_PR_RESERVED;
8533 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8535 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8536 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8537 lun->pr_key_count) {
8539 * If the reservation is a registrants
8540 * only type we need to generate a UA
8541 * for other registered inits. The
8542 * sense code should be RESERVATIONS
8546 for (i = softc->init_min; i < softc->init_max; i++) {
8547 if (ctl_get_prkey(lun, i) == 0)
8550 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8553 lun->pr_res_type = 0;
8554 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8555 if (lun->pr_key_count==0) {
8556 lun->flags &= ~CTL_LUN_PR_RESERVED;
8557 lun->pr_res_type = 0;
8558 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8561 lun->pr_generation++;
8564 case CTL_PR_RESERVE:
8565 lun->flags |= CTL_LUN_PR_RESERVED;
8566 lun->pr_res_type = msg->pr.pr_info.res_type;
8567 lun->pr_res_idx = msg->pr.pr_info.residx;
8571 case CTL_PR_RELEASE:
8573 * If this isn't an exclusive access reservation and NUAR
8574 * is not set, generate UA for all other registrants.
8576 if (lun->pr_res_type != SPR_TYPE_EX_AC &&
8577 lun->pr_res_type != SPR_TYPE_WR_EX &&
8578 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8579 for (i = softc->init_min; i < softc->init_max; i++)
8580 if (i == residx || ctl_get_prkey(lun, i) == 0)
8582 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8585 lun->flags &= ~CTL_LUN_PR_RESERVED;
8586 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8587 lun->pr_res_type = 0;
8590 case CTL_PR_PREEMPT:
8591 ctl_pro_preempt_other(lun, msg);
8594 lun->flags &= ~CTL_LUN_PR_RESERVED;
8595 lun->pr_res_type = 0;
8596 lun->pr_key_count = 0;
8597 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8599 for (i=0; i < CTL_MAX_INITIATORS; i++) {
8600 if (ctl_get_prkey(lun, i) == 0)
8602 ctl_clr_prkey(lun, i);
8603 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8605 lun->pr_generation++;
8609 mtx_unlock(&lun->lun_lock);
8613 ctl_read_write(struct ctl_scsiio *ctsio)
8615 struct ctl_lun *lun = CTL_LUN(ctsio);
8616 struct ctl_lba_len_flags *lbalen;
8618 uint32_t num_blocks;
8622 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8625 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8626 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8627 switch (ctsio->cdb[0]) {
8630 struct scsi_rw_6 *cdb;
8632 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8634 lba = scsi_3btoul(cdb->addr);
8635 /* only 5 bits are valid in the most significant address byte */
8637 num_blocks = cdb->length;
8639 * This is correct according to SBC-2.
8641 if (num_blocks == 0)
8647 struct scsi_rw_10 *cdb;
8649 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8650 if (cdb->byte2 & SRW10_FUA)
8651 flags |= CTL_LLF_FUA;
8652 if (cdb->byte2 & SRW10_DPO)
8653 flags |= CTL_LLF_DPO;
8654 lba = scsi_4btoul(cdb->addr);
8655 num_blocks = scsi_2btoul(cdb->length);
8658 case WRITE_VERIFY_10: {
8659 struct scsi_write_verify_10 *cdb;
8661 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8662 flags |= CTL_LLF_FUA;
8663 if (cdb->byte2 & SWV_DPO)
8664 flags |= CTL_LLF_DPO;
8665 lba = scsi_4btoul(cdb->addr);
8666 num_blocks = scsi_2btoul(cdb->length);
8671 struct scsi_rw_12 *cdb;
8673 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8674 if (cdb->byte2 & SRW12_FUA)
8675 flags |= CTL_LLF_FUA;
8676 if (cdb->byte2 & SRW12_DPO)
8677 flags |= CTL_LLF_DPO;
8678 lba = scsi_4btoul(cdb->addr);
8679 num_blocks = scsi_4btoul(cdb->length);
8682 case WRITE_VERIFY_12: {
8683 struct scsi_write_verify_12 *cdb;
8685 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8686 flags |= CTL_LLF_FUA;
8687 if (cdb->byte2 & SWV_DPO)
8688 flags |= CTL_LLF_DPO;
8689 lba = scsi_4btoul(cdb->addr);
8690 num_blocks = scsi_4btoul(cdb->length);
8695 struct scsi_rw_16 *cdb;
8697 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8698 if (cdb->byte2 & SRW12_FUA)
8699 flags |= CTL_LLF_FUA;
8700 if (cdb->byte2 & SRW12_DPO)
8701 flags |= CTL_LLF_DPO;
8702 lba = scsi_8btou64(cdb->addr);
8703 num_blocks = scsi_4btoul(cdb->length);
8706 case WRITE_ATOMIC_16: {
8707 struct scsi_write_atomic_16 *cdb;
8709 if (lun->be_lun->atomicblock == 0) {
8710 ctl_set_invalid_opcode(ctsio);
8711 ctl_done((union ctl_io *)ctsio);
8712 return (CTL_RETVAL_COMPLETE);
8715 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb;
8716 if (cdb->byte2 & SRW12_FUA)
8717 flags |= CTL_LLF_FUA;
8718 if (cdb->byte2 & SRW12_DPO)
8719 flags |= CTL_LLF_DPO;
8720 lba = scsi_8btou64(cdb->addr);
8721 num_blocks = scsi_2btoul(cdb->length);
8722 if (num_blocks > lun->be_lun->atomicblock) {
8723 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8724 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8726 ctl_done((union ctl_io *)ctsio);
8727 return (CTL_RETVAL_COMPLETE);
8731 case WRITE_VERIFY_16: {
8732 struct scsi_write_verify_16 *cdb;
8734 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8735 flags |= CTL_LLF_FUA;
8736 if (cdb->byte2 & SWV_DPO)
8737 flags |= CTL_LLF_DPO;
8738 lba = scsi_8btou64(cdb->addr);
8739 num_blocks = scsi_4btoul(cdb->length);
8744 * We got a command we don't support. This shouldn't
8745 * happen, commands should be filtered out above us.
8747 ctl_set_invalid_opcode(ctsio);
8748 ctl_done((union ctl_io *)ctsio);
8750 return (CTL_RETVAL_COMPLETE);
8751 break; /* NOTREACHED */
8755 * The first check is to make sure we're in bounds, the second
8756 * check is to catch wrap-around problems. If the lba + num blocks
8757 * is less than the lba, then we've wrapped around and the block
8758 * range is invalid anyway.
8760 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8761 || ((lba + num_blocks) < lba)) {
8762 ctl_set_lba_out_of_range(ctsio,
8763 MAX(lba, lun->be_lun->maxlba + 1));
8764 ctl_done((union ctl_io *)ctsio);
8765 return (CTL_RETVAL_COMPLETE);
8769 * According to SBC-3, a transfer length of 0 is not an error.
8770 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8771 * translates to 256 blocks for those commands.
8773 if (num_blocks == 0) {
8774 ctl_set_success(ctsio);
8775 ctl_done((union ctl_io *)ctsio);
8776 return (CTL_RETVAL_COMPLETE);
8779 /* Set FUA and/or DPO if caches are disabled. */
8781 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0)
8782 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8784 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8785 flags |= CTL_LLF_FUA;
8788 lbalen = (struct ctl_lba_len_flags *)
8789 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8791 lbalen->len = num_blocks;
8792 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8794 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8795 ctsio->kern_rel_offset = 0;
8797 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8799 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8804 ctl_cnw_cont(union ctl_io *io)
8806 struct ctl_lun *lun = CTL_LUN(io);
8807 struct ctl_scsiio *ctsio;
8808 struct ctl_lba_len_flags *lbalen;
8811 ctsio = &io->scsiio;
8812 ctsio->io_hdr.status = CTL_STATUS_NONE;
8813 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8814 lbalen = (struct ctl_lba_len_flags *)
8815 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8816 lbalen->flags &= ~CTL_LLF_COMPARE;
8817 lbalen->flags |= CTL_LLF_WRITE;
8819 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8820 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8825 ctl_cnw(struct ctl_scsiio *ctsio)
8827 struct ctl_lun *lun = CTL_LUN(ctsio);
8828 struct ctl_lba_len_flags *lbalen;
8830 uint32_t num_blocks;
8833 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8836 switch (ctsio->cdb[0]) {
8837 case COMPARE_AND_WRITE: {
8838 struct scsi_compare_and_write *cdb;
8840 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8841 if (cdb->byte2 & SRW10_FUA)
8842 flags |= CTL_LLF_FUA;
8843 if (cdb->byte2 & SRW10_DPO)
8844 flags |= CTL_LLF_DPO;
8845 lba = scsi_8btou64(cdb->addr);
8846 num_blocks = cdb->length;
8851 * We got a command we don't support. This shouldn't
8852 * happen, commands should be filtered out above us.
8854 ctl_set_invalid_opcode(ctsio);
8855 ctl_done((union ctl_io *)ctsio);
8857 return (CTL_RETVAL_COMPLETE);
8858 break; /* NOTREACHED */
8862 * The first check is to make sure we're in bounds, the second
8863 * check is to catch wrap-around problems. If the lba + num blocks
8864 * is less than the lba, then we've wrapped around and the block
8865 * range is invalid anyway.
8867 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8868 || ((lba + num_blocks) < lba)) {
8869 ctl_set_lba_out_of_range(ctsio,
8870 MAX(lba, lun->be_lun->maxlba + 1));
8871 ctl_done((union ctl_io *)ctsio);
8872 return (CTL_RETVAL_COMPLETE);
8876 * According to SBC-3, a transfer length of 0 is not an error.
8878 if (num_blocks == 0) {
8879 ctl_set_success(ctsio);
8880 ctl_done((union ctl_io *)ctsio);
8881 return (CTL_RETVAL_COMPLETE);
8884 /* Set FUA if write cache is disabled. */
8885 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8886 flags |= CTL_LLF_FUA;
8888 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
8889 ctsio->kern_rel_offset = 0;
8892 * Set the IO_CONT flag, so that if this I/O gets passed to
8893 * ctl_data_submit_done(), it'll get passed back to
8894 * ctl_ctl_cnw_cont() for further processing.
8896 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
8897 ctsio->io_cont = ctl_cnw_cont;
8899 lbalen = (struct ctl_lba_len_flags *)
8900 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8902 lbalen->len = num_blocks;
8903 lbalen->flags = CTL_LLF_COMPARE | flags;
8905 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
8906 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8911 ctl_verify(struct ctl_scsiio *ctsio)
8913 struct ctl_lun *lun = CTL_LUN(ctsio);
8914 struct ctl_lba_len_flags *lbalen;
8916 uint32_t num_blocks;
8920 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
8923 flags = CTL_LLF_FUA;
8924 switch (ctsio->cdb[0]) {
8926 struct scsi_verify_10 *cdb;
8928 cdb = (struct scsi_verify_10 *)ctsio->cdb;
8929 if (cdb->byte2 & SVFY_BYTCHK)
8931 if (cdb->byte2 & SVFY_DPO)
8932 flags |= CTL_LLF_DPO;
8933 lba = scsi_4btoul(cdb->addr);
8934 num_blocks = scsi_2btoul(cdb->length);
8938 struct scsi_verify_12 *cdb;
8940 cdb = (struct scsi_verify_12 *)ctsio->cdb;
8941 if (cdb->byte2 & SVFY_BYTCHK)
8943 if (cdb->byte2 & SVFY_DPO)
8944 flags |= CTL_LLF_DPO;
8945 lba = scsi_4btoul(cdb->addr);
8946 num_blocks = scsi_4btoul(cdb->length);
8950 struct scsi_rw_16 *cdb;
8952 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8953 if (cdb->byte2 & SVFY_BYTCHK)
8955 if (cdb->byte2 & SVFY_DPO)
8956 flags |= CTL_LLF_DPO;
8957 lba = scsi_8btou64(cdb->addr);
8958 num_blocks = scsi_4btoul(cdb->length);
8963 * We got a command we don't support. This shouldn't
8964 * happen, commands should be filtered out above us.
8966 ctl_set_invalid_opcode(ctsio);
8967 ctl_done((union ctl_io *)ctsio);
8968 return (CTL_RETVAL_COMPLETE);
8972 * The first check is to make sure we're in bounds, the second
8973 * check is to catch wrap-around problems. If the lba + num blocks
8974 * is less than the lba, then we've wrapped around and the block
8975 * range is invalid anyway.
8977 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8978 || ((lba + num_blocks) < lba)) {
8979 ctl_set_lba_out_of_range(ctsio,
8980 MAX(lba, lun->be_lun->maxlba + 1));
8981 ctl_done((union ctl_io *)ctsio);
8982 return (CTL_RETVAL_COMPLETE);
8986 * According to SBC-3, a transfer length of 0 is not an error.
8988 if (num_blocks == 0) {
8989 ctl_set_success(ctsio);
8990 ctl_done((union ctl_io *)ctsio);
8991 return (CTL_RETVAL_COMPLETE);
8994 lbalen = (struct ctl_lba_len_flags *)
8995 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8997 lbalen->len = num_blocks;
8999 lbalen->flags = CTL_LLF_COMPARE | flags;
9000 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9002 lbalen->flags = CTL_LLF_VERIFY | flags;
9003 ctsio->kern_total_len = 0;
9005 ctsio->kern_rel_offset = 0;
9007 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9008 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9013 ctl_report_luns(struct ctl_scsiio *ctsio)
9015 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9016 struct ctl_port *port = CTL_PORT(ctsio);
9017 struct ctl_lun *lun, *request_lun = CTL_LUN(ctsio);
9018 struct scsi_report_luns *cdb;
9019 struct scsi_report_luns_data *lun_data;
9020 int num_filled, num_luns, num_port_luns, retval;
9021 uint32_t alloc_len, lun_datalen;
9022 uint32_t initidx, targ_lun_id, lun_id;
9024 retval = CTL_RETVAL_COMPLETE;
9025 cdb = (struct scsi_report_luns *)ctsio->cdb;
9027 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9030 num_port_luns = port->lun_map ? port->lun_map_size : CTL_MAX_LUNS;
9031 mtx_lock(&softc->ctl_lock);
9032 for (targ_lun_id = 0; targ_lun_id < num_port_luns; targ_lun_id++) {
9033 if (ctl_lun_map_from_port(port, targ_lun_id) != UINT32_MAX)
9036 mtx_unlock(&softc->ctl_lock);
9038 switch (cdb->select_report) {
9039 case RPL_REPORT_DEFAULT:
9040 case RPL_REPORT_ALL:
9041 case RPL_REPORT_NONSUBSID:
9043 case RPL_REPORT_WELLKNOWN:
9044 case RPL_REPORT_ADMIN:
9045 case RPL_REPORT_CONGLOM:
9049 ctl_set_invalid_field(ctsio,
9055 ctl_done((union ctl_io *)ctsio);
9057 break; /* NOTREACHED */
9060 alloc_len = scsi_4btoul(cdb->length);
9062 * The initiator has to allocate at least 16 bytes for this request,
9063 * so he can at least get the header and the first LUN. Otherwise
9064 * we reject the request (per SPC-3 rev 14, section 6.21).
9066 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9067 sizeof(struct scsi_report_luns_lundata))) {
9068 ctl_set_invalid_field(ctsio,
9074 ctl_done((union ctl_io *)ctsio);
9078 lun_datalen = sizeof(*lun_data) +
9079 (num_luns * sizeof(struct scsi_report_luns_lundata));
9081 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9082 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9083 ctsio->kern_sg_entries = 0;
9085 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9087 mtx_lock(&softc->ctl_lock);
9088 for (targ_lun_id = 0, num_filled = 0;
9089 targ_lun_id < num_port_luns && num_filled < num_luns;
9091 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9092 if (lun_id == UINT32_MAX)
9094 lun = softc->ctl_luns[lun_id];
9098 be64enc(lun_data->luns[num_filled++].lundata,
9099 ctl_encode_lun(targ_lun_id));
9102 * According to SPC-3, rev 14 section 6.21:
9104 * "The execution of a REPORT LUNS command to any valid and
9105 * installed logical unit shall clear the REPORTED LUNS DATA
9106 * HAS CHANGED unit attention condition for all logical
9107 * units of that target with respect to the requesting
9108 * initiator. A valid and installed logical unit is one
9109 * having a PERIPHERAL QUALIFIER of 000b in the standard
9110 * INQUIRY data (see 6.4.2)."
9112 * If request_lun is NULL, the LUN this report luns command
9113 * was issued to is either disabled or doesn't exist. In that
9114 * case, we shouldn't clear any pending lun change unit
9117 if (request_lun != NULL) {
9118 mtx_lock(&lun->lun_lock);
9119 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE);
9120 mtx_unlock(&lun->lun_lock);
9123 mtx_unlock(&softc->ctl_lock);
9126 * It's quite possible that we've returned fewer LUNs than we allocated
9127 * space for. Trim it.
9129 lun_datalen = sizeof(*lun_data) +
9130 (num_filled * sizeof(struct scsi_report_luns_lundata));
9132 if (lun_datalen < alloc_len) {
9133 ctsio->residual = alloc_len - lun_datalen;
9134 ctsio->kern_data_len = lun_datalen;
9135 ctsio->kern_total_len = lun_datalen;
9137 ctsio->residual = 0;
9138 ctsio->kern_data_len = alloc_len;
9139 ctsio->kern_total_len = alloc_len;
9141 ctsio->kern_data_resid = 0;
9142 ctsio->kern_rel_offset = 0;
9143 ctsio->kern_sg_entries = 0;
9146 * We set this to the actual data length, regardless of how much
9147 * space we actually have to return results. If the user looks at
9148 * this value, he'll know whether or not he allocated enough space
9149 * and reissue the command if necessary. We don't support well
9150 * known logical units, so if the user asks for that, return none.
9152 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9155 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9158 ctl_set_success(ctsio);
9159 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9160 ctsio->be_move_done = ctl_config_move_done;
9161 ctl_datamove((union ctl_io *)ctsio);
9166 ctl_request_sense(struct ctl_scsiio *ctsio)
9168 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9169 struct ctl_lun *lun = CTL_LUN(ctsio);
9170 struct scsi_request_sense *cdb;
9171 struct scsi_sense_data *sense_ptr;
9174 u_int sense_len = SSD_FULL_SIZE;
9175 scsi_sense_data_type sense_format;
9176 ctl_ua_type ua_type;
9177 uint8_t asc = 0, ascq = 0;
9179 cdb = (struct scsi_request_sense *)ctsio->cdb;
9181 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9184 * Determine which sense format the user wants.
9186 if (cdb->byte2 & SRS_DESC)
9187 sense_format = SSD_TYPE_DESC;
9189 sense_format = SSD_TYPE_FIXED;
9191 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9192 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9193 ctsio->kern_sg_entries = 0;
9196 * struct scsi_sense_data, which is currently set to 256 bytes, is
9197 * larger than the largest allowed value for the length field in the
9198 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9200 ctsio->residual = 0;
9201 ctsio->kern_data_len = cdb->length;
9202 ctsio->kern_total_len = cdb->length;
9204 ctsio->kern_data_resid = 0;
9205 ctsio->kern_rel_offset = 0;
9206 ctsio->kern_sg_entries = 0;
9209 * If we don't have a LUN, we don't have any pending sense.
9212 ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
9213 softc->ha_link < CTL_HA_LINK_UNKNOWN)) {
9214 /* "Logical unit not supported" */
9215 ctl_set_sense_data(sense_ptr, &sense_len, NULL, sense_format,
9216 /*current_error*/ 1,
9217 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
9225 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9227 * Check for pending sense, and then for pending unit attentions.
9228 * Pending sense gets returned first, then pending unit attentions.
9230 mtx_lock(&lun->lun_lock);
9232 if (ctl_is_set(lun->have_ca, initidx)) {
9233 scsi_sense_data_type stored_format;
9236 * Check to see which sense format was used for the stored
9239 stored_format = scsi_sense_type(&lun->pending_sense[initidx]);
9242 * If the user requested a different sense format than the
9243 * one we stored, then we need to convert it to the other
9244 * format. If we're going from descriptor to fixed format
9245 * sense data, we may lose things in translation, depending
9246 * on what options were used.
9248 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9249 * for some reason we'll just copy it out as-is.
9251 if ((stored_format == SSD_TYPE_FIXED)
9252 && (sense_format == SSD_TYPE_DESC))
9253 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9254 &lun->pending_sense[initidx],
9255 (struct scsi_sense_data_desc *)sense_ptr);
9256 else if ((stored_format == SSD_TYPE_DESC)
9257 && (sense_format == SSD_TYPE_FIXED))
9258 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9259 &lun->pending_sense[initidx],
9260 (struct scsi_sense_data_fixed *)sense_ptr);
9262 memcpy(sense_ptr, &lun->pending_sense[initidx],
9263 MIN(sizeof(*sense_ptr),
9264 sizeof(lun->pending_sense[initidx])));
9266 ctl_clear_mask(lun->have_ca, initidx);
9270 if (have_error == 0) {
9271 ua_type = ctl_build_ua(lun, initidx, sense_ptr, &sense_len,
9273 if (ua_type != CTL_UA_NONE)
9276 if (have_error == 0) {
9278 * Report informational exception if have one and allowed.
9280 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) {
9282 ascq = lun->ie_ascq;
9284 ctl_set_sense_data(sense_ptr, &sense_len, lun, sense_format,
9285 /*current_error*/ 1,
9286 /*sense_key*/ SSD_KEY_NO_SENSE,
9291 mtx_unlock(&lun->lun_lock);
9295 * We report the SCSI status as OK, since the status of the command
9296 * itself is OK. We're reporting sense as parameter data.
9298 ctl_set_success(ctsio);
9299 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9300 ctsio->be_move_done = ctl_config_move_done;
9301 ctl_datamove((union ctl_io *)ctsio);
9302 return (CTL_RETVAL_COMPLETE);
9306 ctl_tur(struct ctl_scsiio *ctsio)
9309 CTL_DEBUG_PRINT(("ctl_tur\n"));
9311 ctl_set_success(ctsio);
9312 ctl_done((union ctl_io *)ctsio);
9314 return (CTL_RETVAL_COMPLETE);
9318 * SCSI VPD page 0x00, the Supported VPD Pages page.
9321 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9323 struct ctl_lun *lun = CTL_LUN(ctsio);
9324 struct scsi_vpd_supported_pages *pages;
9328 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9329 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9330 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9331 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9332 ctsio->kern_sg_entries = 0;
9334 if (sup_page_size < alloc_len) {
9335 ctsio->residual = alloc_len - sup_page_size;
9336 ctsio->kern_data_len = sup_page_size;
9337 ctsio->kern_total_len = sup_page_size;
9339 ctsio->residual = 0;
9340 ctsio->kern_data_len = alloc_len;
9341 ctsio->kern_total_len = alloc_len;
9343 ctsio->kern_data_resid = 0;
9344 ctsio->kern_rel_offset = 0;
9345 ctsio->kern_sg_entries = 0;
9348 * The control device is always connected. The disk device, on the
9349 * other hand, may not be online all the time. Need to change this
9350 * to figure out whether the disk device is actually online or not.
9353 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9354 lun->be_lun->lun_type;
9356 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9359 /* Supported VPD pages */
9360 pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9362 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9363 /* Device Identification */
9364 pages->page_list[p++] = SVPD_DEVICE_ID;
9365 /* Extended INQUIRY Data */
9366 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9367 /* Mode Page Policy */
9368 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9370 pages->page_list[p++] = SVPD_SCSI_PORTS;
9371 /* Third-party Copy */
9372 pages->page_list[p++] = SVPD_SCSI_TPC;
9373 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9375 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9376 /* Block Device Characteristics */
9377 pages->page_list[p++] = SVPD_BDC;
9378 /* Logical Block Provisioning */
9379 pages->page_list[p++] = SVPD_LBP;
9383 ctl_set_success(ctsio);
9384 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9385 ctsio->be_move_done = ctl_config_move_done;
9386 ctl_datamove((union ctl_io *)ctsio);
9387 return (CTL_RETVAL_COMPLETE);
9391 * SCSI VPD page 0x80, the Unit Serial Number page.
9394 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9396 struct ctl_lun *lun = CTL_LUN(ctsio);
9397 struct scsi_vpd_unit_serial_number *sn_ptr;
9400 data_len = 4 + CTL_SN_LEN;
9401 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9402 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9403 if (data_len < alloc_len) {
9404 ctsio->residual = alloc_len - data_len;
9405 ctsio->kern_data_len = data_len;
9406 ctsio->kern_total_len = data_len;
9408 ctsio->residual = 0;
9409 ctsio->kern_data_len = alloc_len;
9410 ctsio->kern_total_len = alloc_len;
9412 ctsio->kern_data_resid = 0;
9413 ctsio->kern_rel_offset = 0;
9414 ctsio->kern_sg_entries = 0;
9417 * The control device is always connected. The disk device, on the
9418 * other hand, may not be online all the time. Need to change this
9419 * to figure out whether the disk device is actually online or not.
9422 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9423 lun->be_lun->lun_type;
9425 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9427 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9428 sn_ptr->length = CTL_SN_LEN;
9430 * If we don't have a LUN, we just leave the serial number as
9434 strncpy((char *)sn_ptr->serial_num,
9435 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9437 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9439 ctl_set_success(ctsio);
9440 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9441 ctsio->be_move_done = ctl_config_move_done;
9442 ctl_datamove((union ctl_io *)ctsio);
9443 return (CTL_RETVAL_COMPLETE);
9448 * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9451 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9453 struct ctl_lun *lun = CTL_LUN(ctsio);
9454 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9457 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9458 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9459 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9460 ctsio->kern_sg_entries = 0;
9462 if (data_len < alloc_len) {
9463 ctsio->residual = alloc_len - data_len;
9464 ctsio->kern_data_len = data_len;
9465 ctsio->kern_total_len = data_len;
9467 ctsio->residual = 0;
9468 ctsio->kern_data_len = alloc_len;
9469 ctsio->kern_total_len = alloc_len;
9471 ctsio->kern_data_resid = 0;
9472 ctsio->kern_rel_offset = 0;
9473 ctsio->kern_sg_entries = 0;
9476 * The control device is always connected. The disk device, on the
9477 * other hand, may not be online all the time.
9480 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9481 lun->be_lun->lun_type;
9483 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9484 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9485 scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9487 * We support head of queue, ordered and simple tags.
9489 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9491 * Volatile cache supported.
9493 eid_ptr->flags3 = SVPD_EID_V_SUP;
9496 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9497 * attention for a particular IT nexus on all LUNs once we report
9498 * it to that nexus once. This bit is required as of SPC-4.
9500 eid_ptr->flags4 = SVPD_EID_LUICLR;
9503 * We support revert to defaults (RTD) bit in MODE SELECT.
9505 eid_ptr->flags5 = SVPD_EID_RTD_SUP;
9508 * XXX KDM in order to correctly answer this, we would need
9509 * information from the SIM to determine how much sense data it
9510 * can send. So this would really be a path inquiry field, most
9511 * likely. This can be set to a maximum of 252 according to SPC-4,
9512 * but the hardware may or may not be able to support that much.
9513 * 0 just means that the maximum sense data length is not reported.
9515 eid_ptr->max_sense_length = 0;
9517 ctl_set_success(ctsio);
9518 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9519 ctsio->be_move_done = ctl_config_move_done;
9520 ctl_datamove((union ctl_io *)ctsio);
9521 return (CTL_RETVAL_COMPLETE);
9525 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9527 struct ctl_lun *lun = CTL_LUN(ctsio);
9528 struct scsi_vpd_mode_page_policy *mpp_ptr;
9531 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9532 sizeof(struct scsi_vpd_mode_page_policy_descr);
9534 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9535 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9536 ctsio->kern_sg_entries = 0;
9538 if (data_len < alloc_len) {
9539 ctsio->residual = alloc_len - data_len;
9540 ctsio->kern_data_len = data_len;
9541 ctsio->kern_total_len = data_len;
9543 ctsio->residual = 0;
9544 ctsio->kern_data_len = alloc_len;
9545 ctsio->kern_total_len = alloc_len;
9547 ctsio->kern_data_resid = 0;
9548 ctsio->kern_rel_offset = 0;
9549 ctsio->kern_sg_entries = 0;
9552 * The control device is always connected. The disk device, on the
9553 * other hand, may not be online all the time.
9556 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9557 lun->be_lun->lun_type;
9559 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9560 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9561 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9562 mpp_ptr->descr[0].page_code = 0x3f;
9563 mpp_ptr->descr[0].subpage_code = 0xff;
9564 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9566 ctl_set_success(ctsio);
9567 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9568 ctsio->be_move_done = ctl_config_move_done;
9569 ctl_datamove((union ctl_io *)ctsio);
9570 return (CTL_RETVAL_COMPLETE);
9574 * SCSI VPD page 0x83, the Device Identification page.
9577 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9579 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9580 struct ctl_port *port = CTL_PORT(ctsio);
9581 struct ctl_lun *lun = CTL_LUN(ctsio);
9582 struct scsi_vpd_device_id *devid_ptr;
9583 struct scsi_vpd_id_descriptor *desc;
9587 data_len = sizeof(struct scsi_vpd_device_id) +
9588 sizeof(struct scsi_vpd_id_descriptor) +
9589 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9590 sizeof(struct scsi_vpd_id_descriptor) +
9591 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9592 if (lun && lun->lun_devid)
9593 data_len += lun->lun_devid->len;
9594 if (port && port->port_devid)
9595 data_len += port->port_devid->len;
9596 if (port && port->target_devid)
9597 data_len += port->target_devid->len;
9599 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9600 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9601 ctsio->kern_sg_entries = 0;
9603 if (data_len < alloc_len) {
9604 ctsio->residual = alloc_len - data_len;
9605 ctsio->kern_data_len = data_len;
9606 ctsio->kern_total_len = data_len;
9608 ctsio->residual = 0;
9609 ctsio->kern_data_len = alloc_len;
9610 ctsio->kern_total_len = alloc_len;
9612 ctsio->kern_data_resid = 0;
9613 ctsio->kern_rel_offset = 0;
9614 ctsio->kern_sg_entries = 0;
9617 * The control device is always connected. The disk device, on the
9618 * other hand, may not be online all the time.
9621 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9622 lun->be_lun->lun_type;
9624 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9625 devid_ptr->page_code = SVPD_DEVICE_ID;
9626 scsi_ulto2b(data_len - 4, devid_ptr->length);
9628 if (port && port->port_type == CTL_PORT_FC)
9629 proto = SCSI_PROTO_FC << 4;
9630 else if (port && port->port_type == CTL_PORT_ISCSI)
9631 proto = SCSI_PROTO_ISCSI << 4;
9633 proto = SCSI_PROTO_SPI << 4;
9634 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9637 * We're using a LUN association here. i.e., this device ID is a
9638 * per-LUN identifier.
9640 if (lun && lun->lun_devid) {
9641 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9642 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9643 lun->lun_devid->len);
9647 * This is for the WWPN which is a port association.
9649 if (port && port->port_devid) {
9650 memcpy(desc, port->port_devid->data, port->port_devid->len);
9651 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9652 port->port_devid->len);
9656 * This is for the Relative Target Port(type 4h) identifier
9658 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9659 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9660 SVPD_ID_TYPE_RELTARG;
9662 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9663 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9664 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9667 * This is for the Target Port Group(type 5h) identifier
9669 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9670 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9671 SVPD_ID_TYPE_TPORTGRP;
9673 if (softc->is_single ||
9674 (port && port->status & CTL_PORT_STATUS_HA_SHARED))
9677 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt;
9678 scsi_ulto2b(g, &desc->identifier[2]);
9679 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9680 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9683 * This is for the Target identifier
9685 if (port && port->target_devid) {
9686 memcpy(desc, port->target_devid->data, port->target_devid->len);
9689 ctl_set_success(ctsio);
9690 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9691 ctsio->be_move_done = ctl_config_move_done;
9692 ctl_datamove((union ctl_io *)ctsio);
9693 return (CTL_RETVAL_COMPLETE);
9697 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9699 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9700 struct ctl_lun *lun = CTL_LUN(ctsio);
9701 struct scsi_vpd_scsi_ports *sp;
9702 struct scsi_vpd_port_designation *pd;
9703 struct scsi_vpd_port_designation_cont *pdc;
9704 struct ctl_port *port;
9705 int data_len, num_target_ports, iid_len, id_len;
9707 num_target_ports = 0;
9710 mtx_lock(&softc->ctl_lock);
9711 STAILQ_FOREACH(port, &softc->port_list, links) {
9712 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9715 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9718 if (port->init_devid)
9719 iid_len += port->init_devid->len;
9720 if (port->port_devid)
9721 id_len += port->port_devid->len;
9723 mtx_unlock(&softc->ctl_lock);
9725 data_len = sizeof(struct scsi_vpd_scsi_ports) +
9726 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9727 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9728 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9729 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9730 ctsio->kern_sg_entries = 0;
9732 if (data_len < alloc_len) {
9733 ctsio->residual = alloc_len - data_len;
9734 ctsio->kern_data_len = data_len;
9735 ctsio->kern_total_len = data_len;
9737 ctsio->residual = 0;
9738 ctsio->kern_data_len = alloc_len;
9739 ctsio->kern_total_len = alloc_len;
9741 ctsio->kern_data_resid = 0;
9742 ctsio->kern_rel_offset = 0;
9743 ctsio->kern_sg_entries = 0;
9746 * The control device is always connected. The disk device, on the
9747 * other hand, may not be online all the time. Need to change this
9748 * to figure out whether the disk device is actually online or not.
9751 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9752 lun->be_lun->lun_type;
9754 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9756 sp->page_code = SVPD_SCSI_PORTS;
9757 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9759 pd = &sp->design[0];
9761 mtx_lock(&softc->ctl_lock);
9762 STAILQ_FOREACH(port, &softc->port_list, links) {
9763 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9766 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9768 scsi_ulto2b(port->targ_port, pd->relative_port_id);
9769 if (port->init_devid) {
9770 iid_len = port->init_devid->len;
9771 memcpy(pd->initiator_transportid,
9772 port->init_devid->data, port->init_devid->len);
9775 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9776 pdc = (struct scsi_vpd_port_designation_cont *)
9777 (&pd->initiator_transportid[iid_len]);
9778 if (port->port_devid) {
9779 id_len = port->port_devid->len;
9780 memcpy(pdc->target_port_descriptors,
9781 port->port_devid->data, port->port_devid->len);
9784 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9785 pd = (struct scsi_vpd_port_designation *)
9786 ((uint8_t *)pdc->target_port_descriptors + id_len);
9788 mtx_unlock(&softc->ctl_lock);
9790 ctl_set_success(ctsio);
9791 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9792 ctsio->be_move_done = ctl_config_move_done;
9793 ctl_datamove((union ctl_io *)ctsio);
9794 return (CTL_RETVAL_COMPLETE);
9798 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9800 struct ctl_lun *lun = CTL_LUN(ctsio);
9801 struct scsi_vpd_block_limits *bl_ptr;
9804 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9805 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9806 ctsio->kern_sg_entries = 0;
9808 if (sizeof(*bl_ptr) < alloc_len) {
9809 ctsio->residual = alloc_len - sizeof(*bl_ptr);
9810 ctsio->kern_data_len = sizeof(*bl_ptr);
9811 ctsio->kern_total_len = sizeof(*bl_ptr);
9813 ctsio->residual = 0;
9814 ctsio->kern_data_len = alloc_len;
9815 ctsio->kern_total_len = alloc_len;
9817 ctsio->kern_data_resid = 0;
9818 ctsio->kern_rel_offset = 0;
9819 ctsio->kern_sg_entries = 0;
9822 * The control device is always connected. The disk device, on the
9823 * other hand, may not be online all the time. Need to change this
9824 * to figure out whether the disk device is actually online or not.
9827 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9828 lun->be_lun->lun_type;
9830 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9832 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9833 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
9834 bl_ptr->max_cmp_write_len = 0xff;
9835 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9837 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
9838 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9840 ctl_get_opt_number(&lun->be_lun->options,
9841 "unmap_max_lba", &ival);
9842 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt);
9844 ctl_get_opt_number(&lun->be_lun->options,
9845 "unmap_max_descr", &ival);
9846 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt);
9847 if (lun->be_lun->ublockexp != 0) {
9848 scsi_ulto4b((1 << lun->be_lun->ublockexp),
9849 bl_ptr->opt_unmap_grain);
9850 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
9851 bl_ptr->unmap_grain_align);
9854 scsi_ulto4b(lun->be_lun->atomicblock,
9855 bl_ptr->max_atomic_transfer_length);
9856 scsi_ulto4b(0, bl_ptr->atomic_alignment);
9857 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
9858 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary);
9859 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size);
9861 ctl_get_opt_number(&lun->be_lun->options, "write_same_max_lba", &ival);
9862 scsi_u64to8b(ival, bl_ptr->max_write_same_length);
9865 ctl_set_success(ctsio);
9866 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9867 ctsio->be_move_done = ctl_config_move_done;
9868 ctl_datamove((union ctl_io *)ctsio);
9869 return (CTL_RETVAL_COMPLETE);
9873 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
9875 struct ctl_lun *lun = CTL_LUN(ctsio);
9876 struct scsi_vpd_block_device_characteristics *bdc_ptr;
9880 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
9881 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
9882 ctsio->kern_sg_entries = 0;
9884 if (sizeof(*bdc_ptr) < alloc_len) {
9885 ctsio->residual = alloc_len - sizeof(*bdc_ptr);
9886 ctsio->kern_data_len = sizeof(*bdc_ptr);
9887 ctsio->kern_total_len = sizeof(*bdc_ptr);
9889 ctsio->residual = 0;
9890 ctsio->kern_data_len = alloc_len;
9891 ctsio->kern_total_len = alloc_len;
9893 ctsio->kern_data_resid = 0;
9894 ctsio->kern_rel_offset = 0;
9895 ctsio->kern_sg_entries = 0;
9898 * The control device is always connected. The disk device, on the
9899 * other hand, may not be online all the time. Need to change this
9900 * to figure out whether the disk device is actually online or not.
9903 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9904 lun->be_lun->lun_type;
9906 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9907 bdc_ptr->page_code = SVPD_BDC;
9908 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
9910 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL)
9911 i = strtol(value, NULL, 0);
9913 i = CTL_DEFAULT_ROTATION_RATE;
9914 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
9916 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL)
9917 i = strtol(value, NULL, 0);
9920 bdc_ptr->wab_wac_ff = (i & 0x0f);
9921 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
9923 ctl_set_success(ctsio);
9924 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9925 ctsio->be_move_done = ctl_config_move_done;
9926 ctl_datamove((union ctl_io *)ctsio);
9927 return (CTL_RETVAL_COMPLETE);
9931 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
9933 struct ctl_lun *lun = CTL_LUN(ctsio);
9934 struct scsi_vpd_logical_block_prov *lbp_ptr;
9937 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
9938 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
9939 ctsio->kern_sg_entries = 0;
9941 if (sizeof(*lbp_ptr) < alloc_len) {
9942 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
9943 ctsio->kern_data_len = sizeof(*lbp_ptr);
9944 ctsio->kern_total_len = sizeof(*lbp_ptr);
9946 ctsio->residual = 0;
9947 ctsio->kern_data_len = alloc_len;
9948 ctsio->kern_total_len = alloc_len;
9950 ctsio->kern_data_resid = 0;
9951 ctsio->kern_rel_offset = 0;
9952 ctsio->kern_sg_entries = 0;
9955 * The control device is always connected. The disk device, on the
9956 * other hand, may not be online all the time. Need to change this
9957 * to figure out whether the disk device is actually online or not.
9960 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9961 lun->be_lun->lun_type;
9963 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9965 lbp_ptr->page_code = SVPD_LBP;
9966 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
9967 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
9968 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9969 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
9970 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
9971 value = ctl_get_opt(&lun->be_lun->options, "provisioning_type");
9972 if (value != NULL) {
9973 if (strcmp(value, "resource") == 0)
9974 lbp_ptr->prov_type = SVPD_LBP_RESOURCE;
9975 else if (strcmp(value, "thin") == 0)
9976 lbp_ptr->prov_type = SVPD_LBP_THIN;
9978 lbp_ptr->prov_type = SVPD_LBP_THIN;
9981 ctl_set_success(ctsio);
9982 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9983 ctsio->be_move_done = ctl_config_move_done;
9984 ctl_datamove((union ctl_io *)ctsio);
9985 return (CTL_RETVAL_COMPLETE);
9989 * INQUIRY with the EVPD bit set.
9992 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
9994 struct ctl_lun *lun = CTL_LUN(ctsio);
9995 struct scsi_inquiry *cdb;
9996 int alloc_len, retval;
9998 cdb = (struct scsi_inquiry *)ctsio->cdb;
9999 alloc_len = scsi_2btoul(cdb->length);
10001 switch (cdb->page_code) {
10002 case SVPD_SUPPORTED_PAGES:
10003 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
10005 case SVPD_UNIT_SERIAL_NUMBER:
10006 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
10008 case SVPD_DEVICE_ID:
10009 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
10011 case SVPD_EXTENDED_INQUIRY_DATA:
10012 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
10014 case SVPD_MODE_PAGE_POLICY:
10015 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
10017 case SVPD_SCSI_PORTS:
10018 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10020 case SVPD_SCSI_TPC:
10021 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10023 case SVPD_BLOCK_LIMITS:
10024 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10026 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10029 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10031 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10034 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10036 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10040 ctl_set_invalid_field(ctsio,
10046 ctl_done((union ctl_io *)ctsio);
10047 retval = CTL_RETVAL_COMPLETE;
10055 * Standard INQUIRY data.
10058 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10060 struct ctl_softc *softc = CTL_SOFTC(ctsio);
10061 struct ctl_port *port = CTL_PORT(ctsio);
10062 struct ctl_lun *lun = CTL_LUN(ctsio);
10063 struct scsi_inquiry_data *inq_ptr;
10064 struct scsi_inquiry *cdb;
10066 uint32_t alloc_len, data_len;
10067 ctl_port_type port_type;
10069 port_type = port->port_type;
10070 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10071 port_type = CTL_PORT_SCSI;
10073 cdb = (struct scsi_inquiry *)ctsio->cdb;
10074 alloc_len = scsi_2btoul(cdb->length);
10077 * We malloc the full inquiry data size here and fill it
10078 * in. If the user only asks for less, we'll give him
10081 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10082 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10083 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10084 ctsio->kern_sg_entries = 0;
10085 ctsio->kern_data_resid = 0;
10086 ctsio->kern_rel_offset = 0;
10088 if (data_len < alloc_len) {
10089 ctsio->residual = alloc_len - data_len;
10090 ctsio->kern_data_len = data_len;
10091 ctsio->kern_total_len = data_len;
10093 ctsio->residual = 0;
10094 ctsio->kern_data_len = alloc_len;
10095 ctsio->kern_total_len = alloc_len;
10099 if ((lun->flags & CTL_LUN_PRIMARY_SC) ||
10100 softc->ha_link >= CTL_HA_LINK_UNKNOWN) {
10101 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10102 lun->be_lun->lun_type;
10104 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) |
10105 lun->be_lun->lun_type;
10107 if (lun->flags & CTL_LUN_REMOVABLE)
10108 inq_ptr->dev_qual2 |= SID_RMB;
10110 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10112 /* RMB in byte 2 is 0 */
10113 inq_ptr->version = SCSI_REV_SPC5;
10116 * According to SAM-3, even if a device only supports a single
10117 * level of LUN addressing, it should still set the HISUP bit:
10119 * 4.9.1 Logical unit numbers overview
10121 * All logical unit number formats described in this standard are
10122 * hierarchical in structure even when only a single level in that
10123 * hierarchy is used. The HISUP bit shall be set to one in the
10124 * standard INQUIRY data (see SPC-2) when any logical unit number
10125 * format described in this standard is used. Non-hierarchical
10126 * formats are outside the scope of this standard.
10128 * Therefore we set the HiSup bit here.
10130 * The response format is 2, per SPC-3.
10132 inq_ptr->response_format = SID_HiSup | 2;
10134 inq_ptr->additional_length = data_len -
10135 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10136 CTL_DEBUG_PRINT(("additional_length = %d\n",
10137 inq_ptr->additional_length));
10139 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10140 if (port_type == CTL_PORT_SCSI)
10141 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10142 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10143 inq_ptr->flags = SID_CmdQue;
10144 if (port_type == CTL_PORT_SCSI)
10145 inq_ptr->flags |= SID_WBus16 | SID_Sync;
10148 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10149 * We have 8 bytes for the vendor name, and 16 bytes for the device
10150 * name and 4 bytes for the revision.
10152 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10153 "vendor")) == NULL) {
10154 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10156 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10157 strncpy(inq_ptr->vendor, val,
10158 min(sizeof(inq_ptr->vendor), strlen(val)));
10161 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10162 sizeof(inq_ptr->product));
10163 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10164 switch (lun->be_lun->lun_type) {
10166 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10167 sizeof(inq_ptr->product));
10170 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10171 sizeof(inq_ptr->product));
10174 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT,
10175 sizeof(inq_ptr->product));
10178 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10179 sizeof(inq_ptr->product));
10183 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10184 strncpy(inq_ptr->product, val,
10185 min(sizeof(inq_ptr->product), strlen(val)));
10189 * XXX make this a macro somewhere so it automatically gets
10190 * incremented when we make changes.
10192 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10193 "revision")) == NULL) {
10194 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10196 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10197 strncpy(inq_ptr->revision, val,
10198 min(sizeof(inq_ptr->revision), strlen(val)));
10202 * For parallel SCSI, we support double transition and single
10203 * transition clocking. We also support QAS (Quick Arbitration
10204 * and Selection) and Information Unit transfers on both the
10205 * control and array devices.
10207 if (port_type == CTL_PORT_SCSI)
10208 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10211 /* SAM-6 (no version claimed) */
10212 scsi_ulto2b(0x00C0, inq_ptr->version1);
10213 /* SPC-5 (no version claimed) */
10214 scsi_ulto2b(0x05C0, inq_ptr->version2);
10215 if (port_type == CTL_PORT_FC) {
10216 /* FCP-2 ANSI INCITS.350:2003 */
10217 scsi_ulto2b(0x0917, inq_ptr->version3);
10218 } else if (port_type == CTL_PORT_SCSI) {
10219 /* SPI-4 ANSI INCITS.362:200x */
10220 scsi_ulto2b(0x0B56, inq_ptr->version3);
10221 } else if (port_type == CTL_PORT_ISCSI) {
10222 /* iSCSI (no version claimed) */
10223 scsi_ulto2b(0x0960, inq_ptr->version3);
10224 } else if (port_type == CTL_PORT_SAS) {
10225 /* SAS (no version claimed) */
10226 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10230 /* SBC-4 (no version claimed) */
10231 scsi_ulto2b(0x0600, inq_ptr->version4);
10233 switch (lun->be_lun->lun_type) {
10235 /* SBC-4 (no version claimed) */
10236 scsi_ulto2b(0x0600, inq_ptr->version4);
10241 /* MMC-6 (no version claimed) */
10242 scsi_ulto2b(0x04E0, inq_ptr->version4);
10249 ctl_set_success(ctsio);
10250 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10251 ctsio->be_move_done = ctl_config_move_done;
10252 ctl_datamove((union ctl_io *)ctsio);
10253 return (CTL_RETVAL_COMPLETE);
10257 ctl_inquiry(struct ctl_scsiio *ctsio)
10259 struct scsi_inquiry *cdb;
10262 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10264 cdb = (struct scsi_inquiry *)ctsio->cdb;
10265 if (cdb->byte2 & SI_EVPD)
10266 retval = ctl_inquiry_evpd(ctsio);
10267 else if (cdb->page_code == 0)
10268 retval = ctl_inquiry_std(ctsio);
10270 ctl_set_invalid_field(ctsio,
10276 ctl_done((union ctl_io *)ctsio);
10277 return (CTL_RETVAL_COMPLETE);
10284 ctl_get_config(struct ctl_scsiio *ctsio)
10286 struct ctl_lun *lun = CTL_LUN(ctsio);
10287 struct scsi_get_config_header *hdr;
10288 struct scsi_get_config_feature *feature;
10289 struct scsi_get_config *cdb;
10290 uint32_t alloc_len, data_len;
10293 cdb = (struct scsi_get_config *)ctsio->cdb;
10294 rt = (cdb->rt & SGC_RT_MASK);
10295 starting = scsi_2btoul(cdb->starting_feature);
10296 alloc_len = scsi_2btoul(cdb->length);
10298 data_len = sizeof(struct scsi_get_config_header) +
10299 sizeof(struct scsi_get_config_feature) + 8 +
10300 sizeof(struct scsi_get_config_feature) + 8 +
10301 sizeof(struct scsi_get_config_feature) + 4 +
10302 sizeof(struct scsi_get_config_feature) + 4 +
10303 sizeof(struct scsi_get_config_feature) + 8 +
10304 sizeof(struct scsi_get_config_feature) +
10305 sizeof(struct scsi_get_config_feature) + 4 +
10306 sizeof(struct scsi_get_config_feature) + 4 +
10307 sizeof(struct scsi_get_config_feature) + 4 +
10308 sizeof(struct scsi_get_config_feature) + 4 +
10309 sizeof(struct scsi_get_config_feature) + 4 +
10310 sizeof(struct scsi_get_config_feature) + 4;
10311 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10312 ctsio->kern_sg_entries = 0;
10313 ctsio->kern_data_resid = 0;
10314 ctsio->kern_rel_offset = 0;
10316 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr;
10317 if (lun->flags & CTL_LUN_NO_MEDIA)
10318 scsi_ulto2b(0x0000, hdr->current_profile);
10320 scsi_ulto2b(0x0010, hdr->current_profile);
10321 feature = (struct scsi_get_config_feature *)(hdr + 1);
10323 if (starting > 0x003b)
10325 if (starting > 0x003a)
10327 if (starting > 0x002b)
10329 if (starting > 0x002a)
10331 if (starting > 0x001f)
10333 if (starting > 0x001e)
10335 if (starting > 0x001d)
10337 if (starting > 0x0010)
10339 if (starting > 0x0003)
10341 if (starting > 0x0002)
10343 if (starting > 0x0001)
10345 if (starting > 0x0000)
10349 scsi_ulto2b(0x0000, feature->feature_code);
10350 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT;
10351 feature->add_length = 8;
10352 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */
10353 feature->feature_data[2] = 0x00;
10354 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */
10355 feature->feature_data[6] = 0x01;
10356 feature = (struct scsi_get_config_feature *)
10357 &feature->feature_data[feature->add_length];
10360 scsi_ulto2b(0x0001, feature->feature_code);
10361 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10362 feature->add_length = 8;
10363 scsi_ulto4b(0x00000000, &feature->feature_data[0]);
10364 feature->feature_data[4] = 0x03;
10365 feature = (struct scsi_get_config_feature *)
10366 &feature->feature_data[feature->add_length];
10369 scsi_ulto2b(0x0002, feature->feature_code);
10370 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10371 feature->add_length = 4;
10372 feature->feature_data[0] = 0x02;
10373 feature = (struct scsi_get_config_feature *)
10374 &feature->feature_data[feature->add_length];
10376 f3: /* Removable Medium */
10377 scsi_ulto2b(0x0003, feature->feature_code);
10378 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10379 feature->add_length = 4;
10380 feature->feature_data[0] = 0x39;
10381 feature = (struct scsi_get_config_feature *)
10382 &feature->feature_data[feature->add_length];
10384 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA))
10387 f10: /* Random Read */
10388 scsi_ulto2b(0x0010, feature->feature_code);
10389 feature->flags = 0x00;
10390 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10391 feature->flags |= SGC_F_CURRENT;
10392 feature->add_length = 8;
10393 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]);
10394 scsi_ulto2b(1, &feature->feature_data[4]);
10395 feature->feature_data[6] = 0x00;
10396 feature = (struct scsi_get_config_feature *)
10397 &feature->feature_data[feature->add_length];
10399 f1d: /* Multi-Read */
10400 scsi_ulto2b(0x001D, feature->feature_code);
10401 feature->flags = 0x00;
10402 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10403 feature->flags |= SGC_F_CURRENT;
10404 feature->add_length = 0;
10405 feature = (struct scsi_get_config_feature *)
10406 &feature->feature_data[feature->add_length];
10409 scsi_ulto2b(0x001E, feature->feature_code);
10410 feature->flags = 0x00;
10411 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10412 feature->flags |= SGC_F_CURRENT;
10413 feature->add_length = 4;
10414 feature->feature_data[0] = 0x00;
10415 feature = (struct scsi_get_config_feature *)
10416 &feature->feature_data[feature->add_length];
10418 f1f: /* DVD Read */
10419 scsi_ulto2b(0x001F, feature->feature_code);
10420 feature->flags = 0x08;
10421 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10422 feature->flags |= SGC_F_CURRENT;
10423 feature->add_length = 4;
10424 feature->feature_data[0] = 0x01;
10425 feature->feature_data[2] = 0x03;
10426 feature = (struct scsi_get_config_feature *)
10427 &feature->feature_data[feature->add_length];
10430 scsi_ulto2b(0x002A, feature->feature_code);
10431 feature->flags = 0x04;
10432 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10433 feature->flags |= SGC_F_CURRENT;
10434 feature->add_length = 4;
10435 feature->feature_data[0] = 0x00;
10436 feature->feature_data[1] = 0x00;
10437 feature = (struct scsi_get_config_feature *)
10438 &feature->feature_data[feature->add_length];
10441 scsi_ulto2b(0x002B, feature->feature_code);
10442 feature->flags = 0x00;
10443 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10444 feature->flags |= SGC_F_CURRENT;
10445 feature->add_length = 4;
10446 feature->feature_data[0] = 0x00;
10447 feature = (struct scsi_get_config_feature *)
10448 &feature->feature_data[feature->add_length];
10450 f3a: /* DVD+RW Dual Layer */
10451 scsi_ulto2b(0x003A, feature->feature_code);
10452 feature->flags = 0x00;
10453 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10454 feature->flags |= SGC_F_CURRENT;
10455 feature->add_length = 4;
10456 feature->feature_data[0] = 0x00;
10457 feature->feature_data[1] = 0x00;
10458 feature = (struct scsi_get_config_feature *)
10459 &feature->feature_data[feature->add_length];
10461 f3b: /* DVD+R Dual Layer */
10462 scsi_ulto2b(0x003B, feature->feature_code);
10463 feature->flags = 0x00;
10464 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10465 feature->flags |= SGC_F_CURRENT;
10466 feature->add_length = 4;
10467 feature->feature_data[0] = 0x00;
10468 feature = (struct scsi_get_config_feature *)
10469 &feature->feature_data[feature->add_length];
10472 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10473 if (rt == SGC_RT_SPECIFIC && data_len > 4) {
10474 feature = (struct scsi_get_config_feature *)(hdr + 1);
10475 if (scsi_2btoul(feature->feature_code) == starting)
10476 feature = (struct scsi_get_config_feature *)
10477 &feature->feature_data[feature->add_length];
10478 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10480 scsi_ulto4b(data_len - 4, hdr->data_length);
10481 if (data_len < alloc_len) {
10482 ctsio->residual = alloc_len - data_len;
10483 ctsio->kern_data_len = data_len;
10484 ctsio->kern_total_len = data_len;
10486 ctsio->residual = 0;
10487 ctsio->kern_data_len = alloc_len;
10488 ctsio->kern_total_len = alloc_len;
10491 ctl_set_success(ctsio);
10492 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10493 ctsio->be_move_done = ctl_config_move_done;
10494 ctl_datamove((union ctl_io *)ctsio);
10495 return (CTL_RETVAL_COMPLETE);
10499 ctl_get_event_status(struct ctl_scsiio *ctsio)
10501 struct scsi_get_event_status_header *hdr;
10502 struct scsi_get_event_status *cdb;
10503 uint32_t alloc_len, data_len;
10506 cdb = (struct scsi_get_event_status *)ctsio->cdb;
10507 if ((cdb->byte2 & SGESN_POLLED) == 0) {
10508 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
10509 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
10510 ctl_done((union ctl_io *)ctsio);
10511 return (CTL_RETVAL_COMPLETE);
10513 notif_class = cdb->notif_class;
10514 alloc_len = scsi_2btoul(cdb->length);
10516 data_len = sizeof(struct scsi_get_event_status_header);
10517 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10518 ctsio->kern_sg_entries = 0;
10519 ctsio->kern_data_resid = 0;
10520 ctsio->kern_rel_offset = 0;
10522 if (data_len < alloc_len) {
10523 ctsio->residual = alloc_len - data_len;
10524 ctsio->kern_data_len = data_len;
10525 ctsio->kern_total_len = data_len;
10527 ctsio->residual = 0;
10528 ctsio->kern_data_len = alloc_len;
10529 ctsio->kern_total_len = alloc_len;
10532 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr;
10533 scsi_ulto2b(0, hdr->descr_length);
10534 hdr->nea_class = SGESN_NEA;
10535 hdr->supported_class = 0;
10537 ctl_set_success(ctsio);
10538 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10539 ctsio->be_move_done = ctl_config_move_done;
10540 ctl_datamove((union ctl_io *)ctsio);
10541 return (CTL_RETVAL_COMPLETE);
10545 ctl_mechanism_status(struct ctl_scsiio *ctsio)
10547 struct scsi_mechanism_status_header *hdr;
10548 struct scsi_mechanism_status *cdb;
10549 uint32_t alloc_len, data_len;
10551 cdb = (struct scsi_mechanism_status *)ctsio->cdb;
10552 alloc_len = scsi_2btoul(cdb->length);
10554 data_len = sizeof(struct scsi_mechanism_status_header);
10555 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10556 ctsio->kern_sg_entries = 0;
10557 ctsio->kern_data_resid = 0;
10558 ctsio->kern_rel_offset = 0;
10560 if (data_len < alloc_len) {
10561 ctsio->residual = alloc_len - data_len;
10562 ctsio->kern_data_len = data_len;
10563 ctsio->kern_total_len = data_len;
10565 ctsio->residual = 0;
10566 ctsio->kern_data_len = alloc_len;
10567 ctsio->kern_total_len = alloc_len;
10570 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr;
10571 hdr->state1 = 0x00;
10572 hdr->state2 = 0xe0;
10573 scsi_ulto3b(0, hdr->lba);
10574 hdr->slots_num = 0;
10575 scsi_ulto2b(0, hdr->slots_length);
10577 ctl_set_success(ctsio);
10578 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10579 ctsio->be_move_done = ctl_config_move_done;
10580 ctl_datamove((union ctl_io *)ctsio);
10581 return (CTL_RETVAL_COMPLETE);
10585 ctl_ultomsf(uint32_t lba, uint8_t *buf)
10590 buf[1] = bin2bcd((lba / 75) / 60);
10591 buf[2] = bin2bcd((lba / 75) % 60);
10592 buf[3] = bin2bcd(lba % 75);
10596 ctl_read_toc(struct ctl_scsiio *ctsio)
10598 struct ctl_lun *lun = CTL_LUN(ctsio);
10599 struct scsi_read_toc_hdr *hdr;
10600 struct scsi_read_toc_type01_descr *descr;
10601 struct scsi_read_toc *cdb;
10602 uint32_t alloc_len, data_len;
10605 cdb = (struct scsi_read_toc *)ctsio->cdb;
10606 msf = (cdb->byte2 & CD_MSF) != 0;
10607 format = cdb->format;
10608 alloc_len = scsi_2btoul(cdb->data_len);
10610 data_len = sizeof(struct scsi_read_toc_hdr);
10612 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr);
10614 data_len += sizeof(struct scsi_read_toc_type01_descr);
10615 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10616 ctsio->kern_sg_entries = 0;
10617 ctsio->kern_data_resid = 0;
10618 ctsio->kern_rel_offset = 0;
10620 if (data_len < alloc_len) {
10621 ctsio->residual = alloc_len - data_len;
10622 ctsio->kern_data_len = data_len;
10623 ctsio->kern_total_len = data_len;
10625 ctsio->residual = 0;
10626 ctsio->kern_data_len = alloc_len;
10627 ctsio->kern_total_len = alloc_len;
10630 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr;
10632 scsi_ulto2b(0x12, hdr->data_length);
10635 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10636 descr->addr_ctl = 0x14;
10637 descr->track_number = 1;
10639 ctl_ultomsf(0, descr->track_start);
10641 scsi_ulto4b(0, descr->track_start);
10643 descr->addr_ctl = 0x14;
10644 descr->track_number = 0xaa;
10646 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start);
10648 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start);
10650 scsi_ulto2b(0x0a, hdr->data_length);
10653 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10654 descr->addr_ctl = 0x14;
10655 descr->track_number = 1;
10657 ctl_ultomsf(0, descr->track_start);
10659 scsi_ulto4b(0, descr->track_start);
10662 ctl_set_success(ctsio);
10663 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10664 ctsio->be_move_done = ctl_config_move_done;
10665 ctl_datamove((union ctl_io *)ctsio);
10666 return (CTL_RETVAL_COMPLETE);
10670 * For known CDB types, parse the LBA and length.
10673 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10675 if (io->io_hdr.io_type != CTL_IO_SCSI)
10678 switch (io->scsiio.cdb[0]) {
10679 case COMPARE_AND_WRITE: {
10680 struct scsi_compare_and_write *cdb;
10682 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10684 *lba = scsi_8btou64(cdb->addr);
10685 *len = cdb->length;
10690 struct scsi_rw_6 *cdb;
10692 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10694 *lba = scsi_3btoul(cdb->addr);
10695 /* only 5 bits are valid in the most significant address byte */
10697 *len = cdb->length;
10702 struct scsi_rw_10 *cdb;
10704 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10706 *lba = scsi_4btoul(cdb->addr);
10707 *len = scsi_2btoul(cdb->length);
10710 case WRITE_VERIFY_10: {
10711 struct scsi_write_verify_10 *cdb;
10713 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10715 *lba = scsi_4btoul(cdb->addr);
10716 *len = scsi_2btoul(cdb->length);
10721 struct scsi_rw_12 *cdb;
10723 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10725 *lba = scsi_4btoul(cdb->addr);
10726 *len = scsi_4btoul(cdb->length);
10729 case WRITE_VERIFY_12: {
10730 struct scsi_write_verify_12 *cdb;
10732 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10734 *lba = scsi_4btoul(cdb->addr);
10735 *len = scsi_4btoul(cdb->length);
10740 struct scsi_rw_16 *cdb;
10742 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10744 *lba = scsi_8btou64(cdb->addr);
10745 *len = scsi_4btoul(cdb->length);
10748 case WRITE_ATOMIC_16: {
10749 struct scsi_write_atomic_16 *cdb;
10751 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb;
10753 *lba = scsi_8btou64(cdb->addr);
10754 *len = scsi_2btoul(cdb->length);
10757 case WRITE_VERIFY_16: {
10758 struct scsi_write_verify_16 *cdb;
10760 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10762 *lba = scsi_8btou64(cdb->addr);
10763 *len = scsi_4btoul(cdb->length);
10766 case WRITE_SAME_10: {
10767 struct scsi_write_same_10 *cdb;
10769 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10771 *lba = scsi_4btoul(cdb->addr);
10772 *len = scsi_2btoul(cdb->length);
10775 case WRITE_SAME_16: {
10776 struct scsi_write_same_16 *cdb;
10778 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10780 *lba = scsi_8btou64(cdb->addr);
10781 *len = scsi_4btoul(cdb->length);
10785 struct scsi_verify_10 *cdb;
10787 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10789 *lba = scsi_4btoul(cdb->addr);
10790 *len = scsi_2btoul(cdb->length);
10794 struct scsi_verify_12 *cdb;
10796 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10798 *lba = scsi_4btoul(cdb->addr);
10799 *len = scsi_4btoul(cdb->length);
10803 struct scsi_verify_16 *cdb;
10805 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10807 *lba = scsi_8btou64(cdb->addr);
10808 *len = scsi_4btoul(cdb->length);
10816 case SERVICE_ACTION_IN: { /* GET LBA STATUS */
10817 struct scsi_get_lba_status *cdb;
10819 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10820 *lba = scsi_8btou64(cdb->addr);
10826 break; /* NOTREACHED */
10833 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10836 uint64_t endlba1, endlba2;
10838 endlba1 = lba1 + len1 - (seq ? 0 : 1);
10839 endlba2 = lba2 + len2 - 1;
10841 if ((endlba1 < lba2) || (endlba2 < lba1))
10842 return (CTL_ACTION_PASS);
10844 return (CTL_ACTION_BLOCK);
10848 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10850 struct ctl_ptr_len_flags *ptrlen;
10851 struct scsi_unmap_desc *buf, *end, *range;
10855 /* If not UNMAP -- go other way. */
10856 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10857 io->scsiio.cdb[0] != UNMAP)
10858 return (CTL_ACTION_ERROR);
10860 /* If UNMAP without data -- block and wait for data. */
10861 ptrlen = (struct ctl_ptr_len_flags *)
10862 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10863 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10864 ptrlen->ptr == NULL)
10865 return (CTL_ACTION_BLOCK);
10867 /* UNMAP with data -- check for collision. */
10868 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10869 end = buf + ptrlen->len / sizeof(*buf);
10870 for (range = buf; range < end; range++) {
10871 lba = scsi_8btou64(range->lba);
10872 len = scsi_4btoul(range->length);
10873 if ((lba < lba2 + len2) && (lba + len > lba2))
10874 return (CTL_ACTION_BLOCK);
10876 return (CTL_ACTION_PASS);
10880 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10882 uint64_t lba1, lba2;
10883 uint64_t len1, len2;
10886 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10887 return (CTL_ACTION_ERROR);
10889 retval = ctl_extent_check_unmap(io1, lba2, len2);
10890 if (retval != CTL_ACTION_ERROR)
10893 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10894 return (CTL_ACTION_ERROR);
10896 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10898 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
10902 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2)
10904 uint64_t lba1, lba2;
10905 uint64_t len1, len2;
10907 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10908 return (CTL_ACTION_PASS);
10909 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10910 return (CTL_ACTION_ERROR);
10911 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10912 return (CTL_ACTION_ERROR);
10914 if (lba1 + len1 == lba2)
10915 return (CTL_ACTION_BLOCK);
10916 return (CTL_ACTION_PASS);
10920 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10921 union ctl_io *ooa_io)
10923 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10924 const ctl_serialize_action *serialize_row;
10927 * The initiator attempted multiple untagged commands at the same
10928 * time. Can't do that.
10930 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10931 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10932 && ((pending_io->io_hdr.nexus.targ_port ==
10933 ooa_io->io_hdr.nexus.targ_port)
10934 && (pending_io->io_hdr.nexus.initid ==
10935 ooa_io->io_hdr.nexus.initid))
10936 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10937 CTL_FLAG_STATUS_SENT)) == 0))
10938 return (CTL_ACTION_OVERLAP);
10941 * The initiator attempted to send multiple tagged commands with
10942 * the same ID. (It's fine if different initiators have the same
10945 * Even if all of those conditions are true, we don't kill the I/O
10946 * if the command ahead of us has been aborted. We won't end up
10947 * sending it to the FETD, and it's perfectly legal to resend a
10948 * command with the same tag number as long as the previous
10949 * instance of this tag number has been aborted somehow.
10951 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10952 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10953 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10954 && ((pending_io->io_hdr.nexus.targ_port ==
10955 ooa_io->io_hdr.nexus.targ_port)
10956 && (pending_io->io_hdr.nexus.initid ==
10957 ooa_io->io_hdr.nexus.initid))
10958 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10959 CTL_FLAG_STATUS_SENT)) == 0))
10960 return (CTL_ACTION_OVERLAP_TAG);
10963 * If we get a head of queue tag, SAM-3 says that we should
10964 * immediately execute it.
10966 * What happens if this command would normally block for some other
10967 * reason? e.g. a request sense with a head of queue tag
10968 * immediately after a write. Normally that would block, but this
10969 * will result in its getting executed immediately...
10971 * We currently return "pass" instead of "skip", so we'll end up
10972 * going through the rest of the queue to check for overlapped tags.
10974 * XXX KDM check for other types of blockage first??
10976 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10977 return (CTL_ACTION_PASS);
10980 * Ordered tags have to block until all items ahead of them
10981 * have completed. If we get called with an ordered tag, we always
10982 * block, if something else is ahead of us in the queue.
10984 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10985 return (CTL_ACTION_BLOCK);
10988 * Simple tags get blocked until all head of queue and ordered tags
10989 * ahead of them have completed. I'm lumping untagged commands in
10990 * with simple tags here. XXX KDM is that the right thing to do?
10992 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10993 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10994 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10995 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10996 return (CTL_ACTION_BLOCK);
10998 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
10999 KASSERT(pending_entry->seridx < CTL_SERIDX_COUNT,
11000 ("%s: Invalid seridx %d for pending CDB %02x %02x @ %p",
11001 __func__, pending_entry->seridx, pending_io->scsiio.cdb[0],
11002 pending_io->scsiio.cdb[1], pending_io));
11003 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
11004 if (ooa_entry->seridx == CTL_SERIDX_INVLD)
11005 return (CTL_ACTION_PASS); /* Unsupported command in OOA queue */
11006 KASSERT(ooa_entry->seridx < CTL_SERIDX_COUNT,
11007 ("%s: Invalid seridx %d for ooa CDB %02x %02x @ %p",
11008 __func__, ooa_entry->seridx, ooa_io->scsiio.cdb[0],
11009 ooa_io->scsiio.cdb[1], ooa_io));
11011 serialize_row = ctl_serialize_table[ooa_entry->seridx];
11013 switch (serialize_row[pending_entry->seridx]) {
11014 case CTL_SER_BLOCK:
11015 return (CTL_ACTION_BLOCK);
11016 case CTL_SER_EXTENT:
11017 return (ctl_extent_check(ooa_io, pending_io,
11018 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
11019 case CTL_SER_EXTENTOPT:
11020 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
11021 SCP_QUEUE_ALG_UNRESTRICTED)
11022 return (ctl_extent_check(ooa_io, pending_io,
11024 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
11025 return (CTL_ACTION_PASS);
11026 case CTL_SER_EXTENTSEQ:
11027 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF)
11028 return (ctl_extent_check_seq(ooa_io, pending_io));
11029 return (CTL_ACTION_PASS);
11031 return (CTL_ACTION_PASS);
11032 case CTL_SER_BLOCKOPT:
11033 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
11034 SCP_QUEUE_ALG_UNRESTRICTED)
11035 return (CTL_ACTION_BLOCK);
11036 return (CTL_ACTION_PASS);
11038 return (CTL_ACTION_SKIP);
11040 panic("%s: Invalid serialization value %d for %d => %d",
11041 __func__, serialize_row[pending_entry->seridx],
11042 pending_entry->seridx, ooa_entry->seridx);
11045 return (CTL_ACTION_ERROR);
11049 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
11051 * - pending_io is generally either incoming, or on the blocked queue
11052 * - starting I/O is the I/O we want to start the check with.
11055 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
11056 union ctl_io *starting_io)
11058 union ctl_io *ooa_io;
11061 mtx_assert(&lun->lun_lock, MA_OWNED);
11064 * Run back along the OOA queue, starting with the current
11065 * blocked I/O and going through every I/O before it on the
11066 * queue. If starting_io is NULL, we'll just end up returning
11069 for (ooa_io = starting_io; ooa_io != NULL;
11070 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
11074 * This routine just checks to see whether
11075 * cur_blocked is blocked by ooa_io, which is ahead
11076 * of it in the queue. It doesn't queue/dequeue
11079 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
11081 case CTL_ACTION_BLOCK:
11082 case CTL_ACTION_OVERLAP:
11083 case CTL_ACTION_OVERLAP_TAG:
11084 case CTL_ACTION_SKIP:
11085 case CTL_ACTION_ERROR:
11087 break; /* NOTREACHED */
11088 case CTL_ACTION_PASS:
11091 panic("%s: Invalid action %d\n", __func__, action);
11095 return (CTL_ACTION_PASS);
11100 * - An I/O has just completed, and has been removed from the per-LUN OOA
11101 * queue, so some items on the blocked queue may now be unblocked.
11104 ctl_check_blocked(struct ctl_lun *lun)
11106 struct ctl_softc *softc = lun->ctl_softc;
11107 union ctl_io *cur_blocked, *next_blocked;
11109 mtx_assert(&lun->lun_lock, MA_OWNED);
11112 * Run forward from the head of the blocked queue, checking each
11113 * entry against the I/Os prior to it on the OOA queue to see if
11114 * there is still any blockage.
11116 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
11117 * with our removing a variable on it while it is traversing the
11120 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
11121 cur_blocked != NULL; cur_blocked = next_blocked) {
11122 union ctl_io *prev_ooa;
11125 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
11128 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
11129 ctl_ooaq, ooa_links);
11132 * If cur_blocked happens to be the first item in the OOA
11133 * queue now, prev_ooa will be NULL, and the action
11134 * returned will just be CTL_ACTION_PASS.
11136 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
11139 case CTL_ACTION_BLOCK:
11140 /* Nothing to do here, still blocked */
11142 case CTL_ACTION_OVERLAP:
11143 case CTL_ACTION_OVERLAP_TAG:
11145 * This shouldn't happen! In theory we've already
11146 * checked this command for overlap...
11149 case CTL_ACTION_PASS:
11150 case CTL_ACTION_SKIP: {
11151 const struct ctl_cmd_entry *entry;
11154 * The skip case shouldn't happen, this transaction
11155 * should have never made it onto the blocked queue.
11158 * This I/O is no longer blocked, we can remove it
11159 * from the blocked queue. Since this is a TAILQ
11160 * (doubly linked list), we can do O(1) removals
11161 * from any place on the list.
11163 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
11165 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11167 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
11168 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){
11170 * Need to send IO back to original side to
11173 union ctl_ha_msg msg_info;
11175 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11176 msg_info.hdr.original_sc =
11177 cur_blocked->io_hdr.original_sc;
11178 msg_info.hdr.serializing_sc = cur_blocked;
11179 msg_info.hdr.msg_type = CTL_MSG_R2R;
11180 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11181 sizeof(msg_info.hdr), M_NOWAIT);
11184 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
11187 * Check this I/O for LUN state changes that may
11188 * have happened while this command was blocked.
11189 * The LUN state may have been changed by a command
11190 * ahead of us in the queue, so we need to re-check
11191 * for any states that can be caused by SCSI
11194 if (ctl_scsiio_lun_check(lun, entry,
11195 &cur_blocked->scsiio) == 0) {
11196 cur_blocked->io_hdr.flags |=
11197 CTL_FLAG_IS_WAS_ON_RTR;
11198 ctl_enqueue_rtr(cur_blocked);
11200 ctl_done(cur_blocked);
11205 * This probably shouldn't happen -- we shouldn't
11206 * get CTL_ACTION_ERROR, or anything else.
11212 return (CTL_RETVAL_COMPLETE);
11216 * This routine (with one exception) checks LUN flags that can be set by
11217 * commands ahead of us in the OOA queue. These flags have to be checked
11218 * when a command initially comes in, and when we pull a command off the
11219 * blocked queue and are preparing to execute it. The reason we have to
11220 * check these flags for commands on the blocked queue is that the LUN
11221 * state may have been changed by a command ahead of us while we're on the
11224 * Ordering is somewhat important with these checks, so please pay
11225 * careful attention to the placement of any new checks.
11228 ctl_scsiio_lun_check(struct ctl_lun *lun,
11229 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11231 struct ctl_softc *softc = lun->ctl_softc;
11237 mtx_assert(&lun->lun_lock, MA_OWNED);
11240 * If this shelf is a secondary shelf controller, we may have to
11241 * reject some commands disallowed by HA mode and link state.
11243 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11244 if (softc->ha_link == CTL_HA_LINK_OFFLINE &&
11245 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11246 ctl_set_lun_unavail(ctsio);
11250 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 &&
11251 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11252 ctl_set_lun_transit(ctsio);
11256 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY &&
11257 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) {
11258 ctl_set_lun_standby(ctsio);
11263 /* The rest of checks are only done on executing side */
11264 if (softc->ha_mode == CTL_HA_MODE_XFER)
11268 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11270 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) {
11271 ctl_set_hw_write_protected(ctsio);
11275 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) {
11276 ctl_set_sense(ctsio, /*current_error*/ 1,
11277 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11278 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11285 * Check for a reservation conflict. If this command isn't allowed
11286 * even on reserved LUNs, and if this initiator isn't the one who
11287 * reserved us, reject the command with a reservation conflict.
11289 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11290 if ((lun->flags & CTL_LUN_RESERVED)
11291 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11292 if (lun->res_idx != residx) {
11293 ctl_set_reservation_conflict(ctsio);
11299 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11300 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11301 /* No reservation or command is allowed. */;
11302 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11303 (lun->pr_res_type == SPR_TYPE_WR_EX ||
11304 lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
11305 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) {
11306 /* The command is allowed for Write Exclusive resv. */;
11309 * if we aren't registered or it's a res holder type
11310 * reservation and this isn't the res holder then set a
11313 if (ctl_get_prkey(lun, residx) == 0 ||
11314 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) {
11315 ctl_set_reservation_conflict(ctsio);
11321 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) {
11322 if (lun->flags & CTL_LUN_EJECTED)
11323 ctl_set_lun_ejected(ctsio);
11324 else if (lun->flags & CTL_LUN_NO_MEDIA) {
11325 if (lun->flags & CTL_LUN_REMOVABLE)
11326 ctl_set_lun_no_media(ctsio);
11328 ctl_set_lun_int_reqd(ctsio);
11329 } else if (lun->flags & CTL_LUN_STOPPED)
11330 ctl_set_lun_stopped(ctsio);
11342 ctl_failover_io(union ctl_io *io, int have_lock)
11344 ctl_set_busy(&io->scsiio);
11349 ctl_failover_lun(union ctl_io *rio)
11351 struct ctl_softc *softc = CTL_SOFTC(rio);
11352 struct ctl_lun *lun;
11353 struct ctl_io_hdr *io, *next_io;
11356 targ_lun = rio->io_hdr.nexus.targ_mapped_lun;
11357 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun));
11359 /* Find and lock the LUN. */
11360 mtx_lock(&softc->ctl_lock);
11361 if (targ_lun > CTL_MAX_LUNS ||
11362 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11363 mtx_unlock(&softc->ctl_lock);
11366 mtx_lock(&lun->lun_lock);
11367 mtx_unlock(&softc->ctl_lock);
11368 if (lun->flags & CTL_LUN_DISABLED) {
11369 mtx_unlock(&lun->lun_lock);
11373 if (softc->ha_mode == CTL_HA_MODE_XFER) {
11374 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11375 /* We are master */
11376 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11377 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11378 io->flags |= CTL_FLAG_ABORT;
11379 io->flags |= CTL_FLAG_FAILOVER;
11380 } else { /* This can be only due to DATAMOVE */
11381 io->msg_type = CTL_MSG_DATAMOVE_DONE;
11382 io->flags &= ~CTL_FLAG_DMA_INPROG;
11383 io->flags |= CTL_FLAG_IO_ACTIVE;
11384 io->port_status = 31340;
11385 ctl_enqueue_isc((union ctl_io *)io);
11389 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11390 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11391 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11392 io->flags |= CTL_FLAG_FAILOVER;
11394 ctl_set_busy(&((union ctl_io *)io)->
11396 ctl_done((union ctl_io *)io);
11400 } else { /* SERIALIZE modes */
11401 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links,
11403 /* We are master */
11404 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11405 TAILQ_REMOVE(&lun->blocked_queue, io,
11407 io->flags &= ~CTL_FLAG_BLOCKED;
11408 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11409 ctl_free_io((union ctl_io *)io);
11412 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11413 /* We are master */
11414 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11415 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11416 ctl_free_io((union ctl_io *)io);
11419 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11420 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11421 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) {
11422 ctl_set_busy(&((union ctl_io *)io)->
11424 ctl_done((union ctl_io *)io);
11428 ctl_check_blocked(lun);
11430 mtx_unlock(&lun->lun_lock);
11434 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio)
11436 struct ctl_lun *lun;
11437 const struct ctl_cmd_entry *entry;
11438 uint32_t initidx, targ_lun;
11442 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11443 if (targ_lun < CTL_MAX_LUNS)
11444 lun = softc->ctl_luns[targ_lun];
11447 * If the LUN is invalid, pretend that it doesn't exist.
11448 * It will go away as soon as all pending I/O has been
11451 mtx_lock(&lun->lun_lock);
11452 if (lun->flags & CTL_LUN_DISABLED) {
11453 mtx_unlock(&lun->lun_lock);
11457 CTL_LUN(ctsio) = lun;
11459 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
11462 * Every I/O goes into the OOA queue for a particular LUN,
11463 * and stays there until completion.
11466 if (TAILQ_EMPTY(&lun->ooa_queue))
11467 lun->idle_time += getsbinuptime() - lun->last_busy;
11469 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
11472 /* Get command entry and return error if it is unsuppotyed. */
11473 entry = ctl_validate_command(ctsio);
11474 if (entry == NULL) {
11476 mtx_unlock(&lun->lun_lock);
11480 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11481 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11484 * Check to see whether we can send this command to LUNs that don't
11485 * exist. This should pretty much only be the case for inquiry
11486 * and request sense. Further checks, below, really require having
11487 * a LUN, so we can't really check the command anymore. Just put
11488 * it on the rtr queue.
11491 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) {
11492 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11493 ctl_enqueue_rtr((union ctl_io *)ctsio);
11497 ctl_set_unsupported_lun(ctsio);
11498 ctl_done((union ctl_io *)ctsio);
11499 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11503 * Make sure we support this particular command on this LUN.
11504 * e.g., we don't support writes to the control LUN.
11506 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11507 mtx_unlock(&lun->lun_lock);
11508 ctl_set_invalid_opcode(ctsio);
11509 ctl_done((union ctl_io *)ctsio);
11514 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11518 * If we've got a request sense, it'll clear the contingent
11519 * allegiance condition. Otherwise, if we have a CA condition for
11520 * this initiator, clear it, because it sent down a command other
11521 * than request sense.
11523 if ((ctsio->cdb[0] != REQUEST_SENSE)
11524 && (ctl_is_set(lun->have_ca, initidx)))
11525 ctl_clear_mask(lun->have_ca, initidx);
11529 * If the command has this flag set, it handles its own unit
11530 * attention reporting, we shouldn't do anything. Otherwise we
11531 * check for any pending unit attentions, and send them back to the
11532 * initiator. We only do this when a command initially comes in,
11533 * not when we pull it off the blocked queue.
11535 * According to SAM-3, section 5.3.2, the order that things get
11536 * presented back to the host is basically unit attentions caused
11537 * by some sort of reset event, busy status, reservation conflicts
11538 * or task set full, and finally any other status.
11540 * One issue here is that some of the unit attentions we report
11541 * don't fall into the "reset" category (e.g. "reported luns data
11542 * has changed"). So reporting it here, before the reservation
11543 * check, may be technically wrong. I guess the only thing to do
11544 * would be to check for and report the reset events here, and then
11545 * check for the other unit attention types after we check for a
11546 * reservation conflict.
11548 * XXX KDM need to fix this
11550 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11551 ctl_ua_type ua_type;
11552 u_int sense_len = 0;
11554 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11555 &sense_len, SSD_TYPE_NONE);
11556 if (ua_type != CTL_UA_NONE) {
11557 mtx_unlock(&lun->lun_lock);
11558 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11559 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11560 ctsio->sense_len = sense_len;
11561 ctl_done((union ctl_io *)ctsio);
11567 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11568 mtx_unlock(&lun->lun_lock);
11569 ctl_done((union ctl_io *)ctsio);
11574 * XXX CHD this is where we want to send IO to other side if
11575 * this LUN is secondary on this SC. We will need to make a copy
11576 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11577 * the copy we send as FROM_OTHER.
11578 * We also need to stuff the address of the original IO so we can
11579 * find it easily. Something similar will need be done on the other
11580 * side so when we are done we can find the copy.
11582 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
11583 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 &&
11584 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) {
11585 union ctl_ha_msg msg_info;
11588 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11589 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11590 mtx_unlock(&lun->lun_lock);
11592 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11593 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11594 msg_info.hdr.serializing_sc = NULL;
11595 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11596 msg_info.scsi.tag_num = ctsio->tag_num;
11597 msg_info.scsi.tag_type = ctsio->tag_type;
11598 msg_info.scsi.cdb_len = ctsio->cdb_len;
11599 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11601 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11602 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data),
11603 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) {
11604 ctl_set_busy(ctsio);
11605 ctl_done((union ctl_io *)ctsio);
11611 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11612 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11613 ctl_ooaq, ooa_links))) {
11614 case CTL_ACTION_BLOCK:
11615 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11616 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11618 mtx_unlock(&lun->lun_lock);
11620 case CTL_ACTION_PASS:
11621 case CTL_ACTION_SKIP:
11622 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11623 mtx_unlock(&lun->lun_lock);
11624 ctl_enqueue_rtr((union ctl_io *)ctsio);
11626 case CTL_ACTION_OVERLAP:
11627 mtx_unlock(&lun->lun_lock);
11628 ctl_set_overlapped_cmd(ctsio);
11629 ctl_done((union ctl_io *)ctsio);
11631 case CTL_ACTION_OVERLAP_TAG:
11632 mtx_unlock(&lun->lun_lock);
11633 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11634 ctl_done((union ctl_io *)ctsio);
11636 case CTL_ACTION_ERROR:
11638 mtx_unlock(&lun->lun_lock);
11639 ctl_set_internal_failure(ctsio,
11641 /*retry_count*/ 0);
11642 ctl_done((union ctl_io *)ctsio);
11648 const struct ctl_cmd_entry *
11649 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11651 const struct ctl_cmd_entry *entry;
11652 int service_action;
11654 entry = &ctl_cmd_table[ctsio->cdb[0]];
11656 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11657 if (entry->flags & CTL_CMD_FLAG_SA5) {
11658 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11659 entry = &((const struct ctl_cmd_entry *)
11660 entry->execute)[service_action];
11665 const struct ctl_cmd_entry *
11666 ctl_validate_command(struct ctl_scsiio *ctsio)
11668 const struct ctl_cmd_entry *entry;
11672 entry = ctl_get_cmd_entry(ctsio, &sa);
11673 if (entry->execute == NULL) {
11675 ctl_set_invalid_field(ctsio,
11682 ctl_set_invalid_opcode(ctsio);
11683 ctl_done((union ctl_io *)ctsio);
11686 KASSERT(entry->length > 0,
11687 ("Not defined length for command 0x%02x/0x%02x",
11688 ctsio->cdb[0], ctsio->cdb[1]));
11689 for (i = 1; i < entry->length; i++) {
11690 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11693 ctl_set_invalid_field(ctsio,
11698 /*bit*/ fls(diff) - 1);
11699 ctl_done((union ctl_io *)ctsio);
11706 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11709 switch (lun_type) {
11711 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0)
11715 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
11719 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0)
11729 ctl_scsiio(struct ctl_scsiio *ctsio)
11732 const struct ctl_cmd_entry *entry;
11734 retval = CTL_RETVAL_COMPLETE;
11736 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11738 entry = ctl_get_cmd_entry(ctsio, NULL);
11741 * If this I/O has been aborted, just send it straight to
11742 * ctl_done() without executing it.
11744 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11745 ctl_done((union ctl_io *)ctsio);
11750 * All the checks should have been handled by ctl_scsiio_precheck().
11751 * We should be clear now to just execute the I/O.
11753 retval = entry->execute(ctsio);
11760 * Since we only implement one target right now, a bus reset simply resets
11761 * our single target.
11764 ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io)
11766 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET));
11770 ctl_target_reset(struct ctl_softc *softc, union ctl_io *io,
11771 ctl_ua_type ua_type)
11773 struct ctl_port *port = CTL_PORT(io);
11774 struct ctl_lun *lun;
11777 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11778 union ctl_ha_msg msg_info;
11780 msg_info.hdr.nexus = io->io_hdr.nexus;
11781 if (ua_type==CTL_UA_TARG_RESET)
11782 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11784 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11785 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11786 msg_info.hdr.original_sc = NULL;
11787 msg_info.hdr.serializing_sc = NULL;
11788 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11789 sizeof(msg_info.task), M_WAITOK);
11793 mtx_lock(&softc->ctl_lock);
11794 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11795 if (port != NULL &&
11796 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
11798 retval += ctl_do_lun_reset(lun, io, ua_type);
11800 mtx_unlock(&softc->ctl_lock);
11801 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11806 * The LUN should always be set. The I/O is optional, and is used to
11807 * distinguish between I/Os sent by this initiator, and by other
11808 * initiators. We set unit attention for initiators other than this one.
11809 * SAM-3 is vague on this point. It does say that a unit attention should
11810 * be established for other initiators when a LUN is reset (see section
11811 * 5.7.3), but it doesn't specifically say that the unit attention should
11812 * be established for this particular initiator when a LUN is reset. Here
11813 * is the relevant text, from SAM-3 rev 8:
11815 * 5.7.2 When a SCSI initiator port aborts its own tasks
11817 * When a SCSI initiator port causes its own task(s) to be aborted, no
11818 * notification that the task(s) have been aborted shall be returned to
11819 * the SCSI initiator port other than the completion response for the
11820 * command or task management function action that caused the task(s) to
11821 * be aborted and notification(s) associated with related effects of the
11822 * action (e.g., a reset unit attention condition).
11824 * XXX KDM for now, we're setting unit attention for all initiators.
11827 ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11835 mtx_lock(&lun->lun_lock);
11837 * Run through the OOA queue and abort each I/O.
11839 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11840 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11841 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11845 * This version sets unit attention for every
11848 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11849 ctl_est_ua_all(lun, initidx, ua_type);
11851 ctl_est_ua_all(lun, -1, ua_type);
11855 * A reset (any kind, really) clears reservations established with
11856 * RESERVE/RELEASE. It does not clear reservations established
11857 * with PERSISTENT RESERVE OUT, but we don't support that at the
11858 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
11859 * reservations made with the RESERVE/RELEASE commands, because
11860 * those commands are obsolete in SPC-3.
11862 lun->flags &= ~CTL_LUN_RESERVED;
11865 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11866 ctl_clear_mask(lun->have_ca, i);
11868 lun->prevent_count = 0;
11869 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11870 ctl_clear_mask(lun->prevent, i);
11871 mtx_unlock(&lun->lun_lock);
11877 ctl_lun_reset(struct ctl_softc *softc, union ctl_io *io)
11879 struct ctl_lun *lun;
11883 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11884 mtx_lock(&softc->ctl_lock);
11885 if (targ_lun >= CTL_MAX_LUNS ||
11886 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11887 mtx_unlock(&softc->ctl_lock);
11888 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11891 retval = ctl_do_lun_reset(lun, io, CTL_UA_LUN_RESET);
11892 mtx_unlock(&softc->ctl_lock);
11893 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11895 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) {
11896 union ctl_ha_msg msg_info;
11898 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11899 msg_info.hdr.nexus = io->io_hdr.nexus;
11900 msg_info.task.task_action = CTL_TASK_LUN_RESET;
11901 msg_info.hdr.original_sc = NULL;
11902 msg_info.hdr.serializing_sc = NULL;
11903 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11904 sizeof(msg_info.task), M_WAITOK);
11910 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11915 mtx_assert(&lun->lun_lock, MA_OWNED);
11918 * Run through the OOA queue and attempt to find the given I/O.
11919 * The target port, initiator ID, tag type and tag number have to
11920 * match the values that we got from the initiator. If we have an
11921 * untagged command to abort, simply abort the first untagged command
11922 * we come to. We only allow one untagged command at a time of course.
11924 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11925 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11927 if ((targ_port == UINT32_MAX ||
11928 targ_port == xio->io_hdr.nexus.targ_port) &&
11929 (init_id == UINT32_MAX ||
11930 init_id == xio->io_hdr.nexus.initid)) {
11931 if (targ_port != xio->io_hdr.nexus.targ_port ||
11932 init_id != xio->io_hdr.nexus.initid)
11933 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
11934 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11935 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11936 union ctl_ha_msg msg_info;
11938 msg_info.hdr.nexus = xio->io_hdr.nexus;
11939 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11940 msg_info.task.tag_num = xio->scsiio.tag_num;
11941 msg_info.task.tag_type = xio->scsiio.tag_type;
11942 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11943 msg_info.hdr.original_sc = NULL;
11944 msg_info.hdr.serializing_sc = NULL;
11945 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11946 sizeof(msg_info.task), M_NOWAIT);
11953 ctl_abort_task_set(union ctl_io *io)
11955 struct ctl_softc *softc = CTL_SOFTC(io);
11956 struct ctl_lun *lun;
11962 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11963 mtx_lock(&softc->ctl_lock);
11964 if (targ_lun >= CTL_MAX_LUNS ||
11965 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11966 mtx_unlock(&softc->ctl_lock);
11967 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11971 mtx_lock(&lun->lun_lock);
11972 mtx_unlock(&softc->ctl_lock);
11973 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
11974 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11975 io->io_hdr.nexus.initid,
11976 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11977 } else { /* CTL_TASK_CLEAR_TASK_SET */
11978 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
11979 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11981 mtx_unlock(&lun->lun_lock);
11982 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11987 ctl_i_t_nexus_reset(union ctl_io *io)
11989 struct ctl_softc *softc = CTL_SOFTC(io);
11990 struct ctl_lun *lun;
11993 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11994 union ctl_ha_msg msg_info;
11996 msg_info.hdr.nexus = io->io_hdr.nexus;
11997 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET;
11998 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11999 msg_info.hdr.original_sc = NULL;
12000 msg_info.hdr.serializing_sc = NULL;
12001 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12002 sizeof(msg_info.task), M_WAITOK);
12005 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12006 mtx_lock(&softc->ctl_lock);
12007 STAILQ_FOREACH(lun, &softc->lun_list, links) {
12008 mtx_lock(&lun->lun_lock);
12009 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12010 io->io_hdr.nexus.initid, 1);
12012 ctl_clear_mask(lun->have_ca, initidx);
12014 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx))
12015 lun->flags &= ~CTL_LUN_RESERVED;
12016 if (ctl_is_set(lun->prevent, initidx)) {
12017 ctl_clear_mask(lun->prevent, initidx);
12018 lun->prevent_count--;
12020 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS);
12021 mtx_unlock(&lun->lun_lock);
12023 mtx_unlock(&softc->ctl_lock);
12024 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12029 ctl_abort_task(union ctl_io *io)
12031 struct ctl_softc *softc = CTL_SOFTC(io);
12033 struct ctl_lun *lun;
12036 char printbuf[128];
12046 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12047 mtx_lock(&softc->ctl_lock);
12048 if (targ_lun >= CTL_MAX_LUNS ||
12049 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12050 mtx_unlock(&softc->ctl_lock);
12051 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12056 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
12057 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
12060 mtx_lock(&lun->lun_lock);
12061 mtx_unlock(&softc->ctl_lock);
12063 * Run through the OOA queue and attempt to find the given I/O.
12064 * The target port, initiator ID, tag type and tag number have to
12065 * match the values that we got from the initiator. If we have an
12066 * untagged command to abort, simply abort the first untagged command
12067 * we come to. We only allow one untagged command at a time of course.
12069 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12070 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12072 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
12074 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
12075 lun->lun, xio->scsiio.tag_num,
12076 xio->scsiio.tag_type,
12077 (xio->io_hdr.blocked_links.tqe_prev
12078 == NULL) ? "" : " BLOCKED",
12079 (xio->io_hdr.flags &
12080 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
12081 (xio->io_hdr.flags &
12082 CTL_FLAG_ABORT) ? " ABORT" : "",
12083 (xio->io_hdr.flags &
12084 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
12085 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
12087 printf("%s\n", sbuf_data(&sb));
12090 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12091 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12092 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12096 * If the abort says that the task is untagged, the
12097 * task in the queue must be untagged. Otherwise,
12098 * we just check to see whether the tag numbers
12099 * match. This is because the QLogic firmware
12100 * doesn't pass back the tag type in an abort
12104 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12105 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12106 || (xio->scsiio.tag_num == io->taskio.tag_num))
12109 * XXX KDM we've got problems with FC, because it
12110 * doesn't send down a tag type with aborts. So we
12111 * can only really go by the tag number...
12112 * This may cause problems with parallel SCSI.
12113 * Need to figure that out!!
12115 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12116 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12118 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
12119 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12120 union ctl_ha_msg msg_info;
12122 msg_info.hdr.nexus = io->io_hdr.nexus;
12123 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12124 msg_info.task.tag_num = io->taskio.tag_num;
12125 msg_info.task.tag_type = io->taskio.tag_type;
12126 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12127 msg_info.hdr.original_sc = NULL;
12128 msg_info.hdr.serializing_sc = NULL;
12130 printf("Sent Abort to other side\n");
12132 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12133 sizeof(msg_info.task), M_NOWAIT);
12136 printf("ctl_abort_task: found I/O to abort\n");
12140 mtx_unlock(&lun->lun_lock);
12144 * This isn't really an error. It's entirely possible for
12145 * the abort and command completion to cross on the wire.
12146 * This is more of an informative/diagnostic error.
12149 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
12150 "%u:%u:%u tag %d type %d\n",
12151 io->io_hdr.nexus.initid,
12152 io->io_hdr.nexus.targ_port,
12153 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
12154 io->taskio.tag_type);
12157 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12162 ctl_query_task(union ctl_io *io, int task_set)
12164 struct ctl_softc *softc = CTL_SOFTC(io);
12166 struct ctl_lun *lun;
12170 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12171 mtx_lock(&softc->ctl_lock);
12172 if (targ_lun >= CTL_MAX_LUNS ||
12173 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12174 mtx_unlock(&softc->ctl_lock);
12175 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12178 mtx_lock(&lun->lun_lock);
12179 mtx_unlock(&softc->ctl_lock);
12180 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12181 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12183 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12184 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12185 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12188 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) {
12193 mtx_unlock(&lun->lun_lock);
12195 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12197 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12202 ctl_query_async_event(union ctl_io *io)
12204 struct ctl_softc *softc = CTL_SOFTC(io);
12205 struct ctl_lun *lun;
12207 uint32_t targ_lun, initidx;
12209 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12210 mtx_lock(&softc->ctl_lock);
12211 if (targ_lun >= CTL_MAX_LUNS ||
12212 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12213 mtx_unlock(&softc->ctl_lock);
12214 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12217 mtx_lock(&lun->lun_lock);
12218 mtx_unlock(&softc->ctl_lock);
12219 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12220 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp);
12221 mtx_unlock(&lun->lun_lock);
12222 if (ua != CTL_UA_NONE)
12223 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12225 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12230 ctl_run_task(union ctl_io *io)
12232 struct ctl_softc *softc = CTL_SOFTC(io);
12235 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12236 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12237 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type));
12238 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED;
12239 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp));
12240 switch (io->taskio.task_action) {
12241 case CTL_TASK_ABORT_TASK:
12242 retval = ctl_abort_task(io);
12244 case CTL_TASK_ABORT_TASK_SET:
12245 case CTL_TASK_CLEAR_TASK_SET:
12246 retval = ctl_abort_task_set(io);
12248 case CTL_TASK_CLEAR_ACA:
12250 case CTL_TASK_I_T_NEXUS_RESET:
12251 retval = ctl_i_t_nexus_reset(io);
12253 case CTL_TASK_LUN_RESET:
12254 retval = ctl_lun_reset(softc, io);
12256 case CTL_TASK_TARGET_RESET:
12257 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET);
12259 case CTL_TASK_BUS_RESET:
12260 retval = ctl_bus_reset(softc, io);
12262 case CTL_TASK_PORT_LOGIN:
12264 case CTL_TASK_PORT_LOGOUT:
12266 case CTL_TASK_QUERY_TASK:
12267 retval = ctl_query_task(io, 0);
12269 case CTL_TASK_QUERY_TASK_SET:
12270 retval = ctl_query_task(io, 1);
12272 case CTL_TASK_QUERY_ASYNC_EVENT:
12273 retval = ctl_query_async_event(io);
12276 printf("%s: got unknown task management event %d\n",
12277 __func__, io->taskio.task_action);
12281 io->io_hdr.status = CTL_SUCCESS;
12283 io->io_hdr.status = CTL_ERROR;
12288 * For HA operation. Handle commands that come in from the other
12292 ctl_handle_isc(union ctl_io *io)
12294 struct ctl_softc *softc = CTL_SOFTC(io);
12295 struct ctl_lun *lun;
12296 const struct ctl_cmd_entry *entry;
12299 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12300 switch (io->io_hdr.msg_type) {
12301 case CTL_MSG_SERIALIZE:
12302 ctl_serialize_other_sc_cmd(&io->scsiio);
12304 case CTL_MSG_R2R: /* Only used in SER_ONLY mode. */
12305 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12306 if (targ_lun >= CTL_MAX_LUNS ||
12307 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12311 mtx_lock(&lun->lun_lock);
12312 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) {
12313 mtx_unlock(&lun->lun_lock);
12317 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12318 mtx_unlock(&lun->lun_lock);
12319 ctl_enqueue_rtr(io);
12321 case CTL_MSG_FINISH_IO:
12322 if (softc->ha_mode == CTL_HA_MODE_XFER) {
12326 if (targ_lun >= CTL_MAX_LUNS ||
12327 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12331 mtx_lock(&lun->lun_lock);
12332 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
12333 ctl_check_blocked(lun);
12334 mtx_unlock(&lun->lun_lock);
12337 case CTL_MSG_PERS_ACTION:
12338 ctl_hndl_per_res_out_on_other_sc(io);
12341 case CTL_MSG_BAD_JUJU:
12344 case CTL_MSG_DATAMOVE: /* Only used in XFER mode */
12345 ctl_datamove_remote(io);
12347 case CTL_MSG_DATAMOVE_DONE: /* Only used in XFER mode */
12348 io->scsiio.be_move_done(io);
12350 case CTL_MSG_FAILOVER:
12351 ctl_failover_lun(io);
12355 printf("%s: Invalid message type %d\n",
12356 __func__, io->io_hdr.msg_type);
12365 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12366 * there is no match.
12368 static ctl_lun_error_pattern
12369 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12371 const struct ctl_cmd_entry *entry;
12372 ctl_lun_error_pattern filtered_pattern, pattern;
12374 pattern = desc->error_pattern;
12377 * XXX KDM we need more data passed into this function to match a
12378 * custom pattern, and we actually need to implement custom pattern
12381 if (pattern & CTL_LUN_PAT_CMD)
12382 return (CTL_LUN_PAT_CMD);
12384 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12385 return (CTL_LUN_PAT_ANY);
12387 entry = ctl_get_cmd_entry(ctsio, NULL);
12389 filtered_pattern = entry->pattern & pattern;
12392 * If the user requested specific flags in the pattern (e.g.
12393 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12396 * If the user did not specify any flags, it doesn't matter whether
12397 * or not the command supports the flags.
12399 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12400 (pattern & ~CTL_LUN_PAT_MASK))
12401 return (CTL_LUN_PAT_NONE);
12404 * If the user asked for a range check, see if the requested LBA
12405 * range overlaps with this command's LBA range.
12407 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12413 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12415 return (CTL_LUN_PAT_NONE);
12417 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12418 desc->lba_range.len, FALSE);
12420 * A "pass" means that the LBA ranges don't overlap, so
12421 * this doesn't match the user's range criteria.
12423 if (action == CTL_ACTION_PASS)
12424 return (CTL_LUN_PAT_NONE);
12427 return (filtered_pattern);
12431 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12433 struct ctl_error_desc *desc, *desc2;
12435 mtx_assert(&lun->lun_lock, MA_OWNED);
12437 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12438 ctl_lun_error_pattern pattern;
12440 * Check to see whether this particular command matches
12441 * the pattern in the descriptor.
12443 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12444 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12447 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12448 case CTL_LUN_INJ_ABORTED:
12449 ctl_set_aborted(&io->scsiio);
12451 case CTL_LUN_INJ_MEDIUM_ERR:
12452 ctl_set_medium_error(&io->scsiio,
12453 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) !=
12454 CTL_FLAG_DATA_OUT);
12456 case CTL_LUN_INJ_UA:
12457 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12459 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12461 case CTL_LUN_INJ_CUSTOM:
12463 * We're assuming the user knows what he is doing.
12464 * Just copy the sense information without doing
12467 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12468 MIN(sizeof(desc->custom_sense),
12469 sizeof(io->scsiio.sense_data)));
12470 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12471 io->scsiio.sense_len = SSD_FULL_SIZE;
12472 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12474 case CTL_LUN_INJ_NONE:
12477 * If this is an error injection type we don't know
12478 * about, clear the continuous flag (if it is set)
12479 * so it will get deleted below.
12481 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12485 * By default, each error injection action is a one-shot
12487 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12490 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12496 #ifdef CTL_IO_DELAY
12498 ctl_datamove_timer_wakeup(void *arg)
12502 io = (union ctl_io *)arg;
12506 #endif /* CTL_IO_DELAY */
12509 ctl_datamove(union ctl_io *io)
12511 void (*fe_datamove)(union ctl_io *io);
12513 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
12515 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12518 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12523 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12524 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12526 sbuf_cat(&sb, path_str);
12527 switch (io->io_hdr.io_type) {
12529 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12530 sbuf_printf(&sb, "\n");
12531 sbuf_cat(&sb, path_str);
12532 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12533 io->scsiio.tag_num, io->scsiio.tag_type);
12536 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12537 "Tag Type: %d\n", io->taskio.task_action,
12538 io->taskio.tag_num, io->taskio.tag_type);
12541 panic("%s: Invalid CTL I/O type %d\n",
12542 __func__, io->io_hdr.io_type);
12544 sbuf_cat(&sb, path_str);
12545 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12546 (intmax_t)time_uptime - io->io_hdr.start_time);
12548 printf("%s", sbuf_data(&sb));
12550 #endif /* CTL_TIME_IO */
12552 #ifdef CTL_IO_DELAY
12553 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12554 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12557 && (lun->delay_info.datamove_delay > 0)) {
12559 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
12560 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12561 callout_reset(&io->io_hdr.delay_callout,
12562 lun->delay_info.datamove_delay * hz,
12563 ctl_datamove_timer_wakeup, io);
12564 if (lun->delay_info.datamove_type ==
12565 CTL_DELAY_TYPE_ONESHOT)
12566 lun->delay_info.datamove_delay = 0;
12573 * This command has been aborted. Set the port status, so we fail
12576 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12577 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n",
12578 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12579 io->io_hdr.nexus.targ_port,
12580 io->io_hdr.nexus.targ_lun);
12581 io->io_hdr.port_status = 31337;
12583 * Note that the backend, in this case, will get the
12584 * callback in its context. In other cases it may get
12585 * called in the frontend's interrupt thread context.
12587 io->scsiio.be_move_done(io);
12591 /* Don't confuse frontend with zero length data move. */
12592 if (io->scsiio.kern_data_len == 0) {
12593 io->scsiio.be_move_done(io);
12597 fe_datamove = CTL_PORT(io)->fe_datamove;
12602 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12604 union ctl_ha_msg msg;
12606 struct bintime cur_bt;
12609 memset(&msg, 0, sizeof(msg));
12610 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12611 msg.hdr.original_sc = io;
12612 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12613 msg.hdr.nexus = io->io_hdr.nexus;
12614 msg.hdr.status = io->io_hdr.status;
12615 msg.scsi.tag_num = io->scsiio.tag_num;
12616 msg.scsi.tag_type = io->scsiio.tag_type;
12617 msg.scsi.scsi_status = io->scsiio.scsi_status;
12618 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12619 io->scsiio.sense_len);
12620 msg.scsi.sense_len = io->scsiio.sense_len;
12621 msg.scsi.sense_residual = io->scsiio.sense_residual;
12622 msg.scsi.fetd_status = io->io_hdr.port_status;
12623 msg.scsi.residual = io->scsiio.residual;
12624 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12625 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12626 ctl_failover_io(io, /*have_lock*/ have_lock);
12629 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12630 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12631 msg.scsi.sense_len, M_WAITOK);
12634 getbinuptime(&cur_bt);
12635 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
12636 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
12638 io->io_hdr.num_dmas++;
12642 * The DMA to the remote side is done, now we need to tell the other side
12643 * we're done so it can continue with its data movement.
12646 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12653 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12654 printf("%s: ISC DMA write failed with error %d", __func__,
12656 ctl_set_internal_failure(&io->scsiio,
12658 /*retry_count*/ rq->ret);
12661 ctl_dt_req_free(rq);
12663 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12664 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12665 free(io->io_hdr.remote_sglist, M_CTL);
12666 io->io_hdr.remote_sglist = NULL;
12667 io->io_hdr.local_sglist = NULL;
12670 * The data is in local and remote memory, so now we need to send
12671 * status (good or back) back to the other side.
12673 ctl_send_datamove_done(io, /*have_lock*/ 0);
12677 * We've moved the data from the host/controller into local memory. Now we
12678 * need to push it over to the remote controller's memory.
12681 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12685 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12686 ctl_datamove_remote_write_cb);
12691 ctl_datamove_remote_write(union ctl_io *io)
12694 void (*fe_datamove)(union ctl_io *io);
12697 * - Get the data from the host/HBA into local memory.
12698 * - DMA memory from the local controller to the remote controller.
12699 * - Send status back to the remote controller.
12702 retval = ctl_datamove_remote_sgl_setup(io);
12706 /* Switch the pointer over so the FETD knows what to do */
12707 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12710 * Use a custom move done callback, since we need to send completion
12711 * back to the other controller, not to the backend on this side.
12713 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12715 fe_datamove = CTL_PORT(io)->fe_datamove;
12720 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12729 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12730 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12731 free(io->io_hdr.remote_sglist, M_CTL);
12732 io->io_hdr.remote_sglist = NULL;
12733 io->io_hdr.local_sglist = NULL;
12736 scsi_path_string(io, path_str, sizeof(path_str));
12737 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12738 sbuf_cat(&sb, path_str);
12739 scsi_command_string(&io->scsiio, NULL, &sb);
12740 sbuf_printf(&sb, "\n");
12741 sbuf_cat(&sb, path_str);
12742 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12743 io->scsiio.tag_num, io->scsiio.tag_type);
12744 sbuf_cat(&sb, path_str);
12745 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12746 io->io_hdr.flags, io->io_hdr.status);
12748 printk("%s", sbuf_data(&sb));
12753 * The read is done, now we need to send status (good or bad) back
12754 * to the other side.
12756 ctl_send_datamove_done(io, /*have_lock*/ 0);
12762 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12765 void (*fe_datamove)(union ctl_io *io);
12769 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12770 printf("%s: ISC DMA read failed with error %d\n", __func__,
12772 ctl_set_internal_failure(&io->scsiio,
12774 /*retry_count*/ rq->ret);
12777 ctl_dt_req_free(rq);
12779 /* Switch the pointer over so the FETD knows what to do */
12780 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12783 * Use a custom move done callback, since we need to send completion
12784 * back to the other controller, not to the backend on this side.
12786 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12788 /* XXX KDM add checks like the ones in ctl_datamove? */
12790 fe_datamove = CTL_PORT(io)->fe_datamove;
12795 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12797 struct ctl_sg_entry *local_sglist;
12798 uint32_t len_to_go;
12803 local_sglist = io->io_hdr.local_sglist;
12804 len_to_go = io->scsiio.kern_data_len;
12807 * The difficult thing here is that the size of the various
12808 * S/G segments may be different than the size from the
12809 * remote controller. That'll make it harder when DMAing
12810 * the data back to the other side.
12812 for (i = 0; len_to_go > 0; i++) {
12813 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT);
12814 local_sglist[i].addr =
12815 malloc(local_sglist[i].len, M_CTL, M_WAITOK);
12817 len_to_go -= local_sglist[i].len;
12820 * Reset the number of S/G entries accordingly. The original
12821 * number of S/G entries is available in rem_sg_entries.
12823 io->scsiio.kern_sg_entries = i;
12826 printf("%s: kern_sg_entries = %d\n", __func__,
12827 io->scsiio.kern_sg_entries);
12828 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12829 printf("%s: sg[%d] = %p, %lu\n", __func__, i,
12830 local_sglist[i].addr, local_sglist[i].len);
12837 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12838 ctl_ha_dt_cb callback)
12840 struct ctl_ha_dt_req *rq;
12841 struct ctl_sg_entry *remote_sglist, *local_sglist;
12842 uint32_t local_used, remote_used, total_used;
12845 rq = ctl_dt_req_alloc();
12848 * If we failed to allocate the request, and if the DMA didn't fail
12849 * anyway, set busy status. This is just a resource allocation
12853 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12854 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS))
12855 ctl_set_busy(&io->scsiio);
12857 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12858 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
12861 ctl_dt_req_free(rq);
12864 * The data move failed. We need to return status back
12865 * to the other controller. No point in trying to DMA
12866 * data to the remote controller.
12869 ctl_send_datamove_done(io, /*have_lock*/ 0);
12874 local_sglist = io->io_hdr.local_sglist;
12875 remote_sglist = io->io_hdr.remote_sglist;
12881 * Pull/push the data over the wire from/to the other controller.
12882 * This takes into account the possibility that the local and
12883 * remote sglists may not be identical in terms of the size of
12884 * the elements and the number of elements.
12886 * One fundamental assumption here is that the length allocated for
12887 * both the local and remote sglists is identical. Otherwise, we've
12888 * essentially got a coding error of some sort.
12890 isc_ret = CTL_HA_STATUS_SUCCESS;
12891 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12895 rq->command = command;
12899 * Both pointers should be aligned. But it is possible
12900 * that the allocation length is not. They should both
12901 * also have enough slack left over at the end, though,
12902 * to round up to the next 8 byte boundary.
12904 cur_len = MIN(local_sglist[i].len - local_used,
12905 remote_sglist[j].len - remote_used);
12906 rq->size = cur_len;
12908 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12909 tmp_ptr += local_used;
12912 /* Use physical addresses when talking to ISC hardware */
12913 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12914 /* XXX KDM use busdma */
12915 rq->local = vtophys(tmp_ptr);
12917 rq->local = tmp_ptr;
12919 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
12920 ("HA does not support BUS_ADDR"));
12921 rq->local = tmp_ptr;
12924 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12925 tmp_ptr += remote_used;
12926 rq->remote = tmp_ptr;
12928 rq->callback = NULL;
12930 local_used += cur_len;
12931 if (local_used >= local_sglist[i].len) {
12936 remote_used += cur_len;
12937 if (remote_used >= remote_sglist[j].len) {
12941 total_used += cur_len;
12943 if (total_used >= io->scsiio.kern_data_len)
12944 rq->callback = callback;
12947 printf("%s: %s: local %p remote %p size %d\n", __func__,
12948 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
12949 rq->local, rq->remote, rq->size);
12952 isc_ret = ctl_dt_single(rq);
12953 if (isc_ret > CTL_HA_STATUS_SUCCESS)
12956 if (isc_ret != CTL_HA_STATUS_WAIT) {
12965 ctl_datamove_remote_read(union ctl_io *io)
12971 * This will send an error to the other controller in the case of a
12974 retval = ctl_datamove_remote_sgl_setup(io);
12978 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12979 ctl_datamove_remote_read_cb);
12982 * Make sure we free memory if there was an error.. The
12983 * ctl_datamove_remote_xfer() function will send the
12984 * datamove done message, or call the callback with an
12985 * error if there is a problem.
12987 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12988 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12989 free(io->io_hdr.remote_sglist, M_CTL);
12990 io->io_hdr.remote_sglist = NULL;
12991 io->io_hdr.local_sglist = NULL;
12996 * Process a datamove request from the other controller. This is used for
12997 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
12998 * first. Once that is complete, the data gets DMAed into the remote
12999 * controller's memory. For reads, we DMA from the remote controller's
13000 * memory into our memory first, and then move it out to the FETD.
13003 ctl_datamove_remote(union ctl_io *io)
13006 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
13008 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
13009 ctl_failover_io(io, /*have_lock*/ 0);
13014 * Note that we look for an aborted I/O here, but don't do some of
13015 * the other checks that ctl_datamove() normally does.
13016 * We don't need to run the datamove delay code, since that should
13017 * have been done if need be on the other controller.
13019 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
13020 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__,
13021 io->scsiio.tag_num, io->io_hdr.nexus.initid,
13022 io->io_hdr.nexus.targ_port,
13023 io->io_hdr.nexus.targ_lun);
13024 io->io_hdr.port_status = 31338;
13025 ctl_send_datamove_done(io, /*have_lock*/ 0);
13029 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
13030 ctl_datamove_remote_write(io);
13031 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
13032 ctl_datamove_remote_read(io);
13034 io->io_hdr.port_status = 31339;
13035 ctl_send_datamove_done(io, /*have_lock*/ 0);
13040 ctl_process_done(union ctl_io *io)
13042 struct ctl_softc *softc = CTL_SOFTC(io);
13043 struct ctl_lun *lun = CTL_LUN(io);
13044 void (*fe_done)(union ctl_io *io);
13045 union ctl_ha_msg msg;
13046 uint32_t targ_port = io->io_hdr.nexus.targ_port;
13048 CTL_DEBUG_PRINT(("ctl_process_done\n"));
13049 fe_done = softc->ctl_ports[targ_port]->fe_done;
13052 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
13057 ctl_scsi_path_string(io, path_str, sizeof(path_str));
13058 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13060 sbuf_cat(&sb, path_str);
13061 switch (io->io_hdr.io_type) {
13063 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
13064 sbuf_printf(&sb, "\n");
13065 sbuf_cat(&sb, path_str);
13066 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13067 io->scsiio.tag_num, io->scsiio.tag_type);
13070 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
13071 "Tag Type: %d\n", io->taskio.task_action,
13072 io->taskio.tag_num, io->taskio.tag_type);
13075 panic("%s: Invalid CTL I/O type %d\n",
13076 __func__, io->io_hdr.io_type);
13078 sbuf_cat(&sb, path_str);
13079 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
13080 (intmax_t)time_uptime - io->io_hdr.start_time);
13082 printf("%s", sbuf_data(&sb));
13084 #endif /* CTL_TIME_IO */
13086 switch (io->io_hdr.io_type) {
13090 if (ctl_debug & CTL_DEBUG_INFO)
13091 ctl_io_error_print(io, NULL);
13095 panic("%s: Invalid CTL I/O type %d\n",
13096 __func__, io->io_hdr.io_type);
13100 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13101 io->io_hdr.nexus.targ_mapped_lun));
13105 mtx_lock(&lun->lun_lock);
13108 * Check to see if we have any informational exception and status
13109 * of this command can be modified to report it in form of either
13110 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field.
13112 if (lun->ie_reported == 0 && lun->ie_asc != 0 &&
13113 io->io_hdr.status == CTL_SUCCESS &&
13114 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) {
13115 uint8_t mrie = lun->MODE_IE.mrie;
13116 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) ||
13117 (lun->MODE_VER.byte3 & SMS_VER_PER));
13118 if (((mrie == SIEP_MRIE_REC_COND && per) ||
13119 mrie == SIEP_MRIE_REC_UNCOND ||
13120 mrie == SIEP_MRIE_NO_SENSE) &&
13121 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags &
13122 CTL_CMD_FLAG_NO_SENSE) == 0) {
13123 ctl_set_sense(&io->scsiio,
13124 /*current_error*/ 1,
13125 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ?
13126 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR,
13127 /*asc*/ lun->ie_asc,
13128 /*ascq*/ lun->ie_ascq,
13130 lun->ie_reported = 1;
13132 } else if (lun->ie_reported < 0)
13133 lun->ie_reported = 0;
13136 * Check to see if we have any errors to inject here. We only
13137 * inject errors for commands that don't already have errors set.
13139 if (!STAILQ_EMPTY(&lun->error_list) &&
13140 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
13141 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
13142 ctl_inject_error(lun, io);
13145 * XXX KDM how do we treat commands that aren't completed
13148 * XXX KDM should we also track I/O latency?
13150 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13151 io->io_hdr.io_type == CTL_IO_SCSI) {
13153 struct bintime cur_bt;
13157 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13159 type = CTL_STATS_READ;
13160 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13162 type = CTL_STATS_WRITE;
13164 type = CTL_STATS_NO_IO;
13166 lun->stats.ports[targ_port].bytes[type] +=
13167 io->scsiio.kern_total_len;
13168 lun->stats.ports[targ_port].operations[type]++;
13170 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
13171 &io->io_hdr.dma_bt);
13172 getbinuptime(&cur_bt);
13173 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
13174 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
13176 lun->stats.ports[targ_port].num_dmas[type] +=
13177 io->io_hdr.num_dmas;
13181 * Remove this from the OOA queue.
13183 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13185 if (TAILQ_EMPTY(&lun->ooa_queue))
13186 lun->last_busy = getsbinuptime();
13190 * Run through the blocked queue on this LUN and see if anything
13191 * has become unblocked, now that this transaction is done.
13193 ctl_check_blocked(lun);
13196 * If the LUN has been invalidated, free it if there is nothing
13197 * left on its OOA queue.
13199 if ((lun->flags & CTL_LUN_INVALID)
13200 && TAILQ_EMPTY(&lun->ooa_queue)) {
13201 mtx_unlock(&lun->lun_lock);
13202 mtx_lock(&softc->ctl_lock);
13204 mtx_unlock(&softc->ctl_lock);
13206 mtx_unlock(&lun->lun_lock);
13211 * If this command has been aborted, make sure we set the status
13212 * properly. The FETD is responsible for freeing the I/O and doing
13213 * whatever it needs to do to clean up its state.
13215 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13216 ctl_set_task_aborted(&io->scsiio);
13219 * If enabled, print command error status.
13221 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
13222 (ctl_debug & CTL_DEBUG_INFO) != 0)
13223 ctl_io_error_print(io, NULL);
13226 * Tell the FETD or the other shelf controller we're done with this
13227 * command. Note that only SCSI commands get to this point. Task
13228 * management commands are completed above.
13230 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
13231 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) {
13232 memset(&msg, 0, sizeof(msg));
13233 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13234 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
13235 msg.hdr.nexus = io->io_hdr.nexus;
13236 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13237 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data),
13246 * Front end should call this if it doesn't do autosense. When the request
13247 * sense comes back in from the initiator, we'll dequeue this and send it.
13250 ctl_queue_sense(union ctl_io *io)
13252 struct ctl_softc *softc = CTL_SOFTC(io);
13253 struct ctl_port *port = CTL_PORT(io);
13254 struct ctl_lun *lun;
13255 uint32_t initidx, targ_lun;
13257 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13259 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13262 * LUN lookup will likely move to the ctl_work_thread() once we
13263 * have our new queueing infrastructure (that doesn't put things on
13264 * a per-LUN queue initially). That is so that we can handle
13265 * things like an INQUIRY to a LUN that we don't have enabled. We
13266 * can't deal with that right now.
13267 * If we don't have a LUN for this, just toss the sense information.
13269 mtx_lock(&softc->ctl_lock);
13270 if (targ_lun >= CTL_MAX_LUNS ||
13271 (lun = softc->ctl_luns[targ_lun]) == NULL) {
13272 mtx_unlock(&softc->ctl_lock);
13275 mtx_lock(&lun->lun_lock);
13276 mtx_unlock(&softc->ctl_lock);
13279 * Already have CA set for this LUN...toss the sense information.
13281 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13282 if (ctl_is_set(lun->have_ca, initidx)) {
13283 mtx_unlock(&lun->lun_lock);
13287 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data,
13288 MIN(sizeof(lun->pending_sense[initidx]),
13289 sizeof(io->scsiio.sense_data)));
13290 ctl_set_mask(lun->have_ca, initidx);
13291 mtx_unlock(&lun->lun_lock);
13295 return (CTL_RETVAL_COMPLETE);
13300 * Primary command inlet from frontend ports. All SCSI and task I/O
13301 * requests must go through this function.
13304 ctl_queue(union ctl_io *io)
13306 struct ctl_port *port = CTL_PORT(io);
13308 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13311 io->io_hdr.start_time = time_uptime;
13312 getbinuptime(&io->io_hdr.start_bt);
13313 #endif /* CTL_TIME_IO */
13315 /* Map FE-specific LUN ID into global one. */
13316 io->io_hdr.nexus.targ_mapped_lun =
13317 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13319 switch (io->io_hdr.io_type) {
13322 if (ctl_debug & CTL_DEBUG_CDB)
13324 ctl_enqueue_incoming(io);
13327 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13331 return (CTL_RETVAL_COMPLETE);
13334 #ifdef CTL_IO_DELAY
13336 ctl_done_timer_wakeup(void *arg)
13340 io = (union ctl_io *)arg;
13343 #endif /* CTL_IO_DELAY */
13346 ctl_serseq_done(union ctl_io *io)
13348 struct ctl_lun *lun = CTL_LUN(io);;
13350 if (lun->be_lun == NULL ||
13351 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF)
13353 mtx_lock(&lun->lun_lock);
13354 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE;
13355 ctl_check_blocked(lun);
13356 mtx_unlock(&lun->lun_lock);
13360 ctl_done(union ctl_io *io)
13364 * Enable this to catch duplicate completion issues.
13367 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13368 printf("%s: type %d msg %d cdb %x iptl: "
13369 "%u:%u:%u tag 0x%04x "
13370 "flag %#x status %x\n",
13372 io->io_hdr.io_type,
13373 io->io_hdr.msg_type,
13375 io->io_hdr.nexus.initid,
13376 io->io_hdr.nexus.targ_port,
13377 io->io_hdr.nexus.targ_lun,
13378 (io->io_hdr.io_type ==
13380 io->taskio.tag_num :
13381 io->scsiio.tag_num,
13383 io->io_hdr.status);
13385 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13389 * This is an internal copy of an I/O, and should not go through
13390 * the normal done processing logic.
13392 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13395 #ifdef CTL_IO_DELAY
13396 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13397 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13399 struct ctl_lun *lun = CTL_LUN(io);
13402 && (lun->delay_info.done_delay > 0)) {
13404 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
13405 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13406 callout_reset(&io->io_hdr.delay_callout,
13407 lun->delay_info.done_delay * hz,
13408 ctl_done_timer_wakeup, io);
13409 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13410 lun->delay_info.done_delay = 0;
13414 #endif /* CTL_IO_DELAY */
13416 ctl_enqueue_done(io);
13420 ctl_work_thread(void *arg)
13422 struct ctl_thread *thr = (struct ctl_thread *)arg;
13423 struct ctl_softc *softc = thr->ctl_softc;
13427 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13431 * We handle the queues in this order:
13433 * - done queue (to free up resources, unblock other commands)
13437 * If those queues are empty, we break out of the loop and
13440 mtx_lock(&thr->queue_lock);
13441 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13443 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13444 mtx_unlock(&thr->queue_lock);
13445 ctl_handle_isc(io);
13448 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13450 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13451 /* clear any blocked commands, call fe_done */
13452 mtx_unlock(&thr->queue_lock);
13453 ctl_process_done(io);
13456 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13458 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13459 mtx_unlock(&thr->queue_lock);
13460 if (io->io_hdr.io_type == CTL_IO_TASK)
13463 ctl_scsiio_precheck(softc, &io->scsiio);
13466 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13468 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13469 mtx_unlock(&thr->queue_lock);
13470 retval = ctl_scsiio(&io->scsiio);
13471 if (retval != CTL_RETVAL_COMPLETE)
13472 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13476 /* Sleep until we have something to do. */
13477 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13482 ctl_lun_thread(void *arg)
13484 struct ctl_softc *softc = (struct ctl_softc *)arg;
13485 struct ctl_be_lun *be_lun;
13487 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13490 mtx_lock(&softc->ctl_lock);
13491 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13492 if (be_lun != NULL) {
13493 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13494 mtx_unlock(&softc->ctl_lock);
13495 ctl_create_lun(be_lun);
13499 /* Sleep until we have something to do. */
13500 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13501 PDROP | PRIBIO, "-", 0);
13506 ctl_thresh_thread(void *arg)
13508 struct ctl_softc *softc = (struct ctl_softc *)arg;
13509 struct ctl_lun *lun;
13510 struct ctl_logical_block_provisioning_page *page;
13512 union ctl_ha_msg msg;
13513 uint64_t thres, val;
13516 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13519 mtx_lock(&softc->ctl_lock);
13520 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13521 if ((lun->flags & CTL_LUN_DISABLED) ||
13522 (lun->flags & CTL_LUN_NO_MEDIA) ||
13523 lun->backend->lun_attr == NULL)
13525 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
13526 softc->ha_mode == CTL_HA_MODE_XFER)
13528 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0)
13531 page = &lun->MODE_LBP;
13532 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13533 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13535 thres = scsi_4btoul(page->descr[i].count);
13536 thres <<= CTL_LBP_EXPONENT;
13537 switch (page->descr[i].resource) {
13539 attr = "blocksavail";
13542 attr = "blocksused";
13545 attr = "poolblocksavail";
13548 attr = "poolblocksused";
13553 mtx_unlock(&softc->ctl_lock); // XXX
13554 val = lun->backend->lun_attr(
13555 lun->be_lun->be_lun, attr);
13556 mtx_lock(&softc->ctl_lock);
13557 if (val == UINT64_MAX)
13559 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13560 == SLBPPD_ARMING_INC)
13561 e = (val >= thres);
13563 e = (val <= thres);
13567 mtx_lock(&lun->lun_lock);
13569 scsi_u64to8b((uint8_t *)&page->descr[i] -
13570 (uint8_t *)page, lun->ua_tpt_info);
13571 if (lun->lasttpt == 0 ||
13572 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13573 lun->lasttpt = time_uptime;
13574 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13580 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13583 mtx_unlock(&lun->lun_lock);
13585 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
13586 /* Send msg to other side. */
13587 bzero(&msg.ua, sizeof(msg.ua));
13588 msg.hdr.msg_type = CTL_MSG_UA;
13589 msg.hdr.nexus.initid = -1;
13590 msg.hdr.nexus.targ_port = -1;
13591 msg.hdr.nexus.targ_lun = lun->lun;
13592 msg.hdr.nexus.targ_mapped_lun = lun->lun;
13594 msg.ua.ua_set = (set > 0);
13595 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES;
13596 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8);
13597 mtx_unlock(&softc->ctl_lock); // XXX
13598 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13599 sizeof(msg.ua), M_WAITOK);
13600 mtx_lock(&softc->ctl_lock);
13603 mtx_unlock(&softc->ctl_lock);
13604 pause("-", CTL_LBP_PERIOD * hz);
13609 ctl_enqueue_incoming(union ctl_io *io)
13611 struct ctl_softc *softc = CTL_SOFTC(io);
13612 struct ctl_thread *thr;
13615 idx = (io->io_hdr.nexus.targ_port * 127 +
13616 io->io_hdr.nexus.initid) % worker_threads;
13617 thr = &softc->threads[idx];
13618 mtx_lock(&thr->queue_lock);
13619 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13620 mtx_unlock(&thr->queue_lock);
13625 ctl_enqueue_rtr(union ctl_io *io)
13627 struct ctl_softc *softc = CTL_SOFTC(io);
13628 struct ctl_thread *thr;
13630 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13631 mtx_lock(&thr->queue_lock);
13632 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13633 mtx_unlock(&thr->queue_lock);
13638 ctl_enqueue_done(union ctl_io *io)
13640 struct ctl_softc *softc = CTL_SOFTC(io);
13641 struct ctl_thread *thr;
13643 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13644 mtx_lock(&thr->queue_lock);
13645 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13646 mtx_unlock(&thr->queue_lock);
13651 ctl_enqueue_isc(union ctl_io *io)
13653 struct ctl_softc *softc = CTL_SOFTC(io);
13654 struct ctl_thread *thr;
13656 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13657 mtx_lock(&thr->queue_lock);
13658 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13659 mtx_unlock(&thr->queue_lock);